~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/sum.cc

Refactor

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>
41
44
 
42
45
#include <algorithm>
43
46
 
44
47
using namespace std;
45
48
 
46
 
namespace drizzled
47
 
{
 
49
namespace drizzled {
48
50
 
49
 
extern my_decimal decimal_zero;
50
51
extern plugin::StorageEngine *heap_engine;
51
52
 
52
53
/**
73
74
 
74
75
bool Item_sum::init_sum_func_check(Session *session)
75
76
{
76
 
  if (!session->lex->allow_sum_func)
 
77
  if (!session->lex().allow_sum_func)
77
78
  {
78
79
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
79
80
               MYF(0));
80
81
    return true;
81
82
  }
82
83
  /* Set a reference to the nesting set function if there is  any */
83
 
  in_sum_func= session->lex->in_sum_func;
 
84
  in_sum_func= session->lex().in_sum_func;
84
85
  /* 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;
 
86
  session->lex().in_sum_func= this;
 
87
  nest_level= session->lex().current_select->nest_level;
87
88
  ref_by= 0;
88
89
  aggr_level= -1;
89
90
  aggr_sel= NULL;
90
91
  max_arg_level= -1;
91
92
  max_sum_func_level= -1;
92
 
  outer_fields.empty();
 
93
  outer_fields.clear();
93
94
  return false;
94
95
}
95
96
 
145
146
bool Item_sum::check_sum_func(Session *session, Item **ref)
146
147
{
147
148
  bool invalid= false;
148
 
  nesting_map allow_sum_func= session->lex->allow_sum_func;
 
149
  nesting_map allow_sum_func= session->lex().allow_sum_func;
149
150
  /*
150
151
    The value of max_arg_level is updated if an argument of the set function
151
152
    contains a column reference resolved  against a subquery whose level is
178
179
  if (!invalid && aggr_level < 0)
179
180
  {
180
181
    aggr_level= nest_level;
181
 
    aggr_sel= session->lex->current_select;
 
182
    aggr_sel= session->lex().current_select;
182
183
  }
183
184
  /*
184
185
    By this moment we either found a subquery where the set function is
224
225
    Check that non-aggregated fields and sum functions aren't mixed in the
225
226
    same select in the ONLY_FULL_GROUP_BY mode.
226
227
  */
227
 
  if (outer_fields.elements)
 
228
  if (outer_fields.size())
228
229
  {
229
230
    Item_field *field;
230
231
    /*
253
254
        select the field belongs to. If there are some then an error is
254
255
        raised.
255
256
    */
256
 
    List_iterator<Item_field> of(outer_fields);
 
257
    List<Item_field>::iterator of(outer_fields.begin());
257
258
    while ((field= of++))
258
259
    {
259
260
      Select_Lex *sel= field->cached_table->select_lex;
284
285
  }
285
286
  aggr_sel->full_group_by_flag.set(SUM_FUNC_USED);
286
287
  update_used_tables();
287
 
  session->lex->in_sum_func= in_sum_func;
 
288
  session->lex().in_sum_func= in_sum_func;
288
289
  return false;
289
290
}
290
291
 
316
317
bool Item_sum::register_sum_func(Session *session, Item **ref)
317
318
{
318
319
  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() ;
 
320
  nesting_map allow_sum_func= session->lex().allow_sum_func;
 
321
  for (sl= session->lex().current_select->master_unit()->outer_select() ;
321
322
       sl && sl->nest_level > max_arg_level;
322
323
       sl= sl->master_unit()->outer_select() )
323
324
  {
368
369
      with_sum_func being set for an Select_Lex means that this Select_Lex
369
370
      has aggregate functions directly referenced (i.e. not through a sub-select).
370
371
    */
371
 
    for (sl= session->lex->current_select;
 
372
    for (sl= session->lex().current_select;
372
373
         sl && sl != aggr_sel && sl->master_unit()->item;
373
374
         sl= sl->master_unit()->outer_select() )
374
375
      sl->master_unit()->item->with_sum_func= 1;
375
376
  }
376
 
  session->lex->current_select->mark_as_dependent(aggr_sel);
 
377
  session->lex().current_select->mark_as_dependent(aggr_sel);
377
378
  return false;
378
379
}
379
380
 
380
381
 
381
 
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements),
 
382
Item_sum::Item_sum(List<Item> &list) :arg_count(list.size()),
382
383
  forced_const(false)
383
384
{
384
385
  if ((args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
385
386
  {
386
387
    uint32_t i=0;
387
 
    List_iterator_fast<Item> li(list);
 
388
    List<Item>::iterator li(list.begin());
388
389
    Item *item;
389
390
 
390
391
    while ((item=li++))
393
394
    }
394
395
  }
395
396
  mark_as_sum_func();
396
 
  list.empty();                                 // Fields are used
 
397
  list.clear();                                 // Fields are used
397
398
}
398
399
 
399
400
 
411
412
  if (arg_count <= 2)
412
413
    args=tmp_args;
413
414
  else
414
 
    if (!(args= (Item**) session->alloc(sizeof(Item*)*arg_count)))
 
415
    if (!(args= (Item**) session->getMemRoot()->allocate(sizeof(Item*)*arg_count)))
415
416
      return;
416
417
  memcpy(args, item->args, sizeof(Item*)*arg_count);
417
418
}
419
420
 
420
421
void Item_sum::mark_as_sum_func()
421
422
{
422
 
  Select_Lex *cur_select= current_session->lex->current_select;
 
423
  Select_Lex *cur_select= getSession().lex().current_select;
423
424
  cur_select->n_sum_items++;
424
425
  cur_select->with_sum_func= 1;
425
426
  with_sum_func= 1;
446
447
}
447
448
 
448
449
 
449
 
void Item_sum::print(String *str, enum_query_type query_type)
 
450
void Item_sum::print(String *str)
450
451
{
451
452
  str->append(func_name());
452
453
  for (uint32_t i=0 ; i < arg_count ; i++)
453
454
  {
454
455
    if (i)
455
456
      str->append(',');
456
 
    args[i]->print(str, query_type);
 
457
    args[i]->print(str);
457
458
  }
458
459
  str->append(')');
459
460
}
583
584
}
584
585
 
585
586
 
586
 
my_decimal *Item_sum_num::val_decimal(my_decimal *decimal_value)
 
587
type::Decimal *Item_sum_num::val_decimal(type::Decimal *decimal_value)
587
588
{
588
589
  return val_decimal_from_real(decimal_value);
589
590
}
596
597
}
597
598
 
