MySQL  8.0.17
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, 2019, Oracle and/or its affiliates. All rights reserved.
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 <limits.h>
27 #include <stddef.h>
28 #include <sys/types.h>
29 #include <cmath> // isfinite
30 #include <functional>
31 
32 #include "decimal.h"
33 #include "ft_global.h"
34 #include "lex_string.h"
35 #include "m_ctype.h"
36 #include "my_alloc.h"
37 #include "my_base.h"
38 #include "my_compiler.h"
39 #include "my_dbug.h"
40 #include "my_inttypes.h"
41 #include "my_pointer_arithmetic.h"
42 #include "my_sys.h"
43 #include "my_table_map.h"
44 #include "my_thread_local.h"
45 #include "my_time.h"
48 #include "mysql_com.h"
49 #include "mysql_time.h"
50 #include "mysqld_error.h"
51 #include "sql/enum_query_type.h"
52 #include "sql/field.h"
53 #include "sql/handler.h"
54 #include "sql/item.h" // Item_result_field
55 #include "sql/my_decimal.h" // string2my_decimal
57 #include "sql/set_var.h" // enum_var_type
58 #include "sql/sql_const.h"
59 #include "sql/sql_udf.h" // udf_handler
60 #include "sql/table.h"
61 #include "sql/thr_malloc.h"
62 #include "sql_string.h"
63 
64 class Json_wrapper;
65 class PT_item_list;
66 class Protocol;
67 class SELECT_LEX;
68 class THD;
69 class sp_rcontext;
70 struct MY_BITMAP;
71 template <class T>
72 class List;
73 
74 /* Function items used by mysql */
75 
76 extern bool reject_geometry_args(uint arg_count, Item **args,
77  Item_result_field *me);
78 void unsupported_json_comparison(size_t arg_count, Item **args,
79  const char *msg);
80 
81 class Item_func : public Item_result_field {
83 
84  protected:
85  Item **args, *tmp_arg[2];
86  /**
87  Affects how to determine that NULL argument implies a NULL function return.
88  Default behaviour in this class is:
89  - if true, any NULL argument means the function returns NULL.
90  - if false, no such assumption is made and not_null_tables_cache is thus
91  set to 0.
92  null_on_null is true for all Item_func derived classes, except Item_func_sp,
93  all CASE derived functions and a few other functions.
94  RETURNS NULL ON NULL INPUT can be implemented for stored functions by
95  modifying this member in class Item_func_sp.
96  */
97  bool null_on_null = true;
98  /*
99  Allowed numbers of columns in result (usually 1, which means scalar value)
100  0 means get this number from first argument
101  */
103  /// Value used in calculation of result of used_tables()
105  /// Value used in calculation of result of not_null_tables()
107 
108  public:
110  /*
111  When updating Functype with new spatial functions,
112  is_spatial_operator() should also be updated.
113 
114  DD_INTERNAL_FUNC:
115  Some of the internal functions introduced for the INFORMATION_SCHEMA views
116  opens data-dictionary tables. DD_INTERNAL_FUNC is used for the such type
117  of functions.
118  */
119  enum Functype {
212  };
219  };
220  enum Type type() const override { return FUNC_ITEM; }
221  virtual enum Functype functype() const { return UNKNOWN_FUNC; }
223 
224  explicit Item_func(const POS &pos) : super(pos), arg_count(0) {
225  args = tmp_arg;
226  }
227 
229  args = tmp_arg;
230  args[0] = a;
232  }
233  Item_func(const POS &pos, Item *a) : super(pos), arg_count(1) {
234  args = tmp_arg;
235  args[0] = a;
236  }
237 
238  Item_func(Item *a, Item *b) : arg_count(2) {
239  args = tmp_arg;
240  args[0] = a;
241  args[1] = b;
242  m_accum_properties = 0;
245  }
246  Item_func(const POS &pos, Item *a, Item *b) : super(pos), arg_count(2) {
247  args = tmp_arg;
248  args[0] = a;
249  args[1] = b;
250  }
251 
252  Item_func(Item *a, Item *b, Item *c) : arg_count(3) {
253  if ((args = (Item **)(*THR_MALLOC)->Alloc(sizeof(Item *) * 3))) {
254  args[0] = a;
255  args[1] = b;
256  args[2] = c;
257  m_accum_properties = 0;
261  } else
262  arg_count = 0; // OOM
263  }
264 
265  Item_func(const POS &pos, Item *a, Item *b, Item *c)
266  : super(pos), arg_count(3) {
267  if ((args = (Item **)(*THR_MALLOC)->Alloc(sizeof(Item *) * 3))) {
268  args[0] = a;
269  args[1] = b;
270  args[2] = c;
271  } else
272  arg_count = 0; // OOM
273  }
274 
275  Item_func(Item *a, Item *b, Item *c, Item *d) : arg_count(4) {
276  if ((args = (Item **)(*THR_MALLOC)->Alloc(sizeof(Item *) * 4))) {
277  args[0] = a;
278  args[1] = b;
279  args[2] = c;
280  args[3] = d;
281  m_accum_properties = 0;
286  } else
287  arg_count = 0; // OOM
288  }
289  Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
290  : super(pos), arg_count(4) {
291  if ((args = (Item **)(*THR_MALLOC)->Alloc(sizeof(Item *) * 4))) {
292  args[0] = a;
293  args[1] = b;
294  args[2] = c;
295  args[3] = d;
296  } else
297  arg_count = 0; // OOM
298  }
299 
300  Item_func(Item *a, Item *b, Item *c, Item *d, Item *e) : arg_count(5) {
301  if ((args = (Item **)(*THR_MALLOC)->Alloc(sizeof(Item *) * 5))) {
302  args[0] = a;
303  args[1] = b;
304  args[2] = c;
305  args[3] = d;
306  args[4] = e;
307  m_accum_properties = 0;
313  } else
314  arg_count = 0; // OOM
315  }
316  Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e)
317  : super(pos), arg_count(5) {
318  if ((args = (Item **)(*THR_MALLOC)->Alloc(sizeof(Item *) * 5))) {
319  args[0] = a;
320  args[1] = b;
321  args[2] = c;
322  args[3] = d;
323  args[4] = e;
324  } else
325  arg_count = 0; // OOM
326  }
328 
329  Item_func(const POS &pos, PT_item_list *opt_list);
330 
331  // Constructor used for Item_cond_and/or (see Item comment)
332  Item_func(THD *thd, Item_func *item);
333 
334  bool itemize(Parse_context *pc, Item **res) override;
335 
336  bool fix_fields(THD *, Item **ref) override;
337  bool fix_func_arg(THD *, Item **arg);
338  void fix_after_pullout(SELECT_LEX *parent_select,
339  SELECT_LEX *removed_select) override;
340  /**
341  Returns the pseudo tables depended upon in order to evaluate this
342  function expression. The default implementation returns the empty
343  set.
344  */
345  virtual table_map get_initial_pseudo_tables() const { return 0; }
346  table_map used_tables() const override { return used_tables_cache; }
347  table_map not_null_tables() const override { return not_null_tables_cache; }
348  void update_used_tables() override;
350  bool eq(const Item *item, bool binary_cmp) const override;
351  virtual optimize_type select_optimize(const THD *) { return OPTIMIZE_NONE; }
352  virtual bool have_rev_func() const { return 0; }
353  virtual Item *key_item() const { return args[0]; }
354  inline Item **arguments() const {
356  return args;
357  }
358  /**
359  Copy arguments from list to args array
360 
361  @param list function argument list
362  @param context_free true: for use in context-independent
363  constructors (Item_func(POS,...)) i.e. for use
364  in the parser
365  */
366  void set_arguments(List<Item> &list, bool context_free);
367  inline uint argument_count() const { return arg_count; }
368  void split_sum_func(THD *thd, Ref_item_array ref_item_array,
369  List<Item> &fields) override;
370  void print(const THD *thd, String *str,
371  enum_query_type query_type) const override;
372  void print_op(const THD *thd, String *str, enum_query_type query_type) const;
373  void print_args(const THD *thd, String *str, uint from,
374  enum_query_type query_type) const;
375  virtual void fix_num_length_and_dec();
376  virtual bool is_deprecated() const { return false; }
377  bool get_arg0_date(MYSQL_TIME *ltime, my_time_flags_t fuzzy_date) {
378  return (null_value = args[0]->get_date(ltime, fuzzy_date));
379  }
380  inline bool get_arg0_time(MYSQL_TIME *ltime) {
381  return (null_value = args[0]->get_time(ltime));
382  }
383  bool is_null() override {
384  /*
385  TODO : Implement error handling for this function as
386  update_null_value() can return error.
387  */
388  (void)update_null_value();
389  return null_value;
390  }
391  void signal_divide_by_null();
393  friend class udf_handler;
395  Field *tmp_table_field(TABLE *t_arg) override;
396  Item *get_tmp_table_item(THD *thd) override;
397 
398  my_decimal *val_decimal(my_decimal *) override;
399 
401  int item_sep) {
402  return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep);
403  }
404  /*
405  Aggregate arguments for string result, e.g: CONCAT(a,b)
406  - convert to @@character_set_connection if all arguments are numbers
407  - allow DERIVATION_NONE
408  */
410  uint nitems, int item_sep = 1) {
412  item_sep);
413  }
414  /*
415  Aggregate arguments for comparison, e.g: a=b, a LIKE b, a RLIKE b
416  - don't convert to @@character_set_connection if all arguments are numbers
417  - don't allow DERIVATION_NONE
418  */
420  uint nitems, int item_sep = 1) {
421  return agg_item_charsets_for_comparison(c, func_name(), items, nitems,
422  item_sep);
423  }
424  /*
425  Aggregate arguments for string result, when some comparison
426  is involved internally, e.g: REPLACE(a,b,c)
427  - convert to @@character_set_connection if all arguments are numbers
428  - disallow DERIVATION_NONE
429  */
431  Item **items,
432  uint nitems,
433  int item_sep = 1) {
435  c, func_name(), items, nitems, item_sep);
436  }
437  bool walk(Item_processor processor, enum_walk walk, uchar *arg) override;
438  Item *transform(Item_transformer transformer, uchar *arg) override;
439  Item *compile(Item_analyzer analyzer, uchar **arg_p,
440  Item_transformer transformer, uchar *arg_t) override;
441  void traverse_cond(Cond_traverser traverser, void *arg,
442  traverse_order order) override;
443 
444  /**
445  Throw an error if the input double number is not finite, i.e. is either
446  +/-INF or NAN.
447  */
448  inline double check_float_overflow(double value) {
449  return std::isfinite(value) ? value : raise_float_overflow();
450  }
451  /**
452  Throw an error if the input BIGINT value represented by the
453  (longlong value, bool unsigned flag) pair cannot be returned by the
454  function, i.e. is not compatible with this Item's unsigned_flag.
455  */
456  inline longlong check_integer_overflow(longlong value, bool val_unsigned) {
457  if ((unsigned_flag && !val_unsigned && value < 0) ||
458  (!unsigned_flag && val_unsigned &&
459  (ulonglong)value > (ulonglong)LLONG_MAX))
460  return raise_integer_overflow();
461  return value;
462  }
463  /**
464  Throw an error if the error code of a DECIMAL operation is E_DEC_OVERFLOW.
465  */
466  inline int check_decimal_overflow(int error) {
468  }
469 
472  for (uint i = 0; i < arg_count; i++) {
473  if (args[i]->type() == Item::FIELD_ITEM &&
475  return true;
476  }
477  return false;
478  }
479 
480  bool has_date_args() {
482  for (uint i = 0; i < arg_count; i++) {
483  if (args[i]->type() == Item::FIELD_ITEM &&
484  (args[i]->data_type() == MYSQL_TYPE_DATE ||
486  return true;
487  }
488  return false;
489  }
490 
491  bool has_time_args() {
493  for (uint i = 0; i < arg_count; i++) {
494  if (args[i]->type() == Item::FIELD_ITEM &&
495  (args[i]->data_type() == MYSQL_TYPE_TIME ||
497  return true;
498  }
499  return false;
500  }
501 
504  for (uint i = 0; i < arg_count; i++) {
505  if (args[i]->type() == Item::FIELD_ITEM &&
507  return true;
508  }
509  return false;
510  }
511 
512  /*
513  We assume the result of any function that has a TIMESTAMP argument to be
514  timezone-dependent, since a TIMESTAMP value in both numeric and string
515  contexts is interpreted according to the current timezone.
516  The only exception is UNIX_TIMESTAMP() which returns the internal
517  representation of a TIMESTAMP argument verbatim, and thus does not depend on
518  the timezone.
519  */
521  return has_timestamp_args();
522  }
523 
524  Item *gc_subst_transformer(uchar *arg) override;
525 
526  /**
527  Does essentially the same as THD::change_item_tree, plus
528  maintains any necessary any invariants.
529  */
530  virtual void replace_argument(THD *thd, Item **oldpp, Item *newp);
531 
532  /**
533  Whether an arg of a JSON function can be cached to avoid repetitive
534  string->JSON conversion. This function returns true only for those args,
535  which are the source of JSON data. JSON path args are cached independently
536  and for them this function returns false. Same as for all other type of
537  args.
538 
539  @param arg the arg to cache
540 
541  @returns
542  true arg can be cached
543  false otherwise
544  */
546  Item *arg MY_ATTRIBUTE((unused))) {
547  return CACHE_NONE;
548  }
549 
550  protected:
551  /**
552  Whether or not an item should contribute to the filtering effect
553  (@see get_filtering_effect()). First it verifies that table
554  requirements are satisfied as follows:
555 
556  1) The item must refer to a field in 'filter_for_table' in some
557  way. This reference may be indirect through any number of
558  intermediate items. For example, this item may be an
559  Item_cond_and which refers to an Item_func_eq which refers to
560  the field.
561  2) The item must not refer to other tables than those already
562  read and the table in 'filter_for_table'
563 
564  Then it contines to other properties as follows:
565 
566  Item_funcs represent "<operand1> OP <operand2> [OP ...]". If the
567  Item_func is to contribute to the filtering effect, then
568 
569  1) one of the operands must be a field from 'filter_for_table' that is not
570  in 'fields_to_ignore', and
571  2) depending on the Item_func type filtering effect is calculated
572  for, one or all [1] of the other operand(s) must be an available
573  value, i.e.:
574  - a constant, or
575  - a constant subquery, or
576  - a field value read from a table in 'read_tables', or
577  - a second field in 'filter_for_table', or
578  - a function that only refers to constants or tables in
579  'read_tables', or
580  - special case: an implicit value like NULL in the case of
581  "field IS NULL". Such Item_funcs have arg_count==1.
582 
583  [1] "At least one" for multiple equality (X = Y = Z = ...), "all"
584  for the rest (e.g. BETWEEN)
585 
586  @param read_tables Tables earlier in the join sequence.
587  Predicates for table 'filter_for_table' that
588  rely on values from these tables can be part of
589  the filter effect.
590  @param filter_for_table The table we are calculating filter effect for
591  @param fields_to_ignore Columns that should be ignored.
592 
593 
594  @return Item_field that participates in the predicate if none of the
595  requirements are broken, NULL otherwise
596 
597  @note: This function only applies to items doing comparison, i.e.
598  boolean predicates. Unfortunately, some of those items do not
599  inherit from Item_bool_func so the member function has to be
600  placed in Item_func.
601  */
603  table_map read_tables, table_map filter_for_table,
604  const MY_BITMAP *fields_to_ignore) const;
605  /**
606  Named parameters are allowed in a parameter list
607 
608  The syntax to name parameters in a function call is as follow:
609  <code>foo(expr AS named, expr named, expr AS "named", expr "named")</code>
610  where "AS" is optional.
611  Only UDF function support that syntax.
612 
613  @return true if the function item can have named parameters
614  */
615  virtual bool may_have_named_parameters() const { return false; }
616  virtual bool is_non_const_over_literals(uchar *) override { return false; }
617 
618  bool check_function_as_value_generator(uchar *checker_args) override {
619  if (is_deprecated()) {
622  checker_args);
623  func_arg->banned_function_name = func_name();
624  return true;
625  }
626  return false;
627  }
628 };
629 
630 class Item_real_func : public Item_func {
631  public:
633  explicit Item_real_func(const POS &pos) : Item_func(pos) {
635  }
636 
638  Item_real_func(const POS &pos, Item *a) : Item_func(pos, a) {
640  }
641 
643 
644  Item_real_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
646  }
647 
649 
652  }
653 
654  String *val_str(String *str) override;
655  my_decimal *val_decimal(my_decimal *decimal_value) override;
656  longlong val_int() override {
659  }
660  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
661  return get_date_from_real(ltime, fuzzydate);
662  }
663  bool get_time(MYSQL_TIME *ltime) override {
664  return get_time_from_real(ltime);
665  }
666  enum Item_result result_type() const override { return REAL_RESULT; }
667  bool resolve_type(THD *) override { return false; }
668 };
669 
671  protected:
673 
674  public:
677  }
681  }
682 
684  : Item_func(a, b), hybrid_type(REAL_RESULT) {
686  }
688  : Item_func(pos, a, b), hybrid_type(REAL_RESULT) {
690  }
691 
695  }
699  }
700 
701  enum Item_result result_type() const override { return hybrid_type; }
702  bool resolve_type(THD *thd) override;
703  void fix_num_length_and_dec() override;
704  virtual void set_numeric_type() = 0; // To be called from resolve_type()
705 
706  double val_real() override;
707  longlong val_int() override;
708  my_decimal *val_decimal(my_decimal *) override;
709  String *val_str(String *str) override;
710  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
711  bool get_time(MYSQL_TIME *ltime) override;
712  /**
713  @brief Performs the operation that this functions implements when the
714  result type is INT.
715 
716  @return The result of the operation.
717  */
718  virtual longlong int_op() = 0;
719 
720  /**
721  @brief Performs the operation that this functions implements when the
722  result type is REAL.
723 
724  @return The result of the operation.
725  */
726  virtual double real_op() = 0;
727 
728  /**
729  @brief Performs the operation that this functions implements when the
730  result type is DECIMAL.
731 
732  @param decimal_value A pointer where the DECIMAL value will be allocated.
733  @return
734  - 0 If the result is NULL
735  - The same pointer it was given, with the area initialized to the
736  result of the operation.
737  */
738  virtual my_decimal *decimal_op(my_decimal *decimal_value) = 0;
739 
740  /**
741  @brief Performs the operation that this functions implements when the
742  result type is a string type.
743 
744  @return The result of the operation.
745  */
746  virtual String *str_op(String *) = 0;
747  /**
748  @brief Performs the operation that this functions implements when the
749  result type is MYSQL_TYPE_DATE or MYSQL_TYPE_DATETIME.
750 
751  @return The result of the operation.
752  */
753  virtual bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) = 0;
754  virtual bool time_op(MYSQL_TIME *ltime) = 0;
755  bool is_null() override {
756  /*
757  TODO : Implement error handling for this function as
758  update_null_value() can return error.
759  */
760  (void)update_null_value();
761  return null_value;
762  }
763 };
764 
765 /* function where type of result detected by first argument */
767  public:
770 
772  Item_func_num1(const POS &pos, Item *a, Item *b)
773  : Item_func_numhybrid(pos, a, b) {}
774 
775  void fix_num_length_and_dec() override;
776  void set_numeric_type() override;
777  String *str_op(String *) override {
778  DBUG_ASSERT(0);
779  return 0;
780  }
781  bool date_op(MYSQL_TIME *, my_time_flags_t) override {
782  DBUG_ASSERT(0);
783  return 0;
784  }
785  bool time_op(MYSQL_TIME *) override {
786  DBUG_ASSERT(0);
787  return 0;
788  }
789 };
790 
791 /* Base class for operations like '+', '-', '*' */
793  public:
795  Item_num_op(const POS &pos, Item *a, Item *b)
796  : Item_func_numhybrid(pos, a, b) {}
797 
798  virtual void result_precision() = 0;
799 
800  void print(const THD *thd, String *str,
801  enum_query_type query_type) const override {
802  print_op(thd, str, query_type);
803  }
804 
805  void set_numeric_type() override;
806  String *str_op(String *) override {
807  DBUG_ASSERT(0);
808  return 0;
809  }
810  bool date_op(MYSQL_TIME *, my_time_flags_t) override {
811  DBUG_ASSERT(0);
812  return 0;
813  }
814  bool time_op(MYSQL_TIME *) override {
815  DBUG_ASSERT(0);
816  return 0;
817  }
818 };
819 
820 class Item_int_func : public Item_func {
821  public:
823  explicit Item_int_func(const POS &pos) : Item_func(pos) {
825  }
826 
828  Item_int_func(const POS &pos, Item *a) : Item_func(pos, a) {
830  }
831 
832  Item_int_func(Item *a, Item *b) : Item_func(a, b) {
834  }
835  Item_int_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
837  }
838 
839  Item_int_func(Item *a, Item *b, Item *c) : Item_func(a, b, c) {
841  }
842  Item_int_func(const POS &pos, Item *a, Item *b, Item *c)
843  : Item_func(pos, a, b, c) {
845  }
846 
847  Item_int_func(Item *a, Item *b, Item *c, Item *d) : Item_func(a, b, c, d) {
849  }
850  Item_int_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
851  : Item_func(pos, a, b, c, d) {
853  }
854 
857  }
858  Item_int_func(const POS &pos, PT_item_list *opt_list)
859  : Item_func(pos, opt_list) {
861  }
862 
863  Item_int_func(THD *thd, Item_int_func *item) : Item_func(thd, item) {
865  }
866  double val_real() override;
867  String *val_str(String *str) override;
868  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
869  return get_date_from_int(ltime, fuzzydate);
870  }
871  bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
872  enum Item_result result_type() const override { return INT_RESULT; }
873  bool resolve_type(THD *) override { return false; }
874 };
875 
878 
880 
881  public:
883 
884  bool itemize(Parse_context *pc, Item **res) override;
885  const char *func_name() const override { return "connection_id"; }
886  bool resolve_type(THD *thd) override;
887  bool fix_fields(THD *thd, Item **ref) override;
888  longlong val_int() override {
890  return value;
891  }
892  bool check_function_as_value_generator(uchar *checker_args) override {
895  checker_args);
896  func_arg->banned_function_name = func_name();
897  return ((func_arg->source == VGS_GENERATED_COLUMN) ||
898  (func_arg->source == VGS_CHECK_CONSTRAINT));
899  }
900 };
901 
903  public:
905  unsigned_flag = false;
906  }
907  const char *func_name() const override { return "cast_as_signed"; }
908  longlong val_int() override;
910  bool resolve_type(THD *thd) override;
911  void print(const THD *thd, String *str,
912  enum_query_type query_type) const override;
913  uint decimal_precision() const override {
914  return args[0]->decimal_precision();
915  }
916  enum Functype functype() const override { return TYPECAST_FUNC; }
917 };
918 
920  public:
922  : Item_typecast_signed(pos, a) {
923  unsigned_flag = true;
924  }
925  const char *func_name() const override { return "cast_as_unsigned"; }
926  longlong val_int() override;
927  void print(const THD *thd, String *str,
928  enum_query_type query_type) const override;
929  enum Functype functype() const override { return TYPECAST_FUNC; }
930 };
931 
933  public:
934  Item_typecast_decimal(const POS &pos, Item *a, int len, int dec)
935  : Item_func(pos, a) {
937  }
938  String *val_str(String *str) override;
939  double val_real() override;
940  longlong val_int() override;
941  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
942  return get_date_from_decimal(ltime, fuzzydate);
943  }
944  bool get_time(MYSQL_TIME *ltime) override {
945  return get_time_from_decimal(ltime);
946  }
947  my_decimal *val_decimal(my_decimal *) override;
948  enum Item_result result_type() const override { return DECIMAL_RESULT; }
949  bool resolve_type(THD *) override { return false; }
950  const char *func_name() const override { return "cast_as_decimal"; }
951  enum Functype functype() const override { return TYPECAST_FUNC; }
952  void print(const THD *thd, String *str,
953  enum_query_type query_type) const override;
954 };
955 
956 /**
957  Class used to implement CAST to floating-point data types.
958 */
960  public:
961  Item_typecast_real(const POS &pos, Item *a, bool as_double)
962  : Item_func(pos, a) {
963  if (as_double)
965  else
967  }
968  String *val_str(String *str) override;
969  double val_real() override;
970  longlong val_int() override;
971  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
972  bool get_time(MYSQL_TIME *ltime) override;
973  my_decimal *val_decimal(my_decimal *decimal_value) override;
974  enum Item_result result_type() const override { return REAL_RESULT; }
975  bool resolve_type(THD *) override { return false; }
976  const char *func_name() const override { return "cast_as_real"; }
977  enum Functype functype() const override { return TYPECAST_FUNC; }
978  void print(const THD *thd, String *str,
979  enum_query_type query_type) const override;
980 };
981 
983  public:
986  : Item_num_op(pos, a, b) {}
987 
988  void result_precision() override;
989  bool check_partition_func_processor(uchar *) override { return false; }
990  bool check_function_as_value_generator(uchar *) override { return false; }
991 };
992 
994  public:
996  Item_func_plus(const POS &pos, Item *a, Item *b)
997  : Item_func_additive_op(pos, a, b) {}
998 
999  const char *func_name() const override { return "+"; }
1000 
1001  // SUPPRESS_UBSAN: signed integer overflow
1002  longlong int_op() override SUPPRESS_UBSAN;
1003 
1004  double real_op() override;
1005  my_decimal *decimal_op(my_decimal *) override;
1006  enum Functype functype() const override { return PLUS_FUNC; }
1007 };
1008 
1010  public:
1012  Item_func_minus(const POS &pos, Item *a, Item *b)
1013  : Item_func_additive_op(pos, a, b) {}
1014 
1015  const char *func_name() const override { return "-"; }
1016 
1017  // SUPPRESS_UBSAN: signed integer overflow
1018  longlong int_op() override SUPPRESS_UBSAN;
1019 
1020  double real_op() override;
1021  my_decimal *decimal_op(my_decimal *) override;
1022  bool resolve_type(THD *thd) override;
1023  enum Functype functype() const override { return MINUS_FUNC; }
1024 };
1025 
1027  public:
1028  Item_func_mul(Item *a, Item *b) : Item_num_op(a, b) {}
1029  Item_func_mul(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1030 
1031  const char *func_name() const override { return "*"; }
1032  longlong int_op() override;
1033  double real_op() override;
1034  my_decimal *decimal_op(my_decimal *) override;
1035  void result_precision() override;
1036  bool check_partition_func_processor(uchar *) override { return false; }
1037  bool check_function_as_value_generator(uchar *) override { return false; }
1038  enum Functype functype() const override { return MUL_FUNC; }
1039 };
1040 
1042  public:
1044  Item_func_div(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1045  longlong int_op() override {
1046  DBUG_ASSERT(0);
1047  return 0;
1048  }
1049  double real_op() override;
1050  my_decimal *decimal_op(my_decimal *) override;
1051  const char *func_name() const override { return "/"; }
1052  bool resolve_type(THD *thd) override;
1053  void result_precision() override;
1054  enum Functype functype() const override { return DIV_FUNC; }
1055 };
1056 
1058  public:
1061  : Item_int_func(pos, a, b) {}
1062  longlong val_int() override;
1063  const char *func_name() const override { return "DIV"; }
1064  bool resolve_type(THD *thd) override;
1065 
1066  void print(const THD *thd, String *str,
1067  enum_query_type query_type) const override {
1068  print_op(thd, str, query_type);
1069  }
1070 
1071  bool check_partition_func_processor(uchar *) override { return false; }
1072  bool check_function_as_value_generator(uchar *) override { return false; }
1073 };
1074 
1076  public:
1077  Item_func_mod(Item *a, Item *b) : Item_num_op(a, b) {}
1078  Item_func_mod(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1079 
1080  longlong int_op() override;
1081  double real_op() override;
1082  my_decimal *decimal_op(my_decimal *) override;
1083  const char *func_name() const override { return "%"; }
1084  void result_precision() override;
1085  bool resolve_type(THD *thd) override;
1086  bool check_partition_func_processor(uchar *) override { return false; }
1087  bool check_function_as_value_generator(uchar *) override { return false; }
1088  enum Functype functype() const override { return MOD_FUNC; }
1089 };
1090 
1092  public:
1095 
1096  double real_op() override;
1097  longlong int_op() override;
1098  my_decimal *decimal_op(my_decimal *) override;
1099  const char *func_name() const override { return "-"; }
1100  enum Functype functype() const override { return NEG_FUNC; }
1101  bool resolve_type(THD *thd) override;
1102  void fix_num_length_and_dec() override;
1103  uint decimal_precision() const override {
1104  return args[0]->decimal_precision();
1105  }
1106  bool check_partition_func_processor(uchar *) override { return false; }
1107  bool check_function_as_value_generator(uchar *) override { return false; }
1108 };
1109 
1111  public:
1113  double real_op() override;
1114  longlong int_op() override;
1115  my_decimal *decimal_op(my_decimal *) override;
1116  const char *func_name() const override { return "abs"; }
1117  bool resolve_type(THD *) override;
1118  bool check_partition_func_processor(uchar *) override { return false; }
1119  bool check_function_as_value_generator(uchar *) override { return false; }
1120  enum Functype functype() const override { return ABS_FUNC; }
1121 };
1122 
1123 // A class to handle logarithmic and trigonometric functions
1124 
1126  public:
1129 
1130  Item_dec_func(const POS &pos, Item *a, Item *b) : Item_real_func(pos, a, b) {}
1131  bool resolve_type(THD *thd) override;
1132 };
1133 
1135  public:
1137  double val_real() override;
1138  const char *func_name() const override { return "exp"; }
1139 };
1140 
1142  public:
1143  Item_func_ln(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1144  double val_real() override;
1145  const char *func_name() const override { return "ln"; }
1146  enum Functype functype() const override { return LN_FUNC; }
1147 };
1148 
1150  public:
1152  Item_func_log(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1153  double val_real() override;
1154  const char *func_name() const override { return "log"; }
1155  enum Functype functype() const override { return LOG_FUNC; }
1156 };
1157 
1159  public:
1161  double val_real() override;
1162  const char *func_name() const override { return "log2"; }
1163 };
1164 
1166  public:
1168  double val_real() override;
1169  const char *func_name() const override { return "log10"; }
1170  enum Functype functype() const override { return LOG10_FUNC; }
1171 };
1172 
1174  public:
1176  double val_real() override;
1177  const char *func_name() const override { return "sqrt"; }
1178  enum Functype functype() const override { return SQRT_FUNC; }
1179 };
1180 
1182  public:
1183  Item_func_pow(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1184  double val_real() override;
1185  const char *func_name() const override { return "pow"; }
1186 };
1187 
1189  public:
1191  double val_real() override;
1192  const char *func_name() const override { return "acos"; }
1193  enum Functype functype() const override { return ACOS_FUNC; }
1194 };
1195 
1197  public:
1199  double val_real() override;
1200  const char *func_name() const override { return "asin"; }
1201  enum Functype functype() const override { return ASIN_FUNC; }
1202 };
1203 
1205  public:
1207  Item_func_atan(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1208  double val_real() override;
1209  const char *func_name() const override { return "atan"; }
1210  enum Functype functype() const override { return ATAN_FUNC; }
1211 };
1212 
1214  public:
1216  double val_real() override;
1217  const char *func_name() const override { return "cos"; }
1218  enum Functype functype() const override { return COS_FUNC; }
1219 };
1220 
1222  public:
1224  double val_real() override;
1225  const char *func_name() const override { return "sin"; }
1226  enum Functype functype() const override { return SIN_FUNC; }
1227 };
1228 
1230  public:
1232  double val_real() override;
1233  const char *func_name() const override { return "tan"; }
1234  enum Functype functype() const override { return TAN_FUNC; }
1235 };
1236 
1238  public:
1240  double val_real() override;
1241  const char *func_name() const override { return "cot"; }
1242 };
1243 
1245  public:
1247  bool resolve_type(THD *thd) override;
1248 };
1249 
1251  public:
1254  void fix_num_length_and_dec() override;
1255  void set_numeric_type() override;
1256 };
1257 
1259  public:
1262  const char *func_name() const override { return "ceiling"; }
1263  longlong int_op() override;
1264  double real_op() override;
1265  my_decimal *decimal_op(my_decimal *) override;
1266  bool check_partition_func_processor(uchar *) override { return false; }
1267  bool check_function_as_value_generator(uchar *) override { return false; }
1268  enum Functype functype() const override { return CEILING_FUNC; }
1269 };
1270 
1272  public:
1275  const char *func_name() const override { return "floor"; }
1276  longlong int_op() override;
1277  double real_op() override;
1278  my_decimal *decimal_op(my_decimal *) override;
1279  bool check_partition_func_processor(uchar *) override { return false; }
1280  bool check_function_as_value_generator(uchar *) override { return false; }
1281  enum Functype functype() const override { return FLOOR_FUNC; }
1282 };
1283 
1284 /* This handles round and truncate */
1285 
1287  bool truncate;
1288 
1289  public:
1290  Item_func_round(Item *a, Item *b, bool trunc_arg)
1291  : Item_func_num1(a, b), truncate(trunc_arg) {}
1292  Item_func_round(const POS &pos, Item *a, Item *b, bool trunc_arg)
1293  : Item_func_num1(pos, a, b), truncate(trunc_arg) {}
1294 
1295  const char *func_name() const override {
1296  return truncate ? "truncate" : "round";
1297  }
1298  double real_op() override;
1299  longlong int_op() override;
1300  my_decimal *decimal_op(my_decimal *) override;
1301  bool resolve_type(THD *) override;
1302 };
1303 
1306 
1308  bool first_eval; // true if val_real() is called 1st time
1309  public:
1311  : Item_real_func(pos, a), rand(0), first_eval(true) {}
1312  explicit Item_func_rand(const POS &pos) : Item_real_func(pos) {}
1313 
1314  bool itemize(Parse_context *pc, Item **res) override;
1315  double val_real() override;
1316  const char *func_name() const override { return "rand"; }
1317  /**
1318  This function is non-deterministic and hence depends on the
1319  'RAND' pseudo-table.
1320 
1321  @retval RAND_TABLE_BIT
1322  */
1324  return RAND_TABLE_BIT;
1325  }
1326  bool fix_fields(THD *thd, Item **ref) override;
1327  bool resolve_type(THD *thd) override;
1328  void cleanup() override {
1329  first_eval = true;
1331  }
1332  bool check_function_as_value_generator(uchar *checker_args) override {
1335  checker_args);
1336  func_arg->banned_function_name = func_name();
1337  return ((func_arg->source == VGS_GENERATED_COLUMN) ||
1338  (func_arg->source == VGS_CHECK_CONSTRAINT));
1339  }
1340 
1341  private:
1342  void seed_random(Item *val);
1343 };
1344 
1346  public:
1348  const char *func_name() const override { return "sign"; }
1349  longlong val_int() override;
1350  bool resolve_type(THD *thd) override;
1351 };
1352 
1354  const char *name;
1355  double mul, add;
1356 
1357  public:
1358  Item_func_units(const POS &pos, const char *name_arg, Item *a, double mul_arg,
1359  double add_arg)
1360  : Item_real_func(pos, a), name(name_arg), mul(mul_arg), add(add_arg) {}
1361  double val_real() override;
1362  const char *func_name() const override { return name; }
1363  bool resolve_type(THD *thd) override;
1364 };
1365 
1367  public:
1368  Item_func_min_max(const POS &pos, PT_item_list *opt_list, bool is_least_func)
1369  : Item_func_numhybrid(pos, opt_list),
1370  m_is_least_func(is_least_func),
1371  temporal_item(nullptr) {}
1372 
1373  longlong val_int() override;
1374  double val_real() override;
1375  my_decimal *val_decimal(my_decimal *) override;
1376  longlong int_op() override;
1377  double real_op() override;
1378  my_decimal *decimal_op(my_decimal *) override;
1379  String *str_op(String *) override;
1380  bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1381  bool time_op(MYSQL_TIME *ltime) override;
1382  bool resolve_type(THD *) override;
1383  void set_numeric_type() override {}
1384  enum Item_result result_type() const override { return hybrid_type; }
1385 
1386  /**
1387  Make CAST(LEAST_OR_GREATEST(datetime_expr, varchar_expr))
1388  return a number in format YYMMDDhhmmss.
1389  */
1390  enum Item_result cast_to_int_type() const override {
1391  return compare_as_dates() ? INT_RESULT : result_type();
1392  }
1393 
1394  /// Returns true if arguments to this function should be compared as dates.
1395  bool compare_as_dates() const {
1396  return temporal_item != nullptr &&
1398  }
1399 
1400  /// Returns true if at least one of the arguments was of temporal type.
1401  bool has_temporal_arg() const { return temporal_item; }
1402 
1403  private:
1404  /// True if LEAST function, false if GREATEST.
1405  const bool m_is_least_func;
1407  /*
1408  Used for determining whether one of the arguments is of temporal type and
1409  for converting arguments to a common output format if arguments are
1410  compared as dates and result type is character string. For example,
1411  LEAST('95-05-05', date '10-10-10') should return '1995-05-05', not
1412  '95-05-05'.
1413  */
1415  /**
1416  Compare arguments as datetime values.
1417 
1418  @param value Pointer to which the datetime value of the winning argument
1419  is written.
1420 
1421  @return true if error, false otherwise.
1422  */
1423  bool cmp_datetimes(longlong *value);
1424 
1425  /**
1426  Compare arguments as time values.
1427 
1428  @param value Pointer to which the time value of the winning argument is
1429  written.
1430 
1431  @return true if error, false otherwise.
1432  */
1433  bool cmp_times(longlong *value);
1434 };
1435 
1437  public:
1438  Item_func_min(const POS &pos, PT_item_list *opt_list)
1439  : Item_func_min_max(pos, opt_list, true) {}
1440  const char *func_name() const override { return "least"; }
1441  enum Functype functype() const override { return LEAST_FUNC; }
1442 };
1443 
1445  public:
1446  Item_func_max(const POS &pos, PT_item_list *opt_list)
1447  : Item_func_min_max(pos, opt_list, false) {}
1448  const char *func_name() const override { return "greatest"; }
1449  enum Functype functype() const override { return GREATEST_FUNC; }
1450 };
1451 
1452 /*
1453  Objects of this class are used for ROLLUP queries to wrap up
1454  each constant item referred to in GROUP BY list.
1455 
1456  Before grouping the wrapped item could be considered constant, but after
1457  grouping it is not, as rollup adds NULL values, which can affect later
1458  phases like DISTINCT or windowing.
1459 */
1460 
1462  public:
1464  double val_real() override;
1465  longlong val_int() override;
1466  String *val_str(String *str) override;
1467  my_decimal *val_decimal(my_decimal *dec) override;
1468  bool val_json(Json_wrapper *result) override;
1469  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1470  return (null_value = args[0]->get_date(ltime, fuzzydate));
1471  }
1472  bool get_time(MYSQL_TIME *ltime) override {
1473  return (null_value = args[0]->get_time(ltime));
1474  }
1475  const char *func_name() const override { return "rollup_const"; }
1476  table_map used_tables() const override {
1477  /*
1478  If underlying item is non-constant, return its used_tables value.
1479  Otherwise ensure it is non-constant by returning RAND_TABLE_BIT.
1480  */
1481  return args[0]->used_tables() ? args[0]->used_tables() : RAND_TABLE_BIT;
1482  }
1483  Item_result result_type() const override { return args[0]->result_type(); }
1484  bool resolve_type(THD *) override {
1486 
1487  // The item could be a NULL constant.
1488  null_value = args[0]->is_null();
1489  return false;
1490  }
1491 };
1492 
1495 
1496  public:
1498  longlong val_int() override;
1499  const char *func_name() const override { return "length"; }
1500  bool resolve_type(THD *) override {
1501  max_length = 10;
1502  return false;
1503  }
1504 };
1505 
1507  public:
1509  longlong val_int() override {
1510  DBUG_ASSERT(fixed);
1511  return Item_func_length::val_int() * 8;
1512  }
1513  const char *func_name() const override { return "bit_length"; }
1514 };
1515 
1518 
1519  public:
1522  longlong val_int() override;
1523  const char *func_name() const override { return "char_length"; }
1524  bool resolve_type(THD *) override {
1525  max_length = 10;
1526  return false;
1527  }
1528 };
1529 
1531  public:
1533  null_on_null = false;
1534  }
1535  longlong val_int() override;
1536  const char *func_name() const override { return "coercibility"; }
1537  bool resolve_type(THD *) override {
1538  max_length = 10;
1539  maybe_null = false;
1540  return false;
1541  }
1542 };
1543 
1547 
1548  public:
1551  : Item_int_func(pos, a, b) {}
1552  Item_func_locate(const POS &pos, Item *a, Item *b, Item *c)
1553  : Item_int_func(pos, a, b, c) {}
1554 
1555  const char *func_name() const override { return "locate"; }
1556  longlong val_int() override;
1557  bool resolve_type(THD *thd) override;
1558  void print(const THD *thd, String *str,
1559  enum_query_type query_type) const override;
1560 };
1561 
1563  public:
1564  Item_func_instr(const POS &pos, Item *a, Item *b)
1565  : Item_func_locate(pos, a, b) {}
1566 
1567  const char *func_name() const override { return "instr"; }
1568 };
1569 
1571  public:
1573  : Item_int_func(pos, a) {}
1574  longlong val_int() override;
1575  const char *func_name() const override {
1576  return "validate_password_strength";
1577  }
1578  bool resolve_type(THD *) override {
1579  max_length = 10;
1580  maybe_null = true;
1581  return false;
1582  }
1583 };
1584 
1589 
1590  public:
1592  : Item_int_func(pos, opt_list) {}
1593  longlong val_int() override;
1594  const char *func_name() const override { return "field"; }
1595  bool resolve_type(THD *thd) override;
1596 };
1597 
1600 
1601  public:
1603  longlong val_int() override;
1604  const char *func_name() const override { return "ascii"; }
1605  bool resolve_type(THD *) override {
1606  max_length = 3;
1607  return false;
1608  }
1609 };
1610 
1613 
1614  public:
1616  longlong val_int() override;
1617  const char *func_name() const override { return "ord"; }
1618 };
1619 
1625 
1626  public:
1628  : Item_int_func(pos, a, b), enum_value(0) {}
1629  longlong val_int() override;
1630  const char *func_name() const override { return "find_in_set"; }
1631  bool resolve_type(THD *) override;
1632 };
1633 
1634 /* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
1635 
1636 class Item_func_bit : public Item_func {
1637  protected:
1638  /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1640  /// Buffer storing the determined value
1642  /**
1643  @returns true if the second argument should be of binary type for the
1644  result to be of binary type.
1645  */
1646  virtual bool binary_result_requires_binary_second_arg() const = 0;
1647 
1648  public:
1649  Item_func_bit(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {}
1650  Item_func_bit(const POS &pos, Item *a) : Item_func(pos, a) {}
1651 
1652  bool resolve_type(THD *) override;
1653  enum Item_result result_type() const override { return hybrid_type; }
1654 
1655  longlong val_int() override;
1656  String *val_str(String *str) override;
1657  double val_real() override;
1658  my_decimal *val_decimal(my_decimal *decimal_value) override;
1659 
1660  void print(const THD *thd, String *str,
1661  enum_query_type query_type) const override {
1662  print_op(thd, str, query_type);
1663  }
1664  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1665  if (hybrid_type == INT_RESULT)
1666  return get_date_from_int(ltime, fuzzydate);
1667  else
1668  return get_date_from_string(ltime, fuzzydate);
1669  }
1670  bool get_time(MYSQL_TIME *ltime) override {
1671  if (hybrid_type == INT_RESULT)
1672  return get_time_from_int(ltime);
1673  else
1674  return get_time_from_string(ltime);
1675  }
1676 
1677  private:
1678  /**
1679  @brief Performs the operation on integers to produce a result of type
1680  INT_RESULT.
1681  @return The result of the operation.
1682  */
1683  virtual longlong int_op() = 0;
1684 
1685  /**
1686  @brief Performs the operation on binary strings to produce a result of
1687  type STRING_RESULT.
1688  @return The result of the operation.
1689  */
1690  virtual String *str_op(String *) = 0;
1691 };
1692 
1693 /**
1694  Base class for all the bit functions that work with two binary
1695  arguments: '&', '|', '^'.
1696 */
1697 
1699  protected:
1700  bool binary_result_requires_binary_second_arg() const { return true; }
1701  template <class Char_func, class Int_func>
1702  String *eval_str_op(String *, Char_func char_func, Int_func int_func);
1703  template <class Int_func>
1704  longlong eval_int_op(Int_func int_func);
1705 
1706  public:
1708  : Item_func_bit(pos, a, b) {}
1709 };
1710 
1712  public:
1714  : Item_func_bit_two_param(pos, a, b) {}
1715  const char *func_name() const override { return "|"; }
1716 
1717  private:
1718  longlong int_op() override { return eval_int_op(std::bit_or<ulonglong>()); }
1719  String *str_op(String *str) override {
1720  return eval_str_op(str, std::bit_or<char>(), std::bit_or<ulonglong>());
1721  }
1722 };
1723 
1725  public:
1727  : Item_func_bit_two_param(pos, a, b) {}
1728  const char *func_name() const override { return "&"; }
1729 
1730  private:
1731  longlong int_op() override { return eval_int_op(std::bit_and<ulonglong>()); }
1732  String *str_op(String *str) override {
1733  return eval_str_op(str, std::bit_and<char>(), std::bit_and<ulonglong>());
1734  }
1735 };
1736 
1738  public:
1740  : Item_func_bit_two_param(pos, a, b) {}
1741  const char *func_name() const override { return "^"; }
1742 
1743  private:
1744  longlong int_op() override { return eval_int_op(std::bit_xor<ulonglong>()); }
1745  String *str_op(String *str) override {
1746  return eval_str_op(str, std::bit_xor<char>(), std::bit_xor<ulonglong>());
1747  }
1748 };
1749 
1751  public:
1753  longlong val_int() override;
1754  const char *func_name() const override { return "bit_count"; }
1755  bool resolve_type(THD *) override {
1757  return false;
1758  }
1759 };
1760 
1762  protected:
1763  bool binary_result_requires_binary_second_arg() const { return false; }
1764  template <bool to_left>
1766  template <bool to_left>
1767  String *eval_str_op(String *str);
1768 
1769  public:
1770  Item_func_shift(const POS &pos, Item *a, Item *b)
1771  : Item_func_bit(pos, a, b) {}
1772 };
1773 
1775  public:
1777  : Item_func_shift(pos, a, b) {}
1778  const char *func_name() const override { return "<<"; }
1779 
1780  private:
1781  longlong int_op() override { return eval_int_op<true>(); }
1782  String *str_op(String *str) override { return eval_str_op<true>(str); }
1783 };
1784 
1786  public:
1788  : Item_func_shift(pos, a, b) {}
1789  const char *func_name() const override { return ">>"; }
1790 
1791  private:
1792  longlong int_op() override { return eval_int_op<false>(); }
1793  String *str_op(String *str) override { return eval_str_op<false>(str); }
1794 };
1795 
1797  protected:
1799  return false;
1800  }
1801 
1802  public:
1804  const char *func_name() const override { return "~"; }
1805  void print(const THD *thd, String *str,
1806  enum_query_type query_type) const override {
1807  Item_func::print(thd, str, query_type);
1808  }
1809 
1810  private:
1811  longlong int_op() override;
1812  String *str_op(String *str) override;
1813 };
1814 
1817 
1818  public:
1822 
1823  bool itemize(Parse_context *pc, Item **res) override;
1824  longlong val_int() override;
1825  const char *func_name() const override { return "last_insert_id"; }
1826  bool resolve_type(THD *) override {
1827  unsigned_flag = true;
1828  if (arg_count) max_length = args[0]->max_length;
1829  return false;
1830  }
1831  bool check_function_as_value_generator(uchar *checker_args) override {
1834  checker_args);
1835  func_arg->banned_function_name = func_name();
1836  return true;
1837  }
1838 };
1839 
1842 
1843  public:
1844  Item_func_benchmark(const POS &pos, Item *count_expr, Item *expr)
1845  : Item_int_func(pos, count_expr, expr) {}
1846 
1847  /// Ensure that "benchmark()" is never optimized away
1849  return RAND_TABLE_BIT;
1850  }
1851 
1852  bool itemize(Parse_context *pc, Item **res) override;
1853  longlong val_int() override;
1854  const char *func_name() const override { return "benchmark"; }
1855  bool resolve_type(THD *) override {
1856  max_length = 1;
1857  maybe_null = true;
1858  return false;
1859  }
1860  void print(const THD *thd, String *str,
1861  enum_query_type query_type) const override;
1862  bool check_function_as_value_generator(uchar *checker_args) override {
1865  checker_args);
1866  func_arg->banned_function_name = func_name();
1867  return true;
1868  }
1869 };
1870 
1871 void item_func_sleep_init();
1872 void item_func_sleep_free();
1873 
1876 
1877  public:
1879 
1880  bool itemize(Parse_context *pc, Item **res) override;
1881  const char *func_name() const override { return "sleep"; }
1882  /**
1883  This function is non-deterministic and hence depends on the
1884  'RAND' pseudo-table.
1885 
1886  @retval RAND_TABLE_BIT
1887  */
1889  return RAND_TABLE_BIT;
1890  }
1891  bool check_function_as_value_generator(uchar *checker_args) override {
1894  checker_args);
1895  func_arg->banned_function_name = func_name();
1896  return true;
1897  }
1898  longlong val_int() override;
1899 };
1900 
1901 class Item_udf_func : public Item_func {
1902  typedef Item_func super;
1903 
1904  protected:
1906  bool is_expensive_processor(uchar *) override { return true; }
1907 
1908  public:
1909  Item_udf_func(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1910  : Item_func(pos, opt_list), udf(udf_arg) {
1911  null_on_null = false;
1912  }
1913 
1914  bool itemize(Parse_context *pc, Item **res) override;
1915  const char *func_name() const override { return udf.name(); }
1916  enum Functype functype() const override { return UDF_FUNC; }
1917  bool fix_fields(THD *thd, Item **) override {
1918  DBUG_ASSERT(fixed == 0);
1919  bool res = udf.fix_fields(thd, this, arg_count, args);
1921  fixed = true;
1922  return res;
1923  }
1924  void update_used_tables() override {
1925  /*
1926  TODO: Make a member in UDF_INIT and return if a UDF is deterministic or
1927  not.
1928  Currently UDF_INIT has a member (const_item) that is an in/out
1929  parameter to the init() call.
1930  The code in udf_handler::fix_fields also duplicates the arguments
1931  handling code in Item_func::fix_fields().
1932 
1933  The lack of information if a UDF is deterministic makes writing
1934  a correct update_used_tables() for UDFs impossible.
1935  One solution to this would be :
1936  - Add a is_deterministic member of UDF_INIT
1937  - (optionally) deprecate the const_item member of UDF_INIT
1938  - Take away the duplicate code from udf_handler::fix_fields() and
1939  make Item_udf_func call Item_func::fix_fields() to process its
1940  arguments as for any other function.
1941  - Store the deterministic flag returned by <udf>_init into the
1942  udf_handler.
1943  - Don't implement Item_udf_func::fix_fields, implement
1944  Item_udf_func::resolve_type() instead (similar to non-UDF functions).
1945  - Override Item_func::update_used_tables to call
1946  Item_func::update_used_tables() and add a RAND_TABLE_BIT to the
1947  result of Item_func::update_used_tables() if the UDF is
1948  non-deterministic.
1949  - (optionally) rename RAND_TABLE_BIT to NONDETERMINISTIC_BIT to
1950  better describe its usage.
1951 
1952  The above would require a change of the UDF API.
1953  Until that change is done here's how the current code works:
1954  We call Item_func::update_used_tables() only when we know that
1955  the function depends on real non-const tables and is deterministic.
1956  This can be done only because we know that the optimizer will
1957  call update_used_tables() only when there's possibly a new const
1958  table. So update_used_tables() can only make a Item_func more
1959  constant than it is currently.
1960  That's why we don't need to do anything if a function is guaranteed
1961  to return non-constant (it's non-deterministic) or is already a
1962  const.
1963  */
1967  }
1968  void cleanup() override;
1969  Item_result result_type() const override { return udf.result_type(); }
1970  bool is_expensive() override { return true; }
1971  void print(const THD *thd, String *str,
1972  enum_query_type query_type) const override;
1973 
1974  bool check_function_as_value_generator(uchar *checker_args) override {
1977  checker_args);
1978  func_arg->banned_function_name = func_name();
1979  return true;
1980  }
1981 
1982  protected:
1983  bool may_have_named_parameters() const override { return true; }
1984 };
1985 
1987  public:
1988  Item_func_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1989  : Item_udf_func(pos, udf_arg, opt_list) {}
1990  longlong val_int() override {
1991  DBUG_ASSERT(fixed == 1);
1992  return (longlong)rint(Item_func_udf_float::val_real());
1993  }
1994  my_decimal *val_decimal(my_decimal *dec_buf) override {
1995  double res = val_real();
1996  if (null_value) return NULL;
1997  double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
1998  return dec_buf;
1999  }
2000  double val_real() override;
2001  String *val_str(String *str) override;
2002  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2003  return get_date_from_real(ltime, fuzzydate);
2004  }
2005  bool get_time(MYSQL_TIME *ltime) override {
2006  return get_time_from_real(ltime);
2007  }
2008  bool resolve_type(THD *) override {
2010  fix_num_length_and_dec(); // @todo - needed?
2011  return false;
2012  }
2013 };
2014 
2016  public:
2017  Item_func_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2018  : Item_udf_func(pos, udf_arg, opt_list) {}
2019  longlong val_int() override;
2020  double val_real() override {
2021  return static_cast<double>(Item_func_udf_int::val_int());
2022  }
2023  String *val_str(String *str) override;
2024  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2025  return get_date_from_int(ltime, fuzzydate);
2026  }
2027  bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
2028  enum Item_result result_type() const override { return INT_RESULT; }
2029  bool resolve_type(THD *) override {
2031  return false;
2032  }
2033 };
2034 
2036  public:
2038  PT_item_list *opt_list)
2039  : Item_udf_func(pos, udf_arg, opt_list) {}
2040  longlong val_int() override;
2041  double val_real() override;
2042  my_decimal *val_decimal(my_decimal *) override;
2043  String *val_str(String *str) override;
2044  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2045  return get_date_from_decimal(ltime, fuzzydate);
2046  }
2047  bool get_time(MYSQL_TIME *ltime) override {
2048  return get_time_from_decimal(ltime);
2049  }
2050  enum Item_result result_type() const override { return DECIMAL_RESULT; }
2051  bool resolve_type(THD *thd) override;
2052 };
2053 
2055  public:
2056  Item_func_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2057  : Item_udf_func(pos, udf_arg, opt_list) {}
2058 
2059  String *val_str(String *) override;
2060  double val_real() override {
2061  int err_not_used;
2062  const char *end_not_used;
2063  String *res;
2064  res = val_str(&str_value);
2065  return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2066  &end_not_used, &err_not_used)
2067  : 0.0;
2068  }
2069  longlong val_int() override {
2070  int err_not_used;
2071  String *res;
2072  res = val_str(&str_value);
2073  return res ? my_strntoll(res->charset(), res->ptr(), res->length(), 10,
2074  nullptr, &err_not_used)
2075  : (longlong)0;
2076  }
2077  my_decimal *val_decimal(my_decimal *dec_buf) override {
2078  String *res = val_str(&str_value);
2079  if (!res) return NULL;
2080  string2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
2081  return dec_buf;
2082  }
2083  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2084  return get_date_from_string(ltime, fuzzydate);
2085  }
2086  bool get_time(MYSQL_TIME *ltime) override {
2087  return get_time_from_string(ltime);
2088  }
2089  enum Item_result result_type() const override { return STRING_RESULT; }
2090  bool resolve_type(THD *thd) override;
2091 };
2092 
2093 void mysql_ull_cleanup(THD *thd);
2095 
2098 
2100 
2101  public:
2103  : Item_int_func(pos, a, b) {}
2104 
2105  bool itemize(Parse_context *pc, Item **res) override;
2106  longlong val_int() override;
2107  const char *func_name() const override { return "get_lock"; }
2108  bool resolve_type(THD *) override {
2109  max_length = 1;
2110  maybe_null = true;
2111  return false;
2112  }
2113  bool is_non_const_over_literals(uchar *) override { return true; }
2114  bool check_function_as_value_generator(uchar *checker_args) override {
2117  checker_args);
2118  func_arg->banned_function_name = func_name();
2119  return true;
2120  }
2121  uint decimal_precision() const override { return max_length; }
2122 };
2123 
2126 
2128 
2129  public:
2131  bool itemize(Parse_context *pc, Item **res) override;
2132 
2133  longlong val_int() override;
2134  const char *func_name() const override { return "release_lock"; }
2135  bool resolve_type(THD *) override {
2136  max_length = 1;
2137  maybe_null = true;
2138  return false;
2139  }
2140  bool is_non_const_over_literals(uchar *) override { return true; }
2141  bool check_function_as_value_generator(uchar *checker_args) override {
2144  checker_args);
2145  func_arg->banned_function_name = func_name();
2146  return true;
2147  }
2148  uint decimal_precision() const override { return max_length; }
2149 };
2150 
2153 
2154  public:
2156  bool itemize(Parse_context *pc, Item **res) override;
2157 
2158  longlong val_int() override;
2159  const char *func_name() const override { return "release_all_locks"; }
2160  bool resolve_type(THD *) override {
2161  unsigned_flag = true;
2162  return false;
2163  }
2164  bool is_non_const_over_literals(uchar *) override { return true; }
2165  bool check_function_as_value_generator(uchar *checker_args) override {
2168  checker_args);
2169  func_arg->banned_function_name = func_name();
2170  return true;
2171  }
2172 };
2173 
2174 /* replication functions */
2175 
2179 
2180  public:
2182  : Item_int_func(pos, a, b) {}
2184  : Item_int_func(pos, a, b, c) {}
2185  Item_master_pos_wait(const POS &pos, Item *a, Item *b, Item *c, Item *d)
2186  : Item_int_func(pos, a, b, c, d) {}
2187 
2188  bool itemize(Parse_context *pc, Item **res) override;
2189  longlong val_int() override;
2190  const char *func_name() const override { return "master_pos_wait"; }
2191  bool resolve_type(THD *) override {
2192  max_length = 21;
2193  maybe_null = true;
2194  return false;
2195  }
2196  bool check_function_as_value_generator(uchar *checker_args) override {
2199  checker_args);
2200  func_arg->banned_function_name = func_name();
2201  return true;
2202  }
2203 };
2204 
2205 /**
2206  This class is used for implementing the new wait_for_executed_gtid_set
2207  function and the functions related to them. This new function is independent
2208  of the slave threads.
2209 */
2212 
2214 
2215  public:
2217  : Item_int_func(pos, a) {}
2219  : Item_int_func(pos, a, b) {}
2220 
2221  bool itemize(Parse_context *pc, Item **res) override;
2222  longlong val_int() override;
2223  const char *func_name() const override {
2224  return "wait_for_executed_gtid_set";
2225  }
2226  bool resolve_type(THD *) override {
2227  maybe_null = true;
2228  return false;
2229  }
2230 };
2231 
2234 
2236 
2237  public:
2240  : Item_int_func(pos, a, b) {}
2242  : Item_int_func(pos, a, b, c) {}
2243 
2244  bool itemize(Parse_context *pc, Item **res) override;
2245  longlong val_int() override;
2246  const char *func_name() const override {
2247  return "wait_until_sql_thread_after_gtids";
2248  }
2249  bool resolve_type(THD *) override {
2250  maybe_null = true;
2251  return false;
2252  }
2253 };
2254 
2258 
2259  public:
2261  : Item_int_func(pos, a, b) {}
2262  longlong val_int() override;
2263  const char *func_name() const override { return "gtid_subset"; }
2264  bool resolve_type(THD *) override {
2265  maybe_null = false;
2266  return false;
2267  }
2268  bool is_bool_func() const override { return true; }
2269 };
2270 
2271 /**
2272  Internal functions used by INFORMATION_SCHEMA implementation to check
2273  if user have access to given database/table/column.
2274 */
2275 
2277  public:
2279  : Item_int_func(pos, a) {}
2280  longlong val_int() override;
2281  const char *func_name() const override { return "can_access_database"; }
2282  bool resolve_type(THD *) override {
2283  maybe_null = true;
2284  return false;
2285  }
2286 };
2287 
2289  public:
2291  : Item_int_func(pos, a, b) {}
2292  longlong val_int() override;
2293  const char *func_name() const override { return "can_access_table"; }
2294  bool resolve_type(THD *) override {
2295  maybe_null = true;
2296  return false;
2297  }
2298 };
2299 
2301  public:
2303  : Item_int_func(pos, a, b) {}
2304  longlong val_int();
2305  const char *func_name() const { return "can_access_trigger"; }
2306  bool resolve_type(THD *) {
2307  max_length = 4;
2308  maybe_null = true;
2309  return false;
2310  }
2311 };
2312 
2314  public:
2316  : Item_int_func(pos, list) {}
2317  longlong val_int();
2318  const char *func_name() const { return "can_access_routine"; }
2319  bool resolve_type(THD *) {
2320  max_length = 4;
2321  maybe_null = true;
2322  return false;
2323  }
2324 };
2325 
2327  public:
2329  longlong val_int();
2330  const char *func_name() const { return "can_access_event"; }
2331  bool resolve_type(THD *) {
2332  maybe_null = true;
2333  return false;
2334  }
2335 };
2336 
2338  public:
2340  : Item_int_func(pos, a) {}
2341  longlong val_int();
2342  const char *func_name() const { return "can_access_resource_group"; }
2343  bool resolve_type(THD *) {
2344  max_length = 1; // Function can return 0 or 1.
2345  maybe_null = true;
2346  return false;
2347  }
2348 };
2349 
2351  public:
2353  : Item_int_func(pos, a, b, c, d) {}
2354  longlong val_int() override;
2355  const char *func_name() const override { return "can_access_view"; }
2356  bool resolve_type(THD *) override {
2357  maybe_null = true;
2358  return false;
2359  }
2360 };
2361 
2363  public:
2365  : Item_int_func(pos, a, b, c) {}
2366  longlong val_int() override;
2367  const char *func_name() const override { return "can_access_column"; }
2368  bool resolve_type(THD *) override {
2369  maybe_null = true;
2370  return false;
2371  }
2372 };
2373 
2375  public:
2377  : Item_int_func(pos, a) {}
2379  : Item_int_func(pos, a, b) {}
2380  longlong val_int();
2381  const char *func_name() const { return "is_visible_dd_object"; }
2382  bool resolve_type(THD *) {
2383  max_length = 1;
2384  maybe_null = true;
2385  return false;
2386  }
2387 };
2388 
2390  public:
2392  : Item_int_func(pos, list) {}
2393  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2394  longlong val_int() override;
2395  const char *func_name() const override { return "internal_table_rows"; }
2396  bool resolve_type(THD *) override {
2397  maybe_null = true;
2398  unsigned_flag = true;
2399  null_on_null = false;
2400  return false;
2401  }
2402 };
2403 
2405  public:
2407  : Item_int_func(pos, list) {}
2408  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2409  longlong val_int() override;
2410  const char *func_name() const override { return "internal_avg_row_length"; }
2411  bool resolve_type(THD *) override {
2412  maybe_null = true;
2413  unsigned_flag = true;
2414  null_on_null = false;
2415  return false;
2416  }
2417 };
2418 
2420  public:
2422  : Item_int_func(pos, list) {}
2423  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2424  longlong val_int() override;
2425  const char *func_name() const override { return "internal_data_length"; }
2426  bool resolve_type(THD *) override {
2427  maybe_null = true;
2428  unsigned_flag = true;
2429  null_on_null = false;
2430  return false;
2431  }
2432 };
2433 
2435  public:
2437  : Item_int_func(pos, list) {}
2438  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2439  longlong val_int() override;
2440  const char *func_name() const override { return "internal_max_data_length"; }
2441  bool resolve_type(THD *) override {
2442  maybe_null = true;
2443  unsigned_flag = true;
2444  null_on_null = false;
2445  return false;
2446  }
2447 };
2448 
2450  public:
2452  : Item_int_func(pos, list) {}
2453  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2454  longlong val_int() override;
2455  const char *func_name() const override { return "internal_index_length"; }
2456  bool resolve_type(THD *) override {
2457  maybe_null = true;
2458  unsigned_flag = true;
2459  null_on_null = false;
2460  return false;
2461  }
2462 };
2463 
2465  public:
2467  : Item_int_func(pos, list) {}
2468  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2469  longlong val_int() override;
2470  const char *func_name() const override { return "internal_data_free"; }
2471  bool resolve_type(THD *) override {
2472  maybe_null = true;
2473  unsigned_flag = true;
2474  null_on_null = false;
2475  return false;
2476  }
2477 };
2478 
2480  public:
2482  : Item_int_func(pos, list) {}
2483  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2484  longlong val_int() override;
2485  const char *func_name() const override { return "internal_auto_increment"; }
2486  bool resolve_type(THD *) override {
2487  maybe_null = true;
2488  unsigned_flag = true;
2489  null_on_null = false;
2490  return false;
2491  }
2492 };
2493 
2495  public:
2497  : Item_int_func(pos, list) {}
2498  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2499  longlong val_int() override;
2500  const char *func_name() const override { return "internal_checksum"; }
2501  bool resolve_type(THD *) override {
2502  maybe_null = true;
2503  null_on_null = false;
2504  return false;
2505  }
2506 };
2507 
2509  public:
2511  : Item_int_func(pos, a) {}
2512  longlong val_int() override;
2513  const char *func_name() const override { return "internal_keys_disabled"; }
2514  bool resolve_type(THD *) override {
2515  maybe_null = false;
2516  null_on_null = false;
2517  return false;
2518  }
2519 };
2520 
2522  public:
2524  PT_item_list *list)
2525  : Item_int_func(pos, list) {}
2526  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2527  longlong val_int() override;
2528  const char *func_name() const override {
2529  return "internal_index_column_cardinality";
2530  }
2531  bool resolve_type(THD *) override {
2532  maybe_null = true;
2533  null_on_null = false;
2534  return false;
2535  }
2536 };
2537 
2539  public:
2541  Item *d)
2542  : Item_int_func(pos, a, b, c, d) {}
2543  longlong val_int() override;
2544  const char *func_name() const override { return "internal_dd_char_length"; }
2545  bool resolve_type(THD *) override {
2546  maybe_null = true;
2547  null_on_null = false;
2548  return false;
2549  }
2550 };
2551 
2553  : public Item_int_func {
2554  public:
2556  PT_item_list *list)
2557  : Item_int_func(pos, list) {}
2558  longlong val_int() override;
2559  const char *func_name() const override {
2560  return "internal_get_view_warning_or_error";
2561  }
2562  bool resolve_type(THD *) override {
2563  max_length = 1;
2564  maybe_null = false;
2565  null_on_null = false;
2566  return false;
2567  }
2568 };
2569 
2571  public:
2573  : Item_int_func(pos, list) {}
2574  longlong val_int() override;
2575  bool resolve_type(THD *) override {
2576  maybe_null = true;
2577  null_on_null = false;
2578  return false;
2579  }
2580  const char *func_name() const override {
2581  return "get_dd_index_sub_part_length";
2582  }
2583 };
2584 
2586  public:
2588  Item *d)
2589  : Item_int_func(pos, a, b, c, d) {}
2590  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2591  longlong val_int() override;
2592  const char *func_name() const override { return "internal_tablespace_id"; }
2593  bool resolve_type(THD *) override {
2594  maybe_null = true;
2595  null_on_null = false;
2596  return false;
2597  }
2598 };
2599 
2601  : public Item_int_func {
2602  public:
2604  Item *b, Item *c, Item *d)
2605  : Item_int_func(pos, a, b, c, d) {}
2606 
2607  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2608  longlong val_int() override;
2609 
2610  const char *func_name() const override {
2611  return "internal_tablespace_logfile_group_number";
2612  }
2613 
2614  bool resolve_type(THD *) override {
2615  maybe_null = true;
2616  null_on_null = false;
2617  return false;
2618  }
2619 };
2620 
2622  public:
2624  Item *c, Item *d)
2625  : Item_int_func(pos, a, b, c, d) {}
2626 
2627  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2628  longlong val_int() override;
2629 
2630  const char *func_name() const override {
2631  return "internal_tablespace_free_extents";
2632  }
2633 
2634  bool resolve_type(THD *) override {
2635  maybe_null = true;
2636  null_on_null = false;
2637  return false;
2638  }
2639 };
2640 
2642  public:
2644  Item *c, Item *d)
2645  : Item_int_func(pos, a, b, c, d) {}
2646 
2647  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2648  longlong val_int() override;
2649 
2650  const char *func_name() const override {
2651  return "internal_tablespace_total_extents";
2652  }
2653 
2654  bool resolve_type(THD *) override {
2655  maybe_null = true;
2656  null_on_null = false;
2657  return false;
2658  }
2659 };
2660 
2662  public:
2664  Item *c, Item *d)
2665  : Item_int_func(pos, a, b, c, d) {}
2666 
2667  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2668  longlong val_int() override;
2669 
2670  const char *func_name() const override {
2671  return "internal_tablespace_extent_size";
2672  }
2673 
2674  bool resolve_type(THD *) override {
2675  maybe_null = true;
2676  null_on_null = false;
2677  return false;
2678  }
2679 };
2680 
2682  public:
2684  Item *c, Item *d)
2685  : Item_int_func(pos, a, b, c, d) {}
2686 
2687  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2688  longlong val_int() override;
2689 
2690  const char *func_name() const override {
2691  return "internal_tablespace_initial_size";
2692  }
2693 
2694  bool resolve_type(THD *) override {
2695  maybe_null = true;
2696  null_on_null = false;
2697  return false;
2698  }
2699 };
2700 
2702  public:
2704  Item *c, Item *d)
2705  : Item_int_func(pos, a, b, c, d) {}
2706 
2707  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2708  longlong val_int() override;
2709 
2710  const char *func_name() const override {
2711  return "internal_tablespace_maximum_size";
2712  }
2713 
2714  bool resolve_type(THD *) override {
2715  maybe_null = true;
2716  null_on_null = false;
2717  return false;
2718  }
2719 };
2720 
2722  public:
2724  Item *b, Item *c, Item *d)
2725  : Item_int_func(pos, a, b, c, d) {}
2726 
2727  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2728  longlong val_int() override;
2729 
2730  const char *func_name() const override {
2731  return "internal_tablespace_autoextend_size";
2732  }
2733 
2734  bool resolve_type(THD *) override {
2735  maybe_null = true;
2736  null_on_null = false;
2737  return false;
2738  }
2739 };
2740 
2742  public:
2744  Item *c, Item *d)
2745  : Item_int_func(pos, a, b, c, d) {}
2746 
2747  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2748  longlong val_int() override;
2749 
2750  const char *func_name() const override {
2751  return "internal_tablespace_version";
2752  }
2753 
2754  bool resolve_type(THD *) override {
2755  maybe_null = true;
2756  null_on_null = false;
2757  return false;
2758  }
2759 };
2760 
2762  public:
2764  Item *c, Item *d)
2765  : Item_int_func(pos, a, b, c, d) {}
2766 
2767  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2768  longlong val_int() override;
2769 
2770  const char *func_name() const override {
2771  return "internal_tablespace_data_free";
2772  }
2773 
2774  bool resolve_type(THD *) override {
2775  maybe_null = true;
2776  null_on_null = false;
2777  return false;
2778  }
2779 };
2780 
2781 /**
2782  Common class for:
2783  Item_func_get_system_var
2784  Item_func_get_user_var
2785  Item_func_set_user_var
2786 */
2787 class Item_var_func : public Item_func {
2788  public:
2790  explicit Item_var_func(const POS &pos) : Item_func(pos) {}
2791 
2792  Item_var_func(THD *thd, Item_var_func *item) : Item_func(thd, item) {}
2793 
2795  Item_var_func(const POS &pos, Item *a) : Item_func(pos, a) {}
2796 
2797  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2798  return get_date_from_non_temporal(ltime, fuzzydate);
2799  }
2800  bool get_time(MYSQL_TIME *ltime) override {
2801  return get_time_from_non_temporal(ltime);
2802  }
2803  bool check_function_as_value_generator(uchar *checker_args) override {
2806  checker_args);
2807  func_arg->err_code = (func_arg->source == VGS_CHECK_CONSTRAINT)
2808  ? ER_CHECK_CONSTRAINT_VARIABLES
2809  : ER_DEFAULT_VAL_GENERATED_VARIABLES;
2810  return true;
2811  }
2812 };
2813 
2814 /* Handling of user definable variables */
2815 
2816 // this is needed for user_vars hash
2818  static const size_t extra_size = sizeof(double);
2819  char *m_ptr; // Value
2820  size_t m_length; // Value length
2821  Item_result m_type; // Value type
2823 
2824  void reset_value() {
2825  m_ptr = NULL;
2826  m_length = 0;
2827  }
2828  void set_value(char *value, size_t length) {
2829  m_ptr = value;
2830  m_length = length;
2831  }
2832 
2833  /**
2834  Position inside a user_var_entry where small values are stored:
2835  double values, longlong values and string values with length
2836  up to extra_size (should be 8 bytes on all platforms).
2837  String values with length longer than 8 are stored in a separate
2838  memory buffer, which is allocated when needed using the method realloc().
2839  */
2841  return pointer_cast<char *>(this) + ALIGN_SIZE(sizeof(user_var_entry));
2842  }
2843 
2844  /**
2845  Position inside a user_var_entry where a null-terminates array
2846  of characters representing the variable name is stored.
2847  */
2848  char *name_ptr() { return internal_buffer_ptr() + extra_size; }
2849 
2850  /**
2851  Initialize m_ptr to the internal buffer (if the value is small enough),
2852  or allocate a separate buffer.
2853  @param length - length of the value to be stored.
2854  */
2855  bool mem_realloc(size_t length);
2856 
2857  /**
2858  Check if m_ptr point to an external buffer previously alloced by realloc().
2859  @retval true - an external buffer is alloced.
2860  @retval false - m_ptr is null, or points to the internal buffer.
2861  */
2862  bool alloced() { return m_ptr && m_ptr != internal_buffer_ptr(); }
2863 
2864  /**
2865  Free the external value buffer, if it's allocated.
2866  */
2867  void free_value() {
2868  if (alloced()) my_free(m_ptr);
2869  }
2870 
2871  /**
2872  Copy the array of characters from the given name into the internal
2873  name buffer and initialize entry_name to point to it.
2874  */
2876  name.strcpy(name_ptr());
2877  entry_name = Name_string(name_ptr(), name.length());
2878  }
2879 
2880  /**
2881  Initialize all members
2882 
2883  @param thd Current session.
2884  @param name Name of the user_var_entry instance.
2885  @param cs charset information of the user_var_entry instance.
2886  */
2887  void init(THD *thd, const Simple_cstring &name, const CHARSET_INFO *cs);
2888 
2889  /**
2890  Store a value of the given type into a user_var_entry instance.
2891  @param from Value
2892  @param length Size of the value
2893  @param type type
2894  @return
2895  @retval false on success
2896  @retval true on memory allocation error
2897  */
2898  bool store(const void *from, size_t length, Item_result type);
2899 
2900  /**
2901  Assert the user variable is locked.
2902  This is debug code only.
2903  The thread LOCK_thd_data mutex protects:
2904  - the thd->user_vars hash itself
2905  - the values in the user variable itself.
2906  The protection is required for monitoring,
2907  as a different thread can inspect this session
2908  user variables, on a live session.
2909  */
2910  void assert_locked() const;
2911 
2912  public:
2913  user_var_entry() {} /* Remove gcc warning */
2914 
2915  Simple_cstring entry_name; // Variable name
2916  DTCollation collation; // Collation with attributes
2918  bool unsigned_flag; // true if unsigned, false if signed
2919 
2920  /**
2921  Store a value of the given type and attributes (collation, sign)
2922  into a user_var_entry instance.
2923  @param ptr Value
2924  @param length Size of the value
2925  @param type type
2926  @param cs Character set and collation of the value
2927  @param dv Collation derivation of the value
2928  @param unsigned_arg Signess of the value
2929  @return
2930  @retval false on success
2931  @retval true on memory allocation error
2932  */
2933  bool store(const void *ptr, size_t length, Item_result type,
2934  const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
2935  /**
2936  Set type of to the given value.
2937  @param type Data type.
2938  */
2940  assert_locked();
2941  m_type = type;
2942  }
2943  /**
2944  Set value to NULL
2945  @param type Data type.
2946  */
2947 
2949  assert_locked();
2950  free_value();
2951  reset_value();
2952  m_type = type;
2953  }
2954 
2955  /**
2956  Allocates and initializes a user variable instance.
2957 
2958  @param thd Current session.
2959  @param name Name of the variable.
2960  @param cs Charset of the variable.
2961 
2962  @return Address of the allocated and initialized user_var_entry instance.
2963  @retval NULL On allocation error.
2964  */
2965  static user_var_entry *create(THD *thd, const Name_string &name,
2966  const CHARSET_INFO *cs);
2967 
2968  /**
2969  Free all memory used by a user_var_entry instance
2970  previously created by create().
2971  */
2972  void destroy() {
2973  assert_locked();
2974  free_value(); // Free the external value buffer
2975  my_free(this); // Free the instance itself
2976  }
2977 
2978  void lock();
2979  void unlock();
2980 
2981  /* Routines to access the value and its type */
2982  const char *ptr() const { return m_ptr; }
2983  size_t length() const { return m_length; }
2984  Item_result type() const { return m_type; }
2985  /* Item-alike routines to access the value */
2986  double val_real(bool *null_value) const;
2987  longlong val_int(bool *null_value) const;
2988  String *val_str(bool *null_value, String *str, uint decimals) const;
2989  my_decimal *val_decimal(bool *null_value, my_decimal *result) const;
2990 };
2991 
2995  /*
2996  The entry_thread_id variable is used:
2997  1) to skip unnecessary updates of the entry field (see above);
2998  2) to reset the entry field that was initialized in the other thread
2999  (for example, an item tree of a trigger that updates user variables
3000  may be shared between several connections, and the entry_thread_id field
3001  prevents updates of one connection user variables from a concurrent
3002  connection calling the same trigger that initially updated some
3003  user variable it the first connection context).
3004  */
3006  /**
3007  Delayed setting of non-constness.
3008 
3009  Normally, Item_func_get_user_var objects are tagged as not const
3010  when Item_func_set_user_var::fix_fields() is called for the same
3011  variable in the same query. If delayed_non_constness is set, the
3012  tagging is delayed until the variable is actually set. This means
3013  that Item_func_get_user_var objects will still be treated as a
3014  constant by the optimizer and executor until the variable is
3015  actually changed.
3016 
3017  @see select_dumpvar::send_data().
3018  */
3023  union {
3025  double vreal;
3028  } save_result;
3029 
3030  public:
3031  Name_string name; // keep it public
3032 
3034  : Item_var_func(b),
3036  entry(NULL),
3037  entry_thread_id(0),
3038  delayed_non_constness(delayed),
3039  name(a) {}
3040  Item_func_set_user_var(const POS &pos, Name_string a, Item *b, bool delayed)
3041  : Item_var_func(pos, b),
3043  entry(NULL),
3044  entry_thread_id(0),
3045  delayed_non_constness(delayed),
3046  name(a) {}
3047 
3049  : Item_var_func(thd, item),
3051  entry(item->entry),
3054  value(item->value),
3055  decimal_buff(item->decimal_buff),
3056  null_item(item->null_item),
3057  save_result(item->save_result),
3058  name(item->name) {}
3059  enum Functype functype() const override { return SUSERVAR_FUNC; }
3060  double val_real() override;
3061  longlong val_int() override;
3062  String *val_str(String *str) override;
3063  my_decimal *val_decimal(my_decimal *) override;
3064  bool update_hash(const void *ptr, uint length, enum Item_result type,
3065  const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
3066  bool send(Protocol *protocol, String *str_arg) override;
3067  void make_field(Send_field *tmp_field) override;
3068  bool check(bool use_result_field);
3069  void save_item_result(Item *item);
3070  bool update();
3071  enum Item_result result_type() const override { return cached_result_type; }
3072  bool fix_fields(THD *thd, Item **ref) override;
3073  bool resolve_type(THD *) override;
3074  void print(const THD *thd, String *str,
3075  enum_query_type query_type) const override;
3076  void print_assignment(const THD *thd, String *str,
3077  enum_query_type query_type) const;
3078  const char *func_name() const override { return "set_user_var"; }
3079 
3080  type_conversion_status save_in_field(Field *field, bool no_conversions,
3081  bool can_use_result_field);
3082 
3083  void save_org_in_field(Field *field) override {
3084  save_in_field(field, true, false);
3085  }
3086 
3087  bool set_entry(THD *thd, bool create_if_not_exists);
3088  void cleanup() override;
3089 
3090  protected:
3092  bool no_conversions) override {
3093  return save_in_field(field, no_conversions, true);
3094  }
3095 };
3096 
3098  private Settable_routine_parameter {
3101 
3102  public:
3103  Name_string name; // keep it public
3104 
3109 
3110  enum Functype functype() const override { return GUSERVAR_FUNC; }
3111  double val_real() override;
3112  longlong val_int() override;
3113  my_decimal *val_decimal(my_decimal *) override;
3114  String *val_str(String *str) override;
3115  bool resolve_type(THD *) override;
3116  void update_used_tables() override {} // Keep existing used tables
3117  void print(const THD *thd, String *str,
3118  enum_query_type query_type) const override;
3119  enum Item_result result_type() const override;
3120  /*
3121  We must always return variables as strings to guard against selects of type
3122  select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
3123  */
3124  const char *func_name() const override { return "get_user_var"; }
3125  bool is_non_const_over_literals(uchar *) override { return true; }
3126  bool eq(const Item *item, bool binary_cmp) const override;
3127 
3128  private:
3129  bool set_value(THD *thd, sp_rcontext *ctx, Item **it) override;
3130 
3131  public:
3133  return this;
3134  }
3135 };
3136 
3137 /*
3138  This item represents user variable used as out parameter (e.g in LOAD DATA),
3139  and it is supposed to be used only for this purprose. So it is simplified
3140  a lot. Actually you should never obtain its value.
3141 
3142  The only two reasons for this thing being an Item is possibility to store it
3143  in List<Item> and desire to place this code somewhere near other functions
3144  working with user variables.
3145 */
3149 
3150  public:
3152  : Item(pos), name(a) {
3153  item_name.copy(a);
3154  }
3155  /* We should return something different from FIELD_ITEM here */
3156  enum Type type() const override { return STRING_ITEM; }
3157  double val_real() override;
3158  longlong val_int() override;
3159  String *val_str(String *str) override;
3160  my_decimal *val_decimal(my_decimal *decimal_buffer) override;
3162  DBUG_ASSERT(0);
3163  return true;
3164  }
3165  bool get_time(MYSQL_TIME *) override {
3166  DBUG_ASSERT(0);
3167  return true;
3168  }
3169 
3170  /* fix_fields() binds variable name with its entry structure */
3171  bool fix_fields(THD *thd, Item **ref) override;
3172  void print(const THD *thd, String *str,
3173  enum_query_type query_type) const override;
3174  void set_null_value(const CHARSET_INFO *cs);
3175  void set_value(const char *str, size_t length, const CHARSET_INFO *cs);
3176 };
3177 
3178 /* A system variable */
3179 
3180 #define GET_SYS_VAR_CACHE_LONG 1
3181 #define GET_SYS_VAR_CACHE_DOUBLE 2
3182 #define GET_SYS_VAR_CACHE_STRING 4
3183 
3185 
3186 /** Class to log audit event MYSQL_AUDIT_GLOBAL_VARIABLE_GET. */
3188  public:
3190  uchar cache_type);
3192 
3193  private:
3194  // Thread handle.
3196 
3197  // Item_func_get_system_var instance.
3199 
3200  /*
3201  Value conversion type.
3202  Depending on the value conversion type GET_SYS_VAR_CACHE_* is stored in this
3203  member while creating the object. While converting value if there are any
3204  intermediate conversions in the same query then this member is used to avoid
3205  auditing more than once.
3206  */
3208 
3209  /*
3210  To indicate event auditing is required or not. Event is not audited if
3211  * scope of the variable is *not* GLOBAL.
3212  * or the event is already audited for global variable for the same query.
3213  */
3215 };
3216 
3222  double cached_dval;
3227 
3228  template <typename T>
3230 
3232 
3233  public:
3234  Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
3235  LEX_STRING *component_arg, const char *name_arg,
3236  size_t name_len_arg);
3237  enum Functype functype() const override { return GSYSVAR_FUNC; }
3238  bool resolve_type(THD *) override;
3239  void print(const THD *thd, String *str,
3240  enum_query_type query_type) const override;
3241  table_map used_tables() const override { return 0; }
3242  bool is_non_const_over_literals(uchar *) override { return true; }
3243  enum Item_result result_type() const override;
3244  double val_real() override;
3245  longlong val_int() override;
3246  String *val_str(String *) override;
3247  my_decimal *val_decimal(my_decimal *dec_buf) override {
3248  return val_decimal_from_real(dec_buf);
3249  }
3250  /* TODO: fix to support views */
3251  const char *func_name() const override { return "get_system_var"; }
3252  /**
3253  Indicates whether this system variable is written to the binlog or not.
3254 
3255  Variables are written to the binlog as part of "status_vars" in
3256  Query_log_event, as an Intvar_log_event, or a Rand_log_event.
3257 
3258  @return true if the variable is written to the binlog, false otherwise.
3259  */
3260  bool is_written_to_binlog();
3261  bool eq(const Item *item, bool binary_cmp) const override;
3262 
3263  void cleanup() override;
3264 };
3265 
3266 class JOIN;
3267 
3270 
3271  public:
3274  bool join_key;
3278  /**
3279  Master item means that if identical items are present in the
3280  statement, they use the same FT handler. FT handler is initialized
3281  only for master item and slave items just use it. FT hints initialized
3282  for master only, slave items HINTS are not accessed.
3283  */
3285  Item *concat_ws; // Item_func_concat_ws
3286  String value; // value of concat_ws
3287  String search_value; // key_item()'s value converted to cmp_collation
3288 
3289  /**
3290  Constructor for Item_func_match class.
3291 
3292  @param pos Position of token in the parser.
3293  @param a List of arguments.
3294  @param against_arg Expression to match against.
3295  @param b FT Flags.
3296  */
3297  Item_func_match(const POS &pos, PT_item_list *a, Item *against_arg, uint b)
3298  : Item_real_func(pos, a),
3299  against(against_arg),
3300  key(0),
3301  flags(b),
3302  join_key(false),
3303  ft_handler(NULL),
3304  table_ref(NULL),
3305  master(NULL),
3306  concat_ws(NULL),
3307  hints(NULL),
3309  null_on_null = false;
3310  }
3311 
3312  bool itemize(Parse_context *pc, Item **res) override;
3313 
3314  void cleanup() override {
3315  DBUG_ENTER("Item_func_match::cleanup");
3317  if (!master && ft_handler) {
3319  destroy(hints);
3320  }
3321  ft_handler = NULL;
3322  concat_ws = NULL;
3323  table_ref = NULL; // required by Item_func_match::eq()
3324  master = NULL;
3326  }
3327  Item *key_item() const override { return against; }
3328  enum Functype functype() const override { return FT_FUNC; }
3329  const char *func_name() const override { return "match"; }
3330  void update_used_tables() override {}
3331  bool fix_fields(THD *thd, Item **ref) override;
3332  bool eq(const Item *, bool binary_cmp) const override;
3333  /* The following should be safe, even if we compare doubles */
3334  longlong val_int() override {
3335  DBUG_ASSERT(fixed);
3336  return val_real() != 0.0;
3337  }
3338  double val_real() override;
3339  void print(const THD *thd, String *str,
3340  enum_query_type query_type) const override;
3341 
3342  bool fix_index(const THD *thd);
3343  bool init_search(THD *thd);
3344  bool check_function_as_value_generator(uchar *checker_args) override {
3347  checker_args);
3348  func_arg->banned_function_name = func_name();
3349  return true;
3350  }
3351 
3352  /**
3353  Get number of matching rows from FT handler.
3354 
3355  @note Requires that FT handler supports the extended API
3356 
3357  @return Number of matching rows in result
3358  */
3362 
3363  return ((FT_INFO_EXT *)ft_handler)
3364  ->could_you->count_matches((FT_INFO_EXT *)ft_handler);
3365  }
3366 
3367  /**
3368  Check whether FT result is ordered on rank
3369 
3370  @return true if result is ordered
3371  @return false otherwise
3372  */
3374  DBUG_ASSERT(!master);
3375  if (hints->get_flags() & FT_SORTED) return true;
3376 
3378  return false;
3379 
3381  return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3383  }
3384 
3385  /**
3386  Check whether FT result contains the document ID
3387 
3388  @return true if document ID is available
3389  @return false otherwise
3390  */
3393 
3395  return false;
3396 
3397  return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3399  }
3400 
3401  float get_filtering_effect(THD *thd, table_map filter_for_table,
3402  table_map read_tables,
3403  const MY_BITMAP *fields_to_ignore,
3404  double rows_in_table) override;
3405 
3406  /**
3407  Returns master MATCH function.
3408 
3409  @return pointer to master MATCH function.
3410  */
3412  if (master) return master->get_master();
3413  return this;
3414  }
3415 
3416  /**
3417  Set master MATCH function and adjust used_in_where_only value.
3418 
3419  @param item item for which master should be set.
3420  */
3423  item->master = this;
3424  }
3425 
3426  /**
3427  Returns pointer to Ft_hints object belonging to master MATCH function.
3428 
3429  @return pointer to Ft_hints object
3430  */
3432  DBUG_ASSERT(!master);
3433  return hints;
3434  }
3435 
3436  /**
3437  Set comparison operation type and and value for master MATCH function.
3438 
3439  @param type comparison operation type
3440  @param value_arg comparison operation value
3441  */
3442  void set_hints_op(enum ft_operation type, double value_arg) {
3443  DBUG_ASSERT(!master);
3444  hints->set_hint_op(type, value_arg);
3445  }
3446 
3447  /**
3448  Set FT hints.
3449  */
3450  void set_hints(JOIN *join, uint ft_flag, ha_rows ft_limit, bool no_cond);
3451 
3452  /**
3453  Check if ranking is not needed.
3454 
3455  @return true if ranking is not needed
3456  @return false otherwise
3457  */
3459  DBUG_ASSERT(!master);
3460  return (!(hints->get_flags() & FT_SORTED) && // FT_SORTED is no set
3461  used_in_where_only && // MATCH result is not used
3462  // in expression
3463  hints->get_op_type() == FT_OP_NO); // MATCH is single function
3464  }
3465 
3466  /**
3467  Set flag that the function is a simple expression.
3468 
3469  @param val true if the function is a simple expression, false otherwise
3470  */
3471  void set_simple_expression(bool val) {
3472  DBUG_ASSERT(!master);
3473  simple_expression = val;
3474  }
3475 
3476  /**
3477  Check if this MATCH function is a simple expression in WHERE condition.
3478 
3479  @return true if simple expression
3480  @return false otherwise
3481  */
3483  DBUG_ASSERT(!master);
3484  return simple_expression;
3485  }
3486 
3487  private:
3488  /**
3489  Fulltext index hints, initialized for master MATCH function only.
3490  */
3492  /**
3493  Flag is true when MATCH function is used as a simple expression in
3494  WHERE condition, i.e. there is no AND/OR combinations, just simple
3495  MATCH function or [MATCH, rank] comparison operation.
3496  */
3498  /**
3499  true if MATCH function is used in WHERE condition only.
3500  Used to dermine what hints can be used for FT handler.
3501  Note that only master MATCH function has valid value.
3502  it's ok since only master function is involved in the hint processing.
3503  */
3505  /**
3506  Check whether storage engine for given table,
3507  allows FTS Boolean search on non-indexed columns.
3508 
3509  @todo A flag should be added to the extended fulltext API so that
3510  it may be checked whether search on non-indexed columns are
3511  supported. Currently, it is not possible to check for such a
3512  flag since @c this->ft_handler is not yet set when this function is
3513  called. The current hack is to assume that search on non-indexed
3514  columns are supported for engines that does not support the extended
3515  fulltext API (e.g., MyISAM), while it is not supported for other
3516  engines (e.g., InnoDB)
3517 
3518  @param tr Table for which storage engine to check
3519 
3520  @retval true if BOOLEAN search on non-indexed columns is supported
3521  @retval false otherwise
3522  */
3524  // Only Boolean search may support non_indexed columns
3525  if (!(flags & FT_BOOL)) return false;
3526 
3527  DBUG_ASSERT(tr && tr->file);
3528 
3529  // Assume that if extended fulltext API is not supported,
3530  // non-indexed columns are allowed. This will be true for MyISAM.
3531  if ((tr->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0) return true;
3532 
3533  return false;
3534  }
3535 };
3536 
3539 
3541 
3542  public:
3544 
3545  bool itemize(Parse_context *pc, Item **res) override;
3546  longlong val_int() override;
3547  const char *func_name() const override { return "is_free_lock"; }
3548  bool resolve_type(THD *) override {
3549  max_length = 1;
3550  maybe_null = true;
3551  return false;
3552  }
3553  bool is_non_const_over_literals(uchar *) override { return true; }
3554  bool check_function_as_value_generator(uchar *checker_args) override {
3557  checker_args);
3558  func_arg->banned_function_name = func_name();
3559  return true;
3560  }
3561 };
3562 
3565 
3567 
3568  public:
3570 
3571  bool itemize(Parse_context *pc, Item **res) override;
3572  longlong val_int() override;
3573  const char *func_name() const override { return "is_used_lock"; }
3574  bool resolve_type(THD *) override {
3575  unsigned_flag = true;
3576  maybe_null = true;
3577  return false;
3578  }
3579  bool is_non_const_over_literals(uchar *) override { return true; }
3580  bool check_function_as_value_generator(uchar *checker_args) override {
3583  checker_args);
3584  func_arg->banned_function_name = func_name();
3585  return true;
3586  }
3587 };
3588 
3591 
3592  public:
3594 
3595  bool itemize(Parse_context *pc, Item **res) override;
3596 
3597  longlong val_int() override;
3598  const char *func_name() const override { return "row_count"; }
3599  bool resolve_type(THD *) override {
3600  maybe_null = false;
3601  return false;
3602  }
3603  bool check_function_as_value_generator(uchar *checker_args) override {
3606  checker_args);
3607  func_arg->banned_function_name = func_name();
3608  return true;
3609  }
3610 };
3611 
3612 /*
3613  *
3614  * Stored FUNCTIONs
3615  *
3616  */
3617 
3618 class sp_head;
3619 class sp_name;
3620 
3621 class Item_func_sp final : public Item_func {
3622  typedef Item_func super;
3623 
3624  private:
3627  mutable sp_head *m_sp;
3630  /*
3631  The result field of the concrete stored function.
3632  */
3634 
3635  bool execute();
3636  bool execute_impl(THD *thd);
3637  bool init_result_field(THD *thd);
3638 
3639  protected:
3640  bool is_expensive_processor(uchar *) override { return true; }
3641 
3642  public:
3643  Item_func_sp(const POS &pos, const LEX_STRING &db_name,
3644  const LEX_STRING &fn_name, bool use_explicit_name,
3645  PT_item_list *opt_list);
3646 
3647  bool itemize(Parse_context *pc, Item **res) override;
3648  /**
3649  Must not be called before the procedure is resolved,
3650  i.e. @c init_result_field().
3651  */
3652  table_map get_initial_pseudo_tables() const override;
3653  void update_used_tables() override;
3654  void fix_after_pullout(SELECT_LEX *parent_select,
3655  SELECT_LEX *removed_select) override;
3656  void cleanup() override;
3657 
3658  const char *func_name() const override;
3659 
3660  Field *tmp_table_field(TABLE *t_arg) override;
3661 
3662  void make_field(Send_field *tmp_field) override;
3663 
3664  Item_result result_type() const override;
3665 
3666  longlong val_int() override {
3667  if (execute() || null_value) return (longlong)0;
3668  return sp_result_field->val_int();
3669  }
3670 
3671  double val_real() override {
3672  if (execute() || null_value) return 0.0;
3673  return sp_result_field->val_real();
3674  }
3675 
3676  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
3677  if (execute() || null_value) return true;
3678  return sp_result_field->get_date(ltime, fuzzydate);
3679  }
3680 
3681  bool get_time(MYSQL_TIME *ltime) override {
3682  if (execute() || null_value) return true;
3683 
3684  return sp_result_field->get_time(ltime);
3685  }
3686 
3687  my_decimal *val_decimal(my_decimal *dec_buf) override {
3688  if (execute() || null_value) return NULL;
3689  return sp_result_field->val_decimal(dec_buf);
3690  }
3691 
3692  String *val_str(String *str) override {
3693  String buf;
3694  char buff[20];
3695  buf.set(buff, 20, str->charset());
3696  buf.length(0);
3697  if (execute() || null_value) return NULL;
3698  /*
3699  result_field will set buf pointing to internal buffer
3700  of the resul_field. Due to this it will change any time
3701  when SP is executed. In order to prevent occasional
3702  corruption of returned value, we make here a copy.
3703  */
3704  sp_result_field->val_str(&buf);
3705  str->copy(buf);
3706  return str;
3707  }
3708 
3709  bool val_json(Json_wrapper *result) override;
3710 
3711  bool change_context_processor(uchar *cntx) override {
3712  context = reinterpret_cast<Name_resolution_context *>(cntx);
3713  return false;
3714  }
3715 
3716  bool sp_check_access(THD *thd);
3717  enum Functype functype() const override { return FUNC_SP; }
3718 
3719  bool fix_fields(THD *thd, Item **ref) override;
3720  bool resolve_type(THD *thd) override;
3721 
3722  bool is_expensive() override { return true; }
3723 
3725  bool check_function_as_value_generator(uchar *checker_args) override {
3728  checker_args);
3729  func_arg->banned_function_name = func_name();
3730  return true;
3731  }
3732 };
3733 
3736 
3737  public:
3739 
3740  bool itemize(Parse_context *pc, Item **res) override;
3741  longlong val_int() override;
3742  const char *func_name() const override { return "found_rows"; }
3743  bool resolve_type(THD *) override {
3744  maybe_null = false;
3745  return false;
3746  }
3747  bool check_function_as_value_generator(uchar *checker_args) override {
3750  checker_args);
3751  func_arg->banned_function_name = func_name();
3752  return true;
3753  }
3754 };
3755 
3756 void uuid_short_init();
3757 
3760 
3761  public:
3763 
3764  bool itemize(Parse_context *pc, Item **res) override;
3765  const char *func_name() const override { return "uuid_short"; }
3766  longlong val_int() override;
3767  bool resolve_type(THD *) override {
3768  unsigned_flag = true;
3769  return false;
3770  }
3771  bool check_partition_func_processor(uchar *) override { return false; }
3772  bool check_function_as_value_generator(uchar *checker_args) override {
3775  checker_args);
3776  func_arg->banned_function_name = func_name();
3777  return ((func_arg->source == VGS_GENERATED_COLUMN) ||
3778  (func_arg->source == VGS_CHECK_CONSTRAINT));
3779  }
3780 };
3781 
3784 
3785  public:
3786  explicit Item_func_version(const POS &pos);
3787 
3788  bool itemize(Parse_context *pc, Item **res) override;
3789 };
3790 
3792  LEX_STRING component);
3793 extern bool check_reserved_words(LEX_STRING *name);
3794 extern enum_field_types agg_field_type(Item **items, uint nitems);
3795 double my_double_round(double value, longlong dec, bool dec_unsigned,
3796  bool truncate);
3797 bool eval_const_cond(THD *thd, Item *cond, bool *value);
3799  Field **found = NULL);
3800 
3801 void retrieve_tablespace_statistics(THD *thd, Item **args, bool *null_value);
3802 
3803 extern bool volatile mqh_used;
3804 
3805 #endif /* ITEM_FUNC_INCLUDED */
Definition: sql_udf.h:42
bool get_time(MYSQL_TIME *ltime) override
Definition: item_func.h:2047
Definition: item_func.h:1221
longlong val_int() override
Definition: item_func.cc:3565
Definition: item_func.h:2054
const char * func_name() const override
Definition: item_func.h:1575
Definition: item_func.h:2681
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize()/contextualize_() but with additional parameter.
Definition: item_func.cc:4862
Definition: item_func.h:159
longlong int_op() override
Performs the operation that this functions implements when the result type is INT.
Definition: item_func.cc:2946
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1141
my_decimal * val_decimal_from_real(my_decimal *decimal_value)
Definition: item.cc:297
longlong val_int() override
Definition: item_func.cc:8762
Item *(Item::* Item_transformer)(uchar *arg)
Definition: item.h:663
enum Item_result result_type() const override
Definition: item_func.h:1653
Item_func_bit(const POS &pos, Item *a, Item *b)
Definition: item_func.h:1649
enum Functype functype() const override
Definition: item_func.h:1100
Item_func_instr(const POS &pos, Item *a, Item *b)
Definition: item_func.h:1564
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize()/contextualize_() but with additional parameter.
Definition: item_func.cc:7242
Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e)
Definition: item_func.h:316
const char * func_name() const override
Definition: item_func.h:976
Item_real_func(Item *a)
Definition: item_func.h:637
bool date_op(MYSQL_TIME *, my_time_flags_t) override
Performs the operation that this functions implements when the result type is MYSQL_TYPE_DATE or MYSQ...
Definition: item_func.h:781
Definition: item.h:3349
Wrapper for struct ft_hints.
Definition: handler.h:3522
unsigned long long int ulonglong
Definition: my_inttypes.h:74
Definition: item_func.h:993
Item_name_string item_name
Name from query.
Definition: item.h:2654
This class is used for implementing the new wait_for_executed_gtid_set function and the functions rel...
Definition: item_func.h:2210
Definition: field.h:4906
not valid for UDFs
Definition: udf_registration_types.h:44
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.h:2774
virtual bool time_op(MYSQL_TIME *ltime)=0
Definition: item_func.h:145
Item_wait_for_executed_gtid_set(const POS &pos, Item *a)
Definition: item_func.h:2216
void init(THD *thd, const Simple_cstring &name, const CHARSET_INFO *cs)
Initialize all members.
Definition: item_func.cc:5414
double val_real() override
Definition: item_func.cc:2515
bool get_date_from_int(MYSQL_TIME *ltime, my_time_flags_t flags)
Convert val_int() to date in MYSQL_TIME.
Definition: item.cc:1217
void set_hints(JOIN *join, uint ft_flag, ha_rows ft_limit, bool no_cond)
Set FT hints.
Definition: item_func.cc:7162
Item_func_can_access_view(const POS &pos, Item *a, Item *b, Item *c, Item *d)
Definition: item_func.h:2352
enum Functype functype() const override
Definition: item_func.h:2707
Class used to implement CAST to floating-point data types.
Definition: item_func.h:959
enum Functype functype() const override
Definition: item_func.h:1006
~Audit_global_variable_get_event()
Definition: item_func.cc:6454
double val_real() override
Definition: item_func.cc:2482
String search_value
Definition: item_func.h:3287
double real_op() override
Performs the operation that this functions implements when the result type is REAL.
Definition: item_func.cc:2272
virtual double val_real()=0
longlong val_int() override
Definition: item_func.cc:1388
Definition: item_func.h:125
const char * func_name() const override
Definition: item_func.h:1536
Item_func_can_access_table(const POS &pos, Item *a, Item *b)
Definition: item_func.h:2290
enum Functype functype() const override
Definition: item_func.h:1441
const char * ptr() const
Definition: item_func.h:2982
enum Functype functype() const override
Definition: item_func.h:929
Item_real_func(const POS &pos, PT_item_list *list)
Definition: item_func.h:650
unsigned char uchar
Definition: my_inttypes.h:51
Definition: sql_optimizer.h:177
Item_func(Item *a, Item *b, Item *c, Item *d)
Definition: item_func.h:275
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_func.cc:1652
double val_real() override
Definition: item_func.cc:5807
double val_real() override
Definition: item_func.cc:2501
Our own string classes, used pervasively throughout the executor.
double val_real() override
Definition: item_func.cc:2494
bool check_partition_func_processor(uchar *) override
Check if a partition function is allowed.
Definition: item_func.h:1086
bool binary_result_requires_binary_second_arg() const override
Definition: item_func.h:1798
enum Functype functype() const override
Definition: item_func.h:2727
char * internal_buffer_ptr()
Position inside a user_var_entry where small values are stored: double values, longlong values and st...
Definition: item_func.h:2840
longlong int_op() override
Performs the operation that this functions implements when the result type is INT.
Definition: item_func.cc:2278
Item_int_func()
Definition: item_func.h:822
optimize_type
Definition: item_func.h:213
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_func.h:2024
Item_int_func super
Definition: item_func.h:2177
void result_precision() override
Definition: item_func.cc:2254
"public" interface to sys_var - server configuration variables.
t pos
Definition: dbug_analyze.cc:148
Item_result
Type of the user defined function return slot and arguments
Definition: udf_registration_types.h:38
my_decimal * vdec
Definition: item_func.h:3027
longlong raise_integer_overflow()
Definition: item.h:4639
Definition: item_func.h:140
not valid for UDFs
Definition: udf_registration_types.h:40
enum Functype functype() const override
Definition: item_func.h:1170
enum Item_result result_type() const override
Definition: item_func.h:1384
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.h:1500
bool change_context_processor(uchar *cntx) override
Definition: item_func.h:3711
const char * db_name
Definition: rules_table_service.cc:54
String * str_op(String *str) override
Performs the operation on binary strings to produce a result of type STRING_RESULT.
Definition: item_func.h:1719
Item_func_numhybrid(const POS &pos, Item *a, Item *b)
Definition: item_func.h:687
Definition: item_func.h:1057
String * val_str(String *str) override
Definition: item_func.cc:1675
Item_func_can_access_event(const POS &pos, Item *a)
Definition: item_func.h:2328
ulonglong table_map
Definition: my_table_map.h:32
Item_dec_func(const POS &pos, Item *a, Item *b)
Definition: item_func.h:1130
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_func.cc:4144
const char * func_name() const override
Definition: item_func.h:1162
int check_decimal_overflow(int error)
Throw an error if the error code of a DECIMAL operation is E_DEC_OVERFLOW.
Definition: item_func.h:466
const char * func_name() const override
Definition: item_func.h:2159
const char * func_name() const override
Definition: item_func.h:1200
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.h:2456
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.h:949
Definition: item_func.h:1436
Table_flags ha_table_flags() const
The cached_table_flags is set at ha_open and ha_external_lock.
Definition: handler.h:4328
Item_func(List< Item > &list)
Definition: item_func.h:327
uint decimal_precision() const override
Definition: item_func.h:2121
enum_field_types data_type() const
Retrieve the derived data type of the Item.
Definition: item.h:1007
const char * func_name() const override
Definition: item_func.h:1728
Definition: mysql_lex_string.h:34
THD * m_thd
Definition: item_func.h:3195
void set_value(char *value, size_t length)
Definition: item_func.h:2828
T pointer_cast(void *p)
Casts from one pointer type, to another, without using reinterpret_cast or C-style cast: foo f; bar *...
Definition: template_utils.h:70
String value1
Definition: item_func.h:1545
Definition: item_func.h:3589
const char * func_name() const override
Definition: item_func.h:1275
my_decimal * decimal_op(my_decimal *) override
Performs the operation that this functions implements when the result type is DECIMAL.
Definition: item_func.cc:2937
bool time_op(MYSQL_TIME *) override
Definition: item_func.h:785
bool agg_item_charsets_for_string_result_with_comparison(DTCollation &c, const char *name, Item **items, uint nitems, int item_sep=1)
Definition: item.h:3112
longlong int_op() override
Performs the operation that this functions implements when the result type is INT.
Definition: item_func.cc:1936
bool get_time(MYSQL_TIME *ltime) override
Definition: item_func.h:944
Definition: item_func.h:2538
String * str_op(String *str) override
Performs the operation on binary strings to produce a result of type STRING_RESULT.
Definition: item_func.h:1732
Item_int_func super
Definition: item_func.h:2152
enum Functype functype() const override
Definition: item_func.h:916
bool is_temporal_type_with_date(enum_field_types type)
Tests if field type is temporal and has date part, i.e.
Definition: field.h:431
longlong int_op() override
Performs the operation on integers to produce a result of type INT_RESULT.
Definition: item_func.h:1792
Item_func_min_max(const POS &pos, PT_item_list *opt_list, bool is_least_func)
Definition: item_func.h:1368
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.cc:3728
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize()/contextualize_() but with additional parameter.
Definition: item_func.cc:7278
const char * func_name() const
Definition: item_func.h:2381
Item_func_mod(Item *a, Item *b)
Definition: item_func.h:1077
String * val_str(bool *null_value, String *str, uint decimals) const
Get the value of a variable as a string.
Definition: item_func.cc:5601
void set_type(Item_result type)
Set type of to the given value.
Definition: item_func.h:2939
longlong val_int() override
INFORMATION_SCHEMA picks metadata from DD using system views.
Definition: item_func.cc:8205
Definition: item_func.h:1091
longlong int_op() override
Performs the operation on integers to produce a result of type INT_RESULT.
Definition: item_func.h:1731
Definition: item_func.h:3097
Item_func_internal_dd_char_length(const POS &pos, Item *a, Item *b, Item *c, Item *d)
Definition: item_func.h:2540
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.cc:2265
String * vstr
Definition: item_func.h:3026
Definition: item_func.h:161
void set_data_type_from_item(Item *item)
Set data type properties of the item from the properties of another item.
Definition: item.h:1273
const char * func_name() const override
Definition: item_func.h:1099
virtual bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)=0
Performs the operation that this functions implements when the result type is MYSQL_TYPE_DATE or MYSQ...
int string2my_decimal(uint mask, const String *str, my_decimal *d)
Definition: my_decimal.h:357
enum Functype functype() const override
Definition: item_func.h:2607
bool date_op(MYSQL_TIME *, my_time_flags_t) override
Performs the operation that this functions implements when the result type is MYSQL_TYPE_DATE or MYSQ...
Definition: item_func.h:810
bool compare_as_dates() const
Returns true if arguments to this function should be compared as dates.
Definition: item_func.h:1395
String * val_str(String *str) override
Definition: item_func.cc:1599
Item_int_func super
Definition: item_func.h:877
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.h:3599
Definition: mysql_com.h:1006
bool check_function_as_value_generator(uchar *checker_args) override
Check if this item is allowed for a virtual column or inside a default expression.
Definition: item_func.h:1862
bool get_time_from_real(MYSQL_TIME *ltime)
Convert val_real() to time in MYSQL_TIME.
Definition: item.cc:1286
Definition: item_func.h:1075
Item_func_tan(const POS &pos, Item *a)
Definition: item_func.h:1231
Definition: item_func.h:192
Definition: item_func.h:1258
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_func.h:2002
my_decimal * decimal_op(my_decimal *) override
See Item_func_plus::decimal_op for comments.
Definition: item_func.cc:2006
Definition: item_func.h:137
#define SUPPRESS_UBSAN
Definition: my_compiler.h:130
const string name("\ame\)
bool check_function_as_value_generator(uchar *checker_args) override
Check if this item is allowed for a virtual column or inside a default expression.
Definition: item_func.h:3772
longlong val_int() override
INFORMATION_SCHEMA picks metadata from DD using system views.
Definition: item_func.cc:7810
const char * func_name() const override
Definition: item_func.h:2293
char * name_ptr()
Position inside a user_var_entry where a null-terminates array of characters representing the variabl...
Definition: item_func.h:2848
Definition: item.h:705
Field * get_sp_result_field()
Definition: item_func.h:3724
enum Item_result result_type() const override
Definition: item_func.cc:6416
Definition: item_func.h:1110
bool agg_item_charsets(DTCollation &coll, const char *fname, Item **args, uint nargs, uint flags, int item_sep)
Definition: item.cc:2332
void fix_num_length_and_dec() override
Definition: item_func.cc:2849
void cleanup() override
Definition: item_func.cc:413