-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathitem_func.h
4085 lines (3568 loc) · 131 KB
/
item_func.h
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
#ifndef ITEM_FUNC_INCLUDED
#define ITEM_FUNC_INCLUDED
/* Copyright (c) 2000, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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, version 2.0, 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 <sys/types.h>
#include <climits>
#include <cmath> // isfinite
#include <cstddef>
#include <functional>
#include "decimal.h"
#include "field_types.h"
#include "ft_global.h"
#include "lex_string.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_pointer_arithmetic.h"
#include "my_table_map.h"
#include "my_thread_local.h"
#include "my_time.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/strings/m_ctype.h"
#include "mysql/udf_registration_types.h"
#include "mysql_time.h"
#include "mysqld_error.h"
#include "sql-common/my_decimal.h" // str2my_decimal
#include "sql/enum_query_type.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/item.h" // Item_result_field
#include "sql/parse_location.h" // POS
#include "sql/set_var.h" // enum_var_type
#include "sql/sql_const.h"
#include "sql/sql_udf.h" // udf_handler
#include "sql/table.h"
#include "sql/thr_malloc.h"
#include "sql_string.h"
#include "template_utils.h"
class Json_wrapper;
class PT_item_list;
class Protocol;
class Query_block;
class THD;
class sp_rcontext;
struct MY_BITMAP;
struct Parse_context;
struct TYPELIB;
template <class T>
class List;
/* Function items used by mysql */
extern bool reject_geometry_args(uint arg_count, Item **args,
Item_result_field *me);
void unsupported_json_comparison(size_t arg_count, Item **args,
const char *msg);
void report_conversion_error(const CHARSET_INFO *to_cs, const char *from,
size_t from_length, const CHARSET_INFO *from_cs);
bool simplify_string_args(THD *thd, const DTCollation &c, Item **items,
uint nitems);
String *eval_string_arg_noinline(const CHARSET_INFO *to_cs, Item *arg,
String *buffer);
inline String *eval_string_arg(const CHARSET_INFO *to_cs, Item *arg,
String *buffer) {
if (my_charset_same(to_cs, arg->collation.collation))
return arg->val_str(buffer);
return eval_string_arg_noinline(to_cs, arg, buffer);
}
class Item_func : public Item_result_field {
protected:
/**
Array of pointers to arguments. If there are max 2 arguments, this array
is often just m_embedded_arguments; otherwise it's explicitly allocated in
the constructor.
*/
Item **args;
private:
Item *m_embedded_arguments[2];
/// Allocates space for the given number of arguments, if needed. Uses
/// #m_embedded_arguments if it's big enough.
bool alloc_args(MEM_ROOT *mem_root, unsigned num_args) {
if (num_args <= array_elements(m_embedded_arguments)) {
args = m_embedded_arguments;
} else {
args = mem_root->ArrayAlloc<Item *>(num_args);
if (args == nullptr) {
// OOM
arg_count = 0;
return true;
}
}
arg_count = num_args;
return false;
}
public:
uint arg_count; ///< How many arguments in 'args'
virtual uint argument_count() const { return arg_count; }
inline Item **arguments() const {
return (argument_count() > 0) ? args : nullptr;
}
protected:
/*
These decide of types of arguments which are prepared-statement
parameters.
*/
bool param_type_uses_non_param(THD *thd,
enum_field_types def = MYSQL_TYPE_VARCHAR);
bool param_type_is_default(THD *thd, uint start, uint end, uint step,
enum_field_types def);
bool param_type_is_default(THD *thd, uint start, uint end,
enum_field_types def = MYSQL_TYPE_VARCHAR) {
return param_type_is_default(thd, start, end, 1, def);
}
bool param_type_is_rejected(uint start, uint end);
/**
Affects how to determine that NULL argument implies a NULL function return.
Default behaviour in this class is:
- if true, any NULL argument means the function returns NULL.
- if false, no such assumption is made and not_null_tables_cache is thus
set to 0.
null_on_null is true for all Item_func derived classes, except Item_func_sp,
all CASE derived functions and a few other functions.
RETURNS NULL ON NULL INPUT can be implemented for stored functions by
modifying this member in class Item_func_sp.
*/
bool null_on_null{true};
/*
Allowed numbers of columns in result (usually 1, which means scalar value)
0 means get this number from first argument
*/
uint allowed_arg_cols{1};
/// Value used in calculation of result of used_tables()
table_map used_tables_cache{0};
/// Value used in calculation of result of not_null_tables()
table_map not_null_tables_cache{0};
public:
bool is_null_on_null() const { return null_on_null; }
/*
When updating Functype with new spatial functions,
is_spatial_operator() should also be updated.
DD_INTERNAL_FUNC:
Some of the internal functions introduced for the INFORMATION_SCHEMA views
opens data-dictionary tables. DD_INTERNAL_FUNC is used for the such type
of functions.
*/
enum Functype {
UNKNOWN_FUNC,
EQ_FUNC,
EQUAL_FUNC,
NE_FUNC,
LT_FUNC,
LE_FUNC,
GE_FUNC,
GT_FUNC,
FT_FUNC,
MATCH_FUNC,
LIKE_FUNC,
ISNULL_FUNC,
ISNOTNULL_FUNC,
ISTRUTH_FUNC,
COND_AND_FUNC,
COND_OR_FUNC,
XOR_FUNC,
BETWEEN,
IN_FUNC,
MULT_EQUAL_FUNC,
INTERVAL_FUNC,
ISNOTNULLTEST_FUNC,
SP_EQUALS_FUNC,
SP_DISJOINT_FUNC,
SP_DISTANCE_FUNC,
SP_INTERSECTS_FUNC,
SP_TOUCHES_FUNC,
SP_CROSSES_FUNC,
SP_WITHIN_FUNC,
SP_CONTAINS_FUNC,
SP_COVEREDBY_FUNC,
SP_COVERS_FUNC,
SP_OVERLAPS_FUNC,
SP_STARTPOINT,
SP_ENDPOINT,
SP_EXTERIORRING,
SP_POINTN,
SP_GEOMETRYN,
SP_INTERIORRINGN,
NOT_FUNC,
NOT_ALL_FUNC,
NOW_FUNC,
FROM_DAYS_FUNC,
TRIG_COND_FUNC,
SUSERVAR_FUNC,
GUSERVAR_FUNC,
COLLATE_FUNC,
EXTRACT_FUNC,
TYPECAST_FUNC,
FUNC_SP,
UDF_FUNC,
NEG_FUNC,
GSYSVAR_FUNC,
GROUPING_FUNC,
ROLLUP_GROUP_ITEM_FUNC,
TABLE_FUNC,
DD_INTERNAL_FUNC,
PLUS_FUNC,
MINUS_FUNC,
MUL_FUNC,
DIV_FUNC,
CEILING_FUNC,
ROUND_FUNC,
TRUNCATE_FUNC,
SQRT_FUNC,
ABS_FUNC,
POW_FUNC,
SIGN_FUNC,
FLOOR_FUNC,
LOG_FUNC,
LN_FUNC,
LOG10_FUNC,
SIN_FUNC,
TAN_FUNC,
COS_FUNC,
COT_FUNC,
DEGREES_FUNC,
RADIANS_FUNC,
EXP_FUNC,
ASIN_FUNC,
ATAN_FUNC,
ACOS_FUNC,
MOD_FUNC,
IF_FUNC,
NULLIF_FUNC,
CASE_FUNC,
YEAR_FUNC,
YEARWEEK_FUNC,
MAKEDATE_FUNC,
MAKETIME_FUNC,
MONTH_FUNC,
MONTHNAME_FUNC,
DAY_FUNC,
DAYNAME_FUNC,
TO_DAYS_FUNC,
TO_SECONDS_FUNC,
DATE_FUNC,
HOUR_FUNC,
MINUTE_FUNC,
SECOND_FUNC,
MICROSECOND_FUNC,
DAYOFYEAR_FUNC,
ADDTIME_FUNC,
QUARTER_FUNC,
WEEK_FUNC,
WEEKDAY_FUNC,
DATEADD_FUNC,
FROM_UNIXTIME_FUNC,
CONVERT_TZ_FUNC,
LAST_DAY_FUNC,
UNIX_TIMESTAMP_FUNC,
TIME_TO_SEC_FUNC,
TIMESTAMPDIFF_FUNC,
DATETIME_LITERAL,
GREATEST_FUNC,
COALESCE_FUNC,
LEAST_FUNC,
JSON_CONTAINS,
JSON_OVERLAPS,
JSON_UNQUOTE_FUNC,
MEMBER_OF_FUNC,
STRCMP_FUNC,
TRUE_FUNC,
FALSE_FUNC,
SYSDATE_FUNC,
TIMEDIFF_FUNC,
PERIODADD_FUNC,
PERIODDIFF_FUNC,
SEC_TO_TIME_FUNC,
GET_FORMAT_FUNC,
ANY_VALUE_FUNC,
JSON_LENGTH_FUNC,
JSON_DEPTH_FUNC,
JSON_EXTRACT_FUNC,
JSON_OBJECT_FUNC,
JSON_ARRAY_FUNC
};
enum optimize_type {
OPTIMIZE_NONE,
OPTIMIZE_KEY,
OPTIMIZE_OP,
OPTIMIZE_NULL,
OPTIMIZE_EQUAL
};
enum Type type() const override { return FUNC_ITEM; }
virtual enum Functype functype() const { return UNKNOWN_FUNC; }
Item_func() : args(m_embedded_arguments), arg_count(0) {}
explicit Item_func(const POS &pos)
: Item_result_field(pos), args(m_embedded_arguments), arg_count(0) {}
Item_func(Item *a) : args(m_embedded_arguments), arg_count(1) {
args[0] = a;
set_accum_properties(a);
}
Item_func(const POS &pos, Item *a)
: Item_result_field(pos), args(m_embedded_arguments), arg_count(1) {
args[0] = a;
}
Item_func(Item *a, Item *b) : args(m_embedded_arguments), arg_count(2) {
args[0] = a;
args[1] = b;
m_accum_properties = 0;
add_accum_properties(a);
add_accum_properties(b);
}
Item_func(const POS &pos, Item *a, Item *b)
: Item_result_field(pos), args(m_embedded_arguments), arg_count(2) {
args[0] = a;
args[1] = b;
}
Item_func(Item *a, Item *b, Item *c) {
if (alloc_args(*THR_MALLOC, 3)) return;
args[0] = a;
args[1] = b;
args[2] = c;
m_accum_properties = 0;
add_accum_properties(a);
add_accum_properties(b);
add_accum_properties(c);
}
Item_func(const POS &pos, Item *a, Item *b, Item *c)
: Item_result_field(pos) {
if (alloc_args(*THR_MALLOC, 3)) return;
args[0] = a;
args[1] = b;
args[2] = c;
}
Item_func(Item *a, Item *b, Item *c, Item *d) {
if (alloc_args(*THR_MALLOC, 4)) return;
args[0] = a;
args[1] = b;
args[2] = c;
args[3] = d;
m_accum_properties = 0;
add_accum_properties(a);
add_accum_properties(b);
add_accum_properties(c);
add_accum_properties(d);
}
Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
: Item_result_field(pos) {
if (alloc_args(*THR_MALLOC, 4)) return;
args[0] = a;
args[1] = b;
args[2] = c;
args[3] = d;
}
Item_func(Item *a, Item *b, Item *c, Item *d, Item *e) {
if (alloc_args(*THR_MALLOC, 5)) return;
args[0] = a;
args[1] = b;
args[2] = c;
args[3] = d;
args[4] = e;
m_accum_properties = 0;
add_accum_properties(a);
add_accum_properties(b);
add_accum_properties(c);
add_accum_properties(d);
add_accum_properties(e);
}
Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e)
: Item_result_field(pos) {
if (alloc_args(*THR_MALLOC, 5)) return;
args[0] = a;
args[1] = b;
args[2] = c;
args[3] = d;
args[4] = e;
}
Item_func(Item *a, Item *b, Item *c, Item *d, Item *e, Item *f) {
if (alloc_args(*THR_MALLOC, 6)) return;
args[0] = a;
args[1] = b;
args[2] = c;
args[3] = d;
args[4] = e;
args[5] = f;
m_accum_properties = 0;
add_accum_properties(a);
add_accum_properties(b);
add_accum_properties(c);
add_accum_properties(d);
add_accum_properties(e);
add_accum_properties(f);
}
Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e,
Item *f)
: Item_result_field(pos) {
if (alloc_args(*THR_MALLOC, 6)) return;
args[0] = a;
args[1] = b;
args[2] = c;
args[3] = d;
args[4] = e;
args[5] = f;
}
explicit Item_func(mem_root_deque<Item *> *list) {
set_arguments(list, false);
}
Item_func(const POS &pos, PT_item_list *opt_list);
// Constructor used for Item_cond_and/or (see Item comment)
Item_func(THD *thd, const Item_func *item);
/// Get the i'th argument of the function that this object represents.
virtual Item *get_arg(uint i) { return args[i]; }
/// Get the i'th argument of the function that this object represents.
virtual const Item *get_arg(uint i) const { return args[i]; }
virtual Item *set_arg(THD *, uint, Item *) {
assert(0);
return nullptr;
}
bool do_itemize(Parse_context *pc, Item **res) override;
bool fix_fields(THD *, Item **ref) override;
bool fix_func_arg(THD *, Item **arg);
void fix_after_pullout(Query_block *parent_query_block,
Query_block *removed_query_block) override;
/**
Resolve type of function after all arguments have had their data types
resolved. Called from resolve_type() when no dynamic parameters
are used and from propagate_type() otherwise.
*/
virtual bool resolve_type_inner(THD *) {
assert(false);
return false;
}
bool propagate_type(THD *thd, const Type_properties &type) override;
/**
Returns the pseudo tables depended upon in order to evaluate this
function expression. The default implementation returns the empty
set.
*/
virtual table_map get_initial_pseudo_tables() const { return 0; }
table_map used_tables() const override { return used_tables_cache; }
table_map not_null_tables() const override { return not_null_tables_cache; }
void update_used_tables() override;
void set_used_tables(table_map map) { used_tables_cache = map; }
bool eq(const Item *item, bool binary_cmp) const override;
virtual optimize_type select_optimize(const THD *) { return OPTIMIZE_NONE; }
virtual bool have_rev_func() const { return false; }
virtual Item *key_item() const { return args[0]; }
/**
Copy arguments from list to args array
@param list function argument list
@param context_free true: for use in context-independent
constructors (Item_func(POS,...)) i.e. for use
in the parser
@return true on OOM, false otherwise
*/
bool set_arguments(mem_root_deque<Item *> *list, bool context_free);
bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
mem_root_deque<Item *> *fields) override;
void print(const THD *thd, String *str,
enum_query_type query_type) const override;
void print_op(const THD *thd, String *str, enum_query_type query_type) const;
void print_args(const THD *thd, String *str, uint from,
enum_query_type query_type) const;
virtual void fix_num_length_and_dec();
virtual bool is_deprecated() const { return false; }
bool get_arg0_date(MYSQL_TIME *ltime, my_time_flags_t fuzzy_date) {
return (null_value = args[0]->get_date(ltime, fuzzy_date));
}
inline bool get_arg0_time(MYSQL_TIME *ltime) {
return (null_value = args[0]->get_time(ltime));
}
bool is_null() override { return update_null_value() || null_value; }
void signal_divide_by_null();
void signal_invalid_argument_for_log();
friend class udf_handler;
Field *tmp_table_field(TABLE *t_arg) override;
Item *get_tmp_table_item(THD *thd) override;
my_decimal *val_decimal(my_decimal *) override;
bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems, uint flags,
int item_sep) {
return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep,
false);
}
/*
Aggregate arguments for string result, e.g: CONCAT(a,b)
- convert to @@character_set_connection if all arguments are numbers
- allow DERIVATION_NONE
*/
bool agg_arg_charsets_for_string_result(DTCollation &c, Item **items,
uint nitems, int item_sep = 1) {
return agg_item_charsets_for_string_result(c, func_name(), items, nitems,
item_sep);
}
/*
Aggregate arguments for comparison, e.g: a=b, a LIKE b, a RLIKE b
- don't convert to @@character_set_connection if all arguments are numbers
- don't allow DERIVATION_NONE
*/
bool agg_arg_charsets_for_comparison(DTCollation &c, Item **items,
uint nitems, int item_sep = 1) {
return agg_item_charsets_for_comparison(c, func_name(), items, nitems,
item_sep);
}
Item *replace_func_call(uchar *) override;
bool walk(Item_processor processor, enum_walk walk, uchar *arg) override;
Item *transform(Item_transformer transformer, uchar *arg) override;
Item *compile(Item_analyzer analyzer, uchar **arg_p,
Item_transformer transformer, uchar *arg_t) override;
void traverse_cond(Cond_traverser traverser, void *arg,
traverse_order order) override;
bool replace_equal_field_checker(uchar **arg) override {
Replace_equal *replace = pointer_cast<Replace_equal *>(*arg);
replace->stack.push_front(this);
return true;
}
Item *replace_equal_field(uchar *arg) override {
pointer_cast<Replace_equal *>(arg)->stack.pop();
return this;
}
/**
Check whether a function allows replacement of a field with another item:
In particular, a replacement that changes the metadata of some Item
from non-nullable to nullable is not allowed.
Notice that e.g. changing the nullability of an operand of a comparison
operator in a WHERE clause that ignores UNKNOWN values is allowed,
according to this criterion.
@param original the field that could be replaced
@param subst the item that could be the replacement
@returns true if replacement is allowed, false otherwise
*/
virtual bool allow_replacement(Item_field *const original,
Item *const subst) {
return original->is_nullable() || !subst->is_nullable();
}
/**
Throw an error if the input double number is not finite, i.e. is either
+/-INF or NAN.
*/
inline double check_float_overflow(double value) {
return std::isfinite(value) ? value : raise_float_overflow();
}
/**
Throw an error if the input BIGINT value represented by the
(longlong value, bool unsigned flag) pair cannot be returned by the
function, i.e. is not compatible with this Item's unsigned_flag.
*/
inline longlong check_integer_overflow(longlong value, bool val_unsigned) {
if ((unsigned_flag && !val_unsigned && value < 0) ||
(!unsigned_flag && val_unsigned &&
(ulonglong)value > (ulonglong)LLONG_MAX))
return raise_integer_overflow();
return value;
}
/**
Throw an error if the error code of a DECIMAL operation is E_DEC_OVERFLOW.
*/
inline int check_decimal_overflow(int error) {
return (error == E_DEC_OVERFLOW) ? raise_decimal_overflow() : error;
}
bool has_timestamp_args() {
assert(fixed);
for (uint i = 0; i < arg_count; i++) {
if (args[i]->type() == Item::FIELD_ITEM &&
args[i]->data_type() == MYSQL_TYPE_TIMESTAMP)
return true;
}
return false;
}
bool has_date_args() {
assert(fixed);
for (uint i = 0; i < arg_count; i++) {
if (args[i]->type() == Item::FIELD_ITEM &&
(args[i]->data_type() == MYSQL_TYPE_DATE ||
args[i]->data_type() == MYSQL_TYPE_DATETIME))
return true;
}
return false;
}
bool has_time_args() {
assert(fixed);
for (uint i = 0; i < arg_count; i++) {
if (args[i]->type() == Item::FIELD_ITEM &&
(args[i]->data_type() == MYSQL_TYPE_TIME ||
args[i]->data_type() == MYSQL_TYPE_DATETIME))
return true;
}
return false;
}
bool has_datetime_args() {
assert(fixed);
for (uint i = 0; i < arg_count; i++) {
if (args[i]->type() == Item::FIELD_ITEM &&
args[i]->data_type() == MYSQL_TYPE_DATETIME)
return true;
}
return false;
}
/*
We assume the result of any function that has a TIMESTAMP argument to be
timezone-dependent, since a TIMESTAMP value in both numeric and string
contexts is interpreted according to the current timezone.
The only exception is UNIX_TIMESTAMP() which returns the internal
representation of a TIMESTAMP argument verbatim, and thus does not depend on
the timezone.
*/
bool check_valid_arguments_processor(uchar *) override {
return has_timestamp_args();
}
Item *gc_subst_transformer(uchar *arg) override;
bool resolve_type(THD *thd) override {
// By default, pick PS-param's type from other arguments, or VARCHAR
return param_type_uses_non_param(thd);
}
/**
Whether an arg of a JSON function can be cached to avoid repetitive
string->JSON conversion. This function returns true only for those args,
which are the source of JSON data. JSON path args are cached independently
and for them this function returns false. Same as for all other type of
args.
@param arg the arg to cache
@retval true arg can be cached
@retval false otherwise
*/
virtual enum_const_item_cache can_cache_json_arg(Item *arg [[maybe_unused]]) {
return CACHE_NONE;
}
/// Whether this Item is an equi-join condition. If this Item is a compound
/// item (i.e. multiple condition AND'ed together), it will only return true
/// if the Item contains only equi-join conditions AND'ed together. This is
/// used to determine whether the condition can be used as a join condition
/// for hash join (join conditions in hash join must be equi-join conditions),
/// or if it should be placed as a filter after the join.
virtual bool contains_only_equi_join_condition() const { return false; }
protected:
/**
Whether or not an item should contribute to the filtering effect
(@see get_filtering_effect()). First it verifies that table
requirements are satisfied as follows:
1) The item must refer to a field in 'filter_for_table' in some
way. This reference may be indirect through any number of
intermediate items. For example, this item may be an
Item_cond_and which refers to an Item_func_eq which refers to
the field.
2) The item must not refer to other tables than those already
read and the table in 'filter_for_table'
Then it contines to other properties as follows:
Item_funcs represent "<operand1> OP <operand2> [OP ...]". If the
Item_func is to contribute to the filtering effect, then
1) one of the operands must be a field from 'filter_for_table' that is not
in 'fields_to_ignore', and
2) depending on the Item_func type filtering effect is calculated
for, one or all [1] of the other operand(s) must be an available
value, i.e.:
- a constant, or
- a constant subquery, or
- a field value read from a table in 'read_tables', or
- a second field in 'filter_for_table', or
- a function that only refers to constants or tables in
'read_tables', or
- special case: an implicit value like NULL in the case of
"field IS NULL". Such Item_funcs have arg_count==1.
[1] "At least one" for multiple equality (X = Y = Z = ...), "all"
for the rest (e.g. BETWEEN)
@param thd The current thread.
@param read_tables Tables earlier in the join sequence.
Predicates for table 'filter_for_table' that
rely on values from these tables can be part of
the filter effect.
@param filter_for_table The table we are calculating filter effect for
@param fields_to_ignore Columns that should be ignored.
@return Item_field that participates in the predicate if none of the
requirements are broken, NULL otherwise
@note: This function only applies to items doing comparison, i.e.
boolean predicates. Unfortunately, some of those items do not
inherit from Item_bool_func so the member function has to be
placed in Item_func.
*/
const Item_field *contributes_to_filter(
THD *thd, table_map read_tables, table_map filter_for_table,
const MY_BITMAP *fields_to_ignore) const;
/**
Named parameters are allowed in a parameter list
The syntax to name parameters in a function call is as follow:
<code>foo(expr AS named, expr named, expr AS "named", expr "named")</code>
where "AS" is optional.
Only UDF function support that syntax.
@return true if the function item can have named parameters
*/
virtual bool may_have_named_parameters() const { return false; }
bool is_non_const_over_literals(uchar *) override { return false; }
bool check_function_as_value_generator(uchar *checker_args) override {
if (is_deprecated()) {
Check_function_as_value_generator_parameters *func_arg =
pointer_cast<Check_function_as_value_generator_parameters *>(
checker_args);
func_arg->banned_function_name = func_name();
return true;
}
return false;
}
bool is_valid_for_pushdown(uchar *arg) override;
bool check_column_in_window_functions(uchar *arg) override;
bool check_column_in_group_by(uchar *arg) override;
longlong val_int_from_real();
};
class Item_real_func : public Item_func {
public:
Item_real_func() : Item_func() { set_data_type_double(); }
explicit Item_real_func(const POS &pos) : Item_func(pos) {
set_data_type_double();
}
Item_real_func(Item *a) : Item_func(a) { set_data_type_double(); }
Item_real_func(const POS &pos, Item *a) : Item_func(pos, a) {
set_data_type_double();
}
Item_real_func(Item *a, Item *b) : Item_func(a, b) { set_data_type_double(); }
Item_real_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
set_data_type_double();
}
explicit Item_real_func(mem_root_deque<Item *> *list) : Item_func(list) {
set_data_type_double();
}
Item_real_func(const POS &pos, PT_item_list *list) : Item_func(pos, list) {
set_data_type_double();
}
String *val_str(String *str) override;
my_decimal *val_decimal(my_decimal *decimal_value) override;
longlong val_int() override {
assert(fixed);
return llrint_with_overflow_check(val_real());
}
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
return get_date_from_real(ltime, fuzzydate);
}
bool get_time(MYSQL_TIME *ltime) override {
return get_time_from_real(ltime);
}
enum Item_result result_type() const override { return REAL_RESULT; }
};
class Item_func_numhybrid : public Item_func {
protected:
Item_result hybrid_type;
public:
Item_func_numhybrid(Item *a) : Item_func(a), hybrid_type(REAL_RESULT) {
collation.set_numeric();
}
Item_func_numhybrid(const POS &pos, Item *a)
: Item_func(pos, a), hybrid_type(REAL_RESULT) {
collation.set_numeric();
}
Item_func_numhybrid(Item *a, Item *b)
: Item_func(a, b), hybrid_type(REAL_RESULT) {
collation.set_numeric();
}
Item_func_numhybrid(const POS &pos, Item *a, Item *b)
: Item_func(pos, a, b), hybrid_type(REAL_RESULT) {
collation.set_numeric();
}
explicit Item_func_numhybrid(mem_root_deque<Item *> *list)
: Item_func(list), hybrid_type(REAL_RESULT) {
collation.set_numeric();
}
Item_func_numhybrid(const POS &pos, PT_item_list *list)
: Item_func(pos, list), hybrid_type(REAL_RESULT) {
collation.set_numeric();
}
enum Item_result result_type() const override { return hybrid_type; }
enum_field_types default_data_type() const override {
return MYSQL_TYPE_DOUBLE;
}
bool resolve_type(THD *thd) override;
bool resolve_type_inner(THD *thd) override;
void fix_num_length_and_dec() override;
virtual void set_numeric_type() = 0; // To be called from resolve_type()
double val_real() override;
longlong val_int() override;
my_decimal *val_decimal(my_decimal *) override;
String *val_str(String *str) override;
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
bool get_time(MYSQL_TIME *ltime) override;
/**
@brief Performs the operation that this functions implements when the
result type is INT.
@return The result of the operation.
*/
virtual longlong int_op() = 0;
/**
@brief Performs the operation that this functions implements when the
result type is REAL.
@return The result of the operation.
*/
virtual double real_op() = 0;
/**
@brief Performs the operation that this functions implements when the
result type is DECIMAL.
@param decimal_value A pointer where the DECIMAL value will be allocated.
@return
- 0 If the result is NULL
- The same pointer it was given, with the area initialized to the
result of the operation.
*/
virtual my_decimal *decimal_op(my_decimal *decimal_value) = 0;
/**
@brief Performs the operation that this functions implements when the
result type is a string type.
@return The result of the operation.
*/
virtual String *str_op(String *) = 0;
/**
@brief Performs the operation that this functions implements when the
result type is MYSQL_TYPE_DATE or MYSQL_TYPE_DATETIME.
@return The result of the operation.
*/
virtual bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) = 0;
virtual bool time_op(MYSQL_TIME *ltime) = 0;
bool is_null() override { return update_null_value() || null_value; }
};
/* function where type of result detected by first argument */
class Item_func_num1 : public Item_func_numhybrid {
public:
Item_func_num1(Item *a) : Item_func_numhybrid(a) {}
Item_func_num1(const POS &pos, Item *a) : Item_func_numhybrid(pos, a) {}
Item_func_num1(Item *a, Item *b) : Item_func_numhybrid(a, b) {}
Item_func_num1(const POS &pos, Item *a, Item *b)
: Item_func_numhybrid(pos, a, b) {}
void fix_num_length_and_dec() override;
void set_numeric_type() override;
String *str_op(String *) override {
assert(0);
return nullptr;
}
bool date_op(MYSQL_TIME *, my_time_flags_t) override {
assert(0);
return false;
}
bool time_op(MYSQL_TIME *) override {
assert(0);
return false;
}
};
/* Base class for operations like '+', '-', '*' */
class Item_num_op : public Item_func_numhybrid {
public:
Item_num_op(Item *a, Item *b) : Item_func_numhybrid(a, b) {}
Item_num_op(const POS &pos, Item *a, Item *b)
: Item_func_numhybrid(pos, a, b) {}
virtual void result_precision() = 0;
void print(const THD *thd, String *str,
enum_query_type query_type) const override {
print_op(thd, str, query_type);
}
void set_numeric_type() override;
String *str_op(String *) override {
assert(0);
return nullptr;
}
bool date_op(MYSQL_TIME *, my_time_flags_t) override {
assert(0);
return false;
}
bool time_op(MYSQL_TIME *) override {
assert(0);
return false;
}
};
class Item_int_func : public Item_func {
public:
Item_int_func() : Item_func() { set_data_type_longlong(); }
explicit Item_int_func(const POS &pos) : Item_func(pos) {
set_data_type_longlong();
}
Item_int_func(Item *a) : Item_func(a) { set_data_type_longlong(); }
Item_int_func(const POS &pos, Item *a) : Item_func(pos, a) {
set_data_type_longlong();
}
Item_int_func(Item *a, Item *b) : Item_func(a, b) {
set_data_type_longlong();
}
Item_int_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
set_data_type_longlong();
}