17
17
#include <drizzled/server_includes.h>
18
18
#include <drizzled/sql_select.h>
19
#include <drizzled/drizzled_error_messages.h>
19
#include <drizzled/error.h>
22
#if defined(CMATH_NAMESPACE)
23
using namespace CMATH_NAMESPACE;
21
26
const String my_null_string("NULL", 4, default_charset_info);
37
42
return &real_traits_instance;
45
int64_t Hybrid_type_traits::val_int(Hybrid_type *val,
48
return (int64_t) rint(val->real);
42
Hybrid_type_traits::val_decimal(Hybrid_type *val,
43
my_decimal *to __attribute__((unused))) const
52
Hybrid_type_traits::val_decimal(Hybrid_type *val, my_decimal *) const
45
54
double2my_decimal(E_DEC_FATAL_ERROR, val->real, val->dec_buf);
46
55
return val->dec_buf;
256
264
res->ptr(), res->length(), res->charset(),
257
265
decimal_value) & E_DEC_BAD_NUM)
259
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
267
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
260
268
ER_TRUNCATED_WRONG_VALUE,
261
269
ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
262
270
str_value.c_ptr());
382
390
cmp_context= (Item_result)-1;
384
392
/* Put item in free list so that we can free all items at end */
385
THD *thd= current_thd;
386
next= thd->free_list;
387
thd->free_list= this;
393
Session *session= current_session;
394
next= session->free_list;
395
session->free_list= this;
389
397
Item constructor can be called during execution other then SQL_COM
390
command => we should check thd->lex->current_select on zero (thd->lex
398
command => we should check session->lex->current_select on zero (session->lex
391
399
can be uninitialised)
393
if (thd->lex->current_select)
401
if (session->lex->current_select)
395
403
enum_parsing_place place=
396
thd->lex->current_select->parsing_place;
404
session->lex->current_select->parsing_place;
397
405
if (place == SELECT_LIST ||
398
406
place == IN_HAVING)
399
thd->lex->current_select->select_n_having_items++;
407
session->lex->current_select->select_n_having_items++;
424
432
collation(item->collation),
425
433
cmp_context(item->cmp_context)
427
next= thd->free_list; // Put in free list
428
thd->free_list= this;
435
next= session->free_list; // Put in free list
436
session->free_list= this;
432
uint Item::decimal_precision() const
440
uint32_t Item::decimal_precision() const
434
442
Item_result restype= result_type();
436
444
if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
437
return min(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
445
return cmin(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
438
446
(unsigned int)DECIMAL_MAX_PRECISION);
439
return min(max_length, (uint32_t)DECIMAL_MAX_PRECISION);
447
return cmin(max_length, (uint32_t)DECIMAL_MAX_PRECISION);
501
509
This function is designed to ease transformation of Item trees.
502
510
Re-execution note: every such transformation is registered for
503
rollback by THD::change_item_tree() and is rolled back at the end
504
of execution by THD::rollback_item_tree_changes().
511
rollback by Session::change_item_tree() and is rolled back at the end
512
of execution by Session::rollback_item_tree_changes().
507
515
- this function can not be used at prepared statement prepare
508
516
(in particular, in fix_fields!), as only permanent
509
517
transformation of Item trees are allowed at prepare.
510
518
- the transformer function shall allocate new Items in execution
511
memory root (thd->mem_root) and not anywhere else: allocated
519
memory root (session->mem_root) and not anywhere else: allocated
512
520
items will be gone in the end of execution.
514
522
If you don't need to transform an item tree, but only traverse
519
527
@param arg opaque argument passed to the functor
522
Returns pointer to the new subtree root. THD::change_item_tree()
530
Returns pointer to the new subtree root. Session::change_item_tree()
523
531
should be called for it if transformation took place, i.e. if a
524
532
pointer to newly allocated item is returned.
527
Item* Item::transform(Item_transformer transformer, uchar *arg)
535
Item* Item::transform(Item_transformer transformer, unsigned char *arg)
529
537
return (this->*transformer)(arg);
548
556
Constructor used by Item_field & Item_*_ref (see Item comment)
551
Item_ident::Item_ident(THD *thd, Item_ident *item)
559
Item_ident::Item_ident(Session *session, Item_ident *item)
560
:Item(session, item),
553
561
orig_db_name(item->orig_db_name),
554
562
orig_table_name(item->orig_table_name),
555
563
orig_field_name(item->orig_field_name),
661
669
column read set or to register used fields in a view
664
bool Item_field::register_field_in_read_map(uchar *arg)
672
bool Item_field::register_field_in_read_map(unsigned char *arg)
666
674
Table *table= (Table *) arg;
667
675
if (field->table == table || !table)
668
676
bitmap_set_bit(field->table->read_set, field->field_index);
677
if (field->vcol_info && field->vcol_info->expr_item)
678
return field->vcol_info->expr_item->walk(&Item::register_field_in_read_map,
673
bool Item::check_cols(uint c)
684
Mark field in bitmap supplied as *arg
688
bool Item_field::register_field_in_bitmap(unsigned char *arg)
690
MY_BITMAP *bitmap= (MY_BITMAP *) arg;
692
bitmap_set_bit(bitmap, field->field_index);
697
bool Item::check_cols(uint32_t c)
705
729
if (orig_len != length && !is_autogenerated_name)
708
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
732
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
709
733
ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY),
710
734
str + length - orig_len);
712
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
736
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
713
737
ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES),
714
738
str + length - orig_len);
901
925
int64_t value= val_int();
903
if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1LL)
927
if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
905
929
char buff[22], *end;
906
930
end= int64_t10_to_str(value, buff, -10);
907
make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
931
make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
908
932
buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
956
980
Table *table= field->table;
957
THD *thd= table->in_use;
958
enum_check_fields tmp= thd->count_cuted_fields;
959
ulong sql_mode= thd->variables.sql_mode;
960
thd->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
961
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
981
Session *session= table->in_use;
982
enum_check_fields tmp= session->count_cuted_fields;
983
ulong sql_mode= session->variables.sql_mode;
984
session->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
985
session->count_cuted_fields= CHECK_FIELD_IGNORE;
962
986
res= save_in_field(field, no_conversions);
963
thd->count_cuted_fields= tmp;
964
thd->variables.sql_mode= sql_mode;
987
session->count_cuted_fields= tmp;
988
session->variables.sql_mode= sql_mode;
1004
1028
All found SUM items are added FIRST in the fields list and
1005
1029
we replace the item with a reference.
1007
thd->fatal_error() may be called if we are out of memory
1031
session->fatal_error() may be called if we are out of memory
1010
void Item::split_sum_func2(THD *thd, Item **ref_pointer_array,
1034
void Item::split_sum_func2(Session *session, Item **ref_pointer_array,
1011
1035
List<Item> &fields, Item **ref,
1012
1036
bool skip_registered)
1021
1045
((Item_func *) this)->functype() == Item_func::TRIG_COND_FUNC)))
1023
1047
/* Will split complicated items and ignore simple ones */
1024
split_sum_func(thd, ref_pointer_array, fields);
1048
split_sum_func(session, ref_pointer_array, fields);
1026
1050
else if ((type() == SUM_FUNC_ITEM || (used_tables() & ~PARAM_TABLE_BIT)) &&
1027
1051
type() != SUBSELECT_ITEM &&
1039
1063
Item_ref to allow fields from view being stored in tmp table.
1041
1065
Item_aggregate_ref *item_ref;
1042
uint el= fields.elements;
1066
uint32_t el= fields.elements;
1043
1067
Item *real_itm= real_item();
1045
1069
ref_pointer_array[el]= real_itm;
1046
if (!(item_ref= new Item_aggregate_ref(&thd->lex->current_select->context,
1070
if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
1047
1071
ref_pointer_array + el, 0, name)))
1048
1072
return; // fatal_error is set
1049
1073
if (type() == SUM_FUNC_ITEM)
1050
1074
item_ref->depended_from= ((Item_sum *) this)->depended_from();
1051
1075
fields.push_front(real_itm);
1052
thd->change_item_tree(ref, item_ref);
1076
session->change_item_tree(ref, item_ref);
1245
1269
bool agg_item_collations(DTCollation &c, const char *fname,
1246
Item **av, uint count, uint flags, int item_sep)
1270
Item **av, uint32_t count, uint32_t flags, int item_sep)
1250
1274
c.set(av[0]->collation);
1251
1275
for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
1269
1293
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
1270
Item **av, uint count, uint flags)
1294
Item **av, uint32_t count, uint32_t flags)
1272
1296
return (agg_item_collations(c, fname, av, count,
1273
1297
flags | MY_COLL_DISALLOW_NONE, 1));
1296
1320
collect(A,B,C) ::= collect(collect(A,B),C)
1298
Since this function calls THD::change_item_tree() on the passed Item **
1322
Since this function calls Session::change_item_tree() on the passed Item **
1299
1323
pointers, it is necessary to pass the original Item **'s, not copies.
1300
1324
Otherwise their values will not be properly restored (see BUG#20769).
1301
1325
If the items are not consecutive (eg. args[2] and args[5]), use the
1308
1332
bool agg_item_charsets(DTCollation &coll, const char *fname,
1309
Item **args, uint nargs, uint flags, int item_sep)
1333
Item **args, uint32_t nargs, uint32_t flags, int item_sep)
1311
1335
Item **arg, *safe_args[2];
1328
1352
safe_args[1]= args[item_sep];
1331
THD *thd= current_thd;
1332
Query_arena *arena, backup;
1355
Session *session= current_session;
1333
1356
bool res= false;
1336
In case we're in statement prepare, create conversion item
1337
in its memory: it will be reused on each execute.
1341
1359
for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
1375
1393
been created in prepare. In this case register the change for
1378
thd->change_item_tree(arg, conv);
1396
session->change_item_tree(arg, conv);
1380
1398
We do not check conv->fixed, because Item_func_conv_charset which can
1381
1399
be return by safe_charset_converter can't be fixed at creation
1383
conv->fix_fields(thd, arg);
1401
conv->fix_fields(session, arg);
1386
thd->restore_active_arena(arena, &backup);
1404
1421
/**********************************************/
1406
1423
Item_field::Item_field(Field *f)
1407
:Item_ident(0, NullS, *f->table_name, f->field_name),
1424
:Item_ident(0, NULL, *f->table_name, f->field_name),
1408
1425
item_equal(0), no_const_subst(0),
1409
1426
have_privileges(0), any_privileges(0)
1424
1441
Item_field (this is important in prepared statements).
1427
Item_field::Item_field(THD *thd __attribute__((unused)),
1428
Name_resolution_context *context_arg,
1444
Item_field::Item_field(Session *, Name_resolution_context *context_arg,
1430
1446
:Item_ident(context_arg, f->table->s->db.str, *f->table_name, f->field_name),
1431
1447
item_equal(0), no_const_subst(0),
1442
1458
field(0), result_field(0), item_equal(0), no_const_subst(0),
1443
1459
have_privileges(0), any_privileges(0)
1445
SELECT_LEX *select= current_thd->lex->current_select;
1461
SELECT_LEX *select= current_session->lex->current_select;
1446
1462
collation.set(DERIVATION_IMPLICIT);
1447
1463
if (select && select->parsing_place != IN_HAVING)
1448
1464
select->select_n_where_fields++;
1452
1468
Constructor need to process subselect with temporary tables (see Item)
1455
Item_field::Item_field(THD *thd, Item_field *item)
1456
:Item_ident(thd, item),
1471
Item_field::Item_field(Session *session, Item_field *item)
1472
:Item_ident(session, item),
1457
1473
field(item->field),
1458
1474
result_field(item->result_field),
1459
1475
item_equal(item->item_equal),
1505
1521
tmp=(char*) sql_alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
1506
1522
(uint) strlen(field_name)+3);
1507
strxmov(tmp,db_name,".",table_name,".",field_name,NullS);
1523
strxmov(tmp,db_name,".",table_name,".",field_name,NULL);
1513
1529
tmp= (char*) sql_alloc((uint) strlen(table_name) +
1514
1530
(uint) strlen(field_name) + 2);
1515
strxmov(tmp, table_name, ".", field_name, NullS);
1531
strxmov(tmp, table_name, ".", field_name, NULL);
1518
1534
tmp= (char*) field_name;
1523
1539
void Item_ident::print(String *str,
1524
enum_query_type query_type __attribute__((unused)))
1526
THD *thd= current_thd;
1542
Session *session= current_session;
1527
1543
char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME];
1528
1544
const char *d_name= db_name, *t_name= table_name;
1529
1545
if (lower_case_table_names== 1 ||
1532
1548
if (table_name && table_name[0])
1534
stpcpy(t_name_buff, table_name);
1550
my_stpcpy(t_name_buff, table_name);
1535
1551
my_casedn_str(files_charset_info, t_name_buff);
1536
1552
t_name= t_name_buff;
1538
1554
if (db_name && db_name[0])
1540
stpcpy(d_name_buff, db_name);
1556
my_stpcpy(d_name_buff, db_name);
1541
1557
my_casedn_str(files_charset_info, d_name_buff);
1542
1558
d_name= d_name_buff;
1548
1564
const char *nm= (field_name && field_name[0]) ?
1549
1565
field_name : name ? name : "tmp_field";
1550
append_identifier(thd, str, nm, (uint) strlen(nm));
1566
append_identifier(session, str, nm, (uint) strlen(nm));
1553
1569
if (db_name && db_name[0] && !alias_name_used)
1556
append_identifier(thd, str, d_name, (uint)strlen(d_name));
1572
append_identifier(session, str, d_name, (uint)strlen(d_name));
1557
1573
str->append('.');
1559
append_identifier(thd, str, t_name, (uint)strlen(t_name));
1575
append_identifier(session, str, t_name, (uint)strlen(t_name));
1560
1576
str->append('.');
1561
append_identifier(thd, str, field_name, (uint)strlen(field_name));
1577
append_identifier(session, str, field_name, (uint)strlen(field_name));
1565
1581
if (table_name[0])
1567
append_identifier(thd, str, t_name, (uint) strlen(t_name));
1583
append_identifier(session, str, t_name, (uint) strlen(t_name));
1568
1584
str->append('.');
1569
append_identifier(thd, str, field_name, (uint) strlen(field_name));
1585
append_identifier(session, str, field_name, (uint) strlen(field_name));
1572
append_identifier(thd, str, field_name, (uint) strlen(field_name));
1588
append_identifier(session, str, field_name, (uint) strlen(field_name));
1754
Item *Item_field::get_tmp_table_item(THD *thd)
1768
Item *Item_field::get_tmp_table_item(Session *session)
1756
Item_field *new_item= new Item_field(thd, this);
1770
Item_field *new_item= new Item_field(session, this);
1758
1772
new_item->field= new_item->result_field;
1759
1773
return new_item;
1762
int64_t Item_field::val_int_endpoint(bool left_endp __attribute__((unused)),
1763
bool *incl_endp __attribute__((unused)))
1776
int64_t Item_field::val_int_endpoint(bool, bool *)
1765
1778
int64_t res= val_int();
1766
1779
return null_value? INT64_MIN : res;
1809
Item_uint::Item_uint(const char *str_arg, uint length):
1821
Item_uint::Item_uint(const char *str_arg, uint32_t length):
1810
1822
Item_int(str_arg, length)
1812
1824
unsigned_flag= 1;
1816
Item_uint::Item_uint(const char *str_arg, int64_t i, uint length):
1828
Item_uint::Item_uint(const char *str_arg, int64_t i, uint32_t length):
1817
1829
Item_int(str_arg, i, length)
1819
1831
unsigned_flag= 1;
1841
Item_decimal::Item_decimal(const char *str_arg, uint length,
1852
Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
1842
1853
const CHARSET_INFO * const charset)
1844
1855
str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
1862
Item_decimal::Item_decimal(double val,
1863
int precision __attribute__((unused)),
1864
int scale __attribute__((unused)))
1873
Item_decimal::Item_decimal(double val, int, int)
1866
1875
double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
1867
1876
decimals= (uint8_t) decimal_value.frac;
1895
Item_decimal::Item_decimal(const uchar *bin, int precision, int scale)
1904
Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
1897
1906
binary2my_decimal(E_DEC_FATAL_ERROR, bin,
1898
1907
&decimal_value, precision, scale);
1927
void Item_decimal::print(String *str,
1928
enum_query_type query_type __attribute__((unused)))
1936
void Item_decimal::print(String *str, enum_query_type)
1930
1938
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
1931
1939
str->append(str_value);
1935
bool Item_decimal::eq(const Item *item,
1936
bool binary_cmp __attribute__((unused))) const
1943
bool Item_decimal::eq(const Item *item, bool) const
1938
1945
if (type() == item->type() && item->basic_const_item())
2033
2054
We can use str_value.ptr() here as Item_string is gurantee to put an
2036
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2057
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2037
2058
ER_TRUNCATED_WRONG_VALUE,
2038
2059
ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
2039
2060
str_value.ptr());
2064
2085
(end != org_end && !check_if_only_end_space(cs, end, org_end)))
2066
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2087
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2067
2088
ER_TRUNCATED_WRONG_VALUE,
2068
2089
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
2069
2090
str_value.ptr());
2129
default_set_param_func(Item_param *param,
2130
uchar **pos __attribute__((unused)),
2131
ulong len __attribute__((unused)))
2149
default_set_param_func(Item_param *param, unsigned char **, ulong)
2133
2151
param->set_null();
2137
Item_param::Item_param(uint pos_in_query_arg) :
2155
Item_param::Item_param(uint32_t pos_in_query_arg) :
2138
2156
state(NO_VALUE),
2139
2157
item_result_type(STRING_RESULT),
2140
2158
/* Don't pretend to be a literal unless value for this item is set. */
2245
2264
value.time.minute > 59 || value.time.second > 59)
2247
2266
char buff[MAX_DATE_STRING_REP_LENGTH];
2248
uint length= my_TIME_to_str(&value.time, buff);
2249
make_truncated_value_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2267
uint32_t length= my_TIME_to_str(&value.time, buff);
2268
make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2250
2269
buff, length, time_type, 0);
2251
2270
set_zero_time(&value.time, DRIZZLE_TIMESTAMP_ERROR);
2682
2701
if (value.cs_info.final_character_set_of_str_value !=
2683
2702
value.cs_info.character_set_of_placeholder)
2685
rc= thd->convert_string(&str_value,
2704
rc= session->convert_string(&str_value,
2686
2705
value.cs_info.character_set_of_placeholder,
2687
2706
value.cs_info.final_character_set_of_str_value);
2921
2938
table_name, (table_name [0] ? "." : ""),
2922
2939
resolved_item->field_name,
2923
2940
current->select_number, last->select_number);
2924
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2941
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2925
2942
ER_WARN_FIELD_RESOLVED, warn_buff);
2985
3002
prev_subselect_item->used_tables_cache|=
2986
3003
found_field->table->map;
2987
3004
prev_subselect_item->const_item_cache= 0;
2988
mark_as_dependent(thd, last_select, current_sel, resolved_item,
3005
mark_as_dependent(session, last_select, current_sel, resolved_item,
3137
resolve_ref_in_select_and_group(THD *thd, Item_ident *ref, SELECT_LEX *select)
3154
resolve_ref_in_select_and_group(Session *session, Item_ident *ref, SELECT_LEX *select)
3139
3156
Item **group_by_ref= NULL;
3140
3157
Item **select_ref= NULL;
3141
3158
order_st *group_list= (order_st*) select->group_list.first;
3142
3159
bool ambiguous_fields= false;
3144
3161
enum_resolution_type resolution;
3164
3181
!((*group_by_ref)->eq(*select_ref, 0)))
3166
3183
ambiguous_fields= true;
3167
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
3184
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
3168
3185
ER(ER_NON_UNIQ_ERROR), ref->full_name(),
3169
current_thd->where);
3186
current_session->where);
3207
3224
current select as dependent. The found reference of field should be
3208
3225
provided in 'from_field'.
3210
@param[in] thd current thread
3227
@param[in] session current thread
3211
3228
@param[in,out] from_field found field reference or (Field*)not_found_field
3212
3229
@param[in,out] reference view column if this item was resolved to a
3282
3299
the found view field into '*reference', in other words, it
3283
3300
substitutes this Item_field with the found expression.
3285
if (field_found || (*from_field= find_field_in_tables(thd, this,
3302
if (field_found || (*from_field= find_field_in_tables(session, this,
3286
3303
outer_context->
3287
3304
first_name_resolution_table,
3288
3305
outer_context->
3317
3334
if (!(rf= new Item_outer_ref(context, this)))
3319
thd->change_item_tree(reference, rf);
3336
session->change_item_tree(reference, rf);
3320
3337
select->inner_refs_list.push_back(rf);
3321
rf->in_sum_func= thd->lex->in_sum_func;
3338
rf->in_sum_func= session->lex->in_sum_func;
3324
3341
A reference is resolved to a nest level that's outer or the same as
3325
3342
the nest level of the enclosing set function : adjust the value of
3326
3343
max_arg_level for the function if it's needed.
3328
if (thd->lex->in_sum_func &&
3329
thd->lex->in_sum_func->nest_level >= select->nest_level)
3345
if (session->lex->in_sum_func &&
3346
session->lex->in_sum_func->nest_level >= select->nest_level)
3331
3348
Item::Type ref_type= (*reference)->type();
3332
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
3349
set_if_bigger(session->lex->in_sum_func->max_arg_level,
3333
3350
select->nest_level);
3334
3351
set_field(*from_field);
3336
mark_as_dependent(thd, last_checked_context->select_lex,
3353
mark_as_dependent(session, last_checked_context->select_lex,
3337
3354
context->select_lex, this,
3338
3355
((ref_type == REF_ITEM ||
3339
3356
ref_type == FIELD_ITEM) ?
3348
3365
(*reference)->used_tables();
3349
3366
prev_subselect_item->const_item_cache&=
3350
3367
(*reference)->const_item();
3351
mark_as_dependent(thd, last_checked_context->select_lex,
3368
mark_as_dependent(session, last_checked_context->select_lex,
3352
3369
context->select_lex, this,
3353
3370
((ref_type == REF_ITEM || ref_type == FIELD_ITEM) ?
3354
3371
(Item_ident*) (*reference) :
3368
3385
/* Search in SELECT and GROUP lists of the outer select. */
3369
3386
if (place != IN_WHERE && place != IN_ON)
3371
if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
3388
if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
3372
3389
return -1; /* Some error occurred (e.g. ambiguous names). */
3373
3390
if (ref != not_found_item)
3396
3413
if (upward_lookup)
3398
3415
// We can't say exactly what absent table or field
3399
my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), thd->where);
3416
my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), session->where);
3403
3420
/* Call find_field_in_tables only to report the error */
3404
find_field_in_tables(thd, this,
3421
find_field_in_tables(session, this,
3405
3422
context->first_name_resolution_table,
3406
3423
context->last_name_resolution_table,
3407
3424
reference, REPORT_ALL_ERRORS,
3440
3457
if (place != IN_HAVING && select->group_list.elements)
3442
3459
outer_context->select_lex->inner_refs_list.push_back((Item_outer_ref*)rf);
3443
((Item_outer_ref*)rf)->in_sum_func= thd->lex->in_sum_func;
3460
((Item_outer_ref*)rf)->in_sum_func= session->lex->in_sum_func;
3445
thd->change_item_tree(reference, rf);
3462
session->change_item_tree(reference, rf);
3447
3464
rf is Item_ref => never substitute other items (in this case)
3448
3465
during fix_fields() => we can use rf after fix_fields()
3450
3467
assert(!rf->fixed); // Assured by Item_ref()
3451
if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3468
if (rf->fix_fields(session, reference) || rf->check_cols(1))
3454
mark_as_dependent(thd, last_checked_context->select_lex,
3471
mark_as_dependent(session, last_checked_context->select_lex,
3455
3472
context->select_lex, this,
3461
mark_as_dependent(thd, last_checked_context->select_lex,
3478
mark_as_dependent(session, last_checked_context->select_lex,
3462
3479
context->select_lex,
3463
3480
this, (Item_ident*)*reference);
3464
3481
if (last_checked_context->select_lex->having_fix_field)
3469
3486
(char*) cached_table->alias, (char*) field_name);
3472
thd->change_item_tree(reference, rf);
3489
session->change_item_tree(reference, rf);
3474
3491
rf is Item_ref => never substitute other items (in this case)
3475
3492
during fix_fields() => we can use rf after fix_fields()
3477
3494
assert(!rf->fixed); // Assured by Item_ref()
3478
if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3495
if (rf->fix_fields(session, reference) || rf->check_cols(1))
3542
3559
expression to 'reference', i.e. it substitute that expression instead
3543
3560
of this Item_field
3545
if ((from_field= find_field_in_tables(thd, this,
3562
if ((from_field= find_field_in_tables(session, this,
3546
3563
context->first_name_resolution_table,
3547
3564
context->last_name_resolution_table,
3549
thd->lex->use_only_table_context ?
3566
session->lex->use_only_table_context ?
3550
3567
REPORT_ALL_ERRORS :
3551
3568
IGNORE_EXCEPT_NON_UNIQUE,
3552
3569
!any_privileges,
3557
3574
/* Look up in current select's item_list to find aliased fields */
3558
if (thd->lex->current_select->is_item_list_lookup)
3575
if (session->lex->current_select->is_item_list_lookup)
3561
3578
enum_resolution_type resolution;
3562
Item** res= find_item_in_list(this, thd->lex->current_select->item_list,
3579
Item** res= find_item_in_list(this, session->lex->current_select->item_list,
3563
3580
&counter, REPORT_EXCEPT_NOT_FOUND,
3600
3617
Item_ref *rf= new Item_ref(context, db_name,table_name,field_name);
3603
thd->change_item_tree(reference, rf);
3620
session->change_item_tree(reference, rf);
3605
3622
Because Item_ref never substitutes itself with other items
3606
3623
in Item_ref::fix_fields(), we can safely use the original
3607
3624
pointer to it even after fix_fields()
3609
return rf->fix_fields(thd, reference) || rf->check_cols(1);
3626
return rf->fix_fields(session, reference) || rf->check_cols(1);
3613
if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3630
if ((ret= fix_outer_field(session, &from_field, reference)) < 0)
3615
3632
outer_fixed= true;
3649
3666
set_field(from_field);
3650
if (thd->lex->in_sum_func &&
3651
thd->lex->in_sum_func->nest_level ==
3652
thd->lex->current_select->nest_level)
3653
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
3654
thd->lex->current_select->nest_level);
3667
if (session->lex->in_sum_func &&
3668
session->lex->in_sum_func->nest_level ==
3669
session->lex->current_select->nest_level)
3670
set_if_bigger(session->lex->in_sum_func->max_arg_level,
3671
session->lex->current_select->nest_level);
3656
else if (thd->mark_used_columns != MARK_COLUMNS_NONE)
3673
else if (session->mark_used_columns != MARK_COLUMNS_NONE)
3658
3675
Table *table= field->table;
3659
3676
MY_BITMAP *current_bitmap, *other_bitmap;
3660
if (thd->mark_used_columns == MARK_COLUMNS_READ)
3677
if (session->mark_used_columns == MARK_COLUMNS_READ)
3662
3679
current_bitmap= table->read_set;
3663
3680
other_bitmap= table->write_set;
3969
3984
/* Check whether we got a well-formed string */
3970
3985
const CHARSET_INFO * const cs= str->charset();
3971
3986
int well_formed_error;
3972
uint wlen= cs->cset->well_formed_len(cs,
3987
uint32_t wlen= cs->cset->well_formed_len(cs,
3973
3988
str->ptr(), str->ptr() + str->length(),
3974
3989
str->length(), &well_formed_error);
3975
3990
if (wlen < str->length())
3977
THD *thd= current_thd;
3992
Session *session= current_session;
3978
3993
char hexbuf[7];
3979
3994
enum DRIZZLE_ERROR::enum_warning_level level;
3980
uint diff= str->length() - wlen;
3995
uint32_t diff= str->length() - wlen;
3981
3996
set_if_smaller(diff, 3);
3982
3997
octet2hex(hexbuf, str->ptr() + wlen, diff);
3983
3998
if (send_error)
4080
4095
\# Created field
4083
Field *Item::tmp_table_field_from_field_type(Table *table, bool fixed_length __attribute__((unused)))
4098
Field *Item::tmp_table_field_from_field_type(Table *table, bool)
4086
4101
The field functions defines a field to be not null if null_ptr is not 0
4088
uchar *null_ptr= maybe_null ? (uchar*) "" : 0;
4103
unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
4091
4106
switch (field_type()) {
4092
4107
case DRIZZLE_TYPE_NEWDECIMAL:
4093
field= new Field_new_decimal((uchar*) 0, max_length, null_ptr, 0,
4108
field= new Field_new_decimal((unsigned char*) 0, max_length, null_ptr, 0,
4094
4109
Field::NONE, name, decimals, 0,
4095
4110
unsigned_flag);
4097
case DRIZZLE_TYPE_TINY:
4098
field= new Field_tiny((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4099
name, 0, unsigned_flag);
4101
case DRIZZLE_TYPE_SHORT:
4102
field= new Field_short((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4103
name, 0, unsigned_flag);
4105
4112
case DRIZZLE_TYPE_LONG:
4106
field= new Field_long((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4113
field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
4107
4114
name, 0, unsigned_flag);
4109
4116
case DRIZZLE_TYPE_LONGLONG:
4110
field= new Field_int64_t((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4117
field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
4111
4118
name, 0, unsigned_flag);
4113
4120
case DRIZZLE_TYPE_DOUBLE:
4114
field= new Field_double((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4121
field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
4115
4122
name, decimals, 0, unsigned_flag);
4117
4124
case DRIZZLE_TYPE_NULL:
4118
field= new Field_null((uchar*) 0, max_length, Field::NONE,
4125
field= new Field_null((unsigned char*) 0, max_length, Field::NONE,
4119
4126
name, &my_charset_bin);
4121
4128
case DRIZZLE_TYPE_NEWDATE:
4335
int Item_decimal::save_in_field(Field *field,
4336
bool no_conversions __attribute__((unused)))
4340
int Item_decimal::save_in_field(Field *field, bool)
4338
4342
field->set_notnull();
4339
4343
return field->store_decimal(&decimal_value);
4343
bool Item_int::eq(const Item *arg,
4344
bool binary_cmp __attribute__((unused))) const
4347
bool Item_int::eq(const Item *arg, bool) const
4346
4349
/* No need to check for null value as basic constant can't be NULL */
4347
4350
if (arg->basic_const_item() && arg->type() == type())
4509
4509
// following assert is redundant, because fixed=1 assigned in constructor
4510
4510
assert(fixed == 1);
4511
4511
char *end=(char*) str_value.ptr()+str_value.length(),
4512
*ptr=end-min(str_value.length(),(uint32_t)sizeof(int64_t));
4512
*ptr=end-cmin(str_value.length(),(uint32_t)sizeof(int64_t));
4514
4514
uint64_t value=0;
4515
4515
for (; ptr != end ; ptr++)
4516
value=(value << 8)+ (uint64_t) (uchar) *ptr;
4516
value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
4517
4517
return (int64_t) value;
4531
int Item_hex_string::save_in_field(Field *field,
4532
bool no_conversions __attribute__((unused)))
4531
int Item_hex_string::save_in_field(Field *field, bool)
4534
4533
field->set_notnull();
4535
4534
if (field->result_type() == STRING_RESULT)
4536
return field->store(str_value.ptr(), str_value.length(),
4535
return field->store(str_value.ptr(), str_value.length(),
4537
4536
collation.collation);
4562
void Item_hex_string::print(String *str,
4563
enum_query_type query_type __attribute__((unused)))
4561
void Item_hex_string::print(String *str, enum_query_type)
4565
4563
char *end= (char*) str_value.ptr() + str_value.length(),
4566
*ptr= end - min(str_value.length(), (uint32_t)sizeof(int64_t));
4564
*ptr= end - cmin(str_value.length(), (uint32_t)sizeof(int64_t));
4567
4565
str->append("0x");
4568
4566
for (; ptr != end ; ptr++)
4570
str->append(_dig_vec_lower[((uchar) *ptr) >> 4]);
4571
str->append(_dig_vec_lower[((uchar) *ptr) & 0x0F]);
4568
str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
4569
str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
4604
4602
In number context this is a int64_t value.
4607
Item_bin_string::Item_bin_string(const char *str, uint str_length)
4605
Item_bin_string::Item_bin_string(const char *str, uint32_t str_length)
4609
4607
const char *end= str + str_length - 1;
4608
unsigned char bits= 0;
4613
4611
max_length= (str_length + 7) >> 3;
4614
4612
char *ptr= (char*) sql_alloc(max_length + 1);
4748
4729
need to set no_errors to prevent warnings about type conversion
4751
THD *thd= field->table->in_use;
4732
Session *session= field->table->in_use;
4754
no_errors= thd->no_errors;
4735
no_errors= session->no_errors;
4736
session->no_errors= 1;
4756
4737
Item::update_null_value();
4757
thd->no_errors= no_errors;
4738
session->no_errors= no_errors;
4822
4803
Item **item, const char *table_name_arg,
4823
4804
const char *field_name_arg,
4824
4805
bool alias_name_used_arg)
4825
:Item_ident(context_arg, NullS, table_name_arg, field_name_arg),
4806
:Item_ident(context_arg, NULL, table_name_arg, field_name_arg),
4826
4807
result_field(0), ref(item)
4828
4809
alias_name_used= alias_name_used_arg;
4898
4879
false on success
4901
bool Item_ref::fix_fields(THD *thd, Item **reference)
4882
bool Item_ref::fix_fields(Session *session, Item **reference)
4903
4884
enum_parsing_place place= NO_MATTER;
4904
4885
assert(fixed == 0);
4905
SELECT_LEX *current_sel= thd->lex->current_select;
4886
SELECT_LEX *current_sel= session->lex->current_select;
4907
4888
if (!ref || ref == not_found_item)
4909
if (!(ref= resolve_ref_in_select_and_group(thd, this,
4890
if (!(ref= resolve_ref_in_select_and_group(session, this,
4910
4891
context->select_lex)))
4911
4892
goto error; /* Some error occurred (e.g. ambiguous names). */
4946
4927
/* Search in the SELECT and GROUP lists of the outer select. */
4947
4928
if (outer_context->resolve_in_select_list)
4949
if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
4930
if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
4950
4931
goto error; /* Some error occurred (e.g. ambiguous names). */
4951
4932
if (ref != not_found_item)
4983
4964
field expression to 'reference', i.e. it substitute that
4984
4965
expression instead of this Item_ref
4986
from_field= find_field_in_tables(thd, this,
4967
from_field= find_field_in_tables(session, this,
4987
4968
outer_context->
4988
4969
first_name_resolution_table,
4989
4970
outer_context->
5001
4982
prev_subselect_item->const_item_cache&=
5002
4983
(*reference)->const_item();
5003
4984
assert((*reference)->type() == REF_ITEM);
5004
mark_as_dependent(thd, last_checked_context->select_lex,
4985
mark_as_dependent(session, last_checked_context->select_lex,
5005
4986
context->select_lex, this,
5006
4987
((refer_type == REF_ITEM ||
5007
4988
refer_type == FIELD_ITEM) ?
5054
5035
Item_field* fld;
5055
5036
if (!(fld= new Item_field(from_field)))
5057
thd->change_item_tree(reference, fld);
5058
mark_as_dependent(thd, last_checked_context->select_lex,
5059
thd->lex->current_select, this, fld);
5038
session->change_item_tree(reference, fld);
5039
mark_as_dependent(session, last_checked_context->select_lex,
5040
session->lex->current_select, this, fld);
5061
5042
A reference is resolved to a nest level that's outer or the same as
5062
5043
the nest level of the enclosing set function : adjust the value of
5063
5044
max_arg_level for the function if it's needed.
5065
if (thd->lex->in_sum_func &&
5066
thd->lex->in_sum_func->nest_level >=
5046
if (session->lex->in_sum_func &&
5047
session->lex->in_sum_func->nest_level >=
5067
5048
last_checked_context->select_lex->nest_level)
5068
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5049
set_if_bigger(session->lex->in_sum_func->max_arg_level,
5069
5050
last_checked_context->select_lex->nest_level);
5074
5055
/* The item was not a table field and not a reference */
5075
5056
my_error(ER_BAD_FIELD_ERROR, MYF(0),
5076
this->full_name(), current_thd->where);
5057
this->full_name(), current_session->where);
5079
5060
/* Should be checked in resolve_ref_in_select_and_group(). */
5080
5061
assert(*ref && (*ref)->fixed);
5081
mark_as_dependent(thd, last_checked_context->select_lex,
5062
mark_as_dependent(session, last_checked_context->select_lex,
5082
5063
context->select_lex, this, this);
5084
5065
A reference is resolved to a nest level that's outer or the same as
5085
5066
the nest level of the enclosing set function : adjust the value of
5086
5067
max_arg_level for the function if it's needed.
5088
if (thd->lex->in_sum_func &&
5089
thd->lex->in_sum_func->nest_level >=
5069
if (session->lex->in_sum_func &&
5070
session->lex->in_sum_func->nest_level >=
5090
5071
last_checked_context->select_lex->nest_level)
5091
set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5072
set_if_bigger(session->lex->in_sum_func->max_arg_level,
5092
5073
last_checked_context->select_lex->nest_level);
5163
5144
if ((*ref)->type() != Item::CACHE_ITEM && ref_type() != VIEW_REF &&
5164
5145
!table_name && name && alias_name_used)
5166
THD *thd= current_thd;
5167
append_identifier(thd, str, name, (uint) strlen(name));
5147
Session *session= current_session;
5148
append_identifier(session, str, name, (uint) strlen(name));
5170
5151
(*ref)->print(str, query_type);
5438
bool Item_direct_view_ref::fix_fields(THD *thd, Item **reference)
5419
bool Item_direct_view_ref::fix_fields(Session *session, Item **reference)
5440
5421
/* view fild reference must be defined */
5442
5423
/* (*ref)->check_cols() will be made in Item_direct_ref::fix_fields */
5443
5424
if (!(*ref)->fixed &&
5444
((*ref)->fix_fields(thd, ref)))
5425
((*ref)->fix_fields(session, ref)))
5446
return Item_direct_ref::fix_fields(thd, reference);
5427
return Item_direct_ref::fix_fields(session, reference);
5462
bool Item_outer_ref::fix_fields(THD *thd, Item **reference)
5443
bool Item_outer_ref::fix_fields(Session *session, Item **reference)
5465
5446
/* outer_ref->check_cols() will be made in Item_direct_ref::fix_fields */
5466
if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(thd, reference)))
5447
if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(session, reference)))
5468
err= Item_direct_ref::fix_fields(thd, reference);
5449
err= Item_direct_ref::fix_fields(session, reference);
5469
5450
if (!outer_ref)
5470
5451
outer_ref= *ref;
5471
5452
if ((*ref)->type() == Item::FIELD_ITEM)
5623
5601
same time it can replace some nodes in the tree.
5626
Item *Item_default_value::transform(Item_transformer transformer, uchar *args)
5604
Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
5628
5606
Item *new_item= arg->transform(transformer, args);
5633
THD::change_item_tree() should be called only if the tree was
5611
Session::change_item_tree() should be called only if the tree was
5634
5612
really transformed, i.e. when a new item has been created.
5635
5613
Otherwise we'll be allocating a lot of unnecessary memory for
5636
5614
change records at each execution.
5638
5616
if (arg != new_item)
5639
current_thd->change_item_tree(&arg, new_item);
5617
current_session->change_item_tree(&arg, new_item);
5640
5618
return (this->*transformer)(args);
5799
5776
case REAL_RESULT:
5800
5777
{ // It must REAL_RESULT
5801
5778
double result= item->val_real();
5802
uint length=item->max_length,decimals=item->decimals;
5779
uint32_t length=item->max_length,decimals=item->decimals;
5803
5780
bool null_value=item->null_value;
5804
5781
new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
5805
5782
new Item_float(name, result, decimals, length));
5810
5787
my_decimal decimal_value;
5811
5788
my_decimal *result= item->val_decimal(&decimal_value);
5812
uint length= item->max_length, decimals= item->decimals;
5789
uint32_t length= item->max_length, decimals= item->decimals;
5813
5790
bool null_value= item->null_value;
5814
5791
new_item= (null_value ?
5815
5792
(Item*) new Item_null(name) :
6142
6119
void Item_cache_row::bring_value()
6144
for (uint i= 0; i < item_count; i++)
6121
for (uint32_t i= 0; i < item_count; i++)
6145
6122
values[i]->bring_value();
6150
Item_type_holder::Item_type_holder(THD *thd, Item *item)
6151
:Item(thd, item), enum_set_typelib(0), fld_type(get_real_type(item))
6127
Item_type_holder::Item_type_holder(Session *session, Item *item)
6128
:Item(session, item), enum_set_typelib(0), fld_type(get_real_type(item))
6153
6130
assert(item->fixed);
6154
6131
maybe_null= item->maybe_null;
6255
bool Item_type_holder::join_types(THD *thd __attribute__((unused)),
6232
bool Item_type_holder::join_types(Session *, Item *item)
6258
uint max_length_orig= max_length;
6259
uint decimals_orig= decimals;
6234
uint32_t max_length_orig= max_length;
6235
uint32_t decimals_orig= decimals;
6260
6236
fld_type= Field::field_type_merge(fld_type, get_real_type(item));
6262
6238
int item_decimals= item->decimals;
6263
6239
/* fix variable decimals which always is NOT_FIXED_DEC */
6264
6240
if (Field::result_merge_type(fld_type) == INT_RESULT)
6265
6241
item_decimals= 0;
6266
decimals= max((int)decimals, item_decimals);
6242
decimals= cmax((int)decimals, item_decimals);
6268
6244
if (Field::result_merge_type(fld_type) == DECIMAL_RESULT)
6270
decimals= min((int)max(decimals, item->decimals), DECIMAL_MAX_SCALE);
6271
int precision= min(max(prev_decimal_int_part, item->decimal_int_part())
6246
decimals= cmin((int)cmax(decimals, item->decimals), DECIMAL_MAX_SCALE);
6247
int precision= cmin(cmax(prev_decimal_int_part, item->decimal_int_part())
6272
6248
+ decimals, DECIMAL_MAX_PRECISION);
6273
6249
unsigned_flag&= item->unsigned_flag;
6274
6250
max_length= my_decimal_precision_to_length(precision, decimals,
6395
6368
The field functions defines a field to be not null if null_ptr is not 0
6397
uchar *null_ptr= maybe_null ? (uchar*) "" : 0;
6370
unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
6400
6373
switch (fld_type) {
6401
6374
case DRIZZLE_TYPE_ENUM:
6402
6375
assert(enum_set_typelib);
6403
field= new Field_enum((uchar *) 0, max_length, null_ptr, 0,
6376
field= new Field_enum((unsigned char *) 0, max_length, null_ptr, 0,
6404
6377
Field::NONE, name,
6405
6378
get_enum_pack_length(enum_set_typelib->count),
6406
6379
enum_set_typelib, collation.collation);