summaryrefslogtreecommitdiff
path: root/mysys/ma_dyncol.c
blob: c717f69c3e53cf78ef9aab8da713604ec5568b6f (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
/* Copyright (c) 2011, Monty Program Ab
   Copyright (c) 2011, Oleksandr Byelkin

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are
   met:

   1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

   2. Redistributions in binary form must the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.

   THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY
   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   SUCH DAMAGE.
*/

#include "mysys_priv.h"
#include <m_string.h>
#include <ma_dyncol.h>

/*
  Flag byte bits

  2 bits which determinate size of offset in the header -1
*/
/* mask to get above bits */
#define DYNCOL_FLG_OFFSET  3
/* All known flags mask */
#define DYNCOL_FLG_KNOWN  3

/* dynamic column size reserve */
#define DYNCOL_SYZERESERVE 80

/* length of fixed string header 1 byte - flags, 2 bytes - columns counter */
#define FIXED_HEADER_SIZE 3

#define COLUMN_NUMBER_SIZE 2

#define MAX_OFFSET_LENGTH  5

static enum enum_dyncol_func_result
dynamic_column_time_store(DYNAMIC_COLUMN *str,
                          MYSQL_TIME *value);
static enum enum_dyncol_func_result
dynamic_column_date_store(DYNAMIC_COLUMN *str,
                          MYSQL_TIME *value);
static enum enum_dyncol_func_result
dynamic_column_time_read_internal(DYNAMIC_COLUMN_VALUE *store_it_here,
                                  uchar *data, size_t length);
static enum enum_dyncol_func_result
dynamic_column_date_read_internal(DYNAMIC_COLUMN_VALUE *store_it_here,
                                  uchar *data, size_t length);

/**
  Initialize dynamic column string with (make it empty but correct format)

  @param str             The string to initialize
  @param size            Amount of preallocated memory for the string.

  @retval FALSE OK
  @retval TRUE  error
*/

static my_bool dynamic_column_init_str(DYNAMIC_COLUMN *str, size_t size)
{
  DBUG_ASSERT(size != 0);

  /*
    Make string with no fields (empty header)
    - First \0 is flags
    - other 2 \0 is number of fields
  */
  if (init_dynamic_string(str, NULL,
                          size + FIXED_HEADER_SIZE, DYNCOL_SYZERESERVE))
    return TRUE;
  bzero(str->str, FIXED_HEADER_SIZE);
  str->length= FIXED_HEADER_SIZE;
  return FALSE;
}


/**
  Calculate how many bytes needed to store val as variable length integer
  where first bit indicate continuation of the sequence.

  @param val             The value for which we are calculating length

  @return number of bytes
*/

static size_t dynamic_column_var_uint_bytes(ulonglong val)
{
  size_t len= 0;
  do
  {
    len++;
    val>>= 7;
  } while (val);
  return len;
}


/**
   Stores variable length unsigned integer value to a string

  @param str             The string where to append the value
  @param val             The value to put in the string

  @return ER_DYNCOL_* return code

  @notes
  This is used to store a number together with other data in the same
  object.  (Like decimals, length of string etc)
  (As we don't know the length of this object, we can't store 0 in 0 bytes)
*/

static enum enum_dyncol_func_result
dynamic_column_var_uint_store(DYNAMIC_COLUMN *str, ulonglong val)
{
  if (dynstr_realloc(str, 10))                  /* max what we can use */
    return ER_DYNCOL_RESOURCE;

  do
  {
    ulonglong rest= val >> 7;
    str->str[str->length++]= ((val & 0x7f) | (rest ? 0x80 : 0x00));
    val= rest;
  } while (val);
  return ER_DYNCOL_OK;
}


/**
  Reads variable length unsigned integer value from a string

  @param data            The string from which the int should be read
  @param data_length	 Max length of data
  @param len             Where to put length of the string read in bytes

  @return value of the unsigned integer read from the string

  In case of error, *len is set to 0
*/

static ulonglong
dynamic_column_var_uint_get(uchar *data, size_t data_length,
                            size_t *len)
{
  ulonglong val= 0;
  uint length;
  uchar *end= data + data_length;

  for (length=0; data < end ; data++)
  {
    val+= (((ulonglong)((*data) & 0x7f)) << (length * 7));
    length++;
    if (!((*data) & 0x80))
    {
      /* End of data */
      *len= length;
      return val;
    }
  }
  /* Something was wrong with data */
  *len= 0;                                      /* Mark error */
  return 0;
}


/**
  Calculate how many bytes needed to store val as unsigned.

  @param val             The value for which we are calculating length

  @return number of bytes (0-8)
*/

static size_t dynamic_column_uint_bytes(ulonglong val)
{
  size_t len;

  for (len= 0; val ; val>>= 8, len++)
    ;
  return len;
}


/**
  Append the string with given unsigned int value.

  @param str             The string where to put the value
  @param val             The value to put in the string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_uint_store(DYNAMIC_COLUMN *str, ulonglong val)
{
  if (dynstr_realloc(str, 8)) /* max what we can use */
    return ER_DYNCOL_RESOURCE;

  for (; val; val>>= 8)
    str->str[str->length++]= (char) (val & 0xff);
  return ER_DYNCOL_OK;
}


/**
  Read unsigned int value of given length from the string

  @param store_it_here   The structure to store the value
  @param data            The string which should be read
  @param length          The length (in bytes) of the value in nthe string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_uint_read(DYNAMIC_COLUMN_VALUE *store_it_here,
                         uchar *data, size_t length)
{
  ulonglong value= 0;
  size_t i;

  for (i= 0; i < length; i++)
    value+= ((ulonglong)data[i]) << (i*8);

  store_it_here->x.ulong_value= value;
  return ER_DYNCOL_OK;
}

/**
  Calculate how many bytes needed to store val as signed in following encoding:
    0 -> 0
   -1 -> 1
    1 -> 2
   -2 -> 3
    2 -> 4
   ...

  @param val             The value for which we are calculating length

  @return number of bytes
*/

static size_t dynamic_column_sint_bytes(longlong val)
{
  return dynamic_column_uint_bytes((val << 1) ^
                                   (val < 0 ? ULL(0xffffffffffffffff) : 0));
}


/**
  Append the string with given signed int value.

  @param str             the string where to put the value
  @param val             the value to put in the string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_sint_store(DYNAMIC_COLUMN *str, longlong val)
{
  return dynamic_column_uint_store(str,
                                 (val << 1) ^
                                 (val < 0 ? ULL(0xffffffffffffffff) : 0));
}


/**
  Read signed int value of given length from the string

  @param store_it_here   The structure to store the value
  @param data            The string which should be read
  @param length          The length (in bytes) of the value in the string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_sint_read(DYNAMIC_COLUMN_VALUE *store_it_here,
                         uchar *data, size_t length)
{
  ulonglong val;
  dynamic_column_uint_read(store_it_here, data, length);
  val= store_it_here->x.ulong_value;
  if (val & 1)
    val= (val >> 1) ^ ULL(0xffffffffffffffff);
  else
    val>>= 1;
  store_it_here->x.long_value= (longlong) val;
  return ER_DYNCOL_OK;
}


/**
  Calculate how many bytes needed to store the value.

  @param value          The value for which we are calculating length

  @return
  Error:  (size_t) ~0
  ok      number of bytes
*/

