~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Brian Aker
  • Date: 2008-12-11 08:52:18 UTC
  • mfrom: (670.1.11 devel)
  • Revision ID: brian@tangent.org-20081211085218-cpmznmzrflyd82j2
Merge from Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
#include <drizzled/field/enum.h>
41
41
#include <drizzled/field/null.h>
42
42
#include <drizzled/field/date.h>
43
 
#include <drizzled/field/fdecimal.h>
 
43
#include <drizzled/field/decimal.h>
44
44
#include <drizzled/field/real.h>
45
45
#include <drizzled/field/double.h>
46
46
#include <drizzled/field/long.h>
965
965
  }
966
966
}
967
967
 
968
 
/****************************************************************************
969
 
  Item_copy_string
970
 
****************************************************************************/
971
 
 
972
 
void Item_copy_string::copy()
973
 
{
974
 
  String *res=item->val_str(&str_value);
975
 
  if (res && res != &str_value)
976
 
    str_value.copy(*res);
977
 
  null_value=item->null_value;
978
 
}
979
 
 
980
 
/* ARGSUSED */
981
 
String *Item_copy_string::val_str(String *)
982
 
{
983
 
  // Item_copy_string is used without fix_fields call
984
 
  if (null_value)
985
 
    return (String*) 0;
986
 
  return &str_value;
987
 
}
988
 
 
989
 
 
990
 
my_decimal *Item_copy_string::val_decimal(my_decimal *decimal_value)
991
 
{
992
 
  // Item_copy_string is used without fix_fields call
993
 
  if (null_value)
994
 
    return 0;
995
 
  string2my_decimal(E_DEC_FATAL_ERROR, &str_value, decimal_value);
996
 
  return (decimal_value);
997
 
}
998
 
 
999
 
 
1000
968
/*
1001
969
  Functions to convert item to field (for send_fields)
1002
970
*/
1011
979
  return false;
1012
980
}
1013
981
 
1014
 
double Item_ref_null_helper::val_real()
1015
 
{
1016
 
  assert(fixed == 1);
1017
 
  double tmp= (*ref)->val_result();
1018
 
  owner->was_null|= null_value= (*ref)->null_value;
1019
 
  return tmp;
1020
 
}
1021
 
 
1022
 
 
1023
 
int64_t Item_ref_null_helper::val_int()
1024
 
{
1025
 
  assert(fixed == 1);
1026
 
  int64_t tmp= (*ref)->val_int_result();
1027
 
  owner->was_null|= null_value= (*ref)->null_value;
1028
 
  return tmp;
1029
 
}
1030
 
 
1031
 
 
1032
 
my_decimal *Item_ref_null_helper::val_decimal(my_decimal *decimal_value)
1033
 
{
1034
 
  assert(fixed == 1);
1035
 
  my_decimal *val= (*ref)->val_decimal_result(decimal_value);
1036
 
  owner->was_null|= null_value= (*ref)->null_value;
1037
 
  return val;
1038
 
}
1039
 
 
1040
 
 
1041
 
bool Item_ref_null_helper::val_bool()
1042
 
{
1043
 
  assert(fixed == 1);
1044
 
  bool val= (*ref)->val_bool_result();
1045
 
  owner->was_null|= null_value= (*ref)->null_value;
1046
 
  return val;
1047
 
}
1048
 
 
1049
 
 
1050
 
String* Item_ref_null_helper::val_str(String* s)
1051
 
{
1052
 
  assert(fixed == 1);
1053
 
  String* tmp= (*ref)->str_result(s);
1054
 
  owner->was_null|= null_value= (*ref)->null_value;
1055
 
  return tmp;
1056
 
}
1057
 
 
1058
 
 
1059
 
bool Item_ref_null_helper::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
1060
 
{
1061
 
  return (owner->was_null|= null_value= (*ref)->get_date(ltime, fuzzydate));
1062
 
}
1063
 
 
1064
982
 
1065
983
/**
1066
984
  Mark item and SELECT_LEXs as dependent if item was resolved in
1399
1317
  return f_type;
1400
1318
}
1401
1319
 
1402
 
 
1403
 
void Item_empty_string::make_field(Send_field *tmp_field)
1404
 
{
1405
 
  init_make_field(tmp_field, string_field_type());
1406
 
}
1407
 
 
1408
 
 
1409
1320
enum_field_types Item::field_type() const
1410
1321
{
1411
1322
  switch (result_type()) {
1681
1592
}
1682
1593
 
1683
1594
 
1684
 
Item *Item_int_with_ref::clone_item()
1685
 
{
1686
 
  assert(ref->const_item());
1687
 
  /*
1688
 
    We need to evaluate the constant to make sure it works with
1689
 
    parameter markers.
1690
 
  */
1691
 
  return (ref->unsigned_flag ?
1692
 
          new Item_uint(ref->name, ref->val_int(), ref->max_length) :
1693
 
          new Item_int(ref->name, ref->val_int(), ref->max_length));
1694
 
}
1695
 
 
1696
 
 
1697
 
inline uint32_t char_val(char X)
1698
 
{
1699
 
  return (uint) (X >= '0' && X <= '9' ? X-'0' :
1700
 
                 X >= 'A' && X <= 'Z' ? X-'A'+10 :
1701
 
                 X-'a'+10);
1702
 
}
1703
 
 
1704
 
 
1705
 
Item_hex_string::Item_hex_string(const char *str, uint32_t str_length)
1706
 
{
1707
 
  max_length=(str_length+1)/2;
1708
 
  char *ptr=(char*) sql_alloc(max_length+1);
1709
 
  if (!ptr)
1710
 
    return;
1711
 
  str_value.set(ptr,max_length,&my_charset_bin);
1712
 
  char *end=ptr+max_length;
1713
 
  if (max_length*2 != str_length)
1714
 
    *ptr++=char_val(*str++);                    // Not even, assume 0 prefix
1715
 
  while (ptr != end)
1716
 
  {
1717
 
    *ptr++= (char) (char_val(str[0])*16+char_val(str[1]));
1718
 
    str+=2;
1719
 
  }
1720
 
  *ptr=0;                                       // Keep purify happy
1721
 
  collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
1722
 
  fixed= 1;
1723
 
  unsigned_flag= 1;
1724
 
}
1725
 
 
1726
 
