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