static size_t
dynamic_column_value_len(DYNAMIC_COLUMN_VALUE *value)
{
  switch (value->type) {
  case DYN_COL_NULL:
    return 0;
  case DYN_COL_INT:
    return dynamic_column_sint_bytes(value->x.long_value);
  case DYN_COL_UINT:
    return dynamic_column_uint_bytes(value->x.ulong_value);
  case DYN_COL_DOUBLE:
    return 8;
  case DYN_COL_STRING:
    return (dynamic_column_var_uint_bytes(value->x.string.charset->number) +
            value->x.string.value.length);
  case DYN_COL_DECIMAL:
  {
    int precision= value->x.decimal.value.intg + value->x.decimal.value.frac;
    int scale= value->x.decimal.value.frac;

    if (precision == 0 || decimal_is_zero(&value->x.decimal.value))
    {
      /* This is here to simplify dynamic_column_decimal_store() */
      value->x.decimal.value.intg= value->x.decimal.value.frac= 0;
      return 0;
    }
    /*
      Check if legal decimal;  This is needed to not get an assert in
      decimal_bin_size(). However this should be impossible as all
      decimals entered here should be valid and we have the special check
      above to handle the unlikely but possible case that decimal.value.intg
      and decimal.frac is 0.
    */
    if (scale < 0 || precision <= 0)
    {
      DBUG_ASSERT(0);                           /* Impossible */
      return (size_t) ~0;
    }
    return (dynamic_column_var_uint_bytes(value->x.decimal.value.intg) +
            dynamic_column_var_uint_bytes(value->x.decimal.value.frac) +
            decimal_bin_size(precision, scale));
  }
  case DYN_COL_DATETIME:
    /* date+time in bits: 14 + 4 + 5 + 10 + 6 + 6 + 20 + 1 66bits ~= 9 bytes */
    return 9;
  case DYN_COL_DATE:
    /* date in dits: 14 + 4 + 5 = 23bits ~= 3bytes*/
    return 3;
  case DYN_COL_TIME:
    /* time in bits: 10 + 6 + 6 + 20 + 1 = 43bits ~= 6bytes*/
    return 6;
  }
  DBUG_ASSERT(0);
  return 0;
}


/**
  Append double value to a string

  @param str             the string where to put the value
  @param val             the value to put in the string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_double_store(DYNAMIC_COLUMN *str, double val)
{
   if (dynstr_realloc(str, 8))
     return ER_DYNCOL_RESOURCE;
   float8store(str->str + str->length, val);
   str->length+= 8;
   return ER_DYNCOL_OK;
}


/**
  Read double value of given length from the string

  @param store_it_here   The structure to store the value
  @param data            The string which should be read
  @param length          The length (in bytes) of the value in nthe string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_double_read(DYNAMIC_COLUMN_VALUE *store_it_here,
                               uchar *data, size_t length)
{
  if (length != 8)
    return ER_DYNCOL_FORMAT;
  float8get(store_it_here->x.double_value, data);
  return ER_DYNCOL_OK;
}


/**
  Append the string with given string value.

  @param str             the string where to put the value
  @param val             the value to put in the string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_string_store(DYNAMIC_COLUMN *str, LEX_STRING *string,
                            CHARSET_INFO *charset)
{
  enum enum_dyncol_func_result rc;
  if ((rc= dynamic_column_var_uint_store(str, charset->number)))
    return rc;
  if (dynstr_append_mem(str, string->str, string->length))
    return ER_DYNCOL_RESOURCE;
  return ER_DYNCOL_OK;
}


/**
  Read string value of given length from the packed string

  @param store_it_here   The structure to store the value
  @param data            The packed string which should be read
  @param length          The length (in bytes) of the value in nthe string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_string_read(DYNAMIC_COLUMN_VALUE *store_it_here,
                           uchar *data, size_t length)
{
  size_t len;
  uint charset_nr= (uint)dynamic_column_var_uint_get(data, length, &len);
  if (len == 0)                                /* Wrong packed number */
    return ER_DYNCOL_FORMAT;
  store_it_here->x.string.charset= get_charset(charset_nr, MYF(MY_WME));
  if (store_it_here->x.string.charset == NULL)
    return ER_DYNCOL_UNKNOWN_CHARSET;
  data+= len;
  store_it_here->x.string.value.length= (length-= len);
  store_it_here->x.string.value.str= (char*) data;
  return ER_DYNCOL_OK;
}


/**
  Append the string with given decimal value.

  @param str             the string where to put the value
  @param val             the value to put in the string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_decimal_store(DYNAMIC_COLUMN *str,
                             decimal_t *value)
{
  uint bin_size;
  int precision= value->intg + value->frac;
  
  /* Store decimal zero as empty string */
  if (precision == 0)
    return ER_DYNCOL_OK;

  bin_size= decimal_bin_size(precision, value->frac);
  if (dynstr_realloc(str, bin_size + 20))
    return ER_DYNCOL_RESOURCE;

  /* The following can't fail as memory is already allocated */
  (void) dynamic_column_var_uint_store(str, value->intg);
  (void) dynamic_column_var_uint_store(str, value->frac);

  decimal2bin(value, (uchar *) str->str + str->length,
              precision, value->frac);
  str->length+= bin_size;
  return ER_DYNCOL_OK;
}


/**
  Prepare the value to be used as decimal.

  @param value           The value structure which sould be setup.
*/

void dynamic_column_prepare_decimal(DYNAMIC_COLUMN_VALUE *value)
{
  value->x.decimal.value.buf= value->x.decimal.buffer;
  value->x.decimal.value.len= DECIMAL_BUFF_LENGTH;
  /* just to be safe */
  value->type= DYN_COL_DECIMAL;
  decimal_make_zero(&value->x.decimal.value);
}


/**
  Read decimal value of given length from the string

  @param store_it_here   The structure to store the value
  @param data            The string which should be read
  @param length          The length (in bytes) of the value in nthe string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_decimal_read(DYNAMIC_COLUMN_VALUE *store_it_here,
                            uchar *data, size_t length)
{
  size_t intg_len, frac_len;
  int intg, frac, precision, scale;

  dynamic_column_prepare_decimal(store_it_here);
  /* Decimals 0.0 is stored as a zero length string */
  if (length == 0)
    return ER_DYNCOL_OK;                        /* value contains zero */

  intg= (int)dynamic_column_var_uint_get(data, length, &intg_len);
  data+= intg_len;
  frac= (int)dynamic_column_var_uint_get(data, length - intg_len, &frac_len);
  data+= frac_len;

  /* Check the size of data is correct */
  precision= intg + frac;
  scale=     frac;
  if (scale < 0 || precision <= 0 || scale > precision ||
      (length - intg_len - frac_len) >
      (size_t) (DECIMAL_BUFF_LENGTH*sizeof(decimal_digit_t)) ||
      decimal_bin_size(intg + frac, frac) !=
      (int) (length - intg_len - frac_len))
    return ER_DYNCOL_FORMAT;

  if (bin2decimal(data, &store_it_here->x.decimal.value, precision, scale) !=
      E_DEC_OK)
    return ER_DYNCOL_FORMAT;
  return ER_DYNCOL_OK;
}


/**
  Append the string with given datetime value.

  @param str             the string where to put the value
  @param value           the value to put in the string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_date_time_store(DYNAMIC_COLUMN *str, MYSQL_TIME *value)
{
  enum enum_dyncol_func_result rc;
  /*
    0<----year----><mn><day>00000!<-hours--><min-><sec-><---microseconds--->
     12345678901234123412345     1123456789012345612345612345678901234567890
    <123456><123456><123456><123456><123456><123456><123456><123456><123456>
  */
  if ((rc= dynamic_column_date_store(str, value)) ||
      (rc= dynamic_column_time_store(str, value)))
    return rc;
  return ER_DYNCOL_OK;
}


