~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_sum.cc

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

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 CMATH_H
 
27
 
 
28
#if defined(CMATH_NAMESPACE)
 
29
using namespace CMATH_NAMESPACE;
 
30
#endif
 
31
 
 
32
extern my_decimal decimal_zero;
26
33
 
27
34
/**
28
35
  Prepare an aggregate function item for checking context conditions.
33
40
    If the set function is not allowed in any subquery where it occurs
34
41
    an error is reported immediately.
35
42
 
36
 
  @param thd      reference to the thread context info
 
43
  @param session      reference to the thread context info
37
44
 
38
45
  @note
39
46
    This function is to be called for any item created for a set function
46
53
    FALSE  otherwise
47
54
*/
48
55
 
49
 
bool Item_sum::init_sum_func_check(THD *thd)
 
56
bool Item_sum::init_sum_func_check(Session *session)
50
57
{
51
 
  if (!thd->lex->allow_sum_func)
 
58
  if (!session->lex->allow_sum_func)
52
59
  {
53
60
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
54
61
               MYF(0));
55
62
    return true;
56
63
  }
57
64
  /* Set a reference to the nesting set function if there is  any */
58
 
  in_sum_func= thd->lex->in_sum_func;
 
65
  in_sum_func= session->lex->in_sum_func;
59
66
  /* 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;
 
67
  session->lex->in_sum_func= this;
 
68
  nest_level= session->lex->current_select->nest_level;
62
69
  ref_by= 0;
63
70
  aggr_level= -1;
64
71
  aggr_sel= NULL;
86
93
    conditions. They are specified in the comment before the Item_sum
87
94
    class declaration.
88
95
    Additionally a bitmap variable called allow_sum_func is employed.
89
 
    It is included into the thd->lex structure.
 
96
    It is included into the session->lex structure.
90
97
    The bitmap contains 1 at n-th position if the set function happens
91
98
    to occur under a construct of the n-th level subquery where usage
92
99
    of set functions are allowed (i.e either in the SELECT list or
102
109
    - for AVG(t1.b) - 1 at the first position, 0 at the second position
103
110
    - for MIN(t2.d) - 1 at the first position, 1 at the second position.
104
111
 
105
 
  @param thd  reference to the thread context info
 
112
  @param session  reference to the thread context info
106
113
  @param ref  location of the pointer to this item in the embedding expression
107
114
 
108
115
  @note
117
124
    FALSE  otherwise
118
125
*/
119
126
 
120
 
bool Item_sum::check_sum_func(THD *thd, Item **ref)
 
