summaryrefslogtreecommitdiff
path: root/storage/innobase/row/row0merge.cc
blob: 53e3016180aed899a056860f7beb5b8c4e5a5d83 (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
/*****************************************************************************

Copyright (c) 2005, 2017, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2014, 2021, MariaDB Corporation.

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 Street, Fifth Floor, Boston, MA 02110-1335 USA

*****************************************************************************/

/**************************************************//**
@file row/row0merge.cc
New index creation routines using a merge sort

Created 12/4/2005 Jan Lindstrom
Completed by Sunny Bains and Marko Makela
*******************************************************/
#include <my_global.h>
#include <log.h>
#include <sql_class.h>
#include <math.h>

#include "row0merge.h"
#include "row0ext.h"
#include "row0log.h"
#include "row0ins.h"
#include "row0row.h"
#include "row0sel.h"
#include "log0crypt.h"
#include "dict0crea.h"
#include "trx0purge.h"
#include "lock0lock.h"
#include "pars0pars.h"
#include "ut0sort.h"
#include "row0ftsort.h"
#include "row0import.h"
#include "row0vers.h"
#include "handler0alter.h"
#include "btr0bulk.h"
#ifdef BTR_CUR_ADAPT
# include "btr0sea.h"
#endif /* BTR_CUR_ADAPT */
#include "ut0stage.h"
#include "fil0crypt.h"
#include "srv0mon.h"

/* Ignore posix_fadvise() on those platforms where it does not exist */
#if defined _WIN32
# define posix_fadvise(fd, offset, len, advice) /* nothing */
#endif /* _WIN32 */

/* Whether to disable file system cache */
char	srv_disable_sort_file_cache;

/** Class that caches index row tuples made from a single cluster
index page scan, and then insert into corresponding index tree */
class index_tuple_info_t {
public:
	/** constructor
	@param[in]	heap	memory heap
	@param[in]	index	index to be created */
	index_tuple_info_t(mem_heap_t* heap, dict_index_t* index) :
		m_dtuple_vec(UT_NEW_NOKEY(idx_tuple_vec())),
		m_index(index), m_heap(heap)
	{ ut_ad(index->is_spatial()); }

	/** destructor */
	~index_tuple_info_t()
	{
		UT_DELETE(m_dtuple_vec);
	}

	/** Get the index object
	@return the index object */
	dict_index_t*   get_index() UNIV_NOTHROW
	{
		return(m_index);
	}

	/** Caches an index row into index tuple vector
	@param[in]	row	table row
	@param[in]	ext	externally stored column
	prefixes, or NULL */
	void add(
		const dtuple_t*		row,
		const row_ext_t*	ext) UNIV_NOTHROW
	{
		dtuple_t*	dtuple;

		dtuple = row_build_index_entry(row, ext, m_index, m_heap);

		ut_ad(dtuple);

		m_dtuple_vec->push_back(dtuple);
	}

	/** Insert spatial index rows cached in vector into spatial index
	@param[in]	trx_id		transaction id
	@param[in,out]	row_heap	memory heap
	@param[in]	pcur		cluster index scanning cursor
	@param[in,out]	mtr_started	whether scan_mtr is active
	@param[in,out]	scan_mtr	mini-transaction for pcur
	@return DB_SUCCESS if successful, else error number */
	dberr_t insert(trx_id_t trx_id, mem_heap_t* row_heap, btr_pcur_t* pcur,
		       bool& mtr_started, mtr_t* scan_mtr) const
	{
		big_rec_t*      big_rec;
		rec_t*          rec;
		btr_cur_t       ins_cur;
		mtr_t           mtr;
		rtr_info_t      rtr_info;
		rec_offs*	ins_offsets = NULL;
		dberr_t		error = DB_SUCCESS;
		dtuple_t*	dtuple;
		ulint		count = 0;
		const ulint	flag = BTR_NO_UNDO_LOG_FLAG
				       | BTR_NO_LOCKING_FLAG
				       | BTR_KEEP_SYS_FLAG | BTR_CREATE_FLAG;

		ut_ad(mtr_started == scan_mtr->is_active());

		DBUG_EXECUTE_IF("row_merge_instrument_log_check_flush",
				log_sys.set_check_flush_or_checkpoint(););

		for (idx_tuple_vec::iterator it = m_dtuple_vec->begin();
		     it != m_dtuple_vec->end();
		     ++it) {
			dtuple = *it;
			ut_ad(dtuple);

			if (log_sys.check_flush_or_checkpoint()) {
				if (mtr_started) {
					btr_pcur_move_to_prev_on_page(pcur);
					btr_pcur_store_position(pcur, scan_mtr);
					scan_mtr->commit();
					mtr_started = false;
				}

				log_free_check();
			}

			mtr.start();
			m_index->set_modified(mtr);

			ins_cur.index = m_index;
			rtr_init_rtr_info(&rtr_info, false, &ins_cur, m_index,
					  false);
			rtr_info_update_btr(&ins_cur, &rtr_info);

			btr_cur_search_to_nth_level(m_index, 0, dtuple,
						    PAGE_CUR_RTREE_INSERT,
						    BTR_MODIFY_LEAF, &ins_cur,
						    0, &mtr);

			/* It need to update MBR in parent entry,
			so change search mode to BTR_MODIFY_TREE */
			if (rtr_info.mbr_adj) {
				mtr_commit(&mtr);
				rtr_clean_rtr_info(&rtr_info, true);
				rtr_init_rtr_info(&rtr_info, false, &ins_cur,
						  m_index, false);
				rtr_info_update_btr(&ins_cur, &rtr_info);
				mtr_start(&mtr);
				m_index->set_modified(mtr);
				btr_cur_search_to_nth_level(
					m_index, 0, dtuple,
					PAGE_CUR_RTREE_INSERT,
					BTR_MODIFY_TREE, &ins_cur, 0, &mtr);
			}

			error = btr_cur_optimistic_insert(
				flag, &ins_cur, &ins_offsets, &row_heap,
				dtuple, &rec, &big_rec, 0, NULL, &mtr);

			if (error == DB_FAIL) {
				ut_ad(!big_rec);
				mtr.commit();
				mtr.start();
				m_index->set_modified(mtr);

				rtr_clean_rtr_info(&rtr_info, true);
				rtr_init_rtr_info(&rtr_info, false,
						  &ins_cur, m_index, false);

				rtr_info_update_btr(&ins_cur, &rtr_info);
				btr_cur_search_to_nth_level(
					m_index, 0, dtuple,
					PAGE_CUR_RTREE_INSERT,
					BTR_MODIFY_TREE,
					&ins_cur, 0, &mtr);

				error = btr_cur_pessimistic_insert(
						flag, &ins_cur, &ins_offsets,
						&row_heap, dtuple, &rec,
						&big_rec, 0, NULL, &mtr);
			}

			DBUG_EXECUTE_IF(
				"row_merge_ins_spatial_fail",
				error = DB_FAIL;
			);

			if (error == DB_SUCCESS) {
				if (rtr_info.mbr_adj) {
					error = rtr_ins_enlarge_mbr(
							&ins_cur, &mtr);
				}

				if (error == DB_SUCCESS) {
					page_update_max_trx_id(
						btr_cur_get_block(&ins_cur),
						btr_cur_get_page_zip(&ins_cur),
						trx_id, &mtr);
				}
			}

			mtr_commit(&mtr);

			rtr_clean_rtr_info(&rtr_info, true);
			count++;
		}

		m_dtuple_vec->clear();

		return(error);
	}

private:
	/** Cache index rows made from a cluster index scan. Usually
	for rows on single cluster index page */
	typedef std::vector<dtuple_t*, ut_allocator<dtuple_t*> >
		idx_tuple_vec;

	/** vector used to cache index rows made from cluster index scan */
	idx_tuple_vec* const	m_dtuple_vec;

	/** the index being built */
	dict_index_t* const	m_index;

	/** memory heap for creating index tuples */
	mem_heap_t* const	m_heap;
};

/* Maximum pending doc memory limit in bytes for a fts tokenization thread */
#define FTS_PENDING_DOC_MEMORY_LIMIT	1000000

/** Insert sorted data tuples to the index.
@param[in]	index		index to be inserted
@param[in]	old_table	old table
@param[in]	fd		file descriptor
@param[in,out]	block		file buffer
@param[in]	row_buf		row_buf the sorted data tuples,
or NULL if fd, block will be used instead
@param[in,out]	btr_bulk	btr bulk instance
@param[in,out]	stage		performance schema accounting object, used by
ALTER TABLE. If not NULL stage->begin_phase_insert() will be called initially
and then stage->inc() will be called for each record that is processed.
@return DB_SUCCESS or error number */
static	MY_ATTRIBUTE((warn_unused_result))
dberr_t
row_merge_insert_index_tuples(
	dict_index_t*		index,
	const dict_table_t*	old_table,
	const pfs_os_file_t&	fd,
	row_merge_block_t*	block,
	const row_merge_buf_t*	row_buf,
	BtrBulk*		btr_bulk,
	const ib_uint64_t	table_total_rows, /*!< in: total rows of old table */
	const double		pct_progress,	/*!< in: total progress
						percent until now */
	const double		pct_cost, /*!< in: current progress percent
					  */
	row_merge_block_t*	crypt_block, /*!< in: crypt buf or NULL */
	ulint			space,	   /*!< in: space id */
	ut_stage_alter_t*	stage = NULL);

/******************************************************//**
Encode an index record. */
static MY_ATTRIBUTE((nonnull))
void
row_merge_buf_encode(
/*=================*/
	byte**			b,		/*!< in/out: pointer to
						current end of output buffer */
	const dict_index_t*	index,		/*!< in: index */
	const mtuple_t*		entry,		/*!< in: index fields
						of the record to encode */
	ulint			n_fields)	/*!< in: number of fields
						in the entry */
{
	ulint	size;
	ulint	extra_size;

	size = rec_get_converted_size_temp<false>(
		index, entry->fields, n_fields, &extra_size);
	ut_ad(size >= extra_size);

	/* Encode extra_size + 1 */
	if (extra_size + 1 < 0x80) {
		*(*b)++ = (byte) (extra_size + 1);
	} else {
		ut_ad((extra_size + 1) < 0x8000);
		*(*b)++ = (byte) (0x80 | ((extra_size + 1) >> 8));
		*(*b)++ = (byte) (extra_size + 1);
	}

	rec_convert_dtuple_to_temp<false>(*b + extra_size, index,
				   entry->fields, n_fields);

	*b += size;
}

/******************************************************//**
Allocate a sort buffer.
@return own: sort buffer */
static MY_ATTRIBUTE((malloc, nonnull))
row_merge_buf_t*
row_merge_buf_create_low(
/*=====================*/
	mem_heap_t*	heap,		/*!< in: heap where allocated */
	dict_index_t*	index,		/*!< in: secondary index */
	ulint		max_tuples,	/*!< in: maximum number of
					data tuples */
	ulint		buf_size)	/*!< in: size of the buffer,
					in bytes */
{
	row_merge_buf_t*	buf;

	ut_ad(max_tuples > 0);

	ut_ad(max_tuples <= srv_sort_buf_size);

	buf = static_cast<row_merge_buf_t*>(mem_heap_zalloc(heap, buf_size));
	buf->heap = heap;
	buf->index = index;
	buf->max_tuples = max_tuples;
	buf->tuples = static_cast<mtuple_t*>(
		ut_malloc_nokey(2 * max_tuples * sizeof *buf->tuples));
	buf->tmp_tuples = buf->tuples + max_tuples;

	return(buf);
}

/******************************************************//**
Allocate a sort buffer.
@return own: sort buffer */
row_merge_buf_t*
row_merge_buf_create(
/*=================*/
	dict_index_t*	index)	/*!< in: secondary index */
{
	row_merge_buf_t*	buf;
	ulint			max_tuples;
	ulint			buf_size;
	mem_heap_t*		heap;

	max_tuples = srv_sort_buf_size
		/ std::max<ulint>(1, dict_index_get_min_size(index));

	buf_size = (sizeof *buf);

	heap = mem_heap_create(buf_size);

	buf = row_merge_buf_create_low(heap, index, max_tuples, buf_size);

	return(buf);
}

/******************************************************//**
Empty a sort buffer.
@return sort buffer */
row_merge_buf_t*
row_merge_buf_empty(
/*================*/
	row_merge_buf_t*	buf)	/*!< in,own: sort buffer */
{
	ulint		buf_size	= sizeof *buf;
	ulint		max_tuples	= buf->max_tuples;
	mem_heap_t*	heap		= buf->heap;
	dict_index_t*	index		= buf->index;
	mtuple_t*	tuples		= buf->tuples;

	mem_heap_empty(heap);

	buf = static_cast<row_merge_buf_t*>(mem_heap_zalloc(heap, buf_size));
	buf->heap = heap;
	buf->index = index;
	buf->max_tuples = max_tuples;
	buf->tuples = tuples;
	buf->tmp_tuples = buf->tuples + max_tuples;

	return(buf);
}

/******************************************************//**
Deallocate a sort buffer. */
void
row_merge_buf_free(
/*===============*/
	row_merge_buf_t*	buf)	/*!< in,own: sort buffer to be freed */
{
	ut_free(buf->tuples);
	mem_heap_free(buf->heap);
}

/** Convert the field data from compact to redundant format.
@param[in]	row_field	field to copy from
@param[out]	field		field to copy to
@param[in]	len		length of the field data
@param[in]	zip_size	compressed BLOB page size,
				zero for uncompressed BLOBs
@param[in,out]	heap		memory heap where to allocate data when
				converting to ROW_FORMAT=REDUNDANT, or NULL
				when not to invoke
				row_merge_buf_redundant_convert(). */
static
void
row_merge_buf_redundant_convert(
	const dfield_t*		row_field,
	dfield_t*		field,
	ulint			len,
	ulint			zip_size,
	mem_heap_t*		heap)
{
	ut_ad(field->type.mbminlen == 1);
	ut_ad(field->type.mbmaxlen > 1);

	byte*		buf = (byte*) mem_heap_alloc(heap, len);
	ulint		field_len = row_field->len;
	ut_ad(field_len <= len);

	if (row_field->ext) {
		const byte*	field_data = static_cast<const byte*>(
			dfield_get_data(row_field));
		ulint		ext_len;

		ut_a(field_len >= BTR_EXTERN_FIELD_REF_SIZE);
		ut_a(memcmp(field_data + field_len - BTR_EXTERN_FIELD_REF_SIZE,
			    field_ref_zero, BTR_EXTERN_FIELD_REF_SIZE));

		byte*	data = btr_copy_externally_stored_field(
			&ext_len, field_data, zip_size, field_len, heap);

		ut_ad(ext_len < len);

		memcpy(buf, data, ext_len);
		field_len = ext_len;
	} else {
		memcpy(buf, row_field->data, field_len);
	}

	memset(buf + field_len, 0x20, len - field_len);

	dfield_set_data(field, buf, len);
}

/** Insert a data tuple into a sort buffer.
@param[in,out]	buf		sort buffer
@param[in]	fts_index	fts index to be created
@param[in]	old_table	original table
@param[in]	new_table	new table
@param[in,out]	psort_info	parallel sort info
@param[in,out]	row		table row
@param[in]	ext		cache of externally stored
				column prefixes, or NULL
@param[in,out]	doc_id		Doc ID if we are creating
				FTS index
@param[in,out]	conv_heap	memory heap where to allocate data when
				converting to ROW_FORMAT=REDUNDANT, or NULL
				when not to invoke
				row_merge_buf_redundant_convert()
@param[in,out]	err		set if error occurs
@param[in,out]	v_heap		heap memory to process data for virtual column
@param[in,out]	my_table	mysql table object
@param[in]	trx		transaction object
@return number of rows added, 0 if out of space */
static
ulint
row_merge_buf_add(
	row_merge_buf_t*	buf,
	dict_index_t*		fts_index,
	const dict_table_t*	old_table,
	const dict_table_t*	new_table,
	fts_psort_t*		psort_info,
	dtuple_t*		row,
	const row_ext_t*	ext,
	doc_id_t*		doc_id,
	mem_heap_t*		conv_heap,
	dberr_t*		err,
	mem_heap_t**		v_heap,
	TABLE*			my_table,
	trx_t*			trx)
{
	ulint			i;
	const dict_index_t*	index;
	mtuple_t*		entry;
	dfield_t*		field;
	const dict_field_t*	ifield;
	ulint			n_fields;
	ulint			data_size;
	ulint			extra_size;
	ulint			bucket = 0;
	doc_id_t		write_doc_id;
	ulint			n_row_added = 0;
	VCOL_STORAGE		vcol_storage;
	DBUG_ENTER("row_merge_buf_add");

	if (buf->n_tuples >= buf->max_tuples) {
error:
		n_row_added = 0;
		goto end;
	}

	DBUG_EXECUTE_IF(
		"ib_row_merge_buf_add_two",
		if (buf->n_tuples >= 2) DBUG_RETURN(0););

	UNIV_PREFETCH_R(row->fields);

	/* If we are building FTS index, buf->index points to
	the 'fts_sort_idx', and real FTS index is stored in
	fts_index */
	index = (buf->index->type & DICT_FTS) ? fts_index : buf->index;

	/* create spatial index should not come here */
	ut_ad(!dict_index_is_spatial(index));

	n_fields = dict_index_get_n_fields(index);

	entry = &buf->tuples[buf->n_tuples];
	field = entry->fields = static_cast<dfield_t*>(
		mem_heap_alloc(buf->heap, n_fields * sizeof *entry->fields));

	data_size = 0;
	extra_size = UT_BITS_IN_BYTES(unsigned(index->n_nullable));

	ifield = dict_index_get_nth_field(index, 0);

	for (i = 0; i < n_fields; i++, field++, ifield++) {
		ulint			len;
		ulint			fixed_len;
		const dfield_t*		row_field;
		const dict_col_t* const col = ifield->col;
		const dict_v_col_t* const v_col = col->is_virtual()
			? reinterpret_cast<const dict_v_col_t*>(col)
			: NULL;

		/* Process the Doc ID column */
		if (!v_col && *doc_id
		    && col->ind == index->table->fts->doc_col) {
			fts_write_doc_id((byte*) &write_doc_id, *doc_id);

			/* Note: field->data now points to a value on the
			stack: &write_doc_id after dfield_set_data(). Because
			there is only one doc_id per row, it shouldn't matter.
			We allocate a new buffer before we leave the function
			later below. */

			dfield_set_data(
				field, &write_doc_id, sizeof(write_doc_id));

			field->type.mtype = ifield->col->mtype;
			field->type.prtype = ifield->col->prtype;
			field->type.mbminlen = 0;
			field->type.mbmaxlen = 0;
			field->type.len = ifield->col->len;
		} else {
			/* Use callback to get the virtual column value */
			if (v_col) {
				dict_index_t*	clust_index
					= dict_table_get_first_index(new_table);

                                if (!vcol_storage.innobase_record &&
                                    !innobase_allocate_row_for_vcol(
						trx->mysql_thd, clust_index,
						v_heap, &my_table,
						&vcol_storage)) {
					*err = DB_OUT_OF_MEMORY;
					goto error;
				}

				row_field = innobase_get_computed_value(
					row, v_col, clust_index,
					v_heap, NULL, ifield, trx->mysql_thd,
					my_table, vcol_storage.innobase_record, 
					old_table, NULL, NULL);

				if (row_field == NULL) {
					*err = DB_COMPUTE_VALUE_FAILED;
					goto error;
				}
				dfield_copy(field, row_field);
			} else {
				row_field = dtuple_get_nth_field(row,
								 col->ind);
				dfield_copy(field, row_field);
			}


			/* Tokenize and process data for FTS */
			if (index->type & DICT_FTS) {
				fts_doc_item_t*	doc_item;
				byte*		value;
				void*		ptr;
				const ulint	max_trial_count = 10000;
				ulint		trial_count = 0;

				/* fetch Doc ID if it already exists
				in the row, and not supplied by the
				caller. Even if the value column is
				NULL, we still need to get the Doc
				ID so to maintain the correct max
				Doc ID */
				if (*doc_id == 0) {
					const dfield_t*	doc_field;
					doc_field = dtuple_get_nth_field(
						row,
						index->table->fts->doc_col);
					*doc_id = (doc_id_t) mach_read_from_8(
						static_cast<const byte*>(
						dfield_get_data(doc_field)));

					if (*doc_id == 0) {
						ib::warn() << "FTS Doc ID is"
							" zero. Record"
							" skipped";
						goto error;
					}
				}

				if (dfield_is_null(field)) {
					n_row_added = 1;
					continue;
				}

				ptr = ut_malloc_nokey(sizeof(*doc_item)
						      + field->len);

				doc_item = static_cast<fts_doc_item_t*>(ptr);
				value = static_cast<byte*>(ptr)
					+ sizeof(*doc_item);
				memcpy(value, field->data, field->len);
				field->data = value;

				doc_item->field = field;
				doc_item->doc_id = *doc_id;

				bucket = static_cast<ulint>(
					*doc_id % fts_sort_pll_degree);

				/* Add doc item to fts_doc_list */
				mysql_mutex_lock(&psort_info[bucket].mutex);

				if (psort_info[bucket].error == DB_SUCCESS) {
					UT_LIST_ADD_LAST(
						psort_info[bucket].fts_doc_list,
						doc_item);
					psort_info[bucket].memory_used +=
						sizeof(*doc_item) + field->len;
				} else {
					ut_free(doc_item);
				}

				mysql_mutex_unlock(&psort_info[bucket].mutex);

				/* Sleep when memory used exceeds limit*/
				while (psort_info[bucket].memory_used
				       > FTS_PENDING_DOC_MEMORY_LIMIT
				       && trial_count++ < max_trial_count) {
					std::this_thread::sleep_for(
						std::chrono::milliseconds(1));
				}

				n_row_added = 1;
				continue;
			}

			/* innobase_get_computed_value() sets the
			length of the virtual column field. */
			if (v_col == NULL
			    && field->len != UNIV_SQL_NULL
			    && col->mtype == DATA_MYSQL
			    && col->len != field->len) {
				if (conv_heap != NULL) {
					row_merge_buf_redundant_convert(
						row_field, field, col->len,
						old_table->space->zip_size(),
						conv_heap);
				} else {
					/* Field length mismatch should not
					happen when rebuilding redundant row
					format table. */
					ut_ad(index->table->not_redundant());
				}
			}
		}

		len = dfield_get_len(field);

		if (dfield_is_null(field)) {
			ut_ad(!(col->prtype & DATA_NOT_NULL));
			continue;
		} else if (!ext) {
		} else if (dict_index_is_clust(index)) {
			/* Flag externally stored fields. */
			const byte*	buf = row_ext_lookup(ext, col->ind,
							     &len);
			if (UNIV_LIKELY_NULL(buf)) {
				ut_a(buf != field_ref_zero);
				if (i < dict_index_get_n_unique(index)) {
					dfield_set_data(field, buf, len);
				} else {
					dfield_set_ext(field);
					len = dfield_get_len(field);
				}
			}
		} else if (!v_col) {
			/* Only non-virtual column are stored externally */
			const byte*	buf = row_ext_lookup(ext, col->ind,
							     &len);
			if (UNIV_LIKELY_NULL(buf)) {
				ut_a(buf != field_ref_zero);
				dfield_set_data(field, buf, len);
			}
		}

		/* If a column prefix index, take only the prefix */

		if (ifield->prefix_len) {
			len = dtype_get_at_most_n_mbchars(
				col->prtype,
				col->mbminlen, col->mbmaxlen,
				ifield->prefix_len,
				len,
				static_cast<char*>(dfield_get_data(field)));
			dfield_set_len(field, len);
		}

		ut_ad(len <= col->len
		      || DATA_LARGE_MTYPE(col->mtype));

		fixed_len = ifield->fixed_len;
		if (fixed_len && !dict_table_is_comp(index->table)
		    && col->mbminlen != col->mbmaxlen) {
			/* CHAR in ROW_FORMAT=REDUNDANT is always
			fixed-length, but in the temporary file it is
			variable-length for variable-length character
			sets. */
			fixed_len = 0;
		}

		if (fixed_len) {
#ifdef UNIV_DEBUG
			/* len should be between size calcualted base on
			mbmaxlen and mbminlen */
			ut_ad(len <= fixed_len);
			ut_ad(!col->mbmaxlen || len >= col->mbminlen
			      * (fixed_len / col->mbmaxlen));

			ut_ad(!dfield_is_ext(field));
#endif /* UNIV_DEBUG */
		} else if (dfield_is_ext(field)) {
			extra_size += 2;
		} else if (len < 128
			   || (!DATA_BIG_COL(col))) {
			extra_size++;
		} else {
			/* For variable-length columns, we look up the
			maximum length from the column itself.  If this
			is a prefix index column shorter than 256 bytes,
			this will waste one byte. */
			extra_size += 2;
		}
		data_size += len;
	}

	/* If this is FTS index, we already populated the sort buffer, return
	here */
	if (index->type & DICT_FTS) {
		goto end;
	}

#ifdef UNIV_DEBUG
	{
		ulint	size;
		ulint	extra;

		size = rec_get_converted_size_temp<false>(
			index, entry->fields, n_fields, &extra);

		ut_ad(data_size + extra_size == size);
		ut_ad(extra_size == extra);
	}
#endif /* UNIV_DEBUG */

	/* Add to the total size of the record in row_merge_block_t
	the encoded length of extra_size and the extra bytes (extra_size).
	See row_merge_buf_write() for the variable-length encoding
	of extra_size. */
	data_size += (extra_size + 1) + ((extra_size + 1) >= 0x80);

	/* Record size can exceed page size while converting to
	redundant row format. But there is assert
	ut_ad(size < srv_page_size) in rec_offs_data_size().
	It may hit the assert before attempting to insert the row. */
	if (conv_heap != NULL && data_size > srv_page_size) {
		*err = DB_TOO_BIG_RECORD;
	}

	ut_ad(data_size < srv_sort_buf_size);

	/* Reserve bytes for the end marker of row_merge_block_t. */
	if (buf->total_size + data_size >= srv_sort_buf_size) {
		goto error;
	}

	buf->total_size += data_size;
	buf->n_tuples++;
	n_row_added++;

	field = entry->fields;

	/* Copy the data fields. */

	do {
		dfield_dup(field++, buf->heap);
	} while (--n_fields);

	if (conv_heap != NULL) {
		mem_heap_empty(conv_heap);
	}

end:
        if (vcol_storage.innobase_record)
		innobase_free_row_for_vcol(&vcol_storage);
	DBUG_RETURN(n_row_added);
}

/*************************************************************//**
Report a duplicate key. */
void
row_merge_dup_report(
/*=================*/
	row_merge_dup_t*	dup,	/*!< in/out: for reporting duplicates */
	const dfield_t*		entry)	/*!< in: duplicate index entry */
{
	if (!dup->n_dup++) {
		/* Only report the first duplicate record,
		but count all duplicate records. */
		innobase_fields_to_mysql(dup->table, dup->index, entry);
	}
}

/*************************************************************//**
Compare two tuples.
@return positive, 0, negative if a is greater, equal, less, than b,
respectively */
static MY_ATTRIBUTE((warn_unused_result))
int
row_merge_tuple_cmp(
/*================*/
	ulint			n_uniq,	/*!< in: number of unique fields */
	ulint			n_field,/*!< in: number of fields */
	const mtuple_t&		a,	/*!< in: first tuple to be compared */
	const mtuple_t&		b,	/*!< in: second tuple to be compared */
	row_merge_dup_t*	dup)	/*!< in/out: for reporting duplicates,
					NULL if non-unique index */
{
	int		cmp;
	const dfield_t*	af	= a.fields;
	const dfield_t*	bf	= b.fields;
	ulint		n	= n_uniq;

	ut_ad(n_uniq > 0);
	ut_ad(n_uniq <= n_field);

	/* Compare the fields of the tuples until a difference is
	found or we run out of fields to compare.  If !cmp at the
	end, the tuples are equal. */
	do {
		cmp = cmp_dfield_dfield(af++, bf++);
	} while (!cmp && --n);

	if (cmp) {
		return(cmp);
	}

	if (dup) {
		/* Report a duplicate value error if the tuples are
		logically equal.  NULL columns are logically inequal,
		although they are equal in the sorting order.  Find
		out if any of the fields are NULL. */
		for (const dfield_t* df = a.fields; df != af; df++) {
			if (dfield_is_null(df)) {
				goto no_report;
			}
		}

		row_merge_dup_report(dup, a.fields);
	}

no_report:
	/* The n_uniq fields were equal, but we compare all fields so
	that we will get the same (internal) order as in the B-tree. */
	for (n = n_field - n_uniq + 1; --n; ) {
		cmp = cmp_dfield_dfield(af++, bf++);
		if (cmp) {
			return(cmp);
		}
	}

	/* This should never be reached, except in a secondary index
	when creating a secondary index and a PRIMARY KEY, and there
	is a duplicate in the PRIMARY KEY that has not been detected
	yet. Internally, an index must never contain duplicates. */
	return(cmp);
}

/** Wrapper for row_merge_tuple_sort() to inject some more context to
UT_SORT_FUNCTION_BODY().
@param tuples array of tuples that being sorted
@param aux work area, same size as tuples[]
@param low lower bound of the sorting area, inclusive
@param high upper bound of the sorting area, inclusive */
#define row_merge_tuple_sort_ctx(tuples, aux, low, high)		\
	row_merge_tuple_sort(n_uniq, n_field, dup, tuples, aux, low, high)
/** Wrapper for row_merge_tuple_cmp() to inject some more context to
UT_SORT_FUNCTION_BODY().
@param a first tuple to be compared
@param b second tuple to be compared
@return positive, 0, negative, if a is greater, equal, less, than b,
respectively */
#define row_merge_tuple_cmp_ctx(a,b)			\
	row_merge_tuple_cmp(n_uniq, n_field, a, b, dup)

/**********************************************************************//**
Merge sort the tuple buffer in main memory. */
static
void
row_merge_tuple_sort(
/*=================*/
	ulint			n_uniq,	/*!< in: number of unique fields */
	ulint			n_field,/*!< in: number of fields */
	row_merge_dup_t*	dup,	/*!< in/out: reporter of duplicates
					(NULL if non-unique index) */
	mtuple_t*		tuples,	/*!< in/out: tuples */
	mtuple_t*		aux,	/*!< in/out: work area */
	ulint			low,	/*!< in: lower bound of the
					sorting area, inclusive */
	ulint			high)	/*!< in: upper bound of the
					sorting area, exclusive */
{
	ut_ad(n_field > 0);
	ut_ad(n_uniq <= n_field);

	UT_SORT_FUNCTION_BODY(row_merge_tuple_sort_ctx,
			      tuples, aux, low, high, row_merge_tuple_cmp_ctx);
}

/******************************************************//**
Sort a buffer. */
void
row_merge_buf_sort(
/*===============*/
	row_merge_buf_t*	buf,	/*!< in/out: sort buffer */
	row_merge_dup_t*	dup)	/*!< in/out: reporter of duplicates
					(NULL if non-unique index) */
{
	ut_ad(!dict_index_is_spatial(buf->index));

	row_merge_tuple_sort(dict_index_get_n_unique(buf->index),
			     dict_index_get_n_fields(buf->index),
			     dup,
			     buf->tuples, buf->tmp_tuples, 0, buf->n_tuples);
}

/******************************************************//**
Write a buffer to a block. */
void
row_merge_buf_write(
/*================*/
	const row_merge_buf_t*	buf,	/*!< in: sorted buffer */
	const merge_file_t*	of UNIV_UNUSED,
					/*!< in: output file */
	row_merge_block_t*	block)	/*!< out: buffer for writing to file */
{
	const dict_index_t*	index	= buf->index;
	ulint			n_fields= dict_index_get_n_fields(index);
	byte*			b	= &block[0];

	DBUG_ENTER("row_merge_buf_write");

	for (ulint i = 0; i < buf->n_tuples; i++) {
		const mtuple_t*	entry	= &buf->tuples[i];

		row_merge_buf_encode(&b, index, entry, n_fields);
		ut_ad(b < &block[srv_sort_buf_size]);

		DBUG_LOG("ib_merge_sort",
			 reinterpret_cast<const void*>(b) << ','
			 << of->fd << ',' << of->offset << ' ' <<
			 i << ": " <<
			 rec_printer(entry->fields, n_fields).str());
	}

	/* Write an "end-of-chunk" marker. */
	ut_a(b < &block[srv_sort_buf_size]);
	ut_a(b == &block[0] + buf->total_size);
	*b++ = 0;
#ifdef HAVE_valgrind
	/* The rest of the block is uninitialized.  Initialize it
	to avoid bogus warnings. */
	memset(b, 0xff, &block[srv_sort_buf_size] - b);
#endif /* HAVE_valgrind */
	DBUG_LOG("ib_merge_sort",
		 "write " << reinterpret_cast<const void*>(b) << ','
		 << of->fd << ',' << of->offset << " EOF");
	DBUG_VOID_RETURN;
}

/******************************************************//**
Create a memory heap and allocate space for row_merge_rec_offsets()
and mrec_buf_t[3].
@return memory heap */
static
mem_heap_t*
row_merge_heap_create(
/*==================*/
	const dict_index_t*	index,		/*!< in: record descriptor */
	mrec_buf_t**		buf,		/*!< out: 3 buffers */
	rec_offs**		offsets1,	/*!< out: offsets */
	rec_offs**		offsets2)	/*!< out: offsets */
{
	ulint		i	= 1 + REC_OFFS_HEADER_SIZE
		+ dict_index_get_n_fields(index);
	mem_heap_t*	heap	= mem_heap_create(2 * i * sizeof **offsets1
						  + 3 * sizeof **buf);

	*buf = static_cast<mrec_buf_t*>(
		mem_heap_alloc(heap, 3 * sizeof **buf));
	*offsets1 = static_cast<rec_offs*>(
		mem_heap_alloc(heap, i * sizeof **offsets1));
	*offsets2 = static_cast<rec_offs*>(
		mem_heap_alloc(heap, i * sizeof **offsets2));

	rec_offs_set_n_alloc(*offsets1, i);
	rec_offs_set_n_alloc(*offsets2, i);
	rec_offs_set_n_fields(*offsets1, dict_index_get_n_fields(index));
	rec_offs_set_n_fields(*offsets2, dict_index_get_n_fields(index));

	return(heap);
}

/** Read a merge block from the file system.
@return whether the request was completed successfully */
bool
row_merge_read(
/*===========*/
	const pfs_os_file_t&	fd,	/*!< in: file descriptor */
	ulint			offset,	/*!< in: offset where to read
					in number of row_merge_block_t
					elements */
	row_merge_block_t*	buf,	/*!< out: data */
	row_merge_block_t*	crypt_buf, /*!< in: crypt buf or NULL */
	ulint			space)		/*!< in: space id */
{
	os_offset_t	ofs = ((os_offset_t) offset) * srv_sort_buf_size;

	DBUG_ENTER("row_merge_read");
	DBUG_LOG("ib_merge_sort", "fd=" << fd << " ofs=" << ofs);
	DBUG_EXECUTE_IF("row_merge_read_failure", DBUG_RETURN(FALSE););

	const bool	success = DB_SUCCESS == os_file_read_no_error_handling(
		IORequestRead, fd, buf, ofs, srv_sort_buf_size, 0);

	/* If encryption is enabled decrypt buffer */
	if (success && log_tmp_is_encrypted()) {
		if (!log_tmp_block_decrypt(buf, srv_sort_buf_size,
					   crypt_buf, ofs)) {
			return (FALSE);
		}

		srv_stats.n_merge_blocks_decrypted.inc();
		memcpy(buf, crypt_buf, srv_sort_buf_size);
	}

#ifdef POSIX_FADV_DONTNEED
	/* Each block is read exactly once.  Free up the file cache. */
	posix_fadvise(fd, ofs, srv_sort_buf_size, POSIX_FADV_DONTNEED);
#endif /* POSIX_FADV_DONTNEED */

	if (!success) {
		ib::error() << "Failed to read merge block at " << ofs;
	}

	DBUG_RETURN(success);
}

/********************************************************************//**
Write a merge block to the file system.
@return whether the request was completed successfully
@retval	false	on error
@retval	true	on success */
bool
row_merge_write(
	const pfs_os_file_t&	fd,			/*!< in: file descriptor */
	ulint		offset,			/*!< in: offset where to write,
						in number of row_merge_block_t elements */
	const void*	buf,			/*!< in: data */
	void*		crypt_buf,		/*!< in: crypt buf or NULL */
	ulint		space)			/*!< in: space id */
{
	size_t		buf_len = srv_sort_buf_size;
	os_offset_t	ofs = buf_len * (os_offset_t) offset;
	void*		out_buf = (void *)buf;

	DBUG_ENTER("row_merge_write");
	DBUG_LOG("ib_merge_sort", "fd=" << fd << " ofs=" << ofs);
	DBUG_EXECUTE_IF("row_merge_write_failure", DBUG_RETURN(FALSE););

	/* For encrypted tables, encrypt data before writing */
	if (log_tmp_is_encrypted()) {
		if (!log_tmp_block_encrypt(static_cast<const byte*>(buf),
					   buf_len,
					   static_cast<byte*>(crypt_buf),
					   ofs)) {
			return false;
		}

		srv_stats.n_merge_blocks_encrypted.inc();
		out_buf = crypt_buf;
	}

	const bool	success = DB_SUCCESS == os_file_write(
		IORequestWrite, "(merge)", fd, out_buf, ofs, buf_len);

#ifdef POSIX_FADV_DONTNEED
	/* The block will be needed on the next merge pass,
	but it can be evicted from the file cache meanwhile. */
	posix_fadvise(fd, ofs, buf_len, POSIX_FADV_DONTNEED);
#endif /* POSIX_FADV_DONTNEED */

	DBUG_RETURN(success);
}

/********************************************************************//**
Read a merge record.
@return pointer to next record, or NULL on I/O error or end of list */
const byte*
row_merge_read_rec(
/*===============*/
	row_merge_block_t*	block,	/*!< in/out: file buffer */
	mrec_buf_t*		buf,	/*!< in/out: secondary buffer */
	const byte*		b,	/*!< in: pointer to record */
	const dict_index_t*	index,	/*!< in: index of the record */
	const pfs_os_file_t&	fd,	/*!< in: file descriptor */
	ulint*			foffs,	/*!< in/out: file offset */
	const mrec_t**		mrec,	/*!< out: pointer to merge record,
					or NULL on end of list
					(non-NULL on I/O error) */
	rec_offs*		offsets,/*!< out: offsets of mrec */
	row_merge_block_t*	crypt_block, /*!< in: crypt buf or NULL */
	ulint			space) /*!< in: space id */
{
	ulint	extra_size;
	ulint	data_size;
	ulint	avail_size;

	ut_ad(b >= &block[0]);
	ut_ad(b < &block[srv_sort_buf_size]);

	ut_ad(rec_offs_get_n_alloc(offsets) == 1 + REC_OFFS_HEADER_SIZE
	      + dict_index_get_n_fields(index));

	DBUG_ENTER("row_merge_read_rec");

	extra_size = *b++;

	if (UNIV_UNLIKELY(!extra_size)) {
		/* End of list */
		*mrec = NULL;
		DBUG_LOG("ib_merge_sort",
			 "read " << reinterpret_cast<const void*>(b) << ',' <<
			 reinterpret_cast<const void*>(block) << ',' <<
			 fd << ',' << *foffs << " EOF");
		DBUG_RETURN(NULL);
	}

	if (extra_size >= 0x80) {
		/* Read another byte of extra_size. */

		if (UNIV_UNLIKELY(b >= &block[srv_sort_buf_size])) {
			if (!row_merge_read(fd, ++(*foffs), block,
					    crypt_block,
					    space)) {
err_exit:
				/* Signal I/O error. */
				*mrec = b;
				DBUG_RETURN(NULL);
			}

			/* Wrap around to the beginning of the buffer. */
			b = &block[0];
		}

		extra_size = (extra_size & 0x7f) << 8;
		extra_size |= *b++;
	}

	/* Normalize extra_size.  Above, value 0 signals "end of list". */
	extra_size--;

	/* Read the extra bytes. */

	if (UNIV_UNLIKELY(b + extra_size >= &block[srv_sort_buf_size])) {
		/* The record spans two blocks.  Copy the entire record
		to the auxiliary buffer and handle this as a special
		case. */

		avail_size = ulint(&block[srv_sort_buf_size] - b);
		ut_ad(avail_size < sizeof *buf);
		memcpy(*buf, b, avail_size);

		if (!row_merge_read(fd, ++(*foffs), block,
				    crypt_block,
				    space)) {

			goto err_exit;
		}

		/* Wrap around to the beginning of the buffer. */
		b = &block[0];

		/* Copy the record. */
		memcpy(*buf + avail_size, b, extra_size - avail_size);
		b += extra_size - avail_size;

		*mrec = *buf + extra_size;

		rec_init_offsets_temp(*mrec, index, offsets);

		data_size = rec_offs_data_size(offsets);

		/* These overflows should be impossible given that
		records are much smaller than either buffer, and
		the record starts near the beginning of each buffer. */
		ut_a(extra_size + data_size < sizeof *buf);
		ut_a(b + data_size < &block[srv_sort_buf_size]);

		/* Copy the data bytes. */
		memcpy(*buf + extra_size, b, data_size);
		b += data_size;

		goto func_exit;
	}

	*mrec = b + extra_size;

	rec_init_offsets_temp(*mrec, index, offsets);

	data_size = rec_offs_data_size(offsets);
	ut_ad(extra_size + data_size < sizeof *buf);

	b += extra_size + data_size;

	if (UNIV_LIKELY(b < &block[srv_sort_buf_size])) {
		/* The record fits entirely in the block.
		This is the normal case. */
		goto func_exit;
	}

	/* The record spans two blocks.  Copy it to buf. */

	b -= extra_size + data_size;
	avail_size = ulint(&block[srv_sort_buf_size] - b);
	memcpy(*buf, b, avail_size);
	*mrec = *buf + extra_size;

	rec_init_offsets_temp(*mrec, index, offsets);

	if (!row_merge_read(fd, ++(*foffs), block,
			    crypt_block,
			    space)) {

		goto err_exit;
	}

	/* Wrap around to the beginning of the buffer. */
	b = &block[0];

	/* Copy the rest of the record. */
	memcpy(*buf + avail_size, b, extra_size + data_size - avail_size);
	b += extra_size + data_size - avail_size;

func_exit:
	DBUG_LOG("ib_merge_sort",
		 reinterpret_cast<const void*>(b) << ',' <<
		 reinterpret_cast<const void*>(block)
		 << ",fd=" << fd << ',' << *foffs << ": "
		 << rec_printer(*mrec, 0, offsets).str());
	DBUG_RETURN(b);
}

/********************************************************************//**
Write a merge record. */
static
void
row_merge_write_rec_low(
/*====================*/
	byte*		b,	/*!< out: buffer */
	ulint		e,	/*!< in: encoded extra_size */
#ifndef DBUG_OFF
	ulint		size,	/*!< in: total size to write */
	const pfs_os_file_t&	fd,	/*!< in: file descriptor */
	ulint		foffs,	/*!< in: file offset */
#endif /* !DBUG_OFF */
	const mrec_t*	mrec,	/*!< in: record to write */
	const rec_offs*	offsets)/*!< in: offsets of mrec */
#ifdef DBUG_OFF
# define row_merge_write_rec_low(b, e, size, fd, foffs, mrec, offsets)	\
	row_merge_write_rec_low(b, e, mrec, offsets)
#endif /* DBUG_OFF */
{
	DBUG_ENTER("row_merge_write_rec_low");

#ifndef DBUG_OFF
	const byte* const end = b + size;
#endif /* DBUG_OFF */
	DBUG_ASSERT(e == rec_offs_extra_size(offsets) + 1);

	DBUG_LOG("ib_merge_sort",
		 reinterpret_cast<const void*>(b) << ",fd=" << fd << ','
		 << foffs << ": " << rec_printer(mrec, 0, offsets).str());

	if (e < 0x80) {
		*b++ = (byte) e;
	} else {
		*b++ = (byte) (0x80 | (e >> 8));
		*b++ = (byte) e;
	}

	memcpy(b, mrec - rec_offs_extra_size(offsets), rec_offs_size(offsets));
	DBUG_SLOW_ASSERT(b + rec_offs_size(offsets) == end);
	DBUG_VOID_RETURN;
}

/********************************************************************//**
Write a merge record.
@return pointer to end of block, or NULL on error */
static
byte*
row_merge_write_rec(
/*================*/
	row_merge_block_t*	block,	/*!< in/out: file buffer */
	mrec_buf_t*		buf,	/*!< in/out: secondary buffer */
	byte*			b,	/*!< in: pointer to end of block */
	const pfs_os_file_t&	fd,	/*!< in: file descriptor */
	ulint*			foffs,	/*!< in/out: file offset */
	const mrec_t*		mrec,	/*!< in: record to write */
	const rec_offs*         offsets,/*!< in: offsets of mrec */
	row_merge_block_t*	crypt_block, /*!< in: crypt buf or NULL */
	ulint			space)	   /*!< in: space id */
{
	ulint	extra_size;
	ulint	size;
	ulint	avail_size;

	ut_ad(block);
	ut_ad(buf);
	ut_ad(b >= &block[0]);
	ut_ad(b < &block[srv_sort_buf_size]);
	ut_ad(mrec);
	ut_ad(foffs);
	ut_ad(mrec < &block[0] || mrec > &block[srv_sort_buf_size]);
	ut_ad(mrec < buf[0] || mrec > buf[1]);

	/* Normalize extra_size.  Value 0 signals "end of list". */
	extra_size = rec_offs_extra_size(offsets) + 1;

	size = extra_size + (extra_size >= 0x80)
		+ rec_offs_data_size(offsets);

	if (UNIV_UNLIKELY(b + size >= &block[srv_sort_buf_size])) {
		/* The record spans two blocks.
		Copy it to the temporary buffer first. */
		avail_size = ulint(&block[srv_sort_buf_size] - b);

		row_merge_write_rec_low(buf[0],
					extra_size, size, fd, *foffs,
					mrec, offsets);

		/* Copy the head of the temporary buffer, write
		the completed block, and copy the tail of the
		record to the head of the new block. */
		memcpy(b, buf[0], avail_size);

		if (!row_merge_write(fd, (*foffs)++, block,
				     crypt_block,
				     space)) {
			return(NULL);
		}

		MEM_UNDEFINED(&block[0], srv_sort_buf_size);

		/* Copy the rest. */
		b = &block[0];
		memcpy(b, buf[0] + avail_size, size - avail_size);
		b += size - avail_size;
	} else {
		row_merge_write_rec_low(b, extra_size, size, fd, *foffs,
					mrec, offsets);
		b += size;
	}

	return(b);
}

/********************************************************************//**
Write an end-of-list marker.
@return pointer to end of block, or NULL on error */
static
byte*
row_merge_write_eof(
/*================*/
	row_merge_block_t*	block,		/*!< in/out: file buffer */
	byte*			b,		/*!< in: pointer to end of block */
	const pfs_os_file_t&	fd,		/*!< in: file descriptor */
	ulint*			foffs,		/*!< in/out: file offset */
	row_merge_block_t*	crypt_block, 	/*!< in: crypt buf or NULL */
	ulint			space)	   	/*!< in: space id */
{
	ut_ad(block);
	ut_ad(b >= &block[0]);
	ut_ad(b < &block[srv_sort_buf_size]);
	ut_ad(foffs);

	DBUG_ENTER("row_merge_write_eof");
	DBUG_LOG("ib_merge_sort",
		 reinterpret_cast<const void*>(b) << ',' <<
		 reinterpret_cast<const void*>(block) <<
		 ",fd=" << fd << ',' << *foffs);

	*b++ = 0;
	MEM_CHECK_DEFINED(&block[0], b - &block[0]);
	MEM_CHECK_ADDRESSABLE(&block[0], srv_sort_buf_size);

	/* The rest of the block is uninitialized. Silence warnings. */
	MEM_MAKE_DEFINED(b, &block[srv_sort_buf_size] - b);

	if (!row_merge_write(fd, (*foffs)++, block, crypt_block, space)) {
		DBUG_RETURN(NULL);
	}

	MEM_UNDEFINED(&block[0], srv_sort_buf_size);
	DBUG_RETURN(&block[0]);
}

/** Create a temporary file if it has not been created already.
@param[in,out]	tmpfd	temporary file handle
@param[in]	path	location for creating temporary file
@return true on success, false on error */
static MY_ATTRIBUTE((warn_unused_result))
bool
row_merge_tmpfile_if_needed(
	pfs_os_file_t*		tmpfd,
	const char*	path)
{
	if (*tmpfd == OS_FILE_CLOSED) {
		*tmpfd = row_merge_file_create_low(path);
		if (*tmpfd != OS_FILE_CLOSED) {
			MONITOR_ATOMIC_INC(MONITOR_ALTER_TABLE_SORT_FILES);
		}
	}

	return(*tmpfd != OS_FILE_CLOSED);
}

/** Create a temporary file for merge sort if it was not created already.
@param[in,out]	file	merge file structure
@param[in]	nrec	number of records in the file
@param[in]	path	location for creating temporary file
@return  true on success, false on error */
static MY_ATTRIBUTE((warn_unused_result))
bool
row_merge_file_create_if_needed(
	merge_file_t*	file,
	pfs_os_file_t*	tmpfd,
	ulint		nrec,
	const char*	path)
{
	ut_ad(file->fd == OS_FILE_CLOSED || *tmpfd != OS_FILE_CLOSED);
	if (file->fd == OS_FILE_CLOSED && row_merge_file_create(file, path)!= OS_FILE_CLOSED) {
		MONITOR_ATOMIC_INC(MONITOR_ALTER_TABLE_SORT_FILES);
		if (!row_merge_tmpfile_if_needed(tmpfd, path) ) {
			return(false);
		}

		file->n_rec = nrec;
	}

	ut_ad(file->fd == OS_FILE_CLOSED || *tmpfd != OS_FILE_CLOSED);
	return(file->fd != OS_FILE_CLOSED);
}

/** Copy the merge data tuple from another merge data tuple.
@param[in]	mtuple		source merge data tuple
@param[in,out]	prev_mtuple	destination merge data tuple
@param[in]	n_unique	number of unique fields exist in the mtuple
@param[in,out]	heap		memory heap where last_mtuple allocated */
static
void
row_mtuple_create(
	const mtuple_t*	mtuple,
	mtuple_t*	prev_mtuple,
	ulint		n_unique,
	mem_heap_t*	heap)
{
	memcpy(prev_mtuple->fields, mtuple->fields,
	       n_unique * sizeof *mtuple->fields);

	dfield_t*	field = prev_mtuple->fields;

	for (ulint i = 0; i < n_unique; i++) {
		dfield_dup(field++, heap);
	}
}

/** Compare two merge data tuples.
@param[in]	prev_mtuple	merge data tuple
@param[in]	current_mtuple	merge data tuple
@param[in,out]	dup		reporter of duplicates
@retval positive, 0, negative if current_mtuple is greater, equal, less, than
last_mtuple. */
static
int
row_mtuple_cmp(
	const mtuple_t*		prev_mtuple,
	const mtuple_t*		current_mtuple,
	row_merge_dup_t*	dup)
{
	ut_ad(dict_index_is_clust(dup->index));
	const ulint	n_unique = dict_index_get_n_unique(dup->index);

	return(row_merge_tuple_cmp(
		       n_unique, n_unique, *current_mtuple, *prev_mtuple, dup));
}

/** Insert cached spatial index rows.
@param[in]	trx_id		transaction id
@param[in]	sp_tuples	cached spatial rows
@param[in]	num_spatial	number of spatial indexes
@param[in,out]	heap		heap for insert
@param[in,out]	sp_heap		heap for tuples
@param[in,out]	pcur		cluster index cursor
@param[in,out]	started		whether mtr is active
@param[in,out]	mtr		mini-transaction
@return DB_SUCCESS or error number */
static
dberr_t
row_merge_spatial_rows(
	trx_id_t		trx_id,
	index_tuple_info_t**	sp_tuples,
	ulint			num_spatial,
	mem_heap_t*		heap,
	mem_heap_t*		sp_heap,
	btr_pcur_t*		pcur,
	bool&			started,
	mtr_t*			mtr)
{
  if (!sp_tuples)
    return DB_SUCCESS;

  for (ulint j= 0; j < num_spatial; j++)
    if (dberr_t err= sp_tuples[j]->insert(trx_id, heap, pcur, started, mtr))
      return err;

  mem_heap_empty(sp_heap);
  return DB_SUCCESS;
}

/** Check if the geometry field is valid.
@param[in]	row		the row
@param[in]	index		spatial index
@return true if it's valid, false if it's invalid. */
static
bool
row_geo_field_is_valid(
	const dtuple_t*		row,
	dict_index_t*		index)
{
	const dict_field_t*	ind_field
		= dict_index_get_nth_field(index, 0);
	const dict_col_t*	col
		= ind_field->col;
	ulint			col_no
		= dict_col_get_no(col);
	const dfield_t*		dfield
		= dtuple_get_nth_field(row, col_no);

	if (dfield_is_null(dfield)
	    || dfield_get_len(dfield) < GEO_DATA_HEADER_SIZE) {
		return(false);
	}

	return(true);
}

/** Reads clustered index of the table and create temporary files
containing the index entries for the indexes to be built.
@param[in]	trx		transaction
@param[in,out]	table		MySQL table object, for reporting erroneous
				records
@param[in]	old_table	table where rows are read from
@param[in]	new_table	table where indexes are created; identical to
				old_table unless creating a PRIMARY KEY
@param[in]	online		true if creating indexes online
@param[in]	index		indexes to be created
@param[in]	fts_sort_idx	full-text index to be created, or NULL
@param[in]	psort_info	parallel sort info for fts_sort_idx creation,
				or NULL
@param[in]	files		temporary files
@param[in]	key_numbers	MySQL key numbers to create
@param[in]	n_index		number of indexes to create
@param[in]	defaults	default values of added, changed columns, or NULL
@param[in]	add_v		newly added virtual columns along with indexes
@param[in]	col_map		mapping of old column numbers to new ones, or
NULL if old_table == new_table
@param[in]	add_autoinc	number of added AUTO_INCREMENT columns, or
ULINT_UNDEFINED if none is added
@param[in,out]	sequence	autoinc sequence
@param[in,out]	block		file buffer
@param[in]	skip_pk_sort	whether the new PRIMARY KEY will follow
existing order
@param[in,out]	tmpfd		temporary file handle
@param[in,out]	stage		performance schema accounting object, used by
ALTER TABLE. stage->n_pk_recs_inc() will be called for each record read and
stage->inc() will be called for each page read.
@param[in]	pct_cost	percent of task weight out of total alter job
@param[in,out]	crypt_block	crypted file buffer
@param[in]	eval_table	mysql table used to evaluate virtual column
				value, see innobase_get_computed_value().
@param[in]	allow_not_null	allow null to not-null conversion
@return DB_SUCCESS or error */
static MY_ATTRIBUTE((warn_unused_result))
dberr_t
row_merge_read_clustered_index(
	trx_t*			trx,
	struct TABLE*		table,
	const dict_table_t*	old_table,
	dict_table_t*		new_table,
	bool			online,
	dict_index_t**		index,
	dict_index_t*		fts_sort_idx,
	fts_psort_t*		psort_info,
	merge_file_t*		files,
	const ulint*		key_numbers,
	ulint			n_index,
	const dtuple_t*		defaults,
	const dict_add_v_col_t*	add_v,
	const ulint*		col_map,
	ulint			add_autoinc,
	ib_sequence_t&		sequence,
	row_merge_block_t*	block,
	bool			skip_pk_sort,
	pfs_os_file_t*		tmpfd,
	ut_stage_alter_t*	stage,
	double 			pct_cost,
	row_merge_block_t*	crypt_block,
	struct TABLE*		eval_table,
	bool			allow_not_null)
{
	dict_index_t*		clust_index;	/* Clustered index */
	mem_heap_t*		row_heap = NULL;/* Heap memory to create
						clustered index tuples */
	row_merge_buf_t**	merge_buf;	/* Temporary list for records*/
	mem_heap_t*		v_heap = NULL;	/* Heap memory to process large
						data for virtual column */
	btr_pcur_t		pcur;		/* Cursor on the clustered
						index */
	mtr_t			mtr;		/* Mini transaction */
	bool			mtr_started = false;
	dberr_t			err = DB_SUCCESS;/* Return code */
	ulint			n_nonnull = 0;	/* number of columns
						changed to NOT NULL */
	ulint*			nonnull = NULL;	/* NOT NULL columns */
	dict_index_t*		fts_index = NULL;/* FTS index */
	doc_id_t		doc_id = 0;
	doc_id_t		max_doc_id = 0;
	ibool			add_doc_id = FALSE;
	pthread_cond_t*		fts_parallel_sort_cond = nullptr;
	index_tuple_info_t**	sp_tuples = NULL;
	mem_heap_t*		sp_heap = NULL;
	ulint			num_spatial = 0;
	BtrBulk*		clust_btr_bulk = NULL;
	bool			clust_temp_file = false;
	mem_heap_t*		mtuple_heap = NULL;
	mtuple_t		prev_mtuple;
	mem_heap_t*		conv_heap = NULL;
	double 			curr_progress = 0.0;
	ib_uint64_t		read_rows = 0;
	ib_uint64_t		table_total_rows = 0;
	char			new_sys_trx_start[8];
	char			new_sys_trx_end[8];
	byte			any_autoinc_data[8] = {0};
	bool			vers_update_trt = false;

	DBUG_ENTER("row_merge_read_clustered_index");

	ut_ad((old_table == new_table) == !col_map);
	ut_ad(!defaults || col_map);
	ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE));
	ut_ad(trx->id);

	table_total_rows = dict_table_get_n_rows(old_table);
	if(table_total_rows == 0) {
		/* We don't know total row count */
		table_total_rows = 1;
	}

	trx->op_info = "reading clustered index";