/**
  Read datetime value of given length from the packed string

  @param store_it_here   The structure to store the value
  @param data            The packed string which should be read
  @param length          The length (in bytes) of the value in nthe string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_date_time_read(DYNAMIC_COLUMN_VALUE *store_it_here,
                              uchar *data, size_t length)
{
  enum enum_dyncol_func_result rc= ER_DYNCOL_FORMAT;
  /*
    0<----year----><mn><day>00000!<-hours--><min-><sec-><---microseconds--->
     12345678901234123412345     1123456789012345612345612345678901234567890
    <123456><123456><123456><123456><123456><123456><123456><123456><123456>
  */
  if (length != 9)
    goto err;
  store_it_here->x.time_value.time_type= MYSQL_TIMESTAMP_DATETIME;
  if ((rc= dynamic_column_date_read_internal(store_it_here, data, 3)) ||
      (rc= dynamic_column_time_read_internal(store_it_here, data + 3, 6)))
    goto err;
  return ER_DYNCOL_OK;

err:
  store_it_here->x.time_value.time_type= MYSQL_TIMESTAMP_ERROR;
  return rc;
}


/**
  Append the string with given time value.

  @param str             the string where to put the value
  @param value           the value to put in the string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_time_store(DYNAMIC_COLUMN *str, MYSQL_TIME *value)
{
  uchar *buf;
  if (dynstr_realloc(str, 6))
    return ER_DYNCOL_RESOURCE;

  buf= ((uchar *)str->str) + str->length;

  if (value->time_type == MYSQL_TIMESTAMP_NONE ||
      value->time_type == MYSQL_TIMESTAMP_ERROR ||
      value->time_type == MYSQL_TIMESTAMP_DATE)
  {
    value->neg= 0;
    value->second_part= 0;
    value->hour= 0;
    value->minute= 0;
    value->second= 0;
  }
  DBUG_ASSERT(value->hour <= 838);
  DBUG_ASSERT(value->minute <= 59);
  DBUG_ASSERT(value->second <= 59);
  DBUG_ASSERT(value->second_part <= 999999);
  /*
    00000!<-hours--><min-><sec-><---microseconds--->
         1123456789012345612345612345678901234567890
    <123456><123456><123456><123456><123456><123456>
  */
  buf[0]= (value->second_part & 0xff);
  buf[1]= ((value->second_part & 0xff00) >> 8);
  buf[2]= (uchar)(((value->second & 0xf) << 4) |
           ((value->second_part & 0xf0000) >> 16));
  buf[3]= ((value->minute << 2) | ((value->second & 0x30) >> 4));
  buf[4]= (value->hour & 0xff);
  buf[5]= ((value->neg ? 0x4 : 0) | (value->hour >> 8));
  str->length+= 6;
  return ER_DYNCOL_OK;
}


/**
  Read time value of given length from the packed string

  @param store_it_here   The structure to store the value
  @param data            The packed string which should be read
  @param length          The length (in bytes) of the value in nthe string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_time_read(DYNAMIC_COLUMN_VALUE *store_it_here,
                         uchar *data, size_t length)
{
  store_it_here->x.time_value.year= store_it_here->x.time_value.month=
    store_it_here->x.time_value.day= 0;
  store_it_here->x.time_value.time_type= MYSQL_TIMESTAMP_TIME;
  return dynamic_column_time_read_internal(store_it_here, data, length);
}

/**
  Internal function for reading time part from the string.

  @param store_it_here   The structure to store the value
  @param data            The packed string which should be read
  @param length          The length (in bytes) of the value in nthe string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_time_read_internal(DYNAMIC_COLUMN_VALUE *store_it_here,
                                  uchar *data, size_t length)
{
  if (length != 6)
    goto err;
  /*
    00000!<-hours--><min-><sec-><---microseconds--->
         1123456789012345612345612345678901234567890
    <123456><123456><123456><123456><123456><123456>
  */
  store_it_here->x.time_value.second_part= (data[0] |
                                          (data[1] << 8) |
                                          ((data[2] & 0xf) << 16));
  store_it_here->x.time_value.second= ((data[2] >> 4) |
                                     ((data[3] & 0x3) << 4));
  store_it_here->x.time_value.minute= (data[3] >> 2);
  store_it_here->x.time_value.hour= (((((uint)data[5]) & 0x3 ) << 8) | data[4]);
  store_it_here->x.time_value.neg= ((data[5] & 0x4) ? 1 : 0);
  if (store_it_here->x.time_value.second > 59 ||
      store_it_here->x.time_value.minute > 59 ||
      store_it_here->x.time_value.hour > 838 ||
      store_it_here->x.time_value.second_part > 999999)
    goto err;
  return ER_DYNCOL_OK;

err:
  store_it_here->x.time_value.time_type= MYSQL_TIMESTAMP_ERROR;
  return ER_DYNCOL_FORMAT;
}


/**
  Append the string with given date value.

  @param str             the string where to put the value
  @param value           the value to put in the string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_date_store(DYNAMIC_COLUMN *str, MYSQL_TIME *value)
{
  uchar *buf;
  if (dynstr_realloc(str, 3))
    return ER_DYNCOL_RESOURCE;

  buf= ((uchar *)str->str) + str->length;
  if (value->time_type == MYSQL_TIMESTAMP_NONE ||
      value->time_type == MYSQL_TIMESTAMP_ERROR ||
      value->time_type == MYSQL_TIMESTAMP_TIME)
    value->year= value->month= value->day = 0;
  DBUG_ASSERT(value->year <= 9999);
  DBUG_ASSERT(value->month <= 12);
  DBUG_ASSERT(value->day <= 31);
  /*
    0<----year----><mn><day>
    012345678901234123412345
    <123456><123456><123456>
  */
  buf[0]= (value->day |
           ((value->month & 0x7) << 5));
  buf[1]= ((value->month >> 3) | ((value->year & 0x7F) << 1));
  buf[2]= (value->year >> 7);
  str->length+= 3;
  return ER_DYNCOL_OK;
}



/**
  Read date value of given length from the packed string

  @param store_it_here   The structure to store the value
  @param data            The packed string which should be read
  @param length          The length (in bytes) of the value in nthe string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_date_read(DYNAMIC_COLUMN_VALUE *store_it_here,
                         uchar *data, size_t length)
{
  store_it_here->x.time_value.neg= 0;
  store_it_here->x.time_value.second_part= 0;
  store_it_here->x.time_value.hour= 0;
  store_it_here->x.time_value.minute= 0;
  store_it_here->x.time_value.second= 0;
  store_it_here->x.time_value.time_type= MYSQL_TIMESTAMP_DATE;
  return dynamic_column_date_read_internal(store_it_here, data, length);
}

/**
  Internal function for reading date part from the string.

  @param store_it_here   The structure to store the value
  @param data            The packed string which should be read
  @param length          The length (in bytes) of the value in nthe string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_date_read_internal(DYNAMIC_COLUMN_VALUE *store_it_here,
                                  uchar *data,
                                  size_t length)
{
  if (length != 3)
    goto err;
  /*
    0<----year----><mn><day>
     12345678901234123412345
    <123456><123456><123456>
  */
  store_it_here->x.time_value.day= (data[0] & 0x1f);
  store_it_here->x.time_value.month= (((data[1] & 0x1) << 3) |
                                    (data[0] >> 5));
  store_it_here->x.time_value.year= ((((uint)data[2]) << 7) |
                                    (data[1] >> 1));
  if (store_it_here->x.time_value.day > 31 ||
      store_it_here->x.time_value.month > 12 ||
      store_it_here->x.time_value.year > 9999)
    goto err;
  return ER_DYNCOL_OK;

