~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/sum.cc

  • Committer: Barry.Leslie at PrimeBase
  • Date: 2011-01-22 03:22:44 UTC
  • mfrom: (2101 staging)
  • mto: (2228.1.4 build)
  • mto: This revision was merged to the branch mainline in revision 2230.
  • Revision ID: barry.leslie@primebase.com-20110122032244-ukbe3mlj7fs8xph6
Merged with lp:drizzle.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#include <drizzled/item/sum.h>
34
34
#include <drizzled/field/decimal.h>
35
35
#include <drizzled/field/double.h>
36
 
#include <drizzled/field/int64_t.h>
 
36
#include <drizzled/field/int64.h>
37
37
#include <drizzled/field/date.h>
38
38
#include <drizzled/field/datetime.h>
39
39
 
46
46
namespace drizzled
47
47
{
48
48
 
49
 
extern my_decimal decimal_zero;
 
49
extern type::Decimal decimal_zero;
50
50
extern plugin::StorageEngine *heap_engine;
51
51
 
52
52
/**
508
508
                                  Table *table,
509
509
                                  uint32_t convert_blob_length)
510
510
{
511
 
  Field *field;
 
511
  Field *field= NULL;
 
512
 
512
513
  switch (result_type()) {
513
514
  case REAL_RESULT:
514
515
    field= new Field_double(max_length, maybe_null, name, decimals, true);
515
516
    break;
 
517
 
516
518
  case INT_RESULT:
517
 
    field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
 
519
    field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
518
520
    break;
 
521
 
519
522
  case STRING_RESULT:
520
523
    if (max_length/collation.collation->mbmaxlen <= 255 ||
521
524
        convert_blob_length > Field_varstring::MAX_SIZE ||
522
525
        !convert_blob_length)
 
526
    {
523
527
      return make_string_field(table);
 
528
    }
524
529
 
525
530
    table->setVariableWidth();
526
531
    field= new Field_varstring(convert_blob_length, maybe_null,
527
532
                               name, collation.collation);
528
533
    break;
 
534
 
529
535
  case DECIMAL_RESULT:
530
536
    field= new Field_decimal(max_length, maybe_null, name,
531
 
                                 decimals, unsigned_flag);
 
537
                             decimals, unsigned_flag);
532
538
    break;
 
539
 
533
540
  case ROW_RESULT:
534
 
  default:
535
541
    // This case should never be choosen
536
542
    assert(0);
537
543
    return 0;
538
544
  }
 
545
 
539
546
  if (field)
540
547
    field->init(table);
 
548
 
541
549
  return field;
542
550
}
543
551
 
575
583
}
576
584
 
577
585
 
578
 
my_decimal *Item_sum_num::val_decimal(my_decimal *decimal_value)
 
586
type::Decimal *Item_sum_num::val_decimal(type::Decimal *decimal_value)
579
587
{
580
588
  return val_decimal_from_real(decimal_value);
581
589
}
588
596
}
589
597
 
590
598
 
591
 
my_decimal *Item_sum_int::val_decimal(my_decimal *decimal_value)
 
599
type::Decimal *Item_sum_int::val_decimal(type::Decimal *decimal_value)
592
600
{
593
601
  return val_decimal_from_int(decimal_value);
594
602
}
635
643
    sum_int= item->sum_int;
636
644
    break;
637
645
  case DECIMAL_RESULT:
638
 
    my_decimal2decimal(&item->sum_dec, &sum_dec);
 
646
    class_decimal2decimal(&item->sum_dec, &sum_dec);
639
647
    break;
640
648
  case REAL_RESULT:
641
649
    sum= item->sum;
647
655
    */
648
656
    break;
649
657
  case ROW_RESULT:
650
 
  default:
651
658
    assert(0);
652
659
  }
653
660
  collation.set(item->collation);
676
683
    break;
677
684
  case DECIMAL_RESULT:
678
685
    max_length= item->max_length;
679
 
    my_decimal_set_zero(&sum_dec);
 
686
    sum_dec.set_zero();
680
687
    break;
681
688
  case REAL_RESULT:
682
689
    max_length= float_length(decimals);
686
693
    max_length= item->max_length;
687
694
    break;
688
695
  case ROW_RESULT:
689
 
  default:
690
696
    assert(0);
691
697
  };
692
698
  /* MIN/MAX can return NULL for empty set indepedent of the used column */
759
765
  /* TODO: check if the following assignments are really needed */
760
766
  if (hybrid_type == DECIMAL_RESULT)
761
767
  {
762
 
    my_decimal2decimal(item->dec_buffs, dec_buffs);
763
 
    my_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
 
768
    class_decimal2decimal(item->dec_buffs, dec_buffs);
 
769
    class_decimal2decimal(item->dec_buffs + 1, dec_buffs + 1);
764
770
  }
765
771
  else
766
772
    sum= item->sum;
778
784
  if (hybrid_type == DECIMAL_RESULT)
779
785
  {
780
786
    curr_dec_buff= 0;
781
 
    my_decimal_set_zero(dec_buffs);
 
787
    dec_buffs->set_zero();
782
788
  }
783
789
  else
784
790
    sum= 0.0;
798
804
    break;
799
805
  case INT_RESULT:
800
806
  case DECIMAL_RESULT:
801
 
  {
802
 
    /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
803
 
    int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
804
 
    max_length= my_decimal_precision_to_length(precision, decimals,
805
 
                                               unsigned_flag);
806
 
    curr_dec_buff= 0;
807
 
    hybrid_type= DECIMAL_RESULT;
808
 
    my_decimal_set_zero(dec_buffs);
809
 
    break;
810
 
  }
 
807
    {
 
808
      /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
 
809
      int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
 
810
      max_length= class_decimal_precision_to_length(precision, decimals,
 
811
                                                 unsigned_flag);
 
812
      curr_dec_buff= 0;
 
813
      hybrid_type= DECIMAL_RESULT;
 
814
      dec_buffs->set_zero();
 
815
      break;
 
816
    }
811
817
  case ROW_RESULT:
812
 
  default:
813
818
    assert(0);
814
819
  }
815
 
  return;
816
820
}
817
821
 
818
822
 
820
824
{
821
825
  if (hybrid_type == DECIMAL_RESULT)
822
826
  {
823
 
    my_decimal value, *val= args[0]->val_decimal(&value);
 
827
    type::Decimal value, *val= args[0]->val_decimal(&value);
824
828
    if (!args[0]->null_value)
825
829
    {
826
 
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
 
830
      class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs + (curr_dec_buff^1),
827
831
                     val, dec_buffs + curr_dec_buff);
828
832
      curr_dec_buff^= 1;
829
833
      null_value= 0;
845
849
  if (hybrid_type == DECIMAL_RESULT)
846
850
  {
847
851
    int64_t result;
848
 
    my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
849
 
                   &result);
 
852
    (dec_buffs + curr_dec_buff)->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
850
853
    return result;
851
854
  }
852
855
  return (int64_t) rint(val_real());
857
860
{
858
861
  assert(fixed == 1);
859
862
  if (hybrid_type == DECIMAL_RESULT)
860
 
    my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
 
863
    class_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
861
864
  return sum;
862
865
}
863
866
 
870
873
}
871
874
 
872
875
 
873
 
my_decimal *Item_sum_sum::val_decimal(my_decimal *val)
 
876
type::Decimal *Item_sum_sum::val_decimal(type::Decimal *val)
874
877
{
875
878
  if (hybrid_type == DECIMAL_RESULT)
876
879
    return (dec_buffs + curr_dec_buff);
933
936
 
934
937
  virtual void div(Hybrid_type *val, uint64_t u) const
935
938
  {
936
 
    int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
 
939
    int2_class_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
937
940
    val->used_dec_buf_no= 0;
938
941
    val->traits= Hybrid_type_traits_decimal::instance();
939
942
    val->traits->div(val, u);
966
969
    table_field_type= DRIZZLE_TYPE_DOUBLE;
967
970
    break;
968
971
  case INT_RESULT:
969
 
  /*
970
 
    Preserving int8, int16, int32 field types gives ~10% performance boost
971
 
    as the size of result tree becomes significantly smaller.
972
 
    Another speed up we gain by using int64_t for intermediate
973
 
    calculations. The range of int64 is enough to hold sum 2^32 distinct
974
 
    integers each <= 2^32.
975
 
  */
976
 
  if (table_field_type == DRIZZLE_TYPE_LONG)
977
 
  {
978
 
    val.traits= Hybrid_type_traits_fast_decimal::instance();
979
 
    break;
980
 
  }
981
 
  table_field_type= DRIZZLE_TYPE_LONGLONG;
982
 
  /* fallthrough */
 
972
    /*
 
973
      Preserving int8, int16, int32 field types gives ~10% performance boost
 
974
      as the size of result tree becomes significantly smaller.
 
975
      Another speed up we gain by using int64_t for intermediate
 
976
      calculations. The range of int64 is enough to hold sum 2^32 distinct
 
977
      integers each <= 2^32.
 
978
    */
 
979
    if (table_field_type == DRIZZLE_TYPE_LONG)
 
980
    {
 
981
      val.traits= Hybrid_type_traits_fast_decimal::instance();
 
982
      break;
 
983
    }
 
984
    table_field_type= DRIZZLE_TYPE_LONGLONG;
 
985
    /* fallthrough */
983
986
  case DECIMAL_RESULT:
984
987
    val.traits= Hybrid_type_traits_decimal::instance();
985
988
    if (table_field_type != DRIZZLE_TYPE_LONGLONG)
986
989
      table_field_type= DRIZZLE_TYPE_DECIMAL;
987
990
    break;
988
991
  case ROW_RESULT:
989
 
  default:
990
992
    assert(0);
991
993
  }
 
994
 
992
995
  val.traits->fix_length_and_dec(this, args[0]);
993
996
}
994
997
 
1127
1130
}
1128
1131
 
1129
1132
 
1130
 
my_decimal *Item_sum_distinct::val_decimal(my_decimal *to)
 
1133
type::Decimal *Item_sum_distinct::val_decimal(type::Decimal *to)
1131
1134
{
1132
1135
  calculate_val_and_count();
1133
1136
  if (null_value)
1227
1230
  {
1228
1231
    int precision= args[0]->decimal_precision() + prec_increment;
1229
1232
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1230
 
    max_length= my_decimal_precision_to_length(precision, decimals,
 
1233
    max_length= class_decimal_precision_to_length(precision, decimals,
1231
1234
                                               unsigned_flag);
1232
1235
    f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1233
1236
    f_scale=  args[0]->decimals;
1234
 
    dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
 
1237
    dec_bin_size= class_decimal_get_binary_size(f_precision, f_scale);
1235
1238
  }
1236
1239
  else {
1237
1240
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1307
1310
}
1308
1311
 
1309
1312
 
1310
 
my_decimal *Item_sum_avg::val_decimal(my_decimal *val)
 
1313
type::Decimal *Item_sum_avg::val_decimal(type::Decimal *val)
1311
1314
{
1312
 
  my_decimal sum_buff, cnt;
1313
 
  const my_decimal *sum_dec;
 
1315
  type::Decimal sum_buff, cnt;
 
1316
  const type::Decimal *sum_dec;
1314
1317
  assert(fixed == 1);
1315
1318
  if (!count)
1316
1319
  {
1326
1329
    return val_decimal_from_real(val);
1327
1330
 
1328
1331
  sum_dec= dec_buffs + curr_dec_buff;
1329
 
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
1330
 
  my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
 
1332
  int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
 
1333
  class_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
1331
1334
  return val;
1332
1335
}
1333
1336
 
1436
1439
    break;
1437
1440
  case INT_RESULT:
1438
1441
  case DECIMAL_RESULT:
1439
 
  {
1440
 
    int precision= args[0]->decimal_precision()*2 + prec_increment;
1441
 
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1442
 
    max_length= my_decimal_precision_to_length(precision, decimals,
1443
 
                                               unsigned_flag);
 
1442
    {
 
1443
      int precision= args[0]->decimal_precision()*2 + prec_increment;
 
1444
      decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
 
1445
      max_length= class_decimal_precision_to_length(precision, decimals,
 
1446
                                                 unsigned_flag);
1444
1447
 
1445
 
    break;
1446
 
  }
 
1448
      break;
 
1449
    }
1447
1450
  case ROW_RESULT:
1448
 
  default:
1449
1451
    assert(0);
1450
1452
  }
1451
 
  return;
1452
1453
}
1453
1454
 
1454
1455
 
1537
1538
}
1538
1539
 
1539
1540
 
1540
 
my_decimal *Item_sum_variance::val_decimal(my_decimal *dec_buf)
 
1541
type::Decimal *Item_sum_variance::val_decimal(type::Decimal *dec_buf)
1541
1542
{
1542
1543
  assert(fixed == 1);
1543
1544
  return val_decimal_from_real(dec_buf);
1601
1602
    sum_int= 0;
1602
1603
    break;
1603
1604
  case DECIMAL_RESULT:
1604
 
    my_decimal_set_zero(&sum_dec);
 
1605
    sum_dec.set_zero();
1605
1606
    break;
1606
1607
  case REAL_RESULT:
1607
1608
    sum= 0.0;
1617
1618
  assert(fixed == 1);
1618
1619
  if (null_value)
1619
1620
    return 0.0;
 
1621
 
1620
1622
  switch (hybrid_type) {
1621
1623
  case STRING_RESULT:
1622
 
  {
1623
 
    char *end_not_used;
1624
 
    int err_not_used;
1625
 
    String *res;  res=val_str(&str_value);
1626
 
    return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1627
 
                             &end_not_used, &err_not_used) : 0.0);
1628
 
  }
 