#ifdef FTS_INTERNAL_DIAG_PRINT
	DEBUG_FTS_SORT_PRINT("FTS_SORT: Start Create Index\n");
#endif

	/* Create and initialize memory for record buffers */

	merge_buf = static_cast<row_merge_buf_t**>(
		ut_malloc_nokey(n_index * sizeof *merge_buf));

	row_merge_dup_t	clust_dup = {index[0], table, col_map, 0};
	dfield_t*	prev_fields = nullptr;
	const ulint	n_uniq = dict_index_get_n_unique(index[0]);

	ut_ad(trx->mysql_thd != NULL);

	const char*	path = thd_innodb_tmpdir(trx->mysql_thd);

	ut_ad(!skip_pk_sort || dict_index_is_clust(index[0]));
	/* There is no previous tuple yet. */
	prev_mtuple.fields = NULL;

	for (ulint i = 0; i < n_index; i++) {
		if (index[i]->type & DICT_FTS) {

			/* We are building a FT index, make sure
			we have the temporary 'fts_sort_idx' */
			ut_a(fts_sort_idx);

			fts_index = index[i];

			merge_buf[i] = row_merge_buf_create(fts_sort_idx);

			add_doc_id = DICT_TF2_FLAG_IS_SET(
				new_table, DICT_TF2_FTS_ADD_DOC_ID);

			/* If Doc ID does not exist in the table itself,
			fetch the first FTS Doc ID */
			if (add_doc_id) {
				fts_get_next_doc_id(
					(dict_table_t*) new_table,
					&doc_id);
				ut_ad(doc_id > 0);
			}

			row_fts_start_psort(psort_info);
			fts_parallel_sort_cond =
				 &psort_info[0].psort_common->sort_cond;
		} else {
			if (dict_index_is_spatial(index[i])) {
				num_spatial++;
			}

			merge_buf[i] = row_merge_buf_create(index[i]);
		}
	}

	if (num_spatial > 0) {
		ulint	count = 0;

		sp_heap = mem_heap_create(512);

		sp_tuples = static_cast<index_tuple_info_t**>(
			ut_malloc_nokey(num_spatial
					* sizeof(*sp_tuples)));

		for (ulint i = 0; i < n_index; i++) {
			if (dict_index_is_spatial(index[i])) {
				sp_tuples[count]
					= UT_NEW_NOKEY(
						index_tuple_info_t(
							sp_heap,
							index[i]));
				count++;
			}
		}

		ut_ad(count == num_spatial);
	}

	mtr.start();
	mtr_started = true;

	/* Find the clustered index and create a persistent cursor
	based on that. */

	clust_index = dict_table_get_first_index(old_table);
	const ulint old_trx_id_col = ulint(old_table->n_cols)
		- (DATA_N_SYS_COLS - DATA_TRX_ID);
	ut_ad(old_table->cols[old_trx_id_col].mtype == DATA_SYS);
	ut_ad(old_table->cols[old_trx_id_col].prtype
	      == (DATA_TRX_ID | DATA_NOT_NULL));
	ut_ad(old_table->cols[old_trx_id_col + 1].mtype == DATA_SYS);
	ut_ad(old_table->cols[old_trx_id_col + 1].prtype
	      == (DATA_ROLL_PTR | DATA_NOT_NULL));
	const ulint new_trx_id_col = col_map
		? col_map[old_trx_id_col] : old_trx_id_col;

	btr_pcur_open_at_index_side(
		true, clust_index, BTR_SEARCH_LEAF, &pcur, true, 0, &mtr);
	mtr_started = true;
	btr_pcur_move_to_next_user_rec(&pcur, &mtr);
	if (rec_is_metadata(btr_pcur_get_rec(&pcur), *clust_index)) {
		ut_ad(btr_pcur_is_on_user_rec(&pcur));
		/* Skip the metadata pseudo-record. */
	} else {
		ut_ad(!clust_index->is_instant());
		btr_pcur_move_to_prev_on_page(&pcur);
	}

	uint64_t n_rows = 0;

	/* Check if the table is supposed to be empty for our read view.

	If we read bulk_trx_id as an older transaction ID, it is not
	incorrect to check here whether that transaction should be
	visible to us. If bulk_trx_id is not visible to us, the table
	must have been empty at an earlier point of time, also in our
	read view.

	An INSERT would only update bulk_trx_id in
	row_ins_clust_index_entry_low() if the table really was empty
	(everything had been purged), when holding a leaf page latch
	in the clustered index (actually, the root page is the only
	leaf page in that case).

	We are holding a clustered index leaf page latch here.
	That will obviously prevent any concurrent INSERT from
	updating bulk_trx_id while we read it. */
	if (!online) {
	} else if (trx_id_t bulk_trx_id = old_table->bulk_trx_id) {
		ut_ad(trx->read_view.is_open());
		ut_ad(bulk_trx_id != trx->id);
		if (!trx->read_view.changes_visible(bulk_trx_id)) {
			goto func_exit;
		}
	}

	if (old_table != new_table) {
		/* The table is being rebuilt.  Identify the columns
		that were flagged NOT NULL in the new table, so that
		we can quickly check that the records in the old table
		do not violate the added NOT NULL constraints. */

		nonnull = static_cast<ulint*>(
			ut_malloc_nokey(dict_table_get_n_cols(new_table)
				  * sizeof *nonnull));

		for (ulint i = 0; i < dict_table_get_n_cols(old_table); i++) {
			if (dict_table_get_nth_col(old_table, i)->prtype
			    & DATA_NOT_NULL) {
				continue;
			}

			const ulint j = col_map[i];

			if (j == ULINT_UNDEFINED) {
				/* The column was dropped. */
				continue;
			}

			if (dict_table_get_nth_col(new_table, j)->prtype
			    & DATA_NOT_NULL) {
				nonnull[n_nonnull++] = j;
			}
		}

		if (!n_nonnull) {
			ut_free(nonnull);
			nonnull = NULL;
		}
	}

	row_heap = mem_heap_create(sizeof(mrec_buf_t));

	if (dict_table_is_comp(old_table)
	    && !dict_table_is_comp(new_table)) {
		conv_heap = mem_heap_create(sizeof(mrec_buf_t));
	}

	if (skip_pk_sort) {
		prev_fields = static_cast<dfield_t*>(
			ut_malloc_nokey(n_uniq * sizeof *prev_fields));
		mtuple_heap = mem_heap_create(sizeof(mrec_buf_t));
	}

	mach_write_to_8(new_sys_trx_start, trx->id);
	mach_write_to_8(new_sys_trx_end, TRX_ID_MAX);

	/* Scan the clustered index. */
	for (;;) {
		/* Do not continue if table pages are still encrypted */
		if (!old_table->is_readable() || !new_table->is_readable()) {
			err = DB_DECRYPTION_FAILED;
			trx->error_key_num = 0;
			goto func_exit;
		}

		const rec_t*	rec;
		trx_id_t	rec_trx_id;
		rec_offs*	offsets;
		dtuple_t*	row;
		row_ext_t*	ext;
		page_cur_t*	cur	= btr_pcur_get_page_cur(&pcur);

		mem_heap_empty(row_heap);

		page_cur_move_to_next(cur);

		stage->n_pk_recs_inc();

		if (page_cur_is_after_last(cur)) {

			stage->inc();

			if (UNIV_UNLIKELY(trx_is_interrupted(trx))) {
				err = DB_INTERRUPTED;
				trx->error_key_num = 0;
				goto func_exit;
			}

			if (online && old_table != new_table) {
				err = row_log_table_get_error(clust_index);
				if (err != DB_SUCCESS) {
					trx->error_key_num = 0;
					goto func_exit;
				}
			}

			/* Insert the cached spatial index rows. */
			err = row_merge_spatial_rows(
				trx->id, sp_tuples, num_spatial,
				row_heap, sp_heap, &pcur, mtr_started, &mtr);

			if (err != DB_SUCCESS) {
				goto func_exit;
			}

			if (!mtr_started) {
				goto scan_next;
			}

			if (clust_index->lock.is_waiting()) {
				/* There are waiters on the clustered
				index tree lock, likely the purge
				thread. Store and restore the cursor
				position, and yield so that scanning a
				large table will not starve other
				threads. */

				/* Store the cursor position on the last user
				record on the page. */
				btr_pcur_move_to_prev_on_page(&pcur);
				/* Leaf pages must never be empty, unless
				this is the only page in the index tree. */
				ut_ad(btr_pcur_is_on_user_rec(&pcur)
				      || btr_pcur_get_block(
					      &pcur)->page.id().page_no()
				      == clust_index->page);

				btr_pcur_store_position(&pcur, &mtr);
				mtr.commit();
				mtr_started = false;

				/* Give the waiters a chance to proceed. */
				std::this_thread::yield();
scan_next:
				ut_ad(!mtr_started);
				ut_ad(!mtr.is_active());
				mtr.start();
				mtr_started = true;
				/* Restore position on the record, or its
				predecessor if the record was purged
				meanwhile. */
				btr_pcur_restore_position(
					BTR_SEARCH_LEAF, &pcur, &mtr);
				/* Move to the successor of the
				original record. */
				if (!btr_pcur_move_to_next_user_rec(
					    &pcur, &mtr)) {
end_of_index:
					row = NULL;
					mtr.commit();
					mtr_started = false;
					mem_heap_free(row_heap);
					row_heap = NULL;
					ut_free(nonnull);
					nonnull = NULL;
					goto write_buffers;
				}
			} else {
				uint32_t next_page_no = btr_page_get_next(
					page_cur_get_page(cur));

				if (next_page_no == FIL_NULL) {
					goto end_of_index;
				}

				buf_block_t* block = btr_block_get(
					*clust_index, next_page_no,
					RW_S_LATCH, false, &mtr);

				btr_leaf_page_release(page_cur_get_block(cur),
						      BTR_SEARCH_LEAF, &mtr);
				page_cur_set_before_first(block, cur);
				page_cur_move_to_next(cur);

				ut_ad(!page_cur_is_after_last(cur));
			}
		}

		rec = page_cur_get_rec(cur);

		if (online) {
			offsets = rec_get_offsets(rec, clust_index, NULL,
						  clust_index->n_core_fields,
						  ULINT_UNDEFINED, &row_heap);
			rec_trx_id = row_get_rec_trx_id(rec, clust_index,
							offsets);

			/* Perform a REPEATABLE READ.

			When rebuilding the table online,
			row_log_table_apply() must not see a newer
			state of the table when applying the log.
			This is mainly to prevent false duplicate key
			errors, because the log will identify records
			by the PRIMARY KEY, and also to prevent unsafe
			BLOB access.

			When creating a secondary index online, this
			table scan must not see records that have only
			been inserted to the clustered index, but have
			not been written to the online_log of
			index[]. If we performed READ UNCOMMITTED, it
			could happen that the ADD INDEX reaches
			ONLINE_INDEX_COMPLETE state between the time
			the DML thread has updated the clustered index
			but has not yet accessed secondary index. */
			ut_ad(trx->read_view.is_open());
			ut_ad(rec_trx_id != trx->id);

			if (!trx->read_view.changes_visible(
				    rec_trx_id, old_table->name)) {
				rec_t*	old_vers;

				row_vers_build_for_consistent_read(
					rec, &mtr, clust_index, &offsets,
					&trx->read_view, &row_heap,
					row_heap, &old_vers, NULL);

				if (!old_vers) {
					continue;
				}

				/* The old version must necessarily be
				in the "prehistory", because the
				exclusive lock in
				ha_innobase::prepare_inplace_alter_table()
				forced the completion of any transactions
				that accessed this table. */
				ut_ad(row_get_rec_trx_id(old_vers, clust_index,
							 offsets) < trx->id);

				rec = old_vers;
				rec_trx_id = 0;
			}

			if (rec_get_deleted_flag(
				    rec,
				    dict_table_is_comp(old_table))) {
				/* In delete-marked records, DB_TRX_ID must
				always refer to an existing undo log record.
				Above, we did reset rec_trx_id = 0
				for rec = old_vers.*/
				ut_ad(rec == page_cur_get_rec(cur)
				      ? rec_trx_id
				      : !rec_trx_id);
				/* This record was deleted in the latest
				committed version, or it was deleted and
				then reinserted-by-update before purge
				kicked in. Skip it. */
				continue;
			}

			ut_ad(!rec_offs_any_null_extern(rec, offsets));
		} else if (rec_get_deleted_flag(
				   rec, dict_table_is_comp(old_table))) {
			/* In delete-marked records, DB_TRX_ID must
			always refer to an existing undo log record. */
			ut_d(rec_trx_id = rec_get_trx_id(rec, clust_index));
			ut_ad(rec_trx_id);
			/* This must be a purgeable delete-marked record,
			and the transaction that delete-marked the record
			must have been committed before this
			!online ALTER TABLE transaction. */
			ut_ad(rec_trx_id < trx->id);
			/* Skip delete-marked records.

			Skipping delete-marked records will make the
			created indexes unuseable for transactions
			whose read views were created before the index
			creation completed, but an attempt to preserve
			the history would make it tricky to detect
			duplicate keys. */
			continue;
		} else {
			offsets = rec_get_offsets(rec, clust_index, NULL,
						  clust_index->n_core_fields,
						  ULINT_UNDEFINED, &row_heap);
			/* This is a locking ALTER TABLE.

			If we are not rebuilding the table, the
			DB_TRX_ID does not matter, as it is not being
			written to any secondary indexes; see
			if (old_table == new_table) below.

			If we are rebuilding the table, the
			DB_TRX_ID,DB_ROLL_PTR should be reset, because
			there will be no history available. */
			ut_ad(rec_get_trx_id(rec, clust_index) < trx->id);
			rec_trx_id = 0;
		}

		/* When !online, we are holding a lock on old_table, preventing
		any inserts that could have written a record 'stub' before
		writing out off-page columns. */
		ut_ad(!rec_offs_any_null_extern(rec, offsets));

		/* Build a row based on the clustered index. */

		row = row_build_w_add_vcol(ROW_COPY_POINTERS, clust_index,
					   rec, offsets, new_table,
					   defaults, add_v, col_map, &ext,
					   row_heap);
		ut_ad(row);

		for (ulint i = 0; i < n_nonnull; i++) {
			dfield_t*	field	= &row->fields[nonnull[i]];

			ut_ad(dfield_get_type(field)->prtype & DATA_NOT_NULL);

			if (dfield_is_null(field)) {

				Field* null_field =
					table->field[nonnull[i]];

				null_field->set_warning(
					Sql_condition::WARN_LEVEL_WARN,
					WARN_DATA_TRUNCATED, 1,
					ulong(n_rows + 1));

				if (!allow_not_null) {
					err = DB_INVALID_NULL;
					trx->error_key_num = 0;
					goto func_exit;
				}

				const dfield_t& default_field
					= defaults->fields[nonnull[i]];

				*field = default_field;
			}
		}

		/* Get the next Doc ID */
		if (add_doc_id) {
			doc_id++;
		} else {
			doc_id = 0;
		}

		ut_ad(row->fields[new_trx_id_col].type.mtype == DATA_SYS);
		ut_ad(row->fields[new_trx_id_col].type.prtype
		      == (DATA_TRX_ID | DATA_NOT_NULL));
		ut_ad(row->fields[new_trx_id_col].len == DATA_TRX_ID_LEN);
		ut_ad(row->fields[new_trx_id_col + 1].type.mtype == DATA_SYS);
		ut_ad(row->fields[new_trx_id_col + 1].type.prtype
		      == (DATA_ROLL_PTR | DATA_NOT_NULL));
		ut_ad(row->fields[new_trx_id_col + 1].len == DATA_ROLL_PTR_LEN);

		if (old_table == new_table) {
			/* Do not bother touching DB_TRX_ID,DB_ROLL_PTR
			because they are not going to be written into
			secondary indexes. */
		} else if (rec_trx_id < trx->id) {
			/* Reset the DB_TRX_ID,DB_ROLL_PTR of old rows
			for which history is not going to be
			available after the rebuild operation.
			This essentially mimics row_purge_reset_trx_id(). */
			row->fields[new_trx_id_col].data
				= const_cast<byte*>(reset_trx_id);
			row->fields[new_trx_id_col + 1].data
				= const_cast<byte*>(reset_trx_id
						    + DATA_TRX_ID_LEN);
		}

		if (add_autoinc != ULINT_UNDEFINED) {

			ut_ad(add_autoinc
			      < dict_table_get_n_user_cols(new_table));

			bool history_row = false;
			if (new_table->versioned()) {
				const dfield_t* dfield = dtuple_get_nth_field(
				    row, new_table->vers_end);
				history_row = dfield->vers_history_row();
			}

			dfield_t* dfield = dtuple_get_nth_field(row,
								add_autoinc);

			if (new_table->versioned()) {
				if (history_row) {
					if (dfield_get_type(dfield)->prtype & DATA_NOT_NULL) {
						err = DB_UNSUPPORTED;
						my_error(ER_UNSUPPORTED_EXTENSION, MYF(0),
							 old_table->name.m_name);
						goto func_exit;
					}
					dfield_set_null(dfield);
				} else {
					// set not null
					ulint len = dfield_get_type(dfield)->len;
					dfield_set_data(dfield, any_autoinc_data, len);
				}
			}

			if (dfield_is_null(dfield)) {
				goto write_buffers;
			}

			const dtype_t*  dtype = dfield_get_type(dfield);
			byte*	b = static_cast<byte*>(dfield_get_data(dfield));

			if (sequence.eof()) {
				err = DB_ERROR;
				trx->error_key_num = 0;

				ib_errf(trx->mysql_thd, IB_LOG_LEVEL_ERROR,
					ER_AUTOINC_READ_FAILED, "[NULL]");

				goto func_exit;
			}

			ulonglong	value = sequence++;

			switch (dtype_get_mtype(dtype)) {
			case DATA_INT: {
				ibool	usign;
				ulint	len = dfield_get_len(dfield);

				usign = dtype_get_prtype(dtype) & DATA_UNSIGNED;
				mach_write_ulonglong(b, value, len, usign);

				break;
				}

			case DATA_FLOAT:
				mach_float_write(
					b, static_cast<float>(value));
				break;

			case DATA_DOUBLE:
				mach_double_write(
					b, static_cast<double>(value));
				break;

			default:
				ut_ad(0);
			}
		}

		if (old_table->versioned()) {
			if (!new_table->versioned()
			    && clust_index->vers_history_row(rec, offsets)) {
				continue;
			}
		} else if (new_table->versioned()) {
			dfield_t* start =
			    dtuple_get_nth_field(row, new_table->vers_start);
			dfield_t* end =
			    dtuple_get_nth_field(row, new_table->vers_end);
			dfield_set_data(start, new_sys_trx_start, 8);
			dfield_set_data(end, new_sys_trx_end, 8);
			vers_update_trt = true;
		}

write_buffers:
		/* Build all entries for all the indexes to be created
		in a single scan of the clustered index. */

		n_rows++;
		ulint	s_idx_cnt = 0;
		bool	skip_sort = skip_pk_sort
			&& dict_index_is_clust(merge_buf[0]->index);

		for (ulint k = 0, i = 0; i < n_index; i++, skip_sort = false) {
			row_merge_buf_t*	buf	= merge_buf[i];
			ulint			rows_added = 0;

			if (dict_index_is_spatial(buf->index)) {
				if (!row) {
					continue;
				}

				ut_ad(sp_tuples[s_idx_cnt]->get_index()
				      == buf->index);

				/* If the geometry field is invalid, report
				error. */
				if (!row_geo_field_is_valid(row, buf->index)) {
					err = DB_CANT_CREATE_GEOMETRY_OBJECT;
					break;
				}

				sp_tuples[s_idx_cnt]->add(row, ext);
				s_idx_cnt++;

				continue;
			}

			ut_ad(!row
			      || !dict_index_is_clust(buf->index)
			      || trx_id_check(row->fields[new_trx_id_col].data,
					      trx->id));

			merge_file_t*	file = &files[k++];

			if (UNIV_LIKELY
			    (row && (rows_added = row_merge_buf_add(
					buf, fts_index, old_table, new_table,
					psort_info, row, ext, &doc_id,
					conv_heap, &err,
					&v_heap, eval_table, trx)))) {

				/* If we are creating FTS index,
				a single row can generate more
				records for tokenized word */
				file->n_rec += rows_added;

				if (err != DB_SUCCESS) {
					ut_ad(err == DB_TOO_BIG_RECORD);
					break;
				}

				if (doc_id > max_doc_id) {
					max_doc_id = doc_id;
				}

				if (buf->index->type & DICT_FTS) {
					/* Check if error occurs in child thread */
					for (ulint j = 0;
					     j < fts_sort_pll_degree; j++) {
						if (psort_info[j].error
							!= DB_SUCCESS) {
							err = psort_info[j].error;
							trx->error_key_num = i;
							break;
						}
					}

					if (err != DB_SUCCESS) {
						break;
					}
				}

				if (skip_sort) {
					ut_ad(buf->n_tuples > 0);
					const mtuple_t*	curr =
						&buf->tuples[buf->n_tuples - 1];

					ut_ad(i == 0);
					ut_ad(dict_index_is_clust(merge_buf[0]->index));
					/* Detect duplicates by comparing the
					current record with previous record.
					When temp file is not used, records
					should be in sorted order. */
					if (prev_mtuple.fields != NULL
					    && (row_mtuple_cmp(
						&prev_mtuple, curr,
						&clust_dup) == 0)) {

						err = DB_DUPLICATE_KEY;
						trx->error_key_num
							= key_numbers[0];
						goto func_exit;
					}

					prev_mtuple.fields = curr->fields;
				}

				continue;
			}

			if (err == DB_COMPUTE_VALUE_FAILED) {
				trx->error_key_num = i;
				goto func_exit;
			}

			if (buf->index->type & DICT_FTS) {
				if (!row || !doc_id) {
					continue;
				}
			}

			/* The buffer must be sufficiently large
			to hold at least one record. It may only
			be empty when we reach the end of the
			clustered index. row_merge_buf_add()
			must not have been called in this loop. */
			ut_ad(buf->n_tuples || row == NULL);

			/* We have enough data tuples to form a block.
			Sort them and write to disk if temp file is used
			or insert into index if temp file is not used. */
			ut_ad(old_table == new_table
			      ? !dict_index_is_clust(buf->index)
			      : (i == 0) == dict_index_is_clust(buf->index));

			/* We have enough data tuples to form a block.
			Sort them (if !skip_sort) and write to disk. */

			if (buf->n_tuples) {
				if (skip_sort) {
					/* Temporary File is not used.
					so insert sorted block to the index */
					if (row != NULL) {
						/* We have to do insert the
						cached spatial index rows, since
						after the mtr_commit, the cluster
						index page could be updated, then
						the data in cached rows become
						invalid. */
						err = row_merge_spatial_rows(
							trx->id, sp_tuples,
							num_spatial,
							row_heap, sp_heap,
							&pcur, mtr_started,
							&mtr);

						if (err != DB_SUCCESS) {
							goto func_exit;
						}

						/* We are not at the end of
						the scan yet. We must
						mtr.commit() in order to be
						able to call log_free_check()
						in row_merge_insert_index_tuples().
						Due to mtr.commit(), the
						current row will be invalid, and
						we must reread it on the next
						loop iteration. */
						if (mtr_started) {
							btr_pcur_move_to_prev_on_page(
								&pcur);
							btr_pcur_store_position(
								&pcur, &mtr);

							mtr.commit();
							mtr_started = false;
						}
					}

					mem_heap_empty(mtuple_heap);
					prev_mtuple.fields = prev_fields;

					row_mtuple_create(
						&buf->tuples[buf->n_tuples - 1],
						&prev_mtuple, n_uniq,
						mtuple_heap);

					if (clust_btr_bulk == NULL) {
						clust_btr_bulk = UT_NEW_NOKEY(
							BtrBulk(index[i],
								trx));
					} else {
						clust_btr_bulk->latch();
					}

					err = row_merge_insert_index_tuples(
						index[i], old_table,
						OS_FILE_CLOSED, NULL, buf,
						clust_btr_bulk,
						table_total_rows,
						curr_progress,
						pct_cost,
						crypt_block,
						new_table->space_id);

					if (row == NULL) {
						err = clust_btr_bulk->finish(
							err);
						UT_DELETE(clust_btr_bulk);
						clust_btr_bulk = NULL;
					} else {
						/* Release latches for possible
						log_free_chck in spatial index
						build. */
						clust_btr_bulk->release();
					}

					if (err != DB_SUCCESS) {
						break;
					}

					if (row != NULL) {
						/* Restore the cursor on the
						previous clustered index record,
						and empty the buffer. The next
						iteration of the outer loop will
						advance the cursor and read the
						next record (the one which we
						had to ignore due to the buffer
						overflow). */
						mtr.start();
						mtr_started = true;
						btr_pcur_restore_position(
							BTR_SEARCH_LEAF, &pcur,
							&mtr);
						buf = row_merge_buf_empty(buf);
						merge_buf[i] = buf;
						/* Restart the outer loop on the
						record. We did not insert it
						into any index yet. */
						ut_ad(i == 0);
						break;
					}
				} else if (dict_index_is_unique(buf->index)) {
					row_merge_dup_t	dup = {
						buf->index, table, col_map, 0};

					row_merge_buf_sort(buf, &dup);

					if (dup.n_dup) {
						err = DB_DUPLICATE_KEY;
						trx->error_key_num
							= key_numbers[i];
						break;
					}
				} else {
					row_merge_buf_sort(buf, NULL);
				}
			} else if (online && new_table == old_table) {
				/* Note the newest transaction that
				modified this index when the scan was
				completed. We prevent older readers
				from accessing this index, to ensure
				read consistency. */

				ut_a(row == NULL);

				dict_index_t* index = buf->index;
				index->lock.x_lock(SRW_LOCK_CALL);
				ut_a(dict_index_get_online_status(index)
				     == ONLINE_INDEX_CREATION);

				trx_id_t max_trx_id = row_log_get_max_trx(
					index);

				if (max_trx_id > index->trx_id) {
					index->trx_id = max_trx_id;
				}

				index->lock.x_unlock();
			}

			/* Secondary index and clustered index which is
			not in sorted order can use the temporary file.
			Fulltext index should not use the temporary file. */
			if (!skip_sort && !(buf->index->type & DICT_FTS)) {
				/* In case we can have all rows in sort buffer,
				we can insert directly into the index without
				temporary file if clustered index does not uses
				temporary file. */
				if (row == NULL && file->fd == OS_FILE_CLOSED
				    && !clust_temp_file) {
					DBUG_EXECUTE_IF(
						"row_merge_write_failure",
						err = DB_TEMP_FILE_WRITE_FAIL;
						trx->error_key_num = i;
						goto all_done;);

					DBUG_EXECUTE_IF(
						"row_merge_tmpfile_fail",
						err = DB_OUT_OF_MEMORY;
						trx->error_key_num = i;
						goto all_done;);

					BtrBulk	btr_bulk(index[i], trx);

					err = row_merge_insert_index_tuples(
						index[i], old_table,
						OS_FILE_CLOSED, NULL, buf,
						&btr_bulk,
						table_total_rows,
						curr_progress,
						pct_cost,
						crypt_block,
						new_table->space_id);

					err = btr_bulk.finish(err);

					DBUG_EXECUTE_IF(
						"row_merge_insert_big_row",
						err = DB_TOO_BIG_RECORD;);

					if (err != DB_SUCCESS) {
						break;
					}
				} else {
					if (!row_merge_file_create_if_needed(
						file, tmpfd,
						buf->n_tuples, path)) {
						err = DB_OUT_OF_MEMORY;
						trx->error_key_num = i;
						break;
					}

					/* Ensure that duplicates in the
					clustered index will be detected before
					inserting secondary index records. */
					if (dict_index_is_clust(buf->index)) {
						clust_temp_file = true;
					}

					ut_ad(file->n_rec > 0);

					row_merge_buf_write(buf, file, block);

					if (!row_merge_write(
						    file->fd, file->offset++,
						    block, crypt_block,
						    new_table->space_id)) {
						err = DB_TEMP_FILE_WRITE_FAIL;
						trx->error_key_num = i;
						break;
					}

					MEM_UNDEFINED(
						&block[0], srv_sort_buf_size);
				}
			}
			merge_buf[i] = row_merge_buf_empty(buf);
			buf = merge_buf[i];

			if (UNIV_LIKELY(row != NULL)) {
				/* Try writing the record again, now
				that the buffer has been written out
				and emptied. */

				if (UNIV_UNLIKELY
				    (!(rows_added = row_merge_buf_add(
						buf, fts_index, old_table,
						new_table, psort_info, row, ext,
						&doc_id, conv_heap,
						&err, &v_heap, eval_table, trx)))) {
                                        /* An empty buffer should have enough
                                        room for at least one record. */
					ut_ad(err == DB_COMPUTE_VALUE_FAILED
					      || err == DB_OUT_OF_MEMORY
					      || err == DB_TOO_BIG_RECORD);
				} else if (err == DB_SUCCESS) {
					file->n_rec += rows_added;
					continue;
				}

				trx->error_key_num = i;
				break;
			}
		}

		if (row == NULL) {
			if (old_table != new_table) {
				new_table->stat_n_rows = n_rows;
			}

			goto all_done;
		}

		if (err != DB_SUCCESS) {
			goto func_exit;
		}

		if (v_heap) {
			mem_heap_empty(v_heap);
		}

		/* Increment innodb_onlineddl_pct_progress status variable */
		read_rows++;
		if(read_rows % 1000 == 0) {
			/* Update progress for each 1000 rows */
			curr_progress = (read_rows >= table_total_rows) ?
					pct_cost :
				pct_cost * static_cast<double>(read_rows)
				/ static_cast<double>(table_total_rows);
			/* presenting 10.12% as 1012 integer */
			onlineddl_pct_progress = (ulint) (curr_progress * 100);
		}
	}

