~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/sum.cc

  • Committer: Joe Daly
  • Date: 2010-04-29 03:08:04 UTC
  • mto: This revision was merged to the branch mainline in revision 1523.
  • Revision ID: skinny.moey@gmail.com-20100429030804-ppssp19xwyrgm5of
remove com_stat_vars and rework lock in scoreboard to not lock on Sessions locating a slot they previously owned

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/**
20
20
  @brief
21
21
  Sum functions (COUNT, MIN...)
22
22
*/
23
 
#include <config.h>
24
 
#include <cstdio>
 
23
#include "config.h"
25
24
#include <math.h>
26
25
#include <drizzled/sql_select.h>
27
26
#include <drizzled/error.h>
29
28
#include <drizzled/hybrid_type_traits_integer.h>
30
29
#include <drizzled/hybrid_type_traits_decimal.h>
31
30
#include <drizzled/sql_base.h>
32
 
#include <drizzled/session.h>
33
31
 
34
32
#include <drizzled/item/sum.h>
35
33
#include <drizzled/field/decimal.h>
36
34
#include <drizzled/field/double.h>
37
 
#include <drizzled/field/int64.h>
 
35
#include <drizzled/field/int64_t.h>
38
36
#include <drizzled/field/date.h>
39
37
#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>
 
38
 
 
39
#include "drizzled/internal/m_string.h"
45
40
 
46
41
#include <algorithm>
47
42
 
50
45
namespace drizzled
51
46
{
52
47
 
 
48
extern my_decimal decimal_zero;
53
49
extern plugin::StorageEngine *heap_engine;
54
50
 
55
51
/**
76
72
 
77
73
bool Item_sum::init_sum_func_check(Session *session)
78
74
{
79
 
  if (!session->getLex()->allow_sum_func)
 
75
  if (!session->lex->allow_sum_func)
80
76
  {
81
77
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
82
78
               MYF(0));
83
79
    return true;
84
80
  }
85
81
  /* Set a reference to the nesting set function if there is  any */
86
 
  in_sum_func= session->getLex()->in_sum_func;
 
82
  in_sum_func= session->lex->in_sum_func;
87
83
  /* 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;
 
84
  session->lex->in_sum_func= this;
 
85
  nest_level= session->lex->current_select->nest_level;
90
86
  ref_by= 0;
91
87
  aggr_level= -1;
92
88
  aggr_sel= NULL;
93
89
  max_arg_level= -1;
94
90
  max_sum_func_level= -1;
95
 
  outer_fields.clear();
 
91
  outer_fields.empty();
96
92
  return false;
97
93
}
98
94
 
148
144
bool Item_sum::check_sum_func(Session *session, Item **ref)
149
145
{
150
146
  bool invalid= false;
151
 
  nesting_map allow_sum_func= session->getLex()->allow_sum_func;
 
147
  nesting_map allow_sum_func= session->lex->allow_sum_func;
152
148
  /*
153
149
    The value of max_arg_level is updated if an argument of the set function
154
150
    contains a column reference resolved  against a subquery whose level is
181
177
  if (!invalid && aggr_level < 0)
182
178
  {
183
179
    aggr_level= nest_level;
184
 
    aggr_sel= session->getLex()->current_select;
 
180
    aggr_sel= session->lex->current_select;
185
181
  }
186
182
  /*
187
183
    By this moment we either found a subquery where the set function is
256
252
        select the field belongs to. If there are some then an error is
257
253
        raised.
258
254
    */
259
 
    List<Item_field>::iterator of(outer_fields.begin());
 
255
    List_iterator<Item_field> of(outer_fields);
260
256
    while ((field= of++))
261
257
    {
262
258
      Select_Lex *sel= field->cached_table->select_lex;
287
283
  }
288
284
  aggr_sel->full_group_by_flag.set(SUM_FUNC_USED);
289
285
  update_used_tables();
290
 
  session->getLex()->in_sum_func= in_sum_func;
 
286
  session->lex->in_sum_func= in_sum_func;
291
287
  return false;
292
288
}
293
289
 
319
315
bool Item_sum::register_sum_func(Session *session, Item **ref)
320
316
{
321
317
  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() ;
 
318
  nesting_map allow_sum_func= session->lex->allow_sum_func;
 
319
  for (sl= session->lex->current_select->master_unit()->outer_select() ;
324
320
       sl && sl->nest_level > max_arg_level;
325
321
       sl= sl->master_unit()->outer_select() )
326
322
  {
371
367
      with_sum_func being set for an Select_Lex means that this Select_Lex
372
368
      has aggregate functions directly referenced (i.e. not through a sub-select).
373
369
    */
374
 
    for (sl= session->getLex()->current_select;
 
370
    for (sl= session->lex->current_select;
375
371
         sl && sl != aggr_sel && sl->master_unit()->item;
376
372
         sl= sl->master_unit()->outer_select() )
377
373
      sl->master_unit()->item->with_sum_func= 1;
378
374
  }
379
 
  session->getLex()->current_select->mark_as_dependent(aggr_sel);
 
375
  session->lex->current_select->mark_as_dependent(aggr_sel);
380
376
  return false;
381
377
}
382
378
 
387
383
  if ((args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
388
384
  {
389
385
    uint32_t i=0;
390
 
    List<Item>::iterator li(list.begin());
 
386
    List_iterator_fast<Item> li(list);
391
387
    Item *item;
392
388
 
393
389
    while ((item=li++))
396
392
    }
397
393
  }
398
394
  mark_as_sum_func();
399
 
  list.clear();                                 // Fields are used
 
395
  list.empty();                                 // Fields are used
400
396
}
401
397
 
402
398
 
414
410
  if (arg_count <= 2)
415
411
    args=tmp_args;
416
412
  else
417
 
    if (!(args= (Item**) session->getMemRoot()->allocate(sizeof(Item*)*arg_count)))
 
413
    if (!(args= (Item**) session->alloc(sizeof(Item*)*arg_count)))
418
414
      return;
419
415
  memcpy(args, item->args, sizeof(Item*)*arg_count);
420
416
}
422
418
 
423
419
void Item_sum::mark_as_sum_func()
424
420
{
425
 
  Select_Lex *cur_select= getSession().getLex()->current_select;
 
421
  Select_Lex *cur_select= current_session->lex->current_select;
426
422
  cur_select->n_sum_items++;
427
423
  cur_select->with_sum_func= 1;
428
424
  with_sum_func= 1;
511
507
                                  Table *table,
512
508
                                  uint32_t convert_blob_length)
513
509
{
514
 
  Field *field= NULL;
515
 
 
 
510
  Field *field;
516
511
  switch (result_type()) {
517
512
  case REAL_RESULT:
518
513
    field= new Field_double(max_length, maybe_null, name, decimals, true);
519
514
    break;
520
 
 
521
515
  case INT_RESULT:
522
 
    field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
 
516
    field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
523
517
    break;
524
 
 
525
518
  case STRING_RESULT:
526
519
    if (max_length/collation.collation->mbmaxlen <= 255 ||
527
520
        convert_blob_length > Field_varstring::MAX_SIZE ||
528
521
        !convert_blob_length)
529
 
    {
530
522
      return make_string_field(table);
531
 
    }
532
 
 
533
 
    table->setVariableWidth();
534
523
    field= new Field_varstring(convert_blob_length, maybe_null,
535
 
                               name, collation.collation);
 
524
                               name, table->s, collation.collation);
536
525
    break;
537
 
 
538
526
  case DECIMAL_RESULT:
539
527
    field= new Field_decimal(max_length, maybe_null, name,
540
 
                             decimals, unsigned_flag);
 
528
                                 decimals, unsigned_flag);
