692
696
fields creations separately.
694
698
switch (args[0]->field_type()) {
695
case DRIZZLE_TYPE_NEWDATE:
699
case MYSQL_TYPE_NEWDATE:
696
700
field= new Field_newdate(maybe_null, name, collation.collation);
698
case DRIZZLE_TYPE_TIME:
702
case MYSQL_TYPE_TIME:
699
703
field= new Field_time(maybe_null, name, collation.collation);
701
case DRIZZLE_TYPE_TIMESTAMP:
702
case DRIZZLE_TYPE_DATETIME:
705
case MYSQL_TYPE_TIMESTAMP:
706
case MYSQL_TYPE_DATETIME:
703
707
field= new Field_datetime(maybe_null, name, collation.collation);
847
851
/***************************************************************************/
853
855
/* Declarations for auxilary C-callbacks */
855
857
static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
857
return memcmp(key1, key2, *(uint32_t *) arg);
859
return memcmp(key1, key2, *(uint *) arg);
861
863
static int item_sum_distinct_walk(void *element,
862
element_count num_of_dups __attribute__((unused)),
864
element_count num_of_dups __attribute__((__unused__)),
865
867
return ((Item_sum_distinct*) (item))->unique_walk_function(element);
872
872
/* Item_sum_distinct */
946
947
calculations. The range of int64 is enough to hold sum 2^32 distinct
947
948
integers each <= 2^32.
949
if (table_field_type >= DRIZZLE_TYPE_TINY && table_field_type <= DRIZZLE_TYPE_LONG)
950
if (table_field_type >= MYSQL_TYPE_TINY && table_field_type <= MYSQL_TYPE_LONG)
951
952
val.traits= Hybrid_type_traits_fast_decimal::instance();
954
table_field_type= DRIZZLE_TYPE_LONGLONG;
955
table_field_type= MYSQL_TYPE_LONGLONG;
955
956
/* fallthrough */
956
957
case DECIMAL_RESULT:
957
958
val.traits= Hybrid_type_traits_decimal::instance();
958
if (table_field_type != DRIZZLE_TYPE_LONGLONG)
959
table_field_type= DRIZZLE_TYPE_NEWDECIMAL;
959
if (table_field_type != MYSQL_TYPE_LONGLONG)
960
table_field_type= MYSQL_TYPE_NEWDECIMAL;
1193
1194
if (hybrid_type == DECIMAL_RESULT)
1195
1196
int precision= args[0]->decimal_precision() + prec_increment;
1196
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1197
decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1197
1198
max_length= my_decimal_precision_to_length(precision, decimals,
1198
1199
unsigned_flag);
1199
f_precision= cmin(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1200
f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1200
1201
f_scale= args[0]->decimals;
1201
1202
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);
1205
decimals= min(args[0]->decimals + prec_increment, NOT_FIXED_DEC);
1205
1206
max_length= args[0]->max_length + prec_increment;
1224
1225
The easiest way is to do this is to store both value in a string
1225
1226
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);
1228
field= new Field_string(((hybrid_type == DECIMAL_RESULT) ?
1229
dec_bin_size : sizeof(double)) + sizeof(int64_t),
1230
0, name, &my_charset_bin);
1231
1232
else if (hybrid_type == DECIMAL_RESULT)
1232
1233
field= new Field_new_decimal(max_length, maybe_null, name,
1392
1393
switch (args[0]->result_type()) {
1393
1394
case REAL_RESULT:
1394
1395
case STRING_RESULT:
1395
decimals= cmin(args[0]->decimals + 4, NOT_FIXED_DEC);
1396
decimals= min(args[0]->decimals + 4, NOT_FIXED_DEC);
1397
1398
case INT_RESULT:
1398
1399
case DECIMAL_RESULT:
1400
1401
int precision= args[0]->decimal_precision()*2 + prec_increment;
1401
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1402
decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1402
1403
max_length= my_decimal_precision_to_length(precision, decimals,
1403
1404
unsigned_flag);
2445
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2446
int composite_key_cmp(void* arg, uchar* key1, uchar* key2)
2447
2448
Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2448
2449
Field **field = item->table->field;
2449
2450
Field **field_end= field + item->table->s->fields;
2450
uint32_t *lengths=item->field_lengths;
2451
uint32 *lengths=item->field_lengths;
2451
2452
for (; field < field_end; ++field)
2453
2454
Field* f = *field;
2561
2558
tmp_table_param->force_copy_fields= force_copy_fields;
2562
2559
assert(table == 0);
2564
if (!(table= create_tmp_table(thd, tmp_table_param, list, (order_st*) 0, 1,
2561
if (!(table= create_tmp_table(thd, tmp_table_param, list, (ORDER*) 0, 1,
2566
2563
(select_lex->options | thd->options),
2567
2564
HA_POS_ERROR, (char*)"")))
2723
2720
return table->file->stats.records;
2724
/****************************************************************************
2725
** Functions to handle dynamic loadable aggregates
2726
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2727
** Adapted for UDAs by: Andreas F. Bobak <bobak@relog.ch>.
2728
** Rewritten by: Monty.
2729
****************************************************************************/
2733
void Item_udf_sum::clear()
2739
bool Item_udf_sum::add()
2741
udf.add(&null_value);
2745
void Item_udf_sum::cleanup()
2748
udf_handler::cleanup() nicely handles case when we have not
2749
original item but one created by copy_or_same() method.
2752
Item_sum::cleanup();
2756
void Item_udf_sum::print(String *str, enum_query_type query_type)
2758
str->append(func_name());
2760
for (uint i=0 ; i < arg_count ; i++)
2764
args[i]->print(str, query_type);
2770
Item *Item_sum_udf_float::copy_or_same(THD* thd)
2772
return new (thd->mem_root) Item_sum_udf_float(thd, this);
2775
double Item_sum_udf_float::val_real()
2778
return(udf.val(&null_value));
2782
String *Item_sum_udf_float::val_str(String *str)
2784
return val_string_from_real(str);
2788
my_decimal *Item_sum_udf_float::val_decimal(my_decimal *dec)
2790
return val_decimal_from_real(dec);
2794
String *Item_sum_udf_decimal::val_str(String *str)
2796
return val_string_from_decimal(str);
2800
double Item_sum_udf_decimal::val_real()
2802
return val_real_from_decimal();
2806
int64_t Item_sum_udf_decimal::val_int()
2808
return val_int_from_decimal();
2812
my_decimal *Item_sum_udf_decimal::val_decimal(my_decimal *dec_buf)
2815
return(udf.val_decimal(&null_value, dec_buf));
2819
Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
2821
return new (thd->mem_root) Item_sum_udf_decimal(thd, this);
2825
Item *Item_sum_udf_int::copy_or_same(THD* thd)
2827
return new (thd->mem_root) Item_sum_udf_int(thd, this);
2830
int64_t Item_sum_udf_int::val_int()
2833
return(udf.val_int(&null_value));
2837
String *Item_sum_udf_int::val_str(String *str)
2839
return val_string_from_int(str);
2842
my_decimal *Item_sum_udf_int::val_decimal(my_decimal *dec)
2844
return val_decimal_from_int(dec);
2848
/** Default max_length is max argument length. */
2850
void Item_sum_udf_str::fix_length_and_dec()
2853
for (uint i = 0; i < arg_count; i++)
2854
set_if_bigger(max_length,args[i]->max_length);
2859
Item *Item_sum_udf_str::copy_or_same(THD* thd)
2861
return new (thd->mem_root) Item_sum_udf_str(thd, this);
2865
my_decimal *Item_sum_udf_str::val_decimal(my_decimal *dec)
2867
return val_decimal_from_string(dec);
2870
String *Item_sum_udf_str::val_str(String *str)
2873
String *res=udf.val_str(str,&str_value);
2878
#endif /* HAVE_DLOPEN */
2726
2881
/*****************************************************************************
2727
2882
GROUP_CONCAT function
2730
GROUP_CONCAT([DISTINCT] expr,... [order_st BY col [ASC|DESC],...]
2885
GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
2731
2886
[SEPARATOR str_const])
2733
2888
concat of values from "group by" operation
2736
Blobs doesn't work with DISTINCT or order_st BY
2891
Blobs doesn't work with DISTINCT or ORDER BY
2737
2892
*****************************************************************************/
2786
function of sort for syntax: GROUP_CONCAT(expr,... order_st BY col,... )
2941
function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
2789
2944
int group_concat_key_cmp_with_order(void* arg, const void* key1,
2790
2945
const void* key2)
2792
2947
Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2793
order_st **order_item, **end;
2794
Table *table= grp_item->table;
2948
ORDER **order_item, **end;
2949
TABLE *table= grp_item->table;
2796
2951
for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2797
2952
order_item < end;
2830
2985
Append data from current leaf to item->result.
2833
int dump_leaf_key(unsigned char* key, element_count count __attribute__((unused)),
2988
int dump_leaf_key(uchar* key, element_count count __attribute__((unused)),
2834
2989
Item_func_group_concat *item)
2836
Table *table= item->table;
2991
TABLE *table= item->table;
2837
2992
String tmp((char *)table->record[1], table->s->reclength,
2838
2993
default_charset_info);
2840
2995
String *result= &item->result;
2841
2996
Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
2842
uint32_t old_length= result->length();
2997
uint old_length= result->length();
2844
2999
if (item->no_appended)
2845
3000
item->no_appended= false;
3064
3219
copy_fields(tmp_table_param);
3065
3220
copy_funcs(tmp_table_param->items_to_copy);
3067
for (uint32_t i= 0; i < arg_count_field; i++)
3222
for (uint i= 0; i < arg_count_field; i++)
3069
3224
Item *show_item= args[i];
3070
3225
if (!show_item->const_item())
3072
3227
Field *f= show_item->get_tmp_table_field();
3073
if (f->is_null_in_record((const unsigned char*) table->record[0]))
3228
if (f->is_null_in_record((const uchar*) table->record[0]))
3074
3229
return 0; // Skip row if it contains null
3140
3295
max_length= thd->variables.group_concat_max_len;
3143
3298
if (separator->needs_conversion(separator->length(), separator->charset(),
3144
3299
collation.collation, &offset))
3146
uint32_t buflen= collation.collation->mbmaxlen * separator->length();
3147
uint32_t errors, conv_length;
3301
uint32 buflen= collation.collation->mbmaxlen * separator->length();
3302
uint errors, conv_length;
3149
3304
String *new_separator;
3151
if (!(buf= (char*) thd->alloc(buflen)) ||
3152
!(new_separator= new(thd->mem_root)
3306
if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
3307
!(new_separator= new(thd->stmt_arena->mem_root)
3153
3308
String(buf, buflen, collation.collation)))
3233
3388
We have to create a temporary table to get descriptions of fields
3234
3389
(types, sizes and so on).
3236
Note that in the table, we first have the order_st BY fields, then the
3391
Note that in the table, we first have the ORDER BY fields, then the
3239
3394
if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
3240
(order_st*) 0, 0, true,
3395
(ORDER*) 0, 0, true,
3241
3396
(select_lex->options | thd->options),
3242
3397
HA_POS_ERROR, (char*) "")))
3249
3404
Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3250
3405
the row is not added to the result.
3252
uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
3407
uint tree_key_length= table->s->reclength - table->s->null_bytes;
3254
3409
if (arg_count_order)
3256
3411
tree= &tree_base;
3258
3413
Create a tree for sorting. The tree is used to sort (according to the
3259
syntax of this function). If there is no order_st BY clause, we don't
3414
syntax of this function). If there is no ORDER BY clause, we don't
3260
3415
create this tree.
3262
init_tree(tree, (uint) cmin(thd->variables.max_heap_table_size,
3417
init_tree(tree, (uint) min(thd->variables.max_heap_table_size,
3263
3418
thd->variables.sortbuff_size/16), 0,
3264
3419
tree_key_length,
3265
3420
group_concat_key_cmp_with_order , 0, NULL, (void*) this);