~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/sum.cc

  • Committer: Stewart Smith
  • Date: 2010-03-18 12:01:34 UTC
  • mto: (1666.2.3 build)
  • mto: This revision was merged to the branch mainline in revision 1596.
  • Revision ID: stewart@flamingspork.com-20100318120134-45fdnsw8g3j6c7oy
move RAND() into a plugin

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
/**
21
21
  Sum functions (COUNT, MIN...)
22
22
*/
23
23
#include "config.h"
24
 
#include <cstdio>
25
24
#include <math.h>
26
25
#include <drizzled/sql_select.h>
27
26
#include <drizzled/error.h>
33
32
#include <drizzled/item/sum.h>
34
33
#include <drizzled/field/decimal.h>
35
34
#include <drizzled/field/double.h>
36
 
#include <drizzled/field/int64.h>
 
35
#include <drizzled/field/int64_t.h>
37
36
#include <drizzled/field/date.h>
38
37
#include <drizzled/field/datetime.h>
39
38
 
508
507
                                  Table *table,
509
508
                                  uint32_t convert_blob_length)
510
509
{
511
 
  Field *field= NULL;
512
 
 
 
510
  Field *field;
513
511
  switch (result_type()) {
514
512
  case REAL_RESULT:
515
513
    field= new Field_double(max_length, maybe_null, name, decimals, true);
516
514
    break;
517
 
 
518
515
  case INT_RESULT:
519
 
    field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
 
516
    field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
520
517
    break;
521
 
 
522
518
  case STRING_RESULT:
523
519
    if (max_length/collation.collation->mbmaxlen <= 255 ||
524
520
        convert_blob_length > Field_varstring::MAX_SIZE ||
525
521
        !convert_blob_length)
526
 
    {
527
522
      return make_string_field(table);
528
 
    }
529
 
 
530
 
    table->setVariableWidth();
531
523
    field= new Field_varstring(convert_blob_length, maybe_null,
532
 
                               name, collation.collation);
 
524
                               name, table->s, collation.collation);
533
525
    break;
534
 
 
535
526
  case DECIMAL_RESULT:
536
527
    field= new Field_decimal(max_length, maybe_null, name,
537
 
                             decimals, unsigned_flag);
 
528
                                 decimals, unsigned_flag);
538
529
    break;
539
 
 
540
530
  case ROW_RESULT:
 
531
  default:
541
532
    // This case should never be choosen
542
533
    assert(0);
543
534
    return 0;
544
535
  }
545
 
 
546
536
  if (field)
547
537
    field->init(table);
548
 
 
549
538
  return field;
550
539
}
551
540
 
655
644
    */
656
645
    break;
657
646
  case ROW_RESULT:
 
647
  default:
658
648
    assert(0);
659
649
  }
660
650
  collation.set(item->collation);
693
683
    max_length= item->max_length;
694
684
    break;
695
685
  case ROW_RESULT:
 
686
  default:
696
687
    assert(0);
697
688
  };
698
689
  /* MIN/MAX can return NULL for empty set indepedent of the used column */
804
795
    break;
805
796
  case INT_RESULT:
806
797
  case DECIMAL_RESULT:
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= my_decimal_precision_to_length(precision, decimals,
811
 
                                                 unsigned_flag);
812
 
      curr_dec_buff= 0;
813
 
      hybrid_type= DECIMAL_RESULT;
814
 
      my_decimal_set_zero(dec_buffs);
815
 
      break;
816
 
    }
 
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
  }
817
808
  case ROW_RESULT:
 
809
  default:
818
810
    assert(0);
819
811
  }
 
812
  return;
820
813
}
821
814
 
822
815
 