err:
  store_it_here->x.time_value.time_type= MYSQL_TIMESTAMP_ERROR;
  return ER_DYNCOL_FORMAT;
}


/**
  Append the string with given value.

  @param str             the string where to put the value
  @param value           the value to put in the string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
data_store(DYNAMIC_COLUMN *str, DYNAMIC_COLUMN_VALUE *value)
{
  switch (value->type) {
  case DYN_COL_INT:
    return dynamic_column_sint_store(str, value->x.long_value);
  case DYN_COL_UINT:
    return dynamic_column_uint_store(str, value->x.ulong_value);
  case DYN_COL_DOUBLE:
    return dynamic_column_double_store(str, value->x.double_value);
  case DYN_COL_STRING:
    return dynamic_column_string_store(str, &value->x.string.value,
                                     value->x.string.charset);
  case DYN_COL_DECIMAL:
    return dynamic_column_decimal_store(str, &value->x.decimal.value);
  case DYN_COL_DATETIME:
    /* date+time in bits: 14 + 4 + 5 + 5 + 6 + 6 40bits = 5 bytes */
    return dynamic_column_date_time_store(str, &value->x.time_value);
  case DYN_COL_DATE:
    /* date in dits: 14 + 4 + 5 = 23bits ~= 3bytes*/
    return dynamic_column_date_store(str, &value->x.time_value);
  case DYN_COL_TIME:
    /* time in bits: 5 + 6 + 6 = 17bits ~= 3bytes*/
    return dynamic_column_time_store(str, &value->x.time_value);
  case DYN_COL_NULL:
    break;                                      /* Impossible */
  }
  DBUG_ASSERT(0);
  return ER_DYNCOL_OK;                          /* Impossible */
}


/**
  Calculate length of offset field for given data length

  @param data_length     Length of the data segment

  @return number of bytes
*/

static size_t dynamic_column_offset_bytes(size_t data_length)
{
  if (data_length < 0x1f)                /* all 1 value is reserved */
    return 1;
  if (data_length < 0x1fff)              /* all 1 value is reserved */
    return 2;
  if (data_length < 0x1fffff)            /* all 1 value is reserved */
    return 3;
  if (data_length < 0x1fffffff)          /* all 1 value is reserved */
    return 4;
  return MAX_OFFSET_LENGTH;              /* For future */
}

/**
  Store offset and type information in the given place

  @param place           Beginning of the index entry
  @param offset_size     Size of offset field in bytes
  @param type            Type to be written
  @param offset          Offset to be written
*/

static void type_and_offset_store(uchar *place, size_t offset_size,
                                  DYNAMIC_COLUMN_TYPE type,
                                  size_t offset)
{
  ulong val = (((ulong) offset) << 3) | (type - 1);
  DBUG_ASSERT(type != DYN_COL_NULL);
  DBUG_ASSERT(((type - 1) & (~7)) == 0); /* fit in 3 bits */

  /* Index entry starts with column number; Jump over it */
  place+= COLUMN_NUMBER_SIZE;                   

  switch (offset_size) {
  case 1:
    DBUG_ASSERT(offset < 0x1f);          /* all 1 value is reserved */
    place[0]= (uchar)val;
    break;
  case 2:
    DBUG_ASSERT(offset < 0x1fff);        /* all 1 value is reserved */
    int2store(place, val);
    break;
  case 3:
    DBUG_ASSERT(offset < 0x1fffff);      /* all 1 value is reserved */
    int3store(place, val);
    break;
  case 4:
    DBUG_ASSERT(offset < 0x1fffffff);    /* all 1 value is reserved */
    int4store(place, val);
    break;
  default:
    DBUG_ASSERT(0);                             /* impossible */
  }
}


/**
  Read offset and type information from index entry

  @param type            Where to put type info
  @param offset          Where to put offset info
  @param place           Beginning of the index entry
  @param offset_size     Size of offset field in bytes
*/

static void type_and_offset_read(DYNAMIC_COLUMN_TYPE *type,
                                 size_t *offset,
                                 uchar *place, size_t offset_size)
{
  ulong UNINIT_VAR(val);

  place+= COLUMN_NUMBER_SIZE;                 /* skip column number */
  switch (offset_size) {
  case 1:
    val= (ulong)place[0];
    break;
  case 2:
    val= uint2korr(place);
    break;
  case 3:
    val= uint3korr(place);
    break;
  case 4:
    val= uint4korr(place);
    break;
  default:
    DBUG_ASSERT(0);                             /* impossible */
  }
  *type= (val & 0x7) + 1;
  *offset= val >> 3;
}


/**
  Comparator function for references on column numbers for qsort
*/

static int column_sort(const void *a, const void *b)
{
  return **((uint **)a) - **((uint **)b);
}


/**
  Write information to the fixed header

  @param str             String where to write the header
  @param offset_size     Size of offset field in bytes
  @param column_count    Number of columns
*/

static void set_fixed_header(DYNAMIC_COLUMN *str,
                             uint offset_size,
                             uint column_count)
{
  DBUG_ASSERT(column_count <= 0xffff);
  DBUG_ASSERT(offset_size <= 4);
  str->str[0]= ((str->str[0] & ~DYNCOL_FLG_OFFSET) |
                (offset_size - 1));             /* size of offset */
  int2store(str->str + 1, column_count);        /* columns number */
  DBUG_ASSERT((str->str[0] & (~DYNCOL_FLG_KNOWN)) == 0);
}

/*
  Calculate entry size (E) and header size (H) by offset size (O) and column
  count (C).
*/

#define calc_param(E,H,O,C) do { \
  (*(E))= (O) + COLUMN_NUMBER_SIZE;           \
  (*(H))= (*(E)) * (C);                       \
}while(0);


/**
  Adds columns into the empty string

  @param str             String where to write the data
  @param header_size     Size of the header without fixed part
  @param offset_size     Size of offset field in bytes
  @param column_count    Number of columns in the arrays
  @parem not_null_count  Number of non-null columns in the arrays
  @param data_size       Size of the data segment
  @param column_numbers  Array of columns numbers
  @param values          Array of columns values
  @param new_str         True if we need to allocate new string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_new_column_store(DYNAMIC_COLUMN *str,
                         size_t header_size,
                         size_t offset_size,
                         uint column_count,
                         uint not_null_count,
                         size_t data_size,
                         uint *column_numbers,
                         DYNAMIC_COLUMN_VALUE *values,
                         my_bool new_str)
{
  uchar *header_end;
  uint **columns_order;
  uint i;
  uint entry_size= COLUMN_NUMBER_SIZE + offset_size;
  enum enum_dyncol_func_result rc= ER_DYNCOL_RESOURCE;

  if (!(columns_order= malloc(sizeof(uint*)*column_count)))
    return ER_DYNCOL_RESOURCE;
  if (new_str)
  {
    if (dynamic_column_init_str(str,
                                data_size + header_size + DYNCOL_SYZERESERVE))
      goto err;
  }
  else
  {
    str->length= 0;
    if (dynstr_realloc(str, data_size + header_size + DYNCOL_SYZERESERVE))
      goto err;
    bzero(str->str, FIXED_HEADER_SIZE);
    str->length= FIXED_HEADER_SIZE;
  }

  /* sort columns for the header */
  for (i= 0; i < column_count; i++)
    columns_order[i]= column_numbers + i;
  qsort(columns_order, (size_t)column_count, sizeof(uint*), &column_sort);

  /*
    For now we don't allow creating two columns with the same number
    at the time of create.  This can be fixed later to just use the later
    by comparing the pointers.
  */
  for (i= 0; i < column_count - 1; i++)
  {
    if (columns_order[i][0] > UINT_MAX16 ||
        columns_order[i][0] == columns_order[i + 1][0])
    {
      rc= ER_DYNCOL_DATA;
      goto err;
    }
  }
  if (columns_order[i][0] > UINT_MAX16)
  {
    rc= ER_DYNCOL_DATA;
    goto err;
  }

  DBUG_ASSERT(str->max_length >= str->length + header_size);
  set_fixed_header(str, offset_size, not_null_count);
  str->length+= header_size; /* reserve place for header */
  header_end= (uchar *)str->str + FIXED_HEADER_SIZE;
  for (i= 0; i < column_count; i++)
  {
    uint ord= columns_order[i] - column_numbers;
    if (values[ord].type != DYN_COL_NULL)
    {
      /* Store header first in the str */
      int2store(header_end, column_numbers[ord]);
      type_and_offset_store(header_end, offset_size,
                            values[ord].type,
                            str->length - header_size - FIXED_HEADER_SIZE);

      /* Store value in 'str + str->length' and increase str->length */
      if ((rc= data_store(str, values + ord)))
        goto err;
      header_end+= entry_size;
    }
  }
  rc= ER_DYNCOL_OK;
