item_func.h 41.5 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
2

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4 5 6
   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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
7

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8 9 10 11
   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.
12

bk@work.mysql.com's avatar
bk@work.mysql.com committed
13 14 15 16 17 18 19
   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 */

20
#ifdef USE_PRAGMA_INTERFACE
bk@work.mysql.com's avatar
bk@work.mysql.com committed
21 22 23 24 25 26 27 28 29 30 31 32 33
#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:
34
  Item **args, *tmp_arg[2];
35 36 37 38
  /*
    Allowed numbers of columns in result (usually 1, which means scalar value)
    0 means get this number from first argument
  */
39
  uint allowed_arg_cols;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
40 41
public:
  uint arg_count;
42
  table_map used_tables_cache, not_null_tables_cache;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
43 44 45
  bool const_item_cache;
  enum Functype { UNKNOWN_FUNC,EQ_FUNC,EQUAL_FUNC,NE_FUNC,LT_FUNC,LE_FUNC,
		  GE_FUNC,GT_FUNC,FT_FUNC,
46
		  LIKE_FUNC,ISNULL_FUNC,ISNOTNULL_FUNC,
47 48
		  COND_AND_FUNC, COND_OR_FUNC, COND_XOR_FUNC,
                  BETWEEN, IN_FUNC, MULT_EQUAL_FUNC,
49
		  INTERVAL_FUNC, ISNOTNULLTEST_FUNC,
50 51 52 53
		  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,
54
		  SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
55 56
                  NOT_FUNC, NOT_ALL_FUNC,
                  NOW_FUNC, TRIG_COND_FUNC,
57
                  GUSERVAR_FUNC, COLLATE_FUNC,
58
                  EXTRACT_FUNC, CHAR_TYPECAST_FUNC, FUNC_SP };
59 60
  enum optimize_type { OPTIMIZE_NONE,OPTIMIZE_KEY,OPTIMIZE_OP, OPTIMIZE_NULL,
                       OPTIMIZE_EQUAL };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
61 62
  enum Type type() const { return FUNC_ITEM; }
  virtual enum Functype functype() const   { return UNKNOWN_FUNC; }
