~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to sql/item_func.h

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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 */
 
15
 
 
16
 
 
17
/* Function items used by mysql */
 
18
 
 
19
#ifdef USE_PRAGMA_INTERFACE
 
20
#pragma interface                       /* gcc class implementation */
 
21
#endif
 
22
 
 
23
#ifdef HAVE_IEEEFP_H
 
24
extern "C"                              /* Bug in BSDI include file */
 
25
{
 
26
#include <ieeefp.h>
 
27
}
 
28
#endif
 
29
 
 
30
class Item_func :public Item_result_field
 
31
{
 
32
protected:
 
33
  Item **args, *tmp_arg[2];
 
34
  /*
 
35
    Allowed numbers of columns in result (usually 1, which means scalar value)
 
36
    0 means get this number from first argument
 
37
  */
 
38
  uint allowed_arg_cols;
 
39
public:
 
40
  uint arg_count;
 
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,
 
60
                       OPTIMIZE_EQUAL };
 
61
  enum Type type() const { return FUNC_ITEM; }
 
62
  virtual enum Functype functype() const   { return UNKNOWN_FUNC; }
 
63
  Item_func(void):
 
64
    allowed_arg_cols(1), arg_count(0)
 
65
  {
 
66
    with_sum_func= 0;
 
67
  }
 
68
  Item_func(Item *a):
 
69
    allowed_arg_cols(1), arg_count(1)
 
70
  {
 
71
    args= tmp_arg;
 
72
    args[0]= a;
 
73
    with_sum_func= a->with_sum_func;
 
74
  }
 
75
  Item_func(Item *a,Item *b):
 
76
    allowed_arg_cols(1), arg_count(2)
 
77
  {
 
78
    args= tmp_arg;
 
79
    args[0]= a; args[1]= b;
 
80
    with_sum_func= a->with_sum_func || b->with_sum_func;
 
81
  }
 
82
  Item_func(Item *a,Item *b,Item *c):
 
83
    allowed_arg_cols(1)
 
84
  {
 
85
    arg_count= 0;
 
86
    if ((args= (Item**) sql_alloc(sizeof(Item*)*3)))
 
87
    {
 
88
      arg_count= 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;
 
91
    }
 
92
  }
 
93
  Item_func(Item *a,Item *b,Item *c,Item *d):
 
94
    allowed_arg_cols(1)
 
95
  {
 
96
    arg_count= 0;
 
97
    if ((args= (Item**) sql_alloc(sizeof(Item*)*4)))
 
98
    {
 
99
      arg_count= 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;
 
103
    }
 
104
  }
 
105
  Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
 
106
    allowed_arg_cols(1)
 
107
  {
 
108
    arg_count= 5;
 
109
    if ((args= (Item**) sql_alloc(sizeof(Item*)*5)))
 
110
    {
 
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 ;
 
114
    }
 
115
  }
 
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)
 
141
  {
 
142
    return (null_value=args[0]->get_date(ltime, fuzzy_date));
 
143
  }
 
144
  inline bool get_arg0_time(MYSQL_TIME *ltime)
 
145
  {
 
146
    return (null_value=args[0]->get_time(ltime));
 
147
  }
 
148
  bool is_null() { 
 
149
    update_null_value();
 
150
    return null_value; 
 
151
  }
 
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);
 
157
 
 
158
  my_decimal *val_decimal(my_decimal *);
 
159
 
 
160
  bool agg_arg_collations(DTCollation &c, Item **items, uint nitems,
 
161
                          uint flags)
 
162
  {
 
163
    return agg_item_collations(c, func_name(), items, nitems, flags, 1);
 
164
  }
 
165
  bool agg_arg_collations_for_comparison(DTCollation &c,
 
166
                                         Item **items, uint nitems,
 
167
                                         uint flags)
 
168
  {
 
169
    return agg_item_collations_for_comparison(c, func_name(),
 
170
                                              items, nitems, flags);
 
171
  }
 
172
  bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems,
 
173
                        uint flags, int item_sep)
 
174
  {
 
175
    return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep);
 
176
  }
 
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)
 
186
  {
 
187
    if (isfinite(value))
 
188
      return value;
 
189
    null_value=1;
 
190
    return 0.0;
 
191
  }
 
192
  bool has_timestamp_args()
 
193
  {
 
194
    DBUG_ASSERT(fixed == TRUE);
 
195
    for (uint i= 0; i < arg_count; i++)
 
196
    {
 
197
      if (args[i]->type() == Item::FIELD_ITEM &&
 
198
          args[i]->field_type() == MYSQL_TYPE_TIMESTAMP)
 
199
        return TRUE;
 
200
    }
 
201
    return FALSE;
 
202
  }
 
203
  /*
 
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
 
209
    the timezone.
 
210
   */
 
211
  virtual bool is_timezone_dependent_processor(uchar *bool_arg)
 
212
  {
 
213
    return has_timestamp_args();
 
214
  }
 
215
 
 
216
  virtual bool find_function_processor (uchar *arg)
 
217
  {
 
218
    return functype() == *(Functype *) arg;
 
219
  }
 
220
};
 
221
 
 
222
 
 
223
class Item_real_func :public Item_func
 
224
{
 
225
public:
 
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);
 
232
  longlong val_int()
 
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); }
 
237
};
 
238
 
 
239
 
 
240
class Item_func_numhybrid: public Item_func
 
