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