item_func.h 30.3 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
2

unknown's avatar
unknown 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

unknown's avatar
unknown 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

unknown's avatar
unknown committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
   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 */

#ifdef __GNUC__
#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];
unknown's avatar
unknown committed
35
  uint allowed_arg_cols;
unknown's avatar
unknown committed
36 37
public:
  uint arg_count;
38
  table_map used_tables_cache, not_null_tables_cache;
unknown's avatar
unknown committed
39 40 41 42
  bool const_item_cache;
  enum Functype { UNKNOWN_FUNC,EQ_FUNC,EQUAL_FUNC,NE_FUNC,LT_FUNC,LE_FUNC,
		  GE_FUNC,GT_FUNC,FT_FUNC,
		  LIKE_FUNC,NOTLIKE_FUNC,ISNULL_FUNC,ISNOTNULL_FUNC,
43 44
		  COND_AND_FUNC, COND_OR_FUNC, COND_XOR_FUNC,
                  BETWEEN, IN_FUNC, MULT_EQUAL_FUNC,
45
		  INTERVAL_FUNC, ISNOTNULLTEST_FUNC,
unknown's avatar
unknown committed
46 47 48 49
		  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,
50
		  SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN,
unknown's avatar
unknown committed
51 52
                  NOT_FUNC, NOT_ALL_FUNC,
                  NOW_FUNC, TRIG_COND_FUNC,
53
                  GUSERVAR_FUNC};
54 55
  enum optimize_type { OPTIMIZE_NONE,OPTIMIZE_KEY,OPTIMIZE_OP, OPTIMIZE_NULL,
                       OPTIMIZE_EQUAL };
unknown's avatar
unknown committed
56 57
  enum Type type() const { return FUNC_ITEM; }
  virtual enum Functype functype() const   { return UNKNOWN_FUNC; }
unknown's avatar
unknown committed
58 59
  Item_func(void):
    allowed_arg_cols(1), arg_count(0)
unknown's avatar
unknown committed
60
  {
61
    with_sum_func= 0;
unknown's avatar
unknown committed
62
  }
unknown's avatar
unknown committed
63 64
  Item_func(Item *a):
    allowed_arg_cols(1), arg_count(1)
unknown's avatar
unknown committed
65
  {
66
    args= tmp_arg;
67 68
    args[0]= a;
    with_sum_func= a->with_sum_func;
unknown's avatar
unknown committed
69
  }
unknown's avatar
unknown committed
70 71
  Item_func(Item *a,Item *b):
    allowed_arg_cols(1), arg_count(2)
unknown's avatar
unknown committed
72
  {
73
    args= tmp_arg;
74 75
    args[0]= a; args[1]= b;
    with_sum_func= a->with_sum_func || b->with_sum_func;
unknown's avatar
unknown committed
76
  }
unknown's avatar
unknown committed
77 78
  Item_func(Item *a,Item *b,Item *c):
    allowed_arg_cols(1)
unknown's avatar
unknown committed
79
  {
80 81
    arg_count= 0;
    if ((args= (Item**) sql_alloc(sizeof(Item*)*3)))
unknown's avatar
unknown committed
82
    {
83 84 85
      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;
unknown's avatar
unknown committed
86 87
    }
  }
unknown's avatar
unknown committed
88 89
  Item_func(Item *a,Item *b,Item *c,Item *d):
    allowed_arg_cols(1)
unknown's avatar
unknown committed
90
  {
91 92
    arg_count= 0;
    if ((args= (Item**) sql_alloc(sizeof(Item*)*4)))
unknown's avatar
unknown committed
93
    {
94 95 96 97
      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;
unknown's avatar
unknown committed
98 99
    }
  }
unknown's avatar
unknown committed
100 101
  Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
    allowed_arg_cols(1)
unknown's avatar
unknown committed
102
  {
103 104
    arg_count= 5;
    if ((args= (Item**) sql_alloc(sizeof(Item*)*5)))
unknown's avatar
unknown committed
105
    {
106 107 108
      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 ;
unknown's avatar
unknown committed
109 110 111
    }
  }
  Item_func(List<Item> &list);
112
  // Constructor used for Item_cond_and/or (see Item comment)
113
  Item_func(THD *thd, Item_func *item);
unknown's avatar
unknown committed
114
  bool fix_fields(THD *,struct st_table_list *, Item **ref);
unknown's avatar
unknown committed
115
  table_map used_tables() const;
116
  table_map not_null_tables() const;
