-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
item_func.cc
8935 lines (7663 loc) · 232 KB
/
item_func.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, 2018, 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 */
/**
@file
@brief
This file defines all numerical functions
*/
#include "item_func.h"
#include "my_bit.h" // my_count_bits
#include "auth_common.h" // check_password_strength
#include "binlog.h" // mysql_bin_log
#include "debug_sync.h" // DEBUG_SYNC
#include "item_cmpfunc.h" // get_datetime_value
#include "item_strfunc.h" // Item_func_geohash
#include <mysql/service_thd_wait.h>
#include "parse_tree_helpers.h" // PT_item_list
#include "rpl_mi.h" // Master_info
#include "rpl_msr.h" // channel_map
#include "rpl_rli.h" // Relay_log_info
#include "sp.h" // sp_find_routine
#include "sp_head.h" // sp_name
#include "sql_audit.h" // audit_global_variable
#include "sql_base.h" // Internal_error_handler_holder
#include "sql_class.h" // THD
#include "sql_optimizer.h" // JOIN
#include "sql_parse.h" // check_stack_overrun
#include "sql_show.h" // append_identifier
#include "sql_time.h" // TIME_from_longlong_packed
#include "strfunc.h" // find_type
#include "item_json_func.h" // Item_func_json_quote
#include <cfloat> // DBL_DIG
using std::min;
using std::max;
bool check_reserved_words(LEX_STRING *name)
{
if (!my_strcasecmp(system_charset_info, name->str, "GLOBAL") ||
!my_strcasecmp(system_charset_info, name->str, "LOCAL") ||
!my_strcasecmp(system_charset_info, name->str, "SESSION"))
return TRUE;
return FALSE;
}
/**
Evaluate a constant condition, represented by an Item tree
@param thd Thread handler
@param cond The constant condition to evaluate
@param[out] value Returned value, either true or false
@returns false if evaluation is successful, true otherwise
*/
bool eval_const_cond(THD *thd, Item *cond, bool *value)
{
DBUG_ASSERT(cond->const_item());
*value= cond->val_int();
return thd->is_error();
}
/**
Test if the sum of arguments overflows the ulonglong range.
*/
static inline bool test_if_sum_overflows_ull(ulonglong arg1, ulonglong arg2)
{
return ULLONG_MAX - arg1 < arg2;
}
void Item_func::set_arguments(List<Item> &list, bool context_free)
{
allowed_arg_cols= 1;
arg_count=list.elements;
args= tmp_arg; // If 2 arguments
if (arg_count <= 2 || (args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
{
List_iterator_fast<Item> li(list);
Item *item;
Item **save_args= args;
while ((item=li++))
{
*(save_args++)= item;
if (!context_free)
with_sum_func|= item->with_sum_func;
}
}
else
arg_count= 0; // OOM
list.empty(); // Fields are used
}
Item_func::Item_func(List<Item> &list)
:allowed_arg_cols(1)
{
set_arguments(list, false);
}
Item_func::Item_func(const POS &pos, PT_item_list *opt_list)
: super(pos), allowed_arg_cols(1)
{
if (opt_list == NULL)
{
args= tmp_arg;
arg_count= 0;
}
else
set_arguments(opt_list->value, true);
}
Item_func::Item_func(THD *thd, Item_func *item)
:Item_result_field(thd, item),
const_item_cache(0),
allowed_arg_cols(item->allowed_arg_cols),
used_tables_cache(item->used_tables_cache),
not_null_tables_cache(item->not_null_tables_cache),
arg_count(item->arg_count)
{
if (arg_count)
{
if (arg_count <=2)
args= tmp_arg;
else
{
if (!(args=(Item**) thd->alloc(sizeof(Item*)*arg_count)))
return;
}
memcpy((char*) args, (char*) item->args, sizeof(Item*)*arg_count);
}
}
bool Item_func::itemize(Parse_context *pc, Item **res)
{
if (skip_itemize(res))
return false;
if (super::itemize(pc, res))
return true;
with_sum_func= 0;
const bool no_named_params= !may_have_named_parameters();
for (size_t i= 0; i < arg_count; i++)
{
with_sum_func|= args[i]->with_sum_func;
if (args[i]->itemize(pc, &args[i]))
return true;
if (no_named_params && !args[i]->item_name.is_autogenerated())
{
my_error(functype() == FUNC_SP ? ER_WRONG_PARAMETERS_TO_STORED_FCT
: ER_WRONG_PARAMETERS_TO_NATIVE_FCT,
MYF(0), func_name());
return true;
}
}
return false;
}
/*
Resolve references to table column for a function and its argument
SYNOPSIS:
fix_fields()
thd Thread object
ref Pointer to where this object is used. This reference
is used if we want to replace this object with another
one (for example in the summary functions).
DESCRIPTION
Call fix_fields() for all arguments to the function. The main intention
is to allow all Item_field() objects to setup pointers to the table fields.
Sets as a side effect the following class variables:
maybe_null Set if any argument may return NULL
with_sum_func Set if any of the arguments contains a sum function
used_tables_cache Set to union of the tables used by arguments
str_value.charset If this is a string function, set this to the
character set for the first argument.
If any argument is binary, this is set to binary
If for any item any of the defaults are wrong, then this can
be fixed in the fix_length_and_dec() function that is called
after this one or by writing a specialized fix_fields() for the
item.
RETURN VALUES
FALSE ok
TRUE Got error. Stored with my_error().
*/
bool
Item_func::fix_fields(THD *thd, Item **ref)
{
DBUG_ASSERT(fixed == 0 || basic_const_item());
Item **arg,**arg_end;
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
/*
Semi-join flattening should only be performed for top-level
predicates. Disable it for predicates that live under an
Item_func.
*/
Disable_semijoin_flattening DSF(thd->lex->current_select(), true);
used_tables_cache= get_initial_pseudo_tables();
not_null_tables_cache= 0;
const_item_cache=1;
/*
Use stack limit of STACK_MIN_SIZE * 2 since
on some platforms a recursive call to fix_fields
requires more than STACK_MIN_SIZE bytes (e.g. for
MIPS, it takes about 22kB to make one recursive
call to Item_func::fix_fields())
*/
if (check_stack_overrun(thd, STACK_MIN_SIZE * 2, buff))
return TRUE; // Fatal error if flag is set!
if (arg_count)
{ // Print purify happy
for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
{
if (fix_func_arg(thd, arg))
return true;
}
}
fix_length_and_dec();
if (thd->is_error()) // An error inside fix_length_and_dec occured
return TRUE;
fixed= 1;
return FALSE;
}
bool Item_func::fix_func_arg(THD *thd, Item **arg)
{
if ((!(*arg)->fixed && (*arg)->fix_fields(thd, arg)))
return true; /* purecov: inspected */
Item *item= *arg;
if (allowed_arg_cols)
{
if (item->check_cols(allowed_arg_cols))
return true;
}
else
{
/* we have to fetch allowed_arg_cols from first argument */
DBUG_ASSERT(arg == args); // it is first argument
allowed_arg_cols= item->cols();
DBUG_ASSERT(allowed_arg_cols); // Can't be 0 any more
}
maybe_null|= item->maybe_null;
with_sum_func|= item->with_sum_func;
used_tables_cache|= item->used_tables();
not_null_tables_cache|= item->not_null_tables();
const_item_cache&= item->const_item();
with_subselect|= item->has_subquery();
with_stored_program|= item->has_stored_program();
return false;
}
void Item_func::fix_after_pullout(st_select_lex *parent_select,
st_select_lex *removed_select)
{
if (const_item())
{
/*
Pulling out a const item changes nothing to it. Moreover, some items may
have decided that they're const by some other logic than the generic
one below, and we must preserve that decision.
*/
return;
}
Item **arg,**arg_end;
used_tables_cache= get_initial_pseudo_tables();
not_null_tables_cache= 0;
const_item_cache=1;
if (arg_count)
{
for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
{
Item *const item= *arg;
item->fix_after_pullout(parent_select, removed_select);
used_tables_cache|= item->used_tables();
not_null_tables_cache|= item->not_null_tables();
const_item_cache&= item->const_item();
}
}
}
bool Item_func::walk(Item_processor processor, enum_walk walk, uchar *argument)
{
if ((walk & WALK_PREFIX) && (this->*processor)(argument))
return true;
Item **arg, **arg_end;
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
{
if ((*arg)->walk(processor, walk, argument))
return true;
}
return (walk & WALK_POSTFIX) && (this->*processor)(argument);
}
void Item_func::traverse_cond(Cond_traverser traverser,
void *argument, traverse_order order)
{
if (arg_count)
{
Item **arg,**arg_end;
switch (order) {
case(PREFIX):
(*traverser)(this, argument);
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
{
(*arg)->traverse_cond(traverser, argument, order);
}
break;
case (POSTFIX):
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
{
(*arg)->traverse_cond(traverser, argument, order);
}
(*traverser)(this, argument);
}
}
else
(*traverser)(this, argument);
}
/**
Transform an Item_func object with a transformer callback function.
The function recursively applies the transform method to each
argument of the Item_func node.
If the call of the method for an argument item returns a new item
the old item is substituted for a new one.
After this the transformer is applied to the root node
of the Item_func object.
@param transformer the transformer callback function to be applied to
the nodes of the tree of the object
@param argument parameter to be passed to the transformer
@return
Item returned as the result of transformation of the root node
*/
Item *Item_func::transform(Item_transformer transformer, uchar *argument)
{
DBUG_ASSERT(!current_thd->stmt_arena->is_stmt_prepare());
if (arg_count)
{
Item **arg,**arg_end;
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
{
Item *new_item= (*arg)->transform(transformer, argument);
if (!new_item)
return 0;
/*
THD::change_item_tree() should be called only if the tree was
really transformed, i.e. when a new item has been created.
Otherwise we'll be allocating a lot of unnecessary memory for
change records at each execution.
*/
if (*arg != new_item)
current_thd->change_item_tree(arg, new_item);
}
}
return (this->*transformer)(argument);
}
/**
Compile Item_func object with a processor and a transformer
callback functions.
First the function applies the analyzer to the root node of
the Item_func object. Then if the analizer succeeeds (returns TRUE)
the function recursively applies the compile method to each argument
of the Item_func node.
If the call of the method for an argument item returns a new item
the old item is substituted for a new one.
After this the transformer is applied to the root node
of the Item_func object.
@param analyzer the analyzer callback function to be applied to the
nodes of the tree of the object
@param[in,out] arg_p parameter to be passed to the processor
@param transformer the transformer callback function to be applied to the
nodes of the tree of the object
@param arg_t parameter to be passed to the transformer
@return Item returned as result of transformation of the node,
the same item if no transformation applied, or NULL if
transformation caused an error.
*/
Item *Item_func::compile(Item_analyzer analyzer, uchar **arg_p,
Item_transformer transformer, uchar *arg_t)
{
if (!(this->*analyzer)(arg_p))
return this;
if (arg_count)
{
Item **arg,**arg_end;
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
{
/*
The same parameter value of arg_p must be passed
to analyze any argument of the condition formula.
*/
uchar *arg_v= *arg_p;
Item *new_item= (*arg)->compile(analyzer, &arg_v, transformer, arg_t);
if (new_item == NULL)
return NULL;
if (*arg != new_item)
current_thd->change_item_tree(arg, new_item);
}
}
return (this->*transformer)(arg_t);
}
/**
See comments in Item_cmp_func::split_sum_func()
*/
void Item_func::split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
List<Item> &fields)
{
Item **arg, **arg_end;
for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++)
(*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg, TRUE);
}
void Item_func::update_used_tables()
{
used_tables_cache= get_initial_pseudo_tables();
const_item_cache=1;
with_subselect= false;
with_stored_program= false;
for (uint i=0 ; i < arg_count ; i++)
{
args[i]->update_used_tables();
used_tables_cache|=args[i]->used_tables();
const_item_cache&=args[i]->const_item();
with_subselect|= args[i]->has_subquery();
with_stored_program|= args[i]->has_stored_program();
}
}
void Item_func_sp::fix_after_pullout(SELECT_LEX *parent_select,
SELECT_LEX *removed_select)
{
Item_func::fix_after_pullout(parent_select, removed_select);
/*
Prevents function from being evaluated before it is locked.
@todo - make this dependent on READS SQL or MODIFIES SQL.
Due to a limitation in how functions are evaluated, we need to
ensure that we are in a prelocked mode even though the function
doesn't reference any tables.
*/
used_tables_cache|= PARAM_TABLE_BIT;
}
table_map Item_func::used_tables() const
{
return used_tables_cache;
}
table_map Item_func::not_null_tables() const
{
return not_null_tables_cache;
}
void Item_func::print(String *str, enum_query_type query_type)
{
str->append(func_name());
str->append('(');
print_args(str, 0, query_type);
str->append(')');
}
void Item_func::print_args(String *str, uint from, enum_query_type query_type)
{
for (uint i=from ; i < arg_count ; i++)
{
if (i != from)
str->append(',');
args[i]->print(str, query_type);
}
}
void Item_func::print_op(String *str, enum_query_type query_type)
{
str->append('(');
for (uint i=0 ; i < arg_count-1 ; i++)
{
args[i]->print(str, query_type);
str->append(' ');
str->append(func_name());
str->append(' ');
}
args[arg_count-1]->print(str, query_type);
str->append(')');
}
/// @note Please keep in sync with Item_sum::eq().
bool Item_func::eq(const Item *item, bool binary_cmp) const
{
/* Assume we don't have rtti */
if (this == item)
return 1;
if (item->type() != FUNC_ITEM)
return 0;
Item_func *item_func=(Item_func*) item;
Item_func::Functype func_type;
if ((func_type= functype()) != item_func->functype() ||
arg_count != item_func->arg_count ||
(func_type != Item_func::FUNC_SP &&
func_name() != item_func->func_name()) ||
(func_type == Item_func::FUNC_SP &&
my_strcasecmp(system_charset_info, func_name(), item_func->func_name())))
return 0;
for (uint i=0; i < arg_count ; i++)
if (!args[i]->eq(item_func->args[i], binary_cmp))
return 0;
return 1;
}
Field *Item_func::tmp_table_field(TABLE *table)
{
Field *field= NULL;
switch (result_type()) {
case INT_RESULT:
if (max_char_length() > MY_INT32_NUM_DECIMAL_DIGITS)
field= new Field_longlong(max_char_length(), maybe_null, item_name.ptr(),
unsigned_flag);
else
field= new Field_long(max_char_length(), maybe_null, item_name.ptr(),
unsigned_flag);
break;
case REAL_RESULT:
field= new Field_double(max_char_length(), maybe_null, item_name.ptr(), decimals);
break;
case STRING_RESULT:
return make_string_field(table);
break;
case DECIMAL_RESULT:
field= Field_new_decimal::create_from_item(this);
break;
case ROW_RESULT:
default:
// This case should never be chosen
DBUG_ASSERT(0);
field= 0;
break;
}
if (field)
field->init(table);
return field;
}
my_decimal *Item_func::val_decimal(my_decimal *decimal_value)
{
DBUG_ASSERT(fixed);
longlong nr= val_int();
if (null_value)
return 0; /* purecov: inspected */
int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
return decimal_value;
}
type_conversion_status Item_func::save_possibly_as_json(Field *field,
bool no_conversions)
{
if (field->type() == MYSQL_TYPE_JSON)
{
// Store the value in the JSON binary format.
Field_json *f= down_cast<Field_json *>(field);
Json_wrapper wr;
val_json(&wr);
if (null_value)
return set_field_to_null(field);
field->set_notnull();
return f->store_json(&wr);
}
else
{
// TODO Convert the JSON value to text.
return Item_func::save_in_field_inner(field, no_conversions);
}
}
String *Item_real_func::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
double nr= val_real();
if (null_value)
return 0; /* purecov: inspected */
str->set_real(nr, decimals, collation.collation);
return str;
}
my_decimal *Item_real_func::val_decimal(my_decimal *decimal_value)
{
DBUG_ASSERT(fixed);
double nr= val_real();
if (null_value)
return 0; /* purecov: inspected */
double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
return decimal_value;
}
void Item_func::fix_num_length_and_dec()
{
uint fl_length= 0;
decimals=0;
for (uint i=0 ; i < arg_count ; i++)
{
set_if_bigger(decimals,args[i]->decimals);
set_if_bigger(fl_length, args[i]->max_length);
}
max_length=float_length(decimals);
if (fl_length > max_length)
{
decimals= NOT_FIXED_DEC;
max_length= float_length(NOT_FIXED_DEC);
}
}
void Item_func_numhybrid::fix_num_length_and_dec()
{}
/**
Count max_length and decimals for temporal functions.
@param item Argument array
@param nitems Number of arguments in the array.
@retval False on success, true on error.
*/
void Item_func::count_datetime_length(Item **item, uint nitems)
{
unsigned_flag= 0;
decimals= 0;
if (field_type() != MYSQL_TYPE_DATE)
{
for (uint i= 0; i < nitems; i++)
set_if_bigger(decimals,
field_type() == MYSQL_TYPE_TIME ?
item[i]->time_precision() : item[i]->datetime_precision());
}
set_if_smaller(decimals, DATETIME_MAX_DECIMALS);
uint len= decimals ? (decimals + 1) : 0;
switch (field_type())
{
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
len+= MAX_DATETIME_WIDTH;
break;
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_NEWDATE:
len+= MAX_DATE_WIDTH;
break;
case MYSQL_TYPE_TIME:
len+= MAX_TIME_WIDTH;
break;
default:
DBUG_ASSERT(0);
}
fix_char_length(len);
}
/**
Set max_length/decimals of function if function is fixed point and
result length/precision depends on argument ones.
@param item Argument array.
@param nitems Number of arguments in the array.
This function doesn't set unsigned_flag. Call agg_result_type()
first to do that.
*/
void Item_func::count_decimal_length(Item **item, uint nitems)
{
int max_int_part= 0;
decimals= 0;
for (uint i=0 ; i < nitems ; i++)
{
set_if_bigger(decimals, item[i]->decimals);
set_if_bigger(max_int_part, item[i]->decimal_int_part());
}
int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
fix_char_length(my_decimal_precision_to_length_no_truncation(precision,
decimals,
unsigned_flag));
}
/**
Set char_length to the maximum number of characters required by any
of this function's arguments.
This function doesn't set unsigned_flag. Call agg_result_type()
first to do that.
*/
void Item_func::count_only_length(Item **item, uint nitems)
{
uint32 char_length= 0;
for (uint i= 0; i < nitems; i++)
set_if_bigger(char_length, item[i]->max_char_length());
fix_char_length(char_length);
}
/**
Set max_length/decimals of function if function is floating point and
result length/precision depends on argument ones.
@param item Argument array.
@param nitems Number of arguments in the array.
*/
void Item_func::count_real_length(Item **item, uint nitems)
{
uint32 length= 0;
decimals= 0;
max_length= 0;
for (uint i=0 ; i < nitems; i++)
{
if (decimals != NOT_FIXED_DEC)
{
set_if_bigger(decimals, item[i]->decimals);
set_if_bigger(length, (item[i]->max_length - item[i]->decimals));
}
set_if_bigger(max_length, item[i]->max_length);
}
if (decimals != NOT_FIXED_DEC)
{
max_length= length;
length+= decimals;
if (length < max_length) // If previous operation gave overflow
max_length= UINT_MAX32;
else
max_length= length;
}
}
/**
Calculate max_length and decimals for STRING_RESULT functions.
@param field_type Field type.
@param items Argument array.
@param nitems Number of arguments.
@retval False on success, true on error.
*/
bool Item_func::count_string_result_length(enum_field_types field_type,
Item **items, uint nitems)
{
if (agg_arg_charsets_for_string_result(collation, items, nitems))
return true;
if (is_temporal_type(field_type))
count_datetime_length(items, nitems);
else
{
decimals= NOT_FIXED_DEC;
count_only_length(items, nitems);
}
return false;
}
void Item_func::signal_divide_by_null()
{
THD *thd= current_thd;
if (thd->variables.sql_mode & MODE_ERROR_FOR_DIVISION_BY_ZERO)
push_warning(thd, Sql_condition::SL_WARNING, ER_DIVISION_BY_ZERO,
ER(ER_DIVISION_BY_ZERO));
null_value= 1;
}
void Item_func::signal_invalid_argument_for_log()
{
THD *thd= current_thd;
push_warning(thd, Sql_condition::SL_WARNING,
ER_INVALID_ARGUMENT_FOR_LOGARITHM,
ER(ER_INVALID_ARGUMENT_FOR_LOGARITHM));
null_value= TRUE;
}
Item *Item_func::get_tmp_table_item(THD *thd)
{
if (!with_sum_func && !const_item())
return new Item_field(result_field);
return copy_or_same(thd);
}
const Item_field*
Item_func::contributes_to_filter(table_map read_tables,
table_map filter_for_table,
const MY_BITMAP *fields_to_ignore) const
{
DBUG_ASSERT((read_tables & filter_for_table) == 0);
/*
Multiple equality (Item_equal) should not call this function
because it would reject valid comparisons.
*/
DBUG_ASSERT(functype() != MULT_EQUAL_FUNC);
/*
To contribute to filering effect, the condition must refer to
exactly one unread table: the table filtering is currently
calculated for.
*/
if ((used_tables() & ~read_tables) != filter_for_table)
return NULL;
/*
Whether or not this Item_func has an operand that is a field in
'filter_for_table' that is not in 'fields_to_ignore'.
*/
Item_field* usable_field= NULL;
/*
Whether or not this Item_func has an operand that can be used as
available value. arg_count==1 for Items with implicit values like
"field IS NULL".
*/
bool found_comparable= (arg_count == 1);
for (uint i= 0; i < arg_count; i++)
{
const Item::Type arg_type= args[i]->real_item()->type();
if (arg_type == Item::SUBSELECT_ITEM)
{
if (args[i]->const_item())
{
// Constant subquery, i.e., not a dependent subquery.
found_comparable= true;
continue;
}
/*
This is either "fld OP <dependent_subquery>" or "fld BETWEEN X
and Y" where either X or Y is a dependent subquery. Filtering
effect should not be calculated for this item because the cost
of evaluating the dependent subquery is currently not
calculated and its accompanying filtering effect is too
uncertain. See WL#7384.
*/
return NULL;
} // ... if subquery.
const table_map used_tabs= args[i]->used_tables();
if (arg_type == Item::FIELD_ITEM && (used_tabs == filter_for_table))
{
/*
The qualifying table of args[i] is filter_for_table. args[i]
may be a field or a reference to a field, e.g. through a
view.
*/
Item_field *fld= static_cast<Item_field*>(args[i]->real_item());
/*
Use args[i] as value if
1) this field shall be ignored, or
2) a usable field has already been found (meaning that
this is "filter_for_table.colX OP filter_for_table.colY").
*/
if (bitmap_is_set(fields_to_ignore, fld->field->field_index) || // 1)
usable_field) // 2)
{
found_comparable= true;
continue;
}
/*
This field shall contribute to filtering effect if a
value is found for it
*/
usable_field= fld;
} // if field.
else
{
/*
It's not a subquery. May be a function, a constant, an outer
reference, a field of another table...
Already checked that this predicate does not refer to tables
later in the join sequence. Verify it:
*/
DBUG_ASSERT(!(used_tabs & (~read_tables & ~filter_for_table)));
found_comparable= true;
}
}
return (found_comparable ? usable_field : NULL);
}
/**
Return new Item_field if given expression matches GC
@see substitute_gc()
@param func Expression to be replaced
@param fld GCs field
@param type Result type to match with Field
@returns
item new Item_field for matched GC
NULL otherwise
*/
Item_field *get_gc_for_expr(Item_func **func, Field *fld, Item_result type)
{
Item_func *expr= down_cast<Item_func*>(fld->gcol_info->expr_item);
/*
In the case where the generated column expression returns JSON and
the predicate compares the values as strings, it is not safe to
replace the expression with the generated column, since the
indexed string values will be double-quoted. The generated column
expression should use the JSON_UNQUOTE function to strip off the
double-quotes in order to get a usable index for looking up
strings. See also the comment below.
*/
if (type == STRING_RESULT && expr->field_type() == MYSQL_TYPE_JSON)
return NULL;
/*
Skip unquoting function. This is needed to address JSON string
comparison issue. All JSON_* functions return quoted strings. In
order to create usable index, GC column expression has to include
JSON_UNQUOTE function, e.g JSON_UNQUOTE(JSON_EXTRACT(..)).
Hence, the unquoting function in column expression have to be
skipped in order to correctly match GC expr to expr in
WHERE condition. The exception is if user has explicitly used
JSON_UNQUOTE in WHERE condition.
*/
if (!strcmp(expr->func_name(),"json_unquote") &&
strcmp((*func)->func_name(),"json_unquote"))
{
if (!expr->arguments()[0]->can_be_substituted_for_gc())
return NULL;
expr= down_cast<Item_func*>(expr->arguments()[0]);
}
DBUG_ASSERT(expr->can_be_substituted_for_gc());