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_t.h>
36
#include <drizzled/field/int64.h>
37
37
#include <drizzled/field/date.h>
38
38
#include <drizzled/field/datetime.h>
509
509
uint32_t convert_blob_length)
512
513
switch (result_type()) {
513
514
case REAL_RESULT:
514
515
field= new Field_double(max_length, maybe_null, name, decimals, true);
517
field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
519
field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
519
522
case STRING_RESULT:
520
523
if (max_length/collation.collation->mbmaxlen <= 255 ||
521
524
convert_blob_length > Field_varstring::MAX_SIZE ||
522
525
!convert_blob_length)
523
527
return make_string_field(table);
525
530
table->setVariableWidth();
526
531
field= new Field_varstring(convert_blob_length, maybe_null,
527
532
name, collation.collation);
529
535
case DECIMAL_RESULT:
530
536
field= new Field_decimal(max_length, maybe_null, name,
531
decimals, unsigned_flag);
537
decimals, unsigned_flag);
535
541
// This case should never be choosen
540
547
field->init(table);
759
765
/* TODO: check if the following assignments are really needed */
760
766
if (hybrid_type == DECIMAL_RESULT)
762
my_decimal2decimal(item->dec_buffs, dec_buffs);
763
my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
768
class_decimal2decimal(item->dec_buffs, dec_buffs);
769
class_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
800
806
case DECIMAL_RESULT:
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);
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= class_decimal_precision_to_length(precision, decimals,
813
hybrid_type= DECIMAL_RESULT;
814
dec_buffs->set_zero();
821
825
if (hybrid_type == DECIMAL_RESULT)
823
my_decimal value, *val= args[0]->val_decimal(&value);
827
type::Decimal value, *val= args[0]->val_decimal(&value);
824
828
if (!args[0]->null_value)
826
my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
830
class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
827
831
val, dec_buffs + curr_dec_buff);
828
832
curr_dec_buff^= 1;
845
849
if (hybrid_type == DECIMAL_RESULT)
848
my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
852
(dec_buffs + curr_dec_buff)->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
852
855
return (int64_t) rint(val_real());
858
861
assert(fixed == 1);
859
862
if (hybrid_type == DECIMAL_RESULT)
860
my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
863
class_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
934
937
virtual void div(Hybrid_type *val, uint64_t u) const
936
int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
939
int2_class_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
937
940
val->used_dec_buf_no= 0;
938
941
val->traits= Hybrid_type_traits_decimal::instance();
939
942
val->traits->div(val, u);
966
969
table_field_type= DRIZZLE_TYPE_DOUBLE;
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;
973
Preserving int8, int16, int32 field types gives ~10% performance boost
974
as the size of result tree becomes significantly smaller.
975
Another speed up we gain by using int64_t for intermediate
976
calculations. The range of int64 is enough to hold sum 2^32 distinct
977
integers each <= 2^32.
979
if (table_field_type == DRIZZLE_TYPE_LONG)
981
val.traits= Hybrid_type_traits_fast_decimal::instance();
984
table_field_type= DRIZZLE_TYPE_LONGLONG;
983
986
case DECIMAL_RESULT:
984
987
val.traits= Hybrid_type_traits_decimal::instance();
985
988
if (table_field_type != DRIZZLE_TYPE_LONGLONG)
986
989
table_field_type= DRIZZLE_TYPE_DECIMAL;
992
995
val.traits->fix_length_and_dec(this, args[0]);
1228
1231
int precision= args[0]->decimal_precision() + prec_increment;
1229
1232
decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1230
max_length= my_decimal_precision_to_length(precision, decimals,
1233
max_length= class_decimal_precision_to_length(precision, decimals,
1231
1234
unsigned_flag);
1232
1235
f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1233
1236
f_scale= args[0]->decimals;
1234
dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1237
dec_bin_size= class_decimal_get_binary_size(f_precision, f_scale);
1237
1240
decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1310
my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
1313
type::Decimal *Item_sum_avg::val_decimal(type::Decimal *val)
1312
my_decimal sum_buff, cnt;
1313
const my_decimal *sum_dec;
1315
type::Decimal sum_buff, cnt;
1316
const type::Decimal *sum_dec;
1314
1317
assert(fixed == 1);
1326
1329
return val_decimal_from_real(val);
1328
1331
sum_dec= dec_buffs + curr_dec_buff;
1329
int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
1330
my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1332
int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
1333
class_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1437
1440
case INT_RESULT:
1438
1441
case DECIMAL_RESULT:
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,
1443
int precision= args[0]->decimal_precision()*2 + prec_increment;
1444
decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1445
max_length= class_decimal_precision_to_length(precision, decimals,
1447
1450
case ROW_RESULT:
1617
1618
assert(fixed == 1);
1618
1619
if (null_value)
1620
1622
switch (hybrid_type) {
1621
1623
case STRING_RESULT:
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);
1627
String *res; res=val_str(&str_value);
1628
return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1629
&end_not_used, &err_not_used) : 0.0);
1629
1631
case INT_RESULT:
1630
1632
return (double) sum_int;
1631
1633
case DECIMAL_RESULT:
1632
my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1634
class_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1634
1636
case REAL_RESULT:
1636
1638
case ROW_RESULT:
1638
1639
// This case should never be choosen
1644
1647
int64_t Item_sum_hybrid::val_int()
1664
my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
1667
type::Decimal *Item_sum_hybrid::val_decimal(type::Decimal *val)
1666
1669
assert(fixed == 1);
1667
1670
if (null_value)
1669
1673
switch (hybrid_type) {
1670
1674
case STRING_RESULT:
1671
string2my_decimal(E_DEC_FATAL_ERROR, &value, val);
1675
val->store(E_DEC_FATAL_ERROR, &value);
1673
1677
case REAL_RESULT:
1674
double2my_decimal(E_DEC_FATAL_ERROR, sum, val);
1678
double2_class_decimal(E_DEC_FATAL_ERROR, sum, val);
1676
1680
case DECIMAL_RESULT:
1679
1683
case INT_RESULT:
1680
int2my_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1684
int2_class_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1682
1686
case ROW_RESULT:
1684
1687
// This case should never be choosen
1688
1692
return val; // Keep compiler happy
1702
1707
str->set_real(sum,decimals, &my_charset_bin);
1704
1709
case DECIMAL_RESULT:
1705
my_decimal2string(E_DEC_FATAL_ERROR, &sum_dec, 0, 0, 0, str);
1710
class_decimal2string(E_DEC_FATAL_ERROR, &sum_dec, 0, 0, 0, str);
1707
1712
case INT_RESULT:
1708
1713
str->set_int(sum_int, unsigned_flag, &my_charset_bin);
1751
1756
switch (hybrid_type) {
1752
1757
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))
1758
value.copy(*result);
1759
String *result=args[0]->val_str(&tmp_value);
1760
if (!args[0]->null_value &&
1761
(null_value || sortcmp(&value,result,collation.collation) > 0))
1763
value.copy(*result);
1763
1768
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)))
1770
int64_t nr=args[0]->val_int();
1771
if (!args[0]->null_value && (null_value ||
1773
(uint64_t) nr < (uint64_t) sum_int) ||
1774
(!unsigned_flag && nr < sum_int)))
1776
1781
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)))
1782
my_decimal2decimal(val, &sum_dec);
1783
type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1784
if (!args[0]->null_value &&
1785
(null_value || (class_decimal_cmp(&sum_dec, val) > 0)))
1787
class_decimal2decimal(val, &sum_dec);
1787
1792
case REAL_RESULT:
1789
double nr= args[0]->val_real();
1790
if (!args[0]->null_value && (null_value || nr < sum))
1794
double nr= args[0]->val_real();
1795
if (!args[0]->null_value && (null_value || nr < sum))
1797
1802
case ROW_RESULT:
1799
1803
// This case should never be choosen
1815
1819
switch (hybrid_type) {
1816
1820
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))
1822
value.copy(*result);
1822
String *result=args[0]->val_str(&tmp_value);
1823
if (!args[0]->null_value &&
1824
(null_value || sortcmp(&value,result,collation.collation) < 0))
1826
value.copy(*result);
1827
1831
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)))
1833
int64_t nr=args[0]->val_int();
1834
if (!args[0]->null_value && (null_value ||
1836
(uint64_t) nr > (uint64_t) sum_int) ||
1837
(!unsigned_flag && nr > sum_int)))
1840
1844
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)))
1846
my_decimal2decimal(val, &sum_dec);
1846
type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1847
if (!args[0]->null_value &&
1848
(null_value || (class_decimal_cmp(val, &sum_dec) > 0)))
1850
class_decimal2decimal(val, &sum_dec);
1851
1855
case REAL_RESULT:
1853
double nr= args[0]->val_real();
1854
if (!args[0]->null_value && (null_value || nr > sum))
1857
double nr= args[0]->val_real();
1858
if (!args[0]->null_value && (null_value || nr > sum))
1861
1865
case ROW_RESULT:
1863
1866
// This case should never be choosen
1952
1956
switch(hybrid_type) {
1953
1957
case STRING_RESULT:
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)
1959
char buff[MAX_FIELD_WIDTH];
1960
String tmp(buff,sizeof(buff),result_field->charset()),*res;
1962
res=args[0]->val_str(&tmp);
1963
if (args[0]->null_value)
2012
1965
result_field->set_null();
1966
result_field->reset();
2014
1970
result_field->set_notnull();
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);
1971
result_field->store(res->ptr(),res->length(),tmp.charset());
1977
int64_t nr=args[0]->val_int();
1981
if (args[0]->null_value)
1984
result_field->set_null();
1987
result_field->set_notnull();
1989
result_field->store(nr, unsigned_flag);
1994
double nr= args[0]->val_real();
1998
if (args[0]->null_value)
2001
result_field->set_null();
2004
result_field->set_notnull();
2006
result_field->store(nr);
2009
case DECIMAL_RESULT:
2011
type::Decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2015
if (args[0]->null_value)
2016
result_field->set_null();
2018
result_field->set_notnull();
2021
We must store zero in the field as we will use the field value in
2024
if (!arg_dec) // Null
2025
arg_dec= &decimal_zero;
2026
result_field->store_decimal(arg_dec);
2025
2029
case ROW_RESULT:
2034
2037
if (hybrid_type == DECIMAL_RESULT)
2036
my_decimal value, *arg_val= args[0]->val_decimal(&value);
2039
type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2037
2040
if (!arg_val) // Null
2038
2041
arg_val= &decimal_zero;
2039
2042
result_field->store_decimal(arg_val);
2079
my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
2082
arg_dec->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
2080
2083
res+= dec_bin_size;
2081
2084
int8store(res, tmp);
2121
2124
if (hybrid_type == DECIMAL_RESULT)
2123
my_decimal value, *arg_val= args[0]->val_decimal(&value);
2126
type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2124
2127
if (!args[0]->null_value)
2126
2129
if (!result_field->is_null())
2128
my_decimal field_value,
2131
type::Decimal field_value,
2129
2132
*field_val= result_field->val_decimal(&field_value);
2130
my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2133
class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2131
2134
result_field->store_decimal(dec_buffs);
2172
2175
unsigned char *res=result_field->ptr;
2173
2176
if (hybrid_type == DECIMAL_RESULT)
2175
my_decimal value, *arg_val= args[0]->val_decimal(&value);
2178
type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2176
2179
if (!args[0]->null_value)
2178
binary2my_decimal(E_DEC_FATAL_ERROR, res,
2181
binary2_class_decimal(E_DEC_FATAL_ERROR, res,
2179
2182
dec_buffs + 1, f_precision, f_scale);
2180
2183
field_count= sint8korr(res + dec_bin_size);
2181
my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
2182
my_decimal2binary(E_DEC_FATAL_ERROR, dec_buffs,
2183
res, f_precision, f_scale);
2184
class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
2185
dec_buffs->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
2184
2186
res+= dec_bin_size;
2186
2188
int8store(res, field_count);
2232
2235
if (!args[0]->null_value)
2234
result_field->val_str(&tmp_value);
2237
result_field->val_str_internal(&tmp_value);
2236
2239
if (result_field->is_null() ||
2237
2240
(cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2297
2300
Item_sum_hybrid::min_max_update_decimal_field()
2299
2302
/* TODO: optimize: do not get result_field in case of args[0] is NULL */
2300
my_decimal old_val, nr_val;
2301
const my_decimal *old_nr= result_field->val_decimal(&old_val);
2302
const my_decimal *nr= args[0]->val_decimal(&nr_val);
2303
type::Decimal old_val, nr_val;
2304
const type::Decimal *old_nr= result_field->val_decimal(&old_val);
2305
const type::Decimal *nr= args[0]->val_decimal(&nr_val);
2303
2306
if (!args[0]->null_value)
2305
2308
if (result_field->is_null(0))
2309
bool res= my_decimal_cmp(old_nr, nr) > 0;
2312
bool res= class_decimal_cmp(old_nr, nr) > 0;
2310
2313
/* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2311
2314
if ((cmp_sign > 0) ^ (!res))
2373
2376
if ((null_value= !count))
2376
my_decimal dec_count, dec_field;
2377
binary2my_decimal(E_DEC_FATAL_ERROR,
2379
type::Decimal dec_count, dec_field;
2380
binary2_class_decimal(E_DEC_FATAL_ERROR,
2378
2381
field->ptr, &dec_field, f_precision, f_scale);
2379
int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
2380
my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2382
int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
2383
class_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2381
2384
&dec_field, &dec_count, prec_increment);
2382
2385
return dec_buf;
2411
my_decimal *Item_std_field::val_decimal(my_decimal *dec_buf)
2414
type::Decimal *Item_std_field::val_decimal(type::Decimal *dec_buf)
2414
2417
We can't call val_decimal_from_real() for DECIMAL_RESULT as
2415
2418
Item_variance_field::val_real() would cause an infinite loop
2417
my_decimal tmp_dec, *dec;
2420
type::Decimal tmp_dec, *dec;
2419
2422
if (hybrid_type == REAL_RESULT)
2420
2423
return val_decimal_from_real(dec_buf);
2422
2425
dec= Item_variance_field::val_decimal(dec_buf);
2425
my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2428
class_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2426
2429
assert(nr >= 0.0);
2428
double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2429
my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
2431
double2_class_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2432
class_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
2430
2433
return dec_buf;
2913
2916
uint32_t offset= (field->offset(field->getTable()->record[0]) -
2914
2917
table->getShare()->null_bytes);
2915
2918
assert(offset < table->getShare()->getRecordLength());
2916
res= field->val_str(&tmp, key + offset);
2919
res= field->val_str_internal(&tmp, key + offset);
2919
2922
res= (*arg)->val_str(&tmp);