int64_t Item_hex_string::val_int()
1727
 
{
1728
 
  // following assert is redundant, because fixed=1 assigned in constructor
1729
 
  assert(fixed == 1);
1730
 
  char *end=(char*) str_value.ptr()+str_value.length(),
1731
 
       *ptr=end-cmin(str_value.length(),(uint32_t)sizeof(int64_t));
1732
 
 
1733
 
  uint64_t value=0;
1734
 
  for (; ptr != end ; ptr++)
1735
 
    value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
1736
 
  return (int64_t) value;
1737
 
}
1738
 
 
1739
 
 
1740
 
my_decimal *Item_hex_string::val_decimal(my_decimal *decimal_value)
1741
 
{
1742
 
  // following assert is redundant, because fixed=1 assigned in constructor
1743
 
  assert(fixed == 1);
1744
 
  uint64_t value= (uint64_t)val_int();
1745
 
  int2my_decimal(E_DEC_FATAL_ERROR, value, true, decimal_value);
1746
 
  return (decimal_value);
1747
 
}
1748
 
 
1749
 
 
1750
 
int Item_hex_string::save_in_field(Field *field, bool)
1751
 
{
1752
 
  field->set_notnull();
1753
 
  if (field->result_type() == STRING_RESULT)
1754
 
    return field->store(str_value.ptr(), str_value.length(),
1755
 
                        collation.collation);
1756
 
 
1757
 
  uint64_t nr;
1758
 
  uint32_t length= str_value.length();
1759
 
  if (length > 8)
1760
 
  {
1761
 
    nr= field->flags & UNSIGNED_FLAG ? UINT64_MAX : INT64_MAX;
1762
 
    goto warn;
1763
 
  }
1764
 
  nr= (uint64_t) val_int();
1765
 
  if ((length == 8) && !(field->flags & UNSIGNED_FLAG) && (nr > INT64_MAX))
1766
 
  {
1767
 
    nr= INT64_MAX;
1768
 
    goto warn;
1769
 
  }
1770
 
  return field->store((int64_t) nr, true);  // Assume hex numbers are unsigned
1771
 
 
1772
 
warn:
1773
 
  if (!field->store((int64_t) nr, true))
1774
 
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE,
1775
 
                       1);
1776
 
  return 1;
1777
 
}
1778
 
 
1779
 
 
1780
 
void Item_hex_string::print(String *str, enum_query_type)
1781
 
{
1782
 
  char *end= (char*) str_value.ptr() + str_value.length(),
1783
 
       *ptr= end - cmin(str_value.length(), (uint32_t)sizeof(int64_t));
1784
 
  str->append("0x");
1785
 
  for (; ptr != end ; ptr++)
1786
 
  {
1787
 
    str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
1788
 
    str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
1789
 
  }
1790
 
}
1791
 
 
1792
 
 
1793
 
bool Item_hex_string::eq(const Item *arg, bool binary_cmp) const
1794
 
{
1795
 
  if (arg->basic_const_item() && arg->type() == type())
1796
 
  {
1797
 
    if (binary_cmp)
1798
 
      return !stringcmp(&str_value, &arg->str_value);
1799
 
    return !sortcmp(&str_value, &arg->str_value, collation.collation);
1800
 
  }
1801
 
  return false;
1802
 
}
1803
 
 
1804
 
 
1805
 
Item *Item_hex_string::safe_charset_converter(const CHARSET_INFO * const tocs)
1806
 
{
1807
 
  Item_string *conv;
1808
 
  String tmp, *str= val_str(&tmp);
1809
 
 
1810
 
  if (!(conv= new Item_string(str->ptr(), str->length(), tocs)))
1811
 
    return NULL;
1812
 
  conv->str_value.copy();
1813
 
  conv->str_value.mark_as_const();
1814
 
  return conv;
1815
 
}
1816
 
 
1817
 
 
1818
 
/*
1819
 
  bin item.
1820
 
  In string context this is a binary string.
1821
 
  In number context this is a int64_t value.
1822
 
*/
1823
 
 
1824
 
Item_bin_string::Item_bin_string(const char *str, uint32_t str_length)
1825
 
{
1826
 
  const char *end= str + str_length - 1;
1827
 
  unsigned char bits= 0;
1828
 
  uint32_t power= 1;
1829
 
 
1830
 
  max_length= (str_length + 7) >> 3;
1831
 
  char *ptr= (char*) sql_alloc(max_length + 1);
1832
 
  if (!ptr)
1833
 
    return;
1834
 
  str_value.set(ptr, max_length, &my_charset_bin);
1835
 
  ptr+= max_length - 1;
1836
 
  ptr[1]= 0;                     // Set end null for string
1837
 
  for (; end >= str; end--)
1838
 
  {
1839
 
    if (power == 256)
1840
 
    {
1841
 
      power= 1;
1842
 
      *ptr--= bits;
1843
 
      bits= 0;
1844
 
    }
1845
 
    if (*end == '1')
1846
 
      bits|= power;
1847
 
    power<<= 1;
1848
 
  }
1849
 
  *ptr= (char) bits;
1850
 
  collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
1851
 
  fixed= 1;
1852
 
}
1853
 
 
1854
 
 
1855
1595
/**
1856
1596
  This is only called from items that is not of type item_field.
1857
1597
*/
1926
1666
}
1927
1667
 
1928
1668
 
1929
 
Item_ref::Item_ref(Name_resolution_context *context_arg,
1930
 
                   Item **item, const char *table_name_arg,
1931
 
                   const char *field_name_arg,
1932
 
                   bool alias_name_used_arg)
1933
 
  :Item_ident(context_arg, NULL, table_name_arg, field_name_arg),
1934
 
   result_field(0), ref(item)
1935
 
