12
12
You should have received a copy of the GNU General Public License
13
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
29
29
#include <drizzled/hybrid_type_traits_integer.h>
30
30
#include <drizzled/hybrid_type_traits_decimal.h>
31
31
#include <drizzled/sql_base.h>
32
#include <drizzled/session.h>
34
33
#include <drizzled/item/sum.h>
35
34
#include <drizzled/field/decimal.h>
36
35
#include <drizzled/field/double.h>
37
#include <drizzled/field/int64.h>
36
#include <drizzled/field/int64_t.h>
38
37
#include <drizzled/field/date.h>
39
38
#include <drizzled/field/datetime.h>
40
#include <drizzled/unique.h>
42
#include <drizzled/type/decimal.h>
44
#include <drizzled/internal/m_string.h>
40
#include "drizzled/internal/m_string.h"
46
42
#include <algorithm>
77
74
bool Item_sum::init_sum_func_check(Session *session)
79
if (!session->getLex()->allow_sum_func)
76
if (!session->lex->allow_sum_func)
81
78
my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
85
82
/* Set a reference to the nesting set function if there is any */
86
in_sum_func= session->getLex()->in_sum_func;
83
in_sum_func= session->lex->in_sum_func;
87
84
/* Save a pointer to object to be used in items for nested set functions */
88
session->getLex()->in_sum_func= this;
89
nest_level= session->getLex()->current_select->nest_level;
85
session->lex->in_sum_func= this;
86
nest_level= session->lex->current_select->nest_level;
94
91
max_sum_func_level= -1;
148
145
bool Item_sum::check_sum_func(Session *session, Item **ref)
150
147
bool invalid= false;
151
nesting_map allow_sum_func= session->getLex()->allow_sum_func;
148
nesting_map allow_sum_func= session->lex->allow_sum_func;
153
150
The value of max_arg_level is updated if an argument of the set function
154
151
contains a column reference resolved against a subquery whose level is
319
316
bool Item_sum::register_sum_func(Session *session, Item **ref)
322
nesting_map allow_sum_func= session->getLex()->allow_sum_func;
323
for (sl= session->getLex()->current_select->master_unit()->outer_select() ;
319
nesting_map allow_sum_func= session->lex->allow_sum_func;
320
for (sl= session->lex->current_select->master_unit()->outer_select() ;
324
321
sl && sl->nest_level > max_arg_level;
325
322
sl= sl->master_unit()->outer_select() )
371
368
with_sum_func being set for an Select_Lex means that this Select_Lex
372
369
has aggregate functions directly referenced (i.e. not through a sub-select).
374
for (sl= session->getLex()->current_select;
371
for (sl= session->lex->current_select;
375
372
sl && sl != aggr_sel && sl->master_unit()->item;
376
373
sl= sl->master_unit()->outer_select() )
377
374
sl->master_unit()->item->with_sum_func= 1;
379
session->getLex()->current_select->mark_as_dependent(aggr_sel);
376
session->lex->current_select->mark_as_dependent(aggr_sel);
423
420
void Item_sum::mark_as_sum_func()
425
Select_Lex *cur_select= getSession().getLex()->current_select;
422
Select_Lex *cur_select= current_session->lex->current_select;
426
423
cur_select->n_sum_items++;
427
424
cur_select->with_sum_func= 1;
428
425
with_sum_func= 1;
512
509
uint32_t convert_blob_length)
516
512
switch (result_type()) {
517
513
case REAL_RESULT:
518
514
field= new Field_double(max_length, maybe_null, name, decimals, true);
522
field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
517
field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
525
519
case STRING_RESULT:
526
520
if (max_length/collation.collation->mbmaxlen <= 255 ||
527
521
convert_blob_length > Field_varstring::MAX_SIZE ||
528
522
!convert_blob_length)
530
523
return make_string_field(table);
533
table->setVariableWidth();
534
524
field= new Field_varstring(convert_blob_length, maybe_null,
535
name, collation.collation);
525
name, table->getMutableShare(), collation.collation);
538
527
case DECIMAL_RESULT:
539
528
field= new Field_decimal(max_length, maybe_null, name,
540
decimals, unsigned_flag);
529
decimals, unsigned_flag);
544
533
// This case should never be choosen
550
538
field->init(table);
727
716
field= ((Item_field*) args[0])->field;
729
if ((field= create_tmp_field_from_field(&getSession(), field, name, table,
718
if ((field= create_tmp_field_from_field(current_session, field, name, table,
730
719
NULL, convert_blob_length)))
731
720
field->flags&= ~NOT_NULL_FLAG;
739
728
switch (args[0]->field_type()) {
740
729
case DRIZZLE_TYPE_DATE:
741
field= new Field_date(maybe_null, name);
730
field= new Field_date(maybe_null, name, collation.collation);
743
732
case DRIZZLE_TYPE_TIMESTAMP:
744
733
case DRIZZLE_TYPE_DATETIME:
745
field= new Field_datetime(maybe_null, name);
734
field= new Field_datetime(maybe_null, name, collation.collation);
748
737
return Item_sum::create_tmp_field(group, table, convert_blob_length);
752
740
field->init(table);
770
757
/* TODO: check if the following assignments are really needed */
771
758
if (hybrid_type == DECIMAL_RESULT)
773
class_decimal2decimal(item->dec_buffs, dec_buffs);
774
class_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
760
my_decimal2decimal(item->dec_buffs, dec_buffs);
761
my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
811
798
case DECIMAL_RESULT:
813
/* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
814
int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
815
max_length= class_decimal_precision_to_length(precision, decimals,
818
hybrid_type= DECIMAL_RESULT;
819
dec_buffs->set_zero();
800
/* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
801
int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
802
max_length= my_decimal_precision_to_length(precision, decimals,
805
hybrid_type= DECIMAL_RESULT;
806
my_decimal_set_zero(dec_buffs);
830
819
if (hybrid_type == DECIMAL_RESULT)
832
type::Decimal value, *val= args[0]->val_decimal(&value);
821
my_decimal value, *val= args[0]->val_decimal(&value);
833
822
if (!args[0]->null_value)
835
class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
824
my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
836
825
val, dec_buffs + curr_dec_buff);
837
826
curr_dec_buff^= 1;
854
843
if (hybrid_type == DECIMAL_RESULT)
857
(dec_buffs + curr_dec_buff)->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
846
my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
860
850
return (int64_t) rint(val_real());
866
856
assert(fixed == 1);
867
857
if (hybrid_type == DECIMAL_RESULT)
868
class_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
858
my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
942
932
virtual void div(Hybrid_type *val, uint64_t u) const
944
int2_class_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
934
int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
945
935
val->used_dec_buf_no= 0;
946
936
val->traits= Hybrid_type_traits_decimal::instance();
947
937
val->traits->div(val, u);
974
963
table_field_type= DRIZZLE_TYPE_DOUBLE;
978
Preserving int8, int16, int32 field types gives ~10% performance boost
979
as the size of result tree becomes significantly smaller.
980
Another speed up we gain by using int64_t for intermediate
981
calculations. The range of int64 is enough to hold sum 2^32 distinct
982
integers each <= 2^32.
984
if (table_field_type == DRIZZLE_TYPE_LONG)
986
val.traits= Hybrid_type_traits_fast_decimal::instance();
989
table_field_type= DRIZZLE_TYPE_LONGLONG;
967
Preserving int8, int16, int32 field types gives ~10% performance boost
968
as the size of result tree becomes significantly smaller.
969
Another speed up we gain by using int64_t for intermediate
970
calculations. The range of int64 is enough to hold sum 2^32 distinct
971
integers each <= 2^32.
973
if (table_field_type == DRIZZLE_TYPE_LONG)
975
val.traits= Hybrid_type_traits_fast_decimal::instance();
978
table_field_type= DRIZZLE_TYPE_LONGLONG;
991
980
case DECIMAL_RESULT:
992
981
val.traits= Hybrid_type_traits_decimal::instance();
993
982
if (table_field_type != DRIZZLE_TYPE_LONGLONG)
994
983
table_field_type= DRIZZLE_TYPE_DECIMAL;
1000
989
val.traits->fix_length_and_dec(this, args[0]);
1035
1024
field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
1036
1025
args[0]->decimals, args[0]->maybe_null);
1038
if (! (table= session->getInstanceTable(field_list)))
1027
if (! (table= session->create_virtual_tmp_table(field_list)))
1041
1030
/* XXX: check that the case of CHAR(0) works OK */
1167
1156
Item_sum_avg_distinct::fix_length_and_dec()
1169
1158
Item_sum_distinct::fix_length_and_dec();
1170
prec_increment= getSession().variables.div_precincrement;
1159
prec_increment= current_session->variables.div_precincrement;
1172
1161
AVG() will divide val by count. We need to reserve digits
1173
1162
after decimal point as the result can be fractional.
1231
1220
Item_sum_sum::fix_length_and_dec();
1232
1221
maybe_null=null_value=1;
1233
prec_increment= getSession().variables.div_precincrement;
1222
prec_increment= current_session->variables.div_precincrement;
1235
1223
if (hybrid_type == DECIMAL_RESULT)
1237
1225
int precision= args[0]->decimal_precision() + prec_increment;
1238
1226
decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1239
max_length= class_decimal_precision_to_length(precision, decimals,
1227
max_length= my_decimal_precision_to_length(precision, decimals,
1240
1228
unsigned_flag);
1241
1229
f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1242
1230
f_scale= args[0]->decimals;
1243
dec_bin_size= class_decimal_get_binary_size(f_precision, f_scale);
1231
dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1246
1234
decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1266
1254
The easiest way is to do this is to store both value in a string
1267
1255
and unpack on access.
1269
table->setVariableWidth();
1270
1257
field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
1271
1258
dec_bin_size : sizeof(double)) + sizeof(int64_t),
1272
0, name, &my_charset_bin);
1259
0, name, table->getMutableShare(), &my_charset_bin);
1274
1261
else if (hybrid_type == DECIMAL_RESULT)
1275
1262
field= new Field_decimal(max_length, maybe_null, name,
1319
type::Decimal *Item_sum_avg::val_decimal(type::Decimal *val)
1306
my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
1321
type::Decimal sum_buff, cnt;
1322
const type::Decimal *sum_dec;
1308
my_decimal sum_buff, cnt;
1309
const my_decimal *sum_dec;
1323
1310
assert(fixed == 1);
1335
1322
return val_decimal_from_real(val);
1337
1324
sum_dec= dec_buffs + curr_dec_buff;
1338
int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
1339
class_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1325
int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
1326
my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1428
1415
void Item_sum_variance::fix_length_and_dec()
1430
1417
maybe_null= null_value= 1;
1431
prec_increment= getSession().variables.div_precincrement;
1418
prec_increment= current_session->variables.div_precincrement;
1434
1421
According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1446
1433
case INT_RESULT:
1447
1434
case DECIMAL_RESULT:
1449
int precision= args[0]->decimal_precision()*2 + prec_increment;
1450
decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1451
max_length= class_decimal_precision_to_length(precision, decimals,
1436
int precision= args[0]->decimal_precision()*2 + prec_increment;
1437
decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1438
max_length= my_decimal_precision_to_length(precision, decimals,
1456
1443
case ROW_RESULT:
1481
1470
The easiest way is to do this is to store both value in a string
1482
1471
and unpack on access.
1484
table->setVariableWidth();
1485
field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, &my_charset_bin);
1473
field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, table->getMutableShare(), &my_charset_bin);
1488
1476
field= new Field_double(max_length, maybe_null, name, decimals, true);
1624
1612
assert(fixed == 1);
1625
1613
if (null_value)
1628
1615
switch (hybrid_type) {
1629
1616
case STRING_RESULT:
1633
String *res; res=val_str(&str_value);
1634
return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1635
&end_not_used, &err_not_used) : 0.0);
1620
String *res; res=val_str(&str_value);
1621
return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1622
&end_not_used, &err_not_used) : 0.0);
1637
1624
case INT_RESULT:
1638
1625
return (double) sum_int;
1639
1626
case DECIMAL_RESULT:
1640
class_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1627
my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1642
1629
case REAL_RESULT:
1644
1631
case ROW_RESULT:
1645
1633
// This case should never be choosen
1653
1639
int64_t Item_sum_hybrid::val_int()
1673
type::Decimal *Item_sum_hybrid::val_decimal(type::Decimal *val)
1659
my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
1675
1661
assert(fixed == 1);
1676
1662
if (null_value)
1679
1664
switch (hybrid_type) {
1680
1665
case STRING_RESULT:
1681
val->store(E_DEC_FATAL_ERROR, &value);
1666
string2my_decimal(E_DEC_FATAL_ERROR, &value, val);
1683
1668
case REAL_RESULT:
1684
double2_class_decimal(E_DEC_FATAL_ERROR, sum, val);
1669
double2my_decimal(E_DEC_FATAL_ERROR, sum, val);
1686
1671
case DECIMAL_RESULT:
1689
1674
case INT_RESULT:
1690
int2_class_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1675
int2my_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1692
1677
case ROW_RESULT:
1693
1679
// This case should never be choosen
1698
1683
return val; // Keep compiler happy
1713
1697
str->set_real(sum,decimals, &my_charset_bin);
1715
1699
case DECIMAL_RESULT:
1716
class_decimal2string(&sum_dec, 0, str);
1700
my_decimal2string(E_DEC_FATAL_ERROR, &sum_dec, 0, 0, 0, str);
1718
1702
case INT_RESULT:
1719
1703
str->set_int(sum_int, unsigned_flag, &my_charset_bin);
1762
1746
switch (hybrid_type) {
1763
1747
case STRING_RESULT:
1749
String *result=args[0]->val_str(&tmp_value);
1750
if (!args[0]->null_value &&
1751
(null_value || sortcmp(&value,result,collation.collation) > 0))
1765
String *result=args[0]->val_str(&tmp_value);
1766
if (!args[0]->null_value &&
1767
(null_value || sortcmp(&value,result,collation.collation) > 0))
1769
value.copy(*result);
1753
value.copy(*result);
1774
1758
case INT_RESULT:
1760
int64_t nr=args[0]->val_int();
1761
if (!args[0]->null_value && (null_value ||
1763
(uint64_t) nr < (uint64_t) sum_int) ||
1764
(!unsigned_flag && nr < sum_int)))
1776
int64_t nr=args[0]->val_int();
1777
if (!args[0]->null_value && (null_value ||
1779
(uint64_t) nr < (uint64_t) sum_int) ||
1780
(!unsigned_flag && nr < sum_int)))
1787
1771
case DECIMAL_RESULT:
1773
my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1774
if (!args[0]->null_value &&
1775
(null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
1789
type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1790
if (!args[0]->null_value &&
1791
(null_value || (class_decimal_cmp(&sum_dec, val) > 0)))
1793
class_decimal2decimal(val, &sum_dec);
1777
my_decimal2decimal(val, &sum_dec);
1798
1782
case REAL_RESULT:
1784
double nr= args[0]->val_real();
1785
if (!args[0]->null_value && (null_value || nr < sum))
1800
double nr= args[0]->val_real();
1801
if (!args[0]->null_value && (null_value || nr < sum))
1808
1792
case ROW_RESULT:
1809
1794
// This case should never be choosen
1825
1810
switch (hybrid_type) {
1826
1811
case STRING_RESULT:
1813
String *result=args[0]->val_str(&tmp_value);
1814
if (!args[0]->null_value &&
1815
(null_value || sortcmp(&value,result,collation.collation) < 0))
1828
String *result=args[0]->val_str(&tmp_value);
1829
if (!args[0]->null_value &&
1830
(null_value || sortcmp(&value,result,collation.collation) < 0))
1832
value.copy(*result);
1817
value.copy(*result);
1837
1822
case INT_RESULT:
1824
int64_t nr=args[0]->val_int();
1825
if (!args[0]->null_value && (null_value ||
1827
(uint64_t) nr > (uint64_t) sum_int) ||
1828
(!unsigned_flag && nr > sum_int)))
1839
int64_t nr=args[0]->val_int();
1840
if (!args[0]->null_value && (null_value ||
1842
(uint64_t) nr > (uint64_t) sum_int) ||
1843
(!unsigned_flag && nr > sum_int)))
1850
1835
case DECIMAL_RESULT:
1837
my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1838
if (!args[0]->null_value &&
1839
(null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
1852
type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1853
if (!args[0]->null_value &&
1854
(null_value || (class_decimal_cmp(val, &sum_dec) > 0)))
1856
class_decimal2decimal(val, &sum_dec);
1841
my_decimal2decimal(val, &sum_dec);
1861
1846
case REAL_RESULT:
1848
double nr= args[0]->val_real();
1849
if (!args[0]->null_value && (null_value || nr > sum))
1863
double nr= args[0]->val_real();
1864
if (!args[0]->null_value && (null_value || nr > sum))
1871
1856
case ROW_RESULT:
1872
1858
// This case should never be choosen
1962
1947
switch(hybrid_type) {
1963
1948
case STRING_RESULT:
1965
char buff[MAX_FIELD_WIDTH];
1966
String tmp(buff,sizeof(buff),result_field->charset()),*res;
1968
res=args[0]->val_str(&tmp);
1969
if (args[0]->null_value)
1950
char buff[MAX_FIELD_WIDTH];
1951
String tmp(buff,sizeof(buff),result_field->charset()),*res;
1953
res=args[0]->val_str(&tmp);
1954
if (args[0]->null_value)
1956
result_field->set_null();
1957
result_field->reset();
1961
result_field->set_notnull();
1962
result_field->store(res->ptr(),res->length(),tmp.charset());
1968
int64_t nr=args[0]->val_int();
1972
if (args[0]->null_value)
1975
result_field->set_null();
1978
result_field->set_notnull();
1980
result_field->store(nr, unsigned_flag);
1985
double nr= args[0]->val_real();
1989
if (args[0]->null_value)
1992
result_field->set_null();
1995
result_field->set_notnull();
1997
result_field->store(nr);
2000
case DECIMAL_RESULT:
2002
my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2006
if (args[0]->null_value)
1971
2007
result_field->set_null();
1972
result_field->reset();
1976
2009
result_field->set_notnull();
1977
result_field->store(res->ptr(),res->length(),tmp.charset());
1983
int64_t nr=args[0]->val_int();
1987
if (args[0]->null_value)
1990
result_field->set_null();
1993
result_field->set_notnull();
1995
result_field->store(nr, unsigned_flag);
2000
double nr= args[0]->val_real();
2004
if (args[0]->null_value)
2007
result_field->set_null();
2010
result_field->set_notnull();
2012
result_field->store(nr);
2015
case DECIMAL_RESULT:
2017
type::Decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2021
if (args[0]->null_value)
2022
result_field->set_null();
2024
result_field->set_notnull();
2027
We must store zero in the field as we will use the field value in
2030
if (!arg_dec) // Null
2031
arg_dec= &decimal_zero;
2032
result_field->store_decimal(arg_dec);
2012
We must store zero in the field as we will use the field value in
2015
if (!arg_dec) // Null
2016
arg_dec= &decimal_zero;
2017
result_field->store_decimal(arg_dec);
2035
2020
case ROW_RESULT:
2043
2029
if (hybrid_type == DECIMAL_RESULT)
2045
type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2031
my_decimal value, *arg_val= args[0]->val_decimal(&value);
2046
2032
if (!arg_val) // Null
2047
2033
arg_val= &decimal_zero;
2048
2034
result_field->store_decimal(arg_val);
2088
arg_dec->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
2074
my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
2089
2075
res+= dec_bin_size;
2090
2076
int8store(res, tmp);
2130
2116
if (hybrid_type == DECIMAL_RESULT)
2132
type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2118
my_decimal value, *arg_val= args[0]->val_decimal(&value);
2133
2119
if (!args[0]->null_value)
2135
2121
if (!result_field->is_null())
2137
type::Decimal field_value,
2123
my_decimal field_value,
2138
2124
*field_val= result_field->val_decimal(&field_value);
2139
class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2125
my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2140
2126
result_field->store_decimal(dec_buffs);
2181
2167
unsigned char *res=result_field->ptr;
2182
2168
if (hybrid_type == DECIMAL_RESULT)
2184
type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2170
my_decimal value, *arg_val= args[0]->val_decimal(&value);
2185
2171
if (!args[0]->null_value)
2187
binary2_class_decimal(E_DEC_FATAL_ERROR, res,
2173
binary2my_decimal(E_DEC_FATAL_ERROR, res,
2188
2174
dec_buffs + 1, f_precision, f_scale);
2189
2175
field_count= sint8korr(res + dec_bin_size);
2190
class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
2191
dec_buffs->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
2176
my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
2177
my_decimal2binary(E_DEC_FATAL_ERROR, dec_buffs,
2178
res, f_precision, f_scale);
2192
2179
res+= dec_bin_size;
2194
2181
int8store(res, field_count);
2241
2227
if (!args[0]->null_value)
2243
result_field->val_str_internal(&tmp_value);
2229
result_field->val_str(&tmp_value);
2245
2231
if (result_field->is_null() ||
2246
2232
(cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2306
2292
Item_sum_hybrid::min_max_update_decimal_field()
2308
2294
/* TODO: optimize: do not get result_field in case of args[0] is NULL */
2309
type::Decimal old_val, nr_val;
2310
const type::Decimal *old_nr= result_field->val_decimal(&old_val);
2311
const type::Decimal *nr= args[0]->val_decimal(&nr_val);
2295
my_decimal old_val, nr_val;
2296
const my_decimal *old_nr= result_field->val_decimal(&old_val);
2297
const my_decimal *nr= args[0]->val_decimal(&nr_val);
2312
2298
if (!args[0]->null_value)
2314
2300
if (result_field->is_null(0))
2318
bool res= class_decimal_cmp(old_nr, nr) > 0;
2304
bool res= my_decimal_cmp(old_nr, nr) > 0;
2319
2305
/* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2320
2306
if ((cmp_sign > 0) ^ (!res))
2382
2368
if ((null_value= !count))
2385
type::Decimal dec_count, dec_field;
2386
binary2_class_decimal(E_DEC_FATAL_ERROR,
2371
my_decimal dec_count, dec_field;
2372
binary2my_decimal(E_DEC_FATAL_ERROR,
2387
2373
field->ptr, &dec_field, f_precision, f_scale);
2388
int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
2389
class_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2374
int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
2375
my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2390
2376
&dec_field, &dec_count, prec_increment);
2391
2377
return dec_buf;
2420
type::Decimal *Item_std_field::val_decimal(type::Decimal *dec_buf)
2406
my_decimal *Item_std_field::val_decimal(my_decimal *dec_buf)
2423
2409
We can't call val_decimal_from_real() for DECIMAL_RESULT as
2424
2410
Item_variance_field::val_real() would cause an infinite loop
2426
type::Decimal tmp_dec, *dec;
2412
my_decimal tmp_dec, *dec;
2428
2414
if (hybrid_type == REAL_RESULT)
2429
2415
return val_decimal_from_real(dec_buf);
2431
2417
dec= Item_variance_field::val_decimal(dec_buf);
2434
class_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2420
my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2435
2421
assert(nr >= 0.0);
2437
double2_class_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2438
class_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
2423
double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2424
my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
2439
2425
return dec_buf;
2612
2598
tmp_table_param->force_copy_fields= force_copy_fields;
2613
2599
assert(table == 0);
2615
if (!(table= create_tmp_table(session, tmp_table_param, list, (Order*) 0, 1,
2601
if (!(table= create_tmp_table(session, tmp_table_param, list, (order_st*) 0, 1,
2617
2603
(select_lex->options | session->options),
2618
2604
HA_POS_ERROR, (char*)"")))
2669
2655
uint32_t *length;
2670
2656
compare_key= (qsort_cmp2) composite_key_cmp;
2671
2657
cmp_arg= (void*) this;
2672
field_lengths= (uint32_t*) session->getMemRoot()->allocate(table->getShare()->sizeFields() * sizeof(uint32_t));
2658
field_lengths= (uint32_t*) session->alloc(table->getShare()->sizeFields() * sizeof(uint32_t));
2673
2659
for (tree_key_length= 0, length= field_lengths, field= table->getFields();
2674
2660
field < field_end; ++field, ++length)
2723
2709
if (always_null)
2725
2711
copy_fields(tmp_table_param);
2726
if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
2712
copy_funcs(tmp_table_param->items_to_copy);
2729
2714
for (Field **field= table->getFields() ; *field ; field++)
2833
2818
Field *field= item->get_tmp_table_field();
2835
uint32_t offset= field->offset(field->getTable()->record[0])-table->getShare()->null_bytes;
2820
uint32_t offset= field->offset(field->table->record[0])-table->getShare()->null_bytes;
2836
2821
if((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2848
2833
const void* key2)
2850
2835
Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2851
Order **order_item, **end;
2836
order_st **order_item, **end;
2852
2837
Table *table= grp_item->table;
2854
2839
for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2869
2854
if (field && !item->const_item())
2872
uint32_t offset= (field->offset(field->getTable()->record[0]) -
2857
uint32_t offset= (field->offset(field->table->record[0]) -
2873
2858
table->getShare()->null_bytes);
2874
2859
if ((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2875
2860
return (*order_item)->asc ? res : -res;
2892
2877
Item_func_group_concat *item)
2894
2879
Table *table= item->table;
2895
String tmp((char *)table->getUpdateRecord(), table->getShare()->getRecordLength(),
2880
String tmp((char *)table->record[1], table->getShare()->getRecordLength(),
2896
2881
default_charset_info);
2898
2883
String *result= &item->result;
2919
2904
because it contains both order and arg list fields.
2921
2906
Field *field= (*arg)->get_tmp_table_field();
2922
uint32_t offset= (field->offset(field->getTable()->record[0]) -
2907
uint32_t offset= (field->offset(field->table->record[0]) -
2923
2908
table->getShare()->null_bytes);
2924
2909
assert(offset < table->getShare()->getRecordLength());
2925
res= field->val_str_internal(&tmp, key + offset);
2910
res= field->val_str(&tmp, key + offset);
2928
2913
res= (*arg)->val_str(&tmp);
2992
2977
order - arg_count_order
2994
2979
if (!(args= (Item**) memory::sql_alloc(sizeof(Item*) * arg_count +
2995
sizeof(Order*)*arg_count_order)))
2980
sizeof(order_st*)*arg_count_order)))
2998
order= (Order**)(args + arg_count);
2983
order= (order_st**)(args + arg_count);
3000
2985
/* fill args items of show and sort */
3001
List<Item>::iterator li(select_list->begin());
2986
List_iterator_fast<Item> li(*select_list);
3003
2988
for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
3004
2989
*arg_ptr= item_select;
3006
2991
if (arg_count_order)
3008
Order **order_ptr= order;
3009
for (Order *order_item= (Order*) order_list->first;
2993
order_st **order_ptr= order;
2994
for (order_st *order_item= (order_st*) order_list->first;
3010
2995
order_item != NULL;
3011
2996
order_item= order_item->next)
3054
3039
char warn_buff[DRIZZLE_ERRMSG_SIZE];
3055
3040
snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3056
warning->set_msg(&getSession(), warn_buff);
3041
warning->set_msg(current_session, warn_buff);
3276
3260
if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3277
(Order*) 0, 0, true,
3261
(order_st*) 0, 0, true,
3278
3262
(select_lex->options | session->options),
3279
3263
HA_POS_ERROR, (char*) "")))