~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/sum.cc

  • Committer: Brian Aker
  • Date: 2010-05-18 15:49:50 UTC
  • Revision ID: brian@gaz-20100518154950-qhctxu6lvvnyxy9i
Simplify out Open_tables_state() backup/restore (no longer needed).

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