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
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 >= DRIZZLE_TYPE_TINY && table_field_type <= DRIZZLE_TYPE_LONG)
949
if (table_field_type >= MYSQL_TYPE_TINY && table_field_type <= MYSQL_TYPE_LONG)
951
951
val.traits= Hybrid_type_traits_fast_decimal::instance();
954
table_field_type= DRIZZLE_TYPE_LONGLONG;
954
table_field_type= MYSQL_TYPE_LONGLONG;
955
955
/* fallthrough */
956
956
case DECIMAL_RESULT:
957
957
val.traits= Hybrid_type_traits_decimal::instance();
958
if (table_field_type != DRIZZLE_TYPE_LONGLONG)
959
table_field_type= DRIZZLE_TYPE_NEWDECIMAL;
958
if (table_field_type != MYSQL_TYPE_LONGLONG)
959
table_field_type= MYSQL_TYPE_NEWDECIMAL;
1193
1193
if (hybrid_type == DECIMAL_RESULT)
1195
1195
int precision= args[0]->decimal_precision() + prec_increment;
1196
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1196
decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1197
1197
max_length= my_decimal_precision_to_length(precision, decimals,
1198
1198
unsigned_flag);
1199
f_precision= cmin(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1199
f_precision= min(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= cmin(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1204
decimals= min(args[0]->decimals + prec_increment, 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_varstring(((hybrid_type == DECIMAL_RESULT) ?
1228
dec_bin_size : sizeof(double)) + sizeof(int64_t),
1229
0, name, table->s, &my_charset_bin);
1227
field= new Field_string(((hybrid_type == DECIMAL_RESULT) ?
1228
dec_bin_size : sizeof(double)) + sizeof(int64_t),
1229
0, name, &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= cmin(args[0]->decimals + 4, NOT_FIXED_DEC);
1395
decimals= min(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= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1401
decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1402
1402
max_length= my_decimal_precision_to_length(precision, decimals,
1403
1403
unsigned_flag);
2443
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2445
int composite_key_cmp(void* arg, uchar* key1, uchar* key2)
2445
2447
Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2446
2448
Field **field = item->table->field;
2447
2449
Field **field_end= field + item->table->s->fields;
2448
uint32_t *lengths=item->field_lengths;
2450
uint32 *lengths=item->field_lengths;
2449
2451
for (; field < field_end; ++field)
2451
2453
Field* f = *field;
2559
2557
tmp_table_param->force_copy_fields= force_copy_fields;
2560
2558
assert(table == 0);
2562
if (!(table= create_tmp_table(thd, tmp_table_param, list, (order_st*) 0, 1,
2560
if (!(table= create_tmp_table(thd, tmp_table_param, list, (ORDER*) 0, 1,
2564
2562
(select_lex->options | thd->options),
2565
2563
HA_POS_ERROR, (char*)"")))
2721
2719
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 */
2724
2880
/*****************************************************************************
2725
2881
GROUP_CONCAT function
2728
GROUP_CONCAT([DISTINCT] expr,... [order_st BY col [ASC|DESC],...]
2884
GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
2729
2885
[SEPARATOR str_const])
2731
2887
concat of values from "group by" operation
2734
Blobs doesn't work with DISTINCT or order_st BY
2890
Blobs doesn't work with DISTINCT or ORDER BY
2735
2891
*****************************************************************************/
2784
function of sort for syntax: GROUP_CONCAT(expr,... order_st BY col,... )
2940
function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
2787
2943
int group_concat_key_cmp_with_order(void* arg, const void* key1,
2788
2944
const void* key2)
2790
2946
Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2791
order_st **order_item, **end;
2792
Table *table= grp_item->table;
2947
ORDER **order_item, **end;
2948
TABLE *table= grp_item->table;
2794
2950
for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2795
2951
order_item < end;
2828
2984
Append data from current leaf to item->result.
2831
int dump_leaf_key(unsigned char* key, element_count count __attribute__((unused)),
2987
int dump_leaf_key(uchar* key, element_count count __attribute__((unused)),
2832
2988
Item_func_group_concat *item)
2834
Table *table= item->table;
2990
TABLE *table= item->table;
2835
2991
String tmp((char *)table->record[1], table->s->reclength,
2836
2992
default_charset_info);
2838
2994
String *result= &item->result;
2839
2995
Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
2840
uint32_t old_length= result->length();
2996
uint old_length= result->length();
2842
2998
if (item->no_appended)
2843
2999
item->no_appended= false;
3062
3218
copy_fields(tmp_table_param);
3063
3219
copy_funcs(tmp_table_param->items_to_copy);
3065
for (uint32_t i= 0; i < arg_count_field; i++)
3221
for (uint i= 0; i < arg_count_field; i++)
3067
3223
Item *show_item= args[i];
3068
3224
if (!show_item->const_item())
3070
3226
Field *f= show_item->get_tmp_table_field();
3071
if (f->is_null_in_record((const unsigned char*) table->record[0]))
3227
if (f->is_null_in_record((const uchar*) table->record[0]))
3072
3228
return 0; // Skip row if it contains null
3138
3294
max_length= thd->variables.group_concat_max_len;
3141
3297
if (separator->needs_conversion(separator->length(), separator->charset(),
3142
3298
collation.collation, &offset))
3144
uint32_t buflen= collation.collation->mbmaxlen * separator->length();
3145
uint32_t errors, conv_length;
3300
uint32 buflen= collation.collation->mbmaxlen * separator->length();
3301
uint errors, conv_length;
3147
3303
String *new_separator;
3149
if (!(buf= (char*) thd->alloc(buflen)) ||
3150
!(new_separator= new(thd->mem_root)
3305
if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
3306
!(new_separator= new(thd->stmt_arena->mem_root)
3151
3307
String(buf, buflen, collation.collation)))
3231
3387
We have to create a temporary table to get descriptions of fields
3232
3388
(types, sizes and so on).
3234
Note that in the table, we first have the order_st BY fields, then the
3390
Note that in the table, we first have the ORDER BY fields, then the
3237
3393
if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
3238
(order_st*) 0, 0, true,
3394
(ORDER*) 0, 0, true,
3239
3395
(select_lex->options | thd->options),
3240
3396
HA_POS_ERROR, (char*) "")))
3247
3403
Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3248
3404
the row is not added to the result.
3250
uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
3406
uint tree_key_length= table->s->reclength - table->s->null_bytes;
3252
3408
if (arg_count_order)
3254
3410
tree= &tree_base;
3256
3412
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
3413
syntax of this function). If there is no ORDER BY clause, we don't
3258
3414
create this tree.
3260
init_tree(tree, (uint) cmin(thd->variables.max_heap_table_size,
3416
init_tree(tree, (uint) min(thd->variables.max_heap_table_size,
3261
3417
thd->variables.sortbuff_size/16), 0,
3262
3418
tree_key_length,
3263
3419
group_concat_key_cmp_with_order , 0, NULL, (void*) this);