127
bool Item_sum::check_sum_func(Session *session, Item **ref)
121
128
{
122
129
  bool invalid= false;
123
 
  nesting_map allow_sum_func= thd->lex->allow_sum_func;
 
130
  nesting_map allow_sum_func= session->lex->allow_sum_func;
124
131
  /*  
125
132
    The value of max_arg_level is updated if an argument of the set function
126
133
    contains a column reference resolved  against a subquery whose level is
144
151
      Try to find a subquery where it can be aggregated;
145
152
      If we fail to find such a subquery report an error.
146
153
    */
147
 
    if (register_sum_func(thd, ref))
 
154
    if (register_sum_func(session, ref))
148
155
      return true;
149
156
    invalid= aggr_level < 0 && !(allow_sum_func & (1 << nest_level));
150
157
    if (!invalid && false)
153
160
  if (!invalid && aggr_level < 0)
154
161
  {
155
162
    aggr_level= nest_level;
156
 
    aggr_sel= thd->lex->current_select;
 
163
    aggr_sel= session->lex->current_select;
157
164
  }
158
165
  /*
159
166
    By this moment we either found a subquery where the set function is
257
264
  }
258
265
  aggr_sel->full_group_by_flag|= SUM_FUNC_USED;
259
266
  update_used_tables();
260
 
  thd->lex->in_sum_func= in_sum_func;
 
267
  session->lex->in_sum_func= in_sum_func;
261
268
  return false;
262
269
}
263
270
 
277
284
    a subquery in one chain. It would simplify the process of 'splitting'
278
285
    for set functions.
279
286
 
280
 
  @param thd  reference to the thread context info
 
287
  @param session  reference to the thread context info
281
288
  @param ref  location of the pointer to this item in the embedding expression
282
289
 
283
290
  @retval
286
293
    TRUE   otherwise
287
294
*/  
288
295
 
289
 
bool Item_sum::register_sum_func(THD *thd, Item **ref)
 
296
bool Item_sum::register_sum_func(Session *session, Item **ref)
290
297
{
291
298
  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() ;
 
299
  nesting_map allow_sum_func= session->lex->allow_sum_func;
 
300
  for (sl= session->lex->current_select->master_unit()->outer_select() ;
294
301
       sl && sl->nest_level > max_arg_level;
295
302
       sl= sl->master_unit()->outer_select() )
296
303
  {
341
348
      with_sum_func being set for an st_select_lex means that this st_select_lex
342
349
      has aggregate functions directly referenced (i.e. not through a sub-select).
343
350
    */
344
 
    for (sl= thd->lex->current_select; 
 
351
    for (sl= session->lex->current_select; 
345
352
         sl && sl != aggr_sel && sl->master_unit()->item;
346
353
         sl= sl->master_unit()->outer_select() )
347
354
      sl->master_unit()->item->with_sum_func= 1;
348
355
  }
349
 
  thd->lex->current_select->mark_as_dependent(aggr_sel);
 
356
  session->lex->current_select->mark_as_dependent(aggr_sel);
350
357
  return false;
351
358
}
352
359
 
356
363
{
357
364
  if ((args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
358
365
  {
359
 
    uint i=0;
 
366
    uint32_t i=0;
360
367
    List_iterator_fast<Item> li(list);
361
368
    Item *item;
362
369
 
374
381
  Constructor used in processing select with temporary tebles.
375
382
*/
376
383
 
377
 
Item_sum::Item_sum(THD *thd, Item_sum *item):
378
 
  Item_result_field(thd, item), arg_count(item->arg_count),
 
384
Item_sum::Item_sum(Session *session, Item_sum *item):
 
385
  Item_result_field(session, item), arg_count(item->arg_count),
379
386
  aggr_sel(item->aggr_sel),
380
387
  nest_level(item->nest_level), aggr_level(item->aggr_level),
381
388
  quick_group(item->quick_group), used_tables_cache(item->used_tables_cache),
384
391
  if (arg_count <= 2)
385
392
    args=tmp_args;
386
393
  else
387
 
    if (!(args= (Item**) thd->alloc(sizeof(Item*)*arg_count)))
 
394
    if (!(args= (Item**) session->alloc(sizeof(Item*)*arg_count)))
388
395
      return;
389
396
  memcpy(args, item->args, sizeof(Item*)*arg_count);
390
397
}
392
399
 
393
400
void Item_sum::mark_as_sum_func()
394
401
{
395
 
  SELECT_LEX *cur_select= current_thd->lex->current_select;
 
402
  SELECT_LEX *cur_select= current_session->lex->current_select;
396
403
  cur_select->n_sum_items++;
397
404
  cur_select->with_sum_func= 1;
398
405
  with_sum_func= 1;
422
429
void Item_sum::print(String *str, enum_query_type query_type)
423
430
{
424
431
  str->append(func_name());
425
 
  for (uint i=0 ; i < arg_count ; i++)
 
432
  for (uint32_t i=0 ; i < arg_count ; i++)
426
433
  {
427
434
    if (i)
428
435
      str->append(',');
434
441
void Item_sum::fix_num_length_and_dec()
435
442
{
436
443
  decimals=0;
437
 
  for (uint i=0 ; i < arg_count ; i++)
 
444
  for (uint32_t i=0 ; i < arg_count ; i++)
438
445
    set_if_bigger(decimals,args[i]->decimals);
439
446
  max_length=float_length(decimals);
440
447
}
441
448
 
442
 
Item *Item_sum::get_tmp_table_item(THD *thd)
 
449
Item *Item_sum::get_tmp_table_item(Session *session)
443
450
{
444
 
  Item_sum* sum_item= (Item_sum *) copy_or_same(thd);
 
451
  Item_sum* sum_item= (Item_sum *) copy_or_same(session);
445
452
  if (sum_item && sum_item->result_field)          // If not a const sum func
446
453
  {
447
454
    Field *result_field_tmp= sum_item->result_field;
448
 
    for (uint i=0 ; i < sum_item->arg_count ; i++)
 
455
    for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
449
456
    {
450
457
      Item *arg= sum_item->args[i];
451
458
      if (!arg->const_item())
462
469
 
463
470
 
464
471
bool Item_sum::walk (Item_processor processor, bool walk_subquery,
465
 
                     uchar *argument)
 
472
                     unsigned char *argument)
466
473
{
467
474
  if (arg_count)
468
475
  {
479
486
 
480
487
Field *Item_sum::create_tmp_field(bool group __attribute__((unused)),
481
488
                                  Table *table,
482
 
                                  uint convert_blob_length)
 
489
                                  uint32_t convert_blob_length)
483
490
{
484
491
  Field *field;
485
492
  switch (result_type()) {
518
525
  if (!forced_const)
519
526
  {
520
527
    used_tables_cache= 0;
521
 
    for (uint i=0 ; i < arg_count ; i++)
 
528
    for (uint32_t i=0 ; i < arg_count ; i++)
522
529
    {
523
530
      args[i]->update_used_tables();
524
531
      used_tables_cache|= args[i]->used_tables();
539
546
}
540
547
 
541
548
 
 
549
int64_t Item_sum_num::val_int()
 
550
{
 
551
  assert(fixed == 1);
 
552
  return (int64_t) rint(val_real());             /* Real as default */
 
553
}
 
554
 
 
555
 
542
556
my_decimal *Item_sum_num::val_decimal(my_decimal *decimal_value)
543
557
{
544
558
  return val_decimal_from_real(decimal_value);
559
573
 
560
574
 
561
575
bool
562
 
Item_sum_num::fix_fields(THD *thd, Item **ref)
 
576
Item_sum_num::fix_fields(Session *session, Item **ref)
563
577
{
564
578
  assert(fixed == 0);
565
579
 
566
 
  if (init_sum_func_check(thd))
 
580
  if (init_sum_func_check(session))
567
581
    return true;
568
582
 
569
583
  decimals=0;
570
584
  maybe_null=0;
571
 
  for (uint i=0 ; i < arg_count ; i++)
 
585
  for (uint32_t i=0 ; i < arg_count ; i++)
572
586
  {
573
 
    if (args[i]->fix_fields(thd, args + i) || args[i]->check_cols(1))
 
587
    if (args[i]->fix_fields(session, args + i) || args[i]->check_cols(1))
574
588
      return true;
575
589
    set_if_bigger(decimals, args[i]->decimals);
576
590
    maybe_null |= args[i]->maybe_null;
580
594
  null_value=1;
581
595
  fix_length_and_dec();
582
596
 
583
 
  if (check_sum_func(thd, ref))
 
597
  if (check_sum_func(session, ref))
584
598
    return true;
585
599
 
586
600
  fixed= 1;
588
602
}
589
603
 
590
604
 
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),
 
605
Item_sum_hybrid::Item_sum_hybrid(Session *session, Item_sum_hybrid *item)
 
606
  :Item_sum(session, item), value(item->value), hybrid_type(item->hybrid_type),
593
607
  hybrid_field_type(item->hybrid_field_type), cmp_sign(item->cmp_sign),
594
608
  was_values(item->was_values)
595
609
{
618
632
}
619
633
 
620
634
bool
621
 
Item_sum_hybrid::fix_fields(THD *thd, Item **ref)
 
635
Item_sum_hybrid::fix_fields(Session *session, Item **ref)
622
636
{
623
637
  assert(fixed == 0);
624
638
 
625
639
  Item *item= args[0];
626
640
 
627
 
  if (init_sum_func_check(thd))
 
641
  if (init_sum_func_check(session))
628
642
    return true;
629
643
 
630
644
  // 'item' can be changed during fix_fields
631
 
  if ((!item->fixed && item->fix_fields(thd, args)) ||
 
645
  if ((!item->fixed && item->fix_fields(session, args)) ||
632
646
      (item= args[0])->check_cols(1))
633
647
    return true;
634
648
  decimals=item->decimals;
666
680
  else
667
681
    hybrid_field_type= Item::field_type();
668
682
 
669
 
  if (check_sum_func(thd, ref))
 
683
  if (check_sum_func(session, ref))
670
684
    return true;
671
685
 
672
686
  fixed= 1;
674
688
}
675
689
 
676
690
Field *Item_sum_hybrid::create_tmp_field(bool group, Table *table,
677
 
                                         uint convert_blob_length)
 
691
                                         uint32_t convert_blob_length)
678
692
{
679
693
  Field *field;
680
694
  if (args[0]->type() == Item::FIELD_ITEM)
681
695
  {
682
696
    field= ((Item_field*) args[0])->field;
683
697
    
684
 
    if ((field= create_tmp_field_from_field(current_thd, field, name, table,
 
698
    if ((field= create_tmp_field_from_field(current_session, field, name, table,
685
699
                                            NULL, convert_blob_length)))
686
700
      field->flags&= ~NOT_NULL_FLAG;
687
701
    return field;
719
733
  @todo
720
734
  check if the following assignments are really needed
721
735
*/
722
 
Item_sum_sum::Item_sum_sum(THD *thd, Item_sum_sum *item) 
723
 
  :Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
 
736
Item_sum_sum::Item_sum_sum(Session *session, Item_sum_sum *item) 
 
737
  :Item_sum_num(session, item), hybrid_type(item->hybrid_type),
724
738
   curr_dec_buff(item->curr_dec_buff)
725
739
{
726
740
  /* TODO: check if the following assignments are really needed */
733
747
    sum= item->sum;
734
748
}
735
749
 
736
 
Item *Item_sum_sum::copy_or_same(THD* thd)
 
750
Item *Item_sum_sum::copy_or_same(Session* session)
737
751
{
738
 
  return new (thd->mem_root) Item_sum_sum(thd, this);
 
752
  return new (session->mem_root) Item_sum_sum(session, this);
739
753
}
740
754
 
741
755
 
846
860
 
847
861
/***************************************************************************/
848
862
 
849
 
C_MODE_START
 
863
#ifdef __cplusplus
 
864
extern "C" {
 
865
#endif
850
866
 
851
867
/* Declarations for auxilary C-callbacks */
852
868
 
853
869
static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
854
870
{
855
 
    return memcmp(key1, key2, *(uint *) arg);
 
871
    return memcmp(key1, key2, *(uint32_t *) arg);
856
872
}
857
873
 
858
874
 
863
879
  return ((Item_sum_distinct*) (item))->unique_walk_function(element);
864
880
}
865
881
 
866
 
C_MODE_END
 
882
#ifdef __cplusplus
 
883
}
 
884
#endif
867
885
 
868
886
/* Item_sum_distinct */
869
887
 
880
898
}
881
899
 
882
900
 
883
 
Item_sum_distinct::Item_sum_distinct(THD *thd, Item_sum_distinct *original)
884
 
  :Item_sum_num(thd, original), val(original->val), tree(0),
 
901
Item_sum_distinct::Item_sum_distinct(Session *session, Item_sum_distinct *original)
 
902
  :Item_sum_num(session, original), val(original->val), tree(0),
885
903
  table_field_type(original->table_field_type)
886
904
{
887
905
  quick_group= 0;
942
960
    calculations. The range of int64 is enough to hold sum 2^32 distinct
943
961
    integers each <= 2^32.
944
962
  */
945
 
  if (table_field_type >= DRIZZLE_TYPE_TINY && table_field_type <= DRIZZLE_TYPE_LONG)
 
963
  if (table_field_type == DRIZZLE_TYPE_LONG)
946
964
  {
947
965
    val.traits= Hybrid_type_traits_fast_decimal::instance();
948
966
    break;
966
984
  @todo
967
985
  check that the case of CHAR(0) works OK
968
986
*/
969
 
bool Item_sum_distinct::setup(THD *thd)
 
987
bool Item_sum_distinct::setup(Session *session)
970
988
{
971
989
  List<Create_field> field_list;
972
990
  Create_field field_def;                              /* field definition */
991
1009
                               args[0]->decimals, args[0]->maybe_null,
992
1010
                               args[0]->unsigned_flag);
993
1011
 
994
 
  if (! (table= create_virtual_tmp_table(thd, field_list)))
 
1012
  if (! (table= create_virtual_tmp_table(session, field_list)))
995
1013
    return(true);
996
1014
 
997
1015
  /* XXX: check that the case of CHAR(0) works OK */
1004
1022
    are converted to binary representation as well.
1005
1023
  */
1006
1024
  tree= new Unique(simple_raw_key_cmp, &tree_key_length, tree_key_length,
1007
 
                   thd->variables.max_heap_table_size);
 
1025
                   session->variables.max_heap_table_size);
1008
1026
 
1009
1027
  is_evaluated= false;
1010
1028
  return(tree == 0);
1122
1140
Item_sum_avg_distinct::fix_length_and_dec()
1123
1141
{
1124
1142
  Item_sum_distinct::fix_length_and_dec();
1125
 
  prec_increment= current_thd->variables.div_precincrement;
 
1143
  prec_increment= current_session->variables.div_precincrement;
1126
1144
  /*
1127
1145
    AVG() will divide val by count. We need to reserve digits
1128
1146
    after decimal point as the result can be fractional.
1129
1147
  */
1130
 
  decimals= min(decimals + prec_increment, (unsigned int)NOT_FIXED_DEC);
 
1148
  decimals= cmin(decimals + prec_increment, (unsigned int)NOT_FIXED_DEC);
1131
1149
}
1132
1150
 
1133
1151
 
1144
1162
}
1145
1163
 
1146
1164
 
1147
 
Item *Item_sum_count::copy_or_same(THD* thd)
 
1165
Item *Item_sum_count::copy_or_same(Session* session)
1148
1166
{
1149
 
  return new (thd->mem_root) Item_sum_count(thd, this);
 
1167
  return new (session->mem_root) Item_sum_count(session, this);
1150
1168
}
1151
1169
 
1152
1170
 
1185
1203
{
1186
1204
  Item_sum_sum::fix_length_and_dec();
1187
1205
  maybe_null=null_value=1;
1188
 
  prec_increment= current_thd->variables.div_precincrement;
 
1206
  prec_increment= current_session->variables.div_precincrement;
1189
1207
  if (hybrid_type == DECIMAL_RESULT)
1190
1208
  {
1191
1209
    int precision= args[0]->decimal_precision() + prec_increment;
1192
 
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
 
1210
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1193
1211
    max_length= my_decimal_precision_to_length(precision, decimals,
1194
1212
                                               unsigned_flag);
1195
 
    f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
 
1213
    f_precision= cmin(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1196
1214
    f_scale=  args[0]->decimals;
1197
1215
    dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1198
1216
  }
1199
1217
  else {
1200
 
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
 
1218
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1201
1219
    max_length= args[0]->max_length + prec_increment;
1202
1220
  }
1203
1221
}
1204
1222
 
1205
1223
 
1206
 
Item *Item_sum_avg::copy_or_same(THD* thd)
 
1224
Item *Item_sum_avg::copy_or_same(Session* session)
1207
1225
{
1208
 
  return new (thd->mem_root) Item_sum_avg(thd, this);
 
1226
  return new (session->mem_root) Item_sum_avg(session, this);
1209
1227
}
1210
1228
 
1211
1229
 
1212
1230
Field *Item_sum_avg::create_tmp_field(bool group, Table *table,
1213
 
                                      uint convert_blob_len __attribute__((unused)))
 
1231
                                      uint32_t convert_blob_len __attribute__((unused)))
1214
1232
{
1215
1233
  Field *field;
1216
1234
  if (group)
1263
1281
}
1264
1282
 
1265
1283
 
 
1284
int64_t Item_sum_avg::val_int()
 
1285
{
 
1286
  return (int64_t) rint(val_real());
 
1287
}
 
1288
 
 
1289
 
1266
1290
my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
1267
1291
{
1268
1292
  my_decimal sum_buff, cnt;
1308
1332
  return sqrt(nr);
1309
1333
}
1310
1334
 
1311
 
Item *Item_sum_std::copy_or_same(THD* thd)
 
1335
Item *Item_sum_std::copy_or_same(Session* session)
1312
1336
{
1313
 
  return new (thd->mem_root) Item_sum_std(thd, this);
 
1337
  return new (session->mem_root) Item_sum_std(session, this);
1314
1338
}
1315
1339
 
1316
1340
 
1362
1386
}
1363
1387
 
1364
1388
 
1365
 
Item_sum_variance::Item_sum_variance(THD *thd, Item_sum_variance *item):
1366
 
  Item_sum_num(thd, item), hybrid_type(item->hybrid_type),
 
1389
Item_sum_variance::Item_sum_variance(Session *session, Item_sum_variance *item):
 
1390
  Item_sum_num(session, item), hybrid_type(item->hybrid_type),
1367
1391
    count(item->count), sample(item->sample),
1368
1392
    prec_increment(item->prec_increment)
1369
1393
{
1375
1399
void Item_sum_variance::fix_length_and_dec()
1376
1400
{
1377
1401
  maybe_null= null_value= 1;
1378
 
  prec_increment= current_thd->variables.div_precincrement;
 
1402
  prec_increment= current_session->variables.div_precincrement;
1379
1403
 
1380
1404
  /*
1381
1405
    According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1388
1412
  switch (args[0]->result_type()) {
1389
1413
  case REAL_RESULT:
1390
1414
  case STRING_RESULT:
1391
 
    decimals= min(args[0]->decimals + 4, NOT_FIXED_DEC);
 
1415
    decimals= cmin(args[0]->decimals + 4, NOT_FIXED_DEC);
1392
1416
    break;
1393
1417
  case INT_RESULT:
1394
1418
  case DECIMAL_RESULT:
1395
1419
  {
1396
1420
    int precision= args[0]->decimal_precision()*2 + prec_increment;
1397
 
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
 
1421
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1398
1422
    max_length= my_decimal_precision_to_length(precision, decimals,
1399
1423
                                               unsigned_flag);
1400
1424
 
1408
1432
}
1409
1433
 
1410
1434
 
1411
 
Item *Item_sum_variance::copy_or_same(THD* thd)
 
1435
Item *Item_sum_variance::copy_or_same(Session* session)
1412
1436
{
1413
 
  return new (thd->mem_root) Item_sum_variance(thd, this);
 
1437
  return new (session->mem_root) Item_sum_variance(session, this);
1414
1438
}
1415
1439
 
1416
1440
 
1420
1444
  pass around.
1421
1445
*/
1422
1446
Field *Item_sum_variance::create_tmp_field(bool group, Table *table,
1423
 
                                           uint convert_blob_len __attribute__((unused)))
 
1447
                                           uint32_t convert_blob_len __attribute__((unused)))
1424
1448
{
1425
1449
  Field *field;
1426
1450
  if (group)
1485
1509
}
1486
1510
 
1487
1511
 
 
1512
int64_t Item_sum_variance::val_int()
 
1513
{
 
1514
  /* can't be fix_fields()ed */
 
1515
  return (int64_t) rint(val_real());
 
1516
}
 
1517
 
 
1518
 
1488
1519
my_decimal *Item_sum_variance::val_decimal(my_decimal *dec_buf)
1489
1520
{
1490
1521
  assert(fixed == 1);
1495
1526
void Item_sum_variance::reset_field()
1496
1527
{
1497
1528
  double nr;
1498
 
  uchar *res= result_field->ptr;
 
1529
  unsigned char *res= result_field->ptr;
1499
1530
 
1500
1531
  nr= args[0]->val_real();              /* sets null_value as side-effect */
1501
1532
 
1518
1549
void Item_sum_variance::update_field()
1519
1550
{
1520
1551
  uint64_t field_count;
1521
 
  uchar *res=result_field->ptr;
 
1552
  unsigned char *res=result_field->ptr;
1522
1553
 
1523
1554
  double nr= args[0]->val_real();       /* sets null_value as side-effect */
1524
1555
 
1575
1606
                             &end_not_used, &err_not_used) : 0.0);
1576
1607
  }
1577
1608
  case INT_RESULT:
1578
 
    if (unsigned_flag)
1579
 
      return uint64_t2double(sum_int);
1580
1609
    return (double) sum_int;
1581
1610
  case DECIMAL_RESULT:
1582
1611
    my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1690
1719
}
1691
1720
 
1692
1721
 
1693
 
Item *Item_sum_min::copy_or_same(THD* thd)
 
1722
Item *Item_sum_min::copy_or_same(Session* session)
1694
1723
{
1695
 
  return new (thd->mem_root) Item_sum_min(thd, this);
 
1724
  return new (session->mem_root) Item_sum_min(session, this);
1696
1725
}
1697
1726
 
1698
1727
 
1754
1783
}
1755
1784
 
1756
1785
 
1757
 
Item *Item_sum_max::copy_or_same(THD* thd)
 
1786
Item *Item_sum_max::copy_or_same(Session* session)
1758
1787
{
1759
 
  return new (thd->mem_root) Item_sum_max(thd, this);
 
1788
  return new (session->mem_root) Item_sum_max(session, this);
1760
1789
}
1761
1790
 
1762
1791
 
1832
1861
  bits= reset_bits;
1833
1862
}
1834
1863
 
1835
 
Item *Item_sum_or::copy_or_same(THD* thd)
 
1864
Item *Item_sum_or::copy_or_same(Session* session)
1836
1865
{
1837
 
  return new (thd->mem_root) Item_sum_or(thd, this);
 
1866
  return new (session->mem_root) Item_sum_or(session, this);
1838
1867
}
1839
1868
 
1840
1869
 
1846
1875
  return 0;
1847
1876
}
1848
1877
 
1849
 
Item *Item_sum_xor::copy_or_same(THD* thd)
 
1878
Item *Item_sum_xor::copy_or_same(Session* session)
1850
1879
{
1851
 
  return new (thd->mem_root) Item_sum_xor(thd, this);
 
1880
  return new (session->mem_root) Item_sum_xor(session, this);
1852
1881
}
1853
1882
 
1854
1883
 
1860
1889
  return 0;
1861
1890
}
1862
1891
 
1863
 
Item *Item_sum_and::copy_or_same(THD* thd)
 
1892
Item *Item_sum_and::copy_or_same(Session* session)
1864
1893
{
1865
 
  return new (thd->mem_root) Item_sum_and(thd, this);
 
1894
  return new (session->mem_root) Item_sum_and(session, this);
1866
1895
}
1867
1896
 
1868
1897
 
1881
1910
void Item_sum_num::reset_field()
1882
1911
{
1883
1912
  double nr= args[0]->val_real();
1884
 
  uchar *res=result_field->ptr;
 
1913
  unsigned char *res=result_field->ptr;
1885
1914
 
1886
1915
  if (maybe_null)
1887
1916
  {
2003
2032
 
2004
2033
void Item_sum_count::reset_field()
2005
2034
{
2006
 
  uchar *res=result_field->ptr;
 
2035
  unsigned char *res=result_field->ptr;
2007
2036
  int64_t nr=0;
2008
2037
 
2009
2038
  if (!args[0]->maybe_null || !args[0]->is_null())
2014
2043
 
2015
2044
void Item_sum_avg::reset_field()
2016
2045
{
2017
 
  uchar *res=result_field->ptr;
 
2046
  unsigned char *res=result_field->ptr;
2018
2047
  if (hybrid_type == DECIMAL_RESULT)
2019
2048
  {
2020
2049
    int64_t tmp;
2055
2084
 
2056
2085
void Item_sum_bit::update_field()
2057
2086
{
2058
 
  uchar *res=result_field->ptr;
 
2087
  unsigned char *res=result_field->ptr;
2059
2088
  bits= uint8korr(res);
2060
2089
  add();
2061
2090
  int8store(res, bits);
2090
2119
  else
2091
2120
  {
2092
2121
    double old_nr,nr;
2093
 
    uchar *res=result_field->ptr;
 
2122
    unsigned char *res=result_field->ptr;
2094
2123
 
2095
2124
    float8get(old_nr,res);
2096
2125
    nr= args[0]->val_real();
2107
2136
void Item_sum_count::update_field()
2108
2137
{
2109
2138
  int64_t nr;
2110
 
  uchar *res=result_field->ptr;
 
2139
  unsigned char *res=result_field->ptr;
2111
2140
 
2112
2141
  nr=sint8korr(res);
2113
2142
  if (!args[0]->maybe_null || !args[0]->is_null())
2119
2148
void Item_sum_avg::update_field()
2120
2149
{
2121
2150
  int64_t field_count;
2122
 
  uchar *res=result_field->ptr;
 
2151
  unsigned char *res=result_field->ptr;
2123
2152
  if (hybrid_type == DECIMAL_RESULT)
2124
2153
  {
2125
2154
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2292
2321
  // fix_fields() never calls for this Item
2293
2322
  double nr;
2294
2323
  int64_t count;
2295
 
  uchar *res;
 
2324
  unsigned char *res;
2296
2325
 
2297
2326
  if (hybrid_type == DECIMAL_RESULT)
2298
2327
    return val_real_from_decimal();
2403
2432
}
2404
2433
 
2405
2434
 
 
2435
int64_t Item_variance_field::val_int()
 
2436
{
 
2437
  /* can't be fix_fields()ed */
 
2438
  return (int64_t) rint(val_real());
 
2439
}
 
2440
 
 
2441
 
2406
2442
double Item_variance_field::val_real()
2407
2443
{
2408
2444
  // fix_fields() never calls for this Item
2425
2461
** COUNT(DISTINCT ...)
2426
2462
****************************************************************************/
2427
2463
 
2428
 
int simple_str_key_cmp(void* arg, uchar* key1, uchar* key2)
 
2464
int simple_str_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2429
2465
{
2430
2466
  Field *f= (Field*) arg;
2431
2467
  return f->cmp(key1, key2);
2438
2474
  static
2439
2475
*/
2440
2476
 
2441
 
int composite_key_cmp(void* arg, uchar* key1, uchar* key2)
 
2477
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2442
2478
{
2443
2479
  Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2444
2480
  Field **field    = item->table->field;
2457
2493
  return 0;
2458
2494
}
2459
2495
 
2460
 
 
2461
 
C_MODE_START
 
2496
#ifdef __cplusplus
 
2497
extern "C" {
 
2498
#endif
2462
2499
 
2463
2500
static int count_distinct_walk(void *elem __attribute__((unused)),
2464
2501
                               element_count count __attribute__((unused)),
2468
2505
  return 0;
2469
2506
}
2470
2507
 
2471
 
C_MODE_END
 
2508
#ifdef __cplusplus
 
2509
}
 
2510
#endif
 
2511
 
2472
2512
 
2473
2513
 
2474
2514
void Item_sum_count_distinct::cleanup()
2522
2562
}
2523
2563
 
2524
2564
 
2525
 
bool Item_sum_count_distinct::setup(THD *thd)
 
2565
bool Item_sum_count_distinct::setup(Session *session)
2526
2566
{
2527
2567
  List<Item> list;
2528
 
  SELECT_LEX *select_lex= thd->lex->current_select;
 
2568
  SELECT_LEX *select_lex= session->lex->current_select;
2529
2569
 
2530
2570
  /*
2531
2571
    Setup can be called twice for ROLLUP items. This is a bug.
2539
2579
    return true;
2540
2580
 
2541
2581
  /* Create a table with an unique key over all parameters */
2542
 
  for (uint i=0; i < arg_count ; i++)
 
2582
  for (uint32_t i=0; i < arg_count ; i++)
2543
2583
  {
2544
2584
    Item *item=args[i];
2545
2585
    if (list.push_back(item))
2553
2593
  tmp_table_param->force_copy_fields= force_copy_fields;
2554
2594
  assert(table == 0);
2555
2595
 
2556
 
  if (!(table= create_tmp_table(thd, tmp_table_param, list, (order_st*) 0, 1,
 
2596
  if (!(table= create_tmp_table(session, tmp_table_param, list, (order_st*) 0, 1,
2557
2597
                                0,
2558
 
                                (select_lex->options | thd->options),
 
2598
                                (select_lex->options | session->options),
2559
2599
                                HA_POS_ERROR, (char*)"")))
2560
2600
    return true;
2561
2601
  table->file->extra(HA_EXTRA_NO_ROWS);         // Don't update rows
2608
2648
        uint32_t *length;
2609
2649
        compare_key= (qsort_cmp2) composite_key_cmp;
2610
2650
        cmp_arg= (void*) this;
2611
 
        field_lengths= (uint32_t*) thd->alloc(table->s->fields * sizeof(uint32_t));
 
2651
        field_lengths= (uint32_t*) session->alloc(table->s->fields * sizeof(uint32_t));
2612
2652
        for (tree_key_length= 0, length= field_lengths, field= table->field;
2613
2653
             field < field_end; ++field, ++length)
2614
2654
        {
2619
2659
    }
2620
2660
    assert(tree == 0);
2621
2661
    tree= new Unique(compare_key, cmp_arg, tree_key_length,
2622
 
                     thd->variables.max_heap_table_size);
 
2662
                     session->variables.max_heap_table_size);
2623
2663
    /*
2624
2664
      The only time tree_key_length could be 0 is if someone does
2625
2665
      count(distinct) on a char(0) field - stupid thing to do,
2634
2674
}
2635
2675
 
2636
2676
 
2637
 
Item *Item_sum_count_distinct::copy_or_same(THD* thd) 
 
2677
Item *Item_sum_count_distinct::copy_or_same(Session* session) 
2638
2678
{
2639
 
  return new (thd->mem_root) Item_sum_count_distinct(thd, this);
 
2679
  return new (session->mem_root) Item_sum_count_distinct(session, this);
2640
2680
}
2641
2681
 
2642
2682
 
2691
2731
  int error;
2692
2732
  assert(fixed == 1);
2693
2733
  if (!table)                                   // Empty query
2694
 
    return 0LL;
 
2734
    return 0L;
2695
2735
  if (tree)
2696
2736
  {
2697
2737
    if (is_evaluated)
2715
2755
  return table->file->stats.records;
2716
2756
}
2717
2757
 
2718
 
 
2719
 
/****************************************************************************
2720
 
** Functions to handle dynamic loadable aggregates
2721
 
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2722
 
** Adapted for UDAs by: Andreas F. Bobak <bobak@relog.ch>.
2723
 
** Rewritten by: Monty.
2724
 
****************************************************************************/
2725
 
 
2726
 
void Item_udf_sum::clear()
2727
 
{
2728
 
  udf.clear();
2729
 
  return;
2730
 
}
2731
 
 
2732
 
bool Item_udf_sum::add()
2733
 
{
2734
 
  udf.add(&null_value);
2735
 
  return(0);
2736
 
}
2737
 
 
2738
 
void Item_udf_sum::cleanup()
2739
 
{
2740
 
  /*
2741
 
    udf_handler::cleanup() nicely handles case when we have not
2742
 
    original item but one created by copy_or_same() method.
2743
 
  */
2744
 
  udf.cleanup();
2745
 
  Item_sum::cleanup();
2746
 
}
2747
 
 
2748
 
 
2749
 
void Item_udf_sum::print(String *str, enum_query_type query_type)
2750
 
{
2751
 
  str->append(func_name());
2752
 
  str->append('(');
2753
 
  for (uint i=0 ; i < arg_count ; i++)
2754
 
  {
2755
 
    if (i)
2756
 
      str->append(',');
2757
 
    args[i]->print(str, query_type);
2758
 
  }
2759
 
  str->append(')');
2760
 
}
2761
 
 
2762
 
 
2763
 
Item *Item_sum_udf_float::copy_or_same(THD* thd)
2764
 
{
2765
 
  return new (thd->mem_root) Item_sum_udf_float(thd, this);
2766
 
}
2767
 
 
2768
 
double Item_sum_udf_float::val_real()
2769
 
{
2770
 
  assert(fixed == 1);
2771
 
  return(udf.val(&null_value));
2772
 
}
2773
 
 
2774
 
 
2775
 
String *Item_sum_udf_float::val_str(String *str)
2776
 
{
2777
 
  return val_string_from_real(str);
2778
 
}
2779
 
 
2780
 
 
2781
 
my_decimal *Item_sum_udf_float::val_decimal(my_decimal *dec)
2782
 
{
2783
 
  return val_decimal_from_real(dec);
2784
 
}
2785
 
 
2786
 
 
2787
 
String *Item_sum_udf_decimal::val_str(String *str)
2788
 
{
2789
 
  return val_string_from_decimal(str);
2790
 
}
2791
 
 
2792
 
 
2793
 
double Item_sum_udf_decimal::val_real()
2794
 
{
2795
 
  return val_real_from_decimal();
2796
 
}
2797
 
 
2798
 
 
2799
 
int64_t Item_sum_udf_decimal::val_int()
2800
 
{
2801
 
  return val_int_from_decimal();
2802
 
}
2803
 
 
2804
 
 
2805
 
my_decimal *Item_sum_udf_decimal::val_decimal(my_decimal *dec_buf)
2806
 
{
2807
 
  assert(fixed == 1);
2808
 
  return(udf.val_decimal(&null_value, dec_buf));
2809
 
}
2810
 
 
2811
 
 
2812
 
Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
2813
 
{
2814
 
  return new (thd->mem_root) Item_sum_udf_decimal(thd, this);
2815
 
}
2816
 
 
2817
 
 
2818
 
Item *Item_sum_udf_int::copy_or_same(THD* thd)
2819
 
{
2820
 
  return new (thd->mem_root) Item_sum_udf_int(thd, this);
2821
 
}
2822
 
 
2823
 
int64_t Item_sum_udf_int::val_int()
2824
 
{
2825
 
  assert(fixed == 1);
2826
 
  return(udf.val_int(&null_value));
2827
 
}
2828
 
 
2829
 
 
2830
 
String *Item_sum_udf_int::val_str(String *str)
2831
 
{
2832
 
  return val_string_from_int(str);
2833
 
}
2834
 
 
2835
 
my_decimal *Item_sum_udf_int::val_decimal(my_decimal *dec)
2836
 
{
2837
 
  return val_decimal_from_int(dec);
2838
 
}
2839
 
 
2840
 
 
2841
 
/** Default max_length is max argument length. */
2842
 
 
2843
 
void Item_sum_udf_str::fix_length_and_dec()
2844
 
{
2845
 
  max_length=0;
2846
 
  for (uint i = 0; i < arg_count; i++)
2847
 
    set_if_bigger(max_length,args[i]->max_length);
2848
 
  return;
2849
 
}
2850
 
 
2851
 
 
2852
 
Item *Item_sum_udf_str::copy_or_same(THD* thd)
2853
 
{
2854
 
  return new (thd->mem_root) Item_sum_udf_str(thd, this);
2855
 
}
2856
 
 
2857
 
 
2858
 
my_decimal *Item_sum_udf_str::val_decimal(my_decimal *dec)
2859
 
{
2860
 
  return val_decimal_from_string(dec);
2861
 
}
2862
 
 
2863
 
String *Item_sum_udf_str::val_str(String *str)
2864
 
{
2865
 
  assert(fixed == 1);
2866
 
  String *res=udf.val_str(str,&str_value);
2867
 
  null_value = !res;
2868
 
  return(res);
2869
 
}
2870
 
 
2871
2758
/*****************************************************************************
2872
2759
 GROUP_CONCAT function
2873
2760
 
2903
2790
  Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
2904
2791
  Table *table= item_func->table;
2905
2792
 
2906
 
  for (uint i= 0; i < item_func->arg_count_field; i++)
 
2793
  for (uint32_t i= 0; i < item_func->arg_count_field; i++)
2907
2794
  {
2908
2795
    Item *item= item_func->args[i];
2909
2796
    /* 
2919
2806
    */
2920
2807
    Field *field= item->get_tmp_table_field();
2921
2808
    int res;
2922
 
    uint offset= field->offset(field->table->record[0])-table->s->null_bytes;
2923
 
    if((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
 
2809
    uint32_t offset= field->offset(field->table->record[0])-table->s->null_bytes;
 
2810
    if((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2924
2811
      return res;
2925
2812
  }
2926
2813
  return 0;
2956
2843
    if (field && !item->const_item())
2957
2844
    {
2958
2845
      int res;
2959
 
      uint offset= (field->offset(field->table->record[0]) -
 
2846
      uint32_t offset= (field->offset(field->table->record[0]) -
2960
2847
                    table->s->null_bytes);
2961
 
      if ((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
 
2848
      if ((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2962
2849
        return (*order_item)->asc ? res : -res;
2963
2850
    }
2964
2851
  }
2975
2862
  Append data from current leaf to item->result.
2976
2863
*/
2977
2864
 
2978
 
int dump_leaf_key(uchar* key, element_count count __attribute__((unused)),
 
2865
int dump_leaf_key(unsigned char* key, element_count count __attribute__((unused)),
2979
2866
                  Item_func_group_concat *item)
2980
2867
{
2981
2868
  Table *table= item->table;
2984
2871
  String tmp2;
2985
2872
  String *result= &item->result;
2986
2873
  Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
2987
 
  uint old_length= result->length();
 
2874
  uint32_t old_length= result->length();
2988
2875
 
2989
2876
  if (item->no_appended)
2990
2877
    item->no_appended= false;
3006
2893
        because it contains both order and arg list fields.
3007
2894
      */
3008
2895
      Field *field= (*arg)->get_tmp_table_field();
3009
 
      uint offset= (field->offset(field->table->record[0]) -
 
2896
      uint32_t offset= (field->offset(field->table->record[0]) -
3010
2897
                    table->s->null_bytes);
3011
2898
      assert(offset < table->s->reclength);
3012
2899
      res= field->val_str(&tmp, key + offset);
3023
2910
    int well_formed_error;
3024
2911
    const CHARSET_INFO * const cs= item->collation.collation;
3025
2912
    const char *ptr= result->ptr();
3026
 
    uint add_length;
 
2913
    uint32_t add_length;
3027
2914
    /*
3028
2915
      It's ok to use item->result.length() as the fourth argument
3029
2916
      as this is never used to limit the length of the data.
3105
2992
}
3106
2993
 
3107
2994
 
3108
 
Item_func_group_concat::Item_func_group_concat(THD *thd,
 
2995
Item_func_group_concat::Item_func_group_concat(Session *session,
3109
2996
                                               Item_func_group_concat *item)
3110
 
  :Item_sum(thd, item),
 
2997
  :Item_sum(session, item),
3111
2998
  tmp_table_param(item->tmp_table_param),
3112
2999
  warning(item->warning),
3113
3000
  separator(item->separator),
3140
3027
  {
3141
3028
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
3142
3029
    sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3143
 
    warning->set_msg(current_thd, warn_buff);
 
3030
    warning->set_msg(current_session, warn_buff);
3144
3031
    warning= 0;
3145
3032
  }
3146
3033
 
3154
3041
    tmp_table_param= 0;
3155
3042
    if (table)
3156
3043
    {
3157
 
      THD *thd= table->in_use;
3158
 
      table->free_tmp_table(thd);
 
3044
      Session *session= table->in_use;
 
3045
      table->free_tmp_table(session);
3159
3046
      table= 0;
3160
3047
      if (tree)
3161
3048
      {
3171
3058
      {
3172
3059
        char warn_buff[DRIZZLE_ERRMSG_SIZE];
3173
3060
        sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3174
 
        warning->set_msg(thd, warn_buff);
 
3061
        warning->set_msg(session, warn_buff);
3175
3062
        warning= 0;
3176
3063
      }
3177
3064
    }
3181
3068
}
3182
3069
 
3183
3070
 
3184
 
Item *Item_func_group_concat::copy_or_same(THD* thd)
 
3071
Item *Item_func_group_concat::copy_or_same(Session* session)
3185
3072
{
3186
 
  return new (thd->mem_root) Item_func_group_concat(thd, this);
 
3073
  return new (session->mem_root) Item_func_group_concat(session, this);
3187
3074
}
3188
3075
 
3189
3076
 
3209
3096
  copy_fields(tmp_table_param);
3210
3097
  copy_funcs(tmp_table_param->items_to_copy);
3211
3098
 
3212
 
  for (uint i= 0; i < arg_count_field; i++)
 
3099
  for (uint32_t i= 0; i < arg_count_field; i++)
3213
3100
  {
3214
3101
    Item *show_item= args[i];
3215
3102
    if (!show_item->const_item())
3216
3103
    {
3217
3104
      Field *f= show_item->get_tmp_table_field();
3218
 
      if (f->is_null_in_record((const uchar*) table->record[0]))
 
3105
      if (f->is_null_in_record((const unsigned char*) table->record[0]))
3219
3106
        return 0;                               // Skip row if it contains null
3220
3107
    }
3221
3108
  }
3226
3113
  if (distinct) 
3227
3114
  {
3228
3115
    /* Filter out duplicate rows. */
3229
 
    uint count= unique_filter->elements_in_tree();
 
3116
    uint32_t count= unique_filter->elements_in_tree();
3230
3117
    unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3231
3118
    if (count == unique_filter->elements_in_tree())
3232
3119
      row_eligible= false;
3250
3137
 
3251
3138
 
3252
3139
bool
3253
 
Item_func_group_concat::fix_fields(THD *thd, Item **ref)
 
3140
Item_func_group_concat::fix_fields(Session *session, Item **ref)
3254
3141
{
3255
 
  uint i;                       /* for loop variable */
 
3142
  uint32_t i;                       /* for loop variable */
3256
3143
  assert(fixed == 0);
3257
3144
 
3258
 
  if (init_sum_func_check(thd))
 
3145
  if (init_sum_func_check(session))
3259
3146
    return true;
3260
3147
 
3261
3148
  maybe_null= 1;
3267
3154
  for (i=0 ; i < arg_count ; i++)
3268
3155
  {
3269
3156
    if ((!args[i]->fixed &&
3270
 
         args[i]->fix_fields(thd, args + i)) ||
 
3157
         args[i]->fix_fields(session, args + i)) ||
3271
3158
        args[i]->check_cols(1))
3272
3159
      return true;
3273
3160
  }
3282
3169
  result.set_charset(collation.collation);
3283
3170
  result_field= 0;
3284
3171
  null_value= 1;
3285
 
  max_length= thd->variables.group_concat_max_len;
 
3172
  max_length= session->variables.group_concat_max_len;
3286
3173
 
3287
3174
  uint32_t offset;
3288
3175
  if (separator->needs_conversion(separator->length(), separator->charset(),
3289
3176
                                  collation.collation, &offset))
3290
3177
  {
3291
3178
    uint32_t buflen= collation.collation->mbmaxlen * separator->length();
3292
 
    uint errors, conv_length;
 
3179
    uint32_t errors, conv_length;
3293
3180
    char *buf;
3294
3181
    String *new_separator;
3295
3182
 
3296
 
    if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
3297
 
        !(new_separator= new(thd->stmt_arena->mem_root)
 
3183
    if (!(buf= (char*) session->alloc(buflen)) ||
 
3184
        !(new_separator= new(session->mem_root)
3298
3185
                           String(buf, buflen, collation.collation)))
3299
3186
      return true;
3300
3187
    
3305
3192
    separator= new_separator;
3306
3193
  }
3307
3194
 
3308
 
  if (check_sum_func(thd, ref))
 
3195
  if (check_sum_func(session, ref))
3309
3196
    return true;
3310
3197
 
3311
3198
  fixed= 1;
3313
3200
}
3314
3201
 
3315
3202
 
3316
 
bool Item_func_group_concat::setup(THD *thd)
 
3203
bool Item_func_group_concat::setup(Session *session)
3317
3204
{
3318
3205
  List<Item> list;
3319
 
  SELECT_LEX *select_lex= thd->lex->current_select;
 
3206
  SELECT_LEX *select_lex= session->lex->current_select;
3320
3207
 
3321
3208
  /*
3322
3209
    Currently setup() can be called twice. Please add
3333
3220
                                        collation.collation->mbmaxlen;
3334
3221
  /* Push all not constant fields to the list and create a temp table */
3335
3222
  always_null= 0;
3336
 
  for (uint i= 0; i < arg_count_field; i++)
 
3223
  for (uint32_t i= 0; i < arg_count_field; i++)
3337
3224
  {
3338
3225
    Item *item= args[i];
3339
3226
    if (list.push_back(item))
3356
3243
    tmp table columns.
3357
3244
  */
3358
3245
  if (arg_count_order &&
3359
 
      setup_order(thd, args, context->table_list, list, all_fields, *order))
 
3246
      setup_order(session, args, context->table_list, list, all_fields, *order))
3360
3247
    return(true);
3361
3248
 
3362
3249
  count_field_types(select_lex, tmp_table_param, all_fields, 0);
3381
3268
    Note that in the table, we first have the order_st BY fields, then the
3382
3269
    field list.
3383
3270
  */
3384
 
  if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
 
3271
  if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3385
3272
                                (order_st*) 0, 0, true,
3386
 
                                (select_lex->options | thd->options),
 
3273
                                (select_lex->options | session->options),
3387
3274
                                HA_POS_ERROR, (char*) "")))
3388
3275
    return(true);
3389
3276
  table->file->extra(HA_EXTRA_NO_ROWS);
3394
3281
     Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3395
3282
     the row is not added to the result.
3396
3283
  */
3397
 
  uint tree_key_length= table->s->reclength - table->s->null_bytes;
 
3284
  uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
3398
3285
 
3399
3286
  if (arg_count_order)
3400
3287
  {
3404
3291
      syntax of this function). If there is no order_st BY clause, we don't
3405
3292
      create this tree.
3406
3293
    */
3407
 
    init_tree(tree, (uint) min(thd->variables.max_heap_table_size,
3408
 
                               thd->variables.sortbuff_size/16), 0,
 
3294
    init_tree(tree, (uint) cmin(session->variables.max_heap_table_size,
 
3295
                               session->variables.sortbuff_size/16), 0,
3409
3296
              tree_key_length, 
3410
3297
              group_concat_key_cmp_with_order , 0, NULL, (void*) this);
3411
3298
  }
3414
3301
    unique_filter= new Unique(group_concat_key_cmp_with_distinct,
3415
3302
                              (void*)this,
3416
3303
                              tree_key_length,
3417
 
                              thd->variables.max_heap_table_size);
 
3304
                              session->variables.max_heap_table_size);
3418
3305
  
3419
3306
  return(false);
3420
3307
}
3461
3348
  str->append(STRING_WITH_LEN("group_concat("));
3462
3349
  if (distinct)
3463
3350
    str->append(STRING_WITH_LEN("distinct "));
3464
 
  for (uint i= 0; i < arg_count_field; i++)
 
3351
  for (uint32_t i= 0; i < arg_count_field; i++)
3465
3352
  {
3466
3353
    if (i)
3467
3354
      str->append(',');
3470
3357
  if (arg_count_order)
3471
3358
  {
3472
3359
    str->append(STRING_WITH_LEN(" order by "));
3473
 
    for (uint i= 0 ; i < arg_count_order ; i++)
 
3360
    for (uint32_t i= 0 ; i < arg_count_order ; i++)
3474
3361
    {
3475
3362
      if (i)
3476
3363
        str->append(',');