~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/sum.cc

  • Committer: Stewart Smith
  • Date: 2010-08-12 16:48:46 UTC
  • mto: This revision was merged to the branch mainline in revision 1707.
  • Revision ID: stewart@flamingspork.com-20100812164846-s9bhy47g60bvqs41
bug lp:611379 Equivalent queries with Impossible where return different results

The following two equivalent queries return different results in maria 5.2 and 5.3 (and identical results in mysql 5.5.5) :

SELECT SUM( DISTINCT table1 .`pk` ) FROM B table1 STRAIGHT_JOIN ( BB table2 JOIN CC ON table2 .`col_varchar_key` ) ON table2 .`pk` ;

SELECT * FROM ( SELECT SUM( DISTINCT table1 .`pk` ) FROM B table1 STRAIGHT_JOIN ( BB table2 JOIN CC ON table2 .`col_varchar_key` ) ON table2 .`pk` );

MariaDB returns 0 on the second query and NULL on the first, whereas MySQL returns NULL on both. In MariaDB, both EXPLAIN plans agree that "Impossible WHERE noticed after reading const tables"



We have some slightly different output in drizzle:

main.bug_lp611379 [ fail ]
drizzletest: At line 9: query 'explain select * from (select sum(distinct t1.a) from t1,t2 where t1.a=t2.a)
as t' failed: 1048: Column 'sum(distinct t1.a)' cannot be null

but the fix gets us the correct query results, although with slightly different execution plans.



This fix is directly ported from MariaDB.

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>
 
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>
36
35
#include <drizzled/field/double.h>
37
 
#include <drizzled/field/int64.h>
 
36
#include <drizzled/field/int64_t.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;
511
508
                                  Table *table,
512
509
                                  uint32_t convert_blob_length)
513
510
{
514
 
  Field *field= NULL;
515
 
 
 
511
  Field *field;
516
512
  switch (result_type()) {
517
513
  case REAL_RESULT:
518
514
    field= new Field_double(max_length, maybe_null, name, decimals, true);
519
515
    break;
520
 
 
521
516
  case INT_RESULT:
522
 
    field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
 
517
    field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
523
518
    break;
524
 
 
525
519
  case STRING_RESULT:
526
520
    if (max_length/collation.collation->mbmaxlen <= 255 ||
527
521
        convert_blob_length > Field_varstring::MAX_SIZE ||
528
522
        !convert_blob_length)
529
 
    {
530
523
      return make_string_field(table);
531
 
    }
532
 
 
533
 
    table->setVariableWidth();
534
524
    field= new Field_varstring(convert_blob_length, maybe_null,
535
 
                               name, collation.collation);
 
525
                               name, table->getMutableShare(), collation.collation);
536
526
    break;
537
 
 
538
527
  case DECIMAL_RESULT:
539
528
    field= new Field_decimal(max_length, maybe_null, name,
540
 
                             decimals, unsigned_flag);
 
529
                                 decimals, unsigned_flag);
541
530
    break;
542
 
 
543
531
  case ROW_RESULT:
 
532
  default:
544
533
    // This case should never be choosen
545
534
    assert(0);
546
535
    return 0;
547
536
  }
548
 
 
549
537
  if (field)
550
538
    field->init(table);
551
 
 
552
539
  return field;
553
540
}
554
541
 
586
573
}
587
574
 
588
575
 
589
 
type::Decimal *Item_sum_num::val_decimal(type::Decimal *decimal_value)
 
576
my_decimal *Item_sum_num::val_decimal(my_decimal *decimal_value)
590
577
{
591
578
  return val_decimal_from_real(decimal_value);
592
579
}
599
586
}
600
587
 
601
588
 
602
 
type::Decimal *Item_sum_int::val_decimal(type::Decimal *decimal_value)
 
589
my_decimal *Item_sum_int::val_decimal(my_decimal *decimal_value)
603
590
{
604
591
  return val_decimal_from_int(decimal_value);
605
592
}
646
633
    sum_int= item->sum_int;
647
634
    break;
648
635
  case DECIMAL_RESULT:
649
 
    class_decimal2decimal(&item->sum_dec, &sum_dec);
 
636
    my_decimal2decimal(&item->sum_dec, &sum_dec);
650
637
    break;
651
638
  case REAL_RESULT:
652
639
    sum= item->sum;
658
645
    */
659
646
    break;
660
647
  case ROW_RESULT:
 
648
  default:
661
649
    assert(0);
662
650
  }
663
651
  collation.set(item->collation);
686
674
    break;
687
675
  case DECIMAL_RESULT:
688
676
    max_length= item->max_length;
689
 
    sum_dec.set_zero();
 
677
    my_decimal_set_zero(&sum_dec);
690
678
    break;
691
679
  case REAL_RESULT:
692
680
    max_length= float_length(decimals);