unknown's avatar
unknown committed
117
  void update_used_tables();
118
  bool eq(const Item *item, bool binary_cmp) const;
unknown's avatar
unknown committed
119 120 121 122 123 124
  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]; }
  virtual const char *func_name() const { return "?"; }
  virtual bool const_item() const { return const_item_cache; }
  inline Item **arguments() const { return args; }
125
  void set_arguments(List<Item> &list);
unknown's avatar
unknown committed
126 127
  inline uint argument_count() const { return arg_count; }
  inline void remove_arguments() { arg_count=0; }
128
  virtual void split_sum_func(Item **ref_pointer_array, List<Item> &fields);
unknown's avatar
unknown committed
129 130
  void print(String *str);
  void print_op(String *str);
unknown's avatar
unknown committed
131
  void print_args(String *str, uint from);
unknown's avatar
unknown committed
132
  void fix_num_length_and_dec();
133
  inline bool get_arg0_date(TIME *ltime, uint fuzzy_date)
unknown's avatar
unknown committed
134
  {
135
    return (null_value=args[0]->get_date(ltime, fuzzy_date));
unknown's avatar
unknown committed
136 137 138 139 140
  }
  inline bool get_arg0_time(TIME *ltime)
  {
    return (null_value=args[0]->get_time(ltime));
  }
unknown's avatar
unknown committed
141
  bool is_null() { (void) val_int(); return null_value; }
unknown's avatar
unknown committed
142
  void signal_divide_by_null();
unknown's avatar
unknown committed
143
  friend class udf_handler;
144
  Field *tmp_table_field() { return result_field; }
145
  Field *tmp_table_field(TABLE *t_arg);
146
  Item *get_tmp_table_item(THD *thd);
unknown's avatar
unknown committed
147
  
148 149 150 151 152
  bool agg_arg_collations(DTCollation &c, Item **items, uint nitems,
                          bool allow_superset_conversion= FALSE);
  bool agg_arg_collations_for_comparison(DTCollation &c,
                                         Item **items, uint nitems,
                                         bool allow_superset_comversion= FALSE);
unknown's avatar
unknown committed
153 154

  bool walk(Item_processor processor, byte *arg);
unknown's avatar
unknown committed
155
  Item *transform(Item_transformer transformer, byte *arg);
unknown's avatar
unknown committed
156 157 158 159 160 161 162 163 164 165 166
};


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);
167
  longlong val_int() { DBUG_ASSERT(fixed == 1); return (longlong) val(); }
unknown's avatar
unknown committed
168 169 170 171
  enum Item_result result_type () const { return REAL_RESULT; }
  void fix_length_and_dec() { decimals=NOT_FIXED_DEC; max_length=float_length(decimals); }
};

172

unknown's avatar
unknown committed
173 174 175 176 177 178 179 180
class Item_num_func :public Item_func
{
 protected:
  Item_result hybrid_type;
public:
  Item_num_func(Item *a) :Item_func(a),hybrid_type(REAL_RESULT) {}
  Item_num_func(Item *a,Item *b) :Item_func(a,b),hybrid_type(REAL_RESULT) {}
  String *val_str(String*str);
181
  longlong val_int() { DBUG_ASSERT(fixed == 1); return (longlong) val(); }
unknown's avatar
unknown committed
182 183
  enum Item_result result_type () const { return hybrid_type; }
  void fix_length_and_dec() { fix_num_length_and_dec(); }
unknown's avatar
unknown committed
184
  bool is_null() { (void) val(); return null_value; }
unknown's avatar
unknown committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198
};


class Item_num_op :public Item_func
{
 protected:
  Item_result hybrid_type;
 public:
  Item_num_op(Item *a,Item *b) :Item_func(a,b),hybrid_type(REAL_RESULT) {}
  String *val_str(String*str);
  void print(String *str) { print_op(str); }
  enum Item_result result_type () const { return hybrid_type; }
  void fix_length_and_dec() { fix_num_length_and_dec(); find_num_type(); }
  void find_num_type(void);
unknown's avatar
unknown committed
199
  bool is_null() { (void) val(); return null_value; }
unknown's avatar
unknown committed
200 201 202 203 204 205
};


class Item_int_func :public Item_func
{
public:
unknown's avatar
unknown committed
206 207 208 209 210
  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; }
211
  Item_int_func(THD *thd, Item_int_func *item) :Item_func(thd, item) {}
212
  double val() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
unknown's avatar
unknown committed
213 214
  String *val_str(String*str);
  enum Item_result result_type () const { return INT_RESULT; }
