Debian Codegraph @

storage/innobase/rem/rem0cmp.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 rem/rem0cmp.cc
29 Comparison services for records
30
31 Created 7/1/1994 Heikki Tuuri
32 ************************************************************************/
33
34 #include <gstream.h>
35 #include <page0cur.h>
36 #include <spatial.h>
37 #include <sys/types.h>
38 #include <algorithm>
39
40 #include "ha_prototypes.h"
41 #include "handler0alter.h"
42 #include "rem0cmp.h"
43 #include "srv0srv.h"
44 namespace dd {
45 class Spatial_reference_system;
46 }
47
48 /* ALPHABETICAL ORDER
49 ==================
50
51 The records are put into alphabetical order in the following
52 way: let F be the first field where two records disagree.
53 If there is a character in some position n where the
54 records disagree, the order is determined by comparison of
55 the characters at position n, possibly after
56 collating transformation. If there is no such character,
57 but the corresponding fields have different lengths, then
58 if the data type of the fields is paddable,
59 shorter field is padded with a padding character. If the
60 data type is not paddable, longer field is considered greater.
61 Finally, the SQL null is bigger than any other value.
62
63 At the present, the comparison functions return 0 in the case,
64 where two records disagree only in the way that one
65 has more fields than the other. */
66
67 /** Compare two data fields.
68 @param[in] prtype precise type
69 @param[in] a data field
70 @param[in] a_length length of a, in bytes (not UNIV_SQL_NULL)
71 @param[in] b data field
72 @param[in] b_length length of b, in bytes (not UNIV_SQL_NULL)
73 @return positive, 0, negative, if a is greater, equal, less than b,
74 respectively */
75 static inline int innobase_mysql_cmp(ulint prtype, const byte *a,
76 size_t a_length, const byte *b,
77 size_t b_length) {
78 #ifdef UNIV_DEBUG
79 switch (prtype & DATA_MYSQL_TYPE_MASK) {
80 case MYSQL_TYPE_BIT:
81 case MYSQL_TYPE_STRING:
82 case MYSQL_TYPE_VAR_STRING:
83 case MYSQL_TYPE_TINY_BLOB:
84 case MYSQL_TYPE_MEDIUM_BLOB:
85 case MYSQL_TYPE_BLOB:
86 case MYSQL_TYPE_LONG_BLOB:
87 case MYSQL_TYPE_VARCHAR:
88 break;
89 default:
90 ut_error;
91 }
92 #endif /* UNIV_DEBUG */
93
94 uint cs_num = (uint)dtype_get_charset_coll(prtype);
95
96 if (CHARSET_INFO *cs = get_charset(cs_num, MYF(MY_WME))) {
97 if ((prtype & DATA_MYSQL_TYPE_MASK) == MYSQL_TYPE_STRING &&
98 cs->pad_attribute == NO_PAD) {
99 /* MySQL specifies that CHAR fields are stripped of
100 trailing spaces before being returned from the database.
101 Normally this is done in Field_string::val_str(),
102 but since we don't involve the Field classes for internal
103 index comparisons, we need to do the same thing here
104 for NO PAD collations. (If not, strnncollsp will ignore
105 the spaces for us, so we don't need to do it here.) */
106 a_length = cs->cset->lengthsp(cs, (const char *)a, a_length);
107 b_length = cs->cset->lengthsp(cs, (const char *)b, b_length);
108 }
109
110 return (cs->coll->strnncollsp(cs, a, a_length, b, b_length));
111 }
112
113 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_919)
114 << "Unable to find charset-collation " << cs_num;
115 return (0);
116 }
117
118 bool cmp_cols_are_equal(const dict_col_t *col1, const dict_col_t *col2,
119 bool check_charsets) {
120 if (dtype_is_non_binary_string_type(col1->mtype, col1->prtype) &&
121 dtype_is_non_binary_string_type(col2->mtype, col2->prtype)) {
122 /* Both are non-binary string types: they can be compared if
123 and only if the charset-collation is the same */
124
125 if (check_charsets) {
126 return (dtype_get_charset_coll(col1->prtype) ==
127 dtype_get_charset_coll(col2->prtype));
128 } else {
129 return true;
130 }
131 }
132
133 if (dtype_is_binary_string_type(col1->mtype, col1->prtype) &&
134 dtype_is_binary_string_type(col2->mtype, col2->prtype)) {
135 /* Both are binary string types: they can be compared */
136
137 return true;
138 }
139
140 if (col1->mtype != col2->mtype) {
141 return false;
142 }
143
144 if (col1->mtype == DATA_INT &&
145 (col1->prtype & DATA_UNSIGNED) != (col2->prtype & DATA_UNSIGNED)) {
146 /* The storage format of an unsigned integer is different
147 from a signed integer: in a signed integer we OR
148 0x8000... to the value of positive integers. */
149
150 return false;
151 }
152
153 return (col1->mtype != DATA_INT || col1->len == col2->len);
154 }
155
156 /** Compare two DATA_DECIMAL (MYSQL_TYPE_DECIMAL) fields.
157 TODO: Remove this function. Everything should use MYSQL_TYPE_NEWDECIMAL.
158 @param[in] a data field
159 @param[in] a_length length of a, in bytes (not UNIV_SQL_NULL)
160 @param[in] b data field
161 @param[in] b_length length of b, in bytes (not UNIV_SQL_NULL)
162 @param[in] is_asc true=ascending, false=descending order
163 @return positive, 0, negative, if a is greater, equal, less than b,
164 respectively */
165 static UNIV_COLD int cmp_decimal(const byte *a, unsigned int a_length,
166 const byte *b, unsigned int b_length,
167 bool is_asc) {
168 int swap_flag = is_asc ? 1 : -1;
169
170 /* Remove preceding spaces */
171 for (; a_length && *a == ' '; a++, a_length--) {
172 }
173 for (; b_length && *b == ' '; b++, b_length--) {
174 }
175
176 if (*a == '-') {
177 swap_flag = -swap_flag;
178
179 if (*b != '-') {
180 return (swap_flag);
181 }
182
183 a++;
184 b++;
185 a_length--;
186 b_length--;
187 } else {
188 if (*b == '-') {
189 return (swap_flag);
190 }
191 }
192
193 while (a_length > 0 && (*a == '+' || *a == '0')) {
194 a++;
195 a_length--;
196 }
197
198 while (b_length > 0 && (*b == '+' || *b == '0')) {
199 b++;
200 b_length--;
201 }
202
203 if (a_length != b_length) {
204 if (a_length < b_length) {
205 return (-swap_flag);
206 }
207
208 return (swap_flag);
209 }
210
211 while (a_length > 0 && *a == *b) {
212 a++;
213 b++;
214 a_length--;
215 }
216
217 if (a_length == 0) {
218 return (0);
219 }
220
221 if (*a <= *b) {
222 swap_flag = -swap_flag;
223 }
224
225 return (swap_flag);
226 }
227
228 /** Innobase uses this function to compare two geometry data fields
229 @return 1, 0, -1, if a is greater, equal, less than b, respectively */
230 static int cmp_geometry_field(ulint prtype, /*!< in: precise type */
231 const byte *a, /*!< in: data field */
232 unsigned int a_length, /*!< in: data field length,
233 not UNIV_SQL_NULL */
234 const byte *b, /*!< in: data field */
235 unsigned int b_length) /*!< in: data field length,
236 not UNIV_SQL_NULL */
237 {
238 double x1, x2;
239 double y1, y2;
240
241 ut_ad(prtype & DATA_GIS_MBR);
242
243 if (a_length < sizeof(double) || b_length < sizeof(double)) {
244 return (0);
245 }
246
247 /* Try to compare mbr left lower corner (xmin, ymin) */
248 x1 = mach_double_read(a);
249 x2 = mach_double_read(b);
250 y1 = mach_double_read(a + sizeof(double) * SPDIMS);
251 y2 = mach_double_read(b + sizeof(double) * SPDIMS);
252
253 if (x1 > x2) {
254 return (1);
255 } else if (x2 > x1) {
256 return (-1);
257 }
258
259 if (y1 > y2) {
260 return (1);
261 } else if (y2 > y1) {
262 return (-1);
263 }
264
265 /* left lower corner (xmin, ymin) overlaps, now right upper corner */
266 x1 = mach_double_read(a + sizeof(double));
267 x2 = mach_double_read(b + sizeof(double));
268 y1 = mach_double_read(a + sizeof(double) * SPDIMS + sizeof(double));
269 y2 = mach_double_read(b + sizeof(double) * SPDIMS + sizeof(double));
270
271 if (x1 > x2) {
272 return (1);
273 } else if (x2 > x1) {
274 return (-1);
275 }
276
277 if (y1 > y2) {
278 return (1);
279 } else if (y2 > y1) {
280 return (-1);
281 }
282
283 return (0);
284 }
285
286 /** Innobase uses this function to compare two gis data fields
287 @return 1, 0, -1, if mode == PAGE_CUR_MBR_EQUAL. And return
288 1, 0 for rest compare modes, depends on a and b qualifies the
289 relationship (CONTAIN, WITHIN etc.) */
290 static int cmp_gis_field(
291 page_cur_mode_t mode, /*!< in: compare mode */
292 const byte *a, /*!< in: data field */
293 unsigned int a_length, /*!< in: data field length,
294 not UNIV_SQL_NULL */
295 const byte *b, /*!< in: data field */
296 unsigned int b_length, /*!< in: data field length,
297 not UNIV_SQL_NULL */
298 const dd::Spatial_reference_system *srs) /*!< in: SRS of R-tree */
299 {
300 if (mode == PAGE_CUR_MBR_EQUAL) {
301 /* TODO: Since the DATA_GEOMETRY is not used in compare
302 function, we could pass it instead of a specific type now */
303 return (cmp_geometry_field(DATA_GIS_MBR, a, a_length, b, b_length));
304 } else {
305 return (rtree_key_cmp(mode, a, a_length, b, b_length, srs) ? 0 : 1);
306 }
307 }
308
309 /** Compare two data fields.
310 @param[in] mtype main type
311 @param[in] prtype precise type
312 @param[in] is_asc true=ascending, false=descending order
313 @param[in] a data field
314 @param[in] a_length length of a, in bytes (not UNIV_SQL_NULL)
315 @param[in] b data field
316 @param[in] b_length length of b, in bytes (not UNIV_SQL_NULL)
317 @return positive, 0, negative, if a is greater, equal, less than b,
318 respectively */
319 static int cmp_whole_field(ulint mtype, ulint prtype, bool is_asc,
320 const byte *a, unsigned int a_length, const byte *b,
321 unsigned int b_length) {
322 float f_1;
323 float f_2;
324 double d_1;
325 double d_2;
326 int cmp;
327
328 switch (mtype) {
329 case DATA_DECIMAL:
330 return (cmp_decimal(a, a_length, b, b_length, is_asc));
331 case DATA_DOUBLE:
332 d_1 = mach_double_read(a);
333 d_2 = mach_double_read(b);
334
335 if (d_1 > d_2) {
336 return (is_asc ? 1 : -1);
337 } else if (d_2 > d_1) {
338 return (is_asc ? -1 : 1);
339 }
340
341 return (0);
342
343 case DATA_FLOAT:
344 f_1 = mach_float_read(a);
345 f_2 = mach_float_read(b);
346
347 if (f_1 > f_2) {
348 return (is_asc ? 1 : -1);
349 } else if (f_2 > f_1) {
350 return (is_asc ? -1 : 1);
351 }
352
353 return (0);
354 case DATA_VARCHAR:
355 case DATA_CHAR:
356 cmp = my_charset_latin1.coll->strnncollsp(&my_charset_latin1, a, a_length,
357 b, b_length);
358 break;
359 case DATA_BLOB:
360 if (prtype & DATA_BINARY_TYPE) {
361 ib::error(ER_IB_MSG_920) << "Comparing a binary BLOB"
362 " using a character set collation!";
363 ut_d(ut_error);
364 }
365 [[fallthrough]];
366 case DATA_VARMYSQL:
367 case DATA_MYSQL:
368 cmp = innobase_mysql_cmp(prtype, a, a_length, b, b_length);
369 break;
370 case DATA_POINT:
371 case DATA_VAR_POINT:
372 case DATA_GEOMETRY:
373 return (cmp_geometry_field(prtype, a, a_length, b, b_length));
374 default:
375 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_921)
376 << "Unknown data type number " << mtype;
377 cmp = 0;
378 }
379 if (!is_asc) {
380 cmp = -cmp;
381 }
382 return (cmp);
383 }
384
385 /** Compare two data fields.
386 @param[in] mtype main type
387 @param[in] prtype precise type
388 @param[in] is_asc true=ascending, false=descending order
389 @param[in] data1 data field
390 @param[in] len1 length of data1 in bytes, or UNIV_SQL_NULL
391 @param[in] data2 data field
392 @param[in] len2 length of data2 in bytes, or UNIV_SQL_NULL
393 @return the comparison result of data1 and data2
394 @retval 0 if data1 is equal to data2
395 @retval negative if data1 is less than data2
396 @retval positive if data1 is greater than data2 */
397 inline int cmp_data(ulint mtype, ulint prtype, bool is_asc, const byte *data1,
398 ulint len1, const byte *data2, ulint len2) {
399 ut_ad(!(prtype & DATA_MULTI_VALUE) ||
400 (len1 != UNIV_MULTI_VALUE_ARRAY_MARKER && len1 != UNIV_NO_INDEX_VALUE &&
401 len2 != UNIV_MULTI_VALUE_ARRAY_MARKER && len2 != UNIV_NO_INDEX_VALUE));
402
403 if (len1 == UNIV_SQL_NULL || len2 == UNIV_SQL_NULL) {
404 if (len1 == len2) {
405 return (0);
406 }
407
408 /* We define the SQL null to be the smallest possible
409 value of a field. */
410 return ((len1 == UNIV_SQL_NULL) == is_asc ? -1 : 1);
411 }
412
413 ulint pad;
414
415 switch (mtype) {
416 case DATA_FIXBINARY:
417 case DATA_BINARY:
418 if (dtype_get_charset_coll(prtype) != DATA_MYSQL_BINARY_CHARSET_COLL) {
419 pad = 0x20;
420 break;
421 }
422 [[fallthrough]];
423 case DATA_INT:
424 case DATA_SYS_CHILD:
425 case DATA_SYS:
426 pad = ULINT_UNDEFINED;
427 break;
428 case DATA_POINT:
429 case DATA_VAR_POINT:
430 /* Since DATA_POINT has a fixed length of DATA_POINT_LEN,
431 currently, pad is not needed. Meanwhile, DATA_VAR_POINT acts
432 the same as DATA_GEOMETRY */
433 case DATA_GEOMETRY:
434 ut_ad(prtype & DATA_BINARY_TYPE);
435 pad = ULINT_UNDEFINED;
436 if (prtype & DATA_GIS_MBR) {
437 return (cmp_whole_field(mtype, prtype, is_asc, data1, (unsigned)len1,
438 data2, (unsigned)len2));
439 }
440 break;
441 case DATA_BLOB:
442 if (prtype & DATA_BINARY_TYPE) {
443 pad = ULINT_UNDEFINED;
444 break;
445 }
446 [[fallthrough]];
447 default:
448 return (cmp_whole_field(mtype, prtype, is_asc, data1, (unsigned)len1,
449 data2, (unsigned)len2));
450 }
451
452 ulint len;
453
454 if (len1 < len2) {
455 len = len1;
456 len2 -= len;
457 len1 = 0;
458 } else {
459 len = len2;
460 len1 -= len;
461 len2 = 0;
462 }
463
464 int cmp;
465
466 if (len > 0) {
467 #if defined __i386__ || defined __x86_64__ || defined _M_IX86 || defined _M_X64
468 /* Compare the first bytes with a loop to avoid the call
469 overhead of memcmp(). On x86 and x86-64, the GCC built-in
470 (repz cmpsb) seems to be very slow, so we will be calling the
471 libc version. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052
472 tracks the slowness of the GCC built-in memcmp().
473
474 We compare up to the first 4..7 bytes with the loop.
475 The (len & 3) is used for "normalizing" or
476 "quantizing" the len parameter for the memcmp() call,
477 in case the whole prefix is equal. On x86 and x86-64,
478 the GNU libc memcmp() of equal strings is faster with
479 len=4 than with len=3.
480
481 On other architectures than the IA32 or AMD64, there could
482 be a built-in memcmp() that is faster than the loop.
483 We only use the loop where we know that it can improve
484 the performance. */
485 for (ulint i = 4 + (len & 3); i > 0; i--) {
486 cmp = int(*data1++) - int(*data2++);
487
488 if (cmp != 0) {
489 return (is_asc ? cmp : -cmp);
490 }
491
492 if (!--len) {
493 break;
494 }
495 }
496
497 if (len) {
498 #endif /* IA32 or AMD64 */
499 cmp = memcmp(data1, data2, len);
500
501 if (cmp != 0) {
502 return (is_asc ? cmp : -cmp);
503 }
504
505 data1 += len;
506 data2 += len;
507 #if defined __i386__ || defined __x86_64__ || defined _M_IX86 || defined _M_X64
508 }
509 #endif /* IA32 or AMD64 */
510 }
511
512 cmp = (int)(len1 - len2);
513
514 if (cmp == 0) {
515 return (0);
516 }
517
518 if (pad == ULINT_UNDEFINED) {
519 return (is_asc ? cmp : -cmp);
520 }
521
522 if (len1 > 0) {
523 ulint len{};
524
525 do {
526 cmp = static_cast<int>(mach_read_from_1(&data1[len]) - pad);
527 ++len;
528 } while (cmp == 0 && len < len1);
529
530 } else {
531 ut_ad(len2 > 0);
532
533 ulint len{};
534
535 do {
536 cmp = static_cast<int>(pad - mach_read_from_1(&data2[len]));
537 ++len;
538 } while (cmp == 0 && len < len2);
539 }
540
541 return (is_asc ? cmp : -cmp);
542 }
543
544 int cmp_dtuple_rec_with_gis(const dtuple_t *dtuple, const rec_t *rec,
545 const ulint *offsets, page_cur_mode_t mode,
546 const dd::Spatial_reference_system *srs) {
547 const dfield_t *dtuple_field;
548 ulint dtuple_f_len;
549 ulint rec_f_len;
550 const byte *rec_b_ptr;
551 int ret = 0;
552
553 dtuple_field = dtuple_get_nth_field(dtuple, 0);
554 dtuple_f_len = dfield_get_len(dtuple_field);
555
556 rec_b_ptr = rec_get_nth_field(nullptr, rec, offsets, 0, &rec_f_len);
557 ret = cmp_gis_field(
558 mode, static_cast<const byte *>(dfield_get_data(dtuple_field)),
559 (unsigned)dtuple_f_len, rec_b_ptr, (unsigned)rec_f_len, srs);
560
561 return (ret);
562 }
563
564 int cmp_dtuple_rec_with_gis_internal(const dtuple_t *dtuple, const rec_t *rec,
565 const ulint *offsets,
566 const dd::Spatial_reference_system *srs) {
567 const dfield_t *dtuple_field; /* current field in logical record */
568 ulint dtuple_f_len; /* the length of the current field
569 in the logical record */
570 ulint rec_f_len; /* length of current field in rec */
571 const byte *rec_b_ptr; /* pointer to the current byte in
572 rec field */
573 int ret = 0; /* return value */
574
575 dtuple_field = dtuple_get_nth_field(dtuple, 0);
576 dtuple_f_len = dfield_get_len(dtuple_field);
577
578 rec_b_ptr = rec_get_nth_field(nullptr, rec, offsets, 0, &rec_f_len);
579 ret = cmp_gis_field(
580 PAGE_CUR_WITHIN, static_cast<const byte *>(dfield_get_data(dtuple_field)),
581 (unsigned)dtuple_f_len, rec_b_ptr, (unsigned)rec_f_len, srs);
582 if (ret != 0) {
583 return (ret);
584 }
585
586 dtuple_field = dtuple_get_nth_field(dtuple, 1);
587 dtuple_f_len = dfield_get_len(dtuple_field);
588 rec_b_ptr = rec_get_nth_field(nullptr, rec, offsets, 1, &rec_f_len);
589
590 return (cmp_data(dtuple_field->type.mtype, dtuple_field->type.prtype, true,
591 static_cast<const byte *>(dtuple_field->data), dtuple_f_len,
592 rec_b_ptr, rec_f_len));
593 }
594
595 int cmp_data_data(ulint mtype, ulint prtype, bool is_asc, const byte *data1,
596 ulint len1, const byte *data2, ulint len2) {
597 return (cmp_data(mtype, prtype, is_asc, data1, len1, data2, len2));
598 }
599
600 int cmp_dtuple_rec_with_match_low(const dtuple_t *dtuple, const rec_t *rec,
601 const dict_index_t *index,
602 const ulint *offsets, ulint n_cmp,
603 ulint *matched_fields) {
604 ut_ad(dtuple_check_typed(dtuple));
605 ut_ad(rec_offs_validate(rec, index, offsets));
606
607 ut_ad(n_cmp > 0);
608 ut_ad(*matched_fields == DISABLE_MIN_REC_FLAG_CHECK ||
609 *matched_fields <= n_cmp);
610 ut_ad(n_cmp <= dtuple_get_n_fields(dtuple));
611 ut_ad(*matched_fields == DISABLE_MIN_REC_FLAG_CHECK ||
612 *matched_fields <= rec_offs_n_fields(offsets));
613
614 if (*matched_fields == 0) {
615 ulint rec_info = rec_get_info_bits(rec, rec_offs_comp(offsets));
616 ulint tup_info = dtuple_get_info_bits(dtuple);
617
618 /* The leftmost node pointer record is defined as
619 smaller than any other node pointer, independent of
620 any ASC/DESC flags. It is an "infimum node pointer". */
621 if (rec_info & REC_INFO_MIN_REC_FLAG) {
622 return (!(tup_info & REC_INFO_MIN_REC_FLAG));
623 } else if (tup_info & REC_INFO_MIN_REC_FLAG) {
624 return (-1);
625 }
626 } else if (*matched_fields == DISABLE_MIN_REC_FLAG_CHECK) {
627 /* Disable the left most node check. */
628 *matched_fields = 0;
629 }
630
631 /* Compare fields in a loop. */
632 for (auto i = *matched_fields; i < n_cmp; ++i) {
633 const auto dtuple_field = dtuple_get_nth_field(dtuple, i);
634
635 const auto dtuple_b_ptr =
636 static_cast<const byte *>(dfield_get_data(dtuple_field));
637
638 const auto type = dfield_get_type(dtuple_field);
639
640 auto dtuple_f_len = dfield_get_len(dtuple_field);
641
642 /* We should never compare against an externally
643 stored field. Only clustered index records can
644 contain externally stored fields, and the first fields
645 (primary key fields) should already differ. */
646 ut_ad(!rec_offs_nth_extern(index, offsets, i));
647
648 /* So does the field with default value */
649 ut_ad(!rec_offs_nth_default(index, offsets, i));
650
651 ulint rec_f_len;
652
653 const auto rec_b_ptr =
654 rec_get_nth_field(index, rec, offsets, i, &rec_f_len);
655
656 ut_ad(!dfield_is_ext(dtuple_field));
657
658 int ret{};
659
660 if (dfield_is_multi_value(dtuple_field) &&
661 (dtuple_f_len == UNIV_MULTI_VALUE_ARRAY_MARKER ||
662 dtuple_f_len == UNIV_NO_INDEX_VALUE)) {
663 /* If it's the value parsed from the array, or NULL, then
664 the calculation can be done in a normal way in the else branch */
665 ut_ad(index->is_multi_value());
666 if (dtuple_f_len == UNIV_NO_INDEX_VALUE) {
667 ret = 1;
668 } else {
669 multi_value_data *mv_data =
670 static_cast<multi_value_data *>(dtuple_field->data);
671 ret = mv_data->has(type, rec_b_ptr, rec_f_len) ? 0 : 1;
672 }
673 } else {
674 /* For now, change buffering is only supported on
675 indexes with ascending order on the columns. */
676 ret = cmp_data(
677 type->mtype, type->prtype,
678 dict_index_is_ibuf(index) || index->get_field(i)->is_ascending,
679 dtuple_b_ptr, dtuple_f_len, rec_b_ptr, rec_f_len);
680 }
681
682 if (ret) {
683 *matched_fields = i;
684 return (ret);
685 }
686 }
687
688 /* If we ran out of fields, dtuple was equal to rec up to the common fields */
689 *matched_fields = n_cmp;
690
691 return (0);
692 }
693
694 /** Get the pad character code point for a type.
695 @param[in] type SQL data type
696 @return pad character code point
697 @retval ULINT_UNDEFINED if no padding is specified */
698 static inline ulint cmp_get_pad_char(const dtype_t *type) {
699 switch (type->mtype) {
700 case DATA_FIXBINARY:
701 case DATA_BINARY:
702 if (dtype_get_charset_coll(type->prtype) ==
703 DATA_MYSQL_BINARY_CHARSET_COLL) {
704 /* Starting from 5.0.18, do not pad
705 VARBINARY or BINARY columns. */
706 return (ULINT_UNDEFINED);
707 }
708 [[fallthrough]];
709 case DATA_CHAR:
710 case DATA_VARCHAR:
711 case DATA_MYSQL:
712 case DATA_VARMYSQL:
713 /* Space is the padding character for all char and binary
714 strings, and starting from 5.0.3, also for TEXT strings. */
715 return (0x20);
716 case DATA_GEOMETRY:
717 /* DATA_GEOMETRY is binary data, not ASCII-based. */
718 return (ULINT_UNDEFINED);
719 case DATA_BLOB:
720 if (!(type->prtype & DATA_BINARY_TYPE)) {
721 return (0x20);
722 }
723 [[fallthrough]];
724 default:
725 /* No padding specified */
726 return (ULINT_UNDEFINED);
727 }
728 }
729
730 int cmp_dtuple_rec_with_match_bytes(const dtuple_t *dtuple, const rec_t *rec,
731 const dict_index_t *index,
732 const ulint *offsets, ulint *matched_fields,
733 ulint *matched_bytes) {
734 ulint n_cmp = dtuple_get_n_fields_cmp(dtuple);
735 ulint cur_field; /* current field number */
736 ulint cur_bytes;
737 int ret; /* return value */
738
739 ut_ad(dtuple_check_typed(dtuple));
740 ut_ad(rec_offs_validate(rec, index, offsets));
741 ut_ad(!(REC_INFO_MIN_REC_FLAG & dtuple_get_info_bits(dtuple)));
742 ut_ad(!(REC_INFO_MIN_REC_FLAG &
743 rec_get_info_bits(rec, rec_offs_comp(offsets))));
744
745 cur_field = *matched_fields;
746 cur_bytes = *matched_bytes;
747
748 ut_ad(n_cmp <= dtuple_get_n_fields(dtuple));
749 ut_ad(cur_field <= n_cmp);
750 ut_ad(cur_field + (cur_bytes > 0) <= rec_offs_n_fields(offsets));
751
752 /* Match fields in a loop; stop if we run out of fields in dtuple
753 or find an externally stored field */
754
755 while (cur_field < n_cmp) {
756 const dfield_t *dfield = dtuple_get_nth_field(dtuple, cur_field);
757 const dtype_t *type = dfield_get_type(dfield);
758 ulint dtuple_f_len = dfield_get_len(dfield);
759 const byte *dtuple_b_ptr;
760 const byte *rec_b_ptr;
761 ulint rec_f_len;
762
763 ut_ad(!rec_offs_nth_default(index, offsets, cur_field));
764
765 /* For now, change buffering is only supported on
766 indexes with ascending order on the columns. */
767 const bool is_ascending =
768 dict_index_is_ibuf(index) || index->fields[cur_field].is_ascending;
769
770 dtuple_b_ptr = static_cast<const byte *>(dfield_get_data(dfield));
771 rec_b_ptr = rec_get_nth_field(index, rec, offsets, cur_field, &rec_f_len);
772 ut_ad(!rec_offs_nth_extern(index, offsets, cur_field));
773
774 /* If we have matched yet 0 bytes, it may be that one or
775 both the fields are SQL null, or the record or dtuple may be
776 the predefined minimum record. */
777 if (cur_bytes == 0) {
778 if (dtuple_f_len == UNIV_SQL_NULL) {
779 if (rec_f_len == UNIV_SQL_NULL) {
780 goto next_field;
781 }
782
783 ret = is_ascending ? -1 : 1;
784 goto order_resolved;
785 } else if (rec_f_len == UNIV_SQL_NULL) {
786 /* We define the SQL null to be the
787 smallest possible value of a field
788 in the alphabetical order */
789
790 ret = is_ascending ? 1 : -1;
791 goto order_resolved;
792 }
793 }
794
795 switch (type->mtype) {
796 case DATA_FIXBINARY:
797 case DATA_BINARY:
798 case DATA_INT:
799 case DATA_SYS_CHILD:
800 case DATA_SYS:
801 break;
802 case DATA_BLOB:
803 if (type->prtype & DATA_BINARY_TYPE) {
804 break;
805 }
806 [[fallthrough]];
807 default:
808 ut_ad(!(dfield_is_multi_value(dfield) &&
809 dtuple_f_len == UNIV_MULTI_VALUE_ARRAY_MARKER));
810 ret = cmp_data(type->mtype, type->prtype, is_ascending, dtuple_b_ptr,
811 dtuple_f_len, rec_b_ptr, rec_f_len);
812
813 if (!ret) {
814 goto next_field;
815 }
816
817 cur_bytes = 0;
818 goto order_resolved;
819 }
820
821 /* Set the pointers at the current byte */
822
823 rec_b_ptr += cur_bytes;
824 dtuple_b_ptr += cur_bytes;
825 /* Compare then the fields */
826
827 for (const ulint pad = cmp_get_pad_char(type);; cur_bytes++) {
828 ulint rec_byte = pad;
829 ulint dtuple_byte = pad;
830
831 if (rec_f_len <= cur_bytes) {
832 if (dtuple_f_len <= cur_bytes) {
833 goto next_field;
834 }
835
836 if (rec_byte == ULINT_UNDEFINED) {
837 ret = is_ascending ? 1 : -1;
838 goto order_resolved;
839 }
840 } else {
841 rec_byte = *rec_b_ptr++;
842 }
843
844 if (dtuple_f_len <= cur_bytes) {
845 if (dtuple_byte == ULINT_UNDEFINED) {
846 ret = is_ascending ? -1 : 1;
847 goto order_resolved;
848 }
849 } else {
850 dtuple_byte = *dtuple_b_ptr++;
851 }
852
853 if (dtuple_byte < rec_byte) {
854 ret = is_ascending ? -1 : 1;
855 goto order_resolved;
856 } else if (dtuple_byte > rec_byte) {
857 ret = is_ascending ? 1 : -1;
858 goto order_resolved;
859 }
860 }
861
862 next_field:
863 cur_field++;
864 cur_bytes = 0;
865 }
866
867 ut_ad(cur_bytes == 0);
868
869 ret = 0; /* If we ran out of fields, dtuple was equal to rec
870 up to the common fields */
871 order_resolved:
872 *matched_fields = cur_field;
873 *matched_bytes = cur_bytes;
874
875 return (ret);
876 }
877
878 int cmp_dtuple_rec(const dtuple_t *dtuple, const rec_t *rec,
879 const dict_index_t *index, const ulint *offsets) {
880 ulint matched_fields = 0;
881
882 return (dtuple->compare(rec, index, offsets, &matched_fields));
883 }
884
885 bool cmp_dtuple_is_prefix_of_rec(const dtuple_t *dtuple, const rec_t *rec,
886 const dict_index_t *index,
887 const ulint *offsets) {
888 ut_ad(!dict_index_is_spatial(index));
889
890 auto n_fields = dtuple_get_n_fields(dtuple);
891
892 if (n_fields > rec_offs_n_fields(offsets)) {
893 ut_d(ut_error);
894 ut_o(return (false));
895 }
896
897 ulint matched_fields = 0;
898
899 return (dtuple->compare(rec, index, offsets, &matched_fields) == 0);
900 }
901
902 /** Compare two physical record fields.
903 @param[in] rec1 Physical record.
904 @param[in] rec2 Physical record.
905 @param[in] offsets1 rec_get_offsets(rec1, ...).
906 @param[in] offsets2 rec_get_offsets(rec2, ...).
907 @param[in] index Data dictionary index.
908 @param[in] n Field to compare.
909 @retval positive if rec1 field is greater than rec2
910 @retval negative if rec1 field is less than rec2
911 @retval 0 if rec1 field equals to rec2 */
912 [[nodiscard]] static int cmp_rec_rec_simple_field(
913 const rec_t *rec1, const rec_t *rec2, const ulint *offsets1,
914 const ulint *offsets2, const dict_index_t *index, ulint n) {
915 ulint rec1_f_len;
916 ulint rec2_f_len;
917 const byte *rec1_b_ptr;
918 const byte *rec2_b_ptr;
919 const dict_col_t *col = index->get_col(n);
920 const dict_field_t *field = index->get_field(n);
921
922 ut_ad(!rec_offs_nth_extern(index, offsets1, n));
923 ut_ad(!rec_offs_nth_extern(index, offsets2, n));
924
925 rec1_b_ptr = rec_get_nth_field_instant(rec1, offsets1, n, index, &rec1_f_len);
926 rec2_b_ptr = rec_get_nth_field_instant(rec2, offsets2, n, index, &rec2_f_len);
927
928 return (cmp_data(col->mtype, col->prtype, field->is_ascending, rec1_b_ptr,
929 rec1_f_len, rec2_b_ptr, rec2_f_len));
930 }
931
932 int cmp_rec_rec_simple(const rec_t *rec1, const rec_t *rec2,
933 const ulint *offsets1, const ulint *offsets2,
934 const dict_index_t *index, TABLE *table) {
935 ulint n;
936 bool null_eq{};
937 const auto n_uniq = dict_index_get_n_unique(index);
938
939 ut_ad(rec_offs_n_fields(offsets1) >= n_uniq);
940 ut_ad(rec_offs_comp(offsets1) == rec_offs_comp(offsets2));
941 ut_ad(rec_offs_n_fields(offsets2) == rec_offs_n_fields(offsets2));
942
943 for (n = 0; n < n_uniq; n++) {
944 auto r = cmp_rec_rec_simple_field(rec1, rec2, offsets1, offsets2, index, n);
945
946 if (r != 0) {
947 return r;
948 }
949
950 /* If the fields are internally equal, they must both
951 be NULL or non-NULL. */
952 ut_ad(rec_offs_nth_sql_null(index, offsets1, n) ==
953 rec_offs_nth_sql_null(index, offsets2, n));
954
955 if (rec_offs_nth_sql_null(index, offsets1, n)) {
956 ut_ad(!(index->get_col(n)->prtype & DATA_NOT_NULL));
957 null_eq = true;
958 }
959 }
960
961 /* If we ran out of fields, the ordering columns of rec1 were
962 equal to rec2. Issue a duplicate key error if needed. */
963
964 if (!null_eq && table && dict_index_is_unique(index)) {
965 /* Report erroneous row using new version of table. */
966 innobase_rec_to_mysql(table, rec1, index, offsets1);
967 return 0;
968 }
969
970 /* Else, keep comparing so that we have the full internal order. */
971 for (; n < dict_index_get_n_fields(index); n++) {
972 auto r = cmp_rec_rec_simple_field(rec1, rec2, offsets1, offsets2, index, n);
973
974 if (r != 0) {
975 return r;
976 }
977
978 /* If the fields are equal, they must both be NULL or non-NULL. */
979 ut_ad(rec_offs_nth_sql_null(index, offsets1, n) ==
980 rec_offs_nth_sql_null(index, offsets2, n));
981 }
982
983 return 0;
984 }
985
986 int cmp_rec_rec_with_match(const rec_t *rec1, const rec_t *rec2,
987 const ulint *offsets1, const ulint *offsets2,
988 const dict_index_t *index,
989 bool spatial_index_non_leaf, bool nulls_unequal,
990 ulint *matched_fields, bool cmp_btree_recs) {
991 ut_ad(rec1 != nullptr);
992 ut_ad(rec2 != nullptr);
993 ut_ad(index != nullptr);
994 ut_ad(rec_offs_validate(rec1, index, offsets1));
995 ut_ad(rec_offs_validate(rec2, index, offsets2));
996 ut_ad(rec_offs_comp(offsets1) == rec_offs_comp(offsets2));
997
998 const auto comp = rec_offs_comp(offsets1);
999 const auto rec1_n_fields = rec_offs_n_fields(offsets1);
1000 const auto rec2_n_fields = rec_offs_n_fields(offsets2);
1001
1002 *matched_fields = 0;
1003
1004 /* NOTE: This is an optimisation where we're comparing two B-tree records
1005 during index validation. */
1006 if (cmp_btree_recs) {
1007 /* Test if rec is the predefined minimum record */
1008 if (rec_get_info_bits(rec1, comp) & REC_INFO_MIN_REC_FLAG) {
1009 ut_ad(!(rec_get_info_bits(rec2, comp) & REC_INFO_MIN_REC_FLAG));
1010 return (-1);
1011 } else if (rec_get_info_bits(rec2, comp) & REC_INFO_MIN_REC_FLAG) {
1012 return (1);
1013 }
1014 }
1015
1016 ulint i;
1017
1018 for (i = 0; i < rec1_n_fields && i < rec2_n_fields; ++i) {
1019 /* If this is node-ptr records then avoid comparing node-ptr
1020 field. Only key field needs to be compared. In case of a
1021 spatial index we need to compare the node-ptr for a non-leaf
1022 page */
1023 if (i == dict_index_get_n_unique_in_tree(index)) {
1024 *matched_fields = i;
1025 return (0);
1026 }
1027
1028 ulint mtype;
1029 ulint prtype;
1030 bool is_asc;
1031
1032 if (dict_index_is_ibuf(index)) {
1033 /* This is for the insert buffer B-tree. */
1034 mtype = DATA_BINARY;
1035 prtype = 0;
1036 is_asc = true;
1037 } else if ((i == 1) && spatial_index_non_leaf) {
1038 /* When the page is non-leaf spatial index page we should
1039 not depend upon the dictionary information because the
1040 page doesn't hold any primary key information.The spatial
1041 non-leaf has only two fields MBR and page number to child
1042 node.*/
1043 mtype = DATA_SYS_CHILD;
1044 prtype = 0;
1045 is_asc = true;
1046 } else {
1047 auto col = index->get_col(i);
1048 const auto field = index->get_field(i);
1049
1050 ut_ad(col == field->col);
1051
1052 mtype = col->mtype;
1053 prtype = col->prtype;
1054 is_asc = field->is_ascending;
1055 }
1056
1057 /* If the index is spatial index, we mark the
1058 prtype of the first field as MBR field. */
1059 if (i == 0 && dict_index_is_spatial(index)) {
1060 ut_ad(DATA_GEOMETRY_MTYPE(mtype));
1061 prtype |= DATA_GIS_MBR;
1062 }
1063
1064 /* We should never encounter an externally stored field.
1065 Externally stored fields only exist in clustered index
1066 leaf page records. These fields should already differ
1067 in the primary key columns already, before DB_TRX_ID,
1068 DB_ROLL_PTR, and any externally stored columns. */
1069 ut_ad(!rec_offs_nth_extern(index, offsets1, i));
1070 ut_ad(!rec_offs_nth_extern(index, offsets2, i));
1071
1072 ulint r1_len;
1073
1074 const auto r1 =
1075 rec_get_nth_field_instant(rec1, offsets1, i, index, &r1_len);
1076
1077 ulint r2_len;
1078
1079 const auto r2 =
1080 rec_get_nth_field_instant(rec2, offsets2, i, index, &r2_len);
1081
1082 if (nulls_unequal && r1_len == UNIV_SQL_NULL && r2_len == UNIV_SQL_NULL) {
1083 *matched_fields = i;
1084 return (-1);
1085 }
1086
1087 auto ret = cmp_data(mtype, prtype, is_asc, r1, r1_len, r2, r2_len);
1088
1089 if (ret != 0) {
1090 *matched_fields = i;
1091 return (ret);
1092 }
1093 }
1094
1095 /* If we ran out of fields, rec1 was equal to rec2 up to the common fields */
1096 *matched_fields = i;
1097
1098 return (0);
1099 }
1100
1101 #ifdef UNIV_COMPILE_TEST_FUNCS
1102
1103 #ifdef HAVE_UT_CHRONO_T
1104
1105 void test_cmp_data_data(ulint len) {
1106 static byte zeros[64];
1107
1108 if (len > sizeof(zeros)) {
1109 len = sizeof(zeros);
1110 }
1111
1112 ut_chrono_t ch(__func__);
1113
1114 for (int i = 1000000; i > 0; i--) {
1115 i += cmp_data(DATA_INT, 0, zeros, len, zeros, len);
1116 }
1117 }
1118
1119 #endif /* HAVE_UT_CHRONO_T */
1120
1121 #endif /* UNIV_COMPILE_TEST_FUNCS */
1122
1123 int dtuple_t::compare(const rec_t *rec, const dict_index_t *index,
1124 const ulint *offsets, ulint *matched_fields) const {
1125 return (cmp_dtuple_rec_with_match_low(this, rec, index, offsets, n_fields_cmp,
1126 matched_fields));
1127 }