forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.h
4719 lines (4252 loc) · 160 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, 2017, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "my_global.h"
#include "my_base.h" // ha_storage_media
#include "my_compare.h" // portable_sizeof_char_ptr
#include "my_time.h" // MYSQL_TIME_NOTE_TRUNCATED
#include "binary_log_funcs.h" // my_time_binary_length
#include "handler.h" // column_format_type
#include "mysqld.h" // system_charset_info
#include "mysqld_error.h" // ER_*
#include "sql_error.h" // Sql_condition
#include "sql_string.h" // String
#include "table.h" // TABLE
#include "mysql_version.h" // FRM_VER
class Create_field;
class Json_dom;
class Json_wrapper;
class Protocol;
class Relay_log_info;
class Send_field;
/*
Field class hierarchy
Field (abstract)
|
+--Field_bit
| +--Field_bit_as_char
|
+--Field_num (abstract)
| | +--Field_real (asbstract)
| | +--Field_decimal
| | +--Field_float
| | +--Field_double
| |
| +--Field_new_decimal
| +--Field_short
| +--Field_medium
| +--Field_long
| +--Field_longlong
| +--Field_tiny
| +--Field_year
|
+--Field_str (abstract)
| +--Field_longstr
| | +--Field_string
| | +--Field_varstring
| | +--Field_blob
| | +--Field_geom
| | +--Field_json
| |
| +--Field_null
| +--Field_enum
| +--Field_set
|
+--Field_temporal (abstract)
+--Field_time_common (abstract)
| +--Field_time
| +--Field_timef
|
+--Field_temporal_with_date (abstract)
+--Field_newdate
+--Field_temporal_with_date_and_time (abstract)
+--Field_timestamp
+--Field_datetime
+--Field_temporal_with_date_and_timef (abstract)
+--Field_timestampf
+--Field_datetimef
*/
enum enum_check_fields
{
CHECK_FIELD_IGNORE,
CHECK_FIELD_WARN,
CHECK_FIELD_ERROR_FOR_NULL
};
enum Derivation
{
DERIVATION_IGNORABLE= 6,
DERIVATION_NUMERIC= 5,
DERIVATION_COERCIBLE= 4,
DERIVATION_SYSCONST= 3,
DERIVATION_IMPLICIT= 2,
DERIVATION_NONE= 1,
DERIVATION_EXPLICIT= 0
};
/**
Status when storing a value in a field or converting from one
datatype to another. The values should be listed in order of
increasing seriousness so that if two type_conversion_status
variables are compared, the bigger one is most serious.
*/
enum type_conversion_status
{
/// Storage/conversion went fine.
TYPE_OK= 0,
/**
A minor problem when converting between temporal values, e.g.
if datetime is converted to date the time information is lost.
*/
TYPE_NOTE_TIME_TRUNCATED,
/**
Value outside min/max limit of datatype. The min/max value is
stored by Field::store() instead (if applicable)
*/
TYPE_WARN_OUT_OF_RANGE,
/**
Value was stored, but something was cut. What was cut is
considered insignificant enough to only issue a note. Example:
trying to store a number with 5 decimal places into a field that
can only store 3 decimals. The number rounded to 3 decimal places
should be stored. Another example: storing the string "foo " into
a VARCHAR(3). The string "foo" is stored in this case, so only
whitespace is cut.
*/
TYPE_NOTE_TRUNCATED,
/**
Value was stored, but something was cut. What was cut is
considered significant enough to issue a warning. Example: storing
the string "foo" into a VARCHAR(2). The string "fo" is stored in
this case. Another example: storing the string "2010-01-01foo"
into a DATE. The garbage in the end of the string is cut in this
case.
*/
TYPE_WARN_TRUNCATED,
/**
Value has invalid string data. When present in a predicate with
equality operator, range optimizer returns an impossible where.
*/
TYPE_WARN_INVALID_STRING,
/// Trying to store NULL in a NOT NULL field.
TYPE_ERR_NULL_CONSTRAINT_VIOLATION,
/**
Store/convert incompatible values, like converting "foo" to a
date.
*/
TYPE_ERR_BAD_VALUE,
/// Out of memory
TYPE_ERR_OOM
};
/*
Some defines for exit codes for ::is_equal class functions.
*/
#define IS_EQUAL_NO 0
#define IS_EQUAL_YES 1
#define IS_EQUAL_PACK_LENGTH 2
#define STORAGE_TYPE_MASK 7
#define COLUMN_FORMAT_MASK 7
#define COLUMN_FORMAT_SHIFT 3
#define my_charset_numeric my_charset_latin1
#define MY_REPERTOIRE_NUMERIC MY_REPERTOIRE_ASCII
#define FRM_VER_TRUE_VARCHAR (FRM_VER+4) /* 10 */
struct st_cache_field;
type_conversion_status field_conv(Field *to,Field *from);
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;
}
inline type_conversion_status
decimal_err_to_type_conv_status(int dec_error)
{
if (dec_error & E_DEC_OOM)
return TYPE_ERR_OOM;
if (dec_error & (E_DEC_DIV_ZERO | E_DEC_BAD_NUM))
return TYPE_ERR_BAD_VALUE;
if (dec_error & E_DEC_TRUNCATED)
return TYPE_NOTE_TRUNCATED;
if (dec_error & E_DEC_OVERFLOW)
return TYPE_WARN_OUT_OF_RANGE;
if (dec_error == E_DEC_OK)
return TYPE_OK;
// impossible
DBUG_ASSERT(false);
return TYPE_ERR_BAD_VALUE;
}
/**
Convert warnings returned from str_to_time() and str_to_datetime()
to their corresponding type_conversion_status codes.
*/
inline type_conversion_status
time_warning_to_type_conversion_status(const int warn)
{
if (warn & MYSQL_TIME_NOTE_TRUNCATED)
return TYPE_NOTE_TIME_TRUNCATED;
if (warn & MYSQL_TIME_WARN_OUT_OF_RANGE)
return TYPE_WARN_OUT_OF_RANGE;
if (warn & MYSQL_TIME_WARN_TRUNCATED)
return TYPE_NOTE_TRUNCATED;
if (warn & (MYSQL_TIME_WARN_ZERO_DATE | MYSQL_TIME_WARN_ZERO_IN_DATE))
return TYPE_ERR_BAD_VALUE;
if (warn & MYSQL_TIME_WARN_INVALID_TIMESTAMP)
// date was fine but pointed to daylight saving time switch gap
return TYPE_OK;
DBUG_ASSERT(!warn);
return TYPE_OK;
}
#define ASSERT_COLUMN_MARKED_FOR_READ \
DBUG_ASSERT(!table || (!table->read_set || \
bitmap_is_set(table->read_set, field_index)))
#define ASSERT_COLUMN_MARKED_FOR_WRITE \
DBUG_ASSERT(!table || (!table->write_set || \
bitmap_is_set(table->write_set, field_index)))
/**
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)
{
switch (type)
{
case MYSQL_TYPE_TIME:
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_NEWDATE:
return true;
default:
return false;
}
}
/**
Tests if field real type is temporal, i.e. represents
all existing implementations of
DATE, TIME, DATETIME or TIMESTAMP types in SQL.
@param type Field real type, as returned by field->real_type()
@retval true If field real type is temporal
@retval false If field real type is not temporal
*/
inline bool is_temporal_real_type(enum_field_types type)
{
switch (type)
{
case MYSQL_TYPE_TIME2:
case MYSQL_TYPE_TIMESTAMP2:
case MYSQL_TYPE_DATETIME2:
return true;
default:
return is_temporal_type(type);
}
}
/**
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;
}
}
/**
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;
default:
return false;
}
}
/**
Tests if field type is temporal and has date and time parts,
i.e. represents 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 and time parts.
@retval false If field type is not temporal type with date and time parts.
*/
inline bool is_temporal_type_with_date_and_time(enum_field_types type)
{
switch (type)
{
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
return true;
default:
return false;
}
}
/**
Tests if field real type can have "DEFAULT CURRENT_TIMESTAMP",
i.e. represents TIMESTAMP types in SQL.
@param type Field type, as returned by field->real_type().
@retval true If field real type can have "DEFAULT CURRENT_TIMESTAMP".
@retval false If field real type can not have "DEFAULT CURRENT_TIMESTAMP".
*/
inline bool real_type_with_now_as_default(enum_field_types type)
{
return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2 ||
type == MYSQL_TYPE_DATETIME || type == MYSQL_TYPE_DATETIME2;
}
/**
Tests if field real type can have "ON UPDATE CURRENT_TIMESTAMP",
i.e. represents TIMESTAMP types in SQL.
@param type Field type, as returned by field->real_type().
@retval true If field real type can have "ON UPDATE CURRENT_TIMESTAMP".
@retval false If field real type can not have "ON UPDATE CURRENT_TIMESTAMP".
*/
inline bool real_type_with_now_on_update(enum_field_types type)
{
return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2 ||
type == MYSQL_TYPE_DATETIME || type == MYSQL_TYPE_DATETIME2;
}
/**
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;
}
}
/**
Copies an integer value to a format comparable with memcmp(). The
format is characterized by the following:
- The sign bit goes first and is unset for negative values.
- The representation is big endian.
The function template can be instantiated to copy from little or
big endian values.
@tparam Is_big_endian True if the source integer is big endian.
@param to Where to write the integer.
@param to_length Size in bytes of the destination buffer.
@param from Where to read the integer.
@param from_length Size in bytes of the source integer
@param is_unsigned True if the source integer is an unsigned value.
*/
template<bool Is_big_endian>
void copy_integer(uchar *to, size_t to_length,
const uchar* from, size_t from_length,
bool is_unsigned)
{
if (Is_big_endian)
{
if (is_unsigned)
to[0]= from[0];
else
to[0]= (char)(from[0] ^ 128); // Reverse the sign bit.
memcpy(to + 1, from + 1, to_length - 1);
}
else
{
const int sign_byte= from[from_length - 1];
if (is_unsigned)
to[0]= sign_byte;
else
to[0]= static_cast<char>(sign_byte ^ 128); // Reverse the sign bit.
for (size_t i= 1, j= from_length - 2; i < to_length; ++i, --j)
to[i]= from[j];
}
}
/**
This class is used for recording the information of
generated column. It will be created during define a
generated column or the table is opened.
If one field contains such an object, it means the field
is a genereated one.
*/
class Generated_column: public Sql_alloc
{
public:
Item *expr_item;
LEX_STRING expr_str;
/* It's used to free the items created in parsing generated expression */
Item *item_free_list;
/// Bitmap records base columns which a generated column depends on.
MY_BITMAP base_columns_map;
Generated_column()
: expr_item(0), item_free_list(0),
field_type(MYSQL_TYPE_LONG),
stored_in_db(false), num_non_virtual_base_cols(0),
m_expr_str_mem_root(NULL), permanent_changes_completed(false)
{
expr_str.str= NULL;
expr_str.length= 0;
};
~Generated_column() {}
enum_field_types get_real_type() const
{
return field_type;
}
void set_field_type(enum_field_types fld_type)
{
field_type= fld_type;
}
bool get_field_stored() const
{
return stored_in_db;
}
void set_field_stored(bool stored)
{
stored_in_db= stored;
}
bool register_base_columns(TABLE *table);
/**
Get the number of non virtual base columns that this generated
column needs.
@return number of non virtual base columns
*/
uint non_virtual_base_columns() const { return num_non_virtual_base_cols; }
/**
Duplicates a string into expr_str.
@param root MEM_ROOT to use for allocation; if NULL, use the remembered
one; if non-NULL, remember it.
@param src source string
@param len length of 'src' in bytes
*/
void dup_expr_str(MEM_ROOT *root, const char *src, size_t len);
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*/
bool stored_in_db; /* Indication that the field is
phisically stored in the database*/
/// How many non-virtual base columns in base_columns_map
uint num_non_virtual_base_cols;
/// MEM_ROOT which provides memory storage for expr_str.str
MEM_ROOT *m_expr_str_mem_root;
public:
/**
Used to make sure permanent changes to the item tree of expr_item are
made only once.
*/
bool permanent_changes_completed;
};
class Proto_field
{
public:
virtual bool send_binary(Protocol *protocol)= 0;
virtual bool send_text(Protocol *protocol)= 0;
};
class Field: public Proto_field
{
Field(const Item &); /* Prevent use of these */
void operator=(Field &);
public:
bool has_insert_default_function() const
{
return unireg_check == TIMESTAMP_DN_FIELD ||
unireg_check == TIMESTAMP_DNUN_FIELD;
}
bool has_update_default_function() const
{
return unireg_check == TIMESTAMP_UN_FIELD ||
unireg_check == TIMESTAMP_DNUN_FIELD;
}
/* To do: inherit Sql_alloc and get these for free */
static void *operator new(size_t size) throw ()
{ return sql_alloc(size); }
static void *operator new(size_t size, MEM_ROOT *mem_root) throw () {
return alloc_root(mem_root, size);
}
static void operator delete(void *ptr, MEM_ROOT *mem_root)
{ DBUG_ASSERT(false); /* never called */ }
static void operator delete(void *ptr_arg, size_t size) throw()
{ TRASH(ptr_arg, size); }
uchar *ptr; // Position to field in record
private:
/**
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 *m_null_ptr;
/**
Flag: if the NOT-NULL field can be temporary NULL.
*/
bool m_is_tmp_nullable;
/**
This is a flag with the following semantics:
- it can be changed only when m_is_tmp_nullable is true;
- it specifies if this field in the first current record
(TABLE::record[0]) was set to NULL (temporary NULL).
This flag is used for trigger handling.
*/
bool m_is_tmp_null;
/**
The value of THD::count_cuted_fields at the moment of setting
m_is_tmp_null attribute.
*/
enum_check_fields m_count_cuted_fields_saved;
protected:
const uchar *get_null_ptr() const
{ return m_null_ptr; }
uchar *get_null_ptr()
{ return m_null_ptr; }
public:
/*
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 **table_name, *field_name;
LEX_STRING comment;
/* Field is part of the following keys */
key_map key_start; /* Keys that starts with this field */
/// Indexes which contain this field entirely (not only a prefix)
key_map part_of_key;
key_map part_of_sortkey; /* ^ but only keys usable for sorting */
/**
All keys that include this field, but not extended by the storage engine to
include primary key columns.
*/
key_map part_of_key_not_extended;
/*
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,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
BIT_FIELD, TIMESTAMP_OLD_FIELD, CAPITALIZE, BLOB_FIELD,
TIMESTAMP_DN_FIELD, TIMESTAMP_UN_FIELD, TIMESTAMP_DNUN_FIELD,
GENERATED_FIELD= 128 };
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 if this field belongs to some index (unlike part_of_key, the index
might have only a prefix).
*/
bool m_indexed;
private:
enum enum_pushed_warnings
{
BAD_NULL_ERROR_PUSHED= 1,
NO_DEFAULT_FOR_FIELD_PUSHED= 2,
NO_DEFAULT_FOR_VIEW_FIELD_PUSHED= 4
};
/*
Bitmask specifying which warnings have been already pushed in order
not to repeat the same warning for the collmn multiple times.
Uses values of enum_pushed_warnings to control pushed warnings.
*/
unsigned int m_warnings_pushed;
public:
/* Generated column data */
Generated_column *gcol_info;
/*
Indication that the field is phycically stored in tables
rather than just generated on SQL queries.
As of now, FALSE can only be set for virtual generated columns.
*/
bool stored_in_db;
bool is_gcol() const { return gcol_info; }
bool is_virtual_gcol() const { return gcol_info && !stored_in_db; }
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()
{ }
void reset_warnings()
{ m_warnings_pushed= 0; }
/**
Turn on temporary nullability for the field.
*/
void set_tmp_nullable()
{
m_is_tmp_nullable= true;
}
/**
Turn off temporary nullability for the field.
*/
void reset_tmp_nullable()
{
m_is_tmp_nullable= false;
}
/**
Reset temporary NULL value for field
*/
void reset_tmp_null()
{
m_is_tmp_null= false;
}
void set_tmp_null();
/**
@return temporary NULL-ability flag.
@retval true if NULL can be assigned temporary to the Field.
@retval false if NULL can not be assigned even temporary to the Field.
*/
bool is_tmp_nullable() const
{ return m_is_tmp_nullable; }
/**
@return whether Field has temporary value NULL.
@retval true if the Field has temporary value NULL.
@retval false if the Field's value is NOT NULL, or if the temporary
NULL-ability flag is reset.
*/
bool is_tmp_null() const
{ return is_tmp_nullable() && m_is_tmp_null; }
/* Store functions returns 1 on overflow and -1 on fatal error */
virtual type_conversion_status store(const char *to, size_t length,
const CHARSET_INFO *cs)=0;
virtual type_conversion_status store(double nr)=0;
virtual type_conversion_status store(longlong nr, bool unsigned_val)=0;
/**
Store a temporal value in packed longlong format into a field.
The packed value is compatible with TIME_to_longlong_time_packed(),
TIME_to_longlong_date_packed() or TIME_to_longlong_datetime_packed().
Note, the value must be properly rounded or truncated according
according to field->decimals().
@param nr temporal value in packed longlong format.
@retval false on success
@retval true on error
*/
virtual type_conversion_status store_packed(longlong nr)
{
return store(nr, 0);
}
virtual type_conversion_status store_decimal(const my_decimal *d)=0;
/**
Store MYSQL_TIME value with the given amount of decimal digits
into a field.
Note, the "dec" parameter represents number of digits of the Item
that previously created the MYSQL_TIME value. It's needed when we
store the value into a CHAR/VARCHAR/TEXT field to display
the proper amount of fractional digits.
For other field types the "dec" value does not matter and is ignored.
@param ltime Time, date or datetime value.
@param dec Number of decimals in ltime.
@retval false on success
@retval true on error
*/
virtual type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec);
/**
Store MYSQL_TYPE value into a field when the number of fractional
digits is not important or is not know.
@param ltime Time, date or datetime value.
@retval false on success
@retval true on error
*/
type_conversion_status store_time(MYSQL_TIME *ltime)
{
return store_time(ltime, 0);
}
type_conversion_status store(const char *to, size_t length,
const CHARSET_INFO *cs,
enum_check_fields check_level);
virtual double val_real(void)=0;
virtual longlong val_int(void)=0;
/**
Returns TIME value in packed longlong format.
This method should not be called for non-temporal types.
Temporal field types override the default method.
*/
virtual longlong val_time_temporal()
{
DBUG_ASSERT(0);
return 0;
}
/**
Returns DATE/DATETIME value in packed longlong format.
This method should not be called for non-temporal types.
Temporal field types override the default method.
*/
virtual longlong val_date_temporal()
{
DBUG_ASSERT(0);
return 0;
}
/**
Returns "native" packed longlong representation of
a TIME or DATE/DATETIME field depending on field type.
*/
longlong val_temporal_by_field_type()
{
// Return longlong TIME or DATETIME representation, depending on field type
if (type() == MYSQL_TYPE_TIME)
return val_time_temporal();
DBUG_ASSERT(is_temporal_with_date());
return val_date_temporal();
}
virtual my_decimal *val_decimal(my_decimal *)= 0;
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, my_bool unsigned_flag);
/*
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;
/**
Returns Item_result type of a field when it appears
in numeric context such as:
SELECT time_column + 1;
SELECT SUM(time_column);
Examples:
- a column of type TIME, DATETIME, TIMESTAMP act as INT.
- a column of type TIME(1), DATETIME(1), TIMESTAMP(1)
act as DECIMAL with 1 fractional digits.
*/
virtual Item_result numeric_context_result_type() const
{
return result_type();
}
virtual Item_result cmp_type () const { return result_type(); }
virtual Item_result cast_to_int_type () const { return result_type(); }
static bool type_can_have_key_part(enum_field_types);
static enum_field_types field_type_merge(enum_field_types, enum_field_types);
static Item_result result_merge_type(enum_field_types);
bool gcol_expr_is_equal(const Field *field) const;
bool gcol_expr_is_equal(const Create_field *field) const;
virtual bool eq(Field *field)
{
return (ptr == field->ptr && m_null_ptr == field->m_null_ptr &&
null_bit == field->null_bit && field->type() == type());
}
virtual bool eq_def(Field *field);
/*
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.
Useful only for variable length datatypes where it's overloaded.
By default assume the length is constant.
*/
virtual uint32 data_length(uint row_offset= 0) { return pack_length(); }
virtual uint32 sort_length() const { return pack_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 type_conversion_status reset(void)
{
memset(ptr, 0, pack_length());
return TYPE_OK;
}
virtual void reset_fields() {}
/**
Returns timestamp value in "struct timeval" format.
This method is used in "SELECT UNIX_TIMESTAMP(field)"
to avoid conversion from timestamp to MYSQL_TIME and back.
*/
virtual bool get_timestamp(struct timeval *tm, int *warnings);
/**
Stores a timestamp value in timeval format in a field.
@note
- store_timestamp(), get_timestamp() and store_time() do not depend on
timezone and always work "in UTC".
- The default implementation of this interface expects that storing the
value will not fail. For most Field descendent classes, this is not the
case. However, this interface is only used when the function
CURRENT_TIMESTAMP is used as a column default expression, and currently we
only allow TIMESTAMP and DATETIME columns to be declared with this as the
column default. Hence it is enough that the classes implementing columns
with these types either override this interface, or that
store_time(MYSQL_TIME*, uint8) does not fail.
- The column types above interpret decimals() to mean the scale of the
fractional seconds.
- We also have the limitation that the scale of a column must be the same as
the scale of the CURRENT_TIMESTAMP. I.e. we only allow
@code
[ TIMESTAMP | DATETIME ] (n) [ DEFAULT | ON UPDATE ] CURRENT_TIMESTAMP (n)
@endcode
Since this interface relies on the caller to truncate the value according to this
Field's scale, it will work with all constructs that we currently allow.
*/
virtual void store_timestamp(const timeval *tm) { DBUG_ASSERT(false); }
/**