unknown's avatar
unknown committed
215
  void fix_length_and_dec() {}
unknown's avatar
unknown committed
216 217
};

218

219 220 221 222
class Item_func_signed :public Item_int_func
{
public:
  Item_func_signed(Item *a) :Item_int_func(a) {}
unknown's avatar
unknown committed
223 224 225 226 227 228 229 230 231 232 233 234
  double val()
  {
    double tmp= args[0]->val();
    null_value= args[0]->null_value;
    return tmp;
  }
  longlong val_int()
  {
    longlong tmp= args[0]->val_int();
    null_value= args[0]->null_value; 
    return tmp;
  }
235
  void fix_length_and_dec()
unknown's avatar
unknown committed
236
  { max_length=args[0]->max_length; unsigned_flag=0; }
237
  void print(String *str);
238 239
};

240

unknown's avatar
unknown committed
241
class Item_func_unsigned :public Item_func_signed
242 243
{
public:
unknown's avatar
unknown committed
244
  Item_func_unsigned(Item *a) :Item_func_signed(a) {}
245
  void fix_length_and_dec()
unknown's avatar
unknown committed
246
  { max_length=args[0]->max_length; unsigned_flag=1; }
247
  void print(String *str);
248 249 250
};


unknown's avatar
unknown committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
class Item_func_plus :public Item_num_op
{
public:
  Item_func_plus(Item *a,Item *b) :Item_num_op(a,b) {}
  const char *func_name() const { return "+"; }
  double val();
  longlong val_int();
};

class Item_func_minus :public Item_num_op
{
public:
  Item_func_minus(Item *a,Item *b) :Item_num_op(a,b) {}
  const char *func_name() const { return "-"; }
  double val();
  longlong val_int();
267
  void fix_length_and_dec();
unknown's avatar
unknown committed
268 269
};

270

unknown's avatar
unknown committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
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 "*"; }
  double val();
  longlong val_int();
};


class Item_func_div :public Item_num_op
{
public:
  Item_func_div(Item *a,Item *b) :Item_num_op(a,b) {}
  double val();
  longlong val_int();
  const char *func_name() const { return "/"; }
  void fix_length_and_dec();
};


292 293 294 295 296
class Item_func_int_div :public Item_num_op
{
public:
  Item_func_int_div(Item *a,Item *b) :Item_num_op(a,b)
  { hybrid_type=INT_RESULT; }
297
  double val() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
298 299 300 301 302 303
  longlong val_int();
  const char *func_name() const { return "DIV"; }
  void fix_length_and_dec();
};


unknown's avatar
unknown committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
class Item_func_mod :public Item_num_op
{
public:
  Item_func_mod(Item *a,Item *b) :Item_num_op(a,b) {}
  double val();
  longlong val_int();
  const char *func_name() const { return "%"; }
  void fix_length_and_dec();
};


class Item_func_neg :public Item_num_func
{
public:
  Item_func_neg(Item *a) :Item_num_func(a) {}
  double val();
  longlong val_int();
  const char *func_name() const { return "-"; }
  void fix_length_and_dec();
};


class Item_func_abs :public Item_num_func
{
public:
  Item_func_abs(Item *a) :Item_num_func(a) {}
  const char *func_name() const { return "abs"; }
  double val();
  longlong val_int();
  enum Item_result result_type () const
  { return args[0]->result_type() == INT_RESULT ? INT_RESULT : REAL_RESULT; }
  void fix_length_and_dec();
};

// A class to handle logaritmic and trigometric functions

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()
  {
347
    decimals=NOT_FIXED_DEC; max_length=float_length(decimals);
unknown's avatar
unknown committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    maybe_null=1;
  }
  inline double fix_result(double value)
  {
#ifndef HAVE_FINITE
    return value;
#else
    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) {}
  double val();
  const char *func_name() const { return "exp"; }
};

371 372 373 374 375 376 377 378 379 380

class Item_func_ln :public Item_dec_func
{
public:
  Item_func_ln(Item *a) :Item_dec_func(a) {}
  double val();
  const char *func_name() const { return "ln"; }
};


unknown's avatar
unknown committed
381 382 383 384
class Item_func_log :public Item_dec_func
{
public:
  Item_func_log(Item *a) :Item_dec_func(a) {}
385
  Item_func_log(Item *a,Item *b) :Item_dec_func(a,b) {}
unknown's avatar
unknown committed
386 387 388 389 390
  double val();
  const char *func_name() const { return "log"; }
};


