-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathsql_type_fixedbin.h
1990 lines (1867 loc) · 63.8 KB
/
sql_type_fixedbin.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 SQL_TYPE_FIXEDBIN_H
#define SQL_TYPE_FIXEDBIN_H
/* Copyright (c) 2019,2021 MariaDB Corporation
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-1335 USA */
/*
This is a common code for plugin (?) types that are generally
handled like strings, but have their own fixed size on-disk binary storage
format and their own (variable size) canonical string representation.
Examples are INET6 and UUID types.
*/
#define MYSQL_SERVER
#include "sql_class.h" // THD, SORT_FIELD_ATTR
#include "opt_range.h" // SEL_ARG, null_element
#include "sql_type_fixedbin_storage.h"
/***********************************************************************/
template<class FbtImpl> class Type_collection_fbt;
template<class FbtImpl, class TypeCollectionImpl = Type_collection_fbt<FbtImpl> >
class Type_handler_fbt: public Type_handler
{
/* =[ internal helper classes ]=============================== */
public:
class Fbt: public FbtImpl
{
protected:
using FbtImpl::m_buffer;
bool make_from_item(Item *item, bool warn)
{
if (item->type_handler() == singleton())
{
Native tmp(m_buffer, sizeof(m_buffer));
bool rc= item->val_native(current_thd, &tmp);
if (rc)
return true;
DBUG_ASSERT(tmp.length() == sizeof(m_buffer));
if (tmp.ptr() != m_buffer)
memcpy(m_buffer, tmp.ptr(), sizeof(m_buffer));
return false;
}
StringBuffer<FbtImpl::max_char_length()+1> tmp;
String *str= item->val_str(&tmp);
return str ? make_from_character_or_binary_string(str, warn) : true;
}
bool character_string_to_fbt(const char *str, size_t str_length,
CHARSET_INFO *cs)
{
if (cs->state & MY_CS_NONASCII)
{
char tmp[FbtImpl::max_char_length()+1];
String_copier copier;
uint length= copier.well_formed_copy(&my_charset_latin1, tmp, sizeof(tmp),
cs, str, str_length);
return FbtImpl::ascii_to_fbt(tmp, length);
}
return FbtImpl::ascii_to_fbt(str, str_length);
}
bool make_from_character_or_binary_string(const String *str, bool warn)
{
if (str->charset() != &my_charset_bin)
{
bool rc= character_string_to_fbt(str->ptr(), str->length(),
str->charset());
if (rc && warn)
current_thd->push_warning_wrong_value(Sql_condition::WARN_LEVEL_WARN,
singleton()->name().ptr(), ErrConvString(str).ptr());
return rc;
}
if (str->length() != sizeof(m_buffer))
{
if (warn)
current_thd->push_warning_wrong_value(Sql_condition::WARN_LEVEL_WARN,
singleton()->name().ptr(), ErrConvString(str).ptr());
return true;
}
DBUG_ASSERT(str->ptr() != m_buffer);
memcpy(m_buffer, str->ptr(), sizeof(m_buffer));
return false;
}
bool binary_to_fbt(const char *str, size_t length)
{
if (length != sizeof(m_buffer))
return true;
memcpy(m_buffer, str, length);
return false;
}
Fbt() { }
public:
static Fbt zero()
{
Fbt fbt;
fbt.set_zero();
return fbt;
}
static Fbt record_to_memory(const char *ptr)
{
Fbt fbt;
FbtImpl::record_to_memory(fbt.m_buffer, ptr);
return fbt;
}
/*
Check at Item's fix_fields() time if "item" can return a nullable value
on conversion to Fbt, or conversion produces a NOT NULL Fbt value.
*/
static bool fix_fields_maybe_null_on_conversion_to_fbt(Item *item)
{
if (item->maybe_null())
return true;
if (item->type_handler() == singleton())
return false;
if (!item->const_item() || item->is_expensive())
return true;
return Fbt_null(item, false).is_null();
}
/*
Check at fix_fields() time if any of the items can return a nullable
value on conversion to Fbt.
*/
static bool fix_fields_maybe_null_on_conversion_to_fbt(Item **items,
uint count)
{
for (uint i= 0; i < count; i++)
{
if (Fbt::fix_fields_maybe_null_on_conversion_to_fbt(items[i]))
return true;
}
return false;
}
public:
Fbt(Item *item, bool *error, bool warn= true)
{
*error= make_from_item(item, warn);
}
void to_record(char *str, size_t str_size) const
{
DBUG_ASSERT(str_size >= sizeof(m_buffer));
FbtImpl::memory_to_record(str, m_buffer);
}
bool to_binary(String *to) const
{
return to->copy(m_buffer, sizeof(m_buffer), &my_charset_bin);
}
bool to_native(Native *to) const
{
return to->copy(m_buffer, sizeof(m_buffer));
}
bool to_string(String *to) const
{
to->set_charset(&my_charset_latin1);
if (to->alloc(FbtImpl::max_char_length()+1))
return true;
to->length((uint32) FbtImpl::to_string(const_cast<char*>(to->ptr()),
FbtImpl::max_char_length()+1));
return false;
}
bool to_bool() const
{
return !this->only_zero_bytes(m_buffer, FbtImpl::binary_length());
}
int cmp(const Binary_string &other) const
{
return FbtImpl::cmp(FbtImpl::to_lex_cstring(), other.to_lex_cstring());
}
int cmp(const Fbt &other) const
{
return FbtImpl::cmp(FbtImpl::to_lex_cstring(), other.to_lex_cstring());
}
};
class Fbt_null: public Fbt, public Null_flag
{
public:
// Initialize from a text representation
Fbt_null(const char *str, size_t length, CHARSET_INFO *cs)
:Null_flag(Fbt::character_string_to_fbt(str, length, cs)) { }
Fbt_null(const String &str)
:Fbt_null(str.ptr(), str.length(), str.charset()) { }
// Initialize from a binary representation
Fbt_null(const char *str, size_t length)
:Null_flag(Fbt::binary_to_fbt(str, length)) { }
Fbt_null(const Binary_string &str)
:Fbt_null(str.ptr(), str.length()) { }
// Initialize from an Item
Fbt_null(Item *item, bool warn= true)
:Null_flag(Fbt::make_from_item(item, warn)) { }
public:
const Fbt& to_fbt() const
{
DBUG_ASSERT(!is_null());
return *this;
}
void to_record(char *str, size_t str_size) const
{
to_fbt().to_record(str, str_size);
}
bool to_binary(String *to) const
{
return to_fbt().to_binary(to);
}
bool to_string(String *to) const
{
return to_fbt().to_string(to);
}
};
/* =[ API classes ]=========================================== */
class Type_std_attributes_fbt: public Type_std_attributes
{
public:
Type_std_attributes_fbt()
:Type_std_attributes(
Type_numeric_attributes(FbtImpl::max_char_length(), 0, true),
DTCollation_numeric())
{ }
};
class Item_literal_fbt: public Item_literal
{
Fbt m_value;
public:
Item_literal_fbt(THD *thd)
:Item_literal(thd),
m_value(Fbt::zero())
{ }
Item_literal_fbt(THD *thd, const Fbt &value)
:Item_literal(thd),
m_value(value)
{ }
const Type_handler *type_handler() const override
{
return singleton();
}
bool val_bool() override
{
return m_value.to_bool();
}
longlong val_int() override
{
return 0;
}
double val_real() override
{
return 0;
}
String *val_str(String *to) override
{
return m_value.to_string(to) ? NULL : to;
}
my_decimal *val_decimal(my_decimal *to) override
{
my_decimal_set_zero(to);
return to;
}
bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
{
set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
return false;
}
bool val_native(THD *thd, Native *to) override
{
return m_value.to_native(to);
}
void print(String *str, enum_query_type query_type) override
{
StringBuffer<FbtImpl::max_char_length()+64> tmp;
str->append(singleton()->name().lex_cstring());
str->append('\'');
m_value.to_string(&tmp);
str->append(tmp);
str->append('\'');
}
Item *do_get_copy(THD *thd) const override
{ return get_item_copy<Item_literal_fbt>(thd, this); }
Item *do_build_clone(THD *thd) const override { return get_copy(thd); }
// Non-overriding methods
void set_value(const Fbt &value)
{
m_value= value;
}
};
class Field_fbt: public Field
{
static void set_min_value(char *ptr)
{
memset(ptr, 0, FbtImpl::binary_length());
}
static void set_max_value(char *ptr)
{
memset(ptr, 0xFF, FbtImpl::binary_length());
}
void store_warning(const ErrConv &str,
Sql_condition::enum_warning_level level)
{
if (get_thd()->count_cuted_fields <= CHECK_FIELD_EXPRESSION)
return;
const TABLE_SHARE *s= table->s;
static const Name type_name= singleton()->name();
get_thd()->push_warning_truncated_value_for_field(level, type_name.ptr(),
str.ptr(), s ? s->db.str : nullptr, s ? s->table_name.str : nullptr,
field_name.str);
}
int set_null_with_warn(const ErrConv &str)
{
store_warning(str, Sql_condition::WARN_LEVEL_WARN);
set_null();
return 1;
}
int set_min_value_with_warn(const ErrConv &str)
{
store_warning(str, Sql_condition::WARN_LEVEL_WARN);
set_min_value((char*) ptr);
return 1;
}
int set_max_value_with_warn(const ErrConv &str)
{
store_warning(str, Sql_condition::WARN_LEVEL_WARN);
set_max_value((char*) ptr);
return 1;
}
int store_fbt_null_with_warn(const Fbt_null &fbt,
const ErrConvString &err)
{
DBUG_ASSERT(marked_for_write_or_computed());
if (fbt.is_null())
return maybe_null() ? set_null_with_warn(err)
: set_min_value_with_warn(err);
fbt.to_record((char *) ptr, FbtImpl::binary_length());
return 0;
}
public:
Field_fbt(const LEX_CSTRING *field_name_arg, const Record_addr &rec)
:Field(rec.ptr(), FbtImpl::max_char_length(),
rec.null_ptr(), rec.null_bit(), Field::NONE, field_name_arg)
{
flags|= BINARY_FLAG | UNSIGNED_FLAG;
}
const Type_handler *type_handler() const override
{
return singleton();
}
uint32 max_display_length() const override { return field_length; }
bool str_needs_quotes() const override { return true; }
const DTCollation &dtcollation() const override
{
static DTCollation_numeric c;
return c;
}
CHARSET_INFO *charset(void) const override { return &my_charset_numeric; }
const CHARSET_INFO *sort_charset(void) const override { return &my_charset_bin; }
/**
This makes client-server protocol convert the value according
to @@character_set_client.
*/
bool binary() const override { return false; }
enum ha_base_keytype key_type() const override { return HA_KEYTYPE_BINARY; }
bool is_equal(const Column_definition &new_field) const override
{
return new_field.type_handler() == type_handler();
}
bool eq_def(const Field *field) const override
{
return Field::eq_def(field);
}
double pos_in_interval(Field *min, Field *max) override
{
return pos_in_interval_val_str(min, max, 0);
}
int cmp(const uchar *a, const uchar *b) const override
{ return memcmp(a, b, pack_length()); }
void sort_string(uchar *to, uint length) override
{
DBUG_ASSERT(length == pack_length());
memcpy(to, ptr, length);
}
uint32 pack_length() const override
{
return FbtImpl::binary_length();
}
uint pack_length_from_metadata(uint field_metadata) const override
{
return FbtImpl::binary_length();
}
void sql_type(String &str) const override
{
static Name name= singleton()->name();
str.set_ascii(name.ptr(), name.length());
}
void make_send_field(Send_field *to) override
{
Field::make_send_field(to);
to->set_data_type_name(singleton()->name().lex_cstring());
}
bool validate_value_in_record(THD *thd, const uchar *record) const override
{
return false;
}
bool val_native(Native *to) override
{
DBUG_ASSERT(marked_for_read());
if (to->alloc(FbtImpl::binary_length()))
return true;
to->length(FbtImpl::binary_length());
FbtImpl::record_to_memory((char*) to->ptr(), (const char*) ptr);
return false;
}
Fbt to_fbt() const
{
DBUG_ASSERT(marked_for_read());
return Fbt::record_to_memory((const char*) ptr);
}
String *val_str(String *val_buffer, String *) override
{
return to_fbt().to_string(val_buffer) ? NULL : val_buffer;
}
my_decimal *val_decimal(my_decimal *to) override
{
DBUG_ASSERT(marked_for_read());
my_decimal_set_zero(to);
return to;
}
longlong val_int() override
{
DBUG_ASSERT(marked_for_read());
return 0;
}
double val_real() override
{
DBUG_ASSERT(marked_for_read());
return 0;
}
bool get_date(MYSQL_TIME *ltime, date_mode_t fuzzydate) override
{
DBUG_ASSERT(marked_for_read());
set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
return false;
}
bool val_bool(void) override
{
DBUG_ASSERT(marked_for_read());
return !Fbt::only_zero_bytes((const char *) ptr, FbtImpl::binary_length());
}
int store_native(const Native &value) override
{
DBUG_ASSERT(marked_for_write_or_computed());
DBUG_ASSERT(value.length() == FbtImpl::binary_length());
FbtImpl::memory_to_record((char*) ptr, value.ptr());
return 0;
}
int store(const char *str, size_t length, CHARSET_INFO *cs) override
{
return cs == &my_charset_bin ? store_binary(str, length)
: store_text(str, length, cs);
}
int store_text(const char *str, size_t length, CHARSET_INFO *cs) override
{
return store_fbt_null_with_warn(Fbt_null(str, length, cs),
ErrConvString(str, length, cs));
}
int store_binary(const char *str, size_t length) override
{
return store_fbt_null_with_warn(Fbt_null(str, length),
ErrConvString(str, length,
&my_charset_bin));
}
int store_hex_hybrid(const char *str, size_t length) override
{
return Field_fbt::store_binary(str, length);
}
int store_decimal(const my_decimal *num) override
{
DBUG_ASSERT(marked_for_write_or_computed());
return set_min_value_with_warn(ErrConvDecimal(num));
}
int store(longlong nr, bool unsigned_flag) override
{
DBUG_ASSERT(marked_for_write_or_computed());
return set_min_value_with_warn(
ErrConvInteger(Longlong_hybrid(nr, unsigned_flag)));
}
int store(double nr) override
{
DBUG_ASSERT(marked_for_write_or_computed());
return set_min_value_with_warn(ErrConvDouble(nr));
}
int store_time_dec(const MYSQL_TIME *ltime, uint dec) override
{
DBUG_ASSERT(marked_for_write_or_computed());
return set_min_value_with_warn(ErrConvTime(ltime));
}
/*** Field conversion routines ***/
int store_field(Field *from) override
{
// INSERT INTO t1 (fbt_field) SELECT different_field_type FROM t2;
return from->save_in_field(this);
}
int save_in_field(Field *to) override
{
// INSERT INTO t2 (different_field_type) SELECT fbt_field FROM t1;
if (to->charset() == &my_charset_bin &&
dynamic_cast<const Type_handler_general_purpose_string*>
(to->type_handler()))
{
NativeBuffer<FbtImpl::binary_length()+1> res;
val_native(&res);
return to->store(res.ptr(), res.length(), &my_charset_bin);
}
return save_in_field_str(to);
}
Copy_func *get_copy_func(const Field *from) const override
{
// ALTER to FBT from another field
return do_field_string;
}
Copy_func *get_copy_func_to(const Field *to) const override
{
if (type_handler() == to->type_handler())
{
// ALTER from FBT to FBT
DBUG_ASSERT(pack_length() == to->pack_length());
DBUG_ASSERT(charset() == to->charset());
DBUG_ASSERT(sort_charset() == to->sort_charset());
return Field::do_field_eq;
}
// ALTER from FBT to another fbt type
if (to->charset() == &my_charset_bin &&
dynamic_cast<const Type_handler_general_purpose_string*>
(to->type_handler()))
{
/*
ALTER from FBT to a binary string type, e.g.:
BINARY, TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB
*/
return do_field_fbt_native_to_binary;
}
return do_field_string;
}
static void do_field_fbt_native_to_binary(const Copy_field *copy)
{
NativeBuffer<FbtImpl::binary_length()+1> res;
copy->from_field->val_native(&res);
copy->to_field->store(res.ptr(), res.length(), &my_charset_bin);
}
bool memcpy_field_possible(const Field *from) const override
{
// INSERT INTO t1 (fbt_field) SELECT field2 FROM t2;
return type_handler() == from->type_handler();
}
enum_conv_type rpl_conv_type_from(const Conv_source &source,
const Relay_log_info *rli,
const Conv_param ¶m) const override
{
if (type_handler() == source.type_handler() ||
(source.type_handler() == &type_handler_string &&
source.type_handler()->max_display_length_for_field(source) ==
FbtImpl::binary_length()))
return rpl_conv_type_from_same_data_type(source.metadata(), rli, param);
return CONV_TYPE_IMPOSSIBLE;
}
/*** Optimizer routines ***/
bool test_if_equality_guarantees_uniqueness(const Item *const_item) const override
{
/*
This condition:
WHERE fbt_field=const
should return a single distinct value only,
as comparison is done according to FBT.
*/
return true;
}
bool can_be_substituted_to_equal_item(const Context &ctx,
const Item_equal *item_equal)
override
{
switch (ctx.subst_constraint()) {
case ANY_SUBST:
return ctx.compare_type_handler() == item_equal->compare_type_handler();
case IDENTITY_SUBST:
return true;
}
return false;
}
Item *get_equal_const_item(THD *thd, const Context &ctx,
Item *const_item) override
{
Fbt_null tmp(const_item);
if (tmp.is_null())
return NULL;
return new (thd->mem_root) Item_literal_fbt(thd, tmp);
}
Data_type_compatibility can_optimize_keypart_ref(const Item_bool_func *cond,
const Item *item)
const override
{
/*
Mixing of two different non-traditional types is currently prevented.
This may change in the future.
*/
DBUG_ASSERT(item->type_handler()->type_handler_base_or_self()->
is_traditional_scalar_type() ||
item->type_handler()->type_collection() ==
type_handler()->type_collection());
return Data_type_compatibility::OK;
}
/**
Test if Field can use range optimizer for a standard comparison operation:
<=, <, =, <=>, >, >=
Note, this method does not cover spatial operations.
*/
Data_type_compatibility can_optimize_range(const Item_bool_func *cond,
const Item *item,
bool is_eq_func) const override
{
// See the DBUG_ASSERT comment in can_optimize_keypart_ref()
DBUG_ASSERT(item->type_handler()->type_handler_base_or_self()->
is_traditional_scalar_type() ||
item->type_handler()->type_collection() ==
type_handler()->type_collection());
return Data_type_compatibility::OK;
}
void hash_not_null(Hasher *hasher) override
{
FbtImpl::hash_record(ptr, hasher);
}
SEL_ARG *get_mm_leaf(RANGE_OPT_PARAM *prm, KEY_PART *key_part,
const Item_bool_func *cond,
scalar_comparison_op op, Item *value) override
{
DBUG_ENTER("Field_fbt::get_mm_leaf");
if (can_optimize_scalar_range(prm, key_part, cond, op, value) !=
Data_type_compatibility::OK)
DBUG_RETURN(0);
int err= value->save_in_field_no_warnings(this, 1);
if ((op != SCALAR_CMP_EQUAL && is_real_null()) || err < 0)
DBUG_RETURN(&null_element);
if (err > 0)
{
if (op == SCALAR_CMP_EQ || op == SCALAR_CMP_EQUAL)
DBUG_RETURN(new (prm->mem_root) SEL_ARG_IMPOSSIBLE(this));
DBUG_RETURN(NULL); /* Cannot infer anything */
}
DBUG_RETURN(stored_field_make_mm_leaf(prm, key_part, op, value));
}
Data_type_compatibility can_optimize_hash_join(const Item_bool_func *cond,
const Item *item)
const override
{
return can_optimize_keypart_ref(cond, item);
}
Data_type_compatibility can_optimize_group_min_max(
const Item_bool_func *cond,
const Item *const_item) const override
{
return Data_type_compatibility::OK;
}
uint row_pack_length() const override { return pack_length(); }
Binlog_type_info binlog_type_info() const override
{
DBUG_ASSERT(type() == binlog_type());
return Binlog_type_info_fixed_string(Field_fbt::binlog_type(),
FbtImpl::binary_length(), &my_charset_bin);
}
uchar *pack(uchar *to, const uchar *from, uint max_length) override
{
DBUG_PRINT("debug", ("Packing field '%s'", field_name.str));
return FbtImpl::pack(to, from, max_length);
}
const uchar *unpack(uchar *to, const uchar *from, const uchar *from_end,
uint param_data) override
{
return FbtImpl::unpack(to, from, from_end, param_data);
}
uint max_packed_col_length(uint max_length) override
{
return StringPack::max_packed_col_length(max_length);
}
uint packed_col_length(const uchar *fbt_ptr, uint length) override
{
return StringPack::packed_col_length(fbt_ptr, length);
}
uint size_of() const override { return sizeof(*this); }
};
class cmp_item_fbt: public cmp_item_scalar
{
Fbt m_native;
public:
cmp_item_fbt()
:cmp_item_scalar(),
m_native(Fbt::zero())
{ }
void store_value(Item *item) override
{
m_native= Fbt(item, &m_null_value);
}
int cmp_not_null(const Value *val) override
{
DBUG_ASSERT(!val->is_null());
DBUG_ASSERT(val->is_string());
Fbt_null tmp(val->m_string);
DBUG_ASSERT(!tmp.is_null());
return m_native.cmp(tmp);
}
int cmp(Item *arg) override
{
Fbt_null tmp(arg);
return m_null_value || tmp.is_null() ? UNKNOWN : m_native.cmp(tmp) != 0;
}
int compare(const cmp_item *ci) const override
{
const cmp_item_fbt *tmp= static_cast<const cmp_item_fbt*>(ci);
DBUG_ASSERT(!m_null_value);
DBUG_ASSERT(!tmp->m_null_value);
return m_native.cmp(tmp->m_native);
}
cmp_item *make_same(THD *thd) override
{
return new (thd->mem_root) cmp_item_fbt();
}
};
class in_fbt :public in_vector
{
Fbt m_value;
static int cmp_fbt(void *cmp_arg, const void *a, const void *b)
{
return static_cast<const Fbt*>(a)->cmp(*static_cast<const Fbt*>(b));
}
public:
in_fbt(THD *thd, uint elements)
:in_vector(thd, elements, sizeof(Fbt), cmp_fbt, 0),
m_value(Fbt::zero())
{ }
const Type_handler *type_handler() const override
{
return singleton();
}
bool set(uint pos, Item *item) override
{
Fbt *buff= &((Fbt *) base)[pos];
Fbt_null value(item);
if (value.is_null())
{
*buff= Fbt::zero();
return true;
}
*buff= value;
return false;
}
uchar *get_value(Item *item) override
{
Fbt_null value(item);
if (value.is_null())
return 0;
m_value= value;
return (uchar *) &m_value;
}
Item* create_item(THD *thd) override
{
return new (thd->mem_root) Item_literal_fbt(thd);
}
void value_to_item(uint pos, Item *item) override
{
const Fbt &buff= (((Fbt*) base)[pos]);
static_cast<Item_literal_fbt*>(item)->set_value(buff);
}
};
class Item_copy_fbt: public Item_copy
{
NativeBuffer<Fbt::binary_length()+1> m_value;
public:
Item_copy_fbt(THD *thd, Item *item_arg): Item_copy(thd, item_arg) {}
bool val_native(THD *thd, Native *to) override
{
if (null_value)
return true;
return to->copy(m_value.ptr(), m_value.length());
}
String *val_str(String *to) override
{
if (null_value)
return NULL;
Fbt_null tmp(m_value.ptr(), m_value.length());
return tmp.is_null() || tmp.to_string(to) ? NULL : to;
}
my_decimal *val_decimal(my_decimal *to) override
{
my_decimal_set_zero(to);
return to;
}
double val_real() override
{
return 0;
}
longlong val_int() override
{
return 0;
}
bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
{
set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
return null_value;
}
void copy() override
{
null_value= item->val_native(current_thd, &m_value);
DBUG_ASSERT(null_value == item->null_value);
}
int save_in_field(Field *field, bool no_conversions) override
{
return Item::save_in_field(field, no_conversions);
}
Item *do_get_copy(THD *thd) const override
{ return get_item_copy<Item_copy_fbt>(thd, this); }
Item *do_build_clone(THD *thd) const override { return get_copy(thd); }
};
class Item_char_typecast_func_handler_fbt_to_binary:
public Item_handled_func::Handler_str
{
public:
const Type_handler *return_type_handler(const Item_handled_func *item)
const override
{
if (item->max_length > MAX_FIELD_VARCHARLENGTH)
return Type_handler::blob_type_handler(item->max_length);
if (item->max_length > 255)
return &type_handler_varchar;
return &type_handler_string;
}
bool fix_length_and_dec(Item_handled_func *xitem) const override
{
return false;
}
String *val_str(Item_handled_func *item, String *to) const override
{
DBUG_ASSERT(dynamic_cast<const Item_char_typecast*>(item));
return static_cast<Item_char_typecast*>(item)->
val_str_binary_from_native(to);
}
};
class Item_fbt_func: public Item_func
{
public:
using Item_func::Item_func;
const Type_handler *type_handler() const override
{ return singleton(); }
bool fix_length_and_dec(THD *thd) override
{
Type_std_attributes::operator=(Type_std_attributes_fbt());
return false;
}
String *val_str(String *to) override
{
Fbt_null tmp(args[0]);
return (null_value= tmp.is_null() || tmp.to_string(to)) ? NULL : to;
}
longlong val_int() override
{
return 0;
}
double val_real() override
{
return 0;
}
my_decimal *val_decimal(my_decimal *to) override
{
my_decimal_set_zero(to);
return to;
}
bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
{
set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
return false;
}
};
class Item_typecast_fbt: public Item_fbt_func
{
public:
Item_typecast_fbt(THD *thd, Item *a) :Item_fbt_func(thd, a) {}
Item_func::Functype functype() const override
{ return Item_func::CHAR_TYPECAST_FUNC; }
bool eq(const Item *item, bool binary_cmp) const override
{
if (this == item)
return true;
if (item->type() != Item_fbt_func::FUNC_ITEM ||
functype() != ((Item_func*)item)->functype())
return false;
if (Item_fbt_func::type_handler() != item->type_handler())
return false;
Item_typecast_fbt *cast= (Item_typecast_fbt*) item;
return Item_fbt_func::args[0]->eq(cast->args[0], binary_cmp);
}
LEX_CSTRING func_name_cstring() const override
{
static Name name= singleton()->name();
size_t len= 9+name.length()+1;
char *buf= current_thd->alloc(len);
strmov(strmov(buf, "cast_as_"), name.ptr());
return { buf, len };
}
void print(String *str, enum_query_type query_type) override
{
str->append(STRING_WITH_LEN("cast("));
Item_fbt_func::args[0]->print(str, query_type);
str->append(STRING_WITH_LEN(" as "));
str->append(singleton()->name().lex_cstring());
str->append(')');
}
bool fix_length_and_dec(THD *thd) override
{
Item_fbt_func::fix_length_and_dec(thd);
if (Fbt::fix_fields_maybe_null_on_conversion_to_fbt(
Item_fbt_func::args[0]))
Item_fbt_func::set_maybe_null();
return false;
}
bool val_native(THD *thd, Native *to) override
{
Fbt_null tmp(Item_fbt_func::args[0]);
return Item_fbt_func::null_value= tmp.is_null() || tmp.to_native(to);
}
Item *do_get_copy(THD *thd) const override
{ return get_item_copy<Item_typecast_fbt>(thd, this); }
};
class Item_cache_fbt: public Item_cache
{
NativeBuffer<FbtImpl::binary_length()+1> m_value;
public: