1 /* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
2    rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
23 
24 
25 /**
26   @file
27 
28   @brief
29   Sum functions (COUNT, MIN...)
30 */
31 
32 #include "sql_priv.h"
33 #include "sql_select.h"
34 #include "sql_tmp_table.h"                 // create_tmp_table
35 #include "sql_resolver.h"                  // setup_order, fix_inner_refs
36 #include "sql_optimizer.h"                 // JOIN
37 
38 using std::min;
39 using std::max;
40 
41 /**
42   Calculate the affordable RAM limit for structures like TREE or Unique
43   used in Item_sum_*
44 */
45 
ram_limitation(THD * thd)46 ulonglong Item_sum::ram_limitation(THD *thd)
47 {
48   return min(thd->variables.tmp_table_size,
49       thd->variables.max_heap_table_size);
50 }
51 
52 
53 /**
54   Prepare an aggregate function item for checking context conditions.
55 
56     The function initializes the members of the Item_sum object created
57     for a set function that are used to check validity of the set function
58     occurrence.
59     If the set function is not allowed in any subquery where it occurs
60     an error is reported immediately.
61 
62   @param thd      reference to the thread context info
63 
64   @note
65     This function is to be called for any item created for a set function
66     object when the traversal of trees built for expressions used in the query
67     is performed at the phase of context analysis. This function is to
68     be invoked at the descent of this traversal.
69   @retval
70     TRUE   if an error is reported
71   @retval
72     FALSE  otherwise
73 */
74 
init_sum_func_check(THD * thd)75 bool Item_sum::init_sum_func_check(THD *thd)
76 {
77   if (!thd->lex->allow_sum_func)
78   {
79     my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
80                MYF(0));
81     return TRUE;
82   }
83   /* Set a reference to the nesting set function if there is  any */
84   in_sum_func= thd->lex->in_sum_func;
85   /* Save a pointer to object to be used in items for nested set functions */
86   thd->lex->in_sum_func= this;
87   nest_level= thd->lex->current_select->nest_level;
88   ref_by= 0;
89   aggr_level= -1;
90   aggr_sel= NULL;
91   max_arg_level= -1;
92   max_sum_func_level= -1;
93   outer_fields.empty();
94   return FALSE;
95 }
96 
97 /**
98   Check constraints imposed on a usage of a set function.
99 
100     The method verifies whether context conditions imposed on a usage
101     of any set function are met for this occurrence.
102     It checks whether the set function occurs in the position where it
103     can be aggregated and, when it happens to occur in argument of another
104     set function, the method checks that these two functions are aggregated in
105     different subqueries.
106     If the context conditions are not met the method reports an error.
107     If the set function is aggregated in some outer subquery the method
108     adds it to the chain of items for such set functions that is attached
109     to the the st_select_lex structure for this subquery.
110 
111     A number of designated members of the object are used to check the
112     conditions. They are specified in the comment before the Item_sum
113     class declaration.
114     Additionally a bitmap variable called allow_sum_func is employed.
115     It is included into the thd->lex structure.
116     The bitmap contains 1 at n-th position if the set function happens
117     to occur under a construct of the n-th level subquery where usage
118     of set functions are allowed (i.e either in the SELECT list or
119     in the HAVING clause of the corresponding subquery)
120     Consider the query:
121     @code
122        SELECT SUM(t1.b) FROM t1 GROUP BY t1.a
123          HAVING t1.a IN (SELECT t2.c FROM t2 WHERE AVG(t1.b) > 20) AND
124                 t1.a > (SELECT MIN(t2.d) FROM t2);
125     @endcode
126     allow_sum_func will contain:
127     - for SUM(t1.b) - 1 at the first position
128     - for AVG(t1.b) - 1 at the first position, 0 at the second position
129     - for MIN(t2.d) - 1 at the first position, 1 at the second position.
130 
131   @param thd  reference to the thread context info
132   @param ref  location of the pointer to this item in the embedding expression
133 
134   @note
135     This function is to be called for any item created for a set function
136     object when the traversal of trees built for expressions used in the query
137     is performed at the phase of context analysis. This function is to
138     be invoked at the ascent of this traversal.
139 
140   @retval
141     TRUE   if an error is reported
142   @retval
143     FALSE  otherwise
144 */
145 
check_sum_func(THD * thd,Item ** ref)146 bool Item_sum::check_sum_func(THD *thd, Item **ref)
147 {
148   bool invalid= FALSE;
149   nesting_map allow_sum_func= thd->lex->allow_sum_func;
150   /*
151     The value of max_arg_level is updated if an argument of the set function
152     contains a column reference resolved  against a subquery whose level is
153     greater than the current value of max_arg_level.
154     max_arg_level cannot be greater than nest level.
155     nest level is always >= 0
156   */
157   if (nest_level == max_arg_level)
158   {
159     /*
160       The function must be aggregated in the current subquery,
161       If it is there under a construct where it is not allowed
162       we report an error.
163     */
164     invalid= !(allow_sum_func & ((nesting_map)1 << max_arg_level));
165   }
166   else if (max_arg_level >= 0 ||
167            !(allow_sum_func & ((nesting_map)1 << nest_level)))
168   {
169     /*
170       The set function can be aggregated only in outer subqueries.
171       Try to find a subquery where it can be aggregated;
172       If we fail to find such a subquery report an error.
173     */
174     if (register_sum_func(thd, ref))
175       return TRUE;
176     invalid= aggr_level < 0 &&
177              !(allow_sum_func & ((nesting_map)1 << nest_level));
178     if (!invalid && thd->variables.sql_mode & MODE_ANSI)
179       invalid= aggr_level < 0 && max_arg_level < nest_level;
180   }
181   if (!invalid && aggr_level < 0)
182   {
183     aggr_level= nest_level;
184     aggr_sel= thd->lex->current_select;
185   }
186   /*
187     By this moment we either found a subquery where the set function is
188     to be aggregated  and assigned a value that is  >= 0 to aggr_level,
189     or set the value of 'invalid' to TRUE to report later an error.
190   */
191   /*
192     Additionally we have to check whether possible nested set functions
193     are acceptable here: they are not, if the level of aggregation of
194     some of them is less than aggr_level.
195   */
196   if (!invalid)
197     invalid= aggr_level <= max_sum_func_level;
198   if (invalid)
199   {
200     my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
201                MYF(0));
202     return TRUE;
203   }
204 
205   if (in_sum_func)
206   {
207     /*
208       If the set function is nested adjust the value of
209       max_sum_func_level for the nesting set function.
210       We take into account only enclosed set functions that are to be
211       aggregated on the same level or above of the nest level of
212       the enclosing set function.
213       But we must always pass up the max_sum_func_level because it is
214       the maximum nested level of all directly and indirectly enclosed
215       set functions. We must do that even for set functions that are
216       aggregated inside of their enclosing set function's nest level
217       because the enclosing function may contain another enclosing
218       function that is to be aggregated outside or on the same level
219       as its parent's nest level.
220     */
221     if (in_sum_func->nest_level >= aggr_level)
222       set_if_bigger(in_sum_func->max_sum_func_level, aggr_level);
223     set_if_bigger(in_sum_func->max_sum_func_level, max_sum_func_level);
224   }
225 
226   /*
227     Check that non-aggregated fields and sum functions aren't mixed in the
228     same select in the ONLY_FULL_GROUP_BY mode.
229   */
230   if (outer_fields.elements)
231   {
232     Item_field *field;
233     /*
234       Here we compare the nesting level of the select to which an outer field
235       belongs to with the aggregation level of the sum function. All fields in
236       the outer_fields list are checked.
237 
238       If the nesting level is equal to the aggregation level then the field is
239         aggregated by this sum function.
240       If the nesting level is less than the aggregation level then the field
241         belongs to an outer select. In this case if there is an embedding sum
242         function add current field to functions outer_fields list. If there is
243         no embedding function then the current field treated as non aggregated
244         and the select it belongs to is marked accordingly.
245       If the nesting level is greater than the aggregation level then it means
246         that this field was added by an inner sum function.
247         Consider an example:
248 
249           select avg ( <-- we are here, checking outer.f1
250             select (
251               select sum(outer.f1 + inner.f1) from inner
252             ) from outer)
253           from most_outer;
254 
255         In this case we check that no aggregate functions are used in the
256         select the field belongs to. If there are some then an error is
257         raised.
258     */
259     List_iterator<Item_field> of(outer_fields);
260     while ((field= of++))
261     {
262       SELECT_LEX *sel= field->cached_table->select_lex;
263       if (sel->nest_level < aggr_level)
264       {
265         if (in_sum_func)
266         {
267           /*
268             Let upper function decide whether this field is a non
269             aggregated one.
270           */
271           in_sum_func->outer_fields.push_back(field);
272         }
273         else
274           sel->set_non_agg_field_used(true);
275       }
276       if (sel->nest_level > aggr_level &&
277           (sel->agg_func_used()) &&
278           !sel->group_list.elements)
279       {
280         my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
281                    ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
282         return TRUE;
283       }
284     }
285   }
286   aggr_sel->set_agg_func_used(true);
287   update_used_tables();
288   thd->lex->in_sum_func= in_sum_func;
289   return FALSE;
290 }
291 
292 /**
293   Attach a set function to the subquery where it must be aggregated.
294 
295     The function looks for an outer subquery where the set function must be
296     aggregated. If it finds such a subquery then aggr_level is set to
297     the nest level of this subquery and the item for the set function
298     is added to the list of set functions used in nested subqueries
299     inner_sum_func_list defined for each subquery. When the item is placed
300     there the field 'ref_by' is set to ref.
301 
302   @note
303     Now we 'register' only set functions that are aggregated in outer
304     subqueries. Actually it makes sense to link all set function for
305     a subquery in one chain. It would simplify the process of 'splitting'
306     for set functions.
307 
308   @param thd  reference to the thread context info
309   @param ref  location of the pointer to this item in the embedding expression
310 
311   @retval
312     FALSE  if the executes without failures (currently always)
313   @retval
314     TRUE   otherwise
315 */
316 
register_sum_func(THD * thd,Item ** ref)317 bool Item_sum::register_sum_func(THD *thd, Item **ref)
318 {
319   nesting_map allow_sum_func= thd->lex->allow_sum_func;
320 
321   // Find the outer-most query block where this function can be aggregated.
322 
323   for (SELECT_LEX *sl= thd->lex->current_select->outer_select();
324        sl && sl->nest_level >= max_arg_level;
325        sl= sl->outer_select())
326   {
327     if (allow_sum_func & ((nesting_map)1 << sl->nest_level))
328     {
329       aggr_level= sl->nest_level;
330       aggr_sel= sl;
331     }
332     /*
333       Cannot go above level zero. This might be possible in 5.6 because the
334       nest_level of the query blocks of a view are not adjusted when the view
335       is attached to the outer query. However, 5.7 adjusts nest_level
336       correctly, so this code is only necessary in 5.6.
337     */
338     if (sl->nest_level == 0)
339       break;
340   }
341 
342   if (aggr_level >= 0)
343   {
344     ref_by= ref;
345     /* Add the object to the list of registered objects assigned to aggr_sel */
346     if (!aggr_sel->inner_sum_func_list)
347       next= this;
348     else
349     {
350       next= aggr_sel->inner_sum_func_list->next;
351       aggr_sel->inner_sum_func_list->next= this;
352     }
353     aggr_sel->inner_sum_func_list= this;
354     aggr_sel->with_sum_func= true;
355 
356     /*
357       Mark Item_subselect(s) as containing aggregate function all the way up
358       to aggregate function's calculation context.
359       Note that we must not mark the Item of calculation context itself
360       because with_sum_func on the calculation context st_select_lex is
361       already set above.
362 
363       with_sum_func being set for an Item means that this Item refers
364       (somewhere in it, e.g. one of its arguments if it's a function) directly
365       or through intermediate items to an aggregate function that is calculated
366       in a context "outside" of the Item (e.g. in the current or outer select).
367 
368       with_sum_func being set for an st_select_lex means that this query block
369       has aggregate functions directly referenced (i.e. not through a subquery).
370     */
371     for (SELECT_LEX *sl= thd->lex->current_select;
372          sl && sl != aggr_sel && sl->master_unit()->item;
373          sl= sl->outer_select())
374       sl->master_unit()->item->with_sum_func= true;
375   }
376   thd->lex->current_select->mark_as_dependent(aggr_sel);
377   return false;
378 }
379 
380 
Item_sum(List<Item> & list)381 Item_sum::Item_sum(List<Item> &list) :next(NULL), arg_count(list.elements),
382   forced_const(FALSE)
383 {
384   if ((args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
385   {
386     uint i=0;
387     List_iterator_fast<Item> li(list);
388     Item *item;
389 
390     while ((item=li++))
391     {
392       args[i++]= item;
393     }
394   }
395   mark_as_sum_func();
396   init_aggregator();
397   list.empty();					// Fields are used
398 }
399 
400 
401 /**
402   Constructor used in processing select with temporary tebles.
403 */
404 
Item_sum(THD * thd,Item_sum * item)405 Item_sum::Item_sum(THD *thd, Item_sum *item):
406   Item_result_field(thd, item),
407   next(NULL),
408   aggr_sel(item->aggr_sel),
409   nest_level(item->nest_level), aggr_level(item->aggr_level),
410   quick_group(item->quick_group),
411   arg_count(item->arg_count),
412   used_tables_cache(item->used_tables_cache),
413   forced_const(item->forced_const)
414 {
415   if (arg_count <= 2)
416     args= tmp_args;
417   else if (!(args= (Item**) thd->alloc(sizeof(Item*)*arg_count)))
418     return;
419   memcpy(args, item->args, sizeof(Item*)*arg_count);
420   init_aggregator();
421   with_distinct= item->with_distinct;
422   if (item->aggr)
423     set_aggregator(item->aggr->Aggrtype());
424 }
425 
426 
mark_as_sum_func()427 void Item_sum::mark_as_sum_func()
428 {
429   SELECT_LEX *cur_select= current_thd->lex->current_select;
430   cur_select->n_sum_items++;
431   cur_select->with_sum_func= 1;
432   with_sum_func= 1;
433 }
434 
435 
print(String * str,enum_query_type query_type)436 void Item_sum::print(String *str, enum_query_type query_type)
437 {
438   str->append(func_name());
439   for (uint i=0 ; i < arg_count ; i++)
440   {
441     if (i)
442       str->append(',');
443     args[i]->print(str, query_type);
444   }
445   str->append(')');
446 }
447 
fix_num_length_and_dec()448 void Item_sum::fix_num_length_and_dec()
449 {
450   decimals=0;
451   for (uint i=0 ; i < arg_count ; i++)
452     set_if_bigger(decimals,args[i]->decimals);
453   max_length=float_length(decimals);
454 }
455 
get_tmp_table_item(THD * thd)456 Item *Item_sum::get_tmp_table_item(THD *thd)
457 {
458   Item_sum* sum_item= (Item_sum *) copy_or_same(thd);
459   if (sum_item && sum_item->result_field)	   // If not a const sum func
460   {
461     Field *result_field_tmp= sum_item->result_field;
462     for (uint i=0 ; i < sum_item->arg_count ; i++)
463     {
464       Item *arg= sum_item->args[i];
465       if (!arg->const_item())
466       {
467 	if (arg->type() == Item::FIELD_ITEM)
468 	  ((Item_field*) arg)->field= result_field_tmp++;
469 	else
470 	  sum_item->args[i]= new Item_field(result_field_tmp++);
471       }
472     }
473   }
474   return sum_item;
475 }
476 
477 
walk(Item_processor processor,bool walk_subquery,uchar * argument)478 bool Item_sum::walk (Item_processor processor, bool walk_subquery,
479                      uchar *argument)
480 {
481   if (arg_count)
482   {
483     Item **arg,**arg_end;
484     for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
485     {
486       if ((*arg)->walk(processor, walk_subquery, argument))
487 	return 1;
488     }
489   }
490   return (this->*processor)(argument);
491 }
492 
493 
494 /**
495   Remove the item from the list of inner aggregation functions in the
496   SELECT_LEX it was moved to by Item_sum::register_sum_func().
497 
498   This is done to undo some of the effects of
499   Item_sum::register_sum_func() so that the item may be removed from
500   the query.
501 
502   @note This doesn't completely undo Item_sum::register_sum_func(), as
503   with_sum_func information is left untouched. This means that if this
504   item is removed, aggr_sel and all Item_subselects between aggr_sel
505   and this item may be left with with_sum_func set to true, even if
506   there are no aggregation functions. To our knowledge, this has no
507   impact on the query result.
508 
509   @see Item_sum::register_sum_func()
510   @see remove_redundant_subquery_clauses()
511  */
clean_up_after_removal(uchar * arg)512 bool Item_sum::clean_up_after_removal(uchar *arg)
513 {
514   /*
515     Don't do anything if
516     1) this is an unresolved item (This may happen if an
517        expression occurs twice in the same query. In that case, the
518        whole item tree for the second occurence is replaced by the
519        item tree for the first occurence, without calling fix_fields()
520        on the second tree. Therefore there's nothing to clean up.), or
521     2) there is no inner_sum_func_list, or
522     3) the item is not an element in the inner_sum_func_list.
523   */
524   if (!fixed ||                                                    // 1
525       aggr_sel == NULL || aggr_sel->inner_sum_func_list == NULL || // 2
526       next == NULL)                                                // 3
527     return false;
528 
529   if (next == this)
530     aggr_sel->inner_sum_func_list= NULL;
531   else
532   {
533     Item_sum *prev;
534     for (prev= this; prev->next != this; prev= prev->next)
535       ;
536     prev->next= next;
537     if (aggr_sel->inner_sum_func_list == this)
538       aggr_sel->inner_sum_func_list= prev;
539   }
540 
541   return false;
542 }
543 
544 
create_tmp_field(bool group,TABLE * table)545 Field *Item_sum::create_tmp_field(bool group, TABLE *table)
546 {
547   Field *field;
548   switch (result_type()) {
549   case REAL_RESULT:
550     field= new Field_double(max_length, maybe_null, item_name.ptr(), decimals, TRUE);
551     break;
552   case INT_RESULT:
553     field= new Field_longlong(max_length, maybe_null, item_name.ptr(), unsigned_flag);
554     break;
555   case STRING_RESULT:
556     return make_string_field(table);
557   case DECIMAL_RESULT:
558     field= Field_new_decimal::create_from_item(this);
559     break;
560   case ROW_RESULT:
561   default:
562     // This case should never be choosen
563     DBUG_ASSERT(0);
564     return 0;
565   }
566   if (field)
567     field->init(table);
568   return field;
569 }
570 
571 
update_used_tables()572 void Item_sum::update_used_tables ()
573 {
574   if (!forced_const)
575   {
576     used_tables_cache= 0;
577     with_subselect= false;
578     with_stored_program= false;
579     for (uint i=0 ; i < arg_count ; i++)
580     {
581       args[i]->update_used_tables();
582       used_tables_cache|= args[i]->used_tables();
583       with_subselect|= args[i]->has_subquery();
584       with_stored_program|= args[i]->has_stored_program();
585     }
586 
587     used_tables_cache&= PSEUDO_TABLE_BITS;
588 
589     /* the aggregate function is aggregated into its local context */
590     used_tables_cache|= ((table_map)1 << aggr_sel->join->tables) - 1;
591   }
592 }
593 
594 
set_arg(uint i,THD * thd,Item * new_val)595 Item *Item_sum::set_arg(uint i, THD *thd, Item *new_val)
596 {
597   thd->change_item_tree(args + i, new_val);
598   return new_val;
599 }
600 
601 
set_aggregator(Aggregator::Aggregator_type aggregator)602 int Item_sum::set_aggregator(Aggregator::Aggregator_type aggregator)
603 {
604   /*
605     Dependent subselects may be executed multiple times, making
606     set_aggregator to be called multiple times. The aggregator type
607     will be the same, but it needs to be reset so that it is
608     reevaluated with the new dependent data.
609     This function may also be called multiple times during query optimization.
610     In this case, the type may change, so we delete the old aggregator,
611     and create a new one.
612   */
613   if (aggr && aggregator == aggr->Aggrtype())
614   {
615     aggr->clear();
616     return FALSE;
617   }
618 
619   delete aggr;
620   switch (aggregator)
621   {
622   case Aggregator::DISTINCT_AGGREGATOR:
623     aggr= new Aggregator_distinct(this);
624     break;
625   case Aggregator::SIMPLE_AGGREGATOR:
626     aggr= new Aggregator_simple(this);
627     break;
628   };
629   return aggr ? FALSE : TRUE;
630 }
631 
632 
cleanup()633 void Item_sum::cleanup()
634 {
635   if (aggr)
636   {
637     delete aggr;
638     aggr= NULL;
639   }
640   Item_result_field::cleanup();
641   forced_const= FALSE;
642 }
643 
644 
645 /**
646   Compare keys consisting of single field that cannot be compared as binary.
647 
648   Used by the Unique class to compare keys. Will do correct comparisons
649   for all field types.
650 
651   @param    arg     Pointer to the relevant Field class instance
652   @param    key1    left key image
653   @param    key2    right key image
654   @return   comparison result
655     @retval < 0       if key1 < key2
656     @retval = 0       if key1 = key2
657     @retval > 0       if key1 > key2
658 */
659 
simple_str_key_cmp(void * arg,uchar * key1,uchar * key2)660 static int simple_str_key_cmp(void* arg, uchar* key1, uchar* key2)
661 {
662   Field *f= (Field*) arg;
663   return f->cmp(key1, key2);
664 }
665 
666 
667 /**
668   Correctly compare composite keys.
669 
670   Used by the Unique class to compare keys. Will do correct comparisons
671   for composite keys with various field types.
672 
673   @param arg     Pointer to the relevant Aggregator_distinct instance
674   @param key1    left key image
675   @param key2    right key image
676   @return        comparison result
677     @retval <0       if key1 < key2
678     @retval =0       if key1 = key2
679     @retval >0       if key1 > key2
680 */
681 
composite_key_cmp(void * arg,uchar * key1,uchar * key2)682 int Aggregator_distinct::composite_key_cmp(void* arg, uchar* key1, uchar* key2)
683 {
684   Aggregator_distinct *aggr= (Aggregator_distinct *) arg;
685   Field **field    = aggr->table->field;
686   Field **field_end= field + aggr->table->s->fields;
687   uint32 *lengths=aggr->field_lengths;
688   for (; field < field_end; ++field)
689   {
690     Field* f = *field;
691     int len = *lengths++;
692     int res = f->cmp(key1, key2);
693     if (res)
694       return res;
695     key1 += len;
696     key2 += len;
697   }
698   return 0;
699 }
700 
701 
702 static enum enum_field_types
calc_tmp_field_type(enum enum_field_types table_field_type,Item_result result_type)703 calc_tmp_field_type(enum enum_field_types table_field_type,
704                     Item_result result_type)
705 {
706   /* Adjust tmp table type according to the chosen aggregation type */
707   switch (result_type) {
708   case STRING_RESULT:
709   case REAL_RESULT:
710     if (table_field_type != MYSQL_TYPE_FLOAT)
711       table_field_type= MYSQL_TYPE_DOUBLE;
712     break;
713   case INT_RESULT:
714     table_field_type= MYSQL_TYPE_LONGLONG;
715     /* fallthrough */
716   case DECIMAL_RESULT:
717     if (table_field_type != MYSQL_TYPE_LONGLONG)
718       table_field_type= MYSQL_TYPE_NEWDECIMAL;
719     break;
720   case ROW_RESULT:
721   default:
722     DBUG_ASSERT(0);
723   }
724   return table_field_type;
725 }
726 
727 
728 /***************************************************************************/
729 
730 C_MODE_START
731 
732 /* Declarations for auxilary C-callbacks */
733 
simple_raw_key_cmp(const void * arg,const void * key1,const void * key2)734 static int simple_raw_key_cmp(const void* arg,
735                               const void* key1, const void* key2)
736 {
737     return memcmp(key1, key2, *(const uint *) arg);
738 }
739 
740 
item_sum_distinct_walk(void * element,element_count num_of_dups,void * item)741 static int item_sum_distinct_walk(void *element, element_count num_of_dups,
742                                   void *item)
743 {
744   return ((Aggregator_distinct*) (item))->unique_walk_function(element);
745 }
746 
747 C_MODE_END
748 
749 /***************************************************************************/
750 /**
751   Called before feeding the first row. Used to allocate/setup
752   the internal structures used for aggregation.
753 
754   @param thd Thread descriptor
755   @return status
756     @retval FALSE success
757     @retval TRUE  faliure
758 
759     Prepares Aggregator_distinct to process the incoming stream.
760     Creates the temporary table and the Unique class if needed.
761     Called by Item_sum::aggregator_setup()
762 */
763 
setup(THD * thd)764 bool Aggregator_distinct::setup(THD *thd)
765 {
766   endup_done= FALSE;
767   /*
768     Setup can be called twice for ROLLUP items. This is a bug.
769     Please add DBUG_ASSERT(tree == 0) here when it's fixed.
770   */
771   if (tree || table || tmp_table_param)
772     return FALSE;
773 
774   if (item_sum->setup(thd))
775     return TRUE;
776   if (item_sum->sum_func() == Item_sum::COUNT_FUNC ||
777       item_sum->sum_func() == Item_sum::COUNT_DISTINCT_FUNC)
778   {
779     List<Item> list;
780     SELECT_LEX *select_lex= thd->lex->current_select;
781 
782     if (!(tmp_table_param= new TMP_TABLE_PARAM))
783       return TRUE;
784 
785     /**
786       Create a table with an unique key over all parameters.
787       If the list contains only const values, const_distinct
788       is set to CONST_NOT_NULL to avoid creation of temp table
789       and thereby counting as count(distinct of const values)
790       will always be 1. If any of these const values is null,
791       const_distinct is set to CONST_NULL to ensure aggregation
792       does not happen.
793      */
794     uint const_items= 0;
795     uint num_args= item_sum->get_arg_count();
796     DBUG_ASSERT(num_args);
797     for (uint i=0; i < num_args; i++)
798     {
799       Item *item=item_sum->get_arg(i);
800       if (list.push_back(item))
801         return true;                              // End of memory
802       if (item->const_item())
803       {
804         if (item->is_null())
805         {
806           const_distinct= CONST_NULL;
807           return false;
808         }
809         else
810           const_items++;
811       }
812     }
813     if (num_args == const_items)
814     {
815       const_distinct= CONST_NOT_NULL;
816       return false;
817     }
818     count_field_types(select_lex, tmp_table_param, list, 0);
819     tmp_table_param->force_copy_fields= item_sum->has_force_copy_fields();
820     DBUG_ASSERT(table == 0);
821     /*
822       Make create_tmp_table() convert BIT columns to BIGINT.
823       This is needed because BIT fields store parts of their data in table's
824       null bits, and we don't have methods to compare two table records, which
825       is needed by Unique which is used when HEAP table is used.
826     */
827     {
828       List_iterator_fast<Item> li(list);
829       Item *item;
830       while ((item= li++))
831       {
832         if (item->type() == Item::FIELD_ITEM &&
833             ((Item_field*)item)->field->type() == FIELD_TYPE_BIT)
834           item->marker=4;
835       }
836     }
837     if (!(table= create_tmp_table(thd, tmp_table_param, list, (ORDER*) 0, 1,
838                                   0,
839                                   (select_lex->options | thd->variables.option_bits),
840                                   HA_POS_ERROR, "")))
841       return TRUE;
842     table->file->extra(HA_EXTRA_NO_ROWS);		// Don't update rows
843     table->no_rows=1;
844 
845     if (table->s->db_type() == heap_hton)
846     {
847       /*
848         No blobs, otherwise it would have been MyISAM: set up a compare
849         function and its arguments to use with Unique.
850       */
851       qsort_cmp2 compare_key;
852       void* cmp_arg;
853       Field **field= table->field;
854       Field **field_end= field + table->s->fields;
855       bool all_binary= TRUE;
856 
857       for (tree_key_length= 0; field < field_end; ++field)
858       {
859         Field *f= *field;
860         enum enum_field_types type= f->type();
861         tree_key_length+= f->pack_length();
862         if ((type == MYSQL_TYPE_VARCHAR) ||
863             (!f->binary() && (type == MYSQL_TYPE_STRING ||
864                              type == MYSQL_TYPE_VAR_STRING)))
865         {
866           all_binary= FALSE;
867           break;
868         }
869       }
870       if (all_binary)
871       {
872         cmp_arg= (void*) &tree_key_length;
873         compare_key= (qsort_cmp2) simple_raw_key_cmp;
874       }
875       else
876       {
877         if (table->s->fields == 1)
878         {
879           /*
880             If we have only one field, which is the most common use of
881             count(distinct), it is much faster to use a simpler key
882             compare method that can take advantage of not having to worry
883             about other fields.
884           */
885           compare_key= (qsort_cmp2) simple_str_key_cmp;
886           cmp_arg= (void*) table->field[0];
887           /* tree_key_length has been set already */
888         }
889         else
890         {
891           uint32 *length;
892           compare_key= (qsort_cmp2) composite_key_cmp;
893           cmp_arg= (void*) this;
894           field_lengths= (uint32*) thd->alloc(table->s->fields * sizeof(uint32));
895           for (tree_key_length= 0, length= field_lengths, field= table->field;
896                field < field_end; ++field, ++length)
897           {
898             *length= (*field)->pack_length();
899             tree_key_length+= *length;
900           }
901         }
902       }
903       DBUG_ASSERT(tree == 0);
904       tree= new Unique(compare_key, cmp_arg, tree_key_length,
905                        item_sum->ram_limitation(thd));
906       /*
907         The only time tree_key_length could be 0 is if someone does
908         count(distinct) on a char(0) field - stupid thing to do,
909         but this has to be handled - otherwise someone can crash
910         the server with a DoS attack
911       */
912       if (! tree)
913         return TRUE;
914     }
915     return FALSE;
916   }
917   else
918   {
919     List<Create_field> field_list;
920     Create_field field_def;                              /* field definition */
921     Item *arg;
922     DBUG_ENTER("Aggregator_distinct::setup");
923     /* It's legal to call setup() more than once when in a subquery */
924     if (tree)
925       DBUG_RETURN(FALSE);
926 
927     /*
928       Virtual table and the tree are created anew on each re-execution of
929       PS/SP. Hence all further allocations are performed in the runtime
930       mem_root.
931     */
932     if (field_list.push_back(&field_def))
933       DBUG_RETURN(TRUE);
934 
935     item_sum->null_value= item_sum->maybe_null= 1;
936     item_sum->quick_group= 0;
937 
938     DBUG_ASSERT(item_sum->get_arg(0)->fixed);
939 
940     arg= item_sum->get_arg(0);
941     if (arg->const_item())
942     {
943       (void) arg->val_int();
944       if (arg->null_value)
945       {
946         const_distinct= CONST_NULL;
947         DBUG_RETURN(false);
948       }
949     }
950 
951 
952     enum enum_field_types field_type;
953 
954     field_type= calc_tmp_field_type(arg->field_type(),
955                               arg->result_type());
956     field_def.init_for_tmp_table(field_type,
957                                  arg->max_length,
958                                  arg->decimals,
959                                  arg->maybe_null,
960                                  arg->unsigned_flag);
961 
962     if (! (table= create_virtual_tmp_table(thd, field_list)))
963       DBUG_RETURN(TRUE);
964 
965     /* XXX: check that the case of CHAR(0) works OK */
966     tree_key_length= table->s->reclength - table->s->null_bytes;
967 
968     /*
969       Unique handles all unique elements in a tree until they can't fit
970       in.  Then the tree is dumped to the temporary file. We can use
971       simple_raw_key_cmp because the table contains numbers only; decimals
972       are converted to binary representation as well.
973     */
974     tree= new Unique(simple_raw_key_cmp, &tree_key_length, tree_key_length,
975                      item_sum->ram_limitation(thd));
976 
977     DBUG_RETURN(tree == 0);
978   }
979 }
980 
981 
982 /**
983   Invalidate calculated value and clear the distinct rows.
984 
985   Frees space used by the internal data structures.
986   Removes the accumulated distinct rows. Invalidates the calculated result.
987 */
988 
clear()989 void Aggregator_distinct::clear()
990 {
991   endup_done= FALSE;
992   item_sum->clear();
993   if (tree)
994     tree->reset();
995   /* tree and table can be both null only if const_distinct is enabled*/
996   if (item_sum->sum_func() == Item_sum::COUNT_FUNC ||
997       item_sum->sum_func() == Item_sum::COUNT_DISTINCT_FUNC)
998   {
999     if (!tree && table)
1000     {
1001       table->file->extra(HA_EXTRA_NO_CACHE);
1002       table->file->ha_delete_all_rows();
1003       table->file->extra(HA_EXTRA_WRITE_CACHE);
1004     }
1005   }
1006   else
1007   {
1008     item_sum->null_value= 1;
1009   }
1010 }
1011 
1012 
1013 /**
1014   Process incoming row.
1015 
1016   Add it to Unique/temp hash table if it's unique. Skip the row if
1017   not unique.
1018   Prepare Aggregator_distinct to process the incoming stream.
1019   Create the temporary table and the Unique class if needed.
1020   Called by Item_sum::aggregator_add().
1021   To actually get the result value in item_sum's buffers
1022   Aggregator_distinct::endup() must be called.
1023 
1024   @return status
1025     @retval FALSE     success
1026     @retval TRUE      failure
1027 */
1028 
add()1029 bool Aggregator_distinct::add()
1030 {
1031   if (const_distinct == CONST_NULL)
1032     return 0;
1033 
1034   if (item_sum->sum_func() == Item_sum::COUNT_FUNC ||
1035       item_sum->sum_func() == Item_sum::COUNT_DISTINCT_FUNC)
1036   {
1037     int error;
1038 
1039     if (const_distinct == CONST_NOT_NULL)
1040     {
1041       DBUG_ASSERT(item_sum->fixed == 1);
1042       Item_sum_count *sum= (Item_sum_count *)item_sum;
1043       sum->count= 1;
1044       return 0;
1045     }
1046     copy_fields(tmp_table_param);
1047     if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
1048       return TRUE;
1049 
1050     for (Field **field=table->field ; *field ; field++)
1051       if ((*field)->is_real_null(0))
1052         return 0;					// Don't count NULL
1053 
1054     if (tree)
1055     {
1056       /*
1057         The first few bytes of record (at least one) are just markers
1058         for deleted and NULLs. We want to skip them since they will
1059         bloat the tree without providing any valuable info. Besides,
1060         key_length used to initialize the tree didn't include space for them.
1061       */
1062       return tree->unique_add(table->record[0] + table->s->null_bytes);
1063     }
1064     if ((error= table->file->ha_write_row(table->record[0])) &&
1065         table->file->is_fatal_error(error, HA_CHECK_DUP))
1066       return TRUE;
1067     return FALSE;
1068   }
1069   else
1070   {
1071     item_sum->get_arg(0)->save_in_field(table->field[0], FALSE);
1072     if (table->field[0]->is_null())
1073       return 0;
1074     DBUG_ASSERT(tree);
1075     item_sum->null_value= 0;
1076     /*
1077       '0' values are also stored in the tree. This doesn't matter
1078       for SUM(DISTINCT), but is important for AVG(DISTINCT)
1079     */
1080     return tree->unique_add(table->field[0]->ptr);
1081   }
1082 }
1083 
1084 
1085 /**
1086   Calculate the aggregate function value.
1087 
1088   Since Distinct_aggregator::add() just collects the distinct rows,
1089   we must go over the distinct rows and feed them to the aggregation
1090   function before returning its value.
1091   This is what endup () does. It also sets the result validity flag
1092   endup_done to TRUE so it will not recalculate the aggregate value
1093   again if the Item_sum hasn't been reset.
1094 */
1095 
endup()1096 void Aggregator_distinct::endup()
1097 {
1098   /* prevent consecutive recalculations */
1099   if (endup_done)
1100     return;
1101 
1102   if (const_distinct ==  CONST_NOT_NULL)
1103   {
1104     endup_done= TRUE;
1105     return;
1106   }
1107 
1108   /* we are going to calculate the aggregate value afresh */
1109   item_sum->clear();
1110 
1111   /* The result will definitely be null : no more calculations needed */
1112   if (const_distinct == CONST_NULL)
1113     return;
1114 
1115   if (item_sum->sum_func() == Item_sum::COUNT_FUNC ||
1116       item_sum->sum_func() == Item_sum::COUNT_DISTINCT_FUNC)
1117   {
1118     DBUG_ASSERT(item_sum->fixed == 1);
1119     Item_sum_count *sum= (Item_sum_count *)item_sum;
1120 
1121     if (tree && tree->elements == 0)
1122     {
1123       /* everything fits in memory */
1124       sum->count= (longlong) tree->elements_in_tree();
1125       endup_done= TRUE;
1126     }
1127     if (!tree)
1128     {
1129       /* there were blobs */
1130       table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
1131       sum->count= table->file->stats.records;
1132       endup_done= TRUE;
1133     }
1134   }
1135 
1136  /*
1137    We don't have a tree only if 'setup()' hasn't been called;
1138    this is the case of sql_executor.cc:return_zero_rows.
1139  */
1140   if (tree && !endup_done)
1141   {
1142    /*
1143      All tree's values are not NULL.
1144      Note that value of field is changed as we walk the tree, in
1145      Aggregator_distinct::unique_walk_function, but it's always not NULL.
1146    */
1147    table->field[0]->set_notnull();
1148     /* go over the tree of distinct keys and calculate the aggregate value */
1149     use_distinct_values= TRUE;
1150     tree->walk(item_sum_distinct_walk, (void*) this);
1151     use_distinct_values= FALSE;
1152   }
1153   /* prevent consecutive recalculations */
1154   endup_done= TRUE;
1155 }
1156 
1157 
1158 String *
val_str(String * str)1159 Item_sum_num::val_str(String *str)
1160 {
1161   return val_string_from_real(str);
1162 }
1163 
1164 
val_decimal(my_decimal * decimal_value)1165 my_decimal *Item_sum_num::val_decimal(my_decimal *decimal_value)
1166 {
1167   return val_decimal_from_real(decimal_value);
1168 }
1169 
1170 
1171 String *
val_str(String * str)1172 Item_sum_int::val_str(String *str)
1173 {
1174   return val_string_from_int(str);
1175 }
1176 
1177 
val_decimal(my_decimal * decimal_value)1178 my_decimal *Item_sum_int::val_decimal(my_decimal *decimal_value)
1179 {
1180   return val_decimal_from_int(decimal_value);
1181 }
1182 
1183 
1184 bool
fix_fields(THD * thd,Item ** ref)1185 Item_sum_num::fix_fields(THD *thd, Item **ref)
1186 {
1187   DBUG_ASSERT(fixed == 0);
1188 
1189   if (init_sum_func_check(thd))
1190     return TRUE;
1191 
1192   decimals=0;
1193   maybe_null=0;
1194   for (uint i=0 ; i < arg_count ; i++)
1195   {
1196     if ((!args[i]->fixed && args[i]->fix_fields(thd, args + i)) ||
1197         args[i]->check_cols(1))
1198       return TRUE;
1199     set_if_bigger(decimals, args[i]->decimals);
1200     maybe_null |= args[i]->maybe_null;
1201   }
1202   result_field=0;
1203   max_length=float_length(decimals);
1204   null_value=1;
1205   fix_length_and_dec();
1206 
1207   if (check_sum_func(thd, ref))
1208     return TRUE;
1209 
1210   fixed= 1;
1211   return FALSE;
1212 }
1213 
1214 
1215 bool
fix_fields(THD * thd,Item ** ref)1216 Item_sum_hybrid::fix_fields(THD *thd, Item **ref)
1217 {
1218   DBUG_ASSERT(fixed == 0);
1219 
1220   Item *item= args[0];
1221 
1222   if (init_sum_func_check(thd))
1223     return TRUE;
1224 
1225   // 'item' can be changed during fix_fields
1226   if ((!item->fixed && item->fix_fields(thd, args)) ||
1227       (item= args[0])->check_cols(1))
1228     return TRUE;
1229   decimals=item->decimals;
1230 
1231   switch (hybrid_type= item->result_type()) {
1232   case INT_RESULT:
1233   case DECIMAL_RESULT:
1234   case STRING_RESULT:
1235     max_length= item->max_length;
1236     break;
1237   case REAL_RESULT:
1238     max_length= float_length(decimals);
1239     break;
1240   case ROW_RESULT:
1241   default:
1242     DBUG_ASSERT(0);
1243   };
1244   setup_hybrid(args[0], NULL);
1245   /* MIN/MAX can return NULL for empty set indepedent of the used column */
1246   maybe_null= 1;
1247   unsigned_flag=item->unsigned_flag;
1248   result_field=0;
1249   null_value=1;
1250   fix_length_and_dec();
1251   item= item->real_item();
1252   if (item->type() == Item::FIELD_ITEM)
1253     hybrid_field_type= ((Item_field*) item)->field->type();
1254   else
1255     hybrid_field_type= Item::field_type();
1256 
1257   if (check_sum_func(thd, ref))
1258     return TRUE;
1259 
1260   fixed= 1;
1261   return FALSE;
1262 }
1263 
1264 
1265 /**
1266   MIN/MAX function setup.
1267 
1268   @param item       argument of MIN/MAX function
1269   @param value_arg  calculated value of MIN/MAX function
1270 
1271   @details
1272     Setup cache/comparator of MIN/MAX functions. When called by the
1273     copy_or_same function value_arg parameter contains calculated value
1274     of the original MIN/MAX object and it is saved in this object's cache.
1275 */
1276 
setup_hybrid(Item * item,Item * value_arg)1277 void Item_sum_hybrid::setup_hybrid(Item *item, Item *value_arg)
1278 {
1279   value= Item_cache::get_cache(item);
1280   value->setup(item);
1281   value->store(value_arg);
1282   arg_cache= Item_cache::get_cache(item);
1283   arg_cache->setup(item);
1284   cmp= new Arg_comparator();
1285   cmp->set_cmp_func(this, (Item**)&arg_cache, (Item**)&value, FALSE);
1286   collation.set(item->collation);
1287 }
1288 
1289 
create_tmp_field(bool group,TABLE * table)1290 Field *Item_sum_hybrid::create_tmp_field(bool group, TABLE *table)
1291 {
1292   Field *field;
1293   if (args[0]->type() == Item::FIELD_ITEM)
1294   {
1295     field= ((Item_field*) args[0])->field;
1296 
1297     if ((field= create_tmp_field_from_field(current_thd, field, item_name.ptr(),
1298                                             table, NULL)))
1299       field->flags&= ~NOT_NULL_FLAG;
1300     return field;
1301   }
1302   /*
1303     DATE/TIME fields have STRING_RESULT result types.
1304     In order to preserve field type, it's needed to handle DATE/TIME
1305     fields creations separately.
1306   */
1307   switch (args[0]->field_type()) {
1308   case MYSQL_TYPE_DATE:
1309     field= new Field_newdate(maybe_null, item_name.ptr());
1310     break;
1311   case MYSQL_TYPE_TIME:
1312     field= new Field_timef(maybe_null, item_name.ptr(), decimals);
1313     break;
1314   case MYSQL_TYPE_TIMESTAMP:
1315   case MYSQL_TYPE_DATETIME:
1316     field= new Field_datetimef(maybe_null, item_name.ptr(), decimals);
1317     break;
1318   default:
1319     return Item_sum::create_tmp_field(group, table);
1320   }
1321   if (field)
1322     field->init(table);
1323   return field;
1324 }
1325 
1326 
1327 /***********************************************************************
1328 ** reset and add of sum_func
1329 ***********************************************************************/
1330 
1331 /**
1332   @todo
1333   check if the following assignments are really needed
1334 */
Item_sum_sum(THD * thd,Item_sum_sum * item)1335 Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item)
1336   :Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
1337    curr_dec_buff(item->curr_dec_buff)
1338 {
1339   /* TODO: check if the following assignments are really needed */
1340   if (hybrid_type == DECIMAL_RESULT)
1341   {
1342     my_decimal2decimal(item->dec_buffs, dec_buffs);
1343     my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
1344   }
1345   else
1346     sum= item->sum;
1347 }
1348 
copy_or_same(THD * thd)1349 Item *Item_sum_sum::copy_or_same(THD* thd)
1350 {
1351   return new (thd->mem_root) Item_sum_sum(thd, this);
1352 }
1353 
1354 
clear()1355 void Item_sum_sum::clear()
1356 {
1357   DBUG_ENTER("Item_sum_sum::clear");
1358   null_value=1;
1359   if (hybrid_type == DECIMAL_RESULT)
1360   {
1361     curr_dec_buff= 0;
1362     my_decimal_set_zero(dec_buffs);
1363   }
1364   else
1365     sum= 0.0;
1366   DBUG_VOID_RETURN;
1367 }
1368 
1369 
fix_length_and_dec()1370 void Item_sum_sum::fix_length_and_dec()
1371 {
1372   DBUG_ENTER("Item_sum_sum::fix_length_and_dec");
1373   maybe_null=null_value=1;
1374   decimals= args[0]->decimals;
1375   switch (args[0]->numeric_context_result_type()) {
1376   case REAL_RESULT:
1377     hybrid_type= REAL_RESULT;
1378     sum= 0.0;
1379     break;
1380   case INT_RESULT:
1381   case DECIMAL_RESULT:
1382   {
1383     /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
1384     int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
1385     max_length= my_decimal_precision_to_length_no_truncation(precision,
1386                                                              decimals,
1387                                                              unsigned_flag);
1388     curr_dec_buff= 0;
1389     hybrid_type= DECIMAL_RESULT;
1390     my_decimal_set_zero(dec_buffs);
1391     break;
1392   }
1393   case STRING_RESULT:
1394   case ROW_RESULT:
1395   default:
1396     DBUG_ASSERT(0);
1397   }
1398   DBUG_PRINT("info", ("Type: %s (%d, %d)",
1399                       (hybrid_type == REAL_RESULT ? "REAL_RESULT" :
1400                        hybrid_type == DECIMAL_RESULT ? "DECIMAL_RESULT" :
1401                        hybrid_type == INT_RESULT ? "INT_RESULT" :
1402                        "--ILLEGAL!!!--"),
1403                       max_length,
1404                       (int)decimals));
1405   DBUG_VOID_RETURN;
1406 }
1407 
1408 
add()1409 bool Item_sum_sum::add()
1410 {
1411   DBUG_ENTER("Item_sum_sum::add");
1412   if (hybrid_type == DECIMAL_RESULT)
1413   {
1414     my_decimal value;
1415     const my_decimal *val= aggr->arg_val_decimal(&value);
1416     if (!aggr->arg_is_null(true))
1417     {
1418       my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
1419                      val, dec_buffs + curr_dec_buff);
1420       curr_dec_buff^= 1;
1421       null_value= 0;
1422     }
1423   }
1424   else
1425   {
1426     sum+= aggr->arg_val_real();
1427     if (!aggr->arg_is_null(true))
1428       null_value= 0;
1429   }
1430   DBUG_RETURN(0);
1431 }
1432 
1433 
val_int()1434 longlong Item_sum_sum::val_int()
1435 {
1436   DBUG_ASSERT(fixed == 1);
1437   if (aggr)
1438     aggr->endup();
1439   if (hybrid_type == DECIMAL_RESULT)
1440   {
1441     longlong result;
1442     my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
1443                    &result);
1444     return result;
1445   }
1446   return (longlong) rint(val_real());
1447 }
1448 
1449 
val_real()1450 double Item_sum_sum::val_real()
1451 {
1452   DBUG_ASSERT(fixed == 1);
1453   if (aggr)
1454     aggr->endup();
1455   if (hybrid_type == DECIMAL_RESULT)
1456     my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
1457   return sum;
1458 }
1459 
1460 
val_str(String * str)1461 String *Item_sum_sum::val_str(String *str)
1462 {
1463   if (aggr)
1464     aggr->endup();
1465   if (hybrid_type == DECIMAL_RESULT)
1466     return val_string_from_decimal(str);
1467   return val_string_from_real(str);
1468 }
1469 
1470 
val_decimal(my_decimal * val)1471 my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
1472 {
1473   if (aggr)
1474     aggr->endup();
1475   if (hybrid_type == DECIMAL_RESULT)
1476     return (dec_buffs + curr_dec_buff);
1477   return val_decimal_from_real(val);
1478 }
1479 
1480 /**
1481   Aggregate a distinct row from the distinct hash table.
1482 
1483   Called for each row into the hash table 'Aggregator_distinct::table'.
1484   Includes the current distinct row into the calculation of the
1485   aggregate value. Uses the Field classes to get the value from the row.
1486   This function is used for AVG/SUM(DISTINCT). For COUNT(DISTINCT)
1487   it's called only when there are no blob arguments and the data don't
1488   fit into memory (so Unique makes persisted trees on disk).
1489 
1490   @param element     pointer to the row data.
1491 
1492   @return status
1493     @retval FALSE     success
1494     @retval TRUE      failure
1495 */
1496 
unique_walk_function(void * element)1497 bool Aggregator_distinct::unique_walk_function(void *element)
1498 {
1499   memcpy(table->field[0]->ptr, element, tree_key_length);
1500   item_sum->add();
1501   return 0;
1502 }
1503 
1504 
~Aggregator_distinct()1505 Aggregator_distinct::~Aggregator_distinct()
1506 {
1507   if (tree)
1508   {
1509     delete tree;
1510     tree= NULL;
1511   }
1512   if (table)
1513   {
1514     free_tmp_table(table->in_use, table);
1515     table=NULL;
1516   }
1517   if (tmp_table_param)
1518   {
1519     delete tmp_table_param;
1520     tmp_table_param= NULL;
1521   }
1522 }
1523 
1524 
arg_val_decimal(my_decimal * value)1525 my_decimal *Aggregator_simple::arg_val_decimal(my_decimal *value)
1526 {
1527   return item_sum->args[0]->val_decimal(value);
1528 }
1529 
1530 
arg_val_real()1531 double Aggregator_simple::arg_val_real()
1532 {
1533   return item_sum->args[0]->val_real();
1534 }
1535 
1536 
arg_is_null(bool use_null_value)1537 bool Aggregator_simple::arg_is_null(bool use_null_value)
1538 {
1539   Item **item= item_sum->args;
1540   const uint item_count= item_sum->arg_count;
1541   if (use_null_value)
1542   {
1543     for (uint i= 0; i < item_count; i++)
1544     {
1545       if (item[i]->null_value)
1546         return true;
1547     }
1548   }
1549   else
1550   {
1551     for (uint i= 0; i < item_count; i++)
1552     {
1553       if (item[i]->maybe_null && item[i]->is_null())
1554         return true;
1555     }
1556   }
1557   return false;
1558 }
1559 
1560 
arg_val_decimal(my_decimal * value)1561 my_decimal *Aggregator_distinct::arg_val_decimal(my_decimal * value)
1562 {
1563   return use_distinct_values ? table->field[0]->val_decimal(value) :
1564     item_sum->args[0]->val_decimal(value);
1565 }
1566 
1567 
arg_val_real()1568 double Aggregator_distinct::arg_val_real()
1569 {
1570   return use_distinct_values ? table->field[0]->val_real() :
1571     item_sum->args[0]->val_real();
1572 }
1573 
1574 
arg_is_null(bool use_null_value)1575 bool Aggregator_distinct::arg_is_null(bool use_null_value)
1576 {
1577   if (use_distinct_values)
1578   {
1579     const bool rc= table->field[0]->is_null();
1580     DBUG_ASSERT(!rc); // NULLs are never stored in 'tree'
1581     return rc;
1582   }
1583   return use_null_value ?
1584     item_sum->args[0]->null_value :
1585     (item_sum->args[0]->maybe_null && item_sum->args[0]->is_null());
1586 }
1587 
1588 
copy_or_same(THD * thd)1589 Item *Item_sum_count::copy_or_same(THD* thd)
1590 {
1591   return new (thd->mem_root) Item_sum_count(thd, this);
1592 }
1593 
1594 
clear()1595 void Item_sum_count::clear()
1596 {
1597   count= 0;
1598 }
1599 
1600 
add()1601 bool Item_sum_count::add()
1602 {
1603   if (aggr->arg_is_null(false))
1604     return 0;
1605   count++;
1606   return 0;
1607 }
1608 
val_int()1609 longlong Item_sum_count::val_int()
1610 {
1611   DBUG_ASSERT(fixed == 1);
1612   if (aggr)
1613     aggr->endup();
1614   return (longlong) count;
1615 }
1616 
1617 
cleanup()1618 void Item_sum_count::cleanup()
1619 {
1620   DBUG_ENTER("Item_sum_count::cleanup");
1621   count= 0;
1622   Item_sum_int::cleanup();
1623   DBUG_VOID_RETURN;
1624 }
1625 
1626 
1627 /*
1628   Avgerage
1629 */
fix_length_and_dec()1630 void Item_sum_avg::fix_length_and_dec()
1631 {
1632   Item_sum_sum::fix_length_and_dec();
1633   maybe_null=null_value=1;
1634   prec_increment= current_thd->variables.div_precincrement;
1635   if (hybrid_type == DECIMAL_RESULT)
1636   {
1637     int precision= args[0]->decimal_precision() + prec_increment;
1638     decimals= min<uint>(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1639     max_length= my_decimal_precision_to_length_no_truncation(precision,
1640                                                              decimals,
1641                                                              unsigned_flag);
1642     f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1643     f_scale=  args[0]->decimals;
1644     dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1645   }
1646   else {
1647     decimals= min<uint>(args[0]->decimals + prec_increment, NOT_FIXED_DEC);
1648     max_length= args[0]->max_length + prec_increment;
1649   }
1650 }
1651 
1652 
copy_or_same(THD * thd)1653 Item *Item_sum_avg::copy_or_same(THD* thd)
1654 {
1655   return new (thd->mem_root) Item_sum_avg(thd, this);
1656 }
1657 
1658 
create_tmp_field(bool group,TABLE * table)1659 Field *Item_sum_avg::create_tmp_field(bool group, TABLE *table)
1660 {
1661   Field *field;
1662   if (group)
1663   {
1664     /*
1665       We must store both value and counter in the temporary table in one field.
1666       The easiest way is to do this is to store both value in a string
1667       and unpack on access.
1668     */
1669     field= new Field_string(((hybrid_type == DECIMAL_RESULT) ?
1670                              dec_bin_size : sizeof(double)) + sizeof(longlong),
1671                             0, item_name.ptr(), &my_charset_bin);
1672   }
1673   else if (hybrid_type == DECIMAL_RESULT)
1674     field= Field_new_decimal::create_from_item(this);
1675   else
1676     field= new Field_double(max_length, maybe_null, item_name.ptr(), decimals, TRUE);
1677   if (field)
1678     field->init(table);
1679   return field;
1680 }
1681 
1682 
clear()1683 void Item_sum_avg::clear()
1684 {
1685   Item_sum_sum::clear();
1686   count=0;
1687 }
1688 
1689 
add()1690 bool Item_sum_avg::add()
1691 {
1692   if (Item_sum_sum::add())
1693     return TRUE;
1694   if (!aggr->arg_is_null(true))
1695     count++;
1696   return FALSE;
1697 }
1698 
val_real()1699 double Item_sum_avg::val_real()
1700 {
1701   DBUG_ASSERT(fixed == 1);
1702   if (aggr)
1703     aggr->endup();
1704   if (!count)
1705   {
1706     null_value=1;
1707     return 0.0;
1708   }
1709   return Item_sum_sum::val_real() / ulonglong2double(count);
1710 }
1711 
1712 
val_decimal(my_decimal * val)1713 my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
1714 {
1715   my_decimal sum_buff, cnt;
1716   const my_decimal *sum_dec;
1717   DBUG_ASSERT(fixed == 1);
1718   if (aggr)
1719     aggr->endup();
1720   if (!count)
1721   {
1722     null_value=1;
1723     return NULL;
1724   }
1725 
1726   /*
1727     For non-DECIMAL hybrid_type the division will be done in
1728     Item_sum_avg::val_real().
1729   */
1730   if (hybrid_type != DECIMAL_RESULT)
1731     return val_decimal_from_real(val);
1732 
1733   sum_dec= dec_buffs + curr_dec_buff;
1734   int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
1735   my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1736   return val;
1737 }
1738 
1739 
val_str(String * str)1740 String *Item_sum_avg::val_str(String *str)
1741 {
1742   if (aggr)
1743     aggr->endup();
1744   if (hybrid_type == DECIMAL_RESULT)
1745     return val_string_from_decimal(str);
1746   return val_string_from_real(str);
1747 }
1748 
1749 
1750 /*
1751   Standard deviation
1752 */
1753 
val_real()1754 double Item_sum_std::val_real()
1755 {
1756   DBUG_ASSERT(fixed == 1);
1757   double nr= Item_sum_variance::val_real();
1758   DBUG_ASSERT(nr >= 0.0);
1759   return sqrt(nr);
1760 }
1761 
copy_or_same(THD * thd)1762 Item *Item_sum_std::copy_or_same(THD* thd)
1763 {
1764   return new (thd->mem_root) Item_sum_std(thd, this);
1765 }
1766 
1767 
1768 /*
1769   Variance
1770 */
1771 
1772 
1773 /**
1774   Variance implementation for floating-point implementations, without
1775   catastrophic cancellation, from Knuth's _TAoCP_, 3rd ed, volume 2, pg232.
1776   This alters the value at m, s, and increments count.
1777 */
1778 
1779 /*
1780   These two functions are used by the Item_sum_variance and the
1781   Item_variance_field classes, which are unrelated, and each need to calculate
1782   variance.  The difference between the two classes is that the first is used
1783   for a mundane SELECT, while the latter is used in a GROUPing SELECT.
1784 */
variance_fp_recurrence_next(double * m,double * s,ulonglong * count,double nr)1785 static void variance_fp_recurrence_next(double *m, double *s, ulonglong *count, double nr)
1786 {
1787   *count += 1;
1788 
1789   if (*count == 1)
1790   {
1791     *m= nr;
1792     *s= 0;
1793   }
1794   else
1795   {
1796     double m_kminusone= *m;
1797     *m= m_kminusone + (nr - m_kminusone) / (double) *count;
1798     *s= *s + (nr - m_kminusone) * (nr - *m);
1799   }
1800 }
1801 
1802 
variance_fp_recurrence_result(double s,ulonglong count,bool is_sample_variance)1803 static double variance_fp_recurrence_result(double s, ulonglong count, bool is_sample_variance)
1804 {
1805   if (count == 1)
1806     return 0.0;
1807 
1808   if (is_sample_variance)
1809     return s / (count - 1);
1810 
1811   /* else, is a population variance */
1812   return s / count;
1813 }
1814 
1815 
Item_sum_variance(THD * thd,Item_sum_variance * item)1816 Item_sum_variance::Item_sum_variance(THD *thd, Item_sum_variance *item):
1817   Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
1818     count(item->count), sample(item->sample),
1819     prec_increment(item->prec_increment)
1820 {
1821   recurrence_m= item->recurrence_m;
1822   recurrence_s= item->recurrence_s;
1823 }
1824 
1825 
fix_length_and_dec()1826 void Item_sum_variance::fix_length_and_dec()
1827 {
1828   DBUG_ENTER("Item_sum_variance::fix_length_and_dec");
1829   maybe_null= null_value= 1;
1830 
1831   /*
1832     According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1833     aggregate function; paragraph 7h of Syntax Rules), "the declared
1834     type of the result is an implementation-defined aproximate numeric
1835     type.
1836   */
1837   hybrid_type= REAL_RESULT;
1838   decimals= NOT_FIXED_DEC;
1839   max_length= float_length(decimals);
1840 
1841   DBUG_PRINT("info", ("Type: REAL_RESULT (%d, %d)", max_length, (int)decimals));
1842   DBUG_VOID_RETURN;
1843 }
1844 
1845 
copy_or_same(THD * thd)1846 Item *Item_sum_variance::copy_or_same(THD* thd)
1847 {
1848   return new (thd->mem_root) Item_sum_variance(thd, this);
1849 }
1850 
1851 
1852 /**
1853   Create a new field to match the type of value we're expected to yield.
1854   If we're grouping, then we need some space to serialize variables into, to
1855   pass around.
1856 */
create_tmp_field(bool group,TABLE * table)1857 Field *Item_sum_variance::create_tmp_field(bool group, TABLE *table)
1858 {
1859   Field *field;
1860   if (group)
1861   {
1862     /*
1863       We must store both value and counter in the temporary table in one field.
1864       The easiest way is to do this is to store both value in a string
1865       and unpack on access.
1866     */
1867     field= new Field_string(sizeof(double)*2 + sizeof(longlong), 0, item_name.ptr(), &my_charset_bin);
1868   }
1869   else
1870     field= new Field_double(max_length, maybe_null, item_name.ptr(), decimals, TRUE);
1871 
1872   if (field != NULL)
1873     field->init(table);
1874 
1875   return field;
1876 }
1877 
1878 
clear()1879 void Item_sum_variance::clear()
1880 {
1881   count= 0;
1882 }
1883 
add()1884 bool Item_sum_variance::add()
1885 {
1886   /*
1887     Why use a temporary variable?  We don't know if it is null until we
1888     evaluate it, which has the side-effect of setting null_value .
1889   */
1890   double nr= args[0]->val_real();
1891 
1892   if (!args[0]->null_value)
1893     variance_fp_recurrence_next(&recurrence_m, &recurrence_s, &count, nr);
1894   return 0;
1895 }
1896 
val_real()1897 double Item_sum_variance::val_real()
1898 {
1899   DBUG_ASSERT(fixed == 1);
1900 
1901   /*
1902     'sample' is a 1/0 boolean value.  If it is 1/true, id est this is a sample
1903     variance call, then we should set nullness when the count of the items
1904     is one or zero.  If it's zero, i.e. a population variance, then we only
1905     set nullness when the count is zero.
1906 
1907     Another way to read it is that 'sample' is the numerical threshhold, at and
1908     below which a 'count' number of items is called NULL.
1909   */
1910   DBUG_ASSERT((sample == 0) || (sample == 1));
1911   if (count <= sample)
1912   {
1913     null_value=1;
1914     return 0.0;
1915   }
1916 
1917   null_value=0;
1918   return variance_fp_recurrence_result(recurrence_s, count, sample);
1919 }
1920 
1921 
val_decimal(my_decimal * dec_buf)1922 my_decimal *Item_sum_variance::val_decimal(my_decimal *dec_buf)
1923 {
1924   DBUG_ASSERT(fixed == 1);
1925   return val_decimal_from_real(dec_buf);
1926 }
1927 
1928 
reset_field()1929 void Item_sum_variance::reset_field()
1930 {
1931   double nr;
1932   uchar *res= result_field->ptr;
1933 
1934   nr= args[0]->val_real();              /* sets null_value as side-effect */
1935 
1936   if (args[0]->null_value)
1937     memset(res, 0, sizeof(double)*2+sizeof(longlong));
1938   else
1939   {
1940     /* Serialize format is (double)m, (double)s, (longlong)count */
1941     ulonglong tmp_count;
1942     double tmp_s;
1943     float8store(res, nr);               /* recurrence variable m */
1944     tmp_s= 0.0;
1945     float8store(res + sizeof(double), tmp_s);
1946     tmp_count= 1;
1947     int8store(res + sizeof(double)*2, tmp_count);
1948   }
1949 }
1950 
1951 
update_field()1952 void Item_sum_variance::update_field()
1953 {
1954   ulonglong field_count;
1955   uchar *res=result_field->ptr;
1956 
1957   double nr= args[0]->val_real();       /* sets null_value as side-effect */
1958 
1959   if (args[0]->null_value)
1960     return;
1961 
1962   /* Serialize format is (double)m, (double)s, (longlong)count */
1963   double field_recurrence_m, field_recurrence_s;
1964   float8get(field_recurrence_m, res);
1965   float8get(field_recurrence_s, res + sizeof(double));
1966   field_count=sint8korr(res+sizeof(double)*2);
1967 
1968   variance_fp_recurrence_next(&field_recurrence_m, &field_recurrence_s, &field_count, nr);
1969 
1970   float8store(res, field_recurrence_m);
1971   float8store(res + sizeof(double), field_recurrence_s);
1972   res+= sizeof(double)*2;
1973   int8store(res,field_count);
1974 }
1975 
1976 
1977 /* min & max */
1978 
clear()1979 void Item_sum_hybrid::clear()
1980 {
1981   value->clear();
1982   null_value= 1;
1983 }
1984 
val_real()1985 double Item_sum_hybrid::val_real()
1986 {
1987   DBUG_ASSERT(fixed == 1);
1988   if (null_value)
1989     return 0.0;
1990   double retval= value->val_real();
1991   if ((null_value= value->null_value))
1992     DBUG_ASSERT(retval == 0.0);
1993   return retval;
1994 }
1995 
val_int()1996 longlong Item_sum_hybrid::val_int()
1997 {
1998   DBUG_ASSERT(fixed == 1);
1999   if (null_value)
2000     return 0;
2001   longlong retval= value->val_int();
2002   if ((null_value= value->null_value))
2003     DBUG_ASSERT(retval == 0);
2004   return retval;
2005 }
2006 
2007 
val_time_temporal()2008 longlong Item_sum_hybrid::val_time_temporal()
2009 {
2010   DBUG_ASSERT(fixed == 1);
2011   if (null_value)
2012     return 0;
2013   longlong retval= value->val_time_temporal();
2014   if ((null_value= value->null_value))
2015     DBUG_ASSERT(retval == 0);
2016   return retval;
2017 }
2018 
2019 
val_date_temporal()2020 longlong Item_sum_hybrid::val_date_temporal()
2021 {
2022   DBUG_ASSERT(fixed == 1);
2023   if (null_value)
2024     return 0;
2025   longlong retval= value->val_date_temporal();
2026   if ((null_value= value->null_value))
2027     DBUG_ASSERT(retval == 0);
2028   return retval;
2029 }
2030 
2031 
val_decimal(my_decimal * val)2032 my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
2033 {
2034   DBUG_ASSERT(fixed == 1);
2035   if (null_value)
2036     return 0;
2037   my_decimal *retval= value->val_decimal(val);
2038   if ((null_value= value->null_value))
2039     DBUG_ASSERT(retval == NULL);
2040   return retval;
2041 }
2042 
2043 
get_date(MYSQL_TIME * ltime,uint fuzzydate)2044 bool Item_sum_hybrid::get_date(MYSQL_TIME *ltime, uint fuzzydate)
2045 {
2046   DBUG_ASSERT(fixed == 1);
2047   if (null_value)
2048     return true;
2049   return (null_value= value->get_date(ltime, fuzzydate));
2050 }
2051 
2052 
get_time(MYSQL_TIME * ltime)2053 bool Item_sum_hybrid::get_time(MYSQL_TIME *ltime)
2054 {
2055   DBUG_ASSERT(fixed == 1);
2056   if (null_value)
2057     return true;
2058   return (null_value= value->get_time(ltime));
2059 }
2060 
2061 
2062 String *
val_str(String * str)2063 Item_sum_hybrid::val_str(String *str)
2064 {
2065   DBUG_ASSERT(fixed == 1);
2066   if (null_value)
2067     return 0;
2068   String *retval= value->val_str(str);
2069   if ((null_value= value->null_value))
2070     DBUG_ASSERT(retval == NULL);
2071   return retval;
2072 }
2073 
2074 
cleanup()2075 void Item_sum_hybrid::cleanup()
2076 {
2077   DBUG_ENTER("Item_sum_hybrid::cleanup");
2078   Item_sum::cleanup();
2079   forced_const= FALSE;
2080   if (cmp)
2081     delete cmp;
2082   cmp= 0;
2083   /*
2084     by default it is TRUE to avoid TRUE reporting by
2085     Item_func_not_all/Item_func_nop_all if this item was never called.
2086 
2087     no_rows_in_result() set it to FALSE if was not results found.
2088     If some results found it will be left unchanged.
2089   */
2090   was_values= TRUE;
2091   DBUG_VOID_RETURN;
2092 }
2093 
no_rows_in_result()2094 void Item_sum_hybrid::no_rows_in_result()
2095 {
2096   was_values= FALSE;
2097   clear();
2098 }
2099 
2100 
copy_or_same(THD * thd)2101 Item *Item_sum_min::copy_or_same(THD* thd)
2102 {
2103   Item_sum_min *item= new (thd->mem_root) Item_sum_min(thd, this);
2104   item->setup_hybrid(args[0], value);
2105   return item;
2106 }
2107 
2108 
add()2109 bool Item_sum_min::add()
2110 {
2111   /* args[0] < value */
2112   arg_cache->cache_value();
2113   if (!arg_cache->null_value &&
2114       (null_value || cmp->compare() < 0))
2115   {
2116     value->store(arg_cache);
2117     value->cache_value();
2118     null_value= 0;
2119   }
2120   return 0;
2121 }
2122 
2123 
copy_or_same(THD * thd)2124 Item *Item_sum_max::copy_or_same(THD* thd)
2125 {
2126   Item_sum_max *item= new (thd->mem_root) Item_sum_max(thd, this);
2127   item->setup_hybrid(args[0], value);
2128   return item;
2129 }
2130 
2131 
add()2132 bool Item_sum_max::add()
2133 {
2134   /* args[0] > value */
2135   arg_cache->cache_value();
2136   if (!arg_cache->null_value &&
2137       (null_value || cmp->compare() > 0))
2138   {
2139     value->store(arg_cache);
2140     value->cache_value();
2141     null_value= 0;
2142   }
2143   return 0;
2144 }
2145 
2146 
2147 /* bit_or and bit_and */
2148 
val_int()2149 longlong Item_sum_bit::val_int()
2150 {
2151   DBUG_ASSERT(fixed == 1);
2152   return (longlong) bits;
2153 }
2154 
2155 
clear()2156 void Item_sum_bit::clear()
2157 {
2158   bits= reset_bits;
2159 }
2160 
copy_or_same(THD * thd)2161 Item *Item_sum_or::copy_or_same(THD* thd)
2162 {
2163   return new (thd->mem_root) Item_sum_or(thd, this);
2164 }
2165 
2166 
add()2167 bool Item_sum_or::add()
2168 {
2169   ulonglong value= (ulonglong) args[0]->val_int();
2170   if (!args[0]->null_value)
2171     bits|=value;
2172   return 0;
2173 }
2174 
copy_or_same(THD * thd)2175 Item *Item_sum_xor::copy_or_same(THD* thd)
2176 {
2177   return new (thd->mem_root) Item_sum_xor(thd, this);
2178 }
2179 
2180 
add()2181 bool Item_sum_xor::add()
2182 {
2183   ulonglong value= (ulonglong) args[0]->val_int();
2184   if (!args[0]->null_value)
2185     bits^=value;
2186   return 0;
2187 }
2188 
copy_or_same(THD * thd)2189 Item *Item_sum_and::copy_or_same(THD* thd)
2190 {
2191   return new (thd->mem_root) Item_sum_and(thd, this);
2192 }
2193 
2194 
add()2195 bool Item_sum_and::add()
2196 {
2197   ulonglong value= (ulonglong) args[0]->val_int();
2198   if (!args[0]->null_value)
2199     bits&=value;
2200   return 0;
2201 }
2202 
2203 /************************************************************************
2204 ** reset result of a Item_sum with is saved in a tmp_table
2205 *************************************************************************/
2206 
reset_field()2207 void Item_sum_num::reset_field()
2208 {
2209   double nr= args[0]->val_real();
2210   uchar *res=result_field->ptr;
2211 
2212   if (maybe_null)
2213   {
2214     if (args[0]->null_value)
2215     {
2216       nr=0.0;
2217       result_field->set_null();
2218     }
2219     else
2220       result_field->set_notnull();
2221   }
2222   float8store(res,nr);
2223 }
2224 
2225 
reset_field()2226 void Item_sum_hybrid::reset_field()
2227 {
2228   switch(hybrid_type) {
2229   case STRING_RESULT:
2230   {
2231     if (args[0]->is_temporal())
2232     {
2233       longlong nr= args[0]->val_temporal_by_field_type();
2234       if (maybe_null)
2235       {
2236         if (args[0]->null_value)
2237         {
2238           nr= 0;
2239           result_field->set_null();
2240         }
2241         else
2242           result_field->set_notnull();
2243       }
2244       result_field->store_packed(nr);
2245       break;
2246     }
2247 
2248     char buff[MAX_FIELD_WIDTH];
2249     String tmp(buff,sizeof(buff),result_field->charset()),*res;
2250 
2251     res=args[0]->val_str(&tmp);
2252     if (args[0]->null_value)
2253     {
2254       result_field->set_null();
2255       result_field->reset();
2256     }
2257     else
2258     {
2259       result_field->set_notnull();
2260       result_field->store(res->ptr(),res->length(),tmp.charset());
2261     }
2262     break;
2263   }
2264   case INT_RESULT:
2265   {
2266     longlong nr=args[0]->val_int();
2267 
2268     if (maybe_null)
2269     {
2270       if (args[0]->null_value)
2271       {
2272 	nr=0;
2273 	result_field->set_null();
2274       }
2275       else
2276 	result_field->set_notnull();
2277     }
2278     result_field->store(nr, unsigned_flag);
2279     break;
2280   }
2281   case REAL_RESULT:
2282   {
2283     double nr= args[0]->val_real();
2284 
2285     if (maybe_null)
2286     {
2287       if (args[0]->null_value)
2288       {
2289 	nr=0.0;
2290 	result_field->set_null();
2291       }
2292       else
2293 	result_field->set_notnull();
2294     }
2295     result_field->store(nr);
2296     break;
2297   }
2298   case DECIMAL_RESULT:
2299   {
2300     my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2301 
2302     if (maybe_null)
2303     {
2304       if (args[0]->null_value)
2305         result_field->set_null();
2306       else
2307         result_field->set_notnull();
2308     }
2309     /*
2310       We must store zero in the field as we will use the field value in
2311       add()
2312     */
2313     if (!arg_dec)                               // Null
2314       arg_dec= &decimal_zero;
2315     result_field->store_decimal(arg_dec);
2316     break;
2317   }
2318   case ROW_RESULT:
2319   default:
2320     DBUG_ASSERT(0);
2321   }
2322 }
2323 
2324 
reset_field()2325 void Item_sum_sum::reset_field()
2326 {
2327   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2328   if (hybrid_type == DECIMAL_RESULT)
2329   {
2330     my_decimal value, *arg_val= args[0]->val_decimal(&value);
2331     if (!arg_val)                               // Null
2332       arg_val= &decimal_zero;
2333     result_field->store_decimal(arg_val);
2334   }
2335   else
2336   {
2337     DBUG_ASSERT(hybrid_type == REAL_RESULT);
2338     double nr= args[0]->val_real();			// Nulls also return 0
2339     float8store(result_field->ptr, nr);
2340   }
2341   if (args[0]->null_value)
2342     result_field->set_null();
2343   else
2344     result_field->set_notnull();
2345 }
2346 
2347 
reset_field()2348 void Item_sum_count::reset_field()
2349 {
2350   uchar *res=result_field->ptr;
2351   longlong nr=0;
2352   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2353 
2354   if (!args[0]->maybe_null || !args[0]->is_null())
2355     nr=1;
2356   int8store(res,nr);
2357 }
2358 
2359 
reset_field()2360 void Item_sum_avg::reset_field()
2361 {
2362   uchar *res=result_field->ptr;
2363   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2364   if (hybrid_type == DECIMAL_RESULT)
2365   {
2366     longlong tmp;
2367     my_decimal value, *arg_dec= args[0]->val_decimal(&value);
2368     if (args[0]->null_value)
2369     {
2370       arg_dec= &decimal_zero;
2371       tmp= 0;
2372     }
2373     else
2374       tmp= 1;
2375     my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
2376     res+= dec_bin_size;
2377     int8store(res, tmp);
2378   }
2379   else
2380   {
2381     double nr= args[0]->val_real();
2382 
2383     if (args[0]->null_value)
2384       memset(res, 0, sizeof(double)+sizeof(longlong));
2385     else
2386     {
2387       longlong tmp= 1;
2388       float8store(res,nr);
2389       res+=sizeof(double);
2390       int8store(res,tmp);
2391     }
2392   }
2393 }
2394 
2395 
reset_field()2396 void Item_sum_bit::reset_field()
2397 {
2398   reset_and_add();
2399   int8store(result_field->ptr, bits);
2400 }
2401 
update_field()2402 void Item_sum_bit::update_field()
2403 {
2404   uchar *res=result_field->ptr;
2405   bits= uint8korr(res);
2406   add();
2407   int8store(res, bits);
2408 }
2409 
2410 
2411 /**
2412   calc next value and merge it with field_value.
2413 */
2414 
update_field()2415 void Item_sum_sum::update_field()
2416 {
2417   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2418   if (hybrid_type == DECIMAL_RESULT)
2419   {
2420     my_decimal value, *arg_val= args[0]->val_decimal(&value);
2421     if (!args[0]->null_value)
2422     {
2423       if (!result_field->is_null())
2424       {
2425         my_decimal field_value,
2426                    *field_val= result_field->val_decimal(&field_value);
2427         my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2428         result_field->store_decimal(dec_buffs);
2429       }
2430       else
2431       {
2432         result_field->store_decimal(arg_val);
2433         result_field->set_notnull();
2434       }
2435     }
2436   }
2437   else
2438   {
2439     double old_nr,nr;
2440     uchar *res=result_field->ptr;
2441 
2442     float8get(old_nr,res);
2443     nr= args[0]->val_real();
2444     if (!args[0]->null_value)
2445     {
2446       old_nr+=nr;
2447       result_field->set_notnull();
2448     }
2449     float8store(res,old_nr);
2450   }
2451 }
2452 
2453 
update_field()2454 void Item_sum_count::update_field()
2455 {
2456   longlong nr;
2457   uchar *res=result_field->ptr;
2458 
2459   nr=sint8korr(res);
2460   if (!args[0]->maybe_null || !args[0]->is_null())
2461     nr++;
2462   int8store(res,nr);
2463 }
2464 
2465 
update_field()2466 void Item_sum_avg::update_field()
2467 {
2468   longlong field_count;
2469   uchar *res=result_field->ptr;
2470 
2471   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2472 
2473   if (hybrid_type == DECIMAL_RESULT)
2474   {
2475     my_decimal value, *arg_val= args[0]->val_decimal(&value);
2476     if (!args[0]->null_value)
2477     {
2478       binary2my_decimal(E_DEC_FATAL_ERROR, res,
2479                         dec_buffs + 1, f_precision, f_scale);
2480       field_count= sint8korr(res + dec_bin_size);
2481       my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
2482       my_decimal2binary(E_DEC_FATAL_ERROR, dec_buffs,
2483                         res, f_precision, f_scale);
2484       res+= dec_bin_size;
2485       field_count++;
2486       int8store(res, field_count);
2487     }
2488   }
2489   else
2490   {
2491     double nr;
2492 
2493     nr= args[0]->val_real();
2494     if (!args[0]->null_value)
2495     {
2496       double old_nr;
2497       float8get(old_nr, res);
2498       field_count= sint8korr(res + sizeof(double));
2499       old_nr+= nr;
2500       float8store(res,old_nr);
2501       res+= sizeof(double);
2502       field_count++;
2503       int8store(res, field_count);
2504     }
2505   }
2506 }
2507 
2508 
update_field()2509 void Item_sum_hybrid::update_field()
2510 {
2511   switch (hybrid_type) {
2512   case STRING_RESULT:
2513     if (args[0]->is_temporal())
2514       min_max_update_temporal_field();
2515     else
2516       min_max_update_str_field();
2517     break;
2518   case INT_RESULT:
2519     min_max_update_int_field();
2520     break;
2521   case DECIMAL_RESULT:
2522     min_max_update_decimal_field();
2523     break;
2524   default:
2525     min_max_update_real_field();
2526   }
2527 }
2528 
2529 
min_max_update_temporal_field()2530 void Item_sum_hybrid::min_max_update_temporal_field()
2531 {
2532   longlong nr, old_nr;
2533   old_nr= result_field->val_temporal_by_field_type();
2534   nr= args[0]->val_temporal_by_field_type();
2535   if (!args[0]->null_value)
2536   {
2537     if (result_field->is_null(0))
2538       old_nr= nr;
2539     else
2540     {
2541       bool res= unsigned_flag ?
2542                 (ulonglong) old_nr > (ulonglong) nr : old_nr > nr;
2543       if ((cmp_sign > 0) ^ (!res))
2544         old_nr= nr;
2545     }
2546     result_field->set_notnull();
2547   }
2548   else if (result_field->is_null(0))
2549     result_field->set_null();
2550   result_field->store_packed(old_nr);
2551 }
2552 
2553 
min_max_update_str_field()2554 void Item_sum_hybrid::min_max_update_str_field()
2555 {
2556   DBUG_ASSERT(cmp);
2557   String *res_str=args[0]->val_str(&cmp->value1);
2558 
2559   if (!args[0]->null_value)
2560   {
2561     result_field->val_str(&cmp->value2);
2562 
2563     if (result_field->is_null() ||
2564 	(cmp_sign * sortcmp(res_str,&cmp->value2,collation.collation)) < 0)
2565       result_field->store(res_str->ptr(),res_str->length(),res_str->charset());
2566     result_field->set_notnull();
2567   }
2568 }
2569 
2570 
min_max_update_real_field()2571 void Item_sum_hybrid::min_max_update_real_field()
2572 {
2573   double nr,old_nr;
2574 
2575   old_nr=result_field->val_real();
2576   nr= args[0]->val_real();
2577   if (!args[0]->null_value)
2578   {
2579     if (result_field->is_null(0) ||
2580 	(cmp_sign > 0 ? old_nr > nr : old_nr < nr))
2581       old_nr=nr;
2582     result_field->set_notnull();
2583   }
2584   else if (result_field->is_null(0))
2585     result_field->set_null();
2586   result_field->store(old_nr);
2587 }
2588 
2589 
min_max_update_int_field()2590 void Item_sum_hybrid::min_max_update_int_field()
2591 {
2592   longlong nr,old_nr;
2593 
2594   old_nr=result_field->val_int();
2595   nr=args[0]->val_int();
2596   if (!args[0]->null_value)
2597   {
2598     if (result_field->is_null(0))
2599       old_nr=nr;
2600     else
2601     {
2602       bool res=(unsigned_flag ?
2603 		(ulonglong) old_nr > (ulonglong) nr :
2604 		old_nr > nr);
2605       /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2606       if ((cmp_sign > 0) ^ (!res))
2607 	old_nr=nr;
2608     }
2609     result_field->set_notnull();
2610   }
2611   else if (result_field->is_null(0))
2612     result_field->set_null();
2613   result_field->store(old_nr, unsigned_flag);
2614 }
2615 
2616 
2617 /**
2618   @todo
2619   optimize: do not get result_field in case of args[0] is NULL
2620 */
min_max_update_decimal_field()2621 void Item_sum_hybrid::min_max_update_decimal_field()
2622 {
2623   /* TODO: optimize: do not get result_field in case of args[0] is NULL */
2624   my_decimal old_val, nr_val;
2625   const my_decimal *old_nr= result_field->val_decimal(&old_val);
2626   const my_decimal *nr= args[0]->val_decimal(&nr_val);
2627   if (!args[0]->null_value)
2628   {
2629     if (result_field->is_null(0))
2630       old_nr=nr;
2631     else
2632     {
2633       bool res= my_decimal_cmp(old_nr, nr) > 0;
2634       /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2635       if ((cmp_sign > 0) ^ (!res))
2636         old_nr=nr;
2637     }
2638     result_field->set_notnull();
2639   }
2640   else if (result_field->is_null(0))
2641     result_field->set_null();
2642   result_field->store_decimal(old_nr);
2643 }
2644 
2645 
Item_avg_field(Item_result res_type,Item_sum_avg * item)2646 Item_avg_field::Item_avg_field(Item_result res_type, Item_sum_avg *item)
2647 {
2648   item_name= item->item_name;
2649   decimals=item->decimals;
2650   max_length= item->max_length;
2651   unsigned_flag= item->unsigned_flag;
2652   field=item->result_field;
2653   maybe_null=1;
2654   hybrid_type= res_type;
2655   prec_increment= item->prec_increment;
2656   if (hybrid_type == DECIMAL_RESULT)
2657   {
2658     f_scale= item->f_scale;
2659     f_precision= item->f_precision;
2660     dec_bin_size= item->dec_bin_size;
2661   }
2662 }
2663 
val_real()2664 double Item_avg_field::val_real()
2665 {
2666   // fix_fields() never calls for this Item
2667   double nr;
2668   longlong count;
2669   uchar *res;
2670 
2671   if (hybrid_type == DECIMAL_RESULT)
2672     return val_real_from_decimal();
2673 
2674   float8get(nr,field->ptr);
2675   res= (field->ptr+sizeof(double));
2676   count= sint8korr(res);
2677 
2678   if ((null_value= !count))
2679     return 0.0;
2680   return nr/(double) count;
2681 }
2682 
2683 
val_decimal(my_decimal * dec_buf)2684 my_decimal *Item_avg_field::val_decimal(my_decimal *dec_buf)
2685 {
2686   // fix_fields() never calls for this Item
2687   if (hybrid_type == REAL_RESULT)
2688     return val_decimal_from_real(dec_buf);
2689 
2690   longlong count= sint8korr(field->ptr + dec_bin_size);
2691   if ((null_value= !count))
2692     return 0;
2693 
2694   my_decimal dec_count, dec_field;
2695   binary2my_decimal(E_DEC_FATAL_ERROR,
2696                     field->ptr, &dec_field, f_precision, f_scale);
2697   int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
2698   my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2699                  &dec_field, &dec_count, prec_increment);
2700   return dec_buf;
2701 }
2702 
2703 
val_str(String * str)2704 String *Item_avg_field::val_str(String *str)
2705 {
2706   // fix_fields() never calls for this Item
2707   if (hybrid_type == DECIMAL_RESULT)
2708     return val_string_from_decimal(str);
2709   return val_string_from_real(str);
2710 }
2711 
2712 
Item_std_field(Item_sum_std * item)2713 Item_std_field::Item_std_field(Item_sum_std *item)
2714   : Item_variance_field(item)
2715 {
2716 }
2717 
2718 
val_real()2719 double Item_std_field::val_real()
2720 {
2721   double nr;
2722   // fix_fields() never calls for this Item
2723   nr= Item_variance_field::val_real();
2724   DBUG_ASSERT(nr >= 0.0);
2725   return sqrt(nr);
2726 }
2727 
2728 
val_decimal(my_decimal * dec_buf)2729 my_decimal *Item_std_field::val_decimal(my_decimal *dec_buf)
2730 {
2731   /*
2732     We can't call val_decimal_from_real() for DECIMAL_RESULT as
2733     Item_variance_field::val_real() would cause an infinite loop
2734   */
2735   my_decimal tmp_dec, *dec;
2736   double nr;
2737   if (hybrid_type == REAL_RESULT)
2738     return val_decimal_from_real(dec_buf);
2739 
2740   dec= Item_variance_field::val_decimal(dec_buf);
2741   if (!dec)
2742     return 0;
2743   my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2744   DBUG_ASSERT(nr >= 0.0);
2745   nr= sqrt(nr);
2746   double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2747   my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, FALSE, dec_buf);
2748   return dec_buf;
2749 }
2750 
2751 
Item_variance_field(Item_sum_variance * item)2752 Item_variance_field::Item_variance_field(Item_sum_variance *item)
2753 {
2754   item_name= item->item_name;
2755   decimals=item->decimals;
2756   max_length=item->max_length;
2757   unsigned_flag= item->unsigned_flag;
2758   field=item->result_field;
2759   maybe_null=1;
2760   sample= item->sample;
2761   prec_increment= item->prec_increment;
2762   if ((hybrid_type= item->hybrid_type) == DECIMAL_RESULT)
2763   {
2764     f_scale0= item->f_scale0;
2765     f_precision0= item->f_precision0;
2766     dec_bin_size0= item->dec_bin_size0;
2767     f_scale1= item->f_scale1;
2768     f_precision1= item->f_precision1;
2769     dec_bin_size1= item->dec_bin_size1;
2770   }
2771 }
2772 
2773 
val_real()2774 double Item_variance_field::val_real()
2775 {
2776   // fix_fields() never calls for this Item
2777   if (hybrid_type == DECIMAL_RESULT)
2778     return val_real_from_decimal();
2779 
2780   double recurrence_s;
2781   ulonglong count;
2782   float8get(recurrence_s, (field->ptr + sizeof(double)));
2783   count=sint8korr(field->ptr+sizeof(double)*2);
2784 
2785   if ((null_value= (count <= sample)))
2786     return 0.0;
2787 
2788   return variance_fp_recurrence_result(recurrence_s, count, sample);
2789 }
2790 
2791 
2792 /****************************************************************************
2793 ** Functions to handle dynamic loadable aggregates
2794 ** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2795 ** Adapted for UDAs by: Andreas F. Bobak <bobak@relog.ch>.
2796 ** Rewritten by: Monty.
2797 ****************************************************************************/
2798 
2799 #ifdef HAVE_DLOPEN
2800 
clear()2801 void Item_udf_sum::clear()
2802 {
2803   DBUG_ENTER("Item_udf_sum::clear");
2804   udf.clear();
2805   DBUG_VOID_RETURN;
2806 }
2807 
add()2808 bool Item_udf_sum::add()
2809 {
2810   DBUG_ENTER("Item_udf_sum::add");
2811   udf.add(&null_value);
2812   DBUG_RETURN(0);
2813 }
2814 
cleanup()2815 void Item_udf_sum::cleanup()
2816 {
2817   /*
2818     udf_handler::cleanup() nicely handles case when we have not
2819     original item but one created by copy_or_same() method.
2820   */
2821   udf.cleanup();
2822   Item_sum::cleanup();
2823 }
2824 
2825 
print(String * str,enum_query_type query_type)2826 void Item_udf_sum::print(String *str, enum_query_type query_type)
2827 {
2828   str->append(func_name());
2829   str->append('(');
2830   for (uint i=0 ; i < arg_count ; i++)
2831   {
2832     if (i)
2833       str->append(',');
2834     args[i]->print(str, query_type);
2835   }
2836   str->append(')');
2837 }
2838 
2839 
copy_or_same(THD * thd)2840 Item *Item_sum_udf_float::copy_or_same(THD* thd)
2841 {
2842   return new (thd->mem_root) Item_sum_udf_float(thd, this);
2843 }
2844 
val_real()2845 double Item_sum_udf_float::val_real()
2846 {
2847   DBUG_ASSERT(fixed == 1);
2848   DBUG_ENTER("Item_sum_udf_float::val");
2849   DBUG_PRINT("info",("result_type: %d  arg_count: %d",
2850 		     args[0]->result_type(), arg_count));
2851   DBUG_RETURN(udf.val(&null_value));
2852 }
2853 
2854 
val_str(String * str)2855 String *Item_sum_udf_float::val_str(String *str)
2856 {
2857   return val_string_from_real(str);
2858 }
2859 
2860 
val_decimal(my_decimal * dec)2861 my_decimal *Item_sum_udf_float::val_decimal(my_decimal *dec)
2862 {
2863   return val_decimal_from_real(dec);
2864 }
2865 
2866 
val_str(String * str)2867 String *Item_sum_udf_decimal::val_str(String *str)
2868 {
2869   return val_string_from_decimal(str);
2870 }
2871 
2872 
val_real()2873 double Item_sum_udf_decimal::val_real()
2874 {
2875   return val_real_from_decimal();
2876 }
2877 
2878 
val_int()2879 longlong Item_sum_udf_decimal::val_int()
2880 {
2881   return val_int_from_decimal();
2882 }
2883 
2884 
val_decimal(my_decimal * dec_buf)2885 my_decimal *Item_sum_udf_decimal::val_decimal(my_decimal *dec_buf)
2886 {
2887   DBUG_ASSERT(fixed == 1);
2888   DBUG_ENTER("Item_func_udf_decimal::val_decimal");
2889   DBUG_PRINT("info",("result_type: %d  arg_count: %d",
2890                      args[0]->result_type(), arg_count));
2891 
2892   DBUG_RETURN(udf.val_decimal(&null_value, dec_buf));
2893 }
2894 
2895 
copy_or_same(THD * thd)2896 Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
2897 {
2898   return new (thd->mem_root) Item_sum_udf_decimal(thd, this);
2899 }
2900 
2901 
copy_or_same(THD * thd)2902 Item *Item_sum_udf_int::copy_or_same(THD* thd)
2903 {
2904   return new (thd->mem_root) Item_sum_udf_int(thd, this);
2905 }
2906 
val_int()2907 longlong Item_sum_udf_int::val_int()
2908 {
2909   DBUG_ASSERT(fixed == 1);
2910   DBUG_ENTER("Item_sum_udf_int::val_int");
2911   DBUG_PRINT("info",("result_type: %d  arg_count: %d",
2912 		     args[0]->result_type(), arg_count));
2913   DBUG_RETURN(udf.val_int(&null_value));
2914 }
2915 
2916 
val_str(String * str)2917 String *Item_sum_udf_int::val_str(String *str)
2918 {
2919   return val_string_from_int(str);
2920 }
2921 
val_decimal(my_decimal * dec)2922 my_decimal *Item_sum_udf_int::val_decimal(my_decimal *dec)
2923 {
2924   return val_decimal_from_int(dec);
2925 }
2926 
2927 
2928 /** Default max_length is max argument length. */
2929 
fix_length_and_dec()2930 void Item_sum_udf_str::fix_length_and_dec()
2931 {
2932   DBUG_ENTER("Item_sum_udf_str::fix_length_and_dec");
2933   max_length=0;
2934   for (uint i = 0; i < arg_count; i++)
2935     set_if_bigger(max_length,args[i]->max_length);
2936   DBUG_VOID_RETURN;
2937 }
2938 
2939 
copy_or_same(THD * thd)2940 Item *Item_sum_udf_str::copy_or_same(THD* thd)
2941 {
2942   return new (thd->mem_root) Item_sum_udf_str(thd, this);
2943 }
2944 
2945 
val_decimal(my_decimal * dec)2946 my_decimal *Item_sum_udf_str::val_decimal(my_decimal *dec)
2947 {
2948   return val_decimal_from_string(dec);
2949 }
2950 
val_str(String * str)2951 String *Item_sum_udf_str::val_str(String *str)
2952 {
2953   DBUG_ASSERT(fixed == 1);
2954   DBUG_ENTER("Item_sum_udf_str::str");
2955   String *res=udf.val_str(str,&str_value);
2956   null_value = !res;
2957   DBUG_RETURN(res);
2958 }
2959 
2960 #endif /* HAVE_DLOPEN */
2961 
2962 
2963 /*****************************************************************************
2964  GROUP_CONCAT function
2965 
2966  SQL SYNTAX:
2967   GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
2968     [SEPARATOR str_const])
2969 
2970  concat of values from "group by" operation
2971 
2972  BUGS
2973    Blobs doesn't work with DISTINCT or ORDER BY
2974 *****************************************************************************/
2975 
2976 
2977 
2978 /**
2979   Compares the values for fields in expr list of GROUP_CONCAT.
2980   @note
2981 
2982      GROUP_CONCAT([DISTINCT] expr [,expr ...]
2983               [ORDER BY {unsigned_integer | col_name | expr}
2984                   [ASC | DESC] [,col_name ...]]
2985               [SEPARATOR str_val])
2986 
2987   @return
2988   @retval -1 : key1 < key2
2989   @retval  0 : key1 = key2
2990   @retval  1 : key1 > key2
2991 */
2992 
2993 extern "C"
group_concat_key_cmp_with_distinct(const void * arg,const void * key1,const void * key2)2994 int group_concat_key_cmp_with_distinct(const void* arg, const void* key1,
2995                                        const void* key2)
2996 {
2997   Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
2998   TABLE *table= item_func->table;
2999 
3000   for (uint i= 0; i < item_func->arg_count_field; i++)
3001   {
3002     Item *item= item_func->args[i];
3003     /*
3004       If item is a const item then either get_tmp_table_field returns 0
3005       or it is an item over a const table.
3006     */
3007     if (item->const_item())
3008       continue;
3009     /*
3010       We have to use get_tmp_table_field() instead of
3011       real_item()->get_tmp_table_field() because we want the field in
3012       the temporary table, not the original field
3013     */
3014     Field *field= item->get_tmp_table_field();
3015 
3016     if (!field)
3017       continue;
3018 
3019     uint offset= field->offset(field->table->record[0])-table->s->null_bytes;
3020     int res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset);
3021     if (res)
3022       return res;
3023   }
3024   return 0;
3025 }
3026 
3027 
3028 /**
3029   function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
3030 */
3031 
3032 extern "C"
group_concat_key_cmp_with_order(const void * arg,const void * key1,const void * key2)3033 int group_concat_key_cmp_with_order(const void* arg, const void* key1,
3034                                     const void* key2)
3035 {
3036   const Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
3037   ORDER **order_item, **end;
3038   TABLE *table= grp_item->table;
3039 
3040   for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
3041        order_item < end;
3042        order_item++)
3043   {
3044     Item *item= *(*order_item)->item;
3045     /*
3046       If item is a const item then either get_tmp_table_field returns 0
3047       or it is an item over a const table.
3048     */
3049     if (item->const_item())
3050       continue;
3051     /*
3052       We have to use get_tmp_table_field() instead of
3053       real_item()->get_tmp_table_field() because we want the field in
3054       the temporary table, not the original field
3055      */
3056     Field *field= item->get_tmp_table_field();
3057     if (!field)
3058       continue;
3059 
3060     uint offset= (field->offset(field->table->record[0]) -
3061                   table->s->null_bytes);
3062     int res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset);
3063     if (res)
3064       return ((*order_item)->direction == ORDER::ORDER_ASC) ? res : -res;
3065   }
3066   /*
3067     We can't return 0 because in that case the tree class would remove this
3068     item as double value. This would cause problems for case-changes and
3069     if the returned values are not the same we do the sort on.
3070   */
3071   return 1;
3072 }
3073 
3074 
3075 /**
3076   Append data from current leaf to item->result.
3077 */
3078 
3079 extern "C"
dump_leaf_key(void * key_arg,element_count count MY_ATTRIBUTE ((unused)),void * item_arg)3080 int dump_leaf_key(void* key_arg, element_count count MY_ATTRIBUTE((unused)),
3081                   void* item_arg)
3082 {
3083   Item_func_group_concat *item= (Item_func_group_concat *) item_arg;
3084   TABLE *table= item->table;
3085   String tmp((char *)table->record[1], table->s->reclength,
3086              default_charset_info);
3087   String tmp2;
3088   uchar *key= (uchar *) key_arg;
3089   String *result= &item->result;
3090   Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
3091   uint old_length= result->length();
3092 
3093   if (item->no_appended)
3094     item->no_appended= FALSE;
3095   else
3096     result->append(*item->separator);
3097 
3098   tmp.length(0);
3099 
3100   for (; arg < arg_end; arg++)
3101   {
3102     String *res;
3103     /*
3104       We have to use get_tmp_table_field() instead of
3105       real_item()->get_tmp_table_field() because we want the field in
3106       the temporary table, not the original field
3107       We also can't use table->field array to access the fields
3108       because it contains both order and arg list fields.
3109      */
3110     if ((*arg)->const_item())
3111       res= (*arg)->val_str(&tmp);
3112     else
3113     {
3114       Field *field= (*arg)->get_tmp_table_field();
3115       if (field)
3116       {
3117         uint offset= (field->offset(field->table->record[0]) -
3118                       table->s->null_bytes);
3119         DBUG_ASSERT(offset < table->s->reclength);
3120         res= field->val_str(&tmp, key + offset);
3121       }
3122       else
3123         res= (*arg)->val_str(&tmp);
3124     }
3125     if (res)
3126       result->append(*res);
3127   }
3128 
3129   item->row_count++;
3130 
3131   /* stop if length of result more than max_length */
3132   if (result->length() > item->max_length)
3133   {
3134     int well_formed_error;
3135     const CHARSET_INFO *cs= item->collation.collation;
3136     const char *ptr= result->ptr();
3137     uint add_length;
3138     /*
3139       It's ok to use item->result.length() as the fourth argument
3140       as this is never used to limit the length of the data.
3141       Cut is done with the third argument.
3142     */
3143     add_length= cs->cset->well_formed_len(cs,
3144                                           ptr + old_length,
3145                                           ptr + item->max_length,
3146                                           result->length(),
3147                                           &well_formed_error);
3148     result->length(old_length + add_length);
3149     item->warning_for_row= TRUE;
3150     push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
3151                         ER_CUT_VALUE_GROUP_CONCAT, ER(ER_CUT_VALUE_GROUP_CONCAT),
3152                         item->row_count);
3153 
3154     /**
3155        To avoid duplicated warnings in Item_func_group_concat::val_str()
3156     */
3157     if (table && table->blob_storage)
3158       table->blob_storage->set_truncated_value(false);
3159     return 1;
3160   }
3161   return 0;
3162 }
3163 
3164 
3165 /**
3166   Constructor of Item_func_group_concat.
3167 
3168   @param distinct_arg   distinct
3169   @param select_list    list of expression for show values
3170   @param order_list     list of sort columns
3171   @param separator_arg  string value of separator.
3172 */
3173 
3174 Item_func_group_concat::
Item_func_group_concat(Name_resolution_context * context_arg,bool distinct_arg,List<Item> * select_list,const SQL_I_List<ORDER> & order_list,String * separator_arg)3175 Item_func_group_concat(Name_resolution_context *context_arg,
3176                        bool distinct_arg, List<Item> *select_list,
3177                        const SQL_I_List<ORDER> &order_list,
3178                        String *separator_arg)
3179   :tmp_table_param(0), separator(separator_arg), tree(0),
3180    unique_filter(NULL), table(0),
3181    order(0), context(context_arg),
3182    arg_count_order(order_list.elements),
3183    arg_count_field(select_list->elements),
3184    row_count(0),
3185    distinct(distinct_arg),
3186    warning_for_row(FALSE),
3187    force_copy_fields(0), original(0)
3188 {
3189   Item *item_select;
3190   Item **arg_ptr;
3191 
3192   quick_group= FALSE;
3193   arg_count= arg_count_field + arg_count_order;
3194 
3195   /*
3196     We need to allocate:
3197     args - arg_count_field+arg_count_order
3198            (for possible order items in temporare tables)
3199     order - arg_count_order
3200   */
3201   if (!(args= (Item**) sql_alloc(sizeof(Item*) * arg_count +
3202                                  sizeof(ORDER*)*arg_count_order)))
3203     return;
3204 
3205   order= (ORDER**)(args + arg_count);
3206 
3207   /* fill args items of show and sort */
3208   List_iterator_fast<Item> li(*select_list);
3209 
3210   for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
3211     *arg_ptr= item_select;
3212 
3213   if (arg_count_order)
3214   {
3215     ORDER **order_ptr= order;
3216     for (ORDER *order_item= order_list.first;
3217          order_item != NULL;
3218          order_item= order_item->next)
3219     {
3220       (*order_ptr++)= order_item;
3221       *arg_ptr= *order_item->item;
3222       order_item->item= arg_ptr++;
3223     }
3224   }
3225 }
3226 
3227 
Item_func_group_concat(THD * thd,Item_func_group_concat * item)3228 Item_func_group_concat::Item_func_group_concat(THD *thd,
3229                                                Item_func_group_concat *item)
3230   :Item_sum(thd, item),
3231   tmp_table_param(item->tmp_table_param),
3232   separator(item->separator),
3233   tree(item->tree),
3234   unique_filter(item->unique_filter),
3235   table(item->table),
3236   context(item->context),
3237   arg_count_order(item->arg_count_order),
3238   arg_count_field(item->arg_count_field),
3239   row_count(item->row_count),
3240   distinct(item->distinct),
3241   warning_for_row(item->warning_for_row),
3242   always_null(item->always_null),
3243   force_copy_fields(item->force_copy_fields),
3244   original(item)
3245 {
3246   quick_group= item->quick_group;
3247   result.set_charset(collation.collation);
3248 
3249   /*
3250     Since the ORDER structures pointed to by the elements of the 'order' array
3251     may be modified in find_order_in_list() called from
3252     Item_func_group_concat::setup(), create a copy of those structures so that
3253     such modifications done in this object would not have any effect on the
3254     object being copied.
3255   */
3256   ORDER *tmp;
3257   if (!(order= (ORDER **) thd->alloc(sizeof(ORDER *) * arg_count_order +
3258                                      sizeof(ORDER) * arg_count_order)))
3259     return;
3260   tmp= (ORDER *)(order + arg_count_order);
3261   for (uint i= 0; i < arg_count_order; i++, tmp++)
3262   {
3263     /*
3264       Compiler generated copy constructor is used to
3265       to copy all the members of ORDER struct.
3266       It's also necessary to update ORDER::next pointer
3267       so that it points to new ORDER element.
3268     */
3269     new (tmp) st_order(*(item->order[i]));
3270     tmp->next= (i + 1 == arg_count_order) ? NULL : (tmp + 1);
3271     order[i]= tmp;
3272   }
3273 }
3274 
3275 
3276 
cleanup()3277 void Item_func_group_concat::cleanup()
3278 {
3279   DBUG_ENTER("Item_func_group_concat::cleanup");
3280   Item_sum::cleanup();
3281 
3282   /*
3283     Free table and tree if they belong to this item (if item have not pointer
3284     to original item from which was made copy => it own its objects )
3285   */
3286   if (!original)
3287   {
3288     delete tmp_table_param;
3289     tmp_table_param= 0;
3290     if (table)
3291     {
3292       THD *thd= table->in_use;
3293       if (table->blob_storage)
3294         delete table->blob_storage;
3295       free_tmp_table(thd, table);
3296       table= 0;
3297       if (tree)
3298       {
3299         delete_tree(tree);
3300         tree= 0;
3301       }
3302       if (unique_filter)
3303       {
3304         delete unique_filter;
3305         unique_filter= NULL;
3306       }
3307     }
3308     DBUG_ASSERT(tree == 0);
3309   }
3310   /*
3311     As the ORDER structures pointed to by the elements of the
3312     'order' array may be modified in find_order_in_list() called
3313     from Item_func_group_concat::setup() to point to runtime
3314     created objects, we need to reset them back to the original
3315     arguments of the function.
3316   */
3317   ORDER **order_ptr= order;
3318   for (uint i= 0; i < arg_count_order; i++)
3319   {
3320 
3321     if ((*order_ptr)->counter_used)
3322       args[arg_count_field + i]= (*order_ptr)->item_ptr;
3323     order_ptr++;
3324   }
3325   DBUG_VOID_RETURN;
3326 }
3327 
3328 
make_string_field(TABLE * table)3329 Field *Item_func_group_concat::make_string_field(TABLE *table)
3330 {
3331   Field *field;
3332   DBUG_ASSERT(collation.collation);
3333   /*
3334     max_characters is maximum number of characters
3335     what can fit into max_length size. It's necessary
3336     to use field size what allows to store group_concat
3337     result without truncation. For this purpose we use
3338     max_characters * CS->mbmaxlen.
3339   */
3340   const uint32 max_characters= max_length / collation.collation->mbminlen;
3341   if (max_characters > CONVERT_IF_BIGGER_TO_BLOB)
3342     field= new Field_blob(max_characters * collation.collation->mbmaxlen,
3343                           maybe_null, item_name.ptr(), collation.collation, TRUE);
3344   else
3345     field= new Field_varstring(max_characters * collation.collation->mbmaxlen,
3346                                maybe_null, item_name.ptr(), table->s, collation.collation);
3347 
3348   if (field)
3349     field->init(table);
3350   return field;
3351 }
3352 
3353 
copy_or_same(THD * thd)3354 Item *Item_func_group_concat::copy_or_same(THD* thd)
3355 {
3356   return new (thd->mem_root) Item_func_group_concat(thd, this);
3357 }
3358 
3359 
clear()3360 void Item_func_group_concat::clear()
3361 {
3362   result.length(0);
3363   result.copy();
3364   null_value= TRUE;
3365   warning_for_row= FALSE;
3366   no_appended= TRUE;
3367   if (tree)
3368     reset_tree(tree);
3369   if (unique_filter)
3370     unique_filter->reset();
3371   if (table && table->blob_storage)
3372     table->blob_storage->reset();
3373   /* No need to reset the table as we never call write_row */
3374 }
3375 
3376 
add()3377 bool Item_func_group_concat::add()
3378 {
3379   if (always_null)
3380     return 0;
3381   copy_fields(tmp_table_param);
3382   if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
3383     return TRUE;
3384 
3385   for (uint i= 0; i < arg_count_field; i++)
3386   {
3387     Item *show_item= args[i];
3388     if (show_item->const_item())
3389       continue;
3390 
3391     Field *field= show_item->get_tmp_table_field();
3392     if (field && field->is_null_in_record((const uchar*) table->record[0]))
3393         return 0;                               // Skip row if it contains null
3394   }
3395 
3396   null_value= FALSE;
3397   bool row_eligible= TRUE;
3398 
3399   if (distinct)
3400   {
3401     /* Filter out duplicate rows. */
3402     uint count= unique_filter->elements_in_tree();
3403     unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3404     if (count == unique_filter->elements_in_tree())
3405       row_eligible= FALSE;
3406   }
3407 
3408   TREE_ELEMENT *el= 0;                          // Only for safety
3409   if (row_eligible && tree)
3410   {
3411     DBUG_EXECUTE_IF("trigger_OOM_in_gconcat_add",
3412                      DBUG_SET("+d,simulate_persistent_out_of_memory"););
3413     el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
3414                     tree->custom_arg);
3415     DBUG_EXECUTE_IF("trigger_OOM_in_gconcat_add",
3416                     DBUG_SET("-d,simulate_persistent_out_of_memory"););
3417     /* check if there was enough memory to insert the row */
3418     if (!el)
3419       return 1;
3420   }
3421   /*
3422     If the row is not a duplicate (el->count == 1)
3423     we can dump the row here in case of GROUP_CONCAT(DISTINCT...)
3424     instead of doing tree traverse later.
3425   */
3426   if (row_eligible && !warning_for_row &&
3427       (!tree || (el->count == 1 && distinct && !arg_count_order)))
3428     dump_leaf_key(table->record[0] + table->s->null_bytes, 1, this);
3429 
3430   return 0;
3431 }
3432 
3433 
3434 bool
fix_fields(THD * thd,Item ** ref)3435 Item_func_group_concat::fix_fields(THD *thd, Item **ref)
3436 {
3437   uint i;                       /* for loop variable */
3438   DBUG_ASSERT(fixed == 0);
3439 
3440   if (init_sum_func_check(thd))
3441     return TRUE;
3442 
3443   maybe_null= 1;
3444 
3445   /*
3446     Fix fields for select list and ORDER clause
3447   */
3448 
3449   for (i=0 ; i < arg_count ; i++)
3450   {
3451     if ((!args[i]->fixed &&
3452          args[i]->fix_fields(thd, args + i)) ||
3453         args[i]->check_cols(1))
3454       return TRUE;
3455   }
3456 
3457   /* skip charset aggregation for order columns */
3458   if (agg_item_charsets_for_string_result(collation, func_name(),
3459                                           args, arg_count - arg_count_order))
3460     return 1;
3461 
3462   result.set_charset(collation.collation);
3463   result_field= 0;
3464   null_value= 1;
3465   max_length= thd->variables.group_concat_max_len;
3466 
3467   uint32 offset;
3468   if (separator->needs_conversion(separator->length(), separator->charset(),
3469                                   collation.collation, &offset))
3470   {
3471     uint32 buflen= collation.collation->mbmaxlen * separator->length();
3472     uint errors, conv_length;
3473     char *buf;
3474     String *new_separator;
3475 
3476     if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
3477         !(new_separator= new(thd->stmt_arena->mem_root)
3478                            String(buf, buflen, collation.collation)))
3479       return TRUE;
3480 
3481     conv_length= copy_and_convert(buf, buflen, collation.collation,
3482                                   separator->ptr(), separator->length(),
3483                                   separator->charset(), &errors);
3484     new_separator->length(conv_length);
3485     separator= new_separator;
3486   }
3487 
3488   if (check_sum_func(thd, ref))
3489     return TRUE;
3490 
3491   fixed= 1;
3492   return FALSE;
3493 }
3494 
3495 
setup(THD * thd)3496 bool Item_func_group_concat::setup(THD *thd)
3497 {
3498   List<Item> list;
3499   SELECT_LEX *select_lex= thd->lex->current_select;
3500   const bool order_or_distinct= MY_TEST(arg_count_order > 0 || distinct);
3501   DBUG_ENTER("Item_func_group_concat::setup");
3502 
3503   /*
3504     Currently setup() can be called twice. Please add
3505     assertion here when this is fixed.
3506   */
3507   if (table || tree)
3508     DBUG_RETURN(FALSE);
3509 
3510   if (!(tmp_table_param= new TMP_TABLE_PARAM))
3511     DBUG_RETURN(TRUE);
3512 
3513   /* Push all not constant fields to the list and create a temp table */
3514   always_null= 0;
3515   for (uint i= 0; i < arg_count_field; i++)
3516   {
3517     Item *item= args[i];
3518     if (list.push_back(item))
3519       DBUG_RETURN(TRUE);
3520     if (item->const_item())
3521     {
3522       if (item->is_null())
3523       {
3524         always_null= 1;
3525         DBUG_RETURN(FALSE);
3526       }
3527     }
3528   }
3529 
3530   List<Item> all_fields(list);
3531   /*
3532     Try to find every ORDER expression in the list of GROUP_CONCAT
3533     arguments. If an expression is not found, prepend it to
3534     "all_fields". The resulting field list is used as input to create
3535     tmp table columns.
3536   */
3537   if (arg_count_order &&
3538       setup_order(thd, Ref_ptr_array(args, arg_count),
3539                   context->table_list, list, all_fields, *order))
3540     DBUG_RETURN(TRUE);
3541 
3542   count_field_types(select_lex, tmp_table_param, all_fields, 0);
3543   tmp_table_param->force_copy_fields= force_copy_fields;
3544   DBUG_ASSERT(table == 0);
3545   if (order_or_distinct)
3546   {
3547     /*
3548       Force the create_tmp_table() to convert BIT columns to INT
3549       as we cannot compare two table records containg BIT fields
3550       stored in the the tree used for distinct/order by.
3551       Moreover we don't even save in the tree record null bits
3552       where BIT fields store parts of their data.
3553     */
3554     List_iterator_fast<Item> li(all_fields);
3555     Item *item;
3556     while ((item= li++))
3557     {
3558       if (item->type() == Item::FIELD_ITEM &&
3559           ((Item_field*) item)->field->type() == FIELD_TYPE_BIT)
3560         item->marker= 4;
3561     }
3562   }
3563 
3564   /*
3565     We have to create a temporary table to get descriptions of fields
3566     (types, sizes and so on).
3567 
3568     Note that in the table, we first have the ORDER BY fields, then the
3569     field list.
3570   */
3571   if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
3572                                 (ORDER*) 0, 0, TRUE,
3573                                 (select_lex->options | thd->variables.option_bits),
3574                                 HA_POS_ERROR, (char*) "")))
3575     DBUG_RETURN(TRUE);
3576   table->file->extra(HA_EXTRA_NO_ROWS);
3577   table->no_rows= 1;
3578 
3579   /**
3580     Initialize blob_storage if GROUP_CONCAT is used
3581     with ORDER BY | DISTINCT and BLOB field count > 0.
3582   */
3583   if (order_or_distinct && table->s->blob_fields)
3584     table->blob_storage= new Blob_mem_storage();
3585 
3586   /*
3587      Need sorting or uniqueness: init tree and choose a function to sort.
3588      Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3589      the row is not added to the result.
3590   */
3591   uint tree_key_length= table->s->reclength - table->s->null_bytes;
3592 
3593   if (arg_count_order)
3594   {
3595     tree= &tree_base;
3596     /*
3597       Create a tree for sorting. The tree is used to sort (according to the
3598       syntax of this function). If there is no ORDER BY clause, we don't
3599       create this tree.
3600     */
3601     init_tree(tree,  min<uint>(thd->variables.max_heap_table_size,
3602                                thd->variables.sortbuff_size/16), 0,
3603               tree_key_length,
3604               group_concat_key_cmp_with_order , 0, NULL, (void*) this);
3605   }
3606 
3607   if (distinct)
3608     unique_filter= new Unique(group_concat_key_cmp_with_distinct,
3609                               (void*)this,
3610                               tree_key_length,
3611                               ram_limitation(thd));
3612 
3613   DBUG_RETURN(FALSE);
3614 }
3615 
3616 
3617 /* This is used by rollup to create a separate usable copy of the function */
3618 
make_unique()3619 void Item_func_group_concat::make_unique()
3620 {
3621   tmp_table_param= 0;
3622   table=0;
3623   original= 0;
3624   force_copy_fields= 1;
3625   tree= 0;
3626 }
3627 
3628 
val_str(String * str)3629 String* Item_func_group_concat::val_str(String* str)
3630 {
3631   DBUG_ASSERT(fixed == 1);
3632   if (null_value)
3633     return 0;
3634   if (no_appended && tree)
3635     /* Tree is used for sorting as in ORDER BY */
3636     tree_walk(tree, &dump_leaf_key, this, left_root_right);
3637 
3638   if (table && table->blob_storage &&
3639       table->blob_storage->is_truncated_value())
3640   {
3641     warning_for_row= true;
3642     push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
3643                         ER_CUT_VALUE_GROUP_CONCAT, ER(ER_CUT_VALUE_GROUP_CONCAT),
3644                         row_count);
3645   }
3646 
3647   return &result;
3648 }
3649 
3650 
print(String * str,enum_query_type query_type)3651 void Item_func_group_concat::print(String *str, enum_query_type query_type)
3652 {
3653   str->append(STRING_WITH_LEN("group_concat("));
3654   if (distinct)
3655     str->append(STRING_WITH_LEN("distinct "));
3656   for (uint i= 0; i < arg_count_field; i++)
3657   {
3658     if (i)
3659       str->append(',');
3660     args[i]->print(str, query_type);
3661   }
3662   if (arg_count_order)
3663   {
3664     str->append(STRING_WITH_LEN(" order by "));
3665     for (uint i= 0 ; i < arg_count_order ; i++)
3666     {
3667       if (i)
3668         str->append(',');
3669       args[i + arg_count_field]->print(str, query_type);
3670       if (order[i]->direction == ORDER::ORDER_ASC)
3671         str->append(STRING_WITH_LEN(" ASC"));
3672       else
3673         str->append(STRING_WITH_LEN(" DESC"));
3674     }
3675   }
3676   str->append(STRING_WITH_LEN(" separator \'"));
3677   str->append(*separator);
3678   str->append(STRING_WITH_LEN("\')"));
3679 }
3680 
3681 
~Item_func_group_concat()3682 Item_func_group_concat::~Item_func_group_concat()
3683 {
3684   if (!original && unique_filter)
3685     delete unique_filter;
3686 }
3687