MySQL  8.0.16
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 {
203  };
210  };
211  enum Type type() const override { return FUNC_ITEM; }
212  virtual enum Functype functype() const { return UNKNOWN_FUNC; }
214 
215  explicit Item_func(const POS &pos) : super(pos), arg_count(0) {
216  args = tmp_arg;
217  }
218 
220  args = tmp_arg;
221  args[0] = a;
223  }
224  Item_func(const POS &pos, Item *a) : super(pos), arg_count(1) {
225  args = tmp_arg;
226  args[0] = a;
227  }
228 
229  Item_func(Item *a, Item *b) : arg_count(2) {
230  args = tmp_arg;
231  args[0] = a;
232  args[1] = b;
233  m_accum_properties = 0;
236  }
237  Item_func(const POS &pos, Item *a, Item *b) : super(pos), arg_count(2) {
238  args = tmp_arg;
239  args[0] = a;
240  args[1] = b;
241  }
242 
243  Item_func(Item *a, Item *b, Item *c) : arg_count(3) {
244  if ((args = (Item **)sql_alloc(sizeof(Item *) * 3))) {
245  args[0] = a;
246  args[1] = b;
247  args[2] = c;
248  m_accum_properties = 0;
252  } else
253  arg_count = 0; // OOM
254  }
255 
256  Item_func(const POS &pos, Item *a, Item *b, Item *c)
257  : super(pos), arg_count(3) {
258  if ((args = (Item **)sql_alloc(sizeof(Item *) * 3))) {
259  args[0] = a;
260  args[1] = b;
261  args[2] = c;
262  } else
263  arg_count = 0; // OOM
264  }
265 
266  Item_func(Item *a, Item *b, Item *c, Item *d) : arg_count(4) {
267  if ((args = (Item **)sql_alloc(sizeof(Item *) * 4))) {
268  args[0] = a;
269  args[1] = b;
270  args[2] = c;
271  args[3] = d;
272  m_accum_properties = 0;
277  } else
278  arg_count = 0; // OOM
279  }
280  Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
281  : super(pos), arg_count(4) {
282  if ((args = (Item **)sql_alloc(sizeof(Item *) * 4))) {
283  args[0] = a;
284  args[1] = b;
285  args[2] = c;
286  args[3] = d;
287  } else
288  arg_count = 0; // OOM
289  }
290 
291  Item_func(Item *a, Item *b, Item *c, Item *d, Item *e) : arg_count(5) {
292  if ((args = (Item **)sql_alloc(sizeof(Item *) * 5))) {
293  args[0] = a;
294  args[1] = b;
295  args[2] = c;
296  args[3] = d;
297  args[4] = e;
298  m_accum_properties = 0;
304  } else
305  arg_count = 0; // OOM
306  }
307  Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e)
308  : super(pos), arg_count(5) {
309  if ((args = (Item **)sql_alloc(sizeof(Item *) * 5))) {
310  args[0] = a;
311  args[1] = b;
312  args[2] = c;
313  args[3] = d;
314  args[4] = e;
315  } else
316  arg_count = 0; // OOM
317  }
319 
320  Item_func(const POS &pos, PT_item_list *opt_list);
321 
322  // Constructor used for Item_cond_and/or (see Item comment)
323  Item_func(THD *thd, Item_func *item);
324 
325  bool itemize(Parse_context *pc, Item **res) override;
326 
327  bool fix_fields(THD *, Item **ref) override;
328  bool fix_func_arg(THD *, Item **arg);
329  void fix_after_pullout(SELECT_LEX *parent_select,
330  SELECT_LEX *removed_select) override;
331  /**
332  Returns the pseudo tables depended upon in order to evaluate this
333  function expression. The default implementation returns the empty
334  set.
335  */
336  virtual table_map get_initial_pseudo_tables() const { return 0; }
337  table_map used_tables() const override { return used_tables_cache; }
338  table_map not_null_tables() const override { return not_null_tables_cache; }
339  void update_used_tables() override;
341  bool eq(const Item *item, bool binary_cmp) const override;
342  virtual optimize_type select_optimize() const { return OPTIMIZE_NONE; }
343  virtual bool have_rev_func() const { return 0; }
344  virtual Item *key_item() const { return args[0]; }
345  inline Item **arguments() const {
347  return args;
348  }
349  /**
350  Copy arguments from list to args array
351 
352  @param list function argument list
353  @param context_free true: for use in context-independent
354  constructors (Item_func(POS,...)) i.e. for use
355  in the parser
356  */
357  void set_arguments(List<Item> &list, bool context_free);
358  inline uint argument_count() const { return arg_count; }
359  void split_sum_func(THD *thd, Ref_item_array ref_item_array,
360  List<Item> &fields) override;
361  void print(const THD *thd, String *str,
362  enum_query_type query_type) const override;
363  void print_op(const THD *thd, String *str, enum_query_type query_type) const;
364  void print_args(const THD *thd, String *str, uint from,
365  enum_query_type query_type) const;
366  virtual void fix_num_length_and_dec();
367  virtual bool is_deprecated() const { return false; }
368  bool get_arg0_date(MYSQL_TIME *ltime, my_time_flags_t fuzzy_date) {
369  return (null_value = args[0]->get_date(ltime, fuzzy_date));
370  }
371  inline bool get_arg0_time(MYSQL_TIME *ltime) {
372  return (null_value = args[0]->get_time(ltime));
373  }
374  bool is_null() override {
375  /*
376  TODO : Implement error handling for this function as
377  update_null_value() can return error.
378  */
379  (void)update_null_value();
380  return null_value;
381  }
382  void signal_divide_by_null();
384  friend class udf_handler;
386  Field *tmp_table_field(TABLE *t_arg) override;
387  Item *get_tmp_table_item(THD *thd) override;
388 
389  my_decimal *val_decimal(my_decimal *) override;
390 
391  /**
392  Same as save_in_field() except that special logic is added to
393  avoid serialization to string followed by parsing of the string
394  when saving a JSON value in a JSON column.
395 
396  Unless both the return type of the function and the type of the
397  target column are JSON, this function works exactly as
398  save_in_field(). For the JSON type, this means:
399 
400  - JSON values saved in non-JSON columns: The JSON value is
401  serialized to a character string and then attempted saved in the
402  target column. The usual conversions are performed if the target
403  column is not a character string column.
404 
405  - Non-JSON values saved in JSON columns: Strings are parsed as
406  JSON text, converted to JSON binary representation and saved in
407  the target column. Non-strings cause a conversion error to be
408  raised.
409 
410  A better solution might be to put this logic into
411  Item_func::save_in_field_inner() or even Item::save_in_field_inner().
412  But that would mean providing val_json() overrides for
413  more Item subclasses. And that feels like pulling on a
414  ball of yarn late in the release cycle for 5.7. FIXME.
415 
416  @param[out] field The field to set the value to.
417  @param no_conversions Passed to save_in_field_inner().
418 
419  @retval 0 On success.
420  @retval >0 On error.
421  */
423  bool no_conversions);
424 
426  int item_sep) {
427  return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep);
428  }
429  /*
430  Aggregate arguments for string result, e.g: CONCAT(a,b)
431  - convert to @@character_set_connection if all arguments are numbers
432  - allow DERIVATION_NONE
433  */
435  uint nitems, int item_sep = 1) {
437  item_sep);
438  }
439  /*
440  Aggregate arguments for comparison, e.g: a=b, a LIKE b, a RLIKE b
441  - don't convert to @@character_set_connection if all arguments are numbers
442  - don't allow DERIVATION_NONE
443  */
445  uint nitems, int item_sep = 1) {
446  return agg_item_charsets_for_comparison(c, func_name(), items, nitems,
447  item_sep);
448  }
449  /*
450  Aggregate arguments for string result, when some comparison
451  is involved internally, e.g: REPLACE(a,b,c)
452  - convert to @@character_set_connection if all arguments are numbers
453  - disallow DERIVATION_NONE
454  */
456  Item **items,
457  uint nitems,
458  int item_sep = 1) {
460  c, func_name(), items, nitems, item_sep);
461  }
462  bool walk(Item_processor processor, enum_walk walk, uchar *arg) override;
463  Item *transform(Item_transformer transformer, uchar *arg) override;
464  Item *compile(Item_analyzer analyzer, uchar **arg_p,
465  Item_transformer transformer, uchar *arg_t) override;
466  void traverse_cond(Cond_traverser traverser, void *arg,
467  traverse_order order) override;
468 
469  /**
470  Throw an error if the input double number is not finite, i.e. is either
471  +/-INF or NAN.
472  */
473  inline double check_float_overflow(double value) {
474  return std::isfinite(value) ? value : raise_float_overflow();
475  }
476  /**
477  Throw an error if the input BIGINT value represented by the
478  (longlong value, bool unsigned flag) pair cannot be returned by the
479  function, i.e. is not compatible with this Item's unsigned_flag.
480  */
481  inline longlong check_integer_overflow(longlong value, bool val_unsigned) {
482  if ((unsigned_flag && !val_unsigned && value < 0) ||
483  (!unsigned_flag && val_unsigned &&
484  (ulonglong)value > (ulonglong)LLONG_MAX))
485  return raise_integer_overflow();
486  return value;
487  }
488  /**
489  Throw an error if the error code of a DECIMAL operation is E_DEC_OVERFLOW.
490  */
491  inline int check_decimal_overflow(int error) {
493  }
494 
497  for (uint i = 0; i < arg_count; i++) {
498  if (args[i]->type() == Item::FIELD_ITEM &&
500  return true;
501  }
502  return false;
503  }
504 
505  bool has_date_args() {
507  for (uint i = 0; i < arg_count; i++) {
508  if (args[i]->type() == Item::FIELD_ITEM &&
509  (args[i]->data_type() == MYSQL_TYPE_DATE ||
511  return true;
512  }
513  return false;
514  }
515 
516  bool has_time_args() {
518  for (uint i = 0; i < arg_count; i++) {
519  if (args[i]->type() == Item::FIELD_ITEM &&
520  (args[i]->data_type() == MYSQL_TYPE_TIME ||
522  return true;
523  }
524  return false;
525  }
526 
529  for (uint i = 0; i < arg_count; i++) {
530  if (args[i]->type() == Item::FIELD_ITEM &&
532  return true;
533  }
534  return false;
535  }
536 
537  /*
538  We assume the result of any function that has a TIMESTAMP argument to be
539  timezone-dependent, since a TIMESTAMP value in both numeric and string
540  contexts is interpreted according to the current timezone.
541  The only exception is UNIX_TIMESTAMP() which returns the internal
542  representation of a TIMESTAMP argument verbatim, and thus does not depend on
543  the timezone.
544  */
546  return has_timestamp_args();
547  }
548 
549  Item *gc_subst_transformer(uchar *arg) override;
550 
551  /**
552  Does essentially the same as THD::change_item_tree, plus
553  maintains any necessary any invariants.
554  */
555  virtual void replace_argument(THD *thd, Item **oldpp, Item *newp);
556 
557  protected:
558  /**
559  Whether or not an item should contribute to the filtering effect
560  (@see get_filtering_effect()). First it verifies that table
561  requirements are satisfied as follows:
562 
563  1) The item must refer to a field in 'filter_for_table' in some
564  way. This reference may be indirect through any number of
565  intermediate items. For example, this item may be an
566  Item_cond_and which refers to an Item_func_eq which refers to
567  the field.
568  2) The item must not refer to other tables than those already
569  read and the table in 'filter_for_table'
570 
571  Then it contines to other properties as follows:
572 
573  Item_funcs represent "<operand1> OP <operand2> [OP ...]". If the
574  Item_func is to contribute to the filtering effect, then
575 
576  1) one of the operands must be a field from 'filter_for_table' that is not
577  in 'fields_to_ignore', and
578  2) depending on the Item_func type filtering effect is calculated
579  for, one or all [1] of the other operand(s) must be an available
580  value, i.e.:
581  - a constant, or
582  - a constant subquery, or
583  - a field value read from a table in 'read_tables', or
584  - a second field in 'filter_for_table', or
585  - a function that only refers to constants or tables in
586  'read_tables', or
587  - special case: an implicit value like NULL in the case of
588  "field IS NULL". Such Item_funcs have arg_count==1.
589 
590  [1] "At least one" for multiple equality (X = Y = Z = ...), "all"
591  for the rest (e.g. BETWEEN)
592 
593  @param read_tables Tables earlier in the join sequence.
594  Predicates for table 'filter_for_table' that
595  rely on values from these tables can be part of
596  the filter effect.
597  @param filter_for_table The table we are calculating filter effect for
598  @param fields_to_ignore Columns that should be ignored.
599 
600 
601  @return Item_field that participates in the predicate if none of the
602  requirements are broken, NULL otherwise
603 
604  @note: This function only applies to items doing comparison, i.e.
605  boolean predicates. Unfortunately, some of those items do not
606  inherit from Item_bool_func so the member function has to be
607  placed in Item_func.
608  */
610  table_map read_tables, table_map filter_for_table,
611  const MY_BITMAP *fields_to_ignore) const;
612  /**
613  Named parameters are allowed in a parameter list
614 
615  The syntax to name parameters in a function call is as follow:
616  <code>foo(expr AS named, expr named, expr AS "named", expr "named")</code>
617  where "AS" is optional.
618  Only UDF function support that syntax.
619 
620  @return true if the function item can have named parameters
621  */
622  virtual bool may_have_named_parameters() const { return false; }
623  virtual bool is_non_const_over_literals(uchar *) override { return false; }
624 
625  bool check_function_as_value_generator(uchar *checker_args) override {
626  if (is_deprecated()) {
629  checker_args);
630  func_arg->banned_function_name = func_name();
631  return true;
632  }
633  return false;
634  }
635 };
636 
637 class Item_real_func : public Item_func {
638  public:
640  explicit Item_real_func(const POS &pos) : Item_func(pos) {
642  }
643 
645  Item_real_func(const POS &pos, Item *a) : Item_func(pos, a) {
647  }
648 
650 
651  Item_real_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
653  }
654 
656 
659  }
660 
661  String *val_str(String *str) override;
662  my_decimal *val_decimal(my_decimal *decimal_value) override;
663  longlong val_int() override {
666  }
667  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
668  return get_date_from_real(ltime, fuzzydate);
669  }
670  bool get_time(MYSQL_TIME *ltime) override {
671  return get_time_from_real(ltime);
672  }
673  enum Item_result result_type() const override { return REAL_RESULT; }
674  bool resolve_type(THD *) override { return false; }
675 };
676 
678  protected:
680 
681  public:
684  }
688  }
689 
691  : Item_func(a, b), hybrid_type(REAL_RESULT) {
693  }
695  : Item_func(pos, a, b), hybrid_type(REAL_RESULT) {
697  }
698 
702  }
706  }
707 
708  enum Item_result result_type() const override { return hybrid_type; }
709  bool resolve_type(THD *thd) override;
710  void fix_num_length_and_dec() override;
711  virtual void set_numeric_type() = 0; // To be called from resolve_type()
712 
713  double val_real() override;
714  longlong val_int() override;
715  my_decimal *val_decimal(my_decimal *) override;
716  String *val_str(String *str) override;
717  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
718  bool get_time(MYSQL_TIME *ltime) override;
719  /**
720  @brief Performs the operation that this functions implements when the
721  result type is INT.
722 
723  @return The result of the operation.
724  */
725  virtual longlong int_op() = 0;
726 
727  /**
728  @brief Performs the operation that this functions implements when the
729  result type is REAL.
730 
731  @return The result of the operation.
732  */
733  virtual double real_op() = 0;
734 
735  /**
736  @brief Performs the operation that this functions implements when the
737  result type is DECIMAL.
738 
739  @param decimal_value A pointer where the DECIMAL value will be allocated.
740  @return
741  - 0 If the result is NULL
742  - The same pointer it was given, with the area initialized to the
743  result of the operation.
744  */
745  virtual my_decimal *decimal_op(my_decimal *decimal_value) = 0;
746 
747  /**
748  @brief Performs the operation that this functions implements when the
749  result type is a string type.
750 
751  @return The result of the operation.
752  */
753  virtual String *str_op(String *) = 0;
754  /**
755  @brief Performs the operation that this functions implements when the
756  result type is MYSQL_TYPE_DATE or MYSQL_TYPE_DATETIME.
757 
758  @return The result of the operation.
759  */
760  virtual bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) = 0;
761  virtual bool time_op(MYSQL_TIME *ltime) = 0;
762  bool is_null() override {
763  /*
764  TODO : Implement error handling for this function as
765  update_null_value() can return error.
766  */
767  (void)update_null_value();
768  return null_value;
769  }
770 };
771 
772 /* function where type of result detected by first argument */
774  public:
777 
779  Item_func_num1(const POS &pos, Item *a, Item *b)
780  : Item_func_numhybrid(pos, a, b) {}
781 
782  void fix_num_length_and_dec() override;
783  void set_numeric_type() override;
784  String *str_op(String *) override {
785  DBUG_ASSERT(0);
786  return 0;
787  }
788  bool date_op(MYSQL_TIME *, my_time_flags_t) override {
789  DBUG_ASSERT(0);
790  return 0;
791  }
792  bool time_op(MYSQL_TIME *) override {
793  DBUG_ASSERT(0);
794  return 0;
795  }
796 };
797 
798 /* Base class for operations like '+', '-', '*' */
800  public:
802  Item_num_op(const POS &pos, Item *a, Item *b)
803  : Item_func_numhybrid(pos, a, b) {}
804 
805  virtual void result_precision() = 0;
806 
807  void print(const THD *thd, String *str,
808  enum_query_type query_type) const override {
809  print_op(thd, str, query_type);
810  }
811 
812  void set_numeric_type() override;
813  String *str_op(String *) override {
814  DBUG_ASSERT(0);
815  return 0;
816  }
817  bool date_op(MYSQL_TIME *, my_time_flags_t) override {
818  DBUG_ASSERT(0);
819  return 0;
820  }
821  bool time_op(MYSQL_TIME *) override {
822  DBUG_ASSERT(0);
823  return 0;
824  }
825 };
826 
827 class Item_int_func : public Item_func {
828  public:
830  explicit Item_int_func(const POS &pos) : Item_func(pos) {
832  }
833 
835  Item_int_func(const POS &pos, Item *a) : Item_func(pos, a) {
837  }
838 
839  Item_int_func(Item *a, Item *b) : Item_func(a, b) {
841  }
842  Item_int_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
844  }
845 
846  Item_int_func(Item *a, Item *b, Item *c) : Item_func(a, b, c) {
848  }
849  Item_int_func(const POS &pos, Item *a, Item *b, Item *c)
850  : Item_func(pos, a, b, c) {
852  }
853 
854  Item_int_func(Item *a, Item *b, Item *c, Item *d) : Item_func(a, b, c, d) {
856  }
857  Item_int_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
858  : Item_func(pos, a, b, c, d) {
860  }
861 
864  }
865  Item_int_func(const POS &pos, PT_item_list *opt_list)
866  : Item_func(pos, opt_list) {
868  }
869 
870  Item_int_func(THD *thd, Item_int_func *item) : Item_func(thd, item) {
872  }
873  double val_real() override;
874  String *val_str(String *str) override;
875  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
876  return get_date_from_int(ltime, fuzzydate);
877  }
878  bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
879  enum Item_result result_type() const override { return INT_RESULT; }
880  bool resolve_type(THD *) override { return false; }
881 };
882 
885 
887 
888  public:
890 
891  bool itemize(Parse_context *pc, Item **res) override;
892  const char *func_name() const override { return "connection_id"; }
893  bool resolve_type(THD *thd) override;
894  bool fix_fields(THD *thd, Item **ref) override;
895  longlong val_int() override {
897  return value;
898  }
899  bool check_function_as_value_generator(uchar *checker_args) override {
902  checker_args);
903  func_arg->banned_function_name = func_name();
904  return ((func_arg->source == VGS_GENERATED_COLUMN) ||
905  (func_arg->source == VGS_CHECK_CONSTRAINT));
906  }
907 };
908 
910  public:
912  unsigned_flag = false;
913  }
914  const char *func_name() const override { return "cast_as_signed"; }
915  longlong val_int() override;
917  bool resolve_type(THD *thd) override;
918  void print(const THD *thd, String *str,
919  enum_query_type query_type) const override;
920  uint decimal_precision() const override {
921  return args[0]->decimal_precision();
922  }
923  enum Functype functype() const override { return TYPECAST_FUNC; }
924 };
925 
927  public:
929  unsigned_flag = 1;
930  }
931  const char *func_name() const override { return "cast_as_unsigned"; }
932  longlong val_int() override;
933  void print(const THD *thd, String *str,
934  enum_query_type query_type) const override;
935  enum Functype functype() const override { return TYPECAST_FUNC; }
936 };
937 
940 
941  public:
942  Item_decimal_typecast(const POS &pos, Item *a, int len, int dec)
943  : Item_func(pos, a) {
945  }
946  String *val_str(String *str) override;
947  double val_real() override;
948  longlong val_int() override;
949  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
950  return get_date_from_decimal(ltime, fuzzydate);
951  }
952  bool get_time(MYSQL_TIME *ltime) override {
953  return get_time_from_decimal(ltime);
954  }
955  my_decimal *val_decimal(my_decimal *) override;
956  enum Item_result result_type() const override { return DECIMAL_RESULT; }
957  bool resolve_type(THD *) override { return false; }
958  const char *func_name() const override { return "decimal_typecast"; }
959  enum Functype functype() const override { return TYPECAST_FUNC; }
960  void print(const THD *thd, String *str,
961  enum_query_type query_type) const override;
962 };
963 
965  public:
968  : Item_num_op(pos, a, b) {}
969 
970  void result_precision() override;
971  bool check_partition_func_processor(uchar *) override { return false; }
972  bool check_function_as_value_generator(uchar *) override { return false; }
973 };
974 
976  public:
978  Item_func_plus(const POS &pos, Item *a, Item *b)
979  : Item_func_additive_op(pos, a, b) {}
980 
981  const char *func_name() const override { return "+"; }
982 
983  // SUPPRESS_UBSAN: signed integer overflow
984  longlong int_op() override SUPPRESS_UBSAN;
985 
986  double real_op() override;
987  my_decimal *decimal_op(my_decimal *) override;
988  enum Functype functype() const override { return PLUS_FUNC; }
989 };
990 
992  public:
994  Item_func_minus(const POS &pos, Item *a, Item *b)
995  : Item_func_additive_op(pos, a, b) {}
996 
997  const char *func_name() const override { return "-"; }
998 
999  // SUPPRESS_UBSAN: signed integer overflow
1000  longlong int_op() override SUPPRESS_UBSAN;
1001 
1002  double real_op() override;
1003  my_decimal *decimal_op(my_decimal *) override;
1004  bool resolve_type(THD *thd) override;
1005  enum Functype functype() const override { return MINUS_FUNC; }
1006 };
1007 
1009  public:
1010  Item_func_mul(Item *a, Item *b) : Item_num_op(a, b) {}
1011  Item_func_mul(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1012 
1013  const char *func_name() const override { return "*"; }
1014  longlong int_op() override;
1015  double real_op() override;
1016  my_decimal *decimal_op(my_decimal *) override;
1017  void result_precision() override;
1018  bool check_partition_func_processor(uchar *) override { return false; }
1019  bool check_function_as_value_generator(uchar *) override { return false; }
1020  enum Functype functype() const override { return MUL_FUNC; }
1021 };
1022 
1024  public:
1026  Item_func_div(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1027  longlong int_op() override {
1028  DBUG_ASSERT(0);
1029  return 0;
1030  }
1031  double real_op() override;
1032  my_decimal *decimal_op(my_decimal *) override;
1033  const char *func_name() const override { return "/"; }
1034  bool resolve_type(THD *thd) override;
1035  void result_precision() override;
1036  enum Functype functype() const override { return DIV_FUNC; }
1037 };
1038 
1040  public:
1043  : Item_int_func(pos, a, b) {}
1044  longlong val_int() override;
1045  const char *func_name() const override { return "DIV"; }
1046  bool resolve_type(THD *thd) override;
1047 
1048  void print(const THD *thd, String *str,
1049  enum_query_type query_type) const override {
1050  print_op(thd, str, query_type);
1051  }
1052 
1053  bool check_partition_func_processor(uchar *) override { return false; }
1054  bool check_function_as_value_generator(uchar *) override { return false; }
1055 };
1056 
1058  public:
1059  Item_func_mod(Item *a, Item *b) : Item_num_op(a, b) {}
1060  Item_func_mod(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1061 
1062  longlong int_op() override;
1063  double real_op() override;
1064  my_decimal *decimal_op(my_decimal *) override;
1065  const char *func_name() const override { return "%"; }
1066  void result_precision() override;
1067  bool resolve_type(THD *thd) override;
1068  bool check_partition_func_processor(uchar *) override { return false; }
1069  bool check_function_as_value_generator(uchar *) override { return false; }
1070  enum Functype functype() const override { return MOD_FUNC; }
1071 };
1072 
1074  public:
1077 
1078  double real_op() override;
1079  longlong int_op() override;
1080  my_decimal *decimal_op(my_decimal *) override;
1081  const char *func_name() const override { return "-"; }
1082  enum Functype functype() const override { return NEG_FUNC; }
1083  bool resolve_type(THD *thd) override;
1084  void fix_num_length_and_dec() override;
1085  uint decimal_precision() const override {
1086  return args[0]->decimal_precision();
1087  }
1088  bool check_partition_func_processor(uchar *) override { return false; }
1089  bool check_function_as_value_generator(uchar *) override { return false; }
1090 };
1091 
1093  public:
1095  double real_op() override;
1096  longlong int_op() override;
1097  my_decimal *decimal_op(my_decimal *) override;
1098  const char *func_name() const override { return "abs"; }
1099  bool resolve_type(THD *) override;
1100  bool check_partition_func_processor(uchar *) override { return false; }
1101  bool check_function_as_value_generator(uchar *) override { return false; }
1102  enum Functype functype() const override { return ABS_FUNC; }
1103 };
1104 
1105 // A class to handle logarithmic and trigonometric functions
1106 
1108  public:
1111 
1112  Item_dec_func(const POS &pos, Item *a, Item *b) : Item_real_func(pos, a, b) {}
1113  bool resolve_type(THD *thd) override;
1114 };
1115 
1117  public:
1119  double val_real() override;
1120  const char *func_name() const override { return "exp"; }
1121 };
1122 
1124  public:
1125  Item_func_ln(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1126  double val_real() override;
1127  const char *func_name() const override { return "ln"; }
1128 };
1129 
1131  public:
1133  Item_func_log(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1134  double val_real() override;
1135  const char *func_name() const override { return "log"; }
1136  enum Functype functype() const override { return LOG_FUNC; }
1137 };
1138 
1140  public:
1142  double val_real() override;
1143  const char *func_name() const override { return "log2"; }
1144 };
1145 
1147  public:
1149  double val_real() override;
1150  const char *func_name() const override { return "log10"; }
1151 };
1152 
1154  public:
1156  double val_real() override;
1157  const char *func_name() const override { return "sqrt"; }
1158  enum Functype functype() const override { return SQRT_FUNC; }
1159 };
1160 
1162  public:
1163  Item_func_pow(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1164  double val_real() override;
1165  const char *func_name() const override { return "pow"; }
1166 };
1167 
1169  public:
1171  double val_real() override;
1172  const char *func_name() const override { return "acos"; }
1173  enum Functype functype() const override { return ACOS_FUNC; }
1174 };
1175 
1177  public:
1179  double val_real() override;
1180  const char *func_name() const override { return "asin"; }
1181  enum Functype functype() const override { return ASIN_FUNC; }
1182 };
1183 
1185  public:
1187  Item_func_atan(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1188  double val_real() override;
1189  const char *func_name() const override { return "atan"; }
1190  enum Functype functype() const override { return ATAN_FUNC; }
1191 };
1192 
1194  public:
1196  double val_real() override;
1197  const char *func_name() const override { return "cos"; }
1198  enum Functype functype() const override { return COS_FUNC; }
1199 };
1200 
1202  public:
1204  double val_real() override;
1205  const char *func_name() const override { return "sin"; }
1206  enum Functype functype() const override { return SIN_FUNC; }
1207 };
1208 
1210  public:
1212  double val_real() override;
1213  const char *func_name() const override { return "tan"; }
1214  enum Functype functype() const override { return TAN_FUNC; }
1215 };
1216 
1218  public:
1220  double val_real() override;
1221  const char *func_name() const override { return "cot"; }
1222 };
1223 
1225  public:
1227  bool resolve_type(THD *thd) override;
1228 };
1229 
1231  public:
1234  void fix_num_length_and_dec() override;
1235  void set_numeric_type() override;
1236 };
1237 
1239  public:
1242  const char *func_name() const override { return "ceiling"; }
1243  longlong int_op() override;
1244  double real_op() override;
1245  my_decimal *decimal_op(my_decimal *) override;
1246  bool check_partition_func_processor(uchar *) override { return false; }
1247  bool check_function_as_value_generator(uchar *) override { return false; }
1248  enum Functype functype() const override { return CEILING_FUNC; }
1249 };
1250 
1252  public:
1255  const char *func_name() const override { return "floor"; }
1256  longlong int_op() override;
1257  double real_op() override;
1258  my_decimal *decimal_op(my_decimal *) override;
1259  bool check_partition_func_processor(uchar *) override { return false; }
1260  bool check_function_as_value_generator(uchar *) override { return false; }
1261  enum Functype functype() const override { return FLOOR_FUNC; }
1262 };
1263 
1264 /* This handles round and truncate */
1265 
1267  bool truncate;
1268 
1269  public:
1270  Item_func_round(Item *a, Item *b, bool trunc_arg)
1271  : Item_func_num1(a, b), truncate(trunc_arg) {}
1272  Item_func_round(const POS &pos, Item *a, Item *b, bool trunc_arg)
1273  : Item_func_num1(pos, a, b), truncate(trunc_arg) {}
1274 
1275  const char *func_name() const override {
1276  return truncate ? "truncate" : "round";
1277  }
1278  double real_op() override;
1279  longlong int_op() override;
1280  my_decimal *decimal_op(my_decimal *) override;
1281  bool resolve_type(THD *) override;
1282 };
1283 
1286 
1288  bool first_eval; // true if val_real() is called 1st time
1289  public:
1291  : Item_real_func(pos, a), rand(0), first_eval(true) {}
1292  explicit Item_func_rand(const POS &pos) : Item_real_func(pos) {}
1293 
1294  bool itemize(Parse_context *pc, Item **res) override;
1295  double val_real() override;
1296  const char *func_name() const override { return "rand"; }
1297  /**
1298  This function is non-deterministic and hence depends on the
1299  'RAND' pseudo-table.
1300 
1301  @retval RAND_TABLE_BIT
1302  */
1304  return RAND_TABLE_BIT;
1305  }
1306  bool fix_fields(THD *thd, Item **ref) override;
1307  bool resolve_type(THD *thd) override;
1308  void cleanup() override {
1309  first_eval = true;
1311  }
1312  bool check_function_as_value_generator(uchar *checker_args) override {
1315  checker_args);
1316  func_arg->banned_function_name = func_name();
1317  return ((func_arg->source == VGS_GENERATED_COLUMN) ||
1318  (func_arg->source == VGS_CHECK_CONSTRAINT));
1319  }
1320 
1321  private:
1322  void seed_random(Item *val);
1323 };
1324 
1326  public:
1328  const char *func_name() const override { return "sign"; }
1329  longlong val_int() override;
1330  bool resolve_type(THD *thd) override;
1331 };
1332 
1334  const char *name;
1335  double mul, add;
1336 
1337  public:
1338  Item_func_units(const POS &pos, const char *name_arg, Item *a, double mul_arg,
1339  double add_arg)
1340  : Item_real_func(pos, a), name(name_arg), mul(mul_arg), add(add_arg) {}
1341  double val_real() override;
1342  const char *func_name() const override { return name; }
1343  bool resolve_type(THD *thd) override;
1344 };
1345 
1347  public:
1348  Item_func_min_max(const POS &pos, PT_item_list *opt_list, bool is_least_func)
1349  : Item_func_numhybrid(pos, opt_list),
1350  m_is_least_func(is_least_func),
1351  temporal_item(nullptr) {}
1352 
1353  longlong val_int() override;
1354  double val_real() override;
1355  my_decimal *val_decimal(my_decimal *) override;
1356  longlong int_op() override;
1357  double real_op() override;
1358  my_decimal *decimal_op(my_decimal *) override;
1359  String *str_op(String *) override;
1360  bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1361  bool time_op(MYSQL_TIME *ltime) override;
1362  bool resolve_type(THD *) override;
1363  void set_numeric_type() override {}
1364  enum Item_result result_type() const override { return hybrid_type; }
1365 
1366  /**
1367  Make CAST(LEAST_OR_GREATEST(datetime_expr, varchar_expr))
1368  return a number in format YYMMDDhhmmss.
1369  */
1370  enum Item_result cast_to_int_type() const override {
1371  return compare_as_dates() ? INT_RESULT : result_type();
1372  }
1373 
1374  /// Returns true if arguments to this function should be compared as dates.
1375  bool compare_as_dates() const {
1376  return temporal_item != nullptr &&
1378  }
1379 
1380  /// Returns true if at least one of the arguments was of temporal type.
1381  bool has_temporal_arg() const { return temporal_item; }
1382 
1383  private:
1384  /// True if LEAST function, false if GREATEST.
1385  const bool m_is_least_func;
1387  /*
1388  Used for determining whether one of the arguments is of temporal type and
1389  for converting arguments to a common output format if arguments are
1390  compared as dates and result type is character string. For example,
1391  LEAST('95-05-05', date '10-10-10') should return '1995-05-05', not
1392  '95-05-05'.
1393  */
1395  /**
1396  Compare arguments as datetime values.
1397 
1398  @param value Pointer to which the datetime value of the winning argument
1399  is written.
1400 
1401  @return true if error, false otherwise.
1402  */
1403  bool cmp_datetimes(longlong *value);
1404 
1405  /**
1406  Compare arguments as time values.
1407 
1408  @param value Pointer to which the time value of the winning argument is
1409  written.
1410 
1411  @return true if error, false otherwise.
1412  */
1413  bool cmp_times(longlong *value);
1414 };
1415 
1417  public:
1418  Item_func_min(const POS &pos, PT_item_list *opt_list)
1419  : Item_func_min_max(pos, opt_list, true) {}
1420  const char *func_name() const override { return "least"; }
1421  enum Functype functype() const override { return LEAST_FUNC; }
1422 };
1423 
1425  public:
1426  Item_func_max(const POS &pos, PT_item_list *opt_list)
1427  : Item_func_min_max(pos, opt_list, false) {}
1428  const char *func_name() const override { return "greatest"; }
1429  enum Functype functype() const override { return GREATEST_FUNC; }
1430 };
1431 
1432 /*
1433  Objects of this class are used for ROLLUP queries to wrap up
1434  each constant item referred to in GROUP BY list.
1435 
1436  Before grouping the wrapped item could be considered constant, but after
1437  grouping it is not, as rollup adds NULL values, which can affect later
1438  phases like DISTINCT or windowing.
1439 */
1440 
1442  public:
1444  double val_real() override;
1445  longlong val_int() override;
1446  String *val_str(String *str) override;
1447  my_decimal *val_decimal(my_decimal *dec) override;
1448  bool val_json(Json_wrapper *result) override;
1449  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1450  return (null_value = args[0]->get_date(ltime, fuzzydate));
1451  }
1452  bool get_time(MYSQL_TIME *ltime) override {
1453  return (null_value = args[0]->get_time(ltime));
1454  }
1455  const char *func_name() const override { return "rollup_const"; }
1456  table_map used_tables() const override {
1457  /*
1458  If underlying item is non-constant, return its used_tables value.
1459  Otherwise ensure it is non-constant by returning RAND_TABLE_BIT.
1460  */
1461  return args[0]->used_tables() ? args[0]->used_tables() : RAND_TABLE_BIT;
1462  }
1463  Item_result result_type() const override { return args[0]->result_type(); }
1464  bool resolve_type(THD *) override {
1466 
1467  // The item could be a NULL constant.
1468  null_value = args[0]->is_null();
1469  return false;
1470  }
1471 };
1472 
1475 
1476  public:
1478  longlong val_int() override;
1479  const char *func_name() const override { return "length"; }
1480  bool resolve_type(THD *) override {
1481  max_length = 10;
1482  return false;
1483  }
1484 };
1485 
1487  public:
1489  longlong val_int() override {
1490  DBUG_ASSERT(fixed);
1491  return Item_func_length::val_int() * 8;
1492  }
1493  const char *func_name() const override { return "bit_length"; }
1494 };
1495 
1498 
1499  public:
1502  longlong val_int() override;
1503  const char *func_name() const override { return "char_length"; }
1504  bool resolve_type(THD *) override {
1505  max_length = 10;
1506  return false;
1507  }
1508 };
1509 
1511  public:
1513  null_on_null = false;
1514  }
1515  longlong val_int() override;
1516  const char *func_name() const override { return "coercibility"; }
1517  bool resolve_type(THD *) override {
1518  max_length = 10;
1519  maybe_null = false;
1520  return false;
1521  }
1522 };
1523 
1527 
1528  public:
1531  : Item_int_func(pos, a, b) {}
1532  Item_func_locate(const POS &pos, Item *a, Item *b, Item *c)
1533  : Item_int_func(pos, a, b, c) {}
1534 
1535  const char *func_name() const override { return "locate"; }
1536  longlong val_int() override;
1537  bool resolve_type(THD *thd) override;
1538  void print(const THD *thd, String *str,
1539  enum_query_type query_type) const override;
1540 };
1541 
1543  public:
1544  Item_func_instr(const POS &pos, Item *a, Item *b)
1545  : Item_func_locate(pos, a, b) {}
1546 
1547  const char *func_name() const override { return "instr"; }
1548 };
1549 
1551  public:
1553  : Item_int_func(pos, a) {}
1554  longlong val_int() override;
1555  const char *func_name() const override {
1556  return "validate_password_strength";
1557  }
1558  bool resolve_type(THD *) override {
1559  max_length = 10;
1560  maybe_null = true;
1561  return false;
1562  }
1563 };
1564 
1569 
1570  public:
1572  : Item_int_func(pos, opt_list) {}
1573  longlong val_int() override;
1574  const char *func_name() const override { return "field"; }
1575  bool resolve_type(THD *thd) override;
1576 };
1577 
1580 
1581  public:
1583  longlong val_int() override;
1584  const char *func_name() const override { return "ascii"; }
1585  bool resolve_type(THD *) override {
1586  max_length = 3;
1587  return false;
1588  }
1589 };
1590 
1593 
1594  public:
1596  longlong val_int() override;
1597  const char *func_name() const override { return "ord"; }
1598 };
1599 
1605 
1606  public:
1608  : Item_int_func(pos, a, b), enum_value(0) {}
1609  longlong val_int() override;
1610  const char *func_name() const override { return "find_in_set"; }
1611  bool resolve_type(THD *) override;
1612 };
1613 
1614 /* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
1615 
1616 class Item_func_bit : public Item_func {
1617  protected:
1618  /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1620  /// Buffer storing the determined value
1622  /**
1623  @returns true if the second argument should be of binary type for the
1624  result to be of binary type.
1625  */
1626  virtual bool binary_result_requires_binary_second_arg() const = 0;
1627 
1628  public:
1629  Item_func_bit(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {}
1630  Item_func_bit(const POS &pos, Item *a) : Item_func(pos, a) {}
1631 
1632  bool resolve_type(THD *) override;
1633  enum Item_result result_type() const override { return hybrid_type; }
1634 
1635  longlong val_int() override;
1636  String *val_str(String *str) override;
1637  double val_real() override;
1638  my_decimal *val_decimal(my_decimal *decimal_value) override;
1639 
1640  void print(const THD *thd, String *str,
1641  enum_query_type query_type) const override {
1642  print_op(thd, str, query_type);
1643  }
1644  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1645  if (hybrid_type == INT_RESULT)
1646  return get_date_from_int(ltime, fuzzydate);
1647  else
1648  return get_date_from_string(ltime, fuzzydate);
1649  }
1650  bool get_time(MYSQL_TIME *ltime) override {
1651  if (hybrid_type == INT_RESULT)
1652  return get_time_from_int(ltime);
1653  else
1654  return get_time_from_string(ltime);
1655  }
1656 
1657  private:
1658  /**
1659  @brief Performs the operation on integers to produce a result of type
1660  INT_RESULT.
1661  @return The result of the operation.
1662  */
1663  virtual longlong int_op() = 0;
1664 
1665  /**
1666  @brief Performs the operation on binary strings to produce a result of
1667  type STRING_RESULT.
1668  @return The result of the operation.
1669  */
1670  virtual String *str_op(String *) = 0;
1671 };
1672 
1673 /**
1674  Base class for all the bit functions that work with two binary
1675  arguments: '&', '|', '^'.
1676 */
1677 
1679  protected:
1680  bool binary_result_requires_binary_second_arg() const { return true; }
1681  template <class Char_func, class Int_func>
1682  String *eval_str_op(String *, Char_func char_func, Int_func int_func);
1683  template <class Int_func>
1684  longlong eval_int_op(Int_func int_func);
1685 
1686  public:
1688  : Item_func_bit(pos, a, b) {}
1689 };
1690 
1692  public:
1694  : Item_func_bit_two_param(pos, a, b) {}
1695  const char *func_name() const override { return "|"; }
1696 
1697  private:
1698  longlong int_op() override { return eval_int_op(std::bit_or<ulonglong>()); }
1699  String *str_op(String *str) override {
1700  return eval_str_op(str, std::bit_or<char>(), std::bit_or<ulonglong>());
1701  }
1702 };
1703 
1705  public:
1707  : Item_func_bit_two_param(pos, a, b) {}
1708  const char *func_name() const override { return "&"; }
1709 
1710  private:
1711  longlong int_op() override { return eval_int_op(std::bit_and<ulonglong>()); }
1712  String *str_op(String *str) override {
1713  return eval_str_op(str, std::bit_and<char>(), std::bit_and<ulonglong>());
1714  }
1715 };
1716 
1718  public:
1720  : Item_func_bit_two_param(pos, a, b) {}
1721  const char *func_name() const override { return "^"; }
1722 
1723  private:
1724  longlong int_op() override { return eval_int_op(std::bit_xor<ulonglong>()); }
1725  String *str_op(String *str) override {
1726  return eval_str_op(str, std::bit_xor<char>(), std::bit_xor<ulonglong>());
1727  }
1728 };
1729 
1731  public:
1733  longlong val_int() override;
1734  const char *func_name() const override { return "bit_count"; }
1735  bool resolve_type(THD *) override {
1737  return false;
1738  }
1739 };
1740 
1742  protected:
1743  bool binary_result_requires_binary_second_arg() const { return false; }
1744  template <bool to_left>
1746  template <bool to_left>
1747  String *eval_str_op(String *str);
1748 
1749  public:
1750  Item_func_shift(const POS &pos, Item *a, Item *b)
1751  : Item_func_bit(pos, a, b) {}
1752 };
1753 
1755  public:
1757  : Item_func_shift(pos, a, b) {}
1758  const char *func_name() const override { return "<<"; }
1759 
1760  private:
1761  longlong int_op() override { return eval_int_op<true>(); }
1762  String *str_op(String *str) override { return eval_str_op<true>(str); }
1763 };
1764 
1766  public:
1768  : Item_func_shift(pos, a, b) {}
1769  const char *func_name() const override { return ">>"; }
1770 
1771  private:
1772  longlong int_op() override { return eval_int_op<false>(); }
1773  String *str_op(String *str) override { return eval_str_op<false>(str); }
1774 };
1775 
1777  protected:
1779  return false;
1780  }
1781 
1782  public:
1784  const char *func_name() const override { return "~"; }
1785  void print(const THD *thd, String *str,
1786  enum_query_type query_type) const override {
1787  Item_func::print(thd, str, query_type);
1788  }
1789 
1790  private:
1791  longlong int_op() override;
1792  String *str_op(String *str) override;
1793 };
1794 
1797 
1798  public:
1802 
1803  bool itemize(Parse_context *pc, Item **res) override;
1804  longlong val_int() override;
1805  const char *func_name() const override { return "last_insert_id"; }
1806  bool resolve_type(THD *) override {
1807  unsigned_flag = true;
1808  if (arg_count) max_length = args[0]->max_length;
1809  return false;
1810  }
1811  bool check_function_as_value_generator(uchar *checker_args) override {
1814  checker_args);
1815  func_arg->banned_function_name = func_name();
1816  return true;
1817  }
1818 };
1819 
1822 
1823  public:
1824  Item_func_benchmark(const POS &pos, Item *count_expr, Item *expr)
1825  : Item_int_func(pos, count_expr, expr) {}
1826 
1827  /// Ensure that "benchmark()" is never optimized away
1829  return RAND_TABLE_BIT;
1830  }
1831 
1832  bool itemize(Parse_context *pc, Item **res) override;
1833  longlong val_int() override;
1834  const char *func_name() const override { return "benchmark"; }
1835  bool resolve_type(THD *) override {
1836  max_length = 1;
1837  maybe_null = true;
1838  return false;
1839  }
1840  void print(const THD *thd, String *str,
1841  enum_query_type query_type) const override;
1842  bool check_function_as_value_generator(uchar *checker_args) override {
1845  checker_args);
1846  func_arg->banned_function_name = func_name();
1847  return true;
1848  }
1849 };
1850 
1851 void item_func_sleep_init();
1852 void item_func_sleep_free();
1853 
1856 
1857  public:
1859 
1860  bool itemize(Parse_context *pc, Item **res) override;
1861  const char *func_name() const override { return "sleep"; }
1862  /**
1863  This function is non-deterministic and hence depends on the
1864  'RAND' pseudo-table.
1865 
1866  @retval RAND_TABLE_BIT
1867  */
1869  return RAND_TABLE_BIT;
1870  }
1871  bool check_function_as_value_generator(uchar *checker_args) override {
1874  checker_args);
1875  func_arg->banned_function_name = func_name();
1876  return true;
1877  }
1878  longlong val_int() override;
1879 };
1880 
1881 class Item_udf_func : public Item_func {
1882  typedef Item_func super;
1883 
1884  protected:
1886  bool is_expensive_processor(uchar *) override { return true; }
1887 
1888  public:
1889  Item_udf_func(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1890  : Item_func(pos, opt_list), udf(udf_arg) {
1891  null_on_null = false;
1892  }
1893 
1894  bool itemize(Parse_context *pc, Item **res) override;
1895  const char *func_name() const override { return udf.name(); }
1896  enum Functype functype() const override { return UDF_FUNC; }
1897  bool fix_fields(THD *thd, Item **) override {
1898  DBUG_ASSERT(fixed == 0);
1899  bool res = udf.fix_fields(thd, this, arg_count, args);
1901  fixed = true;
1902  return res;
1903  }
1904  void update_used_tables() override {
1905  /*
1906  TODO: Make a member in UDF_INIT and return if a UDF is deterministic or
1907  not.
1908  Currently UDF_INIT has a member (const_item) that is an in/out
1909  parameter to the init() call.
1910  The code in udf_handler::fix_fields also duplicates the arguments
1911  handling code in Item_func::fix_fields().
1912 
1913  The lack of information if a UDF is deterministic makes writing
1914  a correct update_used_tables() for UDFs impossible.
1915  One solution to this would be :
1916  - Add a is_deterministic member of UDF_INIT
1917  - (optionally) deprecate the const_item member of UDF_INIT
1918  - Take away the duplicate code from udf_handler::fix_fields() and
1919  make Item_udf_func call Item_func::fix_fields() to process its
1920  arguments as for any other function.
1921  - Store the deterministic flag returned by <udf>_init into the
1922  udf_handler.
1923  - Don't implement Item_udf_func::fix_fields, implement
1924  Item_udf_func::resolve_type() instead (similar to non-UDF functions).
1925  - Override Item_func::update_used_tables to call
1926  Item_func::update_used_tables() and add a RAND_TABLE_BIT to the
1927  result of Item_func::update_used_tables() if the UDF is
1928  non-deterministic.
1929  - (optionally) rename RAND_TABLE_BIT to NONDETERMINISTIC_BIT to
1930  better describe its usage.
1931 
1932  The above would require a change of the UDF API.
1933  Until that change is done here's how the current code works:
1934  We call Item_func::update_used_tables() only when we know that
1935  the function depends on real non-const tables and is deterministic.
1936  This can be done only because we know that the optimizer will
1937  call update_used_tables() only when there's possibly a new const
1938  table. So update_used_tables() can only make a Item_func more
1939  constant than it is currently.
1940  That's why we don't need to do anything if a function is guaranteed
1941  to return non-constant (it's non-deterministic) or is already a
1942  const.
1943  */
1947  }
1948  void cleanup() override;
1949  Item_result result_type() const override { return udf.result_type(); }
1950  bool is_expensive() override { return true; }
1951  void print(const THD *thd, String *str,
1952  enum_query_type query_type) const override;
1953 
1954  bool check_function_as_value_generator(uchar *checker_args) override {
1957  checker_args);
1958  func_arg->banned_function_name = func_name();
1959  return true;
1960  }
1961 
1962  protected:
1963  bool may_have_named_parameters() const override { return true; }
1964 };
1965 
1967  public:
1968  Item_func_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1969  : Item_udf_func(pos, udf_arg, opt_list) {}
1970  longlong val_int() override {
1971  DBUG_ASSERT(fixed == 1);
1972  return (longlong)rint(Item_func_udf_float::val_real());
1973  }
1974  my_decimal *val_decimal(my_decimal *dec_buf) override {
1975  double res = val_real();
1976  if (null_value) return NULL;
1977  double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
1978  return dec_buf;
1979  }
1980  double val_real() override;
1981  String *val_str(String *str) override;
1982  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1983  return get_date_from_real(ltime, fuzzydate);
1984  }
1985  bool get_time(MYSQL_TIME *ltime) override {
1986  return get_time_from_real(ltime);
1987  }
1988  bool resolve_type(THD *) override {
1990  fix_num_length_and_dec(); // @todo - needed?
1991  return false;
1992  }
1993 };
1994 
1996  public:
1997  Item_func_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1998  : Item_udf_func(pos, udf_arg, opt_list) {}
1999  longlong val_int() override;
2000  double val_real() override {
2001  return static_cast<double>(Item_func_udf_int::val_int());
2002  }
2003  String *val_str(String *str) override;
2004  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2005  return get_date_from_int(ltime, fuzzydate);
2006  }
2007  bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
2008  enum Item_result result_type() const override { return INT_RESULT; }
2009  bool resolve_type(THD *) override {
2011  return false;
2012  }
2013 };
2014 
2016  public:
2018  PT_item_list *opt_list)
2019  : Item_udf_func(pos, udf_arg, opt_list) {}
2020  longlong val_int() override;
2021  double val_real() override;
2022  my_decimal *val_decimal(my_decimal *) override;
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_decimal(ltime, fuzzydate);
2026  }
2027  bool get_time(MYSQL_TIME *ltime) override {
2028  return get_time_from_decimal(ltime);
2029  }
2030  enum Item_result result_type() const override { return DECIMAL_RESULT; }
2031  bool resolve_type(THD *thd) override;
2032 };
2033 
2035  public:
2036  Item_func_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2037  : Item_udf_func(pos, udf_arg, opt_list) {}
2038 
2039  String *val_str(String *) override;
2040  double val_real() override {
2041  int err_not_used;
2042  const char *end_not_used;
2043  String *res;
2044  res = val_str(&str_value);
2045  return res ? my_strntod(res->charset(), (char *)res->ptr(), res->length(),
2046  &end_not_used, &err_not_used)
2047  : 0.0;
2048  }
2049  longlong val_int() override {
2050  int err_not_used;
2051  String *res;
2052  res = val_str(&str_value);
2053  return res ? my_strntoll(res->charset(), res->ptr(), res->length(), 10,
2054  nullptr, &err_not_used)
2055  : (longlong)0;
2056  }
2057  my_decimal *val_decimal(my_decimal *dec_buf) override {
2058  String *res = val_str(&str_value);
2059  if (!res) return NULL;
2060  string2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
2061  return dec_buf;
2062  }
2063  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2064  return get_date_from_string(ltime, fuzzydate);
2065  }
2066  bool get_time(MYSQL_TIME *ltime) override {
2067  return get_time_from_string(ltime);
2068  }
2069  enum Item_result result_type() const override { return STRING_RESULT; }
2070  bool resolve_type(THD *thd) override;
2071 };
2072 
2073 void mysql_ull_cleanup(THD *thd);
2075 
2078 
2080 
2081  public:
2083  : Item_int_func(pos, a, b) {}
2084 
2085  bool itemize(Parse_context *pc, Item **res) override;
2086  longlong val_int() override;
2087  const char *func_name() const override { return "get_lock"; }
2088  bool resolve_type(THD *) override {
2089  max_length = 1;
2090  maybe_null = true;
2091  return false;
2092  }
2093  bool is_non_const_over_literals(uchar *) override { return true; }
2094  bool check_function_as_value_generator(uchar *checker_args) override {
2097  checker_args);
2098  func_arg->banned_function_name = func_name();
2099  return true;
2100  }
2101  uint decimal_precision() const override { return max_length; }
2102 };
2103 
2106 
2108 
2109  public:
2111  bool itemize(Parse_context *pc, Item **res) override;
2112 
2113  longlong val_int() override;
2114  const char *func_name() const override { return "release_lock"; }
2115  bool resolve_type(THD *) override {
2116  max_length = 1;
2117  maybe_null = true;
2118  return false;
2119  }
2120  bool is_non_const_over_literals(uchar *) override { return true; }
2121  bool check_function_as_value_generator(uchar *checker_args) override {
2124  checker_args);
2125  func_arg->banned_function_name = func_name();
2126  return true;
2127  }
2128  uint decimal_precision() const override { return max_length; }
2129 };
2130 
2133 
2134  public:
2136  bool itemize(Parse_context *pc, Item **res) override;
2137 
2138  longlong val_int() override;
2139  const char *func_name() const override { return "release_all_locks"; }
2140  bool resolve_type(THD *) override {
2141  unsigned_flag = true;
2142  return false;
2143  }
2144  bool is_non_const_over_literals(uchar *) override { return true; }
2145  bool check_function_as_value_generator(uchar *checker_args) override {
2148  checker_args);
2149  func_arg->banned_function_name = func_name();
2150  return true;
2151  }
2152 };
2153 
2154 /* replication functions */
2155 
2159 
2160  public:
2162  : Item_int_func(pos, a, b) {}
2164  : Item_int_func(pos, a, b, c) {}
2165  Item_master_pos_wait(const POS &pos, Item *a, Item *b, Item *c, Item *d)
2166  : Item_int_func(pos, a, b, c, d) {}
2167 
2168  bool itemize(Parse_context *pc, Item **res) override;
2169  longlong val_int() override;
2170  const char *func_name() const override { return "master_pos_wait"; }
2171  bool resolve_type(THD *) override {
2172  max_length = 21;
2173  maybe_null = true;
2174  return false;
2175  }
2176  bool check_function_as_value_generator(uchar *checker_args) override {
2179  checker_args);
2180  func_arg->banned_function_name = func_name();
2181  return true;
2182  }
2183 };
2184 
2185 /**
2186  This class is used for implementing the new wait_for_executed_gtid_set
2187  function and the functions related to them. This new function is independent
2188  of the slave threads.
2189 */
2192 
2194 
2195  public:
2197  : Item_int_func(pos, a) {}
2199  : Item_int_func(pos, a, b) {}
2200 
2201  bool itemize(Parse_context *pc, Item **res) override;
2202  longlong val_int() override;
2203  const char *func_name() const override {
2204  return "wait_for_executed_gtid_set";
2205  }
2206  bool resolve_type(THD *) override {
2207  maybe_null = true;
2208  return false;
2209  }
2210 };
2211 
2214 
2216 
2217  public:
2220  : Item_int_func(pos, a, b) {}
2222  : Item_int_func(pos, a, b, c) {}
2223 
2224  bool itemize(Parse_context *pc, Item **res) override;
2225  longlong val_int() override;
2226  const char *func_name() const override {
2227  return "wait_until_sql_thread_after_gtids";
2228  }
2229  bool resolve_type(THD *) override {
2230  maybe_null = true;
2231  return false;
2232  }
2233 };
2234 
2238 
2239  public:
2241  : Item_int_func(pos, a, b) {}
2242  longlong val_int() override;
2243  const char *func_name() const override { return "gtid_subset"; }
2244  bool resolve_type(THD *) override {
2245  maybe_null = false;
2246  return false;
2247  }
2248  bool is_bool_func() const override { return true; }
2249 };
2250 
2251 /**
2252  Internal functions used by INFORMATION_SCHEMA implementation to check
2253  if user have access to given database/table/column.
2254 */
2255 
2257  public:
2259  : Item_int_func(pos, a) {}
2260  longlong val_int() override;
2261  const char *func_name() const override { return "can_access_database"; }
2262  bool resolve_type(THD *) override {
2263  maybe_null = true;
2264  return false;
2265  }
2266 };
2267 
2269  public:
2271  : Item_int_func(pos, a, b) {}
2272  longlong val_int() override;
2273  const char *func_name() const override { return "can_access_table"; }
2274  bool resolve_type(THD *) override {
2275  maybe_null = true;
2276  return false;
2277  }
2278 };
2279 
2281  public:
2283  : Item_int_func(pos, a, b) {}
2284  longlong val_int();
2285  const char *func_name() const { return "can_access_trigger"; }
2286  bool resolve_type(THD *) {
2287  max_length = 4;
2288  maybe_null = true;
2289  return false;
2290  }
2291 };
2292 
2294  public:
2296  : Item_int_func(pos, list) {}
2297  longlong val_int();
2298  const char *func_name() const { return "can_access_routine"; }
2299  bool resolve_type(THD *) {
2300  max_length = 4;
2301  maybe_null = true;
2302  return false;
2303  }
2304 };
2305 
2307  public:
2309  longlong val_int();
2310  const char *func_name() const { return "can_access_event"; }
2311  bool resolve_type(THD *) {
2312  maybe_null = true;
2313  return false;
2314  }
2315 };
2316 
2318  public:
2320  : Item_int_func(pos, a) {}
2321  longlong val_int();
2322  const char *func_name() const { return "can_access_resource_group"; }
2323  bool resolve_type(THD *) {
2324  max_length = 1; // Function can return 0 or 1.
2325  maybe_null = true;
2326  return false;
2327  }
2328 };
2329 
2331  public:
2333  : Item_int_func(pos, a, b, c, d) {}
2334  longlong val_int() override;
2335  const char *func_name() const override { return "can_access_view"; }
2336  bool resolve_type(THD *) override {
2337  maybe_null = true;
2338  return false;
2339  }
2340 };
2341 
2343  public:
2345  : Item_int_func(pos, a, b, c) {}
2346  longlong val_int() override;
2347  const char *func_name() const override { return "can_access_column"; }
2348  bool resolve_type(THD *) override {
2349  maybe_null = true;
2350  return false;
2351  }
2352 };
2353 
2355  public:
2357  : Item_int_func(pos, a) {}
2359  : Item_int_func(pos, a, b) {}
2360  longlong val_int();
2361  const char *func_name() const { return "is_visible_dd_object"; }
2362  bool resolve_type(THD *) {
2363  max_length = 1;
2364  maybe_null = true;
2365  return false;
2366  }
2367 };
2368 
2370  public:
2372  : Item_int_func(pos, list) {}
2373  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2374  longlong val_int() override;
2375  const char *func_name() const override { return "internal_table_rows"; }
2376  bool resolve_type(THD *) override {
2377  maybe_null = true;
2378  unsigned_flag = true;
2379  null_on_null = false;
2380  return false;
2381  }
2382 };
2383 
2385  public:
2387  : Item_int_func(pos, list) {}
2388  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2389  longlong val_int() override;
2390  const char *func_name() const override { return "internal_avg_row_length"; }
2391  bool resolve_type(THD *) override {
2392  maybe_null = true;
2393  unsigned_flag = true;
2394  null_on_null = false;
2395  return false;
2396  }
2397 };
2398 
2400  public:
2402  : Item_int_func(pos, list) {}
2403  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2404  longlong val_int() override;
2405  const char *func_name() const override { return "internal_data_length"; }
2406  bool resolve_type(THD *) override {
2407  maybe_null = true;
2408  unsigned_flag = true;
2409  null_on_null = false;
2410  return false;
2411  }
2412 };
2413 
2415  public:
2417  : Item_int_func(pos, list) {}
2418  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2419  longlong val_int() override;
2420  const char *func_name() const override { return "internal_max_data_length"; }
2421  bool resolve_type(THD *) override {
2422  maybe_null = true;
2423  unsigned_flag = true;
2424  null_on_null = false;
2425  return false;
2426  }
2427 };
2428 
2430  public:
2432  : Item_int_func(pos, list) {}
2433  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2434  longlong val_int() override;
2435  const char *func_name() const override { return "internal_index_length"; }
2436  bool resolve_type(THD *) override {
2437  maybe_null = true;
2438  unsigned_flag = true;
2439  null_on_null = false;
2440  return false;
2441  }
2442 };
2443 
2445  public:
2447  : Item_int_func(pos, list) {}
2448  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2449  longlong val_int() override;
2450  const char *func_name() const override { return "internal_data_free"; }
2451  bool resolve_type(THD *) override {
2452  maybe_null = true;
2453  unsigned_flag = true;
2454  null_on_null = false;
2455  return false;
2456  }
2457 };
2458 
2460  public:
2462  : Item_int_func(pos, list) {}
2463  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2464  longlong val_int() override;
2465  const char *func_name() const override { return "internal_auto_increment"; }
2466  bool resolve_type(THD *) override {
2467  maybe_null = true;
2468  unsigned_flag = true;
2469  null_on_null = false;
2470  return false;
2471  }
2472 };
2473 
2475  public:
2477  : Item_int_func(pos, list) {}
2478  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2479  longlong val_int() override;
2480  const char *func_name() const override { return "internal_checksum"; }
2481  bool resolve_type(THD *) override {
2482  maybe_null = true;
2483  null_on_null = false;
2484  return false;
2485  }
2486 };
2487 
2489  public:
2491  : Item_int_func(pos, a) {}
2492  longlong val_int() override;
2493  const char *func_name() const override { return "internal_keys_disabled"; }
2494  bool resolve_type(THD *) override {
2495  maybe_null = false;
2496  null_on_null = false;
2497  return false;
2498  }
2499 };
2500 
2502  public:
2504  PT_item_list *list)
2505  : Item_int_func(pos, list) {}
2506  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2507  longlong val_int() override;
2508  const char *func_name() const override {
2509  return "internal_index_column_cardinality";
2510  }
2511  bool resolve_type(THD *) override {
2512  maybe_null = true;
2513  null_on_null = false;
2514  return false;
2515  }
2516 };
2517 
2519  public:
2521  Item *d)
2522  : Item_int_func(pos, a, b, c, d) {}
2523  longlong val_int() override;
2524  const char *func_name() const override { return "internal_dd_char_length"; }
2525  bool resolve_type(THD *) override {
2526  maybe_null = true;
2527  null_on_null = false;
2528  return false;
2529  }
2530 };
2531 
2533  : public Item_int_func {
2534  public:
2536  PT_item_list *list)
2537  : Item_int_func(pos, list) {}
2538  longlong val_int() override;
2539  const char *func_name() const override {
2540  return "internal_get_view_warning_or_error";
2541  }
2542  bool resolve_type(THD *) override {
2543  max_length = 1;
2544  maybe_null = false;
2545  null_on_null = false;
2546  return false;
2547  }
2548 };
2549 
2551  public:
2553  : Item_int_func(pos, list) {}
2554  longlong val_int() override;
2555  bool resolve_type(THD *) override {
2556  maybe_null = true;
2557  null_on_null = false;
2558  return false;
2559  }
2560  const char *func_name() const override {
2561  return "get_dd_index_sub_part_length";
2562  }
2563 };
2564 
2566  public:
2568  Item *d)
2569  : Item_int_func(pos, a, b, c, d) {}
2570  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2571  longlong val_int() override;
2572  const char *func_name() const override { return "internal_tablespace_id"; }
2573  bool resolve_type(THD *) override {
2574  maybe_null = true;
2575  null_on_null = false;
2576  return false;
2577  }
2578 };
2579 
2581  : public Item_int_func {
2582  public:
2584  Item *b, Item *c, Item *d)
2585  : Item_int_func(pos, a, b, c, d) {}
2586 
2587  enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2588  longlong val_int() override;
2589 
2590  const char *func_name() const override {
2591  return "internal_tablespace_logfile_group_number";
2592  }
2593 
2594  bool resolve_type(THD *) override {
2595  maybe_null = true;
2596  null_on_null = false;
2597  return false;
2598  }
2599 };
2600 
2602  public:
2604  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_free_extents";
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_total_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_extent_size";
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_initial_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_maximum_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 *b, 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_autoextend_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 *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_version";
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_data_free";
2752  }
2753 
2754  bool resolve_type(THD *) override {
2755  maybe_null = true;
2756  null_on_null = false;
2757  return false;
2758  }
2759 };
2760 
2761 /**
2762  Common class for:
2763  Item_func_get_system_var
2764  Item_func_get_user_var
2765  Item_func_set_user_var
2766 */
2767 class Item_var_func : public Item_func {
2768  public:
2770  explicit Item_var_func(const POS &pos) : Item_func(pos) {}
2771 
2772  Item_var_func(THD *thd, Item_var_func *item) : Item_func(thd, item) {}
2773 
2775  Item_var_func(const POS &pos, Item *a) : Item_func(pos, a) {}
2776 
2777  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2778  return get_date_from_non_temporal(ltime, fuzzydate);
2779  }
2780  bool get_time(MYSQL_TIME *ltime) override {
2781  return get_time_from_non_temporal(ltime);
2782  }
2783  bool check_function_as_value_generator(uchar *checker_args) override {
2786  checker_args);
2787  func_arg->err_code = (func_arg->source == VGS_CHECK_CONSTRAINT)
2788  ? ER_CHECK_CONSTRAINT_VARIABLES
2789  : ER_DEFAULT_VAL_GENERATED_VARIABLES;
2790  return true;
2791  }
2792 };
2793 
2794 /* Handling of user definable variables */
2795 
2796 // this is needed for user_vars hash
2798  static const size_t extra_size = sizeof(double);
2799  char *m_ptr; // Value
2800  size_t m_length; // Value length
2801  Item_result m_type; // Value type
2803 
2804  void reset_value() {
2805  m_ptr = NULL;
2806  m_length = 0;
2807  }
2808  void set_value(char *value, size_t length) {
2809  m_ptr = value;
2810  m_length = length;
2811  }
2812 
2813  /**
2814  Position inside a user_var_entry where small values are stored:
2815  double values, longlong values and string values with length
2816  up to extra_size (should be 8 bytes on all platforms).
2817  String values with length longer than 8 are stored in a separate
2818  memory buffer, which is allocated when needed using the method realloc().
2819  */
2820  char *internal_buffer_ptr() const {
2821  return (char *)this + ALIGN_SIZE(sizeof(user_var_entry));
2822  }
2823 
2824  /**
2825  Position inside a user_var_entry where a null-terminates array
2826  of characters representing the variable name is stored.
2827  */
2828  char *name_ptr() const { return internal_buffer_ptr() + extra_size; }
2829 
2830  /**
2831  Initialize m_ptr to the internal buffer (if the value is small enough),
2832  or allocate a separate buffer.
2833  @param length - length of the value to be stored.
2834  */
2835  bool mem_realloc(size_t length);
2836 
2837  /**
2838  Check if m_ptr point to an external buffer previously alloced by realloc().
2839  @retval true - an external buffer is alloced.
2840  @retval false - m_ptr is null, or points to the internal buffer.
2841  */
2842  bool alloced() { return m_ptr && m_ptr != internal_buffer_ptr(); }
2843 
2844  /**
2845  Free the external value buffer, if it's allocated.
2846  */
2847  void free_value() {
2848  if (alloced()) my_free(m_ptr);
2849  }
2850 
2851  /**
2852  Copy the array of characters from the given name into the internal
2853  name buffer and initialize entry_name to point to it.
2854  */
2856  name.strcpy(name_ptr());
2857  entry_name = Name_string(name_ptr(), name.length());
2858  }
2859 
2860  /**
2861  Initialize all members
2862 
2863  @param thd Current session.
2864  @param name Name of the user_var_entry instance.
2865  @param cs charset information of the user_var_entry instance.
2866  */
2867  void init(THD *thd, const Simple_cstring &name, const CHARSET_INFO *cs);
2868 
2869  /**
2870  Store a value of the given type into a user_var_entry instance.
2871  @param from Value
2872  @param length Size of the value
2873  @param type type
2874  @return
2875  @retval false on success
2876  @retval true on memory allocation error
2877  */
2878  bool store(const void *from, size_t length, Item_result type);
2879 
2880  /**
2881  Assert the user variable is locked.
2882  This is debug code only.
2883  The thread LOCK_thd_data mutex protects:
2884  - the thd->user_vars hash itself
2885  - the values in the user variable itself.
2886  The protection is required for monitoring,
2887  as a different thread can inspect this session
2888  user variables, on a live session.
2889  */
2890  void assert_locked() const;
2891 
2892  public:
2893  user_var_entry() {} /* Remove gcc warning */
2894 
2895  Simple_cstring entry_name; // Variable name
2896  DTCollation collation; // Collation with attributes
2898  bool unsigned_flag; // true if unsigned, false if signed
2899 
2900  /**
2901  Store a value of the given type and attributes (collation, sign)
2902  into a user_var_entry instance.
2903  @param ptr Value
2904  @param length Size of the value
2905  @param type type
2906  @param cs Character set and collation of the value
2907  @param dv Collation derivation of the value
2908  @param unsigned_arg Signess of the value
2909  @return
2910  @retval false on success
2911  @retval true on memory allocation error
2912  */
2913  bool store(const void *ptr, size_t length, Item_result type,
2914  const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
2915  /**
2916  Set type of to the given value.
2917  @param type Data type.
2918  */
2920  assert_locked();
2921  m_type = type;
2922  }
2923  /**
2924  Set value to NULL
2925  @param type Data type.
2926  */
2927 
2929  assert_locked();
2930  free_value();
2931  reset_value();
2932  m_type = type;
2933  }
2934 
2935  /**
2936  Allocates and initializes a user variable instance.
2937 
2938  @param thd Current session.
2939  @param name Name of the variable.
2940  @param cs Charset of the variable.
2941 
2942  @return Address of the allocated and initialized user_var_entry instance.
2943  @retval NULL On allocation error.
2944  */
2945  static user_var_entry *create(THD *thd, const Name_string &name,
2946  const CHARSET_INFO *cs);
2947 
2948  /**
2949  Free all memory used by a user_var_entry instance
2950  previously created by create().
2951  */
2952  void destroy() {
2953  assert_locked();
2954  free_value(); // Free the external value buffer
2955  my_free(this); // Free the instance itself
2956  }
2957 
2958  void lock();
2959  void unlock();
2960 
2961  /* Routines to access the value and its type */
2962  const char *ptr() const { return m_ptr; }
2963  size_t length() const { return m_length; }
2964  Item_result type() const { return m_type; }
2965  /* Item-alike routines to access the value */
2966  double val_real(bool *null_value) const;
2967  longlong val_int(bool *null_value) const;
2968  String *val_str(bool *null_value, String *str, uint decimals) const;
2969  my_decimal *val_decimal(bool *null_value, my_decimal *result) const;
2970 };
2971 
2975  /*
2976  The entry_thread_id variable is used:
2977  1) to skip unnecessary updates of the entry field (see above);
2978  2) to reset the entry field that was initialized in the other thread
2979  (for example, an item tree of a trigger that updates user variables
2980  may be shared between several connections, and the entry_thread_id field
2981  prevents updates of one connection user variables from a concurrent
2982  connection calling the same trigger that initially updated some
2983  user variable it the first connection context).
2984  */
2986  /**
2987  Delayed setting of non-constness.
2988 
2989  Normally, Item_func_get_user_var objects are tagged as not const
2990  when Item_func_set_user_var::fix_fields() is called for the same
2991  variable in the same query. If delayed_non_constness is set, the
2992  tagging is delayed until the variable is actually set. This means
2993  that Item_func_get_user_var objects will still be treated as a
2994  constant by the optimizer and executor until the variable is
2995  actually changed.
2996 
2997  @see select_dumpvar::send_data().
2998  */
3003  union {
3005  double vreal;
3008  } save_result;
3009 
3010  public:
3011  Name_string name; // keep it public
3012 
3014  : Item_var_func(b),
3016  entry(NULL),
3017  entry_thread_id(0),
3018  delayed_non_constness(delayed),
3019  name(a) {}
3020  Item_func_set_user_var(const POS &pos, Name_string a, Item *b, bool delayed)
3021  : Item_var_func(pos, b),
3023  entry(NULL),
3024  entry_thread_id(0),
3025  delayed_non_constness(delayed),
3026  name(a) {}
3027 
3029  : Item_var_func(thd, item),
3031  entry(item->entry),
3034  value(item->value),
3035  decimal_buff(item->decimal_buff),
3036  null_item(item->null_item),
3037  save_result(item->save_result),
3038  name(item->name) {}
3039  enum Functype functype() const override { return SUSERVAR_FUNC; }
3040  double val_real() override;
3041  longlong val_int() override;
3042  String *val_str(String *str) override;
3043  my_decimal *val_decimal(my_decimal *) override;
3044  bool update_hash(const void *ptr, uint length, enum Item_result type,
3045  const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
3046  bool send(Protocol *protocol, String *str_arg) override;
3047  void make_field(Send_field *tmp_field) override;
3048  bool check(bool use_result_field);
3049  void save_item_result(Item *item);
3050  bool update();
3051  enum Item_result result_type() const override { return cached_result_type; }
3052  bool fix_fields(THD *thd, Item **ref) override;
3053  bool resolve_type(THD *) override;
3054  void print(const THD *thd, String *str,
3055  enum_query_type query_type) const override;
3056  void print_assignment(const THD *thd, String *str,
3057  enum_query_type query_type) const;
3058  const char *func_name() const override { return "set_user_var"; }
3059 
3060  type_conversion_status save_in_field(Field *field, bool no_conversions,
3061  bool can_use_result_field);
3062 
3063  void save_org_in_field(Field *field) override {
3064  save_in_field(field, true, false);
3065  }
3066 
3067  bool set_entry(THD *thd, bool create_if_not_exists);
3068  void cleanup() override;
3069 
3070  protected:
3072  bool no_conversions) override {
3073  return save_in_field(field, no_conversions, true);
3074  }
3075 };
3076 
3078  private Settable_routine_parameter {
3081 
3082  public:
3083  Name_string name; // keep it public
3084 
3089 
3090  enum Functype functype() const override { return GUSERVAR_FUNC; }
3091  double val_real() override;
3092  longlong val_int() override;
3093  my_decimal *val_decimal(my_decimal *) override;
3094  String *val_str(String *str) override;
3095  bool resolve_type(THD *) override;
3096  void update_used_tables() override {} // Keep existing used tables
3097  void print(const THD *thd, String *str,
3098  enum_query_type query_type) const override;
3099  enum Item_result result_type() const override;
3100  /*
3101  We must always return variables as strings to guard against selects of type
3102  select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
3103  */
3104  const char *func_name() const override { return "get_user_var"; }
3105  bool is_non_const_over_literals(uchar *) override { return true; }
3106  bool eq(const Item *item, bool binary_cmp) const override;
3107 
3108  private:
3109  bool set_value(THD *thd, sp_rcontext *ctx, Item **it) override;
3110 
3111  public:
3113  return this;
3114  }
3115 };
3116 
3117 /*
3118  This item represents user variable used as out parameter (e.g in LOAD DATA),
3119  and it is supposed to be used only for this purprose. So it is simplified
3120  a lot. Actually you should never obtain its value.
3121 
3122  The only two reasons for this thing being an Item is possibility to store it
3123  in List<Item> and desire to place this code somewhere near other functions
3124  working with user variables.
3125 */
3129 
3130  public:
3132  : Item(pos), name(a) {
3133  item_name.copy(a);
3134  }
3135  /* We should return something different from FIELD_ITEM here */
3136  enum Type type() const override { return STRING_ITEM; }
3137  double val_real() override;
3138  longlong val_int() override;
3139  String *val_str(String *str) override;
3140  my_decimal *val_decimal(my_decimal *decimal_buffer) override;
3142  DBUG_ASSERT(0);
3143  return true;
3144  }
3145  bool get_time(MYSQL_TIME *) override {
3146  DBUG_ASSERT(0);
3147  return true;
3148  }
3149 
3150  /* fix_fields() binds variable name with its entry structure */
3151  bool fix_fields(THD *thd, Item **ref) override;
3152  void print(const THD *thd, String *str,
3153  enum_query_type query_type) const override;
3154  void set_null_value(const CHARSET_INFO *cs);
3155  void set_value(const char *str, size_t length, const CHARSET_INFO *cs);
3156 };
3157 
3158 /* A system variable */
3159 
3160 #define GET_SYS_VAR_CACHE_LONG 1
3161 #define GET_SYS_VAR_CACHE_DOUBLE 2
3162 #define GET_SYS_VAR_CACHE_STRING 4
3163 
3165 
3166 /** Class to log audit event MYSQL_AUDIT_GLOBAL_VARIABLE_GET. */
3168  public:
3170  uchar cache_type);
3172 
3173  private:
3174  // Thread handle.
3176 
3177  // Item_func_get_system_var instance.
3179 
3180  /*
3181  Value conversion type.
3182  Depending on the value conversion type GET_SYS_VAR_CACHE_* is stored in this
3183  member while creating the object. While converting value if there are any
3184  intermediate conversions in the same query then this member is used to avoid
3185  auditing more than once.
3186  */
3188 
3189  /*
3190  To indicate event auditing is required or not. Event is not audited if
3191  * scope of the variable is *not* GLOBAL.
3192  * or the event is already audited for global variable for the same query.
3193  */
3195 };
3196 
3202  double cached_dval;
3207 
3208  template <typename T>
3210 
3212 
3213  public:
3214  Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
3215  LEX_STRING *component_arg, const char *name_arg,
3216  size_t name_len_arg);
3217  enum Functype functype() const override { return GSYSVAR_FUNC; }
3218  bool resolve_type(THD *) override;
3219  void print(const THD *thd, String *str,
3220  enum_query_type query_type) const override;
3221  table_map used_tables() const override { return 0; }
3222  bool is_non_const_over_literals(uchar *) override { return true; }
3223  enum Item_result result_type() const override;
3224  double val_real() override;
3225  longlong val_int() override;
3226  String *val_str(String *) override;
3227  my_decimal *val_decimal(my_decimal *dec_buf) override {
3228  return val_decimal_from_real(dec_buf);
3229  }
3230  /* TODO: fix to support views */
3231  const char *func_name() const override { return "get_system_var"; }
3232  /**
3233  Indicates whether this system variable is written to the binlog or not.
3234 
3235  Variables are written to the binlog as part of "status_vars" in
3236  Query_log_event, as an Intvar_log_event, or a Rand_log_event.
3237 
3238  @return true if the variable is written to the binlog, false otherwise.
3239  */
3240  bool is_written_to_binlog();
3241  bool eq(const Item *item, bool binary_cmp) const override;
3242 
3243  void cleanup() override;
3244 };
3245 
3246 class JOIN;
3247 
3250 
3251  public:
3254  bool join_key;
3258  /**
3259  Master item means that if idendical items are present in the
3260  statement, they use the same FT handler. FT handler is initialized
3261  only for master item and slave items just use it. FT hints initialized
3262  for master only, slave items HINTS are not accessed.
3263  */
3265  Item *concat_ws; // Item_func_concat_ws
3266  String value; // value of concat_ws
3267  String search_value; // key_item()'s value converted to cmp_collation
3268 
3269  /**
3270  Constructor for Item_func_match class.
3271 
3272  @param pos Position of token in the parser.
3273  @param a List of arguments.
3274  @param against_arg Expression to match against.
3275  @param b FT Flags.
3276  */
3277  Item_func_match(const POS &pos, PT_item_list *a, Item *against_arg, uint b)
3278  : Item_real_func(pos, a),
3279  against(against_arg),
3280  key(0),
3281  flags(b),
3282  join_key(false),
3283  ft_handler(NULL),
3284  table_ref(NULL),
3285  master(NULL),
3286  concat_ws(NULL),
3287  hints(NULL),
3289  null_on_null = false;
3290  }
3291 
3292  bool itemize(Parse_context *pc, Item **res) override;
3293 
3294  void cleanup() override {
3295  DBUG_ENTER("Item_func_match::cleanup");
3297  if (!master && ft_handler) {
3299  destroy(hints);
3300  }
3301  ft_handler = NULL;
3302  concat_ws = NULL;
3303  table_ref = NULL; // required by Item_func_match::eq()
3304  master = NULL;
3306  }
3307  Item *key_item() const override { return against; }
3308  enum Functype functype() const override { return FT_FUNC; }
3309  const char *func_name() const override { return "match"; }
3310  void update_used_tables() override {}
3311  bool fix_fields(THD *thd, Item **ref) override;
3312  bool eq(const Item *, bool binary_cmp) const override;
3313  /* The following should be safe, even if we compare doubles */
3314  longlong val_int() override {
3315  DBUG_ASSERT(fixed);
3316  return val_real() != 0.0;
3317  }
3318  double val_real() override;
3319  void print(const THD *thd, String *str,
3320  enum_query_type query_type) const override;
3321 
3322  bool fix_index(const THD *thd);
3323  bool init_search(THD *thd);
3324  bool check_function_as_value_generator(uchar *checker_args) override {
3327  checker_args);
3328  func_arg->banned_function_name = func_name();
3329  return true;
3330  }
3331 
3332  /**
3333  Get number of matching rows from FT handler.
3334 
3335  @note Requires that FT handler supports the extended API
3336 
3337  @return Number of matching rows in result
3338  */
3342 
3343  return ((FT_INFO_EXT *)ft_handler)
3344  ->could_you->count_matches((FT_INFO_EXT *)ft_handler);
3345  }
3346 
3347  /**
3348  Check whether FT result is ordered on rank
3349 
3350  @return true if result is ordered
3351  @return false otherwise
3352  */
3354  DBUG_ASSERT(!master);
3355  if (hints->get_flags() & FT_SORTED) return true;
3356 
3358  return false;
3359 
3361  return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3363  }
3364 
3365  /**
3366  Check whether FT result contains the document ID
3367 
3368  @return true if document ID is available
3369  @return false otherwise
3370  */
3373 
3375  return false;
3376 
3377  return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3379  }
3380 
3381  float get_filtering_effect(THD *thd, table_map filter_for_table,
3382  table_map read_tables,
3383  const MY_BITMAP *fields_to_ignore,
3384  double rows_in_table) override;
3385 
3386  /**
3387  Returns master MATCH function.
3388 
3389  @return pointer to master MATCH function.
3390  */
3392  if (master) return master->get_master();
3393  return this;
3394  }
3395 
3396  /**
3397  Set master MATCH function and adjust used_in_where_only value.
3398 
3399  @param item item for which master should be set.
3400  */
3403  item->master = this;
3404  }
3405 
3406  /**
3407  Returns pointer to Ft_hints object belonging to master MATCH function.
3408 
3409  @return pointer to Ft_hints object
3410  */
3412  DBUG_ASSERT(!master);
3413  return hints;
3414  }
3415 
3416  /**
3417  Set comparison operation type and and value for master MATCH function.
3418 
3419  @param type comparison operation type
3420  @param value_arg comparison operation value
3421  */
3422  void set_hints_op(enum ft_operation type, double value_arg) {
3423  DBUG_ASSERT(!master);
3424  hints->set_hint_op(type, value_arg);
3425  }
3426 
3427  /**
3428  Set FT hints.
3429  */
3430  void set_hints(JOIN *join, uint ft_flag, ha_rows ft_limit, bool no_cond);
3431 
3432  /**
3433  Check if ranking is not needed.
3434 
3435  @return true if ranking is not needed
3436  @return false otherwise
3437  */
3439  DBUG_ASSERT(!master);
3440  return (!(hints->get_flags() & FT_SORTED) && // FT_SORTED is no set
3441  used_in_where_only && // MATCH result is not used
3442  // in expression
3443  hints->get_op_type() == FT_OP_NO); // MATCH is single function
3444  }
3445 
3446  /**
3447  Set flag that the function is a simple expression.
3448 
3449  @param val true if the function is a simple expression, false otherwise
3450  */
3451  void set_simple_expression(bool val) {
3452  DBUG_ASSERT(!master);
3453  simple_expression = val;
3454  }
3455 
3456  /**
3457  Check if this MATCH function is a simple expression in WHERE condition.
3458 
3459  @return true if simple expression
3460  @return false otherwise
3461  */
3463  DBUG_ASSERT(!master);
3464  return simple_expression;
3465  }
3466 
3467  private:
3468  /**
3469  Fulltext index hints, initialized for master MATCH function only.
3470  */
3472  /**
3473  Flag is true when MATCH function is used as a simple expression in
3474  WHERE condition, i.e. there is no AND/OR combinations, just simple
3475  MATCH function or [MATCH, rank] comparison operation.
3476  */
3478  /**
3479  true if MATCH function is used in WHERE condition only.
3480  Used to dermine what hints can be used for FT handler.
3481  Note that only master MATCH function has valid value.
3482  it's ok since only master function is involved in the hint processing.
3483  */
3485  /**
3486  Check whether storage engine for given table,
3487  allows FTS Boolean search on non-indexed columns.
3488 
3489  @todo A flag should be added to the extended fulltext API so that
3490  it may be checked whether search on non-indexed columns are
3491  supported. Currently, it is not possible to check for such a
3492  flag since @c this->ft_handler is not yet set when this function is
3493  called. The current hack is to assume that search on non-indexed
3494  columns are supported for engines that does not support the extended
3495  fulltext API (e.g., MyISAM), while it is not supported for other
3496  engines (e.g., InnoDB)
3497 
3498  @param tr Table for which storage engine to check
3499 
3500  @retval true if BOOLEAN search on non-indexed columns is supported
3501  @retval false otherwise
3502  */
3504  // Only Boolean search may support non_indexed columns
3505  if (!(flags & FT_BOOL)) return false;
3506 
3507  DBUG_ASSERT(tr && tr->file);
3508 
3509  // Assume that if extended fulltext API is not supported,
3510  // non-indexed columns are allowed. This will be true for MyISAM.
3511  if ((tr->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0) return true;
3512 
3513  return false;
3514  }
3515 };
3516 
3519 
3521 
3522  public:
3524 
3525  bool itemize(Parse_context *pc, Item **res) override;
3526  longlong val_int() override;
3527  const char *func_name() const override { return "is_free_lock"; }
3528  bool resolve_type(THD *) override {
3529  max_length = 1;
3530  maybe_null = true;
3531  return false;
3532  }
3533  bool is_non_const_over_literals(uchar *) override { return true; }
3534  bool check_function_as_value_generator(uchar *checker_args) override {
3537  checker_args);
3538  func_arg->banned_function_name = func_name();
3539  return true;
3540  }
3541 };
3542 
3545 
3547 
3548  public:
3550 
3551  bool itemize(Parse_context *pc, Item **res) override;
3552  longlong val_int() override;
3553  const char *func_name() const override { return "is_used_lock"; }
3554  bool resolve_type(THD *) override {
3555  unsigned_flag = true;
3556  maybe_null = true;
3557  return false;
3558  }
3559  bool is_non_const_over_literals(uchar *) override { return true; }
3560  bool check_function_as_value_generator(uchar *checker_args) override {
3563  checker_args);
3564  func_arg->banned_function_name = func_name();
3565  return true;
3566  }
3567 };
3568 
3571 
3572  public:
3574 
3575  bool itemize(Parse_context *pc, Item **res) override;
3576 
3577  longlong val_int() override;
3578  const char *func_name() const override { return "row_count"; }
3579  bool resolve_type(THD *) override {
3580  maybe_null = false;
3581  return false;
3582  }
3583  bool check_function_as_value_generator(uchar *checker_args) override {
3586  checker_args);
3587  func_arg->banned_function_name = func_name();
3588  return true;
3589  }
3590 };
3591 
3592 /*
3593  *
3594  * Stored FUNCTIONs
3595  *
3596  */
3597 
3598 class sp_head;
3599 class sp_name;
3600 
3601 class Item_func_sp final : public Item_func {
3602  typedef Item_func super;
3603 
3604  private:
3607  mutable sp_head *m_sp;
3610  /*
3611  The result field of the concrete stored function.
3612  */
3614 
3615  bool execute();
3616  bool execute_impl(THD *thd);
3617  bool init_result_field(THD *thd);
3618 
3619  protected:
3620  bool is_expensive_processor(uchar *) override { return true; }
3622  bool no_conversions) override;
3623 
3624  public:
3625  Item_func_sp(const POS &pos, const LEX_STRING &db_name,
3626  const LEX_STRING &fn_name, bool use_explicit_name,
3627  PT_item_list *opt_list);
3628 
3629  bool itemize(Parse_context *pc, Item **res) override;
3630  /**
3631  Must not be called before the procedure is resolved,
3632  i.e. @c init_result_field().
3633  */
3634  table_map get_initial_pseudo_tables() const override;
3635  void update_used_tables() override;
3636  void fix_after_pullout(SELECT_LEX *parent_select,
3637  SELECT_LEX *removed_select) override;
3638  void cleanup() override;
3639 
3640  const char *func_name() const override;
3641 
3642  Field *tmp_table_field(TABLE *t_arg) override;
3643 
3644  void make_field(Send_field *tmp_field) override;
3645 
3646  Item_result result_type() const override;
3647 
3648  longlong val_int() override {
3649  if (execute() || null_value) return (longlong)0;
3650  return sp_result_field->val_int();
3651  }
3652 
3653  double val_real() override {
3654  if (execute() || null_value) return 0.0;
3655  return sp_result_field->val_real();
3656  }
3657 
3658  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
3659  if (execute() || null_value) return true;
3660  return sp_result_field->get_date(ltime, fuzzydate);
3661  }
3662 
3663  bool get_time(MYSQL_TIME *ltime) override {
3664  if (execute() || null_value) return true;
3665 
3666  return sp_result_field->get_time(ltime);
3667  }
3668 
3669  my_decimal *val_decimal(my_decimal *dec_buf) override {
3670  if (execute() || null_value) return NULL;
3671  return sp_result_field->val_decimal(dec_buf);
3672  }
3673 
3674  String *val_str(String *str) override {
3675  String buf;
3676  char buff[20];
3677  buf.set(buff, 20, str->charset());
3678  buf.length(0);
3679  if (execute() || null_value) return NULL;
3680  /*
3681  result_field will set buf pointing to internal buffer
3682  of the resul_field. Due to this it will change any time
3683  when SP is executed. In order to prevent occasional
3684  corruption of returned value, we make here a copy.
3685  */
3686  sp_result_field->val_str(&buf);
3687  str->copy(buf);
3688  return str;
3689  }
3690 
3691  bool val_json(Json_wrapper *result) override;
3692 
3693  bool change_context_processor(uchar *cntx) override {
3694  context = reinterpret_cast<Name_resolution_context *>(cntx);
3695  return false;
3696  }
3697 
3698  bool sp_check_access(THD *thd);
3699  enum Functype functype() const override { return FUNC_SP; }
3700 
3701  bool fix_fields(THD *thd, Item **ref) override;
3702  bool resolve_type(THD *thd) override;
3703 
3704  bool is_expensive() override { return true; }
3705 
3707  bool check_function_as_value_generator(uchar *checker_args) override {
3710  checker_args);
3711  func_arg->banned_function_name = func_name();
3712  return true;
3713  }
3714 };
3715 
3718 
3719  public:
3721 
3722  bool itemize(Parse_context *pc, Item **res) override;
3723  longlong val_int() override;
3724  const char *func_name() const override { return "found_rows"; }
3725  bool resolve_type(THD *) override {
3726  maybe_null = false;
3727  return false;
3728  }
3729  bool check_function_as_value_generator(uchar *checker_args) override {
3732  checker_args);
3733  func_arg->banned_function_name = func_name();
3734  return true;
3735  }
3736 };
3737 
3738 void uuid_short_init();
3739 
3742 
3743  public:
3745 
3746  bool itemize(Parse_context *pc, Item **res) override;
3747  const char *func_name() const override { return "uuid_short"; }
3748  longlong val_int() override;
3749  bool resolve_type(THD *) override {
3750  unsigned_flag = true;
3751  return false;
3752  }
3753  bool check_partition_func_processor(uchar *) override { return false; }
3754  bool check_function_as_value_generator(uchar *checker_args) override {
3757  checker_args);
3758  func_arg->banned_function_name = func_name();
3759  return ((func_arg->source == VGS_GENERATED_COLUMN) ||
3760  (func_arg->source == VGS_CHECK_CONSTRAINT));
3761  }
3762 };
3763 
3766 
3767  public:
3768  explicit Item_func_version(const POS &pos);
3769 
3770  bool itemize(Parse_context *pc, Item **res) override;
3771 };
3772 
3774  LEX_STRING component);
3775 extern bool check_reserved_words(LEX_STRING *name);
3776 extern enum_field_types agg_field_type(Item **items, uint nitems);
3777 double my_double_round(double value, longlong dec, bool dec_unsigned,
3778  bool truncate);
3779 bool eval_const_cond(THD *thd, Item *cond, bool *value);
3781 
3782 void retrieve_tablespace_statistics(THD *thd, Item **args, bool *null_value);
3783 
3784 extern bool volatile mqh_used;
3785 
3786 #endif /* ITEM_FUNC_INCLUDED */
Definition: sql_udf.h:42
bool get_time(MYSQL_TIME *ltime) override
Definition: item_func.h:2027
Definition: item_func.h:1201
longlong val_int() override
Definition: item_func.cc:3321
Definition: item_func.h:2034
const char * func_name() const override
Definition: item_func.h:1555
Definition: item_func.h:2661
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize()/contextualize_() but with additional parameter.
Definition: item_func.cc:4612
Definition: item_func.h:157
longlong int_op() override
Performs the operation that this functions implements when the result type is INT.
Definition: item_func.cc:2705
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1118
my_decimal * val_decimal_from_real(my_decimal *decimal_value)
Definition: item.cc:297
longlong val_int() override
Definition: item_func.cc:8479
Item *(Item::* Item_transformer)(uchar *arg)
Definition: item.h:663
enum Item_result result_type() const override
Definition: item_func.h:1633
Item_func_bit(const POS &pos, Item *a, Item *b)
Definition: item_func.h:1629
enum Functype functype() const override
Definition: item_func.h:1082
Item_func_instr(const POS &pos, Item *a, Item *b)
Definition: item_func.h:1544
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize()/contextualize_() but with additional parameter.
Definition: item_func.cc:6958
Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e)
Definition: item_func.h:307
Item_real_func(Item *a)
Definition: item_func.h:644
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:788
Definition: item.h:3287
Wrapper for struct ft_hints.
Definition: handler.h:3448
unsigned long long int ulonglong
Definition: my_inttypes.h:69
Definition: item_func.h:975
Item_name_string item_name
Name from query.
Definition: item.h:2618
This class is used for implementing the new wait_for_executed_gtid_set function and the functions rel...
Definition: item_func.h:2190
Definition: field.h:4576
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:2754
virtual bool time_op(MYSQL_TIME *ltime)=0
Definition: item_func.h:143
Item_wait_for_executed_gtid_set(const POS &pos, Item *a)
Definition: item_func.h:2196
void init(THD *thd, const Simple_cstring &name, const CHARSET_INFO *cs)
Initialize all members.
Definition: item_func.cc:5164
double val_real() override
Definition: item_func.cc:2274
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:6878
Item_func_can_access_view(const POS &pos, Item *a, Item *b, Item *c, Item *d)
Definition: item_func.h:2332
enum Functype functype() const override
Definition: item_func.h:2687
enum Functype functype() const override
Definition: item_func.h:988
~Audit_global_variable_get_event()
Definition: item_func.cc:6187
double val_real() override
Definition: item_func.cc:2241
String search_value
Definition: item_func.h:3267
double real_op() override
Performs the operation that this functions implements when the result type is REAL.
Definition: item_func.cc:2031
virtual double val_real()=0
longlong val_int() override
Definition: item_func.cc:1207
Definition: item_func.h:125
const char * func_name() const override
Definition: item_func.h:1516
Item_func_can_access_table(const POS &pos, Item *a, Item *b)
Definition: item_func.h:2270
enum Functype functype() const override
Definition: item_func.h:1421
const char * ptr() const
Definition: item_func.h:2962
Item_real_func(const POS &pos, PT_item_list *list)
Definition: item_func.h:657
unsigned char uchar
Definition: my_inttypes.h:49
Definition: sql_optimizer.h:177
Item_func(Item *a, Item *b, Item *c, Item *d)
Definition: item_func.h:266
double val_real() override
Definition: item_func.cc:5557
double val_real() override
Definition: item_func.cc:2260
Our own string classes, used pervasively throughout the executor.
double val_real() override
Definition: item_func.cc:2253
bool check_partition_func_processor(uchar *) override
Check if a partition function is allowed.
Definition: item_func.h:1068
bool binary_result_requires_binary_second_arg() const override
Definition: item_func.h:1778
enum Functype functype() const override
Definition: item_func.h:2707
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_func.cc:1385
longlong int_op() override
Performs the operation that this functions implements when the result type is INT.
Definition: item_func.cc:2037
Item_int_func()
Definition: item_func.h:829
optimize_type
Definition: item_func.h:204
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_func.h:2004
virtual type_conversion_status save_possibly_as_json(Field *field, bool no_conversions)
Same as save_in_field() except that special logic is added to avoid serialization to string followed ...
Definition: item_func.cc:555
Item_int_func super
Definition: item_func.h:2157
void result_precision() override
Definition: item_func.cc:2013
"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:3007
longlong raise_integer_overflow()
Definition: item.h:4565
Definition: item_func.h:138
not valid for UDFs
Definition: udf_registration_types.h:40
enum Item_result result_type() const override
Definition: item_func.h:1364
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.h:1480
bool change_context_processor(uchar *cntx) override
Definition: item_func.h:3693
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:1699
Item_func_numhybrid(const POS &pos, Item *a, Item *b)
Definition: item_func.h:694
Definition: item_func.h:1039
Item_func_can_access_event(const POS &pos, Item *a)
Definition: item_func.h:2308
ulonglong table_map
Definition: my_table_map.h:32
Item_dec_func(const POS &pos, Item *a, Item *b)
Definition: item_func.h:1112
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_func.cc:3894
const char * func_name() const override
Definition: item_func.h:1143
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:491
const char * func_name() const override
Definition: item_func.h:2139
const char * func_name() const override
Definition: item_func.h:1180
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.h:2436
Definition: item_func.h:1416
Table_flags ha_table_flags() const
The cached_table_flags is set at ha_open and ha_external_lock.
Definition: handler.h:4246
Item_func(List< Item > &list)
Definition: item_func.h:318
uint decimal_precision() const override
Definition: item_func.h:2101
enum_field_types data_type() const
Retrieve the derived data type of the Item.
Definition: item.h:982
const char * func_name() const override
Definition: item_func.h:1708
Definition: mysql_lex_string.h:34
THD * m_thd
Definition: item_func.h:3175
void set_value(char *value, size_t length)
Definition: item_func.h:2808
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:1525
Definition: item_func.h:3569
const char * func_name() const override
Definition: item_func.h:1255
my_decimal * decimal_op(my_decimal *) override
Performs the operation that this functions implements when the result type is DECIMAL.
Definition: item_func.cc:2696
bool time_op(MYSQL_TIME *) override
Definition: item_func.h:792
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:3050
longlong int_op() override
Performs the operation that this functions implements when the result type is INT.
Definition: item_func.cc:1695
Definition: item_func.h:2518
String * str_op(String *str) override
Performs the operation on binary strings to produce a result of type STRING_RESULT.
Definition: item_func.h:1712
Item_int_func super
Definition: item_func.h:2132
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:429
longlong int_op() override
Performs the operation on integers to produce a result of type INT_RESULT.
Definition: item_func.h:1772
Item_func_min_max(const POS &pos, PT_item_list *opt_list, bool is_least_func)
Definition: item_func.h:1348
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.cc:3484
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize()/contextualize_() but with additional parameter.
Definition: item_func.cc:6994
const char * func_name() const
Definition: item_func.h:2361
Item_func_mod(Item *a, Item *b)
Definition: item_func.h:1059
String * val_str(bool *null_value, String *str, uint decimals) const
Get the value of a variable as a string.
Definition: item_func.cc:5351
void set_type(Item_result type)
Set type of to the given value.
Definition: item_func.h:2919
longlong val_int() override
INFORMATION_SCHEMA picks metadata from DD using system views.
Definition: item_func.cc:7922
Definition: item_func.h:1073
longlong int_op() override
Performs the operation on integers to produce a result of type INT_RESULT.
Definition: item_func.h:1711
Definition: item_func.h:3077
Item_func_internal_dd_char_length(const POS &pos, Item *a, Item *b, Item *c, Item *d)
Definition: item_func.h:2520
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.cc:2024
String * vstr
Definition: item_func.h:3006
Definition: item_func.h:159
void set_data_type_from_item(Item *item)
Set data type properties of the item from the properties of another item.
Definition: item.h:1248
const char * func_name() const override
Definition: item_func.h:1081
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:338
enum Functype functype() const override
Definition: item_func.h:2587
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:817
bool compare_as_dates() const
Returns true if arguments to this function should be compared as dates.
Definition: item_func.h:1375
Item_int_func super
Definition: item_func.h:884
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.h:3579
Definition: mysql_com.h:1015
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:1842
bool get_time_from_real(MYSQL_TIME *ltime)
Convert val_real() to time in MYSQL_TIME.
Definition: item.cc:1286
Definition: item_func.h:1057
Item_func_tan(const POS &pos, Item *a)
Definition: item_func.h:1211
Definition: item_func.h:188
Definition: item_func.h:1238
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_func.h:1982
my_decimal * decimal_op(my_decimal *) override
See Item_func_plus::decimal_op for comments.
Definition: item_func.cc:1765
Definition: item_func.h:135
#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:3754
longlong val_int() override
INFORMATION_SCHEMA picks metadata from DD using system views.
Definition: item_func.cc:7528
const char * func_name() const override
Definition: item_func.h:2273
Definition: item.h:703
Field * get_sp_result_field()
Definition: item_func.h:3706
enum Item_result result_type() const override
Definition: item_func.cc:6149
Definition: item_func.h:1092
bool agg_item_charsets(DTCollation &coll, const char *fname, Item **args, uint nargs, uint flags, int item_sep)
Definition: item.cc:2315
void fix_num_length_and_dec() override
Definition: item_func.cc:2608
void cleanup() override
Definition: item_func.cc:3889
longlong int_op() override
Performs the operation that this functions implements when the result type is INT.
Definition: item_func.cc:2665
Item_func_get_user_var(Name_string a)
Definition: item_func.h:3085
my_decimal * val_decimal(my_decimal *dec_buf) override
Definition: item_func.h:2057
longlong val_int() override
Definition: item_func.cc:6968
longlong val_int() override
Definition: item_func.cc:7307
Definition: item_func.h:183
String value
Definition: item_func.h:2158
bool check_partition_func_processor(uchar *) override
Check if a partition function is allowed.
Definition: item_func.h:1053
char fn_name[64]
Definition: dbug_analyze.cc:301
enum Functype functype() const override
Definition: item_func.h:959
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize()/contextualize_() but with additional parameter.
Definition: item_func.cc:2930
Name_resolution_context * context
Definition: item_func.h:3605
longlong val_int() override
Definition: item_func.cc:3450
bool agg_arg_charsets_for_string_result(DTCollation &c, Item **items, uint nitems, int item_sep=1)
Definition: item_func.h:434
Item_func_internal_table_rows(const POS &pos, PT_item_list *list)
Definition: item_func.h:2371
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.h:1517
Item_func_internal_tablespace_initial_size(const POS &pos, Item *a, Item *b, Item *c, Item *d)
Definition: item_func.h:2663
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.cc:2287
Definition: item_func.h:3543
longlong val_int() override
Definition: item_func.cc:4839
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_func.h:2494
Item_func_log10(const POS &pos, Item *a)
Definition: item_func.h:1148
bool fix_fields(THD *thd, Item_result_field *item, uint arg_count, Item **args)
Definition: item_func.cc:3624
Definition: item_func.h:3601
File containing constants that can be used throughout the server.
Item_func_rollup_const(Item *a)
Definition: item_func.h:1443
longlong val_int() override
Definition: item_func.cc:8213
Item_int_func(List< Item > &list)
Definition: item_func.h:862
Definition: item_func.h:128
double val_real() override
Definition: item_func.cc:2194
Audit_global_variable_get_event(THD *thd, Item_func_get_system_var *item, uchar cache_type)
Definition: item_func.cc:6173
void signal_divide_by_null()
Definition: item_func.cc:604