17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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/session.h>
20
#include <drizzled/server_includes.h>
25
#include <drizzled/version.h>
27
#include <drizzled/sql_string.h>
32
28
#include <drizzled/sql_list.h>
33
#include <drizzled/sql_string.h>
44
Item_func::Item_func(void):
45
_session(*current_session),
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
_session(*current_session),
55
allowed_arg_cols(1), arg_count(1),
56
const_item_cache(false)
60
with_sum_func= a->with_sum_func;
61
collation.set(DERIVATION_SYSCONST);
64
Item_func::Item_func(Item *a,Item *b):
65
_session(*current_session),
66
allowed_arg_cols(1), arg_count(2),
67
const_item_cache(false)
70
args[0]= a; args[1]= b;
71
with_sum_func= a->with_sum_func || b->with_sum_func;
72
collation.set(DERIVATION_SYSCONST);
75
Item_func::Item_func(Item *a,Item *b,Item *c):
76
_session(*current_session),
78
const_item_cache(false)
81
if ((args= (Item**) memory::sql_alloc(sizeof(Item*)*3)))
84
args[0]= a; args[1]= b; args[2]= c;
85
with_sum_func= a->with_sum_func || b->with_sum_func || c->with_sum_func;
87
collation.set(DERIVATION_SYSCONST);
90
Item_func::Item_func(Item *a,Item *b,Item *c,Item *d):
91
_session(*current_session),
93
const_item_cache(false)
96
if ((args= (Item**) memory::sql_alloc(sizeof(Item*)*4)))
99
args[0]= a; args[1]= b; args[2]= c; args[3]= d;
100
with_sum_func= a->with_sum_func || b->with_sum_func ||
101
c->with_sum_func || d->with_sum_func;
103
collation.set(DERIVATION_SYSCONST);
106
Item_func::Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
107
_session(*current_session),
109
const_item_cache(false)
112
if ((args= (Item**) memory::sql_alloc(sizeof(Item*)*5)))
114
args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
115
with_sum_func= a->with_sum_func || b->with_sum_func ||
116
c->with_sum_func || d->with_sum_func || e->with_sum_func ;
118
collation.set(DERIVATION_SYSCONST);
30
#include <drizzled/functions/int.h>
122
33
void Item_func::set_arguments(List<Item> &list)
136
47
with_sum_func|=item->with_sum_func;
139
list.clear(); // Fields are used
50
list.empty(); // Fields are used
142
Item_func::Item_func(List<Item> &list) :
143
_session(*current_session),
145
const_item_cache(false)
53
Item_func::Item_func(List<Item> &list)
147
collation.set(DERIVATION_SYSCONST);
148
56
set_arguments(list);
151
Item_func::Item_func(Session *session, Item_func *item) :
152
Item_result_field(session, item),
153
_session(*current_session),
154
allowed_arg_cols(item->allowed_arg_cols),
155
arg_count(item->arg_count),
156
used_tables_cache(item->used_tables_cache),
157
not_null_tables_cache(item->not_null_tables_cache),
158
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)
209
Item_func::fix_fields(Session *session, Item **)
115
Item_func::fix_fields(THD *thd, Item **ref __attribute__((unused)))
211
117
assert(fixed == 0);
212
118
Item **arg,**arg_end;
213
void *save_session_marker= session->session_marker;
119
void *save_thd_marker= thd->thd_marker;
214
120
unsigned char buff[STACK_BUFF_ALLOC]; // Max argument in function
215
session->session_marker= 0;
216
122
used_tables_cache= not_null_tables_cache= 0;
217
const_item_cache= true;
219
if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
125
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
220
126
return true; // Fatal error if flag is set!
222
128
{ // Print purify happy
361
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
362
268
really transformed, i.e. when a new item has been created.
363
269
Otherwise we'll be allocating a lot of unnecessary memory for
364
270
change records at each execution.
366
272
if (*arg != new_item)
367
getSession().change_item_tree(arg, new_item);
273
current_thd->change_item_tree(arg, new_item);
370
276
return (this->*transformer)(argument);
422
328
See comments in Item_cmp_func::split_sum_func()
425
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,
426
332
List<Item> &fields)
428
334
Item **arg, **arg_end;
429
335
for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++)
430
(*arg)->split_sum_func(session, ref_pointer_array, fields, arg, true);
336
(*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg, true);
434
340
void Item_func::update_used_tables()
436
342
used_tables_cache=0;
437
const_item_cache= true;
438
344
for (uint32_t i=0 ; i < arg_count ; i++)
440
346
args[i]->update_used_tables();
536
442
Field *Item_func::tmp_table_field(Table *table)
540
446
switch (result_type()) {
544
field= new field::Size(max_length, maybe_null, name, true);
546
else if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
548
field= new field::Int64(max_length, maybe_null, name, false);
448
if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
449
field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
552
field= new field::Int32(max_length, maybe_null, name, false);
451
field= new Field_long(max_length, maybe_null, name, unsigned_flag);
557
453
case REAL_RESULT:
558
454
field= new Field_double(max_length, maybe_null, name, decimals);
561
456
case STRING_RESULT:
562
457
return make_string_field(table);
564
459
case DECIMAL_RESULT:
565
field= new Field_decimal(class_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);
574
468
// This case should never be chosen
580
474
field->init(table);
586
type::Decimal *Item_func::val_decimal(type::Decimal *decimal_value)
479
my_decimal *Item_func::val_decimal(my_decimal *decimal_value)
589
int2_class_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
482
int2my_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
590
483
return decimal_value;
618
511
double Item_func::fix_result(double value)
620
static double fix_infinity= numeric_limits<double>::infinity();
622
if (value != fix_infinity && value != -fix_infinity)
513
if (CMATH_NAMESPACE::isfinite(value))
629
void Item_func::fix_num_length_and_dec()
631
uint32_t fl_length= 0;
633
for (uint32_t i=0 ; i < arg_count ; i++)
635
set_if_bigger(decimals,args[i]->decimals);
636
set_if_bigger(fl_length, args[i]->max_length);
638
max_length=float_length(decimals);
639
if (fl_length > max_length)
641
decimals= NOT_FIXED_DEC;
642
max_length= float_length(NOT_FIXED_DEC);
647
Set max_length/decimals of function if function is fixed point and
648
result length/precision depends on argument ones.
651
void Item_func::count_decimal_length()
656
for (uint32_t i= 0 ; i < arg_count ; i++)
658
set_if_bigger(decimals, args[i]->decimals);
659
set_if_bigger(max_int_part, args[i]->decimal_int_part());
660
set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
662
int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
663
max_length= class_decimal_precision_to_length(precision, decimals,
669
Set max_length of if it is maximum length of its arguments.
672
void Item_func::count_only_length()
676
for (uint32_t i=0 ; i < arg_count ; i++)
678
set_if_bigger(max_length, args[i]->max_length);
679
set_if_bigger(unsigned_flag, args[i]->unsigned_flag);
685
Set max_length/decimals of function if function is floating point and
686
result length/precision depends on argument ones.
689
void Item_func::count_real_length()
694
for (uint32_t i=0 ; i < arg_count ; i++)
696
if (decimals != NOT_FIXED_DEC)
698
set_if_bigger(decimals, args[i]->decimals);
699
set_if_bigger(length, (args[i]->max_length - args[i]->decimals));
701
set_if_bigger(max_length, args[i]->max_length);
703
if (decimals != NOT_FIXED_DEC)
707
if (length < max_length) // If previous operation gave overflow
708
max_length= UINT32_MAX;
716
void Item_func::signal_divide_by_null()
718
my_error(ER_DIVISION_BY_ZERO, MYF(0));
723
Item *Item_func::get_tmp_table_item(Session *session)
725
if (!with_sum_func && !const_item() && functype() != SUSERVAR_FUNC)
726
return new Item_field(result_field);
727
return copy_or_same(session);
731
} /* namespace drizzled */