1 #ifndef ITEM_FUNC_INCLUDED
2 #define ITEM_FUNC_INCLUDED
3 /* Copyright (c) 2000, 2016, Oracle and/or its affiliates.
4    Copyright (c) 2009, 2016, MariaDB
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 Street, Fifth Floor, Boston, MA  02110-1335  USA */
18 
19 
20 /* Function items used by mysql */
21 
22 #ifdef USE_PRAGMA_INTERFACE
23 #pragma interface			/* gcc class implementation */
24 #endif
25 
26 #ifdef HAVE_IEEEFP_H
27 extern "C"				/* Bug in BSDI include file */
28 {
29 #include <ieeefp.h>
30 }
31 #endif
32 
33 #include "sql_udf.h"    // udf_handler
34 #include "my_decimal.h" // string2my_decimal
35 #include <cmath>
36 
37 
38 class Item_func :public Item_func_or_sum
39 {
40   void sync_with_sum_func_and_with_field(List<Item> &list);
41 protected:
42   String *val_str_from_val_str_ascii(String *str, String *str2);
43 
check_arguments()44   virtual bool check_arguments() const
45   {
46     return check_argument_types_scalar(0, arg_count);
47   }
48   bool check_argument_types_like_args0() const;
49   bool check_argument_types_scalar(uint start, uint end) const;
50   bool check_argument_types_traditional_scalar(uint start, uint end) const;
51   bool check_argument_types_or_binary(const Type_handler *handler,
52                                       uint start, uint end) const;
53   bool check_argument_types_can_return_int(uint start, uint end) const;
54   bool check_argument_types_can_return_real(uint start, uint end) const;
55   bool check_argument_types_can_return_str(uint start, uint end) const;
56   bool check_argument_types_can_return_text(uint start, uint end) const;
57   bool check_argument_types_can_return_date(uint start, uint end) const;
58   bool check_argument_types_can_return_time(uint start, uint end) const;
59 public:
60 
61   table_map not_null_tables_cache;
62 
63   enum Functype { UNKNOWN_FUNC,EQ_FUNC,EQUAL_FUNC,NE_FUNC,LT_FUNC,LE_FUNC,
64 		  GE_FUNC,GT_FUNC,FT_FUNC,
65 		  LIKE_FUNC,ISNULL_FUNC,ISNOTNULL_FUNC,
66 		  COND_AND_FUNC, COND_OR_FUNC, XOR_FUNC,
67                   BETWEEN, IN_FUNC, MULT_EQUAL_FUNC,
68 		  INTERVAL_FUNC, ISNOTNULLTEST_FUNC,
69 		  SP_EQUALS_FUNC, SP_DISJOINT_FUNC,SP_INTERSECTS_FUNC,
70 		  SP_TOUCHES_FUNC,SP_CROSSES_FUNC,SP_WITHIN_FUNC,
71 		  SP_CONTAINS_FUNC,SP_OVERLAPS_FUNC,
72 		  SP_STARTPOINT,SP_ENDPOINT,SP_EXTERIORRING,
73 		  SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN, SP_RELATE_FUNC,
74                   NOT_FUNC, NOT_ALL_FUNC, TEMPTABLE_ROWID,
75                   NOW_FUNC, NOW_UTC_FUNC, SYSDATE_FUNC, TRIG_COND_FUNC,
76                   SUSERVAR_FUNC, GUSERVAR_FUNC, COLLATE_FUNC,
77                   EXTRACT_FUNC, CHAR_TYPECAST_FUNC, FUNC_SP, UDF_FUNC,
78                   NEG_FUNC, GSYSVAR_FUNC, IN_OPTIMIZER_FUNC, DYNCOL_FUNC,
79                   JSON_EXTRACT_FUNC,
80                   CASE_SEARCHED_FUNC, // Used by ColumnStore/Spider
81                   CASE_SIMPLE_FUNC    // Used by ColumnStore/spider
82                 };
type()83   enum Type type() const { return FUNC_ITEM; }
functype()84   virtual enum Functype functype() const   { return UNKNOWN_FUNC; }
Item_func(THD * thd)85   Item_func(THD *thd): Item_func_or_sum(thd)
86   {
87     with_sum_func= 0;
88     with_field= 0;
89     with_param= 0;
90   }
Item_func(THD * thd,Item * a)91   Item_func(THD *thd, Item *a): Item_func_or_sum(thd, a)
92   {
93     with_sum_func= a->with_sum_func;
94     with_param= a->with_param;
95     with_field= a->with_field;
96   }
Item_func(THD * thd,Item * a,Item * b)97   Item_func(THD *thd, Item *a, Item *b):
98     Item_func_or_sum(thd, a, b)
99   {
100     with_sum_func= a->with_sum_func || b->with_sum_func;
101     with_param= a->with_param || b->with_param;
102     with_field= a->with_field || b->with_field;
103   }
Item_func(THD * thd,Item * a,Item * b,Item * c)104   Item_func(THD *thd, Item *a, Item *b, Item *c):
105     Item_func_or_sum(thd, a, b, c)
106   {
107     with_sum_func= a->with_sum_func || b->with_sum_func || c->with_sum_func;
108     with_field= a->with_field || b->with_field || c->with_field;
109     with_param= a->with_param || b->with_param || c->with_param;
110   }
Item_func(THD * thd,Item * a,Item * b,Item * c,Item * d)111   Item_func(THD *thd, Item *a, Item *b, Item *c, Item *d):
112     Item_func_or_sum(thd, a, b, c, d)
113   {
114     with_sum_func= a->with_sum_func || b->with_sum_func ||
115                    c->with_sum_func || d->with_sum_func;
116     with_field= a->with_field || b->with_field ||
117                 c->with_field || d->with_field;
118     with_param= a->with_param || b->with_param ||
119                 c->with_param || d->with_param;
120   }
Item_func(THD * thd,Item * a,Item * b,Item * c,Item * d,Item * e)121   Item_func(THD *thd, Item *a, Item *b, Item *c, Item *d, Item* e):
122     Item_func_or_sum(thd, a, b, c, d, e)
123   {
124     with_sum_func= a->with_sum_func || b->with_sum_func ||
125                    c->with_sum_func || d->with_sum_func || e->with_sum_func;
126     with_field= a->with_field || b->with_field ||
127                 c->with_field || d->with_field || e->with_field;
128     with_param= a->with_param || b->with_param ||
129                 c->with_param || d->with_param || e->with_param;
130   }
Item_func(THD * thd,List<Item> & list)131   Item_func(THD *thd, List<Item> &list):
132     Item_func_or_sum(thd, list)
133   {
134     set_arguments(thd, list);
135   }
136   // Constructor used for Item_cond_and/or (see Item comment)
Item_func(THD * thd,Item_func * item)137   Item_func(THD *thd, Item_func *item):
138     Item_func_or_sum(thd, item),
139     not_null_tables_cache(item->not_null_tables_cache)
140   {
141   }
142   bool fix_fields(THD *, Item **ref);
cleanup()143   void cleanup()
144   {
145     Item_func_or_sum::cleanup();
146     used_tables_and_const_cache_init();
147   }
148   void fix_after_pullout(st_select_lex *new_parent, Item **ref, bool merge);
149   void quick_fix_field();
150   table_map not_null_tables() const;
update_used_tables()151   void update_used_tables()
152   {
153     used_tables_and_const_cache_init();
154     used_tables_and_const_cache_update_and_join(arg_count, args);
155   }
156   COND *build_equal_items(THD *thd, COND_EQUAL *inherited,
157                           bool link_item_fields,
158                           COND_EQUAL **cond_equal_ref);
get_mm_tree(RANGE_OPT_PARAM * param,Item ** cond_ptr)159   SEL_TREE *get_mm_tree(RANGE_OPT_PARAM *param, Item **cond_ptr)
160   {
161     DBUG_ENTER("Item_func::get_mm_tree");
162     DBUG_RETURN(const_item() ? get_mm_tree_for_const(param) : NULL);
163   }
164   bool eq(const Item *item, bool binary_cmp) const;
key_item()165   virtual Item *key_item() const { return args[0]; }
set_arguments(THD * thd,List<Item> & list)166   void set_arguments(THD *thd, List<Item> &list)
167   {
168     Item_args::set_arguments(thd, list);
169     sync_with_sum_func_and_with_field(list);
170     list.empty();                                     // Fields are used
171   }
172   void split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
173                       List<Item> &fields, uint flags);
174   virtual void print(String *str, enum_query_type query_type);
175   void print_op(String *str, enum_query_type query_type);
176   void print_args(String *str, uint from, enum_query_type query_type);
get_arg0_date(MYSQL_TIME * ltime,ulonglong fuzzy_date)177   inline bool get_arg0_date(MYSQL_TIME *ltime, ulonglong fuzzy_date)
178   {
179     DBUG_ASSERT(!(fuzzy_date & TIME_TIME_ONLY));
180     Datetime dt(current_thd, args[0], fuzzy_date);
181     return (null_value= dt.copy_to_mysql_time(ltime));
182   }
is_null()183   bool is_null() {
184     update_null_value();
185     return null_value;
186   }
187   void signal_divide_by_null();
188   friend class udf_handler;
create_field_for_create_select(TABLE * table)189   Field *create_field_for_create_select(TABLE *table)
190   { return tmp_table_field_from_field_type(table); }
191   Item *get_tmp_table_item(THD *thd);
192 
193   my_decimal *val_decimal(my_decimal *);
194 
fix_char_length_ulonglong(ulonglong max_char_length_arg)195   void fix_char_length_ulonglong(ulonglong max_char_length_arg)
196   {
197     ulonglong max_result_length= max_char_length_arg *
198                                  collation.collation->mbmaxlen;
199     if (max_result_length >= MAX_BLOB_WIDTH)
200     {
201       max_length= MAX_BLOB_WIDTH;
202       maybe_null= 1;
203     }
204     else
205       max_length= (uint32) max_result_length;
206   }
207   Item *transform(THD *thd, Item_transformer transformer, uchar *arg);
208   Item* compile(THD *thd, Item_analyzer analyzer, uchar **arg_p,
209                 Item_transformer transformer, uchar *arg_t);
210   void traverse_cond(Cond_traverser traverser,
211                      void * arg, traverse_order order);
212   bool eval_not_null_tables(void *opt_arg);
213  // bool is_expensive_processor(void *arg);
214  // virtual bool is_expensive() { return 0; }
raise_numeric_overflow(const char * type_name)215   inline void raise_numeric_overflow(const char *type_name)
216   {
217     char buf[256];
218     String str(buf, sizeof(buf), system_charset_info);
219     str.length(0);
220     print(&str, QT_NO_DATA_EXPANSION);
221     my_error(ER_DATA_OUT_OF_RANGE, MYF(0), type_name, str.c_ptr_safe());
222   }
raise_float_overflow()223   inline double raise_float_overflow()
224   {
225     raise_numeric_overflow("DOUBLE");
226     return 0.0;
227   }
raise_integer_overflow()228   inline longlong raise_integer_overflow()
229   {
230     raise_numeric_overflow(unsigned_flag ? "BIGINT UNSIGNED": "BIGINT");
231     return 0;
232   }
raise_decimal_overflow()233   inline int raise_decimal_overflow()
234   {
235     raise_numeric_overflow("DECIMAL");
236     return E_DEC_OVERFLOW;
237   }
238   /**
239      Throw an error if the input double number is not finite, i.e. is either
240      +/-INF or NAN.
241   */
check_float_overflow(double value)242   inline double check_float_overflow(double value)
243   {
244     return std::isfinite(value) ? value : raise_float_overflow();
245   }
246   /**
247     Throw an error if the input BIGINT value represented by the
248     (longlong value, bool unsigned flag) pair cannot be returned by the
249     function, i.e. is not compatible with this Item's unsigned_flag.
250   */
check_integer_overflow(longlong value,bool val_unsigned)251   inline longlong check_integer_overflow(longlong value, bool val_unsigned)
252   {
253     if ((unsigned_flag && !val_unsigned && value < 0) ||
254         (!unsigned_flag && val_unsigned &&
255          (ulonglong) value > (ulonglong) LONGLONG_MAX))
256       return raise_integer_overflow();
257     return value;
258   }
259   /**
260      Throw an error if the error code of a DECIMAL operation is E_DEC_OVERFLOW.
261   */
check_decimal_overflow(int error)262   inline int check_decimal_overflow(int error)
263   {
264     return (error == E_DEC_OVERFLOW) ? raise_decimal_overflow() : error;
265   }
266 
has_timestamp_args()267   bool has_timestamp_args()
268   {
269     DBUG_ASSERT(fixed == TRUE);
270     for (uint i= 0; i < arg_count; i++)
271     {
272       if (args[i]->type() == Item::FIELD_ITEM &&
273           args[i]->field_type() == MYSQL_TYPE_TIMESTAMP)
274         return TRUE;
275     }
276     return FALSE;
277   }
278 
has_date_args()279   bool has_date_args()
280   {
281     DBUG_ASSERT(fixed == TRUE);
282     for (uint i= 0; i < arg_count; i++)
283     {
284       if (args[i]->type() == Item::FIELD_ITEM &&
285           (args[i]->field_type() == MYSQL_TYPE_DATE ||
286            args[i]->field_type() == MYSQL_TYPE_DATETIME))
287         return TRUE;
288     }
289     return FALSE;
290   }
291 
has_time_args()292   bool has_time_args()
293   {
294     DBUG_ASSERT(fixed == TRUE);
295     for (uint i= 0; i < arg_count; i++)
296     {
297       if (args[i]->type() == Item::FIELD_ITEM &&
298           (args[i]->field_type() == MYSQL_TYPE_TIME ||
299            args[i]->field_type() == MYSQL_TYPE_DATETIME))
300         return TRUE;
301     }
302     return FALSE;
303   }
304 
has_datetime_args()305   bool has_datetime_args()
306   {
307     DBUG_ASSERT(fixed == TRUE);
308     for (uint i= 0; i < arg_count; i++)
309     {
310       if (args[i]->type() == Item::FIELD_ITEM &&
311           args[i]->field_type() == MYSQL_TYPE_DATETIME)
312         return TRUE;
313     }
314     return FALSE;
315   }
316 
propagate_equal_fields(THD * thd,const Context & ctx,COND_EQUAL * cond)317   Item* propagate_equal_fields(THD *thd, const Context &ctx, COND_EQUAL *cond)
318   {
319     /*
320       By default only substitution for a field whose two different values
321       are never equal is allowed in the arguments of a function.
322       This is overruled for the direct arguments of comparison functions.
323     */
324     Item_args::propagate_equal_fields(thd, Context_identity(), cond);
325     return this;
326   }
327 
excl_dep_on_table(table_map tab_map)328   bool excl_dep_on_table(table_map tab_map)
329   {
330     if (used_tables() & OUTER_REF_TABLE_BIT)
331       return false;
332     return !(used_tables() & ~tab_map) ||
333            Item_args::excl_dep_on_table(tab_map);
334   }
335 
excl_dep_on_grouping_fields(st_select_lex * sel)336   bool excl_dep_on_grouping_fields(st_select_lex *sel)
337   {
338     return Item_args::excl_dep_on_grouping_fields(sel);
339   }
340 
341   /*
342     We assume the result of any function that has a TIMESTAMP argument to be
343     timezone-dependent, since a TIMESTAMP value in both numeric and string
344     contexts is interpreted according to the current timezone.
345     The only exception is UNIX_TIMESTAMP() which returns the internal
346     representation of a TIMESTAMP argument verbatim, and thus does not depend on
347     the timezone.
348    */
check_valid_arguments_processor(void * bool_arg)349   virtual bool check_valid_arguments_processor(void *bool_arg)
350   {
351     return has_timestamp_args();
352   }
353 
find_function_processor(void * arg)354   virtual bool find_function_processor (void *arg)
355   {
356     return functype() == *(Functype *) arg;
357   }
358 
no_rows_in_result()359   void no_rows_in_result()
360   {
361     for (uint i= 0; i < arg_count; i++)
362     {
363       args[i]->no_rows_in_result();
364     }
365   }
restore_to_before_no_rows_in_result()366   void restore_to_before_no_rows_in_result()
367   {
368     for (uint i= 0; i < arg_count; i++)
369     {
370       args[i]->no_rows_in_result();
371     }
372   }
373   void convert_const_compared_to_int_field(THD *thd);
374   /**
375     Prepare arguments and setup a comparator.
376     Used in Item_func_xxx with two arguments and a comparator,
377     e.g. Item_bool_func2 and Item_func_nullif.
378     args[0] or args[1] can be modified:
379     - converted to character set and collation of the operation
380     - or replaced to an Item_int_with_ref
381   */
382   bool setup_args_and_comparator(THD *thd, Arg_comparator *cmp);
383 };
384 
385 
386 class Item_real_func :public Item_func
387 {
388 public:
Item_real_func(THD * thd)389   Item_real_func(THD *thd): Item_func(thd) { collation.set_numeric(); }
Item_real_func(THD * thd,Item * a)390   Item_real_func(THD *thd, Item *a): Item_func(thd, a)
391   { collation.set_numeric(); }
Item_real_func(THD * thd,Item * a,Item * b)392   Item_real_func(THD *thd, Item *a, Item *b): Item_func(thd, a, b)
393   { collation.set_numeric(); }
Item_real_func(THD * thd,List<Item> & list)394   Item_real_func(THD *thd, List<Item> &list): Item_func(thd, list)
395   { collation.set_numeric(); }
396   String *val_str(String*str);
397   my_decimal *val_decimal(my_decimal *decimal_value);
val_int()398   longlong val_int()
399   {
400     DBUG_ASSERT(fixed == 1);
401     return Converter_double_to_longlong(val_real(), unsigned_flag).result();
402   }
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)403   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
404   { return get_date_from_real(ltime, fuzzydate); }
type_handler()405   const Type_handler *type_handler() const { return &type_handler_double; }
fix_length_and_dec()406   bool fix_length_and_dec()
407   {
408     decimals= NOT_FIXED_DEC;
409     max_length= float_length(decimals);
410     return FALSE;
411   }
412 };
413 
414 
415 /**
416   Functions whose returned field type is determined at fix_fields() time.
417 */
418 class Item_hybrid_func: public Item_func,
419                         public Type_handler_hybrid_field_type,
420                         public Type_geometry_attributes
421 {
422 protected:
423   bool fix_attributes(Item **item, uint nitems);
424 public:
Item_hybrid_func(THD * thd)425   Item_hybrid_func(THD *thd): Item_func(thd) { }
Item_hybrid_func(THD * thd,Item * a)426   Item_hybrid_func(THD *thd, Item *a):  Item_func(thd, a) { }
Item_hybrid_func(THD * thd,Item * a,Item * b)427   Item_hybrid_func(THD *thd, Item *a, Item *b): Item_func(thd, a, b) { }
Item_hybrid_func(THD * thd,Item * a,Item * b,Item * c)428   Item_hybrid_func(THD *thd, Item *a, Item *b, Item *c):
429     Item_func(thd, a, b, c) { }
Item_hybrid_func(THD * thd,List<Item> & list)430   Item_hybrid_func(THD *thd, List<Item> &list): Item_func(thd, list) { }
Item_hybrid_func(THD * thd,Item_hybrid_func * item)431   Item_hybrid_func(THD *thd, Item_hybrid_func *item)
432     :Item_func(thd, item), Type_handler_hybrid_field_type(item) { }
type_handler()433   const Type_handler *type_handler() const
434   { return Type_handler_hybrid_field_type::type_handler(); }
get_geometry_type()435   Field::geometry_type get_geometry_type() const
436   { return Type_geometry_attributes::get_geometry_type(); };
set_geometry_type(uint type)437   void set_geometry_type(uint type)
438   {
439     Type_geometry_attributes::set_geometry_type(type);
440   }
441 };
442 
443 
444 /**
445   Functions that at fix_fields() time determine the returned field type,
446   trying to preserve the exact data type of the arguments.
447 
448   The descendants have to implement "native" value methods,
449   i.e. str_op(), date_op(), int_op(), real_op(), decimal_op().
450   fix_fields() chooses which of the above value methods will be
451   used during execution time, according to the returned field type.
452 
453   For example, if fix_fields() determines that the returned value type
454   is MYSQL_TYPE_LONG, then:
455   - int_op() is chosen as the execution time native method.
456   - val_int() returns the result of int_op() as is.
457   - all other methods, i.e. val_real(), val_decimal(), val_str(), get_date(),
458     call int_op() first, then convert the result to the requested data type.
459 */
460 class Item_func_hybrid_field_type: public Item_hybrid_func
461 {
462   /*
463     Helper methods to make sure that the result of
464     decimal_op(), str_op() and date_op() is properly synched with null_value.
465   */
date_op_with_null_check(MYSQL_TIME * ltime)466   bool date_op_with_null_check(MYSQL_TIME *ltime)
467   {
468      bool rc= date_op(ltime, 0);
469      DBUG_ASSERT(!rc ^ null_value);
470      return rc;
471   }
time_op_with_null_check(MYSQL_TIME * ltime)472   bool time_op_with_null_check(MYSQL_TIME *ltime)
473   {
474      bool rc= time_op(ltime);
475      DBUG_ASSERT(!rc ^ null_value);
476      DBUG_ASSERT(rc || ltime->time_type == MYSQL_TIMESTAMP_TIME);
477      return rc;
478   }
str_op_with_null_check(String * str)479   String *str_op_with_null_check(String *str)
480   {
481     String *res= str_op(str);
482     DBUG_ASSERT((res != NULL) ^ null_value);
483     return res;
484   }
decimal_op_with_null_check(my_decimal * decimal_buffer)485   my_decimal *decimal_op_with_null_check(my_decimal *decimal_buffer)
486   {
487     my_decimal *res= decimal_op(decimal_buffer);
488     DBUG_ASSERT((res != NULL) ^ null_value);
489     return res;
490   }
make_zero_mysql_time(MYSQL_TIME * ltime,ulonglong fuzzydate)491   bool make_zero_mysql_time(MYSQL_TIME *ltime, ulonglong fuzzydate)
492   {
493     bzero(ltime, sizeof(*ltime));
494     return null_value|= !(fuzzydate & TIME_FUZZY_DATES);
495   }
496 
497 public:
498   // Value methods that involve no conversion
val_str_from_str_op(String * str)499   String *val_str_from_str_op(String *str)
500   {
501     return str_op_with_null_check(&str_value);
502   }
val_decimal_from_decimal_op(my_decimal * dec)503   my_decimal *val_decimal_from_decimal_op(my_decimal *dec)
504   {
505     return decimal_op_with_null_check(dec);
506   }
val_int_from_int_op()507   longlong val_int_from_int_op()
508   {
509     return int_op();
510   }
val_real_from_real_op()511   double val_real_from_real_op()
512   {
513     return real_op();
514   }
515 
516   // Value methods that involve conversion
517   String *val_str_from_decimal_op(String *str);
518   String *val_str_from_real_op(String *str);
519   String *val_str_from_int_op(String *str);
520   String *val_str_from_date_op(String *str);
521   String *val_str_from_time_op(String *str);
522 
523   my_decimal *val_decimal_from_str_op(my_decimal *dec);
524   my_decimal *val_decimal_from_real_op(my_decimal *dec);
525   my_decimal *val_decimal_from_int_op(my_decimal *dec);
526   my_decimal *val_decimal_from_date_op(my_decimal *dec);
527   my_decimal *val_decimal_from_time_op(my_decimal *dec);
528 
529   longlong val_int_from_str_op();
530   longlong val_int_from_real_op();
531   longlong val_int_from_decimal_op();
532   longlong val_int_from_date_op();
533   longlong val_int_from_time_op();
534 
535   double val_real_from_str_op();
536   double val_real_from_decimal_op();
537   double val_real_from_date_op();
538   double val_real_from_time_op();
539   double val_real_from_int_op();
540 
541   bool get_date_from_str_op(MYSQL_TIME *ltime, ulonglong fuzzydate);
542   bool get_date_from_real_op(MYSQL_TIME *ltime, ulonglong fuzzydate);
543   bool get_date_from_decimal_op(MYSQL_TIME *ltime, ulonglong fuzzydate);
544   bool get_date_from_int_op(MYSQL_TIME *ltime, ulonglong fuzzydate);
545 
546 public:
Item_func_hybrid_field_type(THD * thd)547   Item_func_hybrid_field_type(THD *thd):
548     Item_hybrid_func(thd)
549   { collation.set_numeric(); }
Item_func_hybrid_field_type(THD * thd,Item * a)550   Item_func_hybrid_field_type(THD *thd, Item *a):
551     Item_hybrid_func(thd, a)
552   { collation.set_numeric(); }
Item_func_hybrid_field_type(THD * thd,Item * a,Item * b)553   Item_func_hybrid_field_type(THD *thd, Item *a, Item *b):
554     Item_hybrid_func(thd, a, b)
555   { collation.set_numeric(); }
Item_func_hybrid_field_type(THD * thd,Item * a,Item * b,Item * c)556   Item_func_hybrid_field_type(THD *thd, Item *a, Item *b, Item *c):
557     Item_hybrid_func(thd, a, b, c)
558   { collation.set_numeric(); }
Item_func_hybrid_field_type(THD * thd,List<Item> & list)559   Item_func_hybrid_field_type(THD *thd, List<Item> &list):
560     Item_hybrid_func(thd, list)
561   { collation.set_numeric(); }
562 
val_real()563   double val_real()
564   {
565     DBUG_ASSERT(fixed);
566     return Item_func_hybrid_field_type::type_handler()->
567            Item_func_hybrid_field_type_val_real(this);
568   }
val_int()569   longlong val_int()
570   {
571     DBUG_ASSERT(fixed);
572     return Item_func_hybrid_field_type::type_handler()->
573            Item_func_hybrid_field_type_val_int(this);
574   }
val_decimal(my_decimal * dec)575   my_decimal *val_decimal(my_decimal *dec)
576   {
577     DBUG_ASSERT(fixed);
578     return Item_func_hybrid_field_type::type_handler()->
579            Item_func_hybrid_field_type_val_decimal(this, dec);
580   }
val_str(String * str)581   String *val_str(String*str)
582   {
583     DBUG_ASSERT(fixed);
584     String *res= Item_func_hybrid_field_type::type_handler()->
585                  Item_func_hybrid_field_type_val_str(this, str);
586     DBUG_ASSERT(null_value == (res == NULL));
587     return res;
588   }
get_date(MYSQL_TIME * res,ulonglong fuzzy_date)589   bool get_date(MYSQL_TIME *res, ulonglong fuzzy_date)
590   {
591     DBUG_ASSERT(fixed);
592     return Item_func_hybrid_field_type::type_handler()->
593            Item_func_hybrid_field_type_get_date(this, res, fuzzy_date);
594   }
595 
596   /**
597      @brief Performs the operation that this functions implements when the
598      result type is INT.
599 
600      @return The result of the operation.
601   */
602   virtual longlong int_op()= 0;
603 
604   /**
605      @brief Performs the operation that this functions implements when the
606      result type is REAL.
607 
608      @return The result of the operation.
609   */
610   virtual double real_op()= 0;
611 
612   /**
613      @brief Performs the operation that this functions implements when the
614      result type is DECIMAL.
615 
616      @param A pointer where the DECIMAL value will be allocated.
617      @return
618        - 0 If the result is NULL
619        - The same pointer it was given, with the area initialized to the
620          result of the operation.
621   */
622   virtual my_decimal *decimal_op(my_decimal *)= 0;
623 
624   /**
625      @brief Performs the operation that this functions implements when the
626      result type is a string type.
627 
628      @return The result of the operation.
629   */
630   virtual String *str_op(String *)= 0;
631 
632   /**
633      @brief Performs the operation that this functions implements when
634      field type is DATETIME or DATE.
635      @return The result of the operation.
636   */
637   virtual bool date_op(MYSQL_TIME *res, ulonglong fuzzy_date)= 0;
638 
639   /**
640      @brief Performs the operation that this functions implements when
641      field type is TIME.
642      @return The result of the operation.
643   */
644   virtual bool time_op(MYSQL_TIME *res)= 0;
645 
646 };
647 
648 
649 /*
650   This class resembles SQL standard CASE-alike expressions:
651   CASE and its abbreviations COALESCE, NULLIF, IFNULL, IF.
652 
653   <case expression> ::=   <case abbreviation>
654                         | <case specification>
655 */
656 class Item_func_case_expression: public Item_func_hybrid_field_type
657 {
658 public:
Item_func_case_expression(THD * thd)659   Item_func_case_expression(THD *thd)
660    :Item_func_hybrid_field_type(thd)
661   { }
Item_func_case_expression(THD * thd,Item * a)662   Item_func_case_expression(THD *thd, Item *a)
663    :Item_func_hybrid_field_type(thd, a)
664   { }
Item_func_case_expression(THD * thd,Item * a,Item * b)665   Item_func_case_expression(THD *thd, Item *a, Item *b)
666    :Item_func_hybrid_field_type(thd, a, b)
667   { }
Item_func_case_expression(THD * thd,Item * a,Item * b,Item * c)668   Item_func_case_expression(THD *thd, Item *a, Item *b, Item *c)
669    :Item_func_hybrid_field_type(thd, a, b, c)
670   { }
Item_func_case_expression(THD * thd,List<Item> & list)671   Item_func_case_expression(THD *thd, List<Item> &list):
672     Item_func_hybrid_field_type(thd, list)
673   { }
674 };
675 
676 
677 class Item_func_numhybrid: public Item_func_hybrid_field_type
678 {
679 protected:
680 
fix_decimals()681   inline void fix_decimals()
682   {
683     DBUG_ASSERT(result_type() == DECIMAL_RESULT);
684     if (decimals == NOT_FIXED_DEC)
685       set_if_smaller(decimals, max_length - 1);
686   }
687 
688 public:
Item_func_numhybrid(THD * thd)689   Item_func_numhybrid(THD *thd): Item_func_hybrid_field_type(thd)
690   { }
Item_func_numhybrid(THD * thd,Item * a)691   Item_func_numhybrid(THD *thd, Item *a): Item_func_hybrid_field_type(thd, a)
692   { }
Item_func_numhybrid(THD * thd,Item * a,Item * b)693   Item_func_numhybrid(THD *thd, Item *a, Item *b):
694     Item_func_hybrid_field_type(thd, a, b)
695   { }
Item_func_numhybrid(THD * thd,Item * a,Item * b,Item * c)696   Item_func_numhybrid(THD *thd, Item *a, Item *b, Item *c):
697     Item_func_hybrid_field_type(thd, a, b, c)
698   { }
Item_func_numhybrid(THD * thd,List<Item> & list)699   Item_func_numhybrid(THD *thd, List<Item> &list):
700     Item_func_hybrid_field_type(thd, list)
701   { }
str_op(String * str)702   String *str_op(String *str) { DBUG_ASSERT(0); return 0; }
date_op(MYSQL_TIME * ltime,ulonglong fuzzydate)703   bool date_op(MYSQL_TIME *ltime, ulonglong fuzzydate)
704   {
705     DBUG_ASSERT(0);
706     return true;
707   }
time_op(MYSQL_TIME * ltime)708   bool time_op(MYSQL_TIME *ltime)
709   {
710     DBUG_ASSERT(0);
711     return true;
712   }
713 };
714 
715 
716 /* function where type of result detected by first argument */
717 class Item_func_num1: public Item_func_numhybrid
718 {
719 public:
Item_func_num1(THD * thd,Item * a)720   Item_func_num1(THD *thd, Item *a): Item_func_numhybrid(thd, a) {}
Item_func_num1(THD * thd,Item * a,Item * b)721   Item_func_num1(THD *thd, Item *a, Item *b): Item_func_numhybrid(thd, a, b) {}
check_partition_func_processor(void * int_arg)722   bool check_partition_func_processor(void *int_arg) { return FALSE; }
check_vcol_func_processor(void * arg)723   bool check_vcol_func_processor(void *arg) { return FALSE; }
724 };
725 
726 
727 /* Base class for operations like '+', '-', '*' */
728 class Item_num_op :public Item_func_numhybrid
729 {
730  public:
Item_num_op(THD * thd,Item * a,Item * b)731   Item_num_op(THD *thd, Item *a, Item *b): Item_func_numhybrid(thd, a, b) {}
732   virtual void result_precision()= 0;
733 
print(String * str,enum_query_type query_type)734   virtual inline void print(String *str, enum_query_type query_type)
735   {
736     print_op(str, query_type);
737   }
738   bool fix_type_handler(const Type_aggregator *aggregator);
fix_length_and_dec_double()739   void fix_length_and_dec_double()
740   {
741     count_real_length(args, arg_count);
742     max_length= float_length(decimals);
743   }
fix_length_and_dec_decimal()744   void fix_length_and_dec_decimal()
745   {
746     unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
747     result_precision();
748     fix_decimals();
749   }
fix_length_and_dec_int()750   void fix_length_and_dec_int()
751   {
752     unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
753     result_precision();
754     decimals= 0;
755     set_handler(type_handler_long_or_longlong());
756   }
fix_length_and_dec_temporal(bool downcast_decimal_to_int)757   void fix_length_and_dec_temporal(bool downcast_decimal_to_int)
758   {
759     set_handler(&type_handler_newdecimal);
760     fix_length_and_dec_decimal();
761     if (decimals == 0 && downcast_decimal_to_int)
762       set_handler(type_handler_long_or_longlong());
763   }
need_parentheses_in_default()764   bool need_parentheses_in_default() { return true; }
765 };
766 
767 
768 class Item_int_func :public Item_func
769 {
770 public:
771   /*
772     QQ: shouldn't 20 characters be enough:
773     Max unsigned =  18,446,744,073,709,551,615 = 20 digits, 20 characters
774     Max signed   =   9,223,372,036,854,775,807 = 19 digits, 19 characters
775     Min signed   =  -9,223,372,036,854,775,808 = 19 digits, 20 characters
776   */
Item_int_func(THD * thd)777   Item_int_func(THD *thd): Item_func(thd)
778   { collation.set_numeric(); fix_char_length(21); }
Item_int_func(THD * thd,Item * a)779   Item_int_func(THD *thd, Item *a): Item_func(thd, a)
780   { collation.set_numeric(); fix_char_length(21); }
Item_int_func(THD * thd,Item * a,Item * b)781   Item_int_func(THD *thd, Item *a, Item *b): Item_func(thd, a, b)
782   { collation.set_numeric(); fix_char_length(21); }
Item_int_func(THD * thd,Item * a,Item * b,Item * c)783   Item_int_func(THD *thd, Item *a, Item *b, Item *c): Item_func(thd, a, b, c)
784   { collation.set_numeric(); fix_char_length(21); }
Item_int_func(THD * thd,Item * a,Item * b,Item * c,Item * d)785   Item_int_func(THD *thd, Item *a, Item *b, Item *c, Item *d):
786     Item_func(thd, a, b, c, d)
787   { collation.set_numeric(); fix_char_length(21); }
Item_int_func(THD * thd,List<Item> & list)788   Item_int_func(THD *thd, List<Item> &list): Item_func(thd, list)
789   { collation.set_numeric(); fix_char_length(21); }
Item_int_func(THD * thd,Item_int_func * item)790   Item_int_func(THD *thd, Item_int_func *item) :Item_func(thd, item)
791   { collation.set_numeric(); }
792   double val_real();
793   String *val_str(String*str);
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)794   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
795   { return get_date_from_int(ltime, fuzzydate); }
796   const Type_handler *type_handler() const= 0;
fix_length_and_dec()797   bool fix_length_and_dec() { return FALSE; }
798 };
799 
800 
801 class Item_long_func: public Item_int_func
802 {
803 public:
Item_long_func(THD * thd)804   Item_long_func(THD *thd): Item_int_func(thd) { }
Item_long_func(THD * thd,Item * a)805   Item_long_func(THD *thd, Item *a): Item_int_func(thd, a) {}
Item_long_func(THD * thd,Item * a,Item * b)806   Item_long_func(THD *thd, Item *a, Item *b): Item_int_func(thd, a, b) {}
Item_long_func(THD * thd,Item * a,Item * b,Item * c)807   Item_long_func(THD *thd, Item *a, Item *b, Item *c): Item_int_func(thd, a, b, c) {}
Item_long_func(THD * thd,List<Item> & list)808   Item_long_func(THD *thd, List<Item> &list): Item_int_func(thd, list) { }
Item_long_func(THD * thd,Item_long_func * item)809   Item_long_func(THD *thd, Item_long_func *item) :Item_int_func(thd, item) {}
type_handler()810   const Type_handler *type_handler() const { return &type_handler_long; }
fix_length_and_dec()811   bool fix_length_and_dec() { max_length= 11; return FALSE; }
812 };
813 
814 
815 class Item_longlong_func: public Item_int_func
816 {
817 public:
Item_longlong_func(THD * thd)818   Item_longlong_func(THD *thd): Item_int_func(thd) { }
Item_longlong_func(THD * thd,Item * a)819   Item_longlong_func(THD *thd, Item *a): Item_int_func(thd, a) {}
Item_longlong_func(THD * thd,Item * a,Item * b)820   Item_longlong_func(THD *thd, Item *a, Item *b): Item_int_func(thd, a, b) {}
Item_longlong_func(THD * thd,Item * a,Item * b,Item * c)821   Item_longlong_func(THD *thd, Item *a, Item *b, Item *c): Item_int_func(thd, a, b, c) {}
Item_longlong_func(THD * thd,Item * a,Item * b,Item * c,Item * d)822   Item_longlong_func(THD *thd, Item *a, Item *b, Item *c, Item *d):
823     Item_int_func(thd, a, b, c, d) {}
Item_longlong_func(THD * thd,List<Item> & list)824   Item_longlong_func(THD *thd, List<Item> &list): Item_int_func(thd, list) { }
Item_longlong_func(THD * thd,Item_longlong_func * item)825   Item_longlong_func(THD *thd, Item_longlong_func *item) :Item_int_func(thd, item) {}
type_handler()826   const Type_handler *type_handler() const { return &type_handler_longlong; }
827 };
828 
829 
830 class Cursor_ref
831 {
832 protected:
833   LEX_CSTRING m_cursor_name;
834   uint m_cursor_offset;
835   class sp_cursor *get_open_cursor_or_error();
Cursor_ref(const LEX_CSTRING * name,uint offset)836   Cursor_ref(const LEX_CSTRING *name, uint offset)
837    :m_cursor_name(*name), m_cursor_offset(offset)
838   { }
839   void print_func(String *str, const char *func_name);
840 };
841 
842 
843 
844 class Item_func_cursor_rowcount: public Item_longlong_func,
845                                  public Cursor_ref
846 {
847 public:
Item_func_cursor_rowcount(THD * thd,const LEX_CSTRING * name,uint offset)848   Item_func_cursor_rowcount(THD *thd, const LEX_CSTRING *name, uint offset)
849    :Item_longlong_func(thd), Cursor_ref(name, offset) { maybe_null= true; }
func_name()850   const char *func_name() const { return "%ROWCOUNT"; }
851   longlong val_int();
check_vcol_func_processor(void * arg)852   bool check_vcol_func_processor(void *arg)
853   {
854     return mark_unsupported_function(func_name(), arg, VCOL_SESSION_FUNC);
855   }
print(String * str,enum_query_type query_type)856   void print(String *str, enum_query_type query_type)
857   {
858     return Cursor_ref::print_func(str, func_name());
859   }
get_copy(THD * thd)860   Item *get_copy(THD *thd)
861   { return get_item_copy<Item_func_cursor_rowcount>(thd, this); }
862 };
863 
864 
865 
866 class Item_func_connection_id :public Item_long_func
867 {
868   longlong value;
869 
870 public:
Item_func_connection_id(THD * thd)871   Item_func_connection_id(THD *thd): Item_long_func(thd) { unsigned_flag=1; }
func_name()872   const char *func_name() const { return "connection_id"; }
873   bool fix_length_and_dec();
874   bool fix_fields(THD *thd, Item **ref);
val_int()875   longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
check_vcol_func_processor(void * arg)876   bool check_vcol_func_processor(void *arg)
877   {
878     return mark_unsupported_function(func_name(), "()", arg, VCOL_SESSION_FUNC);
879   }
get_copy(THD * thd)880   Item *get_copy(THD *thd)
881   { return get_item_copy<Item_func_connection_id>(thd, this); }
882 };
883 
884 
885 class Item_func_signed :public Item_int_func
886 {
887 public:
Item_func_signed(THD * thd,Item * a)888   Item_func_signed(THD *thd, Item *a): Item_int_func(thd, a)
889   {
890     unsigned_flag= 0;
891   }
func_name()892   const char *func_name() const { return "cast_as_signed"; }
type_handler()893   const Type_handler *type_handler() const
894   { return type_handler_long_or_longlong(); }
val_int()895   longlong val_int()
896   {
897     longlong value= args[0]->val_int_signed_typecast();
898     null_value= args[0]->null_value;
899     return value;
900   }
fix_length_and_dec_double()901   void fix_length_and_dec_double()
902   {
903     fix_char_length(MAX_BIGINT_WIDTH);
904   }
fix_length_and_dec_generic()905   void fix_length_and_dec_generic()
906   {
907     uint32 char_length= MY_MIN(args[0]->max_char_length(),
908                                MY_INT64_NUM_DECIMAL_DIGITS);
909     /*
910       args[0]->max_char_length() can return 0.
911       Reserve max_length to fit at least one character for one digit,
912       plus one character for the sign (if signed).
913     */
914     set_if_bigger(char_length, 1U + (unsigned_flag ? 0 : 1));
915     fix_char_length(char_length);
916   }
fix_length_and_dec_string()917   void fix_length_and_dec_string()
918   {
919     /*
920       For strings, use decimal_int_part() instead of max_char_length().
921       This is important for Item_hex_hybrid:
922         SELECT CAST(0x1FFFFFFFF AS SIGNED);
923       Length is 5, decimal_int_part() is 13.
924     */
925     uint32 char_length= MY_MIN(args[0]->decimal_int_part(),
926                                MY_INT64_NUM_DECIMAL_DIGITS);
927     set_if_bigger(char_length, 1U + (unsigned_flag ? 0 : 1));
928     fix_char_length(char_length);
929   }
fix_length_and_dec()930   bool fix_length_and_dec()
931   {
932     return args[0]->type_handler()->Item_func_signed_fix_length_and_dec(this);
933   }
934   virtual void print(String *str, enum_query_type query_type);
decimal_precision()935   uint decimal_precision() const { return args[0]->decimal_precision(); }
need_parentheses_in_default()936   bool need_parentheses_in_default() { return true; }
get_copy(THD * thd)937   Item *get_copy(THD *thd)
938   { return get_item_copy<Item_func_signed>(thd, this); }
939 };
940 
941 
942 class Item_func_unsigned :public Item_func_signed
943 {
944 public:
Item_func_unsigned(THD * thd,Item * a)945   Item_func_unsigned(THD *thd, Item *a): Item_func_signed(thd, a)
946   {
947     unsigned_flag= 1;
948   }
func_name()949   const char *func_name() const { return "cast_as_unsigned"; }
type_handler()950   const Type_handler *type_handler() const
951   {
952     if (max_char_length() <= MY_INT32_NUM_DECIMAL_DIGITS - 1)
953       return &type_handler_long;
954     return &type_handler_longlong;
955   }
val_int()956   longlong val_int()
957   {
958     longlong value= args[0]->val_int_unsigned_typecast();
959     null_value= args[0]->null_value;
960     return value;
961   }
fix_length_and_dec()962   bool fix_length_and_dec()
963   {
964     return args[0]->type_handler()->Item_func_unsigned_fix_length_and_dec(this);
965   }
decimal_precision()966   uint decimal_precision() const { return max_length; }
967   virtual void print(String *str, enum_query_type query_type);
get_copy(THD * thd)968   Item *get_copy(THD *thd)
969   { return get_item_copy<Item_func_unsigned>(thd, this); }
970 };
971 
972 
973 class Item_decimal_typecast :public Item_func
974 {
975   my_decimal decimal_value;
976 public:
Item_decimal_typecast(THD * thd,Item * a,uint len,uint dec)977   Item_decimal_typecast(THD *thd, Item *a, uint len, uint dec)
978    :Item_func(thd, a)
979   {
980     decimals= (uint8) dec;
981     collation.set_numeric();
982     fix_char_length(my_decimal_precision_to_length_no_truncation(len, dec,
983                                                                  unsigned_flag));
984   }
985   String *val_str(String *str);
986   double val_real();
987   longlong val_int();
988   my_decimal *val_decimal(my_decimal*);
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)989   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
990   { return get_date_from_decimal(ltime, fuzzydate); }
type_handler()991   const Type_handler *type_handler() const { return &type_handler_newdecimal; }
fix_length_and_dec_generic()992   void fix_length_and_dec_generic() {}
fix_length_and_dec()993   bool fix_length_and_dec()
994   {
995     return
996       args[0]->type_handler()->Item_decimal_typecast_fix_length_and_dec(this);
997   }
func_name()998   const char *func_name() const { return "decimal_typecast"; }
999   virtual void print(String *str, enum_query_type query_type);
need_parentheses_in_default()1000   bool need_parentheses_in_default() { return true; }
get_copy(THD * thd)1001   Item *get_copy(THD *thd)
1002   { return get_item_copy<Item_decimal_typecast>(thd, this); }
1003 };
1004 
1005 
1006 class Item_real_typecast: public Item_real_func
1007 {
1008 protected:
1009   double val_real_with_truncate(double max_value);
1010 public:
Item_real_typecast(THD * thd,Item * a,uint len,uint dec)1011   Item_real_typecast(THD *thd, Item *a, uint len, uint dec)
1012    :Item_real_func(thd, a)
1013   {
1014     decimals=   (uint8)  dec;
1015     max_length= (uint32) len;
1016   }
need_parentheses_in_default()1017   bool need_parentheses_in_default() { return true; }
1018   void print(String *str, enum_query_type query_type);
fix_length_and_dec_generic()1019   void fix_length_and_dec_generic() { maybe_null= 1; }
1020 };
1021 
1022 
1023 class Item_float_typecast :public Item_real_typecast
1024 {
1025 public:
Item_float_typecast(THD * thd,Item * a)1026   Item_float_typecast(THD *thd, Item *a)
1027    :Item_real_typecast(thd, a, MAX_FLOAT_STR_LENGTH, NOT_FIXED_DEC)
1028   { }
type_handler()1029   const Type_handler *type_handler() const { return &type_handler_float; }
fix_length_and_dec()1030   bool fix_length_and_dec()
1031   {
1032     return
1033       args[0]->type_handler()->Item_float_typecast_fix_length_and_dec(this);
1034   }
func_name()1035   const char *func_name() const { return "float_typecast"; }
val_real()1036   double val_real()
1037   {
1038     return (double) (float) val_real_with_truncate(FLT_MAX);
1039   }
val_str(String * str)1040   String *val_str(String*str)
1041   {
1042     Float nr(Item_float_typecast::val_real());
1043     if (null_value)
1044       return 0;
1045     nr.to_string(str, decimals);
1046     return str;
1047   }
get_copy(THD * thd)1048   Item *get_copy(THD *thd)
1049   { return get_item_copy<Item_float_typecast>(thd, this); }
1050 };
1051 
1052 
1053 class Item_double_typecast :public Item_real_typecast
1054 {
1055 public:
Item_double_typecast(THD * thd,Item * a,uint len,uint dec)1056   Item_double_typecast(THD *thd, Item *a, uint len, uint dec):
1057     Item_real_typecast(thd, a, len, dec)
1058   { }
fix_length_and_dec()1059   bool fix_length_and_dec()
1060   {
1061     return
1062       args[0]->type_handler()->Item_double_typecast_fix_length_and_dec(this);
1063   }
func_name()1064   const char *func_name() const { return "double_typecast"; }
val_real()1065   double val_real() { return val_real_with_truncate(DBL_MAX); }
get_copy(THD * thd)1066   Item *get_copy(THD *thd)
1067   { return get_item_copy<Item_double_typecast>(thd, this); }
1068 };
1069 
1070 
1071 class Item_func_additive_op :public Item_num_op
1072 {
1073 public:
Item_func_additive_op(THD * thd,Item * a,Item * b)1074   Item_func_additive_op(THD *thd, Item *a, Item *b): Item_num_op(thd, a, b) {}
1075   void result_precision();
check_partition_func_processor(void * int_arg)1076   bool check_partition_func_processor(void *int_arg) {return FALSE;}
check_vcol_func_processor(void * arg)1077   bool check_vcol_func_processor(void *arg) { return FALSE;}
1078 };
1079 
1080 
1081 class Item_func_plus :public Item_func_additive_op
1082 {
1083 public:
Item_func_plus(THD * thd,Item * a,Item * b)1084   Item_func_plus(THD *thd, Item *a, Item *b):
1085     Item_func_additive_op(thd, a, b) {}
func_name()1086   const char *func_name() const { return "+"; }
precedence()1087   enum precedence precedence() const { return ADD_PRECEDENCE; }
1088   bool fix_length_and_dec();
1089   longlong int_op();
1090   double real_op();
1091   my_decimal *decimal_op(my_decimal *);
get_copy(THD * thd)1092   Item *get_copy(THD *thd)
1093   { return get_item_copy<Item_func_plus>(thd, this); }
1094 };
1095 
1096 class Item_func_minus :public Item_func_additive_op
1097 {
1098   bool m_depends_on_sql_mode_no_unsigned_subtraction;
1099 public:
Item_func_minus(THD * thd,Item * a,Item * b)1100   Item_func_minus(THD *thd, Item *a, Item *b):
1101     Item_func_additive_op(thd, a, b),
1102     m_depends_on_sql_mode_no_unsigned_subtraction(false)
1103   { }
func_name()1104   const char *func_name() const { return "-"; }
precedence()1105   enum precedence precedence() const { return ADD_PRECEDENCE; }
1106   Sql_mode_dependency value_depends_on_sql_mode() const;
1107   longlong int_op();
1108   double real_op();
1109   my_decimal *decimal_op(my_decimal *);
1110   bool fix_length_and_dec();
1111   void fix_unsigned_flag();
fix_length_and_dec_double()1112   void fix_length_and_dec_double()
1113   {
1114     Item_func_additive_op::fix_length_and_dec_double();
1115     fix_unsigned_flag();
1116   }
fix_length_and_dec_decimal()1117   void fix_length_and_dec_decimal()
1118   {
1119     Item_func_additive_op::fix_length_and_dec_decimal();
1120     fix_unsigned_flag();
1121   }
fix_length_and_dec_int()1122   void fix_length_and_dec_int()
1123   {
1124     Item_func_additive_op::fix_length_and_dec_int();
1125     fix_unsigned_flag();
1126   }
get_copy(THD * thd)1127   Item *get_copy(THD *thd)
1128   { return get_item_copy<Item_func_minus>(thd, this); }
1129 };
1130 
1131 
1132 class Item_func_mul :public Item_num_op
1133 {
1134 public:
Item_func_mul(THD * thd,Item * a,Item * b)1135   Item_func_mul(THD *thd, Item *a, Item *b):
1136     Item_num_op(thd, a, b) {}
func_name()1137   const char *func_name() const { return "*"; }
precedence()1138   enum precedence precedence() const { return MUL_PRECEDENCE; }
1139   longlong int_op();
1140   double real_op();
1141   my_decimal *decimal_op(my_decimal *);
1142   void result_precision();
1143   bool fix_length_and_dec();
check_partition_func_processor(void * int_arg)1144   bool check_partition_func_processor(void *int_arg) {return FALSE;}
check_vcol_func_processor(void * arg)1145   bool check_vcol_func_processor(void *arg) { return FALSE;}
get_copy(THD * thd)1146   Item *get_copy(THD *thd)
1147   { return get_item_copy<Item_func_mul>(thd, this); }
1148 };
1149 
1150 
1151 class Item_func_div :public Item_num_op
1152 {
1153 public:
1154   uint prec_increment;
Item_func_div(THD * thd,Item * a,Item * b)1155   Item_func_div(THD *thd, Item *a, Item *b): Item_num_op(thd, a, b) {}
int_op()1156   longlong int_op() { DBUG_ASSERT(0); return 0; }
1157   double real_op();
1158   my_decimal *decimal_op(my_decimal *);
func_name()1159   const char *func_name() const { return "/"; }
precedence()1160   enum precedence precedence() const { return MUL_PRECEDENCE; }
1161   bool fix_length_and_dec();
1162   void fix_length_and_dec_double();
1163   void fix_length_and_dec_int();
1164   void result_precision();
get_copy(THD * thd)1165   Item *get_copy(THD *thd)
1166   { return get_item_copy<Item_func_div>(thd, this); }
1167 };
1168 
1169 
1170 class Item_func_int_div :public Item_int_func
1171 {
1172 public:
Item_func_int_div(THD * thd,Item * a,Item * b)1173   Item_func_int_div(THD *thd, Item *a, Item *b): Item_int_func(thd, a, b)
1174   {}
1175   longlong val_int();
func_name()1176   const char *func_name() const { return "DIV"; }
precedence()1177   enum precedence precedence() const { return MUL_PRECEDENCE; }
type_handler()1178   const Type_handler *type_handler() const
1179   { return type_handler_long_or_longlong(); }
1180   bool fix_length_and_dec();
print(String * str,enum_query_type query_type)1181   void print(String *str, enum_query_type query_type)
1182   {
1183     print_op(str, query_type);
1184   }
1185 
check_partition_func_processor(void * int_arg)1186   bool check_partition_func_processor(void *int_arg) {return FALSE;}
check_vcol_func_processor(void * arg)1187   bool check_vcol_func_processor(void *arg) { return FALSE;}
need_parentheses_in_default()1188   bool need_parentheses_in_default() { return true; }
get_copy(THD * thd)1189   Item *get_copy(THD *thd)
1190   { return get_item_copy<Item_func_int_div>(thd, this); }
1191 };
1192 
1193 
1194 class Item_func_mod :public Item_num_op
1195 {
1196 public:
Item_func_mod(THD * thd,Item * a,Item * b)1197   Item_func_mod(THD *thd, Item *a, Item *b): Item_num_op(thd, a, b) {}
1198   longlong int_op();
1199   double real_op();
1200   my_decimal *decimal_op(my_decimal *);
func_name()1201   const char *func_name() const { return "MOD"; }
precedence()1202   enum precedence precedence() const { return MUL_PRECEDENCE; }
1203   void result_precision();
1204   bool fix_length_and_dec();
fix_length_and_dec_double()1205   void fix_length_and_dec_double()
1206   {
1207     Item_num_op::fix_length_and_dec_double();
1208     unsigned_flag= args[0]->unsigned_flag;
1209   }
fix_length_and_dec_decimal()1210   void fix_length_and_dec_decimal()
1211   {
1212     result_precision();
1213     fix_decimals();
1214   }
fix_length_and_dec_int()1215   void fix_length_and_dec_int()
1216   {
1217     result_precision();
1218     DBUG_ASSERT(decimals == 0);
1219     set_handler(type_handler_long_or_longlong());
1220   }
check_partition_func_processor(void * int_arg)1221   bool check_partition_func_processor(void *int_arg) {return FALSE;}
check_vcol_func_processor(void * arg)1222   bool check_vcol_func_processor(void *arg) { return FALSE;}
get_copy(THD * thd)1223   Item *get_copy(THD *thd)
1224   { return get_item_copy<Item_func_mod>(thd, this); }
1225 };
1226 
1227 
1228 class Item_func_neg :public Item_func_num1
1229 {
1230 public:
Item_func_neg(THD * thd,Item * a)1231   Item_func_neg(THD *thd, Item *a): Item_func_num1(thd, a) {}
1232   double real_op();
1233   longlong int_op();
1234   my_decimal *decimal_op(my_decimal *);
func_name()1235   const char *func_name() const { return "-"; }
functype()1236   enum Functype functype() const   { return NEG_FUNC; }
precedence()1237   enum precedence precedence() const { return NEG_PRECEDENCE; }
print(String * str,enum_query_type query_type)1238   void print(String *str, enum_query_type query_type)
1239   {
1240     str->append(func_name());
1241     args[0]->print_parenthesised(str, query_type, precedence());
1242   }
1243   void fix_length_and_dec_int();
1244   void fix_length_and_dec_double();
1245   void fix_length_and_dec_decimal();
1246   bool fix_length_and_dec();
decimal_precision()1247   uint decimal_precision() const { return args[0]->decimal_precision(); }
need_parentheses_in_default()1248   bool need_parentheses_in_default() { return true; }
get_copy(THD * thd)1249   Item *get_copy(THD *thd)
1250   { return get_item_copy<Item_func_neg>(thd, this); }
1251 };
1252 
1253 
1254 class Item_func_abs :public Item_func_num1
1255 {
1256 public:
Item_func_abs(THD * thd,Item * a)1257   Item_func_abs(THD *thd, Item *a): Item_func_num1(thd, a) {}
1258   double real_op();
1259   longlong int_op();
1260   my_decimal *decimal_op(my_decimal *);
func_name()1261   const char *func_name() const { return "abs"; }
1262   void fix_length_and_dec_int();
1263   void fix_length_and_dec_double();
1264   void fix_length_and_dec_decimal();
1265   bool fix_length_and_dec();
get_copy(THD * thd)1266   Item *get_copy(THD *thd)
1267   { return get_item_copy<Item_func_abs>(thd, this); }
1268 };
1269 
1270 // A class to handle logarithmic and trigonometric functions
1271 
1272 class Item_dec_func :public Item_real_func
1273 {
check_arguments()1274   bool check_arguments() const
1275   { return check_argument_types_can_return_real(0, arg_count); }
1276  public:
Item_dec_func(THD * thd,Item * a)1277   Item_dec_func(THD *thd, Item *a): Item_real_func(thd, a) {}
Item_dec_func(THD * thd,Item * a,Item * b)1278   Item_dec_func(THD *thd, Item *a, Item *b): Item_real_func(thd, a, b) {}
fix_length_and_dec()1279   bool fix_length_and_dec()
1280   {
1281     decimals=NOT_FIXED_DEC; max_length=float_length(decimals);
1282     maybe_null=1;
1283     return FALSE;
1284   }
1285 };
1286 
1287 class Item_func_exp :public Item_dec_func
1288 {
1289 public:
Item_func_exp(THD * thd,Item * a)1290   Item_func_exp(THD *thd, Item *a): Item_dec_func(thd, a) {}
1291   double val_real();
func_name()1292   const char *func_name() const { return "exp"; }
get_copy(THD * thd)1293   Item *get_copy(THD *thd)
1294   { return get_item_copy<Item_func_exp>(thd, this); }
1295 };
1296 
1297 
1298 class Item_func_ln :public Item_dec_func
1299 {
1300 public:
Item_func_ln(THD * thd,Item * a)1301   Item_func_ln(THD *thd, Item *a): Item_dec_func(thd, a) {}
1302   double val_real();
func_name()1303   const char *func_name() const { return "ln"; }
get_copy(THD * thd)1304   Item *get_copy(THD *thd)
1305   { return get_item_copy<Item_func_ln>(thd, this); }
1306 };
1307 
1308 
1309 class Item_func_log :public Item_dec_func
1310 {
1311 public:
Item_func_log(THD * thd,Item * a)1312   Item_func_log(THD *thd, Item *a): Item_dec_func(thd, a) {}
Item_func_log(THD * thd,Item * a,Item * b)1313   Item_func_log(THD *thd, Item *a, Item *b): Item_dec_func(thd, a, b) {}
1314   double val_real();
func_name()1315   const char *func_name() const { return "log"; }
get_copy(THD * thd)1316   Item *get_copy(THD *thd)
1317   { return get_item_copy<Item_func_log>(thd, this); }
1318 };
1319 
1320 
1321 class Item_func_log2 :public Item_dec_func
1322 {
1323 public:
Item_func_log2(THD * thd,Item * a)1324   Item_func_log2(THD *thd, Item *a): Item_dec_func(thd, a) {}
1325   double val_real();
func_name()1326   const char *func_name() const { return "log2"; }
get_copy(THD * thd)1327   Item *get_copy(THD *thd)
1328   { return get_item_copy<Item_func_log2>(thd, this); }
1329 };
1330 
1331 
1332 class Item_func_log10 :public Item_dec_func
1333 {
1334 public:
Item_func_log10(THD * thd,Item * a)1335   Item_func_log10(THD *thd, Item *a): Item_dec_func(thd, a) {}
1336   double val_real();
func_name()1337   const char *func_name() const { return "log10"; }
get_copy(THD * thd)1338   Item *get_copy(THD *thd)
1339   { return get_item_copy<Item_func_log10>(thd, this); }
1340 };
1341 
1342 
1343 class Item_func_sqrt :public Item_dec_func
1344 {
1345 public:
Item_func_sqrt(THD * thd,Item * a)1346   Item_func_sqrt(THD *thd, Item *a): Item_dec_func(thd, a) {}
1347   double val_real();
func_name()1348   const char *func_name() const { return "sqrt"; }
get_copy(THD * thd)1349   Item *get_copy(THD *thd)
1350   { return get_item_copy<Item_func_sqrt>(thd, this); }
1351 };
1352 
1353 
1354 class Item_func_pow :public Item_dec_func
1355 {
1356 public:
Item_func_pow(THD * thd,Item * a,Item * b)1357   Item_func_pow(THD *thd, Item *a, Item *b): Item_dec_func(thd, a, b) {}
1358   double val_real();
func_name()1359   const char *func_name() const { return "pow"; }
get_copy(THD * thd)1360   Item *get_copy(THD *thd)
1361   { return get_item_copy<Item_func_pow>(thd, this); }
1362 };
1363 
1364 
1365 class Item_func_acos :public Item_dec_func
1366 {
1367 public:
Item_func_acos(THD * thd,Item * a)1368   Item_func_acos(THD *thd, Item *a): Item_dec_func(thd, a) {}
1369   double val_real();
func_name()1370   const char *func_name() const { return "acos"; }
get_copy(THD * thd)1371   Item *get_copy(THD *thd)
1372   { return get_item_copy<Item_func_acos>(thd, this); }
1373 };
1374 
1375 class Item_func_asin :public Item_dec_func
1376 {
1377 public:
Item_func_asin(THD * thd,Item * a)1378   Item_func_asin(THD *thd, Item *a): Item_dec_func(thd, a) {}
1379   double val_real();
func_name()1380   const char *func_name() const { return "asin"; }
get_copy(THD * thd)1381   Item *get_copy(THD *thd)
1382   { return get_item_copy<Item_func_asin>(thd, this); }
1383 };
1384 
1385 class Item_func_atan :public Item_dec_func
1386 {
1387 public:
Item_func_atan(THD * thd,Item * a)1388   Item_func_atan(THD *thd, Item *a): Item_dec_func(thd, a) {}
Item_func_atan(THD * thd,Item * a,Item * b)1389   Item_func_atan(THD *thd, Item *a, Item *b): Item_dec_func(thd, a, b) {}
1390   double val_real();
func_name()1391   const char *func_name() const { return "atan"; }
get_copy(THD * thd)1392   Item *get_copy(THD *thd)
1393   { return get_item_copy<Item_func_atan>(thd, this); }
1394 };
1395 
1396 class Item_func_cos :public Item_dec_func
1397 {
1398 public:
Item_func_cos(THD * thd,Item * a)1399   Item_func_cos(THD *thd, Item *a): Item_dec_func(thd, a) {}
1400   double val_real();
func_name()1401   const char *func_name() const { return "cos"; }
get_copy(THD * thd)1402   Item *get_copy(THD *thd)
1403   { return get_item_copy<Item_func_cos>(thd, this); }
1404 };
1405 
1406 class Item_func_sin :public Item_dec_func
1407 {
1408 public:
Item_func_sin(THD * thd,Item * a)1409   Item_func_sin(THD *thd, Item *a): Item_dec_func(thd, a) {}
1410   double val_real();
func_name()1411   const char *func_name() const { return "sin"; }
get_copy(THD * thd)1412   Item *get_copy(THD *thd)
1413   { return get_item_copy<Item_func_sin>(thd, this); }
1414 };
1415 
1416 class Item_func_tan :public Item_dec_func
1417 {
1418 public:
Item_func_tan(THD * thd,Item * a)1419   Item_func_tan(THD *thd, Item *a): Item_dec_func(thd, a) {}
1420   double val_real();
func_name()1421   const char *func_name() const { return "tan"; }
get_copy(THD * thd)1422   Item *get_copy(THD *thd)
1423   { return get_item_copy<Item_func_tan>(thd, this); }
1424 };
1425 
1426 class Item_func_cot :public Item_dec_func
1427 {
1428 public:
Item_func_cot(THD * thd,Item * a)1429   Item_func_cot(THD *thd, Item *a): Item_dec_func(thd, a) {}
1430   double val_real();
func_name()1431   const char *func_name() const { return "cot"; }
get_copy(THD * thd)1432   Item *get_copy(THD *thd)
1433   { return get_item_copy<Item_func_cot>(thd, this); }
1434 };
1435 
1436 
1437 class Item_func_int_val :public Item_func_num1
1438 {
1439 public:
Item_func_int_val(THD * thd,Item * a)1440   Item_func_int_val(THD *thd, Item *a): Item_func_num1(thd, a) {}
1441   void fix_length_and_dec_double();
1442   void fix_length_and_dec_int_or_decimal();
1443   bool fix_length_and_dec();
1444 };
1445 
1446 
1447 class Item_func_ceiling :public Item_func_int_val
1448 {
1449 public:
Item_func_ceiling(THD * thd,Item * a)1450   Item_func_ceiling(THD *thd, Item *a): Item_func_int_val(thd, a) {}
func_name()1451   const char *func_name() const { return "ceiling"; }
1452   longlong int_op();
1453   double real_op();
1454   my_decimal *decimal_op(my_decimal *);
get_copy(THD * thd)1455   Item *get_copy(THD *thd)
1456   { return get_item_copy<Item_func_ceiling>(thd, this); }
1457 };
1458 
1459 
1460 class Item_func_floor :public Item_func_int_val
1461 {
1462 public:
Item_func_floor(THD * thd,Item * a)1463   Item_func_floor(THD *thd, Item *a): Item_func_int_val(thd, a) {}
func_name()1464   const char *func_name() const { return "floor"; }
1465   longlong int_op();
1466   double real_op();
1467   my_decimal *decimal_op(my_decimal *);
get_copy(THD * thd)1468   Item *get_copy(THD *thd)
1469   { return get_item_copy<Item_func_floor>(thd, this); }
1470 };
1471 
1472 /* This handles round and truncate */
1473 
1474 class Item_func_round :public Item_func_numhybrid
1475 {
1476   bool truncate;
1477   void fix_length_and_dec_decimal(uint decimals_to_set);
1478   void fix_length_and_dec_double(uint decimals_to_set);
1479 public:
Item_func_round(THD * thd,Item * a,Item * b,bool trunc_arg)1480   Item_func_round(THD *thd, Item *a, Item *b, bool trunc_arg)
1481     :Item_func_numhybrid(thd, a, b), truncate(trunc_arg) {}
func_name()1482   const char *func_name() const { return truncate ? "truncate" : "round"; }
1483   double real_op();
1484   longlong int_op();
1485   my_decimal *decimal_op(my_decimal *);
1486   void fix_arg_decimal();
1487   void fix_arg_int();
1488   void fix_arg_double();
fix_length_and_dec()1489   bool fix_length_and_dec()
1490   {
1491     return args[0]->type_handler()->Item_func_round_fix_length_and_dec(this);
1492   }
get_copy(THD * thd)1493   Item *get_copy(THD *thd)
1494   { return get_item_copy<Item_func_round>(thd, this); }
1495 };
1496 
1497 
1498 class Item_func_rand :public Item_real_func
1499 {
1500   struct my_rnd_struct *rand;
1501   bool first_eval; // TRUE if val_real() is called 1st time
check_arguments()1502   bool check_arguments() const
1503   { return check_argument_types_can_return_int(0, arg_count); }
1504 public:
Item_func_rand(THD * thd,Item * a)1505   Item_func_rand(THD *thd, Item *a):
1506     Item_real_func(thd, a), rand(0), first_eval(TRUE) {}
Item_func_rand(THD * thd)1507   Item_func_rand(THD *thd): Item_real_func(thd) {}
1508   double val_real();
func_name()1509   const char *func_name() const { return "rand"; }
const_item()1510   bool const_item() const { return 0; }
1511   void update_used_tables();
1512   bool fix_fields(THD *thd, Item **ref);
cleanup()1513   void cleanup() { first_eval= TRUE; Item_real_func::cleanup(); }
check_vcol_func_processor(void * arg)1514   bool check_vcol_func_processor(void *arg)
1515   {
1516     return mark_unsupported_function(func_name(), "()", arg, VCOL_NON_DETERMINISTIC);
1517   }
get_copy(THD * thd)1518   Item *get_copy(THD *thd)
1519   { return get_item_copy<Item_func_rand>(thd, this); }
1520 private:
1521   void seed_random (Item * val);
1522 };
1523 
1524 
1525 class Item_func_sign :public Item_long_func
1526 {
check_arguments()1527   bool check_arguments() const
1528   { return args[0]->check_type_can_return_real(func_name()); }
1529 public:
Item_func_sign(THD * thd,Item * a)1530   Item_func_sign(THD *thd, Item *a): Item_long_func(thd, a) {}
func_name()1531   const char *func_name() const { return "sign"; }
decimal_precision()1532   uint decimal_precision() const { return 1; }
fix_length_and_dec()1533   bool fix_length_and_dec() { fix_char_length(2); return FALSE; }
1534   longlong val_int();
get_copy(THD * thd)1535   Item *get_copy(THD *thd)
1536   { return get_item_copy<Item_func_sign>(thd, this); }
1537 };
1538 
1539 
1540 class Item_func_units :public Item_real_func
1541 {
1542   char *name;
1543   double mul,add;
check_arguments()1544   bool check_arguments() const
1545   { return check_argument_types_can_return_real(0, arg_count); }
1546 public:
Item_func_units(THD * thd,char * name_arg,Item * a,double mul_arg,double add_arg)1547   Item_func_units(THD *thd, char *name_arg, Item *a, double mul_arg,
1548                   double add_arg):
1549     Item_real_func(thd, a), name(name_arg), mul(mul_arg), add(add_arg) {}
1550   double val_real();
func_name()1551   const char *func_name() const { return name; }
fix_length_and_dec()1552   bool fix_length_and_dec()
1553   {
1554     decimals= NOT_FIXED_DEC;
1555     max_length= float_length(decimals);
1556     return FALSE;
1557   }
get_copy(THD * thd)1558   Item *get_copy(THD *thd)
1559   { return get_item_copy<Item_func_units>(thd, this); }
1560 };
1561 
1562 
1563 /**
1564   Item_func_min_max does not derive from Item_func_hybrid_field_type
1565   because the way how its methods val_xxx() and get_date() work depend
1566   not only by its arguments, but also on the context in which
1567   LEAST() and GREATEST() appear.
1568   For example, using Item_func_min_max in a CAST like this:
1569     CAST(LEAST('11','2') AS SIGNED)
1570   forces Item_func_min_max to compare the arguments as numbers rather
1571   than strings.
1572   Perhaps this should be changed eventually (see MDEV-5893).
1573 */
1574 class Item_func_min_max :public Item_hybrid_func
1575 {
1576   String tmp_value;
1577   int cmp_sign;
1578 protected:
1579   bool fix_attributes(Item **item, uint nitems);
1580 public:
Item_func_min_max(THD * thd,List<Item> & list,int cmp_sign_arg)1581   Item_func_min_max(THD *thd, List<Item> &list, int cmp_sign_arg):
1582     Item_hybrid_func(thd, list), cmp_sign(cmp_sign_arg)
1583   {}
1584   String *val_str_native(String *str);
1585   double val_real_native();
1586   longlong val_int_native();
1587   my_decimal *val_decimal_native(my_decimal *);
1588   bool get_date_native(MYSQL_TIME *res, ulonglong fuzzydate);
1589   bool get_time_native(MYSQL_TIME *res);
1590 
val_real()1591   double val_real()
1592   {
1593     DBUG_ASSERT(fixed);
1594     return Item_func_min_max::type_handler()->
1595              Item_func_min_max_val_real(this);
1596   }
val_int()1597   longlong val_int()
1598   {
1599     DBUG_ASSERT(fixed);
1600     return Item_func_min_max::type_handler()->
1601              Item_func_min_max_val_int(this);
1602   }
val_str(String * str)1603   String *val_str(String *str)
1604   {
1605     DBUG_ASSERT(fixed);
1606     return Item_func_min_max::type_handler()->
1607              Item_func_min_max_val_str(this, str);
1608   }
val_decimal(my_decimal * dec)1609   my_decimal *val_decimal(my_decimal *dec)
1610   {
1611     DBUG_ASSERT(fixed);
1612     return Item_func_min_max::type_handler()->
1613              Item_func_min_max_val_decimal(this, dec);
1614   }
get_date(MYSQL_TIME * res,ulonglong fuzzy_date)1615   bool get_date(MYSQL_TIME *res, ulonglong fuzzy_date)
1616   {
1617     DBUG_ASSERT(fixed);
1618     return Item_func_min_max::type_handler()->
1619              Item_func_min_max_get_date(this, res, fuzzy_date);
1620   }
aggregate_attributes_real(Item ** items,uint nitems)1621   void aggregate_attributes_real(Item **items, uint nitems)
1622   {
1623     /*
1624       Aggregating attributes for the double data type for LEAST/GREATEST
1625       is almost the same with aggregating for CASE-alike hybrid functions,
1626       (CASE..THEN, COALESCE, IF, etc).
1627       There is one notable difference though, when a numeric argument is mixed
1628       with a string argument:
1629       - CASE-alike functions return a string data type in such cases
1630         COALESCE(10,'x') -> VARCHAR(2) = '10'
1631       - LEAST/GREATEST returns double:
1632         GREATEST(10,'10e4') -> DOUBLE = 100000
1633       As the string argument can represent a number in the scientific notation,
1634       like in the example above, max_length of the result can be longer than
1635       max_length of the arguments. To handle this properly, max_length is
1636       additionally assigned to the result of float_length(decimals).
1637     */
1638     Item_func::aggregate_attributes_real(items, nitems);
1639     max_length= float_length(decimals);
1640   }
fix_length_and_dec()1641   bool fix_length_and_dec()
1642   {
1643     if (aggregate_for_min_max(func_name(), args, arg_count))
1644       return true;
1645     fix_attributes(args, arg_count);
1646     return false;
1647   }
1648 };
1649 
1650 class Item_func_min :public Item_func_min_max
1651 {
1652 public:
Item_func_min(THD * thd,List<Item> & list)1653   Item_func_min(THD *thd, List<Item> &list): Item_func_min_max(thd, list, 1) {}
func_name()1654   const char *func_name() const { return "least"; }
get_copy(THD * thd)1655   Item *get_copy(THD *thd)
1656   { return get_item_copy<Item_func_min>(thd, this); }
1657 };
1658 
1659 class Item_func_max :public Item_func_min_max
1660 {
1661 public:
Item_func_max(THD * thd,List<Item> & list)1662   Item_func_max(THD *thd, List<Item> &list): Item_func_min_max(thd, list, -1) {}
func_name()1663   const char *func_name() const { return "greatest"; }
get_copy(THD * thd)1664   Item *get_copy(THD *thd)
1665   { return get_item_copy<Item_func_max>(thd, this); }
1666 };
1667 
1668 
1669 /*
1670   Objects of this class are used for ROLLUP queries to wrap up
1671   each constant item referred to in GROUP BY list.
1672 */
1673 
1674 class Item_func_rollup_const :public Item_func
1675 {
1676 public:
Item_func_rollup_const(THD * thd,Item * a)1677   Item_func_rollup_const(THD *thd, Item *a): Item_func(thd, a)
1678   {
1679     name= a->name;
1680   }
val_real()1681   double val_real() { return val_real_from_item(args[0]); }
val_int()1682   longlong val_int() { return val_int_from_item(args[0]); }
val_str(String * str)1683   String *val_str(String *str) { return val_str_from_item(args[0], str); }
val_decimal(my_decimal * dec)1684   my_decimal *val_decimal(my_decimal *dec)
1685     { return val_decimal_from_item(args[0], dec); }
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)1686   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
1687     { return get_date_from_item(args[0], ltime, fuzzydate); }
func_name()1688   const char *func_name() const { return "rollup_const"; }
const_item()1689   bool const_item() const { return 0; }
type_handler()1690   const Type_handler *type_handler() const { return args[0]->type_handler(); }
fix_length_and_dec()1691   bool fix_length_and_dec()
1692   {
1693     collation= args[0]->collation;
1694     max_length= args[0]->max_length;
1695     decimals=args[0]->decimals;
1696     return FALSE;
1697   }
get_copy(THD * thd)1698   Item *get_copy(THD *thd)
1699   { return get_item_copy<Item_func_rollup_const>(thd, this); }
1700 };
1701 
1702 
1703 class Item_long_func_length: public Item_long_func
1704 {
check_arguments()1705   bool check_arguments() const
1706   { return args[0]->check_type_can_return_str(func_name()); }
1707 public:
Item_long_func_length(THD * thd,Item * a)1708   Item_long_func_length(THD *thd, Item *a): Item_long_func(thd, a) {}
fix_length_and_dec()1709   bool fix_length_and_dec() { max_length=10; return FALSE; }
1710 };
1711 
1712 
1713 class Item_func_octet_length :public Item_long_func_length
1714 {
1715   String value;
1716 public:
Item_func_octet_length(THD * thd,Item * a)1717   Item_func_octet_length(THD *thd, Item *a): Item_long_func_length(thd, a) {}
1718   longlong val_int();
func_name()1719   const char *func_name() const { return "octet_length"; }
get_copy(THD * thd)1720   Item *get_copy(THD *thd)
1721   { return get_item_copy<Item_func_octet_length>(thd, this); }
1722 };
1723 
1724 class Item_func_bit_length :public Item_longlong_func
1725 {
1726   String value;
1727 public:
Item_func_bit_length(THD * thd,Item * a)1728   Item_func_bit_length(THD *thd, Item *a): Item_longlong_func(thd, a) {}
fix_length_and_dec()1729   bool fix_length_and_dec()
1730   {
1731     max_length= 11; // 0x100000000*8 = 34,359,738,368
1732     return FALSE;
1733   }
1734   longlong val_int();
func_name()1735   const char *func_name() const { return "bit_length"; }
get_copy(THD * thd)1736   Item *get_copy(THD *thd)
1737   { return get_item_copy<Item_func_bit_length>(thd, this); }
1738 };
1739 
1740 class Item_func_char_length :public Item_long_func_length
1741 {
1742   String value;
1743 public:
Item_func_char_length(THD * thd,Item * a)1744   Item_func_char_length(THD *thd, Item *a): Item_long_func_length(thd, a) {}
1745   longlong val_int();
func_name()1746   const char *func_name() const { return "char_length"; }
get_copy(THD * thd)1747   Item *get_copy(THD *thd)
1748   { return get_item_copy<Item_func_char_length>(thd, this); }
1749 };
1750 
1751 class Item_func_coercibility :public Item_long_func
1752 {
check_arguments()1753   bool check_arguments() const
1754   { return args[0]->check_type_can_return_str(func_name()); }
1755 public:
Item_func_coercibility(THD * thd,Item * a)1756   Item_func_coercibility(THD *thd, Item *a): Item_long_func(thd, a) {}
1757   longlong val_int();
func_name()1758   const char *func_name() const { return "coercibility"; }
fix_length_and_dec()1759   bool fix_length_and_dec() { max_length=10; maybe_null= 0; return FALSE; }
eval_not_null_tables(void *)1760   bool eval_not_null_tables(void *)
1761   {
1762     not_null_tables_cache= 0;
1763     return false;
1764   }
propagate_equal_fields(THD * thd,const Context & ctx,COND_EQUAL * cond)1765   Item* propagate_equal_fields(THD *thd, const Context &ctx, COND_EQUAL *cond)
1766   { return this; }
const_item()1767   bool const_item() const { return true; }
get_copy(THD * thd)1768   Item *get_copy(THD *thd)
1769   { return get_item_copy<Item_func_coercibility>(thd, this); }
1770 };
1771 
1772 
1773 /*
1774   In the corner case LOCATE could return (4,294,967,296 + 1),
1775   which would not fit into Item_long_func range.
1776   But string lengths are limited with max_allowed_packet,
1777   which cannot be bigger than 1024*1024*1024.
1778 */
1779 class Item_func_locate :public Item_long_func
1780 {
check_arguments()1781   bool check_arguments() const
1782   {
1783     return check_argument_types_can_return_str(0, 2) ||
1784            (arg_count > 2 && args[2]->check_type_can_return_int(func_name()));
1785   }
1786   String value1,value2;
1787   DTCollation cmp_collation;
1788 public:
Item_func_locate(THD * thd,Item * a,Item * b)1789   Item_func_locate(THD *thd, Item *a, Item *b)
1790    :Item_long_func(thd, a, b) {}
Item_func_locate(THD * thd,Item * a,Item * b,Item * c)1791   Item_func_locate(THD *thd, Item *a, Item *b, Item *c)
1792    :Item_long_func(thd, a, b, c) {}
func_name()1793   const char *func_name() const { return "locate"; }
1794   longlong val_int();
fix_length_and_dec()1795   bool fix_length_and_dec()
1796   {
1797     max_length= MY_INT32_NUM_DECIMAL_DIGITS;
1798     return agg_arg_charsets_for_comparison(cmp_collation, args, 2);
1799   }
1800   virtual void print(String *str, enum_query_type query_type);
get_copy(THD * thd)1801   Item *get_copy(THD *thd)
1802   { return get_item_copy<Item_func_locate>(thd, this); }
1803 };
1804 
1805 
1806 class Item_func_field :public Item_long_func
1807 {
1808   String value,tmp;
1809   Item_result cmp_type;
1810   DTCollation cmp_collation;
1811 public:
Item_func_field(THD * thd,List<Item> & list)1812   Item_func_field(THD *thd, List<Item> &list): Item_long_func(thd, list) {}
1813   longlong val_int();
func_name()1814   const char *func_name() const { return "field"; }
1815   bool fix_length_and_dec();
get_copy(THD * thd)1816   Item *get_copy(THD *thd)
1817   { return get_item_copy<Item_func_field>(thd, this); }
1818 };
1819 
1820 
1821 class Item_func_ascii :public Item_long_func
1822 {
check_arguments()1823   bool check_arguments() const
1824   { return check_argument_types_can_return_str(0, arg_count); }
1825   String value;
1826 public:
Item_func_ascii(THD * thd,Item * a)1827   Item_func_ascii(THD *thd, Item *a): Item_long_func(thd, a) {}
1828   longlong val_int();
func_name()1829   const char *func_name() const { return "ascii"; }
fix_length_and_dec()1830   bool fix_length_and_dec() { max_length=3; return FALSE; }
get_copy(THD * thd)1831   Item *get_copy(THD *thd)
1832   { return get_item_copy<Item_func_ascii>(thd, this); }
1833 };
1834 
1835 class Item_func_ord :public Item_long_func
1836 {
check_arguments()1837   bool check_arguments() const
1838   { return args[0]->check_type_can_return_str(func_name()); }
1839   String value;
1840 public:
Item_func_ord(THD * thd,Item * a)1841   Item_func_ord(THD *thd, Item *a): Item_long_func(thd, a) {}
fix_length_and_dec()1842   bool fix_length_and_dec() { fix_char_length(7); return FALSE; }
1843   longlong val_int();
func_name()1844   const char *func_name() const { return "ord"; }
get_copy(THD * thd)1845   Item *get_copy(THD *thd)
1846   { return get_item_copy<Item_func_ord>(thd, this); }
1847 };
1848 
1849 class Item_func_find_in_set :public Item_long_func
1850 {
check_arguments()1851   bool check_arguments() const
1852   { return check_argument_types_can_return_str(0, 2); }
1853   String value,value2;
1854   uint enum_value;
1855   ulonglong enum_bit;
1856   DTCollation cmp_collation;
1857 public:
Item_func_find_in_set(THD * thd,Item * a,Item * b)1858   Item_func_find_in_set(THD *thd, Item *a, Item *b):
1859     Item_long_func(thd, a, b), enum_value(0) {}
1860   longlong val_int();
func_name()1861   const char *func_name() const { return "find_in_set"; }
1862   bool fix_length_and_dec();
get_copy(THD * thd)1863   Item *get_copy(THD *thd)
1864   { return get_item_copy<Item_func_find_in_set>(thd, this); }
1865 };
1866 
1867 /* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
1868 
1869 class Item_func_bit: public Item_longlong_func
1870 {
check_arguments()1871   bool check_arguments() const
1872   { return check_argument_types_can_return_int(0, arg_count); }
1873 public:
Item_func_bit(THD * thd,Item * a,Item * b)1874   Item_func_bit(THD *thd, Item *a, Item *b): Item_longlong_func(thd, a, b) {}
Item_func_bit(THD * thd,Item * a)1875   Item_func_bit(THD *thd, Item *a): Item_longlong_func(thd, a) {}
fix_length_and_dec()1876   bool fix_length_and_dec() { unsigned_flag= 1; return FALSE; }
1877 
print(String * str,enum_query_type query_type)1878   virtual inline void print(String *str, enum_query_type query_type)
1879   {
1880     print_op(str, query_type);
1881   }
need_parentheses_in_default()1882   bool need_parentheses_in_default() { return true; }
1883 };
1884 
1885 class Item_func_bit_or :public Item_func_bit
1886 {
1887 public:
Item_func_bit_or(THD * thd,Item * a,Item * b)1888   Item_func_bit_or(THD *thd, Item *a, Item *b): Item_func_bit(thd, a, b) {}
1889   longlong val_int();
func_name()1890   const char *func_name() const { return "|"; }
precedence()1891   enum precedence precedence() const { return BITOR_PRECEDENCE; }
get_copy(THD * thd)1892   Item *get_copy(THD *thd)
1893   { return get_item_copy<Item_func_bit_or>(thd, this); }
1894 };
1895 
1896 class Item_func_bit_and :public Item_func_bit
1897 {
1898 public:
Item_func_bit_and(THD * thd,Item * a,Item * b)1899   Item_func_bit_and(THD *thd, Item *a, Item *b): Item_func_bit(thd, a, b) {}
1900   longlong val_int();
func_name()1901   const char *func_name() const { return "&"; }
precedence()1902   enum precedence precedence() const { return BITAND_PRECEDENCE; }
get_copy(THD * thd)1903   Item *get_copy(THD *thd)
1904   { return get_item_copy<Item_func_bit_and>(thd, this); }
1905 };
1906 
1907 class Item_func_bit_count :public Item_long_func
1908 {
check_arguments()1909   bool check_arguments() const
1910   { return args[0]->check_type_can_return_int(func_name()); }
1911 public:
Item_func_bit_count(THD * thd,Item * a)1912   Item_func_bit_count(THD *thd, Item *a): Item_long_func(thd, a) {}
1913   longlong val_int();
func_name()1914   const char *func_name() const { return "bit_count"; }
fix_length_and_dec()1915   bool fix_length_and_dec() { max_length=2; return FALSE; }
get_copy(THD * thd)1916   Item *get_copy(THD *thd)
1917   { return get_item_copy<Item_func_bit_count>(thd, this); }
1918 };
1919 
1920 class Item_func_shift_left :public Item_func_bit
1921 {
1922 public:
Item_func_shift_left(THD * thd,Item * a,Item * b)1923   Item_func_shift_left(THD *thd, Item *a, Item *b): Item_func_bit(thd, a, b) {}
1924   longlong val_int();
func_name()1925   const char *func_name() const { return "<<"; }
precedence()1926   enum precedence precedence() const { return SHIFT_PRECEDENCE; }
get_copy(THD * thd)1927   Item *get_copy(THD *thd)
1928   { return get_item_copy<Item_func_shift_left>(thd, this); }
1929 };
1930 
1931 class Item_func_shift_right :public Item_func_bit
1932 {
1933 public:
Item_func_shift_right(THD * thd,Item * a,Item * b)1934   Item_func_shift_right(THD *thd, Item *a, Item *b): Item_func_bit(thd, a, b) {}
1935   longlong val_int();
func_name()1936   const char *func_name() const { return ">>"; }
precedence()1937   enum precedence precedence() const { return SHIFT_PRECEDENCE; }
get_copy(THD * thd)1938   Item *get_copy(THD *thd)
1939   { return get_item_copy<Item_func_shift_right>(thd, this); }
1940 };
1941 
1942 class Item_func_bit_neg :public Item_func_bit
1943 {
1944 public:
Item_func_bit_neg(THD * thd,Item * a)1945   Item_func_bit_neg(THD *thd, Item *a): Item_func_bit(thd, a) {}
1946   longlong val_int();
func_name()1947   const char *func_name() const { return "~"; }
precedence()1948   enum precedence precedence() const { return NEG_PRECEDENCE; }
print(String * str,enum_query_type query_type)1949   void print(String *str, enum_query_type query_type)
1950   {
1951     str->append(func_name());
1952     args[0]->print_parenthesised(str, query_type, precedence());
1953   }
get_copy(THD * thd)1954   Item *get_copy(THD *thd)
1955   { return get_item_copy<Item_func_bit_neg>(thd, this); }
1956 };
1957 
1958 
1959 class Item_func_last_insert_id :public Item_longlong_func
1960 {
check_arguments()1961   bool check_arguments() const
1962   { return check_argument_types_can_return_int(0, arg_count); }
1963 public:
Item_func_last_insert_id(THD * thd)1964   Item_func_last_insert_id(THD *thd): Item_longlong_func(thd) {}
Item_func_last_insert_id(THD * thd,Item * a)1965   Item_func_last_insert_id(THD *thd, Item *a): Item_longlong_func(thd, a) {}
1966   longlong val_int();
func_name()1967   const char *func_name() const { return "last_insert_id"; }
fix_length_and_dec()1968   bool fix_length_and_dec()
1969   {
1970     unsigned_flag= true;
1971     if (arg_count)
1972       max_length= args[0]->max_length;
1973     return FALSE;
1974   }
1975   bool fix_fields(THD *thd, Item **ref);
check_vcol_func_processor(void * arg)1976   bool check_vcol_func_processor(void *arg)
1977   {
1978     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
1979   }
get_copy(THD * thd)1980   Item *get_copy(THD *thd)
1981   { return get_item_copy<Item_func_last_insert_id>(thd, this); }
1982 };
1983 
1984 
1985 class Item_func_benchmark :public Item_long_func
1986 {
check_arguments()1987   bool check_arguments() const
1988   {
1989     return args[0]->check_type_can_return_int(func_name()) ||
1990            args[1]->check_type_scalar(func_name());
1991   }
1992 public:
Item_func_benchmark(THD * thd,Item * count_expr,Item * expr)1993   Item_func_benchmark(THD *thd, Item *count_expr, Item *expr):
1994     Item_long_func(thd, count_expr, expr)
1995   {}
1996   longlong val_int();
func_name()1997   const char *func_name() const { return "benchmark"; }
fix_length_and_dec()1998   bool fix_length_and_dec() { max_length=1; maybe_null=0; return FALSE; }
1999   virtual void print(String *str, enum_query_type query_type);
check_vcol_func_processor(void * arg)2000   bool check_vcol_func_processor(void *arg)
2001   {
2002     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
2003   }
get_copy(THD * thd)2004   Item *get_copy(THD *thd)
2005   { return get_item_copy<Item_func_benchmark>(thd, this); }
2006 };
2007 
2008 
2009 void item_func_sleep_init(void);
2010 void item_func_sleep_free(void);
2011 
2012 class Item_func_sleep :public Item_long_func
2013 {
check_arguments()2014   bool check_arguments() const
2015   { return args[0]->check_type_can_return_real(func_name()); }
2016 public:
Item_func_sleep(THD * thd,Item * a)2017   Item_func_sleep(THD *thd, Item *a): Item_long_func(thd, a) {}
fix_length_and_dec()2018   bool fix_length_and_dec() { fix_char_length(1); return FALSE; }
const_item()2019   bool const_item() const { return 0; }
func_name()2020   const char *func_name() const { return "sleep"; }
used_tables()2021   table_map used_tables() const
2022   {
2023     return used_tables_cache | RAND_TABLE_BIT;
2024   }
is_expensive()2025   bool is_expensive() { return 1; }
2026   longlong val_int();
check_vcol_func_processor(void * arg)2027   bool check_vcol_func_processor(void *arg)
2028   {
2029     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
2030   }
get_copy(THD * thd)2031   Item *get_copy(THD *thd)
2032   { return get_item_copy<Item_func_sleep>(thd, this); }
2033 };
2034 
2035 
2036 
2037 #ifdef HAVE_DLOPEN
2038 
2039 class Item_udf_func :public Item_func
2040 {
2041   /**
2042     Mark "this" as non-deterministic if it uses no tables
2043     and is not a constant at the same time.
2044   */
set_non_deterministic_if_needed()2045   void set_non_deterministic_if_needed()
2046   {
2047     if (!const_item_cache && !used_tables_cache)
2048       used_tables_cache= RAND_TABLE_BIT;
2049   }
2050 protected:
2051   udf_handler udf;
is_expensive_processor(void * arg)2052   bool is_expensive_processor(void *arg) { return TRUE; }
2053 
2054 public:
Item_udf_func(THD * thd,udf_func * udf_arg)2055   Item_udf_func(THD *thd, udf_func *udf_arg):
2056     Item_func(thd), udf(udf_arg) {}
Item_udf_func(THD * thd,udf_func * udf_arg,List<Item> & list)2057   Item_udf_func(THD *thd, udf_func *udf_arg, List<Item> &list):
2058     Item_func(thd, list), udf(udf_arg) {}
func_name()2059   const char *func_name() const { return udf.name(); }
functype()2060   enum Functype functype() const   { return UDF_FUNC; }
fix_fields(THD * thd,Item ** ref)2061   bool fix_fields(THD *thd, Item **ref)
2062   {
2063     DBUG_ASSERT(fixed == 0);
2064     bool res= udf.fix_fields(thd, this, arg_count, args);
2065     set_non_deterministic_if_needed();
2066     fixed= 1;
2067     return res;
2068   }
2069   void fix_num_length_and_dec();
update_used_tables()2070   void update_used_tables()
2071   {
2072     /*
2073       TODO: Make a member in UDF_INIT and return if a UDF is deterministic or
2074       not.
2075       Currently UDF_INIT has a member (const_item) that is an in/out
2076       parameter to the init() call.
2077       The code in udf_handler::fix_fields also duplicates the arguments
2078       handling code in Item_func::fix_fields().
2079 
2080       The lack of information if a UDF is deterministic makes writing
2081       a correct update_used_tables() for UDFs impossible.
2082       One solution to this would be :
2083        - Add a is_deterministic member of UDF_INIT
2084        - (optionally) deprecate the const_item member of UDF_INIT
2085        - Take away the duplicate code from udf_handler::fix_fields() and
2086          make Item_udf_func call Item_func::fix_fields() to process its
2087          arguments as for any other function.
2088        - Store the deterministic flag returned by <udf>_init into the
2089        udf_handler.
2090        - Don't implement Item_udf_func::fix_fields, implement
2091        Item_udf_func::fix_length_and_dec() instead (similar to non-UDF
2092        functions).
2093        - Override Item_func::update_used_tables to call
2094        Item_func::update_used_tables() and add a RAND_TABLE_BIT to the
2095        result of Item_func::update_used_tables() if the UDF is
2096        non-deterministic.
2097        - (optionally) rename RAND_TABLE_BIT to NONDETERMINISTIC_BIT to
2098        better describe its usage.
2099 
2100       The above would require a change of the UDF API.
2101       Until that change is done here's how the current code works:
2102       We call Item_func::update_used_tables() only when we know that
2103       the function depends on real non-const tables and is deterministic.
2104       This can be done only because we know that the optimizer will
2105       call update_used_tables() only when there's possibly a new const
2106       table. So update_used_tables() can only make a Item_func more
2107       constant than it is currently.
2108       That's why we don't need to do anything if a function is guaranteed
2109       to return non-constant (it's non-deterministic) or is already a
2110       const.
2111     */
2112     if ((used_tables_cache & ~PSEUDO_TABLE_BITS) &&
2113         !(used_tables_cache & RAND_TABLE_BIT))
2114     {
2115       Item_func::update_used_tables();
2116       set_non_deterministic_if_needed();
2117     }
2118   }
2119   void cleanup();
eval_not_null_tables(void * opt_arg)2120   bool eval_not_null_tables(void *opt_arg)
2121   {
2122     not_null_tables_cache= 0;
2123     return 0;
2124   }
is_expensive()2125   bool is_expensive() { return 1; }
2126   virtual void print(String *str, enum_query_type query_type);
check_vcol_func_processor(void * arg)2127   bool check_vcol_func_processor(void *arg)
2128   {
2129     return mark_unsupported_function(func_name(), "()", arg, VCOL_NON_DETERMINISTIC);
2130   }
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)2131   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
2132   {
2133     return type_handler()->Item_get_date(this, ltime, fuzzydate);
2134   }
2135 };
2136 
2137 
2138 class Item_func_udf_float :public Item_udf_func
2139 {
2140  public:
Item_func_udf_float(THD * thd,udf_func * udf_arg)2141   Item_func_udf_float(THD *thd, udf_func *udf_arg):
2142     Item_udf_func(thd, udf_arg) {}
Item_func_udf_float(THD * thd,udf_func * udf_arg,List<Item> & list)2143   Item_func_udf_float(THD *thd, udf_func *udf_arg,
2144                       List<Item> &list):
2145     Item_udf_func(thd, udf_arg, list) {}
val_int()2146   longlong val_int()
2147   {
2148     DBUG_ASSERT(fixed == 1);
2149     return Converter_double_to_longlong(Item_func_udf_float::val_real(),
2150                                         unsigned_flag).result();
2151   }
val_decimal(my_decimal * dec_buf)2152   my_decimal *val_decimal(my_decimal *dec_buf)
2153   {
2154     double res=val_real();
2155     if (null_value)
2156       return NULL;
2157     double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
2158     return dec_buf;
2159   }
2160   double val_real();
2161   String *val_str(String *str);
type_handler()2162   const Type_handler *type_handler() const { return &type_handler_double; }
fix_length_and_dec()2163   bool fix_length_and_dec() { fix_num_length_and_dec(); return FALSE; }
get_copy(THD * thd)2164   Item *get_copy(THD *thd)
2165   { return get_item_copy<Item_func_udf_float>(thd, this); }
2166 };
2167 
2168 
2169 class Item_func_udf_int :public Item_udf_func
2170 {
2171 public:
Item_func_udf_int(THD * thd,udf_func * udf_arg)2172   Item_func_udf_int(THD *thd, udf_func *udf_arg):
2173     Item_udf_func(thd, udf_arg) {}
Item_func_udf_int(THD * thd,udf_func * udf_arg,List<Item> & list)2174   Item_func_udf_int(THD *thd, udf_func *udf_arg,
2175                     List<Item> &list):
2176     Item_udf_func(thd, udf_arg, list) {}
2177   longlong val_int();
val_real()2178   double val_real() { return (double) Item_func_udf_int::val_int(); }
2179   String *val_str(String *str);
type_handler()2180   const Type_handler *type_handler() const { return &type_handler_longlong; }
fix_length_and_dec()2181   bool fix_length_and_dec() { decimals= 0; max_length= 21; return FALSE; }
get_copy(THD * thd)2182   Item *get_copy(THD *thd)
2183   { return get_item_copy<Item_func_udf_int>(thd, this); }
2184 };
2185 
2186 
2187 class Item_func_udf_decimal :public Item_udf_func
2188 {
2189 public:
Item_func_udf_decimal(THD * thd,udf_func * udf_arg)2190   Item_func_udf_decimal(THD *thd, udf_func *udf_arg):
2191     Item_udf_func(thd, udf_arg) {}
Item_func_udf_decimal(THD * thd,udf_func * udf_arg,List<Item> & list)2192   Item_func_udf_decimal(THD *thd, udf_func *udf_arg, List<Item> &list):
2193     Item_udf_func(thd, udf_arg, list) {}
2194   longlong val_int();
2195   double val_real();
2196   my_decimal *val_decimal(my_decimal *);
2197   String *val_str(String *str);
type_handler()2198   const Type_handler *type_handler() const { return &type_handler_newdecimal; }
fix_length_and_dec()2199   bool fix_length_and_dec() { fix_num_length_and_dec(); return FALSE; }
get_copy(THD * thd)2200   Item *get_copy(THD *thd)
2201   { return get_item_copy<Item_func_udf_decimal>(thd, this); }
2202 };
2203 
2204 
2205 class Item_func_udf_str :public Item_udf_func
2206 {
2207 public:
Item_func_udf_str(THD * thd,udf_func * udf_arg)2208   Item_func_udf_str(THD *thd, udf_func *udf_arg):
2209     Item_udf_func(thd, udf_arg) {}
Item_func_udf_str(THD * thd,udf_func * udf_arg,List<Item> & list)2210   Item_func_udf_str(THD *thd, udf_func *udf_arg, List<Item> &list):
2211     Item_udf_func(thd, udf_arg, list) {}
2212   String *val_str(String *);
val_real()2213   double val_real()
2214   {
2215     int err_not_used;
2216     char *end_not_used;
2217     String *res;
2218     res= val_str(&str_value);
2219     return res ? my_strntod(res->charset(),(char*) res->ptr(),
2220                             res->length(), &end_not_used, &err_not_used) : 0.0;
2221   }
val_int()2222   longlong val_int()
2223   {
2224     int err_not_used;
2225     String *res;  res=val_str(&str_value);
2226     return res ? my_strntoll(res->charset(),res->ptr(),res->length(),10,
2227                              (char**) 0, &err_not_used) : (longlong) 0;
2228   }
val_decimal(my_decimal * dec_buf)2229   my_decimal *val_decimal(my_decimal *dec_buf)
2230   {
2231     String *res=val_str(&str_value);
2232     if (!res)
2233       return NULL;
2234     string2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
2235     return dec_buf;
2236   }
type_handler()2237   const Type_handler *type_handler() const { return string_type_handler(); }
2238   bool fix_length_and_dec();
get_copy(THD * thd)2239   Item *get_copy(THD *thd)
2240   { return get_item_copy<Item_func_udf_str>(thd, this); }
2241 };
2242 
2243 #else /* Dummy functions to get sql_yacc.cc compiled */
2244 
2245 class Item_func_udf_float :public Item_real_func
2246 {
2247  public:
Item_func_udf_float(THD * thd,udf_func * udf_arg)2248   Item_func_udf_float(THD *thd, udf_func *udf_arg):
2249     Item_real_func(thd) {}
Item_func_udf_float(THD * thd,udf_func * udf_arg,List<Item> & list)2250   Item_func_udf_float(THD *thd, udf_func *udf_arg, List<Item> &list):
2251     Item_real_func(thd, list) {}
val_real()2252   double val_real() { DBUG_ASSERT(fixed == 1); return 0.0; }
2253 };
2254 
2255 
2256 class Item_func_udf_int :public Item_int_func
2257 {
2258 public:
Item_func_udf_int(THD * thd,udf_func * udf_arg)2259   Item_func_udf_int(THD *thd, udf_func *udf_arg):
2260     Item_int_func(thd) {}
Item_func_udf_int(THD * thd,udf_func * udf_arg,List<Item> & list)2261   Item_func_udf_int(THD *thd, udf_func *udf_arg, List<Item> &list):
2262     Item_int_func(thd, list) {}
type_handler()2263   const Type_handler *type_handler() const { return &type_handler_longlong; }
val_int()2264   longlong val_int() { DBUG_ASSERT(fixed == 1); return 0; }
2265 };
2266 
2267 
2268 class Item_func_udf_decimal :public Item_int_func
2269 {
2270 public:
Item_func_udf_decimal(THD * thd,udf_func * udf_arg)2271   Item_func_udf_decimal(THD *thd, udf_func *udf_arg):
2272     Item_int_func(thd) {}
Item_func_udf_decimal(THD * thd,udf_func * udf_arg,List<Item> & list)2273   Item_func_udf_decimal(THD *thd, udf_func *udf_arg, List<Item> &list):
2274     Item_int_func(thd, list) {}
type_handler()2275   const Type_handler *type_handler() const { return &type_handler_longlong; }
val_decimal(my_decimal *)2276   my_decimal *val_decimal(my_decimal *) { DBUG_ASSERT(fixed == 1); return 0; }
2277 };
2278 
2279 
2280 class Item_func_udf_str :public Item_func
2281 {
2282 public:
Item_func_udf_str(THD * thd,udf_func * udf_arg)2283   Item_func_udf_str(THD *thd, udf_func *udf_arg):
2284     Item_func(thd) {}
Item_func_udf_str(THD * thd,udf_func * udf_arg,List<Item> & list)2285   Item_func_udf_str(THD *thd, udf_func *udf_arg, List<Item> &list):
2286     Item_func(thd, list) {}
val_str(String *)2287   String *val_str(String *)
2288     { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
val_real()2289   double val_real() { DBUG_ASSERT(fixed == 1); null_value= 1; return 0.0; }
val_int()2290   longlong val_int() { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
fix_length_and_dec()2291   bool fix_length_and_dec() { maybe_null=1; max_length=0; return FALSE; }
2292 };
2293 
2294 #endif /* HAVE_DLOPEN */
2295 
2296 void mysql_ull_cleanup(THD *thd);
2297 void mysql_ull_set_explicit_lock_duration(THD *thd);
2298 
2299 class Item_func_get_lock :public Item_long_func
2300 {
check_arguments()2301   bool check_arguments() const
2302   {
2303     return args[0]->check_type_general_purpose_string(func_name()) ||
2304            args[1]->check_type_can_return_real(func_name());
2305   }
2306   String value;
2307  public:
Item_func_get_lock(THD * thd,Item * a,Item * b)2308   Item_func_get_lock(THD *thd, Item *a, Item *b) :Item_long_func(thd, a, b) {}
2309   longlong val_int();
func_name()2310   const char *func_name() const { return "get_lock"; }
fix_length_and_dec()2311   bool fix_length_and_dec() { max_length=1; maybe_null=1; return FALSE; }
used_tables()2312   table_map used_tables() const
2313   {
2314     return used_tables_cache | RAND_TABLE_BIT;
2315   }
const_item()2316   bool const_item() const { return 0; }
is_expensive()2317   bool is_expensive() { return 1; }
check_vcol_func_processor(void * arg)2318   bool check_vcol_func_processor(void *arg)
2319   {
2320     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
2321   }
get_copy(THD * thd)2322   Item *get_copy(THD *thd)
2323   { return get_item_copy<Item_func_get_lock>(thd, this); }
2324 };
2325 
2326 class Item_func_release_lock :public Item_long_func
2327 {
check_arguments()2328   bool check_arguments() const
2329   { return args[0]->check_type_general_purpose_string(func_name()); }
2330   String value;
2331 public:
Item_func_release_lock(THD * thd,Item * a)2332   Item_func_release_lock(THD *thd, Item *a): Item_long_func(thd, a) {}
2333   longlong val_int();
func_name()2334   const char *func_name() const { return "release_lock"; }
fix_length_and_dec()2335   bool fix_length_and_dec() { max_length= 1; maybe_null= 1; return FALSE; }
used_tables()2336   table_map used_tables() const
2337   {
2338     return used_tables_cache | RAND_TABLE_BIT;
2339   }
const_item()2340   bool const_item() const { return 0; }
is_expensive()2341   bool is_expensive() { return 1; }
check_vcol_func_processor(void * arg)2342   bool check_vcol_func_processor(void *arg)
2343   {
2344     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
2345   }
get_copy(THD * thd)2346   Item *get_copy(THD *thd)
2347   { return get_item_copy<Item_func_release_lock>(thd, this); }
2348 };
2349 
2350 /* replication functions */
2351 
2352 class Item_master_pos_wait :public Item_longlong_func
2353 {
check_arguments()2354   bool check_arguments() const
2355   {
2356     return
2357       args[0]->check_type_general_purpose_string(func_name()) ||
2358       args[1]->check_type_can_return_int(func_name()) ||
2359       (arg_count > 2 && args[2]->check_type_can_return_int(func_name())) ||
2360       (arg_count > 3 && args[3]->check_type_general_purpose_string(func_name()));
2361   }
2362   String value;
2363 public:
Item_master_pos_wait(THD * thd,Item * a,Item * b)2364   Item_master_pos_wait(THD *thd, Item *a, Item *b)
2365    :Item_longlong_func(thd, a, b) {}
Item_master_pos_wait(THD * thd,Item * a,Item * b,Item * c)2366   Item_master_pos_wait(THD *thd, Item *a, Item *b, Item *c):
2367     Item_longlong_func(thd, a, b, c) {}
Item_master_pos_wait(THD * thd,Item * a,Item * b,Item * c,Item * d)2368   Item_master_pos_wait(THD *thd, Item *a, Item *b, Item *c, Item *d):
2369     Item_longlong_func(thd, a, b, c, d) {}
2370   longlong val_int();
func_name()2371   const char *func_name() const { return "master_pos_wait"; }
fix_length_and_dec()2372   bool fix_length_and_dec() { max_length=21; maybe_null=1; return FALSE; }
check_vcol_func_processor(void * arg)2373   bool check_vcol_func_processor(void *arg)
2374   {
2375     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
2376   }
get_copy(THD * thd)2377   Item *get_copy(THD *thd)
2378   { return get_item_copy<Item_master_pos_wait>(thd, this); }
2379 };
2380 
2381 
2382 class Item_master_gtid_wait :public Item_long_func
2383 {
check_arguments()2384   bool check_arguments() const
2385   {
2386     return args[0]->check_type_general_purpose_string(func_name()) ||
2387            (arg_count > 1 && args[1]->check_type_can_return_real(func_name()));
2388   }
2389   String value;
2390 public:
Item_master_gtid_wait(THD * thd,Item * a)2391   Item_master_gtid_wait(THD *thd, Item *a)
2392    :Item_long_func(thd, a) {}
Item_master_gtid_wait(THD * thd,Item * a,Item * b)2393   Item_master_gtid_wait(THD *thd, Item *a, Item *b)
2394    :Item_long_func(thd, a, b) {}
2395   longlong val_int();
func_name()2396   const char *func_name() const { return "master_gtid_wait"; }
fix_length_and_dec()2397   bool fix_length_and_dec() { max_length=2; return FALSE; }
check_vcol_func_processor(void * arg)2398   bool check_vcol_func_processor(void *arg)
2399   {
2400     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
2401   }
get_copy(THD * thd)2402   Item *get_copy(THD *thd)
2403   { return get_item_copy<Item_master_gtid_wait>(thd, this); }
2404 };
2405 
2406 
2407 /* Handling of user definable variables */
2408 
2409 class user_var_entry;
2410 
2411 
2412 /**
2413   A class to set and get user variables
2414 */
2415 class Item_func_user_var :public Item_hybrid_func
2416 {
2417 protected:
2418   user_var_entry *m_var_entry;
2419 public:
2420   LEX_CSTRING name; // keep it public
Item_func_user_var(THD * thd,const LEX_CSTRING * a)2421   Item_func_user_var(THD *thd, const LEX_CSTRING *a)
2422     :Item_hybrid_func(thd), m_var_entry(NULL), name(*a) { }
Item_func_user_var(THD * thd,const LEX_CSTRING * a,Item * b)2423   Item_func_user_var(THD *thd, const LEX_CSTRING *a, Item *b)
2424     :Item_hybrid_func(thd, b), m_var_entry(NULL), name(*a) { }
Item_func_user_var(THD * thd,Item_func_user_var * item)2425   Item_func_user_var(THD *thd, Item_func_user_var *item)
2426     :Item_hybrid_func(thd, item),
2427     m_var_entry(item->m_var_entry), name(item->name) { }
create_tmp_field(bool group,TABLE * table)2428   Field *create_tmp_field(bool group, TABLE *table)
2429   { return create_table_field_from_handler(table); }
create_field_for_create_select(TABLE * table)2430   Field *create_field_for_create_select(TABLE *table)
2431   { return create_table_field_from_handler(table); }
2432   bool check_vcol_func_processor(void *arg);
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)2433   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
2434   { return type_handler()->Item_get_date(this, ltime, fuzzydate); }
2435 };
2436 
2437 
2438 class Item_func_set_user_var :public Item_func_user_var
2439 {
2440   /*
2441     The entry_thread_id variable is used:
2442     1) to skip unnecessary updates of the entry field (see above);
2443     2) to reset the entry field that was initialized in the other thread
2444        (for example, an item tree of a trigger that updates user variables
2445        may be shared between several connections, and the entry_thread_id field
2446        prevents updates of one connection user variables from a concurrent
2447        connection calling the same trigger that initially updated some
2448        user variable it the first connection context).
2449   */
2450   my_thread_id entry_thread_id;
2451   String value;
2452   my_decimal decimal_buff;
2453   bool null_item;
2454   union
2455   {
2456     longlong vint;
2457     double vreal;
2458     String *vstr;
2459     my_decimal *vdec;
2460   } save_result;
2461 
2462 public:
Item_func_set_user_var(THD * thd,const LEX_CSTRING * a,Item * b)2463   Item_func_set_user_var(THD *thd, const LEX_CSTRING *a, Item *b):
2464     Item_func_user_var(thd, a, b),
2465     entry_thread_id(0)
2466   {}
Item_func_set_user_var(THD * thd,Item_func_set_user_var * item)2467   Item_func_set_user_var(THD *thd, Item_func_set_user_var *item)
2468     :Item_func_user_var(thd, item),
2469     entry_thread_id(item->entry_thread_id),
2470     value(item->value), decimal_buff(item->decimal_buff),
2471     null_item(item->null_item), save_result(item->save_result)
2472   {}
2473 
functype()2474   enum Functype functype() const { return SUSERVAR_FUNC; }
2475   double val_real();
2476   longlong val_int();
2477   String *val_str(String *str);
2478   my_decimal *val_decimal(my_decimal *);
2479   double val_result();
2480   longlong val_int_result();
2481   bool val_bool_result();
2482   String *str_result(String *str);
2483   my_decimal *val_decimal_result(my_decimal *);
2484   bool is_null_result();
2485   bool update_hash(void *ptr, size_t length, enum Item_result type,
2486                    CHARSET_INFO *cs, bool unsigned_arg);
2487   bool send(Protocol *protocol, st_value *buffer);
2488   void make_send_field(THD *thd, Send_field *tmp_field);
2489   bool check(bool use_result_field);
2490   void save_item_result(Item *item);
2491   bool update();
2492   bool fix_fields(THD *thd, Item **ref);
2493   bool fix_length_and_dec();
2494   void print(String *str, enum_query_type query_type);
precedence()2495   enum precedence precedence() const { return ASSIGN_PRECEDENCE; }
2496   void print_as_stmt(String *str, enum_query_type query_type);
func_name()2497   const char *func_name() const { return "set_user_var"; }
2498   int save_in_field(Field *field, bool no_conversions,
2499                     bool can_use_result_field);
save_in_field(Field * field,bool no_conversions)2500   int save_in_field(Field *field, bool no_conversions)
2501   {
2502     return save_in_field(field, no_conversions, 1);
2503   }
save_org_in_field(Field * field,fast_field_copier data)2504   void save_org_in_field(Field *field,
2505                          fast_field_copier data __attribute__ ((__unused__)))
2506     { (void)save_in_field(field, 1, 0); }
2507   bool register_field_in_read_map(void *arg);
2508   bool register_field_in_bitmap(void *arg);
2509   bool set_entry(THD *thd, bool create_if_not_exists);
2510   void cleanup();
get_copy(THD * thd)2511   Item *get_copy(THD *thd)
2512   { return get_item_copy<Item_func_set_user_var>(thd, this); }
excl_dep_on_table(table_map tab_map)2513   bool excl_dep_on_table(table_map tab_map) { return false; }
2514 };
2515 
2516 
2517 class Item_func_get_user_var :public Item_func_user_var,
2518                               private Settable_routine_parameter
2519 {
2520 public:
Item_func_get_user_var(THD * thd,const LEX_CSTRING * a)2521   Item_func_get_user_var(THD *thd, const LEX_CSTRING *a):
2522     Item_func_user_var(thd, a) {}
functype()2523   enum Functype functype() const { return GUSERVAR_FUNC; }
get_name()2524   LEX_CSTRING get_name() { return name; }
2525   double val_real();
2526   longlong val_int();
2527   my_decimal *val_decimal(my_decimal*);
2528   String *val_str(String* str);
2529   bool fix_length_and_dec();
2530   virtual void print(String *str, enum_query_type query_type);
2531   /*
2532     We must always return variables as strings to guard against selects of type
2533     select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
2534   */
func_name()2535   const char *func_name() const { return "get_user_var"; }
2536   bool const_item() const;
used_tables()2537   table_map used_tables() const
2538   { return const_item() ? 0 : RAND_TABLE_BIT; }
2539   bool eq(const Item *item, bool binary_cmp) const;
get_copy(THD * thd)2540   Item *get_copy(THD *thd)
2541   { return get_item_copy<Item_func_get_user_var>(thd, this); }
2542 private:
2543   bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
2544 
2545 public:
get_settable_routine_parameter()2546   Settable_routine_parameter *get_settable_routine_parameter()
2547   {
2548     return this;
2549   }
2550 };
2551 
2552 
2553 /*
2554   This item represents user variable used as out parameter (e.g in LOAD DATA),
2555   and it is supposed to be used only for this purprose. So it is simplified
2556   a lot. Actually you should never obtain its value.
2557 
2558   The only two reasons for this thing being an Item is possibility to store it
2559   in List<Item> and desire to place this code somewhere near other functions
2560   working with user variables.
2561 */
2562 class Item_user_var_as_out_param :public Item,
2563                                   public Load_data_outvar
2564 {
2565   LEX_CSTRING org_name;
2566   user_var_entry *entry;
2567 public:
Item_user_var_as_out_param(THD * thd,const LEX_CSTRING * a)2568   Item_user_var_as_out_param(THD *thd, const LEX_CSTRING *a)
2569   :Item(thd)
2570   {
2571     DBUG_ASSERT(a->length < UINT_MAX32);
2572     org_name= *a;
2573     set_name(thd, a->str, a->length, system_charset_info);
2574   }
get_load_data_outvar()2575   Load_data_outvar *get_load_data_outvar()
2576   {
2577     return this;
2578   }
load_data_set_null(THD * thd,const Load_data_param * param)2579   bool load_data_set_null(THD *thd, const Load_data_param *param)
2580   {
2581     set_null_value(param->charset());
2582     return false;
2583   }
load_data_set_no_data(THD * thd,const Load_data_param * param)2584   bool load_data_set_no_data(THD *thd, const Load_data_param *param)
2585   {
2586     set_null_value(param->charset());
2587     return false;
2588   }
load_data_set_value(THD * thd,const char * pos,uint length,const Load_data_param * param)2589   bool load_data_set_value(THD *thd, const char *pos, uint length,
2590                            const Load_data_param *param)
2591   {
2592     set_value(pos, length, param->charset());
2593     return false;
2594   }
2595   void load_data_print_for_log_event(THD *thd, String *to) const;
load_data_add_outvar(THD * thd,Load_data_param * param)2596   bool load_data_add_outvar(THD *thd, Load_data_param *param) const
2597   {
2598     return param->add_outvar_user_var(thd);
2599   }
load_data_fixed_length()2600   uint load_data_fixed_length() const
2601   {
2602     return 0;
2603   }
2604   /* We should return something different from FIELD_ITEM here */
type()2605   enum Type type() const { return STRING_ITEM;}
2606   double val_real();
2607   longlong val_int();
2608   String *val_str(String *str);
2609   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
2610   my_decimal *val_decimal(my_decimal *decimal_buffer);
2611   /* fix_fields() binds variable name with its entry structure */
2612   bool fix_fields(THD *thd, Item **ref);
2613   void set_null_value(CHARSET_INFO* cs);
2614   void set_value(const char *str, uint length, CHARSET_INFO* cs);
type_handler()2615   const Type_handler *type_handler() const { return &type_handler_double; }
get_copy(THD * thd)2616   Item *get_copy(THD *thd)
2617   { return get_item_copy<Item_user_var_as_out_param>(thd, this); }
2618 };
2619 
2620 
2621 /* A system variable */
2622 
2623 #define GET_SYS_VAR_CACHE_LONG     1
2624 #define GET_SYS_VAR_CACHE_DOUBLE   2
2625 #define GET_SYS_VAR_CACHE_STRING   4
2626 
2627 class Item_func_get_system_var :public Item_func
2628 {
2629   sys_var *var;
2630   enum_var_type var_type, orig_var_type;
2631   LEX_CSTRING component;
2632   longlong cached_llval;
2633   double cached_dval;
2634   String cached_strval;
2635   bool cached_null_value;
2636   query_id_t used_query_id;
2637   uchar cache_present;
2638 
2639 public:
2640   Item_func_get_system_var(THD *thd, sys_var *var_arg,
2641                            enum_var_type var_type_arg,
2642                            LEX_CSTRING *component_arg, const char *name_arg,
2643                            size_t name_len_arg);
functype()2644   enum Functype functype() const { return GSYSVAR_FUNC; }
2645   void update_null_value();
2646   bool fix_length_and_dec();
2647   void print(String *str, enum_query_type query_type);
const_item()2648   bool const_item() const { return true; }
used_tables()2649   table_map used_tables() const { return 0; }
2650   const Type_handler *type_handler() const;
2651   double val_real();
2652   longlong val_int();
2653   String* val_str(String*);
val_decimal(my_decimal * dec_buf)2654   my_decimal *val_decimal(my_decimal *dec_buf)
2655   { return val_decimal_from_real(dec_buf); }
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)2656   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
2657   {
2658     return type_handler()->Item_get_date(this, ltime, fuzzydate);
2659   }
2660   /* TODO: fix to support views */
func_name()2661   const char *func_name() const { return "get_system_var"; }
2662   /**
2663     Indicates whether this system variable is written to the binlog or not.
2664 
2665     Variables are written to the binlog as part of "status_vars" in
2666     Query_log_event, as an Intvar_log_event, or a Rand_log_event.
2667 
2668     @return true if the variable is written to the binlog, false otherwise.
2669   */
2670   bool is_written_to_binlog();
2671   bool eq(const Item *item, bool binary_cmp) const;
2672 
2673   void cleanup();
2674   bool check_vcol_func_processor(void *arg);
get_copy(THD * thd)2675   Item *get_copy(THD *thd)
2676   { return get_item_copy<Item_func_get_system_var>(thd, this); }
2677 };
2678 
2679 
2680 /* for fulltext search */
2681 
2682 class Item_func_match :public Item_real_func
2683 {
2684 public:
2685   uint key, flags;
2686   bool join_key;
2687   DTCollation cmp_collation;
2688   FT_INFO *ft_handler;
2689   TABLE *table;
2690   Item_func_match *master;   // for master-slave optimization
2691   Item *concat_ws;           // Item_func_concat_ws
2692   String value;              // value of concat_ws
2693   String search_value;       // key_item()'s value converted to cmp_collation
2694 
Item_func_match(THD * thd,List<Item> & a,uint b)2695   Item_func_match(THD *thd, List<Item> &a, uint b):
2696     Item_real_func(thd, a), key(0), flags(b), join_key(0), ft_handler(0),
2697     table(0), master(0), concat_ws(0) { }
cleanup()2698   void cleanup()
2699   {
2700     DBUG_ENTER("Item_func_match::cleanup");
2701     Item_real_func::cleanup();
2702     if (!master && ft_handler)
2703       ft_handler->please->close_search(ft_handler);
2704     ft_handler= 0;
2705     concat_ws= 0;
2706     table= 0;           // required by Item_func_match::eq()
2707     DBUG_VOID_RETURN;
2708   }
is_expensive_processor(void * arg)2709   bool is_expensive_processor(void *arg) { return TRUE; }
functype()2710   enum Functype functype() const { return FT_FUNC; }
func_name()2711   const char *func_name() const { return "match"; }
eval_not_null_tables(void * opt_arg)2712   bool eval_not_null_tables(void *opt_arg)
2713   {
2714     not_null_tables_cache= 0;
2715     return 0;
2716   }
2717   bool fix_fields(THD *thd, Item **ref);
2718   bool eq(const Item *, bool binary_cmp) const;
2719   /* The following should be safe, even if we compare doubles */
val_int()2720   longlong val_int() { DBUG_ASSERT(fixed == 1); return val_real() != 0.0; }
2721   double val_real();
2722   virtual void print(String *str, enum_query_type query_type);
2723 
2724   bool fix_index();
2725   bool init_search(THD *thd, bool no_order);
check_vcol_func_processor(void * arg)2726   bool check_vcol_func_processor(void *arg)
2727   {
2728     return mark_unsupported_function("match ... against()", arg, VCOL_IMPOSSIBLE);
2729   }
get_copy(THD * thd)2730   Item *get_copy(THD *thd)
2731   { return get_item_copy<Item_func_match>(thd, this); }
build_clone(THD * thd)2732   Item *build_clone(THD *thd) { return 0; }
2733 private:
2734   /**
2735      Check whether storage engine for given table,
2736      allows FTS Boolean search on non-indexed columns.
2737 
2738      @todo A flag should be added to the extended fulltext API so that
2739            it may be checked whether search on non-indexed columns are
2740            supported. Currently, it is not possible to check for such a
2741            flag since @c this->ft_handler is not yet set when this function is
2742            called.  The current hack is to assume that search on non-indexed
2743            columns are supported for engines that does not support the extended
2744            fulltext API (e.g., MyISAM), while it is not supported for other
2745            engines (e.g., InnoDB)
2746 
2747      @param table_arg Table for which storage engine to check
2748 
2749      @retval true if BOOLEAN search on non-indexed columns is supported
2750      @retval false otherwise
2751    */
allows_search_on_non_indexed_columns(TABLE * table_arg)2752   bool allows_search_on_non_indexed_columns(TABLE* table_arg)
2753   {
2754     // Only Boolean search may support non_indexed columns
2755     if (!(flags & FT_BOOL))
2756       return false;
2757 
2758     DBUG_ASSERT(table_arg && table_arg->file);
2759 
2760     // Assume that if extended fulltext API is not supported,
2761     // non-indexed columns are allowed.  This will be true for MyISAM.
2762     if ((table_arg->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0)
2763       return true;
2764 
2765     return false;
2766   }
2767 };
2768 
2769 
2770 class Item_func_bit_xor : public Item_func_bit
2771 {
2772 public:
Item_func_bit_xor(THD * thd,Item * a,Item * b)2773   Item_func_bit_xor(THD *thd, Item *a, Item *b): Item_func_bit(thd, a, b) {}
2774   longlong val_int();
func_name()2775   const char *func_name() const { return "^"; }
precedence()2776   enum precedence precedence() const { return BITXOR_PRECEDENCE; }
get_copy(THD * thd)2777   Item *get_copy(THD *thd)
2778   { return get_item_copy<Item_func_bit_xor>(thd, this); }
2779 };
2780 
2781 class Item_func_is_free_lock :public Item_long_func
2782 {
check_arguments()2783   bool check_arguments() const
2784   { return args[0]->check_type_general_purpose_string(func_name()); }
2785   String value;
2786 public:
Item_func_is_free_lock(THD * thd,Item * a)2787   Item_func_is_free_lock(THD *thd, Item *a): Item_long_func(thd, a) {}
2788   longlong val_int();
func_name()2789   const char *func_name() const { return "is_free_lock"; }
fix_length_and_dec()2790   bool fix_length_and_dec()
2791   {
2792     decimals=0; max_length=1; maybe_null=1;
2793     return FALSE;
2794   }
check_vcol_func_processor(void * arg)2795   bool check_vcol_func_processor(void *arg)
2796   {
2797     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
2798   }
get_copy(THD * thd)2799   Item *get_copy(THD *thd)
2800   { return get_item_copy<Item_func_is_free_lock>(thd, this); }
2801 };
2802 
2803 class Item_func_is_used_lock :public Item_long_func
2804 {
check_arguments()2805   bool check_arguments() const
2806   { return args[0]->check_type_general_purpose_string(func_name()); }
2807   String value;
2808 public:
Item_func_is_used_lock(THD * thd,Item * a)2809   Item_func_is_used_lock(THD *thd, Item *a): Item_long_func(thd, a) {}
2810   longlong val_int();
func_name()2811   const char *func_name() const { return "is_used_lock"; }
fix_length_and_dec()2812   bool fix_length_and_dec()
2813   {
2814     decimals=0; max_length=10; maybe_null=1;
2815     return FALSE;
2816   }
check_vcol_func_processor(void * arg)2817   bool check_vcol_func_processor(void *arg)
2818   {
2819     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
2820   }
get_copy(THD * thd)2821   Item *get_copy(THD *thd)
2822   { return get_item_copy<Item_func_is_used_lock>(thd, this); }
2823 };
2824 
2825 
2826 struct Lex_cast_type_st: public Lex_length_and_dec_st
2827 {
2828 private:
2829   const Type_handler *m_type_handler;
2830 public:
setLex_cast_type_st2831   void set(const Type_handler *handler, const char *length, const char *dec)
2832   {
2833     m_type_handler= handler;
2834     Lex_length_and_dec_st::set(length, dec);
2835   }
setLex_cast_type_st2836   void set(const Type_handler *handler, Lex_length_and_dec_st length_and_dec)
2837   {
2838     m_type_handler= handler;
2839     Lex_length_and_dec_st::operator=(length_and_dec);
2840   }
setLex_cast_type_st2841   void set(const Type_handler *handler, const char *length)
2842   {
2843     set(handler, length, 0);
2844   }
setLex_cast_type_st2845   void set(const Type_handler *handler)
2846   {
2847     set(handler, 0, 0);
2848   }
type_handlerLex_cast_type_st2849   const Type_handler *type_handler() const { return m_type_handler; }
2850   Item *create_typecast_item(THD *thd, Item *item, CHARSET_INFO *cs= NULL)
2851   {
2852     return m_type_handler->
2853       create_typecast_item(thd, item,
2854                            Type_cast_attributes(length(), dec(), cs));
2855   }
2856 };
2857 
2858 
2859 class Item_func_row_count :public Item_longlong_func
2860 {
2861 public:
Item_func_row_count(THD * thd)2862   Item_func_row_count(THD *thd): Item_longlong_func(thd) {}
2863   longlong val_int();
func_name()2864   const char *func_name() const { return "row_count"; }
fix_length_and_dec()2865   bool fix_length_and_dec() { decimals= 0; maybe_null=0; return FALSE; }
check_vcol_func_processor(void * arg)2866   bool check_vcol_func_processor(void *arg)
2867   {
2868     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
2869   }
get_copy(THD * thd)2870   Item *get_copy(THD *thd)
2871   { return get_item_copy<Item_func_row_count>(thd, this); }
2872 };
2873 
2874 
2875 /*
2876  *
2877  * Stored FUNCTIONs
2878  *
2879  */
2880 
2881 class Item_func_sp :public Item_func,
2882                     public Item_sp
2883 {
2884 private:
2885   const Sp_handler *m_handler;
2886 
2887   bool execute();
2888 
2889 protected:
is_expensive_processor(void * arg)2890   bool is_expensive_processor(void *arg)
2891   { return is_expensive(); }
2892 
check_arguments()2893   bool check_arguments() const
2894   {
2895     // sp_prepare_func_item() checks that the number of columns is correct
2896     return false;
2897   }
2898 public:
2899 
2900   Item_func_sp(THD *thd, Name_resolution_context *context_arg,
2901                sp_name *name, const Sp_handler *sph);
2902 
2903   Item_func_sp(THD *thd, Name_resolution_context *context_arg,
2904                sp_name *name, const Sp_handler *sph, List<Item> &list);
2905 
~Item_func_sp()2906   virtual ~Item_func_sp()
2907   {}
2908 
2909   void update_used_tables();
2910 
2911   void cleanup();
2912 
2913   const char *func_name() const;
2914 
2915   const Type_handler *type_handler() const;
2916 
create_field_for_create_select(TABLE * table)2917   Field *create_field_for_create_select(TABLE *table)
2918   {
2919     return result_type() != STRING_RESULT ?
2920            sp_result_field :
2921            create_table_field_from_handler(table);
2922   }
2923   void make_send_field(THD *thd, Send_field *tmp_field);
2924 
val_int()2925   longlong val_int()
2926   {
2927     if (execute())
2928       return (longlong) 0;
2929     return sp_result_field->val_int();
2930   }
2931 
val_real()2932   double val_real()
2933   {
2934     if (execute())
2935       return 0.0;
2936     return sp_result_field->val_real();
2937   }
2938 
val_decimal(my_decimal * dec_buf)2939   my_decimal *val_decimal(my_decimal *dec_buf)
2940   {
2941     if (execute())
2942       return NULL;
2943     return sp_result_field->val_decimal(dec_buf);
2944   }
2945 
get_date(MYSQL_TIME * ltime,ulonglong fuzzydate)2946   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
2947   {
2948     if (execute())
2949       return true;
2950     return sp_result_field->get_date(ltime, fuzzydate);
2951   }
2952 
val_str(String * str)2953   String *val_str(String *str)
2954   {
2955     String buf;
2956     char buff[20];
2957     buf.set(buff, 20, str->charset());
2958     buf.length(0);
2959     if (execute())
2960       return NULL;
2961     /*
2962       result_field will set buf pointing to internal buffer
2963       of the resul_field. Due to this it will change any time
2964       when SP is executed. In order to prevent occasional
2965       corruption of returned value, we make here a copy.
2966     */
2967     sp_result_field->val_str(&buf);
2968     str->copy(buf);
2969     return str;
2970   }
2971 
update_null_value()2972   void update_null_value()
2973   {
2974     execute();
2975   }
2976 
change_context_processor(void * cntx)2977   virtual bool change_context_processor(void *cntx)
2978     { context= (Name_resolution_context *)cntx; return FALSE; }
2979 
functype()2980   virtual enum Functype functype() const { return FUNC_SP; }
2981 
2982   bool fix_fields(THD *thd, Item **ref);
2983   bool fix_length_and_dec(void);
2984   bool is_expensive();
2985 
get_sp_result_field()2986   inline Field *get_sp_result_field()
2987   {
2988     return sp_result_field;
2989   }
get_sp_name()2990   const sp_name *get_sp_name() const
2991   {
2992     return m_name;
2993   }
2994 
2995   bool check_vcol_func_processor(void *arg);
limit_index_condition_pushdown_processor(void * opt_arg)2996   bool limit_index_condition_pushdown_processor(void *opt_arg)
2997   {
2998     return TRUE;
2999   }
get_copy(THD *)3000   Item *get_copy(THD *) { return 0; }
eval_not_null_tables(void * opt_arg)3001   bool eval_not_null_tables(void *opt_arg)
3002   {
3003     not_null_tables_cache= 0;
3004     return 0;
3005   }
3006 };
3007 
3008 
3009 class Item_func_found_rows :public Item_longlong_func
3010 {
3011 public:
Item_func_found_rows(THD * thd)3012   Item_func_found_rows(THD *thd): Item_longlong_func(thd) {}
3013   longlong val_int();
func_name()3014   const char *func_name() const { return "found_rows"; }
fix_length_and_dec()3015   bool fix_length_and_dec() { decimals= 0; maybe_null=0; return FALSE; }
check_vcol_func_processor(void * arg)3016   bool check_vcol_func_processor(void *arg)
3017   {
3018     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
3019   }
get_copy(THD * thd)3020   Item *get_copy(THD *thd)
3021   { return get_item_copy<Item_func_found_rows>(thd, this); }
3022 };
3023 
3024 
3025 class Item_func_oracle_sql_rowcount :public Item_longlong_func
3026 {
3027 public:
Item_func_oracle_sql_rowcount(THD * thd)3028   Item_func_oracle_sql_rowcount(THD *thd): Item_longlong_func(thd) {}
3029   longlong val_int();
func_name()3030   const char *func_name() const { return "SQL%ROWCOUNT"; }
print(String * str,enum_query_type query_type)3031   void print(String *str, enum_query_type query_type)
3032   {
3033     str->append(func_name());
3034   }
check_vcol_func_processor(void * arg)3035   bool check_vcol_func_processor(void *arg)
3036   {
3037     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
3038   }
get_copy(THD * thd)3039   Item *get_copy(THD *thd)
3040   { return get_item_copy<Item_func_oracle_sql_rowcount>(thd, this); }
3041 };
3042 
3043 
3044 class Item_func_sqlcode: public Item_long_func
3045 {
3046 public:
Item_func_sqlcode(THD * thd)3047   Item_func_sqlcode(THD *thd): Item_long_func(thd) { }
3048   longlong val_int();
func_name()3049   const char *func_name() const { return "SQLCODE"; }
print(String * str,enum_query_type query_type)3050   void print(String *str, enum_query_type query_type)
3051   {
3052     str->append(func_name());
3053   }
check_vcol_func_processor(void * arg)3054   bool check_vcol_func_processor(void *arg)
3055   {
3056     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
3057   }
fix_length_and_dec()3058   bool fix_length_and_dec()
3059   {
3060     maybe_null= null_value= false;
3061     max_length= 11;
3062     return FALSE;
3063   }
get_copy(THD * thd)3064   Item *get_copy(THD *thd)
3065   { return get_item_copy<Item_func_sqlcode>(thd, this); }
3066 };
3067 
3068 
3069 void uuid_short_init();
3070 
3071 class Item_func_uuid_short :public Item_longlong_func
3072 {
3073 public:
Item_func_uuid_short(THD * thd)3074   Item_func_uuid_short(THD *thd): Item_longlong_func(thd) {}
func_name()3075   const char *func_name() const { return "uuid_short"; }
3076   longlong val_int();
const_item()3077   bool const_item() const { return false; }
fix_length_and_dec()3078   bool fix_length_and_dec()
3079   { max_length= 21; unsigned_flag=1; return FALSE; }
used_tables()3080   table_map used_tables() const { return RAND_TABLE_BIT; }
check_vcol_func_processor(void * arg)3081   bool check_vcol_func_processor(void *arg)
3082   {
3083     return mark_unsupported_function(func_name(), "()", arg, VCOL_NON_DETERMINISTIC);
3084   }
get_copy(THD * thd)3085   Item *get_copy(THD *thd)
3086   { return get_item_copy<Item_func_uuid_short>(thd, this); }
3087 };
3088 
3089 
3090 class Item_func_last_value :public Item_func
3091 {
3092 protected:
3093   Item *last_value;
3094 public:
Item_func_last_value(THD * thd,List<Item> & list)3095   Item_func_last_value(THD *thd, List<Item> &list): Item_func(thd, list) {}
3096   double val_real();
3097   longlong val_int();
3098   String *val_str(String *);
3099   my_decimal *val_decimal(my_decimal *);
3100   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
3101   bool fix_length_and_dec();
func_name()3102   const char *func_name() const { return "last_value"; }
type_handler()3103   const Type_handler *type_handler() const { return last_value->type_handler(); }
eval_not_null_tables(void *)3104   bool eval_not_null_tables(void *)
3105   {
3106     not_null_tables_cache= 0;
3107     return 0;
3108   }
const_item()3109   bool const_item() const { return 0; }
3110   void evaluate_sideeffects();
update_used_tables()3111   void update_used_tables()
3112   {
3113     Item_func::update_used_tables();
3114     maybe_null= last_value->maybe_null;
3115   }
get_copy(THD * thd)3116   Item *get_copy(THD *thd)
3117   { return get_item_copy<Item_func_last_value>(thd, this); }
3118 };
3119 
3120 
3121 /* Implementation for sequences: NEXT VALUE FOR sequence and NEXTVAL() */
3122 
3123 class Item_func_nextval :public Item_longlong_func
3124 {
3125 protected:
3126   TABLE_LIST *table_list;
3127   TABLE *table;
3128 public:
Item_func_nextval(THD * thd,TABLE_LIST * table_list_arg)3129   Item_func_nextval(THD *thd, TABLE_LIST *table_list_arg):
3130   Item_longlong_func(thd), table_list(table_list_arg) {}
3131   longlong val_int();
func_name()3132   const char *func_name() const { return "nextval"; }
fix_length_and_dec()3133   bool fix_length_and_dec()
3134   {
3135     unsigned_flag= 0;
3136     max_length= MAX_BIGINT_WIDTH;
3137     maybe_null= 1;                              /* In case of errors */
3138     return FALSE;
3139   }
3140   /*
3141     update_table() function must be called during the value function
3142     as in case of DEFAULT the sequence table may not yet be open
3143     while fix_fields() are called
3144   */
update_table()3145   void update_table()
3146   {
3147     if (!(table= table_list->table))
3148     {
3149       /*
3150         If nextval was used in DEFAULT then next_local points to
3151         the table_list used by to open the sequence table
3152       */
3153       table= table_list->next_local->table;
3154     }
3155   }
const_item()3156   bool const_item() const { return 0; }
get_copy(THD * thd)3157   Item *get_copy(THD *thd)
3158   { return get_item_copy<Item_func_nextval>(thd, this); }
3159   void print(String *str, enum_query_type query_type);
check_vcol_func_processor(void * arg)3160   bool check_vcol_func_processor(void *arg)
3161   {
3162     return mark_unsupported_function(func_name(), "()", arg,
3163                                      (VCOL_NON_DETERMINISTIC |
3164                                       VCOL_NOT_VIRTUAL));
3165   }
3166 };
3167 
3168 
3169 /* Implementation for sequences: LASTVAL(sequence), PostgreSQL style */
3170 
3171 class Item_func_lastval :public Item_func_nextval
3172 {
3173 public:
Item_func_lastval(THD * thd,TABLE_LIST * table_list_arg)3174   Item_func_lastval(THD *thd, TABLE_LIST *table_list_arg):
3175   Item_func_nextval(thd, table_list_arg) {}
3176   longlong val_int();
func_name()3177   const char *func_name() const { return "lastval"; }
get_copy(THD * thd)3178   Item *get_copy(THD *thd)
3179   { return get_item_copy<Item_func_lastval>(thd, this); }
3180 };
3181 
3182 
3183 /* Implementation for sequences: SETVAL(sequence), PostgreSQL style */
3184 
3185 class Item_func_setval :public Item_func_nextval
3186 {
3187   longlong nextval;
3188   ulonglong round;
3189   bool is_used;
3190 public:
Item_func_setval(THD * thd,TABLE_LIST * table_list_arg,longlong nextval_arg,ulonglong round_arg,bool is_used_arg)3191   Item_func_setval(THD *thd, TABLE_LIST *table_list_arg, longlong nextval_arg,
3192                    ulonglong round_arg, bool is_used_arg)
3193     : Item_func_nextval(thd, table_list_arg),
3194     nextval(nextval_arg), round(round_arg), is_used(is_used_arg)
3195   {}
3196   longlong val_int();
func_name()3197   const char *func_name() const { return "setval"; }
3198   void print(String *str, enum_query_type query_type);
get_copy(THD * thd)3199   Item *get_copy(THD *thd)
3200   { return get_item_copy<Item_func_setval>(thd, this); }
3201 };
3202 
3203 
3204 Item *get_system_var(THD *thd, enum_var_type var_type,
3205                      const LEX_CSTRING *name, const LEX_CSTRING *component);
3206 extern bool check_reserved_words(const LEX_CSTRING *name);
3207 double my_double_round(double value, longlong dec, bool dec_unsigned,
3208                        bool truncate);
3209 
3210 extern bool volatile  mqh_used;
3211 
3212 bool update_hash(user_var_entry *entry, bool set_null, void *ptr, size_t length,
3213                  Item_result type, CHARSET_INFO *cs,
3214                  bool unsigned_arg);
3215 
3216 #endif /* ITEM_FUNC_INCLUDED */
3217