541
529
    break;
542
 
 
543
530
  case ROW_RESULT:
 
531
  default:
544
532
    // This case should never be choosen
545
533
    assert(0);
546
534
    return 0;
547
535
  }
548
 
 
549
536
  if (field)
550
537
    field->init(table);
551
 
 
552
538
  return field;
553
539
}
554
540
 
586
572
}
587
573
 
588
574
 
589
 
type::Decimal *Item_sum_num::val_decimal(type::Decimal *decimal_value)
 
575
my_decimal *Item_sum_num::val_decimal(my_decimal *decimal_value)
590
576
{
591
577
  return val_decimal_from_real(decimal_value);
592
578
}
599
585
}
600
586
 
601
587
 
602
 
type::Decimal *Item_sum_int::val_decimal(type::Decimal *decimal_value)
 
588
my_decimal *Item_sum_int::val_decimal(my_decimal *decimal_value)
603
589
{
604
590
  return val_decimal_from_int(decimal_value);
605
591
}
646
632
    sum_int= item->sum_int;
647
633
    break;
648
634
  case DECIMAL_RESULT:
649
 
    class_decimal2decimal(&item->sum_dec, &sum_dec);
 
635
    my_decimal2decimal(&item->sum_dec, &sum_dec);
650
636
    break;
651
637
  case REAL_RESULT:
652
638
    sum= item->sum;
658
644
    */
659
645
    break;
660
646
  case ROW_RESULT:
 
647
  default:
661
648
    assert(0);
662
649
  }
663
650
  collation.set(item->collation);
686
673
    break;
687
674
  case DECIMAL_RESULT:
688
675
    max_length= item->max_length;
689
 
    sum_dec.set_zero();
 
676
    my_decimal_set_zero(&sum_dec);
690
677
    break;
691
678
  case REAL_RESULT:
692
679
    max_length= float_length(decimals);
696
683
    max_length= item->max_length;
697
684
    break;
698
685
  case ROW_RESULT:
 
686
  default:
699
687
    assert(0);
700
688
  };
701
689
  /* MIN/MAX can return NULL for empty set indepedent of the used column */
726
714
  {
727
715
    field= ((Item_field*) args[0])->field;
728
716
 
729
 
    if ((field= create_tmp_field_from_field(&getSession(), field, name, table,
 
717
    if ((field= create_tmp_field_from_field(current_session, field, name, table,
730
718
                                            NULL, convert_blob_length)))
731
719
      field->flags&= ~NOT_NULL_FLAG;
732
720
    return field;
738
726
  */
739
727
  switch (args[0]->field_type()) {
740
728
  case DRIZZLE_TYPE_DATE:
741
 
    field= new Field_date(maybe_null, name);
 
729
    field= new Field_date(maybe_null, name, collation.collation);
742
730
    break;
743
731
  case DRIZZLE_TYPE_TIMESTAMP:
744
732
  case DRIZZLE_TYPE_DATETIME:
745
 
    field= new Field_datetime(maybe_null, name);
 
733
    field= new Field_datetime(maybe_null, name, collation.collation);
746
734
    break;
747
735
  default:
748
736
    return Item_sum::create_tmp_field(group, table, convert_blob_length);
749
737
  }
750
 
 
751
738
  if (field)
752
739
    field->init(table);
753
 
 
754
740
  return field;
755
741
}
756
742
 
770
756
  /* TODO: check if the following assignments are really needed */
771
757
  if (hybrid_type == DECIMAL_RESULT)
772
758
  {
773
 
    class_decimal2decimal(item->dec_buffs, dec_buffs);
774
 
    class_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
 
759
    my_decimal2decimal(item->dec_buffs, dec_buffs);
 
760
    my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
775
761
  }
776
762
  else
777
763
    sum= item->sum;
789
775
  if (hybrid_type == DECIMAL_RESULT)
790
776
  {
791
777
    curr_dec_buff= 0;
792
 
    dec_buffs->set_zero();
 
778
    my_decimal_set_zero(dec_buffs);
793
779
  }
794
780
  else
795
781
    sum= 0.0;
809
795
    break;
810
796
  case INT_RESULT:
811
797
  case DECIMAL_RESULT:
812
 
    {
813
 
      /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
814
 
      int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
815
 
      max_length= class_decimal_precision_to_length(precision, decimals,
816
 
                                                 unsigned_flag);
817
 
      curr_dec_buff= 0;
818
 
      hybrid_type= DECIMAL_RESULT;
819
 
      dec_buffs->set_zero();
820
 
      break;
821
 
    }
 
798
  {
 
799
    /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
 
800
    int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
 
801
    max_length= my_decimal_precision_to_length(precision, decimals,
 
802
                                               unsigned_flag);
 
803
    curr_dec_buff= 0;
 
804
    hybrid_type= DECIMAL_RESULT;
 
805
    my_decimal_set_zero(dec_buffs);
 
806
    break;
 
807
  }
822
808
  case ROW_RESULT:
 
809
  default:
823
810
    assert(0);
824
811
  }
 
812
  return;
825
813
}
826
814
 
827
815
 
829
817
{
830
818
  if (hybrid_type == DECIMAL_RESULT)
831
819
  {
832
 
    type::Decimal value, *val= args[0]->val_decimal(&value);
 
820
    my_decimal value, *val= args[0]->val_decimal(&value);
833
821
    if (!args[0]->null_value)
834
822
    {
835
 
      class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
 
823
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
836
824
                     val, dec_buffs + curr_dec_buff);
837
825
      curr_dec_buff^= 1;
838
826
      null_value= 0;
854
842
  if (hybrid_type == DECIMAL_RESULT)
855
843
  {
856
844
    int64_t result;
857
 
    (dec_buffs + curr_dec_buff)->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
 
845
    my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
 
846
                   &result);
858
847
    return result;
859
848
  }
860
849
  return (int64_t) rint(val_real());
865
854
{
866
855
  assert(fixed == 1);
867
856
  if (hybrid_type == DECIMAL_RESULT)
868
 
    class_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
 
857
    my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
869
858
  return sum;
870
859
}
871
860
 
878
867
}
879
868
 
880
869
 
881
 
type::Decimal *Item_sum_sum::val_decimal(type::Decimal *val)
 
870
my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
882
871
{
883
872
  if (hybrid_type == DECIMAL_RESULT)
884
873
    return (dec_buffs + curr_dec_buff);
941
930
 
942
931
  virtual void div(Hybrid_type *val, uint64_t u) const
943
932
  {
944
 
    int2_class_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
 
933
    int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
945
934
    val->used_dec_buf_no= 0;
946
935
    val->traits= Hybrid_type_traits_decimal::instance();
947
936
    val->traits->div(val, u);
963
952
{
964
953
  assert(args[0]->fixed);
965
954
 
966
 
  null_value= maybe_null= true;
967
955
  table_field_type= args[0]->field_type();
968
956
 
969
957
  /* Adjust tmp table type according to the chosen aggregation type */
974
962
    table_field_type= DRIZZLE_TYPE_DOUBLE;
975
963
    break;
976
964
  case INT_RESULT:
977
 
    /*
978
 
      Preserving int8, int16, int32 field types gives ~10% performance boost
979
 
      as the size of result tree becomes significantly smaller.
980
 
      Another speed up we gain by using int64_t for intermediate
981
 
      calculations. The range of int64 is enough to hold sum 2^32 distinct
982
 
      integers each <= 2^32.
983
 
    */
984
 
    if (table_field_type == DRIZZLE_TYPE_LONG)
985
 
    {
986
 
      val.traits= Hybrid_type_traits_fast_decimal::instance();
987
 
      break;
988
 
    }
989
 
    table_field_type= DRIZZLE_TYPE_LONGLONG;
990
 
    /* fallthrough */
 
965
  /*
 
966
    Preserving int8, int16, int32 field types gives ~10% performance boost
 
967
    as the size of result tree becomes significantly smaller.
 
968
    Another speed up we gain by using int64_t for intermediate
 
969
    calculations. The range of int64 is enough to hold sum 2^32 distinct
 
970
    integers each <= 2^32.
 
971
  */
 
972
  if (table_field_type == DRIZZLE_TYPE_LONG)
 
973
  {
 
974
    val.traits= Hybrid_type_traits_fast_decimal::instance();
 
975
    break;
 
976
  }
 
977
  table_field_type= DRIZZLE_TYPE_LONGLONG;
 
978
  /* fallthrough */
991
979
  case DECIMAL_RESULT:
992
980
    val.traits= Hybrid_type_traits_decimal::instance();
993
981
    if (table_field_type != DRIZZLE_TYPE_LONGLONG)
994
982
      table_field_type= DRIZZLE_TYPE_DECIMAL;
995
983
    break;
996
984
  case ROW_RESULT:
 
985
  default:
997
986
    assert(0);
998
987
  }
999
 
 
1000
988
  val.traits->fix_length_and_dec(this, args[0]);
1001
989
}
1002
990
 
1035
1023
  field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
1036
1024
                               args[0]->decimals, args[0]->maybe_null);
1037
1025
 
1038
 
  if (! (table= session->getInstanceTable(field_list)))
 
1026
  if (! (table= create_virtual_tmp_table(session, field_list)))
1039
1027
    return(true);
1040
1028
 
1041
1029
  /* XXX: check that the case of CHAR(0) works OK */
1042
 
  tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
 
1030
  tree_key_length= table->s->reclength - table->s->null_bytes;
1043
1031
 
1044
1032
  /*
1045
1033
    Unique handles all unique elements in a tree until they can't fit
1058
1046
 
1059
1047
bool Item_sum_distinct::add()
1060
1048
{
1061
 
  args[0]->save_in_field(table->getField(0), false);
 
1049
  args[0]->save_in_field(table->field[0], false);
1062
1050
  is_evaluated= false;
1063
 
  if (!table->getField(0)->is_null())
 
1051
  if (!table->field[0]->is_null())
1064
1052
  {
1065
1053
    assert(tree);
1066
1054
    null_value= 0;
1068
1056
      '0' values are also stored in the tree. This doesn't matter
1069
1057
      for SUM(DISTINCT), but is important for AVG(DISTINCT)
1070
1058
    */
1071
 
    return tree->unique_add(table->getField(0)->ptr);
 
1059
    return tree->unique_add(table->field[0]->ptr);
1072
1060
  }