598
599
 
599
 
my_decimal *Item_sum_int::val_decimal(my_decimal *decimal_value)
 
600
type::Decimal *Item_sum_int::val_decimal(type::Decimal *decimal_value)
600
601
{
601
602
  return val_decimal_from_int(decimal_value);
602
603
}
643
644
    sum_int= item->sum_int;
644
645
    break;
645
646
  case DECIMAL_RESULT:
646
 
    my_decimal2decimal(&item->sum_dec, &sum_dec);
 
647
    class_decimal2decimal(&item->sum_dec, &sum_dec);
647
648
    break;
648
649
  case REAL_RESULT:
649
650
    sum= item->sum;
683
684
    break;
684
685
  case DECIMAL_RESULT:
685
686
    max_length= item->max_length;
686
 
    my_decimal_set_zero(&sum_dec);
 
687
    sum_dec.set_zero();
687
688
    break;
688
689
  case REAL_RESULT:
689
690
    max_length= float_length(decimals);
723
724
  {
724
725
    field= ((Item_field*) args[0])->field;
725
726
 
726
 
    if ((field= create_tmp_field_from_field(current_session, field, name, table,
 
727
    if ((field= create_tmp_field_from_field(&getSession(), field, name, table,
727
728
                                            NULL, convert_blob_length)))
728
729
      field->flags&= ~NOT_NULL_FLAG;
729
730
    return field;
735
736
  */
736
737
  switch (args[0]->field_type()) {
737
738
  case DRIZZLE_TYPE_DATE:
738
 
    field= new Field_date(maybe_null, name, collation.collation);
 
739
    field= new Field_date(maybe_null, name);
739
740
    break;
740
741
  case DRIZZLE_TYPE_TIMESTAMP:
741
742
  case DRIZZLE_TYPE_DATETIME:
742
 
    field= new Field_datetime(maybe_null, name, collation.collation);
 
743
    field= new Field_datetime(maybe_null, name);
743
744
    break;
744
745
  default:
745
746
    return Item_sum::create_tmp_field(group, table, convert_blob_length);
746
747
  }
 
748
 
747
749
  if (field)
748
750
    field->init(table);
 
751
 
749
752
  return field;
750
753
}
751
754
 
765
768
  /* TODO: check if the following assignments are really needed */
766
769
  if (hybrid_type == DECIMAL_RESULT)
767
770
  {
768
 
    my_decimal2decimal(item->dec_buffs, dec_buffs);
769
 
    my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
 
771
    class_decimal2decimal(item->dec_buffs, dec_buffs);
 
772
    class_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
770
773
  }
771
774
  else
772
775
    sum= item->sum;
784
787
  if (hybrid_type == DECIMAL_RESULT)
785
788
  {
786
789
    curr_dec_buff= 0;
787
 
    my_decimal_set_zero(dec_buffs);
 
790
    dec_buffs->set_zero();
788
791
  }
789
792
  else
790
793
    sum= 0.0;
807
810
    {
808
811
      /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
809
812
      int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
810
 
      max_length= my_decimal_precision_to_length(precision, decimals,
 
813
      max_length= class_decimal_precision_to_length(precision, decimals,
811
814
                                                 unsigned_flag);
812
815
      curr_dec_buff= 0;
813
816
      hybrid_type= DECIMAL_RESULT;
814
 
      my_decimal_set_zero(dec_buffs);
 
817
      dec_buffs->set_zero();
815
818
      break;
816
819
    }
817
820
  case ROW_RESULT:
824
827
{
825
828
  if (hybrid_type == DECIMAL_RESULT)
826
829
  {
827
 
    my_decimal value, *val= args[0]->val_decimal(&value);
 
830
    type::Decimal value, *val= args[0]->val_decimal(&value);
828
831
    if (!args[0]->null_value)
829
832
    {
830
 
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
 
833
      class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
831
834
                     val, dec_buffs + curr_dec_buff);
832
835
      curr_dec_buff^= 1;
833
836
      null_value= 0;
849
852
  if (hybrid_type == DECIMAL_RESULT)
850
853
  {
851
854
    int64_t result;
852
 
    my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
853
 
                   &result);
 
855
    (dec_buffs + curr_dec_buff)->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
854
856
    return result;
855
857
  }
856
858
  return (int64_t) rint(val_real());
861
863
{
862
864
  assert(fixed == 1);
863
865
  if (hybrid_type == DECIMAL_RESULT)
864
 
    my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
 
866
    class_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
865
867
  return sum;
866
868
}
867
869
 
874
876
}
875
877
 
876
878
 
877
 
my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
 
879
type::Decimal *Item_sum_sum::val_decimal(type::Decimal *val)
878
880
{
879
881
  if (hybrid_type == DECIMAL_RESULT)
880
882
    return (dec_buffs + curr_dec_buff);
937
939
 
938
940
  virtual void div(Hybrid_type *val, uint64_t u) const
939
941
  {
940
 
    int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
 
942
    int2_class_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
941
943
    val->used_dec_buf_no= 0;
942
944
    val->traits= Hybrid_type_traits_decimal::instance();
943
945
    val->traits->div(val, u);
1020
1022
    PS/SP. Hence all further allocations are performed in the runtime
1021
1023
    mem_root.
1022
1024
  */
1023
 
  if (field_list.push_back(&field_def))
1024
 
    return(true);
1025
 
 
 
1025
  field_list.push_back(&field_def);
1026
1026
  null_value= maybe_null= 1;
1027
1027
  quick_group= 0;
1028
1028
 
1131
1131
}
1132
1132
 
1133
1133
 
1134
 
my_decimal *Item_sum_distinct::val_decimal(my_decimal *to)
 
1134
type::Decimal *Item_sum_distinct::val_decimal(type::Decimal *to)
1135
1135
{
1136
1136
  calculate_val_and_count();
1137
1137
  if (null_value)
1163
1163
Item_sum_avg_distinct::fix_length_and_dec()
1164
1164
{
1165
1165
  Item_sum_distinct::fix_length_and_dec();
1166
 
  prec_increment= current_session->variables.div_precincrement;
 
1166
  prec_increment= getSession().variables.div_precincrement;
1167
1167
  /*
1168
1168
    AVG() will divide val by count. We need to reserve digits
1169
1169
    after decimal point as the result can be fractional.
1226
1226
{
1227
1227
  Item_sum_sum::fix_length_and_dec();
1228
1228
  maybe_null=null_value=1;
1229
 
  prec_increment= current_session->variables.div_precincrement;
 
1229
  prec_increment= getSession().variables.div_precincrement;
 
1230
 
1230
1231
  if (hybrid_type == DECIMAL_RESULT)
1231
1232
  {
1232
1233
    int precision= args[0]->decimal_precision() + prec_increment;
1233
1234
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1234
 
    max_length= my_decimal_precision_to_length(precision, decimals,
 
1235
    max_length= class_decimal_precision_to_length(precision, decimals,
1235
1236
                                               unsigned_flag);
1236
1237
    f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1237
1238
    f_scale=  args[0]->decimals;
1238
 
    dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
 
1239
    dec_bin_size= class_decimal_get_binary_size(f_precision, f_scale);
1239
1240
  }
1240
1241
  else {
1241
1242
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1311
1312
}
1312
1313
 
1313
1314
 
1314
 
my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
 
1315
type::Decimal *Item_sum_avg::val_decimal(type::Decimal *val)
1315
1316
{
1316
 
  my_decimal sum_buff, cnt;
1317
 
  const my_decimal *sum_dec;
 
1317
  type::Decimal sum_buff, cnt;
 
1318
  const type::Decimal *sum_dec;
1318
1319
  assert(fixed == 1);
1319
1320
  if (!count)
1320
1321
  {
1330
1331
    return val_decimal_from_real(val);
1331
1332
 
1332
1333
  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);
 
1334
  int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
 
1335
  class_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1335
1336
  return val;
1336
1337
}
1337
1338
 
1423
1424
void Item_sum_variance::fix_length_and_dec()
1424
1425
{
1425
1426
  maybe_null= null_value= 1;
1426
 
  prec_increment= current_session->variables.div_precincrement;
 
1427
  prec_increment= getSession().variables.div_precincrement;
1427
1428
 
1428
1429
  /*
1429
1430
    According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1443
1444
    {
1444
1445
      int precision= args[0]->decimal_precision()*2 + prec_increment;
1445
1446
      decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1446
 
      max_length= my_decimal_precision_to_length(precision, decimals,
 
1447
      max_length= class_decimal_precision_to_length(precision, decimals,
1447
1448
                                                 unsigned_flag);
1448
1449
 
1449
1450
      break;
1539
1540
}
1540
1541
 
1541
1542
 
1542
 
my_decimal *Item_sum_variance::val_decimal(my_decimal *dec_buf)
 
1543
type::Decimal *Item_sum_variance::val_decimal(type::Decimal *dec_buf)
1543
1544
{
1544
1545
  assert(fixed == 1);
1545
1546
  return val_decimal_from_real(dec_buf);
1603
1604
    sum_int= 0;
1604
1605
    break;
1605
1606
  case DECIMAL_RESULT:
1606
 
    my_decimal_set_zero(&sum_dec);
 
1607
    sum_dec.set_zero();
1607
1608
    break;
1608
1609
  case REAL_RESULT:
1609
1610
    sum= 0.0;
1632
1633
  case INT_RESULT:
1633
1634
    return (double) sum_int;
1634
1635
  case DECIMAL_RESULT:
1635
 
    my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
 
1636
    class_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1636
1637
    return sum;
1637
1638
  case REAL_RESULT:
1638
1639
    return sum;
1656
1657
  case DECIMAL_RESULT:
1657
1658
  {
1658
1659
    int64_t result;
1659
 
    my_decimal2int(E_DEC_FATAL_ERROR, &sum_dec, unsigned_flag, &result);
 
1660
    sum_dec.val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
1660
1661
    return sum_int;
1661
1662
  }
1662
1663
  default:
1665
1666
}
1666
1667
 
1667
1668
 
1668
 
my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
 
1669
type::Decimal *Item_sum_hybrid::val_decimal(type::Decimal *val)
1669
1670
{
1670
1671
  assert(fixed == 1);
1671
1672
  if (null_value)
1673
1674
 
1674
1675
  switch (hybrid_type) {
1675
1676
  case STRING_RESULT:
1676
 
    string2my_decimal(E_DEC_FATAL_ERROR, &value, val);
 
1677
    val->store(E_DEC_FATAL_ERROR, &value);
1677
1678
    break;
1678
1679
  case REAL_RESULT:
1679
 
    double2my_decimal(E_DEC_FATAL_ERROR, sum, val);
 
1680
    double2_class_decimal(E_DEC_FATAL_ERROR, sum, val);
1680
1681
    break;
1681
1682
  case DECIMAL_RESULT:
1682
1683
    val= &sum_dec;
1683
1684
    break;
1684
1685
  case INT_RESULT:
1685
 
    int2my_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
 
1686
    int2_class_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1686
1687
    break;
1687
1688
  case ROW_RESULT:
1688
1689
    // This case should never be choosen
1708
1709
    str->set_real(sum,decimals, &my_charset_bin);
1709
1710
    break;
1710
1711
  case DECIMAL_RESULT:
1711
 
    my_decimal2string(E_DEC_FATAL_ERROR, &sum_dec, 0, 0, 0, str);
 
1712
    class_decimal2string(&sum_dec, 0, str);
1712
1713
    return str;
1713
1714
  case INT_RESULT:
1714
1715
    str->set_int(sum_int, unsigned_flag, &my_charset_bin);
1781
1782
    break;
1782
1783
  case DECIMAL_RESULT:
1783
1784
    {
1784
 
      my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1785
      type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1785
1786
      if (!args[0]->null_value &&
1786
 
          (null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
 
1787
          (null_value || (class_decimal_cmp(&sum_dec, val) > 0)))
1787
1788
      {
1788
 
        my_decimal2decimal(val, &sum_dec);
 
1789
        class_decimal2decimal(val, &sum_dec);
1789
1790
        null_value= 0;
1790
1791
      }
1791
1792
    }
1844
1845
    break;
1845
1846
  case DECIMAL_RESULT:
1846
1847
    {
1847
 
      my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1848
      type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1848
1849
      if (!args[0]->null_value &&
1849
 
          (null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
 
1850
          (null_value || (class_decimal_cmp(val, &sum_dec) > 0)))
1850
1851
      {
1851
 
        my_decimal2decimal(val, &sum_dec);
 
1852
        class_decimal2decimal(val, &sum_dec);
1852
1853
        null_value= 0;
1853
1854
      }
1854
1855
    }
2009
2010
    }
2010
2011
  case DECIMAL_RESULT:
2011
2012
    {
2012
 
      my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
 
2013
      type::Decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2013
2014
 
2014
2015
      if (maybe_null)
2015
2016
      {
2037
2038
{
2038
2039
  if (hybrid_type == DECIMAL_RESULT)
2039
2040
  {
2040
 
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
 
2041
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2041
2042
    if (!arg_val)                               // Null
2042
2043
      arg_val= &decimal_zero;
2043
2044
    result_field->store_decimal(arg_val);
2072
2073
  if (hybrid_type == DECIMAL_RESULT)
2073
2074
  {
2074
2075
    int64_t tmp;
2075
 
    my_decimal value, *arg_dec= args[0]->val_decimal(&value);
 
2076
    type::Decimal value, *arg_dec= args[0]->val_decimal(&value);
2076
2077
    if (args[0]->null_value)
2077
2078
    {
2078
2079
      arg_dec= &decimal_zero;
2080
2081
    }
2081
2082
    else
2082
2083
      tmp= 1;
2083
 
    my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
 
2084
    arg_dec->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
2084
2085
    res+= dec_bin_size;
2085
2086
    int8store(res, tmp);
2086
2087
  }
2124
2125
{
2125
2126
  if (hybrid_type == DECIMAL_RESULT)
2126
2127
  {
2127
 
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
 
2128
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2128
2129
    if (!args[0]->null_value)
2129
2130
    {
2130
2131
      if (!result_field->is_null())
2131
2132
      {
2132
 
        my_decimal field_value,
 
2133
        type::Decimal field_value,
2133
2134
                   *field_val= result_field->val_decimal(&field_value);
2134
 
        my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
 
2135
        class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2135
2136
        result_field->store_decimal(dec_buffs);
2136
2137
      }
2137
2138
      else
2176
2177
  unsigned char *res=result_field->ptr;
2177
2178
  if (hybrid_type == DECIMAL_RESULT)
2178
2179
  {
2179
 
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
 
2180
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2180
2181
    if (!args[0]->null_value)
2181
2182
    {
2182
 
      binary2my_decimal(E_DEC_FATAL_ERROR, res,
 
2183
      binary2_class_decimal(E_DEC_FATAL_ERROR, res,
2183
2184
                        dec_buffs + 1, f_precision, f_scale);
2184
2185
      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);
 
2186
      class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
 
2187
      dec_buffs->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
2188
2188
      res+= dec_bin_size;
2189
2189
      field_count++;
2190
2190
      int8store(res, field_count);
2302
2302
Item_sum_hybrid::min_max_update_decimal_field()
2303
2303
{
2304
2304
  /* 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);
 
2305
  type::Decimal old_val, nr_val;
 
2306
  const type::Decimal *old_nr= result_field->val_decimal(&old_val);
 
2307
  const type::Decimal *nr= args[0]->val_decimal(&nr_val);
2308
2308
  if (!args[0]->null_value)
2309
2309
  {
2310
2310
    if (result_field->is_null(0))
2311
2311
      old_nr=nr;
2312
2312
    else
2313
2313
    {
2314
 
      bool res= my_decimal_cmp(old_nr, nr) > 0;
 
2314
      bool res= class_decimal_cmp(old_nr, nr) > 0;
2315
2315
      /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2316
2316
      if ((cmp_sign > 0) ^ (!res))
2317
2317
        old_nr=nr;
2368
2368
}
2369
2369
 
2370
2370
 
2371
 
my_decimal *Item_avg_field::val_decimal(my_decimal *dec_buf)
 
2371
type::Decimal *Item_avg_field::val_decimal(type::Decimal *dec_buf)
2372
2372
{
2373
2373
  // fix_fields() never calls for this Item
2374
2374
  if (hybrid_type == REAL_RESULT)
2378
2378
  if ((null_value= !count))
2379
2379
    return 0;
2380
2380
 
2381
 
  my_decimal dec_count, dec_field;
2382
 
  binary2my_decimal(E_DEC_FATAL_ERROR,
 
2381
  type::Decimal dec_count, dec_field;
 
2382
  binary2_class_decimal(E_DEC_FATAL_ERROR,
2383
2383
                    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,
 
2384
  int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
 
2385
  class_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2386
2386
                 &dec_field, &dec_count, prec_increment);
2387
2387
  return dec_buf;
2388
2388
}
2413
2413
}
2414
2414
 
2415
2415
 
2416
 
my_decimal *Item_std_field::val_decimal(my_decimal *dec_buf)
 
2416
type::Decimal *Item_std_field::val_decimal(type::Decimal *dec_buf)
2417
2417
{
2418
2418
  /*
2419
2419
    We can't call val_decimal_from_real() for DECIMAL_RESULT as
2420
2420
    Item_variance_field::val_real() would cause an infinite loop
2421
2421
  */
2422
 
  my_decimal tmp_dec, *dec;
 
2422
  type::Decimal tmp_dec, *dec;
2423
2423
  double nr;
2424
2424
  if (hybrid_type == REAL_RESULT)
2425
2425
    return val_decimal_from_real(dec_buf);
2427
2427
  dec= Item_variance_field::val_decimal(dec_buf);
2428
2428
  if (!dec)
2429
2429
    return 0;
2430
 
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
 
2430
  class_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2431
2431
  assert(nr >= 0.0);
2432
2432
  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);
 
2433
  double2_class_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
 
2434
  class_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
2435
2435
  return dec_buf;
2436
2436
}
2437
2437
 
2580
2580
bool Item_sum_count_distinct::setup(Session *session)
2581
2581
{
2582
2582
  List<Item> list;
2583
 
  Select_Lex *select_lex= session->lex->current_select;
 
2583
  Select_Lex *select_lex= session->lex().current_select;
2584
2584
 
2585
2585
  /*
2586
2586
    Setup can be called twice for ROLLUP items. This is a bug.
2597
2597
  for (uint32_t i=0; i < arg_count ; i++)
2598
2598
  {
2599
2599
    Item *item=args[i];
2600
 
    if (list.push_back(item))
2601
 
      return true;                              // End of memory
 
2600
    list.push_back(item);
2602
2601
    if (item->const_item() && item->is_null())
2603
2602
      always_null= 1;
2604
2603
  }
2665
2664
        uint32_t *length;
2666
2665
        compare_key= (qsort_cmp2) composite_key_cmp;
2667
2666
        cmp_arg= (void*) this;
2668
 
        field_lengths= (uint32_t*) session->alloc(table->getShare()->sizeFields() * sizeof(uint32_t));
 
2667
        field_lengths= (uint32_t*) session->getMemRoot()->allocate(table->getShare()->sizeFields() * sizeof(uint32_t));
2669
2668
        for (tree_key_length= 0, length= field_lengths, field= table->getFields();
2670
2669
             field < field_end; ++field, ++length)
2671
2670
        {
2969
2968
   separator(separator_arg), tree(NULL), unique_filter(NULL), table(0),
2970
2969
   order(0), context(context_arg),
2971
2970
   arg_count_order(order_list ? order_list->elements : 0),
2972
 
   arg_count_field(select_list->elements),
 
2971
   arg_count_field(select_list->size()),
2973
2972
   count_cut_values(0),
2974
2973
   distinct(distinct_arg),
2975
2974
   warning_for_row(false),
2994
2993
  order= (Order**)(args + arg_count);
2995
2994
 
2996
2995
  /* fill args items of show and sort */
2997
 
  List_iterator_fast<Item> li(*select_list);
 
2996
  List<Item>::iterator li(select_list->begin());
2998
2997
 
2999
2998
  for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
3000
2999
    *arg_ptr= item_select;
3049
3048
  {
3050
3049
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
3051
3050
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3052
 
    warning->set_msg(current_session, warn_buff);
 
3051
    warning->set_msg(&getSession(), warn_buff);
3053
3052
    warning= 0;
3054
3053
  }
3055
3054
 
3204
3203
bool Item_func_group_concat::setup(Session *session)
3205
3204
{
3206
3205
  List<Item> list;
3207
 
  Select_Lex *select_lex= session->lex->current_select;
 
3206
  Select_Lex *select_lex= session->lex().current_select;
3208
3207
 
3209
3208
  /*
3210
3209
    Currently setup() can be called twice. Please add
3224
3223
  for (uint32_t i= 0; i < arg_count_field; i++)
3225
3224
  {
3226
3225
    Item *item= args[i];
3227
 
    if (list.push_back(item))
3228
 
      return(true);
 
3226
    list.push_back(item);
3229
3227
    if (item->const_item())
3230
3228
    {
3231
3229
      if (item->is_null())
3364
3362
}
3365
3363
 
3366
3364
 
3367
 
void Item_func_group_concat::print(String *str, enum_query_type query_type)
 
3365
void Item_func_group_concat::print(String *str)
3368
3366
{
3369
3367
  str->append(STRING_WITH_LEN("group_concat("));
3370
3368
  if (distinct)
3373
3371
  {
3374
3372
    if (i)
3375
3373
      str->append(',');
3376
 
    args[i]->print(str, query_type);
 
3374
    args[i]->print(str);
3377
3375
  }
3378
3376
  if (arg_count_order)
3379
3377
  {
3382
3380
    {
3383
3381
      if (i)
3384
3382
        str->append(',');
3385
 
      (*order[i]->item)->print(str, query_type);
 
3383
      (*order[i]->item)->print(str);
3386
3384
      if (order[i]->asc)
3387
3385
        str->append(STRING_WITH_LEN(" ASC"));
3388
3386
      else