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