1/*****************************************************************************
2
3Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
4Copyright (c) 2017, MariaDB Corporation.
5
6This program is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free Software
8Foundation; version 2 of the License.
9
10This program is distributed in the hope that it will be useful, but WITHOUT
11ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program; if not, write to the Free Software Foundation, Inc.,
1651 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17
18*****************************************************************************/
19
20/*******************************************************************//**
21@file include/rem0cmp.h
22Comparison services for records
23
24Created 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/*************************************************************//**
38Returns TRUE if two columns are equal for comparison purposes.
39@return TRUE if the columns are considered equal in comparisons */
40ibool
41cmp_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 */
58int
59cmp_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 */
75UNIV_INLINE
76int
77cmp_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 */
89int
90cmp_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.
99We need to check the page number field, since we don't store pk field in
100rtree 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 */
106int
107cmp_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 */
122int
123cmp_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
140field 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 */
145int
146cmp_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 */
163int
164cmp_dtuple_rec(
165 const dtuple_t* dtuple,
166 const rec_t* rec,
167 const ulint* offsets);
168/**************************************************************//**
169Checks if a dtuple is a prefix of a record. The last field in dtuple
170is allowed to be a prefix of the corresponding field in the record.
171@return TRUE if prefix */
172ibool
173cmp_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,
179none 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 */
183int
184cmp_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
202statistics estimation, and innodb_stats_method=nulls_unequal
203or innodb_stats_method=nulls_ignored
204@param[out] matched_fields number of completely matched fields
205within 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 */
210int
211cmp_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.
221Only the common first fields are compared, and externally stored field
222are 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,
230respectively */
231UNIV_INLINE
232int
233cmp_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 */
248UNIV_INLINE
249int
250cmp_dfield_dfield_like_prefix(
251 const dfield_t* dfield1,
252 const dfield_t* dfield2);
253
254#include "rem0cmp.ic"
255
256#endif
257