959
952
{
960
953
  assert(args[0]->fixed);
961
954
 
962
 
  null_value= maybe_null= true;
963
955
  table_field_type= args[0]->field_type();
964
956
 
965
957
  /* Adjust tmp table type according to the chosen aggregation type */
970
962
    table_field_type= DRIZZLE_TYPE_DOUBLE;
971
963
    break;
972
964
  case INT_RESULT:
973
 
    /*
974
 
      Preserving int8, int16, int32 field types gives ~10% performance boost
975
 
      as the size of result tree becomes significantly smaller.
976
 
      Another speed up we gain by using int64_t for intermediate
977
 
      calculations. The range of int64 is enough to hold sum 2^32 distinct
978
 
      integers each <= 2^32.
979
 
    */
980
 
    if (table_field_type == DRIZZLE_TYPE_LONG)
981
 
    {
982
 
      val.traits= Hybrid_type_traits_fast_decimal::instance();
983
 
      break;
984
 
    }
985
 
    table_field_type= DRIZZLE_TYPE_LONGLONG;
986
 
    /* 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 */
987
979
  case DECIMAL_RESULT:
988
980
    val.traits= Hybrid_type_traits_decimal::instance();
989
981
    if (table_field_type != DRIZZLE_TYPE_LONGLONG)
990
982
      table_field_type= DRIZZLE_TYPE_DECIMAL;
991
983
    break;
992
984
  case ROW_RESULT:
 
985
  default:
993
986
    assert(0);
994
987
  }
995
 
 
996
988
  val.traits->fix_length_and_dec(this, args[0]);
997
989
}
998
990
 
1031
1023
  field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
1032
1024
                               args[0]->decimals, args[0]->maybe_null);
1033
1025
 
1034
 
  if (! (table= session->getInstanceTable(field_list)))
 
1026
  if (! (table= create_virtual_tmp_table(session, field_list)))
1035
1027
    return(true);
1036
1028
 
1037
1029
  /* XXX: check that the case of CHAR(0) works OK */
1038
 
  tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
 
1030
  tree_key_length= table->s->reclength - table->s->null_bytes;
1039
1031
 
1040
1032
  /*
1041
1033
    Unique handles all unique elements in a tree until they can't fit
1054
1046
 
1055
1047
bool Item_sum_distinct::add()
1056
1048
{
1057
 
  args[0]->save_in_field(table->getField(0), false);
 
1049
  args[0]->save_in_field(table->field[0], false);
1058
1050
  is_evaluated= false;
1059
 
  if (!table->getField(0)->is_null())
 
1051
  if (!table->field[0]->is_null())
1060
1052
  {
1061
1053
    assert(tree);
1062
1054
    null_value= 0;
1064
1056
      '0' values are also stored in the tree. This doesn't matter
1065
1057
      for SUM(DISTINCT), but is important for AVG(DISTINCT)
1066
1058
    */
1067
 
    return tree->unique_add(table->getField(0)->ptr);
 
1059
    return tree->unique_add(table->field[0]->ptr);
1068
1060
  }
1069
1061
  return 0;
1070
1062
}
1072
1064
 
1073
1065
bool Item_sum_distinct::unique_walk_function(void *element)
1074
1066
{
1075
 
  memcpy(table->getField(0)->ptr, element, tree_key_length);
 
1067
  memcpy(table->field[0]->ptr, element, tree_key_length);
1076
1068
  ++count;
1077
 
  val.traits->add(&val, table->getField(0));
 
1069
  val.traits->add(&val, table->field[0]);
1078
1070
  return 0;
1079
1071
}
1080
1072
 
1116
1108
     */
1117
1109
    if (tree)
1118
1110
    {
1119
 
      table->getField(0)->set_notnull();
 
1111
      table->field[0]->set_notnull();
1120
1112
      tree->walk(item_sum_distinct_walk, (void*) this);
1121
1113
    }
1122
1114
    is_evaluated= true;
1261
1253
      The easiest way is to do this is to store both value in a string
1262
1254
      and unpack on access.
1263
1255
    */
1264
 
    table->setVariableWidth();
1265
1256
    field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
1266
1257
                                dec_bin_size : sizeof(double)) + sizeof(int64_t),
1267
 
                               0, name, &my_charset_bin);
 
1258
                               0, name, table->s, &my_charset_bin);
1268
1259
  }
1269
1260
  else if (hybrid_type == DECIMAL_RESULT)
1270
1261
    field= new Field_decimal(max_length, maybe_null, name,
1440
1431
    break;
1441
1432
  case INT_RESULT:
1442
1433
  case DECIMAL_RESULT:
1443
 
    {
1444
 
      int precision= args[0]->decimal_precision()*2 + prec_increment;
1445
 
      decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1446
 
      max_length= my_decimal_precision_to_length(precision, decimals,
1447
 
                                                 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);
1448
1439
 
1449
 
      break;
1450
 
    }
 
1440
    break;
 
1441
  }
