~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/sum.cc

  • Committer: Brian Aker
  • Date: 2010-12-24 21:27:54 UTC
  • mto: (2035.1.1 clean)
  • mto: This revision was merged to the branch mainline in revision 2037.
  • Revision ID: brian@tangent.org-20101224212754-85xvjullymvhibr9
Merge in cast() for BOOLEAN.

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
 
#include <drizzled/session.h>
33
32
 
34
33
#include <drizzled/item/sum.h>
35
34
#include <drizzled/field/decimal.h>
37
36
#include <drizzled/field/int64.h>
38
37
#include <drizzled/field/date.h>
39
38
#include <drizzled/field/datetime.h>
40
 
#include <drizzled/unique.h>
41
 
 
42
 
#include <drizzled/type/decimal.h>
43
 
 
44
 
#include <drizzled/internal/m_string.h>
 
39
 
 
40
#include "drizzled/internal/m_string.h"
45
41
 
46
42
#include <algorithm>
47
43
 
50
46
namespace drizzled
51
47
{
52
48
 
 
49
extern my_decimal decimal_zero;
53
50
extern plugin::StorageEngine *heap_engine;
54
51
 
55
52
/**
76
73
 
77
74
bool Item_sum::init_sum_func_check(Session *session)
78
75
{
79
 
  if (!session->getLex()->allow_sum_func)
 
76
  if (!session->lex->allow_sum_func)
80
77
  {
81
78
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
82
79
               MYF(0));
83
80
    return true;
84
81
  }
85
82
  /* Set a reference to the nesting set function if there is  any */
86
 
  in_sum_func= session->getLex()->in_sum_func;
 
83
  in_sum_func= session->lex->in_sum_func;
87
84
  /* Save a pointer to object to be used in items for nested set functions */
88
 
  session->getLex()->in_sum_func= this;
89
 
  nest_level= session->getLex()->current_select->nest_level;
 
85
  session->lex->in_sum_func= this;
 
86
  nest_level= session->lex->current_select->nest_level;
90
87
  ref_by= 0;
91
88
  aggr_level= -1;
92
89
  aggr_sel= NULL;
93
90
  max_arg_level= -1;
94
91
  max_sum_func_level= -1;
95
 
  outer_fields.clear();
 
92
  outer_fields.empty();
96
93
  return false;
97
94
}
98
95
 
148
145
bool Item_sum::check_sum_func(Session *session, Item **ref)
149
146
{
150
147
  bool invalid= false;
151
 
  nesting_map allow_sum_func= session->getLex()->allow_sum_func;
 
148
  nesting_map allow_sum_func= session->lex->allow_sum_func;
152
149
  /*
153
150
    The value of max_arg_level is updated if an argument of the set function
154
151
    contains a column reference resolved  against a subquery whose level is
181
178
  if (!invalid && aggr_level < 0)
182
179
  {
183
180
    aggr_level= nest_level;
184
 
    aggr_sel= session->getLex()->current_select;
 
181
    aggr_sel= session->lex->current_select;
185
182
  }
186
183
  /*
187
184
    By this moment we either found a subquery where the set function is
256
253
        select the field belongs to. If there are some then an error is
257
254
        raised.
258
255
    */
259
 
    List<Item_field>::iterator of(outer_fields.begin());
 
256
    List_iterator<Item_field> of(outer_fields);
260
257
    while ((field= of++))
261
258
    {
262
259
      Select_Lex *sel= field->cached_table->select_lex;
287
284
  }
288
285
  aggr_sel->full_group_by_flag.set(SUM_FUNC_USED);
289
286
  update_used_tables();
290
 
  session->getLex()->in_sum_func= in_sum_func;
 
287
  session->lex->in_sum_func= in_sum_func;
291
288
  return false;
292
289
}
293
290
 