391 392 393 394 395 396 397 398 399
class Item_func_log2 :public Item_dec_func
{
public:
  Item_func_log2(Item *a) :Item_dec_func(a) {}
  double val();
  const char *func_name() const { return "log2"; }
};


unknown's avatar
unknown committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
class Item_func_log10 :public Item_dec_func
{
public:
  Item_func_log10(Item *a) :Item_dec_func(a) {}
  double val();
  const char *func_name() const { return "log10"; }
};


class Item_func_sqrt :public Item_dec_func
{
public:
  Item_func_sqrt(Item *a) :Item_dec_func(a) {}
  double val();
  const char *func_name() const { return "sqrt"; }
};


class Item_func_pow :public Item_dec_func
{
public:
  Item_func_pow(Item *a,Item *b) :Item_dec_func(a,b) {}
  double val();
  const char *func_name() const { return "pow"; }
};


class Item_func_acos :public Item_dec_func
{
 public:
  Item_func_acos(Item *a) :Item_dec_func(a) {}
  double val();
  const char *func_name() const { return "acos"; }
};

class Item_func_asin :public Item_dec_func
{
 public:
  Item_func_asin(Item *a) :Item_dec_func(a) {}
  double val();
  const char *func_name() const { return "asin"; }
};

class Item_func_atan :public Item_dec_func
{
 public:
  Item_func_atan(Item *a) :Item_dec_func(a) {}
  Item_func_atan(Item *a,Item *b) :Item_dec_func(a,b) {}
  double val();
  const char *func_name() const { return "atan"; }
};

class Item_func_cos :public Item_dec_func
{
 public:
  Item_func_cos(Item *a) :Item_dec_func(a) {}
  double val();
  const char *func_name() const { return "cos"; }
};

class Item_func_sin :public Item_dec_func
{
 public:
  Item_func_sin(Item *a) :Item_dec_func(a) {}
  double val();
  const char *func_name() const { return "sin"; }
};

class Item_func_tan :public Item_dec_func
{
 public:
  Item_func_tan(Item *a) :Item_dec_func(a) {}
  double val();
  const char *func_name() const { return "tan"; }
};

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


class Item_func_ceiling :public Item_func_integer
{
  Item_func_ceiling();				/* Never called */
public:
  Item_func_ceiling(Item *a) :Item_func_integer(a) {}
  const char *func_name() const { return "ceiling"; }
  longlong val_int();
};

class Item_func_floor :public Item_func_integer
{
public:
  Item_func_floor(Item *a) :Item_func_integer(a) {}
  const char *func_name() const { return "floor"; }
  longlong val_int();
};

/* This handles round and truncate */

class Item_func_round :public Item_real_func
{
  bool truncate;
public:
  Item_func_round(Item *a,Item *b,bool trunc_arg)
    :Item_real_func(a,b),truncate(trunc_arg) {}
  const char *func_name() const { return truncate ? "truncate" : "round"; }
  double val();
  void fix_length_and_dec();
};


class Item_func_rand :public Item_real_func
{
517
  struct rand_struct *rand;
unknown's avatar
unknown committed
518 519 520 521 522 523
public:
  Item_func_rand(Item *a) :Item_real_func(a) {}
  Item_func_rand()	  :Item_real_func()  {}
  double val();
  const char *func_name() const { return "rand"; }
  bool const_item() const { return 0; }
unknown's avatar
unknown committed
524
  void update_used_tables();
525
  void fix_length_and_dec();
unknown's avatar
unknown committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
};


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();
};


class Item_func_units :public Item_real_func
{
  char *name;
  double mul,add;
 public:
  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) {}
  double val();
  const char *func_name() const { return name; }
  void fix_length_and_dec() { decimals=NOT_FIXED_DEC; max_length=float_length(decimals); }
};


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),
558
    cmp_type(INT_RESULT), cmp_sign(cmp_sign_arg) {}
unknown's avatar
unknown committed
559 560 561 562 563
  double val();
  longlong val_int();
  String *val_str(String *);
  void fix_length_and_dec();
  enum Item_result result_type () const { return cmp_type; }
unknown's avatar
unknown committed
564
  table_map not_null_tables() const { return 0; }
unknown's avatar
unknown committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
};

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"; }
};

unknown's avatar
unknown committed
581

unknown's avatar
unknown committed
582 583 584 585 586 587 588 589 590 591
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; }
};