err:
  free(columns_order);
  return rc;
}

/**
  Create packed string which contains given columns (internal)

  @param str             String where to write the data
  @param column_count    Number of columns in the arrays
  @param column_numbers  Array of columns numbers
  @param values          Array of columns values
  @param new_str         True if we need allocate new string

  @return ER_DYNCOL_* return code
*/

static enum enum_dyncol_func_result
dynamic_column_create_many_internal(DYNAMIC_COLUMN *str,
                                    uint column_count,
                                    uint *column_numbers,
                                    DYNAMIC_COLUMN_VALUE *values,
                                    my_bool new_str)
{
  size_t data_size= 0;
  size_t header_size, offset_size;
  uint i;
  int not_null_column_count= 0;

  if (new_str)
  {
    /* to make dynstr_free() working in case of errors */
    bzero(str, sizeof(DYNAMIC_COLUMN));
  }

  for (i= 0; i < column_count; i++)
  {
    if (values[i].type != DYN_COL_NULL)
    {
      size_t tmp;
      not_null_column_count++;
      data_size+= (tmp=dynamic_column_value_len(values + i));
      if (tmp == (size_t) ~0)
        return ER_DYNCOL_DATA;
    }
  }

  /* We can handle data up to 1fffffff = 536870911 bytes now */
  if ((offset_size= dynamic_column_offset_bytes(data_size)) >=
      MAX_OFFSET_LENGTH)
    return ER_DYNCOL_LIMIT;

  /* header entry is column number + offset & type */
  header_size= not_null_column_count * (offset_size + 2);

  return dynamic_new_column_store(str,
                                  header_size, offset_size,
                                  column_count,
                                  not_null_column_count,
                                  data_size,
                                  column_numbers, values,
                                  new_str);
}


/**
  Create packed string which contains given columns

  @param str             String where to write the data
  @param column_count    Number of columns in the arrays
  @param column_numbers  Array of columns numbers
  @param values          Array of columns values

  @return ER_DYNCOL_* return code
*/

enum enum_dyncol_func_result
dynamic_column_create_many(DYNAMIC_COLUMN *str,
                           uint column_count,
                           uint *column_numbers,
                           DYNAMIC_COLUMN_VALUE *values)
{
  DBUG_ENTER("dynamic_column_create_many");
  DBUG_RETURN(dynamic_column_create_many_internal(str, column_count,
                                                  column_numbers, values,
                                                  TRUE));
}


/**
  Create packed string which contains given column

  @param str             String where to write the data
  @param column_number   Column number
  @param value           The columns value

  @return ER_DYNCOL_* return code
*/

enum enum_dyncol_func_result
dynamic_column_create(DYNAMIC_COLUMN *str, uint column_nr,
                      DYNAMIC_COLUMN_VALUE *value)
{
  DBUG_ENTER("dynamic_column_create");
  DBUG_RETURN(dynamic_column_create_many(str, 1, &column_nr, value));
}


/**
  Calculate length of data between given two header entries

  @param entry           Pointer to the first entry
  @param entry_next      Pointer to the last entry
  @param header_end      Pointer to the header end
  @param offset_size     Size of offset field in bytes
  @param last_offset     Size of the data segment

  @return number of bytes
*/

static size_t get_length_interval(uchar *entry, uchar *entry_next,
                                  uchar *header_end, size_t offset_size,
                                  size_t last_offset)
{
  size_t offset, offset_next;
  DYNAMIC_COLUMN_TYPE type, type_next;
  DBUG_ASSERT(entry < entry_next);

  type_and_offset_read(&type, &offset, entry, offset_size);
  if (entry_next >= header_end)
    return (last_offset - offset);
  type_and_offset_read(&type_next, &offset_next, entry_next, offset_size);
  return (offset_next - offset);
}

/*
  Calculate length of data of one column


  @param entry           Pointer to the first entry
  @param header_end      Pointer to the header end
  @param offset_size     Size of offset field in bytes
  @param last_offset     Size of the data segment

  @return number of bytes
*/

static size_t get_length(uchar *entry, uchar *header_end,
                         size_t offset_size,
                         size_t last_offset)
{
  return get_length_interval(entry,
                             entry + offset_size + COLUMN_NUMBER_SIZE,
                             header_end, offset_size, last_offset);
}


/**
  Comparator function for references to header entries for qsort
*/

static int header_compar(const void *a, const void *b)
{
  uint va= uint2korr((uchar*)a), vb= uint2korr((uchar*)b);
  return (va > vb ? 1 : (va < vb ? -1 : 0));
}


/**
  Find column and fill information about it

  @param type            Returns type of the column
  @param data            Returns a pointer to the data
  @param length          Returns length of the data
  @param offset_size     Size of offset field in bytes
  @param column_count    Number of column in the packed string
  @param data_end        Pointer to the data end
  @param num             Number of the column we want to fetch
  @param entry_pos       NULL or place where to put reference to the entry

  @return 0 ok
  @return 1 error in data
*/

static my_bool
find_column(DYNAMIC_COLUMN_TYPE *type, uchar **data, size_t *length,
            uchar *header, size_t offset_size, uint column_count,
            uchar *data_end, uint num, uchar **entry_pos)
{
  uchar *entry;
  size_t offset, total_data, header_size, entry_size;
  uchar key[2+4];

  if (!entry_pos)
    entry_pos= &entry;

  calc_param(&entry_size, &header_size, offset_size, column_count);

  if (header + header_size > data_end)
    return 1;

  int2store(key, num);
  entry= bsearch(key, header, (size_t)column_count, entry_size,
                 &header_compar);
  if (!entry)
  {
    /* Column not found */
    *type= DYN_COL_NULL;
    *entry_pos= NULL;
    return 0;
  }
  type_and_offset_read(type, &offset, entry, offset_size);
  total_data= data_end - (header + header_size);
  if (offset > total_data)
    return 1;
  *data= header + header_size + offset;
  *length= get_length(entry, header + header_size, offset_size,
                      total_data);
  /*
    Check that the found data is withing the ranges. This can happen if
    we get data with wrong offsets.
  */
  if ((long) *length < 0 || offset + *length > total_data)
    return 1;

  *entry_pos= entry;
  return 0;
}


