MySQL  8.0.27
Source Code Documentation
item_func.h
Go to the documentation of this file.
1 #ifndef ITEM_FUNC_INCLUDED
2 #define ITEM_FUNC_INCLUDED
3 
4 /* Copyright (c) 2000, 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 <sys/types.h>
27 
28 #include <climits>
29 #include <cmath> // isfinite
30 #include <cstddef>
31 #include <functional>
32 
33 #include "decimal.h"
34 #include "field_types.h"
35 #include "ft_global.h"
36 #include "lex_string.h"
37 #include "m_ctype.h"
38 #include "my_alloc.h"
39 #include "my_base.h"
40 #include "my_compiler.h"
41 #include "my_dbug.h"
42 #include "my_inttypes.h"
43 #include "my_pointer_arithmetic.h"
44 #include "my_table_map.h"
45 #include "my_thread_local.h"
46 #include "my_time.h"
49 #include "mysql_com.h"
50 #include "mysql_time.h"
51 #include "mysqld_error.h"
52 #include "sql/enum_query_type.h"
53 #include "sql/field.h"
54 #include "sql/handler.h"
55 #include "sql/item.h" // Item_result_field
56 #include "sql/my_decimal.h" // str2my_decimal
57 #include "sql/parse_location.h" // POS
58 #include "sql/set_var.h" // enum_var_type
59 #include "sql/sql_const.h"
60 #include "sql/sql_udf.h" // udf_handler
61 #include "sql/table.h"
62 #include "sql/thr_malloc.h"
63 #include "sql_string.h"
64 #include "template_utils.h"
65 
66 class Json_wrapper;
67 class PT_item_list;
68 class Protocol;
69 class Query_block;
70 class THD;
71 class sp_rcontext;
72 struct MY_BITMAP;
73 struct Parse_context;
74 
75 template <class T>
76 class List;
77 
78 /* Function items used by mysql */
79 
80 extern bool reject_geometry_args(uint arg_count, Item **args,
81  Item_result_field *me);
82 void unsupported_json_comparison(size_t arg_count, Item **args,
83  const char *msg);
84 
85 void report_conversion_error(const CHARSET_INFO *to_cs, const char *from,
86  size_t from_length, const CHARSET_INFO *from_cs);
87 
88 bool simplify_string_args(THD *thd, const DTCollation &c, Item **items,
89  uint nitems);
90 
91 String *eval_string_arg(const CHARSET_INFO *to_cs, Item *arg, String *buffer);
92 
93 class Item_func : public Item_result_field {
94  protected:
95  /**
96  Array of pointers to arguments. If there are max 2 arguments, this array
97  is often just m_embedded_arguments; otherwise it's explicitly allocated in
98  the constructor.
99  */
101 
102  private:
104 
105  /// Allocates space for the given number of arguments, if needed. Uses
106  /// #m_embedded_arguments if it's big enough.
107  bool alloc_args(MEM_ROOT *mem_root, unsigned num_args) {
108  if (num_args <= array_elements(m_embedded_arguments)) {
110  } else {
111  args = mem_root->ArrayAlloc<Item *>(num_args);
112  if (args == nullptr) {
113  // OOM
114  arg_count = 0;
115  return true;
116  }
117  }
118  arg_count = num_args;
119  return false;
120  }
121 
122  public:
123  uint arg_count; ///< How many arguments in 'args'
124  /// Changes argument and maintains any necessary invariants.
125  virtual void set_arg_resolve(THD *, uint i, Item *arg) { args[i] = arg; }
126  virtual uint argument_count() const { return arg_count; }
127  inline Item **arguments() const {
128  return (argument_count() > 0) ? args : nullptr;
129  }
130 
131  protected:
132  /*
133  These decide of types of arguments which are prepared-statement
134  parameters.
135  */
136  bool param_type_uses_non_param(THD *thd,
138  bool param_type_is_default(THD *thd, uint start, uint end, uint step,
139  enum_field_types def);
142  return param_type_is_default(thd, start, end, 1, def);
143  }
145 
146  /**
147  Affects how to determine that NULL argument implies a NULL function return.
148  Default behaviour in this class is:
149  - if true, any NULL argument means the function returns NULL.
150  - if false, no such assumption is made and not_null_tables_cache is thus
151  set to 0.
152  null_on_null is true for all Item_func derived classes, except Item_func_sp,
153  all CASE derived functions and a few other functions.
154  RETURNS NULL ON NULL INPUT can be implemented for stored functions by
155  modifying this member in class Item_func_sp.
156  */
157  bool null_on_null{true};
158  /*
159  Allowed numbers of columns in result (usually 1, which means scalar value)
160  0 means get this number from first argument
161  */
163  /// Value used in calculation of result of used_tables()
165  /// Value used in calculation of result of not_null_tables()
167 
168  public:
169  /*
170  When updating Functype with new spatial functions,
171  is_spatial_operator() should also be updated.
172 
173  DD_INTERNAL_FUNC:
174  Some of the internal functions introduced for the INFORMATION_SCHEMA views
175  opens data-dictionary tables. DD_INTERNAL_FUNC is used for the such type
176  of functions.
177  */
178  enum Functype {
295  TRUE_FUNC
296  };
303  };
304  enum Type type() const override { return FUNC_ITEM; }
305  virtual enum Functype functype() const { return UNKNOWN_FUNC; }
307 
308  explicit Item_func(const POS &pos)
310 
312  args[0] = a;
314  }
315  Item_func(const POS &pos, Item *a)
317  args[0] = a;
318  }
319 
321  args[0] = a;
322  args[1] = b;
323  m_accum_properties = 0;
326  }
327  Item_func(const POS &pos, Item *a, Item *b)
329  args[0] = a;
330  args[1] = b;
331  }
332 
333  Item_func(Item *a, Item *b, Item *c) {
334  if (alloc_args(*THR_MALLOC, 3)) return;
335  args[0] = a;
336  args[1] = b;
337  args[2] = c;
338  m_accum_properties = 0;
342  }
343 
344  Item_func(const POS &pos, Item *a, Item *b, Item *c)
346  if (alloc_args(*THR_MALLOC, 3)) return;
347  args[0] = a;
348  args[1] = b;
349  args[2] = c;
350  }
351 
352  Item_func(Item *a, Item *b, Item *c, Item *d) {
353  if (alloc_args(*THR_MALLOC, 4)) return;
354  args[0] = a;
355  args[1] = b;
356  args[2] = c;
357  args[3] = d;
358  m_accum_properties = 0;
363  }
364 
365  Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
367  if (alloc_args(*THR_MALLOC, 4)) return;
368  args[0] = a;
369  args[1] = b;
370  args[2] = c;
371  args[3] = d;
372  }
373  Item_func(Item *a, Item *b, Item *c, Item *d, Item *e) {
374  if (alloc_args(*THR_MALLOC, 5)) return;
375  args[0] = a;
376  args[1] = b;
377  args[2] = c;
378  args[3] = d;
379  args[4] = e;
380  m_accum_properties = 0;
386  }
387  Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e)
389  if (alloc_args(*THR_MALLOC, 5)) return;
390  args[0] = a;
391  args[1] = b;
392  args[2] = c;
393  args[3] = d;
394  args[4] = e;
395  }
397  set_arguments(list, false);
398  }
399 
400  Item_func(const POS &pos, PT_item_list *opt_list);
401 
402  // Constructor used for Item_cond_and/or (see Item comment)
403  Item_func(THD *thd, const Item_func *item);
404 
405  virtual Item *get_arg(uint i) { return args[i]; }
406  virtual Item *set_arg(THD *, uint, Item *) {
407  assert(0);
408  return nullptr;
409  }
410 
411  bool itemize(Parse_context *pc, Item **res) override;
412 
413  bool fix_fields(THD *, Item **ref) override;
414  bool fix_func_arg(THD *, Item **arg);
415  void fix_after_pullout(Query_block *parent_query_block,
416  Query_block *removed_query_block) override;
417  /**
418  Resolve type of function after all arguments have had their data types
419  resolved. Called from resolve_type() when no dynamic parameters
420  are used and from propagate_type() otherwise.
421  */
422  virtual bool resolve_type_inner(THD *) {
423  assert(false);
424  return false;
425  }
426  bool propagate_type(THD *thd, const Type_properties &type) override;
427  /**
428  Returns the pseudo tables depended upon in order to evaluate this
429  function expression. The default implementation returns the empty
430  set.
431  */
432  virtual table_map get_initial_pseudo_tables() const { return 0; }
433  table_map used_tables() const override { return used_tables_cache; }
434  table_map not_null_tables() const override { return not_null_tables_cache; }
435  void update_used_tables() override;
437  bool eq(const Item *item, bool binary_cmp) const override;
438  virtual optimize_type select_optimize(const THD *) { return OPTIMIZE_NONE; }
439  virtual bool have_rev_func() const { return false; }
440  virtual Item *key_item() const { return args[0]; }
441  /**
442  Copy arguments from list to args array
443 
444  @param list function argument list
445  @param context_free true: for use in context-independent
446  constructors (Item_func(POS,...)) i.e. for use
447  in the parser
448  @return true on OOM, false otherwise
449  */
450  bool set_arguments(mem_root_deque<Item *> *list, bool context_free);
451  void split_sum_func(THD *thd, Ref_item_array ref_item_array,
452  mem_root_deque<Item *> *fields) override;
453  void print(const THD *thd, String *str,
454  enum_query_type query_type) const override;
455  void print_op(const THD *thd, String *str, enum_query_type query_type) const;
456  void print_args(const THD *thd, String *str, uint from,
457  enum_query_type query_type) const;
458  virtual void fix_num_length_and_dec();
459  virtual bool is_deprecated() const { return false; }
460  bool get_arg0_date(MYSQL_TIME *ltime, my_time_flags_t fuzzy_date) {
461  return (null_value = args[0]->get_date(ltime, fuzzy_date));
462  }
463  inline bool get_arg0_time(MYSQL_TIME *ltime) {
464  return (null_value = args[0]->get_time(ltime));
465  }
466  bool is_null() override { return update_null_value() || null_value; }
467  void signal_divide_by_null();
469  friend class udf_handler;
470  Field *tmp_table_field(TABLE *t_arg) override;
471  Item *get_tmp_table_item(THD *thd) override;
472 
473  my_decimal *val_decimal(my_decimal *) override;
474 
475  bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems, uint flags,
476  int item_sep) {
477  return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep,
478  false);
479  }
480  /*
481  Aggregate arguments for string result, e.g: CONCAT(a,b)
482  - convert to @@character_set_connection if all arguments are numbers
483  - allow DERIVATION_NONE
484  */
486  uint nitems, int item_sep = 1) {
487  return agg_item_charsets_for_string_result(c, func_name(), items, nitems,
488  item_sep);
489  }
490  /*
491  Aggregate arguments for comparison, e.g: a=b, a LIKE b, a RLIKE b
492  - don't convert to @@character_set_connection if all arguments are numbers
493  - don't allow DERIVATION_NONE
494  */
496  uint nitems, int item_sep = 1) {
497  return agg_item_charsets_for_comparison(c, func_name(), items, nitems,
498  item_sep);
499  }
500 
501  bool walk(Item_processor processor, enum_walk walk, uchar *arg) override;
502  Item *transform(Item_transformer transformer, uchar *arg) override;
503  Item *compile(Item_analyzer analyzer, uchar **arg_p,
504  Item_transformer transformer, uchar *arg_t) override;
505  void traverse_cond(Cond_traverser traverser, void *arg,
506  traverse_order order) override;
507 
508  /**
509  Throw an error if the input double number is not finite, i.e. is either
510  +/-INF or NAN.
511  */
512  inline double check_float_overflow(double value) {
513  return std::isfinite(value) ? value : raise_float_overflow();
514  }
515  /**
516  Throw an error if the input BIGINT value represented by the
517  (longlong value, bool unsigned flag) pair cannot be returned by the
518  function, i.e. is not compatible with this Item's unsigned_flag.
519  */
520  inline longlong check_integer_overflow(longlong value, bool val_unsigned) {
521  if ((unsigned_flag && !val_unsigned && value < 0) ||
522  (!unsigned_flag && val_unsigned &&
523  (ulonglong)value > (ulonglong)LLONG_MAX))
524  return raise_integer_overflow();
525  return value;
526  }
527  /**
528  Throw an error if the error code of a DECIMAL operation is E_DEC_OVERFLOW.
529  */
530  inline int check_decimal_overflow(int error) {
532  }
533 
535  assert(fixed);
536  for (uint i = 0; i < arg_count; i++) {
537  if (args[i]->type() == Item::FIELD_ITEM &&
539  return true;
540  }
541  return false;
542  }
543 
544  bool has_date_args() {
545  assert(fixed);
546  for (uint i = 0; i < arg_count; i++) {
547  if (args[i]->type() == Item::FIELD_ITEM &&
548  (args[i]->data_type() == MYSQL_TYPE_DATE ||
550  return true;
551  }
552  return false;
553  }
554 
555  bool has_time_args() {
556  assert(fixed);
557  for (uint i = 0; i < arg_count; i++) {
558  if (args[i]->type() == Item::FIELD_ITEM &&
559  (args[i]->data_type() == MYSQL_TYPE_TIME ||
561  return true;
562  }
563  return false;
564  }
565 
567  assert(fixed);
568  for (uint i = 0; i < arg_count; i++) {
569  if (args[i]->type() == Item::FIELD_ITEM &&
571  return true;
572  }
573  return false;
574  }
575 
576  /*
577  We assume the result of any function that has a TIMESTAMP argument to be
578  timezone-dependent, since a TIMESTAMP value in both numeric and string
579  contexts is interpreted according to the current timezone.
580  The only exception is UNIX_TIMESTAMP() which returns the internal
581  representation of a TIMESTAMP argument verbatim, and thus does not depend on
582  the timezone.
583  */
585  return has_timestamp_args();
586  }
587 
588  Item *gc_subst_transformer(uchar *arg) override;
589 
590  bool resolve_type(THD *thd) override {
591  // By default, pick PS-param's type from other arguments, or VARCHAR
592  return param_type_uses_non_param(thd);
593  }
594 
595  /**
596  Whether an arg of a JSON function can be cached to avoid repetitive
597  string->JSON conversion. This function returns true only for those args,
598  which are the source of JSON data. JSON path args are cached independently
599  and for them this function returns false. Same as for all other type of
600  args.
601 
602  @param arg the arg to cache
603 
604  @retval true arg can be cached
605  @retval false otherwise
606  */
607  virtual enum_const_item_cache can_cache_json_arg(Item *arg [[maybe_unused]]) {
608  return CACHE_NONE;
609  }
610 
611  /// Whether this Item is an equi-join condition. If this Item is a compound
612  /// item (i.e. multiple condition AND'ed together), it will only return true
613  /// if the Item contains only equi-join conditions AND'ed together. This is
614  /// used to determine whether the condition can be used as a join condition
615  /// for hash join (join conditions in hash join must be equi-join conditions),
616  /// or if it should be placed as a filter after the join.
617  virtual bool contains_only_equi_join_condition() const { return false; }
618 
620 
621  protected:
622  /**
623  Whether or not an item should contribute to the filtering effect
624  (@see get_filtering_effect()). First it verifies that table
625  requirements are satisfied as follows:
626 
627  1) The item must refer to a field in 'filter_for_table' in some
628  way. This reference may be indirect through any number of
629  intermediate items. For example, this item may be an
630  Item_cond_and which refers to an Item_func_eq which refers to
631  the field.
632  2) The item must not refer to other tables than those already
633  read and the table in 'filter_for_table'
634 
635  Then it contines to other properties as follows:
636 
637  Item_funcs represent "<operand1> OP <operand2> [OP ...]". If the
638  Item_func is to contribute to the filtering effect, then
639 
640  1) one of the operands must be a field from 'filter_for_table' that is not
641  in 'fields_to_ignore', and
642  2) depending on the Item_func type filtering effect is calculated
643  for, one or all [1] of the other operand(s) must be an available
644  value, i.e.:
645  - a constant, or
646  - a constant subquery, or
647  - a field value read from a table in 'read_tables', or
648  - a second field in 'filter_for_table', or
649  - a function that only refers to constants or tables in
650  'read_tables', or
651  - special case: an implicit value like NULL in the case of
652  "field IS NULL". Such Item_funcs have arg_count==1.
653 
654  [1] "At least one" for multiple equality (X = Y = Z = ...), "all"
655  for the rest (e.g. BETWEEN)
656 
657  @param read_tables Tables earlier in the join sequence.
658  Predicates for table 'filter_for_table' that
659  rely on values from these tables can be part of
660  the filter effect.
661  @param filter_for_table The table we are calculating filter effect for
662  @param fields_to_ignore Columns that should be ignored.
663 
664 
665  @return Item_field that participates in the predicate if none of the
666  requirements are broken, NULL otherwise
667 
668  @note: This function only applies to items doing comparison, i.e.
669  boolean predicates. Unfortunately, some of those items do not
670  inherit from Item_bool_func so the member function has to be
671  placed in Item_func.
672  */
674  table_map read_tables, table_map filter_for_table,
675  const MY_BITMAP *fields_to_ignore) const;
676  /**
677  Named parameters are allowed in a parameter list
678 
679  The syntax to name parameters in a function call is as follow:
680  <code>foo(expr AS named, expr named, expr AS "named", expr "named")</code>
681  where "AS" is optional.
682  Only UDF function support that syntax.
683 
684  @return true if the function item can have named parameters
685  */
686  virtual bool may_have_named_parameters() const { return false; }
687  bool is_non_const_over_literals(uchar *) override { return false; }
688 
689  bool check_function_as_value_generator(uchar *checker_args) override {
690  if (is_deprecated()) {
692  pointer_cast<Check_function_as_value_generator_parameters *>(
693  checker_args);
694  func_arg->banned_function_name = func_name();
695  return true;
696  }
697  return false;
698  }
699  bool check_column_from_derived_table(uchar *arg [[maybe_unused]]) override {
700  return false;
701  }
702  bool check_column_in_window_functions(uchar *arg) override;
703  bool check_column_in_group_by(uchar *arg) override;
704 
706 };
707 
708 class Item_real_func : public Item_func {
709  public:
711  explicit Item_real_func(const POS &pos) : Item_func(pos) {
713  }
714 
716  Item_real_func(const POS &pos, Item *a) : Item_func(pos, a) {
718  }
719 
721 
722  Item_real_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
724  }
725 
728  }
729 
730  Item_real_func(const POS &pos, PT_item_list *list) : Item_func(pos, list) {
732  }
733 
734  String *val_str(String *str) override;
735  my_decimal *val_decimal(my_decimal *decimal_value) override;
736  longlong val_int() override {
737  assert(fixed);
739  }
740  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
741  return get_date_from_real(ltime, fuzzydate);
742  }
743  bool get_time(MYSQL_TIME *ltime) override {
744  return get_time_from_real(ltime);
745  }
746  enum Item_result result_type() const override { return REAL_RESULT; }
747 };
748 
750  protected:
752 
753  public:
756  }
760  }
761 
763  : Item_func(a, b), hybrid_type(REAL_RESULT) {
765  }
767  : Item_func(pos, a, b), hybrid_type(REAL_RESULT) {
769  }
770 
772  : Item_func(list), hybrid_type(REAL_RESULT) {
774  }
776  : Item_func(pos, list), hybrid_type(REAL_RESULT) {
778  }
779 
780  enum Item_result result_type() const override { return hybrid_type; }
782  return MYSQL_TYPE_DOUBLE;
783  }
784  bool resolve_type(THD *thd) override;
785  bool resolve_type_inner(THD *thd) override;
786  void fix_num_length_and_dec() override;
787  virtual void set_numeric_type() = 0; // To be called from resolve_type()
788 
789  double val_real() override;
790  longlong val_int() override;
791  my_decimal *val_decimal(my_decimal *) override;
792  String *val_str(String *str) override;
793  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
794  bool get_time(MYSQL_TIME *ltime) override;
795  /**
796  @brief Performs the operation that this functions implements when the
797  result type is INT.
798 
799  @return The result of the operation.
800  */
801  virtual longlong int_op() = 0;
802 
803  /**
804  @brief Performs the operation that this functions implements when the
805  result type is REAL.
806 
807  @return The result of the operation.
808  */
809  virtual double real_op() = 0;
810 
811  /**
812  @brief Performs the operation that this functions implements when the
813  result type is DECIMAL.
814 
815  @param decimal_value A pointer where the DECIMAL value will be allocated.
816  @return
817  - 0 If the result is NULL
818  - The same pointer it was given, with the area initialized to the
819  result of the operation.
820  */
821  virtual my_decimal *decimal_op(my_decimal *decimal_value) = 0;
822 
823  /**
824  @brief Performs the operation that this functions implements when the
825  result type is a string type.
826 
827  @return The result of the operation.
828  */
829  virtual String *str_op(String *) = 0;
830  /**
831  @brief Performs the operation that this functions implements when the
832  result type is MYSQL_TYPE_DATE or MYSQL_TYPE_DATETIME.
833 
834  @return The result of the operation.
835  */
836  virtual bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) = 0;
837  virtual bool time_op(MYSQL_TIME *ltime) = 0;
838  bool is_null() override { return update_null_value() || null_value; }
839 };
840 
841 /* function where type of result detected by first argument */
843  public:
846 
848  Item_func_num1(const POS &pos, Item *a, Item *b)
849  : Item_func_numhybrid(pos, a, b) {}
850 
851  void fix_num_length_and_dec() override;
852  void set_numeric_type() override;
853  String *str_op(String *) override {
854  assert(0);
855  return nullptr;
856  }
857  bool date_op(MYSQL_TIME *, my_time_flags_t) override {
858  assert(0);
859  return false;
860  }
861  bool time_op(MYSQL_TIME *) override {
862  assert(0);
863  return false;
864  }
865 };
866 
867 /* Base class for operations like '+', '-', '*' */
869  public:
871  Item_num_op(const POS &pos, Item *a, Item *b)
872  : Item_func_numhybrid(pos, a, b) {}
873 
874  virtual void result_precision() = 0;
875 
876  void print(const THD *thd, String *str,
877  enum_query_type query_type) const override {
878  print_op(thd, str, query_type);
879  }
880 
881  void set_numeric_type() override;
882  String *str_op(String *) override {
883  assert(0);
884  return nullptr;
885  }
886  bool date_op(MYSQL_TIME *, my_time_flags_t) override {
887  assert(0);
888  return false;
889  }
890  bool time_op(MYSQL_TIME *) override {
891  assert(0);
892  return false;
893  }
894 };
895 
896 class Item_int_func : public Item_func {
897  public:
899  explicit Item_int_func(const POS &pos) : Item_func(pos) {
901  }
902 
904  Item_int_func(const POS &pos, Item *a) : Item_func(pos, a) {
906  }
907 
908  Item_int_func(Item *a, Item *b) : Item_func(a, b) {
910  }
911  Item_int_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
913  }
914 
915  Item_int_func(Item *a, Item *b, Item *c) : Item_func(a, b, c) {
917  }
918  Item_int_func(const POS &pos, Item *a, Item *b, Item *c)
919  : Item_func(pos, a, b, c) {
921  }
922 
923  Item_int_func(Item *a, Item *b, Item *c, Item *d) : Item_func(a, b, c, d) {
925  }
926  Item_int_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
927  : Item_func(pos, a, b, c, d) {
929  }
930 
933  }
934  Item_int_func(const POS &pos, PT_item_list *opt_list)
935  : Item_func(pos, opt_list) {
937  }
938 
939  Item_int_func(THD *thd, Item_int_func *item) : Item_func(thd, item) {
941  }
942  double val_real() override;
943  String *val_str(String *str) override;
944  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
945  return get_date_from_int(ltime, fuzzydate);
946  }
947  bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
948  enum Item_result result_type() const override { return INT_RESULT; }
949  /*
950  Concerning PS-param types,
951  resolve_type(THD *) is not overidden here, as experience shows that for
952  most child classes of this class, VARCHAR is the best default
953  */
954 };
955 
958 
960 
961  public:
963 
964  bool itemize(Parse_context *pc, Item **res) override;
965  const char *func_name() const override { return "connection_id"; }
966  bool resolve_type(THD *thd) override;
967  bool fix_fields(THD *thd, Item **ref) override;
968  longlong val_int() override {
969  assert(fixed);
970  return value;
971  }
972  bool check_function_as_value_generator(uchar *checker_args) override {
974  pointer_cast<Check_function_as_value_generator_parameters *>(
975  checker_args);
976  func_arg->banned_function_name = func_name();
977  return ((func_arg->source == VGS_GENERATED_COLUMN) ||
978  (func_arg->source == VGS_CHECK_CONSTRAINT));
979  }
980 };
981 
982 class Item_typecast_signed final : public Item_int_func {
983  public:
985  unsigned_flag = false;
986  }
987  const char *func_name() const override { return "cast_as_signed"; }
988  longlong val_int() override;
989  bool resolve_type(THD *thd) override;
990  void print(const THD *thd, String *str,
991  enum_query_type query_type) const override;
992  enum Functype functype() const override { return TYPECAST_FUNC; }
993 };
994 
995 class Item_typecast_unsigned final : public Item_int_func {
996  public:
998  unsigned_flag = true;
999  }
1000  const char *func_name() const override { return "cast_as_unsigned"; }
1001  longlong val_int() override;
1002  bool resolve_type(THD *thd) override;
1003  void print(const THD *thd, String *str,
1004  enum_query_type query_type) const override;
1005  enum Functype functype() const override { return TYPECAST_FUNC; }
1006 };
1007 
1008 class Item_typecast_decimal final : public Item_func {
1009  public:
1010  Item_typecast_decimal(const POS &pos, Item *a, int len, int dec)
1011  : Item_func(pos, a) {
1012  set_data_type_decimal(len, dec);
1013  }
1014  String *val_str(String *str) override;
1015  double val_real() override;
1016  longlong val_int() override;
1017  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1018  return get_date_from_decimal(ltime, fuzzydate);
1019  }
1020  bool get_time(MYSQL_TIME *ltime) override {
1021  return get_time_from_decimal(ltime);
1022  }
1024  enum Item_result result_type() const override { return DECIMAL_RESULT; }
1025  bool resolve_type(THD *thd) override {
1026  if (args[0]->propagate_type(thd, MYSQL_TYPE_NEWDECIMAL, false, true))
1027  return true;
1028  return false;
1029  }
1030  const char *func_name() const override { return "cast_as_decimal"; }
1031  enum Functype functype() const override { return TYPECAST_FUNC; }
1032  void print(const THD *thd, String *str,
1033  enum_query_type query_type) const override;
1034 };
1035 
1036 /**
1037  Class used to implement CAST to floating-point data types.
1038 */
1039 class Item_typecast_real final : public Item_func {
1040  public:
1041  Item_typecast_real(const POS &pos, Item *a, bool as_double)
1042  : Item_func(pos, a) {
1043  if (as_double)
1045  else
1047  }
1049  String *val_str(String *str) override;
1050  double val_real() override;
1051  longlong val_int() override { return val_int_from_real(); }
1052  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1053  bool get_time(MYSQL_TIME *ltime) override;
1054  my_decimal *val_decimal(my_decimal *decimal_value) override;
1055  enum Item_result result_type() const override { return REAL_RESULT; }
1056  bool resolve_type(THD *thd) override {
1057  return args[0]->propagate_type(thd, MYSQL_TYPE_DOUBLE, false, true);
1058  }
1059  const char *func_name() const override { return "cast_as_real"; }
1060  enum Functype functype() const override { return TYPECAST_FUNC; }
1061  void print(const THD *thd, String *str,
1062  enum_query_type query_type) const override;
1063 };
1064 
1066  public:
1069  : Item_num_op(pos, a, b) {}
1070 
1071  void result_precision() override;
1072  bool check_partition_func_processor(uchar *) override { return false; }
1073  bool check_function_as_value_generator(uchar *) override { return false; }
1074 };
1075 
1077  public:
1079  Item_func_plus(const POS &pos, Item *a, Item *b)
1080  : Item_func_additive_op(pos, a, b) {}
1081 
1082  const char *func_name() const override { return "+"; }
1083 
1084  // SUPPRESS_UBSAN: signed integer overflow
1085  longlong int_op() override SUPPRESS_UBSAN;
1086 
1087  double real_op() override;
1089  enum Functype functype() const override { return PLUS_FUNC; }
1090 };
1091 
1093  public:
1095  Item_func_minus(const POS &pos, Item *a, Item *b)
1096  : Item_func_additive_op(pos, a, b) {}
1097 
1098  const char *func_name() const override { return "-"; }
1099 
1100  // SUPPRESS_UBSAN: signed integer overflow
1101  longlong int_op() override SUPPRESS_UBSAN;
1102 
1103  double real_op() override;
1104  my_decimal *decimal_op(my_decimal *) override;
1105  bool resolve_type(THD *thd) override;
1106  enum Functype functype() const override { return MINUS_FUNC; }
1107 };
1108 
1109 class Item_func_mul final : public Item_num_op {
1110  public:
1111  Item_func_mul(Item *a, Item *b) : Item_num_op(a, b) {}
1112  Item_func_mul(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1113 
1114  const char *func_name() const override { return "*"; }
1115  longlong int_op() override;
1116  double real_op() override;
1117  my_decimal *decimal_op(my_decimal *) override;
1118  void result_precision() override;
1119  bool check_partition_func_processor(uchar *) override { return false; }
1120  bool check_function_as_value_generator(uchar *) override { return false; }
1121  enum Functype functype() const override { return MUL_FUNC; }
1122 };
1123 
1125  public:
1127  : Item_num_op(pos, a, b) {}
1129  longlong int_op() override;
1130  double real_op() override;
1132  enum Functype functype() const override { return DIV_FUNC; }
1133 
1134  protected:
1136 };
1137 
1138 class Item_func_div final : public Item_func_div_base {
1139  public:
1140  Item_func_div(const POS &pos, Item *a, Item *b)
1141  : Item_func_div_base(pos, a, b) {}
1142  const char *func_name() const override { return "/"; }
1143  bool resolve_type(THD *thd) override;
1144  void result_precision() override;
1145 };
1146 
1148  public:
1151  : Item_func_div_base(pos, a, b) {}
1152  const char *func_name() const override { return "DIV"; }
1154  return MYSQL_TYPE_LONGLONG;
1155  }
1156  bool resolve_type(THD *thd) override;
1157  void result_precision() override;
1158  void set_numeric_type() override;
1159  bool check_partition_func_processor(uchar *) override { return false; }
1160  bool check_function_as_value_generator(uchar *) override { return false; }
1161 };
1162 
1163 class Item_func_mod final : public Item_num_op {
1164  public:
1165  Item_func_mod(Item *a, Item *b) : Item_num_op(a, b) {}
1166  Item_func_mod(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1167 
1168  longlong int_op() override;
1169  double real_op() override;
1170  my_decimal *decimal_op(my_decimal *) override;
1171  const char *func_name() const override { return "%"; }
1172  void result_precision() override;
1173  bool resolve_type(THD *thd) override;
1174  bool check_partition_func_processor(uchar *) override { return false; }
1175  bool check_function_as_value_generator(uchar *) override { return false; }
1176  enum Functype functype() const override { return MOD_FUNC; }
1177 };
1178 
1179 class Item_func_neg final : public Item_func_num1 {
1180  public:
1183 
1184  double real_op() override;
1185  longlong int_op() override;
1186  my_decimal *decimal_op(my_decimal *) override;
1187  const char *func_name() const override { return "-"; }
1188  enum Functype functype() const override { return NEG_FUNC; }
1189  bool resolve_type(THD *thd) override;
1190  void fix_num_length_and_dec() override;
1191  bool check_partition_func_processor(uchar *) override { return false; }
1192  bool check_function_as_value_generator(uchar *) override { return false; }
1193 };
1194 
1195 class Item_func_abs final : public Item_func_num1 {
1196  public:
1198  double real_op() override;
1199  longlong int_op() override;
1200  my_decimal *decimal_op(my_decimal *) override;
1201  const char *func_name() const override { return "abs"; }
1202  bool resolve_type(THD *) override;
1203  bool check_partition_func_processor(uchar *) override { return false; }
1204  bool check_function_as_value_generator(uchar *) override { return false; }
1205  enum Functype functype() const override { return ABS_FUNC; }
1206 };
1207 
1208 // A class to handle logarithmic and trigonometric functions
1209 
1211  public:
1214 
1215  Item_dec_func(const POS &pos, Item *a, Item *b) : Item_real_func(pos, a, b) {}
1216  bool resolve_type(THD *thd) override;
1217 };
1218 
1219 class Item_func_exp final : public Item_dec_func {
1220  public:
1222  double val_real() override;
1223  const char *func_name() const override { return "exp"; }
1224  enum Functype functype() const override { return EXP_FUNC; }
1225 };
1226 
1227 class Item_func_ln final : public Item_dec_func {
1228  public:
1229  Item_func_ln(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1230  double val_real() override;
1231  const char *func_name() const override { return "ln"; }
1232  enum Functype functype() const override { return LN_FUNC; }
1233 };
1234 
1235 class Item_func_log final : public Item_dec_func {
1236  public:
1238  Item_func_log(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1239  double val_real() override;
1240  const char *func_name() const override { return "log"; }
1241  enum Functype functype() const override { return LOG_FUNC; }
1242 };
1243 
1244 class Item_func_log2 final : public Item_dec_func {
1245  public:
1247  double val_real() override;
1248  const char *func_name() const override { return "log2"; }
1249 };
1250 
1251 class Item_func_log10 final : public Item_dec_func {
1252  public:
1254  double val_real() override;
1255  const char *func_name() const override { return "log10"; }
1256  enum Functype functype() const override { return LOG10_FUNC; }
1257 };
1258 
1259 class Item_func_sqrt final : public Item_dec_func {
1260  public:
1262  double val_real() override;
1263  const char *func_name() const override { return "sqrt"; }
1264  enum Functype functype() const override { return SQRT_FUNC; }
1265 };
1266 
1267 class Item_func_pow final : public Item_dec_func {
1268  public:
1269  Item_func_pow(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1270  double val_real() override;
1271  const char *func_name() const override { return "pow"; }
1272 };
1273 
1274 class Item_func_acos final : public Item_dec_func {
1275  public:
1277  double val_real() override;
1278  const char *func_name() const override { return "acos"; }
1279  enum Functype functype() const override { return ACOS_FUNC; }
1280 };
1281 
1282 class Item_func_asin final : public Item_dec_func {
1283  public:
1285  double val_real() override;
1286  const char *func_name() const override { return "asin"; }
1287  enum Functype functype() const override { return ASIN_FUNC; }
1288 };
1289 
1290 class Item_func_atan final : public Item_dec_func {
1291  public:
1293  Item_func_atan(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1294  double val_real() override;
1295  const char *func_name() const override { return "atan"; }
1296  enum Functype functype() const override { return ATAN_FUNC; }
1297 };
1298 
1299 class Item_func_cos final : public Item_dec_func {
1300  public:
1302  double val_real() override;
1303  const char *func_name() const override { return "cos"; }
1304  enum Functype functype() const override { return COS_FUNC; }
1305 };
1306 
1307 class Item_func_sin final : public Item_dec_func {
1308  public:
1310  double val_real() override;
1311  const char *func_name() const override { return "sin"; }
1312  enum Functype functype() const override { return SIN_FUNC; }
1313 };
1314 
1315 class Item_func_tan final : public Item_dec_func {
1316  public:
1318  double val_real() override;
1319  const char *func_name() const override { return "tan"; }
1320  enum Functype functype() const override { return TAN_FUNC; }
1321 };
1322 
1323 class Item_func_cot final : public Item_dec_func {
1324  public:
1326  double val_real() override;
1327  const char *func_name() const override { return "cot"; }
1328  enum Functype functype() const override { return COT_FUNC; }
1329 };
1330 
1332  public:
1335  bool resolve_type_inner(THD *thd) override;
1336 };
1337 
1338 class Item_func_ceiling final : public Item_func_int_val {
1339  public:
1342  const char *func_name() const override { return "ceiling"; }
1343  longlong int_op() override;
1344  double real_op() override;
1345  my_decimal *decimal_op(my_decimal *) override;
1346  bool check_partition_func_processor(uchar *) override { return false; }
1347  bool check_function_as_value_generator(uchar *) override { return false; }
1348  enum Functype functype() const override { return CEILING_FUNC; }
1349 };
1350 
1351 class Item_func_floor final : public Item_func_int_val {
1352  public:
1355  const char *func_name() const override { return "floor"; }
1356  longlong int_op() override;
1357  double real_op() override;
1358  my_decimal *decimal_op(my_decimal *) override;
1359  bool check_partition_func_processor(uchar *) override { return false; }
1360  bool check_function_as_value_generator(uchar *) override { return false; }
1361  enum Functype functype() const override { return FLOOR_FUNC; }
1362 };
1363 
1364 /* This handles round and truncate */
1365 
1366 class Item_func_round final : public Item_func_num1 {
1367  bool truncate;
1368 
1369  public:
1370  Item_func_round(Item *a, Item *b, bool trunc_arg)
1371  : Item_func_num1(a, b), truncate(trunc_arg) {}
1372  Item_func_round(const POS &pos, Item *a, Item *b, bool trunc_arg)
1373  : Item_func_num1(pos, a, b), truncate(trunc_arg) {}
1374 
1375  const char *func_name() const override {
1376  return truncate ? "truncate" : "round";
1377  }
1378  double real_op() override;
1379  longlong int_op() override;
1380  my_decimal *decimal_op(my_decimal *) override;
1381  bool resolve_type(THD *) override;
1382  enum Functype functype() const override {
1383  return truncate ? TRUNCATE_FUNC : ROUND_FUNC;
1384  }
1385 };
1386 
1387 class Item_func_rand final : public Item_real_func {
1389 
1390  rand_struct *m_rand{nullptr};
1391  bool first_eval{true}; // true if val_real() is called 1st time
1392  public:
1394  explicit Item_func_rand(const POS &pos) : Item_real_func(pos) {}
1395 
1396  bool itemize(Parse_context *pc, Item **res) override;
1397  double val_real() override;
1398  const char *func_name() const override { return "rand"; }
1399  /**
1400  This function is non-deterministic and hence depends on the
1401  'RAND' pseudo-table.
1402 
1403  @retval RAND_TABLE_BIT
1404  */
1406  return RAND_TABLE_BIT;
1407  }
1408  bool fix_fields(THD *thd, Item **ref) override;
1409  bool resolve_type(THD *thd) override;
1410  void cleanup() override {
1411  first_eval = true;
1413  }
1414  bool check_function_as_value_generator(uchar *checker_args) override {
1416  pointer_cast<Check_function_as_value_generator_parameters *>(
1417  checker_args);
1418  func_arg->banned_function_name = func_name();
1419  return ((func_arg->source == VGS_GENERATED_COLUMN) ||
1420  (func_arg->source == VGS_CHECK_CONSTRAINT));
1421  }
1422 
1423  private:
1424  void seed_random(Item *val);
1425 };
1426 
1427 class Item_func_sign final : public Item_int_func {
1428  public:
1430  const char *func_name() const override { return "sign"; }
1431  longlong val_int() override;
1432  bool resolve_type(THD *thd) override;
1433 };
1434 
1435 // Common base class for the DEGREES and RADIANS functions.
1437  double mul, add;
1438 
1439  protected:
1440  Item_func_units(const POS &pos, Item *a, double mul_arg, double add_arg)
1441  : Item_real_func(pos, a), mul(mul_arg), add(add_arg) {}
1442 
1443  public:
1444  double val_real() override;
1445  bool resolve_type(THD *thd) override;
1446 };
1447 
1448 class Item_func_degrees final : public Item_func_units {
1449  public:
1451  : Item_func_units(pos, a, 180.0 / M_PI, 0.0) {}
1452  const char *func_name() const override { return "degrees"; }
1453  enum Functype functype() const override { return DEGREES_FUNC; }
1454 };
1455 
1456 class Item_func_radians final : public Item_func_units {
1457  public:
1459  : Item_func_units(pos, a, M_PI / 180.0, 0.0) {}
1460  const char *func_name() const override { return "radians"; }
1461  enum Functype functype() const override { return RADIANS_FUNC; }
1462 };
1464  public:
1465  Item_func_min_max(const POS &pos, PT_item_list *opt_list, bool is_least_func)
1466  : Item_func_numhybrid(pos, opt_list),
1467  m_is_least_func(is_least_func),
1469 
1470  longlong val_int() override;
1471  double val_real() override;
1472  my_decimal *val_decimal(my_decimal *) override;
1473  longlong int_op() override;
1474  double real_op() override;
1475  my_decimal *decimal_op(my_decimal *) override;
1476  String *str_op(String *) override;
1477  bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1478  bool time_op(MYSQL_TIME *ltime) override;
1480  return MYSQL_TYPE_VARCHAR;
1481  }
1482  bool resolve_type(THD *thd) override;
1483  bool resolve_type_inner(THD *thd) override;
1484  void set_numeric_type() override {}
1485  enum Item_result result_type() const override { return hybrid_type; }
1486 
1487  /**
1488  Make CAST(LEAST_OR_GREATEST(datetime_expr, varchar_expr))
1489  return a number in format YYMMDDhhmmss.
1490  */
1491  enum Item_result cast_to_int_type() const override {
1492  return compare_as_dates() ? INT_RESULT : result_type();
1493  }
1494 
1495  /// Returns true if arguments to this function should be compared as dates.
1496  bool compare_as_dates() const {
1497  return temporal_item != nullptr &&
1499  }
1500 
1501  /// Returns true if at least one of the arguments was of temporal type.
1502  bool has_temporal_arg() const { return temporal_item; }
1503 
1504  private:
1505  /// True if LEAST function, false if GREATEST.
1506  const bool m_is_least_func;
1508  /*
1509  Used for determining whether one of the arguments is of temporal type and
1510  for converting arguments to a common output format if arguments are
1511  compared as dates and result type is character string. For example,
1512  LEAST('95-05-05', date '10-10-10') should return '1995-05-05', not
1513  '95-05-05'.
1514  */
1516  /**
1517  Compare arguments as datetime values.
1518 
1519  @param value Pointer to which the datetime value of the winning argument
1520  is written.
1521 
1522  @return true if error, false otherwise.
1523  */
1524  bool cmp_datetimes(longlong *value);
1525 
1526  /**
1527  Compare arguments as time values.
1528 
1529  @param value Pointer to which the time value of the winning argument is
1530  written.
1531 
1532  @return true if error, false otherwise.
1533  */
1534  bool cmp_times(longlong *value);
1535 };
1536 
1537 class Item_func_min final : public Item_func_min_max {
1538  public:
1539  Item_func_min(const POS &pos, PT_item_list *opt_list)
1540  : Item_func_min_max(pos, opt_list, true) {}
1541  const char *func_name() const override { return "least"; }
1542  enum Functype functype() const override { return LEAST_FUNC; }
1543 };
1544 
1545 class Item_func_max final : public Item_func_min_max {
1546  public:
1547  Item_func_max(const POS &pos, PT_item_list *opt_list)
1548  : Item_func_min_max(pos, opt_list, false) {}
1549  const char *func_name() const override { return "greatest"; }
1550  enum Functype functype() const override { return GREATEST_FUNC; }
1551 };
1552 
1553 /**
1554  A wrapper Item that normally returns its parameter, but becomes NULL when
1555  processing rows for rollup. Rollup is implemented by AggregateIterator, and
1556  works by means of hierarchical levels -- 0 is the “grand totals” phase, 1 is
1557  where only one group level is active, and so on. E.g., for a query with GROUP
1558  BY a,b, the rows will look like this:
1559 
1560  a b rollup level
1561  1 1 2
1562  1 2 2
1563  1 NULL 1
1564  2 1 2
1565  2 NULL 1
1566  NULL NULL 0
1567 
1568  Each rollup group item has a minimum level for when it becomes NULL. In the
1569  example above, a would have minimum level 0 and b would have minimum level 1.
1570  For simplicity, the JOIN carries a list of all rollup group items, and they
1571  are being given the current rollup level when it changes. A rollup level of
1572  INT_MAX essentially always disables rollup, which is useful when there are
1573  leftover group items in places that are not relevant for rollup
1574  (e.g., sometimes resolving can leave rollup wrappers in place for temporary
1575  tables that are created before grouping, which should then effectively be
1576  disabled).
1577  */
1578 class Item_rollup_group_item final : public Item_func {
1579  public:
1584  // We're going to replace inner_item in the SELECT list, so copy its hidden
1585  // status. (We could have done this in the caller, but it fits naturally in
1586  // with all the other copying done here.)
1588  set_nullable(true);
1589  set_rollup_expr();
1590  }
1591  double val_real() override;
1592  longlong val_int() override;
1593  String *val_str(String *str) override;
1594  my_decimal *val_decimal(my_decimal *dec) override;
1595  bool val_json(Json_wrapper *result) override;
1596  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1597  bool get_time(MYSQL_TIME *ltime) override;
1598  const char *func_name() const override { return "rollup_group_item"; }
1599  table_map used_tables() const override {
1600  /*
1601  If underlying item is non-constant, return its used_tables value.
1602  Otherwise, ensure it is non-constant by adding RAND_TABLE_BIT.
1603  */
1604  return args[0]->const_for_execution()
1605  ? (args[0]->used_tables() | RAND_TABLE_BIT)
1606  : args[0]->used_tables();
1607  }
1608  Item_result result_type() const override { return args[0]->result_type(); }
1609  bool resolve_type(THD *) override {
1610  // needn't handle dynamic parameter as its const_item() is false.
1612 
1613  // The item could be a NULL constant.
1614  null_value = args[0]->is_null();
1615  return false;
1616  }
1617  Item *inner_item() const { return args[0]; }
1618  bool rollup_null() const {
1620  }
1621  enum Functype functype() const override { return ROLLUP_GROUP_ITEM_FUNC; }
1622  void print(const THD *thd, String *str,
1623  enum_query_type query_type) const override;
1624 
1625  // Used by AggregateIterator.
1627 
1628  // Used when cloning the item only.
1629  int min_rollup_level() const { return m_min_rollup_level; }
1630 
1631  private:
1633  int m_current_rollup_level = INT_MAX;
1634 };
1635 
1638 
1639  public:
1641  longlong val_int() override;
1642  const char *func_name() const override { return "length"; }
1643  bool resolve_type(THD *thd) override {
1644  if (param_type_is_default(thd, 0, 1)) return true;
1645  max_length = 10;
1646  return false;
1647  }
1648 };
1649 
1651  public:
1653  longlong val_int() override {
1654  assert(fixed);
1655  return Item_func_length::val_int() * 8;
1656  }
1657  const char *func_name() const override { return "bit_length"; }
1658 };
1659 
1660 class Item_func_char_length final : public Item_int_func {
1662 
1663  public:
1666  longlong val_int() override;
1667  const char *func_name() const override { return "char_length"; }
1668  bool resolve_type(THD *thd) override {
1669  max_length = 10;
1670  return Item_int_func::resolve_type(thd);
1671  }
1672 };
1673 
1675  public:
1677  null_on_null = false;
1678  }
1679  longlong val_int() override;
1680  const char *func_name() const override { return "coercibility"; }
1681  bool resolve_type(THD *thd) override {
1682  max_length = 10;
1683  set_nullable(false);
1684  return Item_int_func::resolve_type(thd);
1685  }
1686 };
1687 
1690 
1691  public:
1694  : Item_int_func(pos, a, b) {}
1695  Item_func_locate(const POS &pos, Item *a, Item *b, Item *c)
1696  : Item_int_func(pos, a, b, c) {}
1697 
1698  const char *func_name() const override { return "locate"; }
1699  longlong val_int() override;
1700  bool resolve_type(THD *thd) override;
1701  void print(const THD *thd, String *str,
1702  enum_query_type query_type) const override;
1703 };
1704 
1705 class Item_func_instr final : public Item_func_locate {
1706  public:
1707  Item_func_instr(const POS &pos, Item *a, Item *b)
1708  : Item_func_locate(pos, a, b) {}
1709 
1710  const char *func_name() const override { return "instr"; }
1711 };
1712 
1714  public:
1716  : Item_int_func(pos, a) {}
1717  longlong val_int() override;
1718  const char *func_name() const override {
1719  return "validate_password_strength";
1720  }
1721  bool resolve_type(THD *thd) override {
1722  max_length = 10;
1723  set_nullable(true);
1724  return Item_int_func::resolve_type(thd);
1725  }
1726 };
1727 
1728 class Item_func_field final : public Item_int_func {
1731 
1732  public:
1734  : Item_int_func(pos, opt_list) {}
1735  longlong val_int() override;
1736  const char *func_name() const override { return "field"; }
1737  bool resolve_type(THD *thd) override;
1738 };
1739 
1740 class Item_func_ascii final : public Item_int_func {
1742 
1743  public:
1745  longlong val_int() override;
1746  const char *func_name() const override { return "ascii"; }
1747  bool resolve_type(THD *thd) override {
1748  max_length = 3;
1749  return Item_int_func::resolve_type(thd);
1750  }
1751 };
1752 
1753 class Item_func_ord final : public Item_int_func {
1755 
1756  public:
1758  longlong val_int() override;
1759  const char *func_name() const override { return "ord"; }
1760 };
1761 
1762 class Item_func_find_in_set final : public Item_int_func {
1767 
1768  public:
1770  : Item_int_func(pos, a, b), enum_value(0) {}
1771  longlong val_int() override;
1772  const char *func_name() const override { return "find_in_set"; }
1773  bool resolve_type(THD *) override;
1774  const CHARSET_INFO *compare_collation() const override {
1775  return cmp_collation.collation;
1776  }
1777 };
1778 
1779 /* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
1780 
1781 class Item_func_bit : public Item_func {
1782  protected:
1783  /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1785  /// Buffer storing the determined value
1787  /**
1788  @returns true if the second argument should be of binary type for the
1789  result to be of binary type.
1790  */
1792 
1793  public:
1794  Item_func_bit(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {}
1795  Item_func_bit(const POS &pos, Item *a) : Item_func(pos, a) {}
1796 
1797  bool resolve_type(THD *) override;
1798  enum Item_result result_type() const override { return hybrid_type; }
1799 
1800  longlong val_int() override;
1801  String *val_str(String *str) override;
1802  double val_real() override;
1803  my_decimal *val_decimal(my_decimal *decimal_value) override;
1804 
1805  void print(const THD *thd, String *str,
1806  enum_query_type query_type) const override {
1807  print_op(thd, str, query_type);
1808  }
1809  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1810  if (hybrid_type == INT_RESULT)
1811  return get_date_from_int(ltime, fuzzydate);
1812  else
1813  return get_date_from_string(ltime, fuzzydate);
1814  }
1815  bool get_time(MYSQL_TIME *ltime) override {
1816  if (hybrid_type == INT_RESULT)
1817  return get_time_from_int(ltime);
1818  else
1819  return get_time_from_string(ltime);
1820  }
1821 
1822  private:
1823  /**
1824  @brief Performs the operation on integers to produce a result of type
1825  INT_RESULT.
1826  @return The result of the operation.
1827  */
1828  virtual longlong int_op() = 0;
1829 
1830  /**
1831  @brief Performs the operation on binary strings to produce a result of
1832  type STRING_RESULT.
1833  @return The result of the operation.
1834  */
1835  virtual String *str_op(String *) = 0;
1836 };
1837 
1838 /**
1839  Base class for all the bit functions that work with two binary
1840  arguments: '&', '|', '^'.
1841 */
1842 
1844  protected:
1846  return true;
1847  }
1848  template <class Char_func, class Int_func>
1849  String *eval_str_op(String *, Char_func char_func, Int_func int_func);
1850  template <class Int_func>
1851  longlong eval_int_op(Int_func int_func);
1852 
1853  public:
1855  : Item_func_bit(pos, a, b) {}
1856 };
1857 
1859  public:
1861  : Item_func_bit_two_param(pos, a, b) {}
1862  const char *func_name() const override { return "|"; }
1863 
1864  private:
1865  longlong int_op() override { return eval_int_op(std::bit_or<ulonglong>()); }
1866  String *str_op(String *str) override {
1867  return eval_str_op(str, std::bit_or<char>(), std::bit_or<ulonglong>());
1868  }
1869 };
1870 
1872  public:
1874  : Item_func_bit_two_param(pos, a, b) {}
1875  const char *func_name() const override { return "&"; }
1876 
1877  private:
1878  longlong int_op() override { return eval_int_op(std::bit_and<ulonglong>()); }
1879  String *str_op(String *str) override {
1880  return eval_str_op(str, std::bit_and<char>(), std::bit_and<ulonglong>());
1881  }
1882 };
1883 
1885  public:
1887  : Item_func_bit_two_param(pos, a, b) {}
1888  const char *func_name() const override { return "^"; }
1889 
1890  private:
1891  longlong int_op() override { return eval_int_op(std::bit_xor<ulonglong>()); }
1892  String *str_op(String *str) override {
1893  return eval_str_op(str, std::bit_xor<char>(), std::bit_xor<ulonglong>());
1894  }
1895 };
1896 
1897 class Item_func_bit_count final : public Item_int_func {
1898  public:
1900  longlong val_int() override;
1901  const char *func_name() const override { return "bit_count"; }
1902  bool resolve_type(THD *thd) override {
1903  // Default: binary string; reprepare if integer
1904  if (args[0]->data_type() == MYSQL_TYPE_INVALID &&
1905  args[0]->propagate_type(
1907  return true;
1909  return false;
1910  }
1911 };
1912 
1914  protected:
1916  return false;
1917  }
1918  template <bool to_left>
1920  template <bool to_left>
1922 
1923  public:
1924  Item_func_shift(const POS &pos, Item *a, Item *b)
1925  : Item_func_bit(pos, a, b) {}
1926 };
1927 
1929  public:
1931  : Item_func_shift(pos, a, b) {}
1932  const char *func_name() const override { return "<<"; }
1933 
1934  private:
1935  longlong int_op() override { return eval_int_op<true>(); }
1936  String *str_op(String *str) override { return eval_str_op<true>(str); }
1937 };
1938 
1940  public:
1942  : Item_func_shift(pos, a, b) {}
1943  const char *func_name() const override { return ">>"; }
1944 
1945  private:
1946  longlong int_op() override { return eval_int_op<false>(); }
1947  String *str_op(String *str) override { return eval_str_op<false>(str); }
1948 };
1949 
1950 class Item_func_bit_neg final : public Item_func_bit {
1951  protected:
1953  return false;
1954  }
1955 
1956  public:
1958  const char *func_name() const override { return "~"; }
1959  void print(const THD *thd, String *str,
1960  enum_query_type query_type) const override {
1961  Item_func::print(thd, str, query_type);
1962  }
1963 
1964  private:
1965  longlong int_op() override;
1966  String *str_op(String *str) override;
1967 };
1968 
1971 
1972  public:
1976 
1977  bool itemize(Parse_context *pc, Item **res) override;
1978  longlong val_int() override;
1979  const char *func_name() const override { return "last_insert_id"; }
1980  bool resolve_type(THD *thd) override {
1981  if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_LONGLONG)) return true;
1982  unsigned_flag = true;
1983  return false;
1984  }
1985  bool check_function_as_value_generator(uchar *checker_args) override {
1987  pointer_cast<Check_function_as_value_generator_parameters *>(
1988  checker_args);
1989  func_arg->banned_function_name = func_name();
1990  return true;
1991  }
1992 };
1993 
1994 class Item_func_benchmark final : public Item_int_func {
1996 
1997  public:
1998  Item_func_benchmark(const POS &pos, Item *count_expr, Item *expr)
1999  : Item_int_func(pos, count_expr, expr) {}
2000 
2001  /// Ensure that "benchmark()" is never optimized away
2003  return RAND_TABLE_BIT;
2004  }
2005 
2006  bool itemize(Parse_context *pc, Item **res) override;
2007  longlong val_int() override;
2008  const char *func_name() const override { return "benchmark"; }
2009  bool resolve_type(THD *thd) override {
2010  if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_LONGLONG)) return true;
2011  if (param_type_is_default(thd, 1, 2)) return true;
2012  max_length = 1;
2013  set_nullable(true);
2014  return false;
2015  }
2016  void print(const THD *thd, String *str,
2017  enum_query_type query_type) const override;
2018  bool check_function_as_value_generator(uchar *checker_args) override {
2020  pointer_cast<Check_function_as_value_generator_parameters *>(
2021  checker_args);
2022  func_arg->banned_function_name = func_name();
2023  return true;
2024  }
2025 };
2026 
2027 void item_func_sleep_init();
2028 void item_func_sleep_free();
2029 
2030 class Item_func_sleep final : public Item_int_func {
2032 
2033  public:
2035 
2036  bool itemize(Parse_context *pc, Item **res) override;
2037  const char *func_name() const override { return "sleep"; }
2038  /**
2039  This function is non-deterministic and hence depends on the
2040  'RAND' pseudo-table.
2041 
2042  @retval RAND_TABLE_BIT
2043  */
2045  return RAND_TABLE_BIT;
2046  }
2047  bool check_function_as_value_generator(uchar *checker_args) override {
2049  pointer_cast<Check_function_as_value_generator_parameters *>(
2050  checker_args);
2051  func_arg->banned_function_name = func_name();
2052  return true;
2053  }
2054  bool resolve_type(THD *thd) override {
2055  if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_DOUBLE)) return true;
2056  return Item_int_func::resolve_type(thd);
2057  }
2058  longlong val_int() override;
2059 };
2060 
2061 class Item_udf_func : public Item_func {
2062  typedef Item_func super;
2063 
2064  protected:
2066  bool is_expensive_processor(uchar *) override { return true; }
2067 
2068  public:
2069  Item_udf_func(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2070  : Item_func(pos, opt_list), udf(udf_arg) {
2071  null_on_null = false;
2072  }
2073  ~Item_udf_func() override = default;
2074 
2075  bool itemize(Parse_context *pc, Item **res) override;
2076  const char *func_name() const override { return udf.name(); }
2077  enum Functype functype() const override { return UDF_FUNC; }
2078  bool fix_fields(THD *thd, Item **ref) override;
2079  void update_used_tables() override {
2080  /*
2081  TODO: Make a member in UDF_INIT and return if a UDF is deterministic or
2082  not.
2083  Currently UDF_INIT has a member (const_item) that is an in/out
2084  parameter to the init() call.
2085  The code in udf_handler::fix_fields also duplicates the arguments
2086  handling code in Item_func::fix_fields().
2087 
2088  The lack of information if a UDF is deterministic makes writing
2089  a correct update_used_tables() for UDFs impossible.
2090  One solution to this would be :
2091  - Add a is_deterministic member of UDF_INIT
2092  - (optionally) deprecate the const_item member of UDF_INIT
2093  - Take away the duplicate code from udf_handler::fix_fields() and
2094  make Item_udf_func call Item_func::fix_fields() to process its
2095  arguments as for any other function.
2096  - Store the deterministic flag returned by <udf>_init into the
2097  udf_handler.
2098  - Don't implement Item_udf_func::fix_fields, implement
2099  Item_udf_func::resolve_type() instead (similar to non-UDF functions).
2100  - Override Item_func::update_used_tables to call
2101  Item_func::update_used_tables() and add a RAND_TABLE_BIT to the
2102  result of Item_func::update_used_tables() if the UDF is
2103  non-deterministic.
2104  - (optionally) rename RAND_TABLE_BIT to NONDETERMINISTIC_BIT to
2105  better describe its usage.
2106 
2107  The above would require a change of the UDF API.
2108  Until that change is done here's how the current code works:
2109  We call Item_func::update_used_tables() only when we know that
2110  the function depends on real non-const tables and is deterministic.
2111  This can be done only because we know that the optimizer will
2112  call update_used_tables() only when there's possibly a new const
2113  table. So update_used_tables() can only make a Item_func more
2114  constant than it is currently.
2115  That's why we don't need to do anything if a function is guaranteed
2116  to return non-constant (it's non-deterministic) or is already a
2117  const.
2118  */
2122 
2124  assert(!null_on_null); // no need to update not_null_tables_cache
2125  }
2126  void cleanup() override;
2127  Item_result result_type() const override { return udf.result_type(); }
2128  bool is_expensive() override { return true; }
2129  void print(const THD *thd, String *str,
2130  enum_query_type query_type) const override;
2131 
2132  bool check_function_as_value_generator(uchar *checker_args) override {
2134  pointer_cast<Check_function_as_value_generator_parameters *>(
2135  checker_args);
2136  func_arg->banned_function_name = func_name();
2137  return true;
2138  }
2139 
2140  protected:
2141  bool may_have_named_parameters() const override { return true; }
2142 };
2143 
2144 class Item_func_udf_float final : public Item_udf_func {
2145  public:
2146  Item_func_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2147  : Item_udf_func(pos, udf_arg, opt_list) {}
2148  longlong val_int() override {
2149  assert(fixed == 1);
2150  return (longlong)rint(Item_func_udf_float::val_real());
2151  }
2152  my_decimal *val_decimal(my_decimal *dec_buf) override {
2153  double res = val_real();
2154  if (null_value) return nullptr;
2155  double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
2156  return dec_buf;
2157  }
2158  double val_real() override;
2159  String *val_str(String *str) override;
2160  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2161  return get_date_from_real(ltime, fuzzydate);
2162  }
2163  bool get_time(MYSQL_TIME *ltime) override {
2164  return get_time_from_real(ltime);
2165  }
2166  bool resolve_type(THD *) override {
2168  fix_num_length_and_dec(); // @todo - needed?
2169  return false;
2170  }
2171 };
2172 
2173 class Item_func_udf_int final : public Item_udf_func {
2174  public:
2175  Item_func_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2176  : Item_udf_func(pos, udf_arg, opt_list) {}
2177  longlong val_int() override;
2178  double val_real() override {
2179  return static_cast<double>(Item_func_udf_int::val_int());
2180  }
2181  String *val_str(String *str) override;
2182  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2183  return get_date_from_int(ltime, fuzzydate);
2184  }
2185  bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
2186  enum Item_result result_type() const override { return INT_RESULT; }
2187  bool resolve_type(THD *) override {
2189  return false;
2190  }
2191 };
2192 
2194  public:
2196  PT_item_list *opt_list)
2197  : Item_udf_func(pos, udf_arg, opt_list) {}
2198  longlong val_int() override;
2199  double val_real() override;
2200  my_decimal *val_decimal(my_decimal *) override;
2201  String *val_str(String *str) override;
2202  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2203  return get_date_from_decimal(ltime, fuzzydate);
2204  }
2205  bool get_time(MYSQL_TIME *ltime) override {
2206  return get_time_from_decimal(ltime);
2207  }
2208  enum Item_result result_type() const override { return DECIMAL_RESULT; }
2209  bool resolve_type(THD *thd) override;
2210 };
2211 
2213  public:
2214  Item_func_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2215  : Item_udf_func(pos, udf_arg, opt_list) {}
2216 
2217  String *val_str(String *) override;
2218  double val_real() override {
2219  int err_not_used;
2220  const char *end_not_used;
2221  String *res;
2222  res = val_str(&str_value);
2223  return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2224  &end_not_used, &err_not_used)
2225  : 0.0;
2226  }
2227  longlong val_int() override {
2228  int err_not_used;
2229  String *res;
2230  res = val_str(&str_value);
2231  return res ? my_strntoll(res->charset(), res->ptr(), res->length(), 10,
2232  nullptr, &err_not_used)
2233  : (longlong)0;
2234  }
2235  my_decimal *val_decimal(my_decimal *dec_buf) override {
2236  String *res = val_str(&str_value);
2237  if (!res) return nullptr;
2238  str2my_decimal(E_DEC_FATAL_ERROR, res->ptr(), res->length(), res->charset(),
2239  dec_buf);
2240  return dec_buf;
2241  }
2242  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2243  return get_date_from_string(ltime, fuzzydate);
2244  }
2245  bool get_time(MYSQL_TIME *ltime) override {
2246  return get_time_from_string(ltime);
2247  }
2248  enum Item_result result_type() const override { return STRING_RESULT; }
2249  bool resolve_type(THD *thd) override;
2250 };
2251 
2252 void mysql_ull_cleanup(THD *thd);
2254 
2255 class Item_func_get_lock final : public Item_int_func {
2257 
2259 
2260  public:
2262  : Item_int_func(pos, a, b) {}
2263 
2264  bool itemize(Parse_context *pc, Item **res) override;
2265  longlong val_int() override;
2266  const char *func_name() const override { return "get_lock"; }
2267  bool resolve_type(THD *thd) override {
2268  if (param_type_is_default(thd, 0, 1)) return true;
2269  if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_LONGLONG)) return true;
2270  max_length = 1;
2271  set_nullable(true);
2272  return false;
2273  }
2274  bool is_non_const_over_literals(uchar *) override { return true; }
2275  bool check_function_as_value_generator(uchar *checker_args) override {
2277  pointer_cast<Check_function_as_value_generator_parameters *>(
2278  checker_args);
2279  func_arg->banned_function_name = func_name();
2280  return true;
2281  }
2282 };
2283 
2286 
2288 
2289  public:
2291  bool itemize(Parse_context *pc, Item **res) override;
2292 
2293  longlong val_int() override;
2294  const char *func_name() const override { return "release_lock"; }
2295  bool resolve_type(THD *thd) override {
2296  if (param_type_is_default(thd, 0, 1)) return true;
2297  max_length = 1;
2298  set_nullable(true);
2299  return false;
2300  }
2301  bool is_non_const_over_literals(uchar *) override { return true; }
2302  bool check_function_as_value_generator(uchar *checker_args) override {
2304  pointer_cast<Check_function_as_value_generator_parameters *>(
2305  checker_args);
2306  func_arg->banned_function_name = func_name();
2307  return true;
2308  }
2309 };
2310 
2313 
2314  public:
2316  bool itemize(Parse_context *pc, Item **res) override;
2317 
2318  longlong val_int() override;
2319  const char *func_name() const override { return "release_all_locks"; }
2320  bool resolve_type(THD *) override {
2321  unsigned_flag = true;
2322  return false;
2323  }
2324  bool is_non_const_over_literals(uchar *) override { return true; }
2325  bool check_function_as_value_generator(uchar *checker_args) override {
2327  pointer_cast<Check_function_as_value_generator_parameters *>(
2328  checker_args);
2329  func_arg->banned_function_name = func_name();
2330  return true;
2331  }
2332 };
2333 
2334 /* replication functions */
2335 
2339 
2340  public:
2342  : Item_int_func(pos, a, b) {}
2344  : Item_int_func(pos, a, b, c) {}
2345  Item_source_pos_wait(const POS &pos, Item *a, Item *b, Item *c, Item *d)
2346  : Item_int_func(pos, a, b, c, d) {}
2347 
2348  bool itemize(Parse_context *pc, Item **res) override;
2349  longlong val_int() override;
2350  const char *func_name() const override { return "source_pos_wait"; }
2351  bool resolve_type(THD *thd) override {
2352  if (param_type_is_default(thd, 0, 1)) return true;
2353  if (param_type_is_default(thd, 1, 3, MYSQL_TYPE_LONGLONG)) return true;
2354  if (param_type_is_default(thd, 3, 4)) return true;
2355  max_length = 21;
2356  set_nullable(true);
2357  return false;
2358  }
2359  bool check_function_as_value_generator(uchar *checker_args) override {
2361  pointer_cast<Check_function_as_value_generator_parameters *>(
2362  checker_args);
2363  func_arg->banned_function_name = func_name();
2364  return true;
2365  }
2366 };
2367 
2369  public:
2371  : Item_source_pos_wait(pos, a, b) {}
2373  : Item_source_pos_wait(pos, a, b, c) {}
2374  Item_master_pos_wait(const POS &pos, Item *a, Item *b, Item *c, Item *d)
2375  : Item_source_pos_wait(pos, a, b, c, d) {}
2376  longlong val_int() override;
2377 };
2378 
2379 /**
2380  This class is used for implementing the new wait_for_executed_gtid_set
2381  function and the functions related to them. This new function is independent
2382  of the slave threads.
2383 */
2386 
2388 
2389  public:
2391  : Item_int_func(pos, a) {}
2393  : Item_int_func(pos, a, b) {}
2394 
2395  bool itemize(Parse_context *pc, Item **res) override;
2396  longlong val_int() override;
2397  const char *func_name() const override {
2398  return "wait_for_executed_gtid_set";
2399  }
2400  bool resolve_type(THD *thd) override {
2401  if (param_type_is_default(thd, 0, 1)) return true;
2402  if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_DOUBLE)) return true;
2403  set_nullable(true);
2404  return false;
2405  }
2406 };
2407 
2410 
2412 
2413  public:
2414  Item_master_gtid_set_wait(const POS &pos, Item *a);
2415  Item_master_gtid_set_wait(const POS &pos, Item *a, Item *b);
2416  Item_master_gtid_set_wait(const POS &pos, Item *a, Item *b, Item *c);
2417 
2418  bool itemize(Parse_context *pc, Item **res) override;
2419  longlong val_int() override;
2420  const char *func_name() const override {
2421  return "wait_until_sql_thread_after_gtids";
2422  }
2423  bool resolve_type(THD *thd) override {
2424  if (param_type_is_default(thd, 0, 1)) return true;
2425  if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_DOUBLE)) return true;
2426  if (param_type_is_default(thd, 2, 3)) return true;
2427  set_nullable(true);
2428  return false;
2429  }
2430 };
2431 
2432 class Item_func_gtid_subset final : public Item_int_func {
2435 
2436  public:
2438  : Item_int_func(pos, a, b) {}
2439  longlong val_int() override;
2440  const char *func_name() const override { return "gtid_subset"; }
2441  bool resolve_type(THD *thd) override {
2442  if (param_type_is_default(thd, 0, -1)) return true;
2443  set_nullable(false);
2444  return false;
2445  }
2446  bool is_bool_func() const override { return true; }
2447 };
2448 
2449 /**
2450  Internal functions used by INFORMATION_SCHEMA implementation to check
2451  if user have access to given database/table/column.
2452 */
2453 
2455  public:
2457  : Item_int_func(pos, a) {}
2458  longlong val_int() override;
2459  const char *func_name() const override { return "can_access_database"; }
2460  bool resolve_type(THD *) override {
2461  set_nullable(true);
2462  return false;
2463  }
2464 };
2465 
2467  public:
2469  : Item_int_func(pos, a, b) {}
2470  longlong val_int() override;
2471  const char *func_name() const override { return "can_access_table"; }
2472  bool resolve_type(THD *) override {
2473  set_nullable(true);
2474  return false;
2475  }
2476 };
2477 
2479  public:
2481  : Item_int_func(pos, a, b) {}
2482  longlong val_int() override;
2483  const char *func_name() const override { return "can_access_user"; }
2484  bool resolve_type(THD *) override {
2485  set_nullable(true);
2486  return false;
2487  }
2488 };
2489 
2491  public:
2493  : Item_int_func(pos, a, b) {}
2494  longlong val_int() override;
2495  const char *func_name() const override { return "can_access_trigger"; }
2496  bool resolve_type(THD *) override {
2497  max_length = 4;
2498  set_nullable(true);
2499  return false;
2500  }
2501 };
2502 
2504  public:
2506  : Item_int_func(pos, list) {}
2507  longlong val_int() override;
2508  const char *func_name() const override { return "can_access_routine"; }
2509  bool resolve_type(THD *) override {
2510  max_length = 4;
2511  set_nullable(true);
2512  return false;
2513  }
2514 };
2515 
2517  public:
2519  longlong val_int() override;
2520  const char *func_name() const override { return "can_access_event"; }
2521  bool resolve_type(THD *) override {
2522  set_nullable(true);
2523  return false;
2524  }
2525 };
2526 
2528  public:
2530  : Item_int_func(pos, a) {}
2531  longlong val_int() override;
2532  const char *func_name() const override { return "can_access_resource_group"; }
2533  bool resolve_type(THD *) override {
2534  max_length = 1; // Function can return 0 or 1.
2535  set_nullable(true);
2536  return false;
2537  }
2538 };
2539 
2541  public:
2543  : Item_int_func(pos, a, b, c, d) {}
2544  longlong val_int() override;
2545  const char *func_name() const override { return "can_access_view"; }
2546  bool resolve_type(THD *) override {
2547  set_nullable(true);
2548  return false;
2549  }
2550 };
2551 
2553  public:
2555  : Item_int_func(pos, a, b, c) {}
2556  longlong val_int() override;
2557  const char *func_name() const override { return "can_access_column"; }
2558  bool resolve_type(THD *) override {
2559  set_nullable(true);
2560  return false;
2561  }
2562 };
2563 
2565  public:
2567  : Item_int_func(pos, a) {}
2569  : Item_int_func(pos, a, b) {}
2570  longlong val_int() override;
2571  const char *func_name() const override { return "is_visible_dd_object"; }
2572  bool resolve_type(THD *) override {
2573  max_length = 1;
2574  set_nullable(true);
2575  return false;
2576  }
2577 };
2578 
2580  public:
2582  : Item_int_func(pos, list) {}
2583  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2584  longlong val_int() override;
2585  const char *func_name() const override { return "internal_table_rows"; }
2586  bool resolve_type(THD *) override {
2587  set_nullable(true);
2588  unsigned_flag = true;
2589  null_on_null = false;
2590  return false;
2591  }
2592 };
2593 
2595  public:
2597  : Item_int_func(pos, list) {}
2598  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2599  longlong val_int() override;
2600  const char *func_name() const override { return "internal_avg_row_length"; }
2601  bool resolve_type(THD *) override {
2602  set_nullable(true);
2603  unsigned_flag = true;
2604  null_on_null = false;
2605  return false;
2606  }
2607 };
2608 
2610  public:
2612  : Item_int_func(pos, list) {}
2613  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2614  longlong val_int() override;
2615  const char *func_name() const override { return "internal_data_length"; }
2616  bool resolve_type(THD *) override {
2617  set_nullable(true);
2618  unsigned_flag = true;
2619  null_on_null = false;
2620  return false;
2621  }
2622 };
2623 
2625  public:
2627  : Item_int_func(pos, list) {}
2628  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2629  longlong val_int() override;
2630  const char *func_name() const override { return "internal_max_data_length"; }
2631  bool resolve_type(THD *) override {
2632  set_nullable(true);
2633  unsigned_flag = true;
2634  null_on_null = false;
2635  return false;
2636  }
2637 };
2638 
2640  public:
2642  : Item_int_func(pos, list) {}
2643  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2644  longlong val_int() override;
2645  const char *func_name() const override { return "internal_index_length"; }
2646  bool resolve_type(THD *) override {
2647  set_nullable(true);
2648  unsigned_flag = true;
2649  null_on_null = false;
2650  return false;
2651  }
2652 };
2653 
2655  public:
2657  : Item_int_func(pos, list) {}
2658  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2659  longlong val_int() override;
2660  const char *func_name() const override { return "internal_data_free"; }
2661  bool resolve_type(THD *) override {
2662  set_nullable(true);
2663  unsigned_flag = true;
2664  null_on_null = false;
2665  return false;
2666  }
2667 };
2668 
2670  public:
2672  : Item_int_func(pos, list) {}
2673  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2674  longlong val_int() override;
2675  const char *func_name() const override { return "internal_auto_increment"; }
2676  bool resolve_type(THD *) override {
2677  set_nullable(true);
2678  unsigned_flag = true;
2679  null_on_null = false;
2680  return false;
2681  }
2682 };
2683 
2685  public:
2687  : Item_int_func(pos, list) {}
2688  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2689  longlong val_int() override;
2690  const char *func_name() const override { return "internal_checksum"; }
2691  bool resolve_type(THD *) override {
2692  set_nullable(true);
2693  null_on_null = false;
2694  return false;
2695  }
2696 };
2697 
2699  public:
2701  : Item_int_func(pos, a) {}
2702  longlong val_int() override;
2703  const char *func_name() const override { return "internal_keys_disabled"; }
2704  bool resolve_type(THD *) override {
2705  set_nullable(false);
2706  null_on_null = false;
2707  return false;
2708  }
2709 };
2710 
2712  public:
2714  PT_item_list *list)
2715  : Item_int_func(pos, list) {}
2716  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2717  longlong val_int() override;
2718  const char *func_name() const override {
2719  return "internal_index_column_cardinality";
2720  }
2721  bool resolve_type(THD *) override {
2722  set_nullable(true);
2723  null_on_null = false;
2724  return false;
2725  }
2726 };
2727 
2729  public:
2731  Item *d)
2732  : Item_int_func(pos, a, b, c, d) {}
2733  longlong val_int() override;
2734  const char *func_name() const override { return "internal_dd_char_length"; }
2735  bool resolve_type(THD *) override {
2736  set_nullable(true);
2737  null_on_null = false;
2738  return false;
2739  }
2740 };
2741 
2743  : public Item_int_func {
2744  public:
2746  PT_item_list *list)
2747  : Item_int_func(pos, list) {}
2748  longlong val_int() override;
2749  const char *func_name() const override {
2750  return "internal_get_view_warning_or_error";
2751  }
2752  bool resolve_type(THD *) override {
2753  max_length = 1;
2754  set_nullable(false);
2755  null_on_null = false;
2756  return false;
2757  }
2758 };
2759 
2761  public:
2763  : Item_int_func(pos, list) {}
2764  longlong val_int() override;
2765  bool resolve_type(THD *) override {
2766  set_nullable(true);
2767  null_on_null = false;
2768  return false;
2769  }
2770  const char *func_name() const override {
2771  return "get_dd_index_sub_part_length";
2772  }
2773 };
2774 
2776  public:
2778  Item *d)
2779  : Item_int_func(pos, a, b, c, d) {}
2780  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2781  longlong val_int() override;
2782  const char *func_name() const override { return "internal_tablespace_id"; }
2783  bool resolve_type(THD *) override {
2784  set_nullable(true);
2785  null_on_null = false;
2786  return false;
2787  }
2788 };
2789 
2791  : public Item_int_func {
2792  public:
2794  Item *b, Item *c, Item *d)
2795  : Item_int_func(pos, a, b, c, d) {}
2796 
2797  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2798  longlong val_int() override;
2799 
2800  const char *func_name() const override {
2801  return "internal_tablespace_logfile_group_number";
2802  }
2803 
2804  bool resolve_type(THD *) override {
2805  set_nullable(true);
2806  null_on_null = false;
2807  return false;
2808  }
2809 };
2810 
2812  public:
2814  Item *c, Item *d)
2815  : Item_int_func(pos, a, b, c, d) {}
2816 
2817  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2818  longlong val_int() override;
2819 
2820  const char *func_name() const override {
2821  return "internal_tablespace_free_extents";
2822  }
2823 
2824  bool resolve_type(THD *) override {
2825  set_nullable(true);
2826  null_on_null = false;
2827  return false;
2828  }
2829 };
2830 
2832  public:
2834  Item *c, Item *d)
2835  : Item_int_func(pos, a, b, c, d) {}
2836 
2837  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2838  longlong val_int() override;
2839 
2840  const char *func_name() const override {
2841  return "internal_tablespace_total_extents";
2842  }
2843 
2844  bool resolve_type(THD *) override {
2845  set_nullable(true);
2846  null_on_null = false;
2847  return false;
2848  }
2849 };
2850 
2852  public:
2854  Item *c, Item *d)
2855  : Item_int_func(pos, a, b, c, d) {}
2856 
2857  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2858  longlong val_int() override;
2859 
2860  const char *func_name() const override {
2861  return "internal_tablespace_extent_size";
2862  }
2863 
2864  bool resolve_type(THD *) override {
2865  set_nullable(true);
2866  null_on_null = false;
2867  return false;
2868  }
2869 };
2870 
2872  public:
2874  Item *c, Item *d)
2875  : Item_int_func(pos, a, b, c, d) {}
2876 
2877  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2878  longlong val_int() override;
2879 
2880  const char *func_name() const override {
2881  return "internal_tablespace_initial_size";
2882  }
2883 
2884  bool resolve_type(THD *) override {
2885  set_nullable(true);
2886  null_on_null = false;
2887  return false;
2888  }
2889 };
2890 
2892  public:
2894  Item *c, Item *d)
2895  : Item_int_func(pos, a, b, c, d) {}
2896 
2897  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2898  longlong val_int() override;
2899 
2900  const char *func_name() const override {
2901  return "internal_tablespace_maximum_size";
2902  }
2903 
2904  bool resolve_type(THD *) override {
2905  set_nullable(true);
2906  null_on_null = false;
2907  return false;
2908  }
2909 };
2910 
2912  public:
2914  Item *b, Item *c, Item *d)
2915  : Item_int_func(pos, a, b, c, d) {}
2916 
2917  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2918  longlong val_int() override;
2919 
2920  const char *func_name() const override {
2921  return "internal_tablespace_autoextend_size";
2922  }
2923 
2924  bool resolve_type(THD *) override {
2925  set_nullable(true);
2926  null_on_null = false;
2927  return false;
2928  }
2929 };
2930 
2932  public:
2934  Item *c, Item *d)
2935  : Item_int_func(pos, a, b, c, d) {}
2936 
2937  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2938  longlong val_int() override;
2939 
2940  const char *func_name() const override {
2941  return "internal_tablespace_version";
2942  }
2943 
2944  bool resolve_type(THD *) override {
2945  set_nullable(true);
2946  null_on_null = false;
2947  return false;
2948  }
2949 };
2950 
2952  public:
2954  Item *c, Item *d)
2955  : Item_int_func(pos, a, b, c, d) {}
2956 
2957  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2958  longlong val_int() override;
2959 
2960  const char *func_name() const override {
2961  return "internal_tablespace_data_free";
2962  }
2963 
2964  bool resolve_type(THD *) override {
2965  set_nullable(true);
2966  null_on_null = false;
2967  return false;
2968  }
2969 };
2970 
2971 /**
2972  Common class for:
2973  Item_func_get_system_var
2974  Item_func_get_user_var
2975  Item_func_set_user_var
2976 */
2977 class Item_var_func : public Item_func {
2978  public:
2980  explicit Item_var_func(const POS &pos) : Item_func(pos) {}
2981 
2982  Item_var_func(THD *thd, Item_var_func *item) : Item_func(thd, item) {}
2983 
2985  Item_var_func(const POS &pos, Item *a) : Item_func(pos, a) {}
2986 
2987  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2988  return get_date_from_non_temporal(ltime, fuzzydate);
2989  }
2990  bool get_time(MYSQL_TIME *ltime) override {
2991  return get_time_from_non_temporal(ltime);
2992  }
2993  bool check_function_as_value_generator(uchar *checker_args) override {
2995  pointer_cast<Check_function_as_value_generator_parameters *>(
2996  checker_args);
2997  func_arg->err_code = (func_arg->source == VGS_CHECK_CONSTRAINT)
2998  ? ER_CHECK_CONSTRAINT_VARIABLES
2999  : ER_DEFAULT_VAL_GENERATED_VARIABLES;
3000  return true;
3001  }
3002 };
3003 
3004 /* Handling of user definable variables */
3005 
3006 // this is needed for user_vars hash
3008  void reset_value() {
3009  m_ptr = nullptr;
3010  m_length = 0;
3011  }
3012  void set_value(char *value, size_t length) {
3013  m_ptr = value;
3014  m_length = length;
3015  }
3016 
3017  /**
3018  Position inside a user_var_entry where small values are stored:
3019  double values, longlong values and string values with length
3020  up to extra_size (should be 8 bytes on all platforms).
3021  String values with length longer than 8 are stored in a separate
3022  memory buffer, which is allocated when needed using the method realloc().
3023  */
3025  return pointer_cast<char *>(this) + ALIGN_SIZE(sizeof(user_var_entry));
3026  }
3027 
3028  /**
3029  Position inside a user_var_entry where a null-terminates array
3030  of characters representing the variable name is stored.
3031  */
3032  char *name_ptr() { return internal_buffer_ptr() + extra_size; }
3033 
3034  /**
3035  Initialize m_ptr to the internal buffer (if the value is small enough),
3036  or allocate a separate buffer.
3037  @param length - length of the value to be stored.
3038  */
3039  bool mem_realloc(size_t length);
3040 
3041  /**
3042  Check if m_ptr point to an external buffer previously alloced by realloc().
3043  @retval true - an external buffer is alloced.
3044  @retval false - m_ptr is null, or points to the internal buffer.
3045  */
3046  bool alloced() { return m_ptr && m_ptr != internal_buffer_ptr(); }
3047 
3048  /**
3049  Free the external value buffer, if it's allocated.
3050  */
3051  void free_value() {
3052  if (alloced()) my_free(m_ptr);
3053  }
3054 
3055  /**
3056  Copy the array of characters from the given name into the internal
3057  name buffer and initialize entry_name to point to it.
3058  */
3060  name.strcpy(name_ptr());
3061  entry_name = Name_string(name_ptr(), name.length());
3062  }
3063 
3064  /**
3065  Initialize all members
3066 
3067  @param thd Current session.
3068  @param name Name of the user_var_entry instance.
3069  @param cs charset information of the user_var_entry instance.
3070  */
3071  void init(THD *thd, const Simple_cstring &name, const CHARSET_INFO *cs);
3072 
3073  /**
3074  Store a value of the given type into a user_var_entry instance.
3075  @param from Value
3076  @param length Size of the value
3077  @param type type
3078  @retval false on success
3079  @retval true on memory allocation error
3080  */
3081  bool store(const void *from, size_t length, Item_result type);
3082 
3083  /**
3084  Assert the user variable is locked.
3085  This is debug code only.
3086  The thread LOCK_thd_data mutex protects:
3087  - the thd->user_vars hash itself
3088  - the values in the user variable itself.
3089  The protection is required for monitoring,
3090  as a different thread can inspect this session
3091  user variables, on a live session.
3092  */
3093  void assert_locked() const;
3094 
3095  static const size_t extra_size = sizeof(double);
3096  char *m_ptr; ///< Value
3097  size_t m_length; ///< Value length
3098  Item_result m_type; ///< Value type
3100  /**
3101  Set to the id of the most recent query that has used the variable.
3102  Used in binlogging: When set, there is no need to add a reference to this
3103  variable to the binlog. Imagine it is this:
3104 
3105  INSERT INTO t SELECT @a:=10, @a:=@a+1.
3106 
3107  Then we have a Item_func_get_user_var (because of the `@a+1`) so we
3108  think we have to write the value of `@a` to the binlog. But before that,
3109  we have a Item_func_set_user_var to create `@a` (`@a:=10`), in this we mark
3110  the variable as "already logged" so that it won't be logged
3111  by Item_func_get_user_var (because that's not necessary).
3112  */
3114 
3115  public:
3116  user_var_entry() = default; /* Remove gcc warning */
3117 
3118  THD *owner_session() const { return m_owner; }
3119 
3120  Simple_cstring entry_name; // Variable name
3121  DTCollation collation; // Collation with attributes
3122  bool unsigned_flag; // true if unsigned, false if signed
3123 
3124  /**
3125  Set value to user variable.
3126 
3127  @param ptr pointer to buffer with new value
3128  @param length length of new value
3129  @param type type of new value
3130  @param cs charset info for new value
3131  @param dv derivation for new value
3132  @param unsigned_arg indiates if a value of type INT_RESULT is unsigned
3133 
3134  @note Sets error and fatal error if allocation fails.
3135 
3136  @retval
3137  false success
3138  @retval
3139  true failure
3140  */
3141  bool store(const void *ptr, size_t length, Item_result type,
3142  const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
3143  /**
3144  Set type of to the given value.
3145  @param type Data type.
3146  */
3148  assert_locked();
3149  m_type = type;
3150  }
3151  /**
3152  Set value to NULL
3153  @param type Data type.
3154  */
3155 
3157  assert_locked();
3158  free_value();
3159  reset_value();
3160  m_type = type;
3161  }
3162 
3163  void set_used_query_id(query_id_t query_id) { m_used_query_id = query_id; }
3165 
3166  /**
3167  Allocates and initializes a user variable instance.
3168 
3169  @param thd Current session.
3170  @param name Name of the variable.
3171  @param cs Charset of the variable.
3172 
3173  @return Address of the allocated and initialized user_var_entry instance.
3174  @retval NULL On allocation error.
3175  */
3176  static user_var_entry *create(THD *thd, const Name_string &name,
3177  const CHARSET_INFO *cs);
3178 
3179  /**
3180  Free all memory used by a user_var_entry instance
3181  previously created by create().
3182  */
3183  void destroy() {
3184  assert_locked();
3185  free_value(); // Free the external value buffer
3186  my_free(this); // Free the instance itself
3187  }
3188 
3189  void lock();
3190  void unlock();
3191 
3192  /* Routines to access the value and its type */
3193  const char *ptr() const { return m_ptr; }
3194  size_t length() const { return m_length; }
3195  Item_result type() const { return m_type; }
3196  /* Item-alike routines to access the value */
3197  double val_real(bool *null_value) const;
3198  longlong val_int(bool *null_value) const;
3199  String *val_str(bool *null_value, String *str, uint decimals) const;
3200  my_decimal *val_decimal(bool *null_value, my_decimal *result) const;
3201 };
3202 
3203 /**
3204  This class is used to implement operations like
3205  SET \@variable or \@variable:= expression.
3206 */
3207 
3214  union {
3216  double vreal;
3220 
3221  public:
3222  Name_string name; // keep it public
3223 
3226  : Item_var_func(pos, b), name(a) {}
3227 
3229  : Item_var_func(thd, item),
3231  entry(item->entry),
3232  value(item->value),
3233  decimal_buff(item->decimal_buff),
3234  null_item(item->null_item),
3235  save_result(item->save_result),
3236  name(item->name) {}
3237  enum Functype functype() const override { return SUSERVAR_FUNC; }
3238  double val_real() override;
3239  longlong val_int() override;
3240  String *val_str(String *str) override;
3241  my_decimal *val_decimal(my_decimal *) override;
3242  bool update_hash(const void *ptr, uint length, enum Item_result type,
3243  const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
3244  bool send(Protocol *protocol, String *str_arg) override;
3245  void make_field(Send_field *tmp_field) override;
3246  bool check(bool use_result_field);
3247  void save_item_result(Item *item);
3248  bool update();
3249  enum Item_result result_type() const override { return cached_result_type; }
3250  bool fix_fields(THD *thd, Item **ref) override;
3251  bool resolve_type(THD *) override;
3252  void print(const THD *thd, String *str,
3253  enum_query_type query_type) const override;
3254  void print_assignment(const THD *thd, String *str,
3255  enum_query_type query_type) const;
3256  const char *func_name() const override { return "set_user_var"; }
3257 
3258  type_conversion_status save_in_field(Field *field, bool no_conversions,
3259  bool can_use_result_field);
3260 
3261  void save_org_in_field(Field *field) override {
3262  save_in_field(field, true, false);
3263  }
3264 
3265  bool set_entry(THD *thd, bool create_if_not_exists);
3266  void cleanup() override;
3267 
3268  protected:
3270  bool no_conversions) override {
3271  return save_in_field(field, no_conversions, true);
3272  }
3273 };
3274 
3276  private Settable_routine_parameter {
3279 
3280  public:
3281  Name_string name; // keep it public
3282 
3287 
3288  enum Functype functype() const override { return GUSERVAR_FUNC; }
3289  double val_real() override;
3290  longlong val_int() override;
3291  my_decimal *val_decimal(my_decimal *) override;
3292  String *val_str(String *str) override;
3293  const CHARSET_INFO *charset_for_protocol() override;
3294  bool resolve_type(THD *) override;
3295  bool propagate_type(THD *thd, const Type_properties &type) override;
3296  void cleanup() override;
3297  void update_used_tables() override {} // Keep existing used tables
3298  void print(const THD *thd, String *str,
3299  enum_query_type query_type) const override;
3300  enum Item_result result_type() const override;
3301  /*
3302  We must always return variables as strings to guard against selects of type
3303  select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
3304  */
3305  const char *func_name() const override { return "get_user_var"; }
3306  bool is_non_const_over_literals(uchar *) override { return true; }
3307  bool eq(const Item *item, bool binary_cmp) const override;
3308 
3309  private:
3310  bool set_value(THD *thd, sp_rcontext *ctx, Item **it) override;
3311 
3312  public:
3314  return this;
3315  }
3316 };
3317 
3318 /*
3319  This item represents user variable used as out parameter (e.g in LOAD DATA),
3320  and it is supposed to be used only for this purprose. So it is simplified
3321  a lot. Actually you should never obtain its value.
3322 
3323  The only two reasons for this thing being an Item is possibility to store it
3324  in const mem_root_deque<Item> and desire to place this code somewhere near
3325  other functions working with user variables.
3326 */
3330 
3331  public:
3333  : Item(pos), name(a) {
3334  item_name.copy(a);
3335  }
3336  /* We should return something different from FIELD_ITEM here */
3337  enum Type type() const override { return STRING_ITEM; }
3338  double val_real() override;
3339  longlong val_int() override;
3340  String *val_str(String *str) override;
3341  my_decimal *val_decimal(my_decimal *decimal_buffer) override;
3343  assert(0);
3344  return true;
3345  }
3346  bool get_time(MYSQL_TIME *) override {
3347  assert(0);
3348  return true;
3349  }
3350 
3351  /* fix_fields() binds variable name with its entry structure */
3352  bool fix_fields(THD *thd, Item **ref) override;
3353  void print(const THD *thd, String *str,
3354  enum_query_type query_type) const override;
3355  void set_null_value(const CHARSET_INFO *cs);
3356  void set_value(const char *str, size_t length, const CHARSET_INFO *cs);
3357 };
3358 
3359 /* A system variable */
3360 
3361 #define GET_SYS_VAR_CACHE_LONG 1
3362 #define GET_SYS_VAR_CACHE_DOUBLE 2
3363 #define GET_SYS_VAR_CACHE_STRING 4
3364 
3366 
3367 /** Class to log audit event MYSQL_AUDIT_GLOBAL_VARIABLE_GET. */
3369  public:
3371  uchar cache_type);
3373 
3374  private:
3375  // Thread handle.
3377 
3378  // Item_func_get_system_var instance.
3380 
3381  /*
3382  Value conversion type.
3383  Depending on the value conversion type GET_SYS_VAR_CACHE_* is stored in this
3384  member while creating the object. While converting value if there are any
3385  intermediate conversions in the same query then this member is used to avoid
3386  auditing more than once.
3387  */
3389 
3390  /*
3391  To indicate event auditing is required or not. Event is not audited if
3392  * scope of the variable is *not* GLOBAL.
3393  * or the event is already audited for global variable for the same query.
3394  */
3396 };
3397 
3403  double cached_dval;
3409 
3410  template <typename T>
3412 
3414 
3415  public:
3417  LEX_STRING *component_arg, const char *name_arg,
3418  size_t name_len_arg);
3419  enum Functype functype() const override { return GSYSVAR_FUNC; }
3421  return INNER_TABLE_BIT;
3422  }
3423  bool resolve_type(THD *) override;
3424  void print(const THD *thd, String *str,
3425  enum_query_type query_type) const override;
3426  bool is_non_const_over_literals(uchar *) override { return true; }
3427  enum Item_result result_type() const override;
3428  double val_real() override;
3429  longlong val_int() override;
3430  String *val_str(String *) override;
3431  my_decimal *val_decimal(my_decimal *dec_buf) override {
3432  return val_decimal_from_real(dec_buf);
3433  }
3434  /* TODO: fix to support views */
3435  const char *func_name() const override { return "get_system_var"; }
3436  bool eq(const Item *item, bool binary_cmp) const override;
3437 
3438  void cleanup() override;
3439  bool bind(THD *thd);
3440 };
3441 
3442 class JOIN;
3443 
3444 class Item_func_match final : public Item_real_func {
3446 
3447  public:
3450  /// True if we are doing a full-text index scan with this MATCH function as a
3451  /// predicate, and the score can be retrieved with get_relevance(). If it is
3452  /// false, the score of the document must be retrieved with find_relevance().
3457  /**
3458  Master item means that if identical items are present in the
3459  statement, they use the same FT handler. FT handler is initialized
3460  only for master item and slave items just use it. FT hints initialized
3461  for master only, slave items HINTS are not accessed.
3462  */
3464  Item *concat_ws; // Item_func_concat_ws
3465  String value; // value of concat_ws
3466  String search_value; // key_item()'s value converted to cmp_collation
3467 
3468  /**
3469  Constructor for Item_func_match class.
3470 
3471  @param pos Position of token in the parser.
3472  @param a List of arguments.
3473  @param against_arg Expression to match against.
3474  @param b FT Flags.
3475  */
3476  Item_func_match(const POS &pos, PT_item_list *a, Item *against_arg, uint b)
3477  : Item_real_func(pos, a),
3478  against(against_arg),
3479  key(0),
3480  flags(b),
3482  table_ref(nullptr),
3483  master(nullptr),
3484  concat_ws(nullptr),
3485  hints(nullptr),
3486  simple_expression(false),
3487  used_in_where_only(false) {
3488  null_on_null = false;
3489  }
3490 
3491  bool itemize(Parse_context *pc, Item **res) override;
3492 
3493  void cleanup() override {
3494  DBUG_TRACE;
3496  if (master == nullptr && ft_handler != nullptr) {
3498  }
3499  score_from_index_scan = false;
3500  ft_handler = nullptr;
3501  concat_ws = nullptr;
3502  return;
3503  }
3504  Item *key_item() const override { return against; }
3505  enum Functype functype() const override { return FT_FUNC; }
3506  const char *func_name() const override { return "match"; }
3507  bool fix_fields(THD *thd, Item **ref) override;
3508  bool eq(const Item *, bool binary_cmp) const override;
3509  /* The following should be safe, even if we compare doubles */
3510  longlong val_int() override {
3511  assert(fixed);
3512  return val_real() != 0.0;
3513  }
3514  double val_real() override;
3515  void print(const THD *thd, String *str,
3516  enum_query_type query_type) const override;
3517 
3518  bool fix_index(const THD *thd);
3519  bool init_search(THD *thd);
3520  bool check_function_as_value_generator(uchar *checker_args) override {
3522  pointer_cast<Check_function_as_value_generator_parameters *>(
3523  checker_args);
3524  func_arg->banned_function_name = func_name();
3525  return true;
3526  }
3527 
3528  /**
3529  Get number of matching rows from FT handler.
3530 
3531  @note Requires that FT handler supports the extended API
3532 
3533  @return Number of matching rows in result
3534  */
3536  assert(ft_handler);
3538 
3539  return ((FT_INFO_EXT *)ft_handler)
3540  ->could_you->count_matches((FT_INFO_EXT *)ft_handler);
3541  }
3542 
3543  /**
3544  Check whether FT result is ordered on rank
3545 
3546  @return true if result is ordered
3547  @return false otherwise
3548  */
3550  assert(!master);
3551  if (hints->get_flags() & FT_SORTED) return true;
3552 
3554  return false;
3555 
3556  assert(ft_handler);
3557  return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3559  }
3560 
3561  /**
3562  Check whether FT result contains the document ID
3563 
3564  @return true if document ID is available
3565  @return false otherwise
3566  */
3568  assert(ft_handler);
3569 
3571  return false;
3572 
3573  return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3575  }
3576 
3577  float get_filtering_effect(THD *thd, table_map filter_for_table,
3578  table_map read_tables,
3579  const MY_BITMAP *fields_to_ignore,
3580  double rows_in_table) override;
3581 
3582  /**
3583  Returns master MATCH function.
3584 
3585  @return pointer to master MATCH function.
3586  */
3588  if (master) return master->get_master();
3589  return this;
3590  }
3591 
3592  /**
3593  Set master MATCH function and adjust used_in_where_only value.
3594 
3595  @param item item for which master should be set.
3596  */
3599  item->master = this;
3600  }
3601 
3602  /**
3603  Returns pointer to Ft_hints object belonging to master MATCH function.
3604 
3605  @return pointer to Ft_hints object
3606  */
3608  assert(!master);
3609  return hints;
3610  }
3611 
3612  /**
3613  Set comparison operation type and and value for master MATCH function.
3614 
3615  @param type comparison operation type
3616  @param value_arg comparison operation value
3617  */
3618  void set_hints_op(enum ft_operation type, double value_arg) {
3619  assert(!master);
3620  hints->set_hint_op(type, value_arg);
3621  }
3622 
3623  /**
3624  Set FT hints.
3625  */
3626  void set_hints(JOIN *join, uint ft_flag, ha_rows ft_limit, bool no_cond);
3627 
3628  /**
3629  Check if ranking is not needed.
3630 
3631  @return true if ranking is not needed
3632  @return false otherwise
3633  */
3635  assert(!master);
3636  return (!(hints->get_flags() & FT_SORTED) && // FT_SORTED is no set
3637  used_in_where_only && // MATCH result is not used
3638  // in expression
3639  hints->get_op_type() == FT_OP_NO); // MATCH is single function
3640  }
3641 
3642  /**
3643  Set flag that the function is a simple expression.
3644 
3645  @param val true if the function is a simple expression, false otherwise
3646  */
3647  void set_simple_expression(bool val) {
3648  assert(!master);
3649  simple_expression = val;
3650  }
3651 
3652  /**
3653  Check if this MATCH function is a simple expression in WHERE condition.
3654 
3655  @return true if simple expression
3656  @return false otherwise
3657  */
3659  assert(!master);
3660  return simple_expression;
3661  }
3662 
3663  private:
3664  /**
3665  Fulltext index hints, initialized for master MATCH function only.
3666  */
3668  /**
3669  Flag is true when MATCH function is used as a simple expression in
3670  WHERE condition, i.e. there is no AND/OR combinations, just simple
3671  MATCH function or [MATCH, rank] comparison operation.
3672  */
3674  /**
3675  true if MATCH function is used in WHERE condition only.
3676  Used to dermine what hints can be used for FT handler.
3677  Note that only master MATCH function has valid value.
3678  it's ok since only master function is involved in the hint processing.
3679  */
3681  /**
3682  Check whether storage engine for given table,
3683  allows FTS Boolean search on non-indexed columns.
3684 
3685  @todo A flag should be added to the extended fulltext API so that
3686  it may be checked whether search on non-indexed columns are
3687  supported. Currently, it is not possible to check for such a
3688  flag since @c this->ft_handler is not yet set when this function is
3689  called. The current hack is to assume that search on non-indexed
3690  columns are supported for engines that does not support the extended
3691  fulltext API (e.g., MyISAM), while it is not supported for other
3692  engines (e.g., InnoDB)
3693 
3694  @param tr Table for which storage engine to check
3695 
3696  @retval true if BOOLEAN search on non-indexed columns is supported
3697  @retval false otherwise
3698  */
3700  // Only Boolean search may support non_indexed columns
3701  if (!(flags & FT_BOOL)) return false;
3702 
3703  assert(tr && tr->file);
3704 
3705  // Assume that if extended fulltext API is not supported,
3706  // non-indexed columns are allowed. This will be true for MyISAM.
3707  if ((tr->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0) return true;
3708 
3709  return false;
3710  }
3711 };
3712 
3715 
3717 
3718  public:
3720 
3721  bool itemize(Parse_context *pc, Item **res) override;
3722  longlong val_int() override;
3723  const char *func_name() const override { return "is_free_lock"; }
3724  bool resolve_type(THD *thd) override {
3725  if (param_type_is_default(thd, 0, 1)) return true;
3726  max_length = 1;
3727  set_nullable(true);
3728  return false;
3729  }
3730  bool is_non_const_over_literals(uchar *) override { return true; }
3731  bool check_function_as_value_generator(uchar *checker_args) override {
3733  pointer_cast<Check_function_as_value_generator_parameters *>(
3734  checker_args);
3735  func_arg->banned_function_name = func_name();
3736  return true;
3737  }
3738 };
3739 
3742 
3744 
3745  public:
3747 
3748  bool itemize(Parse_context *pc, Item **res) override;
3749  longlong val_int() override;
3750  const char *func_name() const override { return "is_used_lock"; }
3751  bool resolve_type(THD *thd) override {
3752  if (param_type_is_default(thd, 0, 1)) return true;
3753  unsigned_flag = true;
3754  set_nullable(true);
3755  return false;
3756  }
3757  bool is_non_const_over_literals(uchar *) override { return true; }
3758  bool check_function_as_value_generator(uchar *checker_args) override {
3760  pointer_cast<Check_function_as_value_generator_parameters *>(
3761  checker_args);
3762  func_arg->banned_function_name = func_name();
3763  return true;
3764  }
3765 };
3766 
3767 class Item_func_row_count final : public Item_int_func {
3769 
3770  public:
3772 
3773  bool itemize(Parse_context *pc, Item **res) override;
3774 
3775  longlong val_int() override;
3776  const char *func_name() const override { return "row_count"; }
3777  bool resolve_type(THD *) override {
3778  set_nullable(false);
3779  return false;
3780  }
3781  bool check_function_as_value_generator(uchar *checker_args) override {
3783  pointer_cast<Check_function_as_value_generator_parameters *>(
3784  checker_args);
3785  func_arg->banned_function_name = func_name();
3786  return true;
3787  }
3788 };
3789 
3790 /*
3791  *
3792  * Stored FUNCTIONs
3793  *
3794  */
3795 
3796 class sp_head;
3797 class sp_name;
3798 
3799 class Item_func_sp final : public Item_func {
3800  typedef Item_func super;
3801 
3802  private:
3804  /// The name of the stored function
3805  sp_name *m_name{nullptr};
3806  /// Pointer to actual function instance (null when not resolved or executing)
3807  sp_head *m_sp{nullptr};
3808  /// The result field of the concrete stored function.
3810  /// @returns true when function execution is deterministic
3811  bool m_deterministic{false};
3812 
3813  bool execute();
3814  bool execute_impl(THD *thd);
3815  bool init_result_field(THD *thd);
3816 
3817  protected:
3818  bool is_expensive_processor(uchar *) override { return true; }
3819 
3820  public:
3821  Item_func_sp(const POS &pos, const LEX_STRING &db_name,
3822  const LEX_STRING &fn_name, bool use_explicit_name,
3823  PT_item_list *opt_list);
3824 
3825  bool itemize(Parse_context *pc, Item **res) override;
3826  /**
3827  Must not be called before the procedure is resolved,
3828  i.e. @c init_result_field().
3829  */
3830  table_map get_initial_pseudo_tables() const override;
3831  void update_used_tables() override;
3832  void fix_after_pullout(Query_block *parent_query_block,
3833  Query_block *removed_query_block) override;
3834  void cleanup() override;
3835 
3836  const char *func_name() const override;
3837 
3838  Field *tmp_table_field(TABLE *t_arg) override;
3839 
3840  void make_field(Send_field *tmp_field) override;
3841 
3842  Item_result result_type() const override;
3843 
3844  longlong val_int() override {
3845  if (execute() || null_value) return (longlong)0;
3846  return sp_result_field->val_int();
3847  }
3848 
3849  double val_real() override {
3850  if (execute() || null_value) return 0.0;
3851  return sp_result_field->val_real();
3852  }
3853 
3854  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
3855  if (execute() || null_value) return true;
3856  return sp_result_field->get_date(ltime, fuzzydate);
3857  }
3858 
3859  bool get_time(MYSQL_TIME *ltime) override {
3860  if (execute() || null_value) return true;
3861 
3862  return sp_result_field->get_time(ltime);
3863  }
3864 
3865  my_decimal *val_decimal(my_decimal *dec_buf) override {
3866  if (execute() || null_value) return nullptr;
3867  return sp_result_field->val_decimal(dec_buf);
3868  }
3869 
3870  String *val_str(String *str) override {
3871  String buf;
3872  char buff[20];
3873  buf.set(buff, 20, str->charset());
3874  buf.length(0);
3875  if (execute() || null_value) return nullptr;
3876  /*
3877  result_field will set buf pointing to internal buffer
3878  of the resul_field. Due to this it will change any time
3879  when SP is executed. In order to prevent occasional
3880  corruption of returned value, we make here a copy.
3881  */
3883  str->copy(buf);
3884  return str;
3885  }
3886 
3887  bool val_json(Json_wrapper *result) override;
3888 
3889  bool change_context_processor(uchar *arg) override {
3890  context = reinterpret_cast<Item_ident::Change_context *>(arg)->m_context;
3891  return false;
3892  }
3893 
3894  bool sp_check_access(THD *thd);
3895  enum Functype functype() const override { return FUNC_SP; }
3896 
3897  bool fix_fields(THD *thd, Item **ref) override;
3898  bool resolve_type(THD *thd) override;
3899 
3900  bool is_expensive() override { return true; }
3901 
3903  bool check_function_as_value_generator(uchar *checker_args) override {
3905  pointer_cast<Check_function_as_value_generator_parameters *>(
3906  checker_args);
3907  func_arg->banned_function_name = func_name();
3908  return true;
3909  }
3910 };
3911 
3912 class Item_func_found_rows final : public Item_int_func {
3914 
3915  public:
3917 
3918  bool itemize(Parse_context *pc, Item **res) override;
3919  longlong val_int() override;
3920  const char *func_name() const override { return "found_rows"; }
3921  bool resolve_type(THD *) override {
3922  set_nullable(false);
3923  return false;
3924  }
3925  bool check_function_as_value_generator(uchar *checker_args) override {
3927  pointer_cast<Check_function_as_value_generator_parameters *>(
3928  checker_args);
3929  func_arg->banned_function_name = func_name();
3930  return true;
3931  }
3932 };
3933 
3934 void uuid_short_init();
3935 
3936 class Item_func_uuid_short final : public Item_int_func {
3938 
3939  public:
3941 
3942  bool itemize(Parse_context *pc, Item **res) override;
3943  const char *func_name() const override { return "uuid_short"; }
3944  longlong val_int() override;
3945  bool resolve_type(THD *) override {
3946  unsigned_flag = true;
3947  return false;
3948  }
3949  bool check_partition_func_processor(uchar *) override { return false; }
3950  bool check_function_as_value_generator(uchar *checker_args) override {
3952  pointer_cast<Check_function_as_value_generator_parameters *>(
3953  checker_args);
3954  func_arg->banned_function_name = func_name();
3955  return ((func_arg->source == VGS_GENERATED_COLUMN) ||
3956  (func_arg->source == VGS_CHECK_CONSTRAINT));
3957  }
3958 };
3959 
3962 
3963  public:
3964  explicit Item_func_version(const POS &pos);
3965 
3966  bool itemize(Parse_context *pc, Item **res) override;
3967 };
3968 
3969 /**
3970  Internal function used by INFORMATION_SCHEMA implementation to check
3971  if a role is a mandatory role.
3972 */
3973 
3975  public:
3977  : Item_int_func(pos, a, b) {}
3978  longlong val_int() override;
3979  const char *func_name() const override {
3980  return "internal_is_mandatory_role";
3981  }
3982  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
3983  bool resolve_type(THD *) override {
3984  set_nullable(true);
3985  return false;
3986  }
3987 };
3988 
3989 /**
3990  Internal function used by INFORMATION_SCHEMA implementation to check
3991  if a role is enabled.
3992 */
3993 
3995  public:
3997  : Item_int_func(pos, a, b) {}
3998  longlong val_int() override;
3999  const char *func_name() const override { return "internal_is_enabled_role"; }
4000  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
4001  bool resolve_type(THD *) override {
4002  set_nullable(true);
4003  return false;
4004  }
4005 };
4006 
4008  LEX_STRING component, bool unsafe_for_replication);
4009 extern bool check_reserved_words(const char *name);
4010 extern enum_field_types agg_field_type(Item **items, uint nitems);
4011 double my_double_round(double value, longlong dec, bool dec_unsigned,
4012  bool truncate);
4013 bool eval_const_cond(THD *thd, Item *cond, bool *value);
4014 Item_field *get_gc_for_expr(const Item *func, Field *fld, Item_result type,
4015  Field **found = nullptr);
4016 
4017 void retrieve_tablespace_statistics(THD *thd, Item **args, bool *null_value);
4018 
4019 extern bool volatile mqh_used;
4020 
4021 #endif /* ITEM_FUNC_INCLUDED */
int64 query_id_t
Definition: binlog.h:70
Class to log audit event MYSQL_AUDIT_GLOBAL_VARIABLE_GET.
Definition: item_func.h:3368
Audit_global_variable_get_event(THD *thd, Item_func_get_system_var *item, uchar cache_type)
Definition: item_func.cc:7194
THD * m_thd
Definition: item_func.h:3376
~Audit_global_variable_get_event()
Definition: item_func.cc:7208
bool m_audit_event
Definition: item_func.h:3395
Item_func_get_system_var * m_item
Definition: item_func.h:3379
uchar m_val_type
Definition: item_func.h:3388
Definition: item.h:172
void set_numeric()
Definition: item.h:208
const CHARSET_INFO * collation
Definition: item.h:174
Definition: field.h:590
virtual my_decimal * val_decimal(my_decimal *) const =0
virtual double val_real() const =0
String * val_str(String *str) const
Definition: field.h:1015
virtual longlong val_int() const =0
virtual bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const
Definition: field.cc:2097
virtual bool get_time(MYSQL_TIME *ltime) const
Definition: field.cc:2104
Wrapper for struct ft_hints.
Definition: handler.h:3683
uint get_flags() const
Get Ft_hints flags.
Definition: handler.h:3746
enum ft_operation get_op_type() const
Get Ft_hints operation type.
Definition: handler.h:3739
void set_hint_op(enum ft_operation type, double value)
Set comparison operation type and and value for master MATCH function.
Definition: handler.h:3701
Definition: item_func.h:1210
Item_dec_func(const POS &pos, Item *a)
Definition: item_func.h:1213
Item_dec_func(const POS &pos, Item *a, Item *b)
Definition: item_func.h:1215
Item_dec_func(Item *a)
Definition: item_func.h:1212
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_func.cc:2708
Definition: item.h:4027
Definition: item_func.h:1195
enum Functype functype() const override
Definition: item_func.h:1205
double real_op() override
Performs the operation that