MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
item_func.h
1 #ifndef ITEM_FUNC_INCLUDED
2 #define ITEM_FUNC_INCLUDED
3 
4 /* Copyright (c) 2000, 2013, 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 as published by
8  the Free Software Foundation; version 2 of the License.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18 
19 
20 /* Function items used by mysql */
21 
22 #ifdef HAVE_IEEEFP_H
23 extern "C" /* Bug in BSDI include file */
24 {
25 #include <ieeefp.h>
26 }
27 #endif
28 
30 {
31 protected:
32  Item **args, *tmp_arg[2];
35  /*
36  Allowed numbers of columns in result (usually 1, which means scalar value)
37  0 means get this number from first argument
38  */
39  uint allowed_arg_cols;
41  table_map used_tables_cache;
44 public:
45  uint arg_count;
46  //bool const_item_cache;
47  enum Functype { UNKNOWN_FUNC,EQ_FUNC,EQUAL_FUNC,NE_FUNC,LT_FUNC,LE_FUNC,
48  GE_FUNC,GT_FUNC,FT_FUNC,
49  LIKE_FUNC,ISNULL_FUNC,ISNOTNULL_FUNC,
50  COND_AND_FUNC, COND_OR_FUNC, XOR_FUNC,
51  BETWEEN, IN_FUNC, MULT_EQUAL_FUNC,
52  INTERVAL_FUNC, ISNOTNULLTEST_FUNC,
53  SP_EQUALS_FUNC, SP_DISJOINT_FUNC,SP_INTERSECTS_FUNC,
54  SP_TOUCHES_FUNC,SP_CROSSES_FUNC,SP_WITHIN_FUNC,
55  SP_CONTAINS_FUNC,SP_OVERLAPS_FUNC,
56  SP_STARTPOINT,SP_ENDPOINT,SP_EXTERIORRING,
57  SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN,
58  NOT_FUNC, NOT_ALL_FUNC,
59  NOW_FUNC, TRIG_COND_FUNC,
60  SUSERVAR_FUNC, GUSERVAR_FUNC, COLLATE_FUNC,
61  EXTRACT_FUNC, CHAR_TYPECAST_FUNC, FUNC_SP, UDF_FUNC,
62  NEG_FUNC, GSYSVAR_FUNC };
63  enum optimize_type { OPTIMIZE_NONE,OPTIMIZE_KEY,OPTIMIZE_OP, OPTIMIZE_NULL,
64  OPTIMIZE_EQUAL };
65  enum Type type() const { return FUNC_ITEM; }
66  virtual enum Functype functype() const { return UNKNOWN_FUNC; }
67  Item_func(void):
68  allowed_arg_cols(1), arg_count(0)
69  {
70  with_sum_func= 0;
71  }
72  Item_func(Item *a):
73  allowed_arg_cols(1), arg_count(1)
74  {
75  args= tmp_arg;
76  args[0]= a;
77  with_sum_func= a->with_sum_func;
78  }
79  Item_func(Item *a,Item *b):
80  allowed_arg_cols(1), arg_count(2)
81  {
82  args= tmp_arg;
83  args[0]= a; args[1]= b;
84  with_sum_func= a->with_sum_func || b->with_sum_func;
85  }
86  Item_func(Item *a,Item *b,Item *c):
87  allowed_arg_cols(1)
88  {
89  arg_count= 0;
90  if ((args= (Item**) sql_alloc(sizeof(Item*)*3)))
91  {
92  arg_count= 3;
93  args[0]= a; args[1]= b; args[2]= c;
94  with_sum_func= a->with_sum_func || b->with_sum_func || c->with_sum_func;
95  }
96  }
97  Item_func(Item *a,Item *b,Item *c,Item *d):
98  allowed_arg_cols(1)
99  {
100  arg_count= 0;
101  if ((args= (Item**) sql_alloc(sizeof(Item*)*4)))
102  {
103  arg_count= 4;
104  args[0]= a; args[1]= b; args[2]= c; args[3]= d;
105  with_sum_func= a->with_sum_func || b->with_sum_func ||
106  c->with_sum_func || d->with_sum_func;
107  }
108  }
109  Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
110  allowed_arg_cols(1)
111  {
112  arg_count= 5;
113  if ((args= (Item**) sql_alloc(sizeof(Item*)*5)))
114  {
115  args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
116  with_sum_func= a->with_sum_func || b->with_sum_func ||
117  c->with_sum_func || d->with_sum_func || e->with_sum_func ;
118  }
119  }
120  Item_func(List<Item> &list);
121  // Constructor used for Item_cond_and/or (see Item comment)
122  Item_func(THD *thd, Item_func *item);
123  bool fix_fields(THD *, Item **ref);
124  void fix_after_pullout(st_select_lex *parent_select,
125  st_select_lex *removed_select);
126  table_map used_tables() const;
132  virtual table_map get_initial_pseudo_tables() const { return 0; }
133  table_map not_null_tables() const;
134  void update_used_tables();
135  void set_used_tables(table_map map) { used_tables_cache= map; }
136  void set_not_null_tables(table_map map) { not_null_tables_cache= map; }
137  bool eq(const Item *item, bool binary_cmp) const;
138  virtual optimize_type select_optimize() const { return OPTIMIZE_NONE; }
139  virtual bool have_rev_func() const { return 0; }
140  virtual Item *key_item() const { return args[0]; }
141  virtual bool const_item() const { return const_item_cache; }
142  inline Item **arguments() const
143  { DBUG_ASSERT(argument_count() > 0); return args; }
144  void set_arguments(List<Item> &list);
145  inline uint argument_count() const { return arg_count; }
146  inline void remove_arguments() { arg_count=0; }
147  void split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
148  List<Item> &fields);
149  virtual void print(String *str, enum_query_type query_type);
150  void print_op(String *str, enum_query_type query_type);
151  void print_args(String *str, uint from, enum_query_type query_type);
152  virtual void fix_num_length_and_dec();
153  void count_only_length(Item **item, uint nitems);
154  void count_real_length();
155  void count_decimal_length();
156  void count_datetime_length(Item **item, uint nitems);
157  bool count_string_result_length(enum_field_types field_type,
158  Item **item, uint nitems);
159  inline bool get_arg0_date(MYSQL_TIME *ltime, uint fuzzy_date)
160  {
161  return (null_value=args[0]->get_date(ltime, fuzzy_date));
162  }
163  inline bool get_arg0_time(MYSQL_TIME *ltime)
164  {
165  return (null_value= args[0]->get_time(ltime));
166  }
167  bool is_null() {
168  update_null_value();
169  return null_value;
170  }
171  void signal_divide_by_null();
172  friend class udf_handler;
173  Field *tmp_table_field() { return result_field; }
174  Field *tmp_table_field(TABLE *t_arg);
175  Item *get_tmp_table_item(THD *thd);
176 
177  my_decimal *val_decimal(my_decimal *);
178 
179  bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems,
180  uint flags, int item_sep)
181  {
182  return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep);
183  }
184  /*
185  Aggregate arguments for string result, e.g: CONCAT(a,b)
186  - convert to @@character_set_connection if all arguments are numbers
187  - allow DERIVATION_NONE
188  */
189  bool agg_arg_charsets_for_string_result(DTCollation &c,
190  Item **items, uint nitems,
191  int item_sep= 1)
192  {
193  return agg_item_charsets_for_string_result(c, func_name(),
194  items, nitems, item_sep);
195  }
196  /*
197  Aggregate arguments for comparison, e.g: a=b, a LIKE b, a RLIKE b
198  - don't convert to @@character_set_connection if all arguments are numbers
199  - don't allow DERIVATION_NONE
200  */
201  bool agg_arg_charsets_for_comparison(DTCollation &c,
202  Item **items, uint nitems,
203  int item_sep= 1)
204  {
205  return agg_item_charsets_for_comparison(c, func_name(),
206  items, nitems, item_sep);
207  }
208  /*
209  Aggregate arguments for string result, when some comparison
210  is involved internally, e.g: REPLACE(a,b,c)
211  - convert to @@character_set_connection if all arguments are numbers
212  - disallow DERIVATION_NONE
213  */
214  bool agg_arg_charsets_for_string_result_with_comparison(DTCollation &c,
215  Item **items,
216  uint nitems,
217  int item_sep= 1)
218  {
219  return agg_item_charsets_for_string_result_with_comparison(c, func_name(),
220  items, nitems,
221  item_sep);
222  }
223  bool walk(Item_processor processor, bool walk_subquery, uchar *arg);
224  Item *transform(Item_transformer transformer, uchar *arg);
225  Item* compile(Item_analyzer analyzer, uchar **arg_p,
226  Item_transformer transformer, uchar *arg_t);
227  void traverse_cond(Cond_traverser traverser,
228  void * arg, traverse_order order);
229  inline double fix_result(double value)
230  {
231  if (isfinite(value))
232  return value;
233  null_value=1;
234  return 0.0;
235  }
236  inline void raise_numeric_overflow(const char *type_name)
237  {
238  char buf[256];
239  String str(buf, sizeof(buf), system_charset_info);
240  str.length(0);
241  print(&str, QT_ORDINARY);
242  my_error(ER_DATA_OUT_OF_RANGE, MYF(0), type_name, str.c_ptr_safe());
243  }
244  inline double raise_float_overflow()
245  {
246  raise_numeric_overflow("DOUBLE");
247  return 0.0;
248  }
249  inline longlong raise_integer_overflow()
250  {
251  raise_numeric_overflow(unsigned_flag ? "BIGINT UNSIGNED": "BIGINT");
252  return 0;
253  }
254  inline int raise_decimal_overflow()
255  {
256  raise_numeric_overflow("DECIMAL");
257  return E_DEC_OVERFLOW;
258  }
263  inline double check_float_overflow(double value)
264  {
265  return isfinite(value) ? value : raise_float_overflow();
266  }
272  inline longlong check_integer_overflow(longlong value, bool val_unsigned)
273  {
274  if ((unsigned_flag && !val_unsigned && value < 0) ||
275  (!unsigned_flag && val_unsigned &&
276  (ulonglong) value > (ulonglong) LONGLONG_MAX))
277  return raise_integer_overflow();
278  return value;
279  }
283  inline int check_decimal_overflow(int error)
284  {
285  return (error == E_DEC_OVERFLOW) ? raise_decimal_overflow() : error;
286  }
287 
288  bool has_timestamp_args()
289  {
290  DBUG_ASSERT(fixed == TRUE);
291  for (uint i= 0; i < arg_count; i++)
292  {
293  if (args[i]->type() == Item::FIELD_ITEM &&
294  args[i]->field_type() == MYSQL_TYPE_TIMESTAMP)
295  return TRUE;
296  }
297  return FALSE;
298  }
299 
300  bool has_date_args()
301  {
302  DBUG_ASSERT(fixed == TRUE);
303  for (uint i= 0; i < arg_count; i++)
304  {
305  if (args[i]->type() == Item::FIELD_ITEM &&
306  (args[i]->field_type() == MYSQL_TYPE_DATE ||
307  args[i]->field_type() == MYSQL_TYPE_DATETIME))
308  return TRUE;
309  }
310  return FALSE;
311  }
312 
313  bool has_time_args()
314  {
315  DBUG_ASSERT(fixed == TRUE);
316  for (uint i= 0; i < arg_count; i++)
317  {
318  if (args[i]->type() == Item::FIELD_ITEM &&
319  (args[i]->field_type() == MYSQL_TYPE_TIME ||
320  args[i]->field_type() == MYSQL_TYPE_DATETIME))
321  return TRUE;
322  }
323  return FALSE;
324  }
325 
326  bool has_datetime_args()
327  {
328  DBUG_ASSERT(fixed == TRUE);
329  for (uint i= 0; i < arg_count; i++)
330  {
331  if (args[i]->type() == Item::FIELD_ITEM &&
332  args[i]->field_type() == MYSQL_TYPE_DATETIME)
333  return TRUE;
334  }
335  return FALSE;
336  }
337 
338  /*
339  We assume the result of any function that has a TIMESTAMP argument to be
340  timezone-dependent, since a TIMESTAMP value in both numeric and string
341  contexts is interpreted according to the current timezone.
342  The only exception is UNIX_TIMESTAMP() which returns the internal
343  representation of a TIMESTAMP argument verbatim, and thus does not depend on
344  the timezone.
345  */
346  virtual bool check_valid_arguments_processor(uchar *bool_arg)
347  {
348  return has_timestamp_args();
349  }
350 
351  virtual bool find_function_processor (uchar *arg)
352  {
353  return functype() == *(Functype *) arg;
354  }
355 };
356 
357 
359 {
360 public:
361  Item_real_func() :Item_func() { collation.set_numeric(); }
362  Item_real_func(Item *a) :Item_func(a) { collation.set_numeric(); }
363  Item_real_func(Item *a,Item *b) :Item_func(a,b) { collation.set_numeric(); }
364  Item_real_func(List<Item> &list) :Item_func(list) { collation.set_numeric(); }
365  String *val_str(String*str);
366  my_decimal *val_decimal(my_decimal *decimal_value);
367  longlong val_int()
368  { DBUG_ASSERT(fixed == 1); return (longlong) rint(val_real()); }
369  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
370  {
371  return get_date_from_real(ltime, fuzzydate);
372  }
373  bool get_time(MYSQL_TIME *ltime)
374  {
375  return get_time_from_real(ltime);
376  }
377  enum Item_result result_type () const { return REAL_RESULT; }
378  void fix_length_and_dec()
379  { decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
380 };
381 
382 
384 {
385 protected:
386  Item_result hybrid_type;
387 public:
388  Item_func_numhybrid(Item *a) :Item_func(a), hybrid_type(REAL_RESULT)
389  { collation.set_numeric(); }
391  :Item_func(a,b), hybrid_type(REAL_RESULT)
392  { collation.set_numeric(); }
394  :Item_func(list), hybrid_type(REAL_RESULT)
395  { collation.set_numeric(); }
396 
397  enum Item_result result_type () const { return hybrid_type; }
398  void fix_length_and_dec();
399  void fix_num_length_and_dec();
400  virtual void find_num_type()= 0; /* To be called from fix_length_and_dec */
401 
402  double val_real();
403  longlong val_int();
404  my_decimal *val_decimal(my_decimal *);
405  String *val_str(String*str);
406  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
407  bool get_time(MYSQL_TIME *ltime);
414  virtual longlong int_op()= 0;
415 
422  virtual double real_op()= 0;
423 
434  virtual my_decimal *decimal_op(my_decimal *)= 0;
435 
442  virtual String *str_op(String *)= 0;
449  virtual bool date_op(MYSQL_TIME *ltime, uint fuzzydate)= 0;
450  virtual bool time_op(MYSQL_TIME *ltime)= 0;
451  bool is_null() { update_null_value(); return null_value; }
452 };
453 
454 /* function where type of result detected by first argument */
456 {
457 public:
459  Item_func_num1(Item *a, Item *b) :Item_func_numhybrid(a, b) {}
460 
461  void fix_num_length_and_dec();
462  void find_num_type();
463  String *str_op(String *str) { DBUG_ASSERT(0); return 0; }
464  bool date_op(MYSQL_TIME *ltime, uint fuzzydate)
465  { DBUG_ASSERT(0); return 0; }
466  bool time_op(MYSQL_TIME *ltime)
467  { DBUG_ASSERT(0); return 0; }
468 };
469 
470 
471 /* Base class for operations like '+', '-', '*' */
473 {
474  public:
475  Item_num_op(Item *a,Item *b) :Item_func_numhybrid(a, b) {}
476  virtual void result_precision()= 0;
477 
478  virtual inline void print(String *str, enum_query_type query_type)
479  {
480  print_op(str, query_type);
481  }
482 
483  void find_num_type();
484  String *str_op(String *str) { DBUG_ASSERT(0); return 0; }
485  bool date_op(MYSQL_TIME *ltime, uint fuzzydate)
486  { DBUG_ASSERT(0); return 0; }
487  bool time_op(MYSQL_TIME *ltime)
488  { DBUG_ASSERT(0); return 0; }
489 };
490 
491 
493 {
494 public:
496  { collation.set_numeric(); fix_char_length(21); }
498  { collation.set_numeric(); fix_char_length(21); }
499  Item_int_func(Item *a,Item *b) :Item_func(a,b)
500  { collation.set_numeric(); fix_char_length(21); }
501  Item_int_func(Item *a,Item *b,Item *c) :Item_func(a,b,c)
502  { collation.set_numeric(); fix_char_length(21); }
503  Item_int_func(List<Item> &list) :Item_func(list)
504  { collation.set_numeric(); fix_char_length(21); }
505  Item_int_func(THD *thd, Item_int_func *item) :Item_func(thd, item)
506  { collation.set_numeric(); }
507  double val_real();
508  String *val_str(String*str);
509  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
510  {
511  return get_date_from_int(ltime, fuzzydate);
512  }
513  bool get_time(MYSQL_TIME *ltime)
514  {
515  return get_time_from_int(ltime);
516  }
517  enum Item_result result_type () const { return INT_RESULT; }
518  void fix_length_and_dec() {}
519 };
520 
521 
523 {
524  longlong value;
525 
526 public:
528  const char *func_name() const { return "connection_id"; }
529  void fix_length_and_dec();
530  bool fix_fields(THD *thd, Item **ref);
531  longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
532 };
533 
534 
536 {
537 public:
539  {
540  unsigned_flag= 0;
541  }
542  const char *func_name() const { return "cast_as_signed"; }
543  longlong val_int();
544  longlong val_int_from_str(int *error);
545  void fix_length_and_dec()
546  {
547  fix_char_length(std::min<uint32>(args[0]->max_char_length(),
548  MY_INT64_NUM_DECIMAL_DIGITS));
549  }
550  virtual void print(String *str, enum_query_type query_type);
551  uint decimal_precision() const { return args[0]->decimal_precision(); }
552 };
553 
554 
556 {
557 public:
559  {
560  unsigned_flag= 1;
561  }
562  const char *func_name() const { return "cast_as_unsigned"; }
563  longlong val_int();
564  virtual void print(String *str, enum_query_type query_type);
565 };
566 
567 
569 {
570  my_decimal decimal_value;
571 public:
572  Item_decimal_typecast(Item *a, int len, int dec) :Item_func(a)
573  {
574  decimals= dec;
575  collation.set_numeric();
576  fix_char_length(my_decimal_precision_to_length_no_truncation(len, dec,
577  unsigned_flag));
578  }
579  String *val_str(String *str);
580  double val_real();
581  longlong val_int();
582  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
583  {
584  return get_date_from_decimal(ltime, fuzzydate);
585  }
586  bool get_time(MYSQL_TIME *ltime)
587  {
588  return get_time_from_decimal(ltime);
589  }
590  my_decimal *val_decimal(my_decimal*);
591  enum Item_result result_type () const { return DECIMAL_RESULT; }
592  enum_field_types field_type() const { return MYSQL_TYPE_NEWDECIMAL; }
593  void fix_length_and_dec() {};
594  const char *func_name() const { return "decimal_typecast"; }
595  virtual void print(String *str, enum_query_type query_type);
596 };
597 
598 
600 {
601 public:
603  void result_precision();
604  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
605 };
606 
607 
609 {
610 public:
612  const char *func_name() const { return "+"; }
613  longlong int_op();
614  double real_op();
616 };
617 
619 {
620 public:
622  const char *func_name() const { return "-"; }
623  longlong int_op();
624  double real_op();
626  void fix_length_and_dec();
627 };
628 
629 
631 {
632 public:
633  Item_func_mul(Item *a,Item *b) :Item_num_op(a,b) {}
634  const char *func_name() const { return "*"; }
635  longlong int_op();
636  double real_op();
638  void result_precision();
639  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
640 };
641 
642 
644 {
645 public:
646  uint prec_increment;
647  Item_func_div(Item *a,Item *b) :Item_num_op(a,b) {}
648  longlong int_op() { DBUG_ASSERT(0); return 0; }
649  double real_op();
651  const char *func_name() const { return "/"; }
652  void fix_length_and_dec();
653  void result_precision();
654 };
655 
656 
658 {
659 public:
661  {}
662  longlong val_int();
663  const char *func_name() const { return "DIV"; }
664  void fix_length_and_dec();
665 
666  virtual inline void print(String *str, enum_query_type query_type)
667  {
668  print_op(str, query_type);
669  }
670 
671  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
672 };
673 
674 
676 {
677 public:
678  Item_func_mod(Item *a,Item *b) :Item_num_op(a,b) {}
679  longlong int_op();
680  double real_op();
682  const char *func_name() const { return "%"; }
683  void result_precision();
684  void fix_length_and_dec();
685  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
686 };
687 
688 
690 {
691 public:
693  double real_op();
694  longlong int_op();
696  const char *func_name() const { return "-"; }
697  enum Functype functype() const { return NEG_FUNC; }
698  void fix_length_and_dec();
699  void fix_num_length_and_dec();
700  uint decimal_precision() const { return args[0]->decimal_precision(); }
701  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
702 };
703 
704 
706 {
707 public:
709  double real_op();
710  longlong int_op();
712  const char *func_name() const { return "abs"; }
713  void fix_length_and_dec();
714  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
715 };
716 
717 // A class to handle logarithmic and trigonometric functions
718 
720 {
721  public:
723  Item_dec_func(Item *a,Item *b) :Item_real_func(a,b) {}
724  void fix_length_and_dec()
725  {
726  decimals=NOT_FIXED_DEC; max_length=float_length(decimals);
727  maybe_null=1;
728  }
729 };
730 
732 {
733 public:
734  Item_func_exp(Item *a) :Item_dec_func(a) {}
735  double val_real();
736  const char *func_name() const { return "exp"; }
737 };
738 
739 
741 {
742 public:
743  Item_func_ln(Item *a) :Item_dec_func(a) {}
744  double val_real();
745  const char *func_name() const { return "ln"; }
746 };
747 
748 
750 {
751 public:
752  Item_func_log(Item *a) :Item_dec_func(a) {}
753  Item_func_log(Item *a,Item *b) :Item_dec_func(a,b) {}
754  double val_real();
755  const char *func_name() const { return "log"; }
756 };
757 
758 
760 {
761 public:
763  double val_real();
764  const char *func_name() const { return "log2"; }
765 };
766 
767 
769 {
770 public:
772  double val_real();
773  const char *func_name() const { return "log10"; }
774 };
775 
776 
778 {
779 public:
781  double val_real();
782  const char *func_name() const { return "sqrt"; }
783 };
784 
785 
787 {
788 public:
789  Item_func_pow(Item *a,Item *b) :Item_dec_func(a,b) {}
790  double val_real();
791  const char *func_name() const { return "pow"; }
792 };
793 
794 
796 {
797 public:
799  double val_real();
800  const char *func_name() const { return "acos"; }
801 };
802 
804 {
805 public:
807  double val_real();
808  const char *func_name() const { return "asin"; }
809 };
810 
812 {
813 public:
815  Item_func_atan(Item *a,Item *b) :Item_dec_func(a,b) {}
816  double val_real();
817  const char *func_name() const { return "atan"; }
818 };
819 
821 {
822 public:
823  Item_func_cos(Item *a) :Item_dec_func(a) {}
824  double val_real();
825  const char *func_name() const { return "cos"; }
826 };
827 
829 {
830 public:
831  Item_func_sin(Item *a) :Item_dec_func(a) {}
832  double val_real();
833  const char *func_name() const { return "sin"; }
834 };
835 
837 {
838 public:
839  Item_func_tan(Item *a) :Item_dec_func(a) {}
840  double val_real();
841  const char *func_name() const { return "tan"; }
842 };
843 
845 {
846 public:
847  Item_func_cot(Item *a) :Item_dec_func(a) {}
848  double val_real();
849  const char *func_name() const { return "cot"; }
850 };
851 
853 {
854 public:
855  inline Item_func_integer(Item *a) :Item_int_func(a) {}
856  void fix_length_and_dec();
857 };
858 
859 
861 {
862 public:
864  void fix_num_length_and_dec();
865  void find_num_type();
866 };
867 
868 
870 {
871 public:
873  const char *func_name() const { return "ceiling"; }
874  longlong int_op();
875  double real_op();
877  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
878 };
879 
880 
882 {
883 public:
885  const char *func_name() const { return "floor"; }
886  longlong int_op();
887  double real_op();
889  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
890 };
891 
892 /* This handles round and truncate */
893 
895 {
896  bool truncate;
897 public:
898  Item_func_round(Item *a, Item *b, bool trunc_arg)
899  :Item_func_num1(a,b), truncate(trunc_arg) {}
900  const char *func_name() const { return truncate ? "truncate" : "round"; }
901  double real_op();
902  longlong int_op();
904  void fix_length_and_dec();
905 };
906 
907 
909 {
910  struct rand_struct *rand;
911  bool first_eval; // TRUE if val_real() is called 1st time
912 public:
913  Item_func_rand(Item *a) :Item_real_func(a), rand(0), first_eval(TRUE) {}
915  double val_real();
916  const char *func_name() const { return "rand"; }
917  bool const_item() const { return 0; }
924  table_map get_initial_pseudo_tables() const { return RAND_TABLE_BIT; }
925  bool fix_fields(THD *thd, Item **ref);
926  void cleanup() { first_eval= TRUE; Item_real_func::cleanup(); }
927 private:
928  void seed_random (Item * val);
929 };
930 
931 
933 {
934 public:
936  const char *func_name() const { return "sign"; }
937  longlong val_int();
938 };
939 
940 
942 {
943  char *name;
944  double mul,add;
945 public:
946  Item_func_units(char *name_arg,Item *a,double mul_arg,double add_arg)
947  :Item_real_func(a),name(name_arg),mul(mul_arg),add(add_arg) {}
948  double val_real();
949  const char *func_name() const { return name; }
950  void fix_length_and_dec()
951  { decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
952 };
953 
954 
956 {
957  Item_result cmp_type;
958  String tmp_value;
959  int cmp_sign;
960  /* TRUE <=> arguments should be compared in the DATETIME context. */
961  bool compare_as_dates;
962  /* An item used for issuing warnings while string to DATETIME conversion. */
963  Item *datetime_item;
964  THD *thd;
965 protected:
966  enum_field_types cached_field_type;
967  uint cmp_datetimes(longlong *value);
968  uint cmp_times(longlong *value);
969 public:
970  Item_func_min_max(List<Item> &list,int cmp_sign_arg) :Item_func(list),
971  cmp_type(INT_RESULT), cmp_sign(cmp_sign_arg), compare_as_dates(FALSE),
972  datetime_item(0) {}
973  double val_real();
974  longlong val_int();
975  String *val_str(String *);
976  my_decimal *val_decimal(my_decimal *);
977  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
978  bool get_time(MYSQL_TIME *ltime);
979  void fix_length_and_dec();
980  enum Item_result result_type () const
981  {
982  /*
983  If we compare as dates, then:
984  - field_type is MYSQL_TYPE_VARSTRING, MYSQL_TYPE_DATETIME
985  or MYSQL_TYPE_DATE.
986  - cmp_type is INT_RESULT or DECIMAL_RESULT,
987  depending on the amount of fractional digits.
988  We need to return STRING_RESULT in this case instead of cmp_type.
989  */
990  return compare_as_dates ? STRING_RESULT : cmp_type;
991  }
992  enum Item_result cast_to_int_type () const
993  {
994  /*
995  make CAST(LEAST_OR_GREATEST(datetime_expr, varchar_expr))
996  return a number in format "YYYMMDDhhmmss".
997  */
998  return compare_as_dates ? INT_RESULT : result_type();
999  }
1000  enum_field_types field_type() const { return cached_field_type; }
1001 };
1002 
1004 {
1005 public:
1006  Item_func_min(List<Item> &list) :Item_func_min_max(list,1) {}
1007  const char *func_name() const { return "least"; }
1008 };
1009 
1011 {
1012 public:
1013  Item_func_max(List<Item> &list) :Item_func_min_max(list,-1) {}
1014  const char *func_name() const { return "greatest"; }
1015 };
1016 
1017 
1018 /*
1019  Objects of this class are used for ROLLUP queries to wrap up
1020  each constant item referred to in GROUP BY list.
1021 */
1022 
1024 {
1025 public:
1027  {
1028  item_name= a->item_name;
1029  }
1030  double val_real() { return args[0]->val_real(); }
1031  longlong val_int() { return args[0]->val_int(); }
1032  String *val_str(String *str) { return args[0]->val_str(str); }
1033  my_decimal *val_decimal(my_decimal *dec) { return args[0]->val_decimal(dec); }
1034  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
1035  {
1036  return args[0]->get_date(ltime, fuzzydate);
1037  }
1038  bool get_time(MYSQL_TIME *ltime)
1039  {
1040  return args[0]->get_time(ltime);
1041  }
1042  const char *func_name() const { return "rollup_const"; }
1043  bool const_item() const { return 0; }
1044  Item_result result_type() const { return args[0]->result_type(); }
1045  void fix_length_and_dec()
1046  {
1047  collation= args[0]->collation;
1048  max_length= args[0]->max_length;
1049  decimals=args[0]->decimals;
1050  /* The item could be a NULL constant. */
1051  null_value= args[0]->is_null();
1052  }
1053  enum_field_types field_type() const { return args[0]->field_type(); }
1054 };
1055 
1056 
1058 {
1059  String value;
1060 public:
1062  longlong val_int();
1063  const char *func_name() const { return "length"; }
1064  void fix_length_and_dec() { max_length=10; }
1065 };
1066 
1068 {
1069 public:
1071  longlong val_int()
1072  { DBUG_ASSERT(fixed == 1); return Item_func_length::val_int()*8; }
1073  const char *func_name() const { return "bit_length"; }
1074 };
1075 
1077 {
1078  String value;
1079 public:
1081  longlong val_int();
1082  const char *func_name() const { return "char_length"; }
1083  void fix_length_and_dec() { max_length=10; }
1084 };
1085 
1087 {
1088 public:
1090  longlong val_int();
1091  const char *func_name() const { return "coercibility"; }
1092  void fix_length_and_dec() { max_length=10; maybe_null= 0; }
1093  table_map not_null_tables() const { return 0; }
1094 };
1095 
1097 {
1098  String value1,value2;
1099  DTCollation cmp_collation;
1100 public:
1101  Item_func_locate(Item *a,Item *b) :Item_int_func(a,b) {}
1102  Item_func_locate(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
1103  const char *func_name() const { return "locate"; }
1104  longlong val_int();
1105  void fix_length_and_dec();
1106  virtual void print(String *str, enum_query_type query_type);
1107 };
1108 
1109 
1111 {
1112  String value;
1113 public:
1115  longlong val_int();
1116  const char *func_name() const { return "validate_password_strength"; }
1117  void fix_length_and_dec() { max_length= 10; maybe_null= 1; }
1118 };
1119 
1120 
1122 {
1123  String value,tmp;
1124  Item_result cmp_type;
1125  DTCollation cmp_collation;
1126 public:
1127  Item_func_field(List<Item> &list) :Item_int_func(list) {}
1128  longlong val_int();
1129  const char *func_name() const { return "field"; }
1130  void fix_length_and_dec();
1131 };
1132 
1133 
1135 {
1136  String value;
1137 public:
1139  longlong val_int();
1140  const char *func_name() const { return "ascii"; }
1141  void fix_length_and_dec() { max_length=3; }
1142 };
1143 
1145 {
1146  String value;
1147 public:
1148  Item_func_ord(Item *a) :Item_int_func(a) {}
1149  longlong val_int();
1150  const char *func_name() const { return "ord"; }
1151 };
1152 
1154 {
1155  String value,value2;
1156  uint enum_value;
1157  ulonglong enum_bit;
1158  DTCollation cmp_collation;
1159 public:
1160  Item_func_find_in_set(Item *a,Item *b) :Item_int_func(a,b),enum_value(0) {}
1161  longlong val_int();
1162  const char *func_name() const { return "find_in_set"; }
1163  void fix_length_and_dec();
1164 };
1165 
1166 /* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
1167 
1169 {
1170 public:
1171  Item_func_bit(Item *a, Item *b) :Item_int_func(a, b) {}
1172  Item_func_bit(Item *a) :Item_int_func(a) {}
1173  void fix_length_and_dec() { unsigned_flag= 1; }
1174 
1175  virtual inline void print(String *str, enum_query_type query_type)
1176  {
1177  print_op(str, query_type);
1178  }
1179 };
1180 
1182 {
1183 public:
1184  Item_func_bit_or(Item *a, Item *b) :Item_func_bit(a, b) {}
1185  longlong val_int();
1186  const char *func_name() const { return "|"; }
1187 };
1188 
1190 {
1191 public:
1192  Item_func_bit_and(Item *a, Item *b) :Item_func_bit(a, b) {}
1193  longlong val_int();
1194  const char *func_name() const { return "&"; }
1195 };
1196 
1198 {
1199 public:
1201  longlong val_int();
1202  const char *func_name() const { return "bit_count"; }
1203  void fix_length_and_dec() { max_length=2; }
1204 };
1205 
1207 {
1208 public:
1209  Item_func_shift_left(Item *a, Item *b) :Item_func_bit(a, b) {}
1210  longlong val_int();
1211  const char *func_name() const { return "<<"; }
1212 };
1213 
1215 {
1216 public:
1217  Item_func_shift_right(Item *a, Item *b) :Item_func_bit(a, b) {}
1218  longlong val_int();
1219  const char *func_name() const { return ">>"; }
1220 };
1221 
1223 {
1224 public:
1226  longlong val_int();
1227  const char *func_name() const { return "~"; }
1228 
1229  virtual inline void print(String *str, enum_query_type query_type)
1230  {
1231  Item_func::print(str, query_type);
1232  }
1233 };
1234 
1235 
1237 {
1238 public:
1241  longlong val_int();
1242  const char *func_name() const { return "last_insert_id"; }
1243  void fix_length_and_dec()
1244  {
1245  unsigned_flag= TRUE;
1246  if (arg_count)
1247  max_length= args[0]->max_length;
1248  }
1249  bool fix_fields(THD *thd, Item **ref);
1250 };
1251 
1252 
1254 {
1255 public:
1256  Item_func_benchmark(Item *count_expr, Item *expr)
1257  :Item_int_func(count_expr, expr)
1258  {}
1259  longlong val_int();
1260  const char *func_name() const { return "benchmark"; }
1261  void fix_length_and_dec() { max_length=1; maybe_null=0; }
1262  virtual void print(String *str, enum_query_type query_type);
1263 };
1264 
1265 
1267 {
1268 public:
1270  bool const_item() const { return 0; }
1271  const char *func_name() const { return "sleep"; }
1278  table_map get_initial_pseudo_tables() const { return RAND_TABLE_BIT; }
1279  longlong val_int();
1280 };
1281 
1282 
1283 
1284 #ifdef HAVE_DLOPEN
1285 
1286 class Item_udf_func :public Item_func
1287 {
1288 protected:
1289  udf_handler udf;
1290  bool is_expensive_processor(uchar *arg) { return TRUE; }
1291 
1292 public:
1293  Item_udf_func(udf_func *udf_arg)
1294  :Item_func(), udf(udf_arg) {}
1295  Item_udf_func(udf_func *udf_arg, List<Item> &list)
1296  :Item_func(list), udf(udf_arg) {}
1297  const char *func_name() const { return udf.name(); }
1298  enum Functype functype() const { return UDF_FUNC; }
1299  bool fix_fields(THD *thd, Item **ref)
1300  {
1301  DBUG_ASSERT(fixed == 0);
1302  bool res= udf.fix_fields(thd, this, arg_count, args);
1303  used_tables_cache= udf.used_tables_cache;
1304  const_item_cache= udf.const_item_cache;
1305  fixed= 1;
1306  return res;
1307  }
1308  void update_used_tables()
1309  {
1310  /*
1311  TODO: Make a member in UDF_INIT and return if a UDF is deterministic or
1312  not.
1313  Currently UDF_INIT has a member (const_item) that is an in/out
1314  parameter to the init() call.
1315  The code in udf_handler::fix_fields also duplicates the arguments
1316  handling code in Item_func::fix_fields().
1317 
1318  The lack of information if a UDF is deterministic makes writing
1319  a correct update_used_tables() for UDFs impossible.
1320  One solution to this would be :
1321  - Add a is_deterministic member of UDF_INIT
1322  - (optionally) deprecate the const_item member of UDF_INIT
1323  - Take away the duplicate code from udf_handler::fix_fields() and
1324  make Item_udf_func call Item_func::fix_fields() to process its
1325  arguments as for any other function.
1326  - Store the deterministic flag returned by <udf>_init into the
1327  udf_handler.
1328  - Don't implement Item_udf_func::fix_fields, implement
1329  Item_udf_func::fix_length_and_dec() instead (similar to non-UDF
1330  functions).
1331  - Override Item_func::update_used_tables to call
1332  Item_func::update_used_tables() and add a RAND_TABLE_BIT to the
1333  result of Item_func::update_used_tables() if the UDF is
1334  non-deterministic.
1335  - (optionally) rename RAND_TABLE_BIT to NONDETERMINISTIC_BIT to
1336  better describe its usage.
1337 
1338  The above would require a change of the UDF API.
1339  Until that change is done here's how the current code works:
1340  We call Item_func::update_used_tables() only when we know that
1341  the function depends on real non-const tables and is deterministic.
1342  This can be done only because we know that the optimizer will
1343  call update_used_tables() only when there's possibly a new const
1344  table. So update_used_tables() can only make a Item_func more
1345  constant than it is currently.
1346  That's why we don't need to do anything if a function is guaranteed
1347  to return non-constant (it's non-deterministic) or is already a
1348  const.
1349  */
1350  if ((used_tables_cache & ~PSEUDO_TABLE_BITS) &&
1351  !(used_tables_cache & RAND_TABLE_BIT))
1352  {
1353  Item_func::update_used_tables();
1354  if (!const_item_cache && !used_tables_cache)
1355  used_tables_cache= RAND_TABLE_BIT;
1356  }
1357  }
1358  void cleanup();
1359  Item_result result_type () const { return udf.result_type(); }
1360  table_map not_null_tables() const { return 0; }
1361  bool is_expensive() { return 1; }
1362  virtual void print(String *str, enum_query_type query_type);
1363 };
1364 
1365 
1366 class Item_func_udf_float :public Item_udf_func
1367 {
1368  public:
1369  Item_func_udf_float(udf_func *udf_arg)
1370  :Item_udf_func(udf_arg) {}
1371  Item_func_udf_float(udf_func *udf_arg,
1372  List<Item> &list)
1373  :Item_udf_func(udf_arg, list) {}
1374  longlong val_int()
1375  {
1376  DBUG_ASSERT(fixed == 1);
1377  return (longlong) rint(Item_func_udf_float::val_real());
1378  }
1379  my_decimal *val_decimal(my_decimal *dec_buf)
1380  {
1381  double res=val_real();
1382  if (null_value)
1383  return NULL;
1384  double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
1385  return dec_buf;
1386  }
1387  double val_real();
1388  String *val_str(String *str);
1389  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
1390  {
1391  return get_date_from_real(ltime, fuzzydate);
1392  }
1393  bool get_time(MYSQL_TIME *ltime)
1394  {
1395  return get_time_from_real(ltime);
1396  }
1397  void fix_length_and_dec() { fix_num_length_and_dec(); }
1398 };
1399 
1400 
1401 class Item_func_udf_int :public Item_udf_func
1402 {
1403 public:
1404  Item_func_udf_int(udf_func *udf_arg)
1405  :Item_udf_func(udf_arg) {}
1406  Item_func_udf_int(udf_func *udf_arg,
1407  List<Item> &list)
1408  :Item_udf_func(udf_arg, list) {}
1409  longlong val_int();
1410  double val_real() { return (double) Item_func_udf_int::val_int(); }
1411  String *val_str(String *str);
1412  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
1413  {
1414  return get_date_from_int(ltime, fuzzydate);
1415  }
1416  bool get_time(MYSQL_TIME *ltime)
1417  {
1418  return get_time_from_int(ltime);
1419  }
1420  enum Item_result result_type () const { return INT_RESULT; }
1421  void fix_length_and_dec() { decimals= 0; max_length= 21; }
1422 };
1423 
1424 
1425 class Item_func_udf_decimal :public Item_udf_func
1426 {
1427 public:
1429  :Item_udf_func(udf_arg) {}
1430  Item_func_udf_decimal(udf_func *udf_arg, List<Item> &list)
1431  :Item_udf_func(udf_arg, list) {}
1432  longlong val_int();
1433  double val_real();
1434  my_decimal *val_decimal(my_decimal *);
1435  String *val_str(String *str);
1436  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
1437  {
1438  return get_date_from_decimal(ltime, fuzzydate);
1439  }
1440  bool get_time(MYSQL_TIME *ltime)
1441  {
1442  return get_time_from_decimal(ltime);
1443  }
1444  enum Item_result result_type () const { return DECIMAL_RESULT; }
1445  void fix_length_and_dec();
1446 };
1447 
1448 
1449 class Item_func_udf_str :public Item_udf_func
1450 {
1451 public:
1452  Item_func_udf_str(udf_func *udf_arg)
1453  :Item_udf_func(udf_arg) {}
1454  Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
1455  :Item_udf_func(udf_arg, list) {}
1456  String *val_str(String *);
1457  double val_real()
1458  {
1459  int err_not_used;
1460  char *end_not_used;
1461  String *res;
1462  res= val_str(&str_value);
1463  return res ? my_strntod(res->charset(),(char*) res->ptr(),
1464  res->length(), &end_not_used, &err_not_used) : 0.0;
1465  }
1466  longlong val_int()
1467  {
1468  int err_not_used;
1469  String *res; res=val_str(&str_value);
1470  return res ? my_strntoll(res->charset(),res->ptr(),res->length(),10,
1471  (char**) 0, &err_not_used) : (longlong) 0;
1472  }
1473  my_decimal *val_decimal(my_decimal *dec_buf)
1474  {
1475  String *res=val_str(&str_value);
1476  if (!res)
1477  return NULL;
1478  string2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
1479  return dec_buf;
1480  }
1481  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
1482  {
1483  return get_date_from_string(ltime, fuzzydate);
1484  }
1485  bool get_time(MYSQL_TIME *ltime)
1486  {
1487  return get_time_from_string(ltime);
1488  }
1489  enum Item_result result_type () const { return STRING_RESULT; }
1490  void fix_length_and_dec();
1491 };
1492 
1493 #else /* Dummy functions to get sql_yacc.cc compiled */
1494 
1495 
1497 {
1498  public:
1499  Item_func_udf_float(udf_func *udf_arg)
1500  :Item_real_func() {}
1501  Item_func_udf_float(udf_func *udf_arg, List<Item> &list)
1502  :Item_real_func(list) {}
1503  double val_real() { DBUG_ASSERT(fixed == 1); return 0.0; }
1504 };
1505 
1506 
1508 {
1509 public:
1510  Item_func_udf_int(udf_func *udf_arg)
1511  :Item_int_func() {}
1512  Item_func_udf_int(udf_func *udf_arg, List<Item> &list)
1513  :Item_int_func(list) {}
1514  longlong val_int() { DBUG_ASSERT(fixed == 1); return 0; }
1515 };
1516 
1517 
1519 {
1520 public:
1522  :Item_int_func() {}
1523  Item_func_udf_decimal(udf_func *udf_arg, List<Item> &list)
1524  :Item_int_func(list) {}
1525  my_decimal *val_decimal(my_decimal *) { DBUG_ASSERT(fixed == 1); return 0; }
1526 };
1527 
1528 
1530 {
1531 public:
1532  Item_func_udf_str(udf_func *udf_arg)
1533  :Item_func() {}
1534  Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
1535  :Item_func(list) {}
1536  String *val_str(String *)
1537  { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
1538  double val_real() { DBUG_ASSERT(fixed == 1); null_value= 1; return 0.0; }
1539  longlong val_int() { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
1540  enum Item_result result_type () const { return STRING_RESULT; }
1541  void fix_length_and_dec() { maybe_null=1; max_length=0; }
1542 };
1543 
1544 #endif /* HAVE_DLOPEN */
1545 
1546 /*
1547 ** User level locks
1548 */
1549 
1550 class User_level_lock;
1551 void item_user_lock_init(void);
1552 void item_user_lock_release(User_level_lock *ull);
1553 void item_user_lock_free(void);
1554 
1556 {
1557  String value;
1558  public:
1559  Item_func_get_lock(Item *a,Item *b) :Item_int_func(a,b) {}
1560  longlong val_int();
1561  const char *func_name() const { return "get_lock"; }
1562  void fix_length_and_dec() { max_length=1; maybe_null=1;}
1563 };
1564 
1566 {
1567  String value;
1568 public:
1570  longlong val_int();
1571  const char *func_name() const { return "release_lock"; }
1572  void fix_length_and_dec() { max_length=1; maybe_null=1;}
1573 };
1574 
1575 /* replication functions */
1576 
1578 {
1579  String value;
1580 public:
1582  Item_master_pos_wait(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
1583  longlong val_int();
1584  const char *func_name() const { return "master_pos_wait"; }
1585  void fix_length_and_dec() { max_length=21; maybe_null=1;}
1586 };
1587 
1589 {
1590  String value;
1591 public:
1594  longlong val_int();
1595  const char *func_name() const { return "wait_until_sql_thread_after_gtids"; }
1596  void fix_length_and_dec() { max_length= 21; maybe_null= 1; }
1597 };
1598 
1600 {
1601  String buf1;
1602  String buf2;
1603 public:
1604  Item_func_gtid_subset(Item *a, Item *b) : Item_int_func(a, b) {}
1605  longlong val_int();
1606  const char *func_name() const { return "gtid_subset"; }
1607  void fix_length_and_dec() { max_length= 21; maybe_null= 0; }
1608 };
1609 
1610 
1618 {
1619 public:
1620  Item_var_func() :Item_func() { }
1621  Item_var_func(THD *thd, Item_var_func *item) :Item_func(thd, item) { }
1622  Item_var_func(Item *a) :Item_func(a) { }
1623  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
1624  {
1625  return get_date_from_non_temporal(ltime, fuzzydate);
1626  }
1627  bool get_time(MYSQL_TIME *ltime)
1628  {
1629  return get_time_from_non_temporal(ltime);
1630  }
1631 };
1632 
1633 
1634 /* Handling of user definable variables */
1635 
1636 class user_var_entry;
1637 
1639 {
1640  enum Item_result cached_result_type;
1641  user_var_entry *entry;
1642  /*
1643  The entry_thread_id variable is used:
1644  1) to skip unnecessary updates of the entry field (see above);
1645  2) to reset the entry field that was initialized in the other thread
1646  (for example, an item tree of a trigger that updates user variables
1647  may be shared between several connections, and the entry_thread_id field
1648  prevents updates of one connection user variables from a concurrent
1649  connection calling the same trigger that initially updated some
1650  user variable it the first connection context).
1651  */
1652  my_thread_id entry_thread_id;
1666  bool delayed_non_constness;
1667  char buffer[MAX_FIELD_WIDTH];
1668  String value;
1669  my_decimal decimal_buff;
1670  bool null_item;
1671  union
1672  {
1673  longlong vint;
1674  double vreal;
1675  String *vstr;
1676  my_decimal *vdec;
1677  } save_result;
1678 
1679 public:
1680  Name_string name; // keep it public
1681  Item_func_set_user_var(Name_string a, Item *b, bool delayed)
1682  :Item_var_func(b), cached_result_type(INT_RESULT),
1683  entry(NULL), entry_thread_id(0), delayed_non_constness(delayed), name(a)
1684  {}
1686  :Item_var_func(thd, item), cached_result_type(item->cached_result_type),
1687  entry(item->entry), entry_thread_id(item->entry_thread_id),
1688  delayed_non_constness(item->delayed_non_constness), value(item->value),
1689  decimal_buff(item->decimal_buff), null_item(item->null_item),
1690  save_result(item->save_result), name(item->name)
1691  {}
1692  enum Functype functype() const { return SUSERVAR_FUNC; }
1693  double val_real();
1694  longlong val_int();
1695  String *val_str(String *str);
1696  my_decimal *val_decimal(my_decimal *);
1697  double val_result();
1698  longlong val_int_result();
1699  bool val_bool_result();
1700  String *str_result(String *str);
1701  my_decimal *val_decimal_result(my_decimal *);
1702  bool is_null_result();
1703  bool update_hash(void *ptr, uint length, enum Item_result type,
1704  const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
1705  bool send(Protocol *protocol, String *str_arg);
1706  void make_field(Send_field *tmp_field);
1707  bool check(bool use_result_field);
1708  void save_item_result(Item *item);
1709  bool update();
1710  enum Item_result result_type () const { return cached_result_type; }
1711  bool fix_fields(THD *thd, Item **ref);
1712  void fix_length_and_dec();
1713  virtual void print(String *str, enum_query_type query_type);
1714  void print_assignment(String *str, enum_query_type query_type);
1715  const char *func_name() const { return "set_user_var"; }
1716  type_conversion_status save_in_field(Field *field, bool no_conversions,
1717  bool can_use_result_field);
1718  type_conversion_status save_in_field(Field *field, bool no_conversions)
1719  {
1720  return save_in_field(field, no_conversions, 1);
1721  }
1722  void save_org_in_field(Field *field) { (void)save_in_field(field, 1, 0); }
1723  bool register_field_in_read_map(uchar *arg);
1724  bool set_entry(THD *thd, bool create_if_not_exists);
1725  void cleanup();
1726 };
1727 
1728 
1731 {
1732  user_var_entry *var_entry;
1733  Item_result m_cached_result_type;
1734 
1735 public:
1736  Name_string name; // keep it public
1738  Item_var_func(), m_cached_result_type(STRING_RESULT), name(a) {}
1739  enum Functype functype() const { return GUSERVAR_FUNC; }
1740  double val_real();
1741  longlong val_int();
1742  my_decimal *val_decimal(my_decimal*);
1743  String *val_str(String* str);
1744  void fix_length_and_dec();
1745  virtual void print(String *str, enum_query_type query_type);
1746  enum Item_result result_type() const;
1747  /*
1748  We must always return variables as strings to guard against selects of type
1749  select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
1750  */
1751  const char *func_name() const { return "get_user_var"; }
1752  bool const_item() const;
1753  table_map used_tables() const
1754  { return const_item() ? 0 : RAND_TABLE_BIT; }
1755  bool eq(const Item *item, bool binary_cmp) const;
1756 private:
1757  bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
1758 
1759 public:
1760  Settable_routine_parameter *get_settable_routine_parameter()
1761  {
1762  return this;
1763  }
1764 };
1765 
1766 
1767 /*
1768  This item represents user variable used as out parameter (e.g in LOAD DATA),
1769  and it is supposed to be used only for this purprose. So it is simplified
1770  a lot. Actually you should never obtain its value.
1771 
1772  The only two reasons for this thing being an Item is possibility to store it
1773  in List<Item> and desire to place this code somewhere near other functions
1774  working with user variables.
1775 */
1777 {
1778  Name_string name;
1779  user_var_entry *entry;
1780 public:
1782  { item_name.copy(a); }
1783  /* We should return something different from FIELD_ITEM here */
1784  enum Type type() const { return STRING_ITEM;}
1785  double val_real();
1786  longlong val_int();
1787  String *val_str(String *str);
1788  my_decimal *val_decimal(my_decimal *decimal_buffer);
1789  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
1790  {
1791  DBUG_ASSERT(0);
1792  return true;
1793  }
1794  bool get_time(MYSQL_TIME *ltime)
1795  {
1796  DBUG_ASSERT(0);
1797  return true;
1798  }
1799 
1800  /* fix_fields() binds variable name with its entry structure */
1801  bool fix_fields(THD *thd, Item **ref);
1802  virtual void print(String *str, enum_query_type query_type);
1803  void set_null_value(const CHARSET_INFO* cs);
1804  void set_value(const char *str, uint length, const CHARSET_INFO* cs);
1805 };
1806 
1807 
1808 /* A system variable */
1809 
1810 #define GET_SYS_VAR_CACHE_LONG 1
1811 #define GET_SYS_VAR_CACHE_DOUBLE 2
1812 #define GET_SYS_VAR_CACHE_STRING 4
1813 
1815 {
1816  sys_var *var;
1817  enum_var_type var_type, orig_var_type;
1818  LEX_STRING component;
1819  longlong cached_llval;
1820  double cached_dval;
1821  String cached_strval;
1822  my_bool cached_null_value;
1823  query_id_t used_query_id;
1824  uchar cache_present;
1825 
1826 public:
1827  Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
1828  LEX_STRING *component_arg, const char *name_arg,
1829  size_t name_len_arg);
1830  enum Functype functype() const { return GSYSVAR_FUNC; }
1831  void update_null_value();
1832  void fix_length_and_dec();
1833  void print(String *str, enum_query_type query_type);
1834  bool const_item() const { return true; }
1835  table_map used_tables() const { return 0; }
1836  enum Item_result result_type() const;
1837  enum_field_types field_type() const;
1838  double val_real();
1839  longlong val_int();
1840  String* val_str(String*);
1841  my_decimal *val_decimal(my_decimal *dec_buf)
1842  { return val_decimal_from_real(dec_buf); }
1843  /* TODO: fix to support views */
1844  const char *func_name() const { return "get_system_var"; }
1853  bool is_written_to_binlog();
1854  bool eq(const Item *item, bool binary_cmp) const;
1855 
1856  void cleanup();
1857 };
1858 
1859 
1860 /* for fulltext search */
1861 #include <ft_global.h>
1862 
1864 {
1865 public:
1866  uint key, flags;
1867  bool join_key;
1868  DTCollation cmp_collation;
1869  FT_INFO *ft_handler;
1870  TABLE *table;
1871  Item_func_match *master; // for master-slave optimization
1872  Item *concat_ws; // Item_func_concat_ws
1873  String value; // value of concat_ws
1874  String search_value; // key_item()'s value converted to cmp_collation
1875 
1876  Item_func_match(List<Item> &a, uint b): Item_real_func(a), key(0), flags(b),
1877  join_key(0), ft_handler(0), table(0), master(0), concat_ws(0) { }
1878  void cleanup()
1879  {
1880  DBUG_ENTER("Item_func_match::cleanup");
1881  Item_real_func::cleanup();
1882  if (!master && ft_handler)
1883  ft_handler->please->close_search(ft_handler);
1884  ft_handler= 0;
1885  concat_ws= 0;
1886  table= 0; // required by Item_func_match::eq()
1887  DBUG_VOID_RETURN;
1888  }
1889  enum Functype functype() const { return FT_FUNC; }
1890  const char *func_name() const { return "match"; }
1891  void update_used_tables() {}
1892  table_map not_null_tables() const { return 0; }
1893  bool fix_fields(THD *thd, Item **ref);
1894  bool eq(const Item *, bool binary_cmp) const;
1895  /* The following should be safe, even if we compare doubles */
1896  longlong val_int() { DBUG_ASSERT(fixed == 1); return val_real() != 0.0; }
1897  double val_real();
1898  virtual void print(String *str, enum_query_type query_type);
1899 
1900  bool fix_index();
1901  void init_search(bool no_order);
1902 
1910  ulonglong get_count()
1911  {
1912  DBUG_ASSERT(ft_handler);
1913  DBUG_ASSERT(table->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT);
1914 
1915  return ((FT_INFO_EXT *)ft_handler)->could_you->
1916  count_matches((FT_INFO_EXT *)ft_handler);
1917  }
1918 
1926  {
1927  if (flags & FT_SORTED)
1928  return true;
1929 
1930  if ((table->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0)
1931  return false;
1932 
1933  DBUG_ASSERT(ft_handler);
1934  return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
1935  FTS_ORDERED_RESULT;
1936  }
1937 
1945  {
1946  DBUG_ASSERT(ft_handler);
1947 
1948  if ((table->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0)
1949  return false;
1950 
1951  return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
1952  FTS_DOCID_IN_RESULT;
1953  }
1954 
1955 private:
1974  bool allows_search_on_non_indexed_columns(TABLE* table_arg)
1975  {
1976  // Only Boolean search may support non_indexed columns
1977  if (!(flags & FT_BOOL))
1978  return false;
1979 
1980  DBUG_ASSERT(table_arg && table_arg->file);
1981 
1982  // Assume that if extended fulltext API is not supported,
1983  // non-indexed columns are allowed. This will be true for MyISAM.
1984  if ((table_arg->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0)
1985  return true;
1986 
1987  return false;
1988  }
1989 
1990 };
1991 
1998 {
1999  FT_INFO_EXT *ft_handler;
2000 public:
2001  Item_func_docid(FT_INFO_EXT *handler) : ft_handler(handler)
2002  {
2003  max_length= 21;
2004  maybe_null= false;
2005  unsigned_flag= true;
2006  }
2007 
2008  const char *func_name() const { return "docid"; }
2009 
2010  void update_used_tables()
2011  {
2012  Item_int_func::update_used_tables();
2013  used_tables_cache|= RAND_TABLE_BIT;
2014  const_item_cache= false;
2015  }
2016 
2017  longlong val_int()
2018  {
2019  DBUG_ASSERT(ft_handler);
2020  return ft_handler->could_you->get_docid(ft_handler);
2021  }
2022 };
2023 
2025 {
2026 public:
2027  Item_func_bit_xor(Item *a, Item *b) :Item_func_bit(a, b) {}
2028  longlong val_int();
2029  const char *func_name() const { return "^"; }
2030 };
2031 
2033 {
2034  String value;
2035 public:
2037  longlong val_int();
2038  const char *func_name() const { return "is_free_lock"; }
2039  void fix_length_and_dec() { decimals=0; max_length=1; maybe_null=1;}
2040 };
2041 
2043 {
2044  String value;
2045 public:
2047  longlong val_int();
2048  const char *func_name() const { return "is_used_lock"; }
2049  void fix_length_and_dec() { decimals=0; max_length=10; maybe_null=1;}
2050 };
2051 
2052 /* For type casts */
2053 
2054 enum Cast_target
2055 {
2056  ITEM_CAST_BINARY, ITEM_CAST_SIGNED_INT, ITEM_CAST_UNSIGNED_INT,
2057  ITEM_CAST_DATE, ITEM_CAST_TIME, ITEM_CAST_DATETIME, ITEM_CAST_CHAR,
2058  ITEM_CAST_DECIMAL
2059 };
2060 
2061 
2063 {
2064 public:
2066  longlong val_int();
2067  const char *func_name() const { return "row_count"; }
2068  void fix_length_and_dec() { decimals= 0; maybe_null=0; }
2069 };
2070 
2071 
2072 /*
2073  *
2074  * Stored FUNCTIONs
2075  *
2076  */
2077 
2078 class sp_head;
2079 class sp_name;
2080 struct st_sp_security_context;
2081 
2083 {
2084 private:
2085  Name_resolution_context *context;
2086  sp_name *m_name;
2087  mutable sp_head *m_sp;
2088  TABLE *dummy_table;
2089  uchar result_buf[64];
2090  /*
2091  The result field of the concrete stored function.
2092  */
2093  Field *sp_result_field;
2094 
2095  bool execute();
2096  bool execute_impl(THD *thd);
2097  bool init_result_field(THD *thd);
2098 
2099 protected:
2100  bool is_expensive_processor(uchar *arg) { return TRUE; }
2101 
2102 public:
2103 
2105 
2107  sp_name *name, List<Item> &list);
2108 
2109  virtual ~Item_func_sp()
2110  {}
2111 
2116  table_map get_initial_pseudo_tables() const;
2117  void update_used_tables();
2118 
2119  void cleanup();
2120 
2121  const char *func_name() const;
2122 
2123  enum enum_field_types field_type() const;
2124 
2125  Field *tmp_table_field(TABLE *t_arg);
2126 
2127  void make_field(Send_field *tmp_field);
2128 
2129  Item_result result_type() const;
2130 
2131  longlong val_int()
2132  {
2133  if (execute())
2134  return (longlong) 0;
2135  return sp_result_field->val_int();
2136  }
2137 
2138  double val_real()
2139  {
2140  if (execute())
2141  return 0.0;
2142  return sp_result_field->val_real();
2143  }
2144 
2145  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
2146  {
2147  if (execute())
2148  return true;
2149  return sp_result_field->get_date(ltime, fuzzydate);
2150  }
2151 
2152  bool get_time(MYSQL_TIME *ltime)
2153  {
2154  if (execute())
2155  return true;
2156  return sp_result_field->get_time(ltime);
2157  }
2158 
2159  my_decimal *val_decimal(my_decimal *dec_buf)
2160  {
2161  if (execute())
2162  return NULL;
2163  return sp_result_field->val_decimal(dec_buf);
2164  }
2165 
2166  String *val_str(String *str)
2167  {
2168  String buf;
2169  char buff[20];
2170  buf.set(buff, 20, str->charset());
2171  buf.length(0);
2172  if (execute())
2173  return NULL;
2174  /*
2175  result_field will set buf pointing to internal buffer
2176  of the resul_field. Due to this it will change any time
2177  when SP is executed. In order to prevent occasional
2178  corruption of returned value, we make here a copy.
2179  */
2180  sp_result_field->val_str(&buf);
2181  str->copy(buf);
2182  return str;
2183  }
2184 
2185  virtual bool change_context_processor(uchar *cntx)
2186  { context= (Name_resolution_context *)cntx; return FALSE; }
2187 
2188  bool sp_check_access(THD * thd);
2189  virtual enum Functype functype() const { return FUNC_SP; }
2190 
2191  bool fix_fields(THD *thd, Item **ref);
2192  void fix_length_and_dec(void);
2193  bool is_expensive() { return 1; }
2194 
2195  inline Field *get_sp_result_field()
2196  {
2197  return sp_result_field;
2198  }
2199 
2200  virtual void update_null_value();
2201 };
2202 
2203 
2205 {
2206 public:
2208  longlong val_int();
2209  const char *func_name() const { return "found_rows"; }
2210  void fix_length_and_dec() { decimals= 0; maybe_null=0; }
2211 };
2212 
2213 
2214 void uuid_short_init();
2215 
2217 {
2218 public:
2220  const char *func_name() const { return "uuid_short"; }
2221  longlong val_int();
2222  void fix_length_and_dec()
2223  { max_length= 21; unsigned_flag=1; }
2224  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
2225 };
2226 
2227 Item *get_system_var(THD *thd, enum_var_type var_type, LEX_STRING name,
2228  LEX_STRING component);
2229 extern bool check_reserved_words(LEX_STRING *name);
2230 extern enum_field_types agg_field_type(Item **items, uint nitems);
2231 double my_double_round(double value, longlong dec, bool dec_unsigned,
2232  bool truncate);
2233 bool eval_const_cond(Item *cond);
2234 
2235 extern bool volatile mqh_used;
2236 
2237 #endif /* ITEM_FUNC_INCLUDED */