func_exit:
	ut_ad(mtr_started == mtr.is_active());
	if (mtr_started) {
		mtr.commit();
	}
	if (row_heap) {
		mem_heap_free(row_heap);
	}
	ut_free(nonnull);

all_done:
	if (clust_btr_bulk != NULL) {
		ut_ad(err != DB_SUCCESS);
		clust_btr_bulk->latch();
		err = clust_btr_bulk->finish(
			err);
		UT_DELETE(clust_btr_bulk);
	}

	if (prev_fields) {
		ut_free(prev_fields);
		mem_heap_free(mtuple_heap);
	}

	if (v_heap) {
		mem_heap_free(v_heap);
	}

	if (conv_heap != NULL) {
		mem_heap_free(conv_heap);
	}

#ifdef FTS_INTERNAL_DIAG_PRINT
	DEBUG_FTS_SORT_PRINT("FTS_SORT: Complete Scan Table\n");
#endif
	if (UNIV_LIKELY_NULL(fts_parallel_sort_cond)) {
wait_again:
                /* Check if error occurs in child thread */
		for (ulint j = 0; j < fts_sort_pll_degree; j++) {
			if (psort_info[j].error != DB_SUCCESS) {
				err = psort_info[j].error;
				trx->error_key_num = j;
				break;
			}
		}

		/* Tell all children that parent has done scanning */
		for (ulint i = 0; i < fts_sort_pll_degree; i++) {
			if (err == DB_SUCCESS) {
				psort_info[i].state = FTS_PARENT_COMPLETE;
			} else {
				psort_info[i].state = FTS_PARENT_EXITING;
			}
		}

		/* Now wait all children to report back to be completed */
		timespec abstime;
		set_timespec(abstime, 1);
		mysql_mutex_lock(&psort_info[0].mutex);
		my_cond_timedwait(fts_parallel_sort_cond,
				  &psort_info[0].mutex.m_mutex, &abstime);
		mysql_mutex_unlock(&psort_info[0].mutex);

		for (ulint i = 0; i < fts_sort_pll_degree; i++) {
			if (!psort_info[i].child_status) {
				goto wait_again;
			}
		}

		for (ulint j = 0; j < fts_sort_pll_degree; j++) {
			psort_info[j].task->wait();
			delete psort_info[j].task;
		}
	}

