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