241
{
 
242
protected:
 
243
  Item_result hybrid_type;
 
244
public:
 
245
  Item_func_numhybrid(Item *a) :Item_func(a), hybrid_type(REAL_RESULT)
 
246
  {}
 
247
  Item_func_numhybrid(Item *a,Item *b)
 
248
    :Item_func(a,b), hybrid_type(REAL_RESULT)
 
249
  {}
 
250
  Item_func_numhybrid(List<Item> &list)
 
251
    :Item_func(list), hybrid_type(REAL_RESULT)
 
252
  {}
 
253
 
 
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 */
 
258
 
 
259
  double val_real();
 
260
  longlong val_int();
 
261
  my_decimal *val_decimal(my_decimal *);
 
262
  String *val_str(String*str);
 
263
 
 
264
  /**
 
265
     @brief Performs the operation that this functions implements when the
 
266
     result type is INT.
 
267
 
 
268
     @return The result of the operation.
 
269
  */
 
270
  virtual longlong int_op()= 0;
 
271
 
 
272
  /**
 
273
     @brief Performs the operation that this functions implements when the
 
274
     result type is REAL.
 
275
 
 
276
     @return The result of the operation.
 
277
  */
 
278
  virtual double real_op()= 0;
 
279
 
 
280
  /**
 
281
     @brief Performs the operation that this functions implements when the
 
282
     result type is DECIMAL.
 
283
 
 
284
     @param A pointer where the DECIMAL value will be allocated.
 
285
     @return 
 
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.
 
289
  */
 
290
  virtual my_decimal *decimal_op(my_decimal *)= 0;
 
291
 
 
292
  /**
 
293
     @brief Performs the operation that this functions implements when the
 
294
     result type is a string type.
 
295
 
 
296
     @return The result of the operation.
 
297
  */
 
298
  virtual String *str_op(String *)= 0;
 
299
  bool is_null() { update_null_value(); return null_value; }
 
300
};
 
301
 
 
302
/* function where type of result detected by first argument */
 
303
class Item_func_num1: public Item_func_numhybrid
 
304
{
 
305
public:
 
306
  Item_func_num1(Item *a) :Item_func_numhybrid(a) {}
 
307
  Item_func_num1(Item *a, Item *b) :Item_func_numhybrid(a, b) {}
 
308
 
 
309
  void fix_num_length_and_dec();
 
310
  void find_num_type();
 
311
  String *str_op(String *str) { DBUG_ASSERT(0); return 0; }
 
312
};
 
313
 
 
314
 
 
315
/* Base class for operations like '+', '-', '*' */
 
316
class Item_num_op :public Item_func_numhybrid
 
317
{
 
318
 public:
 
319
  Item_num_op(Item *a,Item *b) :Item_func_numhybrid(a, b) {}
 
320
  virtual void result_precision()= 0;
 
321
 
 
322
  virtual inline void print(String *str, enum_query_type query_type)
 
323
  {
 
324
    print_op(str, query_type);
 
325
  }
 
326
 
 
327
  void find_num_type();
 
328
  String *str_op(String *str) { DBUG_ASSERT(0); return 0; }
 
329
};
 
330
 
 
331
 
 
332
class Item_int_func :public Item_func
 
333
{
 
334
public:
 
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)
 
339
  { max_length= 21; }
 
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) {}
 
342
  double val_real();
 
343
  String *val_str(String*str);
 
344
  enum Item_result result_type () const { return INT_RESULT; }
 
345
  void fix_length_and_dec() {}
 
346
};
 
347
 
 
348
 
 
349
class Item_func_connection_id :public Item_int_func
 
350
{
 
351
  longlong value;
 
352
 
 
353
public:
 
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; }
 
359
};
 
360
 
 
361
 
 
362
class Item_func_signed :public Item_int_func
 
363
{
 
364
public:
 
365
  Item_func_signed(Item *a) :Item_int_func(a) {}
 
366
  const char *func_name() const { return "cast_as_signed"; }
 
367
  longlong val_int();
 
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(); }
 
373
};
 
374
 
 
375
 
 
376
class Item_func_unsigned :public Item_func_signed
 
377
{
 
378
public:
 
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()
 
382
  {
 
383
    max_length= min(args[0]->max_length, DECIMAL_MAX_PRECISION + 2);
 
384
    unsigned_flag=1;
 
385
  }
 
386
  longlong val_int();
 
387
  virtual void print(String *str, enum_query_type query_type);
 
388
};
 
389
 
 
390
 
 
391
class Item_decimal_typecast :public Item_func
 
392
{
 
393
  my_decimal decimal_value;
 
394
public:
 
395
  Item_decimal_typecast(Item *a, int len, int dec) :Item_func(a)
 
396
  {
 
397
    decimals= dec;
 
398
    max_length= my_decimal_precision_to_length_no_truncation(len, dec,
 
399
                                                             unsigned_flag);
 
400
  }
 
401
  String *val_str(String *str);
 
402
  double val_real();
 
403
  longlong val_int();
 
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);
 
410
};
 
411
 
 
412
 
 
413
class Item_func_additive_op :public Item_num_op
 
414
{
 
415
public:
 
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;}
 
419
};
 
420
 
 
421
 
 
422
class Item_func_plus :public Item_func_additive_op
 
423
{
 
424
public:
 
425
  Item_func_plus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
 
426
  const char *func_name() const { return "+"; }
 
427
  longlong int_op();
 
428
  double real_op();
 
429
  my_decimal *decimal_op(my_decimal *);
 
430
};
 
431
 
 
432
class Item_func_minus :public Item_func_additive_op
 
433
{
 
434
public:
 
435
  Item_func_minus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
 
436
  const char *func_name() const { return "-"; }
 
437
  longlong int_op();
 
438
  double real_op();
 
439
  my_decimal *decimal_op(my_decimal *);
 
440
  void fix_length_and_dec();
 
441
};
 
442
 
 
443
 
 
444
class Item_func_mul :public Item_num_op
 
445
{
 
446
public:
 
447
  Item_func_mul(Item *a,Item *b) :Item_num_op(a,b) {}
 
448
  const char *func_name() const { return "*"; }
 
449
  longlong int_op();
 
450
  double real_op();
 
451
  my_decimal *decimal_op(my_decimal *);
 
452
  void result_precision();
 
453
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
 
454
};
 
455
 
 
456
 
 
457
class Item_func_div :public Item_num_op
 
458
{
 
459
public:
 
460
  uint prec_increment;
 
461
  Item_func_div(Item *a,Item *b) :Item_num_op(a,b) {}
 
462
  longlong int_op() { DBUG_ASSERT(0); return 0; }
 
463
  double real_op();
 
464
  my_decimal *decimal_op(my_decimal *);
 
465
  const char *func_name() const { return "/"; }
 
466
  void fix_length_and_dec();
 
467
  void result_precision();
 
468
};
 
