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