63 64
  Item_func(void):
    allowed_arg_cols(1), arg_count(0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
65
  {
66
    with_sum_func= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
67
  }
68 69
  Item_func(Item *a):
    allowed_arg_cols(1), arg_count(1)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
70
  {
71
    args= tmp_arg;
72 73
    args[0]= a;
    with_sum_func= a->with_sum_func;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
74
  }
75 76
  Item_func(Item *a,Item *b):
    allowed_arg_cols(1), arg_count(2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
77
  {
78
    args= tmp_arg;
79 80
    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
81
  }
82 83
  Item_func(Item *a,Item *b,Item *c):
    allowed_arg_cols(1)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
84
  {
85 86
    arg_count= 0;
    if ((args= (Item**) sql_alloc(sizeof(Item*)*3)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
87
    {
88 89 90
      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
91 92
    }
  }
93 94
  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
95
  {
96 97
    arg_count= 0;
    if ((args= (Item**) sql_alloc(sizeof(Item*)*4)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
98
    {
99 100 101 102
      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
103 104
    }
  }
105 106
  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
107
  {
108 109
    arg_count= 5;
    if ((args= (Item**) sql_alloc(sizeof(Item*)*5)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
110
    {
111 112 113
      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
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);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
120
  table_map used_tables() const;
121
  table_map not_null_tables() const;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
122
  void update_used_tables();
123
  bool eq(const Item *item, bool binary_cmp) const;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
124 125 126
  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]; }
127 128 129 130 131 132 133 134 135 136 137
  /*
    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
138 139
  virtual bool const_item() const { return const_item_cache; }
  inline Item **arguments() const { return args; }
140
  void set_arguments(List<Item> &list);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
141 142
  inline uint argument_count() const { return arg_count; }
  inline void remove_arguments() { arg_count=0; }
143
  void split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
144 145
  void print(String *str);
  void print_op(String *str);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
146
  void print_args(String *str, uint from);
147 148 149 150
  virtual void fix_num_length_and_dec();
  void count_only_length();
  void count_real_length();
  void count_decimal_length();
151
  inline bool get_arg0_date(TIME *ltime, uint fuzzy_date)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
152
  {
153
    return (null_value=args[0]->get_date(ltime, fuzzy_date));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
154 155 156 157 158
  }
  inline bool get_arg0_time(TIME *ltime)
  {
    return (null_value=args[0]->get_time(ltime));
  }
159 160 161 162
  bool is_null() { 
    (void) val_int();  /* Discard result. It sets null_value as side-effect. */ 
    return null_value; 
  }
163
  void signal_divide_by_null();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
164
  friend class udf_handler;
165
  Field *tmp_table_field() { return result_field; }
166
  Field *tmp_table_field(TABLE *t_arg);
167
  Item *get_tmp_table_item(THD *thd);
168 169 170

  my_decimal *val_decimal(my_decimal *);

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


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);
203
  my_decimal *val_decimal(my_decimal *decimal_value);
204
  longlong val_int()
205
    { DBUG_ASSERT(fixed == 1); return (longlong) rint(val_real()); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
206
  enum Item_result result_type () const { return REAL_RESULT; }
207 208
  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
209 210
};

211

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
226
  enum Item_result result_type () const { return hybrid_type; }
227 228 229 230 231 232 233 234 235 236 237 238
  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;
239
  virtual String *str_op(String *)= 0;
240
  bool is_null() { (void) val_real(); return null_value; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
241 242
};

243 244 245 246 247 248
/* 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
249

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

256 257 258

/* Base class for operations like '+', '-', '*' */
class Item_num_op :public Item_func_numhybrid
bk@work.mysql.com's avatar
bk@work.mysql.com committed
259 260
{
 public:
261 262
  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
263
  void print(String *str) { print_op(str); }
264
  void find_num_type();
265
  String *str_op(String *str) { DBUG_ASSERT(0); return 0; }
266
  bool check_partition_func_processor(byte *int_arg) { return FALSE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
267 268 269 270 271 272
};


class Item_int_func :public Item_func
{
public:
273 274 275 276 277 278
  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; }
279
  Item_int_func(THD *thd, Item_int_func *item) :Item_func(thd, item) {}
280
  double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
281 282
  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
283
  void fix_length_and_dec() {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
284 285
};

286

287 288 289 290 291
class Item_func_connection_id :public Item_int_func
{
  longlong value;

public:
292
  Item_func_connection_id() {}
293 294 295 296 297 298 299
  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; }
};


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

320

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


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


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
371 372
};

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

384

bk@work.mysql.com's avatar
bk@work.mysql.com committed
385 386 387 388 389
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 "*"; }
390 391 392 393
  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
394 395 396 397 398 399
};


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


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


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


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


450
class Item_func_abs :public Item_func_num1
bk@work.mysql.com's avatar
bk@work.mysql.com committed
451 452
{
public:
453 454 455 456
  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
457 458 459 460
  const char *func_name() const { return "abs"; }
  void fix_length_and_dec();
};

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

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()
  {
470
    decimals=NOT_FIXED_DEC; max_length=float_length(decimals);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
471 472 473 474 475 476 477
    maybe_null=1;
  }
  inline double fix_result(double value)
  {
#ifndef HAVE_FINITE
    return value;
#else
478
    /* The following should be safe, even if we compare doubles */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
479 480 481 482 483 484 485 486 487 488 489 490
    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) {}
491
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
492
  const char *func_name() const { return "exp"; }
493
  bool check_partition_func_processor(byte *int_arg) { return FALSE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
494 495
};

496 497 498 499 500

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


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


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


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


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


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


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

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

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

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

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

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

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


621 622 623 624 625 626 627 628 629 630
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
631 632
{
public:
633
  Item_func_ceiling(Item *a) :Item_func_int_val(a) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
634
  const char *func_name() const { return "ceiling"; }
635 636 637
  longlong int_op();
  double real_op();
  my_decimal *decimal_op(my_decimal *);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
638 639
};

640 641

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

/* This handles round and truncate */

653
class Item_func_round :public Item_func_num1
bk@work.mysql.com's avatar
bk@work.mysql.com committed
654 655 656
{
  bool truncate;
public:
657 658
  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
659
  const char *func_name() const { return truncate ? "truncate" : "round"; }
660 661 662
  double real_op();
  longlong int_op();
  my_decimal *decimal_op(my_decimal *);
663
  void fix_length_and_dec();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
664 665 666 667 668
};


class Item_func_rand :public Item_real_func
{
669
  struct rand_struct *rand;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
670
public:
671 672
  Item_func_rand(Item *a) :Item_real_func(a), rand(0) {}
  Item_func_rand()	  :Item_real_func() {}
673
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
674 675
  const char *func_name() const { return "rand"; }
  bool const_item() const { return 0; }
676
  void update_used_tables();
677
  bool fix_fields(THD *thd, Item **ref);
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(byte *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(byte *int_arg) { return FALSE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
703 704 705 706 707 708 709 710 711 712
};


class Item_func_min_max :public Item_func
{
  Item_result cmp_type;
  String tmp_value;
  int cmp_sign;
public:
  Item_func_min_max(List<Item> &list,int cmp_sign_arg) :Item_func(list),
713
    cmp_type(INT_RESULT), cmp_sign(cmp_sign_arg) {}
714
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
715 716
  longlong val_int();
  String *val_str(String *);
717
  my_decimal *val_decimal(my_decimal *);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
718 719
  void fix_length_and_dec();
  enum Item_result result_type () const { return cmp_type; }
720
  bool check_partition_func_processor(byte *int_arg) { return FALSE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
};

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
737

bk@work.mysql.com's avatar
bk@work.mysql.com committed
738 739 740 741 742 743 744 745
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; }
746
  bool check_partition_func_processor(byte *int_arg) { return FALSE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
747 748
};

749 750 751 752
class Item_func_bit_length :public Item_func_length
{
public:
  Item_func_bit_length(Item *a) :Item_func_length(a) {}
753 754
  longlong val_int()
    { DBUG_ASSERT(fixed == 1); return Item_func_length::val_int()*8; }
755 756 757
  const char *func_name() const { return "bit_length"; }
};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
758 759 760 761 762 763 764 765
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; }
766
  bool check_partition_func_processor(byte *int_arg) { return FALSE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
767 768
};

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
769 770 771 772 773 774
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"; }
775 776
  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
777 778
};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
779 780 781
class Item_func_locate :public Item_int_func
{
  String value1,value2;
782
  DTCollation cmp_collation;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
783 784 785 786 787
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();
788
  void fix_length_and_dec();
789
  void print(String *str);
790
  bool check_partition_func_processor(byte *int_arg) { return FALSE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
791 792 793 794 795 796
};


class Item_func_field :public Item_int_func
{
  String value,tmp;
797 798
  Item_result cmp_type;
  DTCollation cmp_collation;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
799
public:
800
  Item_func_field(List<Item> &list) :Item_int_func(list) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
801 802
  longlong val_int();
  const char *func_name() const { return "field"; }
803
  void fix_length_and_dec();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
804 805 806 807 808 809 810 811 812 813 814
};


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; }
815
  bool check_partition_func_processor(byte *int_arg) { return FALSE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
816 817 818 819 820 821 822 823 824
};

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"; }
825
  bool check_partition_func_processor(byte *int_arg) { return FALSE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
826 827 828 829 830 831 832
};

class Item_func_find_in_set :public Item_int_func
{
  String value,value2;
  uint enum_value;
  ulonglong enum_bit;
833
  DTCollation cmp_collation;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
834 835 836 837 838
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();
839
  bool check_partition_func_processor(byte *int_arg) { return FALSE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
840 841
};

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

844
class Item_func_bit: public Item_int_func
bk@work.mysql.com's avatar
bk@work.mysql.com committed
845 846
{
public:
847 848 849
  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
850
  void print(String *str) { print_op(str); }
851
  bool check_partition_func_processor(byte *int_arg) { return FALSE; }
852 853 854 855 856 857
};

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
858 859 860 861
  longlong val_int();
  const char *func_name() const { return "|"; }
};

862
class Item_func_bit_and :public Item_func_bit
bk@work.mysql.com's avatar
bk@work.mysql.com committed
863 864
{
public:
865
  Item_func_bit_and(Item *a, Item *b) :Item_func_bit(a, b) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
866 867 868 869 870 871 872 873 874 875
  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
876
  void fix_length_and_dec() { max_length=2; }
877
  bool check_partition_func_processor(byte *int_arg) { return FALSE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
878 879
};

880
class Item_func_shift_left :public Item_func_bit
bk@work.mysql.com's avatar
bk@work.mysql.com committed
881 882
{
public:
883
  Item_func_shift_left(Item *a, Item *b) :Item_func_bit(a, b) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
884 885 886 887
  longlong val_int();
  const char *func_name() const { return "<<"; }
};

888
class Item_func_shift_right :public Item_func_bit
bk@work.mysql.com's avatar
bk@work.mysql.com committed
889 890
{
public:
891
  Item_func_shift_right(Item *a, Item *b) :Item_func_bit(a, b) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
892 893 894 895
  longlong val_int();
  const char *func_name() const { return ">>"; }
};

896
class Item_func_bit_neg :public Item_func_bit
bk@work.mysql.com's avatar
bk@work.mysql.com committed
897 898
{
public:
899
  Item_func_bit_neg(Item *a) :Item_func_bit(a) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
900 901
  longlong val_int();
  const char *func_name() const { return "~"; }
monty@mysql.com's avatar
monty@mysql.com committed
902
  void print(String *str) { Item_func::print(str); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
903 904
};

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

906
class Item_func_last_insert_id :public Item_int_func
bk@work.mysql.com's avatar
bk@work.mysql.com committed
907 908
{
public:
909 910
  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
911 912
  longlong val_int();
  const char *func_name() const { return "last_insert_id"; }
913 914 915 916 917
  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
918 919
};

920

bk@work.mysql.com's avatar
bk@work.mysql.com committed
921 922 923
class Item_func_benchmark :public Item_int_func
{
  ulong loop_count;
924
public:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
925 926 927 928 929
  Item_func_benchmark(ulong loop_count_arg,Item *expr)
    :Item_int_func(expr), loop_count(loop_count_arg)
  {}
  longlong val_int();
  const char *func_name() const { return "benchmark"; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
930
  void fix_length_and_dec() { max_length=1; maybe_null=0; }
931
  void print(String *str);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
932 933 934
};


935 936 937 938
class Item_func_sleep :public Item_int_func
{
public:
  Item_func_sleep(Item *a) :Item_int_func(a) {}
939
  bool const_item() const { return 0; }
940
  const char *func_name() const { return "sleep"; }
941 942 943 944 945
  void update_used_tables()
  {
    Item_int_func::update_used_tables();
    used_tables_cache|= RAND_TABLE_BIT;
  }
946 947 948 949 950
  longlong val_int();
};



bk@work.mysql.com's avatar
bk@work.mysql.com committed
951 952 953 954
#ifdef HAVE_DLOPEN

class Item_udf_func :public Item_func
{
955
protected:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
956 957 958
  udf_handler udf;

public:
959 960
  Item_udf_func(udf_func *udf_arg)
    :Item_func(), udf(udf_arg) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
961 962 963
  Item_udf_func(udf_func *udf_arg, List<Item> &list)
    :Item_func(list), udf(udf_arg) {}
  const char *func_name() const { return udf.name(); }
964
  bool fix_fields(THD *thd, Item **ref)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
965
  {
966
    DBUG_ASSERT(fixed == 0);
967
    bool res= udf.fix_fields(thd, this, arg_count, args);
968 969
    used_tables_cache= udf.used_tables_cache;
    const_item_cache= udf.const_item_cache;
970
    fixed= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
971 972
    return res;
  }
973
  void cleanup();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
974
  Item_result result_type () const { return udf.result_type(); }
975
  table_map not_null_tables() const { return 0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
976 977 978 979 980 981
};


class Item_func_udf_float :public Item_udf_func
{
 public:
982 983 984 985 986
  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) {}
987
  longlong val_int()
988 989
  {
    DBUG_ASSERT(fixed == 1);
990
    return (longlong) rint(Item_func_udf_float::val_real());
991
  }
992 993 994 995 996 997 998 999
  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;
  }
1000
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1001 1002 1003 1004 1005 1006 1007 1008
  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:
1009 1010 1011 1012 1013
  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
1014
  longlong val_int();
1015
  double val_real() { return (double) Item_func_udf_int::val_int(); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1016 1017
  String *val_str(String *str);
  enum Item_result result_type () const { return INT_RESULT; }
1018 1019 1020 1021 1022 1023 1024
  void fix_length_and_dec() { decimals= 0; max_length= 21; }
};


class Item_func_udf_decimal :public Item_udf_func
{
public:
1025 1026
  Item_func_udf_decimal(udf_func *udf_arg)
    :Item_udf_func(udf_arg) {}
1027
  Item_func_udf_decimal(udf_func *udf_arg, List<Item> &list)
1028
    :Item_udf_func(udf_arg, list) {}
1029 1030 1031 1032 1033 1034
  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
1035 1036 1037 1038 1039 1040
};


class Item_func_udf_str :public Item_udf_func
{
public:
1041 1042
  Item_func_udf_str(udf_func *udf_arg)
    :Item_udf_func(udf_arg) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1043
  Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
1044
    :Item_udf_func(udf_arg, list) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1045
  String *val_str(String *);
1046
  double val_real()
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1047
  {
monty@mysql.com's avatar
monty@mysql.com committed
1048 1049 1050 1051 1052 1053
    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
1054 1055 1056
  }
  longlong val_int()
  {
monty@mysql.com's avatar
monty@mysql.com committed
1057
    int err_not_used;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1058
    String *res;  res=val_str(&str_value);
monty@mysql.com's avatar
monty@mysql.com committed
1059 1060
    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
1061
  }
1062 1063 1064 1065 1066 1067 1068 1069
  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
1070 1071 1072 1073 1074 1075 1076 1077 1078
  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:
1079 1080 1081 1082
  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) {}
1083
  double val_real() { DBUG_ASSERT(fixed == 1); return 0.0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1084 1085 1086 1087 1088 1089
};


class Item_func_udf_int :public Item_int_func
{
public:
1090 1091 1092 1093
  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) {}
1094
  longlong val_int() { DBUG_ASSERT(fixed == 1); return 0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1095 1096 1097
};


1098 1099 1100
class Item_func_udf_decimal :public Item_int_func
{
public:
1101 1102 1103 1104
  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) {}
1105 1106 1107 1108
  my_decimal *val_decimal(my_decimal *) { DBUG_ASSERT(fixed == 1); return 0; }
};


bk@work.mysql.com's avatar
bk@work.mysql.com committed
1109 1110 1111
class Item_func_udf_str :public Item_func
{
public:
1112 1113 1114 1115
  Item_func_udf_str(udf_func *udf_arg)
    :Item_func() {}
  Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
    :Item_func(list) {}
1116 1117
  String *val_str(String *)
    { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
1118
  double val_real() { DBUG_ASSERT(fixed == 1); null_value= 1; return 0.0; }
1119
  longlong val_int() { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
  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
*/

1130
class User_level_lock;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1131
void item_user_lock_init(void);
1132
void item_user_lock_release(User_level_lock *ull);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1133 1134 1135 1136 1137 1138 1139 1140 1141
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
1142
  void fix_length_and_dec() { max_length=1; maybe_null=1;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1143 1144 1145 1146 1147
};

class Item_func_release_lock :public Item_int_func
{
  String value;
1148
public:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1149 1150 1151
  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
1152
  void fix_length_and_dec() { max_length=1; maybe_null=1;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1153 1154
};

1155 1156 1157 1158 1159
/* replication functions */

class Item_master_pos_wait :public Item_int_func
{
  String value;
1160
public:
1161
  Item_master_pos_wait(Item *a,Item *b) :Item_int_func(a,b) {}
1162
  Item_master_pos_wait(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
1163 1164
  longlong val_int();
  const char *func_name() const { return "master_pos_wait"; }
1165
  void fix_length_and_dec() { max_length=21; maybe_null=1;}
1166 1167
};

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

1169
/* Handling of user definable variables */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1170 1171 1172 1173 1174 1175 1176

class user_var_entry;

class Item_func_set_user_var :public Item_func
{
  enum Item_result cached_result_type;
  user_var_entry *entry;
1177 1178
  char buffer[MAX_FIELD_WIDTH];
  String value;
1179
  my_decimal decimal_buff;
1180
  bool null_item;
1181 1182 1183 1184 1185
  union
  {
    longlong vint;
    double vreal;
    String *vstr;
1186
    my_decimal *vdec;
1187 1188 1189
  } save_result;
  String save_buff;
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1190 1191

public:
1192
  LEX_STRING name; // keep it public
1193 1194 1195
  Item_func_set_user_var(LEX_STRING a,Item *b)
    :Item_func(b), cached_result_type(INT_RESULT), name(a)
  {}
1196
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1197 1198
  longlong val_int();
  String *val_str(String *str);
1199
  my_decimal *val_decimal(my_decimal *);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1200
  bool update_hash(void *ptr, uint length, enum Item_result type, 
1201
  		   CHARSET_INFO *cs, Derivation dv);
1202
  bool check();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1203 1204
  bool update();
  enum Item_result result_type () const { return cached_result_type; }
1205
  bool fix_fields(THD *thd, Item **ref);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1206
  void fix_length_and_dec();
1207
  void print(String *str);
1208
  void print_as_stmt(String *str);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1209 1210 1211 1212
  const char *func_name() const { return "set_user_var"; }
};


1213 1214
class Item_func_get_user_var :public Item_func,
                              private Settable_routine_parameter
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1215
{
1216
  user_var_entry *var_entry;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1217 1218

public:
1219
  LEX_STRING name; // keep it public
1220
  Item_func_get_user_var(LEX_STRING a):
1221
    Item_func(), name(a) {}
1222 1223
  enum Functype functype() const { return GUSERVAR_FUNC; }
  LEX_STRING get_name() { return name; }
1224
  double val_real();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1225
  longlong val_int();
1226
  my_decimal *val_decimal(my_decimal*);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1227 1228
  String *val_str(String* str);
  void fix_length_and_dec();
1229
  void print(String *str);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1230
  enum Item_result result_type() const;
1231 1232 1233 1234
  /*
    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)
  */
1235
  enum_field_types field_type() const  { return MYSQL_TYPE_VARCHAR; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1236
  const char *func_name() const { return "get_user_var"; }
1237
  bool const_item() const;
1238
  table_map used_tables() const
1239
  { return const_item() ? 0 : RAND_TABLE_BIT; }
1240
  bool eq(const Item *item, bool binary_cmp) const;
1241 1242

private:
1243
  bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
1244 1245 1246 1247 1248 1249

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

1252

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
/*
  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 */
1275
  bool fix_fields(THD *thd, Item **ref);
1276 1277 1278 1279 1280 1281
  void print(String *str);
  void set_null_value(CHARSET_INFO* cs);
  void set_value(const char *str, uint length, CHARSET_INFO* cs);
};


1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
/* 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);
1293
  bool fix_fields(THD *thd, Item **ref);
1294 1295 1296 1297
  /*
    Stubs for pure virtual methods. Should never be called: this
    item is always substituted with a constant in fix_fields().
  */
1298
  double val_real()         { DBUG_ASSERT(0); return 0.0; }
1299 1300 1301
  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); }
1302 1303
  /* TODO: fix to support views */
  const char *func_name() const { return "get_system_var"; }
1304 1305 1306
};


bk@work.mysql.com's avatar
bk@work.mysql.com committed
1307 1308 1309 1310 1311 1312 1313
class Item_func_inet_aton : public Item_int_func
{
public:
   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;}
1314
  bool check_partition_func_processor(byte *int_arg) { return FALSE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1315 1316 1317
};


1318
/* for fulltext search */
1319
#include <ft_global.h>
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1320 1321 1322 1323

class Item_func_match :public Item_real_func
{
public:
1324
  uint key, flags;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1325
  bool join_key;
1326 1327 1328 1329 1330 1331 1332
  DTCollation cmp_collation;
  FT_INFO *ft_handler;
  TABLE *table;
  Item_func_match *master;   // for master-slave optimization
  Item *concat;              // Item_func_concat_ws
  String value;              // value of concat
  String search_value;       // key_item()'s value converted to cmp_collation
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1333

serg@serg.mylan's avatar
serg@serg.mylan committed
1334 1335
  Item_func_match(List<Item> &a, uint b): Item_real_func(a), key(0), flags(b),
       join_key(0), ft_handler(0), table(0), master(0), concat(0) { }
hf@deer.(none)'s avatar
hf@deer.(none) committed
1336
  void cleanup()
1337
  {
1338
    DBUG_ENTER("Item_func_match");
1339
    Item_real_func::cleanup();
1340
    if (!master && ft_handler)
1341
      ft_handler->please->close_search(ft_handler);
1342
    ft_handler= 0;
1343
    concat= 0;
1344
    DBUG_VOID_RETURN;
1345
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1346
  enum Functype functype() const { return FT_FUNC; }
1347
  const char *func_name() const { return "match"; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1348
  void update_used_tables() {}
1349
  table_map not_null_tables() const { return 0; }
1350
  bool fix_fields(THD *thd, Item **ref);
1351
  bool eq(const Item *, bool binary_cmp) const;
1352
  /* The following should be safe, even if we compare doubles */
1353 1354
  longlong val_int() { DBUG_ASSERT(fixed == 1); return val_real() != 0.0; }
  double val_real();
1355
  void print(String *str);
1356 1357

  bool fix_index();
1358
  void init_search(bool no_order);
1359
};
1360

1361

1362
class Item_func_bit_xor : public Item_func_bit
1363 1364
{
public:
1365
  Item_func_bit_xor(Item *a, Item *b) :Item_func_bit(a, b) {}
1366 1367 1368 1369
  longlong val_int();
  const char *func_name() const { return "^"; }
};

1370
class Item_func_is_free_lock :public Item_int_func
1371 1372 1373
{
  String value;
public:
1374
  Item_func_is_free_lock(Item *a) :Item_int_func(a) {}
1375
  longlong val_int();
1376
  const char *func_name() const { return "is_free_lock"; }
1377
  void fix_length_and_dec() { decimals=0; max_length=1; maybe_null=1;}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1378 1379
};

hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
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
1390 1391
/* For type casts */

1392
enum Cast_target
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1393 1394
{
  ITEM_CAST_BINARY, ITEM_CAST_SIGNED_INT, ITEM_CAST_UNSIGNED_INT,
1395 1396
  ITEM_CAST_DATE, ITEM_CAST_TIME, ITEM_CAST_DATETIME, ITEM_CAST_CHAR,
  ITEM_CAST_DECIMAL
1397
};
1398 1399


1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
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; }
};


1410 1411 1412 1413 1414 1415 1416
/*
 *
 * Stored FUNCTIONs
 *
 */

class sp_head;
1417
class sp_name;
1418
struct st_sp_security_context;
1419 1420 1421 1422

class Item_func_sp :public Item_func
{
private:
1423
  Name_resolution_context *context;
1424
  sp_name *m_name;
1425
  mutable sp_head *m_sp;
1426
  TABLE *dummy_table;
1427 1428
  Field *result_field;
  char result_buf[64];
1429

1430 1431
  bool execute(Field **flp);
  bool execute_impl(THD *thd, Field *return_value_fld);
1432
  Field *sp_result_field(void) const;
1433 1434 1435

public:

1436
  Item_func_sp(Name_resolution_context *context_arg, sp_name *name);
1437

1438 1439
  Item_func_sp(Name_resolution_context *context_arg,
               sp_name *name, List<Item> &list);
1440 1441 1442 1443

  virtual ~Item_func_sp()
  {}

1444
  void cleanup();
1445

1446
  const char *func_name() const;
1447

1448 1449
  enum enum_field_types field_type() const;

1450 1451 1452 1453
  Field *tmp_table_field(TABLE *t_arg);

  void make_field(Send_field *tmp_field);

1454 1455 1456 1457
  Item_result result_type() const;

  longlong val_int()
  {
1458
    if (execute(&result_field))
1459
      return (longlong) 0;
1460
    return result_field->val_int();
1461 1462
  }

1463
  double val_real()
1464
  {
1465
    if (execute(&result_field))
1466
      return 0.0;
1467
    return result_field->val_real();
1468 1469
  }

1470 1471
  my_decimal *val_decimal(my_decimal *dec_buf)
  {
1472
    if (execute(&result_field))
1473
      return NULL;
1474
    return result_field->val_decimal(dec_buf);
1475 1476
  }

1477 1478
  String *val_str(String *str)
  {
1479 1480 1481 1482
    String buf;
    char buff[20];
    buf.set(buff, 20, str->charset());
    buf.length(0);
1483
    if (execute(&result_field))
1484
      return NULL;
1485 1486 1487 1488 1489 1490 1491 1492 1493
    /*
      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.
    */
    result_field->val_str(&buf);
    str->copy(buf);
    return str;
1494 1495
  }

1496 1497 1498
  virtual bool change_context_processor(byte *cntx)
    { context= (Name_resolution_context *)cntx; return FALSE; }

1499
  void fix_length_and_dec();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1500 1501
  bool find_and_check_access(THD * thd, ulong want_access,
                             Security_context **backup);
1502
  virtual enum Functype functype() const { return FUNC_SP; }
1503

1504
  bool fix_fields(THD *thd, Item **ref);
1505
};
monty@mysql.com's avatar
monty@mysql.com committed
1506 1507


1508 1509 1510 1511 1512 1513 1514 1515
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; }
};