~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

move functions from item.cc/item.h to item directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
1399
1399
  return f_type;
1400
1400
}
1401
1401
 
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
1402
enum_field_types Item::field_type() const
1410
1403
{
1411
1404
  switch (result_type()) {
1694
1687
}
1695
1688
 
1696
1689
 
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
1690
/**
1856
1691
  This is only called from items that is not of type item_field.
1857
1692
*/
1926
1761
}
1927
1762
 
1928
1763
 
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
1764
void Item_ref_null_helper::print(String *str, enum_query_type query_type)
2472
1765
{
2473
1766
  str->append(STRING_WITH_LEN("<ref_null_helper>("));
2566
1859
  }
2567
1860
}
2568
1861
 
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
1862
 
2578
1863
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
2579
1864
{