319
316
bool Item_sum::register_sum_func(Session *session, Item **ref)
320
317
{
321
318
  Select_Lex *sl;
322
 
  nesting_map allow_sum_func= session->getLex()->allow_sum_func;
323
 
  for (sl= session->getLex()->current_select->master_unit()->outer_select() ;
 
319
  nesting_map allow_sum_func= session->lex->allow_sum_func;
 
320
  for (sl= session->lex->current_select->master_unit()->outer_select() ;
324
321
       sl && sl->nest_level > max_arg_level;
325
322
       sl= sl->master_unit()->outer_select() )
326
323
  {
371
368
      with_sum_func being set for an Select_Lex means that this Select_Lex
372
369
      has aggregate functions directly referenced (i.e. not through a sub-select).
373
370
    */
374
 
    for (sl= session->getLex()->current_select;
 
371
    for (sl= session->lex->current_select;
375
372
         sl && sl != aggr_sel && sl->master_unit()->item;
376
373
         sl= sl->master_unit()->outer_select() )
377
374
      sl->master_unit()->item->with_sum_func= 1;
378
375
  }
379
 
  session->getLex()->current_select->mark_as_dependent(aggr_sel);
 
376
  session->lex->current_select->mark_as_dependent(aggr_sel);
380
377
  return false;
381
378
}
382
379
 
387
384
  if ((args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
388
385
  {
389
386
    uint32_t i=0;
390
 
    List<Item>::iterator li(list.begin());
 
387
    List_iterator_fast<Item> li(list);
391
388
    Item *item;
392
389
 
393
390
    while ((item=li++))
396
393
    }
397
394
  }
398
395
  mark_as_sum_func();
399
 
  list.clear();                                 // Fields are used
 
396
  list.empty();                                 // Fields are used
400
397
}
401
398
 
402
399
 
414
411
  if (arg_count <= 2)
415
412
    args=tmp_args;
416
413
  else
417
 
    if (!(args= (Item**) session->getMemRoot()->allocate(sizeof(Item*)*arg_count)))
 
414
    if (!(args= (Item**) session->alloc(sizeof(Item*)*arg_count)))
418
415
      return;
419
416
  memcpy(args, item->args, sizeof(Item*)*arg_count);
420
417
}
422
419
 
423
420
void Item_sum::mark_as_sum_func()
424
421
{
425
 
  Select_Lex *cur_select= getSession().getLex()->current_select;
 
422
  Select_Lex *cur_select= current_session->lex->current_select;
426
423
  cur_select->n_sum_items++;
427
424
  cur_select->with_sum_func= 1;
428
425
  with_sum_func= 1;
586
583
}
587
584
 
588
585
 
589
 
type::Decimal *Item_sum_num::val_decimal(type::Decimal *decimal_value)
 
586
my_decimal *Item_sum_num::val_decimal(my_decimal *decimal_value)
590
587
{
591
588
  return val_decimal_from_real(decimal_value);
592
589
}
599
596
}
600
597
 
601
598
 
602
 
type::Decimal *Item_sum_int::val_decimal(type::Decimal *decimal_value)
 
599
my_decimal *Item_sum_int::val_decimal(my_decimal *decimal_value)
603
600
{
604
601
  return val_decimal_from_int(decimal_value);
605
602
}
646
643
    sum_int= item->sum_int;
647
644
    break;
648
645
  case DECIMAL_RESULT:
649
 
    class_decimal2decimal(&item->sum_dec, &sum_dec);
 
646
    my_decimal2decimal(&item->sum_dec, &sum_dec);
650
647
    break;
651
648
  case REAL_RESULT:
652
649
    sum= item->sum;
686
683
    break;
687
684
  case DECIMAL_RESULT:
688
685
    max_length= item->max_length;
689
 
    sum_dec.set_zero();
 
686
    my_decimal_set_zero(&sum_dec);
690
687
    break;
691
688
  case REAL_RESULT:
692
689
    max_length= float_length(decimals);
