Debian Codegraph @

storage/innobase/include/data0data.ic

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 include/data0data.ic
29 SQL data field and tuple
30
31 Created 5/30/1994 Heikki Tuuri
32 *************************************************************************/
33
34 #include "btr0types.h"
35 #include "mem0mem.h"
36
37 #ifdef UNIV_DEBUG
38 /** Dummy variable to catch access to uninitialized fields. In the
39 debug version, dtuple_create() will make all fields of dtuple_t point
40 to data_error. */
41 extern byte data_error;
42
43 /** Gets pointer to the type struct of SQL data field.
44 @return pointer to the type struct */
45 static inline dtype_t *dfield_get_type(
46 const dfield_t *field) /*!< in: SQL data field */
47 {
48 ut_ad(field);
49
50 return ((dtype_t *)&(field->type));
51 }
52 #endif /* UNIV_DEBUG */
53
54 /** Sets the type struct of SQL data field. */
55 static inline void dfield_set_type(
56 dfield_t *field, /*!< in: SQL data field */
57 const dtype_t *type) /*!< in: pointer to data type struct */
58 {
59 ut_ad(field != nullptr);
60 ut_ad(type != nullptr);
61
62 field->type = *type;
63 }
64
65 #ifdef UNIV_DEBUG
66 /** Gets pointer to the data in a field.
67 @return pointer to data */
68 static inline void *dfield_get_data(const dfield_t *field) /*!< in: field */
69 {
70 ut_ad(field);
71 ut_ad((field->len == UNIV_SQL_NULL) || (field->data != &data_error));
72
73 return ((void *)field->data);
74 }
75 #endif /* UNIV_DEBUG */
76
77 /** Gets length of field data.
78 @return length of data; UNIV_SQL_NULL if SQL null data */
79 static inline uint32_t dfield_get_len(const dfield_t *field) /*!< in: field */
80 {
81 ut_ad(field);
82 ut_ad((field->len == UNIV_SQL_NULL) || (field->data != &data_error));
83
84 return (field->len);
85 }
86
87 /** Sets length in a field. */
88 static inline void dfield_set_len(dfield_t *field, /*!< in: field */
89 ulint len) /*!< in: length or UNIV_SQL_NULL */
90 {
91 ut_ad(field);
92 #ifdef UNIV_VALGRIND_DEBUG
93 if (len != UNIV_SQL_NULL) UNIV_MEM_ASSERT_RW(field->data, len);
94 #endif /* UNIV_VALGRIND_DEBUG */
95
96 field->ext = 0;
97 field->len = static_cast<unsigned int>(len);
98 }
99
100 /** Determines if a field is SQL NULL
101 @return nonzero if SQL null data */
102 static inline ulint dfield_is_null(const dfield_t *field) /*!< in: field */
103 {
104 ut_ad(field);
105
106 return (field->len == UNIV_SQL_NULL);
107 }
108
109 /** Determines if a field is externally stored
110 @return nonzero if externally stored */
111 static inline bool dfield_is_ext(const dfield_t *field) /*!< in: field */
112 {
113 ut_ad(field);
114 ut_ad(!field->ext || field->len >= BTR_EXTERN_FIELD_REF_SIZE);
115
116 return (field->ext);
117 }
118
119 /** Sets the "external storage" flag */
120 static inline void dfield_set_ext(dfield_t *field) /*!< in/out: field */
121 {
122 ut_ad(field);
123
124 field->ext = 1;
125 }
126
127 /** Gets spatial status for "external storage"
128 @param[in,out] field field */
129 static inline spatial_status_t dfield_get_spatial_status(
130 const dfield_t *field) {
131 ut_ad(field);
132 ut_ad(dfield_is_ext(field));
133
134 return (static_cast<spatial_status_t>(field->spatial_status));
135 }
136
137 /** Sets spatial status for "external storage"
138 @param[in,out] field field
139 @param[in] spatial_status spatial status */
140 static inline void dfield_set_spatial_status(dfield_t *field,
141 spatial_status_t spatial_status) {
142 ut_ad(field);
143 ut_ad(dfield_is_ext(field));
144
145 field->spatial_status = spatial_status;
146 }
147
148 /** Sets pointer to the data and length in a field. */
149 static inline void dfield_set_data(
150 dfield_t *field, /*!< in: field */
151 const void *data, /*!< in: data */
152 ulint len) /*!< in: length or UNIV_SQL_NULL */
153 {
154 ut_ad(field);
155
156 #ifdef UNIV_VALGRIND_DEBUG
157 if (len != UNIV_SQL_NULL) UNIV_MEM_ASSERT_RW(data, len);
158 #endif /* UNIV_VALGRIND_DEBUG */
159 field->data = (void *)data;
160 field->ext = 0;
161 field->len = static_cast<unsigned int>(len);
162 }
163
164 /** Sets pointer to the data and length in a field. */
165 static inline void dfield_write_mbr(dfield_t *field, /*!< in: field */
166 const double *mbr) /*!< in: data */
167 {
168 ut_ad(field);
169
170 #ifdef UNIV_VALGRIND_DEBUG
171 if (len != UNIV_SQL_NULL) UNIV_MEM_ASSERT_RW(data, len);
172 #endif /* UNIV_VALGRIND_DEBUG */
173 field->ext = 0;
174
175 for (uint i = 0; i < SPDIMS * 2; i++) {
176 mach_double_write(static_cast<byte *>(field->data) + i * sizeof(double),
177 mbr[i]);
178 }
179
180 field->len = DATA_MBR_LEN;
181 }
182
183 /** Sets a data field to SQL NULL. */
184 static inline void dfield_set_null(dfield_t *field) /*!< in/out: field */
185 {
186 dfield_set_data(field, nullptr, UNIV_SQL_NULL);
187 }
188
189 /** Copies the data and len fields. */
190 static inline void dfield_copy_data(
191 dfield_t *field1, /*!< out: field to copy to */
192 const dfield_t *field2) /*!< in: field to copy from */
193 {
194 ut_ad(field1 != nullptr);
195 ut_ad(field2 != nullptr);
196
197 field1->data = field2->data;
198 field1->len = field2->len;
199 field1->ext = field2->ext;
200 field1->spatial_status = field2->spatial_status;
201 }
202
203 /** Copies a data field to another. */
204 static inline void dfield_copy(
205 dfield_t *field1, /*!< out: field to copy to */
206 const dfield_t *field2) /*!< in: field to copy from */
207 {
208 *field1 = *field2;
209 }
210
211 /** Copies the data pointed to by a data field. */
212 static inline void dfield_dup(
213 dfield_t *field, /*!< in/out: data field */
214 mem_heap_t *heap) /*!< in: memory heap where allocated */
215 {
216 ut_ad(!dfield_is_multi_value(field) ||
217 dfield_get_len(field) != UNIV_MULTI_VALUE_ARRAY_MARKER);
218
219 if (!dfield_is_null(field) && field->data != nullptr) {
220 UNIV_MEM_ASSERT_RW(field->data, field->len);
221 field->data = mem_heap_dup(heap, field->data, field->len);
222 }
223 }
224
225 static inline bool dfield_is_multi_value(const dfield_t *field) {
226 /* The value should be initialized already. */
227 ut_ad(field->type.prtype != DATA_ERROR);
228 return ((field->type.prtype & DATA_MULTI_VALUE) != 0);
229 }
230
231 /** Copies the data pointed to by a data field.
232 This function works for multi-value fields only.
233 @param[in,out] field data field
234 @param[in] heap memory heap where allocated */
235 static inline void dfield_multi_value_dup(dfield_t *field, mem_heap_t *heap) {
236 ut_ad(dfield_is_multi_value(field));
237
238 /* Actually, this checking exempts the NULL and UNIV_NO_INDEX_VALUE cases */
239 if (!dfield_is_null(field) && field->data != nullptr) {
240 ut_ad(field->len == UNIV_MULTI_VALUE_ARRAY_MARKER);
241 multi_value_data *mv_data = static_cast<multi_value_data *>(field->data);
242 multi_value_data *value =
243 static_cast<multi_value_data *>(mem_heap_zalloc(heap, sizeof(*value)));
244 value->copy(mv_data, heap);
245 field->data = value;
246 }
247 }
248
249 inline bool dfield_datas_are_binary_equal(const dfield_t *field1,
250 const dfield_t *field2, ulint len) {
251 if (dfield_is_multi_value(field1)) {
252 ut_ad(dfield_is_multi_value(field2));
253 ut_ad(field1->len == UNIV_MULTI_VALUE_ARRAY_MARKER ||
254 field1->len == UNIV_SQL_NULL || field1->len == UNIV_NO_INDEX_VALUE);
255 ut_ad(field2->len == UNIV_MULTI_VALUE_ARRAY_MARKER ||
256 field2->len == UNIV_SQL_NULL || field2->len == UNIV_NO_INDEX_VALUE);
257 if (field1->len != field2->len) {
258 return (false);
259 }
260
261 if (field2->len != UNIV_MULTI_VALUE_ARRAY_MARKER) {
262 return (true);
263 }
264
265 ut_ad(field1->len == UNIV_MULTI_VALUE_ARRAY_MARKER);
266 ut_ad(field2->len == UNIV_MULTI_VALUE_ARRAY_MARKER);
267
268 const auto *multi_val1 = static_cast<multi_value_data *>(field1->data);
269 const auto *multi_val2 = static_cast<multi_value_data *>(field2->data);
270
271 return (multi_val1->equal(multi_val2));
272 }
273
274 auto len2 = len;
275
276 if (field1->len == UNIV_SQL_NULL || len == 0 || field1->len < len) {
277 len = field1->len;
278 }
279
280 if (field2->len == UNIV_SQL_NULL || len2 == 0 || field2->len < len2) {
281 len2 = field2->len;
282 }
283
284 return (len == len2 &&
285 (len == UNIV_SQL_NULL || !memcmp(field1->data, field2->data, len)));
286 }
287
288 static inline bool dfield_data_is_binary_equal(const dfield_t *field, ulint len,
289 const byte *data) {
290 if (dfield_is_multi_value(field)) {
291 uint64_t field_len = dfield_get_len(field);
292 if (field_len == UNIV_MULTI_VALUE_ARRAY_MARKER) {
293 if (len != UNIV_MULTI_VALUE_ARRAY_MARKER) {
294 return (false);
295 }
296
297 multi_value_data *mv_data =
298 reinterpret_cast<multi_value_data *>(field->data);
299 return (mv_data->equal(reinterpret_cast<const multi_value_data *>(data)));
300 } else {
301 return (field_len == len &&
302 (len == UNIV_SQL_NULL || len == UNIV_NO_INDEX_VALUE ||
303 memcmp(dfield_get_data(field), data, len) == 0));
304 }
305 } else {
306 return (len == dfield_get_len(field) &&
307 (len == UNIV_SQL_NULL || len == 0 ||
308 !memcmp(dfield_get_data(field), data, len)));
309 }
310 }
311 #ifndef UNIV_HOTBACKUP
312 #endif /* !UNIV_HOTBACKUP */
313
314 /** Gets info bits in a data tuple.
315 @return info bits */
316 static inline ulint dtuple_get_info_bits(
317 const dtuple_t *tuple) /*!< in: tuple */
318 {
319 ut_ad(tuple);
320
321 return (tuple->info_bits);
322 }
323
324 /** Sets info bits in a data tuple. */
325 static inline void dtuple_set_info_bits(dtuple_t *tuple, /*!< in: tuple */
326 ulint info_bits) /*!< in: info bits */
327 {
328 ut_ad(tuple);
329
330 tuple->info_bits = info_bits;
331 }
332
333 /** Gets number of fields used in record comparisons.
334 @return number of fields used in comparisons in rem0cmp.* */
335 static inline ulint dtuple_get_n_fields_cmp(
336 const dtuple_t *tuple) /*!< in: tuple */
337 {
338 ut_ad(tuple);
339
340 return (tuple->n_fields_cmp);
341 }
342
343 static inline void dtuple_set_n_fields_cmp(dtuple_t *tuple,
344 ulint n_fields_cmp) {
345 ut_ad(tuple);
346 ut_ad(n_fields_cmp <= tuple->n_fields);
347
348 tuple->n_fields_cmp = n_fields_cmp;
349 }
350
351 /** Gets number of fields in a data tuple.
352 @return number of fields */
353 static inline ulint dtuple_get_n_fields(const dtuple_t *tuple) /*!< in: tuple */
354 {
355 ut_ad(tuple);
356
357 return (tuple->n_fields);
358 }
359
360 /** Gets the number of virtual fields in a data tuple.
361 @param[in] tuple dtuple to check
362 @return number of fields */
363 static inline ulint dtuple_get_n_v_fields(const dtuple_t *tuple) {
364 ut_ad(tuple);
365
366 return (tuple->n_v_fields);
367 }
368 #ifdef UNIV_DEBUG
369 static inline dfield_t *dtuple_get_nth_field(const dtuple_t *tuple, ulint n) {
370 ut_ad(tuple);
371 ut_ad(n < tuple->n_fields);
372
373 return ((dfield_t *)tuple->fields + n);
374 }
375 static inline dfield_t *dtuple_get_nth_v_field(const dtuple_t *tuple, ulint n) {
376 ut_ad(tuple);
377 ut_ad(n < tuple->n_v_fields);
378
379 return (static_cast<dfield_t *>(tuple->v_fields + n));
380 }
381 #endif /* UNIV_DEBUG */
382
383 /** Creates a data tuple from an already allocated chunk of memory.
384 The size of the chunk must be at least DTUPLE_EST_ALLOC(n_fields).
385 The default value for number of fields used in record comparisons
386 for this tuple is n_fields.
387 @param[in,out] buf buffer to use
388 @param[in] buf_size buffer size
389 @param[in] n_fields number of field
390 @param[in] n_v_fields number of fields on virtual columns
391 @return created tuple (inside buf) */
392 static inline dtuple_t *dtuple_create_from_mem(void *buf, ulint buf_size,
393 ulint n_fields,
394 ulint n_v_fields) {
395 dtuple_t *tuple;
396 ulint n_t_fields = n_fields + n_v_fields;
397
398 ut_ad(buf != nullptr);
399 ut_a(buf_size >= DTUPLE_EST_ALLOC(n_t_fields));
400
401 tuple = (dtuple_t *)buf;
402 tuple->info_bits = 0;
403 tuple->n_fields = n_fields;
404 tuple->n_v_fields = n_v_fields;
405 tuple->n_fields_cmp = n_fields;
406 tuple->fields = (dfield_t *)&tuple[1];
407 if (n_v_fields > 0) {
408 tuple->v_fields = &tuple->fields[n_fields];
409 } else {
410 tuple->v_fields = nullptr;
411 }
412
413 #ifdef UNIV_DEBUG
414 tuple->magic_n = dtuple_t::MAGIC_N;
415
416 { /* In the debug version, initialize fields to an error value */
417 ulint i;
418
419 for (i = 0; i < n_t_fields; i++) {
420 dfield_t *field;
421
422 if (i >= n_fields) {
423 field = dtuple_get_nth_v_field(tuple, i - n_fields);
424 } else {
425 field = dtuple_get_nth_field(tuple, i);
426 }
427
428 dfield_set_len(field, UNIV_SQL_NULL);
429 field->data = &data_error;
430 dfield_get_type(field)->mtype = DATA_ERROR;
431 dfield_get_type(field)->prtype = DATA_ERROR;
432 }
433 }
434 #endif
435 UNIV_MEM_ASSERT_W(tuple->fields, n_t_fields * sizeof *tuple->fields);
436 UNIV_MEM_INVALID(tuple->fields, n_t_fields * sizeof *tuple->fields);
437 return (tuple);
438 }
439
440 /** Duplicate the virtual field data in a dtuple_t
441 @param[in,out] vrow dtuple contains the virtual fields
442 @param[in] heap heap memory to use */
443 static inline void dtuple_dup_v_fld(const dtuple_t *vrow, mem_heap_t *heap) {
444 for (ulint i = 0; i < vrow->n_v_fields; i++) {
445 dfield_t *dfield = dtuple_get_nth_v_field(vrow, i);
446 if (dfield_is_multi_value(dfield)) {
447 dfield_multi_value_dup(dfield, heap);
448 } else {
449 dfield_dup(dfield, heap);
450 }
451 }
452 }
453
454 /** Initialize the virtual field data in a dtuple_t
455 @param[in,out] vrow dtuple contains the virtual fields */
456 static inline void dtuple_init_v_fld(const dtuple_t *vrow) {
457 for (ulint i = 0; i < vrow->n_v_fields; i++) {
458 dfield_t *dfield = dtuple_get_nth_v_field(vrow, i);
459 dfield_get_type(dfield)->mtype = DATA_MISSING;
460 dfield_get_type(dfield)->prtype = 0;
461 dfield_set_len(dfield, UNIV_SQL_NULL);
462 }
463 }
464
465 /** Creates a data tuple to a memory heap. The default value for number
466 of fields used in record comparisons for this tuple is n_fields.
467 @return own: created tuple */
468 static inline dtuple_t *dtuple_create(
469 mem_heap_t *heap, /*!< in: memory heap where the tuple
470 is created, DTUPLE_EST_ALLOC(n_fields)
471 bytes will be allocated from this heap */
472 ulint n_fields) /*!< in: number of fields */
473 {
474 return (dtuple_create_with_vcol(heap, n_fields, 0));
475 }
476
477 /** Creates a data tuple with virtual columns to a memory heap.
478 @param[in] heap memory heap where the tuple is created
479 @param[in] n_fields number of fields
480 @param[in] n_v_fields number of fields on virtual col
481 @return own: created tuple */
482 static inline dtuple_t *dtuple_create_with_vcol(mem_heap_t *heap,
483 ulint n_fields,
484 ulint n_v_fields) {
485 void *buf;
486 ulint buf_size;
487 dtuple_t *tuple;
488
489 ut_ad(heap);
490
491 buf_size = DTUPLE_EST_ALLOC(n_fields + n_v_fields);
492 buf = mem_heap_zalloc(heap, buf_size);
493
494 tuple = dtuple_create_from_mem(buf, buf_size, n_fields, n_v_fields);
495
496 #ifdef UNIV_DEBUG
497 tuple->m_heap = heap;
498 #endif /* UNIV_DEBUG */
499
500 return (tuple);
501 }
502
503 /** Copies a data tuple's virtual fields to another. This is a shallow copy;
504 @param[in,out] d_tuple destination tuple
505 @param[in] s_tuple source tuple */
506 static inline void dtuple_copy_v_fields(dtuple_t *d_tuple,
507 const dtuple_t *s_tuple) {
508 ulint n_v_fields = dtuple_get_n_v_fields(d_tuple);
509 ut_ad(n_v_fields == dtuple_get_n_v_fields(s_tuple));
510
511 for (ulint i = 0; i < n_v_fields; i++) {
512 dfield_copy(dtuple_get_nth_v_field(d_tuple, i),
513 dtuple_get_nth_v_field(s_tuple, i));
514 }
515 }
516
517 /** Copies a data tuple to another. This is a shallow copy; if a deep copy
518 is desired, dfield_dup() will have to be invoked on each field.
519 @return own: copy of tuple */
520 static inline dtuple_t *dtuple_copy(
521 const dtuple_t *tuple, /*!< in: tuple to copy from */
522 mem_heap_t *heap) /*!< in: memory heap
523 where the tuple is created */
524 {
525 ulint n_fields = dtuple_get_n_fields(tuple);
526 ulint n_v_fields = dtuple_get_n_v_fields(tuple);
527 dtuple_t *new_tuple = dtuple_create_with_vcol(heap, n_fields, n_v_fields);
528 ulint i;
529
530 for (i = 0; i < n_fields; i++) {
531 dfield_copy(dtuple_get_nth_field(new_tuple, i),
532 dtuple_get_nth_field(tuple, i));
533 }
534
535 for (i = 0; i < n_v_fields; i++) {
536 dfield_copy(dtuple_get_nth_v_field(new_tuple, i),
537 dtuple_get_nth_v_field(tuple, i));
538 }
539
540 return (new_tuple);
541 }
542
543 /** The following function returns the sum of data lengths of a tuple. The space
544 occupied by the field structs or the tuple struct is not counted. Neither
545 is possible space in externally stored parts of the field.
546 @return sum of data lengths */
547 static inline ulint dtuple_get_data_size(
548 const dtuple_t *tuple, /*!< in: typed data tuple */
549 ulint comp) /*!< in: nonzero=ROW_FORMAT=COMPACT */
550 {
551 const dfield_t *field;
552 ulint n_fields;
553 ulint len;
554 ulint i;
555 ulint sum = 0;
556
557 ut_ad(tuple);
558 ut_ad(dtuple_check_typed(tuple));
559 ut_ad(tuple->magic_n == dtuple_t::MAGIC_N);
560
561 n_fields = tuple->n_fields;
562
563 for (i = 0; i < n_fields; i++) {
564 field = dtuple_get_nth_field(tuple, i);
565 len = dfield_get_len(field);
566
567 if (len == UNIV_SQL_NULL) {
568 len = dtype_get_sql_null_size(dfield_get_type(field), comp);
569 }
570
571 ut_ad(len != UNIV_SQL_INSTANT_DROP_COL);
572
573 sum += len;
574 }
575
576 return (sum);
577 }
578
579 /** Sets types of fields binary in a tuple. */
580 static inline void dtuple_set_types_binary(
581 dtuple_t *tuple, /*!< in: data tuple */
582 ulint n) /*!< in: number of fields to set */
583 {
584 dtype_t *dfield_type;
585 ulint i;
586
587 for (i = 0; i < n; i++) {
588 bool is_multi_value = dfield_is_multi_value(dtuple_get_nth_field(tuple, i));
589 dfield_type = dfield_get_type(dtuple_get_nth_field(tuple, i));
590 dtype_set(dfield_type, DATA_BINARY, (is_multi_value ? DATA_MULTI_VALUE : 0),
591 0);
592 }
593 }
594
595 static inline uint64_t dtuple_hash(const dtuple_t *tuple, ulint n_fields,
596 ulint n_bytes, uint64_t hash_value) {
597 const dfield_t *field;
598 ulint i;
599 const byte *data;
600 ulint len;
601
602 ut_ad(tuple);
603 ut_ad(tuple->magic_n == dtuple_t::MAGIC_N);
604 ut_ad(dtuple_check_typed(tuple));
605
606 for (i = 0; i < n_fields; i++) {
607 field = dtuple_get_nth_field(tuple, i);
608
609 data = (const byte *)dfield_get_data(field);
610 len = dfield_get_len(field);
611
612 if (len != UNIV_SQL_NULL) {
613 hash_value = ut::hash_binary(data, len, hash_value);
614 }
615 }
616
617 if (n_bytes > 0) {
618 field = dtuple_get_nth_field(tuple, i);
619
620 data = (const byte *)dfield_get_data(field);
621 len = dfield_get_len(field);
622
623 if (len != UNIV_SQL_NULL) {
624 if (len > n_bytes) {
625 len = n_bytes;
626 }
627
628 hash_value = ut::hash_binary(data, len, hash_value);
629 }
630 }
631
632 return (hash_value);
633 }
634
635 static inline void data_write_sql_null(byte *data, ulint len) {
636 memset(data, 0, len);
637 }
638
639 static inline bool dtuple_contains_null(const dtuple_t *tuple) {
640 auto n = dtuple_get_n_fields(tuple);
641
642 for (ulint i = 0; i < n; i++) {
643 if (dfield_is_null(dtuple_get_nth_field(tuple, i))) {
644 return (true);
645 }
646 }
647
648 return (false);
649 }
650
651 void dtuple_big_rec_free(big_rec_t *vector) { mem_heap_free(vector->heap); }