/**
   Read and check the header of the dynamic string

  @param str             Dynamic string

  @retval FALSE OK
  @retval TRUE  error

  Note
    We don't check for str->length == 0 as all code that calls this
    already have handled this case.
*/

static inline my_bool read_fixed_header(DYNAMIC_COLUMN *str,
                                        size_t *offset_size,
                                        uint *column_count)
{
  DBUG_ASSERT(str != NULL && str->length != 0);
  if ((str->length < FIXED_HEADER_SIZE) ||
      (str->str[0] & (~DYNCOL_FLG_KNOWN)))
    return 1;                                   /* Wrong header */
  *offset_size= (str->str[0] & DYNCOL_FLG_OFFSET) + 1;
  *column_count= uint2korr(str->str + 1);
  return 0;
}


/**
  Get dynamic column value

  @param str             The packed string to extract the column
  @param column_nr       Number of column to fetch
  @param store_it_here   Where to store the extracted value

  @return ER_DYNCOL_* return code
*/

int dynamic_column_get(DYNAMIC_COLUMN *str, uint column_nr,
                       DYNAMIC_COLUMN_VALUE *store_it_here)
{
  uchar *data;
  size_t offset_size, length;
  uint column_count;
  enum enum_dyncol_func_result rc= ER_DYNCOL_FORMAT;

  if (str->length == 0)
    goto null;

  if (read_fixed_header(str, &offset_size, &column_count))
    goto err;

  if (column_count == 0)
    goto null;

  if (find_column(&store_it_here->type, &data, &length,
                  (uchar*)str->str + FIXED_HEADER_SIZE,
                  offset_size, column_count, (uchar*)str->str + str->length,
                  column_nr, NULL))
    goto err;

  switch (store_it_here->type) {
  case DYN_COL_INT:
    rc= dynamic_column_sint_read(store_it_here, data, length);
    break;
  case DYN_COL_UINT:
    rc= dynamic_column_uint_read(store_it_here, data, length);
    break;
  case DYN_COL_DOUBLE:
    rc= dynamic_column_double_read(store_it_here, data, length);
    break;
  case DYN_COL_STRING:
    rc= dynamic_column_string_read(store_it_here, data, length);
    break;
  case DYN_COL_DECIMAL:
    rc= dynamic_column_decimal_read(store_it_here, data, length);
    break;
  case DYN_COL_DATETIME:
    rc= dynamic_column_date_time_read(store_it_here, data, length);
    break;
  case DYN_COL_DATE:
    rc= dynamic_column_date_read(store_it_here, data, length);
    break;
  case DYN_COL_TIME:
    rc= dynamic_column_time_read(store_it_here, data, length);
    break;
  case DYN_COL_NULL:
    rc= ER_DYNCOL_OK;
    break;
  default:
    goto err;
  }
  return rc;

null:
    rc= ER_DYNCOL_OK;
err:
    store_it_here->type= DYN_COL_NULL;
    return rc;
}

/**
  Delete column with given number from the packed string

  @param str             The packed string to delete the column
  @param column_nr       Number of column to delete

  @return ER_DYNCOL_* return code
*/

int dynamic_column_delete(DYNAMIC_COLUMN *str, uint column_nr)
{
  uchar *data, *header_entry, *read, *write;
  size_t offset_size, new_offset_size, length, entry_size, new_entry_size,
         header_size, new_header_size, data_size, new_data_size,
         deleted_entry_offset;
  uint column_count, i;
  DYNAMIC_COLUMN_TYPE type;

  if (str->length == 0)
    return ER_DYNCOL_OK;  /* no columns */

  if (read_fixed_header(str, &offset_size, &column_count))
    return ER_DYNCOL_FORMAT;

  if (column_count == 0)
  {
    str->length= 0;
    return ER_DYNCOL_OK;  /* no columns */
  }

  if (find_column(&type, &data, &length, (uchar*)str->str + FIXED_HEADER_SIZE,
                  offset_size, column_count, (uchar*)str->str + str->length,
                  column_nr, &header_entry))
    return ER_DYNCOL_FORMAT;

  if (type == DYN_COL_NULL)
    return ER_DYNCOL_OK;  /* no such column */

  if (column_count == 1)
  {
    /* delete the only column; Return empty string */
    str->length= 0;
    return ER_DYNCOL_OK;
  }

  /* Calculate entry_size and header_size */
  calc_param(&entry_size, &header_size, offset_size, column_count);
  data_size= str->length - FIXED_HEADER_SIZE - header_size;

  new_data_size= data_size - length;
  if ((new_offset_size= dynamic_column_offset_bytes(new_data_size)) >=
      MAX_OFFSET_LENGTH)
    return ER_DYNCOL_LIMIT;
  DBUG_ASSERT(new_offset_size <= offset_size);

  calc_param(&new_entry_size, &new_header_size,
             new_offset_size, column_count - 1);

  deleted_entry_offset= ((data - (uchar*) str->str) -
                         header_size - FIXED_HEADER_SIZE);

  /* rewrite header*/
  set_fixed_header(str, new_offset_size, column_count - 1);
  for (i= 0, write= read= (uchar *)str->str + FIXED_HEADER_SIZE;
       i < column_count;
       i++, read+= entry_size, write+= new_entry_size)
  {
    size_t offs;
    uint nm;
    DYNAMIC_COLUMN_TYPE tp;
    if (read == header_entry)
    {
#ifndef DBUG_OFF
      nm= uint2korr(read);
      type_and_offset_read(&tp, &offs, read,
                           offset_size);
      DBUG_ASSERT(nm == column_nr);
      DBUG_ASSERT(offs == deleted_entry_offset);
#endif
      write-= new_entry_size;                 /* do not move writer */
      continue;                               /* skip removed field */
    }

    nm= uint2korr(read),
    type_and_offset_read(&tp, &offs, read,
                         offset_size);

    if (offs > deleted_entry_offset)
      offs-= length;              /* data stored after removed data */

    int2store(write, nm);
    type_and_offset_store(write, new_offset_size, tp, offs);
  }

  /* move data */
  {
    size_t first_chunk_len= ((data - (uchar *)str->str) -
                             FIXED_HEADER_SIZE - header_size);
    size_t second_chunk_len= new_data_size - first_chunk_len;
    if (first_chunk_len)
      memmove(str->str + FIXED_HEADER_SIZE + new_header_size,
              str->str + FIXED_HEADER_SIZE + header_size,
              first_chunk_len);
    if (second_chunk_len)
      memmove(str->str +
              FIXED_HEADER_SIZE + new_header_size + first_chunk_len,
              str->str +
              FIXED_HEADER_SIZE + header_size + first_chunk_len + length,
              second_chunk_len);
  }

  /* fix str length */
  DBUG_ASSERT(str->length >=
              FIXED_HEADER_SIZE + new_header_size + new_data_size);
  str->length= FIXED_HEADER_SIZE + new_header_size + new_data_size;

  return ER_DYNCOL_OK;
}


/**
  Check existence of the column in the packed string

  @param str             The packed string to check the column
  @param column_nr       Number of column to check

  @return ER_DYNCOL_* return code
*/

enum enum_dyncol_func_result
dynamic_column_exists(DYNAMIC_COLUMN *str, uint column_nr)
{
  uchar *data;
  size_t offset_size, length;
  uint column_count;
  DYNAMIC_COLUMN_TYPE type;

  if (str->length == 0)
    return ER_DYNCOL_NO;                        /* no columns */

  if (read_fixed_header(str, &offset_size, &column_count))
    return ER_DYNCOL_FORMAT;

  if (column_count == 0)
    return ER_DYNCOL_NO;                        /* no columns */

  if (find_column(&type, &data, &length, (uchar*)str->str + FIXED_HEADER_SIZE,
                  offset_size, column_count, (uchar*)str->str + str->length,
                  column_nr, NULL))
    return ER_DYNCOL_FORMAT;

  return (type != DYN_COL_NULL ? ER_DYNCOL_YES : ER_DYNCOL_NO);
}