unknown's avatar
unknown committed
592 593 594 595
class Item_func_bit_length :public Item_func_length
{
public:
  Item_func_bit_length(Item *a) :Item_func_length(a) {}
596 597
  longlong val_int()
    { DBUG_ASSERT(fixed == 1); return Item_func_length::val_int()*8; }
unknown's avatar
unknown committed
598 599 600
  const char *func_name() const { return "bit_length"; }
};

unknown's avatar
unknown committed
601 602 603 604 605 606 607 608 609 610
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; }
};

unknown's avatar
unknown committed
611 612 613 614 615 616 617 618 619
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"; }
  void fix_length_and_dec() { max_length=10; }
};

unknown's avatar
unknown committed
620 621 622
class Item_func_locate :public Item_int_func
{
  String value1,value2;
623
  DTCollation cmp_collation;
unknown's avatar
unknown committed
624 625 626 627 628
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();
629
  void fix_length_and_dec();
630
  void print(String *str);
unknown's avatar
unknown committed
631 632 633 634 635 636
};


class Item_func_field :public Item_int_func
{
  String value,tmp;
637 638
  Item_result cmp_type;
  DTCollation cmp_collation;
unknown's avatar
unknown committed
639
public:
640
  Item_func_field(List<Item> &list) :Item_int_func(list) {}
unknown's avatar
unknown committed
641 642
  longlong val_int();
  const char *func_name() const { return "field"; }
643
  void fix_length_and_dec();
unknown's avatar
unknown committed
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
};


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

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"; }
};

class Item_func_find_in_set :public Item_int_func
{
  String value,value2;
  uint enum_value;
  ulonglong enum_bit;
671
  DTCollation cmp_collation;
unknown's avatar
unknown committed
672 673 674 675 676 677 678
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();
};

