1 /* Copyright (c) 2000, 2015, Oracle and/or its affiliates.
2    Copyright (c) 2008, 2020, MariaDB
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 as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
16 
17 
18 /**
19   @file
force()20 
21   @brief
22   Sum functions (COUNT, MIN...)
23 */
24 
25 #ifdef USE_PRAGMA_IMPLEMENTATION
26 #pragma implementation				// gcc: Class implementation
27 #endif
28 
29 #include "mariadb.h"
30 #include "sql_priv.h"
31 #include "sql_select.h"
32 #include "uniques.h"
33 #include "sp_rcontext.h"
34 #include "sp.h"
35 #include "sql_parse.h"
36 #include "sp_head.h"
37 
38 /**
39   Calculate the affordable RAM limit for structures like TREE or Unique
40   used in Item_sum_*
41 */
42 
43 size_t Item_sum::ram_limitation(THD *thd)
44 {
45   return (size_t)MY_MIN(thd->variables.tmp_memory_table_size,
46                 thd->variables.max_heap_table_size);
47 }
48 
49 
50 /**
51   Prepare an aggregate function item for checking context conditions.
52 
53     The function initializes the members of the Item_sum object created
54     for a set function that are used to check validity of the set function
new() -> TokenStream55     occurrence.
56     If the set function is not allowed in any subquery where it occurs
57     an error is reported immediately.
58 
59   @param thd      reference to the thread context info
60 
61   @note
62     This function is to be called for any item created for a set function
63     object when the traversal of trees built for expressions used in the query
64     is performed at the phase of context analysis. This function is to
65     be invoked at the descent of this traversal.
66   @retval
67     TRUE   if an error is reported
68   @retval
69     FALSE  otherwise
70 */
71 
72 bool Item_sum::init_sum_func_check(THD *thd)
73 {
74   SELECT_LEX *curr_sel= thd->lex->current_select;
75   if (curr_sel && curr_sel->name_visibility_map.is_clear_all())
76   {
77     for (SELECT_LEX *sl= curr_sel; sl; sl= sl->context.outer_select())
78     {
79       curr_sel->name_visibility_map.set_bit(sl->nest_level);
80     }
81   }
82   if (!curr_sel ||
83       !(thd->lex->allow_sum_func.is_overlapping(curr_sel->name_visibility_map)))
84   {
85     my_message(ER_INVALID_GROUP_FUNC_USE, ER_THD(thd, ER_INVALID_GROUP_FUNC_USE),
86                MYF(0));
87     return TRUE;
88   }
89   /* Set a reference to the nesting set function if there is  any */
90   in_sum_func= thd->lex->in_sum_func;
91   /* Save a pointer to object to be used in items for nested set functions */
92   thd->lex->in_sum_func= this;
93   nest_level= thd->lex->current_select->nest_level;
94   ref_by= 0;
95   aggr_level= -1;
96   aggr_sel= NULL;
97   max_arg_level= -1;
98   max_sum_func_level= -1;
99   outer_fields.empty();
100   return FALSE;
101 }
102 
103 /**
104   Check constraints imposed on a usage of a set function.
105 
106     The method verifies whether context conditions imposed on a usage
107     of any set function are met for this occurrence.
108 
109     The function first checks if we are using any window functions as
110     arguments to the set function. In that case it returns an error.
111 
112     Afterwards, it checks whether the set function occurs in the position where it
drop(&mut self)113     can be aggregated and, when it happens to occur in argument of another
114     set function, the method checks that these two functions are aggregated in
115     different subqueries.
116     If the context conditions are not met the method reports an error.
117     If the set function is aggregated in some outer subquery the method
118     adds it to the chain of items for such set functions that is attached
119     to the the st_select_lex structure for this subquery.
120 
121     A number of designated members of the object are used to check the
122     conditions. They are specified in the comment before the Item_sum
123     class declaration.
124     Additionally a bitmap variable called allow_sum_func is employed.
125     It is included into the thd->lex structure.
126     The bitmap contains 1 at n-th position if the set function happens
127     to occur under a construct of the n-th level subquery where usage
128     of set functions are allowed (i.e either in the SELECT list or
129     in the HAVING clause of the corresponding subquery)
130     Consider the query:
131     @code
132        SELECT SUM(t1.b) FROM t1 GROUP BY t1.a
133          HAVING t1.a IN (SELECT t2.c FROM t2 WHERE AVG(t1.b) > 20) AND
134                 t1.a > (SELECT MIN(t2.d) FROM t2);
135     @endcode
136     allow_sum_func will contain:
137     - for SUM(t1.b) - 1 at the first position
138     - for AVG(t1.b) - 1 at the first position, 0 at the second position
139     - for MIN(t2.d) - 1 at the first position, 1 at the second position.
140 
141   @param thd  reference to the thread context info
142   @param ref  location of the pointer to this item in the embedding expression
143 
144   @note
145     This function is to be called for any item created for a set function
146     object when the traversal of trees built for expressions used in the query
147     is performed at the phase of context analysis. This function is to
148     be invoked at the ascent of this traversal.
149 
150   @retval
151     TRUE   if an error is reported
152   @retval
153     FALSE  otherwise
154 */
155 
156 bool Item_sum::check_sum_func(THD *thd, Item **ref)
157 {
158   SELECT_LEX *curr_sel= thd->lex->current_select;
159   nesting_map allow_sum_func(thd->lex->allow_sum_func);
160   allow_sum_func.intersect(curr_sel->name_visibility_map);
161   bool invalid= FALSE;
162   // should be set already
163   DBUG_ASSERT(!curr_sel->name_visibility_map.is_clear_all());
164 
165   /*
166      Window functions can not be used as arguments to sum functions.
167      Aggregation happes before window function computation, so there
168      are no values to aggregate over.
169   */
170   if (with_window_func)
171   {
172     my_message(ER_SUM_FUNC_WITH_WINDOW_FUNC_AS_ARG,
173                ER_THD(thd, ER_SUM_FUNC_WITH_WINDOW_FUNC_AS_ARG),
174                MYF(0));
175     return TRUE;
176   }
177 
178   if (window_func_sum_expr_flag)
179     return false;
180   /*
181     The value of max_arg_level is updated if an argument of the set function
182     contains a column reference resolved  against a subquery whose level is
183     greater than the current value of max_arg_level.
184     max_arg_level cannot be greater than nest level.
185     nest level is always >= 0
186   */
187   if (nest_level == max_arg_level)
188   {
189     /*
190       The function must be aggregated in the current subquery,
191       If it is there under a construct where it is not allowed
192       we report an error.
193     */
194     invalid= !(allow_sum_func.is_set(max_arg_level));
195   }
196   else if (max_arg_level >= 0 ||
197            !(allow_sum_func.is_set(nest_level)))
198   {
199     /*
200       The set function can be aggregated only in outer subqueries.
201       Try to find a subquery where it can be aggregated;
202       If we fail to find such a subquery report an error.
203     */
204     if (register_sum_func(thd, ref))
205       return TRUE;
206     invalid= aggr_level < 0 &&
207              !(allow_sum_func.is_set(nest_level));
208     if (!invalid && thd->variables.sql_mode & MODE_ANSI)
209       invalid= aggr_level < 0 && max_arg_level < nest_level;
210   }
211   if (!invalid && aggr_level < 0)
212   {
213     aggr_level= nest_level;
214     aggr_sel= curr_sel;
215   }
216   /*
217     By this moment we either found a subquery where the set function is
218     to be aggregated  and assigned a value that is  >= 0 to aggr_level,
219     or set the value of 'invalid' to TRUE to report later an error.
220   */
221   /*
222     Additionally we have to check whether possible nested set functions
223     are acceptable here: they are not, if the level of aggregation of
224     some of them is less than aggr_level.
225   */
226   if (!invalid)
227     invalid= aggr_level <= max_sum_func_level;
228   if (invalid)
229   {
230     my_message(ER_INVALID_GROUP_FUNC_USE,
231                ER_THD(thd, ER_INVALID_GROUP_FUNC_USE),
232                MYF(0));
233     return TRUE;
234   }
235 
236   if (in_sum_func)
237   {
238     /*
239       If the set function is nested adjust the value of
240       max_sum_func_level for the nesting set function.
241       We take into account only enclosed set functions that are to be
242       aggregated on the same level or above of the nest level of
243       the enclosing set function.
244       But we must always pass up the max_sum_func_level because it is
245       the maximum nested level of all directly and indirectly enclosed
246       set functions. We must do that even for set functions that are
247       aggregated inside of their enclosing set function's nest level
248       because the enclosing function may contain another enclosing
249       function that is to be aggregated outside or on the same level
250       as its parent's nest level.
251     */
252     if (in_sum_func->nest_level >= aggr_level)
253       set_if_bigger(in_sum_func->max_sum_func_level, aggr_level);
254     set_if_bigger(in_sum_func->max_sum_func_level, max_sum_func_level);
255   }
256 
257   /*
258     Check that non-aggregated fields and sum functions aren't mixed in the
259     same select in the ONLY_FULL_GROUP_BY mode.
260   */
261   if (outer_fields.elements)
262   {
263     Item_field *field;
264     /*
265       Here we compare the nesting level of the select to which an outer field
266       belongs to with the aggregation level of the sum function. All fields in
267       the outer_fields list are checked.
268 
269       If the nesting level is equal to the aggregation level then the field is
270         aggregated by this sum function.
271       If the nesting level is less than the aggregation level then the field
272         belongs to an outer select. In this case if there is an embedding sum
273         function add current field to functions outer_fields list. If there is
274         no embedding function then the current field treated as non aggregated
275         and the select it belongs to is marked accordingly.
276       If the nesting level is greater than the aggregation level then it means
277         that this field was added by an inner sum function.
278         Consider an example:
279 
280           select avg ( <-- we are here, checking outer.f1
281             select (
282               select sum(outer.f1 + inner.f1) from inner
283             ) from outer)
284           from most_outer;
285 
286         In this case we check that no aggregate functions are used in the
287         select the field belongs to. If there are some then an error is
288         raised.
289     */
290     List_iterator<Item_field> of(outer_fields);
291     while ((field= of++))
292     {
293       SELECT_LEX *sel= field->field->table->pos_in_table_list->select_lex;
294       if (sel->nest_level < aggr_level)
295       {
296         if (in_sum_func)
297         {
298           /*
299             Let upper function decide whether this field is a non
300             aggregated one.
301           */
302           in_sum_func->outer_fields.push_back(field, thd->mem_root);
303         }
304         else
305           sel->set_non_agg_field_used(true);
306       }
307       if (sel->nest_level > aggr_level &&
308           (sel->agg_func_used()) &&
309           !sel->group_list.elements)
310       {
311         my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
312                    ER_THD(thd, ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
313         return TRUE;
314       }
315     }
316   }
317   aggr_sel->set_agg_func_used(true);
318   if (sum_func() == SP_AGGREGATE_FUNC)
319     aggr_sel->set_custom_agg_func_used(true);
320   update_used_tables();
321   thd->lex->in_sum_func= in_sum_func;
322   return FALSE;
323 }
324 
325 /**
326   Attach a set function to the subquery where it must be aggregated.
327 
328     The function looks for an outer subquery where the set function must be
329     aggregated. If it finds such a subquery then aggr_level is set to
330     the nest level of this subquery and the item for the set function
331     is added to the list of set functions used in nested subqueries
332     inner_sum_func_list defined for each subquery. When the item is placed
333     there the field 'ref_by' is set to ref.
334 
335   @note
336     Now we 'register' only set functions that are aggregated in outer
337     subqueries. Actually it makes sense to link all set function for
338     a subquery in one chain. It would simplify the process of 'splitting'
339     for set functions.
340 
span_within(&self, span: Span) -> bool341   @param thd  reference to the thread context info
342   @param ref  location of the pointer to this item in the embedding expression
343 
344   @retval
345     FALSE  if the executes without failures (currently always)
346   @retval
347     TRUE   otherwise
348 */
349 
350 bool Item_sum::register_sum_func(THD *thd, Item **ref)
351 {
352   SELECT_LEX *sl;
353   nesting_map allow_sum_func= thd->lex->allow_sum_func;
354   for (sl= thd->lex->current_select->context.outer_select() ;
355        sl && sl->nest_level > max_arg_level;
356        sl= sl->context.outer_select())
357   {
358     if (aggr_level < 0 &&
359         (allow_sum_func.is_set(sl->nest_level)))
360     {
361       /* Found the most nested subquery where the function can be aggregated */
362       aggr_level= sl->nest_level;
363       aggr_sel= sl;
364     }
365   }
366   if (sl && (allow_sum_func.is_set(sl->nest_level)))
367   {
368     /*
369       We reached the subquery of level max_arg_level and checked
370       that the function can be aggregated here.
371       The set function will be aggregated in this subquery.
372     */
373     aggr_level= sl->nest_level;
374     aggr_sel= sl;
375 
376   }
377   if (aggr_level >= 0)
378   {
379     ref_by= ref;
380     /* Add the object to the list of registered objects assigned to aggr_sel */
381     if (!aggr_sel->inner_sum_func_list)
382       next= this;
383     else
384     {
385       next= aggr_sel->inner_sum_func_list->next;
386       aggr_sel->inner_sum_func_list->next= this;
387     }
388     aggr_sel->inner_sum_func_list= this;
389     aggr_sel->with_sum_func= 1;
390 
391     /*
392       Mark Item_subselect(s) as containing aggregate function all the way up
393       to aggregate function's calculation context.
394       Note that we must not mark the Item of calculation context itself
395       because with_sum_func on the calculation context st_select_lex is
396       already set above.
397 
398       with_sum_func being set for an Item means that this Item refers
399       (somewhere in it, e.g. one of its arguments if it's a function) directly
400       or through intermediate items to an aggregate function that is calculated
401       in a context "outside" of the Item (e.g. in the current or outer select).
402 
403       with_sum_func being set for an st_select_lex means that this st_select_lex
404       has aggregate functions directly referenced (i.e. not through a sub-select).
405     */
406     for (sl= thd->lex->current_select;
407          sl && sl != aggr_sel && sl->master_unit()->item;
408          sl= sl->master_unit()->outer_select() )
409       sl->master_unit()->item->with_sum_func= 1;
410   }
411   thd->lex->current_select->mark_as_dependent(thd, aggr_sel, NULL);
412 
413   if ((thd->lex->describe & DESCRIBE_EXTENDED) && aggr_sel)
414   {
415     push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
416                         ER_WARN_AGGFUNC_DEPENDENCE,
417                         ER_THD(thd, ER_WARN_AGGFUNC_DEPENDENCE),
418                         func_name(),
419                         thd->lex->current_select->select_number,
420                         aggr_sel->select_number);
421   }
422   return FALSE;
423 }
424 
call_site() -> Span425 
426 bool Item_sum::collect_outer_ref_processor(void *param)
427 {
428   Collect_deps_prm *prm= (Collect_deps_prm *)param;
429   SELECT_LEX *ds;
430   if ((ds= depended_from()) &&
431       ds->nest_level_base == prm->nest_level_base &&
432       ds->nest_level < prm->nest_level)
433   {
434     if (prm->collect)
435       prm->parameters->add_unique(this, &cmp_items);
436     else
437       prm->count++;
438   }
439   return FALSE;
440 }
441 
442 
443 Item_sum::Item_sum(THD *thd, List<Item> &list): Item_func_or_sum(thd, list)
444 {
445   if (!(orig_args= (Item **) thd->alloc(sizeof(Item *) * arg_count)))
located_at(&self, other: Span) -> Span446   {
447     args= NULL;
448   }
449   mark_as_sum_func();
450   init_aggregator();
source_file(&self) -> SourceFile451   list.empty();					// Fields are used
452 }
453 
454 
455 /**
456   Constructor used in processing select with temporary tebles.
457 */
458 
459 Item_sum::Item_sum(THD *thd, Item_sum *item):
460   Item_func_or_sum(thd, item),
461   aggr_sel(item->aggr_sel),
start(&self) -> LineColumn462   nest_level(item->nest_level), aggr_level(item->aggr_level),
463   quick_group(item->quick_group),
464   orig_args(NULL)
465 {
466   if (arg_count <= 2)
467   {
468     orig_args=tmp_orig_args;
469   }
470   else
471   {
472     if (!(orig_args= (Item**) thd->alloc(sizeof(Item*)*arg_count)))
473       return;
474   }
475   if (arg_count)
476     memcpy(orig_args, item->orig_args, sizeof(Item*)*arg_count);
477   init_aggregator();
478   with_distinct= item->with_distinct;
479   if (item->aggr)
480     set_aggregator(item->aggr->Aggrtype());
481 }
482 
483 
484 void Item_sum::mark_as_sum_func()
join(&self, other: Span) -> Option<Span>485 {
486   SELECT_LEX *cur_select= current_thd->lex->current_select;
487   cur_select->n_sum_items++;
488   cur_select->with_sum_func= 1;
489   const_item_cache= false;
490   with_sum_func= 1;
491   with_field= 0;
492   window_func_sum_expr_flag= false;
493 }
494 
495 
496 void Item_sum::print(String *str, enum_query_type query_type)
497 {
498   /* orig_args is not filled with valid values until fix_fields() */
499   Item **pargs= fixed ? orig_args : args;
first_byte(self) -> Self500   str->append(func_name());
501   /*
502     TODO:
503     The fact that func_name() may return a name with an extra '('
504     is really annoying. This shoud be fixed.
first_byte(self) -> Self505   */
506   if (!is_aggr_sum_func())
507     str->append('(');
508   for (uint i=0 ; i < arg_count ; i++)
509   {
510     if (i)
511       str->append(',');
512     pargs[i]->print(str, query_type);
last_byte(self) -> Self513   }
514   str->append(')');
515 }
516 
517 void Item_sum::fix_num_length_and_dec()
last_byte(self) -> Self518 {
519   decimals=0;
520   for (uint i=0 ; i < arg_count ; i++)
521     set_if_bigger(decimals,args[i]->decimals);
522   max_length=float_length(decimals);
523 }
524 
525 Item *Item_sum::get_tmp_table_item(THD *thd)
526 {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result527   Item_sum* sum_item= (Item_sum *) copy_or_same(thd);
528   if (sum_item && sum_item->result_field)	   // If not a const sum func
529   {
530     Field *result_field_tmp= sum_item->result_field;
531     for (uint i=0 ; i < sum_item->arg_count ; i++)
532     {
533       Item *arg= sum_item->args[i];
534       if (!arg->const_item())
535       {
debug_span_field_if_nontrivial(debug: &mut fmt::DebugStruct, span: Span)536 	if (arg->type() == Item::FIELD_ITEM)
537 	  ((Item_field*) arg)->field= result_field_tmp++;
538 	else
539 	  sum_item->args[i]= new (thd->mem_root) Item_temptable_field(thd, result_field_tmp++);
540       }
541     }
542   }
543   return sum_item;
544 }
545 
546 
547 void Item_sum::update_used_tables ()
548 {
549   if (!Item_sum::const_item())
550   {
551     used_tables_cache= 0;
552     for (uint i=0 ; i < arg_count ; i++)
553     {
554       args[i]->update_used_tables();
555       used_tables_cache|= args[i]->used_tables();
556     }
new(delimiter: Delimiter, stream: TokenStream) -> Group557     /*
558       MariaDB: don't run the following {
559 
560       used_tables_cache&= PSEUDO_TABLE_BITS;
561 
562       // the aggregate function is aggregated into its local context
563       used_tables_cache|= ((table_map)1 << aggr_sel->join->tables) - 1;
564 
565       } because if we do it, table elimination will assume that
566         - constructs like "COUNT(*)" use columns from all tables
567         - so, it is not possible to eliminate any table
568       our solution for COUNT(*) is that it has
569         item->used_tables() == 0 && !item->const_item()
570     */
571   }
572 }
573 
574 
575 Item *Item_sum::set_arg(uint i, THD *thd, Item *new_val)
576 {
577   thd->change_item_tree(args + i, new_val);
578   return new_val;
579 }
580 
span_close(&self) -> Span581 
582 int Item_sum::set_aggregator(Aggregator::Aggregator_type aggregator)
583 {
584   /*
585     Dependent subselects may be executed multiple times, making
586     set_aggregator to be called multiple times. The aggregator type
587     will be the same, but it needs to be reset so that it is
588     reevaluated with the new dependent data.
589     This function may also be called multiple times during query optimization.
590     In this case, the type may change, so we delete the old aggregator,
591     and create a new one.
592   */
593   if (aggr && aggregator == aggr->Aggrtype())
594   {
595     aggr->clear();
596     return FALSE;
597   }
598 
599   delete aggr;
600   switch (aggregator)
601   {
602   case Aggregator::DISTINCT_AGGREGATOR:
603     aggr= new Aggregator_distinct(this);
604     break;
605   case Aggregator::SIMPLE_AGGREGATOR:
606     aggr= new Aggregator_simple(this);
607     break;
608   };
609   return aggr ? FALSE : TRUE;
610 }
611 
612 
613 void Item_sum::cleanup()
614 {
615   if (aggr)
616   {
617     delete aggr;
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result618     aggr= NULL;
619   }
620   Item_result_field::cleanup();
621   const_item_cache= false;
622 }
623 
624 Item *Item_sum::result_item(THD *thd, Field *field)
625 {
626   return new (thd->mem_root) Item_field(thd, field);
627 }
628 
629 bool Item_sum::check_vcol_func_processor(void *arg)
630 {
631   return mark_unsupported_function(func_name(),
632                                    is_aggr_sum_func() ? ")" : "()",
633                                    arg, VCOL_IMPOSSIBLE);
634 }
_new(string: &str, raw: bool, span: Span) -> Ident635 
636 
637 /**
638   Compare keys consisting of single field that cannot be compared as binary.
639 
640   Used by the Unique class to compare keys. Will do correct comparisons
641   for all field types.
642 
643   @param    arg     Pointer to the relevant Field class instance
644   @param    key1    left key image
645   @param    key2    right key image
646   @return   comparison result
647     @retval < 0       if key1 < key2
648     @retval = 0       if key1 = key2
649     @retval > 0       if key1 > key2
650 */
651 
652 int simple_str_key_cmp(void* arg, uchar* key1, uchar* key2)
653 {
654   Field *f= (Field*) arg;
655   return f->cmp(key1, key2);
656 }
set_span(&mut self, span: Span)657 
658 
659 C_MODE_START
660 
661 int count_distinct_walk(void *elem, element_count count, void *arg)
662 {
663   (*((ulonglong*)arg))++;
664   return 0;
665 }
666 
667 C_MODE_END
668 
is_ident_continue(c: char) -> bool669 
670 /**
671   Correctly compare composite keys.
672 
673   Used by the Unique class to compare keys. Will do correct comparisons
674   for composite keys with various field types.
675 
676   @param arg     Pointer to the relevant Aggregator_distinct instance
677   @param key1    left key image
678   @param key2    right key image
679   @return        comparison result
680     @retval <0       if key1 < key2
681     @retval =0       if key1 = key2
682     @retval >0       if key1 > key2
683 */
684 
685 int Aggregator_distinct::composite_key_cmp(void* arg, uchar* key1, uchar* key2)
686 {
687   Aggregator_distinct *aggr= (Aggregator_distinct *) arg;
688   Field **field    = aggr->table->field;
689   Field **field_end= field + aggr->table->s->fields;
690   uint32 *lengths=aggr->field_lengths;
691   for (; field < field_end; ++field)
692   {
693     Field* f = *field;
694     int len = *lengths++;
695     int res = f->cmp(key1, key2);
696     if (res)
697       return res;
698     key1 += len;
699     key2 += len;
700   }
701   return 0;
702 }
703 
704 
705 /***************************************************************************/
706 
eq(&self, other: &Ident) -> bool707 C_MODE_START
708 
709 /* Declarations for auxiliary C-callbacks */
710 
711 int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
712 {
713     return memcmp(key1, key2, *(uint *) arg);
714 }
715 
eq(&self, other: &T) -> bool716 
717 static int item_sum_distinct_walk_for_count(void *element,
718                                             element_count num_of_dups,
719                                             void *item)
720 {
721   return ((Aggregator_distinct*) (item))->unique_walk_function_for_count(element);
722 }
723 
724 
725 static int item_sum_distinct_walk(void *element, element_count num_of_dups,
726                                   void *item)
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result727 {
728   return ((Aggregator_distinct*) (item))->unique_walk_function(element);
729 }
730 
731 C_MODE_END
732 
733 /***************************************************************************/
734 /**
735   Called before feeding the first row. Used to allocate/setup
736   the internal structures used for aggregation.
737 
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result738   @param thd Thread descriptor
739   @return status
740     @retval FALSE success
741     @retval TRUE  failure
742 
743     Prepares Aggregator_distinct to process the incoming stream.
744     Creates the temporary table and the Unique class if needed.
745     Called by Item_sum::aggregator_setup()
746 */
747 
748 bool Aggregator_distinct::setup(THD *thd)
749 {
750   endup_done= FALSE;
751   /*
752     Setup can be called twice for ROLLUP items. This is a bug.
753     Please add DBUG_ASSERT(tree == 0) here when it's fixed.
754   */
755   if (tree || table || tmp_table_param)
756     return FALSE;
757 
758   if (item_sum->setup(thd))
759     return TRUE;
760   if (item_sum->sum_func() == Item_sum::COUNT_FUNC ||
761       item_sum->sum_func() == Item_sum::COUNT_DISTINCT_FUNC)
762   {
763     List<Item> list;
764     SELECT_LEX *select_lex= thd->lex->current_select;
765 
766     if (!(tmp_table_param= new TMP_TABLE_PARAM))
767       return TRUE;
768 
769     /* Create a table with an unique key over all parameters */
770     for (uint i=0; i < item_sum->get_arg_count() ; i++)
771     {
772       Item *item=item_sum->get_arg(i);
773       if (list.push_back(item, thd->mem_root))
774         return TRUE;                              // End of memory
775       if (item->const_item() && item->is_null())
776         always_null= true;
777     }
778     if (always_null)
779       return FALSE;
780     count_field_types(select_lex, tmp_table_param, list, 0);
781     tmp_table_param->force_copy_fields= item_sum->has_force_copy_fields();
782     DBUG_ASSERT(table == 0);
783     /*
784       Make create_tmp_table() convert BIT columns to BIGINT.
785       This is needed because BIT fields store parts of their data in table's
786       null bits, and we don't have methods to compare two table records, which
787       is needed by Unique which is used when HEAP table is used.
788     */
789     {
790       List_iterator_fast<Item> li(list);
791       Item *item;
792       while ((item= li++))
793       {
794         if (item->type() == Item::FIELD_ITEM &&
795             ((Item_field*)item)->field->type() == FIELD_TYPE_BIT)
796           item->marker=4;
797       }
798     }
799     if (!(table= create_tmp_table(thd, tmp_table_param, list, (ORDER*) 0, 1,
800                                   0,
801                                   (select_lex->options | thd->variables.option_bits),
802                                   HA_POS_ERROR, &empty_clex_str)))
803       return TRUE;
804     table->file->extra(HA_EXTRA_NO_ROWS);		// Don't update rows
805     table->no_rows=1;
806 
807     if (table->s->db_type() == heap_hton)
808     {
809       /*
810         No blobs, otherwise it would have been MyISAM: set up a compare
811         function and its arguments to use with Unique.
812       */
813       qsort_cmp2 compare_key;
814       void* cmp_arg;
815       Field **field= table->field;
816       Field **field_end= field + table->s->fields;
817       bool all_binary= TRUE;
818 
819       for (tree_key_length= 0; field < field_end; ++field)
820       {
821         Field *f= *field;
822         enum enum_field_types type= f->type();
823         tree_key_length+= f->pack_length();
824         if ((type == MYSQL_TYPE_VARCHAR) ||
825             (!f->binary() && (type == MYSQL_TYPE_STRING ||
826                              type == MYSQL_TYPE_VAR_STRING)))
827         {
828           all_binary= FALSE;
829           break;
830         }
831       }
832       if (all_binary)
833       {
834         cmp_arg= (void*) &tree_key_length;
835         compare_key= (qsort_cmp2) simple_raw_key_cmp;
836       }
837       else
838       {
839         if (table->s->fields == 1)
840         {
841           /*
842             If we have only one field, which is the most common use of
843             count(distinct), it is much faster to use a simpler key
844             compare method that can take advantage of not having to worry
845             about other fields.
846           */
847           compare_key= (qsort_cmp2) simple_str_key_cmp;
848           cmp_arg= (void*) table->field[0];
849           /* tree_key_length has been set already */
850         }
851         else
852         {
853           uint32 *length;
854           compare_key= (qsort_cmp2) composite_key_cmp;
855           cmp_arg= (void*) this;
856           field_lengths= (uint32*) thd->alloc(table->s->fields * sizeof(uint32));
857           for (tree_key_length= 0, length= field_lengths, field= table->field;
858                field < field_end; ++field, ++length)
859           {
860             *length= (*field)->pack_length();
861             tree_key_length+= *length;
862           }
863         }
864       }
865       DBUG_ASSERT(tree == 0);
866       tree= new Unique(compare_key, cmp_arg, tree_key_length,
867                        item_sum->ram_limitation(thd));
868       /*
869         The only time tree_key_length could be 0 is if someone does
870         count(distinct) on a char(0) field - stupid thing to do,
871         but this has to be handled - otherwise someone can crash
872         the server with a DoS attack
873       */
874       if (! tree)
875         return TRUE;
876     }
877     return FALSE;
878   }
879   else
880   {
881     Item *arg;
882     DBUG_ENTER("Aggregator_distinct::setup");
883     /* It's legal to call setup() more than once when in a subquery */
884     if (tree)
885       DBUG_RETURN(FALSE);
886 
887     /*
888       Virtual table and the tree are created anew on each re-execution of
889       PS/SP. Hence all further allocations are performed in the runtime
890       mem_root.
891     */
892 
893     item_sum->null_value= item_sum->maybe_null= 1;
894     item_sum->quick_group= 0;
895 
896     DBUG_ASSERT(item_sum->get_arg(0)->fixed);
897 
898     arg= item_sum->get_arg(0);
899     if (arg->const_item())
900     {
901       (void) arg->is_null();
902       if (arg->null_value)
903         always_null= true;
904     }
905 
906     if (always_null)
907       DBUG_RETURN(FALSE);
908 
909     Field *field= arg->type_handler()->
910                     make_num_distinct_aggregator_field(thd->mem_root, arg);
911     if (!field || !(table= create_virtual_tmp_table(thd, field)))
912       DBUG_RETURN(TRUE);
913 
914     /* XXX: check that the case of CHAR(0) works OK */
915     tree_key_length= table->s->reclength - table->s->null_bytes;
916 
917     /*
918       Unique handles all unique elements in a tree until they can't fit
919       in.  Then the tree is dumped to the temporary file. We can use
920       simple_raw_key_cmp because the table contains numbers only; decimals
921       are converted to binary representation as well.
922     */
923     tree= new Unique(simple_raw_key_cmp, &tree_key_length, tree_key_length,
924                      item_sum->ram_limitation(thd));
925 
926     DBUG_RETURN(tree == 0);
927   }
928 }
929 
930 
931 /**
932   Invalidate calculated value and clear the distinct rows.
933 
934   Frees space used by the internal data structures.
935   Removes the accumulated distinct rows. Invalidates the calculated result.
936 */
937 
938 void Aggregator_distinct::clear()
939 {
940   endup_done= FALSE;
941   item_sum->clear();
942   if (tree)
943     tree->reset();
944   /* tree and table can be both null only if always_null */
945   if (item_sum->sum_func() == Item_sum::COUNT_FUNC ||
946       item_sum->sum_func() == Item_sum::COUNT_DISTINCT_FUNC)
947   {
948     if (!tree && table)
949     {
950       table->file->extra(HA_EXTRA_NO_CACHE);
951       table->file->ha_delete_all_rows();
952       table->file->extra(HA_EXTRA_WRITE_CACHE);
953     }
954   }
955   else
956   {
957     item_sum->null_value= 1;
958   }
959 }
960 
961 
962 /**
963   Process incoming row.
964 
965   Add it to Unique/temp hash table if it's unique. Skip the row if
966   not unique.
967   Prepare Aggregator_distinct to process the incoming stream.
968   Create the temporary table and the Unique class if needed.
969   Called by Item_sum::aggregator_add().
970   To actually get the result value in item_sum's buffers
971   Aggregator_distinct::endup() must be called.
972 
973   @return status
974     @retval FALSE     success
975     @retval TRUE      failure
976 */
977 
978 bool Aggregator_distinct::add()
979 {
980   if (always_null)
981     return 0;
982 
983   if (item_sum->sum_func() == Item_sum::COUNT_FUNC ||
984       item_sum->sum_func() == Item_sum::COUNT_DISTINCT_FUNC)
985   {
986     int error;
987     copy_fields(tmp_table_param);
988     if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
989       return TRUE;
990 
991     for (Field **field=table->field ; *field ; field++)
992       if ((*field)->is_real_null(0))
993         return 0;					// Don't count NULL
994 
995     if (tree)
996     {
997       /*
998         The first few bytes of record (at least one) are just markers
999         for deleted and NULLs. We want to skip them since they will
1000         bloat the tree without providing any valuable info. Besides,
1001         key_length used to initialize the tree didn't include space for them.
1002       */
1003       return tree->unique_add(table->record[0] + table->s->null_bytes);
1004     }
1005     if (unlikely((error= table->file->ha_write_tmp_row(table->record[0]))) &&
1006         table->file->is_fatal_error(error, HA_CHECK_DUP))
1007       return TRUE;
1008     return FALSE;
1009   }
1010   else
1011   {
1012     item_sum->get_arg(0)->save_in_field(table->field[0], FALSE);
1013     if (table->field[0]->is_null())
1014       return 0;
1015     DBUG_ASSERT(tree);
1016     item_sum->null_value= 0;
1017     /*
1018       '0' values are also stored in the tree. This doesn't matter
1019       for SUM(DISTINCT), but is important for AVG(DISTINCT)
1020     */
1021     return tree->unique_add(table->field[0]->ptr);
1022   }
1023 }
1024 
1025 
1026 /**
1027   Calculate the aggregate function value.
1028 
1029   Since Distinct_aggregator::add() just collects the distinct rows,
1030   we must go over the distinct rows and feed them to the aggregation
1031   function before returning its value.
1032   This is what endup () does. It also sets the result validity flag
1033   endup_done to TRUE so it will not recalculate the aggregate value
1034   again if the Item_sum hasn't been reset.
1035 */
1036 
1037 void Aggregator_distinct::endup()
1038 {
1039   /* prevent consecutive recalculations */
1040   if (endup_done)
1041     return;
1042 
1043   /* we are going to calculate the aggregate value afresh */
1044   item_sum->clear();
1045 
1046   /* The result will definitely be null : no more calculations needed */
1047   if (always_null)
1048     return;
1049 
1050   if (item_sum->sum_func() == Item_sum::COUNT_FUNC ||
1051       item_sum->sum_func() == Item_sum::COUNT_DISTINCT_FUNC)
1052   {
1053     DBUG_ASSERT(item_sum->fixed == 1);
1054     Item_sum_count *sum= (Item_sum_count *)item_sum;
1055     if (tree && tree->elements == 0)
1056     {
1057       /* everything fits in memory */
1058       sum->count= (longlong) tree->elements_in_tree();
1059       endup_done= TRUE;
1060     }
1061     if (!tree)
1062     {
1063       /* there were blobs */
1064       table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
1065       sum->count= table->file->stats.records;
1066       endup_done= TRUE;
1067     }
1068   }
1069 
1070  /*
1071    We don't have a tree only if 'setup()' hasn't been called;
1072    this is the case of sql_executor.cc:return_zero_rows.
1073  */
1074   if (tree && !endup_done)
1075   {
1076    /*
1077      All tree's values are not NULL.
1078      Note that value of field is changed as we walk the tree, in
1079      Aggregator_distinct::unique_walk_function, but it's always not NULL.
1080    */
1081    table->field[0]->set_notnull();
1082     /* go over the tree of distinct keys and calculate the aggregate value */
1083     use_distinct_values= TRUE;
1084     tree_walk_action func;
1085     if (item_sum->sum_func() == Item_sum::COUNT_DISTINCT_FUNC)
1086       func= item_sum_distinct_walk_for_count;
1087     else
1088       func= item_sum_distinct_walk;
1089     tree->walk(table, func, (void*) this);
1090     use_distinct_values= FALSE;
1091   }
1092   /* prevent consecutive recalculations */
1093   endup_done= TRUE;
1094 }
1095 
1096 
1097 String *
1098 Item_sum_int::val_str(String *str)
1099 {
1100   return val_string_from_int(str);
1101 }
1102 
1103 
1104 my_decimal *Item_sum_int::val_decimal(my_decimal *decimal_value)
1105 {
1106   return val_decimal_from_int(decimal_value);
1107 }
1108 
1109 
1110 bool
1111 Item_sum_num::fix_fields(THD *thd, Item **ref)
1112 {
1113   DBUG_ASSERT(fixed == 0);
1114 
1115   if (init_sum_func_check(thd))
1116     return TRUE;
1117 
1118   decimals=0;
1119   maybe_null= sum_func() != COUNT_FUNC;
1120   for (uint i=0 ; i < arg_count ; i++)
1121   {
1122     if (args[i]->fix_fields_if_needed_for_scalar(thd, &args[i]))
1123       return TRUE;
1124     set_if_bigger(decimals, args[i]->decimals);
1125     m_with_subquery|= args[i]->with_subquery();
1126     with_param|= args[i]->with_param;
1127     with_window_func|= args[i]->with_window_func;
1128   }
1129   result_field=0;
1130   max_length=float_length(decimals);
1131   null_value=1;
1132   if (fix_length_and_dec() ||
1133       check_sum_func(thd, ref))
1134     return TRUE;
1135 
1136   if (arg_count)
1137     memcpy (orig_args, args, sizeof (Item *) * arg_count);
1138   fixed= 1;
1139   return FALSE;
1140 }
1141 
1142 
1143 bool
1144 Item_sum_min_max::fix_fields(THD *thd, Item **ref)
1145 {
1146   DBUG_ENTER("Item_sum_min_max::fix_fields");
1147   DBUG_ASSERT(fixed == 0);
1148 
1149   if (init_sum_func_check(thd))
1150     DBUG_RETURN(TRUE);
1151 
1152   // 'item' can be changed during fix_fields
1153   if (args[0]->fix_fields_if_needed_for_scalar(thd, &args[0]))
1154     DBUG_RETURN(TRUE);
1155 
1156   m_with_subquery= args[0]->with_subquery();
1157   with_param= args[0]->with_param;
1158   with_window_func|= args[0]->with_window_func;
1159 
1160   if (fix_length_and_dec())
1161     DBUG_RETURN(TRUE);
1162 
1163   if (!is_window_func_sum_expr())
1164     setup_hybrid(thd, args[0], NULL);
1165   result_field=0;
1166 
1167   if (check_sum_func(thd, ref))
1168     DBUG_RETURN(TRUE);
1169 
1170   orig_args[0]= args[0];
1171   fixed= 1;
1172   DBUG_RETURN(FALSE);
1173 }
1174 
1175 
1176 bool Item_sum_hybrid::fix_length_and_dec_generic()
1177 {
1178   Item *item= arguments()[0];
1179   Type_std_attributes::set(item);
1180   set_handler(item->type_handler());
1181   return false;
1182 }
1183 
1184 
1185 /**
1186   MAX/MIN for the traditional numeric types preserve the exact data type
1187   from Fields, but do not preserve the exact type from Items:
1188     MAX(float_field)              -> FLOAT
1189     MAX(smallint_field)           -> LONGLONG
1190     MAX(COALESCE(float_field))    -> DOUBLE
1191     MAX(COALESCE(smallint_field)) -> LONGLONG
1192   QQ: Items should probably be fixed to preserve the exact type.
1193 */
1194 bool Item_sum_hybrid::fix_length_and_dec_numeric(const Type_handler *handler)
1195 {
1196   Item *item= arguments()[0];
1197   Item *item2= item->real_item();
1198   Type_std_attributes::set(item);
1199   if (item2->type() == Item::FIELD_ITEM)
1200     set_handler(item2->type_handler());
1201   else
1202     set_handler(handler);
1203   return false;
1204 }
1205 
1206 
1207 /**
1208    MAX(str_field) converts ENUM/SET to CHAR, and preserve all other types
1209    for Fields.
1210    QQ: This works differently from UNION, which preserve the exact data
1211    type for ENUM/SET if the joined ENUM/SET fields are equally defined.
1212    Perhaps should be fixed.
1213    MAX(str_item) chooses the best suitable string type.
1214 */
1215 bool Item_sum_hybrid::fix_length_and_dec_string()
1216 {
1217   Item *item= arguments()[0];
1218   Item *item2= item->real_item();
1219   Type_std_attributes::set(item);
1220   if (item2->type() == Item::FIELD_ITEM)
1221   {
1222     // Fields: convert ENUM/SET to CHAR, preserve the type otherwise.
1223     set_handler(item->type_handler());
1224   }
1225   else
1226   {
1227     // Items: choose VARCHAR/BLOB/MEDIUMBLOB/LONGBLOB, depending on length.
1228     set_handler(type_handler_varchar.
1229           type_handler_adjusted_to_max_octet_length(max_length,
1230                                                     collation.collation));
1231   }
1232   return false;
1233 }
1234 
1235 
1236 bool Item_sum_min_max::fix_length_and_dec()
1237 {
1238   DBUG_ASSERT(args[0]->field_type() == args[0]->real_item()->field_type());
1239   DBUG_ASSERT(args[0]->result_type() == args[0]->real_item()->result_type());
1240   /* MIN/MAX can return NULL for empty set indepedent of the used column */
1241   maybe_null= null_value= true;
1242   return args[0]->type_handler()->Item_sum_hybrid_fix_length_and_dec(this);
1243 }
1244 
1245 
1246 /**
1247   MIN/MAX function setup.
1248 
1249   @param item       argument of MIN/MAX function
1250   @param value_arg  calculated value of MIN/MAX function
1251 
1252   @details
1253     Setup cache/comparator of MIN/MAX functions. When called by the
1254     copy_or_same function value_arg parameter contains calculated value
1255     of the original MIN/MAX object and it is saved in this object's cache.
1256 
1257     We mark the value and arg_cache with 'RAND_TABLE_BIT' to ensure
1258     that Arg_comparator::compare_datetime() doesn't allocate new
1259     item inside of Arg_comparator.  This would cause compare_datetime()
1260     and Item_sum_min::add() to use different values!
1261 */
1262 
1263 void Item_sum_min_max::setup_hybrid(THD *thd, Item *item, Item *value_arg)
1264 {
1265   DBUG_ENTER("Item_sum_min_max::setup_hybrid");
1266   if (!(value= item->get_cache(thd)))
1267     DBUG_VOID_RETURN;
1268   value->setup(thd, item);
1269   value->store(value_arg);
1270   /* Don't cache value, as it will change */
1271   if (!item->const_item())
1272     value->set_used_tables(RAND_TABLE_BIT);
1273   if (!(arg_cache= item->get_cache(thd)))
1274     DBUG_VOID_RETURN;
1275   arg_cache->setup(thd, item);
1276   /* Don't cache value, as it will change */
1277   if (!item->const_item())
1278     arg_cache->set_used_tables(RAND_TABLE_BIT);
1279   cmp= new Arg_comparator();
1280   if (cmp)
1281     cmp->set_cmp_func(this, (Item**)&arg_cache, (Item**)&value, FALSE);
1282   DBUG_VOID_RETURN;
1283 }
1284 
1285 
1286 Field *Item_sum_min_max::create_tmp_field(bool group, TABLE *table)
1287 {
1288   DBUG_ENTER("Item_sum_min_max::create_tmp_field");
1289 
1290   if (args[0]->type() == Item::FIELD_ITEM)
1291   {
1292     Field *field= ((Item_field*) args[0])->field;
1293     if ((field= create_tmp_field_from_field(table->in_use, field, &name,
1294                                             table, NULL)))
1295       field->flags&= ~NOT_NULL_FLAG;
1296     DBUG_RETURN(field);
1297   }
1298   DBUG_RETURN(tmp_table_field_from_field_type(table));
1299 }
1300 
1301 /***********************************************************************
1302 ** Item_sum_sp class
1303 ***********************************************************************/
1304 
1305 Item_sum_sp::Item_sum_sp(THD *thd, Name_resolution_context *context_arg,
1306                            sp_name *name_arg, sp_head *sp, List<Item> &list)
1307   :Item_sum(thd, list), Item_sp(thd, context_arg, name_arg)
1308 {
1309   maybe_null= 1;
1310   quick_group= 0;
1311   m_sp= sp;
1312 }
1313 
1314 Item_sum_sp::Item_sum_sp(THD *thd, Name_resolution_context *context_arg,
1315                            sp_name *name_arg, sp_head *sp)
1316   :Item_sum(thd), Item_sp(thd, context_arg, name_arg)
1317 {
1318   maybe_null= 1;
1319   quick_group= 0;
1320   m_sp= sp;
1321 }
1322 
1323 Item_sum_sp::Item_sum_sp(THD *thd, Item_sum_sp *item):
1324              Item_sum(thd, item), Item_sp(thd, item)
1325 {
1326   maybe_null= item->maybe_null;
1327   quick_group= item->quick_group;
1328 }
1329 
1330 bool
1331 Item_sum_sp::fix_fields(THD *thd, Item **ref)
1332 {
1333   DBUG_ASSERT(fixed == 0);
1334   if (init_sum_func_check(thd))
1335     return TRUE;
1336   decimals= 0;
1337 
1338   m_sp= m_sp ? m_sp : sp_handler_function.sp_find_routine(thd, m_name, true);
1339 
1340   if (!m_sp)
1341   {
1342     my_missing_function_error(m_name->m_name, ErrConvDQName(m_name).ptr());
1343     context->process_error(thd);
1344     return TRUE;
1345   }
1346 
1347   if (init_result_field(thd, max_length, maybe_null, &null_value, &name))
1348     return TRUE;
1349 
1350   for (uint i= 0 ; i < arg_count ; i++)
1351   {
1352     if (args[i]->fix_fields_if_needed_for_scalar(thd, &args[i]))
1353       return TRUE;
1354     set_if_bigger(decimals, args[i]->decimals);
1355     m_with_subquery|= args[i]->with_subquery();
1356     with_window_func|= args[i]->with_window_func;
1357   }
1358   result_field= NULL;
1359   max_length= float_length(decimals);
1360   null_value= 1;
1361   if (fix_length_and_dec())
1362     return TRUE;
1363 
1364   if (check_sum_func(thd, ref))
1365     return TRUE;
1366 
1367   if (arg_count)
1368     memcpy(orig_args, args, sizeof(Item *) * arg_count);
1369   fixed= 1;
1370   return FALSE;
1371 }
1372 
1373 /**
1374   Execute function to store value in result field.
1375   This is called when we need the value to be returned for the function.
1376   Here we send a signal in form of the server status that all rows have been
1377   fetched and now we have to exit from the function with the return value.
1378   @return Function returns error status.
1379   @retval FALSE on success.
1380   @retval TRUE if an error occurred.
1381 */
1382 
1383 bool
1384 Item_sum_sp::execute()
1385 {
1386   THD *thd= current_thd;
1387   bool res;
1388   uint old_server_status= thd->server_status;
1389 
1390   /*
1391     We set server status so we can send a signal to exit from the
1392     function with the return value.
1393   */
1394 
1395   thd->server_status|= SERVER_STATUS_LAST_ROW_SENT;
1396   res= Item_sp::execute(thd, &null_value, args, arg_count);
1397   thd->server_status= old_server_status;
1398   return res;
1399 }
1400 
1401 /**
1402   Handles the aggregation of the values.
1403   @note: See class description for more details on how and why this is done.
1404   @return The error state.
1405   @retval FALSE on success.
1406   @retval TRUE if an error occurred.
1407 */
1408 
1409 bool
1410 Item_sum_sp::add()
1411 {
1412   return execute_impl(current_thd, args, arg_count);
1413 }
1414 
1415 
1416 void
1417 Item_sum_sp::clear()
1418 {
1419   delete func_ctx;
1420   func_ctx= NULL;
1421   sp_query_arena->free_items();
1422   free_root(&sp_mem_root, MYF(0));
1423 }
1424 
1425 const Type_handler *Item_sum_sp::type_handler() const
1426 {
1427   DBUG_ENTER("Item_sum_sp::type_handler");
1428   DBUG_PRINT("info", ("m_sp = %p", (void *) m_sp));
1429   DBUG_ASSERT(sp_result_field);
1430   // This converts ENUM/SET to STRING
1431   const Type_handler *handler= sp_result_field->type_handler();
1432   DBUG_RETURN(handler->type_handler_for_item_field());
1433 }
1434 
1435 void
1436 Item_sum_sp::cleanup()
1437 {
1438   Item_sp::cleanup();
1439   Item_sum::cleanup();
1440 }
1441 
1442 /**
1443   Initialize local members with values from the Field interface.
1444   @note called from Item::fix_fields.
1445 */
1446 
1447 bool
1448 Item_sum_sp::fix_length_and_dec()
1449 {
1450   DBUG_ENTER("Item_sum_sp::fix_length_and_dec");
1451   DBUG_ASSERT(sp_result_field);
1452   Type_std_attributes::set(sp_result_field->type_std_attributes());
1453   bool res= Item_sum::fix_length_and_dec();
1454   DBUG_RETURN(res);
1455 }
1456 
1457 const char *
1458 Item_sum_sp::func_name() const
1459 {
1460   THD *thd= current_thd;
1461   return Item_sp::func_name(thd);
1462 }
1463 
1464 Item* Item_sum_sp::copy_or_same(THD *thd)
1465 {
1466   Item_sum_sp *copy_item= new (thd->mem_root) Item_sum_sp(thd, this);
1467   copy_item->init_result_field(thd, max_length, maybe_null,
1468                                &copy_item->null_value, &copy_item->name);
1469   return copy_item;
1470 }
1471 
1472 /***********************************************************************
1473 ** reset and add of sum_func
1474 ***********************************************************************/
1475 
1476 /**
1477   @todo
1478   check if the following assignments are really needed
1479 */
1480 Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item)
1481   :Item_sum_num(thd, item),
1482    Type_handler_hybrid_field_type(item),
1483    direct_added(FALSE), direct_reseted_field(FALSE),
1484    curr_dec_buff(item->curr_dec_buff),
1485    count(item->count)
1486 {
1487   /* TODO: check if the following assignments are really needed */
1488   if (result_type() == DECIMAL_RESULT)
1489   {
1490     my_decimal2decimal(item->dec_buffs, dec_buffs);
1491     my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
1492   }
1493   else
1494     sum= item->sum;
1495 }
1496 
1497 Item *Item_sum_sum::copy_or_same(THD* thd)
1498 {
1499   return new (thd->mem_root) Item_sum_sum(thd, this);
1500 }
1501 
1502 
1503 void Item_sum_sum::cleanup()
1504 {
1505   DBUG_ENTER("Item_sum_sum::cleanup");
1506   direct_added= direct_reseted_field= FALSE;
1507   Item_sum_num::cleanup();
1508   DBUG_VOID_RETURN;
1509 }
1510 
1511 
1512 void Item_sum_sum::clear()
1513 {
1514   DBUG_ENTER("Item_sum_sum::clear");
1515   null_value=1;
1516   count= 0;
1517   if (result_type() == DECIMAL_RESULT)
1518   {
1519     curr_dec_buff= 0;
1520     my_decimal_set_zero(dec_buffs);
1521   }
1522   else
1523     sum= 0.0;
1524   DBUG_VOID_RETURN;
1525 }
1526 
1527 
1528 void Item_sum_sum::fix_length_and_dec_double()
1529 {
1530   set_handler(&type_handler_double); // Change FLOAT to DOUBLE
1531   decimals= args[0]->decimals;
1532   sum= 0.0;
1533 }
1534 
1535 
1536 void Item_sum_sum::fix_length_and_dec_decimal()
1537 {
1538   set_handler(&type_handler_newdecimal); // Change temporal to new DECIMAL
1539   decimals= args[0]->decimals;
1540   /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
1541   int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
1542   max_length= my_decimal_precision_to_length_no_truncation(precision,
1543                                                            decimals,
1544                                                            unsigned_flag);
1545   curr_dec_buff= 0;
1546   my_decimal_set_zero(dec_buffs);
1547 }
1548 
1549 
1550 bool Item_sum_sum::fix_length_and_dec()
1551 {
1552   DBUG_ENTER("Item_sum_sum::fix_length_and_dec");
1553   maybe_null=null_value=1;
1554   if (args[0]->cast_to_int_type_handler()->
1555       Item_sum_sum_fix_length_and_dec(this))
1556     DBUG_RETURN(TRUE);
1557   DBUG_PRINT("info", ("Type: %s (%d, %d)", type_handler()->name().ptr(),
1558                       max_length, (int) decimals));
1559   DBUG_RETURN(FALSE);
1560 }
1561 
1562 
1563 void Item_sum_sum::direct_add(my_decimal *add_sum_decimal)
1564 {
1565   DBUG_ENTER("Item_sum_sum::direct_add");
1566   DBUG_PRINT("info", ("add_sum_decimal: %p", add_sum_decimal));
1567   direct_added= TRUE;
1568   direct_reseted_field= FALSE;
1569   if (add_sum_decimal)
1570   {
1571     direct_sum_is_null= FALSE;
1572     direct_sum_decimal= *add_sum_decimal;
1573   }
1574   else
1575   {
1576     direct_sum_is_null= TRUE;
1577     direct_sum_decimal= decimal_zero;
1578   }
1579   DBUG_VOID_RETURN;
1580 }
1581 
1582 
1583 void Item_sum_sum::direct_add(double add_sum_real, bool add_sum_is_null)
1584 {
1585   DBUG_ENTER("Item_sum_sum::direct_add");
1586   DBUG_PRINT("info", ("add_sum_real: %f", add_sum_real));
1587   direct_added= TRUE;
1588   direct_reseted_field= FALSE;
1589   direct_sum_is_null= add_sum_is_null;
1590   direct_sum_real= add_sum_real;
1591   DBUG_VOID_RETURN;
1592 }
1593 
1594 
1595 bool Item_sum_sum::add()
1596 {
1597   DBUG_ENTER("Item_sum_sum::add");
1598   add_helper(false);
1599   DBUG_RETURN(0);
1600 }
1601 
1602 void Item_sum_sum::add_helper(bool perform_removal)
1603 {
1604   DBUG_ENTER("Item_sum_sum::add_helper");
1605 
1606   if (result_type() == DECIMAL_RESULT)
1607   {
1608     if (unlikely(direct_added))
1609     {
1610       /* Add value stored by Item_sum_sum::direct_add */
1611       DBUG_ASSERT(!perform_removal);
1612 
1613       direct_added= FALSE;
1614       if (likely(!direct_sum_is_null))
1615       {
1616         my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
1617                        &direct_sum_decimal, dec_buffs + curr_dec_buff);
1618         curr_dec_buff^= 1;
1619         null_value= 0;
1620       }
1621     }
1622     else
1623     {
1624       direct_reseted_field= FALSE;
1625       my_decimal value;
1626       const my_decimal *val= aggr->arg_val_decimal(&value);
1627       if (!aggr->arg_is_null(true))
1628       {
1629         if (perform_removal)
1630         {
1631           if (count > 0)
1632           {
1633             my_decimal_sub(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff ^ 1),
1634               dec_buffs + curr_dec_buff, val);
1635             count--;
1636           }
1637           else
1638             DBUG_VOID_RETURN;
1639         }
1640         else
1641         {
1642           count++;
1643           my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff ^ 1),
1644             val, dec_buffs + curr_dec_buff);
1645         }
1646         curr_dec_buff^= 1;
1647         null_value= (count > 0) ? 0 : 1;
1648       }
1649     }
1650   }
1651   else
1652   {
1653     if (unlikely(direct_added))
1654     {
1655       /* Add value stored by Item_sum_sum::direct_add */
1656       DBUG_ASSERT(!perform_removal);
1657 
1658       direct_added= FALSE;
1659       if (!direct_sum_is_null)
1660       {
1661         sum+= direct_sum_real;
1662         null_value= 0;
1663       }
1664     }
1665     else
1666     {
1667       direct_reseted_field= FALSE;
1668       if (perform_removal && count > 0)
1669         sum-= aggr->arg_val_real();
1670       else
1671         sum+= aggr->arg_val_real();
1672       if (!aggr->arg_is_null(true))
1673       {
1674         if (perform_removal)
1675         {
1676           if (count > 0)
1677           {
1678             count--;
1679           }
1680         }
1681         else
1682           count++;
1683 
1684         null_value= (count > 0) ? 0 : 1;
1685       }
1686     }
1687   }
1688   DBUG_VOID_RETURN;
1689 }
1690 
1691 
1692 longlong Item_sum_sum::val_int()
1693 {
1694   DBUG_ASSERT(fixed == 1);
1695   if (aggr)
1696     aggr->endup();
1697   if (result_type() == DECIMAL_RESULT)
1698   {
1699     longlong result;
1700     my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
1701                    &result);
1702     return result;
1703   }
1704   return val_int_from_real();
1705 }
1706 
1707 
1708 double Item_sum_sum::val_real()
1709 {
1710   DBUG_ASSERT(fixed == 1);
1711   if (aggr)
1712     aggr->endup();
1713   if (result_type() == DECIMAL_RESULT)
1714     my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
1715   return sum;
1716 }
1717 
1718 
1719 String *Item_sum_sum::val_str(String *str)
1720 {
1721   if (aggr)
1722     aggr->endup();
1723   if (result_type() == DECIMAL_RESULT)
1724     return val_string_from_decimal(str);
1725   return val_string_from_real(str);
1726 }
1727 
1728 
1729 my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
1730 {
1731   if (aggr)
1732     aggr->endup();
1733   if (result_type() == DECIMAL_RESULT)
1734     return null_value ? NULL : (dec_buffs + curr_dec_buff);
1735   return val_decimal_from_real(val);
1736 }
1737 
1738 void Item_sum_sum::remove()
1739 {
1740   DBUG_ENTER("Item_sum_sum::remove");
1741   add_helper(true);
1742   DBUG_VOID_RETURN;
1743 }
1744 
1745 /**
1746   Aggregate a distinct row from the distinct hash table.
1747 
1748   Called for each row into the hash table 'Aggregator_distinct::table'.
1749   Includes the current distinct row into the calculation of the
1750   aggregate value. Uses the Field classes to get the value from the row.
1751   This function is used for AVG/SUM(DISTINCT). For COUNT(DISTINCT)
1752   it's called only when there are no blob arguments and the data don't
1753   fit into memory (so Unique makes persisted trees on disk).
1754 
1755   @param element     pointer to the row data.
1756 
1757   @return status
1758     @retval FALSE     success
1759     @retval TRUE      failure
1760 */
1761 
1762 bool Aggregator_distinct::unique_walk_function(void *element)
1763 {
1764   memcpy(table->field[0]->ptr, element, tree_key_length);
1765   item_sum->add();
1766   return 0;
1767 }
1768 
1769 
1770 /*
1771   A variant of unique_walk_function() that is to be used with Item_sum_count.
1772 
1773   COUNT is a special aggregate function: it doesn't need the values, it only
1774   needs to count them. COUNT needs to know the values are not NULLs, but NULL
1775   values are not put into the Unique, so we don't need to check for NULLs here.
1776 */
1777 
1778 bool Aggregator_distinct::unique_walk_function_for_count(void *element)
1779 {
1780   Item_sum_count *sum= (Item_sum_count *)item_sum;
1781   sum->count++;
1782   return 0;
1783 }
1784 
1785 
1786 Aggregator_distinct::~Aggregator_distinct()
1787 {
1788   if (tree)
1789   {
1790     delete tree;
1791     tree= NULL;
1792   }
1793   if (table)
1794   {
1795     free_tmp_table(table->in_use, table);
1796     table=NULL;
1797   }
1798   if (tmp_table_param)
1799   {
1800     delete tmp_table_param;
1801     tmp_table_param= NULL;
1802   }
1803 }
1804 
1805 
1806 my_decimal *Aggregator_simple::arg_val_decimal(my_decimal *value)
1807 {
1808   return item_sum->args[0]->val_decimal(value);
1809 }
1810 
1811 
1812 double Aggregator_simple::arg_val_real()
1813 {
1814   return item_sum->args[0]->val_real();
1815 }
1816 
1817 
1818 bool Aggregator_simple::arg_is_null(bool use_null_value)
1819 {
1820   Item **item= item_sum->args;
1821   const uint item_count= item_sum->arg_count;
1822   if (use_null_value)
1823   {
1824     for (uint i= 0; i < item_count; i++)
1825     {
1826       if (item[i]->null_value)
1827         return true;
1828     }
1829   }
1830   else
1831   {
1832     for (uint i= 0; i < item_count; i++)
1833     {
1834       if (item[i]->maybe_null && item[i]->is_null())
1835         return true;
1836     }
1837   }
1838   return false;
1839 }
1840 
1841 
1842 my_decimal *Aggregator_distinct::arg_val_decimal(my_decimal * value)
1843 {
1844   return use_distinct_values ? table->field[0]->val_decimal(value) :
1845     item_sum->args[0]->val_decimal(value);
1846 }
1847 
1848 
1849 double Aggregator_distinct::arg_val_real()
1850 {
1851   return use_distinct_values ? table->field[0]->val_real() :
1852     item_sum->args[0]->val_real();
1853 }
1854 
1855 
1856 bool Aggregator_distinct::arg_is_null(bool use_null_value)
1857 {
1858   if (use_distinct_values)
1859   {
1860     const bool rc= table->field[0]->is_null();
1861     DBUG_ASSERT(!rc); // NULLs are never stored in 'tree'
1862     return rc;
1863   }
1864   return use_null_value ?
1865     item_sum->args[0]->null_value :
1866     (item_sum->args[0]->maybe_null && item_sum->args[0]->is_null());
1867 }
1868 
1869 
1870 Item *Item_sum_count::copy_or_same(THD* thd)
1871 {
1872   DBUG_ENTER("Item_sum_count::copy_or_same");
1873   DBUG_RETURN(new (thd->mem_root) Item_sum_count(thd, this));
1874 }
1875 
1876 
1877 void Item_sum_count::direct_add(longlong add_count)
1878 {
1879   DBUG_ENTER("Item_sum_count::direct_add");
1880   DBUG_PRINT("info", ("add_count: %lld", add_count));
1881   direct_counted= TRUE;
1882   direct_reseted_field= FALSE;
1883   direct_count= add_count;
1884   DBUG_VOID_RETURN;
1885 }
1886 
1887 
1888 void Item_sum_count::clear()
1889 {
1890   DBUG_ENTER("Item_sum_count::clear");
1891   count= 0;
1892   DBUG_VOID_RETURN;
1893 }
1894 
1895 
1896 bool Item_sum_count::add()
1897 {
1898   DBUG_ENTER("Item_sum_count::add");
1899   if (direct_counted)
1900   {
1901     direct_counted= FALSE;
1902     count+= direct_count;
1903   }
1904   else
1905   {
1906     direct_reseted_field= FALSE;
1907     if (aggr->arg_is_null(false))
1908       DBUG_RETURN(0);
1909     count++;
1910   }
1911   DBUG_RETURN(0);
1912 }
1913 
1914 
1915 /*
1916   Remove a row. This is used by window functions.
1917 */
1918 
1919 void Item_sum_count::remove()
1920 {
1921   DBUG_ASSERT(aggr->Aggrtype() == Aggregator::SIMPLE_AGGREGATOR);
1922   if (aggr->arg_is_null(false))
1923     return;
1924   if (count > 0)
1925     count--;
1926 }
1927 
1928 longlong Item_sum_count::val_int()
1929 {
1930   DBUG_ENTER("Item_sum_count::val_int");
1931   DBUG_ASSERT(fixed == 1);
1932   if (aggr)
1933     aggr->endup();
1934   DBUG_RETURN((longlong)count);
1935 }
1936 
1937 
1938 void Item_sum_count::cleanup()
1939 {
1940   DBUG_ENTER("Item_sum_count::cleanup");
1941   count= 0;
1942   direct_counted= FALSE;
1943   direct_reseted_field= FALSE;
1944   Item_sum_int::cleanup();
1945   DBUG_VOID_RETURN;
1946 }
1947 
1948 
1949 /*
1950   Average
1951 */
1952 
1953 void Item_sum_avg::fix_length_and_dec_decimal()
1954 {
1955   Item_sum_sum::fix_length_and_dec_decimal();
1956   int precision= args[0]->decimal_precision() + prec_increment;
1957   decimals= MY_MIN(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1958   max_length= my_decimal_precision_to_length_no_truncation(precision,
1959                                                            decimals,
1960                                                            unsigned_flag);
1961   f_precision= MY_MIN(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1962   f_scale=  args[0]->decimals;
1963   dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1964 }
1965 
1966 
1967 void Item_sum_avg::fix_length_and_dec_double()
1968 {
1969   Item_sum_sum::fix_length_and_dec_double();
1970   decimals= MY_MIN(args[0]->decimals + prec_increment,
1971                    FLOATING_POINT_DECIMALS);
1972   max_length= MY_MIN(args[0]->max_length + prec_increment, float_length(decimals));
1973 }
1974 
1975 
1976 bool Item_sum_avg::fix_length_and_dec()
1977 {
1978   DBUG_ENTER("Item_sum_avg::fix_length_and_dec");
1979   prec_increment= current_thd->variables.div_precincrement;
1980   maybe_null=null_value=1;
1981   if (args[0]->cast_to_int_type_handler()->
1982       Item_sum_avg_fix_length_and_dec(this))
1983     DBUG_RETURN(TRUE);
1984   DBUG_PRINT("info", ("Type: %s (%d, %d)", type_handler()->name().ptr(),
1985                       max_length, (int) decimals));
1986   DBUG_RETURN(FALSE);
1987 }
1988 
1989 
1990 Item *Item_sum_avg::copy_or_same(THD* thd)
1991 {
1992   return new (thd->mem_root) Item_sum_avg(thd, this);
1993 }
1994 
1995 
1996 Field *Item_sum_avg::create_tmp_field(bool group, TABLE *table)
1997 {
1998 
1999   if (group)
2000   {
2001     /*
2002       We must store both value and counter in the temporary table in one field.
2003       The easiest way is to do this is to store both value in a string
2004       and unpack on access.
2005     */
2006     Field *field= new (table->in_use->mem_root)
2007       Field_string(((result_type() == DECIMAL_RESULT) ?
2008                    dec_bin_size : sizeof(double)) + sizeof(longlong),
2009                    0, &name, &my_charset_bin);
2010     if (field)
2011       field->init(table);
2012     return field;
2013   }
2014   return tmp_table_field_from_field_type(table);
2015 }
2016 
2017 
2018 void Item_sum_avg::clear()
2019 {
2020   Item_sum_sum::clear();
2021   count=0;
2022 }
2023 
2024 
2025 bool Item_sum_avg::add()
2026 {
2027   if (Item_sum_sum::add())
2028     return TRUE;
2029   if (!aggr->arg_is_null(true))
2030     count++;
2031   return FALSE;
2032 }
2033 
2034 void Item_sum_avg::remove()
2035 {
2036   Item_sum_sum::remove();
2037   if (!aggr->arg_is_null(true))
2038   {
2039     if (count > 0)
2040       count--;
2041   }
2042 }
2043 
2044 double Item_sum_avg::val_real()
2045 {
2046   DBUG_ASSERT(fixed == 1);
2047   if (aggr)
2048     aggr->endup();
2049   if (!count)
2050   {
2051     null_value=1;
2052     return 0.0;
2053   }
2054   return Item_sum_sum::val_real() / ulonglong2double(count);
2055 }
2056 
2057 
2058 my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
2059 {
2060   my_decimal cnt;
2061   const my_decimal *sum_dec;
2062   DBUG_ASSERT(fixed == 1);
2063   if (aggr)
2064     aggr->endup();
2065   if (!count)
2066   {
2067     null_value=1;
2068     return NULL;
2069   }
2070 
2071   /*
2072     For non-DECIMAL result_type() the division will be done in
2073     Item_sum_avg::val_real().
2074   */
2075   if (result_type() != DECIMAL_RESULT)
2076     return val_decimal_from_real(val);
2077 
2078   sum_dec= dec_buffs + curr_dec_buff;
2079   int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
2080   my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
2081   return val;
2082 }
2083 
2084 
2085 String *Item_sum_avg::val_str(String *str)
2086 {
2087   if (aggr)
2088     aggr->endup();
2089   if (result_type() == DECIMAL_RESULT)
2090     return val_string_from_decimal(str);
2091   return val_string_from_real(str);
2092 }
2093 
2094 
2095 /*
2096   Standard deviation
2097 */
2098 
2099 double Item_sum_std::val_real()
2100 {
2101   DBUG_ASSERT(fixed == 1);
2102   double nr= Item_sum_variance::val_real();
2103   if (std::isnan(nr))
2104   {
2105     /*
2106       variance_fp_recurrence_next() can overflow in some cases and return "nan":
2107 
2108       CREATE OR REPLACE TABLE t1 (a DOUBLE);
2109       INSERT INTO t1 VALUES (1.7e+308), (-1.7e+308), (0);
2110       SELECT STDDEV_SAMP(a) FROM t1;
2111     */
2112     null_value= true; // Convert "nan" to NULL
2113     return 0;
2114   }
2115   if (std::isinf(nr))
2116     return DBL_MAX;
2117   DBUG_ASSERT(nr >= 0.0);
2118   return sqrt(nr);
2119 }
2120 
2121 Item *Item_sum_std::copy_or_same(THD* thd)
2122 {
2123   return new (thd->mem_root) Item_sum_std(thd, this);
2124 }
2125 
2126 
2127 Item *Item_sum_std::result_item(THD *thd, Field *field)
2128 {
2129   return new (thd->mem_root) Item_std_field(thd, this);
2130 }
2131 
2132 
2133 /*
2134   Variance
2135 */
2136 
2137 
2138 /**
2139   Variance implementation for floating-point implementations, without
2140   catastrophic cancellation, from Knuth's _TAoCP_, 3rd ed, volume 2, pg232.
2141   This alters the value at m, s, and increments count.
2142 */
2143 
2144 /*
2145   These two functions are used by the Item_sum_variance and the
2146   Item_variance_field classes, which are unrelated, and each need to calculate
2147   variance.  The difference between the two classes is that the first is used
2148   for a mundane SELECT, while the latter is used in a GROUPing SELECT.
2149 */
2150 static void variance_fp_recurrence_next(double *m, double *s, ulonglong *count, double nr)
2151 {
2152   *count += 1;
2153 
2154   if (*count == 1)
2155   {
2156     *m= nr;
2157     *s= 0;
2158   }
2159   else
2160   {
2161     double m_kminusone= *m;
2162     volatile double diff= nr - m_kminusone;
2163     *m= m_kminusone + diff / (double) *count;
2164     *s= *s + diff * (nr - *m);
2165   }
2166 }
2167 
2168 
2169 static double variance_fp_recurrence_result(double s, ulonglong count, bool is_sample_variance)
2170 {
2171   if (count == 1)
2172     return 0.0;
2173 
2174   if (is_sample_variance)
2175     return s / (count - 1);
2176 
2177   /* else, is a population variance */
2178   return s / count;
2179 }
2180 
2181 
2182 Item_sum_variance::Item_sum_variance(THD *thd, Item_sum_variance *item):
2183   Item_sum_double(thd, item),
2184     count(item->count), sample(item->sample),
2185     prec_increment(item->prec_increment)
2186 {
2187   recurrence_m= item->recurrence_m;
2188   recurrence_s= item->recurrence_s;
2189 }
2190 
2191 
2192 void Item_sum_variance::fix_length_and_dec_double()
2193 {
2194   DBUG_ASSERT(Item_sum_variance::type_handler() == &type_handler_double);
2195   decimals= MY_MIN(args[0]->decimals + 4, FLOATING_POINT_DECIMALS);
2196 }
2197 
2198 
2199 void Item_sum_variance::fix_length_and_dec_decimal()
2200 {
2201   DBUG_ASSERT(Item_sum_variance::type_handler() == &type_handler_double);
2202   int precision= args[0]->decimal_precision() * 2 + prec_increment;
2203   decimals= MY_MIN(args[0]->decimals + prec_increment,
2204                    FLOATING_POINT_DECIMALS - 1);
2205   max_length= my_decimal_precision_to_length_no_truncation(precision,
2206                                                            decimals,
2207                                                            unsigned_flag);
2208 }
2209 
2210 
2211 bool Item_sum_variance::fix_length_and_dec()
2212 {
2213   DBUG_ENTER("Item_sum_variance::fix_length_and_dec");
2214   maybe_null= null_value= 1;
2215   prec_increment= current_thd->variables.div_precincrement;
2216 
2217   /*
2218     According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
2219     aggregate function; paragraph 7h of Syntax Rules), "the declared
2220     type of the result is an implementation-defined approximate numeric
2221     type.
2222   */
2223   if (args[0]->type_handler()->Item_sum_variance_fix_length_and_dec(this))
2224     DBUG_RETURN(TRUE);
2225   DBUG_PRINT("info", ("Type: %s (%d, %d)", type_handler()->name().ptr(),
2226                       max_length, (int)decimals));
2227   DBUG_RETURN(FALSE);
2228 }
2229 
2230 
2231 Item *Item_sum_variance::copy_or_same(THD* thd)
2232 {
2233   return new (thd->mem_root) Item_sum_variance(thd, this);
2234 }
2235 
2236 
2237 /**
2238   Create a new field to match the type of value we're expected to yield.
2239   If we're grouping, then we need some space to serialize variables into, to
2240   pass around.
2241 */
2242 Field *Item_sum_variance::create_tmp_field(bool group, TABLE *table)
2243 {
2244   Field *field;
2245   if (group)
2246   {
2247     /*
2248       We must store both value and counter in the temporary table in one field.
2249       The easiest way is to do this is to store both value in a string
2250       and unpack on access.
2251     */
2252     field= new Field_string(sizeof(double)*2 + sizeof(longlong), 0,
2253                             &name, &my_charset_bin);
2254   }
2255   else
2256     field= new Field_double(max_length, maybe_null, &name, decimals,
2257                             TRUE);
2258 
2259   if (field != NULL)
2260     field->init(table);
2261 
2262   return field;
2263 }
2264 
2265 
2266 void Item_sum_variance::clear()
2267 {
2268   count= 0;
2269 }
2270 
2271 bool Item_sum_variance::add()
2272 {
2273   /*
2274     Why use a temporary variable?  We don't know if it is null until we
2275     evaluate it, which has the side-effect of setting null_value .
2276   */
2277   double nr= args[0]->val_real();
2278 
2279   if (!args[0]->null_value)
2280     variance_fp_recurrence_next(&recurrence_m, &recurrence_s, &count, nr);
2281   return 0;
2282 }
2283 
2284 double Item_sum_variance::val_real()
2285 {
2286   DBUG_ASSERT(fixed == 1);
2287 
2288   /*
2289     'sample' is a 1/0 boolean value.  If it is 1/true, id est this is a sample
2290     variance call, then we should set nullness when the count of the items
2291     is one or zero.  If it's zero, i.e. a population variance, then we only
2292     set nullness when the count is zero.
2293 
2294     Another way to read it is that 'sample' is the numerical threshold, at and
2295     below which a 'count' number of items is called NULL.
2296   */
2297   DBUG_ASSERT((sample == 0) || (sample == 1));
2298   if (count <= sample)
2299   {
2300     null_value=1;
2301     return 0.0;
2302   }
2303 
2304   null_value=0;
2305   return variance_fp_recurrence_result(recurrence_s, count, sample);
2306 }
2307 
2308 
2309 void Item_sum_variance::reset_field()
2310 {
2311   double nr;
2312   uchar *res= result_field->ptr;
2313 
2314   nr= args[0]->val_real();              /* sets null_value as side-effect */
2315 
2316   if (args[0]->null_value)
2317     bzero(res,sizeof(double)*2+sizeof(longlong));
2318   else
2319   {
2320     /* Serialize format is (double)m, (double)s, (longlong)count */
2321     ulonglong tmp_count;
2322     double tmp_s;
2323     float8store(res, nr);               /* recurrence variable m */
2324     tmp_s= 0.0;
2325     float8store(res + sizeof(double), tmp_s);
2326     tmp_count= 1;
2327     int8store(res + sizeof(double)*2, tmp_count);
2328   }
2329 }
2330 
2331 
2332 void Item_sum_variance::update_field()
2333 {
2334   ulonglong field_count;
2335   uchar *res=result_field->ptr;
2336 
2337   double nr= args[0]->val_real();       /* sets null_value as side-effect */
2338 
2339   if (args[0]->null_value)
2340     return;
2341 
2342   /* Serialize format is (double)m, (double)s, (longlong)count */
2343   double field_recurrence_m, field_recurrence_s;
2344   float8get(field_recurrence_m, res);
2345   float8get(field_recurrence_s, res + sizeof(double));
2346   field_count=sint8korr(res+sizeof(double)*2);
2347 
2348   variance_fp_recurrence_next(&field_recurrence_m, &field_recurrence_s, &field_count, nr);
2349 
2350   float8store(res, field_recurrence_m);
2351   float8store(res + sizeof(double), field_recurrence_s);
2352   res+= sizeof(double)*2;
2353   int8store(res,field_count);
2354 }
2355 
2356 
2357 Item *Item_sum_variance::result_item(THD *thd, Field *field)
2358 {
2359   return new (thd->mem_root) Item_variance_field(thd, this);
2360 }
2361 
2362 /* min & max */
2363 
2364 void Item_sum_min_max::clear()
2365 {
2366   DBUG_ENTER("Item_sum_min_max::clear");
2367   value->clear();
2368   null_value= 1;
2369   DBUG_VOID_RETURN;
2370 }
2371 
2372 
2373 bool
2374 Item_sum_min_max::get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
2375 {
2376   DBUG_ASSERT(fixed == 1);
2377   if (null_value)
2378     return true;
2379   bool retval= value->get_date(ltime, fuzzydate);
2380   if ((null_value= value->null_value))
2381     DBUG_ASSERT(retval == true);
2382   return retval;
2383 }
2384 
2385 
2386 void Item_sum_min_max::direct_add(Item *item)
2387 {
2388   DBUG_ENTER("Item_sum_min_max::direct_add");
2389   DBUG_PRINT("info", ("item: %p", item));
2390   direct_added= TRUE;
2391   direct_item= item;
2392   DBUG_VOID_RETURN;
2393 }
2394 
2395 
2396 double Item_sum_min_max::val_real()
2397 {
2398   DBUG_ENTER("Item_sum_min_max::val_real");
2399   DBUG_ASSERT(fixed == 1);
2400   if (null_value)
2401     DBUG_RETURN(0.0);
2402   double retval= value->val_real();
2403   if ((null_value= value->null_value))
2404     DBUG_ASSERT(retval == 0.0);
2405   DBUG_RETURN(retval);
2406 }
2407 
2408 longlong Item_sum_min_max::val_int()
2409 {
2410   DBUG_ENTER("Item_sum_min_max::val_int");
2411   DBUG_ASSERT(fixed == 1);
2412   if (null_value)
2413     DBUG_RETURN(0);
2414   longlong retval= value->val_int();
2415   if ((null_value= value->null_value))
2416     DBUG_ASSERT(retval == 0);
2417   DBUG_RETURN(retval);
2418 }
2419 
2420 
2421 my_decimal *Item_sum_min_max::val_decimal(my_decimal *val)
2422 {
2423   DBUG_ENTER("Item_sum_min_max::val_decimal");
2424   DBUG_ASSERT(fixed == 1);
2425   if (null_value)
2426     DBUG_RETURN(0);
2427   my_decimal *retval= value->val_decimal(val);
2428   if ((null_value= value->null_value))
2429     DBUG_ASSERT(retval == NULL);
2430   DBUG_RETURN(retval);
2431 }
2432 
2433 
2434 String *
2435 Item_sum_min_max::val_str(String *str)
2436 {
2437   DBUG_ENTER("Item_sum_min_max::val_str");
2438   DBUG_ASSERT(fixed == 1);
2439   if (null_value)
2440     DBUG_RETURN(0);
2441   String *retval= value->val_str(str);
2442   if ((null_value= value->null_value))
2443     DBUG_ASSERT(retval == NULL);
2444   DBUG_RETURN(retval);
2445 }
2446 
2447 
2448 void Item_sum_min_max::cleanup()
2449 {
2450   DBUG_ENTER("Item_sum_min_max::cleanup");
2451   Item_sum::cleanup();
2452   if (cmp)
2453     delete cmp;
2454   cmp= 0;
2455   /*
2456     by default it is TRUE to avoid TRUE reporting by
2457     Item_func_not_all/Item_func_nop_all if this item was never called.
2458 
2459     no_rows_in_result() set it to FALSE if was not results found.
2460     If some results found it will be left unchanged.
2461   */
2462   was_values= TRUE;
2463   DBUG_VOID_RETURN;
2464 }
2465 
2466 void Item_sum_min_max::no_rows_in_result()
2467 {
2468   DBUG_ENTER("Item_sum_min_max::no_rows_in_result");
2469   /* We may be called here twice in case of ref field in function */
2470   if (was_values)
2471   {
2472     was_values= FALSE;
2473     was_null_value= value->null_value;
2474     clear();
2475   }
2476   DBUG_VOID_RETURN;
2477 }
2478 
2479 void Item_sum_min_max::restore_to_before_no_rows_in_result()
2480 {
2481   if (!was_values)
2482   {
2483     was_values= TRUE;
2484     null_value= value->null_value= was_null_value;
2485   }
2486 }
2487 
2488 
2489 Item *Item_sum_min::copy_or_same(THD* thd)
2490 {
2491   DBUG_ENTER("Item_sum_min::copy_or_same");
2492   Item_sum_min *item= new (thd->mem_root) Item_sum_min(thd, this);
2493   item->setup_hybrid(thd, args[0], value);
2494   DBUG_RETURN(item);
2495 }
2496 
2497 
2498 bool Item_sum_min::add()
2499 {
2500   Item *UNINIT_VAR(tmp_item);
2501   DBUG_ENTER("Item_sum_min::add");
2502   DBUG_PRINT("enter", ("this: %p", this));
2503 
2504   if (unlikely(direct_added))
2505   {
2506     /* Change to use direct_item */
2507     tmp_item= arg_cache->get_item();
2508     arg_cache->store(direct_item);
2509   }
2510   DBUG_PRINT("info", ("null_value: %s", null_value ? "TRUE" : "FALSE"));
2511   /* args[0] < value */
2512   arg_cache->cache_value();
2513   if (!arg_cache->null_value &&
2514       (null_value || cmp->compare() < 0))
2515   {
2516     value->store(arg_cache);
2517     value->cache_value();
2518     null_value= 0;
2519   }
2520   if (unlikely(direct_added))
2521   {
2522     /* Restore original item */
2523     direct_added= FALSE;
2524     arg_cache->store(tmp_item);
2525   }
2526   DBUG_RETURN(0);
2527 }
2528 
2529 
2530 Item *Item_sum_max::copy_or_same(THD* thd)
2531 {
2532   Item_sum_max *item= new (thd->mem_root) Item_sum_max(thd, this);
2533   item->setup_hybrid(thd, args[0], value);
2534   return item;
2535 }
2536 
2537 
2538 bool Item_sum_max::add()
2539 {
2540   Item * UNINIT_VAR(tmp_item);
2541   DBUG_ENTER("Item_sum_max::add");
2542   DBUG_PRINT("enter", ("this: %p", this));
2543 
2544   if (unlikely(direct_added))
2545   {
2546     /* Change to use direct_item */
2547     tmp_item= arg_cache->get_item();
2548     arg_cache->store(direct_item);
2549   }
2550   /* args[0] > value */
2551   arg_cache->cache_value();
2552   DBUG_PRINT("info", ("null_value: %s", null_value ? "TRUE" : "FALSE"));
2553   if (!arg_cache->null_value &&
2554       (null_value || cmp->compare() > 0))
2555   {
2556     value->store(arg_cache);
2557     value->cache_value();
2558     null_value= 0;
2559   }
2560   if (unlikely(direct_added))
2561   {
2562     /* Restore original item */
2563     direct_added= FALSE;
2564     arg_cache->store(tmp_item);
2565   }
2566   DBUG_RETURN(0);
2567 }
2568 
2569 
2570 /* bit_or and bit_and */
2571 
2572 longlong Item_sum_bit::val_int()
2573 {
2574   DBUG_ASSERT(fixed == 1);
2575   return (longlong) bits;
2576 }
2577 
2578 
2579 void Item_sum_bit::clear()
2580 {
2581   bits= reset_bits;
2582   if (as_window_function)
2583     clear_as_window();
2584 }
2585 
2586 Item *Item_sum_or::copy_or_same(THD* thd)
2587 {
2588   return new (thd->mem_root) Item_sum_or(thd, this);
2589 }
2590 
2591 bool Item_sum_bit::clear_as_window()
2592 {
2593   memset(bit_counters, 0, sizeof(bit_counters));
2594   num_values_added= 0;
2595   set_bits_from_counters();
2596   return 0;
2597 }
2598 
2599 bool Item_sum_bit::remove_as_window(ulonglong value)
2600 {
2601   DBUG_ASSERT(as_window_function);
2602   if (num_values_added == 0)
2603     return 0; // Nothing to remove.
2604 
2605   for (int i= 0; i < NUM_BIT_COUNTERS; i++)
2606   {
2607     if (!bit_counters[i])
2608     {
2609       // Don't attempt to remove values that were never added.
2610       DBUG_ASSERT((value & (1ULL << i)) == 0);
2611       continue;
2612     }
2613     bit_counters[i]-= (value & (1ULL << i)) ? 1 : 0;
2614   }
2615 
2616   // Prevent overflow;
2617   num_values_added = MY_MIN(num_values_added, num_values_added - 1);
2618   set_bits_from_counters();
2619   return 0;
2620 }
2621 
2622 bool Item_sum_bit::add_as_window(ulonglong value)
2623 {
2624   DBUG_ASSERT(as_window_function);
2625   for (int i= 0; i < NUM_BIT_COUNTERS; i++)
2626   {
2627     bit_counters[i]+= (value & (1ULL << i)) ? 1 : 0;
2628   }
2629   // Prevent overflow;
2630   num_values_added = MY_MAX(num_values_added, num_values_added + 1);
2631   set_bits_from_counters();
2632   return 0;
2633 }
2634 
2635 void Item_sum_or::set_bits_from_counters()
2636 {
2637   ulonglong value= 0;
2638   for (int i= 0; i < NUM_BIT_COUNTERS; i++)
2639   {
2640     value|= bit_counters[i] > 0 ? (1 << i) : 0;
2641   }
2642   bits= value | reset_bits;
2643 }
2644 
2645 bool Item_sum_or::add()
2646 {
2647   ulonglong value= (ulonglong) args[0]->val_int();
2648   if (!args[0]->null_value)
2649   {
2650     if (as_window_function)
2651       return add_as_window(value);
2652     bits|=value;
2653   }
2654   return 0;
2655 }
2656 
2657 void Item_sum_xor::set_bits_from_counters()
2658 {
2659   ulonglong value= 0;
2660   for (int i= 0; i < NUM_BIT_COUNTERS; i++)
2661   {
2662     value|= (bit_counters[i] % 2) ? (1 << i) : 0;
2663   }
2664   bits= value ^ reset_bits;
2665 }
2666 
2667 Item *Item_sum_xor::copy_or_same(THD* thd)
2668 {
2669   return new (thd->mem_root) Item_sum_xor(thd, this);
2670 }
2671 
2672 
2673 bool Item_sum_xor::add()
2674 {
2675   ulonglong value= (ulonglong) args[0]->val_int();
2676   if (!args[0]->null_value)
2677   {
2678     if (as_window_function)
2679       return add_as_window(value);
2680     bits^=value;
2681   }
2682   return 0;
2683 }
2684 
2685 void Item_sum_and::set_bits_from_counters()
2686 {
2687   ulonglong value= 0;
2688   if (!num_values_added)
2689   {
2690     bits= reset_bits;
2691     return;
2692   }
2693 
2694   for (int i= 0; i < NUM_BIT_COUNTERS; i++)
2695   {
2696     // We've only added values of 1 for this bit.
2697     if (bit_counters[i] == num_values_added)
2698       value|= (1ULL << i);
2699   }
2700   bits= value & reset_bits;
2701 }
2702 Item *Item_sum_and::copy_or_same(THD* thd)
2703 {
2704   return new (thd->mem_root) Item_sum_and(thd, this);
2705 }
2706 
2707 
2708 bool Item_sum_and::add()
2709 {
2710   ulonglong value= (ulonglong) args[0]->val_int();
2711   if (!args[0]->null_value)
2712   {
2713     if (as_window_function)
2714       return add_as_window(value);
2715     bits&=value;
2716   }
2717   return 0;
2718 }
2719 
2720 /************************************************************************
2721 ** reset result of a Item_sum with is saved in a tmp_table
2722 *************************************************************************/
2723 
2724 void Item_sum_num::reset_field()
2725 {
2726   double nr= args[0]->val_real();
2727   uchar *res=result_field->ptr;
2728 
2729   if (maybe_null)
2730   {
2731     if (args[0]->null_value)
2732     {
2733       nr=0.0;
2734       result_field->set_null();
2735     }
2736     else
2737       result_field->set_notnull();
2738   }
2739   float8store(res,nr);
2740 }
2741 
2742 
2743 void Item_sum_min_max::reset_field()
2744 {
2745   Item *UNINIT_VAR(tmp_item), *arg0;
2746   DBUG_ENTER("Item_sum_min_max::reset_field");
2747 
2748   arg0= args[0];
2749   if (unlikely(direct_added))
2750   {
2751     /* Switch to use direct item */
2752     tmp_item= value->get_item();
2753     value->store(direct_item);
2754     arg0= direct_item;
2755   }
2756 
2757   switch(result_type()) {
2758   case STRING_RESULT:
2759   {
2760     char buff[MAX_FIELD_WIDTH];
2761     String tmp(buff,sizeof(buff),result_field->charset()),*res;
2762 
2763     res= arg0->val_str(&tmp);
2764     if (arg0->null_value)
2765     {
2766       result_field->set_null();
2767       result_field->reset();
2768     }
2769     else
2770     {
2771       result_field->set_notnull();
2772       result_field->store(res->ptr(),res->length(),tmp.charset());
2773     }
2774     break;
2775   }
2776   case INT_RESULT:
2777   {
2778     longlong nr= arg0->val_int();
2779 
2780     if (maybe_null)
2781     {
2782       if (arg0->null_value)
2783       {
2784 	nr=0;
2785 	result_field->set_null();
2786       }
2787       else
2788 	result_field->set_notnull();
2789     }
2790     DBUG_PRINT("info", ("nr: %lld", nr));
2791     result_field->store(nr, unsigned_flag);
2792     break;
2793   }
2794   case REAL_RESULT:
2795   {
2796     double nr= arg0->val_real();
2797 
2798     if (maybe_null)
2799     {
2800       if (arg0->null_value)
2801       {
2802 	nr=0.0;
2803 	result_field->set_null();
2804       }
2805       else
2806 	result_field->set_notnull();
2807     }
2808     result_field->store(nr);
2809     break;
2810   }
2811   case DECIMAL_RESULT:
2812   {
2813     my_decimal value_buff, *arg_dec= arg0->val_decimal(&value_buff);
2814 
2815     if (maybe_null)
2816     {
2817       if (arg0->null_value)
2818         result_field->set_null();
2819       else
2820         result_field->set_notnull();
2821     }
2822     /*
2823       We must store zero in the field as we will use the field value in
2824       add()
2825     */
2826     if (!arg_dec)                               // Null
2827       arg_dec= &decimal_zero;
2828     result_field->store_decimal(arg_dec);
2829     break;
2830   }
2831   case ROW_RESULT:
2832   case TIME_RESULT:
2833     DBUG_ASSERT(0);
2834   }
2835 
2836   if (unlikely(direct_added))
2837   {
2838     direct_added= FALSE;
2839     value->store(tmp_item);
2840   }
2841   DBUG_VOID_RETURN;
2842 }
2843 
2844 
2845 void Item_sum_sum::reset_field()
2846 {
2847   my_bool null_flag;
2848   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2849   if (result_type() == DECIMAL_RESULT)
2850   {
2851     my_decimal value, *arg_val;
2852     if (unlikely(direct_added))
2853       arg_val= &direct_sum_decimal;
2854     else
2855     {
2856       if (!(arg_val= args[0]->val_decimal(&value)))
2857         arg_val= &decimal_zero;                 // Null
2858     }
2859     result_field->store_decimal(arg_val);
2860   }
2861   else
2862   {
2863     DBUG_ASSERT(result_type() == REAL_RESULT);
2864     double nr= likely(!direct_added) ? args[0]->val_real() : direct_sum_real;
2865     float8store(result_field->ptr, nr);
2866   }
2867 
2868   if (unlikely(direct_added))
2869   {
2870     direct_added= FALSE;
2871     direct_reseted_field= TRUE;
2872     null_flag= direct_sum_is_null;
2873   }
2874   else
2875     null_flag= args[0]->null_value;
2876 
2877   if (null_flag)
2878     result_field->set_null();
2879   else
2880     result_field->set_notnull();
2881 }
2882 
2883 
2884 void Item_sum_count::reset_field()
2885 {
2886   DBUG_ENTER("Item_sum_count::reset_field");
2887   uchar *res=result_field->ptr;
2888   longlong nr=0;
2889   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2890 
2891   if (unlikely(direct_counted))
2892   {
2893     nr= direct_count;
2894     direct_counted= FALSE;
2895     direct_reseted_field= TRUE;
2896   }
2897   else if (!args[0]->maybe_null || !args[0]->is_null())
2898     nr= 1;
2899   DBUG_PRINT("info", ("nr: %lld", nr));
2900   int8store(res,nr);
2901   DBUG_VOID_RETURN;
2902 }
2903 
2904 
2905 void Item_sum_avg::reset_field()
2906 {
2907   uchar *res=result_field->ptr;
2908   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2909   if (result_type() == DECIMAL_RESULT)
2910   {
2911     longlong tmp;
2912     my_decimal value, *arg_dec= args[0]->val_decimal(&value);
2913     if (args[0]->null_value)
2914     {
2915       arg_dec= &decimal_zero;
2916       tmp= 0;
2917     }
2918     else
2919       tmp= 1;
2920     my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
2921     res+= dec_bin_size;
2922     int8store(res, tmp);
2923   }
2924   else
2925   {
2926     double nr= args[0]->val_real();
2927 
2928     if (args[0]->null_value)
2929       bzero(res,sizeof(double)+sizeof(longlong));
2930     else
2931     {
2932       longlong tmp= 1;
2933       float8store(res,nr);
2934       res+=sizeof(double);
2935       int8store(res,tmp);
2936     }
2937   }
2938 }
2939 
2940 
2941 void Item_sum_bit::reset_field()
2942 {
2943   reset_and_add();
2944   int8store(result_field->ptr, bits);
2945 }
2946 
2947 void Item_sum_bit::update_field()
2948 {
2949   // We never call update_field when computing the function as a window
2950   // function. Setting bits to a random value invalidates the bits counters and
2951   // the result of the bit function becomes erroneous.
2952   DBUG_ASSERT(!as_window_function);
2953   uchar *res=result_field->ptr;
2954   bits= uint8korr(res);
2955   add();
2956   int8store(res, bits);
2957 }
2958 
2959 
2960 /**
2961   calc next value and merge it with field_value.
2962 */
2963 
2964 void Item_sum_sum::update_field()
2965 {
2966   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2967   if (result_type() == DECIMAL_RESULT)
2968   {
2969     my_decimal value, *arg_val;
2970     my_bool null_flag;
2971     if (unlikely(direct_added || direct_reseted_field))
2972     {
2973       direct_added= direct_reseted_field= FALSE;
2974       arg_val= &direct_sum_decimal;
2975       null_flag= direct_sum_is_null;
2976     }
2977     else
2978     {
2979       arg_val= args[0]->val_decimal(&value);
2980       null_flag= args[0]->null_value;
2981     }
2982 
2983     if (!null_flag)
2984     {
2985       if (!result_field->is_null())
2986       {
2987         my_decimal field_value;
2988         my_decimal *field_val= result_field->val_decimal(&field_value);
2989         my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2990         result_field->store_decimal(dec_buffs);
2991       }
2992       else
2993       {
2994         result_field->store_decimal(arg_val);
2995         result_field->set_notnull();
2996       }
2997     }
2998   }
2999   else
3000   {
3001     double old_nr,nr;
3002     uchar *res= result_field->ptr;
3003     my_bool null_flag;
3004 
3005     float8get(old_nr,res);
3006     if (unlikely(direct_added || direct_reseted_field))
3007     {
3008       direct_added= direct_reseted_field= FALSE;
3009       null_flag= direct_sum_is_null;
3010       nr= direct_sum_real;
3011     }
3012     else
3013     {
3014       nr= args[0]->val_real();
3015       null_flag= args[0]->null_value;
3016     }
3017     if (!null_flag)
3018     {
3019       old_nr+=nr;
3020       result_field->set_notnull();
3021     }
3022     float8store(res,old_nr);
3023   }
3024 }
3025 
3026 
3027 void Item_sum_count::update_field()
3028 {
3029   DBUG_ENTER("Item_sum_count::update_field");
3030   longlong nr;
3031   uchar *res=result_field->ptr;
3032 
3033   nr=sint8korr(res);
3034   if (unlikely(direct_counted || direct_reseted_field))
3035   {
3036     direct_counted= direct_reseted_field= FALSE;
3037     nr+= direct_count;
3038   }
3039   else if (!args[0]->maybe_null || !args[0]->is_null())
3040     nr++;
3041   DBUG_PRINT("info", ("nr: %lld", nr));
3042   int8store(res,nr);
3043   DBUG_VOID_RETURN;
3044 }
3045 
3046 
3047 void Item_sum_avg::update_field()
3048 {
3049   longlong field_count;
3050   uchar *res=result_field->ptr;
3051 
3052   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
3053 
3054   if (result_type() == DECIMAL_RESULT)
3055   {
3056     my_decimal value, *arg_val= args[0]->val_decimal(&value);
3057     if (!args[0]->null_value)
3058     {
3059       binary2my_decimal(E_DEC_FATAL_ERROR, res,
3060                         dec_buffs + 1, f_precision, f_scale);
3061       field_count= sint8korr(res + dec_bin_size);
3062       my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
3063       my_decimal2binary(E_DEC_FATAL_ERROR, dec_buffs,
3064                         res, f_precision, f_scale);
3065       res+= dec_bin_size;
3066       field_count++;
3067       int8store(res, field_count);
3068     }
3069   }
3070   else
3071   {
3072     double nr;
3073 
3074     nr= args[0]->val_real();
3075     if (!args[0]->null_value)
3076     {
3077       double old_nr;
3078       float8get(old_nr, res);
3079       field_count= sint8korr(res + sizeof(double));
3080       old_nr+= nr;
3081       float8store(res,old_nr);
3082       res+= sizeof(double);
3083       field_count++;
3084       int8store(res, field_count);
3085     }
3086   }
3087 }
3088 
3089 
3090 Item *Item_sum_avg::result_item(THD *thd, Field *field)
3091 {
3092   return
3093     result_type() == DECIMAL_RESULT ?
3094     (Item_avg_field*) new (thd->mem_root) Item_avg_field_decimal(thd, this) :
3095     (Item_avg_field*) new (thd->mem_root) Item_avg_field_double(thd, this);
3096 }
3097 
3098 
3099 void Item_sum_min_max::update_field()
3100 {
3101   DBUG_ENTER("Item_sum_min_max::update_field");
3102   Item *UNINIT_VAR(tmp_item);
3103   if (unlikely(direct_added))
3104   {
3105     tmp_item= args[0];
3106     args[0]= direct_item;
3107   }
3108   switch (result_type()) {
3109   case STRING_RESULT:
3110     min_max_update_str_field();
3111     break;
3112   case INT_RESULT:
3113     min_max_update_int_field();
3114     break;
3115   case DECIMAL_RESULT:
3116     min_max_update_decimal_field();
3117     break;
3118   default:
3119     min_max_update_real_field();
3120   }
3121   if (unlikely(direct_added))
3122   {
3123     direct_added= FALSE;
3124     args[0]= tmp_item;
3125   }
3126   DBUG_VOID_RETURN;
3127 }
3128 
3129 
3130 void
3131 Item_sum_min_max::min_max_update_str_field()
3132 {
3133   DBUG_ENTER("Item_sum_min_max::min_max_update_str_field");
3134   DBUG_ASSERT(cmp);
3135   String *res_str=args[0]->val_str(&cmp->value1);
3136 
3137   if (!args[0]->null_value)
3138   {
3139     if (result_field->is_null())
3140       result_field->store(res_str->ptr(),res_str->length(),res_str->charset());
3141     else
3142     {
3143       result_field->val_str(&cmp->value2);
3144       if ((cmp_sign * sortcmp(res_str,&cmp->value2,collation.collation)) < 0)
3145         result_field->store(res_str->ptr(),res_str->length(),res_str->charset());
3146     }
3147     result_field->set_notnull();
3148   }
3149   DBUG_VOID_RETURN;
3150 }
3151 
3152 
3153 void
3154 Item_sum_min_max::min_max_update_real_field()
3155 {
3156   double nr,old_nr;
3157 
3158   DBUG_ENTER("Item_sum_min_max::min_max_update_real_field");
3159   old_nr=result_field->val_real();
3160   nr= args[0]->val_real();
3161   if (!args[0]->null_value)
3162   {
3163     if (result_field->is_null(0) ||
3164 	(cmp_sign > 0 ? old_nr > nr : old_nr < nr))
3165       old_nr=nr;
3166     result_field->set_notnull();
3167   }
3168   else if (result_field->is_null(0))
3169     result_field->set_null();
3170   result_field->store(old_nr);
3171   DBUG_VOID_RETURN;
3172 }
3173 
3174 
3175 void
3176 Item_sum_min_max::min_max_update_int_field()
3177 {
3178   longlong nr,old_nr;
3179 
3180   DBUG_ENTER("Item_sum_min_max::min_max_update_int_field");
3181   old_nr=result_field->val_int();
3182   nr=args[0]->val_int();
3183   if (!args[0]->null_value)
3184   {
3185     if (result_field->is_null(0))
3186       old_nr=nr;
3187     else
3188     {
3189       bool res=(unsigned_flag ?
3190 		(ulonglong) old_nr > (ulonglong) nr :
3191 		old_nr > nr);
3192       /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
3193       if ((cmp_sign > 0) ^ (!res))
3194 	old_nr=nr;
3195     }
3196     result_field->set_notnull();
3197   }
3198   else if (result_field->is_null(0))
3199     result_field->set_null();
3200   DBUG_PRINT("info", ("nr: %lld", old_nr));
3201   result_field->store(old_nr, unsigned_flag);
3202   DBUG_VOID_RETURN;
3203 }
3204 
3205 
3206 /**
3207   @todo
3208   optimize: do not get result_field in case of args[0] is NULL
3209 */
3210 void
3211 Item_sum_min_max::min_max_update_decimal_field()
3212 {
3213   DBUG_ENTER("Item_sum_min_max::min_max_update_decimal_field");
3214   my_decimal old_val, nr_val;
3215   const my_decimal *old_nr;
3216   const my_decimal *nr= args[0]->val_decimal(&nr_val);
3217   if (!args[0]->null_value)
3218   {
3219     if (result_field->is_null(0))
3220       old_nr=nr;
3221     else
3222     {
3223       old_nr= result_field->val_decimal(&old_val);
3224       bool res= my_decimal_cmp(old_nr, nr) > 0;
3225       /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
3226       if ((cmp_sign > 0) ^ (!res))
3227         old_nr=nr;
3228     }
3229     result_field->set_notnull();
3230     result_field->store_decimal(old_nr);
3231   }
3232   else if (result_field->is_null(0))
3233     result_field->set_null();
3234   DBUG_VOID_RETURN;
3235 }
3236 
3237 
3238 double Item_avg_field_double::val_real()
3239 {
3240   // fix_fields() never calls for this Item
3241   double nr;
3242   longlong count;
3243   uchar *res;
3244 
3245   float8get(nr,field->ptr);
3246   res= (field->ptr+sizeof(double));
3247   count= sint8korr(res);
3248 
3249   if ((null_value= !count))
3250     return 0.0;
3251   return nr/(double) count;
3252 }
3253 
3254 
3255 my_decimal *Item_avg_field_decimal::val_decimal(my_decimal *dec_buf)
3256 {
3257   // fix_fields() never calls for this Item
3258   longlong count= sint8korr(field->ptr + dec_bin_size);
3259   if ((null_value= !count))
3260     return 0;
3261 
3262   my_decimal dec_count, dec_field;
3263   binary2my_decimal(E_DEC_FATAL_ERROR,
3264                     field->ptr, &dec_field, f_precision, f_scale);
3265   int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
3266   my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
3267                  &dec_field, &dec_count, prec_increment);
3268   return dec_buf;
3269 }
3270 
3271 
3272 double Item_std_field::val_real()
3273 {
3274   double nr;
3275   // fix_fields() never calls for this Item
3276   nr= Item_variance_field::val_real();
3277   DBUG_ASSERT(nr >= 0.0);
3278   return sqrt(nr);
3279 }
3280 
3281 
3282 double Item_variance_field::val_real()
3283 {
3284   // fix_fields() never calls for this Item
3285   double recurrence_s;
3286   ulonglong count;
3287   float8get(recurrence_s, (field->ptr + sizeof(double)));
3288   count=sint8korr(field->ptr+sizeof(double)*2);
3289 
3290   if ((null_value= (count <= sample)))
3291     return 0.0;
3292 
3293   return variance_fp_recurrence_result(recurrence_s, count, sample);
3294 }
3295 
3296 
3297 /****************************************************************************
3298 ** Functions to handle dynamic loadable aggregates
3299 ** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
3300 ** Adapted for UDAs by: Andreas F. Bobak <bobak@relog.ch>.
3301 ** Rewritten by: Monty.
3302 ****************************************************************************/
3303 
3304 #ifdef HAVE_DLOPEN
3305 
3306 void Item_udf_sum::clear()
3307 {
3308   DBUG_ENTER("Item_udf_sum::clear");
3309   udf.clear();
3310   DBUG_VOID_RETURN;
3311 }
3312 
3313 bool Item_udf_sum::add()
3314 {
3315   my_bool tmp_null_value;
3316   DBUG_ENTER("Item_udf_sum::add");
3317   udf.add(&tmp_null_value);
3318   null_value= tmp_null_value;
3319   DBUG_RETURN(0);
3320 }
3321 
3322 void Item_udf_sum::cleanup()
3323 {
3324   /*
3325     udf_handler::cleanup() nicely handles case when we have not
3326     original item but one created by copy_or_same() method.
3327   */
3328   udf.cleanup();
3329   Item_sum::cleanup();
3330 }
3331 
3332 
3333 void Item_udf_sum::print(String *str, enum_query_type query_type)
3334 {
3335   str->append(func_name());
3336   str->append('(');
3337   for (uint i=0 ; i < arg_count ; i++)
3338   {
3339     if (i)
3340       str->append(',');
3341     args[i]->print(str, query_type);
3342   }
3343   str->append(')');
3344 }
3345 
3346 
3347 Item *Item_sum_udf_float::copy_or_same(THD* thd)
3348 {
3349   return new (thd->mem_root) Item_sum_udf_float(thd, this);
3350 }
3351 
3352 double Item_sum_udf_float::val_real()
3353 {
3354   my_bool tmp_null_value;
3355   double res;
3356   DBUG_ASSERT(fixed == 1);
3357   DBUG_ENTER("Item_sum_udf_float::val");
3358   DBUG_PRINT("enter",("result_type: %d  arg_count: %d",
3359 		     args[0]->result_type(), arg_count));
3360   res= udf.val(&tmp_null_value);
3361   null_value= tmp_null_value;
3362   DBUG_RETURN(res);
3363 }
3364 
3365 
3366 String *Item_sum_udf_float::val_str(String *str)
3367 {
3368   return val_string_from_real(str);
3369 }
3370 
3371 
3372 my_decimal *Item_sum_udf_float::val_decimal(my_decimal *dec)
3373 {
3374   return val_decimal_from_real(dec);
3375 }
3376 
3377 
3378 String *Item_sum_udf_decimal::val_str(String *str)
3379 {
3380   return val_string_from_decimal(str);
3381 }
3382 
3383 
3384 double Item_sum_udf_decimal::val_real()
3385 {
3386   return val_real_from_decimal();
3387 }
3388 
3389 
3390 longlong Item_sum_udf_decimal::val_int()
3391 {
3392   return val_int_from_decimal();
3393 }
3394 
3395 
3396 my_decimal *Item_sum_udf_decimal::val_decimal(my_decimal *dec_buf)
3397 {
3398   my_decimal *res;
3399   my_bool tmp_null_value;
3400   DBUG_ASSERT(fixed == 1);
3401   DBUG_ENTER("Item_func_udf_decimal::val_decimal");
3402   DBUG_PRINT("enter",("result_type: %d  arg_count: %d",
3403                      args[0]->result_type(), arg_count));
3404 
3405   res= udf.val_decimal(&tmp_null_value, dec_buf);
3406   null_value= tmp_null_value;
3407   DBUG_RETURN(res);
3408 }
3409 
3410 
3411 Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
3412 {
3413   return new (thd->mem_root) Item_sum_udf_decimal(thd, this);
3414 }
3415 
3416 
3417 Item *Item_sum_udf_int::copy_or_same(THD* thd)
3418 {
3419   return new (thd->mem_root) Item_sum_udf_int(thd, this);
3420 }
3421 
3422 longlong Item_sum_udf_int::val_int()
3423 {
3424   my_bool tmp_null_value;
3425   longlong res;
3426   DBUG_ASSERT(fixed == 1);
3427   DBUG_ENTER("Item_sum_udf_int::val_int");
3428   DBUG_PRINT("enter",("result_type: %d  arg_count: %d",
3429 		     args[0]->result_type(), arg_count));
3430   res= udf.val_int(&tmp_null_value);
3431   null_value= tmp_null_value;
3432   DBUG_RETURN(res);
3433 }
3434 
3435 
3436 String *Item_sum_udf_int::val_str(String *str)
3437 {
3438   return val_string_from_int(str);
3439 }
3440 
3441 my_decimal *Item_sum_udf_int::val_decimal(my_decimal *dec)
3442 {
3443   return val_decimal_from_int(dec);
3444 }
3445 
3446 
3447 /** Default max_length is max argument length. */
3448 
3449 bool Item_sum_udf_str::fix_length_and_dec()
3450 {
3451   DBUG_ENTER("Item_sum_udf_str::fix_length_and_dec");
3452   max_length=0;
3453   for (uint i = 0; i < arg_count; i++)
3454     set_if_bigger(max_length,args[i]->max_length);
3455   DBUG_RETURN(FALSE);
3456 }
3457 
3458 
3459 Item *Item_sum_udf_str::copy_or_same(THD* thd)
3460 {
3461   return new (thd->mem_root) Item_sum_udf_str(thd, this);
3462 }
3463 
3464 
3465 my_decimal *Item_sum_udf_str::val_decimal(my_decimal *dec)
3466 {
3467   return val_decimal_from_string(dec);
3468 }
3469 
3470 String *Item_sum_udf_str::val_str(String *str)
3471 {
3472   DBUG_ASSERT(fixed == 1);
3473   DBUG_ENTER("Item_sum_udf_str::str");
3474   String *res=udf.val_str(str,&str_value);
3475   null_value = !res;
3476   DBUG_RETURN(res);
3477 }
3478 
3479 #endif /* HAVE_DLOPEN */
3480 
3481 
3482 /*****************************************************************************
3483  GROUP_CONCAT function
3484 
3485  SQL SYNTAX:
3486   GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
3487     [SEPARATOR str_const])
3488 
3489  concat of values from "group by" operation
3490 
3491  BUGS
3492    Blobs doesn't work with DISTINCT or ORDER BY
3493 *****************************************************************************/
3494 
3495 
3496 
3497 /**
3498   Compares the values for fields in expr list of GROUP_CONCAT.
3499   @note
3500 
3501      GROUP_CONCAT([DISTINCT] expr [,expr ...]
3502               [ORDER BY {unsigned_integer | col_name | expr}
3503                   [ASC | DESC] [,col_name ...]]
3504               [SEPARATOR str_val])
3505 
3506   @return
3507   @retval -1 : key1 < key2
3508   @retval  0 : key1 = key2
3509   @retval  1 : key1 > key2
3510 */
3511 
3512 extern "C"
3513 int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
3514                                        const void* key2)
3515 {
3516   Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
3517 
3518   for (uint i= 0; i < item_func->arg_count_field; i++)
3519   {
3520     Item *item= item_func->args[i];
3521     /*
3522       If item is a const item then either get_tmp_table_field returns 0
3523       or it is an item over a const table.
3524     */
3525     if (item->const_item())
3526       continue;
3527     /*
3528       We have to use get_tmp_table_field() instead of
3529       real_item()->get_tmp_table_field() because we want the field in
3530       the temporary table, not the original field
3531     */
3532     Field *field= item->get_tmp_table_field();
3533 
3534     if (!field)
3535       continue;
3536 
3537     uint offset= (field->offset(field->table->record[0]) -
3538                   field->table->s->null_bytes);
3539     int res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset);
3540     if (res)
3541       return res;
3542   }
3543   return 0;
3544 }
3545 
3546 
3547 /**
3548   function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
3549 */
3550 
3551 extern "C"
3552 int group_concat_key_cmp_with_order(void* arg, const void* key1,
3553                                     const void* key2)
3554 {
3555   Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
3556   ORDER **order_item, **end;
3557 
3558   for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
3559        order_item < end;
3560        order_item++)
3561   {
3562     Item *item= *(*order_item)->item;
3563     /*
3564       If field_item is a const item then either get_tmp_table_field returns 0
3565       or it is an item over a const table.
3566     */
3567     if (item->const_item())
3568       continue;
3569     /*
3570       If item is a const item then either get_tmp_table_field returns 0
3571       or it is an item over a const table.
3572     */
3573     if (item->const_item())
3574       continue;
3575     /*
3576       We have to use get_tmp_table_field() instead of
3577       real_item()->get_tmp_table_field() because we want the field in
3578       the temporary table, not the original field
3579 
3580       Note that for the case of ROLLUP, field may point to another table
3581       tham grp_item->table. This is however ok as the table definitions are
3582       the same.
3583     */
3584     Field *field= item->get_tmp_table_field();
3585     if (!field)
3586       continue;
3587 
3588     uint offset= (field->offset(field->table->record[0]) -
3589                   field->table->s->null_bytes);
3590     int res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset);
3591     if (res)
3592       return ((*order_item)->direction == ORDER::ORDER_ASC) ? res : -res;
3593   }
3594   /*
3595     We can't return 0 because in that case the tree class would remove this
3596     item as double value. This would cause problems for case-changes and
3597     if the returned values are not the same we do the sort on.
3598   */
3599   return 1;
3600 }
3601 
3602 
3603 /**
3604   Append data from current leaf to item->result.
3605 */
3606 
3607 extern "C"
3608 int dump_leaf_key(void* key_arg, element_count count __attribute__((unused)),
3609                   void* item_arg)
3610 {
3611   Item_func_group_concat *item= (Item_func_group_concat *) item_arg;
3612   TABLE *table= item->table;
3613   uint max_length= table->in_use->variables.group_concat_max_len;
3614   String tmp((char *)table->record[1], table->s->reclength,
3615              default_charset_info);
3616   String tmp2;
3617   uchar *key= (uchar *) key_arg;
3618   String *result= &item->result;
3619   Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
3620   uint old_length= result->length();
3621 
3622   ulonglong *offset_limit= &item->copy_offset_limit;
3623   ulonglong *row_limit = &item->copy_row_limit;
3624   if (item->limit_clause && !(*row_limit))
3625   {
3626     item->result_finalized= true;
3627     return 1;
3628   }
3629 
3630   tmp.length(0);
3631 
3632   if (item->limit_clause && (*offset_limit))
3633   {
3634     item->row_count++;
3635     (*offset_limit)--;
3636     return 0;
3637   }
3638 
3639   if (!item->result_finalized)
3640     item->result_finalized= true;
3641   else
3642     result->append(*item->separator);
3643 
3644   for (; arg < arg_end; arg++)
3645   {
3646     String *res;
3647     /*
3648       We have to use get_tmp_table_field() instead of
3649       real_item()->get_tmp_table_field() because we want the field in
3650       the temporary table, not the original field
3651       We also can't use table->field array to access the fields
3652       because it contains both order and arg list fields.
3653      */
3654     if ((*arg)->const_item())
3655       res= (*arg)->val_str(&tmp);
3656     else
3657     {
3658       Field *field= (*arg)->get_tmp_table_field();
3659       if (field)
3660       {
3661         uint offset= (field->offset(field->table->record[0]) -
3662                       table->s->null_bytes);
3663         DBUG_ASSERT(offset < table->s->reclength);
3664         res= field->val_str(&tmp, key + offset);
3665       }
3666       else
3667         res= (*arg)->val_str(&tmp);
3668     }
3669     if (res)
3670       result->append(*res);
3671   }
3672 
3673   if (item->limit_clause)
3674     (*row_limit)--;
3675   item->row_count++;
3676 
3677   /* stop if length of result more than max_length */
3678   if (result->length() > max_length)
3679   {
3680     CHARSET_INFO *cs= item->collation.collation;
3681     const char *ptr= result->ptr();
3682     THD *thd= current_thd;
3683     /*
3684       It's ok to use item->result.length() as the fourth argument
3685       as this is never used to limit the length of the data.
3686       Cut is done with the third argument.
3687     */
3688     size_t add_length= Well_formed_prefix(cs,
3689                                         ptr + old_length,
3690                                         ptr + max_length,
3691                                         result->length()).length();
3692     result->length(old_length + add_length);
3693     item->warning_for_row= TRUE;
3694     push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
3695                         ER_CUT_VALUE_GROUP_CONCAT,
3696                         ER_THD(thd, ER_CUT_VALUE_GROUP_CONCAT),
3697                         item->row_count);
3698 
3699     /**
3700        To avoid duplicated warnings in Item_func_group_concat::val_str()
3701     */
3702     if (table && table->blob_storage)
3703       table->blob_storage->set_truncated_value(false);
3704     return 1;
3705   }
3706   return 0;
3707 }
3708 
3709 
3710 /**
3711   Constructor of Item_func_group_concat.
3712 
3713   @param distinct_arg   distinct
3714   @param select_list    list of expression for show values
3715   @param order_list     list of sort columns
3716   @param separator_arg  string value of separator.
3717 */
3718 
3719 Item_func_group_concat::
3720 Item_func_group_concat(THD *thd, Name_resolution_context *context_arg,
3721                        bool distinct_arg, List<Item> *select_list,
3722                        const SQL_I_List<ORDER> &order_list,
3723                        String *separator_arg, bool limit_clause,
3724                        Item *row_limit_arg, Item *offset_limit_arg)
3725   :Item_sum(thd), tmp_table_param(0), separator(separator_arg), tree(0),
3726    unique_filter(NULL), table(0),
3727    order(0), context(context_arg),
3728    arg_count_order(order_list.elements),
3729    arg_count_field(select_list->elements),
3730    row_count(0),
3731    distinct(distinct_arg),
3732    warning_for_row(FALSE),
3733    force_copy_fields(0), row_limit(NULL),
3734    offset_limit(NULL), limit_clause(limit_clause),
3735    copy_offset_limit(0), copy_row_limit(0), original(0)
3736 {
3737   Item *item_select;
3738   Item **arg_ptr;
3739 
3740   quick_group= FALSE;
3741   arg_count= arg_count_field + arg_count_order;
3742 
3743   /*
3744     We need to allocate:
3745     args - arg_count_field+arg_count_order
3746            (for possible order items in temporary tables)
3747     order - arg_count_order
3748   */
3749   if (!(args= (Item**) thd->alloc(sizeof(Item*) * arg_count * 2 +
3750                                   sizeof(ORDER*)*arg_count_order)))
3751     return;
3752 
3753   order= (ORDER**)(args + arg_count);
3754 
3755   /* fill args items of show and sort */
3756   List_iterator_fast<Item> li(*select_list);
3757 
3758   for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
3759     *arg_ptr= item_select;
3760 
3761   if (arg_count_order)
3762   {
3763     ORDER **order_ptr= order;
3764     for (ORDER *order_item= order_list.first;
3765          order_item != NULL;
3766          order_item= order_item->next)
3767     {
3768       (*order_ptr++)= order_item;
3769       *arg_ptr= *order_item->item;
3770       order_item->item= arg_ptr++;
3771     }
3772   }
3773 
3774   /* orig_args is only used for print() */
3775   orig_args= (Item**) (order + arg_count_order);
3776   if (arg_count)
3777     memcpy(orig_args, args, sizeof(Item*) * arg_count);
3778   if (limit_clause)
3779   {
3780     row_limit= row_limit_arg;
3781     offset_limit= offset_limit_arg;
3782   }
3783 }
3784 
3785 
3786 Item_func_group_concat::Item_func_group_concat(THD *thd,
3787                                                Item_func_group_concat *item)
3788   :Item_sum(thd, item),
3789   tmp_table_param(item->tmp_table_param),
3790   separator(item->separator),
3791   tree(item->tree),
3792   tree_len(item->tree_len),
3793   unique_filter(item->unique_filter),
3794   table(item->table),
3795   context(item->context),
3796   arg_count_order(item->arg_count_order),
3797   arg_count_field(item->arg_count_field),
3798   row_count(item->row_count),
3799   distinct(item->distinct),
3800   warning_for_row(item->warning_for_row),
3801   always_null(item->always_null),
3802   force_copy_fields(item->force_copy_fields),
3803   row_limit(item->row_limit), offset_limit(item->offset_limit),
3804   limit_clause(item->limit_clause),copy_offset_limit(item->copy_offset_limit),
3805   copy_row_limit(item->copy_row_limit), original(item)
3806 {
3807   quick_group= item->quick_group;
3808   result.set_charset(collation.collation);
3809 
3810   /*
3811     Since the ORDER structures pointed to by the elements of the 'order' array
3812     may be modified in find_order_in_list() called from
3813     Item_func_group_concat::setup(), create a copy of those structures so that
3814     such modifications done in this object would not have any effect on the
3815     object being copied.
3816   */
3817   ORDER *tmp;
3818   if (!(tmp= (ORDER *) thd->alloc(sizeof(ORDER *) * arg_count_order +
3819                                   sizeof(ORDER) * arg_count_order)))
3820     return;
3821   order= (ORDER **)(tmp + arg_count_order);
3822   for (uint i= 0; i < arg_count_order; i++, tmp++)
3823   {
3824     /*
3825       Compiler generated copy constructor is used to
3826       to copy all the members of ORDER struct.
3827       It's also necessary to update ORDER::next pointer
3828       so that it points to new ORDER element.
3829     */
3830     new (tmp) st_order(*(item->order[i]));
3831     tmp->next= (i + 1 == arg_count_order) ? NULL : (tmp + 1);
3832     order[i]= tmp;
3833   }
3834 }
3835 
3836 
3837 void Item_func_group_concat::cleanup()
3838 {
3839   DBUG_ENTER("Item_func_group_concat::cleanup");
3840   Item_sum::cleanup();
3841 
3842   /*
3843     Free table and tree if they belong to this item (if item have not pointer
3844     to original item from which was made copy => it own its objects )
3845   */
3846   if (!original)
3847   {
3848     delete tmp_table_param;
3849     tmp_table_param= 0;
3850     if (table)
3851     {
3852       THD *thd= table->in_use;
3853       if (table->blob_storage)
3854         delete table->blob_storage;
3855       free_tmp_table(thd, table);
3856       table= 0;
3857       if (tree)
3858       {
3859         delete_tree(tree, 0);
3860         tree= 0;
3861       }
3862       if (unique_filter)
3863       {
3864         delete unique_filter;
3865         unique_filter= NULL;
3866       }
3867     }
3868     DBUG_ASSERT(tree == 0);
3869   }
3870   /*
3871     As the ORDER structures pointed to by the elements of the
3872     'order' array may be modified in find_order_in_list() called
3873     from Item_func_group_concat::setup() to point to runtime
3874     created objects, we need to reset them back to the original
3875     arguments of the function.
3876   */
3877   ORDER **order_ptr= order;
3878   for (uint i= 0; i < arg_count_order; i++)
3879   {
3880     (*order_ptr)->item= &args[arg_count_field + i];
3881     order_ptr++;
3882   }
3883   DBUG_VOID_RETURN;
3884 }
3885 
3886 
3887 Item *Item_func_group_concat::copy_or_same(THD* thd)
3888 {
3889   return new (thd->mem_root) Item_func_group_concat(thd, this);
3890 }
3891 
3892 
3893 void Item_func_group_concat::clear()
3894 {
3895   result.length(0);
3896   result.copy();
3897   null_value= TRUE;
3898   warning_for_row= FALSE;
3899   result_finalized= FALSE;
3900   if (offset_limit)
3901     copy_offset_limit= offset_limit->val_int();
3902   if (row_limit)
3903     copy_row_limit= row_limit->val_int();
3904   if (tree)
3905   {
3906     reset_tree(tree);
3907     tree_len= 0;
3908   }
3909   if (unique_filter)
3910     unique_filter->reset();
3911   if (table && table->blob_storage)
3912     table->blob_storage->reset();
3913   /* No need to reset the table as we never call write_row */
3914 }
3915 
3916 struct st_repack_tree {
3917   TREE tree;
3918   TABLE *table;
3919   size_t len, maxlen;
3920 };
3921 
3922 extern "C"
3923 int copy_to_tree(void* key, element_count count __attribute__((unused)),
3924                  void* arg)
3925 {
3926   struct st_repack_tree *st= (struct st_repack_tree*)arg;
3927   TABLE *table= st->table;
3928   Field* field= table->field[0];
3929   const uchar *ptr= field->ptr_in_record((uchar*)key - table->s->null_bytes);
3930   size_t len= (size_t)field->val_int(ptr);
3931 
3932   DBUG_ASSERT(count == 1);
3933   if (!tree_insert(&st->tree, key, 0, st->tree.custom_arg))
3934     return 1;
3935 
3936   st->len += len;
3937   return st->len > st->maxlen;
3938 }
3939 
3940 bool Item_func_group_concat::repack_tree(THD *thd)
3941 {
3942   struct st_repack_tree st;
3943   int size= tree->size_of_element;
3944   if (!tree->offset_to_key)
3945     size-= sizeof(void*);
3946 
3947   init_tree(&st.tree, (size_t) MY_MIN(thd->variables.max_heap_table_size,
3948                                       thd->variables.sortbuff_size/16), 0,
3949             size, group_concat_key_cmp_with_order, NULL,
3950             (void*) this, MYF(MY_THREAD_SPECIFIC));
3951   DBUG_ASSERT(tree->size_of_element == st.tree.size_of_element);
3952   st.table= table;
3953   st.len= 0;
3954   st.maxlen= thd->variables.group_concat_max_len;
3955   tree_walk(tree, &copy_to_tree, &st, left_root_right);
3956   if (st.len <= st.maxlen) // Copying aborted. Must be OOM
3957   {
3958     delete_tree(&st.tree, 0);
3959     return 1;
3960   }
3961   delete_tree(tree, 0);
3962   *tree= st.tree;
3963   tree_len= st.len;
3964   return 0;
3965 }
3966 
3967 /*
3968   Repacking the tree is expensive. But it keeps the tree small, and
3969   inserting into an unnecessary large tree is also waste of time.
3970 
3971   The following number is best-by-test. Test execution time slowly
3972   decreases up to N=10 (that is, factor=1024) and then starts to increase,
3973   again, very slowly.
3974 */
3975 #define GCONCAT_REPACK_FACTOR 10
3976 
3977 bool Item_func_group_concat::add()
3978 {
3979   if (always_null)
3980     return 0;
3981   copy_fields(tmp_table_param);
3982   if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
3983     return TRUE;
3984 
3985   size_t row_str_len= 0;
3986   StringBuffer<MAX_FIELD_WIDTH> buf;
3987   String *res;
3988   for (uint i= 0; i < arg_count_field; i++)
3989   {
3990     Item *show_item= args[i];
3991     if (show_item->const_item())
3992       continue;
3993 
3994     Field *field= show_item->get_tmp_table_field();
3995     if (field)
3996     {
3997       if (field->is_null_in_record((const uchar*) table->record[0]))
3998         return 0;                    // Skip row if it contains null
3999       if (tree && (res= field->val_str(&buf)))
4000         row_str_len+= res->length();
4001     }
4002   }
4003 
4004   null_value= FALSE;
4005   bool row_eligible= TRUE;
4006 
4007   if (distinct)
4008   {
4009     /* Filter out duplicate rows. */
4010     uint count= unique_filter->elements_in_tree();
4011     unique_filter->unique_add(table->record[0] + table->s->null_bytes);
4012     if (count == unique_filter->elements_in_tree())
4013       row_eligible= FALSE;
4014   }
4015 
4016   TREE_ELEMENT *el= 0;                          // Only for safety
4017   if (row_eligible && tree)
4018   {
4019     THD *thd= table->in_use;
4020     table->field[0]->store(row_str_len, FALSE);
4021     if ((tree_len >> GCONCAT_REPACK_FACTOR) > thd->variables.group_concat_max_len
4022         && tree->elements_in_tree > 1)
4023       if (repack_tree(thd))
4024         return 1;
4025     el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
4026                     tree->custom_arg);
4027     /* check if there was enough memory to insert the row */
4028     if (!el)
4029       return 1;
4030     tree_len+= row_str_len;
4031   }
4032 
4033   /*
4034     In case of GROUP_CONCAT with DISTINCT or ORDER BY (or both) don't dump the
4035     row to the output buffer here. That will be done in val_str.
4036   */
4037   if (row_eligible && !warning_for_row && (!tree && !distinct))
4038     dump_leaf_key(table->record[0] + table->s->null_bytes, 1, this);
4039 
4040   return 0;
4041 }
4042 
4043 
4044 bool
4045 Item_func_group_concat::fix_fields(THD *thd, Item **ref)
4046 {
4047   uint i;                       /* for loop variable */
4048   DBUG_ASSERT(fixed == 0);
4049 
4050   if (init_sum_func_check(thd))
4051     return TRUE;
4052 
4053   maybe_null= 1;
4054 
4055   /*
4056     Fix fields for select list and ORDER clause
4057   */
4058 
4059   for (i=0 ; i < arg_count ; i++)
4060   {
4061     if (args[i]->fix_fields_if_needed_for_scalar(thd, &args[i]))
4062       return TRUE;
4063     m_with_subquery|= args[i]->with_subquery();
4064     with_param|= args[i]->with_param;
4065     with_window_func|= args[i]->with_window_func;
4066   }
4067 
4068   /* skip charset aggregation for order columns */
4069   if (agg_arg_charsets_for_string_result(collation,
4070                                          args, arg_count - arg_count_order))
4071     return 1;
4072 
4073   result.set_charset(collation.collation);
4074   result_field= 0;
4075   null_value= 1;
4076   max_length= (uint32)MY_MIN(thd->variables.group_concat_max_len
4077                              / collation.collation->mbminlen
4078                              * collation.collation->mbmaxlen, UINT_MAX32);
4079 
4080   uint32 offset;
4081   if (separator->needs_conversion(separator->length(), separator->charset(),
4082                                   collation.collation, &offset))
4083   {
4084     uint32 buflen= collation.collation->mbmaxlen * separator->length();
4085     uint errors, conv_length;
4086     char *buf;
4087     String *new_separator;
4088 
4089     if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
4090         !(new_separator= new(thd->stmt_arena->mem_root)
4091                            String(buf, buflen, collation.collation)))
4092       return TRUE;
4093 
4094     conv_length= copy_and_convert(buf, buflen, collation.collation,
4095                                   separator->ptr(), separator->length(),
4096                                   separator->charset(), &errors);
4097     new_separator->length(conv_length);
4098     separator= new_separator;
4099   }
4100 
4101   if (check_sum_func(thd, ref))
4102     return TRUE;
4103 
4104   fixed= 1;
4105   return FALSE;
4106 }
4107 
4108 
4109 bool Item_func_group_concat::setup(THD *thd)
4110 {
4111   List<Item> list;
4112   SELECT_LEX *select_lex= thd->lex->current_select;
4113   const bool order_or_distinct= MY_TEST(arg_count_order > 0 || distinct);
4114   DBUG_ENTER("Item_func_group_concat::setup");
4115 
4116   /*
4117     Currently setup() can be called twice. Please add
4118     assertion here when this is fixed.
4119   */
4120   if (table || tree)
4121     DBUG_RETURN(FALSE);
4122 
4123   if (!(tmp_table_param= new TMP_TABLE_PARAM))
4124     DBUG_RETURN(TRUE);
4125 
4126   /* Push all not constant fields to the list and create a temp table */
4127   always_null= 0;
4128   for (uint i= 0; i < arg_count_field; i++)
4129   {
4130     Item *item= args[i];
4131     if (list.push_back(item, thd->mem_root))
4132       DBUG_RETURN(TRUE);
4133     if (item->const_item())
4134     {
4135       if (item->is_null())
4136       {
4137         always_null= 1;
4138         DBUG_RETURN(FALSE);
4139       }
4140     }
4141   }
4142 
4143   List<Item> all_fields(list);
4144   /*
4145     Try to find every ORDER expression in the list of GROUP_CONCAT
4146     arguments. If an expression is not found, prepend it to
4147     "all_fields". The resulting field list is used as input to create
4148     tmp table columns.
4149   */
4150   if (arg_count_order)
4151   {
4152     uint n_elems= arg_count_order + all_fields.elements;
4153     ref_pointer_array= static_cast<Item**>(thd->alloc(sizeof(Item*) * n_elems));
4154     if (!ref_pointer_array)
4155       DBUG_RETURN(TRUE);
4156     memcpy(ref_pointer_array, args, arg_count * sizeof(Item*));
4157     if (setup_order(thd, Ref_ptr_array(ref_pointer_array, n_elems),
4158                     context->table_list, list,  all_fields, *order))
4159       DBUG_RETURN(TRUE);
4160     /*
4161       Prepend the field to store the length of the string representation
4162       of this row. Used to detect when the tree goes over group_concat_max_len
4163     */
4164     Item *item= new (thd->mem_root)
4165                     Item_uint(thd, thd->variables.group_concat_max_len);
4166     if (!item || all_fields.push_front(item, thd->mem_root))
4167       DBUG_RETURN(TRUE);
4168   }
4169 
4170   count_field_types(select_lex, tmp_table_param, all_fields, 0);
4171   tmp_table_param->force_copy_fields= force_copy_fields;
4172   tmp_table_param->hidden_field_count= (arg_count_order > 0);
4173   DBUG_ASSERT(table == 0);
4174   if (order_or_distinct)
4175   {
4176     /*
4177       Force the create_tmp_table() to convert BIT columns to INT
4178       as we cannot compare two table records containing BIT fields
4179       stored in the the tree used for distinct/order by.
4180       Moreover we don't even save in the tree record null bits
4181       where BIT fields store parts of their data.
4182     */
4183     List_iterator_fast<Item> li(all_fields);
4184     Item *item;
4185     while ((item= li++))
4186     {
4187       if (item->type() == Item::FIELD_ITEM &&
4188           ((Item_field*) item)->field->type() == FIELD_TYPE_BIT)
4189         item->marker= 4;
4190     }
4191   }
4192 
4193   /*
4194     We have to create a temporary table to get descriptions of fields
4195     (types, sizes and so on).
4196 
4197     Note that in the table, we first have the ORDER BY fields, then the
4198     field list.
4199   */
4200   if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
4201                                 (ORDER*) 0, 0, TRUE,
4202                                 (select_lex->options |
4203                                  thd->variables.option_bits),
4204                                 HA_POS_ERROR, &empty_clex_str)))
4205     DBUG_RETURN(TRUE);
4206   table->file->extra(HA_EXTRA_NO_ROWS);
4207   table->no_rows= 1;
4208 
4209   /**
4210     Initialize blob_storage if GROUP_CONCAT is used
4211     with ORDER BY | DISTINCT and BLOB field count > 0.
4212   */
4213   if (order_or_distinct && table->s->blob_fields)
4214     table->blob_storage= new Blob_mem_storage();
4215 
4216   /*
4217      Need sorting or uniqueness: init tree and choose a function to sort.
4218      Don't reserve space for NULLs: if any of gconcat arguments is NULL,
4219      the row is not added to the result.
4220   */
4221   uint tree_key_length= table->s->reclength - table->s->null_bytes;
4222 
4223   if (arg_count_order)
4224   {
4225     tree= &tree_base;
4226     /*
4227       Create a tree for sorting. The tree is used to sort (according to the
4228       syntax of this function). If there is no ORDER BY clause, we don't
4229       create this tree.
4230     */
4231     init_tree(tree, (size_t)MY_MIN(thd->variables.max_heap_table_size,
4232                                    thd->variables.sortbuff_size/16), 0,
4233               tree_key_length,
4234               group_concat_key_cmp_with_order, NULL, (void*) this,
4235               MYF(MY_THREAD_SPECIFIC));
4236     tree_len= 0;
4237   }
4238 
4239   if (distinct)
4240     unique_filter= new Unique(group_concat_key_cmp_with_distinct,
4241                               (void*)this,
4242                               tree_key_length,
4243                               ram_limitation(thd));
4244   if ((row_limit && row_limit->cmp_type() != INT_RESULT) ||
4245       (offset_limit && offset_limit->cmp_type() != INT_RESULT))
4246   {
4247     my_error(ER_INVALID_VALUE_TO_LIMIT, MYF(0));
4248     DBUG_RETURN(TRUE);
4249   }
4250 
4251   DBUG_RETURN(FALSE);
4252 }
4253 
4254 
4255 /* This is used by rollup to create a separate usable copy of the function */
4256 
4257 void Item_func_group_concat::make_unique()
4258 {
4259   tmp_table_param= 0;
4260   table=0;
4261   original= 0;
4262   force_copy_fields= 1;
4263   tree= 0;
4264 }
4265 
4266 
4267 String* Item_func_group_concat::val_str(String* str)
4268 {
4269   DBUG_ASSERT(fixed == 1);
4270   if (null_value)
4271     return 0;
4272 
4273   if (!result_finalized) // Result yet to be written.
4274   {
4275     if (tree != NULL) // order by
4276       tree_walk(tree, &dump_leaf_key, this, left_root_right);
4277     else if (distinct) // distinct (and no order by).
4278       unique_filter->walk(table, &dump_leaf_key, this);
4279     else if (row_limit && copy_row_limit == (ulonglong)row_limit->val_int())
4280       return &result;
4281     else
4282       DBUG_ASSERT(false); // Can't happen
4283   }
4284 
4285   if (table && table->blob_storage &&
4286       table->blob_storage->is_truncated_value())
4287   {
4288     warning_for_row= true;
4289     push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
4290                         ER_CUT_VALUE_GROUP_CONCAT, ER(ER_CUT_VALUE_GROUP_CONCAT),
4291                         row_count);
4292   }
4293 
4294   return &result;
4295 }
4296 
4297 
4298 void Item_func_group_concat::print(String *str, enum_query_type query_type)
4299 {
4300   str->append(STRING_WITH_LEN("group_concat("));
4301   if (distinct)
4302     str->append(STRING_WITH_LEN("distinct "));
4303   for (uint i= 0; i < arg_count_field; i++)
4304   {
4305     if (i)
4306       str->append(',');
4307     orig_args[i]->print(str, query_type);
4308   }
4309   if (arg_count_order)
4310   {
4311     str->append(STRING_WITH_LEN(" order by "));
4312     for (uint i= 0 ; i < arg_count_order ; i++)
4313     {
4314       if (i)
4315         str->append(',');
4316       orig_args[i + arg_count_field]->print(str, query_type);
4317       if (order[i]->direction == ORDER::ORDER_ASC)
4318         str->append(STRING_WITH_LEN(" ASC"));
4319      else
4320         str->append(STRING_WITH_LEN(" DESC"));
4321     }
4322   }
4323   str->append(STRING_WITH_LEN(" separator \'"));
4324   str->append_for_single_quote(separator->ptr(), separator->length());
4325   str->append(STRING_WITH_LEN("\'"));
4326 
4327   if (limit_clause)
4328   {
4329     str->append(STRING_WITH_LEN(" limit "));
4330     if (offset_limit)
4331     {
4332       offset_limit->print(str, query_type);
4333       str->append(',');
4334     }
4335     row_limit->print(str, query_type);
4336   }
4337   str->append(STRING_WITH_LEN(")"));
4338 }
4339 
4340 
4341 Item_func_group_concat::~Item_func_group_concat()
4342 {
4343   if (!original && unique_filter)
4344     delete unique_filter;
4345 }
4346