696
684
    max_length= item->max_length;
697
685
    break;
698
686
  case ROW_RESULT:
 
687
  default:
699
688
    assert(0);
700
689
  };
701
690
  /* MIN/MAX can return NULL for empty set indepedent of the used column */
726
715
  {
727
716
    field= ((Item_field*) args[0])->field;
728
717
 
729
 
    if ((field= create_tmp_field_from_field(&getSession(), field, name, table,
 
718
    if ((field= create_tmp_field_from_field(current_session, field, name, table,
730
719
                                            NULL, convert_blob_length)))
731
720
      field->flags&= ~NOT_NULL_FLAG;
732
721
    return field;
738
727
  */
739
728
  switch (args[0]->field_type()) {
740
729
  case DRIZZLE_TYPE_DATE:
741
 
    field= new Field_date(maybe_null, name);
 
730
    field= new Field_date(maybe_null, name, collation.collation);
742
731
    break;
743
732
  case DRIZZLE_TYPE_TIMESTAMP:
744
733
  case DRIZZLE_TYPE_DATETIME:
745
 
    field= new Field_datetime(maybe_null, name);
 
734
    field= new Field_datetime(maybe_null, name, collation.collation);
746
735
    break;
747
736
  default:
748
737
    return Item_sum::create_tmp_field(group, table, convert_blob_length);
749
738
  }
750
 
 
751
739
  if (field)
752
740
    field->init(table);
753
 
 
754
741
  return field;
755
742
}
756
743
 
770
757
  /* TODO: check if the following assignments are really needed */
771
758
  if (hybrid_type == DECIMAL_RESULT)
772
759
  {
773
 
    class_decimal2decimal(item->dec_buffs, dec_buffs);
774
 
    class_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
 
760
    my_decimal2decimal(item->dec_buffs, dec_buffs);
 
761
    my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
775
762
  }
776
763
  else
777
764
    sum= item->sum;
789
776
  if (hybrid_type == DECIMAL_RESULT)
790
777
  {
791
778
    curr_dec_buff= 0;
792
 
    dec_buffs->set_zero();
 
779
    my_decimal_set_zero(dec_buffs);
793
780
  }
794
781
  else
795
782
    sum= 0.0;
809
796
    break;
810
797
  case INT_RESULT:
811
798
  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
 
    }
 
799
  {
 
800
    /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
 
801
    int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
 
802
    max_length= my_decimal_precision_to_length(precision, decimals,
 
803
                                               unsigned_flag);
 
804
    curr_dec_buff= 0;
 
805
    hybrid_type= DECIMAL_RESULT;
 
806
    my_decimal_set_zero(dec_buffs);
 
807
    break;
 
808
  }
822
809
  case ROW_RESULT:
 
810
  default:
823
811
    assert(0);
824
812
  }
 
813
  return;
825
814
}
826
815
 
827
816
 
829
818
{
830
819
  if (hybrid_type == DECIMAL_RESULT)
831
820
  {
832
 
    type::Decimal value, *val= args[0]->val_decimal(&value);
 
821
    my_decimal value, *val= args[0]->val_decimal(&value);
833
822
    if (!args[0]->null_value)
834
823
    {
835
 
      class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
 
824
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
836
825
                     val, dec_buffs + curr_dec_buff);
837
826
      curr_dec_buff^= 1;
838
827
      null_value= 0;
854
843
  if (hybrid_type == DECIMAL_RESULT)
855
844
  {
856
845
    int64_t result;
857
 
    (dec_buffs + curr_dec_buff)->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
 
846
    my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
 
847
                   &result);
858
848
    return result;
859
849
  }
860
850
  return (int64_t) rint(val_real());
865
855
{
866
856
  assert(fixed == 1);
867
857
  if (hybrid_type == DECIMAL_RESULT)
868
 
    class_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
 
858
    my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
869
859
  return sum;
870
860
}
871
861
 
878
868
}
879
869
 
880
870
 
881
 
type::Decimal *Item_sum_sum::val_decimal(type::Decimal *val)
 