469
 
 
470
 
 
471
class Item_func_int_div :public Item_int_func
 
472
{
 
473
public:
 
474
  Item_func_int_div(Item *a,Item *b) :Item_int_func(a,b)
 
475
  {}
 
476
  longlong val_int();
 
477
  const char *func_name() const { return "DIV"; }
 
478
  void fix_length_and_dec();
 
479
 
 
480
  virtual inline void print(String *str, enum_query_type query_type)
 
481
  {
 
482
    print_op(str, query_type);
 
483
  }
 
484
 
 
485
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
 
486
};
 
487
 
 
488
 
 
489
class Item_func_mod :public Item_num_op
 
490
{
 
491
public:
 
492
  Item_func_mod(Item *a,Item *b) :Item_num_op(a,b) {}
 
493
  longlong int_op();
 
494
  double real_op();
 
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;}
 
500
};
 
501
 
 
502
 
 
503
class Item_func_neg :public Item_func_num1
 
504
{
 
505
public:
 
506
  Item_func_neg(Item *a) :Item_func_num1(a) {}
 
507
  double real_op();
 
508
  longlong int_op();
 
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;}
 
516
};
 
517
 
 
518
 
 
519
class Item_func_abs :public Item_func_num1
 
520
{
 
521
public:
 
522
  Item_func_abs(Item *a) :Item_func_num1(a) {}
 
523
  double real_op();
 
524
  longlong int_op();
 
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;}
 
529
};
 
530
 
 
531
// A class to handle logarithmic and trigonometric functions
 
532
 
 
533
class Item_dec_func :public Item_real_func
 
534
{
 
535
 public:
 
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()
 
539
  {
 
540
    decimals=NOT_FIXED_DEC; max_length=float_length(decimals);
 
541
    maybe_null=1;
 
542
  }
 
543
};
 
544
 
 
545
class Item_func_exp :public Item_dec_func
 
546
{
 
547
public:
 
548
  Item_func_exp(Item *a) :Item_dec_func(a) {}
 
549
  double val_real();
 
550
  const char *func_name() const { return "exp"; }
 
551
};
 
552
 
 
553
 
 
554
class Item_func_ln :public Item_dec_func
 
555
{
 
556
public:
 
557
  Item_func_ln(Item *a) :Item_dec_func(a) {}
 
558
  double val_real();
 
559
  const char *func_name() const { return "ln"; }
 
560
};
 
561
 
 
562
 
 
563
class Item_func_log :public Item_dec_func
 
564
{
 
565
public:
 
566
  Item_func_log(Item *a) :Item_dec_func(a) {}
 
567
  Item_func_log(Item *a,Item *b) :Item_dec_func(a,b) {}
 
568
  double val_real();
 
569
  const char *func_name() const { return "log"; }
 
570
};
 
571
 
 
572
 
 
573
class Item_func_log2 :public Item_dec_func
 
574
{
 
575
public:
 
576
  Item_func_log2(Item *a) :Item_dec_func(a) {}
 
577
  double val_real();
 
578
  const char *func_name() const { return "log2"; }
 
579
};
 
580
 
 
581
 
 
582
class Item_func_log10 :public Item_dec_func
 
583
{
 
584
public:
 
585
  Item_func_log10(Item *a) :Item_dec_func(a) {}
 
586
  double val_real();
 
587
  const char *func_name() const { return "log10"; }
 
588
};
 
589
 
 
590
 
 
591
class Item_func_sqrt :public Item_dec_func
 
592
{
 
593
public:
 
594
  Item_func_sqrt(Item *a) :Item_dec_func(a) {}
 
595
  double val_real();
 
596
  const char *func_name() const { return "sqrt"; }
 
597
};
 
598
 
 
599
 
 
600
class Item_func_pow :public Item_dec_func
 
601
{
 
602
public:
 
603
  Item_func_pow(Item *a,Item *b) :Item_dec_func(a,b) {}
 
604
  double val_real();
 
605
  const char *func_name() const { return "pow"; }
 
606
};
 
607
 
 
608
 
 
609
class Item_func_acos :public Item_dec_func
 
610
{
 
611
public:
 
612
  Item_func_acos(Item *a) :Item_dec_func(a) {}
 
613
  double val_real();
 
614
  const char *func_name() const { return "acos"; }
 
615
};
 
616
 
 
617
class Item_func_asin :public Item_dec_func
 
618
{
 
619
public:
 
620
  Item_func_asin(Item *a) :Item_dec_func(a) {}
 
621
  double val_real();
 
622
  const char *func_name() const { return "asin"; }
 
623
};
 
624
 
 
625
class Item_func_atan :public Item_dec_func
 
626
{
 
627
public:
 
628
  Item_func_atan(Item *a) :Item_dec_func(a) {}
 
629
  Item_func_atan(Item *a,Item *b) :Item_dec_func(a,b) {}
 
630
  double val_real();
 
631
  const char *func_name() const { return "atan"; }
 
632
};
 
633
 
 
634
class Item_func_cos :public Item_dec_func
 
635
{
 
636
public:
 
637
  Item_func_cos(Item *a) :Item_dec_func(a) {}
 
638
  double val_real();
 
639
  const char *func_name() const { return "cos"; }
 
640
};
 
641
 
 
642
class Item_func_sin :public Item_dec_func
 
643
{
 
644
public:
 
645
  Item_func_sin(Item *a) :Item_dec_func(a) {}
 
646
  double val_real();
 
647
  const char *func_name() const { return "sin"; }
 
648
};
 
649
 
 
650
class Item_func_tan :public Item_dec_func
 
651
{
 
652
public:
 
653
  Item_func_tan(Item *a) :Item_dec_func(a) {}
 
654
  double val_real();
 
655
  const char *func_name() const { return "tan"; }
 
656
};
 
657
 
 
658
class Item_func_integer :public Item_int_func
 