1451
1442
  case ROW_RESULT:
 
1443
  default:
1452
1444
    assert(0);
1453
1445
  }
 
1446
  return;
1454
1447
}
1455
1448
 
1456
1449
 
1476
1469
      The easiest way is to do this is to store both value in a string
1477
1470
      and unpack on access.
1478
1471
    */
1479
 
    table->setVariableWidth();
1480
 
    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);
1481
1473
  }
1482
1474
  else
1483
1475
    field= new Field_double(max_length, maybe_null, name, decimals, true);
1619
1611
  assert(fixed == 1);
1620
1612
  if (null_value)
1621
1613
    return 0.0;
1622
 
 
1623
1614
  switch (hybrid_type) {
1624
1615
  case STRING_RESULT:
1625
 
    {
1626
 
      char *end_not_used;
1627
 
      int err_not_used;
1628
 
      String *res;  res=val_str(&str_value);
1629
 
      return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1630
 
                               &end_not_used, &err_not_used) : 0.0);
1631
 
    }
 
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
  }
1632
1623
  case INT_RESULT:
1633
1624
    return (double) sum_int;
1634
1625
  case DECIMAL_RESULT:
1637
1628
  case REAL_RESULT:
1638
1629
    return sum;
1639
1630
  case ROW_RESULT:
 
1631
  default:
1640
1632
    // This case should never be choosen
1641
 
    break;
 
1633
    assert(0);
 
1634
    return 0;
1642
1635
  }
1643
 
 
1644
 
  assert(0);
1645
 
  return 0;
1646
1636
}
1647
1637
 
1648
1638
int64_t Item_sum_hybrid::val_int()
1670
1660
  assert(fixed == 1);
1671
1661
  if (null_value)
1672
1662
    return 0;
1673
 
 
1674
1663
  switch (hybrid_type) {
1675
1664
  case STRING_RESULT:
1676
1665
    string2my_decimal(E_DEC_FATAL_ERROR, &value, val);
1685
1674
    int2my_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1686
1675
    break;
1687
1676
  case ROW_RESULT:
 
1677
  default:
1688
1678
    // This case should never be choosen
1689
1679
    assert(0);
1690
1680
    break;
1691
1681
  }
1692
 
 
1693
1682
  return val;                                   // Keep compiler happy
1694
1683
}
1695
1684
 
1700
1689
  assert(fixed == 1);
1701
1690
  if (null_value)
1702
1691
    return 0;
1703
 
 
1704
1692
  switch (hybrid_type) {
1705
1693
  case STRING_RESULT:
1706
1694
    return &value;
1716
1704
  case ROW_RESULT:
1717
1705
  default:
1718
1706
    // This case should never be choosen
 
1707
    assert(0);
1719
1708
    break;
1720
1709
  }
1721
 
 
1722
1710
  return str;                                   // Keep compiler happy
1723
1711
}
1724
1712
 
1756
1744
{
1757
1745
  switch (hybrid_type) {
1758
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))
1759
1751
    {
1760
 
      String *result=args[0]->val_str(&tmp_value);
1761
 
      if (!args[0]->null_value &&
1762
 
          (null_value || sortcmp(&value,result,collation.collation) > 0))
1763
 
      {
1764
 
        value.copy(*result);
1765
 
        null_value=0;
1766
 
      }
 
1752
      value.copy(*result);
 
1753
      null_value=0;
1767
1754
    }
1768
 
    break;
 
1755
  }
 
1756
  break;
1769
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)))
1770
1764
    {
1771
 
      int64_t nr=args[0]->val_int();
1772
 
      if (!args[0]->null_value && (null_value ||
1773
 
                                   (unsigned_flag &&
1774
 
                                    (uint64_t) nr < (uint64_t) sum_int) ||
1775
 
                                   (!unsigned_flag && nr < sum_int)))
1776
 
      {
1777
 
        sum_int=nr;
1778
 
        null_value=0;
1779
 
      }
 
1765
      sum_int=nr;
 
1766
      null_value=0;
1780
1767
    }
1781
 
    break;
 
1768
  }
 
1769
  break;
