1684
Item *Item_int_with_ref::clone_item()
1686
assert(ref->const_item());
1688
We need to evaluate the constant to make sure it works with
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));
1697
inline uint32_t char_val(char X)
1699
return (uint) (X >= '0' && X <= '9' ? X-'0' :
1700
X >= 'A' && X <= 'Z' ? X-'A'+10 :
1705
Item_hex_string::Item_hex_string(const char *str, uint32_t str_length)
1707
max_length=(str_length+1)/2;
1708
char *ptr=(char*) sql_alloc(max_length+1);
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
1717
*ptr++= (char) (char_val(str[0])*16+char_val(str[1]));
1720
*ptr=0; // Keep purify happy
1721
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
1726
int64_t Item_hex_string::val_int()
1728
// following assert is redundant, because fixed=1 assigned in constructor
1730
char *end=(char*) str_value.ptr()+str_value.length(),
1731
*ptr=end-cmin(str_value.length(),(uint32_t)sizeof(int64_t));
1734
for (; ptr != end ; ptr++)
1735
value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
1736
return (int64_t) value;
1740
my_decimal *Item_hex_string::val_decimal(my_decimal *decimal_value)
1742
// following assert is redundant, because fixed=1 assigned in constructor
1744
uint64_t value= (uint64_t)val_int();
1745
int2my_decimal(E_DEC_FATAL_ERROR, value, true, decimal_value);
1746
return (decimal_value);
1750
int Item_hex_string::save_in_field(Field *field, bool)
1752
field->set_notnull();
1753
if (field->result_type() == STRING_RESULT)
1754
return field->store(str_value.ptr(), str_value.length(),
1755
collation.collation);
1758
uint32_t length= str_value.length();
1761
nr= field->flags & UNSIGNED_FLAG ? UINT64_MAX : INT64_MAX;
1764
nr= (uint64_t) val_int();
1765
if ((length == 8) && !(field->flags & UNSIGNED_FLAG) && (nr > INT64_MAX))
1770
return field->store((int64_t) nr, true); // Assume hex numbers are unsigned
1773
if (!field->store((int64_t) nr, true))
1774
field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE,
1780
void Item_hex_string::print(String *str, enum_query_type)
1782
char *end= (char*) str_value.ptr() + str_value.length(),
1783
*ptr= end - cmin(str_value.length(), (uint32_t)sizeof(int64_t));
1785
for (; ptr != end ; ptr++)
1787
str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
1788
str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
1793
bool Item_hex_string::eq(const Item *arg, bool binary_cmp) const
1795
if (arg->basic_const_item() && arg->type() == type())
1798
return !stringcmp(&str_value, &arg->str_value);
1799
return !sortcmp(&str_value, &arg->str_value, collation.collation);
1805
Item *Item_hex_string::safe_charset_converter(const CHARSET_INFO * const tocs)
1808
String tmp, *str= val_str(&tmp);
1810
if (!(conv= new Item_string(str->ptr(), str->length(), tocs)))
1812
conv->str_value.copy();
1813
conv->str_value.mark_as_const();
1820
In string context this is a binary string.
1821
In number context this is a int64_t value.
1824
Item_bin_string::Item_bin_string(const char *str, uint32_t str_length)
1826
const char *end= str + str_length - 1;
1827
unsigned char bits= 0;
1830
max_length= (str_length + 7) >> 3;
1831
char *ptr= (char*) sql_alloc(max_length + 1);
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--)
1850
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
1856
1596
This is only called from items that is not of type item_field.
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)
1936
alias_name_used= alias_name_used_arg;
1938
This constructor used to create some internals references over fixed items
1940
if (ref && *ref && (*ref)->fixed)
1946
Resolve the name of a reference to a column reference.
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.
1954
Item_ref::ref is 0 or points to a valid item.
1957
The name resolution algorithm used is (where [T_j] is an optional table
1958
name that qualifies the column name):
1961
resolve_extended([T_j].col_ref_i)
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.
1966
if such a column is NOT found AND // Lookup in outer queries.
1967
there are outer queries
1969
for each outer query Q_k beginning from the inner-most one
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.
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
1978
- Q_(k-1) is not in a HAVING or SELECT clause of Q_k
1980
search for a column or derived column named col_ref_i
1981
[in table T_j] in the FROM clause of Q_k;
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.
1993
@param[in] session current thread
1994
@param[in,out] reference view column if this item was resolved to a
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.
2009
bool Item_ref::fix_fields(Session *session, Item **reference)
2011
enum_parsing_place place= NO_MATTER;
2013
SELECT_LEX *current_sel= session->lex->current_select;
2015
if (!ref || ref == not_found_item)
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). */
2021
if (ref == not_found_item) /* This reference was not resolved. */
2023
Name_resolution_context *last_checked_context= context;
2024
Name_resolution_context *outer_context= context->outer_context;
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);
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).
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.
2045
from_field= (Field*) not_found_field;
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;
2054
/* Search in the SELECT and GROUP lists of the outer select. */
2055
if (outer_context->resolve_in_select_list)
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)
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();
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.
2074
place= prev_subselect_item->parsing_place;
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
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.
2085
if ((place != IN_HAVING ||
2086
(!select->with_sum_func &&
2087
select->group_list.elements == 0)))
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
2094
from_field= find_field_in_tables(session, this,
2096
first_name_resolution_table,
2098
last_name_resolution_table,
2100
IGNORE_EXCEPT_NON_UNIQUE,
2104
if (from_field == view_ref_found)
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) :
2119
view reference found, we substituted it instead of this
2124
if (from_field != not_found_field)
2126
if (cached_table && cached_table->select_lex &&
2127
outer_context->select_lex &&
2128
cached_table->select_lex != outer_context->select_lex)
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.
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);
2145
prev_subselect_item->used_tables_cache|= from_field->table->map;
2146
prev_subselect_item->const_item_cache= 0;
2150
assert(from_field == not_found_field);
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;
2156
outer_context= outer_context->outer_context;
2157
} while (outer_context);
2159
assert(from_field != 0 && from_field != view_ref_found);
2160
if (from_field != not_found_field)
2163
if (!(fld= new Item_field(from_field)))
2165
session->change_item_tree(reference, fld);
2166
mark_as_dependent(session, last_checked_context->select_lex,
2167
session->lex->current_select, this, fld);
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.
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);
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);
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);
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.
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);
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.
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)) ||
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"));
2227
if ((*ref)->check_cols(1))
2232
context->process_error(session);
2237
void Item_ref::set_properties()
2239
max_length= (*ref)->max_length;
2240
maybe_null= (*ref)->maybe_null;
2241
decimals= (*ref)->decimals;
2242
collation.set((*ref)->collation);
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.
2247
with_sum_func= (*ref)->with_sum_func;
2248
unsigned_flag= (*ref)->unsigned_flag;
2250
if (alias_name_used)
2252
if ((*ref)->type() == FIELD_ITEM)
2253
alias_name_used= ((Item_ident *) (*ref))->alias_name_used;
2255
alias_name_used= true; // it is not field, so it is was resolved by alias
2259
void Item_ref::cleanup()
2261
Item_ident::cleanup();
2267
void Item_ref::print(String *str, enum_query_type query_type)
2271
if ((*ref)->type() != Item::CACHE_ITEM &&
2272
!table_name && name && alias_name_used)
2274
Session *session= current_session;
2275
append_identifier(session, str, name, (uint) strlen(name));
2278
(*ref)->print(str, query_type);
2281
Item_ident::print(str, query_type);
2285
bool Item_ref::send(Protocol *prot, String *tmp)
2288
return prot->store(result_field);
2289
return (*ref)->send(prot, tmp);
2293
double Item_ref::val_result()
2297
if ((null_value= result_field->is_null()))
2299
return result_field->val_real();
2305
int64_t Item_ref::val_int_result()
2309
if ((null_value= result_field->is_null()))
2311
return result_field->val_int();
2317
String *Item_ref::str_result(String* str)
2321
if ((null_value= result_field->is_null()))
2323
str->set_charset(str_value.charset());
2324
return result_field->val_str(str, &str_value);
2326
return val_str(str);
2330
my_decimal *Item_ref::val_decimal_result(my_decimal *decimal_value)
2334
if ((null_value= result_field->is_null()))
2336
return result_field->val_decimal(decimal_value);
2338
return val_decimal(decimal_value);
2342
bool Item_ref::val_bool_result()
2346
if ((null_value= result_field->is_null()))
2348
switch (result_field->result_type()) {
2350
return result_field->val_int() != 0;
2351
case DECIMAL_RESULT:
2353
my_decimal decimal_value;
2354
my_decimal *val= result_field->val_decimal(&decimal_value);
2356
return !my_decimal_is_zero(val);
2361
return result_field->val_real() != 0.0;
2371
double Item_ref::val_real()
2374
double tmp=(*ref)->val_result();
2375
null_value=(*ref)->null_value;
2380
int64_t Item_ref::val_int()
2383
int64_t tmp=(*ref)->val_int_result();
2384
null_value=(*ref)->null_value;
2389
bool Item_ref::val_bool()
2392
bool tmp= (*ref)->val_bool_result();
2393
null_value= (*ref)->null_value;
2398
String *Item_ref::val_str(String* tmp)
2401
tmp=(*ref)->str_result(tmp);
2402
null_value=(*ref)->null_value;
2407
bool Item_ref::is_null()
2410
return (*ref)->is_null();
2414
bool Item_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
2416
return (null_value=(*ref)->get_date_result(ltime,fuzzydate));
2420
my_decimal *Item_ref::val_decimal(my_decimal *decimal_value)
2422
my_decimal *val= (*ref)->val_decimal_result(decimal_value);
2423
null_value= (*ref)->null_value;
2427
int Item_ref::save_in_field(Field *to, bool no_conversions)
2430
assert(!result_field);
2431
res= (*ref)->save_in_field(to, no_conversions);
2432
null_value= (*ref)->null_value;
2437
void Item_ref::save_org_in_field(Field *field)
2439
(*ref)->save_org_in_field(field);
2443
void Item_ref::make_field(Send_field *field)
2445
(*ref)->make_field(field);
2446
/* Non-zero in case of a view */
2448
field->col_name= name;
2450
field->table_name= table_name;
2452
field->db_name= db_name;
2456
Item *Item_ref::get_tmp_table_item(Session *session)
2459
return (*ref)->get_tmp_table_item(session);
2461
Item_field *item= new Item_field(result_field);
2464
item->table_name= table_name;
2465
item->db_name= db_name;
2471
void Item_ref_null_helper::print(String *str, enum_query_type query_type)
2473
str->append(STRING_WITH_LEN("<ref_null_helper>("));
2475
(*ref)->print(str, query_type);
2482
double Item_direct_ref::val_real()
2484
double tmp=(*ref)->val_real();
2485
null_value=(*ref)->null_value;
2490
int64_t Item_direct_ref::val_int()
2492
int64_t tmp=(*ref)->val_int();
2493
null_value=(*ref)->null_value;
2498
String *Item_direct_ref::val_str(String* tmp)
2500
tmp=(*ref)->val_str(tmp);
2501
null_value=(*ref)->null_value;
2506
my_decimal *Item_direct_ref::val_decimal(my_decimal *decimal_value)
2508
my_decimal *tmp= (*ref)->val_decimal(decimal_value);
2509
null_value=(*ref)->null_value;
2514
bool Item_direct_ref::val_bool()
2516
bool tmp= (*ref)->val_bool();
2517
null_value=(*ref)->null_value;
2522
bool Item_direct_ref::is_null()
2524
return (*ref)->is_null();
2528
bool Item_direct_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
2530
return (null_value=(*ref)->get_date(ltime,fuzzydate));
2534
Prepare referenced outer field then call usual Item_direct_ref::fix_fields
2537
Item_outer_ref::fix_fields()
2538
session thread handler
2539
reference reference on reference where this item stored
2546
bool Item_outer_ref::fix_fields(Session *session, Item **reference)
2549
/* outer_ref->check_cols() will be made in Item_direct_ref::fix_fields */
2550
if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(session, reference)))
2552
err= Item_direct_ref::fix_fields(session, reference);
2555
if ((*ref)->type() == Item::FIELD_ITEM)
2556
table_name= ((Item_field*)outer_ref)->table_name;
2560
void Item_outer_ref::fix_after_pullout(st_select_lex *new_parent, Item **ref)
2562
if (depended_from == new_parent)
2565
outer_ref->fix_after_pullout(new_parent, ref);
2569
void Item_ref::fix_after_pullout(st_select_lex *new_parent, Item **)
2571
if (depended_from == new_parent)
2573
(*ref)->fix_after_pullout(new_parent, ref);
2574
depended_from= NULL;
2578
1669
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
2580
1671
return item->type() == DEFAULT_VALUE_ITEM &&
2915
1931
return result == field->val_real();
2918
Item_cache* Item_cache::get_cache(const Item *item)
2920
switch (item->result_type()) {
2922
return new Item_cache_int();
2924
return new Item_cache_real();
2925
case DECIMAL_RESULT:
2926
return new Item_cache_decimal();
2928
return new Item_cache_str(item);
2930
return new Item_cache_row();
2932
// should never be in real life
2939
void Item_cache::print(String *str, enum_query_type query_type)
2941
str->append(STRING_WITH_LEN("<cache>("));
2943
example->print(str, query_type);
2945
Item::print(str, query_type);
2950
bool Item_cache::eq_def(Field *field)
2952
return cached_field ? cached_field->eq_def (field) : false;
2956
void Item_cache_int::store(Item *item)
2958
value= item->val_int_result();
2959
null_value= item->null_value;
2960
unsigned_flag= item->unsigned_flag;
2964
void Item_cache_int::store(Item *item, int64_t val_arg)
2967
null_value= item->null_value;
2968
unsigned_flag= item->unsigned_flag;
2972
String *Item_cache_int::val_str(String *str)
2975
str->set(value, default_charset());
2980
my_decimal *Item_cache_int::val_decimal(my_decimal *decimal_val)
2983
int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_val);
2988
void Item_cache_real::store(Item *item)
2990
value= item->val_result();
2991
null_value= item->null_value;
2995
int64_t Item_cache_real::val_int()
2998
return (int64_t) rint(value);
3002
String* Item_cache_real::val_str(String *str)
3005
str->set_real(value, decimals, default_charset());
3010
my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val)
3013
double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
3018
void Item_cache_decimal::store(Item *item)
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);
3025
double Item_cache_decimal::val_real()
3029
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &res);
3033
int64_t Item_cache_decimal::val_int()
3037
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &res);
3041
String* Item_cache_decimal::val_str(String *str)
3044
my_decimal_round(E_DEC_FATAL_ERROR, &decimal_value, decimals, false,
3046
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, str);
3050
my_decimal *Item_cache_decimal::val_decimal(my_decimal *)
3053
return &decimal_value;
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())
3065
void Item_cache_str::store(Item *item)
3067
value_buff.set(buffer, sizeof(buffer), item->collation.collation);
3068
value= item->str_result(&value_buff);
3069
if ((null_value= item->null_value))
3071
else if (value != &value_buff)
3074
We copy string value to avoid changing value if 'item' is table field
3075
in queries like following (where t1.c is varchar):
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)
3081
value_buff.copy(*value);
3086
double Item_cache_str::val_real()
3092
return my_strntod(value->charset(), (char*) value->ptr(),
3093
value->length(), &end_not_used, &err_not_used);
3098
int64_t Item_cache_str::val_int()
3103
return my_strntoll(value->charset(), value->ptr(),
3104
value->length(), 10, (char**) 0, &err);
3109
my_decimal *Item_cache_str::val_decimal(my_decimal *decimal_val)
3113
string2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
3120
int Item_cache_str::save_in_field(Field *field, bool no_conversions)
3122
int res= Item_cache::save_in_field(field, no_conversions);
3129
1935
Dummy error processor used by default by Name_resolution_context.