| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2020, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 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 |
|
| 38 |
namespace ddl { |
| 39 |
|
| 40 |
/** Compare the keys of an index. */ |
| 41 |
struct Compare_key { |
| 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), |
| 48 |
m_n_unique(dict_index_get_n_unique(index)), |
| 49 |
m_n_fields(compare_all ? dict_index_get_n_fields(index) : m_n_unique), |
| 50 |
m_fields(index->fields) { |
| 51 |
ut_a(m_n_unique <= m_n_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. */ |
| 122 |
Dup *m_dups{}; |
| 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. */ |
| 131 |
const dict_field_t *m_fields{}; |
| 132 |
}; |
| 133 |
|
| 134 |
} // namespace ddl |
| 135 |
|
| 136 |
#endif /* !ddl0impl_compare_h */ |