{
1936
 
  alias_name_used= alias_name_used_arg;
1937
 
  /*
1938
 
    This constructor used to create some internals references over fixed items
1939
 
  */
1940
 
  if (ref && *ref && (*ref)->fixed)
1941
 
    set_properties();
1942
 
}
1943
 
 
1944
 
 
1945
 
/**
1946
 
  Resolve the name of a reference to a column reference.
1947
 
 
1948
 
  The method resolves the column reference represented by 'this' as a column
1949
 
  present in one of: GROUP BY clause, SELECT clause, outer queries. It is
1950
 
  used typically for columns in the HAVING clause which are not under
1951
 
  aggregate functions.
1952
 
 
1953
 
  POSTCONDITION @n
1954
 
  Item_ref::ref is 0 or points to a valid item.
1955
 
 
1956
 
  @note
1957
 
    The name resolution algorithm used is (where [T_j] is an optional table
1958
 
    name that qualifies the column name):
1959
 
 
1960
 
  @code
1961
 
        resolve_extended([T_j].col_ref_i)
1962
 
        {
1963
 
          Search for a column or derived column named col_ref_i [in table T_j]
1964
 
          in the SELECT and GROUP clauses of Q.
1965
 
 
1966
 
          if such a column is NOT found AND    // Lookup in outer queries.
1967
 
             there are outer queries
1968
 
          {
1969
 
            for each outer query Q_k beginning from the inner-most one
1970
 
           {
1971
 
              Search for a column or derived column named col_ref_i
1972
 
              [in table T_j] in the SELECT and GROUP clauses of Q_k.
1973
 
 
1974
 
              if such a column is not found AND
1975
 
                 - Q_k is not a group query AND
1976
 
                 - Q_k is not inside an aggregate function
1977
 
                 OR
1978
 
                 - Q_(k-1) is not in a HAVING or SELECT clause of Q_k
1979
 
              {
1980
 
                search for a column or derived column named col_ref_i
1981
 
                [in table T_j] in the FROM clause of Q_k;
1982
 
              }
1983
 
            }
1984
 
          }
1985
 
        }
1986
 
  @endcode
1987
 
  @n
1988
 
    This procedure treats GROUP BY and SELECT clauses as one namespace for
1989
 
    column references in HAVING. Notice that compared to
1990
 
    Item_field::fix_fields, here we first search the SELECT and GROUP BY
1991
 
    clauses, and then we search the FROM clause.
1992
 
 
1993
 
  @param[in]     session        current thread
1994
 
  @param[in,out] reference  view column if this item was resolved to a
1995
 
    view column
1996
 
 
1997
 
  @todo
1998
 
    Here we could first find the field anyway, and then test this
1999
 
    condition, so that we can give a better error message -
2000
 
    ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
2001
 
    ER_BAD_FIELD_ERROR which we produce now.
2002
 
 
2003
 
  @retval
2004
 
    true  if error
2005
 
  @retval
2006
 
    false on success
2007
 
*/
2008
 
 
2009
 
bool Item_ref::fix_fields(Session *session, Item **reference)
2010
 
