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 */
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
table->setVariableWidth();
531
524
field= new Field_varstring(convert_blob_length, maybe_null,
532
name, collation.collation);
525
name, table->s, collation.collation);
535
527
case DECIMAL_RESULT:
536
528
field= new Field_decimal(max_length, maybe_null, name,
537
decimals, unsigned_flag);
529
decimals, unsigned_flag);
541
533
// This case should never be choosen
547
538
field->init(table);
806
798
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);
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);
970
963
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;
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;
987
980
case DECIMAL_RESULT:
988
981
val.traits= Hybrid_type_traits_decimal::instance();
989
982
if (table_field_type != DRIZZLE_TYPE_LONGLONG)
990
983
table_field_type= DRIZZLE_TYPE_DECIMAL;
996
989
val.traits->fix_length_and_dec(this, args[0]);
1031
1024
field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
1032
1025
args[0]->decimals, args[0]->maybe_null);
1034
if (! (table= session->getInstanceTable(field_list)))
1027
if (! (table= session->create_virtual_tmp_table(field_list)))
1037
1030
/* XXX: check that the case of CHAR(0) works OK */
1038
tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
1031
tree_key_length= table->s->reclength - table->s->null_bytes;
1041
1034
Unique handles all unique elements in a tree until they can't fit
1055
1048
bool Item_sum_distinct::add()
1057
args[0]->save_in_field(table->getField(0), false);
1050
args[0]->save_in_field(table->field[0], false);
1058
1051
is_evaluated= false;
1059
if (!table->getField(0)->is_null())
1052
if (!table->field[0]->is_null())
1064
1057
'0' values are also stored in the tree. This doesn't matter
1065
1058
for SUM(DISTINCT), but is important for AVG(DISTINCT)
1067
return tree->unique_add(table->getField(0)->ptr);
1060
return tree->unique_add(table->field[0]->ptr);
1073
1066
bool Item_sum_distinct::unique_walk_function(void *element)
1075
memcpy(table->getField(0)->ptr, element, tree_key_length);
1068
memcpy(table->field[0]->ptr, element, tree_key_length);
1077
val.traits->add(&val, table->getField(0));
1070
val.traits->add(&val, table->field[0]);
1261
1254
The easiest way is to do this is to store both value in a string
1262
1255
and unpack on access.
1264
table->setVariableWidth();
1265
1257
field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
1266
1258
dec_bin_size : sizeof(double)) + sizeof(int64_t),
1267
0, name, &my_charset_bin);
1259
0, name, table->s, &my_charset_bin);
1269
1261
else if (hybrid_type == DECIMAL_RESULT)
1270
1262
field= new Field_decimal(max_length, maybe_null, name,
1441
1433
case INT_RESULT:
1442
1434
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,
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,
1451
1443
case ROW_RESULT:
1476
1470
The easiest way is to do this is to store both value in a string
1477
1471
and unpack on access.
1479
table->setVariableWidth();
1480
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->s, &my_charset_bin);
1483
1476
field= new Field_double(max_length, maybe_null, name, decimals, true);
1619
1612
assert(fixed == 1);
1620
1613
if (null_value)
1623
1615
switch (hybrid_type) {
1624
1616
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);
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);
1632
1624
case INT_RESULT:
1633
1625
return (double) sum_int;
1634
1626
case DECIMAL_RESULT:
1757
1746
switch (hybrid_type) {
1758
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))
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);
1753
value.copy(*result);
1769
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)))
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
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)))
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);
1777
my_decimal2decimal(val, &sum_dec);
1793
1782
case REAL_RESULT:
1784
double nr= args[0]->val_real();
1785
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
1792
case ROW_RESULT:
1804
1794
// This case should never be choosen
1820
1810
switch (hybrid_type) {
1821
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))
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);
1817
value.copy(*result);
1832
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)))
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
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)))
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);
1841
my_decimal2decimal(val, &sum_dec);
1856
1846
case REAL_RESULT:
1848
double nr= args[0]->val_real();
1849
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
1856
case ROW_RESULT:
1867
1858
// This case should never be choosen
1957
1947
switch(hybrid_type) {
1958
1948
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)
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)
1966
2007
result_field->set_null();
1967
result_field->reset();
1971
2009
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);
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);
2030
2020
case ROW_RESULT:
2237
2227
if (!args[0]->null_value)
2239
result_field->val_str_internal(&tmp_value);
2229
result_field->val_str(&tmp_value);
2241
2231
if (result_field->is_null() ||
2242
2232
(cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2503
2493
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2505
2495
Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2506
Field **field = item->table->getFields();
2507
Field **field_end= field + item->table->getShare()->sizeFields();
2496
Field **field = item->table->field;
2497
Field **field_end= field + item->table->s->fields;
2508
2498
uint32_t *lengths=item->field_lengths;
2509
2499
for (; field < field_end; ++field)
2608
2599
tmp_table_param->force_copy_fields= force_copy_fields;
2609
2600
assert(table == 0);
2611
if (!(table= create_tmp_table(session, tmp_table_param, list, (Order*) 0, 1,
2602
if (!(table= create_tmp_table(session, tmp_table_param, list, (order_st*) 0, 1,
2613
2604
(select_lex->options | session->options),
2614
2605
HA_POS_ERROR, (char*)"")))
2618
2607
table->cursor->extra(HA_EXTRA_NO_ROWS); // Don't update rows
2619
2608
table->no_rows=1;
2621
if (table->getShare()->db_type() == heap_engine)
2610
if (table->s->db_type() == heap_engine)
2624
2613
No blobs, otherwise it would have been MyISAM: set up a compare
2627
2616
qsort_cmp2 compare_key;
2629
Field **field= table->getFields();
2630
Field **field_end= field + table->getShare()->sizeFields();
2618
Field **field= table->field;
2619
Field **field_end= field + table->s->fields;
2631
2620
bool all_binary= true;
2633
2622
for (tree_key_length= 0; field < field_end; ++field)
2665
2654
uint32_t *length;
2666
2655
compare_key= (qsort_cmp2) composite_key_cmp;
2667
2656
cmp_arg= (void*) this;
2668
field_lengths= (uint32_t*) session->alloc(table->getShare()->sizeFields() * sizeof(uint32_t));
2669
for (tree_key_length= 0, length= field_lengths, field= table->getFields();
2657
field_lengths= (uint32_t*) session->alloc(table->s->fields * sizeof(uint32_t));
2658
for (tree_key_length= 0, length= field_lengths, field= table->field;
2670
2659
field < field_end; ++field, ++length)
2672
2661
*length= (*field)->pack_length();
2719
2708
if (always_null)
2721
2710
copy_fields(tmp_table_param);
2722
if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
2711
copy_funcs(tmp_table_param->items_to_copy);
2725
for (Field **field= table->getFields() ; *field ; field++)
2713
for (Field **field=table->field ; *field ; field++)
2727
2714
if ((*field)->is_real_null(0))
2729
2715
return 0; // Don't count NULL
2733
2717
is_evaluated= false;
2739
2723
bloat the tree without providing any valuable info. Besides,
2740
2724
key_length used to initialize the tree didn't include space for them.
2742
return tree->unique_add(table->record[0] + table->getShare()->null_bytes);
2726
return tree->unique_add(table->record[0] + table->s->null_bytes);
2744
2728
if ((error= table->cursor->insertRecord(table->record[0])) &&
2745
2729
table->cursor->is_fatal_error(error, HA_CHECK_DUP))
2829
2813
Field *field= item->get_tmp_table_field();
2831
uint32_t offset= field->offset(field->getTable()->record[0])-table->getShare()->null_bytes;
2815
uint32_t offset= field->offset(field->table->record[0])-table->s->null_bytes;
2832
2816
if((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2844
2828
const void* key2)
2846
2830
Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2847
Order **order_item, **end;
2831
order_st **order_item, **end;
2848
2832
Table *table= grp_item->table;
2850
2834
for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2865
2849
if (field && !item->const_item())
2868
uint32_t offset= (field->offset(field->getTable()->record[0]) -
2869
table->getShare()->null_bytes);
2852
uint32_t offset= (field->offset(field->table->record[0]) -
2853
table->s->null_bytes);
2870
2854
if ((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2871
2855
return (*order_item)->asc ? res : -res;
2888
2872
Item_func_group_concat *item)
2890
2874
Table *table= item->table;
2891
String tmp((char *)table->getUpdateRecord(), table->getShare()->getRecordLength(),
2875
String tmp((char *)table->record[1], table->s->reclength,
2892
2876
default_charset_info);
2894
2878
String *result= &item->result;
2915
2899
because it contains both order and arg list fields.
2917
2901
Field *field= (*arg)->get_tmp_table_field();
2918
uint32_t offset= (field->offset(field->getTable()->record[0]) -
2919
table->getShare()->null_bytes);
2920
assert(offset < table->getShare()->getRecordLength());
2921
res= field->val_str_internal(&tmp, key + offset);
2902
uint32_t offset= (field->offset(field->table->record[0]) -
2903
table->s->null_bytes);
2904
assert(offset < table->s->reclength);
2905
res= field->val_str(&tmp, key + offset);
2924
2908
res= (*arg)->val_str(&tmp);
2988
2972
order - arg_count_order
2990
2974
if (!(args= (Item**) memory::sql_alloc(sizeof(Item*) * arg_count +
2991
sizeof(Order*)*arg_count_order)))
2975
sizeof(order_st*)*arg_count_order)))
2994
order= (Order**)(args + arg_count);
2978
order= (order_st**)(args + arg_count);
2996
2980
/* fill args items of show and sort */
2997
2981
List_iterator_fast<Item> li(*select_list);
3002
2986
if (arg_count_order)
3004
Order **order_ptr= order;
3005
for (Order *order_item= (Order*) order_list->first;
2988
order_st **order_ptr= order;
2989
for (order_st *order_item= (order_st*) order_list->first;
3006
2990
order_item != NULL;
3007
2991
order_item= order_item->next)
3137
3121
/* Filter out duplicate rows. */
3138
3122
uint32_t count= unique_filter->elements_in_tree();
3139
unique_filter->unique_add(table->record[0] + table->getShare()->null_bytes);
3123
unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3140
3124
if (count == unique_filter->elements_in_tree())
3141
3125
row_eligible= false;
3144
3128
TREE_ELEMENT *el= 0; // Only for safety
3145
3129
if (row_eligible && tree)
3146
el= tree_insert(tree, table->record[0] + table->getShare()->null_bytes, 0,
3130
el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
3147
3131
tree->custom_arg);
3149
3133
If the row is not a duplicate (el->count == 1)
3153
3137
if (row_eligible && !warning_for_row &&
3154
3138
(!tree || (el->count == 1 && distinct && !arg_count_order)))
3155
dump_leaf_key(table->record[0] + table->getShare()->null_bytes, 1, this);
3139
dump_leaf_key(table->record[0] + table->s->null_bytes, 1, this);
3272
3256
if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3273
(Order*) 0, 0, true,
3257
(order_st*) 0, 0, true,
3274
3258
(select_lex->options | session->options),
3275
3259
HA_POS_ERROR, (char*) "")))
3280
3261
table->cursor->extra(HA_EXTRA_NO_ROWS);
3281
3262
table->no_rows= 1;
3285
3266
Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3286
3267
the row is not added to the result.
3288
uint32_t tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
3269
uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
3290
3271
if (arg_count_order)