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