/
item_func.h
4312 lines (3963 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, 2016, Oracle and/or its affiliates.
Copyright (c) 2009, 2021, MariaDB
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 Street, Fifth Floor, Boston, MA 02110-1335 USA */
/* Function items used by mysql */
#ifdef USE_PRAGMA_INTERFACE
#pragma interface /* gcc class implementation */
#endif
#ifdef HAVE_IEEEFP_H
extern "C" /* Bug in BSDI include file */
{
#include <ieeefp.h>
}
#endif
#include "sql_udf.h" // udf_handler
#include "my_decimal.h" // string2my_decimal
#include <cmath>
class Item_func :public Item_func_or_sum
{
void sync_with_sum_func_and_with_field(List<Item> &list);
protected:
virtual bool check_arguments() const
{
return check_argument_types_scalar(0, arg_count);
}
bool check_argument_types_like_args0() const;
bool check_argument_types_scalar(uint start, uint end) const;
bool check_argument_types_traditional_scalar(uint start, uint end) const;
bool check_argument_types_or_binary(const Type_handler *handler,
uint start, uint end) const;
bool check_argument_types_can_return_int(uint start, uint end) const;
bool check_argument_types_can_return_real(uint start, uint end) const;
bool check_argument_types_can_return_str(uint start, uint end) const;
bool check_argument_types_can_return_text(uint start, uint end) const;
bool check_argument_types_can_return_date(uint start, uint end) const;
bool check_argument_types_can_return_time(uint start, uint end) const;
void print_cast_temporal(String *str, enum_query_type query_type);
void print_schema_qualified_name(String *to,
const LEX_CSTRING &schema_name,
const LEX_CSTRING &function_name) const
{
// e.g. oracle_schema.func()
to->append(schema_name);
to->append('.');
to->append(function_name);
}
void print_sql_mode_qualified_name(String *to,
enum_query_type query_type,
const LEX_CSTRING &function_name) const
{
const Schema *func_schema= schema();
if (!func_schema || func_schema == Schema::find_implied(current_thd))
to->append(function_name);
else
print_schema_qualified_name(to, func_schema->name(), function_name);
}
void print_sql_mode_qualified_name(String *to, enum_query_type query_type)
const
{
return print_sql_mode_qualified_name(to, query_type, func_name_cstring());
}
bool aggregate_args2_for_comparison_with_conversion(THD *thd,
Type_handler_hybrid_field_type *th);
public:
table_map not_null_tables_cache;
enum Functype { UNKNOWN_FUNC,EQ_FUNC,EQUAL_FUNC,NE_FUNC,LT_FUNC,LE_FUNC,
GE_FUNC,GT_FUNC,FT_FUNC,
LIKE_FUNC,ISNULL_FUNC,ISNOTNULL_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_INTERSECTS_FUNC,
SP_TOUCHES_FUNC,SP_CROSSES_FUNC,SP_WITHIN_FUNC,
SP_CONTAINS_FUNC,SP_OVERLAPS_FUNC,
SP_STARTPOINT,SP_ENDPOINT,SP_EXTERIORRING,
SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN, SP_RELATE_FUNC,
NOT_FUNC, NOT_ALL_FUNC, TEMPTABLE_ROWID,
NOW_FUNC, NOW_UTC_FUNC, SYSDATE_FUNC, TRIG_COND_FUNC,
SUSERVAR_FUNC, GUSERVAR_FUNC, COLLATE_FUNC,
EXTRACT_FUNC, CHAR_TYPECAST_FUNC, FUNC_SP, UDF_FUNC,
NEG_FUNC, GSYSVAR_FUNC, IN_OPTIMIZER_FUNC, DYNCOL_FUNC,
JSON_EXTRACT_FUNC, JSON_VALID_FUNC, ROWNUM_FUNC,
CASE_SEARCHED_FUNC, // Used by ColumnStore/Spider
CASE_SIMPLE_FUNC, // Used by ColumnStore/spider,
DATE_FUNC, YEAR_FUNC
};
/*
A function bitmap. Useful when some operation needs to be applied only
to certain functions. For now we only need to distinguish some
comparison predicates.
*/
enum Bitmap : ulonglong
{
BITMAP_NONE= 0,
BITMAP_EQ= 1ULL << EQ_FUNC,
BITMAP_EQUAL= 1ULL << EQUAL_FUNC,
BITMAP_NE= 1ULL << NE_FUNC,
BITMAP_LT= 1ULL << LT_FUNC,
BITMAP_LE= 1ULL << LE_FUNC,
BITMAP_GE= 1ULL << GE_FUNC,
BITMAP_GT= 1ULL << GT_FUNC,
BITMAP_LIKE= 1ULL << LIKE_FUNC,
BITMAP_BETWEEN= 1ULL << BETWEEN,
BITMAP_IN= 1ULL << IN_FUNC,
BITMAP_MULT_EQUAL= 1ULL << MULT_EQUAL_FUNC,
BITMAP_OTHER= 1ULL << 63,
BITMAP_ALL= 0xFFFFFFFFFFFFFFFFULL,
BITMAP_ANY_EQUALITY= BITMAP_EQ | BITMAP_EQUAL | BITMAP_MULT_EQUAL,
BITMAP_EXCEPT_ANY_EQUALITY= BITMAP_ALL & ~BITMAP_ANY_EQUALITY,
};
ulonglong bitmap_bit() const
{
Functype type= functype();
return 1ULL << (type > 63 ? 63 : type);
}
static scalar_comparison_op functype_to_scalar_comparison_op(Functype type)
{
switch (type) {
case EQ_FUNC: return SCALAR_CMP_EQ;
case EQUAL_FUNC: return SCALAR_CMP_EQUAL;
case LT_FUNC: return SCALAR_CMP_LT;
case LE_FUNC: return SCALAR_CMP_LE;
case GE_FUNC: return SCALAR_CMP_GE;
case GT_FUNC: return SCALAR_CMP_GT;
default: break;
}
DBUG_ASSERT(0);
return SCALAR_CMP_EQ;
}
enum Type type() const override { return FUNC_ITEM; }
virtual enum Functype functype() const { return UNKNOWN_FUNC; }
Item_func(THD *thd): Item_func_or_sum(thd)
{
DBUG_ASSERT(with_flags == item_with_t::NONE);
with_flags= item_with_t::NONE;
}
Item_func(THD *thd, Item *a): Item_func_or_sum(thd, a)
{
with_flags= a->with_flags;
}
Item_func(THD *thd, Item *a, Item *b):
Item_func_or_sum(thd, a, b)
{
with_flags= a->with_flags | b->with_flags;
}
Item_func(THD *thd, Item *a, Item *b, Item *c):
Item_func_or_sum(thd, a, b, c)
{
with_flags|= a->with_flags | b->with_flags | c->with_flags;
}
Item_func(THD *thd, Item *a, Item *b, Item *c, Item *d):
Item_func_or_sum(thd, a, b, c, d)
{
with_flags= a->with_flags | b->with_flags | c->with_flags | d->with_flags;
}
Item_func(THD *thd, Item *a, Item *b, Item *c, Item *d, Item* e):
Item_func_or_sum(thd, a, b, c, d, e)
{
with_flags= (a->with_flags | b->with_flags | c->with_flags | d->with_flags |
e->with_flags);
}
Item_func(THD *thd, List<Item> &list):
Item_func_or_sum(thd, list)
{
set_arguments(thd, list);
}
// Constructor used for Item_cond_and/or (see Item comment)
Item_func(THD *thd, Item_func *item):
Item_func_or_sum(thd, item),
not_null_tables_cache(item->not_null_tables_cache)
{ }
bool fix_fields(THD *, Item **ref) override;
void cleanup() override
{
Item_func_or_sum::cleanup();
used_tables_and_const_cache_init();
}
void fix_after_pullout(st_select_lex *new_parent, Item **ref, bool merge)
override;
void quick_fix_field() override;
table_map not_null_tables() const override;
void update_used_tables() override
{
used_tables_and_const_cache_init();
used_tables_and_const_cache_update_and_join(arg_count, args);
}
COND *build_equal_items(THD *thd, COND_EQUAL *inherited,
bool link_item_fields,
COND_EQUAL **cond_equal_ref) override;
SEL_TREE *get_mm_tree(RANGE_OPT_PARAM *param, Item **cond_ptr) override
{
DBUG_ENTER("Item_func::get_mm_tree");
DBUG_RETURN(const_item() ? get_mm_tree_for_const(param) : NULL);
}
bool eq(const Item *item, bool binary_cmp) const override;
virtual Item *key_item() const { return args[0]; }
void set_arguments(THD *thd, List<Item> &list)
{
Item_args::set_arguments(thd, list);
sync_with_sum_func_and_with_field(list);
list.empty(); // Fields are used
}
void split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
List<Item> &fields, uint flags) override;
void print(String *str, enum_query_type query_type) override;
void print_op(String *str, enum_query_type query_type);
void print_args(String *str, uint from, enum_query_type query_type) const;
void print_args_parenthesized(String *str, enum_query_type query_type) const
{
str->append('(');
print_args(str, 0, query_type);
str->append(')');
}
bool is_null() override
{
update_null_value();
return null_value;
}
String *val_str_from_val_str_ascii(String *str, String *str2);
void signal_divide_by_null();
friend class udf_handler;
Field *create_field_for_create_select(MEM_ROOT *root, TABLE *table) override
{ return tmp_table_field_from_field_type(root, table); }
Item *get_tmp_table_item(THD *thd) override;
void fix_char_length_ulonglong(ulonglong max_char_length_arg)
{
ulonglong max_result_length= max_char_length_arg *
collation.collation->mbmaxlen;
if (max_result_length >= MAX_BLOB_WIDTH)
{
max_length= MAX_BLOB_WIDTH;
set_maybe_null();
}
else
max_length= (uint32) max_result_length;
}
Item *transform(THD *thd, Item_transformer transformer, uchar *arg) override;
Item* compile(THD *thd, 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 eval_not_null_tables(void *opt_arg) override;
bool find_not_null_fields(table_map allowed) override;
// bool is_expensive_processor(void *arg);
// virtual bool is_expensive() { return 0; }
inline void raise_numeric_overflow(const char *type_name)
{
char buf[256];
String str(buf, sizeof(buf), system_charset_info);
str.length(0);
print(&str, QT_NO_DATA_EXPANSION);
my_error(ER_DATA_OUT_OF_RANGE, MYF(0), type_name, str.c_ptr_safe());
}
inline double raise_float_overflow()
{
raise_numeric_overflow("DOUBLE");
return 0.0;
}
inline longlong raise_integer_overflow()
{
raise_numeric_overflow(unsigned_flag ? "BIGINT UNSIGNED": "BIGINT");
return 0;
}
inline int raise_decimal_overflow()
{
raise_numeric_overflow("DECIMAL");
return E_DEC_OVERFLOW;
}
/**
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)
{
return check_integer_overflow(Longlong_hybrid(value, val_unsigned));
}
// Check if the value is compatible with Item::unsigned_flag.
inline longlong check_integer_overflow(const Longlong_hybrid &sval)
{
Longlong_null res= sval.val_int(unsigned_flag);
return res.is_null() ? raise_integer_overflow() : res.value();
}
// Check if the value is compatible with Item::unsigned_flag.
longlong check_integer_overflow(const ULonglong_hybrid &uval)
{
Longlong_null res= uval.val_int(unsigned_flag);
return res.is_null() ? raise_integer_overflow() : res.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()
{
DBUG_ASSERT(fixed());
for (uint i= 0; i < arg_count; i++)
{
if (args[i]->type() == Item::FIELD_ITEM &&
args[i]->field_type() == MYSQL_TYPE_TIMESTAMP)
return TRUE;
}
return FALSE;
}
bool has_date_args()
{
DBUG_ASSERT(fixed());
for (uint i= 0; i < arg_count; i++)
{
if (args[i]->type() == Item::FIELD_ITEM &&
(args[i]->field_type() == MYSQL_TYPE_DATE ||
args[i]->field_type() == MYSQL_TYPE_DATETIME))
return TRUE;
}
return FALSE;
}
bool has_time_args()
{
DBUG_ASSERT(fixed());
for (uint i= 0; i < arg_count; i++)
{
if (args[i]->type() == Item::FIELD_ITEM &&
(args[i]->field_type() == MYSQL_TYPE_TIME ||
args[i]->field_type() == MYSQL_TYPE_DATETIME))
return TRUE;
}
return FALSE;
}
bool has_datetime_args()
{
DBUG_ASSERT(fixed());
for (uint i= 0; i < arg_count; i++)
{
if (args[i]->type() == Item::FIELD_ITEM &&
args[i]->field_type() == MYSQL_TYPE_DATETIME)
return TRUE;
}
return FALSE;
}
Item* propagate_equal_fields(THD *thd, const Context &ctx, COND_EQUAL *cond)
override
{
/*
By default only substitution for a field whose two different values
are never equal is allowed in the arguments of a function.
This is overruled for the direct arguments of comparison functions.
*/
Item_args::propagate_equal_fields(thd, Context_identity(), cond);
return this;
}
bool has_rand_bit()
{
return used_tables() & RAND_TABLE_BIT;
}
bool excl_dep_on_table(table_map tab_map) override
{
if (used_tables() & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
return false;
return !(used_tables() & ~tab_map) ||
Item_args::excl_dep_on_table(tab_map);
}
bool excl_dep_on_grouping_fields(st_select_lex *sel) override
{
if (has_rand_bit() || with_subquery())
return false;
return Item_args::excl_dep_on_grouping_fields(sel);
}
bool excl_dep_on_in_subq_left_part(Item_in_subselect *subq_pred) override
{
return Item_args::excl_dep_on_in_subq_left_part(subq_pred);
}
/*
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(void *bool_arg) override
{
return has_timestamp_args();
}
bool find_function_processor (void *arg) override
{
return functype() == *(Functype *) arg;
}
void no_rows_in_result() override
{
for (uint i= 0; i < arg_count; i++)
{
args[i]->no_rows_in_result();
}
}
void restore_to_before_no_rows_in_result() override
{
for (uint i= 0; i < arg_count; i++)
{
args[i]->restore_to_before_no_rows_in_result();
}
}
void convert_const_compared_to_int_field(THD *thd);
Item_func *get_item_func() override { return this; }
bool is_simplified_cond_processor(void *arg) override
{ return const_item() && !val_int(); }
};
class Item_real_func :public Item_func
{
public:
Item_real_func(THD *thd): Item_func(thd) { collation= DTCollation_numeric(); }
Item_real_func(THD *thd, Item *a): Item_func(thd, a)
{ collation= DTCollation_numeric(); }
Item_real_func(THD *thd, Item *a, Item *b): Item_func(thd, a, b)
{ collation= DTCollation_numeric(); }
Item_real_func(THD *thd, List<Item> &list): Item_func(thd, list)
{ collation= DTCollation_numeric(); }
String *val_str(String*str) override;
my_decimal *val_decimal(my_decimal *decimal_value) override;
longlong val_int() override
{
DBUG_ASSERT(fixed());
return Converter_double_to_longlong(val_real(), unsigned_flag).result();
}
bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
{ return get_date_from_real(thd, ltime, fuzzydate); }
const Type_handler *type_handler() const override
{ return &type_handler_double; }
bool fix_length_and_dec(THD *thd) override
{
decimals= NOT_FIXED_DEC;
max_length= float_length(decimals);
return FALSE;
}
};
/**
Functions whose returned field type is determined at fix_fields() time.
*/
class Item_hybrid_func: public Item_func,
public Type_handler_hybrid_field_type,
public Type_extra_attributes
{
protected:
bool fix_attributes(Item **item, uint nitems);
public:
Item_hybrid_func(THD *thd): Item_func(thd) { }
Item_hybrid_func(THD *thd, Item *a): Item_func(thd, a) { }
Item_hybrid_func(THD *thd, Item *a, Item *b): Item_func(thd, a, b) { }
Item_hybrid_func(THD *thd, Item *a, Item *b, Item *c):
Item_func(thd, a, b, c) { }
Item_hybrid_func(THD *thd, List<Item> &list): Item_func(thd, list) { }
Item_hybrid_func(THD *thd, Item_hybrid_func *item)
:Item_func(thd, item), Type_handler_hybrid_field_type(item) { }
const Type_handler *type_handler() const override
{ return Type_handler_hybrid_field_type::type_handler(); }
Type_extra_attributes *type_extra_attributes_addr() override
{
return this;
}
const Type_extra_attributes type_extra_attributes() const override
{
return *this;
}
void fix_length_and_dec_long_or_longlong(uint char_length, bool unsigned_arg)
{
collation= DTCollation_numeric();
unsigned_flag= unsigned_arg;
max_length= char_length;
set_handler(Type_handler::type_handler_long_or_longlong(char_length,
unsigned_arg));
}
void fix_length_and_dec_ulong_or_ulonglong_by_nbits(uint nbits)
{
uint digits= Type_handler_bit::Bit_decimal_notation_int_digits_by_nbits(nbits);
collation= DTCollation_numeric();
unsigned_flag= true;
max_length= digits;
if (nbits > 32)
set_handler(&type_handler_ulonglong);
else
set_handler(&type_handler_ulong);
}
};
class Item_handled_func: public Item_func
{
public:
class Handler
{
public:
virtual ~Handler() = default;
virtual String *val_str(Item_handled_func *, String *) const= 0;
virtual String *val_str_ascii(Item_handled_func *, String *) const= 0;
virtual double val_real(Item_handled_func *) const= 0;
virtual longlong val_int(Item_handled_func *) const= 0;
virtual my_decimal *val_decimal(Item_handled_func *, my_decimal *) const= 0;
virtual bool get_date(THD *thd, Item_handled_func *, MYSQL_TIME *, date_mode_t fuzzydate) const= 0;
virtual bool val_native(THD *thd, Item_handled_func *, Native *to) const
{
DBUG_ASSERT(0);
to->length(0);
return true;
}
virtual const Type_handler *
return_type_handler(const Item_handled_func *item) const= 0;
virtual const Type_handler *
type_handler_for_create_select(const Item_handled_func *item) const
{
return return_type_handler(item);
}
virtual bool fix_length_and_dec(Item_handled_func *) const= 0;
};
class Handler_str: public Handler
{
public:
String *val_str_ascii(Item_handled_func *item, String *str) const
{
return item->Item::val_str_ascii(str);
}
double val_real(Item_handled_func *item) const
{
DBUG_ASSERT(item->fixed());
StringBuffer<64> tmp;
String *res= item->val_str(&tmp);
return res ? item->double_from_string_with_check(res) : 0.0;
}
longlong val_int(Item_handled_func *item) const
{
DBUG_ASSERT(item->fixed());
StringBuffer<22> tmp;
String *res= item->val_str(&tmp);
return res ? item->longlong_from_string_with_check(res) : 0;
}
my_decimal *val_decimal(Item_handled_func *item, my_decimal *to) const
{
return item->val_decimal_from_string(to);
}
bool get_date(THD *thd, Item_handled_func *item, MYSQL_TIME *to,
date_mode_t fuzzydate) const
{
return item->get_date_from_string(thd, to, fuzzydate);
}
};
/**
Abstract class for functions returning TIME, DATE, DATETIME or string values,
whose data type depends on parameters and is set at fix_fields time.
*/
class Handler_temporal: public Handler
{
public:
String *val_str(Item_handled_func *item, String *to) const
{
StringBuffer<MAX_FIELD_WIDTH> ascii_buf;
return item->val_str_from_val_str_ascii(to, &ascii_buf);
}
};
/**
Abstract class for functions returning strings,
which are generated from get_date() results,
when get_date() can return different MYSQL_TIMESTAMP_XXX per row.
*/
class Handler_temporal_string: public Handler_temporal
{
public:
const Type_handler *return_type_handler(const Item_handled_func *) const
{
return &type_handler_string;
}
const Type_handler *
type_handler_for_create_select(const Item_handled_func *item) const
{
return return_type_handler(item)->type_handler_for_tmp_table(item);
}
double val_real(Item_handled_func *item) const
{
return Temporal_hybrid(item).to_double();
}
longlong val_int(Item_handled_func *item) const
{
return Temporal_hybrid(item).to_longlong();
}
my_decimal *val_decimal(Item_handled_func *item, my_decimal *to) const
{
return Temporal_hybrid(item).to_decimal(to);
}
String *val_str_ascii(Item_handled_func *item, String *to) const
{
return Temporal_hybrid(item).to_string(to, item->decimals);
}
};
class Handler_date: public Handler_temporal
{
public:
const Type_handler *return_type_handler(const Item_handled_func *) const
{
return &type_handler_newdate;
}
bool fix_length_and_dec(Item_handled_func *item) const
{
item->fix_attributes_date();
return false;
}
double val_real(Item_handled_func *item) const
{
return Date(item).to_double();
}
longlong val_int(Item_handled_func *item) const
{
return Date(item).to_longlong();
}
my_decimal *val_decimal(Item_handled_func *item, my_decimal *to) const
{
return Date(item).to_decimal(to);
}
String *val_str_ascii(Item_handled_func *item, String *to) const
{
return Date(item).to_string(to);
}
};
class Handler_time: public Handler_temporal
{
public:
const Type_handler *return_type_handler(const Item_handled_func *) const
{
return &type_handler_time2;
}
double val_real(Item_handled_func *item) const
{
return Time(item).to_double();
}
longlong val_int(Item_handled_func *item) const
{
return Time(item).to_longlong();
}
my_decimal *val_decimal(Item_handled_func *item, my_decimal *to) const
{
return Time(item).to_decimal(to);
}
String *val_str_ascii(Item_handled_func *item, String *to) const
{
return Time(item).to_string(to, item->decimals);
}
bool val_native(THD *thd, Item_handled_func *item, Native *to) const
{
return Time(thd, item).to_native(to, item->decimals);
}
};
class Handler_datetime: public Handler_temporal
{
public:
const Type_handler *return_type_handler(const Item_handled_func *) const
{
return &type_handler_datetime2;
}
double val_real(Item_handled_func *item) const
{
return Datetime(item).to_double();
}
longlong val_int(Item_handled_func *item) const
{
return Datetime(item).to_longlong();
}
my_decimal *val_decimal(Item_handled_func *item, my_decimal *to) const
{
return Datetime(item).to_decimal(to);
}
String *val_str_ascii(Item_handled_func *item, String *to) const
{
return Datetime(item).to_string(to, item->decimals);
}
};
class Handler_int: public Handler
{
public:
String *val_str(Item_handled_func *item, String *to) const
{
longlong nr= val_int(item);
if (item->null_value)
return 0;
to->set_int(nr, item->unsigned_flag, item->collation.collation);
return to;
}
String *val_str_ascii(Item_handled_func *item, String *to) const
{
return item->Item::val_str_ascii(to);
}
double val_real(Item_handled_func *item) const
{
return item->unsigned_flag ? (double) ((ulonglong) val_int(item)) :
(double) val_int(item);
}
my_decimal *val_decimal(Item_handled_func *item, my_decimal *to) const
{
return item->val_decimal_from_int(to);
}
bool get_date(THD *thd, Item_handled_func *item,
MYSQL_TIME *to, date_mode_t fuzzydate) const
{
return item->get_date_from_int(thd, to, fuzzydate);
}
longlong val_int(Item_handled_func *item) const
{
Longlong_null tmp= to_longlong_null(item);
item->null_value= tmp.is_null();
return tmp.value();
}
virtual Longlong_null to_longlong_null(Item_handled_func *item) const= 0;
};
class Handler_slong: public Handler_int
{
public:
const Type_handler *return_type_handler(const Item_handled_func *item) const
{
return &type_handler_slong;
}
bool fix_length_and_dec(Item_handled_func *item) const
{
item->unsigned_flag= false;
item->collation= DTCollation_numeric();
item->fix_char_length(11);
return false;
}
};
class Handler_slong2: public Handler_slong
{
public:
bool fix_length_and_dec(Item_handled_func *func) const
{
bool rc= Handler_slong::fix_length_and_dec(func);
func->max_length= 2;
return rc;
}
};
class Handler_ulonglong: public Handler_int
{
public:
const Type_handler *return_type_handler(const Item_handled_func *item) const
{
return &type_handler_ulonglong;
}
bool fix_length_and_dec(Item_handled_func *item) const
{
item->unsigned_flag= true;
item->collation= DTCollation_numeric();
item->fix_char_length(21);
return false;
}
};
protected:
const Handler *m_func_handler;
public:
Item_handled_func(THD *thd, Item *a)
:Item_func(thd, a), m_func_handler(NULL) { }
Item_handled_func(THD *thd, Item *a, Item *b)
:Item_func(thd, a, b), m_func_handler(NULL) { }
void set_func_handler(const Handler *handler)
{
m_func_handler= handler;
}
const Type_handler *type_handler() const override
{
return m_func_handler->return_type_handler(this);
}
Field *create_field_for_create_select(MEM_ROOT *root, TABLE *table) override
{
DBUG_ASSERT(fixed());
const Type_handler *h= m_func_handler->type_handler_for_create_select(this);
return h->make_and_init_table_field(root, &name,
Record_addr(maybe_null()),
*this, table);
}
String *val_str(String *to) override
{
return m_func_handler->val_str(this, to);
}
String *val_str_ascii(String *to) override
{
return m_func_handler->val_str_ascii(this, to);
}
double val_real() override
{
return m_func_handler->val_real(this);
}
longlong val_int() override
{
return m_func_handler->val_int(this);
}
my_decimal *val_decimal(my_decimal *to) override
{
return m_func_handler->val_decimal(this, to);
}
bool get_date(THD *thd, MYSQL_TIME *to, date_mode_t fuzzydate) override
{
return m_func_handler->get_date(thd, this, to, fuzzydate);
}
bool val_native(THD *thd, Native *to) override
{
return m_func_handler->val_native(thd, this, to);
}
};
/**
Functions that at fix_fields() time determine the returned field type,
trying to preserve the exact data type of the arguments.
The descendants have to implement "native" value methods,
i.e. str_op(), date_op(), int_op(), real_op(), decimal_op().
fix_fields() chooses which of the above value methods will be
used during execution time, according to the returned field type.
For example, if fix_fields() determines that the returned value type
is MYSQL_TYPE_LONG, then:
- int_op() is chosen as the execution time native method.
- val_int() returns the result of int_op() as is.
- all other methods, i.e. val_real(), val_decimal(), val_str(), get_date(),
call int_op() first, then convert the result to the requested data type.
*/
class Item_func_hybrid_field_type: public Item_hybrid_func
{
/*
Helper methods to make sure that the result of
decimal_op(), str_op() and date_op() is properly synched with null_value.
*/
bool date_op_with_null_check(THD *thd, MYSQL_TIME *ltime)
{
bool rc= date_op(thd, ltime, date_mode_t(0));
DBUG_ASSERT(!rc ^ null_value);
return rc;
}
bool time_op_with_null_check(THD *thd, MYSQL_TIME *ltime)
{
bool rc= time_op(thd, ltime);
DBUG_ASSERT(!rc ^ null_value);
DBUG_ASSERT(rc || ltime->time_type == MYSQL_TIMESTAMP_TIME);
return rc;
}
String *str_op_with_null_check(String *str)
{
String *res= str_op(str);
DBUG_ASSERT((res != NULL) ^ null_value);
return res;
}
public:
// Value methods that involve no conversion
String *val_str_from_str_op(String *str)
{
return str_op_with_null_check(&str_value);
}
longlong val_int_from_int_op()
{
return int_op();
}
double val_real_from_real_op()
{
return real_op();
}
// Value methods that involve conversion
String *val_str_from_real_op(String *str);
String *val_str_from_int_op(String *str);
String *val_str_from_date_op(String *str);
String *val_str_from_time_op(String *str);
my_decimal *val_decimal_from_str_op(my_decimal *dec);
my_decimal *val_decimal_from_real_op(my_decimal *dec);
my_decimal *val_decimal_from_int_op(my_decimal *dec);
my_decimal *val_decimal_from_date_op(my_decimal *dec);
my_decimal *val_decimal_from_time_op(my_decimal *dec);
longlong val_int_from_str_op();
longlong val_int_from_real_op();
longlong val_int_from_date_op();
longlong val_int_from_time_op();
double val_real_from_str_op();
double val_real_from_date_op();
double val_real_from_time_op();
double val_real_from_int_op();
public:
Item_func_hybrid_field_type(THD *thd):
Item_hybrid_func(thd)
{ collation= DTCollation_numeric(); }
Item_func_hybrid_field_type(THD *thd, Item *a):
Item_hybrid_func(thd, a)
{ collation= DTCollation_numeric(); }
Item_func_hybrid_field_type(THD *thd, Item *a, Item *b):
Item_hybrid_func(thd, a, b)
{ collation= DTCollation_numeric(); }
Item_func_hybrid_field_type(THD *thd, Item *a, Item *b, Item *c):
Item_hybrid_func(thd, a, b, c)
{ collation= DTCollation_numeric(); }
Item_func_hybrid_field_type(THD *thd, List<Item> &list):
Item_hybrid_func(thd, list)
{ collation= DTCollation_numeric(); }
double val_real() override
{
DBUG_ASSERT(fixed());
return Item_func_hybrid_field_type::type_handler()->
Item_func_hybrid_field_type_val_real(this);
}
longlong val_int() override
{
DBUG_ASSERT(fixed());
return Item_func_hybrid_field_type::type_handler()->
Item_func_hybrid_field_type_val_int(this);
}
my_decimal *val_decimal(my_decimal *dec) override
{
DBUG_ASSERT(fixed());
return Item_func_hybrid_field_type::type_handler()->
Item_func_hybrid_field_type_val_decimal(this, dec);
}
String *val_str(String*str) override
{
DBUG_ASSERT(fixed());
String *res= Item_func_hybrid_field_type::type_handler()->
Item_func_hybrid_field_type_val_str(this, str);
DBUG_ASSERT(null_value == (res == NULL));
return res;
}