MySQL 8.0.39
Source Code Documentation
item_json_func.h
Go to the documentation of this file.
1#ifndef ITEM_JSON_FUNC_INCLUDED
2#define ITEM_JSON_FUNC_INCLUDED
3
4/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27#include <assert.h>
28#include <sys/types.h>
29
30#include <cstddef>
31#include <cstdint>
32#include <memory>
33#include <utility> // std::forward
34
35#include "m_ctype.h"
36
37#include "my_inttypes.h"
38#include "my_time.h"
40#include "mysql_com.h"
41#include "mysql_time.h"
42#include "prealloced_array.h" // Prealloced_array
43#include "sql-common/json_path.h" // Json_path
44#include "sql/enum_query_type.h"
45#include "sql/field.h"
46#include "sql/item.h"
47#include "sql/item_cmpfunc.h"
48#include "sql/item_func.h"
49#include "sql/item_strfunc.h" // Item_str_func
50#include "sql/mem_root_array.h" // Mem_root_array
51#include "sql/parse_location.h" // POS
52#include "sql/psi_memory_key.h" // key_memory_JSON
53#include "sql_string.h"
54
56class Json_array;
57class Json_dom;
59class Json_wrapper;
60class PT_item_list;
61class THD;
62class my_decimal;
63enum Cast_target : unsigned char;
64enum class Json_on_response_type : uint16;
65struct Cast_type;
66struct TABLE;
67
68/** For use by JSON_CONTAINS_PATH() and JSON_SEARCH() */
75};
76
77/**
78 Path cache for JSON functions. Caches parsed path
79 objects for arguments which are string literals.
80 Maintains a vector of path objects and an array of
81 ints which map path argument numbers to slots in
82 the array.
83*/
85 private:
86 /// Holder for path strings.
88
89 /// List of paths.
91
92 /// Enum that tells the status of a cell in m_paths.
93 enum class enum_path_status : uint8 {
96 OK_NULL,
97 };
98
99 /// Struct that points to a cell in m_paths and tells its status.
100 struct Path_cell {
102 size_t m_index = 0;
103 };
104
105 /// Map argument indexes to indexes into m_paths.
107
108 public:
109 Json_path_cache(THD *thd, uint size);
111
112 /**
113 Parse a path expression if necessary. Does nothing if the path
114 expression is constant and it has already been parsed. Assumes that
115 we've already verified that the path expression is not null. Raises an
116 error if the path expression is syntactically incorrect. Raises an
117 error if the path expression contains wildcard tokens but is not
118 supposed to. Otherwise puts the parsed path onto the
119 path vector.
120
121 @param[in] thd THD handle
122 @param[in] args Array of args to a JSON function
123 @param[in] arg_idx Index of the path_expression in args
124 @param[in] forbid_wildcards True if the path shouldn't contain * or **
125
126 @returns false on success (valid path or NULL), true on error
127 */
128 bool parse_and_cache_path(const THD *thd, Item **args, uint arg_idx,
129 bool forbid_wildcards);
130
131 /**
132 Return an already parsed path expression.
133
134 @param[in] arg_idx Index of the path_expression in the JSON function args
135
136 @returns the already parsed path, possibly NULL
137 */
138 const Json_path *get_path(uint arg_idx) const;
139
140 /**
141 Reset the cache for re-use when a statement is re-executed.
142 */
143 void reset_cache();
144};
145
146/* JSON function support */
147
148/**
149 Base class for all item functions that a return JSON value
150*/
151class Item_json_func : public Item_func {
152 /// Can this function type be used in partial update?
153 virtual bool can_use_in_partial_update() const { return false; }
154
155 protected:
156 /// String used when reading JSON binary values or JSON text values.
158 /// String used for converting JSON text values to utf8mb4 charset.
160 /// String used for converting a JSON value to text in val_str().
162
163 // Cache for constant path expressions
165
166 /**
167 Target column for partial update, if this function is used in an
168 update statement and partial update can be used.
169 */
171
172 public:
173 /**
174 Construct an Item_json_func instance.
175 @param thd THD handle
176 @param parent_args arguments to forward to Item_func's constructor
177 */
178 template <typename... Args>
179 Item_json_func(THD *thd, Args &&... parent_args)
180 : Item_func(std::forward<Args>(parent_args)...),
181 m_path_cache(thd, arg_count) {
183 }
184
185 bool resolve_type(THD *) override {
186 set_nullable(true);
187 return false;
188 }
189 enum Item_result result_type() const override { return STRING_RESULT; }
190 String *val_str(String *arg) override;
191 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
192 bool get_time(MYSQL_TIME *ltime) override;
193 longlong val_int() override;
194 double val_real() override;
195 my_decimal *val_decimal(my_decimal *decimal_value) override;
196
197 void cleanup() override;
198
199 Item_result cast_to_int_type() const override { return INT_RESULT; }
200
201 /**
202 Does this function call support partial update of the given JSON column?
203
204 JSON_SET, JSON_REPLACE and JSON_REMOVE support partial update of a JSON
205 column if the JSON column is the first argument of the function call, or if
206 the first argument is a sequence of nested JSON_SET, JSON_REPLACE and
207 JSON_REMOVE calls in which the JSON column is the first argument of the
208 inner function call.
209
210 For example, this expression can be used to partially update column
211 `json_col`:
212
213 JSON_SET(JSON_REPLACE(json_col, path1, val1), path2, val2)
214 */
215 bool supports_partial_update(const Field_json *field) const override;
216
217 /**
218 Mark this expression as used in partial update. Should only be
219 called if #supports_partial_update returns true.
220 */
221 void mark_for_partial_update(const Field_json *field);
222};
223
224bool sql_scalar_to_json(Item *arg, const char *calling_function, String *value,
225 String *tmp, Json_wrapper *wr,
226 Json_scalar_holder *scalar, bool scalar_string);
227
228/**
229 Return the JSON value of the argument in a wrapper.
230
231 Handles arguments with type JSON, including array objects (which do
232 not report type JSON but rather the type of individual elements).
233
234 Does not handle literals.
235 See also get_json_wrapper.
236
237 @param[in] arg the argument
238 @param[in,out] result the JSON value wrapper
239 @param[out] has_value true if argument was handled, false otherwise
240 undefined when error
241*/
242bool json_value(Item *arg, Json_wrapper *result, bool *has_value);
243
244/**
245 Return the JSON value of the argument in a wrapper. Abstracts whether
246 the value comes from a field or a function or a valid JSON text.
247
248 @param[in] args the arguments
249 @param[in] arg_idx the argument index
250 @param[out] str the string buffer
251 @param[in] func_name the name of the function we are executing
252 @param[out] wrapper the JSON value wrapper
253 @returns false if we found a value or NULL, true if not.
254*/
255bool get_json_wrapper(Item **args, uint arg_idx, String *str,
256 const char *func_name, Json_wrapper *wrapper);
257
258/**
259 Convert Json values or MySQL values to JSON.
260
261 @param[in] args arguments to function
262 @param[in] arg_idx the index of the argument to process
263 @param[in] calling_function name of the calling function
264 @param[in,out] value working area (if the returned Json_wrapper points
265 to a binary value rather than a DOM, this string
266 will end up holding the binary representation, and
267 it must stay alive until the wrapper is destroyed
268 or converted from binary to DOM)
269 @param[in,out] tmp temporary scratch space for converting strings to
270 the correct charset; only used if accept_string is
271 true and conversion is needed
272 @param[in,out] wr the result wrapper
273 @param[in,out] scalar pointer to pre-allocated memory that can be
274 borrowed by the result wrapper if the result is a
275 scalar. If the pointer is NULL, memory for a
276 scalar result will be allocated on the heap.
277 @param[in] accept_string
278 if true, accept MySQL strings as JSON strings
279 by converting them to UTF8, else emit an error
280 @returns false if we found a value or NULL, true otherwise
281*/
282bool get_json_atom_wrapper(Item **args, uint arg_idx,
283 const char *calling_function, String *value,
284 String *tmp, Json_wrapper *wr,
285 Json_scalar_holder *scalar, bool accept_string);
286
287/**
288 Check a non-empty val for character set. If it has character set
289 my_charset_binary, signal error and return false. Else, try to convert to
290 my_charset_utf8mb4_bin. If this fails, signal error and return true, else
291 return false.
292
293 @param[in] val the string to be checked
294 @param[in,out] buf buffer to hold the converted string
295 @param[out] resptr the resulting, possibly converted string,
296 only set if no error
297 @param[out] reslength the length of resptr
298 @param[in] require_string
299 If true, give error messages if binary string. If we
300 see a conversion error (space), we give error
301 notwithstanding this parameter value
302
303 @returns True if the string could not be converted. False on success.
304*/
305bool ensure_utf8mb4(const String &val, String *buf, const char **resptr,
306 size_t *reslength, bool require_string);
307
308/**
309 Represents the JSON function JSON_VALID( <value> )
310*/
313
314 public:
315 Item_func_json_valid(const POS &pos, Item *a) : Item_int_func(pos, a) {}
316
317 const char *func_name() const override { return "json_valid"; }
318
319 bool is_bool_func() const override { return true; }
320
321 longlong val_int() override;
322
323 bool resolve_type(THD *thd) override {
324 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_JSON)) return true;
325 set_nullable(true);
326 return false;
327 }
328};
329
330/**
331 Represents the JSON function JSON_SCHEMA_VALID( <json schema>, <json doc> )
332*/
334 public:
335 Item_func_json_schema_valid(const POS &pos, Item *a, Item *b);
337
338 const char *func_name() const override { return "json_schema_valid"; }
339
340 bool val_bool() override;
341
342 longlong val_int() override { return val_bool() ? 1 : 0; }
343
344 bool fix_fields(THD *, Item **) override;
345
346 void cleanup() override;
347
348 private:
349 // Wrap the object in a unique_ptr so that the relevant rapidjson destructors
350 // are called.
353};
354
355/**
356 Represents the JSON function
357 JSON_SCHEMA_VALIDATION_REPORT( <json schema>, <json doc> )
358*/
360 public:
362 PT_item_list *a);
364
365 const char *func_name() const override {
366 return "json_schema_validation_report";
367 }
368
369 bool val_json(Json_wrapper *wr) override;
370
371 bool resolve_type(THD *thd) override {
372 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_JSON)) return true;
373 set_nullable(true);
374 return false;
375 }
376
377 bool fix_fields(THD *, Item **) override;
378
379 void cleanup() override;
380
381 private:
382 // Wrap the object in a unique_ptr so that the relevant rapidjson destructors
383 // are called.
386};
387
388/**
389 Represents the JSON function JSON_CONTAINS()
390*/
394
395 public:
397 : Item_int_func(pos, a), m_path_cache(thd, arg_count) {}
398
399 const char *func_name() const override { return "json_contains"; }
400 enum Functype functype() const override { return JSON_CONTAINS; }
401 optimize_type select_optimize(const THD *) override { return OPTIMIZE_KEY; }
402 bool gc_subst_analyzer(uchar **) override { return true; }
403
404 bool is_bool_func() const override { return true; }
405
406 longlong val_int() override;
407
408 bool resolve_type(THD *thd) override {
409 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_JSON)) return true;
410 if (param_type_is_default(thd, 1, 3)) return true;
411 set_nullable(true);
412 return false;
413 }
414
415 /** Cleanup between executions of the statement */
416 void cleanup() override;
417
419 return (arg == args[0] || arg == args[1]) ? CACHE_JSON_VALUE : CACHE_NONE;
420 }
421};
422
423/**
424 Represents the JSON function JSON_CONTAINS_PATH()
425*/
429
430 // Cache for constant path expressions
432
433 public:
435 : Item_int_func(pos, a),
437 m_path_cache(thd, arg_count) {}
438
439 const char *func_name() const override { return "json_contains_path"; }
440
441 bool is_bool_func() const override { return true; }
442
443 longlong val_int() override;
444
445 bool resolve_type(THD *thd) override {
446 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_JSON)) return true;
447 if (param_type_is_default(thd, 1, -1)) return true;
448 set_nullable(true);
449 return false;
450 }
451
452 /** Cleanup between executions of the statement */
453 void cleanup() override;
454
456 return (arg == args[0]) ? CACHE_JSON_VALUE : CACHE_NONE;
457 }
458};
459
460/**
461 Represents the JSON function JSON_TYPE
462*/
465
466 public:
467 Item_func_json_type(const POS &pos, Item *a) : Item_str_func(pos, a) {}
468
469 const char *func_name() const override { return "json_type"; }
470
471 bool resolve_type(THD *) override;
472
473 String *val_str(String *) override;
474};
475
476/**
477 Represents a "CAST( <value> AS JSON )" coercion.
478*/
479class Item_typecast_json final : public Item_json_func {
481
482 public:
483 Item_typecast_json(THD *thd, const POS &pos, Item *a)
484 : Item_json_func(thd, pos, a) {}
485
486 bool resolve_type(THD *thd) override {
487 if (Item_json_func::resolve_type(thd)) return true;
488 return args[0]->propagate_type(thd, MYSQL_TYPE_JSON, false, true);
489 }
490
491 void print(const THD *thd, String *str,
492 enum_query_type query_type) const override;
493 const char *func_name() const override { return "cast_as_json"; }
494 const char *cast_type() const { return "json"; }
495 bool val_json(Json_wrapper *wr) override;
496};
497
498/**
499 Represents the JSON function JSON_LENGTH()
500*/
503
504 public:
505 Item_func_json_length(const POS &pos, Item *doc) : Item_int_func(pos, doc) {}
506
507 bool resolve_type(THD *thd) override {
508 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_JSON)) return true;
509 if (param_type_is_default(thd, 1, 2)) return true;
510 set_nullable(true);
511 return false;
512 }
513
514 const char *func_name() const override { return "json_length"; }
515
516 longlong val_int() override;
517};
518
519/**
520 Represents the JSON function JSON_DEPTH()
521*/
524
525 public:
526 Item_func_json_depth(const POS &pos, Item *a) : Item_int_func(pos, a) {}
527
528 const char *func_name() const override { return "json_depth"; }
529
530 bool resolve_type(THD *thd) override {
531 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_JSON)) return true;
532 set_nullable(true);
533 return false;
534 }
535
536 longlong val_int() override;
537};
538
539/**
540 Represents the JSON function JSON_KEYS()
541*/
544
545 public:
546 Item_func_json_keys(THD *thd, const POS &pos, Item *a)
547 : Item_json_func(thd, pos, a) {}
548
549 Item_func_json_keys(THD *thd, const POS &pos, Item *a, Item *b)
550 : Item_json_func(thd, pos, a, b) {}
551
552 const char *func_name() const override { return "json_keys"; }
553
554 bool resolve_type(THD *thd) override {
555 if (Item_json_func::resolve_type(thd)) return true;
556 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_JSON)) return true;
557 if (param_type_is_default(thd, 1, 2)) return true;
558 return false;
559 }
560
561 bool val_json(Json_wrapper *wr) override;
562};
563
564/**
565 Represents the JSON function JSON_EXTRACT()
566*/
569
570 public:
572 : Item_json_func(thd, pos, a) {}
573
574 Item_func_json_extract(THD *thd, const POS &pos, Item *a, Item *b)
575 : Item_json_func(thd, pos, a, b) {}
576
577 const char *func_name() const override { return "json_extract"; }
578
579 bool resolve_type(THD *thd) override {
580 if (Item_json_func::resolve_type(thd)) return true;
581 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_JSON)) return true;
582 if (param_type_is_default(thd, 1, -1)) return true;
583 return false;
584 }
585
586 bool val_json(Json_wrapper *wr) override;
587
588 bool eq(const Item *item, bool binary_cmp) const override;
589};
590
591/// Base class for all the functions that take a JSON document as the first
592/// argument and one of more pairs of a JSON path and a value to insert into the
593/// JSON document, and returns the modified JSON document.
595 protected:
596 template <typename... Args>
597 explicit Item_func_modify_json_in_path(Args &&... parent_args)
598 : Item_json_func(std::forward<Args>(parent_args)...) {
599 // The function does not necessarily return NULL when an argument is NULL.
600 // It returns NULL only if the first argument is NULL, or if one of the JSON
601 // path arguments is null. The set of tables for which the function is
602 // null-rejecting, is calculated in resolve_type() and possibly updated in
603 // update_used_tables().
604 null_on_null = false;
605 }
607
608 public:
609 bool resolve_type(THD *thd) final;
610 void update_used_tables() final;
611
612 private:
613 /// Calculates the set of tables to return from not_used_tables(). The
614 /// returned value is cached by resolve_type() and update_used_tables().
616};
617
618/**
619 Represents the JSON function JSON_ARRAY_APPEND()
620*/
622 public:
624 : Item_func_modify_json_in_path(thd, pos, a) {}
625
626 const char *func_name() const override { return "json_array_append"; }
627
628 bool val_json(Json_wrapper *wr) override;
629};
630
631/**
632 Represents the JSON function JSON_INSERT()
633*/
635 public:
637 : Item_func_modify_json_in_path(thd, pos, a) {}
638
639 const char *func_name() const override { return "json_insert"; }
640
641 bool val_json(Json_wrapper *wr) override;
642};
643
644/**
645 Represents the JSON function JSON_ARRAY_INSERT()
646*/
648 public:
650 : Item_func_modify_json_in_path(thd, pos, a) {}
651
652 const char *func_name() const override { return "json_array_insert"; }
653
654 bool val_json(Json_wrapper *wr) override;
655};
656
657/**
658 Common base class for JSON_SET() and JSON_REPLACE().
659*/
661 /// True if this is JSON_SET, false if it is JSON_REPLACE.
662 const bool m_json_set;
664 bool can_use_in_partial_update() const override { return true; }
665
666 protected:
667 template <typename... Args>
668 explicit Item_func_json_set_replace(bool json_set, Args &&... parent_args)
669 : Item_func_modify_json_in_path(std::forward<Args>(parent_args)...),
670 m_json_set(json_set),
671 m_path(key_memory_JSON) {}
672
673 public:
674 bool val_json(Json_wrapper *wr) override;
675};
676
677/**
678 Represents the JSON function JSON_SET()
679*/
681 public:
682 template <typename... Args>
683 explicit Item_func_json_set(Args &&... parent_args)
684 : Item_func_json_set_replace(true, std::forward<Args>(parent_args)...) {}
685
686 const char *func_name() const override { return "json_set"; }
687};
688
689/**
690 Represents the JSON function JSON_REPLACE()
691*/
693 public:
694 template <typename... Args>
695 explicit Item_func_json_replace(Args &&... parent_args)
696 : Item_func_json_set_replace(false, std::forward<Args>(parent_args)...) {}
697
698 const char *func_name() const override { return "json_replace"; }
699};
700
701/**
702 Represents the JSON function JSON_ARRAY()
703*/
705 public:
706 template <typename... Args>
707 explicit Item_func_json_array(Args &&... parent_args)
708 : Item_json_func(std::forward<Args>(parent_args)...) {
709 // Does not return NULL on NULL input. A NULL argument is interpreted as the
710 // JSON null literal.
711 null_on_null = false;
712 }
713
714 const char *func_name() const override { return "json_array"; }
715
716 bool resolve_type(THD *thd) override {
717 if (Item_json_func::resolve_type(thd)) return true;
718 if (param_type_is_default(thd, 0, -1)) return true;
719 return false;
720 }
721
722 bool val_json(Json_wrapper *wr) override;
723};
724
725/**
726 Represents the JSON function JSON_OBJECT()
727*/
730
731 public:
733 : Item_json_func(thd, pos, a) {
734 // Does not return NULL on NULL input. If a key argument is NULL, an error
735 // is raised. If a value argument is NULL, it is interpreted as the JSON
736 // null literal.
737 null_on_null = false;
738 }
739
740 const char *func_name() const override { return "json_object"; }
741
742 bool resolve_type(THD *thd) override {
743 if (Item_json_func::resolve_type(thd)) return true;
744 if (param_type_is_default(thd, 0, -1)) return true;
745 return false;
746 }
747
748 bool val_json(Json_wrapper *wr) override;
749};
750
751/**
752 Represents the JSON function JSON_SEARCH()
753*/
757
758 // LIKE machinery
761
762 public:
763 /**
764 Construct a JSON_SEARCH() node.
765
766 @param parent_args arguments to pass to Item_json_func's constructor
767 */
768 template <typename... Args>
769 Item_func_json_search(Args &&... parent_args)
770 : Item_json_func(std::forward<Args>(parent_args)...),
771 m_cached_ooa(ooa_uninitialized) {}
772
773 const char *func_name() const override { return "json_search"; }
774
775 bool val_json(Json_wrapper *wr) override;
776
777 /**
778 Bind logic for the JSON_SEARCH() node.
779 */
780 bool fix_fields(THD *, Item **) override;
781
782 bool resolve_type(THD *thd) override {
783 if (Item_json_func::resolve_type(thd)) return true;
784 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_JSON)) return true;
785 if (param_type_is_default(thd, 1, -1)) return true;
786 return false;
787 }
788
789 void cleanup() override;
790};
791
792/**
793 Represents the JSON function JSON_REMOVE()
794*/
797 bool can_use_in_partial_update() const override { return true; }
798
799 public:
800 template <typename... Args>
801 Item_func_json_remove(Args &&... parent_args)
802 : Item_json_func(std::forward<Args>(parent_args)...) {}
803
804 const char *func_name() const override { return "json_remove"; }
805
806 bool resolve_type(THD *thd) override {
807 if (Item_json_func::resolve_type(thd)) return true;
808 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_JSON)) return true;
809 if (param_type_is_default(thd, 1, -1)) return true;
810 return false;
811 }
812
813 bool val_json(Json_wrapper *wr) override;
814};
815
816/**
817 Represents the JSON function JSON_MERGE_PRESERVE.
818*/
820 public:
822 : Item_json_func(thd, pos, a) {}
823
824 const char *func_name() const override { return "json_merge_preserve"; }
825
826 bool resolve_type(THD *thd) override {
827 if (Item_json_func::resolve_type(thd)) return true;
828 if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_JSON)) return true;
829 return false;
830 }
831
832 bool val_json(Json_wrapper *wr) override;
833};
834
835/**
836 Represents the JSON function JSON_MERGE. It is a deprecated alias
837 for JSON_MERGE_PRESERVE.
838*/
840 public:
841 Item_func_json_merge(THD *thd, const POS &pos, PT_item_list *a);
842
843 bool is_deprecated() const override { return true; }
844};
845
846/**
847 Represents the JSON function JSON_MERGE_PATCH.
848*/
850 public:
852 : Item_json_func(thd, pos, a) {}
853
854 const char *func_name() const override { return "json_merge_patch"; }
855
856 bool resolve_type(THD *thd) override {
857 if (Item_json_func::resolve_type(thd)) return true;
858 if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_JSON)) return true;
859 return false;
860 }
861
862 bool val_json(Json_wrapper *wr) override;
863};
864
865/**
866 Represents the JSON function JSON_QUOTE()
867*/
870
871 public:
873 : Item_str_func(pos, a) {}
874
875 const char *func_name() const override { return "json_quote"; }
876
877 bool resolve_type(THD *thd) override {
878 if (param_type_is_default(thd, 0, -1)) return true;
879 set_nullable(true);
880
881 /*
882 Any interior character could be replaced by a 6 character
883 escape sequence. Plus we will add 2 framing quote characters.
884 */
885 uint32 max_char_length = (6 * args[0]->max_char_length()) + 2;
887 return false;
888 }
889
890 String *val_str(String *tmpspace) override;
891};
892
893/**
894 Represents the JSON function JSON_UNQUOTE()
895*/
899
900 public:
902 : Item_str_func(pos, a) {}
903
904 Item_func_json_unquote(const POS &pos, Item *a) : Item_str_func(pos, a) {}
905
906 const char *func_name() const override { return "json_unquote"; }
907
908 enum Functype functype() const override { return JSON_UNQUOTE_FUNC; }
909
910 bool resolve_type(THD *thd) override {
911 if (param_type_is_default(thd, 0, -1)) return true;
912 set_nullable(true);
914 return false;
915 }
916
917 String *val_str(String *str) override;
918};
919
920/**
921 Represents the JSON_PRETTY function.
922*/
924 public:
925 Item_func_json_pretty(const POS &pos, Item *a) : Item_str_func(pos, a) {}
926
927 const char *func_name() const override { return "json_pretty"; }
928
929 bool resolve_type(THD *thd) override {
930 if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_JSON)) return true;
932 return false;
933 }
934
935 String *val_str(String *str) override;
936};
937
938/**
939 Class that represents the function JSON_STORAGE_SIZE.
940*/
942 public:
944 : Item_int_func(pos, a) {}
945 const char *func_name() const override { return "json_storage_size"; }
946
947 bool resolve_type(THD *thd) override {
948 if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_JSON)) return true;
949 if (Item_int_func::resolve_type(thd)) return true;
950 set_nullable(true);
951 return false;
952 }
953
954 longlong val_int() override;
955};
956
957/**
958 Class that represents the function JSON_STORAGE_FREE.
959*/
961 public:
963 : Item_int_func(pos, a) {}
964 const char *func_name() const override { return "json_storage_free"; }
965
966 bool resolve_type(THD *thd) override {
967 if (param_type_is_default(thd, 0, -1, MYSQL_TYPE_JSON)) return true;
968 return false;
969 }
970
971 longlong val_int() override;
972};
973
974/**
975 Class that represents CAST(<expr> AS <type> ARRAY)
976*/
977
978class Item_func_array_cast final : public Item_func {
979 /// Type to cast to
981 /**
982 Whether use of CAST(.. AS .. ARRAY) is allowed
983
984 Currently use of CAST(.. AS .. ARRAY) is limited only to CREATE
985 TABLE/INDEX. In all other cases an error is thrown. This flag is set to
986 true only for allowed cases to ensure allowed function usage.
987 */
988 bool m_is_allowed{false};
989
990 /**
991 An array used by #save_in_field_inner() to store the result of an array cast
992 operation. It is cached in the Item in order to avoid the need for
993 reallocation on each row.
994 */
996
997 public:
998 Item_func_array_cast(const POS &pos, Item *a, Cast_target type, uint len_arg,
999 uint dec_arg, const CHARSET_INFO *cs_arg);
1001 const char *func_name() const override { return "cast_as_array"; }
1002 enum Functype functype() const override { return TYPECAST_FUNC; }
1003 bool returns_array() const override { return true; }
1004 bool val_json(Json_wrapper *wr) override;
1005 void print(const THD *thd, String *str,
1006 enum_query_type query_type) const override;
1007 enum Item_result result_type() const override;
1008 bool resolve_type(THD *) override;
1009 Field *tmp_table_field(TABLE *table) override;
1010 bool fix_fields(THD *thd, Item **ref) override;
1011 void cleanup() override;
1012 void allow_array_cast() override { m_is_allowed = true; }
1014 bool no_conversions) override;
1015 // Regular val_x() funcs shouldn't be called
1016 /* purecov: begin inspected */
1017 longlong val_int() override {
1018 assert(false);
1019 return 0;
1020 }
1021 String *val_str(String *) override {
1022 assert(false);
1023 return nullptr;
1024 }
1026 assert(false);
1027 return nullptr;
1028 }
1029 double val_real() override {
1030 assert(false);
1031 return 0;
1032 }
1034 assert(false);
1035 return true;
1036 }
1037 bool get_time(MYSQL_TIME *) override {
1038 assert(false);
1039 return true;
1040 }
1041 /* purecov: end */
1042};
1043
1045 public:
1047 : Item_bool_func(pos, a, b) {}
1048 const char *func_name() const override { return "json_overlaps"; }
1049 enum Functype functype() const override { return JSON_OVERLAPS; }
1050 bool gc_subst_analyzer(uchar **) override { return true; }
1051 optimize_type select_optimize(const THD *) override { return OPTIMIZE_KEY; }
1052 longlong val_int() override;
1053 Item *key_item() const override;
1055 return (arg == args[0] || arg == args[1]) ? CACHE_JSON_VALUE : CACHE_NONE;
1056 }
1057};
1058
1060 public:
1061 Item_func_member_of(const POS &pos, Item *a, Item *b)
1062 : Item_bool_func(pos, a, b) {}
1063 const char *func_name() const override { return "member of"; }
1064 enum Functype functype() const override { return MEMBER_OF_FUNC; }
1065 bool resolve_type(THD *thd) override {
1066 if (param_type_is_default(thd, 0, 2, MYSQL_TYPE_JSON)) return true;
1068 return false;
1069 }
1070 bool gc_subst_analyzer(uchar **) override { return true; }
1071 optimize_type select_optimize(const THD *) override { return OPTIMIZE_KEY; }
1072 longlong val_int() override;
1073 void print(const THD *thd, String *str,
1074 enum_query_type query_type) const override;
1075 Item *key_item() const override { return args[1]; }
1077 return (arg == args[1]) ? CACHE_JSON_VALUE
1078 : ((arg == args[0]) ? CACHE_JSON_ATOM : CACHE_NONE);
1079 }
1080};
1081
1082/**
1083 Class implementing the JSON_VALUE function.
1084
1085 Functionality-wise it's a combination of CAST, JSON_UNQUOTE and JSON_EXTRACT,
1086 but with additional functionality for flexible handling of empty values and
1087 conversion errors.
1088*/
1089class Item_func_json_value final : public Item_func {
1090 public:
1091 Item_func_json_value(const POS &pos, Item *arg, Item *path,
1092 const Cast_type &cast_type, unsigned length,
1093 unsigned precision, Json_on_response_type on_empty_type,
1094 Item *on_empty_default,
1095 Json_on_response_type on_error_type,
1096 Item *on_error_default);
1098 const char *func_name() const override { return "json_value"; }
1099 enum Item_result result_type() const override;
1100 bool resolve_type(THD *) override;
1101 bool fix_fields(THD *thd, Item **ref) override;
1102 void print(const THD *thd, String *str,
1103 enum_query_type query_type) const override;
1104 bool eq(const Item *item, bool binary_cmp) const override;
1105 bool val_json(Json_wrapper *wr) override;
1106 String *val_str(String *buffer) override;
1107 double val_real() override;
1108 longlong val_int() override;
1109 my_decimal *val_decimal(my_decimal *value) override;
1110 bool get_date(MYSQL_TIME *ltime, my_time_flags_t flags) override;
1111 bool get_time(MYSQL_TIME *ltime) override;
1112
1113 private:
1114 /// Represents a default value given in JSON_VALUE's DEFAULT xxx ON EMPTY or
1115 /// DEFAULT xxx ON ERROR clause.
1116 struct Default_value;
1117
1118 /// Parsed path.
1120 /// Type of the ON EMPTY clause.
1122 /// Type of the ON ERROR clause.
1124 /// The default value for ON EMPTY (if not ERROR or NULL ON EMPTY).
1126 /// The default value for ON EMPTY (if not ERROR or NULL ON EMPTY).
1128 /// The target data type.
1130
1131 /**
1132 Creates a Json_value_default object representing the default value given in
1133 a DEFAULT xxx ON EMPTY clause or a DEFAULT xxx ON ERROR clause.
1134
1135 @param thd the current session
1136 @param item the Item that represents the default value expression
1137 @return a pointer to the created object on success, nullptr on error
1138 */
1139 unique_ptr_destroy_only<Default_value> create_json_value_default(THD *thd,
1140 Item *item);
1141
1142 /**
1143 Extracts the JSON value at the given path.
1144
1145 @param[out] json the extracted JSON value, if the path matched exactly
1146 one value; empty otherwise
1147 @param[out] return_default the default value to return if a
1148 DEFAULT ... ON EMPTY or DEFAULT ... ON ERROR clause was invoked,
1149 or nullptr if no DEFAULT clause was invoked
1150 @return true if an error was raised, false otherwise
1151 */
1152 bool extract_json_value(Json_wrapper *json,
1153 const Default_value **return_default);
1154
1155 /// Implements val_int() for RETURNING SIGNED and RETURNING UNSIGNED.
1156 int64_t extract_integer_value();
1157 /// Implements val_int() for RETURNING YEAR
1158 int64_t extract_year_value();
1159 /// Implements get_date() for RETURNING DATE.
1160 bool extract_date_value(MYSQL_TIME *ltime);
1161 /// Implements get_time() for RETURNING TIME.
1162 bool extract_time_value(MYSQL_TIME *ltime);
1163 /// Implements get_date() for RETURNING DATETIME.
1164 bool extract_datetime_value(MYSQL_TIME *ltime);
1165 /// Implements val_decimal() for RETURNING DECIMAL.
1166 my_decimal *extract_decimal_value(my_decimal *value);
1167 /// Implements val_str() for RETURNING CHAR and RETURNING BINARY.
1168 String *extract_string_value(String *buffer);
1169 /// Implements val_real() for RETURNING FLOAT/REAL/DOUBLE.
1170 double extract_real_value();
1171};
1172
1173/**
1174 Turn a GEOMETRY value into a JSON value per the GeoJSON specification
1175 revision 1.0. This method is implemented in item_geofunc.cc.
1176
1177 @param[in,out] wr The wrapper to be stuffed with the JSON value.
1178 @param[in] swkb The source GEOMETRY value.
1179 @param[in] calling_function Name of user-invoked function (for errors)
1180 @param[in] max_decimal_digits See the user documentation for ST_AsGeoJSON.
1181 @param[in] add_bounding_box See the user documentation for ST_AsGeoJSON.
1182 @param[in] add_short_crs_urn See the user documentation for ST_AsGeoJSON.
1183 @param[in] add_long_crs_urn See the user documentation for ST_AsGeoJSON.
1184 @param[in,out] geometry_srid Spatial Reference System Identifier to be filled
1185 in.
1186
1187 @return false if the conversion succeeds, true otherwise
1188*/
1189bool geometry_to_json(Json_wrapper *wr, String *swkb,
1190 const char *calling_function, int max_decimal_digits,
1191 bool add_bounding_box, bool add_short_crs_urn,
1192 bool add_long_crs_urn, uint32 *geometry_srid);
1193
1194/**
1195 Convert a value represented with an Item to a JSON value
1196
1197 @param[in] item the input value, may be any data type
1198 @param[in] func_name for error reporting
1199 @param[in,out] wr the result wrapper for the JSON value
1200
1201 @return false if success, true if error
1202*/
1203bool convert_value_to_json(Item *item, const char *func_name, Json_wrapper *wr);
1204/**
1205 Convert JSON values or MySQL values to JSON. Converts SQL NULL
1206 to the JSON null literal.
1207
1208 @param[in] args arguments to function
1209 @param[in] arg_idx the index of the argument to process
1210 @param[in] calling_function name of the calling function
1211 @param[in,out] value working area (if the returned Json_wrapper points
1212 to a binary value rather than a DOM, this string
1213 will end up holding the binary representation, and
1214 it must stay alive until the wrapper is destroyed
1215 or converted from binary to DOM)
1216 @param[in,out] tmp temporary scratch space for converting strings to
1217 the correct charset; only used if accept_string is
1218 true and conversion is needed
1219 @param[in,out] wr the result wrapper
1220 @returns false if we found a value or NULL, true otherwise
1221*/
1222bool get_atom_null_as_null(Item **args, uint arg_idx,
1223 const char *calling_function, String *value,
1224 String *tmp, Json_wrapper *wr);
1225
1226/**
1227 Gets a JSON object member name from an Item. An error is raised if
1228 the Item evaluates to NULL, or if it cannot be converted to a
1229 utf8mb4 string.
1230
1231 @param[in] thd THD handle
1232 @param[in] arg_item An argument Item
1233 @param[out] value Where to materialize the arg_item's string value
1234 @param[out] utf8_res Buffer for use by ensure_utf8mb4.
1235 @param[out] safep String pointer after any relevant conversion
1236 @param[out] safe_length Corresponding string length
1237
1238 @returns true if the Item is not a utf8mb4 string
1239*/
1240bool get_json_object_member_name(const THD *thd, Item *arg_item, String *value,
1241 String *utf8_res, const char **safep,
1242 size_t *safe_length);
1243using Json_dom_ptr = std::unique_ptr<Json_dom>;
1244
1245bool parse_json(const String &res, Json_dom_ptr *dom, bool require_str_or_json,
1246 const JsonParseErrorHandler &error_handler,
1247 const JsonDocumentDepthHandler &depth_handler);
1248
1251
1252bool save_json_to_field(THD *thd, Field *field, const Json_wrapper *w,
1253 bool no_error);
1254#endif /* ITEM_JSON_FUNC_INCLUDED */
A field that stores a JSON value.
Definition: field.h:3986
Definition: field.h:575
Definition: item_cmpfunc.h:295
Class that represents CAST(<expr> AS <type> ARRAY)
Definition: item_json_func.h:978
unique_ptr_destroy_only< Json_array > m_result_array
An array used by save_in_field_inner() to store the result of an array cast operation.
Definition: item_json_func.h:995
bool get_time(MYSQL_TIME *) override
Definition: item_json_func.h:1037
void allow_array_cast() override
A helper function to ensure proper usage of CAST(.
Definition: item_json_func.h:1012
const char * func_name() const override
Definition: item_json_func.h:1001
longlong val_int() override
Definition: item_json_func.h:1017
bool returns_array() const override
Whether the item returns array of its data type.
Definition: item_json_func.h:1003
bool get_date(MYSQL_TIME *, my_time_flags_t) override
Definition: item_json_func.h:1033
my_decimal * val_decimal(my_decimal *) override
Definition: item_json_func.h:1025
enum Functype functype() const override
Definition: item_json_func.h:1002
double val_real() override
Definition: item_json_func.h:1029
~Item_func_array_cast() override
Cast_target cast_type
Type to cast to.
Definition: item_json_func.h:980
String * val_str(String *) override
Definition: item_json_func.h:1021
Represents the JSON function JSON_ARRAY_APPEND()
Definition: item_json_func.h:621
Item_func_json_array_append(THD *thd, const POS &pos, PT_item_list *a)
Definition: item_json_func.h:623
const char * func_name() const override
Definition: item_json_func.h:626
Represents the JSON function JSON_ARRAY_INSERT()
Definition: item_json_func.h:647
Item_func_json_array_insert(THD *thd, const POS &pos, PT_item_list *a)
Definition: item_json_func.h:649
const char * func_name() const override
Definition: item_json_func.h:652
Represents the JSON function JSON_ARRAY()
Definition: item_json_func.h:704
const char * func_name() const override
Definition: item_json_func.h:714
Item_func_json_array(Args &&... parent_args)
Definition: item_json_func.h:707
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:716
Represents the JSON function JSON_CONTAINS_PATH()
Definition: item_json_func.h:426
void cleanup() override
Cleanup between executions of the statement.
Definition: item_json_func.cc:987
bool is_bool_func() const override
Definition: item_json_func.h:441
longlong val_int() override
Definition: item_json_func.cc:994
Json_path_cache m_path_cache
Definition: item_json_func.h:431
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:445
String m_doc_value
Definition: item_json_func.h:427
Item_func_json_contains_path(THD *thd, const POS &pos, PT_item_list *a)
Definition: item_json_func.h:434
const char * func_name() const override
Definition: item_json_func.h:439
enum_const_item_cache can_cache_json_arg(Item *arg) override
Whether an arg of a JSON function can be cached to avoid repetitive string->JSON conversion.
Definition: item_json_func.h:455
enum_one_or_all_type m_cached_ooa
Definition: item_json_func.h:428
Represents the JSON function JSON_CONTAINS()
Definition: item_json_func.h:391
bool gc_subst_analyzer(uchar **) override
Analyzer function for GC substitution.
Definition: item_json_func.h:402
optimize_type select_optimize(const THD *) override
Definition: item_json_func.h:401
Item_func_json_contains(THD *thd, const POS &pos, PT_item_list *a)
Definition: item_json_func.h:396
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:408
longlong val_int() override
Definition: item_json_func.cc:925
String m_doc_value
Definition: item_json_func.h:392
enum Functype functype() const override
Definition: item_json_func.h:400
Json_path_cache m_path_cache
Definition: item_json_func.h:393
enum_const_item_cache can_cache_json_arg(Item *arg) override
Whether an arg of a JSON function can be cached to avoid repetitive string->JSON conversion.
Definition: item_json_func.h:418
void cleanup() override
Cleanup between executions of the statement.
Definition: item_json_func.cc:919
const char * func_name() const override
Definition: item_json_func.h:399
bool is_bool_func() const override
Definition: item_json_func.h:404
Represents the JSON function JSON_DEPTH()
Definition: item_json_func.h:522
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:530
longlong val_int() override
Definition: item_json_func.cc:1741
String m_doc_value
Definition: item_json_func.h:523
const char * func_name() const override
Definition: item_json_func.h:528
Item_func_json_depth(const POS &pos, Item *a)
Definition: item_json_func.h:526
Represents the JSON function JSON_EXTRACT()
Definition: item_json_func.h:567
const char * func_name() const override
Definition: item_json_func.h:577
Item_func_json_extract(THD *thd, const POS &pos, PT_item_list *a)
Definition: item_json_func.h:571
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:579
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_json_func.cc:1823
Item_func_json_extract(THD *thd, const POS &pos, Item *a, Item *b)
Definition: item_json_func.h:574
String m_doc_value
Definition: item_json_func.h:568
bool eq(const Item *item, bool binary_cmp) const override
Definition: item_json_func.cc:1890
Represents the JSON function JSON_INSERT()
Definition: item_json_func.h:634
Item_func_json_insert(THD *thd, const POS &pos, PT_item_list *a)
Definition: item_json_func.h:636
const char * func_name() const override
Definition: item_json_func.h:639
Represents the JSON function JSON_KEYS()
Definition: item_json_func.h:542
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_json_func.cc:1762
String m_doc_value
Definition: item_json_func.h:543
Item_func_json_keys(THD *thd, const POS &pos, Item *a, Item *b)
Definition: item_json_func.h:549
Item_func_json_keys(THD *thd, const POS &pos, Item *a)
Definition: item_json_func.h:546
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:554
const char * func_name() const override
Definition: item_json_func.h:552
Represents the JSON function JSON_LENGTH()
Definition: item_json_func.h:501
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:507
Item_func_json_length(const POS &pos, Item *doc)
Definition: item_json_func.h:505
longlong val_int() override
Definition: item_json_func.cc:1716
String m_doc_value
Definition: item_json_func.h:502
const char * func_name() const override
Definition: item_json_func.h:514
Represents the JSON function JSON_MERGE_PATCH.
Definition: item_json_func.h:849
Item_func_json_merge_patch(THD *thd, const POS &pos, PT_item_list *a)
Definition: item_json_func.h:851
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:856
const char * func_name() const override
Definition: item_json_func.h:854
Represents the JSON function JSON_MERGE_PRESERVE.
Definition: item_json_func.h:819
const char * func_name() const override
Definition: item_json_func.h:824
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:826
Item_func_json_merge_preserve(THD *thd, const POS &pos, PT_item_list *a)
Definition: item_json_func.h:821
Represents the JSON function JSON_MERGE.
Definition: item_json_func.h:839
bool is_deprecated() const override
Definition: item_json_func.h:843
Definition: item_json_func.h:1044
bool gc_subst_analyzer(uchar **) override
Analyzer function for GC substitution.
Definition: item_json_func.h:1050
enum Functype functype() const override
Definition: item_json_func.h:1049
const char * func_name() const override
Definition: item_json_func.h:1048
Item_func_json_overlaps(const POS &pos, Item *a, Item *b)
Definition: item_json_func.h:1046
optimize_type select_optimize(const THD *) override
Definition: item_json_func.h:1051
enum_const_item_cache can_cache_json_arg(Item *arg) override
Whether an arg of a JSON function can be cached to avoid repetitive string->JSON conversion.
Definition: item_json_func.h:1054
Represents the JSON_PRETTY function.
Definition: item_json_func.h:923
const char * func_name() const override
Definition: item_json_func.h:927
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:929
Item_func_json_pretty(const POS &pos, Item *a)
Definition: item_json_func.h:925
Represents the JSON function JSON_QUOTE()
Definition: item_json_func.h:868
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:877
String m_value
Definition: item_json_func.h:869
Item_func_json_quote(const POS &pos, PT_item_list *a)
Definition: item_json_func.h:872
const char * func_name() const override
Definition: item_json_func.h:875
Represents the JSON function JSON_REMOVE()
Definition: item_json_func.h:795
const char * func_name() const override
Definition: item_json_func.h:804
bool can_use_in_partial_update() const override
Can this function type be used in partial update?
Definition: item_json_func.h:797
String m_doc_value
Definition: item_json_func.h:796
Item_func_json_remove(Args &&... parent_args)
Definition: item_json_func.h:801
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:806
Represents the JSON function JSON_REPLACE()
Definition: item_json_func.h:692
const char * func_name() const override
Definition: item_json_func.h:698
Item_func_json_replace(Args &&... parent_args)
Definition: item_json_func.h:695
Represents the JSON function JSON_OBJECT()
Definition: item_json_func.h:728
String tmp_key_value
Definition: item_json_func.h:729
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:742
Item_func_json_row_object(THD *thd, const POS &pos, PT_item_list *a)
Definition: item_json_func.h:732
const char * func_name() const override
Definition: item_json_func.h:740
Represents the JSON function JSON_SCHEMA_VALID( <json schema>, <json doc> )
Definition: item_json_func.h:333
unique_ptr_destroy_only< const Json_schema_validator > m_cached_schema_validator
Definition: item_json_func.h:352
Item_func_json_schema_valid(const POS &pos, Item *a, Item *b)
Definition: item_json_func.cc:588
bool val_bool() override
Definition: item_json_func.cc:648
bool fix_fields(THD *, Item **) override
Definition: item_json_func.cc:575
longlong val_int() override
Definition: item_json_func.h:342
const char * func_name() const override
Definition: item_json_func.h:338
~Item_func_json_schema_valid() override
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_json_func.cc:586
Represents the JSON function JSON_SCHEMA_VALIDATION_REPORT( <json schema>, <json doc> )
Definition: item_json_func.h:359
const char * func_name() const override
Definition: item_json_func.h:365
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:371
unique_ptr_destroy_only< const Json_schema_validator > m_cached_schema_validator
Definition: item_json_func.h:385
void cleanup() override
JSON_*() support methods.
Definition: item_json_func.cc:687
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_json_func.cc:699
bool fix_fields(THD *, Item **) override
Definition: item_json_func.cc:676
Item_func_json_schema_validation_report(THD *thd, const POS &pos, PT_item_list *a)
Definition: item_json_func.cc:692
Represents the JSON function JSON_SEARCH()
Definition: item_json_func.h:754
String m_doc_value
Definition: item_json_func.h:755
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:782
enum_one_or_all_type m_cached_ooa
Definition: item_json_func.h:756
Item_func_like * m_like_node
Definition: item_json_func.h:760
Item_func_json_search(Args &&... parent_args)
Construct a JSON_SEARCH() node.
Definition: item_json_func.h:769
const char * func_name() const override
Definition: item_json_func.h:773
Item_string * m_source_string_item
Definition: item_json_func.h:759
Common base class for JSON_SET() and JSON_REPLACE().
Definition: item_json_func.h:660
Item_func_json_set_replace(bool json_set, Args &&... parent_args)
Definition: item_json_func.h:668
const bool m_json_set
True if this is JSON_SET, false if it is JSON_REPLACE.
Definition: item_json_func.h:662
Json_path_clone m_path
Definition: item_json_func.h:663
bool can_use_in_partial_update() const override
Can this function type be used in partial update?
Definition: item_json_func.h:664
Represents the JSON function JSON_SET()
Definition: item_json_func.h:680
Item_func_json_set(Args &&... parent_args)
Definition: item_json_func.h:683
const char * func_name() const override
Definition: item_json_func.h:686
Class that represents the function JSON_STORAGE_FREE.
Definition: item_json_func.h:960
Item_func_json_storage_free(const POS &pos, Item *a)
Definition: item_json_func.h:962
const char * func_name() const override
Definition: item_json_func.h:964
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:966
Class that represents the function JSON_STORAGE_SIZE.
Definition: item_json_func.h:941
Item_func_json_storage_size(const POS &pos, Item *a)
Definition: item_json_func.h:943
const char * func_name() const override
Definition: item_json_func.h:945
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:947
Represents the JSON function JSON_TYPE.
Definition: item_json_func.h:463
const char * func_name() const override
Definition: item_json_func.h:469
String * val_str(String *) override
Definition: item_json_func.cc:1237
String m_value
Definition: item_json_func.h:464
Item_func_json_type(const POS &pos, Item *a)
Definition: item_json_func.h:467
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.cc:1186
Represents the JSON function JSON_UNQUOTE()
Definition: item_json_func.h:896
String m_value
Definition: item_json_func.h:897
const char * func_name() const override
Definition: item_json_func.h:906
Item_func_json_unquote(const POS &pos, Item *a)
Definition: item_json_func.h:904
Item_func_json_unquote(const POS &pos, PT_item_list *a)
Definition: item_json_func.h:901
enum Functype functype() const override
Definition: item_json_func.h:908
String m_conversion_buffer
Definition: item_json_func.h:898
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:910
Represents the JSON function JSON_VALID( <value> )
Definition: item_json_func.h:311
const char * func_name() const override
Definition: item_json_func.h:317
Item_func_json_valid(const POS &pos, Item *a)
Definition: item_json_func.h:315
longlong val_int() override
Definition: item_json_func.cc:527
bool is_bool_func() const override
Definition: item_json_func.h:319
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:323
String m_value
Definition: item_json_func.h:312
Class implementing the JSON_VALUE function.
Definition: item_json_func.h:1089
unique_ptr_destroy_only< Default_value > m_default_error
The default value for ON EMPTY (if not ERROR or NULL ON EMPTY).
Definition: item_json_func.h:1127
~Item_func_json_value() override
Json_on_response_type m_on_error
Type of the ON ERROR clause.
Definition: item_json_func.h:1123
Cast_target m_cast_target
The target data type.
Definition: item_json_func.h:1129
unique_ptr_destroy_only< Default_value > m_default_empty
The default value for ON EMPTY (if not ERROR or NULL ON EMPTY).
Definition: item_json_func.h:1125
const char * func_name() const override
Definition: item_json_func.h:1098
Json_on_response_type m_on_empty
Type of the ON EMPTY clause.
Definition: item_json_func.h:1121
Json_path m_path_json
Parsed path.
Definition: item_json_func.h:1116
Definition: item_cmpfunc.h:2347
Definition: item_json_func.h:1059
enum_const_item_cache can_cache_json_arg(Item *arg) override
Whether an arg of a JSON function can be cached to avoid repetitive string->JSON conversion.
Definition: item_json_func.h:1076
Item_func_member_of(const POS &pos, Item *a, Item *b)
Definition: item_json_func.h:1061
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:1065
enum Functype functype() const override
Definition: item_json_func.h:1064
bool gc_subst_analyzer(uchar **) override
Analyzer function for GC substitution.
Definition: item_json_func.h:1070
Item * key_item() const override
Definition: item_json_func.h:1075
optimize_type select_optimize(const THD *) override
Definition: item_json_func.h:1071
const char * func_name() const override
Definition: item_json_func.h:1063
Base class for all the functions that take a JSON document as the first argument and one of more pair...
Definition: item_json_func.h:594
void update_used_tables() final
Updates used tables, not null tables information and accumulates properties up the item tree,...
Definition: item_json_func.cc:1926
String m_doc_value
Definition: item_json_func.h:606
table_map calculate_not_null_tables() const
Calculates the set of tables to return from not_used_tables().
Definition: item_json_func.cc:1931
Item_func_modify_json_in_path(Args &&... parent_args)
Definition: item_json_func.h:597
bool resolve_type(THD *thd) final
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.cc:1914
Definition: item_func.h:102
Item ** args
Array of pointers to arguments.
Definition: item_func.h:109
Functype
Definition: item_func.h:185
@ JSON_CONTAINS
Definition: item_func.h:301
@ JSON_UNQUOTE_FUNC
Definition: item_func.h:303
@ MEMBER_OF_FUNC
Definition: item_func.h:304
@ TYPECAST_FUNC
Definition: item_func.h:233
@ JSON_OVERLAPS
Definition: item_func.h:302
optimize_type
Definition: item_func.h:309
@ OPTIMIZE_KEY
Definition: item_func.h:311
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_func.h:633
enum Type type() const override
Definition: item_func.h:316
virtual Item * key_item() const
Definition: item_func.h:483
bool param_type_is_default(THD *thd, uint start, uint end, uint step, enum_field_types def)
For arguments of this Item_func ("args" array), in range [start, start+step, start+2*step,...
Definition: item_func.cc:519
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_func.cc:724
bool fix_fields(THD *, Item **ref) override
Definition: item_func.cc:397
uint arg_count
How many arguments in 'args'.
Definition: item_func.h:132
Field * tmp_table_field(TABLE *t_arg) override
Definition: item_func.cc:771
bool eq(const Item *item, bool binary_cmp) const override
Definition: item_func.cc:754
bool null_on_null
Affects how to determine that NULL argument implies a NULL function return.
Definition: item_func.h:164
Definition: item_func.h:935
Base class for all item functions that a return JSON value.
Definition: item_json_func.h:151
void mark_for_partial_update(const Field_json *field)
Mark this expression as used in partial update.
Definition: item_json_func.cc:2315
String m_conversion_buffer
String used for converting JSON text values to utf8mb4 charset.
Definition: item_json_func.h:159
longlong val_int() override
Definition: item_json_func.cc:1318
Item_result cast_to_int_type() const override
Definition: item_json_func.h:199
String m_value
String used when reading JSON binary values or JSON text values.
Definition: item_json_func.h:157
virtual bool can_use_in_partial_update() const
Can this function type be used in partial update?
Definition: item_json_func.h:153
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_json_func.cc:1296
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:185
bool get_time(MYSQL_TIME *ltime) override
Definition: item_json_func.cc:1307
String * val_str(String *arg) override
Definition: item_json_func.cc:1283
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_json_func.cc:1340
enum Item_result result_type() const override
Definition: item_json_func.h:189
String m_string_buffer
String used for converting a JSON value to text in val_str().
Definition: item_json_func.h:161
bool supports_partial_update(const Field_json *field) const override
Does this function call support partial update of the given JSON column?
Definition: item_json_func.cc:2332
Json_path_cache m_path_cache
Definition: item_json_func.h:164
const Field_json * m_partial_update_column
Target column for partial update, if this function is used in an update statement and partial update ...
Definition: item_json_func.h:170
double val_real() override
Definition: item_json_func.cc:1327
Item_json_func(THD *thd, Args &&... parent_args)
Construct an Item_json_func instance.
Definition: item_json_func.h:179
void cleanup() override
JSON_*() support methods.
Definition: item_json_func.cc:521
virtual const char * func_name() const =0
Definition: item_strfunc.h:76
Definition: item.h:5255
Represents a "CAST( <value> AS JSON )" coercion.
Definition: item_json_func.h:479
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_json_func.cc:1660
const char * cast_type() const
Definition: item_json_func.h:494
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_json_func.cc:1707
Item_json_func super
Definition: item_json_func.h:480
const char * func_name() const override
Definition: item_json_func.h:493
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_json_func.h:486
Item_typecast_json(THD *thd, const POS &pos, Item *a)
Definition: item_json_func.h:483
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:853
uint32 max_char_length() const
Definition: item.h:3175
void set_nullable(bool nullable)
Definition: item.h:3470
virtual bool propagate_type(THD *thd, const Type_properties &type)
Propagate data type specifications into parameters and user variables.
Definition: item.h:1209
virtual type_conversion_status save_in_field_inner(Field *field, bool no_conversions)
Helper function which does all of the work for save_in_field(Field*, bool), except some error checkin...
Definition: item.cc:6678
virtual bool val_json(Json_wrapper *result)
Get a JSON value from an Item.
Definition: item.h:1929
enum_const_item_cache
How to cache constant JSON data.
Definition: item.h:927
@ CACHE_NONE
Don't cache.
Definition: item.h:929
@ CACHE_JSON_VALUE
Source data is a JSON string, parse and cache result.
Definition: item.h:931
@ CACHE_JSON_ATOM
Source data is SQL scalar, convert and cache result.
Definition: item.h:933
void set_data_type_json()
Set the data type of the Item to be JSON.
Definition: item.h:1603
void set_data_type_string(uint32 max_l)
Set the Item to be variable length string.
Definition: item.h:1459
virtual void mark_json_as_scalar()
For Items with data type JSON, mark that a string argument is treated as a scalar JSON value.
Definition: item.h:1251
Represents a JSON array container, i.e.
Definition: json_dom.h:520
JSON DOM abstract base class.
Definition: json_dom.h:173
Path cache for JSON functions.
Definition: item_json_func.h:84
Json_path_cache(THD *thd, uint size)
Json_path_cache.
Definition: item_json_func.cc:457
const Json_path * get_path(uint arg_idx) const
Return an already parsed path expression.
Definition: item_json_func.cc:502
void reset_cache()
Reset the cache for re-use when a statement is re-executed.
Definition: item_json_func.cc:512
enum_path_status
Enum that tells the status of a cell in m_paths.
Definition: item_json_func.h:93
Prealloced_array< Json_path, 8 > m_paths
List of paths.
Definition: item_json_func.h:90
Mem_root_array< Path_cell > m_arg_idx_to_vector_idx
Map argument indexes to indexes into m_paths.
Definition: item_json_func.h:106
String m_path_value
Holder for path strings.
Definition: item_json_func.h:87
bool parse_and_cache_path(const THD *thd, Item **args, uint arg_idx, bool forbid_wildcards)
Parse a path expression if necessary.
Definition: item_json_func.cc:464
A lightweight path expression.
Definition: json_path.h:451
A JSON path expression.
Definition: json_path.h:357
A class that is capable of holding objects of any sub-type of Json_scalar.
Definition: json_dom.h:1893
Json_schema_validator is an object that contains a JSON Schema that can be re-used multiple times.
Definition: json_schema.h:127
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1161
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:426
Wrapper class for an Item list head, used to allocate Item lists in the parser in a context-independe...
Definition: parse_tree_helpers.h:105
A typesafe replacement for DYNAMIC_ARRAY.
Definition: prealloced_array.h:71
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:168
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:94
enum_query_type
Query type constants (usable as bitmap flags).
Definition: enum_query_type.h:31
@ MYSQL_TYPE_JSON
Definition: field_types.h:77
Cast_target
Definition: item_create.h:56
static int flags[50]
Definition: hp_test1.cc:40
bool json_value(Item *arg, Json_wrapper *result, bool *has_value)
Return the JSON value of the argument in a wrapper.
Definition: item_json_func.cc:1066
bool geometry_to_json(Json_wrapper *wr, String *swkb, const char *calling_function, int max_decimal_digits, bool add_bounding_box, bool add_short_crs_urn, bool add_long_crs_urn, uint32 *geometry_srid)
Turn a GEOMETRY value into a JSON value per the GeoJSON specification revision 1.0.
Definition: item_geofunc.cc:2326
bool parse_json(const String &res, Json_dom_ptr *dom, bool require_str_or_json, const JsonParseErrorHandler &error_handler, const JsonDocumentDepthHandler &depth_handler)
Parse a JSON dom out of an argument to a JSON function.
Definition: item_json_func.cc:132
bool sort_and_remove_dups(const Json_wrapper &orig, Sorted_index_array *v)
Sort the elements of a JSON array and remove duplicates.
Definition: item_json_func.cc:753
Prealloced_array< size_t, 16 > Sorted_index_array
Definition: item_json_func.h:1249
bool ensure_utf8mb4(const String &val, String *buf, const char **resptr, size_t *reslength, bool require_string)
Check a non-empty val for character set.
Definition: item_json_func.cc:81
bool save_json_to_field(THD *thd, Field *field, const Json_wrapper *w, bool no_error)
Save JSON to a given field.
Definition: item_json_func.cc:4051
bool get_json_atom_wrapper(Item **args, uint arg_idx, const char *calling_function, String *value, String *tmp, Json_wrapper *wr, Json_scalar_holder *scalar, bool accept_string)
Convert Json values or MySQL values to JSON.
Definition: item_json_func.cc:1589
bool get_json_wrapper(Item **args, uint arg_idx, String *str, const char *func_name, Json_wrapper *wrapper)
Return the JSON value of the argument in a wrapper.
Definition: item_json_func.cc:1086
bool get_json_object_member_name(const THD *thd, Item *arg_item, String *value, String *utf8_res, const char **safep, size_t *safe_length)
Gets a JSON object member name from an Item.
Definition: item_json_func.cc:219
bool sql_scalar_to_json(Item *arg, const char *calling_function, String *value, String *tmp, Json_wrapper *wr, Json_scalar_holder *scalar, bool scalar_string)
Get a JSON value from an SQL scalar value.
Definition: item_json_func.cc:1395
bool get_atom_null_as_null(Item **args, uint arg_idx, const char *calling_function, String *value, String *tmp, Json_wrapper *wr)
Convert JSON values or MySQL values to JSON.
Definition: item_json_func.cc:1646
bool convert_value_to_json(Item *item, const char *func_name, Json_wrapper *wr)
Convert a value represented with an Item to a JSON value.
enum_one_or_all_type
For use by JSON_CONTAINS_PATH() and JSON_SEARCH()
Definition: item_json_func.h:69
@ ooa_null
Definition: item_json_func.h:72
@ ooa_one
Definition: item_json_func.h:70
@ ooa_all
Definition: item_json_func.h:71
@ ooa_uninitialized
Definition: item_json_func.h:74
@ ooa_error
Definition: item_json_func.h:73
std::unique_ptr< Json_dom > Json_dom_ptr
Definition: json_dom.h:65
std::function< void()> JsonDocumentDepthHandler
Definition: json_error_handler.h:32
std::function< void(const char *parse_err, size_t err_offset)> JsonParseErrorHandler
Definition: json_error_handler.h:31
This file contains interface support for the JSON path abstraction.
A better implementation of the UNIX ctype(3) library.
CHARSET_INFO my_charset_utf8mb4_bin
Definition: ctype-utf8.cc:7794
std::unique_ptr< T, Destroy_only< T > > unique_ptr_destroy_only
std::unique_ptr, but only destroying.
Definition: my_alloc.h:489
Some integer typedefs for easier portability.
uint8_t uint8
Definition: my_inttypes.h:63
unsigned char uchar
Definition: my_inttypes.h:52
long long int longlong
Definition: my_inttypes.h:55
uint16_t uint16
Definition: my_inttypes.h:65
uint32_t uint32
Definition: my_inttypes.h:67
uint64_t table_map
Definition: my_table_map.h:30
Interface for low level time utilities.
unsigned int my_time_flags_t
Flags to str_to_datetime and number_to_datetime.
Definition: my_time.h:94
Common definition between mysql server & client.
#define MAX_BLOB_WIDTH
Default width for blob in bytes.
Definition: mysql_com.h:905
Time declarations shared between the server and client API: you should not add anything to this heade...
static char * path
Definition: mysqldump.cc:137
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1052
Definition: buf0block_hint.cc:30
PT & ref(PT *tp)
Definition: tablespace_impl.cc:359
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:420
Definition: gcs_xcom_synode.h:64
PSI_memory_key key_memory_JSON
Definition: psi_memory_key.cc:53
type_conversion_status
Status when storing a value in a field or converting from one datatype to another.
Definition: field.h:202
Our own string classes, used pervasively throughout the executor.
Definition: m_ctype.h:385
Definition: parser_yystype.h:181
Definition: item_json_func.cc:4170
Struct that points to a cell in m_paths and tells its status.
Definition: item_json_func.h:100
size_t m_index
Definition: item_json_func.h:102
enum_path_status m_status
Definition: item_json_func.h:101
Definition: mysql_time.h:82
Definition: table.h:1399
Bison "location" class.
Definition: parse_location.h:43
Definition: result.h:30
Json_on_response_type
Types of ON EMPTY/ON ERROR clauses for JSON_TABLE and JSON_VALUE.
Definition: table_function.h:192
unsigned int uint
Definition: uca9-dump.cc:75
Item_result
Type of the user defined function return slot and arguments.
Definition: udf_registration_types.h:39
@ STRING_RESULT
not valid for UDFs
Definition: udf_registration_types.h:41
@ INT_RESULT
double
Definition: udf_registration_types.h:43