1073
1061
  return 0;
1074
1062
}
1076
1064
 
1077
1065
bool Item_sum_distinct::unique_walk_function(void *element)
1078
1066
{
1079
 
  memcpy(table->getField(0)->ptr, element, tree_key_length);
 
1067
  memcpy(table->field[0]->ptr, element, tree_key_length);
1080
1068
  ++count;
1081
 
  val.traits->add(&val, table->getField(0));
 
1069
  val.traits->add(&val, table->field[0]);
1082
1070
  return 0;
1083
1071
}
1084
1072
 
1120
1108
     */
1121
1109
    if (tree)
1122
1110
    {
1123
 
      table->getField(0)->set_notnull();
 
1111
      table->field[0]->set_notnull();
1124
1112
      tree->walk(item_sum_distinct_walk, (void*) this);
1125
1113
    }
1126
1114
    is_evaluated= true;
1135
1123
}
1136
1124
 
1137
1125
 
1138
 
type::Decimal *Item_sum_distinct::val_decimal(type::Decimal *to)
 
1126
my_decimal *Item_sum_distinct::val_decimal(my_decimal *to)
1139
1127
{
1140
1128
  calculate_val_and_count();
1141
1129
  if (null_value)
1167
1155
Item_sum_avg_distinct::fix_length_and_dec()
1168
1156
{
1169
1157
  Item_sum_distinct::fix_length_and_dec();
1170
 
  prec_increment= getSession().variables.div_precincrement;
 
1158
  prec_increment= current_session->variables.div_precincrement;
1171
1159
  /*
1172
1160
    AVG() will divide val by count. We need to reserve digits
1173
1161
    after decimal point as the result can be fractional.
1230
1218
{
1231
1219
  Item_sum_sum::fix_length_and_dec();
1232
1220
  maybe_null=null_value=1;
1233
 
  prec_increment= getSession().variables.div_precincrement;
1234
 
 
 
1221
  prec_increment= current_session->variables.div_precincrement;
1235
1222
  if (hybrid_type == DECIMAL_RESULT)
1236
1223
  {
1237
1224
    int precision= args[0]->decimal_precision() + prec_increment;
1238
1225
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1239
 
    max_length= class_decimal_precision_to_length(precision, decimals,
 
1226
    max_length= my_decimal_precision_to_length(precision, decimals,
1240
1227
                                               unsigned_flag);
1241
1228
    f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1242
1229
    f_scale=  args[0]->decimals;
1243
 
    dec_bin_size= class_decimal_get_binary_size(f_precision, f_scale);
 
1230
    dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1244
1231
  }
1245
1232
  else {
1246
1233
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1266
1253
      The easiest way is to do this is to store both value in a string
1267
1254
      and unpack on access.
1268
1255
    */
1269
 
    table->setVariableWidth();
1270
1256
    field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
1271
1257
                                dec_bin_size : sizeof(double)) + sizeof(int64_t),
1272
 
                               0, name, &my_charset_bin);
 
1258
                               0, name, table->s, &my_charset_bin);
1273
1259
  }
1274
1260
  else if (hybrid_type == DECIMAL_RESULT)
1275
1261
    field= new Field_decimal(max_length, maybe_null, name,
1316
1302
}
1317
1303
 
1318
1304
 
1319
 
type::Decimal *Item_sum_avg::val_decimal(type::Decimal *val)
 
1305
my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
1320
1306
{
1321
 
  type::Decimal sum_buff, cnt;
1322
 
  const type::Decimal *sum_dec;
 
1307
  my_decimal sum_buff, cnt;
 
1308
  const my_decimal *sum_dec;
1323
1309
  assert(fixed == 1);
1324
1310
  if (!count)
1325
1311
  {
1335
1321
    return val_decimal_from_real(val);
1336
1322
 
1337
1323
  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);
 
1324
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
 
1325
  my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1340
1326
  return val;
1341
1327
}
1342
1328
 