871
my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
882
872
{
883
873
  if (hybrid_type == DECIMAL_RESULT)
884
874
    return (dec_buffs + curr_dec_buff);
941
931
 
942
932
  virtual void div(Hybrid_type *val, uint64_t u) const
943
933
  {
944
 
    int2_class_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
 
934
    int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
945
935
    val->used_dec_buf_no= 0;
946
936
    val->traits= Hybrid_type_traits_decimal::instance();
947
937
    val->traits->div(val, u);
974
964
    table_field_type= DRIZZLE_TYPE_DOUBLE;
975
965
    break;
976
966
  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 */
 
967
  /*
 
968
    Preserving int8, int16, int32 field types gives ~10% performance boost
 
969
    as the size of result tree becomes significantly smaller.
 
970
    Another speed up we gain by using int64_t for intermediate
 
971
    calculations. The range of int64 is enough to hold sum 2^32 distinct
 
972
    integers each <= 2^32.
 
973
  */
 
974
  if (table_field_type == DRIZZLE_TYPE_LONG)
 
975
  {
 
976
    val.traits= Hybrid_type_traits_fast_decimal::instance();
 
977
    break;
 
978
  }
 
979
  table_field_type= DRIZZLE_TYPE_LONGLONG;
 
980
  /* fallthrough */
991
981
  case DECIMAL_RESULT:
992
982
    val.traits= Hybrid_type_traits_decimal::instance();
993
983
    if (table_field_type != DRIZZLE_TYPE_LONGLONG)
994
984
      table_field_type= DRIZZLE_TYPE_DECIMAL;
995
985
    break;
996
986
  case ROW_RESULT:
 
987
  default:
997
988
    assert(0);
998
989
  }
999
 
 
1000
990
  val.traits->fix_length_and_dec(this, args[0]);
1001
991
}
1002
992
 
1035
1025
  field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
1036
1026
                               args[0]->decimals, args[0]->maybe_null);
1037
1027
 
1038
 
  if (! (table= session->getInstanceTable(field_list)))
 
1028
  if (! (table= session->create_virtual_tmp_table(field_list)))
1039
1029
    return(true);
1040
1030
 
1041
1031
  /* XXX: check that the case of CHAR(0) works OK */
1135
1125
}
1136
1126
 
1137
1127
 
1138
 
type::Decimal *Item_sum_distinct::val_decimal(type::Decimal *to)
 
1128
my_decimal *Item_sum_distinct::val_decimal(my_decimal *to)
1139
1129
{
1140
1130
  calculate_val_and_count();
1141
1131
  if (null_value)
1167
1157
Item_sum_avg_distinct::fix_length_and_dec()
1168
1158
{
1169
1159
  Item_sum_distinct::fix_length_and_dec();
1170
 
  prec_increment= getSession().variables.div_precincrement;
 
1160
  prec_increment= current_session->variables.div_precincrement;
1171
1161
  /*
1172
1162
    AVG() will divide val by count. We need to reserve digits
1173
1163
    after decimal point as the result can be fractional.
1230
1220
{
1231
1221
  Item_sum_sum::fix_length_and_dec();
1232
1222
  maybe_null=null_value=1;
1233
 
  prec_increment= getSession().variables.div_precincrement;
1234
 
 
 
1223
  prec_increment= current_session->variables.div_precincrement;
1235
1224
  if (hybrid_type == DECIMAL_RESULT)
1236
1225
  {
1237
1226
    int precision= args[0]->decimal_precision() + prec_increment;
1238
1227
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1239
 
    max_length= class_decimal_precision_to_length(precision, decimals,
 
1228
    max_length= my_decimal_precision_to_length(precision, decimals,
1240
1229
                                               unsigned_flag);
1241
1230
    f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1242
1231
    f_scale=  args[0]->decimals;
1243
 
    dec_bin_size= class_decimal_get_binary_size(f_precision, f_scale);
 
1232
    dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1244
1233
  }
1245
1234
  else {
1246
1235
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1266
1255
      The easiest way is to do this is to store both value in a string
1267
1256
      and unpack on access.
1268
1257
    */
1269
 
    table->setVariableWidth();
1270
1258
    field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
1271
1259
                                dec_bin_size : sizeof(double)) + sizeof(int64_t),
1272
 
                               0, name, &my_charset_bin);
 
1260
                               0, name, table->getMutableShare(), &my_charset_bin);
1273
1261
  }
1274
1262
  else if (hybrid_type == DECIMAL_RESULT)
1275
1263
    field= new Field_decimal(max_length, maybe_null, name,
1316
1304
}
1317
1305
 
1318
1306
 
1319
 
type::Decimal *Item_sum_avg::val_decimal(type::Decimal *val)
 
1307
my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
1320
1308
{
1321
 
  type::Decimal sum_buff, cnt;
1322
 
  const type::Decimal *sum_dec;
 
1309
  my_decimal sum_buff, cnt;
 
1310
  const my_decimal *sum_dec;
1323
1311
  assert(fixed == 1);
1324
1312
  if (!count)
1325
1313
  {
1335
1323
    return val_decimal_from_real(val);
1336
1324
 
1337
1325
  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);
 
1326
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
 
1327
  my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1340
1328
  return val;
1341
1329
}
1342
1330
 
