-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
field.h
4204 lines (3888 loc) · 148 KB
/
field.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 FIELD_INCLUDED
#define FIELD_INCLUDED
/* Copyright (c) 2000, 2015, Oracle and/or its affiliates.
Copyright (c) 2008, 2017, 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-1301 USA */
/*
Because of the function make_new_field() all field classes that have static
variables must declare the size_of() member function.
*/
#ifdef USE_PRAGMA_INTERFACE
#pragma interface /* gcc class implementation */
#endif
#include "mysqld.h" /* system_charset_info */
#include "table.h" /* TABLE */
#include "sql_string.h" /* String */
#include "my_decimal.h" /* my_decimal */
#include "sql_error.h" /* Sql_condition */
#include "compat56.h"
#include "sql_type.h" /* Type_std_attributes */
class Send_field;
class Copy_field;
class Protocol;
class Create_field;
class Relay_log_info;
class Field;
class Column_statistics;
class Column_statistics_collected;
class Item_func;
class Item_bool_func;
class Item_equal;
enum enum_check_fields
{
CHECK_FIELD_IGNORE,
CHECK_FIELD_WARN,
CHECK_FIELD_ERROR_FOR_NULL
};
/*
Common declarations for Field and Item
*/
class Value_source
{
protected:
// Parameters for warning and note generation
class Warn_filter
{
bool m_want_warning_edom;
bool m_want_note_truncated_spaces;
public:
Warn_filter(bool want_warning_edom, bool want_note_truncated_spaces) :
m_want_warning_edom(want_warning_edom),
m_want_note_truncated_spaces(want_note_truncated_spaces)
{ }
Warn_filter(const THD *thd);
bool want_warning_edom() const
{ return m_want_warning_edom; }
bool want_note_truncated_spaces() const
{ return m_want_note_truncated_spaces; }
};
class Warn_filter_all: public Warn_filter
{
public:
Warn_filter_all() :Warn_filter(true, true) { }
};
class Converter_double_to_longlong
{
protected:
bool m_error;
longlong m_result;
public:
Converter_double_to_longlong(double nr, bool unsigned_flag);
longlong result() const { return m_result; }
bool error() const { return m_error; }
void push_warning(THD *thd, double nr, bool unsigned_flag);
};
class Converter_double_to_longlong_with_warn:
public Converter_double_to_longlong
{
public:
Converter_double_to_longlong_with_warn(THD *thd, double nr,
bool unsigned_flag)
:Converter_double_to_longlong(nr, unsigned_flag)
{
if (m_error)
push_warning(thd, nr, unsigned_flag);
}
Converter_double_to_longlong_with_warn(double nr, bool unsigned_flag)
:Converter_double_to_longlong(nr, unsigned_flag)
{
if (m_error)
push_warning(current_thd, nr, unsigned_flag);
}
};
// String-to-number converters
class Converter_string_to_number
{
protected:
char *m_end_of_num; // Where the low-level conversion routine stopped
int m_error; // The error code returned by the low-level routine
bool m_edom; // If EDOM-alike error happened during conversion
/**
Check string-to-number conversion and produce a warning if
- could not convert any digits (EDOM-alike error)
- found garbage at the end of the string
- found extra spaces at the end (a note)
See also Field_num::check_edom_and_truncation() for a similar function.
@param thd - the thread that will be used to generate warnings.
Can be NULL (which means current_thd will be used
if a warning is really necessary).
@param type - name of the data type
(e.g. "INTEGER", "DECIMAL", "DOUBLE")
@param cs - character set of the original string
@param str - the original string
@param end - the end of the string
@param allow_notes - tells if trailing space notes should be displayed
or suppressed.
Unlike Field_num::check_edom_and_truncation(), this function does not
distinguish between EDOM and truncation and reports the same warning for
both cases. Perhaps we should eventually print different warnings,
to make the explicit CAST work closer to the implicit cast in
Field_xxx::store().
*/
void check_edom_and_truncation(THD *thd, Warn_filter filter,
const char *type,
CHARSET_INFO *cs,
const char *str,
size_t length) const;
public:
int error() const { return m_error; }
};
class Converter_strntod: public Converter_string_to_number
{
double m_result;
public:
Converter_strntod(CHARSET_INFO *cs, const char *str, size_t length)
{
m_result= my_strntod(cs, (char *) str, length, &m_end_of_num, &m_error);
// strntod() does not set an error if the input string was empty
m_edom= m_error !=0 || str == m_end_of_num;
}
double result() const { return m_result; }
};
class Converter_string_to_longlong: public Converter_string_to_number
{
protected:
longlong m_result;
public:
longlong result() const { return m_result; }
};
class Converter_strntoll: public Converter_string_to_longlong
{
public:
Converter_strntoll(CHARSET_INFO *cs, const char *str, size_t length)
{
m_result= my_strntoll(cs, str, length, 10, &m_end_of_num, &m_error);
/*
All non-zero errors means EDOM error.
strntoll() does not set an error if the input string was empty.
Check it here.
Notice the different with the same condition in Converter_strntoll10.
*/
m_edom= m_error != 0 || str == m_end_of_num;
}
};
class Converter_strtoll10: public Converter_string_to_longlong
{
public:
Converter_strtoll10(CHARSET_INFO *cs, const char *str, size_t length)
{
m_end_of_num= (char *) str + length;
m_result= (*(cs->cset->strtoll10))(cs, str, &m_end_of_num, &m_error);
/*
Negative error means "good negative number".
Only a positive m_error value means a real error.
strtoll10() sets error to MY_ERRNO_EDOM in case of an empty string,
so we don't have to additionally catch empty strings here.
*/
m_edom= m_error > 0;
}
};
class Converter_str2my_decimal: public Converter_string_to_number
{
public:
Converter_str2my_decimal(uint mask,
CHARSET_INFO *cs, const char *str, size_t length,
my_decimal *buf)
{
m_error= str2my_decimal(mask, str, length, cs,
buf, (const char **) &m_end_of_num);
// E_DEC_TRUNCATED means a very minor truncation: '1e-100' -> 0
m_edom= m_error && m_error != E_DEC_TRUNCATED;
}
};
// String-to-number converters with automatic warning generation
class Converter_strntod_with_warn: public Converter_strntod
{
public:
Converter_strntod_with_warn(THD *thd, Warn_filter filter,
CHARSET_INFO *cs,
const char *str, size_t length)
:Converter_strntod(cs, str, length)
{
check_edom_and_truncation(thd, filter, "DOUBLE", cs, str, length);
}
};
class Converter_strntoll_with_warn: public Converter_strntoll
{
public:
Converter_strntoll_with_warn(THD *thd, Warn_filter filter,
CHARSET_INFO *cs,
const char *str, size_t length)
:Converter_strntoll(cs, str, length)
{
check_edom_and_truncation(thd, filter, "INTEGER", cs, str, length);
}
};
class Converter_strtoll10_with_warn: public Converter_strtoll10
{
public:
Converter_strtoll10_with_warn(THD *thd, Warn_filter filter,
CHARSET_INFO *cs,
const char *str, size_t length)
:Converter_strtoll10(cs, str, length)
{
check_edom_and_truncation(thd, filter, "INTEGER", cs, str, length);
}
};
class Converter_str2my_decimal_with_warn: public Converter_str2my_decimal
{
public:
Converter_str2my_decimal_with_warn(THD *thd, Warn_filter filter,
uint mask, CHARSET_INFO *cs,
const char *str, size_t length,
my_decimal *buf)
:Converter_str2my_decimal(mask, cs, str, length, buf)
{
check_edom_and_truncation(thd, filter, "DECIMAL", cs, str, length);
}
};
// String-to-number convertion methods for the old code compatibility
longlong longlong_from_string_with_check(CHARSET_INFO *cs, const char *cptr,
const char *end) const
{
/*
TODO: Give error if we wanted a signed integer and we got an unsigned
one
Notice, longlong_from_string_with_check() honors thd->no_error, because
it's used to handle queries like this:
SELECT COUNT(@@basedir);
and is called when Item_func_get_system_var::update_null_value()
suppresses warnings and then calls val_int().
The other methods {double|decimal}_from_string_with_check() ignore
thd->no_errors, because they are not used for update_null_value()
and they always allow all kind of warnings.
*/
THD *thd= current_thd;
return Converter_strtoll10_with_warn(thd, Warn_filter(thd),
cs, cptr, end - cptr).result();
}
double double_from_string_with_check(CHARSET_INFO *cs, const char *cptr,
const char *end) const
{
return Converter_strntod_with_warn(NULL, Warn_filter_all(),
cs, cptr, end - cptr).result();
}
my_decimal *decimal_from_string_with_check(my_decimal *decimal_value,
CHARSET_INFO *cs,
const char *cptr,
const char *end)
{
Converter_str2my_decimal_with_warn(NULL, Warn_filter_all(),
E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
cs, cptr, end - cptr, decimal_value);
return decimal_value;
}
longlong longlong_from_hex_hybrid(const char *str, uint32 length)
{
const char *end= str + length;
const char *ptr= end - MY_MIN(length, sizeof(longlong));
ulonglong value= 0;
for ( ; ptr != end ; ptr++)
value= (value << 8) + (ulonglong) (uchar) *ptr;
return (longlong) value;
}
longlong longlong_from_string_with_check(const String *str) const
{
return longlong_from_string_with_check(str->charset(),
str->ptr(), str->end());
}
double double_from_string_with_check(const String *str) const
{
return double_from_string_with_check(str->charset(),
str->ptr(), str->end());
}
my_decimal *decimal_from_string_with_check(my_decimal *decimal_value,
const String *str)
{
return decimal_from_string_with_check(decimal_value, str->charset(),
str->ptr(), str->end());
}
// End of String-to-number conversion methods
public:
/*
The enumeration Subst_constraint is currently used only in implementations
of the virtual function subst_argument_checker.
*/
enum Subst_constraint
{
ANY_SUBST, /* Any substitution for a field is allowed */
IDENTITY_SUBST /* Substitution for a field is allowed if any two
different values of the field type are not equal */
};
/*
Item context attributes.
Comparison functions pass their attributes to propagate_equal_fields().
For exmple, for string comparison, the collation of the comparison
operation is important inside propagate_equal_fields().
*/
class Context
{
/*
Which type of propagation is allowed:
- ANY_SUBST (loose equality, according to the collation), or
- IDENTITY_SUBST (strict binary equality).
*/
Subst_constraint m_subst_constraint;
/*
Comparison type.
Impostant only when ANY_SUBSTS.
*/
Item_result m_compare_type;
/*
Collation of the comparison operation.
Important only when ANY_SUBST.
*/
CHARSET_INFO *m_compare_collation;
public:
Context(Subst_constraint subst, Item_result type, CHARSET_INFO *cs)
:m_subst_constraint(subst),
m_compare_type(type),
m_compare_collation(cs) { }
Subst_constraint subst_constraint() const { return m_subst_constraint; }
Item_result compare_type() const
{
DBUG_ASSERT(m_subst_constraint == ANY_SUBST);
return m_compare_type;
}
CHARSET_INFO *compare_collation() const
{
DBUG_ASSERT(m_subst_constraint == ANY_SUBST);
return m_compare_collation;
}
};
class Context_identity: public Context
{ // Use this to request only exact value, no invariants.
public:
Context_identity()
:Context(IDENTITY_SUBST, STRING_RESULT, &my_charset_bin) { }
};
class Context_boolean: public Context
{ // Use this when an item is [a part of] a boolean expression
public:
Context_boolean() :Context(ANY_SUBST, INT_RESULT, &my_charset_bin) { }
};
};
#define STORAGE_TYPE_MASK 7
#define COLUMN_FORMAT_MASK 7
#define COLUMN_FORMAT_SHIFT 3
/* The length of the header part for each virtual column in the .frm file */
#define FRM_VCOL_OLD_HEADER_SIZE(b) (3 + MY_TEST(b))
#define FRM_VCOL_NEW_BASE_SIZE 16
#define FRM_VCOL_NEW_HEADER_SIZE 6
class Count_distinct_field;
struct ha_field_option_struct;
struct st_cache_field;
int field_conv(Field *to,Field *from);
int truncate_double(double *nr, uint field_length, uint dec,
bool unsigned_flag, double max_value);
inline uint get_enum_pack_length(int elements)
{
return elements < 256 ? 1 : 2;
}
inline uint get_set_pack_length(int elements)
{
uint len= (elements + 7) / 8;
return len > 4 ? 8 : len;
}
/**
Tests if field type is temporal and has date part,
i.e. represents DATE, DATETIME or TIMESTAMP types in SQL.
@param type Field type, as returned by field->type().
@retval true If field type is temporal type with date part.
@retval false If field type is not temporal type with date part.
*/
inline bool is_temporal_type_with_date(enum_field_types type)
{
switch (type)
{
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
return true;
case MYSQL_TYPE_DATETIME2:
case MYSQL_TYPE_TIMESTAMP2:
DBUG_ASSERT(0); // field->real_type() should not get to here.
default:
return false;
}
}
/**
Recognizer for concrete data type (called real_type for some reason),
returning true if it is one of the TIMESTAMP types.
*/
inline bool is_timestamp_type(enum_field_types type)
{
return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2;
}
/**
Convert temporal real types as retuned by field->real_type()
to field type as returned by field->type().
@param real_type Real type.
@retval Field type.
*/
inline enum_field_types real_type_to_type(enum_field_types real_type)
{
switch (real_type)
{
case MYSQL_TYPE_TIME2:
return MYSQL_TYPE_TIME;
case MYSQL_TYPE_DATETIME2:
return MYSQL_TYPE_DATETIME;
case MYSQL_TYPE_TIMESTAMP2:
return MYSQL_TYPE_TIMESTAMP;
case MYSQL_TYPE_NEWDATE:
return MYSQL_TYPE_DATE;
/* Note: NEWDECIMAL is a type, not only a real_type */
default: return real_type;
}
}
static inline enum enum_mysql_timestamp_type
mysql_type_to_time_type(enum enum_field_types mysql_type)
{
switch(mysql_type) {
case MYSQL_TYPE_TIME2:
case MYSQL_TYPE_TIME: return MYSQL_TIMESTAMP_TIME;
case MYSQL_TYPE_TIMESTAMP2:
case MYSQL_TYPE_TIMESTAMP:
case MYSQL_TYPE_DATETIME2:
case MYSQL_TYPE_DATETIME: return MYSQL_TIMESTAMP_DATETIME;
case MYSQL_TYPE_NEWDATE:
case MYSQL_TYPE_DATE: return MYSQL_TIMESTAMP_DATE;
default: return MYSQL_TIMESTAMP_ERROR;
}
}
/**
Tests if field type is temporal, i.e. represents
DATE, TIME, DATETIME or TIMESTAMP types in SQL.
@param type Field type, as returned by field->type().
@retval true If field type is temporal
@retval false If field type is not temporal
*/
inline bool is_temporal_type(enum_field_types type)
{
return mysql_type_to_time_type(type) != MYSQL_TIMESTAMP_ERROR;
}
/**
Tests if field type is temporal and has time part,
i.e. represents TIME, DATETIME or TIMESTAMP types in SQL.
@param type Field type, as returned by field->type().
@retval true If field type is temporal type with time part.
@retval false If field type is not temporal type with time part.
*/
inline bool is_temporal_type_with_time(enum_field_types type)
{
switch (type)
{
case MYSQL_TYPE_TIME:
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
return true;
default:
return false;
}
}
enum enum_vcol_info_type
{
VCOL_GENERATED_VIRTUAL, VCOL_GENERATED_STORED,
VCOL_DEFAULT, VCOL_CHECK_FIELD, VCOL_CHECK_TABLE
};
static inline const char *vcol_type_name(enum_vcol_info_type type)
{
switch (type)
{
case VCOL_GENERATED_VIRTUAL:
case VCOL_GENERATED_STORED:
return "GENERATED ALWAYS AS";
case VCOL_DEFAULT:
return "DEFAULT";
case VCOL_CHECK_FIELD:
case VCOL_CHECK_TABLE:
return "CHECK";
}
return 0;
}
/*
Flags for Virtual_column_info. If none is set, the expression must be
a constant with no side-effects, so it's calculated at CREATE TABLE time,
stored in table->record[2], and not recalculated for every statement.
*/
#define VCOL_FIELD_REF 1
#define VCOL_NON_DETERMINISTIC 2
#define VCOL_SESSION_FUNC 4 /* uses session data, e.g. USER or DAYNAME */
#define VCOL_TIME_FUNC 8
#define VCOL_IMPOSSIBLE 16
#define VCOL_NOT_STRICTLY_DETERMINISTIC \
(VCOL_NON_DETERMINISTIC | VCOL_TIME_FUNC | VCOL_SESSION_FUNC)
/*
Virtual_column_info is the class to contain additional
characteristics that is specific for a virtual/computed
field such as:
- the defining expression that is evaluated to compute the value
of the field
- whether the field is to be stored in the database
- whether the field is used in a partitioning expression
*/
class Virtual_column_info: public Sql_alloc
{
private:
/*
The following data is only updated by the parser and read
when a Create_field object is created/initialized.
*/
enum_field_types field_type; /* Real field type*/
/* Flag indicating that the field used in a partitioning expression */
bool in_partitioning_expr;
public:
/* Flag indicating that the field is physically stored in the database */
bool stored_in_db;
bool utf8; /* Already in utf8 */
Item *expr;
LEX_STRING name; /* Name of constraint */
uint flags;
Virtual_column_info()
: field_type((enum enum_field_types)MYSQL_TYPE_VIRTUAL),
in_partitioning_expr(FALSE), stored_in_db(FALSE),
utf8(TRUE), expr(NULL), flags(0)
{
name.str= NULL;
name.length= 0;
};
~Virtual_column_info() {}
enum_field_types get_real_type() const
{
return field_type;
}
void set_field_type(enum_field_types fld_type)
{
/* Calling this function can only be done once. */
field_type= fld_type;
}
bool is_stored() const
{
return stored_in_db;
}
void set_stored_in_db_flag(bool stored)
{
stored_in_db= stored;
}
bool is_in_partitioning_expr() const
{
return in_partitioning_expr;
}
void mark_as_in_partitioning_expr()
{
in_partitioning_expr= TRUE;
}
inline bool is_equal(const Virtual_column_info* vcol) const;
void print(String*);
};
class Field: public Value_source
{
Field(const Item &); /* Prevent use of these */
void operator=(Field &);
protected:
int save_in_field_str(Field *to)
{
StringBuffer<MAX_FIELD_WIDTH> result(charset());
val_str(&result);
return to->store(result.ptr(), result.length(), charset());
}
static void do_field_int(Copy_field *copy);
static void do_field_real(Copy_field *copy);
static void do_field_string(Copy_field *copy);
static void do_field_temporal(Copy_field *copy);
static void do_field_decimal(Copy_field *copy);
public:
static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
{ return alloc_root(mem_root, size); }
static void *operator new(size_t size) throw ()
{ return thd_alloc(current_thd, size); }
static void operator delete(void *ptr_arg, size_t size) { TRASH(ptr_arg, size); }
static void operator delete(void *ptr, MEM_ROOT *mem_root)
{ DBUG_ASSERT(0); }
uchar *ptr; // Position to field in record
/**
Byte where the @c NULL bit is stored inside a record. If this Field is a
@c NOT @c NULL field, this member is @c NULL.
*/
uchar *null_ptr;
/*
Note that you can use table->in_use as replacement for current_thd member
only inside of val_*() and store() members (e.g. you can't use it in cons)
*/
TABLE *table; // Pointer for table
TABLE *orig_table; // Pointer to original table
const char * const *table_name;
const char *field_name;
/** reference to the list of options or NULL */
engine_option_value *option_list;
ha_field_option_struct *option_struct; /* structure with parsed options */
LEX_STRING comment;
/* Field is part of the following keys */
key_map key_start, part_of_key, part_of_key_not_clustered;
/*
Bitmap of indexes that have records ordered by col1, ... this_field, ...
For example, INDEX (col(prefix_n)) is not present in col.part_of_sortkey.
*/
key_map part_of_sortkey;
/*
We use three additional unireg types for TIMESTAMP to overcome limitation
of current binary format of .frm file. We'd like to be able to support
NOW() as default and on update value for such fields but unable to hold
this info anywhere except unireg_check field. This issue will be resolved
in more clean way with transition to new text based .frm format.
See also comment for Field_timestamp::Field_timestamp().
*/
enum utype {
NONE=0,
NEXT_NUMBER=15, // AUTO_INCREMENT
TIMESTAMP_OLD_FIELD=18, // TIMESTAMP created before 4.1.3
TIMESTAMP_DN_FIELD=21, // TIMESTAMP DEFAULT NOW()
TIMESTAMP_UN_FIELD=22, // TIMESTAMP ON UPDATE NOW()
TIMESTAMP_DNUN_FIELD=23 // TIMESTAMP DEFAULT NOW() ON UPDATE NOW()
};
enum geometry_type
{
GEOM_GEOMETRY = 0, GEOM_POINT = 1, GEOM_LINESTRING = 2, GEOM_POLYGON = 3,
GEOM_MULTIPOINT = 4, GEOM_MULTILINESTRING = 5, GEOM_MULTIPOLYGON = 6,
GEOM_GEOMETRYCOLLECTION = 7
};
enum imagetype { itRAW, itMBR};
utype unireg_check;
uint32 field_length; // Length of field
uint32 flags;
uint16 field_index; // field number in fields array
uchar null_bit; // Bit used to test null bit
/**
If true, this field was created in create_tmp_field_from_item from a NULL
value. This means that the type of the field is just a guess, and the type
may be freely coerced to another type.
@see create_tmp_field_from_item
@see Item_type_holder::get_real_type
*/
bool is_created_from_null_item;
/* TRUE in Field objects created for column min/max values */
bool is_stat_field;
/*
Selectivity of the range condition over this field.
When calculating this selectivity a range predicate
is taken into account only if:
- it is extracted from the WHERE clause
- it depends only on the table the field belongs to
*/
double cond_selectivity;
/*
The next field in the class of equal fields at the top AND level
of the WHERE clause
*/
Field *next_equal_field;
/*
This structure is used for statistical data on the column
that has been read from the statistical table column_stat
*/
Column_statistics *read_stats;
/*
This structure is used for statistical data on the column that
is collected by the function collect_statistics_for_table
*/
Column_statistics_collected *collected_stats;
/*
This is additional data provided for any computed(virtual) field,
default function or check constraint.
In particular it includes a pointer to the item by which this field
can be computed from other fields.
*/
Virtual_column_info *vcol_info, *check_constraint, *default_value;
Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
uchar null_bit_arg, utype unireg_check_arg,
const char *field_name_arg);
virtual ~Field() {}
DTCollation dtcollation() const
{
return DTCollation(charset(), derivation(), repertoire());
}
Type_std_attributes type_std_attributes() const
{
return Type_std_attributes(field_length, decimals(),
MY_TEST(flags & UNSIGNED_FLAG),
dtcollation());
}
/**
Convenience definition of a copy function returned by
Field::get_copy_func()
*/
typedef void Copy_func(Copy_field*);
virtual Copy_func *get_copy_func(const Field *from) const= 0;
/* Store functions returns 1 on overflow and -1 on fatal error */
virtual int store_field(Field *from) { return from->save_in_field(this); }
virtual int save_in_field(Field *to)= 0;
/**
Check if it is possible just copy the value
of the field 'from' to the field 'this', e.g. for
INSERT INTO t1 (field1) SELECT field2 FROM t2;
@param from - The field to copy from
@retval true - it is possible to just copy value of 'from' to 'this'
@retval false - conversion is needed
*/
virtual bool memcpy_field_possible(const Field *from) const= 0;
virtual int store(const char *to, uint length,CHARSET_INFO *cs)=0;
virtual int store_hex_hybrid(const char *str, uint length);
virtual int store(double nr)=0;
virtual int store(longlong nr, bool unsigned_val)=0;
virtual int store_decimal(const my_decimal *d)=0;
virtual int store_time_dec(MYSQL_TIME *ltime, uint dec);
int store_time(MYSQL_TIME *ltime)
{ return store_time_dec(ltime, TIME_SECOND_PART_DIGITS); }
int store(const char *to, uint length, CHARSET_INFO *cs,
enum_check_fields check_level);
int store(const LEX_STRING *ls, CHARSET_INFO *cs)
{ return store(ls->str, ls->length, cs); }
virtual double val_real(void)=0;
virtual longlong val_int(void)=0;
virtual bool val_bool(void)= 0;
virtual my_decimal *val_decimal(my_decimal *);
inline String *val_str(String *str) { return val_str(str, str); }
/*
val_str(buf1, buf2) gets two buffers and should use them as follows:
if it needs a temp buffer to convert result to string - use buf1
example Field_tiny::val_str()
if the value exists as a string already - use buf2
example Field_string::val_str()
consequently, buf2 may be created as 'String buf;' - no memory
will be allocated for it. buf1 will be allocated to hold a
value if it's too small. Using allocated buffer for buf2 may result in
an unnecessary free (and later, may be an alloc).
This trickery is used to decrease a number of malloc calls.
*/
virtual String *val_str(String*,String *)=0;
String *val_int_as_str(String *val_buffer, bool unsigned_flag);
fast_field_copier get_fast_field_copier(const Field *from);
/*
str_needs_quotes() returns TRUE if the value returned by val_str() needs
to be quoted when used in constructing an SQL query.
*/
virtual bool str_needs_quotes() { return FALSE; }
virtual Item_result result_type () const=0;
virtual Item_result cmp_type () const { return result_type(); }
virtual const Type_handler *cast_to_int_type_handler() const
{
return Type_handler::get_handler_by_field_type(type());
}
static bool type_can_have_key_part(enum_field_types);
static enum_field_types field_type_merge(enum_field_types, enum_field_types);
virtual bool eq(Field *field)
{
return (ptr == field->ptr && null_ptr == field->null_ptr &&
null_bit == field->null_bit && field->type() == type());
}
virtual bool eq_def(const Field *field) const;
/*
pack_length() returns size (in bytes) used to store field data in memory
(i.e. it returns the maximum size of the field in a row of the table,
which is located in RAM).
*/
virtual uint32 pack_length() const { return (uint32) field_length; }
/*
pack_length_in_rec() returns size (in bytes) used to store field data on
storage (i.e. it returns the maximal size of the field in a row of the
table, which is located on disk).
*/
virtual uint32 pack_length_in_rec() const { return pack_length(); }
virtual bool compatible_field_size(uint metadata, Relay_log_info *rli,
uint16 mflags, int *order);
virtual uint pack_length_from_metadata(uint field_metadata)
{
DBUG_ENTER("Field::pack_length_from_metadata");
DBUG_RETURN(field_metadata);
}
virtual uint row_pack_length() const { return 0; }
virtual int save_field_metadata(uchar *first_byte)
{ return do_save_field_metadata(first_byte); }
/*
data_length() return the "real size" of the data in memory.
*/
virtual uint32 data_length() { return pack_length(); }
virtual uint32 sort_length() const { return pack_length(); }
/*
Get the number bytes occupied by the value in the field.
CHAR values are stripped of trailing spaces.
Flexible values are stripped of their length.
*/
virtual uint32 value_length()
{
uint len;
if (!zero_pack() &&
(type() == MYSQL_TYPE_STRING &&
(len= pack_length()) >= 4 && len < 256))
{
uchar *str, *end;
for (str= ptr, end= str+len; end > str && end[-1] == ' '; end--) {}
len=(uint) (end-str);
return len;
}
return data_length();
}
/**
Get the maximum size of the data in packed format.
@return Maximum data length of the field when packed using the
Field::pack() function.
*/
virtual uint32 max_data_length() const {
return pack_length();
};
virtual int reset(void) { bzero(ptr,pack_length()); return 0; }
virtual void reset_fields() {}
const uchar *ptr_in_record(const uchar *record) const
{
my_ptrdiff_t l_offset= (my_ptrdiff_t) (record - table->record[0]);
return ptr + l_offset;
}
virtual void set_default();
bool has_update_default_function() const
{
return flags & ON_UPDATE_NOW_FLAG;
}
bool has_default_now_unireg_check() const
{
return unireg_check == TIMESTAMP_DN_FIELD
|| unireg_check == TIMESTAMP_DNUN_FIELD;
}
/*
Mark the field as having a value supplied by the client, thus it should
not be auto-updated.
*/
void set_has_explicit_value()
{
bitmap_set_bit(&table->has_value_set, field_index);
}
bool has_explicit_value()
{
return bitmap_is_set(&table->has_value_set, field_index);
}
virtual bool set_explicit_default(Item *value);
/**
Evaluates the @c UPDATE default function, if one exists, and stores the
result in the record buffer. If no such function exists for the column,
or the function is not valid for the column's data type, invoking this
function has no effect.
*/
virtual int evaluate_update_default_function() { return 0; }
virtual bool binary() const { return 1; }
virtual bool zero_pack() const { return 1; }
virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
virtual uint32 key_length() const { return pack_length(); }
virtual enum_field_types type() const =0;
virtual enum_field_types real_type() const { return type(); }
virtual enum_field_types binlog_type() const
{
/*
Binlog stores field->type() as type code by default. For example,
it puts MYSQL_TYPE_STRING in case of CHAR, VARCHAR, SET and ENUM,
with extra data type details put into metadata.
Binlog behaviour slightly differs between various MySQL and MariaDB
versions for the temporal data types TIME, DATETIME and TIMESTAMP.
MySQL prior to 5.6 uses MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME
and MYSQL_TYPE_TIMESTAMP type codes in binlog and stores no
additional metadata.
MariaDB-5.3 implements new versions for TIME, DATATIME, TIMESTAMP
with fractional second precision, but uses the old format for the
types TIME(0), DATETIME(0), TIMESTAMP(0), and it still stores
MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME and MYSQL_TYPE_TIMESTAMP in binlog,
with no additional metadata.
So row-based replication between temporal data types of
different precision is not possible in MariaDB.
MySQL-5.6 also implements a new version of TIME, DATETIME, TIMESTAMP
which support fractional second precision 0..6, and use the new
format even for the types TIME(0), DATETIME(0), TIMESTAMP(0).
For these new data types, MySQL-5.6 stores new type codes
MYSQL_TYPE_TIME2, MYSQL_TYPE_DATETIME2, MYSQL_TYPE_TIMESTAMP2 in binlog,