Debian Codegraph @

unittest/gunit/temptable/cell_calculator-t.cc

raw symbols SCIP
1 /* Copyright (c) 2018, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <gtest/gtest.h>
25 #include <array>
26 #include <cstring>
27 #include <memory>
28
29 #include "my_byteorder.h"
30 #include "sql/table.h"
31 #include "storage/temptable/include/temptable/cell_calculator.h"
32
33 namespace temptable_test {
34
35 namespace {
36
37 class Table_wrapper {
38 public:
39 static const uint32 FIELD_SHORT_LENGTH = 2;
40 static const uint32 FIELD_STRING_PAD_LENGTH = 20 * 4;
41 static const uint32 FIELD_STRING_NOPAD_LENGTH = 20 * 4;
42
43 Table_wrapper();
44
45 Field_short *get_field_short();
46
47 Field_string *get_field_string_pad();
48
49 Field_string *get_field_string_nopad();
50
51 void store_short_value(std::array<uchar, FIELD_SHORT_LENGTH> &storage,
52 short value);
53
54 private:
55 static const size_t RECORD_SIZE = 1024;
56
57 static const char *FIELD_SHORT_NAME;
58 static const int FIELD_SHORT_OFFSET = 100;
59 static const int FIELD_SHORT_NULL_BIT = 0;
60
61 static const char *FIELD_STRING_PAD_NAME;
62 static const int FIELD_STRING_PAD_OFFSET = 200;
63 static const int FIELD_STRING_PAD_NULL_BIT = 1;
64
65 static const char *FIELD_STRING_NOPAD_NAME;
66 static const int FIELD_STRING_NOPAD_OFFSET = 300;
67 static const int FIELD_STRING_NOPAD_NULL_BIT = 2;
68
69 uint8 *get_data_ptr(int offset);
70
71 uint8 *get_null_ptr(int bit);
72
73 uchar get_null_bit(int bit);
74
75 TABLE m_table;
76 TABLE_SHARE m_table_share;
77 std::array<uchar, RECORD_SIZE> m_record0;
78 std::array<uchar, RECORD_SIZE> m_record1;
79
80 std::unique_ptr<Field_short> m_field_short;
81 std::unique_ptr<Field_string> m_field_string_pad;
82 std::unique_ptr<Field_string> m_field_string_nopad;
83 };
84
85 const char *Table_wrapper::FIELD_SHORT_NAME = "Short";
86 const char *Table_wrapper::FIELD_STRING_PAD_NAME = "StringPad";
87 const char *Table_wrapper::FIELD_STRING_NOPAD_NAME = "StringNoPad";
88
89 Table_wrapper::Table_wrapper() {
90 m_table_share.db_low_byte_first = false;
91 m_table.s = &m_table_share;
92 m_table.record[0] = m_record0.data();
93 m_table.record[1] = m_record1.data();
94
95 m_field_short.reset(new Field_short(
96 get_data_ptr(FIELD_SHORT_OFFSET), FIELD_SHORT_LENGTH,
97 get_null_ptr(FIELD_SHORT_NULL_BIT), get_null_bit(FIELD_SHORT_NULL_BIT), 0,
98 FIELD_SHORT_NAME, false, false));
99 m_field_short->table = &m_table;
100
101 m_field_string_pad.reset(new Field_string(
102 get_data_ptr(FIELD_STRING_PAD_OFFSET), FIELD_STRING_PAD_LENGTH,
103 get_null_ptr(FIELD_STRING_PAD_NULL_BIT),
104 get_null_bit(FIELD_STRING_PAD_NULL_BIT), 0, FIELD_STRING_PAD_NAME,
105 &my_charset_utf8mb4_general_ci));
106 m_field_string_pad->table = &m_table;
107
108 m_field_string_nopad.reset(new Field_string(
109 get_data_ptr(FIELD_STRING_NOPAD_OFFSET), FIELD_STRING_NOPAD_LENGTH,
110 get_null_ptr(FIELD_STRING_NOPAD_NULL_BIT),
111 get_null_bit(FIELD_STRING_NOPAD_NULL_BIT), 0, FIELD_STRING_NOPAD_NAME,
112 &my_charset_utf8mb4_0900_ai_ci));
113 m_field_string_nopad->table = &m_table;
114 }
115
116 Field_short *Table_wrapper::get_field_short() { return m_field_short.get(); }
117
118 Field_string *Table_wrapper::get_field_string_pad() {
119 return m_field_string_pad.get();
120 }
121
122 Field_string *Table_wrapper::get_field_string_nopad() {
123 return m_field_string_nopad.get();
124 }
125
126 uint8 *Table_wrapper::get_data_ptr(int offset) {
127 return m_record0.data() + offset;
128 }
129
130 uint8 *Table_wrapper::get_null_ptr(int bit) {
131 int offset = bit / 8;
132 return m_record0.data() + offset;
133 }
134
135 uchar Table_wrapper::get_null_bit(int bit) { return bit % 8; }
136
137 /* must mimic the Field_short::store() */
138 void Table_wrapper::store_short_value(
139 std::array<uchar, Table_wrapper::FIELD_SHORT_LENGTH> &storage,
140 short value) {
141 #ifdef WORDS_BIGENDIAN
142 if (m_table.s->db_low_byte_first)
143 int2store(storage.data(), value);
144 else
145 #endif
146 shortstore(storage.data(), value);
147 }
148
149 } // namespace
150
151 TEST(CellCalculator, NullEmpty) {
152 Table_wrapper table;
153
154 std::array<uchar, Table_wrapper::FIELD_SHORT_LENGTH> value;
155
156 table.store_short_value(value, 0x1234);
157
158 temptable::Cell cell_valid(false, value.size(), value.data());
159 temptable::Cell cell_empty(false, 0, value.data());
160 temptable::Cell cell_null1(true, 0, nullptr);
161 temptable::Cell cell_null2(true, value.size(), value.data());
162
163 KEY_PART_INFO key;
164 key.init_from_field(table.get_field_short());
165
166 temptable::Cell_calculator calculator(key);
167
168 EXPECT_EQ(calculator.hash(cell_null1), 1);
169 EXPECT_EQ(calculator.hash(cell_null2), 1);
170 EXPECT_EQ(calculator.hash(cell_empty), 0);
171
172 EXPECT_EQ(calculator.compare(cell_null1, cell_null2), 0);
173
174 EXPECT_LT(calculator.compare(cell_null1, cell_valid), 0);
175 EXPECT_LT(calculator.compare(cell_null2, cell_valid), 0);
176
177 EXPECT_GT(calculator.compare(cell_valid, cell_null1), 0);
178 EXPECT_GT(calculator.compare(cell_valid, cell_null2), 0);
179 }
180
181 TEST(CellCalculator, Short) {
182 Table_wrapper table;
183
184 std::array<uchar, Table_wrapper::FIELD_SHORT_LENGTH> value1a;
185 std::array<uchar, Table_wrapper::FIELD_SHORT_LENGTH> value1b;
186 std::array<uchar, Table_wrapper::FIELD_SHORT_LENGTH> value2a;
187 std::array<uchar, Table_wrapper::FIELD_SHORT_LENGTH> value2b;
188
189 table.store_short_value(value1a, 0x1234);
190 table.store_short_value(value1b, 0x1234);
191 table.store_short_value(value2a, 0x4321);
192 table.store_short_value(value2b, 0x4321);
193
194 temptable::Cell cell1a(false, value1a.size(), value1a.data());
195 temptable::Cell cell1b(false, value1b.size(), value1b.data());
196 temptable::Cell cell2a(false, value2a.size(), value2a.data());
197 temptable::Cell cell2b(false, value2b.size(), value2b.data());
198
199 /* check for field */
200
201 temptable::Cell_calculator field_calculator(table.get_field_short());
202
203 EXPECT_EQ(field_calculator.hash(cell1a), field_calculator.hash(cell1b));
204 EXPECT_EQ(field_calculator.hash(cell2a), field_calculator.hash(cell2b));
205
206 EXPECT_EQ(field_calculator.compare(cell1a, cell1b), 0);
207 EXPECT_EQ(field_calculator.compare(cell1a, cell1b), 0);
208
209 EXPECT_LT(field_calculator.compare(cell1a, cell2a), 0);
210 EXPECT_GT(field_calculator.compare(cell2a, cell1a), 0);
211
212 EXPECT_GT(field_calculator.compare(cell2b, cell1b), 0);
213 EXPECT_LT(field_calculator.compare(cell1b, cell2b), 0);
214
215 /* check for key */
216
217 KEY_PART_INFO key;
218 key.init_from_field(table.get_field_short());
219
220 temptable::Cell_calculator key_calculator(key);
221
222 EXPECT_EQ(key_calculator.hash(cell1a), key_calculator.hash(cell1b));
223 EXPECT_EQ(key_calculator.hash(cell2a), key_calculator.hash(cell2b));
224
225 EXPECT_EQ(key_calculator.compare(cell1a, cell1b), 0);
226 EXPECT_EQ(key_calculator.compare(cell1a, cell1b), 0);
227
228 EXPECT_LT(key_calculator.compare(cell1a, cell2a), 0);
229 EXPECT_GT(key_calculator.compare(cell2a, cell1a), 0);
230
231 EXPECT_GT(key_calculator.compare(cell2b, cell1b), 0);
232 EXPECT_LT(key_calculator.compare(cell1b, cell2b), 0);
233 }
234
235 TEST(CellCalculator, StringPad) {
236 Table_wrapper table;
237
238 std::array<uchar, Table_wrapper::FIELD_STRING_PAD_LENGTH> value1a;
239 std::array<uchar, Table_wrapper::FIELD_STRING_PAD_LENGTH> value1b;
240 std::array<uchar, Table_wrapper::FIELD_STRING_PAD_LENGTH> value2a;
241 std::array<uchar, Table_wrapper::FIELD_STRING_PAD_LENGTH> value3a;
242
243 value1a.fill(' ');
244 std::memcpy(value1a.data(), "abcde", 5);
245 value1b.fill(' ');
246 std::memcpy(value1b.data(), "abcde", 5);
247 value2a.fill(' ');
248 std::memcpy(value2a.data(), "aaaaabcde", 10);
249 value3a.fill(' ');
250 std::memcpy(value3a.data(), "abcdfg", 6);
251
252 temptable::Cell cell1a(false, value1a.size(), value1a.data());
253 temptable::Cell cell1b(false, value1b.size(), value1b.data());
254 temptable::Cell cell2a(false, value2a.size(), value2a.data());
255 temptable::Cell cell3a(false, value3a.size(), value3a.data());
256
257 /* check for field */
258
259 temptable::Cell_calculator field_calculator(table.get_field_string_pad());
260
261 EXPECT_EQ(field_calculator.hash(cell1a), field_calculator.hash(cell1b));
262
263 EXPECT_EQ(field_calculator.compare(cell1a, cell1b), 0);
264
265 EXPECT_LT(field_calculator.compare(cell2a, cell1a), 0);
266 EXPECT_GT(field_calculator.compare(cell1a, cell2a), 0);
267
268 EXPECT_GT(field_calculator.compare(cell3a, cell1a), 0);
269 EXPECT_LT(field_calculator.compare(cell1a, cell3a), 0);
270
271 EXPECT_LT(field_calculator.compare(cell2a, cell3a), 0);
272
273 /* check for full (whole column) key */
274
275 KEY_PART_INFO full_key;
276 full_key.init_from_field(table.get_field_string_pad());
277
278 temptable::Cell_calculator full_key_calculator(full_key);
279
280 EXPECT_EQ(full_key_calculator.hash(cell1a), full_key_calculator.hash(cell1b));
281
282 EXPECT_EQ(full_key_calculator.compare(cell1a, cell1b), 0);
283
284 EXPECT_LT(full_key_calculator.compare(cell2a, cell1a), 0);
285 EXPECT_GT(full_key_calculator.compare(cell1a, cell2a), 0);
286
287 EXPECT_GT(full_key_calculator.compare(cell3a, cell1a), 0);
288 EXPECT_LT(full_key_calculator.compare(cell1a, cell3a), 0);
289
290 EXPECT_LT(full_key_calculator.compare(cell2a, cell3a), 0);
291
292 /* check for prefix key */
293
294 KEY_PART_INFO prefix_key;
295 prefix_key.init_from_field(table.get_field_string_pad());
296 prefix_key.key_part_flag |= HA_PART_KEY_SEG;
297 prefix_key.length =
298 3 * table.get_field_string_pad()->charset_for_protocol()->mbmaxlen;
299
300 temptable::Cell_calculator prefix_key_calculator(prefix_key);
301
302 EXPECT_EQ(prefix_key_calculator.hash(cell1a),
303 prefix_key_calculator.hash(cell1b));
304 EXPECT_EQ(prefix_key_calculator.hash(cell1a),
305 prefix_key_calculator.hash(cell3a));
306
307 EXPECT_EQ(prefix_key_calculator.compare(cell1a, cell1b), 0);
308 EXPECT_EQ(prefix_key_calculator.compare(cell1a, cell3a), 0);
309 EXPECT_EQ(prefix_key_calculator.compare(cell1b, cell3a), 0);
310
311 EXPECT_LT(prefix_key_calculator.compare(cell2a, cell1a), 0);
312 EXPECT_GT(prefix_key_calculator.compare(cell1a, cell2a), 0);
313
314 EXPECT_LT(prefix_key_calculator.compare(cell2a, cell3a), 0);
315 EXPECT_GT(prefix_key_calculator.compare(cell3a, cell2a), 0);
316 }
317
318 TEST(CellCalculator, StringNoPad) {
319 Table_wrapper table;
320
321 std::array<uchar, Table_wrapper::FIELD_STRING_NOPAD_LENGTH> value1a;
322 std::array<uchar, Table_wrapper::FIELD_STRING_NOPAD_LENGTH> value1b;
323 std::array<uchar, Table_wrapper::FIELD_STRING_NOPAD_LENGTH> value2a;
324 std::array<uchar, Table_wrapper::FIELD_STRING_NOPAD_LENGTH> value3a;
325
326 value1a.fill(' ');
327 std::memcpy(value1a.data(), "abcde", 5);
328 value1b.fill(' ');
329 std::memcpy(value1b.data(), "abcde", 5);
330 value2a.fill(' ');
331 std::memcpy(value2a.data(), "aaaaabcde", 10);
332 value3a.fill(' ');
333 std::memcpy(value3a.data(), "abcdfg", 6);
334
335 temptable::Cell cell1a(false, value1a.size(), value1a.data());
336 temptable::Cell cell1b(false, value1b.size(), value1b.data());
337 temptable::Cell cell2a(false, value2a.size(), value2a.data());
338 temptable::Cell cell3a(false, value3a.size(), value3a.data());
339
340 /* check for field */
341
342 temptable::Cell_calculator field_calculator(table.get_field_string_nopad());
343
344 EXPECT_EQ(field_calculator.hash(cell1a), field_calculator.hash(cell1b));
345
346 EXPECT_EQ(field_calculator.compare(cell1a, cell1b), 0);
347
348 EXPECT_LT(field_calculator.compare(cell2a, cell1a), 0);
349 EXPECT_GT(field_calculator.compare(cell1a, cell2a), 0);
350
351 EXPECT_GT(field_calculator.compare(cell3a, cell1a), 0);
352 EXPECT_LT(field_calculator.compare(cell1a, cell3a), 0);
353
354 EXPECT_LT(field_calculator.compare(cell2a, cell3a), 0);
355
356 /* check for full (whole column) key */
357
358 KEY_PART_INFO full_key;
359 full_key.init_from_field(table.get_field_string_nopad());
360
361 temptable::Cell_calculator full_key_calculator(full_key);
362
363 EXPECT_EQ(full_key_calculator.hash(cell1a), full_key_calculator.hash(cell1b));
364
365 EXPECT_EQ(full_key_calculator.compare(cell1a, cell1b), 0);
366
367 EXPECT_LT(full_key_calculator.compare(cell2a, cell1a), 0);
368 EXPECT_GT(full_key_calculator.compare(cell1a, cell2a), 0);
369
370 EXPECT_GT(full_key_calculator.compare(cell3a, cell1a), 0);
371 EXPECT_LT(full_key_calculator.compare(cell1a, cell3a), 0);
372
373 EXPECT_LT(full_key_calculator.compare(cell2a, cell3a), 0);
374
375 /* check for prefix key */
376
377 KEY_PART_INFO prefix_key;
378 prefix_key.init_from_field(table.get_field_string_nopad());
379 prefix_key.key_part_flag |= HA_PART_KEY_SEG;
380 prefix_key.length =
381 3 * table.get_field_string_nopad()->charset_for_protocol()->mbmaxlen;
382
383 temptable::Cell_calculator prefix_key_calculator(prefix_key);
384
385 EXPECT_EQ(prefix_key_calculator.hash(cell1a),
386 prefix_key_calculator.hash(cell1b));
387 EXPECT_EQ(prefix_key_calculator.hash(cell1a),
388 prefix_key_calculator.hash(cell3a));
389
390 EXPECT_EQ(prefix_key_calculator.compare(cell1a, cell1b), 0);
391 EXPECT_EQ(prefix_key_calculator.compare(cell1a, cell3a), 0);
392 EXPECT_EQ(prefix_key_calculator.compare(cell1b, cell3a), 0);
393
394 EXPECT_LT(prefix_key_calculator.compare(cell2a, cell1a), 0);
395 EXPECT_GT(prefix_key_calculator.compare(cell1a, cell2a), 0);
396
397 EXPECT_LT(prefix_key_calculator.compare(cell2a, cell3a), 0);
398 EXPECT_GT(prefix_key_calculator.compare(cell3a, cell2a), 0);
399 }
400
401 } // namespace temptable_test