659
{
 
660
public:
 
661
  inline Item_func_integer(Item *a) :Item_int_func(a) {}
 
662
  void fix_length_and_dec();
 
663
};
 
664
 
 
665
 
 
666
class Item_func_int_val :public Item_func_num1
 
667
{
 
668
public:
 
669
  Item_func_int_val(Item *a) :Item_func_num1(a) {}
 
670
  void fix_num_length_and_dec();
 
671
  void find_num_type();
 
672
};
 
673
 
 
674
 
 
675
class Item_func_ceiling :public Item_func_int_val
 
676
{
 
677
public:
 
678
  Item_func_ceiling(Item *a) :Item_func_int_val(a) {}
 
679
  const char *func_name() const { return "ceiling"; }
 
680
  longlong int_op();
 
681
  double real_op();
 
682
  my_decimal *decimal_op(my_decimal *);
 
683
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
 
684
};
 
685
 
 
686
 
 
687
class Item_func_floor :public Item_func_int_val
 
688
{
 
689
public:
 
690
  Item_func_floor(Item *a) :Item_func_int_val(a) {}
 
691
  const char *func_name() const { return "floor"; }
 
692
  longlong int_op();
 
693
  double real_op();
 
694
  my_decimal *decimal_op(my_decimal *);
 
695
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
 
696
};
 
697
 
 
698
/* This handles round and truncate */
 
699
 
 
700
class Item_func_round :public Item_func_num1
 
701
{
 
702
  bool truncate;
 
703
public:
 
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"; }
 
707
  double real_op();
 
708
  longlong int_op();
 
709
  my_decimal *decimal_op(my_decimal *);
 
710
  void fix_length_and_dec();
 
711
};
 
712
 
 
713
 
 
714
class Item_func_rand :public Item_real_func
 
715
{
 
716
  struct rand_struct *rand;
 
717
  bool first_eval; // TRUE if val_real() is called 1st time
 
718
public:
 
719
  Item_func_rand(Item *a) :Item_real_func(a), rand(0), first_eval(TRUE) {}
 
720
  Item_func_rand()        :Item_real_func() {}
 
721
  double val_real();
 
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(); }
 
727
private:
 
728
  void seed_random (Item * val);  
 
729
};
 
730
 
 
731
 
 
732
class Item_func_sign :public Item_int_func
 
733
{
 
734
public:
 
735
  Item_func_sign(Item *a) :Item_int_func(a) {}
 
736
  const char *func_name() const { return "sign"; }
 
737
  longlong val_int();
 
738
};
 
739
 
 
740
 
 
741
class Item_func_units :public Item_real_func
 
742
{
 
743
  char *name;
 
744
  double mul,add;
 
745
public:
 
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) {}
 
748
  double val_real();
 
749
  const char *func_name() const { return name; }
 
750
  void fix_length_and_dec()
 
751
  { decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
 
752
};
 
753
 
 
754
 
 
755
class Item_func_min_max :public Item_func
 
756
{
 
757
  Item_result cmp_type;
 
758
  String tmp_value;
 
759
  int cmp_sign;
 
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. */
 
763
  Item *datetime_item;
 
764
  THD *thd;
 
765
protected:
 
766
  enum_field_types cached_field_type;
 
767
public:
 
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),
 
770
    datetime_item(0) {}
 
771
  double val_real();
 
772
  longlong val_int();
 
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; }
 
780
};
 
781
 
 
782
class Item_func_min :public Item_func_min_max
 
783
{
 
784
public:
 
785
  Item_func_min(List<Item> &list) :Item_func_min_max(list,1) {}
 
786
  const char *func_name() const { return "least"; }
 
787
};
 
788
 
 
789
class Item_func_max :public Item_func_min_max
 
790
{
 
791
public:
 
792
  Item_func_max(List<Item> &list) :Item_func_min_max(list,-1) {}
 
793
  const char *func_name() const { return "greatest"; }
 
794
};
 
795
 
 
796
 
 
797
/* 
 
798
  Objects of this class are used for ROLLUP queries to wrap up 
 
799
  each constant item referred to in GROUP BY list. 
 
800
*/
 
801
 
 
802
class Item_func_rollup_const :public Item_func
 
803
{
 
804
public:
 
805
  Item_func_rollup_const(Item *a) :Item_func(a)
 
806
  {
 
807
    name= a->name;
 
808
    name_length= a->name_length;
 
809
  }
 
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()
 
818
  {
 
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();
 
824
  }
 
825
};
 
826
 
 
827
 
 
828
class Item_func_length :public Item_int_func
 
829
{
 
830
  String value;
 
831
public:
 
832
  Item_func_length(Item *a) :Item_int_func(a) {}
 
833
  longlong val_int();
 
834
  const char *func_name() const { return "length"; }
 
835
  void fix_length_and_dec() { max_length=10; }
 
836
};
 
837
 
 
838
class Item_func_bit_length :public Item_func_length
 
839
{
 
840
public:
 
841
  Item_func_bit_length(Item *a) :Item_func_length(a) {}
 
842
  longlong val_int()
 
843
    { DBUG_ASSERT(fixed == 1); return Item_func_length::val_int()*8; }
 
844
  const char *func_name() const { return "bit_length"; }
 
845
};
 
846
 
 
847
class Item_func_char_length :public Item_int_func
 
848
{
 
849
  String value;
 
850
public:
 
851
  Item_func_char_length(Item *a) :Item_int_func(a) {}
 
852
  longlong val_int();
 
853
  const char *func_name() const { return "char_length"; }
 
854
  void fix_length_and_dec() { max_length=10; }
 
855
};
 
856
 
 
857
class Item_func_coercibility :public Item_int_func
 
858
{
 
859
public:
 
860
  Item_func_coercibility(Item *a) :Item_int_func(a) {}
 
861
  longlong val_int();
 
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; }
 
865
};
 
866
 
 
867
class Item_func_locate :public Item_int_func
 