#ifdef FTS_INTERNAL_DIAG_PRINT
	DEBUG_FTS_SORT_PRINT("FTS_SORT: Complete Tokenization\n");
#endif
	for (ulint i = 0; i < n_index; i++) {
		row_merge_buf_free(merge_buf[i]);
	}

	row_fts_free_pll_merge_buf(psort_info);

	ut_free(merge_buf);

	btr_pcur_close(&pcur);

	if (sp_tuples != NULL) {
		for (ulint i = 0; i < num_spatial; i++) {
			UT_DELETE(sp_tuples[i]);
		}
		ut_free(sp_tuples);

		if (sp_heap) {
			mem_heap_free(sp_heap);
		}
	}

	/* Update the next Doc ID we used. Table should be locked, so
	no concurrent DML */
	if (max_doc_id && err == DB_SUCCESS) {
		/* Sync fts cache for other fts indexes to keep all
		fts indexes consistent in sync_doc_id. */
		err = fts_sync_table(const_cast<dict_table_t*>(new_table));

		if (err == DB_SUCCESS) {
			fts_update_next_doc_id(NULL, new_table, max_doc_id);
		}
	}

	if (vers_update_trt) {
		trx->mod_tables.emplace(new_table, 0)
			.first->second.set_versioned(0);
	}

	trx->op_info = "";

	DBUG_RETURN(err);
}