1624
    {
 
1625
      char *end_not_used;
 
1626
      int err_not_used;
 
1627
      String *res;  res=val_str(&str_value);
 
1628
      return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
 
1629
                               &end_not_used, &err_not_used) : 0.0);
 
1630
    }
1629
1631
  case INT_RESULT:
1630
1632
    return (double) sum_int;
1631
1633
  case DECIMAL_RESULT:
1632
 
    my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
 
1634
    class_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1633
1635
    return sum;
1634
1636
  case REAL_RESULT:
1635
1637
    return sum;
1636
1638
  case ROW_RESULT:
1637
 
  default:
1638
1639
    // This case should never be choosen
1639
 
    assert(0);
1640
 
    return 0;
 
1640
    break;
1641
1641
  }
 
1642
 
 
1643
  assert(0);
 
1644
  return 0;
1642
1645
}
1643
1646
 
1644
1647
int64_t Item_sum_hybrid::val_int()
1652
1655
  case DECIMAL_RESULT:
1653
1656
  {
1654
1657
    int64_t result;
1655
 
    my_decimal2int(E_DEC_FATAL_ERROR, &sum_dec, unsigned_flag, &result);
 
1658
    sum_dec.val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
1656
1659
    return sum_int;
1657
1660
  }
1658
1661
  default:
1661
1664
}
1662
1665
 
1663
1666
 
1664
 
my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
 
1667
type::Decimal *Item_sum_hybrid::val_decimal(type::Decimal *val)
1665
1668
{
1666
1669
  assert(fixed == 1);
1667
1670
  if (null_value)
1668
1671
    return 0;
 
1672
 
1669
1673
  switch (hybrid_type) {
1670
1674
  case STRING_RESULT:
1671
 
    string2my_decimal(E_DEC_FATAL_ERROR, &value, val);
 
1675
    val->store(E_DEC_FATAL_ERROR, &value);
1672
1676
    break;
1673
1677
  case REAL_RESULT:
1674
 
    double2my_decimal(E_DEC_FATAL_ERROR, sum, val);
 
1678
    double2_class_decimal(E_DEC_FATAL_ERROR, sum, val);
1675
1679
    break;
1676
1680
  case DECIMAL_RESULT:
1677
1681
    val= &sum_dec;
1678
1682
    break;
1679
1683
  case INT_RESULT:
1680
 
    int2my_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
 
1684
    int2_class_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1681
1685
    break;
1682
1686
  case ROW_RESULT:
1683
 
  default:
1684
1687
    // This case should never be choosen
1685
1688
    assert(0);
1686
1689
    break;
1687
1690
  }
 
1691
 
1688
1692
  return val;                                   // Keep compiler happy
1689
1693
}
1690
1694
 
1695
1699
  assert(fixed == 1);
1696
1700
  if (null_value)
1697
1701
    return 0;
 
1702
 
1698
1703
  switch (hybrid_type) {
1699
1704
  case STRING_RESULT:
1700
1705
    return &value;
1702
1707
    str->set_real(sum,decimals, &my_charset_bin);
1703
1708
    break;
1704
1709
  case DECIMAL_RESULT:
1705
 
    my_decimal2string(E_DEC_FATAL_ERROR, &sum_dec, 0, 0, 0, str);
 
1710
    class_decimal2string(E_DEC_FATAL_ERROR, &sum_dec, 0, 0, 0, str);
1706
1711
    return str;
1707
1712
  case INT_RESULT:
1708
1713
    str->set_int(sum_int, unsigned_flag, &my_charset_bin);
1710
1715
  case ROW_RESULT:
1711
1716
  default:
1712
1717
    // This case should never be choosen
1713
 
    assert(0);
1714
1718
    break;
1715
1719
  }
 
1720
 
1716
1721
  return str;                                   // Keep compiler happy
1717
1722
}
1718
1723
 
