33
33
#include <drizzled/item/sum.h>
34
34
#include <drizzled/field/decimal.h>
35
35
#include <drizzled/field/double.h>
36
#include <drizzled/field/int64.h>
36
#include <drizzled/field/int64_t.h>
37
37
#include <drizzled/field/date.h>
38
38
#include <drizzled/field/datetime.h>
509
509
uint32_t convert_blob_length)
513
512
switch (result_type()) {
514
513
case REAL_RESULT:
515
514
field= new Field_double(max_length, maybe_null, name, decimals, true);
519
field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
517
field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
522
519
case STRING_RESULT:
523
520
if (max_length/collation.collation->mbmaxlen <= 255 ||
524
521
convert_blob_length > Field_varstring::MAX_SIZE ||
525
522
!convert_blob_length)
527
523
return make_string_field(table);
530
525
table->setVariableWidth();
531
526
field= new Field_varstring(convert_blob_length, maybe_null,
532
527
name, collation.collation);
535
529
case DECIMAL_RESULT:
536
530
field= new Field_decimal(max_length, maybe_null, name,
537
decimals, unsigned_flag);
531
decimals, unsigned_flag);
541
535
// This case should never be choosen
547
540
field->init(table);
806
800
case DECIMAL_RESULT:
808
/* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
809
int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
810
max_length= my_decimal_precision_to_length(precision, decimals,
813
hybrid_type= DECIMAL_RESULT;
814
my_decimal_set_zero(dec_buffs);
802
/* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
803
int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
804
max_length= my_decimal_precision_to_length(precision, decimals,
807
hybrid_type= DECIMAL_RESULT;
808
my_decimal_set_zero(dec_buffs);
970
966
table_field_type= DRIZZLE_TYPE_DOUBLE;
974
Preserving int8, int16, int32 field types gives ~10% performance boost
975
as the size of result tree becomes significantly smaller.
976
Another speed up we gain by using int64_t for intermediate
977
calculations. The range of int64 is enough to hold sum 2^32 distinct
978
integers each <= 2^32.
980
if (table_field_type == DRIZZLE_TYPE_LONG)
982
val.traits= Hybrid_type_traits_fast_decimal::instance();
985
table_field_type= DRIZZLE_TYPE_LONGLONG;
970
Preserving int8, int16, int32 field types gives ~10% performance boost
971
as the size of result tree becomes significantly smaller.
972
Another speed up we gain by using int64_t for intermediate
973
calculations. The range of int64 is enough to hold sum 2^32 distinct
974
integers each <= 2^32.
976
if (table_field_type == DRIZZLE_TYPE_LONG)
978
val.traits= Hybrid_type_traits_fast_decimal::instance();
981
table_field_type= DRIZZLE_TYPE_LONGLONG;
987
983
case DECIMAL_RESULT:
988
984
val.traits= Hybrid_type_traits_decimal::instance();
989
985
if (table_field_type != DRIZZLE_TYPE_LONGLONG)
990
986
table_field_type= DRIZZLE_TYPE_DECIMAL;
996
992
val.traits->fix_length_and_dec(this, args[0]);
1031
1027
field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
1032
1028
args[0]->decimals, args[0]->maybe_null);
1034
if (! (table= session->getInstanceTable(field_list)))
1030
if (! (table= session->create_virtual_tmp_table(field_list)))
1037
1033
/* XXX: check that the case of CHAR(0) works OK */
1441
1437
case INT_RESULT:
1442
1438
case DECIMAL_RESULT:
1444
int precision= args[0]->decimal_precision()*2 + prec_increment;
1445
decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1446
max_length= my_decimal_precision_to_length(precision, decimals,
1440
int precision= args[0]->decimal_precision()*2 + prec_increment;
1441
decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1442
max_length= my_decimal_precision_to_length(precision, decimals,
1451
1447
case ROW_RESULT:
1619
1617
assert(fixed == 1);
1620
1618
if (null_value)
1623
1620
switch (hybrid_type) {
1624
1621
case STRING_RESULT:
1628
String *res; res=val_str(&str_value);
1629
return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1630
&end_not_used, &err_not_used) : 0.0);
1625
String *res; res=val_str(&str_value);
1626
return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1627
&end_not_used, &err_not_used) : 0.0);
1632
1629
case INT_RESULT:
1633
1630
return (double) sum_int;
1634
1631
case DECIMAL_RESULT:
1757
1751
switch (hybrid_type) {
1758
1752
case STRING_RESULT:
1754
String *result=args[0]->val_str(&tmp_value);
1755
if (!args[0]->null_value &&
1756
(null_value || sortcmp(&value,result,collation.collation) > 0))
1760
String *result=args[0]->val_str(&tmp_value);
1761
if (!args[0]->null_value &&
1762
(null_value || sortcmp(&value,result,collation.collation) > 0))
1764
value.copy(*result);
1758
value.copy(*result);
1769
1763
case INT_RESULT:
1765
int64_t nr=args[0]->val_int();
1766
if (!args[0]->null_value && (null_value ||
1768
(uint64_t) nr < (uint64_t) sum_int) ||
1769
(!unsigned_flag && nr < sum_int)))
1771
int64_t nr=args[0]->val_int();
1772
if (!args[0]->null_value && (null_value ||
1774
(uint64_t) nr < (uint64_t) sum_int) ||
1775
(!unsigned_flag && nr < sum_int)))
1782
1776
case DECIMAL_RESULT:
1778
my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1779
if (!args[0]->null_value &&
1780
(null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
1784
my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1785
if (!args[0]->null_value &&
1786
(null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
1788
my_decimal2decimal(val, &sum_dec);
1782
my_decimal2decimal(val, &sum_dec);
1793
1787
case REAL_RESULT:
1789
double nr= args[0]->val_real();
1790
if (!args[0]->null_value && (null_value || nr < sum))
1795
double nr= args[0]->val_real();
1796
if (!args[0]->null_value && (null_value || nr < sum))
1803
1797
case ROW_RESULT:
1804
1799
// This case should never be choosen
1820
1815
switch (hybrid_type) {
1821
1816
case STRING_RESULT:
1818
String *result=args[0]->val_str(&tmp_value);
1819
if (!args[0]->null_value &&
1820
(null_value || sortcmp(&value,result,collation.collation) < 0))
1823
String *result=args[0]->val_str(&tmp_value);
1824
if (!args[0]->null_value &&
1825
(null_value || sortcmp(&value,result,collation.collation) < 0))
1827
value.copy(*result);
1822
value.copy(*result);
1832
1827
case INT_RESULT:
1829
int64_t nr=args[0]->val_int();
1830
if (!args[0]->null_value && (null_value ||
1832
(uint64_t) nr > (uint64_t) sum_int) ||
1833
(!unsigned_flag && nr > sum_int)))
1834
int64_t nr=args[0]->val_int();
1835
if (!args[0]->null_value && (null_value ||
1837
(uint64_t) nr > (uint64_t) sum_int) ||
1838
(!unsigned_flag && nr > sum_int)))
1845
1840
case DECIMAL_RESULT:
1842
my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1843
if (!args[0]->null_value &&
1844
(null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
1847
my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1848
if (!args[0]->null_value &&
1849
(null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
1851
my_decimal2decimal(val, &sum_dec);
1846
my_decimal2decimal(val, &sum_dec);
1856
1851
case REAL_RESULT:
1853
double nr= args[0]->val_real();
1854
if (!args[0]->null_value && (null_value || nr > sum))
1858
double nr= args[0]->val_real();
1859
if (!args[0]->null_value && (null_value || nr > sum))
1866
1861
case ROW_RESULT:
1867
1863
// This case should never be choosen
1957
1952
switch(hybrid_type) {
1958
1953
case STRING_RESULT:
1960
char buff[MAX_FIELD_WIDTH];
1961
String tmp(buff,sizeof(buff),result_field->charset()),*res;
1963
res=args[0]->val_str(&tmp);
1964
if (args[0]->null_value)
1955
char buff[MAX_FIELD_WIDTH];
1956
String tmp(buff,sizeof(buff),result_field->charset()),*res;
1958
res=args[0]->val_str(&tmp);
1959
if (args[0]->null_value)
1961
result_field->set_null();
1962
result_field->reset();
1966
result_field->set_notnull();
1967
result_field->store(res->ptr(),res->length(),tmp.charset());
1973
int64_t nr=args[0]->val_int();
1977
if (args[0]->null_value)
1980
result_field->set_null();
1983
result_field->set_notnull();
1985
result_field->store(nr, unsigned_flag);
1990
double nr= args[0]->val_real();
1994
if (args[0]->null_value)
1997
result_field->set_null();
2000
result_field->set_notnull();
2002
result_field->store(nr);
2005
case DECIMAL_RESULT:
2007
my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2011
if (args[0]->null_value)
1966
2012
result_field->set_null();
1967
result_field->reset();
1971
2014
result_field->set_notnull();
1972
result_field->store(res->ptr(),res->length(),tmp.charset());
1978
int64_t nr=args[0]->val_int();
1982
if (args[0]->null_value)
1985
result_field->set_null();
1988
result_field->set_notnull();
1990
result_field->store(nr, unsigned_flag);
1995
double nr= args[0]->val_real();
1999
if (args[0]->null_value)
2002
result_field->set_null();
2005
result_field->set_notnull();
2007
result_field->store(nr);
2010
case DECIMAL_RESULT:
2012
my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2016
if (args[0]->null_value)
2017
result_field->set_null();
2019
result_field->set_notnull();
2022
We must store zero in the field as we will use the field value in
2025
if (!arg_dec) // Null
2026
arg_dec= &decimal_zero;
2027
result_field->store_decimal(arg_dec);
2017
We must store zero in the field as we will use the field value in
2020
if (!arg_dec) // Null
2021
arg_dec= &decimal_zero;
2022
result_field->store_decimal(arg_dec);
2030
2025
case ROW_RESULT:
2237
2232
if (!args[0]->null_value)
2239
result_field->val_str_internal(&tmp_value);
2234
result_field->val_str(&tmp_value);
2241
2236
if (result_field->is_null() ||
2242
2237
(cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2608
2603
tmp_table_param->force_copy_fields= force_copy_fields;
2609
2604
assert(table == 0);
2611
if (!(table= create_tmp_table(session, tmp_table_param, list, (Order*) 0, 1,
2606
if (!(table= create_tmp_table(session, tmp_table_param, list, (order_st*) 0, 1,
2613
2608
(select_lex->options | session->options),
2614
2609
HA_POS_ERROR, (char*)"")))
2719
2714
if (always_null)
2721
2716
copy_fields(tmp_table_param);
2722
if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
2717
copy_funcs(tmp_table_param->items_to_copy);
2725
2719
for (Field **field= table->getFields() ; *field ; field++)
2844
2838
const void* key2)
2846
2840
Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2847
Order **order_item, **end;
2841
order_st **order_item, **end;
2848
2842
Table *table= grp_item->table;
2850
2844
for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2918
2912
uint32_t offset= (field->offset(field->getTable()->record[0]) -
2919
2913
table->getShare()->null_bytes);
2920
2914
assert(offset < table->getShare()->getRecordLength());
2921
res= field->val_str_internal(&tmp, key + offset);
2915
res= field->val_str(&tmp, key + offset);
2924
2918
res= (*arg)->val_str(&tmp);
2988
2982
order - arg_count_order
2990
2984
if (!(args= (Item**) memory::sql_alloc(sizeof(Item*) * arg_count +
2991
sizeof(Order*)*arg_count_order)))
2985
sizeof(order_st*)*arg_count_order)))
2994
order= (Order**)(args + arg_count);
2988
order= (order_st**)(args + arg_count);
2996
2990
/* fill args items of show and sort */
2997
2991
List_iterator_fast<Item> li(*select_list);
3002
2996
if (arg_count_order)
3004
Order **order_ptr= order;
3005
for (Order *order_item= (Order*) order_list->first;
2998
order_st **order_ptr= order;
2999
for (order_st *order_item= (order_st*) order_list->first;
3006
3000
order_item != NULL;
3007
3001
order_item= order_item->next)
3272
3265
if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3273
(Order*) 0, 0, true,
3266
(order_st*) 0, 0, true,
3274
3267
(select_lex->options | session->options),
3275
3268
HA_POS_ERROR, (char*) "")))