/** Write a record via buffer 2 and read the next record to buffer N.
@param N number of the buffer (0 or 1)
@param INDEX record descriptor
@param AT_END statement to execute at end of input */
#define ROW_MERGE_WRITE_GET_NEXT_LOW(N, INDEX, AT_END)			\
	do {								\
		b2 = row_merge_write_rec(&block[2 * srv_sort_buf_size], \
					 &buf[2], b2,			\
					 of->fd, &of->offset,		\
					 mrec##N, offsets##N,		\
			crypt_block ? &crypt_block[2 * srv_sort_buf_size] : NULL , \
					space);				\
		if (UNIV_UNLIKELY(!b2 || ++of->n_rec > file->n_rec)) {	\
			goto corrupt;					\
		}							\
		b##N = row_merge_read_rec(&block[N * srv_sort_buf_size],\
					  &buf[N], b##N, INDEX,		\
					  file->fd, foffs##N,		\
					  &mrec##N, offsets##N,		\
			crypt_block ? &crypt_block[N * srv_sort_buf_size] : NULL, \
					  space);			\
									\
		if (UNIV_UNLIKELY(!b##N)) {				\
			if (mrec##N) {					\
				goto corrupt;				\
			}						\
			AT_END;						\
		}							\
	} while (0)

#ifdef HAVE_PSI_STAGE_INTERFACE
#define ROW_MERGE_WRITE_GET_NEXT(N, INDEX, AT_END)			\
	do {								\
		if (stage != NULL) {					\
			stage->inc();					\
		}							\
		ROW_MERGE_WRITE_GET_NEXT_LOW(N, INDEX, AT_END);		\
	} while (0)
#else /* HAVE_PSI_STAGE_INTERFACE */
#define ROW_MERGE_WRITE_GET_NEXT(N, INDEX, AT_END)			\
	ROW_MERGE_WRITE_GET_NEXT_LOW(N, INDEX, AT_END)
#endif /* HAVE_PSI_STAGE_INTERFACE */

/** Merge two blocks of records on disk and write a bigger block.
@param[in]	dup	descriptor of index being created
@param[in]	file	file containing index entries
@param[in,out]	block	3 buffers
@param[in,out]	foffs0	offset of first source list in the file
@param[in,out]	foffs1	offset of second source list in the file
@param[in,out]	of	output file
@param[in,out]	stage	performance schema accounting object, used by
ALTER TABLE. If not NULL stage->inc() will be called for each record
processed.
@param[in,out]	crypt_block	encryption buffer
@param[in]	space	tablespace ID for encryption
@return DB_SUCCESS or error code */
static MY_ATTRIBUTE((warn_unused_result))
dberr_t
row_merge_blocks(
	const row_merge_dup_t*	dup,
	const merge_file_t*	file,
	row_merge_block_t*	block,
	ulint*			foffs0,
	ulint*			foffs1,
	merge_file_t*		of,
	ut_stage_alter_t*	stage MY_ATTRIBUTE((unused)),
	row_merge_block_t*	crypt_block,
	ulint			space)
{
	mem_heap_t*	heap;	/*!< memory heap for offsets0, offsets1 */

	mrec_buf_t*	buf;	/*!< buffer for handling
				split mrec in block[] */
	const byte*	b0;	/*!< pointer to block[0] */
	const byte*	b1;	/*!< pointer to block[srv_sort_buf_size] */
	byte*		b2;	/*!< pointer to block[2 * srv_sort_buf_size] */
	const mrec_t*	mrec0;	/*!< merge rec, points to block[0] or buf[0] */
	const mrec_t*	mrec1;	/*!< merge rec, points to
				block[srv_sort_buf_size] or buf[1] */
	rec_offs*	offsets0;/* offsets of mrec0 */
	rec_offs*	offsets1;/* offsets of mrec1 */

	DBUG_ENTER("row_merge_blocks");
	DBUG_LOG("ib_merge_sort",
		 "fd=" << file->fd << ',' << *foffs0 << '+' << *foffs1
		 << " to fd=" << of->fd << ',' << of->offset);

	heap = row_merge_heap_create(dup->index, &buf, &offsets0, &offsets1);

	/* Write a record and read the next record.  Split the output
	file in two halves, which can be merged on the following pass. */

	if (!row_merge_read(file->fd, *foffs0, &block[0],
			    crypt_block ? &crypt_block[0] : NULL,
			    space) ||
	    !row_merge_read(file->fd, *foffs1, &block[srv_sort_buf_size],
			    crypt_block ? &crypt_block[srv_sort_buf_size] : NULL,
			    space)) {
corrupt:
		mem_heap_free(heap);
		DBUG_RETURN(DB_CORRUPTION);
	}

	b0 = &block[0];
	b1 = &block[srv_sort_buf_size];
	b2 = &block[2 * srv_sort_buf_size];

	b0 = row_merge_read_rec(
		&block[0], &buf[0], b0, dup->index,
		file->fd, foffs0, &mrec0, offsets0,
		crypt_block ? &crypt_block[0] : NULL,
		space);

	b1 = row_merge_read_rec(
		&block[srv_sort_buf_size],
		&buf[srv_sort_buf_size], b1, dup->index,
		file->fd, foffs1, &mrec1, offsets1,
		crypt_block ? &crypt_block[srv_sort_buf_size] : NULL,
		space);

	if (UNIV_UNLIKELY(!b0 && mrec0)
	    || UNIV_UNLIKELY(!b1 && mrec1)) {

		goto corrupt;
	}

	while (mrec0 && mrec1) {
		int cmp = cmp_rec_rec_simple(
			mrec0, mrec1, offsets0, offsets1,
			dup->index, dup->table);
		if (cmp < 0) {
			ROW_MERGE_WRITE_GET_NEXT(0, dup->index, goto merged);
		} else if (cmp) {
			ROW_MERGE_WRITE_GET_NEXT(1, dup->index, goto merged);
		} else {
			mem_heap_free(heap);
			DBUG_RETURN(DB_DUPLICATE_KEY);
		}
	}

merged:
	if (mrec0) {
		/* append all mrec0 to output */
		for (;;) {
			ROW_MERGE_WRITE_GET_NEXT(0, dup->index, goto done0);
		}
	}
done0:
	if (mrec1) {
		/* append all mrec1 to output */
		for (;;) {
			ROW_MERGE_WRITE_GET_NEXT(1, dup->index, goto done1);
		}
	}
done1:

	mem_heap_free(heap);

	b2 = row_merge_write_eof(
		&block[2 * srv_sort_buf_size],
		b2, of->fd, &of->offset,
		crypt_block ? &crypt_block[2 * srv_sort_buf_size] : NULL,
		space);
	DBUG_RETURN(b2 ? DB_SUCCESS : DB_CORRUPTION);
}

