696
692
fields creations separately.
698
694
switch (args[0]->field_type()) {
699
case MYSQL_TYPE_NEWDATE:
695
case DRIZZLE_TYPE_NEWDATE:
700
696
field= new Field_newdate(maybe_null, name, collation.collation);
702
case MYSQL_TYPE_TIME:
698
case DRIZZLE_TYPE_TIME:
703
699
field= new Field_time(maybe_null, name, collation.collation);
705
case MYSQL_TYPE_TIMESTAMP:
706
case MYSQL_TYPE_DATETIME:
701
case DRIZZLE_TYPE_TIMESTAMP:
702
case DRIZZLE_TYPE_DATETIME:
707
703
field= new Field_datetime(maybe_null, name, collation.collation);
851
847
/***************************************************************************/
855
853
/* Declarations for auxilary C-callbacks */
857
855
static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
859
return memcmp(key1, key2, *(uint *) arg);
857
return memcmp(key1, key2, *(uint32_t *) arg);
863
861
static int item_sum_distinct_walk(void *element,
864
element_count num_of_dups __attribute__((__unused__)),
862
element_count num_of_dups __attribute__((unused)),
867
865
return ((Item_sum_distinct*) (item))->unique_walk_function(element);
872
872
/* Item_sum_distinct */
946
946
calculations. The range of int64 is enough to hold sum 2^32 distinct
947
947
integers each <= 2^32.
949
if (table_field_type >= MYSQL_TYPE_TINY && table_field_type <= MYSQL_TYPE_LONG)
949
if (table_field_type >= DRIZZLE_TYPE_TINY && table_field_type <= DRIZZLE_TYPE_LONG)
951
951
val.traits= Hybrid_type_traits_fast_decimal::instance();
954
table_field_type= MYSQL_TYPE_LONGLONG;
954
table_field_type= DRIZZLE_TYPE_LONGLONG;
955
955
/* fallthrough */
956
956
case DECIMAL_RESULT:
957
957
val.traits= Hybrid_type_traits_decimal::instance();
958
if (table_field_type != MYSQL_TYPE_LONGLONG)
959
table_field_type= MYSQL_TYPE_NEWDECIMAL;
958
if (table_field_type != DRIZZLE_TYPE_LONGLONG)
959
table_field_type= DRIZZLE_TYPE_NEWDECIMAL;
1193
1193
if (hybrid_type == DECIMAL_RESULT)
1195
1195
int precision= args[0]->decimal_precision() + prec_increment;
1196
decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1196
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1197
1197
max_length= my_decimal_precision_to_length(precision, decimals,
1198
1198
unsigned_flag);
1199
f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1199
f_precision= cmin(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1200
1200
f_scale= args[0]->decimals;
1201
1201
dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1204
decimals= min(args[0]->decimals + prec_increment, NOT_FIXED_DEC);
1204
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1205
1205
max_length= args[0]->max_length + prec_increment;
1224
1224
The easiest way is to do this is to store both value in a string
1225
1225
and unpack on access.
1227
field= new Field_string(((hybrid_type == DECIMAL_RESULT) ?
1228
dec_bin_size : sizeof(double)) + sizeof(int64_t),
1229
0, name, &my_charset_bin);
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);
1231
1231
else if (hybrid_type == DECIMAL_RESULT)
1232
1232
field= new Field_new_decimal(max_length, maybe_null, name,
1392
1392
switch (args[0]->result_type()) {
1393
1393
case REAL_RESULT:
1394
1394
case STRING_RESULT:
1395
decimals= min(args[0]->decimals + 4, NOT_FIXED_DEC);
1395
decimals= cmin(args[0]->decimals + 4, NOT_FIXED_DEC);
1397
1397
case INT_RESULT:
1398
1398
case DECIMAL_RESULT:
1400
1400
int precision= args[0]->decimal_precision()*2 + prec_increment;
1401
decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1401
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1402
1402
max_length= my_decimal_precision_to_length(precision, decimals,
1403
1403
unsigned_flag);
2445
int composite_key_cmp(void* arg, uchar* key1, uchar* key2)
2443
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2447
2445
Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2448
2446
Field **field = item->table->field;
2449
2447
Field **field_end= field + item->table->s->fields;
2450
uint32 *lengths=item->field_lengths;
2448
uint32_t *lengths=item->field_lengths;
2451
2449
for (; field < field_end; ++field)
2453
2451
Field* f = *field;
2557
2559
tmp_table_param->force_copy_fields= force_copy_fields;
2558
2560
assert(table == 0);
2560
if (!(table= create_tmp_table(thd, tmp_table_param, list, (ORDER*) 0, 1,
2562
if (!(table= create_tmp_table(thd, tmp_table_param, list, (order_st*) 0, 1,
2562
2564
(select_lex->options | thd->options),
2563
2565
HA_POS_ERROR, (char*)"")))
2719
2721
return table->file->stats.records;
2723
/****************************************************************************
2724
** Functions to handle dynamic loadable aggregates
2725
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2726
** Adapted for UDAs by: Andreas F. Bobak <bobak@relog.ch>.
2727
** Rewritten by: Monty.
2728
****************************************************************************/
2732
void Item_udf_sum::clear()
2738
bool Item_udf_sum::add()
2740
udf.add(&null_value);
2744
void Item_udf_sum::cleanup()
2747
udf_handler::cleanup() nicely handles case when we have not
2748
original item but one created by copy_or_same() method.
2751
Item_sum::cleanup();
2755
void Item_udf_sum::print(String *str, enum_query_type query_type)
2757
str->append(func_name());
2759
for (uint i=0 ; i < arg_count ; i++)
2763
args[i]->print(str, query_type);
2769
Item *Item_sum_udf_float::copy_or_same(THD* thd)
2771
return new (thd->mem_root) Item_sum_udf_float(thd, this);
2774
double Item_sum_udf_float::val_real()
2777
return(udf.val(&null_value));
2781
String *Item_sum_udf_float::val_str(String *str)
2783
return val_string_from_real(str);
2787
my_decimal *Item_sum_udf_float::val_decimal(my_decimal *dec)
2789
return val_decimal_from_real(dec);
2793
String *Item_sum_udf_decimal::val_str(String *str)
2795
return val_string_from_decimal(str);
2799
double Item_sum_udf_decimal::val_real()
2801
return val_real_from_decimal();
2805
int64_t Item_sum_udf_decimal::val_int()
2807
return val_int_from_decimal();
2811
my_decimal *Item_sum_udf_decimal::val_decimal(my_decimal *dec_buf)
2814
return(udf.val_decimal(&null_value, dec_buf));
2818
Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
2820
return new (thd->mem_root) Item_sum_udf_decimal(thd, this);
2824
Item *Item_sum_udf_int::copy_or_same(THD* thd)
2826
return new (thd->mem_root) Item_sum_udf_int(thd, this);
2829
int64_t Item_sum_udf_int::val_int()
2832
return(udf.val_int(&null_value));
2836
String *Item_sum_udf_int::val_str(String *str)
2838
return val_string_from_int(str);
2841
my_decimal *Item_sum_udf_int::val_decimal(my_decimal *dec)
2843
return val_decimal_from_int(dec);
2847
/** Default max_length is max argument length. */
2849
void Item_sum_udf_str::fix_length_and_dec()
2852
for (uint i = 0; i < arg_count; i++)
2853
set_if_bigger(max_length,args[i]->max_length);
2858
Item *Item_sum_udf_str::copy_or_same(THD* thd)
2860
return new (thd->mem_root) Item_sum_udf_str(thd, this);
2864
my_decimal *Item_sum_udf_str::val_decimal(my_decimal *dec)
2866
return val_decimal_from_string(dec);
2869
String *Item_sum_udf_str::val_str(String *str)
2872
String *res=udf.val_str(str,&str_value);
2877
#endif /* HAVE_DLOPEN */
2880
2724
/*****************************************************************************
2881
2725
GROUP_CONCAT function
2884
GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
2728
GROUP_CONCAT([DISTINCT] expr,... [order_st BY col [ASC|DESC],...]
2885
2729
[SEPARATOR str_const])
2887
2731
concat of values from "group by" operation
2890
Blobs doesn't work with DISTINCT or ORDER BY
2734
Blobs doesn't work with DISTINCT or order_st BY
2891
2735
*****************************************************************************/
2940
function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
2784
function of sort for syntax: GROUP_CONCAT(expr,... order_st BY col,... )
2943
2787
int group_concat_key_cmp_with_order(void* arg, const void* key1,
2944
2788
const void* key2)
2946
2790
Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2947
ORDER **order_item, **end;
2948
TABLE *table= grp_item->table;
2791
order_st **order_item, **end;
2792
Table *table= grp_item->table;
2950
2794
for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2951
2795
order_item < end;
2984
2828
Append data from current leaf to item->result.
2987
int dump_leaf_key(uchar* key, element_count count __attribute__((unused)),
2831
int dump_leaf_key(unsigned char* key, element_count count __attribute__((unused)),
2988
2832
Item_func_group_concat *item)
2990
TABLE *table= item->table;
2834
Table *table= item->table;
2991
2835
String tmp((char *)table->record[1], table->s->reclength,
2992
2836
default_charset_info);
2994
2838
String *result= &item->result;
2995
2839
Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
2996
uint old_length= result->length();
2840
uint32_t old_length= result->length();
2998
2842
if (item->no_appended)
2999
2843
item->no_appended= false;
3218
3062
copy_fields(tmp_table_param);
3219
3063
copy_funcs(tmp_table_param->items_to_copy);
3221
for (uint i= 0; i < arg_count_field; i++)
3065
for (uint32_t i= 0; i < arg_count_field; i++)
3223
3067
Item *show_item= args[i];
3224
3068
if (!show_item->const_item())
3226
3070
Field *f= show_item->get_tmp_table_field();
3227
if (f->is_null_in_record((const uchar*) table->record[0]))
3071
if (f->is_null_in_record((const unsigned char*) table->record[0]))
3228
3072
return 0; // Skip row if it contains null
3294
3138
max_length= thd->variables.group_concat_max_len;
3297
3141
if (separator->needs_conversion(separator->length(), separator->charset(),
3298
3142
collation.collation, &offset))
3300
uint32 buflen= collation.collation->mbmaxlen * separator->length();
3301
uint errors, conv_length;
3144
uint32_t buflen= collation.collation->mbmaxlen * separator->length();
3145
uint32_t errors, conv_length;
3303
3147
String *new_separator;
3305
if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
3306
!(new_separator= new(thd->stmt_arena->mem_root)
3149
if (!(buf= (char*) thd->alloc(buflen)) ||
3150
!(new_separator= new(thd->mem_root)
3307
3151
String(buf, buflen, collation.collation)))
3387
3231
We have to create a temporary table to get descriptions of fields
3388
3232
(types, sizes and so on).
3390
Note that in the table, we first have the ORDER BY fields, then the
3234
Note that in the table, we first have the order_st BY fields, then the
3393
3237
if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
3394
(ORDER*) 0, 0, true,
3238
(order_st*) 0, 0, true,
3395
3239
(select_lex->options | thd->options),
3396
3240
HA_POS_ERROR, (char*) "")))
3403
3247
Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3404
3248
the row is not added to the result.
3406
uint tree_key_length= table->s->reclength - table->s->null_bytes;
3250
uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
3408
3252
if (arg_count_order)
3410
3254
tree= &tree_base;
3412
3256
Create a tree for sorting. The tree is used to sort (according to the
3413
syntax of this function). If there is no ORDER BY clause, we don't
3257
syntax of this function). If there is no order_st BY clause, we don't
3414
3258
create this tree.
3416
init_tree(tree, (uint) min(thd->variables.max_heap_table_size,
3260
init_tree(tree, (uint) cmin(thd->variables.max_heap_table_size,
3417
3261
thd->variables.sortbuff_size/16), 0,
3418
3262
tree_key_length,
3419
3263
group_concat_key_cmp_with_order , 0, NULL, (void*) this);