1 #ifndef ITEM_FUNC_INCLUDED
2 #define ITEM_FUNC_INCLUDED
3 
4 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License, version 2.0,
8    as published by the Free Software Foundation.
9 
10    This program is also distributed with certain software (including
11    but not limited to OpenSSL) that is licensed under separate terms,
12    as designated in a particular file or component or in included license
13    documentation.  The authors of MySQL hereby grant you an additional
14    permission to link the program and your derivative works with the
15    separately licensed software that they have included with MySQL.
16 
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License, version 2.0, for more details.
21 
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
25 
26 #include "my_global.h"
27 #include "item.h"       // Item_result_field
28 #include "my_decimal.h" // string2my_decimal
29 #include "set_var.h"    // enum_var_type
30 #include "sql_udf.h"    // udf_handler
31 
32 /* Function items used by mysql */
33 
34 class PT_item_list;
35 
36 #ifdef MYSQL_SERVER
37 
38 extern void reject_geometry_args(uint arg_count, Item **args,
39                                  Item_result_field *me);
40 
41 #endif /* MYSQL_SERVER */
42 
43 void unsupported_json_comparison(size_t arg_count, Item **args,
44                                  const char *msg);
45 
46 class Item_func :public Item_result_field
47 {
48   typedef Item_result_field super;
49 protected:
50   Item **args, *tmp_arg[2];
51   /// Value used in calculation of result of const_item()
52   bool const_item_cache;
53   /*
54     Allowed numbers of columns in result (usually 1, which means scalar value)
55     0 means get this number from first argument
56   */
57   uint allowed_arg_cols;
58   /// Value used in calculation of result of used_tables()
59   table_map used_tables_cache;
60   /// Value used in calculation of result of not_null_tables()
61   table_map not_null_tables_cache;
62 public:
63   uint arg_count;
64   //bool const_item_cache;
65   // When updating Functype with new spatial functions,
66   // is_spatial_operator() should also be updated.
67   enum Functype { UNKNOWN_FUNC,EQ_FUNC,EQUAL_FUNC,NE_FUNC,LT_FUNC,LE_FUNC,
68 		  GE_FUNC,GT_FUNC,FT_FUNC,
69 		  LIKE_FUNC,ISNULL_FUNC,ISNOTNULL_FUNC,
70 		  COND_AND_FUNC, COND_OR_FUNC, XOR_FUNC,
71                   BETWEEN, IN_FUNC, MULT_EQUAL_FUNC,
72 		  INTERVAL_FUNC, ISNOTNULLTEST_FUNC,
73 		  SP_EQUALS_FUNC, SP_DISJOINT_FUNC,SP_INTERSECTS_FUNC,
74 		  SP_TOUCHES_FUNC,SP_CROSSES_FUNC,SP_WITHIN_FUNC,
75 		  SP_CONTAINS_FUNC,SP_COVEREDBY_FUNC,SP_COVERS_FUNC,
76                   SP_OVERLAPS_FUNC,
77 		  SP_STARTPOINT,SP_ENDPOINT,SP_EXTERIORRING,
78 		  SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN,
79                   NOT_FUNC, NOT_ALL_FUNC,
80                   NOW_FUNC, TRIG_COND_FUNC,
81                   SUSERVAR_FUNC, GUSERVAR_FUNC, COLLATE_FUNC,
82                   EXTRACT_FUNC, TYPECAST_FUNC, FUNC_SP, UDF_FUNC,
83                   NEG_FUNC, GSYSVAR_FUNC };
84   enum optimize_type { OPTIMIZE_NONE,OPTIMIZE_KEY,OPTIMIZE_OP, OPTIMIZE_NULL,
85                        OPTIMIZE_EQUAL };
type()86   enum Type type() const { return FUNC_ITEM; }
functype()87   virtual enum Functype functype() const   { return UNKNOWN_FUNC; }
Item_func(void)88   Item_func(void):
89     allowed_arg_cols(1), arg_count(0)
90   {
91     args= tmp_arg;
92     with_sum_func= 0;
93   }
94 
Item_func(const POS & pos)95   explicit Item_func(const POS &pos)
96     : super(pos), allowed_arg_cols(1), arg_count(0)
97   {
98     args= tmp_arg;
99   }
100 
Item_func(Item * a)101   Item_func(Item *a):
102     allowed_arg_cols(1), arg_count(1)
103   {
104     args= tmp_arg;
105     args[0]= a;
106     with_sum_func= a->with_sum_func;
107   }
Item_func(const POS & pos,Item * a)108   Item_func(const POS &pos, Item *a): super(pos),
109     allowed_arg_cols(1), arg_count(1)
110   {
111     args= tmp_arg;
112     args[0]= a;
113   }
114 
Item_func(Item * a,Item * b)115   Item_func(Item *a,Item *b):
116     allowed_arg_cols(1), arg_count(2)
117   {
118     args= tmp_arg;
119     args[0]= a; args[1]= b;
120     with_sum_func= a->with_sum_func || b->with_sum_func;
121   }
Item_func(const POS & pos,Item * a,Item * b)122   Item_func(const POS &pos, Item *a,Item *b): super(pos),
123     allowed_arg_cols(1), arg_count(2)
124   {
125     args= tmp_arg;
126     args[0]= a; args[1]= b;
127   }
128 
Item_func(Item * a,Item * b,Item * c)129   Item_func(Item *a,Item *b,Item *c):
130     allowed_arg_cols(1), arg_count(3)
131   {
132     if ((args= (Item**) sql_alloc(sizeof(Item*)*3)))
133     {
134       args[0]= a; args[1]= b; args[2]= c;
135       with_sum_func= a->with_sum_func || b->with_sum_func || c->with_sum_func;
136     }
137     else
138       arg_count= 0; // OOM
139   }
140 
Item_func(const POS & pos,Item * a,Item * b,Item * c)141   Item_func(const POS &pos, Item *a,Item *b,Item *c): super(pos),
142     allowed_arg_cols(1), arg_count(3)
143   {
144     if ((args= (Item**) sql_alloc(sizeof(Item*)*3)))
145     {
146       args[0]= a; args[1]= b; args[2]= c;
147     }
148     else
149       arg_count= 0; // OOM
150   }
151 
Item_func(Item * a,Item * b,Item * c,Item * d)152   Item_func(Item *a,Item *b,Item *c,Item *d):
153     allowed_arg_cols(1), arg_count(4)
154   {
155     if ((args= (Item**) sql_alloc(sizeof(Item*)*4)))
156     {
157       args[0]= a; args[1]= b; args[2]= c; args[3]= d;
158       with_sum_func= a->with_sum_func || b->with_sum_func ||
159 	c->with_sum_func || d->with_sum_func;
160     }
161     else
162       arg_count= 0; // OOM
163   }
Item_func(const POS & pos,Item * a,Item * b,Item * c,Item * d)164   Item_func(const POS &pos, Item *a,Item *b,Item *c,Item *d): super(pos),
165     allowed_arg_cols(1), arg_count(4)
166   {
167     if ((args= (Item**) sql_alloc(sizeof(Item*)*4)))
168     {
169       args[0]= a; args[1]= b; args[2]= c; args[3]= d;
170     }
171     else
172       arg_count= 0; // OOM
173   }
174 
Item_func(Item * a,Item * b,Item * c,Item * d,Item * e)175   Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
176     allowed_arg_cols(1), arg_count(5)
177   {
178     if ((args= (Item**) sql_alloc(sizeof(Item*)*5)))
179     {
180       args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
181       with_sum_func= a->with_sum_func || b->with_sum_func ||
182 	c->with_sum_func || d->with_sum_func || e->with_sum_func ;
183     }
184     else
185       arg_count= 0; // OOM
186   }
Item_func(const POS & pos,Item * a,Item * b,Item * c,Item * d,Item * e)187   Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item* e)
188     : super(pos), allowed_arg_cols(1), arg_count(5)
189   {
190     if ((args= (Item**) sql_alloc(sizeof(Item*)*5)))
191     {
192       args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
193     }
194     else
195       arg_count= 0; // OOM
196   }
197 
198   Item_func(List<Item> &list);
199   Item_func(const POS &pos, PT_item_list *opt_list);
200 
201   // Constructor used for Item_cond_and/or (see Item comment)
202   Item_func(THD *thd, Item_func *item);
203 
204   virtual bool itemize(Parse_context *pc, Item **res);
205 
206   bool fix_fields(THD *, Item **ref);
207   bool fix_func_arg(THD *, Item **arg);
208   virtual void fix_after_pullout(st_select_lex *parent_select,
209                                  st_select_lex *removed_select);
210   table_map used_tables() const;
211   /**
212      Returns the pseudo tables depended upon in order to evaluate this
213      function expression. The default implementation returns the empty
214      set.
215   */
get_initial_pseudo_tables()216   virtual table_map get_initial_pseudo_tables() const { return 0; }
217   table_map not_null_tables() const;
218   void update_used_tables();
set_used_tables(table_map map)219   void set_used_tables(table_map map) { used_tables_cache= map; }
set_not_null_tables(table_map map)220   void set_not_null_tables(table_map map) { not_null_tables_cache= map; }
221   bool eq(const Item *item, bool binary_cmp) const;
select_optimize()222   virtual optimize_type select_optimize() const { return OPTIMIZE_NONE; }
have_rev_func()223   virtual bool have_rev_func() const { return 0; }
key_item()224   virtual Item *key_item() const { return args[0]; }
const_item()225   virtual bool const_item() const { return const_item_cache; }
arguments()226   inline Item **arguments() const
227   { assert(argument_count() > 0); return args; }
228   /**
229     Copy arguments from list to args array
230 
231     @param list           function argument list
232     @param context_free   true: for use in context-independent
233                           constructors (Item_func(POS,...)) i.e. for use
234                           in the parser
235   */
236   void set_arguments(List<Item> &list, bool context_free);
argument_count()237   inline uint argument_count() const { return arg_count; }
remove_arguments()238   inline void remove_arguments() { arg_count=0; }
239   void split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
240                       List<Item> &fields);
241   virtual void print(String *str, enum_query_type query_type);
242   void print_op(String *str, enum_query_type query_type);
243   void print_args(String *str, uint from, enum_query_type query_type);
244   virtual void fix_num_length_and_dec();
245   void count_only_length(Item **item, uint nitems);
246   void count_real_length(Item **item, uint nitems);
247   void count_decimal_length(Item **item, uint nitems);
248   void count_datetime_length(Item **item, uint nitems);
249   bool count_string_result_length(enum_field_types field_type,
250                                   Item **item, uint nitems);
get_arg0_date(MYSQL_TIME * ltime,my_time_flags_t fuzzy_date)251   bool get_arg0_date(MYSQL_TIME *ltime, my_time_flags_t fuzzy_date)
252   {
253     return (null_value=args[0]->get_date(ltime, fuzzy_date));
254   }
get_arg0_time(MYSQL_TIME * ltime)255   inline bool get_arg0_time(MYSQL_TIME *ltime)
256   {
257     return (null_value= args[0]->get_time(ltime));
258   }
is_null()259   bool is_null() {
260     update_null_value();
261     return null_value;
262   }
263   void signal_divide_by_null();
264   void signal_invalid_argument_for_log();
265   friend class udf_handler;
tmp_table_field()266   Field *tmp_table_field() { return result_field; }
267   Field *tmp_table_field(TABLE *t_arg);
268   Item *get_tmp_table_item(THD *thd);
269 
270   my_decimal *val_decimal(my_decimal *);
271 
272   /**
273     Same as save_in_field except that special logic is added
274     to handle JSON values. If the target field has JSON type,
275     then do NOT first serialize the JSON value into a string form.
276 
277     A better solution might be to put this logic into
278     Item_func::save_in_field_inner() or even Item::save_in_field_inner().
279     But that would mean providing val_json() overrides for
280     more Item subclasses. And that feels like pulling on a
281     ball of yarn late in the release cycle for 5.7. FIXME.
282 
283     @param[out] field  The field to set the value to.
284     @retval 0         On success.
285     @retval > 0       On error.
286   */
287   virtual type_conversion_status save_possibly_as_json(Field *field,
288                                                        bool no_conversions);
289 
agg_arg_charsets(DTCollation & c,Item ** items,uint nitems,uint flags,int item_sep)290   bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems,
291                         uint flags, int item_sep)
292   {
293     return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep);
294   }
295   /*
296     Aggregate arguments for string result, e.g: CONCAT(a,b)
297     - convert to @@character_set_connection if all arguments are numbers
298     - allow DERIVATION_NONE
299   */
300   bool agg_arg_charsets_for_string_result(DTCollation &c,
301                                           Item **items, uint nitems,
302                                           int item_sep= 1)
303   {
304     return agg_item_charsets_for_string_result(c, func_name(),
305                                                items, nitems, item_sep);
306   }
307   /*
308     Aggregate arguments for comparison, e.g: a=b, a LIKE b, a RLIKE b
309     - don't convert to @@character_set_connection if all arguments are numbers
310     - don't allow DERIVATION_NONE
311   */
312   bool agg_arg_charsets_for_comparison(DTCollation &c,
313                                        Item **items, uint nitems,
314                                        int item_sep= 1)
315   {
316     return agg_item_charsets_for_comparison(c, func_name(),
317                                             items, nitems, item_sep);
318   }
319   /*
320     Aggregate arguments for string result, when some comparison
321     is involved internally, e.g: REPLACE(a,b,c)
322     - convert to @@character_set_connection if all arguments are numbers
323     - disallow DERIVATION_NONE
324   */
325   bool agg_arg_charsets_for_string_result_with_comparison(DTCollation &c,
326                                                           Item **items,
327                                                           uint nitems,
328                                                           int item_sep= 1)
329   {
330     return agg_item_charsets_for_string_result_with_comparison(c, func_name(),
331                                                                items, nitems,
332                                                                item_sep);
333   }
334   bool walk(Item_processor processor, enum_walk walk, uchar *arg);
335   Item *transform(Item_transformer transformer, uchar *arg);
336   Item* compile(Item_analyzer analyzer, uchar **arg_p,
337                 Item_transformer transformer, uchar *arg_t);
338   void traverse_cond(Cond_traverser traverser,
339                      void * arg, traverse_order order);
fix_result(double value)340   inline double fix_result(double value)
341   {
342     if (my_isfinite(value))
343       return value;
344     null_value=1;
345     return 0.0;
346   }
raise_numeric_overflow(const char * type_name)347   inline void raise_numeric_overflow(const char *type_name)
348   {
349     char buf[256];
350     String str(buf, sizeof(buf), system_charset_info);
351     str.length(0);
352     print(&str, QT_NO_DATA_EXPANSION);
353     my_error(ER_DATA_OUT_OF_RANGE, MYF(0), type_name, str.c_ptr_safe());
354   }
raise_float_overflow()355   inline double raise_float_overflow()
356   {
357     raise_numeric_overflow("DOUBLE");
358     return 0.0;
359   }
raise_integer_overflow()360   inline longlong raise_integer_overflow()
361   {
362     raise_numeric_overflow(unsigned_flag ? "BIGINT UNSIGNED": "BIGINT");
363     return 0;
364   }
raise_decimal_overflow()365   inline int raise_decimal_overflow()
366   {
367     raise_numeric_overflow("DECIMAL");
368     return E_DEC_OVERFLOW;
369   }
370   /**
371      Throw an error if the input double number is not finite, i.e. is either
372      +/-INF or NAN.
373   */
check_float_overflow(double value)374   inline double check_float_overflow(double value)
375   {
376     return my_isfinite(value) ? value : raise_float_overflow();
377   }
378   /**
379     Throw an error if the input BIGINT value represented by the
380     (longlong value, bool unsigned flag) pair cannot be returned by the
381     function, i.e. is not compatible with this Item's unsigned_flag.
382   */
check_integer_overflow(longlong value,bool val_unsigned)383   inline longlong check_integer_overflow(longlong value, bool val_unsigned)
384   {
385     if ((unsigned_flag && !val_unsigned && value < 0) ||
386         (!unsigned_flag && val_unsigned &&
387          (ulonglong) value > (ulonglong) LLONG_MAX))
388       return raise_integer_overflow();
389     return value;
390   }
391   /**
392      Throw an error if the error code of a DECIMAL operation is E_DEC_OVERFLOW.
393   */
check_decimal_overflow(int error)394   inline int check_decimal_overflow(int error)
395   {
396     return (error == E_DEC_OVERFLOW) ? raise_decimal_overflow() : error;
397   }
398 
has_timestamp_args()399   bool has_timestamp_args()
400   {
401     assert(fixed == TRUE);
402     for (uint i= 0; i < arg_count; i++)
403     {
404       if (args[i]->type() == Item::FIELD_ITEM &&
405           args[i]->field_type() == MYSQL_TYPE_TIMESTAMP)
406         return TRUE;
407     }
408     return FALSE;
409   }
410 
has_date_args()411   bool has_date_args()
412   {
413     assert(fixed == TRUE);
414     for (uint i= 0; i < arg_count; i++)
415     {
416       if (args[i]->type() == Item::FIELD_ITEM &&
417           (args[i]->field_type() == MYSQL_TYPE_DATE ||
418            args[i]->field_type() == MYSQL_TYPE_DATETIME))
419         return TRUE;
420     }
421     return FALSE;
422   }
423 
has_time_args()424   bool has_time_args()
425   {
426     assert(fixed == TRUE);
427     for (uint i= 0; i < arg_count; i++)
428     {
429       if (args[i]->type() == Item::FIELD_ITEM &&
430           (args[i]->field_type() == MYSQL_TYPE_TIME ||
431            args[i]->field_type() == MYSQL_TYPE_DATETIME))
432         return TRUE;
433     }
434     return FALSE;
435   }
436 
has_datetime_args()437   bool has_datetime_args()
438   {
439     assert(fixed == TRUE);
440     for (uint i= 0; i < arg_count; i++)
441     {
442       if (args[i]->type() == Item::FIELD_ITEM &&
443           args[i]->field_type() == MYSQL_TYPE_DATETIME)
444         return TRUE;
445     }
446     return FALSE;
447   }
448 
449   /*
450     We assume the result of any function that has a TIMESTAMP argument to be
451     timezone-dependent, since a TIMESTAMP value in both numeric and string
452     contexts is interpreted according to the current timezone.
453     The only exception is UNIX_TIMESTAMP() which returns the internal
454     representation of a TIMESTAMP argument verbatim, and thus does not depend on
455     the timezone.
456    */
check_valid_arguments_processor(uchar * bool_arg)457   virtual bool check_valid_arguments_processor(uchar *bool_arg)
458   {
459     return has_timestamp_args();
460   }
461 
find_function_processor(uchar * arg)462   virtual bool find_function_processor (uchar *arg)
463   {
464     return functype() == *(Functype *) arg;
465   }
466   virtual Item *gc_subst_transformer(uchar *arg);
467 
468   /**
469     Does essentially the same as THD::change_item_tree, plus
470     maintains any necessary any invariants.
471   */
472   virtual void replace_argument(THD *thd, Item **oldpp, Item *newp);
473 
474 protected:
475   /**
476     Whether or not an item should contribute to the filtering effect
477     (@see get_filtering_effect()). First it verifies that table
478     requirements are satisfied as follows:
479 
480      1) The item must refer to a field in 'filter_for_table' in some
481         way. This reference may be indirect through any number of
482         intermediate items. For example, this item may be an
483         Item_cond_and which refers to an Item_func_eq which refers to
484         the field.
485      2) The item must not refer to other tables than those already
486         read and the table in 'filter_for_table'
487 
488     Then it contines to other properties as follows:
489 
490     Item_funcs represent "<operand1> OP <operand2> [OP ...]". If the
491     Item_func is to contribute to the filtering effect, then
492 
493     1) one of the operands must be a field from 'filter_for_table' that is not
494        in 'fields_to_ignore', and
495     2) depending on the Item_func type filtering effect is calculated
496        for, one or all [1] of the other operand(s) must be an available
497        value, i.e.:
498        - a constant, or
499        - a constant subquery, or
500        - a field value read from a table in 'read_tables', or
501        - a second field in 'filter_for_table', or
502        - a function that only refers to constants or tables in
503          'read_tables', or
504        - special case: an implicit value like NULL in the case of
505          "field IS NULL". Such Item_funcs have arg_count==1.
506 
507     [1] "At least one" for multiple equality (X = Y = Z = ...), "all"
508     for the rest (e.g. BETWEEN)
509 
510     @param read_tables       Tables earlier in the join sequence.
511                              Predicates for table 'filter_for_table' that
512                              rely on values from these tables can be part of
513                              the filter effect.
514     @param filter_for_table  The table we are calculating filter effect for
515 
516     @return Item_field that participates in the predicate if none of the
517             requirements are broken, NULL otherwise
518 
519     @note: This function only applies to items doing comparison, i.e.
520     boolean predicates. Unfortunately, some of those items do not
521     inherit from Item_bool_func so the member function has to be
522     placed in Item_func.
523   */
524   const Item_field*
525     contributes_to_filter(table_map read_tables,
526                           table_map filter_for_table,
527                           const MY_BITMAP *fields_to_ignore) const;
528   /**
529     Named parameters are allowed in a parameter list
530 
531     The syntax to name parameters in a function call is as follow:
532     <code>foo(expr AS named, expr named, expr AS "named", expr "named")</code>
533     where "AS" is optional.
534     Only UDF function support that syntax.
535 
536     @return true if the function item can have named parameters
537   */
may_have_named_parameters()538   virtual bool may_have_named_parameters() const { return false; }
539 };
540 
541 #ifdef MYSQL_SERVER
542 
543 class Item_real_func :public Item_func
544 {
545 public:
Item_real_func()546   Item_real_func() :Item_func() { collation.set_numeric(); }
547   explicit
Item_real_func(const POS & pos)548   Item_real_func(const POS &pos) :Item_func(pos) { collation.set_numeric(); }
549 
Item_real_func(Item * a)550   Item_real_func(Item *a) :Item_func(a) { collation.set_numeric(); }
Item_real_func(const POS & pos,Item * a)551   Item_real_func(const POS &pos, Item *a) :Item_func(pos, a)
552   { collation.set_numeric(); }
553 
Item_real_func(Item * a,Item * b)554   Item_real_func(Item *a,Item *b) :Item_func(a,b) { collation.set_numeric(); }
Item_real_func(const POS & pos,Item * a,Item * b)555   Item_real_func(const POS &pos, Item *a,Item *b) :Item_func(pos, a,b)
556   { collation.set_numeric(); }
557 
Item_real_func(List<Item> & list)558   Item_real_func(List<Item> &list) :Item_func(list) { collation.set_numeric(); }
Item_real_func(const POS & pos,PT_item_list * list)559   Item_real_func(const POS &pos, PT_item_list *list) :Item_func(pos, list)
560   { collation.set_numeric(); }
561 
562   String *val_str(String*str);
563   my_decimal *val_decimal(my_decimal *decimal_value);
val_int()564   longlong val_int()
565   { assert(fixed == 1); return (longlong) rint(val_real()); }
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate)566   bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
567   {
568     return get_date_from_real(ltime, fuzzydate);
569   }
get_time(MYSQL_TIME * ltime)570   bool get_time(MYSQL_TIME *ltime)
571   {
572     return get_time_from_real(ltime);
573   }
result_type()574   enum Item_result result_type () const { return REAL_RESULT; }
fix_length_and_dec()575   void fix_length_and_dec()
576   { decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
577 };
578 
579 
580 class Item_func_numhybrid: public Item_func
581 {
582 protected:
583   Item_result hybrid_type;
584 public:
Item_func_numhybrid(Item * a)585   Item_func_numhybrid(Item *a) :Item_func(a), hybrid_type(REAL_RESULT)
586   { collation.set_numeric(); }
Item_func_numhybrid(const POS & pos,Item * a)587   Item_func_numhybrid(const POS &pos, Item *a)
588     :Item_func(pos, a), hybrid_type(REAL_RESULT)
589   { collation.set_numeric(); }
590 
Item_func_numhybrid(Item * a,Item * b)591   Item_func_numhybrid(Item *a,Item *b)
592     :Item_func(a,b), hybrid_type(REAL_RESULT)
593   { collation.set_numeric(); }
Item_func_numhybrid(const POS & pos,Item * a,Item * b)594   Item_func_numhybrid(const POS &pos, Item *a,Item *b)
595     :Item_func(pos, a,b), hybrid_type(REAL_RESULT)
596   { collation.set_numeric(); }
597 
Item_func_numhybrid(List<Item> & list)598   Item_func_numhybrid(List<Item> &list)
599     :Item_func(list), hybrid_type(REAL_RESULT)
600   { collation.set_numeric(); }
Item_func_numhybrid(const POS & pos,PT_item_list * list)601   Item_func_numhybrid(const POS &pos, PT_item_list *list)
602     :Item_func(pos, list), hybrid_type(REAL_RESULT)
603   { collation.set_numeric(); }
604 
result_type()605   enum Item_result result_type () const { return hybrid_type; }
606   void fix_length_and_dec();
607   void fix_num_length_and_dec();
608   virtual void find_num_type()= 0; /* To be called from fix_length_and_dec */
609 
610   double val_real();
611   longlong val_int();
612   my_decimal *val_decimal(my_decimal *);
613   String *val_str(String*str);
614   bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate);
615   bool get_time(MYSQL_TIME *ltime);
616   /**
617      @brief Performs the operation that this functions implements when the
618      result type is INT.
619 
620      @return The result of the operation.
621   */
622   virtual longlong int_op()= 0;
623 
624   /**
625      @brief Performs the operation that this functions implements when the
626      result type is REAL.
627 
628      @return The result of the operation.
629   */
630   virtual double real_op()= 0;
631 
632   /**
633      @brief Performs the operation that this functions implements when the
634      result type is DECIMAL.
635 
636      @param A pointer where the DECIMAL value will be allocated.
637      @return
638        - 0 If the result is NULL
639        - The same pointer it was given, with the area initialized to the
640          result of the operation.
641   */
642   virtual my_decimal *decimal_op(my_decimal *)= 0;
643 
644   /**
645      @brief Performs the operation that this functions implements when the
646      result type is a string type.
647 
648      @return The result of the operation.
649   */
650   virtual String *str_op(String *)= 0;
651   /**
652      @brief Performs the operation that this functions implements when the
653      result type is MYSQL_TYPE_DATE or MYSQL_TYPE_DATETIME.
654 
655      @return The result of the operation.
656   */
657   virtual bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)= 0;
658   virtual bool time_op(MYSQL_TIME *ltime)= 0;
is_null()659   bool is_null() { update_null_value(); return null_value; }
660 };
661 
662 /* function where type of result detected by first argument */
663 class Item_func_num1: public Item_func_numhybrid
664 {
665 public:
Item_func_num1(Item * a)666   Item_func_num1(Item *a) :Item_func_numhybrid(a) {}
Item_func_num1(const POS & pos,Item * a)667   Item_func_num1(const POS &pos, Item *a) :Item_func_numhybrid(pos, a) {}
668 
Item_func_num1(Item * a,Item * b)669   Item_func_num1(Item *a, Item *b) :Item_func_numhybrid(a, b) {}
Item_func_num1(const POS & pos,Item * a,Item * b)670   Item_func_num1(const POS &pos, Item *a, Item *b)
671     :Item_func_numhybrid(pos, a, b)
672   {}
673 
674   void fix_num_length_and_dec();
675   void find_num_type();
str_op(String * str)676   String *str_op(String *str) { assert(0); return 0; }
date_op(MYSQL_TIME * ltime,my_time_flags_t fuzzydate)677   bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
678   { assert(0); return 0; }
time_op(MYSQL_TIME * ltime)679   bool time_op(MYSQL_TIME *ltime)
680   { assert(0); return 0; }
681 };
682 
683 
684 /* Base class for operations like '+', '-', '*' */
685 class Item_num_op :public Item_func_numhybrid
686 {
687  public:
Item_num_op(Item * a,Item * b)688   Item_num_op(Item *a,Item *b) :Item_func_numhybrid(a, b) {}
Item_num_op(const POS & pos,Item * a,Item * b)689   Item_num_op(const POS &pos, Item *a,Item *b) :Item_func_numhybrid(pos, a, b)
690   {}
691 
692   virtual void result_precision()= 0;
693 
print(String * str,enum_query_type query_type)694   virtual inline void print(String *str, enum_query_type query_type)
695   {
696     print_op(str, query_type);
697   }
698 
699   void find_num_type();
str_op(String * str)700   String *str_op(String *str) { assert(0); return 0; }
date_op(MYSQL_TIME * ltime,my_time_flags_t fuzzydate)701   bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
702   { assert(0); return 0; }
time_op(MYSQL_TIME * ltime)703   bool time_op(MYSQL_TIME *ltime)
704   { assert(0); return 0; }
705 };
706 
707 #endif /* MYSQL_SERVER */
708 
709 class Item_int_func :public Item_func
710 {
711 public:
Item_int_func()712   Item_int_func() :Item_func()
713   { collation.set_numeric(); fix_char_length(21); }
Item_int_func(const POS & pos)714   explicit Item_int_func(const POS &pos) :Item_func(pos)
715   { collation.set_numeric(); fix_char_length(21); }
716 
Item_int_func(Item * a)717   Item_int_func(Item *a) :Item_func(a)
718   { collation.set_numeric(); fix_char_length(21); }
Item_int_func(const POS & pos,Item * a)719   Item_int_func(const POS &pos, Item *a) :Item_func(pos, a)
720   { collation.set_numeric(); fix_char_length(21); }
721 
Item_int_func(Item * a,Item * b)722   Item_int_func(Item *a,Item *b) :Item_func(a,b)
723   { collation.set_numeric(); fix_char_length(21); }
Item_int_func(const POS & pos,Item * a,Item * b)724   Item_int_func(const POS &pos, Item *a,Item *b) :Item_func(pos, a,b)
725   { collation.set_numeric(); fix_char_length(21); }
726 
Item_int_func(Item * a,Item * b,Item * c)727   Item_int_func(Item *a,Item *b,Item *c) :Item_func(a,b,c)
728   { collation.set_numeric(); fix_char_length(21); }
Item_int_func(const POS & pos,Item * a,Item * b,Item * c)729   Item_int_func(const POS &pos, Item *a,Item *b,Item *c) :Item_func(pos, a,b,c)
730   { collation.set_numeric(); fix_char_length(21); }
731 
Item_int_func(Item * a,Item * b,Item * c,Item * d)732   Item_int_func(Item *a, Item *b, Item *c, Item *d): Item_func(a,b,c,d)
733   { collation.set_numeric(); fix_char_length(21); }
Item_int_func(const POS & pos,Item * a,Item * b,Item * c,Item * d)734   Item_int_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
735     :Item_func(pos,a,b,c,d)
736   { collation.set_numeric(); fix_char_length(21); }
737 
Item_int_func(List<Item> & list)738   Item_int_func(List<Item> &list) :Item_func(list)
739   { collation.set_numeric(); fix_char_length(21); }
Item_int_func(const POS & pos,PT_item_list * opt_list)740   Item_int_func(const POS &pos, PT_item_list *opt_list)
741     :Item_func(pos, opt_list)
742   { collation.set_numeric(); fix_char_length(21); }
743 
Item_int_func(THD * thd,Item_int_func * item)744   Item_int_func(THD *thd, Item_int_func *item) :Item_func(thd, item)
745   { collation.set_numeric(); }
746   double val_real();
747   String *val_str(String*str);
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate)748   bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
749   {
750     return get_date_from_int(ltime, fuzzydate);
751   }
get_time(MYSQL_TIME * ltime)752   bool get_time(MYSQL_TIME *ltime)
753   {
754     return get_time_from_int(ltime);
755   }
result_type()756   enum Item_result result_type () const { return INT_RESULT; }
fix_length_and_dec()757   void fix_length_and_dec() {}
758 };
759 
760 #ifdef MYSQL_SERVER
761 
762 class Item_func_connection_id :public Item_int_func
763 {
764   typedef Item_int_func super;
765 
766   longlong value;
767 
768 public:
Item_func_connection_id(const POS & pos)769   Item_func_connection_id(const POS &pos) :Item_int_func(pos) {}
770 
771   virtual bool itemize(Parse_context *pc, Item **res);
func_name()772   const char *func_name() const { return "connection_id"; }
773   void fix_length_and_dec();
774   bool fix_fields(THD *thd, Item **ref);
val_int()775   longlong val_int() { assert(fixed == 1); return value; }
check_gcol_func_processor(uchar * int_arg)776   bool check_gcol_func_processor(uchar *int_arg) { return true;}
777 };
778 
779 
780 class Item_func_signed :public Item_int_func
781 {
782 public:
Item_func_signed(const POS & pos,Item * a)783   Item_func_signed(const POS &pos, Item *a) :Item_int_func(pos, a)
784   {
785     unsigned_flag= 0;
786   }
func_name()787   const char *func_name() const { return "cast_as_signed"; }
788   longlong val_int();
789   longlong val_int_from_str(int *error);
790   void fix_length_and_dec();
791   virtual void print(String *str, enum_query_type query_type);
decimal_precision()792   uint decimal_precision() const { return args[0]->decimal_precision(); }
functype()793   enum Functype functype() const { return TYPECAST_FUNC; }
794 };
795 
796 
797 class Item_func_unsigned :public Item_func_signed
798 {
799 public:
Item_func_unsigned(const POS & pos,Item * a)800   Item_func_unsigned(const POS &pos, Item *a) :Item_func_signed(pos, a)
801   {
802     unsigned_flag= 1;
803   }
func_name()804   const char *func_name() const { return "cast_as_unsigned"; }
805   longlong val_int();
806   virtual void print(String *str, enum_query_type query_type);
functype()807   enum Functype functype() const { return TYPECAST_FUNC; }
808 };
809 
810 
811 class Item_decimal_typecast :public Item_func
812 {
813   my_decimal decimal_value;
814 public:
Item_decimal_typecast(const POS & pos,Item * a,int len,int dec)815   Item_decimal_typecast(const POS &pos, Item *a, int len, int dec)
816     :Item_func(pos, a)
817   {
818     decimals= dec;
819     collation.set_numeric();
820     fix_char_length(my_decimal_precision_to_length_no_truncation(len, dec,
821                                                                  unsigned_flag));
822   }
823   String *val_str(String *str);
824   double val_real();
825   longlong val_int();
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate)826   bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
827   {
828     return get_date_from_decimal(ltime, fuzzydate);
829   }
get_time(MYSQL_TIME * ltime)830   bool get_time(MYSQL_TIME *ltime)
831   {
832     return get_time_from_decimal(ltime);
833   }
834   my_decimal *val_decimal(my_decimal*);
result_type()835   enum Item_result result_type () const { return DECIMAL_RESULT; }
field_type()836   enum_field_types field_type() const { return MYSQL_TYPE_NEWDECIMAL; }
fix_length_and_dec()837   void fix_length_and_dec() {};
func_name()838   const char *func_name() const { return "decimal_typecast"; }
functype()839   enum Functype functype() const { return TYPECAST_FUNC; }
840   virtual void print(String *str, enum_query_type query_type);
841 };
842 
843 
844 class Item_func_additive_op :public Item_num_op
845 {
846 public:
Item_func_additive_op(Item * a,Item * b)847   Item_func_additive_op(Item *a,Item *b) :Item_num_op(a,b) {}
Item_func_additive_op(const POS & pos,Item * a,Item * b)848   Item_func_additive_op(const POS &pos, Item *a,Item *b) :Item_num_op(pos, a,b)
849   {}
850 
851   void result_precision();
check_partition_func_processor(uchar * int_arg)852   bool check_partition_func_processor(uchar *int_arg) {return false;}
check_gcol_func_processor(uchar * int_arg)853   bool check_gcol_func_processor(uchar *int_arg) { return false;}
854 };
855 
856 
857 class Item_func_plus :public Item_func_additive_op
858 {
859 public:
Item_func_plus(Item * a,Item * b)860   Item_func_plus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
Item_func_plus(const POS & pos,Item * a,Item * b)861   Item_func_plus(const POS &pos, Item *a,Item *b)
862     :Item_func_additive_op(pos, a, b)
863   {}
864 
func_name()865   const char *func_name() const { return "+"; }
866   longlong int_op();
867   double real_op();
868   my_decimal *decimal_op(my_decimal *);
869 };
870 
871 class Item_func_minus :public Item_func_additive_op
872 {
873 public:
Item_func_minus(Item * a,Item * b)874   Item_func_minus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
Item_func_minus(const POS & pos,Item * a,Item * b)875   Item_func_minus(const POS &pos, Item *a,Item *b)
876     :Item_func_additive_op(pos, a, b)
877   {}
878 
func_name()879   const char *func_name() const { return "-"; }
880   longlong int_op();
881   double real_op();
882   my_decimal *decimal_op(my_decimal *);
883   void fix_length_and_dec();
884 };
885 
886 
887 class Item_func_mul :public Item_num_op
888 {
889 public:
Item_func_mul(Item * a,Item * b)890   Item_func_mul(Item *a,Item *b) :Item_num_op(a,b) {}
Item_func_mul(const POS & pos,Item * a,Item * b)891   Item_func_mul(const POS &pos, Item *a,Item *b) :Item_num_op(pos, a,b) {}
892 
func_name()893   const char *func_name() const { return "*"; }
894   longlong int_op();
895   double real_op();
896   my_decimal *decimal_op(my_decimal *);
897   void result_precision();
check_partition_func_processor(uchar * int_arg)898   bool check_partition_func_processor(uchar *int_arg) {return false;}
check_gcol_func_processor(uchar * int_arg)899   bool check_gcol_func_processor(uchar *int_arg) { return false;}
900 };
901 
902 
903 class Item_func_div :public Item_num_op
904 {
905 public:
906   uint prec_increment;
Item_func_div(const POS & pos,Item * a,Item * b)907   Item_func_div(const POS &pos, Item *a,Item *b) :Item_num_op(pos, a,b) {}
int_op()908   longlong int_op() { assert(0); return 0; }
909   double real_op();
910   my_decimal *decimal_op(my_decimal *);
func_name()911   const char *func_name() const { return "/"; }
912   void fix_length_and_dec();
913   void result_precision();
914 };
915 
916 
917 class Item_func_int_div :public Item_int_func
918 {
919 public:
Item_func_int_div(Item * a,Item * b)920   Item_func_int_div(Item *a,Item *b) :Item_int_func(a,b)
921   {}
Item_func_int_div(const POS & pos,Item * a,Item * b)922   Item_func_int_div(const POS &pos, Item *a,Item *b) :Item_int_func(pos, a,b)
923   {}
924   longlong val_int();
func_name()925   const char *func_name() const { return "DIV"; }
926   void fix_length_and_dec();
927 
print(String * str,enum_query_type query_type)928   virtual inline void print(String *str, enum_query_type query_type)
929   {
930     print_op(str, query_type);
931   }
932 
check_partition_func_processor(uchar * int_arg)933   bool check_partition_func_processor(uchar *int_arg) {return false;}
check_gcol_func_processor(uchar * int_arg)934   bool check_gcol_func_processor(uchar *int_arg) { return false;}
935 };
936 
937 
938 class Item_func_mod :public Item_num_op
939 {
940 public:
Item_func_mod(Item * a,Item * b)941   Item_func_mod(Item *a,Item *b) :Item_num_op(a,b) {}
Item_func_mod(const POS & pos,Item * a,Item * b)942   Item_func_mod(const POS &pos, Item *a,Item *b) :Item_num_op(pos, a,b) {}
943 
944   longlong int_op();
945   double real_op();
946   my_decimal *decimal_op(my_decimal *);
func_name()947   const char *func_name() const { return "%"; }
948   void result_precision();
949   void fix_length_and_dec();
check_partition_func_processor(uchar * int_arg)950   bool check_partition_func_processor(uchar *int_arg) {return false;}
check_gcol_func_processor(uchar * int_arg)951   bool check_gcol_func_processor(uchar *int_arg) { return false;}
952 };
953 
954 
955 class Item_func_neg :public Item_func_num1
956 {
957 public:
Item_func_neg(Item * a)958   Item_func_neg(Item *a) :Item_func_num1(a) {}
Item_func_neg(const POS & pos,Item * a)959   Item_func_neg(const POS &pos, Item *a) :Item_func_num1(pos, a) {}
960 
961   double real_op();
962   longlong int_op();
963   my_decimal *decimal_op(my_decimal *);
func_name()964   const char *func_name() const { return "-"; }
functype()965   enum Functype functype() const   { return NEG_FUNC; }
966   void fix_length_and_dec();
967   void fix_num_length_and_dec();
decimal_precision()968   uint decimal_precision() const { return args[0]->decimal_precision(); }
check_partition_func_processor(uchar * int_arg)969   bool check_partition_func_processor(uchar *int_arg) {return false;}
check_gcol_func_processor(uchar * int_arg)970   bool check_gcol_func_processor(uchar *int_arg) { return false;}
971 };
972 
973 
974 class Item_func_abs :public Item_func_num1
975 {
976 public:
Item_func_abs(const POS & pos,Item * a)977   Item_func_abs(const POS &pos, Item *a) :Item_func_num1(pos, a) {}
978   double real_op();
979   longlong int_op();
980   my_decimal *decimal_op(my_decimal *);
func_name()981   const char *func_name() const { return "abs"; }
982   void fix_length_and_dec();
check_partition_func_processor(uchar * int_arg)983   bool check_partition_func_processor(uchar *int_arg) {return false;}
check_gcol_func_processor(uchar * int_arg)984   bool check_gcol_func_processor(uchar *int_arg) { return false;}
985 };
986 
987 
988 /**
989   This is a superclass for Item_func_longfromgeohash and
990   Item_func_latfromgeohash, since they share almost all code.
991 */
992 class Item_func_latlongfromgeohash :public Item_real_func
993 {
994 private:
995   /**
996    The lower limit for latitude output value. Normally, this will be
997    set to -90.0.
998   */
999   const double lower_latitude;
1000 
1001   /**
1002    The upper limit for latitude output value. Normally, this will be
1003    set to 90.0.
1004   */
1005   const double upper_latitude;
1006 
1007   /**
1008    The lower limit for longitude output value. Normally, this will
1009    be set to -180.0.
1010   */
1011   const double lower_longitude;
1012 
1013   /**
1014    The upper limit for longitude output value. Normally, this will
1015    be set to 180.0.
1016   */
1017   const double upper_longitude;
1018 
1019   /**
1020    If this is set to TRUE the algorithm will start decoding on the first bit,
1021    which decodes a longitude value. If it is FALSE, it will start on the
1022    second bit which decodes a latitude value.
1023   */
1024   const bool start_on_even_bit;
1025 public:
Item_func_latlongfromgeohash(const POS & pos,Item * a,double lower_latitude,double upper_latitude,double lower_longitude,double upper_longitude,bool start_on_even_bit_arg)1026   Item_func_latlongfromgeohash(const POS &pos, Item *a,
1027                                double lower_latitude, double upper_latitude,
1028                                double lower_longitude, double upper_longitude,
1029                                bool start_on_even_bit_arg)
1030     :Item_real_func(pos, a), lower_latitude(lower_latitude),
1031     upper_latitude(upper_latitude), lower_longitude(lower_longitude),
1032     upper_longitude(upper_longitude), start_on_even_bit(start_on_even_bit_arg)
1033   {}
1034   double val_real();
1035   void fix_length_and_dec();
1036   bool fix_fields(THD *thd, Item **ref);
1037   static bool decode_geohash(String *geohash, double upper_latitude,
1038                              double lower_latitude, double upper_longitude,
1039                              double lower_longitude, double *result_latitude,
1040                              double *result_longitude);
1041   static double round_latlongitude(double latlongitude, double error_range,
1042                                    double lower_limit, double upper_limit);
1043   static bool check_geohash_argument_valid_type(Item *item);
1044 };
1045 
1046 
1047 /**
1048   This handles the <double> = ST_LATFROMGEOHASH(<string>) funtion.
1049   It returns the latitude-part of a geohash, in the range of [-90, 90].
1050 */
1051 class Item_func_latfromgeohash :public Item_func_latlongfromgeohash
1052 {
1053 public:
Item_func_latfromgeohash(const POS & pos,Item * a)1054   Item_func_latfromgeohash(const POS &pos, Item *a)
1055     :Item_func_latlongfromgeohash(pos, a, -90.0, 90.0, -180.0, 180.0, false)
1056   {}
1057 
func_name()1058   const char *func_name() const { return "ST_LATFROMGEOHASH"; }
1059 };
1060 
1061 
1062 /**
1063   This handles the <double> = ST_LONGFROMGEOHASH(<string>) funtion.
1064   It returns the longitude-part of a geohash, in the range of [-180, 180].
1065 */
1066 class Item_func_longfromgeohash :public Item_func_latlongfromgeohash
1067 {
1068 public:
Item_func_longfromgeohash(const POS & pos,Item * a)1069   Item_func_longfromgeohash(const POS &pos, Item *a)
1070     :Item_func_latlongfromgeohash(pos, a, -90.0, 90.0, -180.0, 180.0, true)
1071   {}
1072 
func_name()1073   const char *func_name() const { return "ST_LONGFROMGEOHASH"; }
1074 };
1075 
1076 
1077 // A class to handle logarithmic and trigonometric functions
1078 
1079 class Item_dec_func :public Item_real_func
1080 {
1081  public:
Item_dec_func(Item * a)1082   Item_dec_func(Item *a) :Item_real_func(a) {}
Item_dec_func(const POS & pos,Item * a)1083   Item_dec_func(const POS &pos, Item *a) :Item_real_func(pos, a) {}
1084 
Item_dec_func(const POS & pos,Item * a,Item * b)1085   Item_dec_func(const POS &pos, Item *a,Item *b) :Item_real_func(pos, a,b) {}
1086   void fix_length_and_dec();
1087 };
1088 
1089 class Item_func_exp :public Item_dec_func
1090 {
1091 public:
Item_func_exp(const POS & pos,Item * a)1092   Item_func_exp(const POS &pos, Item *a) :Item_dec_func(pos, a) {}
1093   double val_real();
func_name()1094   const char *func_name() const { return "exp"; }
1095 };
1096 
1097 
1098 class Item_func_ln :public Item_dec_func
1099 {
1100 public:
Item_func_ln(const POS & pos,Item * a)1101   Item_func_ln(const POS &pos, Item *a) :Item_dec_func(pos, a) {}
1102   double val_real();
func_name()1103   const char *func_name() const { return "ln"; }
1104 };
1105 
1106 
1107 class Item_func_log :public Item_dec_func
1108 {
1109 public:
Item_func_log(const POS & pos,Item * a)1110   Item_func_log(const POS &pos, Item *a) :Item_dec_func(pos, a) {}
Item_func_log(const POS & pos,Item * a,Item * b)1111   Item_func_log(const POS &pos, Item *a, Item *b) :Item_dec_func(pos, a, b) {}
1112   double val_real();
func_name()1113   const char *func_name() const { return "log"; }
1114 };
1115 
1116 
1117 class Item_func_log2 :public Item_dec_func
1118 {
1119 public:
Item_func_log2(const POS & pos,Item * a)1120   Item_func_log2(const POS &pos, Item *a) :Item_dec_func(pos, a) {}
1121   double val_real();
func_name()1122   const char *func_name() const { return "log2"; }
1123 };
1124 
1125 
1126 class Item_func_log10 :public Item_dec_func
1127 {
1128 public:
Item_func_log10(const POS & pos,Item * a)1129   Item_func_log10(const POS &pos, Item *a) :Item_dec_func(pos, a) {}
1130   double val_real();
func_name()1131   const char *func_name() const { return "log10"; }
1132 };
1133 
1134 
1135 class Item_func_sqrt :public Item_dec_func
1136 {
1137 public:
Item_func_sqrt(const POS & pos,Item * a)1138   Item_func_sqrt(const POS &pos, Item *a) :Item_dec_func(pos, a) {}
1139   double val_real();
func_name()1140   const char *func_name() const { return "sqrt"; }
1141 };
1142 
1143 
1144 class Item_func_pow :public Item_dec_func
1145 {
1146 public:
Item_func_pow(const POS & pos,Item * a,Item * b)1147   Item_func_pow(const POS &pos, Item *a, Item *b) :Item_dec_func(pos, a, b) {}
1148   double val_real();
func_name()1149   const char *func_name() const { return "pow"; }
1150 };
1151 
1152 
1153 class Item_func_acos :public Item_dec_func
1154 {
1155 public:
Item_func_acos(const POS & pos,Item * a)1156   Item_func_acos(const POS &pos, Item *a) :Item_dec_func(pos, a) {}
1157   double val_real();
func_name()1158   const char *func_name() const { return "acos"; }
1159 };
1160 
1161 class Item_func_asin :public Item_dec_func
1162 {
1163 public:
Item_func_asin(const POS & pos,Item * a)1164   Item_func_asin(const POS &pos, Item *a) :Item_dec_func(pos, a) {}
1165   double val_real();
func_name()1166   const char *func_name() const { return "asin"; }
1167 };
1168 
1169 class Item_func_atan :public Item_dec_func
1170 {
1171 public:
Item_func_atan(const POS & pos,Item * a)1172   Item_func_atan(const POS &pos, Item *a) :Item_dec_func(pos, a) {}
Item_func_atan(const POS & pos,Item * a,Item * b)1173   Item_func_atan(const POS &pos, Item *a, Item *b) :Item_dec_func(pos, a, b) {}
1174   double val_real();
func_name()1175   const char *func_name() const { return "atan"; }
1176 };
1177 
1178 class Item_func_cos :public Item_dec_func
1179 {
1180 public:
Item_func_cos(const POS & pos,Item * a)1181   Item_func_cos(const POS &pos, Item *a) :Item_dec_func(pos, a) {}
1182   double val_real();
func_name()1183   const char *func_name() const { return "cos"; }
1184 };
1185 
1186 class Item_func_sin :public Item_dec_func
1187 {
1188 public:
Item_func_sin(const POS & pos,Item * a)1189   Item_func_sin(const POS &pos, Item *a) :Item_dec_func(pos, a) {}
1190   double val_real();
func_name()1191   const char *func_name() const { return "sin"; }
1192 };
1193 
1194 class Item_func_tan :public Item_dec_func
1195 {
1196 public:
Item_func_tan(const POS & pos,Item * a)1197   Item_func_tan(const POS &pos, Item *a) :Item_dec_func(pos, a) {}
1198   double val_real();
func_name()1199   const char *func_name() const { return "tan"; }
1200 };
1201 
1202 class Item_func_cot :public Item_dec_func
1203 {
1204 public:
Item_func_cot(const POS & pos,Item * a)1205   Item_func_cot(const POS &pos, Item *a) :Item_dec_func(pos, a) {}
1206   double val_real();
func_name()1207   const char *func_name() const { return "cot"; }
1208 };
1209 
1210 class Item_func_integer :public Item_int_func
1211 {
1212 public:
Item_func_integer(Item * a)1213   inline Item_func_integer(Item *a) :Item_int_func(a) {}
1214   void fix_length_and_dec();
1215 };
1216 
1217 
1218 class Item_func_int_val :public Item_func_num1
1219 {
1220 public:
Item_func_int_val(Item * a)1221   Item_func_int_val(Item *a) :Item_func_num1(a) {}
Item_func_int_val(const POS & pos,Item * a)1222   Item_func_int_val(const POS &pos, Item *a) :Item_func_num1(pos, a) {}
1223   void fix_num_length_and_dec();
1224   void find_num_type();
1225 };
1226 
1227 
1228 class Item_func_ceiling :public Item_func_int_val
1229 {
1230 public:
Item_func_ceiling(Item * a)1231   Item_func_ceiling(Item *a) :Item_func_int_val(a) {}
Item_func_ceiling(const POS & pos,Item * a)1232   Item_func_ceiling(const POS &pos, Item *a) :Item_func_int_val(pos, a) {}
func_name()1233   const char *func_name() const { return "ceiling"; }
1234   longlong int_op();
1235   double real_op();
1236   my_decimal *decimal_op(my_decimal *);
check_partition_func_processor(uchar * int_arg)1237   bool check_partition_func_processor(uchar *int_arg) {return false;}
check_gcol_func_processor(uchar * int_arg)1238   bool check_gcol_func_processor(uchar *int_arg) { return false;}
1239 };
1240 
1241 
1242 class Item_func_floor :public Item_func_int_val
1243 {
1244 public:
Item_func_floor(Item * a)1245   Item_func_floor(Item *a) :Item_func_int_val(a) {}
Item_func_floor(const POS & pos,Item * a)1246   Item_func_floor(const POS &pos, Item *a) :Item_func_int_val(pos, a) {}
func_name()1247   const char *func_name() const { return "floor"; }
1248   longlong int_op();
1249   double real_op();
1250   my_decimal *decimal_op(my_decimal *);
check_partition_func_processor(uchar * int_arg)1251   bool check_partition_func_processor(uchar *int_arg) {return false;}
check_gcol_func_processor(uchar * int_arg)1252   bool check_gcol_func_processor(uchar *int_arg) { return false;}
1253 };
1254 
1255 /* This handles round and truncate */
1256 
1257 class Item_func_round :public Item_func_num1
1258 {
1259   bool truncate;
1260 public:
Item_func_round(Item * a,Item * b,bool trunc_arg)1261   Item_func_round(Item *a, Item *b, bool trunc_arg)
1262     :Item_func_num1(a,b), truncate(trunc_arg) {}
Item_func_round(const POS & pos,Item * a,Item * b,bool trunc_arg)1263   Item_func_round(const POS &pos, Item *a, Item *b, bool trunc_arg)
1264     :Item_func_num1(pos, a,b), truncate(trunc_arg)
1265   {}
1266 
func_name()1267   const char *func_name() const { return truncate ? "truncate" : "round"; }
1268   double real_op();
1269   longlong int_op();
1270   my_decimal *decimal_op(my_decimal *);
1271   void fix_length_and_dec();
1272 };
1273 
1274 
1275 class Item_func_rand :public Item_real_func
1276 {
1277   typedef Item_real_func super;
1278 
1279   struct rand_struct *rand;
1280   bool first_eval; // TRUE if val_real() is called 1st time
1281 public:
Item_func_rand(const POS & pos,Item * a)1282   Item_func_rand(const POS &pos, Item *a)
1283     :Item_real_func(pos, a), rand(0), first_eval(true)
1284   {}
1285   explicit
Item_func_rand(const POS & pos)1286   Item_func_rand(const POS &pos) :Item_real_func(pos) {}
1287 
1288   virtual bool itemize(Parse_context *pc, Item **res);
1289   double val_real();
func_name()1290   const char *func_name() const { return "rand"; }
const_item()1291   bool const_item() const { return 0; }
1292   /**
1293     This function is non-deterministic and hence depends on the
1294     'RAND' pseudo-table.
1295 
1296     @retval RAND_TABLE_BIT
1297   */
get_initial_pseudo_tables()1298   table_map get_initial_pseudo_tables() const { return RAND_TABLE_BIT; }
1299   bool fix_fields(THD *thd, Item **ref);
1300   void fix_length_and_dec();
cleanup()1301   void cleanup() { first_eval= TRUE; Item_real_func::cleanup(); }
1302 private:
1303   void seed_random (Item * val);
1304 };
1305 
1306 
1307 class Item_func_sign :public Item_int_func
1308 {
1309 public:
Item_func_sign(const POS & pos,Item * a)1310   Item_func_sign(const POS &pos, Item *a) :Item_int_func(pos, a) {}
func_name()1311   const char *func_name() const { return "sign"; }
1312   longlong val_int();
1313   void fix_length_and_dec();
1314 };
1315 
1316 
1317 class Item_func_units :public Item_real_func
1318 {
1319   char *name;
1320   double mul,add;
1321 public:
Item_func_units(const POS & pos,char * name_arg,Item * a,double mul_arg,double add_arg)1322   Item_func_units(const POS &pos, char *name_arg, Item *a, double mul_arg,
1323                   double add_arg)
1324     :Item_real_func(pos, a),name(name_arg),mul(mul_arg),add(add_arg)
1325   {}
1326   double val_real();
func_name()1327   const char *func_name() const { return name; }
1328   void fix_length_and_dec();
1329 };
1330 
1331 
1332 class Item_func_min_max :public Item_func
1333 {
1334   Item_result cmp_type;
1335   String tmp_value;
1336   int cmp_sign;
1337   /* TRUE <=> arguments should be compared in the DATETIME context. */
1338   bool compare_as_dates;
1339   /* An item used for issuing warnings while string to DATETIME conversion. */
1340   Item *datetime_item;
1341 protected:
1342   enum_field_types cached_field_type;
1343   uint cmp_datetimes(longlong *value);
1344   uint cmp_times(longlong *value);
1345 public:
Item_func_min_max(List<Item> & list,int cmp_sign_arg)1346   Item_func_min_max(List<Item> &list,int cmp_sign_arg) :Item_func(list), // TODO: remove
1347     cmp_type(INT_RESULT), cmp_sign(cmp_sign_arg), compare_as_dates(FALSE),
1348     datetime_item(0) {}
Item_func_min_max(const POS & pos,PT_item_list * opt_list,int cmp_sign_arg)1349   Item_func_min_max(const POS &pos, PT_item_list *opt_list, int cmp_sign_arg)
1350     :Item_func(pos, opt_list),
1351     cmp_type(INT_RESULT), cmp_sign(cmp_sign_arg), compare_as_dates(FALSE),
1352     datetime_item(0)
1353   {}
1354   double val_real();
1355   longlong val_int();
1356   String *val_str(String *);
1357   my_decimal *val_decimal(my_decimal *);
1358   bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate);
1359   bool get_time(MYSQL_TIME *ltime);
1360   void fix_length_and_dec();
result_type()1361   enum Item_result result_type () const
1362   {
1363     /*
1364       If we compare as dates, then:
1365       - field_type is MYSQL_TYPE_VARSTRING, MYSQL_TYPE_DATETIME
1366         or MYSQL_TYPE_DATE.
1367       - cmp_type is INT_RESULT or DECIMAL_RESULT,
1368         depending on the amount of fractional digits.
1369       We need to return STRING_RESULT in this case instead of cmp_type.
1370     */
1371     return compare_as_dates ? STRING_RESULT : cmp_type;
1372   }
cast_to_int_type()1373   enum Item_result cast_to_int_type () const
1374   {
1375     /*
1376       make CAST(LEAST_OR_GREATEST(datetime_expr, varchar_expr))
1377       return a number in format "YYYMMDDhhmmss".
1378     */
1379     return compare_as_dates ? INT_RESULT : result_type();
1380   }
field_type()1381   enum_field_types field_type() const { return cached_field_type; }
1382 };
1383 
1384 class Item_func_min :public Item_func_min_max
1385 {
1386 public:
Item_func_min(const POS & pos,PT_item_list * opt_list)1387   Item_func_min(const POS &pos, PT_item_list *opt_list)
1388     :Item_func_min_max(pos, opt_list, 1)
1389   {}
func_name()1390   const char *func_name() const { return "least"; }
1391 };
1392 
1393 class Item_func_max :public Item_func_min_max
1394 {
1395 public:
Item_func_max(const POS & pos,PT_item_list * opt_list)1396   Item_func_max(const POS &pos, PT_item_list *opt_list)
1397     :Item_func_min_max(pos, opt_list, -1)
1398   {}
func_name()1399   const char *func_name() const { return "greatest"; }
1400 };
1401 
1402 
1403 /*
1404   Objects of this class are used for ROLLUP queries to wrap up
1405   each constant item referred to in GROUP BY list.
1406 */
1407 
1408 class Item_func_rollup_const :public Item_func
1409 {
1410 public:
Item_func_rollup_const(Item * a)1411   Item_func_rollup_const(Item *a) :Item_func(a)
1412   {
1413     item_name= a->item_name;
1414   }
1415   double val_real();
1416   longlong val_int();
1417   String *val_str(String *str);
1418   my_decimal *val_decimal(my_decimal *dec);
1419   bool val_json(Json_wrapper *result);
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate)1420   bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
1421   {
1422     return (null_value= args[0]->get_date(ltime, fuzzydate));
1423   }
get_time(MYSQL_TIME * ltime)1424   bool get_time(MYSQL_TIME *ltime)
1425   {
1426     return (null_value= args[0]->get_time(ltime));
1427   }
func_name()1428   const char *func_name() const { return "rollup_const"; }
const_item()1429   bool const_item() const { return 0; }
result_type()1430   Item_result result_type() const { return args[0]->result_type(); }
fix_length_and_dec()1431   void fix_length_and_dec()
1432   {
1433     collation= args[0]->collation;
1434     max_length= args[0]->max_length;
1435     decimals=args[0]->decimals;
1436     /* The item could be a NULL constant. */
1437     null_value= args[0]->is_null();
1438   }
field_type()1439   enum_field_types field_type() const { return args[0]->field_type(); }
1440 };
1441 
1442 
1443 class Item_func_length :public Item_int_func
1444 {
1445   String value;
1446 public:
Item_func_length(const POS & pos,Item * a)1447   Item_func_length(const POS &pos, Item *a) :Item_int_func(pos, a) {}
1448   longlong val_int();
func_name()1449   const char *func_name() const { return "length"; }
fix_length_and_dec()1450   void fix_length_and_dec() { max_length=10; }
1451 };
1452 
1453 class Item_func_bit_length :public Item_func_length
1454 {
1455 public:
Item_func_bit_length(const POS & pos,Item * a)1456   Item_func_bit_length(const POS &pos, Item *a) :Item_func_length(pos, a) {}
val_int()1457   longlong val_int()
1458   { assert(fixed == 1); return Item_func_length::val_int()*8; }
func_name()1459   const char *func_name() const { return "bit_length"; }
1460 };
1461 
1462 class Item_func_char_length :public Item_int_func
1463 {
1464   String value;
1465 public:
Item_func_char_length(Item * a)1466   Item_func_char_length(Item *a) :Item_int_func(a) {}
Item_func_char_length(const POS & pos,Item * a)1467   Item_func_char_length(const POS &pos, Item *a) :Item_int_func(pos, a) {}
1468   longlong val_int();
func_name()1469   const char *func_name() const { return "char_length"; }
fix_length_and_dec()1470   void fix_length_and_dec() { max_length=10; }
1471 };
1472 
1473 class Item_func_coercibility :public Item_int_func
1474 {
1475 public:
Item_func_coercibility(const POS & pos,Item * a)1476   Item_func_coercibility(const POS &pos, Item *a) :Item_int_func(pos, a) {}
1477   longlong val_int();
func_name()1478   const char *func_name() const { return "coercibility"; }
fix_length_and_dec()1479   void fix_length_and_dec() { max_length=10; maybe_null= 0; }
not_null_tables()1480   table_map not_null_tables() const { return 0; }
1481 };
1482 
1483 class Item_func_locate :public Item_int_func
1484 {
1485   String value1,value2;
1486   DTCollation cmp_collation;
1487 public:
Item_func_locate(Item * a,Item * b)1488   Item_func_locate(Item *a,Item *b) :Item_int_func(a,b) {}
Item_func_locate(const POS & pos,Item * a,Item * b)1489   Item_func_locate(const POS &pos, Item *a, Item *b) :Item_int_func(pos, a, b)
1490   {}
Item_func_locate(const POS & pos,Item * a,Item * b,Item * c)1491   Item_func_locate(const POS &pos, Item *a, Item *b, Item *c)
1492     :Item_int_func(pos, a, b, c)
1493   {}
1494 
func_name()1495   const char *func_name() const { return "locate"; }
1496   longlong val_int();
1497   void fix_length_and_dec();
1498   virtual void print(String *str, enum_query_type query_type);
1499 };
1500 
1501 
1502 class Item_func_instr : public Item_func_locate
1503 {
1504 public:
Item_func_instr(const POS & pos,Item * a,Item * b)1505   Item_func_instr(const POS &pos, Item *a, Item *b) :Item_func_locate(pos, a, b)
1506   {}
1507 
func_name()1508   const char *func_name() const { return "instr"; }
1509 };
1510 
1511 
1512 class Item_func_validate_password_strength :public Item_int_func
1513 {
1514 public:
Item_func_validate_password_strength(const POS & pos,Item * a)1515   Item_func_validate_password_strength(const POS &pos, Item *a)
1516     :Item_int_func(pos, a)
1517   {}
1518   longlong val_int();
func_name()1519   const char *func_name() const { return "validate_password_strength"; }
fix_length_and_dec()1520   void fix_length_and_dec() { max_length= 10; maybe_null= 1; }
1521 };
1522 
1523 
1524 class Item_func_field :public Item_int_func
1525 {
1526   String value,tmp;
1527   Item_result cmp_type;
1528   DTCollation cmp_collation;
1529 public:
Item_func_field(const POS & pos,PT_item_list * opt_list)1530   Item_func_field(const POS &pos, PT_item_list *opt_list)
1531     :Item_int_func(pos, opt_list)
1532   {}
1533   longlong val_int();
func_name()1534   const char *func_name() const { return "field"; }
1535   void fix_length_and_dec();
1536 };
1537 
1538 
1539 class Item_func_ascii :public Item_int_func
1540 {
1541   String value;
1542 public:
Item_func_ascii(const POS & pos,Item * a)1543   Item_func_ascii(const POS &pos, Item *a) :Item_int_func(pos, a) {}
1544   longlong val_int();
func_name()1545   const char *func_name() const { return "ascii"; }
fix_length_and_dec()1546   void fix_length_and_dec() { max_length=3; }
1547 };
1548 
1549 class Item_func_ord :public Item_int_func
1550 {
1551   String value;
1552 public:
Item_func_ord(const POS & pos,Item * a)1553   Item_func_ord(const POS &pos, Item *a) :Item_int_func(pos, a) {}
1554   longlong val_int();
func_name()1555   const char *func_name() const { return "ord"; }
1556 };
1557 
1558 class Item_func_find_in_set :public Item_int_func
1559 {
1560   String value,value2;
1561   uint enum_value;
1562   ulonglong enum_bit;
1563   DTCollation cmp_collation;
1564 public:
Item_func_find_in_set(const POS & pos,Item * a,Item * b)1565   Item_func_find_in_set(const POS &pos, Item *a, Item *b)
1566     :Item_int_func(pos, a, b), enum_value(0)
1567   {}
1568   longlong val_int();
func_name()1569   const char *func_name() const { return "find_in_set"; }
1570   void fix_length_and_dec();
1571 };
1572 
1573 /* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
1574 
1575 class Item_func_bit: public Item_int_func
1576 {
1577 protected:
1578   /// @returns Second arg which check_deprecated_bin_op() should check.
1579   virtual Item* check_deprecated_second_arg() const= 0;
1580 public:
Item_func_bit(Item * a,Item * b)1581   Item_func_bit(Item *a, Item *b) :Item_int_func(a, b) {}
Item_func_bit(const POS & pos,Item * a,Item * b)1582   Item_func_bit(const POS &pos, Item *a, Item *b) :Item_int_func(pos, a, b) {}
1583 
Item_func_bit(Item * a)1584   Item_func_bit(Item *a) :Item_int_func(a) {}
Item_func_bit(const POS & pos,Item * a)1585   Item_func_bit(const POS &pos, Item *a) :Item_int_func(pos, a) {}
1586 
fix_length_and_dec()1587   void fix_length_and_dec()
1588   {
1589     unsigned_flag= 1;
1590     check_deprecated_bin_op(args[0], check_deprecated_second_arg());
1591   }
1592 
print(String * str,enum_query_type query_type)1593   virtual inline void print(String *str, enum_query_type query_type)
1594   {
1595     print_op(str, query_type);
1596   }
1597 };
1598 
1599 class Item_func_bit_or :public Item_func_bit
1600 {
check_deprecated_second_arg()1601   Item *check_deprecated_second_arg() const { return args[1]; }
1602 public:
Item_func_bit_or(const POS & pos,Item * a,Item * b)1603   Item_func_bit_or(const POS &pos, Item *a, Item *b) :Item_func_bit(pos, a, b)
1604   {}
1605   longlong val_int();
func_name()1606   const char *func_name() const { return "|"; }
1607 };
1608 
1609 class Item_func_bit_and :public Item_func_bit
1610 {
check_deprecated_second_arg()1611   Item *check_deprecated_second_arg() const { return args[1]; }
1612 public:
Item_func_bit_and(const POS & pos,Item * a,Item * b)1613   Item_func_bit_and(const POS &pos, Item *a, Item *b) :Item_func_bit(pos, a, b)
1614   {}
1615   longlong val_int();
func_name()1616   const char *func_name() const { return "&"; }
1617 };
1618 
1619 class Item_func_bit_count :public Item_int_func
1620 {
1621 public:
Item_func_bit_count(const POS & pos,Item * a)1622   Item_func_bit_count(const POS &pos, Item *a) :Item_int_func(pos, a) {}
1623   longlong val_int();
func_name()1624   const char *func_name() const { return "bit_count"; }
fix_length_and_dec()1625   void fix_length_and_dec()
1626   {
1627     max_length=2;
1628     check_deprecated_bin_op(args[0], NULL);
1629   }
1630 };
1631 
1632 class Item_func_shift_left :public Item_func_bit
1633 {
check_deprecated_second_arg()1634   Item *check_deprecated_second_arg() const { return NULL; }
1635 public:
Item_func_shift_left(const POS & pos,Item * a,Item * b)1636   Item_func_shift_left(const POS &pos, Item *a, Item *b)
1637     :Item_func_bit(pos, a, b)
1638   {}
1639   longlong val_int();
func_name()1640   const char *func_name() const { return "<<"; }
1641 };
1642 
1643 class Item_func_shift_right :public Item_func_bit
1644 {
check_deprecated_second_arg()1645   Item *check_deprecated_second_arg() const { return NULL; }
1646 public:
Item_func_shift_right(const POS & pos,Item * a,Item * b)1647   Item_func_shift_right(const POS &pos, Item *a, Item *b)
1648     :Item_func_bit(pos, a, b)
1649   {}
1650   longlong val_int();
func_name()1651   const char *func_name() const { return ">>"; }
1652 };
1653 
1654 class Item_func_bit_neg :public Item_func_bit
1655 {
check_deprecated_second_arg()1656   Item *check_deprecated_second_arg() const { return NULL; }
1657 public:
Item_func_bit_neg(const POS & pos,Item * a)1658   Item_func_bit_neg(const POS &pos, Item *a) :Item_func_bit(pos, a) {}
1659   longlong val_int();
func_name()1660   const char *func_name() const { return "~"; }
1661 
print(String * str,enum_query_type query_type)1662   virtual inline void print(String *str, enum_query_type query_type)
1663   {
1664     Item_func::print(str, query_type);
1665   }
1666 };
1667 
1668 
1669 class Item_func_last_insert_id :public Item_int_func
1670 {
1671   typedef Item_int_func super;
1672 public:
1673   explicit
Item_func_last_insert_id(const POS & pos)1674   Item_func_last_insert_id(const POS &pos) :Item_int_func(pos) {}
Item_func_last_insert_id(const POS & pos,Item * a)1675   Item_func_last_insert_id(const POS &pos, Item *a) :Item_int_func(pos, a) {}
1676 
1677   virtual bool itemize(Parse_context *pc, Item **res);
1678   longlong val_int();
func_name()1679   const char *func_name() const { return "last_insert_id"; }
fix_length_and_dec()1680   void fix_length_and_dec()
1681   {
1682     unsigned_flag= TRUE;
1683     if (arg_count)
1684       max_length= args[0]->max_length;
1685   }
check_gcol_func_processor(uchar * int_arg)1686   bool check_gcol_func_processor(uchar *int_arg)
1687   { return true; }
1688 };
1689 
1690 
1691 class Item_func_benchmark :public Item_int_func
1692 {
1693   typedef Item_int_func super;
1694 public:
Item_func_benchmark(const POS & pos,Item * count_expr,Item * expr)1695   Item_func_benchmark(const POS &pos, Item *count_expr, Item *expr)
1696     :Item_int_func(pos, count_expr, expr)
1697   {}
1698 
1699   virtual bool itemize(Parse_context *pc, Item **res);
1700   longlong val_int();
func_name()1701   const char *func_name() const { return "benchmark"; }
fix_length_and_dec()1702   void fix_length_and_dec() { max_length=1; maybe_null= true; }
1703   virtual void print(String *str, enum_query_type query_type);
check_gcol_func_processor(uchar * int_arg)1704   bool check_gcol_func_processor(uchar *int_arg)
1705   { return true; }
1706 };
1707 
1708 
1709 void item_func_sleep_init();
1710 void item_func_sleep_free();
1711 
1712 class Item_func_sleep :public Item_int_func
1713 {
1714   typedef Item_int_func super;
1715 public:
Item_func_sleep(const POS & pos,Item * a)1716   Item_func_sleep(const POS &pos, Item *a) :Item_int_func(pos, a) {}
1717 
1718   virtual bool itemize(Parse_context *pc, Item **res);
const_item()1719   bool const_item() const { return 0; }
func_name()1720   const char *func_name() const { return "sleep"; }
1721   /**
1722     This function is non-deterministic and hence depends on the
1723     'RAND' pseudo-table.
1724 
1725     @retval RAND_TABLE_BIT
1726   */
get_initial_pseudo_tables()1727   table_map get_initial_pseudo_tables() const { return RAND_TABLE_BIT; }
1728   longlong val_int();
1729 };
1730 
1731 
1732 
1733 #ifdef HAVE_DLOPEN
1734 
1735 class Item_udf_func :public Item_func
1736 {
1737   typedef Item_func super;
1738 protected:
1739   udf_handler udf;
is_expensive_processor(uchar * arg)1740   bool is_expensive_processor(uchar *arg) { return true; }
1741 
1742 public:
Item_udf_func(const POS & pos,udf_func * udf_arg,PT_item_list * opt_list)1743   Item_udf_func(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1744     :Item_func(pos, opt_list), udf(udf_arg)
1745   {}
1746 
1747   virtual bool itemize(Parse_context *pc, Item **res);
func_name()1748   const char *func_name() const { return udf.name(); }
functype()1749   enum Functype functype() const   { return UDF_FUNC; }
fix_fields(THD * thd,Item ** ref)1750   bool fix_fields(THD *thd, Item **ref)
1751   {
1752     assert(fixed == 0);
1753     bool res= udf.fix_fields(thd, this, arg_count, args);
1754     used_tables_cache= udf.used_tables_cache;
1755     const_item_cache= udf.const_item_cache;
1756     fixed= 1;
1757     return res;
1758   }
update_used_tables()1759   void update_used_tables()
1760   {
1761     /*
1762       TODO: Make a member in UDF_INIT and return if a UDF is deterministic or
1763       not.
1764       Currently UDF_INIT has a member (const_item) that is an in/out
1765       parameter to the init() call.
1766       The code in udf_handler::fix_fields also duplicates the arguments
1767       handling code in Item_func::fix_fields().
1768 
1769       The lack of information if a UDF is deterministic makes writing
1770       a correct update_used_tables() for UDFs impossible.
1771       One solution to this would be :
1772        - Add a is_deterministic member of UDF_INIT
1773        - (optionally) deprecate the const_item member of UDF_INIT
1774        - Take away the duplicate code from udf_handler::fix_fields() and
1775          make Item_udf_func call Item_func::fix_fields() to process its
1776          arguments as for any other function.
1777        - Store the deterministic flag returned by <udf>_init into the
1778        udf_handler.
1779        - Don't implement Item_udf_func::fix_fields, implement
1780        Item_udf_func::fix_length_and_dec() instead (similar to non-UDF
1781        functions).
1782        - Override Item_func::update_used_tables to call
1783        Item_func::update_used_tables() and add a RAND_TABLE_BIT to the
1784        result of Item_func::update_used_tables() if the UDF is
1785        non-deterministic.
1786        - (optionally) rename RAND_TABLE_BIT to NONDETERMINISTIC_BIT to
1787        better describe its usage.
1788 
1789       The above would require a change of the UDF API.
1790       Until that change is done here's how the current code works:
1791       We call Item_func::update_used_tables() only when we know that
1792       the function depends on real non-const tables and is deterministic.
1793       This can be done only because we know that the optimizer will
1794       call update_used_tables() only when there's possibly a new const
1795       table. So update_used_tables() can only make a Item_func more
1796       constant than it is currently.
1797       That's why we don't need to do anything if a function is guaranteed
1798       to return non-constant (it's non-deterministic) or is already a
1799       const.
1800     */
1801     if ((used_tables_cache & ~PSEUDO_TABLE_BITS) &&
1802         !(used_tables_cache & RAND_TABLE_BIT))
1803     {
1804       Item_func::update_used_tables();
1805       if (!const_item_cache && !used_tables_cache)
1806         used_tables_cache= RAND_TABLE_BIT;
1807     }
1808   }
1809   void cleanup();
result_type()1810   Item_result result_type () const { return udf.result_type(); }
not_null_tables()1811   table_map not_null_tables() const { return 0; }
is_expensive()1812   bool is_expensive() { return true; }
1813   virtual void print(String *str, enum_query_type query_type);
1814 
1815 protected:
may_have_named_parameters()1816   virtual bool may_have_named_parameters() const { return true; }
1817 };
1818 
1819 
1820 class Item_func_udf_float :public Item_udf_func
1821 {
1822  public:
Item_func_udf_float(const POS & pos,udf_func * udf_arg,PT_item_list * opt_list)1823   Item_func_udf_float(const POS &pos, udf_func *udf_arg,
1824                       PT_item_list *opt_list)
1825     :Item_udf_func(pos, udf_arg, opt_list)
1826   {}
val_int()1827   longlong val_int()
1828   {
1829     assert(fixed == 1);
1830     return (longlong) rint(Item_func_udf_float::val_real());
1831   }
val_decimal(my_decimal * dec_buf)1832   my_decimal *val_decimal(my_decimal *dec_buf)
1833   {
1834     double res=val_real();
1835     if (null_value)
1836       return NULL;
1837     double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
1838     return dec_buf;
1839   }
1840   double val_real();
1841   String *val_str(String *str);
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate)1842   bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
1843   {
1844     return get_date_from_real(ltime, fuzzydate);
1845   }
get_time(MYSQL_TIME * ltime)1846   bool get_time(MYSQL_TIME *ltime)
1847   {
1848     return get_time_from_real(ltime);
1849   }
fix_length_and_dec()1850   void fix_length_and_dec() { fix_num_length_and_dec(); }
1851 };
1852 
1853 
1854 class Item_func_udf_int :public Item_udf_func
1855 {
1856 public:
Item_func_udf_int(const POS & pos,udf_func * udf_arg,PT_item_list * opt_list)1857   Item_func_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1858     :Item_udf_func(pos, udf_arg, opt_list)
1859   {}
1860   longlong val_int();
val_real()1861   double val_real() { return (double) Item_func_udf_int::val_int(); }
1862   String *val_str(String *str);
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate)1863   bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
1864   {
1865     return get_date_from_int(ltime, fuzzydate);
1866   }
get_time(MYSQL_TIME * ltime)1867   bool get_time(MYSQL_TIME *ltime)
1868   {
1869     return get_time_from_int(ltime);
1870   }
result_type()1871   enum Item_result result_type () const { return INT_RESULT; }
fix_length_and_dec()1872   void fix_length_and_dec() { decimals= 0; max_length= 21; }
1873 };
1874 
1875 
1876 class Item_func_udf_decimal :public Item_udf_func
1877 {
1878 public:
Item_func_udf_decimal(const POS & pos,udf_func * udf_arg,PT_item_list * opt_list)1879   Item_func_udf_decimal(const POS &pos, udf_func *udf_arg,
1880                         PT_item_list *opt_list)
1881     :Item_udf_func(pos, udf_arg, opt_list)
1882   {}
1883   longlong val_int();
1884   double val_real();
1885   my_decimal *val_decimal(my_decimal *);
1886   String *val_str(String *str);
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate)1887   bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
1888   {
1889     return get_date_from_decimal(ltime, fuzzydate);
1890   }
get_time(MYSQL_TIME * ltime)1891   bool get_time(MYSQL_TIME *ltime)
1892   {
1893     return get_time_from_decimal(ltime);
1894   }
result_type()1895   enum Item_result result_type () const { return DECIMAL_RESULT; }
1896   void fix_length_and_dec();
1897 };
1898 
1899 
1900 class Item_func_udf_str :public Item_udf_func
1901 {
1902 public:
Item_func_udf_str(const POS & pos,udf_func * udf_arg,PT_item_list * opt_list)1903   Item_func_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1904     :Item_udf_func(pos, udf_arg, opt_list)
1905   {}
1906 
1907   String *val_str(String *);
val_real()1908   double val_real()
1909   {
1910     int err_not_used;
1911     char *end_not_used;
1912     String *res;
1913     res= val_str(&str_value);
1914     return res ? my_strntod(res->charset(),(char*) res->ptr(),
1915                             res->length(), &end_not_used, &err_not_used) : 0.0;
1916   }
val_int()1917   longlong val_int()
1918   {
1919     int err_not_used;
1920     String *res;  res=val_str(&str_value);
1921     return res ? my_strntoll(res->charset(),res->ptr(),res->length(),10,
1922                              (char**) 0, &err_not_used) : (longlong) 0;
1923   }
val_decimal(my_decimal * dec_buf)1924   my_decimal *val_decimal(my_decimal *dec_buf)
1925   {
1926     String *res=val_str(&str_value);
1927     if (!res)
1928       return NULL;
1929     string2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
1930     return dec_buf;
1931   }
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate)1932   bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
1933   {
1934     return get_date_from_string(ltime, fuzzydate);
1935   }
get_time(MYSQL_TIME * ltime)1936   bool get_time(MYSQL_TIME *ltime)
1937   {
1938     return get_time_from_string(ltime);
1939   }
result_type()1940   enum Item_result result_type () const { return STRING_RESULT; }
1941   void fix_length_and_dec();
1942 };
1943 
1944 #endif /* HAVE_DLOPEN */
1945 
1946 void mysql_ull_cleanup(THD *thd);
1947 void mysql_ull_set_explicit_lock_duration(THD *thd);
1948 
1949 class Item_func_get_lock :public Item_int_func
1950 {
1951   typedef Item_int_func super;
1952 
1953   String value;
1954  public:
Item_func_get_lock(const POS & pos,Item * a,Item * b)1955   Item_func_get_lock(const POS &pos, Item *a, Item *b) :Item_int_func(pos, a, b)
1956   {}
1957 
1958   virtual bool itemize(Parse_context *pc, Item **res);
1959   longlong val_int();
func_name()1960   const char *func_name() const { return "get_lock"; }
fix_length_and_dec()1961   void fix_length_and_dec() { max_length=1; maybe_null=1;}
check_gcol_func_processor(uchar * int_arg)1962   bool check_gcol_func_processor(uchar *int_arg)
1963   { return true; }
decimal_precision()1964   virtual uint decimal_precision() const { return max_length; }
1965 };
1966 
1967 class Item_func_release_lock :public Item_int_func
1968 {
1969   typedef Item_int_func super;
1970 
1971   String value;
1972 public:
Item_func_release_lock(const POS & pos,Item * a)1973   Item_func_release_lock(const POS &pos, Item *a) :Item_int_func(pos, a) {}
1974   virtual bool itemize(Parse_context *pc, Item **res);
1975 
1976   longlong val_int();
func_name()1977   const char *func_name() const { return "release_lock"; }
fix_length_and_dec()1978   void fix_length_and_dec() { max_length=1; maybe_null=1;}
check_gcol_func_processor(uchar * int_arg)1979   bool check_gcol_func_processor(uchar *int_arg)
1980   { return true; }
decimal_precision()1981   virtual uint decimal_precision() const { return max_length; }
1982 };
1983 
1984 class Item_func_release_all_locks :public Item_int_func
1985 {
1986   typedef Item_int_func super;
1987 
1988 public:
Item_func_release_all_locks(const POS & pos)1989   explicit Item_func_release_all_locks(const POS &pos) :Item_int_func(pos) {}
1990   virtual bool itemize(Parse_context *pc, Item **res);
1991 
1992   longlong val_int();
func_name()1993   const char *func_name() const { return "release_all_locks"; }
fix_length_and_dec()1994   void fix_length_and_dec() { unsigned_flag= TRUE; }
check_gcol_func_processor(uchar * int_arg)1995   bool check_gcol_func_processor(uchar *int_arg)
1996   { return true; }
1997 };
1998 
1999 /* replication functions */
2000 
2001 class Item_master_pos_wait :public Item_int_func
2002 {
2003   typedef Item_int_func super;
2004   String value;
2005 public:
Item_master_pos_wait(const POS & pos,Item * a,Item * b)2006   Item_master_pos_wait(const POS &pos, Item *a, Item *b)
2007     :Item_int_func(pos, a, b)
2008   {}
Item_master_pos_wait(const POS & pos,Item * a,Item * b,Item * c)2009   Item_master_pos_wait(const POS &pos, Item *a, Item *b, Item *c)
2010     :Item_int_func(pos, a, b, c)
2011   {}
Item_master_pos_wait(const POS & pos,Item * a,Item * b,Item * c,Item * d)2012   Item_master_pos_wait(const POS &pos, Item *a, Item *b, Item *c, Item *d)
2013     :Item_int_func(pos, a, b, c, d)
2014   {}
2015 
2016   virtual bool itemize(Parse_context *pc, Item **res);
2017   longlong val_int();
func_name()2018   const char *func_name() const { return "master_pos_wait"; }
fix_length_and_dec()2019   void fix_length_and_dec() { max_length=21; maybe_null=1;}
check_gcol_func_processor(uchar * int_arg)2020   bool check_gcol_func_processor(uchar *int_arg)
2021   { return true; }
2022 };
2023 
2024 /**
2025   This class is used for implementing the new wait_for_executed_gtid_set
2026   function and the functions related to them. This new function is independent
2027   of the slave threads.
2028 */
2029 class Item_wait_for_executed_gtid_set :public Item_int_func
2030 {
2031   typedef Item_int_func super;
2032 
2033   String value;
2034 public:
Item_wait_for_executed_gtid_set(const POS & pos,Item * a)2035   Item_wait_for_executed_gtid_set(const POS &pos, Item *a) :Item_int_func(pos, a) {}
Item_wait_for_executed_gtid_set(const POS & pos,Item * a,Item * b)2036   Item_wait_for_executed_gtid_set(const POS &pos, Item *a, Item *b)
2037     :Item_int_func(pos, a, b)
2038   {}
2039 
2040   virtual bool itemize(Parse_context *pc, Item **res);
2041   longlong val_int();
func_name()2042   const char *func_name() const { return "wait_for_executed_gtid_set"; }
fix_length_and_dec()2043   void fix_length_and_dec() { max_length= 21; maybe_null= 1; }
2044 };
2045 
2046 class Item_master_gtid_set_wait :public Item_int_func
2047 {
2048   typedef Item_int_func super;
2049 
2050   String value;
2051 public:
Item_master_gtid_set_wait(const POS & pos,Item * a)2052   Item_master_gtid_set_wait(const POS &pos, Item *a) :Item_int_func(pos, a) {}
Item_master_gtid_set_wait(const POS & pos,Item * a,Item * b)2053   Item_master_gtid_set_wait(const POS &pos, Item *a, Item *b)
2054     :Item_int_func(pos, a, b)
2055   {}
Item_master_gtid_set_wait(const POS & pos,Item * a,Item * b,Item * c)2056   Item_master_gtid_set_wait(const POS &pos, Item *a, Item *b, Item *c)
2057     :Item_int_func(pos, a, b, c)
2058   {}
2059 
2060   virtual bool itemize(Parse_context *pc, Item **res);
2061   longlong val_int();
func_name()2062   const char *func_name() const { return "wait_until_sql_thread_after_gtids"; }
fix_length_and_dec()2063   void fix_length_and_dec() { max_length= 21; maybe_null= 1; }
2064 };
2065 
2066 class Item_func_gtid_subset : public Item_int_func
2067 {
2068   String buf1;
2069   String buf2;
2070 public:
Item_func_gtid_subset(const POS & pos,Item * a,Item * b)2071   Item_func_gtid_subset(const POS &pos, Item *a, Item *b)
2072     : Item_int_func(pos, a, b)
2073   {}
2074   longlong val_int();
func_name()2075   const char *func_name() const { return "gtid_subset"; }
fix_length_and_dec()2076   void fix_length_and_dec() { max_length= 21; maybe_null= 0; }
is_bool_func()2077   bool is_bool_func() { return true; }
2078 };
2079 
2080 
2081 /**
2082   Common class for:
2083     Item_func_get_system_var
2084     Item_func_get_user_var
2085     Item_func_set_user_var
2086 */
2087 class Item_var_func :public Item_func
2088 {
2089 public:
Item_var_func()2090   Item_var_func() :Item_func() { }
Item_var_func(const POS & pos)2091   explicit Item_var_func(const POS &pos) :Item_func(pos) { }
2092 
Item_var_func(THD * thd,Item_var_func * item)2093   Item_var_func(THD *thd, Item_var_func *item) :Item_func(thd, item) { }
2094 
Item_var_func(Item * a)2095   Item_var_func(Item *a) :Item_func(a) { }
Item_var_func(const POS & pos,Item * a)2096   Item_var_func(const POS &pos, Item *a) :Item_func(pos, a) { }
2097 
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate)2098   bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
2099   {
2100     return get_date_from_non_temporal(ltime, fuzzydate);
2101   }
get_time(MYSQL_TIME * ltime)2102   bool get_time(MYSQL_TIME *ltime)
2103   {
2104     return get_time_from_non_temporal(ltime);
2105   }
check_gcol_func_processor(uchar * int_arg)2106   bool check_gcol_func_processor(uchar *int_arg) { return true;}
2107 };
2108 
2109 
2110 /* Handling of user definable variables */
2111 
2112 class user_var_entry;
2113 
2114 class Item_func_set_user_var :public Item_var_func
2115 {
2116   enum Item_result cached_result_type;
2117   user_var_entry *entry;
2118   /*
2119     The entry_thread_id variable is used:
2120     1) to skip unnecessary updates of the entry field (see above);
2121     2) to reset the entry field that was initialized in the other thread
2122        (for example, an item tree of a trigger that updates user variables
2123        may be shared between several connections, and the entry_thread_id field
2124        prevents updates of one connection user variables from a concurrent
2125        connection calling the same trigger that initially updated some
2126        user variable it the first connection context).
2127   */
2128   my_thread_id entry_thread_id;
2129   /**
2130     Delayed setting of non-constness.
2131 
2132     Normally, Item_func_get_user_var objects are tagged as not const
2133     when Item_func_set_user_var::fix_fields() is called for the same
2134     variable in the same query. If delayed_non_constness is set, the
2135     tagging is delayed until the variable is actually set. This means
2136     that Item_func_get_user_var objects will still be treated as a
2137     constant by the optimizer and executor until the variable is
2138     actually changed.
2139 
2140     @see select_dumpvar::send_data().
2141    */
2142   bool delayed_non_constness;
2143   String value;
2144   my_decimal decimal_buff;
2145   bool null_item;
2146   union
2147   {
2148     longlong vint;
2149     double vreal;
2150     String *vstr;
2151     my_decimal *vdec;
2152   } save_result;
2153 
2154 public:
2155   Name_string name; // keep it public
2156 
Item_func_set_user_var(Name_string a,Item * b,bool delayed)2157   Item_func_set_user_var(Name_string a, Item *b, bool delayed)
2158     :Item_var_func(b), cached_result_type(INT_RESULT),
2159      entry(NULL), entry_thread_id(0), delayed_non_constness(delayed), name(a)
2160   {}
Item_func_set_user_var(const POS & pos,Name_string a,Item * b,bool delayed)2161   Item_func_set_user_var(const POS &pos, Name_string a, Item *b, bool delayed)
2162     :Item_var_func(pos, b), cached_result_type(INT_RESULT),
2163      entry(NULL), entry_thread_id(0), delayed_non_constness(delayed), name(a)
2164   {}
2165 
Item_func_set_user_var(THD * thd,Item_func_set_user_var * item)2166   Item_func_set_user_var(THD *thd, Item_func_set_user_var *item)
2167     :Item_var_func(thd, item), cached_result_type(item->cached_result_type),
2168      entry(item->entry), entry_thread_id(item->entry_thread_id),
2169      delayed_non_constness(item->delayed_non_constness), value(item->value),
2170      decimal_buff(item->decimal_buff), null_item(item->null_item),
2171      save_result(item->save_result), name(item->name)
2172   {}
functype()2173   enum Functype functype() const { return SUSERVAR_FUNC; }
2174   double val_real();
2175   longlong val_int();
2176   String *val_str(String *str);
2177   my_decimal *val_decimal(my_decimal *);
2178   double val_result();
2179   longlong val_int_result();
2180   bool val_bool_result();
2181   String *str_result(String *str);
2182   my_decimal *val_decimal_result(my_decimal *);
2183   bool is_null_result();
2184   bool update_hash(const void *ptr, uint length, enum Item_result type,
2185   		   const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
2186   bool send(Protocol *protocol, String *str_arg);
2187   void make_field(Send_field *tmp_field);
2188   bool check(bool use_result_field);
2189   void save_item_result(Item *item);
2190   bool update();
result_type()2191   enum Item_result result_type () const { return cached_result_type; }
2192   bool fix_fields(THD *thd, Item **ref);
2193   void fix_length_and_dec();
2194   virtual void print(String *str, enum_query_type query_type);
2195   void print_assignment(String *str, enum_query_type query_type);
func_name()2196   const char *func_name() const { return "set_user_var"; }
2197 
2198   type_conversion_status save_in_field(Field *field, bool no_conversions,
2199                                        bool can_use_result_field);
2200 
save_org_in_field(Field * field)2201   void save_org_in_field(Field *field)
2202   { save_in_field(field, true, false); }
2203 
2204   bool set_entry(THD *thd, bool create_if_not_exists);
2205   void cleanup();
2206 protected:
save_in_field_inner(Field * field,bool no_conversions)2207   type_conversion_status save_in_field_inner(Field *field, bool no_conversions)
2208   { return save_in_field(field, no_conversions, true); }
2209 };
2210 
2211 
2212 class Item_func_get_user_var :public Item_var_func,
2213                               private Settable_routine_parameter
2214 {
2215   user_var_entry *var_entry;
2216   Item_result m_cached_result_type;
2217 
2218 public:
2219   Name_string name; // keep it public
2220 
Item_func_get_user_var(Name_string a)2221   Item_func_get_user_var(Name_string a):
2222     Item_var_func(), m_cached_result_type(STRING_RESULT), name(a) {}
Item_func_get_user_var(const POS & pos,Name_string a)2223   Item_func_get_user_var(const POS &pos, Name_string a):
2224     Item_var_func(pos), m_cached_result_type(STRING_RESULT), name(a) {}
2225 
functype()2226   enum Functype functype() const { return GUSERVAR_FUNC; }
2227   double val_real();
2228   longlong val_int();
2229   my_decimal *val_decimal(my_decimal*);
2230   String *val_str(String* str);
2231   void fix_length_and_dec();
2232   virtual void print(String *str, enum_query_type query_type);
2233   enum Item_result result_type() const;
2234   /*
2235     We must always return variables as strings to guard against selects of type
2236     select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
2237   */
func_name()2238   const char *func_name() const { return "get_user_var"; }
2239   bool const_item() const;
used_tables()2240   table_map used_tables() const
2241   { return const_item() ? 0 : RAND_TABLE_BIT; }
2242   bool eq(const Item *item, bool binary_cmp) const;
2243 private:
2244   bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
2245 
2246 public:
get_settable_routine_parameter()2247   Settable_routine_parameter *get_settable_routine_parameter()
2248   {
2249     return this;
2250   }
2251 };
2252 
2253 
2254 /*
2255   This item represents user variable used as out parameter (e.g in LOAD DATA),
2256   and it is supposed to be used only for this purprose. So it is simplified
2257   a lot. Actually you should never obtain its value.
2258 
2259   The only two reasons for this thing being an Item is possibility to store it
2260   in List<Item> and desire to place this code somewhere near other functions
2261   working with user variables.
2262 */
2263 class Item_user_var_as_out_param :public Item
2264 {
2265   Name_string name;
2266   user_var_entry *entry;
2267 public:
Item_user_var_as_out_param(Name_string a)2268   Item_user_var_as_out_param(Name_string a) :name(a)
2269   { item_name.copy(a); }
2270   /* We should return something different from FIELD_ITEM here */
type()2271   enum Type type() const { return STRING_ITEM;}
2272   double val_real();
2273   longlong val_int();
2274   String *val_str(String *str);
2275   my_decimal *val_decimal(my_decimal *decimal_buffer);
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate)2276   bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
2277   {
2278     assert(0);
2279     return true;
2280   }
get_time(MYSQL_TIME * ltime)2281   bool get_time(MYSQL_TIME *ltime)
2282   {
2283     assert(0);
2284     return true;
2285   }
2286 
2287   /* fix_fields() binds variable name with its entry structure */
2288   bool fix_fields(THD *thd, Item **ref);
2289   virtual void print(String *str, enum_query_type query_type);
2290   void set_null_value(const CHARSET_INFO* cs);
2291   void set_value(const char *str, size_t length, const CHARSET_INFO* cs);
2292 };
2293 
2294 
2295 /* A system variable */
2296 
2297 #define GET_SYS_VAR_CACHE_LONG     1
2298 #define GET_SYS_VAR_CACHE_DOUBLE   2
2299 #define GET_SYS_VAR_CACHE_STRING   4
2300 
2301 class Item_func_get_system_var :public Item_var_func
2302 {
2303   sys_var *var;
2304   enum_var_type var_type, orig_var_type;
2305   LEX_STRING component;
2306   longlong cached_llval;
2307   double cached_dval;
2308   String cached_strval;
2309   my_bool cached_null_value;
2310   query_id_t used_query_id;
2311   uchar cache_present;
2312   Sys_var_tracker var_tracker;
2313 
2314 public:
2315   Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
2316                            LEX_STRING *component_arg, const char *name_arg,
2317                            size_t name_len_arg);
functype()2318   enum Functype functype() const { return GSYSVAR_FUNC; }
2319   void update_null_value();
2320   void fix_length_and_dec();
2321   void print(String *str, enum_query_type query_type);
const_item()2322   bool const_item() const { return true; }
used_tables()2323   table_map used_tables() const { return 0; }
2324   enum Item_result result_type() const;
2325   enum_field_types field_type() const;
2326   double val_real();
2327   longlong val_int();
2328   String* val_str(String*);
val_decimal(my_decimal * dec_buf)2329   my_decimal *val_decimal(my_decimal *dec_buf)
2330   { return val_decimal_from_real(dec_buf); }
2331   /* TODO: fix to support views */
func_name()2332   const char *func_name() const { return "get_system_var"; }
2333   bool eq(const Item *item, bool binary_cmp) const;
2334 
2335   void cleanup();
2336 };
2337 
2338 
2339 class JOIN;
2340 
2341 class Item_func_match :public Item_real_func
2342 {
2343   typedef Item_real_func super;
2344 
2345 public:
2346   Item *against;
2347   uint key, flags;
2348   bool join_key;
2349   DTCollation cmp_collation;
2350   FT_INFO *ft_handler;
2351   TABLE_LIST *table_ref;
2352   /**
2353      Master item means that if idendical items are present in the
2354      statement, they use the same FT handler. FT handler is initialized
2355      only for master item and slave items just use it. FT hints initialized
2356      for master only, slave items HINTS are not accessed.
2357   */
2358   Item_func_match *master;
2359   Item *concat_ws;           // Item_func_concat_ws
2360   String value;              // value of concat_ws
2361   String search_value;       // key_item()'s value converted to cmp_collation
2362 
2363   /**
2364      Constructor for Item_func_match class.
2365 
2366      @param a  List of arguments.
2367      @param b  FT Flags.
2368      @param c  Parsing context.
2369   */
Item_func_match(const POS & pos,PT_item_list * a,Item * against_arg,uint b)2370   Item_func_match(const POS &pos, PT_item_list *a, Item *against_arg, uint b):
2371     Item_real_func(pos, a), against(against_arg), key(0), flags(b),
2372     join_key(false),
2373     ft_handler(NULL), table_ref(NULL),
2374     master(NULL), concat_ws(NULL), hints(NULL), simple_expression(false)
2375   {}
2376 
2377   virtual bool itemize(Parse_context *pc, Item **res);
2378 
cleanup()2379   void cleanup()
2380   {
2381     DBUG_ENTER("Item_func_match::cleanup");
2382     Item_real_func::cleanup();
2383     if (!master && ft_handler)
2384     {
2385       ft_handler->please->close_search(ft_handler);
2386       delete hints;
2387     }
2388     ft_handler= NULL;
2389     concat_ws= NULL;
2390     table_ref= NULL;           // required by Item_func_match::eq()
2391     master= NULL;
2392     DBUG_VOID_RETURN;
2393   }
key_item()2394   virtual Item *key_item() const { return against; }
functype()2395   enum Functype functype() const { return FT_FUNC; }
func_name()2396   const char *func_name() const { return "match"; }
update_used_tables()2397   void update_used_tables() {}
not_null_tables()2398   table_map not_null_tables() const { return 0; }
2399   bool fix_fields(THD *thd, Item **ref);
2400   bool eq(const Item *, bool binary_cmp) const;
2401   /* The following should be safe, even if we compare doubles */
val_int()2402   longlong val_int() { assert(fixed == 1); return val_real() != 0.0; }
2403   double val_real();
2404   virtual void print(String *str, enum_query_type query_type);
2405 
2406   bool fix_index();
2407   bool init_search(THD *thd);
check_gcol_func_processor(uchar * int_arg)2408   bool check_gcol_func_processor(uchar *int_arg)
2409   // TODO: consider adding in support for the MATCH-based generated columns
2410   { return true; }
2411 
2412   /**
2413      Get number of matching rows from FT handler.
2414 
2415      @note Requires that FT handler supports the extended API
2416 
2417      @return Number of matching rows in result
2418    */
get_count()2419   ulonglong get_count()
2420   {
2421     assert(ft_handler);
2422     assert(table_ref->table->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT);
2423 
2424     return ((FT_INFO_EXT *)ft_handler)->could_you->
2425       count_matches((FT_INFO_EXT *)ft_handler);
2426   }
2427 
2428   /**
2429      Check whether FT result is ordered on rank
2430 
2431      @return true if result is ordered
2432      @return false otherwise
2433    */
ordered_result()2434   bool ordered_result()
2435   {
2436     assert(!master);
2437     if (hints->get_flags() & FT_SORTED)
2438       return true;
2439 
2440     if ((table_ref->table->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0)
2441       return false;
2442 
2443     assert(ft_handler);
2444     return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
2445       FTS_ORDERED_RESULT;
2446   }
2447 
2448   /**
2449      Check whether FT result contains the document ID
2450 
2451      @return true if document ID is available
2452      @return false otherwise
2453    */
docid_in_result()2454   bool docid_in_result()
2455   {
2456     assert(ft_handler);
2457 
2458     if ((table_ref->table->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0)
2459       return false;
2460 
2461     return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
2462       FTS_DOCID_IN_RESULT;
2463   }
2464 
2465   float get_filtering_effect(table_map filter_for_table,
2466                              table_map read_tables,
2467                              const MY_BITMAP *fields_to_ignore,
2468                              double rows_in_table);
2469 
2470   /**
2471      Returns master MATCH function.
2472 
2473      @return pointer to master MATCH function.
2474   */
get_master()2475   Item_func_match *get_master()
2476   {
2477     if (master)
2478       return master->get_master();
2479     return this;
2480   }
2481 
2482   /**
2483      Set master MATCH function and adjust used_in_where_only value.
2484 
2485      @param item item for which master should be set.
2486   */
set_master(Item_func_match * item)2487   void set_master(Item_func_match *item)
2488   {
2489     used_in_where_only&= item->used_in_where_only;
2490     item->master= this;
2491   }
2492 
2493   /**
2494      Returns pointer to Ft_hints object belonging to master MATCH function.
2495 
2496      @return pointer to Ft_hints object
2497   */
get_hints()2498   Ft_hints *get_hints()
2499   {
2500     assert(!master);
2501     return hints;
2502   }
2503 
2504   /**
2505      Set comparison operation type and and value for master MATCH function.
2506 
2507      @param type   comparison operation type
2508      @param value  comparison operation value
2509   */
set_hints_op(enum ft_operation type,double value_arg)2510   void set_hints_op(enum ft_operation type, double value_arg)
2511   {
2512     assert(!master);
2513     hints->set_hint_op(type, value_arg);
2514   }
2515 
2516   /**
2517      Set FT hints.
2518   */
2519   void set_hints(JOIN *join, uint ft_flag, ha_rows ft_limit, bool no_cond);
2520 
2521   /**
2522      Check if ranking is not needed.
2523 
2524      @return true if ranking is not needed
2525      @return false otherwise
2526   */
can_skip_ranking()2527   bool can_skip_ranking()
2528   {
2529     assert(!master);
2530     return (!(hints->get_flags() & FT_SORTED) && // FT_SORTED is no set
2531             used_in_where_only &&                // MATCH result is not used
2532                                                  // in expression
2533             hints->get_op_type() == FT_OP_NO);   // MATCH is single function
2534   }
2535 
2536   /**
2537      Set flag that the function is a simple expression.
2538 
2539      @param val true if the function is a simple expression, false otherwise
2540   */
set_simple_expression(bool val)2541   void set_simple_expression(bool val)
2542   {
2543     assert(!master);
2544     simple_expression= val;
2545   }
2546 
2547   /**
2548      Check if this MATCH function is a simple expression in WHERE condition.
2549 
2550      @return true if simple expression
2551      @return false otherwise
2552   */
is_simple_expression()2553   bool is_simple_expression()
2554   {
2555     assert(!master);
2556     return simple_expression;
2557   }
2558 
2559 private:
2560   /**
2561      Fulltext index hints, initialized for master MATCH function only.
2562   */
2563   Ft_hints *hints;
2564   /**
2565      Flag is true when MATCH function is used as a simple expression in
2566      WHERE condition, i.e. there is no AND/OR combinations, just simple
2567      MATCH function or [MATCH, rank] comparison operation.
2568   */
2569   bool simple_expression;
2570   /**
2571      true if MATCH function is used in WHERE condition only.
2572      Used to dermine what hints can be used for FT handler.
2573      Note that only master MATCH function has valid value.
2574      it's ok since only master function is involved in the hint processing.
2575   */
2576   bool used_in_where_only;
2577   /**
2578      Check whether storage engine for given table,
2579      allows FTS Boolean search on non-indexed columns.
2580 
2581      @todo A flag should be added to the extended fulltext API so that
2582            it may be checked whether search on non-indexed columns are
2583            supported. Currently, it is not possible to check for such a
2584            flag since @c this->ft_handler is not yet set when this function is
2585            called.  The current hack is to assume that search on non-indexed
2586            columns are supported for engines that does not support the extended
2587            fulltext API (e.g., MyISAM), while it is not supported for other
2588            engines (e.g., InnoDB)
2589 
2590      @param tr Table for which storage engine to check
2591 
2592      @retval true if BOOLEAN search on non-indexed columns is supported
2593      @retval false otherwise
2594    */
allows_search_on_non_indexed_columns(const TABLE * tr)2595   bool allows_search_on_non_indexed_columns(const TABLE *tr)
2596   {
2597     // Only Boolean search may support non_indexed columns
2598     if (!(flags & FT_BOOL))
2599       return false;
2600 
2601     assert(tr && tr->file);
2602 
2603     // Assume that if extended fulltext API is not supported,
2604     // non-indexed columns are allowed.  This will be true for MyISAM.
2605     if ((tr->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0)
2606       return true;
2607 
2608     return false;
2609   }
2610 };
2611 
2612 
2613 class Item_func_bit_xor : public Item_func_bit
2614 {
check_deprecated_second_arg()2615   Item *check_deprecated_second_arg() const { return args[1]; }
2616 public:
Item_func_bit_xor(const POS & pos,Item * a,Item * b)2617   Item_func_bit_xor(const POS &pos, Item *a, Item *b) :Item_func_bit(pos, a, b)
2618   {}
2619   longlong val_int();
func_name()2620   const char *func_name() const { return "^"; }
2621 };
2622 
2623 class Item_func_is_free_lock :public Item_int_func
2624 {
2625   typedef Item_int_func super;
2626 
2627   String value;
2628 public:
Item_func_is_free_lock(const POS & pos,Item * a)2629   Item_func_is_free_lock(const POS &pos, Item *a) :Item_int_func(pos, a) {}
2630 
2631   virtual bool itemize(Parse_context *pc, Item **res);
2632   longlong val_int();
func_name()2633   const char *func_name() const { return "is_free_lock"; }
fix_length_and_dec()2634   void fix_length_and_dec() { max_length= 1; maybe_null= TRUE;}
check_gcol_func_processor(uchar * int_arg)2635   bool check_gcol_func_processor(uchar *int_arg)
2636   { return true; }
2637 };
2638 
2639 class Item_func_is_used_lock :public Item_int_func
2640 {
2641   typedef Item_int_func super;
2642 
2643   String value;
2644 public:
Item_func_is_used_lock(const POS & pos,Item * a)2645   Item_func_is_used_lock(const POS &pos, Item *a) :Item_int_func(pos, a) {}
2646 
2647   virtual bool itemize(Parse_context *pc, Item **res);
2648   longlong val_int();
func_name()2649   const char *func_name() const { return "is_used_lock"; }
fix_length_and_dec()2650   void fix_length_and_dec() { unsigned_flag= TRUE; maybe_null= TRUE; }
check_gcol_func_processor(uchar * int_arg)2651   bool check_gcol_func_processor(uchar *int_arg)
2652   { return true; }
2653 };
2654 
2655 /* For type casts */
2656 
2657 enum Cast_target
2658 {
2659   ITEM_CAST_BINARY, ITEM_CAST_SIGNED_INT, ITEM_CAST_UNSIGNED_INT,
2660   ITEM_CAST_DATE, ITEM_CAST_TIME, ITEM_CAST_DATETIME, ITEM_CAST_CHAR,
2661   ITEM_CAST_DECIMAL, ITEM_CAST_JSON
2662 };
2663 
2664 
2665 class Item_func_row_count :public Item_int_func
2666 {
2667   typedef Item_int_func super;
2668 
2669 public:
Item_func_row_count(const POS & pos)2670   explicit Item_func_row_count(const POS &pos) :Item_int_func(pos) {}
2671 
2672   virtual bool itemize(Parse_context *pc, Item **res);
2673 
2674   longlong val_int();
func_name()2675   const char *func_name() const { return "row_count"; }
fix_length_and_dec()2676   void fix_length_and_dec() { decimals= 0; maybe_null=0; }
check_gcol_func_processor(uchar * int_arg)2677   bool check_gcol_func_processor(uchar *int_arg)
2678   { return true; }
2679 };
2680 
2681 
2682 /*
2683  *
2684  * Stored FUNCTIONs
2685  *
2686  */
2687 
2688 class sp_head;
2689 class sp_name;
2690 struct st_sp_security_context;
2691 
2692 class Item_func_sp :public Item_func
2693 {
2694   typedef Item_func super;
2695 private:
2696   Name_resolution_context *context;
2697   sp_name *m_name;
2698   mutable sp_head *m_sp;
2699   TABLE *dummy_table;
2700   uchar result_buf[64];
2701   /*
2702      The result field of the concrete stored function.
2703   */
2704   Field *sp_result_field;
2705 
2706   bool execute();
2707   bool execute_impl(THD *thd);
2708   bool init_result_field(THD *thd);
2709 
2710 protected:
is_expensive_processor(uchar * arg)2711   bool is_expensive_processor(uchar *arg) { return true; }
2712   type_conversion_status save_in_field_inner(Field *field, bool no_conversions);
2713 
2714 public:
2715 
2716   Item_func_sp(const POS &pos,
2717                const LEX_STRING &db_name, const LEX_STRING &fn_name,
2718                bool use_explicit_name, PT_item_list *opt_list);
2719 
2720   virtual bool itemize(Parse_context *pc, Item **res);
2721   /**
2722     Must not be called before the procedure is resolved,
2723     i.e. ::init_result_field().
2724   */
2725   table_map get_initial_pseudo_tables() const;
2726   void update_used_tables();
2727 
2728   virtual void fix_after_pullout(st_select_lex *parent_select,
2729                                  st_select_lex *removed_select);
2730 
2731   void cleanup();
2732 
2733   const char *func_name() const;
2734 
2735   enum enum_field_types field_type() const;
2736 
2737   Field *tmp_table_field(TABLE *t_arg);
2738 
2739   void make_field(Send_field *tmp_field);
2740 
2741   Item_result result_type() const;
2742 
val_int()2743   longlong val_int()
2744   {
2745     if (execute())
2746       return (longlong) 0;
2747     return sp_result_field->val_int();
2748   }
2749 
val_real()2750   double val_real()
2751   {
2752     if (execute())
2753       return 0.0;
2754     return sp_result_field->val_real();
2755   }
2756 
get_date(MYSQL_TIME * ltime,my_time_flags_t fuzzydate)2757   bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
2758   {
2759     if (execute())
2760       return true;
2761     return sp_result_field->get_date(ltime, fuzzydate);
2762   }
2763 
get_time(MYSQL_TIME * ltime)2764   bool get_time(MYSQL_TIME *ltime)
2765   {
2766     if (execute())
2767       return true;
2768     return sp_result_field->get_time(ltime);
2769   }
2770 
val_decimal(my_decimal * dec_buf)2771   my_decimal *val_decimal(my_decimal *dec_buf)
2772   {
2773     if (execute())
2774       return NULL;
2775     return sp_result_field->val_decimal(dec_buf);
2776   }
2777 
val_str(String * str)2778   String *val_str(String *str)
2779   {
2780     String buf;
2781     char buff[20];
2782     buf.set(buff, 20, str->charset());
2783     buf.length(0);
2784     if (execute())
2785       return NULL;
2786     /*
2787       result_field will set buf pointing to internal buffer
2788       of the resul_field. Due to this it will change any time
2789       when SP is executed. In order to prevent occasional
2790       corruption of returned value, we make here a copy.
2791     */
2792     sp_result_field->val_str(&buf);
2793     str->copy(buf);
2794     return str;
2795   }
2796 
2797   bool val_json(Json_wrapper *result);
2798 
change_context_processor(uchar * cntx)2799   virtual bool change_context_processor(uchar *cntx)
2800   {
2801     context= reinterpret_cast<Name_resolution_context *>(cntx);
2802     return false;
2803   }
2804 
2805   bool sp_check_access(THD * thd);
functype()2806   virtual enum Functype functype() const { return FUNC_SP; }
2807 
2808   bool fix_fields(THD *thd, Item **ref);
2809   void fix_length_and_dec(void);
is_expensive()2810   bool is_expensive() { return true; }
2811 
get_sp_result_field()2812   inline Field *get_sp_result_field()
2813   {
2814     return sp_result_field;
2815   }
2816 
2817   virtual void update_null_value();
2818 
2819   /**
2820     Ensure that deterministic functions are not evaluated in preparation phase
2821     by returning false before tables are locked and true after they are locked.
2822     (can_be_evaluated_now() handles this because a function has the
2823     has_subquery() property).
2824 
2825      @retval true if tables are locked for deterministic functions
2826      @retval false Otherwise
2827   */
const_item()2828   bool const_item() const
2829   {
2830     if (used_tables() == 0)
2831       return can_be_evaluated_now();
2832     return false;
2833   }
2834 };
2835 
2836 
2837 class Item_func_found_rows :public Item_int_func
2838 {
2839   typedef Item_int_func super;
2840 public:
Item_func_found_rows(const POS & pos)2841   explicit Item_func_found_rows(const POS &pos) :Item_int_func(pos) {}
2842 
2843   virtual bool itemize(Parse_context *pc, Item **res);
2844   longlong val_int();
func_name()2845   const char *func_name() const { return "found_rows"; }
fix_length_and_dec()2846   void fix_length_and_dec() { decimals= 0; maybe_null=0; }
check_gcol_func_processor(uchar * int_arg)2847   bool check_gcol_func_processor(uchar *int_arg)
2848   { return true; }
2849 };
2850 
2851 
2852 void uuid_short_init();
2853 
2854 class Item_func_uuid_short :public Item_int_func
2855 {
2856   typedef Item_int_func super;
2857 public:
Item_func_uuid_short(const POS & pos)2858   Item_func_uuid_short(const POS &pos) :Item_int_func(pos) {}
2859 
2860   virtual bool itemize(Parse_context *pc, Item **res);
func_name()2861   const char *func_name() const { return "uuid_short"; }
2862   longlong val_int();
fix_length_and_dec()2863   void fix_length_and_dec()
2864   { max_length= 21; unsigned_flag=1; }
check_partition_func_processor(uchar * int_arg)2865   bool check_partition_func_processor(uchar *int_arg) {return false;}
check_gcol_func_processor(uchar * int_arg)2866   bool check_gcol_func_processor(uchar *int_arg)
2867   { return true; }
2868 };
2869 
2870 
2871 class Item_func_version : public Item_static_string_func
2872 {
2873   typedef Item_static_string_func super;
2874 public:
Item_func_version(const POS & pos)2875   explicit Item_func_version(const POS &pos)
2876     : Item_static_string_func(pos, NAME_STRING("version()"),
2877                               server_version,
2878                               strlen(server_version),
2879                               system_charset_info,
2880                               DERIVATION_SYSCONST)
2881   {}
2882 
2883   virtual bool itemize(Parse_context *pc, Item **res);
2884 };
2885 
2886 
2887 Item *get_system_var(Parse_context *pc, enum_var_type var_type, LEX_STRING name,
2888                      LEX_STRING component, bool unsafe);
2889 extern bool check_reserved_words(LEX_STRING *name);
2890 extern enum_field_types agg_field_type(Item **items, uint nitems);
2891 double my_double_round(double value, longlong dec, bool dec_unsigned,
2892                        bool truncate);
2893 bool eval_const_cond(THD *thd, Item *cond, bool *value);
2894 Item_field *get_gc_for_expr(Item_func **func, Field *fld, Item_result type);
2895 
2896 extern bool volatile  mqh_used;
2897 
2898 #endif /* MYSQL_SERVER */
2899 
2900 #endif /* ITEM_FUNC_INCLUDED */
2901