MySQL 8.0.39
Source Code Documentation
ddl0impl-compare.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2020, 2024, 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),
49 m_n_fields(compare_all ? dict_index_get_n_fields(index) : m_n_unique),
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 if (report) {
102 m_dups->report(lhs);
103 }
104 }
105
106 /* The m_n_unique fields were equal, but we compare all fields so
107 that we will get the same (internal) order as in the B-tree. */
108 for (auto n = m_n_fields - m_n_unique + 1; --n;) {
109 cmp = cmp_dfield_dfield(lhs_f++, rhs_f++, (f++)->is_ascending);
110 if (cmp != 0) {
111 return cmp;
112 }
113 }
114
115 /* Creating a secondary index and a PRIMARY KEY and there is a duplicate
116 in the PRIMARY KEY that has not been detected yet. Internally, an index
117 must never contain duplicates. */
118 return cmp;
119 }
120
121 /** For collecting duplicates. */
123
124 /** Number of unique fields in the index key. */
125 const size_t m_n_unique{};
126
127 /** Total number of fields in the index key. */
128 const size_t m_n_fields{};
129
130 /** Index key fields. */
132};
133
134} // namespace ddl
135
136#endif /* !ddl0impl_compare_h */
static ulint dfield_is_null(const dfield_t *field)
Determines if a field is SQL NULL.
DDL implementation include file.
static ulint 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...
static ulint 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...
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:42
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:131
const size_t m_n_fields
Total number of fields in the index key.
Definition: ddl0impl-compare.h:128
Compare_key(const Compare_key &)=default
Dup * m_dups
For collecting duplicates.
Definition: ddl0impl-compare.h:122
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:125
~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:146
void report(const dfield_t *entry) noexcept
Report a duplicate key.
Definition: ddl0ddl.cc:83
Structure for an SQL data field.
Definition: data0data.h:605
Data structure for a field in an index.
Definition: dict0mem.h:895
Data structure for an index.
Definition: dict0mem.h:1046
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:57
int n
Definition: xcom_base.cc:509