Debian Codegraph @

storage/innobase/row/row0row.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1996, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file row/row0row.cc
29 General row routines
30
31 Created 4/20/1996 Heikki Tuuri
32 *******************************************************/
33
34 #include "row0row.h"
35
36 #include <sys/types.h>
37
38 #include "btr0btr.h"
39 #include "data0type.h"
40 #include "dict0boot.h"
41 #include "dict0dict.h"
42 #include "ha_prototypes.h"
43 #include "lob0lob.h"
44 #include "mach0data.h"
45 #include "que0que.h"
46 #include "read0read.h"
47 #include "rem0cmp.h"
48 #include "row0ext.h"
49 #include "row0mysql.h"
50 #include "row0upd.h"
51 #include "trx0purge.h"
52 #include "trx0rec.h"
53 #include "trx0roll.h"
54 #include "trx0rseg.h"
55 #include "trx0trx.h"
56 #include "trx0undo.h"
57 #include "ut0mem.h"
58
59 dtuple_t *row_build_index_entry_low(const dtuple_t *row, const row_ext_t *ext,
60 const dict_index_t *index, mem_heap_t *heap,
61 ulint flag) {
62 dtuple_t *entry;
63 ulint entry_len;
64 ulint i;
65 ulint num_v = 0;
66
67 entry_len = dict_index_get_n_fields(index);
68
69 if (flag == ROW_BUILD_FOR_INSERT && index->is_clustered()) {
70 num_v = dict_table_get_n_v_cols(index->table);
71 entry_len = entry_len - index->table->get_n_instant_drop_cols();
72 entry = dtuple_create_with_vcol(heap, entry_len, num_v);
73 } else {
74 entry = dtuple_create(heap, entry_len);
75 }
76
77 if (index->is_corrupted() && dict_index_has_virtual(index) &&
78 dict_index_get_online_status(index) == ONLINE_INDEX_ABORTED_DROPPED) {
79 /* See comments for virtual index in ddl::drop_indexes().
80 In this case, just return the empty entry object */
81 return (entry);
82 }
83
84 if (dict_index_is_ibuf(index)) {
85 dtuple_set_n_fields_cmp(entry, entry_len);
86 /* There may only be externally stored columns
87 in a clustered index B-tree of a user table. */
88 ut_a(!ext);
89 } else {
90 dtuple_set_n_fields_cmp(entry, dict_index_get_n_unique_in_tree(index));
91 }
92
93 for (i = 0; i < entry_len + num_v; i++) {
94 const dict_field_t *ind_field = nullptr;
95 const dict_col_t *col;
96 ulint col_no = 0;
97 dfield_t *dfield;
98 dfield_t *dfield2;
99 ulint len;
100
101 if (i >= entry_len) {
102 /* This is to insert new rows to cluster index */
103 ut_ad(index->is_clustered() && flag == ROW_BUILD_FOR_INSERT);
104 dfield = dtuple_get_nth_v_field(entry, i - entry_len);
105 col = &dict_table_get_nth_v_col(index->table, i - entry_len)->m_col;
106
107 } else {
108 ind_field = index->get_field(i);
109 col = ind_field->col;
110 ut_ad(!col->is_instant_dropped());
111 col_no = dict_col_get_no(col);
112 dfield = dtuple_get_nth_field(entry, i);
113 }
114 static_assert(DATA_MISSING == 0, "DATA_MISSING != 0");
115
116 if (col->is_virtual()) {
117 const dict_v_col_t *v_col = reinterpret_cast<const dict_v_col_t *>(col);
118
119 ut_ad(v_col->v_pos < dtuple_get_n_v_fields(row));
120 dfield2 = dtuple_get_nth_v_field(row, v_col->v_pos);
121
122 ut_ad(dfield_is_null(dfield2) || dfield2->len == UNIV_NO_INDEX_VALUE ||
123 dfield2->data);
124 } else {
125 dfield2 = dtuple_get_nth_field(row, col_no);
126 ut_ad(dfield_get_type(dfield2)->mtype == DATA_MISSING ||
127 (!(dfield_get_type(dfield2)->prtype & DATA_VIRTUAL)));
128 }
129
130 if (UNIV_UNLIKELY(dfield_get_type(dfield2)->mtype == DATA_MISSING)) {
131 /* The field has not been initialized in the row.
132 This should be from trx_undo_rec_get_partial_row(). */
133 return (nullptr);
134 }
135
136 #ifdef UNIV_DEBUG
137 if (dfield_get_type(dfield2)->prtype & DATA_VIRTUAL &&
138 index->is_clustered()) {
139 ut_ad(flag == ROW_BUILD_FOR_INSERT);
140 }
141 #endif /* UNIV_DEBUG */
142
143 /* Special handle spatial index, set the first field
144 which is for store MBR. */
145 if (dict_index_is_spatial(index) && i == 0) {
146 double *mbr;
147
148 dfield_copy(dfield, dfield2);
149 dfield->type.prtype |= DATA_GIS_MBR;
150
151 /* Allocate memory for mbr field */
152 ulint mbr_len = DATA_MBR_LEN;
153 mbr = static_cast<double *>(mem_heap_alloc(heap, mbr_len));
154
155 /* Set mbr field data. */
156 dfield_set_data(dfield, mbr, mbr_len);
157
158 if (dfield2->data) {
159 uchar *dptr = nullptr;
160 ulint dlen = 0;
161 ulint flen = 0;
162 double tmp_mbr[SPDIMS * 2];
163 mem_heap_t *temp_heap = nullptr;
164
165 if (dfield_is_ext(dfield2)) {
166 if (flag == ROW_BUILD_FOR_PURGE) {
167 byte *ptr = nullptr;
168
169 spatial_status_t spatial_status;
170 spatial_status = dfield_get_spatial_status(dfield2);
171
172 switch (spatial_status) {
173 case SPATIAL_ONLY:
174 ptr = static_cast<byte *>(dfield_get_data(dfield2));
175 ut_ad(dfield_get_len(dfield2) == DATA_MBR_LEN);
176 break;
177
178 case SPATIAL_MIXED:
179 ptr = static_cast<byte *>(dfield_get_data(dfield2)) +
180 dfield_get_len(dfield2);
181 break;
182
183 case SPATIAL_NONE:
184 /* Undo record is logged before
185 spatial index is created.*/
186 return (nullptr);
187
188 case SPATIAL_UNKNOWN:
189 ut_d(ut_error);
190 }
191
192 memcpy(mbr, ptr, DATA_MBR_LEN);
193 continue;
194 }
195
196 if (flag == ROW_BUILD_FOR_UNDO &&
197 dict_table_has_atomic_blobs(index->table)) {
198 /* For build entry for undo, and
199 the table is Barrcuda, we need
200 to skip the prefix data. */
201 flen = BTR_EXTERN_FIELD_REF_SIZE;
202 ut_ad(dfield_get_len(dfield2) >= BTR_EXTERN_FIELD_REF_SIZE);
203 dptr = static_cast<byte *>(dfield_get_data(dfield2)) +
204 dfield_get_len(dfield2) - BTR_EXTERN_FIELD_REF_SIZE;
205 } else {
206 flen = dfield_get_len(dfield2);
207 dptr = static_cast<byte *>(dfield_get_data(dfield2));
208 }
209
210 temp_heap = mem_heap_create(100, UT_LOCATION_HERE);
211
212 const page_size_t page_size =
213 (ext != nullptr) ? ext->page_size
214 : dict_table_page_size(index->table);
215
216 const dict_index_t *clust_index =
217 (ext == nullptr ? index->table->first_index() : ext->index);
218
219 dptr = lob::btr_copy_externally_stored_field(
220 nullptr, clust_index, &dlen, nullptr, dptr, page_size, flen,
221 false, temp_heap);
222 } else {
223 dptr = static_cast<uchar *>(dfield_get_data(dfield2));
224 dlen = dfield_get_len(dfield2);
225 }
226
227 if (dlen <= GEO_DATA_HEADER_SIZE) {
228 for (uint i = 0; i < SPDIMS; ++i) {
229 tmp_mbr[i * 2] = DBL_MAX;
230 tmp_mbr[i * 2 + 1] = -DBL_MAX;
231 }
232 } else {
233 get_mbr_from_store(index->rtr_srs.get(), dptr,
234 static_cast<uint>(dlen), SPDIMS, tmp_mbr, nullptr);
235 }
236 dfield_write_mbr(dfield, tmp_mbr);
237 if (temp_heap) {
238 mem_heap_free(temp_heap);
239 }
240 }
241 continue;
242 }
243
244 len = dfield_get_len(dfield2);
245
246 dfield_copy(dfield, dfield2);
247
248 if (!index->is_clustered()) {
249 /* Fields based on virtual columns in secondary indexes are
250 not themselves virtual */
251 dfield->type.prtype &= ~DATA_VIRTUAL;
252 }
253
254 if (dfield_is_null(dfield)) {
255 continue;
256 }
257
258 if ((!ind_field || ind_field->prefix_len == 0) &&
259 (!dfield_is_ext(dfield) || index->is_clustered())) {
260 /* The dfield_copy() above suffices for
261 columns that are stored in-page, or for
262 clustered index record columns that are not
263 part of a column prefix in the PRIMARY KEY,
264 or for virtual columns in cluster index record. */
265 continue;
266 }
267
268 /* If the column is stored externally (off-page) in
269 the clustered index, it must be an ordering field in
270 the secondary index. If !atomic_blobs, the only way
271 we may have a secondary index pointing to a clustered
272 index record with an off-page column is when it is a
273 column prefix index. If atomic_blobs, also fully
274 indexed long columns may be stored off-page. */
275 ut_ad(col->ord_part);
276
277 if (ext && !col->is_virtual()) {
278 /* See if the column is stored externally. */
279 const byte *buf = row_ext_lookup(ext, col_no, &len);
280 if (UNIV_LIKELY_NULL(buf)) {
281 if (UNIV_UNLIKELY(buf == field_ref_zero)) {
282 return (nullptr);
283 }
284 dfield_set_data(dfield, buf, len);
285 }
286
287 if (ind_field->prefix_len == 0) {
288 /* If ROW_FORMAT=DYNAMIC or
289 ROW_FORMAT=COMPRESSED, we can have a
290 secondary index on an entire column
291 that is stored off-page in the
292 clustered index. As this is not a
293 prefix index (prefix_len == 0),
294 include the entire off-page column in
295 the secondary index record. */
296 continue;
297 }
298 } else if (dfield_is_ext(dfield)) {
299 /* This table is either in
300 (ROW_FORMAT=REDUNDANT or ROW_FORMAT=COMPACT)
301 or a purge record where the ordered part of
302 the field is not external.
303 In ROW_FORMAT=REDUNDANT and ROW_FORMAT=COMPACT,
304 the maximum column prefix
305 index length is 767 bytes, and the clustered
306 index record contains a 768-byte prefix of
307 each off-page column. */
308 ut_a(len >= BTR_EXTERN_FIELD_REF_SIZE);
309 len -= BTR_EXTERN_FIELD_REF_SIZE;
310 dfield_set_len(dfield, len);
311 }
312
313 /* If a column prefix index, take only the prefix. */
314 if (ind_field->prefix_len) {
315 len = dtype_get_at_most_n_mbchars(
316 col->prtype, col->mbminmaxlen, ind_field->prefix_len, len,
317 static_cast<char *>(dfield_get_data(dfield)));
318 dfield_set_len(dfield, len);
319 }
320 }
321
322 return (entry);
323 }
324
325 /** An inverse function to row_build_index_entry. Builds a row from a
326 record in a clustered index, with possible indexing on ongoing
327 addition of new virtual columns.
328 @param[in] type ROW_COPY_POINTERS or ROW_COPY_DATA;
329 @param[in] index clustered index
330 @param[in] rec record in the clustered index
331 @param[in] offsets rec_get_offsets(rec,index) or NULL
332 @param[in] col_table table, to check which
333 externally stored columns
334 occur in the ordering columns
335 of an index, or NULL if
336 index->table should be
337 consulted instead
338 @param[in] add_cols default values of added columns, or NULL
339 @param[in] add_v new virtual columns added
340 along with new indexes
341 @param[in] col_map mapping of old column
342 numbers to new ones, or NULL
343 @param[in] ext cache of externally stored column
344 prefixes, or NULL
345 @param[in] heap memory heap from which
346 the memory needed is allocated
347 @return own: row built; */
348 static inline dtuple_t *row_build_low(ulint type, const dict_index_t *index,
349 const rec_t *rec, const ulint *offsets,
350 const dict_table_t *col_table,
351 const dtuple_t *add_cols,
352 const dict_add_v_col_t *add_v,
353 const ulint *col_map, row_ext_t **ext,
354 mem_heap_t *heap) {
355 const byte *copy;
356 dtuple_t *row;
357 ulint n_ext_cols;
358 ulint *ext_cols = nullptr; /* remove warning */
359 ulint len;
360 byte *buf;
361 ulint j;
362 mem_heap_t *tmp_heap = nullptr;
363 ulint offsets_[REC_OFFS_NORMAL_SIZE];
364 rec_offs_init(offsets_);
365
366 ut_ad(index != nullptr);
367 ut_ad(rec != nullptr);
368 ut_ad(heap != nullptr);
369 ut_ad(index->is_clustered());
370 ut_ad(!trx_sys_mutex_own());
371 ut_ad(!col_map || col_table);
372
373 if (!offsets) {
374 offsets = rec_get_offsets(rec, index, offsets_, ULINT_UNDEFINED,
375 UT_LOCATION_HERE, &tmp_heap);
376 } else {
377 ut_ad(rec_offs_validate(rec, index, offsets));
378 }
379
380 #if defined UNIV_DEBUG || defined UNIV_BLOB_LIGHT_DEBUG
381 /* Some blob refs can be NULL during crash recovery before
382 trx_rollback_active() has completed execution, or when a concurrently
383 executing insert or update has committed the B-tree mini-transaction
384 but has not yet managed to restore the cursor position for writing
385 the big_rec. Note that the mini-transaction can be committed multiple
386 times, and the cursor restore can happen multiple times for single
387 insert or update statement. */
388 ut_a(!rec_offs_any_null_extern(index, rec, offsets) ||
389 trx_rw_is_active(row_get_rec_trx_id(rec, index, offsets), false));
390 #endif /* UNIV_DEBUG || UNIV_BLOB_LIGHT_DEBUG */
391
392 if (type != ROW_COPY_POINTERS) {
393 /* Take a copy of rec to heap */
394 buf = static_cast<byte *>(mem_heap_alloc(heap, rec_offs_size(offsets)));
395
396 copy = rec_copy(buf, rec, offsets);
397 } else {
398 copy = rec;
399 }
400
401 n_ext_cols = rec_offs_n_extern(index, offsets);
402 if (n_ext_cols) {
403 ext_cols = static_cast<ulint *>(
404 mem_heap_alloc(heap, n_ext_cols * sizeof *ext_cols));
405 }
406
407 /* Avoid a debug assertion in rec_offs_validate(). */
408 rec_offs_make_valid(copy, index, const_cast<ulint *>(offsets));
409
410 if (!col_table) {
411 ut_ad(!col_map);
412 ut_ad(!add_cols);
413 col_table = index->table;
414 }
415
416 if (add_cols) {
417 ut_ad(col_map);
418 row = dtuple_copy(add_cols, heap);
419 /* dict_table_copy_types() would set the fields to NULL */
420 for (ulint i = 0; i < col_table->get_n_cols(); i++) {
421 col_table->get_col(i)->copy_type(
422 dfield_get_type(dtuple_get_nth_field(row, i)));
423 }
424 } else if (add_v != nullptr) {
425 row = dtuple_create_with_vcol(
426 heap, col_table->get_n_cols(),
427 dict_table_get_n_v_cols(col_table) + add_v->n_v_col);
428 dict_table_copy_types(row, col_table);
429
430 for (ulint i = 0; i < add_v->n_v_col; i++) {
431 add_v->v_col[i].m_col.copy_type(
432 dfield_get_type(dtuple_get_nth_v_field(row, i + col_table->n_v_def)));
433 }
434 } else {
435 row = dtuple_create_with_vcol(heap, col_table->get_n_cols(),
436 dict_table_get_n_v_cols(col_table));
437 dict_table_copy_types(row, col_table);
438 }
439
440 dtuple_set_info_bits(row, rec_get_info_bits(copy, rec_offs_comp(offsets)));
441
442 j = 0;
443
444 for (ulint i = 0; i < rec_offs_n_fields(offsets); i++) {
445 const dict_field_t *ind_field = index->get_field(i);
446
447 if (ind_field->prefix_len) {
448 /* Column prefixes can only occur in key
449 fields, which cannot be stored externally. For
450 a column prefix, there should also be the full
451 field in the clustered index tuple. The row
452 tuple comprises full fields, not prefixes. */
453 ut_ad(!rec_offs_nth_extern(index, offsets, i));
454 continue;
455 }
456
457 const dict_col_t *col = ind_field->col;
458 /* If this column has been dropped instantly, skip it. */
459 if (col->is_instant_dropped()) {
460 continue;
461 }
462
463 ulint col_no = dict_col_get_no(col);
464
465 if (col_map) {
466 col_no = col_map[col_no];
467
468 if (col_no == ULINT_UNDEFINED) {
469 /* dropped column */
470 continue;
471 }
472 }
473
474 dfield_t *dfield = dtuple_get_nth_field(row, col_no);
475
476 const byte *field;
477
478 field = rec_get_nth_field_instant(copy, offsets, i, index, &len);
479
480 dfield_set_data(dfield, field, len);
481
482 if (rec_offs_nth_extern(index, offsets, i)) {
483 dfield_set_ext(dfield);
484
485 col = col_table->get_col(col_no);
486
487 if (col->ord_part) {
488 /* We will have to fetch prefixes of
489 externally stored columns that are
490 referenced by column prefixes. */
491 ext_cols[j++] = col_no;
492 }
493 }
494 }
495
496 rec_offs_make_valid(rec, index, const_cast<ulint *>(offsets));
497
498 ut_ad(dtuple_check_typed(row));
499
500 if (!ext) {
501 /* REDUNDANT and COMPACT formats store a local
502 768-byte prefix of each externally stored
503 column. No cache is needed.
504
505 During online table rebuild,
506 row_log_table_apply_delete_low()
507 may use a cache that was set up by
508 row_log_table_delete(). */
509
510 } else if (j) {
511 *ext = row_ext_create(index, j, ext_cols, index->table->flags, row,
512 dict_index_is_sdi(index), heap);
513 } else {
514 *ext = nullptr;
515 }
516
517 if (tmp_heap) {
518 mem_heap_free(tmp_heap);
519 }
520
521 return (row);
522 }
523
524 dtuple_t *row_build(ulint type, const dict_index_t *index, const rec_t *rec,
525 const ulint *offsets, const dict_table_t *col_table,
526 const dtuple_t *add_cols, const ulint *col_map,
527 row_ext_t **ext, mem_heap_t *heap) {
528 return (row_build_low(type, index, rec, offsets, col_table, add_cols, nullptr,
529 col_map, ext, heap));
530 }
531
532 /** An inverse function to row_build_index_entry. Builds a row from a
533 record in a clustered index, with possible indexing on ongoing
534 addition of new virtual columns.
535 @param[in] type ROW_COPY_POINTERS or ROW_COPY_DATA;
536 @param[in] index clustered index
537 @param[in] rec record in the clustered index
538 @param[in] offsets rec_get_offsets(rec,index) or NULL
539 @param[in] col_table table, to check which
540 externally stored columns
541 occur in the ordering columns
542 of an index, or NULL if
543 index->table should be
544 consulted instead
545 @param[in] add_cols default values of added columns, or NULL
546 @param[in] add_v new virtual columns added
547 along with new indexes
548 @param[in] col_map mapping of old column
549 numbers to new ones, or NULL
550 @param[in] ext cache of externally stored column
551 prefixes, or NULL
552 @param[in] heap memory heap from which
553 the memory needed is allocated
554 @return own: row built */
555 dtuple_t *row_build_w_add_vcol(ulint type, const dict_index_t *index,
556 const rec_t *rec, const ulint *offsets,
557 const dict_table_t *col_table,
558 const dtuple_t *add_cols,
559 const dict_add_v_col_t *add_v,
560 const ulint *col_map, row_ext_t **ext,
561 mem_heap_t *heap) {
562 return (row_build_low(type, index, rec, offsets, col_table, add_cols, add_v,
563 col_map, ext, heap));
564 }
565
566 /** Converts an index record to a typed data tuple.
567 @return index entry built; does not set info_bits, and the data fields
568 in the entry will point directly to rec */
569 dtuple_t *row_rec_to_index_entry_low(
570 const rec_t *rec, /*!< in: record in the index */
571 const dict_index_t *index, /*!< in: index */
572 const ulint *offsets, /*!< in: rec_get_offsets(rec, index) */
573 mem_heap_t *heap) /*!< in: memory heap from which
574 the memory needed is allocated */
575 {
576 dtuple_t *entry;
577 dfield_t *dfield;
578 ulint i;
579 const byte *field;
580 ulint len;
581 ulint rec_len;
582
583 ut_ad(rec != nullptr);
584 ut_ad(heap != nullptr);
585 ut_ad(index != nullptr);
586
587 /* Because this function may be invoked by row0ddl.cc
588 on a record whose header is in different format, the check
589 rec_offs_validate(rec, index, offsets) must be avoided here. */
590
591 rec_len = rec_offs_n_fields(offsets);
592
593 entry = dtuple_create(heap, rec_len);
594
595 dtuple_set_n_fields_cmp(entry, dict_index_get_n_unique_in_tree(index));
596
597 ut_ad(rec_len == dict_index_get_n_fields(index) ||
598 /* non-leaf record which has keys and child page no as record data */
599 rec_len == dict_index_get_n_unique(index) + 1U
600 /* a record for older SYS_INDEXES table
601 (missing merge_threshold column) is acceptable. */
602 || (index->table->id == DICT_INDEXES_ID &&
603 rec_len == dict_index_get_n_fields(index) - 1U));
604
605 dict_index_copy_types(entry, index, rec_len);
606
607 for (i = 0; i < rec_len; i++) {
608 dfield = dtuple_get_nth_field(entry, i);
609
610 field = rec_get_nth_field_instant(rec, offsets, i, index, &len);
611
612 dfield_set_data(dfield, field, len);
613
614 if (rec_offs_nth_extern(index, offsets, i)) {
615 dfield_set_ext(dfield);
616 }
617 }
618
619 ut_ad(dtuple_check_typed(entry));
620 ut_d(entry->validate_for_index(index));
621
622 return (entry);
623 }
624
625 /** Converts an index record to a typed data tuple. NOTE that externally
626 stored (often big) fields are NOT copied to heap.
627 @return own: index entry built */
628 dtuple_t *row_rec_to_index_entry(
629 const rec_t *rec, /*!< in: record in the index */
630 const dict_index_t *index, /*!< in: index */
631 const ulint *offsets, /*!< in: rec_get_offsets(rec) */
632 mem_heap_t *heap) /*!< in: memory heap from which
633 the memory needed is allocated */
634 {
635 dtuple_t *entry;
636 byte *buf;
637 const rec_t *copy_rec;
638 DBUG_TRACE;
639
640 ut_ad(rec != nullptr);
641 ut_ad(heap != nullptr);
642 ut_ad(index != nullptr);
643 ut_ad(rec_offs_validate(rec, index, offsets));
644
645 /* Take a copy of rec to heap */
646 buf = static_cast<byte *>(mem_heap_alloc(heap, rec_offs_size(offsets)));
647
648 copy_rec = rec_copy(buf, rec, offsets);
649
650 rec_offs_make_valid(copy_rec, index, const_cast<ulint *>(offsets));
651 entry = row_rec_to_index_entry_low(copy_rec, index, offsets, heap);
652 rec_offs_make_valid(rec, index, const_cast<ulint *>(offsets));
653
654 dtuple_set_info_bits(entry, rec_get_info_bits(rec, rec_offs_comp(offsets)));
655
656 return entry;
657 }
658
659 /** Builds from a secondary index record a row reference with which we can
660 search the clustered index record.
661 @return own: row reference built; see the NOTE below! */
662 dtuple_t *row_build_row_ref(
663 ulint type, /*!< in: ROW_COPY_DATA, or ROW_COPY_POINTERS:
664 the former copies also the data fields to
665 heap, whereas the latter only places pointers
666 to data fields on the index page */
667 const dict_index_t *index, /*!< in: secondary index */
668 const rec_t *rec, /*!< in: record in the index;
669 NOTE: in the case ROW_COPY_POINTERS
670 the data fields in the row will point
671 directly into this record, therefore,
672 the buffer page of this record must be
673 at least s-latched and the latch held
674 as long as the row reference is used! */
675 mem_heap_t *heap) /*!< in: memory heap from which the memory
676 needed is allocated */
677 {
678 dict_table_t *table;
679 dict_index_t *clust_index;
680 dfield_t *dfield;
681 dtuple_t *ref;
682 const byte *field;
683 ulint len;
684 ulint ref_len;
685 ulint pos;
686 byte *buf;
687 ulint clust_col_prefix_len;
688 ulint i;
689 mem_heap_t *tmp_heap = nullptr;
690 ulint offsets_[REC_OFFS_NORMAL_SIZE];
691 ulint *offsets = offsets_;
692 rec_offs_init(offsets_);
693
694 ut_ad(index != nullptr);
695 ut_ad(rec != nullptr);
696 ut_ad(heap != nullptr);
697 ut_ad(!index->is_clustered());
698
699 offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
700 UT_LOCATION_HERE, &tmp_heap);
701 /* Secondary indexes must not contain externally stored columns. */
702 ut_ad(!rec_offs_any_extern(offsets));
703
704 if (type == ROW_COPY_DATA) {
705 /* Take a copy of rec to heap */
706
707 buf = static_cast<byte *>(mem_heap_alloc(heap, rec_offs_size(offsets)));
708
709 rec = rec_copy(buf, rec, offsets);
710 /* Avoid a debug assertion in rec_offs_validate(). */
711 rec_offs_make_valid(rec, index, offsets);
712 }
713
714 table = index->table;
715
716 clust_index = table->first_index();
717
718 ref_len = dict_index_get_n_unique(clust_index);
719
720 ref = dtuple_create(heap, ref_len);
721
722 dict_index_copy_types(ref, clust_index, ref_len);
723
724 for (i = 0; i < ref_len; i++) {
725 dfield = dtuple_get_nth_field(ref, i);
726
727 pos = dict_index_get_nth_field_pos(index, clust_index, i);
728
729 ut_a(pos != ULINT_UNDEFINED);
730
731 field = rec_get_nth_field(index, rec, offsets, pos, &len);
732
733 dfield_set_data(dfield, field, len);
734
735 /* If the primary key contains a column prefix, then the
736 secondary index may contain a longer prefix of the same
737 column, or the full column, and we must adjust the length
738 accordingly. */
739
740 clust_col_prefix_len = clust_index->get_field(i)->prefix_len;
741
742 if (clust_col_prefix_len > 0) {
743 if (len != UNIV_SQL_NULL) {
744 const dtype_t *dtype = dfield_get_type(dfield);
745
746 dfield_set_len(dfield, dtype_get_at_most_n_mbchars(
747 dtype->prtype, dtype->mbminmaxlen,
748 clust_col_prefix_len, len, (char *)field));
749 }
750 }
751 }
752
753 ut_ad(dtuple_check_typed(ref));
754 if (tmp_heap) {
755 mem_heap_free(tmp_heap);
756 }
757
758 return (ref);
759 }
760
761 void row_build_row_ref_in_tuple(dtuple_t *ref, const rec_t *rec,
762 const dict_index_t *index, ulint *offsets) {
763 const dict_index_t *clust_index;
764 dfield_t *dfield;
765 const byte *field;
766 ulint len;
767 ulint ref_len;
768 ulint pos;
769 ulint clust_col_prefix_len;
770 ulint i;
771 mem_heap_t *heap = nullptr;
772 ulint offsets_[REC_OFFS_NORMAL_SIZE];
773 rec_offs_init(offsets_);
774
775 ut_a(ref);
776 ut_a(index);
777 ut_a(rec);
778 ut_ad(!index->is_clustered());
779 ut_a(index->table);
780
781 clust_index = index->table->first_index();
782 ut_ad(clust_index);
783
784 if (!offsets) {
785 offsets = rec_get_offsets(rec, index, offsets_, ULINT_UNDEFINED,
786 UT_LOCATION_HERE, &heap);
787 } else {
788 ut_ad(rec_offs_validate(rec, index, offsets));
789 }
790
791 /* Secondary indexes must not contain externally stored columns. */
792 ut_ad(!rec_offs_any_extern(offsets));
793 ref_len = dict_index_get_n_unique(clust_index);
794
795 ut_ad(ref_len == dtuple_get_n_fields(ref));
796
797 dict_index_copy_types(ref, clust_index, ref_len);
798
799 for (i = 0; i < ref_len; i++) {
800 dfield = dtuple_get_nth_field(ref, i);
801
802 pos = dict_index_get_nth_field_pos(index, clust_index, i);
803
804 ut_a(pos != ULINT_UNDEFINED);
805
806 field = rec_get_nth_field(index, rec, offsets, pos, &len);
807
808 dfield_set_data(dfield, field, len);
809
810 /* If the primary key contains a column prefix, then the
811 secondary index may contain a longer prefix of the same
812 column, or the full column, and we must adjust the length
813 accordingly. */
814
815 clust_col_prefix_len = clust_index->get_field(i)->prefix_len;
816
817 if (clust_col_prefix_len > 0) {
818 if (len != UNIV_SQL_NULL) {
819 const dtype_t *dtype = dfield_get_type(dfield);
820
821 dfield_set_len(dfield, dtype_get_at_most_n_mbchars(
822 dtype->prtype, dtype->mbminmaxlen,
823 clust_col_prefix_len, len, (char *)field));
824 }
825 }
826 }
827
828 ut_ad(dtuple_check_typed(ref));
829 if (UNIV_LIKELY_NULL(heap)) {
830 mem_heap_free(heap);
831 }
832 }
833
834 /** Searches the clustered index record for a row, if we have the row reference.
835 @return true if found */
836 bool row_search_on_row_ref(btr_pcur_t *pcur, /*!< out: persistent cursor, which
837 must be closed by the caller */
838 ulint mode, /*!< in: BTR_MODIFY_LEAF, ... */
839 dict_table_t *table, /*!< in: table */
840 const dtuple_t *ref, /*!< in: row reference */
841 mtr_t *mtr) /*!< in/out: mtr */
842 {
843 ulint low_match;
844 rec_t *rec;
845 dict_index_t *index;
846
847 ut_ad(dtuple_check_typed(ref));
848
849 index = table->first_index();
850
851 ut_a(dtuple_get_n_fields(ref) == dict_index_get_n_unique(index));
852
853 pcur->open(index, 0, ref, PAGE_CUR_LE, mode, mtr, UT_LOCATION_HERE);
854
855 low_match = pcur->get_low_match();
856
857 rec = pcur->get_rec();
858
859 if (page_rec_is_infimum(rec)) {
860 return false;
861 }
862
863 if (low_match != dtuple_get_n_fields(ref)) {
864 return false;
865 }
866
867 return true;
868 }
869
870 /** Fetches the clustered index record for a secondary index record. The latches
871 on the secondary index record are preserved.
872 @return record or NULL, if no record found */
873 rec_t *row_get_clust_rec(
874 ulint mode, /*!< in: BTR_MODIFY_LEAF, ... */
875 const rec_t *rec, /*!< in: record in a secondary index */
876 const dict_index_t *index, /*!< in: secondary index */
877 dict_index_t **clust_index, /*!< out: clustered index */
878 mtr_t *mtr) /*!< in: mtr */
879 {
880 mem_heap_t *heap;
881 dtuple_t *ref;
882 dict_table_t *table;
883 btr_pcur_t pcur;
884 rec_t *clust_rec;
885
886 ut_ad(!index->is_clustered());
887
888 table = index->table;
889
890 heap = mem_heap_create(256, UT_LOCATION_HERE);
891
892 ref = row_build_row_ref(ROW_COPY_POINTERS, index, rec, heap);
893
894 auto found = row_search_on_row_ref(&pcur, mode, table, ref, mtr);
895
896 clust_rec = found ? pcur.get_rec() : nullptr;
897
898 mem_heap_free(heap);
899
900 pcur.close();
901
902 *clust_index = table->first_index();
903
904 return (clust_rec);
905 }
906
907 /** Parse the integer data from specified field, which could be
908 DATA_INT, DATA_FLOAT or DATA_DOUBLE. We could return 0 if
909 1) the value is less than 0 and the type is not unsigned
910 or 2) the field is null.
911 @param[in] field field to read the int value
912 @return the integer value read from the field, 0 for negative signed
913 int or NULL field */
914 uint64_t row_parse_int_from_field(const dfield_t *field) {
915 const dtype_t *dtype = dfield_get_type(field);
916 ulint len = dfield_get_len(field);
917 const byte *data = static_cast<const byte *>(dfield_get_data(field));
918 ulint mtype = dtype_get_mtype(dtype);
919 bool unsigned_type = dtype->prtype & DATA_UNSIGNED;
920
921 if (dfield_is_null(field)) {
922 return (0);
923 } else {
924 return (row_parse_int(data, len, mtype, unsigned_type));
925 }
926 }
927
928 /** Read the autoinc counter from the clustered index row.
929 @param[in] row row to read the autoinc counter
930 @param[in] n autoinc counter is in the nth field
931 @return the autoinc counter read */
932 uint64_t row_get_autoinc_counter(const dtuple_t *row, ulint n) {
933 const dfield_t *field = dtuple_get_nth_field(row, n);
934
935 return (row_parse_int_from_field(field));
936 }
937
938 /** Searches an index record.
939 @return whether the record was found or buffered */
940 enum row_search_result row_search_index_entry(
941 dict_index_t *index, /*!< in: index */
942 const dtuple_t *entry, /*!< in: index entry */
943 ulint mode, /*!< in: BTR_MODIFY_LEAF, ... */
944 btr_pcur_t *pcur, /*!< in/out: persistent cursor, which must
945 be closed by the caller */
946 mtr_t *mtr) /*!< in: mtr */
947 {
948 ulint n_fields;
949 ulint low_match;
950 rec_t *rec;
951
952 ut_ad(dtuple_check_typed(entry));
953
954 if (dict_index_is_spatial(index)) {
955 ut_ad(mode & BTR_MODIFY_LEAF || mode & BTR_MODIFY_TREE);
956 rtr_pcur_open(index, entry, PAGE_CUR_RTREE_LOCATE, mode, pcur,
957 UT_LOCATION_HERE, mtr);
958 } else {
959 pcur->open(index, 0, entry, PAGE_CUR_LE, mode, mtr, UT_LOCATION_HERE);
960 }
961
962 switch (pcur->get_btr_cur()->flag) {
963 case BTR_CUR_UNSET:
964 ut_d(ut_error);
965 ut_o(break);
966
967 case BTR_CUR_DELETE_REF:
968 ut_a(mode & BTR_DELETE && !dict_index_is_spatial(index));
969 return (ROW_NOT_DELETED_REF);
970
971 case BTR_CUR_DEL_MARK_IBUF:
972 case BTR_CUR_DELETE_IBUF:
973 case BTR_CUR_INSERT_TO_IBUF:
974 return (ROW_BUFFERED);
975
976 case BTR_CUR_HASH:
977 case BTR_CUR_HASH_NOT_ATTEMPTED:
978 case BTR_CUR_HASH_FAIL:
979 case BTR_CUR_BINARY:
980 break;
981 }
982
983 low_match = pcur->get_low_match();
984
985 rec = pcur->get_rec();
986
987 n_fields = dtuple_get_n_fields(entry);
988
989 if (page_rec_is_infimum(rec)) {
990 return (ROW_NOT_FOUND);
991 } else if (low_match != n_fields) {
992 return (ROW_NOT_FOUND);
993 }
994
995 return (ROW_FOUND);
996 }
997
998 /** Formats the raw data in "data" (in InnoDB on-disk format) that is of
999 type DATA_INT using "prtype" and writes the result to "buf".
1000 If the data is in unknown format, then nothing is written to "buf",
1001 0 is returned and "format_in_hex" is set to true, otherwise
1002 "format_in_hex" is left untouched.
1003 Not more than "buf_size" bytes are written to "buf".
1004 The result is always '\0'-terminated (provided buf_size > 0) and the
1005 number of bytes that were written to "buf" is returned (including the
1006 terminating '\0').
1007 @return number of bytes that were written */
1008 static ulint row_raw_format_int(const char *data, /*!< in: raw data */
1009 ulint data_len, /*!< in: raw data length
1010 in bytes */
1011 ulint prtype, /*!< in: precise type */
1012 char *buf, /*!< out: output buffer */
1013 ulint buf_size, /*!< in: output buffer size
1014 in bytes */
1015 bool *format_in_hex) /*!< out: should the data
1016 be formatted in hex */
1017 {
1018 ulint ret;
1019
1020 if (data_len <= sizeof(uint64_t)) {
1021 uint64_t value;
1022 bool unsigned_type = (prtype & DATA_UNSIGNED) != 0;
1023
1024 value = mach_read_int_type((const byte *)data, data_len, unsigned_type);
1025
1026 ret =
1027 snprintf(buf, buf_size, unsigned_type ? UINT64PF : "%" PRId64, value) +
1028 1;
1029 } else {
1030 *format_in_hex = true;
1031 ret = 0;
1032 }
1033
1034 return (std::min(ret, buf_size));
1035 }
1036
1037 /** Formats the raw data in "data" (in InnoDB on-disk format) that is of
1038 type DATA_(CHAR|VARCHAR|MYSQL|VARMYSQL) using "prtype" and writes the
1039 result to "buf".
1040 If the data is in binary format, then nothing is written to "buf",
1041 0 is returned and "format_in_hex" is set to true, otherwise
1042 "format_in_hex" is left untouched.
1043 Not more than "buf_size" bytes are written to "buf".
1044 The result is always '\0'-terminated (provided buf_size > 0) and the
1045 number of bytes that were written to "buf" is returned (including the
1046 terminating '\0').
1047 @return number of bytes that were written */
1048 static ulint row_raw_format_str(const char *data, /*!< in: raw data */
1049 ulint data_len, /*!< in: raw data length
1050 in bytes */
1051 ulint prtype, /*!< in: precise type */
1052 char *buf, /*!< out: output buffer */
1053 ulint buf_size, /*!< in: output buffer size
1054 in bytes */
1055 bool *format_in_hex) /*!< out: should the data
1056 be formatted in hex */
1057 {
1058 ulint charset_coll;
1059
1060 if (buf_size == 0) {
1061 return (0);
1062 }
1063
1064 /* we assume system_charset_info is UTF-8 */
1065
1066 charset_coll = dtype_get_charset_coll(prtype);
1067
1068 if (UNIV_LIKELY(dtype_is_utf8(prtype))) {
1069 return (ut_str_sql_format(data, data_len, buf, buf_size));
1070 }
1071 /* else */
1072
1073 if (charset_coll == DATA_MYSQL_BINARY_CHARSET_COLL) {
1074 *format_in_hex = true;
1075 return (0);
1076 }
1077 /* else */
1078
1079 return (innobase_raw_format(data, data_len, charset_coll, buf, buf_size));
1080 }
1081
1082 /** Formats the raw data in "data" (in InnoDB on-disk format) using
1083 "dict_field" and writes the result to "buf".
1084 Not more than "buf_size" bytes are written to "buf".
1085 The result is always NUL-terminated (provided buf_size is positive) and the
1086 number of bytes that were written to "buf" is returned (including the
1087 terminating NUL).
1088 @return number of bytes that were written */
1089 ulint row_raw_format(const char *data, /*!< in: raw data */
1090 ulint data_len, /*!< in: raw data length
1091 in bytes */
1092 const dict_field_t *dict_field, /*!< in: index field */
1093 char *buf, /*!< out: output buffer */
1094 ulint buf_size) /*!< in: output buffer size
1095 in bytes */
1096 {
1097 ulint mtype;
1098 ulint prtype;
1099 ulint ret;
1100 bool format_in_hex;
1101
1102 if (buf_size == 0) {
1103 return (0);
1104 }
1105
1106 ut_ad(data_len != UNIV_SQL_ADD_COL_DEFAULT);
1107
1108 if (data_len == UNIV_SQL_NULL) {
1109 ret = snprintf((char *)buf, buf_size, "NULL") + 1;
1110
1111 return (std::min(ret, buf_size));
1112 }
1113
1114 mtype = dict_field->col->mtype;
1115 prtype = dict_field->col->prtype;
1116
1117 format_in_hex = false;
1118
1119 switch (mtype) {
1120 case DATA_INT:
1121
1122 ret = row_raw_format_int(data, data_len, prtype, buf, buf_size,
1123 &format_in_hex);
1124 if (format_in_hex) {
1125 goto format_in_hex;
1126 }
1127 break;
1128 case DATA_CHAR:
1129 case DATA_VARCHAR:
1130 case DATA_MYSQL:
1131 case DATA_VARMYSQL:
1132
1133 ret = row_raw_format_str(data, data_len, prtype, buf, buf_size,
1134 &format_in_hex);
1135 if (format_in_hex) {
1136 goto format_in_hex;
1137 }
1138
1139 break;
1140 /* XXX support more data types */
1141 default:
1142 format_in_hex:
1143
1144 if (UNIV_LIKELY(buf_size > 2)) {
1145 memcpy(buf, "0x", 2);
1146 buf += 2;
1147 buf_size -= 2;
1148 ret = 2 + ut_raw_to_hex(data, data_len, buf, buf_size);
1149 } else {
1150 buf[0] = '\0';
1151 ret = 1;
1152 }
1153 }
1154
1155 return (ret);
1156 }
1157
1158 dfield_t *Multi_value_entry_builder_normal::find_multi_value_field() {
1159 for (size_t i = 0; i < m_row->n_v_fields; ++i) {
1160 const auto field = &m_row->v_fields[i];
1161 if (!dfield_is_multi_value(field)) {
1162 continue;
1163 }
1164
1165 const auto vcol = dict_table_get_nth_v_col(m_index->table, i);
1166 m_mv_field_no = m_index->has_multi_value_col(vcol);
1167
1168 if (m_mv_field_no == 0) {
1169 continue;
1170 }
1171 return field;
1172 }
1173
1174 return nullptr;
1175 }
1176
1177 #ifdef UNIV_ENABLE_UNIT_TEST_ROW_RAW_FORMAT_INT
1178
1179 #ifdef HAVE_UT_CHRONO_T
1180
1181 static inline void CALL_AND_TEST(char *data, ulint data_len, ulint prtype,
1182 char *buf, ulint buf_size, ulint ret_expected,
1183 char *buf_expected,
1184 int format_in_hex_expected) {
1185 bool ok = true;
1186 memset(buf, 'x', 10);
1187 buf[10] = '\0';
1188 format_in_hex = false;
1189 fprintf(stderr, "TESTING \"\\x");
1190 for (ulint i = 0; i < data_len; i++) {
1191 fprintf(stderr, "%02hhX", data[i]);
1192 }
1193 fprintf(stderr, "\", %lu, %lu, %lu\n", data_len, prtype, buf_size);
1194 auto ret =
1195 row_raw_format_int(data, data_len, prtype, buf, buf_size, &format_in_hex);
1196 if (ret != ret_expected) {
1197 fprintf(stderr, "expected ret %lu, got %lu\n", ret_expected, ret);
1198 ok = false;
1199 }
1200 if (strcmp(buf, buf_expected) != 0) {
1201 fprintf(stderr, "expected buf \"%s\", got \"%s\"\n", buf_expected, buf);
1202 ok = false;
1203 }
1204 if (format_in_hex != format_in_hex_expected) {
1205 fprintf(stderr, "expected format_in_hex %d, got %d\n",
1206 (int)format_in_hex_expected, (int)format_in_hex);
1207 ok = false;
1208 }
1209 if (ok) {
1210 fprintf(stderr, "OK: %lu, \"%s\" %d\n\n", (ulint)ret, buf,
1211 (int)format_in_hex);
1212 }
1213 }
1214
1215 void test_row_raw_format_int() {
1216 char buf[128];
1217 bool format_in_hex;
1218
1219 /* min values for signed 1-8 byte integers */
1220
1221 CALL_AND_TEST("\x00", 1, 0, buf, sizeof(buf), 5, "-128", 0);
1222
1223 CALL_AND_TEST("\x00\x00", 2, 0, buf, sizeof(buf), 7, "-32768", 0);
1224
1225 CALL_AND_TEST("\x00\x00\x00", 3, 0, buf, sizeof(buf), 9, "-8388608", 0);
1226
1227 CALL_AND_TEST("\x00\x00\x00\x00", 4, 0, buf, sizeof(buf), 12, "-2147483648",
1228 0);
1229
1230 CALL_AND_TEST("\x00\x00\x00\x00\x00", 5, 0, buf, sizeof(buf), 14,
1231 "-549755813888", 0);
1232
1233 CALL_AND_TEST("\x00\x00\x00\x00\x00\x00", 6, 0, buf, sizeof(buf), 17,
1234 "-140737488355328", 0);
1235
1236 CALL_AND_TEST("\x00\x00\x00\x00\x00\x00\x00", 7, 0, buf, sizeof(buf), 19,
1237 "-36028797018963968", 0);
1238
1239 CALL_AND_TEST("\x00\x00\x00\x00\x00\x00\x00\x00", 8, 0, buf, sizeof(buf), 21,
1240 "-9223372036854775808", 0);
1241
1242 /* min values for unsigned 1-8 byte integers */
1243
1244 CALL_AND_TEST("\x00", 1, DATA_UNSIGNED, buf, sizeof(buf), 2, "0", 0);
1245
1246 CALL_AND_TEST("\x00\x00", 2, DATA_UNSIGNED, buf, sizeof(buf), 2, "0", 0);
1247
1248 CALL_AND_TEST("\x00\x00\x00", 3, DATA_UNSIGNED, buf, sizeof(buf), 2, "0", 0);
1249
1250 CALL_AND_TEST("\x00\x00\x00\x00", 4, DATA_UNSIGNED, buf, sizeof(buf), 2, "0",
1251 0);
1252
1253 CALL_AND_TEST("\x00\x00\x00\x00\x00", 5, DATA_UNSIGNED, buf, sizeof(buf), 2,
1254 "0", 0);
1255
1256 CALL_AND_TEST("\x00\x00\x00\x00\x00\x00", 6, DATA_UNSIGNED, buf, sizeof(buf),
1257 2, "0", 0);
1258
1259 CALL_AND_TEST("\x00\x00\x00\x00\x00\x00\x00", 7, DATA_UNSIGNED, buf,
1260 sizeof(buf), 2, "0", 0);
1261
1262 CALL_AND_TEST("\x00\x00\x00\x00\x00\x00\x00\x00", 8, DATA_UNSIGNED, buf,
1263 sizeof(buf), 2, "0", 0);
1264
1265 /* max values for signed 1-8 byte integers */
1266
1267 CALL_AND_TEST("\xFF", 1, 0, buf, sizeof(buf), 4, "127", 0);
1268
1269 CALL_AND_TEST("\xFF\xFF", 2, 0, buf, sizeof(buf), 6, "32767", 0);
1270
1271 CALL_AND_TEST("\xFF\xFF\xFF", 3, 0, buf, sizeof(buf), 8, "8388607", 0);
1272
1273 CALL_AND_TEST("\xFF\xFF\xFF\xFF", 4, 0, buf, sizeof(buf), 11, "2147483647",
1274 0);
1275
1276 CALL_AND_TEST("\xFF\xFF\xFF\xFF\xFF", 5, 0, buf, sizeof(buf), 13,
1277 "549755813887", 0);
1278
1279 CALL_AND_TEST("\xFF\xFF\xFF\xFF\xFF\xFF", 6, 0, buf, sizeof(buf), 16,
1280 "140737488355327", 0);
1281
1282 CALL_AND_TEST("\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 7, 0, buf, sizeof(buf), 18,
1283 "36028797018963967", 0);
1284
1285 CALL_AND_TEST("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 8, 0, buf, sizeof(buf), 20,
1286 "9223372036854775807", 0);
1287
1288 /* max values for unsigned 1-8 byte integers */
1289
1290 CALL_AND_TEST("\xFF", 1, DATA_UNSIGNED, buf, sizeof(buf), 4, "255", 0);
1291
1292 CALL_AND_TEST("\xFF\xFF", 2, DATA_UNSIGNED, buf, sizeof(buf), 6, "65535", 0);
1293
1294 CALL_AND_TEST("\xFF\xFF\xFF", 3, DATA_UNSIGNED, buf, sizeof(buf), 9,
1295 "16777215", 0);
1296
1297 CALL_AND_TEST("\xFF\xFF\xFF\xFF", 4, DATA_UNSIGNED, buf, sizeof(buf), 11,
1298 "4294967295", 0);
1299
1300 CALL_AND_TEST("\xFF\xFF\xFF\xFF\xFF", 5, DATA_UNSIGNED, buf, sizeof(buf), 14,
1301 "1099511627775", 0);
1302
1303 CALL_AND_TEST("\xFF\xFF\xFF\xFF\xFF\xFF", 6, DATA_UNSIGNED, buf, sizeof(buf),
1304 16, "281474976710655", 0);
1305
1306 CALL_AND_TEST("\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 7, DATA_UNSIGNED, buf,
1307 sizeof(buf), 18, "72057594037927935", 0);
1308
1309 CALL_AND_TEST("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 8, DATA_UNSIGNED, buf,
1310 sizeof(buf), 21, "18446744073709551615", 0);
1311
1312 /* some random values */
1313
1314 CALL_AND_TEST("\x52", 1, 0, buf, sizeof(buf), 4, "-46", 0);
1315
1316 CALL_AND_TEST("\x0E", 1, DATA_UNSIGNED, buf, sizeof(buf), 3, "14", 0);
1317
1318 CALL_AND_TEST("\x62\xCE", 2, 0, buf, sizeof(buf), 6, "-7474", 0);
1319
1320 CALL_AND_TEST("\x29\xD6", 2, DATA_UNSIGNED, buf, sizeof(buf), 6, "10710", 0);
1321
1322 CALL_AND_TEST("\x7F\xFF\x90", 3, 0, buf, sizeof(buf), 5, "-112", 0);
1323
1324 CALL_AND_TEST("\x00\xA1\x16", 3, DATA_UNSIGNED, buf, sizeof(buf), 6, "41238",
1325 0);
1326
1327 CALL_AND_TEST("\x7F\xFF\xFF\xF7", 4, 0, buf, sizeof(buf), 3, "-9", 0);
1328
1329 CALL_AND_TEST("\x00\x00\x00\x5C", 4, DATA_UNSIGNED, buf, sizeof(buf), 3, "92",
1330 0);
1331
1332 CALL_AND_TEST("\x7F\xFF\xFF\xFF\xFF\xFF\xDC\x63", 8, 0, buf, sizeof(buf), 6,
1333 "-9117", 0);
1334
1335 CALL_AND_TEST("\x00\x00\x00\x00\x00\x01\x64\x62", 8, DATA_UNSIGNED, buf,
1336 sizeof(buf), 6, "91234", 0);
1337
1338 /* speed test */
1339
1340 ut_chrono_t ch(__func__);
1341
1342 for (auto i = 0; i < 1000000; i++) {
1343 row_raw_format_int("\x23", 1, 0, buf, sizeof(buf), &format_in_hex);
1344 row_raw_format_int("\x23", 1, DATA_UNSIGNED, buf, sizeof(buf),
1345 &format_in_hex);
1346
1347 row_raw_format_int("\x00\x00\x00\x00\x00\x01\x64\x62", 8, 0, buf,
1348 sizeof(buf), &format_in_hex);
1349 row_raw_format_int("\x00\x00\x00\x00\x00\x01\x64\x62", 8, DATA_UNSIGNED,
1350 buf, sizeof(buf), &format_in_hex);
1351 }
1352 }
1353
1354 #endif /* HAVE_UT_CHRONO_T */
1355
1356 #endif /* UNIV_ENABLE_UNIT_TEST_ROW_RAW_FORMAT_INT */