MySQL 8.0.31
Source Code Documentation
ddl0impl-compare.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2020, 2022, 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 also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/** @file include/ddl0impl-compare.h
28 DDL key comparison.
29 Created 2020-11-01 by Sunny Bains. */
30
31#ifndef ddl0impl_compare_h
32#define ddl0impl_compare_h
33
34#include "ddl0impl.h"
35#include "rem0cmp.h"
36
37namespace ddl {
38
39/** Compare the keys of an index. */
41 /** Constructor.
42 @param[in] index Compare the keys of this index.
43 @param[in,out] dups For capturing the duplicate entries.
44 @param[in] compare_all If set, compare all columns in the key. */
45 Compare_key(const dict_index_t *index, Dup *dups, bool compare_all) noexcept
46 : m_dups(dups),
48 m_n_fields(compare_all ? dict_index_get_n_fields(index) : m_n_unique),
49 m_fields(index->fields) {
51 ut_a(m_dups == nullptr || index == m_dups->m_index);
52 }
53
54 Compare_key(const Compare_key &) = default;
55
56 /** Destructor. */
57 ~Compare_key() = default;
58
59 /** Compare two tuples.
60 @param[in] lhs Tuple to compare on the left hand side
61 @param[in] rhs Tuple to compare on the Right hand side
62 @retval +ve - if lhs > rhs
63 @retval -ve - if lhs < rhs
64 @retval 0 - if lhs == rhs */
65 int operator()(const dfield_t *lhs, const dfield_t *rhs) const noexcept {
66 auto f = m_fields;
67 auto lhs_f = lhs;
68 auto rhs_f = rhs;
69 auto n = m_n_unique;
70
71 ut_a(n > 0);
72
73 /* Compare the fields of the tuples until a difference is
74 found or we run out of fields to compare. If cmp == 0 at
75 the end, then the tuples are equal. */
76 int cmp;
77
78 do {
79 cmp = cmp_dfield_dfield(lhs_f++, rhs_f++, (f++)->is_ascending);
80 } while (cmp == 0 && --n);
81
82 if (cmp != 0) {
83 return cmp;
84 }
85
86 if (m_dups != nullptr) {
87 bool report{true};
88
89 /* Report a duplicate value error if the tuples are
90 logically equal. nullptr columns are logically inequal,
91 although they are equal in the sorting order. Find
92 out if any of the fields are nullptr. */
93 for (auto df = lhs; df != lhs_f; ++df) {
94 if (dfield_is_null(df)) {
95 report = false;
96 break;
97 }
98 }
99
100 if (report) {
101 m_dups->report(lhs);
102 }
103 }
104
105 /* The m_n_unique fields were equal, but we compare all fields so
106 that we will get the same (internal) order as in the B-tree. */
107 for (auto n = m_n_fields - m_n_unique + 1; --n;) {
108 cmp = cmp_dfield_dfield(lhs_f++, rhs_f++, (f++)->is_ascending);
109 if (cmp != 0) {
110 return cmp;
111 }
112 }
113
114 /* Creating a secondary index and a PRIMARY KEY and there is a duplicate
115 in the PRIMARY KEY that has not been detected yet. Internally, an index
116 must never contain duplicates. */
117 return cmp;
118 }
119
120 /** For collecting duplicates. */
122
123 /** Number of unique fields in the index key. */
124 const size_t m_n_unique{};
125
126 /** Total number of fields in the index key. */
127 const size_t m_n_fields{};
128
129 /** Index key fields. */
131};
132
133} // namespace ddl
134
135#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:41
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:40
const dict_field_t * m_fields
Index key fields.
Definition: ddl0impl-compare.h:130
const size_t m_n_fields
Total number of fields in the index key.
Definition: ddl0impl-compare.h:127
Compare_key(const Compare_key &)=default
Dup * m_dups
For collecting duplicates.
Definition: ddl0impl-compare.h:121
int operator()(const dfield_t *lhs, const dfield_t *rhs) const noexcept
Compare two tuples.
Definition: ddl0impl-compare.h:65
const size_t m_n_unique
Number of unique fields in the index key.
Definition: ddl0impl-compare.h:124
~Compare_key()=default
Destructor.
Compare_key(const dict_index_t *index, Dup *dups, bool compare_all) noexcept
Constructor.
Definition: ddl0impl-compare.h:45
Structure for reporting duplicate records.
Definition: ddl0ddl.h:131
dict_index_t * m_index
Index being sorted.
Definition: ddl0ddl.h:145
void report(const dfield_t *entry) noexcept
Report a duplicate key.
Definition: ddl0ddl.cc:82
Structure for an SQL data field.
Definition: data0data.h:604
Data structure for a field in an index.
Definition: dict0mem.h:870
Data structure for an index.
Definition: dict0mem.h:1021
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:56
int n
Definition: xcom_base.cc:505