| 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/rem0cmp.ic |
| 29 |
Comparison services for records |
| 30 |
|
| 31 |
Created 7/1/1994 Heikki Tuuri |
| 32 |
************************************************************************/ |
| 33 |
|
| 34 |
#include <mysql_com.h> |
| 35 |
|
| 36 |
#include "field_types.h" // enum_field_types |
| 37 |
|
| 38 |
/** Compare two data fields. |
| 39 |
@param[in] dfield1 data field; must have type field set |
| 40 |
@param[in] dfield2 data field |
| 41 |
@param[in] is_asc true=ASC, false=DESC |
| 42 |
@return the comparison result of dfield1 and dfield2 |
| 43 |
@retval 0 if dfield1 is equal to dfield2 |
| 44 |
@retval negative if dfield1 is less than dfield2 |
| 45 |
@retval positive if dfield1 is greater than dfield2 */ |
| 46 |
static inline int cmp_dfield_dfield(const dfield_t *dfield1, |
| 47 |
const dfield_t *dfield2, bool is_asc) { |
| 48 |
const dtype_t *type; |
| 49 |
|
| 50 |
/* The multi-value array data is not expected, however, if it's one |
| 51 |
value of the array, then it's fine to compare it */ |
| 52 |
ut_ad(!(dfield_is_multi_value(dfield1) && |
| 53 |
(dfield_get_len(dfield1) == UNIV_MULTI_VALUE_ARRAY_MARKER || |
| 54 |
dfield_get_len(dfield1) == UNIV_NO_INDEX_VALUE))); |
| 55 |
ut_ad(!(dfield_is_multi_value(dfield2) && |
| 56 |
(dfield_get_len(dfield2) == UNIV_MULTI_VALUE_ARRAY_MARKER || |
| 57 |
dfield_get_len(dfield2) == UNIV_NO_INDEX_VALUE))); |
| 58 |
ut_ad(dfield_check_typed(dfield1)); |
| 59 |
|
| 60 |
type = dfield_get_type(dfield1); |
| 61 |
|
| 62 |
return (cmp_data_data( |
| 63 |
type->mtype, type->prtype, is_asc, (const byte *)dfield_get_data(dfield1), |
| 64 |
dfield_get_len(dfield1), (const byte *)dfield_get_data(dfield2), |
| 65 |
dfield_get_len(dfield2))); |
| 66 |
} |
| 67 |
|
| 68 |
static inline int cmp_multi_value_dfield_dfield(const dfield_t *dfield1, |
| 69 |
const dfield_t *dfield2) { |
| 70 |
ut_ad(dfield_is_multi_value(dfield1)); |
| 71 |
ut_ad(dfield_is_multi_value(dfield2)); |
| 72 |
ut_ad(dfield_is_null(dfield2) || |
| 73 |
(dfield_get_len(dfield2) != UNIV_MULTI_VALUE_ARRAY_MARKER && |
| 74 |
dfield_get_len(dfield2) != UNIV_NO_INDEX_VALUE)); |
| 75 |
|
| 76 |
if (dfield_is_null(dfield1)) { |
| 77 |
return (dfield_is_null(dfield2) ? 0 : 1); |
| 78 |
} else if (dfield_is_null(dfield2)) { |
| 79 |
return (1); |
| 80 |
} |
| 81 |
|
| 82 |
if (dfield1->len == UNIV_NO_INDEX_VALUE) { |
| 83 |
/* Nothing to be indexed, of course nothing match */ |
| 84 |
return (1); |
| 85 |
} |
| 86 |
|
| 87 |
ut_ad(dfield1->len == UNIV_MULTI_VALUE_ARRAY_MARKER); |
| 88 |
|
| 89 |
multi_value_data *multi_value = |
| 90 |
reinterpret_cast<multi_value_data *>(dfield_get_data(dfield1)); |
| 91 |
|
| 92 |
return (multi_value->has(dfield_get_type(dfield2), |
| 93 |
static_cast<const byte *>(dfield_get_data(dfield2)), |
| 94 |
dfield_get_len(dfield2)) |
| 95 |
? 0 |
| 96 |
: 1); |
| 97 |
} |
| 98 |
|
| 99 |
static inline int cmp_rec_rec(const rec_t *rec1, const rec_t *rec2, |
| 100 |
const ulint *offsets1, const ulint *offsets2, |
| 101 |
const dict_index_t *index, |
| 102 |
bool spatial_index_non_leaf, |
| 103 |
ulint *matched_fields, bool cmp_btree_recs) { |
| 104 |
ulint match_f; |
| 105 |
int ret; |
| 106 |
|
| 107 |
ret = cmp_rec_rec_with_match(rec1, rec2, offsets1, offsets2, index, |
| 108 |
spatial_index_non_leaf, false, &match_f, |
| 109 |
cmp_btree_recs); |
| 110 |
|
| 111 |
if (matched_fields != nullptr) { |
| 112 |
*matched_fields = match_f; |
| 113 |
} |
| 114 |
|
| 115 |
return (ret); |
| 116 |
} |
| 117 |
|
| 118 |
#ifndef UNIV_HOTBACKUP |
| 119 |
/** Compare two data fields. |
| 120 |
@param[in] dfield1 data field |
| 121 |
@param[in] dfield2 data field |
| 122 |
@return the comparison result of dfield1 and dfield2 |
| 123 |
@retval true if dfield1 is equal to dfield2, or a prefix of dfield1 |
| 124 |
@retval false otherwise */ |
| 125 |
static inline bool cmp_dfield_dfield_eq_prefix(const dfield_t *dfield1, |
| 126 |
const dfield_t *dfield2) { |
| 127 |
const dtype_t *type; |
| 128 |
|
| 129 |
ut_ad(dfield_check_typed(dfield1)); |
| 130 |
ut_ad(dfield_check_typed(dfield2)); |
| 131 |
|
| 132 |
type = dfield_get_type(dfield1); |
| 133 |
|
| 134 |
#ifdef UNIV_DEBUG |
| 135 |
switch (type->prtype & DATA_MYSQL_TYPE_MASK) { |
| 136 |
case MYSQL_TYPE_BIT: |
| 137 |
case MYSQL_TYPE_STRING: |
| 138 |
case MYSQL_TYPE_VAR_STRING: |
| 139 |
case MYSQL_TYPE_TINY_BLOB: |
| 140 |
case MYSQL_TYPE_MEDIUM_BLOB: |
| 141 |
case MYSQL_TYPE_BLOB: |
| 142 |
case MYSQL_TYPE_LONG_BLOB: |
| 143 |
case MYSQL_TYPE_VARCHAR: |
| 144 |
break; |
| 145 |
default: |
| 146 |
ut_error; |
| 147 |
} |
| 148 |
#endif /* UNIV_DEBUG */ |
| 149 |
|
| 150 |
uint cs_num = (uint)dtype_get_charset_coll(type->prtype); |
| 151 |
|
| 152 |
if (CHARSET_INFO *cs = get_charset(cs_num, MYF(MY_WME))) { |
| 153 |
return (!cs->coll->strnncoll( |
| 154 |
cs, static_cast<uchar *>(dfield_get_data(dfield1)), |
| 155 |
dfield_get_len(dfield1), static_cast<uchar *>(dfield_get_data(dfield2)), |
| 156 |
dfield_get_len(dfield2), true)); |
| 157 |
} |
| 158 |
|
| 159 |
#ifdef UNIV_NO_ERR_MSGS |
| 160 |
ib::fatal(UT_LOCATION_HERE) |
| 161 |
#else |
| 162 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_627) |
| 163 |
#endif /* !UNIV_NO_ERR_MSGS */ |
| 164 |
<< "Unable to find charset-collation " << cs_num; |
| 165 |
|
| 166 |
return (false); |
| 167 |
} |
| 168 |
#endif /* UNIV_HOTBACKUP */ |