1782
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)))
1783
1775
    {
1784
 
      my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1785
 
      if (!args[0]->null_value &&
1786
 
          (null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
1787
 
      {
1788
 
        my_decimal2decimal(val, &sum_dec);
1789
 
        null_value= 0;
1790
 
      }
 
1776
      my_decimal2decimal(val, &sum_dec);
 
1777
      null_value= 0;
1791
1778
    }
1792
 
    break;
 
1779
  }
 
1780
  break;
1793
1781
  case REAL_RESULT:
 
1782
  {
 
1783
    double nr= args[0]->val_real();
 
1784
    if (!args[0]->null_value && (null_value || nr < sum))
1794
1785
    {
1795
 
      double nr= args[0]->val_real();
1796
 
      if (!args[0]->null_value && (null_value || nr < sum))
1797
 
      {
1798
 
        sum=nr;
1799
 
        null_value=0;
1800
 
      }
 
1786
      sum=nr;
 
1787
      null_value=0;
1801
1788
    }
1802
 
    break;
 
1789
  }
 
1790
  break;
1803
1791
  case ROW_RESULT:
 
1792
  default:
1804
1793
    // This case should never be choosen
1805
1794
    assert(0);
1806
1795
    break;
1819
1808
{
1820
1809
  switch (hybrid_type) {
1821
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))
1822
1815
    {
1823
 
      String *result=args[0]->val_str(&tmp_value);
1824
 
      if (!args[0]->null_value &&
1825
 
          (null_value || sortcmp(&value,result,collation.collation) < 0))
1826
 
      {
1827
 
        value.copy(*result);
1828
 
        null_value=0;
1829
 
      }
 
1816
      value.copy(*result);
 
1817
      null_value=0;
1830
1818
    }
1831
 
    break;
 
1819
  }
 
1820
  break;
1832
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)))
1833
1828
    {
1834
 
      int64_t nr=args[0]->val_int();
1835
 
      if (!args[0]->null_value && (null_value ||
1836
 
                                   (unsigned_flag &&
1837
 
                                    (uint64_t) nr > (uint64_t) sum_int) ||
1838
 
                                   (!unsigned_flag && nr > sum_int)))
1839
 
      {
1840
 
        sum_int=nr;
1841
 
        null_value=0;
1842
 
      }
 
1829
      sum_int=nr;
 
1830
      null_value=0;
1843
1831
    }
1844
 
    break;
 
1832
  }
 
1833
  break;
1845
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)))
1846
1839
    {
1847
 
      my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1848
 
      if (!args[0]->null_value &&
1849
 
          (null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
1850
 
      {
1851
 
        my_decimal2decimal(val, &sum_dec);
1852
 
        null_value= 0;
1853
 
      }
 
1840
      my_decimal2decimal(val, &sum_dec);
 
1841
      null_value= 0;
1854
1842
    }
1855
 
    break;
 
1843
  }
 
1844
  break;
1856
1845
  case REAL_RESULT:
 
1846
  {
 
1847
    double nr= args[0]->val_real();
 
1848
    if (!args[0]->null_value && (null_value || nr > sum))
1857
1849
    {
1858
 
      double nr= args[0]->val_real();
1859
 
      if (!args[0]->null_value && (null_value || nr > sum))
1860
 
      {
1861
 
        sum=nr;
1862
 
        null_value=0;
1863
 
      }
 
1850
      sum=nr;
 
1851
      null_value=0;
1864
1852
    }
1865
 
    break;
 
1853
  }
 
1854
  break;
1866
1855
  case ROW_RESULT:
 
1856
  default:
1867
1857
    // This case should never be choosen
1868
1858
    assert(0);
1869
1859
    break;
1870
1860
  }
1871
 
 
1872
1861
  return 0;
1873
1862
}
1874
1863
 