868
{
 
869
  String value1,value2;
 
870
  DTCollation cmp_collation;
 
871
public:
 
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"; }
 
875
  longlong val_int();
 
876
  void fix_length_and_dec();
 
877
  virtual void print(String *str, enum_query_type query_type);
 
878
};
 
879
 
 
880
 
 
881
class Item_func_field :public Item_int_func
 
882
{
 
883
  String value,tmp;
 
884
  Item_result cmp_type;
 
885
  DTCollation cmp_collation;
 
886
public:
 
887
  Item_func_field(List<Item> &list) :Item_int_func(list) {}
 
888
  longlong val_int();
 
889
  const char *func_name() const { return "field"; }
 
890
  void fix_length_and_dec();
 
891
};
 
892
 
 
893
 
 
894
class Item_func_ascii :public Item_int_func
 
895
{
 
896
  String value;
 
897
public:
 
898
  Item_func_ascii(Item *a) :Item_int_func(a) {}
 
899
  longlong val_int();
 
900
  const char *func_name() const { return "ascii"; }
 
901
  void fix_length_and_dec() { max_length=3; }
 
902
};
 
903
 
 
904
class Item_func_ord :public Item_int_func
 
905
{
 
906
  String value;
 
907
public:
 
908
  Item_func_ord(Item *a) :Item_int_func(a) {}
 
909
  longlong val_int();
 
910
  const char *func_name() const { return "ord"; }
 
911
};
 
912
 
 
913
class Item_func_find_in_set :public Item_int_func
 
914
{
 
915
  String value,value2;
 
916
  uint enum_value;
 
917
  ulonglong enum_bit;
 
918
  DTCollation cmp_collation;
 
919
public:
 
920
  Item_func_find_in_set(Item *a,Item *b) :Item_int_func(a,b),enum_value(0) {}
 
921
  longlong val_int();
 
922
  const char *func_name() const { return "find_in_set"; }
 
923
  void fix_length_and_dec();
 
924
};
 
925
 
 
926
/* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
 
927
 
 
928
class Item_func_bit: public Item_int_func
 
929
{
 
930
public:
 
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; }
 
934
 
 
935
  virtual inline void print(String *str, enum_query_type query_type)
 
936
  {
 
937
    print_op(str, query_type);
 
938
  }
 
939
};
 
940
 
 
941
class Item_func_bit_or :public Item_func_bit
 
942
{
 
943
public:
 
944
  Item_func_bit_or(Item *a, Item *b) :Item_func_bit(a, b) {}
 
945
  longlong val_int();
 
946
  const char *func_name() const { return "|"; }
 
947
};
 
948
 
 
949
class Item_func_bit_and :public Item_func_bit
 
950
{
 
951
public:
 
952
  Item_func_bit_and(Item *a, Item *b) :Item_func_bit(a, b) {}
 
953
  longlong val_int();
 
954
  const char *func_name() const { return "&"; }
 
955
};
 
956
 
 
957
class Item_func_bit_count :public Item_int_func
 
958
{
 
959
public:
 
960
  Item_func_bit_count(Item *a) :Item_int_func(a) {}
 
961
  longlong val_int();
 
962
  const char *func_name() const { return "bit_count"; }
 
963
  void fix_length_and_dec() { max_length=2; }
 
964
};
 
965
 
 
966
class Item_func_shift_left :public Item_func_bit
 
967
{
 
968
public:
 
969
  Item_func_shift_left(Item *a, Item *b) :Item_func_bit(a, b) {}
 
970
  longlong val_int();
 
971
  const char *func_name() const { return "<<"; }
 
972
};
 
973
 
 
974
class Item_func_shift_right :public Item_func_bit
 
975
{
 
976
public:
 
977
  Item_func_shift_right(Item *a, Item *b) :Item_func_bit(a, b) {}
 
978
  longlong val_int();
 
979
  const char *func_name() const { return ">>"; }
 
980
};
 
981
 
 
982
class Item_func_bit_neg :public Item_func_bit
 
983
{
 
984
public:
 
985
  Item_func_bit_neg(Item *a) :Item_func_bit(a) {}
 
986
  longlong val_int();
 
987
  const char *func_name() const { return "~"; }
 
988
 
 
989
  virtual inline void print(String *str, enum_query_type query_type)
 
990
  {
 
991
    Item_func::print(str, query_type);
 
992
  }
 
993
};
 
994
 
 
995
 
 
996
class Item_func_last_insert_id :public Item_int_func
 
997
{
 
998
public:
 
999
  Item_func_last_insert_id() :Item_int_func() {}
 
1000
  Item_func_last_insert_id(Item *a) :Item_int_func(a) {}
 
1001
  longlong val_int();
 
1002
  const char *func_name() const { return "last_insert_id"; }
 
1003
  void fix_length_and_dec()
 
1004
  {
 
1005
    if (arg_count)
 
1006
      max_length= args[0]->max_length;
 
1007
  }
 
1008
  bool fix_fields(THD *thd, Item **ref);
 
1009
};
 
1010
 
 
1011
 
 
1012
class Item_func_benchmark :public Item_int_func
 
1013
{
 
1014
public:
 
1015
  Item_func_benchmark(Item *count_expr, Item *expr)
 
1016
    :Item_int_func(count_expr, expr)
 
1017
  {}
 
1018
  longlong val_int();
 
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);
 
1022
};
 
1023
 
 
1024
 
 
1025
class Item_func_sleep :public Item_int_func
 
1026
{
 
1027
public:
 
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()
 
1032
  {
 
1033
    Item_int_func::update_used_tables();
 
1034
    used_tables_cache|= RAND_TABLE_BIT;
 
1035
  }
 
1036
  longlong val_int();
 
1037
};
 
1038
 
 
1039
 
 
1040
 
 
1041
#ifdef HAVE_DLOPEN
 
1042
 
 
1043
class Item_udf_func :public Item_func
 
1044
{
 
1045
protected:
 
1046
  udf_handler udf;
 
1047
 
 
1048
public:
 
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)
 
1056
  {
 
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;
 
1061
    fixed= 1;
 
1062
    return res;
 
1063
  }
 
1064
  void update_used_tables() 
 
1065
  {
 
1066
    /*
 
1067
      TODO: Make a member in UDF_INIT and return if a UDF is deterministic or
 
1068
      not.
 
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().
 
1073
      
 
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 
 
1083
       udf_handler. 
 
1084
       - Don't implement Item_udf_func::fix_fields, implement
 
1085
       Item_udf_func::fix_length_and_dec() instead (similar to non-UDF
 
1086
       functions).
 
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 
 
1090
       non-deterministic.
 
1091
       - (optionally) rename RAND_TABLE_BIT to NONDETERMINISTIC_BIT to
 
1092
       better describe its usage.
 
1093
       
 
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
 
1104
      const.
 
1105
    */  
 