{
2011
 
  enum_parsing_place place= NO_MATTER;
2012
 
  assert(fixed == 0);
2013
 
  SELECT_LEX *current_sel= session->lex->current_select;
2014
 
 
2015
 
  if (!ref || ref == not_found_item)
2016
 
  {
2017
 
    if (!(ref= resolve_ref_in_select_and_group(session, this,
2018
 
                                               context->select_lex)))
2019
 
      goto error;             /* Some error occurred (e.g. ambiguous names). */
2020
 
 
2021
 
    if (ref == not_found_item) /* This reference was not resolved. */
2022
 
    {
2023
 
      Name_resolution_context *last_checked_context= context;
2024
 
      Name_resolution_context *outer_context= context->outer_context;
2025
 
      Field *from_field;
2026
 
      ref= 0;
2027
 
 
2028
 
      if (!outer_context)
2029
 
      {
2030
 
        /* The current reference cannot be resolved in this query. */
2031
 
        my_error(ER_BAD_FIELD_ERROR,MYF(0),
2032
 
                 this->full_name(), current_session->where);
2033
 
        goto error;
2034
 
      }
2035
 
 
2036
 
      /*
2037
 
        If there is an outer context (select), and it is not a derived table
2038
 
        (which do not support the use of outer fields for now), try to
2039
 
        resolve this reference in the outer select(s).
2040
 
 
2041
 
        We treat each subselect as a separate namespace, so that different
2042
 
        subselects may contain columns with the same names. The subselects are
2043
 
        searched starting from the innermost.
2044
 
      */
2045
 
      from_field= (Field*) not_found_field;
2046
 
 
2047
 
      do
2048
 
      {
2049
 
        SELECT_LEX *select= outer_context->select_lex;
2050
 
        Item_subselect *prev_subselect_item=
2051
 
          last_checked_context->select_lex->master_unit()->item;
2052
 
        last_checked_context= outer_context;
2053
 
 
2054
 
        /* Search in the SELECT and GROUP lists of the outer select. */
2055
 
        if (outer_context->resolve_in_select_list)
2056
 
        {
2057
 
          if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
2058
 
            goto error; /* Some error occurred (e.g. ambiguous names). */
2059
 
          if (ref != not_found_item)
2060
 
          {
2061
 
            assert(*ref && (*ref)->fixed);
2062
 
            prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
2063
 
            prev_subselect_item->const_item_cache&= (*ref)->const_item();
2064
 
            break;
2065
 
          }
2066
 
          /*
2067
 
            Set ref to 0 to ensure that we get an error in case we replaced
2068
 
            this item with another item and still use this item in some
2069
 
            other place of the parse tree.
2070
 
          */
2071
 
          ref= 0;
2072
 
        }
2073
 
 
2074
 
        place= prev_subselect_item->parsing_place;
2075
 
        /*
2076
 
          Check table fields only if the subquery is used somewhere out of
2077
 
          HAVING or the outer SELECT does not use grouping (i.e. tables are
2078
 
          accessible).
2079
 
          TODO:
2080
 
          Here we could first find the field anyway, and then test this
2081
 
          condition, so that we can give a better error message -
2082
 
          ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
2083
 
          ER_BAD_FIELD_ERROR which we produce now.
2084
 
        */
2085
 
        if ((place != IN_HAVING ||
2086
 
             (!select->with_sum_func &&
2087
 
              select->group_list.elements == 0)))
2088
 
        {
2089
 
          /*
2090
 
            In case of view, find_field_in_tables() write pointer to view
2091
 
            field expression to 'reference', i.e. it substitute that
2092
 
            expression instead of this Item_ref
2093
 
          */
2094
 
          from_field= find_field_in_tables(session, this,
2095
 
                                           outer_context->
2096
 
                                             first_name_resolution_table,
2097
 
                                           outer_context->
2098
 
                                             last_name_resolution_table,
2099
 
                                           reference,
2100
 
                                           IGNORE_EXCEPT_NON_UNIQUE,
2101
 
                                           true, true);
2102
 
          if (! from_field)
2103
 
            goto error;
2104
 
          if (from_field == view_ref_found)
2105
 
          {
2106
 
            Item::Type refer_type= (*reference)->type();
2107
 
            prev_subselect_item->used_tables_cache|=
2108
 
              (*reference)->used_tables();
2109
 
            prev_subselect_item->const_item_cache&=
2110
 
              (*reference)->const_item();
2111
 
            assert((*reference)->type() == REF_ITEM);
2112
 
            mark_as_dependent(session, last_checked_context->select_lex,
2113
 
                              context->select_lex, this,
2114
 
                              ((refer_type == REF_ITEM ||
2115
 
                                refer_type == FIELD_ITEM) ?
2116
 
                               (Item_ident*) (*reference) :
2117
 
                               0));
2118
 
            /*
2119
 
              view reference found, we substituted it instead of this
2120
 
              Item, so can quit
2121
 
            */
2122
 
            return false;
2123
 
          }
2124
 
          if (from_field != not_found_field)
2125
 
          {
2126
 
            if (cached_table && cached_table->select_lex &&
2127
 
                outer_context->select_lex &&
2128
 
                cached_table->select_lex != outer_context->select_lex)
2129
 
            {
2130
 
              /*
2131
 
                Due to cache, find_field_in_tables() can return field which
2132
 
                doesn't belong to provided outer_context. In this case we have
2133
 
                to find proper field context in order to fix field correcly.
2134
 
              */
2135
 
              do
2136
 
              {
2137
 
                outer_context= outer_context->outer_context;
2138
 
                select= outer_context->select_lex;
2139
 
                prev_subselect_item=
2140
 
                  last_checked_context->select_lex->master_unit()->item;
2141
 
                last_checked_context= outer_context;
2142
 
              } while (outer_context && outer_context->select_lex &&
2143
 
                       cached_table->select_lex != outer_context->select_lex);
2144
 
            }
2145
 
            prev_subselect_item->used_tables_cache|= from_field->table->map;
2146
 
            prev_subselect_item->const_item_cache= 0;
2147
 
            break;
2148
 
          }
2149
 
        }
2150
 
        assert(from_field == not_found_field);
2151
 
 
2152
 
        /* Reference is not found => depend on outer (or just error). */
2153
 
        prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
2154
 
        prev_subselect_item->const_item_cache= 0;
2155
 
 
2156
 
        outer_context= outer_context->outer_context;
2157
 
      } while (outer_context);
2158
 
 
2159
 
      assert(from_field != 0 && from_field != view_ref_found);
2160
 
      if (from_field != not_found_field)
2161
 
      {
2162
 
        Item_field* fld;
2163
 
        if (!(fld= new Item_field(from_field)))
2164
 
          goto error;
2165
 
        session->change_item_tree(reference, fld);
2166
 
        mark_as_dependent(session, last_checked_context->select_lex,
2167
 
                          session->lex->current_select, this, fld);
2168
 
        /*
2169
 
          A reference is resolved to a nest level that's outer or the same as
2170
 
          the nest level of the enclosing set function : adjust the value of
2171
 
          max_arg_level for the function if it's needed.
2172
 
        */
2173
 
        if (session->lex->in_sum_func &&
2174
 
            session->lex->in_sum_func->nest_level >=
2175
 
            last_checked_context->select_lex->nest_level)
2176
 
          set_if_bigger(session->lex->in_sum_func->max_arg_level,
2177
 
                        last_checked_context->select_lex->nest_level);
2178
 
        return false;
2179
 
      }
2180
 
      if (ref == 0)
2181
 
      {
2182
 
        /* The item was not a table field and not a reference */
2183
 
        my_error(ER_BAD_FIELD_ERROR, MYF(0),
2184
 
                 this->full_name(), current_session->where);
2185
 
        goto error;
2186
 
      }
2187
 
      /* Should be checked in resolve_ref_in_select_and_group(). */
2188
 
      assert(*ref && (*ref)->fixed);
2189
 
      mark_as_dependent(session, last_checked_context->select_lex,
2190
 
                        context->select_lex, this, this);
2191
 
      /*
2192
 
        A reference is resolved to a nest level that's outer or the same as
2193
 
        the nest level of the enclosing set function : adjust the value of
2194
 
        max_arg_level for the function if it's needed.
2195
 
      */
2196
 
      if (session->lex->in_sum_func &&
2197
 
          session->lex->in_sum_func->nest_level >=
2198
 
          last_checked_context->select_lex->nest_level)
2199
 
        set_if_bigger(session->lex->in_sum_func->max_arg_level,
2200
 
                      last_checked_context->select_lex->nest_level);
2201
 
    }
2202
 
  }
2203
 
 
2204
 
  assert(*ref);
2205
 
  /*
2206
 
    Check if this is an incorrect reference in a group function or forward
2207
 
    reference. Do not issue an error if this is:
2208
 
      1. outer reference (will be fixed later by the fix_inner_refs function);
2209
 
      2. an unnamed reference inside an aggregate function.
2210
 
  */
2211
 
  if (!((*ref)->type() == REF_ITEM &&
2212
 
       ((Item_ref *)(*ref))->ref_type() == OUTER_REF) &&
2213
 
      (((*ref)->with_sum_func && name &&
2214
 
        !(current_sel->linkage != GLOBAL_OPTIONS_TYPE &&
2215
 
          current_sel->having_fix_field)) ||
2216
 
       !(*ref)->fixed))
2217
 
  {
2218
 
    my_error(ER_ILLEGAL_REFERENCE, MYF(0),
2219
 
             name, ((*ref)->with_sum_func?
2220
 
                    "reference to group function":
2221
 
                    "forward reference in item list"));
2222
 
    goto error;
2223
 
  }
2224
 
 
2225
 
  set_properties();
2226
 
 
2227
 
  if ((*ref)->check_cols(1))
2228
 
    goto error;
2229
 
  return false;
2230
 
 
2231
 
error:
2232
 
  context->process_error(session);
2233
 
  return true;
2234
 
}
2235
 
 
2236
 
 
2237
 
void Item_ref::set_properties()
2238
 
{
2239
 
  max_length= (*ref)->max_length;
2240
 
  maybe_null= (*ref)->maybe_null;
2241
 
  decimals=   (*ref)->decimals;
2242
 
  collation.set((*ref)->collation);
2243
 
  /*
2244
 
    We have to remember if we refer to a sum function, to ensure that
2245
 
    split_sum_func() doesn't try to change the reference.
2246
 
  */
2247
 
  with_sum_func= (*ref)->with_sum_func;
2248
 
  unsigned_flag= (*ref)->unsigned_flag;
2249
 
  fixed= 1;
2250
 
  if (alias_name_used)
2251
 
    return;
2252
 
  if ((*ref)->type() == FIELD_ITEM)
2253
 
    alias_name_used= ((Item_ident *) (*ref))->alias_name_used;
2254
 
  else
2255
 
    alias_name_used= true; // it is not field, so it is was resolved by alias
2256
 
}
2257
 
 
2258
 
 
2259
 
void Item_ref::cleanup()
2260
 
{
2261
 
  Item_ident::cleanup();
2262
 
  result_field= 0;
2263
 
  return;
2264
 
}
2265
 
 
2266
 
 
2267
 
void Item_ref::print(String *str, enum_query_type query_type)
2268
 
{
2269
 
  if (ref)
2270
 
  {
2271
 
    if ((*ref)->type() != Item::CACHE_ITEM &&
2272
 
        !table_name && name && alias_name_used)
2273
 
    {
2274
 
      Session *session= current_session;
2275
 
      append_identifier(session, str, name, (uint) strlen(name));
2276
 
    }
2277
 
    else
2278
 
      (*ref)->print(str, query_type);
2279
 
  }
2280
 
  else
2281
 
    Item_ident::print(str, query_type);
2282
 
}
2283
 
 
2284
 
 
2285
 
bool Item_ref::send(Protocol *prot, String *tmp)
2286
 
{
2287
 
  if (result_field)
2288
 
    return prot->store(result_field);
2289
 
  return (*ref)->send(prot, tmp);
2290
 
}
2291
 
 
2292
 
 
2293
 
double Item_ref::val_result()
2294
 
{
2295
 
  if (result_field)
2296
 
  {
2297
 
    if ((null_value= result_field->is_null()))
2298
 
      return 0.0;
2299
 
    return result_field->val_real();
2300
 
  }
2301
 
  return val_real();
2302
 
}
2303
 
 
2304
 
 
2305
 
int64_t Item_ref::val_int_result()
2306
 
{
2307
 
  if (result_field)
2308
 
  {
2309
 
    if ((null_value= result_field->is_null()))
2310
 
      return 0;
2311
 
    return result_field->val_int();
2312
 
  }
2313
 
  return val_int();
2314
 
}
2315
 
 
2316
 
 
2317
 
String *Item_ref::str_result(String* str)
2318
 
{
2319
 
  if (result_field)
2320
 
  {
2321
 
    if ((null_value= result_field->is_null()))
2322
 
      return 0;
2323
 
    str->set_charset(str_value.charset());
2324
 
    return result_field->val_str(str, &str_value);
2325
 
  }
2326
 
  return val_str(str);
2327
 
}
2328
 
 
2329
 
 
2330
 
my_decimal *Item_ref::val_decimal_result(my_decimal *decimal_value)
2331
 
{
2332
 
  if (result_field)
2333
 
  {
2334
 
    if ((null_value= result_field->is_null()))
2335
 
      return 0;
2336
 
    return result_field->val_decimal(decimal_value);
2337
 
  }
2338
 
  return val_decimal(decimal_value);
2339
 
}
2340
 
 
2341
 
 
2342
 
bool Item_ref::val_bool_result()
2343
 
{
2344
 
  if (result_field)
2345
 
  {
2346
 
    if ((null_value= result_field->is_null()))
2347
 
      return 0;
2348
 
    switch (result_field->result_type()) {
2349
 
    case INT_RESULT:
2350
 
      return result_field->val_int() != 0;
2351
 
    case DECIMAL_RESULT:
2352
 
    {
2353
 
      my_decimal decimal_value;
2354
 
      my_decimal *val= result_field->val_decimal(&decimal_value);
2355
 
      if (val)
2356
 
        return !my_decimal_is_zero(val);
2357
 
      return 0;
2358
 
    }
2359
 
    case REAL_RESULT:
2360
 
    case STRING_RESULT:
2361
 
      return result_field->val_real() != 0.0;
2362
 
    case ROW_RESULT:
2363
 
    default:
2364
 
      assert(0);
2365
 
    }
2366
 
  }
2367
 
  return val_bool();
2368
 
}
2369
 
 
2370
 
 
2371
 
double Item_ref::val_real()
2372
 
{
2373
 
  assert(fixed);
2374
 
  double tmp=(*ref)->val_result();
2375
 
  null_value=(*ref)->null_value;
2376
 
  return tmp;
2377
 
}
2378
 
 
2379
 
 
2380
 
int64_t Item_ref::val_int()
2381
 