1428
1416
void Item_sum_variance::fix_length_and_dec()
1429
1417
{
1430
1418
  maybe_null= null_value= 1;
1431
 
  prec_increment= getSession().variables.div_precincrement;
 
1419
  prec_increment= current_session->variables.div_precincrement;
1432
1420
 
1433
1421
  /*
1434
1422
    According to the SQL2003 standard (Part 2, Foundations; sec 10.9,
1445
1433
    break;
1446
1434
  case INT_RESULT:
1447
1435
  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);
 
1436
  {
 
1437
    int precision= args[0]->decimal_precision()*2 + prec_increment;
 
1438
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
 
1439
    max_length= my_decimal_precision_to_length(precision, decimals,
 
1440
                                               unsigned_flag);
1453
1441
 
1454
 
      break;
1455
 
    }
 
1442
    break;
 
1443
  }
1456
1444
  case ROW_RESULT:
 
1445
  default:
1457
1446
    assert(0);
1458
1447
  }
 
1448
  return;
1459
1449
}
1460
1450
 
1461
1451
 
1481
1471
      The easiest way is to do this is to store both value in a string
1482
1472
      and unpack on access.
1483
1473
    */
1484
 
    table->setVariableWidth();
1485
 
    field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, &my_charset_bin);
 
1474
    field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, table->getMutableShare(), &my_charset_bin);
1486
1475
  }
1487
1476
  else
1488
1477
    field= new Field_double(max_length, maybe_null, name, decimals, true);
1544
1533
}
1545
1534
 
1546
1535
 
1547
 
type::Decimal *Item_sum_variance::val_decimal(type::Decimal *dec_buf)
 
1536
my_decimal *Item_sum_variance::val_decimal(my_decimal *dec_buf)
1548
1537
{
1549
1538
  assert(fixed == 1);
1550
1539
  return val_decimal_from_real(dec_buf);
1608
1597
    sum_int= 0;
1609
1598
    break;
1610
1599
  case DECIMAL_RESULT:
1611
 
    sum_dec.set_zero();
 
1600
    my_decimal_set_zero(&sum_dec);
1612
1601
    break;
1613
1602
  case REAL_RESULT:
1614
1603
    sum= 0.0;
1624
1613
  assert(fixed == 1);
1625
1614
  if (null_value)
1626
1615
    return 0.0;
1627
 
 
1628
1616
  switch (hybrid_type) {
1629
1617
  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
 
    }
 
1618
  {
 
1619
    char *end_not_used;
 
1620
    int err_not_used;
 
1621
    String *res;  res=val_str(&str_value);
 
1622
    return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
 
1623
                             &end_not_used, &err_not_used) : 0.0);
 
1624
  }
1637
1625
  case INT_RESULT:
1638
1626
    return (double) sum_int;
1639
1627
  case DECIMAL_RESULT:
1640
 
    class_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
 
1628
    my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1641
1629
    return sum;
1642
1630
  case REAL_RESULT:
1643
1631
    return sum;
1644
1632
  case ROW_RESULT:
 
1633
  default:
1645
1634
    // This case should never be choosen
1646
 
    break;
 
1635
    assert(0);
 
1636
    return 0;
1647
1637
  }
1648
 
 
1649
 
  assert(0);
1650
 
  return 0;
1651
1638
}
1652
1639
 
1653
1640
int64_t Item_sum_hybrid::val_int()
1661
1648
  case DECIMAL_RESULT:
1662
1649
  {
1663
1650
    int64_t result;
1664
 
    sum_dec.val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
 
1651
    my_decimal2int(E_DEC_FATAL_ERROR, &sum_dec, unsigned_flag, &result);
1665
1652
    return sum_int;
1666
1653
  }
1667
1654
  default:
1670
1657
}
1671
1658
 
1672
1659
 
1673
 
type::Decimal *Item_sum_hybrid::val_decimal(type::Decimal *val)
 
1660
my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
1674
1661
{
1675
1662
  assert(fixed == 1);
1676
1663
  if (null_value)
1677
1664
    return 0;
1678
 
 
1679
1665
  switch (hybrid_type) {
1680
1666
  case STRING_RESULT:
1681
 
    val->store(E_DEC_FATAL_ERROR, &value);
 
1667
    string2my_decimal(E_DEC_FATAL_ERROR, &value, val);
1682
1668
    break;
1683
1669
  case REAL_RESULT:
1684
 
    double2_class_decimal(E_DEC_FATAL_ERROR, sum, val);
 
1670
    double2my_decimal(E_DEC_FATAL_ERROR, sum, val);
1685
1671
    break;
1686
1672
  case DECIMAL_RESULT:
1687
1673
    val= &sum_dec;
1688
1674
    break;
1689
1675
  case INT_RESULT:
1690
 
    int2_class_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
 
1676
    int2my_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1691
1677
    break;
1692
1678
  case ROW_RESULT:
 
1679
  default:
1693
1680
    // This case should never be choosen
1694
1681
    assert(0);
1695
1682
    break;
1696
1683
  }
1697
 
 
1698
1684
  return val;                                   // Keep compiler happy
1699
1685
}
1700
1686
 
