1 #ifndef FIELD_INCLUDED
2 #define FIELD_INCLUDED
3
4 /* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is also distributed with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26 #include <limits.h>
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32
33 #include <algorithm>
34
35 #include "decimal.h" // E_DEC_OOM
36 #include "field_types.h" // enum_field_types
37 #include "lex_string.h"
38 #include "libbinlogevents/export/binary_log_funcs.h" // my_time_binary_length
39 #include "m_ctype.h"
40 #include "my_alloc.h"
41 #include "my_base.h" // ha_storage_media
42 #include "my_bitmap.h"
43 #include "my_compiler.h"
44 #include "my_dbug.h"
45 #include "my_inttypes.h"
46 #include "my_sys.h"
47 #include "my_time.h" // MYSQL_TIME_NOTE_TRUNCATED
48 #include "mysql/udf_registration_types.h"
49 #include "mysql_com.h"
50 #include "mysql_time.h"
51 #include "mysqld_error.h" // ER_*
52 #include "nullable.h"
53 #include "sql/dd/types/column.h"
54 #include "sql/gis/srid.h"
55 #include "sql/sql_bitmap.h"
56 #include "sql/sql_const.h"
57 #include "sql/sql_error.h" // Sql_condition
58 #include "sql/table.h"
59 #include "sql_string.h" // String
60 #include "template_utils.h"
61
62 class Create_field;
63 class Field;
64 class Field_bit;
65 class Field_bit_as_char;
66 class Field_blob;
67 class Field_datetime;
68 class Field_decimal;
69 class Field_double;
70 class Field_enum;
71 class Field_float;
72 class Field_json;
73 class Field_long;
74 class Field_longlong;
75 class Field_medium;
76 class Field_new_decimal;
77 class Field_newdate;
78 class Field_num;
79 class Field_real;
80 class Field_set;
81 class Field_short;
82 class Field_str;
83 class Field_string;
84 class Field_temporal;
85 class Field_temporal_with_date;
86 class Field_temporal_with_date_and_time;
87 class Field_temporal_with_date_and_timef;
88 class Field_time;
89 class Field_time_common;
90 class Field_timef;
91 class Field_timestamp;
92 class Field_tiny;
93 class Field_varstring;
94 class Field_year;
95 class Item;
96 class Item_field;
97 class Json_array;
98 class Json_diff_vector;
99 class Json_wrapper;
100 class KEY;
101 class Protocol;
102 class Relay_log_info;
103 class Send_field;
104 class THD;
105 class my_decimal;
106 struct TYPELIB;
107 struct timeval;
108
109 using Mysql::Nullable;
110
111 /*
112 Inside an in-memory data record, memory pointers to pieces of the
113 record (like BLOBs) are stored in their native byte order and in
114 this amount of bytes.
115 */
116 #define portable_sizeof_char_ptr 8
117
118 /*
119
120 Field class hierarchy
121
122
123 Field (abstract)
124 |
125 +--Field_bit
126 | +--Field_bit_as_char
127 |
128 +--Field_num (abstract)
129 | | +--Field_real (abstract)
130 | | +--Field_decimal
131 | | +--Field_float
132 | | +--Field_double
133 | |
134 | +--Field_new_decimal
135 | +--Field_short
136 | +--Field_medium
137 | +--Field_long
138 | +--Field_longlong
139 | +--Field_tiny
140 | +--Field_year
141 |
142 +--Field_str (abstract)
143 | +--Field_longstr
144 | | +--Field_string
145 | | +--Field_varstring
146 | | +--Field_blob
147 | | +--Field_geom
148 | | +--Field_json
149 | | +--Field_typed_array
150 | |
151 | +--Field_null
152 | +--Field_enum
153 | +--Field_set
154 |
155 +--Field_temporal (abstract)
156 +--Field_time_common (abstract)
157 | +--Field_time
158 | +--Field_timef
159 |
160 +--Field_temporal_with_date (abstract)
161 +--Field_newdate
162 +--Field_temporal_with_date_and_time (abstract)
163 +--Field_timestamp
164 +--Field_datetime
165 +--Field_temporal_with_date_and_timef (abstract)
166 +--Field_timestampf
167 +--Field_datetimef
168 */
169
170 enum enum_check_fields : int {
171 CHECK_FIELD_IGNORE = 0,
172 CHECK_FIELD_WARN,
173 CHECK_FIELD_ERROR_FOR_NULL
174 };
175
176 enum Derivation {
177 DERIVATION_IGNORABLE = 6,
178 DERIVATION_NUMERIC = 5,
179 DERIVATION_COERCIBLE = 4,
180 DERIVATION_SYSCONST = 3,
181 DERIVATION_IMPLICIT = 2,
182 DERIVATION_NONE = 1,
183 DERIVATION_EXPLICIT = 0
184 };
185
186 /* Specifies data storage format for individual columns */
187 enum column_format_type {
188 COLUMN_FORMAT_TYPE_DEFAULT = 0, /* Not specified (use engine default) */
189 COLUMN_FORMAT_TYPE_FIXED = 1, /* FIXED format */
190 COLUMN_FORMAT_TYPE_DYNAMIC = 2 /* DYNAMIC format */
191 };
192
193 /**
194 Status when storing a value in a field or converting from one
195 datatype to another. The values should be listed in order of
196 increasing seriousness so that if two type_conversion_status
197 variables are compared, the bigger one is most serious.
198 */
199 enum type_conversion_status {
200 /// Storage/conversion went fine.
201 TYPE_OK = 0,
202 /**
203 A minor problem when converting between temporal values, e.g.
204 if datetime is converted to date the time information is lost.
205 */
206 TYPE_NOTE_TIME_TRUNCATED,
207 /**
208 Value was stored, but something was cut. What was cut is
209 considered insignificant enough to only issue a note. Example:
210 trying to store a number with 5 decimal places into a field that
211 can only store 3 decimals. The number rounded to 3 decimal places
212 should be stored. Another example: storing the string "foo " into
213 a VARCHAR(3). The string "foo" is stored in this case, so only
214 whitespace is cut.
215 */
216 TYPE_NOTE_TRUNCATED,
217 /**
218 Value outside min/max limit of datatype. The min/max value is
219 stored by Field::store() instead (if applicable)
220 */
221 TYPE_WARN_OUT_OF_RANGE,
222 /**
223 Value was stored, but something was cut. What was cut is
224 considered significant enough to issue a warning. Example: storing
225 the string "foo" into a VARCHAR(2). The string "fo" is stored in
226 this case. Another example: storing the string "2010-01-01foo"
227 into a DATE. The garbage in the end of the string is cut in this
228 case.
229 */
230 TYPE_WARN_TRUNCATED,
231 /**
232 Value has invalid string data. When present in a predicate with
233 equality operator, range optimizer returns an impossible where.
234 */
235 TYPE_WARN_INVALID_STRING,
236 /// Trying to store NULL in a NOT NULL field.
237 TYPE_ERR_NULL_CONSTRAINT_VIOLATION,
238 /**
239 Store/convert incompatible values, like converting "foo" to a
240 date.
241 */
242 TYPE_ERR_BAD_VALUE,
243 /// Out of memory
244 TYPE_ERR_OOM
245 };
246
247 /*
248 Some defines for exit codes for ::is_equal class functions.
249 */
250 #define IS_EQUAL_NO 0
251 #define IS_EQUAL_YES 1
252 #define IS_EQUAL_PACK_LENGTH 2
253
254 #define my_charset_numeric my_charset_latin1
255 #define MY_REPERTOIRE_NUMERIC MY_REPERTOIRE_ASCII
256
257 type_conversion_status field_conv(Field *to, const Field *from);
258
get_enum_pack_length(int elements)259 inline uint get_enum_pack_length(int elements) {
260 return elements < 256 ? 1 : 2;
261 }
262
get_set_pack_length(int elements)263 inline uint get_set_pack_length(int elements) {
264 uint len = (elements + 7) / 8;
265 return len > 4 ? 8 : len;
266 }
267
decimal_err_to_type_conv_status(int dec_error)268 inline type_conversion_status decimal_err_to_type_conv_status(int dec_error) {
269 if (dec_error & E_DEC_OOM) return TYPE_ERR_OOM;
270
271 if (dec_error & (E_DEC_DIV_ZERO | E_DEC_BAD_NUM)) return TYPE_ERR_BAD_VALUE;
272
273 if (dec_error & E_DEC_TRUNCATED) return TYPE_NOTE_TRUNCATED;
274
275 if (dec_error & E_DEC_OVERFLOW) return TYPE_WARN_OUT_OF_RANGE;
276
277 if (dec_error == E_DEC_OK) return TYPE_OK;
278
279 // impossible
280 DBUG_ASSERT(false);
281 return TYPE_ERR_BAD_VALUE;
282 }
283
284 /**
285 Convert warnings returned from str_to_time() and str_to_datetime()
286 to their corresponding type_conversion_status codes.
287 */
time_warning_to_type_conversion_status(const int warn)288 inline type_conversion_status time_warning_to_type_conversion_status(
289 const int warn) {
290 if (warn & MYSQL_TIME_NOTE_TRUNCATED) return TYPE_NOTE_TIME_TRUNCATED;
291
292 if (warn & MYSQL_TIME_WARN_OUT_OF_RANGE) return TYPE_WARN_OUT_OF_RANGE;
293
294 if (warn & MYSQL_TIME_WARN_TRUNCATED) return TYPE_NOTE_TRUNCATED;
295
296 if (warn & (MYSQL_TIME_WARN_ZERO_DATE | MYSQL_TIME_WARN_ZERO_IN_DATE))
297 return TYPE_ERR_BAD_VALUE;
298
299 if (warn & MYSQL_TIME_WARN_INVALID_TIMESTAMP)
300 // date was fine but pointed to daylight saving time switch gap
301 return TYPE_OK;
302
303 DBUG_ASSERT(!warn);
304 return TYPE_OK;
305 }
306
307 #define ASSERT_COLUMN_MARKED_FOR_READ \
308 DBUG_ASSERT(!table || (!table->read_set || \
309 bitmap_is_set(table->read_set, field_index())))
310 #define ASSERT_COLUMN_MARKED_FOR_WRITE \
311 DBUG_ASSERT(!table || (!table->write_set || \
312 bitmap_is_set(table->write_set, field_index())))
313
314 /**
315 Tests if field type is an integer
316
317 @param type Field type, as returned by field->type()
318
319 @returns true if integer type, false otherwise
320 */
is_integer_type(enum_field_types type)321 inline bool is_integer_type(enum_field_types type) {
322 switch (type) {
323 case MYSQL_TYPE_TINY:
324 case MYSQL_TYPE_SHORT:
325 case MYSQL_TYPE_INT24:
326 case MYSQL_TYPE_LONG:
327 case MYSQL_TYPE_LONGLONG:
328 return true;
329 default:
330 return false;
331 }
332 }
333
334 /**
335 Tests if field type is a numeric type
336
337 @param type Field type, as returned by field->type()
338
339 @returns true if numeric type, false otherwise
340 */
is_numeric_type(enum_field_types type)341 inline bool is_numeric_type(enum_field_types type) {
342 switch (type) {
343 case MYSQL_TYPE_TINY:
344 case MYSQL_TYPE_SHORT:
345 case MYSQL_TYPE_INT24:
346 case MYSQL_TYPE_LONG:
347 case MYSQL_TYPE_LONGLONG:
348 case MYSQL_TYPE_FLOAT:
349 case MYSQL_TYPE_DOUBLE:
350 case MYSQL_TYPE_DECIMAL:
351 case MYSQL_TYPE_NEWDECIMAL:
352 return true;
353 default:
354 return false;
355 }
356 }
357 /**
358 Tests if field type is temporal, i.e. represents
359 DATE, TIME, DATETIME or TIMESTAMP types in SQL.
360
361 @param type Field type, as returned by field->type().
362 @retval true If field type is temporal
363 @retval false If field type is not temporal
364 */
is_temporal_type(enum_field_types type)365 inline bool is_temporal_type(enum_field_types type) {
366 switch (type) {
367 case MYSQL_TYPE_TIME:
368 case MYSQL_TYPE_DATETIME:
369 case MYSQL_TYPE_TIMESTAMP:
370 case MYSQL_TYPE_DATE:
371 case MYSQL_TYPE_NEWDATE:
372 return true;
373 default:
374 return false;
375 }
376 }
377
378 /**
379 Tests if field real type is temporal, i.e. represents
380 all existing implementations of
381 DATE, TIME, DATETIME or TIMESTAMP types in SQL.
382
383 @param type Field real type, as returned by field->real_type()
384 @retval true If field real type is temporal
385 @retval false If field real type is not temporal
386 */
is_temporal_real_type(enum_field_types type)387 inline bool is_temporal_real_type(enum_field_types type) {
388 switch (type) {
389 case MYSQL_TYPE_TIME2:
390 case MYSQL_TYPE_TIMESTAMP2:
391 case MYSQL_TYPE_DATETIME2:
392 return true;
393 default:
394 return is_temporal_type(type);
395 }
396 }
397
398 /**
399 Tests if field type is temporal and has time part,
400 i.e. represents TIME, DATETIME or TIMESTAMP types in SQL.
401
402 @param type Field type, as returned by field->type().
403 @retval true If field type is temporal type with time part.
404 @retval false If field type is not temporal type with time part.
405 */
is_temporal_type_with_time(enum_field_types type)406 inline bool is_temporal_type_with_time(enum_field_types type) {
407 switch (type) {
408 case MYSQL_TYPE_TIME:
409 case MYSQL_TYPE_DATETIME:
410 case MYSQL_TYPE_TIMESTAMP:
411 return true;
412 default:
413 return false;
414 }
415 }
416
417 /**
418 Tests if field type is temporal and has date part,
419 i.e. represents DATE, DATETIME or TIMESTAMP types in SQL.
420
421 @param type Field type, as returned by field->type().
422 @retval true If field type is temporal type with date part.
423 @retval false If field type is not temporal type with date part.
424 */
is_temporal_type_with_date(enum_field_types type)425 inline bool is_temporal_type_with_date(enum_field_types type) {
426 switch (type) {
427 case MYSQL_TYPE_DATE:
428 case MYSQL_TYPE_DATETIME:
429 case MYSQL_TYPE_TIMESTAMP:
430 return true;
431 default:
432 return false;
433 }
434 }
435
436 /**
437 Tests if field type is temporal and has date and time parts,
438 i.e. represents DATETIME or TIMESTAMP types in SQL.
439
440 @param type Field type, as returned by field->type().
441 @retval true If field type is temporal type with date and time parts.
442 @retval false If field type is not temporal type with date and time parts.
443 */
is_temporal_type_with_date_and_time(enum_field_types type)444 inline bool is_temporal_type_with_date_and_time(enum_field_types type) {
445 switch (type) {
446 case MYSQL_TYPE_DATETIME:
447 case MYSQL_TYPE_TIMESTAMP:
448 return true;
449 default:
450 return false;
451 }
452 }
453
454 /**
455 Tests if field real type can have "DEFAULT CURRENT_TIMESTAMP",
456 i.e. represents TIMESTAMP types in SQL.
457
458 @param type Field type, as returned by field->real_type().
459 @retval true If field real type can have "DEFAULT CURRENT_TIMESTAMP".
460 @retval false If field real type can not have "DEFAULT CURRENT_TIMESTAMP".
461 */
real_type_with_now_as_default(enum_field_types type)462 inline bool real_type_with_now_as_default(enum_field_types type) {
463 return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2 ||
464 type == MYSQL_TYPE_DATETIME || type == MYSQL_TYPE_DATETIME2;
465 }
466
467 /**
468 Tests if field real type can have "ON UPDATE CURRENT_TIMESTAMP",
469 i.e. represents TIMESTAMP types in SQL.
470
471 @param type Field type, as returned by field->real_type().
472 @retval true If field real type can have "ON UPDATE CURRENT_TIMESTAMP".
473 @retval false If field real type can not have "ON UPDATE CURRENT_TIMESTAMP".
474 */
real_type_with_now_on_update(enum_field_types type)475 inline bool real_type_with_now_on_update(enum_field_types type) {
476 return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2 ||
477 type == MYSQL_TYPE_DATETIME || type == MYSQL_TYPE_DATETIME2;
478 }
479
480 /**
481 Recognizer for concrete data type (called real_type for some reason),
482 returning true if it is one of the TIMESTAMP types.
483 */
is_timestamp_type(enum_field_types type)484 inline bool is_timestamp_type(enum_field_types type) {
485 return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2;
486 }
487
488 /**
489 Convert temporal real types as retuned by field->real_type()
490 to field type as returned by field->type().
491
492 @param real_type Real type.
493 @retval Field type.
494 */
real_type_to_type(enum_field_types real_type)495 inline enum_field_types real_type_to_type(enum_field_types real_type) {
496 switch (real_type) {
497 case MYSQL_TYPE_TIME2:
498 return MYSQL_TYPE_TIME;
499 case MYSQL_TYPE_DATETIME2:
500 return MYSQL_TYPE_DATETIME;
501 case MYSQL_TYPE_TIMESTAMP2:
502 return MYSQL_TYPE_TIMESTAMP;
503 case MYSQL_TYPE_NEWDATE:
504 return MYSQL_TYPE_DATE;
505 /* Note: NEWDECIMAL is a type, not only a real_type */
506 default:
507 return real_type;
508 }
509 }
510
511 /**
512 Return the appropriate MYSQL_TYPE_X_BLOB value based on the
513 pack_length.
514
515 @param pack_length pack_length for BLOB
516 @retval MYSQL_TYPE_X_BLOB corresponding to pack_length.
517 */
blob_type_from_pack_length(uint pack_length)518 inline enum_field_types blob_type_from_pack_length(uint pack_length) {
519 DBUG_TRACE;
520 switch (pack_length) {
521 case 1:
522 return MYSQL_TYPE_TINY_BLOB;
523 case 2:
524 return MYSQL_TYPE_BLOB;
525 case 3:
526 return MYSQL_TYPE_MEDIUM_BLOB;
527 case 4:
528 return MYSQL_TYPE_LONG_BLOB;
529 default:
530 DBUG_ASSERT(false);
531 return MYSQL_TYPE_LONG_BLOB;
532 }
533 }
534
535 /**
536 Copies an integer value to a format comparable with memcmp(). The
537 format is characterized by the following:
538
539 - The sign bit goes first and is unset for negative values.
540 - The representation is big endian.
541
542 The function template can be instantiated to copy from little or
543 big endian values.
544
545 @tparam Is_big_endian True if the source integer is big endian.
546
547 @param to Where to write the integer.
548 @param to_length Size in bytes of the destination buffer.
549 @param from Where to read the integer.
550 @param from_length Size in bytes of the source integer
551 @param is_unsigned True if the source integer is an unsigned value.
552 */
553 template <bool Is_big_endian>
copy_integer(uchar * to,size_t to_length,const uchar * from,size_t from_length,bool is_unsigned)554 void copy_integer(uchar *to, size_t to_length, const uchar *from,
555 size_t from_length, bool is_unsigned) {
556 if (to_length == 0) return;
557 if (Is_big_endian) {
558 std::copy(from, from + std::min(to_length, from_length), to);
559 if (!is_unsigned)
560 to[0] = static_cast<char>(to[0] ^ 128); // Reverse the sign bit.
561 } else {
562 const uchar *from_end = from + from_length;
563 const uchar *from_start = from_end - std::min(from_length, to_length);
564 std::reverse_copy(from_start, from_end, to);
565 if (!is_unsigned)
566 to[0] = static_cast<char>(to[0] ^ 128); // Reverse the sign bit.
567 }
568 }
569
570 /**
571 Enum to indicate source for which value generator is used. This is needed
572 while unpacking value generator expression and pre-validating the
573 expression for generated column, default expression or check constraint.
574 */
575 enum Value_generator_source : short {
576 VGS_GENERATED_COLUMN = 0, // Value generator for GENERATED_COLUMN.
577 VGS_DEFAULT_EXPRESSION, // Value generator for Default expression.
578 VGS_CHECK_CONSTRAINT // Value generator for check constraints.
579 };
580
581 /**
582 Used for storing information associated with generated column, default
583 values generated from expression or check constraint expression.
584 */
585 class Value_generator {
586 public:
587 /**
588 Item representing the generation expression.
589 This is non-NULL for every Field of a TABLE, if that field is a generated
590 column.
591 Contrast this with the Field of a TABLE_SHARE, which has expr_item==NULL
592 even if it's a generated column; that makes sense, as an Item tree cannot
593 be shared.
594 */
595 Item *expr_item;
596 /**
597 Text of the expression. Used in only one case:
598 - the text read from the DD is put into the Value_generator::expr_str of
599 the Field of the TABLE_SHARE; then this expr_str is used as source
600 to produce expr_item for the Field of every TABLE derived from this
601 TABLE_SHARE.
602 */
603 LEX_STRING expr_str;
604
605 /**
606 Bit field indicating the type of statement for binary logging.
607 It needs to be saved because this is determined only once when it is parsed
608 but it needs to be set on the lex for each statement that uses this
609 value generator. And since unpacking is done once on table open, it will
610 be set for the rest of the statements in refix_inner_value_generator_items.
611 */
612 uint32 m_backup_binlog_stmt_flags{0};
613
614 /// List of all items created when parsing and resolving generated expression
615 Item *item_list;
616 /// Bitmap records base columns which a generated column depends on.
617 MY_BITMAP base_columns_map;
618
Value_generator()619 Value_generator()
620 : expr_item(nullptr),
621 item_list(nullptr),
622 field_type(MYSQL_TYPE_LONG),
623 stored_in_db(false),
624 num_non_virtual_base_cols(0),
625 permanent_changes_completed(false) {
626 expr_str.str = nullptr;
627 expr_str.length = 0;
628 }
~Value_generator()629 ~Value_generator() {}
get_real_type()630 enum_field_types get_real_type() const { return field_type; }
631
set_field_type(enum_field_types fld_type)632 void set_field_type(enum_field_types fld_type) { field_type = fld_type; }
633
634 /**
635 Set the binary log flags in m_backup_binlog_stmt_flags
636 @param backup_binlog_stmt_flags the falgs to be backed up
637 */
backup_stmt_unsafe_flags(uint32 backup_binlog_stmt_flags)638 void backup_stmt_unsafe_flags(uint32 backup_binlog_stmt_flags) {
639 m_backup_binlog_stmt_flags = backup_binlog_stmt_flags;
640 }
641
642 /**
643 Get the binary log flags from m_backup_binlog_stmt_flags
644 @return the flags backed up by unpack_value_generator
645 */
get_stmt_unsafe_flags()646 uint32 get_stmt_unsafe_flags() { return m_backup_binlog_stmt_flags; }
647
get_field_stored()648 bool get_field_stored() const { return stored_in_db; }
set_field_stored(bool stored)649 void set_field_stored(bool stored) { stored_in_db = stored; }
650 bool register_base_columns(TABLE *table);
651 /**
652 Get the number of non virtual base columns that this generated
653 column needs.
654
655 @return number of non virtual base columns
656 */
non_virtual_base_columns()657 uint non_virtual_base_columns() const { return num_non_virtual_base_cols; }
658
659 /**
660 Duplicates a string into expr_str.
661
662 @param root MEM_ROOT to use for allocation
663 @param src source string
664 @param len length of 'src' in bytes
665 */
666 void dup_expr_str(MEM_ROOT *root, const char *src, size_t len);
667
668 /**
669 Writes the generation expression into a String with proper syntax.
670 @param thd THD
671 @param out output String
672 */
673 void print_expr(THD *thd, String *out);
674
675 private:
676 /*
677 The following data is only updated by the parser and read
678 when a Create_field object is created/initialized.
679 */
680 enum_field_types field_type; /* Real field type*/
681 bool stored_in_db; /* Indication that the field is
682 phisically stored in the database*/
683 /// How many non-virtual base columns in base_columns_map
684 uint num_non_virtual_base_cols;
685
686 public:
687 /**
688 Used to make sure permanent changes to the item tree of expr_item are
689 made only once.
690 */
691 bool permanent_changes_completed;
692 };
693
694 class Field {
695 public:
696 /*
697 Field(const Item &) = delete;
698 The original intention was seemingly for Field to be non-copyable,
699 but due to a typo, this was never enforced, and now there's lots of
700 code that copies Field objects around. Thus, the default copy
701 constructor needs to stay (assignment is blocked off), but it's probably
702 better not to write code that depends on it.
703 */
704 Field(const Field &) = default;
705 void operator=(Field &) = delete;
706
707 /**
708 Checks if the field is marked as having a general expression to generate
709 default values.
710
711 @retval true The field has general expression as default
712 @retval false The field doesn't have any general expression as default
713 */
has_insert_default_general_value_expression()714 bool has_insert_default_general_value_expression() const {
715 return auto_flags & GENERATED_FROM_EXPRESSION;
716 }
717
718 /**
719 Checks if the field is marked as having a datetime value expression to
720 generate default values on inserts.
721
722 @retval true The field has datetime expression as default
723 @retval false The field doesn't have a datime value expression as default
724 */
has_insert_default_datetime_value_expression()725 bool has_insert_default_datetime_value_expression() const {
726 return auto_flags & DEFAULT_NOW;
727 }
728
729 /**
730 Checks if the field is marked as having a datetime value expression to
731 generate default values on updates.
732
733 @retval true The field has datetime expression as default for on update
734 @retval false The field doesn't have a datime value expression as default
735 for on update
736 */
has_update_default_datetime_value_expression()737 bool has_update_default_datetime_value_expression() const {
738 return auto_flags & ON_UPDATE_NOW;
739 }
740
741 protected:
742 /// Holds the position to the field in record
743 uchar *ptr;
744
745 private:
746 dd::Column::enum_hidden_type m_hidden;
747
748 /**
749 Byte where the @c NULL bit is stored inside a record. If this Field is a
750 @c NOT @c NULL field, this member is @c NULL.
751 */
752 uchar *m_null_ptr;
753
754 /**
755 Flag: if the NOT-NULL field can be temporary NULL.
756 */
757 bool m_is_tmp_nullable;
758
759 /**
760 This is a flag with the following semantics:
761 - it can be changed only when m_is_tmp_nullable is true;
762 - it specifies if this field in the first current record
763 (TABLE::record[0]) was set to NULL (temporary NULL).
764
765 This flag is used for trigger handling.
766 */
767 bool m_is_tmp_null;
768
769 /**
770 The value of THD::check_for_truncated_fields at the moment of setting
771 m_is_tmp_null attribute.
772 */
773 enum_check_fields m_check_for_truncated_fields_saved;
774
775 protected:
776 /*
777 null_ptr buffer to be used for Fields that are nullable but
778 cannot store null. Typically used from create_tmp_field().
779 */
780 static uchar dummy_null_buffer;
781
782 public:
get_null_ptr()783 uchar *get_null_ptr() { return m_null_ptr; }
784
785 /*
786 Note that you can use table->in_use as replacement for current_thd member
787 only inside of val_*() and store() members (e.g. you can't use it in cons)
788 */
789 TABLE *table; // Pointer for table
790 const TABLE *orig_table; // Pointer to original table
791 const char **table_name, *field_name;
792 LEX_CSTRING comment;
793 /* Field is part of the following keys */
794 Key_map key_start; /* Keys that starts with this field */
795 Key_map part_of_key; ///< Keys that includes this field
796 ///< except of prefix keys.
797 Key_map part_of_prefixkey; ///< Prefix keys
798 Key_map part_of_sortkey; /* ^ but only keys usable for sorting */
799 /**
800 All keys that include this field, but not extended by the storage engine to
801 include primary key columns.
802 */
803 Key_map part_of_key_not_extended;
804
805 /**
806 Flags for Field::auto_flags / Create_field::auto_flags bitmaps.
807
808 @note NEXT_NUMBER and DEFAULT_NOW/ON_UPDATE_NOW/GENERATED flags should
809 never be set at the same time. Also DEFAULT_NOW and GENERATED
810 should not be set at the same time.
811
812 @warning The values of this enum are used as bit masks for uchar
813 Field::auto_flags.
814 */
815 enum enum_auto_flags {
816 NONE = 0,
817 NEXT_NUMBER = 1, ///< AUTO_INCREMENT
818 DEFAULT_NOW = 2, ///< DEFAULT CURRENT_TIMESTAMP
819 ON_UPDATE_NOW = 4, ///< ON UPDATE CURRENT_TIMESTAMP
820 GENERATED_FROM_EXPRESSION = 8 ///< DEFAULT (expression)
821 };
822
823 enum geometry_type {
824 GEOM_GEOMETRY = 0,
825 GEOM_POINT = 1,
826 GEOM_LINESTRING = 2,
827 GEOM_POLYGON = 3,
828 GEOM_MULTIPOINT = 4,
829 GEOM_MULTILINESTRING = 5,
830 GEOM_MULTIPOLYGON = 6,
831 GEOM_GEOMETRYCOLLECTION = 7
832 };
833 enum imagetype { itRAW, itMBR };
834
835 // Length of field. Never write to this member directly; instead, use
836 // set_field_length().
837 uint32 field_length;
set_field_length(uint32 length)838 virtual void set_field_length(uint32 length) { field_length = length; }
839
840 private:
841 uint32 flags{0};
842 uint16 m_field_index; // field number in fields array
843
844 public:
is_flag_set(unsigned flag)845 bool is_flag_set(unsigned flag) const { return flags & flag; }
set_flag(unsigned flag)846 void set_flag(unsigned flag) { flags |= flag; }
clear_flag(unsigned flag)847 void clear_flag(unsigned flag) { flags &= ~flag; }
848 // Avoid using this function as it makes it harder to change the internal
849 // representation.
all_flags()850 uint32 all_flags() const { return flags; }
851 uchar null_bit; // Bit used to test null bit
852 /**
853 Bitmap of flags indicating if field value is auto-generated by default
854 and/or on update, and in which way.
855
856 @sa Field::enum_auto_flags for possible options.
857
858 @sa Field::utype and Field::unireg_check in pre-8.0 versions of server
859 for historical perspective.
860 */
861 uchar auto_flags;
862 /**
863 If true, this field was created in create_tmp_field_from_item from a NULL
864 value. This means that the type of the field is just a guess, and the type
865 may be freely coerced to another type.
866
867 @see create_tmp_field_from_item
868 @see Item_type_holder::get_real_type
869
870 */
871 bool is_created_from_null_item;
872 /**
873 True if this field belongs to some index (unlike part_of_key, the index
874 might have only a prefix).
875 */
876 bool m_indexed;
877
878 LEX_CSTRING m_engine_attribute = EMPTY_CSTR;
879 LEX_CSTRING m_secondary_engine_attribute = EMPTY_CSTR;
880
881 private:
882 enum enum_pushed_warnings {
883 BAD_NULL_ERROR_PUSHED = 1,
884 NO_DEFAULT_FOR_FIELD_PUSHED = 2,
885 NO_DEFAULT_FOR_VIEW_FIELD_PUSHED = 4
886 };
887
888 /*
889 Bitmask specifying which warnings have been already pushed in order
890 not to repeat the same warning for the collmn multiple times.
891 Uses values of enum_pushed_warnings to control pushed warnings.
892 */
893 unsigned int m_warnings_pushed;
894
895 public:
896 /* Generated column data */
897 Value_generator *gcol_info{nullptr};
898 /*
899 Indication that the field is phycically stored in tables
900 rather than just generated on SQL queries.
901 As of now, false can only be set for virtual generated columns.
902 */
903 bool stored_in_db;
904 /**
905 Whether the field is signed or not. Meaningful only for numeric fields
906 and numeric arrays.
907 */
is_unsigned()908 virtual bool is_unsigned() const { return false; }
is_gcol()909 bool is_gcol() const { return gcol_info; }
is_virtual_gcol()910 bool is_virtual_gcol() const { return gcol_info && !stored_in_db; }
911
912 /// Holds the expression to be used to generate default values.
913 Value_generator *m_default_val_expr{nullptr};
914
915 /**
916 Sets the hidden type for this field.
917
918 @param hidden the new hidden type to set.
919 */
set_hidden(dd::Column::enum_hidden_type hidden)920 void set_hidden(dd::Column::enum_hidden_type hidden) { m_hidden = hidden; }
921
922 /// @returns the hidden type for this field.
hidden()923 dd::Column::enum_hidden_type hidden() const { return m_hidden; }
924
925 /**
926 @retval true if this field should be hidden away from users.
927 @retval false is this field is visible to the user.
928 */
is_hidden_from_user()929 bool is_hidden_from_user() const {
930 return hidden() != dd::Column::enum_hidden_type::HT_VISIBLE &&
931 DBUG_EVALUATE_IF("show_hidden_columns", false, true);
932 }
933
934 /**
935 @returns true if this is a hidden field that is used for implementing
936 functional indexes. Note that if we need different types of hidden
937 fields in the future (like invisible columns), this function needs
938 to be changed so it can distinguish between the different "types"
939 of hidden.
940 */
is_field_for_functional_index()941 bool is_field_for_functional_index() const {
942 return hidden() == dd::Column::enum_hidden_type::HT_HIDDEN_SQL &&
943 gcol_info != nullptr;
944 }
945
946 Field(uchar *ptr_arg, uint32 length_arg, uchar *null_ptr_arg,
947 uchar null_bit_arg, uchar auto_flags_arg, const char *field_name_arg);
948
949 virtual ~Field() = default;
950
reset_warnings()951 void reset_warnings() { m_warnings_pushed = 0; }
952
953 /**
954 Turn on temporary nullability for the field.
955 */
set_tmp_nullable()956 void set_tmp_nullable() { m_is_tmp_nullable = true; }
957
958 /**
959 Turn off temporary nullability for the field.
960 */
reset_tmp_nullable()961 void reset_tmp_nullable() { m_is_tmp_nullable = false; }
962
963 /**
964 Reset temporary NULL value for field
965 */
reset_tmp_null()966 void reset_tmp_null() { m_is_tmp_null = false; }
967
968 void set_tmp_null();
969
970 /**
971 @return temporary NULL-ability flag.
972 @retval true if NULL can be assigned temporary to the Field.
973 @retval false if NULL can not be assigned even temporary to the Field.
974 */
is_tmp_nullable()975 bool is_tmp_nullable() const { return m_is_tmp_nullable; }
976
977 /**
978 @return whether Field has temporary value NULL.
979 @retval true if the Field has temporary value NULL.
980 @retval false if the Field's value is NOT NULL, or if the temporary
981 NULL-ability flag is reset.
982 */
is_tmp_null()983 bool is_tmp_null() const { return is_tmp_nullable() && m_is_tmp_null; }
984
985 /* Store functions returns 1 on overflow and -1 on fatal error */
986 virtual type_conversion_status store(const char *to, size_t length,
987 const CHARSET_INFO *cs) = 0;
988 virtual type_conversion_status store(double nr) = 0;
989 virtual type_conversion_status store(longlong nr, bool unsigned_val) = 0;
990 /**
991 Store a temporal value in packed longlong format into a field.
992 The packed value is compatible with TIME_to_longlong_time_packed(),
993 TIME_to_longlong_date_packed() or TIME_to_longlong_datetime_packed().
994 Note, the value must be properly rounded or truncated according
995 according to field->decimals().
996
997 @param nr temporal value in packed longlong format.
998 @retval false on success
999 @retval true on error
1000 */
store_packed(longlong nr)1001 virtual type_conversion_status store_packed(longlong nr) {
1002 return store(nr, false);
1003 }
1004 virtual type_conversion_status store_decimal(const my_decimal *d) = 0;
1005 /**
1006 Store MYSQL_TIME value with the given amount of decimal digits
1007 into a field.
1008
1009 Note, the "dec" parameter represents number of digits of the Item
1010 that previously created the MYSQL_TIME value. It's needed when we
1011 store the value into a CHAR/VARCHAR/TEXT field to display
1012 the proper amount of fractional digits.
1013 For other field types the "dec" value does not matter and is ignored.
1014
1015 @param ltime Time, date or datetime value.
1016 @param dec_arg Number of decimals in ltime.
1017 @retval false on success
1018 @retval true on error
1019 */
1020 virtual type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec_arg);
1021 /**
1022 Store MYSQL_TYPE value into a field when the number of fractional
1023 digits is not important or is not know.
1024
1025 @param ltime Time, date or datetime value.
1026 @retval false on success
1027 @retval true on error
1028 */
store_time(MYSQL_TIME * ltime)1029 type_conversion_status store_time(MYSQL_TIME *ltime) {
1030 return store_time(ltime, 0);
1031 }
1032 type_conversion_status store(const char *to, size_t length,
1033 const CHARSET_INFO *cs,
1034 enum_check_fields check_level);
1035 virtual double val_real() const = 0;
1036 virtual longlong val_int() const = 0;
1037 /**
1038 Returns TIME value in packed longlong format.
1039 This method should not be called for non-temporal types.
1040 Temporal field types override the default method.
1041 */
val_time_temporal()1042 virtual longlong val_time_temporal() const {
1043 DBUG_ASSERT(0);
1044 return 0;
1045 }
1046 /**
1047 Returns DATE/DATETIME value in packed longlong format.
1048 This method should not be called for non-temporal types.
1049 Temporal field types override the default method.
1050 */
val_date_temporal()1051 virtual longlong val_date_temporal() const {
1052 DBUG_ASSERT(0);
1053 return 0;
1054 }
1055 /**
1056 Returns "native" packed longlong representation of
1057 a TIME or DATE/DATETIME field depending on field type.
1058 */
val_temporal_by_field_type()1059 longlong val_temporal_by_field_type() const {
1060 // Return longlong TIME or DATETIME representation, depending on field type
1061 const enum_field_types field_type = type();
1062 if (field_type == MYSQL_TYPE_TIME) return val_time_temporal();
1063 DBUG_ASSERT(is_temporal_type_with_date(field_type));
1064 return val_date_temporal();
1065 }
1066 virtual my_decimal *val_decimal(my_decimal *) const = 0;
val_str(String * str)1067 String *val_str(String *str) const { return val_str(str, str); }
1068 /*
1069 val_str(buf1, buf2) gets two buffers and should use them as follows:
1070 if it needs a temp buffer to convert result to string - use buf1
1071 example Field_tiny::val_str()
1072 if the value exists as a string already - use buf2
1073 example Field_string::val_str()
1074 consequently, buf2 may be created as 'String buf;' - no memory
1075 will be allocated for it. buf1 will be allocated to hold a
1076 value if it's too small. Using allocated buffer for buf2 may result in
1077 an unnecessary free (and later, may be an alloc).
1078 This trickery is used to decrease a number of malloc calls.
1079 */
1080 virtual String *val_str(String *, String *) const = 0;
1081 String *val_int_as_str(String *val_buffer, bool unsigned_flag) const;
1082 /*
1083 str_needs_quotes() returns true if the value returned by val_str() needs
1084 to be quoted when used in constructing an SQL query.
1085 */
str_needs_quotes()1086 virtual bool str_needs_quotes() const { return false; }
1087 virtual Item_result result_type() const = 0;
1088 /**
1089 Returns Item_result type of a field when it appears
1090 in numeric context such as:
1091 SELECT time_column + 1;
1092 SELECT SUM(time_column);
1093 Examples:
1094 - a column of type TIME, DATETIME, TIMESTAMP act as INT.
1095 - a column of type TIME(1), DATETIME(1), TIMESTAMP(1)
1096 act as DECIMAL with 1 fractional digits.
1097 */
numeric_context_result_type()1098 virtual Item_result numeric_context_result_type() const {
1099 return result_type();
1100 }
cmp_type()1101 virtual Item_result cmp_type() const { return result_type(); }
cast_to_int_type()1102 virtual Item_result cast_to_int_type() const { return result_type(); }
1103 static bool type_can_have_key_part(enum_field_types);
1104 static enum_field_types field_type_merge(enum_field_types, enum_field_types);
1105 static Item_result result_merge_type(enum_field_types);
1106 bool gcol_expr_is_equal(const Create_field *field) const;
eq(const Field * field)1107 virtual bool eq(const Field *field) const {
1108 return (ptr == field->ptr && m_null_ptr == field->m_null_ptr &&
1109 null_bit == field->null_bit && field->type() == type());
1110 }
1111 virtual bool eq_def(const Field *field) const;
1112
1113 /*
1114 pack_length() returns size (in bytes) used to store field data in memory
1115 (i.e. it returns the maximum size of the field in a row of the table,
1116 which is located in RAM).
1117 */
pack_length()1118 virtual uint32 pack_length() const { return (uint32)field_length; }
1119
1120 /*
1121 pack_length_in_rec() returns size (in bytes) used to store field data on
1122 storage (i.e. it returns the maximal size of the field in a row of the
1123 table, which is located on disk).
1124 */
pack_length_in_rec()1125 virtual uint32 pack_length_in_rec() const { return pack_length(); }
1126 virtual bool compatible_field_size(uint metadata, Relay_log_info *, uint16,
1127 int *order) const;
pack_length_from_metadata(uint field_metadata)1128 virtual uint pack_length_from_metadata(uint field_metadata) const {
1129 DBUG_TRACE;
1130 return field_metadata;
1131 }
row_pack_length()1132 virtual uint row_pack_length() const { return 0; }
save_field_metadata(uchar * first_byte)1133 int save_field_metadata(uchar *first_byte) {
1134 return do_save_field_metadata(first_byte);
1135 }
1136
1137 /*
1138 data_length() return the "real size" of the data in memory.
1139 Useful only for variable length datatypes where it's overloaded.
1140 By default assume the length is constant.
1141 */
1142 virtual uint32 data_length(
1143 ptrdiff_t row_offset MY_ATTRIBUTE((unused)) = 0) const {
1144 return pack_length();
1145 }
1146
1147 /**
1148 Get the maximum size of the data in packed format.
1149
1150 @return Maximum data length of the field when packed using the
1151 Field::pack() function.
1152 */
max_data_length()1153 virtual uint32 max_data_length() const { return pack_length(); }
1154
reset()1155 virtual type_conversion_status reset() {
1156 memset(ptr, 0, pack_length());
1157 return TYPE_OK;
1158 }
1159 /**
1160 Returns timestamp value in "struct timeval" format.
1161 This method is used in "SELECT UNIX_TIMESTAMP(field)"
1162 to avoid conversion from timestamp to MYSQL_TIME and back.
1163 */
1164 virtual bool get_timestamp(struct timeval *tm, int *warnings) const;
1165 /**
1166 Stores a timestamp value in timeval format in a field.
1167
1168 @note
1169 - store_timestamp(), get_timestamp() and store_time() do not depend on
1170 timezone and always work "in UTC".
1171
1172 - The default implementation of this interface expects that storing the
1173 value will not fail. For most Field descendent classes, this is not the
1174 case. However, this interface is only used when the function
1175 CURRENT_TIMESTAMP is used as a column default expression, and currently we
1176 only allow TIMESTAMP and DATETIME columns to be declared with this as the
1177 column default. Hence it is enough that the classes implementing columns
1178 with these types either override this interface, or that
1179 store_time(MYSQL_TIME*, uint8) does not fail.
1180
1181 - The column types above interpret decimals() to mean the scale of the
1182 fractional seconds.
1183
1184 - We also have the limitation that the scale of a column must be the same as
1185 the scale of the CURRENT_TIMESTAMP. I.e. we only allow
1186
1187 @code
1188
1189 [ TIMESTAMP | DATETIME ] (n) [ DEFAULT | ON UPDATE ] CURRENT_TIMESTAMP (n)
1190
1191 @endcode
1192
1193 Since this interface relies on the caller to truncate the value according to
1194 this Field's scale, it will work with all constructs that we currently allow.
1195 */
store_timestamp(const timeval *)1196 virtual void store_timestamp(const timeval *) { DBUG_ASSERT(false); }
1197
1198 virtual void set_default();
1199
1200 /**
1201 Evaluates the @c INSERT default function and stores the result in the
1202 field. If no such function exists for the column, or the function is not
1203 valid for the column's data type, invoking this function has no effect.
1204 */
1205 void evaluate_insert_default_function();
1206
1207 /**
1208 Evaluates the @c UPDATE default function, if one exists, and stores the
1209 result in the record buffer. If no such function exists for the column,
1210 or the function is not valid for the column's data type, invoking this
1211 function has no effect.
1212 */
1213 void evaluate_update_default_function();
binary()1214 virtual bool binary() const { return true; }
zero_pack()1215 virtual bool zero_pack() const { return true; }
key_type()1216 virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
key_length()1217 virtual uint32 key_length() const { return pack_length(); }
1218 virtual enum_field_types type() const = 0;
real_type()1219 virtual enum_field_types real_type() const { return type(); }
binlog_type()1220 virtual enum_field_types binlog_type() const {
1221 /*
1222 Binlog stores field->type() as type code by default.
1223 This puts MYSQL_TYPE_STRING in case of CHAR, VARCHAR, SET and ENUM,
1224 with extra data type details put into metadata.
1225
1226 We cannot store field->type() in case of temporal types with
1227 fractional seconds: TIME(n), DATETIME(n) and TIMESTAMP(n),
1228 because binlog records with MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME
1229 type codes do not have metadata.
1230 So for temporal data types with fractional seconds we'll store
1231 real_type() type codes instead, i.e.
1232 MYSQL_TYPE_TIME2, MYSQL_TYPE_DATETIME2, MYSQL_TYPE_TIMESTAMP2,
1233 and put precision into metatada.
1234
1235 Note: perhaps binlog should eventually be modified to store
1236 real_type() instead of type() for all column types.
1237 */
1238 return type();
1239 }
cmp(const uchar * str)1240 int cmp(const uchar *str) const { return cmp(ptr, str); }
cmp_max(const uchar * a,const uchar * b,uint max_len MY_ATTRIBUTE ((unused)))1241 virtual int cmp_max(const uchar *a, const uchar *b,
1242 uint max_len MY_ATTRIBUTE((unused))) const {
1243 return cmp(a, b);
1244 }
1245 virtual int cmp(const uchar *, const uchar *) const = 0;
1246 virtual int cmp_binary(const uchar *a, const uchar *b,
1247 uint32 max_length MY_ATTRIBUTE((unused)) = ~0L) const {
1248 return memcmp(a, b, pack_length());
1249 }
cmp_offset(ptrdiff_t row_offset)1250 virtual int cmp_offset(ptrdiff_t row_offset) const {
1251 return cmp(ptr, ptr + row_offset);
1252 }
cmp_binary_offset(ptrdiff_t row_offset)1253 virtual int cmp_binary_offset(ptrdiff_t row_offset) const {
1254 return cmp_binary(ptr, ptr + row_offset);
1255 }
key_cmp(const uchar * a,const uchar * b)1256 virtual int key_cmp(const uchar *a, const uchar *b) const {
1257 return cmp(a, b);
1258 }
key_cmp(const uchar * str,uint length MY_ATTRIBUTE ((unused)))1259 virtual int key_cmp(const uchar *str,
1260 uint length MY_ATTRIBUTE((unused))) const {
1261 return cmp(ptr, str);
1262 }
decimals()1263 virtual uint decimals() const { return 0; }
is_text_key_type()1264 virtual bool is_text_key_type() const { return false; }
1265
1266 /*
1267 Caller beware: sql_type can change str.Ptr, so check
1268 ptr() to see if it changed if you are using your own buffer
1269 in str and restore it with set() if needed
1270 */
1271 virtual void sql_type(String &str) const = 0;
1272
1273 /**
1274 Check whether the full table's row is NULL or the Field has value NULL.
1275
1276 @return true if the full table's row is NULL or the Field has value NULL
1277 false if neither table's row nor the Field has value NULL
1278 */
1279 bool is_null(ptrdiff_t row_offset = 0) const {
1280 /*
1281 if the field is NULLable, it returns NULLity based
1282 on m_null_ptr[row_offset] value. Otherwise it returns
1283 NULL flag depending on TABLE::has_null_row() value.
1284
1285 The table may have been marked as containing only NULL values
1286 for all fields if it is a NULL-complemented row of an OUTER JOIN
1287 or if the query is an implicitly grouped query (has aggregate
1288 functions but no GROUP BY clause) with no qualifying rows. If
1289 this is the case (in which TABLE::has_null_row() is true) and the
1290 field is not nullable, the field is considered to be NULL.
1291
1292 Do not change the order of testing. Fields may be associated
1293 with a TABLE object without being part of the current row.
1294 For NULL value check to work for these fields, they must
1295 have a valid m_null_ptr, and this pointer must be checked before
1296 TABLE::has_null_row().
1297 */
1298 if (is_nullable()) return (m_null_ptr[row_offset] & null_bit);
1299
1300 if (is_tmp_nullable()) return m_is_tmp_null;
1301
1302 return table->has_null_row();
1303 }
1304
1305 /**
1306 Check whether the Field has value NULL (temporary or actual).
1307
1308 @return true if the Field has value NULL (temporary or actual)
1309 false if the Field has value NOT NULL.
1310 */
1311 bool is_real_null(ptrdiff_t row_offset = 0) const {
1312 if (is_nullable()) return (m_null_ptr[row_offset] & null_bit);
1313
1314 if (is_tmp_nullable()) return m_is_tmp_null;
1315
1316 return false;
1317 }
1318
1319 /**
1320 Check if the Field has value NULL or the record specified by argument
1321 has value NULL for this Field.
1322
1323 @return true if the Field has value NULL or the record has value NULL
1324 for thois Field.
1325 */
is_null_in_record(const uchar * record)1326 bool is_null_in_record(const uchar *record) const {
1327 if (is_nullable()) return (record[null_offset()] & null_bit);
1328
1329 return is_tmp_nullable() ? m_is_tmp_null : false;
1330 }
1331
1332 void set_null(ptrdiff_t row_offset = 0);
1333
1334 void set_notnull(ptrdiff_t row_offset = 0);
1335
1336 // Cannot be const as it calls set_warning
1337 type_conversion_status check_constraints(int mysql_errno);
1338
1339 /**
1340 Remember the value of THD::check_for_truncated_fields to handle possible
1341 NOT-NULL constraint errors after BEFORE-trigger execution is finished.
1342 We should save the value of THD::check_for_truncated_fields before starting
1343 BEFORE-trigger processing since during triggers execution the
1344 value of THD::check_for_truncated_fields could be changed.
1345 */
set_check_for_truncated_fields(enum_check_fields check_for_truncated_fields)1346 void set_check_for_truncated_fields(
1347 enum_check_fields check_for_truncated_fields) {
1348 m_check_for_truncated_fields_saved = check_for_truncated_fields;
1349 }
1350
1351 /// @return true if this field is NULL-able, false otherwise.
is_nullable()1352 bool is_nullable() const { return m_null_ptr != nullptr; }
1353
null_offset(const uchar * record)1354 uint null_offset(const uchar *record) const {
1355 return (uint)(m_null_ptr - record);
1356 }
1357
1358 uint null_offset() const;
1359
set_null_ptr(uchar * p_null_ptr,uint p_null_bit)1360 void set_null_ptr(uchar *p_null_ptr, uint p_null_bit) {
1361 m_null_ptr = p_null_ptr;
1362 null_bit = p_null_bit;
1363 }
1364
1365 /**
1366 Populates a Send_field object with metadata about the column represented by
1367 this Field object. The Send_field object is used for sending column metadata
1368 to the client.
1369
1370 @param[out] send_field the Send_field object to populate
1371 */
1372 virtual void make_send_field(Send_field *send_field) const;
1373
1374 /**
1375 Writes a copy of the current value in the record buffer, suitable for
1376 sorting using byte-by-byte comparison. Integers are always in big-endian
1377 regardless of hardware architecture. At most length bytes are written
1378 into the buffer.
1379
1380 @param buff The buffer, assumed to be at least length bytes.
1381
1382 @param length Number of bytes to write.
1383
1384 @retval The number of bytes actually written.
1385
1386 @note This is now only used by replication; filesort makes its own
1387 sort keys based off of Items, not Fields.
1388 */
1389 virtual size_t make_sort_key(uchar *buff, size_t length) const = 0;
1390 virtual bool optimize_range(uint idx, uint part) const;
1391 /*
1392 This should be true for fields which, when compared with constant
1393 items, can be casted to longlong. In this case we will at 'fix_fields'
1394 stage cast the constant items to longlongs and at the execution stage
1395 use field->val_int() for comparison. Used to optimize clauses like
1396 'a_column BETWEEN date_const, date_const'.
1397 */
can_be_compared_as_longlong()1398 virtual bool can_be_compared_as_longlong() const { return false; }
mem_free()1399 virtual void mem_free() {}
1400
1401 virtual Field *new_field(MEM_ROOT *root, TABLE *new_table) const;
1402
new_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uchar * new_null_ptr,uint new_null_bit)1403 Field *new_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr,
1404 uchar *new_null_ptr, uint new_null_bit) const {
1405 Field *field = new_field(root, new_table);
1406 field->move_field(new_ptr, new_null_ptr, new_null_bit);
1407 return field;
1408 }
1409
1410 virtual Field *new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr,
1411 uchar *new_null_ptr, uint new_null_bit) const;
1412
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr)1413 Field *new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr) const {
1414 return new_key_field(root, new_table, new_ptr, m_null_ptr, null_bit);
1415 }
1416
1417 /**
1418 Makes a shallow copy of the Field object.
1419
1420 @note This member function must be overridden in all concrete
1421 subclasses. Several of the Field subclasses are concrete even though they
1422 are not leaf classes, so the compiler will not always catch this.
1423
1424 @param mem_root MEM_ROOT to use for memory allocation.
1425 @retval NULL If memory allocation failed.
1426 */
1427 virtual Field *clone(MEM_ROOT *mem_root) const = 0;
1428
move_field(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg)1429 void move_field(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg) {
1430 ptr = ptr_arg;
1431 m_null_ptr = null_ptr_arg;
1432 null_bit = null_bit_arg;
1433 }
1434
move_field_offset(ptrdiff_t ptr_diff)1435 virtual void move_field_offset(ptrdiff_t ptr_diff) {
1436 ptr += ptr_diff;
1437 if (is_nullable()) m_null_ptr += ptr_diff;
1438 }
1439
get_image(uchar * buff,size_t length,const CHARSET_INFO *)1440 virtual void get_image(uchar *buff, size_t length,
1441 const CHARSET_INFO *) const {
1442 memcpy(buff, ptr, length);
1443 }
1444
set_image(const uchar * buff,size_t length,const CHARSET_INFO *)1445 virtual void set_image(const uchar *buff, size_t length,
1446 const CHARSET_INFO *) {
1447 memcpy(ptr, buff, length);
1448 }
1449
1450 /*
1451 Copy a field part into an output buffer.
1452
1453 SYNOPSIS
1454 Field::get_key_image()
1455 buff [out] output buffer
1456 length output buffer size
1457 type itMBR for geometry blobs, otherwise itRAW
1458
1459 DESCRIPTION
1460 This function makes a copy of field part of size equal to or
1461 less than "length" parameter value.
1462 For fields of string types (CHAR, VARCHAR, TEXT) the rest of buffer
1463 is padded by zero byte.
1464
1465 NOTES
1466 For variable length character fields (i.e. UTF-8) the "length"
1467 parameter means a number of output buffer bytes as if all field
1468 characters have maximal possible size (mbmaxlen). In the other words,
1469 "length" parameter is a number of characters multiplied by
1470 field_charset->mbmaxlen.
1471
1472 RETURN
1473 Number of copied bytes (excluding padded zero bytes -- see above).
1474 */
1475
get_key_image(uchar * buff,size_t length,imagetype type MY_ATTRIBUTE ((unused)))1476 virtual size_t get_key_image(uchar *buff, size_t length,
1477 imagetype type MY_ATTRIBUTE((unused))) const {
1478 get_image(buff, length, &my_charset_bin);
1479 return length;
1480 }
set_key_image(const uchar * buff,size_t length)1481 virtual void set_key_image(const uchar *buff, size_t length) {
1482 set_image(buff, length, &my_charset_bin);
1483 }
val_int_offset(ptrdiff_t row_offset)1484 longlong val_int_offset(ptrdiff_t row_offset) {
1485 ptr += row_offset;
1486 longlong tmp = val_int();
1487 ptr -= row_offset;
1488 return tmp;
1489 }
val_int(uchar * new_ptr)1490 longlong val_int(uchar *new_ptr) {
1491 uchar *old_ptr = ptr;
1492 longlong return_value;
1493 ptr = new_ptr;
1494 return_value = val_int();
1495 ptr = old_ptr;
1496 return return_value;
1497 }
val_str(String * str,uchar * new_ptr)1498 String *val_str(String *str, uchar *new_ptr) {
1499 uchar *old_ptr = ptr;
1500 ptr = new_ptr;
1501 val_str(str);
1502 ptr = old_ptr;
1503 return str;
1504 }
1505
1506 /**
1507 Send the value of this field over the protocol using the correct
1508 Protocol::store*() function which matches the type of the field.
1509 */
1510 virtual bool send_to_protocol(Protocol *protocol) const;
1511
1512 /**
1513 Pack the field into a format suitable for storage and transfer.
1514
1515 To implement packing functionality, only the virtual function
1516 should be overridden. The other functions are just convenience
1517 functions and hence should not be overridden.
1518
1519 The actual format is opaque and will vary between types of Field
1520 (it is meant to be unpacked by unpack(), but be aware that it is
1521 used among others in the replication log, so you cannot change it
1522 without incurring a format break.
1523
1524 @note The default implementation just copies the raw bytes
1525 of the record into the destination, but never more than
1526 <code>max_length</code> characters.
1527
1528 @param to
1529 Pointer to memory area where representation of field should be put.
1530
1531 @param from
1532 Pointer to memory area where record representation of field is
1533 stored, typically field->field_ptr().
1534
1535 @param max_length
1536 Available space in “to”, in bytes. pack() will not write more bytes than
1537 this; if the field is too short, the contents _are not unpackable by
1538 unpack()_. (It is nominally supposed to be a prefix of what would have
1539 been written with a full buffer, ie., the same as packing and then
1540 truncating the output, but not all Field classes follow this.)
1541
1542 @return The byte after the last byte in “to” written to. If the return
1543 value is equal to (to + max_length), it could either be that the value
1544 fit exactly, or that the buffer was too small; you cannot distinguish
1545 between the two cases based on the return value alone.
1546 */
1547 virtual uchar *pack(uchar *to, const uchar *from, size_t max_length) const;
1548
pack(uchar * to)1549 uchar *pack(uchar *to) const { return pack(to, ptr, UINT_MAX); }
1550
1551 virtual const uchar *unpack(uchar *to, const uchar *from, uint param_data);
1552
unpack(const uchar * from)1553 const uchar *unpack(const uchar *from) { return unpack(ptr, from, 0U); }
1554
1555 /**
1556 This function does the same thing as pack(), except for the difference
1557 that max_length does not mean the number of bytes in the output, but the
1558 maximum field length from the input (which must be exactly
1559 field->max_field_length()). The difference is currently only relevant for
1560 Field_blob, but can be summed up as follows:
1561
1562 - If the actual field length is longer than "max_length", by way of
1563 software bug or otherwise, the function may behave as if it were shorter,
1564 and write something that is still readable by unpack().
1565 - There is no bounds checking; the caller must verify that there is
1566 sufficient space in "to". Even in the case of truncation, "to" must
1567 be long enough to hold the untruncated field, as the return pointer
1568 would otherwise be invalid, causing undefined behavior as per the C++
1569 standard.
1570 */
pack_with_metadata_bytes(uchar * to,const uchar * from,uint max_length)1571 virtual uchar *pack_with_metadata_bytes(uchar *to, const uchar *from,
1572 uint max_length) const {
1573 return pack(to, from, max_length);
1574 }
1575
1576 /**
1577 Write the field for the binary log in diff format.
1578
1579 This should only write the field if the diff format is smaller
1580 than the full format. Otherwise it should leave the buffer
1581 untouched.
1582
1583 @param[in,out] to Pointer to buffer where the field will be
1584 written. This will be changed to point to the next byte after the
1585 last byte that was written.
1586
1587 @param value_options bitmap that indicates if full or partial
1588 JSON format is to be used.
1589
1590 @retval true The field was not written, either because the data
1591 type does not support it, or because it was disabled according to
1592 value_options, or because there was no diff information available
1593 from the optimizer, or because the the diff format was bigger than
1594 the full format. The 'to' parameter is unchanged in this case.
1595
1596 @retval false The field was written.
1597 */
pack_diff(uchar ** to MY_ATTRIBUTE ((unused)),ulonglong value_options MY_ATTRIBUTE ((unused)))1598 virtual bool pack_diff(uchar **to MY_ATTRIBUTE((unused)),
1599 ulonglong value_options MY_ATTRIBUTE((unused))) const {
1600 return true;
1601 }
1602
1603 /**
1604 This is a wrapper around pack_length() used by filesort() to determine
1605 how many bytes we need for packing "addon fields".
1606 @returns maximum size of a row when stored in the filesort buffer.
1607 */
1608
max_packed_col_length()1609 virtual uint max_packed_col_length() const { return pack_length(); }
1610
offset(uchar * record)1611 uint offset(uchar *record) const { return (uint)(ptr - record); }
1612
1613 void copy_data(ptrdiff_t src_record_offset);
1614
1615 virtual bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const;
1616
1617 virtual bool get_time(MYSQL_TIME *ltime) const;
1618
charset()1619 virtual const CHARSET_INFO *charset() const { return &my_charset_bin; }
1620
charset_for_protocol()1621 const CHARSET_INFO *charset_for_protocol() const {
1622 return binary() ? &my_charset_bin : charset();
1623 }
sort_charset()1624 virtual const CHARSET_INFO *sort_charset() const { return charset(); }
has_charset()1625 virtual bool has_charset() const { return false; }
1626 /*
1627 match_collation_to_optimize_range() is to distinguish in
1628 range optimizer (see opt_range.cc) between real string types:
1629 CHAR, VARCHAR, TEXT
1630 and the other string-alike types with result_type() == STRING_RESULT:
1631 DATE, TIME, DATETIME, TIMESTAMP
1632 We need it to decide whether to test if collation of the operation
1633 matches collation of the field (needed only for real string types).
1634 QQ: shouldn't DATE/TIME types have their own XXX_RESULT types eventually?
1635 */
1636
match_collation_to_optimize_range()1637 virtual bool match_collation_to_optimize_range() const { return false; }
derivation()1638 virtual enum Derivation derivation() const { return DERIVATION_IMPLICIT; }
repertoire()1639 virtual uint repertoire() const { return MY_REPERTOIRE_UNICODE30; }
set_derivation(enum Derivation)1640 virtual void set_derivation(enum Derivation) {}
1641
1642 /**
1643 Produce warning or note about data saved into field.
1644
1645 @param level - level of message (Note/Warning/Error)
1646 @param code - error code of message to be produced
1647 @param cut_increment - whenever we should increase cut fields count
1648
1649 @note
1650 This function won't produce warning and increase cut fields counter
1651 if check_for_truncated_fields == CHECK_FIELD_IGNORE for current thread.
1652
1653 if check_for_truncated_fields == CHECK_FIELD_IGNORE then we ignore notes.
1654 This allows us to avoid notes in optimization, like
1655 convert_constant_item().
1656
1657 @retval
1658 1 if check_for_truncated_fields == CHECK_FIELD_IGNORE and error level
1659 is not NOTE
1660 @retval
1661 0 otherwise
1662 */
set_warning(Sql_condition::enum_severity_level level,unsigned int code,int cut_increment)1663 bool set_warning(Sql_condition::enum_severity_level level, unsigned int code,
1664 int cut_increment) {
1665 return set_warning(level, code, cut_increment, nullptr, nullptr);
1666 }
1667
1668 bool set_warning(Sql_condition::enum_severity_level level, uint code,
1669 int cut_increment, const char *view_db,
1670 const char *view_name);
1671
1672 bool warn_if_overflow(int op_result);
1673 virtual void init(TABLE *table_arg);
1674
1675 /* maximum possible display length */
1676 virtual uint32 max_display_length() const = 0;
1677
1678 /**
1679 Whether a field being created is type-compatible with an existing one.
1680
1681 Used by the ALTER TABLE code to evaluate whether the new definition
1682 of a table is compatible with the old definition so that it can
1683 determine if data needs to be copied over (table data change).
1684 Constraints and generation clause (default value, generation expression)
1685 are not checked by this function.
1686
1687 @param new_field new field definition from alter.
1688 @retval IS_EQUAL_YES if there is no change.
1689 @retval IS_EQUAL_PACK_LENGTH if the data are unchanged, but the length
1690 requirements have changed
1691 @retval IS_EQUAL_NO if there is an incompatible change requiring copy.
1692 */
1693
1694 virtual uint is_equal(const Create_field *new_field) const;
1695
1696 /* convert decimal to longlong with overflow check */
1697 longlong convert_decimal2longlong(const my_decimal *val, bool unsigned_flag,
1698 bool *has_overflow);
1699 /* The max. number of characters */
char_length()1700 virtual uint32 char_length() const {
1701 return field_length / charset()->mbmaxlen;
1702 }
1703
get_geometry_type()1704 virtual geometry_type get_geometry_type() const {
1705 /* shouldn't get here. */
1706 DBUG_ASSERT(0);
1707 return GEOM_GEOMETRY;
1708 }
1709 #ifndef DBUG_OFF
1710 /* Print field value into debug trace, in NULL-aware way. */
dbug_print()1711 void dbug_print() const {
1712 if (is_real_null())
1713 fprintf(DBUG_FILE, "NULL");
1714 else {
1715 char buf[256];
1716 String str(buf, sizeof(buf), &my_charset_bin);
1717 str.length(0);
1718 String *pstr;
1719 pstr = val_str(&str);
1720 fprintf(DBUG_FILE, "'%s'", pstr->c_ptr_safe());
1721 }
1722 }
1723 #endif
1724
field_storage_type()1725 ha_storage_media field_storage_type() const {
1726 return (ha_storage_media)((flags >> FIELD_FLAGS_STORAGE_MEDIA) & 3);
1727 }
1728
set_storage_type(ha_storage_media storage_type_arg)1729 void set_storage_type(ha_storage_media storage_type_arg) {
1730 DBUG_ASSERT(field_storage_type() == HA_SM_DEFAULT);
1731 flags |= (storage_type_arg << FIELD_FLAGS_STORAGE_MEDIA);
1732 }
1733
column_format()1734 column_format_type column_format() const {
1735 return (column_format_type)((flags >> FIELD_FLAGS_COLUMN_FORMAT) & 3);
1736 }
1737
set_column_format(column_format_type column_format_arg)1738 void set_column_format(column_format_type column_format_arg) {
1739 DBUG_ASSERT(column_format() == COLUMN_FORMAT_TYPE_DEFAULT);
1740 flags |= (column_format_arg << FIELD_FLAGS_COLUMN_FORMAT);
1741 }
1742
1743 /* Validate the value stored in a field */
validate_stored_val(THD * thd MY_ATTRIBUTE ((unused)))1744 virtual type_conversion_status validate_stored_val(
1745 THD *thd MY_ATTRIBUTE((unused))) {
1746 return TYPE_OK;
1747 }
1748
1749 /* Hash value */
1750 virtual void hash(ulong *nr, ulong *nr2) const;
1751
1752 /**
1753 Get the upper limit of the MySQL integral and floating-point type.
1754
1755 @return maximum allowed value for the field
1756 */
get_max_int_value()1757 virtual ulonglong get_max_int_value() const {
1758 DBUG_ASSERT(false);
1759 return 0ULL;
1760 }
1761
1762 /**
1763 Return a const pointer to the actual data in the record buffer.
1764
1765 For most fields, this is the same as field_ptr(), but BLOBs and VARCHARs
1766 it is not. Ideally this function should not be used as it makes it hard
1767 to change the internal representation of Field.
1768 */
data_ptr()1769 virtual const uchar *data_ptr() const { return ptr; }
1770
1771 /**
1772 Return a const pointer to where the field is stored in the record buffer.
1773
1774 Ideally this function should not be used as it makes it hard
1775 to change the internal representation of Field.
1776 */
field_ptr()1777 const uchar *field_ptr() const { return ptr; }
1778
1779 /**
1780 Return a pointer to where the field is stored in the record buffer.
1781
1782 Ideally this function should not be used as it makes it hard
1783 to change the internal representation of Field.
1784 */
field_ptr()1785 uchar *field_ptr() { return ptr; }
1786
set_field_ptr(uchar * ptr_arg)1787 void set_field_ptr(uchar *ptr_arg) { ptr = ptr_arg; }
1788
1789 /**
1790 Checks whether a string field is part of write_set.
1791
1792 @return
1793 false - If field is not char/varchar/....
1794 - If field is char/varchar/.. and is not part of write set.
1795 true - If field is char/varchar/.. and is part of write set.
1796 */
is_updatable()1797 virtual bool is_updatable() const { return false; }
1798
1799 /**
1800 Check whether field is part of the index taking the index extensions flag
1801 into account. Index extensions are also not applicable to UNIQUE indexes
1802 for loose index scans.
1803
1804 @param[in] thd THD object
1805 @param[in] cur_index Index of the key
1806 @param[in] cur_index_info key_info object
1807
1808 @retval true Field is part of the key
1809 @retval false otherwise
1810
1811 */
1812
1813 bool is_part_of_actual_key(THD *thd, uint cur_index,
1814 KEY *cur_index_info) const;
1815
1816 /**
1817 Get covering prefix keys.
1818
1819 @retval covering prefix keys.
1820 */
1821 Key_map get_covering_prefix_keys() const;
1822
1823 /// Whether the field is a typed array
is_array()1824 virtual bool is_array() const { return false; }
1825
1826 /**
1827 Return number of bytes the field's length takes
1828
1829 Valid only for varchar and typed arrays of varchar
1830 */
get_length_bytes()1831 virtual uint32 get_length_bytes() const {
1832 DBUG_ASSERT(0);
1833 return 0;
1834 }
1835
1836 /**
1837 Whether field's old valued have to be handled.
1838
1839 @returns
1840 true if field is virtual an either one of BLOB types or typed array
1841 false otherwise
1842 */
handle_old_value()1843 bool handle_old_value() const {
1844 return (is_flag_set(BLOB_FLAG) || is_array()) && is_virtual_gcol();
1845 }
1846
1847 /**
1848 Sets field index.
1849
1850 @param[in] field_index Field index.
1851 */
set_field_index(uint16 field_index)1852 virtual void set_field_index(uint16 field_index) {
1853 m_field_index = field_index;
1854 }
1855
1856 /**
1857 Returns field index.
1858
1859 @returns Field index.
1860 */
field_index()1861 uint16 field_index() const { return m_field_index; }
1862
1863 private:
1864 /**
1865 Retrieve the field metadata for fields.
1866
1867 This default implementation returns 0 and saves 0 in the metadata_ptr
1868 value.
1869
1870 @param metadata_ptr First byte of field metadata
1871
1872 @returns 0 no bytes written.
1873 */
do_save_field_metadata(uchar * metadata_ptr MY_ATTRIBUTE ((unused)))1874 virtual int do_save_field_metadata(
1875 uchar *metadata_ptr MY_ATTRIBUTE((unused))) const {
1876 return 0;
1877 }
1878
1879 protected:
1880 uchar *pack_int16(uchar *to, const uchar *from, size_t max_length) const;
1881
1882 const uchar *unpack_int16(uchar *to, const uchar *from) const;
1883
1884 uchar *pack_int24(uchar *to, const uchar *from, size_t max_length) const;
1885
1886 const uchar *unpack_int24(uchar *to, const uchar *from) const;
1887
1888 uchar *pack_int32(uchar *to, const uchar *from, size_t max_length) const;
1889
1890 const uchar *unpack_int32(uchar *to, const uchar *from) const;
1891
1892 uchar *pack_int64(uchar *to, const uchar *from, size_t max_length) const;
1893
1894 const uchar *unpack_int64(uchar *to, const uchar *from) const;
1895 };
1896
1897 /**
1898 This class is a substitute for the Field classes during CREATE TABLE
1899
1900 When adding a functional index at table creation, we need to resolve the
1901 expression we are indexing. All functions that references one or more
1902 columns expects a Field to be available. But during CREATE TABLE, we only
1903 have access to Create_field. So this class acts as a subsitute for the
1904 Field classes so that expressions can be properly resolved. Thus, trying
1905 to call store or val_* on this class will cause an assertion.
1906 */
1907 class Create_field_wrapper final : public Field {
1908 const Create_field *m_field;
1909
1910 public:
1911 Create_field_wrapper(const Create_field *fld);
1912 Item_result result_type() const final override;
1913 Item_result numeric_context_result_type() const final override;
1914 enum_field_types type() const final override;
1915 uint32 max_display_length() const final override;
1916
1917 const CHARSET_INFO *charset() const final override;
1918
1919 uint32 pack_length() const final override;
1920
1921 // Since it's not a real field, functions below shouldn't be used.
1922 /* purecov: begin deadcode */
store(const char *,size_t,const CHARSET_INFO *)1923 type_conversion_status store(const char *, size_t,
1924 const CHARSET_INFO *) final override {
1925 DBUG_ASSERT(false);
1926 return TYPE_ERR_BAD_VALUE;
1927 }
store(double)1928 type_conversion_status store(double) final override {
1929 DBUG_ASSERT(false);
1930 return TYPE_ERR_BAD_VALUE;
1931 }
store(longlong,bool)1932 type_conversion_status store(longlong, bool) final override {
1933 DBUG_ASSERT(false);
1934 return TYPE_ERR_BAD_VALUE;
1935 }
store_decimal(const my_decimal *)1936 type_conversion_status store_decimal(const my_decimal *) final override {
1937 DBUG_ASSERT(false);
1938 return TYPE_ERR_BAD_VALUE;
1939 }
val_real(void)1940 double val_real(void) const final override {
1941 DBUG_ASSERT(false);
1942 return 0.0;
1943 }
val_int(void)1944 longlong val_int(void) const final override {
1945 DBUG_ASSERT(false);
1946 return 0;
1947 }
val_decimal(my_decimal *)1948 my_decimal *val_decimal(my_decimal *) const final override {
1949 DBUG_ASSERT(false);
1950 return nullptr;
1951 }
val_str(String *,String *)1952 String *val_str(String *, String *) const final override {
1953 DBUG_ASSERT(false);
1954 return nullptr;
1955 }
cmp(const uchar *,const uchar *)1956 int cmp(const uchar *, const uchar *) const final override {
1957 DBUG_ASSERT(false);
1958 return -1;
1959 }
sql_type(String &)1960 void sql_type(String &) const final override { DBUG_ASSERT(false); }
make_sort_key(uchar *,size_t)1961 size_t make_sort_key(uchar *, size_t) const final override {
1962 DBUG_ASSERT(false);
1963 return 0;
1964 }
clone(MEM_ROOT * mem_root)1965 Field *clone(MEM_ROOT *mem_root) const final override {
1966 return new (mem_root) Create_field_wrapper(*this);
1967 }
1968 /* purecov: end */
1969 };
1970
1971 class Field_num : public Field {
1972 private:
1973 /**
1974 Whether the field is signed or not. Meaningful only for numeric fields
1975 and numeric arrays.
1976 */
1977 const bool unsigned_flag;
1978
1979 public:
1980 const uint8 dec;
1981 /**
1982 True if the column was declared with the ZEROFILL attribute. If it has the
1983 attribute, values should be zero-padded up to the declared display width
1984 when they are converted to strings.
1985 */
1986 bool zerofill; // Purify cannot handle bit fields
1987 Field_num(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1988 uchar null_bit_arg, uchar auto_flags_arg,
1989 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
1990 bool unsigned_arg);
is_unsigned()1991 bool is_unsigned() const override final { return unsigned_flag; }
result_type()1992 Item_result result_type() const override { return REAL_RESULT; }
derivation()1993 enum Derivation derivation() const final override {
1994 return DERIVATION_NUMERIC;
1995 }
repertoire()1996 uint repertoire() const final override { return MY_REPERTOIRE_NUMERIC; }
charset()1997 const CHARSET_INFO *charset() const final override {
1998 return &my_charset_numeric;
1999 }
2000 void prepend_zeros(String *value) const;
decimals()2001 uint decimals() const final override { return (uint)dec; }
2002 bool eq_def(const Field *field) const final override;
2003 type_conversion_status store_decimal(const my_decimal *) override;
2004 type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec) override;
2005 my_decimal *val_decimal(my_decimal *) const override;
2006 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const override;
2007 bool get_time(MYSQL_TIME *ltime) const override;
2008 uint is_equal(const Create_field *new_field) const override;
row_pack_length()2009 uint row_pack_length() const final override { return pack_length(); }
pack_length_from_metadata(uint)2010 uint32 pack_length_from_metadata(uint) const override {
2011 return pack_length();
2012 }
2013 type_conversion_status check_int(const CHARSET_INFO *cs, const char *str,
2014 size_t length, const char *int_end,
2015 int error);
2016 type_conversion_status get_int(const CHARSET_INFO *cs, const char *from,
2017 size_t len, longlong *rnd,
2018 ulonglong unsigned_max, longlong signed_min,
2019 longlong signed_max);
2020 };
2021
2022 class Field_str : public Field {
2023 protected:
2024 const CHARSET_INFO *field_charset;
2025 enum Derivation field_derivation;
2026
2027 public:
2028 Field_str(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2029 uchar null_bit_arg, uchar auto_flags_arg,
2030 const char *field_name_arg, const CHARSET_INFO *charset);
result_type()2031 Item_result result_type() const override { return STRING_RESULT; }
numeric_context_result_type()2032 Item_result numeric_context_result_type() const final override {
2033 return REAL_RESULT;
2034 }
decimals()2035 uint decimals() const override { return DECIMAL_NOT_SPECIFIED; }
2036 void make_send_field(Send_field *field) const override;
2037 type_conversion_status store(double nr) override;
2038 type_conversion_status store(longlong nr, bool unsigned_val) override = 0;
2039 type_conversion_status store_decimal(const my_decimal *) override;
2040 type_conversion_status store(const char *to, size_t length,
2041 const CHARSET_INFO *cs) override = 0;
2042
repertoire()2043 uint repertoire() const final override {
2044 return my_charset_repertoire(field_charset);
2045 }
charset()2046 const CHARSET_INFO *charset() const override { return field_charset; }
set_charset(const CHARSET_INFO * charset_arg)2047 void set_charset(const CHARSET_INFO *charset_arg) {
2048 field_charset = charset_arg;
2049 char_length_cache = char_length();
2050 }
set_field_length(uint32 length)2051 void set_field_length(uint32 length) final override {
2052 Field::set_field_length(length);
2053 char_length_cache = char_length();
2054 }
derivation()2055 enum Derivation derivation() const final override { return field_derivation; }
set_derivation(enum Derivation derivation_arg)2056 void set_derivation(enum Derivation derivation_arg) final override {
2057 field_derivation = derivation_arg;
2058 }
binary()2059 bool binary() const override { return field_charset == &my_charset_bin; }
max_display_length()2060 uint32 max_display_length() const override { return field_length; }
str_needs_quotes()2061 bool str_needs_quotes() const final override { return true; }
2062 uint is_equal(const Create_field *new_field) const override;
2063
2064 // An always-updated cache of the result of char_length(), because
2065 // dividing by charset()->mbmaxlen can be surprisingly costly compared
2066 // to the rest of e.g. make_sort_key().
2067 uint32 char_length_cache;
2068 };
2069
2070 /* base class for Field_string, Field_varstring and Field_blob */
2071
2072 class Field_longstr : public Field_str {
2073 private:
2074 type_conversion_status report_if_important_data(const char *ptr,
2075 const char *end,
2076 bool count_spaces);
2077
2078 protected:
2079 type_conversion_status check_string_copy_error(
2080 const char *well_formed_error_pos, const char *cannot_convert_error_pos,
2081 const char *from_end_pos, const char *end, bool count_spaces,
2082 const CHARSET_INFO *cs);
2083
2084 public:
Field_longstr(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,const CHARSET_INFO * charset_arg)2085 Field_longstr(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2086 uchar null_bit_arg, uchar auto_flags_arg,
2087 const char *field_name_arg, const CHARSET_INFO *charset_arg)
2088 : Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2089 field_name_arg, charset_arg) {}
2090
2091 type_conversion_status store_decimal(const my_decimal *d) override;
2092 uint32 max_data_length() const override;
2093 bool is_updatable() const final override;
2094 };
2095
2096 /* base class for float and double and decimal (old one) */
2097 class Field_real : public Field_num {
2098 public:
2099 bool not_fixed;
2100 enum Truncate_result {
2101 TR_OK = 0,
2102 TR_POSITIVE_OVERFLOW = 1,
2103 TR_NEGATIVE_OVERFLOW = 2
2104 };
2105
Field_real(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)2106 Field_real(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2107 uchar null_bit_arg, uchar auto_flags_arg,
2108 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2109 bool unsigned_arg)
2110 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2111 field_name_arg, dec_arg, zero_arg, unsigned_arg),
2112 not_fixed(dec_arg >= DECIMAL_NOT_SPECIFIED) {}
2113 type_conversion_status store_decimal(const my_decimal *) final override;
2114 type_conversion_status store_time(MYSQL_TIME *ltime,
2115 uint8 dec) final override;
2116 my_decimal *val_decimal(my_decimal *) const final override;
2117 bool get_date(MYSQL_TIME *ltime,
2118 my_time_flags_t fuzzydate) const final override;
2119 bool get_time(MYSQL_TIME *ltime) const final override;
2120 Truncate_result truncate(double *nr, double max_length);
max_display_length()2121 uint32 max_display_length() const final override { return field_length; }
2122 const uchar *unpack(uchar *to, const uchar *from, uint param_data) override;
2123 uchar *pack(uchar *to, const uchar *from, size_t max_length) const override;
2124 };
2125
2126 class Field_decimal final : public Field_real {
2127 public:
Field_decimal(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)2128 Field_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2129 uchar null_bit_arg, uchar auto_flags_arg,
2130 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2131 bool unsigned_arg)
2132 : Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2133 field_name_arg, dec_arg, zero_arg, unsigned_arg) {}
type()2134 enum_field_types type() const final override { return MYSQL_TYPE_DECIMAL; }
key_type()2135 enum ha_base_keytype key_type() const final override {
2136 return zerofill ? HA_KEYTYPE_BINARY : HA_KEYTYPE_NUM;
2137 }
2138 type_conversion_status store(const char *to, size_t length,
2139 const CHARSET_INFO *charset) final override;
2140 type_conversion_status store(double nr) final override;
2141 type_conversion_status store(longlong nr, bool unsigned_val) final override;
2142 double val_real() const final override;
2143 longlong val_int() const final override;
2144 String *val_str(String *, String *) const final override;
2145 int cmp(const uchar *, const uchar *) const final override;
2146 size_t make_sort_key(uchar *buff, size_t length) const final override;
2147 void overflow(bool negative);
zero_pack()2148 bool zero_pack() const final override { return false; }
2149 void sql_type(String &str) const final override;
clone(MEM_ROOT * mem_root)2150 Field_decimal *clone(MEM_ROOT *mem_root) const final override {
2151 DBUG_ASSERT(type() == MYSQL_TYPE_DECIMAL);
2152 return new (mem_root) Field_decimal(*this);
2153 }
unpack(uchar * to,const uchar * from,uint param_data)2154 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final {
2155 return Field::unpack(to, from, param_data);
2156 }
pack(uchar * to,const uchar * from,size_t max_length)2157 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2158 return Field::pack(to, from, max_length);
2159 }
2160 };
2161
2162 /* New decimal/numeric field which use fixed point arithmetic */
2163 class Field_new_decimal : public Field_num {
2164 private:
2165 /**
2166 Normally, the underlying decimal code will degrade values' excessive
2167 precision. E.g. value 0.0 stored as decimal(10,4) will be returned as
2168 decimal(4,4). This is fine for general purpose, but isn't usable for
2169 multi-valued index. Field_typed_array uses a field for conversion and it
2170 expects the value read from it to be exactly same as it would be stored
2171 in SE, i.e with preserved precision. Otherwise, SE won't be able to
2172 index it.
2173 TRUE here tells underlying DECIMAL reading code to keep the precision as
2174 is.
2175 */
2176 bool m_keep_precision{false};
2177 int do_save_field_metadata(uchar *first_byte) const final override;
2178
2179 public:
2180 /* The maximum number of decimal digits can be stored */
2181 uint precision;
2182 uint bin_size;
2183 /*
2184 Constructors take max_length of the field as a parameter - not the
2185 precision as the number of decimal digits allowed.
2186 So for example we need to count length from precision handling
2187 CREATE TABLE ( DECIMAL(x,y))
2188 */
2189 Field_new_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2190 uchar null_bit_arg, uchar auto_flags_arg,
2191 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2192 bool unsigned_arg);
2193 Field_new_decimal(uint32 len_arg, bool is_nullable_arg,
2194 const char *field_name_arg, uint8 dec_arg,
2195 bool unsigned_arg);
type()2196 enum_field_types type() const final override { return MYSQL_TYPE_NEWDECIMAL; }
key_type()2197 enum ha_base_keytype key_type() const final override {
2198 return HA_KEYTYPE_BINARY;
2199 }
result_type()2200 Item_result result_type() const final override { return DECIMAL_RESULT; }
2201 type_conversion_status reset() final override;
2202 type_conversion_status store_value(const my_decimal *decimal_value);
2203 void set_value_on_overflow(my_decimal *decimal_value, bool sign) const;
2204 type_conversion_status store(const char *to, size_t length,
2205 const CHARSET_INFO *charset) final override;
2206 type_conversion_status store(double nr) final override;
2207 type_conversion_status store(longlong nr, bool unsigned_val) final override;
2208 type_conversion_status store_time(MYSQL_TIME *ltime,
2209 uint8 dec) final override;
2210 type_conversion_status store_decimal(const my_decimal *) final override;
2211 double val_real() const final override;
2212 longlong val_int() const final override;
2213 my_decimal *val_decimal(my_decimal *) const final override;
2214 bool get_date(MYSQL_TIME *ltime,
2215 my_time_flags_t fuzzydate) const final override;
2216 bool get_time(MYSQL_TIME *ltime) const final override;
2217 String *val_str(String *, String *) const final override;
2218 int cmp(const uchar *, const uchar *) const final override;
2219 size_t make_sort_key(uchar *buff, size_t length) const final override;
zero_pack()2220 bool zero_pack() const final override { return false; }
2221 void sql_type(String &str) const final override;
max_display_length()2222 uint32 max_display_length() const final override { return field_length; }
pack_length()2223 uint32 pack_length() const final override { return (uint32)bin_size; }
2224 uint pack_length_from_metadata(uint field_metadata) const final override;
2225 bool compatible_field_size(uint field_metadata, Relay_log_info *, uint16,
2226 int *order_var) const final override;
2227 uint is_equal(const Create_field *new_field) const final override;
clone(MEM_ROOT * mem_root)2228 Field_new_decimal *clone(MEM_ROOT *mem_root) const final override {
2229 DBUG_ASSERT(type() == MYSQL_TYPE_NEWDECIMAL);
2230 return new (mem_root) Field_new_decimal(*this);
2231 }
2232 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final;
2233 static Field *create_from_item(const Item *item);
2234 bool send_to_protocol(Protocol *protocol) const final override;
set_keep_precision(bool arg)2235 void set_keep_precision(bool arg) { m_keep_precision = arg; }
2236 };
2237
2238 class Field_tiny : public Field_num {
2239 public:
Field_tiny(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,bool zero_arg,bool unsigned_arg)2240 Field_tiny(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2241 uchar null_bit_arg, uchar auto_flags_arg,
2242 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2243 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2244 field_name_arg, 0, zero_arg, unsigned_arg) {}
Field_tiny(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,bool unsigned_arg)2245 Field_tiny(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2246 bool unsigned_arg)
2247 : Field_num(nullptr, len_arg,
2248 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2249 field_name_arg, 0, false, unsigned_arg) {}
result_type()2250 enum Item_result result_type() const final override { return INT_RESULT; }
type()2251 enum_field_types type() const override { return MYSQL_TYPE_TINY; }
key_type()2252 enum ha_base_keytype key_type() const final override {
2253 return is_unsigned() ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8;
2254 }
2255 type_conversion_status store(const char *to, size_t length,
2256 const CHARSET_INFO *charset) override;
2257 type_conversion_status store(double nr) override;
2258 type_conversion_status store(longlong nr, bool unsigned_val) override;
2259 double val_real() const override;
2260 longlong val_int() const override;
2261 String *val_str(String *, String *) const override;
2262 bool send_to_protocol(Protocol *protocol) const override;
2263 int cmp(const uchar *, const uchar *) const final override;
2264 size_t make_sort_key(uchar *buff, size_t length) const final override;
pack_length()2265 uint32 pack_length() const final override { return 1; }
2266 void sql_type(String &str) const override;
max_display_length()2267 uint32 max_display_length() const final override { return 4; }
clone(MEM_ROOT * mem_root)2268 Field_tiny *clone(MEM_ROOT *mem_root) const override {
2269 DBUG_ASSERT(type() == MYSQL_TYPE_TINY);
2270 return new (mem_root) Field_tiny(*this);
2271 }
pack(uchar * to,const uchar * from,size_t max_length)2272 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2273 if (max_length > 0) *to = *from;
2274 return to + 1;
2275 }
2276
unpack(uchar * to,const uchar * from,uint param_data MY_ATTRIBUTE ((unused)))2277 const uchar *unpack(uchar *to, const uchar *from,
2278 uint param_data MY_ATTRIBUTE((unused))) final {
2279 *to = *from;
2280 return from + 1;
2281 }
2282
get_max_int_value()2283 ulonglong get_max_int_value() const final override {
2284 return is_unsigned() ? 0xFFULL : 0x7FULL;
2285 }
2286 };
2287
2288 class Field_short final : public Field_num {
2289 public:
Field_short(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,bool zero_arg,bool unsigned_arg)2290 Field_short(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2291 uchar null_bit_arg, uchar auto_flags_arg,
2292 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2293 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2294 field_name_arg, 0, zero_arg, unsigned_arg) {}
Field_short(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,bool unsigned_arg)2295 Field_short(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2296 bool unsigned_arg)
2297 : Field_num(nullptr, len_arg,
2298 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2299 field_name_arg, 0, false, unsigned_arg) {}
Field_short(uint32 len_arg,const char * field_name_arg,bool unsigned_arg)2300 Field_short(uint32 len_arg, const char *field_name_arg, bool unsigned_arg)
2301 : Field_short(len_arg, false, field_name_arg, unsigned_arg) {}
result_type()2302 enum Item_result result_type() const final override { return INT_RESULT; }
type()2303 enum_field_types type() const final override { return MYSQL_TYPE_SHORT; }
key_type()2304 enum ha_base_keytype key_type() const final override {
2305 return is_unsigned() ? HA_KEYTYPE_USHORT_INT : HA_KEYTYPE_SHORT_INT;
2306 }
2307 type_conversion_status store(const char *to, size_t length,
2308 const CHARSET_INFO *charset) final override;
2309 type_conversion_status store(double nr) final override;
2310 type_conversion_status store(longlong nr, bool unsigned_val) final override;
2311 double val_real() const final override;
2312 longlong val_int() const final override;
2313 String *val_str(String *, String *) const final override;
2314 bool send_to_protocol(Protocol *protocol) const final override;
2315 int cmp(const uchar *, const uchar *) const final override;
2316 size_t make_sort_key(uchar *buff, size_t length) const final override;
pack_length()2317 uint32 pack_length() const final override { return 2; }
2318 void sql_type(String &str) const final override;
max_display_length()2319 uint32 max_display_length() const final override { return 6; }
clone(MEM_ROOT * mem_root)2320 Field_short *clone(MEM_ROOT *mem_root) const final override {
2321 DBUG_ASSERT(type() == MYSQL_TYPE_SHORT);
2322 return new (mem_root) Field_short(*this);
2323 }
pack(uchar * to,const uchar * from,size_t max_length)2324 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2325 return pack_int16(to, from, max_length);
2326 }
2327
unpack(uchar * to,const uchar * from,uint param_data MY_ATTRIBUTE ((unused)))2328 const uchar *unpack(uchar *to, const uchar *from,
2329 uint param_data MY_ATTRIBUTE((unused))) final {
2330 return unpack_int16(to, from);
2331 }
2332
get_max_int_value()2333 ulonglong get_max_int_value() const final override {
2334 return is_unsigned() ? 0xFFFFULL : 0x7FFFULL;
2335 }
2336 };
2337
2338 class Field_medium final : public Field_num {
2339 public:
Field_medium(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,bool zero_arg,bool unsigned_arg)2340 Field_medium(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2341 uchar null_bit_arg, uchar auto_flags_arg,
2342 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2343 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2344 field_name_arg, 0, zero_arg, unsigned_arg) {}
Field_medium(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,bool unsigned_arg)2345 Field_medium(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2346 bool unsigned_arg)
2347 : Field_num(nullptr, len_arg,
2348 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2349 field_name_arg, 0, false, unsigned_arg) {}
result_type()2350 enum Item_result result_type() const final override { return INT_RESULT; }
type()2351 enum_field_types type() const final override { return MYSQL_TYPE_INT24; }
key_type()2352 enum ha_base_keytype key_type() const final override {
2353 return is_unsigned() ? HA_KEYTYPE_UINT24 : HA_KEYTYPE_INT24;
2354 }
2355 type_conversion_status store(const char *to, size_t length,
2356 const CHARSET_INFO *charset) final override;
2357 type_conversion_status store(double nr) final override;
2358 type_conversion_status store(longlong nr, bool unsigned_val) final override;
2359 double val_real() const final override;
2360 longlong val_int() const final override;
2361 String *val_str(String *, String *) const final override;
2362 bool send_to_protocol(Protocol *protocol) const final override;
2363 int cmp(const uchar *, const uchar *) const final override;
2364 size_t make_sort_key(uchar *buff, size_t length) const final override;
pack_length()2365 uint32 pack_length() const final override { return 3; }
2366 void sql_type(String &str) const final override;
max_display_length()2367 uint32 max_display_length() const final override { return 8; }
clone(MEM_ROOT * mem_root)2368 Field_medium *clone(MEM_ROOT *mem_root) const final override {
2369 DBUG_ASSERT(type() == MYSQL_TYPE_INT24);
2370 return new (mem_root) Field_medium(*this);
2371 }
get_max_int_value()2372 ulonglong get_max_int_value() const final override {
2373 return is_unsigned() ? 0xFFFFFFULL : 0x7FFFFFULL;
2374 }
2375 };
2376
2377 class Field_long : public Field_num {
2378 public:
2379 static const int PACK_LENGTH = 4;
2380
Field_long(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,bool zero_arg,bool unsigned_arg)2381 Field_long(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2382 uchar null_bit_arg, uchar auto_flags_arg,
2383 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2384 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2385 field_name_arg, 0, zero_arg, unsigned_arg) {}
Field_long(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,bool unsigned_arg)2386 Field_long(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2387 bool unsigned_arg)
2388 : Field_num(nullptr, len_arg,
2389 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2390 field_name_arg, 0, false, unsigned_arg) {}
result_type()2391 enum Item_result result_type() const final override { return INT_RESULT; }
type()2392 enum_field_types type() const final override { return MYSQL_TYPE_LONG; }
key_type()2393 enum ha_base_keytype key_type() const final override {
2394 return is_unsigned() ? HA_KEYTYPE_ULONG_INT : HA_KEYTYPE_LONG_INT;
2395 }
2396 type_conversion_status store(const char *to, size_t length,
2397 const CHARSET_INFO *charset) final override;
2398 type_conversion_status store(double nr) final override;
2399 type_conversion_status store(longlong nr, bool unsigned_val) override;
2400 double val_real() const final override;
2401 longlong val_int() const final override;
2402 bool send_to_protocol(Protocol *protocol) const final override;
2403 String *val_str(String *, String *) const final override;
2404 int cmp(const uchar *, const uchar *) const final override;
2405 size_t make_sort_key(uchar *buff, size_t length) const final override;
pack_length()2406 uint32 pack_length() const final override { return PACK_LENGTH; }
2407 void sql_type(String &str) const final override;
max_display_length()2408 uint32 max_display_length() const final override {
2409 return MY_INT32_NUM_DECIMAL_DIGITS;
2410 }
clone(MEM_ROOT * mem_root)2411 Field_long *clone(MEM_ROOT *mem_root) const final override {
2412 DBUG_ASSERT(type() == MYSQL_TYPE_LONG);
2413 return new (mem_root) Field_long(*this);
2414 }
pack(uchar * to,const uchar * from,size_t max_length)2415 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2416 return pack_int32(to, from, max_length);
2417 }
unpack(uchar * to,const uchar * from,uint param_data MY_ATTRIBUTE ((unused)))2418 const uchar *unpack(uchar *to, const uchar *from,
2419 uint param_data MY_ATTRIBUTE((unused))) final {
2420 return unpack_int32(to, from);
2421 }
2422
get_max_int_value()2423 ulonglong get_max_int_value() const final override {
2424 return is_unsigned() ? 0xFFFFFFFFULL : 0x7FFFFFFFULL;
2425 }
2426 };
2427
2428 class Field_longlong : public Field_num {
2429 public:
2430 static const int PACK_LENGTH = 8;
2431
Field_longlong(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,bool zero_arg,bool unsigned_arg)2432 Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2433 uchar null_bit_arg, uchar auto_flags_arg,
2434 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2435 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2436 field_name_arg, 0, zero_arg, unsigned_arg) {}
Field_longlong(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,bool unsigned_arg)2437 Field_longlong(uint32 len_arg, bool is_nullable_arg,
2438 const char *field_name_arg, bool unsigned_arg)
2439 : Field_num(nullptr, len_arg,
2440 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2441 field_name_arg, 0, false, unsigned_arg) {}
result_type()2442 enum Item_result result_type() const final override { return INT_RESULT; }
type()2443 enum_field_types type() const final override { return MYSQL_TYPE_LONGLONG; }
key_type()2444 enum ha_base_keytype key_type() const final override {
2445 return is_unsigned() ? HA_KEYTYPE_ULONGLONG : HA_KEYTYPE_LONGLONG;
2446 }
2447 type_conversion_status store(const char *to, size_t length,
2448 const CHARSET_INFO *charset) final override;
2449 type_conversion_status store(double nr) final override;
2450 type_conversion_status store(longlong nr, bool unsigned_val) override;
2451 double val_real() const final override;
2452 longlong val_int() const override;
2453 String *val_str(String *, String *) const final override;
2454 bool send_to_protocol(Protocol *protocol) const final override;
2455 int cmp(const uchar *, const uchar *) const final override;
2456 size_t make_sort_key(uchar *buff, size_t length) const final override;
pack_length()2457 uint32 pack_length() const final override { return PACK_LENGTH; }
2458 void sql_type(String &str) const final override;
can_be_compared_as_longlong()2459 bool can_be_compared_as_longlong() const final override { return true; }
max_display_length()2460 uint32 max_display_length() const final override { return 20; }
clone(MEM_ROOT * mem_root)2461 Field_longlong *clone(MEM_ROOT *mem_root) const final override {
2462 DBUG_ASSERT(type() == MYSQL_TYPE_LONGLONG);
2463 return new (mem_root) Field_longlong(*this);
2464 }
pack(uchar * to,const uchar * from,size_t max_length)2465 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2466 return pack_int64(to, from, max_length);
2467 }
unpack(uchar * to,const uchar * from,uint param_data MY_ATTRIBUTE ((unused)))2468 const uchar *unpack(uchar *to, const uchar *from,
2469 uint param_data MY_ATTRIBUTE((unused))) final {
2470 return unpack_int64(to, from);
2471 }
2472
get_max_int_value()2473 ulonglong get_max_int_value() const final override {
2474 return is_unsigned() ? 0xFFFFFFFFFFFFFFFFULL : 0x7FFFFFFFFFFFFFFFULL;
2475 }
2476 };
2477
2478 class Field_float final : public Field_real {
2479 public:
Field_float(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)2480 Field_float(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2481 uchar null_bit_arg, uchar auto_flags_arg,
2482 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2483 bool unsigned_arg)
2484 : Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2485 field_name_arg, dec_arg, zero_arg, unsigned_arg) {}
Field_float(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,uint8 dec_arg,bool unsigned_arg)2486 Field_float(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2487 uint8 dec_arg, bool unsigned_arg)
2488 : Field_real(nullptr, len_arg,
2489 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2490 field_name_arg, dec_arg, false, unsigned_arg) {}
type()2491 enum_field_types type() const final override { return MYSQL_TYPE_FLOAT; }
key_type()2492 enum ha_base_keytype key_type() const final override {
2493 return HA_KEYTYPE_FLOAT;
2494 }
2495 type_conversion_status store(const char *to, size_t length,
2496 const CHARSET_INFO *charset) final override;
2497 type_conversion_status store(double nr) final override;
2498 type_conversion_status store(longlong nr, bool unsigned_val) final override;
2499 double val_real() const final override;
2500 longlong val_int() const final override;
2501 String *val_str(String *, String *) const final override;
2502 bool send_to_protocol(Protocol *protocol) const final override;
2503 int cmp(const uchar *, const uchar *) const final override;
2504 size_t make_sort_key(uchar *buff, size_t length) const final override;
pack_length()2505 uint32 pack_length() const final override { return sizeof(float); }
2506 void sql_type(String &str) const final override;
clone(MEM_ROOT * mem_root)2507 Field_float *clone(MEM_ROOT *mem_root) const final override {
2508 DBUG_ASSERT(type() == MYSQL_TYPE_FLOAT);
2509 return new (mem_root) Field_float(*this);
2510 }
2511
get_max_int_value()2512 ulonglong get_max_int_value() const final override {
2513 /*
2514 We use the maximum as per IEEE754-2008 standard, 2^24
2515 */
2516 return 0x1000000ULL;
2517 }
2518
2519 private:
2520 int do_save_field_metadata(uchar *first_byte) const final override;
2521 };
2522
2523 class Field_double final : public Field_real {
2524 public:
Field_double(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)2525 Field_double(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2526 uchar null_bit_arg, uchar auto_flags_arg,
2527 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2528 bool unsigned_arg)
2529 : Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2530 field_name_arg, dec_arg, zero_arg, unsigned_arg) {}
Field_double(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,uint8 dec_arg)2531 Field_double(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2532 uint8 dec_arg)
2533 : Field_real(nullptr, len_arg,
2534 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2535 field_name_arg, dec_arg, false, false) {}
Field_double(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,uint8 dec_arg,bool unsigned_arg)2536 Field_double(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2537 uint8 dec_arg, bool unsigned_arg)
2538 : Field_real(nullptr, len_arg,
2539 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2540 field_name_arg, dec_arg, false, unsigned_arg) {}
Field_double(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,uint8 dec_arg,bool unsigned_arg,bool not_fixed_arg)2541 Field_double(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2542 uint8 dec_arg, bool unsigned_arg, bool not_fixed_arg)
2543 : Field_real(nullptr, len_arg,
2544 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2545 field_name_arg, dec_arg, false, unsigned_arg) {
2546 not_fixed = not_fixed_arg;
2547 }
type()2548 enum_field_types type() const final override { return MYSQL_TYPE_DOUBLE; }
key_type()2549 enum ha_base_keytype key_type() const final override {
2550 return HA_KEYTYPE_DOUBLE;
2551 }
2552 type_conversion_status store(const char *to, size_t length,
2553 const CHARSET_INFO *charset) final override;
2554 type_conversion_status store(double nr) final override;
2555 type_conversion_status store(longlong nr, bool unsigned_val) final override;
2556 double val_real() const final override;
2557 longlong val_int() const final override;
2558 String *val_str(String *, String *) const final override;
2559 bool send_to_protocol(Protocol *protocol) const final override;
2560 int cmp(const uchar *, const uchar *) const final override;
2561 size_t make_sort_key(uchar *buff, size_t length) const final override;
pack_length()2562 uint32 pack_length() const final override { return sizeof(double); }
2563 void sql_type(String &str) const final override;
clone(MEM_ROOT * mem_root)2564 Field_double *clone(MEM_ROOT *mem_root) const final override {
2565 DBUG_ASSERT(type() == MYSQL_TYPE_DOUBLE);
2566 return new (mem_root) Field_double(*this);
2567 }
2568
get_max_int_value()2569 ulonglong get_max_int_value() const final override {
2570 /*
2571 We use the maximum as per IEEE754-2008 standard, 2^53
2572 */
2573 return 0x20000000000000ULL;
2574 }
2575
2576 private:
2577 int do_save_field_metadata(uchar *first_byte) const final override;
2578 };
2579
2580 /* Everything saved in this will disappear. It will always return NULL */
2581
2582 class Field_null final : public Field_str {
2583 public:
Field_null(uchar * ptr_arg,uint32 len_arg,uchar auto_flags_arg,const char * field_name_arg,const CHARSET_INFO * cs)2584 Field_null(uchar *ptr_arg, uint32 len_arg, uchar auto_flags_arg,
2585 const char *field_name_arg, const CHARSET_INFO *cs)
2586 // (dummy_null_buffer & 32) is true, so is_null() always returns true.
2587 : Field_str(ptr_arg, len_arg, &dummy_null_buffer, 32, auto_flags_arg,
2588 field_name_arg, cs) {}
type()2589 enum_field_types type() const final override { return MYSQL_TYPE_NULL; }
store(const char *,size_t,const CHARSET_INFO *)2590 type_conversion_status store(const char *, size_t,
2591 const CHARSET_INFO *) final override {
2592 return TYPE_OK;
2593 }
store(double)2594 type_conversion_status store(double) final override { return TYPE_OK; }
store(longlong,bool)2595 type_conversion_status store(longlong, bool) final override {
2596 return TYPE_OK;
2597 }
store_decimal(const my_decimal *)2598 type_conversion_status store_decimal(const my_decimal *) final override {
2599 return TYPE_OK;
2600 }
reset()2601 type_conversion_status reset() final override { return TYPE_OK; }
val_real()2602 double val_real() const final override { return 0.0; }
val_int()2603 longlong val_int() const final override { return 0; }
val_decimal(my_decimal *)2604 my_decimal *val_decimal(my_decimal *) const final override { return nullptr; }
val_str(String *,String * value2)2605 String *val_str(String *, String *value2) const final override {
2606 value2->length(0);
2607 return value2;
2608 }
cmp(const uchar *,const uchar *)2609 int cmp(const uchar *, const uchar *) const final override { return 0; }
make_sort_key(uchar *,size_t len)2610 size_t make_sort_key(uchar *, size_t len) const final override { return len; }
pack_length()2611 uint32 pack_length() const final override { return 0; }
2612 void sql_type(String &str) const final override;
max_display_length()2613 uint32 max_display_length() const final override { return 4; }
clone(MEM_ROOT * mem_root)2614 Field_null *clone(MEM_ROOT *mem_root) const final override {
2615 DBUG_ASSERT(type() == MYSQL_TYPE_NULL);
2616 return new (mem_root) Field_null(*this);
2617 }
2618 };
2619
2620 /*
2621 Abstract class for TIME, DATE, DATETIME, TIMESTAMP
2622 with and without fractional part.
2623 */
2624 class Field_temporal : public Field {
2625 protected:
2626 uint8 dec; // Number of fractional digits
2627
2628 /**
2629 Adjust number of decimal digits from DECIMAL_NOT_SPECIFIED to
2630 DATETIME_MAX_DECIMALS
2631 */
normalize_dec(uint8 dec_arg)2632 static uint8 normalize_dec(uint8 dec_arg) {
2633 return dec_arg == DECIMAL_NOT_SPECIFIED ? DATETIME_MAX_DECIMALS : dec_arg;
2634 }
2635
2636 /**
2637 Low level routine to store a MYSQL_TIME value into a field.
2638 The value must be already properly rounded or truncated
2639 and checked for being a valid TIME/DATE/DATETIME value.
2640
2641 @param[in] ltime MYSQL_TIME value.
2642 @param[out] error Error flag vector, set in case of error.
2643 @retval false In case of success.
2644 @retval true In case of error.
2645 */
2646 virtual type_conversion_status store_internal(const MYSQL_TIME *ltime,
2647 int *error) = 0;
2648
2649 /**
2650 Low level routine to store a MYSQL_TIME value into a field
2651 with rounding/truncation according to the field decimals() value and
2652 sql_mode.
2653
2654 @param[in] ltime MYSQL_TIME value.
2655 @param[out] warnings Error flag vector, set in case of error.
2656 @retval false In case of success.
2657 @retval true In case of error.
2658 */
2659 virtual type_conversion_status store_internal_adjust_frac(MYSQL_TIME *ltime,
2660 int *warnings) = 0;
2661
2662 /**
2663 Store a temporal value in lldiv_t into a field,
2664 with rounding according to the field decimals() value.
2665
2666 @param[in] lld Temporal value.
2667 @param[out] warning Warning flag vector.
2668 @retval false In case of success.
2669 @retval true In case of error.
2670 */
2671 type_conversion_status store_lldiv_t(const lldiv_t *lld, int *warning);
2672
2673 /**
2674 Convert a string to MYSQL_TIME, according to the field type.
2675
2676 @param[in] str String
2677 @param[in] len String length
2678 @param[in] cs String character set
2679 @param[out] ltime The value is stored here
2680 @param[out] status Conversion status
2681 @retval false Conversion went fine, ltime contains a valid time
2682 @retval true Conversion failed, ltime was reset and contains nothing
2683 */
2684 virtual bool convert_str_to_TIME(const char *str, size_t len,
2685 const CHARSET_INFO *cs, MYSQL_TIME *ltime,
2686 MYSQL_TIME_STATUS *status) = 0;
2687 /**
2688 Convert a number with fractional part with nanosecond precision
2689 into MYSQL_TIME, according to the field type. Nanoseconds
2690 are rounded to milliseconds and added to ltime->second_part.
2691
2692 @param[in] nr Number
2693 @param[in] unsigned_val SIGNED/UNSIGNED flag
2694 @param[in] nanoseconds Fractional part in nanoseconds
2695 @param[out] ltime The value is stored here
2696 @param[in,out] warning Warnings found during execution
2697
2698 @return Conversion status
2699 @retval false On success
2700 @retval true On error
2701 */
2702 virtual type_conversion_status convert_number_to_TIME(longlong nr,
2703 bool unsigned_val,
2704 int nanoseconds,
2705 MYSQL_TIME *ltime,
2706 int *warning) = 0;
2707
2708 /**
2709 Convert an integer number into MYSQL_TIME, according to the field type.
2710
2711 @param[in] nr Number
2712 @param[in] unsigned_val SIGNED/UNSIGNED flag
2713 @param[out] ltime The value is stored here
2714 @param[in,out] warnings Warnings found during execution
2715
2716 @retval false On success
2717 @retval true On error
2718 */
2719 longlong convert_number_to_datetime(longlong nr, bool unsigned_val,
2720 MYSQL_TIME *ltime, int *warnings);
2721
2722 /**
2723 Set a warning according to warning bit flag vector.
2724 Multiple warnings are possible at the same time.
2725 Every warning in the bit vector is set by an individual
2726 set_datetime_warning() call.
2727
2728 @param str Warning parameter
2729 @param warnings Warning bit flag
2730
2731 @retval false Function reported warning
2732 @retval true Function reported error
2733 */
2734 bool set_warnings(const ErrConvString &str, int warnings)
2735 MY_ATTRIBUTE((warn_unused_result));
2736
2737 /**
2738 Flags that are passed as "flag" argument to
2739 check_date(), number_to_datetime(), str_to_datetime().
2740
2741 Flags depend on the session sql_mode settings, such as
2742 MODE_NO_ZERO_DATE, MODE_NO_ZERO_IN_DATE.
2743 Also, Field_newdate, Field_datetime, Field_datetimef add TIME_FUZZY_DATE
2744 to the session sql_mode settings, to allow relaxed date format,
2745 while Field_timestamp, Field_timestampf do not.
2746
2747 @param thd THD
2748 @retval sql_mode flags mixed with the field type flags.
2749 */
date_flags(const THD * thd MY_ATTRIBUTE ((unused)))2750 virtual my_time_flags_t date_flags(
2751 const THD *thd MY_ATTRIBUTE((unused))) const {
2752 return 0;
2753 }
2754
2755 /**
2756 Flags that are passed as "flag" argument to
2757 check_date(), number_to_datetime(), str_to_datetime().
2758 Similar to the above when we don't have a THD value.
2759 */
2760 my_time_flags_t date_flags() const;
2761
2762 /**
2763 Set a single warning using make_truncated_value_warning().
2764
2765 @param[in] level Warning level (error, warning, note)
2766 @param[in] code Warning code
2767 @param[in] val Warning parameter
2768 @param[in] ts_type Timestamp type (time, date, datetime, none)
2769 @param[in] truncate_increment Incrementing of truncated field counter
2770
2771 @retval false Function reported warning
2772 @retval true Function reported error
2773 */
2774 bool set_datetime_warning(Sql_condition::enum_severity_level level, uint code,
2775 const ErrConvString &val,
2776 enum_mysql_timestamp_type ts_type,
2777 int truncate_increment)
2778 MY_ATTRIBUTE((warn_unused_result));
2779
2780 public:
2781 /**
2782 Constructor for Field_temporal
2783 @param ptr_arg See Field definition
2784 @param null_ptr_arg See Field definition
2785 @param null_bit_arg See Field definition
2786 @param auto_flags_arg See Field definition
2787 @param field_name_arg See Field definition
2788 @param len_arg Number of characters in the integer part.
2789 @param dec_arg Number of second fraction digits, 0..6.
2790 */
Field_temporal(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint32 len_arg,uint8 dec_arg)2791 Field_temporal(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2792 uchar auto_flags_arg, const char *field_name_arg,
2793 uint32 len_arg, uint8 dec_arg)
2794 : Field(ptr_arg,
2795 len_arg +
2796 ((normalize_dec(dec_arg)) ? normalize_dec(dec_arg) + 1 : 0),
2797 null_ptr_arg, null_bit_arg, auto_flags_arg, field_name_arg) {
2798 set_flag(BINARY_FLAG);
2799 dec = normalize_dec(dec_arg);
2800 }
result_type()2801 Item_result result_type() const final override { return STRING_RESULT; }
max_display_length()2802 uint32 max_display_length() const final override { return field_length; }
str_needs_quotes()2803 bool str_needs_quotes() const final override { return true; }
2804 uint is_equal(const Create_field *new_field) const final override;
numeric_context_result_type()2805 Item_result numeric_context_result_type() const final override {
2806 return dec ? DECIMAL_RESULT : INT_RESULT;
2807 }
cmp_type()2808 enum Item_result cmp_type() const final override { return INT_RESULT; }
derivation()2809 enum Derivation derivation() const final override {
2810 return DERIVATION_NUMERIC;
2811 }
repertoire()2812 uint repertoire() const final override { return MY_REPERTOIRE_NUMERIC; }
charset()2813 const CHARSET_INFO *charset() const final override {
2814 return &my_charset_numeric;
2815 }
can_be_compared_as_longlong()2816 bool can_be_compared_as_longlong() const final override { return true; }
binary()2817 bool binary() const final override { return true; }
2818 type_conversion_status store(const char *str, size_t len,
2819 const CHARSET_INFO *cs) final override;
2820 type_conversion_status store_decimal(
2821 const my_decimal *decimal) final override;
2822 type_conversion_status store(longlong nr, bool unsigned_val) override;
2823 type_conversion_status store(double nr) final override;
val_real()2824 double val_real() const override // FSP-enable types redefine it.
2825 {
2826 return (double)val_int();
2827 }
2828 my_decimal *val_decimal(
2829 my_decimal *decimal_value) const override; // FSP types redefine it
2830 };
2831
2832 /**
2833 Abstract class for types with date
2834 with optional time, with or without fractional part:
2835 DATE, DATETIME, DATETIME(N), TIMESTAMP, TIMESTAMP(N).
2836 */
2837 class Field_temporal_with_date : public Field_temporal {
2838 protected:
2839 /**
2840 Low level function to get value into MYSQL_TIME,
2841 without checking for being valid.
2842 */
2843 virtual bool get_date_internal(MYSQL_TIME *ltime) const = 0;
2844
2845 /**
2846 Get value into MYSQL_TIME and check TIME_NO_ZERO_DATE flag.
2847 @retval True on error: we get a zero value but flags disallow zero dates.
2848 @retval False on success.
2849 */
2850 bool get_internal_check_zero(MYSQL_TIME *ltime,
2851 my_time_flags_t fuzzydate) const;
2852
2853 type_conversion_status convert_number_to_TIME(longlong nr, bool unsigned_val,
2854 int nanoseconds,
2855 MYSQL_TIME *ltime,
2856 int *warning) final override;
2857 bool convert_str_to_TIME(const char *str, size_t len, const CHARSET_INFO *cs,
2858 MYSQL_TIME *ltime,
2859 MYSQL_TIME_STATUS *status) final override;
2860
2861 type_conversion_status store_internal_adjust_frac(
2862 MYSQL_TIME *ltime, int *warnings) final override;
2863 using Field_temporal::date_flags;
2864
2865 public:
2866 /**
2867 Constructor for Field_temporal
2868 @param ptr_arg See Field definition
2869 @param null_ptr_arg See Field definition
2870 @param null_bit_arg See Field definition
2871 @param auto_flags_arg See Field definition
2872 @param field_name_arg See Field definition
2873 @param int_length_arg Number of characters in the integer part.
2874 @param dec_arg Number of second fraction digits, 0..6.
2875 */
Field_temporal_with_date(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint8 int_length_arg,uint8 dec_arg)2876 Field_temporal_with_date(uchar *ptr_arg, uchar *null_ptr_arg,
2877 uchar null_bit_arg, uchar auto_flags_arg,
2878 const char *field_name_arg, uint8 int_length_arg,
2879 uint8 dec_arg)
2880 : Field_temporal(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2881 field_name_arg, int_length_arg, dec_arg) {}
2882 bool send_to_protocol(Protocol *protocol) const override;
2883 type_conversion_status store_time(MYSQL_TIME *ltime,
2884 uint8 dec) final override;
2885 String *val_str(String *, String *) const override;
2886 longlong val_time_temporal() const override;
2887 longlong val_date_temporal() const override;
get_time(MYSQL_TIME * ltime)2888 bool get_time(MYSQL_TIME *ltime) const final override {
2889 return get_date(ltime, TIME_FUZZY_DATE);
2890 }
2891 /* Validate the value stored in a field */
2892 type_conversion_status validate_stored_val(THD *thd) override;
2893 };
2894
2895 /**
2896 Abstract class for types with date and time,
2897 with or without fractional part:
2898 DATETIME, DATETIME(N), TIMESTAMP, TIMESTAMP(N).
2899 */
2900 class Field_temporal_with_date_and_time : public Field_temporal_with_date {
2901 private:
do_save_field_metadata(uchar * metadata_ptr)2902 int do_save_field_metadata(uchar *metadata_ptr) const override {
2903 if (decimals()) {
2904 *metadata_ptr = decimals();
2905 return 1;
2906 }
2907 return 0;
2908 }
2909
2910 protected:
2911 /**
2912 Initialize flags for TIMESTAMP DEFAULT CURRENT_TIMESTAMP / ON UPDATE
2913 CURRENT_TIMESTAMP columns.
2914
2915 @todo get rid of TIMESTAMP_FLAG and ON_UPDATE_NOW_FLAG.
2916 */
2917 void init_timestamp_flags();
2918 /**
2919 Store "struct timeval" value into field.
2920 The value must be properly rounded or truncated according
2921 to the number of fractional second digits.
2922 */
2923 virtual void store_timestamp_internal(const struct timeval *tm) = 0;
2924 bool convert_TIME_to_timestamp(THD *thd, const MYSQL_TIME *ltime,
2925 struct timeval *tm, int *error);
2926
2927 public:
2928 /**
2929 Constructor for Field_temporal_with_date_and_time
2930 @param ptr_arg See Field definition
2931 @param null_ptr_arg See Field definition
2932 @param null_bit_arg See Field definition
2933 @param auto_flags_arg See Field definition
2934 @param field_name_arg See Field definition
2935 @param dec_arg Number of second fraction digits, 0..6.
2936 */
Field_temporal_with_date_and_time(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint8 dec_arg)2937 Field_temporal_with_date_and_time(uchar *ptr_arg, uchar *null_ptr_arg,
2938 uchar null_bit_arg, uchar auto_flags_arg,
2939 const char *field_name_arg, uint8 dec_arg)
2940 : Field_temporal_with_date(ptr_arg, null_ptr_arg, null_bit_arg,
2941 auto_flags_arg, field_name_arg,
2942 MAX_DATETIME_WIDTH, dec_arg) {}
2943 void store_timestamp(const struct timeval *tm) override;
2944 };
2945
2946 /**
2947 Abstract class for types with date and time, with fractional part:
2948 DATETIME, DATETIME(N), TIMESTAMP, TIMESTAMP(N).
2949 */
2950 class Field_temporal_with_date_and_timef
2951 : public Field_temporal_with_date_and_time {
2952 private:
do_save_field_metadata(uchar * metadata_ptr)2953 int do_save_field_metadata(uchar *metadata_ptr) const final override {
2954 *metadata_ptr = decimals();
2955 return 1;
2956 }
2957
2958 public:
2959 /**
2960 Constructor for Field_temporal_with_date_and_timef
2961 @param ptr_arg See Field definition
2962 @param null_ptr_arg See Field definition
2963 @param null_bit_arg See Field definition
2964 @param auto_flags_arg See Field definition
2965 @param field_name_arg See Field definition
2966 @param dec_arg Number of second fraction digits, 0..6.
2967 */
Field_temporal_with_date_and_timef(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint8 dec_arg)2968 Field_temporal_with_date_and_timef(uchar *ptr_arg, uchar *null_ptr_arg,
2969 uchar null_bit_arg, uchar auto_flags_arg,
2970 const char *field_name_arg, uint8 dec_arg)
2971 : Field_temporal_with_date_and_time(ptr_arg, null_ptr_arg, null_bit_arg,
2972 auto_flags_arg, field_name_arg,
2973 dec_arg) {}
2974
decimals()2975 uint decimals() const final override { return dec; }
sort_charset()2976 const CHARSET_INFO *sort_charset() const final override {
2977 return &my_charset_bin;
2978 }
make_sort_key(uchar * to,size_t length)2979 size_t make_sort_key(uchar *to, size_t length) const final override {
2980 memcpy(to, ptr, length);
2981 return length;
2982 }
cmp(const uchar * a_ptr,const uchar * b_ptr)2983 int cmp(const uchar *a_ptr, const uchar *b_ptr) const final override {
2984 return memcmp(a_ptr, b_ptr, pack_length());
2985 }
row_pack_length()2986 uint row_pack_length() const final override { return pack_length(); }
2987 double val_real() const final override;
2988 longlong val_int() const final override;
2989 my_decimal *val_decimal(my_decimal *decimal_value) const final override;
2990 };
2991
2992 /*
2993 Field implementing TIMESTAMP data type without fractional seconds.
2994 We will be removed eventually.
2995 */
2996 class Field_timestamp : public Field_temporal_with_date_and_time {
2997 protected:
2998 my_time_flags_t date_flags(const THD *thd) const final override;
2999 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3000 int *error) final override;
3001 bool get_date_internal(MYSQL_TIME *ltime) const final override;
3002 void store_timestamp_internal(const struct timeval *tm) final override;
3003
3004 public:
3005 static const int PACK_LENGTH = 4;
3006 Field_timestamp(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
3007 uchar null_bit_arg, uchar auto_flags_arg,
3008 const char *field_name_arg);
3009 Field_timestamp(bool is_nullable_arg, const char *field_name_arg);
type()3010 enum_field_types type() const final override { return MYSQL_TYPE_TIMESTAMP; }
key_type()3011 enum ha_base_keytype key_type() const final override {
3012 return HA_KEYTYPE_ULONG_INT;
3013 }
3014 type_conversion_status store_packed(longlong nr) final override;
3015 longlong val_int() const final override;
3016 int cmp(const uchar *, const uchar *) const final override;
3017 size_t make_sort_key(uchar *buff, size_t length) const final override;
pack_length()3018 uint32 pack_length() const final override { return PACK_LENGTH; }
3019 void sql_type(String &str) const final override;
zero_pack()3020 bool zero_pack() const final override { return false; }
3021 /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
3022 bool get_timestamp(struct timeval *tm, int *warnings) const final override;
3023 bool get_date(MYSQL_TIME *ltime,
3024 my_time_flags_t fuzzydate) const final override;
clone(MEM_ROOT * mem_root)3025 Field_timestamp *clone(MEM_ROOT *mem_root) const final override {
3026 DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
3027 return new (mem_root) Field_timestamp(*this);
3028 }
pack(uchar * to,const uchar * from,size_t max_length)3029 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
3030 return pack_int32(to, from, max_length);
3031 }
unpack(uchar * to,const uchar * from,uint param_data MY_ATTRIBUTE ((unused)))3032 const uchar *unpack(uchar *to, const uchar *from,
3033 uint param_data MY_ATTRIBUTE((unused))) final {
3034 return unpack_int32(to, from);
3035 }
3036 /* Validate the value stored in a field */
3037 type_conversion_status validate_stored_val(THD *thd) final override;
3038 };
3039
3040 /*
3041 Field implementing TIMESTAMP(N) data type, where N=0..6.
3042 */
3043 class Field_timestampf : public Field_temporal_with_date_and_timef {
3044 protected:
3045 bool get_date_internal(MYSQL_TIME *ltime) const final override;
3046 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3047 int *error) final override;
3048 my_time_flags_t date_flags(const THD *thd) const final override;
3049 void store_timestamp_internal(const struct timeval *tm) override;
3050
3051 public:
3052 /**
3053 Field_timestampf constructor
3054 @param ptr_arg See Field definition
3055 @param null_ptr_arg See Field definition
3056 @param null_bit_arg See Field definition
3057 @param auto_flags_arg See Field definition
3058 @param field_name_arg See Field definition
3059 @param dec_arg Number of fractional second digits, 0..6.
3060 */
3061 Field_timestampf(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3062 uchar auto_flags_arg, const char *field_name_arg,
3063 uint8 dec_arg);
3064 /**
3065 Field_timestampf constructor
3066 @param is_nullable_arg See Field definition
3067 @param field_name_arg See Field definition
3068 @param dec_arg Number of fractional second digits, 0..6.
3069 */
3070 Field_timestampf(bool is_nullable_arg, const char *field_name_arg,
3071 uint8 dec_arg);
clone(MEM_ROOT * mem_root)3072 Field_timestampf *clone(MEM_ROOT *mem_root) const final override {
3073 DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
3074 return new (mem_root) Field_timestampf(*this);
3075 }
3076
type()3077 enum_field_types type() const final override { return MYSQL_TYPE_TIMESTAMP; }
real_type()3078 enum_field_types real_type() const final override {
3079 return MYSQL_TYPE_TIMESTAMP2;
3080 }
binlog_type()3081 enum_field_types binlog_type() const final override {
3082 return MYSQL_TYPE_TIMESTAMP2;
3083 }
zero_pack()3084 bool zero_pack() const final override { return false; }
3085
pack_length()3086 uint32 pack_length() const final override {
3087 return my_timestamp_binary_length(dec);
3088 }
pack_length_from_metadata(uint field_metadata)3089 uint pack_length_from_metadata(uint field_metadata) const final override {
3090 DBUG_TRACE;
3091 uint tmp = my_timestamp_binary_length(field_metadata);
3092 return tmp;
3093 }
3094
3095 type_conversion_status store_packed(longlong nr) final override;
3096 bool get_date(MYSQL_TIME *ltime,
3097 my_time_flags_t fuzzydate) const final override;
3098 void sql_type(String &str) const final override;
3099
3100 bool get_timestamp(struct timeval *tm, int *warnings) const final override;
3101 /* Validate the value stored in a field */
3102 type_conversion_status validate_stored_val(THD *thd) final override;
3103 };
3104
3105 class Field_year final : public Field_tiny {
3106 public:
3107 enum Limits { MIN_YEAR = 1901, MAX_YEAR = 2155 };
Field_year(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg)3108 Field_year(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3109 uchar auto_flags_arg, const char *field_name_arg)
3110 : Field_tiny(ptr_arg, 4, null_ptr_arg, null_bit_arg, auto_flags_arg,
3111 field_name_arg, true, true) {}
Field_year(bool is_nullable_arg,const char * field_name_arg)3112 Field_year(bool is_nullable_arg, const char *field_name_arg)
3113 : Field_tiny(nullptr, 4, is_nullable_arg ? &dummy_null_buffer : nullptr,
3114 0, NONE, field_name_arg, true, true) {}
type()3115 enum_field_types type() const final override { return MYSQL_TYPE_YEAR; }
3116 type_conversion_status store(const char *to, size_t length,
3117 const CHARSET_INFO *charset) final override;
3118 type_conversion_status store(double nr) final override;
3119 type_conversion_status store(longlong nr, bool unsigned_val) final override;
3120 type_conversion_status store_time(MYSQL_TIME *ltime,
3121 uint8 dec) final override;
3122 double val_real() const final override;
3123 longlong val_int() const final override;
3124 String *val_str(String *, String *) const final override;
3125 bool send_to_protocol(Protocol *protocol) const final override;
3126 void sql_type(String &str) const final override;
can_be_compared_as_longlong()3127 bool can_be_compared_as_longlong() const final override { return true; }
clone(MEM_ROOT * mem_root)3128 Field_year *clone(MEM_ROOT *mem_root) const final override {
3129 DBUG_ASSERT(type() == MYSQL_TYPE_YEAR);
3130 return new (mem_root) Field_year(*this);
3131 }
3132 };
3133
3134 class Field_newdate : public Field_temporal_with_date {
3135 protected:
3136 static const int PACK_LENGTH = 3;
3137 my_time_flags_t date_flags(const THD *thd) const final override;
3138 bool get_date_internal(MYSQL_TIME *ltime) const final override;
3139 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3140 int *error) final override;
3141
3142 public:
Field_newdate(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg)3143 Field_newdate(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3144 uchar auto_flags_arg, const char *field_name_arg)
3145 : Field_temporal_with_date(ptr_arg, null_ptr_arg, null_bit_arg,
3146 auto_flags_arg, field_name_arg, MAX_DATE_WIDTH,
3147 0) {}
Field_newdate(bool is_nullable_arg,const char * field_name_arg)3148 Field_newdate(bool is_nullable_arg, const char *field_name_arg)
3149 : Field_temporal_with_date(nullptr,
3150 is_nullable_arg ? &dummy_null_buffer : nullptr,
3151 0, NONE, field_name_arg, MAX_DATE_WIDTH, 0) {}
type()3152 enum_field_types type() const final override { return MYSQL_TYPE_DATE; }
real_type()3153 enum_field_types real_type() const final override {
3154 return MYSQL_TYPE_NEWDATE;
3155 }
key_type()3156 enum ha_base_keytype key_type() const final override {
3157 return HA_KEYTYPE_UINT24;
3158 }
3159 type_conversion_status store_packed(longlong nr) final override;
3160 longlong val_int() const final override;
3161 longlong val_time_temporal() const final override;
3162 longlong val_date_temporal() const final override;
3163 String *val_str(String *, String *) const final override;
3164 bool send_to_protocol(Protocol *protocol) const final override;
3165 int cmp(const uchar *, const uchar *) const final override;
3166 size_t make_sort_key(uchar *buff, size_t length) const final override;
pack_length()3167 uint32 pack_length() const final override { return PACK_LENGTH; }
3168 void sql_type(String &str) const final override;
zero_pack()3169 bool zero_pack() const final override { return true; }
3170 bool get_date(MYSQL_TIME *ltime,
3171 my_time_flags_t fuzzydate) const final override;
clone(MEM_ROOT * mem_root)3172 Field_newdate *clone(MEM_ROOT *mem_root) const final override {
3173 DBUG_ASSERT(type() == MYSQL_TYPE_DATE);
3174 DBUG_ASSERT(real_type() == MYSQL_TYPE_NEWDATE);
3175 return new (mem_root) Field_newdate(*this);
3176 }
3177 };
3178
3179 /**
3180 Abstract class for TIME and TIME(N).
3181 */
3182 class Field_time_common : public Field_temporal {
3183 protected:
3184 bool convert_str_to_TIME(const char *str, size_t len, const CHARSET_INFO *cs,
3185 MYSQL_TIME *ltime,
3186 MYSQL_TIME_STATUS *status) final override;
3187 /**
3188 @todo: convert_number_to_TIME returns conversion status through
3189 two different interfaces: return value and warning. It should be
3190 refactored to only use return value.
3191 */
3192 type_conversion_status convert_number_to_TIME(longlong nr, bool unsigned_val,
3193 int nanoseconds,
3194 MYSQL_TIME *ltime,
3195 int *warning) final override;
3196 /**
3197 Low-level function to store MYSQL_TIME value.
3198 The value must be rounded or truncated according to decimals().
3199 */
3200 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3201 int *error) override = 0;
3202 /**
3203 Function to store time value.
3204 The value is rounded/truncated according to decimals() and sql_mode.
3205 */
3206 type_conversion_status store_internal_adjust_frac(
3207 MYSQL_TIME *ltime, int *warnings) final override;
3208
3209 my_time_flags_t date_flags(const THD *thd) const final override;
3210 using Field_temporal::date_flags;
3211
3212 public:
3213 /**
3214 Constructor for Field_time_common
3215 @param ptr_arg See Field definition
3216 @param null_ptr_arg See Field definition
3217 @param null_bit_arg See Field definition
3218 @param auto_flags_arg See Field definition
3219 @param field_name_arg See Field definition
3220 @param dec_arg Number of second fraction digits, 0..6.
3221 */
Field_time_common(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint8 dec_arg)3222 Field_time_common(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3223 uchar auto_flags_arg, const char *field_name_arg,
3224 uint8 dec_arg)
3225 : Field_temporal(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3226 field_name_arg, MAX_TIME_WIDTH, dec_arg) {}
3227 type_conversion_status store_time(MYSQL_TIME *ltime,
3228 uint8 dec) final override;
3229 String *val_str(String *, String *) const final override;
3230 bool get_date(MYSQL_TIME *ltime,
3231 my_time_flags_t fuzzydate) const final override;
3232 longlong val_date_temporal() const final override;
3233 bool send_to_protocol(Protocol *protocol) const final override;
3234 };
3235
3236 /*
3237 Field implementing TIME data type without fractional seconds.
3238 It will be removed eventually.
3239 */
3240 class Field_time final : public Field_time_common {
3241 protected:
3242 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3243 int *error) final override;
3244
3245 public:
Field_time(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg)3246 Field_time(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3247 uchar auto_flags_arg, const char *field_name_arg)
3248 : Field_time_common(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3249 field_name_arg, 0) {}
Field_time(const char * field_name_arg)3250 Field_time(const char *field_name_arg)
3251 : Field_time_common(nullptr, nullptr, 0, NONE, field_name_arg, 0) {}
type()3252 enum_field_types type() const final override { return MYSQL_TYPE_TIME; }
key_type()3253 enum ha_base_keytype key_type() const final override {
3254 return HA_KEYTYPE_INT24;
3255 }
3256 type_conversion_status store_packed(longlong nr) final override;
3257 longlong val_int() const final override;
3258 longlong val_time_temporal() const final override;
3259 bool get_time(MYSQL_TIME *ltime) const final override;
3260 int cmp(const uchar *, const uchar *) const final override;
3261 size_t make_sort_key(uchar *buff, size_t length) const final override;
pack_length()3262 uint32 pack_length() const final override { return 3; }
3263 void sql_type(String &str) const final override;
zero_pack()3264 bool zero_pack() const final override { return true; }
clone(MEM_ROOT * mem_root)3265 Field_time *clone(MEM_ROOT *mem_root) const final override {
3266 DBUG_ASSERT(type() == MYSQL_TYPE_TIME);
3267 return new (mem_root) Field_time(*this);
3268 }
3269 };
3270
3271 /*
3272 Field implementing TIME(N) data type, where N=0..6.
3273 */
3274 class Field_timef final : public Field_time_common {
3275 private:
do_save_field_metadata(uchar * metadata_ptr)3276 int do_save_field_metadata(uchar *metadata_ptr) const final override {
3277 *metadata_ptr = decimals();
3278 return 1;
3279 }
3280
3281 protected:
3282 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3283 int *error) final override;
3284
3285 public:
3286 /**
3287 Constructor for Field_timef
3288 @param ptr_arg See Field definition
3289 @param null_ptr_arg See Field definition
3290 @param null_bit_arg See Field definition
3291 @param auto_flags_arg See Field definition
3292 @param field_name_arg See Field definition
3293 @param dec_arg Number of second fraction digits, 0..6.
3294 */
Field_timef(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint8 dec_arg)3295 Field_timef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3296 uchar auto_flags_arg, const char *field_name_arg, uint8 dec_arg)
3297 : Field_time_common(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3298 field_name_arg, dec_arg) {}
3299 /**
3300 Constructor for Field_timef
3301 @param is_nullable_arg See Field definition
3302 @param field_name_arg See Field definition
3303 @param dec_arg Number of second fraction digits, 0..6.
3304 */
Field_timef(bool is_nullable_arg,const char * field_name_arg,uint8 dec_arg)3305 Field_timef(bool is_nullable_arg, const char *field_name_arg, uint8 dec_arg)
3306 : Field_time_common(nullptr,
3307 is_nullable_arg ? &dummy_null_buffer : nullptr, 0,
3308 NONE, field_name_arg, dec_arg) {}
clone(MEM_ROOT * mem_root)3309 Field_timef *clone(MEM_ROOT *mem_root) const final override {
3310 DBUG_ASSERT(type() == MYSQL_TYPE_TIME);
3311 return new (mem_root) Field_timef(*this);
3312 }
decimals()3313 uint decimals() const final override { return dec; }
type()3314 enum_field_types type() const final override { return MYSQL_TYPE_TIME; }
real_type()3315 enum_field_types real_type() const final override { return MYSQL_TYPE_TIME2; }
binlog_type()3316 enum_field_types binlog_type() const final override {
3317 return MYSQL_TYPE_TIME2;
3318 }
3319 type_conversion_status store_packed(longlong nr) final override;
3320 type_conversion_status reset() final override;
3321 double val_real() const final override;
3322 longlong val_int() const final override;
3323 longlong val_time_temporal() const final override;
3324 bool get_time(MYSQL_TIME *ltime) const final override;
3325 my_decimal *val_decimal(my_decimal *) const final override;
pack_length()3326 uint32 pack_length() const final override {
3327 return my_time_binary_length(dec);
3328 }
pack_length_from_metadata(uint field_metadata)3329 uint pack_length_from_metadata(uint field_metadata) const final override {
3330 DBUG_TRACE;
3331 uint tmp = my_time_binary_length(field_metadata);
3332 return tmp;
3333 }
row_pack_length()3334 uint row_pack_length() const final override { return pack_length(); }
3335 void sql_type(String &str) const final override;
zero_pack()3336 bool zero_pack() const final override { return true; }
sort_charset()3337 const CHARSET_INFO *sort_charset() const final override {
3338 return &my_charset_bin;
3339 }
make_sort_key(uchar * to,size_t length)3340 size_t make_sort_key(uchar *to, size_t length) const final override {
3341 memcpy(to, ptr, length);
3342 return length;
3343 }
cmp(const uchar * a_ptr,const uchar * b_ptr)3344 int cmp(const uchar *a_ptr, const uchar *b_ptr) const final override {
3345 return memcmp(a_ptr, b_ptr, pack_length());
3346 }
3347 };
3348
3349 /*
3350 Field implementing DATETIME data type without fractional seconds.
3351 We will be removed eventually.
3352 */
3353 class Field_datetime : public Field_temporal_with_date_and_time {
3354 protected:
3355 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3356 int *error) final override;
3357 bool get_date_internal(MYSQL_TIME *ltime) const final override;
3358 my_time_flags_t date_flags(const THD *thd) const final override;
3359 void store_timestamp_internal(const struct timeval *tm) final override;
3360
3361 public:
3362 static const int PACK_LENGTH = 8;
3363
3364 /**
3365 DATETIME columns can be defined as having CURRENT_TIMESTAMP as the
3366 default value on inserts or updates. This constructor accepts a
3367 auto_flags argument which controls the column default expressions.
3368
3369 For DATETIME columns this argument is a bitmap combining two flags:
3370
3371 - DEFAULT_NOW - means that column has DEFAULT CURRENT_TIMESTAMP attribute.
3372 - ON_UPDATE_NOW - means that column has ON UPDATE CURRENT_TIMESTAMP.
3373
3374 (these two flags can be used orthogonally to each other).
3375 */
Field_datetime(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg)3376 Field_datetime(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3377 uchar auto_flags_arg, const char *field_name_arg)
3378 : Field_temporal_with_date_and_time(ptr_arg, null_ptr_arg, null_bit_arg,
3379 auto_flags_arg, field_name_arg, 0) {}
Field_datetime(const char * field_name_arg)3380 Field_datetime(const char *field_name_arg)
3381 : Field_temporal_with_date_and_time(nullptr, nullptr, 0, NONE,
3382 field_name_arg, 0) {}
type()3383 enum_field_types type() const final override { return MYSQL_TYPE_DATETIME; }
key_type()3384 enum ha_base_keytype key_type() const final override {
3385 return HA_KEYTYPE_ULONGLONG;
3386 }
3387 using Field_temporal_with_date_and_time::store; // Make -Woverloaded-virtual
3388 type_conversion_status store(longlong nr, bool unsigned_val) final override;
3389 type_conversion_status store_packed(longlong nr) final override;
3390 longlong val_int() const final override;
3391 String *val_str(String *, String *) const final override;
3392 int cmp(const uchar *, const uchar *) const final override;
3393 size_t make_sort_key(uchar *buff, size_t length) const final override;
pack_length()3394 uint32 pack_length() const final override { return PACK_LENGTH; }
3395 void sql_type(String &str) const final override;
zero_pack()3396 bool zero_pack() const final override { return true; }
3397 bool get_date(MYSQL_TIME *ltime,
3398 my_time_flags_t fuzzydate) const final override;
clone(MEM_ROOT * mem_root)3399 Field_datetime *clone(MEM_ROOT *mem_root) const final override {
3400 DBUG_ASSERT(type() == MYSQL_TYPE_DATETIME);
3401 return new (mem_root) Field_datetime(*this);
3402 }
pack(uchar * to,const uchar * from,size_t max_length)3403 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
3404 return pack_int64(to, from, max_length);
3405 }
unpack(uchar * to,const uchar * from,uint param_data MY_ATTRIBUTE ((unused)))3406 const uchar *unpack(uchar *to, const uchar *from,
3407 uint param_data MY_ATTRIBUTE((unused))) final {
3408 return unpack_int64(to, from);
3409 }
3410 };
3411
3412 /*
3413 Field implementing DATETIME(N) data type, where N=0..6.
3414 */
3415 class Field_datetimef : public Field_temporal_with_date_and_timef {
3416 protected:
3417 bool get_date_internal(MYSQL_TIME *ltime) const final override;
3418 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3419 int *error) final override;
3420 my_time_flags_t date_flags(const THD *thd) const final override;
3421 void store_timestamp_internal(const struct timeval *tm) final override;
3422
3423 public:
3424 /**
3425 Constructor for Field_datetimef
3426 @param ptr_arg See Field definition
3427 @param null_ptr_arg See Field definition
3428 @param null_bit_arg See Field definition
3429 @param auto_flags_arg See Field definition
3430 @param field_name_arg See Field definition
3431 @param dec_arg Number of second fraction digits, 0..6.
3432 */
Field_datetimef(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint8 dec_arg)3433 Field_datetimef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3434 uchar auto_flags_arg, const char *field_name_arg,
3435 uint8 dec_arg)
3436 : Field_temporal_with_date_and_timef(ptr_arg, null_ptr_arg, null_bit_arg,
3437 auto_flags_arg, field_name_arg,
3438 dec_arg) {}
3439 /**
3440 Constructor for Field_datetimef
3441 @param is_nullable_arg See Field definition
3442 @param field_name_arg See Field definition
3443 @param dec_arg Number of second fraction digits, 0..6.
3444 */
Field_datetimef(bool is_nullable_arg,const char * field_name_arg,uint8 dec_arg)3445 Field_datetimef(bool is_nullable_arg, const char *field_name_arg,
3446 uint8 dec_arg)
3447 : Field_temporal_with_date_and_timef(
3448 nullptr, is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
3449 field_name_arg, dec_arg) {}
clone(MEM_ROOT * mem_root)3450 Field_datetimef *clone(MEM_ROOT *mem_root) const final override {
3451 DBUG_ASSERT(type() == MYSQL_TYPE_DATETIME);
3452 return new (mem_root) Field_datetimef(*this);
3453 }
3454
type()3455 enum_field_types type() const final override { return MYSQL_TYPE_DATETIME; }
real_type()3456 enum_field_types real_type() const final override {
3457 return MYSQL_TYPE_DATETIME2;
3458 }
binlog_type()3459 enum_field_types binlog_type() const final override {
3460 return MYSQL_TYPE_DATETIME2;
3461 }
pack_length()3462 uint32 pack_length() const final override {
3463 return my_datetime_binary_length(dec);
3464 }
pack_length_from_metadata(uint field_metadata)3465 uint pack_length_from_metadata(uint field_metadata) const final override {
3466 DBUG_TRACE;
3467 uint tmp = my_datetime_binary_length(field_metadata);
3468 return tmp;
3469 }
zero_pack()3470 bool zero_pack() const final override { return true; }
3471
3472 type_conversion_status store_packed(longlong nr) final override;
3473 type_conversion_status reset() final override;
3474 longlong val_date_temporal() const final override;
3475 bool get_date(MYSQL_TIME *ltime,
3476 my_time_flags_t fuzzydate) const final override;
3477 void sql_type(String &str) const final override;
3478 };
3479
3480 class Field_string : public Field_longstr {
3481 public:
Field_string(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,const CHARSET_INFO * cs)3482 Field_string(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
3483 uchar null_bit_arg, uchar auto_flags_arg,
3484 const char *field_name_arg, const CHARSET_INFO *cs)
3485 : Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
3486 auto_flags_arg, field_name_arg, cs) {}
Field_string(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,const CHARSET_INFO * cs)3487 Field_string(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
3488 const CHARSET_INFO *cs)
3489 : Field_longstr(nullptr, len_arg,
3490 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
3491 field_name_arg, cs) {}
3492
type()3493 enum_field_types type() const final override { return MYSQL_TYPE_STRING; }
match_collation_to_optimize_range()3494 bool match_collation_to_optimize_range() const final override { return true; }
key_type()3495 enum ha_base_keytype key_type() const final override {
3496 return binary() ? HA_KEYTYPE_BINARY : HA_KEYTYPE_TEXT;
3497 }
zero_pack()3498 bool zero_pack() const final override { return false; }
reset()3499 type_conversion_status reset() final override {
3500 charset()->cset->fill(charset(), (char *)ptr, field_length,
3501 (has_charset() ? ' ' : 0));
3502 return TYPE_OK;
3503 }
3504 type_conversion_status store(const char *to, size_t length,
3505 const CHARSET_INFO *charset) final override;
3506 type_conversion_status store(longlong nr, bool unsigned_val) final override;
3507 // Inherit the store() overloads that have not been overridden.
3508 using Field_longstr::store;
3509 double val_real() const final override;
3510 longlong val_int() const final override;
3511 String *val_str(String *, String *) const final override;
3512 /**
3513 Get the C-string value, without using String class.
3514 @returns The C-string value of this field.
3515 */
val_str_quick()3516 LEX_CSTRING val_str_quick() const {
3517 const char *string = pointer_cast<const char *>(ptr);
3518 return {string,
3519 field_charset->cset->lengthsp(field_charset, string, field_length)};
3520 }
3521 my_decimal *val_decimal(my_decimal *) const final override;
3522 int cmp(const uchar *, const uchar *) const final override;
3523 size_t make_sort_key(uchar *buff, size_t length) const final override;
3524 void sql_type(String &str) const final override;
3525 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final;
3526 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final;
pack_length_from_metadata(uint field_metadata)3527 uint pack_length_from_metadata(uint field_metadata) const final override {
3528 DBUG_PRINT("debug", ("field_metadata: 0x%04x", field_metadata));
3529 if (field_metadata == 0) return row_pack_length();
3530 return (((field_metadata >> 4) & 0x300) ^ 0x300) +
3531 (field_metadata & 0x00ff);
3532 }
3533 bool compatible_field_size(uint field_metadata, Relay_log_info *rli,
3534 uint16 mflags,
3535 int *order_var) const final override;
row_pack_length()3536 uint row_pack_length() const final override { return field_length; }
3537 uint max_packed_col_length() const final override;
real_type()3538 enum_field_types real_type() const final override {
3539 return MYSQL_TYPE_STRING;
3540 }
has_charset()3541 bool has_charset() const final override {
3542 return charset() == &my_charset_bin ? false : true;
3543 }
clone(MEM_ROOT * mem_root)3544 Field_string *clone(MEM_ROOT *mem_root) const final override {
3545 DBUG_ASSERT(real_type() == MYSQL_TYPE_STRING);
3546 return new (mem_root) Field_string(*this);
3547 }
3548 size_t get_key_image(uchar *buff, size_t length,
3549 imagetype type) const final override;
is_text_key_type()3550 bool is_text_key_type() const final override {
3551 return binary() ? false : true;
3552 }
3553
3554 private:
3555 int do_save_field_metadata(uchar *first_byte) const final override;
3556 };
3557
3558 class Field_varstring : public Field_longstr {
3559 public:
3560 Field_varstring(uchar *ptr_arg, uint32 len_arg, uint length_bytes_arg,
3561 uchar *null_ptr_arg, uchar null_bit_arg, uchar auto_flags_arg,
3562 const char *field_name_arg, TABLE_SHARE *share,
3563 const CHARSET_INFO *cs);
3564 Field_varstring(uint32 len_arg, bool is_nullable_arg,
3565 const char *field_name_arg, TABLE_SHARE *share,
3566 const CHARSET_INFO *cs);
3567
type()3568 enum_field_types type() const final override { return MYSQL_TYPE_VARCHAR; }
match_collation_to_optimize_range()3569 bool match_collation_to_optimize_range() const final override { return true; }
3570 enum ha_base_keytype key_type() const final override;
row_pack_length()3571 uint row_pack_length() const final override { return field_length; }
zero_pack()3572 bool zero_pack() const final override { return false; }
pack_length()3573 uint32 pack_length() const final override {
3574 return (uint32)field_length + length_bytes;
3575 }
key_length()3576 uint32 key_length() const final override { return (uint32)field_length; }
3577 type_conversion_status store(const char *to, size_t length,
3578 const CHARSET_INFO *charset) override;
3579 type_conversion_status store(longlong nr, bool unsigned_val) final override;
3580 // Inherit the store() overloads that have not been overridden.
3581 using Field_longstr::store;
3582 double val_real() const final override;
3583 longlong val_int() const final override;
3584 String *val_str(String *, String *) const override;
3585 my_decimal *val_decimal(my_decimal *) const final override;
3586 int cmp_max(const uchar *, const uchar *,
3587 uint max_length) const final override;
cmp(const uchar * a,const uchar * b)3588 int cmp(const uchar *a, const uchar *b) const final override {
3589 return cmp_max(a, b, ~0L);
3590 }
3591 size_t make_sort_key(uchar *buff, size_t length) const final override;
3592 size_t get_key_image(uchar *buff, size_t length,
3593 imagetype type) const final override;
3594 void set_key_image(const uchar *buff, size_t length) final override;
3595 void sql_type(String &str) const final override;
3596 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final;
3597 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final;
3598 int cmp_binary(const uchar *a, const uchar *b,
3599 uint32 max_length = ~0L) const final override;
3600 int key_cmp(const uchar *, const uchar *) const final override;
3601 int key_cmp(const uchar *str, uint length) const final override;
3602
3603 uint32 data_length(ptrdiff_t row_offset = 0) const final override;
real_type()3604 enum_field_types real_type() const final override {
3605 return MYSQL_TYPE_VARCHAR;
3606 }
has_charset()3607 bool has_charset() const final override {
3608 return charset() == &my_charset_bin ? false : true;
3609 }
3610 Field *new_field(MEM_ROOT *root, TABLE *new_table) const final override;
3611 Field *new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr,
3612 uchar *new_null_ptr,
3613 uint new_null_bit) const final override;
clone(MEM_ROOT * mem_root)3614 Field_varstring *clone(MEM_ROOT *mem_root) const final override {
3615 DBUG_ASSERT(type() == MYSQL_TYPE_VARCHAR);
3616 DBUG_ASSERT(real_type() == MYSQL_TYPE_VARCHAR);
3617 return new (mem_root) Field_varstring(*this);
3618 }
3619 uint is_equal(const Create_field *new_field) const final override;
3620 void hash(ulong *nr, ulong *nr2) const final override;
data_ptr()3621 const uchar *data_ptr() const final override { return ptr + length_bytes; }
is_text_key_type()3622 bool is_text_key_type() const final override {
3623 return binary() ? false : true;
3624 }
get_length_bytes()3625 virtual uint32 get_length_bytes() const override { return length_bytes; }
3626
3627 private:
3628 /* Store number of bytes used to store length (1 or 2) */
3629 uint32 length_bytes;
3630
3631 int do_save_field_metadata(uchar *first_byte) const final override;
3632 };
3633
3634 class Field_blob : public Field_longstr {
3635 virtual type_conversion_status store_internal(const char *from, size_t length,
3636 const CHARSET_INFO *cs);
3637 /**
3638 Copy value to memory storage.
3639 */
3640 type_conversion_status store_to_mem(const char *from, size_t length,
3641 const CHARSET_INFO *cs, size_t max_length,
3642 Blob_mem_storage *);
3643
3644 protected:
3645 /**
3646 The number of bytes used to represent the length of the blob.
3647 */
3648 uint packlength;
3649
3650 /**
3651 The 'value'-object is a cache fronting the storage engine.
3652 */
3653 String value;
3654
3655 private:
3656 /**
3657 In order to support update of virtual generated columns of blob type,
3658 we need to allocate the space blob needs on server for old_row and
3659 new_row respectively. This variable is used to record the
3660 allocated blob space for old_row.
3661 */
3662 String old_value;
3663
3664 /**
3665 Whether we need to move the content of 'value' to 'old_value' before
3666 updating the BLOB stored in 'value'. This needs to be done for
3667 updates of BLOB columns that are virtual since the storage engine
3668 does not have its own copy of the old 'value'. This variable is set
3669 to true when we read the data into 'value'. It is reset when we move
3670 'value' to 'old_value'. The purpose of having this is to avoid that we
3671 do the move operation from 'value' to 'old_value' more than one time per
3672 record.
3673 Currently, this variable is introduced because the following call in
3674 sql_data_change.cc:
3675 \/\**
3676 @todo combine this call to update_generated_write_fields() with the one
3677 in fill_record() to avoid updating virtual generated fields twice.
3678 *\/
3679 if (table->has_gcol())
3680 update_generated_write_fields(table->write_set, table);
3681 When the @todo is done, m_keep_old_value can be deleted.
3682 */
3683 bool m_keep_old_value;
3684
3685 /**
3686 Backup String for table's blob fields.
3687 UPDATE of a virtual field (for index update) requires two values to be
3688 kept at the same time - 'new' and 'old' since SE (InnoDB) doesn't know the
3689 latter. In the case when there was an indexed record, it got deleted and
3690 When INSERT inserts into an index a record that coincides with a
3691 previously deleted one, InnoDB needs to recalculate value that was
3692 deleted in order to properly insert the new one.
3693 When two above overlap, a field have to keep 3 different values at the
3694 same time - 'new', 'old' and 'deleted'.
3695 This backup_value is used by @see my_eval_gcolumn_expr_helper() to save
3696 'new' and provide space for 'deleted' to avoid thrashing the former.
3697 Unlike the old_value, backup_value is allocated once and reused for each
3698 new re-calculation, to avoid excessive [re-]allocations. It's freed at the
3699 end of statement. Since InnoDB consumes calculated values only after all
3700 needed table's virtual fields were calculated, we have to have such backup
3701 buffer for each field.
3702 */
3703 String m_blob_backup;
3704
3705 #ifndef DBUG_OFF
3706 /**
3707 Whether the field uses table's backup value storage. @see
3708 TABLE::m_blob_backup. Used only for debug.
3709 */
3710 bool m_uses_backup{false};
3711 #endif
3712
3713 protected:
3714 /**
3715 Store ptr and length.
3716 */
store_ptr_and_length(const char * from,uint32 length)3717 void store_ptr_and_length(const char *from, uint32 length) {
3718 store_length(length);
3719 memmove(ptr + packlength, &from, sizeof(char *));
3720 }
3721
3722 public:
3723 Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3724 uchar auto_flags_arg, const char *field_name_arg,
3725 TABLE_SHARE *share, uint blob_pack_length, const CHARSET_INFO *cs);
3726
Field_blob(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,const CHARSET_INFO * cs,bool set_packlength)3727 Field_blob(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
3728 const CHARSET_INFO *cs, bool set_packlength)
3729 : Field_longstr(nullptr, len_arg,
3730 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
3731 field_name_arg, cs),
3732 packlength(4),
3733 m_keep_old_value(false) {
3734 set_flag(BLOB_FLAG);
3735 if (set_packlength) {
3736 packlength = len_arg <= 255
3737 ? 1
3738 : len_arg <= 65535 ? 2 : len_arg <= 16777215 ? 3 : 4;
3739 }
3740 }
3741
3742 /// Copy static information and reset dynamic information.
Field_blob(const Field_blob & field)3743 Field_blob(const Field_blob &field)
3744 : Field_longstr(field),
3745 packlength(field.packlength),
3746 value(),
3747 old_value(),
3748 m_keep_old_value(field.m_keep_old_value),
3749 m_blob_backup() {
3750 #ifndef DBUG_OFF
3751 m_uses_backup = field.m_uses_backup;
3752 #endif
3753 }
3754
3755 explicit Field_blob(uint32 packlength_arg);
3756
3757 /* Note that the default copy constructor is used, in clone() */
type()3758 enum_field_types type() const override { return MYSQL_TYPE_BLOB; }
match_collation_to_optimize_range()3759 bool match_collation_to_optimize_range() const override { return true; }
key_type()3760 enum ha_base_keytype key_type() const override {
3761 return binary() ? HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2;
3762 }
3763 type_conversion_status store(const char *to, size_t length,
3764 const CHARSET_INFO *charset) override;
3765 type_conversion_status store(double nr) override;
3766 type_conversion_status store(longlong nr, bool unsigned_val) override;
3767 type_conversion_status store(const Field *from);
3768 double val_real() const override;
3769 longlong val_int() const override;
3770 String *val_str(String *, String *) const override;
3771 my_decimal *val_decimal(my_decimal *) const override;
3772 int cmp_max(const uchar *, const uchar *,
3773 uint max_length) const final override;
cmp(const uchar * a,const uchar * b)3774 int cmp(const uchar *a, const uchar *b) const final override {
3775 return cmp_max(a, b, ~0L);
3776 }
3777 int cmp(const uchar *a, uint32 a_length, const uchar *b,
3778 uint32 b_length) const; // No override.
3779 int cmp_binary(const uchar *a, const uchar *b,
3780 uint32 max_length = ~0L) const override;
3781 int key_cmp(const uchar *, const uchar *) const override;
3782 int key_cmp(const uchar *str, uint length) const override;
key_length()3783 uint32 key_length() const override { return 0; }
3784 size_t make_sort_key(uchar *buff, size_t length) const override;
pack_length()3785 uint32 pack_length() const final override {
3786 return (uint32)(packlength + portable_sizeof_char_ptr);
3787 }
3788
3789 /**
3790 Return the packed length without the pointer size added.
3791
3792 This is used to determine the size of the actual data in the row
3793 buffer.
3794
3795 @returns The length of the raw data itself without the pointer.
3796 */
pack_length_no_ptr()3797 uint32 pack_length_no_ptr() const { return (uint32)(packlength); }
row_pack_length()3798 uint row_pack_length() const final override { return pack_length_no_ptr(); }
max_data_length()3799 uint32 max_data_length() const final override {
3800 return (uint32)(((ulonglong)1 << (packlength * 8)) - 1);
3801 }
get_field_buffer_size()3802 size_t get_field_buffer_size() { return value.alloced_length(); }
3803 void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number);
store_length(uint32 number)3804 inline void store_length(uint32 number) {
3805 store_length(ptr, packlength, number);
3806 }
3807 uint32 data_length(ptrdiff_t row_offset = 0) const final override {
3808 return get_length(row_offset);
3809 }
3810 uint32 get_length(ptrdiff_t row_offset = 0) const;
3811 uint32 get_length(const uchar *ptr, uint packlength) const;
3812 uint32 get_length(const uchar *ptr_arg) const;
3813 /** Get a const pointer to the BLOB data of this field. */
get_blob_data()3814 const uchar *get_blob_data() const { return get_blob_data(ptr + packlength); }
3815 /** Get a non-const pointer to the BLOB data of this field. */
3816 uchar *get_blob_data(ptrdiff_t row_offset = 0) {
3817 // row_offset is only used by NDB
3818 return get_blob_data(ptr + packlength + row_offset);
3819 }
3820 /** Get a const pointer to the BLOB data of this field. */
data_ptr()3821 const uchar *data_ptr() const final override { return get_blob_data(); }
3822
3823 protected:
3824 /**
3825 Get the BLOB data pointer stored at the specified position in the record
3826 buffer.
3827 */
get_blob_data(const uchar * position)3828 static uchar *get_blob_data(const uchar *position) {
3829 uchar *data;
3830 memcpy(&data, position, sizeof(data));
3831 return data;
3832 }
3833
3834 public:
set_ptr(const uchar * length,const uchar * data)3835 void set_ptr(const uchar *length, const uchar *data) {
3836 memcpy(ptr, length, packlength);
3837 memcpy(ptr + packlength, &data, sizeof(char *));
3838 }
set_ptr_offset(ptrdiff_t ptr_diff,uint32 length,const uchar * data)3839 void set_ptr_offset(ptrdiff_t ptr_diff, uint32 length, const uchar *data) {
3840 uchar *ptr_ofs = ptr + ptr_diff;
3841 store_length(ptr_ofs, packlength, length);
3842 memcpy(ptr_ofs + packlength, &data, sizeof(char *));
3843 }
set_ptr(uint32 length,const uchar * data)3844 void set_ptr(uint32 length, const uchar *data) {
3845 set_ptr_offset(0, length, data);
3846 }
3847 size_t get_key_image(uchar *buff, size_t length,
3848 imagetype type) const override;
3849 void set_key_image(const uchar *buff, size_t length) final override;
3850 void sql_type(String &str) const override;
3851 bool copy();
clone(MEM_ROOT * mem_root)3852 Field_blob *clone(MEM_ROOT *mem_root) const override {
3853 DBUG_ASSERT(type() == MYSQL_TYPE_BLOB);
3854 return new (mem_root) Field_blob(*this);
3855 }
3856 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final;
3857 uchar *pack_with_metadata_bytes(uchar *to, const uchar *from,
3858 uint max_length) const final;
3859 const uchar *unpack(uchar *, const uchar *from, uint param_data) final;
3860 uint max_packed_col_length() const final override;
mem_free()3861 void mem_free() final override {
3862 // Free all allocated space
3863 value.mem_free();
3864 old_value.mem_free();
3865 m_blob_backup.mem_free();
3866 }
has_charset()3867 bool has_charset() const override {
3868 return charset() == &my_charset_bin ? false : true;
3869 }
3870 uint32 max_display_length() const final override;
3871 uint32 char_length() const override;
3872 bool copy_blob_value(MEM_ROOT *mem_root);
3873 uint is_equal(const Create_field *new_field) const override;
is_text_key_type()3874 bool is_text_key_type() const final override {
3875 return binary() ? false : true;
3876 }
3877
3878 /**
3879 Mark that the BLOB stored in value should be copied before updating it.
3880
3881 When updating virtual generated columns we need to keep the old
3882 'value' for BLOBs since this can be needed when the storage engine
3883 does the update. During read of the record the old 'value' for the
3884 BLOB is evaluated and stored in 'value'. This function is to be used
3885 to specify that we need to copy this BLOB 'value' into 'old_value'
3886 before we compute the new BLOB 'value'. For more information @see
3887 Field_blob::keep_old_value().
3888 */
set_keep_old_value(bool old_value_flag)3889 void set_keep_old_value(bool old_value_flag) {
3890 /*
3891 We should only need to keep a copy of the blob 'value' in the case
3892 where this is a virtual genarated column (that is indexed).
3893 */
3894 DBUG_ASSERT(is_virtual_gcol());
3895
3896 /*
3897 If set to true, ensure that 'value' is copied to 'old_value' when
3898 keep_old_value() is called.
3899 */
3900 m_keep_old_value = old_value_flag;
3901 }
3902
3903 /**
3904 Save the current BLOB value to avoid that it gets overwritten.
3905
3906 This is used when updating virtual generated columns that are
3907 BLOBs. Some storage engines require that we have both the old and
3908 new BLOB value for virtual generated columns that are indexed in
3909 order for the storage engine to be able to maintain the index. This
3910 function will transfer the buffer storing the current BLOB value
3911 from 'value' to 'old_value'. This avoids that the current BLOB value
3912 is over-written when the new BLOB value is saved into this field.
3913
3914 The reason this requires special handling when updating/deleting
3915 virtual columns of BLOB type is that the BLOB value is not known to
3916 the storage engine. For stored columns, the "old" BLOB value is read
3917 by the storage engine, Field_blob is made to point to the engine's
3918 internal buffer; Field_blob's internal buffer (Field_blob::value)
3919 isn't used and remains available to store the "new" value. For
3920 virtual generated columns, the "old" value is written directly into
3921 Field_blob::value when reading the record to be
3922 updated/deleted. This is done in update_generated_read_fields().
3923 Since, in this case, the "old" value already occupies the place to
3924 store the "new" value, we must call this function before we write
3925 the "new" value into Field_blob::value object so that the "old"
3926 value does not get over-written. The table->record[1] buffer will
3927 have a pointer that points to the memory buffer inside
3928 old_value. The storage engine will use table->record[1] to read the
3929 old value for the BLOB and use table->record[0] to read the new
3930 value.
3931
3932 This function must be called before we store the new BLOB value in
3933 this field object.
3934 */
keep_old_value()3935 void keep_old_value() {
3936 /*
3937 We should only need to keep a copy of the blob value in the case
3938 where this is a virtual genarated column (that is indexed).
3939 */
3940 DBUG_ASSERT(is_virtual_gcol());
3941
3942 // Transfer ownership of the current BLOB value to old_value
3943 if (m_keep_old_value) {
3944 old_value.takeover(value);
3945 m_keep_old_value = false;
3946 }
3947 }
3948
3949 /**
3950 Use to store the blob value into an allocated space.
3951 */
store_in_allocated_space(const char * from,uint32 length)3952 void store_in_allocated_space(const char *from, uint32 length) {
3953 store_ptr_and_length(from, length);
3954 }
3955
3956 /**
3957 Backup data stored in 'value' into the backup_value
3958 @see Field_blob::backup_value
3959
3960 @returns
3961 true if backup fails
3962 false otherwise
3963 */
3964 bool backup_blob_field();
3965
3966 /**
3967 Restore backup value
3968 @see Field_blob::backup_value
3969 */
3970 void restore_blob_backup();
3971
3972 private:
3973 int do_save_field_metadata(uchar *first_byte) const override;
3974 };
3975
3976 class Field_geom final : public Field_blob {
3977 private:
3978 const Nullable<gis::srid_t> m_srid;
3979
3980 type_conversion_status store_internal(const char *from, size_t length,
3981 const CHARSET_INFO *cs) final override;
3982
3983 public:
3984 enum geometry_type geom_type;
3985
Field_geom(uchar * ptr_arg,uchar * null_ptr_arg,uint null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,TABLE_SHARE * share,uint blob_pack_length,enum geometry_type geom_type_arg,Nullable<gis::srid_t> srid)3986 Field_geom(uchar *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
3987 uchar auto_flags_arg, const char *field_name_arg,
3988 TABLE_SHARE *share, uint blob_pack_length,
3989 enum geometry_type geom_type_arg, Nullable<gis::srid_t> srid)
3990 : Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3991 field_name_arg, share, blob_pack_length, &my_charset_bin),
3992 m_srid(srid),
3993 geom_type(geom_type_arg) {}
Field_geom(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,enum geometry_type geom_type_arg,Nullable<gis::srid_t> srid)3994 Field_geom(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
3995 enum geometry_type geom_type_arg, Nullable<gis::srid_t> srid)
3996 : Field_blob(len_arg, is_nullable_arg, field_name_arg, &my_charset_bin,
3997 false),
3998 m_srid(srid),
3999 geom_type(geom_type_arg) {}
key_type()4000 enum ha_base_keytype key_type() const final override {
4001 return HA_KEYTYPE_VARBINARY2;
4002 }
type()4003 enum_field_types type() const final override { return MYSQL_TYPE_GEOMETRY; }
match_collation_to_optimize_range()4004 bool match_collation_to_optimize_range() const final override {
4005 return false;
4006 }
4007 void sql_type(String &str) const final override;
4008 using Field_blob::store;
4009 type_conversion_status store(double nr) final override;
4010 type_conversion_status store(longlong nr, bool unsigned_val) final override;
4011 type_conversion_status store_decimal(const my_decimal *) final override;
4012 type_conversion_status store(const char *from, size_t length,
4013 const CHARSET_INFO *cs) final override;
4014
4015 /**
4016 Non-nullable GEOMETRY types cannot have defaults,
4017 but the underlying blob must still be reset.
4018 */
reset()4019 type_conversion_status reset() final override {
4020 type_conversion_status res = Field_blob::reset();
4021 if (res != TYPE_OK) return res;
4022 return (is_nullable() || table->is_nullable())
4023 ? TYPE_OK
4024 : TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
4025 }
4026
get_geometry_type()4027 geometry_type get_geometry_type() const final override { return geom_type; }
clone(MEM_ROOT * mem_root)4028 Field_geom *clone(MEM_ROOT *mem_root) const final override {
4029 DBUG_ASSERT(type() == MYSQL_TYPE_GEOMETRY);
4030 return new (mem_root) Field_geom(*this);
4031 }
4032 uint is_equal(const Create_field *new_field) const final override;
4033
get_srid()4034 Nullable<gis::srid_t> get_srid() const { return m_srid; }
4035 };
4036
4037 /// A field that stores a JSON value.
4038 class Field_json : public Field_blob {
4039 type_conversion_status unsupported_conversion();
4040 type_conversion_status store_binary(const char *ptr, size_t length);
4041
4042 /**
4043 Diagnostics utility for ER_INVALID_JSON_TEXT.
4044
4045 @param err error message argument for ER_INVALID_JSON_TEXT
4046 @param err_offset location in text at which there is an error
4047 */
invalid_text(const char * err,size_t err_offset)4048 void invalid_text(const char *err, size_t err_offset) const {
4049 String s;
4050 s.append(*table_name);
4051 s.append('.');
4052 s.append(field_name);
4053 my_error(ER_INVALID_JSON_TEXT, MYF(0), err, err_offset, s.c_ptr_safe());
4054 }
4055
4056 public:
Field_json(uchar * ptr_arg,uchar * null_ptr_arg,uint null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,TABLE_SHARE * share,uint blob_pack_length)4057 Field_json(uchar *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
4058 uchar auto_flags_arg, const char *field_name_arg,
4059 TABLE_SHARE *share, uint blob_pack_length)
4060 : Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
4061 field_name_arg, share, blob_pack_length, &my_charset_bin) {}
4062
Field_json(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg)4063 Field_json(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg)
4064 : Field_blob(len_arg, is_nullable_arg, field_name_arg, &my_charset_bin,
4065 false) {}
4066
type()4067 enum_field_types type() const override { return MYSQL_TYPE_JSON; }
4068 void sql_type(String &str) const override;
4069 /**
4070 Return a text charset so that string functions automatically
4071 convert the field value to string and treat it as a non-binary
4072 string.
4073 */
charset()4074 const CHARSET_INFO *charset() const override {
4075 return &my_charset_utf8mb4_bin;
4076 }
4077 /**
4078 Sort should treat the field as binary and not attempt any
4079 conversions.
4080 */
sort_charset()4081 const CHARSET_INFO *sort_charset() const final override {
4082 return field_charset;
4083 }
4084 /**
4085 JSON columns don't have an associated charset. Returning false
4086 here prevents SHOW CREATE TABLE from attaching a CHARACTER SET
4087 clause to the column.
4088 */
has_charset()4089 bool has_charset() const final override { return false; }
4090 type_conversion_status store(const char *to, size_t length,
4091 const CHARSET_INFO *charset) override;
4092 type_conversion_status store(double nr) override;
4093 type_conversion_status store(longlong nr, bool unsigned_val) override;
4094 type_conversion_status store_decimal(const my_decimal *) final override;
4095 type_conversion_status store_json(const Json_wrapper *json);
4096 type_conversion_status store_time(MYSQL_TIME *ltime,
4097 uint8 dec_arg) final override;
4098 type_conversion_status store(const Field_json *field);
4099
4100 bool pack_diff(uchar **to, ulonglong value_options) const final override;
4101 /**
4102 Return the length of this field, taking into consideration that it may be in
4103 partial format.
4104
4105 This is the format used when writing the binary log in row format
4106 and using a partial format according to
4107 @@session.binlog_row_value_options.
4108
4109 @param[in] value_options The value of binlog_row_value options.
4110
4111 @param[out] diff_vector_p If this is not NULL, the pointer it
4112 points to will be set to NULL if the field is to be stored in full
4113 format, or to the Json_diff_vector if the field is to be stored in
4114 partial format.
4115
4116 @return The number of bytes needed when writing to the binlog: the
4117 size of the full format if stored in full format and the size of
4118 the diffs if stored in partial format.
4119 */
4120 longlong get_diff_vector_and_length(
4121 ulonglong value_options,
4122 const Json_diff_vector **diff_vector_p = nullptr) const;
4123 /**
4124 Return true if the before-image and after-image for this field are
4125 equal.
4126 */
4127 bool is_before_image_equal_to_after_image() const;
4128 /**
4129 Read the binary diff from the given buffer, and apply it to this field.
4130
4131 @param[in,out] from Pointer to buffer where the binary diff is stored.
4132 This will be changed to point to the next byte after the field.
4133
4134 @retval false Success
4135 @retval true Error (e.g. failed to apply the diff). The error has
4136 been reported through my_error.
4137 */
4138 bool unpack_diff(const uchar **from);
4139
4140 /**
4141 Retrieve the field's value as a JSON wrapper. It
4142 there is an error, wr is not modified and we return
4143 false, else true.
4144
4145 @param[out] wr the JSON value
4146 @return true if a value is retrieved (or NULL), false if error
4147 */
4148 bool val_json(Json_wrapper *wr) const;
4149
4150 /**
4151 Retrieve the JSON as an int if possible. This requires a JSON scalar
4152 of suitable type.
4153
4154 @returns the JSON value as an int
4155 */
4156 longlong val_int() const final override;
4157
4158 /**
4159 Retrieve the JSON as a double if possible. This requires a JSON scalar
4160 of suitable type.
4161
4162 @returns the JSON value as a double
4163 */
4164 double val_real() const final override;
4165
4166 /**
4167 Retrieve the JSON value stored in this field as text
4168
4169 @param[in,out] buf1 string buffer for converting JSON value to string
4170 @param[in,out] buf2 unused
4171 */
4172 String *val_str(String *buf1, String *buf2) const final override;
4173 my_decimal *val_decimal(my_decimal *m) const final override;
4174 bool get_time(MYSQL_TIME *ltime) const final override;
4175 bool get_date(MYSQL_TIME *ltime,
4176 my_time_flags_t fuzzydate) const final override;
4177 Field_json *clone(MEM_ROOT *mem_root) const override;
4178 uint is_equal(const Create_field *new_field) const final override;
cast_to_int_type()4179 Item_result cast_to_int_type() const final override { return INT_RESULT; }
4180 int cmp_binary(const uchar *a, const uchar *b,
4181 uint32 max_length = ~0L) const final override;
4182 size_t make_sort_key(uchar *to, size_t length) const override;
4183
4184 /**
4185 Make a hash key that can be used by sql_executor.cc/unique_hash
4186 in order to support SELECT DISTINCT
4187
4188 @param[in] hash_val An initial hash value.
4189 */
4190 ulonglong make_hash_key(ulonglong hash_val) const;
4191
4192 /**
4193 Get a read-only pointer to the binary representation of the JSON document
4194 in this field.
4195
4196 @param row_offset Field's data offset
4197 */
4198 const char *get_binary(ptrdiff_t row_offset = 0) const;
4199 };
4200
4201 /**
4202 Field that stores array of values of the same type.
4203
4204 This Field class is used together with Item_func_array_cast class
4205 (CAST( .. AS .. ARRAY) function) in implementation of multi-valued index.
4206 Effectively it's a JSON field that contains a single JSON array. When a
4207 JSON value is stored, it's checked to be either a scalar, or an array.
4208 All source values are converted using the internal conversion field and
4209 stored as an array. Field_typed_array ensures that all values stored
4210 in the array have the same type and precision - the one specified by user.
4211 This way InnoDB doesn't have to do the conversion on its own and can easily
4212 index them.
4213
4214 The Field_typed_array always reports type of its element and from this
4215 point of view it's undistinguishable from regular field having the same
4216 type. Due to that, fields are differentiated by is_array() property.
4217 Field_typed_array returns true, all other fields - false.
4218
4219 For conversion and index applicability tests, Field_typed_array employs a
4220 conversion field, which is a regular Field class of array's element type.
4221 It's stored in the m_conv_field. All Field_typed_array::store_*() methods
4222 store values to the conversion field. Conversion field and typed array
4223 field are sharing same field_index, to allow correct read/write_set
4224 checks. So the field always have to be marked for read in order to allow
4225 read of conversions' results.
4226
4227 @see Item_func_array_cast
4228 */
4229
4230 class Field_typed_array final : public Field_json {
4231 /// Conversion item_field
4232 Item_field *m_conv_item{nullptr};
4233 /// The array element's real type.
4234 enum_field_types m_elt_type;
4235 /// Element's decimals
4236 uint m_elt_decimals;
4237 /// Element's charset
4238 const CHARSET_INFO *m_elt_charset;
4239 const bool unsigned_flag;
4240
4241 public:
4242 /**
4243 Constructs a Field_typed_array that is a copy of another Field_typed_array.
4244 @param other the other Field_typed_array object
4245 */
4246 Field_typed_array(const Field_typed_array &other);
4247 /**
4248 Constructs a Field_typed_array object.
4249 */
4250 Field_typed_array(enum_field_types elt_type, bool elt_is_unsigned,
4251 size_t elt_length, uint elt_decimals, uchar *ptr_arg,
4252 uchar *null_ptr_arg, uint null_bit_arg,
4253 uchar auto_flags_arg, const char *field_name_arg,
4254 TABLE_SHARE *share, uint blob_pack_length,
4255 const CHARSET_INFO *cs);
char_length()4256 uint32 char_length() const override {
4257 return field_length / charset()->mbmaxlen;
4258 }
4259 void init(TABLE *table_arg) override;
type()4260 enum_field_types type() const override {
4261 return real_type_to_type(m_elt_type);
4262 }
real_type()4263 enum_field_types real_type() const override { return m_elt_type; }
binlog_type()4264 enum_field_types binlog_type() const override {
4265 return MYSQL_TYPE_TYPED_ARRAY;
4266 }
4267 uint32 key_length() const override;
4268 Field_typed_array *clone(MEM_ROOT *mem_root) const override;
is_unsigned()4269 bool is_unsigned() const override final { return unsigned_flag; }
is_array()4270 bool is_array() const override { return true; }
4271 Item_result result_type() const override;
decimals()4272 uint decimals() const override { return m_elt_decimals; }
binary()4273 bool binary() const override {
4274 return (m_elt_type != MYSQL_TYPE_VARCHAR ||
4275 m_elt_charset == &my_charset_bin);
4276 }
charset()4277 const CHARSET_INFO *charset() const override { return m_elt_charset; }
4278 type_conversion_status store(const char *to, size_t length,
4279 const CHARSET_INFO *charset) override;
4280 type_conversion_status store(double nr) override;
4281 type_conversion_status store(longlong nr, bool unsigned_val) override;
4282 /**
4283 Store a value as an array.
4284 @param data the value to store as an array
4285 @param array scratch space for building the array to store
4286 @return the status of the operation
4287 */
4288 type_conversion_status store_array(const Json_wrapper *data,
4289 Json_array *array);
4290 size_t get_key_image(uchar *buff, size_t length,
4291 imagetype type) const override;
4292 Field *new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr,
4293 uchar *, uint) const override;
4294 /**
4295 These methods are used by handler to prevent returning a row past the
4296 end_range during range access. Since there's no order defined for sorting
4297 set of arrays, always return -1 here, allowing all records fetched from
4298 SE to be returned to server. They will be filtered by WHERE condition later.
4299 */
key_cmp(const uchar *,const uchar *)4300 int key_cmp(const uchar *, const uchar *) const override { return -1; }
key_cmp(const uchar *,uint)4301 int key_cmp(const uchar *, uint) const override { return -1; }
4302 /**
4303 Multi-valued index always works only as a pre-filter for actual
4304 condition check, and the latter always use binary collation, so no point
4305 to match collations in optimizer.
4306 */
match_collation_to_optimize_range()4307 bool match_collation_to_optimize_range() const override { return false; }
4308
4309 /**
4310 Convert arbitrary JSON value to the array's type using the conversion field.
4311 If conversion fails and it's not a coercion test (no_error= false) then an
4312 error is thrown. The converted value is guaranteed to match the field's
4313 type and can be indexed by SE without any additional handling.
4314
4315 @param[in] wr Source data
4316 @param[in] no_error Whether an error should be thrown if value can't be
4317 coerced. Error should be thrown when inserting data
4318 into the index, and shouldn't be thrown when the range
4319 optimizer tests index applicability.
4320 @param[out] coerced The converted value. Can be nullptr if no_error is
4321 true.
4322
4323 @returns
4324 true conversion failed
4325 false conversion succeeded
4326 */
4327 bool coerce_json_value(const Json_wrapper *wr, bool no_error,
4328 Json_wrapper *coerced) const;
4329
4330 /**
4331 Get name of the index defined over this field.
4332
4333 Since typed array fields can be created only as an underlying GC field of
4334 a multi-valued functional index, there's always only one index defined
4335 over the field.
4336
4337 @returns
4338 name of the index defined over the field.
4339 */
4340 const char *get_index_name() const;
get_length_bytes()4341 uint32 get_length_bytes() const override {
4342 DBUG_ASSERT(m_elt_type == MYSQL_TYPE_VARCHAR);
4343 return field_length > 255 ? 2 : 1;
4344 }
make_sort_key(uchar * to,size_t max_len)4345 size_t make_sort_key(uchar *to, size_t max_len) const override {
4346 // Not supported yet
4347 DBUG_ASSERT(false);
4348 // Dummy
4349 return Field_json::make_sort_key(to, max_len);
4350 }
4351 /**
4352 Create sort key out of given JSON value according to array's element type
4353
4354 @param wr JSON value to create sort key from
4355 @param to buffer to create sort key in
4356 @param length buffer's length
4357
4358 @returns
4359 actual sort key length
4360 */
4361 size_t make_sort_key(Json_wrapper *wr, uchar *to, size_t length) const;
4362 /**
4363 Save the field metadata for typed array fields.
4364
4365 Saved metadata contains element type (1 byte) and up to 3 bytes of
4366 metadata - the same as each respective Field class saves
4367 (e.g Field_new_decimal for DECIMAL type). The only difference is that
4368 for VARCHAR type length is stored in 3 bytes. This allows to store longer
4369 strings, as its supported by JSON storage.
4370
4371 @param metadata_ptr First byte of field metadata
4372
4373 @returns number of bytes written to metadata_ptr
4374 */
4375 int do_save_field_metadata(uchar *metadata_ptr) const override;
pack_length_from_metadata(uint)4376 uint pack_length_from_metadata(uint) const override {
4377 return pack_length_no_ptr();
4378 }
4379 void sql_type(String &str) const final override;
4380 void make_send_field(Send_field *field) const final;
4381 void set_field_index(uint16 f_index) final override;
4382 };
4383
4384 class Field_enum : public Field_str {
4385 protected:
4386 uint packlength;
4387
4388 public:
4389 TYPELIB *typelib;
Field_enum(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint packlength_arg,TYPELIB * typelib_arg,const CHARSET_INFO * charset_arg)4390 Field_enum(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
4391 uchar null_bit_arg, uchar auto_flags_arg,
4392 const char *field_name_arg, uint packlength_arg,
4393 TYPELIB *typelib_arg, const CHARSET_INFO *charset_arg)
4394 : Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
4395 field_name_arg, charset_arg),
4396 packlength(packlength_arg),
4397 typelib(typelib_arg) {
4398 set_flag(ENUM_FLAG);
4399 }
Field_enum(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,uint packlength_arg,TYPELIB * typelib_arg,const CHARSET_INFO * charset_arg)4400 Field_enum(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
4401 uint packlength_arg, TYPELIB *typelib_arg,
4402 const CHARSET_INFO *charset_arg)
4403 : Field_enum(nullptr, len_arg,
4404 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
4405 field_name_arg, packlength_arg, typelib_arg, charset_arg) {}
4406 Field *new_field(MEM_ROOT *root, TABLE *new_table) const final override;
type()4407 enum_field_types type() const final override { return MYSQL_TYPE_STRING; }
match_collation_to_optimize_range()4408 bool match_collation_to_optimize_range() const final override {
4409 return false;
4410 }
cmp_type()4411 enum Item_result cmp_type() const final override { return INT_RESULT; }
cast_to_int_type()4412 enum Item_result cast_to_int_type() const final override {
4413 return INT_RESULT;
4414 }
4415 enum ha_base_keytype key_type() const final override;
4416 type_conversion_status store(const char *to, size_t length,
4417 const CHARSET_INFO *charset) override;
4418 type_conversion_status store(double nr) override;
4419 type_conversion_status store(longlong nr, bool unsigned_val) override;
4420 double val_real() const final override;
4421 my_decimal *val_decimal(my_decimal *decimal_value) const final override;
4422 longlong val_int() const final override;
4423 String *val_str(String *, String *) const override;
4424 int cmp(const uchar *, const uchar *) const final override;
4425 size_t make_sort_key(uchar *buff, size_t length) const final override;
pack_length()4426 uint32 pack_length() const final override { return (uint32)packlength; }
4427 void store_type(ulonglong value);
4428 void sql_type(String &str) const override;
real_type()4429 enum_field_types real_type() const override { return MYSQL_TYPE_ENUM; }
pack_length_from_metadata(uint field_metadata)4430 uint pack_length_from_metadata(uint field_metadata) const final override {
4431 return (field_metadata & 0x00ff);
4432 }
row_pack_length()4433 uint row_pack_length() const final override { return pack_length(); }
zero_pack()4434 bool zero_pack() const override { return false; }
optimize_range(uint,uint)4435 bool optimize_range(uint, uint) const final override { return false; }
4436 bool eq_def(const Field *field) const final override;
has_charset()4437 bool has_charset() const override { return true; }
4438 /* enum and set are sorted as integers */
sort_charset()4439 const CHARSET_INFO *sort_charset() const final override {
4440 return &my_charset_bin;
4441 }
clone(MEM_ROOT * mem_root)4442 Field_enum *clone(MEM_ROOT *mem_root) const override {
4443 DBUG_ASSERT(real_type() == MYSQL_TYPE_ENUM);
4444 return new (mem_root) Field_enum(*this);
4445 }
4446 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final;
4447 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final;
4448
4449 private:
4450 int do_save_field_metadata(uchar *first_byte) const final override;
4451 uint is_equal(const Create_field *new_field) const final override;
4452 };
4453
4454 class Field_set final : public Field_enum {
4455 public:
Field_set(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar auto_flags_arg,const char * field_name_arg,uint32 packlength_arg,TYPELIB * typelib_arg,const CHARSET_INFO * charset_arg)4456 Field_set(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
4457 uchar null_bit_arg, uchar auto_flags_arg,
4458 const char *field_name_arg, uint32 packlength_arg,
4459 TYPELIB *typelib_arg, const CHARSET_INFO *charset_arg)
4460 : Field_enum(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
4461 field_name_arg, packlength_arg, typelib_arg, charset_arg),
4462 empty_set_string("", 0, charset_arg) {
4463 clear_flag(ENUM_FLAG);
4464 set_flag(SET_FLAG);
4465 }
Field_set(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg,uint32 packlength_arg,TYPELIB * typelib_arg,const CHARSET_INFO * charset_arg)4466 Field_set(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
4467 uint32 packlength_arg, TYPELIB *typelib_arg,
4468 const CHARSET_INFO *charset_arg)
4469 : Field_set(nullptr, len_arg,
4470 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
4471 field_name_arg, packlength_arg, typelib_arg, charset_arg) {}
4472 type_conversion_status store(const char *to, size_t length,
4473 const CHARSET_INFO *charset) final override;
store(double nr)4474 type_conversion_status store(double nr) final override {
4475 return Field_set::store((longlong)nr, false);
4476 }
4477 type_conversion_status store(longlong nr, bool unsigned_val) final override;
zero_pack()4478 bool zero_pack() const final override { return true; }
4479 String *val_str(String *, String *) const final override;
4480 void sql_type(String &str) const final override;
real_type()4481 enum_field_types real_type() const final override { return MYSQL_TYPE_SET; }
has_charset()4482 bool has_charset() const final override { return true; }
clone(MEM_ROOT * mem_root)4483 Field_set *clone(MEM_ROOT *mem_root) const final override {
4484 DBUG_ASSERT(real_type() == MYSQL_TYPE_SET);
4485 return new (mem_root) Field_set(*this);
4486 }
4487
4488 private:
4489 const String empty_set_string;
4490 };
4491
4492 /*
4493 Note:
4494 To use Field_bit::cmp_binary() you need to copy the bits stored in
4495 the beginning of the record (the NULL bytes) to each memory you
4496 want to compare (where the arguments point).
4497
4498 This is the reason:
4499 - Field_bit::cmp_binary() is only implemented in the base class
4500 (Field::cmp_binary()).
4501 - Field::cmp_binary() currenly use pack_length() to calculate how
4502 long the data is.
4503 - pack_length() includes size of the bits stored in the NULL bytes
4504 of the record.
4505 */
4506 class Field_bit : public Field {
4507 public:
4508 uchar *bit_ptr; // position in record where 'uneven' bits store
4509 uchar bit_ofs; // offset to 'uneven' high bits
4510 uint bit_len; // number of 'uneven' high bits
4511 uint bytes_in_rec;
4512 Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
4513 uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
4514 uchar auto_flags_arg, const char *field_name_arg);
type()4515 enum_field_types type() const final override { return MYSQL_TYPE_BIT; }
key_type()4516 enum ha_base_keytype key_type() const override { return HA_KEYTYPE_BIT; }
max_display_length()4517 uint32 max_display_length() const final override { return field_length; }
result_type()4518 Item_result result_type() const final override { return INT_RESULT; }
4519 type_conversion_status reset() final override;
4520 type_conversion_status store(const char *to, size_t length,
4521 const CHARSET_INFO *charset) override;
4522 type_conversion_status store(double nr) final override;
4523 type_conversion_status store(longlong nr, bool unsigned_val) final override;
4524 type_conversion_status store_decimal(const my_decimal *) final override;
4525 double val_real() const final override;
4526 longlong val_int() const final override;
4527 String *val_str(String *, String *) const final override;
str_needs_quotes()4528 bool str_needs_quotes() const final override { return true; }
4529 my_decimal *val_decimal(my_decimal *) const final override;
cmp(const uchar * a,const uchar * b)4530 int cmp(const uchar *a, const uchar *b) const final override {
4531 DBUG_ASSERT(ptr == a || ptr == b);
4532 const uint cmp_len = bytes_in_rec + (bit_len != 0 ? 1 : 0);
4533 if (ptr == a)
4534 return Field_bit::key_cmp(b, cmp_len);
4535 else
4536 return -Field_bit::key_cmp(a, cmp_len);
4537 }
cmp_binary_offset(ptrdiff_t row_offset)4538 int cmp_binary_offset(ptrdiff_t row_offset) const final override {
4539 return cmp_offset(row_offset);
4540 }
4541 int cmp_max(const uchar *a, const uchar *b,
4542 uint max_length) const final override;
key_cmp(const uchar * a,const uchar * b)4543 int key_cmp(const uchar *a, const uchar *b) const final override {
4544 return cmp_binary(a, b);
4545 }
4546 int key_cmp(const uchar *str, uint length) const final override;
4547 int cmp_offset(ptrdiff_t row_offset) const final override;
get_image(uchar * buff,size_t length,const CHARSET_INFO *)4548 void get_image(uchar *buff, size_t length,
4549 const CHARSET_INFO *) const final override {
4550 get_key_image(buff, length, itRAW);
4551 }
set_image(const uchar * buff,size_t length,const CHARSET_INFO * cs)4552 void set_image(const uchar *buff, size_t length,
4553 const CHARSET_INFO *cs) final override {
4554 Field_bit::store(pointer_cast<const char *>(buff), length, cs);
4555 }
4556 size_t get_key_image(uchar *buff, size_t length,
4557 imagetype type) const final override;
set_key_image(const uchar * buff,size_t length)4558 void set_key_image(const uchar *buff, size_t length) final override {
4559 Field_bit::store(pointer_cast<const char *>(buff), length, &my_charset_bin);
4560 }
make_sort_key(uchar * buff,size_t length)4561 size_t make_sort_key(uchar *buff, size_t length) const final override {
4562 get_key_image(buff, length, itRAW);
4563 return length;
4564 }
pack_length()4565 uint32 pack_length() const final override {
4566 return (uint32)(field_length + 7) / 8;
4567 }
pack_length_in_rec()4568 uint32 pack_length_in_rec() const final override { return bytes_in_rec; }
4569 uint pack_length_from_metadata(uint field_metadata) const final override;
row_pack_length()4570 uint row_pack_length() const final override {
4571 return (bytes_in_rec + ((bit_len > 0) ? 1 : 0));
4572 }
4573 bool compatible_field_size(uint metadata, Relay_log_info *, uint16 mflags,
4574 int *order_var) const final override;
4575 void sql_type(String &str) const override;
4576 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final;
4577 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final;
4578 void set_default() final override;
4579
4580 Field *new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr,
4581 uchar *new_null_ptr,
4582 uint new_null_bit) const final override;
set_bit_ptr(uchar * bit_ptr_arg,uchar bit_ofs_arg)4583 void set_bit_ptr(uchar *bit_ptr_arg, uchar bit_ofs_arg) {
4584 bit_ptr = bit_ptr_arg;
4585 bit_ofs = bit_ofs_arg;
4586 }
eq(const Field * field)4587 bool eq(const Field *field) const final override {
4588 return (Field::eq(field) &&
4589 bit_ptr == down_cast<const Field_bit *>(field)->bit_ptr &&
4590 bit_ofs == down_cast<const Field_bit *>(field)->bit_ofs);
4591 }
4592 uint is_equal(const Create_field *new_field) const final override;
move_field_offset(ptrdiff_t ptr_diff)4593 void move_field_offset(ptrdiff_t ptr_diff) final override {
4594 Field::move_field_offset(ptr_diff);
4595 if (bit_ptr != nullptr) bit_ptr += ptr_diff;
4596 }
4597 void hash(ulong *nr, ulong *nr2) const final override;
clone(MEM_ROOT * mem_root)4598 Field_bit *clone(MEM_ROOT *mem_root) const override {
4599 DBUG_ASSERT(type() == MYSQL_TYPE_BIT);
4600 return new (mem_root) Field_bit(*this);
4601 }
4602
4603 private:
4604 int do_save_field_metadata(uchar *first_byte) const final override;
4605 };
4606
4607 /**
4608 BIT field represented as chars for non-MyISAM tables.
4609
4610 @todo The inheritance relationship is backwards since Field_bit is
4611 an extended version of Field_bit_as_char and not the other way
4612 around. Hence, we should refactor it to fix the hierarchy order.
4613 */
4614 class Field_bit_as_char final : public Field_bit {
4615 public:
4616 Field_bit_as_char(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
4617 uchar null_bit_arg, uchar auto_flags_arg,
4618 const char *field_name_arg);
Field_bit_as_char(uint32 len_arg,bool is_nullable_arg,const char * field_name_arg)4619 Field_bit_as_char(uint32 len_arg, bool is_nullable_arg,
4620 const char *field_name_arg)
4621 : Field_bit_as_char(nullptr, len_arg,
4622 is_nullable_arg ? &dummy_null_buffer : nullptr, 0,
4623 NONE, field_name_arg) {}
key_type()4624 enum ha_base_keytype key_type() const final override {
4625 return HA_KEYTYPE_BINARY;
4626 }
4627 type_conversion_status store(const char *to, size_t length,
4628 const CHARSET_INFO *charset) final override;
4629 // Inherit the store() overloads that have not been overridden.
4630 using Field_bit::store;
4631 void sql_type(String &str) const final override;
clone(MEM_ROOT * mem_root)4632 Field_bit_as_char *clone(MEM_ROOT *mem_root) const final override {
4633 return new (mem_root) Field_bit_as_char(*this);
4634 }
4635 };
4636
4637 /// This function should only be called from legacy code.
4638 Field *make_field(MEM_ROOT *mem_root_arg, TABLE_SHARE *share, uchar *ptr,
4639 size_t field_length, uchar *null_pos, uchar null_bit,
4640 enum_field_types field_type,
4641 const CHARSET_INFO *field_charset,
4642 Field::geometry_type geom_type, uchar auto_flags,
4643 TYPELIB *interval, const char *field_name, bool is_nullable,
4644 bool is_zerofill, bool is_unsigned, uint decimals,
4645 bool treat_bit_as_char, uint pack_length_override,
4646 Nullable<gis::srid_t> srid, bool is_array);
4647
4648 /**
4649 Instantiates a Field object with the given name and record buffer values.
4650 @param create_field The column meta data.
4651 @param share The table share object.
4652
4653 @param field_name Create_field::field_name is overridden with this value
4654 when instantiating the Field object.
4655 @param field_length Create_field::length is overridden with this value
4656 when instantiating the Field object.
4657
4658 @param ptr The address of the data bytes.
4659 @param null_pos The address of the null bytes.
4660 @param null_bit The position of the column's null bit within the row's null
4661 bytes.
4662 */
4663 Field *make_field(const Create_field &create_field, TABLE_SHARE *share,
4664 const char *field_name, size_t field_length, uchar *ptr,
4665 uchar *null_pos, size_t null_bit);
4666
4667 /**
4668 Instantiates a Field object with the given record buffer values.
4669 @param create_field The column meta data.
4670 @param share The table share object.
4671 @param ptr The start of the record buffer.
4672 @param null_pos The address of the null bytes.
4673
4674 @param null_bit The position of the column's null bit within the row's null
4675 bytes.
4676 */
4677 Field *make_field(const Create_field &create_field, TABLE_SHARE *share,
4678 uchar *ptr, uchar *null_pos, size_t null_bit);
4679
4680 /**
4681 Instantiates a Field object without a record buffer.
4682 @param create_field The column meta data.
4683 @param share The table share object.
4684 */
4685 Field *make_field(const Create_field &create_field, TABLE_SHARE *share);
4686
4687 /*
4688 A class for sending info to the client
4689 */
4690
4691 class Send_field {
4692 public:
4693 const char *db_name;
4694 const char *table_name, *org_table_name;
4695 const char *col_name, *org_col_name;
4696 ulong length;
4697 uint charsetnr, flags, decimals;
4698 enum_field_types type;
4699 /*
4700 true <=> source item is an Item_field. Needed to workaround lack of
4701 architecture in legacy Protocol_text implementation. Needed only for
4702 Protocol_classic and descendants.
4703 */
4704 bool field;
Send_field()4705 Send_field() {}
4706 };
4707
4708 /**
4709 Constitutes a mapping from columns of tables in the from clause to
4710 aggregated columns. Typically, this means that they represent the mapping
4711 between columns of temporary tables used for aggregation, but not
4712 always. They are also used for aggregation that can be executed "on the
4713 fly" without a temporary table.
4714 */
4715
4716 class Copy_field {
4717 /**
4718 Convenience definition of a copy function returned by
4719 get_copy_func. The parameters are:
4720 Copy_field* Instance of this class. Used for accessing 'tmp' and
4721 calling invoke_do_copy2().
4722 const Field* Field copying from.
4723 Field* Field copying to.
4724 Note that 'from' is 'm_to_field' if invoke_do_copy()
4725 is called with 'reverse' = true.
4726 */
4727 using Copy_func = void(Copy_field *, const Field *, Field *);
4728 Copy_func *get_copy_func(bool save);
4729
4730 public:
4731 String tmp; // For items
4732
4733 Copy_field() = default;
4734
Copy_field(Field * to,Field * from,bool save)4735 Copy_field(Field *to, Field *from, bool save) : Copy_field() {
4736 set(to, from, save);
4737 }
4738
4739 Copy_field(MEM_ROOT *mem_root, Item_field *item);
4740
4741 void set(Field *to, Field *from, bool save); // Field to field
4742
4743 private:
4744 void (*m_do_copy)(Copy_field *, const Field *, Field *);
4745 void (*m_do_copy2)(Copy_field *, const Field *,
4746 Field *); // Used to handle null values
4747
4748 Field *m_from_field{nullptr};
4749 Field *m_to_field{nullptr};
4750
4751 public:
4752 void invoke_do_copy(bool reverse = false);
4753 void invoke_do_copy2(const Field *from_field, Field *to_field);
4754
from_field()4755 Field *from_field() { return m_from_field; }
4756
to_field()4757 Field *to_field() { return m_to_field; }
4758 };
4759
4760 enum_field_types get_blob_type_from_length(size_t length);
4761 size_t calc_pack_length(enum_field_types type, size_t length);
4762
4763 /**
4764 Calculate the length of the in-memory representation of the column from
4765 information which can be retrieved from dd::Column or Ha_fk_column_type
4766 describing it.
4767
4768 This function calculates the amount of memory necessary to store values
4769 in the record buffer. It is used in cases when we want to calculate
4770 this value from the description of column in the form compatible with
4771 dd::Column without constructing full-blown Field object.
4772
4773 @note The implementation is based on Create_field::init() and
4774 Create_field::create_length_to_internal_length().
4775
4776 @param type Column DD type.
4777 @param char_length Column length as stored in DD.
4778 @param elements_count Number of elements in column of ENUM/SET type.
4779 @param treat_bit_as_char Indicates whether this BIT column is represented
4780 as char column internally.
4781 @param numeric_scale Column numeric scale as stored in DD.
4782 @param is_unsigned Column unsignedness.
4783 */
4784
4785 size_t calc_pack_length(dd::enum_column_types type, size_t char_length,
4786 size_t elements_count, bool treat_bit_as_char,
4787 uint numeric_scale, bool is_unsigned);
4788
4789 uint32 calc_key_length(enum_field_types sql_type, uint32 length,
4790 uint32 decimals, bool is_unsigned, uint32 elements);
4791 type_conversion_status set_field_to_null(Field *field);
4792 type_conversion_status set_field_to_null_with_conversions(Field *field,
4793 bool no_conversions);
4794 type_conversion_status store_internal_with_error_check(Field_new_decimal *field,
4795 int conversion_err,
4796 my_decimal *value);
4797
4798 /**
4799 Generate a Create_field, based on an Item.
4800
4801 This function will generate a Create_field based on an existing Item. This
4802 is used for multiple purposes, including CREATE TABLE AS SELECT and creating
4803 hidden generated columns for functional indexes.
4804
4805 @param thd Thread handler
4806 @param item The Item to generate a Create_field from
4807 @param tmp_table A temporary TABLE object that is used for holding Field
4808 objects that are created
4809
4810 @returns A Create_field allocated on the THDs MEM_ROOT.
4811 */
4812 Create_field *generate_create_field(THD *thd, Item *item, TABLE *tmp_table);
4813
is_blob(enum_field_types sql_type)4814 inline bool is_blob(enum_field_types sql_type) {
4815 return (sql_type == MYSQL_TYPE_BLOB || sql_type == MYSQL_TYPE_MEDIUM_BLOB ||
4816 sql_type == MYSQL_TYPE_TINY_BLOB || sql_type == MYSQL_TYPE_LONG_BLOB);
4817 }
4818
4819 /**
4820 @returns the expression if the input field is a hidden generated column that
4821 represents a functional key part. If not, return the field name. In case of
4822 a functional index; the expression is allocated on the THD's MEM_ROOT.
4823 */
4824 const char *get_field_name_or_expression(THD *thd, const Field *field);
4825
4826 /**
4827 Perform per item-type checks to determine if the expression is allowed for
4828 a generated column, default value expression, a functional index or a check
4829 constraint. Note that validation of the specific function is done later in
4830 procedures open_table_from_share and fix_value_generators_fields.
4831
4832 @param expression the expression to check for validity
4833 @param name used for error reporting
4834 @param source Source of value generator(a generated column, a
4835 regular column with generated default value or
4836 a check constraint).
4837 @return false if ok, true otherwise
4838 */
4839 bool pre_validate_value_generator_expr(Item *expression, const char *name,
4840 Value_generator_source source);
4841 #endif /* FIELD_INCLUDED */
4842