/** Copy a block of index entries.
@param[in]	index	index being created
@param[in]	file	input file
@param[in,out]	block	3 buffers
@param[in,out]	foffs0	input file offset
@param[in,out]	of	output file
@param[in,out]	stage	performance schema accounting object, used by
ALTER TABLE. If not NULL stage->inc() will be called for each record
processed.
@param[in,out]	crypt_block	encryption buffer
@param[in]	space	tablespace ID for encryption
@return TRUE on success, FALSE on failure */
static MY_ATTRIBUTE((warn_unused_result))
ibool
row_merge_blocks_copy(
	const dict_index_t*	index,
	const merge_file_t*	file,
	row_merge_block_t*	block,
	ulint*			foffs0,
	merge_file_t*		of,
	ut_stage_alter_t*	stage MY_ATTRIBUTE((unused)),
	row_merge_block_t*	crypt_block,
	ulint			space)
{
	mem_heap_t*	heap;	/*!< memory heap for offsets0, offsets1 */

	mrec_buf_t*	buf;	/*!< buffer for handling
				split mrec in block[] */
	const byte*	b0;	/*!< pointer to block[0] */
	byte*		b2;	/*!< pointer to block[2 * srv_sort_buf_size] */
	const mrec_t*	mrec0;	/*!< merge rec, points to block[0] */
	rec_offs*	offsets0;/* offsets of mrec0 */
	rec_offs*	offsets1;/* dummy offsets */

	DBUG_ENTER("row_merge_blocks_copy");
	DBUG_LOG("ib_merge_sort",
		 "fd=" << file->fd << ',' << foffs0
		 << " to fd=" << of->fd << ',' << of->offset);

	heap = row_merge_heap_create(index, &buf, &offsets0, &offsets1);

	/* Write a record and read the next record.  Split the output
	file in two halves, which can be merged on the following pass. */

	if (!row_merge_read(file->fd, *foffs0, &block[0],
			crypt_block ? &crypt_block[0] : NULL,
			space)) {
corrupt:
		mem_heap_free(heap);
		DBUG_RETURN(FALSE);
	}

	b0 = &block[0];

	b2 = &block[2 * srv_sort_buf_size];

	b0 = row_merge_read_rec(&block[0], &buf[0], b0, index,
				file->fd, foffs0, &mrec0, offsets0,
				crypt_block ? &crypt_block[0] : NULL,
				space);

	if (UNIV_UNLIKELY(!b0 && mrec0)) {

		goto corrupt;
	}

	if (mrec0) {
		/* append all mrec0 to output */
		for (;;) {
			ROW_MERGE_WRITE_GET_NEXT(0, index, goto done0);
		}
	}
done0:

	/* The file offset points to the beginning of the last page
	that has been read.  Update it to point to the next block. */
	(*foffs0)++;

	mem_heap_free(heap);

	DBUG_RETURN(row_merge_write_eof(
			    &block[2 * srv_sort_buf_size],
			    b2, of->fd, &of->offset,
			    crypt_block
			    ? &crypt_block[2 * srv_sort_buf_size]
			    : NULL, space)
		    != NULL);
}

/** Merge disk files.
@param[in]	trx		transaction
@param[in]	dup		descriptor of index being created
@param[in,out]	file		file containing index entries
@param[in,out]	block		3 buffers
@param[in,out]	tmpfd		temporary file handle
@param[in,out]	num_run		Number of runs that remain to be merged
@param[in,out]	run_offset	Array that contains the first offset number
for each merge run
@param[in,out]	stage		performance schema accounting object, used by
@param[in,out]	crypt_block	encryption buffer
@param[in]	space		tablespace ID for encryption
ALTER TABLE. If not NULL stage->inc() will be called for each record
processed.
@return DB_SUCCESS or error code */
static
dberr_t
row_merge(
	trx_t*			trx,
	const row_merge_dup_t*	dup,
	merge_file_t*		file,
	row_merge_block_t*	block,
	pfs_os_file_t*		tmpfd,
	ulint*			num_run,
	ulint*			run_offset,
	ut_stage_alter_t*	stage,
	row_merge_block_t*	crypt_block,
	ulint			space)
{
	ulint		foffs0;	/*!< first input offset */
	ulint		foffs1;	/*!< second input offset */
	dberr_t		error;	/*!< error code */
	merge_file_t	of;	/*!< output file */
	const ulint	ihalf	= run_offset[*num_run / 2];
				/*!< half the input file */
	ulint		n_run	= 0;
				/*!< num of runs generated from this merge */

	MEM_CHECK_ADDRESSABLE(&block[0], 3 * srv_sort_buf_size);

	if (crypt_block) {
		MEM_CHECK_ADDRESSABLE(&crypt_block[0], 3 * srv_sort_buf_size);
	}

	ut_ad(ihalf < file->offset);

	of.fd = *tmpfd;
	of.offset = 0;
	of.n_rec = 0;

#ifdef POSIX_FADV_SEQUENTIAL
	/* The input file will be read sequentially, starting from the
	beginning and the middle.  In Linux, the POSIX_FADV_SEQUENTIAL
	affects the entire file.  Each block will be read exactly once. */
	posix_fadvise(file->fd, 0, 0,
		      POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE);
#endif /* POSIX_FADV_SEQUENTIAL */

	/* Merge blocks to the output file. */
	foffs0 = 0;
	foffs1 = ihalf;

	MEM_UNDEFINED(run_offset, *num_run * sizeof *run_offset);

	for (; foffs0 < ihalf && foffs1 < file->offset; foffs0++, foffs1++) {

		if (trx_is_interrupted(trx)) {
			return(DB_INTERRUPTED);
		}

		/* Remember the offset number for this run */
		run_offset[n_run++] = of.offset;

		error = row_merge_blocks(dup, file, block,
					 &foffs0, &foffs1, &of, stage,
					 crypt_block, space);

		if (error != DB_SUCCESS) {
			return(error);
		}

	}

	/* Copy the last blocks, if there are any. */

	while (foffs0 < ihalf) {

		if (UNIV_UNLIKELY(trx_is_interrupted(trx))) {
			return(DB_INTERRUPTED);
		}

		/* Remember the offset number for this run */
		run_offset[n_run++] = of.offset;

		if (!row_merge_blocks_copy(dup->index, file, block,
					   &foffs0, &of, stage,
					   crypt_block, space)) {
			return(DB_CORRUPTION);
		}
	}

	ut_ad(foffs0 == ihalf);

	while (foffs1 < file->offset) {

		if (trx_is_interrupted(trx)) {
			return(DB_INTERRUPTED);
		}

		/* Remember the offset number for this run */
		run_offset[n_run++] = of.offset;

		if (!row_merge_blocks_copy(dup->index, file, block,
					   &foffs1, &of, stage,
					   crypt_block, space)) {
			return(DB_CORRUPTION);
		}
	}

	ut_ad(foffs1 == file->offset);

	if (UNIV_UNLIKELY(of.n_rec != file->n_rec)) {
		return(DB_CORRUPTION);
	}

	ut_ad(n_run <= *num_run);

	*num_run = n_run;

	/* Each run can contain one or more offsets. As merge goes on,
	the number of runs (to merge) will reduce until we have one
	single run. So the number of runs will always be smaller than
	the number of offsets in file */
	ut_ad((*num_run) <= file->offset);

	/* The number of offsets in output file is always equal or
	smaller than input file */
	ut_ad(of.offset <= file->offset);

	/* Swap file descriptors for the next pass. */
	*tmpfd = file->fd;
	*file = of;

	MEM_UNDEFINED(&block[0], 3 * srv_sort_buf_size);

	return(DB_SUCCESS);
}

/** Merge disk files.
@param[in]	trx	transaction
@param[in]	dup	descriptor of index being created
@param[in,out]	file	file containing index entries
@param[in,out]	block	3 buffers
@param[in,out]	tmpfd	temporary file handle
@param[in,out]	stage	performance schema accounting object, used by
ALTER TABLE. If not NULL, stage->begin_phase_sort() will be called initially
and then stage->inc() will be called for each record processed.
@return DB_SUCCESS or error code */
dberr_t
row_merge_sort(
	trx_t*			trx,
	const row_merge_dup_t*	dup,
	merge_file_t*		file,
	row_merge_block_t*	block,
	pfs_os_file_t*			tmpfd,
	const bool		update_progress,
					/*!< in: update progress
					status variable or not */
	const double 		pct_progress,
					/*!< in: total progress percent
					until now */
	const double		pct_cost, /*!< in: current progress percent */
	row_merge_block_t*	crypt_block, /*!< in: crypt buf or NULL */
	ulint			space,	   /*!< in: space id */
	ut_stage_alter_t* 	stage)
{
	const ulint	half	= file->offset / 2;
	ulint		num_runs;
	ulint*		run_offset;
	dberr_t		error	= DB_SUCCESS;
	ulint		merge_count = 0;
	ulint		total_merge_sort_count;
	double		curr_progress = 0;

	DBUG_ENTER("row_merge_sort");

	/* Record the number of merge runs we need to perform */
	num_runs = file->offset;

	if (stage != NULL) {
		stage->begin_phase_sort(log2(double(num_runs)));
	}

	/* If num_runs are less than 1, nothing to merge */
	if (num_runs <= 1) {
		DBUG_RETURN(error);
	}

	total_merge_sort_count = ulint(ceil(log2(double(num_runs))));

	/* "run_offset" records each run's first offset number */
	run_offset = (ulint*) ut_malloc_nokey(file->offset * sizeof(ulint));

	/* This tells row_merge() where to start for the first round
	of merge. */
	run_offset[half] = half;

	/* The file should always contain at least one byte (the end
	of file marker).  Thus, it must be at least one block. */
	ut_ad(file->offset > 0);

	/* These thd_progress* calls will crash on sol10-64 when innodb_plugin
	is used. MDEV-9356: innodb.innodb_bug53290 fails (crashes) on
	sol10-64 in buildbot.
	*/
#ifndef UNIV_SOLARIS
	/* Progress report only for "normal" indexes. */
	if (!(dup->index->type & DICT_FTS)) {
		thd_progress_init(trx->mysql_thd, 1);
	}
#endif /* UNIV_SOLARIS */

	if (global_system_variables.log_warnings > 2) {
		sql_print_information("InnoDB: Online DDL : merge-sorting"
				      " has estimated " ULINTPF " runs",
				      num_runs);
	}

	/* Merge the runs until we have one big run */
	do {
		/* Report progress of merge sort to MySQL for
		show processlist progress field */
		/* Progress report only for "normal" indexes. */
#ifndef UNIV_SOLARIS
		if (!(dup->index->type & DICT_FTS)) {
			thd_progress_report(trx->mysql_thd, file->offset - num_runs, file->offset);
		}
#endif /* UNIV_SOLARIS */

		error = row_merge(trx, dup, file, block, tmpfd,
				  &num_runs, run_offset, stage,
				  crypt_block, space);

		if(update_progress) {
			merge_count++;
			curr_progress = (merge_count >= total_merge_sort_count) ?
				pct_cost :
				pct_cost * static_cast<double>(merge_count)
				/ static_cast<double>(total_merge_sort_count);
			/* presenting 10.12% as 1012 integer */;
			onlineddl_pct_progress = (ulint) ((pct_progress + curr_progress) * 100);
		}

		if (error != DB_SUCCESS) {
			break;
		}

		MEM_CHECK_DEFINED(run_offset, num_runs * sizeof *run_offset);
	} while (num_runs > 1);

	ut_free(run_offset);

	/* Progress report only for "normal" indexes. */
#ifndef UNIV_SOLARIS
	if (!(dup->index->type & DICT_FTS)) {
		thd_progress_end(trx->mysql_thd);
	}
#endif /* UNIV_SOLARIS */

	DBUG_RETURN(error);
}

/** Copy externally stored columns to the data tuple.
@param[in]	mrec		record containing BLOB pointers,
or NULL to use tuple instead
@param[in]	offsets		offsets of mrec
@param[in]	zip_size	compressed page size in bytes, or 0
@param[in,out]	tuple		data tuple
@param[in,out]	heap		memory heap */
static
void
row_merge_copy_blobs(
	const mrec_t*		mrec,
	const rec_offs*		offsets,
	ulint			zip_size,
	dtuple_t*		tuple,
	mem_heap_t*		heap)
{
	ut_ad(mrec == NULL || rec_offs_any_extern(offsets));

	for (ulint i = 0; i < dtuple_get_n_fields(tuple); i++) {
		ulint		len;
		const void*	data;
		dfield_t*	field = dtuple_get_nth_field(tuple, i);
		ulint		field_len;
		const byte*	field_data;

		if (!dfield_is_ext(field)) {
			continue;
		}

		ut_ad(!dfield_is_null(field));

		/* During the creation of a PRIMARY KEY, the table is
		X-locked, and we skip copying records that have been
		marked for deletion. Therefore, externally stored
		columns cannot possibly be freed between the time the
		BLOB pointers are read (row_merge_read_clustered_index())
		and dereferenced (below). */
		if (mrec == NULL) {
			field_data
				= static_cast<byte*>(dfield_get_data(field));
			field_len = dfield_get_len(field);

			ut_a(field_len >= BTR_EXTERN_FIELD_REF_SIZE);

			ut_a(memcmp(field_data + field_len
				     - BTR_EXTERN_FIELD_REF_SIZE,
				     field_ref_zero,
				     BTR_EXTERN_FIELD_REF_SIZE));

			data = btr_copy_externally_stored_field(
				&len, field_data, zip_size, field_len, heap);
		} else {
			data = btr_rec_copy_externally_stored_field(
				mrec, offsets, zip_size, i, &len, heap);
		}

		/* Because we have locked the table, any records
		written by incomplete transactions must have been
		rolled back already. There must not be any incomplete
		BLOB columns. */
		ut_a(data);

		dfield_set_data(field, data, len);
	}
}

/** Convert a merge record to a typed data tuple. Note that externally
stored fields are not copied to heap.
@param[in,out]	index	index on the table
@param[in]	mtuple	merge record
@param[in]	heap	memory heap from which memory needed is allocated
@return	index entry built. */
static
void
row_merge_mtuple_to_dtuple(
	dict_index_t*	index,
	dtuple_t*	dtuple,
	const mtuple_t* mtuple)
{
	ut_ad(!dict_index_is_ibuf(index));

	memcpy(dtuple->fields, mtuple->fields,
	       dtuple->n_fields * sizeof *mtuple->fields);
}

/** Insert sorted data tuples to the index.
@param[in]	index		index to be inserted
@param[in]	old_table	old table
@param[in]	fd		file descriptor
@param[in,out]	block		file buffer
@param[in]	row_buf		row_buf the sorted data tuples,
or NULL if fd, block will be used instead
@param[in,out]	btr_bulk	btr bulk instance
@param[in,out]	stage		performance schema accounting object, used by
ALTER TABLE. If not NULL stage->begin_phase_insert() will be called initially
and then stage->inc() will be called for each record that is processed.
@return DB_SUCCESS or error number */
static	MY_ATTRIBUTE((warn_unused_result))
dberr_t
row_merge_insert_index_tuples(
	dict_index_t*		index,
	const dict_table_t*	old_table,
	const pfs_os_file_t&	fd,
	row_merge_block_t*	block,
	const row_merge_buf_t*	row_buf,
	BtrBulk*		btr_bulk,
	const ib_uint64_t	table_total_rows, /*!< in: total rows of old table */
	const double		pct_progress,	/*!< in: total progress
						percent until now */
	const double		pct_cost, /*!< in: current progress percent
					  */
	row_merge_block_t*	crypt_block, /*!< in: crypt buf or NULL */
	ulint			space,	   /*!< in: space id */
	ut_stage_alter_t*	stage)
{
	const byte*		b;
	mem_heap_t*		heap;
	mem_heap_t*		tuple_heap;
	dberr_t			error = DB_SUCCESS;
	ulint			foffs = 0;
	rec_offs*		offsets;
	mrec_buf_t*		buf;
	ulint			n_rows = 0;
	dtuple_t*		dtuple;
	ib_uint64_t		inserted_rows = 0;
	double			curr_progress = 0;
	dict_index_t*		old_index = NULL;
	const mrec_t*		mrec  = NULL;
	mtr_t			mtr;


	DBUG_ENTER("row_merge_insert_index_tuples");

	ut_ad(!srv_read_only_mode);
	ut_ad(!(index->type & DICT_FTS));
	ut_ad(!dict_index_is_spatial(index));

	if (stage != NULL) {
		stage->begin_phase_insert();
	}

	tuple_heap = mem_heap_create(1000);

	{
		ulint i	= 1 + REC_OFFS_HEADER_SIZE
			+ dict_index_get_n_fields(index);
		heap = mem_heap_create(sizeof *buf + i * sizeof *offsets);
		offsets = static_cast<rec_offs*>(
			mem_heap_alloc(heap, i * sizeof *offsets));
		rec_offs_set_n_alloc(offsets, i);
		rec_offs_set_n_fields(offsets, dict_index_get_n_fields(index));
	}

	if (row_buf != NULL) {
		ut_ad(fd == OS_FILE_CLOSED);
		ut_ad(block == NULL);
		DBUG_EXECUTE_IF("row_merge_read_failure",
				error = DB_CORRUPTION;
				goto err_exit;);
		buf = NULL;
		b = NULL;
		dtuple = dtuple_create(
			heap, dict_index_get_n_fields(index));
		dtuple_set_n_fields_cmp(
			dtuple, dict_index_get_n_unique_in_tree(index));
	} else {
		b = block;
		dtuple = NULL;

		if (!row_merge_read(fd, foffs, block, crypt_block, space)) {
			error = DB_CORRUPTION;
			goto err_exit;
		} else {
			buf = static_cast<mrec_buf_t*>(
				mem_heap_alloc(heap, sizeof *buf));
		}
	}

	for (;;) {

		if (stage != NULL) {
			stage->inc();
		}

		if (row_buf != NULL) {
			if (n_rows >= row_buf->n_tuples) {
				break;
			}

			/* Convert merge tuple record from
			row buffer to data tuple record */
			row_merge_mtuple_to_dtuple(
				index, dtuple, &row_buf->tuples[n_rows]);
			n_rows++;
			/* BLOB pointers must be copied from dtuple */
			mrec = NULL;
		} else {
			b = row_merge_read_rec(block, buf, b, index,
					       fd, &foffs, &mrec, offsets,
					       crypt_block,
					       space);

			if (UNIV_UNLIKELY(!b)) {
				/* End of list, or I/O error */
				if (mrec) {
					error = DB_CORRUPTION;
				}
				break;
			}

			dtuple = row_rec_to_index_entry_low(
				mrec, index, offsets, tuple_heap);
		}

		old_index	= dict_table_get_first_index(old_table);

		if (dict_index_is_clust(index)
		    && dict_index_is_online_ddl(old_index)) {
			error = row_log_table_get_error(old_index);
			if (error != DB_SUCCESS) {
				break;
			}
		}

		if (dict_index_is_clust(index) && dtuple_get_n_ext(dtuple)) {
			/* Off-page columns can be fetched safely
			when concurrent modifications to the table
			are disabled. (Purge can process delete-marked
			records, but row_merge_read_clustered_index()
			would have skipped them.)

			When concurrent modifications are enabled,
			row_merge_read_clustered_index() will
			only see rows from transactions that were
			committed before the ALTER TABLE started
			(REPEATABLE READ).

			Any modifications after the
			row_merge_read_clustered_index() scan
			will go through row_log_table_apply().
			Any modifications to off-page columns
			will be tracked by
			row_log_table_blob_alloc() and
			row_log_table_blob_free(). */
			row_merge_copy_blobs(
				mrec, offsets, old_table->space->zip_size(),
				dtuple, tuple_heap);
		}

		ut_ad(dtuple_validate(dtuple));
		error = btr_bulk->insert(dtuple);

		if (error != DB_SUCCESS) {
			goto err_exit;
		}

		mem_heap_empty(tuple_heap);

		/* Increment innodb_onlineddl_pct_progress status variable */
		inserted_rows++;
		if(inserted_rows % 1000 == 0) {
			/* Update progress for each 1000 rows */
			curr_progress = (inserted_rows >= table_total_rows ||
				table_total_rows <= 0) ?
				pct_cost :
				pct_cost * static_cast<double>(inserted_rows)
				/ static_cast<double>(table_total_rows);

			/* presenting 10.12% as 1012 integer */;
			onlineddl_pct_progress = (ulint) ((pct_progress + curr_progress) * 100);
		}
	}

err_exit:
	mem_heap_free(tuple_heap);
	mem_heap_free(heap);

	DBUG_RETURN(error);
}