679
/* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
unknown's avatar
unknown committed
680

681
class Item_func_bit: public Item_int_func
unknown's avatar
unknown committed
682 683
{
public:
684 685 686
  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; }
unknown's avatar
unknown committed
687
  void print(String *str) { print_op(str); }
688 689 690 691 692 693
};

class Item_func_bit_or :public Item_func_bit
{
public:
  Item_func_bit_or(Item *a, Item *b) :Item_func_bit(a, b) {}
unknown's avatar
unknown committed
694 695 696 697
  longlong val_int();
  const char *func_name() const { return "|"; }
};

698
class Item_func_bit_and :public Item_func_bit
unknown's avatar
unknown committed
699 700
{
public:
701
  Item_func_bit_and(Item *a, Item *b) :Item_func_bit(a, b) {}
unknown's avatar
unknown committed
702 703 704 705 706 707 708 709 710 711
  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"; }
unknown's avatar
unknown committed
712
  void fix_length_and_dec() { max_length=2; }
unknown's avatar
unknown committed
713 714
};

715
class Item_func_shift_left :public Item_func_bit
unknown's avatar
unknown committed
716 717
{
public:
718
  Item_func_shift_left(Item *a, Item *b) :Item_func_bit(a, b) {}
unknown's avatar
unknown committed
719 720 721 722
  longlong val_int();
  const char *func_name() const { return "<<"; }
};

723
class Item_func_shift_right :public Item_func_bit
unknown's avatar
unknown committed
724 725
{
public:
726
  Item_func_shift_right(Item *a, Item *b) :Item_func_bit(a, b) {}
unknown's avatar
unknown committed
727 728 729 730
  longlong val_int();
  const char *func_name() const { return ">>"; }
};

731
class Item_func_bit_neg :public Item_func_bit
unknown's avatar
unknown committed
732 733
{
public:
734
  Item_func_bit_neg(Item *a) :Item_func_bit(a) {}
unknown's avatar
unknown committed
735 736
  longlong val_int();
  const char *func_name() const { return "~"; }
unknown's avatar
unknown committed
737
  void print(String *str) { Item_func::print(str); }
unknown's avatar
unknown committed
738 739
};

unknown's avatar
unknown committed
740

741
class Item_func_last_insert_id :public Item_int_func
unknown's avatar
unknown committed
742 743
{
public:
744 745
  Item_func_last_insert_id() :Item_int_func() {}
  Item_func_last_insert_id(Item *a) :Item_int_func(a) {}
unknown's avatar
unknown committed
746 747
  longlong val_int();
  const char *func_name() const { return "last_insert_id"; }
748
  void fix_length_and_dec() { if (arg_count) max_length= args[0]->max_length; }
unknown's avatar
unknown committed
749 750 751 752 753 754 755 756 757 758 759
};

class Item_func_benchmark :public Item_int_func
{
  ulong loop_count;
 public:
  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"; }
unknown's avatar
unknown committed
760
  void fix_length_and_dec() { max_length=1; maybe_null=0; }
761
  void print(String *str);
unknown's avatar
unknown committed
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
};


#ifdef HAVE_DLOPEN

class Item_udf_func :public Item_func
{
 protected:
  udf_handler udf;

public:
  Item_udf_func(udf_func *udf_arg) :Item_func(), udf(udf_arg) {}
  Item_udf_func(udf_func *udf_arg, List<Item> &list)
    :Item_func(list), udf(udf_arg) {}
  const char *func_name() const { return udf.name(); }
unknown's avatar
unknown committed
777
  bool fix_fields(THD *thd, struct st_table_list *tables, Item **ref)
unknown's avatar
unknown committed
778
  {
779
    DBUG_ASSERT(fixed == 0);
unknown's avatar
unknown committed
780 781 782
    bool res= udf.fix_fields(thd, tables, this, arg_count, args);
    used_tables_cache= udf.used_tables_cache;
    const_item_cache= udf.const_item_cache;
783
    fixed= 1;
unknown's avatar
unknown committed
784 785 786
    return res;
  }
  Item_result result_type () const { return udf.result_type(); }
787
  table_map not_null_tables() const { return 0; }
unknown's avatar
unknown committed
788 789 790 791 792 793 794 795 796
};


class Item_func_udf_float :public Item_udf_func
{
 public:
  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) {}
797 798
  longlong val_int()
    { DBUG_ASSERT(fixed == 1); return (longlong) Item_func_udf_float::val(); }
unknown's avatar
unknown committed
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
  double val();
  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:
  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) {}
  longlong val_int();
  double val() { return (double) Item_func_udf_int::val_int(); }
  String *val_str(String *str);
  enum Item_result result_type () const { return INT_RESULT; }
  void fix_length_and_dec() { decimals=0; max_length=21; }
};


class Item_func_udf_str :public Item_udf_func
{
public:
  Item_func_udf_str(udf_func *udf_arg) :Item_udf_func(udf_arg) {}
  Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
    :Item_udf_func(udf_arg,list) {}
  String *val_str(String *);
  double val()
  {
828
    int err;
unknown's avatar
unknown committed
829
    String *res;  res=val_str(&str_value);
830
    return res ? my_strntod(res->charset(),(char*) res->ptr(),res->length(),0,&err) : 0.0;
unknown's avatar
unknown committed
831 832 833
  }
  longlong val_int()
  {
834
    int err;
unknown's avatar
unknown committed
835
    String *res;  res=val_str(&str_value);
836
    return res ? my_strntoll(res->charset(),res->ptr(),res->length(),10,(char**) 0,&err) : (longlong) 0;
unknown's avatar
unknown committed
837 838 839 840 841 842 843 844 845 846 847 848
  }
  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:
  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) {}
849
  double val() { DBUG_ASSERT(fixed == 1); return 0.0; }
unknown's avatar
unknown committed
850 851 852 853 854 855 856 857
};


class Item_func_udf_int :public Item_int_func
{
public:
  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) {}
858
  longlong val_int() { DBUG_ASSERT(fixed == 1); return 0; }
unknown's avatar
unknown committed
859 860 861 862 863 864 865 866
};


class Item_func_udf_str :public Item_func
{
public:
  Item_func_udf_str(udf_func *udf_arg) :Item_func() {}
  Item_func_udf_str(udf_func *udf_arg, List<Item> &list)  :Item_func(list) {}
867 868 869 870
  String *val_str(String *)
    { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
  double val() { DBUG_ASSERT(fixed == 1); null_value=1; return 0.0; }
  longlong val_int() { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
unknown's avatar
unknown committed
871 872 873 874 875 876 877 878 879 880
  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
*/

881
class User_level_lock;
unknown's avatar
unknown committed
882
void item_user_lock_init(void);
883
void item_user_lock_release(User_level_lock *ull);
unknown's avatar
unknown committed
884 885 886 887 888 889 890 891 892
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"; }
unknown's avatar
unknown committed
893
  void fix_length_and_dec() { max_length=1; maybe_null=1;}
unknown's avatar
unknown committed
894 895 896 897 898 899 900 901 902
};

