Debian Codegraph @

storage/innobase/data/data0data.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1994, 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 data/data0data.cc
29 SQL data field and tuple
30
31 Created 5/30/1994 Heikki Tuuri
32 *************************************************************************/
33
34 #include <sys/types.h>
35 #include <new>
36 #include <type_traits>
37
38 #include "data0data.h"
39 #include "ha_prototypes.h"
40
41 #ifndef UNIV_HOTBACKUP
42 #include "btr0cur.h"
43 #include "dict0dict.h"
44 #include "lob0lob.h"
45 #include "page0page.h"
46 #include "page0zip.h"
47 #include "rem0cmp.h"
48 #include "rem0rec.h"
49 #include "row0mysql.h"
50 #include "row0upd.h"
51
52 #endif /* !UNIV_HOTBACKUP */
53
54 #ifdef UNIV_DEBUG
55 /** Dummy variable to catch access to uninitialized fields. In the
56 debug version, dtuple_create() will make all fields of dtuple_t point
57 to data_error. */
58 byte data_error;
59 #endif /* UNIV_DEBUG */
60
61 #ifndef UNIV_HOTBACKUP
62 bool dtuple_coll_eq(const dtuple_t *tuple1, const dtuple_t *tuple2) {
63 ulint n_fields;
64 ulint i;
65 int cmp;
66
67 ut_ad(tuple1 != nullptr);
68 ut_ad(tuple2 != nullptr);
69 ut_ad(tuple1->magic_n == dtuple_t::MAGIC_N);
70 ut_ad(tuple2->magic_n == dtuple_t::MAGIC_N);
71 ut_ad(dtuple_check_typed(tuple1));
72 ut_ad(dtuple_check_typed(tuple2));
73
74 n_fields = dtuple_get_n_fields(tuple1);
75
76 cmp = (int)n_fields - (int)dtuple_get_n_fields(tuple2);
77
78 for (i = 0; cmp == 0 && i < n_fields; i++) {
79 dfield_t *field1 = dtuple_get_nth_field(tuple1, i);
80 const dfield_t *field2 = dtuple_get_nth_field(tuple2, i);
81
82 ut_ad(dfield_get_len(field2) != UNIV_NO_INDEX_VALUE);
83 ut_ad(!dfield_is_multi_value(field2) ||
84 dfield_get_len(field2) != UNIV_MULTI_VALUE_ARRAY_MARKER);
85
86 if (dfield_is_multi_value(field1)) {
87 cmp = cmp_multi_value_dfield_dfield(field1, field2);
88 } else {
89 /* Equality comparison does not care about ASC/DESC. */
90 cmp = cmp_dfield_dfield(field1, field2, true);
91 }
92 }
93
94 return (cmp == 0);
95 }
96
97 void dtuple_set_n_fields(dtuple_t *tuple, ulint n_fields) {
98 ut_ad(tuple);
99
100 tuple->n_fields = n_fields;
101 tuple->n_fields_cmp = n_fields;
102 }
103
104 /** Checks that a data field is typed.
105 @param[in] field Data field.
106 @return true if ok */
107 static bool dfield_check_typed_no_assert(const dfield_t *field) {
108 if (dfield_get_type(field)->mtype > DATA_MTYPE_CURRENT_MAX ||
109 dfield_get_type(field)->mtype < DATA_MTYPE_CURRENT_MIN) {
110 ib::error(ER_IB_MSG_156)
111 << "Data field type " << dfield_get_type(field)->mtype << ", len "
112 << dfield_get_len(field);
113
114 return (false);
115 }
116
117 return (true);
118 }
119
120 /** Checks that a data tuple is typed.
121 @param[in] tuple Tuple to check.
122 @return true if ok */
123 static bool dtuple_check_typed_no_assert(const dtuple_t *tuple) {
124 if (dtuple_get_n_fields(tuple) > REC_MAX_N_FIELDS) {
125 ib::error(ER_IB_MSG_157)
126 << "Index entry has " << dtuple_get_n_fields(tuple) << " fields";
127 dump:
128 fputs("InnoDB: Tuple contents: ", stderr);
129 dtuple_print(stderr, tuple);
130 putc('\n', stderr);
131
132 return (false);
133 }
134
135 for (ulint i = 0; i < dtuple_get_n_fields(tuple); i++) {
136 auto field = dtuple_get_nth_field(tuple, i);
137
138 if (!dfield_check_typed_no_assert(field)) {
139 goto dump;
140 }
141 }
142
143 return (true);
144 }
145 #endif /* !UNIV_HOTBACKUP */
146
147 #ifdef UNIV_DEBUG
148 bool dfield_check_typed(const dfield_t *field) {
149 if (dfield_get_type(field)->mtype > DATA_MTYPE_CURRENT_MAX ||
150 dfield_get_type(field)->mtype < DATA_MTYPE_CURRENT_MIN) {
151 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_158)
152 << "Data field type " << dfield_get_type(field)->mtype << ", len "
153 << dfield_get_len(field);
154 }
155
156 return (true);
157 }
158
159 bool dtuple_check_typed(const dtuple_t *tuple) {
160 const dfield_t *field;
161 ulint i;
162
163 for (i = 0; i < dtuple_get_n_fields(tuple); i++) {
164 field = dtuple_get_nth_field(tuple, i);
165
166 ut_a(dfield_check_typed(field));
167 }
168
169 return (true);
170 }
171
172 bool dtuple_validate(const dtuple_t *tuple) {
173 ut_ad(tuple->magic_n == dtuple_t::MAGIC_N);
174
175 auto n_fields = dtuple_get_n_fields(tuple);
176
177 /* We dereference all the data of each field to test
178 for memory traps */
179
180 for (ulint i = 0; i < n_fields; i++) {
181 auto field = dtuple_get_nth_field(tuple, i);
182 auto len = dfield_get_len(field);
183
184 if (!dfield_is_null(field)) {
185 const byte *data [[maybe_unused]];
186
187 data = static_cast<const byte *>(dfield_get_data(field));
188 #ifndef UNIV_DEBUG_VALGRIND
189 for (ulint j = 0; j < len; j++) {
190 data++;
191 }
192 #endif /* !UNIV_DEBUG_VALGRIND */
193
194 UNIV_MEM_ASSERT_RW(data, len);
195 }
196 }
197
198 ut_a(dtuple_check_typed(tuple));
199
200 return (true);
201 }
202 #endif /* UNIV_DEBUG */
203
204 #ifndef UNIV_HOTBACKUP
205 void dfield_print_also_hex(const dfield_t *dfield) {
206 auto len = dfield_get_len(dfield);
207 auto data = static_cast<const byte *>(dfield_get_data(dfield));
208
209 if (dfield_is_null(dfield)) {
210 fputs("NULL", stderr);
211
212 return;
213 }
214
215 bool print_also_hex{};
216 auto prtype = dtype_get_prtype(dfield_get_type(dfield));
217
218 switch (dtype_get_mtype(dfield_get_type(dfield))) {
219 ib_id_t id;
220 case DATA_INT:
221 switch (len) {
222 ulint val;
223 case 1:
224 val = mach_read_from_1(data);
225
226 if (!(prtype & DATA_UNSIGNED)) {
227 val &= ~0x80;
228 fprintf(stderr, "%ld", (long)val);
229 } else {
230 fprintf(stderr, "%lu", (ulong)val);
231 }
232 break;
233
234 case 2:
235 val = mach_read_from_2(data);
236
237 if (!(prtype & DATA_UNSIGNED)) {
238 val &= ~0x8000;
239 fprintf(stderr, "%ld", (long)val);
240 } else {
241 fprintf(stderr, "%lu", (ulong)val);
242 }
243 break;
244
245 case 3:
246 val = mach_read_from_3(data);
247
248 if (!(prtype & DATA_UNSIGNED)) {
249 val &= ~0x800000;
250 fprintf(stderr, "%ld", (long)val);
251 } else {
252 fprintf(stderr, "%lu", (ulong)val);
253 }
254 break;
255
256 case 4:
257 val = mach_read_from_4(data);
258
259 if (!(prtype & DATA_UNSIGNED)) {
260 val &= ~0x80000000;
261 fprintf(stderr, "%ld", (long)val);
262 } else {
263 fprintf(stderr, "%lu", (ulong)val);
264 }
265 break;
266
267 case 6:
268 id = mach_read_from_6(data);
269 fprintf(stderr, IB_ID_FMT, id);
270 break;
271
272 case 7:
273 id = mach_read_from_7(data);
274 fprintf(stderr, IB_ID_FMT, id);
275 break;
276 case 8:
277 id = mach_read_from_8(data);
278 fprintf(stderr, IB_ID_FMT, id);
279 break;
280 default:
281 goto print_hex;
282 }
283 break;
284
285 case DATA_SYS:
286 switch (prtype & DATA_SYS_PRTYPE_MASK) {
287 case DATA_TRX_ID:
288 id = mach_read_from_6(data);
289
290 fprintf(stderr, "trx_id " TRX_ID_FMT, id);
291 break;
292
293 case DATA_ROLL_PTR:
294 id = mach_read_from_7(data);
295
296 fprintf(stderr, "roll_ptr " TRX_ID_FMT, id);
297 break;
298
299 case DATA_ROW_ID:
300 id = mach_read_from_6(data);
301
302 fprintf(stderr, "row_id " TRX_ID_FMT, id);
303 break;
304
305 default:
306 goto print_hex;
307 }
308 break;
309
310 case DATA_CHAR:
311 case DATA_VARCHAR:
312 print_also_hex = false;
313
314 for (ulint i = 0; i < len; i++) {
315 int c = *data++;
316
317 if (!isprint(c)) {
318 print_also_hex = true;
319
320 fprintf(stderr, "\\x%02x", (unsigned char)c);
321 } else {
322 putc(c, stderr);
323 }
324 }
325
326 if (dfield_is_ext(dfield)) {
327 fputs("(external)", stderr);
328 }
329
330 if (!print_also_hex) {
331 break;
332 }
333
334 data = static_cast<byte *>(dfield_get_data(dfield));
335 [[fallthrough]];
336
337 case DATA_BINARY:
338 default:
339 print_hex:
340 fputs(" Hex: ", stderr);
341
342 for (ulint i = 0; i < len; i++) {
343 fprintf(stderr, "%02lx", static_cast<ulong>(*data++));
344 }
345
346 if (dfield_is_ext(dfield)) {
347 fputs("(external)", stderr);
348 }
349 }
350 }
351
352 /** Print a dfield value using ut_print_buf.
353 @param[in,out] f Output stream.
354 @param[in] dfield Value to print. */
355 static void dfield_print_raw(FILE *f, const dfield_t *dfield) {
356 ulint len = dfield_get_len(dfield);
357 if (!dfield_is_null(dfield)) {
358 ulint print_len = std::min(len, static_cast<ulint>(1000));
359 ut_print_buf(f, dfield_get_data(dfield), print_len);
360 if (len != print_len) {
361 fprintf(f, "(total %lu bytes%s)", (ulong)len,
362 dfield_is_ext(dfield) ? ", external" : "");
363 }
364 } else {
365 fputs(" SQL NULL", f);
366 }
367 }
368
369 void dtuple_print(FILE *f, const dtuple_t *tuple) {
370 auto n_fields = dtuple_get_n_fields(tuple);
371
372 fprintf(f, "DATA TUPLE: %lu fields;\n", (ulong)n_fields);
373
374 for (ulint i = 0; i < n_fields; i++) {
375 fprintf(f, " %lu:", (ulong)i);
376
377 dfield_print_raw(f, dtuple_get_nth_field(tuple, i));
378
379 putc(';', f);
380 putc('\n', f);
381 }
382
383 ut_ad(dtuple_validate(tuple));
384 }
385
386 void dfield_print(std::ostream &o, const dfield_t *field, ulint n) {
387 for (ulint i = 0; i < n; i++, field++) {
388 const void *data = dfield_get_data(field);
389 const ulint len = dfield_get_len(field);
390
391 if (i) {
392 o << ',';
393 }
394
395 if (dfield_is_null(field)) {
396 o << "NULL";
397 } else if (dfield_is_ext(field)) {
398 ulint local_len = len - BTR_EXTERN_FIELD_REF_SIZE;
399 ut_ad(len >= BTR_EXTERN_FIELD_REF_SIZE);
400
401 o << '[' << local_len << '+' << BTR_EXTERN_FIELD_REF_SIZE << ']';
402 ut_print_buf(o, data, local_len);
403 ut_print_buf_hex(o, static_cast<const byte *>(data) + local_len,
404 BTR_EXTERN_FIELD_REF_SIZE);
405 } else {
406 o << '[' << len << ']';
407 ut_print_buf(o, data, len);
408 }
409 }
410 }
411
412 void dtuple_print(std::ostream &o, const dtuple_t *tuple) {
413 const ulint n = dtuple_get_n_fields(tuple);
414
415 o << "TUPLE (info_bits=" << dtuple_get_info_bits(tuple) << ", " << n
416 << " n_cmp=" << tuple->n_fields_cmp << ", fields): {";
417
418 dfield_print(o, tuple->fields, n);
419
420 o << "}";
421 }
422
423 big_rec_t *dtuple_convert_big_rec(dict_index_t *index, upd_t *upd,
424 dtuple_t *entry) {
425 DBUG_TRACE;
426
427 mem_heap_t *heap;
428 big_rec_t *vector;
429 dfield_t *dfield;
430 dict_field_t *ifield;
431 ulint size;
432 ulint n_fields [[maybe_unused]];
433 ulint local_len;
434 ulint local_prefix_len;
435
436 if (!index->is_clustered()) {
437 return nullptr;
438 }
439
440 if (!dict_table_has_atomic_blobs(index->table)) {
441 /* up to MySQL 5.1: store a 768-byte prefix locally */
442 local_len = BTR_EXTERN_FIELD_REF_SIZE + DICT_ANTELOPE_MAX_INDEX_COL_LEN;
443 } else {
444 /* new-format table: do not store any BLOB prefix locally */
445 local_len = BTR_EXTERN_FIELD_REF_SIZE;
446 }
447
448 ut_a(dtuple_check_typed_no_assert(entry));
449
450 size = rec_get_converted_size(index, entry);
451
452 if (size > 1000000000) {
453 ib::warn(ER_IB_MSG_159) << "Tuple size is very big: " << size;
454 fputs("InnoDB: Tuple contents: ", stderr);
455 dtuple_print(stderr, entry);
456 putc('\n', stderr);
457 }
458
459 heap = mem_heap_create(
460 size + dtuple_get_n_fields(entry) * sizeof(big_rec_field_t) + 1000,
461 UT_LOCATION_HERE);
462
463 vector = big_rec_t::alloc(heap, dtuple_get_n_fields(entry));
464
465 /* Decide which fields to shorten: the algorithm is to look for
466 a variable-length field that yields the biggest savings when
467 stored externally */
468
469 n_fields = 0;
470
471 while (page_zip_rec_needs_ext(
472 rec_get_converted_size(index, entry), dict_table_is_comp(index->table),
473 dict_index_get_n_fields(index), dict_table_page_size(index->table))) {
474 byte *data;
475 ulint longest = 0;
476 ulint longest_i = ULINT_MAX;
477 const upd_field_t *uf = nullptr;
478
479 for (ulint i = dict_index_get_n_unique_in_tree(index);
480 i < dtuple_get_n_fields(entry); i++) {
481 ulint savings;
482
483 dfield = dtuple_get_nth_field(entry, i);
484 ifield = index->get_field(i);
485
486 ut_ad(dfield_get_len(dfield) != UNIV_SQL_INSTANT_DROP_COL);
487
488 /* Skip fixed-length, NULL, externally stored,
489 or short columns */
490
491 if (ifield->fixed_len || dfield_is_null(dfield) ||
492 dfield_is_ext(dfield) || dfield_get_len(dfield) <= local_len ||
493 dfield_get_len(dfield) <= BTR_EXTERN_LOCAL_STORED_MAX_SIZE) {
494 goto skip_field;
495 }
496
497 savings = dfield_get_len(dfield) - local_len;
498
499 /* Check that there would be savings */
500 if (longest >= savings) {
501 goto skip_field;
502 }
503
504 /* In DYNAMIC and COMPRESSED format, store
505 locally any non-BLOB columns whose maximum
506 length does not exceed 256 bytes. This is
507 because there is no room for the "external
508 storage" flag when the maximum length is 255
509 bytes or less. This restriction trivially
510 holds in REDUNDANT and COMPACT format, because
511 there we always store locally columns whose
512 length is up to local_len == 788 bytes.
513 @see rec_init_offsets_comp_ordinary */
514 if (!DATA_BIG_COL(ifield->col)) {
515 goto skip_field;
516 }
517
518 longest_i = i;
519 longest = savings;
520
521 skip_field:
522 continue;
523 }
524
525 if (!longest) {
526 /* Cannot shorten more */
527
528 mem_heap_free(heap);
529
530 return nullptr;
531 }
532
533 /* Move data from field longest_i to big rec vector.
534
535 We store the first bytes locally to the record. Then
536 we can calculate all ordering fields in all indexes
537 from locally stored data. */
538
539 dfield = dtuple_get_nth_field(entry, longest_i);
540
541 ifield = index->get_field(longest_i);
542 local_prefix_len = local_len - BTR_EXTERN_FIELD_REF_SIZE;
543
544 big_rec_field_t big_rec(
545 longest_i, dfield_get_len(dfield) - local_prefix_len,
546 static_cast<char *>(dfield_get_data(dfield)) + local_prefix_len);
547
548 /* Allocate the locally stored part of the column. */
549 data = static_cast<byte *>(mem_heap_alloc(heap, local_len));
550
551 /* Copy the local prefix (including LOB pointer). */
552 memcpy(data, dfield_get_data(dfield), local_len);
553
554 /* Clear the extern field reference (BLOB pointer). */
555 memset(data + local_prefix_len, 0, BTR_EXTERN_FIELD_REF_SIZE);
556
557 if (upd != nullptr &&
558 ((uf = upd->get_field_by_field_no(longest_i, index)) != nullptr)) {
559 /* When the externally stored LOB is going to be
560 updated, the old LOB reference (BLOB pointer) can be
561 used to access the old LOB object. So copy the LOB
562 reference here. */
563
564 if (dfield_is_ext(&uf->old_val)) {
565 byte *field_ref = static_cast<byte *>(dfield_get_data(&uf->old_val)) +
566 local_prefix_len;
567 memcpy(data + local_prefix_len, field_ref, lob::ref_t::SIZE);
568 }
569 }
570
571 #if 0
572 /* The following would fail the Valgrind checks in
573 page_cur_insert_rec_low() and page_cur_insert_rec_zip().
574 The BLOB pointers in the record will be initialized after
575 the record and the BLOBs have been written. */
576 UNIV_MEM_ALLOC(data + local_prefix_len,
577 BTR_EXTERN_FIELD_REF_SIZE);
578 #endif
579
580 dfield_set_data(dfield, data, local_len);
581 dfield_set_ext(dfield);
582
583 n_fields++;
584 ut_ad(n_fields < dtuple_get_n_fields(entry));
585
586 if (upd && !upd->is_modified(longest_i)) {
587 DEBUG_SYNC_C("ib_mv_nonupdated_column_offpage");
588
589 upd_field_t upd_field;
590 upd_field.field_no = longest_i;
591 IF_DEBUG(upd_field.field_phy_pos =
592 index->get_field(longest_i)->col->get_col_phy_pos();)
593 upd_field.orig_len = 0;
594 upd_field.exp = nullptr;
595 upd_field.old_v_val = nullptr;
596 upd_field.ext_in_old = dfield_is_ext(dfield);
597 dfield_copy(&upd_field.new_val, dfield->clone(upd->heap));
598 upd->append(upd_field);
599 ut_ad(upd->is_modified(longest_i));
600
601 ut_ad(upd_field.new_val.len >= BTR_EXTERN_FIELD_REF_SIZE);
602 ut_ad(upd_field.new_val.len == local_len);
603 ut_ad(upd_field.new_val.len == dfield_get_len(dfield));
604 }
605
606 if (upd == nullptr) {
607 big_rec.ext_in_old = false;
608 } else {
609 const upd_field_t *const uf =
610 upd->get_field_by_field_no(longest_i, index);
611 ut_ad(uf != nullptr);
612 big_rec.ext_in_old = uf->ext_in_old;
613 }
614
615 big_rec.ext_in_new = true;
616 vector->append(big_rec);
617 }
618
619 ut_ad(n_fields == vector->n_fields);
620 return vector;
621 }
622
623 void dtuple_convert_back_big_rec(dtuple_t *entry, big_rec_t *vector) {
624 auto b = vector->fields;
625 const big_rec_field_t *const end = b + vector->n_fields;
626
627 for (; b < end; b++) {
628 dfield_t *dfield;
629 ulint local_len;
630
631 dfield = dtuple_get_nth_field(entry, b->field_no);
632 local_len = dfield_get_len(dfield);
633
634 ut_ad(dfield_is_ext(dfield));
635 ut_ad(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
636
637 local_len -= BTR_EXTERN_FIELD_REF_SIZE;
638
639 /* Only in REDUNDANT and COMPACT format, we store
640 up to DICT_ANTELOPE_MAX_INDEX_COL_LEN (768) bytes
641 locally */
642 ut_ad(local_len <= DICT_ANTELOPE_MAX_INDEX_COL_LEN);
643
644 dfield_set_data(dfield, (char *)b->data - local_len, b->len + local_len);
645 }
646
647 mem_heap_free(vector->heap);
648 }
649
650 big_rec_t *big_rec_t::alloc(mem_heap_t *heap, ulint n_fld) {
651 big_rec_t *rec =
652 static_cast<big_rec_t *>(mem_heap_alloc(heap, sizeof(big_rec_t)));
653
654 new (rec) big_rec_t(n_fld);
655
656 rec->heap = heap;
657 rec->fields = static_cast<big_rec_field_t *>(
658 mem_heap_alloc(heap, n_fld * sizeof(big_rec_field_t)));
659
660 rec->n_fields = 0;
661 return rec;
662 }
663
664 dfield_t *dfield_t::clone(mem_heap_t *heap) {
665 const ulint size = len == UNIV_SQL_NULL ? 0 : len;
666 dfield_t *obj =
667 static_cast<dfield_t *>(mem_heap_alloc(heap, sizeof(dfield_t) + size));
668
669 obj->ext = ext;
670 obj->len = len;
671 obj->type = type;
672 obj->spatial_status = spatial_status;
673
674 if (len != UNIV_SQL_NULL) {
675 obj->data = obj + 1;
676 memcpy(obj->data, data, len);
677 } else {
678 obj->data = nullptr;
679 }
680
681 return obj;
682 }
683
684 byte *dfield_t::blobref() const {
685 ut_ad(ext);
686
687 return static_cast<byte *>(data) + len - BTR_EXTERN_FIELD_REF_SIZE;
688 }
689
690 uint32_t dfield_t::lob_version() const {
691 ut_ad(ext);
692 byte *field_ref = blobref();
693
694 lob::ref_t ref(field_ref);
695 return (ref.version());
696 }
697
698 void dfield_t::adjust_v_data_mysql(const dict_v_col_t *vcol, bool comp,
699 const byte *field, ulint len,
700 mem_heap_t *heap) {
701 ulint mtype;
702 const byte *data = field;
703
704 ut_ad(heap != nullptr);
705
706 mtype = type.mtype;
707
708 if (mtype != DATA_MYSQL) {
709 dfield_set_data(this, field, len);
710 return;
711 }
712
713 /* Adjust the value if the data type is DATA_MYSQL, either
714 adding or striping trailing spaces when necessary. This may happen
715 in the scenario where there is an ALTER TABLE changing table's
716 row format from compact to non-compact or vice versa, and there
717 is also concurrent INSERT to this table. The log for the data could
718 be in different format from the final format, which should be adjusted.
719 Refer to row_mysql_store_col_in_innobase_format() too. */
720 if (comp && len == vcol->m_col.len && dtype_get_mbminlen(&type) == 1 &&
721 dtype_get_mbmaxlen(&type) > 1) {
722 /* A full length record, which is of multibyte
723 charsets and recorded because old table is non-compact.
724 However, in compact table, no trailing spaces. */
725 ulint n_chars;
726
727 ut_a(!(dtype_get_len(&type) % dtype_get_mbmaxlen(&type)));
728
729 n_chars = dtype_get_len(&type) / dtype_get_mbmaxlen(&type);
730
731 while (len > n_chars && data[len - 1] == 0x20) {
732 --len;
733 }
734 } else if (!comp && len < vcol->m_col.len && dtype_get_mbminlen(&type) == 1) {
735 /* A not full length record from compact table, so have to
736 add trailing spaces. */
737 byte *v_data =
738 reinterpret_cast<byte *>(mem_heap_alloc(heap, vcol->m_col.len));
739
740 memcpy(v_data, field, len);
741 row_mysql_pad_col(1, v_data + len, vcol->m_col.len - len);
742
743 data = v_data;
744 len = vcol->m_col.len;
745 }
746
747 dfield_set_data(this, data, len);
748 }
749
750 std::ostream &dfield_t::print(std::ostream &out) const {
751 out << "[dfield_t: data=" << (void *)data << ", ext=" << ext << " ";
752
753 if (dfield_is_ext(this)) {
754 byte *tmp = static_cast<byte *>(data);
755 lob::ref_t ref(tmp + len - lob::ref_t::SIZE);
756 out << ref;
757 }
758
759 out << ", spatial_status=" << spatial_status << ", len=" << len
760 << ", type=" << type << "]";
761
762 return (out);
763 }
764
765 #ifdef UNIV_DEBUG
766 std::ostream &big_rec_field_t::print(std::ostream &out) const {
767 out << "[big_rec_field_t: field_no=" << field_no << ", len=" << len
768 << ", data=" << PrintBuffer(data, len) << ", ext_in_old=" << ext_in_old
769 << ", ext_in_new=" << ext_in_new << "]";
770 return (out);
771 }
772
773 std::ostream &big_rec_t::print(std::ostream &out) const {
774 out << "[big_rec_t: capacity=" << capacity << ", n_fields=" << n_fields
775 << " ";
776 for (ulint i = 0; i < n_fields; ++i) {
777 out << fields[i];
778 }
779 out << "]";
780 return (out);
781 }
782
783 void dtuple_t::validate() const {
784 for (uint16_t i = 0; i < n_fields; i++) {
785 const dfield_t &field = fields[i];
786 /* All fields in the fields vector represent actual fields in the
787 record, so their DATA_VIRTUAL flag is cleared regardless of
788 whether they are defined for virtual columns. */
789 ut_a((field.type.prtype & DATA_VIRTUAL) == 0U);
790 }
791 for (uint16_t i = 0; i < n_v_fields; i++) {
792 const dfield_t &field = v_fields[i];
793 /* All fields in the v_fields vector represent fields not present
794 in the record, and as such have their DATA_VIRTUAL flag set. */
795 ut_a((field.type.prtype & DATA_VIRTUAL) == DATA_VIRTUAL);
796 }
797 }
798
799 void dtuple_t::validate_for_index(const dict_index_t *index) const {
800 validate();
801 if (!index->is_clustered()) {
802 /* Only tuples for clustered index may have virtual fields.
803 Secondary index fields based on virtual columns are not
804 considered virtual. */
805 ut_a(n_v_fields == 0);
806 }
807 }
808 #endif /* UNIV_DEBUG */
809
810 trx_id_t dtuple_t::get_trx_id() const {
811 for (ulint i = 0; i < n_fields; ++i) {
812 dfield_t &field = fields[i];
813
814 uint32_t prtype = field.type.prtype & DATA_SYS_PRTYPE_MASK;
815
816 if (field.type.mtype == DATA_SYS && prtype == DATA_TRX_ID) {
817 return (mach_read_from_6((byte *)field.data));
818 }
819 }
820
821 return (0);
822 }
823
824 void dtuple_t::ignore_trailing_default(const dict_index_t *index) {
825 ut_a(index->has_instant_cols());
826 ut_a(!index->has_row_versions());
827
828 /* It's necessary to check all the fields that could be default.
829 If it's from normal update, it should be OK to keep original
830 default values in the physical record as is, however,
831 if it's from rollback, it may rollback an update from default
832 value to non-default. To make the rolled back record as is,
833 it has to check all possible default values. */
834 for (; n_fields > index->get_instant_fields(); --n_fields) {
835 const dict_col_t *col = index->get_field(n_fields - 1)->col;
836
837 /* We shall never come here if INSTANT ADD/DROP is done in a version. */
838 ut_a(!col->is_instant_added());
839 ut_a(!col->is_instant_dropped());
840
841 const dfield_t *dfield = dtuple_get_nth_field(this, n_fields - 1);
842 ulint len = dfield_get_len(dfield);
843
844 ut_ad(col->instant_default != nullptr);
845
846 if (len != col->instant_default->len ||
847 (len != UNIV_SQL_NULL &&
848 memcmp(dfield_get_data(dfield), col->instant_default->value, len) !=
849 0)) {
850 break;
851 }
852 }
853 }
854
855 bool is_multi_value_clust_and_sec_equal(const byte *clust_field,
856 uint64_t clust_len,
857 const byte *sec_field, uint64_t sec_len,
858 const dict_col_t *col) {
859 if (clust_len == UNIV_SQL_NULL) {
860 return (sec_len == UNIV_SQL_NULL);
861 } else if (clust_len == UNIV_NO_INDEX_VALUE) {
862 return (false);
863 }
864
865 ut_ad(clust_len == UNIV_MULTI_VALUE_ARRAY_MARKER);
866
867 const multi_value_data *multi_value =
868 reinterpret_cast<const multi_value_data *>(clust_field);
869 ut_ad(multi_value != nullptr);
870 return (multi_value->has(col->mtype, col->prtype, sec_field, sec_len));
871 }
872
873 bool multi_value_data::has(const dtype_t *type, const byte *data,
874 uint64_t len) const {
875 return (has(type->mtype, type->prtype, data, len));
876 }
877
878 bool multi_value_data::has(ulint mtype, ulint prtype, const byte *data,
879 uint64_t len) const {
880 for (uint32_t i = 0; i < num_v; ++i) {
881 if (cmp_data_data(mtype, prtype, true,
882 reinterpret_cast<const byte *>(datap[i]), data_len[i],
883 data, len) == 0) {
884 return (true);
885 }
886 }
887
888 return (false);
889 }
890
891 #endif /* !UNIV_HOTBACKUP */
892
893 void multi_value_data::alloc(uint32_t num, bool alc_bitset, mem_heap_t *heap) {
894 datap =
895 static_cast<const void **>(mem_heap_zalloc(heap, num * sizeof(*datap)));
896 data_len =
897 static_cast<uint32_t *>(mem_heap_zalloc(heap, num * sizeof(*data_len)));
898 conv_buf =
899 static_cast<uint64_t *>(mem_heap_zalloc(heap, num * sizeof(*conv_buf)));
900
901 num_alc = num;
902
903 if (alc_bitset) {
904 alloc_bitset(heap, num);
905 } else {
906 bitset = nullptr;
907 }
908 }
909
910 void multi_value_data::alloc_bitset(mem_heap_t *heap, uint32_t size) {
911 ut_ad(bitset == nullptr);
912
913 bitset = static_cast<Bitset<> *>(mem_heap_zalloc(heap, sizeof(Bitset<>)));
914 /*TODO: why is it num_v? Shouldn't it be num_alc? Looks like copy_low assumes
915 that this->bitset points to a bitmap which is at least as large as other's
916 multi_value->bitset->size(), as it is copying bytes from it, yet the only
917 assert protection is that num_alc is larger than that. So, what's preventing
918 num_alc > multi_value->bitset->size() > num_v = bitmap_bytes? */
919 uint32_t alloc_size = UT_BITS_IN_BYTES(size == 0 ? num_v : size);
920 byte *bitmap = static_cast<byte *>(mem_heap_zalloc(heap, alloc_size));
921 ut_d(bitmap_bytes = alloc_size);
922 new (bitset) Bitset(bitmap, alloc_size);
923 bitset->set();
924 static_assert(
925 std::is_trivially_destructible_v<Bitset<>>,
926 "Bitset<> must be trivially destructible in order to be able to "
927 "(de)allocate it via mem_heap_t");
928 }
929
930 uint32_t Multi_value_logger::get_log_len(bool precise) const {
931 /* Alwayas a multi-value data marker at the beginning */
932 uint32_t total_len = 1;
933
934 if (m_field_len == UNIV_SQL_NULL || m_field_len == UNIV_NO_INDEX_VALUE) {
935 total_len += s_log_length_for_null_or_empty;
936 return (total_len);
937 }
938
939 ut_ad(m_field_len == UNIV_MULTI_VALUE_ARRAY_MARKER);
940
941 /* Keep two bytes for the total length of the log */
942 total_len += 2;
943
944 /* Remember the length of the multi-value array.
945 Will write the data_len[i] in compressed format which at most
946 costs 5 bytes */
947 total_len += precise ? mach_get_compressed_size(m_mv_data->num_v)
948 : s_max_compressed_mv_key_length_size;
949
950 /* Remember each data length and value */
951 for (uint32_t i = 0; i < m_mv_data->num_v; ++i) {
952 ut_ad(m_mv_data->data_len[i] != UNIV_SQL_NULL);
953 total_len += m_mv_data->data_len[i];
954 total_len += precise ? mach_get_compressed_size(m_mv_data->data_len[i])
955 : s_max_compressed_mv_key_length_size;
956 }
957
958 /* Remember the bitset of the multi-value data if exists*/
959 if (m_mv_data->bitset != nullptr) {
960 total_len += UT_BITS_IN_BYTES(m_mv_data->num_v);
961 }
962
963 return (total_len);
964 }
965
966 byte *Multi_value_logger::log(byte **ptr) {
967 mach_write_to_1(*ptr, s_multi_value_virtual_col_length_marker);
968 *ptr += 1;
969
970 if (m_field_len == UNIV_SQL_NULL) {
971 mach_write_to_2(*ptr, s_multi_value_null);
972 *ptr += s_log_length_for_null_or_empty;
973 return (*ptr);
974 } else if (m_field_len == UNIV_NO_INDEX_VALUE) {
975 mach_write_to_2(*ptr, s_multi_value_no_index_value);
976 *ptr += s_log_length_for_null_or_empty;
977 return (*ptr);
978 }
979
980 ut_ad(m_field_len == UNIV_MULTI_VALUE_ARRAY_MARKER);
981 byte *old_ptr = *ptr;
982
983 /* Store data in the same sequence as described in get_log_len() */
984 *ptr += 2;
985 *ptr += mach_write_compressed(*ptr, m_mv_data->num_v);
986 for (uint32_t i = 0; i < m_mv_data->num_v; ++i) {
987 ut_ad(m_mv_data->data_len[i] != UNIV_SQL_NULL);
988 *ptr += mach_write_compressed(*ptr, m_mv_data->data_len[i]);
989 ut_memcpy(*ptr, m_mv_data->datap[i], m_mv_data->data_len[i]);
990 *ptr += m_mv_data->data_len[i];
991 }
992 /* This fragment is optional, which will be detected by reader by comparing
993 the amount of data read, to the length we will write to *old_ptr later. */
994 if (m_mv_data->bitset != nullptr) {
995 /* Always just write out the bitset of enough size for all data,
996 rather than the size of bitset. */
997 uint32_t bitset_len = UT_BITS_IN_BYTES(m_mv_data->num_v);
998 /* make sure we will not read past the buffer */
999 ut_ad(bitset_len <= m_mv_data->bitmap_bytes);
1000 /* make sure we will not miss actual data */
1001 ut_ad(m_mv_data->bitset->size_bytes() <= bitset_len);
1002 ut_memcpy(*ptr, m_mv_data->bitset->data(), bitset_len);
1003 *ptr += bitset_len;
1004 }
1005
1006 mach_write_to_2(old_ptr, *ptr - old_ptr);
1007
1008 return (*ptr);
1009 }
1010
1011 uint32_t Multi_value_logger::read_log_len(const byte *ptr) {
1012 ut_ad(is_multi_value_log(ptr));
1013
1014 uint32_t total_len = mach_read_from_2(ptr + 1);
1015
1016 if (total_len == s_multi_value_null ||
1017 total_len == s_multi_value_no_index_value) {
1018 return (1 + s_log_length_for_null_or_empty);
1019 }
1020
1021 return (1 + total_len);
1022 }
1023
1024 const byte *Multi_value_logger::read(const byte *ptr, dfield_t *field,
1025 mem_heap_t *heap) {
1026 ut_ad(is_multi_value_log(ptr));
1027
1028 ++ptr;
1029
1030 uint32_t total_len = mach_read_from_2(ptr);
1031 const byte *old_ptr = ptr;
1032
1033 ptr += s_log_length_for_null_or_empty;
1034
1035 if (total_len == s_multi_value_null) {
1036 dfield_set_null(field);
1037 return (ptr);
1038 } else if (total_len == s_multi_value_no_index_value) {
1039 dfield_set_data(field, nullptr, UNIV_NO_INDEX_VALUE);
1040 return (ptr);
1041 }
1042
1043 uint32_t num = mach_read_next_compressed(&ptr);
1044 field->data = mem_heap_alloc(heap, sizeof(multi_value_data));
1045 field->len = UNIV_MULTI_VALUE_ARRAY_MARKER;
1046
1047 multi_value_data *multi_val = static_cast<multi_value_data *>(field->data);
1048
1049 multi_val->num_v = num;
1050 multi_val->alloc(num, false, heap);
1051
1052 for (uint32_t i = 0; i < num; ++i) {
1053 uint32_t len = mach_read_next_compressed(&ptr);
1054 multi_val->datap[i] = ptr;
1055 multi_val->data_len[i] = len;
1056
1057 ptr += len;
1058 }
1059
1060 if (ptr < old_ptr + total_len) {
1061 multi_val->alloc_bitset(heap);
1062 ut_ad(num == multi_val->num_v);
1063 ut_ad(UT_BITS_IN_BYTES(num) == multi_val->bitmap_bytes);
1064 ut_a(UT_BITS_IN_BYTES(num) == multi_val->bitset->size_bytes());
1065 multi_val->bitset->copy_from(ptr);
1066 ptr += UT_BITS_IN_BYTES(num);
1067 }
1068
1069 ut_ad(ptr == old_ptr + total_len);
1070 return (ptr);
1071 }
1072
1073 uint32_t Multi_value_logger::get_keys_capacity(uint32_t log_size,
1074 uint32_t key_length,
1075 uint32_t *num_keys) {
1076 uint32_t keys_length = log_size;
1077
1078 /* The calculation should be based on the ::log(), how it logs will
1079 affect how the capacities calculated. And to be safe, in this function,
1080 the length of each key would be assumed to be always the key_length
1081 passed in, regardless of how actual data will consume. */
1082
1083 /* Exclude the bytes for multi-value marker, total log length and
1084 number of keys */
1085 keys_length -= (1 + 2 + 2);
1086
1087 /* Ignore the bitset too, to make the estimation simple */
1088
1089 *num_keys =
1090 keys_length /
1091 (key_length + Multi_value_logger::s_max_compressed_mv_key_length_size);
1092
1093 /* Total key length should also exclude the bytes for length of each key */
1094 keys_length -=
1095 *num_keys * Multi_value_logger::s_max_compressed_mv_key_length_size;
1096
1097 return (keys_length);
1098 }