{
2382
 
  assert(fixed);
2383
 
  int64_t tmp=(*ref)->val_int_result();
2384
 
  null_value=(*ref)->null_value;
2385
 
  return tmp;
2386
 
}
2387
 
 
2388
 
 
2389
 
bool Item_ref::val_bool()
2390
 
{
2391
 
  assert(fixed);
2392
 
  bool tmp= (*ref)->val_bool_result();
2393
 
  null_value= (*ref)->null_value;
2394
 
  return tmp;
2395
 
}
2396
 
 
2397
 
 
2398
 
String *Item_ref::val_str(String* tmp)
2399
 
{
2400
 
  assert(fixed);
2401
 
  tmp=(*ref)->str_result(tmp);
2402
 
  null_value=(*ref)->null_value;
2403
 
  return tmp;
2404
 
}
2405
 
 
2406
 
 
2407
 
bool Item_ref::is_null()
2408
 
{
2409
 
  assert(fixed);
2410
 
  return (*ref)->is_null();
2411
 
}
2412
 
 
2413
 
 
2414
 
bool Item_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
2415
 
{
2416
 
  return (null_value=(*ref)->get_date_result(ltime,fuzzydate));
2417
 
}
2418
 
 
2419
 
 
2420
 
my_decimal *Item_ref::val_decimal(my_decimal *decimal_value)
2421
 
{
2422
 
  my_decimal *val= (*ref)->val_decimal_result(decimal_value);
2423
 
  null_value= (*ref)->null_value;
2424
 
  return val;
2425
 
}
2426
 
 
2427
 
int Item_ref::save_in_field(Field *to, bool no_conversions)
2428
 
{
2429
 
  int res;
2430
 
  assert(!result_field);
2431
 
  res= (*ref)->save_in_field(to, no_conversions);
2432
 
  null_value= (*ref)->null_value;
2433
 
  return res;
2434
 
}
2435
 
 
2436
 
 
2437
 
void Item_ref::save_org_in_field(Field *field)
2438
 
{
2439
 
  (*ref)->save_org_in_field(field);
2440
 
}
2441
 
 
2442
 
 
2443
 
void Item_ref::make_field(Send_field *field)
2444
 
{
2445
 
  (*ref)->make_field(field);
2446
 
  /* Non-zero in case of a view */
2447
 
  if (name)
2448
 
    field->col_name= name;
2449
 
  if (table_name)
2450
 
    field->table_name= table_name;
2451
 
  if (db_name)
2452
 
    field->db_name= db_name;
2453
 
}
2454
 
 
2455
 
 
2456
 
Item *Item_ref::get_tmp_table_item(Session *session)
2457
 
{
2458
 
  if (!result_field)
2459
 
    return (*ref)->get_tmp_table_item(session);
2460
 
 
2461
 
  Item_field *item= new Item_field(result_field);
2462
 
  if (item)
2463
 
  {
2464
 
    item->table_name= table_name;
2465
 
    item->db_name= db_name;
2466
 
  }
2467
 
  return item;
2468
 
}
2469
 
 
2470
 
 
2471
 
void Item_ref_null_helper::print(String *str, enum_query_type query_type)
2472
 
{
2473
 
  str->append(STRING_WITH_LEN("<ref_null_helper>("));
2474
 
  if (ref)
2475
 
    (*ref)->print(str, query_type);
2476
 
  else
2477
 
    str->append('?');
2478
 
  str->append(')');
2479
 
}
2480
 
 
2481
 
 
2482
 
double Item_direct_ref::val_real()
2483
 
{
2484
 
  double tmp=(*ref)->val_real();
2485
 
  null_value=(*ref)->null_value;
2486
 
  return tmp;
2487
 
}
2488
 
 
2489
 
 
2490
 
int64_t Item_direct_ref::val_int()
2491
 
{
2492
 
  int64_t tmp=(*ref)->val_int();
2493
 
  null_value=(*ref)->null_value;
2494
 
  return tmp;
2495
 
}
2496
 
 
2497
 
 
2498
 
String *Item_direct_ref::val_str(String* tmp)
2499
 
{
2500
 
  tmp=(*ref)->val_str(tmp);
2501
 
  null_value=(*ref)->null_value;
2502
 
  return tmp;
2503
 
}
2504
 
 
2505
 
 
2506
 
my_decimal *Item_direct_ref::val_decimal(my_decimal *decimal_value)
2507
 
{
2508
 
  my_decimal *tmp= (*ref)->val_decimal(decimal_value);
2509
 
  null_value=(*ref)->null_value;
2510
 
  return tmp;
2511
 
}
2512
 
 
2513
 
 
2514
 
bool Item_direct_ref::val_bool()
2515
 
{
2516
 
  bool tmp= (*ref)->val_bool();
2517
 
  null_value=(*ref)->null_value;
2518
 
  return tmp;
2519
 
}
2520
 
 
2521
 
 
2522
 
bool Item_direct_ref::is_null()
2523
 
{
2524
 
  return (*ref)->is_null();
2525
 
}
2526
 
 
2527
 
 
2528
 
bool Item_direct_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
2529
 
{
2530
 
  return (null_value=(*ref)->get_date(ltime,fuzzydate));
2531
 
}
2532
 
 
2533
 
/*
2534
 
  Prepare referenced outer field then call usual Item_direct_ref::fix_fields
2535
 
 
2536
 
  SYNOPSIS
2537
 
    Item_outer_ref::fix_fields()
2538
 
    session         thread handler
2539
 
    reference   reference on reference where this item stored
2540
 
 
2541
 
  RETURN
2542
 
    false   OK
2543
 
    true    Error
2544
 
*/
2545
 
 
2546
 
bool Item_outer_ref::fix_fields(Session *session, Item **reference)
2547
 
{
2548
 
  bool err;
2549
 
  /* outer_ref->check_cols() will be made in Item_direct_ref::fix_fields */
2550
 
  if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(session, reference)))
2551
 
    return true;
2552
 
  err= Item_direct_ref::fix_fields(session, reference);
2553
 
  if (!outer_ref)
