23
23
#include <drizzled/server_includes.h>
24
24
#include <drizzled/sql_select.h>
25
#include <drizzled/drizzled_error_messages.h>
25
#include <drizzled/error.h>
28
#if defined(CMATH_NAMESPACE)
29
using namespace CMATH_NAMESPACE;
32
extern my_decimal decimal_zero;
28
35
Prepare an aggregate function item for checking context conditions.
49
bool Item_sum::init_sum_func_check(THD *thd)
56
bool Item_sum::init_sum_func_check(Session *session)
51
if (!thd->lex->allow_sum_func)
58
if (!session->lex->allow_sum_func)
53
60
my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
57
64
/* Set a reference to the nesting set function if there is any */
58
in_sum_func= thd->lex->in_sum_func;
65
in_sum_func= session->lex->in_sum_func;
59
66
/* Save a pointer to object to be used in items for nested set functions */
60
thd->lex->in_sum_func= this;
61
nest_level= thd->lex->current_select->nest_level;
67
session->lex->in_sum_func= this;
68
nest_level= session->lex->current_select->nest_level;
289
bool Item_sum::register_sum_func(THD *thd, Item **ref)
296
bool Item_sum::register_sum_func(Session *session, Item **ref)
292
nesting_map allow_sum_func= thd->lex->allow_sum_func;
293
for (sl= thd->lex->current_select->master_unit()->outer_select() ;
299
nesting_map allow_sum_func= session->lex->allow_sum_func;
300
for (sl= session->lex->current_select->master_unit()->outer_select() ;
294
301
sl && sl->nest_level > max_arg_level;
295
302
sl= sl->master_unit()->outer_select() )
341
348
with_sum_func being set for an st_select_lex means that this st_select_lex
342
349
has aggregate functions directly referenced (i.e. not through a sub-select).
344
for (sl= thd->lex->current_select;
351
for (sl= session->lex->current_select;
345
352
sl && sl != aggr_sel && sl->master_unit()->item;
346
353
sl= sl->master_unit()->outer_select() )
347
354
sl->master_unit()->item->with_sum_func= 1;
349
thd->lex->current_select->mark_as_dependent(aggr_sel);
356
session->lex->current_select->mark_as_dependent(aggr_sel);
374
381
Constructor used in processing select with temporary tebles.
377
Item_sum::Item_sum(THD *thd, Item_sum *item):
378
Item_result_field(thd, item), arg_count(item->arg_count),
384
Item_sum::Item_sum(Session *session, Item_sum *item):
385
Item_result_field(session, item), arg_count(item->arg_count),
379
386
aggr_sel(item->aggr_sel),
380
387
nest_level(item->nest_level), aggr_level(item->aggr_level),
381
388
quick_group(item->quick_group), used_tables_cache(item->used_tables_cache),
434
441
void Item_sum::fix_num_length_and_dec()
437
for (uint i=0 ; i < arg_count ; i++)
444
for (uint32_t i=0 ; i < arg_count ; i++)
438
445
set_if_bigger(decimals,args[i]->decimals);
439
446
max_length=float_length(decimals);
442
Item *Item_sum::get_tmp_table_item(THD *thd)
449
Item *Item_sum::get_tmp_table_item(Session *session)
444
Item_sum* sum_item= (Item_sum *) copy_or_same(thd);
451
Item_sum* sum_item= (Item_sum *) copy_or_same(session);
445
452
if (sum_item && sum_item->result_field) // If not a const sum func
447
454
Field *result_field_tmp= sum_item->result_field;
448
for (uint i=0 ; i < sum_item->arg_count ; i++)
455
for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
450
457
Item *arg= sum_item->args[i];
451
458
if (!arg->const_item())
562
Item_sum_num::fix_fields(THD *thd, Item **ref)
576
Item_sum_num::fix_fields(Session *session, Item **ref)
564
578
assert(fixed == 0);
566
if (init_sum_func_check(thd))
580
if (init_sum_func_check(session))
571
for (uint i=0 ; i < arg_count ; i++)
585
for (uint32_t i=0 ; i < arg_count ; i++)
573
if (args[i]->fix_fields(thd, args + i) || args[i]->check_cols(1))
587
if (args[i]->fix_fields(session, args + i) || args[i]->check_cols(1))
575
589
set_if_bigger(decimals, args[i]->decimals);
576
590
maybe_null |= args[i]->maybe_null;
591
Item_sum_hybrid::Item_sum_hybrid(THD *thd, Item_sum_hybrid *item)
592
:Item_sum(thd, item), value(item->value), hybrid_type(item->hybrid_type),
605
Item_sum_hybrid::Item_sum_hybrid(Session *session, Item_sum_hybrid *item)
606
:Item_sum(session, item), value(item->value), hybrid_type(item->hybrid_type),
593
607
hybrid_field_type(item->hybrid_field_type), cmp_sign(item->cmp_sign),
594
608
was_values(item->was_values)
621
Item_sum_hybrid::fix_fields(THD *thd, Item **ref)
635
Item_sum_hybrid::fix_fields(Session *session, Item **ref)
623
637
assert(fixed == 0);
625
639
Item *item= args[0];
627
if (init_sum_func_check(thd))
641
if (init_sum_func_check(session))
630
644
// 'item' can be changed during fix_fields
631
if ((!item->fixed && item->fix_fields(thd, args)) ||
645
if ((!item->fixed && item->fix_fields(session, args)) ||
632
646
(item= args[0])->check_cols(1))
634
648
decimals=item->decimals;
676
690
Field *Item_sum_hybrid::create_tmp_field(bool group, Table *table,
677
uint convert_blob_length)
691
uint32_t convert_blob_length)
680
694
if (args[0]->type() == Item::FIELD_ITEM)
682
696
field= ((Item_field*) args[0])->field;
684
if ((field= create_tmp_field_from_field(current_thd, field, name, table,
698
if ((field= create_tmp_field_from_field(current_session, field, name, table,
685
699
NULL, convert_blob_length)))
686
700
field->flags&= ~NOT_NULL_FLAG;
720
734
check if the following assignments are really needed
722
Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item)
723
:Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
736
Item_sum_sum::Item_sum_sum(Session *session, Item_sum_sum *item)
737
:Item_sum_num(session, item), hybrid_type(item->hybrid_type),
724
738
curr_dec_buff(item->curr_dec_buff)
726
740
/* TODO: check if the following assignments are really needed */
883
Item_sum_distinct::Item_sum_distinct(THD *thd, Item_sum_distinct *original)
884
:Item_sum_num(thd, original), val(original->val), tree(0),
901
Item_sum_distinct::Item_sum_distinct(Session *session, Item_sum_distinct *original)
902
:Item_sum_num(session, original), val(original->val), tree(0),
885
903
table_field_type(original->table_field_type)
1122
1140
Item_sum_avg_distinct::fix_length_and_dec()
1124
1142
Item_sum_distinct::fix_length_and_dec();
1125
prec_increment= current_thd->variables.div_precincrement;
1143
prec_increment= current_session->variables.div_precincrement;
1127
1145
AVG() will divide val by count. We need to reserve digits
1128
1146
after decimal point as the result can be fractional.
1130
decimals= min(decimals + prec_increment, (unsigned int)NOT_FIXED_DEC);
1148
decimals= cmin(decimals + prec_increment, (unsigned int)NOT_FIXED_DEC);
1186
1204
Item_sum_sum::fix_length_and_dec();
1187
1205
maybe_null=null_value=1;
1188
prec_increment= current_thd->variables.div_precincrement;
1206
prec_increment= current_session->variables.div_precincrement;
1189
1207
if (hybrid_type == DECIMAL_RESULT)
1191
1209
int precision= args[0]->decimal_precision() + prec_increment;
1192
decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1210
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1193
1211
max_length= my_decimal_precision_to_length(precision, decimals,
1194
1212
unsigned_flag);
1195
f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1213
f_precision= cmin(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1196
1214
f_scale= args[0]->decimals;
1197
1215
dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1200
decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1218
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1201
1219
max_length= args[0]->max_length + prec_increment;
1206
Item *Item_sum_avg::copy_or_same(THD* thd)
1224
Item *Item_sum_avg::copy_or_same(Session* session)
1208
return new (thd->mem_root) Item_sum_avg(thd, this);
1226
return new (session->mem_root) Item_sum_avg(session, this);
1212
1230
Field *Item_sum_avg::create_tmp_field(bool group, Table *table,
1213
uint convert_blob_len __attribute__((unused)))
1231
uint32_t convert_blob_len __attribute__((unused)))
1365
Item_sum_variance::Item_sum_variance(THD *thd, Item_sum_variance *item):
1366
Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
1389
Item_sum_variance::Item_sum_variance(Session *session, Item_sum_variance *item):
1390
Item_sum_num(session, item), hybrid_type(item->hybrid_type),
1367
1391
count(item->count), sample(item->sample),
1368
1392
prec_increment(item->prec_increment)
1388
1412
switch (args[0]->result_type()) {
1389
1413
case REAL_RESULT:
1390
1414
case STRING_RESULT:
1391
decimals= min(args[0]->decimals + 4, NOT_FIXED_DEC);
1415
decimals= cmin(args[0]->decimals + 4, NOT_FIXED_DEC);
1393
1417
case INT_RESULT:
1394
1418
case DECIMAL_RESULT:
1396
1420
int precision= args[0]->decimal_precision()*2 + prec_increment;
1397
decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1421
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1398
1422
max_length= my_decimal_precision_to_length(precision, decimals,
1399
1423
unsigned_flag);
2553
2593
tmp_table_param->force_copy_fields= force_copy_fields;
2554
2594
assert(table == 0);
2556
if (!(table= create_tmp_table(thd, tmp_table_param, list, (order_st*) 0, 1,
2596
if (!(table= create_tmp_table(session, tmp_table_param, list, (order_st*) 0, 1,
2558
(select_lex->options | thd->options),
2598
(select_lex->options | session->options),
2559
2599
HA_POS_ERROR, (char*)"")))
2561
2601
table->file->extra(HA_EXTRA_NO_ROWS); // Don't update rows
2715
2755
return table->file->stats.records;
2719
/****************************************************************************
2720
** Functions to handle dynamic loadable aggregates
2721
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2722
** Adapted for UDAs by: Andreas F. Bobak <bobak@relog.ch>.
2723
** Rewritten by: Monty.
2724
****************************************************************************/
2726
void Item_udf_sum::clear()
2732
bool Item_udf_sum::add()
2734
udf.add(&null_value);
2738
void Item_udf_sum::cleanup()
2741
udf_handler::cleanup() nicely handles case when we have not
2742
original item but one created by copy_or_same() method.
2745
Item_sum::cleanup();
2749
void Item_udf_sum::print(String *str, enum_query_type query_type)
2751
str->append(func_name());
2753
for (uint i=0 ; i < arg_count ; i++)
2757
args[i]->print(str, query_type);
2763
Item *Item_sum_udf_float::copy_or_same(THD* thd)
2765
return new (thd->mem_root) Item_sum_udf_float(thd, this);
2768
double Item_sum_udf_float::val_real()
2771
return(udf.val(&null_value));
2775
String *Item_sum_udf_float::val_str(String *str)
2777
return val_string_from_real(str);
2781
my_decimal *Item_sum_udf_float::val_decimal(my_decimal *dec)
2783
return val_decimal_from_real(dec);
2787
String *Item_sum_udf_decimal::val_str(String *str)
2789
return val_string_from_decimal(str);
2793
double Item_sum_udf_decimal::val_real()
2795
return val_real_from_decimal();
2799
int64_t Item_sum_udf_decimal::val_int()
2801
return val_int_from_decimal();
2805
my_decimal *Item_sum_udf_decimal::val_decimal(my_decimal *dec_buf)
2808
return(udf.val_decimal(&null_value, dec_buf));
2812
Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
2814
return new (thd->mem_root) Item_sum_udf_decimal(thd, this);
2818
Item *Item_sum_udf_int::copy_or_same(THD* thd)
2820
return new (thd->mem_root) Item_sum_udf_int(thd, this);
2823
int64_t Item_sum_udf_int::val_int()
2826
return(udf.val_int(&null_value));
2830
String *Item_sum_udf_int::val_str(String *str)
2832
return val_string_from_int(str);
2835
my_decimal *Item_sum_udf_int::val_decimal(my_decimal *dec)
2837
return val_decimal_from_int(dec);
2841
/** Default max_length is max argument length. */
2843
void Item_sum_udf_str::fix_length_and_dec()
2846
for (uint i = 0; i < arg_count; i++)
2847
set_if_bigger(max_length,args[i]->max_length);
2852
Item *Item_sum_udf_str::copy_or_same(THD* thd)
2854
return new (thd->mem_root) Item_sum_udf_str(thd, this);
2858
my_decimal *Item_sum_udf_str::val_decimal(my_decimal *dec)
2860
return val_decimal_from_string(dec);
2863
String *Item_sum_udf_str::val_str(String *str)
2866
String *res=udf.val_str(str,&str_value);
2871
2758
/*****************************************************************************
2872
2759
GROUP_CONCAT function
3108
Item_func_group_concat::Item_func_group_concat(THD *thd,
2995
Item_func_group_concat::Item_func_group_concat(Session *session,
3109
2996
Item_func_group_concat *item)
3110
:Item_sum(thd, item),
2997
:Item_sum(session, item),
3111
2998
tmp_table_param(item->tmp_table_param),
3112
2999
warning(item->warning),
3113
3000
separator(item->separator),
3209
3096
copy_fields(tmp_table_param);
3210
3097
copy_funcs(tmp_table_param->items_to_copy);
3212
for (uint i= 0; i < arg_count_field; i++)
3099
for (uint32_t i= 0; i < arg_count_field; i++)
3214
3101
Item *show_item= args[i];
3215
3102
if (!show_item->const_item())
3217
3104
Field *f= show_item->get_tmp_table_field();
3218
if (f->is_null_in_record((const uchar*) table->record[0]))
3105
if (f->is_null_in_record((const unsigned char*) table->record[0]))
3219
3106
return 0; // Skip row if it contains null
3282
3169
result.set_charset(collation.collation);
3283
3170
result_field= 0;
3285
max_length= thd->variables.group_concat_max_len;
3172
max_length= session->variables.group_concat_max_len;
3287
3174
uint32_t offset;
3288
3175
if (separator->needs_conversion(separator->length(), separator->charset(),
3289
3176
collation.collation, &offset))
3291
3178
uint32_t buflen= collation.collation->mbmaxlen * separator->length();
3292
uint errors, conv_length;
3179
uint32_t errors, conv_length;
3294
3181
String *new_separator;
3296
if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
3297
!(new_separator= new(thd->stmt_arena->mem_root)
3183
if (!(buf= (char*) session->alloc(buflen)) ||
3184
!(new_separator= new(session->mem_root)
3298
3185
String(buf, buflen, collation.collation)))
3356
3243
tmp table columns.
3358
3245
if (arg_count_order &&
3359
setup_order(thd, args, context->table_list, list, all_fields, *order))
3246
setup_order(session, args, context->table_list, list, all_fields, *order))
3362
3249
count_field_types(select_lex, tmp_table_param, all_fields, 0);
3381
3268
Note that in the table, we first have the order_st BY fields, then the
3384
if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
3271
if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3385
3272
(order_st*) 0, 0, true,
3386
(select_lex->options | thd->options),
3273
(select_lex->options | session->options),
3387
3274
HA_POS_ERROR, (char*) "")))
3389
3276
table->file->extra(HA_EXTRA_NO_ROWS);
3404
3291
syntax of this function). If there is no order_st BY clause, we don't
3405
3292
create this tree.
3407
init_tree(tree, (uint) min(thd->variables.max_heap_table_size,
3408
thd->variables.sortbuff_size/16), 0,
3294
init_tree(tree, (uint) cmin(session->variables.max_heap_table_size,
3295
session->variables.sortbuff_size/16), 0,
3409
3296
tree_key_length,
3410
3297
group_concat_key_cmp_with_order , 0, NULL, (void*) this);