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