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
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);
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= my_decimal_precision_to_length(precision, decimals,
813
hybrid_type= DECIMAL_RESULT;
814
my_decimal_set_zero(dec_buffs);
966
970
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;
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;
983
987
case DECIMAL_RESULT:
984
988
val.traits= Hybrid_type_traits_decimal::instance();
985
989
if (table_field_type != DRIZZLE_TYPE_LONGLONG)
986
990
table_field_type= DRIZZLE_TYPE_DECIMAL;
992
996
val.traits->fix_length_and_dec(this, args[0]);
1437
1441
case INT_RESULT:
1438
1442
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,
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,
1447
1451
case ROW_RESULT:
1617
1619
assert(fixed == 1);
1618
1620
if (null_value)
1620
1623
switch (hybrid_type) {
1621
1624
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);
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);
1629
1632
case INT_RESULT:
1630
1633
return (double) sum_int;
1631
1634
case DECIMAL_RESULT:
1751
1757
switch (hybrid_type) {
1752
1758
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);
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);
1763
1769
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)))
1776
1782
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);
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);
1787
1793
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))
1797
1803
case ROW_RESULT:
1799
1804
// This case should never be choosen
1815
1820
switch (hybrid_type) {
1816
1821
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);
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);
1827
1832
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)))
1840
1845
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);
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);
1851
1856
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))
1861
1866
case ROW_RESULT:
1863
1867
// This case should never be choosen
1952
1957
switch(hybrid_type) {
1953
1958
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)
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)
2012
1966
result_field->set_null();
1967
result_field->reset();
2014
1971
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);
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);
2025
2030
case ROW_RESULT: