28 #include "sql_resolver.h"
29 #include "sql_optimizer.h"
41 return min(thd->variables.tmp_table_size,
42 thd->variables.max_heap_table_size);
70 if (!thd->lex->allow_sum_func)
72 my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
77 in_sum_func= thd->lex->in_sum_func;
79 thd->lex->in_sum_func=
this;
80 nest_level= thd->lex->current_select->nest_level;
85 max_sum_func_level= -1;
142 nesting_map allow_sum_func= thd->lex->allow_sum_func;
150 if (nest_level == max_arg_level)
157 invalid= !(allow_sum_func & ((nesting_map)1 << max_arg_level));
159 else if (max_arg_level >= 0 ||
160 !(allow_sum_func & ((nesting_map)1 << nest_level)))
169 invalid= aggr_level < 0 &&
170 !(allow_sum_func & ((nesting_map)1 << nest_level));
171 if (!invalid && thd->variables.sql_mode & MODE_ANSI)
172 invalid= aggr_level < 0 && max_arg_level < nest_level;
174 if (!invalid && aggr_level < 0)
176 aggr_level= nest_level;
177 aggr_sel= thd->lex->current_select;
190 invalid= aggr_level <= max_sum_func_level;
193 my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
214 if (in_sum_func->nest_level >= aggr_level)
215 set_if_bigger(in_sum_func->max_sum_func_level, aggr_level);
216 set_if_bigger(in_sum_func->max_sum_func_level, max_sum_func_level);
223 if (outer_fields.elements)
253 while ((field= of++))
255 SELECT_LEX *sel= field->cached_table->select_lex;
256 if (sel->nest_level < aggr_level)
264 in_sum_func->outer_fields.push_back(field);
267 sel->set_non_agg_field_used(
true);
269 if (sel->nest_level > aggr_level &&
270 (sel->agg_func_used()) &&
271 !sel->group_list.elements)
273 my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
274 ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
279 aggr_sel->set_agg_func_used(
true);
280 update_used_tables();
281 thd->lex->in_sum_func= in_sum_func;
312 nesting_map allow_sum_func= thd->lex->allow_sum_func;
316 for (SELECT_LEX *sl= thd->lex->current_select->outer_select();
317 sl && sl->nest_level >= max_arg_level;
318 sl= sl->outer_select())
320 if (allow_sum_func & ((nesting_map)1 << sl->nest_level))
322 aggr_level= sl->nest_level;
331 if (!aggr_sel->inner_sum_func_list)
335 next= aggr_sel->inner_sum_func_list->next;
336 aggr_sel->inner_sum_func_list->next=
this;
338 aggr_sel->inner_sum_func_list=
this;
339 aggr_sel->with_sum_func=
true;
356 for (SELECT_LEX *sl= thd->lex->current_select;
357 sl && sl != aggr_sel && sl->master_unit()->item;
358 sl= sl->outer_select())
359 sl->master_unit()->item->with_sum_func=
true;
361 thd->lex->current_select->mark_as_dependent(aggr_sel);
366 Item_sum::Item_sum(
List<Item> &list) :next(NULL), arg_count(list.elements),
369 if ((args=(
Item**) sql_alloc(
sizeof(
Item*)*arg_count)))
380 if (!(orig_args= (
Item **) sql_alloc(
sizeof(
Item *) * arg_count)))
397 aggr_sel(item->aggr_sel),
398 nest_level(item->nest_level), aggr_level(item->aggr_level),
399 quick_group(item->quick_group),
400 arg_count(item->arg_count), orig_args(NULL),
401 used_tables_cache(item->used_tables_cache),
402 forced_const(item->forced_const)
407 orig_args=tmp_orig_args;
411 if (!(args= (
Item**) thd->alloc(
sizeof(
Item*)*arg_count)))
413 if (!(orig_args= (
Item**) thd->alloc(
sizeof(
Item*)*arg_count)))
416 memcpy(args, item->args,
sizeof(
Item*)*arg_count);
417 memcpy(orig_args, item->orig_args,
sizeof(
Item*)*arg_count);
419 with_distinct= item->with_distinct;
421 set_aggregator(item->
aggr->Aggrtype());
425 void Item_sum::mark_as_sum_func()
427 SELECT_LEX *cur_select= current_thd->lex->current_select;
428 cur_select->n_sum_items++;
429 cur_select->with_sum_func= 1;
437 Item **pargs= fixed ? orig_args : args;
438 str->append(func_name());
439 for (uint i=0 ; i < arg_count ; i++)
443 pargs[
i]->
print(str, query_type);
448 void Item_sum::fix_num_length_and_dec()
451 for (uint i=0 ; i < arg_count ; i++)
452 set_if_bigger(decimals,args[i]->decimals);
453 max_length=float_length(decimals);
456 Item *Item_sum::get_tmp_table_item(THD *thd)
459 if (sum_item && sum_item->result_field)
461 Field *result_field_tmp= sum_item->result_field;
462 for (uint i=0 ; i < sum_item->arg_count ; i++)
464 Item *arg= sum_item->args[
i];
465 if (!arg->const_item())
467 if (arg->type() == Item::FIELD_ITEM)
468 ((
Item_field*) arg)->field= result_field_tmp++;
470 sum_item->args[
i]=
new Item_field(result_field_tmp++);
478 bool Item_sum::walk (Item_processor processor,
bool walk_subquery,
483 Item **arg,**arg_end;
484 for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
486 if ((*arg)->walk(processor, walk_subquery, argument))
490 return (this->*processor)(argument);
525 aggr_sel == NULL || aggr_sel->inner_sum_func_list == NULL ||
530 aggr_sel->inner_sum_func_list= NULL;
534 for (prev=
this; prev->next !=
this; prev= prev->next)
537 if (aggr_sel->inner_sum_func_list ==
this)
538 aggr_sel->inner_sum_func_list= prev;
548 switch (result_type()) {
550 field=
new Field_double(max_length, maybe_null, item_name.
ptr(), decimals, TRUE);
553 field=
new Field_longlong(max_length, maybe_null, item_name.
ptr(), unsigned_flag);
558 field= Field_new_decimal::create_from_item(
this);
572 void Item_sum::update_used_tables ()
576 used_tables_cache= 0;
577 with_subselect=
false;
578 with_stored_program=
false;
579 for (uint i=0 ; i < arg_count ; i++)
581 args[
i]->update_used_tables();
582 used_tables_cache|= args[
i]->used_tables();
584 with_stored_program|= args[
i]->has_stored_program();
587 used_tables_cache&= PSEUDO_TABLE_BITS;
590 used_tables_cache|= ((table_map)1 << aggr_sel->join->tables) - 1;
595 Item *Item_sum::set_arg(uint i, THD *thd,
Item *new_val)
597 thd->change_item_tree(args + i, new_val);
602 int Item_sum::set_aggregator(Aggregator::Aggregator_type aggregator)
613 if (
aggr && aggregator ==
aggr->Aggrtype())
622 case Aggregator::DISTINCT_AGGREGATOR:
625 case Aggregator::SIMPLE_AGGREGATOR:
629 return aggr ? FALSE : TRUE;
633 void Item_sum::cleanup()
640 Item_result_field::cleanup();
660 static int simple_str_key_cmp(
void* arg, uchar* key1, uchar* key2)
663 return f->cmp(key1, key2);
685 Field **field = aggr->table->field;
686 Field **field_end= field + aggr->table->s->fields;
687 uint32 *lengths=aggr->field_lengths;
688 for (; field < field_end; ++field)
691 int len = *lengths++;
692 int res = f->cmp(key1, key2);
702 static enum enum_field_types
703 calc_tmp_field_type(
enum enum_field_types table_field_type,
704 Item_result result_type)
707 switch (result_type) {
710 if (table_field_type != MYSQL_TYPE_FLOAT)
711 table_field_type= MYSQL_TYPE_DOUBLE;
714 table_field_type= MYSQL_TYPE_LONGLONG;
717 if (table_field_type != MYSQL_TYPE_LONGLONG)
718 table_field_type= MYSQL_TYPE_NEWDECIMAL;
724 return table_field_type;
734 static int simple_raw_key_cmp(
const void* arg,
735 const void* key1,
const void* key2)
737 return memcmp(key1, key2, *(
const uint *) arg);
741 static int item_sum_distinct_walk(
void *element, element_count num_of_dups,
771 if (tree || table || tmp_table_param)
774 if (item_sum->setup(thd))
776 if (item_sum->sum_func() == Item_sum::COUNT_FUNC ||
777 item_sum->sum_func() == Item_sum::COUNT_DISTINCT_FUNC)
780 SELECT_LEX *select_lex= thd->lex->current_select;
782 if (!(tmp_table_param=
new TMP_TABLE_PARAM))
786 for (uint i=0; i < item_sum->get_arg_count() ; i++)
788 Item *item=item_sum->get_arg(i);
789 if (list.push_back(item))
791 if (item->const_item() && item->is_null())
797 tmp_table_param->force_copy_fields= item_sum->has_force_copy_fields();
798 DBUG_ASSERT(table == 0);
810 if (item->type() == Item::FIELD_ITEM &&
811 ((
Item_field*)item)->field->type() == FIELD_TYPE_BIT)
815 if (!(table= create_tmp_table(thd, tmp_table_param, list, (
ORDER*) 0, 1,
817 (select_lex->options | thd->variables.option_bits),
820 table->file->extra(HA_EXTRA_NO_ROWS);
823 if (table->s->db_type() == heap_hton)
829 qsort_cmp2 compare_key;
831 Field **field= table->field;
832 Field **field_end= field + table->s->fields;
833 bool all_binary= TRUE;
835 for (tree_key_length= 0; field < field_end; ++field)
838 enum enum_field_types
type= f->type();
839 tree_key_length+= f->pack_length();
840 if ((type == MYSQL_TYPE_VARCHAR) ||
841 (!f->binary() && (type == MYSQL_TYPE_STRING ||
842 type == MYSQL_TYPE_VAR_STRING)))
850 cmp_arg= (
void*) &tree_key_length;
851 compare_key= (qsort_cmp2) simple_raw_key_cmp;
855 if (table->s->fields == 1)
863 compare_key= (qsort_cmp2) simple_str_key_cmp;
864 cmp_arg= (
void*) table->field[0];
871 cmp_arg= (
void*)
this;
872 field_lengths= (uint32*) thd->alloc(table->s->fields *
sizeof(uint32));
873 for (tree_key_length= 0, length= field_lengths, field= table->field;
874 field < field_end; ++field, ++length)
876 *length= (*field)->pack_length();
877 tree_key_length+= *length;
881 DBUG_ASSERT(tree == 0);
882 tree=
new Unique(compare_key, cmp_arg, tree_key_length,
900 DBUG_ENTER(
"Aggregator_distinct::setup");
910 if (field_list.push_back(&field_def))
913 item_sum->null_value= item_sum->maybe_null= 1;
914 item_sum->quick_group= 0;
916 DBUG_ASSERT(item_sum->get_arg(0)->fixed);
918 arg= item_sum->get_arg(0);
919 if (arg->const_item())
921 (void) arg->val_int();
929 enum enum_field_types field_type;
931 field_type= calc_tmp_field_type(arg->field_type(),
939 if (! (table= create_virtual_tmp_table(thd, field_list)))
943 tree_key_length= table->s->reclength - table->s->null_bytes;
951 tree=
new Unique(simple_raw_key_cmp, &tree_key_length, tree_key_length,
954 DBUG_RETURN(tree == 0);
973 if (item_sum->sum_func() == Item_sum::COUNT_FUNC ||
974 item_sum->sum_func() == Item_sum::COUNT_DISTINCT_FUNC)
978 table->file->extra(HA_EXTRA_NO_CACHE);
980 table->file->extra(HA_EXTRA_WRITE_CACHE);
985 item_sum->null_value= 1;
1011 if (item_sum->sum_func() == Item_sum::COUNT_FUNC ||
1012 item_sum->sum_func() == Item_sum::COUNT_DISTINCT_FUNC)
1016 if (
copy_funcs(tmp_table_param->items_to_copy, table->in_use))
1019 for (
Field **field=table->field ; *field ; field++)
1020 if ((*field)->is_real_null(0))
1031 return tree->unique_add(table->record[0] + table->s->null_bytes);
1033 if ((error= table->file->ha_write_row(table->record[0])) &&
1040 item_sum->get_arg(0)->
save_in_field(table->field[0], FALSE);
1041 if (table->field[0]->is_null())
1044 item_sum->null_value= 0;
1049 return tree->unique_add(table->field[0]->ptr);
1078 if (item_sum->sum_func() == Item_sum::COUNT_FUNC ||
1079 item_sum->sum_func() == Item_sum::COUNT_DISTINCT_FUNC)
1081 DBUG_ASSERT(item_sum->fixed == 1);
1083 if (tree && tree->elements == 0)
1086 sum->count= (longlong) tree->elements_in_tree();
1092 table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
1093 sum->count= table->file->stats.records;
1104 table->field[0]->set_notnull();
1107 if (tree && !endup_done)
1111 tree->walk(item_sum_distinct_walk, (
void*)
this);
1120 Item_sum_num::val_str(
String *str)
1122 return val_string_from_real(str);
1128 return val_decimal_from_real(decimal_value);
1133 Item_sum_int::val_str(
String *str)
1135 return val_string_from_int(str);
1141 return val_decimal_from_int(decimal_value);
1146 Item_sum_num::fix_fields(THD *thd,
Item **ref)
1148 DBUG_ASSERT(fixed == 0);
1155 for (uint i=0 ; i < arg_count ; i++)
1157 if ((!args[i]->fixed && args[i]->fix_fields(thd, args + i)) ||
1158 args[i]->check_cols(1))
1160 set_if_bigger(decimals, args[i]->decimals);
1161 maybe_null |= args[
i]->maybe_null;
1164 max_length=float_length(decimals);
1166 fix_length_and_dec();
1171 memcpy (orig_args, args,
sizeof (
Item *) * arg_count);
1178 Item_sum_hybrid::fix_fields(THD *thd,
Item **ref)
1180 DBUG_ASSERT(fixed == 0);
1182 Item *item= args[0];
1188 if ((!item->fixed && item->fix_fields(thd, args)) ||
1189 (item= args[0])->check_cols(1))
1191 decimals=item->decimals;
1193 switch (hybrid_type= item->result_type()) {
1195 case DECIMAL_RESULT:
1197 max_length= item->max_length;
1200 max_length= float_length(decimals);
1209 unsigned_flag=item->unsigned_flag;
1212 fix_length_and_dec();
1213 item= item->real_item();
1214 if (item->type() == Item::FIELD_ITEM)
1215 hybrid_field_type= ((
Item_field*) item)->field->type();
1217 hybrid_field_type= Item::field_type();
1222 orig_args[0]= args[0];
1242 value= Item_cache::get_cache(item);
1244 value->store(value_arg);
1245 arg_cache= Item_cache::get_cache(item);
1246 arg_cache->setup(item);
1248 cmp->set_cmp_func(
this, (
Item**)&arg_cache, (
Item**)&value, FALSE);
1249 collation.set(item->collation);
1253 Field *Item_sum_hybrid::create_tmp_field(
bool group,
TABLE *table)
1256 if (args[0]->type() == Item::FIELD_ITEM)
1260 if ((field= create_tmp_field_from_field(current_thd, field, item_name.
ptr(),
1262 field->flags&= ~NOT_NULL_FLAG;
1270 switch (args[0]->field_type()) {
1271 case MYSQL_TYPE_DATE:
1274 case MYSQL_TYPE_TIME:
1277 case MYSQL_TYPE_TIMESTAMP:
1278 case MYSQL_TYPE_DATETIME:
1282 return Item_sum::create_tmp_field(group, table);
1299 :
Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
1300 curr_dec_buff(item->curr_dec_buff)
1303 if (hybrid_type == DECIMAL_RESULT)
1305 my_decimal2decimal(item->dec_buffs, dec_buffs);
1306 my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
1312 Item *Item_sum_sum::copy_or_same(THD* thd)
1318 void Item_sum_sum::clear()
1320 DBUG_ENTER(
"Item_sum_sum::clear");
1322 if (hybrid_type == DECIMAL_RESULT)
1325 my_decimal_set_zero(dec_buffs);
1333 void Item_sum_sum::fix_length_and_dec()
1335 DBUG_ENTER(
"Item_sum_sum::fix_length_and_dec");
1336 maybe_null=null_value=1;
1337 decimals= args[0]->decimals;
1340 hybrid_type= REAL_RESULT;
1344 case DECIMAL_RESULT:
1347 int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
1348 max_length= my_decimal_precision_to_length_no_truncation(precision,
1352 hybrid_type= DECIMAL_RESULT;
1353 my_decimal_set_zero(dec_buffs);
1361 DBUG_PRINT(
"info", (
"Type: %s (%d, %d)",
1362 (hybrid_type == REAL_RESULT ?
"REAL_RESULT" :
1363 hybrid_type == DECIMAL_RESULT ?
"DECIMAL_RESULT" :
1364 hybrid_type == INT_RESULT ?
"INT_RESULT" :
1372 bool Item_sum_sum::add()
1374 DBUG_ENTER(
"Item_sum_sum::add");
1375 if (hybrid_type == DECIMAL_RESULT)
1381 my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
1382 val, dec_buffs + curr_dec_buff);
1397 longlong Item_sum_sum::val_int()
1399 DBUG_ASSERT(fixed == 1);
1402 if (hybrid_type == DECIMAL_RESULT)
1405 my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
1409 return (longlong) rint(val_real());
1413 double Item_sum_sum::val_real()
1415 DBUG_ASSERT(fixed == 1);
1418 if (hybrid_type == DECIMAL_RESULT)
1419 my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
1428 if (hybrid_type == DECIMAL_RESULT)
1429 return val_string_from_decimal(str);
1430 return val_string_from_real(str);
1438 if (hybrid_type == DECIMAL_RESULT)
1439 return (dec_buffs + curr_dec_buff);
1440 return val_decimal_from_real(val);
1462 memcpy(table->field[0]->ptr, element, tree_key_length);
1468 Aggregator_distinct::~Aggregator_distinct()
1477 free_tmp_table(table->in_use, table);
1480 if (tmp_table_param)
1482 delete tmp_table_param;
1483 tmp_table_param= NULL;
1490 return item_sum->args[0]->val_decimal(value);
1496 return item_sum->args[0]->val_real();
1502 return item_sum->args[0]->null_value;
1509 item_sum->args[0]->val_decimal(value);
1516 item_sum->args[0]->val_real();
1523 item_sum->args[0]->null_value;
1527 Item *Item_sum_count::copy_or_same(THD* thd)
1533 void Item_sum_count::clear()
1539 bool Item_sum_count::add()
1541 if (!args[0]->maybe_null || !args[0]->is_null())
1546 longlong Item_sum_count::val_int()
1548 DBUG_ASSERT(fixed == 1);
1551 return (longlong) count;
1555 void Item_sum_count::cleanup()
1557 DBUG_ENTER(
"Item_sum_count::cleanup");
1559 Item_sum_int::cleanup();
1567 void Item_sum_avg::fix_length_and_dec()
1569 Item_sum_sum::fix_length_and_dec();
1570 maybe_null=null_value=1;
1571 prec_increment= current_thd->variables.div_precincrement;
1572 if (hybrid_type == DECIMAL_RESULT)
1574 int precision= args[0]->decimal_precision() + prec_increment;
1575 decimals= min<uint>(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1576 max_length= my_decimal_precision_to_length_no_truncation(precision,
1580 f_scale= args[0]->decimals;
1581 dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1584 decimals= min<uint>(args[0]->decimals + prec_increment, NOT_FIXED_DEC);
1585 max_length= args[0]->max_length + prec_increment;
1590 Item *Item_sum_avg::copy_or_same(THD* thd)
1596 Field *Item_sum_avg::create_tmp_field(
bool group,
TABLE *table)
1606 field=
new Field_string(((hybrid_type == DECIMAL_RESULT) ?
1607 dec_bin_size :
sizeof(
double)) +
sizeof(longlong),
1608 0, item_name.
ptr(), &my_charset_bin);
1610 else if (hybrid_type == DECIMAL_RESULT)
1611 field= Field_new_decimal::create_from_item(
this);
1613 field=
new Field_double(max_length, maybe_null, item_name.
ptr(), decimals, TRUE);
1620 void Item_sum_avg::clear()
1622 Item_sum_sum::clear();
1627 bool Item_sum_avg::add()
1629 if (Item_sum_sum::add())
1636 double Item_sum_avg::val_real()
1638 DBUG_ASSERT(fixed == 1);
1646 return Item_sum_sum::val_real() / ulonglong2double(count);
1654 DBUG_ASSERT(fixed == 1);
1667 if (hybrid_type != DECIMAL_RESULT)
1668 return val_decimal_from_real(val);
1670 sum_dec= dec_buffs + curr_dec_buff;
1671 int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
1672 my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1681 if (hybrid_type == DECIMAL_RESULT)
1682 return val_string_from_decimal(str);
1683 return val_string_from_real(str);
1691 double Item_sum_std::val_real()
1693 DBUG_ASSERT(fixed == 1);
1694 double nr= Item_sum_variance::val_real();
1695 DBUG_ASSERT(nr >= 0.0);
1699 Item *Item_sum_std::copy_or_same(THD* thd)
1722 static void variance_fp_recurrence_next(
double *m,
double *s, ulonglong *count,
double nr)
1733 double m_kminusone= *m;
1734 *m= m_kminusone + (nr - m_kminusone) / (
double) *count;
1735 *s= *s + (nr - m_kminusone) * (nr - *m);
1740 static double variance_fp_recurrence_result(
double s, ulonglong count,
bool is_sample_variance)
1745 if (is_sample_variance)
1746 return s / (count - 1);
1754 Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
1755 count(item->count), sample(item->sample),
1756 prec_increment(item->prec_increment)
1758 recurrence_m= item->recurrence_m;
1759 recurrence_s= item->recurrence_s;
1763 void Item_sum_variance::fix_length_and_dec()
1765 DBUG_ENTER(
"Item_sum_variance::fix_length_and_dec");
1766 maybe_null= null_value= 1;
1774 hybrid_type= REAL_RESULT;
1775 decimals= NOT_FIXED_DEC;
1776 max_length= float_length(decimals);
1778 DBUG_PRINT(
"info", (
"Type: REAL_RESULT (%d, %d)", max_length, (
int)decimals));
1783 Item *Item_sum_variance::copy_or_same(THD* thd)
1804 field=
new Field_string(
sizeof(
double)*2 +
sizeof(longlong), 0, item_name.
ptr(), &my_charset_bin);
1807 field=
new Field_double(max_length, maybe_null, item_name.
ptr(), decimals, TRUE);
1816 void Item_sum_variance::clear()
1821 bool Item_sum_variance::add()
1827 double nr= args[0]->val_real();
1829 if (!args[0]->null_value)
1830 variance_fp_recurrence_next(&recurrence_m, &recurrence_s, &count, nr);
1834 double Item_sum_variance::val_real()
1836 DBUG_ASSERT(fixed == 1);
1847 DBUG_ASSERT((sample == 0) || (sample == 1));
1848 if (count <= sample)
1855 return variance_fp_recurrence_result(recurrence_s, count, sample);
1861 DBUG_ASSERT(fixed == 1);
1862 return val_decimal_from_real(dec_buf);
1866 void Item_sum_variance::reset_field()
1869 uchar *res= result_field->ptr;
1871 nr= args[0]->val_real();
1873 if (args[0]->null_value)
1874 memset(res, 0,
sizeof(
double)*2+
sizeof(longlong));
1878 ulonglong tmp_count;
1880 float8store(res, nr);
1882 float8store(res +
sizeof(
double), tmp_s);
1884 int8store(res +
sizeof(
double)*2, tmp_count);
1889 void Item_sum_variance::update_field()
1891 ulonglong field_count;
1892 uchar *res=result_field->ptr;
1894 double nr= args[0]->val_real();
1896 if (args[0]->null_value)
1900 double field_recurrence_m, field_recurrence_s;
1901 float8get(field_recurrence_m, res);
1902 float8get(field_recurrence_s, res +
sizeof(
double));
1903 field_count=sint8korr(res+
sizeof(
double)*2);
1905 variance_fp_recurrence_next(&field_recurrence_m, &field_recurrence_s, &field_count, nr);
1907 float8store(res, field_recurrence_m);
1908 float8store(res +
sizeof(
double), field_recurrence_s);
1909 res+=
sizeof(double)*2;
1910 int8store(res,field_count);
1916 void Item_sum_hybrid::clear()
1922 double Item_sum_hybrid::val_real()
1924 DBUG_ASSERT(fixed == 1);
1927 double retval= value->val_real();
1928 if ((null_value= value->null_value))
1929 DBUG_ASSERT(retval == 0.0);
1933 longlong Item_sum_hybrid::val_int()
1935 DBUG_ASSERT(fixed == 1);
1938 longlong retval= value->val_int();
1939 if ((null_value= value->null_value))
1940 DBUG_ASSERT(retval == 0);
1947 DBUG_ASSERT(fixed == 1);
1950 longlong retval= value->val_time_temporal();
1951 if ((null_value= value->null_value))
1952 DBUG_ASSERT(retval == 0);
1959 DBUG_ASSERT(fixed == 1);
1962 longlong retval= value->val_date_temporal();
1963 if ((null_value= value->null_value))
1964 DBUG_ASSERT(retval == 0);
1971 DBUG_ASSERT(fixed == 1);
1975 if ((null_value= value->null_value))
1976 DBUG_ASSERT(retval == NULL);
1981 bool Item_sum_hybrid::get_date(
MYSQL_TIME *ltime, uint fuzzydate)
1983 DBUG_ASSERT(fixed == 1);
1986 return (null_value= value->get_date(ltime, fuzzydate));
1990 bool Item_sum_hybrid::get_time(
MYSQL_TIME *ltime)
1992 DBUG_ASSERT(fixed == 1);
1995 return (null_value= value->get_time(ltime));
2000 Item_sum_hybrid::val_str(
String *str)
2002 DBUG_ASSERT(fixed == 1);
2005 String *retval= value->val_str(str);
2006 if ((null_value= value->null_value))
2007 DBUG_ASSERT(retval == NULL);
2012 void Item_sum_hybrid::cleanup()
2014 DBUG_ENTER(
"Item_sum_hybrid::cleanup");
2015 Item_sum::cleanup();
2016 forced_const= FALSE;
2038 Item *Item_sum_min::copy_or_same(THD* thd)
2046 bool Item_sum_min::add()
2049 arg_cache->cache_value();
2050 if (!arg_cache->null_value &&
2051 (null_value || cmp->compare() < 0))
2053 value->store(arg_cache);
2054 value->cache_value();
2061 Item *Item_sum_max::copy_or_same(THD* thd)
2069 bool Item_sum_max::add()
2072 arg_cache->cache_value();
2073 if (!arg_cache->null_value &&
2074 (null_value || cmp->compare() > 0))
2076 value->store(arg_cache);
2077 value->cache_value();
2086 longlong Item_sum_bit::val_int()
2088 DBUG_ASSERT(fixed == 1);
2089 return (longlong) bits;
2093 void Item_sum_bit::clear()
2098 Item *Item_sum_or::copy_or_same(THD* thd)
2100 return new (thd->mem_root)
Item_sum_or(thd,
this);
2104 bool Item_sum_or::add()
2106 ulonglong value= (ulonglong) args[0]->val_int();
2107 if (!args[0]->null_value)
2112 Item *Item_sum_xor::copy_or_same(THD* thd)
2118 bool Item_sum_xor::add()
2120 ulonglong value= (ulonglong) args[0]->val_int();
2121 if (!args[0]->null_value)
2126 Item *Item_sum_and::copy_or_same(THD* thd)
2132 bool Item_sum_and::add()
2134 ulonglong value= (ulonglong) args[0]->val_int();
2135 if (!args[0]->null_value)
2144 void Item_sum_num::reset_field()
2146 double nr= args[0]->val_real();
2147 uchar *res=result_field->ptr;
2151 if (args[0]->null_value)
2154 result_field->set_null();
2157 result_field->set_notnull();
2159 float8store(res,nr);
2163 void Item_sum_hybrid::reset_field()
2165 switch(hybrid_type) {
2168 if (args[0]->is_temporal())
2173 if (args[0]->null_value)
2176 result_field->set_null();
2179 result_field->set_notnull();
2185 char buff[MAX_FIELD_WIDTH];
2186 String tmp(buff,
sizeof(buff),result_field->charset()),*res;
2188 res=args[0]->val_str(&tmp);
2189 if (args[0]->null_value)
2191 result_field->set_null();
2192 result_field->reset();
2196 result_field->set_notnull();
2197 result_field->store(res->ptr(),res->length(),tmp.charset());
2203 longlong nr=args[0]->val_int();
2207 if (args[0]->null_value)
2210 result_field->set_null();
2213 result_field->set_notnull();
2215 result_field->store(nr, unsigned_flag);
2220 double nr= args[0]->val_real();
2224 if (args[0]->null_value)
2227 result_field->set_null();
2230 result_field->set_notnull();
2232 result_field->store(nr);
2235 case DECIMAL_RESULT:
2237 my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2241 if (args[0]->null_value)
2242 result_field->set_null();
2244 result_field->set_notnull();
2251 arg_dec= &decimal_zero;
2252 result_field->store_decimal(arg_dec);
2262 void Item_sum_sum::reset_field()
2264 DBUG_ASSERT (
aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2265 if (hybrid_type == DECIMAL_RESULT)
2267 my_decimal value, *arg_val= args[0]->val_decimal(&value);
2269 arg_val= &decimal_zero;
2270 result_field->store_decimal(arg_val);
2274 DBUG_ASSERT(hybrid_type == REAL_RESULT);
2275 double nr= args[0]->val_real();
2276 float8store(result_field->ptr, nr);
2278 if (args[0]->null_value)
2279 result_field->set_null();
2281 result_field->set_notnull();
2285 void Item_sum_count::reset_field()
2287 uchar *res=result_field->ptr;
2289 DBUG_ASSERT (
aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2291 if (!args[0]->maybe_null || !args[0]->is_null())
2297 void Item_sum_avg::reset_field()
2299 uchar *res=result_field->ptr;
2300 DBUG_ASSERT (
aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2301 if (hybrid_type == DECIMAL_RESULT)
2304 my_decimal value, *arg_dec= args[0]->val_decimal(&value);
2305 if (args[0]->null_value)
2307 arg_dec= &decimal_zero;
2312 my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
2314 int8store(res, tmp);
2318 double nr= args[0]->val_real();
2320 if (args[0]->null_value)
2321 memset(res, 0,
sizeof(
double)+
sizeof(longlong));
2325 float8store(res,nr);
2326 res+=
sizeof(double);
2333 void Item_sum_bit::reset_field()
2336 int8store(result_field->ptr, bits);
2339 void Item_sum_bit::update_field()
2341 uchar *res=result_field->ptr;
2342 bits= uint8korr(res);
2344 int8store(res, bits);
2354 DBUG_ASSERT (
aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2355 if (hybrid_type == DECIMAL_RESULT)
2357 my_decimal value, *arg_val= args[0]->val_decimal(&value);
2358 if (!args[0]->null_value)
2360 if (!result_field->is_null())
2363 *field_val= result_field->val_decimal(&field_value);
2364 my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2365 result_field->store_decimal(dec_buffs);
2369 result_field->store_decimal(arg_val);
2370 result_field->set_notnull();
2377 uchar *res=result_field->ptr;
2379 float8get(old_nr,res);
2380 nr= args[0]->val_real();
2381 if (!args[0]->null_value)
2384 result_field->set_notnull();
2386 float8store(res,old_nr);
2391 void Item_sum_count::update_field()
2394 uchar *res=result_field->ptr;
2397 if (!args[0]->maybe_null || !args[0]->is_null())
2405 longlong field_count;
2406 uchar *res=result_field->ptr;
2408 DBUG_ASSERT (
aggr->Aggrtype() != Aggregator::DISTINCT_AGGREGATOR);
2410 if (hybrid_type == DECIMAL_RESULT)
2412 my_decimal value, *arg_val= args[0]->val_decimal(&value);
2413 if (!args[0]->null_value)
2415 binary2my_decimal(E_DEC_FATAL_ERROR, res,
2416 dec_buffs + 1, f_precision, f_scale);
2417 field_count= sint8korr(res + dec_bin_size);
2418 my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
2419 my_decimal2binary(E_DEC_FATAL_ERROR, dec_buffs,
2420 res, f_precision, f_scale);
2423 int8store(res, field_count);
2430 nr= args[0]->val_real();
2431 if (!args[0]->null_value)
2434 float8get(old_nr, res);
2435 field_count= sint8korr(res +
sizeof(
double));
2437 float8store(res,old_nr);
2438 res+=
sizeof(double);
2440 int8store(res, field_count);
2446 void Item_sum_hybrid::update_field()
2448 switch (hybrid_type) {
2450 if (args[0]->is_temporal())
2451 min_max_update_temporal_field();
2453 min_max_update_str_field();
2456 min_max_update_int_field();
2458 case DECIMAL_RESULT:
2462 min_max_update_real_field();
2467 void Item_sum_hybrid::min_max_update_temporal_field()
2469 longlong nr, old_nr;
2472 if (!args[0]->null_value)
2474 if (result_field->is_null(0))
2478 bool res= unsigned_flag ?
2479 (ulonglong) old_nr > (ulonglong) nr : old_nr > nr;
2480 if ((cmp_sign > 0) ^ (!res))
2483 result_field->set_notnull();
2485 else if (result_field->is_null(0))
2486 result_field->set_null();
2487 result_field->store_packed(old_nr);
2494 String *res_str=args[0]->val_str(&cmp->value1);
2496 if (!args[0]->null_value)
2498 result_field->val_str(&cmp->value2);
2500 if (result_field->is_null() ||
2501 (cmp_sign * sortcmp(res_str,&cmp->value2,collation.collation)) < 0)
2502 result_field->store(res_str->ptr(),res_str->length(),res_str->charset());
2503 result_field->set_notnull();
2508 void Item_sum_hybrid::min_max_update_real_field()
2512 old_nr=result_field->val_real();
2513 nr= args[0]->val_real();
2514 if (!args[0]->null_value)
2516 if (result_field->is_null(0) ||
2517 (cmp_sign > 0 ? old_nr > nr : old_nr < nr))
2519 result_field->set_notnull();
2521 else if (result_field->is_null(0))
2522 result_field->set_null();
2523 result_field->store(old_nr);
2527 void Item_sum_hybrid::min_max_update_int_field()
2531 old_nr=result_field->val_int();
2532 nr=args[0]->val_int();
2533 if (!args[0]->null_value)
2535 if (result_field->is_null(0))
2539 bool res=(unsigned_flag ?
2540 (ulonglong) old_nr > (ulonglong) nr :
2543 if ((cmp_sign > 0) ^ (!res))
2546 result_field->set_notnull();
2548 else if (result_field->is_null(0))
2549 result_field->set_null();
2550 result_field->store(old_nr, unsigned_flag);
2562 const my_decimal *old_nr= result_field->val_decimal(&old_val);
2563 const my_decimal *nr= args[0]->val_decimal(&nr_val);
2564 if (!args[0]->null_value)
2566 if (result_field->is_null(0))
2572 if ((cmp_sign > 0) ^ (!res))
2575 result_field->set_notnull();
2577 else if (result_field->is_null(0))
2578 result_field->set_null();
2579 result_field->store_decimal(old_nr);
2583 Item_avg_field::Item_avg_field(Item_result res_type,
Item_sum_avg *item)
2585 item_name= item->item_name;
2586 decimals=item->decimals;
2587 max_length= item->max_length;
2588 unsigned_flag= item->unsigned_flag;
2589 field=item->result_field;
2591 hybrid_type= res_type;
2592 prec_increment= item->prec_increment;
2593 if (hybrid_type == DECIMAL_RESULT)
2595 f_scale= item->f_scale;
2596 f_precision= item->f_precision;
2597 dec_bin_size= item->dec_bin_size;
2601 double Item_avg_field::val_real()
2608 if (hybrid_type == DECIMAL_RESULT)
2609 return val_real_from_decimal();
2611 float8get(nr,field->ptr);
2612 res= (field->ptr+
sizeof(double));
2613 count= sint8korr(res);
2615 if ((null_value= !count))
2617 return nr/(double) count;
2624 if (hybrid_type == REAL_RESULT)
2625 return val_decimal_from_real(dec_buf);
2627 longlong count= sint8korr(field->ptr + dec_bin_size);
2628 if ((null_value= !count))
2632 binary2my_decimal(E_DEC_FATAL_ERROR,
2633 field->ptr, &dec_field, f_precision, f_scale);
2634 int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
2635 my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2636 &dec_field, &dec_count, prec_increment);
2644 if (hybrid_type == DECIMAL_RESULT)
2645 return val_string_from_decimal(str);
2646 return val_string_from_real(str);
2656 double Item_std_field::val_real()
2660 nr= Item_variance_field::val_real();
2661 DBUG_ASSERT(nr >= 0.0);
2674 if (hybrid_type == REAL_RESULT)
2675 return val_decimal_from_real(dec_buf);
2677 dec= Item_variance_field::val_decimal(dec_buf);
2680 my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2681 DBUG_ASSERT(nr >= 0.0);
2683 double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2684 my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, FALSE, dec_buf);
2691 item_name= item->item_name;
2692 decimals=item->decimals;
2693 max_length=item->max_length;
2694 unsigned_flag= item->unsigned_flag;
2695 field=item->result_field;
2697 sample= item->sample;
2698 prec_increment= item->prec_increment;
2699 if ((hybrid_type= item->hybrid_type) == DECIMAL_RESULT)
2701 f_scale0= item->f_scale0;
2702 f_precision0= item->f_precision0;
2703 dec_bin_size0= item->dec_bin_size0;
2704 f_scale1= item->f_scale1;
2705 f_precision1= item->f_precision1;
2706 dec_bin_size1= item->dec_bin_size1;
2711 double Item_variance_field::val_real()
2714 if (hybrid_type == DECIMAL_RESULT)
2715 return val_real_from_decimal();
2717 double recurrence_s;
2719 float8get(recurrence_s, (field->ptr +
sizeof(
double)));
2720 count=sint8korr(field->ptr+
sizeof(
double)*2);
2722 if ((null_value= (count <= sample)))
2725 return variance_fp_recurrence_result(recurrence_s, count, sample);
2738 void Item_udf_sum::clear()
2740 DBUG_ENTER(
"Item_udf_sum::clear");
2745 bool Item_udf_sum::add()
2747 DBUG_ENTER(
"Item_udf_sum::add");
2748 udf.add(&null_value);
2752 void Item_udf_sum::cleanup()
2759 Item_sum::cleanup();
2763 void Item_udf_sum::print(
String *str, enum_query_type query_type)
2765 str->append(func_name());
2767 for (uint i=0 ; i < arg_count ; i++)
2771 args[
i]->print(str, query_type);
2777 Item *Item_sum_udf_float::copy_or_same(THD* thd)
2782 double Item_sum_udf_float::val_real()
2784 DBUG_ASSERT(fixed == 1);
2785 DBUG_ENTER(
"Item_sum_udf_float::val");
2786 DBUG_PRINT(
"info",(
"result_type: %d arg_count: %d",
2787 args[0]->result_type(), arg_count));
2788 DBUG_RETURN(udf.val(&null_value));
2794 return val_string_from_real(str);
2800 return val_decimal_from_real(dec);
2806 return val_string_from_decimal(str);
2810 double Item_sum_udf_decimal::val_real()
2812 return val_real_from_decimal();
2816 longlong Item_sum_udf_decimal::val_int()
2818 return val_int_from_decimal();
2824 DBUG_ASSERT(fixed == 1);
2825 DBUG_ENTER(
"Item_func_udf_decimal::val_decimal");
2826 DBUG_PRINT(
"info",(
"result_type: %d arg_count: %d",
2827 args[0]->result_type(), arg_count));
2829 DBUG_RETURN(udf.val_decimal(&null_value, dec_buf));
2833 Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
2839 Item *Item_sum_udf_int::copy_or_same(THD* thd)
2844 longlong Item_sum_udf_int::val_int()
2846 DBUG_ASSERT(fixed == 1);
2847 DBUG_ENTER(
"Item_sum_udf_int::val_int");
2848 DBUG_PRINT(
"info",(
"result_type: %d arg_count: %d",
2849 args[0]->result_type(), arg_count));
2850 DBUG_RETURN(udf.val_int(&null_value));
2856 return val_string_from_int(str);
2861 return val_decimal_from_int(dec);
2867 void Item_sum_udf_str::fix_length_and_dec()
2869 DBUG_ENTER(
"Item_sum_udf_str::fix_length_and_dec");
2871 for (uint i = 0; i < arg_count; i++)
2872 set_if_bigger(max_length,args[i]->max_length);
2877 Item *Item_sum_udf_str::copy_or_same(THD* thd)
2885 return val_decimal_from_string(dec);
2890 DBUG_ASSERT(fixed == 1);
2891 DBUG_ENTER(
"Item_sum_udf_str::str");
2892 String *res=udf.val_str(str,&str_value);
2935 TABLE *table= item_func->table;
2937 for (uint i= 0; i < item_func->arg_count_field; i++)
2939 Item *item= item_func->args[
i];
2944 if (item->const_item())
2951 Field *field= item->get_tmp_table_field();
2956 uint
offset= field->offset(field->table->record[0])-table->s->null_bytes;
2957 int res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset);
2974 ORDER **order_item, **end;
2975 TABLE *table= grp_item->table;
2977 for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2981 Item *item= *(*order_item)->item;
2986 if (item->const_item())
2993 Field *field= item->get_tmp_table_field();
2997 uint
offset= (field->offset(field->table->record[0]) -
2998 table->s->null_bytes);
2999 int res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset);
3001 return ((*order_item)->direction == ORDER::ORDER_ASC) ? res : -res;
3021 TABLE *table= item->table;
3022 String tmp((
char *)table->record[1], table->s->reclength,
3023 default_charset_info);
3025 uchar *key= (uchar *) key_arg;
3026 String *result= &item->result;
3027 Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
3028 uint old_length= result->length();
3030 if (item->no_appended)
3031 item->no_appended= FALSE;
3033 result->append(*item->separator);
3037 for (; arg < arg_end; arg++)
3047 if ((*arg)->const_item())
3048 res= (*arg)->val_str(&tmp);
3051 Field *field= (*arg)->get_tmp_table_field();
3054 uint
offset= (field->offset(field->table->record[0]) -
3055 table->s->null_bytes);
3056 DBUG_ASSERT(offset < table->s->reclength);
3057 res= field->val_str(&tmp, key + offset);
3060 res= (*arg)->val_str(&tmp);
3063 result->append(*res);
3069 if (result->length() > item->max_length)
3071 int well_formed_error;
3073 const char *ptr= result->ptr();
3080 add_length= cs->cset->well_formed_len(cs,
3082 ptr + item->max_length,
3084 &well_formed_error);
3085 result->length(old_length + add_length);
3086 item->warning_for_row= TRUE;
3087 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
3088 ER_CUT_VALUE_GROUP_CONCAT, ER(ER_CUT_VALUE_GROUP_CONCAT),
3116 :tmp_table_param(0), separator(separator_arg), tree(0),
3117 unique_filter(NULL), table(0),
3118 order(0), context(context_arg),
3119 arg_count_order(order_list.elements),
3120 arg_count_field(select_list->elements),
3122 distinct(distinct_arg),
3123 warning_for_row(FALSE),
3124 force_copy_fields(0), original(0)
3130 arg_count= arg_count_field + arg_count_order;
3138 if (!(args= (
Item**) sql_alloc(
sizeof(
Item*) * arg_count +
3139 sizeof(
ORDER*)*arg_count_order)))
3142 if (!(orig_args= (
Item **) sql_alloc(
sizeof(
Item *) * arg_count)))
3148 order= (
ORDER**)(args + arg_count);
3153 for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
3154 *arg_ptr= item_select;
3156 if (arg_count_order)
3158 ORDER **order_ptr= order;
3161 order_item= order_item->next)
3163 (*order_ptr++)= order_item;
3164 *arg_ptr= *order_item->item;
3165 order_item->item= arg_ptr++;
3168 memcpy(orig_args, args,
sizeof(
Item*) * arg_count);
3175 tmp_table_param(item->tmp_table_param),
3176 separator(item->separator),
3178 unique_filter(item->unique_filter),
3180 context(item->context),
3181 arg_count_order(item->arg_count_order),
3182 arg_count_field(item->arg_count_field),
3183 row_count(item->row_count),
3184 distinct(item->distinct),
3185 warning_for_row(item->warning_for_row),
3186 always_null(item->always_null),
3187 force_copy_fields(item->force_copy_fields),
3190 quick_group= item->quick_group;
3191 result.set_charset(collation.collation);
3201 if (!(order= (
ORDER **) thd->alloc(
sizeof(
ORDER *) * arg_count_order +
3202 sizeof(
ORDER) * arg_count_order)))
3204 tmp= (
ORDER *)(order + arg_count_order);
3205 for (uint i= 0; i < arg_count_order; i++, tmp++)
3213 new (tmp)
st_order(*(item->order[i]));
3214 tmp->next= (i + 1 == arg_count_order) ? NULL : (tmp + 1);
3221 void Item_func_group_concat::cleanup()
3223 DBUG_ENTER(
"Item_func_group_concat::cleanup");
3224 Item_sum::cleanup();
3232 delete tmp_table_param;
3236 THD *thd= table->in_use;
3239 free_tmp_table(thd, table);
3248 delete unique_filter;
3249 unique_filter= NULL;
3252 DBUG_ASSERT(tree == 0);
3261 DBUG_ASSERT(collation.collation);
3269 const uint32 max_characters= max_length / collation.collation->mbminlen;
3270 if (max_characters > CONVERT_IF_BIGGER_TO_BLOB)
3271 field=
new Field_blob(max_characters * collation.collation->mbmaxlen,
3272 maybe_null, item_name.
ptr(), collation.collation, TRUE);
3274 field=
new Field_varstring(max_characters * collation.collation->mbmaxlen,
3275 maybe_null, item_name.
ptr(), table->s, collation.collation);
3283 Item *Item_func_group_concat::copy_or_same(THD* thd)
3289 void Item_func_group_concat::clear()
3294 warning_for_row= FALSE;
3299 unique_filter->reset();
3306 bool Item_func_group_concat::add()
3311 if (
copy_funcs(tmp_table_param->items_to_copy, table->in_use))
3314 for (uint i= 0; i < arg_count_field; i++)
3316 Item *show_item= args[
i];
3317 if (show_item->const_item())
3320 Field *field= show_item->get_tmp_table_field();
3321 if (field && field->is_null_in_record((
const uchar*) table->record[0]))
3326 bool row_eligible= TRUE;
3331 uint count= unique_filter->elements_in_tree();
3332 unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3333 if (count == unique_filter->elements_in_tree())
3334 row_eligible= FALSE;
3338 if (row_eligible && tree)
3340 DBUG_EXECUTE_IF(
"trigger_OOM_in_gconcat_add",
3341 DBUG_SET(
"+d,simulate_persistent_out_of_memory"););
3342 el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
3344 DBUG_EXECUTE_IF(
"trigger_OOM_in_gconcat_add",
3345 DBUG_SET(
"-d,simulate_persistent_out_of_memory"););
3355 if (row_eligible && !warning_for_row &&
3356 (!tree || (el->count == 1 && distinct && !arg_count_order)))
3357 dump_leaf_key(table->record[0] + table->s->null_bytes, 1,
this);
3364 Item_func_group_concat::fix_fields(THD *thd,
Item **ref)
3367 DBUG_ASSERT(fixed == 0);
3378 for (i=0 ; i < arg_count ; i++)
3380 if ((!args[i]->fixed &&
3381 args[i]->fix_fields(thd, args + i)) ||
3382 args[i]->check_cols(1))
3387 if (agg_item_charsets_for_string_result(collation, func_name(),
3388 args, arg_count - arg_count_order))
3391 result.set_charset(collation.collation);
3394 max_length= thd->variables.group_concat_max_len;
3397 if (separator->needs_conversion(separator->length(), separator->charset(),
3398 collation.collation, &
offset))
3400 uint32 buflen= collation.collation->mbmaxlen * separator->length();
3401 uint
errors, conv_length;
3405 if (!(buf= (
char*) thd->stmt_arena->alloc(buflen)) ||
3406 !(new_separator=
new(thd->stmt_arena->mem_root)
3407 String(buf, buflen, collation.collation)))
3410 conv_length= copy_and_convert(buf, buflen, collation.collation,
3411 separator->ptr(), separator->length(),
3412 separator->charset(), &errors);
3413 new_separator->length(conv_length);
3414 separator= new_separator;
3428 SELECT_LEX *select_lex= thd->lex->current_select;
3429 const bool order_or_distinct=
test(arg_count_order > 0 || distinct);
3430 DBUG_ENTER(
"Item_func_group_concat::setup");
3439 if (!(tmp_table_param=
new TMP_TABLE_PARAM))
3444 for (uint i= 0; i < arg_count_field; i++)
3446 Item *item= args[
i];
3447 if (list.push_back(item))
3449 if (item->const_item())
3451 if (item->is_null())
3466 if (arg_count_order &&
3468 context->table_list, list, all_fields, *order))
3472 tmp_table_param->force_copy_fields= force_copy_fields;
3473 DBUG_ASSERT(table == 0);
3474 if (order_or_distinct)
3485 while ((item= li++))
3487 if (item->type() == Item::FIELD_ITEM &&
3488 ((
Item_field*) item)->field->type() == FIELD_TYPE_BIT)
3500 if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
3501 (
ORDER*) 0, 0, TRUE,
3502 (select_lex->options | thd->variables.option_bits),
3503 HA_POS_ERROR, (
char*)
"")))
3505 table->file->extra(HA_EXTRA_NO_ROWS);
3512 if (order_or_distinct && table->s->blob_fields)
3520 uint tree_key_length= table->s->reclength - table->s->null_bytes;
3522 if (arg_count_order)
3530 init_tree(tree, min<uint>(thd->variables.max_heap_table_size,
3531 thd->variables.sortbuff_size/16), 0,
3548 void Item_func_group_concat::make_unique()
3553 force_copy_fields= 1;
3560 DBUG_ASSERT(fixed == 1);
3563 if (no_appended && tree)
3570 warning_for_row=
true;
3571 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
3572 ER_CUT_VALUE_GROUP_CONCAT, ER(ER_CUT_VALUE_GROUP_CONCAT),
3582 str->append(STRING_WITH_LEN(
"group_concat("));
3584 str->append(STRING_WITH_LEN(
"distinct "));
3585 for (uint i= 0; i < arg_count_field; i++)
3589 orig_args[
i]->
print(str, query_type);
3591 if (arg_count_order)
3593 str->append(STRING_WITH_LEN(
" order by "));
3594 for (uint i= 0 ; i < arg_count_order ; i++)
3598 orig_args[i + arg_count_field]->
print(str, query_type);
3599 if (order[i]->direction == ORDER::ORDER_ASC)
3600 str->append(STRING_WITH_LEN(
" ASC"));
3602 str->append(STRING_WITH_LEN(
" DESC"));
3605 str->append(STRING_WITH_LEN(
" separator \'"));
3606 str->append(*separator);
3607 str->append(STRING_WITH_LEN(
"\')"));
3611 Item_func_group_concat::~Item_func_group_concat()
3613 if (!original && unique_filter)
3614 delete unique_filter;