~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/sum.cc

  • Committer: Olaf van der Spek
  • Date: 2011-03-24 00:16:14 UTC
  • mto: This revision was merged to the branch mainline in revision 2251.
  • Revision ID: olafvdspek@gmail.com-20110324001614-wvmgc6eg52oq2321
Remove const_reference and reference from Session

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
  @brief
21
21
  Sum functions (COUNT, MIN...)
22
22
*/
23
 
#include "config.h"
 
23
#include <config.h>
24
24
#include <cstdio>
25
25
#include <math.h>
26
26
#include <drizzled/sql_select.h>
29
29
#include <drizzled/hybrid_type_traits_integer.h>
30
30
#include <drizzled/hybrid_type_traits_decimal.h>
31
31
#include <drizzled/sql_base.h>
32
 
 
 
32
#include <drizzled/session.h>
33
33
#include <drizzled/item/sum.h>
34
34
#include <drizzled/field/decimal.h>
35
35
#include <drizzled/field/double.h>
36
36
#include <drizzled/field/int64.h>
37
37
#include <drizzled/field/date.h>
38
38
#include <drizzled/field/datetime.h>
39
 
 
40
 
#include "drizzled/internal/m_string.h"
 
39
#include <drizzled/unique.h>
 
40
#include <drizzled/type/decimal.h>
 
41
#include <drizzled/internal/m_string.h>
 
42
#include <drizzled/item/subselect.h>
 
43
#include <drizzled/sql_lex.h>
 
44
#include <drizzled/system_variables.h>
41
45
 
42
46
#include <algorithm>
43
47
 
44
48
using namespace std;
45
49
 
46
 
namespace drizzled
47
 