1428
1414
void Item_sum_variance::fix_length_and_dec()
1429
1415
{
1430
1416
  maybe_null= null_value= 1;
1431
 
  prec_increment= getSession().variables.div_precincrement;
 
1417
  prec_increment= current_session->variables.div_precincrement;
1432
1418
 
1433
1419
  /*
1434
1420
    According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1445
1431
    break;
1446
1432
  case INT_RESULT:
1447
1433
  case DECIMAL_RESULT:
1448
 
    {
1449
 
      int precision= args[0]->decimal_precision()*2 + prec_increment;
1450
 
      decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1451
 
      max_length= class_decimal_precision_to_length(precision, decimals,
1452
 
                                                 unsigned_flag);
 
1434
  {
 
1435
    int precision= args[0]->decimal_precision()*2 + prec_increment;
 
1436
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
 
1437
    max_length= my_decimal_precision_to_length(precision, decimals,
 
1438
                                               unsigned_flag);
1453
1439
 
1454
 
      break;
1455
 
    }
 
1440
    break;
 
1441
  }
1456
1442
  case ROW_RESULT:
 
1443
  default:
1457
1444
    assert(0);
1458
1445
  }
 
1446
  return;
1459
1447
}
1460
1448
 
1461
1449
 
1481
1469
      The easiest way is to do this is to store both value in a string
1482
1470
      and unpack on access.
1483
1471
    */
1484
 
    table->setVariableWidth();
1485
 
    field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, &my_charset_bin);
 
1472
    field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, table->s, &my_charset_bin);
1486
1473
  }
1487
1474
  else
1488
1475
    field= new Field_double(max_length, maybe_null, name, decimals, true);
1544
1531
}
1545
1532
 
1546
1533
 
1547
 
type::Decimal *Item_sum_variance::val_decimal(type::Decimal *dec_buf)
 
1534
my_decimal *Item_sum_variance::val_decimal(my_decimal *dec_buf)
1548
1535
{
1549
1536
  assert(fixed == 1);
1550
1537
  return val_decimal_from_real(dec_buf);
1608
1595
    sum_int= 0;
1609
1596
    break;
1610
1597
  case DECIMAL_RESULT:
1611
 
    sum_dec.set_zero();
 
1598
    my_decimal_set_zero(&sum_dec);
1612
1599
    break;
1613
1600
  case REAL_RESULT:
1614
1601
    sum= 0.0;
1624
1611
  assert(fixed == 1);
1625
1612
  if (null_value)
1626
1613
    return 0.0;
1627
 
 
1628
1614
  switch (hybrid_type) {
1629
1615
  case STRING_RESULT:
1630
 
    {
1631
 
      char *end_not_used;
1632
 
      int err_not_used;
1633
 
      String *res;  res=val_str(&str_value);
1634
 
      return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1635
 
                               &end_not_used, &err_not_used) : 0.0);
1636
 
    }
 
1616
  {
 
1617
    char *end_not_used;
 
1618
    int err_not_used;
 
1619
    String *res;  res=val_str(&str_value);
 
1620
    return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
 
1621
                             &end_not_used, &err_not_used) : 0.0);
 
1622
  }
1637
1623
  case INT_RESULT:
1638
1624
    return (double) sum_int;
1639
1625
  case DECIMAL_RESULT:
1640
 
    class_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
 
1626
    my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1641
1627
    return sum;
1642
1628
  case REAL_RESULT:
1643
1629
    return sum;
1644
1630
  case ROW_RESULT:
 
1631
  default:
1645
1632
    // This case should never be choosen
1646
 
    break;
 
1633
    assert(0);
 
1634
    return 0;
1647
1635
  }
1648
 
 
1649
 
  assert(0);
1650
 
  return 0;
1651
1636
}
1652
1637
 
1653
1638
int64_t Item_sum_hybrid::val_int()
1661
1646
  case DECIMAL_RESULT:
1662
1647
  {
1663
1648
    int64_t result;
1664
 
    sum_dec.val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
 
1649
    my_decimal2int(E_DEC_FATAL_ERROR, &sum_dec, unsigned_flag, &result);
1665
1650
    return sum_int;
1666
1651
  }
1667
1652
  default:
1670
1655
}
1671
1656
 
1672
1657
 
1673
 
type::Decimal *Item_sum_hybrid::val_decimal(type::Decimal *val)
 
1658
my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
1674
1659
{
1675
1660
  assert(fixed == 1);
1676
1661
  if (null_value)
1677
1662
    return 0;
1678
 
 
1679
1663
  switch (hybrid_type) {
1680
1664
  case STRING_RESULT:
1681
 
    val->store(E_DEC_FATAL_ERROR, &value);
 
1665
    string2my_decimal(E_DEC_FATAL_ERROR, &value, val);
1682
1666
    break;
1683
1667
  case REAL_RESULT:
1684
 
    double2_class_decimal(E_DEC_FATAL_ERROR, sum, val);
 
1668
    double2my_decimal(E_DEC_FATAL_ERROR, sum, val);
1685
1669
    break;
1686
1670
  case DECIMAL_RESULT:
1687
1671
    val= &sum_dec;
1688
1672
    break;
1689
1673
  case INT_RESULT:
1690
 
    int2_class_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
 
1674
    int2my_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1691
1675
    break;
1692
1676
  case ROW_RESULT:
 
1677
  default:
1693
1678
    // This case should never be choosen
1694
1679
    assert(0);
1695
1680
    break;
1696
1681
  }
1697
 
 
1698
1682
  return val;                                   // Keep compiler happy
1699
1683
}
1700
1684
 
1705
1689
  assert(fixed == 1);
1706
1690
  if (null_value)
1707
1691
    return 0;
1708
 
 
1709
1692
  switch (hybrid_type) {
1710
1693
  case STRING_RESULT:
1711
1694
    return &value;
1713
1696
    str->set_real(sum,decimals, &my_charset_bin);
1714
1697
    break;
1715
1698
  case DECIMAL_RESULT:
1716
 
    class_decimal2string(&sum_dec, 0, str);
 
1699
    my_decimal2string(E_DEC_FATAL_ERROR, &sum_dec, 0, 0, 0, str);
1717
1700
    return str;
1718
1701
  case INT_RESULT:
1719
1702
    str->set_int(sum_int, unsigned_flag, &my_charset_bin);
1721
1704
  case ROW_RESULT:
1722
1705
  default:
1723
1706
    // This case should never be choosen
 
1707
    assert(0);
1724
1708
    break;
1725
1709
  }
1726
 
 
1727
1710
  return str;                                   // Keep compiler happy
1728
1711
}
1729
1712
 