class Item_func_release_lock :public Item_int_func
{
  String value;
 public:
  Item_func_release_lock(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "release_lock"; }
unknown's avatar
unknown committed
903
  void fix_length_and_dec() { max_length=1; maybe_null=1;}
unknown's avatar
unknown committed
904 905
};

unknown's avatar
unknown committed
906 907 908 909 910 911 912
/* replication functions */

class Item_master_pos_wait :public Item_int_func
{
  String value;
 public:
  Item_master_pos_wait(Item *a,Item *b) :Item_int_func(a,b) {}
913
  Item_master_pos_wait(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
unknown's avatar
unknown committed
914 915
  longlong val_int();
  const char *func_name() const { return "master_pos_wait"; }
916
  void fix_length_and_dec() { max_length=21; maybe_null=1;}
unknown's avatar
unknown committed
917 918
};

unknown's avatar
unknown committed
919 920 921 922 923 924 925 926 927 928

/* Handling of user definiable variables */

class user_var_entry;

class Item_func_set_user_var :public Item_func
{
  enum Item_result cached_result_type;
  LEX_STRING name;
  user_var_entry *entry;
929 930
  char buffer[MAX_FIELD_WIDTH];
  String value;
931 932 933 934 935 936 937 938
  union
  {
    longlong vint;
    double vreal;
    String *vstr;
  } save_result;
  String save_buff;
  
unknown's avatar
unknown committed
939 940

public:
941 942 943
  Item_func_set_user_var(LEX_STRING a,Item *b)
    :Item_func(b), cached_result_type(INT_RESULT), name(a)
  {}
unknown's avatar
unknown committed
944 945 946
  double val();
  longlong val_int();
  String *val_str(String *str);
unknown's avatar
unknown committed
947
  bool update_hash(void *ptr, uint length, enum Item_result type, 
948
  		   CHARSET_INFO *cs, Derivation dv);
949
  bool check();
unknown's avatar
unknown committed
950 951
  bool update();
  enum Item_result result_type () const { return cached_result_type; }
unknown's avatar
unknown committed
952
  bool fix_fields(THD *thd, struct st_table_list *tables, Item **ref);
unknown's avatar
unknown committed
953
  void fix_length_and_dec();
unknown's avatar
unknown committed
954
  void print(String *str);
955
  void print_as_stmt(String *str);
unknown's avatar
unknown committed
956 957 958 959 960 961 962
  const char *func_name() const { return "set_user_var"; }
};


class Item_func_get_user_var :public Item_func
{
  LEX_STRING name;
963
  user_var_entry *var_entry;
unknown's avatar
unknown committed
964 965

public:
966
  Item_func_get_user_var(LEX_STRING a):
967
    Item_func(), name(a) {}
968 969
  enum Functype functype() const { return GUSERVAR_FUNC; }
  LEX_STRING get_name() { return name; }
unknown's avatar
unknown committed
970 971 972 973
  double val();
  longlong val_int();
  String *val_str(String* str);
  void fix_length_and_dec();
unknown's avatar
unknown committed
974
  void print(String *str);
unknown's avatar
unknown committed
975
  enum Item_result result_type() const;
976 977 978 979 980
  /*
    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)
  */
  enum_field_types field_type() const  { return MYSQL_TYPE_STRING; }
unknown's avatar
unknown committed
981
  const char *func_name() const { return "get_user_var"; }
982
  bool const_item() const;
983
  table_map used_tables() const
984
  { return const_item() ? 0 : RAND_TABLE_BIT; }
985
  bool eq(const Item *item, bool binary_cmp) const;
986
};
unknown's avatar
unknown committed
987

unknown's avatar
unknown committed
988

unknown's avatar
unknown committed
989 990 991 992 993 994 995 996 997 998
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;}
};


999
/* for fulltext search */
1000
#include <ft_global.h>
unknown's avatar
unknown committed
1001 1002 1003 1004

class Item_func_match :public Item_real_func
{
public:
1005
  uint key, flags;
unknown's avatar
unknown committed
1006
  bool join_key;
1007 1008 1009 1010 1011 1012 1013
  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
unknown's avatar
unknown committed
1014

unknown's avatar
unknown committed
1015 1016
  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) { }
unknown's avatar
unknown committed
1017
  void cleanup()
