| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved. |
| 4 | Copyright (c) 2017, MariaDB Corporation. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU General Public License as published by the Free Software |
| 8 | Foundation; version 2 of the License. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License along with |
| 15 | this program; if not, write to the Free Software Foundation, Inc., |
| 16 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
| 17 | |
| 18 | *****************************************************************************/ |
| 19 | |
| 20 | /*******************************************************************//** |
| 21 | @file include/rem0cmp.h |
| 22 | Comparison services for records |
| 23 | |
| 24 | Created 7/1/1994 Heikki Tuuri |
| 25 | ************************************************************************/ |
| 26 | |
| 27 | #ifndef rem0cmp_h |
| 28 | #define rem0cmp_h |
| 29 | |
| 30 | #include "ha_prototypes.h" |
| 31 | #include "data0data.h" |
| 32 | #include "data0type.h" |
| 33 | #include "dict0dict.h" |
| 34 | #include "rem0rec.h" |
| 35 | #include <my_sys.h> |
| 36 | |
| 37 | /*************************************************************//** |
| 38 | Returns TRUE if two columns are equal for comparison purposes. |
| 39 | @return TRUE if the columns are considered equal in comparisons */ |
| 40 | ibool |
| 41 | cmp_cols_are_equal( |
| 42 | /*===============*/ |
| 43 | const dict_col_t* col1, /*!< in: column 1 */ |
| 44 | const dict_col_t* col2, /*!< in: column 2 */ |
| 45 | ibool check_charsets); |
| 46 | /*!< in: whether to check charsets */ |
| 47 | /** Compare two data fields. |
| 48 | @param[in] mtype main type |
| 49 | @param[in] prtype precise type |
| 50 | @param[in] data1 data field |
| 51 | @param[in] len1 length of data1 in bytes, or UNIV_SQL_NULL |
| 52 | @param[in] data2 data field |
| 53 | @param[in] len2 length of data2 in bytes, or UNIV_SQL_NULL |
| 54 | @return the comparison result of data1 and data2 |
| 55 | @retval 0 if data1 is equal to data2 |
| 56 | @retval negative if data1 is less than data2 |
| 57 | @retval positive if data1 is greater than data2 */ |
| 58 | int |
| 59 | cmp_data_data( |
| 60 | ulint mtype, |
| 61 | ulint prtype, |
| 62 | const byte* data1, |
| 63 | ulint len1, |
| 64 | const byte* data2, |
| 65 | ulint len2) |
| 66 | MY_ATTRIBUTE((warn_unused_result)); |
| 67 | |
| 68 | /** Compare two data fields. |
| 69 | @param[in] dfield1 data field; must have type field set |
| 70 | @param[in] dfield2 data field |
| 71 | @return the comparison result of dfield1 and dfield2 |
| 72 | @retval 0 if dfield1 is equal to dfield2 |
| 73 | @retval negative if dfield1 is less than dfield2 |
| 74 | @retval positive if dfield1 is greater than dfield2 */ |
| 75 | UNIV_INLINE |
| 76 | int |
| 77 | cmp_dfield_dfield( |
| 78 | /*==============*/ |
| 79 | const dfield_t* dfield1,/*!< in: data field; must have type field set */ |
| 80 | const dfield_t* dfield2);/*!< in: data field */ |
| 81 | |
| 82 | |
| 83 | /** Compare a GIS data tuple to a physical record. |
| 84 | @param[in] dtuple data tuple |
| 85 | @param[in] rec B-tree record |
| 86 | @param[in] offsets rec_get_offsets(rec) |
| 87 | @param[in] mode compare mode |
| 88 | @retval negative if dtuple is less than rec */ |
| 89 | int |
| 90 | cmp_dtuple_rec_with_gis( |
| 91 | /*====================*/ |
| 92 | const dtuple_t* dtuple, |
| 93 | const rec_t* rec, |
| 94 | const ulint* offsets, |
| 95 | page_cur_mode_t mode) |
| 96 | MY_ATTRIBUTE((nonnull)); |
| 97 | |
| 98 | /** Compare a GIS data tuple to a physical record in rtree non-leaf node. |
| 99 | We need to check the page number field, since we don't store pk field in |
| 100 | rtree non-leaf node. |
| 101 | @param[in] dtuple data tuple |
| 102 | @param[in] rec R-tree record |
| 103 | @param[in] offsets rec_get_offsets(rec) |
| 104 | @param[in] mode compare mode |
| 105 | @retval negative if dtuple is less than rec */ |
| 106 | int |
| 107 | cmp_dtuple_rec_with_gis_internal( |
| 108 | const dtuple_t* dtuple, |
| 109 | const rec_t* rec, |
| 110 | const ulint* offsets); |
| 111 | |
| 112 | /** Compare a data tuple to a physical record. |
| 113 | @param[in] dtuple data tuple |
| 114 | @param[in] rec B-tree record |
| 115 | @param[in] offsets rec_get_offsets(rec) |
| 116 | @param[in] n_cmp number of fields to compare |
| 117 | @param[in,out] matched_fields number of completely matched fields |
| 118 | @return the comparison result of dtuple and rec |
| 119 | @retval 0 if dtuple is equal to rec |
| 120 | @retval negative if dtuple is less than rec |
| 121 | @retval positive if dtuple is greater than rec */ |
| 122 | int |
| 123 | cmp_dtuple_rec_with_match_low( |
| 124 | const dtuple_t* dtuple, |
| 125 | const rec_t* rec, |
| 126 | const ulint* offsets, |
| 127 | ulint n_cmp, |
| 128 | ulint* matched_fields) |
| 129 | MY_ATTRIBUTE((nonnull)); |
| 130 | #define cmp_dtuple_rec_with_match(tuple,rec,offsets,fields) \ |
| 131 | cmp_dtuple_rec_with_match_low( \ |
| 132 | tuple,rec,offsets,dtuple_get_n_fields_cmp(tuple),fields) |
| 133 | /** Compare a data tuple to a physical record. |
| 134 | @param[in] dtuple data tuple |
| 135 | @param[in] rec B-tree or R-tree index record |
| 136 | @param[in] index index tree |
| 137 | @param[in] offsets rec_get_offsets(rec) |
| 138 | @param[in,out] matched_fields number of completely matched fields |
| 139 | @param[in,out] matched_bytes number of matched bytes in the first |
| 140 | field that is not matched |
| 141 | @return the comparison result of dtuple and rec |
| 142 | @retval 0 if dtuple is equal to rec |
| 143 | @retval negative if dtuple is less than rec |
| 144 | @retval positive if dtuple is greater than rec */ |
| 145 | int |
| 146 | cmp_dtuple_rec_with_match_bytes( |
| 147 | const dtuple_t* dtuple, |
| 148 | const rec_t* rec, |
| 149 | const dict_index_t* index, |
| 150 | const ulint* offsets, |
| 151 | ulint* matched_fields, |
| 152 | ulint* matched_bytes) |
| 153 | MY_ATTRIBUTE((warn_unused_result)); |
| 154 | /** Compare a data tuple to a physical record. |
| 155 | @see cmp_dtuple_rec_with_match |
| 156 | @param[in] dtuple data tuple |
| 157 | @param[in] rec B-tree record |
| 158 | @param[in] offsets rec_get_offsets(rec) |
| 159 | @return the comparison result of dtuple and rec |
| 160 | @retval 0 if dtuple is equal to rec |
| 161 | @retval negative if dtuple is less than rec |
| 162 | @retval positive if dtuple is greater than rec */ |
| 163 | int |
| 164 | cmp_dtuple_rec( |
| 165 | const dtuple_t* dtuple, |
| 166 | const rec_t* rec, |
| 167 | const ulint* offsets); |
| 168 | /**************************************************************//** |
| 169 | Checks if a dtuple is a prefix of a record. The last field in dtuple |
| 170 | is allowed to be a prefix of the corresponding field in the record. |
| 171 | @return TRUE if prefix */ |
| 172 | ibool |
| 173 | cmp_dtuple_is_prefix_of_rec( |
| 174 | /*========================*/ |
| 175 | const dtuple_t* dtuple, /*!< in: data tuple */ |
| 176 | const rec_t* rec, /*!< in: physical record */ |
| 177 | const ulint* offsets);/*!< in: array returned by rec_get_offsets() */ |
| 178 | /** Compare two physical records that contain the same number of columns, |
| 179 | none of which are stored externally. |
| 180 | @retval positive if rec1 (including non-ordering columns) is greater than rec2 |
| 181 | @retval negative if rec1 (including non-ordering columns) is less than rec2 |
| 182 | @retval 0 if rec1 is a duplicate of rec2 */ |
| 183 | int |
| 184 | cmp_rec_rec_simple( |
| 185 | /*===============*/ |
| 186 | const rec_t* rec1, /*!< in: physical record */ |
| 187 | const rec_t* rec2, /*!< in: physical record */ |
| 188 | const ulint* offsets1,/*!< in: rec_get_offsets(rec1, ...) */ |
| 189 | const ulint* offsets2,/*!< in: rec_get_offsets(rec2, ...) */ |
| 190 | const dict_index_t* index, /*!< in: data dictionary index */ |
| 191 | struct TABLE* table) /*!< in: MySQL table, for reporting |
| 192 | duplicate key value if applicable, |
| 193 | or NULL */ |
| 194 | MY_ATTRIBUTE((nonnull(1,2,3,4), warn_unused_result)); |
| 195 | /** Compare two B-tree records. |
| 196 | @param[in] rec1 B-tree record |
| 197 | @param[in] rec2 B-tree record |
| 198 | @param[in] offsets1 rec_get_offsets(rec1, index) |
| 199 | @param[in] offsets2 rec_get_offsets(rec2, index) |
| 200 | @param[in] index B-tree index |
| 201 | @param[in] nulls_unequal true if this is for index cardinality |
| 202 | statistics estimation, and innodb_stats_method=nulls_unequal |
| 203 | or innodb_stats_method=nulls_ignored |
| 204 | @param[out] matched_fields number of completely matched fields |
| 205 | within the first field not completely matched |
| 206 | @return the comparison result |
| 207 | @retval 0 if rec1 is equal to rec2 |
| 208 | @retval negative if rec1 is less than rec2 |
| 209 | @retval positive if rec2 is greater than rec2 */ |
| 210 | int |
| 211 | cmp_rec_rec_with_match( |
| 212 | const rec_t* rec1, |
| 213 | const rec_t* rec2, |
| 214 | const ulint* offsets1, |
| 215 | const ulint* offsets2, |
| 216 | const dict_index_t* index, |
| 217 | bool nulls_unequal, |
| 218 | ulint* matched_fields); |
| 219 | |
| 220 | /** Compare two B-tree records. |
| 221 | Only the common first fields are compared, and externally stored field |
| 222 | are treated as equal. |
| 223 | @param[in] rec1 B-tree record |
| 224 | @param[in] rec2 B-tree record |
| 225 | @param[in] offsets1 rec_get_offsets(rec1, index) |
| 226 | @param[in] offsets2 rec_get_offsets(rec2, index) |
| 227 | @param[out] matched_fields number of completely matched fields |
| 228 | within the first field not completely matched |
| 229 | @return positive, 0, negative if rec1 is greater, equal, less, than rec2, |
| 230 | respectively */ |
| 231 | UNIV_INLINE |
| 232 | int |
| 233 | cmp_rec_rec( |
| 234 | const rec_t* rec1, |
| 235 | const rec_t* rec2, |
| 236 | const ulint* offsets1, |
| 237 | const ulint* offsets2, |
| 238 | const dict_index_t* index, |
| 239 | ulint* matched_fields = NULL); |
| 240 | |
| 241 | /** Compare two data fields. |
| 242 | @param[in] dfield1 data field |
| 243 | @param[in] dfield2 data field |
| 244 | @return the comparison result of dfield1 and dfield2 |
| 245 | @retval 0 if dfield1 is equal to dfield2, or a prefix of dfield1 |
| 246 | @retval negative if dfield1 is less than dfield2 |
| 247 | @retval positive if dfield1 is greater than dfield2 */ |
| 248 | UNIV_INLINE |
| 249 | int |
| 250 | cmp_dfield_dfield_like_prefix( |
| 251 | const dfield_t* dfield1, |
| 252 | const dfield_t* dfield2); |
| 253 | |
| 254 | #include "rem0cmp.ic" |
| 255 | |
| 256 | #endif |
| 257 | |