MySQL 26.10.0
Source Code Documentation
ddl0impl-compare.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2020, 2026, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/ddl0impl-compare.h
29 DDL key comparison.
30 Created 2020-11-01 by Sunny Bains. */
31
32#ifndef ddl0impl_compare_h
33#define ddl0impl_compare_h
34
35#include "ddl0impl.h"
36#include "rem0cmp.h"
37
38namespace ddl {
39
40/** Compare the keys of an index. */
42 /** Constructor.
43 @param[in] index Compare the keys of this index.
44 @param[in,out] dups For capturing the duplicate entries.
45 @param[in] compare_all If set, compare all columns in the key. */
46 Compare_key(const dict_index_t *index, Dup *dups, bool compare_all) noexcept
47 : m_dups(dups),
50 m_fields(index->fields) {
52 ut_a(m_dups == nullptr || index == m_dups->m_index);
53 }
54
55 Compare_key(const Compare_key &) = default;
56
57 /** Destructor. */
58 ~Compare_key() = default;
59
60 /** Compare two tuples.
61 @param[in] lhs Tuple to compare on the left hand side
62 @param[in] rhs Tuple to compare on the Right hand side
63 @retval +ve - if lhs > rhs
64 @retval -ve - if lhs < rhs
65 @retval 0 - if lhs == rhs */
66 int operator()(const dfield_t *lhs, const dfield_t *rhs) const noexcept {
67 auto f = m_fields;
68 auto lhs_f = lhs;
69 auto rhs_f = rhs;
70 auto n = m_n_unique;
71
72 ut_a(n > 0);
73
74 /* Compare the fields of the tuples until a difference is
75 found or we run out of fields to compare. If cmp == 0 at
76 the end, then the tuples are equal. */
77 int cmp;
78
79 do {
80 cmp = cmp_dfield_dfield(lhs_f++, rhs_f++, (f++)->is_ascending);
81 } while (cmp == 0 && --n);
82
83 if (cmp != 0) {
84 return cmp;
85 }
86
87 if (m_dups != nullptr) {
88 bool report{true};
89
90 /* Report a duplicate value error if the tuples are
91 logically equal. nullptr columns are logically inequal,
92 although they are equal in the sorting order. Find
93 out if any of the fields are nullptr. */
94 for (auto df = lhs; df != lhs_f; ++df) {
95 if (dfield_is_null(df)) {
96 report = false;
97 break;
98 }
99 }
100
101 /* For a non-unique secondary index, m_n_unique includes the
102 clustered-key fields and is equal to m_n_fields. If all of those
103 fields match, the records have the same clustered key even if a
104 secondary-key field is NULL. */
105 if (report || m_n_unique == m_n_fields) {
107 }
108 }
109
110 /* The m_n_unique fields were equal, but we compare all fields so
111 that we will get the same (internal) order as in the B-tree. */
112 for (auto n = m_n_fields - m_n_unique + 1; --n;) {
113 cmp = cmp_dfield_dfield(lhs_f++, rhs_f++, (f++)->is_ascending);
114 if (cmp != 0) {
115 return cmp;
116 }
117 }
118
119 /* Creating a secondary index and a PRIMARY KEY and there is a duplicate
120 in the PRIMARY KEY that has not been detected yet. Internally, an index
121 must never contain duplicates. */
122 return cmp;
123 }
124
125 /** For collecting duplicates. */
127
128 /** Number of unique fields in the index key. */
129 const size_t m_n_unique{};
130
131 /** Total number of fields in the index key. */
132 const size_t m_n_fields{};
133
134 /** Index key fields. */
136};
137
138} // namespace ddl
139
140#endif /* !ddl0impl_compare_h */
static ulint dfield_is_null(const dfield_t *field)
Determines if a field is SQL NULL.
DDL implementation include file.
uint16_t dict_index_get_n_fields(const dict_index_t *index)
Gets the number of fields in the internal representation of an index, including fields added by the d...
Definition: dict0dict.ic:545
uint16_t dict_index_get_n_unique(const dict_index_t *index)
Gets the number of fields in the internal representation of an index that uniquely determine the posi...
Definition: dict0dict.ic:565
static int cmp(Bigint *a, Bigint *b)
Definition: dtoa.cc:1064
The general architecture is that the work is done in two phases, roughly the read and write phase.
Definition: btr0load.cc:45
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:76
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
Comparison services for records.
static int cmp_dfield_dfield(const dfield_t *dfield1, const dfield_t *dfield2, bool is_asc)
Compare two data fields.
Compare the keys of an index.
Definition: ddl0impl-compare.h:41
const dict_field_t * m_fields
Index key fields.
Definition: ddl0impl-compare.h:135
const size_t m_n_fields
Total number of fields in the index key.
Definition: ddl0impl-compare.h:132
Compare_key(const Compare_key &)=default
Dup * m_dups
For collecting duplicates.
Definition: ddl0impl-compare.h:126
int operator()(const dfield_t *lhs, const dfield_t *rhs) const noexcept
Compare two tuples.
Definition: ddl0impl-compare.h:66
const size_t m_n_unique
Number of unique fields in the index key.
Definition: ddl0impl-compare.h:129
~Compare_key()=default
Destructor.
Compare_key(const dict_index_t *index, Dup *dups, bool compare_all) noexcept
Constructor.
Definition: ddl0impl-compare.h:46
Structure for reporting duplicate records.
Definition: ddl0ddl.h:132
dict_index_t * m_index
Index being sorted.
Definition: ddl0ddl.h:181
void save_duplicate(const dfield_t *entry)
Save the duplicate tuple information.
Definition: ddl0ddl.h:175
Structure for an SQL data field.
Definition: data0data.h:625
Data structure for a field in an index.
Definition: dict0mem.h:891
Data structure for an index.
Definition: dict0mem.h:1069
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:97
int n
Definition: xcom_base.cc:509