1761
1744
{
1762
1745
  switch (hybrid_type) {
1763
1746
  case STRING_RESULT:
 
1747
  {
 
1748
    String *result=args[0]->val_str(&tmp_value);
 
1749
    if (!args[0]->null_value &&
 
1750
        (null_value || sortcmp(&value,result,collation.collation) > 0))
1764
1751
    {
1765
 
      String *result=args[0]->val_str(&tmp_value);
1766
 
      if (!args[0]->null_value &&
1767
 
          (null_value || sortcmp(&value,result,collation.collation) > 0))
1768
 
      {
1769
 
        value.copy(*result);
1770
 
        null_value=0;
1771
 
      }
 
1752
      value.copy(*result);
 
1753
      null_value=0;
1772
1754
    }
1773
 
    break;
 
1755
  }
 
1756
  break;
1774
1757
  case INT_RESULT:
 
1758
  {
 
1759
    int64_t nr=args[0]->val_int();
 
1760
    if (!args[0]->null_value && (null_value ||
 
1761
                                 (unsigned_flag &&
 
1762
                                  (uint64_t) nr < (uint64_t) sum_int) ||
 
1763
                                 (!unsigned_flag && nr < sum_int)))
1775
1764
    {
1776
 
      int64_t nr=args[0]->val_int();
1777
 
      if (!args[0]->null_value && (null_value ||
1778
 
                                   (unsigned_flag &&
1779
 
                                    (uint64_t) nr < (uint64_t) sum_int) ||
1780
 
                                   (!unsigned_flag && nr < sum_int)))
1781
 
      {
1782
 
        sum_int=nr;
1783
 
        null_value=0;
1784
 
      }
 
1765
      sum_int=nr;
 
1766
      null_value=0;
1785
1767
    }
1786
 
    break;
 
1768
  }
 
1769
  break;
1787
1770
  case DECIMAL_RESULT:
 
1771
  {
 
1772
    my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1773
    if (!args[0]->null_value &&
 
1774
        (null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
1788
1775
    {
1789
 
      type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1790
 
      if (!args[0]->null_value &&
1791
 
          (null_value || (class_decimal_cmp(&sum_dec, val) > 0)))
1792
 
      {
1793
 
        class_decimal2decimal(val, &sum_dec);
1794
 
        null_value= 0;
1795
 
      }
 
1776
      my_decimal2decimal(val, &sum_dec);
 
1777
      null_value= 0;
1796
1778
    }
1797
 
    break;
 
1779
  }
 
1780
  break;
1798
1781
  case REAL_RESULT:
 
1782
  {
 
1783
    double nr= args[0]->val_real();
 
1784
    if (!args[0]->null_value && (null_value || nr < sum))
1799
1785
    {
1800
 
      double nr= args[0]->val_real();
1801
 
      if (!args[0]->null_value && (null_value || nr < sum))
1802
 
      {
1803
 
        sum=nr;
1804
 
        null_value=0;
1805
 
      }
 
1786
      sum=nr;
 
1787
      null_value=0;
1806
1788
    }
1807
 
    break;
 
1789
  }
 
1790
  break;
1808
1791
  case ROW_RESULT:
 
1792
  default:
1809
1793
    // This case should never be choosen
1810
1794
    assert(0);
1811
1795
    break;
1824
1808
{
1825
1809
  switch (hybrid_type) {
1826
1810
  case STRING_RESULT:
 
1811
  {
 
1812
    String *result=args[0]->val_str(&tmp_value);
 
1813
    if (!args[0]->null_value &&
 
1814
        (null_value || sortcmp(&value,result,collation.collation) < 0))
1827
1815
    {
1828
 
      String *result=args[0]->val_str(&tmp_value);
1829
 
      if (!args[0]->null_value &&
1830
 
          (null_value || sortcmp(&value,result,collation.collation) < 0))
1831
 
      {
1832
 
        value.copy(*result);
1833
 
        null_value=0;
1834
 
      }
 
1816
      value.copy(*result);
 
1817
      null_value=0;
1835
1818
    }
1836
 
    break;
 
1819
  }
 
1820
  break;
1837
1821
  case INT_RESULT:
 
1822
  {
 
1823
    int64_t nr=args[0]->val_int();
 
1824
    if (!args[0]->null_value && (null_value ||
 
1825
                                 (unsigned_flag &&
 
1826
                                  (uint64_t) nr > (uint64_t) sum_int) ||
 
1827
                                 (!unsigned_flag && nr > sum_int)))
1838
1828
    {
1839
 
      int64_t nr=args[0]->val_int();
1840
 
      if (!args[0]->null_value && (null_value ||
1841
 
                                   (unsigned_flag &&
1842
 
                                    (uint64_t) nr > (uint64_t) sum_int) ||
1843
 
                                   (!unsigned_flag && nr > sum_int)))
1844
 
      {
1845
 
        sum_int=nr;
1846
 
        null_value=0;
1847
 
      }
 
1829
      sum_int=nr;
 
1830
      null_value=0;
1848
1831
    }
1849
 
    break;
 
1832
  }
 
1833
  break;
1850
1834
  case DECIMAL_RESULT:
 
1835
  {
 
1836
    my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1837
    if (!args[0]->null_value &&
 
1838
        (null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
1851
1839
    {
1852
 
      type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1853
 
      if (!args[0]->null_value &&
1854
 
          (null_value || (class_decimal_cmp(val, &sum_dec) > 0)))
1855
 
      {
1856
 
        class_decimal2decimal(val, &sum_dec);
1857
 
        null_value= 0;
1858
 
      }
 
1840
      my_decimal2decimal(val, &sum_dec);
 
1841
      null_value= 0;
1859
1842
    }
1860
 
    break;
 
1843
  }
 
1844
  break;
1861
1845
  case REAL_RESULT:
 
1846
  {
 
1847
    double nr= args[0]->val_real();
 
1848
    if (!args[0]->null_value && (null_value || nr > sum))
1862
1849
    {
1863
 
      double nr= args[0]->val_real();
1864
 
      if (!args[0]->null_value && (null_value || nr > sum))
1865
 
      {
1866
 
        sum=nr;
1867
 
        null_value=0;
1868
 
      }
 
1850
      sum=nr;
 
1851
      null_value=0;
1869
1852
    }
1870
 
    break;
 
1853
  }
 
1854
  break;
1871
1855
  case ROW_RESULT:
 
1856
  default:
1872
1857
    // This case should never be choosen
1873
1858
    assert(0);
1874
1859
    break;
1875
1860
  }
1876
 
 
1877
1861
  return 0;
1878
1862
}
1879
1863
 
1961
1945
{
1962
1946
  switch(hybrid_type) {
1963
1947
  case STRING_RESULT:
1964
 
    {
1965
 
      char buff[MAX_FIELD_WIDTH];
1966
 
      String tmp(buff,sizeof(buff),result_field->charset()),*res;
1967
 
 
1968
 
      res=args[0]->val_str(&tmp);
1969
 
      if (args[0]->null_value)
1970
 
      {
 
1948
  {
 
1949
    char buff[MAX_FIELD_WIDTH];
 
1950
    String tmp(buff,sizeof(buff),result_field->charset()),*res;
 
1951
 
 
1952
    res=args[0]->val_str(&tmp);
 
1953
    if (args[0]->null_value)
 
1954
    {
 
1955
      result_field->set_null();
 
1956
      result_field->reset();
 
1957
    }
 
1958
    else
 
1959
    {
 
1960
      result_field->set_notnull();
 
1961
      result_field->store(res->ptr(),res->length(),tmp.charset());
 
1962
    }
 
1963
    break;
 
1964
  }
 
1965
  case INT_RESULT:
 
1966
  {
 
1967
    int64_t nr=args[0]->val_int();
 
1968
 
 
1969
    if (maybe_null)
 
1970
    {
 
1971
      if (args[0]->null_value)
 
1972
      {
 
1973
        nr=0;
 
1974
        result_field->set_null();
 
1975
      }
 
1976
      else
 
1977
        result_field->set_notnull();
 
1978
    }
 
1979
    result_field->store(nr, unsigned_flag);
 
1980
    break;
 
1981
  }
 
1982
  case REAL_RESULT:
 
1983
  {
 
1984
    double nr= args[0]->val_real();
 
1985
 
 
1986
    if (maybe_null)
 
1987
    {
 
1988
      if (args[0]->null_value)
 
1989
      {
 
1990
        nr=0.0;
 
1991
        result_field->set_null();
 
1992
      }
 
1993
      else
 
1994
        result_field->set_notnull();
 
1995
    }
 
1996
    result_field->store(nr);
 
1997
    break;
 
1998
  }
 
1999
  case DECIMAL_RESULT:
 
2000
  {
 
2001
    my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
 
2002
 
 
2003
    if (maybe_null)
 
2004
    {
 
2005
      if (args[0]->null_value)
1971
2006
        result_field->set_null();
1972
 
        result_field->reset();
1973
 
      }
1974
2007
      else
1975
 
      {
1976
2008
        result_field->set_notnull();
1977
 
        result_field->store(res->ptr(),res->length(),tmp.charset());
1978
 
      }
1979
 
      break;
1980
 
    }
1981
 
  case INT_RESULT:
1982
 
    {
1983
 
      int64_t nr=args[0]->val_int();
1984
 
 
1985
 
      if (maybe_null)
1986
 
      {
1987
 
        if (args[0]->null_value)
1988
 
        {
1989
 
          nr=0;
1990
 
          result_field->set_null();
1991
 
        }
1992
 
        else
1993
 
          result_field->set_notnull();
1994
 
      }
1995
 
      result_field->store(nr, unsigned_flag);
1996
 
      break;
1997
 
    }
1998
 
  case REAL_RESULT:
1999
 
    {
2000
 
      double nr= args[0]->val_real();
2001
 
 
2002
 
      if (maybe_null)
2003
 
      {
2004
 
        if (args[0]->null_value)
2005
 
        {
2006
 
          nr=0.0;
2007
 
          result_field->set_null();
2008
 
        }
2009
 
        else
2010
 
          result_field->set_notnull();
2011
 
      }
2012
 
      result_field->store(nr);
2013
 
      break;
2014
 
    }
2015
 
  case DECIMAL_RESULT:
2016
 
    {
2017
 
      type::Decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2018
 
 
2019
 
      if (maybe_null)
2020
 
      {
2021
 
        if (args[0]->null_value)
2022
 
          result_field->set_null();
2023
 
        else
2024
 
          result_field->set_notnull();
2025
 
      }
2026
 
      /*
2027
 
        We must store zero in the field as we will use the field value in
2028
 
        add()
2029
 
      */
2030
 
      if (!arg_dec)                               // Null
2031
 
        arg_dec= &decimal_zero;
2032
 
      result_field->store_decimal(arg_dec);
2033
 
      break;
2034
 
    }
 
2009
    }
 
2010
    /*
 
2011
      We must store zero in the field as we will use the field value in
 
2012
      add()
 
2013
    */
 
2014
    if (!arg_dec)                               // Null
 
2015
      arg_dec= &decimal_zero;
 
2016
    result_field->store_decimal(arg_dec);
 
2017
    break;
 
2018
  }
2035
2019
  case ROW_RESULT:
 
2020
  default:
2036
2021
    assert(0);
2037
2022
  }
2038
2023
}
2042
2027
{
2043
2028
  if (hybrid_type == DECIMAL_RESULT)
2044
2029
  {
2045
 
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
 
2030
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2046
2031
    if (!arg_val)                               // Null
2047
2032
      arg_val= &decimal_zero;
2048
2033
    result_field->store_decimal(arg_val);
2077
2062
  if (hybrid_type == DECIMAL_RESULT)
2078
2063
  {
2079
2064
    int64_t tmp;
2080
 
    type::Decimal value, *arg_dec= args[0]->val_decimal(&value);
 
2065
    my_decimal value, *arg_dec= args[0]->val_decimal(&value);
2081
2066
    if (args[0]->null_value)
2082
2067
    {
2083
2068
      arg_dec= &decimal_zero;
2085
2070
    }
2086
2071
    else
2087
2072
      tmp= 1;
2088
 
    arg_dec->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
 
2073
    my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
2089
2074
    res+= dec_bin_size;
2090
2075
    int8store(res, tmp);
2091
2076
  }
2129
2114
{
2130
2115
  if (hybrid_type == DECIMAL_RESULT)
2131
2116
  {
2132
 
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
 
2117
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2133
2118
    if (!args[0]->null_value)
2134
2119
    {
2135
2120
      if (!result_field->is_null())
2136
2121
      {
2137
 
        type::Decimal field_value,
 
2122
        my_decimal field_value,
2138
2123
                   *field_val= result_field->val_decimal(&field_value);
2139
 
        class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
 
2124
        my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2140
2125
        result_field->store_decimal(dec_buffs);
2141
2126
      }
2142
2127
      else
2181
2166
  unsigned char *res=result_field->ptr;
2182
2167
  if (hybrid_type == DECIMAL_RESULT)
2183
2168
  {
2184
 
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
 
2169
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2185
2170
    if (!args[0]->null_value)
2186
2171
    {
2187
 
      binary2_class_decimal(E_DEC_FATAL_ERROR, res,
 
2172
      binary2my_decimal(E_DEC_FATAL_ERROR, res,
2188
2173
                        dec_buffs + 1, f_precision, f_scale);
2189
2174
      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);
 
2175
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
 
2176
      my_decimal2binary(E_DEC_FATAL_ERROR, dec_buffs,
 
2177
                        res, f_precision, f_scale);
2192
2178
      res+= dec_bin_size;
2193
2179
      field_count++;
2194
2180
      int8store(res, field_count);
2226
2212
  case DECIMAL_RESULT:
2227
2213
    min_max_update_decimal_field();
2228
2214
    break;
2229
 
  case REAL_RESULT:
2230
 
  case ROW_RESULT:
 
2215
  default:
2231
2216
    min_max_update_real_field();
2232
2217
  }
2233
2218
}
2240
2225
 
2241
2226
  if (!args[0]->null_value)
2242
2227
  {
2243
 
    result_field->val_str_internal(&tmp_value);
 
2228
    result_field->val_str(&tmp_value);
2244
2229
 
2245
2230
    if (result_field->is_null() ||
2246
2231
        (cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2306
2291
Item_sum_hybrid::min_max_update_decimal_field()
2307
2292
{
2308
2293
  /* 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);
 
2294
  my_decimal old_val, nr_val;
 
2295
  const my_decimal *old_nr= result_field->val_decimal(&old_val);
 
2296
  const my_decimal *nr= args[0]->val_decimal(&nr_val);
2312
2297
  if (!args[0]->null_value)
2313
2298
  {
2314
2299
    if (result_field->is_null(0))
2315
2300
      old_nr=nr;
2316
2301
    else
2317
2302
    {
2318
 
      bool res= class_decimal_cmp(old_nr, nr) > 0;
 
2303
      bool res= my_decimal_cmp(old_nr, nr) > 0;
2319
2304
      /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2320
2305
      if ((cmp_sign > 0) ^ (!res))
2321
2306
        old_nr=nr;
2372
2357
}
2373
2358
 
2374
2359
 
2375
 
type::Decimal *Item_avg_field::val_decimal(type::Decimal *dec_buf)
 
2360
my_decimal *Item_avg_field::val_decimal(my_decimal *dec_buf)
2376
2361
{
2377
2362
  // fix_fields() never calls for this Item
2378
2363
  if (hybrid_type == REAL_RESULT)
2382
2367
  if ((null_value= !count))
2383
2368
    return 0;
2384
2369
 
2385
 
  type::Decimal dec_count, dec_field;
2386
 
  binary2_class_decimal(E_DEC_FATAL_ERROR,
 
2370
  my_decimal dec_count, dec_field;
 
2371
  binary2my_decimal(E_DEC_FATAL_ERROR,
2387
2372
                    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,
 
2373
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
 
2374
  my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2390
2375
                 &dec_field, &dec_count, prec_increment);
2391
2376
  return dec_buf;
2392
2377
}
2417
2402
}
2418
2403
 
2419
2404
 
2420
 
type::Decimal *Item_std_field::val_decimal(type::Decimal *dec_buf)
 
2405
my_decimal *Item_std_field::val_decimal(my_decimal *dec_buf)
2421
2406
{
2422
2407
  /*
2423
2408
    We can't call val_decimal_from_real() for DECIMAL_RESULT as
2424
2409
    Item_variance_field::val_real() would cause an infinite loop
2425
2410
  */
2426
 
  type::Decimal tmp_dec, *dec;
 
2411
  my_decimal tmp_dec, *dec;
2427
2412
  double nr;
2428
2413
  if (hybrid_type == REAL_RESULT)
2429
2414
    return val_decimal_from_real(dec_buf);
2431
2416
  dec= Item_variance_field::val_decimal(dec_buf);
2432
2417
  if (!dec)
2433
2418
    return 0;
2434
 
  class_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
 
2419
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2435
2420
  assert(nr >= 0.0);
2436
2421
  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);
 
2422
  double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
 
2423
  my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
2439
2424
  return dec_buf;
2440
2425
}
2441
2426
 
2507
2492
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2508
2493
{
2509
2494
  Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2510
 
  Field **field    = item->table->getFields();
2511
 
  Field **field_end= field + item->table->getShare()->sizeFields();
 
2495
  Field **field    = item->table->field;
 
2496
  Field **field_end= field + item->table->s->fields;
2512
2497
  uint32_t *lengths=item->field_lengths;
2513
2498
  for (; field < field_end; ++field)
2514
2499
  {
2548
2533
    is_evaluated= false;
2549
2534
    if (table)
2550
2535
    {
 
2536
      table->free_tmp_table(table->in_use);
2551
2537
      table= 0;
2552
2538
    }
2553
2539
    delete tmp_table_param;
2584
2570
bool Item_sum_count_distinct::setup(Session *session)
2585
2571
{
2586
2572
  List<Item> list;
2587
 
  Select_Lex *select_lex= session->getLex()->current_select;
 
2573
  Select_Lex *select_lex= session->lex->current_select;
2588
2574
 
2589
2575
  /*
2590
2576
    Setup can be called twice for ROLLUP items. This is a bug.
2612
2598
  tmp_table_param->force_copy_fields= force_copy_fields;
2613
2599
  assert(table == 0);
2614
2600
 
2615
 
  if (!(table= create_tmp_table(session, tmp_table_param, list, (Order*) 0, 1,
 
2601
  if (!(table= create_tmp_table(session, tmp_table_param, list, (order_st*) 0, 1,
2616
2602
                                0,
2617
2603
                                (select_lex->options | session->options),
2618
2604
                                HA_POS_ERROR, (char*)"")))
2619
 
  {
2620
2605
    return true;
2621
 
  }
2622
2606
  table->cursor->extra(HA_EXTRA_NO_ROWS);               // Don't update rows
2623
2607
  table->no_rows=1;
2624
2608
 
2625
 
  if (table->getShare()->db_type() == heap_engine)
 
2609
  if (table->s->db_type() == heap_engine)
2626
2610
  {
2627
2611
    /*
2628
2612
      No blobs, otherwise it would have been MyISAM: set up a compare
2630
2614
    */
2631
2615
    qsort_cmp2 compare_key;
2632
2616
    void* cmp_arg;
2633
 
    Field **field= table->getFields();
2634
 
    Field **field_end= field + table->getShare()->sizeFields();
 
2617
    Field **field= table->field;
 
2618
    Field **field_end= field + table->s->fields;
2635
2619
    bool all_binary= true;
2636
2620
 
2637
2621
    for (tree_key_length= 0; field < field_end; ++field)
2652
2636
    }
2653
2637
    else
2654
2638
    {
2655
 
      if (table->getShare()->sizeFields() == 1)
 
2639
      if (table->s->fields == 1)
2656
2640
      {
2657
2641
        /*
2658
2642
          If we have only one field, which is the most common use of
2661
2645
          about other fields.
2662
2646
        */
2663
2647
        compare_key= (qsort_cmp2) simple_str_key_cmp;
2664
 
        cmp_arg= (void*) table->getField(0);
 
2648
        cmp_arg= (void*) table->field[0];
2665
2649
        /* tree_key_length has been set already */
2666
2650
      }
2667
2651
      else
2669
2653
        uint32_t *length;
2670
2654
        compare_key= (qsort_cmp2) composite_key_cmp;
2671
2655
        cmp_arg= (void*) this;
2672
 
        field_lengths= (uint32_t*) session->getMemRoot()->allocate(table->getShare()->sizeFields() * sizeof(uint32_t));
2673
 
        for (tree_key_length= 0, length= field_lengths, field= table->getFields();
 
2656
        field_lengths= (uint32_t*) session->alloc(table->s->fields * sizeof(uint32_t));
 
2657
        for (tree_key_length= 0, length= field_lengths, field= table->field;
2674
2658
             field < field_end; ++field, ++length)
2675
2659
        {
2676
2660
          *length= (*field)->pack_length();
2723
2707
  if (always_null)
2724
2708
    return 0;
2725
2709
  copy_fields(tmp_table_param);
2726
 
  if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
2727
 
    return true;
 
2710
  copy_funcs(tmp_table_param->items_to_copy);
2728
2711
 
2729
 
  for (Field **field= table->getFields() ; *field ; field++)
2730
 
  {
 
2712
  for (Field **field=table->field ; *field ; field++)
2731
2713
    if ((*field)->is_real_null(0))
2732
 
    {
2733
2714
      return 0;                                 // Don't count NULL
2734
 
    }
2735
 
  }
2736
2715
 
2737
2716
  is_evaluated= false;
2738
2717
  if (tree)
2743
2722
      bloat the tree without providing any valuable info. Besides,
2744
2723
      key_length used to initialize the tree didn't include space for them.
2745
2724
    */
2746
 
    return tree->unique_add(table->record[0] + table->getShare()->null_bytes);
 
2725
    return tree->unique_add(table->record[0] + table->s->null_bytes);
2747
2726
  }
2748
2727
  if ((error= table->cursor->insertRecord(table->record[0])) &&
2749
2728
      table->cursor->is_fatal_error(error, HA_CHECK_DUP))
2832
2811
    */
2833
2812
    Field *field= item->get_tmp_table_field();
2834
2813
    int res;
2835
 
    uint32_t offset= field->offset(field->getTable()->record[0])-table->getShare()->null_bytes;
 
2814
    uint32_t offset= field->offset(field->table->record[0])-table->s->null_bytes;
2836
2815
    if((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2837
2816
      return res;
2838
2817
  }
2848
2827
                                    const void* key2)
2849
2828
{
2850
2829
  Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2851
 
  Order **order_item, **end;
 
2830
  order_st **order_item, **end;
2852
2831
  Table *table= grp_item->table;
2853
2832
 
2854
2833
  for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2869
2848
    if (field && !item->const_item())
2870
2849
    {
2871
2850
      int res;
2872
 
      uint32_t offset= (field->offset(field->getTable()->record[0]) -
2873
 
                    table->getShare()->null_bytes);
 
2851
      uint32_t offset= (field->offset(field->table->record[0]) -
 
2852
                    table->s->null_bytes);
2874
2853
      if ((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2875
2854
        return (*order_item)->asc ? res : -res;
2876
2855
    }
2892
2871
                  Item_func_group_concat *item)
2893
2872
{
2894
2873
  Table *table= item->table;
2895
 
  String tmp((char *)table->getUpdateRecord(), table->getShare()->getRecordLength(),
 
2874
  String tmp((char *)table->record[1], table->s->reclength,
2896
2875
             default_charset_info);
2897
2876
  String tmp2;
2898
2877
  String *result= &item->result;
2919
2898
        because it contains both order and arg list fields.
2920
2899
      */
2921
2900
      Field *field= (*arg)->get_tmp_table_field();
2922
 
      uint32_t offset= (field->offset(field->getTable()->record[0]) -
2923
 
                    table->getShare()->null_bytes);
2924
 
      assert(offset < table->getShare()->getRecordLength());
2925
 
      res= field->val_str_internal(&tmp, key + offset);
 
2901
      uint32_t offset= (field->offset(field->table->record[0]) -
 
2902
                    table->s->null_bytes);
 
2903
      assert(offset < table->s->reclength);
 
2904
      res= field->val_str(&tmp, key + offset);
2926
2905
    }
2927
2906
    else
2928
2907
      res= (*arg)->val_str(&tmp);
2992
2971
    order - arg_count_order
2993
2972
  */
2994
2973
  if (!(args= (Item**) memory::sql_alloc(sizeof(Item*) * arg_count +
2995
 
                                 sizeof(Order*)*arg_count_order)))
 
2974
                                 sizeof(order_st*)*arg_count_order)))
2996
2975
    return;
2997
2976
 
2998
 
  order= (Order**)(args + arg_count);
 
2977
  order= (order_st**)(args + arg_count);
2999
2978
 
3000
2979
  /* fill args items of show and sort */
3001
 
  List<Item>::iterator li(select_list->begin());
 
2980
  List_iterator_fast<Item> li(*select_list);
3002
2981
 
3003
2982
  for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
3004
2983
    *arg_ptr= item_select;
3005
2984
 
3006
2985
  if (arg_count_order)
3007
2986
  {
3008
 
    Order **order_ptr= order;
3009
 
    for (Order *order_item= (Order*) order_list->first;
 
2987
    order_st **order_ptr= order;
 
2988
    for (order_st *order_item= (order_st*) order_list->first;
3010
2989
         order_item != NULL;
3011
2990
         order_item= order_item->next)
3012
2991
    {
3053
3032
  {
3054
3033
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
3055
3034
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3056
 
    warning->set_msg(&getSession(), warn_buff);
 
3035
    warning->set_msg(current_session, warn_buff);
3057
3036
    warning= 0;
3058
3037
  }
3059
3038
 
3068
3047
    if (table)
3069
3048
    {
3070
3049
      Session *session= table->in_use;
 
3050
      table->free_tmp_table(session);
3071
3051
      table= 0;
3072
3052
      if (tree)
3073
3053
      {
3119
3099
  if (always_null)
3120
3100
    return 0;
3121
3101
  copy_fields(tmp_table_param);
3122
 
  if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
3123
 
    return true;
 
3102
  copy_funcs(tmp_table_param->items_to_copy);
3124
3103
 
3125
3104
  for (uint32_t i= 0; i < arg_count_field; i++)
3126
3105
  {
3140
3119
  {
3141
3120
    /* Filter out duplicate rows. */
3142
3121
    uint32_t count= unique_filter->elements_in_tree();
3143
 
    unique_filter->unique_add(table->record[0] + table->getShare()->null_bytes);
 
3122
    unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3144
3123
    if (count == unique_filter->elements_in_tree())
3145
3124
      row_eligible= false;
3146
3125
  }
3147
3126
 
3148
3127
  TREE_ELEMENT *el= 0;                          // Only for safety
3149
3128
  if (row_eligible && tree)
3150
 
    el= tree_insert(tree, table->record[0] + table->getShare()->null_bytes, 0,
 
3129
    el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
3151
3130
                    tree->custom_arg);
3152
3131
  /*
3153
3132
    If the row is not a duplicate (el->count == 1)
3156
3135
  */
3157
3136
  if (row_eligible && !warning_for_row &&
3158
3137
      (!tree || (el->count == 1 && distinct && !arg_count_order)))
3159
 
    dump_leaf_key(table->record[0] + table->getShare()->null_bytes, 1, this);
 
3138
    dump_leaf_key(table->record[0] + table->s->null_bytes, 1, this);
3160
3139
 
3161
3140
  return 0;
3162
3141
}
3208
3187
bool Item_func_group_concat::setup(Session *session)
3209
3188
{
3210
3189
  List<Item> list;
3211
 
  Select_Lex *select_lex= session->getLex()->current_select;
 
3190
  Select_Lex *select_lex= session->lex->current_select;
3212
3191
 
3213
3192
  /*
3214
3193
    Currently setup() can be called twice. Please add
3274
3253
    field list.
3275
3254
  */
3276
3255
  if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3277
 
                                (Order*) 0, 0, true,
 
3256
                                (order_st*) 0, 0, true,
3278
3257
                                (select_lex->options | session->options),
3279
3258
                                HA_POS_ERROR, (char*) "")))
3280
 
  {
3281
3259
    return(true);
3282
 
  }
3283
 
 
3284
3260
  table->cursor->extra(HA_EXTRA_NO_ROWS);
3285
3261
  table->no_rows= 1;
3286
3262
 
3289
3265
     Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3290
3266
     the row is not added to the result.
3291
3267
  */
3292
 
  uint32_t tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
 
3268
  uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
3293
3269
 
3294
3270
  if (arg_count_order)
3295
3271
  {