17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include <drizzled/server_includes.h>
25
#include <drizzled/version.h>
22
27
#include <drizzled/sql_string.h>
23
28
#include <drizzled/sql_list.h>
25
#include <drizzled/function/math/int.h>
26
#include <drizzled/field/int32.h>
27
#include <drizzled/field/int64.h>
28
#include <drizzled/field/double.h>
29
#include <drizzled/field/decimal.h>
30
#include <drizzled/session.h>
31
#include <drizzled/error.h>
32
#include <drizzled/check_stack_overrun.h>
30
#include <drizzled/functions/int.h>
42
33
void Item_func::set_arguments(List<Item> &list)
59
50
list.empty(); // Fields are used
62
Item_func::Item_func(List<Item> &list) :
63
_session(*current_session),
53
Item_func::Item_func(List<Item> &list)
66
collation.set(DERIVATION_SYSCONST);
67
56
set_arguments(list);
70
Item_func::Item_func(Session *session, Item_func *item) :
71
Item_result_field(session, item),
72
_session(*current_session),
73
allowed_arg_cols(item->allowed_arg_cols),
74
arg_count(item->arg_count),
75
used_tables_cache(item->used_tables_cache),
76
not_null_tables_cache(item->not_null_tables_cache),
77
const_item_cache(item->const_item_cache)
59
Item_func::Item_func(THD *thd, Item_func *item)
60
:Item_result_field(thd, item),
61
allowed_arg_cols(item->allowed_arg_cols),
62
arg_count(item->arg_count),
63
used_tables_cache(item->used_tables_cache),
64
not_null_tables_cache(item->not_null_tables_cache),
65
const_item_cache(item->const_item_cache)
128
Item_func::fix_fields(Session *session, Item **)
115
Item_func::fix_fields(THD *thd, Item **ref __attribute__((unused)))
130
117
assert(fixed == 0);
131
118
Item **arg,**arg_end;
132
void *save_session_marker= session->session_marker;
119
void *save_thd_marker= thd->thd_marker;
133
120
unsigned char buff[STACK_BUFF_ALLOC]; // Max argument in function
134
session->session_marker= 0;
135
122
used_tables_cache= not_null_tables_cache= 0;
136
123
const_item_cache=1;
138
if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
125
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
139
126
return true; // Fatal error if flag is set!
141
128
{ // Print purify happy
146
133
We can't yet set item to *arg as fix_fields may change *arg
147
134
We shouldn't call fix_fields() twice, so check 'fixed' field first
149
if ((!(*arg)->fixed && (*arg)->fix_fields(session, arg)))
136
if ((!(*arg)->fixed && (*arg)->fix_fields(thd, arg)))
137
return true; /* purecov: inspected */
153
140
if (allowed_arg_cols)
176
163
fix_length_and_dec();
177
if (session->is_error()) // An error inside fix_length_and_dec occured
164
if (thd->is_error()) // An error inside fix_length_and_dec occured
180
session->session_marker= save_session_marker;
167
thd->thd_marker= save_thd_marker;
185
void Item_func::fix_after_pullout(Select_Lex *new_parent,
172
void Item_func::fix_after_pullout(st_select_lex *new_parent,
173
Item **ref __attribute__((unused)))
188
175
Item **arg,**arg_end;
228
215
Item **arg,**arg_end;
232
219
(*traverser)(this, argument);
233
220
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
235
222
(*arg)->traverse_cond(traverser, argument, order);
239
226
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
241
228
(*arg)->traverse_cond(traverser, argument, order);
280
Session::change_item_tree() should be called only if the tree was
267
THD::change_item_tree() should be called only if the tree was
281
268
really transformed, i.e. when a new item has been created.
282
269
Otherwise we'll be allocating a lot of unnecessary memory for
283
270
change records at each execution.
285
272
if (*arg != new_item)
286
getSession().change_item_tree(arg, new_item);
273
current_thd->change_item_tree(arg, new_item);
289
276
return (this->*transformer)(argument);
331
318
unsigned char *arg_v= *arg_p;
332
319
Item *new_item= (*arg)->compile(analyzer, &arg_v, transformer, arg_t);
333
320
if (new_item && *arg != new_item)
334
current_session->change_item_tree(arg, new_item);
321
current_thd->change_item_tree(arg, new_item);
337
324
return (this->*transformer)(arg_t);
341
328
See comments in Item_cmp_func::split_sum_func()
344
void Item_func::split_sum_func(Session *session, Item **ref_pointer_array,
331
void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array,
345
332
List<Item> &fields)
347
334
Item **arg, **arg_end;
348
335
for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++)
349
(*arg)->split_sum_func(session, ref_pointer_array, fields, arg, true);
336
(*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg, true);
455
442
Field *Item_func::tmp_table_field(Table *table)
459
446
switch (result_type()) {
461
448
if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
462
field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
449
field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
464
field= new field::Int32(max_length, maybe_null, name, unsigned_flag);
451
field= new Field_long(max_length, maybe_null, name, unsigned_flag);
467
453
case REAL_RESULT:
468
454
field= new Field_double(max_length, maybe_null, name, decimals);
471
456
case STRING_RESULT:
472
457
return make_string_field(table);
474
459
case DECIMAL_RESULT:
475
field= new Field_decimal(my_decimal_precision_to_length(decimal_precision(),
460
field= new Field_new_decimal(
461
my_decimal_precision_to_length(decimal_precision(),
464
maybe_null, name, decimals, unsigned_flag);
484
468
// This case should never be chosen
490
474
field->init(table);
528
511
double Item_func::fix_result(double value)
530
static double fix_infinity= numeric_limits<double>::infinity();
532
if (value != fix_infinity && value != -fix_infinity)
513
if (CMATH_NAMESPACE::isfinite(value))
539
void Item_func::fix_num_length_and_dec()
541
uint32_t fl_length= 0;
543
for (uint32_t i=0 ; i < arg_count ; i++)
545
set_if_bigger(decimals,args[i]->decimals);
546
set_if_bigger(fl_length, args[i]->max_length);
548
max_length=float_length(decimals);
549
if (fl_length > max_length)
551
decimals= NOT_FIXED_DEC;
552
max_length= float_length(NOT_FIXED_DEC);
557
Set max_length/decimals of function if function is fixed point and
558
result length/precision depends on argument ones.
561
void Item_func::count_decimal_length()
566
for (uint32_t i= 0 ; i < arg_count ; i++)
568
set_if_bigger(decimals, args[i]->decimals);
569
set_if_bigger(max_int_part, args[i]->decimal_int_part());
570
set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
572
int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
573
max_length= my_decimal_precision_to_length(precision, decimals,
579
Set max_length of if it is maximum length of its arguments.
582
void Item_func::count_only_length()
586
for (uint32_t i=0 ; i < arg_count ; i++)
588
set_if_bigger(max_length, args[i]->max_length);
589
set_if_bigger(unsigned_flag, args[i]->unsigned_flag);
595
Set max_length/decimals of function if function is floating point and
596
result length/precision depends on argument ones.
599
void Item_func::count_real_length()
604
for (uint32_t i=0 ; i < arg_count ; i++)
606
if (decimals != NOT_FIXED_DEC)
608
set_if_bigger(decimals, args[i]->decimals);
609
set_if_bigger(length, (args[i]->max_length - args[i]->decimals));
611
set_if_bigger(max_length, args[i]->max_length);
613
if (decimals != NOT_FIXED_DEC)
617
if (length < max_length) // If previous operation gave overflow
618
max_length= UINT32_MAX;
626
void Item_func::signal_divide_by_null()
628
my_error(ER_DIVISION_BY_ZERO, MYF(0));
633
Item *Item_func::get_tmp_table_item(Session *session)
635
if (!with_sum_func && !const_item() && functype() != SUSERVAR_FUNC)
636
return new Item_field(result_field);
637
return copy_or_same(session);
641
} /* namespace drizzled */