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
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 
ram_limitation(THD * thd)43 size_t Item_sum::ram_limitation(THD *thd)
44 {
45   return MY_MAX(1024,
46            (size_t)MY_MIN(thd->variables.tmp_memory_table_size,
47                           thd->variables.max_heap_table_size));
48 }
49 
50 
51 /**
52   Prepare an aggregate function item for checking context conditions.
53 
54     The function initializes the members of the Item_sum object created
55     for a set function that are used to check validity of the set function
56     occurrence.
57     If the set function is not allowed in any subquery where it occurs
58     an error is reported immediately.
59 
60   @param thd      reference to the thread context info
61 
62   @note
63     This function is to be called for any item created for a set function
64     object when the traversal of trees built for expressions used in the query
65     is performed at the phase of context analysis. This function is to
66     be invoked at the descent of this traversal.
67   @retval
68     TRUE   if an error is reported
69   @retval
70     FALSE  otherwise
71 */
72 
init_sum_func_check(THD * thd)73 bool Item_sum::init_sum_func_check(THD *thd)
74 {
75   SELECT_LEX *curr_sel= thd->lex->current_select;
76   if (curr_sel && curr_sel->name_visibility_map.is_clear_all())
77   {
78     for (SELECT_LEX *sl= curr_sel; sl; sl= sl->context.outer_select())
79     {
80       curr_sel->name_visibility_map.set_bit(sl->nest_level);
81     }
82   }
83   if (!curr_sel ||
84       !(thd->lex->allow_sum_func.is_overlapping(curr_sel->name_visibility_map)))
85   {
86     my_message(ER_INVALID_GROUP_FUNC_USE, ER_THD(thd, ER_INVALID_GROUP_FUNC_USE),
87                MYF(0));
88     return TRUE;
89   }
90   /* Set a reference to the nesting set function if there is  any */
91   in_sum_func= thd->lex->in_sum_func;
92   /* Save a pointer to object to be used in items for nested set functions */
93   thd->lex->in_sum_func= this;
94   nest_level= thd->lex->current_select->nest_level;
95   ref_by= 0;
96   aggr_level= -1;
97   aggr_sel= NULL;
98   max_arg_level= -1;
99   max_sum_func_level= -1;
100   outer_fields.empty();
101   return FALSE;
102 }
103 
104 /**
105   Check constraints imposed on a usage of a set function.
106 
107     The method verifies whether context conditions imposed on a usage
108     of any set function are met for this occurrence.
109 
110     The function first checks if we are using any window functions as
111     arguments to the set function. In that case it returns an error.
112 
113     Afterwards, it checks whether the set function occurs in the position where it
114     can be aggregated and, when it happens to occur in argument of another
115     set function, the method checks that these two functions are aggregated in
116     different subqueries.
117     If the context conditions are not met the method reports an error.
118     If the set function is aggregated in some outer subquery the method
119     adds it to the chain of items for such set functions that is attached
120     to the the st_select_lex structure for this subquery.
121 
122     A number of designated members of the object are used to check the
123     conditions. They are specified in the comment before the Item_sum
124     class declaration.
125     Additionally a bitmap variable called allow_sum_func is employed.
126     It is included into the thd->lex structure.
127     The bitmap contains 1 at n-th position if the set function happens
128     to occur under a construct of the n-th level subquery where usage
129     of set functions are allowed (i.e either in the SELECT list or
130     in the HAVING clause of the corresponding subquery)
131     Consider the query:
132     @code
133        SELECT SUM(t1.b) FROM t1 GROUP BY t1.a
134          HAVING t1.a IN (SELECT t2.c FROM t2 WHERE AVG(t1.b) > 20) AND
135                 t1.a > (SELECT MIN(t2.d) FROM t2);
136     @endcode
137     allow_sum_func will contain:
138     - for SUM(t1.b) - 1 at the first position
139     - for AVG(t1.b) - 1 at the first position, 0 at the second position
140     - for MIN(t2.d) - 1 at the first position, 1 at the second position.
141 
142   @param thd  reference to the thread context info
143   @param ref  location of the pointer to this item in the embedding expression
144 
145   @note
146     This function is to be called for any item created for a set function
147     object when the traversal of trees built for expressions used in the query
148     is performed at the phase of context analysis. This function is to
149     be invoked at the ascent of this traversal.
150 
151   @retval
152     TRUE   if an error is reported
153   @retval
154     FALSE  otherwise
155 */
156 
check_sum_func(THD * thd,Item ** ref)157 bool Item_sum::check_sum_func(THD *thd, Item **ref)
158 {
159   SELECT_LEX *curr_sel= thd->lex->current_select;
160   nesting_map allow_sum_func(thd->lex->allow_sum_func);
161   allow_sum_func.intersect(curr_sel->name_visibility_map);
162   bool invalid= FALSE;
163   // should be set already
164   DBUG_ASSERT(!curr_sel->name_visibility_map.is_clear_all());
165 
166   /*
167      Window functions can not be used as arguments to sum functions.
168      Aggregation happes before window function computation, so there
169      are no values to aggregate over.
170   */
171   if (with_window_func)
172   {
173     my_message(ER_SUM_FUNC_WITH_WINDOW_FUNC_AS_ARG,
174                ER_THD(thd, ER_SUM_FUNC_WITH_WINDOW_FUNC_AS_ARG),
175                MYF(0));
176     return TRUE;
177   }
178 
179   if (window_func_sum_expr_flag)
180     return false;
181   /*
182     The value of max_arg_level is updated if an argument of the set function
183     contains a column reference resolved  against a subquery whose level is
184     greater than the current value of max_arg_level.
185     max_arg_level cannot be greater than nest level.
186     nest level is always >= 0
187   */
188   if (nest_level == max_arg_level)
189   {
190     /*
191       The function must be aggregated in the current subquery,
192       If it is there under a construct where it is not allowed
193       we report an error.
194     */
195     invalid= !(allow_sum_func.is_set(max_arg_level));
196   }
197   else if (max_arg_level >= 0 ||
198            !(allow_sum_func.is_set(nest_level)))
199   {
200     /*
201       The set function can be aggregated only in outer subqueries.
202       Try to find a subquery where it can be aggregated;
203       If we fail to find such a subquery report an error.
204     */
205     if (register_sum_func(thd, ref))
206       return TRUE;
207     invalid= aggr_level < 0 &&
208              !(allow_sum_func.is_set(nest_level));
209     if (!invalid && thd->variables.sql_mode & MODE_ANSI)
210       invalid= aggr_level < 0 && max_arg_level < nest_level;
211   }
212   if (!invalid && aggr_level < 0)
213   {
214     aggr_level= nest_level;
215     aggr_sel= curr_sel;
216   }
217   /*
218     By this moment we either found a subquery where the set function is
219     to be aggregated  and assigned a value that is  >= 0 to aggr_level,
220     or set the value of 'invalid' to TRUE to report later an error.
221   */
222   /*
223     Additionally we have to check whether possible nested set functions
224     are acceptable here: they are not, if the level of aggregation of
225     some of them is less than aggr_level.
226   */
227   if (!invalid)
228     invalid= aggr_level <= max_sum_func_level;
229   if (invalid)
230   {
231     my_message(ER_INVALID_GROUP_FUNC_USE,
232                ER_THD(thd, ER_INVALID_GROUP_FUNC_USE),
233                MYF(0));
234     return TRUE;
235   }
236 
237   if (in_sum_func)
238   {
239     /*
240       If the set function is nested adjust the value of
241       max_sum_func_level for the nesting set function.
242       We take into account only enclosed set functions that are to be
243       aggregated on the same level or above of the nest level of
244       the enclosing set function.
245       But we must always pass up the max_sum_func_level because it is
246       the maximum nested level of all directly and indirectly enclosed
247       set functions. We must do that even for set functions that are
248       aggregated inside of their enclosing set function's nest level
249       because the enclosing function may contain another enclosing
250       function that is to be aggregated outside or on the same level
251       as its parent's nest level.
252     */
253     if (in_sum_func->nest_level >= aggr_level)
254       set_if_bigger(in_sum_func->max_sum_func_level, aggr_level);
255     set_if_bigger(in_sum_func->max_sum_func_level, max_sum_func_level);
256   }
257 
258   /*
259     Check that non-aggregated fields and sum functions aren't mixed in the
260     same select in the ONLY_FULL_GROUP_BY mode.
261   */
262   if (outer_fields.elements)
263   {
264     Item_field *field;
265     /*
266       Here we compare the nesting level of the select to which an outer field
267       belongs to with the aggregation level of the sum function. All fields in
268       the outer_fields list are checked.
269 
270       If the nesting level is equal to the aggregation level then the field is
271         aggregated by this sum function.
272       If the nesting level is less than the aggregation level then the field
273         belongs to an outer select. In this case if there is an embedding sum
274         function add current field to functions outer_fields list. If there is
275         no embedding function then the current field treated as non aggregated
276         and the select it belongs to is marked accordingly.
277       If the nesting level is greater than the aggregation level then it means
278         that this field was added by an inner sum function.
279         Consider an example:
280 
281           select avg ( <-- we are here, checking outer.f1
282             select (
283               select sum(outer.f1 + inner.f1) from inner
284             ) from outer)
285           from most_outer;
286 
287         In this case we check that no aggregate functions are used in the
288         select the field belongs to. If there are some then an error is
289         raised.
290     */
291     List_iterator<Item_field> of(outer_fields);
292     while ((field= of++))
293     {
294       SELECT_LEX *sel= field->field->table->pos_in_table_list->select_lex;
295       if (sel->nest_level < aggr_level)
296       {
297         if (in_sum_func)
298         {
299           /*
300             Let upper function decide whether this field is a non
301             aggregated one.
302           */
303           in_sum_func->outer_fields.push_back(field, thd->mem_root);
304         }
305         else
306           sel->set_non_agg_field_used(true);
307       }
308       if (sel->nest_level > aggr_level &&
309           (sel->agg_func_used()) &&
310           !sel->group_list.elements)
311       {
312         my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
313                    ER_THD(thd, ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
314         return TRUE;
315       }
316     }
317   }
318   aggr_sel->set_agg_func_used(true);
319   if (sum_func() == SP_AGGREGATE_FUNC)
320     aggr_sel->set_custom_agg_func_used(true);
321   update_used_tables();
322   thd->lex->in_sum_func= in_sum_func;
323   return FALSE;
324 }
325 
326 /**
327   Attach a set function to the subquery where it must be aggregated.
328 
329     The function looks for an outer subquery where the set function must be
330     aggregated. If it finds such a subquery then aggr_level is set to
331     the nest level of this subquery and the item for the set function
332     is added to the list of set functions used in nested subqueries
333     inner_sum_func_list defined for each subquery. When the item is placed
334     there the field 'ref_by' is set to ref.
335 
336   @note
337     Now we 'register' only set functions that are aggregated in outer
338     subqueries. Actually it makes sense to link all set function for
339     a subquery in one chain. It would simplify the process of 'splitting'
340     for set functions.
341 
342   @param thd  reference to the thread context info
343   @param ref  location of the pointer to this item in the embedding expression
344 
345   @retval
346     FALSE  if the executes without failures (currently always)
347   @retval
348     TRUE   otherwise
349 */
350 
register_sum_func(THD * thd,Item ** ref)351 bool Item_sum::register_sum_func(THD *thd, Item **ref)
352 {
353   SELECT_LEX *sl;
354   nesting_map allow_sum_func= thd->lex->allow_sum_func;
355   for (sl= thd->lex->current_select->context.outer_select() ;
356        sl && sl->nest_level > max_arg_level;
357        sl= sl->context.outer_select())
358   {
359     if (aggr_level < 0 &&
360         (allow_sum_func.is_set(sl->nest_level)))
361     {
362       /* Found the most nested subquery where the function can be aggregated */
363       aggr_level= sl->nest_level;
364       aggr_sel= sl;
365     }
366   }
367   if (sl && (allow_sum_func.is_set(sl->nest_level)))
368   {
369     /*
370       We reached the subquery of level max_arg_level and checked
371       that the function can be aggregated here.
372       The set function will be aggregated in this subquery.
373     */
374     aggr_level= sl->nest_level;
375     aggr_sel= sl;
376 
377   }
378   if (aggr_level >= 0)
379   {
380     ref_by= ref;
381     /* Add the object to the list of registered objects assigned to aggr_sel */
382     if (!aggr_sel->inner_sum_func_list)
383       next= this;
384     else
385     {
386       next= aggr_sel->inner_sum_func_list->next;
387       aggr_sel->inner_sum_func_list->next= this;
388     }
389     aggr_sel->inner_sum_func_list= this;
390     aggr_sel->with_sum_func= 1;
391 
392     /*
393       Mark Item_subselect(s) as containing aggregate function all the way up
394       to aggregate function's calculation context.
395       Note that we must not mark the Item of calculation context itself
396       because with_sum_func on the calculation context st_select_lex is
397       already set above.
398 
399       with_sum_func being set for an Item means that this Item refers
400       (somewhere in it, e.g. one of its arguments if it's a function) directly
401       or through intermediate items to an aggregate function that is calculated
402       in a context "outside" of the Item (e.g. in the current or outer select).
403 
404       with_sum_func being set for an st_select_lex means that this st_select_lex
405       has aggregate functions directly referenced (i.e. not through a sub-select).
406     */
407     for (sl= thd->lex->current_select;
408          sl && sl != aggr_sel && sl->master_unit()->item;
409          sl= sl->master_unit()->outer_select() )
410       sl->master_unit()->item->get_with_sum_func_cache()->set_with_sum_func();
411   }
412   thd->lex->current_select->mark_as_dependent(thd, aggr_sel, NULL);
413 
414   if ((thd->lex->describe & DESCRIBE_EXTENDED) && aggr_sel)
415   {
416     push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
417                         ER_WARN_AGGFUNC_DEPENDENCE,
418                         ER_THD(thd, ER_WARN_AGGFUNC_DEPENDENCE),
419                         func_name(),
420                         thd->lex->current_select->select_number,
421                         aggr_sel->select_number);
422   }
423   return FALSE;
424 }
425 
426 
collect_outer_ref_processor(void * param)427 bool Item_sum::collect_outer_ref_processor(void *param)
428 {
429   Collect_deps_prm *prm= (Collect_deps_prm *)param;
430   SELECT_LEX *ds;
431   if ((ds= depended_from()) &&
432       ds->nest_level_base == prm->nest_level_base &&
433       ds->nest_level < prm->nest_level)
434   {
435     if (prm->collect)
436       prm->parameters->add_unique(this, &cmp_items);
437     else
438       prm->count++;
439   }
440   return FALSE;
441 }
442 
443 
Item_sum(THD * thd,List<Item> & list)444 Item_sum::Item_sum(THD *thd, List<Item> &list): Item_func_or_sum(thd, list)
445 {
446   if (!(orig_args= (Item **) thd->alloc(sizeof(Item *) * arg_count)))
447   {
448     args= NULL;
449   }
450   mark_as_sum_func();
451   init_aggregator();
452   list.empty();					// Fields are used
453 }
454 
455 
456 /**
457   Constructor used in processing select with temporary tebles.
458 */
459 
Item_sum(THD * thd,Item_sum * item)460 Item_sum::Item_sum(THD *thd, Item_sum *item):
461   Item_func_or_sum(thd, item),
462   aggr_sel(item->aggr_sel),
463   nest_level(item->nest_level), aggr_level(item->aggr_level),
464   quick_group(item->quick_group),
465   orig_args(NULL)
466 {
467   if (arg_count <= 2)
468   {
469     orig_args=tmp_orig_args;
470   }
471   else
472   {
473     if (!(orig_args= (Item**) thd->alloc(sizeof(Item*)*arg_count)))
474       return;
475   }
476   if (arg_count)
477     memcpy(orig_args, item->orig_args, sizeof(Item*)*arg_count);
478   init_aggregator();
479   with_distinct= item->with_distinct;
480   if (item->aggr)
481     set_aggregator(item->aggr->Aggrtype());
482 }
483 
484 
mark_as_sum_func()485 void Item_sum::mark_as_sum_func()
486 {
487   SELECT_LEX *cur_select= current_thd->lex->current_select;
488   cur_select->n_sum_items++;
489   cur_select->with_sum_func= 1;
490   const_item_cache= false;
491   with_field= 0;
492   window_func_sum_expr_flag= false;
493 }
494 
495 
print(String * str,enum_query_type query_type)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;
500   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.
505   */
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);
513   }
514   str->append(')');
515 }
516 
fix_num_length_and_dec()517 void Item_sum::fix_num_length_and_dec()
518 {
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 
get_tmp_table_item(THD * thd)525 Item *Item_sum::get_tmp_table_item(THD *thd)
526 {
527   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       {
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 
update_used_tables()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     }
557     /*
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 
set_arg(uint i,THD * thd,Item * new_val)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 
581 
set_aggregator(Aggregator::Aggregator_type aggregator)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 
cleanup()613 void Item_sum::cleanup()
614 {
615   if (aggr)
616   {
617     delete aggr;
618     aggr= NULL;
619   }
620   Item_result_field::cleanup();
621   const_item_cache= false;
622 }
623 
result_item(THD * thd,Field * field)624 Item *Item_sum::result_item(THD *thd, Field *field)
625 {
626   return new (thd->mem_root) Item_field(thd, field);
627 }
628 
check_vcol_func_processor(void * arg)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 }
635 
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 
simple_str_key_cmp(void * arg,uchar * key1,uchar * key2)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 }
657 
658 
659 C_MODE_START
660 
count_distinct_walk(void * elem,element_count count,void * arg)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 
669 
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 
composite_key_cmp(void * arg,uchar * key1,uchar * key2)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 
707 C_MODE_START
708 
709 /* Declarations for auxiliary C-callbacks */
710 
simple_raw_key_cmp(void * arg,const void * key1,const void * key2)711 int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
712 {
713     return memcmp(key1, key2, *(uint *) arg);
714 }
715 
716 
item_sum_distinct_walk_for_count(void * element,element_count num_of_dups,void * item)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 
item_sum_distinct_walk(void * element,element_count num_of_dups,void * item)725 static int item_sum_distinct_walk(void *element, element_count num_of_dups,
726                                   void *item)
727 {
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 
738   @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 
setup(THD * thd)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)->is_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 
clear()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 
add()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 
endup()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 *
val_str(String * str)1098 Item_sum_int::val_str(String *str)
1099 {
1100   return val_string_from_int(str);
1101 }
1102 
1103 
val_decimal(my_decimal * decimal_value)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
fix_fields(THD * thd,Item ** ref)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
fix_fields(THD * thd,Item ** ref)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 
fix_length_and_dec_generic()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 */
fix_length_and_dec_numeric(const Type_handler * handler)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 */
fix_length_and_dec_string()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 
fix_length_and_dec()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 
setup_hybrid(THD * thd,Item * item,Item * value_arg)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 
create_tmp_field(MEM_ROOT * root,bool group,TABLE * table)1286 Field *Item_sum_min_max::create_tmp_field(MEM_ROOT *root,
1287                                           bool group, TABLE *table)
1288 {
1289   DBUG_ENTER("Item_sum_min_max::create_tmp_field");
1290 
1291   if (args[0]->type() == Item::FIELD_ITEM)
1292   {
1293     Field *field= ((Item_field*) args[0])->field;
1294     if ((field= field->create_tmp_field(root, table, true)))
1295     {
1296       DBUG_ASSERT((field->flags & NOT_NULL_FLAG) == 0);
1297       field->field_name= name;
1298     }
1299     DBUG_RETURN(field);
1300   }
1301   DBUG_RETURN(tmp_table_field_from_field_type(root, table));
1302 }
1303 
1304 /***********************************************************************
1305 ** Item_sum_sp class
1306 ***********************************************************************/
1307 
Item_sum_sp(THD * thd,Name_resolution_context * context_arg,sp_name * name_arg,sp_head * sp,List<Item> & list)1308 Item_sum_sp::Item_sum_sp(THD *thd, Name_resolution_context *context_arg,
1309                            sp_name *name_arg, sp_head *sp, List<Item> &list)
1310   :Item_sum(thd, list), Item_sp(thd, context_arg, name_arg)
1311 {
1312   maybe_null= 1;
1313   quick_group= 0;
1314   m_sp= sp;
1315 }
1316 
Item_sum_sp(THD * thd,Name_resolution_context * context_arg,sp_name * name_arg,sp_head * sp)1317 Item_sum_sp::Item_sum_sp(THD *thd, Name_resolution_context *context_arg,
1318                            sp_name *name_arg, sp_head *sp)
1319   :Item_sum(thd), Item_sp(thd, context_arg, name_arg)
1320 {
1321   maybe_null= 1;
1322   quick_group= 0;
1323   m_sp= sp;
1324 }
1325 
Item_sum_sp(THD * thd,Item_sum_sp * item)1326 Item_sum_sp::Item_sum_sp(THD *thd, Item_sum_sp *item):
1327              Item_sum(thd, item), Item_sp(thd, item)
1328 {
1329   maybe_null= item->maybe_null;
1330   quick_group= item->quick_group;
1331 }
1332 
1333 bool
fix_fields(THD * thd,Item ** ref)1334 Item_sum_sp::fix_fields(THD *thd, Item **ref)
1335 {
1336   DBUG_ASSERT(fixed == 0);
1337   if (init_sum_func_check(thd))
1338     return TRUE;
1339   decimals= 0;
1340 
1341   m_sp= m_sp ? m_sp : sp_handler_function.sp_find_routine(thd, m_name, true);
1342 
1343   if (!m_sp)
1344   {
1345     my_missing_function_error(m_name->m_name, ErrConvDQName(m_name).ptr());
1346     process_error(thd);
1347     return TRUE;
1348   }
1349 
1350   if (init_result_field(thd, max_length, maybe_null, &null_value, &name))
1351     return TRUE;
1352 
1353   for (uint i= 0 ; i < arg_count ; i++)
1354   {
1355     if (args[i]->fix_fields_if_needed_for_scalar(thd, &args[i]))
1356       return TRUE;
1357     set_if_bigger(decimals, args[i]->decimals);
1358     m_with_subquery|= args[i]->with_subquery();
1359     with_window_func|= args[i]->with_window_func;
1360   }
1361   result_field= NULL;
1362   max_length= float_length(decimals);
1363   null_value= 1;
1364   if (fix_length_and_dec())
1365     return TRUE;
1366 
1367   if (check_sum_func(thd, ref))
1368     return TRUE;
1369 
1370   if (arg_count)
1371     memcpy(orig_args, args, sizeof(Item *) * arg_count);
1372   fixed= 1;
1373   return FALSE;
1374 }
1375 
1376 /**
1377   Execute function to store value in result field.
1378   This is called when we need the value to be returned for the function.
1379   Here we send a signal in form of the server status that all rows have been
1380   fetched and now we have to exit from the function with the return value.
1381   @return Function returns error status.
1382   @retval FALSE on success.
1383   @retval TRUE if an error occurred.
1384 */
1385 
1386 bool
execute()1387 Item_sum_sp::execute()
1388 {
1389   THD *thd= current_thd;
1390   bool res;
1391   uint old_server_status= thd->server_status;
1392 
1393   /*
1394     We set server status so we can send a signal to exit from the
1395     function with the return value.
1396   */
1397 
1398   thd->server_status|= SERVER_STATUS_LAST_ROW_SENT;
1399   res= Item_sp::execute(thd, &null_value, args, arg_count);
1400   thd->server_status= old_server_status;
1401   return res;
1402 }
1403 
1404 /**
1405   Handles the aggregation of the values.
1406   @note: See class description for more details on how and why this is done.
1407   @return The error state.
1408   @retval FALSE on success.
1409   @retval TRUE if an error occurred.
1410 */
1411 
1412 bool
add()1413 Item_sum_sp::add()
1414 {
1415   return execute_impl(current_thd, args, arg_count);
1416 }
1417 
1418 
1419 void
clear()1420 Item_sum_sp::clear()
1421 {
1422   delete func_ctx;
1423   func_ctx= NULL;
1424   sp_query_arena->free_items();
1425   free_root(&sp_mem_root, MYF(0));
1426 }
1427 
type_handler() const1428 const Type_handler *Item_sum_sp::type_handler() const
1429 {
1430   DBUG_ENTER("Item_sum_sp::type_handler");
1431   DBUG_PRINT("info", ("m_sp = %p", (void *) m_sp));
1432   DBUG_ASSERT(sp_result_field);
1433   // This converts ENUM/SET to STRING
1434   const Type_handler *handler= sp_result_field->type_handler();
1435   DBUG_RETURN(handler->type_handler_for_item_field());
1436 }
1437 
1438 void
cleanup()1439 Item_sum_sp::cleanup()
1440 {
1441   Item_sp::cleanup();
1442   Item_sum::cleanup();
1443 }
1444 
1445 /**
1446   Initialize local members with values from the Field interface.
1447   @note called from Item::fix_fields.
1448 */
1449 
1450 bool
fix_length_and_dec()1451 Item_sum_sp::fix_length_and_dec()
1452 {
1453   DBUG_ENTER("Item_sum_sp::fix_length_and_dec");
1454   DBUG_ASSERT(sp_result_field);
1455   Type_std_attributes::set(sp_result_field->type_std_attributes());
1456   bool res= Item_sum::fix_length_and_dec();
1457   DBUG_RETURN(res);
1458 }
1459 
1460 const char *
func_name() const1461 Item_sum_sp::func_name() const
1462 {
1463   THD *thd= current_thd;
1464   return Item_sp::func_name(thd);
1465 }
1466 
copy_or_same(THD * thd)1467 Item* Item_sum_sp::copy_or_same(THD *thd)
1468 {
1469   Item_sum_sp *copy_item= new (thd->mem_root) Item_sum_sp(thd, this);
1470   copy_item->init_result_field(thd, max_length, maybe_null,
1471                                &copy_item->null_value, &copy_item->name);
1472   return copy_item;
1473 }
1474 
1475 /***********************************************************************
1476 ** reset and add of sum_func
1477 ***********************************************************************/
1478 
1479 /**
1480   @todo
1481   check if the following assignments are really needed
1482 */
Item_sum_sum(THD * thd,Item_sum_sum * item)1483 Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item)
1484   :Item_sum_num(thd, item),
1485    Type_handler_hybrid_field_type(item),
1486    direct_added(FALSE), direct_reseted_field(FALSE),
1487    curr_dec_buff(item->curr_dec_buff),
1488    count(item->count)
1489 {
1490   /* TODO: check if the following assignments are really needed */
1491   if (result_type() == DECIMAL_RESULT)
1492   {
1493     my_decimal2decimal(item->dec_buffs, dec_buffs);
1494     my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
1495   }
1496   else
1497     sum= item->sum;
1498 }
1499 
copy_or_same(THD * thd)1500 Item *Item_sum_sum::copy_or_same(THD* thd)
1501 {
1502   return new (thd->mem_root) Item_sum_sum(thd, this);
1503 }
1504 
1505 
cleanup()1506 void Item_sum_sum::cleanup()
1507 {
1508   DBUG_ENTER("Item_sum_sum::cleanup");
1509   direct_added= direct_reseted_field= FALSE;
1510   Item_sum_num::cleanup();
1511   DBUG_VOID_RETURN;
1512 }
1513 
1514 
clear()1515 void Item_sum_sum::clear()
1516 {
1517   DBUG_ENTER("Item_sum_sum::clear");
1518   null_value=1;
1519   count= 0;
1520   if (result_type() == DECIMAL_RESULT)
1521   {
1522     curr_dec_buff= 0;
1523     my_decimal_set_zero(dec_buffs);
1524   }
1525   else
1526     sum= 0.0;
1527   DBUG_VOID_RETURN;
1528 }
1529 
1530 
fix_length_and_dec_double()1531 void Item_sum_sum::fix_length_and_dec_double()
1532 {
1533   set_handler(&type_handler_double); // Change FLOAT to DOUBLE
1534   decimals= args[0]->decimals;
1535   sum= 0.0;
1536 }
1537 
1538 
fix_length_and_dec_decimal()1539 void Item_sum_sum::fix_length_and_dec_decimal()
1540 {
1541   set_handler(&type_handler_newdecimal); // Change temporal to new DECIMAL
1542   decimals= args[0]->decimals;
1543   /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
1544   int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
1545   max_length= my_decimal_precision_to_length_no_truncation(precision,
1546                                                            decimals,
1547                                                            unsigned_flag);
1548   curr_dec_buff= 0;
1549   my_decimal_set_zero(dec_buffs);
1550 }
1551 
1552 
fix_length_and_dec()1553 bool Item_sum_sum::fix_length_and_dec()
1554 {
1555   DBUG_ENTER("Item_sum_sum::fix_length_and_dec");
1556   maybe_null=null_value=1;
1557   if (args[0]->cast_to_int_type_handler()->
1558       Item_sum_sum_fix_length_and_dec(this))
1559     DBUG_RETURN(TRUE);
1560   DBUG_PRINT("info", ("Type: %s (%d, %d)", type_handler()->name().ptr(),
1561                       max_length, (int) decimals));
1562   DBUG_RETURN(FALSE);
1563 }
1564 
1565 
direct_add(my_decimal * add_sum_decimal)1566 void Item_sum_sum::direct_add(my_decimal *add_sum_decimal)
1567 {
1568   DBUG_ENTER("Item_sum_sum::direct_add");
1569   DBUG_PRINT("info", ("add_sum_decimal: %p", add_sum_decimal));
1570   direct_added= TRUE;
1571   direct_reseted_field= FALSE;
1572   if (add_sum_decimal)
1573   {
1574     direct_sum_is_null= FALSE;
1575     direct_sum_decimal= *add_sum_decimal;
1576   }
1577   else
1578   {
1579     direct_sum_is_null= TRUE;
1580     direct_sum_decimal= decimal_zero;
1581   }
1582   DBUG_VOID_RETURN;
1583 }
1584 
1585 
direct_add(double add_sum_real,bool add_sum_is_null)1586 void Item_sum_sum::direct_add(double add_sum_real, bool add_sum_is_null)
1587 {
1588   DBUG_ENTER("Item_sum_sum::direct_add");
1589   DBUG_PRINT("info", ("add_sum_real: %f", add_sum_real));
1590   direct_added= TRUE;
1591   direct_reseted_field= FALSE;
1592   direct_sum_is_null= add_sum_is_null;
1593   direct_sum_real= add_sum_real;
1594   DBUG_VOID_RETURN;
1595 }
1596 
1597 
add()1598 bool Item_sum_sum::add()
1599 {
1600   DBUG_ENTER("Item_sum_sum::add");
1601   add_helper(false);
1602   DBUG_RETURN(0);
1603 }
1604 
add_helper(bool perform_removal)1605 void Item_sum_sum::add_helper(bool perform_removal)
1606 {
1607   DBUG_ENTER("Item_sum_sum::add_helper");
1608 
1609   if (result_type() == DECIMAL_RESULT)
1610   {
1611     if (unlikely(direct_added))
1612     {
1613       /* Add value stored by Item_sum_sum::direct_add */
1614       DBUG_ASSERT(!perform_removal);
1615 
1616       direct_added= FALSE;
1617       if (likely(!direct_sum_is_null))
1618       {
1619         my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
1620                        &direct_sum_decimal, dec_buffs + curr_dec_buff);
1621         curr_dec_buff^= 1;
1622         null_value= 0;
1623       }
1624     }
1625     else
1626     {
1627       direct_reseted_field= FALSE;
1628       my_decimal value;
1629       const my_decimal *val= aggr->arg_val_decimal(&value);
1630       if (!aggr->arg_is_null(true))
1631       {
1632         if (perform_removal)
1633         {
1634           if (count > 0)
1635           {
1636             my_decimal_sub(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff ^ 1),
1637               dec_buffs + curr_dec_buff, val);
1638             count--;
1639           }
1640           else
1641             DBUG_VOID_RETURN;
1642         }
1643         else
1644         {
1645           count++;
1646           my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff ^ 1),
1647             val, dec_buffs + curr_dec_buff);
1648         }
1649         curr_dec_buff^= 1;
1650         null_value= (count > 0) ? 0 : 1;
1651       }
1652     }
1653   }
1654   else
1655   {
1656     if (unlikely(direct_added))
1657     {
1658       /* Add value stored by Item_sum_sum::direct_add */
1659       DBUG_ASSERT(!perform_removal);
1660 
1661       direct_added= FALSE;
1662       if (!direct_sum_is_null)
1663       {
1664         sum+= direct_sum_real;
1665         null_value= 0;
1666       }
1667     }
1668     else
1669     {
1670       direct_reseted_field= FALSE;
1671       if (perform_removal && count > 0)
1672         sum-= aggr->arg_val_real();
1673       else
1674         sum+= aggr->arg_val_real();
1675       if (!aggr->arg_is_null(true))
1676       {
1677         if (perform_removal)
1678         {
1679           if (count > 0)
1680           {
1681             count--;
1682           }
1683         }
1684         else
1685           count++;
1686 
1687         null_value= (count > 0) ? 0 : 1;
1688       }
1689     }
1690   }
1691   DBUG_VOID_RETURN;
1692 }
1693 
1694 
val_int()1695 longlong Item_sum_sum::val_int()
1696 {
1697   DBUG_ASSERT(fixed == 1);
1698   if (aggr)
1699     aggr->endup();
1700   if (result_type() == DECIMAL_RESULT)
1701     return dec_buffs[curr_dec_buff].to_longlong(unsigned_flag);
1702   return val_int_from_real();
1703 }
1704 
1705 
val_real()1706 double Item_sum_sum::val_real()
1707 {
1708   DBUG_ASSERT(fixed == 1);
1709   if (aggr)
1710     aggr->endup();
1711   if (result_type() == DECIMAL_RESULT)
1712     sum= dec_buffs[curr_dec_buff].to_double();
1713   return sum;
1714 }
1715 
1716 
val_str(String * str)1717 String *Item_sum_sum::val_str(String *str)
1718 {
1719   if (aggr)
1720     aggr->endup();
1721   if (result_type() == DECIMAL_RESULT)
1722     return VDec(this).to_string_round(str, decimals);
1723   return val_string_from_real(str);
1724 }
1725 
1726 
val_decimal(my_decimal * val)1727 my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
1728 {
1729   if (aggr)
1730     aggr->endup();
1731   if (result_type() == DECIMAL_RESULT)
1732     return null_value ? NULL : (dec_buffs + curr_dec_buff);
1733   return val_decimal_from_real(val);
1734 }
1735 
remove()1736 void Item_sum_sum::remove()
1737 {
1738   DBUG_ENTER("Item_sum_sum::remove");
1739   add_helper(true);
1740   DBUG_VOID_RETURN;
1741 }
1742 
1743 /**
1744   Aggregate a distinct row from the distinct hash table.
1745 
1746   Called for each row into the hash table 'Aggregator_distinct::table'.
1747   Includes the current distinct row into the calculation of the
1748   aggregate value. Uses the Field classes to get the value from the row.
1749   This function is used for AVG/SUM(DISTINCT). For COUNT(DISTINCT)
1750   it's called only when there are no blob arguments and the data don't
1751   fit into memory (so Unique makes persisted trees on disk).
1752 
1753   @param element     pointer to the row data.
1754 
1755   @return status
1756     @retval FALSE     success
1757     @retval TRUE      failure
1758 */
1759 
unique_walk_function(void * element)1760 bool Aggregator_distinct::unique_walk_function(void *element)
1761 {
1762   memcpy(table->field[0]->ptr, element, tree_key_length);
1763   item_sum->add();
1764   return 0;
1765 }
1766 
1767 
1768 /*
1769   A variant of unique_walk_function() that is to be used with Item_sum_count.
1770 
1771   COUNT is a special aggregate function: it doesn't need the values, it only
1772   needs to count them. COUNT needs to know the values are not NULLs, but NULL
1773   values are not put into the Unique, so we don't need to check for NULLs here.
1774 */
1775 
unique_walk_function_for_count(void * element)1776 bool Aggregator_distinct::unique_walk_function_for_count(void *element)
1777 {
1778   Item_sum_count *sum= (Item_sum_count *)item_sum;
1779   sum->count++;
1780   return 0;
1781 }
1782 
1783 
~Aggregator_distinct()1784 Aggregator_distinct::~Aggregator_distinct()
1785 {
1786   if (tree)
1787   {
1788     delete tree;
1789     tree= NULL;
1790   }
1791   if (table)
1792   {
1793     free_tmp_table(table->in_use, table);
1794     table=NULL;
1795   }
1796   if (tmp_table_param)
1797   {
1798     delete tmp_table_param;
1799     tmp_table_param= NULL;
1800   }
1801 }
1802 
1803 
arg_val_decimal(my_decimal * value)1804 my_decimal *Aggregator_simple::arg_val_decimal(my_decimal *value)
1805 {
1806   return item_sum->args[0]->val_decimal(value);
1807 }
1808 
1809 
arg_val_real()1810 double Aggregator_simple::arg_val_real()
1811 {
1812   return item_sum->args[0]->val_real();
1813 }
1814 
1815 
arg_is_null(bool use_null_value)1816 bool Aggregator_simple::arg_is_null(bool use_null_value)
1817 {
1818   Item **item= item_sum->args;
1819   const uint item_count= item_sum->arg_count;
1820   if (use_null_value)
1821   {
1822     for (uint i= 0; i < item_count; i++)
1823     {
1824       if (item[i]->null_value)
1825         return true;
1826     }
1827   }
1828   else
1829   {
1830     for (uint i= 0; i < item_count; i++)
1831     {
1832       if (item[i]->maybe_null && item[i]->is_null())
1833         return true;
1834     }
1835   }
1836   return false;
1837 }
1838 
1839 
arg_val_decimal(my_decimal * value)1840 my_decimal *Aggregator_distinct::arg_val_decimal(my_decimal * value)
1841 {
1842   return use_distinct_values ? table->field[0]->val_decimal(value) :
1843     item_sum->args[0]->val_decimal(value);
1844 }
1845 
1846 
arg_val_real()1847 double Aggregator_distinct::arg_val_real()
1848 {
1849   return use_distinct_values ? table->field[0]->val_real() :
1850     item_sum->args[0]->val_real();
1851 }
1852 
1853 
arg_is_null(bool use_null_value)1854 bool Aggregator_distinct::arg_is_null(bool use_null_value)
1855 {
1856   if (use_distinct_values)
1857   {
1858     const bool rc= table->field[0]->is_null();
1859     DBUG_ASSERT(!rc); // NULLs are never stored in 'tree'
1860     return rc;
1861   }
1862   return use_null_value ?
1863     item_sum->args[0]->null_value :
1864     (item_sum->args[0]->maybe_null && item_sum->args[0]->is_null());
1865 }
1866 
1867 
copy_or_same(THD * thd)1868 Item *Item_sum_count::copy_or_same(THD* thd)
1869 {
1870   DBUG_ENTER("Item_sum_count::copy_or_same");
1871   DBUG_RETURN(new (thd->mem_root) Item_sum_count(thd, this));
1872 }
1873 
1874 
direct_add(longlong add_count)1875 void Item_sum_count::direct_add(longlong add_count)
1876 {
1877   DBUG_ENTER("Item_sum_count::direct_add");
1878   DBUG_PRINT("info", ("add_count: %lld", add_count));
1879   direct_counted= TRUE;
1880   direct_reseted_field= FALSE;
1881   direct_count= add_count;
1882   DBUG_VOID_RETURN;
1883 }
1884 
1885 
clear()1886 void Item_sum_count::clear()
1887 {
1888   DBUG_ENTER("Item_sum_count::clear");
1889   count= 0;
1890   DBUG_VOID_RETURN;
1891 }
1892 
1893 
add()1894 bool Item_sum_count::add()
1895 {
1896   DBUG_ENTER("Item_sum_count::add");
1897   if (direct_counted)
1898   {
1899     direct_counted= FALSE;
1900     count+= direct_count;
1901   }
1902   else
1903   {
1904     direct_reseted_field= FALSE;
1905     if (aggr->arg_is_null(false))
1906       DBUG_RETURN(0);
1907     count++;
1908   }
1909   DBUG_RETURN(0);
1910 }
1911 
1912 
1913 /*
1914   Remove a row. This is used by window functions.
1915 */
1916 
remove()1917 void Item_sum_count::remove()
1918 {
1919   DBUG_ASSERT(aggr->Aggrtype() == Aggregator::SIMPLE_AGGREGATOR);
1920   if (aggr->arg_is_null(false))
1921     return;
1922   if (count > 0)
1923     count--;
1924 }
1925 
val_int()1926 longlong Item_sum_count::val_int()
1927 {
1928   DBUG_ENTER("Item_sum_count::val_int");
1929   DBUG_ASSERT(fixed == 1);
1930   if (aggr)
1931     aggr->endup();
1932   DBUG_RETURN((longlong)count);
1933 }
1934 
1935 
cleanup()1936 void Item_sum_count::cleanup()
1937 {
1938   DBUG_ENTER("Item_sum_count::cleanup");
1939   count= 0;
1940   direct_counted= FALSE;
1941   direct_reseted_field= FALSE;
1942   Item_sum_int::cleanup();
1943   DBUG_VOID_RETURN;
1944 }
1945 
1946 
1947 /*
1948   Average
1949 */
1950 
fix_length_and_dec_decimal()1951 void Item_sum_avg::fix_length_and_dec_decimal()
1952 {
1953   Item_sum_sum::fix_length_and_dec_decimal();
1954   int precision= args[0]->decimal_precision() + prec_increment;
1955   decimals= MY_MIN(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1956   max_length= my_decimal_precision_to_length_no_truncation(precision,
1957                                                            decimals,
1958                                                            unsigned_flag);
1959   f_precision= MY_MIN(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1960   f_scale=  args[0]->decimals;
1961   dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1962 }
1963 
1964 
fix_length_and_dec_double()1965 void Item_sum_avg::fix_length_and_dec_double()
1966 {
1967   Item_sum_sum::fix_length_and_dec_double();
1968   decimals= MY_MIN(args[0]->decimals + prec_increment,
1969                    FLOATING_POINT_DECIMALS);
1970   max_length= MY_MIN(args[0]->max_length + prec_increment, float_length(decimals));
1971 }
1972 
1973 
fix_length_and_dec()1974 bool Item_sum_avg::fix_length_and_dec()
1975 {
1976   DBUG_ENTER("Item_sum_avg::fix_length_and_dec");
1977   prec_increment= current_thd->variables.div_precincrement;
1978   maybe_null=null_value=1;
1979   if (args[0]->cast_to_int_type_handler()->
1980       Item_sum_avg_fix_length_and_dec(this))
1981     DBUG_RETURN(TRUE);
1982   DBUG_PRINT("info", ("Type: %s (%d, %d)", type_handler()->name().ptr(),
1983                       max_length, (int) decimals));
1984   DBUG_RETURN(FALSE);
1985 }
1986 
1987 
copy_or_same(THD * thd)1988 Item *Item_sum_avg::copy_or_same(THD* thd)
1989 {
1990   return new (thd->mem_root) Item_sum_avg(thd, this);
1991 }
1992 
1993 
create_tmp_field(MEM_ROOT * root,bool group,TABLE * table)1994 Field *Item_sum_avg::create_tmp_field(MEM_ROOT *root, bool group, TABLE *table)
1995 {
1996 
1997   if (group)
1998   {
1999     /*
2000       We must store both value and counter in the temporary table in one field.
2001       The easiest way is to do this is to store both value in a string
2002       and unpack on access.
2003     */
2004     Field *field= new (root)
2005       Field_string(((result_type() == DECIMAL_RESULT) ?
2006                    dec_bin_size : sizeof(double)) + sizeof(longlong),
2007                    0, &name, &my_charset_bin);
2008     if (field)
2009       field->init(table);
2010     return field;
2011   }
2012   return tmp_table_field_from_field_type(root, table);
2013 }
2014 
2015 
clear()2016 void Item_sum_avg::clear()
2017 {
2018   Item_sum_sum::clear();
2019   count=0;
2020 }
2021 
2022 
add()2023 bool Item_sum_avg::add()
2024 {
2025   if (Item_sum_sum::add())
2026     return TRUE;
2027   if (!aggr->arg_is_null(true))
2028     count++;
2029   return FALSE;
2030 }
2031 
remove()2032 void Item_sum_avg::remove()
2033 {
2034   Item_sum_sum::remove();
2035   if (!aggr->arg_is_null(true))
2036   {
2037     if (count > 0)
2038       count--;
2039   }
2040 }
2041 
val_real()2042 double Item_sum_avg::val_real()
2043 {
2044   DBUG_ASSERT(fixed == 1);
2045   if (aggr)
2046     aggr->endup();
2047   if (!count)
2048   {
2049     null_value=1;
2050     return 0.0;
2051   }
2052   return Item_sum_sum::val_real() / ulonglong2double(count);
2053 }
2054 
2055 
val_decimal(my_decimal * val)2056 my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
2057 {
2058   my_decimal cnt;
2059   const my_decimal *sum_dec;
2060   DBUG_ASSERT(fixed == 1);
2061   if (aggr)
2062     aggr->endup();
2063   if (!count)
2064   {
2065     null_value=1;
2066     return NULL;
2067   }
2068 
2069   /*
2070     For non-DECIMAL result_type() the division will be done in
2071     Item_sum_avg::val_real().
2072   */
2073   if (result_type() != DECIMAL_RESULT)
2074     return val_decimal_from_real(val);
2075 
2076   sum_dec= dec_buffs + curr_dec_buff;
2077   int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
2078   my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
2079   return val;
2080 }
2081 
2082 
val_str(String * str)2083 String *Item_sum_avg::val_str(String *str)
2084 {
2085   if (aggr)
2086     aggr->endup();
2087   if (result_type() == DECIMAL_RESULT)
2088     return VDec(this).to_string_round(str, decimals);
2089   return val_string_from_real(str);
2090 }
2091 
2092 
2093 /*
2094   Standard deviation
2095 */
2096 
val_real()2097 double Item_sum_std::val_real()
2098 {
2099   DBUG_ASSERT(fixed == 1);
2100   double nr= Item_sum_variance::val_real();
2101   if (std::isnan(nr))
2102   {
2103     /*
2104       variance_fp_recurrence_next() can overflow in some cases and return "nan":
2105 
2106       CREATE OR REPLACE TABLE t1 (a DOUBLE);
2107       INSERT INTO t1 VALUES (1.7e+308), (-1.7e+308), (0);
2108       SELECT STDDEV_SAMP(a) FROM t1;
2109     */
2110     null_value= true; // Convert "nan" to NULL
2111     return 0;
2112   }
2113   if (std::isinf(nr))
2114     return DBL_MAX;
2115   DBUG_ASSERT(nr >= 0.0);
2116   return sqrt(nr);
2117 }
2118 
copy_or_same(THD * thd)2119 Item *Item_sum_std::copy_or_same(THD* thd)
2120 {
2121   return new (thd->mem_root) Item_sum_std(thd, this);
2122 }
2123 
2124 
result_item(THD * thd,Field * field)2125 Item *Item_sum_std::result_item(THD *thd, Field *field)
2126 {
2127   return new (thd->mem_root) Item_std_field(thd, this);
2128 }
2129 
2130 
2131 /*
2132   Variance
2133 */
2134 
2135 
2136 /**
2137   Variance implementation for floating-point implementations, without
2138   catastrophic cancellation, from Knuth's _TAoCP_, 3rd ed, volume 2, pg232.
2139   This alters the value at m, s, and increments count.
2140 */
2141 
2142 /*
2143   These two functions are used by the Item_sum_variance and the
2144   Item_variance_field classes, which are unrelated, and each need to calculate
2145   variance.  The difference between the two classes is that the first is used
2146   for a mundane SELECT, while the latter is used in a GROUPing SELECT.
2147 */
recurrence_next(double nr)2148 void Stddev::recurrence_next(double nr)
2149 {
2150   if (!m_count++)
2151   {
2152     DBUG_ASSERT(m_m == 0);
2153     DBUG_ASSERT(m_s == 0);
2154     m_m= nr;
2155   }
2156   else
2157   {
2158     double m_kminusone= m_m;
2159     volatile double diff= nr - m_kminusone;
2160     m_m= m_kminusone + diff / (double) m_count;
2161     m_s= m_s + diff * (nr - m_m);
2162   }
2163 }
2164 
2165 
result(bool is_sample_variance)2166 double Stddev::result(bool is_sample_variance)
2167 {
2168   if (m_count == 1)
2169     return 0.0;
2170 
2171   if (is_sample_variance)
2172     return m_s / (m_count - 1);
2173 
2174   /* else, is a population variance */
2175   return m_s / m_count;
2176 }
2177 
2178 
Item_sum_variance(THD * thd,Item_sum_variance * item)2179 Item_sum_variance::Item_sum_variance(THD *thd, Item_sum_variance *item):
2180   Item_sum_double(thd, item),
2181     m_stddev(item->m_stddev), sample(item->sample),
2182     prec_increment(item->prec_increment)
2183 { }
2184 
2185 
fix_length_and_dec_double()2186 void Item_sum_variance::fix_length_and_dec_double()
2187 {
2188   DBUG_ASSERT(Item_sum_variance::type_handler() == &type_handler_double);
2189   decimals= MY_MIN(args[0]->decimals + 4, FLOATING_POINT_DECIMALS);
2190 }
2191 
2192 
fix_length_and_dec_decimal()2193 void Item_sum_variance::fix_length_and_dec_decimal()
2194 {
2195   DBUG_ASSERT(Item_sum_variance::type_handler() == &type_handler_double);
2196   int precision= args[0]->decimal_precision() * 2 + prec_increment;
2197   decimals= MY_MIN(args[0]->decimals + prec_increment,
2198                    FLOATING_POINT_DECIMALS - 1);
2199   max_length= my_decimal_precision_to_length_no_truncation(precision,
2200                                                            decimals,
2201                                                            unsigned_flag);
2202 }
2203 
2204 
fix_length_and_dec()2205 bool Item_sum_variance::fix_length_and_dec()
2206 {
2207   DBUG_ENTER("Item_sum_variance::fix_length_and_dec");
2208   maybe_null= null_value= 1;
2209   prec_increment= current_thd->variables.div_precincrement;
2210 
2211   /*
2212     According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
2213     aggregate function; paragraph 7h of Syntax Rules), "the declared
2214     type of the result is an implementation-defined approximate numeric
2215     type.
2216   */
2217   if (args[0]->type_handler()->Item_sum_variance_fix_length_and_dec(this))
2218     DBUG_RETURN(TRUE);
2219   DBUG_PRINT("info", ("Type: %s (%d, %d)", type_handler()->name().ptr(),
2220                       max_length, (int)decimals));
2221   DBUG_RETURN(FALSE);
2222 }
2223 
2224 
copy_or_same(THD * thd)2225 Item *Item_sum_variance::copy_or_same(THD* thd)
2226 {
2227   return new (thd->mem_root) Item_sum_variance(thd, this);
2228 }
2229 
2230 
2231 /**
2232   Create a new field to match the type of value we're expected to yield.
2233   If we're grouping, then we need some space to serialize variables into, to
2234   pass around.
2235 */
create_tmp_field(MEM_ROOT * root,bool group,TABLE * table)2236 Field *Item_sum_variance::create_tmp_field(MEM_ROOT *root,
2237                                            bool group, TABLE *table)
2238 {
2239   Field *field;
2240   if (group)
2241   {
2242     /*
2243       We must store both value and counter in the temporary table in one field.
2244       The easiest way is to do this is to store both value in a string
2245       and unpack on access.
2246     */
2247     field= new (root) Field_string(Stddev::binary_size(), 0,
2248                                    &name, &my_charset_bin);
2249   }
2250   else
2251     field= new (root) Field_double(max_length, maybe_null, &name, decimals,
2252                                    TRUE);
2253 
2254   if (field != NULL)
2255     field->init(table);
2256 
2257   return field;
2258 }
2259 
2260 
clear()2261 void Item_sum_variance::clear()
2262 {
2263   m_stddev= Stddev();
2264 }
2265 
add()2266 bool Item_sum_variance::add()
2267 {
2268   /*
2269     Why use a temporary variable?  We don't know if it is null until we
2270     evaluate it, which has the side-effect of setting null_value .
2271   */
2272   double nr= args[0]->val_real();
2273 
2274   if (!args[0]->null_value)
2275     m_stddev.recurrence_next(nr);
2276   return 0;
2277 }
2278 
val_real()2279 double Item_sum_variance::val_real()
2280 {
2281   DBUG_ASSERT(fixed == 1);
2282 
2283   /*
2284     'sample' is a 1/0 boolean value.  If it is 1/true, id est this is a sample
2285     variance call, then we should set nullness when the count of the items
2286     is one or zero.  If it's zero, i.e. a population variance, then we only
2287     set nullness when the count is zero.
2288 
2289     Another way to read it is that 'sample' is the numerical threshold, at and
2290     below which a 'count' number of items is called NULL.
2291   */
2292   DBUG_ASSERT((sample == 0) || (sample == 1));
2293   if (m_stddev.count() <= sample)
2294   {
2295     null_value=1;
2296     return 0.0;
2297   }
2298 
2299   null_value=0;
2300   return m_stddev.result(sample);
2301 }
2302 
2303 
reset_field()2304 void Item_sum_variance::reset_field()
2305 {
2306   double nr;
2307   uchar *res= result_field->ptr;
2308 
2309   nr= args[0]->val_real();              /* sets null_value as side-effect */
2310 
2311   if (args[0]->null_value)
2312     bzero(res,Stddev::binary_size());
2313   else
2314     Stddev(nr).to_binary(res);
2315 }
2316 
2317 
Stddev(const uchar * ptr)2318 Stddev::Stddev(const uchar *ptr)
2319 {
2320   float8get(m_m, ptr);
2321   float8get(m_s, ptr + sizeof(double));
2322   m_count= sint8korr(ptr + sizeof(double) * 2);
2323 }
2324 
2325 
to_binary(uchar * ptr) const2326 void Stddev::to_binary(uchar *ptr) const
2327 {
2328   /* Serialize format is (double)m, (double)s, (longlong)count */
2329   float8store(ptr, m_m);
2330   float8store(ptr + sizeof(double), m_s);
2331   ptr+= sizeof(double)*2;
2332   int8store(ptr, m_count);
2333 }
2334 
2335 
update_field()2336 void Item_sum_variance::update_field()
2337 {
2338   uchar *res=result_field->ptr;
2339 
2340   double nr= args[0]->val_real();       /* sets null_value as side-effect */
2341 
2342   if (args[0]->null_value)
2343     return;
2344 
2345   /* Serialize format is (double)m, (double)s, (longlong)count */
2346   Stddev field_stddev(res);
2347   field_stddev.recurrence_next(nr);
2348   field_stddev.to_binary(res);
2349 }
2350 
2351 
result_item(THD * thd,Field * field)2352 Item *Item_sum_variance::result_item(THD *thd, Field *field)
2353 {
2354   return new (thd->mem_root) Item_variance_field(thd, this);
2355 }
2356 
2357 /* min & max */
2358 
clear()2359 void Item_sum_min_max::clear()
2360 {
2361   DBUG_ENTER("Item_sum_min_max::clear");
2362   value->clear();
2363   null_value= 1;
2364   DBUG_VOID_RETURN;
2365 }
2366 
2367 
2368 bool
get_date(THD * thd,MYSQL_TIME * ltime,date_mode_t fuzzydate)2369 Item_sum_min_max::get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate)
2370 {
2371   DBUG_ASSERT(fixed == 1);
2372   if (null_value)
2373     return true;
2374   bool retval= value->get_date(thd, ltime, fuzzydate);
2375   if ((null_value= value->null_value))
2376     DBUG_ASSERT(retval == true);
2377   return retval;
2378 }
2379 
2380 
direct_add(Item * item)2381 void Item_sum_min_max::direct_add(Item *item)
2382 {
2383   DBUG_ENTER("Item_sum_min_max::direct_add");
2384   DBUG_PRINT("info", ("item: %p", item));
2385   direct_added= TRUE;
2386   direct_item= item;
2387   DBUG_VOID_RETURN;
2388 }
2389 
2390 
val_real()2391 double Item_sum_min_max::val_real()
2392 {
2393   DBUG_ENTER("Item_sum_min_max::val_real");
2394   DBUG_ASSERT(fixed == 1);
2395   if (null_value)
2396     DBUG_RETURN(0.0);
2397   double retval= value->val_real();
2398   if ((null_value= value->null_value))
2399     DBUG_ASSERT(retval == 0.0);
2400   DBUG_RETURN(retval);
2401 }
2402 
val_int()2403 longlong Item_sum_min_max::val_int()
2404 {
2405   DBUG_ENTER("Item_sum_min_max::val_int");
2406   DBUG_ASSERT(fixed == 1);
2407   if (null_value)
2408     DBUG_RETURN(0);
2409   longlong retval= value->val_int();
2410   if ((null_value= value->null_value))
2411     DBUG_ASSERT(retval == 0);
2412   DBUG_RETURN(retval);
2413 }
2414 
2415 
val_decimal(my_decimal * val)2416 my_decimal *Item_sum_min_max::val_decimal(my_decimal *val)
2417 {
2418   DBUG_ENTER("Item_sum_min_max::val_decimal");
2419   DBUG_ASSERT(fixed == 1);
2420   if (null_value)
2421     DBUG_RETURN(0);
2422   my_decimal *retval= value->val_decimal(val);
2423   if ((null_value= value->null_value))
2424     DBUG_ASSERT(retval == NULL);
2425   DBUG_RETURN(retval);
2426 }
2427 
2428 
2429 String *
val_str(String * str)2430 Item_sum_min_max::val_str(String *str)
2431 {
2432   DBUG_ENTER("Item_sum_min_max::val_str");
2433   DBUG_ASSERT(fixed == 1);
2434   if (null_value)
2435     DBUG_RETURN(0);
2436   String *retval= value->val_str(str);
2437   if ((null_value= value->null_value))
2438     DBUG_ASSERT(retval == NULL);
2439   DBUG_RETURN(retval);
2440 }
2441 
2442 
val_native(THD * thd,Native * to)2443 bool Item_sum_min_max::val_native(THD *thd, Native *to)
2444 {
2445   DBUG_ASSERT(fixed == 1);
2446   if (null_value)
2447     return true;
2448   return val_native_from_item(thd, value, to);
2449 }
2450 
2451 
cleanup()2452 void Item_sum_min_max::cleanup()
2453 {
2454   DBUG_ENTER("Item_sum_min_max::cleanup");
2455   Item_sum::cleanup();
2456   if (cmp)
2457     delete cmp;
2458   cmp= 0;
2459   /*
2460     by default it is TRUE to avoid TRUE reporting by
2461     Item_func_not_all/Item_func_nop_all if this item was never called.
2462 
2463     no_rows_in_result() set it to FALSE if was not results found.
2464     If some results found it will be left unchanged.
2465   */
2466   was_values= TRUE;
2467   DBUG_VOID_RETURN;
2468 }
2469 
no_rows_in_result()2470 void Item_sum_min_max::no_rows_in_result()
2471 {
2472   DBUG_ENTER("Item_sum_min_max::no_rows_in_result");
2473   /* We may be called here twice in case of ref field in function */
2474   if (was_values)
2475   {
2476     was_values= FALSE;
2477     was_null_value= value->null_value;
2478     clear();
2479   }
2480   DBUG_VOID_RETURN;
2481 }
2482 
restore_to_before_no_rows_in_result()2483 void Item_sum_min_max::restore_to_before_no_rows_in_result()
2484 {
2485   if (!was_values)
2486   {
2487     was_values= TRUE;
2488     null_value= value->null_value= was_null_value;
2489   }
2490 }
2491 
2492 
copy_or_same(THD * thd)2493 Item *Item_sum_min::copy_or_same(THD* thd)
2494 {
2495   DBUG_ENTER("Item_sum_min::copy_or_same");
2496   Item_sum_min *item= new (thd->mem_root) Item_sum_min(thd, this);
2497   item->setup_hybrid(thd, args[0], value);
2498   DBUG_RETURN(item);
2499 }
2500 
2501 
add()2502 bool Item_sum_min::add()
2503 {
2504   Item *UNINIT_VAR(tmp_item);
2505   DBUG_ENTER("Item_sum_min::add");
2506   DBUG_PRINT("enter", ("this: %p", this));
2507 
2508   if (unlikely(direct_added))
2509   {
2510     /* Change to use direct_item */
2511     tmp_item= arg_cache->get_item();
2512     arg_cache->store(direct_item);
2513   }
2514   DBUG_PRINT("info", ("null_value: %s", null_value ? "TRUE" : "FALSE"));
2515   /* args[0] < value */
2516   arg_cache->cache_value();
2517   if (!arg_cache->null_value &&
2518       (null_value || cmp->compare() < 0))
2519   {
2520     value->store(arg_cache);
2521     value->cache_value();
2522     null_value= 0;
2523   }
2524   if (unlikely(direct_added))
2525   {
2526     /* Restore original item */
2527     direct_added= FALSE;
2528     arg_cache->store(tmp_item);
2529   }
2530   DBUG_RETURN(0);
2531 }
2532 
2533 
copy_or_same(THD * thd)2534 Item *Item_sum_max::copy_or_same(THD* thd)
2535 {
2536   Item_sum_max *item= new (thd->mem_root) Item_sum_max(thd, this);
2537   item->setup_hybrid(thd, args[0], value);
2538   return item;
2539 }
2540 
2541 
add()2542 bool Item_sum_max::add()
2543 {
2544   Item * UNINIT_VAR(tmp_item);
2545   DBUG_ENTER("Item_sum_max::add");
2546   DBUG_PRINT("enter", ("this: %p", this));
2547 
2548   if (unlikely(direct_added))
2549   {
2550     /* Change to use direct_item */
2551     tmp_item= arg_cache->get_item();
2552     arg_cache->store(direct_item);
2553   }
2554   /* args[0] > value */
2555   arg_cache->cache_value();
2556   DBUG_PRINT("info", ("null_value: %s", null_value ? "TRUE" : "FALSE"));
2557   if (!arg_cache->null_value &&
2558       (null_value || cmp->compare() > 0))
2559   {
2560     value->store(arg_cache);
2561     value->cache_value();
2562     null_value= 0;
2563   }
2564   if (unlikely(direct_added))
2565   {
2566     /* Restore original item */
2567     direct_added= FALSE;
2568     arg_cache->store(tmp_item);
2569   }
2570   DBUG_RETURN(0);
2571 }
2572 
2573 
2574 /* bit_or and bit_and */
2575 
val_int()2576 longlong Item_sum_bit::val_int()
2577 {
2578   DBUG_ASSERT(fixed == 1);
2579   return (longlong) bits;
2580 }
2581 
2582 
clear()2583 void Item_sum_bit::clear()
2584 {
2585   bits= reset_bits;
2586   if (as_window_function)
2587     clear_as_window();
2588 }
2589 
copy_or_same(THD * thd)2590 Item *Item_sum_or::copy_or_same(THD* thd)
2591 {
2592   return new (thd->mem_root) Item_sum_or(thd, this);
2593 }
2594 
clear_as_window()2595 bool Item_sum_bit::clear_as_window()
2596 {
2597   memset(bit_counters, 0, sizeof(bit_counters));
2598   num_values_added= 0;
2599   set_bits_from_counters();
2600   return 0;
2601 }
2602 
remove_as_window(ulonglong value)2603 bool Item_sum_bit::remove_as_window(ulonglong value)
2604 {
2605   DBUG_ASSERT(as_window_function);
2606   if (num_values_added == 0)
2607     return 0; // Nothing to remove.
2608 
2609   for (int i= 0; i < NUM_BIT_COUNTERS; i++)
2610   {
2611     if (!bit_counters[i])
2612     {
2613       // Don't attempt to remove values that were never added.
2614       DBUG_ASSERT((value & (1ULL << i)) == 0);
2615       continue;
2616     }
2617     bit_counters[i]-= (value & (1ULL << i)) ? 1 : 0;
2618   }
2619 
2620   // Prevent overflow;
2621   num_values_added = MY_MIN(num_values_added, num_values_added - 1);
2622   set_bits_from_counters();
2623   return 0;
2624 }
2625 
add_as_window(ulonglong value)2626 bool Item_sum_bit::add_as_window(ulonglong value)
2627 {
2628   DBUG_ASSERT(as_window_function);
2629   for (int i= 0; i < NUM_BIT_COUNTERS; i++)
2630   {
2631     bit_counters[i]+= (value & (1ULL << i)) ? 1 : 0;
2632   }
2633   // Prevent overflow;
2634   num_values_added = MY_MAX(num_values_added, num_values_added + 1);
2635   set_bits_from_counters();
2636   return 0;
2637 }
2638 
set_bits_from_counters()2639 void Item_sum_or::set_bits_from_counters()
2640 {
2641   ulonglong value= 0;
2642   for (uint i= 0; i < NUM_BIT_COUNTERS; i++)
2643   {
2644     value|= bit_counters[i] > 0 ? (1ULL << i) : 0ULL;
2645   }
2646   bits= value | reset_bits;
2647 }
2648 
add()2649 bool Item_sum_or::add()
2650 {
2651   ulonglong value= (ulonglong) args[0]->val_int();
2652   if (!args[0]->null_value)
2653   {
2654     if (as_window_function)
2655       return add_as_window(value);
2656     bits|=value;
2657   }
2658   return 0;
2659 }
2660 
set_bits_from_counters()2661 void Item_sum_xor::set_bits_from_counters()
2662 {
2663   ulonglong value= 0;
2664   for (int i= 0; i < NUM_BIT_COUNTERS; i++)
2665   {
2666     value|= (bit_counters[i] % 2) ? (1 << i) : 0;
2667   }
2668   bits= value ^ reset_bits;
2669 }
2670 
copy_or_same(THD * thd)2671 Item *Item_sum_xor::copy_or_same(THD* thd)
2672 {
2673   return new (thd->mem_root) Item_sum_xor(thd, this);
2674 }
2675 
2676 
add()2677 bool Item_sum_xor::add()
2678 {
2679   ulonglong value= (ulonglong) args[0]->val_int();
2680   if (!args[0]->null_value)
2681   {
2682     if (as_window_function)
2683       return add_as_window(value);
2684     bits^=value;
2685   }
2686   return 0;
2687 }
2688 
set_bits_from_counters()2689 void Item_sum_and::set_bits_from_counters()
2690 {
2691   ulonglong value= 0;
2692   if (!num_values_added)
2693   {
2694     bits= reset_bits;
2695     return;
2696   }
2697 
2698   for (int i= 0; i < NUM_BIT_COUNTERS; i++)
2699   {
2700     // We've only added values of 1 for this bit.
2701     if (bit_counters[i] == num_values_added)
2702       value|= (1ULL << i);
2703   }
2704   bits= value & reset_bits;
2705 }
copy_or_same(THD * thd)2706 Item *Item_sum_and::copy_or_same(THD* thd)
2707 {
2708   return new (thd->mem_root) Item_sum_and(thd, this);
2709 }
2710 
2711 
add()2712 bool Item_sum_and::add()
2713 {
2714   ulonglong value= (ulonglong) args[0]->val_int();
2715   if (!args[0]->null_value)
2716   {
2717     if (as_window_function)
2718       return add_as_window(value);
2719     bits&=value;
2720   }
2721   return 0;
2722 }
2723 
2724 /************************************************************************
2725 ** reset result of a Item_sum with is saved in a tmp_table
2726 *************************************************************************/
2727 
reset_field()2728 void Item_sum_min_max::reset_field()
2729 {
2730   Item *UNINIT_VAR(tmp_item), *arg0;
2731   DBUG_ENTER("Item_sum_min_max::reset_field");
2732 
2733   arg0= args[0];
2734   if (unlikely(direct_added))
2735   {
2736     /* Switch to use direct item */
2737     tmp_item= value->get_item();
2738     value->store(direct_item);
2739     arg0= direct_item;
2740   }
2741 
2742   switch(result_type()) {
2743   case STRING_RESULT:
2744   {
2745     char buff[MAX_FIELD_WIDTH];
2746     String tmp(buff,sizeof(buff),result_field->charset()),*res;
2747 
2748     res= arg0->val_str(&tmp);
2749     if (arg0->null_value)
2750     {
2751       result_field->set_null();
2752       result_field->reset();
2753     }
2754     else
2755     {
2756       result_field->set_notnull();
2757       result_field->store(res->ptr(),res->length(),tmp.charset());
2758     }
2759     break;
2760   }
2761   case INT_RESULT:
2762   {
2763     longlong nr= arg0->val_int();
2764 
2765     if (maybe_null)
2766     {
2767       if (arg0->null_value)
2768       {
2769 	nr=0;
2770 	result_field->set_null();
2771       }
2772       else
2773 	result_field->set_notnull();
2774     }
2775     DBUG_PRINT("info", ("nr: %lld", nr));
2776     result_field->store(nr, unsigned_flag);
2777     break;
2778   }
2779   case REAL_RESULT:
2780   {
2781     double nr= arg0->val_real();
2782 
2783     if (maybe_null)
2784     {
2785       if (arg0->null_value)
2786       {
2787 	nr=0.0;
2788 	result_field->set_null();
2789       }
2790       else
2791 	result_field->set_notnull();
2792     }
2793     result_field->store(nr);
2794     break;
2795   }
2796   case DECIMAL_RESULT:
2797   {
2798     VDec arg_dec(arg0);
2799 
2800     if (maybe_null)
2801     {
2802       if (arg_dec.is_null())
2803         result_field->set_null();
2804       else
2805         result_field->set_notnull();
2806     }
2807     /*
2808       We must store zero in the field as we will use the field value in
2809       add()
2810     */
2811     result_field->store_decimal(arg_dec.ptr_or(&decimal_zero));
2812     break;
2813   }
2814   case ROW_RESULT:
2815   case TIME_RESULT:
2816     DBUG_ASSERT(0);
2817   }
2818 
2819   if (unlikely(direct_added))
2820   {
2821     direct_added= FALSE;
2822     value->store(tmp_item);
2823   }
2824   DBUG_VOID_RETURN;
2825 }
2826 
2827 
reset_field()2828 void Item_sum_sum::reset_field()
2829 {
2830   my_bool null_flag;
2831   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2832   if (result_type() == DECIMAL_RESULT)
2833   {
2834     if (unlikely(direct_added))
2835       result_field->store_decimal(&direct_sum_decimal);
2836     else
2837       result_field->store_decimal(VDec(args[0]).ptr_or(&decimal_zero));
2838   }
2839   else
2840   {
2841     DBUG_ASSERT(result_type() == REAL_RESULT);
2842     double nr= likely(!direct_added) ? args[0]->val_real() : direct_sum_real;
2843     float8store(result_field->ptr, nr);
2844   }
2845 
2846   if (unlikely(direct_added))
2847   {
2848     direct_added= FALSE;
2849     direct_reseted_field= TRUE;
2850     null_flag= direct_sum_is_null;
2851   }
2852   else
2853     null_flag= args[0]->null_value;
2854 
2855   if (null_flag)
2856     result_field->set_null();
2857   else
2858     result_field->set_notnull();
2859 }
2860 
2861 
reset_field()2862 void Item_sum_count::reset_field()
2863 {
2864   DBUG_ENTER("Item_sum_count::reset_field");
2865   uchar *res=result_field->ptr;
2866   longlong nr=0;
2867   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2868 
2869   if (unlikely(direct_counted))
2870   {
2871     nr= direct_count;
2872     direct_counted= FALSE;
2873     direct_reseted_field= TRUE;
2874   }
2875   else if (!args[0]->maybe_null || !args[0]->is_null())
2876     nr= 1;
2877   DBUG_PRINT("info", ("nr: %lld", nr));
2878   int8store(res,nr);
2879   DBUG_VOID_RETURN;
2880 }
2881 
2882 
reset_field()2883 void Item_sum_avg::reset_field()
2884 {
2885   uchar *res=result_field->ptr;
2886   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2887   if (result_type() == DECIMAL_RESULT)
2888   {
2889     longlong tmp;
2890     VDec value(args[0]);
2891     tmp= value.is_null() ? 0 : 1;
2892     value.to_binary(res, f_precision, f_scale);
2893     res+= dec_bin_size;
2894     int8store(res, tmp);
2895   }
2896   else
2897   {
2898     double nr= args[0]->val_real();
2899 
2900     if (args[0]->null_value)
2901       bzero(res,sizeof(double)+sizeof(longlong));
2902     else
2903     {
2904       longlong tmp= 1;
2905       float8store(res,nr);
2906       res+=sizeof(double);
2907       int8store(res,tmp);
2908     }
2909   }
2910 }
2911 
2912 
reset_field()2913 void Item_sum_bit::reset_field()
2914 {
2915   reset_and_add();
2916   int8store(result_field->ptr, bits);
2917 }
2918 
update_field()2919 void Item_sum_bit::update_field()
2920 {
2921   // We never call update_field when computing the function as a window
2922   // function. Setting bits to a random value invalidates the bits counters and
2923   // the result of the bit function becomes erroneous.
2924   DBUG_ASSERT(!as_window_function);
2925   uchar *res=result_field->ptr;
2926   bits= uint8korr(res);
2927   add();
2928   int8store(res, bits);
2929 }
2930 
2931 
2932 /**
2933   calc next value and merge it with field_value.
2934 */
2935 
update_field()2936 void Item_sum_sum::update_field()
2937 {
2938   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2939   if (result_type() == DECIMAL_RESULT)
2940   {
2941     my_decimal value, *arg_val;
2942     my_bool null_flag;
2943     if (unlikely(direct_added || direct_reseted_field))
2944     {
2945       direct_added= direct_reseted_field= FALSE;
2946       arg_val= &direct_sum_decimal;
2947       null_flag= direct_sum_is_null;
2948     }
2949     else
2950     {
2951       arg_val= args[0]->val_decimal(&value);
2952       null_flag= args[0]->null_value;
2953     }
2954 
2955     if (!null_flag)
2956     {
2957       if (!result_field->is_null())
2958       {
2959         my_decimal field_value(result_field);
2960         my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, &field_value);
2961         result_field->store_decimal(dec_buffs);
2962       }
2963       else
2964       {
2965         result_field->store_decimal(arg_val);
2966         result_field->set_notnull();
2967       }
2968     }
2969   }
2970   else
2971   {
2972     double old_nr,nr;
2973     uchar *res= result_field->ptr;
2974     my_bool null_flag;
2975 
2976     float8get(old_nr,res);
2977     if (unlikely(direct_added || direct_reseted_field))
2978     {
2979       direct_added= direct_reseted_field= FALSE;
2980       null_flag= direct_sum_is_null;
2981       nr= direct_sum_real;
2982     }
2983     else
2984     {
2985       nr= args[0]->val_real();
2986       null_flag= args[0]->null_value;
2987     }
2988     if (!null_flag)
2989     {
2990       old_nr+=nr;
2991       result_field->set_notnull();
2992     }
2993     float8store(res,old_nr);
2994   }
2995 }
2996 
2997 
update_field()2998 void Item_sum_count::update_field()
2999 {
3000   DBUG_ENTER("Item_sum_count::update_field");
3001   longlong nr;
3002   uchar *res=result_field->ptr;
3003 
3004   nr=sint8korr(res);
3005   if (unlikely(direct_counted || direct_reseted_field))
3006   {
3007     direct_counted= direct_reseted_field= FALSE;
3008     nr+= direct_count;
3009   }
3010   else if (!args[0]->maybe_null || !args[0]->is_null())
3011     nr++;
3012   DBUG_PRINT("info", ("nr: %lld", nr));
3013   int8store(res,nr);
3014   DBUG_VOID_RETURN;
3015 }
3016 
3017 
update_field()3018 void Item_sum_avg::update_field()
3019 {
3020   longlong field_count;
3021   uchar *res=result_field->ptr;
3022 
3023   DBUG_ASSERT (aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
3024 
3025   if (result_type() == DECIMAL_RESULT)
3026   {
3027     VDec tmp(args[0]);
3028     if (!tmp.is_null())
3029     {
3030       binary2my_decimal(E_DEC_FATAL_ERROR, res,
3031                         dec_buffs + 1, f_precision, f_scale);
3032       field_count= sint8korr(res + dec_bin_size);
3033       my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, tmp.ptr(), dec_buffs + 1);
3034       dec_buffs->to_binary(res, f_precision, f_scale);
3035       res+= dec_bin_size;
3036       field_count++;
3037       int8store(res, field_count);
3038     }
3039   }
3040   else
3041   {
3042     double nr;
3043 
3044     nr= args[0]->val_real();
3045     if (!args[0]->null_value)
3046     {
3047       double old_nr;
3048       float8get(old_nr, res);
3049       field_count= sint8korr(res + sizeof(double));
3050       old_nr+= nr;
3051       float8store(res,old_nr);
3052       res+= sizeof(double);
3053       field_count++;
3054       int8store(res, field_count);
3055     }
3056   }
3057 }
3058 
3059 
result_item(THD * thd,Field * field)3060 Item *Item_sum_avg::result_item(THD *thd, Field *field)
3061 {
3062   return
3063     result_type() == DECIMAL_RESULT ?
3064     (Item_avg_field*) new (thd->mem_root) Item_avg_field_decimal(thd, this) :
3065     (Item_avg_field*) new (thd->mem_root) Item_avg_field_double(thd, this);
3066 }
3067 
3068 
update_field()3069 void Item_sum_min_max::update_field()
3070 {
3071   DBUG_ENTER("Item_sum_min_max::update_field");
3072   Item *UNINIT_VAR(tmp_item);
3073   if (unlikely(direct_added))
3074   {
3075     tmp_item= args[0];
3076     args[0]= direct_item;
3077   }
3078   if (Item_sum_min_max::type_handler()->is_val_native_ready())
3079   {
3080     /*
3081       TODO-10.5: change Item_sum_min_max to use val_native() for all data types
3082       - make all type handlers val_native() ready
3083       - use min_max_update_native_field() for all data types
3084       - remove Item_sum_min_max::min_max_update_{str|real|int|decimal}_field()
3085     */
3086     min_max_update_native_field();
3087   }
3088   else
3089   {
3090     switch (Item_sum_min_max::type_handler()->cmp_type()) {
3091     case STRING_RESULT:
3092     case TIME_RESULT:
3093       min_max_update_str_field();
3094       break;
3095     case INT_RESULT:
3096       min_max_update_int_field();
3097       break;
3098     case DECIMAL_RESULT:
3099       min_max_update_decimal_field();
3100       break;
3101     default:
3102       min_max_update_real_field();
3103     }
3104   }
3105   if (unlikely(direct_added))
3106   {
3107     direct_added= FALSE;
3108     args[0]= tmp_item;
3109   }
3110   DBUG_VOID_RETURN;
3111 }
3112 
3113 
min_max_update_field_native(THD * thd,Field * field,Item * item,int cmp_sign)3114 void Arg_comparator::min_max_update_field_native(THD *thd,
3115                                                  Field *field,
3116                                                  Item *item,
3117                                                  int cmp_sign)
3118 {
3119   DBUG_ENTER("Arg_comparator::min_max_update_field_native");
3120   if (!item->val_native(current_thd, &m_native2))
3121   {
3122     if (field->is_null())
3123       field->store_native(m_native2); // The first non-null value
3124     else
3125     {
3126       field->val_native(&m_native1);
3127       if ((cmp_sign * m_compare_handler->cmp_native(m_native2, m_native1)) < 0)
3128         field->store_native(m_native2);
3129     }
3130     field->set_notnull();
3131   }
3132   DBUG_VOID_RETURN;
3133 }
3134 
3135 
3136 void
min_max_update_native_field()3137 Item_sum_min_max::min_max_update_native_field()
3138 {
3139   DBUG_ENTER("Item_sum_min_max::min_max_update_native_field");
3140   DBUG_ASSERT(cmp);
3141   DBUG_ASSERT(type_handler_for_comparison() == cmp->compare_type_handler());
3142   THD *thd= current_thd;
3143   cmp->min_max_update_field_native(thd, result_field, args[0], cmp_sign);
3144   DBUG_VOID_RETURN;
3145 }
3146 
3147 
3148 void
min_max_update_str_field()3149 Item_sum_min_max::min_max_update_str_field()
3150 {
3151   DBUG_ENTER("Item_sum_min_max::min_max_update_str_field");
3152   DBUG_ASSERT(cmp);
3153   String *res_str=args[0]->val_str(&cmp->value1);
3154 
3155   if (!args[0]->null_value)
3156   {
3157     if (result_field->is_null())
3158       result_field->store(res_str->ptr(),res_str->length(),res_str->charset());
3159     else
3160     {
3161       result_field->val_str(&cmp->value2);
3162       if ((cmp_sign * sortcmp(res_str,&cmp->value2,collation.collation)) < 0)
3163         result_field->store(res_str->ptr(),res_str->length(),res_str->charset());
3164     }
3165     result_field->set_notnull();
3166   }
3167   DBUG_VOID_RETURN;
3168 }
3169 
3170 
3171 void
min_max_update_real_field()3172 Item_sum_min_max::min_max_update_real_field()
3173 {
3174   double nr,old_nr;
3175 
3176   DBUG_ENTER("Item_sum_min_max::min_max_update_real_field");
3177   old_nr=result_field->val_real();
3178   nr= args[0]->val_real();
3179   if (!args[0]->null_value)
3180   {
3181     if (result_field->is_null(0) ||
3182 	(cmp_sign > 0 ? old_nr > nr : old_nr < nr))
3183       old_nr=nr;
3184     result_field->set_notnull();
3185   }
3186   else if (result_field->is_null(0))
3187     result_field->set_null();
3188   result_field->store(old_nr);
3189   DBUG_VOID_RETURN;
3190 }
3191 
3192 
3193 void
min_max_update_int_field()3194 Item_sum_min_max::min_max_update_int_field()
3195 {
3196   longlong nr,old_nr;
3197 
3198   DBUG_ENTER("Item_sum_min_max::min_max_update_int_field");
3199   old_nr=result_field->val_int();
3200   nr=args[0]->val_int();
3201   if (!args[0]->null_value)
3202   {
3203     if (result_field->is_null(0))
3204       old_nr=nr;
3205     else
3206     {
3207       bool res=(unsigned_flag ?
3208 		(ulonglong) old_nr > (ulonglong) nr :
3209 		old_nr > nr);
3210       /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
3211       if ((cmp_sign > 0) ^ (!res))
3212 	old_nr=nr;
3213     }
3214     result_field->set_notnull();
3215   }
3216   else if (result_field->is_null(0))
3217     result_field->set_null();
3218   DBUG_PRINT("info", ("nr: %lld", old_nr));
3219   result_field->store(old_nr, unsigned_flag);
3220   DBUG_VOID_RETURN;
3221 }
3222 
3223 
3224 /**
3225   @todo
3226   optimize: do not get result_field in case of args[0] is NULL
3227 */
3228 void
min_max_update_decimal_field()3229 Item_sum_min_max::min_max_update_decimal_field()
3230 {
3231   DBUG_ENTER("Item_sum_min_max::min_max_update_decimal_field");
3232   my_decimal old_val, nr_val;
3233   const my_decimal *old_nr;
3234   const my_decimal *nr= args[0]->val_decimal(&nr_val);
3235   if (!args[0]->null_value)
3236   {
3237     if (result_field->is_null(0))
3238       old_nr=nr;
3239     else
3240     {
3241       old_nr= result_field->val_decimal(&old_val);
3242       bool res= my_decimal_cmp(old_nr, nr) > 0;
3243       /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
3244       if ((cmp_sign > 0) ^ (!res))
3245         old_nr=nr;
3246     }
3247     result_field->set_notnull();
3248     result_field->store_decimal(old_nr);
3249   }
3250   else if (result_field->is_null(0))
3251     result_field->set_null();
3252   DBUG_VOID_RETURN;
3253 }
3254 
3255 
val_real()3256 double Item_avg_field_double::val_real()
3257 {
3258   // fix_fields() never calls for this Item
3259   double nr;
3260   longlong count;
3261   uchar *res;
3262 
3263   float8get(nr,field->ptr);
3264   res= (field->ptr+sizeof(double));
3265   count= sint8korr(res);
3266 
3267   if ((null_value= !count))
3268     return 0.0;
3269   return nr/(double) count;
3270 }
3271 
3272 
val_decimal(my_decimal * dec_buf)3273 my_decimal *Item_avg_field_decimal::val_decimal(my_decimal *dec_buf)
3274 {
3275   // fix_fields() never calls for this Item
3276   longlong count= sint8korr(field->ptr + dec_bin_size);
3277   if ((null_value= !count))
3278     return 0;
3279 
3280   my_decimal dec_count, dec_field(field->ptr, f_precision, f_scale);
3281   int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
3282   my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
3283                  &dec_field, &dec_count, prec_increment);
3284   return dec_buf;
3285 }
3286 
3287 
val_real()3288 double Item_std_field::val_real()
3289 {
3290   double nr;
3291   // fix_fields() never calls for this Item
3292   nr= Item_variance_field::val_real();
3293   DBUG_ASSERT(nr >= 0.0);
3294   return sqrt(nr);
3295 }
3296 
3297 
val_real()3298 double Item_variance_field::val_real()
3299 {
3300   // fix_fields() never calls for this Item
3301   Stddev tmp(field->ptr);
3302   if ((null_value= (tmp.count() <= sample)))
3303     return 0.0;
3304 
3305   return tmp.result(sample);
3306 }
3307 
3308 
3309 /****************************************************************************
3310 ** Functions to handle dynamic loadable aggregates
3311 ** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
3312 ** Adapted for UDAs by: Andreas F. Bobak <bobak@relog.ch>.
3313 ** Rewritten by: Monty.
3314 ****************************************************************************/
3315 
3316 #ifdef HAVE_DLOPEN
3317 
clear()3318 void Item_udf_sum::clear()
3319 {
3320   DBUG_ENTER("Item_udf_sum::clear");
3321   udf.clear();
3322   DBUG_VOID_RETURN;
3323 }
3324 
add()3325 bool Item_udf_sum::add()
3326 {
3327   my_bool tmp_null_value;
3328   DBUG_ENTER("Item_udf_sum::add");
3329   udf.add(&tmp_null_value);
3330   null_value= tmp_null_value;
3331   DBUG_RETURN(0);
3332 }
3333 
3334 
supports_removal() const3335 bool Item_udf_sum::supports_removal() const
3336 {
3337   DBUG_ENTER("Item_udf_sum::supports_remove");
3338   DBUG_PRINT("info", ("support: %d", udf.supports_removal()));
3339   DBUG_RETURN(udf.supports_removal());
3340 }
3341 
3342 
remove()3343 void Item_udf_sum::remove()
3344 {
3345   my_bool tmp_null_value;
3346   DBUG_ENTER("Item_udf_sum::remove");
3347   udf.remove(&tmp_null_value);
3348   null_value= tmp_null_value;
3349   DBUG_VOID_RETURN;
3350 }
3351 
3352 
cleanup()3353 void Item_udf_sum::cleanup()
3354 {
3355   /*
3356     udf_handler::cleanup() nicely handles case when we have not
3357     original item but one created by copy_or_same() method.
3358   */
3359   udf.cleanup();
3360   Item_sum::cleanup();
3361 }
3362 
3363 
print(String * str,enum_query_type query_type)3364 void Item_udf_sum::print(String *str, enum_query_type query_type)
3365 {
3366   str->append(func_name());
3367   str->append('(');
3368   for (uint i=0 ; i < arg_count ; i++)
3369   {
3370     if (i)
3371       str->append(',');
3372     args[i]->print(str, query_type);
3373   }
3374   str->append(')');
3375 }
3376 
3377 
copy_or_same(THD * thd)3378 Item *Item_sum_udf_float::copy_or_same(THD* thd)
3379 {
3380   return new (thd->mem_root) Item_sum_udf_float(thd, this);
3381 }
3382 
val_real()3383 double Item_sum_udf_float::val_real()
3384 {
3385   my_bool tmp_null_value;
3386   double res;
3387   DBUG_ASSERT(fixed == 1);
3388   DBUG_ENTER("Item_sum_udf_float::val");
3389   DBUG_PRINT("enter",("result_type: %d  arg_count: %d",
3390 		     args[0]->result_type(), arg_count));
3391   res= udf.val(&tmp_null_value);
3392   null_value= tmp_null_value;
3393   DBUG_RETURN(res);
3394 }
3395 
3396 
val_str(String * str)3397 String *Item_sum_udf_float::val_str(String *str)
3398 {
3399   return val_string_from_real(str);
3400 }
3401 
3402 
val_decimal(my_decimal * dec)3403 my_decimal *Item_sum_udf_float::val_decimal(my_decimal *dec)
3404 {
3405   return val_decimal_from_real(dec);
3406 }
3407 
3408 
val_decimal(my_decimal * dec_buf)3409 my_decimal *Item_sum_udf_decimal::val_decimal(my_decimal *dec_buf)
3410 {
3411   my_decimal *res;
3412   my_bool tmp_null_value;
3413   DBUG_ASSERT(fixed == 1);
3414   DBUG_ENTER("Item_func_udf_decimal::val_decimal");
3415   DBUG_PRINT("enter",("result_type: %d  arg_count: %d",
3416                      args[0]->result_type(), arg_count));
3417 
3418   res= udf.val_decimal(&tmp_null_value, dec_buf);
3419   null_value= tmp_null_value;
3420   DBUG_RETURN(res);
3421 }
3422 
3423 
copy_or_same(THD * thd)3424 Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
3425 {
3426   return new (thd->mem_root) Item_sum_udf_decimal(thd, this);
3427 }
3428 
3429 
copy_or_same(THD * thd)3430 Item *Item_sum_udf_int::copy_or_same(THD* thd)
3431 {
3432   return new (thd->mem_root) Item_sum_udf_int(thd, this);
3433 }
3434 
val_int()3435 longlong Item_sum_udf_int::val_int()
3436 {
3437   my_bool tmp_null_value;
3438   longlong res;
3439   DBUG_ASSERT(fixed == 1);
3440   DBUG_ENTER("Item_sum_udf_int::val_int");
3441   DBUG_PRINT("enter",("result_type: %d  arg_count: %d",
3442 		     args[0]->result_type(), arg_count));
3443   res= udf.val_int(&tmp_null_value);
3444   null_value= tmp_null_value;
3445   DBUG_RETURN(res);
3446 }
3447 
3448 
val_str(String * str)3449 String *Item_sum_udf_int::val_str(String *str)
3450 {
3451   return val_string_from_int(str);
3452 }
3453 
val_decimal(my_decimal * dec)3454 my_decimal *Item_sum_udf_int::val_decimal(my_decimal *dec)
3455 {
3456   return val_decimal_from_int(dec);
3457 }
3458 
3459 
3460 /** Default max_length is max argument length. */
3461 
fix_length_and_dec()3462 bool Item_sum_udf_str::fix_length_and_dec()
3463 {
3464   DBUG_ENTER("Item_sum_udf_str::fix_length_and_dec");
3465   max_length=0;
3466   for (uint i = 0; i < arg_count; i++)
3467     set_if_bigger(max_length,args[i]->max_length);
3468   DBUG_RETURN(FALSE);
3469 }
3470 
3471 
copy_or_same(THD * thd)3472 Item *Item_sum_udf_str::copy_or_same(THD* thd)
3473 {
3474   return new (thd->mem_root) Item_sum_udf_str(thd, this);
3475 }
3476 
3477 
val_decimal(my_decimal * dec)3478 my_decimal *Item_sum_udf_str::val_decimal(my_decimal *dec)
3479 {
3480   return val_decimal_from_string(dec);
3481 }
3482 
val_str(String * str)3483 String *Item_sum_udf_str::val_str(String *str)
3484 {
3485   DBUG_ASSERT(fixed == 1);
3486   DBUG_ENTER("Item_sum_udf_str::str");
3487   String *res=udf.val_str(str,&str_value);
3488   null_value = !res;
3489   DBUG_RETURN(res);
3490 }
3491 
3492 #endif /* HAVE_DLOPEN */
3493 
3494 
3495 /*****************************************************************************
3496  GROUP_CONCAT function
3497 
3498  SQL SYNTAX:
3499   GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
3500     [SEPARATOR str_const])
3501 
3502  concat of values from "group by" operation
3503 
3504  BUGS
3505    Blobs doesn't work with DISTINCT or ORDER BY
3506 *****************************************************************************/
3507 
3508 
3509 
3510 /**
3511   Compares the values for fields in expr list of GROUP_CONCAT.
3512   @note
3513 
3514      GROUP_CONCAT([DISTINCT] expr [,expr ...]
3515               [ORDER BY {unsigned_integer | col_name | expr}
3516                   [ASC | DESC] [,col_name ...]]
3517               [SEPARATOR str_val])
3518 
3519   @return
3520   @retval -1 : key1 < key2
3521   @retval  0 : key1 = key2
3522   @retval  1 : key1 > key2
3523 */
3524 
3525 extern "C"
group_concat_key_cmp_with_distinct(void * arg,const void * key1,const void * key2)3526 int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
3527                                        const void* key2)
3528 {
3529   Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
3530 
3531   for (uint i= 0; i < item_func->arg_count_field; i++)
3532   {
3533     Item *item= item_func->args[i];
3534     /*
3535       If item is a const item then either get_tmp_table_field returns 0
3536       or it is an item over a const table.
3537     */
3538     if (item->const_item())
3539       continue;
3540     /*
3541       We have to use get_tmp_table_field() instead of
3542       real_item()->get_tmp_table_field() because we want the field in
3543       the temporary table, not the original field
3544     */
3545     Field *field= item->get_tmp_table_field();
3546 
3547     if (!field)
3548       continue;
3549 
3550     uint offset= (field->offset(field->table->record[0]) -
3551                   field->table->s->null_bytes);
3552     int res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset);
3553     if (res)
3554       return res;
3555   }
3556   return 0;
3557 }
3558 
3559 
3560 /*
3561   @brief
3562     Comparator function for DISTINCT clause taking into account NULL values.
3563 
3564   @note
3565     Used for JSON_ARRAYAGG function
3566 */
3567 
group_concat_key_cmp_with_distinct_with_nulls(void * arg,const void * key1_arg,const void * key2_arg)3568 int group_concat_key_cmp_with_distinct_with_nulls(void* arg,
3569                                                   const void* key1_arg,
3570                                                   const void* key2_arg)
3571 {
3572   Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
3573 
3574   uchar *key1= (uchar*)key1_arg + item_func->table->s->null_bytes;
3575   uchar *key2= (uchar*)key2_arg + item_func->table->s->null_bytes;
3576 
3577   /*
3578     JSON_ARRAYAGG function only accepts one argument.
3579   */
3580 
3581   Item *item= item_func->args[0];
3582   /*
3583     If item is a const item then either get_tmp_table_field returns 0
3584     or it is an item over a const table.
3585   */
3586   if (item->const_item())
3587     return 0;
3588   /*
3589     We have to use get_tmp_table_field() instead of
3590     real_item()->get_tmp_table_field() because we want the field in
3591     the temporary table, not the original field
3592   */
3593   Field *field= item->get_tmp_table_field();
3594 
3595   if (!field)
3596     return 0;
3597 
3598   if (field->is_null_in_record((uchar*)key1_arg) &&
3599       field->is_null_in_record((uchar*)key2_arg))
3600     return 0;
3601 
3602   if (field->is_null_in_record((uchar*)key1_arg))
3603     return -1;
3604 
3605   if (field->is_null_in_record((uchar*)key2_arg))
3606     return 1;
3607 
3608   uint offset= (field->offset(field->table->record[0]) -
3609                 field->table->s->null_bytes);
3610   int res= field->cmp(key1 + offset, key2 + offset);
3611   if (res)
3612     return res;
3613   return 0;
3614 }
3615 
3616 
3617 /**
3618   function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
3619 */
3620 
3621 extern "C"
group_concat_key_cmp_with_order(void * arg,const void * key1,const void * key2)3622 int group_concat_key_cmp_with_order(void* arg, const void* key1,
3623                                     const void* key2)
3624 {
3625   Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
3626   ORDER **order_item, **end;
3627 
3628   for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
3629        order_item < end;
3630        order_item++)
3631   {
3632     Item *item= *(*order_item)->item;
3633     /*
3634       If field_item is a const item then either get_tmp_table_field returns 0
3635       or it is an item over a const table.
3636     */
3637     if (item->const_item())
3638       continue;
3639     /*
3640       If item is a const item then either get_tmp_table_field returns 0
3641       or it is an item over a const table.
3642     */
3643     if (item->const_item())
3644       continue;
3645     /*
3646       We have to use get_tmp_table_field() instead of
3647       real_item()->get_tmp_table_field() because we want the field in
3648       the temporary table, not the original field
3649 
3650       Note that for the case of ROLLUP, field may point to another table
3651       tham grp_item->table. This is however ok as the table definitions are
3652       the same.
3653     */
3654     Field *field= item->get_tmp_table_field();
3655     if (!field)
3656       continue;
3657 
3658     uint offset= (field->offset(field->table->record[0]) -
3659                   field->table->s->null_bytes);
3660     int res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset);
3661     if (res)
3662       return ((*order_item)->direction == ORDER::ORDER_ASC) ? res : -res;
3663   }
3664   /*
3665     We can't return 0 because in that case the tree class would remove this
3666     item as double value. This would cause problems for case-changes and
3667     if the returned values are not the same we do the sort on.
3668   */
3669   return 1;
3670 }
3671 
3672 
3673 /*
3674   @brief
3675     Comparator function for ORDER BY clause taking into account NULL values.
3676 
3677   @note
3678     Used for JSON_ARRAYAGG function
3679 */
3680 
group_concat_key_cmp_with_order_with_nulls(void * arg,const void * key1_arg,const void * key2_arg)3681 int group_concat_key_cmp_with_order_with_nulls(void *arg, const void *key1_arg,
3682                                                const void *key2_arg)
3683 {
3684   Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
3685   ORDER **order_item, **end;
3686 
3687   uchar *key1= (uchar*)key1_arg + grp_item->table->s->null_bytes;
3688   uchar *key2= (uchar*)key2_arg + grp_item->table->s->null_bytes;
3689 
3690   for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
3691        order_item < end;
3692        order_item++)
3693   {
3694     Item *item= *(*order_item)->item;
3695     /*
3696       If field_item is a const item then either get_tmp_table_field returns 0
3697       or it is an item over a const table.
3698     */
3699     if (item->const_item())
3700       continue;
3701     /*
3702       We have to use get_tmp_table_field() instead of
3703       real_item()->get_tmp_table_field() because we want the field in
3704       the temporary table, not the original field
3705 
3706       Note that for the case of ROLLUP, field may point to another table
3707       tham grp_item->table. This is however ok as the table definitions are
3708       the same.
3709     */
3710     Field *field= item->get_tmp_table_field();
3711     if (!field)
3712       continue;
3713 
3714     if (field->is_null_in_record((uchar*)key1_arg) &&
3715         field->is_null_in_record((uchar*)key2_arg))
3716       continue;
3717 
3718     if (field->is_null_in_record((uchar*)key1_arg))
3719       return ((*order_item)->direction == ORDER::ORDER_ASC) ?  -1 : 1;
3720 
3721     if (field->is_null_in_record((uchar*)key2_arg))
3722       return ((*order_item)->direction == ORDER::ORDER_ASC) ?  1 :  -1;
3723 
3724     uint offset= (field->offset(field->table->record[0]) -
3725                   field->table->s->null_bytes);
3726     int res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset);
3727     if (res)
3728       return ((*order_item)->direction == ORDER::ORDER_ASC) ? res : -res;
3729   }
3730   /*
3731     We can't return 0 because in that case the tree class would remove this
3732     item as double value. This would cause problems for case-changes and
3733     if the returned values are not the same we do the sort on.
3734   */
3735   return 1;
3736 }
3737 
3738 
report_cut_value_error(THD * thd,uint row_count,const char * fname)3739 static void report_cut_value_error(THD *thd, uint row_count, const char *fname)
3740 {
3741   size_t fn_len= strlen(fname);
3742   char *fname_upper= (char *) my_alloca(fn_len + 1);
3743   if (!fname_upper)
3744     fname_upper= (char*) fname;                 // Out of memory
3745   else
3746     memcpy(fname_upper, fname, fn_len+1);
3747   my_caseup_str(&my_charset_latin1, fname_upper);
3748   push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
3749                       ER_CUT_VALUE_GROUP_CONCAT,
3750                       ER_THD(thd, ER_CUT_VALUE_GROUP_CONCAT),
3751                       row_count, fname_upper);
3752   my_afree(fname_upper);
3753 }
3754 
3755 
cut_max_length(String * result,uint old_length,uint max_length) const3756 void Item_func_group_concat::cut_max_length(String *result,
3757         uint old_length, uint max_length) const
3758 {
3759   const char *ptr= result->ptr();
3760   /*
3761     It's ok to use item->result.length() as the fourth argument
3762     as this is never used to limit the length of the data.
3763     Cut is done with the third argument.
3764   */
3765   size_t add_length= Well_formed_prefix(collation.collation,
3766                                       ptr + old_length,
3767                                       ptr + max_length,
3768                                       result->length()).length();
3769   result->length(old_length + add_length);
3770 }
3771 
3772 
3773 /**
3774   Append data from current leaf to item->result.
3775 */
3776 
3777 extern "C"
dump_leaf_key(void * key_arg,element_count count,void * item_arg)3778 int dump_leaf_key(void* key_arg, element_count count __attribute__((unused)),
3779                   void* item_arg)
3780 {
3781   Item_func_group_concat *item= (Item_func_group_concat *) item_arg;
3782   TABLE *table= item->table;
3783   uint max_length= table->in_use->variables.group_concat_max_len;
3784   String tmp((char *)table->record[1], table->s->reclength,
3785              default_charset_info);
3786   String tmp2;
3787   uchar *key= (uchar *) key_arg;
3788   String *result= &item->result;
3789   Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
3790   uint old_length= result->length();
3791 
3792   ulonglong *offset_limit= &item->copy_offset_limit;
3793   ulonglong *row_limit = &item->copy_row_limit;
3794   if (item->limit_clause && !(*row_limit))
3795   {
3796     item->result_finalized= true;
3797     return 1;
3798   }
3799 
3800   tmp.length(0);
3801 
3802   if (item->limit_clause && (*offset_limit))
3803   {
3804     item->row_count++;
3805     (*offset_limit)--;
3806     return 0;
3807   }
3808 
3809   if (!item->result_finalized)
3810     item->result_finalized= true;
3811   else
3812     result->append(*item->separator);
3813 
3814   for (; arg < arg_end; arg++)
3815   {
3816     String *res;
3817     /*
3818       We have to use get_tmp_table_field() instead of
3819       real_item()->get_tmp_table_field() because we want the field in
3820       the temporary table, not the original field
3821       We also can't use table->field array to access the fields
3822       because it contains both order and arg list fields.
3823      */
3824     if ((*arg)->const_item())
3825       res= item->get_str_from_item(*arg, &tmp);
3826     else
3827     {
3828       Field *field= (*arg)->get_tmp_table_field();
3829       if (field)
3830       {
3831         uint offset= (field->offset(field->table->record[0]) -
3832                       table->s->null_bytes);
3833         DBUG_ASSERT(offset < table->s->reclength);
3834         res= item->get_str_from_field(*arg, field, &tmp, key,
3835                                       offset + item->get_null_bytes());
3836       }
3837       else
3838         res= item->get_str_from_item(*arg, &tmp);
3839     }
3840 
3841     if (res)
3842       result->append(*res);
3843   }
3844 
3845   if (item->limit_clause)
3846     (*row_limit)--;
3847   item->row_count++;
3848 
3849   /* stop if length of result more than max_length */
3850   if (result->length() > max_length)
3851   {
3852     THD *thd= current_thd;
3853     item->cut_max_length(result, old_length, max_length);
3854     item->warning_for_row= TRUE;
3855     report_cut_value_error(thd, item->row_count, item->func_name());
3856 
3857     /**
3858        To avoid duplicated warnings in Item_func_group_concat::val_str()
3859     */
3860     if (table && table->blob_storage)
3861       table->blob_storage->set_truncated_value(false);
3862     return 1;
3863   }
3864   return 0;
3865 }
3866 
3867 
3868 /**
3869   Constructor of Item_func_group_concat.
3870 
3871   @param distinct_arg   distinct
3872   @param select_list    list of expression for show values
3873   @param order_list     list of sort columns
3874   @param separator_arg  string value of separator.
3875 */
3876 
3877 Item_func_group_concat::
Item_func_group_concat(THD * thd,Name_resolution_context * context_arg,bool distinct_arg,List<Item> * select_list,const SQL_I_List<ORDER> & order_list,String * separator_arg,bool limit_clause,Item * row_limit_arg,Item * offset_limit_arg)3878 Item_func_group_concat(THD *thd, Name_resolution_context *context_arg,
3879                        bool distinct_arg, List<Item> *select_list,
3880                        const SQL_I_List<ORDER> &order_list,
3881                        String *separator_arg, bool limit_clause,
3882                        Item *row_limit_arg, Item *offset_limit_arg)
3883   :Item_sum(thd), tmp_table_param(0), separator(separator_arg), tree(0),
3884    unique_filter(NULL), table(0),
3885    order(0), context(context_arg),
3886    arg_count_order(order_list.elements),
3887    arg_count_field(select_list->elements),
3888    row_count(0),
3889    distinct(distinct_arg),
3890    warning_for_row(FALSE), always_null(FALSE),
3891    force_copy_fields(0), row_limit(NULL),
3892    offset_limit(NULL), limit_clause(limit_clause),
3893    copy_offset_limit(0), copy_row_limit(0), original(0)
3894 {
3895   Item *item_select;
3896   Item **arg_ptr;
3897 
3898   quick_group= FALSE;
3899   arg_count= arg_count_field + arg_count_order;
3900 
3901   /*
3902     We need to allocate:
3903     args - arg_count_field+arg_count_order
3904            (for possible order items in temporary tables)
3905     order - arg_count_order
3906   */
3907   if (!(args= (Item**) thd->alloc(sizeof(Item*) * arg_count * 2 +
3908                                   sizeof(ORDER*)*arg_count_order)))
3909     return;
3910 
3911   order= (ORDER**)(args + arg_count);
3912 
3913   /* fill args items of show and sort */
3914   List_iterator_fast<Item> li(*select_list);
3915 
3916   for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
3917     *arg_ptr= item_select;
3918 
3919   if (arg_count_order)
3920   {
3921     ORDER **order_ptr= order;
3922     for (ORDER *order_item= order_list.first;
3923          order_item != NULL;
3924          order_item= order_item->next)
3925     {
3926       (*order_ptr++)= order_item;
3927       *arg_ptr= *order_item->item;
3928       order_item->item= arg_ptr++;
3929     }
3930   }
3931 
3932   /* orig_args is only used for print() */
3933   orig_args= (Item**) (order + arg_count_order);
3934   if (arg_count)
3935     memcpy(orig_args, args, sizeof(Item*) * arg_count);
3936   if (limit_clause)
3937   {
3938     row_limit= row_limit_arg;
3939     offset_limit= offset_limit_arg;
3940   }
3941 }
3942 
3943 
Item_func_group_concat(THD * thd,Item_func_group_concat * item)3944 Item_func_group_concat::Item_func_group_concat(THD *thd,
3945                                                Item_func_group_concat *item)
3946   :Item_sum(thd, item),
3947   tmp_table_param(item->tmp_table_param),
3948   separator(item->separator),
3949   tree(item->tree),
3950   tree_len(item->tree_len),
3951   unique_filter(item->unique_filter),
3952   table(item->table),
3953   context(item->context),
3954   arg_count_order(item->arg_count_order),
3955   arg_count_field(item->arg_count_field),
3956   row_count(item->row_count),
3957   distinct(item->distinct),
3958   warning_for_row(item->warning_for_row),
3959   always_null(item->always_null),
3960   force_copy_fields(item->force_copy_fields),
3961   row_limit(item->row_limit), offset_limit(item->offset_limit),
3962   limit_clause(item->limit_clause),copy_offset_limit(item->copy_offset_limit),
3963   copy_row_limit(item->copy_row_limit), original(item)
3964 {
3965   quick_group= item->quick_group;
3966   result.set_charset(collation.collation);
3967 
3968   /*
3969     Since the ORDER structures pointed to by the elements of the 'order' array
3970     may be modified in find_order_in_list() called from
3971     Item_func_group_concat::setup(), create a copy of those structures so that
3972     such modifications done in this object would not have any effect on the
3973     object being copied.
3974   */
3975   ORDER *tmp;
3976   if (!(tmp= (ORDER *) thd->alloc(sizeof(ORDER *) * arg_count_order +
3977                                   sizeof(ORDER) * arg_count_order)))
3978     return;
3979   order= (ORDER **)(tmp + arg_count_order);
3980   for (uint i= 0; i < arg_count_order; i++, tmp++)
3981   {
3982     /*
3983       Compiler generated copy constructor is used to
3984       to copy all the members of ORDER struct.
3985       It's also necessary to update ORDER::next pointer
3986       so that it points to new ORDER element.
3987     */
3988     new (tmp) st_order(*(item->order[i]));
3989     tmp->next= (i + 1 == arg_count_order) ? NULL : (tmp + 1);
3990     order[i]= tmp;
3991   }
3992 }
3993 
3994 
cleanup()3995 void Item_func_group_concat::cleanup()
3996 {
3997   DBUG_ENTER("Item_func_group_concat::cleanup");
3998   Item_sum::cleanup();
3999 
4000   /*
4001     Free table and tree if they belong to this item (if item have not pointer
4002     to original item from which was made copy => it own its objects )
4003   */
4004   if (!original)
4005   {
4006     delete tmp_table_param;
4007     tmp_table_param= 0;
4008     if (table)
4009     {
4010       THD *thd= table->in_use;
4011       if (table->blob_storage)
4012         delete table->blob_storage;
4013       free_tmp_table(thd, table);
4014       table= 0;
4015       if (tree)
4016       {
4017         delete_tree(tree, 0);
4018         tree= 0;
4019       }
4020       if (unique_filter)
4021       {
4022         delete unique_filter;
4023         unique_filter= NULL;
4024       }
4025     }
4026     DBUG_ASSERT(tree == 0);
4027   }
4028   /*
4029     As the ORDER structures pointed to by the elements of the
4030     'order' array may be modified in find_order_in_list() called
4031     from Item_func_group_concat::setup() to point to runtime
4032     created objects, we need to reset them back to the original
4033     arguments of the function.
4034   */
4035   ORDER **order_ptr= order;
4036   for (uint i= 0; i < arg_count_order; i++)
4037   {
4038     (*order_ptr)->item= &args[arg_count_field + i];
4039     order_ptr++;
4040   }
4041   DBUG_VOID_RETURN;
4042 }
4043 
4044 
copy_or_same(THD * thd)4045 Item *Item_func_group_concat::copy_or_same(THD* thd)
4046 {
4047   return new (thd->mem_root) Item_func_group_concat(thd, this);
4048 }
4049 
4050 
clear()4051 void Item_func_group_concat::clear()
4052 {
4053   result.length(0);
4054   result.copy();
4055   null_value= TRUE;
4056   warning_for_row= FALSE;
4057   result_finalized= false;
4058   if (offset_limit)
4059     copy_offset_limit= offset_limit->val_int();
4060   if (row_limit)
4061     copy_row_limit= row_limit->val_int();
4062   if (tree)
4063   {
4064     reset_tree(tree);
4065     tree_len= 0;
4066   }
4067   if (unique_filter)
4068     unique_filter->reset();
4069   if (table && table->blob_storage)
4070     table->blob_storage->reset();
4071   /* No need to reset the table as we never call write_row */
4072 }
4073 
4074 struct st_repack_tree {
4075   TREE tree;
4076   TABLE *table;
4077   size_t len, maxlen;
4078 };
4079 
4080 extern "C"
copy_to_tree(void * key,element_count count,void * arg)4081 int copy_to_tree(void* key, element_count count __attribute__((unused)),
4082                  void* arg)
4083 {
4084   struct st_repack_tree *st= (struct st_repack_tree*)arg;
4085   TABLE *table= st->table;
4086   Field* field= table->field[0];
4087   const uchar *ptr= field->ptr_in_record((uchar*)key - table->s->null_bytes);
4088   size_t len= (size_t)field->val_int(ptr);
4089 
4090   DBUG_ASSERT(count == 1);
4091   if (!tree_insert(&st->tree, key, 0, st->tree.custom_arg))
4092     return 1;
4093 
4094   st->len += len;
4095   return st->len > st->maxlen;
4096 }
4097 
repack_tree(THD * thd)4098 bool Item_func_group_concat::repack_tree(THD *thd)
4099 {
4100   struct st_repack_tree st;
4101   int size= tree->size_of_element;
4102   if (!tree->offset_to_key)
4103     size-= sizeof(void*);
4104 
4105   init_tree(&st.tree, (size_t) MY_MIN(thd->variables.max_heap_table_size,
4106                                       thd->variables.sortbuff_size/16), 0,
4107             size, get_comparator_function_for_order_by(), NULL,
4108             (void*) this, MYF(MY_THREAD_SPECIFIC));
4109   DBUG_ASSERT(tree->size_of_element == st.tree.size_of_element);
4110   st.table= table;
4111   st.len= 0;
4112   st.maxlen= thd->variables.group_concat_max_len;
4113   tree_walk(tree, &copy_to_tree, &st, left_root_right);
4114   if (st.len <= st.maxlen) // Copying aborted. Must be OOM
4115   {
4116     delete_tree(&st.tree, 0);
4117     return 1;
4118   }
4119   delete_tree(tree, 0);
4120   *tree= st.tree;
4121   tree_len= st.len;
4122   return 0;
4123 }
4124 
4125 
4126 /*
4127   Repacking the tree is expensive. But it keeps the tree small, and
4128   inserting into an unnecessary large tree is also waste of time.
4129 
4130   The following number is best-by-test. Test execution time slowly
4131   decreases up to N=10 (that is, factor=1024) and then starts to increase,
4132   again, very slowly.
4133 */
4134 #define GCONCAT_REPACK_FACTOR 10
4135 
add(bool exclude_nulls)4136 bool Item_func_group_concat::add(bool exclude_nulls)
4137 {
4138   if (always_null && exclude_nulls)
4139     return 0;
4140   copy_fields(tmp_table_param);
4141   if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
4142     return TRUE;
4143 
4144   size_t row_str_len= 0;
4145   StringBuffer<MAX_FIELD_WIDTH> buf;
4146   String *res;
4147   for (uint i= 0; i < arg_count_field; i++)
4148   {
4149     Item *show_item= args[i];
4150     if (show_item->const_item())
4151       continue;
4152 
4153     Field *field= show_item->get_tmp_table_field();
4154     if (field)
4155     {
4156       if (field->is_null_in_record((const uchar*) table->record[0]) &&
4157           exclude_nulls)
4158         return 0;                    // Skip row if it contains null
4159       if (tree && (res= field->val_str(&buf)))
4160         row_str_len+= res->length();
4161     }
4162     else
4163     {
4164       /*
4165         should not reach here, we create temp table for all the arguments of
4166         the group_concat function
4167       */
4168       DBUG_ASSERT(0);
4169     }
4170   }
4171 
4172   null_value= FALSE;
4173   bool row_eligible= TRUE;
4174 
4175   if (distinct)
4176   {
4177     /* Filter out duplicate rows. */
4178     uint count= unique_filter->elements_in_tree();
4179     unique_filter->unique_add(get_record_pointer());
4180     if (count == unique_filter->elements_in_tree())
4181       row_eligible= FALSE;
4182   }
4183 
4184   TREE_ELEMENT *el= 0;                          // Only for safety
4185   if (row_eligible && tree)
4186   {
4187     THD *thd= table->in_use;
4188     table->field[0]->store(row_str_len, FALSE);
4189     if ((tree_len >> GCONCAT_REPACK_FACTOR) > thd->variables.group_concat_max_len
4190         && tree->elements_in_tree > 1)
4191       if (repack_tree(thd))
4192         return 1;
4193     el= tree_insert(tree, get_record_pointer(), 0, tree->custom_arg);
4194     /* check if there was enough memory to insert the row */
4195     if (!el)
4196       return 1;
4197     tree_len+= row_str_len;
4198   }
4199 
4200   /*
4201     In case of GROUP_CONCAT with DISTINCT or ORDER BY (or both) don't dump the
4202     row to the output buffer here. That will be done in val_str.
4203   */
4204   if (row_eligible && !warning_for_row && (!tree && !distinct))
4205     dump_leaf_key(get_record_pointer(), 1, this);
4206 
4207   return 0;
4208 }
4209 
4210 
4211 bool
fix_fields(THD * thd,Item ** ref)4212 Item_func_group_concat::fix_fields(THD *thd, Item **ref)
4213 {
4214   uint i;                       /* for loop variable */
4215   DBUG_ASSERT(fixed == 0);
4216 
4217   if (init_sum_func_check(thd))
4218     return TRUE;
4219 
4220   maybe_null= 1;
4221 
4222   /*
4223     Fix fields for select list and ORDER clause
4224   */
4225 
4226   for (i=0 ; i < arg_count ; i++)
4227   {
4228     if (args[i]->fix_fields_if_needed_for_scalar(thd, &args[i]))
4229       return TRUE;
4230     m_with_subquery|= args[i]->with_subquery();
4231     with_param|= args[i]->with_param;
4232     with_window_func|= args[i]->with_window_func;
4233   }
4234 
4235   /* skip charset aggregation for order columns */
4236   if (agg_arg_charsets_for_string_result(collation,
4237                                          args, arg_count - arg_count_order))
4238     return 1;
4239 
4240   result.set_charset(collation.collation);
4241   result_field= 0;
4242   null_value= 1;
4243   max_length= (uint32)MY_MIN(thd->variables.group_concat_max_len
4244                              / collation.collation->mbminlen
4245                              * collation.collation->mbmaxlen, UINT_MAX32);
4246 
4247   uint32 offset;
4248   if (separator->needs_conversion(separator->length(), separator->charset(),
4249                                   collation.collation, &offset))
4250   {
4251     uint32 buflen= collation.collation->mbmaxlen * separator->length();
4252     uint errors, conv_length;
4253     char *buf;
4254     String *new_separator;
4255 
4256     if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
4257         !(new_separator= new(thd->stmt_arena->mem_root)
4258                            String(buf, buflen, collation.collation)))
4259       return TRUE;
4260 
4261     conv_length= copy_and_convert(buf, buflen, collation.collation,
4262                                   separator->ptr(), separator->length(),
4263                                   separator->charset(), &errors);
4264     new_separator->length(conv_length);
4265     separator= new_separator;
4266   }
4267 
4268   if (check_sum_func(thd, ref))
4269     return TRUE;
4270 
4271   fixed= 1;
4272   return FALSE;
4273 }
4274 
4275 
setup(THD * thd)4276 bool Item_func_group_concat::setup(THD *thd)
4277 {
4278   List<Item> list;
4279   SELECT_LEX *select_lex= thd->lex->current_select;
4280   const bool order_or_distinct= MY_TEST(arg_count_order > 0 || distinct);
4281   DBUG_ENTER("Item_func_group_concat::setup");
4282 
4283   /*
4284     Currently setup() can be called twice. Please add
4285     assertion here when this is fixed.
4286   */
4287   if (table || tree)
4288     DBUG_RETURN(FALSE);
4289 
4290   if (!(tmp_table_param= new TMP_TABLE_PARAM))
4291     DBUG_RETURN(TRUE);
4292 
4293   /* Push all not constant fields to the list and create a temp table */
4294   always_null= 0;
4295   for (uint i= 0; i < arg_count_field; i++)
4296   {
4297     Item *item= args[i];
4298     if (list.push_back(item, thd->mem_root))
4299       DBUG_RETURN(TRUE);
4300     if (item->const_item() && item->is_null() && skip_nulls())
4301     {
4302       always_null= 1;
4303       DBUG_RETURN(FALSE);
4304     }
4305   }
4306 
4307   List<Item> all_fields(list);
4308   /*
4309     Try to find every ORDER expression in the list of GROUP_CONCAT
4310     arguments. If an expression is not found, prepend it to
4311     "all_fields". The resulting field list is used as input to create
4312     tmp table columns.
4313   */
4314   if (arg_count_order)
4315   {
4316     uint n_elems= arg_count_order + all_fields.elements;
4317     ref_pointer_array= static_cast<Item**>(thd->alloc(sizeof(Item*) * n_elems));
4318     if (!ref_pointer_array)
4319       DBUG_RETURN(TRUE);
4320     memcpy(ref_pointer_array, args, arg_count * sizeof(Item*));
4321     DBUG_ASSERT(context);
4322     if (setup_order(thd, Ref_ptr_array(ref_pointer_array, n_elems),
4323                     context->table_list, list,  all_fields, *order))
4324       DBUG_RETURN(TRUE);
4325     /*
4326       Prepend the field to store the length of the string representation
4327       of this row. Used to detect when the tree goes over group_concat_max_len
4328     */
4329     Item *item= new (thd->mem_root)
4330                     Item_uint(thd, thd->variables.group_concat_max_len);
4331     if (!item || all_fields.push_front(item, thd->mem_root))
4332       DBUG_RETURN(TRUE);
4333   }
4334 
4335   count_field_types(select_lex, tmp_table_param, all_fields, 0);
4336   tmp_table_param->force_copy_fields= force_copy_fields;
4337   tmp_table_param->hidden_field_count= (arg_count_order > 0);
4338   DBUG_ASSERT(table == 0);
4339   if (order_or_distinct)
4340   {
4341     /*
4342       Force the create_tmp_table() to convert BIT columns to INT
4343       as we cannot compare two table records containing BIT fields
4344       stored in the the tree used for distinct/order by.
4345       Moreover we don't even save in the tree record null bits
4346       where BIT fields store parts of their data.
4347     */
4348     List_iterator_fast<Item> li(all_fields);
4349     Item *item;
4350     while ((item= li++))
4351     {
4352       if (item->type() == Item::FIELD_ITEM &&
4353           ((Item_field*) item)->field->type() == FIELD_TYPE_BIT)
4354         item->marker= 4;
4355     }
4356   }
4357 
4358   /*
4359     We have to create a temporary table to get descriptions of fields
4360     (types, sizes and so on).
4361 
4362     Note that in the table, we first have the ORDER BY fields, then the
4363     field list.
4364   */
4365   if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
4366                                 (ORDER*) 0, 0, TRUE,
4367                                 (select_lex->options |
4368                                  thd->variables.option_bits),
4369                                 HA_POS_ERROR, &empty_clex_str)))
4370     DBUG_RETURN(TRUE);
4371   table->file->extra(HA_EXTRA_NO_ROWS);
4372   table->no_rows= 1;
4373 
4374   /**
4375     Initialize blob_storage if GROUP_CONCAT is used
4376     with ORDER BY | DISTINCT and BLOB field count > 0.
4377   */
4378   if (order_or_distinct && table->s->blob_fields)
4379     table->blob_storage= new Blob_mem_storage();
4380 
4381   /*
4382      Need sorting or uniqueness: init tree and choose a function to sort.
4383      Don't reserve space for NULLs: if any of gconcat arguments is NULL,
4384      the row is not added to the result.
4385   */
4386   uint tree_key_length= table->s->reclength - table->s->null_bytes;
4387 
4388   if (arg_count_order)
4389   {
4390     tree= &tree_base;
4391     /*
4392       Create a tree for sorting. The tree is used to sort (according to the
4393       syntax of this function). If there is no ORDER BY clause, we don't
4394       create this tree.
4395     */
4396     init_tree(tree, (size_t)MY_MIN(thd->variables.max_heap_table_size,
4397                                    thd->variables.sortbuff_size/16), 0,
4398               tree_key_length + get_null_bytes(),
4399               get_comparator_function_for_order_by(), NULL, (void*) this,
4400               MYF(MY_THREAD_SPECIFIC));
4401     tree_len= 0;
4402   }
4403 
4404   if (distinct)
4405     unique_filter= new Unique(get_comparator_function_for_distinct(),
4406                               (void*)this,
4407                               tree_key_length + get_null_bytes(),
4408                               ram_limitation(thd));
4409   if ((row_limit && row_limit->cmp_type() != INT_RESULT) ||
4410       (offset_limit && offset_limit->cmp_type() != INT_RESULT))
4411   {
4412     my_error(ER_INVALID_VALUE_TO_LIMIT, MYF(0));
4413     DBUG_RETURN(TRUE);
4414   }
4415 
4416   DBUG_RETURN(FALSE);
4417 }
4418 
4419 
4420 /* This is used by rollup to create a separate usable copy of the function */
4421 
make_unique()4422 void Item_func_group_concat::make_unique()
4423 {
4424   tmp_table_param= 0;
4425   table=0;
4426   original= 0;
4427   force_copy_fields= 1;
4428   tree= 0;
4429 }
4430 
4431 
val_str(String * str)4432 String* Item_func_group_concat::val_str(String* str)
4433 {
4434   DBUG_ASSERT(fixed == 1);
4435   if (null_value)
4436     return 0;
4437 
4438   if (!result_finalized) // Result yet to be written.
4439   {
4440     if (tree != NULL) // order by
4441       tree_walk(tree, &dump_leaf_key, this, left_root_right);
4442     else if (distinct) // distinct (and no order by).
4443       unique_filter->walk(table, &dump_leaf_key, this);
4444     else if (row_limit && copy_row_limit == (ulonglong)row_limit->val_int())
4445       return &result;
4446     else
4447       DBUG_ASSERT(false); // Can't happen
4448   }
4449 
4450   if (table && table->blob_storage &&
4451       table->blob_storage->is_truncated_value())
4452   {
4453     warning_for_row= true;
4454     report_cut_value_error(current_thd, row_count, func_name());
4455   }
4456 
4457   return &result;
4458 }
4459 
4460 
4461 /*
4462   @brief
4463     Get the comparator function for DISTINT clause
4464 */
4465 
get_comparator_function_for_distinct()4466 qsort_cmp2 Item_func_group_concat::get_comparator_function_for_distinct()
4467 {
4468   return skip_nulls() ?
4469          group_concat_key_cmp_with_distinct :
4470          group_concat_key_cmp_with_distinct_with_nulls;
4471 }
4472 
4473 
4474 /*
4475   @brief
4476     Get the comparator function for ORDER BY clause
4477 */
4478 
get_comparator_function_for_order_by()4479 qsort_cmp2 Item_func_group_concat::get_comparator_function_for_order_by()
4480 {
4481   return skip_nulls() ?
4482          group_concat_key_cmp_with_order :
4483          group_concat_key_cmp_with_order_with_nulls;
4484 }
4485 
4486 
4487 /*
4488 
4489   @brief
4490     Get the record pointer of the current row of the table
4491 
4492   @details
4493     look at the comments for Item_func_group_concat::get_null_bytes
4494 */
4495 
get_record_pointer()4496 uchar* Item_func_group_concat::get_record_pointer()
4497 {
4498   return skip_nulls() ?
4499          table->record[0] + table->s->null_bytes :
4500          table->record[0];
4501 }
4502 
4503 
4504 /*
4505   @brief
4506     Get the null bytes for the table if required.
4507 
4508   @details
4509     This function is used for GROUP_CONCAT (or JSON_ARRAYAGG) implementation
4510     where the Unique tree or the ORDER BY tree may store the null values,
4511     in such case we also store the null bytes inside each node of the tree.
4512 
4513 */
4514 
get_null_bytes()4515 uint Item_func_group_concat::get_null_bytes()
4516 {
4517   return skip_nulls() ? 0 : table->s->null_bytes;
4518 }
4519 
4520 
print(String * str,enum_query_type query_type)4521 void Item_func_group_concat::print(String *str, enum_query_type query_type)
4522 {
4523   str->append(func_name());
4524   if (distinct)
4525     str->append(STRING_WITH_LEN("distinct "));
4526   for (uint i= 0; i < arg_count_field; i++)
4527   {
4528     if (i)
4529       str->append(',');
4530     orig_args[i]->print(str, query_type);
4531   }
4532   if (arg_count_order)
4533   {
4534     str->append(STRING_WITH_LEN(" order by "));
4535     for (uint i= 0 ; i < arg_count_order ; i++)
4536     {
4537       if (i)
4538         str->append(',');
4539       orig_args[i + arg_count_field]->print(str, query_type);
4540       if (order[i]->direction == ORDER::ORDER_ASC)
4541         str->append(STRING_WITH_LEN(" ASC"));
4542      else
4543         str->append(STRING_WITH_LEN(" DESC"));
4544     }
4545   }
4546 
4547   if (sum_func() == GROUP_CONCAT_FUNC)
4548   {
4549     str->append(STRING_WITH_LEN(" separator \'"));
4550     str->append_for_single_quote(separator->ptr(), separator->length());
4551     str->append(STRING_WITH_LEN("\'"));
4552   }
4553 
4554   if (limit_clause)
4555   {
4556     str->append(STRING_WITH_LEN(" limit "));
4557     if (offset_limit)
4558     {
4559       offset_limit->print(str, query_type);
4560       str->append(',');
4561     }
4562     row_limit->print(str, query_type);
4563   }
4564   str->append(STRING_WITH_LEN(")"));
4565 }
4566 
4567 
~Item_func_group_concat()4568 Item_func_group_concat::~Item_func_group_concat()
4569 {
4570   if (!original && unique_filter)
4571     delete unique_filter;
4572 }
4573