1956
1945
{
1957
1946
  switch(hybrid_type) {
1958
1947
  case STRING_RESULT:
1959
 
    {
1960
 
      char buff[MAX_FIELD_WIDTH];
1961
 
      String tmp(buff,sizeof(buff),result_field->charset()),*res;
1962
 
 
1963
 
      res=args[0]->val_str(&tmp);
1964
 
      if (args[0]->null_value)
1965
 
      {
 
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)
1966
2006
        result_field->set_null();
1967
 
        result_field->reset();
1968
 
      }
1969
2007
      else
1970
 
      {
1971
2008
        result_field->set_notnull();
1972
 
        result_field->store(res->ptr(),res->length(),tmp.charset());
1973
 
      }
1974
 
      break;
1975
 
    }
1976
 
  case INT_RESULT:
1977
 
    {
1978
 
      int64_t nr=args[0]->val_int();
1979
 
 
1980
 
      if (maybe_null)
1981
 
      {
1982
 
        if (args[0]->null_value)
1983
 
        {
1984
 
          nr=0;
1985
 
          result_field->set_null();
1986
 
        }
1987
 
        else
1988
 
          result_field->set_notnull();
1989
 
      }
1990
 
      result_field->store(nr, unsigned_flag);
1991
 
      break;
1992
 
    }
1993
 
  case REAL_RESULT:
1994
 
    {
1995
 
      double nr= args[0]->val_real();
1996
 
 
1997
 
      if (maybe_null)
1998
 
      {
1999
 
        if (args[0]->null_value)
2000
 
        {
2001
 
          nr=0.0;
2002
 
          result_field->set_null();
2003
 
        }
2004
 
        else
2005
 
          result_field->set_notnull();
2006
 
      }
2007
 
      result_field->store(nr);
2008
 
      break;
2009
 
    }
2010
 
  case DECIMAL_RESULT:
2011
 
    {
2012
 
      my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2013
 
 
2014
 
      if (maybe_null)
2015
 
      {
2016
 
        if (args[0]->null_value)
2017
 
          result_field->set_null();
2018
 
        else
2019
 
          result_field->set_notnull();
2020
 
      }
2021
 
      /*
2022
 
        We must store zero in the field as we will use the field value in
2023
 
        add()
2024
 
      */
2025
 
      if (!arg_dec)                               // Null
2026
 
        arg_dec= &decimal_zero;
2027
 
      result_field->store_decimal(arg_dec);
2028
 
      break;
2029
 
    }
 
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
  }
2030
2019
  case ROW_RESULT:
 
2020
  default:
2031
2021
    assert(0);
2032
2022
  }
2033
2023
}
2222
2212
  case DECIMAL_RESULT:
2223
2213
    min_max_update_decimal_field();
2224
2214
    break;
2225
 
  case REAL_RESULT:
2226
 
  case ROW_RESULT:
 
2215
  default:
2227
2216
    min_max_update_real_field();
2228
2217
  }
2229
2218
}
2236
2225
 
2237
2226
  if (!args[0]->null_value)
