Debian Codegraph @

unittest/gunit/item_json_func-t.cc

raw symbols SCIP
1 /* Copyright (c) 2017, 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
26 #include <cstring>
27
28 #include "sql-common/json_dom.h"
29 #include "sql/item_json_func.h"
30 #include "sql/json_diff.h"
31 #include "sql/sql_class.h"
32 #include "sql/sql_list.h"
33 #include "unittest/gunit/base_mock_field.h"
34 #include "unittest/gunit/benchmark.h"
35 #include "unittest/gunit/fake_table.h"
36 #include "unittest/gunit/test_utils.h"
37
38 namespace item_json_func_unittest {
39
40 class ItemJsonFuncTest : public ::testing::Test {
41 protected:
42 void SetUp() override {
43 initializer.SetUp();
44 m_table = new Fake_TABLE(&m_field);
45 }
46
47 void TearDown() override {
48 m_table->cleanup_partial_update();
49 delete m_table;
50 initializer.TearDown();
51 }
52
53 THD *thd() { return initializer.thd(); }
54
55 my_testing::Server_initializer initializer;
56
57 Base_mock_field_json m_field{};
58
59 Fake_TABLE *m_table;
60 };
61
62 /**
63 Parse a JSON text and return its DOM representation.
64 @param json_text null-terminated string of JSON text
65 @return a DOM representing the JSON document
66 */
67 static Json_dom_ptr parse_json(const char *json_text) {
68 auto dom = Json_dom::parse(
69 json_text, std::strlen(json_text), [](const char *, size_t) {},
70 [] { ASSERT_TRUE(false); });
71 EXPECT_NE(nullptr, dom);
72 return dom;
73 }
74
75 static Item_string *new_item_string(const char *str) {
76 return new Item_string(str, std::strlen(str), &my_charset_utf8mb4_bin);
77 }
78
79 static void store_json(Field_json *field, const char *json_text) {
80 if (json_text == nullptr) {
81 EXPECT_EQ(TYPE_OK, set_field_to_null(field));
82 } else {
83 field->set_notnull();
84 Json_wrapper doc(parse_json(json_text));
85 EXPECT_EQ(TYPE_OK, field->store_json(&doc));
86 }
87 }
88
89 /**
90 Perform a partial update on a JSON column and verify the result.
91
92 @param func the JSON function to invoke
93 @param field the JSON column to update
94 @param orig_json text representation of the original JSON value
95 @param new_json text representation of the expected value in the
96 column after the partial update
97 @param binary_update whether binary diffs can be used
98 @param logical_update whether logical diffs can be used
99 */
100 static void do_partial_update(Item_json_func *func, Field_json *field,
101 const char *orig_json, const char *new_json,
102 bool binary_update, bool logical_update) {
103 const auto table = field->table;
104
105 if (!func->fixed) {
106 EXPECT_FALSE(func->fix_fields(table->in_use, nullptr));
107 EXPECT_TRUE(func->supports_partial_update(field));
108 func->mark_for_partial_update(field);
109 }
110
111 table->clear_partial_update_diffs();
112
113 store_json(field, orig_json);
114
115 EXPECT_TRUE(table->is_binary_diff_enabled(field));
116 EXPECT_TRUE(table->is_logical_diff_enabled(field));
117
118 Json_wrapper res1;
119 EXPECT_FALSE(func->val_json(&res1));
120 EXPECT_EQ(new_json == nullptr, func->null_value);
121 EXPECT_EQ(binary_update, table->is_binary_diff_enabled(field));
122 EXPECT_EQ(logical_update, table->is_logical_diff_enabled(field));
123
124 if (new_json == nullptr) return;
125
126 Json_wrapper new_doc(parse_json(new_json));
127 EXPECT_EQ(0, res1.compare(new_doc));
128
129 if (!logical_update) {
130 EXPECT_EQ(nullptr, table->get_logical_diffs(field));
131 return;
132 }
133
134 /*
135 Take a copy of the JSON diffs, since the call to
136 clear_partial_update_diffs() below will clear the original
137 Json_diff_vector.
138 */
139 const auto thd = table->in_use;
140 Json_diff_vector diffs(Json_diff_vector::allocator_type(thd->mem_root));
141 for (const auto &diff : *table->get_logical_diffs(field)) {
142 diffs.add_diff(diff.path(), diff.operation(), diff.value().clone_dom());
143 }
144
145 /*
146 apply_json_diffs() will try to collect binary diffs for the
147 changes that it applies to the column, so we should clear the
148 already collected diffs.
149 */
150 table->clear_partial_update_diffs();
151
152 EXPECT_TRUE(table->is_binary_diff_enabled(field));
153 EXPECT_EQ(enum_json_diff_status::SUCCESS, apply_json_diffs(field, &diffs));
154 EXPECT_EQ(binary_update, table->is_binary_diff_enabled(field));
155
156 Json_wrapper res2;
157 EXPECT_FALSE(field->val_json(&res2));
158 EXPECT_EQ(0, res2.compare(new_doc));
159
160 // apply_json_diffs() should produce new JSON diffs.
161 EXPECT_TRUE(table->is_logical_diff_enabled(field));
162 const Json_diff_vector *new_diffs = table->get_logical_diffs(field);
163 EXPECT_NE(nullptr, new_diffs);
164 EXPECT_EQ(diffs.size(), new_diffs->size());
165
166 // ... and applying those new diffs should produce the same result again ...
167 diffs.clear();
168 for (const auto &diff : *new_diffs) {
169 diffs.add_diff(diff.path(), diff.operation(), diff.value().clone_dom());
170 }
171 table->clear_partial_update_diffs();
172 store_json(field, orig_json);
173 EXPECT_EQ(enum_json_diff_status::SUCCESS, apply_json_diffs(field, &diffs));
174 Json_wrapper res3;
175 EXPECT_FALSE(field->val_json(&res3));
176 EXPECT_EQ(0, res3.compare(new_doc));
177 }
178
179 /*
180 Test partial update using various JSON functions.
181 */
182 TEST_F(ItemJsonFuncTest, PartialUpdate) {
183 m_field.make_writable();
184
185 Item *itf1 = new Item_field(&m_field);
186 auto json_set = new Item_func_json_set(
187 thd(), itf1, new_item_string("$[1]"), new_item_string("abc"),
188 new_item_string("$[2]"), new Item_int(100));
189
190 EXPECT_FALSE(m_table->mark_column_for_partial_update(&m_field));
191 EXPECT_FALSE(m_table->setup_partial_update(true));
192
193 // Logical update OK, but not enough space for binary update.
194 {
195 SCOPED_TRACE("");
196 do_partial_update(json_set, &m_field, "[1,2,3]", "[1,\"abc\",100]", false,
197 true);
198 }
199
200 // Both logical update and binary update OK.
201 {
202 SCOPED_TRACE("");
203 do_partial_update(json_set, &m_field, "[4,\"XYZ\",5]", "[4,\"abc\",100]",
204 true, true);
205 }
206
207 // No-op update.
208 {
209 SCOPED_TRACE("");
210 do_partial_update(json_set, &m_field, "[0,\"abc\",100]", "[0,\"abc\",100]",
211 true, true);
212 EXPECT_EQ(0U, m_table->get_binary_diffs(&m_field)->size());
213 EXPECT_EQ(0U, m_table->get_logical_diffs(&m_field)->size());
214 }
215
216 // The array grows, so only logical update is OK.
217 {
218 SCOPED_TRACE("");
219 do_partial_update(json_set, &m_field, "[6,\"XYZ\"]", "[6,\"abc\",100]",
220 false, true);
221 }
222
223 // The root document is auto-wrapped, so no partial update at all.
224 {
225 SCOPED_TRACE("");
226 do_partial_update(json_set, &m_field, "true", "[true,\"abc\",100]", false,
227 false);
228 }
229
230 // A sub-document is auto-wrapped. OK for logical update, but not for binary.
231 {
232 SCOPED_TRACE("");
233 auto wrap_set = new Item_func_json_set(
234 thd(), new Item_field(&m_field), new_item_string("$.x[2]"),
235 new Item_int(2), new_item_string("$.x[1]"), new Item_int(1));
236 do_partial_update(wrap_set, &m_field, "{\"x\":123}", "{\"x\":[123,1]}",
237 false, true);
238 }
239
240 // Replacing the root of the document leads to full update.
241 {
242 SCOPED_TRACE("");
243 auto replace_root = new Item_func_json_replace(
244 thd(), new Item_field(&m_field), new_item_string("$"), new Item_int(1));
245 do_partial_update(replace_root, &m_field, "{\"a\":[1,2,3]}", "1", false,
246 false);
247 }
248
249 // A nested call.
250 {
251 auto inner_func =
252 new Item_func_json_set(thd(), new Item_field(&m_field),
253 new_item_string("$.a[1]"), new Item_int(1));
254
255 auto outer_func = new Item_func_json_replace(
256 thd(), inner_func, new_item_string("$.b"), new Item_int(2));
257
258 {
259 SCOPED_TRACE("");
260 do_partial_update(outer_func, &m_field, "{\"a\":[1,2,3]}",
261 "{\"a\":[1,1,3]}", true, true);
262 }
263 {
264 SCOPED_TRACE("");
265 do_partial_update(outer_func, &m_field, "{\"a\":[1,2,3],\"b\":47}",
266 "{\"a\":[1,1,3],\"b\":2}", true, true);
267 }
268 {
269 SCOPED_TRACE("");
270 do_partial_update(outer_func, &m_field, "{\"a\":8}", "{\"a\":[8,1]}",
271 false, true);
272 }
273 {
274 SCOPED_TRACE("");
275 do_partial_update(outer_func, &m_field, nullptr, nullptr, false, false);
276 }
277 }
278
279 // A nested call where the inner function causes a full update.
280 {
281 auto inner_func = new Item_func_json_set(
282 thd(), new Item_field(&m_field), new_item_string("$"),
283 new Item_func_json_array(thd(), new Item_int(1), new Item_int(2)));
284 auto outer_func = new Item_func_json_set(
285 thd(), inner_func, new_item_string("$[1]"), new Item_int(3));
286
287 SCOPED_TRACE("");
288 do_partial_update(outer_func, &m_field, "[true,false]", "[1,3]", false,
289 false);
290 }
291
292 // Returning NULL should cause full update.
293 {
294 SCOPED_TRACE("");
295 auto null_path = new Item_func_json_set(thd(), new Item_field(&m_field),
296 new Item_null(), new Item_int(1));
297 do_partial_update(null_path, &m_field, "[1,2,3]", nullptr, false, false);
298 }
299
300 // Input document being NULL should cause full update.
301 {
302 SCOPED_TRACE("");
303 auto null_doc =
304 new Item_func_json_set(thd(), new Item_field(&m_field),
305 new_item_string("$.a.b.c"), new Item_int(1));
306 do_partial_update(null_doc, &m_field, nullptr, nullptr, false, false);
307 }
308
309 // Setting object member.
310 {
311 auto set_member =
312 new Item_func_json_set(thd(), new Item_field(&m_field),
313 new_item_string("$.a"), new Item_int(1));
314
315 // Existing member can be replaced with both binary and logical update.
316 {
317 SCOPED_TRACE("");
318 do_partial_update(set_member, &m_field, "{\"a\":\"b\"}", "{\"a\":1}",
319 true, true);
320 }
321
322 // Non-existing member can be added with logical update.
323 {
324 SCOPED_TRACE("");
325 do_partial_update(set_member, &m_field, "{}", "{\"a\":1}", false, true);
326 }
327 {
328 SCOPED_TRACE("");
329 do_partial_update(set_member, &m_field, "[5,6,7]", "[5,6,7]", true, true);
330 }
331 {
332 SCOPED_TRACE("");
333 do_partial_update(set_member, &m_field, "123", "123", true, true);
334 }
335 {
336 SCOPED_TRACE("");
337 do_partial_update(set_member, &m_field, nullptr, nullptr, false, false);
338 }
339 }
340
341 // Replacing object member.
342 {
343 auto replace =
344 new Item_func_json_replace(thd(), new Item_field(&m_field),
345 new_item_string("$.a"), new Item_int(1));
346
347 // Existing member can be replaced with both binary and logical update.
348 {
349 SCOPED_TRACE("");
350 do_partial_update(replace, &m_field, "{\"a\":\"b\"}", "{\"a\":1}", true,
351 true);
352 }
353
354 // Replacing non-existing member is a no-op.
355 {
356 SCOPED_TRACE("");
357 do_partial_update(replace, &m_field, "{}", "{}", true, true);
358 }
359 {
360 SCOPED_TRACE("");
361 do_partial_update(replace, &m_field, "[5,6,7]", "[5,6,7]", true, true);
362 }
363 {
364 SCOPED_TRACE("");
365 do_partial_update(replace, &m_field, "123", "123", true, true);
366 }
367 {
368 SCOPED_TRACE("");
369 do_partial_update(replace, &m_field, nullptr, nullptr, false, false);
370 }
371 }
372
373 // Setting array element.
374 {
375 auto set_element =
376 new Item_func_json_set(thd(), new Item_field(&m_field),
377 new_item_string("$[1]"), new Item_int(1));
378 {
379 SCOPED_TRACE("");
380 do_partial_update(set_element, &m_field, "[4,5,6]", "[4,1,6]", true,
381 true);
382 }
383 {
384 SCOPED_TRACE("");
385 do_partial_update(set_element, &m_field, "[]", "[1]", false, true);
386 }
387 {
388 SCOPED_TRACE("");
389 do_partial_update(set_element, &m_field, "[2]", "[2,1]", false, true);
390 }
391 {
392 SCOPED_TRACE("");
393 do_partial_update(set_element, &m_field, "{\"a\":2}", "[{\"a\":2},1]",
394 false, false);
395 }
396 {
397 SCOPED_TRACE("");
398 do_partial_update(set_element, &m_field, "123", "[123,1]", false, false);
399 }
400 {
401 SCOPED_TRACE("");
402 do_partial_update(set_element, &m_field, nullptr, nullptr, false, false);
403 }
404 }
405 {
406 auto set_element =
407 new Item_func_json_set(thd(), new Item_field(&m_field),
408 new_item_string("$.a[1]"), new Item_int(1));
409 {
410 SCOPED_TRACE("");
411 do_partial_update(set_element, &m_field, "{\"a\":[4,5,6]}",
412 "{\"a\":[4,1,6]}", true, true);
413 }
414 {
415 SCOPED_TRACE("");
416 do_partial_update(set_element, &m_field, "{\"a\":[]}", "{\"a\":[1]}",
417 false, true);
418 }
419 {
420 SCOPED_TRACE("");
421 do_partial_update(set_element, &m_field, "{\"a\":{\"b\":2}}",
422 "{\"a\":[{\"b\":2},1]}", false, true);
423 }
424 {
425 SCOPED_TRACE("");
426 do_partial_update(set_element, &m_field, "{\"a\":123}", "{\"a\":[123,1]}",
427 false, true);
428 }
429 }
430
431 // Replacing array element.
432 {
433 auto replace =
434 new Item_func_json_replace(thd(), new Item_field(&m_field),
435 new_item_string("$[1]"), new Item_int(1));
436 {
437 SCOPED_TRACE("");
438 do_partial_update(replace, &m_field, "[4,5,6]", "[4,1,6]", true, true);
439 }
440 {
441 SCOPED_TRACE("");
442 do_partial_update(replace, &m_field, "[]", "[]", true, true);
443 }
444 {
445 SCOPED_TRACE("");
446 do_partial_update(replace, &m_field, "[2]", "[2]", true, true);
447 }
448 {
449 SCOPED_TRACE("");
450 do_partial_update(replace, &m_field, "{\"a\":2}", "{\"a\":2}", true,
451 true);
452 }
453 {
454 SCOPED_TRACE("");
455 do_partial_update(replace, &m_field, "123", "123", true, true);
456 }
457 {
458 SCOPED_TRACE("");
459 do_partial_update(replace, &m_field, nullptr, nullptr, false, false);
460 }
461 }
462 {
463 auto replace =
464 new Item_func_json_replace(thd(), new Item_field(&m_field),
465 new_item_string("$.a[1]"), new Item_int(1));
466 {
467 SCOPED_TRACE("");
468 do_partial_update(replace, &m_field, "{\"a\":[4,5,6]}", "{\"a\":[4,1,6]}",
469 true, true);
470 }
471 {
472 SCOPED_TRACE("");
473 do_partial_update(replace, &m_field, "{\"a\":[]}", "{\"a\":[]}", true,
474 true);
475 }
476 {
477 SCOPED_TRACE("");
478 do_partial_update(replace, &m_field, "{\"a\":{\"b\":2}}",
479 "{\"a\":{\"b\":2}}", true, true);
480 }
481 {
482 SCOPED_TRACE("");
483 do_partial_update(replace, &m_field, "{\"a\":123}", "{\"a\":123}", true,
484 true);
485 }
486 }
487
488 // Remove an element in an array.
489 {
490 auto remove = new Item_func_json_remove(thd(), new Item_field(&m_field),
491 new_item_string("$[1]"));
492 {
493 SCOPED_TRACE("");
494 do_partial_update(remove, &m_field, "{}", "{}", true, true);
495 }
496 {
497 SCOPED_TRACE("");
498 do_partial_update(remove, &m_field, "[]", "[]", true, true);
499 }
500 {
501 SCOPED_TRACE("");
502 do_partial_update(remove, &m_field, nullptr, nullptr, false, false);
503 }
504 {
505 SCOPED_TRACE("");
506 do_partial_update(remove, &m_field, "[1,2,3]", "[1,3]", true, true);
507 }
508 {
509 SCOPED_TRACE("");
510 do_partial_update(remove, &m_field, "[1,2]", "[1]", true, true);
511 }
512 {
513 SCOPED_TRACE("");
514 do_partial_update(remove, &m_field, "[1]", "[1]", true, true);
515 }
516 {
517 SCOPED_TRACE("");
518 do_partial_update(remove, &m_field, "{\"a\":1}", "{\"a\":1}", true, true);
519 }
520 {
521 SCOPED_TRACE("");
522 do_partial_update(remove, &m_field, "123", "123", true, true);
523 }
524 {
525 SCOPED_TRACE("");
526 do_partial_update(remove, &m_field, nullptr, nullptr, false, false);
527 }
528 }
529
530 // Remove a member from an object.
531 {
532 auto remove = new Item_func_json_remove(thd(), new Item_field(&m_field),
533 new_item_string("$.x"));
534 {
535 SCOPED_TRACE("");
536 do_partial_update(remove, &m_field, "{}", "{}", true, true);
537 }
538 {
539 SCOPED_TRACE("");
540 do_partial_update(remove, &m_field, "{\"a\":1}", "{\"a\":1}", true, true);
541 }
542 {
543 SCOPED_TRACE("");
544 do_partial_update(remove, &m_field, "{\"x\":1}", "{}", true, true);
545 }
546 {
547 SCOPED_TRACE("");
548 do_partial_update(remove, &m_field,
549 "{\"a\":\"b\",\"c\":\"d\",\"x\":\"y\",\"z\":\"w\"}",
550 "{\"a\":\"b\",\"c\":\"d\",\"z\":\"w\"}", true, true);
551 }
552 {
553 SCOPED_TRACE("");
554 do_partial_update(remove, &m_field, "[1,2,3]", "[1,2,3]", true, true);
555 }
556 {
557 SCOPED_TRACE("");
558 do_partial_update(remove, &m_field, "123", "123", true, true);
559 }
560 {
561 SCOPED_TRACE("");
562 do_partial_update(remove, &m_field, nullptr, nullptr, false, false);
563 }
564 }
565
566 // Remove multiple paths.
567 {
568 Item *itf2 = new Item_field(&m_field);
569 auto remove = new Item_func_json_remove(
570 thd(), itf2, new_item_string("$.a.b"), new_item_string("$.c[1]"));
571 {
572 SCOPED_TRACE("");
573 do_partial_update(remove, &m_field, "{}", "{}", true, true);
574 }
575 {
576 SCOPED_TRACE("");
577 do_partial_update(remove, &m_field,
578 "{\"a\":{\"b\":\"c\"}, \"b\":{\"c\":\"d\"}, "
579 "\"c\":[1,2,3]}",
580 "{\"a\":{}, \"b\":{\"c\":\"d\"}, \"c\":[1,3]}", true,
581 true);
582 }
583 {
584 SCOPED_TRACE("");
585 do_partial_update(remove, &m_field,
586 "{\"a\":{\"b\":\"c\"}, \"b\":{\"c\":\"d\"}}",
587 "{\"a\":{}, \"b\":{\"c\":\"d\"}}", true, true);
588 }
589 {
590 SCOPED_TRACE("");
591 do_partial_update(remove, &m_field,
592 "{\"b\":{\"c\":\"d\"}, \"c\":[1,2,3]}",
593 "{\"b\":{\"c\":\"d\"}, \"c\":[1,3]}", true, true);
594 }
595 }
596
597 // JSON_REMOVE with NULL as path.
598 {
599 SCOPED_TRACE("");
600 auto remove = new Item_func_json_remove(thd(), new Item_field(&m_field),
601 new Item_null());
602 do_partial_update(remove, &m_field, "[1,2]", nullptr, false, false);
603 }
604
605 // Mixed JSON_REMOVE/JSON_SET.
606 {
607 Item *itf3 = new Item_field(&m_field);
608 auto set = new Item_func_json_set(thd(), itf3, new_item_string("$.a"),
609 new_item_string("abc"));
610 auto remove = new Item_func_json_remove(thd(), set, new_item_string("$.b"));
611
612 {
613 SCOPED_TRACE("");
614 do_partial_update(remove, &m_field, "{}", "{\"a\":\"abc\"}", false, true);
615 }
616 {
617 SCOPED_TRACE("");
618 do_partial_update(remove, &m_field, nullptr, nullptr, false, false);
619 }
620 {
621 SCOPED_TRACE("");
622 do_partial_update(remove, &m_field, "{\"b\":123}", "{\"a\":\"abc\"}",
623 false, true);
624 }
625 {
626 SCOPED_TRACE("");
627 do_partial_update(remove, &m_field, "{\"a\":\"xyz\",\"b\":123}",
628 "{\"a\":\"abc\"}", true, true);
629 }
630 {
631 SCOPED_TRACE("");
632 do_partial_update(remove, &m_field, nullptr, nullptr, false, false);
633 }
634 }
635
636 // Remove with auto-wrap.
637 {
638 auto remove = new Item_func_json_remove(
639 thd(), new Item_field(&m_field), new_item_string("$[0][0].a[0][0].b"));
640 {
641 SCOPED_TRACE("");
642 do_partial_update(remove, &m_field, "{}", "{}", true, true);
643 }
644 {
645 SCOPED_TRACE("");
646 do_partial_update(remove, &m_field, "[]", "[]", true, true);
647 }
648 {
649 SCOPED_TRACE("");
650 do_partial_update(remove, &m_field, "{\"a\":{\"b\":1,\"c\":2}}",
651 "{\"a\":{\"c\":2}}", true, true);
652 }
653 {
654 SCOPED_TRACE("");
655 do_partial_update(remove, &m_field,
656 "[{\"a\":[{\"b\":1,\"c\":2},123]}, 456]",
657 "[{\"a\":[{\"c\":2},123]}, 456]", true, true);
658 }
659 {
660 SCOPED_TRACE("");
661 do_partial_update(remove, &m_field, nullptr, nullptr, false, false);
662 }
663 }
664
665 // Append or prepend when setting with out-of-bounds array indexes.
666 {
667 SCOPED_TRACE("");
668 auto set = new Item_func_json_set(
669 thd(), new Item_field(&m_field), new_item_string("$[2]"),
670 new Item_int(88), new_item_string("$[last-2]"), new Item_int(99));
671 do_partial_update(set, &m_field, "[]", "[99,88]", false, true);
672 do_partial_update(set, &m_field, "[1]", "[99,1,88]", false, true);
673 do_partial_update(set, &m_field, "[1,2]", "[99,2,88]", false, true);
674 do_partial_update(set, &m_field, "[1,2,3]", "[99,2,88]", true, true);
675 do_partial_update(set, &m_field, "[1,2,3,4]", "[1,99,88,4]", true, true);
676 do_partial_update(set, &m_field, nullptr, nullptr, false, false);
677 }
678 }
679
680 TEST(FieldJSONTest, TruncatedSortKey) {
681 my_testing::Server_initializer initializer;
682 initializer.SetUp();
683
684 const Json_wrapper doc(parse_json("47"));
685
686 Base_mock_field_json field;
687 Fake_TABLE table(&field);
688 field.make_writable();
689 EXPECT_EQ(TYPE_OK, field.store_json(&doc));
690
691 uchar reference[1024];
692 size_t reference_len = field.make_sort_key(reference, sizeof(reference));
693 ASSERT_LT(reference_len, sizeof(reference));
694
695 for (size_t max_len = 0; max_len < 100; ++max_len) {
696 std::unique_ptr<uchar[]> buf(new uchar[max_len]);
697 size_t len = field.make_sort_key(buf.get(), max_len);
698 EXPECT_EQ(len, std::min(max_len, reference_len));
699 EXPECT_EQ(0, memcmp(buf.get(), reference, len));
700 }
701 initializer.TearDown();
702 }
703
704 /**
705 Microbenchmark which tests the performance of the JSON_SEARCH function.
706 */
707 static void BM_JsonSearch(size_t num_iterations) {
708 StopBenchmarkTiming();
709
710 my_testing::Server_initializer initializer;
711 initializer.SetUp();
712
713 const Json_wrapper doc(
714 parse_json("[\"Apple\", \"Orange\", \"Peach\","
715 "{\"key1\":\"Apple\", \"key2\":\"Orange\","
716 "\"key3\":\"Peach\"}, 1, 2, 3, 4, 5, 6]"));
717
718 Base_mock_field_json field;
719 Fake_TABLE table(&field);
720 field.make_writable();
721 EXPECT_EQ(TYPE_OK, field.store_json(&doc));
722
723 Item *itf = new Item_field(&field);
724 auto search = new Item_func_json_search(
725 table.in_use, itf, new_item_string("all"), new_item_string("Apple"));
726 EXPECT_FALSE(search->fix_fields(table.in_use, nullptr));
727
728 StartBenchmarkTiming();
729
730 for (size_t i = 0; i < num_iterations; ++i) {
731 Json_wrapper wr;
732 EXPECT_FALSE(search->val_json(&wr));
733 EXPECT_FALSE(search->null_value);
734 EXPECT_EQ(2U, wr.length());
735 }
736
737 StopBenchmarkTiming();
738 initializer.TearDown();
739 }
740 BENCHMARK(BM_JsonSearch)
741
742 /**
743 Microbenchmark which tests the performance of the JSON_SEARCH
744 function when it's called with arguments that contain wildcards.
745 */
746 static void BM_JsonSearch_Wildcard(size_t num_iterations) {
747 StopBenchmarkTiming();
748
749 my_testing::Server_initializer initializer;
750 initializer.SetUp();
751
752 const Json_wrapper doc(
753 parse_json("[{\"key1\": \"abc\","
754 " \"key2\": \"def\","
755 " \"key3\": \"abc\"},"
756 " {\"key1\": \"def\","
757 " \"key2\": \"abc\","
758 " \"key3\": \"def\"},"
759 " {\"key4\": {\"key4\":\"abc\"}}]"));
760
761 Base_mock_field_json field;
762 Fake_TABLE table(&field);
763 field.make_writable();
764 EXPECT_EQ(TYPE_OK, field.store_json(&doc));
765
766 mem_root_deque<Item *> args(*THR_MALLOC);
767 args.push_back(new Item_field(&field));
768 args.push_back(new_item_string("all"));
769 args.push_back(new_item_string("abc"));
770 args.push_back(new_item_string("")); // escape character
771 args.push_back(new_item_string("$[*].key1"));
772 args.push_back(new_item_string("$**.key4"));
773
774 auto search = new Item_func_json_search(table.in_use, &args);
775 EXPECT_FALSE(search->fix_fields(table.in_use, nullptr));
776
777 StartBenchmarkTiming();
778
779 for (size_t i = 0; i < num_iterations; ++i) {
780 Json_wrapper wr;
781 EXPECT_FALSE(search->val_json(&wr));
782 EXPECT_FALSE(search->null_value);
783 EXPECT_EQ(2U, wr.length());
784 }
785
786 StopBenchmarkTiming();
787 initializer.TearDown();
788 }
789 BENCHMARK(BM_JsonSearch_Wildcard)
790
791 } // namespace item_json_func_unittest