Debian Codegraph @

unittest/gunit/dd_sdi-t.cc

raw symbols SCIP
1 /* Copyright (c) 2014, 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 <stddef.h>
26
27 #include "my_rapidjson_size_t.h" // IWYU pragma: keep
28
29 #include <rapidjson/document.h>
30 #include <rapidjson/prettywriter.h>
31 #include <rapidjson/stringbuffer.h>
32
33 #include "m_string.h"
34 #include "my_inttypes.h"
35 #include "sql/dd/dd.h"
36 #include "sql/dd/impl/properties_impl.h"
37 #include "sql/dd/impl/sdi.h"
38 #include "sql/dd/impl/sdi_impl.h"
39 #include "sql/dd/impl/types/column_impl.h"
40 #include "sql/dd/impl/types/entity_object_impl.h"
41 #include "sql/dd/impl/types/index_impl.h"
42 #include "sql/dd/impl/types/table_impl.h"
43 #include "sql/dd/impl/types/weak_object_impl.h"
44 #include "sql/dd/sdi_file.h"
45 #include "sql/dd/types/column.h"
46 #include "sql/dd/types/column_statistics.h"
47 #include "sql/dd/types/column_type_element.h"
48 #include "sql/dd/types/foreign_key.h"
49 #include "sql/dd/types/foreign_key_element.h"
50 #include "sql/dd/types/index.h"
51 #include "sql/dd/types/index_element.h"
52 #include "sql/dd/types/partition.h"
53 #include "sql/dd/types/partition_index.h"
54 #include "sql/dd/types/partition_value.h"
55 #include "sql/dd/types/table.h"
56 #include "sql/dd/types/tablespace.h"
57 #include "sql/dd/types/tablespace_file.h"
58 #include "sql/histograms/equi_height.h"
59 #include "sql/histograms/histogram.h"
60 #include "sql/histograms/value_map.h"
61
62 namespace {
63 int FANOUT = 3;
64 }
65
66 namespace dd {
67 class Sdi_wcontext;
68 class Sdi_rcontext;
69 } // namespace dd
70
71 namespace sdi_unittest {
72
73 typedef dd::Foreign_key Foreign_key; // To avoid the one from sql_class.h
74
75 ::dd::Sdi_wcontext *get_wctx();
76 ::dd::Sdi_rcontext *get_rctx();
77
78 bool equal_prefix_chars_driver(const dd::String_type &a,
79 const dd::String_type &b, size_t prefix);
80
81 // Mocking functions
82
83 static void mock_properties(dd::Properties &p, uint64 size) {
84 for (uint64 i = 0; i < size; ++i) {
85 dd::String_type key = (down_cast<dd::Properties_impl &>(p)).valid_key_at(i);
86 p.set(key, i);
87 }
88 }
89
90 static void mock_dd_obj(dd::Column_type_element *cte) {
91 if (cte) {
92 cte->set_name("mock_column_type_element");
93 }
94 }
95
96 static void mock_dd_obj(dd::Column *c) {
97 static dd::Object_id curid = 10000;
98 dd::Entity_object_impl *object_impl =
99 dynamic_cast<dd::Entity_object_impl *>(c);
100 object_impl->set_id(curid++);
101 c->set_type(dd::enum_column_types::ENUM);
102 c->set_char_length(42);
103 c->set_numeric_precision(42);
104 c->set_numeric_scale(42);
105 c->set_datetime_precision(42);
106 c->set_default_value("mocked default column value");
107 c->set_default_option("mocked default option");
108 c->set_update_option("mocked update option");
109 c->set_comment("mocked column comment");
110 c->set_srs_id({4326});
111 mock_properties(c->se_private_data(), FANOUT);
112
113 for (int i = 0; i < FANOUT; ++i) {
114 mock_dd_obj(c->add_element());
115 }
116 if (c->ordinal_position() == 0) {
117 down_cast<dd::Column_impl *>(c)->set_ordinal_position(1);
118 }
119 }
120
121 static void mock_column_statistics_obj(dd::Column_statistics *c,
122 MEM_ROOT *mem_root) {
123 c->set_schema_name("my_schema");
124 c->set_table_name("my_table");
125 c->set_column_name("my_column");
126
127 histograms::Value_map<longlong> int_values(&my_charset_latin1,
128 histograms::Value_map_type::INT);
129 int_values.add_values(0LL, 10);
130
131 histograms::Equi_height<longlong> *equi_height =
132 histograms::Equi_height<longlong>::create(
133 mem_root, "my_schema", "my_table", "my_column",
134 histograms::Value_map_type::INT);
135 ASSERT_TRUE(equi_height != nullptr);
136
137 EXPECT_FALSE(equi_height->build_histogram(int_values, 1024));
138 c->set_histogram(equi_height);
139 }
140
141 static void mock_dd_obj(dd::Index_element *ie) {
142 ie->set_length(42);
143 ie->set_order(dd::Index_element::ORDER_DESC);
144 }
145
146 static void mock_dd_obj(dd::Index *i, dd::Column *c = nullptr) {
147 static dd::Object_id curid = 10000;
148 dd::Entity_object_impl *object_impl =
149 dynamic_cast<dd::Entity_object_impl *>(i);
150 object_impl->set_id(curid++);
151 i->set_comment("mocked index comment");
152 mock_properties(i->options(), FANOUT);
153 mock_properties(i->se_private_data(), FANOUT);
154 i->set_engine("mocked index engine");
155 i->set_type(dd::Index::IT_MULTIPLE);
156 i->set_algorithm(dd::Index::IA_HASH);
157
158 mock_dd_obj(i->add_element(c));
159
160 if (i->ordinal_position() == 0) {
161 down_cast<dd::Index_impl *>(i)->set_ordinal_position(1);
162 }
163 }
164
165 static void mock_dd_obj(dd::Foreign_key_element *fke) {
166 fke->referenced_column_name("mocked referenced column name");
167 }
168
169 static void mock_dd_obj(dd::Foreign_key *fk) {
170 fk->set_match_option(Foreign_key::OPTION_PARTIAL);
171 fk->set_update_rule(Foreign_key::RULE_CASCADE);
172 fk->set_delete_rule(Foreign_key::RULE_CASCADE);
173 fk->set_referenced_table_name("mocked referenced table name");
174 for (int i = 0; i < FANOUT; ++i) {
175 mock_dd_obj(fk->add_element());
176 }
177 }
178
179 static void mock_dd_obj(dd::Partition_index *pi) {
180 mock_properties(pi->options(), FANOUT);
181 mock_properties(pi->se_private_data(), FANOUT);
182 }
183
184 static void mock_dd_obj(dd::Partition_value *pv) {
185 pv->set_list_num(42);
186 pv->set_column_num(42);
187 pv->set_value_utf8("mocked partition value");
188 }
189
190 static void mock_dd_obj(dd::Partition *p, dd::Index *ix = nullptr) {
191 p->set_number(42);
192 p->set_engine("mocked partition engine");
193 p->set_comment("mocked comment");
194 mock_properties(p->options(), FANOUT);
195 mock_properties(p->se_private_data(), FANOUT);
196
197 for (int j = 0; j < FANOUT; ++j) {
198 mock_dd_obj(p->add_value());
199 mock_dd_obj(p->add_index(ix));
200 }
201 }
202
203 static void mock_dd_obj(dd::Table *t) {
204 mock_properties(t->options(), FANOUT);
205 t->set_created(42);
206 t->set_last_altered(42);
207
208 t->set_engine("mocked table engine");
209 t->set_comment("mocked table comment");
210 mock_properties(t->se_private_data(), FANOUT);
211
212 t->set_partition_type(dd::Table::PT_RANGE);
213 t->set_default_partitioning(dd::Table::DP_NUMBER);
214 t->set_partition_expression("mocked partition expression");
215 t->set_subpartition_type(dd::Table::ST_LINEAR_HASH);
216 t->set_default_subpartitioning(dd::Table::DP_YES);
217 t->set_subpartition_expression("mocked subpartition expression");
218
219 for (int i = 0; i < FANOUT; ++i) {
220 mock_dd_obj(t->add_foreign_key());
221 dd::Column *c = t->add_column();
222 mock_dd_obj(c);
223 dd::Index *ix = t->add_index();
224 mock_dd_obj(ix, c);
225 dd::Partition *p = t->add_partition();
226 mock_dd_obj(p, ix);
227 }
228 }
229
230 static void mock_dd_obj(dd::Tablespace_file *f) {
231 f->set_filename("mock_tablespace_file");
232 mock_properties(f->se_private_data(), FANOUT);
233 }
234
235 static void mock_dd_obj(dd::Tablespace *ts) {
236 ts->set_comment("Mocked tablespace");
237 mock_properties(ts->options(), FANOUT);
238 mock_properties(ts->se_private_data(), FANOUT);
239 ts->set_engine("mocked storage engine name");
240 for (int i = 0; i < FANOUT; i++) {
241 mock_dd_obj(ts->add_file());
242 }
243 }
244
245 class SdiTest : public ::testing::Test {
246 protected:
247 void SetUp() override {}
248
249 void TearDown() override {}
250
251 SdiTest() = default;
252
253 private:
254 SdiTest(SdiTest const &) = delete;
255 SdiTest &operator=(SdiTest const &) = delete;
256 };
257
258 bool diff(const dd::String_type &expected, dd::String_type actual) {
259 if (actual == expected) {
260 return false;
261 }
262 typedef dd::String_type::const_iterator csit_t;
263 typedef std::pair<csit_t, csit_t> diff_t_;
264 csit_t expected_end = expected.end();
265 actual.resize(expected.size());
266 csit_t actual_end = actual.end();
267 diff_t_ diff =
268 std::mismatch(expected.begin(), expected.end(), actual.begin());
269
270 std::cout << dd::String_type(expected.begin(), diff.first) << "\n@ offset "
271 << diff.first - expected.begin() << ":\n< "
272 << dd::String_type(diff.first, expected_end) << "\n---\n> "
273 << dd::String_type(diff.second, actual_end) << std::endl;
274 return true;
275 }
276
277 template <typename T>
278 dd::String_type serialize_drv(const T *dd_obj) {
279 dd::RJ_StringBuffer buf;
280 dd::Sdi_writer w(buf);
281 dd::String_type s = "driver schema";
282 dd::Sdi_wcontext *wctx = get_wctx();
283 dd_obj->serialize(wctx, &w);
284 return dd::String_type(buf.GetString(), buf.GetSize());
285 }
286
287 template <typename T>
288 T *deserialize_drv(const dd::String_type &sdi) {
289 T *dst_obj = dd::create_object<T>();
290 dd::RJ_Document doc;
291 doc.Parse<0>(sdi.c_str());
292 dd::Sdi_rcontext *rctx = get_rctx();
293 dst_obj->deserialize(rctx, doc);
294 return dst_obj;
295 }
296
297 template <class T>
298 dd::String_type api_serialize(const T *tp) {
299 return dd::serialize(*tp);
300 }
301
302 dd::String_type api_serialize(const dd::Table *table) {
303 return dd::serialize(nullptr, *table, "api_schema");
304 }
305
306 template <typename T>
307 void verify(T *dd_obj) {
308 dd::String_type sdi = serialize_drv(dd_obj);
309 // std::cout << "Verifying json: \n" << sdi << std::endl;
310 ASSERT_GT(sdi.size(), 0u);
311 std::unique_ptr<T> dst_obj{deserialize_drv<T>(sdi)};
312 dd::String_type dst_sdi = serialize_drv(dst_obj.get());
313 EXPECT_EQ(dst_sdi, sdi);
314 }
315
316 template <typename T>
317 void simple_test() {
318 std::unique_ptr<T> dd_obj(dd::create_object<T>());
319 mock_dd_obj(dd_obj.get());
320 verify(dd_obj.get());
321 }
322
323 template <typename AP>
324 void api_test(const AP &ap) {
325 typedef typename AP::element_type T;
326 dd::Sdi_type sdi = api_serialize(ap.get());
327 std::unique_ptr<T> d(dd::create_object<T>());
328 dd::deserialize(nullptr, sdi, d.get());
329
330 dd::Sdi_type d_sdi = api_serialize(d.get());
331
332 EXPECT_EQ(d_sdi.size(), sdi.size());
333 EXPECT_EQ(d_sdi, sdi);
334 ASSERT_FALSE(diff(sdi, d_sdi));
335 }
336
337 // Test cases
338
339 TEST(SdiTest, Column_type_element) { simple_test<dd::Column_type_element>(); }
340
341 TEST(SdiTest, Column) { simple_test<dd::Column>(); }
342
343 TEST(SdiTest, Column_statistics) {
344 std::unique_ptr<dd::Column_statistics> dd_obj(
345 dd::create_object<dd::Column_statistics>());
346
347 MEM_ROOT mem_root(PSI_NOT_INSTRUMENTED, 256);
348
349 mock_column_statistics_obj(dd_obj.get(), &mem_root);
350
351 /*
352 We only support proper serialization of column statistics. Proper
353 deserialization (which will include re-generating histogram data) will be
354 available later.
355 */
356 dd::String_type sdi = serialize_drv(dd_obj.get());
357 EXPECT_FALSE(sdi.empty());
358
359 std::unique_ptr<dd::Column_statistics> deserialized{
360 deserialize_drv<dd::Column_statistics>(sdi)};
361
362 EXPECT_TRUE(dd_obj.get()->schema_name() == deserialized.get()->schema_name());
363 EXPECT_TRUE(dd_obj.get()->table_name() == deserialized.get()->table_name());
364 EXPECT_TRUE(dd_obj.get()->column_name() == deserialized.get()->column_name());
365 mem_root.Clear();
366 }
367
368 TEST(SdiTest, Index_element) { simple_test<dd::Index_element>(); }
369
370 TEST(SdiTest, Index) { simple_test<dd::Index>(); }
371
372 TEST(SdiTest, Foreign_key_element) { simple_test<dd::Foreign_key_element>(); }
373
374 TEST(SdiTest, Foreign_key) { simple_test<dd::Foreign_key>(); }
375
376 TEST(SdiTest, Partition_index) { simple_test<dd::Partition_index>(); }
377
378 TEST(SdiTest, Partition_value) { simple_test<dd::Partition_value>(); }
379
380 TEST(SdiTest, Partition) { simple_test<dd::Partition>(); }
381
382 TEST(SdiTest, Table) { simple_test<dd::Table>(); }
383
384 TEST(SdiTest, Tablespace_file) { simple_test<dd::Tablespace_file>(); }
385
386 TEST(SdiTest, Tablespace) { simple_test<dd::Tablespace>(); }
387
388 TEST(SdiTest, Table_API) {
389 std::unique_ptr<dd::Table> t(dd::create_object<dd::Table>());
390 mock_dd_obj(t.get());
391 // std::cout << "Serialized table:\n" << serialize_ap(t) << std::endl;
392 api_test(t);
393 }
394
395 TEST(SdiTest, Tablespace_API) {
396 std::unique_ptr<dd::Tablespace> ts(dd::create_object<dd::Tablespace>());
397 mock_dd_obj(ts.get());
398
399 api_test(ts);
400 }
401
402 #ifdef NDEBUG
403 TEST(SdiTest, Serialization_perf) {
404 std::unique_ptr<dd::Table> t(dd::create_object<dd::Table>());
405 FANOUT = 20;
406 mock_dd_obj(t.get());
407
408 for (int i = 0; i < 1; ++i) {
409 dd::String_type sdi = dd::serialize(nullptr, *t, "perftest");
410 EXPECT_GT(sdi.size(), 100000u);
411 }
412 }
413 #endif /* NDEBUG */
414
415 TEST(SdiTest, CharPromotion) {
416 signed char x = 127;
417 unsigned char ux = x;
418 EXPECT_EQ(127u, ux);
419
420 unsigned short usx = x;
421 EXPECT_EQ(127u, usx);
422
423 unsigned int uix = x;
424 EXPECT_EQ(127u, usx);
425
426 unsigned char tmp = 0xe0;
427 x = static_cast<signed char>(tmp);
428 EXPECT_EQ(-32, x);
429
430 ux = x;
431 EXPECT_EQ(224u, ux);
432
433 usx = x;
434 short sx = x;
435 EXPECT_EQ(-32, sx);
436 unsigned short usy = sx;
437 EXPECT_EQ(usx, usy);
438 EXPECT_EQ(65504u, usx);
439
440 uix = x;
441 int ix = x;
442 EXPECT_EQ(-32, ix);
443 unsigned int uiy = ix;
444 EXPECT_EQ(uix, uiy);
445 EXPECT_EQ(4294967264u, uix);
446 }
447
448 TEST(SdiTest, Utf8Filename) {
449 dd::Table_impl x{};
450 x.set_name("\xe0\xa0\x80");
451 x.set_id(42);
452 dd::String_type path = dd::sdi_file::sdi_filename(x.id(), x.name(), "foobar");
453 std::replace(path.begin(), path.end(), '\\', '/');
454 EXPECT_EQ("./foobar/@0800_42.sdi", path);
455 }
456
457 TEST(SdiTest, Utf8FilenameTrunc) {
458 dd::String_type name{"\xe0\xa0\x80"};
459 for (int i = 0; i < 16; ++i) {
460 name.append("\xe0\xa0\x80");
461 }
462 EXPECT_EQ(51u, name.length());
463
464 dd::Table_impl x{};
465 x.set_name(name);
466 x.set_id(42);
467 dd::String_type fn = dd::sdi_file::sdi_filename(x.id(), x.name(), "foobar");
468 std::replace(fn.begin(), fn.end(), '\\', '/');
469 EXPECT_EQ(96u, fn.length());
470 EXPECT_EQ(
471 "./foobar/"
472 "@0800@0800@0800@0800@0800@0800@0800@0800@0800@0800@0800@0800@0800@0800@"
473 "0800@0800_42.sdi",
474 fn);
475 }
476
477 TEST(SdiTest, EqualPrefixCharsAscii) {
478 ASSERT_TRUE(equal_prefix_chars_driver("a", "a", 16));
479 ASSERT_FALSE(equal_prefix_chars_driver("a", "b", 16));
480 ASSERT_FALSE(equal_prefix_chars_driver("aaa", "aaab", 16));
481 ASSERT_TRUE(equal_prefix_chars_driver("abcdefghijklmnon_aaa",
482 "abcdefghijklmnon_bbb", 16));
483 }
484
485 TEST(SdiTest, EqualPrefixCharsUtf8) {
486 ASSERT_TRUE(equal_prefix_chars_driver("\xe0\xa0\x80", "\xe0\xa0\x80", 16));
487 ASSERT_FALSE(
488 equal_prefix_chars_driver("\xe0\xa0\x80", "\xf0\x90\x80\x80", 16));
489 ASSERT_FALSE(equal_prefix_chars_driver(
490 "\xe0\xa0\x80\xe0\xa0\x80\xe0\xa0\x80",
491 "\xe0\xa0\x80\xe0\xa0\x80\xe0\xa0\x80\xf0\x90\x80\x80", 16));
492
493 ASSERT_TRUE(equal_prefix_chars_driver(
494 "abc\xe0\xa0\x80"
495 "def\xe0\xa0\x80ghi\xe0\xa0\x80jkl\xe0\xa0\x80_aaa",
496 "abc\xe0\xa0\x80"
497 "def\xe0\xa0\x80ghi\xe0\xa0\x80jkl\xe0\xa0\x80_bbb",
498 16));
499 }
500
501 // Verify that default null, implicit non-null (by setting a value)
502 // and explicit null (by setting to null) is correctly preserved
503 // through serialization and deserialization.
504 TEST(SdiTest, Bug_25792649) {
505 dd::Column_impl cobj;
506 cobj.set_ordinal_position(1);
507
508 ASSERT_TRUE(cobj.is_numeric_scale_null());
509 ASSERT_TRUE(cobj.is_datetime_precision_null());
510 ASSERT_TRUE(cobj.is_default_value_null());
511 ASSERT_TRUE(cobj.is_default_value_utf8_null());
512 ASSERT_TRUE(cobj.is_generation_expression_null());
513 ASSERT_TRUE(cobj.is_generation_expression_utf8_null());
514
515 dd::String_type sdi = serialize_drv(&cobj);
516 dd::RJ_Document doc;
517 doc.Parse<0>(sdi.c_str());
518 dd::Sdi_rcontext *rctx = get_rctx();
519 dd::Column_impl dst;
520 dst.deserialize(rctx, doc);
521 ASSERT_TRUE(dst.is_numeric_scale_null());
522 ASSERT_TRUE(dst.is_datetime_precision_null());
523 ASSERT_TRUE(dst.is_default_value_null());
524 ASSERT_TRUE(dst.is_default_value_utf8_null());
525 ASSERT_TRUE(dst.is_generation_expression_null());
526 ASSERT_TRUE(dst.is_generation_expression_utf8_null());
527
528 cobj.set_numeric_scale(10);
529 cobj.set_datetime_precision(10);
530 cobj.set_default_value("This is my default");
531 cobj.set_default_value_utf8("This is my utf8 default");
532 cobj.set_generation_expression("This is my generation expression");
533 cobj.set_generation_expression_utf8("This is my utf8 generation expression");
534
535 ASSERT_FALSE(cobj.is_numeric_scale_null());
536 ASSERT_FALSE(cobj.is_datetime_precision_null());
537 ASSERT_FALSE(cobj.is_default_value_null());
538 ASSERT_FALSE(cobj.is_default_value_utf8_null());
539 ASSERT_FALSE(cobj.is_generation_expression_null());
540 ASSERT_FALSE(cobj.is_generation_expression_utf8_null());
541
542 sdi = serialize_drv(&cobj);
543 doc.Parse<0>(sdi.c_str());
544 dd::Column_impl dst2;
545 dst2.deserialize(rctx, doc);
546
547 ASSERT_FALSE(dst2.is_numeric_scale_null());
548 ASSERT_FALSE(dst2.is_datetime_precision_null());
549 ASSERT_FALSE(dst2.is_default_value_null());
550 ASSERT_FALSE(dst2.is_default_value_utf8_null());
551 ASSERT_FALSE(dst2.is_generation_expression_null());
552 ASSERT_FALSE(dst2.is_generation_expression_utf8_null());
553
554 cobj.set_numeric_scale_null(true);
555 cobj.set_datetime_precision_null(true);
556 cobj.set_default_value_null(true);
557 cobj.set_default_value_utf8_null(true);
558 cobj.set_generation_expression("");
559 cobj.set_generation_expression_utf8("");
560
561 ASSERT_TRUE(cobj.is_numeric_scale_null());
562 ASSERT_TRUE(cobj.is_datetime_precision_null());
563 ASSERT_TRUE(cobj.is_default_value_null());
564 ASSERT_TRUE(cobj.is_default_value_utf8_null());
565 ASSERT_TRUE(cobj.is_generation_expression_null());
566 ASSERT_TRUE(cobj.is_generation_expression_utf8_null());
567
568 sdi = serialize_drv(&cobj);
569 doc.Parse<0>(sdi.c_str());
570 dd::Column_impl dst3;
571 dst3.deserialize(rctx, doc);
572
573 ASSERT_TRUE(dst3.is_numeric_scale_null());
574 ASSERT_TRUE(dst3.is_datetime_precision_null());
575 ASSERT_TRUE(dst3.is_default_value_null());
576 ASSERT_TRUE(dst3.is_default_value_utf8_null());
577 ASSERT_TRUE(dst3.is_generation_expression_null());
578 ASSERT_TRUE(dst3.is_generation_expression_utf8_null());
579 }
580 } // namespace sdi_unittest