1705
1691
  assert(fixed == 1);
1706
1692
  if (null_value)
1707
1693
    return 0;
1708
 
 
1709
1694
  switch (hybrid_type) {
1710
1695
  case STRING_RESULT:
1711
1696
    return &value;
1713
1698
    str->set_real(sum,decimals, &my_charset_bin);
1714
1699
    break;
1715
1700
  case DECIMAL_RESULT:
1716
 
    class_decimal2string(&sum_dec, 0, str);
 
1701
    my_decimal2string(E_DEC_FATAL_ERROR, &sum_dec, 0, 0, 0, str);
1717
1702
    return str;
1718
1703
  case INT_RESULT:
1719
1704
    str->set_int(sum_int, unsigned_flag, &my_charset_bin);
1721
1706
  case ROW_RESULT:
1722
1707
  default:
1723
1708
    // This case should never be choosen
 
1709
    assert(0);
1724
1710
    break;
1725
1711
  }
1726
 
 
1727
1712
  return str;                                   // Keep compiler happy
1728
1713
}
1729
1714
 
1761
1746
{
1762
1747
  switch (hybrid_type) {
1763
1748
  case STRING_RESULT:
 
1749
  {
 
1750
    String *result=args[0]->val_str(&tmp_value);
 
1751
    if (!args[0]->null_value &&
 
1752
        (null_value || sortcmp(&value,result,collation.collation) > 0))
1764
1753
    {
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
 
      }
 
1754
      value.copy(*result);
 
1755
      null_value=0;
1772
1756
    }
1773
 
    break;
 
1757
  }
 
1758
  break;
1774
1759
  case INT_RESULT:
 
1760
  {
 
1761
    int64_t nr=args[0]->val_int();
 
1762
    if (!args[0]->null_value && (null_value ||
 
1763
                                 (unsigned_flag &&
 
1764
                                  (uint64_t) nr < (uint64_t) sum_int) ||
 
1765
                                 (!unsigned_flag && nr < sum_int)))
1775
1766
    {
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
 
      }
 
1767
      sum_int=nr;
 
1768
      null_value=0;
1785
1769
    }
1786
 
    break;
 
1770
  }
 
1771
  break;
1787
1772
  case DECIMAL_RESULT:
 
1773
  {
 
1774
    my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1775
    if (!args[0]->null_value &&
 
1776
        (null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
1788
1777
    {
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
 
      }
 
1778
      my_decimal2decimal(val, &sum_dec);
 
1779
      null_value= 0;
1796
1780
    }
1797
 
    break;
 
1781
  }
 
1782
  break;
1798
1783
  case REAL_RESULT:
 
1784
  {
 
1785
    double nr= args[0]->val_real();
 
1786
    if (!args[0]->null_value && (null_value || nr < sum))
1799
1787
    {
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
 
      }
 
1788
      sum=nr;
 
1789
      null_value=0;
1806
1790
    }
1807
 
    break;
 
1791
  }
 
1792
  break;
1808
1793
  case ROW_RESULT:
 
1794
  default:
1809
1795
    // This case should never be choosen
1810
1796
    assert(0);
1811
1797
    break;
1824
1810
{
1825
1811
  switch (hybrid_type) {
1826
1812
  case STRING_RESULT:
 
1813
  {
 
1814
    String *result=args[0]->val_str(&tmp_value);
 
1815
    if (!args[0]->null_value &&
 
1816
        (null_value || sortcmp(&value,result,collation.collation) < 0))
1827
1817
    {
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
 
      }
 
1818
      value.copy(*result);
 
1819
      null_value=0;
1835
1820
    }
1836
 
    break;
 
1821
  }
 
1822
  break;
1837
1823
  case INT_RESULT:
 
1824
  {
 
1825
    int64_t nr=args[0]->val_int();
 
1826
    if (!args[0]->null_value && (null_value ||
 
1827
                                 (unsigned_flag &&
 
1828
                                  (uint64_t) nr > (uint64_t) sum_int) ||
 
1829
                                 (!unsigned_flag && nr > sum_int)))
1838
1830
    {
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
 
      }
 
1831
      sum_int=nr;
 
1832
      null_value=0;
1848
1833
    }
1849
 
    break;
 
1834
  }
 
1835
  break;
1850
1836
  case DECIMAL_RESULT:
 
1837
  {
 
1838
    my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1839
    if (!args[0]->null_value &&
 
1840
        (null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
1851
1841
    {
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
 
      }
 
1842
      my_decimal2decimal(val, &sum_dec);
 
1843
      null_value= 0;
1859
1844
    }
1860
 
    break;
 
1845
  }
 
1846
  break;
1861
1847
  case REAL_RESULT:
 
1848
  {
 
1849
    double nr= args[0]->val_real();
 
1850
    if (!args[0]->null_value && (null_value || nr > sum))
1862
1851
    {
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
 
      }
 
1852
      sum=nr;
 
1853
      null_value=0;
1869
1854
    }
1870
 
    break;
 
1855
  }
 