/*********************************************************************//**
Drop an index that was created before an error occurred.
The data dictionary must have been locked exclusively by the caller,
because the transaction will not be committed. */
static
void
row_merge_drop_index_dict(
/*======================*/
	trx_t*		trx,	/*!< in/out: dictionary transaction */
	index_id_t	index_id)/*!< in: index identifier */
{
	static const char sql[] =
		"PROCEDURE DROP_INDEX_PROC () IS\n"
		"BEGIN\n"
		"DELETE FROM SYS_FIELDS WHERE INDEX_ID=:indexid;\n"
		"DELETE FROM SYS_INDEXES WHERE ID=:indexid;\n"
		"END;\n";
	dberr_t		error;
	pars_info_t*	info;

	ut_ad(!srv_read_only_mode);
	ut_ad(trx->dict_operation_lock_mode);
	ut_ad(trx->dict_operation);
	ut_ad(dict_sys.locked());

	info = pars_info_create();
	pars_info_add_ull_literal(info, "indexid", index_id);
	trx->op_info = "dropping index from dictionary";
	error = que_eval_sql(info, sql, trx);

	if (error != DB_SUCCESS) {
		/* Even though we ensure that DDL transactions are WAIT
		and DEADLOCK free, we could encounter other errors e.g.,
		DB_TOO_MANY_CONCURRENT_TRXS. */
		trx->error_state = DB_SUCCESS;

		ib::error() << "row_merge_drop_index_dict failed with error "
			<< error;
	}

	trx->op_info = "";
}

/*********************************************************************//**
Drop indexes that were created before an error occurred.
The data dictionary must have been locked exclusively by the caller,
because the transaction will not be committed. */
static
void
row_merge_drop_indexes_dict(
/*========================*/
	trx_t*		trx,	/*!< in/out: dictionary transaction */
	table_id_t	table_id)/*!< in: table identifier */
{
	static const char sql[] =
		"PROCEDURE DROP_INDEXES_PROC () IS\n"
		"ixid CHAR;\n"
		"found INT;\n"

		"DECLARE CURSOR index_cur IS\n"
		" SELECT ID FROM SYS_INDEXES\n"
		" WHERE TABLE_ID=:tableid AND\n"
		" SUBSTR(NAME,0,1)='" TEMP_INDEX_PREFIX_STR "'\n"
		"FOR UPDATE;\n"

		"BEGIN\n"
		"found := 1;\n"
		"OPEN index_cur;\n"
		"WHILE found = 1 LOOP\n"
		"  FETCH index_cur INTO ixid;\n"
		"  IF (SQL % NOTFOUND) THEN\n"
		"    found := 0;\n"
		"  ELSE\n"
		"    DELETE FROM SYS_FIELDS WHERE INDEX_ID=ixid;\n"
		"    DELETE FROM SYS_INDEXES WHERE CURRENT OF index_cur;\n"
		"  END IF;\n"
		"END LOOP;\n"
		"CLOSE index_cur;\n"

		"END;\n";
	dberr_t		error;
	pars_info_t*	info;

	ut_ad(!srv_read_only_mode);
	ut_ad(trx->dict_operation_lock_mode);
	ut_ad(trx->dict_operation);
	ut_ad(dict_sys.locked());

	/* It is possible that table->n_ref_count > 1 when
	locked=TRUE. In this case, all code that should have an open
	handle to the table be waiting for the next statement to execute,
	or waiting for a meta-data lock.

	A concurrent purge will be prevented by dict_sys.latch. */

	info = pars_info_create();
	pars_info_add_ull_literal(info, "tableid", table_id);
	trx->op_info = "dropping indexes";
	error = que_eval_sql(info, sql, trx);

	switch (error) {
	case DB_SUCCESS:
		break;
	default:
		/* Even though we ensure that DDL transactions are WAIT
		and DEADLOCK free, we could encounter other errors e.g.,
		DB_TOO_MANY_CONCURRENT_TRXS. */
		ib::error() << "row_merge_drop_indexes_dict failed with error "
			<< error;
		/* fall through */
	case DB_TOO_MANY_CONCURRENT_TRXS:
		trx->error_state = DB_SUCCESS;
	}

	trx->op_info = "";
}

/** Drop common internal tables if all fulltext indexes are dropped
@param trx   transaction
@param table user table */
static void row_merge_drop_fulltext_indexes(trx_t *trx, dict_table_t *table)
{
  if (DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID) ||
      !table->fts ||
      !ib_vector_is_empty(table->fts->indexes))
    return;

  for (const dict_index_t *index= dict_table_get_first_index(table);
       index; index= dict_table_get_next_index(index))
    if (index->type & DICT_FTS)
      return;

  fts_optimize_remove_table(table);
  fts_drop_tables(trx, *table);
  fts_free(table);
  DICT_TF2_FLAG_UNSET(table, DICT_TF2_FTS);
}

/** Drop indexes that were created before an error occurred.
The data dictionary must have been locked exclusively by the caller,
because the transaction will not be committed.
@param trx              dictionary transaction
@param table            table containing the indexes
@param locked           True if table is locked,
                        false - may need to do lazy drop
@param alter_trx        Alter table transaction */
void
row_merge_drop_indexes(
        trx_t*          trx,
        dict_table_t*   table,
        bool            locked,
        const trx_t*    alter_trx)
{
	dict_index_t*	index;
	dict_index_t*	next_index;

	ut_ad(!srv_read_only_mode);
	ut_ad(trx->dict_operation_lock_mode);
	ut_ad(trx->dict_operation);
	ut_ad(dict_sys.locked());

	index = dict_table_get_first_index(table);
	ut_ad(dict_index_is_clust(index));
	ut_ad(dict_index_get_online_status(index) == ONLINE_INDEX_COMPLETE);

	/* the caller should have an open handle to the table */
	ut_ad(table->get_ref_count() >= 1);

	/* It is possible that table->n_ref_count > 1 when
	locked=TRUE. In this case, all code that should have an open
	handle to the table be waiting for the next statement to execute,
	or waiting for a meta-data lock.

	A concurrent purge will be prevented by MDL. */

	if (!locked && (table->get_ref_count() > 1
			|| table->has_lock_other_than(alter_trx))) {
		while ((index = dict_table_get_next_index(index)) != NULL) {
			ut_ad(!dict_index_is_clust(index));

			switch (dict_index_get_online_status(index)) {
			case ONLINE_INDEX_ABORTED_DROPPED:
				continue;
			case ONLINE_INDEX_COMPLETE:
				if (index->is_committed()) {
					/* Do nothing to already
					published indexes. */
				} else if (index->type & DICT_FTS) {
					/* Drop a completed FULLTEXT
					index, due to a timeout during
					MDL upgrade for
					commit_inplace_alter_table().
					Because only concurrent reads
					are allowed (and they are not
					seeing this index yet) we
					are safe to drop the index. */
					dict_index_t* prev = UT_LIST_GET_PREV(
						indexes, index);
					/* At least there should be
					the clustered index before
					this one. */
					ut_ad(prev);
					ut_a(table->fts);
					fts_drop_index(table, index, trx);
					row_merge_drop_index_dict(
						trx, index->id);
					/* We can remove a DICT_FTS
					index from the cache, because
					we do not allow ADD FULLTEXT INDEX
					with LOCK=NONE. If we allowed that,
					we should exclude FTS entries from
					prebuilt->ins_node->entry_list
					in ins_node_create_entry_list(). */
#ifdef BTR_CUR_HASH_ADAPT
					ut_ad(!index->search_info->ref_count);
#endif /* BTR_CUR_HASH_ADAPT */
					dict_index_remove_from_cache(
						table, index);
					index = prev;
				} else {
					index->lock.x_lock(SRW_LOCK_CALL);
					dict_index_set_online_status(
						index, ONLINE_INDEX_ABORTED);
					index->type |= DICT_CORRUPT;
					table->drop_aborted = TRUE;
					goto drop_aborted;
				}
				continue;
			case ONLINE_INDEX_CREATION:
				index->lock.x_lock(SRW_LOCK_CALL);
				ut_ad(!index->is_committed());
				row_log_abort_sec(index);
			drop_aborted:
				index->lock.x_unlock();

				DEBUG_SYNC_C("merge_drop_index_after_abort");
				/* covered by dict_sys.latch */
				MONITOR_INC(MONITOR_BACKGROUND_DROP_INDEX);
				/* fall through */
			case ONLINE_INDEX_ABORTED:
				/* Drop the index tree from the
				data dictionary and free it from
				the tablespace, but keep the object
				in the data dictionary cache. */
				row_merge_drop_index_dict(trx, index->id);
				index->lock.x_lock(SRW_LOCK_CALL);
				dict_index_set_online_status(
					index, ONLINE_INDEX_ABORTED_DROPPED);
				index->lock.x_unlock();
				table->drop_aborted = TRUE;
				continue;
			}
			ut_error;
		}

		row_merge_drop_fulltext_indexes(trx, table);
		return;
	}

	row_merge_drop_indexes_dict(trx, table->id);

	/* Invalidate all row_prebuilt_t::ins_graph that are referring
	to this table. That is, force row_get_prebuilt_insert_row() to
	rebuild prebuilt->ins_node->entry_list). */
	if (table->def_trx_id < trx->id) {
		table->def_trx_id = trx->id;
	} else {
		ut_ad(table->def_trx_id == trx->id || table->name.part());
	}

	next_index = dict_table_get_next_index(index);

	while ((index = next_index) != NULL) {
		/* read the next pointer before freeing the index */
		next_index = dict_table_get_next_index(index);

		ut_ad(!dict_index_is_clust(index));

		if (!index->is_committed()) {
			/* If it is FTS index, drop from table->fts
			and also drop its auxiliary tables */
			if (index->type & DICT_FTS) {
				ut_a(table->fts);
				fts_drop_index(table, index, trx);
			}

			switch (dict_index_get_online_status(index)) {
			case ONLINE_INDEX_CREATION:
				/* This state should only be possible
				when prepare_inplace_alter_table() fails
				after invoking row_merge_create_index().
				In inplace_alter_table(),
				row_merge_build_indexes()
				should never leave the index in this state.
				It would invoke row_log_abort_sec() on
				failure. */
			case ONLINE_INDEX_COMPLETE:
				/* In these cases, we are able to drop
				the index straight. The DROP INDEX was
				never deferred. */
				break;
			case ONLINE_INDEX_ABORTED:
			case ONLINE_INDEX_ABORTED_DROPPED:
				/* covered by dict_sys.latch */
				MONITOR_DEC(MONITOR_BACKGROUND_DROP_INDEX);
			}

			dict_index_remove_from_cache(table, index);
		}
	}

	row_merge_drop_fulltext_indexes(trx, table);
	table->drop_aborted = FALSE;
	ut_d(dict_table_check_for_dup_indexes(table, CHECK_ALL_COMPLETE));
}

/** Drop fulltext indexes */
static ibool row_merge_drop_fts(void *node, void *trx)
{
   auto s= static_cast<sel_node_t*>(node);

   const dfield_t *table_id= que_node_get_val(s->select_list);
   ut_ad(table_id->type.mtype == DATA_BINARY);
   node= que_node_get_next(s->select_list);
   ut_ad(!que_node_get_next(node));
   const dfield_t *index_id= que_node_get_val(node);
   ut_ad(index_id->type.mtype == DATA_BINARY);

   static const char sql[]=
     "PROCEDURE DROP_TABLES_PROC () IS\n"
     "tid CHAR;\n"
     "iid CHAR;\n"

     "DECLARE CURSOR cur_tab IS\n"
     "SELECT ID FROM SYS_TABLES\n"
     "WHERE INSTR(NAME,:name)+45=LENGTH(NAME)"
     " AND INSTR('123456',SUBSTR(NAME,LENGTH(NAME)-1,1))>0"
     " FOR UPDATE;\n"

     "DECLARE CURSOR cur_idx IS\n"
     "SELECT ID FROM SYS_INDEXES\n"
     "WHERE TABLE_ID = tid FOR UPDATE;\n"

     "BEGIN\n"
     "OPEN cur_tab;\n"
     "WHILE 1 = 1 LOOP\n"
     "  FETCH cur_tab INTO tid;\n"
     "  IF (SQL % NOTFOUND) THEN EXIT; END IF;\n"
     "  OPEN cur_idx;\n"
     "  WHILE 1 = 1 LOOP\n"
     "    FETCH cur_idx INTO iid;\n"
     "    IF (SQL % NOTFOUND) THEN EXIT; END IF;\n"
     "    DELETE FROM SYS_FIELDS WHERE INDEX_ID=iid;\n"
     "    DELETE FROM SYS_INDEXES WHERE CURRENT OF cur_idx;\n"
     "  END LOOP;\n"
     "  CLOSE cur_idx;\n"
     "  DELETE FROM SYS_COLUMNS WHERE TABLE_ID=tid;\n"
     "  DELETE FROM SYS_TABLES WHERE CURRENT OF cur_tab;\n"
     "END LOOP;\n"
     "CLOSE cur_tab;\n"
     "END;\n";

   if (table_id->len == 8 && index_id->len == 8)
   {
     char buf[sizeof "/FTS_0000000000000000_0000000000000000_INDEX_"];
     snprintf(buf, sizeof buf, "/FTS_%016llx_%016llx_INDEX_",
              static_cast<ulonglong>
              (mach_read_from_8(static_cast<const byte*>(table_id->data))),
              static_cast<ulonglong>
              (mach_read_from_8(static_cast<const byte*>(index_id->data))));
     auto pinfo= pars_info_create();
     pars_info_add_str_literal(pinfo, "name", buf);
     que_eval_sql(pinfo, sql, static_cast<trx_t*>(trx));
   }

   return true;
}

/** During recovery, drop recovered index stubs that were created in
prepare_inplace_alter_table_dict(). */
void row_merge_drop_temp_indexes()
{
	static_assert(DICT_FTS == 32, "compatibility");

	static const char sql[] =
		"PROCEDURE DROP_TEMP_INDEXES_PROC () IS\n"
		"ixid CHAR;\n"
		"found INT;\n"

		"DECLARE FUNCTION drop_fts;\n"

		"DECLARE CURSOR fts_cur IS\n"
		" SELECT TABLE_ID,ID FROM SYS_INDEXES\n"
		" WHERE TYPE=32"
		" AND SUBSTR(NAME,0,1)='" TEMP_INDEX_PREFIX_STR "'\n"
		" FOR UPDATE;\n"

		"DECLARE CURSOR index_cur IS\n"
		" SELECT ID FROM SYS_INDEXES\n"
		" WHERE SUBSTR(NAME,0,1)='" TEMP_INDEX_PREFIX_STR "'\n"
		"FOR UPDATE;\n"

		"BEGIN\n"
		"found := 1;\n"
		"OPEN fts_cur;\n"
		"WHILE found = 1 LOOP\n"
		"  FETCH fts_cur INTO drop_fts();\n"
		"  IF (SQL % NOTFOUND) THEN\n"
		"    found := 0;\n"
		"  END IF;\n"
		"END LOOP;\n"
		"CLOSE fts_cur;\n"

		"OPEN index_cur;\n"
		"WHILE found = 1 LOOP\n"
		"  FETCH index_cur INTO ixid;\n"
		"  IF (SQL % NOTFOUND) THEN\n"
		"    found := 0;\n"
		"  ELSE\n"
		"    DELETE FROM SYS_FIELDS WHERE INDEX_ID=ixid;\n"
		"    DELETE FROM SYS_INDEXES WHERE CURRENT OF index_cur;\n"
		"  END IF;\n"
		"END LOOP;\n"
		"CLOSE index_cur;\n"
		"END;\n";

	/* Load the table definitions that contain partially defined
	indexes, so that the data dictionary information can be checked
	when accessing the tablename.ibd files. */
	trx_t* trx = trx_create();
	trx_start_for_ddl(trx);
	trx->op_info = "dropping partially created indexes";
	dberr_t error = lock_sys_tables(trx);

	row_mysql_lock_data_dictionary(trx);
	/* Ensure that this transaction will be rolled back and locks
	will be released, if the server gets killed before the commit
	gets written to the redo log. */
	trx->dict_operation = true;

	trx->op_info = "dropping indexes";

	pars_info_t* pinfo = pars_info_create();
	pars_info_bind_function(pinfo, "drop_fts", row_merge_drop_fts, trx);
	if (error == DB_SUCCESS) {
		error = que_eval_sql(pinfo, sql, trx);
	}

	if (error) {
		/* Even though we ensure that DDL transactions are WAIT
		and DEADLOCK free, we could encounter other errors e.g.,
		DB_TOO_MANY_CONCURRENT_TRXS. */
		trx->error_state = DB_SUCCESS;

		ib::error() << "row_merge_drop_temp_indexes(): " << error;
	}

	trx_commit_for_mysql(trx);
	row_mysql_unlock_data_dictionary(trx);
	trx->free();
}


/** Create temporary merge files in the given paramater path, and if
UNIV_PFS_IO defined, register the file descriptor with Performance Schema.
@param[in]	path	location for creating temporary merge files, or NULL
@return File descriptor */
pfs_os_file_t
row_merge_file_create_low(
	const char*	path)
{
	innodb_wait_allow_writes();
	if (!path) {
		path = mysql_tmpdir;
	}
#ifdef UNIV_PFS_IO
	/* This temp file open does not go through normal
	file APIs, add instrumentation to register with
	performance schema */
	struct PSI_file_locker*	locker;
	PSI_file_locker_state	state;
	static const char label[] = "/Innodb Merge Temp File";
	char* name = static_cast<char*>(
		ut_malloc_nokey(strlen(path) + sizeof label));
	strcpy(name, path);
	strcat(name, label);

	register_pfs_file_open_begin(
		&state, locker, innodb_temp_file_key,
		PSI_FILE_CREATE, path ? name : label, __FILE__, __LINE__);

#endif
	DBUG_ASSERT(strlen(path) + 2 <= FN_REFLEN);
	char filename[FN_REFLEN];
	File f = create_temp_file(filename, path, "ib",
				  O_BINARY | O_SEQUENTIAL,
				  MYF(MY_WME | MY_TEMPORARY));
	pfs_os_file_t fd = IF_WIN((os_file_t)my_get_osfhandle(f), f);

#ifdef UNIV_PFS_IO
	register_pfs_file_open_end(locker, fd, 
		(fd == OS_FILE_CLOSED)?NULL:&fd);
	ut_free(name);
#endif

	if (fd == OS_FILE_CLOSED) {
		ib::error() << "Cannot create temporary merge file";
	}
	return(fd);
}


/** Create a merge file in the given location.
@param[out]	merge_file	merge file structure
@param[in]	path		location for creating temporary file, or NULL
@return file descriptor, or OS_FILE_CLOSED on error */
pfs_os_file_t
row_merge_file_create(
	merge_file_t*	merge_file,
	const char*	path)
{
	merge_file->fd = row_merge_file_create_low(path);
	merge_file->offset = 0;
	merge_file->n_rec = 0;

	if (merge_file->fd != OS_FILE_CLOSED) {
		if (srv_disable_sort_file_cache) {
			os_file_set_nocache(merge_file->fd,
				"row0merge.cc", "sort");
		}
	}
	return(merge_file->fd);
}

/*********************************************************************//**
Destroy a merge file. And de-register the file from Performance Schema
if UNIV_PFS_IO is defined. */
void
row_merge_file_destroy_low(
/*=======================*/
	const pfs_os_file_t& fd)	/*!< in: merge file descriptor */
{
	if (fd != OS_FILE_CLOSED) {
		int res = mysql_file_close(IF_WIN(my_win_handle2File((os_file_t)fd), fd),
					   MYF(MY_WME));
		ut_a(res != -1);
	}
}
/*********************************************************************//**
Destroy a merge file. */
void
row_merge_file_destroy(
/*===================*/
	merge_file_t*	merge_file)	/*!< in/out: merge file structure */
{
	ut_ad(!srv_read_only_mode);

	if (merge_file->fd != OS_FILE_CLOSED) {
		row_merge_file_destroy_low(merge_file->fd);
		merge_file->fd = OS_FILE_CLOSED;
	}
}

