~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/sum.cc

  • Committer: Mark Atwood
  • Date: 2009-01-05 04:37:22 UTC
  • mto: (758.1.1 devel)
  • mto: This revision was merged to the branch mainline in revision 759.
  • Revision ID: me@mark.atwood.name-20090105043722-03l4mzcxi4yjjjih
replace sql_print_error etc with errmsg_print

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
*/
23
23
#include <drizzled/server_includes.h>
24
24
#include <drizzled/sql_select.h>
25
 
#include <drizzled/drizzled_error_messages.h>
 
25
#include <drizzled/error.h>
 
26
#include <drizzled/hybrid_type_traits.h>
 
27
#include <drizzled/hybrid_type_traits_integer.h>
 
28
#include <drizzled/hybrid_type_traits_decimal.h>
 
29
#include <drizzled/sql_base.h>
 
30
 
 
31
#include <drizzled/item/sum.h>
 
32
#include <drizzled/field/decimal.h>
 
33
#include <drizzled/field/double.h>
 
34
#include <drizzled/field/int64_t.h>
 
35
#include <drizzled/field/date.h>
 
36
#include <drizzled/field/timetype.h>
 
37
#include <drizzled/field/datetime.h>
 
38
 
 
39
#include CMATH_H
 
40
 
 
41
#if defined(CMATH_NAMESPACE)
 
42
using namespace CMATH_NAMESPACE;
 
43
#endif
 
44
 
 
45
extern my_decimal decimal_zero;
26
46
 
27
47
/**
28
48
  Prepare an aggregate function item for checking context conditions.
33
53
    If the set function is not allowed in any subquery where it occurs
34
54
    an error is reported immediately.
35
55
 
36
 
  @param thd      reference to the thread context info
 
56
  @param session      reference to the thread context info
37
57
 
38
58
  @note
39
59
    This function is to be called for any item created for a set function
45
65
  @retval
46
66
    FALSE  otherwise
47
67
*/
48
 
 
49
 
bool Item_sum::init_sum_func_check(THD *thd)
 
68
 
 
69
bool Item_sum::init_sum_func_check(Session *session)
50
70
{
51
 
  if (!thd->lex->allow_sum_func)
 
71
  if (!session->lex->allow_sum_func)
52
72
  {
53
73
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
54
74
               MYF(0));
55
75
    return true;
56
76
  }
57
77
  /* Set a reference to the nesting set function if there is  any */
58
 
  in_sum_func= thd->lex->in_sum_func;
 
78
  in_sum_func= session->lex->in_sum_func;
59
79
  /* Save a pointer to object to be used in items for nested set functions */
60
 
  thd->lex->in_sum_func= this;
61
 
  nest_level= thd->lex->current_select->nest_level;
 
80
  session->lex->in_sum_func= this;
 
81
  nest_level= session->lex->current_select->nest_level;
62
82
  ref_by= 0;
63
83
  aggr_level= -1;
64
84
  aggr_sel= NULL;
86
106
    conditions. They are specified in the comment before the Item_sum
87
107
    class declaration.
88
108
    Additionally a bitmap variable called allow_sum_func is employed.
89
 
    It is included into the thd->lex structure.
 
109
    It is included into the session->lex structure.
90
110
    The bitmap contains 1 at n-th position if the set function happens
91
111
    to occur under a construct of the n-th level subquery where usage