1018
  {
unknown's avatar
unknown committed
1019
    DBUG_ENTER("Item_func_match");
1020
    Item_real_func::cleanup();
1021
    if (!master && ft_handler)
1022
    {
unknown's avatar
unknown committed
1023 1024
      ft_handler->please->close_search(ft_handler);
      ft_handler=0;
unknown's avatar
unknown committed
1025
      if (join_key)
1026
	table->file->ft_handler=0;
unknown's avatar
unknown committed
1027
      table->fulltext_searched=0;
1028
    }
1029
    if (concat)
unknown's avatar
unknown committed
1030
    {
1031
      delete concat;
unknown's avatar
unknown committed
1032 1033
      concat= 0;
    }
unknown's avatar
unknown committed
1034
    DBUG_VOID_RETURN;
1035
  }
unknown's avatar
unknown committed
1036
  enum Functype functype() const { return FT_FUNC; }
1037
  const char *func_name() const { return "match"; }
unknown's avatar
unknown committed
1038
  void update_used_tables() {}
1039
  table_map not_null_tables() const { return 0; }
unknown's avatar
unknown committed
1040
  bool fix_fields(THD *thd, struct st_table_list *tlist, Item **ref);
1041
  bool eq(const Item *, bool binary_cmp) const;
1042
  longlong val_int() { DBUG_ASSERT(fixed == 1); return val()!=0.0; }
1043
  double val();
1044
  void print(String *str);
1045 1046

  bool fix_index();
1047
  void init_search(bool no_order);
1048
};
unknown's avatar
unknown committed
1049

unknown's avatar
unknown committed
1050

1051
class Item_func_bit_xor : public Item_func_bit
unknown's avatar
unknown committed
1052 1053
{
public:
1054
  Item_func_bit_xor(Item *a, Item *b) :Item_func_bit(a, b) {}
unknown's avatar
unknown committed
1055 1056 1057 1058
  longlong val_int();
  const char *func_name() const { return "^"; }
};

1059
class Item_func_is_free_lock :public Item_int_func
unknown's avatar
unknown committed
1060 1061 1062
{
  String value;
public:
1063
  Item_func_is_free_lock(Item *a) :Item_int_func(a) {}
unknown's avatar
unknown committed
1064
  longlong val_int();
1065
  const char *func_name() const { return "is_free_lock"; }
unknown's avatar
unknown committed
1066
  void fix_length_and_dec() { decimals=0; max_length=1; maybe_null=1;}
unknown's avatar
unknown committed
1067 1068
};

unknown's avatar
SCRUM  
unknown committed
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
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;}
};

unknown's avatar
unknown committed
1079 1080
/* For type casts */

1081
enum Cast_target
unknown's avatar
unknown committed
1082 1083
{
  ITEM_CAST_BINARY, ITEM_CAST_SIGNED_INT, ITEM_CAST_UNSIGNED_INT,
unknown's avatar
unknown committed
1084
  ITEM_CAST_DATE, ITEM_CAST_TIME, ITEM_CAST_DATETIME, ITEM_CAST_CHAR
unknown's avatar
unknown committed
1085
};
1086 1087


1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
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; }
};


1098 1099 1100 1101 1102 1103 1104
/*
 *
 * Stored FUNCTIONs
 *
 */

class sp_head;
1105
class sp_name;
1106 1107 1108 1109

class Item_func_sp :public Item_func
{
private:
1110
  sp_name *m_name;
1111
  mutable sp_head *m_sp;
1112 1113 1114 1115 1116

  int execute(Item **itp);

public:

1117
  Item_func_sp(sp_name *name);
1118

1119
  Item_func_sp(sp_name *name, List<Item> &list);
1120 1121 1122 1123

  virtual ~Item_func_sp()
  {}

1124
  const char *func_name() const;
1125

1126 1127
  enum enum_field_types field_type() const;

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
  Item_result result_type() const;

  longlong val_int()
  {
    return (longlong)Item_func_sp::val();
  }

  double val()
  {
    Item *it;
1138
    double d;
1139 1140

    if (execute(&it))
1141 1142
    {
      null_value= 1;
1143
      return 0.0;
1144
    }
1145 1146 1147
    d= it->val();
    null_value= it->null_value;
    return d;
1148 1149 1150 1151 1152
  }

  String *val_str(String *str)
  {
    Item *it;
1153
    String *s;
1154 1155

    if (execute(&it))
1156 1157
    {
      null_value= 1;
1158
      return NULL;
1159
    }
1160 1161 1162
    s= it->val_str(str);
    null_value= it->null_value;
    return s;
1163 1164 1165 1166 1167
  }

  void fix_length_and_dec();

};
unknown's avatar
unknown committed
1168 1169


1170 1171 1172 1173 1174 1175 1176 1177
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; }
};