480
Field *Item_sum::create_tmp_field(bool group __attribute__((unused)),
482
uint32_t convert_blob_length)
484
Field *Item_sum::create_tmp_field(bool group, TABLE *table,
485
uint convert_blob_length)
485
488
switch (result_type()) {
486
489
case REAL_RESULT:
487
field= new Field_double(max_length, maybe_null, name, decimals, true);
490
field= new Field_double(max_length, maybe_null, name, decimals, TRUE);
490
field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
493
field= new Field_longlong(max_length, maybe_null, name, unsigned_flag);
492
495
case STRING_RESULT:
493
496
if (max_length/collation.collation->mbmaxlen <= 255 ||
847
860
/***************************************************************************/
853
864
/* Declarations for auxilary C-callbacks */
855
866
static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
857
return memcmp(key1, key2, *(uint32_t *) arg);
868
return memcmp(key1, key2, *(uint *) arg);
861
static int item_sum_distinct_walk(void *element,
862
element_count num_of_dups __attribute__((unused)),
872
static int item_sum_distinct_walk(void *element, element_count num_of_dups,
865
875
return ((Item_sum_distinct*) (item))->unique_walk_function(element);
872
880
/* Item_sum_distinct */
936
944
case STRING_RESULT:
937
945
case REAL_RESULT:
938
946
val.traits= Hybrid_type_traits::instance();
939
table_field_type= DRIZZLE_TYPE_DOUBLE;
947
if (table_field_type != MYSQL_TYPE_FLOAT)
948
table_field_type= MYSQL_TYPE_DOUBLE;
943
952
Preserving int8, int16, int32 field types gives ~10% performance boost
944
953
as the size of result tree becomes significantly smaller.
945
Another speed up we gain by using int64_t for intermediate
954
Another speed up we gain by using longlong for intermediate
946
955
calculations. The range of int64 is enough to hold sum 2^32 distinct
947
956
integers each <= 2^32.
949
if (table_field_type >= DRIZZLE_TYPE_TINY && table_field_type <= DRIZZLE_TYPE_LONG)
958
if (table_field_type == MYSQL_TYPE_INT24 ||
959
(table_field_type >= MYSQL_TYPE_TINY && table_field_type <= MYSQL_TYPE_LONG))
951
961
val.traits= Hybrid_type_traits_fast_decimal::instance();
954
table_field_type= DRIZZLE_TYPE_LONGLONG;
964
table_field_type= MYSQL_TYPE_LONGLONG;
955
965
/* fallthrough */
956
966
case DECIMAL_RESULT:
957
967
val.traits= Hybrid_type_traits_decimal::instance();
958
if (table_field_type != DRIZZLE_TYPE_LONGLONG)
959
table_field_type= DRIZZLE_TYPE_NEWDECIMAL;
968
if (table_field_type != MYSQL_TYPE_LONGLONG)
969
table_field_type= MYSQL_TYPE_NEWDECIMAL;
965
975
val.traits->fix_length_and_dec(this, args[0]);
1193
1206
if (hybrid_type == DECIMAL_RESULT)
1195
1208
int precision= args[0]->decimal_precision() + prec_increment;
1196
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1209
decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1197
1210
max_length= my_decimal_precision_to_length(precision, decimals,
1198
1211
unsigned_flag);
1199
f_precision= cmin(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1212
f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1200
1213
f_scale= args[0]->decimals;
1201
1214
dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1204
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1217
decimals= min(args[0]->decimals + prec_increment, NOT_FIXED_DEC);
1205
1218
max_length= args[0]->max_length + prec_increment;
1224
1237
The easiest way is to do this is to store both value in a string
1225
1238
and unpack on access.
1227
field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
1228
dec_bin_size : sizeof(double)) + sizeof(int64_t),
1229
0, name, table->s, &my_charset_bin);
1240
field= new Field_string(((hybrid_type == DECIMAL_RESULT) ?
1241
dec_bin_size : sizeof(double)) + sizeof(longlong),
1242
0, name, &my_charset_bin);
1231
1244
else if (hybrid_type == DECIMAL_RESULT)
1232
1245
field= new Field_new_decimal(max_length, maybe_null, name,
1233
1246
decimals, unsigned_flag);
1235
field= new Field_double(max_length, maybe_null, name, decimals, true);
1248
field= new Field_double(max_length, maybe_null, name, decimals, TRUE);
1237
1250
field->init(table);
2537
2550
Setup can be called twice for ROLLUP items. This is a bug.
2538
Please add assert(tree == 0) here when it's fixed.
2551
Please add DBUG_ASSERT(tree == 0) here when it's fixed.
2539
2552
It's legal to call setup() more than once when in a subquery
2541
2554
if (tree || table || tmp_table_param)
2544
2557
if (!(tmp_table_param= new TMP_TABLE_PARAM))
2547
2560
/* Create a table with an unique key over all parameters */
2548
for (uint32_t i=0; i < arg_count ; i++)
2561
for (uint i=0; i < arg_count ; i++)
2550
2563
Item *item=args[i];
2551
2564
if (list.push_back(item))
2552
return true; // End of memory
2565
return TRUE; // End of memory
2553
2566
if (item->const_item() && item->is_null())
2554
2567
always_null= 1;
2556
2569
if (always_null)
2558
2571
count_field_types(select_lex, tmp_table_param, list, 0);
2559
2572
tmp_table_param->force_copy_fields= force_copy_fields;
2573
DBUG_ASSERT(table == 0);
2575
Make create_tmp_table() convert BIT columns to BIGINT.
2576
This is needed because BIT fields store parts of their data in table's
2577
null bits, and we don't have methods to compare two table records, which
2578
is needed by Unique which is used when HEAP table is used.
2581
List_iterator_fast<Item> li(list);
2583
while ((item= li++))
2585
if (item->type() == Item::FIELD_ITEM &&
2586
((Item_field*)item)->field->type() == FIELD_TYPE_BIT)
2562
if (!(table= create_tmp_table(thd, tmp_table_param, list, (order_st*) 0, 1,
2591
if (!(table= create_tmp_table(thd, tmp_table_param, list, (ORDER*) 0, 1,
2564
2593
(select_lex->options | thd->options),
2565
2594
HA_POS_ERROR, (char*)"")))
2567
2596
table->file->extra(HA_EXTRA_NO_ROWS); // Don't update rows
2568
2597
table->no_rows=1;
2721
2750
return table->file->stats.records;
2754
/****************************************************************************
2755
** Functions to handle dynamic loadable aggregates
2756
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2757
** Adapted for UDAs by: Andreas F. Bobak <bobak@relog.ch>.
2758
** Rewritten by: Monty.
2759
****************************************************************************/
2763
void Item_udf_sum::clear()
2765
DBUG_ENTER("Item_udf_sum::clear");
2770
bool Item_udf_sum::add()
2772
DBUG_ENTER("Item_udf_sum::add");
2773
udf.add(&null_value);
2777
void Item_udf_sum::cleanup()
2780
udf_handler::cleanup() nicely handles case when we have not
2781
original item but one created by copy_or_same() method.
2784
Item_sum::cleanup();
2788
void Item_udf_sum::print(String *str, enum_query_type query_type)
2790
str->append(func_name());
2792
for (uint i=0 ; i < arg_count ; i++)
2796
args[i]->print(str, query_type);
2802
Item *Item_sum_udf_float::copy_or_same(THD* thd)
2804
return new (thd->mem_root) Item_sum_udf_float(thd, this);
2807
double Item_sum_udf_float::val_real()
2809
DBUG_ASSERT(fixed == 1);
2810
DBUG_ENTER("Item_sum_udf_float::val");
2811
DBUG_PRINT("info",("result_type: %d arg_count: %d",
2812
args[0]->result_type(), arg_count));
2813
DBUG_RETURN(udf.val(&null_value));
2817
String *Item_sum_udf_float::val_str(String *str)
2819
return val_string_from_real(str);
2823
my_decimal *Item_sum_udf_float::val_decimal(my_decimal *dec)
2825
return val_decimal_from_real(dec);
2829
String *Item_sum_udf_decimal::val_str(String *str)
2831
return val_string_from_decimal(str);
2835
double Item_sum_udf_decimal::val_real()
2837
return val_real_from_decimal();
2841
longlong Item_sum_udf_decimal::val_int()
2843
return val_int_from_decimal();
2847
my_decimal *Item_sum_udf_decimal::val_decimal(my_decimal *dec_buf)
2849
DBUG_ASSERT(fixed == 1);
2850
DBUG_ENTER("Item_func_udf_decimal::val_decimal");
2851
DBUG_PRINT("info",("result_type: %d arg_count: %d",
2852
args[0]->result_type(), arg_count));
2854
DBUG_RETURN(udf.val_decimal(&null_value, dec_buf));
2858
Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
2860
return new (thd->mem_root) Item_sum_udf_decimal(thd, this);
2864
Item *Item_sum_udf_int::copy_or_same(THD* thd)
2866
return new (thd->mem_root) Item_sum_udf_int(thd, this);
2869
longlong Item_sum_udf_int::val_int()
2871
DBUG_ASSERT(fixed == 1);
2872
DBUG_ENTER("Item_sum_udf_int::val_int");
2873
DBUG_PRINT("info",("result_type: %d arg_count: %d",
2874
args[0]->result_type(), arg_count));
2875
DBUG_RETURN(udf.val_int(&null_value));
2879
String *Item_sum_udf_int::val_str(String *str)
2881
return val_string_from_int(str);
2884
my_decimal *Item_sum_udf_int::val_decimal(my_decimal *dec)
2886
return val_decimal_from_int(dec);
2890
/** Default max_length is max argument length. */
2892
void Item_sum_udf_str::fix_length_and_dec()
2894
DBUG_ENTER("Item_sum_udf_str::fix_length_and_dec");
2896
for (uint i = 0; i < arg_count; i++)
2897
set_if_bigger(max_length,args[i]->max_length);
2902
Item *Item_sum_udf_str::copy_or_same(THD* thd)
2904
return new (thd->mem_root) Item_sum_udf_str(thd, this);
2908
my_decimal *Item_sum_udf_str::val_decimal(my_decimal *dec)
2910
return val_decimal_from_string(dec);
2913
String *Item_sum_udf_str::val_str(String *str)
2915
DBUG_ASSERT(fixed == 1);
2916
DBUG_ENTER("Item_sum_udf_str::str");
2917
String *res=udf.val_str(str,&str_value);
2922
#endif /* HAVE_DLOPEN */
2724
2925
/*****************************************************************************
2725
2926
GROUP_CONCAT function
2728
GROUP_CONCAT([DISTINCT] expr,... [order_st BY col [ASC|DESC],...]
2929
GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
2729
2930
[SEPARATOR str_const])
2731
2932
concat of values from "group by" operation
2734
Blobs doesn't work with DISTINCT or order_st BY
2935
Blobs doesn't work with DISTINCT or ORDER BY
2735
2936
*****************************************************************************/
2784
function of sort for syntax: GROUP_CONCAT(expr,... order_st BY col,... )
2985
function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
2787
2988
int group_concat_key_cmp_with_order(void* arg, const void* key1,
2788
2989
const void* key2)
2790
2991
Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2791
order_st **order_item, **end;
2792
Table *table= grp_item->table;
2992
ORDER **order_item, **end;
2993
TABLE *table= grp_item->table;
2794
2995
for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2795
2996
order_item < end;
2828
3029
Append data from current leaf to item->result.
2831
int dump_leaf_key(unsigned char* key, element_count count __attribute__((unused)),
3032
int dump_leaf_key(uchar* key, element_count count __attribute__((unused)),
2832
3033
Item_func_group_concat *item)
2834
Table *table= item->table;
3035
TABLE *table= item->table;
2835
3036
String tmp((char *)table->record[1], table->s->reclength,
2836
3037
default_charset_info);
2838
3039
String *result= &item->result;
2839
3040
Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
2840
uint32_t old_length= result->length();
3041
uint old_length= result->length();
2842
3043
if (item->no_appended)
2843
item->no_appended= false;
3044
item->no_appended= FALSE;
2845
3046
result->append(*item->separator);
3062
3264
copy_fields(tmp_table_param);
3063
3265
copy_funcs(tmp_table_param->items_to_copy);
3065
for (uint32_t i= 0; i < arg_count_field; i++)
3267
for (uint i= 0; i < arg_count_field; i++)
3067
3269
Item *show_item= args[i];
3068
3270
if (!show_item->const_item())
3070
3272
Field *f= show_item->get_tmp_table_field();
3071
if (f->is_null_in_record((const unsigned char*) table->record[0]))
3273
if (f->is_null_in_record((const uchar*) table->record[0]))
3072
3274
return 0; // Skip row if it contains null
3077
bool row_eligible= true;
3279
bool row_eligible= TRUE;
3081
3283
/* Filter out duplicate rows. */
3082
uint32_t count= unique_filter->elements_in_tree();
3284
uint count= unique_filter->elements_in_tree();
3083
3285
unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3084
3286
if (count == unique_filter->elements_in_tree())
3085
row_eligible= false;
3287
row_eligible= FALSE;
3088
3290
TREE_ELEMENT *el= 0; // Only for safety
3138
3340
max_length= thd->variables.group_concat_max_len;
3141
3343
if (separator->needs_conversion(separator->length(), separator->charset(),
3142
3344
collation.collation, &offset))
3144
uint32_t buflen= collation.collation->mbmaxlen * separator->length();
3145
uint32_t errors, conv_length;
3346
uint32 buflen= collation.collation->mbmaxlen * separator->length();
3347
uint errors, conv_length;
3147
3349
String *new_separator;
3149
if (!(buf= (char*) thd->alloc(buflen)) ||
3150
!(new_separator= new(thd->mem_root)
3351
if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
3352
!(new_separator= new(thd->stmt_arena->mem_root)
3151
3353
String(buf, buflen, collation.collation)))
3154
3356
conv_length= copy_and_convert(buf, buflen, collation.collation,
3155
3357
separator->ptr(), separator->length(),
3171
3373
List<Item> list;
3172
3374
SELECT_LEX *select_lex= thd->lex->current_select;
3375
DBUG_ENTER("Item_func_group_concat::setup");
3175
3378
Currently setup() can be called twice. Please add
3176
3379
assertion here when this is fixed.
3178
3381
if (table || tree)
3181
3384
if (!(tmp_table_param= new TMP_TABLE_PARAM))
3184
3387
/* We'll convert all blobs to varchar fields in the temporary table */
3185
3388
tmp_table_param->convert_blob_length= max_length *
3186
3389
collation.collation->mbmaxlen;
3187
3390
/* Push all not constant fields to the list and create a temp table */
3188
3391
always_null= 0;
3189
for (uint32_t i= 0; i < arg_count_field; i++)
3392
for (uint i= 0; i < arg_count_field; i++)
3191
3394
Item *item= args[i];
3192
3395
if (list.push_back(item))
3194
3397
if (item->const_item())
3196
3399
if (item->is_null())
3198
3401
always_null= 1;
3204
3407
List<Item> all_fields(list);
3206
Try to find every order_st expression in the list of GROUP_CONCAT
3409
Try to find every ORDER expression in the list of GROUP_CONCAT
3207
3410
arguments. If an expression is not found, prepend it to
3208
3411
"all_fields". The resulting field list is used as input to create
3209
3412
tmp table columns.
3211
3414
if (arg_count_order &&
3212
3415
setup_order(thd, args, context->table_list, list, all_fields, *order))
3215
3418
count_field_types(select_lex, tmp_table_param, all_fields, 0);
3216
3419
tmp_table_param->force_copy_fields= force_copy_fields;
3420
DBUG_ASSERT(table == 0);
3218
3421
if (arg_count_order > 0 || distinct)
3221
3424
Currently we have to force conversion of BLOB values to VARCHAR's
3222
if we are to store them in TREE objects used for order_st BY and
3425
if we are to store them in TREE objects used for ORDER BY and
3223
3426
DISTINCT. This leads to truncation if the BLOB's size exceeds
3224
3427
Field_varstring::MAX_SIZE.
3226
3429
set_if_smaller(tmp_table_param->convert_blob_length,
3227
3430
Field_varstring::MAX_SIZE);
3433
Force the create_tmp_table() to convert BIT columns to INT
3434
as we cannot compare two table records containg BIT fields
3435
stored in the the tree used for distinct/order by.
3436
Moreover we don't even save in the tree record null bits
3437
where BIT fields store parts of their data.
3439
List_iterator_fast<Item> li(all_fields);
3441
while ((item= li++))
3443
if (item->type() == Item::FIELD_ITEM &&
3444
((Item_field*) item)->field->type() == FIELD_TYPE_BIT)
3231
3450
We have to create a temporary table to get descriptions of fields
3232
3451
(types, sizes and so on).
3234
Note that in the table, we first have the order_st BY fields, then the
3453
Note that in the table, we first have the ORDER BY fields, then the
3237
3456
if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
3238
(order_st*) 0, 0, true,
3457
(ORDER*) 0, 0, TRUE,
3239
3458
(select_lex->options | thd->options),
3240
3459
HA_POS_ERROR, (char*) "")))
3242
3461
table->file->extra(HA_EXTRA_NO_ROWS);
3243
3462
table->no_rows= 1;
3247
3466
Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3248
3467
the row is not added to the result.
3250
uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
3469
uint tree_key_length= table->s->reclength - table->s->null_bytes;
3252
3471
if (arg_count_order)
3254
3473
tree= &tree_base;
3256
3475
Create a tree for sorting. The tree is used to sort (according to the
3257
syntax of this function). If there is no order_st BY clause, we don't
3476
syntax of this function). If there is no ORDER BY clause, we don't
3258
3477
create this tree.
3260
init_tree(tree, (uint) cmin(thd->variables.max_heap_table_size,
3479
init_tree(tree, (uint) min(thd->variables.max_heap_table_size,
3261
3480
thd->variables.sortbuff_size/16), 0,
3262
3481
tree_key_length,
3263
3482
group_concat_key_cmp_with_order , 0, NULL, (void*) this);