1106
    if ((used_tables_cache & ~PSEUDO_TABLE_BITS) && 
 
1107
        !(used_tables_cache & RAND_TABLE_BIT))
 
1108
    {
 
1109
      Item_func::update_used_tables();
 
1110
      if (!const_item_cache && !used_tables_cache)
 
1111
        used_tables_cache= RAND_TABLE_BIT;
 
1112
    }
 
1113
  }
 
1114
  void cleanup();
 
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);
 
1119
};
 
1120
 
 
1121
 
 
1122
class Item_func_udf_float :public Item_udf_func
 
1123
{
 
1124
 public:
 
1125
  Item_func_udf_float(udf_func *udf_arg)
 
1126
    :Item_udf_func(udf_arg) {}
 
1127
  Item_func_udf_float(udf_func *udf_arg,
 
1128
                      List<Item> &list)
 
1129
    :Item_udf_func(udf_arg, list) {}
 
1130
  longlong val_int()
 
1131
  {
 
1132
    DBUG_ASSERT(fixed == 1);
 
1133
    return (longlong) rint(Item_func_udf_float::val_real());
 
1134
  }
 
1135
  my_decimal *val_decimal(my_decimal *dec_buf)
 
1136
  {
 
1137
    double res=val_real();
 
1138
    if (null_value)
 
1139
      return NULL;
 
1140
    double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
 
1141
    return dec_buf;
 
1142
  }
 
1143
  double val_real();
 
1144
  String *val_str(String *str);
 
1145
  void fix_length_and_dec() { fix_num_length_and_dec(); }
 
1146
};
 
1147
 
 
1148
 
 
1149
class Item_func_udf_int :public Item_udf_func
 
1150
{
 
1151
public:
 
1152
  Item_func_udf_int(udf_func *udf_arg)
 
1153
    :Item_udf_func(udf_arg) {}
 
1154
  Item_func_udf_int(udf_func *udf_arg,
 
1155
                    List<Item> &list)
 
1156
    :Item_udf_func(udf_arg, list) {}
 
1157
  longlong val_int();
 
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; }
 
1162
};
 
1163
 
 
1164
 
 
1165
class Item_func_udf_decimal :public Item_udf_func
 
1166
{
 
1167
public:
 
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) {}
 
1172
  longlong val_int();
 
1173
  double val_real();
 
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();
 
1178
};
 
1179
 
 
1180
 
 
1181
class Item_func_udf_str :public Item_udf_func
 
1182
{
 
1183
public:
 
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 *);
 
1189
  double val_real()
 
1190
  {
 
1191
    int err_not_used;
 
1192
    char *end_not_used;
 
1193
    String *res;
 
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;
 
1197
  }
 
1198
  longlong val_int()
 
1199
  {
 
1200
    int err_not_used;
 
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;
 
1204
  }
 
1205
  my_decimal *val_decimal(my_decimal *dec_buf)
 
1206
  {
 
1207
    String *res=val_str(&str_value);
 
1208
    if (!res)
 
1209
      return NULL;
 
1210
    string2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
 
1211
    return dec_buf;
 
1212
  }
 
1213
  enum Item_result result_type () const { return STRING_RESULT; }
 
1214
  void fix_length_and_dec();
 
1215
};
 
1216
 
 
1217
#else /* Dummy functions to get sql_yacc.cc compiled */
 
1218
 
 
1219
class Item_func_udf_float :public Item_real_func
 
1220
{
 
1221
 public:
 
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; }
 
1227
};
 
1228
 
 
1229
 
 
1230
class Item_func_udf_int :public Item_int_func
 
1231
{
 
1232
public:
 
1233
  Item_func_udf_int(udf_func *udf_arg)
 
1234
    :Item_int_func() {}
 
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; }
 
1238
};
 
1239
 
 
1240
 
 
1241
class Item_func_udf_decimal :public Item_int_func
 
1242
{
 
1243
public:
 
1244
  Item_func_udf_decimal(udf_func *udf_arg)
 
1245
    :Item_int_func() {}
 
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; }
 
1249
};
 
1250
 
 
1251
 
 
1252
class Item_func_udf_str :public Item_func
 
1253
{
 
1254
public:
 
1255
  Item_func_udf_str(udf_func *udf_arg)
 
1256
    :Item_func() {}
 
1257
  Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
 
1258
    :Item_func(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; }
 
1265
};
 
1266
 
 
1267
#endif /* HAVE_DLOPEN */
 
1268
 
 
1269
/*
 
1270
** User level locks
 
1271
*/
 
1272
 
 
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);
 
1277
 
 
1278
class Item_func_get_lock :public Item_int_func
 
1279
{
 
1280
  String value;
 
1281
 public:
 
1282
  Item_func_get_lock(Item *a,Item *b) :Item_int_func(a,b) {}
 
1283
  longlong val_int();
 
1284
  const char *func_name() const { return "get_lock"; }
 
1285
  void fix_length_and_dec() { max_length=1; maybe_null=1;}
 
1286
};
 
1287
 
 
1288
class Item_func_release_lock :public Item_int_func
 
1289
{
 
1290
  String value;
 
1291
public:
 
1292
  Item_func_release_lock(Item *a) :Item_int_func(a) {}
 
1293
  longlong val_int();
 
1294
  const char *func_name() const { return "release_lock"; }
 
1295
  void fix_length_and_dec() { max_length=1; maybe_null=1;}
 
1296
};
 
