204
192
return decimal_value;
208
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
195
type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
212
if (get_date(<ime, TIME_FUZZY_DATE))
199
if (get_date(ltime, TIME_FUZZY_DATE))
214
my_decimal_set_zero(decimal_value);
201
decimal_value->set_zero();
215
202
null_value= 1; // set NULL, stop processing
218
return date2my_decimal(<ime, decimal_value);
205
return date2_class_decimal(<ime, decimal_value);
222
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
208
type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
226
if (get_time(<ime))
228
my_decimal_set_zero(decimal_value);
214
decimal_value->set_zero();
231
return date2my_decimal(<ime, decimal_value);
217
return date2_class_decimal(<ime, decimal_value);
235
220
double Item::val_real_from_decimal()
237
222
/* Note that fix_fields may not be called for Item_avg_field items */
239
my_decimal value_buff, *dec_val= val_decimal(&value_buff);
224
type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
242
my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
227
class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
247
231
int64_t Item::val_int_from_decimal()
249
233
/* Note that fix_fields may not be called for Item_avg_field items */
251
my_decimal value, *dec_val= val_decimal(&value);
235
type::Decimal value, *dec_val= val_decimal(&value);
254
my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
239
dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
258
int Item::save_time_in_field(Field *field)
261
if (get_time(<ime))
262
return set_field_to_null(field);
263
field->set_notnull();
264
return field->store_time(<ime, DRIZZLE_TIMESTAMP_TIME);
268
int Item::save_date_in_field(Field *field)
271
if (get_date(<ime, TIME_FUZZY_DATE))
272
return set_field_to_null(field);
273
field->set_notnull();
274
return field->store_time(<ime, DRIZZLE_TIMESTAMP_DATETIME);
279
Store the string value in field directly
282
Item::save_str_value_in_field()
283
field a pointer to field where to store
284
result the pointer to the string value to be stored
287
The method is used by Item_*::save_in_field implementations
288
when we don't need to calculate the value to store
289
See Item_string::save_in_field() implementation for example
292
Check if the Item is null and stores the NULL or the
293
result value in the field accordingly.
296
Nonzero value if error
244
bool Item::save_time_in_field(Field *field)
249
return set_field_to_null(field);
251
field->set_notnull();
253
return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
256
bool Item::save_date_in_field(Field *field)
260
if (get_date(ltime, TIME_FUZZY_DATE))
261
return set_field_to_null(field);
263
field->set_notnull();
265
return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
269
* Check if the Item is null and stores the NULL or the
270
* result value in the field accordingly.
299
272
int Item::save_str_value_in_field(Field *field, String *result)
302
275
return set_field_to_null(field);
303
277
field->set_notnull();
304
return field->store(result->ptr(), result->length(),
305
collation.collation);
279
return field->store(result->ptr(), result->length(), collation.collation);
310
is_expensive_cache(-1), rsize(0), name(0), orig_name(0), name_length(0),
311
fixed(0), is_autogenerated_name(true),
312
collation(&my_charset_bin, DERIVATION_COERCIBLE)
283
is_expensive_cache(-1),
292
unsigned_flag(false),
293
with_sum_func(false),
294
is_autogenerated_name(true),
295
with_subselect(false),
296
collation(&my_charset_bin, DERIVATION_COERCIBLE),
297
_session(*current_session)
317
with_sum_func= false;
318
unsigned_flag= false;
322
299
cmp_context= (Item_result)-1;
324
301
/* Put item in free list so that we can free all items at end */
325
Session *session= current_session;
326
next= session->free_list;
327
session->free_list= this;
302
next= getSession().free_list;
303
getSession().free_list= this;
329
306
Item constructor can be called during execution other then SQL_COM
330
command => we should check session->lex->current_select on zero (session->lex
307
command => we should check session->lex().current_select on zero (session->lex
331
308
can be uninitialised)
333
if (session->lex->current_select)
310
if (getSession().lex().current_select)
335
enum_parsing_place place=
336
session->lex->current_select->parsing_place;
337
if (place == SELECT_LIST ||
339
session->lex->current_select->select_n_having_items++;
312
enum_parsing_place place= getSession().lex().current_select->parsing_place;
313
if (place == SELECT_LIST || place == IN_HAVING)
314
getSession().lex().current_select->select_n_having_items++;
344
Constructor used by Item_field, Item_ref & aggregate (sum)
347
Used for duplicating lists in processing queries with temporary
350
318
Item::Item(Session *session, Item *item):
351
319
is_expensive_cache(-1),
353
320
str_value(item->str_value),
354
321
name(item->name),
355
orig_name(item->orig_name),
322
name_length(item->name_length),
356
323
max_length(item->max_length),
357
324
marker(item->marker),
358
325
decimals(item->decimals),
359
327
maybe_null(item->maybe_null),
360
328
null_value(item->null_value),
361
329
unsigned_flag(item->unsigned_flag),
362
330
with_sum_func(item->with_sum_func),
331
is_autogenerated_name(item->is_autogenerated_name),
332
with_subselect(item->with_subselect),
364
333
collation(item->collation),
365
cmp_context(item->cmp_context)
334
cmp_context(item->cmp_context),
367
next= session->free_list; // Put in free list
368
session->free_list= this;
337
/* Put this item in the session's free list */
338
next= getSession().free_list;
339
getSession().free_list= this;
342
uint32_t Item::float_length(uint32_t decimals_par) const
344
return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;
372
347
uint32_t Item::decimal_precision() const
374
349
Item_result restype= result_type();
376
351
if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
377
return cmin(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
378
(unsigned int)DECIMAL_MAX_PRECISION);
379
return cmin(max_length, (uint32_t)DECIMAL_MAX_PRECISION);
352
return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
353
(uint32_t) DECIMAL_MAX_PRECISION);
354
return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
383
357
int Item::decimal_int_part() const
385
return my_decimal_int_part(decimal_precision(), decimals);
359
return class_decimal_int_part(decimal_precision(), decimals);
389
void Item::print(String *str, enum_query_type)
362
void Item::print(String *str)
391
364
str->append(full_name());
395
void Item::print_item_w_name(String *str, enum_query_type query_type)
367
void Item::print_item_w_name(String *str)
397
print(str, query_type);
401
Session *session= current_session;
402
373
str->append(STRING_WITH_LEN(" AS "));
403
append_identifier(session, str, name, (uint) strlen(name));
374
str->append_identifier(str_ref(name));
408
378
void Item::split_sum_func(Session *, Item **, List<Item> &)
412
381
void Item::cleanup()
423
cleanup() item if it is 'fixed'.
425
@param arg a dummy parameter, is not used here
428
bool Item::cleanup_processor(unsigned char *)
437
rename item (used for views, cleanup() return original name).
439
@param new_name new name of item;
442
void Item::rename(char *new_name)
445
we can compare pointers to names here, because if name was not changed,
448
if (!orig_name && new_name != name)
455
Traverse item tree possibly transforming it (replacing items).
457
This function is designed to ease transformation of Item trees.
458
Re-execution note: every such transformation is registered for
459
rollback by Session::change_item_tree() and is rolled back at the end
460
of execution by Session::rollback_item_tree_changes().
463
- this function can not be used at prepared statement prepare
464
(in particular, in fix_fields!), as only permanent
465
transformation of Item trees are allowed at prepare.
466
- the transformer function shall allocate new Items in execution
467
memory root (session->mem_root) and not anywhere else: allocated
468
items will be gone in the end of execution.
470
If you don't need to transform an item tree, but only traverse
471
it, please use Item::walk() instead.
474
@param transformer functor that performs transformation of a subtree
475
@param arg opaque argument passed to the functor
478
Returns pointer to the new subtree root. Session::change_item_tree()
479
should be called for it if transformation took place, i.e. if a
480
pointer to newly allocated item is returned.
483
387
Item* Item::transform(Item_transformer transformer, unsigned char *arg)
485
389
return (this->*transformer)(arg);
489
Item_ident::Item_ident(Name_resolution_context *context_arg,
490
const char *db_name_arg,const char *table_name_arg,
491
const char *field_name_arg)
492
:orig_db_name(db_name_arg), orig_table_name(table_name_arg),
493
orig_field_name(field_name_arg), context(context_arg),
494
db_name(db_name_arg), table_name(table_name_arg),
495
field_name(field_name_arg),
496
alias_name_used(false), cached_field_index(NO_CACHED_FIELD_INDEX),
497
cached_table(0), depended_from(0)
499
name = (char*) field_name_arg;
504
Constructor used by Item_field & Item_*_ref (see Item comment)
507
Item_ident::Item_ident(Session *session, Item_ident *item)
508
:Item(session, item),
509
orig_db_name(item->orig_db_name),
510
orig_table_name(item->orig_table_name),
511
orig_field_name(item->orig_field_name),
512
context(item->context),
513
db_name(item->db_name),
514
table_name(item->table_name),
515
field_name(item->field_name),
516
alias_name_used(item->alias_name_used),
517
cached_field_index(item->cached_field_index),
518
cached_table(item->cached_table),
519
depended_from(item->depended_from)
522
void Item_ident::cleanup()
524
#ifdef CANT_BE_USED_AS_MEMORY_IS_FREED
525
db_name ? db_name : "(null)",
526
orig_db_name ? orig_db_name : "(null)",
527
table_name ? table_name : "(null)",
528
orig_table_name ? orig_table_name : "(null)",
529
field_name ? field_name : "(null)",
530
orig_field_name ? orig_field_name : "(null)"));
533
db_name= orig_db_name;
534
table_name= orig_table_name;
535
field_name= orig_field_name;
540
bool Item_ident::remove_dependence_processor(unsigned char * arg)
542
if (depended_from == (st_select_lex *) arg)
549
Store the pointer to this item field into a list if not already there.
551
The method is used by Item::walk to collect all unique Item_field objects
552
from a tree of Items into a set of items represented as a list.
554
Item_cond::walk() and Item_func::walk() stop the evaluation of the
555
processor function for its arguments once the processor returns
556
true.Therefore in order to force this method being called for all item
557
arguments in a condition the method must return false.
559
@param arg pointer to a List<Item_field>
562
false to force the evaluation of collect_item_field_processor
563
for the subsequent items.
566
bool Item_field::collect_item_field_processor(unsigned char *arg)
568
List<Item_field> *item_list= (List<Item_field>*) arg;
569
List_iterator<Item_field> item_list_it(*item_list);
570
Item_field *curr_item;
571
while ((curr_item= item_list_it++))
573
if (curr_item->eq(this, 1))
574
return(false); /* Already in the set. */
576
item_list->push_back(this);
582
Check if an Item_field references some field from a list of fields.
584
Check whether the Item_field represented by 'this' references any
585
of the fields in the keyparts passed via 'arg'. Used with the
586
method Item::walk() to test whether any keypart in a sequence of
587
keyparts is referenced in an expression.
589
@param arg Field being compared, arg must be of type Field
592
true if 'this' references the field 'arg'
597
bool Item_field::find_item_in_field_list_processor(unsigned char *arg)
599
KEY_PART_INFO *first_non_group_part= *((KEY_PART_INFO **) arg);
600
KEY_PART_INFO *last_part= *(((KEY_PART_INFO **) arg) + 1);
601
KEY_PART_INFO *cur_part;
603
for (cur_part= first_non_group_part; cur_part != last_part; cur_part++)
605
if (field->eq(cur_part->field))
613
Mark field in read_map
616
This is used by filesort to register used fields in a a temporary
617
column read set or to register used fields in a view
620
bool Item_field::register_field_in_read_map(unsigned char *arg)
622
Table *table= (Table *) arg;
623
if (field->table == table || !table)
624
bitmap_set_bit(field->table->read_set, field->field_index);
625
if (field->vcol_info && field->vcol_info->expr_item)
626
return field->vcol_info->expr_item->walk(&Item::register_field_in_read_map,
632
Mark field in bitmap supplied as *arg
636
bool Item_field::register_field_in_bitmap(unsigned char *arg)
638
MY_BITMAP *bitmap= (MY_BITMAP *) arg;
640
bitmap_set_bit(bitmap, field->field_index);
645
392
bool Item::check_cols(uint32_t c)
649
396
my_error(ER_OPERAND_COLUMNS, MYF(0), c);
656
void Item::set_name(const char *str, uint32_t length, const CHARSET_INFO * const cs)
402
void Item::set_name(const char *str, uint32_t length, const charset_info_st * const cs)
660
406
/* Empty string, used by AS or internal function like last_insert_id() */
667
413
uint32_t orig_len= length;
669
This will probably need a better implementation in the future:
670
a function in CHARSET_INFO structure.
672
while (length && !my_isgraph(cs,*str))
673
{ // Fix problem with yacc
414
while (length && ! my_isgraph(cs, *str))
416
/* Fix problem with yacc */
677
if (orig_len != length && !is_autogenerated_name)
420
if (orig_len != length && ! is_autogenerated_name)
680
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
681
ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY),
682
str + length - orig_len);
423
push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY), str + length - orig_len);
684
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
685
ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES),
686
str + length - orig_len);
425
push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES), str + length - orig_len);
689
if (!my_charset_same(cs, system_charset_info))
692
name= sql_strmake_with_convert(str, name_length= length, cs,
693
MAX_ALIAS_NAME, system_charset_info,
697
name= sql_strmake(str, (name_length= cmin(length,(unsigned int)MAX_ALIAS_NAME)));
428
name= memory::sql_strdup(str_ref(str, length));
703
This function is called when:
704
- Comparing items in the WHERE clause (when doing where optimization)
705
- When trying to find an order_st BY/GROUP BY item in the SELECT part
708
431
bool Item::eq(const Item *item, bool) const
712
435
for all basic constants we have special checks, and Item_param's
713
436
type() can be only among basic constant types.
715
return type() == item->type() && name && item->name &&
716
!my_strcasecmp(system_charset_info,name,item->name);
438
return type() == item->type() &&
441
! my_strcasecmp(system_charset_info, name, item->name);
720
Item *Item::safe_charset_converter(const CHARSET_INFO * const tocs)
444
Item *Item::safe_charset_converter(const charset_info_st * const tocs)
722
446
Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
723
447
return conv->safe ? conv : NULL;
729
Created mostly for mysql_prepare_table(). Important
730
when a string ENUM/SET column is described with a numeric default value:
732
CREATE TABLE t1(a SET('a') DEFAULT 1);
734
We cannot use generic Item::safe_charset_converter(), because
735
the latter returns a non-fixed Item, so val_str() crashes afterwards.
736
Override Item_num method, to return a fixed item.
738
Item *Item_num::safe_charset_converter(const CHARSET_INFO * const)
742
String *s, tmp(buf, sizeof(buf), &my_charset_bin);
744
if ((conv= new Item_string(s->ptr(), s->length(), s->charset())))
746
conv->str_value.copy();
747
conv->str_value.mark_as_const();
753
Item *Item_static_float_func::safe_charset_converter(const CHARSET_INFO * const)
757
String *s, tmp(buf, sizeof(buf), &my_charset_bin);
759
if ((conv= new Item_static_string_func(func_name, s->ptr(), s->length(),
762
conv->str_value.copy();
763
conv->str_value.mark_as_const();
769
Item *Item_string::safe_charset_converter(const CHARSET_INFO * const tocs)
772
uint32_t conv_errors;
774
String tmp, cstr, *ostr= val_str(&tmp);
775
cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
776
if (conv_errors || !(conv= new Item_string(cstr.ptr(), cstr.length(),
778
collation.derivation)))
781
Safe conversion is not possible (or EOM).
782
We could not convert a string into the requested character set
783
without data loss. The target charset does not cover all the
784
characters from the string. Operation cannot be done correctly.
788
if (!(ptr= current_session->strmake(cstr.ptr(), cstr.length())))
790
conv->str_value.set(ptr, cstr.length(), cstr.charset());
791
/* Ensure that no one is going to change the result string */
792
conv->str_value.mark_as_const();
797
Item *Item_param::safe_charset_converter(const CHARSET_INFO * const tocs)
802
String *ostr= val_str(&cnvstr);
803
cnvitem->str_value.copy(ostr->ptr(), ostr->length(),
804
ostr->charset(), tocs, &cnv_errors);
807
cnvitem->str_value.mark_as_const();
808
cnvitem->max_length= cnvitem->str_value.numchars() * tocs->mbmaxlen;
815
Item *Item_static_string_func::safe_charset_converter(const CHARSET_INFO * const tocs)
818
uint32_t conv_errors;
819
String tmp, cstr, *ostr= val_str(&tmp);
820
cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
822
!(conv= new Item_static_string_func(func_name,
823
cstr.ptr(), cstr.length(),
825
collation.derivation)))
828
Safe conversion is not possible (or EOM).
829
We could not convert a string into the requested character set
830
without data loss. The target charset does not cover all the
831
characters from the string. Operation cannot be done correctly.
835
conv->str_value.copy();
836
/* Ensure that no one is going to change the result string */
837
conv->str_value.mark_as_const();
842
bool Item_string::eq(const Item *item, bool binary_cmp) const
844
if (type() == item->type() && item->basic_const_item())
847
return !stringcmp(&str_value, &item->str_value);
848
return (collation.collation == item->collation.collation &&
849
!sortcmp(&str_value, &item->str_value, collation.collation));
856
Get the value of the function as a DRIZZLE_TIME structure.
857
As a extra convenience the time structure is reset on error!
860
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
862
if (result_type() == STRING_RESULT)
865
String tmp(buff,sizeof(buff), &my_charset_bin),*res;
866
if (!(res=val_str(&tmp)) ||
867
str_to_datetime_with_warn(res->ptr(), res->length(),
868
ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
873
int64_t value= val_int();
875
if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
878
end= int64_t10_to_str(value, buff, -10);
879
make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
880
buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
888
memset(ltime, 0, sizeof(*ltime));
893
Get time of first argument.\
895
As a extra convenience the time structure is reset on error!
898
bool Item::get_time(DRIZZLE_TIME *ltime)
450
bool Item::get_date(type::Time <ime,uint32_t fuzzydate)
458
else if (result_type() == STRING_RESULT)
460
char buff[type::Time::MAX_STRING_LENGTH];
461
String tmp(buff,sizeof(buff), &my_charset_bin),*res;
462
if (!(res=val_str(&tmp)) ||
463
str_to_datetime_with_warn(&getSession(), res->ptr(), res->length(),
464
<ime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
471
int64_t value= val_int();
472
type::datetime_t date_value;
474
ltime.convert(date_value, value, fuzzydate);
476
if (not type::is_valid(date_value))
478
char buff[DECIMAL_LONGLONG_DIGITS], *end;
479
end= internal::int64_t10_to_str(value, buff, -10);
480
make_truncated_value_warning(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN,
481
buff, (int) (end-buff), type::DRIZZLE_TIMESTAMP_NONE, NULL);
494
bool Item::get_time(type::Time <ime)
496
char buff[type::Time::MAX_STRING_LENGTH];
901
497
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
902
if (!(res=val_str(&tmp)) ||
903
str_to_time_with_warn(res->ptr(), res->length(), ltime))
498
if (!(res=val_str(&tmp)) or
499
str_to_time_with_warn(&getSession(), res->ptr(), res->length(), <ime))
905
memset(ltime, 0, sizeof(*ltime));
912
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
509
bool Item::get_date_result(type::Time <ime,uint32_t fuzzydate)
914
return get_date(ltime,fuzzydate);
511
return get_date(ltime, fuzzydate);
918
514
bool Item::is_null()
924
519
void Item::update_null_value ()
926
521
(void) val_int();
930
524
void Item::top_level_item(void)
934
527
void Item::set_result_field(Field *)
938
530
bool Item::is_result_field(void)
944
535
bool Item::is_bool_func(void)
950
540
void Item::save_in_result_field(bool)
954
543
void Item::no_rows_in_result(void)
958
546
Item *Item::copy_or_same(Session *)
964
551
Item *Item::copy_andor_structure(Session *)
970
556
Item *Item::real_item(void)
561
const Item *Item::real_item(void) const
976
566
Item *Item::get_tmp_table_item(Session *session)
978
568
return copy_or_same(session);
982
const CHARSET_INFO *Item::default_charset()
571
const charset_info_st *Item::default_charset()
984
return current_session->variables.collation_connection;
573
return current_session->variables.getCollation();
988
const CHARSET_INFO *Item::compare_collation()
576
const charset_info_st *Item::compare_collation()
994
581
bool Item::walk(Item_processor processor, bool, unsigned char *arg)
996
583
return (this->*processor)(arg);
1000
Item* Item::compile(Item_analyzer analyzer, unsigned char **arg_p,
1001
Item_transformer transformer, unsigned char *arg_t)
586
Item* Item::compile(Item_analyzer analyzer,
587
unsigned char **arg_p,
588
Item_transformer transformer,
589
unsigned char *arg_t)
1003
591
if ((this->*analyzer) (arg_p))
1004
592
return ((this->*transformer) (arg_t));
1009
596
void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
1011
598
(*traverser)(this, arg);
1015
601
bool Item::remove_dependence_processor(unsigned char *)
1021
bool Item::remove_fixed(unsigned char *)
1028
606
bool Item::collect_item_field_processor(unsigned char *)
1034
611
bool Item::find_item_in_field_list_processor(unsigned char *)
1040
616
bool Item::change_context_processor(unsigned char *)
1045
bool Item::reset_query_id_processor(unsigned char *)
1051
621
bool Item::register_field_in_read_map(unsigned char *)
1057
bool Item::register_field_in_bitmap(unsigned char *)
1063
626
bool Item::subst_argument_checker(unsigned char **arg)
1264
750
Item_ref to allow fields from view being stored in tmp table.
1266
752
Item_aggregate_ref *item_ref;
1267
uint32_t el= fields.elements;
753
uint32_t el= fields.size();
1268
754
Item *real_itm= real_item();
1270
756
ref_pointer_array[el]= real_itm;
1271
if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
1272
ref_pointer_array + el, 0, name)))
1273
return; // fatal_error is set
757
item_ref= new Item_aggregate_ref(&session->lex().current_select->context, ref_pointer_array + el, 0, name);
1274
758
if (type() == SUM_FUNC_ITEM)
1275
item_ref->depended_from= ((Item_sum *) this)->depended_from();
759
item_ref->depended_from= ((Item_sum *) this)->depended_from();
1276
760
fields.push_front(real_itm);
1277
session->change_item_tree(ref, item_ref);
1282
void Item_ident_for_show::make_field(Send_field *tmp_field)
1284
tmp_field->table_name= tmp_field->org_table_name= table_name;
1285
tmp_field->db_name= db_name;
1286
tmp_field->col_name= tmp_field->org_col_name= field->field_name;
1287
tmp_field->charsetnr= field->charset()->number;
1288
tmp_field->length=field->field_length;
1289
tmp_field->type=field->type();
1290
tmp_field->flags= field->table->maybe_null ?
1291
(field->flags & ~NOT_NULL_FLAG) : field->flags;
1292
tmp_field->decimals= field->decimals();
1296
double Item_ident_for_show::val_real()
1298
return field->val_real();
1302
int64_t Item_ident_for_show::val_int()
1304
return field->val_int();
1308
String *Item_ident_for_show::val_str(String *str)
1310
return field->val_str(str);
1314
my_decimal *Item_ident_for_show::val_decimal(my_decimal *dec)
1316
return field->val_decimal(dec);
1320
/**********************************************/
1322
Item_field::Item_field(Field *f)
1323
:Item_ident(0, NULL, *f->table_name, f->field_name),
1324
item_equal(0), no_const_subst(0),
1325
have_privileges(0), any_privileges(0)
1329
field_name and table_name should not point to garbage
1330
if this item is to be reused
1332
orig_table_name= orig_field_name= "";
1337
Constructor used inside setup_wild().
1339
Ensures that field, table, and database names will live as long as
1340
Item_field (this is important in prepared statements).
1343
Item_field::Item_field(Session *, Name_resolution_context *context_arg,
1345
:Item_ident(context_arg, f->table->s->db.str, *f->table_name, f->field_name),
1346
item_equal(0), no_const_subst(0),
1347
have_privileges(0), any_privileges(0)
1353
Item_field::Item_field(Name_resolution_context *context_arg,
1354
const char *db_arg,const char *table_name_arg,
1355
const char *field_name_arg)
1356
:Item_ident(context_arg, db_arg,table_name_arg,field_name_arg),
1357
field(0), result_field(0), item_equal(0), no_const_subst(0),
1358
have_privileges(0), any_privileges(0)
1360
SELECT_LEX *select= current_session->lex->current_select;
1361
collation.set(DERIVATION_IMPLICIT);
1362
if (select && select->parsing_place != IN_HAVING)
1363
select->select_n_where_fields++;
1367
Constructor need to process subselect with temporary tables (see Item)
1370
Item_field::Item_field(Session *session, Item_field *item)
1371
:Item_ident(session, item),
1373
result_field(item->result_field),
1374
item_equal(item->item_equal),
1375
no_const_subst(item->no_const_subst),
1376
have_privileges(item->have_privileges),
1377
any_privileges(item->any_privileges)
1379
collation.set(DERIVATION_IMPLICIT);
1382
void Item_field::set_field(Field *field_par)
1384
field=result_field=field_par; // for easy coding with fields
1385
maybe_null=field->maybe_null();
1386
decimals= field->decimals();
1387
max_length= field_par->max_display_length();
1388
table_name= *field_par->table_name;
1389
field_name= field_par->field_name;
1390
db_name= field_par->table->s->db.str;
1391
alias_name_used= field_par->table->alias_name_used;
1392
unsigned_flag=test(field_par->flags & UNSIGNED_FLAG);
1393
collation.set(field_par->charset(), field_par->derivation());
1395
if (field->table->s->tmp_table == SYSTEM_TMP_TABLE)
1401
Reset this item to point to a field from the new temporary table.
1402
This is used when we create a new temporary table for each execution
1403
of prepared statement.
1406
void Item_field::reset_field(Field *f)
1409
/* 'name' is pointing at field->field_name of old field */
1410
name= (char*) f->field_name;
1413
const char *Item_ident::full_name() const
1416
if (!table_name || !field_name)
1417
return field_name ? field_name : name ? name : "tmp_field";
1418
if (db_name && db_name[0])
1420
tmp=(char*) sql_alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
1421
(uint) strlen(field_name)+3);
1422
strxmov(tmp,db_name,".",table_name,".",field_name,NULL);
1428
tmp= (char*) sql_alloc((uint) strlen(table_name) +
1429
(uint) strlen(field_name) + 2);
1430
strxmov(tmp, table_name, ".", field_name, NULL);
1433
tmp= (char*) field_name;
1438
void Item_ident::print(String *str,
1441
Session *session= current_session;
1442
char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME];
1443
const char *d_name= db_name, *t_name= table_name;
1444
if (lower_case_table_names== 1 ||
1445
(lower_case_table_names == 2 && !alias_name_used))
1447
if (table_name && table_name[0])
1449
my_stpcpy(t_name_buff, table_name);
1450
my_casedn_str(files_charset_info, t_name_buff);
1451
t_name= t_name_buff;
1453
if (db_name && db_name[0])
1455
my_stpcpy(d_name_buff, db_name);
1456
my_casedn_str(files_charset_info, d_name_buff);
1457
d_name= d_name_buff;
1461
if (!table_name || !field_name || !field_name[0])
1463
const char *nm= (field_name && field_name[0]) ?
1464
field_name : name ? name : "tmp_field";
1465
append_identifier(session, str, nm, (uint) strlen(nm));
1468
if (db_name && db_name[0] && !alias_name_used)
1471
append_identifier(session, str, d_name, (uint)strlen(d_name));
1474
append_identifier(session, str, t_name, (uint)strlen(t_name));
1476
append_identifier(session, str, field_name, (uint)strlen(field_name));
1482
append_identifier(session, str, t_name, (uint) strlen(t_name));
1484
append_identifier(session, str, field_name, (uint) strlen(field_name));
1487
append_identifier(session, str, field_name, (uint) strlen(field_name));
1492
String *Item_field::val_str(String *str)
1495
if ((null_value=field->is_null()))
1497
str->set_charset(str_value.charset());
1498
return field->val_str(str,&str_value);
1502
double Item_field::val_real()
1505
if ((null_value=field->is_null()))
1507
return field->val_real();
1511
int64_t Item_field::val_int()
1514
if ((null_value=field->is_null()))
1516
return field->val_int();
1520
my_decimal *Item_field::val_decimal(my_decimal *decimal_value)
1522
if ((null_value= field->is_null()))
1524
return field->val_decimal(decimal_value);
1528
String *Item_field::str_result(String *str)
1530
if ((null_value=result_field->is_null()))
1532
str->set_charset(str_value.charset());
1533
return result_field->val_str(str,&str_value);
1536
bool Item_field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
1538
if ((null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
1540
memset(ltime, 0, sizeof(*ltime));
1546
bool Item_field::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
1548
if ((null_value=result_field->is_null()) ||
1549
result_field->get_date(ltime,fuzzydate))
1551
memset(ltime, 0, sizeof(*ltime));
1557
bool Item_field::get_time(DRIZZLE_TIME *ltime)
1559
if ((null_value=field->is_null()) || field->get_time(ltime))
1561
memset(ltime, 0, sizeof(*ltime));
1567
double Item_field::val_result()
1569
if ((null_value=result_field->is_null()))
1571
return result_field->val_real();
1574
int64_t Item_field::val_int_result()
1576
if ((null_value=result_field->is_null()))
1578
return result_field->val_int();
1582
my_decimal *Item_field::val_decimal_result(my_decimal *decimal_value)
1584
if ((null_value= result_field->is_null()))
1586
return result_field->val_decimal(decimal_value);
1590
bool Item_field::val_bool_result()
1592
if ((null_value= result_field->is_null()))
1594
switch (result_field->result_type()) {
1596
return result_field->val_int() != 0;
1597
case DECIMAL_RESULT:
1599
my_decimal decimal_value;
1600
my_decimal *val= result_field->val_decimal(&decimal_value);
1602
return !my_decimal_is_zero(val);
1607
return result_field->val_real() != 0.0;
1611
return 0; // Shut up compiler
1616
bool Item_field::eq(const Item *item, bool) const
1618
Item *real_item= ((Item *) item)->real_item();
1619
if (real_item->type() != FIELD_ITEM)
1622
Item_field *item_field= (Item_field*) real_item;
1623
if (item_field->field && field)
1624
return item_field->field == field;
1626
We may come here when we are trying to find a function in a GROUP BY
1627
clause from the select list.
1628
In this case the '100 % correct' way to do this would be to first
1629
run fix_fields() on the GROUP BY item and then retry this function, but
1630
I think it's better to relax the checking a bit as we will in
1631
most cases do the correct thing by just checking the field name.
1632
(In cases where we would choose wrong we would have to generate a
1635
return (!my_strcasecmp(system_charset_info, item_field->name,
1637
(!item_field->table_name || !table_name ||
1638
(!my_strcasecmp(table_alias_charset, item_field->table_name,
1640
(!item_field->db_name || !db_name ||
1641
(item_field->db_name && !strcmp(item_field->db_name,
1646
table_map Item_field::used_tables() const
1648
if (field->table->const_table)
1649
return 0; // const item
1650
return (depended_from ? OUTER_REF_TABLE_BIT : field->table->map);
1654
enum Item_result Item_field::result_type () const
1656
return field->result_type();
1660
Item_result Item_field::cast_to_int_type() const
1662
return field->cast_to_int_type();
1666
enum_field_types Item_field::field_type() const
1668
return field->type();
1672
void Item_field::fix_after_pullout(st_select_lex *new_parent, Item **)
1674
if (new_parent == depended_from)
1675
depended_from= NULL;
1676
Name_resolution_context *ctx= new Name_resolution_context();
1677
ctx->outer_context= NULL; // We don't build a complete name resolver
1678
ctx->select_lex= new_parent;
1679
ctx->first_name_resolution_table= context->first_name_resolution_table;
1680
ctx->last_name_resolution_table= context->last_name_resolution_table;
1685
bool Item_field::is_null()
1687
return field->is_null();
1691
Item *Item_field::get_tmp_table_item(Session *session)
1693
Item_field *new_item= new Item_field(session, this);
1695
new_item->field= new_item->result_field;
1699
int64_t Item_field::val_int_endpoint(bool, bool *)
1701
int64_t res= val_int();
1702
return null_value? INT64_MIN : res;
1706
Create an item from a string we KNOW points to a valid int64_t
1707
end \\0 terminated number string.
1708
This is always 'signed'. Unsigned values are created with Item_uint()
1711
Item_int::Item_int(const char *str_arg, uint32_t length)
1713
char *end_ptr= (char*) str_arg + length;
1715
value= my_strtoll10(str_arg, &end_ptr, &error);
1716
max_length= (uint) (end_ptr - str_arg);
1717
name= (char*) str_arg;
1722
my_decimal *Item_int::val_decimal(my_decimal *decimal_value)
1724
int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_value);
1725
return decimal_value;
1728
String *Item_int::val_str(String *str)
1730
// following assert is redundant, because fixed=1 assigned in constructor
1732
str->set(value, &my_charset_bin);
1736
void Item_int::print(String *str, enum_query_type)
1738
// my_charset_bin is good enough for numbers
1739
str_value.set(value, &my_charset_bin);
1740
str->append(str_value);
1744
Item_uint::Item_uint(const char *str_arg, uint32_t length):
1745
Item_int(str_arg, length)
1751
Item_uint::Item_uint(const char *str_arg, int64_t i, uint32_t length):
1752
Item_int(str_arg, i, length)
1758
String *Item_uint::val_str(String *str)
1760
// following assert is redundant, because fixed=1 assigned in constructor
1762
str->set((uint64_t) value, &my_charset_bin);
1767
void Item_uint::print(String *str, enum_query_type)
1769
// latin1 is good enough for numbers
1770
str_value.set((uint64_t) value, default_charset());
1771
str->append(str_value);
1775
Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
1776
const CHARSET_INFO * const charset)
1778
str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
1779
name= (char*) str_arg;
1780
decimals= (uint8_t) decimal_value.frac;
1782
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1783
decimals, unsigned_flag);
1786
Item_decimal::Item_decimal(int64_t val, bool unsig)
1788
int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
1789
decimals= (uint8_t) decimal_value.frac;
1791
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1792
decimals, unsigned_flag);
1796
Item_decimal::Item_decimal(double val, int, int)
1798
double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
1799
decimals= (uint8_t) decimal_value.frac;
1801
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1802
decimals, unsigned_flag);
1806
Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg,
1807
uint32_t decimal_par, uint32_t length)
1809
my_decimal2decimal(val_arg, &decimal_value);
1811
decimals= (uint8_t) decimal_par;
1817
Item_decimal::Item_decimal(my_decimal *value_par)
1819
my_decimal2decimal(value_par, &decimal_value);
1820
decimals= (uint8_t) decimal_value.frac;
1822
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1823
decimals, unsigned_flag);
1827
Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
1829
binary2my_decimal(E_DEC_FATAL_ERROR, bin,
1830
&decimal_value, precision, scale);
1831
decimals= (uint8_t) decimal_value.frac;
1833
max_length= my_decimal_precision_to_length(precision, decimals,
1838
int64_t Item_decimal::val_int()
1841
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &result);
1845
double Item_decimal::val_real()
1848
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
1852
String *Item_decimal::val_str(String *result)
1854
result->set_charset(&my_charset_bin);
1855
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, result);
1859
void Item_decimal::print(String *str, enum_query_type)
1861
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
1862
str->append(str_value);
1866
bool Item_decimal::eq(const Item *item, bool) const
1868
if (type() == item->type() && item->basic_const_item())
1871
We need to cast off const to call val_decimal(). This should
1872
be OK for a basic constant. Additionally, we can pass 0 as
1873
a true decimal constant will return its internal decimal
1874
storage and ignore the argument.
1876
Item *arg= (Item*) item;
1877
my_decimal *value= arg->val_decimal(0);
1878
return !my_decimal_cmp(&decimal_value, value);
1884
void Item_decimal::set_decimal_value(my_decimal *value_par)
1886
my_decimal2decimal(value_par, &decimal_value);
1887
decimals= (uint8_t) decimal_value.frac;
1888
unsigned_flag= !decimal_value.sign();
1889
max_length= my_decimal_precision_to_length(decimal_value.intg + decimals,
1890
decimals, unsigned_flag);
1894
String *Item_float::val_str(String *str)
1896
// following assert is redundant, because fixed=1 assigned in constructor
1898
str->set_real(value,decimals,&my_charset_bin);
1903
int64_t Item_float::val_int()
1906
if (value <= (double) INT64_MIN)
1910
else if (value >= (double) (uint64_t) INT64_MAX)
1914
return (int64_t) rint(value);
1917
my_decimal *Item_float::val_decimal(my_decimal *decimal_value)
1919
// following assert is redundant, because fixed=1 assigned in constructor
1921
double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_value);
1922
return (decimal_value);
1926
void Item_string::print(String *str, enum_query_type query_type)
1928
if (query_type == QT_ORDINARY && is_cs_specified())
1931
str->append(collation.collation->csname);
1936
if (query_type == QT_ORDINARY ||
1937
my_charset_same(str_value.charset(), system_charset_info))
1939
str_value.print(str);
1943
Session *session= current_session;
1944
LEX_STRING utf8_lex_str;
1946
session->convert_string(&utf8_lex_str,
1947
system_charset_info,
1948
str_value.c_ptr_safe(),
1950
str_value.charset());
1952
String utf8_str(utf8_lex_str.str,
1953
utf8_lex_str.length,
1954
system_charset_info);
1956
utf8_str.print(str);
1963
double Item_string::val_real()
1967
char *end, *org_end;
1969
const CHARSET_INFO * const cs= str_value.charset();
1971
org_end= (char*) str_value.ptr() + str_value.length();
1972
tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end,
1974
if (error || (end != org_end && !check_if_only_end_space(cs, end, org_end)))
1977
We can use str_value.ptr() here as Item_string is gurantee to put an
1980
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1981
ER_TRUNCATED_WRONG_VALUE,
1982
ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
1991
Give error if we wanted a signed integer and we got an unsigned one
1993
int64_t Item_string::val_int()
1998
char *end= (char*) str_value.ptr()+ str_value.length();
2000
const CHARSET_INFO * const cs= str_value.charset();
2002
tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
2004
TODO: Give error if we wanted a signed integer and we got an unsigned
2008
(end != org_end && !check_if_only_end_space(cs, end, org_end)))
2010
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2011
ER_TRUNCATED_WRONG_VALUE,
2012
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
2019
my_decimal *Item_string::val_decimal(my_decimal *decimal_value)
2021
return val_decimal_from_string(decimal_value);
2025
bool Item_null::eq(const Item *item, bool) const
2026
{ return item->type() == type(); }
2029
double Item_null::val_real()
2031
// following assert is redundant, because fixed=1 assigned in constructor
2036
int64_t Item_null::val_int()
2038
// following assert is redundant, because fixed=1 assigned in constructor
2044
String *Item_null::val_str(String *)
2046
// following assert is redundant, because fixed=1 assigned in constructor
2052
my_decimal *Item_null::val_decimal(my_decimal *)
2058
Item *Item_null::safe_charset_converter(const CHARSET_INFO * const tocs)
2060
collation.set(tocs);
2064
/*********************** Item_param related ******************************/
2067
Default function of Item_param::set_param_func, so in case
2068
of malformed packet the server won't SIGSEGV.
2072
default_set_param_func(Item_param *param, unsigned char **, ulong)
2078
Item_param::Item_param(uint32_t pos_in_query_arg) :
2080
item_result_type(STRING_RESULT),
2081
/* Don't pretend to be a literal unless value for this item is set. */
2082
item_type(PARAM_ITEM),
2083
param_type(DRIZZLE_TYPE_VARCHAR),
2084
pos_in_query(pos_in_query_arg),
2085
set_param_func(default_set_param_func),
2086
limit_clause_param(false)
2090
Since we can't say whenever this item can be NULL or cannot be NULL
2091
before mysql_stmt_execute(), so we assuming that it can be NULL until
2095
cnvitem= new Item_string("", 0, &my_charset_bin, DERIVATION_COERCIBLE);
2096
cnvstr.set(cnvbuf, sizeof(cnvbuf), &my_charset_bin);
2100
void Item_param::set_null()
2102
/* These are cleared after each execution by reset() method */
2105
Because of NULL and string values we need to set max_length for each new
2106
placeholder value: user can submit NULL for any placeholder type, and
2107
string length can be different in each execution.
2112
item_type= Item::NULL_ITEM;
2116
void Item_param::set_int(int64_t i, uint32_t max_length_arg)
2118
value.integer= (int64_t) i;
2120
max_length= max_length_arg;
2126
void Item_param::set_double(double d)
2130
max_length= DBL_DIG + 8;
2131
decimals= NOT_FIXED_DEC;
2138
Set decimal parameter value from string.
2140
@param str character string
2141
@param length string length
2144
As we use character strings to send decimal values in
2145
binary protocol, we use str2my_decimal to convert it to
2146
internal decimal value.
2149
void Item_param::set_decimal(char *str, ulong length)
2154
str2my_decimal((uint)E_DEC_FATAL_ERROR, str, &decimal_value, &end);
2155
state= DECIMAL_VALUE;
2156
decimals= decimal_value.frac;
2157
max_length= my_decimal_precision_to_length(decimal_value.precision(),
2158
decimals, unsigned_flag);
2165
Set parameter value from DRIZZLE_TIME value.
2167
@param tm datetime value to set (time_type is ignored)
2168
@param type type of datetime value
2169
@param max_length_arg max length of datetime value as string
2172
If we value to be stored is not normalized, zero value will be stored
2173
instead and proper warning will be produced. This function relies on
2174
the fact that even wrong value sent over binary protocol fits into
2175
MAX_DATE_STRING_REP_LENGTH buffer.
2177
void Item_param::set_time(DRIZZLE_TIME *tm,
2178
enum enum_drizzle_timestamp_type time_type,
2179
uint32_t max_length_arg)
2182
value.time.time_type= time_type;
2184
if (value.time.year > 9999 || value.time.month > 12 ||
2185
value.time.day > 31 ||
2186
((time_type != DRIZZLE_TIMESTAMP_TIME) && value.time.hour > 23) ||
2187
value.time.minute > 59 || value.time.second > 59)
2189
char buff[MAX_DATE_STRING_REP_LENGTH];
2190
uint32_t length= my_TIME_to_str(&value.time, buff);
2191
make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2192
buff, length, time_type, 0);
2193
set_zero_time(&value.time, DRIZZLE_TIMESTAMP_ERROR);
2198
max_length= max_length_arg;
2204
bool Item_param::set_str(const char *str, ulong length)
2207
Assign string with no conversion: data is converted only after it's
2208
been written to the binary log.
2210
uint32_t dummy_errors;
2211
if (str_value.copy(str, length, &my_charset_bin, &my_charset_bin,
2214
state= STRING_VALUE;
2217
/* max_length and decimals are set after charset conversion */
2218
/* sic: str may be not null-terminated */
2223
bool Item_param::set_longdata(const char *str, ulong length)
2226
If client character set is multibyte, end of long data packet
2227
may hit at the middle of a multibyte character. Additionally,
2228
if binary log is open we must write long data value to the
2229
binary log in character set of client. This is why we can't
2230
convert long data to connection character set as it comes
2231
(here), and first have to concatenate all pieces together,
2232
write query to the binary log and only then perform conversion.
2234
if (str_value.append(str, length, &my_charset_bin))
2236
state= LONG_DATA_VALUE;
2244
Set parameter value from user variable value.
2246
@param session Current thread
2247
@param entry User variable structure (NULL means use NULL value)
2255
bool Item_param::set_from_user_var(Session *session, const user_var_entry *entry)
2257
if (entry && entry->value)
2259
item_result_type= entry->type;
2260
unsigned_flag= entry->unsigned_flag;
2261
if (limit_clause_param)
2264
set_int(entry->val_int(&unused), MY_INT64_NUM_DECIMAL_DIGITS);
2265
item_type= Item::INT_ITEM;
2266
return(!unsigned_flag && value.integer < 0 ? 1 : 0);
2268
switch (item_result_type) {
2270
set_double(*(double*)entry->value);
2271
item_type= Item::REAL_ITEM;
2274
set_int(*(int64_t*)entry->value, MY_INT64_NUM_DECIMAL_DIGITS);
2275
item_type= Item::INT_ITEM;
2279
const CHARSET_INFO * const fromcs= entry->collation.collation;
2280
const CHARSET_INFO * const tocs= session->variables.collation_connection;
2281
uint32_t dummy_offset;
2283
value.cs_info.character_set_of_placeholder=
2284
value.cs_info.character_set_client= fromcs;
2286
Setup source and destination character sets so that they
2287
are different only if conversion is necessary: this will
2288
make later checks easier.
2290
value.cs_info.final_character_set_of_str_value=
2291
String::needs_conversion(0, fromcs, tocs, &dummy_offset) ?
2294
Exact value of max_length is not known unless data is converted to
2295
charset of connection, so we have to set it later.
2297
item_type= Item::STRING_ITEM;
2299
if (set_str((const char *)entry->value, entry->length))
2303
case DECIMAL_RESULT:
2305
const my_decimal *ent_value= (const my_decimal *)entry->value;
2306
my_decimal2decimal(ent_value, &decimal_value);
2307
state= DECIMAL_VALUE;
2308
decimals= ent_value->frac;
2309
max_length= my_decimal_precision_to_length(ent_value->precision(),
2310
decimals, unsigned_flag);
2311
item_type= Item::DECIMAL_ITEM;
2326
Resets parameter after execution.
2329
We clear null_value here instead of setting it in set_* methods,
2330
because we want more easily handle case for long data.
2333
void Item_param::reset()
2335
/* Shrink string buffer if it's bigger than max possible CHAR column */
2336
if (str_value.alloced_length() > MAX_CHAR_WIDTH)
2339
str_value.length(0);
2340
str_value_ptr.length(0);
2342
We must prevent all charset conversions until data has been written
2345
str_value.set_charset(&my_charset_bin);
2346
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
2351
Don't reset item_type to PARAM_ITEM: it's only needed to guard
2352
us from item optimizations at prepare stage, when item doesn't yet
2353
contain a literal of some kind.
2354
In all other cases when this object is accessed its value is
2355
set (this assumption is guarded by 'state' and
2356
assertS(state != NO_VALUE) in all Item_param::get_*
2363
int Item_param::save_in_field(Field *field, bool no_conversions)
2365
field->set_notnull();
2369
return field->store(value.integer, unsigned_flag);
2371
return field->store(value.real);
2373
return field->store_decimal(&decimal_value);
2375
field->store_time(&value.time, value.time.time_type);
2378
case LONG_DATA_VALUE:
2379
return field->store(str_value.ptr(), str_value.length(),
2380
str_value.charset());
2382
return set_field_to_null_with_conversions(field, no_conversions);
2391
bool Item_param::get_time(DRIZZLE_TIME *res)
2393
if (state == TIME_VALUE)
2399
If parameter value isn't supplied assertion will fire in val_str()
2400
which is called from Item::get_time().
2402
return Item::get_time(res);
2406
bool Item_param::get_date(DRIZZLE_TIME *res, uint32_t fuzzydate)
2408
if (state == TIME_VALUE)
2413
return Item::get_date(res, fuzzydate);
2417
double Item_param::val_real()
2423
return (double) value.integer;
2427
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
2431
case LONG_DATA_VALUE:
2435
return my_strntod(str_value.charset(), (char*) str_value.ptr(),
2436
str_value.length(), &end_not_used, &dummy_err);
2440
This works for example when user says SELECT ?+0.0 and supplies
2441
time value for the placeholder.
2443
return uint64_t2double(TIME_to_uint64_t(&value.time));
2453
int64_t Item_param::val_int()
2457
return (int64_t) rint(value.real);
2459
return value.integer;
2463
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &i);
2467
case LONG_DATA_VALUE:
2470
return my_strntoll(str_value.charset(), str_value.ptr(),
2471
str_value.length(), 10, (char**) 0, &dummy_err);
2474
return (int64_t) TIME_to_uint64_t(&value.time);
2484
my_decimal *Item_param::val_decimal(my_decimal *dec)
2488
return &decimal_value;
2490
double2my_decimal(E_DEC_FATAL_ERROR, value.real, dec);
2493
int2my_decimal(E_DEC_FATAL_ERROR, value.integer, unsigned_flag, dec);
2496
case LONG_DATA_VALUE:
2497
string2my_decimal(E_DEC_FATAL_ERROR, &str_value, dec);
2501
int64_t i= (int64_t) TIME_to_uint64_t(&value.time);
2502
int2my_decimal(E_DEC_FATAL_ERROR, i, 0, dec);
2514
String *Item_param::val_str(String* str)
2518
case LONG_DATA_VALUE:
2519
return &str_value_ptr;
2521
str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
2524
str->set(value.integer, &my_charset_bin);
2527
if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
2533
if (str->reserve(MAX_DATE_STRING_REP_LENGTH))
2535
str->length((uint) my_TIME_to_str(&value.time, (char*) str->ptr()));
2536
str->set_charset(&my_charset_bin);
2548
Return Param item values in string format, for generating the dynamic
2549
query used in update/binary logs.
2552
- Change interface and implementation to fill log data in place
2553
and avoid one more memcpy/alloc between str and log string.
2554
- In case of error we need to notify replication
2555
that binary log contains wrong statement
2558
const String *Item_param::query_val_str(String* str) const
2562
str->set_int(value.integer, unsigned_flag, &my_charset_bin);
2565
str->set_real(value.real, NOT_FIXED_DEC, &my_charset_bin);
2568
if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value,
2570
return &my_null_string;
2577
TODO: in case of error we need to notify replication
2578
that binary log contains wrong statement
2580
if (str->reserve(MAX_DATE_STRING_REP_LENGTH+3))
2583
/* Create date string inplace */
2584
buf= str->c_ptr_quick();
2587
ptr+= (uint) my_TIME_to_str(&value.time, ptr);
2589
str->length((uint32_t) (ptr - buf));
2593
case LONG_DATA_VALUE:
2596
append_query_string(value.cs_info.character_set_client, &str_value, str);
2600
return &my_null_string;
2609
Convert string from client character set to the character set of
2613
bool Item_param::convert_str_value(Session *session)
2616
if (state == STRING_VALUE || state == LONG_DATA_VALUE)
2619
Check is so simple because all charsets were set up properly
2620
in setup_one_conversion_function, where typecode of
2621
placeholder was also taken into account: the variables are different
2622
here only if conversion is really necessary.
2624
if (value.cs_info.final_character_set_of_str_value !=
2625
value.cs_info.character_set_of_placeholder)
2627
rc= session->convert_string(&str_value,
2628
value.cs_info.character_set_of_placeholder,
2629
value.cs_info.final_character_set_of_str_value);
2632
str_value.set_charset(value.cs_info.final_character_set_of_str_value);
2633
/* Here str_value is guaranteed to be in final_character_set_of_str_value */
2635
max_length= str_value.length();
2638
str_value_ptr is returned from val_str(). It must be not alloced
2639
to prevent it's modification by val_str() invoker.
2641
str_value_ptr.set(str_value.ptr(), str_value.length(),
2642
str_value.charset());
2643
/* Synchronize item charset with value charset */
2644
collation.set(str_value.charset(), DERIVATION_COERCIBLE);
2650
bool Item_param::basic_const_item() const
2652
if (state == NO_VALUE || state == TIME_VALUE)
2659
Item_param::clone_item()
2661
/* see comments in the header file */
2664
return new Item_null(name);
2666
return (unsigned_flag ?
2667
new Item_uint(name, value.integer, max_length) :
2668
new Item_int(name, value.integer, max_length));
2670
return new Item_float(name, value.real, decimals, max_length);
2672
case LONG_DATA_VALUE:
2673
return new Item_string(name, str_value.c_ptr_quick(), str_value.length(),
2674
str_value.charset());
2686
Item_param::eq(const Item *arg, bool binary_cmp) const
2689
if (!basic_const_item() || !arg->basic_const_item() || arg->type() != type())
2692
We need to cast off const to call val_int(). This should be OK for
2701
return value.integer == item->val_int() &&
2702
unsigned_flag == item->unsigned_flag;
2704
return value.real == item->val_real();
2706
case LONG_DATA_VALUE:
2708
return !stringcmp(&str_value, &item->str_value);
2709
return !sortcmp(&str_value, &item->str_value, collation.collation);
2716
/* End of Item_param related */
2718
void Item_param::print(String *str, enum_query_type)
2720
if (state == NO_VALUE)
2726
char buffer[STRING_BUFFER_USUAL_SIZE];
2727
String tmp(buffer, sizeof(buffer), &my_charset_bin);
2729
res= query_val_str(&tmp);
2735
/****************************************************************************
2737
****************************************************************************/
2739
void Item_copy_string::copy()
2741
String *res=item->val_str(&str_value);
2742
if (res && res != &str_value)
2743
str_value.copy(*res);
2744
null_value=item->null_value;
2748
String *Item_copy_string::val_str(String *)
2750
// Item_copy_string is used without fix_fields call
2757
my_decimal *Item_copy_string::val_decimal(my_decimal *decimal_value)
2759
// Item_copy_string is used without fix_fields call
2762
string2my_decimal(E_DEC_FATAL_ERROR, &str_value, decimal_value);
2763
return (decimal_value);
2768
766
Functions to convert item to field (for send_fields)
2772
768
bool Item::fix_fields(Session *, Item **)
2775
// We do not check fields which are fixed during construction
2776
assert(fixed == 0 || basic_const_item());
770
/* We do not check fields which are fixed during construction */
771
assert(! fixed || basic_const_item());
2781
double Item_ref_null_helper::val_real()
2784
double tmp= (*ref)->val_result();
2785
owner->was_null|= null_value= (*ref)->null_value;
2790
int64_t Item_ref_null_helper::val_int()
2793
int64_t tmp= (*ref)->val_int_result();
2794
owner->was_null|= null_value= (*ref)->null_value;
2799
my_decimal *Item_ref_null_helper::val_decimal(my_decimal *decimal_value)
2802
my_decimal *val= (*ref)->val_decimal_result(decimal_value);
2803
owner->was_null|= null_value= (*ref)->null_value;
2808
bool Item_ref_null_helper::val_bool()
2811
bool val= (*ref)->val_bool_result();
2812
owner->was_null|= null_value= (*ref)->null_value;
2817
String* Item_ref_null_helper::val_str(String* s)
2820
String* tmp= (*ref)->str_result(s);
2821
owner->was_null|= null_value= (*ref)->null_value;
2826
bool Item_ref_null_helper::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
2828
return (owner->was_null|= null_value= (*ref)->get_date(ltime, fuzzydate));
2833
Mark item and SELECT_LEXs as dependent if item was resolved in
2836
@param session thread handler
2837
@param last select from which current item depend
2838
@param current current select
2839
@param resolved_item item which was resolved in outer SELECT(for warning)
2840
@param mark_item item which should be marked (can be differ in case of
2844
static void mark_as_dependent(Session *session, SELECT_LEX *last, SELECT_LEX *current,
776
void mark_as_dependent(Session *session, Select_Lex *last, Select_Lex *current,
2845
777
Item_ident *resolved_item,
2846
778
Item_ident *mark_item)
3134
1003
return (Item**) not_found_item;
3139
Resolve the name of an outer select column reference.
3141
The method resolves the column reference represented by 'this' as a column
3142
present in outer selects that contain current select.
3144
In prepared statements, because of cache, find_field_in_tables()
3145
can resolve fields even if they don't belong to current context.
3146
In this case this method only finds appropriate context and marks
3147
current select as dependent. The found reference of field should be
3148
provided in 'from_field'.
3150
@param[in] session current thread
3151
@param[in,out] from_field found field reference or (Field*)not_found_field
3152
@param[in,out] reference view column if this item was resolved to a
3156
This is the inner loop of Item_field::fix_fields:
3158
for each outer query Q_k beginning from the inner-most one
3160
search for a column or derived column named col_ref_i
3161
[in table T_j] in the FROM clause of Q_k;
3163
if such a column is not found
3164
Search for a column or derived column named col_ref_i
3165
[in table T_j] in the SELECT and GROUP clauses of Q_k.
3170
1 column succefully resolved and fix_fields() should continue.
3172
0 column fully fixed and fix_fields() should return false
3178
Item_field::fix_outer_field(Session *session, Field **from_field, Item **reference)
3180
enum_parsing_place place= NO_MATTER;
3181
bool field_found= (*from_field != not_found_field);
3182
bool upward_lookup= false;
3185
If there are outer contexts (outer selects, but current select is
3186
not derived table or view) try to resolve this reference in the
3189
We treat each subselect as a separate namespace, so that different
3190
subselects may contain columns with the same names. The subselects
3191
are searched starting from the innermost.
3193
Name_resolution_context *last_checked_context= context;
3194
Item **ref= (Item **) not_found_item;
3195
SELECT_LEX *current_sel= (SELECT_LEX *) session->lex->current_select;
3196
Name_resolution_context *outer_context= 0;
3197
SELECT_LEX *select= 0;
3198
/* Currently derived tables cannot be correlated */
3199
if (current_sel->master_unit()->first_select()->linkage !=
3201
outer_context= context->outer_context;
3204
outer_context= outer_context->outer_context)
3206
select= outer_context->select_lex;
3207
Item_subselect *prev_subselect_item=
3208
last_checked_context->select_lex->master_unit()->item;
3209
last_checked_context= outer_context;
3210
upward_lookup= true;
3212
place= prev_subselect_item->parsing_place;
3214
If outer_field is set, field was already found by first call
3215
to find_field_in_tables(). Only need to find appropriate context.
3217
if (field_found && outer_context->select_lex !=
3218
cached_table->select_lex)
3221
In case of a view, find_field_in_tables() writes the pointer to
3222
the found view field into '*reference', in other words, it
3223
substitutes this Item_field with the found expression.
3225
if (field_found || (*from_field= find_field_in_tables(session, this,
3227
first_name_resolution_table,
3229
last_name_resolution_table,
3231
IGNORE_EXCEPT_NON_UNIQUE,
3237
if (*from_field != view_ref_found)
3239
prev_subselect_item->used_tables_cache|= (*from_field)->table->map;
3240
prev_subselect_item->const_item_cache= 0;
3241
set_field(*from_field);
3242
if (!last_checked_context->select_lex->having_fix_field &&
3243
select->group_list.elements &&
3244
(place == SELECT_LIST || place == IN_HAVING))
3248
If an outer field is resolved in a grouping select then it
3249
is replaced for an Item_outer_ref object. Otherwise an
3250
Item_field object is used.
3251
The new Item_outer_ref object is saved in the inner_refs_list of
3252
the outer select. Here it is only created. It can be fixed only
3253
after the original field has been fixed and this is done in the
3254
fix_inner_refs() function.
3257
if (!(rf= new Item_outer_ref(context, this)))
3259
session->change_item_tree(reference, rf);
3260
select->inner_refs_list.push_back(rf);
3261
rf->in_sum_func= session->lex->in_sum_func;
3264
A reference is resolved to a nest level that's outer or the same as
3265
the nest level of the enclosing set function : adjust the value of
3266
max_arg_level for the function if it's needed.
3268
if (session->lex->in_sum_func &&
3269
session->lex->in_sum_func->nest_level >= select->nest_level)
3271
Item::Type ref_type= (*reference)->type();
3272
set_if_bigger(session->lex->in_sum_func->max_arg_level,
3273
select->nest_level);
3274
set_field(*from_field);
3276
mark_as_dependent(session, last_checked_context->select_lex,
3277
context->select_lex, this,
3278
((ref_type == REF_ITEM ||
3279
ref_type == FIELD_ITEM) ?
3280
(Item_ident*) (*reference) : 0));
3286
Item::Type ref_type= (*reference)->type();
3287
prev_subselect_item->used_tables_cache|=
3288
(*reference)->used_tables();
3289
prev_subselect_item->const_item_cache&=
3290
(*reference)->const_item();
3291
mark_as_dependent(session, last_checked_context->select_lex,
3292
context->select_lex, this,
3293
((ref_type == REF_ITEM || ref_type == FIELD_ITEM) ?
3294
(Item_ident*) (*reference) :
3297
A reference to a view field had been found and we
3298
substituted it instead of this Item (find_field_in_tables
3299
does it by assigning the new value to *reference), so now
3300
we can return from this function.
3308
/* Search in SELECT and GROUP lists of the outer select. */
3309
if (place != IN_WHERE && place != IN_ON)
3311
if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
3312
return -1; /* Some error occurred (e.g. ambiguous names). */
3313
if (ref != not_found_item)
3315
assert(*ref && (*ref)->fixed);
3316
prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
3317
prev_subselect_item->const_item_cache&= (*ref)->const_item();
3323
Reference is not found in this select => this subquery depend on
3324
outer select (or we just trying to find wrong identifier, in this
3325
case it does not matter which used tables bits we set)
3327
prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
3328
prev_subselect_item->const_item_cache= 0;
3334
if (ref == not_found_item && *from_field == not_found_field)
3338
// We can't say exactly what absent table or field
3339
my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), session->where);
3343
/* Call find_field_in_tables only to report the error */
3344
find_field_in_tables(session, this,
3345
context->first_name_resolution_table,
3346
context->last_name_resolution_table,
3347
reference, REPORT_ALL_ERRORS,
3353
else if (ref != not_found_item)
3358
/* Should have been checked in resolve_ref_in_select_and_group(). */
3359
assert(*ref && (*ref)->fixed);
3361
Here, a subset of actions performed by Item_ref::set_properties
3362
is not enough. So we pass ptr to NULL into Item_[direct]_ref
3363
constructor, so no initialization is performed, and call
3367
*ref= NULL; // Don't call set_properties()
3368
rf= (place == IN_HAVING ?
3369
new Item_ref(context, ref, (char*) table_name,
3370
(char*) field_name, alias_name_used) :
3371
(!select->group_list.elements ?
3372
new Item_direct_ref(context, ref, (char*) table_name,
3373
(char*) field_name, alias_name_used) :
3374
new Item_outer_ref(context, ref, (char*) table_name,
3375
(char*) field_name, alias_name_used)));
3380
if (place != IN_HAVING && select->group_list.elements)
3382
outer_context->select_lex->inner_refs_list.push_back((Item_outer_ref*)rf);
3383
((Item_outer_ref*)rf)->in_sum_func= session->lex->in_sum_func;
3385
session->change_item_tree(reference, rf);
3387
rf is Item_ref => never substitute other items (in this case)
3388
during fix_fields() => we can use rf after fix_fields()
3390
assert(!rf->fixed); // Assured by Item_ref()
3391
if (rf->fix_fields(session, reference) || rf->check_cols(1))
3394
mark_as_dependent(session, last_checked_context->select_lex,
3395
context->select_lex, this,
3401
mark_as_dependent(session, last_checked_context->select_lex,
3402
context->select_lex,
3403
this, (Item_ident*)*reference);
3404
if (last_checked_context->select_lex->having_fix_field)
3407
rf= new Item_ref(context,
3408
(cached_table->db[0] ? cached_table->db : 0),
3409
(char*) cached_table->alias, (char*) field_name);
3412
session->change_item_tree(reference, rf);
3414
rf is Item_ref => never substitute other items (in this case)
3415
during fix_fields() => we can use rf after fix_fields()
3417
assert(!rf->fixed); // Assured by Item_ref()
3418
if (rf->fix_fields(session, reference) || rf->check_cols(1))
3428
Resolve the name of a column reference.
3430
The method resolves the column reference represented by 'this' as a column
3431
present in one of: FROM clause, SELECT clause, GROUP BY clause of a query
3432
Q, or in outer queries that contain Q.
3434
The name resolution algorithm used is (where [T_j] is an optional table
3435
name that qualifies the column name):
3438
resolve_column_reference([T_j].col_ref_i)
3440
search for a column or derived column named col_ref_i
3441
[in table T_j] in the FROM clause of Q;
3443
if such a column is NOT found AND // Lookup in outer queries.
3444
there are outer queries
3446
for each outer query Q_k beginning from the inner-most one
3448
search for a column or derived column named col_ref_i
3449
[in table T_j] in the FROM clause of Q_k;
3451
if such a column is not found
3452
Search for a column or derived column named col_ref_i
3453
[in table T_j] in the SELECT and GROUP clauses of Q_k.
3459
Notice that compared to Item_ref::fix_fields, here we first search the FROM
3460
clause, and then we search the SELECT and GROUP BY clauses.
3462
@param[in] session current thread
3463
@param[in,out] reference view column if this item was resolved to a
3472
bool Item_field::fix_fields(Session *session, Item **reference)
3475
Field *from_field= (Field *)not_found_field;
3476
bool outer_fixed= false;
3478
if (!field) // If field is not checked
3481
In case of view, find_field_in_tables() write pointer to view field
3482
expression to 'reference', i.e. it substitute that expression instead
3485
if ((from_field= find_field_in_tables(session, this,
3486
context->first_name_resolution_table,
3487
context->last_name_resolution_table,
3489
session->lex->use_only_table_context ?
3491
IGNORE_EXCEPT_NON_UNIQUE,
3497
/* Look up in current select's item_list to find aliased fields */
3498
if (session->lex->current_select->is_item_list_lookup)
3501
enum_resolution_type resolution;
3502
Item** res= find_item_in_list(this, session->lex->current_select->item_list,
3503
&counter, REPORT_EXCEPT_NOT_FOUND,
3507
if (resolution == RESOLVED_AGAINST_ALIAS)
3508
alias_name_used= true;
3509
if (res != (Item **)not_found_item)
3511
if ((*res)->type() == Item::FIELD_ITEM)
3514
It's an Item_field referencing another Item_field in the select
3516
Use the field from the Item_field in the select list and leave
3517
the Item_field instance in place.
3520
Field *new_field= (*((Item_field**)res))->field;
3522
if (new_field == NULL)
3524
/* The column to which we link isn't valid. */
3525
my_error(ER_BAD_FIELD_ERROR, MYF(0), (*res)->name,
3526
current_session->where);
3530
set_field(new_field);
3536
It's not an Item_field in the select list so we must make a new
3537
Item_ref to point to the Item in the select list and replace the
3538
Item_field created by the parser with the new Item_ref.
3540
Item_ref *rf= new Item_ref(context, db_name,table_name,field_name);
3543
session->change_item_tree(reference, rf);
3545
Because Item_ref never substitutes itself with other items
3546
in Item_ref::fix_fields(), we can safely use the original
3547
pointer to it even after fix_fields()
3549
return rf->fix_fields(session, reference) || rf->check_cols(1);
3553
if ((ret= fix_outer_field(session, &from_field, reference)) < 0)
3557
goto mark_non_agg_field;
3559
else if (!from_field)
3562
if (!outer_fixed && cached_table && cached_table->select_lex &&
3563
context->select_lex &&
3564
cached_table->select_lex != context->select_lex)
3567
if ((ret= fix_outer_field(session, &from_field, reference)) < 0)
3571
goto mark_non_agg_field;
3575
if it is not expression from merged VIEW we will set this field.
3577
We can leave expression substituted from view for next PS/SP rexecution
3578
(i.e. do not register this substitution for reverting on cleanup()
3579
(register_item_tree_changing())), because this subtree will be
3580
fix_field'ed during setup_tables()->setup_underlying() (i.e. before
3581
all other expressions of query, and references on tables which do
3582
not present in query will not make problems.
3584
Also we suppose that view can't be changed during PS/SP life.
3586
if (from_field == view_ref_found)
3589
set_field(from_field);
3590
if (session->lex->in_sum_func &&
3591
session->lex->in_sum_func->nest_level ==
3592
session->lex->current_select->nest_level)
3593
set_if_bigger(session->lex->in_sum_func->max_arg_level,
3594
session->lex->current_select->nest_level);
3596
else if (session->mark_used_columns != MARK_COLUMNS_NONE)
3598
Table *table= field->table;
3599
MY_BITMAP *current_bitmap, *other_bitmap;
3600
if (session->mark_used_columns == MARK_COLUMNS_READ)
3602
current_bitmap= table->read_set;
3603
other_bitmap= table->write_set;
3607
current_bitmap= table->write_set;
3608
other_bitmap= table->read_set;
3610
if (!bitmap_fast_test_and_set(current_bitmap, field->field_index))
3612
if (!bitmap_is_set(other_bitmap, field->field_index))
3614
/* First usage of column */
3615
table->used_fields++; // Used to optimize loops
3616
/* purecov: begin inspected */
3617
table->covering_keys.intersect(field->part_of_key);
3627
context->process_error(session);
3631
Item *Item_field::safe_charset_converter(const CHARSET_INFO * const tocs)
3634
return Item::safe_charset_converter(tocs);
3638
void Item_field::cleanup()
3640
Item_ident::cleanup();
3642
Even if this object was created by direct link to field in setup_wild()
3643
it will be linked correctly next time by name of field and table alias.
3644
I.e. we can drop 'field'.
3646
field= result_field= 0;
3652
bool Item_field::result_as_int64_t()
3654
return field->can_be_compared_as_int64_t();
3659
Find a field among specified multiple equalities.
3661
The function first searches the field among multiple equalities
3662
of the current level (in the cond_equal->current_level list).
3663
If it fails, it continues searching in upper levels accessed
3664
through a pointer cond_equal->upper_levels.
3665
The search terminates as soon as a multiple equality containing
3668
@param cond_equal reference to list of multiple equalities where
3669
the field (this object) is to be looked for
3672
- First Item_equal containing the field, if success
3676
Item_equal *Item_field::find_item_equal(COND_EQUAL *cond_equal)
3678
Item_equal *item= 0;
3681
List_iterator_fast<Item_equal> li(cond_equal->current_level);
3682
while ((item= li++))
3684
if (item->contains(field))
3688
The field is not found in any of the multiple equalities
3689
of the current level. Look for it in upper levels
3691
cond_equal= cond_equal->upper_levels;
3698
Check whether a field can be substituted by an equal item.
3700
The function checks whether a substitution of the field
3701
occurrence for an equal item is valid.
3703
@param arg *arg != NULL <-> the field is in the context where
3704
substitution for an equal item is valid
3707
The following statement is not always true:
3711
This means substitution of an item for an equal item not always
3712
yields an equavalent condition. Here's an example:
3715
(LENGTH('a')=1) != (LENGTH('a ')=2)
3717
Such a substitution is surely valid if either the substituted
3718
field is not of a STRING type or if it is an argument of
3719
a comparison predicate.
3722
true substitution is valid
3727
bool Item_field::subst_argument_checker(unsigned char **arg)
3729
return (result_type() != STRING_RESULT) || (*arg);
3734
Set a pointer to the multiple equality the field reference belongs to
3737
The function looks for a multiple equality containing the field item
3738
among those referenced by arg.
3739
In the case such equality exists the function does the following.
3740
If the found multiple equality contains a constant, then the field
3741
reference is substituted for this constant, otherwise it sets a pointer
3742
to the multiple equality in the field item.
3745
@param arg reference to list of multiple equalities where
3746
the field (this object) is to be looked for
3749
This function is supposed to be called as a callback parameter in calls
3750
of the compile method.
3753
- pointer to the replacing constant item, if the field item was substituted
3754
- pointer to the field item, otherwise.
3757
Item *Item_field::equal_fields_propagator(unsigned char *arg)
3761
item_equal= find_item_equal((COND_EQUAL *) arg);
3764
item= item_equal->get_const();
3766
Disable const propagation for items used in different comparison contexts.
3767
This must be done because, for example, Item_hex_string->val_int() is not
3768
the same as (Item_hex_string->val_str() in BINARY column)->val_int().
3769
We cannot simply disable the replacement in a particular context (
3770
e.g. <bin_col> = <int_col> AND <bin_col> = <hex_string>) since
3771
Items don't know the context they are in and there are functions like
3772
IF (<hex_string>, 'yes', 'no').
3773
The same problem occurs when comparing a DATE/TIME field with a
3774
DATE/TIME represented as an int and as a string.
3777
(cmp_context != (Item_result)-1 && item->cmp_context != cmp_context))
3785
Mark the item to not be part of substitution if it's not a binary item.
3787
See comments in Arg_comparator::set_compare_func() for details.
3790
bool Item_field::set_no_const_sub(unsigned char *)
3792
if (field->charset() != &my_charset_bin)
3799
Replace an Item_field for an equal Item_field that evaluated earlier
3802
The function returns a pointer to an item that is taken from
3803
the very beginning of the item_equal list which the Item_field
3804
object refers to (belongs to) unless item_equal contains a constant
3805
item. In this case the function returns this constant item,
3806
(if the substitution does not require conversion).
3807
If the Item_field object does not refer any Item_equal object
3808
'this' is returned .
3810
@param arg a dummy parameter, is not used here
3814
This function is supposed to be called as a callback parameter in calls
3815
of the thransformer method.
3818
- pointer to a replacement Item_field if there is a better equal item or
3819
a pointer to a constant equal item;
3823
Item *Item_field::replace_equal_field(unsigned char *)
3827
Item *const_item= item_equal->get_const();
3830
if (cmp_context != (Item_result)-1 &&
3831
const_item->cmp_context != cmp_context)
3835
Item_field *subst= item_equal->get_first();
3836
if (subst && !field->eq(subst->field))
3843
uint32_t Item_field::max_disp_length()
3845
return field->max_display_length();
3849
void Item::init_make_field(Send_field *tmp_field,
3850
enum enum_field_types field_type_arg)
3852
char *empty_name= (char*) "";
3853
tmp_field->db_name= empty_name;
3854
tmp_field->org_table_name= empty_name;
3855
tmp_field->org_col_name= empty_name;
3856
tmp_field->table_name= empty_name;
3857
tmp_field->col_name= name;
3858
tmp_field->charsetnr= collation.collation->number;
3859
tmp_field->flags= (maybe_null ? 0 : NOT_NULL_FLAG) |
3860
(my_binary_compare(collation.collation) ?
3862
tmp_field->type= field_type_arg;
3863
tmp_field->length=max_length;
3864
tmp_field->decimals=decimals;
3867
void Item::make_field(Send_field *tmp_field)
1006
void Item::init_make_field(SendField *tmp_field, enum_field_types field_type_arg)
1008
tmp_field->db_name= "";
1009
tmp_field->org_table_name= "";
1010
tmp_field->org_col_name= "";
1011
tmp_field->table_name= "";
1012
tmp_field->col_name= name;
1013
tmp_field->charsetnr= collation.collation->number;
1014
tmp_field->flags= (maybe_null ? 0 : NOT_NULL_FLAG) | (my_binary_compare(collation.collation) ? BINARY_FLAG : 0);
1015
tmp_field->type= field_type_arg;
1016
tmp_field->length= max_length;
1017
tmp_field->decimals= decimals;
1020
void Item::make_field(SendField *tmp_field)
3869
1022
init_make_field(tmp_field, field_type());
3873
1025
enum_field_types Item::string_field_type() const
3875
1027
enum_field_types f_type= DRIZZLE_TYPE_VARCHAR;
4251
int Item_string::save_in_field(Field *field, bool)
4254
result=val_str(&str_value);
4255
return save_str_value_in_field(field, result);
4259
int Item_uint::save_in_field(Field *field, bool no_conversions)
4261
/* Item_int::save_in_field handles both signed and unsigned. */
4262
return Item_int::save_in_field(field, no_conversions);
4266
int Item_int::save_in_field(Field *field, bool)
4268
int64_t nr=val_int();
4270
return set_field_to_null(field);
4271
field->set_notnull();
4272
return field->store(nr, unsigned_flag);
4276
int Item_decimal::save_in_field(Field *field, bool)
4278
field->set_notnull();
4279
return field->store_decimal(&decimal_value);
4283
bool Item_int::eq(const Item *arg, bool) const
4285
/* No need to check for null value as basic constant can't be NULL */
4286
if (arg->basic_const_item() && arg->type() == type())
4289
We need to cast off const to call val_int(). This should be OK for
4292
Item *item= (Item*) arg;
4293
return item->val_int() == value && item->unsigned_flag == unsigned_flag;
4299
Item *Item_int_with_ref::clone_item()
4301
assert(ref->const_item());
4303
We need to evaluate the constant to make sure it works with
4306
return (ref->unsigned_flag ?
4307
new Item_uint(ref->name, ref->val_int(), ref->max_length) :
4308
new Item_int(ref->name, ref->val_int(), ref->max_length));
4312
Item_num *Item_uint::neg()
4314
Item_decimal *item= new Item_decimal(value, 1);
4319
static uint32_t nr_of_decimals(const char *str, const char *end)
4321
const char *decimal_point;
4323
/* Find position for '.' */
4328
if (*str == 'e' || *str == 'E')
4329
return NOT_FIXED_DEC;
4334
for (; my_isdigit(system_charset_info, *str) ; str++)
4336
if (*str == 'e' || *str == 'E')
4337
return NOT_FIXED_DEC;
4338
return (uint) (str - decimal_point);
4343
This function is only called during parsing. We will signal an error if
4344
value is not a true double value (overflow)
4347
Item_float::Item_float(const char *str_arg, uint32_t length)
4351
value= my_strntod(&my_charset_bin, (char*) str_arg, length, &end_not_used,
4356
Note that we depend on that str_arg is null terminated, which is true
4357
when we are in the parser
4359
assert(str_arg[length] == 0);
4360
my_error(ER_ILLEGAL_VALUE_FOR_TYPE, MYF(0), "double", (char*) str_arg);
4362
presentation= name=(char*) str_arg;
4363
decimals=(uint8_t) nr_of_decimals(str_arg, str_arg+length);
4369
int Item_float::save_in_field(Field *field, bool)
4371
double nr= val_real();
4373
return set_field_to_null(field);
4374
field->set_notnull();
4375
return field->store(nr);
4379
void Item_float::print(String *str, enum_query_type)
4383
str->append(presentation);
4387
String num(buffer, sizeof(buffer), &my_charset_bin);
4388
num.set_real(value, decimals, &my_charset_bin);
4395
In string context this is a binary string.
4396
In number context this is a int64_t value.
4399
bool Item_float::eq(const Item *arg, bool) const
4401
if (arg->basic_const_item() && arg->type() == type())
4404
We need to cast off const to call val_int(). This should be OK for
4407
Item *item= (Item*) arg;
4408
return item->val_real() == value;
4414
inline uint32_t char_val(char X)
4416
return (uint) (X >= '0' && X <= '9' ? X-'0' :
4417
X >= 'A' && X <= 'Z' ? X-'A'+10 :
4422
Item_hex_string::Item_hex_string(const char *str, uint32_t str_length)
4424
max_length=(str_length+1)/2;
4425
char *ptr=(char*) sql_alloc(max_length+1);
4428
str_value.set(ptr,max_length,&my_charset_bin);
4429
char *end=ptr+max_length;
4430
if (max_length*2 != str_length)
4431
*ptr++=char_val(*str++); // Not even, assume 0 prefix
4434
*ptr++= (char) (char_val(str[0])*16+char_val(str[1]));
4437
*ptr=0; // Keep purify happy
4438
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
4443
int64_t Item_hex_string::val_int()
4445
// following assert is redundant, because fixed=1 assigned in constructor
4447
char *end=(char*) str_value.ptr()+str_value.length(),
4448
*ptr=end-cmin(str_value.length(),(uint32_t)sizeof(int64_t));
4451
for (; ptr != end ; ptr++)
4452
value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
4453
return (int64_t) value;
4457
my_decimal *Item_hex_string::val_decimal(my_decimal *decimal_value)
4459
// following assert is redundant, because fixed=1 assigned in constructor
4461
uint64_t value= (uint64_t)val_int();
4462
int2my_decimal(E_DEC_FATAL_ERROR, value, true, decimal_value);
4463
return (decimal_value);
4467
int Item_hex_string::save_in_field(Field *field, bool)
4469
field->set_notnull();
4470
if (field->result_type() == STRING_RESULT)
4471
return field->store(str_value.ptr(), str_value.length(),
4472
collation.collation);
4475
uint32_t length= str_value.length();
4478
nr= field->flags & UNSIGNED_FLAG ? UINT64_MAX : INT64_MAX;
4481
nr= (uint64_t) val_int();
4482
if ((length == 8) && !(field->flags & UNSIGNED_FLAG) && (nr > INT64_MAX))
4487
return field->store((int64_t) nr, true); // Assume hex numbers are unsigned
4490
if (!field->store((int64_t) nr, true))
4491
field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE,
4497
void Item_hex_string::print(String *str, enum_query_type)
4499
char *end= (char*) str_value.ptr() + str_value.length(),
4500
*ptr= end - cmin(str_value.length(), (uint32_t)sizeof(int64_t));
4502
for (; ptr != end ; ptr++)
4504
str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
4505
str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
4510
bool Item_hex_string::eq(const Item *arg, bool binary_cmp) const
4512
if (arg->basic_const_item() && arg->type() == type())
4515
return !stringcmp(&str_value, &arg->str_value);
4516
return !sortcmp(&str_value, &arg->str_value, collation.collation);
4522
Item *Item_hex_string::safe_charset_converter(const CHARSET_INFO * const tocs)
4525
String tmp, *str= val_str(&tmp);
4527
if (!(conv= new Item_string(str->ptr(), str->length(), tocs)))
4529
conv->str_value.copy();
4530
conv->str_value.mark_as_const();
4537
In string context this is a binary string.
4538
In number context this is a int64_t value.
4541
Item_bin_string::Item_bin_string(const char *str, uint32_t str_length)
4543
const char *end= str + str_length - 1;
4544
unsigned char bits= 0;
4547
max_length= (str_length + 7) >> 3;
4548
char *ptr= (char*) sql_alloc(max_length + 1);
4551
str_value.set(ptr, max_length, &my_charset_bin);
4552
ptr+= max_length - 1;
4553
ptr[1]= 0; // Set end null for string
4554
for (; end >= str; end--)
4567
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
4573
Pack data in buffer for sending.
4576
bool Item_null::send(Protocol *protocol,
4579
return protocol->store_null();
4583
This is only called from items that is not of type item_field.
4586
bool Item::send(Protocol *protocol, String *buffer)
4589
enum_field_types f_type;
4591
switch ((f_type=field_type())) {
1261
Check if an item is a constant one and can be cached.
1263
@param arg [out] TRUE <=> Cache this item.
1265
@return TRUE Go deeper in item tree.
1266
@return FALSE Don't go deeper in item tree.
1269
bool Item::cache_const_expr_analyzer(unsigned char **arg)
1271
bool *cache_flag= (bool*)*arg;
1274
Item *item= real_item();
1276
Cache constant items unless it's a basic constant, constant field or
1277
a subselect (they use their own cache).
1280
!(item->basic_const_item() || item->type() == Item::FIELD_ITEM ||
1281
item->type() == SUBSELECT_ITEM ||
1283
Do not cache GET_USER_VAR() function as its const_item() may
1284
return TRUE for the current thread but it still may change
1285
during the execution.
1287
(item->type() == Item::FUNC_ITEM &&
1288
((Item_func*)item)->functype() == Item_func::GUSERVAR_FUNC)))
1296
Cache item if needed.
1298
@param arg TRUE <=> Cache this item.
1300
@return cache if cache needed.
1301
@return this otherwise.
1304
Item* Item::cache_const_expr_transformer(unsigned char *arg)
1308
*((bool*)arg)= false;
1309
Item_cache *cache= Item_cache::get_cache(this);
1319
void Item::send(plugin::Client *client, String *buffer)
1321
switch (field_type())
1323
case DRIZZLE_TYPE_DATE:
4593
1324
case DRIZZLE_TYPE_NULL:
4594
1325
case DRIZZLE_TYPE_ENUM:
4595
1326
case DRIZZLE_TYPE_BLOB:
4596
1327
case DRIZZLE_TYPE_VARCHAR:
4597
case DRIZZLE_TYPE_NEWDECIMAL:
4600
if ((res=val_str(buffer)))
4601
result= protocol->store(res->ptr(),res->length(),res->charset());
1328
case DRIZZLE_TYPE_BOOLEAN:
1329
case DRIZZLE_TYPE_UUID:
1330
case DRIZZLE_TYPE_IPV6:
1331
case DRIZZLE_TYPE_DECIMAL:
1333
if (String* res=val_str(buffer))
1334
client->store(res->ptr(), res->length());
4604
1337
case DRIZZLE_TYPE_LONG:
4609
result= protocol->store_long(nr);
1339
int64_t nr= val_int();
1341
client->store((int32_t)nr);
4612
1344
case DRIZZLE_TYPE_LONGLONG:
4617
result= protocol->store_int64_t(nr, unsigned_flag);
1346
int64_t nr= val_int();
1350
client->store((uint64_t)nr);
1352
client->store((int64_t)nr);
4620
1356
case DRIZZLE_TYPE_DOUBLE:
4622
double nr= val_real();
4624
result= protocol->store(nr, decimals, buffer);
1358
double nr= val_real();
1360
client->store(nr, decimals, buffer);
1363
case DRIZZLE_TYPE_TIME:
4627
1371
case DRIZZLE_TYPE_DATETIME:
1372
case DRIZZLE_TYPE_MICROTIME:
4628
1373
case DRIZZLE_TYPE_TIMESTAMP:
4631
get_date(&tm, TIME_FUZZY_DATE);
4634
if (f_type == DRIZZLE_TYPE_DATE)
4635
return protocol->store_date(&tm);
4637
result= protocol->store(&tm);
1376
get_date(tm, TIME_FUZZY_DATE);
4641
case DRIZZLE_TYPE_TIME:
4646
result= protocol->store_time(&tm);
4650
1382
if (null_value)
4651
result= protocol->store_null();
4656
bool Item_field::send(Protocol *protocol, String *)
4658
return protocol->store(result_field);
4662
void Item_field::update_null_value()
4665
need to set no_errors to prevent warnings about type conversion
4668
Session *session= field->table->in_use;
4671
no_errors= session->no_errors;
4672
session->no_errors= 1;
4673
Item::update_null_value();
4674
session->no_errors= no_errors;
4679
Add the field to the select list and substitute it for the reference to
4683
Item_field::update_value_transformer()
4684
select_arg current select
4687
If the field doesn't belong to the table being inserted into then it is
4688
added to the select list, pointer to it is stored in the ref_pointer_array
4689
of the select and the field itself is substituted for the Item_ref object.
4690
This is done in order to get correct values from update fields that
4691
belongs to the SELECT part in the INSERT .. SELECT .. ON DUPLICATE KEY
4696
ref if all conditions are met
4697
this field otherwise
4700
Item *Item_field::update_value_transformer(unsigned char *select_arg)
4702
SELECT_LEX *select= (SELECT_LEX*)select_arg;
4705
if (field->table != select->context.table_list->table)
4707
List<Item> *all_fields= &select->join->all_fields;
4708
Item **ref_pointer_array= select->ref_pointer_array;
4709
int el= all_fields->elements;
4712
ref_pointer_array[el]= (Item*)this;
4713
all_fields->push_front((Item*)this);
4714
ref= new Item_ref(&select->context, ref_pointer_array + el,
4715
table_name, field_name);
4722
void Item_field::print(String *str, enum_query_type query_type)
4724
if (field && field->table->const_table)
4726
char buff[MAX_FIELD_WIDTH];
4727
String tmp(buff,sizeof(buff),str->charset());
4728
field->val_str(&tmp);
4734
Item_ident::print(str, query_type);
4738
Item_ref::Item_ref(Name_resolution_context *context_arg,
4739
Item **item, const char *table_name_arg,
4740
const char *field_name_arg,
4741
bool alias_name_used_arg)
4742
:Item_ident(context_arg, NULL, table_name_arg, field_name_arg),
4743
result_field(0), ref(item)
4745
alias_name_used= alias_name_used_arg;
4747
This constructor used to create some internals references over fixed items
4749
if (ref && *ref && (*ref)->fixed)
4755
Resolve the name of a reference to a column reference.
4757
The method resolves the column reference represented by 'this' as a column
4758
present in one of: GROUP BY clause, SELECT clause, outer queries. It is
4759
used typically for columns in the HAVING clause which are not under
4760
aggregate functions.
4763
Item_ref::ref is 0 or points to a valid item.
4766
The name resolution algorithm used is (where [T_j] is an optional table
4767
name that qualifies the column name):
4770
resolve_extended([T_j].col_ref_i)
4772
Search for a column or derived column named col_ref_i [in table T_j]
4773
in the SELECT and GROUP clauses of Q.
4775
if such a column is NOT found AND // Lookup in outer queries.
4776
there are outer queries
4778
for each outer query Q_k beginning from the inner-most one
4780
Search for a column or derived column named col_ref_i
4781
[in table T_j] in the SELECT and GROUP clauses of Q_k.
4783
if such a column is not found AND
4784
- Q_k is not a group query AND
4785
- Q_k is not inside an aggregate function
4787
- Q_(k-1) is not in a HAVING or SELECT clause of Q_k
4789
search for a column or derived column named col_ref_i
4790
[in table T_j] in the FROM clause of Q_k;
4797
This procedure treats GROUP BY and SELECT clauses as one namespace for
4798
column references in HAVING. Notice that compared to
4799
Item_field::fix_fields, here we first search the SELECT and GROUP BY
4800
clauses, and then we search the FROM clause.
4802
@param[in] session current thread
4803
@param[in,out] reference view column if this item was resolved to a
4807
Here we could first find the field anyway, and then test this
4808
condition, so that we can give a better error message -
4809
ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
4810
ER_BAD_FIELD_ERROR which we produce now.
4818
bool Item_ref::fix_fields(Session *session, Item **reference)
4820
enum_parsing_place place= NO_MATTER;
4822
SELECT_LEX *current_sel= session->lex->current_select;
4824
if (!ref || ref == not_found_item)
4826
if (!(ref= resolve_ref_in_select_and_group(session, this,
4827
context->select_lex)))
4828
goto error; /* Some error occurred (e.g. ambiguous names). */
4830
if (ref == not_found_item) /* This reference was not resolved. */
4832
Name_resolution_context *last_checked_context= context;
4833
Name_resolution_context *outer_context= context->outer_context;
4839
/* The current reference cannot be resolved in this query. */
4840
my_error(ER_BAD_FIELD_ERROR,MYF(0),
4841
this->full_name(), current_session->where);
4846
If there is an outer context (select), and it is not a derived table
4847
(which do not support the use of outer fields for now), try to
4848
resolve this reference in the outer select(s).
4850
We treat each subselect as a separate namespace, so that different
4851
subselects may contain columns with the same names. The subselects are
4852
searched starting from the innermost.
4854
from_field= (Field*) not_found_field;
4858
SELECT_LEX *select= outer_context->select_lex;
4859
Item_subselect *prev_subselect_item=
4860
last_checked_context->select_lex->master_unit()->item;
4861
last_checked_context= outer_context;
4863
/* Search in the SELECT and GROUP lists of the outer select. */
4864
if (outer_context->resolve_in_select_list)
4866
if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
4867
goto error; /* Some error occurred (e.g. ambiguous names). */
4868
if (ref != not_found_item)
4870
assert(*ref && (*ref)->fixed);
4871
prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
4872
prev_subselect_item->const_item_cache&= (*ref)->const_item();
4876
Set ref to 0 to ensure that we get an error in case we replaced
4877
this item with another item and still use this item in some
4878
other place of the parse tree.
4883
place= prev_subselect_item->parsing_place;
4885
Check table fields only if the subquery is used somewhere out of
4886
HAVING or the outer SELECT does not use grouping (i.e. tables are
4889
Here we could first find the field anyway, and then test this
4890
condition, so that we can give a better error message -
4891
ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
4892
ER_BAD_FIELD_ERROR which we produce now.
4894
if ((place != IN_HAVING ||
4895
(!select->with_sum_func &&
4896
select->group_list.elements == 0)))
4899
In case of view, find_field_in_tables() write pointer to view
4900
field expression to 'reference', i.e. it substitute that
4901
expression instead of this Item_ref
4903
from_field= find_field_in_tables(session, this,
4905
first_name_resolution_table,
4907
last_name_resolution_table,
4909
IGNORE_EXCEPT_NON_UNIQUE,
4913
if (from_field == view_ref_found)
4915
Item::Type refer_type= (*reference)->type();
4916
prev_subselect_item->used_tables_cache|=
4917
(*reference)->used_tables();
4918
prev_subselect_item->const_item_cache&=
4919
(*reference)->const_item();
4920
assert((*reference)->type() == REF_ITEM);
4921
mark_as_dependent(session, last_checked_context->select_lex,
4922
context->select_lex, this,
4923
((refer_type == REF_ITEM ||
4924
refer_type == FIELD_ITEM) ?
4925
(Item_ident*) (*reference) :
4928
view reference found, we substituted it instead of this
4933
if (from_field != not_found_field)
4935
if (cached_table && cached_table->select_lex &&
4936
outer_context->select_lex &&
4937
cached_table->select_lex != outer_context->select_lex)
4940
Due to cache, find_field_in_tables() can return field which
4941
doesn't belong to provided outer_context. In this case we have
4942
to find proper field context in order to fix field correcly.
4946
outer_context= outer_context->outer_context;
4947
select= outer_context->select_lex;
4948
prev_subselect_item=
4949
last_checked_context->select_lex->master_unit()->item;
4950
last_checked_context= outer_context;
4951
} while (outer_context && outer_context->select_lex &&
4952
cached_table->select_lex != outer_context->select_lex);
4954
prev_subselect_item->used_tables_cache|= from_field->table->map;
4955
prev_subselect_item->const_item_cache= 0;
4959
assert(from_field == not_found_field);
4961
/* Reference is not found => depend on outer (or just error). */
4962
prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
4963
prev_subselect_item->const_item_cache= 0;
4965
outer_context= outer_context->outer_context;
4966
} while (outer_context);
4968
assert(from_field != 0 && from_field != view_ref_found);
4969
if (from_field != not_found_field)
4972
if (!(fld= new Item_field(from_field)))
4974
session->change_item_tree(reference, fld);
4975
mark_as_dependent(session, last_checked_context->select_lex,
4976
session->lex->current_select, this, fld);
4978
A reference is resolved to a nest level that's outer or the same as
4979
the nest level of the enclosing set function : adjust the value of
4980
max_arg_level for the function if it's needed.
4982
if (session->lex->in_sum_func &&
4983
session->lex->in_sum_func->nest_level >=
4984
last_checked_context->select_lex->nest_level)
4985
set_if_bigger(session->lex->in_sum_func->max_arg_level,
4986
last_checked_context->select_lex->nest_level);
4991
/* The item was not a table field and not a reference */
4992
my_error(ER_BAD_FIELD_ERROR, MYF(0),
4993
this->full_name(), current_session->where);
4996
/* Should be checked in resolve_ref_in_select_and_group(). */
4997
assert(*ref && (*ref)->fixed);
4998
mark_as_dependent(session, last_checked_context->select_lex,
4999
context->select_lex, this, this);
5001
A reference is resolved to a nest level that's outer or the same as
5002
the nest level of the enclosing set function : adjust the value of
5003
max_arg_level for the function if it's needed.
5005
if (session->lex->in_sum_func &&
5006
session->lex->in_sum_func->nest_level >=
5007
last_checked_context->select_lex->nest_level)
5008
set_if_bigger(session->lex->in_sum_func->max_arg_level,
5009
last_checked_context->select_lex->nest_level);
5015
Check if this is an incorrect reference in a group function or forward
5016
reference. Do not issue an error if this is:
5017
1. outer reference (will be fixed later by the fix_inner_refs function);
5018
2. an unnamed reference inside an aggregate function.
5020
if (!((*ref)->type() == REF_ITEM &&
5021
((Item_ref *)(*ref))->ref_type() == OUTER_REF) &&
5022
(((*ref)->with_sum_func && name &&
5023
!(current_sel->linkage != GLOBAL_OPTIONS_TYPE &&
5024
current_sel->having_fix_field)) ||
5027
my_error(ER_ILLEGAL_REFERENCE, MYF(0),
5028
name, ((*ref)->with_sum_func?
5029
"reference to group function":
5030
"forward reference in item list"));
5036
if ((*ref)->check_cols(1))
5041
context->process_error(session);
5046
void Item_ref::set_properties()
5048
max_length= (*ref)->max_length;
5049
maybe_null= (*ref)->maybe_null;
5050
decimals= (*ref)->decimals;
5051
collation.set((*ref)->collation);
5053
We have to remember if we refer to a sum function, to ensure that
5054
split_sum_func() doesn't try to change the reference.
5056
with_sum_func= (*ref)->with_sum_func;
5057
unsigned_flag= (*ref)->unsigned_flag;
5059
if (alias_name_used)
5061
if ((*ref)->type() == FIELD_ITEM)
5062
alias_name_used= ((Item_ident *) (*ref))->alias_name_used;
5064
alias_name_used= true; // it is not field, so it is was resolved by alias
5068
void Item_ref::cleanup()
5070
Item_ident::cleanup();
5076
void Item_ref::print(String *str, enum_query_type query_type)
5080
if ((*ref)->type() != Item::CACHE_ITEM &&
5081
!table_name && name && alias_name_used)
5083
Session *session= current_session;
5084
append_identifier(session, str, name, (uint) strlen(name));
5087
(*ref)->print(str, query_type);
5090
Item_ident::print(str, query_type);
5094
bool Item_ref::send(Protocol *prot, String *tmp)
5097
return prot->store(result_field);
5098
return (*ref)->send(prot, tmp);
5102
double Item_ref::val_result()
5106
if ((null_value= result_field->is_null()))
5108
return result_field->val_real();
5114
int64_t Item_ref::val_int_result()
5118
if ((null_value= result_field->is_null()))
5120
return result_field->val_int();
5126
String *Item_ref::str_result(String* str)
5130
if ((null_value= result_field->is_null()))
5132
str->set_charset(str_value.charset());
5133
return result_field->val_str(str, &str_value);
5135
return val_str(str);
5139
my_decimal *Item_ref::val_decimal_result(my_decimal *decimal_value)
5143
if ((null_value= result_field->is_null()))
5145
return result_field->val_decimal(decimal_value);
5147
return val_decimal(decimal_value);
5151
bool Item_ref::val_bool_result()
5155
if ((null_value= result_field->is_null()))
5157
switch (result_field->result_type()) {
5159
return result_field->val_int() != 0;
5160
case DECIMAL_RESULT:
5162
my_decimal decimal_value;
5163
my_decimal *val= result_field->val_decimal(&decimal_value);
5165
return !my_decimal_is_zero(val);
5170
return result_field->val_real() != 0.0;
5180
double Item_ref::val_real()
5183
double tmp=(*ref)->val_result();
5184
null_value=(*ref)->null_value;
5189
int64_t Item_ref::val_int()
5192
int64_t tmp=(*ref)->val_int_result();
5193
null_value=(*ref)->null_value;
5198
bool Item_ref::val_bool()
5201
bool tmp= (*ref)->val_bool_result();
5202
null_value= (*ref)->null_value;
5207
String *Item_ref::val_str(String* tmp)
5210
tmp=(*ref)->str_result(tmp);
5211
null_value=(*ref)->null_value;
5216
bool Item_ref::is_null()
5219
return (*ref)->is_null();
5223
bool Item_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
5225
return (null_value=(*ref)->get_date_result(ltime,fuzzydate));
5229
my_decimal *Item_ref::val_decimal(my_decimal *decimal_value)
5231
my_decimal *val= (*ref)->val_decimal_result(decimal_value);
5232
null_value= (*ref)->null_value;
5236
int Item_ref::save_in_field(Field *to, bool no_conversions)
5239
assert(!result_field);
5240
res= (*ref)->save_in_field(to, no_conversions);
5241
null_value= (*ref)->null_value;
5246
void Item_ref::save_org_in_field(Field *field)
5248
(*ref)->save_org_in_field(field);
5252
void Item_ref::make_field(Send_field *field)
5254
(*ref)->make_field(field);
5255
/* Non-zero in case of a view */
5257
field->col_name= name;
5259
field->table_name= table_name;
5261
field->db_name= db_name;
5265
Item *Item_ref::get_tmp_table_item(Session *session)
5268
return (*ref)->get_tmp_table_item(session);
5270
Item_field *item= new Item_field(result_field);
5273
item->table_name= table_name;
5274
item->db_name= db_name;
5280
void Item_ref_null_helper::print(String *str, enum_query_type query_type)
5282
str->append(STRING_WITH_LEN("<ref_null_helper>("));
5284
(*ref)->print(str, query_type);
5291
double Item_direct_ref::val_real()
5293
double tmp=(*ref)->val_real();
5294
null_value=(*ref)->null_value;
5299
int64_t Item_direct_ref::val_int()
5301
int64_t tmp=(*ref)->val_int();
5302
null_value=(*ref)->null_value;
5307
String *Item_direct_ref::val_str(String* tmp)
5309
tmp=(*ref)->val_str(tmp);
5310
null_value=(*ref)->null_value;
5315
my_decimal *Item_direct_ref::val_decimal(my_decimal *decimal_value)
5317
my_decimal *tmp= (*ref)->val_decimal(decimal_value);
5318
null_value=(*ref)->null_value;
5323
bool Item_direct_ref::val_bool()
5325
bool tmp= (*ref)->val_bool();
5326
null_value=(*ref)->null_value;
5331
bool Item_direct_ref::is_null()
5333
return (*ref)->is_null();
5337
bool Item_direct_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
5339
return (null_value=(*ref)->get_date(ltime,fuzzydate));
5343
Prepare referenced outer field then call usual Item_direct_ref::fix_fields
5346
Item_outer_ref::fix_fields()
5347
session thread handler
5348
reference reference on reference where this item stored
5355
bool Item_outer_ref::fix_fields(Session *session, Item **reference)
5358
/* outer_ref->check_cols() will be made in Item_direct_ref::fix_fields */
5359
if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(session, reference)))
5361
err= Item_direct_ref::fix_fields(session, reference);
5364
if ((*ref)->type() == Item::FIELD_ITEM)
5365
table_name= ((Item_field*)outer_ref)->table_name;
5369
void Item_outer_ref::fix_after_pullout(st_select_lex *new_parent, Item **ref)
5371
if (depended_from == new_parent)
5374
outer_ref->fix_after_pullout(new_parent, ref);
5378
void Item_ref::fix_after_pullout(st_select_lex *new_parent, Item **)
5380
if (depended_from == new_parent)
5382
(*ref)->fix_after_pullout(new_parent, ref);
5383
depended_from= NULL;
5387
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
5389
return item->type() == DEFAULT_VALUE_ITEM &&
5390
((Item_default_value *)item)->arg->eq(arg, binary_cmp);
5394
bool Item_default_value::fix_fields(Session *session, Item **)
5397
Item_field *field_arg;
5406
if (!arg->fixed && arg->fix_fields(session, &arg))
5410
real_arg= arg->real_item();
5411
if (real_arg->type() != FIELD_ITEM)
5413
my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
5417
field_arg= (Item_field *)real_arg;
5418
if (field_arg->field->flags & NO_DEFAULT_VALUE_FLAG)
5420
my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
5423
if (!(def_field= (Field*) sql_alloc(field_arg->field->size_of())))
5425
memcpy(def_field, field_arg->field, field_arg->field->size_of());
5426
def_field->move_field_offset((my_ptrdiff_t)
5427
(def_field->table->s->default_values -
5428
def_field->table->record[0]));
5429
set_field(def_field);
5433
context->process_error(session);
5438
void Item_default_value::print(String *str, enum_query_type query_type)
5442
str->append(STRING_WITH_LEN("default"));
5445
str->append(STRING_WITH_LEN("default("));
5446
arg->print(str, query_type);
5451
int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
5455
if (field_arg->flags & NO_DEFAULT_VALUE_FLAG)
5457
if (field_arg->reset())
5459
my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
5460
ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
5465
push_warning_printf(field_arg->table->in_use,
5466
DRIZZLE_ERROR::WARN_LEVEL_WARN,
5467
ER_NO_DEFAULT_FOR_FIELD,
5468
ER(ER_NO_DEFAULT_FOR_FIELD),
5469
field_arg->field_name);
5473
field_arg->set_default();
5476
return Item_field::save_in_field(field_arg, no_conversions);
5481
This method like the walk method traverses the item tree, but at the
5482
same time it can replace some nodes in the tree.
5485
Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
5487
Item *new_item= arg->transform(transformer, args);
5492
Session::change_item_tree() should be called only if the tree was
5493
really transformed, i.e. when a new item has been created.
5494
Otherwise we'll be allocating a lot of unnecessary memory for
5495
change records at each execution.
5497
if (arg != new_item)
5498
current_session->change_item_tree(&arg, new_item);
5499
return (this->*transformer)(args);
5503
bool Item_insert_value::eq(const Item *item, bool binary_cmp) const
5505
return item->type() == INSERT_VALUE_ITEM &&
5506
((Item_default_value *)item)->arg->eq(arg, binary_cmp);
5510
bool Item_insert_value::fix_fields(Session *session, Item **)
5513
/* We should only check that arg is in first table */
5517
TableList *orig_next_table= context->last_name_resolution_table;
5518
context->last_name_resolution_table= context->first_name_resolution_table;
5519
res= arg->fix_fields(session, &arg);
5520
context->last_name_resolution_table= orig_next_table;
5525
if (arg->type() == REF_ITEM)
5527
Item_ref *ref= (Item_ref *)arg;
5528
if (ref->ref[0]->type() != FIELD_ITEM)
5530
my_error(ER_BAD_FIELD_ERROR, MYF(0), "", "VALUES() function");
5536
According to our SQL grammar, VALUES() function can reference
5539
assert(arg->type() == FIELD_ITEM);
5541
Item_field *field_arg= (Item_field *)arg;
5543
if (field_arg->field->table->insert_values)
5545
Field *def_field= (Field*) sql_alloc(field_arg->field->size_of());
5548
memcpy(def_field, field_arg->field, field_arg->field->size_of());
5549
def_field->move_field_offset((my_ptrdiff_t)
5550
(def_field->table->insert_values -
5551
def_field->table->record[0]));
5552
set_field(def_field);
5556
Field *tmp_field= field_arg->field;
5557
/* charset doesn't matter here, it's to avoid sigsegv only */
5558
tmp_field= new Field_null(0, 0, Field::NONE, field_arg->field->field_name,
5562
tmp_field->init(field_arg->field->table);
5563
set_field(tmp_field);
5569
void Item_insert_value::print(String *str, enum_query_type query_type)
5571
str->append(STRING_WITH_LEN("values("));
5572
arg->print(str, query_type);
1386
uint32_t Item::max_char_length() const
1388
return max_length / collation.collation->mbmaxlen;
1391
void Item::fix_char_length(uint32_t max_char_length_arg)
1393
max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
5577
1396
Item_result item_cmp_type(Item_result a,Item_result b)
5579
1398
if (a == STRING_RESULT && b == STRING_RESULT)
5580
1399
return STRING_RESULT;
5581
1401
if (a == INT_RESULT && b == INT_RESULT)
5582
1402
return INT_RESULT;
5583
1403
else if (a == ROW_RESULT || b == ROW_RESULT)
5584
1404
return ROW_RESULT;
5585
1406
if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
5586
1407
(b == INT_RESULT || b == DECIMAL_RESULT))
5587
1408
return DECIMAL_RESULT;
5588
1410
return REAL_RESULT;
5592
1413
void resolve_const_item(Session *session, Item **ref, Item *comp_item)
5594
1415
Item *item= *ref;
5595
1416
Item *new_item= NULL;
5596
1417
if (item->basic_const_item())
5597
return; // Can't be better
5598
Item_result res_type=item_cmp_type(comp_item->result_type(),
5599
item->result_type());
5600
char *name=item->name; // Alloced by sql_alloc
1418
return; /* Can't be better */
1419
Item_result res_type=item_cmp_type(comp_item->result_type(), item->result_type());
1420
const char *name=item->name; /* Alloced by memory::sql_alloc */
5603
1424
case STRING_RESULT:
5605
char buff[MAX_FIELD_WIDTH];
5606
String tmp(buff,sizeof(buff),&my_charset_bin),*result;
5607
result=item->val_str(&tmp);
5608
if (item->null_value)
5609
new_item= new Item_null(name);
5612
uint32_t length= result->length();
5613
char *tmp_str= sql_strmake(result->ptr(), length);
5614
new_item= new Item_string(name, tmp_str, length, result->charset());
1426
char buff[MAX_FIELD_WIDTH];
1427
String tmp(buff,sizeof(buff),&my_charset_bin),*result;
1428
result=item->val_str(&tmp);
1429
if (item->null_value)
1430
new_item= new Item_null(name);
1432
new_item= new Item_string(name, memory::sql_strdup(*result), result->length(), result->charset());
5618
1435
case INT_RESULT:
5620
int64_t result=item->val_int();
5621
uint32_t length=item->max_length;
5622
bool null_value=item->null_value;
5623
new_item= (null_value ? (Item*) new Item_null(name) :
5624
(Item*) new Item_int(name, result, length));
1437
new_item= item->null_value ? (Item*)new Item_null(name) : (Item*)new Item_int(name, item->val_int(), item->max_length);
5627
1440
case ROW_RESULT:
5628
if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
5631
Substitute constants only in Item_rows. Don't affect other Items
5632
with ROW_RESULT (eg Item_singlerow_subselect).
1441
if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1444
Substitute constants only in Item_rows. Don't affect other Items
1445
with ROW_RESULT (eg Item_singlerow_subselect).
5634
For such Items more optimal is to detect if it is constant and replace
5635
it with Item_row. This would optimize queries like this:
5636
SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
5638
Item_row *item_row= (Item_row*) item;
5639
Item_row *comp_item_row= (Item_row*) comp_item;
5643
If item and comp_item are both Item_rows and have same number of cols
5644
then process items in Item_row one by one.
5645
We can't ignore NULL values here as this item may be used with <=>, in
5646
which case NULL's are significant.
5648
assert(item->result_type() == comp_item->result_type());
5649
assert(item_row->cols() == comp_item_row->cols());
5650
col= item_row->cols();
5652
resolve_const_item(session, item_row->addr(col),
5653
comp_item_row->element_index(col));
1447
For such Items more optimal is to detect if it is constant and replace
1448
it with Item_row. This would optimize queries like this:
1449
SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
1451
Item_row *item_row= (Item_row*) item;
1452
Item_row *comp_item_row= (Item_row*) comp_item;
1455
If item and comp_item are both Item_rows and have same number of cols
1456
then process items in Item_row one by one.
1457
We can't ignore NULL values here as this item may be used with <=>, in
1458
which case NULL's are significant.
1460
assert(item->result_type() == comp_item->result_type());
1461
assert(item_row->cols() == comp_item_row->cols());
1462
for (uint32_t col= item_row->cols(); col--; )
1463
resolve_const_item(session, item_row->addr(col), comp_item_row->element_index(col));
5657
1467
case REAL_RESULT:
5658
{ // It must REAL_RESULT
5659
double result= item->val_real();
5660
uint32_t length=item->max_length,decimals=item->decimals;
5661
bool null_value=item->null_value;
5662
new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
5663
new Item_float(name, result, decimals, length));
1468
{ // It must REAL_RESULT
1469
double result= item->val_real();
1470
uint32_t length=item->max_length,decimals=item->decimals;
1471
bool null_value=item->null_value;
1472
new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
1473
new Item_float(name, result, decimals, length));
5666
1476
case DECIMAL_RESULT:
5668
my_decimal decimal_value;
5669
my_decimal *result= item->val_decimal(&decimal_value);
5670
uint32_t length= item->max_length, decimals= item->decimals;
5671
bool null_value= item->null_value;
5672
new_item= (null_value ?
5673
(Item*) new Item_null(name) :
5674
(Item*) new Item_decimal(name, result, length, decimals));
1478
type::Decimal decimal_value;
1479
type::Decimal *result= item->val_decimal(&decimal_value);
1480
uint32_t length= item->max_length, decimals= item->decimals;
1481
bool null_value= item->null_value;
1482
new_item= (null_value ?
1483
(Item*) new Item_null(name) :
1484
(Item*) new Item_decimal(name, result, length, decimals));
5681
session->change_item_tree(ref, new_item);
5685
Return true if the value stored in the field is equal to the const
5688
We need to use this on the range optimizer because in some cases
5689
we can't store the value in the field without some precision/character loss.
5692
1493
bool field_is_equal_to_item(Field *field,Item *item)