item_func.h 43.4 KB
Newer Older
1
/* Copyright (C) 2000-2006 MySQL AB
2

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7 8 9 10
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
11

bk@work.mysql.com's avatar
bk@work.mysql.com committed
12 13 14 15 16 17 18
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/* Function items used by mysql */

19
#ifdef USE_PRAGMA_INTERFACE
bk@work.mysql.com's avatar
bk@work.mysql.com committed
20 21 22 23 24 25 26 27 28 29 30 31 32
#pragma interface			/* gcc class implementation */
#endif

#ifdef HAVE_IEEEFP_H
extern "C"				/* Bug in BSDI include file */
{
#include <ieeefp.h>
}
#endif

class Item_func :public Item_result_field
{
protected:
33
  Item **args, *tmp_arg[2];
34 35 36 37
  /*
    Allowed numbers of columns in result (usually 1, which means scalar value)
    0 means get this number from first argument
  */
38
  uint allowed_arg_cols;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
39 40
public:
  uint arg_count;
41
  table_map used_tables_cache, not_null_tables_cache;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
42 43 44
  bool const_item_cache;
  enum Functype { UNKNOWN_FUNC,EQ_FUNC,EQUAL_FUNC,NE_FUNC,LT_FUNC,LE_FUNC,
		  GE_FUNC,GT_FUNC,FT_FUNC,
45
		  LIKE_FUNC,ISNULL_FUNC,ISNOTNULL_FUNC,
46 47
		  COND_AND_FUNC, COND_OR_FUNC, COND_XOR_FUNC,
                  BETWEEN, IN_FUNC, MULT_EQUAL_FUNC,
48
		  INTERVAL_FUNC, ISNOTNULLTEST_FUNC,
49 50 51 52
		  SP_EQUALS_FUNC, SP_DISJOINT_FUNC,SP_INTERSECTS_FUNC,
		  SP_TOUCHES_FUNC,SP_CROSSES_FUNC,SP_WITHIN_FUNC,
		  SP_CONTAINS_FUNC,SP_OVERLAPS_FUNC,
		  SP_STARTPOINT,SP_ENDPOINT,SP_EXTERIORRING,
53
		  SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
54 55
                  NOT_FUNC, NOT_ALL_FUNC,
                  NOW_FUNC, TRIG_COND_FUNC,
56
                  SUSERVAR_FUNC, GUSERVAR_FUNC, COLLATE_FUNC,
57
                  EXTRACT_FUNC, CHAR_TYPECAST_FUNC, FUNC_SP, UDF_FUNC };
58 59
  enum optimize_type { OPTIMIZE_NONE,OPTIMIZE_KEY,OPTIMIZE_OP, OPTIMIZE_NULL,
                       OPTIMIZE_EQUAL };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
60 61
  enum Type type() const { return FUNC_ITEM; }
  virtual enum Functype functype() const   { return UNKNOWN_FUNC; }
62 63
  Item_func(void):
    allowed_arg_cols(1), arg_count(0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
64
  {
65
    with_sum_func= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
66
  }
67 68
  Item_func(Item *a):
    allowed_arg_cols(1), arg_count(1)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
69
  {
70
    args= tmp_arg;
71 72
    args[0]= a;
    with_sum_func= a->with_sum_func;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
73
  }
74 75
  Item_func(Item *a,Item *b):
    allowed_arg_cols(1), arg_count(2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
76
  {
77
    args= tmp_arg;
78 79
    args[0]= a; args[1]= b;
    with_sum_func= a->with_sum_func || b->with_sum_func;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
80
  }
81 82
  Item_func(Item *a,Item *b,Item *c):
    allowed_arg_cols(1)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
83
  {
84 85
    arg_count= 0;
    if ((args= (Item**) sql_alloc(sizeof(Item*)*3)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
86
    {
87 88 89
      arg_count= 3;
      args[0]= a; args[1]= b; args[2]= c;
      with_sum_func= a->with_sum_func || b->with_sum_func || c->with_sum_func;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
90 91
    }
  }
92 93
  Item_func(Item *a,Item *b,Item *c,Item *d):
    allowed_arg_cols(1)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
94
  {
95 96
    arg_count= 0;
    if ((args= (Item**) sql_alloc(sizeof(Item*)*4)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
97
    {
98 99 100 101
      arg_count= 4;
      args[0]= a; args[1]= b; args[2]= c; args[3]= d;
      with_sum_func= a->with_sum_func || b->with_sum_func ||
	c->with_sum_func || d->with_sum_func;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
102 103
    }
  }
104 105
  Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
    allowed_arg_cols(1)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
106
  {
107 108
    arg_count= 5;
    if ((args= (Item**) sql_alloc(sizeof(Item*)*5)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
109
    {
110 111 112
      args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
      with_sum_func= a->with_sum_func || b->with_sum_func ||
	c->with_sum_func || d->with_sum_func || e->with_sum_func ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
113 114 115
    }
  }
  Item_func(List<Item> &list);
116
  // Constructor used for Item_cond_and/or (see Item comment)
117
  Item_func(THD *thd, Item_func *item);
118
  bool fix_fields(THD *, Item **ref);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
119
  table_map used_tables() const;
120
  table_map not_null_tables() const;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
121
  void update_used_tables();
122
  bool eq(const Item *item, bool binary_cmp) const;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
123 124 125
  virtual optimize_type select_optimize() const { return OPTIMIZE_NONE; }
  virtual bool have_rev_func() const { return 0; }
  virtual Item *key_item() const { return args[0]; }
126 127 128 129 130 131 132 133 134 135 136
  /*
    This method is used for debug purposes to print the name of an
    item to the debug log. The second use of this method is as
    a helper function of print(), where it is applicable.
    To suit both goals it should return a meaningful,
    distinguishable and sintactically correct string.  This method
    should not be used for runtime type identification, use enum
    {Sum}Functype and Item_func::functype()/Item_sum::sum_func()
    instead.
  */
  virtual const char *func_name() const= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
137 138
  virtual bool const_item() const { return const_item_cache; }
  inline Item **arguments() const { return args; }
139
  void set_arguments(List<Item> &list);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
140 141
  inline uint argument_count() const { return arg_count; }
  inline void remove_arguments() { arg_count=0; }
142
  void split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
143 144
  void print(String *str);
  void print_op(String *str);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
145
  void print_args(String *str, uint from);
146 147 148 149
  virtual void fix_num_length_and_dec();
  void count_only_length();
  void count_real_length();
  void count_decimal_length();
150
  inline bool get_arg0_date(MYSQL_TIME *ltime, uint fuzzy_date)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
151
  {
152
    return (null_value=args[0]->get_date(ltime, fuzzy_date));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
153
  }
154
  inline bool get_arg0_time(MYSQL_TIME *ltime)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
155 156 157
  {
    return (null_value=args[0]->get_time(ltime));
  }
158
  bool is_null() { 
159
    update_null_value();
160 161
    return null_value; 
  }
162
  void signal_divide_by_null();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
163
  friend class udf_handler;
164
  Field *tmp_table_field() { return result_field; }
165
  Field *tmp_table_field(TABLE *t_arg);
166
  Item *get_tmp_table_item(THD *thd);
167 168 169

  my_decimal *val_decimal(my_decimal *);

170
  bool agg_arg_collations(DTCollation &c, Item **items, uint nitems,
171
                          uint flags)
172
  {
173
    return agg_item_collations(c, func_name(), items, nitems, flags, 1);
174
  }
175 176
  bool agg_arg_collations_for_comparison(DTCollation &c,
                                         Item **items, uint nitems,
177
                                         uint flags)
178 179 180 181
  {
    return agg_item_collations_for_comparison(c, func_name(),
                                              items, nitems, flags);
  }
bar@mysql.com's avatar
bar@mysql.com committed
182
  bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems,
183
                        uint flags, int item_sep)
184
  {
185
    return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep);
186
  }
187 188 189 190
  bool walk(Item_processor processor, bool walk_subquery, uchar *arg);
  Item *transform(Item_transformer transformer, uchar *arg);
  Item* compile(Item_analyzer analyzer, uchar **arg_p,
                Item_transformer transformer, uchar *arg_t);
191 192
  void traverse_cond(Cond_traverser traverser,
                     void * arg, traverse_order order);
193
  bool is_expensive_processor(uchar *arg);
194
  virtual bool is_expensive() { return 0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
195 196 197 198 199 200 201 202 203 204 205
};


class Item_real_func :public Item_func
{
public:
  Item_real_func() :Item_func() {}
  Item_real_func(Item *a) :Item_func(a) {}
  Item_real_func(Item *a,Item *b) :Item_func(a,b) {}
  Item_real_func(List<Item> &list) :Item_func(list) {}
  String *val_str(String*str);
206
  my_decimal *val_decimal(my_decimal *decimal_value);
207
  longlong val_int()
208
    { DBUG_ASSERT(fixed == 1); return (longlong) rint(val_real()); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
209
  enum Item_result result_type () const { return REAL_RESULT; }
210 211
  void fix_length_and_dec()
  { decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
212 213
};

214

215
class Item_func_numhybrid: public Item_func
bk@work.mysql.com's avatar
bk@work.mysql.com committed
216
{
217
protected:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
218 219
  Item_result hybrid_type;
public:
220
  Item_func_numhybrid(Item *a) :Item_func(a), hybrid_type(REAL_RESULT)
221 222
  {}
  Item_func_numhybrid(Item *a,Item *b)
223
    :Item_func(a,b), hybrid_type(REAL_RESULT)
224
  {}
225
  Item_func_numhybrid(List<Item> &list)
226
    :Item_func(list), hybrid_type(REAL_RESULT)
227
  {}
228

bk@work.mysql.com's avatar
bk@work.mysql.com committed
229
  enum Item_result result_type () const { return hybrid_type; }
230 231 232 233 234 235 236 237 238 239 240 241
  void fix_length_and_dec();
  void fix_num_length_and_dec();
  virtual void find_num_type()= 0; /* To be called from fix_length_and_dec */

  double val_real();
  longlong val_int();
  my_decimal *val_decimal(my_decimal *);
  String *val_str(String*str);

  virtual longlong int_op()= 0;
  virtual double real_op()= 0;
  virtual my_decimal *decimal_op(my_decimal *)= 0;
242
  virtual String *str_op(String *)= 0;
243
  bool is_null() { update_null_value(); return null_value; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
244 245
};

246 247 248 249 250 251
/* function where type of result detected by first argument */
class Item_func_num1: public Item_func_numhybrid
{
public:
  Item_func_num1(Item *a) :Item_func_numhybrid(a) {}
  Item_func_num1(Item *a, Item *b) :Item_func_numhybrid(a, b) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
252

253 254
  void fix_num_length_and_dec();
  void find_num_type();
255
  String *str_op(String *str) { DBUG_ASSERT(0); return 0; }
256
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
257
};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
258

259 260 261

/* Base class for operations like '+', '-', '*' */
class Item_num_op :public Item_func_numhybrid
bk@work.mysql.com's avatar
bk@work.mysql.com committed
262 263
{
 public:
264 265
  Item_num_op(Item *a,Item *b) :Item_func_numhybrid(a, b) {}
  virtual void result_precision()= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
266
  void print(String *str) { print_op(str); }
267
  void find_num_type();
268
  String *str_op(String *str) { DBUG_ASSERT(0); return 0; }
269
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
270 271 272 273 274 275
};


class Item_int_func :public Item_func
{
public:
276 277 278 279 280 281
  Item_int_func() :Item_func() { max_length= 21; }
  Item_int_func(Item *a) :Item_func(a) { max_length= 21; }
  Item_int_func(Item *a,Item *b) :Item_func(a,b) { max_length= 21; }
  Item_int_func(Item *a,Item *b,Item *c) :Item_func(a,b,c)
  { max_length= 21; }
  Item_int_func(List<Item> &list) :Item_func(list) { max_length= 21; }
282
  Item_int_func(THD *thd, Item_int_func *item) :Item_func(thd, item) {}
283
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
284 285
  String *val_str(String*str);
  enum Item_result result_type () const { return INT_RESULT; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
286
  void fix_length_and_dec() {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
287 288
};

289

290 291 292 293 294
class Item_func_connection_id :public Item_int_func
{
  longlong value;

public:
295
  Item_func_connection_id() {}
296 297 298 299 300 301 302
  const char *func_name() const { return "connection_id"; }
  void fix_length_and_dec();
  bool fix_fields(THD *thd, Item **ref);
  longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
};


303 304 305 306
class Item_func_signed :public Item_int_func
{
public:
  Item_func_signed(Item *a) :Item_int_func(a) {}
307
  const char *func_name() const { return "cast_as_signed"; }
308 309
  longlong val_int();
  longlong val_int_from_str(int *error);
310
  void fix_length_and_dec()
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
311
  { max_length=args[0]->max_length; unsigned_flag=0; }
312
  void print(String *str);
313
  uint decimal_precision() const { return args[0]->decimal_precision(); }
314
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
315 316
};

317

318
class Item_func_unsigned :public Item_func_signed
319 320
{
public:
321
  Item_func_unsigned(Item *a) :Item_func_signed(a) {}
322
  const char *func_name() const { return "cast_as_unsigned"; }
323
  void fix_length_and_dec()
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
324
  { max_length=args[0]->max_length; unsigned_flag=1; }
325
  longlong val_int();
326
  void print(String *str);
327 328 329
};


330
class Item_decimal_typecast :public Item_func
bk@work.mysql.com's avatar
bk@work.mysql.com committed
331
{
332
  my_decimal decimal_value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
333
public:
334 335 336
  Item_decimal_typecast(Item *a, int len, int dec) :Item_func(a)
  {
    decimals= dec;
337
    max_length= my_decimal_precision_to_length(len, dec, unsigned_flag);
338 339
  }
  String *val_str(String *str);
340
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
341
  longlong val_int();
342 343
  my_decimal *val_decimal(my_decimal*);
  enum Item_result result_type () const { return DECIMAL_RESULT; }
344
  enum_field_types field_type() const { return MYSQL_TYPE_NEWDECIMAL; }
345
  void fix_length_and_dec() {};
346 347
  const char *func_name() const { return "decimal_typecast"; }
  void print(String *);
348
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
};


class Item_func_additive_op :public Item_num_op
{
public:
  Item_func_additive_op(Item *a,Item *b) :Item_num_op(a,b) {}
  void result_precision();
};


class Item_func_plus :public Item_func_additive_op
{
public:
  Item_func_plus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
  const char *func_name() const { return "+"; }
  longlong int_op();
  double real_op();
  my_decimal *decimal_op(my_decimal *);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
368 369
};

370
class Item_func_minus :public Item_func_additive_op
bk@work.mysql.com's avatar
bk@work.mysql.com committed
371 372
{
public:
373
  Item_func_minus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
374
  const char *func_name() const { return "-"; }
375 376 377
  longlong int_op();
  double real_op();
  my_decimal *decimal_op(my_decimal *);
378
  void fix_length_and_dec();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
379 380
};

381

bk@work.mysql.com's avatar
bk@work.mysql.com committed
382 383 384 385 386
class Item_func_mul :public Item_num_op
{
public:
  Item_func_mul(Item *a,Item *b) :Item_num_op(a,b) {}
  const char *func_name() const { return "*"; }
387 388 389 390
  longlong int_op();
  double real_op();
  my_decimal *decimal_op(my_decimal *);
  void result_precision();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
391 392 393 394 395 396
};


class Item_func_div :public Item_num_op
{
public:
397
  uint prec_increment;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
398
  Item_func_div(Item *a,Item *b) :Item_num_op(a,b) {}
399
  longlong int_op() { DBUG_ASSERT(0); return 0; }
400 401
  double real_op();
  my_decimal *decimal_op(my_decimal *);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
402 403
  const char *func_name() const { return "/"; }
  void fix_length_and_dec();
404
  void result_precision();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
405 406 407
};


408
class Item_func_int_div :public Item_int_func
409 410
{
public:
411
  Item_func_int_div(Item *a,Item *b) :Item_int_func(a,b)
412
  {}
413 414 415
  longlong val_int();
  const char *func_name() const { return "DIV"; }
  void fix_length_and_dec();
416
  void print(String *str) { print_op(str); }
417
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
418 419 420
};


bk@work.mysql.com's avatar
bk@work.mysql.com committed
421 422 423 424
class Item_func_mod :public Item_num_op
{
public:
  Item_func_mod(Item *a,Item *b) :Item_num_op(a,b) {}
425 426 427
  longlong int_op();
  double real_op();
  my_decimal *decimal_op(my_decimal *);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
428
  const char *func_name() const { return "%"; }
429
  void result_precision();
430
  void fix_length_and_dec();
431 432 433
};


434
class Item_func_neg :public Item_func_num1
bk@work.mysql.com's avatar
bk@work.mysql.com committed
435 436
{
public:
437
  Item_func_neg(Item *a) :Item_func_num1(a) {}
438 439 440
  double real_op();
  longlong int_op();
  my_decimal *decimal_op(my_decimal *);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
441
  const char *func_name() const { return "-"; }
442
  void fix_length_and_dec();
443
  void fix_num_length_and_dec();
444
  uint decimal_precision() const { return args[0]->decimal_precision(); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
445 446 447
};


448
class Item_func_abs :public Item_func_num1
bk@work.mysql.com's avatar
bk@work.mysql.com committed
449 450
{
public:
451 452 453 454
  Item_func_abs(Item *a) :Item_func_num1(a) {}
  double real_op();
  longlong int_op();
  my_decimal *decimal_op(my_decimal *);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
455 456 457 458
  const char *func_name() const { return "abs"; }
  void fix_length_and_dec();
};

459
// A class to handle logarithmic and trigonometric functions
bk@work.mysql.com's avatar
bk@work.mysql.com committed
460 461 462 463 464 465 466 467

class Item_dec_func :public Item_real_func
{
 public:
  Item_dec_func(Item *a) :Item_real_func(a) {}
  Item_dec_func(Item *a,Item *b) :Item_real_func(a,b) {}
  void fix_length_and_dec()
  {
468
    decimals=NOT_FIXED_DEC; max_length=float_length(decimals);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
469 470 471 472 473 474 475
    maybe_null=1;
  }
  inline double fix_result(double value)
  {
#ifndef HAVE_FINITE
    return value;
#else
476
    /* The following should be safe, even if we compare doubles */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
477 478 479 480 481 482 483 484 485 486 487 488
    if (finite(value) && value != POSTFIX_ERROR)
      return value;
    null_value=1;
    return 0.0;
#endif
  }
};

class Item_func_exp :public Item_dec_func
{
public:
  Item_func_exp(Item *a) :Item_dec_func(a) {}
489
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
490
  const char *func_name() const { return "exp"; }
491
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
492 493
};

494 495 496 497 498

class Item_func_ln :public Item_dec_func
{
public:
  Item_func_ln(Item *a) :Item_dec_func(a) {}
499
  double val_real();
500
  const char *func_name() const { return "ln"; }
501
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
502 503 504
};


bk@work.mysql.com's avatar
bk@work.mysql.com committed
505 506 507 508
class Item_func_log :public Item_dec_func
{
public:
  Item_func_log(Item *a) :Item_dec_func(a) {}
509
  Item_func_log(Item *a,Item *b) :Item_dec_func(a,b) {}
510
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
511
  const char *func_name() const { return "log"; }
512
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
513 514 515
};


516 517 518 519
class Item_func_log2 :public Item_dec_func
{
public:
  Item_func_log2(Item *a) :Item_dec_func(a) {}
520
  double val_real();
521
  const char *func_name() const { return "log2"; }
522
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
523 524 525
};


bk@work.mysql.com's avatar
bk@work.mysql.com committed
526 527 528 529
class Item_func_log10 :public Item_dec_func
{
public:
  Item_func_log10(Item *a) :Item_dec_func(a) {}
530
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
531
  const char *func_name() const { return "log10"; }
532
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
533 534 535 536 537 538 539
};


class Item_func_sqrt :public Item_dec_func
{
public:
  Item_func_sqrt(Item *a) :Item_dec_func(a) {}
540
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
541
  const char *func_name() const { return "sqrt"; }
542
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
543 544 545 546 547 548 549
};


class Item_func_pow :public Item_dec_func
{
public:
  Item_func_pow(Item *a,Item *b) :Item_dec_func(a,b) {}
550
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
551
  const char *func_name() const { return "pow"; }
552
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
553 554 555 556 557
};


class Item_func_acos :public Item_dec_func
{
558
public:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
559
  Item_func_acos(Item *a) :Item_dec_func(a) {}
560
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
561
  const char *func_name() const { return "acos"; }
562
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
563 564 565 566
};

class Item_func_asin :public Item_dec_func
{
567
public:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
568
  Item_func_asin(Item *a) :Item_dec_func(a) {}
569
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
570
  const char *func_name() const { return "asin"; }
571
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
572 573 574 575
};

class Item_func_atan :public Item_dec_func
{
576
public:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
577 578
  Item_func_atan(Item *a) :Item_dec_func(a) {}
  Item_func_atan(Item *a,Item *b) :Item_dec_func(a,b) {}
579
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
580
  const char *func_name() const { return "atan"; }
581
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
582 583 584 585
};

class Item_func_cos :public Item_dec_func
{
586
public:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
587
  Item_func_cos(Item *a) :Item_dec_func(a) {}
588
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
589
  const char *func_name() const { return "cos"; }
590
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
591 592 593 594
};

class Item_func_sin :public Item_dec_func
{
595
public:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
596
  Item_func_sin(Item *a) :Item_dec_func(a) {}
597
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
598
  const char *func_name() const { return "sin"; }
599
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
600 601 602 603
};

class Item_func_tan :public Item_dec_func
{
604
public:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
605
  Item_func_tan(Item *a) :Item_dec_func(a) {}
606
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
607
  const char *func_name() const { return "tan"; }
608
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
609 610 611 612 613 614 615 616 617 618
};

class Item_func_integer :public Item_int_func
{
public:
  inline Item_func_integer(Item *a) :Item_int_func(a) {}
  void fix_length_and_dec();
};


619 620 621 622 623 624 625 626 627 628
class Item_func_int_val :public Item_func_num1
{
public:
  Item_func_int_val(Item *a) :Item_func_num1(a) {}
  void fix_num_length_and_dec();
  void find_num_type();
};


class Item_func_ceiling :public Item_func_int_val
bk@work.mysql.com's avatar
bk@work.mysql.com committed
629 630
{
public:
631
  Item_func_ceiling(Item *a) :Item_func_int_val(a) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
632
  const char *func_name() const { return "ceiling"; }
633 634 635
  longlong int_op();
  double real_op();
  my_decimal *decimal_op(my_decimal *);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
636 637
};

638 639

class Item_func_floor :public Item_func_int_val
bk@work.mysql.com's avatar
bk@work.mysql.com committed
640 641
{
public:
642
  Item_func_floor(Item *a) :Item_func_int_val(a) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
643
  const char *func_name() const { return "floor"; }
644 645 646
  longlong int_op();
  double real_op();
  my_decimal *decimal_op(my_decimal *);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
647 648 649 650
};

/* This handles round and truncate */

651
class Item_func_round :public Item_func_num1
bk@work.mysql.com's avatar
bk@work.mysql.com committed
652 653 654
{
  bool truncate;
public:
655 656
  Item_func_round(Item *a, Item *b, bool trunc_arg)
    :Item_func_num1(a,b), truncate(trunc_arg) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
657
  const char *func_name() const { return truncate ? "truncate" : "round"; }
658 659 660
  double real_op();
  longlong int_op();
  my_decimal *decimal_op(my_decimal *);
661
  void fix_length_and_dec();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
662 663 664 665 666
};


class Item_func_rand :public Item_real_func
{
667
  struct rand_struct *rand;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
668
public:
669 670
  Item_func_rand(Item *a) :Item_real_func(a), rand(0) {}
  Item_func_rand()	  :Item_real_func() {}
671
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
672 673
  const char *func_name() const { return "rand"; }
  bool const_item() const { return 0; }
674
  void update_used_tables();
675
  bool fix_fields(THD *thd, Item **ref);
676 677
private:
  void seed_random (Item * val);  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
678 679 680 681 682 683 684 685 686
};


class Item_func_sign :public Item_int_func
{
public:
  Item_func_sign(Item *a) :Item_int_func(a) {}
  const char *func_name() const { return "sign"; }
  longlong val_int();
687
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
688 689 690 691 692 693 694
};


class Item_func_units :public Item_real_func
{
  char *name;
  double mul,add;
695
public:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
696 697
  Item_func_units(char *name_arg,Item *a,double mul_arg,double add_arg)
    :Item_real_func(a),name(name_arg),mul(mul_arg),add(add_arg) {}
698
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
699
  const char *func_name() const { return name; }
700 701
  void fix_length_and_dec()
  { decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
702
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
703 704 705 706 707 708 709 710
};


class Item_func_min_max :public Item_func
{
  Item_result cmp_type;
  String tmp_value;
  int cmp_sign;
711 712 713 714 715 716
  /* TRUE <=> arguments should be compared in the DATETIME context. */
  bool compare_as_dates;
  /* An item used for issuing warnings while string to DATETIME conversion. */
  Item *datetime_item;
  THD *thd;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
717 718
public:
  Item_func_min_max(List<Item> &list,int cmp_sign_arg) :Item_func(list),
719 720
    cmp_type(INT_RESULT), cmp_sign(cmp_sign_arg), compare_as_dates(FALSE),
    datetime_item(0) {}
721
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
722 723
  longlong val_int();
  String *val_str(String *);
724
  my_decimal *val_decimal(my_decimal *);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
725 726
  void fix_length_and_dec();
  enum Item_result result_type () const { return cmp_type; }
727
  bool result_as_longlong() { return compare_as_dates; };
728
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
729
  uint cmp_datetimes(ulonglong *value);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
};

class Item_func_min :public Item_func_min_max
{
public:
  Item_func_min(List<Item> &list) :Item_func_min_max(list,1) {}
  const char *func_name() const { return "least"; }
};

class Item_func_max :public Item_func_min_max
{
public:
  Item_func_max(List<Item> &list) :Item_func_min_max(list,-1) {}
  const char *func_name() const { return "greatest"; }
};

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
746

747 748 749 750 751 752 753 754 755
/* 
  Objects of this class are used for ROLLUP queries to wrap up 
  each constant item referred to in GROUP BY list. 
*/

class Item_func_rollup_const :public Item_func
{
public:
  Item_func_rollup_const(Item *a) :Item_func(a)
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
756 757 758 759 760
  {
    name= a->name;
    name_length= a->name_length;
  }
  double val_real() { return args[0]->val_real(); }
761 762
  longlong val_int() { return args[0]->val_int(); }
  String *val_str(String *str) { return args[0]->val_str(str); }
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
763
  my_decimal *val_decimal(my_decimal *dec) { return args[0]->val_decimal(dec); }
764 765 766 767 768 769 770 771 772 773 774 775
  const char *func_name() const { return "rollup_const"; }
  bool const_item() const { return 0; }
  Item_result result_type() const { return args[0]->result_type(); }
  void fix_length_and_dec()
  {
    collation= args[0]->collation;
    max_length= args[0]->max_length;
    decimals=args[0]->decimals; 
  }
};


bk@work.mysql.com's avatar
bk@work.mysql.com committed
776 777 778 779 780 781 782 783
class Item_func_length :public Item_int_func
{
  String value;
public:
  Item_func_length(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "length"; }
  void fix_length_and_dec() { max_length=10; }
784
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
785 786
};

787 788 789 790
class Item_func_bit_length :public Item_func_length
{
public:
  Item_func_bit_length(Item *a) :Item_func_length(a) {}
791 792
  longlong val_int()
    { DBUG_ASSERT(fixed == 1); return Item_func_length::val_int()*8; }
793 794 795
  const char *func_name() const { return "bit_length"; }
};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
796 797 798 799 800 801 802 803
class Item_func_char_length :public Item_int_func
{
  String value;
public:
  Item_func_char_length(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "char_length"; }
  void fix_length_and_dec() { max_length=10; }
804
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
805 806
};

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
807 808 809 810 811 812
class Item_func_coercibility :public Item_int_func
{
public:
  Item_func_coercibility(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "coercibility"; }
813 814
  void fix_length_and_dec() { max_length=10; maybe_null= 0; }
  table_map not_null_tables() const { return 0; }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
815 816
};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
817 818 819
class Item_func_locate :public Item_int_func
{
  String value1,value2;
820
  DTCollation cmp_collation;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
821 822 823 824 825
public:
  Item_func_locate(Item *a,Item *b) :Item_int_func(a,b) {}
  Item_func_locate(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
  const char *func_name() const { return "locate"; }
  longlong val_int();
826
  void fix_length_and_dec();
827
  void print(String *str);
828
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
829 830 831 832 833 834
};


class Item_func_field :public Item_int_func
{
  String value,tmp;
835 836
  Item_result cmp_type;
  DTCollation cmp_collation;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
837
public:
838
  Item_func_field(List<Item> &list) :Item_int_func(list) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
839 840
  longlong val_int();
  const char *func_name() const { return "field"; }
841
  void fix_length_and_dec();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
842 843 844 845 846 847 848 849 850 851 852
};


class Item_func_ascii :public Item_int_func
{
  String value;
public:
  Item_func_ascii(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "ascii"; }
  void fix_length_and_dec() { max_length=3; }
853
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
854 855 856 857 858 859 860 861 862
};

class Item_func_ord :public Item_int_func
{
  String value;
public:
  Item_func_ord(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "ord"; }
863
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
864 865 866 867 868 869 870
};

class Item_func_find_in_set :public Item_int_func
{
  String value,value2;
  uint enum_value;
  ulonglong enum_bit;
871
  DTCollation cmp_collation;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
872 873 874 875 876
public:
  Item_func_find_in_set(Item *a,Item *b) :Item_int_func(a,b),enum_value(0) {}
  longlong val_int();
  const char *func_name() const { return "find_in_set"; }
  void fix_length_and_dec();
877
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
878 879
};

880
/* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
881

882
class Item_func_bit: public Item_int_func
bk@work.mysql.com's avatar
bk@work.mysql.com committed
883 884
{
public:
885 886 887
  Item_func_bit(Item *a, Item *b) :Item_int_func(a, b) {}
  Item_func_bit(Item *a) :Item_int_func(a) {}
  void fix_length_and_dec() { unsigned_flag= 1; }
monty@mysql.com's avatar
monty@mysql.com committed
888
  void print(String *str) { print_op(str); }
889
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
890 891 892 893 894 895
};

class Item_func_bit_or :public Item_func_bit
{
public:
  Item_func_bit_or(Item *a, Item *b) :Item_func_bit(a, b) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
896 897 898 899
  longlong val_int();
  const char *func_name() const { return "|"; }
};

900
class Item_func_bit_and :public Item_func_bit
bk@work.mysql.com's avatar
bk@work.mysql.com committed
901 902
{
public:
903
  Item_func_bit_and(Item *a, Item *b) :Item_func_bit(a, b) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
904 905 906 907 908 909 910 911 912 913
  longlong val_int();
  const char *func_name() const { return "&"; }
};

class Item_func_bit_count :public Item_int_func
{
public:
  Item_func_bit_count(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "bit_count"; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
914
  void fix_length_and_dec() { max_length=2; }
915
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
916 917
};

918
class Item_func_shift_left :public Item_func_bit
bk@work.mysql.com's avatar
bk@work.mysql.com committed
919 920
{
public:
921
  Item_func_shift_left(Item *a, Item *b) :Item_func_bit(a, b) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
922 923 924 925
  longlong val_int();
  const char *func_name() const { return "<<"; }
};

926
class Item_func_shift_right :public Item_func_bit
bk@work.mysql.com's avatar
bk@work.mysql.com committed
927 928
{
public:
929
  Item_func_shift_right(Item *a, Item *b) :Item_func_bit(a, b) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
930 931 932 933
  longlong val_int();
  const char *func_name() const { return ">>"; }
};

934
class Item_func_bit_neg :public Item_func_bit
bk@work.mysql.com's avatar
bk@work.mysql.com committed
935 936
{
public:
937
  Item_func_bit_neg(Item *a) :Item_func_bit(a) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
938 939
  longlong val_int();
  const char *func_name() const { return "~"; }
monty@mysql.com's avatar
monty@mysql.com committed
940
  void print(String *str) { Item_func::print(str); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
941 942
};

monty@mysql.com's avatar
monty@mysql.com committed
943

944
class Item_func_last_insert_id :public Item_int_func
bk@work.mysql.com's avatar
bk@work.mysql.com committed
945 946
{
public:
947 948
  Item_func_last_insert_id() :Item_int_func() {}
  Item_func_last_insert_id(Item *a) :Item_int_func(a) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
949 950
  longlong val_int();
  const char *func_name() const { return "last_insert_id"; }
951 952 953 954 955
  void fix_length_and_dec()
  {
    if (arg_count)
      max_length= args[0]->max_length;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
956 957
};

958

bk@work.mysql.com's avatar
bk@work.mysql.com committed
959 960
class Item_func_benchmark :public Item_int_func
{
961
public:
962 963
  Item_func_benchmark(Item *count_expr, Item *expr)
    :Item_int_func(count_expr, expr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
964 965 966
  {}
  longlong val_int();
  const char *func_name() const { return "benchmark"; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
967
  void fix_length_and_dec() { max_length=1; maybe_null=0; }
968
  void print(String *str);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
969 970 971
};


972 973 974 975
class Item_func_sleep :public Item_int_func
{
public:
  Item_func_sleep(Item *a) :Item_int_func(a) {}
976
  bool const_item() const { return 0; }
977
  const char *func_name() const { return "sleep"; }
978 979 980 981 982
  void update_used_tables()
  {
    Item_int_func::update_used_tables();
    used_tables_cache|= RAND_TABLE_BIT;
  }
983 984 985 986 987
  longlong val_int();
};



bk@work.mysql.com's avatar
bk@work.mysql.com committed
988 989 990 991
#ifdef HAVE_DLOPEN

class Item_udf_func :public Item_func
{
992
protected:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
993 994 995
  udf_handler udf;

public:
996 997
  Item_udf_func(udf_func *udf_arg)
    :Item_func(), udf(udf_arg) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
998 999 1000
  Item_udf_func(udf_func *udf_arg, List<Item> &list)
    :Item_func(list), udf(udf_arg) {}
  const char *func_name() const { return udf.name(); }
1001
  enum Functype functype() const   { return UDF_FUNC; }
1002
  bool fix_fields(THD *thd, Item **ref)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1003
  {
1004
    DBUG_ASSERT(fixed == 0);
1005
    bool res= udf.fix_fields(thd, this, arg_count, args);
1006 1007
    used_tables_cache= udf.used_tables_cache;
    const_item_cache= udf.const_item_cache;
1008
    fixed= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1009 1010
    return res;
  }
1011
  void cleanup();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1012
  Item_result result_type () const { return udf.result_type(); }
1013
  table_map not_null_tables() const { return 0; }
1014
  bool is_expensive() { return 1; }
1015
  void print(String *str);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1016 1017 1018 1019 1020 1021
};


class Item_func_udf_float :public Item_udf_func
{
 public:
1022 1023 1024 1025 1026
  Item_func_udf_float(udf_func *udf_arg)
    :Item_udf_func(udf_arg) {}
  Item_func_udf_float(udf_func *udf_arg,
                      List<Item> &list)
    :Item_udf_func(udf_arg, list) {}
1027
  longlong val_int()
1028 1029
  {
    DBUG_ASSERT(fixed == 1);
1030
    return (longlong) rint(Item_func_udf_float::val_real());
1031
  }
1032 1033 1034 1035 1036 1037 1038 1039
  my_decimal *val_decimal(my_decimal *dec_buf)
  {
    double res=val_real();
    if (null_value)
      return NULL;
    double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
    return dec_buf;
  }
1040
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1041 1042 1043 1044 1045 1046 1047 1048
  String *val_str(String *str);
  void fix_length_and_dec() { fix_num_length_and_dec(); }
};


class Item_func_udf_int :public Item_udf_func
{
public:
1049 1050 1051 1052 1053
  Item_func_udf_int(udf_func *udf_arg)
    :Item_udf_func(udf_arg) {}
  Item_func_udf_int(udf_func *udf_arg,
                    List<Item> &list)
    :Item_udf_func(udf_arg, list) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1054
  longlong val_int();
1055
  double val_real() { return (double) Item_func_udf_int::val_int(); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1056 1057
  String *val_str(String *str);
  enum Item_result result_type () const { return INT_RESULT; }
1058 1059 1060 1061 1062 1063 1064
  void fix_length_and_dec() { decimals= 0; max_length= 21; }
};


class Item_func_udf_decimal :public Item_udf_func
{
public:
1065 1066
  Item_func_udf_decimal(udf_func *udf_arg)
    :Item_udf_func(udf_arg) {}
1067
  Item_func_udf_decimal(udf_func *udf_arg, List<Item> &list)
1068
    :Item_udf_func(udf_arg, list) {}
1069 1070 1071 1072 1073 1074
  longlong val_int();
  double val_real();
  my_decimal *val_decimal(my_decimal *);
  String *val_str(String *str);
  enum Item_result result_type () const { return DECIMAL_RESULT; }
  void fix_length_and_dec();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1075 1076 1077 1078 1079 1080
};


class Item_func_udf_str :public Item_udf_func
{
public:
1081 1082
  Item_func_udf_str(udf_func *udf_arg)
    :Item_udf_func(udf_arg) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1083
  Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
1084
    :Item_udf_func(udf_arg, list) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1085
  String *val_str(String *);
1086
  double val_real()
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1087
  {
monty@mysql.com's avatar
monty@mysql.com committed
1088 1089 1090 1091 1092 1093
    int err_not_used;
    char *end_not_used;
    String *res;
    res= val_str(&str_value);
    return res ? my_strntod(res->charset(),(char*) res->ptr(), 
                            res->length(), &end_not_used, &err_not_used) : 0.0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1094 1095 1096
  }
  longlong val_int()
  {
monty@mysql.com's avatar
monty@mysql.com committed
1097
    int err_not_used;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1098
    String *res;  res=val_str(&str_value);
monty@mysql.com's avatar
monty@mysql.com committed
1099 1100
    return res ? my_strntoll(res->charset(),res->ptr(),res->length(),10,
                             (char**) 0, &err_not_used) : (longlong) 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1101
  }
1102 1103 1104 1105 1106 1107 1108 1109
  my_decimal *val_decimal(my_decimal *dec_buf)
  {
    String *res=val_str(&str_value);
    if (!res)
      return NULL;
    string2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
    return dec_buf;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1110 1111 1112 1113 1114 1115 1116 1117 1118
  enum Item_result result_type () const { return STRING_RESULT; }
  void fix_length_and_dec();
};

#else /* Dummy functions to get sql_yacc.cc compiled */

class Item_func_udf_float :public Item_real_func
{
 public:
1119 1120 1121 1122
  Item_func_udf_float(udf_func *udf_arg)
    :Item_real_func() {}
  Item_func_udf_float(udf_func *udf_arg, List<Item> &list)
    :Item_real_func(list) {}
1123
  double val_real() { DBUG_ASSERT(fixed == 1); return 0.0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1124 1125 1126 1127 1128 1129
};


class Item_func_udf_int :public Item_int_func
{
public:
1130 1131 1132 1133
  Item_func_udf_int(udf_func *udf_arg)
    :Item_int_func() {}
  Item_func_udf_int(udf_func *udf_arg, List<Item> &list)
    :Item_int_func(list) {}
1134
  longlong val_int() { DBUG_ASSERT(fixed == 1); return 0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1135 1136 1137
};


1138 1139 1140
class Item_func_udf_decimal :public Item_int_func
{
public:
1141 1142 1143 1144
  Item_func_udf_decimal(udf_func *udf_arg)
    :Item_int_func() {}
  Item_func_udf_decimal(udf_func *udf_arg, List<Item> &list)
    :Item_int_func(list) {}
1145 1146 1147 1148
  my_decimal *val_decimal(my_decimal *) { DBUG_ASSERT(fixed == 1); return 0; }
};


bk@work.mysql.com's avatar
bk@work.mysql.com committed
1149 1150 1151
class Item_func_udf_str :public Item_func
{
public:
1152 1153 1154 1155
  Item_func_udf_str(udf_func *udf_arg)
    :Item_func() {}
  Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
    :Item_func(list) {}
1156 1157
  String *val_str(String *)
    { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
1158
  double val_real() { DBUG_ASSERT(fixed == 1); null_value= 1; return 0.0; }
1159
  longlong val_int() { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
  enum Item_result result_type () const { return STRING_RESULT; }
  void fix_length_and_dec() { maybe_null=1; max_length=0; }
};

#endif /* HAVE_DLOPEN */

/*
** User level locks
*/

1170
class User_level_lock;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1171
void item_user_lock_init(void);
1172
void item_user_lock_release(User_level_lock *ull);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1173 1174 1175 1176 1177 1178 1179 1180 1181
void item_user_lock_free(void);

class Item_func_get_lock :public Item_int_func
{
  String value;
 public:
  Item_func_get_lock(Item *a,Item *b) :Item_int_func(a,b) {}
  longlong val_int();
  const char *func_name() const { return "get_lock"; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1182
  void fix_length_and_dec() { max_length=1; maybe_null=1;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1183 1184 1185 1186 1187
};

class Item_func_release_lock :public Item_int_func
{
  String value;
1188
public:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1189 1190 1191
  Item_func_release_lock(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "release_lock"; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1192
  void fix_length_and_dec() { max_length=1; maybe_null=1;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1193 1194
};

1195 1196 1197 1198 1199
/* replication functions */

class Item_master_pos_wait :public Item_int_func
{
  String value;
1200
public:
1201
  Item_master_pos_wait(Item *a,Item *b) :Item_int_func(a,b) {}
1202
  Item_master_pos_wait(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
1203 1204
  longlong val_int();
  const char *func_name() const { return "master_pos_wait"; }
1205
  void fix_length_and_dec() { max_length=21; maybe_null=1;}
1206 1207
};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1208

1209
/* Handling of user definable variables */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1210 1211 1212 1213 1214 1215 1216

class user_var_entry;

class Item_func_set_user_var :public Item_func
{
  enum Item_result cached_result_type;
  user_var_entry *entry;
1217 1218
  char buffer[MAX_FIELD_WIDTH];
  String value;
1219
  my_decimal decimal_buff;
1220
  bool null_item;
1221 1222 1223 1224 1225
  union
  {
    longlong vint;
    double vreal;
    String *vstr;
1226
    my_decimal *vdec;
1227
  } save_result;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1228 1229

public:
1230
  LEX_STRING name; // keep it public
1231 1232 1233
  Item_func_set_user_var(LEX_STRING a,Item *b)
    :Item_func(b), cached_result_type(INT_RESULT), name(a)
  {}
1234
  enum Functype functype() const { return SUSERVAR_FUNC; }
1235
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1236 1237
  longlong val_int();
  String *val_str(String *str);
1238
  my_decimal *val_decimal(my_decimal *);
1239
  bool update_hash(void *ptr, uint length, enum Item_result type,
1240
  		   CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
1241
  bool send(Protocol *protocol, String *str_arg);
1242
  void make_field(Send_field *tmp_field);
1243
  bool check(bool use_result_field);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1244 1245
  bool update();
  enum Item_result result_type () const { return cached_result_type; }
1246
  bool fix_fields(THD *thd, Item **ref);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1247
  void fix_length_and_dec();
1248
  void print(String *str);
1249
  void print_as_stmt(String *str);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1250
  const char *func_name() const { return "set_user_var"; }
1251
  int save_in_field(Field *field, bool no_conversions);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1252 1253 1254
};


1255 1256
class Item_func_get_user_var :public Item_func,
                              private Settable_routine_parameter
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1257
{
1258
  user_var_entry *var_entry;
1259
  Item_result m_cached_result_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1260 1261

public:
1262
  LEX_STRING name; // keep it public
1263
  Item_func_get_user_var(LEX_STRING a):
kostja@vajra.(none)'s avatar
kostja@vajra.(none) committed
1264
    Item_func(), m_cached_result_type(STRING_RESULT), name(a) {}
1265 1266
  enum Functype functype() const { return GUSERVAR_FUNC; }
  LEX_STRING get_name() { return name; }
1267
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1268
  longlong val_int();
1269
  my_decimal *val_decimal(my_decimal*);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1270 1271
  String *val_str(String* str);
  void fix_length_and_dec();
1272
  void print(String *str);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1273
  enum Item_result result_type() const;
1274 1275 1276 1277
  /*
    We must always return variables as strings to guard against selects of type
    select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
  */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1278
  const char *func_name() const { return "get_user_var"; }
1279
  bool const_item() const;
1280
  table_map used_tables() const
1281
  { return const_item() ? 0 : RAND_TABLE_BIT; }
1282
  bool eq(const Item *item, bool binary_cmp) const;
1283
private:
1284
  bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
1285 1286 1287 1288 1289 1290

public:
  Settable_routine_parameter *get_settable_routine_parameter()
  {
    return this;
  }
1291
};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1292

1293

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
/*
  This item represents user variable used as out parameter (e.g in LOAD DATA),
  and it is supposed to be used only for this purprose. So it is simplified
  a lot. Actually you should never obtain its value.

  The only two reasons for this thing being an Item is possibility to store it
  in List<Item> and desire to place this code somewhere near other functions
  working with user variables.
*/
class Item_user_var_as_out_param :public Item
{
  LEX_STRING name;
  user_var_entry *entry;
public:
  Item_user_var_as_out_param(LEX_STRING a) : name(a) {}
  /* We should return something different from FIELD_ITEM here */
  enum Type type() const { return STRING_ITEM;}
  double val_real();
  longlong val_int();
  String *val_str(String *str);
  my_decimal *val_decimal(my_decimal *decimal_buffer);
  /* fix_fields() binds variable name with its entry structure */
1316
  bool fix_fields(THD *thd, Item **ref);
1317 1318 1319 1320 1321 1322
  void print(String *str);
  void set_null_value(CHARSET_INFO* cs);
  void set_value(const char *str, uint length, CHARSET_INFO* cs);
};


1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
/* A system variable */

class Item_func_get_system_var :public Item_func
{
  sys_var *var;
  enum_var_type var_type;
  LEX_STRING component;
public:
  Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
                           LEX_STRING *component_arg, const char *name_arg,
                           size_t name_len_arg);
1334
  bool fix_fields(THD *thd, Item **ref);
1335 1336 1337 1338
  /*
    Stubs for pure virtual methods. Should never be called: this
    item is always substituted with a constant in fix_fields().
  */
1339
  double val_real()         { DBUG_ASSERT(0); return 0.0; }
1340 1341 1342
  longlong val_int()        { DBUG_ASSERT(0); return 0; }
  String* val_str(String*)  { DBUG_ASSERT(0); return 0; }
  void fix_length_and_dec() { DBUG_ASSERT(0); }
1343 1344
  /* TODO: fix to support views */
  const char *func_name() const { return "get_system_var"; }
1345 1346 1347
};


bk@work.mysql.com's avatar
bk@work.mysql.com committed
1348 1349 1350
class Item_func_inet_aton : public Item_int_func
{
public:
1351 1352 1353 1354
  Item_func_inet_aton(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "inet_aton"; }
  void fix_length_and_dec() { decimals= 0; max_length= 21; maybe_null= 1; unsigned_flag= 1;}
1355
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1356 1357 1358
};


1359
/* for fulltext search */
1360
#include <ft_global.h>
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1361 1362 1363 1364

class Item_func_match :public Item_real_func
{
public:
1365
  uint key, flags;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1366
  bool join_key;
1367 1368 1369 1370
  DTCollation cmp_collation;
  FT_INFO *ft_handler;
  TABLE *table;
  Item_func_match *master;   // for master-slave optimization
1371 1372
  Item *concat_ws;           // Item_func_concat_ws
  String value;              // value of concat_ws
1373
  String search_value;       // key_item()'s value converted to cmp_collation
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1374

serg@serg.mylan's avatar
serg@serg.mylan committed
1375
  Item_func_match(List<Item> &a, uint b): Item_real_func(a), key(0), flags(b),
1376
       join_key(0), ft_handler(0), table(0), master(0), concat_ws(0) { }
hf@deer.(none)'s avatar
hf@deer.(none) committed
1377
  void cleanup()
1378
  {
1379
    DBUG_ENTER("Item_func_match");
1380
    Item_real_func::cleanup();
1381
    if (!master && ft_handler)
1382
      ft_handler->please->close_search(ft_handler);
1383
    ft_handler= 0;
1384
    concat_ws= 0;
1385
    DBUG_VOID_RETURN;
1386
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1387
  enum Functype functype() const { return FT_FUNC; }
1388
  const char *func_name() const { return "match"; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1389
  void update_used_tables() {}
1390
  table_map not_null_tables() const { return 0; }
1391
  bool fix_fields(THD *thd, Item **ref);
1392
  bool eq(const Item *, bool binary_cmp) const;
1393
  /* The following should be safe, even if we compare doubles */
1394 1395
  longlong val_int() { DBUG_ASSERT(fixed == 1); return val_real() != 0.0; }
  double val_real();
1396
  void print(String *str);
1397 1398

  bool fix_index();
1399
  void init_search(bool no_order);
1400
};
1401

1402

1403
class Item_func_bit_xor : public Item_func_bit
1404 1405
{
public:
1406
  Item_func_bit_xor(Item *a, Item *b) :Item_func_bit(a, b) {}
1407 1408 1409 1410
  longlong val_int();
  const char *func_name() const { return "^"; }
};

1411
class Item_func_is_free_lock :public Item_int_func
1412 1413 1414
{
  String value;
public:
1415
  Item_func_is_free_lock(Item *a) :Item_int_func(a) {}
1416
  longlong val_int();
1417
  const char *func_name() const { return "is_free_lock"; }
1418
  void fix_length_and_dec() { decimals=0; max_length=1; maybe_null=1;}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1419 1420
};

hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
class Item_func_is_used_lock :public Item_int_func
{
  String value;
public:
  Item_func_is_used_lock(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "is_used_lock"; }
  void fix_length_and_dec() { decimals=0; max_length=10; maybe_null=1;}
};

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1431 1432
/* For type casts */

1433
enum Cast_target
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1434 1435
{
  ITEM_CAST_BINARY, ITEM_CAST_SIGNED_INT, ITEM_CAST_UNSIGNED_INT,
1436 1437
  ITEM_CAST_DATE, ITEM_CAST_TIME, ITEM_CAST_DATETIME, ITEM_CAST_CHAR,
  ITEM_CAST_DECIMAL
1438
};
1439 1440


1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
class Item_func_row_count :public Item_int_func
{
public:
  Item_func_row_count() :Item_int_func() {}
  longlong val_int();
  const char *func_name() const { return "row_count"; }
  void fix_length_and_dec() { decimals= 0; maybe_null=0; }
};


1451 1452 1453 1454 1455 1456 1457
/*
 *
 * Stored FUNCTIONs
 *
 */

class sp_head;
1458
class sp_name;
1459
struct st_sp_security_context;
1460 1461 1462 1463

class Item_func_sp :public Item_func
{
private:
1464
  Name_resolution_context *context;
1465
  sp_name *m_name;
1466
  mutable sp_head *m_sp;
1467
  TABLE *dummy_table;
1468
  uchar result_buf[64];
1469 1470 1471 1472
  /*
     The result field of the concrete stored function.
  */
  Field *sp_result_field;
1473

1474 1475 1476
  bool execute();
  bool execute_impl(THD *thd);
  bool init_result_field(THD *thd);
1477
  
1478 1479
public:

1480
  Item_func_sp(Name_resolution_context *context_arg, sp_name *name);
1481

1482 1483
  Item_func_sp(Name_resolution_context *context_arg,
               sp_name *name, List<Item> &list);
1484 1485 1486 1487

  virtual ~Item_func_sp()
  {}

1488 1489
  table_map used_tables() const { return RAND_TABLE_BIT; }

1490
  void cleanup();
1491

1492
  const char *func_name() const;
1493

1494 1495
  enum enum_field_types field_type() const;

1496 1497 1498 1499
  Field *tmp_table_field(TABLE *t_arg);

  void make_field(Send_field *tmp_field);

1500 1501 1502 1503
  Item_result result_type() const;

  longlong val_int()
  {
1504
    if (execute())
1505
      return (longlong) 0;
1506
    return sp_result_field->val_int();
1507 1508
  }

1509
  double val_real()
1510
  {
1511
    if (execute())
1512
      return 0.0;
1513
    return sp_result_field->val_real();
1514 1515
  }

1516 1517
  my_decimal *val_decimal(my_decimal *dec_buf)
  {
1518
    if (execute())
1519
      return NULL;
1520
    return sp_result_field->val_decimal(dec_buf);
1521 1522
  }

1523 1524
  String *val_str(String *str)
  {
1525 1526 1527 1528
    String buf;
    char buff[20];
    buf.set(buff, 20, str->charset());
    buf.length(0);
1529
    if (execute())
1530
      return NULL;
1531 1532 1533 1534 1535 1536
    /*
      result_field will set buf pointing to internal buffer
      of the resul_field. Due to this it will change any time
      when SP is executed. In order to prevent occasional
      corruption of returned value, we make here a copy.
    */
1537
    sp_result_field->val_str(&buf);
1538 1539
    str->copy(buf);
    return str;
1540 1541
  }

1542
  virtual bool change_context_processor(uchar *cntx)
1543 1544
    { context= (Name_resolution_context *)cntx; return FALSE; }

1545
  bool sp_check_access(THD * thd);
1546
  virtual enum Functype functype() const { return FUNC_SP; }
1547

1548
  bool fix_fields(THD *thd, Item **ref);
1549
  void fix_length_and_dec(void);
1550
  bool is_expensive() { return 1; }
1551
};
monty@mysql.com's avatar
monty@mysql.com committed
1552 1553


1554 1555 1556 1557 1558 1559 1560 1561
class Item_func_found_rows :public Item_int_func
{
public:
  Item_func_found_rows() :Item_int_func() {}
  longlong val_int();
  const char *func_name() const { return "found_rows"; }
  void fix_length_and_dec() { decimals= 0; maybe_null=0; }
};
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573


void uuid_short_init();

class Item_func_uuid_short :public Item_int_func
{
public:
  Item_func_uuid_short() :Item_int_func() {}
  const char *func_name() const { return "uuid_short"; }
  longlong val_int();
  void fix_length_and_dec()
  { max_length= 21; unsigned_flag=1; }
1574
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
1575 1576
};