1297
 
 
1298
/* replication functions */
 
1299
 
 
1300
class Item_master_pos_wait :public Item_int_func
 
1301
{
 
1302
  String value;
 
1303
public:
 
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) {}
 
1306
  longlong val_int();
 
1307
  const char *func_name() const { return "master_pos_wait"; }
 
1308
  void fix_length_and_dec() { max_length=21; maybe_null=1;}
 
1309
};
 
1310
 
 
1311
 
 
1312
/* Handling of user definable variables */
 
1313
 
 
1314
class user_var_entry;
 
1315
 
 
1316
class Item_func_set_user_var :public Item_func
 
1317
{
 
1318
  enum Item_result cached_result_type;
 
1319
  user_var_entry *entry;
 
1320
  /*
 
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).
 
1329
  */
 
1330
  my_thread_id entry_thread_id;
 
1331
  char buffer[MAX_FIELD_WIDTH];
 
1332
  String value;
 
1333
  my_decimal decimal_buff;
 
1334
  bool null_item;
 
1335
  union
 
1336
  {
 
1337
    longlong vint;
 
1338
    double vreal;
 
1339
    String *vstr;
 
1340
    my_decimal *vdec;
 
1341
  } save_result;
 
1342
 
 
1343
public:
 
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)
 
1348
  {}
 
1349
  enum Functype functype() const { return SUSERVAR_FUNC; }
 
1350
  double val_real();
 
1351
  longlong val_int();
 
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);
 
1365
  bool update();
 
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)
 
1375
  {
 
1376
    return save_in_field(field, no_conversions, 1);
 
1377
  }
 
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);
 
1381
  void cleanup();
 
1382
};
 
1383
 
 
1384
 
 
1385
class Item_func_get_user_var :public Item_func,
 
1386
                              private Settable_routine_parameter
 
1387
{
 
1388
  user_var_entry *var_entry;
 
1389
  Item_result m_cached_result_type;
 
1390
 
 
1391
public:
 
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; }
 
1397
  double val_real();
 
1398
  longlong val_int();
 
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;
 
1404
  /*
 
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)
 
1407
  */
 
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;
 
1413
private:
 
1414
  bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
 
1415
 
 
1416
public:
 
1417
  Settable_routine_parameter *get_settable_routine_parameter()
 
1418
  {
 
1419
    return this;
 
1420
  }
 
1421
};
 
1422
 
 
1423
 
 
1424
/*
 
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.
 
1428
 
 
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.
 
1432
*/
 
1433
class Item_user_var_as_out_param :public Item
 
1434
{
 
1435
  LEX_STRING name;
 
1436
  user_var_entry *entry;
 
1437
public:
 
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;}
 
1441
  double val_real();
 
1442
  longlong val_int();
 
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);
 
1450
};
 
1451
 
 
1452
 
 
1453
/* A system variable */
 
1454
 
 
1455
#define GET_SYS_VAR_CACHE_LONG     1
 
1456
#define GET_SYS_VAR_CACHE_DOUBLE   2
 
1457
#define GET_SYS_VAR_CACHE_STRING   4
 
1458
 
 
1459
class Item_func_get_system_var :public Item_func
 
1460
{
 
1461
  sys_var *var;
 
1462
  enum_var_type var_type, orig_var_type;
 
1463
  LEX_STRING component;
 
1464
  longlong cached_llval;
 
1465
  double cached_dval;
 
1466
  String cached_strval;
 
1467
  my_bool cached_null_value;
 
1468
  query_id_t used_query_id;
 
1469
  uchar cache_present;
 
1470
 
 
1471
public:
 
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;
 
1483
  double val_real();
 
1484
  longlong val_int();
 
1485
  String* val_str(String*);
 
1486
  /* TODO: fix to support views */
 
1487
  const char *func_name() const { return "get_system_var"; }
 
1488
  /**
 
1489
    Indicates whether this system variable is written to the binlog or not.
 
1490
 
 
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.
 
1493
 
 
1494
    @return true if the variable is written to the binlog, false otherwise.
 
1495
  */
 
1496
  bool is_written_to_binlog();
 
1497
  bool eq(const Item *item, bool binary_cmp) const;
 
1498
 
 
1499
  void cleanup();
 
1500
};
 
1501
 
 
1502
 
 
1503
class Item_func_inet_aton : public Item_int_func
 
1504
{
 
1505
public:
 
1506
  Item_func_inet_aton(Item *a) :Item_int_func(a) {}
 
1507
  longlong val_int();
 
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;}
 
1510
};
 
1511
 
 
1512
 
 
1513
/* for fulltext search */
 
1514
#include <ft_global.h>
 
1515
 
 
1516
class Item_func_match :public Item_real_func
 
1517
{
 
1518
public:
 
1519
  uint key, flags;
 
1520
  bool join_key;
 
1521
  DTCollation cmp_collation;
 
1522
  FT_INFO *ft_handler;
 
1523
  TABLE *table;
 
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
 
1528
 
 
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) { }
 
1531
  void cleanup()
 
1532
  {
 
1533
    DBUG_ENTER("Item_func_match");
 
1534
    Item_real_func::cleanup();
 
1535
    if (!master && ft_handler)
 
1536
      ft_handler->please->close_search(ft_handler);
 
1537
    ft_handler= 0;
 
1538
    concat_ws= 0;
 
1539
    table= 0;           // required by Item_func_match::eq()
 
1540
    DBUG_VOID_RETURN;
 
1541
  }
 
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; }
 
1550
  double val_real();
 
1551
  virtual void print(String *str, enum_query_type query_type);
 
1552
 
 
1553
  bool fix_index();
 
1554
  void init_search(bool no_order);
 
1555
};
 
1556
 
 
1557
 
 
1558
class Item_func_bit_xor : public Item_func_bit
 
1559
{
 
1560
public:
 
1561
  Item_func_bit_xor(Item *a, Item *b) :Item_func_bit(a, b) {}
 
1562
  longlong val_int();
 
1563
  const char *func_name() const { return "^"; }
 
1564
};
 