/*********************************************************************//**
Rename an index in the dictionary that was created. The data
dictionary must have been locked exclusively by the caller, because
the transaction will not be committed.
@return DB_SUCCESS if all OK */
dberr_t
row_merge_rename_index_to_add(
/*==========================*/
	trx_t*		trx,		/*!< in/out: transaction */
	table_id_t	table_id,	/*!< in: table identifier */
	index_id_t	index_id)	/*!< in: index identifier */
{
	dberr_t		err = DB_SUCCESS;
	pars_info_t*	info = pars_info_create();

	/* We use the private SQL parser of Innobase to generate the
	query graphs needed in renaming indexes. */

	static const char rename_index[] =
		"PROCEDURE RENAME_INDEX_PROC () IS\n"
		"BEGIN\n"
		"UPDATE SYS_INDEXES SET NAME=SUBSTR(NAME,1,LENGTH(NAME)-1)\n"
		"WHERE TABLE_ID = :tableid AND ID = :indexid;\n"
		"END;\n";

	ut_ad(trx->dict_operation_lock_mode);
	ut_ad(trx->dict_operation);

	trx->op_info = "renaming index to add";

	pars_info_add_ull_literal(info, "tableid", table_id);
	pars_info_add_ull_literal(info, "indexid", index_id);

	err = que_eval_sql(info, rename_index, trx);

	if (err != DB_SUCCESS) {
		/* Even though we ensure that DDL transactions are WAIT
		and DEADLOCK free, we could encounter other errors e.g.,
		DB_TOO_MANY_CONCURRENT_TRXS. */
		trx->error_state = DB_SUCCESS;

		ib::error() << "row_merge_rename_index_to_add failed with"
			" error " << err;
	}

	trx->op_info = "";

	return(err);
}

/** Create the index and load in to the dictionary.
@param[in,out]	table		the index is on this table
@param[in]	index_def	the index definition
@param[in]	add_v		new virtual columns added along with add
				index call
@return index, or NULL on error */
dict_index_t*
row_merge_create_index(
	dict_table_t*		table,
	const index_def_t*	index_def,
	const dict_add_v_col_t*	add_v)
{
	dict_index_t*	index;
	ulint		n_fields = index_def->n_fields;
	ulint		i;
	ulint		n_add_vcol = 0;

	DBUG_ENTER("row_merge_create_index");

	ut_ad(!srv_read_only_mode);

	/* Create the index prototype, using the passed in def, this is not
	a persistent operation. We pass 0 as the space id, and determine at
	a lower level the space id where to store the table. */

	index = dict_mem_index_create(table, index_def->name,
				      index_def->ind_type, n_fields);
	index->set_committed(index_def->rebuild);

	for (i = 0; i < n_fields; i++) {
		const char*	name;
		index_field_t*	ifield = &index_def->fields[i];

		if (ifield->is_v_col) {
			if (ifield->col_no >= table->n_v_def) {
				ut_ad(ifield->col_no < table->n_v_def
				      + add_v->n_v_col);
				ut_ad(ifield->col_no >= table->n_v_def);
				name = add_v->v_col_name[
					ifield->col_no - table->n_v_def];
				n_add_vcol++;
			} else {
				name = dict_table_get_v_col_name(
					table, ifield->col_no);
			}
		} else {
			name = dict_table_get_col_name(table, ifield->col_no);
		}

		dict_mem_index_add_field(index, name, ifield->prefix_len);
	}

	if (n_add_vcol) {
		index->assign_new_v_col(n_add_vcol);
	}

	DBUG_RETURN(index);
}

/*********************************************************************//**
Check if a transaction can use an index. */
bool
row_merge_is_index_usable(
/*======================*/
	const trx_t*		trx,	/*!< in: transaction */
	const dict_index_t*	index)	/*!< in: index to check */
{
	if (!index->is_primary()
	    && dict_index_is_online_ddl(index)) {
		/* Indexes that are being created are not useable. */
		return(false);
	}

	return(!index->is_corrupted()
	       && (index->table->is_temporary() || index->table->no_rollback()
		   || index->trx_id == 0
		   || !trx->read_view.is_open()
		   || trx->read_view.changes_visible(
			   index->trx_id,
			   index->table->name)));
}

/** Build indexes on a table by reading a clustered index, creating a temporary
file containing index entries, merge sorting these index entries and inserting
sorted index entries to indexes.
@param[in]	trx		transaction
@param[in]	old_table	table where rows are read from
@param[in]	new_table	table where indexes are created; identical to
old_table unless creating a PRIMARY KEY
@param[in]	online		true if creating indexes online
@param[in]	indexes		indexes to be created
@param[in]	key_numbers	MySQL key numbers
@param[in]	n_indexes	size of indexes[]
@param[in,out]	table		MySQL table, for reporting erroneous key value
if applicable
@param[in]	defaults	default values of added, changed columns, or NULL
@param[in]	col_map		mapping of old column numbers to new ones, or
NULL if old_table == new_table
@param[in]	add_autoinc	number of added AUTO_INCREMENT columns, or
ULINT_UNDEFINED if none is added
@param[in,out]	sequence	autoinc sequence
@param[in]	skip_pk_sort	whether the new PRIMARY KEY will follow
existing order
@param[in,out]	stage		performance schema accounting object, used by
ALTER TABLE. stage->begin_phase_read_pk() will be called at the beginning of
this function and it will be passed to other functions for further accounting.
@param[in]	add_v		new virtual columns added along with indexes
@param[in]	eval_table	mysql table used to evaluate virtual column
				value, see innobase_get_computed_value().
@param[in]	allow_not_null	allow the conversion from null to not-null
@return DB_SUCCESS or error code */
dberr_t
row_merge_build_indexes(
	trx_t*			trx,
	dict_table_t*		old_table,
	dict_table_t*		new_table,
	bool			online,
	dict_index_t**		indexes,
	const ulint*		key_numbers,
	ulint			n_indexes,
	struct TABLE*		table,
	const dtuple_t*		defaults,
	const ulint*		col_map,
	ulint			add_autoinc,
	ib_sequence_t&		sequence,
	bool			skip_pk_sort,
	ut_stage_alter_t*	stage,
	const dict_add_v_col_t*	add_v,
	struct TABLE*		eval_table,
	bool			allow_not_null)
{
	merge_file_t*		merge_files;
	row_merge_block_t*	block;
	ut_new_pfx_t		block_pfx;
	size_t			block_size;
	ut_new_pfx_t		crypt_pfx;
	row_merge_block_t*	crypt_block = NULL;
	ulint			i;
	ulint			j;
	dberr_t			error;
	pfs_os_file_t		tmpfd = OS_FILE_CLOSED;
	dict_index_t*		fts_sort_idx = NULL;
	fts_psort_t*		psort_info = NULL;
	fts_psort_t*		merge_info = NULL;
	bool			fts_psort_initiated = false;

	double total_static_cost = 0;
	double total_dynamic_cost = 0;
	ulint total_index_blocks = 0;
	double pct_cost=0;
	double pct_progress=0;

	DBUG_ENTER("row_merge_build_indexes");

	ut_ad(!srv_read_only_mode);
	ut_ad((old_table == new_table) == !col_map);
	ut_ad(!defaults || col_map);

	stage->begin_phase_read_pk(skip_pk_sort && new_table != old_table
				   ? n_indexes - 1
				   : n_indexes);

	/* Allocate memory for merge file data structure and initialize
	fields */

	ut_allocator<row_merge_block_t>	alloc(mem_key_row_merge_sort);

	/* This will allocate "3 * srv_sort_buf_size" elements of type
	row_merge_block_t. The latter is defined as byte. */
	block_size = 3 * srv_sort_buf_size;
	block = alloc.allocate_large(block_size, &block_pfx);

	if (block == NULL) {
		DBUG_RETURN(DB_OUT_OF_MEMORY);
	}

	crypt_pfx.m_size = 0; /* silence bogus -Wmaybe-uninitialized */
	TRASH_ALLOC(&crypt_pfx, sizeof crypt_pfx);

	if (log_tmp_is_encrypted()) {
		crypt_block = static_cast<row_merge_block_t*>(
			alloc.allocate_large(block_size,
					     &crypt_pfx));

		if (crypt_block == NULL) {
			DBUG_RETURN(DB_OUT_OF_MEMORY);
		}
	}

	trx_start_if_not_started_xa(trx, true);
	ulint	n_merge_files = 0;

	for (ulint i = 0; i < n_indexes; i++)
	{
		if (!dict_index_is_spatial(indexes[i])) {
			n_merge_files++;
		}
	}

	merge_files = static_cast<merge_file_t*>(
		ut_malloc_nokey(n_merge_files * sizeof *merge_files));

	/* Initialize all the merge file descriptors, so that we
	don't call row_merge_file_destroy() on uninitialized
	merge file descriptor */

	for (i = 0; i < n_merge_files; i++) {
		merge_files[i].fd = OS_FILE_CLOSED;
		merge_files[i].offset = 0;
		merge_files[i].n_rec = 0;
	}

	total_static_cost = COST_BUILD_INDEX_STATIC
		* static_cast<double>(n_indexes) + COST_READ_CLUSTERED_INDEX;
	total_dynamic_cost = COST_BUILD_INDEX_DYNAMIC
		* static_cast<double>(n_indexes);
	for (i = 0; i < n_indexes; i++) {
		if (indexes[i]->type & DICT_FTS) {
			ibool	opt_doc_id_size = FALSE;

			/* To build FTS index, we would need to extract
			doc's word, Doc ID, and word's position, so
			we need to build a "fts sort index" indexing
			on above three 'fields' */
			fts_sort_idx = row_merge_create_fts_sort_index(
				indexes[i], old_table, &opt_doc_id_size);

			row_merge_dup_t*	dup
				= static_cast<row_merge_dup_t*>(
					ut_malloc_nokey(sizeof *dup));
			dup->index = fts_sort_idx;
			dup->table = table;
			dup->col_map = col_map;
			dup->n_dup = 0;

			/* This can fail e.g. if temporal files can't be
			created */
			if (!row_fts_psort_info_init(
					trx, dup, new_table, opt_doc_id_size,
					old_table->space->zip_size(),
					&psort_info, &merge_info)) {
				error = DB_CORRUPTION;
				goto func_exit;
			}

			/* We need to ensure that we free the resources
			allocated */
			fts_psort_initiated = true;
		}
	}

	if (global_system_variables.log_warnings > 2) {
		sql_print_information("InnoDB: Online DDL : Start reading"
				      " clustered index of the table"
				      " and create temporary files");
	}

	pct_cost = COST_READ_CLUSTERED_INDEX * 100 / (total_static_cost + total_dynamic_cost);

	/* Do not continue if we can't encrypt table pages */
	if (!old_table->is_readable() ||
	    !new_table->is_readable()) {
		error = DB_DECRYPTION_FAILED;
		ib_push_warning(trx->mysql_thd, DB_DECRYPTION_FAILED,
			"Table %s is encrypted but encryption service or"
			" used key_id is not available. "
			" Can't continue reading table.",
			!old_table->is_readable() ? old_table->name.m_name :
				new_table->name.m_name);
		goto func_exit;
	}

	/* Read clustered index of the table and create files for
	secondary index entries for merge sort */
	error = row_merge_read_clustered_index(
		trx, table, old_table, new_table, online, indexes,
		fts_sort_idx, psort_info, merge_files, key_numbers,
		n_indexes, defaults, add_v, col_map, add_autoinc,
		sequence, block, skip_pk_sort, &tmpfd, stage,
		pct_cost, crypt_block, eval_table, allow_not_null);

	stage->end_phase_read_pk();

	pct_progress += pct_cost;

	if (global_system_variables.log_warnings > 2) {
		sql_print_information("InnoDB: Online DDL : End of reading "
				      "clustered index of the table"
				      " and create temporary files");
	}

	for (i = 0; i < n_merge_files; i++) {
		total_index_blocks += merge_files[i].offset;
	}

	if (error != DB_SUCCESS) {
		goto func_exit;
	}

	DEBUG_SYNC_C("row_merge_after_scan");

	/* Now we have files containing index entries ready for
	sorting and inserting. */

	for (ulint k = 0, i = 0; i < n_indexes; i++) {
		dict_index_t*	sort_idx = indexes[i];

		if (dict_index_is_spatial(sort_idx)) {
			continue;
		}

		if (indexes[i]->type & DICT_FTS) {

			sort_idx = fts_sort_idx;

			if (FTS_PLL_MERGE) {
				row_fts_start_parallel_merge(merge_info);
				for (j = 0; j < FTS_NUM_AUX_INDEX; j++) {
					merge_info[j].task->wait();
					delete merge_info[j].task;
				}
			} else {
				/* This cannot report duplicates; an
				assertion would fail in that case. */
				error = row_fts_merge_insert(
					sort_idx, new_table,
					psort_info, 0);
			}

#ifdef FTS_INTERNAL_DIAG_PRINT
			DEBUG_FTS_SORT_PRINT("FTS_SORT: Complete Insert\n");
#endif
		} else if (merge_files[k].fd != OS_FILE_CLOSED) {
			char	buf[NAME_LEN + 1];
			row_merge_dup_t	dup = {
				sort_idx, table, col_map, 0};

			pct_cost = (COST_BUILD_INDEX_STATIC +
				    (total_dynamic_cost
				     * static_cast<double>(merge_files[k].offset)
				     / static_cast<double>(total_index_blocks)))
				/ (total_static_cost + total_dynamic_cost)
				* PCT_COST_MERGESORT_INDEX * 100;
			char*	bufend = innobase_convert_name(
				buf, sizeof buf,
				indexes[i]->name,
				strlen(indexes[i]->name),
				trx->mysql_thd);
			buf[bufend - buf]='\0';

			if (global_system_variables.log_warnings > 2) {
				sql_print_information("InnoDB: Online DDL :"
						      " Start merge-sorting"
						      " index %s"
						      " (" ULINTPF
						      " / " ULINTPF "),"
						      " estimated cost :"
						      " %2.4f",
						      buf, i + 1, n_indexes,
						      pct_cost);
			}

			error = row_merge_sort(
					trx, &dup, &merge_files[k],
					block, &tmpfd, true,
					pct_progress, pct_cost,
					crypt_block, new_table->space_id,
					stage);

			pct_progress += pct_cost;

			if (global_system_variables.log_warnings > 2) {
				sql_print_information("InnoDB: Online DDL :"
						      " End of "
						      " merge-sorting index %s"
						      " (" ULINTPF
						      " / " ULINTPF ")",
						      buf, i + 1, n_indexes);
			}

			if (error == DB_SUCCESS) {
				BtrBulk	btr_bulk(sort_idx, trx);

				pct_cost = (COST_BUILD_INDEX_STATIC +
					    (total_dynamic_cost
					     * static_cast<double>(
						     merge_files[k].offset)
					     / static_cast<double>(
						     total_index_blocks)))
					/ (total_static_cost
					   + total_dynamic_cost)
					* PCT_COST_INSERT_INDEX * 100;

				if (global_system_variables.log_warnings > 2) {
					sql_print_information(
						"InnoDB: Online DDL : Start "
						"building index %s"
						" (" ULINTPF
						" / " ULINTPF "), estimated "
						"cost : %2.4f", buf, i + 1,
						n_indexes, pct_cost);
				}

				error = row_merge_insert_index_tuples(
					sort_idx, old_table,
					merge_files[k].fd, block, NULL,
					&btr_bulk,
					merge_files[k].n_rec, pct_progress, pct_cost,
					crypt_block, new_table->space_id,
					stage);

				error = btr_bulk.finish(error);

				pct_progress += pct_cost;

				if (global_system_variables.log_warnings > 2) {
					sql_print_information(
						"InnoDB: Online DDL : "
						"End of building index %s"
						" (" ULINTPF " / " ULINTPF ")",
						buf, i + 1, n_indexes);
				}
			}
		}

		/* Close the temporary file to free up space. */
		row_merge_file_destroy(&merge_files[k++]);

		if (indexes[i]->type & DICT_FTS) {
			row_fts_psort_info_destroy(psort_info, merge_info);
			fts_psort_initiated = false;
		} else if (old_table != new_table) {
			ut_ad(!sort_idx->online_log);
			ut_ad(sort_idx->online_status
			      == ONLINE_INDEX_COMPLETE);
		}

		if (old_table != new_table
		    || (indexes[i]->type & (DICT_FTS | DICT_SPATIAL))
		    || error != DB_SUCCESS || !online) {
			/* Do not apply any online log. */
		} else {
			if (global_system_variables.log_warnings > 2) {
				sql_print_information(
					"InnoDB: Online DDL : Applying"
					" log to index");
			}

			DEBUG_SYNC_C("row_log_apply_before");
			error = row_log_apply(trx, sort_idx, table, stage);
			DEBUG_SYNC_C("row_log_apply_after");
		}

		if (error != DB_SUCCESS) {
			trx->error_key_num = key_numbers[i];
			goto func_exit;
		}

		if (indexes[i]->type & DICT_FTS
		    && UNIV_UNLIKELY(fts_enable_diag_print)) {
			ib::info() << "Finished building full-text index "
				<< indexes[i]->name;
		}
	}

func_exit:

	DBUG_EXECUTE_IF(
		"ib_build_indexes_too_many_concurrent_trxs",
		error = DB_TOO_MANY_CONCURRENT_TRXS;
		trx->error_state = error;);

	if (fts_psort_initiated) {
		/* Clean up FTS psort related resource */
		row_fts_psort_info_destroy(psort_info, merge_info);
		fts_psort_initiated = false;
	}

	row_merge_file_destroy_low(tmpfd);

	for (i = 0; i < n_merge_files; i++) {
		row_merge_file_destroy(&merge_files[i]);
	}

	if (fts_sort_idx) {
		dict_mem_index_free(fts_sort_idx);
	}

	ut_free(merge_files);

	alloc.deallocate_large(block, &block_pfx);

	if (crypt_block) {
		alloc.deallocate_large(crypt_block, &crypt_pfx);
	}

	DICT_TF2_FLAG_UNSET(new_table, DICT_TF2_FTS_ADD_DOC_ID);

	if (online && old_table == new_table && error != DB_SUCCESS) {
		/* On error, flag all online secondary index creation
		as aborted. */
		for (i = 0; i < n_indexes; i++) {
			ut_ad(!(indexes[i]->type & DICT_FTS));
			ut_ad(!indexes[i]->is_committed());
			ut_ad(!dict_index_is_clust(indexes[i]));

			/* Completed indexes should be dropped as
			well, and indexes whose creation was aborted
			should be dropped from the persistent
			storage. However, at this point we can only
			set some flags in the not-yet-published
			indexes. These indexes will be dropped later
			in row_merge_drop_indexes(), called by
			rollback_inplace_alter_table(). */

			switch (dict_index_get_online_status(indexes[i])) {
			case ONLINE_INDEX_COMPLETE:
				break;
			case ONLINE_INDEX_CREATION:
				indexes[i]->lock.x_lock(SRW_LOCK_CALL);
				row_log_abort_sec(indexes[i]);
				indexes[i]->type |= DICT_CORRUPT;
				indexes[i]->lock.x_unlock();
				new_table->drop_aborted = TRUE;
				/* fall through */
			case ONLINE_INDEX_ABORTED_DROPPED:
			case ONLINE_INDEX_ABORTED:
				MONITOR_ATOMIC_INC(
					MONITOR_BACKGROUND_DROP_INDEX);
			}
		}
	}

	DBUG_EXECUTE_IF("ib_index_crash_after_bulk_load", DBUG_SUICIDE(););
	DBUG_RETURN(error);
}