22 #include <drizzled/check_stack_overrun.h>
23 #include <drizzled/current_session.h>
24 #include <drizzled/error.h>
25 #include <drizzled/field/decimal.h>
26 #include <drizzled/field/double.h>
27 #include <drizzled/field/int32.h>
28 #include <drizzled/field/int64.h>
29 #include <drizzled/field/size.h>
30 #include <drizzled/function/math/int.h>
31 #include <drizzled/item/field.h>
32 #include <drizzled/session.h>
33 #include <drizzled/sql_list.h>
34 #include <drizzled/sql_string.h>
45 Item_func::Item_func(
void):
46 allowed_arg_cols(1), arg_count(0),
47 const_item_cache(false)
50 collation.set(DERIVATION_SYSCONST);
53 Item_func::Item_func(Item *a):
54 allowed_arg_cols(1), arg_count(1),
55 const_item_cache(false)
59 with_sum_func= a->with_sum_func;
60 collation.set(DERIVATION_SYSCONST);
63 Item_func::Item_func(Item *a,Item *b):
64 allowed_arg_cols(1), arg_count(2),
65 const_item_cache(false)
68 args[0]= a; args[1]= b;
69 with_sum_func= a->with_sum_func || b->with_sum_func;
70 collation.set(DERIVATION_SYSCONST);
73 Item_func::Item_func(Item *a,Item *b,Item *c):
75 const_item_cache(false)
78 if ((args= (
Item**) memory::sql_alloc(
sizeof(
Item*)*3)))
81 args[0]= a; args[1]= b; args[2]= c;
82 with_sum_func= a->with_sum_func || b->with_sum_func || c->with_sum_func;
84 collation.set(DERIVATION_SYSCONST);
87 Item_func::Item_func(Item *a,Item *b,Item *c,Item *d):
89 const_item_cache(false)
92 if ((args= (
Item**) memory::sql_alloc(
sizeof(
Item*)*4)))
95 args[0]= a; args[1]= b; args[2]= c; args[3]= d;
96 with_sum_func= a->with_sum_func || b->with_sum_func ||
97 c->with_sum_func || d->with_sum_func;
99 collation.set(DERIVATION_SYSCONST);
102 Item_func::Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
104 const_item_cache(false)
107 if ((args= (
Item**) memory::sql_alloc(
sizeof(
Item*)*5)))
109 args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
110 with_sum_func= a->with_sum_func || b->with_sum_func ||
111 c->with_sum_func || d->with_sum_func || e->with_sum_func ;
113 collation.set(DERIVATION_SYSCONST);
117 void Item_func::set_arguments(List<Item> &list)
120 arg_count=list.size();
122 if (arg_count <= 2 || (args=(
Item**) memory::sql_alloc(
sizeof(
Item*)*arg_count)))
124 List<Item>::iterator li(list.begin());
125 Item **save_args= args;
127 while (
Item* item=li++)
129 *(save_args++)= item;
130 with_sum_func|=item->with_sum_func;
136 Item_func::Item_func(List<Item> &list) :
138 const_item_cache(false)
140 collation.set(DERIVATION_SYSCONST);
144 Item_func::Item_func(Session *session, Item_func *item) :
145 Item_result_field(session, item),
146 allowed_arg_cols(item->allowed_arg_cols),
147 arg_count(item->arg_count),
148 used_tables_cache(item->used_tables_cache),
149 not_null_tables_cache(item->not_null_tables_cache),
150 const_item_cache(item->const_item_cache)
158 args=
new (getSession().mem)
Item*[arg_count];
160 memcpy(args, item->args,
sizeof(
Item*)*arg_count);
162 collation.set(DERIVATION_SYSCONST);
200 Item_func::fix_fields(Session *session, Item **)
203 Item **arg,**arg_end;
204 void *save_session_marker= session->session_marker;
206 session->session_marker= 0;
207 used_tables_cache= not_null_tables_cache= 0;
208 const_item_cache=
true;
214 for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
221 if ((!(*arg)->fixed && (*arg)->fix_fields(session, arg)))
225 if (allowed_arg_cols)
227 if (item->check_cols(allowed_arg_cols))
234 allowed_arg_cols= item->cols();
235 assert(allowed_arg_cols);
238 if (item->maybe_null)
241 with_sum_func= with_sum_func || item->with_sum_func;
242 used_tables_cache|= item->used_tables();
243 not_null_tables_cache|= item->not_null_tables();
244 const_item_cache&= item->const_item();
248 fix_length_and_dec();
249 if (session->is_error())
252 session->session_marker= save_session_marker;
260 Item **arg,**arg_end;
262 used_tables_cache= not_null_tables_cache= 0;
263 const_item_cache=
false;
267 for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
269 (*arg)->fix_after_pullout(new_parent, arg);
280 bool Item_func::walk(Item_processor processor,
bool walk_subquery,
281 unsigned char *argument)
285 Item **arg,**arg_end;
286 for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
288 if ((*arg)->walk(processor, walk_subquery, argument))
292 return (this->*processor)(argument);
295 void Item_func::traverse_cond(Cond_traverser traverser,
296 void *argument, traverse_order order)
300 Item **arg,**arg_end;
304 (*traverser)(
this, argument);
305 for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
307 (*arg)->traverse_cond(traverser, argument, order);
311 for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
313 (*arg)->traverse_cond(traverser, argument, order);
315 (*traverser)(
this, argument);
319 (*traverser)(
this, argument);
344 Item **arg,**arg_end;
345 for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
353 return (this->*transformer)(argument);
382 Item_transformer transformer,
unsigned char *arg_t)
384 if (!(this->*analyzer)(arg_p))
388 Item **arg,**arg_end;
389 for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
395 unsigned char *arg_v= *arg_p;
396 Item *new_item= (*arg)->
compile(analyzer, &arg_v, transformer, arg_t);
397 if (new_item && *arg != new_item)
401 return (this->*transformer)(arg_t);
411 Item **arg, **arg_end;
412 for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++)
413 (*arg)->split_sum_func(session, ref_pointer_array, fields, arg,
true);
417 void Item_func::update_used_tables()
420 const_item_cache=
true;
421 for (uint32_t i=0 ; i < arg_count ; i++)
423 args[i]->update_used_tables();
432 return used_tables_cache;
438 return not_null_tables_cache;
444 str->append(func_name(), strlen(func_name()));
451 void Item_func::print_args(
String *str, uint32_t from)
453 for (uint32_t i=from ; i < arg_count ; i++)
462 void Item_func::print_op(String *str)
465 for (uint32_t i=0 ; i < arg_count-1 ; i++)
469 str->append(func_name(), strlen(func_name()));
472 args[arg_count-1]->
print(str);
482 if (item->type() != FUNC_ITEM)
485 Item_func::Functype func_type;
486 if ((func_type= functype()) != item_func->functype() ||
487 arg_count != item_func->arg_count ||
488 (func_type != Item_func::FUNC_SP &&
489 func_name() != item_func->func_name()) ||
490 (func_type == Item_func::FUNC_SP &&
491 system_charset_info->strcasecmp(func_name(), item_func->func_name())))
493 for (uint32_t i=0; i < arg_count ; i++)
494 if (!args[i]->
eq(item_func->args[i], binary_cmp))
500 bool Item_func::get_arg0_date(
type::Time <ime, uint32_t fuzzy_date)
506 bool Item_func::get_arg0_time(type::Time <ime)
519 Field *Item_func::tmp_table_field(
Table *table)
523 switch (result_type()) {
529 else if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
548 field=
new Field_decimal(class_decimal_precision_to_length(decimal_precision(), decimals, unsigned_flag),
567 int2_class_decimal(E_DEC_FATAL_ERROR,
val_int(), unsigned_flag, decimal_value);
568 return decimal_value;
573 uint32_t nitems, uint32_t flags)
575 return agg_item_collations(c, func_name(), items, nitems, flags, 1);
579 bool Item_func::agg_arg_collations_for_comparison(DTCollation &c,
584 return agg_item_collations_for_comparison(c, func_name(),
585 items, nitems, flags);
589 bool Item_func::agg_arg_charsets(DTCollation &c, Item **items, uint32_t nitems,
590 uint32_t flags,
int item_sep)
592 return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep);
596 double Item_func::fix_result(
double value)
598 static double fix_infinity= numeric_limits<double>::infinity();
600 if (value != fix_infinity && value != -fix_infinity)
607 void Item_func::fix_num_length_and_dec()
609 uint32_t fl_length= 0;
611 for (uint32_t i=0 ; i < arg_count ; i++)
613 set_if_bigger(decimals,args[i]->decimals);
614 set_if_bigger(fl_length, args[i]->max_length);
616 max_length=float_length(decimals);
617 if (fl_length > max_length)
619 decimals= NOT_FIXED_DEC;
620 max_length= float_length(NOT_FIXED_DEC);
634 for (uint32_t i= 0 ; i < arg_count ; i++)
636 set_if_bigger(decimals, args[i]->decimals);
637 set_if_bigger(max_int_part, args[i]->decimal_int_part());
638 set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
640 int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
641 max_length= class_decimal_precision_to_length(precision, decimals,
654 for (uint32_t i=0 ; i < arg_count ; i++)
656 set_if_bigger(max_length, args[i]->max_length);
657 set_if_bigger(unsigned_flag, args[i]->unsigned_flag);
672 for (uint32_t i=0 ; i < arg_count ; i++)
674 if (decimals != NOT_FIXED_DEC)
676 set_if_bigger(decimals, args[i]->decimals);
677 set_if_bigger(length, (args[i]->max_length - args[i]->decimals));
679 set_if_bigger(max_length, args[i]->max_length);
681 if (decimals != NOT_FIXED_DEC)
685 if (length < max_length)
686 max_length= UINT32_MAX;
694 void Item_func::signal_divide_by_null()
696 my_error(ER_DIVISION_BY_ZERO, MYF(0));
701 Item *Item_func::get_tmp_table_item(Session *session)
703 if (!with_sum_func && !
const_item() && functype() != SUSERVAR_FUNC)
704 return new Item_field(result_field);
705 return copy_or_same(session);