1565
 
 
1566
class Item_func_is_free_lock :public Item_int_func
 
1567
{
 
1568
  String value;
 
1569
public:
 
1570
  Item_func_is_free_lock(Item *a) :Item_int_func(a) {}
 
1571
  longlong val_int();
 
1572
  const char *func_name() const { return "is_free_lock"; }
 
1573
  void fix_length_and_dec() { decimals=0; max_length=1; maybe_null=1;}
 
1574
};
 
1575
 
 
1576
class Item_func_is_used_lock :public Item_int_func
 
1577
{
 
1578
  String value;
 
1579
public:
 
1580
  Item_func_is_used_lock(Item *a) :Item_int_func(a) {}
 
1581
  longlong val_int();
 
1582
  const char *func_name() const { return "is_used_lock"; }
 
1583
  void fix_length_and_dec() { decimals=0; max_length=10; maybe_null=1;}
 
1584
};
 
1585
 
 
1586
/* For type casts */
 
1587
 
 
1588
enum Cast_target
 
1589
{
 
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,
 
1592
  ITEM_CAST_DECIMAL
 
1593
};
 
1594
 
 
1595
 
 
1596
class Item_func_row_count :public Item_int_func
 
1597
{
 
1598
public:
 
1599
  Item_func_row_count() :Item_int_func() {}
 
1600
  longlong val_int();
 
1601
  const char *func_name() const { return "row_count"; }
 
1602
  void fix_length_and_dec() { decimals= 0; maybe_null=0; }
 
1603
};
 
1604
 
 
1605
 
 
1606
/*
 
1607
 *
 
1608
 * Stored FUNCTIONs
 
1609
 *
 
1610
 */
 
1611
 
 
1612
class sp_head;
 
1613
class sp_name;
 
1614
struct st_sp_security_context;
 
1615
 
 
1616
class Item_func_sp :public Item_func
 
1617
{
 
1618
private:
 
1619
  Name_resolution_context *context;
 
1620
  sp_name *m_name;
 
1621
  mutable sp_head *m_sp;
 
1622
  TABLE *dummy_table;
 
1623
  uchar result_buf[64];
 
1624
  /*
 
1625
     The result field of the concrete stored function.
 
1626
  */
 
1627
  Field *sp_result_field;
 
1628
 
 
1629
  bool execute();
 
1630
  bool execute_impl(THD *thd);
 
1631
  bool init_result_field(THD *thd);
 
1632
  
 
1633
public:
 
1634
 
 
1635
  Item_func_sp(Name_resolution_context *context_arg, sp_name *name);
 
1636
 
 
1637
  Item_func_sp(Name_resolution_context *context_arg,
 
1638
               sp_name *name, List<Item> &list);
 
1639
 
 
1640
  virtual ~Item_func_sp()
 
1641
  {}
 
1642
 
 
1643
  void update_used_tables();
 
1644
 
 
1645
  void cleanup();
 
1646
 
 
1647
  const char *func_name() const;
 
1648
 
 
1649
  enum enum_field_types field_type() const;
 
1650
 
 
1651
  Field *tmp_table_field(TABLE *t_arg);
 
1652
 
 
1653
  void make_field(Send_field *tmp_field);
 
1654
 
 
1655
  Item_result result_type() const;
 
1656
 
 
1657
  longlong val_int()
 
1658
  {
 
1659
    if (execute())
 
1660
      return (longlong) 0;
 
1661
    return sp_result_field->val_int();
 
1662
  }
 
1663
 
 
1664
  double val_real()
 
1665
  {
 
1666
    if (execute())
 
1667
      return 0.0;
 
1668
    return sp_result_field->val_real();
 
1669
  }
 
1670
 
 
1671
  my_decimal *val_decimal(my_decimal *dec_buf)
 
1672
  {
 
1673
    if (execute())
 
1674
      return NULL;
 
1675
    return sp_result_field->val_decimal(dec_buf);
 
1676
  }
 
1677
 
 
1678
  String *val_str(String *str)
 
1679
  {
 
1680
    String buf;
 
1681
    char buff[20];
 
1682
    buf.set(buff, 20, str->charset());
 
1683
    buf.length(0);
 
1684
    if (execute())
 
1685
      return NULL;
 
1686
    /*
 
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.
 
1691
    */
 
1692
    sp_result_field->val_str(&buf);
 
1693
    str->copy(buf);
 
1694
    return str;
 
1695
  }
 
1696
 
 
1697
  virtual bool change_context_processor(uchar *cntx)
 
1698
    { context= (Name_resolution_context *)cntx; return FALSE; }
 
1699
 
 
1700
  bool sp_check_access(THD * thd);
 
1701
  virtual enum Functype functype() const { return FUNC_SP; }
 
1702
 
 
1703
  bool fix_fields(THD *thd, Item **ref);
 
1704
  void fix_length_and_dec(void);
 
1705
  bool is_expensive() { return 1; }
 
1706
 
 
1707
  inline Field *get_sp_result_field()
 
1708
  {
 
1709
    return sp_result_field;
 
1710
  }
 
1711
};
 
1712
 
 
1713
 
 
1714
class Item_func_found_rows :public Item_int_func
 
1715
{
 
1716
public:
 
1717
  Item_func_found_rows() :Item_int_func() {}
 
1718
  longlong val_int();
 
1719
  const char *func_name() const { return "found_rows"; }
 
1720
  void fix_length_and_dec() { decimals= 0; maybe_null=0; }
 
1721
};
 
1722
 
 
1723
 
 
1724
void uuid_short_init();
 
1725
 
 
1726
class Item_func_uuid_short :public Item_int_func
 
1727
{
 
1728
public:
 
1729
  Item_func_uuid_short() :Item_int_func() {}
 
1730
  const char *func_name() const { return "uuid_short"; }
 
1731
  longlong val_int();
 
1732
  void fix_length_and_dec()
 
1733
  { max_length= 21; unsigned_flag=1; }
 
1734
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
 
1735
};
 
1736