1856
  break;
1871
1857
  case ROW_RESULT:
 
1858
  default:
1872
1859
    // This case should never be choosen
1873
1860
    assert(0);
1874
1861
    break;
1875
1862
  }
1876
 
 
1877
1863
  return 0;
1878
1864
}
1879
1865
 
1961
1947
{
1962
1948
  switch(hybrid_type) {
1963
1949
  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
 
      {
 
1950
  {
 
1951
    char buff[MAX_FIELD_WIDTH];
 
1952
    String tmp(buff,sizeof(buff),result_field->charset()),*res;
 
1953
 
 
1954
    res=args[0]->val_str(&tmp);
 
1955
    if (args[0]->null_value)
 
1956
    {
 
1957
      result_field->set_null();
 
1958
      result_field->reset();
 
1959
    }
 
1960
    else
 
1961
    {
 
1962
      result_field->set_notnull();
 
1963
      result_field->store(res->ptr(),res->length(),tmp.charset());
 
1964
    }
 
1965
    break;
 
1966
  }
 
1967
  case INT_RESULT:
 
1968
  {
 
1969
    int64_t nr=args[0]->val_int();
 
1970
 
 
1971
    if (maybe_null)
 
1972
    {
 
1973
      if (args[0]->null_value)
 
1974
      {
 
1975
        nr=0;
 
1976
        result_field->set_null();
 
1977
      }
 
1978
      else
 
1979
        result_field->set_notnull();
 
1980
    }
 
1981
    result_field->store(nr, unsigned_flag);
 
1982
    break;
 
1983
  }
 
1984
  case REAL_RESULT:
 
1985
  {
 
1986
    double nr= args[0]->val_real();
 
1987
 
 
1988
    if (maybe_null)
 
1989
    {
 
1990
      if (args[0]->null_value)
 
1991
      {
 
1992
        nr=0.0;
 
1993
        result_field->set_null();
 
1994
      }
 
1995
      else
 
1996
        result_field->set_notnull();
 
1997
    }
 
1998
    result_field->store(nr);
 
1999
    break;
 
2000
  }
 
2001
  case DECIMAL_RESULT:
 
2002
  {
 
2003
    my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
 
2004
 
 
2005
    if (maybe_null)
 
2006
    {
 
2007
      if (args[0]->null_value)
1971
2008
        result_field->set_null();
1972
 
        result_field->reset();
1973
 
      }
1974
2009
      else
1975
 
      {
1976
2010
        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
 
    }
 
2011
    }
 
2012
    /*
 
2013
      We must store zero in the field as we will use the field value in
 
2014
      add()
 
2015
    */
 
2016
    if (!arg_dec)                               // Null
 
2017
      arg_dec= &decimal_zero;
 
2018
    result_field->store_decimal(arg_dec);
 
2019
    break;
 
2020
  }
2035
2021
  case ROW_RESULT:
 
2022
  default:
2036
2023
    assert(0);
2037
2024
  }
2038
2025
}
2042
2029
{
2043
2030
  if (hybrid_type == DECIMAL_RESULT)
2044
2031
  {
2045
 
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
 
2032
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2046
2033
    if (!arg_val)                               // Null
2047
2034
      arg_val= &decimal_zero;
2048
2035
    result_field->store_decimal(arg_val);
2077
2064
  if (hybrid_type == DECIMAL_RESULT)
2078
2065
  {
2079
2066
    int64_t tmp;
2080
 
    type::Decimal value, *arg_dec= args[0]->val_decimal(&value);
 
2067
    my_decimal value, *arg_dec= args[0]->val_decimal(&value);
2081
2068
    if (args[0]->null_value)
2082
2069
    {
2083
2070
      arg_dec= &decimal_zero;
2085
2072
    }
2086
2073
    else
2087
2074
      tmp= 1;
2088
 
    arg_dec->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
 
2075
    my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
2089
2076
    res+= dec_bin_size;
2090
2077
    int8store(res, tmp);
2091
2078
  }
2129
2116
{
2130
2117
  if (hybrid_type == DECIMAL_RESULT)
2131
2118
  {
2132
 
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
 
2119
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2133
2120
    if (!args[0]->null_value)
2134
2121
    {
2135
2122
      if (!result_field->is_null())
2136
2123
      {
2137
 
        type::Decimal field_value,
 
2124
        my_decimal field_value,
2138
2125
                   *field_val= result_field->val_decimal(&field_value);
2139
 
        class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
 
2126
        my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2140
2127
        result_field->store_decimal(dec_buffs);
2141
2128
      }
2142
2129
      else
2181
2168
  unsigned char *res=result_field->ptr;
2182
2169
  if (hybrid_type == DECIMAL_RESULT)
2183
2170
  {
2184
 
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
 
2171
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2185
2172
    if (!args[0]->null_value)
2186
2173
    {
2187
 
      binary2_class_decimal(E_DEC_FATAL_ERROR, res,
 
2174
      binary2my_decimal(E_DEC_FATAL_ERROR, res,
2188
2175
                        dec_buffs + 1, f_precision, f_scale);
2189
2176
      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);
 
2177
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
 
2178
      my_decimal2binary(E_DEC_FATAL_ERROR, dec_buffs,
 
2179
                        res, f_precision, f_scale);
2192
2180
      res+= dec_bin_size;
2193
2181
      field_count++;
2194
2182
      int8store(res, field_count);
2226
2214
  case DECIMAL_RESULT:
2227
2215
    min_max_update_decimal_field();
2228
2216
    break;
2229
 
  case REAL_RESULT:
2230
 
  case ROW_RESULT:
 
2217
  default:
2231
2218
    min_max_update_real_field();
2232
2219
  }
2233
2220
}
2240
2227
 
2241
2228
  if (!args[0]->null_value)
2242
2229
  {
2243
 
    result_field->val_str_internal(&tmp_value);
 
2230
    result_field->val_str(&tmp_value);
2244
2231
 
2245
2232
    if (result_field->is_null() ||
2246
2233
        (cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2306
2293
Item_sum_hybrid::min_max_update_decimal_field()
2307
2294
{
2308
2295
  /* 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);
 
2296
  my_decimal old_val, nr_val;
 
2297
  const my_decimal *old_nr= result_field->val_decimal(&old_val);
 
2298
  const my_decimal *nr= args[0]->val_decimal(&nr_val);
2312
2299
  if (!args[0]->null_value)
2313
2300
  {
2314
2301
    if (result_field->is_null(0))
2315
2302
      old_nr=nr;
2316
2303
    else
2317
2304
    {
2318
 
      bool res= class_decimal_cmp(old_nr, nr) > 0;
 
2305
      bool res= my_decimal_cmp(old_nr, nr) > 0;
2319
2306
      /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2320
2307
      if ((cmp_sign > 0) ^ (!res))
2321
2308
        old_nr=nr;
2372
2359
}
2373
2360
 
2374
2361
 
2375
 
type::Decimal *Item_avg_field::val_decimal(type::Decimal *dec_buf)
 
2362
my_decimal *Item_avg_field::val_decimal(my_decimal *dec_buf)
2376
2363
{
2377
2364
  // fix_fields() never calls for this Item
2378
2365
  if (hybrid_type == REAL_RESULT)
2382
2369
  if ((null_value= !count))
2383
2370
    return 0;
2384
2371
 
2385
 
  type::Decimal dec_count, dec_field;
2386
 
  binary2_class_decimal(E_DEC_FATAL_ERROR,
 
2372
  my_decimal dec_count, dec_field;
 
2373
  binary2my_decimal(E_DEC_FATAL_ERROR,
2387
2374
                    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,
 
2375
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
 
2376
  my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2390
2377
                 &dec_field, &dec_count, prec_increment);
2391
2378
  return dec_buf;
2392
2379
}
2417
2404
}
2418
2405
 
2419
2406
 
2420
 
type::Decimal *Item_std_field::val_decimal(type::Decimal *dec_buf)
 
2407
my_decimal *Item_std_field::val_decimal(my_decimal *dec_buf)
2421
2408
{
2422
2409
  /*
2423
2410
    We can't call val_decimal_from_real() for DECIMAL_RESULT as
2424
2411
    Item_variance_field::val_real() would cause an infinite loop
2425
2412
  */
2426
 
  type::Decimal tmp_dec, *dec;
 
2413
  my_decimal tmp_dec, *dec;
2427
2414
  double nr;
2428
2415
  if (hybrid_type == REAL_RESULT)
2429
2416
    return val_decimal_from_real(dec_buf);
2431
2418
  dec= Item_variance_field::val_decimal(dec_buf);
2432
2419
  if (!dec)
2433
2420
    return 0;
2434
 
  class_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
 
2421
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2435
2422
  assert(nr >= 0.0);
2436
2423
  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);
 
2424
  double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
 
2425
  my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
2439
2426
  return dec_buf;
2440
2427
}
2441
2428
 
2584
2571
bool Item_sum_count_distinct::setup(Session *session)
2585
2572
{
2586
2573
  List<Item> list;
2587
 
  Select_Lex *select_lex= session->getLex()->current_select;
 
2574
  Select_Lex *select_lex= session->lex->current_select;
2588
2575
 
2589
2576
  /*
2590
2577
    Setup can be called twice for ROLLUP items. This is a bug.
2612
2599
  tmp_table_param->force_copy_fields= force_copy_fields;
2613
2600
  assert(table == 0);
2614
2601
 
2615
 
  if (!(table= create_tmp_table(session, tmp_table_param, list, (Order*) 0, 1,
 
2602
  if (!(table= create_tmp_table(session, tmp_table_param, list, (order_st*) 0, 1,
2616
2603
                                0,
2617
2604
                                (select_lex->options | session->options),
2618
2605
                                HA_POS_ERROR, (char*)"")))
2669
2656
        uint32_t *length;
2670
2657
        compare_key= (qsort_cmp2) composite_key_cmp;
2671
2658
        cmp_arg= (void*) this;
2672
 
        field_lengths= (uint32_t*) session->getMemRoot()->allocate(table->getShare()->sizeFields() * sizeof(uint32_t));
 
2659
        field_lengths= (uint32_t*) session->alloc(table->getShare()->sizeFields() * sizeof(uint32_t));
2673
2660
        for (tree_key_length= 0, length= field_lengths, field= table->getFields();
2674
2661
             field < field_end; ++field, ++length)
2675
2662
        {
2723
2710
  if (always_null)
2724
2711
    return 0;
2725
2712
  copy_fields(tmp_table_param);
2726
 
  if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
2727
 
    return true;
 
2713
  copy_funcs(tmp_table_param->items_to_copy);
2728
2714
 
2729
2715
  for (Field **field= table->getFields() ; *field ; field++)
2730
2716
  {
2848
2834
                                    const void* key2)
2849
2835
{
2850
2836
  Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2851
 
  Order **order_item, **end;
 
2837
  order_st **order_item, **end;
2852
2838
  Table *table= grp_item->table;
2853
2839
 
2854
2840
  for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2922
2908
      uint32_t offset= (field->offset(field->getTable()->record[0]) -
2923
2909
                    table->getShare()->null_bytes);
2924
2910
      assert(offset < table->getShare()->getRecordLength());
2925
 
      res= field->val_str_internal(&tmp, key + offset);
 
2911
      res= field->val_str(&tmp, key + offset);
2926
2912
    }
2927
2913
    else
2928
2914
      res= (*arg)->val_str(&tmp);
2992
2978
    order - arg_count_order
2993
2979
  */
2994
2980
  if (!(args= (Item**) memory::sql_alloc(sizeof(Item*) * arg_count +
2995
 
                                 sizeof(Order*)*arg_count_order)))
 
2981
                                 sizeof(order_st*)*arg_count_order)))
2996
2982
    return;
2997
2983
 
2998
 
  order= (Order**)(args + arg_count);
 
2984
  order= (order_st**)(args + arg_count);
2999
2985
 
3000
2986
  /* fill args items of show and sort */
3001
 
  List<Item>::iterator li(select_list->begin());
 
2987
  List_iterator_fast<Item> li(*select_list);
3002
2988
 
3003
2989
  for (arg_ptr=args ; (item_select= li++) ; arg_ptr++)
3004
2990
    *arg_ptr= item_select;
3005
2991
 
3006
2992
  if (arg_count_order)
3007
2993
  {
3008
 
    Order **order_ptr= order;
3009
 
    for (Order *order_item= (Order*) order_list->first;
 
2994
    order_st **order_ptr= order;
 
2995
    for (order_st *order_item= (order_st*) order_list->first;
3010
2996
         order_item != NULL;
3011
2997
         order_item= order_item->next)
3012
2998
    {
3053
3039
  {
3054
3040
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
3055
3041
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3056
 
    warning->set_msg(&getSession(), warn_buff);
 
3042
    warning->set_msg(current_session, warn_buff);
3057
3043
    warning= 0;
3058
3044
  }
3059
3045
 
3119
3105
  if (always_null)
3120
3106
    return 0;
3121
3107
  copy_fields(tmp_table_param);
3122
 
  if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
3123
 
    return true;
 
3108
  copy_funcs(tmp_table_param->items_to_copy);
3124
3109
 
3125
3110
  for (uint32_t i= 0; i < arg_count_field; i++)
3126
3111
  {
3208
3193
bool Item_func_group_concat::setup(Session *session)
3209
3194
{
3210
3195
  List<Item> list;
3211
 
  Select_Lex *select_lex= session->getLex()->current_select;
 
3196
  Select_Lex *select_lex= session->lex->current_select;
3212
3197
 
3213
3198
  /*
3214
3199
    Currently setup() can be called twice. Please add
3274
3259
    field list.
3275
3260
  */
3276
3261
  if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3277
 
                                (Order*) 0, 0, true,
 
3262
                                (order_st*) 0, 0, true,
3278
3263
                                (select_lex->options | session->options),
3279
3264
                                HA_POS_ERROR, (char*) "")))
3280
3265
  {