1750
1755
{
1751
1756
  switch (hybrid_type) {
1752
1757
  case STRING_RESULT:
1753
 
  {
1754
 
    String *result=args[0]->val_str(&tmp_value);
1755
 
    if (!args[0]->null_value &&
1756
 
        (null_value || sortcmp(&value,result,collation.collation) > 0))
1757
1758
    {
1758
 
      value.copy(*result);
1759
 
      null_value=0;
 
1759
      String *result=args[0]->val_str(&tmp_value);
 
1760
      if (!args[0]->null_value &&
 
1761
          (null_value || sortcmp(&value,result,collation.collation) > 0))
 
1762
      {
 
1763
        value.copy(*result);
 
1764
        null_value=0;
 
1765
      }
1760
1766
    }
1761
 
  }
1762
 
  break;
 
1767
    break;
1763
1768
  case INT_RESULT:
1764
 
  {
1765
 
    int64_t nr=args[0]->val_int();
1766
 
    if (!args[0]->null_value && (null_value ||
1767
 
                                 (unsigned_flag &&
1768
 
                                  (uint64_t) nr < (uint64_t) sum_int) ||
1769
 
                                 (!unsigned_flag && nr < sum_int)))
1770
1769
    {
1771
 
      sum_int=nr;
1772
 
      null_value=0;
 
1770
      int64_t nr=args[0]->val_int();
 
1771
      if (!args[0]->null_value && (null_value ||
 
1772
                                   (unsigned_flag &&
 
1773
                                    (uint64_t) nr < (uint64_t) sum_int) ||
 
1774
                                   (!unsigned_flag && nr < sum_int)))
 
1775
      {
 
1776
        sum_int=nr;
 
1777
        null_value=0;
 
1778
      }
1773
1779
    }
1774
 
  }
1775
 
  break;
 
1780
    break;
1776
1781
  case DECIMAL_RESULT:
1777
 
  {
1778
 
    my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1779
 
    if (!args[0]->null_value &&
1780
 
        (null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
1781
1782
    {
1782
 
      my_decimal2decimal(val, &sum_dec);
1783
 
      null_value= 0;
 
1783
      type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1784
      if (!args[0]->null_value &&
 
1785
          (null_value || (class_decimal_cmp(&sum_dec, val) > 0)))
 
1786
      {
 
1787
        class_decimal2decimal(val, &sum_dec);
 
1788
        null_value= 0;
 
1789
      }
1784
1790
    }
1785
 
  }
1786
 
  break;
 
1791
    break;
1787
1792
  case REAL_RESULT:
1788
 
  {
1789
 
    double nr= args[0]->val_real();
1790
 
    if (!args[0]->null_value && (null_value || nr < sum))
1791
1793
    {
1792
 
      sum=nr;
1793
 
      null_value=0;
 
1794
      double nr= args[0]->val_real();
 
1795
      if (!args[0]->null_value && (null_value || nr < sum))
 
1796
      {
 
1797
        sum=nr;
 
1798
        null_value=0;
 
1799
      }
1794
1800
    }
1795
 
  }
1796
 
  break;
 
1801
    break;
1797
1802
  case ROW_RESULT:
1798
 
  default:
1799
1803
    // This case should never be choosen
1800
1804
    assert(0);
1801
1805
    break;
1814
1818
{
1815
1819
  switch (hybrid_type) {
1816
1820
  case STRING_RESULT:
1817
 
  {
1818
 
    String *result=args[0]->val_str(&tmp_value);
1819
 
    if (!args[0]->null_value &&
1820
 
        (null_value || sortcmp(&value,result,collation.collation) < 0))
1821
1821
    {
1822
 
      value.copy(*result);
1823
 
      null_value=0;
 
1822
      String *result=args[0]->val_str(&tmp_value);
 
1823
      if (!args[0]->null_value &&
 
1824
          (null_value || sortcmp(&value,result,collation.collation) < 0))
 
1825
      {
 
1826
        value.copy(*result);
 
1827
        null_value=0;
 
1828
      }
1824
1829
    }
1825
 
  }
1826
 
  break;
 
1830
    break;
1827
1831
  case INT_RESULT:
1828
 
  {
1829
 
    int64_t nr=args[0]->val_int();
1830
 
    if (!args[0]->null_value && (null_value ||
1831
 
                                 (unsigned_flag &&
1832
 
                                  (uint64_t) nr > (uint64_t) sum_int) ||
1833
 
                                 (!unsigned_flag && nr > sum_int)))
1834
1832
    {
1835
 
      sum_int=nr;
1836
 
      null_value=0;
 
1833
      int64_t nr=args[0]->val_int();
 
1834
      if (!args[0]->null_value && (null_value ||
 
1835
                                   (unsigned_flag &&
 
1836
                                    (uint64_t) nr > (uint64_t) sum_int) ||
 
1837
                                   (!unsigned_flag && nr > sum_int)))
 
1838
      {
 
1839
        sum_int=nr;
 
1840
        null_value=0;
 
1841
      }
1837
1842
    }
1838
 
  }
1839
 
  break;
 
1843
    break;
1840
1844
  case DECIMAL_RESULT:
1841
 
  {
1842
 
    my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1843
 
    if (!args[0]->null_value &&
1844
 
        (null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
1845
1845
    {
1846
 
      my_decimal2decimal(val, &sum_dec);
1847
 
      null_value= 0;
 
1846
      type::Decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1847
      if (!args[0]->null_value &&
 
1848
          (null_value || (class_decimal_cmp(val, &sum_dec) > 0)))
 
1849
      {
 
1850
        class_decimal2decimal(val, &sum_dec);
 
1851
        null_value= 0;
 
1852
      }
1848
1853
    }
1849
 
  }
1850
 
  break;
 
1854
    break;
1851
1855
  case REAL_RESULT:
1852
 
  {
1853
 
    double nr= args[0]->val_real();
1854
 
    if (!args[0]->null_value && (null_value || nr > sum))
1855
1856
    {
1856
 
      sum=nr;
1857
 
      null_value=0;
 
1857
      double nr= args[0]->val_real();
 
1858
      if (!args[0]->null_value && (null_value || nr > sum))
 
1859
      {
 
1860
        sum=nr;
 
1861
        null_value=0;
 
1862
      }
1858
1863
    }
1859
 
  }
1860
 
  break;
 
1864
    break;
1861
1865
  case ROW_RESULT:
1862
 
  default:
1863
1866
    // This case should never be choosen
1864
1867
    assert(0);
1865
1868
    break;
1866
1869
  }
 
1870
 
1867
1871
  return 0;
1868
1872
}
1869
1873
 
1951
1955
{
1952
1956
  switch(hybrid_type) {
1953
1957
  case STRING_RESULT:
1954
 
  {
1955
 
    char buff[MAX_FIELD_WIDTH];
1956
 
    String tmp(buff,sizeof(buff),result_field->charset()),*res;
1957
 
 
1958
 
    res=args[0]->val_str(&tmp);
1959
 
    if (args[0]->null_value)
1960
 
    {
1961
 
      result_field->set_null();
1962
 
      result_field->reset();
1963
 
    }
1964
 
    else
1965
 
    {
1966
 
      result_field->set_notnull();
1967
 
      result_field->store(res->ptr(),res->length(),tmp.charset());
1968
 
    }
1969
 
    break;
1970
 
  }
1971
 
  case INT_RESULT:
1972
 
  {
1973
 
    int64_t nr=args[0]->val_int();
1974
 
 
1975
 
    if (maybe_null)
1976
 
    {
1977
 
      if (args[0]->null_value)
1978
 
      {
1979
 
        nr=0;
1980
 
        result_field->set_null();
1981
 
      }
1982
 
      else
1983
 
        result_field->set_notnull();
1984
 
    }
1985
 
    result_field->store(nr, unsigned_flag);
1986
 
    break;
1987
 
  }
1988
 
  case REAL_RESULT:
1989
 
  {
1990
 
    double nr= args[0]->val_real();
1991
 
 
1992
 
    if (maybe_null)
1993
 
    {
1994
 
      if (args[0]->null_value)
1995
 
      {
1996
 
        nr=0.0;
1997
 
        result_field->set_null();
1998
 
      }
1999
 
      else
2000
 
        result_field->set_notnull();
2001
 
    }
2002
 
    result_field->store(nr);
2003
 
    break;
2004
 
  }
2005
 
  case DECIMAL_RESULT:
2006
 
  {
2007
 
    my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2008
 
 
2009
 
    if (maybe_null)
2010
 
    {
2011
 
      if (args[0]->null_value)
 
1958
    {
 
1959
      char buff[MAX_FIELD_WIDTH];
 
1960
      String tmp(buff,sizeof(buff),result_field->charset()),*res;
 
1961
 
 
1962
      res=args[0]->val_str(&tmp);
 
1963
      if (args[0]->null_value)
 
1964
      {
2012
1965
        result_field->set_null();
 
1966
        result_field->reset();
 
1967
      }
2013
1968
      else
 
1969
      {
2014
1970
        result_field->set_notnull();
2015
 
    }
2016
 
    /*
2017
 
      We must store zero in the field as we will use the field value in
2018
 
      add()
2019
 
    */
2020
 
    if (!arg_dec)                               // Null
2021
 
      arg_dec= &decimal_zero;
2022
 
    result_field->store_decimal(arg_dec);
2023
 
    break;
2024
 
  }
 
1971
        result_field->store(res->ptr(),res->length(),tmp.charset());
 
1972
      }
 
1973
      break;
 
1974
    }
 
1975
  case INT_RESULT:
 
1976
    {
 
1977
      int64_t nr=args[0]->val_int();
 
1978
 
 
1979
      if (maybe_null)
 
1980
      {
 
1981
        if (args[0]->null_value)
 
1982
        {
 
1983
          nr=0;
 
1984
          result_field->set_null();
 
1985
        }
 
1986
        else
 
1987
          result_field->set_notnull();
 
1988
      }
 
1989
      result_field->store(nr, unsigned_flag);
 
1990
      break;
 
1991
    }
 
1992
  case REAL_RESULT:
 
1993
    {
 
1994
      double nr= args[0]->val_real();
 
1995
 
 
1996
      if (maybe_null)
 
1997
      {
 
1998
        if (args[0]->null_value)
 
1999
        {
 
2000
          nr=0.0;
 
2001
          result_field->set_null();
 
2002
        }
 
2003
        else
 
2004
          result_field->set_notnull();
 
2005
      }
 
2006
      result_field->store(nr);
 
2007
      break;
 
2008
    }
 
2009
  case DECIMAL_RESULT:
 
2010
    {
 
2011
      type::Decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
 
2012
 
 
2013
      if (maybe_null)
 
2014
      {
 
2015
        if (args[0]->null_value)
 
2016
          result_field->set_null();
 
2017
        else
 
2018
          result_field->set_notnull();
 
2019
      }
 
2020
      /*
 
2021
        We must store zero in the field as we will use the field value in
 
2022
        add()
 
2023
      */
 
2024
      if (!arg_dec)                               // Null
 
2025
        arg_dec= &decimal_zero;
 
2026
      result_field->store_decimal(arg_dec);
 
2027
      break;
 
2028
    }
2025
2029
  case ROW_RESULT:
2026
 
  default:
2027
2030
    assert(0);
2028
2031
  }
2029
2032
}
2033
2036
{
2034
2037
  if (hybrid_type == DECIMAL_RESULT)
2035
2038
  {
2036
 
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
 
2039
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2037
2040
    if (!arg_val)                               // Null
2038
2041
      arg_val= &decimal_zero;
2039
2042
    result_field->store_decimal(arg_val);
2068
2071
  if (hybrid_type == DECIMAL_RESULT)
2069
2072
  {
2070
2073
    int64_t tmp;
2071
 
    my_decimal value, *arg_dec= args[0]->val_decimal(&value);
 
2074
    type::Decimal value, *arg_dec= args[0]->val_decimal(&value);
2072
2075
    if (args[0]->null_value)
2073
2076
    {
2074
2077
      arg_dec= &decimal_zero;
2076
2079
    }
2077
2080
    else
2078
2081
      tmp= 1;
2079
 
    my_decimal2binary(E_DEC_FATAL_ERROR, arg_dec, res, f_precision, f_scale);
 
2082
    arg_dec->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
2080
2083
    res+= dec_bin_size;
2081
2084
    int8store(res, tmp);
2082
2085
  }
2120
2123
{
2121
2124
  if (hybrid_type == DECIMAL_RESULT)
2122
2125
  {
2123
 
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
 
2126
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2124
2127
    if (!args[0]->null_value)
2125
2128
    {
2126
2129
      if (!result_field->is_null())
2127
2130
      {
2128
 
        my_decimal field_value,
 
2131
        type::Decimal field_value,
2129
2132
                   *field_val= result_field->val_decimal(&field_value);
2130
 
        my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
 
2133
        class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, field_val);
2131
2134
        result_field->store_decimal(dec_buffs);
2132
2135
      }
2133
2136
      else
2172
2175
  unsigned char *res=result_field->ptr;
2173
2176
  if (hybrid_type == DECIMAL_RESULT)
2174
2177
  {
2175
 
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
 
2178
    type::Decimal value, *arg_val= args[0]->val_decimal(&value);
2176
2179
    if (!args[0]->null_value)
2177
2180
    {
2178
 
      binary2my_decimal(E_DEC_FATAL_ERROR, res,
 
2181
      binary2_class_decimal(E_DEC_FATAL_ERROR, res,
2179
2182
                        dec_buffs + 1, f_precision, f_scale);
2180
2183
      field_count= sint8korr(res + dec_bin_size);
2181
 
      my_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
2182
 
      my_decimal2binary(E_DEC_FATAL_ERROR, dec_buffs,
2183
 
                        res, f_precision, f_scale);
 
2184
      class_decimal_add(E_DEC_FATAL_ERROR, dec_buffs, arg_val, dec_buffs + 1);
 
2185
      dec_buffs->val_binary(E_DEC_FATAL_ERROR, res, f_precision, f_scale);
2184
2186
      res+= dec_bin_size;
2185
2187
      field_count++;
2186
2188
      int8store(res, field_count);
2218
2220
  case DECIMAL_RESULT:
2219
2221
    min_max_update_decimal_field();
2220
2222
    break;
2221
 
  default:
 
2223
  case REAL_RESULT:
 
2224
  case ROW_RESULT:
2222
2225
    min_max_update_real_field();
2223
2226
  }
2224
2227
}
2231
2234
 
2232
2235
  if (!args[0]->null_value)
2233
2236
  {
2234
 
    result_field->val_str(&tmp_value);
 
2237
    result_field->val_str_internal(&tmp_value);
2235
2238
 
2236
2239
    if (result_field->is_null() ||
2237
2240
        (cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2297
2300
Item_sum_hybrid::min_max_update_decimal_field()
2298
2301
{
2299
2302
  /* TODO: optimize: do not get result_field in case of args[0] is NULL */
2300
 
  my_decimal old_val, nr_val;
2301
 
  const my_decimal *old_nr= result_field->val_decimal(&old_val);
2302
 
  const my_decimal *nr= args[0]->val_decimal(&nr_val);
 
2303
  type::Decimal old_val, nr_val;
 
2304
  const type::Decimal *old_nr= result_field->val_decimal(&old_val);
 
2305
  const type::Decimal *nr= args[0]->val_decimal(&nr_val);
2303
2306
  if (!args[0]->null_value)
2304
2307
  {
2305
2308
    if (result_field->is_null(0))
2306
2309
      old_nr=nr;
2307
2310
    else
2308
2311
    {
2309
 
      bool res= my_decimal_cmp(old_nr, nr) > 0;
 
2312
      bool res= class_decimal_cmp(old_nr, nr) > 0;
2310
2313
      /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2311
2314
      if ((cmp_sign > 0) ^ (!res))
2312
2315
        old_nr=nr;
2363
2366
}
2364
2367
 
2365
2368
 
2366
 
my_decimal *Item_avg_field::val_decimal(my_decimal *dec_buf)
 
2369
type::Decimal *Item_avg_field::val_decimal(type::Decimal *dec_buf)
2367
2370
{
2368
2371
  // fix_fields() never calls for this Item
2369
2372
  if (hybrid_type == REAL_RESULT)
2373
2376
  if ((null_value= !count))
2374
2377
    return 0;
2375
2378
 
2376
 
  my_decimal dec_count, dec_field;
2377
 
  binary2my_decimal(E_DEC_FATAL_ERROR,
 
2379
  type::Decimal dec_count, dec_field;
 
2380
  binary2_class_decimal(E_DEC_FATAL_ERROR,
2378
2381
                    field->ptr, &dec_field, f_precision, f_scale);
2379
 
  int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
2380
 
  my_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
 
2382
  int2_class_decimal(E_DEC_FATAL_ERROR, count, 0, &dec_count);
 
2383
  class_decimal_div(E_DEC_FATAL_ERROR, dec_buf,
2381
2384
                 &dec_field, &dec_count, prec_increment);
2382
2385
  return dec_buf;
2383
2386
}
2408
2411
}
2409
2412
 
2410
2413
 
2411
 
my_decimal *Item_std_field::val_decimal(my_decimal *dec_buf)
 
2414
type::Decimal *Item_std_field::val_decimal(type::Decimal *dec_buf)
2412
2415
{
2413
2416
  /*
2414
2417
    We can't call val_decimal_from_real() for DECIMAL_RESULT as
2415
2418
    Item_variance_field::val_real() would cause an infinite loop
2416
2419
  */
2417
 
  my_decimal tmp_dec, *dec;
 
2420
  type::Decimal tmp_dec, *dec;
2418
2421
  double nr;
2419
2422
  if (hybrid_type == REAL_RESULT)
2420
2423
    return val_decimal_from_real(dec_buf);
2422
2425
  dec= Item_variance_field::val_decimal(dec_buf);
2423
2426
  if (!dec)
2424
2427
    return 0;
2425
 
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
 
2428
  class_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2426
2429
  assert(nr >= 0.0);
2427
2430
  nr= sqrt(nr);
2428
 
  double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2429
 
  my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
 
2431
  double2_class_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
 
2432
  class_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
2430
2433
  return dec_buf;
2431
2434
}
2432
2435
 
2913
2916
      uint32_t offset= (field->offset(field->getTable()->record[0]) -
2914
2917
                    table->getShare()->null_bytes);
2915
2918
      assert(offset < table->getShare()->getRecordLength());
2916
 
      res= field->val_str(&tmp, key + offset);
 
2919
      res= field->val_str_internal(&tmp, key + offset);
2917
2920
    }
2918
2921
    else
2919
2922
      res= (*arg)->val_str(&tmp);