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
944
case STRING_RESULT:
962
945
case REAL_RESULT:
963
946
val.traits= Hybrid_type_traits::instance();
964
table_field_type= DRIZZLE_TYPE_DOUBLE;
947
if (table_field_type != MYSQL_TYPE_FLOAT)
948
table_field_type= MYSQL_TYPE_DOUBLE;
968
952
Preserving int8, int16, int32 field types gives ~10% performance boost
969
953
as the size of result tree becomes significantly smaller.
970
Another speed up we gain by using int64_t for intermediate
954
Another speed up we gain by using longlong for intermediate
971
955
calculations. The range of int64 is enough to hold sum 2^32 distinct
972
956
integers each <= 2^32.
974
if (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))
976
961
val.traits= Hybrid_type_traits_fast_decimal::instance();
979
table_field_type= DRIZZLE_TYPE_LONGLONG;
964
table_field_type= MYSQL_TYPE_LONGLONG;
980
965
/* fallthrough */
981
966
case DECIMAL_RESULT:
982
967
val.traits= Hybrid_type_traits_decimal::instance();
983
if (table_field_type != DRIZZLE_TYPE_LONGLONG)
984
table_field_type= DRIZZLE_TYPE_DECIMAL;
968
if (table_field_type != MYSQL_TYPE_LONGLONG)
969
table_field_type= MYSQL_TYPE_NEWDECIMAL;
990
975
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
981
check that the case of CHAR(0) works OK
1004
bool Item_sum_distinct::setup(Session *session)
983
bool Item_sum_distinct::setup(THD *thd)
1006
List<CreateField> field_list;
1007
CreateField field_def; /* field definition */
985
List<Create_field> field_list;
986
Create_field field_def; /* field definition */
987
DBUG_ENTER("Item_sum_distinct::setup");
1008
988
/* It's legal to call setup() more than once when in a subquery */
1013
993
Virtual table and the tree are created anew on each re-execution of
1255
1237
The easiest way is to do this is to store both value in a string
1256
1238
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);
1240
field= new Field_string(((hybrid_type == DECIMAL_RESULT) ?
1241
dec_bin_size : sizeof(double)) + sizeof(longlong),
1242
0, name, &my_charset_bin);
1262
1244
else if (hybrid_type == DECIMAL_RESULT)
1263
field= new Field_decimal(max_length, maybe_null, name,
1264
decimals, unsigned_flag);
1245
field= new Field_new_decimal(max_length, maybe_null, name,
1246
decimals, unsigned_flag);
1266
field= new Field_double(max_length, maybe_null, name, decimals, true);
1248
field= new Field_double(max_length, maybe_null, name, decimals, TRUE);
1268
1250
field->init(table);
1878
1852
bits= reset_bits;
1881
Item *Item_sum_or::copy_or_same(Session* session)
1855
Item *Item_sum_or::copy_or_same(THD* thd)
1883
return new (session->mem_root) Item_sum_or(session, this);
1857
return new (thd->mem_root) Item_sum_or(thd, this);
1887
1861
bool Item_sum_or::add()
1889
uint64_t value= (uint64_t) args[0]->val_int();
1863
ulonglong value= (ulonglong) args[0]->val_int();
1890
1864
if (!args[0]->null_value)
1895
Item *Item_sum_xor::copy_or_same(Session* session)
1869
Item *Item_sum_xor::copy_or_same(THD* thd)
1897
return new (session->mem_root) Item_sum_xor(session, this);
1871
return new (thd->mem_root) Item_sum_xor(thd, this);
1901
1875
bool Item_sum_xor::add()
1903
uint64_t value= (uint64_t) args[0]->val_int();
1877
ulonglong value= (ulonglong) args[0]->val_int();
1904
1878
if (!args[0]->null_value)
1909
Item *Item_sum_and::copy_or_same(Session* session)
1883
Item *Item_sum_and::copy_or_same(THD* thd)
1911
return new (session->mem_root) Item_sum_and(session, this);
1885
return new (thd->mem_root) Item_sum_and(thd, this);
1915
1889
bool Item_sum_and::add()
1917
uint64_t value= (uint64_t) args[0]->val_int();
1891
ulonglong value= (ulonglong) args[0]->val_int();
1918
1892
if (!args[0]->null_value)
2571
bool Item_sum_count_distinct::setup(Session *session)
2544
bool Item_sum_count_distinct::setup(THD *thd)
2573
2546
List<Item> list;
2574
Select_Lex *select_lex= session->lex->current_select;
2547
SELECT_LEX *select_lex= thd->lex->current_select;
2577
2550
Setup can be called twice for ROLLUP items. This is a bug.
2578
Please add assert(tree == 0) here when it's fixed.
2551
Please add DBUG_ASSERT(tree == 0) here when it's fixed.
2579
2552
It's legal to call setup() more than once when in a subquery
2581
2554
if (tree || table || tmp_table_param)
2584
if (!(tmp_table_param= new Tmp_Table_Param))
2557
if (!(tmp_table_param= new TMP_TABLE_PARAM))
2587
2560
/* Create a table with an unique key over all parameters */
2588
for (uint32_t i=0; i < arg_count ; i++)
2561
for (uint i=0; i < arg_count ; i++)
2590
2563
Item *item=args[i];
2591
2564
if (list.push_back(item))
2592
return true; // End of memory
2565
return TRUE; // End of memory
2593
2566
if (item->const_item() && item->is_null())
2594
2567
always_null= 1;
2596
2569
if (always_null)
2598
2571
count_field_types(select_lex, tmp_table_param, list, 0);
2599
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)
2602
if (!(table= create_tmp_table(session, tmp_table_param, list, (order_st*) 0, 1,
2591
if (!(table= create_tmp_table(thd, tmp_table_param, list, (ORDER*) 0, 1,
2604
(select_lex->options | session->options),
2593
(select_lex->options | thd->options),
2605
2594
HA_POS_ERROR, (char*)"")))
2609
table->cursor->extra(HA_EXTRA_NO_ROWS); // Don't update rows
2596
table->file->extra(HA_EXTRA_NO_ROWS); // Don't update rows
2610
2597
table->no_rows=1;
2612
if (table->getShare()->db_type() == heap_engine)
2599
if (table->s->db_type() == heap_hton)
2615
2602
No blobs, otherwise it would have been MyISAM: set up a compare
2655
DBUG_ASSERT(tree == 0);
2669
2656
tree= new Unique(compare_key, cmp_arg, tree_key_length,
2670
(size_t)session->variables.max_heap_table_size);
2657
thd->variables.max_heap_table_size);
2672
2659
The only time tree_key_length could be 0 is if someone does
2673
2660
count(distinct) on a char(0) field - stupid thing to do,
2674
2661
but this has to be handled - otherwise someone can crash
2675
2662
the server with a DoS attack
2677
is_evaluated= false;
2664
is_evaluated= FALSE;
2685
Item *Item_sum_count_distinct::copy_or_same(Session* session)
2672
Item *Item_sum_count_distinct::copy_or_same(THD* thd)
2687
return new (session->mem_root) Item_sum_count_distinct(session, this);
2674
return new (thd->mem_root) Item_sum_count_distinct(thd, this);
2691
2678
void Item_sum_count_distinct::clear()
2693
2680
/* tree and table can be both null only if always_null */
2694
is_evaluated= false;
2681
is_evaluated= FALSE;
2699
2686
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);
2688
table->file->extra(HA_EXTRA_NO_CACHE);
2689
table->file->ha_delete_all_rows();
2690
table->file->extra(HA_EXTRA_WRITE_CACHE);
2729
2712
bloat the tree without providing any valuable info. Besides,
2730
2713
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);
2715
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))
2717
if ((error= table->file->ha_write_row(table->record[0])) &&
2718
table->file->is_fatal_error(error, HA_CHECK_DUP))
2741
int64_t Item_sum_count_distinct::val_int()
2724
longlong Item_sum_count_distinct::val_int()
2727
DBUG_ASSERT(fixed == 1);
2745
2728
if (!table) // Empty query
2749
2732
if (is_evaluated)
2752
2735
if (tree->elements == 0)
2753
return (int64_t) tree->elements_in_tree(); // everything fits in memory
2736
return (longlong) tree->elements_in_tree(); // everything fits in memory
2755
2738
tree->walk(count_distinct_walk, (void*) &count);
2757
return (int64_t) count;
2740
return (longlong) count;
2760
error= table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2743
error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2764
table->print_error(error, MYF(0));
2767
return table->cursor->stats.records;
2747
table->file->print_error(error, MYF(0));
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 */
2770
2925
/*****************************************************************************
2771
2926
GROUP_CONCAT function
2774
GROUP_CONCAT([DISTINCT] expr,... [order_st BY col [ASC|DESC],...]
2929
GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
2775
2930
[SEPARATOR str_const])
2777
2932
concat of values from "group by" operation
2780
Blobs doesn't work with DISTINCT or order_st BY
2935
Blobs doesn't work with DISTINCT or ORDER BY
2781
2936
*****************************************************************************/
2785
2940
Compares the values for fields in expr list of GROUP_CONCAT.
2788
2943
GROUP_CONCAT([DISTINCT] expr [,expr ...]
2789
[order_st BY {unsigned_integer | col_name | expr}
2944
[ORDER BY {unsigned_integer | col_name | expr}
2790
2945
[ASC | DESC] [,col_name ...]]
2791
2946
[SEPARATOR str_val])
2794
@retval -1 : key1 < key2
2949
@retval -1 : key1 < key2
2795
2950
@retval 0 : key1 = key2
2796
@retval 1 : key1 > key2
2951
@retval 1 : key1 > key2
2799
int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
2954
int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
2800
2955
const void* key2)
2802
2957
Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
2803
Table *table= item_func->table;
2958
TABLE *table= item_func->table;
2805
for (uint32_t i= 0; i < item_func->arg_count_field; i++)
2960
for (uint i= 0; i < item_func->arg_count_field; i++)
2807
2962
Item *item= item_func->args[i];
2809
2964
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.
2965
or it is an item over a const table.
2812
2967
if (item->const_item())
3107
3264
copy_fields(tmp_table_param);
3108
3265
copy_funcs(tmp_table_param->items_to_copy);
3110
for (uint32_t i= 0; i < arg_count_field; i++)
3267
for (uint i= 0; i < arg_count_field; i++)
3112
3269
Item *show_item= args[i];
3113
3270
if (!show_item->const_item())
3115
3272
Field *f= show_item->get_tmp_table_field();
3116
if (f->is_null_in_record((const unsigned char*) table->record[0]))
3273
if (f->is_null_in_record((const uchar*) table->record[0]))
3117
3274
return 0; // Skip row if it contains null
3122
bool row_eligible= true;
3279
bool row_eligible= TRUE;
3126
3283
/* 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);
3284
uint count= unique_filter->elements_in_tree();
3285
unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3129
3286
if (count == unique_filter->elements_in_tree())
3130
row_eligible= false;
3287
row_eligible= FALSE;
3133
3290
TREE_ELEMENT *el= 0; // Only for safety
3134
3291
if (row_eligible && tree)
3135
el= tree_insert(tree, table->record[0] + table->getShare()->null_bytes, 0,
3292
el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
3136
3293
tree->custom_arg);
3138
3295
If the row is not a duplicate (el->count == 1)
3180
3337
result.set_charset(collation.collation);
3181
3338
result_field= 0;
3183
max_length= (size_t)session->variables.group_concat_max_len;
3185
if (check_sum_func(session, ref))
3340
max_length= thd->variables.group_concat_max_len;
3343
if (separator->needs_conversion(separator->length(), separator->charset(),
3344
collation.collation, &offset))
3346
uint32 buflen= collation.collation->mbmaxlen * separator->length();
3347
uint errors, conv_length;
3349
String *new_separator;
3351
if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
3352
!(new_separator= new(thd->stmt_arena->mem_root)
3353
String(buf, buflen, collation.collation)))
3356
conv_length= copy_and_convert(buf, buflen, collation.collation,
3357
separator->ptr(), separator->length(),
3358
separator->charset(), &errors);
3359
new_separator->length(conv_length);
3360
separator= new_separator;
3363
if (check_sum_func(thd, ref))
3193
bool Item_func_group_concat::setup(Session *session)
3371
bool Item_func_group_concat::setup(THD *thd)
3195
3373
List<Item> list;
3196
Select_Lex *select_lex= session->lex->current_select;
3374
SELECT_LEX *select_lex= thd->lex->current_select;
3375
DBUG_ENTER("Item_func_group_concat::setup");
3199
3378
Currently setup() can be called twice. Please add
3200
3379
assertion here when this is fixed.
3202
3381
if (table || tree)
3205
if (!(tmp_table_param= new Tmp_Table_Param))
3384
if (!(tmp_table_param= new TMP_TABLE_PARAM))
3208
3387
/* We'll convert all blobs to varchar fields in the temporary table */
3209
3388
tmp_table_param->convert_blob_length= max_length *
3210
3389
collation.collation->mbmaxlen;
3211
3390
/* Push all not constant fields to the list and create a temp table */
3212
3391
always_null= 0;
3213
for (uint32_t i= 0; i < arg_count_field; i++)
3392
for (uint i= 0; i < arg_count_field; i++)
3215
3394
Item *item= args[i];
3216
3395
if (list.push_back(item))
3218
3397
if (item->const_item())
3220
3399
if (item->is_null())
3222
3401
always_null= 1;
3228
3407
List<Item> all_fields(list);
3230
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
3231
3410
arguments. If an expression is not found, prepend it to
3232
3411
"all_fields". The resulting field list is used as input to create
3233
3412
tmp table columns.
3235
3414
if (arg_count_order &&
3236
setup_order(session, args, context->table_list, list, all_fields, *order))
3415
setup_order(thd, args, context->table_list, list, all_fields, *order))
3239
3418
count_field_types(select_lex, tmp_table_param, all_fields, 0);
3240
3419
tmp_table_param->force_copy_fields= force_copy_fields;
3420
DBUG_ASSERT(table == 0);
3242
3421
if (arg_count_order > 0 || distinct)