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>
53 extern plugin::StorageEngine *heap_engine;
77 bool Item_sum::init_sum_func_check(
Session *session)
79 if (!session->lex().allow_sum_func)
81 my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
86 in_sum_func= session->lex().in_sum_func;
88 session->lex().in_sum_func=
this;
89 nest_level= session->lex().current_select->nest_level;
94 max_sum_func_level= -1;
151 nesting_map allow_sum_func= session->lex().allow_sum_func;
159 if (nest_level == max_arg_level)
166 invalid= !(allow_sum_func & (1 << max_arg_level));
168 else if (max_arg_level >= 0 || !(allow_sum_func & (1 << nest_level)))
175 if (register_sum_func(session, ref))
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;
181 if (!invalid && aggr_level < 0)
183 aggr_level= nest_level;
184 aggr_sel= session->lex().current_select;
197 invalid= aggr_level <= max_sum_func_level;
200 my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
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);
230 if (outer_fields.size())
260 while ((field= of++))
262 Select_Lex *sel= field->cached_table->
select_lex;
263 if (sel->nest_level < aggr_level)
271 in_sum_func->outer_fields.push_back(field);
275 sel->full_group_by_flag.set(NON_AGG_FIELD_USED);
278 if (sel->nest_level > aggr_level &&
279 (sel->full_group_by_flag.test(SUM_FUNC_USED)) &&
280 ! sel->group_list.elements)
282 my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
283 ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
288 aggr_sel->full_group_by_flag.set(SUM_FUNC_USED);
289 update_used_tables();
290 session->lex().in_sum_func= in_sum_func;
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() )
327 if (aggr_level < 0 && (allow_sum_func & (1 << sl->nest_level)))
330 aggr_level= sl->nest_level;
334 if (sl && (allow_sum_func & (1 << sl->nest_level)))
341 aggr_level= sl->nest_level;
349 if (!aggr_sel->inner_sum_func_list)
353 next= aggr_sel->inner_sum_func_list->next;
354 aggr_sel->inner_sum_func_list->next=
this;
356 aggr_sel->inner_sum_func_list=
this;
357 aggr_sel->with_sum_func= 1;
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;
379 session->lex().current_select->mark_as_dependent(aggr_sel);
384 Item_sum::Item_sum(
List<Item> &list) :arg_count(list.size()),
387 if ((args=(
Item**) memory::sql_alloc(
sizeof(
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)
417 args=
new (session->mem)
Item*[arg_count];
418 memcpy(args, item->args,
sizeof(
Item*)*arg_count);
422 void Item_sum::mark_as_sum_func()
424 Select_Lex *cur_select= getSession().lex().current_select;
425 cur_select->n_sum_items++;
426 cur_select->with_sum_func= 1;
431 void Item_sum::make_field(SendField *tmp_field)
433 if (args[0]->type() == Item::FIELD_ITEM && keep_field_type())
435 ((Item_field*) args[0])->field->make_field(tmp_field);
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;
443 tmp_field->flags&= ~NOT_NULL_FLAG;
446 init_make_field(tmp_field, field_type());
452 str->append(func_name(), strlen(func_name()));
453 for (uint32_t i=0 ; i < arg_count ; i++)
464 void Item_sum::fix_num_length_and_dec()
467 for (uint32_t i=0 ; i < arg_count ; i++)
469 set_if_bigger(decimals,args[i]->decimals);
471 max_length= float_length(decimals);
474 Item *Item_sum::get_tmp_table_item(Session *session)
476 Item_sum* sum_item= (Item_sum *) copy_or_same(session);
477 if (sum_item && sum_item->result_field)
479 Field *result_field_tmp= sum_item->result_field;
480 for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
482 Item *arg= sum_item->args[i];
483 if (!arg->const_item())
485 if (arg->type() == Item::FIELD_ITEM)
486 ((Item_field*) arg)->field= result_field_tmp++;
488 sum_item->args[i]=
new Item_field(result_field_tmp++);
496 bool Item_sum::walk (Item_processor processor,
bool walk_subquery,
497 unsigned char *argument)
501 Item **arg,**arg_end;
502 for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
504 if ((*arg)->walk(processor, walk_subquery, argument))
508 return (this->*processor)(argument);
512 Field *Item_sum::create_tmp_field(
bool ,
514 uint32_t convert_blob_length)
518 switch (result_type()) {
520 field=
new Field_double(max_length,
maybe_null,
name, decimals,
true);
524 field=
new field::Int64(max_length,
maybe_null,
name, unsigned_flag);
528 if (max_length/collation.collation->mbmaxlen <= 255 ||
529 convert_blob_length > Field_varstring::MAX_SIZE ||
530 !convert_blob_length)
535 table->setVariableWidth();
536 field=
new Field_varstring(convert_blob_length,
maybe_null,
537 name, collation.collation);
542 decimals, unsigned_flag);
558 void Item_sum::update_used_tables ()
562 used_tables_cache= 0;
563 for (uint32_t i=0 ; i < arg_count ; i++)
565 args[i]->update_used_tables();
569 used_tables_cache&= PSEUDO_TABLE_BITS;
572 used_tables_cache |= (1 << aggr_sel->join->tables) - 1;
580 return val_string_from_real(str);
593 return val_decimal_from_real(decimal_value);
600 return val_string_from_int(str);
606 return val_decimal_from_int(decimal_value);
611 Item_sum_num::fix_fields(
Session *session,
Item **ref)
620 for (uint32_t i=0 ; i < arg_count ; i++)
622 if (args[i]->fix_fields(session, args + i) || args[i]->check_cols(1))
624 set_if_bigger(decimals, args[i]->decimals);
628 max_length=float_length(decimals);
630 fix_length_and_dec();
640 Item_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)
646 switch (hybrid_type) {
648 sum_int= item->sum_int;
651 class_decimal2decimal(&item->sum_dec, &sum_dec);
665 collation.set(item->collation);
669 Item_sum_hybrid::fix_fields(Session *session, Item **ref)
679 if ((!item->fixed && item->fix_fields(session, args)) ||
680 (item= args[0])->check_cols(1))
682 decimals=item->decimals;
684 switch (hybrid_type= item->result_type()) {
690 max_length= item->max_length;
694 max_length= float_length(decimals);
698 max_length= item->max_length;
705 unsigned_flag=item->unsigned_flag;
706 collation.set(item->collation);
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();
714 hybrid_field_type= Item::field_type();
723 Field *Item_sum_hybrid::create_tmp_field(
bool group, Table *table,
724 uint32_t convert_blob_length)
727 if (args[0]->type() == Item::FIELD_ITEM)
729 field= ((Item_field*) args[0])->field;
732 NULL, convert_blob_length)))
733 field->flags&= ~NOT_NULL_FLAG;
741 switch (args[0]->field_type()) {
742 case DRIZZLE_TYPE_DATE:
745 case DRIZZLE_TYPE_TIMESTAMP:
746 case DRIZZLE_TYPE_DATETIME:
750 return Item_sum::create_tmp_field(group, table, convert_blob_length);
769 :
Item_sum_num(session, item), hybrid_type(item->hybrid_type),
770 curr_dec_buff(item->curr_dec_buff)
773 if (hybrid_type == DECIMAL_RESULT)
775 class_decimal2decimal(item->dec_buffs, dec_buffs);
776 class_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
788 void Item_sum_sum::clear()
791 if (hybrid_type == DECIMAL_RESULT)
794 dec_buffs->set_zero();
802 void Item_sum_sum::fix_length_and_dec()
805 decimals= args[0]->decimals;
806 switch (args[0]->result_type()) {
809 hybrid_type= REAL_RESULT;
816 int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
817 max_length= class_decimal_precision_to_length(precision, decimals,
820 hybrid_type= DECIMAL_RESULT;
821 dec_buffs->set_zero();
830 bool Item_sum_sum::add()
832 if (hybrid_type == DECIMAL_RESULT)
834 type::Decimal value, *val= args[0]->
val_decimal(&value);
837 class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
838 val, dec_buffs + curr_dec_buff);
856 if (hybrid_type == DECIMAL_RESULT)
859 (dec_buffs + curr_dec_buff)->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
869 if (hybrid_type == DECIMAL_RESULT)
870 class_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
877 if (hybrid_type == DECIMAL_RESULT)
878 return val_string_from_decimal(str);
879 return val_string_from_real(str);
885 if (hybrid_type == DECIMAL_RESULT)
886 return (dec_buffs + curr_dec_buff);
887 return val_decimal_from_real(val);
894 static int simple_raw_key_cmp(
void* arg,
const void* key1,
const void* key2)
896 return memcmp(key1, key2, *(uint32_t *) arg);
900 static int item_sum_distinct_walk(
void *element,
904 return ((Item_sum_distinct*) (item))->unique_walk_function(element);
909 Item_sum_distinct::Item_sum_distinct(Item *item_arg)
910 :Item_sum_num(item_arg), tree(0)
922 Item_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)
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); }
944 virtual void div(
Hybrid_type *val, uint64_t u)
const
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);
958 *Hybrid_type_traits_fast_decimal::instance()
960 return &fast_decimal_traits_instance;
964 void Item_sum_distinct::fix_length_and_dec()
966 assert(args[0]->
fixed);
969 table_field_type= args[0]->field_type();
972 switch (args[0]->result_type()) {
975 val.traits= Hybrid_type_traits::instance();
976 table_field_type= DRIZZLE_TYPE_DOUBLE;
986 if (table_field_type == DRIZZLE_TYPE_LONG)
988 val.traits= Hybrid_type_traits_fast_decimal::instance();
991 table_field_type= DRIZZLE_TYPE_LONGLONG;
994 val.traits= Hybrid_type_traits_decimal::instance();
995 if (table_field_type != DRIZZLE_TYPE_LONGLONG)
996 table_field_type= DRIZZLE_TYPE_DECIMAL;
1002 val.traits->fix_length_and_dec(
this, args[0]);
1006 enum Item_result Item_sum_distinct::result_type ()
const
1008 return val.traits->type();
1030 assert(args[0]->
fixed);
1032 std::list<CreateField> field_list;
1036 table= &session->getInstanceTable(field_list);
1039 tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
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;
1052 bool Item_sum_distinct::add()
1054 args[0]->save_in_field(table->getField(0),
false);
1055 is_evaluated=
false;
1056 if (!table->getField(0)->is_null())
1064 return tree->unique_add(table->getField(0)->ptr);
1070 bool Item_sum_distinct::unique_walk_function(
void *element)
1072 memcpy(table->getField(0)->ptr, element, tree_key_length);
1074 val.traits->add(&val, table->getField(0));
1079 void Item_sum_distinct::clear()
1084 is_evaluated=
false;
1088 void Item_sum_distinct::cleanup()
1090 Item_sum_num::cleanup();
1094 is_evaluated=
false;
1097 Item_sum_distinct::~Item_sum_distinct()
1104 void Item_sum_distinct::calculate_val_and_count()
1109 val.traits->set_zero(&val);
1116 table->getField(0)->set_notnull();
1117 tree->walk(item_sum_distinct_walk, (
void*)
this);
1126 calculate_val_and_count();
1127 return val.traits->val_real(&val);
1133 calculate_val_and_count();
1136 return val.traits->val_decimal(&val, to);
1142 calculate_val_and_count();
1143 return val.traits->val_int(&val, unsigned_flag);
1149 calculate_val_and_count();
1152 return val.traits->val_str(&val, str, decimals);
1160 Item_sum_avg_distinct::fix_length_and_dec()
1162 Item_sum_distinct::fix_length_and_dec();
1163 prec_increment= getSession().
variables.div_precincrement;
1168 decimals= min(decimals + prec_increment, (
unsigned int)NOT_FIXED_DEC);
1173 Item_sum_avg_distinct::calculate_val_and_count()
1177 Item_sum_distinct::calculate_val_and_count();
1179 val.traits->div(&val, count);
1185 Item *Item_sum_count::copy_or_same(Session* session)
1187 return new (session->mem) Item_sum_count(session,
this);
1191 void Item_sum_count::clear()
1197 bool Item_sum_count::add()
1207 return (int64_t) count;
1211 void Item_sum_count::cleanup()
1214 Item_sum_int::cleanup();
1222 void Item_sum_avg::fix_length_and_dec()
1224 Item_sum_sum::fix_length_and_dec();
1226 prec_increment= getSession().
variables.div_precincrement;
1228 if (hybrid_type == DECIMAL_RESULT)
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,
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);
1239 decimals= min(args[0]->decimals + prec_increment, (
unsigned int) NOT_FIXED_DEC);
1240 max_length= args[0]->max_length + prec_increment;
1245 Item *Item_sum_avg::copy_or_same(Session* session)
1247 return new (session->mem) Item_sum_avg(session,
this);
1251 Field *Item_sum_avg::create_tmp_field(
bool group, Table *table,
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);
1267 else if (hybrid_type == DECIMAL_RESULT)
1269 decimals, unsigned_flag);
1271 field=
new Field_double(max_length,
maybe_null,
name, decimals,
true);
1278 void Item_sum_avg::clear()
1280 Item_sum_sum::clear();
1285 bool Item_sum_avg::add()
1287 if (Item_sum_sum::add())
1327 if (hybrid_type != DECIMAL_RESULT)
1328 return val_decimal_from_real(val);
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);
1339 if (hybrid_type == DECIMAL_RESULT)
1340 return val_string_from_decimal(str);
1341 return val_string_from_real(str);
1357 Item *Item_sum_std::copy_or_same(
Session* session)
1391 double m_kminusone= *m;
1392 *m= m_kminusone + (nr - m_kminusone) / (
double) *count;
1393 *s= *s + (nr - m_kminusone) * (nr - *m);
1398 static double variance_fp_recurrence_result(
double s, uint64_t count,
bool is_sample_variance)
1403 if (is_sample_variance)
1404 return s / (count - 1);
1411 Item_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)
1416 recurrence_m= item->recurrence_m;
1417 recurrence_s= item->recurrence_s;
1421 void Item_sum_variance::fix_length_and_dec()
1423 maybe_null= null_value= 1;
1424 prec_increment= getSession().variables.div_precincrement;
1432 hybrid_type= REAL_RESULT;
1434 switch (args[0]->result_type()) {
1437 decimals= min(args[0]->decimals + 4, (
int)NOT_FIXED_DEC);
1440 case DECIMAL_RESULT:
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,
1455 Item *Item_sum_variance::copy_or_same(Session* session)
1457 return new (session->mem) Item_sum_variance(session,
this);
1466 Field *Item_sum_variance::create_tmp_field(
bool group,
Table *table,
1477 table->setVariableWidth();
1478 field=
new Field_varstring(
sizeof(
double)*2 +
sizeof(int64_t), 0, name, &my_charset_bin);
1481 field=
new Field_double(max_length, maybe_null, name, decimals,
true);
1490 void Item_sum_variance::clear()
1495 bool Item_sum_variance::add()
1501 double nr= args[0]->val_real();
1503 if (!args[0]->null_value)
1508 double Item_sum_variance::val_real()
1521 assert((sample == 0) || (sample == 1));
1522 if (count <= sample)
1529 return variance_fp_recurrence_result(recurrence_s, count, sample);
1533 int64_t Item_sum_variance::val_int()
1536 return (int64_t) rint(val_real());
1543 return val_decimal_from_real(dec_buf);
1547 void Item_sum_variance::reset_field()
1550 unsigned char *res= result_field->ptr;
1552 nr= args[0]->val_real();
1554 if (args[0]->null_value)
1555 memset(res, 0,
sizeof(
double)*2+
sizeof(int64_t));
1561 float8store(res, nr);
1563 float8store(res +
sizeof(
double), tmp_s);
1565 int8store(res +
sizeof(
double)*2, tmp_count);
1570 void Item_sum_variance::update_field()
1572 uint64_t field_count;
1573 unsigned char *res=result_field->ptr;
1575 double nr= args[0]->val_real();
1577 if (args[0]->null_value)
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);
1588 float8store(res, field_recurrence_m);
1589 float8store(res +
sizeof(
double), field_recurrence_s);
1590 res+=
sizeof(double)*2;
1591 int8store(res,field_count);
1597 void Item_sum_hybrid::clear()
1599 switch (hybrid_type) {
1603 case DECIMAL_RESULT:
1615 double Item_sum_hybrid::val_real()
1621 switch (hybrid_type) {
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);
1631 return (
double) sum_int;
1632 case DECIMAL_RESULT:
1633 class_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1646 int64_t Item_sum_hybrid::val_int()
1651 switch (hybrid_type) {
1654 case DECIMAL_RESULT:
1657 sum_dec.val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
1661 return (int64_t) rint(Item_sum_hybrid::val_real());
1672 switch (hybrid_type) {
1674 val->
store(E_DEC_FATAL_ERROR, &value);
1677 double2_class_decimal(E_DEC_FATAL_ERROR, sum, val);
1679 case DECIMAL_RESULT:
1683 int2_class_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1702 switch (hybrid_type) {
1706 str->set_real(sum,decimals, &my_charset_bin);
1708 case DECIMAL_RESULT:
1712 str->set_int(sum_int, unsigned_flag, &my_charset_bin);
1724 void Item_sum_hybrid::cleanup()
1726 Item_sum::cleanup();
1727 forced_const=
false;
1740 void Item_sum_hybrid::no_rows_in_result()
1747 Item *Item_sum_min::copy_or_same(
Session* session)
1753 bool Item_sum_min::add()
1755 switch (hybrid_type) {
1758 String *result=args[0]->val_str(&tmp_value);
1759 if (!args[0]->null_value &&
1760 (null_value || sortcmp(&value,result,collation.collation) > 0))
1762 value.copy(*result);
1769 int64_t nr=args[0]->val_int();
1770 if (!args[0]->null_value && (null_value ||
1772 (uint64_t) nr < (uint64_t) sum_int) ||
1773 (!unsigned_flag && nr < sum_int)))
1780 case DECIMAL_RESULT:
1782 type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1783 if (!args[0]->null_value &&
1786 class_decimal2decimal(val, &sum_dec);
1793 double nr= args[0]->val_real();
1794 if (!args[0]->null_value && (null_value || nr < sum))
1810 Item *Item_sum_max::copy_or_same(Session* session)
1812 return new (session->mem) Item_sum_max(session,
this);
1816 bool Item_sum_max::add()
1818 switch (hybrid_type) {
1821 String *result=args[0]->val_str(&tmp_value);
1822 if (!args[0]->null_value &&
1823 (null_value || sortcmp(&value,result,collation.collation) < 0))
1825 value.copy(*result);
1832 int64_t nr=args[0]->val_int();
1833 if (!args[0]->null_value && (null_value ||
1835 (uint64_t) nr > (uint64_t) sum_int) ||
1836 (!unsigned_flag && nr > sum_int)))
1843 case DECIMAL_RESULT:
1845 type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1846 if (!args[0]->null_value &&
1849 class_decimal2decimal(val, &sum_dec);
1856 double nr= args[0]->val_real();
1857 if (!args[0]->null_value && (null_value || nr > sum))
1876 int64_t Item_sum_bit::val_int()
1879 return (int64_t) bits;
1883 void Item_sum_bit::clear()
1888 Item *Item_sum_or::copy_or_same(Session* session)
1890 return new (session->mem) Item_sum_or(session,
this);
1894 bool Item_sum_or::add()
1896 uint64_t value= (uint64_t) args[0]->val_int();
1897 if (!args[0]->null_value)
1902 Item *Item_sum_xor::copy_or_same(Session* session)
1904 return new (session->mem) Item_sum_xor(session,
this);
1908 bool Item_sum_xor::add()
1910 uint64_t value= (uint64_t) args[0]->val_int();
1911 if (!args[0]->null_value)
1916 Item *Item_sum_and::copy_or_same(Session* session)
1918 return new (session->mem) Item_sum_and(session,
this);
1922 bool Item_sum_and::add()
1924 uint64_t value= (uint64_t) args[0]->val_int();
1925 if (!args[0]->null_value)
1934 void Item_sum_num::reset_field()
1936 double nr= args[0]->val_real();
1937 unsigned char *res=result_field->ptr;
1941 if (args[0]->null_value)
1944 result_field->set_null();
1947 result_field->set_notnull();
1949 float8store(res,nr);
1953 void Item_sum_hybrid::reset_field()
1955 switch(hybrid_type) {
1958 char buff[MAX_FIELD_WIDTH];
1959 String tmp(buff,
sizeof(buff),result_field->charset()),*res;
1961 res=args[0]->val_str(&tmp);
1962 if (args[0]->null_value)
1964 result_field->set_null();
1965 result_field->reset();
1969 result_field->set_notnull();
1970 result_field->store(res->ptr(),res->length(),tmp.charset());
1976 int64_t nr=args[0]->val_int();
1980 if (args[0]->null_value)
1983 result_field->set_null();
1986 result_field->set_notnull();
1988 result_field->store(nr, unsigned_flag);
1993 double nr= args[0]->val_real();
1997 if (args[0]->null_value)
2000 result_field->set_null();
2003 result_field->set_notnull();
2005 result_field->store(nr);
2008 case DECIMAL_RESULT:
2010 type::Decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2014 if (args[0]->null_value)
2015 result_field->set_null();
2017 result_field->set_notnull();
2024 arg_dec= &decimal_zero;
2025 result_field->store_decimal(arg_dec);
2034 void Item_sum_sum::reset_field()
2036 if (hybrid_type == DECIMAL_RESULT)
2038 type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2040 arg_val= &decimal_zero;
2041 result_field->store_decimal(arg_val);
2045 assert(hybrid_type == REAL_RESULT);
2046 double nr= args[0]->val_real();
2047 float8store(result_field->ptr, nr);
2049 if (args[0]->null_value)
2050 result_field->set_null();
2052 result_field->set_notnull();
2056 void Item_sum_count::reset_field()
2058 unsigned char *res=result_field->ptr;
2061 if (!args[0]->maybe_null || !args[0]->is_null())
2067 void Item_sum_avg::reset_field()
2069 unsigned char *res=result_field->ptr;
2070 if (hybrid_type == DECIMAL_RESULT)
2073 type::Decimal value, *arg_dec= args[0]->val_decimal(&value);
2074 if (args[0]->null_value)
2076 arg_dec= &decimal_zero;
2081 arg_dec->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
2083 int8store(res, tmp);
2087 double nr= args[0]->val_real();
2089 if (args[0]->null_value)
2090 memset(res, 0,
sizeof(
double)+
sizeof(int64_t));
2094 float8store(res,nr);
2095 res+=
sizeof(double);
2102 void Item_sum_bit::reset_field()
2105 int8store(result_field->ptr, bits);
2108 void Item_sum_bit::update_field()
2110 unsigned char *res=result_field->ptr;
2111 bits= uint8korr(res);
2113 int8store(res, bits);
2121 void Item_sum_sum::update_field()
2123 if (hybrid_type == DECIMAL_RESULT)
2125 type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2126 if (!args[0]->null_value)
2128 if (!result_field->is_null())
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);
2137 result_field->store_decimal(arg_val);
2138 result_field->set_notnull();
2145 unsigned char *res=result_field->ptr;
2147 float8get(old_nr,res);
2148 nr= args[0]->val_real();
2149 if (!args[0]->null_value)
2152 result_field->set_notnull();
2154 float8store(res,old_nr);
2159 void Item_sum_count::update_field()
2162 unsigned char *res=result_field->ptr;
2165 if (!args[0]->maybe_null || !args[0]->is_null())
2171 void Item_sum_avg::update_field()
2173 int64_t field_count;
2174 unsigned char *res=result_field->ptr;
2175 if (hybrid_type == DECIMAL_RESULT)
2177 type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2178 if (!args[0]->null_value)
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);
2187 int8store(res, field_count);
2194 nr= args[0]->val_real();
2195 if (!args[0]->null_value)
2198 float8get(old_nr, res);
2199 field_count= sint8korr(res +
sizeof(
double));
2201 float8store(res,old_nr);
2202 res+=
sizeof(double);
2204 int8store(res, field_count);
2210 void Item_sum_hybrid::update_field()
2212 switch (hybrid_type) {
2214 min_max_update_str_field();
2217 min_max_update_int_field();
2219 case DECIMAL_RESULT:
2220 min_max_update_decimal_field();
2224 min_max_update_real_field();
2230 Item_sum_hybrid::min_max_update_str_field()
2232 String *res_str=args[0]->val_str(&value);
2234 if (!args[0]->null_value)
2236 result_field->val_str_internal(&tmp_value);
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();
2247 Item_sum_hybrid::min_max_update_real_field()
2251 old_nr=result_field->val_real();
2252 nr= args[0]->val_real();
2253 if (!args[0]->null_value)
2255 if (result_field->is_null(0) ||
2256 (cmp_sign > 0 ? old_nr > nr : old_nr < nr))
2258 result_field->set_notnull();
2260 else if (result_field->is_null(0))
2261 result_field->set_null();
2262 result_field->store(old_nr);
2267 Item_sum_hybrid::min_max_update_int_field()
2271 old_nr=result_field->val_int();
2272 nr=args[0]->val_int();
2273 if (!args[0]->null_value)
2275 if (result_field->is_null(0))
2279 bool res=(unsigned_flag ?
2280 (uint64_t) old_nr > (uint64_t) nr :
2283 if ((cmp_sign > 0) ^ (!res))
2286 result_field->set_notnull();
2288 else if (result_field->is_null(0))
2289 result_field->set_null();
2290 result_field->store(old_nr, unsigned_flag);
2303 const type::Decimal *old_nr= result_field->val_decimal(&old_val);
2305 if (!args[0]->null_value)
2307 if (result_field->is_null(0))
2313 if ((cmp_sign > 0) ^ (!res))
2316 result_field->set_notnull();
2318 else if (result_field->is_null(0))
2319 result_field->set_null();
2320 result_field->store_decimal(old_nr);
2324 Item_avg_field::Item_avg_field(Item_result res_type,
Item_sum_avg *item)
2327 decimals=item->decimals;
2328 max_length= item->max_length;
2329 unsigned_flag= item->unsigned_flag;
2330 field=item->result_field;
2332 hybrid_type= res_type;
2333 prec_increment= item->prec_increment;
2334 if (hybrid_type == DECIMAL_RESULT)
2336 f_scale= item->f_scale;
2337 f_precision= item->f_precision;
2338 dec_bin_size= item->dec_bin_size;
2342 double Item_avg_field::val_real()
2349 if (hybrid_type == DECIMAL_RESULT)
2350 return val_real_from_decimal();
2352 float8get(nr,field->ptr);
2353 res= (field->ptr+
sizeof(double));
2354 count= sint8korr(res);
2356 if ((null_value= !count))
2358 return nr/(double) count;
2362 int64_t Item_avg_field::val_int()
2364 return (int64_t) rint(val_real());
2371 if (hybrid_type == REAL_RESULT)
2372 return val_decimal_from_real(dec_buf);
2374 int64_t count= sint8korr(field->ptr + dec_bin_size);
2375 if ((null_value= !count))
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);
2391 if (hybrid_type == DECIMAL_RESULT)
2392 return val_string_from_decimal(str);
2393 return val_string_from_real(str);
2421 if (hybrid_type == REAL_RESULT)
2422 return val_decimal_from_real(dec_buf);
2427 class_decimal2double(E_DEC_FATAL_ERROR, dec, &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);
2439 decimals=item->decimals;
2440 max_length=item->max_length;
2441 unsigned_flag= item->unsigned_flag;
2442 field=item->result_field;
2444 sample= item->sample;
2445 prec_increment= item->prec_increment;
2446 if ((hybrid_type= item->hybrid_type) == DECIMAL_RESULT)
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;
2468 if (hybrid_type == DECIMAL_RESULT)
2469 return val_real_from_decimal();
2471 double recurrence_s;
2473 float8get(recurrence_s, (field->ptr +
sizeof(
double)));
2474 count=sint8korr(field->ptr+
sizeof(
double)*2);
2479 return variance_fp_recurrence_result(recurrence_s, count, sample);
2487 int simple_str_key_cmp(
void* arg,
unsigned char* key1,
unsigned char* key2)
2490 return f->cmp(key1, key2);
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)
2509 int len = *lengths++;
2510 int res = f->cmp(key1, key2);
2519 static int count_distinct_walk(
void *,
2523 (*((uint64_t*)arg))++;
2527 void Item_sum_count_distinct::cleanup()
2529 Item_sum_int::cleanup();
2541 is_evaluated=
false;
2546 delete tmp_table_param;
2563 force_copy_fields= 1;
2565 is_evaluated=
false;
2571 Item_sum_count_distinct::~Item_sum_count_distinct()
2577 bool Item_sum_count_distinct::setup(Session *session)
2580 Select_Lex *select_lex= session->lex().current_select;
2587 if (tree || table || tmp_table_param)
2590 tmp_table_param=
new Tmp_Table_Param;
2593 for (uint32_t i= 0; i < arg_count; i++)
2596 list.push_back(item);
2597 if (item->const_item() && item->is_null())
2603 tmp_table_param->force_copy_fields= force_copy_fields;
2606 if (!(table=
create_tmp_table(session, tmp_table_param, list, NULL, 1, 0, (select_lex->options | session->options), HA_POS_ERROR,
"")))
2610 table->cursor->extra(HA_EXTRA_NO_ROWS);
2613 if (table->getShare()->db_type() == heap_engine)
2619 qsort_cmp2 compare_key;
2621 Field **field= table->getFields();
2622 Field **field_end= field + table->getShare()->sizeFields();
2623 bool all_binary=
true;
2625 for (tree_key_length= 0; field < field_end; ++field)
2628 enum enum_field_types f_type= f->type();
2629 tree_key_length+= f->pack_length();
2630 if (f_type == DRIZZLE_TYPE_VARCHAR)
2638 cmp_arg= (
void*) &tree_key_length;
2639 compare_key= (qsort_cmp2) simple_raw_key_cmp;
2643 if (table->getShare()->sizeFields() == 1)
2651 compare_key= (qsort_cmp2) simple_str_key_cmp;
2652 cmp_arg= (
void*) table->getField(0);
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)
2664 *length= (*field)->pack_length();
2665 tree_key_length+= *length;
2670 tree=
new Unique(compare_key, cmp_arg, tree_key_length,
2671 (
size_t)session->variables.max_heap_table_size);
2678 is_evaluated=
false;
2686 Item *Item_sum_count_distinct::copy_or_same(Session* session)
2688 return new (session->mem) Item_sum_count_distinct(session,
this);
2692 void Item_sum_count_distinct::clear()
2695 is_evaluated=
false;
2702 table->cursor->extra(HA_EXTRA_NO_CACHE);
2703 table->cursor->ha_delete_all_rows();
2704 table->cursor->extra(HA_EXTRA_WRITE_CACHE);
2708 bool Item_sum_count_distinct::add()
2714 if (
copy_funcs(tmp_table_param->items_to_copy, table->in_use))
2717 for (Field **field= table->getFields() ; *field ; field++)
2719 if ((*field)->is_real_null(0))
2725 is_evaluated=
false;
2734 return tree->unique_add(table->record[0] + table->getShare()->null_bytes);
2736 if ((error= table->cursor->insertRecord(table->record[0])) &&
2737 table->cursor->is_fatal_error(error, HA_CHECK_DUP))
2754 if (tree->elements == 0)
2755 return (int64_t) tree->elements_in_tree();
2757 tree->walk(count_distinct_walk, (
void*) &count);
2759 return (int64_t) count;
2762 error= table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2766 table->print_error(error, MYF(0));
2769 return table->cursor->stats.records;
2805 Table *table= item_func->table;
2809 Item *item= item_func->args[i];
2821 Field *field= item->get_tmp_table_field();
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)))
2839 Order **order_item, **end;
2840 Table *table= grp_item->table;
2842 for (order_item= grp_item->order, end=order_item+ grp_item->
arg_count_order;
2846 Item *item= *(*order_item)->item;
2852 Field *field= item->get_tmp_table_field();
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;
2882 Table *table= item->table;
2883 String tmp((
char *)table->getUpdateRecord(), table->getShare()->getRecordLength(), default_charset_info);
2884 String *result= &item->result;
2886 uint32_t old_length= result->length();
2888 if (item->no_appended)
2889 item->no_appended=
false;
2891 result->append(*item->separator);
2895 for (; arg < arg_end; arg++)
2898 if (! (*arg)->const_item())
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);
2914 res= (*arg)->val_str(&tmp);
2916 result->append(*res);
2920 if (result->length() > item->max_length)
2922 int well_formed_error;
2924 const char *ptr= result->ptr();
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;
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)
2975 if (!(args= (
Item**) memory::sql_alloc(
sizeof(
Item*) * arg_count +
2976 sizeof(
Order*)*arg_count_order)))
2979 order= (
Order**)(args + arg_count);
2984 for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
2985 *arg_ptr= item_select;
2987 if (arg_count_order)
2989 Order **order_ptr= order;
2990 for (
Order *order_item= (
Order*) order_list->first;
2992 order_item= order_item->next)
2994 (*order_ptr++)= order_item;
2995 *arg_ptr= *order_item->item;
2996 order_item->item= arg_ptr++;
3005 tmp_table_param(item->tmp_table_param),
3006 warning(item->warning),
3007 separator(item->separator),
3009 unique_filter(item->unique_filter),
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),
3022 quick_group= item->quick_group;
3023 result.set_charset(collation.collation);
3028 void Item_func_group_concat::cleanup()
3030 Item_sum::cleanup();
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);
3047 delete tmp_table_param;
3051 Session *session= table->in_use;
3055 tree->delete_tree();
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);
3070 assert(tree == 0 && warning == 0);
3076 Item *Item_func_group_concat::copy_or_same(Session* session)
3082 void Item_func_group_concat::clear()
3087 warning_for_row=
false;
3097 bool Item_func_group_concat::add()
3102 if (
copy_funcs(tmp_table_param->items_to_copy, table->in_use))
3107 Item *show_item= args[i];
3108 if (!show_item->const_item())
3110 Field *f= show_item->get_tmp_table_field();
3111 if (f->is_null_in_record((
const unsigned char*) table->record[0]))
3117 bool row_eligible=
true;
3123 unique_filter->unique_add(table->record[0] + table->getShare()->null_bytes);
3125 row_eligible=
false;
3128 Tree_Element *el= 0;
3129 if (row_eligible && tree)
3130 el= tree->tree_insert(table->record[0] + table->getShare()->null_bytes, 0,
3131 tree->getCustomArg());
3137 if (row_eligible && !warning_for_row &&
3139 dump_leaf_key(table->record[0] + table->getShare()->null_bytes, 1,
this);
3146 Item_func_group_concat::fix_fields(Session *session, Item **ref)
3160 for (i=0 ; i < arg_count ; i++)
3162 if ((!args[i]->
fixed &&
3163 args[i]->fix_fields(session, args + i)) ||
3164 args[i]->check_cols(1))
3168 if (agg_item_charsets(collation, func_name(),
3172 MY_COLL_ALLOW_CONV, 1))
3175 result.set_charset(collation.collation);
3178 max_length= (size_t)session->variables.group_concat_max_len;
3188 bool Item_func_group_concat::setup(Session *session)
3191 Select_Lex *select_lex= session->lex().current_select;
3200 tmp_table_param=
new Tmp_Table_Param;
3203 tmp_table_param->convert_blob_length= max_length *
3204 collation.collation->mbmaxlen;
3209 Item *item= args[i];
3210 list.push_back(item);
3211 if (item->const_item())
3213 if (item->is_null())
3221 List<Item> all_fields(list);
3233 tmp_table_param->force_copy_fields= force_copy_fields;
3243 set_if_smaller(tmp_table_param->convert_blob_length,
3244 Field_varstring::MAX_SIZE);
3255 (Order*) 0, 0,
true,
3256 (select_lex->options | session->options),
3257 HA_POS_ERROR, (
char*)
"")))
3262 table->cursor->extra(HA_EXTRA_NO_ROWS);
3270 uint32_t tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
3280 tree->
init_tree((uint32_t) min(session->variables.max_heap_table_size,
3281 (uint64_t)(session->variables.sortbuff_size/16)),
3291 (
size_t)session->variables.max_heap_table_size);
3299 void Item_func_group_concat::make_unique()
3304 force_copy_fields= 1;
3311 return res ? internal::my_atof(res->c_ptr()) : 0.0;
3321 end_ptr= (
char*) res->ptr()+ res->length();
3322 return internal::my_strtoll10(res->ptr(), &end_ptr, &error);
3330 if (no_appended && tree)
3332 tree->tree_walk((tree_walk_action)&
dump_leaf_key, (
void*)
this, left_root_right);
3333 if (count_cut_values && !warning)
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));
3350 str->append(STRING_WITH_LEN(
"group_concat("));
3352 str->append(STRING_WITH_LEN(
"distinct "));
3357 args[i]->
print(str);
3361 str->append(STRING_WITH_LEN(
" order by "));
3366 (*order[i]->item)->
print(str);
3368 str->append(STRING_WITH_LEN(
" ASC"));
3370 str->append(STRING_WITH_LEN(
" DESC"));
3373 str->append(STRING_WITH_LEN(
" separator \'"));
3374 str->append(*separator);
3375 str->append(STRING_WITH_LEN(
"\')"));
3379 Item_func_group_concat::~Item_func_group_concat()