summaryrefslogtreecommitdiff
path: root/storage/innobase/rem/rem0rec.cc
blob: b3c2fc842313af84107518b12d652b740771a597 (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
/*****************************************************************************

Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2017, 2020, 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 rem/rem0rec.cc
Record manager

Created 5/30/1994 Heikki Tuuri
*************************************************************************/

#include "rem0rec.h"
#include "page0page.h"
#include "mtr0log.h"
#include "fts0fts.h"
#include "trx0sys.h"

/*			PHYSICAL RECORD (OLD STYLE)
			===========================

The physical record, which is the data type of all the records
found in index pages of the database, has the following format
(lower addresses and more significant bits inside a byte are below
represented on a higher text line):

| offset of the end of the last field of data, the most significant
  bit is set to 1 if and only if the field is SQL-null,
  if the offset is 2-byte, then the second most significant
  bit is set to 1 if the field is stored on another page:
  mostly this will occur in the case of big BLOB fields |
...
| offset of the end of the first field of data + the SQL-null bit |
| 4 bits used to delete mark a record, and mark a predefined
  minimum record in alphabetical order |
| 4 bits giving the number of records owned by this record
  (this term is explained in page0page.h) |
| 13 bits giving the order number of this record in the
  heap of the index page |
| 10 bits giving the number of fields in this record |
| 1 bit which is set to 1 if the offsets above are given in
  one byte format, 0 if in two byte format |
| two bytes giving an absolute pointer to the next record in the page |
ORIGIN of the record
| first field of data |
...
| last field of data |

The origin of the record is the start address of the first field
of data. The offsets are given relative to the origin.
The offsets of the data fields are stored in an inverted
order because then the offset of the first fields are near the
origin, giving maybe a better processor cache hit rate in searches.

The offsets of the data fields are given as one-byte
(if there are less than 127 bytes of data in the record)
or two-byte unsigned integers. The most significant bit
is not part of the offset, instead it indicates the SQL-null
if the bit is set to 1. */

/*			PHYSICAL RECORD (NEW STYLE)
			===========================

The physical record, which is the data type of all the records
found in index pages of the database, has the following format
(lower addresses and more significant bits inside a byte are below
represented on a higher text line):

| length of the last non-null variable-length field of data:
  if the maximum length is 255, one byte; otherwise,
  0xxxxxxx (one byte, length=0..127), or 1exxxxxxxxxxxxxx (two bytes,
  length=128..16383, extern storage flag) |
...
| length of first variable-length field of data |
| SQL-null flags (1 bit per nullable field), padded to full bytes |
| 4 bits used to delete mark a record, and mark a predefined
  minimum record in alphabetical order |
| 4 bits giving the number of records owned by this record
  (this term is explained in page0page.h) |
| 13 bits giving the order number of this record in the
  heap of the index page |
| 3 bits record type: 000=conventional, 001=node pointer (inside B-tree),
  010=infimum, 011=supremum, 1xx=reserved |
| two bytes giving a relative pointer to the next record in the page |
ORIGIN of the record
| first field of data |
...
| last field of data |

The origin of the record is the start address of the first field
of data. The offsets are given relative to the origin.
The offsets of the data fields are stored in an inverted
order because then the offset of the first fields are near the
origin, giving maybe a better processor cache hit rate in searches.

The offsets of the data fields are given as one-byte
(if there are less than 127 bytes of data in the record)
or two-byte unsigned integers. The most significant bit
is not part of the offset, instead it indicates the SQL-null
if the bit is set to 1. */

/* CANONICAL COORDINATES. A record can be seen as a single
string of 'characters' in the following way: catenate the bytes
in each field, in the order of fields. An SQL-null field
is taken to be an empty sequence of bytes. Then after
the position of each field insert in the string
the 'character' <FIELD-END>, except that after an SQL-null field
insert <NULL-FIELD-END>. Now the ordinal position of each
byte in this canonical string is its canonical coordinate.
So, for the record ("AA", SQL-NULL, "BB", ""), the canonical
string is "AA<FIELD_END><NULL-FIELD-END>BB<FIELD-END><FIELD-END>".
We identify prefixes (= initial segments) of a record
with prefixes of the canonical string. The canonical
length of the prefix is the length of the corresponding
prefix of the canonical string. The canonical length of
a record is the length of its canonical string.

For example, the maximal common prefix of records
("AA", SQL-NULL, "BB", "C") and ("AA", SQL-NULL, "B", "C")
is "AA<FIELD-END><NULL-FIELD-END>B", and its canonical
length is 5.

A complete-field prefix of a record is a prefix which ends at the
end of some field (containing also <FIELD-END>).
A record is a complete-field prefix of another record, if
the corresponding canonical strings have the same property. */

/***************************************************************//**
Validates the consistency of an old-style physical record.
@return TRUE if ok */
static
ibool
rec_validate_old(
/*=============*/
	const rec_t*	rec);	/*!< in: physical record */

/******************************************************//**
Determine how many of the first n columns in a compact
physical record are stored externally.
@return number of externally stored columns */
ulint
rec_get_n_extern_new(
/*=================*/
	const rec_t*		rec,	/*!< in: compact physical record */
	const dict_index_t*	index,	/*!< in: record descriptor */
	ulint			n)	/*!< in: number of columns to scan */
{
	const byte*	nulls;
	const byte*	lens;
	ulint		null_mask;
	ulint		n_extern;
	ulint		i;

	ut_ad(dict_table_is_comp(index->table));
	ut_ad(!index->table->supports_instant() || index->is_dummy);
	ut_ad(!index->is_instant());
	ut_ad(rec_get_status(rec) == REC_STATUS_ORDINARY
	      || rec_get_status(rec) == REC_STATUS_INSTANT);
	ut_ad(n == ULINT_UNDEFINED || n <= dict_index_get_n_fields(index));

	if (n == ULINT_UNDEFINED) {
		n = dict_index_get_n_fields(index);
	}

	nulls = rec - (REC_N_NEW_EXTRA_BYTES + 1);
	lens = nulls - UT_BITS_IN_BYTES(index->n_nullable);
	null_mask = 1;
	n_extern = 0;
	i = 0;

	/* read the lengths of fields 0..n */
	do {
		const dict_field_t*	field
			= dict_index_get_nth_field(index, i);
		const dict_col_t*	col
			= dict_field_get_col(field);
		ulint			len;

		if (!(col->prtype & DATA_NOT_NULL)) {
			/* nullable field => read the null flag */

			if (UNIV_UNLIKELY(!(byte) null_mask)) {
				nulls--;
				null_mask = 1;
			}

			if (*nulls & null_mask) {
				null_mask <<= 1;
				/* No length is stored for NULL fields. */
				continue;
			}
			null_mask <<= 1;
		}

		if (UNIV_UNLIKELY(!field->fixed_len)) {
			/* Variable-length field: read the length */
			len = *lens--;
			/* If the maximum length of the field is up
			to 255 bytes, the actual length is always
			stored in one byte. If the maximum length is
			more than 255 bytes, the actual length is
			stored in one byte for 0..127.  The length
			will be encoded in two bytes when it is 128 or
			more, or when the field is stored externally. */
			if (DATA_BIG_COL(col)) {
				if (len & 0x80) {
					/* 1exxxxxxx xxxxxxxx */
					if (len & 0x40) {
						n_extern++;
					}
					lens--;
				}
			}
		}
	} while (++i < n);

	return(n_extern);
}

/** Format of a leaf-page ROW_FORMAT!=REDUNDANT record */
enum rec_leaf_format {
	/** Temporary file record */
	REC_LEAF_TEMP,
	/** Temporary file record, with added columns (REC_STATUS_INSTANT) */
	REC_LEAF_TEMP_INSTANT,
	/** Normal (REC_STATUS_ORDINARY) */
	REC_LEAF_ORDINARY,
	/** With add or drop columns (REC_STATUS_INSTANT) */
	REC_LEAF_INSTANT
};

/** Determine the offset to each field in a leaf-page record
in ROW_FORMAT=COMPACT,DYNAMIC,COMPRESSED.
This is a special case of rec_init_offsets() and rec_get_offsets_func().
@tparam	mblob	whether the record includes a metadata BLOB
@param[in]	rec	leaf-page record
@param[in]	index	the index that the record belongs in
@param[in]	n_core	number of core fields (index->n_core_fields)
@param[in]	def_val	default values for non-core fields, or
			NULL to refer to index->fields[].col->def_val
@param[in,out]	offsets	offsets, with valid rec_offs_n_fields(offsets)
@param[in]	format	record format */
template<bool mblob = false>
static inline
void
rec_init_offsets_comp_ordinary(
	const rec_t*		rec,
	const dict_index_t*	index,
	rec_offs*		offsets,
	ulint			n_core,
	const dict_col_t::def_t*def_val,
	rec_leaf_format		format)
{
	rec_offs	offs		= 0;
	rec_offs	any		= 0;
	const byte*	nulls		= rec;
	const byte*	lens		= NULL;
	ulint		n_fields	= n_core;
	ulint		null_mask	= 1;

	ut_ad(index->n_core_fields >= n_core);
	ut_ad(n_core > 0);
	ut_ad(index->n_fields >= n_core);
	ut_ad(index->n_core_null_bytes <= UT_BITS_IN_BYTES(index->n_nullable));
	ut_ad(format == REC_LEAF_TEMP || format == REC_LEAF_TEMP_INSTANT
	      || dict_table_is_comp(index->table));
	ut_ad(format != REC_LEAF_TEMP_INSTANT
	      || index->n_fields == rec_offs_n_fields(offsets));
	ut_d(ulint n_null= 0);

	if (mblob) {
		ut_ad(index->is_dummy || index->table->instant);
		ut_ad(index->is_dummy || index->is_instant());
		ut_ad(rec_offs_n_fields(offsets)
		      <= ulint(index->n_fields) + 1);
		ut_ad(!def_val);
		ut_ad(format == REC_LEAF_INSTANT);
		nulls -= REC_N_NEW_EXTRA_BYTES;
		n_fields = n_core + 1 + rec_get_n_add_field(nulls);
		ut_ad(n_fields <= ulint(index->n_fields) + 1);
		const ulint n_nullable = index->get_n_nullable(n_fields - 1);
		const ulint n_null_bytes = UT_BITS_IN_BYTES(n_nullable);
		ut_d(n_null = n_nullable);
		ut_ad(n_null <= index->n_nullable);
		ut_ad(n_null_bytes >= index->n_core_null_bytes
		      || n_core < index->n_core_fields);
		lens = --nulls - n_null_bytes;
		goto start;
	}

	switch (format) {
	case REC_LEAF_TEMP:
		if (dict_table_is_comp(index->table)) {
			/* No need to do adjust fixed_len=0. We only need to
			adjust it for ROW_FORMAT=REDUNDANT. */
			format = REC_LEAF_ORDINARY;
		}
		goto ordinary;
	case REC_LEAF_ORDINARY:
		nulls -= REC_N_NEW_EXTRA_BYTES;
ordinary:
		lens = --nulls - index->n_core_null_bytes;

		ut_d(n_null = std::min<uint>(index->n_core_null_bytes * 8U,
					     index->n_nullable));
		break;
	case REC_LEAF_INSTANT:
		nulls -= REC_N_NEW_EXTRA_BYTES;
		ut_ad(index->is_instant());
		/* fall through */
	case REC_LEAF_TEMP_INSTANT:
		n_fields = n_core + rec_get_n_add_field(nulls) + 1;
		ut_ad(n_fields <= index->n_fields);
		const ulint n_nullable = index->get_n_nullable(n_fields);
		const ulint n_null_bytes = UT_BITS_IN_BYTES(n_nullable);
		ut_d(n_null = n_nullable);
		ut_ad(n_null <= index->n_nullable);
		ut_ad(n_null_bytes >= index->n_core_null_bytes
		      || n_core < index->n_core_fields);
		lens = --nulls - n_null_bytes;
	}

start:
#ifdef UNIV_DEBUG
	/* We cannot invoke rec_offs_make_valid() if format==REC_LEAF_TEMP.
	Similarly, rec_offs_validate() will fail in that case, because
	it invokes rec_get_status(). */
	memcpy(&offsets[RECORD_OFFSET], &rec, sizeof(rec));
	memcpy(&offsets[INDEX_OFFSET], &index, sizeof(index));
#endif /* UNIV_DEBUG */

	/* read the lengths of fields 0..n_fields */
	rec_offs len;
	ulint i = 0;
	const dict_field_t* field = index->fields;

	do {
		if (mblob) {
			if (i == index->first_user_field()) {
				offs += FIELD_REF_SIZE;
				len = combine(offs, STORED_OFFPAGE);
				any |= REC_OFFS_EXTERNAL;
				field--;
				continue;
			} else if (i >= n_fields) {
				len = combine(offs, DEFAULT);
				any |= REC_OFFS_DEFAULT;
				continue;
			}
		} else if (i < n_fields) {
			/* The field is present, and will be covered below. */
		} else if (!mblob && def_val) {
			const dict_col_t::def_t& d = def_val[i - n_core];
			if (!d.data) {
				len = combine(offs, SQL_NULL);
				ut_ad(d.len == UNIV_SQL_NULL);
			} else {
				len = combine(offs, DEFAULT);
				any |= REC_OFFS_DEFAULT;
			}

			continue;
		} else {
			ulint dlen;
			if (!index->instant_field_value(i, &dlen)) {
				len = combine(offs, SQL_NULL);
				ut_ad(dlen == UNIV_SQL_NULL);
			} else {
				len = combine(offs, DEFAULT);
				any |= REC_OFFS_DEFAULT;
			}

			continue;
		}

		const dict_col_t* col = field->col;

		if (col->is_nullable()) {
			/* nullable field => read the null flag */
			ut_ad(n_null--);

			if (UNIV_UNLIKELY(!(byte) null_mask)) {
				nulls--;
				null_mask = 1;
			}

			if (*nulls & null_mask) {
				null_mask <<= 1;
				/* No length is stored for NULL fields.
				We do not advance offs, and we set
				the length to zero and enable the
				SQL NULL flag in offsets[]. */
				len = combine(offs, SQL_NULL);
				continue;
			}
			null_mask <<= 1;
		}

		if (!field->fixed_len
		    || (format == REC_LEAF_TEMP
			&& !dict_col_get_fixed_size(col, true))) {
			/* Variable-length field: read the length */
			len = *lens--;
			/* If the maximum length of the field is up
			to 255 bytes, the actual length is always
			stored in one byte. If the maximum length is
			more than 255 bytes, the actual length is
			stored in one byte for 0..127.  The length
			will be encoded in two bytes when it is 128 or
			more, or when the field is stored externally. */
			if ((len & 0x80) && DATA_BIG_COL(col)) {
				/* 1exxxxxxx xxxxxxxx */
				len <<= 8;
				len |= *lens--;

				offs += get_value(len);
				if (UNIV_UNLIKELY(len & 0x4000)) {
					ut_ad(dict_index_is_clust(index));
					any |= REC_OFFS_EXTERNAL;
					len = combine(offs, STORED_OFFPAGE);
				} else {
					len = offs;
				}

				continue;
			}

			len = offs += len;
		} else {
			len = offs += field->fixed_len;
		}
	} while (field++, rec_offs_base(offsets)[++i] = len,
		 i < rec_offs_n_fields(offsets));

	*rec_offs_base(offsets)
		= static_cast<rec_offs>(rec - (lens + 1)) | REC_OFFS_COMPACT | any;
}

#ifdef UNIV_DEBUG
/** Update debug data in offsets, in order to tame rec_offs_validate().
@param[in]	rec	record
@param[in]	index	the index that the record belongs in
@param[in]	leaf	whether the record resides in a leaf page
@param[in,out]	offsets	offsets from rec_get_offsets() to adjust */
void
rec_offs_make_valid(
	const rec_t*		rec,
	const dict_index_t*	index,
	bool			leaf,
	rec_offs*		offsets)
{
	const bool is_alter_metadata = leaf
		&& rec_is_alter_metadata(rec, *index);
	ut_ad(is_alter_metadata
	      || rec_offs_n_fields(offsets)
	      <= (leaf
		  ? dict_index_get_n_fields(index)
		  : dict_index_get_n_unique_in_tree_nonleaf(index) + 1)
	      || index->is_dummy || dict_index_is_ibuf(index));
	const bool is_user_rec = (dict_table_is_comp(index->table)
				  ? rec_get_heap_no_new(rec)
				  : rec_get_heap_no_old(rec))
		>= PAGE_HEAP_NO_USER_LOW;
	ulint n = rec_get_n_fields(rec, index);
	/* The infimum and supremum records carry 1 field. */
	ut_ad(is_user_rec || n == 1);
	ut_ad(is_user_rec || rec_offs_n_fields(offsets) == 1);
	ut_ad(!is_user_rec
	      || (n + (index->id == DICT_INDEXES_ID)) >= index->n_core_fields
	      || n >= rec_offs_n_fields(offsets));
	for (; n < rec_offs_n_fields(offsets); n++) {
		ut_ad(leaf);
		ut_ad(is_alter_metadata
		      || get_type(rec_offs_base(offsets)[1 + n]) == DEFAULT);
	}
	memcpy(&offsets[RECORD_OFFSET], &rec, sizeof(rec));
	memcpy(&offsets[INDEX_OFFSET], &index, sizeof(index));
}

/** Validate offsets returned by rec_get_offsets().
@param[in]	rec	record, or NULL
@param[in]	index	the index that the record belongs in, or NULL
@param[in,out]	offsets	the offsets of the record
@return true */
bool
rec_offs_validate(
	const rec_t*		rec,
	const dict_index_t*	index,
	const rec_offs*		offsets)
{
	ulint	i	= rec_offs_n_fields(offsets);
	ulint	last	= ULINT_MAX;
	ulint	comp	= *rec_offs_base(offsets) & REC_OFFS_COMPACT;

	if (rec) {
		ut_ad(!memcmp(&rec, &offsets[RECORD_OFFSET], sizeof(rec)));
		if (!comp) {
			const bool is_user_rec = rec_get_heap_no_old(rec)
				>= PAGE_HEAP_NO_USER_LOW;
			ulint n = rec_get_n_fields_old(rec);
			/* The infimum and supremum records carry 1 field. */
			ut_ad(is_user_rec || n == 1);
			ut_ad(is_user_rec || i == 1);
			ut_ad(!is_user_rec || n >= i || !index
			      || (n + (index->id == DICT_INDEXES_ID))
			      >= index->n_core_fields);
			for (; n < i; n++) {
				ut_ad(get_type(rec_offs_base(offsets)[1 + n])
				      == DEFAULT);
			}
		}
	}
	if (index) {
		ut_ad(!memcmp(&index, &offsets[INDEX_OFFSET], sizeof(index)));
		ulint max_n_fields = std::max(
			dict_index_get_n_fields(index),
			dict_index_get_n_unique_in_tree(index) + 1);
		if (comp && rec) {
			switch (rec_get_status(rec)) {
			case REC_STATUS_INSTANT:
				ut_ad(index->is_instant() || index->is_dummy);
				ut_ad(max_n_fields == index->n_fields);
				max_n_fields += index->table->instant
					|| index->is_dummy;
				break;
			case REC_STATUS_ORDINARY:
				break;
			case REC_STATUS_NODE_PTR:
				max_n_fields = dict_index_get_n_unique_in_tree(
					index) + 1;
				break;
			case REC_STATUS_INFIMUM:
			case REC_STATUS_SUPREMUM:
				max_n_fields = 1;
				break;
			default:
				ut_error;
			}
		} else if (max_n_fields == index->n_fields
			   && (index->is_dummy
			       || (index->is_instant()
				   && index->table->instant))) {
			max_n_fields++;
		}
		/* index->n_def == 0 for dummy indexes if !comp */
		ut_ad(!comp || index->n_def);
		ut_ad(!index->n_def || i <= max_n_fields);
	}
	while (i--) {
		ulint curr = get_value(rec_offs_base(offsets)[1 + i]);
		ut_ad(curr <= last);
		last = curr;
	}
	return(TRUE);
}
#endif /* UNIV_DEBUG */

/** Determine the offsets to each field in the record.
 The offsets are written to a previously allocated array of
ulint, where rec_offs_n_fields(offsets) has been initialized to the
number of fields in the record.	 The rest of the array will be
initialized by this function.  rec_offs_base(offsets)[0] will be set
to the extra size (if REC_OFFS_COMPACT is set, the record is in the
new format; if REC_OFFS_EXTERNAL is set, the record contains externally
stored columns), and rec_offs_base(offsets)[1..n_fields] will be set to
offsets past the end of fields 0..n_fields, or to the beginning of
fields 1..n_fields+1.  When the type of the offset at [i+1]
is (SQL_NULL), the field i is NULL. When the type of the offset at [i+1]
is (STORED_OFFPAGE), the field i is stored externally.
@param[in]	rec	record
@param[in]	index	the index that the record belongs in
@param[in]	leaf	whether the record resides in a leaf page
@param[in,out]	offsets	array of offsets, with valid rec_offs_n_fields() */
static
void
rec_init_offsets(
	const rec_t*		rec,
	const dict_index_t*	index,
	bool			leaf,
	rec_offs*		offsets)
{
	ulint	i	= 0;
	rec_offs	offs;

	/* This assertion was relaxed for the btr_cur_open_at_index_side()
	call in btr_cur_instant_init_low(). We cannot invoke
	index->is_instant(), because the same assertion would fail there
	until btr_cur_instant_init_low() has invoked
	dict_table_t::deserialise_columns(). */
	ut_ad(index->n_core_null_bytes <= UT_BITS_IN_BYTES(index->n_nullable)
	      || index->in_instant_init);
	ut_d(memcpy(&offsets[RECORD_OFFSET], &rec, sizeof(rec)));
	ut_d(memcpy(&offsets[INDEX_OFFSET], &index, sizeof(index)));

	if (dict_table_is_comp(index->table)) {
		const byte*	nulls;
		const byte*	lens;
		dict_field_t*	field;
		ulint		null_mask;
		rec_comp_status_t status = rec_get_status(rec);
		ulint		n_node_ptr_field = ULINT_UNDEFINED;

		switch (UNIV_EXPECT(status, REC_STATUS_ORDINARY)) {
		case REC_STATUS_INFIMUM:
		case REC_STATUS_SUPREMUM:
			/* the field is 8 bytes long */
			rec_offs_base(offsets)[0]
				= REC_N_NEW_EXTRA_BYTES | REC_OFFS_COMPACT;
			rec_offs_base(offsets)[1] = 8;
			return;
		case REC_STATUS_NODE_PTR:
			ut_ad(!leaf);
			n_node_ptr_field
				= dict_index_get_n_unique_in_tree_nonleaf(
					index);
			break;
		case REC_STATUS_INSTANT:
			ut_ad(leaf);
			ut_ad(index->is_instant());
			rec_init_offsets_comp_ordinary(rec, index, offsets,
						       index->n_core_fields,
						       NULL,
						       REC_LEAF_INSTANT);
			return;
		case REC_STATUS_ORDINARY:
			ut_ad(leaf);
			rec_init_offsets_comp_ordinary(rec, index, offsets,
						       index->n_core_fields,
						       NULL,
						       REC_LEAF_ORDINARY);
			return;
		}

		/* The n_nullable flags in the clustered index node pointer
		records in ROW_FORMAT=COMPACT or ROW_FORMAT=DYNAMIC must
		reflect the number of 'core columns'. These flags are
		useless garbage, and they are only reserved because of
		file format compatibility.
		(Clustered index node pointer records only contain the
		PRIMARY KEY columns, which are always NOT NULL,
		so we should have used n_nullable=0.) */
		ut_ad(index->n_core_fields > 0);

		nulls = rec - (REC_N_NEW_EXTRA_BYTES + 1);
		lens = nulls - index->n_core_null_bytes;
		offs = 0;
		null_mask = 1;

		/* read the lengths of fields 0..n */
		do {
			rec_offs len;
			if (UNIV_UNLIKELY(i == n_node_ptr_field)) {
				len = offs += REC_NODE_PTR_SIZE;
				goto resolved;
			}

			field = dict_index_get_nth_field(index, i);
			if (!(dict_field_get_col(field)->prtype
			      & DATA_NOT_NULL)) {
				/* nullable field => read the null flag */

				if (UNIV_UNLIKELY(!(byte) null_mask)) {
					nulls--;
					null_mask = 1;
				}

				if (*nulls & null_mask) {
					null_mask <<= 1;
					/* No length is stored for NULL fields.
					We do not advance offs, and we set
					the length to zero and enable the
					SQL NULL flag in offsets[]. */
					len = combine(offs, SQL_NULL);
					goto resolved;
				}
				null_mask <<= 1;
			}

			if (UNIV_UNLIKELY(!field->fixed_len)) {
				const dict_col_t*	col
					= dict_field_get_col(field);
				/* Variable-length field: read the length */
				len = *lens--;
				/* If the maximum length of the field
				is up to 255 bytes, the actual length
				is always stored in one byte. If the
				maximum length is more than 255 bytes,
				the actual length is stored in one
				byte for 0..127.  The length will be
				encoded in two bytes when it is 128 or
				more, or when the field is stored
				externally. */
				if (DATA_BIG_COL(col)) {
					if (len & 0x80) {
						/* 1exxxxxxx xxxxxxxx */

						len <<= 8;
						len |= *lens--;

						/* B-tree node pointers
						must not contain externally
						stored columns.  Thus
						the "e" flag must be 0. */
						ut_a(!(len & 0x4000));
						offs += get_value(len);
						len = offs;

						goto resolved;
					}
				}

				len = offs += len;
			} else {
				len = offs += field->fixed_len;
			}
resolved:
			rec_offs_base(offsets)[i + 1] = len;
		} while (++i < rec_offs_n_fields(offsets));

		*rec_offs_base(offsets)
			= static_cast<rec_offs>(rec - (lens + 1))
			  | REC_OFFS_COMPACT;
	} else {
		/* Old-style record: determine extra size and end offsets */
		offs = REC_N_OLD_EXTRA_BYTES;
		const ulint n_fields = rec_get_n_fields_old(rec);
		const ulint n = std::min(n_fields, rec_offs_n_fields(offsets));
		rec_offs any;

		if (rec_get_1byte_offs_flag(rec)) {
			offs += static_cast<rec_offs>(n_fields);
			any = offs;
			/* Determine offsets to fields */
			do {
				offs = rec_1_get_field_end_info(rec, i);
				if (offs & REC_1BYTE_SQL_NULL_MASK) {
					offs &= ~REC_1BYTE_SQL_NULL_MASK;
					set_type(offs, SQL_NULL);
				}
				rec_offs_base(offsets)[1 + i] = offs;
			} while (++i < n);
		} else {
			offs += 2 * static_cast<rec_offs>(n_fields);
			any = offs;
			/* Determine offsets to fields */
			do {
				offs = rec_2_get_field_end_info(rec, i);
				if (offs & REC_2BYTE_SQL_NULL_MASK) {
					offs &= ~REC_2BYTE_SQL_NULL_MASK;
					set_type(offs, SQL_NULL);
				}
				if (offs & REC_2BYTE_EXTERN_MASK) {
					offs &= ~REC_2BYTE_EXTERN_MASK;
					set_type(offs, STORED_OFFPAGE);
					any |= REC_OFFS_EXTERNAL;
				}
				rec_offs_base(offsets)[1 + i] = offs;
			} while (++i < n);
		}

		if (i < rec_offs_n_fields(offsets)) {
			ut_ad(index->is_instant()
			      || i + (index->id == DICT_INDEXES_ID)
			      == rec_offs_n_fields(offsets));

			ut_ad(i != 0);
			offs = combine(rec_offs_base(offsets)[i], DEFAULT);

			do {
				rec_offs_base(offsets)[1 + i] = offs;
			} while (++i < rec_offs_n_fields(offsets));

			any |= REC_OFFS_DEFAULT;
		}

		*rec_offs_base(offsets) = any;
	}
}

/** Determine the offsets to each field in an index record.
@param[in]	rec		physical record
@param[in]	index		the index that the record belongs to
@param[in,out]	offsets		array comprising offsets[0] allocated elements,
				or an array from rec_get_offsets(), or NULL
@param[in]	leaf		whether this is a leaf-page record
@param[in]	n_fields	maximum number of offsets to compute
				(ULINT_UNDEFINED to compute all offsets)
@param[in,out]	heap		memory heap
@return the new offsets */
rec_offs*
rec_get_offsets_func(
	const rec_t*		rec,
	const dict_index_t*	index,
	rec_offs*		offsets,
	bool			leaf,
	ulint			n_fields,
#ifdef UNIV_DEBUG
	const char*		file,	/*!< in: file name where called */
	unsigned		line,	/*!< in: line number where called */
#endif /* UNIV_DEBUG */
	mem_heap_t**		heap)	/*!< in/out: memory heap */
{
	ulint	n;
	ulint	size;
	bool	alter_metadata = false;

	if (dict_table_is_comp(index->table)) {
		switch (UNIV_EXPECT(rec_get_status(rec),
				    REC_STATUS_ORDINARY)) {
		case REC_STATUS_INSTANT:
			alter_metadata = rec_is_alter_metadata(rec, true);
			/* fall through */
		case REC_STATUS_ORDINARY:
			ut_ad(leaf);
			n = dict_index_get_n_fields(index) + alter_metadata;
			break;
		case REC_STATUS_NODE_PTR:
			/* Node pointer records consist of the
			uniquely identifying fields of the record
			followed by a child page number field. */
			ut_ad(!leaf);
			n = dict_index_get_n_unique_in_tree_nonleaf(index) + 1;
			break;
		case REC_STATUS_INFIMUM:
		case REC_STATUS_SUPREMUM:
			/* infimum or supremum record */
			ut_ad(rec_get_heap_no_new(rec)
			      == ulint(rec_get_status(rec)
                                       == REC_STATUS_INFIMUM
                                       ? PAGE_HEAP_NO_INFIMUM
                                       : PAGE_HEAP_NO_SUPREMUM));
			n = 1;
			break;
		default:
			ut_error;
			return(NULL);
		}
	} else {
		n = rec_get_n_fields_old(rec);
		/* Here, rec can be allocated from the heap (copied
		from an index page record), or it can be located in an
		index page. If rec is not in an index page, then
		page_rec_is_user_rec(rec) and similar predicates
		cannot be evaluated. We can still distinguish the
		infimum and supremum record based on the heap number. */
		const bool is_user_rec = rec_get_heap_no_old(rec)
			>= PAGE_HEAP_NO_USER_LOW;
		/* The infimum and supremum records carry 1 field. */
		ut_ad(is_user_rec || n == 1);
		ut_ad(!is_user_rec || leaf || index->is_dummy
		      || dict_index_is_ibuf(index)
		      || n == n_fields /* dict_stats_analyze_index_level() */
		      || n
		      == dict_index_get_n_unique_in_tree_nonleaf(index) + 1);
		ut_ad(!is_user_rec || !leaf || index->is_dummy
		      || dict_index_is_ibuf(index)
		      || n == n_fields /* btr_pcur_restore_position() */
		      || (n + (index->id == DICT_INDEXES_ID)
			  >= index->n_core_fields && n <= index->n_fields
			  + unsigned(rec_is_alter_metadata(rec, false))));

		if (is_user_rec && leaf && n < index->n_fields) {
			ut_ad(!index->is_dummy);
			ut_ad(!dict_index_is_ibuf(index));
			n = index->n_fields;
		}
	}

	if (UNIV_UNLIKELY(n_fields < n)) {
		n = n_fields;
	}

	/* The offsets header consists of the allocation size at
	offsets[0] and the REC_OFFS_HEADER_SIZE bytes. */
	size = n + (1 + REC_OFFS_HEADER_SIZE);

	if (UNIV_UNLIKELY(!offsets)
	    || UNIV_UNLIKELY(rec_offs_get_n_alloc(offsets) < size)) {
		if (UNIV_UNLIKELY(!*heap)) {
			*heap = mem_heap_create_at(size * sizeof(*offsets),
						   file, line);
		}
		offsets = static_cast<rec_offs*>(
			mem_heap_alloc(*heap, size * sizeof(*offsets)));

		rec_offs_set_n_alloc(offsets, size);
	}

	rec_offs_set_n_fields(offsets, n);

	if (UNIV_UNLIKELY(alter_metadata) && index->table->not_redundant()) {
#ifdef UNIV_DEBUG
		memcpy(&offsets[RECORD_OFFSET], &rec, sizeof rec);
		memcpy(&offsets[INDEX_OFFSET], &index, sizeof index);
#endif /* UNIV_DEBUG */
		ut_ad(leaf);
		ut_ad(index->is_dummy || index->table->instant);
		ut_ad(index->is_dummy || index->is_instant());
		ut_ad(rec_offs_n_fields(offsets)
		      <= ulint(index->n_fields) + 1);
		rec_init_offsets_comp_ordinary<true>(rec, index, offsets,
						     index->n_core_fields,
						     NULL,
						     REC_LEAF_INSTANT);
	} else {
		rec_init_offsets(rec, index, leaf, offsets);
	}
	return offsets;
}

/******************************************************//**
The following function determines the offsets to each field
in the record.  It can reuse a previously allocated array. */
void
rec_get_offsets_reverse(
/*====================*/
	const byte*		extra,	/*!< in: the extra bytes of a
					compact record in reverse order,
					excluding the fixed-size
					REC_N_NEW_EXTRA_BYTES */
	const dict_index_t*	index,	/*!< in: record descriptor */
	ulint			node_ptr,/*!< in: nonzero=node pointer,
					0=leaf node */
	rec_offs*		offsets)/*!< in/out: array consisting of
					offsets[0] allocated elements */
{
	ulint		n;
	ulint		i;
	rec_offs	offs;
	rec_offs	any_ext = 0;
	const byte*	nulls;
	const byte*	lens;
	dict_field_t*	field;
	ulint		null_mask;
	ulint		n_node_ptr_field;

	ut_ad(dict_table_is_comp(index->table));
	ut_ad(!index->is_instant());

	if (UNIV_UNLIKELY(node_ptr != 0)) {
		n_node_ptr_field =
			dict_index_get_n_unique_in_tree_nonleaf(index);
		n = n_node_ptr_field + 1;
	} else {
		n_node_ptr_field = ULINT_UNDEFINED;
		n = dict_index_get_n_fields(index);
	}

	ut_a(rec_offs_get_n_alloc(offsets) >= n + (1 + REC_OFFS_HEADER_SIZE));
	rec_offs_set_n_fields(offsets, n);

	nulls = extra;
	lens = nulls + UT_BITS_IN_BYTES(index->n_nullable);
	i = offs = 0;
	null_mask = 1;

	/* read the lengths of fields 0..n */
	do {
		rec_offs len;
		if (UNIV_UNLIKELY(i == n_node_ptr_field)) {
			len = offs += REC_NODE_PTR_SIZE;
			goto resolved;
		}

		field = dict_index_get_nth_field(index, i);
		if (!(dict_field_get_col(field)->prtype & DATA_NOT_NULL)) {
			/* nullable field => read the null flag */

			if (UNIV_UNLIKELY(!(byte) null_mask)) {
				nulls++;
				null_mask = 1;
			}

			if (*nulls & null_mask) {
				null_mask <<= 1;
				/* No length is stored for NULL fields.
				We do not advance offs, and we set
				the length to zero and enable the
				SQL NULL flag in offsets[]. */
				len = combine(offs, SQL_NULL);
				goto resolved;
			}
			null_mask <<= 1;
		}

		if (UNIV_UNLIKELY(!field->fixed_len)) {
			/* Variable-length field: read the length */
			const dict_col_t*	col
				= dict_field_get_col(field);
			len = *lens++;
			/* If the maximum length of the field is up
			to 255 bytes, the actual length is always
			stored in one byte. If the maximum length is
			more than 255 bytes, the actual length is
			stored in one byte for 0..127.  The length
			will be encoded in two bytes when it is 128 or
			more, or when the field is stored externally. */
			if (DATA_BIG_COL(col)) {
				if (len & 0x80) {
					/* 1exxxxxxx xxxxxxxx */
					len <<= 8;
					len |= *lens++;

					offs += get_value(len);
					if (UNIV_UNLIKELY(len & 0x4000)) {
						any_ext = REC_OFFS_EXTERNAL;
						len = combine(offs,
							      STORED_OFFPAGE);
					} else {
						len = offs;
					}

					goto resolved;
				}
			}

			len = offs += len;
		} else {
			len = offs += static_cast<rec_offs>(field->fixed_len);
		}
resolved:
		rec_offs_base(offsets)[i + 1] = len;
	} while (++i < rec_offs_n_fields(offsets));

	ut_ad(lens >= extra);
	*rec_offs_base(offsets)
		= static_cast<rec_offs>(lens - extra + REC_N_NEW_EXTRA_BYTES)
		  | REC_OFFS_COMPACT | any_ext;
}

/************************************************************//**
The following function is used to get the offset to the nth
data field in an old-style record.
@return offset to the field */
ulint
rec_get_nth_field_offs_old(
/*=======================*/
	const rec_t*	rec,	/*!< in: record */
	ulint		n,	/*!< in: index of the field */
	ulint*		len)	/*!< out: length of the field;
				UNIV_SQL_NULL if SQL null */
{
	ulint	os;
	ulint	next_os;

	ut_a(n < rec_get_n_fields_old(rec));

	if (rec_get_1byte_offs_flag(rec)) {
		os = rec_1_get_field_start_offs(rec, n);

		next_os = rec_1_get_field_end_info(rec, n);

		if (next_os & REC_1BYTE_SQL_NULL_MASK) {
			*len = UNIV_SQL_NULL;

			return(os);
		}

		next_os = next_os & ~REC_1BYTE_SQL_NULL_MASK;
	} else {
		os = rec_2_get_field_start_offs(rec, n);

		next_os = rec_2_get_field_end_info(rec, n);

		if (next_os & REC_2BYTE_SQL_NULL_MASK) {
			*len = UNIV_SQL_NULL;

			return(os);
		}

		next_os = next_os & ~(REC_2BYTE_SQL_NULL_MASK
				      | REC_2BYTE_EXTERN_MASK);
	}

	*len = next_os - os;

	ut_ad(*len < srv_page_size);

	return(os);
}

/** Determine the size of a data tuple prefix in ROW_FORMAT=COMPACT.
@tparam	mblob	whether the record includes a metadata BLOB
@param[in]	index		record descriptor; dict_table_is_comp()
				is assumed to hold, even if it doesn't
@param[in]	dfield		array of data fields
@param[in]	n_fields	number of data fields
@param[out]	extra		extra size
@param[in]	status		status flags
@param[in]	temp		whether this is a temporary file record
@return total size */
template<bool mblob = false>
static inline
ulint
rec_get_converted_size_comp_prefix_low(
	const dict_index_t*	index,
	const dfield_t*		dfield,
	ulint			n_fields,
	ulint*			extra,
	rec_comp_status_t	status,
	bool			temp)
{
	ulint	extra_size = temp ? 0 : REC_N_NEW_EXTRA_BYTES;
	ut_ad(n_fields > 0);
	ut_ad(n_fields <= dict_index_get_n_fields(index) + mblob);
	ut_d(ulint n_null = index->n_nullable);
	ut_ad(status == REC_STATUS_ORDINARY || status == REC_STATUS_NODE_PTR
	      || status == REC_STATUS_INSTANT);

	if (mblob) {
		ut_ad(!temp);
		ut_ad(index->table->instant);
		ut_ad(index->is_instant());
		ut_ad(status == REC_STATUS_INSTANT);
		ut_ad(n_fields == ulint(index->n_fields) + 1);
		extra_size += UT_BITS_IN_BYTES(index->n_nullable)
			+ rec_get_n_add_field_len(n_fields - 1
						  - index->n_core_fields);
	} else if (status == REC_STATUS_INSTANT
		   && (!temp || n_fields > index->n_core_fields)) {
		ut_ad(index->is_instant());
		ut_ad(UT_BITS_IN_BYTES(n_null) >= index->n_core_null_bytes);
		extra_size += UT_BITS_IN_BYTES(index->get_n_nullable(n_fields))
			+ rec_get_n_add_field_len(n_fields - 1
						  - index->n_core_fields);
	} else {
		ut_ad(n_fields <= index->n_core_fields);
		extra_size += index->n_core_null_bytes;
	}

	ulint data_size = 0;

	if (temp && dict_table_is_comp(index->table)) {
		/* No need to do adjust fixed_len=0. We only need to
		adjust it for ROW_FORMAT=REDUNDANT. */
		temp = false;
	}

	const dfield_t* const end = dfield + n_fields;
	/* read the lengths of fields 0..n */
	for (ulint i = 0; dfield < end; i++, dfield++) {
		if (mblob && i == index->first_user_field()) {
			data_size += FIELD_REF_SIZE;
			if (++dfield == end) {
				ut_ad(i == index->n_fields);
				break;
			}
		}

		ulint len = dfield_get_len(dfield);

		const dict_field_t* field = dict_index_get_nth_field(index, i);
#ifdef UNIV_DEBUG
		if (dict_index_is_spatial(index)) {
			if (DATA_GEOMETRY_MTYPE(field->col->mtype) && i == 0) {
				ut_ad(dfield->type.prtype & DATA_GIS_MBR);
			} else {
				ut_ad(dfield->type.mtype == DATA_SYS_CHILD
				      || dict_col_type_assert_equal(
					      field->col, &dfield->type));
			}
		} else {
			ut_ad(field->col->is_dropped()
			      || dict_col_type_assert_equal(field->col,
							    &dfield->type));
		}
#endif

		/* All NULLable fields must be included in the n_null count. */
		ut_ad(!field->col->is_nullable() || n_null--);

		if (dfield_is_null(dfield)) {
			/* No length is stored for NULL fields. */
			ut_ad(field->col->is_nullable());
			continue;
		}

		ut_ad(len <= field->col->len
		      || DATA_LARGE_MTYPE(field->col->mtype)
		      || (field->col->len == 0
			  && field->col->mtype == DATA_VARCHAR));

		ulint fixed_len = field->fixed_len;
		if (temp && fixed_len
		    && !dict_col_get_fixed_size(field->col, temp)) {
			fixed_len = 0;
		}
		/* If the maximum length of a variable-length field
		is up to 255 bytes, the actual length is always stored
		in one byte. If the maximum length is more than 255
		bytes, the actual length is stored in one byte for
		0..127.  The length will be encoded in two bytes when
		it is 128 or more, or when the field is stored externally. */

		if (fixed_len) {
#ifdef UNIV_DEBUG
			ut_ad(len <= fixed_len);

			if (dict_index_is_spatial(index)) {
				ut_ad(dfield->type.mtype == DATA_SYS_CHILD
				      || !field->col->mbmaxlen
				      || len >= field->col->mbminlen
				      * fixed_len / field->col->mbmaxlen);
			} else {
				ut_ad(dfield->type.mtype != DATA_SYS_CHILD);

				ut_ad(field->col->is_dropped()
				      || !field->col->mbmaxlen
				      || len >= field->col->mbminlen
				      * fixed_len / field->col->mbmaxlen);
			}

			/* dict_index_add_col() should guarantee this */
			ut_ad(!field->prefix_len
			      || fixed_len == field->prefix_len);
#endif /* UNIV_DEBUG */
		} else if (dfield_is_ext(dfield)) {
			ut_ad(DATA_BIG_COL(field->col));
			extra_size += 2;
		} else if (len < 128 || !DATA_BIG_COL(field->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 (extra) {
		*extra = extra_size;
	}

	return(extra_size + data_size);
}

/**********************************************************//**
Determines the size of a data tuple prefix in ROW_FORMAT=COMPACT.
@return total size */
ulint
rec_get_converted_size_comp_prefix(
/*===============================*/
	const dict_index_t*	index,	/*!< in: record descriptor */
	const dfield_t*		fields,	/*!< in: array of data fields */
	ulint			n_fields,/*!< in: number of data fields */
	ulint*			extra)	/*!< out: extra size */
{
	ut_ad(dict_table_is_comp(index->table));
	return(rec_get_converted_size_comp_prefix_low(
		       index, fields, n_fields, extra,
		       REC_STATUS_ORDINARY, false));
}

/** Determine the size of a record in ROW_FORMAT=COMPACT.
@param[in]	index		record descriptor. dict_table_is_comp()
				is assumed to hold, even if it doesn't
@param[in]	tuple		logical record
@param[out]	extra		extra size
@return total size */
ulint
rec_get_converted_size_comp(
	const dict_index_t*	index,
	const dtuple_t*		tuple,
	ulint*			extra)
{
	ut_ad(tuple->n_fields > 0);

	rec_comp_status_t status = rec_comp_status_t(tuple->info_bits
						     & REC_NEW_STATUS_MASK);

	switch (UNIV_EXPECT(status, REC_STATUS_ORDINARY)) {
	case REC_STATUS_ORDINARY:
		ut_ad(!tuple->is_metadata());
		if (tuple->n_fields > index->n_core_fields) {
			ut_ad(index->is_instant());
			status = REC_STATUS_INSTANT;
		}
		/* fall through */
	case REC_STATUS_INSTANT:
		ut_ad(tuple->n_fields >= index->n_core_fields);
		if (tuple->is_alter_metadata()) {
			return rec_get_converted_size_comp_prefix_low<true>(
				index, tuple->fields, tuple->n_fields,
				extra, status, false);
		}
		ut_ad(tuple->n_fields <= index->n_fields);
		return rec_get_converted_size_comp_prefix_low(
			index, tuple->fields, tuple->n_fields,
			extra, status, false);
	case REC_STATUS_NODE_PTR:
		ut_ad(tuple->n_fields - 1
		      == dict_index_get_n_unique_in_tree_nonleaf(index));
		ut_ad(dfield_get_len(&tuple->fields[tuple->n_fields - 1])
		      == REC_NODE_PTR_SIZE);
		return REC_NODE_PTR_SIZE /* child page number */
			+ rec_get_converted_size_comp_prefix_low(
				index, tuple->fields, tuple->n_fields - 1,
				extra, status, false);
	case REC_STATUS_INFIMUM:
	case REC_STATUS_SUPREMUM:
		/* not supported */
		break;
	}

	ut_error;
	return(ULINT_UNDEFINED);
}

/***********************************************************//**
Sets the value of the ith field SQL null bit of an old-style record. */
void
rec_set_nth_field_null_bit(
/*=======================*/
	rec_t*	rec,	/*!< in: record */
	ulint	i,	/*!< in: ith field */
	ibool	val)	/*!< in: value to set */
{
	ulint	info;

	if (rec_get_1byte_offs_flag(rec)) {

		info = rec_1_get_field_end_info(rec, i);

		if (val) {
			info = info | REC_1BYTE_SQL_NULL_MASK;
		} else {
			info = info & ~REC_1BYTE_SQL_NULL_MASK;
		}

		rec_1_set_field_end_info(rec, i, info);

		return;
	}

	info = rec_2_get_field_end_info(rec, i);

	if (val) {
		info = info | REC_2BYTE_SQL_NULL_MASK;
	} else {
		info = info & ~REC_2BYTE_SQL_NULL_MASK;
	}

	rec_2_set_field_end_info(rec, i, info);
}

/***********************************************************//**
Sets an old-style record field to SQL null.
The physical size of the field is not changed. */
void
rec_set_nth_field_sql_null(
/*=======================*/
	rec_t*	rec,	/*!< in: record */
	ulint	n)	/*!< in: index of the field */
{
	ulint	offset;

	offset = rec_get_field_start_offs(rec, n);

	data_write_sql_null(rec + offset, rec_get_nth_field_size(rec, n));

	rec_set_nth_field_null_bit(rec, n, TRUE);
}

/*********************************************************//**
Builds an old-style physical record out of a data tuple and
stores it beginning from the start of the given buffer.
@return pointer to the origin of physical record */
static
rec_t*
rec_convert_dtuple_to_rec_old(
/*==========================*/
	byte*		buf,	/*!< in: start address of the physical record */
	const dtuple_t*	dtuple,	/*!< in: data tuple */
	ulint		n_ext)	/*!< in: number of externally stored columns */
{
	const dfield_t*	field;
	ulint		n_fields;
	ulint		data_size;
	rec_t*		rec;
	ulint		end_offset;
	ulint		ored_offset;
	ulint		len;
	ulint		i;

	ut_ad(buf && dtuple);
	ut_ad(dtuple_validate(dtuple));
	ut_ad(dtuple_check_typed(dtuple));

	n_fields = dtuple_get_n_fields(dtuple);
	data_size = dtuple_get_data_size(dtuple, 0);

	ut_ad(n_fields > 0);

	/* Calculate the offset of the origin in the physical record */

	rec = buf + rec_get_converted_extra_size(data_size, n_fields, n_ext);
	/* Store the number of fields */
	rec_set_n_fields_old(rec, n_fields);

	/* Set the info bits of the record */
	rec_set_info_bits_old(rec, dtuple_get_info_bits(dtuple)
			      & REC_INFO_BITS_MASK);
	rec_set_heap_no_old(rec, PAGE_HEAP_NO_USER_LOW);

	/* Store the data and the offsets */

	end_offset = 0;

	if (!n_ext && data_size <= REC_1BYTE_OFFS_LIMIT) {

		rec_set_1byte_offs_flag(rec, TRUE);

		for (i = 0; i < n_fields; i++) {

			field = dtuple_get_nth_field(dtuple, i);

			if (dfield_is_null(field)) {
				len = dtype_get_sql_null_size(
					dfield_get_type(field), 0);
				data_write_sql_null(rec + end_offset, len);

				end_offset += len;
				ored_offset = end_offset
					| REC_1BYTE_SQL_NULL_MASK;
			} else {
				/* If the data is not SQL null, store it */
				len = dfield_get_len(field);

				memcpy(rec + end_offset,
				       dfield_get_data(field), len);

				end_offset += len;
				ored_offset = end_offset;
			}

			rec_1_set_field_end_info(rec, i, ored_offset);
		}
	} else {
		rec_set_1byte_offs_flag(rec, FALSE);

		for (i = 0; i < n_fields; i++) {

			field = dtuple_get_nth_field(dtuple, i);

			if (dfield_is_null(field)) {
				len = dtype_get_sql_null_size(
					dfield_get_type(field), 0);
				data_write_sql_null(rec + end_offset, len);

				end_offset += len;
				ored_offset = end_offset
					| REC_2BYTE_SQL_NULL_MASK;
			} else {
				/* If the data is not SQL null, store it */
				len = dfield_get_len(field);

				memcpy(rec + end_offset,
				       dfield_get_data(field), len);

				end_offset += len;
				ored_offset = end_offset;

				if (dfield_is_ext(field)) {
					ored_offset |= REC_2BYTE_EXTERN_MASK;
				}
			}

			rec_2_set_field_end_info(rec, i, ored_offset);
		}
	}

	return(rec);
}

/** Convert a data tuple into a ROW_FORMAT=COMPACT record.
@tparam	mblob	whether the record includes a metadata BLOB
@param[out]	rec		converted record
@param[in]	index		index
@param[in]	field		data fields to convert
@param[in]	n_fields	number of data fields
@param[in]	status		rec_get_status(rec)
@param[in]	temp		whether to use the format for temporary files
				in index creation */
template<bool mblob = false>
static inline
void
rec_convert_dtuple_to_rec_comp(
	rec_t*			rec,
	const dict_index_t*	index,
	const dfield_t*		field,
	ulint			n_fields,
	rec_comp_status_t	status,
	bool			temp)
{
	byte*		end;
	byte*		nulls = temp
		? rec - 1 : rec - (REC_N_NEW_EXTRA_BYTES + 1);
	byte*		UNINIT_VAR(lens);
	ulint		UNINIT_VAR(n_node_ptr_field);
	ulint		null_mask	= 1;

	ut_ad(n_fields > 0);
	ut_ad(temp || dict_table_is_comp(index->table));
	ut_ad(index->n_core_null_bytes <= UT_BITS_IN_BYTES(index->n_nullable));

	ut_d(ulint n_null = index->n_nullable);

	if (mblob) {
		ut_ad(!temp);
		ut_ad(index->table->instant);
		ut_ad(index->is_instant());
		ut_ad(status == REC_STATUS_INSTANT);
		ut_ad(n_fields == ulint(index->n_fields) + 1);
		rec_set_n_add_field(nulls, n_fields - 1
				    - index->n_core_fields);
		rec_set_heap_no_new(rec, PAGE_HEAP_NO_USER_LOW);
		rec_set_status(rec, REC_STATUS_INSTANT);
		n_node_ptr_field = ULINT_UNDEFINED;
		lens = nulls - UT_BITS_IN_BYTES(index->n_nullable);
		goto start;
	}
	switch (status) {
	case REC_STATUS_INSTANT:
		ut_ad(index->is_instant());
		ut_ad(n_fields > index->n_core_fields);
		rec_set_n_add_field(nulls, n_fields - 1
				    - index->n_core_fields);
		/* fall through */
	case REC_STATUS_ORDINARY:
		ut_ad(n_fields <= dict_index_get_n_fields(index));
		if (!temp) {
			rec_set_heap_no_new(rec, PAGE_HEAP_NO_USER_LOW);

			rec_set_status(
				rec, n_fields == index->n_core_fields
				     ? REC_STATUS_ORDINARY
				     : REC_STATUS_INSTANT);
		}

		if (dict_table_is_comp(index->table)) {
			/* No need to do adjust fixed_len=0. We only
			need to adjust it for ROW_FORMAT=REDUNDANT. */
			temp = false;
		}

		n_node_ptr_field = ULINT_UNDEFINED;

		lens = nulls - (index->is_instant()
				? UT_BITS_IN_BYTES(index->get_n_nullable(
					n_fields))
				: UT_BITS_IN_BYTES(
					unsigned(index->n_nullable)));
		break;
	case REC_STATUS_NODE_PTR:
		ut_ad(!temp);
		rec_set_heap_no_new(rec, PAGE_HEAP_NO_USER_LOW);
		rec_set_status(rec, status);
		ut_ad(n_fields
		      == dict_index_get_n_unique_in_tree_nonleaf(index) + 1);
		ut_d(n_null = std::min<uint>(index->n_core_null_bytes * 8U,
					     index->n_nullable));
		n_node_ptr_field = n_fields - 1;
		lens = nulls - index->n_core_null_bytes;
		break;
	case REC_STATUS_INFIMUM:
	case REC_STATUS_SUPREMUM:
		ut_error;
		return;
	}

start:
	end = rec;
	/* clear the SQL-null flags */
	memset(lens + 1, 0, ulint(nulls - lens));

	const dfield_t* const fend = field + n_fields;
	/* Store the data and the offsets */
	for (ulint i = 0; field < fend; i++, field++) {
		ulint len = dfield_get_len(field);

		if (mblob) {
			if (i == index->first_user_field()) {
				ut_ad(len == FIELD_REF_SIZE);
				ut_ad(dfield_is_ext(field));
				memcpy(end, dfield_get_data(field), len);
				end += len;
				if (++field == fend) {
					ut_ad(i == index->n_fields);
					break;
				}
				len = dfield_get_len(field);
			}
		} else if (UNIV_UNLIKELY(i == n_node_ptr_field)) {
			ut_ad(field->type.prtype & DATA_NOT_NULL);
			ut_ad(len == REC_NODE_PTR_SIZE);
			memcpy(end, dfield_get_data(field), len);
			end += REC_NODE_PTR_SIZE;
			break;
		}

		if (!(field->type.prtype & DATA_NOT_NULL)) {
			/* nullable field */
			ut_ad(n_null--);

			if (UNIV_UNLIKELY(!(byte) null_mask)) {
				nulls--;
				null_mask = 1;
			}

			ut_ad(*nulls < null_mask);

			/* set the null flag if necessary */
			if (dfield_is_null(field)) {
				*nulls |= null_mask;
				null_mask <<= 1;
				continue;
			}

			null_mask <<= 1;
		}
		/* only nullable fields can be null */
		ut_ad(!dfield_is_null(field));

		const dict_field_t* ifield
			= dict_index_get_nth_field(index, i);
		ulint fixed_len = ifield->fixed_len;

		if (temp && fixed_len
		    && !dict_col_get_fixed_size(ifield->col, temp)) {
			fixed_len = 0;
		}

		/* If the maximum length of a variable-length field
		is up to 255 bytes, the actual length is always stored
		in one byte. If the maximum length is more than 255
		bytes, the actual length is stored in one byte for
		0..127.  The length will be encoded in two bytes when
		it is 128 or more, or when the field is stored externally. */
		if (fixed_len) {
			ut_ad(len <= fixed_len);
			ut_ad(!ifield->col->mbmaxlen
			      || len >= ifield->col->mbminlen
			      * fixed_len / ifield->col->mbmaxlen);
			ut_ad(!dfield_is_ext(field));
		} else if (dfield_is_ext(field)) {
			ut_ad(DATA_BIG_COL(ifield->col));
			ut_ad(len <= REC_ANTELOPE_MAX_INDEX_COL_LEN
					+ BTR_EXTERN_FIELD_REF_SIZE);
			*lens-- = (byte) (len >> 8) | 0xc0;
			*lens-- = (byte) len;
		} else {
			ut_ad(len <= field->type.len
			      || DATA_LARGE_MTYPE(field->type.mtype)
			      || !strcmp(index->name,
					 FTS_INDEX_TABLE_IND_NAME));
			if (len < 128 || !DATA_BIG_LEN_MTYPE(
				field->type.len, field->type.mtype)) {

				*lens-- = (byte) len;
			} else {
				ut_ad(len < 16384);
				*lens-- = (byte) (len >> 8) | 0x80;
				*lens-- = (byte) len;
			}
		}

		if (len) {
			memcpy(end, dfield_get_data(field), len);
			end += len;
		}
	}
}

/*********************************************************//**
Builds a new-style physical record out of a data tuple and
stores it beginning from the start of the given buffer.
@return pointer to the origin of physical record */
static
rec_t*
rec_convert_dtuple_to_rec_new(
/*==========================*/
	byte*			buf,	/*!< in: start address of
					the physical record */
	const dict_index_t*	index,	/*!< in: record descriptor */
	const dtuple_t*		dtuple)	/*!< in: data tuple */
{
	ut_ad(!(dtuple->info_bits
		& ~(REC_NEW_STATUS_MASK | REC_INFO_DELETED_FLAG
		    | REC_INFO_MIN_REC_FLAG)));

	ulint	extra_size;

	if (UNIV_UNLIKELY(dtuple->is_alter_metadata())) {
		ut_ad((dtuple->info_bits & REC_NEW_STATUS_MASK)
		      == REC_STATUS_INSTANT);
		rec_get_converted_size_comp_prefix_low<true>(
			index, dtuple->fields, dtuple->n_fields,
			&extra_size, REC_STATUS_INSTANT, false);
		buf += extra_size;
		rec_convert_dtuple_to_rec_comp<true>(
			buf, index, dtuple->fields, dtuple->n_fields,
			REC_STATUS_INSTANT, false);
	} else {
		rec_get_converted_size_comp(index, dtuple, &extra_size);
		buf += extra_size;
		rec_comp_status_t status = rec_comp_status_t(
			dtuple->info_bits & REC_NEW_STATUS_MASK);
		if (status == REC_STATUS_ORDINARY
		    && dtuple->n_fields > index->n_core_fields) {
			ut_ad(index->is_instant());
			status = REC_STATUS_INSTANT;
		}

		rec_convert_dtuple_to_rec_comp(
			buf, index, dtuple->fields, dtuple->n_fields,
			status, false);
	}

	rec_set_info_bits_new(buf, dtuple->info_bits & ~REC_NEW_STATUS_MASK);
	return buf;
}

/*********************************************************//**
Builds a physical record out of a data tuple and
stores it beginning from the start of the given buffer.
@return pointer to the origin of physical record */
rec_t*
rec_convert_dtuple_to_rec(
/*======================*/
	byte*			buf,	/*!< in: start address of the
					physical record */
	const dict_index_t*	index,	/*!< in: record descriptor */
	const dtuple_t*		dtuple,	/*!< in: data tuple */
	ulint			n_ext)	/*!< in: number of
					externally stored columns */
{
	rec_t*	rec;

	ut_ad(buf != NULL);
	ut_ad(index != NULL);
	ut_ad(dtuple != NULL);
	ut_ad(dtuple_validate(dtuple));
	ut_ad(dtuple_check_typed(dtuple));

	if (dict_table_is_comp(index->table)) {
		rec = rec_convert_dtuple_to_rec_new(buf, index, dtuple);
	} else {
		rec = rec_convert_dtuple_to_rec_old(buf, dtuple, n_ext);
	}

	return(rec);
}

/** Determine the size of a data tuple prefix in a temporary file.
@param[in]	index		clustered or secondary index
@param[in]	fields		data fields
@param[in]	n_fields	number of data fields
@param[out]	extra		record header size
@param[in]	status		REC_STATUS_ORDINARY or REC_STATUS_INSTANT
@return	total size, in bytes */
ulint
rec_get_converted_size_temp(
	const dict_index_t*	index,
	const dfield_t*		fields,
	ulint			n_fields,
	ulint*			extra,
	rec_comp_status_t	status)
{
	return rec_get_converted_size_comp_prefix_low(
		index, fields, n_fields, extra, status, true);
}

/** Determine the offset to each field in temporary file.
@param[in]	rec	temporary file record
@param[in]	index	index of that the record belongs to
@param[in,out]	offsets	offsets to the fields; in: rec_offs_n_fields(offsets)
@param[in]	n_core	number of core fields (index->n_core_fields)
@param[in]	def_val	default values for non-core fields
@param[in]	status	REC_STATUS_ORDINARY or REC_STATUS_INSTANT */
void
rec_init_offsets_temp(
	const rec_t*		rec,
	const dict_index_t*	index,
	rec_offs*		offsets,
	ulint			n_core,
	const dict_col_t::def_t*def_val,
	rec_comp_status_t	status)
{
	ut_ad(status == REC_STATUS_ORDINARY
	      || status == REC_STATUS_INSTANT);
	/* The table may have been converted to plain format
	if it was emptied during an ALTER TABLE operation. */
	ut_ad(index->n_core_fields == n_core || !index->is_instant());
	ut_ad(index->n_core_fields >= n_core);
	rec_init_offsets_comp_ordinary(rec, index, offsets, n_core, def_val,
				       status == REC_STATUS_INSTANT
				       ? REC_LEAF_TEMP_INSTANT
				       : REC_LEAF_TEMP);
}

/** Determine the offset to each field in temporary file.
@param[in]	rec	temporary file record
@param[in]	index	index of that the record belongs to
@param[in,out]	offsets	offsets to the fields; in: rec_offs_n_fields(offsets)
*/
void
rec_init_offsets_temp(
	const rec_t*		rec,
	const dict_index_t*	index,
	rec_offs*		offsets)
{
	ut_ad(!index->is_instant());
	rec_init_offsets_comp_ordinary(rec, index, offsets,
				       index->n_core_fields, NULL,
				       REC_LEAF_TEMP);
}

/** Convert a data tuple prefix to the temporary file format.
@param[out]	rec		record in temporary file format
@param[in]	index		clustered or secondary index
@param[in]	fields		data fields
@param[in]	n_fields	number of data fields
@param[in]	status		REC_STATUS_ORDINARY or REC_STATUS_INSTANT
*/
void
rec_convert_dtuple_to_temp(
	rec_t*			rec,
	const dict_index_t*	index,
	const dfield_t*		fields,
	ulint			n_fields,
	rec_comp_status_t	status)
{
	rec_convert_dtuple_to_rec_comp(rec, index, fields, n_fields,
				       status, true);
}

/** Copy the first n fields of a (copy of a) physical record to a data tuple.
The fields are copied into the memory heap.
@param[out]	tuple		data tuple
@param[in]	rec		index record, or a copy thereof
@param[in]	is_leaf		whether rec is a leaf page record
@param[in]	n_fields	number of fields to copy
@param[in,out]	heap		memory heap */
void
rec_copy_prefix_to_dtuple(
	dtuple_t*		tuple,
	const rec_t*		rec,
	const dict_index_t*	index,
	bool			is_leaf,
	ulint			n_fields,
	mem_heap_t*		heap)
{
	rec_offs	offsets_[REC_OFFS_NORMAL_SIZE];
	rec_offs*	offsets	= offsets_;
	rec_offs_init(offsets_);

	ut_ad(is_leaf || n_fields
	      <= dict_index_get_n_unique_in_tree_nonleaf(index) + 1);

	offsets = rec_get_offsets(rec, index, offsets, is_leaf,
				  n_fields, &heap);

	ut_ad(rec_validate(rec, offsets));
	ut_ad(!rec_offs_any_default(offsets));
	ut_ad(dtuple_check_typed(tuple));

	tuple->info_bits = rec_get_info_bits(rec, rec_offs_comp(offsets));

	for (ulint i = 0; i < n_fields; i++) {
		dfield_t*	field;
		const byte*	data;
		ulint		len;

		field = dtuple_get_nth_field(tuple, i);
		data = rec_get_nth_field(rec, offsets, i, &len);

		if (len != UNIV_SQL_NULL) {
			dfield_set_data(field,
					mem_heap_dup(heap, data, len), len);
			ut_ad(!rec_offs_nth_extern(offsets, i));
		} else {
			dfield_set_null(field);
		}
	}
}

/**************************************************************//**
Copies the first n fields of an old-style physical record
to a new physical record in a buffer.
@return own: copied record */
static
rec_t*
rec_copy_prefix_to_buf_old(
/*=======================*/
	const rec_t*	rec,		/*!< in: physical record */
	ulint		n_fields,	/*!< in: number of fields to copy */
	ulint		area_end,	/*!< in: end of the prefix data */
	byte**		buf,		/*!< in/out: memory buffer for
					the copied prefix, or NULL */
	ulint*		buf_size)	/*!< in/out: buffer size */
{
	rec_t*	copy_rec;
	ulint	area_start;
	ulint	prefix_len;

	if (rec_get_1byte_offs_flag(rec)) {
		area_start = REC_N_OLD_EXTRA_BYTES + n_fields;
	} else {
		area_start = REC_N_OLD_EXTRA_BYTES + 2 * n_fields;
	}

	prefix_len = area_start + area_end;

	if ((*buf == NULL) || (*buf_size < prefix_len)) {
		ut_free(*buf);
		*buf_size = prefix_len;
		*buf = static_cast<byte*>(ut_malloc_nokey(prefix_len));
	}

	ut_memcpy(*buf, rec - area_start, prefix_len);

	copy_rec = *buf + area_start;

	rec_set_n_fields_old(copy_rec, n_fields);

	return(copy_rec);
}

/**************************************************************//**
Copies the first n fields of a physical record to a new physical record in
a buffer.
@return own: copied record */
rec_t*
rec_copy_prefix_to_buf(
/*===================*/
	const rec_t*		rec,		/*!< in: physical record */
	const dict_index_t*	index,		/*!< in: record descriptor */
	ulint			n_fields,	/*!< in: number of fields
						to copy */
	byte**			buf,		/*!< in/out: memory buffer
						for the copied prefix,
						or NULL */
	ulint*			buf_size)	/*!< in/out: buffer size */
{
	ut_ad(n_fields <= index->n_fields || dict_index_is_ibuf(index));
	ut_ad(index->n_core_null_bytes <= UT_BITS_IN_BYTES(index->n_nullable));
	UNIV_PREFETCH_RW(*buf);

	if (!dict_table_is_comp(index->table)) {
		ut_ad(rec_validate_old(rec));
		return(rec_copy_prefix_to_buf_old(
			       rec, n_fields,
			       rec_get_field_start_offs(rec, n_fields),
			       buf, buf_size));
	}

	ulint		prefix_len	= 0;
	ulint		instant_omit	= 0;
	const byte*	nulls		= rec - (REC_N_NEW_EXTRA_BYTES + 1);
	const byte*	nullf		= nulls;
	const byte*	lens		= nulls - index->n_core_null_bytes;

	switch (rec_get_status(rec)) {
	default:
		/* infimum or supremum record: no sense to copy anything */
		ut_error;
		return(NULL);
	case REC_STATUS_ORDINARY:
		ut_ad(n_fields <= index->n_core_fields);
		break;
	case REC_STATUS_NODE_PTR:
		/* For R-tree, we need to copy the child page number field. */
		compile_time_assert(DICT_INDEX_SPATIAL_NODEPTR_SIZE == 1);
		if (dict_index_is_spatial(index)) {
			ut_ad(index->n_core_null_bytes == 0);
			ut_ad(n_fields == DICT_INDEX_SPATIAL_NODEPTR_SIZE + 1);
			ut_ad(index->fields[0].col->prtype & DATA_NOT_NULL);
			ut_ad(DATA_BIG_COL(index->fields[0].col));
			/* This is a deficiency of the format introduced
			in MySQL 5.7. The length in the R-tree index should
			always be DATA_MBR_LEN. */
			ut_ad(!index->fields[0].fixed_len);
			ut_ad(*lens == DATA_MBR_LEN);
			lens--;
			prefix_len = DATA_MBR_LEN + REC_NODE_PTR_SIZE;
			n_fields = 0; /* skip the "for" loop below */
			break;
		}
		/* it doesn't make sense to copy the child page number field */
		ut_ad(n_fields
		      <= dict_index_get_n_unique_in_tree_nonleaf(index));
		break;
	case REC_STATUS_INSTANT:
		/* We would have !index->is_instant() when rolling back
		an instant ADD COLUMN operation. */
		ut_ad(index->is_instant() || page_rec_is_metadata(rec));
		ut_ad(n_fields <= index->first_user_field());
		nulls++;
		const ulint n_rec = ulint(index->n_core_fields) + 1
			+ rec_get_n_add_field(nulls)
			- rec_is_alter_metadata(rec, true);
		instant_omit = ulint(&rec[-REC_N_NEW_EXTRA_BYTES] - nulls);
		ut_ad(instant_omit == 1 || instant_omit == 2);
		nullf = nulls;
		const uint nb = UT_BITS_IN_BYTES(index->get_n_nullable(n_rec));
		instant_omit += nb - index->n_core_null_bytes;
		lens = --nulls - nb;
	}

	const byte* const lenf = lens;
	UNIV_PREFETCH_R(lens);

	/* read the lengths of fields 0..n */
	for (ulint i = 0, null_mask = 1; i < n_fields; i++) {
		const dict_field_t*	field;
		const dict_col_t*	col;

		field = dict_index_get_nth_field(index, i);
		col = dict_field_get_col(field);

		if (!(col->prtype & DATA_NOT_NULL)) {
			/* nullable field => read the null flag */
			if (UNIV_UNLIKELY(!(byte) null_mask)) {
				nulls--;
				null_mask = 1;
			}

			if (*nulls & null_mask) {
				null_mask <<= 1;
				continue;
			}

			null_mask <<= 1;
		}

		if (field->fixed_len) {
			prefix_len += field->fixed_len;
		} else {
			ulint	len = *lens--;
			/* If the maximum length of the column is up
			to 255 bytes, the actual length is always
			stored in one byte. If the maximum length is
			more than 255 bytes, the actual length is
			stored in one byte for 0..127.  The length
			will be encoded in two bytes when it is 128 or
			more, or when the column is stored externally. */
			if (DATA_BIG_COL(col)) {
				if (len & 0x80) {
					/* 1exxxxxx */
					len &= 0x3f;
					len <<= 8;
					len |= *lens--;
					UNIV_PREFETCH_R(lens);
				}
			}
			prefix_len += len;
		}
	}

	UNIV_PREFETCH_R(rec + prefix_len);

	ulint size = prefix_len + ulint(rec - (lens + 1)) - instant_omit;

	if (*buf == NULL || *buf_size < size) {
		ut_free(*buf);
		*buf_size = size;
		*buf = static_cast<byte*>(ut_malloc_nokey(size));
	}

	if (instant_omit) {
		/* Copy and convert the record header to a format where
		instant ADD COLUMN has not been used:
		+ lengths of variable-length fields in the prefix
		- omit any null flag bytes for any instantly added columns
		+ index->n_core_null_bytes of null flags
		- omit the n_add_fields header (1 or 2 bytes)
		+ REC_N_NEW_EXTRA_BYTES of fixed header */
		byte* b = *buf;
		/* copy the lengths of the variable-length fields */
		memcpy(b, lens + 1, ulint(lenf - lens));
		b += ulint(lenf - lens);
		/* copy the null flags */
		memcpy(b, nullf - index->n_core_null_bytes,
		       index->n_core_null_bytes);
		b += index->n_core_null_bytes + REC_N_NEW_EXTRA_BYTES;
		ut_ad(ulint(b - *buf) + prefix_len == size);
		/* copy the fixed-size header and the record prefix */
		memcpy(b - REC_N_NEW_EXTRA_BYTES, rec - REC_N_NEW_EXTRA_BYTES,
		       prefix_len + REC_N_NEW_EXTRA_BYTES);
		ut_ad(rec_get_status(b) == REC_STATUS_INSTANT);
		rec_set_status(b, REC_STATUS_ORDINARY);
		return b;
	} else {
		memcpy(*buf, lens + 1, size);
		return *buf + (rec - (lens + 1));
	}
}

/***************************************************************//**
Validates the consistency of an old-style physical record.
@return TRUE if ok */
static
ibool
rec_validate_old(
/*=============*/
	const rec_t*	rec)	/*!< in: physical record */
{
	ulint		len;
	ulint		n_fields;
	ulint		len_sum		= 0;
	ulint		i;

	ut_a(rec);
	n_fields = rec_get_n_fields_old(rec);

	if ((n_fields == 0) || (n_fields > REC_MAX_N_FIELDS)) {
		ib::error() << "Record has " << n_fields << " fields";
		return(FALSE);
	}

	for (i = 0; i < n_fields; i++) {
		rec_get_nth_field_offs_old(rec, i, &len);

		if (!((len < srv_page_size) || (len == UNIV_SQL_NULL))) {
			ib::error() << "Record field " << i << " len " << len;
			return(FALSE);
		}

		if (len != UNIV_SQL_NULL) {
			len_sum += len;
		} else {
			len_sum += rec_get_nth_field_size(rec, i);
		}
	}

	if (len_sum != rec_get_data_size_old(rec)) {
		ib::error() << "Record len should be " << len_sum << ", len "
			<< rec_get_data_size_old(rec);
		return(FALSE);
	}

	return(TRUE);
}

/***************************************************************//**
Validates the consistency of a physical record.
@return TRUE if ok */
ibool
rec_validate(
/*=========*/
	const rec_t*	rec,	/*!< in: physical record */
	const rec_offs*	offsets)/*!< in: array returned by rec_get_offsets() */
{
	ulint		len;
	ulint		n_fields;
	ulint		len_sum		= 0;
	ulint		i;

	n_fields = rec_offs_n_fields(offsets);

	if ((n_fields == 0) || (n_fields > REC_MAX_N_FIELDS)) {
		ib::error() << "Record has " << n_fields << " fields";
		return(FALSE);
	}

	ut_a(rec_offs_any_flag(offsets, REC_OFFS_COMPACT | REC_OFFS_DEFAULT)
	     || n_fields <= rec_get_n_fields_old(rec));

	for (i = 0; i < n_fields; i++) {
		rec_get_nth_field_offs(offsets, i, &len);

		switch (len) {
		default:
			if (len >= srv_page_size) {
				ib::error() << "Record field " << i
					<< " len " << len;
				return(FALSE);
			}
			len_sum += len;
			break;
		case UNIV_SQL_DEFAULT:
			break;
		case UNIV_SQL_NULL:
			if (!rec_offs_comp(offsets)) {
				len_sum += rec_get_nth_field_size(rec, i);
			}
		}
	}

	if (len_sum != rec_offs_data_size(offsets)) {
		ib::error() << "Record len should be " << len_sum << ", len "
			<< rec_offs_data_size(offsets);
		return(FALSE);
	}

	if (!rec_offs_comp(offsets)) {
		ut_a(rec_validate_old(rec));
	}

	return(TRUE);
}

/***************************************************************//**
Prints an old-style physical record. */
void
rec_print_old(
/*==========*/
	FILE*		file,	/*!< in: file where to print */
	const rec_t*	rec)	/*!< in: physical record */
{
	const byte*	data;
	ulint		len;
	ulint		n;
	ulint		i;

	n = rec_get_n_fields_old(rec);

	fprintf(file, "PHYSICAL RECORD: n_fields " ULINTPF ";"
		" %u-byte offsets; info bits " ULINTPF "\n",
		n,
		rec_get_1byte_offs_flag(rec) ? 1 : 2,
		rec_get_info_bits(rec, FALSE));

	for (i = 0; i < n; i++) {

		data = rec_get_nth_field_old(rec, i, &len);

		fprintf(file, " " ULINTPF ":", i);

		if (len != UNIV_SQL_NULL) {
			if (len <= 30) {

				ut_print_buf(file, data, len);
			} else {
				ut_print_buf(file, data, 30);

				fprintf(file, " (total " ULINTPF " bytes)",
					len);
			}
		} else {
			fprintf(file, " SQL NULL, size " ULINTPF " ",
				rec_get_nth_field_size(rec, i));
		}

		putc(';', file);
		putc('\n', file);
	}

	rec_validate_old(rec);
}

/***************************************************************//**
Prints a physical record in ROW_FORMAT=COMPACT.  Ignores the
record header. */
static
void
rec_print_comp(
/*===========*/
	FILE*		file,	/*!< in: file where to print */
	const rec_t*	rec,	/*!< in: physical record */
	const rec_offs*	offsets)/*!< in: array returned by rec_get_offsets() */
{
	ulint	i;

	for (i = 0; i < rec_offs_n_fields(offsets); i++) {
          const byte*	UNINIT_VAR(data);
		ulint		len;

		if (rec_offs_nth_default(offsets, i)) {
			len = UNIV_SQL_DEFAULT;
		} else {
			data = rec_get_nth_field(rec, offsets, i, &len);
		}

		fprintf(file, " " ULINTPF ":", i);

		if (len == UNIV_SQL_NULL) {
			fputs(" SQL NULL", file);
		} else if (len == UNIV_SQL_DEFAULT) {
			fputs(" SQL DEFAULT", file);
		} else {
			if (len <= 30) {

				ut_print_buf(file, data, len);
			} else if (rec_offs_nth_extern(offsets, i)) {
				ut_print_buf(file, data, 30);
				fprintf(file,
					" (total " ULINTPF " bytes, external)",
					len);
				ut_print_buf(file, data + len
					     - BTR_EXTERN_FIELD_REF_SIZE,
					     BTR_EXTERN_FIELD_REF_SIZE);
			} else {
				ut_print_buf(file, data, 30);

				fprintf(file, " (total " ULINTPF " bytes)",
					len);
			}
		}
		putc(';', file);
		putc('\n', file);
	}
}

/***************************************************************//**
Prints an old-style spatial index record. */
static
void
rec_print_mbr_old(
/*==============*/
	FILE*		file,	/*!< in: file where to print */
	const rec_t*	rec)	/*!< in: physical record */
{
	const byte*	data;
	ulint		len;
	ulint		n;
	ulint		i;

	ut_ad(rec);

	n = rec_get_n_fields_old(rec);

	fprintf(file, "PHYSICAL RECORD: n_fields %lu;"
		" %u-byte offsets; info bits %lu\n",
		(ulong) n,
		rec_get_1byte_offs_flag(rec) ? 1 : 2,
		(ulong) rec_get_info_bits(rec, FALSE));

	for (i = 0; i < n; i++) {

		data = rec_get_nth_field_old(rec, i, &len);

		fprintf(file, " %lu:", (ulong) i);

		if (len != UNIV_SQL_NULL) {
			if (i == 0) {
				fprintf(file, " MBR:");
				for (; len > 0; len -= sizeof(double)) {
					double	d = mach_double_read(data);

					if (len != sizeof(double)) {
						fprintf(file, "%.2lf,", d);
					} else {
						fprintf(file, "%.2lf", d);
					}

					data += sizeof(double);
				}
			} else {
				if (len <= 30) {

					ut_print_buf(file, data, len);
				} else {
					ut_print_buf(file, data, 30);

					fprintf(file, " (total %lu bytes)",
						(ulong) len);
				}
			}
		} else {
			fprintf(file, " SQL NULL, size " ULINTPF " ",
				rec_get_nth_field_size(rec, i));
		}

		putc(';', file);
		putc('\n', file);
	}

	if (rec_get_deleted_flag(rec, false)) {
		fprintf(file, " Deleted");
	}

	if (rec_get_info_bits(rec, true) & REC_INFO_MIN_REC_FLAG) {
		fprintf(file, " First rec");
	}

	rec_validate_old(rec);
}

/***************************************************************//**
Prints a spatial index record. */
void
rec_print_mbr_rec(
/*==============*/
	FILE*		file,	/*!< in: file where to print */
	const rec_t*	rec,	/*!< in: physical record */
	const rec_offs*	offsets)/*!< in: array returned by rec_get_offsets() */
{
	ut_ad(rec_offs_validate(rec, NULL, offsets));
	ut_ad(!rec_offs_any_default(offsets));

	if (!rec_offs_comp(offsets)) {
		rec_print_mbr_old(file, rec);
		return;
	}

	for (ulint i = 0; i < rec_offs_n_fields(offsets); i++) {
		const byte*	data;
		ulint		len;

		data = rec_get_nth_field(rec, offsets, i, &len);

		if (i == 0) {
			fprintf(file, " MBR:");
			for (; len > 0; len -= sizeof(double)) {
				double	d = mach_double_read(data);

				if (len != sizeof(double)) {
					fprintf(file, "%.2lf,", d);
				} else {
					fprintf(file, "%.2lf", d);
				}

				data += sizeof(double);
			}
		} else {
			fprintf(file, " %lu:", (ulong) i);

			if (len != UNIV_SQL_NULL) {
				if (len <= 30) {

					ut_print_buf(file, data, len);
				} else {
					ut_print_buf(file, data, 30);

					fprintf(file, " (total %lu bytes)",
						(ulong) len);
				}
			} else {
				fputs(" SQL NULL", file);
			}
		}
		putc(';', file);
	}

	if (rec_get_info_bits(rec, true) & REC_INFO_DELETED_FLAG) {
		fprintf(file, " Deleted");
	}

	if (rec_get_info_bits(rec, true) & REC_INFO_MIN_REC_FLAG) {
		fprintf(file, " First rec");
	}


	rec_validate(rec, offsets);
}

/***************************************************************//**
Prints a physical record. */
void
rec_print_new(
/*==========*/
	FILE*		file,	/*!< in: file where to print */
	const rec_t*	rec,	/*!< in: physical record */
	const rec_offs*	offsets)/*!< in: array returned by rec_get_offsets() */
{
	ut_ad(rec_offs_validate(rec, NULL, offsets));

#ifdef UNIV_DEBUG
	if (rec_get_deleted_flag(rec, rec_offs_comp(offsets))) {
		DBUG_PRINT("info", ("deleted "));
	} else {
		DBUG_PRINT("info", ("not-deleted "));
	}
#endif /* UNIV_DEBUG */

	if (!rec_offs_comp(offsets)) {
		rec_print_old(file, rec);
		return;
	}

	fprintf(file, "PHYSICAL RECORD: n_fields " ULINTPF ";"
		" compact format; info bits " ULINTPF "\n",
		rec_offs_n_fields(offsets),
		rec_get_info_bits(rec, TRUE));

	rec_print_comp(file, rec, offsets);
	rec_validate(rec, offsets);
}

/***************************************************************//**
Prints a physical record. */
void
rec_print(
/*======*/
	FILE*			file,	/*!< in: file where to print */
	const rec_t*		rec,	/*!< in: physical record */
	const dict_index_t*	index)	/*!< in: record descriptor */
{
	if (!dict_table_is_comp(index->table)) {
		rec_print_old(file, rec);
		return;
	} else {
		mem_heap_t*	heap	= NULL;
		rec_offs	offsets_[REC_OFFS_NORMAL_SIZE];
		rec_offs_init(offsets_);

		rec_print_new(file, rec,
			      rec_get_offsets(rec, index, offsets_,
					      page_rec_is_leaf(rec),
					      ULINT_UNDEFINED, &heap));
		if (UNIV_LIKELY_NULL(heap)) {
			mem_heap_free(heap);
		}
	}
}

/** Pretty-print a record.
@param[in,out]	o	output stream
@param[in]	rec	physical record
@param[in]	info	rec_get_info_bits(rec)
@param[in]	offsets	rec_get_offsets(rec) */
void
rec_print(
	std::ostream&	o,
	const rec_t*	rec,
	ulint		info,
	const rec_offs*	offsets)
{
	const ulint	comp	= rec_offs_comp(offsets);
	const ulint	n	= rec_offs_n_fields(offsets);

	ut_ad(rec_offs_validate(rec, NULL, offsets));

	o << (comp ? "COMPACT RECORD" : "RECORD")
	  << "(info_bits=" << info << ", " << n << " fields): {";

	for (ulint i = 0; i < n; i++) {
		const byte*	data;
		ulint		len;

		if (i) {
			o << ',';
		}

		data = rec_get_nth_field(rec, offsets, i, &len);

		if (len == UNIV_SQL_DEFAULT) {
			o << "DEFAULT";
			continue;
		}

		if (len == UNIV_SQL_NULL) {
			o << "NULL";
			continue;
		}

		if (rec_offs_nth_extern(offsets, i)) {
			ulint	local_len = len - BTR_EXTERN_FIELD_REF_SIZE;
			ut_ad(len >= BTR_EXTERN_FIELD_REF_SIZE);

			o << '['
			  << local_len
			  << '+' << BTR_EXTERN_FIELD_REF_SIZE << ']';
			ut_print_buf(o, data, local_len);
			ut_print_buf_hex(o, data + local_len,
					 BTR_EXTERN_FIELD_REF_SIZE);
		} else {
			o << '[' << len << ']';
			ut_print_buf(o, data, len);
		}
	}

	o << "}";
}

/** Display a record.
@param[in,out]	o	output stream
@param[in]	r	record to display
@return	the output stream */
std::ostream&
operator<<(std::ostream& o, const rec_index_print& r)
{
	mem_heap_t*	heap	= NULL;
	rec_offs*	offsets	= rec_get_offsets(
		r.m_rec, r.m_index, NULL, page_rec_is_leaf(r.m_rec),
		ULINT_UNDEFINED, &heap);
	rec_print(o, r.m_rec,
		  rec_get_info_bits(r.m_rec, rec_offs_comp(offsets)),
		  offsets);
	mem_heap_free(heap);
	return(o);
}

/** Display a record.
@param[in,out]	o	output stream
@param[in]	r	record to display
@return	the output stream */
std::ostream&
operator<<(std::ostream& o, const rec_offsets_print& r)
{
	rec_print(o, r.m_rec,
		  rec_get_info_bits(r.m_rec, rec_offs_comp(r.m_offsets)),
		  r.m_offsets);
	return(o);
}

#ifdef UNIV_DEBUG
/** Read the DB_TRX_ID of a clustered index record.
@param[in]	rec	clustered index record
@param[in]	index	clustered index
@return the value of DB_TRX_ID */
trx_id_t
rec_get_trx_id(
	const rec_t*		rec,
	const dict_index_t*	index)
{
	const byte*	trx_id;
	ulint		len;
	mem_heap_t*	heap		= NULL;
	rec_offs offsets_[REC_OFFS_HEADER_SIZE + MAX_REF_PARTS + 2];
	rec_offs_init(offsets_);
	rec_offs* offsets = offsets_;

	offsets = rec_get_offsets(rec, index, offsets, true,
				  index->db_trx_id() + 1, &heap);

	trx_id = rec_get_nth_field(rec, offsets, index->db_trx_id(), &len);

	ut_ad(len == DATA_TRX_ID_LEN);

	if (UNIV_LIKELY_NULL(heap)) {
		mem_heap_free(heap);
	}

	return(trx_read_trx_id(trx_id));
}
#endif /* UNIV_DEBUG */

/** Mark the nth field as externally stored.
@param[in]	offsets		array returned by rec_get_offsets()
@param[in]	n		nth field */
void
rec_offs_make_nth_extern(
	rec_offs*	offsets,
	const ulint	n)
{
	ut_ad(!rec_offs_nth_sql_null(offsets, n));
	set_type(rec_offs_base(offsets)[1 + n], STORED_OFFPAGE);
}
#ifdef WITH_WSREP
# include "ha_prototypes.h"

int
wsrep_rec_get_foreign_key(
	byte 		*buf,     /* out: extracted key */
	ulint 		*buf_len, /* in/out: length of buf */
	const rec_t*	rec,	  /* in: physical record */
	dict_index_t*	index_for,  /* in: index in foreign table */
	dict_index_t*	index_ref,  /* in: index in referenced table */
	ibool		new_protocol) /* in: protocol > 1 */
{
	const byte*	data;
	ulint		len;
	ulint		key_len = 0;
	ulint		i;
	uint            key_parts;
	mem_heap_t*	heap	= NULL;
	rec_offs	offsets_[REC_OFFS_NORMAL_SIZE];
	const rec_offs* offsets;

	ut_ad(index_for);
	ut_ad(index_ref);

        rec_offs_init(offsets_);
	offsets = rec_get_offsets(rec, index_for, offsets_, true,
				  ULINT_UNDEFINED, &heap);

	ut_ad(rec_offs_validate(rec, NULL, offsets));

	ut_ad(rec);

	key_parts = dict_index_get_n_unique_in_tree(index_for);
	for (i = 0; 
	     i < key_parts && 
	       (index_for->type & DICT_CLUSTERED || i < key_parts - 1); 
	     i++) {
		dict_field_t*	  field_f = 
			dict_index_get_nth_field(index_for, i);
		const dict_col_t* col_f = dict_field_get_col(field_f);
                dict_field_t*	  field_r = 
			dict_index_get_nth_field(index_ref, i);
		const dict_col_t* col_r = dict_field_get_col(field_r);

		ut_ad(!rec_offs_nth_default(offsets, i));
		data = rec_get_nth_field(rec, offsets, i, &len);
		if (key_len + ((len != UNIV_SQL_NULL) ? len + 1 : 1) > 
		    *buf_len) {
			fprintf(stderr,
				"WSREP: FK key len exceeded "
				ULINTPF " " ULINTPF " " ULINTPF "\n",
				key_len, len, *buf_len);
			goto err_out;
		}

		if (len == UNIV_SQL_NULL) {
			ut_a(!(col_f->prtype & DATA_NOT_NULL));
			*buf++ = 1;
			key_len++;
		} else if (!new_protocol) {
			if (!(col_r->prtype & DATA_NOT_NULL)) {
				*buf++ = 0;
				key_len++;
			}
			memcpy(buf, data, len);
			*buf_len = wsrep_innobase_mysql_sort(
				(int)(col_f->prtype & DATA_MYSQL_TYPE_MASK),
				(uint)dtype_get_charset_coll(col_f->prtype),
				buf, len, *buf_len);
		} else { /* new protocol */
			if (!(col_r->prtype & DATA_NOT_NULL)) {
				*buf++ = 0;
				key_len++;
			}
			switch (col_f->mtype) {
			case DATA_INT: {
				byte* ptr = buf+len;
				for (;;) {
					ptr--;
					*ptr = *data;
					if (ptr == buf) {
						break;
					}
					data++;
				}
		
				if (!(col_f->prtype & DATA_UNSIGNED)) {
					buf[len-1] = (byte) (buf[len-1] ^ 128);
				}

				break;
			}
			case DATA_VARCHAR:
			case DATA_VARMYSQL:
			case DATA_CHAR:
			case DATA_MYSQL:
				/* Copy the actual data */
				ut_memcpy(buf, data, len);
				len = wsrep_innobase_mysql_sort(
					(int)
					(col_f->prtype & DATA_MYSQL_TYPE_MASK),
					(uint)
					dtype_get_charset_coll(col_f->prtype),
					buf, len, *buf_len);
				break;
			case DATA_BLOB:
			case DATA_BINARY:
				memcpy(buf, data, len);
				break;
			default: 
				break;
			}

			key_len += len;
			buf 	+= len;
		}
	}

	rec_validate(rec, offsets);

	if (UNIV_LIKELY_NULL(heap)) {
		mem_heap_free(heap);
	}

	*buf_len = key_len;
	return DB_SUCCESS;

 err_out:
	if (UNIV_LIKELY_NULL(heap)) {
		mem_heap_free(heap);
	}
	return DB_ERROR;
}
#endif // WITH_WSREP