1
/* Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
/* Function items used by mysql */
19
#ifdef USE_PRAGMA_INTERFACE
20
#pragma interface /* gcc class implementation */
24
extern "C" /* Bug in BSDI include file */
30
class Item_func :public Item_result_field
33
Item **args, *tmp_arg[2];
35
Allowed numbers of columns in result (usually 1, which means scalar value)
36
0 means get this number from first argument
38
uint allowed_arg_cols;
41
table_map used_tables_cache, not_null_tables_cache;
42
bool const_item_cache;
43
enum Functype { UNKNOWN_FUNC,EQ_FUNC,EQUAL_FUNC,NE_FUNC,LT_FUNC,LE_FUNC,
44
GE_FUNC,GT_FUNC,FT_FUNC,
45
LIKE_FUNC,ISNULL_FUNC,ISNOTNULL_FUNC,
46
COND_AND_FUNC, COND_OR_FUNC, COND_XOR_FUNC,
47
BETWEEN, IN_FUNC, MULT_EQUAL_FUNC,
48
INTERVAL_FUNC, ISNOTNULLTEST_FUNC,
49
SP_EQUALS_FUNC, SP_DISJOINT_FUNC,SP_INTERSECTS_FUNC,
50
SP_TOUCHES_FUNC,SP_CROSSES_FUNC,SP_WITHIN_FUNC,
51
SP_CONTAINS_FUNC,SP_OVERLAPS_FUNC,
52
SP_STARTPOINT,SP_ENDPOINT,SP_EXTERIORRING,
53
SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN,
54
NOT_FUNC, NOT_ALL_FUNC,
55
NOW_FUNC, TRIG_COND_FUNC,
56
SUSERVAR_FUNC, GUSERVAR_FUNC, COLLATE_FUNC,
57
EXTRACT_FUNC, CHAR_TYPECAST_FUNC, FUNC_SP, UDF_FUNC,
58
NEG_FUNC, GSYSVAR_FUNC };
59
enum optimize_type { OPTIMIZE_NONE,OPTIMIZE_KEY,OPTIMIZE_OP, OPTIMIZE_NULL,
61
enum Type type() const { return FUNC_ITEM; }
62
virtual enum Functype functype() const { return UNKNOWN_FUNC; }
64
allowed_arg_cols(1), arg_count(0)
69
allowed_arg_cols(1), arg_count(1)
73
with_sum_func= a->with_sum_func;
75
Item_func(Item *a,Item *b):
76
allowed_arg_cols(1), arg_count(2)
79
args[0]= a; args[1]= b;
80
with_sum_func= a->with_sum_func || b->with_sum_func;
82
Item_func(Item *a,Item *b,Item *c):
86
if ((args= (Item**) sql_alloc(sizeof(Item*)*3)))
89
args[0]= a; args[1]= b; args[2]= c;
90
with_sum_func= a->with_sum_func || b->with_sum_func || c->with_sum_func;
93
Item_func(Item *a,Item *b,Item *c,Item *d):
97
if ((args= (Item**) sql_alloc(sizeof(Item*)*4)))
100
args[0]= a; args[1]= b; args[2]= c; args[3]= d;
101
with_sum_func= a->with_sum_func || b->with_sum_func ||
102
c->with_sum_func || d->with_sum_func;
105
Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
109
if ((args= (Item**) sql_alloc(sizeof(Item*)*5)))
111
args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
112
with_sum_func= a->with_sum_func || b->with_sum_func ||
113
c->with_sum_func || d->with_sum_func || e->with_sum_func ;
116
Item_func(List<Item> &list);
117
// Constructor used for Item_cond_and/or (see Item comment)
118
Item_func(THD *thd, Item_func *item);
119
bool fix_fields(THD *, Item **ref);
120
table_map used_tables() const;
121
table_map not_null_tables() const;
122
void update_used_tables();
123
bool eq(const Item *item, bool binary_cmp) const;
124
virtual optimize_type select_optimize() const { return OPTIMIZE_NONE; }
125
virtual bool have_rev_func() const { return 0; }
126
virtual Item *key_item() const { return args[0]; }
127
virtual bool const_item() const { return const_item_cache; }
128
inline Item **arguments() const { return args; }
129
void set_arguments(List<Item> &list);
130
inline uint argument_count() const { return arg_count; }
131
inline void remove_arguments() { arg_count=0; }
132
void split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields);
133
virtual void print(String *str, enum_query_type query_type);
134
void print_op(String *str, enum_query_type query_type);
135
void print_args(String *str, uint from, enum_query_type query_type);
136
virtual void fix_num_length_and_dec();
137
void count_only_length();
138
void count_real_length();
139
void count_decimal_length();
140
inline bool get_arg0_date(MYSQL_TIME *ltime, uint fuzzy_date)
142
return (null_value=args[0]->get_date(ltime, fuzzy_date));
144
inline bool get_arg0_time(MYSQL_TIME *ltime)
146
return (null_value=args[0]->get_time(ltime));
152
void signal_divide_by_null();
153
friend class udf_handler;
154
Field *tmp_table_field() { return result_field; }
155
Field *tmp_table_field(TABLE *t_arg);
156
Item *get_tmp_table_item(THD *thd);
158
my_decimal *val_decimal(my_decimal *);
160
bool agg_arg_collations(DTCollation &c, Item **items, uint nitems,
163
return agg_item_collations(c, func_name(), items, nitems, flags, 1);
165
bool agg_arg_collations_for_comparison(DTCollation &c,
166
Item **items, uint nitems,
169
return agg_item_collations_for_comparison(c, func_name(),
170
items, nitems, flags);
172
bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems,
173
uint flags, int item_sep)
175
return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep);
177
bool walk(Item_processor processor, bool walk_subquery, uchar *arg);
178
Item *transform(Item_transformer transformer, uchar *arg);
179
Item* compile(Item_analyzer analyzer, uchar **arg_p,
180
Item_transformer transformer, uchar *arg_t);
181
void traverse_cond(Cond_traverser traverser,
182
void * arg, traverse_order order);
183
bool is_expensive_processor(uchar *arg);
184
virtual bool is_expensive() { return 0; }
185
inline double fix_result(double value)
192
bool has_timestamp_args()
194
DBUG_ASSERT(fixed == TRUE);
195
for (uint i= 0; i < arg_count; i++)
197
if (args[i]->type() == Item::FIELD_ITEM &&
198
args[i]->field_type() == MYSQL_TYPE_TIMESTAMP)
204
We assume the result of any function that has a TIMESTAMP argument to be
205
timezone-dependent, since a TIMESTAMP value in both numeric and string
206
contexts is interpreted according to the current timezone.
207
The only exception is UNIX_TIMESTAMP() which returns the internal
208
representation of a TIMESTAMP argument verbatim, and thus does not depend on
211
virtual bool is_timezone_dependent_processor(uchar *bool_arg)
213
return has_timestamp_args();
216
virtual bool find_function_processor (uchar *arg)
218
return functype() == *(Functype *) arg;
223
class Item_real_func :public Item_func
226
Item_real_func() :Item_func() {}
227
Item_real_func(Item *a) :Item_func(a) {}
228
Item_real_func(Item *a,Item *b) :Item_func(a,b) {}
229
Item_real_func(List<Item> &list) :Item_func(list) {}
230
String *val_str(String*str);
231
my_decimal *val_decimal(my_decimal *decimal_value);
233
{ DBUG_ASSERT(fixed == 1); return (longlong) rint(val_real()); }
234
enum Item_result result_type () const { return REAL_RESULT; }
235
void fix_length_and_dec()
236
{ decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
240
class Item_func_numhybrid: public Item_func
243
Item_result hybrid_type;
245
Item_func_numhybrid(Item *a) :Item_func(a), hybrid_type(REAL_RESULT)
247
Item_func_numhybrid(Item *a,Item *b)
248
:Item_func(a,b), hybrid_type(REAL_RESULT)
250
Item_func_numhybrid(List<Item> &list)
251
:Item_func(list), hybrid_type(REAL_RESULT)
254
enum Item_result result_type () const { return hybrid_type; }
255
void fix_length_and_dec();
256
void fix_num_length_and_dec();
257
virtual void find_num_type()= 0; /* To be called from fix_length_and_dec */
261
my_decimal *val_decimal(my_decimal *);
262
String *val_str(String*str);
265
@brief Performs the operation that this functions implements when the
268
@return The result of the operation.
270
virtual longlong int_op()= 0;
273
@brief Performs the operation that this functions implements when the
276
@return The result of the operation.
278
virtual double real_op()= 0;
281
@brief Performs the operation that this functions implements when the
282
result type is DECIMAL.
284
@param A pointer where the DECIMAL value will be allocated.
286
- 0 If the result is NULL
287
- The same pointer it was given, with the area initialized to the
288
result of the operation.
290
virtual my_decimal *decimal_op(my_decimal *)= 0;
293
@brief Performs the operation that this functions implements when the
294
result type is a string type.
296
@return The result of the operation.
298
virtual String *str_op(String *)= 0;
299
bool is_null() { update_null_value(); return null_value; }
302
/* function where type of result detected by first argument */
303
class Item_func_num1: public Item_func_numhybrid
306
Item_func_num1(Item *a) :Item_func_numhybrid(a) {}
307
Item_func_num1(Item *a, Item *b) :Item_func_numhybrid(a, b) {}
309
void fix_num_length_and_dec();
310
void find_num_type();
311
String *str_op(String *str) { DBUG_ASSERT(0); return 0; }
315
/* Base class for operations like '+', '-', '*' */
316
class Item_num_op :public Item_func_numhybrid
319
Item_num_op(Item *a,Item *b) :Item_func_numhybrid(a, b) {}
320
virtual void result_precision()= 0;
322
virtual inline void print(String *str, enum_query_type query_type)
324
print_op(str, query_type);
327
void find_num_type();
328
String *str_op(String *str) { DBUG_ASSERT(0); return 0; }
332
class Item_int_func :public Item_func
335
Item_int_func() :Item_func() { max_length= 21; }
336
Item_int_func(Item *a) :Item_func(a) { max_length= 21; }
337
Item_int_func(Item *a,Item *b) :Item_func(a,b) { max_length= 21; }
338
Item_int_func(Item *a,Item *b,Item *c) :Item_func(a,b,c)
340
Item_int_func(List<Item> &list) :Item_func(list) { max_length= 21; }
341
Item_int_func(THD *thd, Item_int_func *item) :Item_func(thd, item) {}
343
String *val_str(String*str);
344
enum Item_result result_type () const { return INT_RESULT; }
345
void fix_length_and_dec() {}
349
class Item_func_connection_id :public Item_int_func
354
Item_func_connection_id() {}
355
const char *func_name() const { return "connection_id"; }
356
void fix_length_and_dec();
357
bool fix_fields(THD *thd, Item **ref);
358
longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
362
class Item_func_signed :public Item_int_func
365
Item_func_signed(Item *a) :Item_int_func(a) {}
366
const char *func_name() const { return "cast_as_signed"; }
368
longlong val_int_from_str(int *error);
369
void fix_length_and_dec()
370
{ max_length=args[0]->max_length; unsigned_flag=0; }
371
virtual void print(String *str, enum_query_type query_type);
372
uint decimal_precision() const { return args[0]->decimal_precision(); }
376
class Item_func_unsigned :public Item_func_signed
379
Item_func_unsigned(Item *a) :Item_func_signed(a) {}
380
const char *func_name() const { return "cast_as_unsigned"; }
381
void fix_length_and_dec()
383
max_length= min(args[0]->max_length, DECIMAL_MAX_PRECISION + 2);
387
virtual void print(String *str, enum_query_type query_type);
391
class Item_decimal_typecast :public Item_func
393
my_decimal decimal_value;
395
Item_decimal_typecast(Item *a, int len, int dec) :Item_func(a)
398
max_length= my_decimal_precision_to_length_no_truncation(len, dec,
401
String *val_str(String *str);
404
my_decimal *val_decimal(my_decimal*);
405
enum Item_result result_type () const { return DECIMAL_RESULT; }
406
enum_field_types field_type() const { return MYSQL_TYPE_NEWDECIMAL; }
407
void fix_length_and_dec() {};
408
const char *func_name() const { return "decimal_typecast"; }
409
virtual void print(String *str, enum_query_type query_type);
413
class Item_func_additive_op :public Item_num_op
416
Item_func_additive_op(Item *a,Item *b) :Item_num_op(a,b) {}
417
void result_precision();
418
bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
422
class Item_func_plus :public Item_func_additive_op
425
Item_func_plus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
426
const char *func_name() const { return "+"; }
429
my_decimal *decimal_op(my_decimal *);
432
class Item_func_minus :public Item_func_additive_op
435
Item_func_minus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
436
const char *func_name() const { return "-"; }
439
my_decimal *decimal_op(my_decimal *);
440
void fix_length_and_dec();
444
class Item_func_mul :public Item_num_op
447
Item_func_mul(Item *a,Item *b) :Item_num_op(a,b) {}
448
const char *func_name() const { return "*"; }
451
my_decimal *decimal_op(my_decimal *);
452
void result_precision();
453
bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
457
class Item_func_div :public Item_num_op
461
Item_func_div(Item *a,Item *b) :Item_num_op(a,b) {}
462
longlong int_op() { DBUG_ASSERT(0); return 0; }
464
my_decimal *decimal_op(my_decimal *);
465
const char *func_name() const { return "/"; }
466
void fix_length_and_dec();
467
void result_precision();
471
class Item_func_int_div :public Item_int_func
474
Item_func_int_div(Item *a,Item *b) :Item_int_func(a,b)
477
const char *func_name() const { return "DIV"; }
478
void fix_length_and_dec();
480
virtual inline void print(String *str, enum_query_type query_type)
482
print_op(str, query_type);
485
bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
489
class Item_func_mod :public Item_num_op
492
Item_func_mod(Item *a,Item *b) :Item_num_op(a,b) {}
495
my_decimal *decimal_op(my_decimal *);
496
const char *func_name() const { return "%"; }
497
void result_precision();
498
void fix_length_and_dec();
499
bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
503
class Item_func_neg :public Item_func_num1
506
Item_func_neg(Item *a) :Item_func_num1(a) {}
509
my_decimal *decimal_op(my_decimal *);
510
const char *func_name() const { return "-"; }
511
enum Functype functype() const { return NEG_FUNC; }
512
void fix_length_and_dec();
513
void fix_num_length_and_dec();
514
uint decimal_precision() const { return args[0]->decimal_precision(); }
515
bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
519
class Item_func_abs :public Item_func_num1
522
Item_func_abs(Item *a) :Item_func_num1(a) {}
525
my_decimal *decimal_op(my_decimal *);
526
const char *func_name() const { return "abs"; }
527
void fix_length_and_dec();
528
bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
531
// A class to handle logarithmic and trigonometric functions
533
class Item_dec_func :public Item_real_func
536
Item_dec_func(Item *a) :Item_real_func(a) {}
537
Item_dec_func(Item *a,Item *b) :Item_real_func(a,b) {}
538
void fix_length_and_dec()
540
decimals=NOT_FIXED_DEC; max_length=float_length(decimals);
545
class Item_func_exp :public Item_dec_func
548
Item_func_exp(Item *a) :Item_dec_func(a) {}
550
const char *func_name() const { return "exp"; }
554
class Item_func_ln :public Item_dec_func
557
Item_func_ln(Item *a) :Item_dec_func(a) {}
559
const char *func_name() const { return "ln"; }
563
class Item_func_log :public Item_dec_func
566
Item_func_log(Item *a) :Item_dec_func(a) {}
567
Item_func_log(Item *a,Item *b) :Item_dec_func(a,b) {}
569
const char *func_name() const { return "log"; }
573
class Item_func_log2 :public Item_dec_func
576
Item_func_log2(Item *a) :Item_dec_func(a) {}
578
const char *func_name() const { return "log2"; }
582
class Item_func_log10 :public Item_dec_func
585
Item_func_log10(Item *a) :Item_dec_func(a) {}
587
const char *func_name() const { return "log10"; }
591
class Item_func_sqrt :public Item_dec_func
594
Item_func_sqrt(Item *a) :Item_dec_func(a) {}
596
const char *func_name() const { return "sqrt"; }
600
class Item_func_pow :public Item_dec_func
603
Item_func_pow(Item *a,Item *b) :Item_dec_func(a,b) {}
605
const char *func_name() const { return "pow"; }
609
class Item_func_acos :public Item_dec_func
612
Item_func_acos(Item *a) :Item_dec_func(a) {}
614
const char *func_name() const { return "acos"; }
617
class Item_func_asin :public Item_dec_func
620
Item_func_asin(Item *a) :Item_dec_func(a) {}
622
const char *func_name() const { return "asin"; }
625
class Item_func_atan :public Item_dec_func
628
Item_func_atan(Item *a) :Item_dec_func(a) {}
629
Item_func_atan(Item *a,Item *b) :Item_dec_func(a,b) {}
631
const char *func_name() const { return "atan"; }
634
class Item_func_cos :public Item_dec_func
637
Item_func_cos(Item *a) :Item_dec_func(a) {}
639
const char *func_name() const { return "cos"; }
642
class Item_func_sin :public Item_dec_func
645
Item_func_sin(Item *a) :Item_dec_func(a) {}
647
const char *func_name() const { return "sin"; }
650
class Item_func_tan :public Item_dec_func
653
Item_func_tan(Item *a) :Item_dec_func(a) {}
655
const char *func_name() const { return "tan"; }
658
class Item_func_integer :public Item_int_func
661
inline Item_func_integer(Item *a) :Item_int_func(a) {}
662
void fix_length_and_dec();
666
class Item_func_int_val :public Item_func_num1
669
Item_func_int_val(Item *a) :Item_func_num1(a) {}
670
void fix_num_length_and_dec();
671
void find_num_type();
675
class Item_func_ceiling :public Item_func_int_val
678
Item_func_ceiling(Item *a) :Item_func_int_val(a) {}
679
const char *func_name() const { return "ceiling"; }
682
my_decimal *decimal_op(my_decimal *);
683
bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
687
class Item_func_floor :public Item_func_int_val
690
Item_func_floor(Item *a) :Item_func_int_val(a) {}
691
const char *func_name() const { return "floor"; }
694
my_decimal *decimal_op(my_decimal *);
695
bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
698
/* This handles round and truncate */
700
class Item_func_round :public Item_func_num1
704
Item_func_round(Item *a, Item *b, bool trunc_arg)
705
:Item_func_num1(a,b), truncate(trunc_arg) {}
706
const char *func_name() const { return truncate ? "truncate" : "round"; }
709
my_decimal *decimal_op(my_decimal *);
710
void fix_length_and_dec();
714
class Item_func_rand :public Item_real_func
716
struct rand_struct *rand;
717
bool first_eval; // TRUE if val_real() is called 1st time
719
Item_func_rand(Item *a) :Item_real_func(a), rand(0), first_eval(TRUE) {}
720
Item_func_rand() :Item_real_func() {}
722
const char *func_name() const { return "rand"; }
723
bool const_item() const { return 0; }
724
void update_used_tables();
725
bool fix_fields(THD *thd, Item **ref);
726
void cleanup() { first_eval= TRUE; Item_real_func::cleanup(); }
728
void seed_random (Item * val);
732
class Item_func_sign :public Item_int_func
735
Item_func_sign(Item *a) :Item_int_func(a) {}
736
const char *func_name() const { return "sign"; }
741
class Item_func_units :public Item_real_func
746
Item_func_units(char *name_arg,Item *a,double mul_arg,double add_arg)
747
:Item_real_func(a),name(name_arg),mul(mul_arg),add(add_arg) {}
749
const char *func_name() const { return name; }
750
void fix_length_and_dec()
751
{ decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
755
class Item_func_min_max :public Item_func
757
Item_result cmp_type;
760
/* TRUE <=> arguments should be compared in the DATETIME context. */
761
bool compare_as_dates;
762
/* An item used for issuing warnings while string to DATETIME conversion. */
766
enum_field_types cached_field_type;
768
Item_func_min_max(List<Item> &list,int cmp_sign_arg) :Item_func(list),
769
cmp_type(INT_RESULT), cmp_sign(cmp_sign_arg), compare_as_dates(FALSE),
773
String *val_str(String *);
774
my_decimal *val_decimal(my_decimal *);
775
void fix_length_and_dec();
776
enum Item_result result_type () const { return cmp_type; }
777
bool result_as_longlong() { return compare_as_dates; };
778
uint cmp_datetimes(ulonglong *value);
779
enum_field_types field_type() const { return cached_field_type; }
782
class Item_func_min :public Item_func_min_max
785
Item_func_min(List<Item> &list) :Item_func_min_max(list,1) {}
786
const char *func_name() const { return "least"; }
789
class Item_func_max :public Item_func_min_max
792
Item_func_max(List<Item> &list) :Item_func_min_max(list,-1) {}
793
const char *func_name() const { return "greatest"; }
798
Objects of this class are used for ROLLUP queries to wrap up
799
each constant item referred to in GROUP BY list.
802
class Item_func_rollup_const :public Item_func
805
Item_func_rollup_const(Item *a) :Item_func(a)
808
name_length= a->name_length;
810
double val_real() { return args[0]->val_real(); }
811
longlong val_int() { return args[0]->val_int(); }
812
String *val_str(String *str) { return args[0]->val_str(str); }
813
my_decimal *val_decimal(my_decimal *dec) { return args[0]->val_decimal(dec); }
814
const char *func_name() const { return "rollup_const"; }
815
bool const_item() const { return 0; }
816
Item_result result_type() const { return args[0]->result_type(); }
817
void fix_length_and_dec()
819
collation= args[0]->collation;
820
max_length= args[0]->max_length;
821
decimals=args[0]->decimals;
822
/* The item could be a NULL constant. */
823
null_value= args[0]->is_null();
828
class Item_func_length :public Item_int_func
832
Item_func_length(Item *a) :Item_int_func(a) {}
834
const char *func_name() const { return "length"; }
835
void fix_length_and_dec() { max_length=10; }
838
class Item_func_bit_length :public Item_func_length
841
Item_func_bit_length(Item *a) :Item_func_length(a) {}
843
{ DBUG_ASSERT(fixed == 1); return Item_func_length::val_int()*8; }
844
const char *func_name() const { return "bit_length"; }
847
class Item_func_char_length :public Item_int_func
851
Item_func_char_length(Item *a) :Item_int_func(a) {}
853
const char *func_name() const { return "char_length"; }
854
void fix_length_and_dec() { max_length=10; }
857
class Item_func_coercibility :public Item_int_func
860
Item_func_coercibility(Item *a) :Item_int_func(a) {}
862
const char *func_name() const { return "coercibility"; }
863
void fix_length_and_dec() { max_length=10; maybe_null= 0; }
864
table_map not_null_tables() const { return 0; }
867
class Item_func_locate :public Item_int_func
869
String value1,value2;
870
DTCollation cmp_collation;
872
Item_func_locate(Item *a,Item *b) :Item_int_func(a,b) {}
873
Item_func_locate(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
874
const char *func_name() const { return "locate"; }
876
void fix_length_and_dec();
877
virtual void print(String *str, enum_query_type query_type);
881
class Item_func_field :public Item_int_func
884
Item_result cmp_type;
885
DTCollation cmp_collation;
887
Item_func_field(List<Item> &list) :Item_int_func(list) {}
889
const char *func_name() const { return "field"; }
890
void fix_length_and_dec();
894
class Item_func_ascii :public Item_int_func
898
Item_func_ascii(Item *a) :Item_int_func(a) {}
900
const char *func_name() const { return "ascii"; }
901
void fix_length_and_dec() { max_length=3; }
904
class Item_func_ord :public Item_int_func
908
Item_func_ord(Item *a) :Item_int_func(a) {}
910
const char *func_name() const { return "ord"; }
913
class Item_func_find_in_set :public Item_int_func
918
DTCollation cmp_collation;
920
Item_func_find_in_set(Item *a,Item *b) :Item_int_func(a,b),enum_value(0) {}
922
const char *func_name() const { return "find_in_set"; }
923
void fix_length_and_dec();
926
/* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
928
class Item_func_bit: public Item_int_func
931
Item_func_bit(Item *a, Item *b) :Item_int_func(a, b) {}
932
Item_func_bit(Item *a) :Item_int_func(a) {}
933
void fix_length_and_dec() { unsigned_flag= 1; }
935
virtual inline void print(String *str, enum_query_type query_type)
937
print_op(str, query_type);
941
class Item_func_bit_or :public Item_func_bit
944
Item_func_bit_or(Item *a, Item *b) :Item_func_bit(a, b) {}
946
const char *func_name() const { return "|"; }
949
class Item_func_bit_and :public Item_func_bit
952
Item_func_bit_and(Item *a, Item *b) :Item_func_bit(a, b) {}
954
const char *func_name() const { return "&"; }
957
class Item_func_bit_count :public Item_int_func
960
Item_func_bit_count(Item *a) :Item_int_func(a) {}
962
const char *func_name() const { return "bit_count"; }
963
void fix_length_and_dec() { max_length=2; }
966
class Item_func_shift_left :public Item_func_bit
969
Item_func_shift_left(Item *a, Item *b) :Item_func_bit(a, b) {}
971
const char *func_name() const { return "<<"; }
974
class Item_func_shift_right :public Item_func_bit
977
Item_func_shift_right(Item *a, Item *b) :Item_func_bit(a, b) {}
979
const char *func_name() const { return ">>"; }
982
class Item_func_bit_neg :public Item_func_bit
985
Item_func_bit_neg(Item *a) :Item_func_bit(a) {}
987
const char *func_name() const { return "~"; }
989
virtual inline void print(String *str, enum_query_type query_type)
991
Item_func::print(str, query_type);
996
class Item_func_last_insert_id :public Item_int_func
999
Item_func_last_insert_id() :Item_int_func() {}
1000
Item_func_last_insert_id(Item *a) :Item_int_func(a) {}
1002
const char *func_name() const { return "last_insert_id"; }
1003
void fix_length_and_dec()
1006
max_length= args[0]->max_length;
1008
bool fix_fields(THD *thd, Item **ref);
1012
class Item_func_benchmark :public Item_int_func
1015
Item_func_benchmark(Item *count_expr, Item *expr)
1016
:Item_int_func(count_expr, expr)
1019
const char *func_name() const { return "benchmark"; }
1020
void fix_length_and_dec() { max_length=1; maybe_null=0; }
1021
virtual void print(String *str, enum_query_type query_type);
1025
class Item_func_sleep :public Item_int_func
1028
Item_func_sleep(Item *a) :Item_int_func(a) {}
1029
bool const_item() const { return 0; }
1030
const char *func_name() const { return "sleep"; }
1031
void update_used_tables()
1033
Item_int_func::update_used_tables();
1034
used_tables_cache|= RAND_TABLE_BIT;
1043
class Item_udf_func :public Item_func
1049
Item_udf_func(udf_func *udf_arg)
1050
:Item_func(), udf(udf_arg) {}
1051
Item_udf_func(udf_func *udf_arg, List<Item> &list)
1052
:Item_func(list), udf(udf_arg) {}
1053
const char *func_name() const { return udf.name(); }
1054
enum Functype functype() const { return UDF_FUNC; }
1055
bool fix_fields(THD *thd, Item **ref)
1057
DBUG_ASSERT(fixed == 0);
1058
bool res= udf.fix_fields(thd, this, arg_count, args);
1059
used_tables_cache= udf.used_tables_cache;
1060
const_item_cache= udf.const_item_cache;
1064
void update_used_tables()
1067
TODO: Make a member in UDF_INIT and return if a UDF is deterministic or
1069
Currently UDF_INIT has a member (const_item) that is an in/out
1070
parameter to the init() call.
1071
The code in udf_handler::fix_fields also duplicates the arguments
1072
handling code in Item_func::fix_fields().
1074
The lack of information if a UDF is deterministic makes writing
1075
a correct update_used_tables() for UDFs impossible.
1076
One solution to this would be :
1077
- Add a is_deterministic member of UDF_INIT
1078
- (optionally) deprecate the const_item member of UDF_INIT
1079
- Take away the duplicate code from udf_handler::fix_fields() and
1080
make Item_udf_func call Item_func::fix_fields() to process its
1081
arguments as for any other function.
1082
- Store the deterministic flag returned by <udf>_init into the
1084
- Don't implement Item_udf_func::fix_fields, implement
1085
Item_udf_func::fix_length_and_dec() instead (similar to non-UDF
1087
- Override Item_func::update_used_tables to call
1088
Item_func::update_used_tables() and add a RAND_TABLE_BIT to the
1089
result of Item_func::update_used_tables() if the UDF is
1091
- (optionally) rename RAND_TABLE_BIT to NONDETERMINISTIC_BIT to
1092
better describe its usage.
1094
The above would require a change of the UDF API.
1095
Until that change is done here's how the current code works:
1096
We call Item_func::update_used_tables() only when we know that
1097
the function depends on real non-const tables and is deterministic.
1098
This can be done only because we know that the optimizer will
1099
call update_used_tables() only when there's possibly a new const
1100
table. So update_used_tables() can only make a Item_func more
1101
constant than it is currently.
1102
That's why we don't need to do anything if a function is guaranteed
1103
to return non-constant (it's non-deterministic) or is already a
1106
if ((used_tables_cache & ~PSEUDO_TABLE_BITS) &&
1107
!(used_tables_cache & RAND_TABLE_BIT))
1109
Item_func::update_used_tables();
1110
if (!const_item_cache && !used_tables_cache)
1111
used_tables_cache= RAND_TABLE_BIT;
1115
Item_result result_type () const { return udf.result_type(); }
1116
table_map not_null_tables() const { return 0; }
1117
bool is_expensive() { return 1; }
1118
virtual void print(String *str, enum_query_type query_type);
1122
class Item_func_udf_float :public Item_udf_func
1125
Item_func_udf_float(udf_func *udf_arg)
1126
:Item_udf_func(udf_arg) {}
1127
Item_func_udf_float(udf_func *udf_arg,
1129
:Item_udf_func(udf_arg, list) {}
1132
DBUG_ASSERT(fixed == 1);
1133
return (longlong) rint(Item_func_udf_float::val_real());
1135
my_decimal *val_decimal(my_decimal *dec_buf)
1137
double res=val_real();
1140
double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
1144
String *val_str(String *str);
1145
void fix_length_and_dec() { fix_num_length_and_dec(); }
1149
class Item_func_udf_int :public Item_udf_func
1152
Item_func_udf_int(udf_func *udf_arg)
1153
:Item_udf_func(udf_arg) {}
1154
Item_func_udf_int(udf_func *udf_arg,
1156
:Item_udf_func(udf_arg, list) {}
1158
double val_real() { return (double) Item_func_udf_int::val_int(); }
1159
String *val_str(String *str);
1160
enum Item_result result_type () const { return INT_RESULT; }
1161
void fix_length_and_dec() { decimals= 0; max_length= 21; }
1165
class Item_func_udf_decimal :public Item_udf_func
1168
Item_func_udf_decimal(udf_func *udf_arg)
1169
:Item_udf_func(udf_arg) {}
1170
Item_func_udf_decimal(udf_func *udf_arg, List<Item> &list)
1171
:Item_udf_func(udf_arg, list) {}
1174
my_decimal *val_decimal(my_decimal *);
1175
String *val_str(String *str);
1176
enum Item_result result_type () const { return DECIMAL_RESULT; }
1177
void fix_length_and_dec();
1181
class Item_func_udf_str :public Item_udf_func
1184
Item_func_udf_str(udf_func *udf_arg)
1185
:Item_udf_func(udf_arg) {}
1186
Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
1187
:Item_udf_func(udf_arg, list) {}
1188
String *val_str(String *);
1194
res= val_str(&str_value);
1195
return res ? my_strntod(res->charset(),(char*) res->ptr(),
1196
res->length(), &end_not_used, &err_not_used) : 0.0;
1201
String *res; res=val_str(&str_value);
1202
return res ? my_strntoll(res->charset(),res->ptr(),res->length(),10,
1203
(char**) 0, &err_not_used) : (longlong) 0;
1205
my_decimal *val_decimal(my_decimal *dec_buf)
1207
String *res=val_str(&str_value);
1210
string2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
1213
enum Item_result result_type () const { return STRING_RESULT; }
1214
void fix_length_and_dec();
1217
#else /* Dummy functions to get sql_yacc.cc compiled */
1219
class Item_func_udf_float :public Item_real_func
1222
Item_func_udf_float(udf_func *udf_arg)
1223
:Item_real_func() {}
1224
Item_func_udf_float(udf_func *udf_arg, List<Item> &list)
1225
:Item_real_func(list) {}
1226
double val_real() { DBUG_ASSERT(fixed == 1); return 0.0; }
1230
class Item_func_udf_int :public Item_int_func
1233
Item_func_udf_int(udf_func *udf_arg)
1235
Item_func_udf_int(udf_func *udf_arg, List<Item> &list)
1236
:Item_int_func(list) {}
1237
longlong val_int() { DBUG_ASSERT(fixed == 1); return 0; }
1241
class Item_func_udf_decimal :public Item_int_func
1244
Item_func_udf_decimal(udf_func *udf_arg)
1246
Item_func_udf_decimal(udf_func *udf_arg, List<Item> &list)
1247
:Item_int_func(list) {}
1248
my_decimal *val_decimal(my_decimal *) { DBUG_ASSERT(fixed == 1); return 0; }
1252
class Item_func_udf_str :public Item_func
1255
Item_func_udf_str(udf_func *udf_arg)
1257
Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
1259
String *val_str(String *)
1260
{ DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
1261
double val_real() { DBUG_ASSERT(fixed == 1); null_value= 1; return 0.0; }
1262
longlong val_int() { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
1263
enum Item_result result_type () const { return STRING_RESULT; }
1264
void fix_length_and_dec() { maybe_null=1; max_length=0; }
1267
#endif /* HAVE_DLOPEN */
1273
class User_level_lock;
1274
void item_user_lock_init(void);
1275
void item_user_lock_release(User_level_lock *ull);
1276
void item_user_lock_free(void);
1278
class Item_func_get_lock :public Item_int_func
1282
Item_func_get_lock(Item *a,Item *b) :Item_int_func(a,b) {}
1284
const char *func_name() const { return "get_lock"; }
1285
void fix_length_and_dec() { max_length=1; maybe_null=1;}
1288
class Item_func_release_lock :public Item_int_func
1292
Item_func_release_lock(Item *a) :Item_int_func(a) {}
1294
const char *func_name() const { return "release_lock"; }
1295
void fix_length_and_dec() { max_length=1; maybe_null=1;}
1298
/* replication functions */
1300
class Item_master_pos_wait :public Item_int_func
1304
Item_master_pos_wait(Item *a,Item *b) :Item_int_func(a,b) {}
1305
Item_master_pos_wait(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
1307
const char *func_name() const { return "master_pos_wait"; }
1308
void fix_length_and_dec() { max_length=21; maybe_null=1;}
1312
/* Handling of user definable variables */
1314
class user_var_entry;
1316
class Item_func_set_user_var :public Item_func
1318
enum Item_result cached_result_type;
1319
user_var_entry *entry;
1321
The entry_thread_id variable is used:
1322
1) to skip unnecessary updates of the entry field (see above);
1323
2) to reset the entry field that was initialized in the other thread
1324
(for example, an item tree of a trigger that updates user variables
1325
may be shared between several connections, and the entry_thread_id field
1326
prevents updates of one connection user variables from a concurrent
1327
connection calling the same trigger that initially updated some
1328
user variable it the first connection context).
1330
my_thread_id entry_thread_id;
1331
char buffer[MAX_FIELD_WIDTH];
1333
my_decimal decimal_buff;
1344
LEX_STRING name; // keep it public
1345
Item_func_set_user_var(LEX_STRING a,Item *b)
1346
:Item_func(b), cached_result_type(INT_RESULT),
1347
entry(NULL), entry_thread_id(0), name(a)
1349
enum Functype functype() const { return SUSERVAR_FUNC; }
1352
String *val_str(String *str);
1353
my_decimal *val_decimal(my_decimal *);
1354
double val_result();
1355
longlong val_int_result();
1356
String *str_result(String *str);
1357
my_decimal *val_decimal_result(my_decimal *);
1358
bool is_null_result();
1359
bool update_hash(void *ptr, uint length, enum Item_result type,
1360
CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
1361
bool send(Protocol *protocol, String *str_arg);
1362
void make_field(Send_field *tmp_field);
1363
bool check(bool use_result_field);
1364
void save_item_result(Item *item);
1366
enum Item_result result_type () const { return cached_result_type; }
1367
bool fix_fields(THD *thd, Item **ref);
1368
void fix_length_and_dec();
1369
virtual void print(String *str, enum_query_type query_type);
1370
void print_as_stmt(String *str, enum_query_type query_type);
1371
const char *func_name() const { return "set_user_var"; }
1372
int save_in_field(Field *field, bool no_conversions,
1373
bool can_use_result_field);
1374
int save_in_field(Field *field, bool no_conversions)
1376
return save_in_field(field, no_conversions, 1);
1378
void save_org_in_field(Field *field) { (void)save_in_field(field, 1, 0); }
1379
bool register_field_in_read_map(uchar *arg);
1380
bool set_entry(THD *thd, bool create_if_not_exists);
1385
class Item_func_get_user_var :public Item_func,
1386
private Settable_routine_parameter
1388
user_var_entry *var_entry;
1389
Item_result m_cached_result_type;
1392
LEX_STRING name; // keep it public
1393
Item_func_get_user_var(LEX_STRING a):
1394
Item_func(), m_cached_result_type(STRING_RESULT), name(a) {}
1395
enum Functype functype() const { return GUSERVAR_FUNC; }
1396
LEX_STRING get_name() { return name; }
1399
my_decimal *val_decimal(my_decimal*);
1400
String *val_str(String* str);
1401
void fix_length_and_dec();
1402
virtual void print(String *str, enum_query_type query_type);
1403
enum Item_result result_type() const;
1405
We must always return variables as strings to guard against selects of type
1406
select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
1408
const char *func_name() const { return "get_user_var"; }
1409
bool const_item() const;
1410
table_map used_tables() const
1411
{ return const_item() ? 0 : RAND_TABLE_BIT; }
1412
bool eq(const Item *item, bool binary_cmp) const;
1414
bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
1417
Settable_routine_parameter *get_settable_routine_parameter()
1425
This item represents user variable used as out parameter (e.g in LOAD DATA),
1426
and it is supposed to be used only for this purprose. So it is simplified
1427
a lot. Actually you should never obtain its value.
1429
The only two reasons for this thing being an Item is possibility to store it
1430
in List<Item> and desire to place this code somewhere near other functions
1431
working with user variables.
1433
class Item_user_var_as_out_param :public Item
1436
user_var_entry *entry;
1438
Item_user_var_as_out_param(LEX_STRING a) : name(a) {}
1439
/* We should return something different from FIELD_ITEM here */
1440
enum Type type() const { return STRING_ITEM;}
1443
String *val_str(String *str);
1444
my_decimal *val_decimal(my_decimal *decimal_buffer);
1445
/* fix_fields() binds variable name with its entry structure */
1446
bool fix_fields(THD *thd, Item **ref);
1447
virtual void print(String *str, enum_query_type query_type);
1448
void set_null_value(CHARSET_INFO* cs);
1449
void set_value(const char *str, uint length, CHARSET_INFO* cs);
1453
/* A system variable */
1455
#define GET_SYS_VAR_CACHE_LONG 1
1456
#define GET_SYS_VAR_CACHE_DOUBLE 2
1457
#define GET_SYS_VAR_CACHE_STRING 4
1459
class Item_func_get_system_var :public Item_func
1462
enum_var_type var_type, orig_var_type;
1463
LEX_STRING component;
1464
longlong cached_llval;
1466
String cached_strval;
1467
my_bool cached_null_value;
1468
query_id_t used_query_id;
1469
uchar cache_present;
1472
Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
1473
LEX_STRING *component_arg, const char *name_arg,
1474
size_t name_len_arg);
1475
enum Functype functype() const { return GSYSVAR_FUNC; }
1476
void update_null_value();
1477
void fix_length_and_dec();
1478
void print(String *str, enum_query_type query_type);
1479
bool const_item() const { return true; }
1480
table_map used_tables() const { return 0; }
1481
enum Item_result result_type() const;
1482
enum_field_types field_type() const;
1485
String* val_str(String*);
1486
/* TODO: fix to support views */
1487
const char *func_name() const { return "get_system_var"; }
1489
Indicates whether this system variable is written to the binlog or not.
1491
Variables are written to the binlog as part of "status_vars" in
1492
Query_log_event, as an Intvar_log_event, or a Rand_log_event.
1494
@return true if the variable is written to the binlog, false otherwise.
1496
bool is_written_to_binlog();
1497
bool eq(const Item *item, bool binary_cmp) const;
1503
class Item_func_inet_aton : public Item_int_func
1506
Item_func_inet_aton(Item *a) :Item_int_func(a) {}
1508
const char *func_name() const { return "inet_aton"; }
1509
void fix_length_and_dec() { decimals= 0; max_length= 21; maybe_null= 1; unsigned_flag= 1;}
1513
/* for fulltext search */
1514
#include <ft_global.h>
1516
class Item_func_match :public Item_real_func
1521
DTCollation cmp_collation;
1522
FT_INFO *ft_handler;
1524
Item_func_match *master; // for master-slave optimization
1525
Item *concat_ws; // Item_func_concat_ws
1526
String value; // value of concat_ws
1527
String search_value; // key_item()'s value converted to cmp_collation
1529
Item_func_match(List<Item> &a, uint b): Item_real_func(a), key(0), flags(b),
1530
join_key(0), ft_handler(0), table(0), master(0), concat_ws(0) { }
1533
DBUG_ENTER("Item_func_match");
1534
Item_real_func::cleanup();
1535
if (!master && ft_handler)
1536
ft_handler->please->close_search(ft_handler);
1539
table= 0; // required by Item_func_match::eq()
1542
enum Functype functype() const { return FT_FUNC; }
1543
const char *func_name() const { return "match"; }
1544
void update_used_tables() {}
1545
table_map not_null_tables() const { return 0; }
1546
bool fix_fields(THD *thd, Item **ref);
1547
bool eq(const Item *, bool binary_cmp) const;
1548
/* The following should be safe, even if we compare doubles */
1549
longlong val_int() { DBUG_ASSERT(fixed == 1); return val_real() != 0.0; }
1551
virtual void print(String *str, enum_query_type query_type);
1554
void init_search(bool no_order);
1558
class Item_func_bit_xor : public Item_func_bit
1561
Item_func_bit_xor(Item *a, Item *b) :Item_func_bit(a, b) {}
1563
const char *func_name() const { return "^"; }
1566
class Item_func_is_free_lock :public Item_int_func
1570
Item_func_is_free_lock(Item *a) :Item_int_func(a) {}
1572
const char *func_name() const { return "is_free_lock"; }
1573
void fix_length_and_dec() { decimals=0; max_length=1; maybe_null=1;}
1576
class Item_func_is_used_lock :public Item_int_func
1580
Item_func_is_used_lock(Item *a) :Item_int_func(a) {}
1582
const char *func_name() const { return "is_used_lock"; }
1583
void fix_length_and_dec() { decimals=0; max_length=10; maybe_null=1;}
1586
/* For type casts */
1590
ITEM_CAST_BINARY, ITEM_CAST_SIGNED_INT, ITEM_CAST_UNSIGNED_INT,
1591
ITEM_CAST_DATE, ITEM_CAST_TIME, ITEM_CAST_DATETIME, ITEM_CAST_CHAR,
1596
class Item_func_row_count :public Item_int_func
1599
Item_func_row_count() :Item_int_func() {}
1601
const char *func_name() const { return "row_count"; }
1602
void fix_length_and_dec() { decimals= 0; maybe_null=0; }
1614
struct st_sp_security_context;
1616
class Item_func_sp :public Item_func
1619
Name_resolution_context *context;
1621
mutable sp_head *m_sp;
1623
uchar result_buf[64];
1625
The result field of the concrete stored function.
1627
Field *sp_result_field;
1630
bool execute_impl(THD *thd);
1631
bool init_result_field(THD *thd);
1635
Item_func_sp(Name_resolution_context *context_arg, sp_name *name);
1637
Item_func_sp(Name_resolution_context *context_arg,
1638
sp_name *name, List<Item> &list);
1640
virtual ~Item_func_sp()
1643
void update_used_tables();
1647
const char *func_name() const;
1649
enum enum_field_types field_type() const;
1651
Field *tmp_table_field(TABLE *t_arg);
1653
void make_field(Send_field *tmp_field);
1655
Item_result result_type() const;
1660
return (longlong) 0;
1661
return sp_result_field->val_int();
1668
return sp_result_field->val_real();
1671
my_decimal *val_decimal(my_decimal *dec_buf)
1675
return sp_result_field->val_decimal(dec_buf);
1678
String *val_str(String *str)
1682
buf.set(buff, 20, str->charset());
1687
result_field will set buf pointing to internal buffer
1688
of the resul_field. Due to this it will change any time
1689
when SP is executed. In order to prevent occasional
1690
corruption of returned value, we make here a copy.
1692
sp_result_field->val_str(&buf);
1697
virtual bool change_context_processor(uchar *cntx)
1698
{ context= (Name_resolution_context *)cntx; return FALSE; }
1700
bool sp_check_access(THD * thd);
1701
virtual enum Functype functype() const { return FUNC_SP; }
1703
bool fix_fields(THD *thd, Item **ref);
1704
void fix_length_and_dec(void);
1705
bool is_expensive() { return 1; }
1707
inline Field *get_sp_result_field()
1709
return sp_result_field;
1714
class Item_func_found_rows :public Item_int_func
1717
Item_func_found_rows() :Item_int_func() {}
1719
const char *func_name() const { return "found_rows"; }
1720
void fix_length_and_dec() { decimals= 0; maybe_null=0; }
1724
void uuid_short_init();
1726
class Item_func_uuid_short :public Item_int_func
1729
Item_func_uuid_short() :Item_int_func() {}
1730
const char *func_name() const { return "uuid_short"; }
1732
void fix_length_and_dec()
1733
{ max_length= 21; unsigned_flag=1; }
1734
bool check_partition_func_processor(uchar *int_arg) {return FALSE;}