/**
  List not-null columns in the packed string

  @param str             The packed string
  @param array_of_uint   Where to put reference on created array

  @return ER_DYNCOL_* return code
*/

enum enum_dyncol_func_result
dynamic_column_list(DYNAMIC_COLUMN *str, DYNAMIC_ARRAY *array_of_uint)
{
  uchar *read;
  size_t offset_size, entry_size;
  uint column_count, i;

  bzero(array_of_uint, sizeof(*array_of_uint)); /* In case of errors */
  if (str->length == 0)
    return ER_DYNCOL_OK;                        /* no columns */

  if (read_fixed_header(str, &offset_size, &column_count))
    return ER_DYNCOL_FORMAT;

  entry_size= COLUMN_NUMBER_SIZE + offset_size;

  if (entry_size * column_count + FIXED_HEADER_SIZE > str->length)
    return ER_DYNCOL_FORMAT;

  if (init_dynamic_array(array_of_uint, sizeof(uint), column_count, 0))
    return ER_DYNCOL_RESOURCE;

  for (i= 0, read= (uchar *)str->str + FIXED_HEADER_SIZE;
       i < column_count;
       i++, read+= entry_size)
  {
    uint nm= uint2korr(read);
    /* Insert can't never fail as it's pre-allocated above */
    (void) insert_dynamic(array_of_uint, (uchar *)&nm);
  }
  return ER_DYNCOL_OK;
}


/**
  Find the place of the column in the header or place where it should be put

  @param num             Number of the column
  @param header          Pointer to the header
  @param entry_size      Size of a header entry
  @param column_count    Number of columns in the packed string
  @param entry           Return pointer to the entry or next entry

  @retval TRUE found
  @retval FALSE pointer set to the next row
*/

static my_bool
find_place(uint num, uchar *header, size_t entry_size,
           uint column_count, uchar **entry)
{
  uint mid, start, end, val;
  int flag;
  LINT_INIT(flag);                              /* 100 % safe */

  start= 0;
  end= column_count -1;
  mid= 1;
  while (start != end)
  {
   uint val;
   mid= (start + end) / 2;
   val= uint2korr(header + mid * entry_size);
   if ((flag= CMP_NUM(num, val)) <= 0)
     end= mid;
   else
     start= mid + 1;
  }
  if (start != mid)
  {
    val= uint2korr(header + start * entry_size);
    flag= CMP_NUM(num, val);
  }
  *entry= header + start * entry_size;
  if (flag > 0)
    *entry+= entry_size;        /* Point at next bigger key */
  return flag == 0;
}


/*
  Description of plan of adding/removing/updating a packed string
*/

typedef enum {PLAN_REPLACE, PLAN_ADD, PLAN_DELETE, PLAN_NOP} PLAN_ACT;

struct st_plan {
  DYNAMIC_COLUMN_VALUE *val;
  uint *num;
  uchar *place;
  size_t length;
  int hdelta, ddelta;
  PLAN_ACT act;
};
typedef struct st_plan PLAN;


static int plan_sort(const void *a, const void *b)
{
  return ((PLAN *)a)->num[0] - ((PLAN *)b)->num[0];
}

#define DELTA_CHECK(S, D, C)        \
  if ((S) == 0)                     \
    (S)= (D);                       \
  else if (((S) > 0 && (D) < 0) ||  \
            ((S) < 0 && (D) > 0))   \
  {                                 \
    (C)= TRUE;                      \
    break;                          \
  }                                 \


/**
  Update the packed string with the given columns

  @param str             String where to write the data
  @param add_column_count Number of columns in the arrays
  @param column_numbers  Array of columns numbers
  @param values          Array of columns values

  @return ER_DYNCOL_* return code
*/