2238
2227
  {
2239
 
    result_field->val_str_internal(&tmp_value);
 
2228
    result_field->val_str(&tmp_value);
2240
2229
 
2241
2230
    if (result_field->is_null() ||
2242
2231
        (cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2503
2492
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2504
2493
{
2505
2494
  Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2506
 
  Field **field    = item->table->getFields();
2507
 
  Field **field_end= field + item->table->getShare()->sizeFields();
 
2495
  Field **field    = item->table->field;
 
2496
  Field **field_end= field + item->table->s->fields;
2508
2497
  uint32_t *lengths=item->field_lengths;
2509
2498
  for (; field < field_end; ++field)
2510
2499
  {
2544
2533
    is_evaluated= false;
2545
2534
    if (table)
2546
2535
    {
 
2536
      table->free_tmp_table(table->in_use);
2547
2537
      table= 0;
2548
2538
    }
2549
2539
    delete tmp_table_param;
2608
2598
  tmp_table_param->force_copy_fields= force_copy_fields;
2609
2599
  assert(table == 0);
2610
2600
 
2611
 
  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,
2612
2602
                                0,
2613
2603
                                (select_lex->options | session->options),
2614
2604
                                HA_POS_ERROR, (char*)"")))
2615
 
  {
2616
2605
    return true;
2617
 
  }
2618
2606
  table->cursor->extra(HA_EXTRA_NO_ROWS);               // Don't update rows
2619
2607
  table->no_rows=1;
2620
2608
 
2621
 
  if (table->getShare()->db_type() == heap_engine)
 
2609
  if (table->s->db_type() == heap_engine)
2622
2610
  {
2623
2611
    /*
2624
2612
      No blobs, otherwise it would have been MyISAM: set up a compare
2626
2614
    */
2627
2615
    qsort_cmp2 compare_key;
2628
2616
    void* cmp_arg;
2629
 
    Field **field= table->getFields();
2630
 
    Field **field_end= field + table->getShare()->sizeFields();
 
2617
    Field **field= table->field;
 
2618
    Field **field_end= field + table->s->fields;
2631
2619
    bool all_binary= true;
2632
2620
 
2633
2621
    for (tree_key_length= 0; field < field_end; ++field)
2648
2636
    }
2649
2637
    else
2650
2638
    {
2651
 
      if (table->getShare()->sizeFields() == 1)
 
2639
      if (table->s->fields == 1)
2652
2640
      {
2653
2641
        /*
2654
2642
          If we have only one field, which is the most common use of
2657
2645
          about other fields.
2658
2646
        */
2659
2647
        compare_key= (qsort_cmp2) simple_str_key_cmp;
2660
 
        cmp_arg= (void*) table->getField(0);
 
2648
        cmp_arg= (void*) table->field[0];
2661
2649
        /* tree_key_length has been set already */
2662
2650
      }
2663
2651
      else
2665
2653
        uint32_t *length;
2666
2654
        compare_key= (qsort_cmp2) composite_key_cmp;
2667
2655
        cmp_arg= (void*) this;
2668
 
        field_lengths= (uint32_t*) session->alloc(table->getShare()->sizeFields() * sizeof(uint32_t));
2669
 
        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;
2670
2658
             field < field_end; ++field, ++length)
2671
2659
        {
2672
2660
          *length= (*field)->pack_length();
2719
2707
  if (always_null)
2720
2708
    return 0;
2721
2709
  copy_fields(tmp_table_param);
2722
 
  if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
2723
 
    return true;
 
2710
  copy_funcs(tmp_table_param->items_to_copy);
2724
2711
 
2725
 
  for (Field **field= table->getFields() ; *field ; field++)
2726
 
  {
 
2712
  for (Field **field=table->field ; *field ; field++)
2727
2713
    if ((*field)->is_real_null(0))
2728
 
    {
2729
2714
      return 0;                                 // Don't count NULL
2730
 
    }
2731
 
  }
2732
2715
 
2733
2716
  is_evaluated= false;
2734
2717
  if (tree)
2739
2722
      bloat the tree without providing any valuable info. Besides,
2740
2723
      key_length used to initialize the tree didn't include space for them.
2741
2724
    */
2742
 
    return tree->unique_add(table->record[0] + table->getShare()->null_bytes);
 
2725
    return tree->unique_add(table->record[0] + table->s->null_bytes);
2743
2726
  }
2744
 
  if ((error= table->cursor->insertRecord(table->record[0])) &&
 
2727
  if ((error= table->cursor->ha_write_row(table->record[0])) &&
2745
2728
      table->cursor->is_fatal_error(error, HA_CHECK_DUP))
2746
2729
    return true;
2747
2730
  return false;
2828
2811
    */
2829
2812
    Field *field= item->get_tmp_table_field();
2830
2813
    int res;
2831
 
    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;
2832
2815
    if((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2833
2816
      return res;
2834
2817
  }
2844
2827
                                    const void* key2)
2845
2828
{
2846
2829
  Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2847
 
  Order **order_item, **end;
 
2830
  order_st **order_item, **end;
2848
2831
  Table *table= grp_item->table;
2849
2832
 
2850
2833
  for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2865
2848
    if (field && !item->const_item())
2866
2849
    {
2867
2850
      int res;
2868
 
      uint32_t offset= (field->offset(field->getTable()->record[0]) -
2869
 
                    table->getShare()->null_bytes);
 
2851
      uint32_t offset= (field->offset(field->table->record[0]) -
 
2852
                    table->s->null_bytes);
2870
2853
      if ((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2871
2854
        return (*order_item)->asc ? res : -res;
2872
2855
    }
2888
2871
                  Item_func_group_concat *item)
2889
2872
{
2890
2873
  Table *table= item->table;
2891
 
  String tmp((char *)table->getUpdateRecord(), table->getShare()->getRecordLength(),
 
2874
  String tmp((char *)table->record[1], table->s->reclength,
2892
2875
             default_charset_info);
2893
2876
  String tmp2;
2894
2877
  String *result= &item->result;
2915
2898
        because it contains both order and arg list fields.
2916
2899
      */
2917
2900
      Field *field= (*arg)->get_tmp_table_field();
2918
 
      uint32_t offset= (field->offset(field->getTable()->record[0]) -
2919
 
                    table->getShare()->null_bytes);
2920
 
      assert(offset < table->getShare()->getRecordLength());
2921
 
      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);
2922
2905
    }
2923
2906
    else
2924
2907
      res= (*arg)->val_str(&tmp);
2988
2971
    order - arg_count_order
2989
2972
  */
2990
2973
  if (!(args= (Item**) memory::sql_alloc(sizeof(Item*) * arg_count +
2991
 
                                 sizeof(Order*)*arg_count_order)))
 
2974
                                 sizeof(order_st*)*arg_count_order)))
2992
2975
    return;
2993
2976
 
2994
 
  order= (Order**)(args + arg_count);
 
2977
  order= (order_st**)(args + arg_count);
2995
2978
 
2996
2979
  /* fill args items of show and sort */
2997
2980
  List_iterator_fast<Item> li(*select_list);
3001
2984
 
3002
2985
  if (arg_count_order)
3003
2986
  {
3004
 
    Order **order_ptr= order;
3005
 
    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;
3006
2989
         order_item != NULL;
3007
2990
         order_item= order_item->next)
3008
2991
    {
3048
3031
  if (warning)
3049
3032
  {
3050
3033
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
3051
 
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
 
3034
    sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3052
3035
    warning->set_msg(current_session, warn_buff);
3053
3036
    warning= 0;
3054
3037
  }
3064
3047
    if (table)
3065
3048
    {
3066
3049
      Session *session= table->in_use;
 
3050
      table->free_tmp_table(session);
3067
3051
      table= 0;
3068
3052
      if (tree)
3069
3053
      {
3078
3062
      if (warning)
3079
3063
      {
3080
3064
        char warn_buff[DRIZZLE_ERRMSG_SIZE];
3081
 
        snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
 
3065
        sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3082
3066
        warning->set_msg(session, warn_buff);
3083
3067
        warning= 0;
3084
3068
      }
3115
3099
  if (always_null)
3116
3100
    return 0;
3117
3101
  copy_fields(tmp_table_param);
3118
 
  if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
3119
 
    return true;
 
3102
  copy_funcs(tmp_table_param->items_to_copy);
3120
3103
 
3121
3104
  for (uint32_t i= 0; i < arg_count_field; i++)
3122
3105
  {
3136
3119
  {
3137
3120
    /* Filter out duplicate rows. */
3138
3121
    uint32_t count= unique_filter->elements_in_tree();
3139
 
    unique_filter->unique_add(table->record[0] + table->getShare()->null_bytes);
 
3122
    unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3140
3123
    if (count == unique_filter->elements_in_tree())
3141
3124
      row_eligible= false;
3142
3125
  }
3143
3126
 
3144
3127
  TREE_ELEMENT *el= 0;                          // Only for safety
3145
3128
  if (row_eligible && tree)
3146
 
    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,
3147
3130
                    tree->custom_arg);
3148
3131
  /*
3149
3132
    If the row is not a duplicate (el->count == 1)
3152
3135
  */
3153
3136
  if (row_eligible && !warning_for_row &&
3154
3137
      (!tree || (el->count == 1 && distinct && !arg_count_order)))
3155
 
    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);
3156
3139
 
3157
3140
  return 0;
3158
3141
}
3270
3253
    field list.
3271
3254
  */
3272
3255
  if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3273
 
                                (Order*) 0, 0, true,
 
3256
                                (order_st*) 0, 0, true,
3274
3257
                                (select_lex->options | session->options),
3275
3258
                                HA_POS_ERROR, (char*) "")))
3276
 
  {
3277
3259
    return(true);
3278
 
  }
3279
 
 
3280
3260
  table->cursor->extra(HA_EXTRA_NO_ROWS);
3281
3261
  table->no_rows= 1;
3282
3262
 
3285
3265
     Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3286
3266
     the row is not added to the result.
3287
3267
  */
3288
 
  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;
3289
3269
 
3290
3270
  if (arg_count_order)
3291
3271
  {