2554
 
    outer_ref= *ref;
2555
 
  if ((*ref)->type() == Item::FIELD_ITEM)
2556
 
    table_name= ((Item_field*)outer_ref)->table_name;
2557
 
  return err;
2558
 
}
2559
 
 
2560
 
void Item_outer_ref::fix_after_pullout(st_select_lex *new_parent, Item **ref)
2561
 
{
2562
 
  if (depended_from == new_parent)
2563
 
  {
2564
 
    *ref= outer_ref;
2565
 
    outer_ref->fix_after_pullout(new_parent, ref);
2566
 
  }
2567
 
}
2568
 
 
2569
 
void Item_ref::fix_after_pullout(st_select_lex *new_parent, Item **)
2570
 
{
2571
 
  if (depended_from == new_parent)
2572
 
  {
2573
 
    (*ref)->fix_after_pullout(new_parent, ref);
2574
 
    depended_from= NULL;
2575
 
  }
2576
 
}
2577
 
 
2578
1669
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
2579
1670
{
2580
1671
  return item->type() == DEFAULT_VALUE_ITEM &&
2690
1781
  return (this->*transformer)(args);
2691
1782
}
2692
1783
 
2693
 
 
2694
 
bool Item_insert_value::eq(const Item *item, bool binary_cmp) const
2695
 
{
2696
 
  return item->type() == INSERT_VALUE_ITEM &&
2697
 
    ((Item_default_value *)item)->arg->eq(arg, binary_cmp);
2698
 
}
2699
 
 
2700
 
 
2701
 
bool Item_insert_value::fix_fields(Session *session, Item **)
2702
 
{
2703
 
  assert(fixed == 0);
2704
 
  /* We should only check that arg is in first table */
2705
 
  if (!arg->fixed)
2706
 
  {
2707
 
    bool res;
2708
 
    TableList *orig_next_table= context->last_name_resolution_table;
2709
 
    context->last_name_resolution_table= context->first_name_resolution_table;
2710
 
    res= arg->fix_fields(session, &arg);
2711
 
    context->last_name_resolution_table= orig_next_table;
2712
 
    if (res)
2713
 
      return true;
2714
 
  }
2715
 
 
2716
 
  if (arg->type() == REF_ITEM)
2717
 
  {
2718
 
    Item_ref *ref= (Item_ref *)arg;
2719
 
    if (ref->ref[0]->type() != FIELD_ITEM)
2720
 
    {
2721
 
      my_error(ER_BAD_FIELD_ERROR, MYF(0), "", "VALUES() function");
2722
 
      return true;
2723
 
    }
2724
 
    arg= ref->ref[0];
2725
 
  }
2726
 
  /*
2727
 
    According to our SQL grammar, VALUES() function can reference
2728
 
    only to a column.
2729
 
  */
2730
 
  assert(arg->type() == FIELD_ITEM);
2731
 
 
2732
 
  Item_field *field_arg= (Item_field *)arg;
2733
 
 
2734
 
  if (field_arg->field->table->insert_values)
2735
 
  {
2736
 
    Field *def_field= (Field*) sql_alloc(field_arg->field->size_of());
2737
 
    if (!def_field)
2738
 
      return true;
2739
 
    memcpy(def_field, field_arg->field, field_arg->field->size_of());
2740
 
    def_field->move_field_offset((my_ptrdiff_t)
2741
 
                                 (def_field->table->insert_values -
2742
 
                                  def_field->table->record[0]));
2743
 
    set_field(def_field);
2744
 
  }
2745
 
  else
2746
 
  {
2747
 
    Field *tmp_field= field_arg->field;
2748
 
    /* charset doesn't matter here, it's to avoid sigsegv only */
2749
 
    tmp_field= new Field_null(0, 0, Field::NONE, field_arg->field->field_name,
2750
 
                          &my_charset_bin);
2751
 
    if (tmp_field)
2752
 
    {
2753
 
      tmp_field->init(field_arg->field->table);
2754
 
      set_field(tmp_field);
2755
 
    }
2756
 
  }
2757
 
  return false;
2758
 
}
2759
 
 
2760
 
void Item_insert_value::print(String *str, enum_query_type query_type)
2761
 
{
2762
 
  str->append(STRING_WITH_LEN("values("));
2763
 
  arg->print(str, query_type);
2764
 
  str->append(')');
2765
 
}
2766
 
 
2767
 
 
2768
1784
Item_result item_cmp_type(Item_result a,Item_result b)
2769
1785
{
2770
1786
  if (a == STRING_RESULT && b == STRING_RESULT)
2915
1931
  return result == field->val_real();
2916
1932
}
2917
1933
 
2918
 
Item_cache* Item_cache::get_cache(const Item *item)
2919
 
{
2920
 
  switch (item->result_type()) {
2921
 
  case INT_RESULT:
2922
 
    return new Item_cache_int();
2923
 
  case REAL_RESULT:
2924
 
    return new Item_cache_real();
2925
 
  case DECIMAL_RESULT:
2926
 
    return new Item_cache_decimal();
2927
 
  case STRING_RESULT:
2928
 
    return new Item_cache_str(item);
2929
 
  case ROW_RESULT:
2930
 
    return new Item_cache_row();
2931
 
  default:
2932
 
    // should never be in real life
2933
 
    assert(0);
2934
 
    return 0;
2935
 
  }
2936
 
}
2937
 
 
2938
 
 
2939
 
void Item_cache::print(String *str, enum_query_type query_type)
2940
 
{
2941
 
  str->append(STRING_WITH_LEN("<cache>("));
2942
 
  if (example)
2943
 
    example->print(str, query_type);
2944
 
  else
2945
 
    Item::print(str, query_type);
2946
 
  str->append(')');
2947
 
}
2948
 
 
2949
 
 
2950
 
bool Item_cache::eq_def(Field *field)
2951
 
{
2952
 
  return cached_field ? cached_field->eq_def (field) : false;
2953
 
}
2954
 
 
2955
 
 
2956
 
void Item_cache_int::store(Item *item)
2957
 
