1
/* Copyright (C) 2000-2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21
Sum functions (COUNT, MIN...)
24
#ifdef USE_PRAGMA_IMPLEMENTATION
25
#pragma implementation // gcc: Class implementation
28
#include "mysql_priv.h"
29
#include "sql_select.h"
32
Prepare an aggregate function item for checking context conditions.
34
The function initializes the members of the Item_sum object created
35
for a set function that are used to check validity of the set function
37
If the set function is not allowed in any subquery where it occurs
38
an error is reported immediately.
40
@param thd reference to the thread context info
43
This function is to be called for any item created for a set function
44
object when the traversal of trees built for expressions used in the query
45
is performed at the phase of context analysis. This function is to
46
be invoked at the descent of this traversal.
48
TRUE if an error is reported
53
bool Item_sum::init_sum_func_check(THD *thd)
55
if (!thd->lex->allow_sum_func)
57
my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
61
/* Set a reference to the nesting set function if there is any */
62
in_sum_func= thd->lex->in_sum_func;
63
/* Save a pointer to object to be used in items for nested set functions */
64
thd->lex->in_sum_func= this;
65
nest_level= thd->lex->current_select->nest_level;
70
max_sum_func_level= -1;
76
Check constraints imposed on a usage of a set function.
78
The method verifies whether context conditions imposed on a usage
79
of any set function are met for this occurrence.
80
It checks whether the set function occurs in the position where it
81
can be aggregated and, when it happens to occur in argument of another
82
set function, the method checks that these two functions are aggregated in
84
If the context conditions are not met the method reports an error.
85
If the set function is aggregated in some outer subquery the method
86
adds it to the chain of items for such set functions that is attached
87
to the the st_select_lex structure for this subquery.
89
A number of designated members of the object are used to check the
90
conditions. They are specified in the comment before the Item_sum
92
Additionally a bitmap variable called allow_sum_func is employed.
93
It is included into the thd->lex structure.
94
The bitmap contains 1 at n-th position if the set function happens
95
to occur under a construct of the n-th level subquery where usage
96
of set functions are allowed (i.e either in the SELECT list or
97
in the HAVING clause of the corresponding subquery)
100
SELECT SUM(t1.b) FROM t1 GROUP BY t1.a
101
HAVING t1.a IN (SELECT t2.c FROM t2 WHERE AVG(t1.b) > 20) AND
102
t1.a > (SELECT MIN(t2.d) FROM t2);
104
allow_sum_func will contain:
105
- for SUM(t1.b) - 1 at the first position
106
- for AVG(t1.b) - 1 at the first position, 0 at the second position
107
- for MIN(t2.d) - 1 at the first position, 1 at the second position.
109
@param thd reference to the thread context info
110
@param ref location of the pointer to this item in the embedding expression
113
This function is to be called for any item created for a set function
114
object when the traversal of trees built for expressions used in the query
115
is performed at the phase of context analysis. This function is to
116
be invoked at the ascent of this traversal.
119
TRUE if an error is reported
124
bool Item_sum::check_sum_func(THD *thd, Item **ref)
127
nesting_map allow_sum_func= thd->lex->allow_sum_func;
129
The value of max_arg_level is updated if an argument of the set function
130
contains a column reference resolved against a subquery whose level is
131
greater than the current value of max_arg_level.
132
max_arg_level cannot be greater than nest level.
133
nest level is always >= 0
135
if (nest_level == max_arg_level)
138
The function must be aggregated in the current subquery,
139
If it is there under a construct where it is not allowed
142
invalid= !(allow_sum_func & (1 << max_arg_level));
144
else if (max_arg_level >= 0 || !(allow_sum_func & (1 << nest_level)))
147
The set function can be aggregated only in outer subqueries.
148
Try to find a subquery where it can be aggregated;
149
If we fail to find such a subquery report an error.
151
if (register_sum_func(thd, ref))
153
invalid= aggr_level < 0 && !(allow_sum_func & (1 << nest_level));
154
if (!invalid && thd->variables.sql_mode & MODE_ANSI)
155
invalid= aggr_level < 0 && max_arg_level < nest_level;
157
if (!invalid && aggr_level < 0)
159
aggr_level= nest_level;
160
aggr_sel= thd->lex->current_select;
163
By this moment we either found a subquery where the set function is
164
to be aggregated and assigned a value that is >= 0 to aggr_level,
165
or set the value of 'invalid' to TRUE to report later an error.
168
Additionally we have to check whether possible nested set functions
169
are acceptable here: they are not, if the level of aggregation of
170
some of them is less than aggr_level.
173
invalid= aggr_level <= max_sum_func_level;
176
my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
184
If the set function is nested adjust the value of
185
max_sum_func_level for the nesting set function.
186
We take into account only enclosed set functions that are to be
187
aggregated on the same level or above of the nest level of
188
the enclosing set function.
189
But we must always pass up the max_sum_func_level because it is
190
the maximum nested level of all directly and indirectly enclosed
191
set functions. We must do that even for set functions that are
192
aggregated inside of their enclosing set function's nest level
193
because the enclosing function may contain another enclosing
194
function that is to be aggregated outside or on the same level
195
as its parent's nest level.
197
if (in_sum_func->nest_level >= aggr_level)
198
set_if_bigger(in_sum_func->max_sum_func_level, aggr_level);
199
set_if_bigger(in_sum_func->max_sum_func_level, max_sum_func_level);
203
Check that non-aggregated fields and sum functions aren't mixed in the
204
same select in the ONLY_FULL_GROUP_BY mode.
206
if (outer_fields.elements)
210
Here we compare the nesting level of the select to which an outer field
211
belongs to with the aggregation level of the sum function. All fields in
212
the outer_fields list are checked.
214
If the nesting level is equal to the aggregation level then the field is
215
aggregated by this sum function.
216
If the nesting level is less than the aggregation level then the field
217
belongs to an outer select. In this case if there is an embedding sum
218
function add current field to functions outer_fields list. If there is
219
no embedding function then the current field treated as non aggregated
220
and the select it belongs to is marked accordingly.
221
If the nesting level is greater than the aggregation level then it means
222
that this field was added by an inner sum function.
225
select avg ( <-- we are here, checking outer.f1
227
select sum(outer.f1 + inner.f1) from inner
231
In this case we check that no aggregate functions are used in the
232
select the field belongs to. If there are some then an error is
235
List_iterator<Item_field> of(outer_fields);
236
while ((field= of++))
238
SELECT_LEX *sel= field->cached_table->select_lex;
239
if (sel->nest_level < aggr_level)
244
Let upper function decide whether this field is a non
247
in_sum_func->outer_fields.push_back(field);
250
sel->full_group_by_flag|= NON_AGG_FIELD_USED;
252
if (sel->nest_level > aggr_level &&
253
(sel->full_group_by_flag & SUM_FUNC_USED) &&
254
!sel->group_list.elements)
256
my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
257
ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
262
aggr_sel->full_group_by_flag|= SUM_FUNC_USED;
263
update_used_tables();
264
thd->lex->in_sum_func= in_sum_func;
269
Attach a set function to the subquery where it must be aggregated.
271
The function looks for an outer subquery where the set function must be
272
aggregated. If it finds such a subquery then aggr_level is set to
273
the nest level of this subquery and the item for the set function
274
is added to the list of set functions used in nested subqueries
275
inner_sum_func_list defined for each subquery. When the item is placed
276
there the field 'ref_by' is set to ref.
279
Now we 'register' only set functions that are aggregated in outer
280
subqueries. Actually it makes sense to link all set function for
281
a subquery in one chain. It would simplify the process of 'splitting'
284
@param thd reference to the thread context info
285
@param ref location of the pointer to this item in the embedding expression
288
FALSE if the executes without failures (currently always)
293
bool Item_sum::register_sum_func(THD *thd, Item **ref)
296
nesting_map allow_sum_func= thd->lex->allow_sum_func;
297
for (sl= thd->lex->current_select->master_unit()->outer_select() ;
298
sl && sl->nest_level > max_arg_level;
299
sl= sl->master_unit()->outer_select() )
301
if (aggr_level < 0 && (allow_sum_func & (1 << sl->nest_level)))
303
/* Found the most nested subquery where the function can be aggregated */
304
aggr_level= sl->nest_level;
308
if (sl && (allow_sum_func & (1 << sl->nest_level)))
311
We reached the subquery of level max_arg_level and checked
312
that the function can be aggregated here.
313
The set function will be aggregated in this subquery.
315
aggr_level= sl->nest_level;
322
/* Add the object to the list of registered objects assigned to aggr_sel */
323
if (!aggr_sel->inner_sum_func_list)
327
next= aggr_sel->inner_sum_func_list->next;
328
aggr_sel->inner_sum_func_list->next= this;
330
aggr_sel->inner_sum_func_list= this;
331
aggr_sel->with_sum_func= 1;
334
Mark Item_subselect(s) as containing aggregate function all the way up
335
to aggregate function's calculation context.
336
Note that we must not mark the Item of calculation context itself
337
because with_sum_func on the calculation context st_select_lex is
340
with_sum_func being set for an Item means that this Item refers
341
(somewhere in it, e.g. one of its arguments if it's a function) directly
342
or through intermediate items to an aggregate function that is calculated
343
in a context "outside" of the Item (e.g. in the current or outer select).
345
with_sum_func being set for an st_select_lex means that this st_select_lex
346
has aggregate functions directly referenced (i.e. not through a sub-select).
348
for (sl= thd->lex->current_select;
349
sl && sl != aggr_sel && sl->master_unit()->item;
350
sl= sl->master_unit()->outer_select() )
351
sl->master_unit()->item->with_sum_func= 1;
353
thd->lex->current_select->mark_as_dependent(aggr_sel);
358
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements),
361
if ((args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
364
List_iterator_fast<Item> li(list);
373
list.empty(); // Fields are used
378
Constructor used in processing select with temporary tebles.
381
Item_sum::Item_sum(THD *thd, Item_sum *item):
382
Item_result_field(thd, item), arg_count(item->arg_count),
383
aggr_sel(item->aggr_sel),
384
nest_level(item->nest_level), aggr_level(item->aggr_level),
385
quick_group(item->quick_group), used_tables_cache(item->used_tables_cache),
386
forced_const(item->forced_const)
391
if (!(args= (Item**) thd->alloc(sizeof(Item*)*arg_count)))
393
memcpy(args, item->args, sizeof(Item*)*arg_count);
397
void Item_sum::mark_as_sum_func()
399
SELECT_LEX *cur_select= current_thd->lex->current_select;
400
cur_select->n_sum_items++;
401
cur_select->with_sum_func= 1;
406
void Item_sum::make_field(Send_field *tmp_field)
408
if (args[0]->type() == Item::FIELD_ITEM && keep_field_type())
410
((Item_field*) args[0])->field->make_field(tmp_field);
411
/* For expressions only col_name should be non-empty string. */
412
char *empty_string= (char*)"";
413
tmp_field->db_name= empty_string;
414
tmp_field->org_table_name= empty_string;
415
tmp_field->table_name= empty_string;
416
tmp_field->org_col_name= empty_string;
417
tmp_field->col_name= name;
419
tmp_field->flags&= ~NOT_NULL_FLAG;
422
init_make_field(tmp_field, field_type());
426
void Item_sum::print(String *str, enum_query_type query_type)
428
str->append(func_name());
429
for (uint i=0 ; i < arg_count ; i++)
433
args[i]->print(str, query_type);
438
void Item_sum::fix_num_length_and_dec()
441
for (uint i=0 ; i < arg_count ; i++)
442
set_if_bigger(decimals,args[i]->decimals);
443
max_length=float_length(decimals);
446
Item *Item_sum::get_tmp_table_item(THD *thd)
448
Item_sum* sum_item= (Item_sum *) copy_or_same(thd);
449
if (sum_item && sum_item->result_field) // If not a const sum func
451
Field *result_field_tmp= sum_item->result_field;
452
for (uint i=0 ; i < sum_item->arg_count ; i++)
454
Item *arg= sum_item->args[i];
455
if (!arg->const_item())
457
if (arg->type() == Item::FIELD_ITEM)
458
((Item_field*) arg)->field= result_field_tmp++;
460
sum_item->args[i]= new Item_field(result_field_tmp++);
468
bool Item_sum::walk (Item_processor processor, bool walk_subquery,
473
Item **arg,**arg_end;
474
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
476
if ((*arg)->walk(processor, walk_subquery, argument))
480
return (this->*processor)(argument);
484
Field *Item_sum::create_tmp_field(bool group, TABLE *table,
485
uint convert_blob_length)
488
switch (result_type()) {
490
field= new Field_double(max_length, maybe_null, name, decimals, TRUE);
493
field= new Field_longlong(max_length, maybe_null, name, unsigned_flag);
496
if (max_length/collation.collation->mbmaxlen <= 255 ||
497
convert_blob_length > Field_varstring::MAX_SIZE ||
498
!convert_blob_length)
499
return make_string_field(table);
500
field= new Field_varstring(convert_blob_length, maybe_null,
501
name, table->s, collation.collation);
504
field= new Field_new_decimal(max_length, maybe_null, name,
505
decimals, unsigned_flag);
509
// This case should never be choosen
519
void Item_sum::update_used_tables ()
523
used_tables_cache= 0;
524
for (uint i=0 ; i < arg_count ; i++)
526
args[i]->update_used_tables();
527
used_tables_cache|= args[i]->used_tables();
530
used_tables_cache&= PSEUDO_TABLE_BITS;
532
/* the aggregate function is aggregated into its local context */
533
used_tables_cache |= (1 << aggr_sel->join->tables) - 1;
539
Item_sum_num::val_str(String *str)
541
return val_string_from_real(str);
545
my_decimal *Item_sum_num::val_decimal(my_decimal *decimal_value)
547
return val_decimal_from_real(decimal_value);
552
Item_sum_int::val_str(String *str)
554
return val_string_from_int(str);
558
my_decimal *Item_sum_int::val_decimal(my_decimal *decimal_value)
560
return val_decimal_from_int(decimal_value);
565
Item_sum_num::fix_fields(THD *thd, Item **ref)
567
DBUG_ASSERT(fixed == 0);
569
if (init_sum_func_check(thd))
574
for (uint i=0 ; i < arg_count ; i++)
576
if (args[i]->fix_fields(thd, args + i) || args[i]->check_cols(1))
578
set_if_bigger(decimals, args[i]->decimals);
579
maybe_null |= args[i]->maybe_null;
582
max_length=float_length(decimals);
584
fix_length_and_dec();
586
if (check_sum_func(thd, ref))
594
Item_sum_hybrid::Item_sum_hybrid(THD *thd, Item_sum_hybrid *item)
595
:Item_sum(thd, item), value(item->value), hybrid_type(item->hybrid_type),
596
hybrid_field_type(item->hybrid_field_type), cmp_sign(item->cmp_sign),
597
was_values(item->was_values)
599
/* copy results from old value */
600
switch (hybrid_type) {
602
sum_int= item->sum_int;
605
my_decimal2decimal(&item->sum_dec, &sum_dec);
612
This can happen with ROLLUP. Note that the value is already
613
copied at function call.
620
collation.set(item->collation);
624
Item_sum_hybrid::fix_fields(THD *thd, Item **ref)
626
DBUG_ASSERT(fixed == 0);
630
if (init_sum_func_check(thd))
633
// 'item' can be changed during fix_fields
634
if ((!item->fixed && item->fix_fields(thd, args)) ||
635
(item= args[0])->check_cols(1))
637
decimals=item->decimals;
639
switch (hybrid_type= item->result_type()) {
645
max_length= item->max_length;
646
my_decimal_set_zero(&sum_dec);
649
max_length= float_length(decimals);
653
max_length= item->max_length;
659
/* MIN/MAX can return NULL for empty set indepedent of the used column */
661
unsigned_flag=item->unsigned_flag;
662
collation.set(item->collation);
665
fix_length_and_dec();
666
item= item->real_item();
667
if (item->type() == Item::FIELD_ITEM)
668
hybrid_field_type= ((Item_field*) item)->field->type();
670
hybrid_field_type= Item::field_type();
672
if (check_sum_func(thd, ref))
679
Field *Item_sum_hybrid::create_tmp_field(bool group, TABLE *table,
680
uint convert_blob_length)
683
if (args[0]->type() == Item::FIELD_ITEM)
685
field= ((Item_field*) args[0])->field;
687
if ((field= create_tmp_field_from_field(current_thd, field, name, table,
688
NULL, convert_blob_length)))
689
field->flags&= ~NOT_NULL_FLAG;
693
DATE/TIME fields have STRING_RESULT result types.
694
In order to preserve field type, it's needed to handle DATE/TIME
695
fields creations separately.
697
switch (args[0]->field_type()) {
698
case MYSQL_TYPE_DATE:
699
field= new Field_newdate(maybe_null, name, collation.collation);
701
case MYSQL_TYPE_TIME:
702
field= new Field_time(maybe_null, name, collation.collation);
704
case MYSQL_TYPE_TIMESTAMP:
705
case MYSQL_TYPE_DATETIME:
706
field= new Field_datetime(maybe_null, name, collation.collation);
709
return Item_sum::create_tmp_field(group, table, convert_blob_length);
717
/***********************************************************************
718
** reset and add of sum_func
719
***********************************************************************/
723
check if the following assignments are really needed
725
Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item)
726
:Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
727
curr_dec_buff(item->curr_dec_buff)
729
/* TODO: check if the following assignments are really needed */
730
if (hybrid_type == DECIMAL_RESULT)
732
my_decimal2decimal(item->dec_buffs, dec_buffs);
733
my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
739
Item *Item_sum_sum::copy_or_same(THD* thd)
741
return new (thd->mem_root) Item_sum_sum(thd, this);
745
void Item_sum_sum::clear()
747
DBUG_ENTER("Item_sum_sum::clear");
749
if (hybrid_type == DECIMAL_RESULT)
752
my_decimal_set_zero(dec_buffs);
760
void Item_sum_sum::fix_length_and_dec()
762
DBUG_ENTER("Item_sum_sum::fix_length_and_dec");
763
maybe_null=null_value=1;
764
decimals= args[0]->decimals;
765
switch (args[0]->result_type()) {
768
hybrid_type= REAL_RESULT;
774
/* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
775
int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
776
max_length= my_decimal_precision_to_length(precision, decimals,
779
hybrid_type= DECIMAL_RESULT;
780
my_decimal_set_zero(dec_buffs);
787
DBUG_PRINT("info", ("Type: %s (%d, %d)",
788
(hybrid_type == REAL_RESULT ? "REAL_RESULT" :
789
hybrid_type == DECIMAL_RESULT ? "DECIMAL_RESULT" :
790
hybrid_type == INT_RESULT ? "INT_RESULT" :
798
bool Item_sum_sum::add()
800
DBUG_ENTER("Item_sum_sum::add");
801
if (hybrid_type == DECIMAL_RESULT)
803
my_decimal value, *val= args[0]->val_decimal(&value);
804
if (!args[0]->null_value)
806
my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
807
val, dec_buffs + curr_dec_buff);
814
sum+= args[0]->val_real();
815
if (!args[0]->null_value)
822
longlong Item_sum_sum::val_int()
824
DBUG_ASSERT(fixed == 1);
825
if (hybrid_type == DECIMAL_RESULT)
828
my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
832
return (longlong) rint(val_real());
836
double Item_sum_sum::val_real()
838
DBUG_ASSERT(fixed == 1);
839
if (hybrid_type == DECIMAL_RESULT)
840
my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
845
String *Item_sum_sum::val_str(String *str)
847
if (hybrid_type == DECIMAL_RESULT)
848
return val_string_from_decimal(str);
849
return val_string_from_real(str);
853
my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
855
if (hybrid_type == DECIMAL_RESULT)
856
return (dec_buffs + curr_dec_buff);
857
return val_decimal_from_real(val);
860
/***************************************************************************/
864
/* Declarations for auxilary C-callbacks */
866
static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
868
return memcmp(key1, key2, *(uint *) arg);
872
static int item_sum_distinct_walk(void *element, element_count num_of_dups,
875
return ((Item_sum_distinct*) (item))->unique_walk_function(element);
880
/* Item_sum_distinct */
882
Item_sum_distinct::Item_sum_distinct(Item *item_arg)
883
:Item_sum_num(item_arg), tree(0)
886
quick_group is an optimizer hint, which means that GROUP BY can be
887
handled with help of index on grouped columns.
888
By setting quick_group to zero we force creation of temporary table
895
Item_sum_distinct::Item_sum_distinct(THD *thd, Item_sum_distinct *original)
896
:Item_sum_num(thd, original), val(original->val), tree(0),
897
table_field_type(original->table_field_type)
904
Behaves like an Integer except to fix_length_and_dec().
905
Additionally div() converts val with this traits to a val with true
906
decimal traits along with conversion of integer value to decimal value.
907
This is to speedup SUM/AVG(DISTINCT) evaluation for 8-32 bit integer
910
struct Hybrid_type_traits_fast_decimal: public
911
Hybrid_type_traits_integer
913
virtual Item_result type() const { return DECIMAL_RESULT; }
914
virtual void fix_length_and_dec(Item *item, Item *arg) const
915
{ Hybrid_type_traits_decimal::instance()->fix_length_and_dec(item, arg); }
917
virtual void div(Hybrid_type *val, ulonglong u) const
919
int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
920
val->used_dec_buf_no= 0;
921
val->traits= Hybrid_type_traits_decimal::instance();
922
val->traits->div(val, u);
924
static const Hybrid_type_traits_fast_decimal *instance();
925
Hybrid_type_traits_fast_decimal() {};
928
static const Hybrid_type_traits_fast_decimal fast_decimal_traits_instance;
930
const Hybrid_type_traits_fast_decimal
931
*Hybrid_type_traits_fast_decimal::instance()
933
return &fast_decimal_traits_instance;
936
void Item_sum_distinct::fix_length_and_dec()
938
DBUG_ASSERT(args[0]->fixed);
940
table_field_type= args[0]->field_type();
942
/* Adjust tmp table type according to the chosen aggregation type */
943
switch (args[0]->result_type()) {
946
val.traits= Hybrid_type_traits::instance();
947
if (table_field_type != MYSQL_TYPE_FLOAT)
948
table_field_type= MYSQL_TYPE_DOUBLE;
952
Preserving int8, int16, int32 field types gives ~10% performance boost
953
as the size of result tree becomes significantly smaller.
954
Another speed up we gain by using longlong for intermediate
955
calculations. The range of int64 is enough to hold sum 2^32 distinct
956
integers each <= 2^32.
958
if (table_field_type == MYSQL_TYPE_INT24 ||
959
(table_field_type >= MYSQL_TYPE_TINY && table_field_type <= MYSQL_TYPE_LONG))
961
val.traits= Hybrid_type_traits_fast_decimal::instance();
964
table_field_type= MYSQL_TYPE_LONGLONG;
967
val.traits= Hybrid_type_traits_decimal::instance();
968
if (table_field_type != MYSQL_TYPE_LONGLONG)
969
table_field_type= MYSQL_TYPE_NEWDECIMAL;
975
val.traits->fix_length_and_dec(this, args[0]);
981
check that the case of CHAR(0) works OK
983
bool Item_sum_distinct::setup(THD *thd)
985
List<Create_field> field_list;
986
Create_field field_def; /* field definition */
987
DBUG_ENTER("Item_sum_distinct::setup");
988
/* It's legal to call setup() more than once when in a subquery */
993
Virtual table and the tree are created anew on each re-execution of
994
PS/SP. Hence all further allocations are performed in the runtime
997
if (field_list.push_back(&field_def))
1000
null_value= maybe_null= 1;
1003
DBUG_ASSERT(args[0]->fixed);
1005
field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
1006
args[0]->decimals, args[0]->maybe_null,
1007
args[0]->unsigned_flag);
1009
if (! (table= create_virtual_tmp_table(thd, field_list)))
1012
/* XXX: check that the case of CHAR(0) works OK */
1013
tree_key_length= table->s->reclength - table->s->null_bytes;
1016
Unique handles all unique elements in a tree until they can't fit
1017
in. Then the tree is dumped to the temporary file. We can use
1018
simple_raw_key_cmp because the table contains numbers only; decimals
1019
are converted to binary representation as well.
1021
tree= new Unique(simple_raw_key_cmp, &tree_key_length, tree_key_length,
1022
thd->variables.max_heap_table_size);
1024
is_evaluated= FALSE;
1025
DBUG_RETURN(tree == 0);
1029
bool Item_sum_distinct::add()
1031
args[0]->save_in_field(table->field[0], FALSE);
1032
is_evaluated= FALSE;
1033
if (!table->field[0]->is_null())
1038
'0' values are also stored in the tree. This doesn't matter
1039
for SUM(DISTINCT), but is important for AVG(DISTINCT)
1041
return tree->unique_add(table->field[0]->ptr);
1047
bool Item_sum_distinct::unique_walk_function(void *element)
1049
memcpy(table->field[0]->ptr, element, tree_key_length);
1051
val.traits->add(&val, table->field[0]);
1056
void Item_sum_distinct::clear()
1058
DBUG_ENTER("Item_sum_distinct::clear");
1059
DBUG_ASSERT(tree != 0); /* we always have a tree */
1062
is_evaluated= FALSE;
1066
void Item_sum_distinct::cleanup()
1068
Item_sum_num::cleanup();
1072
is_evaluated= FALSE;
1075
Item_sum_distinct::~Item_sum_distinct()
1078
/* no need to free the table */
1082
void Item_sum_distinct::calculate_val_and_count()
1087
val.traits->set_zero(&val);
1089
We don't have a tree only if 'setup()' hasn't been called;
1090
this is the case of sql_select.cc:return_zero_rows.
1094
table->field[0]->set_notnull();
1095
tree->walk(item_sum_distinct_walk, (void*) this);
1102
double Item_sum_distinct::val_real()
1104
calculate_val_and_count();
1105
return val.traits->val_real(&val);
1109
my_decimal *Item_sum_distinct::val_decimal(my_decimal *to)
1111
calculate_val_and_count();
1114
return val.traits->val_decimal(&val, to);
1118
longlong Item_sum_distinct::val_int()
1120
calculate_val_and_count();
1121
return val.traits->val_int(&val, unsigned_flag);
1125
String *Item_sum_distinct::val_str(String *str)
1127
calculate_val_and_count();
1130
return val.traits->val_str(&val, str, decimals);
1133
/* end of Item_sum_distinct */
1135
/* Item_sum_avg_distinct */
1138
Item_sum_avg_distinct::fix_length_and_dec()
1140
Item_sum_distinct::fix_length_and_dec();
1141
prec_increment= current_thd->variables.div_precincrement;
1143
AVG() will divide val by count. We need to reserve digits
1144
after decimal point as the result can be fractional.
1146
decimals= min(decimals + prec_increment, NOT_FIXED_DEC);
1151
Item_sum_avg_distinct::calculate_val_and_count()
1155
Item_sum_distinct::calculate_val_and_count();
1157
val.traits->div(&val, count);
1163
Item *Item_sum_count::copy_or_same(THD* thd)
1165
return new (thd->mem_root) Item_sum_count(thd, this);
1169
void Item_sum_count::clear()
1175
bool Item_sum_count::add()
1177
if (!args[0]->maybe_null || !args[0]->is_null())
1182
longlong Item_sum_count::val_int()
1184
DBUG_ASSERT(fixed == 1);
1185
return (longlong) count;
1189
void Item_sum_count::cleanup()
1191
DBUG_ENTER("Item_sum_count::cleanup");
1193
Item_sum_int::cleanup();
1201
void Item_sum_avg::fix_length_and_dec()
1203
Item_sum_sum::fix_length_and_dec();
1204
maybe_null=null_value=1;
1205
prec_increment= current_thd->variables.div_precincrement;
1206
if (hybrid_type == DECIMAL_RESULT)
1208
int precision= args[0]->decimal_precision() + prec_increment;
1209
decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1210
max_length= my_decimal_precision_to_length(precision, decimals,
1212
f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1213
f_scale= args[0]->decimals;
1214
dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1217
decimals= min(args[0]->decimals + prec_increment, NOT_FIXED_DEC);
1218
max_length= args[0]->max_length + prec_increment;
1223
Item *Item_sum_avg::copy_or_same(THD* thd)
1225
return new (thd->mem_root) Item_sum_avg(thd, this);
1229
Field *Item_sum_avg::create_tmp_field(bool group, TABLE *table,
1230
uint convert_blob_len)
1236
We must store both value and counter in the temporary table in one field.
1237
The easiest way is to do this is to store both value in a string
1238
and unpack on access.
1240
field= new Field_string(((hybrid_type == DECIMAL_RESULT) ?
1241
dec_bin_size : sizeof(double)) + sizeof(longlong),
1242
0, name, &my_charset_bin);
1244
else if (hybrid_type == DECIMAL_RESULT)
1245
field= new Field_new_decimal(max_length, maybe_null, name,
1246
decimals, unsigned_flag);
1248
field= new Field_double(max_length, maybe_null, name, decimals, TRUE);
1255
void Item_sum_avg::clear()
1257
Item_sum_sum::clear();
1262
bool Item_sum_avg::add()
1264
if (Item_sum_sum::add())
1266
if (!args[0]->null_value)
1271
double Item_sum_avg::val_real()
1273
DBUG_ASSERT(fixed == 1);
1279
return Item_sum_sum::val_real() / ulonglong2double(count);
1283
my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
1285
my_decimal sum_buff, cnt;
1286
const my_decimal *sum_dec;
1287
DBUG_ASSERT(fixed == 1);
1295
For non-DECIMAL hybrid_type the division will be done in
1296
Item_sum_avg::val_real().
1298
if (hybrid_type != DECIMAL_RESULT)
1299
return val_decimal_from_real(val);
1301
sum_dec= dec_buffs + curr_dec_buff;
1302
int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
1303
my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1308
String *Item_sum_avg::val_str(String *str)
1310
if (hybrid_type == DECIMAL_RESULT)
1311
return val_string_from_decimal(str);
1312
return val_string_from_real(str);
1320
double Item_sum_std::val_real()
1322
DBUG_ASSERT(fixed == 1);
1323
double nr= Item_sum_variance::val_real();
1324
DBUG_ASSERT(nr >= 0.0);
1328
Item *Item_sum_std::copy_or_same(THD* thd)
1330
return new (thd->mem_root) Item_sum_std(thd, this);
1340
Variance implementation for floating-point implementations, without
1341
catastrophic cancellation, from Knuth's _TAoCP_, 3rd ed, volume 2, pg232.
1342
This alters the value at m, s, and increments count.
1346
These two functions are used by the Item_sum_variance and the
1347
Item_variance_field classes, which are unrelated, and each need to calculate
1348
variance. The difference between the two classes is that the first is used
1349
for a mundane SELECT, while the latter is used in a GROUPing SELECT.
1351
static void variance_fp_recurrence_next(double *m, double *s, ulonglong *count, double nr)
1362
double m_kminusone= *m;
1363
*m= m_kminusone + (nr - m_kminusone) / (double) *count;
1364
*s= *s + (nr - m_kminusone) * (nr - *m);
1369
static double variance_fp_recurrence_result(double s, ulonglong count, bool is_sample_variance)
1374
if (is_sample_variance)
1375
return s / (count - 1);
1377
/* else, is a population variance */
1382
Item_sum_variance::Item_sum_variance(THD *thd, Item_sum_variance *item):
1383
Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
1384
count(item->count), sample(item->sample),
1385
prec_increment(item->prec_increment)
1387
recurrence_m= item->recurrence_m;
1388
recurrence_s= item->recurrence_s;
1392
void Item_sum_variance::fix_length_and_dec()
1394
DBUG_ENTER("Item_sum_variance::fix_length_and_dec");
1395
maybe_null= null_value= 1;
1396
prec_increment= current_thd->variables.div_precincrement;
1399
According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1400
aggregate function; paragraph 7h of Syntax Rules), "the declared
1401
type of the result is an implementation-defined aproximate numeric
1404
hybrid_type= REAL_RESULT;
1406
switch (args[0]->result_type()) {
1409
decimals= min(args[0]->decimals + 4, NOT_FIXED_DEC);
1412
case DECIMAL_RESULT:
1414
int precision= args[0]->decimal_precision()*2 + prec_increment;
1415
decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1416
max_length= my_decimal_precision_to_length(precision, decimals,
1425
DBUG_PRINT("info", ("Type: REAL_RESULT (%d, %d)", max_length, (int)decimals));
1430
Item *Item_sum_variance::copy_or_same(THD* thd)
1432
return new (thd->mem_root) Item_sum_variance(thd, this);
1437
Create a new field to match the type of value we're expected to yield.
1438
If we're grouping, then we need some space to serialize variables into, to
1441
Field *Item_sum_variance::create_tmp_field(bool group, TABLE *table,
1442
uint convert_blob_len)
1448
We must store both value and counter in the temporary table in one field.
1449
The easiest way is to do this is to store both value in a string
1450
and unpack on access.
1452
field= new Field_string(sizeof(double)*2 + sizeof(longlong), 0, name, &my_charset_bin);
1455
field= new Field_double(max_length, maybe_null, name, decimals, TRUE);
1464
void Item_sum_variance::clear()
1469
bool Item_sum_variance::add()
1472
Why use a temporary variable? We don't know if it is null until we
1473
evaluate it, which has the side-effect of setting null_value .
1475
double nr= args[0]->val_real();
1477
if (!args[0]->null_value)
1478
variance_fp_recurrence_next(&recurrence_m, &recurrence_s, &count, nr);
1482
double Item_sum_variance::val_real()
1484
DBUG_ASSERT(fixed == 1);
1487
'sample' is a 1/0 boolean value. If it is 1/true, id est this is a sample
1488
variance call, then we should set nullness when the count of the items
1489
is one or zero. If it's zero, i.e. a population variance, then we only
1490
set nullness when the count is zero.
1492
Another way to read it is that 'sample' is the numerical threshhold, at and
1493
below which a 'count' number of items is called NULL.
1495
DBUG_ASSERT((sample == 0) || (sample == 1));
1496
if (count <= sample)
1503
return variance_fp_recurrence_result(recurrence_s, count, sample);
1507
my_decimal *Item_sum_variance::val_decimal(my_decimal *dec_buf)
1509
DBUG_ASSERT(fixed == 1);
1510
return val_decimal_from_real(dec_buf);
1514
void Item_sum_variance::reset_field()
1517
uchar *res= result_field->ptr;
1519
nr= args[0]->val_real(); /* sets null_value as side-effect */
1521
if (args[0]->null_value)
1522
bzero(res,sizeof(double)*2+sizeof(longlong));
1525
/* Serialize format is (double)m, (double)s, (longlong)count */
1526
ulonglong tmp_count;
1528
float8store(res, nr); /* recurrence variable m */
1530
float8store(res + sizeof(double), tmp_s);
1532
int8store(res + sizeof(double)*2, tmp_count);
1537
void Item_sum_variance::update_field()
1539
ulonglong field_count;
1540
uchar *res=result_field->ptr;
1542
double nr= args[0]->val_real(); /* sets null_value as side-effect */
1544
if (args[0]->null_value)
1547
/* Serialize format is (double)m, (double)s, (longlong)count */
1548
double field_recurrence_m, field_recurrence_s;
1549
float8get(field_recurrence_m, res);
1550
float8get(field_recurrence_s, res + sizeof(double));
1551
field_count=sint8korr(res+sizeof(double)*2);
1553
variance_fp_recurrence_next(&field_recurrence_m, &field_recurrence_s, &field_count, nr);
1555
float8store(res, field_recurrence_m);
1556
float8store(res + sizeof(double), field_recurrence_s);
1557
res+= sizeof(double)*2;
1558
int8store(res,field_count);
1564
void Item_sum_hybrid::clear()
1566
switch (hybrid_type) {
1570
case DECIMAL_RESULT:
1571
my_decimal_set_zero(&sum_dec);
1582
double Item_sum_hybrid::val_real()
1584
DBUG_ASSERT(fixed == 1);
1587
switch (hybrid_type) {
1592
String *res; res=val_str(&str_value);
1593
return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1594
&end_not_used, &err_not_used) : 0.0);
1598
return ulonglong2double(sum_int);
1599
return (double) sum_int;
1600
case DECIMAL_RESULT:
1601
my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1607
// This case should never be choosen
1613
longlong Item_sum_hybrid::val_int()
1615
DBUG_ASSERT(fixed == 1);
1618
switch (hybrid_type) {
1621
case DECIMAL_RESULT:
1624
my_decimal2int(E_DEC_FATAL_ERROR, &sum_dec, unsigned_flag, &result);
1628
return (longlong) rint(Item_sum_hybrid::val_real());
1633
my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
1635
DBUG_ASSERT(fixed == 1);
1638
switch (hybrid_type) {
1640
string2my_decimal(E_DEC_FATAL_ERROR, &value, val);
1643
double2my_decimal(E_DEC_FATAL_ERROR, sum, val);
1645
case DECIMAL_RESULT:
1649
int2my_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1653
// This case should never be choosen
1657
return val; // Keep compiler happy
1662
Item_sum_hybrid::val_str(String *str)
1664
DBUG_ASSERT(fixed == 1);
1667
switch (hybrid_type) {
1671
str->set_real(sum,decimals, &my_charset_bin);
1673
case DECIMAL_RESULT:
1674
my_decimal2string(E_DEC_FATAL_ERROR, &sum_dec, 0, 0, 0, str);
1677
str->set_int(sum_int, unsigned_flag, &my_charset_bin);
1681
// This case should never be choosen
1685
return str; // Keep compiler happy
1689
void Item_sum_hybrid::cleanup()
1691
DBUG_ENTER("Item_sum_hybrid::cleanup");
1692
Item_sum::cleanup();
1693
forced_const= FALSE;
1696
by default it is TRUE to avoid TRUE reporting by
1697
Item_func_not_all/Item_func_nop_all if this item was never called.
1699
no_rows_in_result() set it to FALSE if was not results found.
1700
If some results found it will be left unchanged.
1706
void Item_sum_hybrid::no_rows_in_result()
1713
Item *Item_sum_min::copy_or_same(THD* thd)
1715
return new (thd->mem_root) Item_sum_min(thd, this);
1719
bool Item_sum_min::add()
1721
switch (hybrid_type) {
1724
String *result=args[0]->val_str(&tmp_value);
1725
if (!args[0]->null_value &&
1726
(null_value || sortcmp(&value,result,collation.collation) > 0))
1728
value.copy(*result);
1735
longlong nr=args[0]->val_int();
1736
if (!args[0]->null_value && (null_value ||
1738
(ulonglong) nr < (ulonglong) sum_int) ||
1739
(!unsigned_flag && nr < sum_int)))
1746
case DECIMAL_RESULT:
1748
my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1749
if (!args[0]->null_value &&
1750
(null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
1752
my_decimal2decimal(val, &sum_dec);
1759
double nr= args[0]->val_real();
1760
if (!args[0]->null_value && (null_value || nr < sum))
1769
// This case should never be choosen
1777
Item *Item_sum_max::copy_or_same(THD* thd)
1779
return new (thd->mem_root) Item_sum_max(thd, this);
1783
bool Item_sum_max::add()
1785
switch (hybrid_type) {
1788
String *result=args[0]->val_str(&tmp_value);
1789
if (!args[0]->null_value &&
1790
(null_value || sortcmp(&value,result,collation.collation) < 0))
1792
value.copy(*result);
1799
longlong nr=args[0]->val_int();
1800
if (!args[0]->null_value && (null_value ||
1802
(ulonglong) nr > (ulonglong) sum_int) ||
1803
(!unsigned_flag && nr > sum_int)))
1810
case DECIMAL_RESULT:
1812
my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1813
if (!args[0]->null_value &&
1814
(null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
1816
my_decimal2decimal(val, &sum_dec);
1823
double nr= args[0]->val_real();
1824
if (!args[0]->null_value && (null_value || nr > sum))
1833
// This case should never be choosen
1841
/* bit_or and bit_and */
1843
longlong Item_sum_bit::val_int()
1845
DBUG_ASSERT(fixed == 1);
1846
return (longlong) bits;
1850
void Item_sum_bit::clear()
1855
Item *Item_sum_or::copy_or_same(THD* thd)
1857
return new (thd->mem_root) Item_sum_or(thd, this);
1861
bool Item_sum_or::add()
1863
ulonglong value= (ulonglong) args[0]->val_int();
1864
if (!args[0]->null_value)
1869
Item *Item_sum_xor::copy_or_same(THD* thd)
1871
return new (thd->mem_root) Item_sum_xor(thd, this);
1875
bool Item_sum_xor::add()
1877
ulonglong value= (ulonglong) args[0]->val_int();
1878
if (!args[0]->null_value)
1883
Item *Item_sum_and::copy_or_same(THD* thd)
1885
return new (thd->mem_root) Item_sum_and(thd, this);
1889
bool Item_sum_and::add()
1891
ulonglong value= (ulonglong) args[0]->val_int();
1892
if (!args[0]->null_value)
1897
/************************************************************************
1898
** reset result of a Item_sum with is saved in a tmp_table
1899
*************************************************************************/
1901
void Item_sum_num::reset_field()
1903
double nr= args[0]->val_real();
1904
uchar *res=result_field->ptr;
1908
if (args[0]->null_value)
1911
result_field->set_null();
1914
result_field->set_notnull();
1916
float8store(res,nr);
1920
void Item_sum_hybrid::reset_field()
1922
switch(hybrid_type) {
1925
char buff[MAX_FIELD_WIDTH];
1926
String tmp(buff,sizeof(buff),result_field->charset()),*res;
1928
res=args[0]->val_str(&tmp);
1929
if (args[0]->null_value)
1931
result_field->set_null();
1932
result_field->reset();
1936
result_field->set_notnull();
1937
result_field->store(res->ptr(),res->length(),tmp.charset());
1943
longlong nr=args[0]->val_int();
1947
if (args[0]->null_value)
1950
result_field->set_null();
1953
result_field->set_notnull();
1955
result_field->store(nr, unsigned_flag);
1960
double nr= args[0]->val_real();
1964
if (args[0]->null_value)
1967
result_field->set_null();
1970
result_field->set_notnull();
1972
result_field->store(nr);
1975
case DECIMAL_RESULT:
1977
my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
1981
if (args[0]->null_value)
1982
result_field->set_null();
1984
result_field->set_notnull();
1987
We must store zero in the field as we will use the field value in
1990
if (!arg_dec) // Null
1991
arg_dec= &decimal_zero;
1992
result_field->store_decimal(arg_dec);
2002
void Item_sum_sum::reset_field()
2004
if (hybrid_type == DECIMAL_RESULT)
2006
my_decimal value, *arg_val= args[0]->val_decimal(&value);
2007
if (!arg_val) // Null
2008
arg_val= &decimal_zero;
2009
result_field->store_decimal(arg_val);
2013
DBUG_ASSERT(hybrid_type == REAL_RESULT);
2014
double nr= args[0]->val_real(); // Nulls also return 0
2015
float8store(result_field->ptr, nr);
2017
if (args[0]->null_value)
2018
result_field->set_null();
2020
result_field->set_notnull();
2024
void Item_sum_count::reset_field()
2026
uchar *res=result_field->ptr;
2029
if (!args[0]->maybe_null || !args[0]->is_null())
2035
void Item_sum_avg::reset_field()
2037
uchar *res=result_field->ptr;
2038
if (hybrid_type == DECIMAL_RESULT)
2041
my_decimal value, *arg_dec= args[0]->val_decimal(&value);
2042
if (args[0]->null_value)
2044
arg_dec= &decimal_zero;
2049
my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
2051
int8store(res, tmp);
2055
double nr= args[0]->val_real();
2057
if (args[0]->null_value)
2058
bzero(res,sizeof(double)+sizeof(longlong));
2062
float8store(res,nr);
2063
res+=sizeof(double);
2070
void Item_sum_bit::reset_field()
2073
int8store(result_field->ptr, bits);
2076
void Item_sum_bit::update_field()
2078
uchar *res=result_field->ptr;
2079
bits= uint8korr(res);
2081
int8store(res, bits);
2086
calc next value and merge it with field_value.
2089
void Item_sum_sum::update_field()
2091
if (hybrid_type == DECIMAL_RESULT)
2093
my_decimal value, *arg_val= args[0]->val_decimal(&value);
2094
if (!args[0]->null_value)
2096
if (!result_field->is_null())
2098
my_decimal field_value,
2099
*field_val= result_field->val_decimal(&field_value);
2100
my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2101
result_field->store_decimal(dec_buffs);
2105
result_field->store_decimal(arg_val);
2106
result_field->set_notnull();
2113
uchar *res=result_field->ptr;
2115
float8get(old_nr,res);
2116
nr= args[0]->val_real();
2117
if (!args[0]->null_value)
2120
result_field->set_notnull();
2122
float8store(res,old_nr);
2127
void Item_sum_count::update_field()
2130
uchar *res=result_field->ptr;
2133
if (!args[0]->maybe_null || !args[0]->is_null())
2139
void Item_sum_avg::update_field()
2141
longlong field_count;
2142
uchar *res=result_field->ptr;
2143
if (hybrid_type == DECIMAL_RESULT)
2145
my_decimal value, *arg_val= args[0]->val_decimal(&value);
2146
if (!args[0]->null_value)
2148
binary2my_decimal(E_DEC_FATAL_ERROR, res,
2149
dec_buffs + 1, f_precision, f_scale);
2150
field_count= sint8korr(res + dec_bin_size);
2151
my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
2152
my_decimal2binary(E_DEC_FATAL_ERROR, dec_buffs,
2153
res, f_precision, f_scale);
2156
int8store(res, field_count);
2163
nr= args[0]->val_real();
2164
if (!args[0]->null_value)
2167
float8get(old_nr, res);
2168
field_count= sint8korr(res + sizeof(double));
2170
float8store(res,old_nr);
2171
res+= sizeof(double);
2173
int8store(res, field_count);
2179
void Item_sum_hybrid::update_field()
2181
switch (hybrid_type) {
2183
min_max_update_str_field();
2186
min_max_update_int_field();
2188
case DECIMAL_RESULT:
2189
min_max_update_decimal_field();
2192
min_max_update_real_field();
2198
Item_sum_hybrid::min_max_update_str_field()
2200
String *res_str=args[0]->val_str(&value);
2202
if (!args[0]->null_value)
2204
result_field->val_str(&tmp_value);
2206
if (result_field->is_null() ||
2207
(cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2208
result_field->store(res_str->ptr(),res_str->length(),res_str->charset());
2209
result_field->set_notnull();
2215
Item_sum_hybrid::min_max_update_real_field()
2219
old_nr=result_field->val_real();
2220
nr= args[0]->val_real();
2221
if (!args[0]->null_value)
2223
if (result_field->is_null(0) ||
2224
(cmp_sign > 0 ? old_nr > nr : old_nr < nr))
2226
result_field->set_notnull();
2228
else if (result_field->is_null(0))
2229
result_field->set_null();
2230
result_field->store(old_nr);
2235
Item_sum_hybrid::min_max_update_int_field()
2239
old_nr=result_field->val_int();
2240
nr=args[0]->val_int();
2241
if (!args[0]->null_value)
2243
if (result_field->is_null(0))
2247
bool res=(unsigned_flag ?
2248
(ulonglong) old_nr > (ulonglong) nr :
2250
/* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2251
if ((cmp_sign > 0) ^ (!res))
2254
result_field->set_notnull();
2256
else if (result_field->is_null(0))
2257
result_field->set_null();
2258
result_field->store(old_nr, unsigned_flag);
2264
optimize: do not get result_field in case of args[0] is NULL
2267
Item_sum_hybrid::min_max_update_decimal_field()
2269
/* TODO: optimize: do not get result_field in case of args[0] is NULL */
2270
my_decimal old_val, nr_val;
2271
const my_decimal *old_nr= result_field->val_decimal(&old_val);
2272
const my_decimal *nr= args[0]->val_decimal(&nr_val);
2273
if (!args[0]->null_value)
2275
if (result_field->is_null(0))
2279
bool res= my_decimal_cmp(old_nr, nr) > 0;
2280
/* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2281
if ((cmp_sign > 0) ^ (!res))
2284
result_field->set_notnull();
2286
else if (result_field->is_null(0))
2287
result_field->set_null();
2288
result_field->store_decimal(old_nr);
2292
Item_avg_field::Item_avg_field(Item_result res_type, Item_sum_avg *item)
2295
decimals=item->decimals;
2296
max_length= item->max_length;
2297
unsigned_flag= item->unsigned_flag;
2298
field=item->result_field;
2300
hybrid_type= res_type;
2301
prec_increment= item->prec_increment;
2302
if (hybrid_type == DECIMAL_RESULT)
2304
f_scale= item->f_scale;
2305
f_precision= item->f_precision;
2306
dec_bin_size= item->dec_bin_size;
2310
double Item_avg_field::val_real()
2312
// fix_fields() never calls for this Item
2317
if (hybrid_type == DECIMAL_RESULT)
2318
return val_real_from_decimal();
2320
float8get(nr,field->ptr);
2321
res= (field->ptr+sizeof(double));
2322
count= sint8korr(res);
2324
if ((null_value= !count))
2326
return nr/(double) count;
2330
longlong Item_avg_field::val_int()
2332
return (longlong) rint(val_real());
2336
my_decimal *Item_avg_field::val_decimal(my_decimal *dec_buf)
2338
// fix_fields() never calls for this Item
2339
if (hybrid_type == REAL_RESULT)
2340
return val_decimal_from_real(dec_buf);
2342
longlong count= sint8korr(field->ptr + dec_bin_size);
2343
if ((null_value= !count))
2346
my_decimal dec_count, dec_field;
2347
binary2my_decimal(E_DEC_FATAL_ERROR,
2348
field->ptr, &dec_field, f_precision, f_scale);
2349
int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
2350
my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2351
&dec_field, &dec_count, prec_increment);
2356
String *Item_avg_field::val_str(String *str)
2358
// fix_fields() never calls for this Item
2359
if (hybrid_type == DECIMAL_RESULT)
2360
return val_string_from_decimal(str);
2361
return val_string_from_real(str);
2365
Item_std_field::Item_std_field(Item_sum_std *item)
2366
: Item_variance_field(item)
2371
double Item_std_field::val_real()
2374
// fix_fields() never calls for this Item
2375
nr= Item_variance_field::val_real();
2376
DBUG_ASSERT(nr >= 0.0);
2381
my_decimal *Item_std_field::val_decimal(my_decimal *dec_buf)
2384
We can't call val_decimal_from_real() for DECIMAL_RESULT as
2385
Item_variance_field::val_real() would cause an infinite loop
2387
my_decimal tmp_dec, *dec;
2389
if (hybrid_type == REAL_RESULT)
2390
return val_decimal_from_real(dec_buf);
2392
dec= Item_variance_field::val_decimal(dec_buf);
2395
my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2396
DBUG_ASSERT(nr >= 0.0);
2398
double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2399
my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, FALSE, dec_buf);
2404
Item_variance_field::Item_variance_field(Item_sum_variance *item)
2407
decimals=item->decimals;
2408
max_length=item->max_length;
2409
unsigned_flag= item->unsigned_flag;
2410
field=item->result_field;
2412
sample= item->sample;
2413
prec_increment= item->prec_increment;
2414
if ((hybrid_type= item->hybrid_type) == DECIMAL_RESULT)
2416
f_scale0= item->f_scale0;
2417
f_precision0= item->f_precision0;
2418
dec_bin_size0= item->dec_bin_size0;
2419
f_scale1= item->f_scale1;
2420
f_precision1= item->f_precision1;
2421
dec_bin_size1= item->dec_bin_size1;
2426
double Item_variance_field::val_real()
2428
// fix_fields() never calls for this Item
2429
if (hybrid_type == DECIMAL_RESULT)
2430
return val_real_from_decimal();
2432
double recurrence_s;
2434
float8get(recurrence_s, (field->ptr + sizeof(double)));
2435
count=sint8korr(field->ptr+sizeof(double)*2);
2437
if ((null_value= (count <= sample)))
2440
return variance_fp_recurrence_result(recurrence_s, count, sample);
2444
/****************************************************************************
2445
** COUNT(DISTINCT ...)
2446
****************************************************************************/
2448
int simple_str_key_cmp(void* arg, uchar* key1, uchar* key2)
2450
Field *f= (Field*) arg;
2451
return f->cmp(key1, key2);
2455
Did not make this one static - at least gcc gets confused when
2456
I try to declare a static function as a friend. If you can figure
2457
out the syntax to make a static function a friend, make this one
2461
int composite_key_cmp(void* arg, uchar* key1, uchar* key2)
2463
Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2464
Field **field = item->table->field;
2465
Field **field_end= field + item->table->s->fields;
2466
uint32 *lengths=item->field_lengths;
2467
for (; field < field_end; ++field)
2470
int len = *lengths++;
2471
int res = f->cmp(key1, key2);
2483
static int count_distinct_walk(void *elem, element_count count, void *arg)
2485
(*((ulonglong*)arg))++;
2492
void Item_sum_count_distinct::cleanup()
2494
DBUG_ENTER("Item_sum_count_distinct::cleanup");
2495
Item_sum_int::cleanup();
2497
/* Free objects only if we own them. */
2501
We need to delete the table and the tree in cleanup() as
2502
they were allocated in the runtime memroot. Using the runtime
2503
memroot reduces memory footprint for PS/SP and simplifies setup().
2507
is_evaluated= FALSE;
2510
free_tmp_table(table->in_use, table);
2513
delete tmp_table_param;
2522
This is used by rollup to create a separate usable copy of
2526
void Item_sum_count_distinct::make_unique()
2530
force_copy_fields= 1;
2532
is_evaluated= FALSE;
2538
Item_sum_count_distinct::~Item_sum_count_distinct()
2544
bool Item_sum_count_distinct::setup(THD *thd)
2547
SELECT_LEX *select_lex= thd->lex->current_select;
2550
Setup can be called twice for ROLLUP items. This is a bug.
2551
Please add DBUG_ASSERT(tree == 0) here when it's fixed.
2552
It's legal to call setup() more than once when in a subquery
2554
if (tree || table || tmp_table_param)
2557
if (!(tmp_table_param= new TMP_TABLE_PARAM))
2560
/* Create a table with an unique key over all parameters */
2561
for (uint i=0; i < arg_count ; i++)
2564
if (list.push_back(item))
2565
return TRUE; // End of memory
2566
if (item->const_item() && item->is_null())
2571
count_field_types(select_lex, tmp_table_param, list, 0);
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)
2591
if (!(table= create_tmp_table(thd, tmp_table_param, list, (ORDER*) 0, 1,
2593
(select_lex->options | thd->options),
2594
HA_POS_ERROR, (char*)"")))
2596
table->file->extra(HA_EXTRA_NO_ROWS); // Don't update rows
2599
if (table->s->db_type() == heap_hton)
2602
No blobs, otherwise it would have been MyISAM: set up a compare
2603
function and its arguments to use with Unique.
2605
qsort_cmp2 compare_key;
2607
Field **field= table->field;
2608
Field **field_end= field + table->s->fields;
2609
bool all_binary= TRUE;
2611
for (tree_key_length= 0; field < field_end; ++field)
2614
enum enum_field_types f_type= f->type();
2615
tree_key_length+= f->pack_length();
2616
if ((f_type == MYSQL_TYPE_VARCHAR) || (!f->binary() && (f_type == MYSQL_TYPE_STRING || f_type == MYSQL_TYPE_VAR_STRING)))
2624
cmp_arg= (void*) &tree_key_length;
2625
compare_key= (qsort_cmp2) simple_raw_key_cmp;
2629
if (table->s->fields == 1)
2632
If we have only one field, which is the most common use of
2633
count(distinct), it is much faster to use a simpler key
2634
compare method that can take advantage of not having to worry
2637
compare_key= (qsort_cmp2) simple_str_key_cmp;
2638
cmp_arg= (void*) table->field[0];
2639
/* tree_key_length has been set already */
2644
compare_key= (qsort_cmp2) composite_key_cmp;
2645
cmp_arg= (void*) this;
2646
field_lengths= (uint32*) thd->alloc(table->s->fields * sizeof(uint32));
2647
for (tree_key_length= 0, length= field_lengths, field= table->field;
2648
field < field_end; ++field, ++length)
2650
*length= (*field)->pack_length();
2651
tree_key_length+= *length;
2655
DBUG_ASSERT(tree == 0);
2656
tree= new Unique(compare_key, cmp_arg, tree_key_length,
2657
thd->variables.max_heap_table_size);
2659
The only time tree_key_length could be 0 is if someone does
2660
count(distinct) on a char(0) field - stupid thing to do,
2661
but this has to be handled - otherwise someone can crash
2662
the server with a DoS attack
2664
is_evaluated= FALSE;
2672
Item *Item_sum_count_distinct::copy_or_same(THD* thd)
2674
return new (thd->mem_root) Item_sum_count_distinct(thd, this);
2678
void Item_sum_count_distinct::clear()
2680
/* tree and table can be both null only if always_null */
2681
is_evaluated= FALSE;
2688
table->file->extra(HA_EXTRA_NO_CACHE);
2689
table->file->ha_delete_all_rows();
2690
table->file->extra(HA_EXTRA_WRITE_CACHE);
2694
bool Item_sum_count_distinct::add()
2699
copy_fields(tmp_table_param);
2700
copy_funcs(tmp_table_param->items_to_copy);
2702
for (Field **field=table->field ; *field ; field++)
2703
if ((*field)->is_real_null(0))
2704
return 0; // Don't count NULL
2706
is_evaluated= FALSE;
2710
The first few bytes of record (at least one) are just markers
2711
for deleted and NULLs. We want to skip them since they will
2712
bloat the tree without providing any valuable info. Besides,
2713
key_length used to initialize the tree didn't include space for them.
2715
return tree->unique_add(table->record[0] + table->s->null_bytes);
2717
if ((error= table->file->ha_write_row(table->record[0])) &&
2718
table->file->is_fatal_error(error, HA_CHECK_DUP))
2724
longlong Item_sum_count_distinct::val_int()
2727
DBUG_ASSERT(fixed == 1);
2728
if (!table) // Empty query
2735
if (tree->elements == 0)
2736
return (longlong) tree->elements_in_tree(); // everything fits in memory
2738
tree->walk(count_distinct_walk, (void*) &count);
2740
return (longlong) count;
2743
error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
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 */
2925
/*****************************************************************************
2926
GROUP_CONCAT function
2929
GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
2930
[SEPARATOR str_const])
2932
concat of values from "group by" operation
2935
Blobs doesn't work with DISTINCT or ORDER BY
2936
*****************************************************************************/
2940
Compares the values for fields in expr list of GROUP_CONCAT.
2943
GROUP_CONCAT([DISTINCT] expr [,expr ...]
2944
[ORDER BY {unsigned_integer | col_name | expr}
2945
[ASC | DESC] [,col_name ...]]
2946
[SEPARATOR str_val])
2949
@retval -1 : key1 < key2
2950
@retval 0 : key1 = key2
2951
@retval 1 : key1 > key2
2954
int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
2957
Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
2958
TABLE *table= item_func->table;
2960
for (uint i= 0; i < item_func->arg_count_field; i++)
2962
Item *item= item_func->args[i];
2964
If field_item is a const item then either get_tp_table_field returns 0
2965
or it is an item over a const table.
2967
if (item->const_item())
2970
We have to use get_tmp_table_field() instead of
2971
real_item()->get_tmp_table_field() because we want the field in
2972
the temporary table, not the original field
2974
Field *field= item->get_tmp_table_field();
2976
uint offset= field->offset(field->table->record[0])-table->s->null_bytes;
2977
if((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
2985
function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
2988
int group_concat_key_cmp_with_order(void* arg, const void* key1,
2991
Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2992
ORDER **order_item, **end;
2993
TABLE *table= grp_item->table;
2995
for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2999
Item *item= *(*order_item)->item;
3001
We have to use get_tmp_table_field() instead of
3002
real_item()->get_tmp_table_field() because we want the field in
3003
the temporary table, not the original field
3005
Field *field= item->get_tmp_table_field();
3007
If item is a const item then either get_tp_table_field returns 0
3008
or it is an item over a const table.
3010
if (field && !item->const_item())
3013
uint offset= (field->offset(field->table->record[0]) -
3014
table->s->null_bytes);
3015
if ((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
3016
return (*order_item)->asc ? res : -res;
3020
We can't return 0 because in that case the tree class would remove this
3021
item as double value. This would cause problems for case-changes and
3022
if the returned values are not the same we do the sort on.
3029
Append data from current leaf to item->result.
3032
int dump_leaf_key(uchar* key, element_count count __attribute__((unused)),
3033
Item_func_group_concat *item)
3035
TABLE *table= item->table;
3036
String tmp((char *)table->record[1], table->s->reclength,
3037
default_charset_info);
3039
String *result= &item->result;
3040
Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
3041
uint old_length= result->length();
3043
if (item->no_appended)
3044
item->no_appended= FALSE;
3046
result->append(*item->separator);
3050
for (; arg < arg_end; arg++)
3053
if (! (*arg)->const_item())
3056
We have to use get_tmp_table_field() instead of
3057
real_item()->get_tmp_table_field() because we want the field in
3058
the temporary table, not the original field
3059
We also can't use table->field array to access the fields
3060
because it contains both order and arg list fields.
3062
Field *field= (*arg)->get_tmp_table_field();
3063
uint offset= (field->offset(field->table->record[0]) -
3064
table->s->null_bytes);
3065
DBUG_ASSERT(offset < table->s->reclength);
3066
res= field->val_str(&tmp, key + offset);
3069
res= (*arg)->val_str(&tmp);
3071
result->append(*res);
3074
/* stop if length of result more than max_length */
3075
if (result->length() > item->max_length)
3077
int well_formed_error;
3078
CHARSET_INFO *cs= item->collation.collation;
3079
const char *ptr= result->ptr();
3082
It's ok to use item->result.length() as the fourth argument
3083
as this is never used to limit the length of the data.
3084
Cut is done with the third argument.
3086
add_length= cs->cset->well_formed_len(cs,
3088
ptr + item->max_length,
3090
&well_formed_error);
3091
result->length(old_length + add_length);
3092
item->count_cut_values++;
3093
item->warning_for_row= TRUE;
3101
Constructor of Item_func_group_concat.
3103
@param distinct_arg distinct
3104
@param select_list list of expression for show values
3105
@param order_list list of sort columns
3106
@param separator_arg string value of separator.
3109
Item_func_group_concat::
3110
Item_func_group_concat(Name_resolution_context *context_arg,
3111
bool distinct_arg, List<Item> *select_list,
3112
SQL_LIST *order_list, String *separator_arg)
3113
:tmp_table_param(0), warning(0),
3114
separator(separator_arg), tree(0), unique_filter(NULL), table(0),
3115
order(0), context(context_arg),
3116
arg_count_order(order_list ? order_list->elements : 0),
3117
arg_count_field(select_list->elements),
3118
count_cut_values(0),
3119
distinct(distinct_arg),
3120
warning_for_row(FALSE),
3121
force_copy_fields(0), original(0)
3127
arg_count= arg_count_field + arg_count_order;
3130
We need to allocate:
3131
args - arg_count_field+arg_count_order
3132
(for possible order items in temporare tables)
3133
order - arg_count_order
3135
if (!(args= (Item**) sql_alloc(sizeof(Item*) * arg_count +
3136
sizeof(ORDER*)*arg_count_order)))
3139
order= (ORDER**)(args + arg_count);
3141
/* fill args items of show and sort */
3142
List_iterator_fast<Item> li(*select_list);
3144
for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
3145
*arg_ptr= item_select;
3147
if (arg_count_order)
3149
ORDER **order_ptr= order;
3150
for (ORDER *order_item= (ORDER*) order_list->first;
3152
order_item= order_item->next)
3154
(*order_ptr++)= order_item;
3155
*arg_ptr= *order_item->item;
3156
order_item->item= arg_ptr++;
3162
Item_func_group_concat::Item_func_group_concat(THD *thd,
3163
Item_func_group_concat *item)
3164
:Item_sum(thd, item),
3165
tmp_table_param(item->tmp_table_param),
3166
warning(item->warning),
3167
separator(item->separator),
3169
unique_filter(item->unique_filter),
3172
context(item->context),
3173
arg_count_order(item->arg_count_order),
3174
arg_count_field(item->arg_count_field),
3175
count_cut_values(item->count_cut_values),
3176
distinct(item->distinct),
3177
warning_for_row(item->warning_for_row),
3178
always_null(item->always_null),
3179
force_copy_fields(item->force_copy_fields),
3182
quick_group= item->quick_group;
3183
result.set_charset(collation.collation);
3188
void Item_func_group_concat::cleanup()
3190
DBUG_ENTER("Item_func_group_concat::cleanup");
3191
Item_sum::cleanup();
3193
/* Adjust warning message to include total number of cut values */
3196
char warn_buff[MYSQL_ERRMSG_SIZE];
3197
sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3198
warning->set_msg(current_thd, warn_buff);
3203
Free table and tree if they belong to this item (if item have not pointer
3204
to original item from which was made copy => it own its objects )
3208
delete tmp_table_param;
3212
THD *thd= table->in_use;
3213
free_tmp_table(thd, table);
3222
delete unique_filter;
3223
unique_filter= NULL;
3227
char warn_buff[MYSQL_ERRMSG_SIZE];
3228
sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3229
warning->set_msg(thd, warn_buff);
3233
DBUG_ASSERT(tree == 0 && warning == 0);
3239
Item *Item_func_group_concat::copy_or_same(THD* thd)
3241
return new (thd->mem_root) Item_func_group_concat(thd, this);
3245
void Item_func_group_concat::clear()
3250
warning_for_row= FALSE;
3255
unique_filter->reset();
3256
/* No need to reset the table as we never call write_row */
3260
bool Item_func_group_concat::add()
3264
copy_fields(tmp_table_param);
3265
copy_funcs(tmp_table_param->items_to_copy);
3267
for (uint i= 0; i < arg_count_field; i++)
3269
Item *show_item= args[i];
3270
if (!show_item->const_item())
3272
Field *f= show_item->get_tmp_table_field();
3273
if (f->is_null_in_record((const uchar*) table->record[0]))
3274
return 0; // Skip row if it contains null
3279
bool row_eligible= TRUE;
3283
/* Filter out duplicate rows. */
3284
uint count= unique_filter->elements_in_tree();
3285
unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3286
if (count == unique_filter->elements_in_tree())
3287
row_eligible= FALSE;
3290
TREE_ELEMENT *el= 0; // Only for safety
3291
if (row_eligible && tree)
3292
el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
3295
If the row is not a duplicate (el->count == 1)
3296
we can dump the row here in case of GROUP_CONCAT(DISTINCT...)
3297
instead of doing tree traverse later.
3299
if (row_eligible && !warning_for_row &&
3300
(!tree || (el->count == 1 && distinct && !arg_count_order)))
3301
dump_leaf_key(table->record[0] + table->s->null_bytes, 1, this);
3308
Item_func_group_concat::fix_fields(THD *thd, Item **ref)
3310
uint i; /* for loop variable */
3311
DBUG_ASSERT(fixed == 0);
3313
if (init_sum_func_check(thd))
3319
Fix fields for select list and ORDER clause
3322
for (i=0 ; i < arg_count ; i++)
3324
if ((!args[i]->fixed &&
3325
args[i]->fix_fields(thd, args + i)) ||
3326
args[i]->check_cols(1))
3330
if (agg_item_charsets(collation, func_name(),
3332
/* skip charset aggregation for order columns */
3333
arg_count - arg_count_order,
3334
MY_COLL_ALLOW_CONV, 1))
3337
result.set_charset(collation.collation);
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))
3371
bool Item_func_group_concat::setup(THD *thd)
3374
SELECT_LEX *select_lex= thd->lex->current_select;
3375
DBUG_ENTER("Item_func_group_concat::setup");
3378
Currently setup() can be called twice. Please add
3379
assertion here when this is fixed.
3384
if (!(tmp_table_param= new TMP_TABLE_PARAM))
3387
/* We'll convert all blobs to varchar fields in the temporary table */
3388
tmp_table_param->convert_blob_length= max_length *
3389
collation.collation->mbmaxlen;
3390
/* Push all not constant fields to the list and create a temp table */
3392
for (uint i= 0; i < arg_count_field; i++)
3394
Item *item= args[i];
3395
if (list.push_back(item))
3397
if (item->const_item())
3399
if (item->is_null())
3407
List<Item> all_fields(list);
3409
Try to find every ORDER expression in the list of GROUP_CONCAT
3410
arguments. If an expression is not found, prepend it to
3411
"all_fields". The resulting field list is used as input to create
3414
if (arg_count_order &&
3415
setup_order(thd, args, context->table_list, list, all_fields, *order))
3418
count_field_types(select_lex, tmp_table_param, all_fields, 0);
3419
tmp_table_param->force_copy_fields= force_copy_fields;
3420
DBUG_ASSERT(table == 0);
3421
if (arg_count_order > 0 || distinct)
3424
Currently we have to force conversion of BLOB values to VARCHAR's
3425
if we are to store them in TREE objects used for ORDER BY and
3426
DISTINCT. This leads to truncation if the BLOB's size exceeds
3427
Field_varstring::MAX_SIZE.
3429
set_if_smaller(tmp_table_param->convert_blob_length,
3430
Field_varstring::MAX_SIZE);
3433
Force the create_tmp_table() to convert BIT columns to INT
3434
as we cannot compare two table records containg BIT fields
3435
stored in the the tree used for distinct/order by.
3436
Moreover we don't even save in the tree record null bits
3437
where BIT fields store parts of their data.
3439
List_iterator_fast<Item> li(all_fields);
3441
while ((item= li++))
3443
if (item->type() == Item::FIELD_ITEM &&
3444
((Item_field*) item)->field->type() == FIELD_TYPE_BIT)
3450
We have to create a temporary table to get descriptions of fields
3451
(types, sizes and so on).
3453
Note that in the table, we first have the ORDER BY fields, then the
3456
if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
3457
(ORDER*) 0, 0, TRUE,
3458
(select_lex->options | thd->options),
3459
HA_POS_ERROR, (char*) "")))
3461
table->file->extra(HA_EXTRA_NO_ROWS);
3465
Need sorting or uniqueness: init tree and choose a function to sort.
3466
Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3467
the row is not added to the result.
3469
uint tree_key_length= table->s->reclength - table->s->null_bytes;
3471
if (arg_count_order)
3475
Create a tree for sorting. The tree is used to sort (according to the
3476
syntax of this function). If there is no ORDER BY clause, we don't
3479
init_tree(tree, (uint) min(thd->variables.max_heap_table_size,
3480
thd->variables.sortbuff_size/16), 0,
3482
group_concat_key_cmp_with_order , 0, NULL, (void*) this);
3486
unique_filter= new Unique(group_concat_key_cmp_with_distinct,
3489
thd->variables.max_heap_table_size);
3495
/* This is used by rollup to create a separate usable copy of the function */
3497
void Item_func_group_concat::make_unique()
3502
force_copy_fields= 1;
3507
String* Item_func_group_concat::val_str(String* str)
3509
DBUG_ASSERT(fixed == 1);
3512
if (no_appended && tree)
3513
/* Tree is used for sorting as in ORDER BY */
3514
tree_walk(tree, (tree_walk_action)&dump_leaf_key, (void*)this,
3516
if (count_cut_values && !warning)
3519
ER_CUT_VALUE_GROUP_CONCAT needs an argument, but this gets set in
3520
Item_func_group_concat::cleanup().
3523
warning= push_warning(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
3524
ER_CUT_VALUE_GROUP_CONCAT,
3525
ER(ER_CUT_VALUE_GROUP_CONCAT));
3531
void Item_func_group_concat::print(String *str, enum_query_type query_type)
3533
str->append(STRING_WITH_LEN("group_concat("));
3535
str->append(STRING_WITH_LEN("distinct "));
3536
for (uint i= 0; i < arg_count_field; i++)
3540
args[i]->print(str, query_type);
3542
if (arg_count_order)
3544
str->append(STRING_WITH_LEN(" order by "));
3545
for (uint i= 0 ; i < arg_count_order ; i++)
3549
(*order[i]->item)->print(str, query_type);
3551
str->append(STRING_WITH_LEN(" ASC"));
3553
str->append(STRING_WITH_LEN(" DESC"));
3556
str->append(STRING_WITH_LEN(" separator \'"));
3557
str->append(*separator);
3558
str->append(STRING_WITH_LEN("\')"));
3562
Item_func_group_concat::~Item_func_group_concat()
3564
if (!original && unique_filter)
3565
delete unique_filter;