{
 
50
namespace drizzled {
48
51
 
49
 
extern my_decimal decimal_zero;
50
52
extern plugin::StorageEngine *heap_engine;
51
53
 
52
54
/**
73
75
 
74
76
bool Item_sum::init_sum_func_check(Session *session)
75
77
{
76
 
  if (!session->lex->allow_sum_func)
 
78
  if (!session->lex().allow_sum_func)
77
79
  {
78
80
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
79
81
               MYF(0));
80
82
    return true;
81
83
  }
82
84
  /* Set a reference to the nesting set function if there is  any */
83
 
  in_sum_func= session->lex->in_sum_func;
 
85
  in_sum_func= session->lex().in_sum_func;
84
86
  /* 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;
 
87
  session->lex().in_sum_func= this;
 
88
  nest_level= session->lex().current_select->nest_level;
87
89
  ref_by= 0;
88
90
  aggr_level= -1;
89
91
  aggr_sel= NULL;
90
92
  max_arg_level= -1;
91
93
  max_sum_func_level= -1;
92
 
  outer_fields.empty();
 
94
  outer_fields.clear();
93
95
  return false;
94
96
}
95
97
 
145
147
bool Item_sum::check_sum_func(Session *session, Item **ref)
146
148
{
147
149
  bool invalid= false;
148
 
  nesting_map allow_sum_func= session->lex->allow_sum_func;
 
150
  nesting_map allow_sum_func= session->lex().allow_sum_func;
149
151
  /*
150
152
    The value of max_arg_level is updated if an argument of the set function
151
153
    contains a column reference resolved  against a subquery whose level is
178
180
  if (!invalid && aggr_level < 0)
179
181
  {
180
182
    aggr_level= nest_level;
181
 
    aggr_sel= session->lex->current_select;
 
183
    aggr_sel= session->lex().current_select;
182
184
  }
183
185
  /*
184
186
    By this moment we either found a subquery where the set function is
224
226
    Check that non-aggregated fields and sum functions aren't mixed in the
225
227
    same select in the ONLY_FULL_GROUP_BY mode.
226
228
  */
227
 
  if (outer_fields.elements)
 
229
  if (outer_fields.size())
228
230
  {
229
231
    Item_field *field;
230
232
    /*
253
255
        select the field belongs to. If there are some then an error is
254
256
        raised.
255
257
    */
256
 
    List_iterator<Item_field> of(outer_fields);
 
258
    List<Item_field>::iterator of(outer_fields.begin());
257
259
    while ((field= of++))
258
260
    {
259
261
      Select_Lex *sel= field->cached_table->select_lex;
284
286
  }
285
287
  aggr_sel->full_group_by_flag.set(SUM_FUNC_USED);
286
288
  update_used_tables();
287
 
  session->lex->in_sum_func= in_sum_func;
 
289
  session->lex().in_sum_func= in_sum_func;
288
290
  return false;
289
291
}
290
292
 
316
318
bool Item_sum::register_sum_func(Session *session, Item **ref)
317
319
{
318
320
  Select_Lex *sl;
319
 
  nesting_map allow_sum_func= session->lex->allow_sum_func;
320
 
  for (sl= session->lex->current_select->master_unit()->outer_select() ;
 
321
  nesting_map allow_sum_func= session->lex().allow_sum_func;
 
322
  for (sl= session->lex().current_select->master_unit()->outer_select() ;
321
323
       sl && sl->nest_level > max_arg_level;
322
324
       sl= sl->master_unit()->outer_select() )
323
325
  {
368
370
      with_sum_func being set for an Select_Lex means that this Select_Lex
369
371
      has aggregate functions directly referenced (i.e. not through a sub-select).
370
372
    */
371
 
    for (sl= session->lex->current_select;
 
373
    for (sl= session->lex().current_select;
372
374
         sl && sl != aggr_sel && sl->master_unit()->item;
373
375
         sl= sl->master_unit()->outer_select() )
374
376
      sl->master_unit()->item->with_sum_func= 1;
375
377
  }
376
 
  session->lex->current_select->mark_as_dependent(aggr_sel);
 
378
  session->lex().current_select->mark_as_dependent(aggr_sel);
377
379
  return false;
378
380
}
379
381
 
380
382
 
381
 
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements),
 
383
Item_sum::Item_sum(List<Item> &list) :arg_count(list.size()),
382
384
  forced_const(false)
383
385
{
384
386
  if ((args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
385
387
  {
386
388
    uint32_t i=0;
387
 
    List_iterator_fast<Item> li(list);
 
389
    List<Item>::iterator li(list.begin());
388
390
    Item *item;
389
391
 
390
392
    while ((item=li++))
393
395
    }
394
396
  }
395
397
  mark_as_sum_func();
396
 
  list.empty();                                 // Fields are used
 
398
  list.clear();                                 // Fields are used
397
399
}
398
400
 
399
401
 
411
413
  if (arg_count <= 2)
412
414
    args=tmp_args;
413
415
  else
414
 
    if (!(args= (Item**) session->alloc(sizeof(Item*)*arg_count)))
 
416
    if (!(args= (Item**) session->getMemRoot()->allocate(sizeof(Item*)*arg_count)))
415
417
      return;
416
418
  memcpy(args, item->args, sizeof(Item*)*arg_count);
417
419
}
419
421
 
420
422
void Item_sum::mark_as_sum_func()
421
423
{
422
 
  Select_Lex *cur_select= current_session->lex->current_select;
 
424
  Select_Lex *cur_select= getSession().lex().current_select;
423
425
  cur_select->n_sum_items++;
424
426
  cur_select->with_sum_func= 1;
425
427
  with_sum_func= 1;
446
448
}
447
449
 
448
450
 
449
 
void Item_sum::print(String *str, enum_query_type query_type)
 
451
void Item_sum::print(String *str)
450
452
{
451
453
  str->append(func_name());
452
454
  for (uint32_t i=0 ; i < arg_count ; i++)
453
455
  {
454
456
    if (i)
455
457
      str->append(',');
456
 
    args[i]->print(str, query_type);
 
458
    args[i]->print(str);
457
459
  }
458
460
  str->append(')');
459
461
}
583
585
}
584
586
 
585
587
 
586
 
my_decimal *Item_sum_num::val_decimal(my_decimal *decimal_value)
 
588
type::Decimal *Item_sum_num::val_decimal(type::Decimal *decimal_value)
587
589
{
588
590
  return val_decimal_from_real(decimal_value);
589
591
}
596
598
}
597
599
 
598
600
 
599
 
my_decimal *Item_sum_int::val_decimal(my_decimal *decimal_value)
 
601
type::Decimal *Item_sum_int::val_decimal(type::Decimal *decimal_value)
600
602
{
601
603
  return val_decimal_from_int(decimal_value);
602
604
}
643
645
    sum_int= item->sum_int;
644
646
    break;
645
647
  case DECIMAL_RESULT:
646
 
    my_decimal2decimal(&item->sum_dec, &sum_dec);
 
648
    class_decimal2decimal(&item->sum_dec, &sum_dec);
647
649
    break;
648
650
  case REAL_RESULT:
649
651
    sum= item->sum;
683
685
    break;
684
686
  case DECIMAL_RESULT:
685
687
    max_length= item->max_length;
686
 
    my_decimal_set_zero(&sum_dec);
 
688
    sum_dec.set_zero();
687
689
    break;
688
690
  case REAL_RESULT:
689
691
    max_length= float_length(decimals);
723
725
  {
724
726
    field= ((Item_field*) args[0])->field;
725
727
 
726
 
    if ((field= create_tmp_field_from_field(current_session, field, name, table,
 
728
    if ((field= create_tmp_field_from_field(&getSession(), field, name, table,
727
729
                                            NULL, convert_blob_length)))
728
730
      field->flags&= ~NOT_NULL_FLAG;
729
731
    return field;
735
737
  */
736
738
  switch (args[0]->field_type()) {
737
739
  case DRIZZLE_TYPE_DATE:
738
 
    field= new Field_date(maybe_null, name, collation.collation);
 
740
    field= new Field_date(maybe_null, name);
739
741
    break;
740
742
  case DRIZZLE_TYPE_TIMESTAMP:
741
743
  case DRIZZLE_TYPE_DATETIME:
742
 
    field= new Field_datetime(maybe_null, name, collation.collation);
 
744
    field= new Field_datetime(maybe_null, name);
743
745
    break;
744
746
  default:
745
747
    return Item_sum::create_tmp_field(group, table, convert_blob_length);
746
748
  }
 
749
 
747
750
  if (field)
748
751
    field->init(table);
 
752
 
749
753
  return field;
750
754
}
751
755
 
765
769
  /* TODO: check if the following assignments are really needed */
766
770
  if (hybrid_type == DECIMAL_RESULT)
767
771
  {
768
 
    my_decimal2decimal(item->dec_buffs, dec_buffs);
769
 
    my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
 
772
    class_decimal2decimal(item->dec_buffs, dec_buffs);
 
773
    class_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
770
774
  }
771
775
  else
772
776
    sum= item->sum;
784
788
  if (hybrid_type == DECIMAL_RESULT)
785
789
  {
786
790
    curr_dec_buff= 0;
787
 
    my_decimal_set_zero(dec_buffs);
 
791
    dec_buffs->set_zero();
788
792
  }
789
793
  else
790
794
    sum= 0.0;
807
811
    {
808
812
      /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
809
813
      int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
810
 
      max_length= my_decimal_precision_to_length(precision, decimals,
 
814
      max_length= class_decimal_precision_to_length(precision, decimals,
811
815
                                                 unsigned_flag);
812
816
      curr_dec_buff= 0;
813
817
      hybrid_type= DECIMAL_RESULT;
814
 
      my_decimal_set_zero(dec_buffs);
 
818
      dec_buffs->set_zero();
815
819
      break;
816
820
    }
817
821
  case ROW_RESULT:
824
828
{
825
829
  if (hybrid_type == DECIMAL_RESULT)
826
830
  {
827
 
    my_decimal value, *val= args[0]->val_decimal(&value);
 
831
    type::Decimal value, *val= args[0]->val_decimal(&value);
828
832
    if (!args[0]->null_value)
829
833
    {
830
 
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
 
834
      class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
831
835
                     val, dec_buffs + curr_dec_buff);
832
836
      curr_dec_buff^= 1;
833
837
      null_value= 0;
849
853
  if (hybrid_type == DECIMAL_RESULT)
850
854
  {
851
855
    int64_t result;
852
 
    my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
853
 
                   &result);
 
856
    (dec_buffs + curr_dec_buff)->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
854
857
    return result;
855
858
  }
856
859
  return (int64_t) rint(val_real());
861
864
{
862
865
  assert(fixed == 1);
863
866
  if (hybrid_type == DECIMAL_RESULT)
864
 
    my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
 
867
    class_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
865
868
  return sum;
866
869
}
867
870
 
874
877
}
875
878
 
876
879
 
877
 
my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
 
880
type::Decimal *Item_sum_sum::val_decimal(type::Decimal *val)
878
881
{
879
882
  if (hybrid_type == DECIMAL_RESULT)
880
883
    return (dec_buffs + curr_dec_buff);
937
940
 
938
941
  virtual void div(Hybrid_type *val, uint64_t u) const
939
942
  {
940
 
    int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
 
943
    int2_class_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
941
944
    val->used_dec_buf_no= 0;
942
945
    val->traits= Hybrid_type_traits_decimal::instance();
943
946
    val->traits->div(val, u);
1009
1012
*/
1010
1013
bool Item_sum_distinct::setup(Session *session)
1011
1014
{
1012
 
  List<CreateField> field_list;
1013
 
  CreateField field_def;                              /* field definition */
1014
1015
  /* It's legal to call setup() more than once when in a subquery */
1015
1016
  if (tree)
1016
 
    return(false);
 
1017
    return false;
1017
1018
 
1018
1019
  /*
1019
1020
    Virtual table and the tree are created anew on each re-execution of
1020
1021
    PS/SP. Hence all further allocations are performed in the runtime
1021
1022
    mem_root.
1022
1023
  */
1023
 
  if (field_list.push_back(&field_def))
1024
 
    return(true);
1025
 
 
1026
1024
  null_value= maybe_null= 1;
1027
1025
  quick_group= 0;
1028
1026
 
1029
1027
  assert(args[0]->fixed);
1030
1028
 
1031
 
  field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
1032
 
                               args[0]->decimals, args[0]->maybe_null);
1033
 
 
1034
 
  if (! (table= session->getInstanceTable(field_list)))
1035
 
    return(true);
 
1029
  std::list<CreateField> field_list;
 
1030
  field_list.push_back(CreateField());
 
1031
  CreateField& field_def = field_list.back();
 
1032
  field_def.init_for_tmp_table(table_field_type, args[0]->max_length, args[0]->decimals, args[0]->maybe_null);
 
1033
  table= &session->getInstanceTable(field_list);
1036
1034
 
1037
1035
  /* XXX: check that the case of CHAR(0) works OK */
1038
1036
  tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
1043
1041
    simple_raw_key_cmp because the table contains numbers only; decimals
1044
1042
    are converted to binary representation as well.
1045
1043
  */
1046
 
  tree= new Unique(simple_raw_key_cmp, &tree_key_length,
1047
 
                   tree_key_length,
1048
 
                   (size_t)session->variables.max_heap_table_size);
1049
 
 
 
1044
  tree= new Unique(simple_raw_key_cmp, &tree_key_length, tree_key_length, (size_t)session->variables.max_heap_table_size);
1050
1045
  is_evaluated= false;
1051
 
  return(tree == 0);
 
1046
  return false;
1052
1047
}
1053
1048
 
1054
 
 
1055
1049
bool Item_sum_distinct::add()
1056
1050
{
1057
1051
  args[0]->save_in_field(table->getField(0), false);
1131
1125
}
1132
1126
 
1133
1127
 
1134
 
my_decimal *Item_sum_distinct::val_decimal(my_decimal *to)
 
1128
type::Decimal *Item_sum_distinct::val_decimal(type::Decimal *to)
1135
1129
{
1136
1130
  calculate_val_and_count();
1137
1131
  if (null_value)
1163
1157
Item_sum_avg_distinct::fix_length_and_dec()
1164
1158
{
1165
1159
  Item_sum_distinct::fix_length_and_dec();
1166
 
  prec_increment= current_session->variables.div_precincrement;
 
1160
  prec_increment= getSession().variables.div_precincrement;
1167
1161
  /*
1168
1162
    AVG() will divide val by count. We need to reserve digits
1169
1163
    after decimal point as the result can be fractional.
1226
1220
{
1227
1221
  Item_sum_sum::fix_length_and_dec();
1228
1222
  maybe_null=null_value=1;
1229
 
  prec_increment= current_session->variables.div_precincrement;
 
1223
  prec_increment= getSession().variables.div_precincrement;
 
1224
 
1230
1225
  if (hybrid_type == DECIMAL_RESULT)
1231
1226
  {
1232
1227
    int precision= args[0]->decimal_precision() + prec_increment;
1233
1228
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1234
 
    max_length= my_decimal_precision_to_length(precision, decimals,
 
1229
    max_length= class_decimal_precision_to_length(precision, decimals,
1235
1230
                                               unsigned_flag);
1236
1231
    f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1237
1232
    f_scale=  args[0]->decimals;
1238
 
    dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
 
1233
    dec_bin_size= class_decimal_get_binary_size(f_precision, f_scale);
1239
1234
  }
1240
1235
  else {
1241
1236
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1311
1306
}
1312
1307
 
1313
1308
 
1314
 
my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
 
1309
type::Decimal *Item_sum_avg::val_decimal(type::Decimal *val)
1315
1310
{
1316
 
  my_decimal sum_buff, cnt;
1317
 
  const my_decimal *sum_dec;
 
1311
  type::Decimal sum_buff, cnt;
 
1312
  const type::Decimal *sum_dec;
1318
1313
  assert(fixed == 1);
1319
1314
  if (!count)
1320
1315
  {
1330
1325
    return val_decimal_from_real(val);
1331
1326
 
1332
1327
  sum_dec= dec_buffs + curr_dec_buff;
1333
 
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
1334
 
  my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
 
1328
  int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
 
1329
  class_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1335
1330
  return val;
1336
1331
}
1337
1332
 
1423
1418
void Item_sum_variance::fix_length_and_dec()
1424
1419
{
1425
1420
  maybe_null= null_value= 1;
1426
 
  prec_increment= current_session->variables.div_precincrement;
 
1421
  prec_increment= getSession().variables.div_precincrement;
1427
1422
 
1428
1423
  /*
1429
1424
    According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1443
1438
    {
1444
1439
      int precision= args[0]->decimal_precision()*2 + prec_increment;
1445
1440
      decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1446
 
      max_length= my_decimal_precision_to_length(precision, decimals,
 
1441
      max_length= class_decimal_precision_to_length(precision, decimals,
1447
1442
                                                 unsigned_flag);
1448
1443
 
1449
1444
      break;
1539
1534
}
1540
1535
 
1541
1536
 
1542
 
my_decimal *Item_sum_variance::val_decimal(my_decimal *dec_buf)
 
1537
type::Decimal *Item_sum_variance::val_decimal(type::Decimal *dec_buf)
1543
1538
{
1544
1539
  assert(fixed == 1);
1545
1540
  return val_decimal_from_real(dec_buf);
1603
1598
    sum_int= 0;
1604
1599
    break;
1605
1600
  case DECIMAL_RESULT:
1606
 
    my_decimal_set_zero(&sum_dec);
 
1601
    sum_dec.set_zero();
1607
1602
    break;
1608
1603
  case REAL_RESULT:
1609
1604
    sum= 0.0;
1632
1627
  case INT_RESULT:
1633
1628
    return (double) sum_int;
1634
1629
  case DECIMAL_RESULT:
1635
 
    my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
 
1630
    class_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1636
1631
    return sum;
1637
1632
  case REAL_RESULT:
1638
1633
    return sum;
1656
1651
  case DECIMAL_RESULT:
1657
1652
  {
1658
1653
    int64_t result;
1659
 
    my_decimal2int(E_DEC_FATAL_ERROR, &sum_dec, unsigned_flag, &result);
 
1654
    sum_dec.val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
1660
1655
    return sum_int;
1661
1656
  }
1662
1657
  default:
1665
1660
}
1666
1661
 
1667
1662
 
1668
 
my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
 
1663
type::Decimal *Item_sum_hybrid::val_decimal(type::Decimal *val)
1669
1664
{
1670
1665
  assert(fixed == 1);
1671
1666
  if (null_value)
1673
1668
 
1674
1669
  switch (hybrid_type) {
1675
1670
  case STRING_RESULT:
1676
 
    string2my_decimal(E_DEC_FATAL_ERROR, &value, val);
 
1671
    val->store(E_DEC_FATAL_ERROR, &value);
1677
1672
    break;
1678
1673
  case REAL_RESULT:
1679
 
    double2my_decimal(E_DEC_FATAL_ERROR, sum, val);
 
1674
    double2_class_decimal(E_DEC_FATAL_ERROR, sum, val);
1680
1675
    break;
1681
1676
  case DECIMAL_RESULT:
1682
1677
    val= &sum_dec;
1683
1678
    break;
1684
1679
  case INT_RESULT:
1685
 
    int2my_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
 
1680
    int2_class_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1686
1681
    break;
1687
1682
  case ROW_RESULT:
1688
1683
    // This case should never be choosen
1708
1703
    str->set_real(sum,decimals, &my_charset_bin);
1709
1704
    break;
1710
1705
  case DECIMAL_RESULT:
1711
 
    my_decimal2string(E_DEC_FATAL_ERROR, &sum_dec, 0, 0, 0, str);
 
1706
    class_decimal2string(&sum_dec, 0, str);
1712
1707
    return str;
1713
1708
  case INT_RESULT:
1714
1709
    str->set_int(sum_int, unsigned_flag, &my_charset_bin);
1781
1776
    break;
1782
1777
  case DECIMAL_RESULT:
1783
1778
    {
1784
 
      my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1779
      type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1785
1780
      if (!args[0]->null_value &&
1786
 
          (null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
 
1781
          (null_value || (class_decimal_cmp(&sum_dec, val) > 0)))
1787
1782
      {
1788
 
        my_decimal2decimal(val, &sum_dec);
 
1783
        class_decimal2decimal(val, &sum_dec);
1789
1784
        null_value= 0;
1790
1785
      }
1791
1786
    }
1844
1839
    break;
1845
1840
  case DECIMAL_RESULT:
1846
1841
    {
1847
 
      my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1842
      type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1848
1843
      if (!args[0]->null_value &&
1849
 
          (null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
 
1844
          (null_value || (class_decimal_cmp(val, &sum_dec) > 0)))
1850
1845
      {
1851
 
        my_decimal2decimal(val, &sum_dec);
 
1846
        class_decimal2decimal(val, &sum_dec);
1852
1847
        null_value= 0;
1853
1848
      }
1854
1849
    }
2009
2004
    }
2010
2005
  case DECIMAL_RESULT:
2011
2006
    {
2012
 
      my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
 
2007
      type::Decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2013
2008
 
2014
2009
      if (maybe_null)
2015
2010
      {
2037
2032
{
2038
2033
  if (hybrid_type == DECIMAL_RESULT)
2039
2034
  {
2040
 
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
 
2035
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2041
2036
    if (!arg_val)                               // Null
2042
2037
      arg_val= &decimal_zero;
2043
2038
    result_field->store_decimal(arg_val);
2072
2067
  if (hybrid_type == DECIMAL_RESULT)
2073
2068
  {
2074
2069
    int64_t tmp;
2075
 
    my_decimal value, *arg_dec= args[0]->val_decimal(&value);
 
2070
    type::Decimal value, *arg_dec= args[0]->val_decimal(&value);
2076
2071
    if (args[0]->null_value)
2077
2072
    {
2078
2073
      arg_dec= &decimal_zero;
2080
2075
    }
2081
2076
    else
2082
2077
      tmp= 1;
2083
 
    my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
 
2078
    arg_dec->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
2084
2079
    res+= dec_bin_size;
2085
2080
    int8store(res, tmp);
2086
2081
  }
2124
2119
{
2125
2120
  if (hybrid_type == DECIMAL_RESULT)
2126
2121
  {
2127
 
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
 
2122
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2128
2123
    if (!args[0]->null_value)
2129
2124
    {
2130
2125
      if (!result_field->is_null())
2131
2126
      {
2132
 
        my_decimal field_value,
 
2127
        type::Decimal field_value,
2133
2128
                   *field_val= result_field->val_decimal(&field_value);
2134
 
        my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
 
2129
        class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2135
2130
        result_field->store_decimal(dec_buffs);
2136
2131
      }
2137
2132
      else
2176
2171
  unsigned char *res=result_field->ptr;
2177
2172
  if (hybrid_type == DECIMAL_RESULT)
2178
2173
  {
2179
 
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
 
2174
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2180
2175
    if (!args[0]->null_value)
2181
2176
    {
2182
 
      binary2my_decimal(E_DEC_FATAL_ERROR, res,
 
2177
      binary2_class_decimal(E_DEC_FATAL_ERROR, res,
2183
2178
                        dec_buffs + 1, f_precision, f_scale);
2184
2179
      field_count= sint8korr(res + dec_bin_size);
2185
 
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
2186
 
      my_decimal2binary(E_DEC_FATAL_ERROR, dec_buffs,
2187
 
                        res, f_precision, f_scale);
 
2180
      class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
 
2181
      dec_buffs->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
2188
2182
      res+= dec_bin_size;
2189
2183
      field_count++;
2190
2184
      int8store(res, field_count);
2302
2296
Item_sum_hybrid::min_max_update_decimal_field()
2303
2297
{
2304
2298
  /* TODO: optimize: do not get result_field in case of args[0] is NULL */
2305
 
  my_decimal old_val, nr_val;
2306
 
  const my_decimal *old_nr= result_field->val_decimal(&old_val);
2307
 
  const my_decimal *nr= args[0]->val_decimal(&nr_val);
 
2299
  type::Decimal old_val, nr_val;
 
2300
  const type::Decimal *old_nr= result_field->val_decimal(&old_val);
 
2301
  const type::Decimal *nr= args[0]->val_decimal(&nr_val);
2308
2302
  if (!args[0]->null_value)
2309
2303
  {
2310
2304
    if (result_field->is_null(0))
2311
2305
      old_nr=nr;
2312
2306
    else
2313
2307
    {
2314
 
      bool res= my_decimal_cmp(old_nr, nr) > 0;
 
2308
      bool res= class_decimal_cmp(old_nr, nr) > 0;
2315
2309
      /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2316
2310
      if ((cmp_sign > 0) ^ (!res))
2317
2311
        old_nr=nr;
2368
2362
}
2369
2363
 
2370
2364
 
2371
 
my_decimal *Item_avg_field::val_decimal(my_decimal *dec_buf)
 
2365
type::Decimal *Item_avg_field::val_decimal(type::Decimal *dec_buf)
2372
2366
{
2373
2367
  // fix_fields() never calls for this Item
2374
2368
  if (hybrid_type == REAL_RESULT)
2378
2372
  if ((null_value= !count))
2379
2373
    return 0;
2380
2374
 
2381
 
  my_decimal dec_count, dec_field;
2382
 
  binary2my_decimal(E_DEC_FATAL_ERROR,
 
2375
  type::Decimal dec_count, dec_field;
 
2376
  binary2_class_decimal(E_DEC_FATAL_ERROR,
2383
2377
                    field->ptr, &dec_field, f_precision, f_scale);
2384
 
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
2385
 
  my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
 
2378
  int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
 
2379
  class_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2386
2380
                 &dec_field, &dec_count, prec_increment);
2387
2381
  return dec_buf;
2388
2382
}
2413
2407
}
2414
2408
 
2415
2409
 
2416
 
my_decimal *Item_std_field::val_decimal(my_decimal *dec_buf)
 
2410
type::Decimal *Item_std_field::val_decimal(type::Decimal *dec_buf)
2417
2411
{
2418
2412
  /*
2419
2413
    We can't call val_decimal_from_real() for DECIMAL_RESULT as
2420
2414
    Item_variance_field::val_real() would cause an infinite loop
2421
2415
  */
2422
 
  my_decimal tmp_dec, *dec;
 
2416
  type::Decimal tmp_dec, *dec;
2423
2417
  double nr;
2424
2418
  if (hybrid_type == REAL_RESULT)
2425
2419
    return val_decimal_from_real(dec_buf);
2427
2421
  dec= Item_variance_field::val_decimal(dec_buf);
2428
2422
  if (!dec)
2429
2423
    return 0;
2430
 
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
 
2424
  class_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2431
2425
  assert(nr >= 0.0);
2432
2426
  nr= sqrt(nr);
2433
 
  double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2434
 
  my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
 
2427
  double2_class_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
 
2428
  class_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
2435
2429
  return dec_buf;
2436
2430
}
2437
2431
 
2580
2574
bool Item_sum_count_distinct::setup(Session *session)
2581
2575
{
2582
2576
  List<Item> list;
2583
 
  Select_Lex *select_lex= session->lex->current_select;
 
2577
  Select_Lex *select_lex= session->lex().current_select;
2584
2578
 
2585
2579
  /*
2586
2580
    Setup can be called twice for ROLLUP items. This is a bug.
2597
2591
  for (uint32_t i=0; i < arg_count ; i++)
2598
2592
  {
2599
2593
    Item *item=args[i];
2600
 
    if (list.push_back(item))
2601
 
      return true;                              // End of memory
 
2594
    list.push_back(item);
2602
2595
    if (item->const_item() && item->is_null())
2603
2596
      always_null= 1;
2604
2597
  }
2665
2658
        uint32_t *length;
2666
2659
        compare_key= (qsort_cmp2) composite_key_cmp;
2667
2660
        cmp_arg= (void*) this;
2668
 
        field_lengths= (uint32_t*) session->alloc(table->getShare()->sizeFields() * sizeof(uint32_t));
 
2661
        field_lengths= (uint32_t*) session->getMemRoot()->allocate(table->getShare()->sizeFields() * sizeof(uint32_t));
2669
2662
        for (tree_key_length= 0, length= field_lengths, field= table->getFields();
2670
2663
             field < field_end; ++field, ++length)
2671
2664
        {
2969
2962
   separator(separator_arg), tree(NULL), unique_filter(NULL), table(0),
2970
2963
   order(0), context(context_arg),
2971
2964
   arg_count_order(order_list ? order_list->elements : 0),
2972
 
   arg_count_field(select_list->elements),
 
2965
   arg_count_field(select_list->size()),
2973
2966
   count_cut_values(0),
2974
2967
   distinct(distinct_arg),
2975
2968
   warning_for_row(false),
2994
2987
  order= (Order**)(args + arg_count);
2995
2988
 
2996
2989
  /* fill args items of show and sort */
2997
 
  List_iterator_fast<Item> li(*select_list);
 
2990
  List<Item>::iterator li(select_list->begin());
2998
2991
 
2999
2992
  for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
3000
2993
    *arg_ptr= item_select;
3049
3042
  {
3050
3043
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
3051
3044
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3052
 
    warning->set_msg(current_session, warn_buff);
 
3045
    warning->set_msg(&getSession(), warn_buff);
3053
3046
    warning= 0;
3054
3047
  }
3055
3048
 
3204
3197
bool Item_func_group_concat::setup(Session *session)
3205
3198
{
3206
3199
  List<Item> list;
3207
 
  Select_Lex *select_lex= session->lex->current_select;
 
3200
  Select_Lex *select_lex= session->lex().current_select;
3208
3201
 
3209
3202
  /*
3210
3203
    Currently setup() can be called twice. Please add
3224
3217
  for (uint32_t i= 0; i < arg_count_field; i++)
3225
3218
  {
3226
3219
    Item *item= args[i];
3227
 
    if (list.push_back(item))
3228
 
      return(true);
 
3220
    list.push_back(item);
3229
3221
    if (item->const_item())
3230
3222
    {
3231
3223
      if (item->is_null())
3364
3356
}
3365
3357
 
3366
3358
 
3367
 
void Item_func_group_concat::print(String *str, enum_query_type query_type)
 
3359
void Item_func_group_concat::print(String *str)
3368
3360
{
3369
3361
  str->append(STRING_WITH_LEN("group_concat("));
3370
3362
  if (distinct)
3373
3365
  {
3374
3366
    if (i)
3375
3367
      str->append(',');
3376
 
    args[i]->print(str, query_type);
 
3368
    args[i]->print(str);
3377
3369
  }
3378
3370
  if (arg_count_order)
3379
3371
  {
3382
3374
    {
3383
3375
      if (i)
3384
3376
        str->append(',');
3385
 
      (*order[i]->item)->print(str, query_type);
 
3377
      (*order[i]->item)->print(str);
3386
3378
      if (order[i]->asc)
3387
3379
        str->append(STRING_WITH_LEN(" ASC"));
3388
3380
      else