726
723
  {
727
724
    field= ((Item_field*) args[0])->field;
728
725
 
729
 
    if ((field= create_tmp_field_from_field(&getSession(), field, name, table,
 
726
    if ((field= create_tmp_field_from_field(current_session, field, name, table,
730
727
                                            NULL, convert_blob_length)))
731
728
      field->flags&= ~NOT_NULL_FLAG;
732
729
    return field;
738
735
  */
739
736
  switch (args[0]->field_type()) {
740
737
  case DRIZZLE_TYPE_DATE:
741
 
    field= new Field_date(maybe_null, name);
 
738
    field= new Field_date(maybe_null, name, collation.collation);
742
739
    break;
743
740
  case DRIZZLE_TYPE_TIMESTAMP:
744
741
  case DRIZZLE_TYPE_DATETIME:
745
 
    field= new Field_datetime(maybe_null, name);
 
742
    field= new Field_datetime(maybe_null, name, collation.collation);
746
743
    break;
747
744
  default:
748
745
    return Item_sum::create_tmp_field(group, table, convert_blob_length);
749
746
  }
750
 
 
751
747
  if (field)
752
748
    field->init(table);
753
 
 
754
749
  return field;
755
750
}
756
751
 
770
765
  /* TODO: check if the following assignments are really needed */
771
766
  if (hybrid_type == DECIMAL_RESULT)
772
767
  {
773
 
    class_decimal2decimal(item->dec_buffs, dec_buffs);
774
 
    class_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
 
768
    my_decimal2decimal(item->dec_buffs, dec_buffs);
 
769
    my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
775
770
  }
776
771
  else
777
772
    sum= item->sum;
789
784
  if (hybrid_type == DECIMAL_RESULT)
790
785
  {
791
786
    curr_dec_buff= 0;
792
 
    dec_buffs->set_zero();
 
787
    my_decimal_set_zero(dec_buffs);
793
788
  }
794
789
  else
795
790
    sum= 0.0;
812
807
    {
813
808
      /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
814
809
      int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
815
 
      max_length= class_decimal_precision_to_length(precision, decimals,
 
810
      max_length= my_decimal_precision_to_length(precision, decimals,
816
811
                                                 unsigned_flag);
817
812
      curr_dec_buff= 0;
818
813
      hybrid_type= DECIMAL_RESULT;
819
 
      dec_buffs->set_zero();
 
814
      my_decimal_set_zero(dec_buffs);
820
815
      break;
821
816
    }
822
817
  case ROW_RESULT:
829
824
{
830
825
  if (hybrid_type == DECIMAL_RESULT)
831
826
  {
832
 
    type::Decimal value, *val= args[0]->val_decimal(&value);
 
827
    my_decimal value, *val= args[0]->val_decimal(&value);
833
828
    if (!args[0]->null_value)
834
829
    {
835
 
      class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
 
830
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
836
831
                     val, dec_buffs + curr_dec_buff);
837
832
      curr_dec_buff^= 1;
838
833
      null_value= 0;
854
849
  if (hybrid_type == DECIMAL_RESULT)
855
850
  {
856
851
    int64_t result;
857
 
    (dec_buffs + curr_dec_buff)->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
 
852
    my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
 
853
                   &result);
858
854
    return result;
859
855
  }
860
856
  return (int64_t) rint(val_real());
865
861
{
866
862
  assert(fixed == 1);
867
863
  if (hybrid_type == DECIMAL_RESULT)
868
 
    class_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
 
864
    my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
869
865
  return sum;
870
866
}
871
867
 
878
874
}
879
875
 
880
876
 
881
 
type::Decimal *Item_sum_sum::val_decimal(type::Decimal *val)
 
877
my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
882
878
{
883
879
  if (hybrid_type == DECIMAL_RESULT)
884
880
    return (dec_buffs + curr_dec_buff);
941
937
 
942
938
  virtual void div(Hybrid_type *val, uint64_t u) const
943
939
  {
944
 
    int2_class_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
 
940
    int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
945
941
    val->used_dec_buf_no= 0;
946
942
    val->traits= Hybrid_type_traits_decimal::instance();
947
943
    val->traits->div(val, u);
1135
1131
}
1136
1132
 
1137
1133
 
1138
 
type::Decimal *Item_sum_distinct::val_decimal(type::Decimal *to)
 
1134
my_decimal *Item_sum_distinct::val_decimal(my_decimal *to)
1139
1135
{
1140
1136
  calculate_val_and_count();
1141
1137
  if (null_value)
1167
1163
Item_sum_avg_distinct::fix_length_and_dec()
1168
1164
{
1169
1165
  Item_sum_distinct::fix_length_and_dec();
1170
 
  prec_increment= getSession().variables.div_precincrement;
 
1166
  prec_increment= current_session->variables.div_precincrement;
1171
1167
  /*
1172
1168
    AVG() will divide val by count. We need to reserve digits
1173
1169
    after decimal point as the result can be fractional.
1230
1226
{
1231
1227
  Item_sum_sum::fix_length_and_dec();
1232
1228
  maybe_null=null_value=1;
1233
 
  prec_increment= getSession().variables.div_precincrement;
1234
 
 
 
1229
  prec_increment= current_session->variables.div_precincrement;
1235
1230
  if (hybrid_type == DECIMAL_RESULT)
1236
1231
  {
1237
1232
    int precision= args[0]->decimal_precision() + prec_increment;
1238
1233
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1239
 
    max_length= class_decimal_precision_to_length(precision, decimals,
 
1234
    max_length= my_decimal_precision_to_length(precision, decimals,
1240
1235
                                               unsigned_flag);
1241
1236
    f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1242
1237
    f_scale=  args[0]->decimals;
1243
 
    dec_bin_size= class_decimal_get_binary_size(f_precision, f_scale);
 
1238
    dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1244
1239
  }
1245
1240
  else {
1246
1241
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1316
1311
}
1317
1312
 
1318
1313
 
1319
 
type::Decimal *Item_sum_avg::val_decimal(type::Decimal *val)
 
1314
my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
1320
1315
{
1321
 
  type::Decimal sum_buff, cnt;
1322
 
  const type::Decimal *sum_dec;
 
1316
  my_decimal sum_buff, cnt;
 
1317
  const my_decimal *sum_dec;
1323
1318
  assert(fixed == 1);
1324
1319
  if (!count)
1325
1320
  {
1335
1330
    return val_decimal_from_real(val);
1336
1331
 
1337
1332
  sum_dec= dec_buffs + curr_dec_buff;
1338
 
  int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
1339
 
  class_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
 
1333
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
 
1334
  my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1340
1335
  return val;
1341
1336
}
1342
1337
 
1428
1423
void Item_sum_variance::fix_length_and_dec()
1429
1424
{
1430
1425
  maybe_null= null_value= 1;
1431
 
  prec_increment= getSession().variables.div_precincrement;
 
1426
  prec_increment= current_session->variables.div_precincrement;
1432
1427
 
1433
1428
  /*
1434
1429
    According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1448
1443
    {
1449
1444
      int precision= args[0]->decimal_precision()*2 + prec_increment;
1450
1445
      decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1451
 
      max_length= class_decimal_precision_to_length(precision, decimals,
 
1446
      max_length= my_decimal_precision_to_length(precision, decimals,
1452
1447
                                                 unsigned_flag);
1453
1448
 
1454
1449
      break;
1544
1539
}
1545
1540
 
1546
1541
 
1547
 
type::Decimal *Item_sum_variance::val_decimal(type::Decimal *dec_buf)
 
1542
my_decimal *Item_sum_variance::val_decimal(my_decimal *dec_buf)
1548
1543
{
1549
1544
  assert(fixed == 1);
1550
1545
  return val_decimal_from_real(dec_buf);
1608
1603
    sum_int= 0;
1609
1604
    break;
1610
1605
  case DECIMAL_RESULT:
1611
 
    sum_dec.set_zero();
 
1606
    my_decimal_set_zero(&sum_dec);
1612
1607
    break;
1613
1608
  case REAL_RESULT:
1614
1609
    sum= 0.0;
1637
1632
  case INT_RESULT:
1638
1633
    return (double) sum_int;
1639
1634
  case DECIMAL_RESULT:
1640
 
    class_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
 
1635
    my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1641
1636
    return sum;
1642
1637
  case REAL_RESULT:
1643
1638
    return sum;
1661
1656
  case DECIMAL_RESULT:
1662
1657
  {
1663
1658
    int64_t result;
1664
 
    sum_dec.val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
 
1659
    my_decimal2int(E_DEC_FATAL_ERROR, &sum_dec, unsigned_flag, &result);
1665
1660
    return sum_int;
1666
1661
  }
1667
1662
  default:
1670
1665
}
1671
1666
 
1672
1667
 
1673
 
type::Decimal *Item_sum_hybrid::val_decimal(type::Decimal *val)
 
1668
my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
1674
1669
{
1675
1670
  assert(fixed == 1);
1676
1671
  if (null_value)
1678
1673
 
1679
1674
  switch (hybrid_type) {
1680
1675
  case STRING_RESULT:
1681
 
    val->store(E_DEC_FATAL_ERROR, &value);
 
1676
    string2my_decimal(E_DEC_FATAL_ERROR, &value, val);
1682
1677
    break;
1683
1678
  case REAL_RESULT:
1684
 
    double2_class_decimal(E_DEC_FATAL_ERROR, sum, val);
 
1679
    double2my_decimal(E_DEC_FATAL_ERROR, sum, val);
1685
1680
    break;
1686
1681
  case DECIMAL_RESULT:
1687
1682
    val= &sum_dec;
1688
1683
    break;
1689
1684
  case INT_RESULT:
1690
 
    int2_class_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
 
1685
    int2my_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1691
1686
    break;
1692
1687
  case ROW_RESULT:
1693
1688
    // This case should never be choosen
1713
1708
    str->set_real(sum,decimals, &my_charset_bin);
1714
1709
    break;
1715
1710
  case DECIMAL_RESULT:
1716
 
    class_decimal2string(&sum_dec, 0, str);
 
1711
    my_decimal2string(E_DEC_FATAL_ERROR, &sum_dec, 0, 0, 0, str);
1717
1712
    return str;
1718
1713
  case INT_RESULT:
1719
1714
    str->set_int(sum_int, unsigned_flag, &my_charset_bin);
1786
1781
    break;
1787
1782
  case DECIMAL_RESULT:
1788
1783
    {
1789
 
      type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1784
      my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1790
1785
      if (!args[0]->null_value &&
1791
 
          (null_value || (class_decimal_cmp(&sum_dec, val) > 0)))
 
1786
          (null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
1792
1787
      {
1793
 
        class_decimal2decimal(val, &sum_dec);
 
1788
        my_decimal2decimal(val, &sum_dec);
1794
1789
        null_value= 0;
1795
1790
      }
1796
1791
    }
1849
1844
    break;
1850
1845
  case DECIMAL_RESULT:
1851
1846
    {
1852
 
      type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1847
      my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1853
1848
      if (!args[0]->null_value &&
1854
 
          (null_value || (class_decimal_cmp(val, &sum_dec) > 0)))
 
1849
          (null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
1855
1850
      {
1856
 
        class_decimal2decimal(val, &sum_dec);
 
1851
        my_decimal2decimal(val, &sum_dec);
1857
1852
        null_value= 0;
1858
1853
      }
1859
1854
    }
2014
2009
    }
2015
2010
  case DECIMAL_RESULT:
2016
2011
    {
2017
 
      type::Decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
 
2012
      my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2018
2013
 
2019
2014
      if (maybe_null)
2020
2015
      {
2042
2037
{
2043
2038
  if (hybrid_type == DECIMAL_RESULT)
2044
2039
  {
2045
 
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
 
2040
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2046
2041
    if (!arg_val)                               // Null
2047
2042
      arg_val= &decimal_zero;
2048
2043
    result_field->store_decimal(arg_val);
2077
2072
  if (hybrid_type == DECIMAL_RESULT)
2078
2073
  {
2079
2074
    int64_t tmp;
2080
 
    type::Decimal value, *arg_dec= args[0]->val_decimal(&value);
 
2075
    my_decimal value, *arg_dec= args[0]->val_decimal(&value);
2081
2076
    if (args[0]->null_value)
2082
2077
    {
2083
2078
      arg_dec= &decimal_zero;
2085
2080
    }
2086
2081
    else
2087
2082
      tmp= 1;
2088
 
    arg_dec->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
 
2083
    my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
2089
2084
    res+= dec_bin_size;
2090
2085
    int8store(res, tmp);
2091
2086
  }
2129
2124
{
2130
2125
  if (hybrid_type == DECIMAL_RESULT)
2131
2126
  {
2132
 
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
 
2127
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2133
2128
    if (!args[0]->null_value)
2134
2129
    {
2135
2130
      if (!result_field->is_null())
2136
2131
      {
2137
 
        type::Decimal field_value,
 
2132
        my_decimal field_value,
2138
2133
                   *field_val= result_field->val_decimal(&field_value);
2139
 
        class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
 
2134
        my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2140
2135
        result_field->store_decimal(dec_buffs);
2141
2136
      }
2142
2137
      else
2181
2176
  unsigned char *res=result_field->ptr;
2182
2177
  if (hybrid_type == DECIMAL_RESULT)
2183
2178
  {
2184
 
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
 
2179
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2185
2180
    if (!args[0]->null_value)
2186
2181
    {
2187
 
      binary2_class_decimal(E_DEC_FATAL_ERROR, res,
 
2182
      binary2my_decimal(E_DEC_FATAL_ERROR, res,
2188
2183
                        dec_buffs + 1, f_precision, f_scale);
2189
2184
      field_count= sint8korr(res + dec_bin_size);
2190
 
      class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
2191
 
      dec_buffs->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
 
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);
2192
2188
      res+= dec_bin_size;
2193
2189
      field_count++;
2194
2190
      int8store(res, field_count);
2306
2302
Item_sum_hybrid::min_max_update_decimal_field()
2307
2303
{
2308
2304
  /* TODO: optimize: do not get result_field in case of args[0] is NULL */
2309
 
  type::Decimal old_val, nr_val;
2310
 
  const type::Decimal *old_nr= result_field->val_decimal(&old_val);
2311
 
  const type::Decimal *nr= args[0]->val_decimal(&nr_val);
 
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);
2312
2308
  if (!args[0]->null_value)
2313
2309
  {
2314
2310
    if (result_field->is_null(0))
2315
2311
      old_nr=nr;
2316
2312
    else
2317
2313
    {
2318
 
      bool res= class_decimal_cmp(old_nr, nr) > 0;
 
2314
      bool res= my_decimal_cmp(old_nr, nr) > 0;
2319
2315
      /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2320
2316
      if ((cmp_sign > 0) ^ (!res))
2321
2317
        old_nr=nr;
2372
2368
}
2373
2369
 
2374
2370
 
2375
 
type::Decimal *Item_avg_field::val_decimal(type::Decimal *dec_buf)
 
2371
my_decimal *Item_avg_field::val_decimal(my_decimal *dec_buf)
2376
2372
{
2377
2373
  // fix_fields() never calls for this Item
2378
2374
  if (hybrid_type == REAL_RESULT)
2382
2378
  if ((null_value= !count))
2383
2379
    return 0;
2384
2380
 
2385
 
  type::Decimal dec_count, dec_field;
2386
 
  binary2_class_decimal(E_DEC_FATAL_ERROR,
 
2381
  my_decimal dec_count, dec_field;
 
2382
  binary2my_decimal(E_DEC_FATAL_ERROR,
2387
2383
                    field->ptr, &dec_field, f_precision, f_scale);
2388
 
  int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
2389
 
  class_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
 
2384
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
 
2385
  my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2390
2386
                 &dec_field, &dec_count, prec_increment);
2391
2387
  return dec_buf;
2392
2388
}
2417
2413
}
2418
2414
 
2419
2415
 
2420
 
type::Decimal *Item_std_field::val_decimal(type::Decimal *dec_buf)
 
2416
my_decimal *Item_std_field::val_decimal(my_decimal *dec_buf)
2421
2417
{
2422
2418
  /*
2423
2419
    We can't call val_decimal_from_real() for DECIMAL_RESULT as
2424
2420
    Item_variance_field::val_real() would cause an infinite loop
2425
2421
  */
2426
 
  type::Decimal tmp_dec, *dec;
 
2422
  my_decimal tmp_dec, *dec;
2427
2423
  double nr;
2428
2424
  if (hybrid_type == REAL_RESULT)
2429
2425
    return val_decimal_from_real(dec_buf);
2431
2427
  dec= Item_variance_field::val_decimal(dec_buf);
2432
2428
  if (!dec)
2433
2429
    return 0;
2434
 
  class_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
 
2430
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2435
2431
  assert(nr >= 0.0);
2436
2432
  nr= sqrt(nr);
2437
 
  double2_class_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2438
 
  class_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
 
2433
  double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
 
2434
  my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
2439
2435
  return dec_buf;
2440
2436
}
2441
2437
 
2584
2580
bool Item_sum_count_distinct::setup(Session *session)
2585
2581
{
2586
2582
  List<Item> list;
2587
 
  Select_Lex *select_lex= session->getLex()->current_select;
 
2583
  Select_Lex *select_lex= session->lex->current_select;
2588
2584
 
2589
2585
  /*
2590
2586
    Setup can be called twice for ROLLUP items. This is a bug.
2669
2665
        uint32_t *length;
2670
2666
        compare_key= (qsort_cmp2) composite_key_cmp;
2671
2667
        cmp_arg= (void*) this;
2672
 
        field_lengths= (uint32_t*) session->getMemRoot()->allocate(table->getShare()->sizeFields() * sizeof(uint32_t));
 
2668
        field_lengths= (uint32_t*) session->alloc(table->getShare()->sizeFields() * sizeof(uint32_t));
2673
2669
        for (tree_key_length= 0, length= field_lengths, field= table->getFields();
2674
2670
             field < field_end; ++field, ++length)
2675
2671
        {
2998
2994
  order= (Order**)(args + arg_count);
2999
2995
 
3000
2996
  /* fill args items of show and sort */
3001
 
  List<Item>::iterator li(select_list->begin());
 
2997
  List_iterator_fast<Item> li(*select_list);
3002
2998
 
3003
2999
  for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
3004
3000
    *arg_ptr= item_select;
3053
3049
  {
3054
3050
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
3055
3051
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3056
 
    warning->set_msg(&getSession(), warn_buff);
 
3052
    warning->set_msg(current_session, warn_buff);
3057
3053
    warning= 0;
3058
3054
  }
3059
3055
 
3208
3204
bool Item_func_group_concat::setup(Session *session)
3209
3205
{
3210
3206
  List<Item> list;
3211
 
  Select_Lex *select_lex= session->getLex()->current_select;
 
3207
  Select_Lex *select_lex= session->lex->current_select;
3212
3208
 
3213
3209
  /*
3214
3210
    Currently setup() can be called twice. Please add