drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
item/sum.cc
Go to the documentation of this file.
1/* Copyright (C) 2000-2003 MySQL AB
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 as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16
23#include <config.h>
24#include <cstdio>
25#include <math.h>
26#include <drizzled/sql_select.h>
27#include <drizzled/error.h>
28#include <drizzled/hybrid_type_traits.h>
29#include <drizzled/hybrid_type_traits_integer.h>
30#include <drizzled/hybrid_type_traits_decimal.h>
31#include <drizzled/sql_base.h>
32#include <drizzled/session.h>
33#include <drizzled/item/sum.h>
34#include <drizzled/field/decimal.h>
35#include <drizzled/field/double.h>
36#include <drizzled/field/int64.h>
37#include <drizzled/field/date.h>
38#include <drizzled/field/datetime.h>
39#include <drizzled/unique.h>
40#include <drizzled/type/decimal.h>
41#include <drizzled/internal/m_string.h>
42#include <drizzled/item/subselect.h>
43#include <drizzled/sql_lex.h>
44#include <drizzled/system_variables.h>
45#include <drizzled/create_field.h>
46
47#include <algorithm>
48
49using namespace std;
50
51namespace drizzled {
52
53extern plugin::StorageEngine *heap_engine;
54
76
78{
79 if (!session->lex().allow_sum_func)
80 {
81 my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
82 MYF(0));
83 return true;
84 }
85 /* Set a reference to the nesting set function if there is any */
86 in_sum_func= session->lex().in_sum_func;
87 /* Save a pointer to object to be used in items for nested set functions */
88 session->lex().in_sum_func= this;
89 nest_level= session->lex().current_select->nest_level;
90 ref_by= 0;
91 aggr_level= -1;
92 aggr_sel= NULL;
93 max_arg_level= -1;
94 max_sum_func_level= -1;
95 outer_fields.clear();
96 return false;
97}
98
147
149{
150 bool invalid= false;
151 nesting_map allow_sum_func= session->lex().allow_sum_func;
152 /*
153 The value of max_arg_level is updated if an argument of the set function
154 contains a column reference resolved against a subquery whose level is
155 greater than the current value of max_arg_level.
156 max_arg_level cannot be greater than nest level.
157 nest level is always >= 0
158 */
159 if (nest_level == max_arg_level)
160 {
161 /*
162 The function must be aggregated in the current subquery,
163 If it is there under a construct where it is not allowed
164 we report an error.
165 */
166 invalid= !(allow_sum_func & (1 << max_arg_level));
167 }
168 else if (max_arg_level >= 0 || !(allow_sum_func & (1 << nest_level)))
169 {
170 /*
171 The set function can be aggregated only in outer subqueries.
172 Try to find a subquery where it can be aggregated;
173 If we fail to find such a subquery report an error.
174 */
175 if (register_sum_func(session, ref))
176 return true;
177 invalid= aggr_level < 0 && !(allow_sum_func & (1 << nest_level));
178 if (!invalid && false)
179 invalid= aggr_level < 0 && max_arg_level < nest_level;
180 }
181 if (!invalid && aggr_level < 0)
182 {
183 aggr_level= nest_level;
184 aggr_sel= session->lex().current_select;
185 }
186 /*
187 By this moment we either found a subquery where the set function is
188 to be aggregated and assigned a value that is >= 0 to aggr_level,
189 or set the value of 'invalid' to TRUE to report later an error.
190 */
191 /*
192 Additionally we have to check whether possible nested set functions
193 are acceptable here: they are not, if the level of aggregation of
194 some of them is less than aggr_level.
195 */
196 if (!invalid)
197 invalid= aggr_level <= max_sum_func_level;
198 if (invalid)
199 {
200 my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
201 MYF(0));
202 return true;
203 }
204
205 if (in_sum_func)
206 {
207 /*
208 If the set function is nested adjust the value of
209 max_sum_func_level for the nesting set function.
210 We take into account only enclosed set functions that are to be
211 aggregated on the same level or above of the nest level of
212 the enclosing set function.
213 But we must always pass up the max_sum_func_level because it is
214 the maximum nested level of all directly and indirectly enclosed
215 set functions. We must do that even for set functions that are
216 aggregated inside of their enclosing set function's nest level
217 because the enclosing function may contain another enclosing
218 function that is to be aggregated outside or on the same level
219 as its parent's nest level.
220 */
221 if (in_sum_func->nest_level >= aggr_level)
222 set_if_bigger(in_sum_func->max_sum_func_level, aggr_level);
223 set_if_bigger(in_sum_func->max_sum_func_level, max_sum_func_level);
224 }
225
226 /*
227 Check that non-aggregated fields and sum functions aren't mixed in the
228 same select in the ONLY_FULL_GROUP_BY mode.
229 */
230 if (outer_fields.size())
231 {
232 Item_field *field;
233 /*
234 Here we compare the nesting level of the select to which an outer field
235 belongs to with the aggregation level of the sum function. All fields in
236 the outer_fields list are checked.
237
238 If the nesting level is equal to the aggregation level then the field is
239 aggregated by this sum function.
240 If the nesting level is less than the aggregation level then the field
241 belongs to an outer select. In this case if there is an embedding sum
242 function add current field to functions outer_fields list. If there is
243 no embedding function then the current field treated as non aggregated
244 and the select it belongs to is marked accordingly.
245 If the nesting level is greater than the aggregation level then it means
246 that this field was added by an inner sum function.
247 Consider an example:
248
249 select avg ( <-- we are here, checking outer.f1
250 select (
251 select sum(outer.f1 + inner.f1) from inner
252 ) from outer)
253 from most_outer;
254
255 In this case we check that no aggregate functions are used in the
256 select the field belongs to. If there are some then an error is
257 raised.
258 */
259 List<Item_field>::iterator of(outer_fields.begin());
260 while ((field= of++))
261 {
262 Select_Lex *sel= field->cached_table->select_lex;
263 if (sel->nest_level < aggr_level)
264 {
265 if (in_sum_func)
266 {
267 /*
268 Let upper function decide whether this field is a non
269 aggregated one.
270 */
271 in_sum_func->outer_fields.push_back(field);
272 }
273 else
274 {
275 sel->full_group_by_flag.set(NON_AGG_FIELD_USED);
276 }
277 }
278 if (sel->nest_level > aggr_level &&
279 (sel->full_group_by_flag.test(SUM_FUNC_USED)) &&
280 ! sel->group_list.elements)
281 {
282 my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
283 ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
284 return true;
285 }
286 }
287 }
288 aggr_sel->full_group_by_flag.set(SUM_FUNC_USED);
289 update_used_tables();
290 session->lex().in_sum_func= in_sum_func;
291 return false;
292}
293
318
320{
321 Select_Lex *sl;
322 nesting_map allow_sum_func= session->lex().allow_sum_func;
323 for (sl= session->lex().current_select->master_unit()->outer_select() ;
324 sl && sl->nest_level > max_arg_level;
325 sl= sl->master_unit()->outer_select() )
326 {
327 if (aggr_level < 0 && (allow_sum_func & (1 << sl->nest_level)))
328 {
329 /* Found the most nested subquery where the function can be aggregated */
330 aggr_level= sl->nest_level;
331 aggr_sel= sl;
332 }
333 }
334 if (sl && (allow_sum_func & (1 << sl->nest_level)))
335 {
336 /*
337 We reached the subquery of level max_arg_level and checked
338 that the function can be aggregated here.
339 The set function will be aggregated in this subquery.
340 */
341 aggr_level= sl->nest_level;
342 aggr_sel= sl;
343
344 }
345 if (aggr_level >= 0)
346 {
347 ref_by= ref;
348 /* Add the object to the list of registered objects assigned to aggr_sel */
349 if (!aggr_sel->inner_sum_func_list)
350 next= this;
351 else
352 {
353 next= aggr_sel->inner_sum_func_list->next;
354 aggr_sel->inner_sum_func_list->next= this;
355 }
356 aggr_sel->inner_sum_func_list= this;
357 aggr_sel->with_sum_func= 1;
358
359 /*
360 Mark Item_subselect(s) as containing aggregate function all the way up
361 to aggregate function's calculation context.
362 Note that we must not mark the Item of calculation context itself
363 because with_sum_func on the calculation context Select_Lex is
364 already set above.
365
366 with_sum_func being set for an Item means that this Item refers
367 (somewhere in it, e.g. one of its arguments if it's a function) directly
368 or through intermediate items to an aggregate function that is calculated
369 in a context "outside" of the Item (e.g. in the current or outer select).
370
371 with_sum_func being set for an Select_Lex means that this Select_Lex
372 has aggregate functions directly referenced (i.e. not through a sub-select).
373 */
374 for (sl= session->lex().current_select;
375 sl && sl != aggr_sel && sl->master_unit()->item;
376 sl= sl->master_unit()->outer_select() )
377 sl->master_unit()->item->with_sum_func= 1;
378 }
379 session->lex().current_select->mark_as_dependent(aggr_sel);
380 return false;
381}
382
383
384Item_sum::Item_sum(List<Item> &list) :arg_count(list.size()),
385 forced_const(false)
386{
387 if ((args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
388 {
389 uint32_t i=0;
390 List<Item>::iterator li(list.begin());
391 Item *item;
392
393 while ((item=li++))
394 {
395 args[i++]= item;
396 }
397 }
398 mark_as_sum_func();
399 list.clear(); // Fields are used
400}
401
402
406
407Item_sum::Item_sum(Session *session, Item_sum *item):
408 Item_result_field(session, item), arg_count(item->arg_count),
409 aggr_sel(item->aggr_sel),
410 nest_level(item->nest_level), aggr_level(item->aggr_level),
411 quick_group(item->quick_group), used_tables_cache(item->used_tables_cache),
412 forced_const(item->forced_const)
413{
414 if (arg_count <= 2)
415 args= tmp_args;
416 else
417 args= new (session->mem) Item*[arg_count];
418 memcpy(args, item->args, sizeof(Item*)*arg_count);
419}
420
421
422void Item_sum::mark_as_sum_func()
423{
424 Select_Lex *cur_select= getSession().lex().current_select;
425 cur_select->n_sum_items++;
426 cur_select->with_sum_func= 1;
427 with_sum_func= 1;
428}
429
430
431void Item_sum::make_field(SendField *tmp_field)
432{
433 if (args[0]->type() == Item::FIELD_ITEM && keep_field_type())
434 {
435 ((Item_field*) args[0])->field->make_field(tmp_field);
436 /* For expressions only col_name should be non-empty string. */
437 tmp_field->db_name= "";
438 tmp_field->org_table_name= "";
439 tmp_field->table_name= "";
440 tmp_field->org_col_name= "";
441 tmp_field->col_name= name;
442 if (maybe_null)
443 tmp_field->flags&= ~NOT_NULL_FLAG;
444 }
445 else
446 init_make_field(tmp_field, field_type());
447}
448
449
451{
452 str->append(func_name(), strlen(func_name()));
453 for (uint32_t i=0 ; i < arg_count ; i++)
454 {
455 if (i)
456 {
457 str->append(',');
458 }
459 args[i]->print(str);
460 }
461 str->append(')');
462}
463
464void Item_sum::fix_num_length_and_dec()
465{
466 decimals= 0;
467 for (uint32_t i=0 ; i < arg_count ; i++)
468 {
469 set_if_bigger(decimals,args[i]->decimals);
470 }
471 max_length= float_length(decimals);
472}
473
474Item *Item_sum::get_tmp_table_item(Session *session)
475{
476 Item_sum* sum_item= (Item_sum *) copy_or_same(session);
477 if (sum_item && sum_item->result_field) // If not a const sum func
478 {
479 Field *result_field_tmp= sum_item->result_field;
480 for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
481 {
482 Item *arg= sum_item->args[i];
483 if (!arg->const_item())
484 {
485 if (arg->type() == Item::FIELD_ITEM)
486 ((Item_field*) arg)->field= result_field_tmp++;
487 else
488 sum_item->args[i]= new Item_field(result_field_tmp++);
489 }
490 }
491 }
492 return sum_item;
493}
494
495
496bool Item_sum::walk (Item_processor processor, bool walk_subquery,
497 unsigned char *argument)
498{
499 if (arg_count)
500 {
501 Item **arg,**arg_end;
502 for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
503 {
504 if ((*arg)->walk(processor, walk_subquery, argument))
505 return 1;
506 }
507 }
508 return (this->*processor)(argument);
509}
510
511
512Field *Item_sum::create_tmp_field(bool ,
513 Table *table,
514 uint32_t convert_blob_length)
515{
516 Field *field= NULL;
517
518 switch (result_type()) {
519 case REAL_RESULT:
520 field= new Field_double(max_length, maybe_null, name, decimals, true);
521 break;
522
523 case INT_RESULT:
524 field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
525 break;
526
527 case STRING_RESULT:
528 if (max_length/collation.collation->mbmaxlen <= 255 ||
529 convert_blob_length > Field_varstring::MAX_SIZE ||
530 !convert_blob_length)
531 {
532 return make_string_field(table);
533 }
534
535 table->setVariableWidth();
536 field= new Field_varstring(convert_blob_length, maybe_null,
537 name, collation.collation);
538 break;
539
540 case DECIMAL_RESULT:
541 field= new Field_decimal(max_length, maybe_null, name,
542 decimals, unsigned_flag);
543 break;
544
545 case ROW_RESULT:
546 // This case should never be choosen
547 assert(0);
548 return 0;
549 }
550
551 if (field)
552 field->init(table);
553
554 return field;
555}
556
557
558void Item_sum::update_used_tables ()
559{
560 if (!forced_const)
561 {
562 used_tables_cache= 0;
563 for (uint32_t i=0 ; i < arg_count ; i++)
564 {
565 args[i]->update_used_tables();
566 used_tables_cache|= args[i]->used_tables();
567 }
568
569 used_tables_cache&= PSEUDO_TABLE_BITS;
570
571 /* the aggregate function is aggregated into its local context */
572 used_tables_cache |= (1 << aggr_sel->join->tables) - 1;
573 }
574}
575
576
577String *
579{
580 return val_string_from_real(str);
581}
582
583
585{
586 assert(fixed == 1);
587 return (int64_t) rint(val_real()); /* Real as default */
588}
589
590
592{
593 return val_decimal_from_real(decimal_value);
594}
595
596
597String *
599{
600 return val_string_from_int(str);
601}
602
603
605{
606 return val_decimal_from_int(decimal_value);
607}
608
609
610bool
611Item_sum_num::fix_fields(Session *session, Item **ref)
612{
613 assert(fixed == 0);
614
615 if (init_sum_func_check(session))
616 return true;
617
618 decimals=0;
619 maybe_null=0;
620 for (uint32_t i=0 ; i < arg_count ; i++)
621 {
622 if (args[i]->fix_fields(session, args + i) || args[i]->check_cols(1))
623 return true;
624 set_if_bigger(decimals, args[i]->decimals);
625 maybe_null |= args[i]->maybe_null;
626 }
627 result_field=0;
628 max_length=float_length(decimals);
629 null_value=1;
630 fix_length_and_dec();
631
632 if (check_sum_func(session, ref))
633 return true;
634
635 fixed= 1;
636 return false;
637}
638
639
640Item_sum_hybrid::Item_sum_hybrid(Session *session, Item_sum_hybrid *item)
641 :Item_sum(session, item), value(item->value), hybrid_type(item->hybrid_type),
642 hybrid_field_type(item->hybrid_field_type), cmp_sign(item->cmp_sign),
643 was_values(item->was_values)
644{
645 /* copy results from old value */
646 switch (hybrid_type) {
647 case INT_RESULT:
648 sum_int= item->sum_int;
649 break;
650 case DECIMAL_RESULT:
651 class_decimal2decimal(&item->sum_dec, &sum_dec);
652 break;
653 case REAL_RESULT:
654 sum= item->sum;
655 break;
656 case STRING_RESULT:
657 /*
658 This can happen with ROLLUP. Note that the value is already
659 copied at function call.
660 */
661 break;
662 case ROW_RESULT:
663 assert(0);
664 }
665 collation.set(item->collation);
666}
667
668bool
669Item_sum_hybrid::fix_fields(Session *session, Item **ref)
670{
671 assert(fixed == 0);
672
673 Item *item= args[0];
674
675 if (init_sum_func_check(session))
676 return true;
677
678 // 'item' can be changed during fix_fields
679 if ((!item->fixed && item->fix_fields(session, args)) ||
680 (item= args[0])->check_cols(1))
681 return true;
682 decimals=item->decimals;
683
684 switch (hybrid_type= item->result_type()) {
685 case INT_RESULT:
686 max_length= 20;
687 sum_int= 0;
688 break;
689 case DECIMAL_RESULT:
690 max_length= item->max_length;
691 sum_dec.set_zero();
692 break;
693 case REAL_RESULT:
694 max_length= float_length(decimals);
695 sum= 0.0;
696 break;
697 case STRING_RESULT:
698 max_length= item->max_length;
699 break;
700 case ROW_RESULT:
701 assert(0);
702 };
703 /* MIN/MAX can return NULL for empty set indepedent of the used column */
704 maybe_null= 1;
705 unsigned_flag=item->unsigned_flag;
706 collation.set(item->collation);
707 result_field=0;
708 null_value=1;
709 fix_length_and_dec();
710 item= item->real_item();
711 if (item->type() == Item::FIELD_ITEM)
712 hybrid_field_type= ((Item_field*) item)->field->type();
713 else
714 hybrid_field_type= Item::field_type();
715
716 if (check_sum_func(session, ref))
717 return true;
718
719 fixed= 1;
720 return false;
721}
722
723Field *Item_sum_hybrid::create_tmp_field(bool group, Table *table,
724 uint32_t convert_blob_length)
725{
726 Field *field;
727 if (args[0]->type() == Item::FIELD_ITEM)
728 {
729 field= ((Item_field*) args[0])->field;
730
731 if ((field= create_tmp_field_from_field(&getSession(), field, name, table,
732 NULL, convert_blob_length)))
733 field->flags&= ~NOT_NULL_FLAG;
734 return field;
735 }
736 /*
737 DATE/TIME fields have STRING_RESULT result types.
738 In order to preserve field type, it's needed to handle DATE/TIME
739 fields creations separately.
740 */
741 switch (args[0]->field_type()) {
742 case DRIZZLE_TYPE_DATE:
743 field= new Field_date(maybe_null, name);
744 break;
745 case DRIZZLE_TYPE_TIMESTAMP:
746 case DRIZZLE_TYPE_DATETIME:
747 field= new Field_datetime(maybe_null, name);
748 break;
749 default:
750 return Item_sum::create_tmp_field(group, table, convert_blob_length);
751 }
752
753 if (field)
754 field->init(table);
755
756 return field;
757}
758
759
760/***********************************************************************
761** reset and add of sum_func
762***********************************************************************/
763
768Item_sum_sum::Item_sum_sum(Session *session, Item_sum_sum *item)
769 :Item_sum_num(session, item), hybrid_type(item->hybrid_type),
770 curr_dec_buff(item->curr_dec_buff)
771{
772 /* TODO: check if the following assignments are really needed */
773 if (hybrid_type == DECIMAL_RESULT)
774 {
775 class_decimal2decimal(item->dec_buffs, dec_buffs);
776 class_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
777 }
778 else
779 sum= item->sum;
780}
781
782Item *Item_sum_sum::copy_or_same(Session* session)
783{
784 return new (session->mem) Item_sum_sum(session, this);
785}
786
787
788void Item_sum_sum::clear()
789{
790 null_value=1;
791 if (hybrid_type == DECIMAL_RESULT)
792 {
793 curr_dec_buff= 0;
794 dec_buffs->set_zero();
795 }
796 else
797 sum= 0.0;
798 return;
799}
800
801
802void Item_sum_sum::fix_length_and_dec()
803{
805 decimals= args[0]->decimals;
806 switch (args[0]->result_type()) {
807 case REAL_RESULT:
808 case STRING_RESULT:
809 hybrid_type= REAL_RESULT;
810 sum= 0.0;
811 break;
812 case INT_RESULT:
813 case DECIMAL_RESULT:
814 {
815 /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
816 int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
817 max_length= class_decimal_precision_to_length(precision, decimals,
818 unsigned_flag);
819 curr_dec_buff= 0;
820 hybrid_type= DECIMAL_RESULT;
821 dec_buffs->set_zero();
822 break;
823 }
824 case ROW_RESULT:
825 assert(0);
826 }
827}
828
829
830bool Item_sum_sum::add()
831{
832 if (hybrid_type == DECIMAL_RESULT)
833 {
834 type::Decimal value, *val= args[0]->val_decimal(&value);
835 if (!args[0]->null_value)
836 {
837 class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
838 val, dec_buffs + curr_dec_buff);
839 curr_dec_buff^= 1;
840 null_value= 0;
841 }
842 }
843 else
844 {
845 sum+= args[0]->val_real();
846 if (!args[0]->null_value)
847 null_value= 0;
848 }
849 return 0;
850}
851
852
854{
855 assert(fixed == 1);
856 if (hybrid_type == DECIMAL_RESULT)
857 {
858 int64_t result;
859 (dec_buffs + curr_dec_buff)->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
860 return result;
861 }
862 return (int64_t) rint(val_real());
863}
864
865
867{
868 assert(fixed == 1);
869 if (hybrid_type == DECIMAL_RESULT)
870 class_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
871 return sum;
872}
873
874
876{
877 if (hybrid_type == DECIMAL_RESULT)
878 return val_string_from_decimal(str);
879 return val_string_from_real(str);
880}
881
882
884{
885 if (hybrid_type == DECIMAL_RESULT)
886 return (dec_buffs + curr_dec_buff);
887 return val_decimal_from_real(val);
888}
889
890/***************************************************************************/
891
892/* Declarations for auxilary C-callbacks */
893
894static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
895{
896 return memcmp(key1, key2, *(uint32_t *) arg);
897}
898
899
900static int item_sum_distinct_walk(void *element,
901 uint32_t ,
902 void *item)
903{
904 return ((Item_sum_distinct*) (item))->unique_walk_function(element);
905}
906
907/* Item_sum_distinct */
908
909Item_sum_distinct::Item_sum_distinct(Item *item_arg)
910 :Item_sum_num(item_arg), tree(0)
911{
912 /*
913 quick_group is an optimizer hint, which means that GROUP BY can be
914 handled with help of index on grouped columns.
915 By setting quick_group to zero we force creation of temporary table
916 to perform GROUP BY.
917 */
918 quick_group= 0;
919}
920
921
922Item_sum_distinct::Item_sum_distinct(Session *session, Item_sum_distinct *original)
923 :Item_sum_num(session, original), val(original->val), tree(0),
924 table_field_type(original->table_field_type)
925{
926 quick_group= 0;
927}
928
929
937struct Hybrid_type_traits_fast_decimal: public
938 Hybrid_type_traits_integer
939{
940 virtual Item_result type() const { return DECIMAL_RESULT; }
941 virtual void fix_length_and_dec(Item *item, Item *arg) const
942 { Hybrid_type_traits_decimal::instance()->fix_length_and_dec(item, arg); }
943
944 virtual void div(Hybrid_type *val, uint64_t u) const
945 {
946 int2_class_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
947 val->used_dec_buf_no= 0;
948 val->traits= Hybrid_type_traits_decimal::instance();
949 val->traits->div(val, u);
950 }
951 static const Hybrid_type_traits_fast_decimal *instance();
952 Hybrid_type_traits_fast_decimal() {};
953};
954
955static const Hybrid_type_traits_fast_decimal fast_decimal_traits_instance;
956
958 *Hybrid_type_traits_fast_decimal::instance()
959{
960 return &fast_decimal_traits_instance;
961}
962
963
964void Item_sum_distinct::fix_length_and_dec()
965{
966 assert(args[0]->fixed);
967
968 null_value= maybe_null= true;
969 table_field_type= args[0]->field_type();
970
971 /* Adjust tmp table type according to the chosen aggregation type */
972 switch (args[0]->result_type()) {
973 case STRING_RESULT:
974 case REAL_RESULT:
975 val.traits= Hybrid_type_traits::instance();
976 table_field_type= DRIZZLE_TYPE_DOUBLE;
977 break;
978 case INT_RESULT:
979 /*
980 Preserving int8, int16, int32 field types gives ~10% performance boost
981 as the size of result tree becomes significantly smaller.
982 Another speed up we gain by using int64_t for intermediate
983 calculations. The range of int64 is enough to hold sum 2^32 distinct
984 integers each <= 2^32.
985 */
986 if (table_field_type == DRIZZLE_TYPE_LONG)
987 {
988 val.traits= Hybrid_type_traits_fast_decimal::instance();
989 break;
990 }
991 table_field_type= DRIZZLE_TYPE_LONGLONG;
992 /* fallthrough */
993 case DECIMAL_RESULT:
994 val.traits= Hybrid_type_traits_decimal::instance();
995 if (table_field_type != DRIZZLE_TYPE_LONGLONG)
996 table_field_type= DRIZZLE_TYPE_DECIMAL;
997 break;
998 case ROW_RESULT:
999 assert(0);
1000 }
1001
1002 val.traits->fix_length_and_dec(this, args[0]);
1003}
1004
1005
1006enum Item_result Item_sum_distinct::result_type () const
1007{
1008 return val.traits->type();
1009}
1010
1011
1017{
1018 /* It's legal to call setup() more than once when in a subquery */
1019 if (tree)
1020 return false;
1021
1022 /*
1023 Virtual table and the tree are created anew on each re-execution of
1024 PS/SP. Hence all further allocations are performed in the runtime
1025 mem.
1026 */
1028 quick_group= 0;
1029
1030 assert(args[0]->fixed);
1031
1032 std::list<CreateField> field_list;
1033 field_list.push_back(CreateField());
1034 CreateField& field_def = field_list.back();
1035 field_def.init_for_tmp_table(table_field_type, args[0]->max_length, args[0]->decimals, args[0]->maybe_null);
1036 table= &session->getInstanceTable(field_list);
1037
1038 /* XXX: check that the case of CHAR(0) works OK */
1039 tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
1040
1041 /*
1042 Unique handles all unique elements in a tree until they can't fit
1043 in. Then the tree is dumped to the temporary file. We can use
1044 simple_raw_key_cmp because the table contains numbers only; decimals
1045 are converted to binary representation as well.
1046 */
1047 tree= new Unique(simple_raw_key_cmp, &tree_key_length, tree_key_length, (size_t)session->variables.max_heap_table_size);
1048 is_evaluated= false;
1049 return false;
1050}
1051
1052bool Item_sum_distinct::add()
1053{
1054 args[0]->save_in_field(table->getField(0), false);
1055 is_evaluated= false;
1056 if (!table->getField(0)->is_null())
1057 {
1058 assert(tree);
1059 null_value= 0;
1060 /*
1061 '0' values are also stored in the tree. This doesn't matter
1062 for SUM(DISTINCT), but is important for AVG(DISTINCT)
1063 */
1064 return tree->unique_add(table->getField(0)->ptr);
1065 }
1066 return 0;
1067}
1068
1069
1070bool Item_sum_distinct::unique_walk_function(void *element)
1071{
1072 memcpy(table->getField(0)->ptr, element, tree_key_length);
1073 ++count;
1074 val.traits->add(&val, table->getField(0));
1075 return 0;
1076}
1077
1078
1079void Item_sum_distinct::clear()
1080{
1081 assert(tree != 0); /* we always have a tree */
1082 null_value= 1;
1083 tree->reset();
1084 is_evaluated= false;
1085 return;
1086}
1087
1088void Item_sum_distinct::cleanup()
1089{
1090 Item_sum_num::cleanup();
1091 delete tree;
1092 tree= 0;
1093 table= 0;
1094 is_evaluated= false;
1095}
1096
1097Item_sum_distinct::~Item_sum_distinct()
1098{
1099 delete tree;
1100 /* no need to free the table */
1101}
1102
1103
1104void Item_sum_distinct::calculate_val_and_count()
1105{
1106 if (!is_evaluated)
1107 {
1108 count= 0;
1109 val.traits->set_zero(&val);
1110 /*
1111 We don't have a tree only if 'setup()' hasn't been called;
1112 this is the case of sql_select.cc:return_zero_rows.
1113 */
1114 if (tree)
1115 {
1116 table->getField(0)->set_notnull();
1117 tree->walk(item_sum_distinct_walk, (void*) this);
1118 }
1119 is_evaluated= true;
1120 }
1121}
1122
1123
1125{
1126 calculate_val_and_count();
1127 return val.traits->val_real(&val);
1128}
1129
1130
1132{
1133 calculate_val_and_count();
1134 if (null_value)
1135 return 0;
1136 return val.traits->val_decimal(&val, to);
1137}
1138
1139
1141{
1142 calculate_val_and_count();
1143 return val.traits->val_int(&val, unsigned_flag);
1144}
1145
1146
1148{
1149 calculate_val_and_count();
1150 if (null_value)
1151 return 0;
1152 return val.traits->val_str(&val, str, decimals);
1153}
1154
1155/* end of Item_sum_distinct */
1156
1157/* Item_sum_avg_distinct */
1158
1159void
1160Item_sum_avg_distinct::fix_length_and_dec()
1161{
1162 Item_sum_distinct::fix_length_and_dec();
1163 prec_increment= getSession().variables.div_precincrement;
1164 /*
1165 AVG() will divide val by count. We need to reserve digits
1166 after decimal point as the result can be fractional.
1167 */
1168 decimals= min(decimals + prec_increment, (unsigned int)NOT_FIXED_DEC);
1169}
1170
1171
1172void
1173Item_sum_avg_distinct::calculate_val_and_count()
1174{
1175 if (!is_evaluated)
1176 {
1177 Item_sum_distinct::calculate_val_and_count();
1178 if (count)
1179 val.traits->div(&val, count);
1180 is_evaluated= true;
1181 }
1182}
1183
1184
1185Item *Item_sum_count::copy_or_same(Session* session)
1186{
1187 return new (session->mem) Item_sum_count(session, this);
1188}
1189
1190
1191void Item_sum_count::clear()
1192{
1193 count= 0;
1194}
1195
1196
1197bool Item_sum_count::add()
1198{
1199 if (!args[0]->maybe_null || !args[0]->is_null())
1200 count++;
1201 return 0;
1202}
1203
1205{
1206 assert(fixed == 1);
1207 return (int64_t) count;
1208}
1209
1210
1211void Item_sum_count::cleanup()
1212{
1213 count= 0;
1214 Item_sum_int::cleanup();
1215 return;
1216}
1217
1218
1219/*
1220 Avgerage
1221*/
1222void Item_sum_avg::fix_length_and_dec()
1223{
1224 Item_sum_sum::fix_length_and_dec();
1226 prec_increment= getSession().variables.div_precincrement;
1227
1228 if (hybrid_type == DECIMAL_RESULT)
1229 {
1230 int precision= args[0]->decimal_precision() + prec_increment;
1231 decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1232 max_length= class_decimal_precision_to_length(precision, decimals,
1233 unsigned_flag);
1234 f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1235 f_scale= args[0]->decimals;
1236 dec_bin_size= class_decimal_get_binary_size(f_precision, f_scale);
1237 }
1238 else {
1239 decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1240 max_length= args[0]->max_length + prec_increment;
1241 }
1242}
1243
1244
1245Item *Item_sum_avg::copy_or_same(Session* session)
1246{
1247 return new (session->mem) Item_sum_avg(session, this);
1248}
1249
1250
1251Field *Item_sum_avg::create_tmp_field(bool group, Table *table,
1252 uint32_t )
1253{
1254 Field *field;
1255 if (group)
1256 {
1257 /*
1258 We must store both value and counter in the temporary table in one field.
1259 The easiest way is to do this is to store both value in a string
1260 and unpack on access.
1261 */
1262 table->setVariableWidth();
1263 field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
1264 dec_bin_size : sizeof(double)) + sizeof(int64_t),
1265 0, name, &my_charset_bin);
1266 }
1267 else if (hybrid_type == DECIMAL_RESULT)
1268 field= new Field_decimal(max_length, maybe_null, name,
1269 decimals, unsigned_flag);
1270 else
1271 field= new Field_double(max_length, maybe_null, name, decimals, true);
1272 if (field)
1273 field->init(table);
1274 return field;
1275}
1276
1277
1278void Item_sum_avg::clear()
1279{
1280 Item_sum_sum::clear();
1281 count=0;
1282}
1283
1284
1285bool Item_sum_avg::add()
1286{
1287 if (Item_sum_sum::add())
1288 return true;
1289 if (!args[0]->null_value)
1290 count++;
1291 return false;
1292}
1293
1295{
1296 assert(fixed == 1);
1297 if (!count)
1298 {
1299 null_value=1;
1300 return 0.0;
1301 }
1302 return Item_sum_sum::val_real() / uint64_t2double(count);
1303}
1304
1305
1307{
1308 return (int64_t) rint(val_real());
1309}
1310
1311
1313{
1314 type::Decimal sum_buff, cnt;
1315 const type::Decimal *sum_dec;
1316 assert(fixed == 1);
1317 if (!count)
1318 {
1319 null_value=1;
1320 return NULL;
1321 }
1322
1323 /*
1324 For non-DECIMAL hybrid_type the division will be done in
1325 Item_sum_avg::val_real().
1326 */
1327 if (hybrid_type != DECIMAL_RESULT)
1328 return val_decimal_from_real(val);
1329
1330 sum_dec= dec_buffs + curr_dec_buff;
1331 int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
1332 class_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1333 return val;
1334}
1335
1336
1338{
1339 if (hybrid_type == DECIMAL_RESULT)
1340 return val_string_from_decimal(str);
1341 return val_string_from_real(str);
1342}
1343
1344
1345/*
1346 Standard deviation
1347*/
1348
1350{
1351 assert(fixed == 1);
1352 double nr= Item_sum_variance::val_real();
1353 assert(nr >= 0.0);
1354 return sqrt(nr);
1355}
1356
1357Item *Item_sum_std::copy_or_same(Session* session)
1358{
1359 return new (session->mem) Item_sum_std(session, this);
1360}
1361
1362
1363/*
1364 Variance
1365*/
1366
1367
1373
1374/*
1375 These two functions are used by the Item_sum_variance and the
1376 Item_variance_field classes, which are unrelated, and each need to calculate
1377 variance. The difference between the two classes is that the first is used
1378 for a mundane SELECT, while the latter is used in a GROUPing SELECT.
1379*/
1380static void variance_fp_recurrence_next(double *m, double *s, uint64_t *count, double nr)
1381{
1382 *count += 1;
1383
1384 if (*count == 1)
1385 {
1386 *m= nr;
1387 *s= 0;
1388 }
1389 else
1390 {
1391 double m_kminusone= *m;
1392 *m= m_kminusone + (nr - m_kminusone) / (double) *count;
1393 *s= *s + (nr - m_kminusone) * (nr - *m);
1394 }
1395}
1396
1397
1398static double variance_fp_recurrence_result(double s, uint64_t count, bool is_sample_variance)
1399{
1400 if (count == 1)
1401 return 0.0;
1402
1403 if (is_sample_variance)
1404 return s / (count - 1);
1405
1406 /* else, is a population variance */
1407 return s / count;
1408}
1409
1410
1411Item_sum_variance::Item_sum_variance(Session *session, Item_sum_variance *item):
1412 Item_sum_num(session, item), hybrid_type(item->hybrid_type),
1413 count(item->count), sample(item->sample),
1414 prec_increment(item->prec_increment)
1415{
1416 recurrence_m= item->recurrence_m;
1417 recurrence_s= item->recurrence_s;
1418}
1419
1420
1421void Item_sum_variance::fix_length_and_dec()
1422{
1423 maybe_null= null_value= 1;
1424 prec_increment= getSession().variables.div_precincrement;
1425
1426 /*
1427 According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1428 aggregate function; paragraph 7h of Syntax Rules), "the declared
1429 type of the result is an implementation-defined aproximate numeric
1430 type.
1431 */
1432 hybrid_type= REAL_RESULT;
1433
1434 switch (args[0]->result_type()) {
1435 case REAL_RESULT:
1436 case STRING_RESULT:
1437 decimals= min(args[0]->decimals + 4, (int)NOT_FIXED_DEC);
1438 break;
1439 case INT_RESULT:
1440 case DECIMAL_RESULT:
1441 {
1442 int precision= args[0]->decimal_precision()*2 + prec_increment;
1443 decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1444 max_length= class_decimal_precision_to_length(precision, decimals,
1445 unsigned_flag);
1446
1447 break;
1448 }
1449 case ROW_RESULT:
1450 assert(0);
1451 }
1452}
1453
1454
1455Item *Item_sum_variance::copy_or_same(Session* session)
1456{
1457 return new (session->mem) Item_sum_variance(session, this);
1458}
1459
1460
1467 uint32_t )
1468{
1469 Field *field;
1470 if (group)
1471 {
1472 /*
1473 We must store both value and counter in the temporary table in one field.
1474 The easiest way is to do this is to store both value in a string
1475 and unpack on access.
1476 */
1477 table->setVariableWidth();
1478 field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, &my_charset_bin);
1479 }
1480 else
1481 field= new Field_double(max_length, maybe_null, name, decimals, true);
1482
1483 if (field != NULL)
1484 field->init(table);
1485
1486 return field;
1487}
1488
1489
1490void Item_sum_variance::clear()
1491{
1492 count= 0;
1493}
1494
1495bool Item_sum_variance::add()
1496{
1497 /*
1498 Why use a temporary variable? We don't know if it is null until we
1499 evaluate it, which has the side-effect of setting null_value .
1500 */
1501 double nr= args[0]->val_real();
1502
1503 if (!args[0]->null_value)
1504 variance_fp_recurrence_next(&recurrence_m, &recurrence_s, &count, nr);
1505 return 0;
1506}
1507
1509{
1510 assert(fixed == 1);
1511
1512 /*
1513 'sample' is a 1/0 boolean value. If it is 1/true, id est this is a sample
1514 variance call, then we should set nullness when the count of the items
1515 is one or zero. If it's zero, i.e. a population variance, then we only
1516 set nullness when the count is zero.
1517
1518 Another way to read it is that 'sample' is the numerical threshhold, at and
1519 below which a 'count' number of items is called NULL.
1520 */
1521 assert((sample == 0) || (sample == 1));
1522 if (count <= sample)
1523 {
1524 null_value=1;
1525 return 0.0;
1526 }
1527
1528 null_value=0;
1529 return variance_fp_recurrence_result(recurrence_s, count, sample);
1530}
1531
1532
1534{
1535 /* can't be fix_fields()ed */
1536 return (int64_t) rint(val_real());
1537}
1538
1539
1541{
1542 assert(fixed == 1);
1543 return val_decimal_from_real(dec_buf);
1544}
1545
1546
1547void Item_sum_variance::reset_field()
1548{
1549 double nr;
1550 unsigned char *res= result_field->ptr;
1551
1552 nr= args[0]->val_real(); /* sets null_value as side-effect */
1553
1554 if (args[0]->null_value)
1555 memset(res, 0, sizeof(double)*2+sizeof(int64_t));
1556 else
1557 {
1558 /* Serialize format is (double)m, (double)s, (int64_t)count */
1559 uint64_t tmp_count;
1560 double tmp_s;
1561 float8store(res, nr); /* recurrence variable m */
1562 tmp_s= 0.0;
1563 float8store(res + sizeof(double), tmp_s);
1564 tmp_count= 1;
1565 int8store(res + sizeof(double)*2, tmp_count);
1566 }
1567}
1568
1569
1570void Item_sum_variance::update_field()
1571{
1572 uint64_t field_count;
1573 unsigned char *res=result_field->ptr;
1574
1575 double nr= args[0]->val_real(); /* sets null_value as side-effect */
1576
1577 if (args[0]->null_value)
1578 return;
1579
1580 /* Serialize format is (double)m, (double)s, (int64_t)count */
1581 double field_recurrence_m, field_recurrence_s;
1582 float8get(field_recurrence_m, res);
1583 float8get(field_recurrence_s, res + sizeof(double));
1584 field_count=sint8korr(res+sizeof(double)*2);
1585
1586 variance_fp_recurrence_next(&field_recurrence_m, &field_recurrence_s, &field_count, nr);
1587
1588 float8store(res, field_recurrence_m);
1589 float8store(res + sizeof(double), field_recurrence_s);
1590 res+= sizeof(double)*2;
1591 int8store(res,field_count);
1592}
1593
1594
1595/* min & max */
1596
1597void Item_sum_hybrid::clear()
1598{
1599 switch (hybrid_type) {
1600 case INT_RESULT:
1601 sum_int= 0;
1602 break;
1603 case DECIMAL_RESULT:
1604 sum_dec.set_zero();
1605 break;
1606 case REAL_RESULT:
1607 sum= 0.0;
1608 break;
1609 default:
1610 value.length(0);
1611 }
1612 null_value= 1;
1613}
1614
1616{
1617 assert(fixed == 1);
1618 if (null_value)
1619 return 0.0;
1620
1621 switch (hybrid_type) {
1622 case STRING_RESULT:
1623 {
1624 char *end_not_used;
1625 int err_not_used;
1626 String *res; res=val_str(&str_value);
1627 return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1628 &end_not_used, &err_not_used) : 0.0);
1629 }
1630 case INT_RESULT:
1631 return (double) sum_int;
1632 case DECIMAL_RESULT:
1633 class_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1634 return sum;
1635 case REAL_RESULT:
1636 return sum;
1637 case ROW_RESULT:
1638 // This case should never be choosen
1639 break;
1640 }
1641
1642 assert(0);
1643 return 0;
1644}
1645
1647{
1648 assert(fixed == 1);
1649 if (null_value)
1650 return 0;
1651 switch (hybrid_type) {
1652 case INT_RESULT:
1653 return sum_int;
1654 case DECIMAL_RESULT:
1655 {
1656 int64_t result;
1657 sum_dec.val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
1658 return sum_int;
1659 }
1660 default:
1661 return (int64_t) rint(Item_sum_hybrid::val_real());
1662 }
1663}
1664
1665
1667{
1668 assert(fixed == 1);
1669 if (null_value)
1670 return 0;
1671
1672 switch (hybrid_type) {
1673 case STRING_RESULT:
1674 val->store(E_DEC_FATAL_ERROR, &value);
1675 break;
1676 case REAL_RESULT:
1677 double2_class_decimal(E_DEC_FATAL_ERROR, sum, val);
1678 break;
1679 case DECIMAL_RESULT:
1680 val= &sum_dec;
1681 break;
1682 case INT_RESULT:
1683 int2_class_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1684 break;
1685 case ROW_RESULT:
1686 // This case should never be choosen
1687 assert(0);
1688 break;
1689 }
1690
1691 return val; // Keep compiler happy
1692}
1693
1694
1695String *
1697{
1698 assert(fixed == 1);
1699 if (null_value)
1700 return 0;
1701
1702 switch (hybrid_type) {
1703 case STRING_RESULT:
1704 return &value;
1705 case REAL_RESULT:
1706 str->set_real(sum,decimals, &my_charset_bin);
1707 break;
1708 case DECIMAL_RESULT:
1709 class_decimal2string(&sum_dec, 0, str);
1710 return str;
1711 case INT_RESULT:
1712 str->set_int(sum_int, unsigned_flag, &my_charset_bin);
1713 break;
1714 case ROW_RESULT:
1715 default:
1716 // This case should never be choosen
1717 break;
1718 }
1719
1720 return str; // Keep compiler happy
1721}
1722
1723
1724void Item_sum_hybrid::cleanup()
1725{
1726 Item_sum::cleanup();
1727 forced_const= false;
1728
1729 /*
1730 by default it is TRUE to avoid TRUE reporting by
1731 Item_func_not_all/Item_func_nop_all if this item was never called.
1732
1733 no_rows_in_result() set it to FALSE if was not results found.
1734 If some results found it will be left unchanged.
1735 */
1736 was_values= true;
1737 return;
1738}
1739
1741{
1742 was_values= false;
1743 clear();
1744}
1745
1746
1747Item *Item_sum_min::copy_or_same(Session* session)
1748{
1749 return new (session->mem) Item_sum_min(session, this);
1750}
1751
1752
1753bool Item_sum_min::add()
1754{
1755 switch (hybrid_type) {
1756 case STRING_RESULT:
1757 {
1758 String *result=args[0]->val_str(&tmp_value);
1759 if (!args[0]->null_value &&
1760 (null_value || sortcmp(&value,result,collation.collation) > 0))
1761 {
1762 value.copy(*result);
1763 null_value=0;
1764 }
1765 }
1766 break;
1767 case INT_RESULT:
1768 {
1769 int64_t nr=args[0]->val_int();
1770 if (!args[0]->null_value && (null_value ||
1771 (unsigned_flag &&
1772 (uint64_t) nr < (uint64_t) sum_int) ||
1773 (!unsigned_flag && nr < sum_int)))
1774 {
1775 sum_int=nr;
1776 null_value=0;
1777 }
1778 }
1779 break;
1780 case DECIMAL_RESULT:
1781 {
1782 type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1783 if (!args[0]->null_value &&
1784 (null_value || (class_decimal_cmp(&sum_dec, val) > 0)))
1785 {
1786 class_decimal2decimal(val, &sum_dec);
1787 null_value= 0;
1788 }
1789 }
1790 break;
1791 case REAL_RESULT:
1792 {
1793 double nr= args[0]->val_real();
1794 if (!args[0]->null_value && (null_value || nr < sum))
1795 {
1796 sum=nr;
1797 null_value=0;
1798 }
1799 }
1800 break;
1801 case ROW_RESULT:
1802 // This case should never be choosen
1803 assert(0);
1804 break;
1805 }
1806 return 0;
1807}
1808
1809
1810Item *Item_sum_max::copy_or_same(Session* session)
1811{
1812 return new (session->mem) Item_sum_max(session, this);
1813}
1814
1815
1816bool Item_sum_max::add()
1817{
1818 switch (hybrid_type) {
1819 case STRING_RESULT:
1820 {
1821 String *result=args[0]->val_str(&tmp_value);
1822 if (!args[0]->null_value &&
1823 (null_value || sortcmp(&value,result,collation.collation) < 0))
1824 {
1825 value.copy(*result);
1826 null_value=0;
1827 }
1828 }
1829 break;
1830 case INT_RESULT:
1831 {
1832 int64_t nr=args[0]->val_int();
1833 if (!args[0]->null_value && (null_value ||
1834 (unsigned_flag &&
1835 (uint64_t) nr > (uint64_t) sum_int) ||
1836 (!unsigned_flag && nr > sum_int)))
1837 {
1838 sum_int=nr;
1839 null_value=0;
1840 }
1841 }
1842 break;
1843 case DECIMAL_RESULT:
1844 {
1845 type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1846 if (!args[0]->null_value &&
1847 (null_value || (class_decimal_cmp(val, &sum_dec) > 0)))
1848 {
1849 class_decimal2decimal(val, &sum_dec);
1850 null_value= 0;
1851 }
1852 }
1853 break;
1854 case REAL_RESULT:
1855 {
1856 double nr= args[0]->val_real();
1857 if (!args[0]->null_value && (null_value || nr > sum))
1858 {
1859 sum=nr;
1860 null_value=0;
1861 }
1862 }
1863 break;
1864 case ROW_RESULT:
1865 // This case should never be choosen
1866 assert(0);
1867 break;
1868 }
1869
1870 return 0;
1871}
1872
1873
1874/* bit_or and bit_and */
1875
1877{
1878 assert(fixed == 1);
1879 return (int64_t) bits;
1880}
1881
1882
1883void Item_sum_bit::clear()
1884{
1885 bits= reset_bits;
1886}
1887
1888Item *Item_sum_or::copy_or_same(Session* session)
1889{
1890 return new (session->mem) Item_sum_or(session, this);
1891}
1892
1893
1894bool Item_sum_or::add()
1895{
1896 uint64_t value= (uint64_t) args[0]->val_int();
1897 if (!args[0]->null_value)
1898 bits|=value;
1899 return 0;
1900}
1901
1902Item *Item_sum_xor::copy_or_same(Session* session)
1903{
1904 return new (session->mem) Item_sum_xor(session, this);
1905}
1906
1907
1908bool Item_sum_xor::add()
1909{
1910 uint64_t value= (uint64_t) args[0]->val_int();
1911 if (!args[0]->null_value)
1912 bits^=value;
1913 return 0;
1914}
1915
1916Item *Item_sum_and::copy_or_same(Session* session)
1917{
1918 return new (session->mem) Item_sum_and(session, this);
1919}
1920
1921
1922bool Item_sum_and::add()
1923{
1924 uint64_t value= (uint64_t) args[0]->val_int();
1925 if (!args[0]->null_value)
1926 bits&=value;
1927 return 0;
1928}
1929
1930/************************************************************************
1931** reset result of a Item_sum with is saved in a tmp_table
1932*************************************************************************/
1933
1934void Item_sum_num::reset_field()
1935{
1936 double nr= args[0]->val_real();
1937 unsigned char *res=result_field->ptr;
1938
1939 if (maybe_null)
1940 {
1941 if (args[0]->null_value)
1942 {
1943 nr=0.0;
1944 result_field->set_null();
1945 }
1946 else
1947 result_field->set_notnull();
1948 }
1949 float8store(res,nr);
1950}
1951
1952
1953void Item_sum_hybrid::reset_field()
1954{
1955 switch(hybrid_type) {
1956 case STRING_RESULT:
1957 {
1958 char buff[MAX_FIELD_WIDTH];
1959 String tmp(buff,sizeof(buff),result_field->charset()),*res;
1960
1961 res=args[0]->val_str(&tmp);
1962 if (args[0]->null_value)
1963 {
1964 result_field->set_null();
1965 result_field->reset();
1966 }
1967 else
1968 {
1969 result_field->set_notnull();
1970 result_field->store(res->ptr(),res->length(),tmp.charset());
1971 }
1972 break;
1973 }
1974 case INT_RESULT:
1975 {
1976 int64_t nr=args[0]->val_int();
1977
1978 if (maybe_null)
1979 {
1980 if (args[0]->null_value)
1981 {
1982 nr=0;
1983 result_field->set_null();
1984 }
1985 else
1986 result_field->set_notnull();
1987 }
1988 result_field->store(nr, unsigned_flag);
1989 break;
1990 }
1991 case REAL_RESULT:
1992 {
1993 double nr= args[0]->val_real();
1994
1995 if (maybe_null)
1996 {
1997 if (args[0]->null_value)
1998 {
1999 nr=0.0;
2000 result_field->set_null();
2001 }
2002 else
2003 result_field->set_notnull();
2004 }
2005 result_field->store(nr);
2006 break;
2007 }
2008 case DECIMAL_RESULT:
2009 {
2010 type::Decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2011
2012 if (maybe_null)
2013 {
2014 if (args[0]->null_value)
2015 result_field->set_null();
2016 else
2017 result_field->set_notnull();
2018 }
2019 /*
2020 We must store zero in the field as we will use the field value in
2021 add()
2022 */
2023 if (!arg_dec) // Null
2024 arg_dec= &decimal_zero;
2025 result_field->store_decimal(arg_dec);
2026 break;
2027 }
2028 case ROW_RESULT:
2029 assert(0);
2030 }
2031}
2032
2033
2034void Item_sum_sum::reset_field()
2035{
2036 if (hybrid_type == DECIMAL_RESULT)
2037 {
2038 type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2039 if (!arg_val) // Null
2040 arg_val= &decimal_zero;
2041 result_field->store_decimal(arg_val);
2042 }
2043 else
2044 {
2045 assert(hybrid_type == REAL_RESULT);
2046 double nr= args[0]->val_real(); // Nulls also return 0
2047 float8store(result_field->ptr, nr);
2048 }
2049 if (args[0]->null_value)
2050 result_field->set_null();
2051 else
2052 result_field->set_notnull();
2053}
2054
2055
2056void Item_sum_count::reset_field()
2057{
2058 unsigned char *res=result_field->ptr;
2059 int64_t nr=0;
2060
2061 if (!args[0]->maybe_null || !args[0]->is_null())
2062 nr=1;
2063 int8store(res,nr);
2064}
2065
2066
2067void Item_sum_avg::reset_field()
2068{
2069 unsigned char *res=result_field->ptr;
2070 if (hybrid_type == DECIMAL_RESULT)
2071 {
2072 int64_t tmp;
2073 type::Decimal value, *arg_dec= args[0]->val_decimal(&value);
2074 if (args[0]->null_value)
2075 {
2076 arg_dec= &decimal_zero;
2077 tmp= 0;
2078 }
2079 else
2080 tmp= 1;
2081 arg_dec->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
2082 res+= dec_bin_size;
2083 int8store(res, tmp);
2084 }
2085 else
2086 {
2087 double nr= args[0]->val_real();
2088
2089 if (args[0]->null_value)
2090 memset(res, 0, sizeof(double)+sizeof(int64_t));
2091 else
2092 {
2093 int64_t tmp= 1;
2094 float8store(res,nr);
2095 res+=sizeof(double);
2096 int8store(res,tmp);
2097 }
2098 }
2099}
2100
2101
2102void Item_sum_bit::reset_field()
2103{
2104 reset();
2105 int8store(result_field->ptr, bits);
2106}
2107
2108void Item_sum_bit::update_field()
2109{
2110 unsigned char *res=result_field->ptr;
2111 bits= uint8korr(res);
2112 add();
2113 int8store(res, bits);
2114}
2115
2116
2120
2122{
2123 if (hybrid_type == DECIMAL_RESULT)
2124 {
2125 type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2126 if (!args[0]->null_value)
2127 {
2128 if (!result_field->is_null())
2129 {
2130 type::Decimal field_value,
2131 *field_val= result_field->val_decimal(&field_value);
2132 class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2133 result_field->store_decimal(dec_buffs);
2134 }
2135 else
2136 {
2137 result_field->store_decimal(arg_val);
2138 result_field->set_notnull();
2139 }
2140 }
2141 }
2142 else
2143 {
2144 double old_nr,nr;
2145 unsigned char *res=result_field->ptr;
2146
2147 float8get(old_nr,res);
2148 nr= args[0]->val_real();
2149 if (!args[0]->null_value)
2150 {
2151 old_nr+=nr;
2152 result_field->set_notnull();
2153 }
2154 float8store(res,old_nr);
2155 }
2156}
2157
2158
2159void Item_sum_count::update_field()
2160{
2161 int64_t nr;
2162 unsigned char *res=result_field->ptr;
2163
2164 nr=sint8korr(res);
2165 if (!args[0]->maybe_null || !args[0]->is_null())
2166 nr++;
2167 int8store(res,nr);
2168}
2169
2170
2171void Item_sum_avg::update_field()
2172{
2173 int64_t field_count;
2174 unsigned char *res=result_field->ptr;
2175 if (hybrid_type == DECIMAL_RESULT)
2176 {
2177 type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2178 if (!args[0]->null_value)
2179 {
2180 binary2_class_decimal(E_DEC_FATAL_ERROR, res,
2181 dec_buffs + 1, f_precision, f_scale);
2182 field_count= sint8korr(res + dec_bin_size);
2183 class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
2184 dec_buffs->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
2185 res+= dec_bin_size;
2186 field_count++;
2187 int8store(res, field_count);
2188 }
2189 }
2190 else
2191 {
2192 double nr;
2193
2194 nr= args[0]->val_real();
2195 if (!args[0]->null_value)
2196 {
2197 double old_nr;
2198 float8get(old_nr, res);
2199 field_count= sint8korr(res + sizeof(double));
2200 old_nr+= nr;
2201 float8store(res,old_nr);
2202 res+= sizeof(double);
2203 field_count++;
2204 int8store(res, field_count);
2205 }
2206 }
2207}
2208
2209
2210void Item_sum_hybrid::update_field()
2211{
2212 switch (hybrid_type) {
2213 case STRING_RESULT:
2214 min_max_update_str_field();
2215 break;
2216 case INT_RESULT:
2217 min_max_update_int_field();
2218 break;
2219 case DECIMAL_RESULT:
2220 min_max_update_decimal_field();
2221 break;
2222 case REAL_RESULT:
2223 case ROW_RESULT:
2224 min_max_update_real_field();
2225 }
2226}
2227
2228
2229void
2230Item_sum_hybrid::min_max_update_str_field()
2231{
2232 String *res_str=args[0]->val_str(&value);
2233
2234 if (!args[0]->null_value)
2235 {
2236 result_field->val_str_internal(&tmp_value);
2237
2238 if (result_field->is_null() ||
2239 (cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2240 result_field->store(res_str->ptr(),res_str->length(),res_str->charset());
2241 result_field->set_notnull();
2242 }
2243}
2244
2245
2246void
2247Item_sum_hybrid::min_max_update_real_field()
2248{
2249 double nr,old_nr;
2250
2251 old_nr=result_field->val_real();
2252 nr= args[0]->val_real();
2253 if (!args[0]->null_value)
2254 {
2255 if (result_field->is_null(0) ||
2256 (cmp_sign > 0 ? old_nr > nr : old_nr < nr))
2257 old_nr=nr;
2258 result_field->set_notnull();
2259 }
2260 else if (result_field->is_null(0))
2261 result_field->set_null();
2262 result_field->store(old_nr);
2263}
2264
2265
2266void
2267Item_sum_hybrid::min_max_update_int_field()
2268{
2269 int64_t nr,old_nr;
2270
2271 old_nr=result_field->val_int();
2272 nr=args[0]->val_int();
2273 if (!args[0]->null_value)
2274 {
2275 if (result_field->is_null(0))
2276 old_nr=nr;
2277 else
2278 {
2279 bool res=(unsigned_flag ?
2280 (uint64_t) old_nr > (uint64_t) nr :
2281 old_nr > nr);
2282 /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2283 if ((cmp_sign > 0) ^ (!res))
2284 old_nr=nr;
2285 }
2286 result_field->set_notnull();
2287 }
2288 else if (result_field->is_null(0))
2289 result_field->set_null();
2290 result_field->store(old_nr, unsigned_flag);
2291}
2292
2293
2298void
2300{
2301 /* TODO: optimize: do not get result_field in case of args[0] is NULL */
2302 type::Decimal old_val, nr_val;
2303 const type::Decimal *old_nr= result_field->val_decimal(&old_val);
2304 const type::Decimal *nr= args[0]->val_decimal(&nr_val);
2305 if (!args[0]->null_value)
2306 {
2307 if (result_field->is_null(0))
2308 old_nr=nr;
2309 else
2310 {
2311 bool res= class_decimal_cmp(old_nr, nr) > 0;
2312 /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2313 if ((cmp_sign > 0) ^ (!res))
2314 old_nr=nr;
2315 }
2316 result_field->set_notnull();
2317 }
2318 else if (result_field->is_null(0))
2319 result_field->set_null();
2320 result_field->store_decimal(old_nr);
2321}
2322
2323
2324Item_avg_field::Item_avg_field(Item_result res_type, Item_sum_avg *item)
2325{
2326 name=item->name;
2327 decimals=item->decimals;
2328 max_length= item->max_length;
2329 unsigned_flag= item->unsigned_flag;
2330 field=item->result_field;
2331 maybe_null=1;
2332 hybrid_type= res_type;
2333 prec_increment= item->prec_increment;
2334 if (hybrid_type == DECIMAL_RESULT)
2335 {
2336 f_scale= item->f_scale;
2337 f_precision= item->f_precision;
2338 dec_bin_size= item->dec_bin_size;
2339 }
2340}
2341
2343{
2344 // fix_fields() never calls for this Item
2345 double nr;
2346 int64_t count;
2347 unsigned char *res;
2348
2349 if (hybrid_type == DECIMAL_RESULT)
2350 return val_real_from_decimal();
2351
2352 float8get(nr,field->ptr);
2353 res= (field->ptr+sizeof(double));
2354 count= sint8korr(res);
2355
2356 if ((null_value= !count))
2357 return 0.0;
2358 return nr/(double) count;
2359}
2360
2361
2363{
2364 return (int64_t) rint(val_real());
2365}
2366
2367
2369{
2370 // fix_fields() never calls for this Item
2371 if (hybrid_type == REAL_RESULT)
2372 return val_decimal_from_real(dec_buf);
2373
2374 int64_t count= sint8korr(field->ptr + dec_bin_size);
2375 if ((null_value= !count))
2376 return 0;
2377
2378 type::Decimal dec_count, dec_field;
2379 binary2_class_decimal(E_DEC_FATAL_ERROR,
2380 field->ptr, &dec_field, f_precision, f_scale);
2381 int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
2382 class_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2383 &dec_field, &dec_count, prec_increment);
2384 return dec_buf;
2385}
2386
2387
2389{
2390 // fix_fields() never calls for this Item
2391 if (hybrid_type == DECIMAL_RESULT)
2392 return val_string_from_decimal(str);
2393 return val_string_from_real(str);
2394}
2395
2396
2397Item_std_field::Item_std_field(Item_sum_std *item)
2398 : Item_variance_field(item)
2399{
2400}
2401
2402
2404{
2405 double nr;
2406 // fix_fields() never calls for this Item
2408 assert(nr >= 0.0);
2409 return sqrt(nr);
2410}
2411
2412
2414{
2415 /*
2416 We can't call val_decimal_from_real() for DECIMAL_RESULT as
2417 Item_variance_field::val_real() would cause an infinite loop
2418 */
2419 type::Decimal tmp_dec, *dec;
2420 double nr;
2421 if (hybrid_type == REAL_RESULT)
2422 return val_decimal_from_real(dec_buf);
2423
2425 if (!dec)
2426 return 0;
2427 class_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2428 assert(nr >= 0.0);
2429 nr= sqrt(nr);
2430 double2_class_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2431 class_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
2432 return dec_buf;
2433}
2434
2435
2436Item_variance_field::Item_variance_field(Item_sum_variance *item)
2437{
2438 name=item->name;
2439 decimals=item->decimals;
2440 max_length=item->max_length;
2441 unsigned_flag= item->unsigned_flag;
2442 field=item->result_field;
2443 maybe_null=1;
2444 sample= item->sample;
2445 prec_increment= item->prec_increment;
2446 if ((hybrid_type= item->hybrid_type) == DECIMAL_RESULT)
2447 {
2448 f_scale0= item->f_scale0;
2449 f_precision0= item->f_precision0;
2450 dec_bin_size0= item->dec_bin_size0;
2451 f_scale1= item->f_scale1;
2452 f_precision1= item->f_precision1;
2453 dec_bin_size1= item->dec_bin_size1;
2454 }
2455}
2456
2457
2459{
2460 /* can't be fix_fields()ed */
2461 return (int64_t) rint(val_real());
2462}
2463
2464
2466{
2467 // fix_fields() never calls for this Item
2468 if (hybrid_type == DECIMAL_RESULT)
2469 return val_real_from_decimal();
2470
2471 double recurrence_s;
2472 uint64_t count;
2473 float8get(recurrence_s, (field->ptr + sizeof(double)));
2474 count=sint8korr(field->ptr+sizeof(double)*2);
2475
2476 if ((null_value= (count <= sample)))
2477 return 0.0;
2478
2479 return variance_fp_recurrence_result(recurrence_s, count, sample);
2480}
2481
2482
2483/****************************************************************************
2484** COUNT(DISTINCT ...)
2485****************************************************************************/
2486
2487int simple_str_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2488{
2489 Field *f= (Field*) arg;
2490 return f->cmp(key1, key2);
2491}
2492
2499
2500int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2501{
2502 Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2503 Field **field = item->table->getFields();
2504 Field **field_end= field + item->table->getShare()->sizeFields();
2505 uint32_t *lengths=item->field_lengths;
2506 for (; field < field_end; ++field)
2507 {
2508 Field* f = *field;
2509 int len = *lengths++;
2510 int res = f->cmp(key1, key2);
2511 if (res)
2512 return res;
2513 key1 += len;
2514 key2 += len;
2515 }
2516 return 0;
2517}
2518
2519static int count_distinct_walk(void *,
2520 uint32_t ,
2521 void *arg)
2522{
2523 (*((uint64_t*)arg))++;
2524 return 0;
2525}
2526
2527void Item_sum_count_distinct::cleanup()
2528{
2529 Item_sum_int::cleanup();
2530
2531 /* Free objects only if we own them. */
2532 if (!original)
2533 {
2534 /*
2535 We need to delete the table and the tree in cleanup() as
2536 they were allocated in the runtime memroot. Using the runtime
2537 memroot reduces memory footprint for PS/SP and simplifies setup().
2538 */
2539 delete tree;
2540 tree= 0;
2541 is_evaluated= false;
2542 if (table)
2543 {
2544 table= 0;
2545 }
2546 delete tmp_table_param;
2547 tmp_table_param= 0;
2548 }
2549 always_null= false;
2550 return;
2551}
2552
2553
2558
2560{
2561 table=0;
2562 original= 0;
2563 force_copy_fields= 1;
2564 tree= 0;
2565 is_evaluated= false;
2566 tmp_table_param= 0;
2567 always_null= false;
2568}
2569
2570
2571Item_sum_count_distinct::~Item_sum_count_distinct()
2572{
2573 cleanup();
2574}
2575
2576
2577bool Item_sum_count_distinct::setup(Session *session)
2578{
2579 List<Item> list;
2580 Select_Lex *select_lex= session->lex().current_select;
2581
2582 /*
2583 Setup can be called twice for ROLLUP items. This is a bug.
2584 Please add assert(tree == 0) here when it's fixed.
2585 It's legal to call setup() more than once when in a subquery
2586 */
2587 if (tree || table || tmp_table_param)
2588 return false;
2589
2590 tmp_table_param= new Tmp_Table_Param;
2591
2592 /* Create a table with an unique key over all parameters */
2593 for (uint32_t i= 0; i < arg_count; i++)
2594 {
2595 Item *item=args[i];
2596 list.push_back(item);
2597 if (item->const_item() && item->is_null())
2598 always_null= 1;
2599 }
2600 if (always_null)
2601 return false;
2602 count_field_types(select_lex, tmp_table_param, list, 0);
2603 tmp_table_param->force_copy_fields= force_copy_fields;
2604 assert(table == 0);
2605
2606 if (!(table= create_tmp_table(session, tmp_table_param, list, NULL, 1, 0, (select_lex->options | session->options), HA_POS_ERROR, "")))
2607 {
2608 return true;
2609 }
2610 table->cursor->extra(HA_EXTRA_NO_ROWS); // Don't update rows
2611 table->no_rows=1;
2612
2613 if (table->getShare()->db_type() == heap_engine)
2614 {
2615 /*
2616 No blobs, otherwise it would have been MyISAM: set up a compare
2617 function and its arguments to use with Unique.
2618 */
2619 qsort_cmp2 compare_key;
2620 void* cmp_arg;
2621 Field **field= table->getFields();
2622 Field **field_end= field + table->getShare()->sizeFields();
2623 bool all_binary= true;
2624
2625 for (tree_key_length= 0; field < field_end; ++field)
2626 {
2627 Field *f= *field;
2628 enum enum_field_types f_type= f->type();
2629 tree_key_length+= f->pack_length();
2630 if (f_type == DRIZZLE_TYPE_VARCHAR)
2631 {
2632 all_binary= false;
2633 break;
2634 }
2635 }
2636 if (all_binary)
2637 {
2638 cmp_arg= (void*) &tree_key_length;
2639 compare_key= (qsort_cmp2) simple_raw_key_cmp;
2640 }
2641 else
2642 {
2643 if (table->getShare()->sizeFields() == 1)
2644 {
2645 /*
2646 If we have only one field, which is the most common use of
2647 count(distinct), it is much faster to use a simpler key
2648 compare method that can take advantage of not having to worry
2649 about other fields.
2650 */
2651 compare_key= (qsort_cmp2) simple_str_key_cmp;
2652 cmp_arg= (void*) table->getField(0);
2653 /* tree_key_length has been set already */
2654 }
2655 else
2656 {
2657 uint32_t *length;
2658 compare_key= (qsort_cmp2) composite_key_cmp;
2659 cmp_arg= this;
2660 field_lengths= new (session->mem) uint32_t[table->getShare()->sizeFields()];
2661 for (tree_key_length= 0, length= field_lengths, field= table->getFields();
2662 field < field_end; ++field, ++length)
2663 {
2664 *length= (*field)->pack_length();
2665 tree_key_length+= *length;
2666 }
2667 }
2668 }
2669 assert(tree == 0);
2670 tree= new Unique(compare_key, cmp_arg, tree_key_length,
2671 (size_t)session->variables.max_heap_table_size);
2672 /*
2673 The only time tree_key_length could be 0 is if someone does
2674 count(distinct) on a char(0) field - stupid thing to do,
2675 but this has to be handled - otherwise someone can crash
2676 the server with a DoS attack
2677 */
2678 is_evaluated= false;
2679 if (! tree)
2680 return true;
2681 }
2682 return false;
2683}
2684
2685
2686Item *Item_sum_count_distinct::copy_or_same(Session* session)
2687{
2688 return new (session->mem) Item_sum_count_distinct(session, this);
2689}
2690
2691
2692void Item_sum_count_distinct::clear()
2693{
2694 /* tree and table can be both null only if always_null */
2695 is_evaluated= false;
2696 if (tree)
2697 {
2698 tree->reset();
2699 }
2700 else if (table)
2701 {
2702 table->cursor->extra(HA_EXTRA_NO_CACHE);
2703 table->cursor->ha_delete_all_rows();
2704 table->cursor->extra(HA_EXTRA_WRITE_CACHE);
2705 }
2706}
2707
2708bool Item_sum_count_distinct::add()
2709{
2710 int error;
2711 if (always_null)
2712 return 0;
2713 copy_fields(tmp_table_param);
2714 if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
2715 return true;
2716
2717 for (Field **field= table->getFields() ; *field ; field++)
2718 {
2719 if ((*field)->is_real_null(0))
2720 {
2721 return 0; // Don't count NULL
2722 }
2723 }
2724
2725 is_evaluated= false;
2726 if (tree)
2727 {
2728 /*
2729 The first few bytes of record (at least one) are just markers
2730 for deleted and NULLs. We want to skip them since they will
2731 bloat the tree without providing any valuable info. Besides,
2732 key_length used to initialize the tree didn't include space for them.
2733 */
2734 return tree->unique_add(table->record[0] + table->getShare()->null_bytes);
2735 }
2736 if ((error= table->cursor->insertRecord(table->record[0])) &&
2737 table->cursor->is_fatal_error(error, HA_CHECK_DUP))
2738 return true;
2739 return false;
2740}
2741
2742
2744{
2745 int error;
2746 assert(fixed == 1);
2747 if (!table) // Empty query
2748 return 0L;
2749 if (tree)
2750 {
2751 if (is_evaluated)
2752 return count;
2753
2754 if (tree->elements == 0)
2755 return (int64_t) tree->elements_in_tree(); // everything fits in memory
2756 count= 0;
2757 tree->walk(count_distinct_walk, (void*) &count);
2758 is_evaluated= true;
2759 return (int64_t) count;
2760 }
2761
2762 error= table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2763
2764 if(error)
2765 {
2766 table->print_error(error, MYF(0));
2767 }
2768
2769 return table->cursor->stats.records;
2770}
2771
2772/*****************************************************************************
2773 GROUP_CONCAT function
2774
2775 SQL SYNTAX:
2776 GROUP_CONCAT([DISTINCT] expr,... [order_st BY col [ASC|DESC],...]
2777 [SEPARATOR str_const])
2778
2779 concat of values from "group by" operation
2780
2781 BUGS
2782 Blobs doesn't work with DISTINCT or order_st BY
2783*****************************************************************************/
2784
2785
2800
2801int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
2802 const void* key2)
2803{
2805 Table *table= item_func->table;
2806
2807 for (uint32_t i= 0; i < item_func->arg_count_field; i++)
2808 {
2809 Item *item= item_func->args[i];
2810 /*
2811 If field_item is a const item then either get_tp_table_field returns 0
2812 or it is an item over a const table.
2813 */
2814 if (item->const_item())
2815 continue;
2816 /*
2817 We have to use get_tmp_table_field() instead of
2818 real_item()->get_tmp_table_field() because we want the field in
2819 the temporary table, not the original field
2820 */
2821 Field *field= item->get_tmp_table_field();
2822 int res;
2823 uint32_t offset= field->offset(field->getTable()->record[0])-table->getShare()->null_bytes;
2824 if((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2825 return res;
2826 }
2827 return 0;
2828}
2829
2830
2834
2835int group_concat_key_cmp_with_order(void* arg, const void* key1,
2836 const void* key2)
2837{
2839 Order **order_item, **end;
2840 Table *table= grp_item->table;
2841
2842 for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2843 order_item < end;
2844 order_item++)
2845 {
2846 Item *item= *(*order_item)->item;
2847 /*
2848 We have to use get_tmp_table_field() instead of
2849 real_item()->get_tmp_table_field() because we want the field in
2850 the temporary table, not the original field
2851 */
2852 Field *field= item->get_tmp_table_field();
2853 /*
2854 If item is a const item then either get_tp_table_field returns 0
2855 or it is an item over a const table.
2856 */
2857 if (field && !item->const_item())
2858 {
2859 int res;
2860 uint32_t offset= (field->offset(field->getTable()->record[0]) -
2861 table->getShare()->null_bytes);
2862 if ((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2863 return (*order_item)->asc ? res : -res;
2864 }
2865 }
2866 /*
2867 We can't return 0 because in that case the tree class would remove this
2868 item as double value. This would cause problems for case-changes and
2869 if the returned values are not the same we do the sort on.
2870 */
2871 return 1;
2872}
2873
2874
2878
2879int dump_leaf_key(unsigned char* key, uint32_t ,
2881{
2882 Table *table= item->table;
2883 String tmp((char *)table->getUpdateRecord(), table->getShare()->getRecordLength(), default_charset_info);
2884 String *result= &item->result;
2885 Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
2886 uint32_t old_length= result->length();
2887
2888 if (item->no_appended)
2889 item->no_appended= false;
2890 else
2891 result->append(*item->separator);
2892
2893 tmp.length(0);
2894
2895 for (; arg < arg_end; arg++)
2896 {
2897 String *res;
2898 if (! (*arg)->const_item())
2899 {
2900 /*
2901 We have to use get_tmp_table_field() instead of
2902 real_item()->get_tmp_table_field() because we want the field in
2903 the temporary table, not the original field
2904 We also can't use table->field array to access the fields
2905 because it contains both order and arg list fields.
2906 */
2907 Field *field= (*arg)->get_tmp_table_field();
2908 uint32_t offset= (field->offset(field->getTable()->record[0]) -
2909 table->getShare()->null_bytes);
2910 assert(offset < table->getShare()->getRecordLength());
2911 res= field->val_str_internal(&tmp, key + offset);
2912 }
2913 else
2914 res= (*arg)->val_str(&tmp);
2915 if (res)
2916 result->append(*res);
2917 }
2918
2919 /* stop if length of result more than max_length */
2920 if (result->length() > item->max_length)
2921 {
2922 int well_formed_error;
2923 const charset_info_st& cs= *item->collation.collation;
2924 const char *ptr= result->ptr();
2925 /*
2926 It's ok to use item->result.length() as the fourth argument
2927 as this is never used to limit the length of the data.
2928 Cut is done with the third argument.
2929 */
2930 uint32_t add_length= cs.cset->well_formed_len(cs, str_ref(ptr + old_length, ptr + item->max_length), result->length(), &well_formed_error);
2931 result->length(old_length + add_length);
2932 item->count_cut_values++;
2933 item->warning_for_row= true;
2934 return 1;
2935 }
2936 return 0;
2937}
2938
2939
2948
2951 bool distinct_arg, List<Item> *select_list,
2952 SQL_LIST *order_list, String *separator_arg)
2953 :tmp_table_param(0), warning(0),
2954 separator(separator_arg), tree(NULL), unique_filter(NULL), table(0),
2955 order(0), context(context_arg),
2956 arg_count_order(order_list ? order_list->elements : 0),
2957 arg_count_field(select_list->size()),
2958 count_cut_values(0),
2959 distinct(distinct_arg),
2960 warning_for_row(false),
2961 force_copy_fields(0), original(0)
2962{
2963 Item *item_select;
2964 Item **arg_ptr;
2965
2966 quick_group= false;
2967 arg_count= arg_count_field + arg_count_order;
2968
2969 /*
2970 We need to allocate:
2971 args - arg_count_field+arg_count_order
2972 (for possible order items in temporare tables)
2973 order - arg_count_order
2974 */
2975 if (!(args= (Item**) memory::sql_alloc(sizeof(Item*) * arg_count +
2976 sizeof(Order*)*arg_count_order)))
2977 return;
2978
2979 order= (Order**)(args + arg_count);
2980
2981 /* fill args items of show and sort */
2982 List<Item>::iterator li(select_list->begin());
2983
2984 for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
2985 *arg_ptr= item_select;
2986
2987 if (arg_count_order)
2988 {
2989 Order **order_ptr= order;
2990 for (Order *order_item= (Order*) order_list->first;
2991 order_item != NULL;
2992 order_item= order_item->next)
2993 {
2994 (*order_ptr++)= order_item;
2995 *arg_ptr= *order_item->item;
2996 order_item->item= arg_ptr++;
2997 }
2998 }
2999}
3000
3001
3004 :Item_sum(session, item),
3005 tmp_table_param(item->tmp_table_param),
3006 warning(item->warning),
3007 separator(item->separator),
3008 tree(item->tree),
3009 unique_filter(item->unique_filter),
3010 table(item->table),
3011 order(item->order),
3012 context(item->context),
3013 arg_count_order(item->arg_count_order),
3014 arg_count_field(item->arg_count_field),
3015 count_cut_values(item->count_cut_values),
3016 distinct(item->distinct),
3017 warning_for_row(item->warning_for_row),
3018 always_null(item->always_null),
3019 force_copy_fields(item->force_copy_fields),
3020 original(item)
3021{
3022 quick_group= item->quick_group;
3023 result.set_charset(collation.collation);
3024}
3025
3026
3027
3028void Item_func_group_concat::cleanup()
3029{
3030 Item_sum::cleanup();
3031
3032 /* Adjust warning message to include total number of cut values */
3033 if (warning)
3034 {
3035 char warn_buff[DRIZZLE_ERRMSG_SIZE];
3036 snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3037 warning->set_msg(&getSession(), warn_buff);
3038 warning= 0;
3039 }
3040
3041 /*
3042 Free table and tree if they belong to this item (if item have not pointer
3043 to original item from which was made copy => it own its objects )
3044 */
3045 if (!original)
3046 {
3047 delete tmp_table_param;
3048 tmp_table_param= 0;
3049 if (table)
3050 {
3051 Session *session= table->in_use;
3052 table= 0;
3053 if (tree)
3054 {
3055 tree->delete_tree();
3056 tree= 0;
3057 }
3058
3059 delete unique_filter;
3060 unique_filter= NULL;
3061
3062 if (warning)
3063 {
3064 char warn_buff[DRIZZLE_ERRMSG_SIZE];
3065 snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3066 warning->set_msg(session, warn_buff);
3067 warning= 0;
3068 }
3069 }
3070 assert(tree == 0 && warning == 0);
3071 }
3072 return;
3073}
3074
3075
3076Item *Item_func_group_concat::copy_or_same(Session* session)
3077{
3078 return new (session->mem) Item_func_group_concat(session, this);
3079}
3080
3081
3082void Item_func_group_concat::clear()
3083{
3084 result.length(0);
3085 result.copy();
3086 null_value= true;
3087 warning_for_row= false;
3088 no_appended= true;
3089 if (tree)
3090 tree->reset_tree();
3091 if (distinct)
3092 unique_filter->reset();
3093 /* No need to reset the table as we never call write_row */
3094}
3095
3096
3097bool Item_func_group_concat::add()
3098{
3099 if (always_null)
3100 return 0;
3101 copy_fields(tmp_table_param);
3102 if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
3103 return true;
3104
3105 for (uint32_t i= 0; i < arg_count_field; i++)
3106 {
3107 Item *show_item= args[i];
3108 if (!show_item->const_item())
3109 {
3110 Field *f= show_item->get_tmp_table_field();
3111 if (f->is_null_in_record((const unsigned char*) table->record[0]))
3112 return 0; // Skip row if it contains null
3113 }
3114 }
3115
3116 null_value= false;
3117 bool row_eligible= true;
3118
3119 if (distinct)
3120 {
3121 /* Filter out duplicate rows. */
3122 uint32_t count= unique_filter->elements_in_tree();
3123 unique_filter->unique_add(table->record[0] + table->getShare()->null_bytes);
3124 if (count == unique_filter->elements_in_tree())
3125 row_eligible= false;
3126 }
3127
3128 Tree_Element *el= 0; // Only for safety
3129 if (row_eligible && tree)
3130 el= tree->tree_insert(table->record[0] + table->getShare()->null_bytes, 0,
3131 tree->getCustomArg());
3132 /*
3133 If the row is not a duplicate (el->count == 1)
3134 we can dump the row here in case of GROUP_CONCAT(DISTINCT...)
3135 instead of doing tree traverse later.
3136 */
3137 if (row_eligible && !warning_for_row &&
3138 (!tree || (el->count == 1 && distinct && !arg_count_order)))
3139 dump_leaf_key(table->record[0] + table->getShare()->null_bytes, 1, this);
3140
3141 return 0;
3142}
3143
3144
3145bool
3146Item_func_group_concat::fix_fields(Session *session, Item **ref)
3147{
3148 uint32_t i; /* for loop variable */
3149 assert(fixed == 0);
3150
3151 if (init_sum_func_check(session))
3152 return true;
3153
3154 maybe_null= 1;
3155
3156 /*
3157 Fix fields for select list and order_st clause
3158 */
3159
3160 for (i=0 ; i < arg_count ; i++)
3161 {
3162 if ((!args[i]->fixed &&
3163 args[i]->fix_fields(session, args + i)) ||
3164 args[i]->check_cols(1))
3165 return true;
3166 }
3167
3168 if (agg_item_charsets(collation, func_name(),
3169 args,
3170 /* skip charset aggregation for order columns */
3171 arg_count - arg_count_order,
3172 MY_COLL_ALLOW_CONV, 1))
3173 return 1;
3174
3175 result.set_charset(collation.collation);
3176 result_field= 0;
3177 null_value= 1;
3178 max_length= (size_t)session->variables.group_concat_max_len;
3179
3180 if (check_sum_func(session, ref))
3181 return true;
3182
3183 fixed= 1;
3184 return false;
3185}
3186
3187
3188bool Item_func_group_concat::setup(Session *session)
3189{
3190 List<Item> list;
3191 Select_Lex *select_lex= session->lex().current_select;
3192
3193 /*
3194 Currently setup() can be called twice. Please add
3195 assertion here when this is fixed.
3196 */
3197 if (table || tree)
3198 return false;
3199
3200 tmp_table_param= new Tmp_Table_Param;
3201
3202 /* We'll convert all blobs to varchar fields in the temporary table */
3203 tmp_table_param->convert_blob_length= max_length *
3204 collation.collation->mbmaxlen;
3205 /* Push all not constant fields to the list and create a temp table */
3206 always_null= 0;
3207 for (uint32_t i= 0; i < arg_count_field; i++)
3208 {
3209 Item *item= args[i];
3210 list.push_back(item);
3211 if (item->const_item())
3212 {
3213 if (item->is_null())
3214 {
3215 always_null= 1;
3216 return false;
3217 }
3218 }
3219 }
3220
3221 List<Item> all_fields(list);
3222 /*
3223 Try to find every order_st expression in the list of GROUP_CONCAT
3224 arguments. If an expression is not found, prepend it to
3225 "all_fields". The resulting field list is used as input to create
3226 tmp table columns.
3227 */
3228 if (arg_count_order &&
3229 setup_order(session, args, context->table_list, list, all_fields, *order))
3230 return true;
3231
3232 count_field_types(select_lex, tmp_table_param, all_fields, 0);
3233 tmp_table_param->force_copy_fields= force_copy_fields;
3234 assert(table == 0);
3235 if (arg_count_order > 0 || distinct)
3236 {
3237 /*
3238 Currently we have to force conversion of BLOB values to VARCHAR's
3239 if we are to store them in TREE objects used for ORDER BY and
3240 DISTINCT. This leads to truncation if the BLOB's size exceeds
3241 Field_varstring::MAX_SIZE.
3242 */
3243 set_if_smaller(tmp_table_param->convert_blob_length,
3244 Field_varstring::MAX_SIZE);
3245 }
3246
3247 /*
3248 We have to create a temporary table to get descriptions of fields
3249 (types, sizes and so on).
3250
3251 Note that in the table, we first have the ORDER BY fields, then the
3252 field list.
3253 */
3254 if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3255 (Order*) 0, 0, true,
3256 (select_lex->options | session->options),
3257 HA_POS_ERROR, (char*) "")))
3258 {
3259 return true;
3260 }
3261
3262 table->cursor->extra(HA_EXTRA_NO_ROWS);
3263 table->no_rows= 1;
3264
3265 /*
3266 Need sorting or uniqueness: init tree and choose a function to sort.
3267 Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3268 the row is not added to the result.
3269 */
3270 uint32_t tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
3271
3272 if (arg_count_order)
3273 {
3274 tree= &tree_base;
3275 /*
3276 Create a tree for sorting. The tree is used to sort (according to the
3277 syntax of this function). If there is no ORDER BY clause, we don't
3278 create this tree.
3279 */
3280 tree->init_tree((uint32_t) min(session->variables.max_heap_table_size,
3281 (uint64_t)(session->variables.sortbuff_size/16)),
3282 0,
3283 tree_key_length,
3284 group_concat_key_cmp_with_order , false, NULL, (void*) this);
3285 }
3286
3287 if (distinct)
3289 (void*)this,
3290 tree_key_length,
3291 (size_t)session->variables.max_heap_table_size);
3292
3293 return false;
3294}
3295
3296
3297/* This is used by rollup to create a separate usable copy of the function */
3298
3299void Item_func_group_concat::make_unique()
3300{
3301 tmp_table_param= 0;
3302 table=0;
3303 original= 0;
3304 force_copy_fields= 1;
3305 tree= 0;
3306}
3307
3309{
3310 String *res; res=val_str(&str_value);
3311 return res ? internal::my_atof(res->c_ptr()) : 0.0;
3312}
3313
3315{
3316 String *res;
3317 char *end_ptr;
3318 int error;
3319 if (!(res= val_str(&str_value)))
3320 return (int64_t) 0;
3321 end_ptr= (char*) res->ptr()+ res->length();
3322 return internal::my_strtoll10(res->ptr(), &end_ptr, &error);
3323}
3324
3326{
3327 assert(fixed == 1);
3328 if (null_value)
3329 return 0;
3330 if (no_appended && tree)
3331 /* Tree is used for sorting as in ORDER BY */
3332 tree->tree_walk((tree_walk_action)&dump_leaf_key, (void*)this, left_root_right);
3333 if (count_cut_values && !warning)
3334 {
3335 /*
3336 ER_CUT_VALUE_GROUP_CONCAT needs an argument, but this gets set in
3337 Item_func_group_concat::cleanup().
3338 */
3339 assert(table);
3340 warning= push_warning(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3341 ER_CUT_VALUE_GROUP_CONCAT,
3342 ER(ER_CUT_VALUE_GROUP_CONCAT));
3343 }
3344 return &result;
3345}
3346
3347
3349{
3350 str->append(STRING_WITH_LEN("group_concat("));
3351 if (distinct)
3352 str->append(STRING_WITH_LEN("distinct "));
3353 for (uint32_t i= 0; i < arg_count_field; i++)
3354 {
3355 if (i)
3356 str->append(',');
3357 args[i]->print(str);
3358 }
3359 if (arg_count_order)
3360 {
3361 str->append(STRING_WITH_LEN(" order by "));
3362 for (uint32_t i= 0 ; i < arg_count_order ; i++)
3363 {
3364 if (i)
3365 str->append(',');
3366 (*order[i]->item)->print(str);
3367 if (order[i]->asc)
3368 str->append(STRING_WITH_LEN(" ASC"));
3369 else
3370 str->append(STRING_WITH_LEN(" DESC"));
3371 }
3372 }
3373 str->append(STRING_WITH_LEN(" separator \'"));
3374 str->append(*separator);
3375 str->append(STRING_WITH_LEN("\')"));
3376}
3377
3378
3379Item_func_group_concat::~Item_func_group_concat()
3380{
3381 if (!original && unique_filter)
3382 delete unique_filter;
3383}
3384
3385} /* namespace drizzled */
void init_for_tmp_table(enum_field_types sql_type_arg, uint32_t max_length, uint32_t decimals, bool maybe_null)
Table * table
Definition field.h:83
String * val_str(String *)
Definition item/sum.cc:2388
type::Decimal * val_decimal(type::Decimal *)
Definition item/sum.cc:2368
friend int group_concat_key_cmp_with_distinct(void *arg, const void *key1, const void *key2)
Definition item/sum.cc:2801
virtual void print(String *str)
Definition item/sum.cc:3348
friend int group_concat_key_cmp_with_order(void *arg, const void *key1, const void *key2)
Definition item/sum.cc:2835
Item_func_group_concat(Name_resolution_context *context_arg, bool is_distinct, List< Item > *is_select, SQL_LIST *is_order, String *is_separator)
Definition item/sum.cc:2950
String * val_str(String *str)
Definition item/sum.cc:3325
friend int dump_leaf_key(unsigned char *key, uint32_t, Item_func_group_concat *group_concat_item)
Definition item/sum.cc:2879
type::Decimal * val_decimal(type::Decimal *)
Definition item/sum.cc:2413
type::Decimal * val_decimal(type::Decimal *)
Definition item/sum.cc:1312
String * val_str(String *str)
Definition item/sum.cc:1337
friend int composite_key_cmp(void *arg, unsigned char *key1, unsigned char *key2)
Definition item/sum.cc:2500
String * val_str(String *str)
Definition item/sum.cc:1147
bool setup(Session *session)
Definition item/sum.cc:1016
type::Decimal * val_decimal(type::Decimal *)
Definition item/sum.cc:1131
String * val_str(String *)
Definition item/sum.cc:1696
type::Decimal * val_decimal(type::Decimal *)
Definition item/sum.cc:1666
type::Decimal * val_decimal(type::Decimal *)
Definition item/sum.cc:604
String * val_str(String *str)
Definition item/sum.cc:598
String * val_str(String *str)
Definition item/sum.cc:578
type::Decimal * val_decimal(type::Decimal *)
Definition item/sum.cc:591
type::Decimal * val_decimal(type::Decimal *)
Definition item/sum.cc:883
String * val_str(String *str)
Definition item/sum.cc:875
Field * create_tmp_field(bool group, Table *table, uint32_t convert_blob_length)
Definition item/sum.cc:1466
type::Decimal * val_decimal(type::Decimal *)
Definition item/sum.cc:1540
virtual void print(String *str)
Definition item/sum.cc:450
bool register_sum_func(Session *session, Item **ref)
Definition item/sum.cc:319
bool init_sum_func_check(Session *session)
Definition item/sum.cc:77
bool check_sum_func(Session *session, Item **ref)
Definition item/sum.cc:148
type::Decimal * val_decimal(type::Decimal *dec_buf)
Definition item/sum.h:713
bool fixed
Definition item.h:122
virtual double val_real()=0
bool null_value
Definition item.h:124
Field * make_string_field(Table *table)
Definition item.cc:1103
const char * name
Definition item.h:112
bool maybe_null
Definition item.h:123
String str_value
Definition item.h:109
drizzle_system_variables & variables
Definition session.h:195
int store(uint32_t mask, const char *from, uint32_t length, const charset_info_st *charset)
Convert string for decimal when string can be in some multibyte charset.
bool copy_funcs(Item **func_ptr, const Session *session)
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List< Item > &fields, bool reset_with_sum_func)
void copy_fields(Tmp_Table_Param *param)
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables, List< Item > &fields, List< Item > &all_fields, Order *order)
TODO: Rename this file - func.h is stupid.
int class_decimal2string(const type::Decimal *d, uint32_t fixed_dec, String *str)
Converting decimal to string.
int group_concat_key_cmp_with_distinct(void *arg, const void *key1, const void *key2)
Definition item/sum.cc:2801
int group_concat_key_cmp_with_order(void *arg, const void *key1, const void *key2)
Definition item/sum.cc:2835
Field * create_tmp_field_from_field(Session *session, Field *org_field, const char *name, Table *table, Item_field *item, uint32_t convert_blob_length)
Table * create_tmp_table(Session *session, Tmp_Table_Param *param, List< Item > &fields, Order *group, bool distinct, bool save_sum_fields, uint64_t select_options, ha_rows rows_limit, const char *alias)
int composite_key_cmp(void *arg, unsigned char *key1, unsigned char *key2)
Definition item/sum.cc:2500
int dump_leaf_key(unsigned char *key, uint32_t, Item_func_group_concat *item)
Definition item/sum.cc:2879
int class_decimal_cmp(const type::Decimal *a, const type::Decimal *b)
static void variance_fp_recurrence_next(double *m, double *s, uint64_t *count, double nr)
Definition item/sum.cc:1380
STL namespace.