forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.cc
10855 lines (9279 loc) · 293 KB
/
item.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "item.h"
#include "mysql.h" // IS_NUM
#include "aggregate_check.h" // Distinct_check
#include "auth_common.h" // get_column_grant
#include "item_cmpfunc.h" // COND_EQUAL
#include "item_create.h" // create_temporal_literal
#include "item_func.h" // item_func_sleep_init
#include "item_json_func.h" // json_value
#include "item_strfunc.h" // Item_func_conv_charset
#include "item_sum.h" // Item_sum
#include "json_dom.h" // Json_wrapper
#include "log_event.h" // append_query_string
#include "sp.h" // sp_map_item_type
#include "sp_rcontext.h" // sp_rcontext
#include "sql_base.h" // view_ref_found
#include "sql_class.h" // THD
#include "sql_show.h" // append_identifier
#include "sql_time.h" // Date_time_format
#include "sql_view.h" // VIEW_ANY_ACL
#include "template_utils.h"
using std::min;
using std::max;
const String my_null_string("NULL", 4, default_charset_info);
/**
Alias from select list can be referenced only from ORDER
BY (SQL Standard) or from HAVING and GROUP BY (MySQL
extension).
*/
static inline bool
select_alias_referencable(enum_parsing_context place)
{
return (place == CTX_HAVING || place == CTX_GROUP_BY ||
place == CTX_ORDER_BY);
}
/****************************************************************************/
/* Hybrid_type_traits {_real} */
void Hybrid_type_traits::fix_length_and_dec(Item *item, Item *arg) const
{
item->decimals= NOT_FIXED_DEC;
item->max_length= item->float_length(arg->decimals);
}
static const Hybrid_type_traits real_traits_instance;
const Hybrid_type_traits *Hybrid_type_traits::instance()
{
return &real_traits_instance;
}
my_decimal *
Hybrid_type_traits::val_decimal(Hybrid_type *val, my_decimal *to) const
{
double2my_decimal(E_DEC_FATAL_ERROR, val->real, val->dec_buf);
return val->dec_buf;
}
String *
Hybrid_type_traits::val_str(Hybrid_type *val, String *to, uint8 decimals) const
{
to->set_real(val->real, decimals, &my_charset_bin);
return to;
}
/* Hybrid_type_traits_decimal */
static const Hybrid_type_traits_decimal decimal_traits_instance;
const Hybrid_type_traits_decimal *Hybrid_type_traits_decimal::instance()
{
return &decimal_traits_instance;
}
void
Hybrid_type_traits_decimal::fix_length_and_dec(Item *item, Item *arg) const
{
item->decimals= arg->decimals;
item->max_length= min<uint32>(arg->max_length + DECIMAL_LONGLONG_DIGITS,
DECIMAL_MAX_STR_LENGTH);
}
void Hybrid_type_traits_decimal::set_zero(Hybrid_type *val) const
{
my_decimal_set_zero(&val->dec_buf[0]);
val->used_dec_buf_no= 0;
}
void Hybrid_type_traits_decimal::add(Hybrid_type *val, Field *f) const
{
my_decimal_add(E_DEC_FATAL_ERROR,
&val->dec_buf[val->used_dec_buf_no ^ 1],
&val->dec_buf[val->used_dec_buf_no],
f->val_decimal(&val->dec_buf[2]));
val->used_dec_buf_no^= 1;
}
/**
@todo
what is '4' for scale?
*/
void Hybrid_type_traits_decimal::div(Hybrid_type *val, ulonglong u) const
{
int2my_decimal(E_DEC_FATAL_ERROR, u, TRUE, &val->dec_buf[2]);
/* XXX: what is '4' for scale? */
my_decimal_div(E_DEC_FATAL_ERROR,
&val->dec_buf[val->used_dec_buf_no ^ 1],
&val->dec_buf[val->used_dec_buf_no],
&val->dec_buf[2], 4);
val->used_dec_buf_no^= 1;
}
longlong
Hybrid_type_traits_decimal::val_int(Hybrid_type *val, bool unsigned_flag) const
{
longlong result;
my_decimal2int(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
unsigned_flag, &result);
return result;
}
double
Hybrid_type_traits_decimal::val_real(Hybrid_type *val) const
{
my_decimal2double(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
&val->real);
return val->real;
}
String *
Hybrid_type_traits_decimal::val_str(Hybrid_type *val, String *to,
uint8 decimals) const
{
my_decimal_round(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
decimals, FALSE, &val->dec_buf[2]);
my_decimal2string(E_DEC_FATAL_ERROR, &val->dec_buf[2], 0, 0, 0, to);
return to;
}
/* Hybrid_type_traits_integer */
static const Hybrid_type_traits_integer integer_traits_instance;
const Hybrid_type_traits_integer *Hybrid_type_traits_integer::instance()
{
return &integer_traits_instance;
}
void
Hybrid_type_traits_integer::fix_length_and_dec(Item *item, Item *arg) const
{
item->decimals= 0;
item->max_length= MY_INT64_NUM_DECIMAL_DIGITS;
item->unsigned_flag= 0;
}
/*****************************************************************************
** Item functions
*****************************************************************************/
/**
Init all special items.
*/
void item_init(void)
{
item_func_sleep_init();
uuid_short_init();
}
/**
@todo
Make this functions class dependent
*/
bool Item::val_bool()
{
switch(result_type()) {
case INT_RESULT:
return val_int() != 0;
case DECIMAL_RESULT:
{
my_decimal decimal_value;
my_decimal *val= val_decimal(&decimal_value);
if (val)
return !my_decimal_is_zero(val);
return 0;
}
case REAL_RESULT:
case STRING_RESULT:
return val_real() != 0.0;
case ROW_RESULT:
default:
DBUG_ASSERT(0);
return 0; // Wrong (but safe)
}
}
/*
For the items which don't have its own fast val_str_ascii()
implementation we provide a generic slower version,
which converts from the Item character set to ASCII.
For better performance conversion happens only in
case of a "tricky" Item character set (e.g. UCS2).
Normally conversion does not happen.
*/
String *Item::val_str_ascii(String *str)
{
if (!(collation.collation->state & MY_CS_NONASCII))
return val_str(str);
DBUG_ASSERT(str != &str_value);
uint errors;
String *res= val_str(&str_value);
if (!res)
return 0;
if ((null_value= str->copy(res->ptr(), res->length(),
collation.collation, &my_charset_latin1,
&errors)))
return 0;
return str;
}
String *Item::val_string_from_real(String *str)
{
double nr= val_real();
if (null_value)
return 0; /* purecov: inspected */
str->set_real(nr,decimals, &my_charset_bin);
return str;
}
String *Item::val_string_from_int(String *str)
{
longlong nr= val_int();
if (null_value)
return 0;
str->set_int(nr, unsigned_flag, &my_charset_bin);
return str;
}
String *Item::val_string_from_decimal(String *str)
{
my_decimal dec_buf, *dec= val_decimal(&dec_buf);
if (null_value)
return 0;
my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, FALSE, &dec_buf);
my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, 0, str);
return str;
}
String *Item::val_string_from_datetime(String *str)
{
DBUG_ASSERT(fixed == 1);
MYSQL_TIME ltime;
if (get_date(<ime, TIME_FUZZY_DATE) ||
(null_value= str->alloc(MAX_DATE_STRING_REP_LENGTH)))
return (String *) 0;
make_datetime((Date_time_format *) 0, <ime, str, decimals);
return str;
}
String *Item::val_string_from_date(String *str)
{
DBUG_ASSERT(fixed == 1);
MYSQL_TIME ltime;
if (get_date(<ime, TIME_FUZZY_DATE) ||
(null_value= str->alloc(MAX_DATE_STRING_REP_LENGTH)))
return (String *) 0;
make_date((Date_time_format *) 0, <ime, str);
return str;
}
String *Item::val_string_from_time(String *str)
{
DBUG_ASSERT(fixed == 1);
MYSQL_TIME ltime;
if (get_time(<ime) || (null_value= str->alloc(MAX_DATE_STRING_REP_LENGTH)))
return (String *) 0;
make_time((Date_time_format *) 0, <ime, str, decimals);
return str;
}
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
{
double nr= val_real();
if (null_value)
return 0;
double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
return (decimal_value);
}
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
{
longlong nr= val_int();
if (null_value)
return 0;
int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
return decimal_value;
}
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
{
String *res;
if (!(res= val_str(&str_value)))
return NULL;
if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
res->ptr(), res->length(), res->charset(),
decimal_value) & E_DEC_BAD_NUM)
{
ErrConvString err(res);
push_warning_printf(current_thd, Sql_condition::SL_WARNING,
ER_TRUNCATED_WRONG_VALUE,
ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
err.ptr());
}
return decimal_value;
}
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
{
DBUG_ASSERT(fixed == 1);
MYSQL_TIME ltime;
if (get_date(<ime, TIME_FUZZY_DATE))
{
/*
The conversion may fail in strict mode. Do not return a NULL pointer,
as the result may be used in subsequent arithmetic operations.
*/
my_decimal_set_zero(decimal_value);
null_value= 1; // set NULL, stop processing
return decimal_value;
}
return date2my_decimal(<ime, decimal_value);
}
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
{
DBUG_ASSERT(fixed == 1);
MYSQL_TIME ltime;
if (get_time(<ime))
{
my_decimal_set_zero(decimal_value);
null_value= 1;
return 0;
}
return date2my_decimal(<ime, decimal_value);
}
longlong Item::val_time_temporal()
{
MYSQL_TIME ltime;
if ((null_value= get_time(<ime)))
return 0;
return TIME_to_longlong_time_packed(<ime);
}
longlong Item::val_date_temporal()
{
MYSQL_TIME ltime;
my_time_flags_t flags= TIME_FUZZY_DATE | TIME_INVALID_DATES;
if (current_thd->variables.sql_mode & MODE_NO_ZERO_IN_DATE)
flags|= TIME_NO_ZERO_IN_DATE;
if (current_thd->variables.sql_mode & MODE_NO_ZERO_DATE)
flags|= TIME_NO_ZERO_DATE;
if ((null_value= get_date(<ime, flags)))
return 0;
return TIME_to_longlong_datetime_packed(<ime);
}
// TS-TODO: split into separate methods?
longlong Item::val_temporal_with_round(enum_field_types type, uint8 dec)
{
longlong nr= val_temporal_by_field_type();
longlong diff= my_time_fraction_remainder(MY_PACKED_TIME_GET_FRAC_PART(nr),
dec);
longlong abs_diff= diff > 0 ? diff : - diff;
if (abs_diff * 2 >= (int) log_10_int[DATETIME_MAX_DECIMALS - dec])
{
/* Needs rounding */
switch (type)
{
case MYSQL_TYPE_TIME:
{
MYSQL_TIME ltime;
TIME_from_longlong_time_packed(<ime, nr);
return my_time_round(<ime, dec) ?
0 : TIME_to_longlong_time_packed(<ime);
}
case MYSQL_TYPE_TIMESTAMP:
case MYSQL_TYPE_DATETIME:
{
MYSQL_TIME ltime;
int warnings= 0;
TIME_from_longlong_datetime_packed(<ime, nr);
return my_datetime_round(<ime, dec, &warnings) ?
0 : TIME_to_longlong_datetime_packed(<ime);
return nr;
}
default:
DBUG_ASSERT(0);
break;
}
}
/* Does not need rounding, do simple truncation. */
nr-= diff;
return nr;
}
double Item::val_real_from_decimal()
{
/* Note that fix_fields may not be called for Item_avg_field items */
double result;
my_decimal value_buff, *dec_val= val_decimal(&value_buff);
if (null_value)
return 0.0;
my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
return result;
}
longlong Item::val_int_from_decimal()
{
/* Note that fix_fields may not be called for Item_avg_field items */
longlong result;
my_decimal value, *dec_val= val_decimal(&value);
if (null_value)
return 0;
my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
return result;
}
longlong Item::val_int_from_time()
{
DBUG_ASSERT(fixed == 1);
MYSQL_TIME ltime;
return get_time(<ime) ?
0LL : (ltime.neg ? -1 : 1) * TIME_to_ulonglong_time_round(<ime);
}
longlong Item::val_int_from_date()
{
DBUG_ASSERT(fixed == 1);
MYSQL_TIME ltime;
return get_date(<ime, TIME_FUZZY_DATE) ?
0LL : (longlong) TIME_to_ulonglong_date(<ime);
}
longlong Item::val_int_from_datetime()
{
DBUG_ASSERT(fixed == 1);
MYSQL_TIME ltime;
return get_date(<ime, TIME_FUZZY_DATE) ?
0LL: (longlong) TIME_to_ulonglong_datetime_round(<ime);
}
type_conversion_status Item::save_time_in_field(Field *field)
{
MYSQL_TIME ltime;
if (get_time(<ime))
return set_field_to_null_with_conversions(field, 0);
field->set_notnull();
return field->store_time(<ime, decimals);
}
type_conversion_status Item::save_date_in_field(Field *field)
{
MYSQL_TIME ltime;
if (get_date(<ime, TIME_FUZZY_DATE))
return set_field_to_null_with_conversions(field, 0);
field->set_notnull();
return field->store_time(<ime, decimals);
}
/*
Store the string value in field directly
SYNOPSIS
Item::save_str_value_in_field()
field a pointer to field where to store
result the pointer to the string value to be stored
DESCRIPTION
The method is used by Item_*::save_in_field_inner() implementations
when we don't need to calculate the value to store
See Item_string::save_in_field_inner() implementation for example
IMPLEMENTATION
Check if the Item is null and stores the NULL or the
result value in the field accordingly.
RETURN
Nonzero value if error
*/
type_conversion_status
Item::save_str_value_in_field(Field *field, String *result)
{
if (null_value)
return set_field_to_null(field);
field->set_notnull();
return field->store(result->ptr(), result->length(), collation.collation);
}
Item::Item():
is_expensive_cache(-1), rsize(0),
marker(0), fixed(0),
collation(&my_charset_bin, DERIVATION_COERCIBLE),
runtime_item(false), derived_used(false), with_subselect(false),
with_stored_program(false), tables_locked_cache(false),
is_parser_item(false)
{
#ifndef DBUG_OFF
contextualized= true;
#endif//DBUG_OFF
maybe_null=null_value=with_sum_func=unsigned_flag=0;
decimals= 0; max_length= 0;
cmp_context= (Item_result)-1;
/* Put item in free list so that we can free all items at end */
THD *thd= current_thd;
next= thd->free_list;
thd->free_list= this;
}
Item::Item(const POS &):
is_expensive_cache(-1), rsize(0),
marker(0), fixed(0),
collation(&my_charset_bin, DERIVATION_COERCIBLE),
runtime_item(false), derived_used(false), with_subselect(false),
with_stored_program(false), tables_locked_cache(false),
is_parser_item(true)
{
maybe_null=null_value=with_sum_func=unsigned_flag=0;
decimals= 0; max_length= 0;
cmp_context= (Item_result)-1;
}
bool Item::itemize(Parse_context *pc, Item **res)
{
if (skip_itemize(res))
return false;
if (super::contextualize(pc))
return true;
/* Put item in free list so that we can free all items at end */
next= pc->thd->free_list;
pc->thd->free_list= this;
/*
Item constructor can be called during execution other then SQL_COM
command => we should check pc->select on zero
*/
if (pc->select)
{
enum_parsing_context place= pc->select->parsing_place;
if (place == CTX_SELECT_LIST ||
place == CTX_HAVING)
pc->select->select_n_having_items++;
}
return false;
}
/**
Constructor used by Item_field, Item_ref & aggregate (sum)
functions.
Used for duplicating lists in processing queries with temporary
tables.
*/
Item::Item(THD *thd, Item *item):
is_expensive_cache(-1),
rsize(0),
str_value(item->str_value),
item_name(item->item_name),
orig_name(item->orig_name),
max_length(item->max_length),
marker(item->marker),
decimals(item->decimals),
maybe_null(item->maybe_null),
null_value(item->null_value),
unsigned_flag(item->unsigned_flag),
with_sum_func(item->with_sum_func),
fixed(item->fixed),
collation(item->collation),
cmp_context(item->cmp_context),
runtime_item(false),
with_subselect(item->has_subquery()),
with_stored_program(item->with_stored_program),
tables_locked_cache(item->tables_locked_cache),
is_parser_item(false)
{
#ifndef DBUG_OFF
DBUG_ASSERT(item->contextualized);
contextualized= true;
#endif//DBUG_OFF
next= thd->free_list; // Put in free list
thd->free_list= this;
}
uint Item::decimal_precision() const
{
Item_result restype= result_type();
if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
{
uint prec=
my_decimal_length_to_precision(max_char_length(), decimals,
unsigned_flag);
return min<uint>(prec, DECIMAL_MAX_PRECISION);
}
switch (field_type())
{
case MYSQL_TYPE_TIME:
return decimals + TIME_INT_DIGITS;
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
return decimals + DATETIME_INT_DIGITS;
case MYSQL_TYPE_DATE:
return decimals + DATE_INT_DIGITS;
default:
break;
}
return min<uint>(max_char_length(), DECIMAL_MAX_PRECISION);
}
uint Item::time_precision()
{
if (const_item() && result_type() == STRING_RESULT && !is_temporal())
{
MYSQL_TIME ltime;
String buf, *tmp;
MYSQL_TIME_STATUS status;
DBUG_ASSERT(fixed);
// Nanosecond rounding is not needed, for performance purposes
if ((tmp= val_str(&buf)) &&
str_to_time(tmp, <ime, TIME_NO_NSEC_ROUNDING, &status) == 0)
return MY_MIN(status.fractional_digits, DATETIME_MAX_DECIMALS);
}
return MY_MIN(decimals, DATETIME_MAX_DECIMALS);
}
uint Item::datetime_precision()
{
if (const_item() && result_type() == STRING_RESULT && !is_temporal())
{
MYSQL_TIME ltime;
String buf, *tmp;
MYSQL_TIME_STATUS status;
DBUG_ASSERT(fixed);
// Nanosecond rounding is not needed, for performance purposes
if ((tmp= val_str(&buf)) &&
!str_to_datetime(tmp, <ime, TIME_NO_NSEC_ROUNDING | TIME_FUZZY_DATE,
&status))
return MY_MIN(status.fractional_digits, DATETIME_MAX_DECIMALS);
}
return MY_MIN(decimals, DATETIME_MAX_DECIMALS);
}
void Item::print_item_w_name(String *str, enum_query_type query_type)
{
print(str, query_type);
if (item_name.is_set() && query_type != QT_NORMALIZED_FORMAT)
{
THD *thd= current_thd;
str->append(STRING_WITH_LEN(" AS "));
append_identifier(thd, str, item_name);
}
}
/**
@details
"SELECT (subq) GROUP BY (same_subq)" confuses ONLY_FULL_GROUP_BY (it does
not see that both subqueries are the same, raises an error).
To avoid hitting this problem, if the original query was:
"SELECT expression AS x GROUP BY x", we print "GROUP BY x", not
"GROUP BY expression". Same for ORDER BY.
This has practical importance for views created as
"CREATE VIEW v SELECT (subq) AS x GROUP BY x"
(print_order() is used to write the view's definition in the frm file).
We make one exception: if the view is merge-able, its ORDER clause will be
merged into the parent query's. If an identifier in the merged ORDER clause
is allowed to be either an alias or an expression of the view's underlying
tables, resolution is difficult: it may be to be found in the underlying
tables of the view, or in the SELECT list of the view; unlike other ORDER
elements directly originating from the parent query.
To avoid this problem, if the view is merge-able, we print the
expression. This does not cause problems with only_full_group_by, because a
merge-able view never has GROUP BY. @see mysql_register_view().
*/
void Item::print_for_order(String *str,
enum_query_type query_type,
bool used_alias)
{
if ((query_type & QT_NORMALIZED_FORMAT) != 0)
str->append("?");
else if (used_alias)
{
DBUG_ASSERT(item_name.is_set());
// In the clause, user has referenced expression using an alias; we use it
append_identifier(current_thd, str, item_name);
}
else
{
if (type() == Item::INT_ITEM && basic_const_item())
{
/*
"ORDER BY N" means "order by the N-th element". To avoid such
interpretation we write "ORDER BY ''", which is equivalent.
*/
str->append("''");
}
else
print(str,query_type);
}
}
void Item::cleanup()
{
DBUG_ENTER("Item::cleanup");
fixed=0;
marker= 0;
tables_locked_cache= false;
if (orig_name.is_set())
item_name= orig_name;
DBUG_VOID_RETURN;
}
/**
cleanup() item if it is 'fixed'.
@param arg a dummy parameter, is not used here
*/
bool Item::cleanup_processor(uchar *arg)
{
if (fixed)
cleanup();
return FALSE;
}
bool Item::visitor_processor(uchar *arg)
{
Select_lex_visitor *visitor= pointer_cast<Select_lex_visitor*>(arg);
return visitor->visit(this);
}
/**
rename item (used for views, cleanup() return original name).
@param new_name new name of item;
*/
void Item::rename(char *new_name)
{
/*
we can compare pointers to names here, because if name was not changed,
pointer will be same
*/
if (!orig_name.is_set() && new_name != item_name.ptr())
orig_name= item_name;
item_name.set(new_name);
}
/**
Traverse item tree possibly transforming it (replacing items).
This function is designed to ease transformation of Item trees.
Re-execution note: every such transformation is registered for
rollback by THD::change_item_tree() and is rolled back at the end
of execution by THD::rollback_item_tree_changes().
Therefore:
- this function can not be used at prepared statement prepare
(in particular, in fix_fields!), as only permanent
transformation of Item trees are allowed at prepare.
- the transformer function shall allocate new Items in execution
memory root (thd->mem_root) and not anywhere else: allocated
items will be gone in the end of execution.
If you don't need to transform an item tree, but only traverse
it, please use Item::walk() instead.
@param transformer functor that performs transformation of a subtree
@param arg opaque argument passed to the functor
@return
Returns pointer to the new subtree root. THD::change_item_tree()
should be called for it if transformation took place, i.e. if a
pointer to newly allocated item is returned.
*/
Item* Item::transform(Item_transformer transformer, uchar *arg)
{
DBUG_ASSERT(!current_thd->stmt_arena->is_stmt_prepare());
return (this->*transformer)(arg);
}
Item_ident::Item_ident(Name_resolution_context *context_arg,
const char *db_name_arg,const char *table_name_arg,
const char *field_name_arg)
:orig_db_name(db_name_arg), orig_table_name(table_name_arg),
orig_field_name(field_name_arg), m_alias_of_expr(false),
context(context_arg),
db_name(db_name_arg), table_name(table_name_arg),
field_name(field_name_arg),
cached_field_index(NO_CACHED_FIELD_INDEX),
cached_table(0), depended_from(0)
{
item_name.set(field_name_arg);
}
Item_ident::Item_ident(const POS &pos,
const char *db_name_arg,const char *table_name_arg,
const char *field_name_arg)
:super(pos), orig_db_name(db_name_arg), orig_table_name(table_name_arg),
orig_field_name(field_name_arg), m_alias_of_expr(false),
db_name(db_name_arg), table_name(table_name_arg),
field_name(field_name_arg),
cached_field_index(NO_CACHED_FIELD_INDEX),
cached_table(0), depended_from(0)
{
item_name.set(field_name_arg);
}
bool Item_ident::itemize(Parse_context *pc, Item **res)
{
if (skip_itemize(res))
return false;
if (super::itemize(pc, res))
return true;
context= pc->thd->lex->current_context();
return false;
}
/**
Constructor used by Item_field & Item_*_ref (see Item comment)
*/
Item_ident::Item_ident(THD *thd, Item_ident *item)
:Item(thd, item),
orig_db_name(item->orig_db_name),
orig_table_name(item->orig_table_name),
orig_field_name(item->orig_field_name),
m_alias_of_expr(item->m_alias_of_expr),
context(item->context),
db_name(item->db_name),
table_name(item->table_name),
field_name(item->field_name),
cached_field_index(item->cached_field_index),
cached_table(item->cached_table),
depended_from(item->depended_from)
{}
void Item_ident::cleanup()
{
DBUG_ENTER("Item_ident::cleanup");
Item::cleanup();
db_name= orig_db_name;
table_name= orig_table_name;
field_name= orig_field_name;
DBUG_VOID_RETURN;
}
bool Item_ident::remove_dependence_processor(uchar * arg)
{
DBUG_ENTER("Item_ident::remove_dependence_processor");
if (depended_from == (st_select_lex *) arg)
depended_from= 0;
context= &((st_select_lex *) arg)->context;
DBUG_RETURN(0);
}
/**
Store the pointer to this item field into a list if not already there.
The method is used by Item::walk to collect all unique Item_field objects
from a tree of Items into a set of items represented as a list.
Item_cond::walk() and Item_func::walk() stop the evaluation of the
processor function for its arguments once the processor returns
true.Therefore in order to force this method being called for all item
arguments in a condition the method must return false.
@param arg pointer to a List<Item_field>
@return
FALSE to force the evaluation of collect_item_field_processor
for the subsequent items.
*/
bool Item_field::collect_item_field_processor(uchar *arg)
{
DBUG_ENTER("Item_field::collect_item_field_processor");
DBUG_PRINT("info", ("%s", field->field_name ? field->field_name : "noname"));
List<Item_field> *item_list= (List<Item_field>*) arg;
List_iterator<Item_field> item_list_it(*item_list);
Item_field *curr_item;
while ((curr_item= item_list_it++))
{
if (curr_item->eq(this, 1))
DBUG_RETURN(FALSE); /* Already in the set. */
}
item_list->push_back(this);
DBUG_RETURN(FALSE);
}
bool Item_field::add_field_to_set_processor(uchar *arg)
{
DBUG_ENTER("Item_field::add_field_to_set_processor");
DBUG_PRINT("info", ("%s", field->field_name ? field->field_name : "noname"));
TABLE *table= (TABLE *) arg;
if (table_ref->table == table)
bitmap_set_bit(&table->tmp_set, field->field_index);
DBUG_RETURN(FALSE);
}
bool Item_field::add_field_to_cond_set_processor(uchar *unused)