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