{
2958
 
  value= item->val_int_result();
2959
 
  null_value= item->null_value;
2960
 
  unsigned_flag= item->unsigned_flag;
2961
 
}
2962
 
 
2963
 
 
2964
 
void Item_cache_int::store(Item *item, int64_t val_arg)
2965
 
{
2966
 
  value= val_arg;
2967
 
  null_value= item->null_value;
2968
 
  unsigned_flag= item->unsigned_flag;
2969
 
}
2970
 
 
2971
 
 
2972
 
String *Item_cache_int::val_str(String *str)
2973
 
{
2974
 
  assert(fixed == 1);
2975
 
  str->set(value, default_charset());
2976
 
  return str;
2977
 
}
2978
 
 
2979
 
 
2980
 
my_decimal *Item_cache_int::val_decimal(my_decimal *decimal_val)
2981
 
{
2982
 
  assert(fixed == 1);
2983
 
  int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_val);
2984
 
  return decimal_val;
2985
 
}
2986
 
 
2987
 
 
2988
 
void Item_cache_real::store(Item *item)
2989
 
{
2990
 
  value= item->val_result();
2991
 
  null_value= item->null_value;
2992
 
}
2993
 
 
2994
 
 
2995
 
int64_t Item_cache_real::val_int()
2996
 
{
2997
 
  assert(fixed == 1);
2998
 
  return (int64_t) rint(value);
2999
 
}
3000
 
 
3001
 
 
3002
 
String* Item_cache_real::val_str(String *str)
3003
 
{
3004
 
  assert(fixed == 1);
3005
 
  str->set_real(value, decimals, default_charset());
3006
 
  return str;
3007
 
}
3008
 
 
3009
 
 
3010
 
my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val)
3011
 
{
3012
 
  assert(fixed == 1);
3013
 
  double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
3014
 
  return decimal_val;
3015
 
}
3016
 
 
3017
 
 
3018
 
void Item_cache_decimal::store(Item *item)
3019
 
{
3020
 
  my_decimal *val= item->val_decimal_result(&decimal_value);
3021
 
  if (!(null_value= item->null_value) && val != &decimal_value)
3022
 
    my_decimal2decimal(val, &decimal_value);
3023
 
}
3024
 
 
3025
 
double Item_cache_decimal::val_real()
3026
 
{
3027
 
  assert(fixed);
3028
 
  double res;
3029
 
  my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &res);
3030
 
  return res;
3031
 
}
3032
 
 
3033
 
int64_t Item_cache_decimal::val_int()
3034
 
{
3035
 
  assert(fixed);
3036
 
  int64_t res;
3037
 
  my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &res);
3038
 
  return res;
3039
 
}
3040
 
 
3041
 
String* Item_cache_decimal::val_str(String *str)
3042
 
{
3043
 
  assert(fixed);
3044
 
  my_decimal_round(E_DEC_FATAL_ERROR, &decimal_value, decimals, false,
3045
 
                   &decimal_value);
3046
 
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, str);
3047
 
  return str;
3048
 
}
3049
 
 
3050
 
my_decimal *Item_cache_decimal::val_decimal(my_decimal *)
3051
 
{
3052
 
  assert(fixed);
3053
 
  return &decimal_value;
3054
 
}
3055
 
 
3056
 
 
3057
 
Item_cache_str::Item_cache_str(const Item *item) :
3058
 
  Item_cache(), value(0),
3059
 
  is_varbinary(item->type() == FIELD_ITEM &&
3060
 
               ((const Item_field *) item)->field->type() ==
3061
 
               DRIZZLE_TYPE_VARCHAR &&
3062
 
               !((const Item_field *) item)->field->has_charset())
3063
 
{}
3064
 
 
3065
 
void Item_cache_str::store(Item *item)
3066
 
{
3067
 
  value_buff.set(buffer, sizeof(buffer), item->collation.collation);
3068
 
  value= item->str_result(&value_buff);
3069
 
  if ((null_value= item->null_value))
3070
 
    value= 0;
3071
 
  else if (value != &value_buff)
3072
 
  {
3073
 
    /*
3074
 
      We copy string value to avoid changing value if 'item' is table field
3075
 
      in queries like following (where t1.c is varchar):
3076
 
      select a,
3077
 
             (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),
3078
 
             (select c from t1 where a=t2.a)
3079
 
        from t2;
3080
 
    */
3081
 
    value_buff.copy(*value);
3082
 
    value= &value_buff;
3083
 
  }
3084
 
}
3085
 
 
3086
 
double Item_cache_str::val_real()
3087
 
{
3088
 
  assert(fixed == 1);
3089
 
  int err_not_used;
3090
 
  char *end_not_used;
3091
 
  if (value)
3092
 
    return my_strntod(value->charset(), (char*) value->ptr(),
3093
 
                      value->length(), &end_not_used, &err_not_used);
3094
 
  return (double) 0;
3095
 
}
3096
 
 
3097
 
 
3098
 
int64_t Item_cache_str::val_int()
3099
 
{
3100
 
  assert(fixed == 1);
3101
 
  int err;
3102
 
  if (value)
3103
 
    return my_strntoll(value->charset(), value->ptr(),
3104
 
                       value->length(), 10, (char**) 0, &err);
3105
 
  else
3106
 
    return (int64_t)0;
3107
 
}
3108
 
 
3109
 
my_decimal *Item_cache_str::val_decimal(my_decimal *decimal_val)
3110
 
{
3111
 
  assert(fixed == 1);
3112
 
  if (value)
3113
 
    string2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
3114
 
  else
3115
 
    decimal_val= 0;
3116
 
  return decimal_val;
3117
 
}
3118
 
 
3119
 
 
3120
 
int Item_cache_str::save_in_field(Field *field, bool no_conversions)
3121
 
{
3122
 
  int res= Item_cache::save_in_field(field, no_conversions);
3123
 
 
3124
 
  return res;
3125
 
}
3126
 
 
3127
 
 
3128
1934
/**
3129
1935
  Dummy error processor used by default by Name_resolution_context.
3130
1936