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 */
21
21
Sum functions (COUNT, MIN...)
23
#include <drizzled/server_includes.h>
26
24
#include <drizzled/sql_select.h>
27
#include <drizzled/error.h>
28
#include <drizzled/hybrid_type_traits.h>
29
#include <drizzled/hybrid_type_traits_integer.h>
30
#include <drizzled/hybrid_type_traits_decimal.h>
31
#include <drizzled/sql_base.h>
33
#include <drizzled/item/sum.h>
34
#include <drizzled/field/decimal.h>
35
#include <drizzled/field/double.h>
36
#include <drizzled/field/int64.h>
37
#include <drizzled/field/date.h>
38
#include <drizzled/field/datetime.h>
40
#include "drizzled/internal/m_string.h"
49
extern my_decimal decimal_zero;
50
extern plugin::StorageEngine *heap_engine;
25
#include <drizzled/drizzled_error_messages.h>
53
28
Prepare an aggregate function item for checking context conditions.
58
33
If the set function is not allowed in any subquery where it occurs
59
34
an error is reported immediately.
61
@param session reference to the thread context info
36
@param thd reference to the thread context info
64
39
This function is to be called for any item created for a set function
74
bool Item_sum::init_sum_func_check(Session *session)
49
bool Item_sum::init_sum_func_check(THD *thd)
76
if (!session->lex->allow_sum_func)
51
if (!thd->lex->allow_sum_func)
78
53
my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
82
57
/* Set a reference to the nesting set function if there is any */
83
in_sum_func= session->lex->in_sum_func;
58
in_sum_func= thd->lex->in_sum_func;
84
59
/* Save a pointer to object to be used in items for nested set functions */
85
session->lex->in_sum_func= this;
86
nest_level= session->lex->current_select->nest_level;
60
thd->lex->in_sum_func= this;
61
nest_level= thd->lex->current_select->nest_level;
105
80
If the context conditions are not met the method reports an error.
106
81
If the set function is aggregated in some outer subquery the method
107
82
adds it to the chain of items for such set functions that is attached
108
to the the Select_Lex structure for this subquery.
83
to the the st_select_lex structure for this subquery.
110
85
A number of designated members of the object are used to check the
111
86
conditions. They are specified in the comment before the Item_sum
112
87
class declaration.
113
88
Additionally a bitmap variable called allow_sum_func is employed.
114
It is included into the session->lex structure.
89
It is included into the thd->lex structure.
115
90
The bitmap contains 1 at n-th position if the set function happens
116
91
to occur under a construct of the n-th level subquery where usage
117
92
of set functions are allowed (i.e either in the SELECT list or
122
97
HAVING t1.a IN (SELECT t2.c FROM t2 WHERE AVG(t1.b) > 20) AND
123
98
t1.a > (SELECT MIN(t2.d) FROM t2);
125
allow_sum_func will contain:
126
- for SUM(t1.b) - 1 at the first position
100
allow_sum_func will contain:
101
- for SUM(t1.b) - 1 at the first position
127
102
- for AVG(t1.b) - 1 at the first position, 0 at the second position
128
103
- for MIN(t2.d) - 1 at the first position, 1 at the second position.
130
@param session reference to the thread context info
105
@param thd reference to the thread context info
131
106
@param ref location of the pointer to this item in the embedding expression
145
bool Item_sum::check_sum_func(Session *session, Item **ref)
120
bool Item_sum::check_sum_func(THD *thd, Item **ref)
147
122
bool invalid= false;
148
nesting_map allow_sum_func= session->lex->allow_sum_func;
123
nesting_map allow_sum_func= thd->lex->allow_sum_func;
150
125
The value of max_arg_level is updated if an argument of the set function
151
126
contains a column reference resolved against a subquery whose level is
152
127
greater than the current value of max_arg_level.
153
128
max_arg_level cannot be greater than nest level.
154
nest level is always >= 0
129
nest level is always >= 0
156
131
if (nest_level == max_arg_level)
159
The function must be aggregated in the current subquery,
160
If it is there under a construct where it is not allowed
134
The function must be aggregated in the current subquery,
135
If it is there under a construct where it is not allowed
163
138
invalid= !(allow_sum_func & (1 << max_arg_level));
165
140
else if (max_arg_level >= 0 || !(allow_sum_func & (1 << nest_level)))
169
144
Try to find a subquery where it can be aggregated;
170
145
If we fail to find such a subquery report an error.
172
if (register_sum_func(session, ref))
147
if (register_sum_func(thd, ref))
174
149
invalid= aggr_level < 0 && !(allow_sum_func & (1 << nest_level));
175
150
if (!invalid && false)
178
153
if (!invalid && aggr_level < 0)
180
155
aggr_level= nest_level;
181
aggr_sel= session->lex->current_select;
156
aggr_sel= thd->lex->current_select;
184
159
By this moment we either found a subquery where the set function is
185
160
to be aggregated and assigned a value that is >= 0 to aggr_level,
186
or set the value of 'invalid' to TRUE to report later an error.
161
or set the value of 'invalid' to TRUE to report later an error.
189
164
Additionally we have to check whether possible nested set functions
190
165
are acceptable here: they are not, if the level of aggregation of
191
166
some of them is less than aggr_level.
194
169
invalid= aggr_level <= max_sum_func_level;
197
172
my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
205
180
If the set function is nested adjust the value of
206
181
max_sum_func_level for the nesting set function.
207
We take into account only enclosed set functions that are to be
208
aggregated on the same level or above of the nest level of
182
We take into account only enclosed set functions that are to be
183
aggregated on the same level or above of the nest level of
209
184
the enclosing set function.
210
185
But we must always pass up the max_sum_func_level because it is
211
186
the maximum nested level of all directly and indirectly enclosed
268
243
in_sum_func->outer_fields.push_back(field);
272
sel->full_group_by_flag.set(NON_AGG_FIELD_USED);
246
sel->full_group_by_flag|= NON_AGG_FIELD_USED;
275
248
if (sel->nest_level > aggr_level &&
276
(sel->full_group_by_flag.test(SUM_FUNC_USED)) &&
277
! sel->group_list.elements)
249
(sel->full_group_by_flag & SUM_FUNC_USED) &&
250
!sel->group_list.elements)
279
252
my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
280
253
ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
295
268
aggregated. If it finds such a subquery then aggr_level is set to
296
269
the nest level of this subquery and the item for the set function
297
270
is added to the list of set functions used in nested subqueries
298
inner_sum_func_list defined for each subquery. When the item is placed
271
inner_sum_func_list defined for each subquery. When the item is placed
299
272
there the field 'ref_by' is set to ref.
304
277
a subquery in one chain. It would simplify the process of 'splitting'
305
278
for set functions.
307
@param session reference to the thread context info
280
@param thd reference to the thread context info
308
281
@param ref location of the pointer to this item in the embedding expression
311
284
FALSE if the executes without failures (currently always)
316
bool Item_sum::register_sum_func(Session *session, Item **ref)
289
bool Item_sum::register_sum_func(THD *thd, Item **ref)
319
nesting_map allow_sum_func= session->lex->allow_sum_func;
320
for (sl= session->lex->current_select->master_unit()->outer_select() ;
292
nesting_map allow_sum_func= thd->lex->allow_sum_func;
293
for (sl= thd->lex->current_select->master_unit()->outer_select() ;
321
294
sl && sl->nest_level > max_arg_level;
322
295
sl= sl->master_unit()->outer_select() )
331
304
if (sl && (allow_sum_func & (1 << sl->nest_level)))
334
307
We reached the subquery of level max_arg_level and checked
335
that the function can be aggregated here.
308
that the function can be aggregated here.
336
309
The set function will be aggregated in this subquery.
338
311
aggr_level= sl->nest_level;
353
326
aggr_sel->inner_sum_func_list= this;
354
327
aggr_sel->with_sum_func= 1;
357
330
Mark Item_subselect(s) as containing aggregate function all the way up
358
331
to aggregate function's calculation context.
359
332
Note that we must not mark the Item of calculation context itself
360
because with_sum_func on the calculation context Select_Lex is
333
because with_sum_func on the calculation context st_select_lex is
361
334
already set above.
363
with_sum_func being set for an Item means that this Item refers
336
with_sum_func being set for an Item means that this Item refers
364
337
(somewhere in it, e.g. one of its arguments if it's a function) directly
365
338
or through intermediate items to an aggregate function that is calculated
366
339
in a context "outside" of the Item (e.g. in the current or outer select).
368
with_sum_func being set for an Select_Lex means that this Select_Lex
341
with_sum_func being set for an st_select_lex means that this st_select_lex
369
342
has aggregate functions directly referenced (i.e. not through a sub-select).
371
for (sl= session->lex->current_select;
344
for (sl= thd->lex->current_select;
372
345
sl && sl != aggr_sel && sl->master_unit()->item;
373
346
sl= sl->master_unit()->outer_select() )
374
347
sl->master_unit()->item->with_sum_func= 1;
376
session->lex->current_select->mark_as_dependent(aggr_sel);
349
thd->lex->current_select->mark_as_dependent(aggr_sel);
381
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements),
354
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements),
382
355
forced_const(false)
384
if ((args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
357
if ((args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
387
360
List_iterator_fast<Item> li(list);
401
374
Constructor used in processing select with temporary tebles.
404
Item_sum::Item_sum(Session *session, Item_sum *item):
405
Item_result_field(session, item), arg_count(item->arg_count),
377
Item_sum::Item_sum(THD *thd, Item_sum *item):
378
Item_result_field(thd, item), arg_count(item->arg_count),
406
379
aggr_sel(item->aggr_sel),
407
380
nest_level(item->nest_level), aggr_level(item->aggr_level),
408
381
quick_group(item->quick_group), used_tables_cache(item->used_tables_cache),
409
forced_const(item->forced_const)
382
forced_const(item->forced_const)
411
384
if (arg_count <= 2)
414
if (!(args= (Item**) session->alloc(sizeof(Item*)*arg_count)))
387
if (!(args= (Item**) thd->alloc(sizeof(Item*)*arg_count)))
416
389
memcpy(args, item->args, sizeof(Item*)*arg_count);
420
393
void Item_sum::mark_as_sum_func()
422
Select_Lex *cur_select= current_session->lex->current_select;
395
SELECT_LEX *cur_select= current_thd->lex->current_select;
423
396
cur_select->n_sum_items++;
424
397
cur_select->with_sum_func= 1;
425
398
with_sum_func= 1;
429
void Item_sum::make_field(SendField *tmp_field)
402
void Item_sum::make_field(Send_field *tmp_field)
431
404
if (args[0]->type() == Item::FIELD_ITEM && keep_field_type())
466
439
max_length=float_length(decimals);
469
Item *Item_sum::get_tmp_table_item(Session *session)
442
Item *Item_sum::get_tmp_table_item(THD *thd)
471
Item_sum* sum_item= (Item_sum *) copy_or_same(session);
444
Item_sum* sum_item= (Item_sum *) copy_or_same(thd);
472
445
if (sum_item && sum_item->result_field) // If not a const sum func
474
447
Field *result_field_tmp= sum_item->result_field;
507
Field *Item_sum::create_tmp_field(bool ,
480
Field *Item_sum::create_tmp_field(bool group __attribute__((unused)),
509
482
uint32_t convert_blob_length)
513
485
switch (result_type()) {
514
486
case REAL_RESULT:
515
487
field= new Field_double(max_length, maybe_null, name, decimals, true);
519
field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
490
field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
522
492
case STRING_RESULT:
523
493
if (max_length/collation.collation->mbmaxlen <= 255 ||
524
494
convert_blob_length > Field_varstring::MAX_SIZE ||
525
495
!convert_blob_length)
527
496
return make_string_field(table);
530
table->setVariableWidth();
531
497
field= new Field_varstring(convert_blob_length, maybe_null,
532
name, collation.collation);
498
name, table->s, collation.collation);
535
500
case DECIMAL_RESULT:
536
field= new Field_decimal(max_length, maybe_null, name,
537
decimals, unsigned_flag);
501
field= new Field_new_decimal(max_length, maybe_null, name,
502
decimals, unsigned_flag);
541
506
// This case should never be choosen
547
511
field->init(table);
606
Item_sum_num::fix_fields(Session *session, Item **ref)
562
Item_sum_num::fix_fields(THD *thd, Item **ref)
608
564
assert(fixed == 0);
610
if (init_sum_func_check(session))
566
if (init_sum_func_check(thd))
615
571
for (uint32_t i=0 ; i < arg_count ; i++)
617
if (args[i]->fix_fields(session, args + i) || args[i]->check_cols(1))
573
if (args[i]->fix_fields(thd, args + i) || args[i]->check_cols(1))
619
575
set_if_bigger(decimals, args[i]->decimals);
620
576
maybe_null |= args[i]->maybe_null;
635
Item_sum_hybrid::Item_sum_hybrid(Session *session, Item_sum_hybrid *item)
636
:Item_sum(session, item), value(item->value), hybrid_type(item->hybrid_type),
591
Item_sum_hybrid::Item_sum_hybrid(THD *thd, Item_sum_hybrid *item)
592
:Item_sum(thd, item), value(item->value), hybrid_type(item->hybrid_type),
637
593
hybrid_field_type(item->hybrid_field_type), cmp_sign(item->cmp_sign),
638
594
was_values(item->was_values)
660
617
collation.set(item->collation);
664
Item_sum_hybrid::fix_fields(Session *session, Item **ref)
621
Item_sum_hybrid::fix_fields(THD *thd, Item **ref)
666
623
assert(fixed == 0);
668
625
Item *item= args[0];
670
if (init_sum_func_check(session))
627
if (init_sum_func_check(thd))
673
630
// 'item' can be changed during fix_fields
674
if ((!item->fixed && item->fix_fields(session, args)) ||
631
if ((!item->fixed && item->fix_fields(thd, args)) ||
675
632
(item= args[0])->check_cols(1))
677
634
decimals=item->decimals;
722
680
if (args[0]->type() == Item::FIELD_ITEM)
724
682
field= ((Item_field*) args[0])->field;
726
if ((field= create_tmp_field_from_field(current_session, field, name, table,
684
if ((field= create_tmp_field_from_field(current_thd, field, name, table,
727
685
NULL, convert_blob_length)))
728
686
field->flags&= ~NOT_NULL_FLAG;
734
692
fields creations separately.
736
694
switch (args[0]->field_type()) {
737
case DRIZZLE_TYPE_DATE:
738
field= new Field_date(maybe_null, name, collation.collation);
695
case DRIZZLE_TYPE_NEWDATE:
696
field= new Field_newdate(maybe_null, name, collation.collation);
698
case DRIZZLE_TYPE_TIME:
699
field= new Field_time(maybe_null, name, collation.collation);
740
701
case DRIZZLE_TYPE_TIMESTAMP:
741
702
case DRIZZLE_TYPE_DATETIME:
759
720
check if the following assignments are really needed
761
Item_sum_sum::Item_sum_sum(Session *session, Item_sum_sum *item)
762
:Item_sum_num(session, item), hybrid_type(item->hybrid_type),
722
Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item)
723
:Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
763
724
curr_dec_buff(item->curr_dec_buff)
765
726
/* TODO: check if the following assignments are really needed */
775
Item *Item_sum_sum::copy_or_same(Session* session)
736
Item *Item_sum_sum::copy_or_same(THD* thd)
777
return new (session->mem_root) Item_sum_sum(session, this);
738
return new (thd->mem_root) Item_sum_sum(thd, this);
806
767
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);
769
/* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
770
int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
771
max_length= my_decimal_precision_to_length(precision, decimals,
774
hybrid_type= DECIMAL_RESULT;
775
my_decimal_set_zero(dec_buffs);
894
861
static int item_sum_distinct_walk(void *element,
862
element_count num_of_dups __attribute__((unused)),
898
865
return ((Item_sum_distinct*) (item))->unique_walk_function(element);
901
872
/* Item_sum_distinct */
903
874
Item_sum_distinct::Item_sum_distinct(Item *item_arg)
916
Item_sum_distinct::Item_sum_distinct(Session *session, Item_sum_distinct *original)
917
:Item_sum_num(session, original), val(original->val), tree(0),
887
Item_sum_distinct::Item_sum_distinct(THD *thd, Item_sum_distinct *original)
888
:Item_sum_num(thd, original), val(original->val), tree(0),
918
889
table_field_type(original->table_field_type)
954
925
return &fast_decimal_traits_instance;
958
928
void Item_sum_distinct::fix_length_and_dec()
960
930
assert(args[0]->fixed);
962
null_value= maybe_null= true;
963
932
table_field_type= args[0]->field_type();
965
934
/* Adjust tmp table type according to the chosen aggregation type */
970
939
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;
943
Preserving int8, int16, int32 field types gives ~10% performance boost
944
as the size of result tree becomes significantly smaller.
945
Another speed up we gain by using int64_t for intermediate
946
calculations. The range of int64 is enough to hold sum 2^32 distinct
947
integers each <= 2^32.
949
if (table_field_type >= DRIZZLE_TYPE_TINY && table_field_type <= DRIZZLE_TYPE_LONG)
951
val.traits= Hybrid_type_traits_fast_decimal::instance();
954
table_field_type= DRIZZLE_TYPE_LONGLONG;
987
956
case DECIMAL_RESULT:
988
957
val.traits= Hybrid_type_traits_decimal::instance();
989
958
if (table_field_type != DRIZZLE_TYPE_LONGLONG)
990
table_field_type= DRIZZLE_TYPE_DECIMAL;
959
table_field_type= DRIZZLE_TYPE_NEWDECIMAL;
996
965
val.traits->fix_length_and_dec(this, args[0]);
1000
enum Item_result Item_sum_distinct::result_type () const
1002
return val.traits->type();
1008
971
check that the case of CHAR(0) works OK
1010
bool Item_sum_distinct::setup(Session *session)
973
bool Item_sum_distinct::setup(THD *thd)
1012
List<CreateField> field_list;
1013
CreateField field_def; /* field definition */
975
List<Create_field> field_list;
976
Create_field field_def; /* field definition */
1014
977
/* It's legal to call setup() more than once when in a subquery */
1029
992
assert(args[0]->fixed);
1031
994
field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
1032
args[0]->decimals, args[0]->maybe_null);
995
args[0]->decimals, args[0]->maybe_null,
996
args[0]->unsigned_flag);
1034
if (! (table= session->getInstanceTable(field_list)))
998
if (! (table= create_virtual_tmp_table(thd, field_list)))
1037
1001
/* XXX: check that the case of CHAR(0) works OK */
1038
tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
1002
tree_key_length= table->s->reclength - table->s->null_bytes;
1041
1005
Unique handles all unique elements in a tree until they can't fit
1043
1007
simple_raw_key_cmp because the table contains numbers only; decimals
1044
1008
are converted to binary representation as well.
1046
tree= new Unique(simple_raw_key_cmp, &tree_key_length,
1048
(size_t)session->variables.max_heap_table_size);
1010
tree= new Unique(simple_raw_key_cmp, &tree_key_length, tree_key_length,
1011
thd->variables.max_heap_table_size);
1050
1013
is_evaluated= false;
1051
1014
return(tree == 0);
1055
1018
bool Item_sum_distinct::add()
1057
args[0]->save_in_field(table->getField(0), false);
1020
args[0]->save_in_field(table->field[0], false);
1058
1021
is_evaluated= false;
1059
if (!table->getField(0)->is_null())
1022
if (!table->field[0]->is_null())
1064
1027
'0' values are also stored in the tree. This doesn't matter
1065
1028
for SUM(DISTINCT), but is important for AVG(DISTINCT)
1067
return tree->unique_add(table->getField(0)->ptr);
1030
return tree->unique_add(table->field[0]->ptr);
1073
1036
bool Item_sum_distinct::unique_walk_function(void *element)
1075
memcpy(table->getField(0)->ptr, element, tree_key_length);
1038
memcpy(table->field[0]->ptr, element, tree_key_length);
1077
val.traits->add(&val, table->getField(0));
1040
val.traits->add(&val, table->field[0]);
1163
1126
Item_sum_avg_distinct::fix_length_and_dec()
1165
1128
Item_sum_distinct::fix_length_and_dec();
1166
prec_increment= current_session->variables.div_precincrement;
1129
prec_increment= current_thd->variables.div_precincrement;
1168
1131
AVG() will divide val by count. We need to reserve digits
1169
1132
after decimal point as the result can be fractional.
1171
decimals= min(decimals + prec_increment, (unsigned int)NOT_FIXED_DEC);
1134
decimals= cmin(decimals + prec_increment, (unsigned int)NOT_FIXED_DEC);
1188
Item *Item_sum_count::copy_or_same(Session* session)
1151
Item *Item_sum_count::copy_or_same(THD* thd)
1190
return new (session->mem_root) Item_sum_count(session, this);
1153
return new (thd->mem_root) Item_sum_count(thd, this);
1227
1190
Item_sum_sum::fix_length_and_dec();
1228
1191
maybe_null=null_value=1;
1229
prec_increment= current_session->variables.div_precincrement;
1192
prec_increment= current_thd->variables.div_precincrement;
1230
1193
if (hybrid_type == DECIMAL_RESULT)
1232
1195
int precision= args[0]->decimal_precision() + prec_increment;
1233
decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1196
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1234
1197
max_length= my_decimal_precision_to_length(precision, decimals,
1235
1198
unsigned_flag);
1236
f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1199
f_precision= cmin(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1237
1200
f_scale= args[0]->decimals;
1238
1201
dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1241
decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1204
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1242
1205
max_length= args[0]->max_length + prec_increment;
1247
Item *Item_sum_avg::copy_or_same(Session* session)
1210
Item *Item_sum_avg::copy_or_same(THD* thd)
1249
return new (session->mem_root) Item_sum_avg(session, this);
1212
return new (thd->mem_root) Item_sum_avg(thd, this);
1253
1216
Field *Item_sum_avg::create_tmp_field(bool group, Table *table,
1217
uint32_t convert_blob_len __attribute__((unused)))
1261
1224
The easiest way is to do this is to store both value in a string
1262
1225
and unpack on access.
1264
table->setVariableWidth();
1265
1227
field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
1266
1228
dec_bin_size : sizeof(double)) + sizeof(int64_t),
1267
0, name, &my_charset_bin);
1229
0, name, table->s, &my_charset_bin);
1269
1231
else if (hybrid_type == DECIMAL_RESULT)
1270
field= new Field_decimal(max_length, maybe_null, name,
1271
decimals, unsigned_flag);
1232
field= new Field_new_decimal(max_length, maybe_null, name,
1233
decimals, unsigned_flag);
1273
1235
field= new Field_double(max_length, maybe_null, name, decimals, true);
1356
1312
return sqrt(nr);
1359
Item *Item_sum_std::copy_or_same(Session* session)
1315
Item *Item_sum_std::copy_or_same(THD* thd)
1361
return new (session->mem_root) Item_sum_std(session, this);
1317
return new (thd->mem_root) Item_sum_std(thd, this);
1413
Item_sum_variance::Item_sum_variance(Session *session, Item_sum_variance *item):
1414
Item_sum_num(session, item), hybrid_type(item->hybrid_type),
1369
Item_sum_variance::Item_sum_variance(THD *thd, Item_sum_variance *item):
1370
Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
1415
1371
count(item->count), sample(item->sample),
1416
1372
prec_increment(item->prec_increment)
1423
1379
void Item_sum_variance::fix_length_and_dec()
1425
1381
maybe_null= null_value= 1;
1426
prec_increment= current_session->variables.div_precincrement;
1382
prec_increment= current_thd->variables.div_precincrement;
1429
1385
According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1430
aggregate function; paragraph 7h of Syntax Rules), "the declared
1386
aggregate function; paragraph 7h of Syntax Rules), "the declared
1431
1387
type of the result is an implementation-defined aproximate numeric
1436
1392
switch (args[0]->result_type()) {
1437
1393
case REAL_RESULT:
1438
1394
case STRING_RESULT:
1439
decimals= min(args[0]->decimals + 4, (int)NOT_FIXED_DEC);
1395
decimals= cmin(args[0]->decimals + 4, NOT_FIXED_DEC);
1441
1397
case INT_RESULT:
1442
1398
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,
1400
int precision= args[0]->decimal_precision()*2 + prec_increment;
1401
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1402
max_length= my_decimal_precision_to_length(precision, decimals,
1451
1407
case ROW_RESULT:
1457
Item *Item_sum_variance::copy_or_same(Session* session)
1415
Item *Item_sum_variance::copy_or_same(THD* thd)
1459
return new (session->mem_root) Item_sum_variance(session, this);
1417
return new (thd->mem_root) Item_sum_variance(thd, this);
1476
1434
The easiest way is to do this is to store both value in a string
1477
1435
and unpack on access.
1479
table->setVariableWidth();
1480
field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, &my_charset_bin);
1437
field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, table->s, &my_charset_bin);
1483
1440
field= new Field_double(max_length, maybe_null, name, decimals, true);
1492
1449
void Item_sum_variance::clear()
1497
1454
bool Item_sum_variance::add()
1500
1457
Why use a temporary variable? We don't know if it is null until we
1501
1458
evaluate it, which has the side-effect of setting null_value .
1503
1460
double nr= args[0]->val_real();
1505
1462
if (!args[0]->null_value)
1506
1463
variance_fp_recurrence_next(&recurrence_m, &recurrence_s, &count, nr);
1619
1569
assert(fixed == 1);
1620
1570
if (null_value)
1623
1572
switch (hybrid_type) {
1624
1573
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);
1577
String *res; res=val_str(&str_value);
1578
return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1579
&end_not_used, &err_not_used) : 0.0);
1632
1581
case INT_RESULT:
1583
return uint64_t2double(sum_int);
1633
1584
return (double) sum_int;
1634
1585
case DECIMAL_RESULT:
1635
1586
my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1749
Item *Item_sum_min::copy_or_same(Session* session)
1697
Item *Item_sum_min::copy_or_same(THD* thd)
1751
return new (session->mem_root) Item_sum_min(session, this);
1699
return new (thd->mem_root) Item_sum_min(thd, this);
1757
1705
switch (hybrid_type) {
1758
1706
case STRING_RESULT:
1708
String *result=args[0]->val_str(&tmp_value);
1709
if (!args[0]->null_value &&
1710
(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);
1712
value.copy(*result);
1769
1717
case INT_RESULT:
1719
int64_t nr=args[0]->val_int();
1720
if (!args[0]->null_value && (null_value ||
1722
(uint64_t) nr < (uint64_t) sum_int) ||
1723
(!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
1730
case DECIMAL_RESULT:
1732
my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1733
if (!args[0]->null_value &&
1734
(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);
1736
my_decimal2decimal(val, &sum_dec);
1793
1741
case REAL_RESULT:
1743
double nr= args[0]->val_real();
1744
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
1751
case ROW_RESULT:
1804
1753
// This case should never be choosen
1812
Item *Item_sum_max::copy_or_same(Session* session)
1761
Item *Item_sum_max::copy_or_same(THD* thd)
1814
return new (session->mem_root) Item_sum_max(session, this);
1763
return new (thd->mem_root) Item_sum_max(thd, this);
1820
1769
switch (hybrid_type) {
1821
1770
case STRING_RESULT:
1772
String *result=args[0]->val_str(&tmp_value);
1773
if (!args[0]->null_value &&
1774
(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);
1776
value.copy(*result);
1832
1781
case INT_RESULT:
1783
int64_t nr=args[0]->val_int();
1784
if (!args[0]->null_value && (null_value ||
1786
(uint64_t) nr > (uint64_t) sum_int) ||
1787
(!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
1794
case DECIMAL_RESULT:
1796
my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1797
if (!args[0]->null_value &&
1798
(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);
1800
my_decimal2decimal(val, &sum_dec);
1856
1805
case REAL_RESULT:
1807
double nr= args[0]->val_real();
1808
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
1815
case ROW_RESULT:
1867
1817
// This case should never be choosen
1887
1836
bits= reset_bits;
1890
Item *Item_sum_or::copy_or_same(Session* session)
1839
Item *Item_sum_or::copy_or_same(THD* thd)
1892
return new (session->mem_root) Item_sum_or(session, this);
1841
return new (thd->mem_root) Item_sum_or(thd, this);
1904
Item *Item_sum_xor::copy_or_same(Session* session)
1853
Item *Item_sum_xor::copy_or_same(THD* thd)
1906
return new (session->mem_root) Item_sum_xor(session, this);
1855
return new (thd->mem_root) Item_sum_xor(thd, this);
1918
Item *Item_sum_and::copy_or_same(Session* session)
1867
Item *Item_sum_and::copy_or_same(THD* thd)
1920
return new (session->mem_root) Item_sum_and(session, this);
1869
return new (thd->mem_root) Item_sum_and(thd, this);
1957
1906
switch(hybrid_type) {
1958
1907
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)
1909
char buff[MAX_FIELD_WIDTH];
1910
String tmp(buff,sizeof(buff),result_field->charset()),*res;
1912
res=args[0]->val_str(&tmp);
1913
if (args[0]->null_value)
1915
result_field->set_null();
1916
result_field->reset();
1920
result_field->set_notnull();
1921
result_field->store(res->ptr(),res->length(),tmp.charset());
1927
int64_t nr=args[0]->val_int();
1931
if (args[0]->null_value)
1934
result_field->set_null();
1937
result_field->set_notnull();
1939
result_field->store(nr, unsigned_flag);
1944
double nr= args[0]->val_real();
1948
if (args[0]->null_value)
1951
result_field->set_null();
1954
result_field->set_notnull();
1956
result_field->store(nr);
1959
case DECIMAL_RESULT:
1961
my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
1965
if (args[0]->null_value)
1966
1966
result_field->set_null();
1967
result_field->reset();
1971
1968
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);
1971
We must store zero in the field as we will use the field value in
1974
if (!arg_dec) // Null
1975
arg_dec= &decimal_zero;
1976
result_field->store_decimal(arg_dec);
2030
1979
case ROW_RESULT:
2237
2186
if (!args[0]->null_value)
2239
result_field->val_str_internal(&tmp_value);
2188
result_field->val_str(&tmp_value);
2241
2190
if (result_field->is_null() ||
2242
2191
(cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2461
int64_t Item_variance_field::val_int()
2463
/* can't be fix_fields()ed */
2464
return (int64_t) rint(val_real());
2468
2410
double Item_variance_field::val_real()
2470
2412
// fix_fields() never calls for this Item
2503
2445
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2505
2447
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();
2448
Field **field = item->table->field;
2449
Field **field_end= field + item->table->s->fields;
2508
2450
uint32_t *lengths=item->field_lengths;
2509
2451
for (; field < field_end; ++field)
2522
static int count_distinct_walk(void *,
2468
static int count_distinct_walk(void *elem __attribute__((unused)),
2469
element_count count __attribute__((unused)),
2526
2472
(*((uint64_t*)arg))++;
2530
2482
void Item_sum_count_distinct::cleanup()
2532
2484
Item_sum_int::cleanup();
2580
bool Item_sum_count_distinct::setup(Session *session)
2533
bool Item_sum_count_distinct::setup(THD *thd)
2582
2535
List<Item> list;
2583
Select_Lex *select_lex= session->lex->current_select;
2536
SELECT_LEX *select_lex= thd->lex->current_select;
2586
2539
Setup can be called twice for ROLLUP items. This is a bug.
2608
2561
tmp_table_param->force_copy_fields= force_copy_fields;
2609
2562
assert(table == 0);
2611
if (!(table= create_tmp_table(session, tmp_table_param, list, (Order*) 0, 1,
2564
if (!(table= create_tmp_table(thd, tmp_table_param, list, (order_st*) 0, 1,
2613
(select_lex->options | session->options),
2566
(select_lex->options | thd->options),
2614
2567
HA_POS_ERROR, (char*)"")))
2618
table->cursor->extra(HA_EXTRA_NO_ROWS); // Don't update rows
2569
table->file->extra(HA_EXTRA_NO_ROWS); // Don't update rows
2619
2570
table->no_rows=1;
2621
if (table->getShare()->db_type() == heap_engine)
2572
if (table->s->db_type() == heap_hton)
2624
2575
No blobs, otherwise it would have been MyISAM: set up a compare
2627
2578
qsort_cmp2 compare_key;
2629
Field **field= table->getFields();
2630
Field **field_end= field + table->getShare()->sizeFields();
2580
Field **field= table->field;
2581
Field **field_end= field + table->s->fields;
2631
2582
bool all_binary= true;
2633
2584
for (tree_key_length= 0; field < field_end; ++field)
2665
2616
uint32_t *length;
2666
2617
compare_key= (qsort_cmp2) composite_key_cmp;
2667
2618
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();
2619
field_lengths= (uint32_t*) thd->alloc(table->s->fields * sizeof(uint32_t));
2620
for (tree_key_length= 0, length= field_lengths, field= table->field;
2670
2621
field < field_end; ++field, ++length)
2672
2623
*length= (*field)->pack_length();
2677
2628
assert(tree == 0);
2678
2629
tree= new Unique(compare_key, cmp_arg, tree_key_length,
2679
(size_t)session->variables.max_heap_table_size);
2630
thd->variables.max_heap_table_size);
2681
2632
The only time tree_key_length could be 0 is if someone does
2682
2633
count(distinct) on a char(0) field - stupid thing to do,
2694
Item *Item_sum_count_distinct::copy_or_same(Session* session)
2645
Item *Item_sum_count_distinct::copy_or_same(THD* thd)
2696
return new (session->mem_root) Item_sum_count_distinct(session, this);
2647
return new (thd->mem_root) Item_sum_count_distinct(thd, this);
2708
2659
else if (table)
2710
table->cursor->extra(HA_EXTRA_NO_CACHE);
2711
table->cursor->ha_delete_all_rows();
2712
table->cursor->extra(HA_EXTRA_WRITE_CACHE);
2661
table->file->extra(HA_EXTRA_NO_CACHE);
2662
table->file->ha_delete_all_rows();
2663
table->file->extra(HA_EXTRA_WRITE_CACHE);
2719
2670
if (always_null)
2721
2672
copy_fields(tmp_table_param);
2722
if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
2673
copy_funcs(tmp_table_param->items_to_copy);
2725
for (Field **field= table->getFields() ; *field ; field++)
2675
for (Field **field=table->field ; *field ; field++)
2727
2676
if ((*field)->is_real_null(0))
2729
2677
return 0; // Don't count NULL
2733
2679
is_evaluated= false;
2739
2685
bloat the tree without providing any valuable info. Besides,
2740
2686
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);
2688
return tree->unique_add(table->record[0] + table->s->null_bytes);
2744
if ((error= table->cursor->insertRecord(table->record[0])) &&
2745
table->cursor->is_fatal_error(error, HA_CHECK_DUP))
2690
if ((error= table->file->ha_write_row(table->record[0])) &&
2691
table->file->is_fatal_error(error, HA_CHECK_DUP))
2767
2713
return (int64_t) count;
2770
error= table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2716
error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2774
table->print_error(error, MYF(0));
2720
table->file->print_error(error, MYF(0));
2777
return table->cursor->stats.records;
2723
return table->file->stats.records;
2780
2726
/*****************************************************************************
2791
2737
*****************************************************************************/
2795
2741
Compares the values for fields in expr list of GROUP_CONCAT.
2798
2744
GROUP_CONCAT([DISTINCT] expr [,expr ...]
2799
2745
[order_st BY {unsigned_integer | col_name | expr}
2800
2746
[ASC | DESC] [,col_name ...]]
2801
2747
[SEPARATOR str_val])
2804
@retval -1 : key1 < key2
2750
@retval -1 : key1 < key2
2805
2751
@retval 0 : key1 = key2
2806
@retval 1 : key1 > key2
2752
@retval 1 : key1 > key2
2809
int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
2755
int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
2810
2756
const void* key2)
2812
2758
Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
2815
2761
for (uint32_t i= 0; i < item_func->arg_count_field; i++)
2817
2763
Item *item= item_func->args[i];
2819
2765
If field_item is a const item then either get_tp_table_field returns 0
2820
or it is an item over a const table.
2766
or it is an item over a const table.
2822
2768
if (item->const_item())
2829
2775
Field *field= item->get_tmp_table_field();
2831
uint32_t offset= field->offset(field->getTable()->record[0])-table->getShare()->null_bytes;
2777
uint32_t offset= field->offset(field->table->record[0])-table->s->null_bytes;
2832
2778
if((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2840
function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
2786
function of sort for syntax: GROUP_CONCAT(expr,... order_st BY col,... )
2843
int group_concat_key_cmp_with_order(void* arg, const void* key1,
2789
int group_concat_key_cmp_with_order(void* arg, const void* key1,
2844
2790
const void* key2)
2846
2792
Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2847
Order **order_item, **end;
2793
order_st **order_item, **end;
2848
2794
Table *table= grp_item->table;
2850
2796
for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2858
2804
the temporary table, not the original field
2860
2806
Field *field= item->get_tmp_table_field();
2862
2808
If item is a const item then either get_tp_table_field returns 0
2863
or it is an item over a const table.
2809
or it is an item over a const table.
2865
2811
if (field && !item->const_item())
2868
uint32_t offset= (field->offset(field->getTable()->record[0]) -
2869
table->getShare()->null_bytes);
2814
uint32_t offset= (field->offset(field->table->record[0]) -
2815
table->s->null_bytes);
2870
2816
if ((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2871
2817
return (*order_item)->asc ? res : -res;
2884
2830
Append data from current leaf to item->result.
2887
int dump_leaf_key(unsigned char* key, uint32_t ,
2833
int dump_leaf_key(unsigned char* key, element_count count __attribute__((unused)),
2888
2834
Item_func_group_concat *item)
2890
2836
Table *table= item->table;
2891
String tmp((char *)table->getUpdateRecord(), table->getShare()->getRecordLength(),
2837
String tmp((char *)table->record[1], table->s->reclength,
2892
2838
default_charset_info);
2894
2840
String *result= &item->result;
2915
2861
because it contains both order and arg list fields.
2917
2863
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);
2864
uint32_t offset= (field->offset(field->table->record[0]) -
2865
table->s->null_bytes);
2866
assert(offset < table->s->reclength);
2867
res= field->val_str(&tmp, key + offset);
2924
2870
res= (*arg)->val_str(&tmp);
2966
2912
bool distinct_arg, List<Item> *select_list,
2967
2913
SQL_LIST *order_list, String *separator_arg)
2968
2914
:tmp_table_param(0), warning(0),
2969
separator(separator_arg), tree(NULL), unique_filter(NULL), table(0),
2915
separator(separator_arg), tree(0), unique_filter(NULL), table(0),
2970
2916
order(0), context(context_arg),
2971
2917
arg_count_order(order_list ? order_list->elements : 0),
2972
2918
arg_count_field(select_list->elements),
2987
2933
(for possible order items in temporare tables)
2988
2934
order - arg_count_order
2990
if (!(args= (Item**) memory::sql_alloc(sizeof(Item*) * arg_count +
2991
sizeof(Order*)*arg_count_order)))
2936
if (!(args= (Item**) sql_alloc(sizeof(Item*) * arg_count +
2937
sizeof(order_st*)*arg_count_order)))
2994
order= (Order**)(args + arg_count);
2940
order= (order_st**)(args + arg_count);
2996
2942
/* fill args items of show and sort */
2997
2943
List_iterator_fast<Item> li(*select_list);
3002
2948
if (arg_count_order)
3004
Order **order_ptr= order;
3005
for (Order *order_item= (Order*) order_list->first;
2950
order_st **order_ptr= order;
2951
for (order_st *order_item= (order_st*) order_list->first;
3006
2952
order_item != NULL;
3007
2953
order_item= order_item->next)
3017
Item_func_group_concat::Item_func_group_concat(Session *session,
2963
Item_func_group_concat::Item_func_group_concat(THD *thd,
3018
2964
Item_func_group_concat *item)
3019
:Item_sum(session, item),
2965
:Item_sum(thd, item),
3020
2966
tmp_table_param(item->tmp_table_param),
3021
2967
warning(item->warning),
3022
2968
separator(item->separator),
3050
2996
char warn_buff[DRIZZLE_ERRMSG_SIZE];
3051
snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3052
warning->set_msg(current_session, warn_buff);
2997
sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
2998
warning->set_msg(current_thd, warn_buff);
3080
3027
char warn_buff[DRIZZLE_ERRMSG_SIZE];
3081
snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3082
warning->set_msg(session, warn_buff);
3028
sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3029
warning->set_msg(thd, warn_buff);
3092
Item *Item_func_group_concat::copy_or_same(Session* session)
3039
Item *Item_func_group_concat::copy_or_same(THD* thd)
3094
return new (session->mem_root) Item_func_group_concat(session, this);
3041
return new (thd->mem_root) Item_func_group_concat(thd, this);
3132
3078
null_value= false;
3133
3079
bool row_eligible= true;
3137
3083
/* Filter out duplicate rows. */
3138
3084
uint32_t count= unique_filter->elements_in_tree();
3139
unique_filter->unique_add(table->record[0] + table->getShare()->null_bytes);
3085
unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3140
3086
if (count == unique_filter->elements_in_tree())
3141
3087
row_eligible= false;
3144
3090
TREE_ELEMENT *el= 0; // Only for safety
3145
3091
if (row_eligible && tree)
3146
el= tree_insert(tree, table->record[0] + table->getShare()->null_bytes, 0,
3092
el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
3147
3093
tree->custom_arg);
3149
3095
If the row is not a duplicate (el->count == 1)
3153
3099
if (row_eligible && !warning_for_row &&
3154
3100
(!tree || (el->count == 1 && distinct && !arg_count_order)))
3155
dump_leaf_key(table->record[0] + table->getShare()->null_bytes, 1, this);
3101
dump_leaf_key(table->record[0] + table->s->null_bytes, 1, this);
3162
Item_func_group_concat::fix_fields(Session *session, Item **ref)
3108
Item_func_group_concat::fix_fields(THD *thd, Item **ref)
3164
3110
uint32_t i; /* for loop variable */
3165
3111
assert(fixed == 0);
3167
if (init_sum_func_check(session))
3113
if (init_sum_func_check(thd))
3191
3137
result.set_charset(collation.collation);
3192
3138
result_field= 0;
3194
max_length= (size_t)session->variables.group_concat_max_len;
3196
if (check_sum_func(session, ref))
3140
max_length= thd->variables.group_concat_max_len;
3143
if (separator->needs_conversion(separator->length(), separator->charset(),
3144
collation.collation, &offset))
3146
uint32_t buflen= collation.collation->mbmaxlen * separator->length();
3147
uint32_t errors, conv_length;
3149
String *new_separator;
3151
if (!(buf= (char*) thd->alloc(buflen)) ||
3152
!(new_separator= new(thd->mem_root)
3153
String(buf, buflen, collation.collation)))
3156
conv_length= copy_and_convert(buf, buflen, collation.collation,
3157
separator->ptr(), separator->length(),
3158
separator->charset(), &errors);
3159
new_separator->length(conv_length);
3160
separator= new_separator;
3163
if (check_sum_func(thd, ref))
3204
bool Item_func_group_concat::setup(Session *session)
3171
bool Item_func_group_concat::setup(THD *thd)
3206
3173
List<Item> list;
3207
Select_Lex *select_lex= session->lex->current_select;
3174
SELECT_LEX *select_lex= thd->lex->current_select;
3210
3177
Currently setup() can be called twice. Please add
3244
3211
tmp table columns.
3246
3213
if (arg_count_order &&
3247
setup_order(session, args, context->table_list, list, all_fields, *order))
3214
setup_order(thd, args, context->table_list, list, all_fields, *order))
3250
3217
count_field_types(select_lex, tmp_table_param, all_fields, 0);
3256
3223
Currently we have to force conversion of BLOB values to VARCHAR's
3257
if we are to store them in TREE objects used for ORDER BY and
3224
if we are to store them in TREE objects used for order_st BY and
3258
3225
DISTINCT. This leads to truncation if the BLOB's size exceeds
3259
3226
Field_varstring::MAX_SIZE.
3261
set_if_smaller(tmp_table_param->convert_blob_length,
3228
set_if_smaller(tmp_table_param->convert_blob_length,
3262
3229
Field_varstring::MAX_SIZE);
3266
3233
We have to create a temporary table to get descriptions of fields
3267
3234
(types, sizes and so on).
3269
Note that in the table, we first have the ORDER BY fields, then the
3236
Note that in the table, we first have the order_st BY fields, then the
3272
if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3273
(Order*) 0, 0, true,
3274
(select_lex->options | session->options),
3239
if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
3240
(order_st*) 0, 0, true,
3241
(select_lex->options | thd->options),
3275
3242
HA_POS_ERROR, (char*) "")))
3280
table->cursor->extra(HA_EXTRA_NO_ROWS);
3244
table->file->extra(HA_EXTRA_NO_ROWS);
3281
3245
table->no_rows= 1;
3285
3249
Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3286
3250
the row is not added to the result.
3288
uint32_t tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
3252
uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
3290
3254
if (arg_count_order)
3292
3256
tree= &tree_base;
3294
3258
Create a tree for sorting. The tree is used to sort (according to the
3295
syntax of this function). If there is no ORDER BY clause, we don't
3259
syntax of this function). If there is no order_st BY clause, we don't
3296
3260
create this tree.
3298
init_tree(tree, (uint32_t) min(session->variables.max_heap_table_size,
3299
(uint64_t)(session->variables.sortbuff_size/16)),
3302
group_concat_key_cmp_with_order , false, NULL, (void*) this);
3262
init_tree(tree, (uint) cmin(thd->variables.max_heap_table_size,
3263
thd->variables.sortbuff_size/16), 0,
3265
group_concat_key_cmp_with_order , 0, NULL, (void*) this);
3306
3269
unique_filter= new Unique(group_concat_key_cmp_with_distinct,
3308
3271
tree_key_length,
3309
(size_t)session->variables.max_heap_table_size);
3272
thd->variables.max_heap_table_size);
3326
double Item_func_group_concat::val_real()
3328
String *res; res=val_str(&str_value);
3329
return res ? internal::my_atof(res->c_ptr()) : 0.0;
3332
int64_t Item_func_group_concat::val_int()
3337
if (!(res= val_str(&str_value)))
3339
end_ptr= (char*) res->ptr()+ res->length();
3340
return internal::my_strtoll10(res->ptr(), &end_ptr, &error);
3343
String* Item_func_group_concat::val_str(String* )
3290
String* Item_func_group_concat::val_str(String* str __attribute__((unused)))
3345
3292
assert(fixed == 1);
3346
3293
if (null_value)