169
148
Try to find a subquery where it can be aggregated;
170
149
If we fail to find such a subquery report an error.
172
if (register_sum_func(session, ref))
151
if (register_sum_func(thd, ref))
174
153
invalid= aggr_level < 0 && !(allow_sum_func & (1 << nest_level));
175
if (!invalid && false)
154
if (!invalid && thd->variables.sql_mode & MODE_ANSI)
176
155
invalid= aggr_level < 0 && max_arg_level < nest_level;
178
157
if (!invalid && aggr_level < 0)
180
159
aggr_level= nest_level;
181
aggr_sel= session->lex->current_select;
160
aggr_sel= thd->lex->current_select;
184
163
By this moment we either found a subquery where the set function is
185
164
to be aggregated and assigned a value that is >= 0 to aggr_level,
186
or set the value of 'invalid' to TRUE to report later an error.
165
or set the value of 'invalid' to TRUE to report later an error.
189
168
Additionally we have to check whether possible nested set functions
190
169
are acceptable here: they are not, if the level of aggregation of
191
170
some of them is less than aggr_level.
194
173
invalid= aggr_level <= max_sum_func_level;
197
176
my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
353
330
aggr_sel->inner_sum_func_list= this;
354
331
aggr_sel->with_sum_func= 1;
357
334
Mark Item_subselect(s) as containing aggregate function all the way up
358
335
to aggregate function's calculation context.
359
336
Note that we must not mark the Item of calculation context itself
360
because with_sum_func on the calculation context Select_Lex is
337
because with_sum_func on the calculation context st_select_lex is
361
338
already set above.
363
with_sum_func being set for an Item means that this Item refers
340
with_sum_func being set for an Item means that this Item refers
364
341
(somewhere in it, e.g. one of its arguments if it's a function) directly
365
342
or through intermediate items to an aggregate function that is calculated
366
343
in a context "outside" of the Item (e.g. in the current or outer select).
368
with_sum_func being set for an Select_Lex means that this Select_Lex
345
with_sum_func being set for an st_select_lex means that this st_select_lex
369
346
has aggregate functions directly referenced (i.e. not through a sub-select).
371
for (sl= session->lex->current_select;
348
for (sl= thd->lex->current_select;
372
349
sl && sl != aggr_sel && sl->master_unit()->item;
373
350
sl= sl->master_unit()->outer_select() )
374
351
sl->master_unit()->item->with_sum_func= 1;
376
session->lex->current_select->mark_as_dependent(aggr_sel);
353
thd->lex->current_select->mark_as_dependent(aggr_sel);
381
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements),
358
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements),
384
if ((args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
361
if ((args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
387
364
List_iterator_fast<Item> li(list);
961
946
case STRING_RESULT:
962
947
case REAL_RESULT:
963
948
val.traits= Hybrid_type_traits::instance();
964
table_field_type= DRIZZLE_TYPE_DOUBLE;
949
if (table_field_type != MYSQL_TYPE_FLOAT)
950
table_field_type= MYSQL_TYPE_DOUBLE;
968
954
Preserving int8, int16, int32 field types gives ~10% performance boost
969
955
as the size of result tree becomes significantly smaller.
970
Another speed up we gain by using int64_t for intermediate
956
Another speed up we gain by using longlong for intermediate
971
957
calculations. The range of int64 is enough to hold sum 2^32 distinct
972
958
integers each <= 2^32.
974
if (table_field_type == DRIZZLE_TYPE_LONG)
960
if (table_field_type >= MYSQL_TYPE_TINY && table_field_type <= MYSQL_TYPE_LONG)
976
962
val.traits= Hybrid_type_traits_fast_decimal::instance();
979
table_field_type= DRIZZLE_TYPE_LONGLONG;
965
table_field_type= MYSQL_TYPE_LONGLONG;
980
966
/* fallthrough */
981
967
case DECIMAL_RESULT:
982
968
val.traits= Hybrid_type_traits_decimal::instance();
983
if (table_field_type != DRIZZLE_TYPE_LONGLONG)
984
table_field_type= DRIZZLE_TYPE_DECIMAL;
969
if (table_field_type != MYSQL_TYPE_LONGLONG)
970
table_field_type= MYSQL_TYPE_NEWDECIMAL;
990
976
val.traits->fix_length_and_dec(this, args[0]);
994
enum Item_result Item_sum_distinct::result_type () const
996
return val.traits->type();
1002
982
check that the case of CHAR(0) works OK
1004
bool Item_sum_distinct::setup(Session *session)
984
bool Item_sum_distinct::setup(THD *thd)
1006
List<CreateField> field_list;
1007
CreateField field_def; /* field definition */
986
List<Create_field> field_list;
987
Create_field field_def; /* field definition */
988
DBUG_ENTER("Item_sum_distinct::setup");
1008
989
/* It's legal to call setup() more than once when in a subquery */
1013
994
Virtual table and the tree are created anew on each re-execution of
1255
1238
The easiest way is to do this is to store both value in a string
1256
1239
and unpack on access.
1258
field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
1259
dec_bin_size : sizeof(double)) + sizeof(int64_t),
1260
0, name, table->getMutableShare(), &my_charset_bin);
1241
field= new Field_string(((hybrid_type == DECIMAL_RESULT) ?
1242
dec_bin_size : sizeof(double)) + sizeof(longlong),
1243
0, name, &my_charset_bin);
1262
1245
else if (hybrid_type == DECIMAL_RESULT)
1263
field= new Field_decimal(max_length, maybe_null, name,
1264
decimals, unsigned_flag);
1246
field= new Field_new_decimal(max_length, maybe_null, name,
1247
decimals, unsigned_flag);
1266
field= new Field_double(max_length, maybe_null, name, decimals, true);
1249
field= new Field_double(max_length, maybe_null, name, decimals, TRUE);
1268
1251
field->init(table);
1878
1853
bits= reset_bits;
1881
Item *Item_sum_or::copy_or_same(Session* session)
1856
Item *Item_sum_or::copy_or_same(THD* thd)
1883
return new (session->mem_root) Item_sum_or(session, this);
1858
return new (thd->mem_root) Item_sum_or(thd, this);
1887
1862
bool Item_sum_or::add()
1889
uint64_t value= (uint64_t) args[0]->val_int();
1864
ulonglong value= (ulonglong) args[0]->val_int();
1890
1865
if (!args[0]->null_value)
1895
Item *Item_sum_xor::copy_or_same(Session* session)
1870
Item *Item_sum_xor::copy_or_same(THD* thd)
1897
return new (session->mem_root) Item_sum_xor(session, this);
1872
return new (thd->mem_root) Item_sum_xor(thd, this);
1901
1876
bool Item_sum_xor::add()
1903
uint64_t value= (uint64_t) args[0]->val_int();
1878
ulonglong value= (ulonglong) args[0]->val_int();
1904
1879
if (!args[0]->null_value)
1909
Item *Item_sum_and::copy_or_same(Session* session)
1884
Item *Item_sum_and::copy_or_same(THD* thd)
1911
return new (session->mem_root) Item_sum_and(session, this);
1886
return new (thd->mem_root) Item_sum_and(thd, this);
1915
1890
bool Item_sum_and::add()
1917
uint64_t value= (uint64_t) args[0]->val_int();
1892
ulonglong value= (ulonglong) args[0]->val_int();
1918
1893
if (!args[0]->null_value)
2571
bool Item_sum_count_distinct::setup(Session *session)
2547
bool Item_sum_count_distinct::setup(THD *thd)
2573
2549
List<Item> list;
2574
Select_Lex *select_lex= session->lex->current_select;
2550
SELECT_LEX *select_lex= thd->lex->current_select;
2577
2553
Setup can be called twice for ROLLUP items. This is a bug.
2578
Please add assert(tree == 0) here when it's fixed.
2554
Please add DBUG_ASSERT(tree == 0) here when it's fixed.
2579
2555
It's legal to call setup() more than once when in a subquery
2581
2557
if (tree || table || tmp_table_param)
2584
if (!(tmp_table_param= new Tmp_Table_Param))
2560
if (!(tmp_table_param= new TMP_TABLE_PARAM))
2587
2563
/* Create a table with an unique key over all parameters */
2588
for (uint32_t i=0; i < arg_count ; i++)
2564
for (uint i=0; i < arg_count ; i++)
2590
2566
Item *item=args[i];
2591
2567
if (list.push_back(item))
2592
return true; // End of memory
2568
return TRUE; // End of memory
2593
2569
if (item->const_item() && item->is_null())
2594
2570
always_null= 1;
2596
2572
if (always_null)
2598
2574
count_field_types(select_lex, tmp_table_param, list, 0);
2599
2575
tmp_table_param->force_copy_fields= force_copy_fields;
2576
DBUG_ASSERT(table == 0);
2602
if (!(table= create_tmp_table(session, tmp_table_param, list, (order_st*) 0, 1,
2578
if (!(table= create_tmp_table(thd, tmp_table_param, list, (ORDER*) 0, 1,
2604
(select_lex->options | session->options),
2580
(select_lex->options | thd->options),
2605
2581
HA_POS_ERROR, (char*)"")))
2609
table->cursor->extra(HA_EXTRA_NO_ROWS); // Don't update rows
2583
table->file->extra(HA_EXTRA_NO_ROWS); // Don't update rows
2610
2584
table->no_rows=1;
2612
if (table->getShare()->db_type() == heap_engine)
2586
if (table->s->db_type() == heap_hton)
2615
2589
No blobs, otherwise it would have been MyISAM: set up a compare
2642
DBUG_ASSERT(tree == 0);
2669
2643
tree= new Unique(compare_key, cmp_arg, tree_key_length,
2670
(size_t)session->variables.max_heap_table_size);
2644
thd->variables.max_heap_table_size);
2672
2646
The only time tree_key_length could be 0 is if someone does
2673
2647
count(distinct) on a char(0) field - stupid thing to do,
2674
2648
but this has to be handled - otherwise someone can crash
2675
2649
the server with a DoS attack
2677
is_evaluated= false;
2651
is_evaluated= FALSE;
2685
Item *Item_sum_count_distinct::copy_or_same(Session* session)
2659
Item *Item_sum_count_distinct::copy_or_same(THD* thd)
2687
return new (session->mem_root) Item_sum_count_distinct(session, this);
2661
return new (thd->mem_root) Item_sum_count_distinct(thd, this);
2691
2665
void Item_sum_count_distinct::clear()
2693
2667
/* tree and table can be both null only if always_null */
2694
is_evaluated= false;
2668
is_evaluated= FALSE;
2699
2673
else if (table)
2701
table->cursor->extra(HA_EXTRA_NO_CACHE);
2702
table->cursor->ha_delete_all_rows();
2703
table->cursor->extra(HA_EXTRA_WRITE_CACHE);
2675
table->file->extra(HA_EXTRA_NO_CACHE);
2676
table->file->ha_delete_all_rows();
2677
table->file->extra(HA_EXTRA_WRITE_CACHE);
2729
2699
bloat the tree without providing any valuable info. Besides,
2730
2700
key_length used to initialize the tree didn't include space for them.
2732
return tree->unique_add(table->record[0] + table->getShare()->null_bytes);
2702
return tree->unique_add(table->record[0] + table->s->null_bytes);
2734
if ((error= table->cursor->insertRecord(table->record[0])) &&
2735
table->cursor->is_fatal_error(error, HA_CHECK_DUP))
2704
if ((error= table->file->ha_write_row(table->record[0])) &&
2705
table->file->is_fatal_error(error, HA_CHECK_DUP))
2741
int64_t Item_sum_count_distinct::val_int()
2711
longlong Item_sum_count_distinct::val_int()
2714
DBUG_ASSERT(fixed == 1);
2745
2715
if (!table) // Empty query
2749
2719
if (is_evaluated)
2752
2722
if (tree->elements == 0)
2753
return (int64_t) tree->elements_in_tree(); // everything fits in memory
2723
return (longlong) tree->elements_in_tree(); // everything fits in memory
2755
2725
tree->walk(count_distinct_walk, (void*) &count);
2757
return (int64_t) count;
2727
return (longlong) count;
2760
error= table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2730
error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2764
table->print_error(error, MYF(0));
2767
return table->cursor->stats.records;
2734
table->file->print_error(error, MYF(0));
2737
return table->file->stats.records;
2741
/****************************************************************************
2742
** Functions to handle dynamic loadable aggregates
2743
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2744
** Adapted for UDAs by: Andreas F. Bobak <bobak@relog.ch>.
2745
** Rewritten by: Monty.
2746
****************************************************************************/
2750
void Item_udf_sum::clear()
2752
DBUG_ENTER("Item_udf_sum::clear");
2757
bool Item_udf_sum::add()
2759
DBUG_ENTER("Item_udf_sum::add");
2760
udf.add(&null_value);
2764
void Item_udf_sum::cleanup()
2767
udf_handler::cleanup() nicely handles case when we have not
2768
original item but one created by copy_or_same() method.
2771
Item_sum::cleanup();
2775
void Item_udf_sum::print(String *str, enum_query_type query_type)
2777
str->append(func_name());
2779
for (uint i=0 ; i < arg_count ; i++)
2783
args[i]->print(str, query_type);
2789
Item *Item_sum_udf_float::copy_or_same(THD* thd)
2791
return new (thd->mem_root) Item_sum_udf_float(thd, this);
2794
double Item_sum_udf_float::val_real()
2796
DBUG_ASSERT(fixed == 1);
2797
DBUG_ENTER("Item_sum_udf_float::val");
2798
DBUG_PRINT("info",("result_type: %d arg_count: %d",
2799
args[0]->result_type(), arg_count));
2800
DBUG_RETURN(udf.val(&null_value));
2804
String *Item_sum_udf_float::val_str(String *str)
2806
return val_string_from_real(str);
2810
my_decimal *Item_sum_udf_float::val_decimal(my_decimal *dec)
2812
return val_decimal_from_real(dec);
2816
String *Item_sum_udf_decimal::val_str(String *str)
2818
return val_string_from_decimal(str);
2822
double Item_sum_udf_decimal::val_real()
2824
return val_real_from_decimal();
2828
longlong Item_sum_udf_decimal::val_int()
2830
return val_int_from_decimal();
2834
my_decimal *Item_sum_udf_decimal::val_decimal(my_decimal *dec_buf)
2836
DBUG_ASSERT(fixed == 1);
2837
DBUG_ENTER("Item_func_udf_decimal::val_decimal");
2838
DBUG_PRINT("info",("result_type: %d arg_count: %d",
2839
args[0]->result_type(), arg_count));
2841
DBUG_RETURN(udf.val_decimal(&null_value, dec_buf));
2845
Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
2847
return new (thd->mem_root) Item_sum_udf_decimal(thd, this);
2851
Item *Item_sum_udf_int::copy_or_same(THD* thd)
2853
return new (thd->mem_root) Item_sum_udf_int(thd, this);
2856
longlong Item_sum_udf_int::val_int()
2858
DBUG_ASSERT(fixed == 1);
2859
DBUG_ENTER("Item_sum_udf_int::val_int");
2860
DBUG_PRINT("info",("result_type: %d arg_count: %d",
2861
args[0]->result_type(), arg_count));
2862
DBUG_RETURN(udf.val_int(&null_value));
2866
String *Item_sum_udf_int::val_str(String *str)
2868
return val_string_from_int(str);
2871
my_decimal *Item_sum_udf_int::val_decimal(my_decimal *dec)
2873
return val_decimal_from_int(dec);
2877
/** Default max_length is max argument length. */
2879
void Item_sum_udf_str::fix_length_and_dec()
2881
DBUG_ENTER("Item_sum_udf_str::fix_length_and_dec");
2883
for (uint i = 0; i < arg_count; i++)
2884
set_if_bigger(max_length,args[i]->max_length);
2889
Item *Item_sum_udf_str::copy_or_same(THD* thd)
2891
return new (thd->mem_root) Item_sum_udf_str(thd, this);
2895
my_decimal *Item_sum_udf_str::val_decimal(my_decimal *dec)
2897
return val_decimal_from_string(dec);
2900
String *Item_sum_udf_str::val_str(String *str)
2902
DBUG_ASSERT(fixed == 1);
2903
DBUG_ENTER("Item_sum_udf_str::str");
2904
String *res=udf.val_str(str,&str_value);
2909
#endif /* HAVE_DLOPEN */
2770
2912
/*****************************************************************************
2771
2913
GROUP_CONCAT function
2774
GROUP_CONCAT([DISTINCT] expr,... [order_st BY col [ASC|DESC],...]
2916
GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
2775
2917
[SEPARATOR str_const])
2777
2919
concat of values from "group by" operation
2780
Blobs doesn't work with DISTINCT or order_st BY
2922
Blobs doesn't work with DISTINCT or ORDER BY
2781
2923
*****************************************************************************/
2785
2927
Compares the values for fields in expr list of GROUP_CONCAT.
2788
2930
GROUP_CONCAT([DISTINCT] expr [,expr ...]
2789
[order_st BY {unsigned_integer | col_name | expr}
2931
[ORDER BY {unsigned_integer | col_name | expr}
2790
2932
[ASC | DESC] [,col_name ...]]
2791
2933
[SEPARATOR str_val])
2794
@retval -1 : key1 < key2
2936
@retval -1 : key1 < key2
2795
2937
@retval 0 : key1 = key2
2796
@retval 1 : key1 > key2
2938
@retval 1 : key1 > key2
2799
int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
2941
int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
2800
2942
const void* key2)
2802
2944
Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
2803
Table *table= item_func->table;
2945
TABLE *table= item_func->table;
2805
for (uint32_t i= 0; i < item_func->arg_count_field; i++)
2947
for (uint i= 0; i < item_func->arg_count_field; i++)
2807
2949
Item *item= item_func->args[i];
2809
2951
If field_item is a const item then either get_tp_table_field returns 0
2810
or it is an item over a const table.
2952
or it is an item over a const table.
2812
2954
if (item->const_item())
3107
3251
copy_fields(tmp_table_param);
3108
3252
copy_funcs(tmp_table_param->items_to_copy);
3110
for (uint32_t i= 0; i < arg_count_field; i++)
3254
for (uint i= 0; i < arg_count_field; i++)
3112
3256
Item *show_item= args[i];
3113
3257
if (!show_item->const_item())
3115
3259
Field *f= show_item->get_tmp_table_field();
3116
if (f->is_null_in_record((const unsigned char*) table->record[0]))
3260
if (f->is_null_in_record((const uchar*) table->record[0]))
3117
3261
return 0; // Skip row if it contains null
3122
bool row_eligible= true;
3266
bool row_eligible= TRUE;
3126
3270
/* Filter out duplicate rows. */
3127
uint32_t count= unique_filter->elements_in_tree();
3128
unique_filter->unique_add(table->record[0] + table->getShare()->null_bytes);
3271
uint count= unique_filter->elements_in_tree();
3272
unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3129
3273
if (count == unique_filter->elements_in_tree())
3130
row_eligible= false;
3274
row_eligible= FALSE;
3133
3277
TREE_ELEMENT *el= 0; // Only for safety
3134
3278
if (row_eligible && tree)
3135
el= tree_insert(tree, table->record[0] + table->getShare()->null_bytes, 0,
3279
el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
3136
3280
tree->custom_arg);
3138
3282
If the row is not a duplicate (el->count == 1)
3180
3324
result.set_charset(collation.collation);
3181
3325
result_field= 0;
3183
max_length= (size_t)session->variables.group_concat_max_len;
3185
if (check_sum_func(session, ref))
3327
max_length= thd->variables.group_concat_max_len;
3330
if (separator->needs_conversion(separator->length(), separator->charset(),
3331
collation.collation, &offset))
3333
uint32 buflen= collation.collation->mbmaxlen * separator->length();
3334
uint errors, conv_length;
3336
String *new_separator;
3338
if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
3339
!(new_separator= new(thd->stmt_arena->mem_root)
3340
String(buf, buflen, collation.collation)))
3343
conv_length= copy_and_convert(buf, buflen, collation.collation,
3344
separator->ptr(), separator->length(),
3345
separator->charset(), &errors);
3346
new_separator->length(conv_length);
3347
separator= new_separator;
3350
if (check_sum_func(thd, ref))
3193
bool Item_func_group_concat::setup(Session *session)
3358
bool Item_func_group_concat::setup(THD *thd)
3195
3360
List<Item> list;
3196
Select_Lex *select_lex= session->lex->current_select;
3361
SELECT_LEX *select_lex= thd->lex->current_select;
3362
DBUG_ENTER("Item_func_group_concat::setup");
3199
3365
Currently setup() can be called twice. Please add
3200
3366
assertion here when this is fixed.
3202
3368
if (table || tree)
3205
if (!(tmp_table_param= new Tmp_Table_Param))
3371
if (!(tmp_table_param= new TMP_TABLE_PARAM))
3208
3374
/* We'll convert all blobs to varchar fields in the temporary table */
3209
3375
tmp_table_param->convert_blob_length= max_length *
3210
3376
collation.collation->mbmaxlen;
3211
3377
/* Push all not constant fields to the list and create a temp table */
3212
3378
always_null= 0;
3213
for (uint32_t i= 0; i < arg_count_field; i++)
3379
for (uint i= 0; i < arg_count_field; i++)
3215
3381
Item *item= args[i];
3216
3382
if (list.push_back(item))
3218
3384
if (item->const_item())
3220
3386
if (item->is_null())
3222
3388
always_null= 1;
3228
3394
List<Item> all_fields(list);
3230
Try to find every order_st expression in the list of GROUP_CONCAT
3396
Try to find every ORDER expression in the list of GROUP_CONCAT
3231
3397
arguments. If an expression is not found, prepend it to
3232
3398
"all_fields". The resulting field list is used as input to create
3233
3399
tmp table columns.
3235
3401
if (arg_count_order &&
3236
setup_order(session, args, context->table_list, list, all_fields, *order))
3402
setup_order(thd, args, context->table_list, list, all_fields, *order))
3239
3405
count_field_types(select_lex, tmp_table_param, all_fields, 0);
3240
3406
tmp_table_param->force_copy_fields= force_copy_fields;
3407
DBUG_ASSERT(table == 0);
3242
3408
if (arg_count_order > 0 || distinct)