enum enum_dyncol_func_result
dynamic_column_update_many(DYNAMIC_COLUMN *str,
                           uint add_column_count,
                           uint *column_numbers,
                           DYNAMIC_COLUMN_VALUE *values)
{
  PLAN *plan;
  uchar *header_end;
  long data_delta= 0;
  uint i, j, k;
  uint new_column_count, column_count, not_null;
  enum enum_dyncol_func_result rc;
  int header_delta;
  size_t offset_size, entry_size, header_size, data_size;
  size_t new_offset_size, new_entry_size, new_header_size, new_data_size;
  size_t max_offset;

  if (add_column_count == 0)
    return ER_DYNCOL_OK;

  /*
    Get columns in column order. As the data in 'str' is already
    in column order this allows to replace all columns in one loop.
  */

  if (!(plan= my_malloc(sizeof(PLAN) * (add_column_count + 1), MYF(0))))
    return ER_DYNCOL_RESOURCE;

  not_null= add_column_count;
  for (i= 0; i < add_column_count; i++)
  {
    if (column_numbers[i] > UINT_MAX16)
    {
      rc= ER_DYNCOL_DATA;
      goto end;
    }

    plan[i].val= values + i;
    plan[i].num= column_numbers + i;
    if (values[i].type == DYN_COL_NULL)
      not_null--;

  }

  if (str->length == 0)
  {
    /*
      Just add new columns. If there was no columns to add we return
      an empty string.
     */
    goto create_new_string;
  }

  /* Check that header is ok */
  if (read_fixed_header(str, &offset_size, &column_count))
  {
    rc= ER_DYNCOL_FORMAT;
    goto end;
  }
  if (column_count == 0)
    goto create_new_string;

  qsort(plan, (size_t)add_column_count, sizeof(PLAN), &plan_sort);

  new_column_count= column_count;
  calc_param(&entry_size, &header_size, offset_size, column_count);
  max_offset= str->length - (FIXED_HEADER_SIZE + header_size);
  header_end= (uchar*) str->str + FIXED_HEADER_SIZE + header_size;

  if (header_size + FIXED_HEADER_SIZE > str->length)
  {
    rc= ER_DYNCOL_FORMAT;
    goto end;
  }

  /*
    Calculate how many columns and data is added/deleted and make a 'plan'
    for each of them.
  */
  header_delta= 0;
  for (i= 0; i < add_column_count; i++)
  {
    uchar *entry;

    /*
      For now we don't allow creating two columns with the same number
      at the time of create.  This can be fixed later to just use the later
      by comparing the pointers.
    */
    if (i < add_column_count - 1 && plan[i].num[0] == plan[i + 1].num[0])
    {
      rc= ER_DYNCOL_DATA;
      goto end;
    }

    /* Set common variables for all plans */
    plan[i].ddelta= data_delta;
    /* get header delta in entries */
    plan[i].hdelta= header_delta;
    plan[i].length= 0;                          /* Length if NULL */

    if (find_place(plan[i].num[0],
                   (uchar *)str->str + FIXED_HEADER_SIZE,
                   entry_size, column_count, &entry))
    {
      size_t entry_data_size;

      /* Data existed; We have to replace or delete it */

      entry_data_size= get_length(entry, header_end,
                                  offset_size, max_offset);
      if ((long) entry_data_size < 0)
      {
        rc= ER_DYNCOL_FORMAT;
        goto end;
      }

      if (plan[i].val->type == DYN_COL_NULL)
      {
        /* Inserting a NULL means delete the old data */

        plan[i].act= PLAN_DELETE;	        /* Remove old value */
        header_delta--;                         /* One row less in header */
        data_delta-= entry_data_size;           /* Less data to store */
      }
      else
      {
        /* Replace the value */

        plan[i].act= PLAN_REPLACE;
        /* get data delta in bytes */
        if ((plan[i].length= dynamic_column_value_len(plan[i].val)) ==
            (size_t) ~0)
        {
          rc= ER_DYNCOL_DATA;
          goto end;
        }
        data_delta+= plan[i].length - entry_data_size;
      }
    }
    else
    {
      /* Data did not exists. Add if it it's not NULL */

      if (plan[i].val->type == DYN_COL_NULL)
      {
        plan[i].act= PLAN_NOP;                  /* Mark entry to be skiped */
      }
      else
      {
        /* Add new value */

        plan[i].act= PLAN_ADD;
        header_delta++;                         /* One more row in header */
        /* get data delta in bytes */
        if ((plan[i].length= dynamic_column_value_len(plan[i].val)) ==
            (size_t) ~0)
        {
          rc= ER_DYNCOL_DATA;
          goto end;
        }
        data_delta+= plan[i].length;
      }
    }
    plan[i].place= entry;
  }
  plan[add_column_count].hdelta= header_delta;
  plan[add_column_count].ddelta= data_delta;
  new_column_count= column_count + header_delta;

  /*
    Check if it is only "increasing" or only "decreasing" plan for (header
    and data separately).
  */
  data_size= str->length - header_size - FIXED_HEADER_SIZE;
  new_data_size= data_size + data_delta;
  if ((new_offset_size= dynamic_column_offset_bytes(new_data_size)) >=
      MAX_OFFSET_LENGTH)
  {
    rc= ER_DYNCOL_LIMIT;
    goto end;
  }

#ifdef NOT_IMPLEMENTED
  /* if (new_offset_size != offset_size) then we have to rewrite header */
  header_delta_sign= new_offset_size - offset_size;
  data_delta_sign= 0;
  for (i= 0; i < add_column_count; i++)
  {
    /* This is the check for increasing/decreasing */
    DELTA_CHECK(header_delta_sign, plan[i].hdelta, copy);
    DELTA_CHECK(data_delta_sign, plan[i].ddelta, copy);
  }
#endif
  calc_param(&new_entry_size, &new_header_size,
             new_offset_size, new_column_count);

  /*
    The following code always make a copy. In future we can do a more
    optimized version when data is only increasing / decreasing.
  */

  /*if (copy) */
  {
    DYNAMIC_COLUMN tmp;
    uchar *header_base= (uchar *)str->str + FIXED_HEADER_SIZE,
          *write;
    if (dynamic_column_init_str(&tmp,
                                (FIXED_HEADER_SIZE + new_header_size +
                                 new_data_size + DYNCOL_SYZERESERVE)))
    {
      rc= ER_DYNCOL_RESOURCE;
      goto end;
    }
    write= (uchar *)tmp.str + FIXED_HEADER_SIZE;
    /* Adjust tmp to contain whole the future header */
    tmp.length= FIXED_HEADER_SIZE + new_header_size;
    set_fixed_header(&tmp, new_offset_size, new_column_count);
    data_delta= 0;

    /*
      Copy data to the new string
      i= index in array of changes
      j= index in packed string header index
    */

    for (i= 0, j= 0; i < add_column_count || j < column_count; i++)
    {
      size_t first_offset;
      uint start= j, end;
      LINT_INIT(first_offset);

      /*
        Search in i and j for the next column to add from i and where to
        add.
      */

      while (i < add_column_count && plan[i].act == PLAN_NOP)
        i++;                                    /* skip NOP */
      if (i == add_column_count)
        j= end= column_count;
      else
      {
        /*
          old data portion. We don't need to check that j < column_count
          as plan[i].place is guaranteed to have a pointer inside the
          data.
        */
        while (header_base + j * entry_size < plan[i].place)
          j++;
        end= j;
        if ((plan[i].act == PLAN_REPLACE || plan[i].act == PLAN_DELETE))
          j++;                              /* data at 'j' will be removed */
      }

      if (plan[i].ddelta == 0 && offset_size == new_offset_size)
      {
        uchar *read= header_base + start * entry_size;
        DYNAMIC_COLUMN_TYPE tp;
        /*
          It's safe to copy the header unchanged. This is usually the
          case for the first header block before any changed data.
        */
        if (start < end)                        /* Avoid memcpy with 0 */
        {
          size_t length= entry_size * (end - start);
          memcpy(write, read, length);
          write+= length;
        }
        /* Read first_offset */
        type_and_offset_read(&tp, &first_offset, read, offset_size);
      }
      else
      {
        /*
          Adjust all headers since last loop.
          We have to do this as the offset for data has moved
        */
        for (k= start; k < end; k++)
        {
          uchar *read= header_base + k * entry_size;
          size_t offs;
          uint nm;
          DYNAMIC_COLUMN_TYPE tp;

          nm= uint2korr(read);                    /* Column nummber */
          type_and_offset_read(&tp, &offs, read, offset_size);
          if (k == start)
            first_offset= offs;
          else if (offs < first_offset)
          {
            dynamic_column_column_free(&tmp);
            rc= ER_DYNCOL_FORMAT;
            goto end;
          }

          offs+= plan[i].ddelta;
          int2store(write, nm);
          /* write rest of data at write + COLUMN_NUMBER_SIZE */
          type_and_offset_store(write, new_offset_size, tp, offs);
          write+= new_entry_size;
        }
      }

      /* copy first the data that was not replaced in original packed data */
      if (start < end)
      {
        /* Add old data last in 'tmp' */
        size_t data_size=
          get_length_interval(header_base + start * entry_size,
                              header_base + end * entry_size,
                              header_end, offset_size, max_offset);
        if ((long) data_size < 0 ||
            data_size > max_offset - first_offset)
        {
          dynamic_column_column_free(&tmp);
          rc= ER_DYNCOL_FORMAT;
          goto end;
        }

        memcpy(tmp.str + tmp.length, (char *)header_end + first_offset,
               data_size);
        tmp.length+= data_size;
      }

      /* new data adding */
      if (i < add_column_count)
      {
        if( plan[i].act == PLAN_ADD || plan[i].act == PLAN_REPLACE)
        {
          int2store(write, plan[i].num[0]);
          type_and_offset_store(write, new_offset_size,
                                plan[i].val[0].type,
                                tmp.length -
                                (FIXED_HEADER_SIZE + new_header_size));
          write+= new_entry_size;
          data_store(&tmp, plan[i].val);        /* Append new data */
        }
        data_delta= plan[i].ddelta;
      }
    }
    dynamic_column_column_free(str);
    *str= tmp;
  }

  rc= ER_DYNCOL_OK;

end:
  my_free(plan);
  return rc;

create_new_string:
  /* There is no columns from before, so let's just add the new ones */
  rc= ER_DYNCOL_OK;
  if (not_null != 0)
    rc= dynamic_column_create_many_internal(str, add_column_count,
                                            column_numbers, values,
                                            str->str == NULL);
  goto end;
}


/**
  Update the packed string with the given column

  @param str             String where to write the data
  @param column_number   Array of columns number
  @param values          Array of columns values

  @return ER_DYNCOL_* return code
*/


int dynamic_column_update(DYNAMIC_COLUMN *str, uint column_nr,
                          DYNAMIC_COLUMN_VALUE *value)
{
  return dynamic_column_update_many(str, 1, &column_nr, value);
}