92
112
    of set functions are allowed (i.e either in the SELECT list or
97
117
         HAVING t1.a IN (SELECT t2.c FROM t2 WHERE AVG(t1.b) > 20) AND
98
118
                t1.a > (SELECT MIN(t2.d) FROM t2);
99
119
    @endcode
100
 
    allow_sum_func will contain: 
101
 
    - for SUM(t1.b) - 1 at the first position 
 
120
    allow_sum_func will contain:
 
121
    - for SUM(t1.b) - 1 at the first position
102
122
    - for AVG(t1.b) - 1 at the first position, 0 at the second position
103
123
    - for MIN(t2.d) - 1 at the first position, 1 at the second position.
104
124
 
105
 
  @param thd  reference to the thread context info
 
125
  @param session  reference to the thread context info
106
126
  @param ref  location of the pointer to this item in the embedding expression
107
127
 
108
128
  @note
116
136
  @retval
117
137
    FALSE  otherwise
118
138
*/
119
 
 
120
 
bool Item_sum::check_sum_func(THD *thd, Item **ref)
 
139
 
 
140
bool Item_sum::check_sum_func(Session *session, Item **ref)
121
141
{
122
142
  bool invalid= false;
123
 
  nesting_map allow_sum_func= thd->lex->allow_sum_func;
124
 
  /*  
 
143
  nesting_map allow_sum_func= session->lex->allow_sum_func;
 
144
  /*
125
145
    The value of max_arg_level is updated if an argument of the set function
126
146
    contains a column reference resolved  against a subquery whose level is
127
147
    greater than the current value of max_arg_level.
128
148
    max_arg_level cannot be greater than nest level.
129
 
    nest level is always >= 0  
130
 
  */ 
 
149
    nest level is always >= 0
 
150
  */
131
151
  if (nest_level == max_arg_level)
132
152
  {
133
153
    /*
134
 
      The function must be aggregated in the current subquery, 
135
 
      If it is there under a construct where it is not allowed 
136
 
      we report an error. 
137
 
    */ 
 
154
      The function must be aggregated in the current subquery,
 
155
      If it is there under a construct where it is not allowed
 
156
      we report an error.
 
157
    */
138
158
    invalid= !(allow_sum_func & (1 << max_arg_level));
139
159
  }
140
160
  else if (max_arg_level >= 0 || !(allow_sum_func & (1 << nest_level)))
144
164
      Try to find a subquery where it can be aggregated;
145
165
      If we fail to find such a subquery report an error.
146
166
    */
147
 
    if (register_sum_func(thd, ref))
 
167
    if (register_sum_func(session, ref))
148
168
      return true;
149
169
    invalid= aggr_level < 0 && !(allow_sum_func & (1 << nest_level));
150
170
    if (!invalid && false)
153
173
  if (!invalid && aggr_level < 0)
154
174
  {
155
175
    aggr_level= nest_level;
156
 
    aggr_sel= thd->lex->current_select;
 
176
    aggr_sel= session->lex->current_select;
157
177
  }
158
178
  /*
159
179
    By this moment we either found a subquery where the set function is
160
180
    to be aggregated  and assigned a value that is  >= 0 to aggr_level,
161
 
    or set the value of 'invalid' to TRUE to report later an error. 
 
181
    or set the value of 'invalid' to TRUE to report later an error.
162
182
  */
163
 
  /* 
 
183
  /*
164
184
    Additionally we have to check whether possible nested set functions
165
185
    are acceptable here: they are not, if the level of aggregation of
166
186
    some of them is less than aggr_level.
167
187
  */
168
 
  if (!invalid) 
 
188
  if (!invalid)
169
189
    invalid= aggr_level <= max_sum_func_level;
170
 
  if (invalid)  
 
190
  if (invalid)
171
191
  {
172
192
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
173
193
               MYF(0));
179
199
    /*
180
200
      If the set function is nested adjust the value of
181
201
      max_sum_func_level for the nesting set function.
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 
 
202
      We take into account only enclosed set functions that are to be
 
203
      aggregated on the same level or above of the nest level of
184
204
      the enclosing set function.
185
205
      But we must always pass up the max_sum_func_level because it is
186
206
      the maximum nested level of all directly and indirectly enclosed
257
277
  }
258
278
  aggr_sel->full_group_by_flag|= SUM_FUNC_USED;
259
279
  update_used_tables();
260
 
  thd->lex->in_sum_func= in_sum_func;
 
280
  session->lex->in_sum_func= in_sum_func;
261
281
  return false;
262
282
}
263
283
 
268
288
    aggregated. If it finds such a subquery then aggr_level is set to
269
289
    the nest level of this subquery and the item for the set function
270
290
    is added to the list of set functions used in nested subqueries
271
 
    inner_sum_func_list defined for each subquery. When the item is placed 
 
291
    inner_sum_func_list defined for each subquery. When the item is placed
272
292
    there the field 'ref_by' is set to ref.
273
293
 
274
294
  @note
277
297
    a subquery in one chain. It would simplify the process of 'splitting'
278
298
    for set functions.
279
299
 
280
 
  @param thd  reference to the thread context info
 
300
  @param session  reference to the thread context info
281
301
  @param ref  location of the pointer to this item in the embedding expression
282
302
 
283
303
  @retval
284
304
    FALSE  if the executes without failures (currently always)
285
305
  @retval
286
306
    TRUE   otherwise
287
 
*/  
 
307
*/
288
308
 
289
 
bool Item_sum::register_sum_func(THD *thd, Item **ref)
 
309
bool Item_sum::register_sum_func(Session *session, Item **ref)
290
310
{
291
311
  SELECT_LEX *sl;
292
 
  nesting_map allow_sum_func= thd->lex->allow_sum_func;
293
 
  for (sl= thd->lex->current_select->master_unit()->outer_select() ;
 
312
  nesting_map allow_sum_func= session->lex->allow_sum_func;
 
313
  for (sl= session->lex->current_select->master_unit()->outer_select() ;
294
314
       sl && sl->nest_level > max_arg_level;
295
315
       sl= sl->master_unit()->outer_select() )
296
316
  {
303
323
  }
304
324
  if (sl && (allow_sum_func & (1 << sl->nest_level)))
305
325
  {
306
 
    /* 
 
326
    /*
307
327
      We reached the subquery of level max_arg_level and checked
308
 
      that the function can be aggregated here. 
 
328
      that the function can be aggregated here.
309
329
      The set function will be aggregated in this subquery.
310
 
    */   
 
330
    */
311
331
    aggr_level= sl->nest_level;
312
332
    aggr_sel= sl;
313
333
 
326
346
    aggr_sel->inner_sum_func_list= this;
327
347
    aggr_sel->with_sum_func= 1;
328
348
 
329
 
    /* 
 
349
    /*
330
350
      Mark Item_subselect(s) as containing aggregate function all the way up
331
351
      to aggregate function's calculation context.
332
352
      Note that we must not mark the Item of calculation context itself
333
353
      because with_sum_func on the calculation context st_select_lex is
334
354
      already set above.
335
355
 
336
 
      with_sum_func being set for an Item means that this Item refers 
 
356
      with_sum_func being set for an Item means that this Item refers
337
357
      (somewhere in it, e.g. one of its arguments if it's a function) directly
338
358
      or through intermediate items to an aggregate function that is calculated
339
359
      in a context "outside" of the Item (e.g. in the current or outer select).
341
361
      with_sum_func being set for an st_select_lex means that this st_select_lex
342
362
      has aggregate functions directly referenced (i.e. not through a sub-select).
343
363
    */
344
 
    for (sl= thd->lex->current_select; 
 
364
    for (sl= session->lex->current_select;
345
365
         sl && sl != aggr_sel && sl->master_unit()->item;
346
366
         sl= sl->master_unit()->outer_select() )
347
367
      sl->master_unit()->item->with_sum_func= 1;
348
368
  }
349
 
  thd->lex->current_select->mark_as_dependent(aggr_sel);
 
369
  session->lex->current_select->mark_as_dependent(aggr_sel);
350
370
  return false;
351
371
}
352
372
 
353
373
 
354
 
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements), 
 
374
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements),
355
375
  forced_const(false)
356
376
{
357
377
  if ((args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
374
394
  Constructor used in processing select with temporary tebles.
375
395
*/
376
396
 
377
 
Item_sum::Item_sum(THD *thd, Item_sum *item):
378
 
  Item_result_field(thd, item), arg_count(item->arg_count),
 
397
Item_sum::Item_sum(Session *session, Item_sum *item):
 
398
  Item_result_field(session, item), arg_count(item->arg_count),
379
399
  aggr_sel(item->aggr_sel),
380
400
  nest_level(item->nest_level), aggr_level(item->aggr_level),
381
401
  quick_group(item->quick_group), used_tables_cache(item->used_tables_cache),
382
 
  forced_const(item->forced_const) 
 
402
  forced_const(item->forced_const)
383
403
{
384
404
  if (arg_count <= 2)
385
405
    args=tmp_args;
386
406
  else
387
 
    if (!(args= (Item**) thd->alloc(sizeof(Item*)*arg_count)))
 
407
    if (!(args= (Item**) session->alloc(sizeof(Item*)*arg_count)))
388
408
      return;
389
409
  memcpy(args, item->args, sizeof(Item*)*arg_count);
390
410
}
392
412
 
393
413
void Item_sum::mark_as_sum_func()
394
414
{
395
 
  SELECT_LEX *cur_select= current_thd->lex->current_select;
 
415
  SELECT_LEX *cur_select= current_session->lex->current_select;
396
416
  cur_select->n_sum_items++;
397
417
  cur_select->with_sum_func= 1;
398
418
  with_sum_func= 1;
439
459
  max_length=float_length(decimals);
440
460
}
441
461
 
442
 
Item *Item_sum::get_tmp_table_item(THD *thd)
 
462
Item *Item_sum::get_tmp_table_item(Session *session)
443
463
{
444
 
  Item_sum* sum_item= (Item_sum *) copy_or_same(thd);
 
464
  Item_sum* sum_item= (Item_sum *) copy_or_same(session);
445
465
  if (sum_item && sum_item->result_field)          // If not a const sum func
446
466
  {
447
467
    Field *result_field_tmp= sum_item->result_field;
539
559
}
540
560
 
541
561
 
 
562
int64_t Item_sum_num::val_int()
 
563
{
 
564
  assert(fixed == 1);
 
565
  return (int64_t) rint(val_real());             /* Real as default */
 
566
}
 
567
 
 
568
 
542
569
my_decimal *Item_sum_num::val_decimal(my_decimal *decimal_value)
543
570
{
544
571
  return val_decimal_from_real(decimal_value);
559
586
 
560
587
 
561
588
bool
562
 
Item_sum_num::fix_fields(THD *thd, Item **ref)
 
589
Item_sum_num::fix_fields(Session *session, Item **ref)
563
590
{
564
591
  assert(fixed == 0);
565
592
 
566
 
  if (init_sum_func_check(thd))
 
593
  if (init_sum_func_check(session))
567
594
    return true;
568
595
 
569
596
  decimals=0;
570
597
  maybe_null=0;
571
598
  for (uint32_t i=0 ; i < arg_count ; i++)
572
599
  {
573
 
    if (args[i]->fix_fields(thd, args + i) || args[i]->check_cols(1))
 
600
    if (args[i]->fix_fields(session, args + i) || args[i]->check_cols(1))
574
601
      return true;
575
602
    set_if_bigger(decimals, args[i]->decimals);
576
603
    maybe_null |= args[i]->maybe_null;
580
607
  null_value=1;
581
608
  fix_length_and_dec();
582
609
 
583
 
  if (check_sum_func(thd, ref))
 
610
  if (check_sum_func(session, ref))
584
611
    return true;
585
612
 
586
613
  fixed= 1;
588
615
}
589
616
 
590
617
 
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),
 
618
Item_sum_hybrid::Item_sum_hybrid(Session *session, Item_sum_hybrid *item)
 
619
  :Item_sum(session, item), value(item->value), hybrid_type(item->hybrid_type),
593
620
  hybrid_field_type(item->hybrid_field_type), cmp_sign(item->cmp_sign),
594
621
  was_values(item->was_values)
595
622
{
618
645
}
619
646
 
620
647
bool
621
 
Item_sum_hybrid::fix_fields(THD *thd, Item **ref)
 
648
Item_sum_hybrid::fix_fields(Session *session, Item **ref)
622
649
{
623
650
  assert(fixed == 0);
624
651
 
625
652
  Item *item= args[0];
626
653
 
627
 
  if (init_sum_func_check(thd))
 
654
  if (init_sum_func_check(session))
628
655
    return true;
629
656
 
630
657
  // 'item' can be changed during fix_fields
631
 
  if ((!item->fixed && item->fix_fields(thd, args)) ||
 
658
  if ((!item->fixed && item->fix_fields(session, args)) ||
632
659
      (item= args[0])->check_cols(1))
633
660
    return true;
634
661
  decimals=item->decimals;
666
693
  else
667
694
    hybrid_field_type= Item::field_type();
668
695
 
669
 
  if (check_sum_func(thd, ref))
 
696
  if (check_sum_func(session, ref))
670
697
    return true;
671
698
 
672
699
  fixed= 1;
680
707
  if (args[0]->type() == Item::FIELD_ITEM)
681
708
  {
682
709
    field= ((Item_field*) args[0])->field;
683
 
    
684
 
    if ((field= create_tmp_field_from_field(current_thd, field, name, table,
 
710
 
 
711
    if ((field= create_tmp_field_from_field(current_session, field, name, table,
685
712
                                            NULL, convert_blob_length)))
686
713
      field->flags&= ~NOT_NULL_FLAG;
687
714
    return field;
692
719
    fields creations separately.
693
720
  */
694
721
  switch (args[0]->field_type()) {
695
 
  case DRIZZLE_TYPE_NEWDATE:
696
 
    field= new Field_newdate(maybe_null, name, collation.collation);
 
722
  case DRIZZLE_TYPE_DATE:
 
723
    field= new Field_date(maybe_null, name, collation.collation);
697
724
    break;
698
725
  case DRIZZLE_TYPE_TIME:
699
726
    field= new Field_time(maybe_null, name, collation.collation);
719
746
  @todo
720
747
  check if the following assignments are really needed
721
748
*/
722
 
Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item) 
723
 
  :Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
 
749
Item_sum_sum::Item_sum_sum(Session *session, Item_sum_sum *item)
 
750
  :Item_sum_num(session, item), hybrid_type(item->hybrid_type),
724
751
   curr_dec_buff(item->curr_dec_buff)
725
752
{
726
753
  /* TODO: check if the following assignments are really needed */
733
760
    sum= item->sum;
734
761
}
735
762
 
736
 
Item *Item_sum_sum::copy_or_same(THD* thd)
 
763
Item *Item_sum_sum::copy_or_same(Session* session)
737
764
{
738
 
  return new (thd->mem_root) Item_sum_sum(thd, this);
 
765
  return new (session->mem_root) Item_sum_sum(session, this);
739
766
}
740
767
 
741
768
 
884
911
}
885
912
 
886
913
 
887
 
Item_sum_distinct::Item_sum_distinct(THD *thd, Item_sum_distinct *original)
888
 
  :Item_sum_num(thd, original), val(original->val), tree(0),
 
914
Item_sum_distinct::Item_sum_distinct(Session *session, Item_sum_distinct *original)
 
915
  :Item_sum_num(session, original), val(original->val), tree(0),
889
916
  table_field_type(original->table_field_type)
890
917
{
891
918
  quick_group= 0;
925
952
  return &fast_decimal_traits_instance;
926
953
}
927
954
 
 
955
 
928
956
void Item_sum_distinct::fix_length_and_dec()
929
957
{
930
958
  assert(args[0]->fixed);
946
974
    calculations. The range of int64 is enough to hold sum 2^32 distinct
947
975
    integers each <= 2^32.
948
976
  */
949
 
  if (table_field_type >= DRIZZLE_TYPE_TINY && table_field_type <= DRIZZLE_TYPE_LONG)
 
977
  if (table_field_type == DRIZZLE_TYPE_LONG)
950
978
  {
951
979
    val.traits= Hybrid_type_traits_fast_decimal::instance();
952
980
    break;
966
994
}
967
995
 
968
996
 
 
997
enum Item_result Item_sum_distinct::result_type () const
 
998
{
 
999
  return val.traits->type();
 
1000
}
 
1001
 
 
1002
 
969
1003
/**
970
1004
  @todo
971
1005
  check that the case of CHAR(0) works OK
972
1006
*/
973
 
bool Item_sum_distinct::setup(THD *thd)
 
1007
bool Item_sum_distinct::setup(Session *session)
974
1008
{
975
1009
  List<Create_field> field_list;
976
1010
  Create_field field_def;                              /* field definition */
995
1029
                               args[0]->decimals, args[0]->maybe_null,
996
1030
                               args[0]->unsigned_flag);
997
1031
 
998
 
  if (! (table= create_virtual_tmp_table(thd, field_list)))
 
1032
  if (! (table= create_virtual_tmp_table(session, field_list)))
999
1033
    return(true);
1000
1034
 
1001
1035
  /* XXX: check that the case of CHAR(0) works OK */
1008
1042
    are converted to binary representation as well.
1009
1043
  */
1010
1044
  tree= new Unique(simple_raw_key_cmp, &tree_key_length, tree_key_length,
1011
 
                   thd->variables.max_heap_table_size);
 
1045
                   session->variables.max_heap_table_size);
1012
1046
 
1013
1047
  is_evaluated= false;
1014
1048
  return(tree == 0);
1126
1160
Item_sum_avg_distinct::fix_length_and_dec()
1127
1161
{
1128
1162
  Item_sum_distinct::fix_length_and_dec();
1129
 
  prec_increment= current_thd->variables.div_precincrement;
 
1163
  prec_increment= current_session->variables.div_precincrement;
1130
1164
  /*
1131
1165
    AVG() will divide val by count. We need to reserve digits
1132
1166
    after decimal point as the result can be fractional.
1148
1182
}
1149
1183
 
1150
1184
 
1151
 
Item *Item_sum_count::copy_or_same(THD* thd)
 
1185
Item *Item_sum_count::copy_or_same(Session* session)
1152
1186
{
1153
 
  return new (thd->mem_root) Item_sum_count(thd, this);
 
1187
  return new (session->mem_root) Item_sum_count(session, this);
1154
1188
}
1155
1189
 
1156
1190
 
1189
1223
{
1190
1224
  Item_sum_sum::fix_length_and_dec();
1191
1225
  maybe_null=null_value=1;
1192
 
  prec_increment= current_thd->variables.div_precincrement;
 
1226
  prec_increment= current_session->variables.div_precincrement;
1193
1227
  if (hybrid_type == DECIMAL_RESULT)
1194
1228
  {
1195
1229
    int precision= args[0]->decimal_precision() + prec_increment;
1207
1241
}
1208
1242
 
1209
1243
 
1210
 
Item *Item_sum_avg::copy_or_same(THD* thd)
 
1244
Item *Item_sum_avg::copy_or_same(Session* session)
1211
1245
{
1212
 
  return new (thd->mem_root) Item_sum_avg(thd, this);
 
1246
  return new (session->mem_root) Item_sum_avg(session, this);
1213
1247
}
1214
1248
 
1215
1249
 
1267
1301
}
1268
1302
 
1269
1303
 
 
1304
int64_t Item_sum_avg::val_int()
 
1305
{
 
1306
  return (int64_t) rint(val_real());
 
1307
}
 
1308
 
 
1309
 
1270
1310
my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
1271
1311
{
1272
1312
  my_decimal sum_buff, cnt;
1312
1352
  return sqrt(nr);
1313
1353
}
1314
1354
 
1315
 
Item *Item_sum_std::copy_or_same(THD* thd)
 
1355
Item *Item_sum_std::copy_or_same(Session* session)
1316
1356
{
1317
 
  return new (thd->mem_root) Item_sum_std(thd, this);
 
1357
  return new (session->mem_root) Item_sum_std(session, this);
1318
1358
}
1319
1359
 
1320
1360
 
1339
1379
{
1340
1380
  *count += 1;
1341
1381
 
1342
 
  if (*count == 1) 
 
1382
  if (*count == 1)
1343
1383
  {
1344
1384
    *m= nr;
1345
1385
    *s= 0;
1366
1406
}
1367
1407
 
1368
1408
 
1369
 
Item_sum_variance::Item_sum_variance(THD *thd, Item_sum_variance *item):
1370
 
  Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
 
1409
Item_sum_variance::Item_sum_variance(Session *session, Item_sum_variance *item):
 
1410
  Item_sum_num(session, item), hybrid_type(item->hybrid_type),
1371
1411
    count(item->count), sample(item->sample),
1372
1412
    prec_increment(item->prec_increment)
1373
1413
{
1379
1419
void Item_sum_variance::fix_length_and_dec()
1380
1420
{
1381
1421
  maybe_null= null_value= 1;
1382
 
  prec_increment= current_thd->variables.div_precincrement;
 
1422
  prec_increment= current_session->variables.div_precincrement;
1383
1423
 
1384
1424
  /*
1385
1425
    According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1386
 
    aggregate function; paragraph 7h of Syntax Rules), "the declared 
 
1426
    aggregate function; paragraph 7h of Syntax Rules), "the declared
1387
1427
    type of the result is an implementation-defined aproximate numeric
1388
1428
    type.
1389
1429
  */
1412
1452
}
1413
1453
 
1414
1454
 
1415
 
Item *Item_sum_variance::copy_or_same(THD* thd)
 
1455
Item *Item_sum_variance::copy_or_same(Session* session)
1416
1456
{
1417
 
  return new (thd->mem_root) Item_sum_variance(thd, this);
 
1457
  return new (session->mem_root) Item_sum_variance(session, this);
1418
1458
}
1419
1459
 
1420
1460
 
1448
1488
 
1449
1489
void Item_sum_variance::clear()
1450
1490
{
1451
 
  count= 0; 
 
1491
  count= 0;
1452
1492
}
1453
1493
 
1454
1494
bool Item_sum_variance::add()
1455
1495
{
1456
 
  /* 
 
1496
  /*
1457
1497
    Why use a temporary variable?  We don't know if it is null until we
1458
1498
    evaluate it, which has the side-effect of setting null_value .
1459
1499
  */
1460
1500
  double nr= args[0]->val_real();
1461
 
  
 
1501
 
1462
1502
  if (!args[0]->null_value)
1463
1503
    variance_fp_recurrence_next(&recurrence_m, &recurrence_s, &count, nr);
1464
1504
  return 0;
1489
1529
}
1490
1530
 
1491
1531
 
 
1532
int64_t Item_sum_variance::val_int()
 
1533
{
 
1534
  /* can't be fix_fields()ed */
 
1535
  return (int64_t) rint(val_real());
 
1536
}
 
1537
 
 
1538
 
1492
1539
my_decimal *Item_sum_variance::val_decimal(my_decimal *dec_buf)
1493
1540
{
1494
1541
  assert(fixed == 1);
1579
1626
                             &end_not_used, &err_not_used) : 0.0);
1580
1627
  }
1581
1628
  case INT_RESULT:
1582
 
    if (unsigned_flag)
1583
 
      return uint64_t2double(sum_int);
1584
1629
    return (double) sum_int;
1585
1630
  case DECIMAL_RESULT:
1586
1631
    my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1694
1739
}
1695
1740
 
1696
1741
 
1697
 
Item *Item_sum_min::copy_or_same(THD* thd)
 
1742
Item *Item_sum_min::copy_or_same(Session* session)
1698
1743
{
1699
 
  return new (thd->mem_root) Item_sum_min(thd, this);
 
1744
  return new (session->mem_root) Item_sum_min(session, this);
1700
1745
}
1701
1746
 
1702
1747
 
1718
1763
  {
1719
1764
    int64_t nr=args[0]->val_int();
1720
1765
    if (!args[0]->null_value && (null_value ||
1721
 
                                 (unsigned_flag && 
 
1766
                                 (unsigned_flag &&
1722
1767
                                  (uint64_t) nr < (uint64_t) sum_int) ||
1723
1768
                                 (!unsigned_flag && nr < sum_int)))
1724
1769
    {
1758
1803
}
1759
1804
 
1760
1805
 
1761
 
Item *Item_sum_max::copy_or_same(THD* thd)
 
1806
Item *Item_sum_max::copy_or_same(Session* session)
1762
1807
{
1763
 
  return new (thd->mem_root) Item_sum_max(thd, this);
 
1808
  return new (session->mem_root) Item_sum_max(session, this);
1764
1809
}
1765
1810
 
1766
1811
 
1782
1827
  {
1783
1828
    int64_t nr=args[0]->val_int();
1784
1829
    if (!args[0]->null_value && (null_value ||
1785
 
                                 (unsigned_flag && 
 
1830
                                 (unsigned_flag &&
1786
1831
                                  (uint64_t) nr > (uint64_t) sum_int) ||
1787
1832
                                 (!unsigned_flag && nr > sum_int)))
1788
1833
    {
1836
1881
  bits= reset_bits;
1837
1882
}
1838
1883
 
1839
 
Item *Item_sum_or::copy_or_same(THD* thd)
 
1884
Item *Item_sum_or::copy_or_same(Session* session)
1840
1885
{
1841
 
  return new (thd->mem_root) Item_sum_or(thd, this);
 
1886
  return new (session->mem_root) Item_sum_or(session, this);
1842
1887
}
1843
1888
 
1844
1889
 
1850
1895
  return 0;
1851
1896
}
1852
1897
 
1853
 
Item *Item_sum_xor::copy_or_same(THD* thd)
 
1898
Item *Item_sum_xor::copy_or_same(Session* session)
1854
1899
{
1855
 
  return new (thd->mem_root) Item_sum_xor(thd, this);
 
1900
  return new (session->mem_root) Item_sum_xor(session, this);
1856
1901
}
1857
1902
 
1858
1903
 
1864
1909
  return 0;
1865
1910
}
1866
1911
 
1867
 
Item *Item_sum_and::copy_or_same(THD* thd)
 
1912
Item *Item_sum_and::copy_or_same(Session* session)
1868
1913
{
1869
 
  return new (thd->mem_root) Item_sum_and(thd, this);
 
1914
  return new (session->mem_root) Item_sum_and(session, this);
1870
1915
}
1871
1916
 
1872
1917
 
2407
2452
}
2408
2453
 
2409
2454
 
 
2455
int64_t Item_variance_field::val_int()
 
2456
{
 
2457
  /* can't be fix_fields()ed */
 
2458
  return (int64_t) rint(val_real());
 
2459
}
 
2460
 
 
2461
 
2410
2462
double Item_variance_field::val_real()
2411
2463
{
2412
2464
  // fix_fields() never calls for this Item
2530
2582
}
2531
2583
 
2532
2584
 
2533
 
bool Item_sum_count_distinct::setup(THD *thd)
 
2585
bool Item_sum_count_distinct::setup(Session *session)
2534
2586
{
2535
2587
  List<Item> list;
2536
 
  SELECT_LEX *select_lex= thd->lex->current_select;
 
2588
  SELECT_LEX *select_lex= session->lex->current_select;
2537
2589
 
2538
2590
  /*
2539
2591
    Setup can be called twice for ROLLUP items. This is a bug.
2561
2613
  tmp_table_param->force_copy_fields= force_copy_fields;
2562
2614
  assert(table == 0);
2563
2615
 
2564
 
  if (!(table= create_tmp_table(thd, tmp_table_param, list, (order_st*) 0, 1,
 
2616
  if (!(table= create_tmp_table(session, tmp_table_param, list, (order_st*) 0, 1,
2565
2617
                                0,
2566
 
                                (select_lex->options | thd->options),
 
2618
                                (select_lex->options | session->options),
2567
2619
                                HA_POS_ERROR, (char*)"")))
2568
2620
    return true;
2569
2621
  table->file->extra(HA_EXTRA_NO_ROWS);         // Don't update rows
2616
2668
        uint32_t *length;
2617
2669
        compare_key= (qsort_cmp2) composite_key_cmp;
2618
2670
        cmp_arg= (void*) this;
2619
 
        field_lengths= (uint32_t*) thd->alloc(table->s->fields * sizeof(uint32_t));
 
2671
        field_lengths= (uint32_t*) session->alloc(table->s->fields * sizeof(uint32_t));
2620
2672
        for (tree_key_length= 0, length= field_lengths, field= table->field;
2621
2673
             field < field_end; ++field, ++length)
2622
2674
        {
2627
2679
    }
2628
2680
    assert(tree == 0);
2629
2681
    tree= new Unique(compare_key, cmp_arg, tree_key_length,
2630
 
                     thd->variables.max_heap_table_size);
 
2682
                     session->variables.max_heap_table_size);
2631
2683
    /*
2632
2684
      The only time tree_key_length could be 0 is if someone does
2633
2685
      count(distinct) on a char(0) field - stupid thing to do,
2642
2694
}
2643
2695
 
2644
2696
 
2645
 
Item *Item_sum_count_distinct::copy_or_same(THD* thd) 
 
2697
Item *Item_sum_count_distinct::copy_or_same(Session* session)
2646
2698
{
2647
 
  return new (thd->mem_root) Item_sum_count_distinct(thd, this);
 
2699
  return new (session->mem_root) Item_sum_count_distinct(session, this);
2648
2700
}
2649
2701
 
2650
2702
 
2737
2789
*****************************************************************************/
2738
2790
 
2739
2791
 
2740
 
/** 
 
2792
/**
2741
2793
  Compares the values for fields in expr list of GROUP_CONCAT.
2742
2794
  @note
2743
 
       
 
2795
 
2744
2796
     GROUP_CONCAT([DISTINCT] expr [,expr ...]
2745
2797
              [order_st BY {unsigned_integer | col_name | expr}
2746
2798
                  [ASC | DESC] [,col_name ...]]
2747
2799
              [SEPARATOR str_val])
2748
 
 
 
2800
 
2749
2801
  @return
2750
 
  @retval -1 : key1 < key2 
 
2802
  @retval -1 : key1 < key2
2751
2803
  @retval  0 : key1 = key2
2752
 
  @retval  1 : key1 > key2 
 
2804
  @retval  1 : key1 > key2
2753
2805
*/
2754
2806
 
2755
 
int group_concat_key_cmp_with_distinct(void* arg, const void* key1, 
 
2807
int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
2756
2808
                                       const void* key2)
2757
2809
{
2758
2810
  Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
2761
2813
  for (uint32_t i= 0; i < item_func->arg_count_field; i++)
2762
2814
  {
2763
2815
    Item *item= item_func->args[i];
2764
 
    /* 
 
2816
    /*
2765
2817
      If field_item is a const item then either get_tp_table_field returns 0
2766
 
      or it is an item over a const table. 
 
2818
      or it is an item over a const table.
2767
2819
    */
2768
2820
    if (item->const_item())
2769
2821
      continue;
2786
2838
  function of sort for syntax: GROUP_CONCAT(expr,... order_st BY col,... )
2787
2839
*/
2788
2840
 
2789
 
int group_concat_key_cmp_with_order(void* arg, const void* key1, 
 
2841
int group_concat_key_cmp_with_order(void* arg, const void* key1,
2790
2842
                                    const void* key2)
2791
2843
{
2792
2844
  Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2804
2856
      the temporary table, not the original field
2805
2857
    */
2806
2858
    Field *field= item->get_tmp_table_field();
2807
 
    /* 
 
2859
    /*
2808
2860
      If item is a const item then either get_tp_table_field returns 0
2809
 
      or it is an item over a const table. 
 
2861
      or it is an item over a const table.
2810
2862
    */
2811
2863
    if (field && !item->const_item())
2812
2864
    {
2960
3012
}
2961
3013
 
2962
3014
 
2963
 
Item_func_group_concat::Item_func_group_concat(THD *thd,
 
3015
Item_func_group_concat::Item_func_group_concat(Session *session,
2964
3016
                                               Item_func_group_concat *item)
2965
 
  :Item_sum(thd, item),
 
3017
  :Item_sum(session, item),
2966
3018
  tmp_table_param(item->tmp_table_param),
2967
3019
  warning(item->warning),
2968
3020
  separator(item->separator),
2995
3047
  {
2996
3048
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
2997
3049
    sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
2998
 
    warning->set_msg(current_thd, warn_buff);
 
3050
    warning->set_msg(current_session, warn_buff);
2999
3051
    warning= 0;
3000
3052
  }
3001
3053
 
3009
3061
    tmp_table_param= 0;
3010
3062
    if (table)
3011
3063
    {
3012
 
      THD *thd= table->in_use;
3013
 
      table->free_tmp_table(thd);
 
3064
      Session *session= table->in_use;
 
3065
      table->free_tmp_table(session);
3014
3066
      table= 0;
3015
3067
      if (tree)
3016
3068
      {
3026
3078
      {
3027
3079
        char warn_buff[DRIZZLE_ERRMSG_SIZE];
3028
3080
        sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3029
 
        warning->set_msg(thd, warn_buff);
 
3081
        warning->set_msg(session, warn_buff);
3030
3082
        warning= 0;
3031
3083
      }
3032
3084
    }
3036
3088
}
3037
3089
 
3038
3090
 
3039
 
Item *Item_func_group_concat::copy_or_same(THD* thd)
 
3091
Item *Item_func_group_concat::copy_or_same(Session* session)
3040
3092
{
3041
 
  return new (thd->mem_root) Item_func_group_concat(thd, this);
 
3093
  return new (session->mem_root) Item_func_group_concat(session, this);
3042
3094
}
3043
3095
 
3044
3096
 
3078
3130
  null_value= false;
3079
3131
  bool row_eligible= true;
3080
3132
 
3081
 
  if (distinct) 
 
3133
  if (distinct)
3082
3134
  {
3083
3135
    /* Filter out duplicate rows. */
3084
3136
    uint32_t count= unique_filter->elements_in_tree();
3105
3157
 
3106
3158
 
3107
3159
bool
3108
 
Item_func_group_concat::fix_fields(THD *thd, Item **ref)
 
3160
Item_func_group_concat::fix_fields(Session *session, Item **ref)
3109
3161
{
3110
3162
  uint32_t i;                       /* for loop variable */
3111
3163
  assert(fixed == 0);
3112
3164
 
3113
 
  if (init_sum_func_check(thd))
 
3165
  if (init_sum_func_check(session))
3114
3166
    return true;
3115
3167
 
3116
3168
  maybe_null= 1;
3122
3174
  for (i=0 ; i < arg_count ; i++)
3123
3175
  {
3124
3176
    if ((!args[i]->fixed &&
3125
 
         args[i]->fix_fields(thd, args + i)) ||
 
3177
         args[i]->fix_fields(session, args + i)) ||
3126
3178
        args[i]->check_cols(1))
3127
3179
      return true;
3128
3180
  }
3137
3189
  result.set_charset(collation.collation);
3138
3190
  result_field= 0;
3139
3191
  null_value= 1;
3140
 
  max_length= thd->variables.group_concat_max_len;
 
3192
  max_length= session->variables.group_concat_max_len;
3141
3193
 
3142
3194
  uint32_t offset;
3143
3195
  if (separator->needs_conversion(separator->length(), separator->charset(),
3148
3200
    char *buf;
3149
3201
    String *new_separator;
3150
3202
 
3151
 
    if (!(buf= (char*) thd->alloc(buflen)) ||
3152
 
        !(new_separator= new(thd->mem_root)
 
3203
    if (!(buf= (char*) session->alloc(buflen)) ||
 
3204
        !(new_separator= new(session->mem_root)
3153
3205
                           String(buf, buflen, collation.collation)))
3154
3206
      return true;
3155
 
    
 
3207
 
3156
3208
    conv_length= copy_and_convert(buf, buflen, collation.collation,
3157
3209
                                  separator->ptr(), separator->length(),
3158
3210
                                  separator->charset(), &errors);
3160
3212
    separator= new_separator;
3161
3213
  }
3162
3214
 
3163
 
  if (check_sum_func(thd, ref))
 
3215
  if (check_sum_func(session, ref))
3164
3216
    return true;
3165
3217
 
3166
3218
  fixed= 1;
3168
3220
}
3169
3221
 
3170
3222
 
3171
 
bool Item_func_group_concat::setup(THD *thd)
 
3223
bool Item_func_group_concat::setup(Session *session)
3172
3224
{
3173
3225
  List<Item> list;
3174
 
  SELECT_LEX *select_lex= thd->lex->current_select;
 
3226
  SELECT_LEX *select_lex= session->lex->current_select;
3175
3227
 
3176
3228
  /*
3177
3229
    Currently setup() can be called twice. Please add
3211
3263
    tmp table columns.
3212
3264
  */
3213
3265
  if (arg_count_order &&
3214
 
      setup_order(thd, args, context->table_list, list, all_fields, *order))
 
3266
      setup_order(session, args, context->table_list, list, all_fields, *order))
3215
3267
    return(true);
3216
3268
 
3217
3269
  count_field_types(select_lex, tmp_table_param, all_fields, 0);
3225
3277
      DISTINCT. This leads to truncation if the BLOB's size exceeds
3226
3278
      Field_varstring::MAX_SIZE.
3227
3279
    */
3228
 
    set_if_smaller(tmp_table_param->convert_blob_length, 
 
3280
    set_if_smaller(tmp_table_param->convert_blob_length,
3229
3281
                   Field_varstring::MAX_SIZE);
3230
3282
  }
3231
3283
 
3236
3288
    Note that in the table, we first have the order_st BY fields, then the
3237
3289
    field list.
3238
3290
  */
3239
 
  if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
 
3291
  if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3240
3292
                                (order_st*) 0, 0, true,
3241
 
                                (select_lex->options | thd->options),
 
3293
                                (select_lex->options | session->options),
3242
3294
                                HA_POS_ERROR, (char*) "")))
3243
3295
    return(true);
3244
3296
  table->file->extra(HA_EXTRA_NO_ROWS);
3259
3311
      syntax of this function). If there is no order_st BY clause, we don't
3260
3312
      create this tree.
3261
3313
    */
3262
 
    init_tree(tree, (uint) cmin(thd->variables.max_heap_table_size,
3263
 
                               thd->variables.sortbuff_size/16), 0,
3264
 
              tree_key_length, 
 
3314
    init_tree(tree, (uint) cmin(session->variables.max_heap_table_size,
 
3315
                               session->variables.sortbuff_size/16), 0,
 
3316
              tree_key_length,
3265
3317
              group_concat_key_cmp_with_order , 0, NULL, (void*) this);
3266
3318
  }
3267
3319
 
3269
3321
    unique_filter= new Unique(group_concat_key_cmp_with_distinct,
3270
3322
                              (void*)this,
3271
3323
                              tree_key_length,
3272
 
                              thd->variables.max_heap_table_size);
3273
 
  
 
3324
                              session->variables.max_heap_table_size);
 
3325
 
3274
3326
  return(false);
3275
3327
}
3276
3328
 
3345
3397
Item_func_group_concat::~Item_func_group_concat()
3346
3398
{
3347
3399
  if (!original && unique_filter)
3348
 
    delete unique_filter;    
 
3400
    delete unique_filter;
3349
3401
}