MySQL 8.1.0
Source Code Documentation
field.h
Go to the documentation of this file.
1#ifndef FIELD_INCLUDED
2#define FIELD_INCLUDED
3
4/* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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 <assert.h>
27#include <limits.h>
28#include <stddef.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <sys/types.h>
33
34#include <algorithm>
35#include <optional>
36
37#include "decimal.h" // E_DEC_OOM
38#include "field_types.h" // enum_field_types
39#include "lex_string.h"
40#include "libbinlogevents/export/binary_log_funcs.h" // my_time_binary_length
41#include "my_alloc.h"
42#include "my_base.h" // ha_storage_media
43#include "my_bitmap.h"
44#include "my_dbug.h"
45#include "my_double2ulonglong.h"
46#include "my_inttypes.h"
47#include "my_sys.h"
48#include "my_time.h" // MYSQL_TIME_NOTE_TRUNCATED
49#include "mysql/strings/dtoa.h"
52#include "mysql_com.h"
53#include "mysql_time.h"
54#include "mysqld_error.h" // ER_*
55#include "sql/dd/types/column.h"
57#include "sql/gis/srid.h"
58#include "sql/sql_bitmap.h"
59#include "sql/sql_const.h"
60#include "sql/sql_error.h" // Sql_condition
61#include "sql/table.h"
62#include "sql_string.h" // String
63#include "template_utils.h"
64
65class Create_field;
66class Field;
67class Field_bit;
69class Field_blob;
70class Field_datetime;
71class Field_decimal;
72class Field_double;
73class Field_enum;
74class Field_float;
75class Field_json;
76class Field_long;
77class Field_longlong;
78class Field_medium;
80class Field_newdate;
81class Field_num;
82class Field_real;
83class Field_set;
84class Field_short;
85class Field_str;
86class Field_string;
87class Field_temporal;
91class Field_time;
93class Field_timef;
94class Field_timestamp;
95class Field_tiny;
96class Field_varstring;
97class Field_year;
98class Item;
99class Item_field;
100class Json_array;
101class Json_diff_vector;
102class Json_wrapper;
103class KEY;
104class Protocol;
105class Relay_log_info;
106class Send_field;
107class THD;
108class Time_zone;
109class my_decimal;
110struct TYPELIB;
111struct timeval;
112
113/*
114 Inside an in-memory data record, memory pointers to pieces of the
115 record (like BLOBs) are stored in their native byte order and in
116 this amount of bytes.
117*/
118#define portable_sizeof_char_ptr 8
119
120/*
121
122Field class hierarchy
123
124
125Field (abstract)
126|
127+--Field_bit
128| +--Field_bit_as_char
129|
130+--Field_num (abstract)
131| | +--Field_real (abstract)
132| | +--Field_decimal
133| | +--Field_float
134| | +--Field_double
135| |
136| +--Field_new_decimal
137| +--Field_short
138| +--Field_medium
139| +--Field_long
140| +--Field_longlong
141| +--Field_tiny
142| +--Field_year
143|
144+--Field_str (abstract)
145| +--Field_longstr
146| | +--Field_string
147| | +--Field_varstring
148| | +--Field_blob
149| | +--Field_geom
150| | +--Field_json
151| | +--Field_typed_array
152| |
153| +--Field_null
154| +--Field_enum
155| +--Field_set
156|
157+--Field_temporal (abstract)
158 +--Field_time_common (abstract)
159 | +--Field_time
160 | +--Field_timef
161 |
162 +--Field_temporal_with_date (abstract)
163 +--Field_newdate
164 +--Field_temporal_with_date_and_time (abstract)
165 +--Field_timestamp
166 +--Field_datetime
167 +--Field_temporal_with_date_and_timef (abstract)
168 +--Field_timestampf
169 +--Field_datetimef
170*/
171
177
178/// For use @see DTCollation::aggregate()
188
189/* Specifies data storage format for individual columns */
191 COLUMN_FORMAT_TYPE_DEFAULT = 0, /* Not specified (use engine default) */
192 COLUMN_FORMAT_TYPE_FIXED = 1, /* FIXED format */
193 COLUMN_FORMAT_TYPE_DYNAMIC = 2 /* DYNAMIC format */
195
196/**
197 Status when storing a value in a field or converting from one
198 datatype to another. The values should be listed in order of
199 increasing seriousness so that if two type_conversion_status
200 variables are compared, the bigger one is most serious.
201*/
203 /// Storage/conversion went fine.
205 /**
206 A minor problem when converting between temporal values, e.g.
207 if datetime is converted to date the time information is lost.
208 */
210 /**
211 Value was stored, but something was cut. What was cut is
212 considered insignificant enough to only issue a note. Example:
213 trying to store a number with 5 decimal places into a field that
214 can only store 3 decimals. The number rounded to 3 decimal places
215 should be stored. Another example: storing the string "foo " into
216 a VARCHAR(3). The string "foo" is stored in this case, so only
217 whitespace is cut.
218 */
220 /**
221 Value outside min/max limit of datatype. The min/max value is
222 stored by Field::store() instead (if applicable)
223 */
225 /**
226 Value was stored, but something was cut. What was cut is
227 considered significant enough to issue a warning. Example: storing
228 the string "foo" into a VARCHAR(2). The string "fo" is stored in
229 this case. Another example: storing the string "2010-01-01foo"
230 into a DATE. The garbage in the end of the string is cut in this
231 case.
232 */
234 /**
235 Value has invalid string data. When present in a predicate with
236 equality operator, range optimizer returns an impossible where.
237 */
239 /// Trying to store NULL in a NOT NULL field.
241 /**
242 Store/convert incompatible values, like converting "foo" to a
243 date.
244 */
246 /// Out of memory
249
250/*
251 Some defines for exit codes for ::is_equal class functions.
252*/
253#define IS_EQUAL_NO 0
254#define IS_EQUAL_YES 1
255#define IS_EQUAL_PACK_LENGTH 2
256
257#define my_charset_numeric my_charset_latin1
258#define MY_REPERTOIRE_NUMERIC MY_REPERTOIRE_ASCII
259
260/**
261 Check if one can copy from “from” to “to” with a simple memcpy(), with
262 pack_length() as the length. This is the case if the types of the two fields
263 are the same and we don't have special copying rules for the type
264 (e.g., blobs, which require allocation, or time functions that require
265 checking for special SQL modes).
266
267 You should never call this with to == from, as such copies are no-ops
268 and memcpy() has undefined behavior with overlapping memory areas.
269 */
270bool fields_are_memcpyable(const Field *to, const Field *from);
271
272/**
273 Copy the value in "from" (assumed to be non-NULL) to "to", doing any
274 required conversions in the process.
275
276 Note that you should only call this if fields_are_memcpyable() is false,
277 since it does an actual conversion on the slow path (and it is not properly
278 tested whether it gives the correct result in all cases if
279 fields_are_memcpyable() is true).
280
281 You should never call this with to == from, as they are no-ops.
282 */
284
285inline uint get_enum_pack_length(int elements) {
286 return elements < 256 ? 1 : 2;
287}
288
289inline uint get_set_pack_length(int elements) {
290 const uint len = (elements + 7) / 8;
291 return len > 4 ? 8 : len;
292}
293
295 if (dec_error & E_DEC_OOM) return TYPE_ERR_OOM;
296
297 if (dec_error & (E_DEC_DIV_ZERO | E_DEC_BAD_NUM)) return TYPE_ERR_BAD_VALUE;
298
299 if (dec_error & E_DEC_TRUNCATED) return TYPE_NOTE_TRUNCATED;
300
301 if (dec_error & E_DEC_OVERFLOW) return TYPE_WARN_OUT_OF_RANGE;
302
303 if (dec_error == E_DEC_OK) return TYPE_OK;
304
305 // impossible
306 assert(false);
307 return TYPE_ERR_BAD_VALUE;
308}
309
310/**
311 Convert warnings returned from str_to_time() and str_to_datetime()
312 to their corresponding type_conversion_status codes.
313*/
315 const int warn) {
317
319
321
323 return TYPE_ERR_BAD_VALUE;
324
326 // date was fine but pointed to daylight saving time switch gap
327 return TYPE_OK;
328
329 assert(!warn);
330 return TYPE_OK;
331}
332
333#define ASSERT_COLUMN_MARKED_FOR_READ \
334 assert(!table || \
335 (!table->read_set || bitmap_is_set(table->read_set, field_index())))
336#define ASSERT_COLUMN_MARKED_FOR_WRITE \
337 assert(!table || (!table->write_set || \
338 bitmap_is_set(table->write_set, field_index())))
339
340/**
341 Tests if field real type is temporal, i.e. represents
342 all existing implementations of
343 DATE, TIME, DATETIME or TIMESTAMP types in SQL.
344
345 @param type Field real type, as returned by field->real_type()
346 @retval true If field real type is temporal
347 @retval false If field real type is not temporal
348*/
350 switch (type) {
351 case MYSQL_TYPE_TIME2:
354 return true;
355 default:
356 return is_temporal_type(type);
357 }
358}
359
360/**
361 Tests if field real type can have "DEFAULT CURRENT_TIMESTAMP",
362 i.e. represents TIMESTAMP types in SQL.
363
364 @param type Field type, as returned by field->real_type().
365 @retval true If field real type can have "DEFAULT CURRENT_TIMESTAMP".
366 @retval false If field real type can not have "DEFAULT CURRENT_TIMESTAMP".
367*/
371}
372
373/**
374 Tests if field real type can have "ON UPDATE CURRENT_TIMESTAMP",
375 i.e. represents TIMESTAMP types in SQL.
376
377 @param type Field type, as returned by field->real_type().
378 @retval true If field real type can have "ON UPDATE CURRENT_TIMESTAMP".
379 @retval false If field real type can not have "ON UPDATE CURRENT_TIMESTAMP".
380*/
384}
385
386/**
387 Convert temporal real types as returned by field->real_type()
388 to field type as returned by field->type().
389
390 @param real_type Real type.
391 @retval Field type.
392*/
394 switch (real_type) {
395 case MYSQL_TYPE_TIME2:
396 return MYSQL_TYPE_TIME;
398 return MYSQL_TYPE_DATETIME;
402 return MYSQL_TYPE_DATE;
403 /* Note: NEWDECIMAL is a type, not only a real_type */
404 default:
405 return real_type;
406 }
407}
408
409/**
410 Return the appropriate MYSQL_TYPE_X_BLOB value based on the
411 pack_length.
412
413 @param pack_length pack_length for BLOB
414 @retval MYSQL_TYPE_X_BLOB corresponding to pack_length.
415*/
418 switch (pack_length) {
419 case 1:
421 case 2:
422 return MYSQL_TYPE_BLOB;
423 case 3:
425 case 4:
427 default:
428 assert(false);
430 }
431}
432
433/**
434 Copies an integer value to a format comparable with memcmp(). The
435 format is characterized by the following:
436
437 - The sign bit goes first and is unset for negative values.
438 - The representation is big endian.
439
440 The function template can be instantiated to copy from little or
441 big endian values.
442
443 @tparam Is_big_endian True if the source integer is big endian.
444
445 @param to Where to write the integer.
446 @param to_length Size in bytes of the destination buffer.
447 @param from Where to read the integer.
448 @param from_length Size in bytes of the source integer
449 @param is_unsigned True if the source integer is an unsigned value.
450*/
451template <bool Is_big_endian>
452void copy_integer(uchar *to, size_t to_length, const uchar *from,
453 size_t from_length, bool is_unsigned) {
454 if (to_length == 0) return;
455 if (Is_big_endian) {
456 std::copy(from, from + std::min(to_length, from_length), to);
457 if (!is_unsigned)
458 to[0] = static_cast<char>(to[0] ^ 128); // Reverse the sign bit.
459 } else {
460 const uchar *from_end = from + from_length;
461 const uchar *from_start = from_end - std::min(from_length, to_length);
462 std::reverse_copy(from_start, from_end, to);
463 if (!is_unsigned)
464 to[0] = static_cast<char>(to[0] ^ 128); // Reverse the sign bit.
465 }
466}
467
468/**
469 Enum to indicate source for which value generator is used. This is needed
470 while unpacking value generator expression and pre-validating the
471 expression for generated column, default expression or check constraint.
472*/
474 VGS_GENERATED_COLUMN = 0, // Value generator for GENERATED_COLUMN.
475 VGS_DEFAULT_EXPRESSION, // Value generator for Default expression.
476 VGS_CHECK_CONSTRAINT // Value generator for check constraints.
478
479/**
480 Used for storing information associated with generated column, default
481 values generated from expression or check constraint expression.
482*/
484 public:
485 /**
486 Item representing the generation expression.
487 This is non-NULL for every Field of a TABLE, if that field is a generated
488 column.
489 Contrast this with the Field of a TABLE_SHARE, which has expr_item==NULL
490 even if it's a generated column; that makes sense, as an Item tree cannot
491 be shared.
492 */
493 Item *expr_item{nullptr};
494 /**
495 Text of the expression. Used in only one case:
496 - the text read from the DD is put into the Value_generator::expr_str of
497 the Field of the TABLE_SHARE; then this expr_str is used as source
498 to produce expr_item for the Field of every TABLE derived from this
499 TABLE_SHARE.
500 */
501 LEX_STRING expr_str{nullptr, 0};
502
503 /**
504 Bit field indicating the type of statement for binary logging.
505 It needs to be saved because this is determined only once when it is parsed
506 but it needs to be set on the lex for each statement that uses this
507 value generator. And since unpacking is done once on table open, it will
508 be set for the rest of the statements in bind_value_generator_to_fields.
509 */
511
512 /// List of all items created when parsing and resolving generated expression
513 Item *item_list{nullptr};
514 /// Bitmap records base columns which a generated column depends on.
516
518
519 void set_field_type(enum_field_types fld_type) { field_type = fld_type; }
520
521 /**
522 Set the binary log flags in m_backup_binlog_stmt_flags
523 @param backup_binlog_stmt_flags the flags to be backed up
524 */
525 void backup_stmt_unsafe_flags(uint32 backup_binlog_stmt_flags) {
526 m_backup_binlog_stmt_flags = backup_binlog_stmt_flags;
527 }
528
529 /**
530 Get the binary log flags from m_backup_binlog_stmt_flags
531 @return the flags backed up by unpack_value_generator
532 */
534
535 bool get_field_stored() const { return stored_in_db; }
536 void set_field_stored(bool stored) { stored_in_db = stored; }
538 /**
539 Get the number of non virtual base columns that this generated
540 column needs.
541
542 @return number of non virtual base columns
543 */
545
546 /**
547 Duplicates a string into expr_str.
548
549 @param root MEM_ROOT to use for allocation
550 @param src source string
551 @param len length of 'src' in bytes
552 */
553 void dup_expr_str(MEM_ROOT *root, const char *src, size_t len);
554
555 /**
556 Writes the generation expression into a String with proper syntax.
557 @param thd THD
558 @param out output String
559 */
560 void print_expr(THD *thd, String *out);
561
562 /*
563 The following data is only updated by the parser and read
564 when a Create_field object is created/initialized.
565 */
566 private:
567 /// Real field type
569 /// Indicates if the field is physically stored in the database
570 bool stored_in_db{false};
571 /// How many non-virtual base columns in base_columns_map
573};
574
575class Field {
576 public:
577 /*
578 Field(const Item &) = delete;
579 The original intention was seemingly for Field to be non-copyable,
580 but due to a typo, this was never enforced, and now there's lots of
581 code that copies Field objects around. Thus, the default copy
582 constructor needs to stay (assignment is blocked off), but it's probably
583 better not to write code that depends on it.
584 */
585 Field(const Field &) = default;
586 void operator=(Field &) = delete;
587
588 /**
589 Checks if the field is marked as having a general expression to generate
590 default values.
591
592 @retval true The field has general expression as default
593 @retval false The field doesn't have any general expression as default
594 */
597 }
598
599 /**
600 Checks if the field is marked as having a datetime value expression to
601 generate default values on inserts.
602
603 @retval true The field has datetime expression as default
604 @retval false The field doesn't have a datime value expression as default
605 */
607 return auto_flags & DEFAULT_NOW;
608 }
609
610 /**
611 Checks if the field is marked as having a datetime value expression to
612 generate default values on updates.
613
614 @retval true The field has datetime expression as default for on update
615 @retval false The field doesn't have a datime value expression as default
616 for on update
617 */
619 return auto_flags & ON_UPDATE_NOW;
620 }
621
622 /**
623 Checks if the field is marked as having a constant expression to generate
624 default values. Relevant when re-creating a Create_field from a Field
625 during ALTER.
626
627 @retval true The field has a constant expression as default
628 @retval false The field doesn't have a constant expression as default
629 */
631 // For now this is true whenever neither GENERATED_FROM_EXPRESSION nor
632 // DEFAULT_NOW is set. If this changes in the future, we can add a separate
633 // flag for this.
635 }
636
637 protected:
638 /// Holds the position to the field in record
640
641 private:
643
644 /**
645 Byte where the @c NULL bit is stored inside a record. If this Field is a
646 @c NOT @c NULL field, this member is @c NULL.
647 */
649
650 /**
651 Flag: if the NOT-NULL field can be temporary NULL.
652 */
654
655 /**
656 This is a flag with the following semantics:
657 - it can be changed only when m_is_tmp_nullable is true;
658 - it specifies if this field in the first current record
659 (TABLE::record[0]) was set to NULL (temporary NULL).
660
661 This flag is used for trigger handling.
662 */
664
665 /**
666 The value of THD::check_for_truncated_fields at the moment of setting
667 m_is_tmp_null attribute.
668 */
670
671 protected:
672 /*
673 null_ptr buffer to be used for Fields that are nullable but
674 cannot store null. Typically used from create_tmp_field().
675 */
677
678 public:
680 /// Pointer to TABLE object that owns this field
682 /// Pointer to original database name, only non-NULL for a temporary table
683 const char *orig_db_name{nullptr};
684 /// Pointer to original table name, only non-NULL for a temporary table
685 const char *orig_table_name{nullptr};
686 const char **table_name, *field_name;
688 /* Field is part of the following keys */
689 Key_map key_start; /* Keys that starts with this field */
690 Key_map part_of_key; ///< Keys that includes this field
691 ///< except of prefix keys.
692 Key_map part_of_prefixkey; ///< Prefix keys
693 Key_map part_of_sortkey; /* ^ but only keys usable for sorting */
694 /**
695 All keys that include this field, but not extended by the storage engine to
696 include primary key columns.
697 */
699
700 /**
701 Flags for Field::auto_flags / Create_field::auto_flags bitmaps.
702
703 @note NEXT_NUMBER and DEFAULT_NOW/ON_UPDATE_NOW/GENERATED flags should
704 never be set at the same time. Also DEFAULT_NOW and GENERATED
705 should not be set at the same time.
706
707 @warning The values of this enum are used as bit masks for uchar
708 Field::auto_flags.
709 */
711 NONE = 0,
712 NEXT_NUMBER = 1, ///< AUTO_INCREMENT
713 DEFAULT_NOW = 2, ///< DEFAULT CURRENT_TIMESTAMP
714 ON_UPDATE_NOW = 4, ///< ON UPDATE CURRENT_TIMESTAMP
715 GENERATED_FROM_EXPRESSION = 8 ///< DEFAULT (expression)
716 };
717
727 };
729
730 // Max width for a VARCHAR column, in number of bytes
731 static constexpr size_t MAX_VARCHAR_WIDTH{65535};
732
733 // Maximum sizes of the four BLOB types, in number of bytes
734 static constexpr size_t MAX_TINY_BLOB_WIDTH{255};
735 static constexpr size_t MAX_SHORT_BLOB_WIDTH{65535};
736 static constexpr size_t MAX_MEDIUM_BLOB_WIDTH{16777215};
737 static constexpr size_t MAX_LONG_BLOB_WIDTH{4294967295};
738
739 // Length of field. Never write to this member directly; instead, use
740 // set_field_length().
743
744 private:
746 uint16 m_field_index; // field number in fields array
747
748 public:
749 bool is_flag_set(unsigned flag) const { return flags & flag; }
750 void set_flag(unsigned flag) { flags |= flag; }
751 void clear_flag(unsigned flag) { flags &= ~flag; }
752 // Avoid using this function as it makes it harder to change the internal
753 // representation.
754 uint32 all_flags() const { return flags; }
755 uchar null_bit; // Bit used to test null bit
756 /**
757 Bitmap of flags indicating if field value is auto-generated by default
758 and/or on update, and in which way.
759
760 @sa Field::enum_auto_flags for possible options.
761
762 @sa Field::utype and Field::unireg_check in pre-8.0 versions of server
763 for historical perspective.
764 */
766 /**
767 If true, this field was created in create_tmp_field_from_item from a NULL
768 value. This means that the type of the field is just a guess, and the type
769 may be freely coerced to another type.
770
771 @see create_tmp_field_from_item
772 @see Item_type_holder::get_real_type
773
774 */
776 /**
777 True if this field belongs to some index (unlike part_of_key, the index
778 might have only a prefix).
779 */
781
784
785 private:
790 };
791
792 /*
793 Bitmask specifying which warnings have been already pushed in order
794 not to repeat the same warning for the collmn multiple times.
795 Uses values of enum_pushed_warnings to control pushed warnings.
796 */
797 unsigned int m_warnings_pushed;
798
799 public:
800 /* Generated column data */
802 /**
803 Indication that the field is physically stored in tables
804 rather than just generated on SQL queries.
805 As of now, false can only be set for virtual generated columns.
806 */
808 /**
809 Whether the field is signed or not. Meaningful only for numeric fields
810 and numeric arrays.
811 */
812 virtual bool is_unsigned() const { return false; }
813 bool is_gcol() const { return gcol_info; }
814 bool is_virtual_gcol() const { return gcol_info && !stored_in_db; }
815
816 /// Holds the expression to be used to generate default values.
818
819 /**
820 Sets the hidden type for this field.
821
822 @param hidden the new hidden type to set.
823 */
825
826 /// @returns the hidden type for this field.
828
829 /**
830 @retval true if this field should be hidden away from users.
831 @retval false is this field is visible to the user.
832 */
833 bool is_hidden() const {
835 DBUG_EVALUATE_IF("show_hidden_columns", false, true);
836 }
837
838 /**
839 @retval true If this column is hidden either in the storage engine
840 or SQL layer. Either way, it is completely hidden from
841 the user.
842 @retval false Otherwise.
843 */
844 bool is_hidden_by_system() const {
847 DBUG_EVALUATE_IF("show_hidden_columns", false, true);
848 }
849
850 /**
851 @retval true If this column is hidden by the user.
852 @retval false otherwise.
853 */
854 bool is_hidden_by_user() const {
856 }
857
858 /**
859 @returns true if this is a hidden field that is used for implementing
860 functional indexes. Note that if we need different types of hidden
861 fields in the future (like invisible columns), this function needs
862 to be changed so it can distinguish between the different "types"
863 of hidden.
864 */
867 gcol_info != nullptr;
868 }
869
870 Field(uchar *ptr_arg, uint32 length_arg, uchar *null_ptr_arg,
871 uchar null_bit_arg, uchar auto_flags_arg, const char *field_name_arg);
872
873 virtual ~Field() = default;
874
876
877 /**
878 Turn on temporary nullability for the field.
879 */
881
882 /**
883 Turn off temporary nullability for the field.
884 */
886
887 /**
888 Reset temporary NULL value for field
889 */
890 void reset_tmp_null() { m_is_tmp_null = false; }
891
892 void set_tmp_null();
893
894 /**
895 @return temporary NULL-ability flag.
896 @retval true if NULL can be assigned temporary to the Field.
897 @retval false if NULL can not be assigned even temporary to the Field.
898 */
899 bool is_tmp_nullable() const { return m_is_tmp_nullable; }
900
901 /**
902 @return whether Field has temporary value NULL.
903 @retval true if the Field has temporary value NULL.
904 @retval false if the Field's value is NOT NULL, or if the temporary
905 NULL-ability flag is reset.
906 */
907 bool is_tmp_null() const { return is_tmp_nullable() && m_is_tmp_null; }
908
909 /* Store functions returns 1 on overflow and -1 on fatal error */
910 virtual type_conversion_status store(const char *to, size_t length,
911 const CHARSET_INFO *cs) = 0;
912 virtual type_conversion_status store(double nr) = 0;
913 virtual type_conversion_status store(longlong nr, bool unsigned_val) = 0;
914 /**
915 Store a temporal value in packed longlong format into a field.
916 The packed value is compatible with TIME_to_longlong_time_packed(),
917 TIME_to_longlong_date_packed() or TIME_to_longlong_datetime_packed().
918 Note, the value must be properly rounded or truncated according
919 according to field->decimals().
920
921 @param nr temporal value in packed longlong format.
922 @retval false on success
923 @retval true on error
924 */
926 return store(nr, false);
927 }
929 /**
930 Store MYSQL_TIME value with the given amount of decimal digits
931 into a field.
932
933 Note, the "dec" parameter represents number of digits of the Item
934 that previously created the MYSQL_TIME value. It's needed when we
935 store the value into a CHAR/VARCHAR/TEXT field to display
936 the proper amount of fractional digits.
937 For other field types the "dec" value does not matter and is ignored.
938
939 @param ltime Time, date or datetime value.
940 @param dec_arg Number of decimals in ltime.
941 @retval false on success
942 @retval true on error
943 */
944 virtual type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec_arg);
945 /**
946 Store MYSQL_TYPE value into a field when the number of fractional
947 digits is not important or is not know.
948
949 @param ltime Time, date or datetime value.
950 @retval false on success
951 @retval true on error
952 */
954 return store_time(ltime, 0);
955 }
956 type_conversion_status store(const char *to, size_t length,
957 const CHARSET_INFO *cs,
958 enum_check_fields check_level);
959 virtual double val_real() const = 0;
960 virtual longlong val_int() const = 0;
961 /**
962 Returns TIME value in packed longlong format.
963 This method should not be called for non-temporal types.
964 Temporal field types override the default method.
965 */
966 virtual longlong val_time_temporal() const {
967 assert(0);
968 return 0;
969 }
970 /**
971 Returns DATE/DATETIME value in packed longlong format.
972 This method should not be called for non-temporal types.
973 Temporal field types override the default method.
974 */
975 virtual longlong val_date_temporal() const {
976 assert(0);
977 return 0;
978 }
979
981 return val_time_temporal();
982 }
983
985 return val_date_temporal();
986 }
987
988 /**
989 Returns "native" packed longlong representation of
990 a TIME or DATE/DATETIME field depending on field type.
991 */
993 // Return longlong TIME or DATETIME representation, depending on field type
994 const enum_field_types field_type = type();
995 if (field_type == MYSQL_TYPE_TIME) return val_time_temporal();
996 assert(is_temporal_type_with_date(field_type));
997 return val_date_temporal();
998 }
999 virtual my_decimal *val_decimal(my_decimal *) const = 0;
1000 String *val_str(String *str) const { return val_str(str, str); }
1001 /*
1002 val_str(buf1, buf2) gets two buffers and should use them as follows:
1003 if it needs a temp buffer to convert result to string - use buf1
1004 example Field_tiny::val_str()
1005 if the value exists as a string already - use buf2
1006 example Field_string::val_str()
1007 consequently, buf2 may be created as 'String buf;' - no memory
1008 will be allocated for it. buf1 will be allocated to hold a
1009 value if it's too small. Using allocated buffer for buf2 may result in
1010 an unnecessary free (and later, may be an alloc).
1011 This trickery is used to decrease a number of malloc calls.
1012 */
1013 virtual String *val_str(String *, String *) const = 0;
1014 String *val_int_as_str(String *val_buffer, bool unsigned_flag) const;
1015 /*
1016 str_needs_quotes() returns true if the value returned by val_str() needs
1017 to be quoted when used in constructing an SQL query.
1018 */
1019 virtual bool str_needs_quotes() const { return false; }
1020 virtual Item_result result_type() const = 0;
1021 /**
1022 Returns Item_result type of a field when it appears
1023 in numeric context such as:
1024 SELECT time_column + 1;
1025 SELECT SUM(time_column);
1026 Examples:
1027 - a column of type TIME, DATETIME, TIMESTAMP act as INT.
1028 - a column of type TIME(1), DATETIME(1), TIMESTAMP(1)
1029 act as DECIMAL with 1 fractional digits.
1030 */
1032 return result_type();
1033 }
1034 virtual Item_result cmp_type() const { return result_type(); }
1035 virtual Item_result cast_to_int_type() const { return result_type(); }
1039 bool gcol_expr_is_equal(const Create_field *field) const;
1040 virtual bool eq(const Field *field) const {
1041 return (ptr == field->ptr && m_null_ptr == field->m_null_ptr &&
1042 null_bit == field->null_bit && field->type() == type());
1043 }
1044 virtual bool eq_def(const Field *field) const;
1045
1046 /*
1047 pack_length() returns size (in bytes) used to store field data in memory
1048 (i.e. it returns the maximum size of the field in a row of the table,
1049 which is located in RAM).
1050 */
1051 virtual uint32 pack_length() const { return (uint32)field_length; }
1052
1053 /*
1054 pack_length_in_rec() returns size (in bytes) used to store field data on
1055 storage (i.e. it returns the maximal size of the field in a row of the
1056 table, which is located on disk).
1057 */
1058 virtual uint32 pack_length_in_rec() const { return pack_length(); }
1059 virtual bool compatible_field_size(uint metadata, Relay_log_info *, uint16,
1060 int *order) const;
1061 virtual uint pack_length_from_metadata(uint field_metadata) const {
1062 DBUG_TRACE;
1063 return field_metadata;
1064 }
1065 virtual uint row_pack_length() const { return 0; }
1066 int save_field_metadata(uchar *first_byte) {
1067 return do_save_field_metadata(first_byte);
1068 }
1069
1070 /*
1071 data_length() return the "real size" of the data in memory.
1072 Useful only for variable length datatypes where it's overloaded.
1073 By default assume the length is constant.
1074 */
1075 virtual uint32 data_length(ptrdiff_t row_offset [[maybe_unused]] = 0) const {
1076 return pack_length();
1077 }
1078
1079 /**
1080 Get the maximum size of the data in packed format.
1081
1082 @return Maximum data length of the field when packed using the
1083 Field::pack() function.
1084 */
1085 virtual uint32 max_data_length() const { return pack_length(); }
1086
1088 memset(ptr, 0, pack_length());
1089 return TYPE_OK;
1090 }
1091 /**
1092 Returns a UTC component in `struct timeval` format. This interface
1093 makes any column appear to be `TIMESTAMP`, i.e. stored in UTC, and
1094 returns the UTC component in (optionally fractional) seconds. This means
1095 converting _to_ UTC from the current session's time zone for types other
1096 than `TIMESTAMP`.
1097
1098 This method was expressly written for `SELECT UNIX_TIMESTAMP(field)`
1099 to avoid conversion from timestamp to MYSQL_TIME and back.
1100 */
1101 virtual bool get_timestamp(my_timeval *tm, int *warnings) const;
1102 /**
1103 Stores a timestamp value in timeval format in a field.
1104
1105 @note
1106 - store_timestamp(), get_timestamp() and store_time() do not depend on
1107 timezone and always work "in UTC".
1108
1109 - The default implementation of this interface expects that storing the
1110 value will not fail. For most Field descendent classes, this is not the
1111 case. However, this interface is only used when the function
1112 CURRENT_TIMESTAMP is used as a column default expression, and currently we
1113 only allow TIMESTAMP and DATETIME columns to be declared with this as the
1114 column default. Hence it is enough that the classes implementing columns
1115 with these types either override this interface, or that
1116 store_time(MYSQL_TIME*, uint8) does not fail.
1117
1118 - The column types above interpret decimals() to mean the scale of the
1119 fractional seconds.
1120
1121 - We also have the limitation that the scale of a column must be the same as
1122 the scale of the CURRENT_TIMESTAMP. I.e. we only allow
1123
1124 @code
1125
1126 [ TIMESTAMP | DATETIME ] (n) [ DEFAULT | ON UPDATE ] CURRENT_TIMESTAMP (n)
1127
1128 @endcode
1129
1130 Since this interface relies on the caller to truncate the value according to
1131 this Field's scale, it will work with all constructs that we currently allow.
1132 */
1133 virtual void store_timestamp(const my_timeval *) { assert(false); }
1134
1135 virtual void set_default();
1136
1137 /**
1138 Evaluates the @c INSERT default function and stores the result in the
1139 field. If no such function exists for the column, or the function is not
1140 valid for the column's data type, invoking this function has no effect.
1141 */
1143
1144 /**
1145 Evaluates the @c UPDATE default function, if one exists, and stores the
1146 result in the record buffer. If no such function exists for the column,
1147 or the function is not valid for the column's data type, invoking this
1148 function has no effect.
1149 */
1151 virtual bool binary() const { return true; }
1152 virtual bool zero_pack() const { return true; }
1153 virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
1154 virtual uint32 key_length() const { return pack_length(); }
1155 virtual enum_field_types type() const = 0;
1156 virtual enum_field_types real_type() const { return type(); }
1158 /*
1159 Binlog stores field->type() as type code by default.
1160 This puts MYSQL_TYPE_STRING in case of CHAR, VARCHAR, SET and ENUM,
1161 with extra data type details put into metadata.
1162
1163 We cannot store field->type() in case of temporal types with
1164 fractional seconds: TIME(n), DATETIME(n) and TIMESTAMP(n),
1165 because binlog records with MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME
1166 type codes do not have metadata.
1167 So for temporal data types with fractional seconds we'll store
1168 real_type() type codes instead, i.e.
1169 MYSQL_TYPE_TIME2, MYSQL_TYPE_DATETIME2, MYSQL_TYPE_TIMESTAMP2,
1170 and put precision into metatada.
1171
1172 Note: perhaps binlog should eventually be modified to store
1173 real_type() instead of type() for all column types.
1174 */
1175 return type();
1176 }
1177 int cmp(const uchar *str) const { return cmp(ptr, str); }
1178 virtual int cmp_max(const uchar *a, const uchar *b,
1179 uint max_len [[maybe_unused]]) const {
1180 return cmp(a, b);
1181 }
1182 virtual int cmp(const uchar *, const uchar *) const = 0;
1183 virtual int cmp_binary(const uchar *a, const uchar *b,
1184 uint32 max_length [[maybe_unused]] = ~0L) const {
1185 return memcmp(a, b, pack_length());
1186 }
1187 virtual int cmp_offset(ptrdiff_t row_offset) const {
1188 return cmp(ptr, ptr + row_offset);
1189 }
1190 virtual int cmp_binary_offset(ptrdiff_t row_offset) const {
1191 return cmp_binary(ptr, ptr + row_offset);
1192 }
1193 virtual int key_cmp(const uchar *a, const uchar *b) const {
1194 return cmp(a, b);
1195 }
1196 virtual int key_cmp(const uchar *str, uint length [[maybe_unused]]) const {
1197 return cmp(ptr, str);
1198 }
1199 virtual uint decimals() const { return 0; }
1200 virtual bool is_text_key_type() const { return false; }
1201
1202 /*
1203 Caller beware: sql_type can change str.Ptr, so check
1204 ptr() to see if it changed if you are using your own buffer
1205 in str and restore it with set() if needed
1206 */
1207 virtual void sql_type(String &str) const = 0;
1208
1209 /**
1210 Check whether the full table's row is NULL or the Field has value NULL.
1211
1212 @return true if the full table's row is NULL or the Field has value NULL
1213 false if neither table's row nor the Field has value NULL
1214 */
1215 bool is_null(ptrdiff_t row_offset = 0) const {
1216 /*
1217 if the field is NULLable, it returns NULLity based
1218 on m_null_ptr[row_offset] value. Otherwise it returns
1219 NULL flag depending on TABLE::has_null_row() value.
1220
1221 The table may have been marked as containing only NULL values
1222 for all fields if it is a NULL-complemented row of an OUTER JOIN
1223 or if the query is an implicitly grouped query (has aggregate
1224 functions but no GROUP BY clause) with no qualifying rows. If
1225 this is the case (in which TABLE::has_null_row() is true) and the
1226 field is not nullable, the field is considered to be NULL.
1227
1228 Do not change the order of testing. Fields may be associated
1229 with a TABLE object without being part of the current row.
1230 For NULL value check to work for these fields, they must
1231 have a valid m_null_ptr, and this pointer must be checked before
1232 TABLE::has_null_row().
1233 */
1234 if (is_nullable()) return (m_null_ptr[row_offset] & null_bit);
1235
1236 if (is_tmp_nullable()) return m_is_tmp_null;
1237
1238 return table->has_null_row();
1239 }
1240
1241 /**
1242 Check whether the Field has value NULL (temporary or actual).
1243
1244 @return true if the Field has value NULL (temporary or actual)
1245 false if the Field has value NOT NULL.
1246 */
1247 bool is_real_null(ptrdiff_t row_offset = 0) const {
1248 if (is_nullable()) return (m_null_ptr[row_offset] & null_bit);
1249
1250 if (is_tmp_nullable()) return m_is_tmp_null;
1251
1252 return false;
1253 }
1254
1255 /**
1256 Check if the Field has value NULL or the record specified by argument
1257 has value NULL for this Field.
1258
1259 @return true if the Field has value NULL or the record has value NULL
1260 for thois Field.
1261 */
1262 bool is_null_in_record(const uchar *record) const {
1263 if (is_nullable()) return (record[null_offset()] & null_bit);
1264
1265 return is_tmp_nullable() ? m_is_tmp_null : false;
1266 }
1267
1268 void set_null(ptrdiff_t row_offset = 0);
1269
1270 void set_notnull(ptrdiff_t row_offset = 0);
1271
1272 // Cannot be const as it calls set_warning
1274
1275 /**
1276 Remember the value of THD::check_for_truncated_fields to handle possible
1277 NOT-NULL constraint errors after BEFORE-trigger execution is finished.
1278 We should save the value of THD::check_for_truncated_fields before starting
1279 BEFORE-trigger processing since during triggers execution the
1280 value of THD::check_for_truncated_fields could be changed.
1281 */
1283 enum_check_fields check_for_truncated_fields) {
1284 m_check_for_truncated_fields_saved = check_for_truncated_fields;
1285 }
1286
1287 /// @return true if this field is NULL-able, false otherwise.
1288 bool is_nullable() const { return m_null_ptr != nullptr; }
1289
1290 uint null_offset(const uchar *record) const {
1291 return (uint)(m_null_ptr - record);
1292 }
1293
1294 uint null_offset() const;
1295
1296 void set_null_ptr(uchar *p_null_ptr, uint p_null_bit) {
1297 m_null_ptr = p_null_ptr;
1298 null_bit = p_null_bit;
1299 }
1300
1301 /**
1302 Populates a Send_field object with metadata about the column represented by
1303 this Field object. The Send_field object is used for sending column metadata
1304 to the client.
1305
1306 @param[out] send_field the Send_field object to populate
1307 */
1308 virtual void make_send_field(Send_field *send_field) const;
1309
1310 /**
1311 Writes a copy of the current value in the record buffer, suitable for
1312 sorting using byte-by-byte comparison. Integers are always in big-endian
1313 regardless of hardware architecture. At most length bytes are written
1314 into the buffer.
1315
1316 @param buff The buffer, assumed to be at least length bytes.
1317
1318 @param length Number of bytes to write.
1319
1320 @retval The number of bytes actually written.
1321
1322 @note This is now only used by replication; filesort makes its own
1323 sort keys based off of Items, not Fields.
1324 */
1325 virtual size_t make_sort_key(uchar *buff, size_t length) const = 0;
1326 /**
1327 Whether this field can be used for index range scans when in
1328 the given keypart of the given index.
1329 */
1330 virtual bool optimize_range(uint idx, uint part) const;
1331 /*
1332 This should be true for fields which, when compared with constant
1333 items, can be casted to longlong. In this case we will at 'fix_fields'
1334 stage cast the constant items to longlongs and at the execution stage
1335 use field->val_int() for comparison. Used to optimize clauses like
1336 'a_column BETWEEN date_const, date_const'.
1337 */
1338 virtual bool can_be_compared_as_longlong() const { return false; }
1339 virtual void mem_free() {}
1340
1341 virtual Field *new_field(MEM_ROOT *root, TABLE *new_table) const;
1342
1343 Field *new_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr,
1344 uchar *new_null_ptr, uint new_null_bit) const {
1345 Field *field = new_field(root, new_table);
1346 field->move_field(new_ptr, new_null_ptr, new_null_bit);
1347 return field;
1348 }
1349
1350 virtual Field *new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr,
1351 uchar *new_null_ptr, uint new_null_bit) const;
1352
1353 Field *new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr) const {
1354 return new_key_field(root, new_table, new_ptr, m_null_ptr, null_bit);
1355 }
1356
1357 /**
1358 Makes a shallow copy of the Field object.
1359
1360 @note This member function must be overridden in all concrete
1361 subclasses. Several of the Field subclasses are concrete even though they
1362 are not leaf classes, so the compiler will not always catch this.
1363
1364 @param mem_root MEM_ROOT to use for memory allocation.
1365 @retval NULL If memory allocation failed.
1366 */
1367 virtual Field *clone(MEM_ROOT *mem_root) const = 0;
1368
1369 void move_field(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg) {
1370 ptr = ptr_arg;
1371 m_null_ptr = null_ptr_arg;
1372 null_bit = null_bit_arg;
1373 }
1374
1375 virtual void move_field_offset(ptrdiff_t ptr_diff) {
1376 ptr += ptr_diff;
1377 if (is_nullable()) m_null_ptr += ptr_diff;
1378 }
1379
1380 virtual void get_image(uchar *buff, size_t length,
1381 const CHARSET_INFO *) const {
1382 memcpy(buff, ptr, length);
1383 }
1384
1385 virtual void set_image(const uchar *buff, size_t length,
1386 const CHARSET_INFO *) {
1387 memcpy(ptr, buff, length);
1388 }
1389
1390 /*
1391 Copy a field part into an output buffer.
1392
1393 SYNOPSIS
1394 Field::get_key_image()
1395 buff [out] output buffer
1396 length output buffer size
1397 type itMBR for geometry blobs, otherwise itRAW
1398
1399 DESCRIPTION
1400 This function makes a copy of field part of size equal to or
1401 less than "length" parameter value.
1402 For fields of string types (CHAR, VARCHAR, TEXT) the rest of buffer
1403 is padded by zero byte.
1404
1405 NOTES
1406 For variable length character fields (i.e. UTF-8) the "length"
1407 parameter means a number of output buffer bytes as if all field
1408 characters have maximal possible size (mbmaxlen). In the other words,
1409 "length" parameter is a number of characters multiplied by
1410 field_charset->mbmaxlen.
1411
1412 RETURN
1413 Number of copied bytes (excluding padded zero bytes -- see above).
1414 */
1415
1416 virtual size_t get_key_image(uchar *buff, size_t length,
1417 imagetype type [[maybe_unused]]) const {
1419 return length;
1420 }
1421 virtual void set_key_image(const uchar *buff, size_t length) {
1423 }
1424 longlong val_int_offset(ptrdiff_t row_offset) {
1425 ptr += row_offset;
1426 const longlong tmp = val_int();
1427 ptr -= row_offset;
1428 return tmp;
1429 }
1431 uchar *old_ptr = ptr;
1432 longlong return_value;
1433 ptr = new_ptr;
1434 return_value = val_int();
1435 ptr = old_ptr;
1436 return return_value;
1437 }
1439 uchar *old_ptr = ptr;
1440 ptr = new_ptr;
1441 val_str(str);
1442 ptr = old_ptr;
1443 return str;
1444 }
1445
1446 /**
1447 Send the value of this field over the protocol using the correct
1448 Protocol::store*() function which matches the type of the field.
1449 */
1450 virtual bool send_to_protocol(Protocol *protocol) const;
1451
1452 /**
1453 Pack the field into a format suitable for storage and transfer.
1454
1455 To implement packing functionality, only the virtual function
1456 should be overridden. The other functions are just convenience
1457 functions and hence should not be overridden.
1458
1459 The actual format is opaque and will vary between types of Field
1460 (it is meant to be unpacked by unpack(), but be aware that it is
1461 used among others in the replication log, so you cannot change it
1462 without incurring a format break.
1463
1464 @note The default implementation just copies the raw bytes
1465 of the record into the destination, but never more than
1466 <code>max_length</code> characters.
1467
1468 @param to
1469 Pointer to memory area where representation of field should be put.
1470
1471 @param from
1472 Pointer to memory area where record representation of field is
1473 stored, typically field->field_ptr().
1474
1475 @param max_length
1476 Available space in “to”, in bytes. pack() will not write more bytes than
1477 this; if the field is too short, the contents _are not unpackable by
1478 unpack()_. (It is nominally supposed to be a prefix of what would have
1479 been written with a full buffer, ie., the same as packing and then
1480 truncating the output, but not all Field classes follow this.)
1481
1482 @return The byte after the last byte in “to” written to. If the return
1483 value is equal to (to + max_length), it could either be that the value
1484 fit exactly, or that the buffer was too small; you cannot distinguish
1485 between the two cases based on the return value alone.
1486 */
1487 virtual uchar *pack(uchar *to, const uchar *from, size_t max_length) const;
1488
1489 uchar *pack(uchar *to) const { return pack(to, ptr, UINT_MAX); }
1490
1491 virtual const uchar *unpack(uchar *to, const uchar *from, uint param_data);
1492
1493 const uchar *unpack(const uchar *from) { return unpack(ptr, from, 0U); }
1494
1495 /**
1496 This function does the same thing as pack(), except for the difference
1497 that max_length does not mean the number of bytes in the output, but the
1498 maximum field length from the input (which must be exactly
1499 field->max_field_length()). The difference is currently only relevant for
1500 Field_blob, but can be summed up as follows:
1501
1502 - If the actual field length is longer than "max_length", by way of
1503 software bug or otherwise, the function may behave as if it were shorter,
1504 and write something that is still readable by unpack().
1505 - There is no bounds checking; the caller must verify that there is
1506 sufficient space in "to". Even in the case of truncation, "to" must
1507 be long enough to hold the untruncated field, as the return pointer
1508 would otherwise be invalid, causing undefined behavior as per the C++
1509 standard.
1510 */
1511 virtual uchar *pack_with_metadata_bytes(uchar *to, const uchar *from,
1512 uint max_length) const {
1513 return pack(to, from, max_length);
1514 }
1515
1516 /**
1517 Write the field for the binary log in diff format.
1518
1519 This should only write the field if the diff format is smaller
1520 than the full format. Otherwise it should leave the buffer
1521 untouched.
1522
1523 @param[in,out] to Pointer to buffer where the field will be
1524 written. This will be changed to point to the next byte after the
1525 last byte that was written.
1526
1527 @param value_options bitmap that indicates if full or partial
1528 JSON format is to be used.
1529
1530 @retval true The field was not written, either because the data
1531 type does not support it, or because it was disabled according to
1532 value_options, or because there was no diff information available
1533 from the optimizer, or because the the diff format was bigger than
1534 the full format. The 'to' parameter is unchanged in this case.
1535
1536 @retval false The field was written.
1537 */
1538 virtual bool pack_diff(uchar **to [[maybe_unused]],
1539 ulonglong value_options [[maybe_unused]]) const {
1540 return true;
1541 }
1542
1543 /**
1544 This is a wrapper around pack_length() used by filesort() to determine
1545 how many bytes we need for packing "addon fields".
1546 @returns maximum size of a row when stored in the filesort buffer.
1547 */
1548
1549 virtual uint max_packed_col_length() const { return pack_length(); }
1550
1551 uint offset(uchar *record) const { return (uint)(ptr - record); }
1552
1553 void copy_data(ptrdiff_t src_record_offset);
1554
1555 virtual bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const;
1556
1557 virtual bool get_time(MYSQL_TIME *ltime) const;
1558
1559 virtual const CHARSET_INFO *charset() const { return &my_charset_bin; }
1560
1562 return binary() ? &my_charset_bin : charset();
1563 }
1564 virtual const CHARSET_INFO *sort_charset() const { return charset(); }
1565 virtual bool has_charset() const { return false; }
1566 /*
1567 match_collation_to_optimize_range() is to distinguish in
1568 range optimizer (see opt_range.cc) between real string types:
1569 CHAR, VARCHAR, TEXT
1570 and the other string-alike types with result_type() == STRING_RESULT:
1571 DATE, TIME, DATETIME, TIMESTAMP
1572 We need it to decide whether to test if collation of the operation
1573 matches collation of the field (needed only for real string types).
1574 QQ: shouldn't DATE/TIME types have their own XXX_RESULT types eventually?
1575 */
1576
1577 virtual bool match_collation_to_optimize_range() const { return false; }
1578 virtual enum Derivation derivation() const { return DERIVATION_IMPLICIT; }
1579 virtual uint repertoire() const { return MY_REPERTOIRE_UNICODE30; }
1580 virtual void set_derivation(enum Derivation) {}
1581
1582 /**
1583 Produce warning or note about data saved into field.
1584
1585 @param level - level of message (Note/Warning/Error)
1586 @param code - error code of message to be produced
1587 @param cut_increment - whenever we should increase cut fields count
1588
1589 @note
1590 This function won't produce warning and increase cut fields counter
1591 if check_for_truncated_fields == CHECK_FIELD_IGNORE for current thread.
1592
1593 if check_for_truncated_fields == CHECK_FIELD_IGNORE then we ignore notes.
1594 This allows us to avoid notes in optimization, like
1595 convert_constant_item().
1596
1597 @retval
1598 1 if check_for_truncated_fields == CHECK_FIELD_IGNORE and error level
1599 is not NOTE
1600 @retval
1601 0 otherwise
1602 */
1604 int cut_increment) {
1605 return set_warning(level, code, cut_increment, nullptr, nullptr);
1606 }
1607
1608 bool set_warning(Sql_condition::enum_severity_level level, uint code,
1609 int cut_increment, const char *view_db,
1610 const char *view_name);
1611
1612 bool warn_if_overflow(int op_result);
1613 virtual void init(TABLE *table_arg);
1614
1615 /* maximum possible display length */
1616 virtual uint32 max_display_length() const = 0;
1617
1618 /**
1619 Whether a field being created is type-compatible with an existing one.
1620
1621 Used by the ALTER TABLE code to evaluate whether the new definition
1622 of a table is compatible with the old definition so that it can
1623 determine if data needs to be copied over (table data change).
1624 Constraints and generation clause (default value, generation expression)
1625 are not checked by this function.
1626
1627 @param new_field new field definition from alter.
1628 @retval IS_EQUAL_YES if there is no change.
1629 @retval IS_EQUAL_PACK_LENGTH if the data are unchanged, but the length
1630 requirements have changed
1631 @retval IS_EQUAL_NO if there is an incompatible change requiring copy.
1632 */
1633
1634 virtual uint is_equal(const Create_field *new_field) const;
1635
1636 /* convert decimal to longlong with overflow check */
1637 longlong convert_decimal2longlong(const my_decimal *val, bool unsigned_flag,
1638 bool *has_overflow);
1639 /* The max. number of characters */
1640 virtual uint32 char_length() const {
1641 return field_length / charset()->mbmaxlen;
1642 }
1643
1645 /* shouldn't get here. */
1646 assert(0);
1647 return GEOM_GEOMETRY;
1648 }
1649#ifndef NDEBUG
1650 /* Print field value into debug trace, in NULL-aware way. */
1651 void dbug_print() const {
1652 if (is_real_null())
1653 fprintf(DBUG_FILE, "NULL");
1654 else {
1655 char buf[256];
1656 String str(buf, sizeof(buf), &my_charset_bin);
1657 str.length(0);
1658 String *pstr;
1659 pstr = val_str(&str);
1660 fprintf(DBUG_FILE, "'%s'", pstr->c_ptr_safe());
1661 }
1662 }
1663#endif
1664
1667 }
1668
1669 void set_storage_type(ha_storage_media storage_type_arg) {
1670 assert(field_storage_type() == HA_SM_DEFAULT);
1671 flags |= (storage_type_arg << FIELD_FLAGS_STORAGE_MEDIA);
1672 }
1673
1676 }
1677
1678 void set_column_format(column_format_type column_format_arg) {
1680 flags |= (column_format_arg << FIELD_FLAGS_COLUMN_FORMAT);
1681 }
1682
1683 /* Validate the value stored in a field */
1685 [[maybe_unused]]) {
1686 return TYPE_OK;
1687 }
1688
1689 /* Hash value */
1690 virtual void hash(ulong *nr, ulong *nr2) const;
1691
1692 /**
1693 Get the upper limit of the MySQL integral and floating-point type.
1694
1695 @return maximum allowed value for the field
1696 */
1698 assert(false);
1699 return 0ULL;
1700 }
1701
1702 /**
1703 Return a const pointer to the actual data in the record buffer.
1704
1705 For most fields, this is the same as field_ptr(), but BLOBs and VARCHARs
1706 it is not. Ideally this function should not be used as it makes it hard
1707 to change the internal representation of Field.
1708 */
1709 virtual const uchar *data_ptr() const { return ptr; }
1710
1711 /**
1712 Return a const pointer to where the field is stored in the record buffer.
1713
1714 Ideally this function should not be used as it makes it hard
1715 to change the internal representation of Field.
1716 */
1717 const uchar *field_ptr() const { return ptr; }
1718
1719 /**
1720 Return a pointer to where the field is stored in the record buffer.
1721
1722 Ideally this function should not be used as it makes it hard
1723 to change the internal representation of Field.
1724 */
1725 uchar *field_ptr() { return ptr; }
1726
1727 void set_field_ptr(uchar *ptr_arg) { ptr = ptr_arg; }
1728
1729 /**
1730 Checks whether a string field is part of write_set.
1731
1732 @return
1733 false - If field is not char/varchar/....
1734 - If field is char/varchar/.. and is not part of write set.
1735 true - If field is char/varchar/.. and is part of write set.
1736 */
1737 virtual bool is_updatable() const { return false; }
1738
1739 /**
1740 Check whether field is part of the index taking the index extensions flag
1741 into account. Index extensions are also not applicable to UNIQUE indexes
1742 for loose index scans.
1743
1744 @param[in] thd THD object
1745 @param[in] cur_index Index of the key
1746 @param[in] cur_index_info key_info object
1747
1748 @retval true Field is part of the key
1749 @retval false otherwise
1750
1751 */
1752
1753 bool is_part_of_actual_key(THD *thd, uint cur_index,
1754 KEY *cur_index_info) const;
1755
1756 /**
1757 Get covering prefix keys.
1758
1759 @retval covering prefix keys.
1760 */
1762
1763 /// Whether the field is a typed array
1764 virtual bool is_array() const { return false; }
1765
1766 /**
1767 Return number of bytes the field's length takes
1768
1769 Valid only for varchar and typed arrays of varchar
1770 */
1771 virtual uint32 get_length_bytes() const {
1772 assert(0);
1773 return 0;
1774 }
1775
1776 /**
1777 Whether field's old valued have to be handled.
1778
1779 @returns
1780 true if field is virtual an either one of BLOB types or typed array
1781 false otherwise
1782 */
1783 bool handle_old_value() const {
1784 return (is_flag_set(BLOB_FLAG) || is_array()) && is_virtual_gcol();
1785 }
1786
1787 /**
1788 Sets field index.
1789
1790 @param[in] field_index Field index.
1791 */
1794 }
1795
1796 /**
1797 Returns field index.
1798
1799 @returns Field index.
1800 */
1802
1803 private:
1804 /**
1805 Retrieve the field metadata for fields.
1806
1807 This default implementation returns 0 and saves 0 in the metadata_ptr
1808 value.
1809
1810 @param metadata_ptr First byte of field metadata
1811
1812 @returns 0 no bytes written.
1813 */
1814 virtual int do_save_field_metadata(uchar *metadata_ptr
1815 [[maybe_unused]]) const {
1816 return 0;
1817 }
1818
1819 protected:
1820 uchar *pack_int16(uchar *to, const uchar *from, size_t max_length) const;
1821
1822 const uchar *unpack_int16(uchar *to, const uchar *from) const;
1823
1824 uchar *pack_int24(uchar *to, const uchar *from, size_t max_length) const;
1825
1826 const uchar *unpack_int24(uchar *to, const uchar *from) const;
1827
1828 uchar *pack_int32(uchar *to, const uchar *from, size_t max_length) const;
1829
1830 const uchar *unpack_int32(uchar *to, const uchar *from) const;
1831
1832 uchar *pack_int64(uchar *to, const uchar *from, size_t max_length) const;
1833
1834 const uchar *unpack_int64(uchar *to, const uchar *from) const;
1835};
1836
1837/**
1838 This class is a substitute for the Field classes during CREATE TABLE
1839
1840 When adding a functional index at table creation, we need to resolve the
1841 expression we are indexing. All functions that references one or more
1842 columns expect a Field to be available. But during CREATE TABLE, we only
1843 have access to Create_field. So this class acts as a substitute for the
1844 Field classes so that expressions can be properly resolved. Thus, trying
1845 to call store or val_* on this class will cause an assertion.
1846*/
1847class Create_field_wrapper final : public Field {
1849
1850 public:
1852 Item_result result_type() const final;
1854 enum_field_types type() const final;
1855 uint32 max_display_length() const final;
1856
1857 const CHARSET_INFO *charset() const final;
1858
1859 uint32 pack_length() const final;
1860
1861 // Since it's not a real field, functions below shouldn't be used.
1862 /* purecov: begin deadcode */
1863 type_conversion_status store(const char *, size_t,
1864 const CHARSET_INFO *) final {
1865 assert(false);
1866 return TYPE_ERR_BAD_VALUE;
1867 }
1869 assert(false);
1870 return TYPE_ERR_BAD_VALUE;
1871 }
1873 assert(false);
1874 return TYPE_ERR_BAD_VALUE;
1875 }
1877 assert(false);
1878 return TYPE_ERR_BAD_VALUE;
1879 }
1880 double val_real(void) const final {
1881 assert(false);
1882 return 0.0;
1883 }
1884 longlong val_int(void) const final {
1885 assert(false);
1886 return 0;
1887 }
1889 assert(false);
1890 return nullptr;
1891 }
1892 String *val_str(String *, String *) const final {
1893 assert(false);
1894 return nullptr;
1895 }
1896 int cmp(const uchar *, const uchar *) const final {
1897 assert(false);
1898 return -1;
1899 }
1900 void sql_type(String &) const final { assert(false); }
1901 size_t make_sort_key(uchar *, size_t) const final {
1902 assert(false);
1903 return 0;
1904 }
1905 Field *clone(MEM_ROOT *mem_root) const final {
1906 return new (mem_root) Create_field_wrapper(*this);
1907 }
1908 /* purecov: end */
1909};
1910
1911class Field_num : public Field {
1912 private:
1913 /**
1914 Whether the field is signed or not. Meaningful only for numeric fields
1915 and numeric arrays.
1916 */
1917 const bool unsigned_flag;
1918
1919 public:
1920 const uint8 dec;
1921 /**
1922 True if the column was declared with the ZEROFILL attribute. If it has the
1923 attribute, values should be zero-padded up to the declared display width
1924 when they are converted to strings.
1925 */
1926 bool zerofill; // Purify cannot handle bit fields
1927 Field_num(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1928 uchar null_bit_arg, uchar auto_flags_arg,
1929 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
1930 bool unsigned_arg);
1931 bool is_unsigned() const final { return unsigned_flag; }
1932 Item_result result_type() const override { return REAL_RESULT; }
1933 enum Derivation derivation() const final { return DERIVATION_NUMERIC; }
1934 uint repertoire() const final { return MY_REPERTOIRE_NUMERIC; }
1935 const CHARSET_INFO *charset() const final { return &my_charset_numeric; }
1936 void prepend_zeros(String *value) const;
1937 uint decimals() const final { return (uint)dec; }
1938 bool eq_def(const Field *field) const final;
1941 my_decimal *val_decimal(my_decimal *) const override;
1942 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const override;
1943 bool get_time(MYSQL_TIME *ltime) const override;
1944 uint is_equal(const Create_field *new_field) const override;
1945 uint row_pack_length() const final { return pack_length(); }
1946 uint32 pack_length_from_metadata(uint) const override {
1947 return pack_length();
1948 }
1950 size_t length, const char *int_end,
1951 int error);
1952 type_conversion_status get_int(const CHARSET_INFO *cs, const char *from,
1953 size_t len, longlong *rnd,
1954 ulonglong unsigned_max, longlong signed_min,
1955 longlong signed_max);
1956};
1957
1958class Field_str : public Field {
1959 protected:
1962
1963 public:
1964 Field_str(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1965 uchar null_bit_arg, uchar auto_flags_arg,
1966 const char *field_name_arg, const CHARSET_INFO *charset);
1967 Item_result result_type() const override { return STRING_RESULT; }
1969 uint decimals() const override { return DECIMAL_NOT_SPECIFIED; }
1970 void make_send_field(Send_field *field) const override;
1971 type_conversion_status store(double nr) override;
1972 type_conversion_status store(longlong nr, bool unsigned_val) override = 0;
1974 type_conversion_status store(const char *to, size_t length,
1975 const CHARSET_INFO *cs) override = 0;
1976
1977 uint repertoire() const final { return my_charset_repertoire(field_charset); }
1978 const CHARSET_INFO *charset() const override { return field_charset; }
1979 void set_charset(const CHARSET_INFO *charset_arg) {
1980 field_charset = charset_arg;
1982 }
1986 }
1987 enum Derivation derivation() const final { return field_derivation; }
1988 void set_derivation(enum Derivation derivation_arg) final {
1989 field_derivation = derivation_arg;
1990 }
1991 bool binary() const override { return field_charset == &my_charset_bin; }
1992 uint32 max_display_length() const override { return field_length; }
1993 bool str_needs_quotes() const final { return true; }
1994 uint is_equal(const Create_field *new_field) const override;
1995
1996 // An always-updated cache of the result of char_length(), because
1997 // dividing by charset()->mbmaxlen can be surprisingly costly compared
1998 // to the rest of e.g. make_sort_key().
2000};
2001
2002/* base class for Field_string, Field_varstring and Field_blob */
2003
2004class Field_longstr : public Field_str {
2005 private:
2007 const char *end,
2008 bool count_spaces);
2009
2010 protected:
2012 const char *well_formed_error_pos, const char *cannot_convert_error_pos,
2013 const char *from_end_pos, const char *end, bool count_spaces,
2014 const CHARSET_INFO *cs);
2015
2016 public:
2017 Field_longstr(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2018 uchar null_bit_arg, uchar auto_flags_arg,
2019 const char *field_name_arg, const CHARSET_INFO *charset_arg)
2020 : Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2021 field_name_arg, charset_arg) {}
2022
2024 uint32 max_data_length() const override;
2025 bool is_updatable() const final;
2026};
2027
2028/* base class for float and double and decimal (old one) */
2029class Field_real : public Field_num {
2030 public:
2033 TR_OK = 0,
2034 TR_POSITIVE_OVERFLOW = 1,
2035 TR_NEGATIVE_OVERFLOW = 2
2037
2038 Field_real(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2039 uchar null_bit_arg, uchar auto_flags_arg,
2040 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2041 bool unsigned_arg)
2042 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2043 field_name_arg, dec_arg, zero_arg, unsigned_arg),
2044 not_fixed(dec_arg >= DECIMAL_NOT_SPECIFIED) {}
2047 my_decimal *val_decimal(my_decimal *) const final;
2048 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
2049 bool get_time(MYSQL_TIME *ltime) const final;
2050 Truncate_result truncate(double *nr, double max_length);
2051 uint32 max_display_length() const final { return field_length; }
2052 const uchar *unpack(uchar *to, const uchar *from, uint param_data) override;
2053 uchar *pack(uchar *to, const uchar *from, size_t max_length) const override;
2054};
2055
2056class Field_decimal final : public Field_real {
2057 public:
2058 Field_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2059 uchar null_bit_arg, uchar auto_flags_arg,
2060 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2061 bool unsigned_arg)
2062 : Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2063 field_name_arg, dec_arg, zero_arg, unsigned_arg) {}
2064 enum_field_types type() const final { return MYSQL_TYPE_DECIMAL; }
2065 enum ha_base_keytype key_type() const final {
2067 }
2068 type_conversion_status store(const char *to, size_t length,
2069 const CHARSET_INFO *charset) final;
2070 type_conversion_status store(double nr) final;
2071 type_conversion_status store(longlong nr, bool unsigned_val) final;
2072 double val_real() const final;
2073 longlong val_int() const final;
2074 String *val_str(String *, String *) const final;
2075 int cmp(const uchar *, const uchar *) const final;
2076 size_t make_sort_key(uchar *buff, size_t length) const final;
2077 void overflow(bool negative);
2078 bool zero_pack() const final { return false; }
2079 void sql_type(String &str) const final;
2081 assert(type() == MYSQL_TYPE_DECIMAL);
2082 return new (mem_root) Field_decimal(*this);
2083 }
2084 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final {
2085 return Field::unpack(to, from, param_data);
2086 }
2087 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2088 return Field::pack(to, from, max_length);
2089 }
2090};
2091
2092/* New decimal/numeric field which use fixed point arithmetic */
2094 private:
2095 /**
2096 Normally, the underlying decimal code will degrade values' excessive
2097 precision. E.g. value 0.0 stored as decimal(10,4) will be returned as
2098 decimal(4,4). This is fine for general purpose, but isn't usable for
2099 multi-valued index. Field_typed_array uses a field for conversion and it
2100 expects the value read from it to be exactly same as it would be stored
2101 in SE, i.e with preserved precision. Otherwise, SE won't be able to
2102 index it.
2103 TRUE here tells underlying DECIMAL reading code to keep the precision as
2104 is.
2105 */
2106 bool m_keep_precision{false};
2107 int do_save_field_metadata(uchar *first_byte) const final;
2108
2109 public:
2110 /* The maximum number of decimal digits can be stored */
2113 /*
2114 Constructors take max_length of the field as a parameter - not the
2115 precision as the number of decimal digits allowed.
2116 So for example we need to count length from precision handling
2117 CREATE TABLE ( DECIMAL(x,y))
2118 */
2119 Field_new_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2120 uchar null_bit_arg, uchar auto_flags_arg,
2121 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2122 bool unsigned_arg);
2123 Field_new_decimal(uint32 len_arg, bool is_nullable_arg,
2124 const char *field_name_arg, uint8 dec_arg,
2125 bool unsigned_arg);
2127 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_BINARY; }
2128 Item_result result_type() const final { return DECIMAL_RESULT; }
2130 type_conversion_status store_value(const my_decimal *decimal_value);
2131 void set_value_on_overflow(my_decimal *decimal_value, bool sign) const;
2132 type_conversion_status store(const char *to, size_t length,
2133 const CHARSET_INFO *charset) final;
2134 type_conversion_status store(double nr) final;
2135 type_conversion_status store(longlong nr, bool unsigned_val) final;
2138 double val_real() const final;
2139 longlong val_int() const final;
2140 my_decimal *val_decimal(my_decimal *) const final;
2141 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
2142 bool get_time(MYSQL_TIME *ltime) const final;
2143 String *val_str(String *, String *) const final;
2144 int cmp(const uchar *, const uchar *) const final;
2145 size_t make_sort_key(uchar *buff, size_t length) const final;
2146 bool zero_pack() const final { return false; }
2147 void sql_type(String &str) const final;
2148 uint32 max_display_length() const final { return field_length; }
2149 uint32 pack_length() const final { return (uint32)bin_size; }
2150 uint pack_length_from_metadata(uint field_metadata) const final;
2151 bool compatible_field_size(uint field_metadata, Relay_log_info *, uint16,
2152 int *order_var) const final;
2153 uint is_equal(const Create_field *new_field) const final;
2155 assert(type() == MYSQL_TYPE_NEWDECIMAL);
2156 return new (mem_root) Field_new_decimal(*this);
2157 }
2158 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final;
2159 static Field *create_from_item(const Item *item);
2160 bool send_to_protocol(Protocol *protocol) const final;
2161 void set_keep_precision(bool arg) { m_keep_precision = arg; }
2162};
2163
2164class Field_tiny : public Field_num {
2165 public:
2166 Field_tiny(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2167 uchar null_bit_arg, uchar auto_flags_arg,
2168 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2169 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2170 field_name_arg, 0, zero_arg, unsigned_arg) {}
2171 Field_tiny(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2172 bool unsigned_arg)
2173 : Field_num(nullptr, len_arg,
2174 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2175 field_name_arg, 0, false, unsigned_arg) {}
2176 enum Item_result result_type() const final { return INT_RESULT; }
2177 enum_field_types type() const override { return MYSQL_TYPE_TINY; }
2178 enum ha_base_keytype key_type() const final {
2180 }
2181 type_conversion_status store(const char *to, size_t length,
2182 const CHARSET_INFO *charset) override;
2183 type_conversion_status store(double nr) override;
2184 type_conversion_status store(longlong nr, bool unsigned_val) override;
2185 double val_real() const override;
2186 longlong val_int() const override;
2187 String *val_str(String *, String *) const override;
2188 bool send_to_protocol(Protocol *protocol) const override;
2189 int cmp(const uchar *, const uchar *) const final;
2190 size_t make_sort_key(uchar *buff, size_t length) const final;
2191 uint32 pack_length() const final { return 1; }
2192 void sql_type(String &str) const override;
2193 uint32 max_display_length() const final { return 4; }
2194 Field_tiny *clone(MEM_ROOT *mem_root) const override {
2195 assert(type() == MYSQL_TYPE_TINY);
2196 return new (mem_root) Field_tiny(*this);
2197 }
2198 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2199 if (max_length > 0) *to = *from;
2200 return to + 1;
2201 }
2202
2203 const uchar *unpack(uchar *to, const uchar *from,
2204 uint param_data [[maybe_unused]]) final {
2205 *to = *from;
2206 return from + 1;
2207 }
2208
2210 return is_unsigned() ? 0xFFULL : 0x7FULL;
2211 }
2212};
2213
2214class Field_short final : public Field_num {
2215 public:
2216 Field_short(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2217 uchar null_bit_arg, uchar auto_flags_arg,
2218 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2219 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2220 field_name_arg, 0, zero_arg, unsigned_arg) {}
2221 Field_short(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2222 bool unsigned_arg)
2223 : Field_num(nullptr, len_arg,
2224 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2225 field_name_arg, 0, false, unsigned_arg) {}
2226 Field_short(uint32 len_arg, const char *field_name_arg, bool unsigned_arg)
2227 : Field_short(len_arg, false, field_name_arg, unsigned_arg) {}
2228 enum Item_result result_type() const final { return INT_RESULT; }
2229 enum_field_types type() const final { return MYSQL_TYPE_SHORT; }
2230 enum ha_base_keytype key_type() const final {
2232 }
2233 type_conversion_status store(const char *to, size_t length,
2234 const CHARSET_INFO *charset) final;
2235 type_conversion_status store(double nr) final;
2236 type_conversion_status store(longlong nr, bool unsigned_val) final;
2237 double val_real() const final;
2238 longlong val_int() const final;
2239 String *val_str(String *, String *) const final;
2240 bool send_to_protocol(Protocol *protocol) const final;
2241 int cmp(const uchar *, const uchar *) const final;
2242 size_t make_sort_key(uchar *buff, size_t length) const final;
2243 uint32 pack_length() const final { return 2; }
2244 void sql_type(String &str) const final;
2245 uint32 max_display_length() const final { return 6; }
2247 assert(type() == MYSQL_TYPE_SHORT);
2248 return new (mem_root) Field_short(*this);
2249 }
2250 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2251 return pack_int16(to, from, max_length);
2252 }
2253
2254 const uchar *unpack(uchar *to, const uchar *from,
2255 uint param_data [[maybe_unused]]) final {
2256 return unpack_int16(to, from);
2257 }
2258
2260 return is_unsigned() ? 0xFFFFULL : 0x7FFFULL;
2261 }
2262};
2263
2264class Field_medium final : public Field_num {
2265 public:
2266 Field_medium(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2267 uchar null_bit_arg, uchar auto_flags_arg,
2268 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2269 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2270 field_name_arg, 0, zero_arg, unsigned_arg) {}
2271 Field_medium(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2272 bool unsigned_arg)
2273 : Field_num(nullptr, len_arg,
2274 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2275 field_name_arg, 0, false, unsigned_arg) {}
2276 enum Item_result result_type() const final { return INT_RESULT; }
2277 enum_field_types type() const final { return MYSQL_TYPE_INT24; }
2278 enum ha_base_keytype key_type() const final {
2280 }
2281 type_conversion_status store(const char *to, size_t length,
2282 const CHARSET_INFO *charset) final;
2283 type_conversion_status store(double nr) final;
2284 type_conversion_status store(longlong nr, bool unsigned_val) final;
2285 double val_real() const final;
2286 longlong val_int() const final;
2287 String *val_str(String *, String *) const final;
2288 bool send_to_protocol(Protocol *protocol) const final;
2289 int cmp(const uchar *, const uchar *) const final;
2290 size_t make_sort_key(uchar *buff, size_t length) const final;
2291 uint32 pack_length() const final { return 3; }
2292 void sql_type(String &str) const final;
2293 uint32 max_display_length() const final { return 8; }
2295 assert(type() == MYSQL_TYPE_INT24);
2296 return new (mem_root) Field_medium(*this);
2297 }
2299 return is_unsigned() ? 0xFFFFFFULL : 0x7FFFFFULL;
2300 }
2301};
2302
2303class Field_long : public Field_num {
2304 public:
2305 static const int PACK_LENGTH = 4;
2306
2307 Field_long(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2308 uchar null_bit_arg, uchar auto_flags_arg,
2309 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2310 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2311 field_name_arg, 0, zero_arg, unsigned_arg) {}
2312 Field_long(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2313 bool unsigned_arg)
2314 : Field_num(nullptr, len_arg,
2315 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2316 field_name_arg, 0, false, unsigned_arg) {}
2317 enum Item_result result_type() const final { return INT_RESULT; }
2318 enum_field_types type() const final { return MYSQL_TYPE_LONG; }
2319 enum ha_base_keytype key_type() const final {
2321 }
2322 type_conversion_status store(const char *to, size_t length,
2323 const CHARSET_INFO *charset) final;
2324 type_conversion_status store(double nr) final;
2325 type_conversion_status store(longlong nr, bool unsigned_val) override;
2326 double val_real() const final;
2327 longlong val_int() const final;
2328 bool send_to_protocol(Protocol *protocol) const final;
2329 String *val_str(String *, String *) const final;
2330 int cmp(const uchar *, const uchar *) const final;
2331 size_t make_sort_key(uchar *buff, size_t length) const final;
2332 uint32 pack_length() const final { return PACK_LENGTH; }
2333 void sql_type(String &str) const final;
2336 }
2338 assert(type() == MYSQL_TYPE_LONG);
2339 return new (mem_root) Field_long(*this);
2340 }
2341 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2342 return pack_int32(to, from, max_length);
2343 }
2344 const uchar *unpack(uchar *to, const uchar *from,
2345 uint param_data [[maybe_unused]]) final {
2346 return unpack_int32(to, from);
2347 }
2348
2350 return is_unsigned() ? 0xFFFFFFFFULL : 0x7FFFFFFFULL;
2351 }
2352};
2353
2355 public:
2356 static const int PACK_LENGTH = 8;
2357
2358 Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2359 uchar null_bit_arg, uchar auto_flags_arg,
2360 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2361 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2362 field_name_arg, 0, zero_arg, unsigned_arg) {}
2363 Field_longlong(uint32 len_arg, bool is_nullable_arg,
2364 const char *field_name_arg, bool unsigned_arg)
2365 : Field_num(nullptr, len_arg,
2366 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2367 field_name_arg, 0, false, unsigned_arg) {}
2368 enum Item_result result_type() const final { return INT_RESULT; }
2370 enum ha_base_keytype key_type() const final {
2372 }
2373 type_conversion_status store(const char *to, size_t length,
2374 const CHARSET_INFO *charset) final;
2375 type_conversion_status store(double nr) final;
2376 type_conversion_status store(longlong nr, bool unsigned_val) override;
2377 double val_real() const final;
2378 longlong val_int() const override;
2379 String *val_str(String *, String *) const final;
2380 bool send_to_protocol(Protocol *protocol) const final;
2381 int cmp(const uchar *, const uchar *) const final;
2382 size_t make_sort_key(uchar *buff, size_t length) const final;
2383 uint32 pack_length() const final { return PACK_LENGTH; }
2384 void sql_type(String &str) const final;
2385 bool can_be_compared_as_longlong() const final { return true; }
2386 uint32 max_display_length() const final { return 20; }
2388 assert(type() == MYSQL_TYPE_LONGLONG);
2389 return new (mem_root) Field_longlong(*this);
2390 }
2391 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2392 return pack_int64(to, from, max_length);
2393 }
2394 const uchar *unpack(uchar *to, const uchar *from,
2395 uint param_data [[maybe_unused]]) final {
2396 return unpack_int64(to, from);
2397 }
2398
2400 return is_unsigned() ? 0xFFFFFFFFFFFFFFFFULL : 0x7FFFFFFFFFFFFFFFULL;
2401 }
2402};
2403
2404class Field_float final : public Field_real {
2405 public:
2406 Field_float(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2407 uchar null_bit_arg, uchar auto_flags_arg,
2408 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2409 bool unsigned_arg)
2410 : Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2411 field_name_arg, dec_arg, zero_arg, unsigned_arg) {}
2412 Field_float(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2413 uint8 dec_arg, bool unsigned_arg)
2414 : Field_real(nullptr, len_arg,
2415 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2416 field_name_arg, dec_arg, false, unsigned_arg) {}
2417 enum_field_types type() const final { return MYSQL_TYPE_FLOAT; }
2418 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_FLOAT; }
2419 type_conversion_status store(const char *to, size_t length,
2420 const CHARSET_INFO *charset) final;
2421 type_conversion_status store(double nr) final;
2422 type_conversion_status store(longlong nr, bool unsigned_val) final;
2423 double val_real() const final;
2424 longlong val_int() const final;
2425 String *val_str(String *, String *) const final;
2426 bool send_to_protocol(Protocol *protocol) const final;
2427 int cmp(const uchar *, const uchar *) const final;
2428 size_t make_sort_key(uchar *buff, size_t length) const final;
2429 uint32 pack_length() const final { return sizeof(float); }
2430 void sql_type(String &str) const final;
2432 assert(type() == MYSQL_TYPE_FLOAT);
2433 return new (mem_root) Field_float(*this);
2434 }
2435
2437 /*
2438 We use the maximum as per IEEE754-2008 standard, 2^24
2439 */
2440 return 0x1000000ULL;
2441 }
2442
2443 private:
2444 int do_save_field_metadata(uchar *first_byte) const final;
2445};
2446
2447class Field_double final : public Field_real {
2448 public:
2449 Field_double(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2450 uchar null_bit_arg, uchar auto_flags_arg,
2451 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2452 bool unsigned_arg)
2453 : Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2454 field_name_arg, dec_arg, zero_arg, unsigned_arg) {}
2455 Field_double(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2456 uint8 dec_arg)
2457 : Field_real(nullptr, len_arg,
2458 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2459 field_name_arg, dec_arg, false, false) {}
2460 Field_double(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2461 uint8 dec_arg, bool unsigned_arg)
2462 : Field_real(nullptr, len_arg,
2463 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2464 field_name_arg, dec_arg, false, unsigned_arg) {}
2465 Field_double(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2466 uint8 dec_arg, bool unsigned_arg, bool not_fixed_arg)
2467 : Field_real(nullptr, len_arg,
2468 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2469 field_name_arg, dec_arg, false, unsigned_arg) {
2470 not_fixed = not_fixed_arg;
2471 }
2472 enum_field_types type() const final { return MYSQL_TYPE_DOUBLE; }
2473 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_DOUBLE; }
2474 type_conversion_status store(const char *to, size_t length,
2475 const CHARSET_INFO *charset) final;
2476 type_conversion_status store(double nr) final;
2477 type_conversion_status store(longlong nr, bool unsigned_val) final;
2478 double val_real() const final;
2479 longlong val_int() const final;
2480 String *val_str(String *, String *) const final;
2481 bool send_to_protocol(Protocol *protocol) const final;
2482 int cmp(const uchar *, const uchar *) const final;
2483 size_t make_sort_key(uchar *buff, size_t length) const final;
2484 uint32 pack_length() const final { return sizeof(double); }
2485 void sql_type(String &str) const final;
2487 assert(type() == MYSQL_TYPE_DOUBLE);
2488 return new (mem_root) Field_double(*this);
2489 }
2490
2492 /*
2493 We use the maximum as per IEEE754-2008 standard, 2^53
2494 */
2495 return 0x20000000000000ULL;
2496 }
2497
2498 private:
2499 int do_save_field_metadata(uchar *first_byte) const final;
2500};
2501
2502/* Everything saved in this will disappear. It will always return NULL */
2503
2504class Field_null final : public Field_str {
2505 public:
2506 Field_null(uchar *ptr_arg, uint32 len_arg, uchar auto_flags_arg,
2507 const char *field_name_arg, const CHARSET_INFO *cs)
2508 // (dummy_null_buffer & 32) is true, so is_null() always returns true.
2509 : Field_str(ptr_arg, len_arg, &dummy_null_buffer, 32, auto_flags_arg,
2510 field_name_arg, cs) {}
2511 enum_field_types type() const final { return MYSQL_TYPE_NULL; }
2512 type_conversion_status store(const char *, size_t,
2513 const CHARSET_INFO *) final {
2514 return TYPE_OK;
2515 }
2516 type_conversion_status store(double) final { return TYPE_OK; }
2519 return TYPE_OK;
2520 }
2522 double val_real() const final { return 0.0; }
2523 longlong val_int() const final { return 0; }
2524 my_decimal *val_decimal(my_decimal *) const final { return nullptr; }
2525 String *val_str(String *, String *value2) const final {
2526 value2->length(0);
2527 return value2;
2528 }
2529 int cmp(const uchar *, const uchar *) const final { return 0; }
2530 size_t make_sort_key(uchar *, size_t len) const final { return len; }
2531 uint32 pack_length() const final { return 0; }
2532 void sql_type(String &str) const final;
2533 uint32 max_display_length() const final { return 4; }
2535 assert(type() == MYSQL_TYPE_NULL);
2536 return new (mem_root) Field_null(*this);
2537 }
2538};
2539
2540/*
2541 Abstract class for TIME, DATE, DATETIME, TIMESTAMP
2542 with and without fractional part.
2543*/
2544class Field_temporal : public Field {
2545 protected:
2546 uint8 dec; // Number of fractional digits
2547
2548 /**
2549 Adjust number of decimal digits from DECIMAL_NOT_SPECIFIED to
2550 DATETIME_MAX_DECIMALS
2551 */
2552 static uint8 normalize_dec(uint8 dec_arg) {
2553 return dec_arg == DECIMAL_NOT_SPECIFIED ? DATETIME_MAX_DECIMALS : dec_arg;
2554 }
2555
2556 /**
2557 Low level routine to store a MYSQL_TIME value into a field.
2558 The value must be already properly rounded or truncated
2559 and checked for being a valid TIME/DATE/DATETIME value.
2560
2561 @param[in] ltime MYSQL_TIME value.
2562 @param[out] error Error flag vector, set in case of error.
2563 @retval false In case of success.
2564 @retval true In case of error.
2565 */
2567 int *error) = 0;
2568
2569 /**
2570 Low level routine to store a MYSQL_TIME value into a field
2571 with rounding/truncation according to the field decimals() value and
2572 sql_mode.
2573
2574 @param[in] ltime MYSQL_TIME value.
2575 @param[out] warnings Error flag vector, set in case of error.
2576 @retval false In case of success.
2577 @retval true In case of error.
2578 */
2580 int *warnings) = 0;
2581
2582 /**
2583 Store a temporal value in lldiv_t into a field,
2584 with rounding according to the field decimals() value.
2585
2586 @param[in] lld Temporal value.
2587 @param[out] warning Warning flag vector.
2588 @retval false In case of success.
2589 @retval true In case of error.
2590 */
2591 type_conversion_status store_lldiv_t(const lldiv_t *lld, int *warning);
2592
2593 /**
2594 Convert a string to MYSQL_TIME, according to the field type.
2595
2596 @param[in] str String
2597 @param[in] len String length
2598 @param[in] cs String character set
2599 @param[out] ltime The value is stored here
2600 @param[out] status Conversion status
2601 @retval false Conversion went fine, ltime contains a valid time
2602 @retval true Conversion failed, ltime was reset and contains nothing
2603 */
2604 virtual bool convert_str_to_TIME(const char *str, size_t len,
2605 const CHARSET_INFO *cs, MYSQL_TIME *ltime,
2607 /**
2608 Convert a number with fractional part with nanosecond precision
2609 into MYSQL_TIME, according to the field type. Nanoseconds
2610 are rounded to milliseconds and added to ltime->second_part.
2611
2612 @param[in] nr Number
2613 @param[in] unsigned_val SIGNED/UNSIGNED flag
2614 @param[in] nanoseconds Fractional part in nanoseconds
2615 @param[out] ltime The value is stored here
2616 @param[in,out] warning Warnings found during execution
2617
2618 @return Conversion status
2619 @retval false On success
2620 @retval true On error
2621 */
2623 bool unsigned_val,
2624 int nanoseconds,
2625 MYSQL_TIME *ltime,
2626 int *warning) = 0;
2627
2628 /**
2629 Convert an integer number into MYSQL_TIME, according to the field type.
2630
2631 @param[in] nr Number
2632 @param[in] unsigned_val SIGNED/UNSIGNED flag
2633 @param[out] ltime The value is stored here
2634 @param[in,out] warnings Warnings found during execution
2635
2636 @retval false On success
2637 @retval true On error
2638 */
2639 longlong convert_number_to_datetime(longlong nr, bool unsigned_val,
2640 MYSQL_TIME *ltime, int *warnings);
2641
2642 /**
2643 Set warnings from a warning vector.
2644 Note, multiple warnings can be set at the same time.
2645 Every warning in the bit vector is set by an individual
2646 set_datetime_warning() call.
2647
2648 @param str Value.
2649 @param warnings Warning vector.
2650
2651 @retval false Function reported warning
2652 @retval true Function reported error
2653
2654 @note STRICT mode can convert warnings to error.
2655 */
2656 [[nodiscard]] bool set_warnings(const ErrConvString &str, int warnings);
2657
2658 /**
2659 Flags that are passed as "flag" argument to
2660 check_date(), number_to_datetime(), str_to_datetime().
2661
2662 Flags depend on the session sql_mode settings, such as
2663 MODE_NO_ZERO_DATE, MODE_NO_ZERO_IN_DATE.
2664 Also, Field_newdate, Field_datetime, Field_datetimef add TIME_FUZZY_DATE
2665 to the session sql_mode settings, to allow relaxed date format,
2666 while Field_timestamp, Field_timestampf do not.
2667
2668 @param thd THD
2669 @retval sql_mode flags mixed with the field type flags.
2670 */
2671 virtual my_time_flags_t date_flags(const THD *thd [[maybe_unused]]) const {
2672 return 0;
2673 }
2674
2675 /**
2676 Flags that are passed as "flag" argument to
2677 check_date(), number_to_datetime(), str_to_datetime().
2678 Similar to the above when we don't have a THD value.
2679 */
2680 my_time_flags_t date_flags() const;
2681
2682 /**
2683 Produce warning or note about double datetime data saved into field.
2684
2685 @param level level of message (Note/Warning/Error)
2686 @param code error code of message to be produced
2687 @param val error parameter (the value)
2688 @param ts_type type of datetime value (datetime/date/time)
2689 @param truncate_increment whether we should increase truncated fields count
2690
2691 @retval false Function reported warning
2692 @retval true Function reported error
2693
2694 @note
2695 This function will always produce some warning but won't increase
2696 truncated fields counter if check_for_truncated_fields == FIELD_CHECK_IGNORE
2697 for current thread.
2698 */
2699 [[nodiscard]] bool set_datetime_warning(
2700 Sql_condition::enum_severity_level level, uint code,
2701 const ErrConvString &val, enum_mysql_timestamp_type ts_type,
2702 int truncate_increment);
2703
2704 public:
2705 /**
2706 Constructor for Field_temporal
2707 @param ptr_arg See Field definition
2708 @param null_ptr_arg See Field definition
2709 @param null_bit_arg See Field definition
2710 @param auto_flags_arg See Field definition
2711 @param field_name_arg See Field definition
2712 @param len_arg Number of characters in the integer part.
2713 @param dec_arg Number of second fraction digits, 0..6.
2714 */
2715 Field_temporal(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2716 uchar auto_flags_arg, const char *field_name_arg,
2717 uint32 len_arg, uint8 dec_arg)
2718 : Field(ptr_arg,
2719 len_arg +
2720 ((normalize_dec(dec_arg)) ? normalize_dec(dec_arg) + 1 : 0),
2721 null_ptr_arg, null_bit_arg, auto_flags_arg, field_name_arg) {
2723 dec = normalize_dec(dec_arg);
2724 }
2725 Item_result result_type() const final { return STRING_RESULT; }
2726 uint32 max_display_length() const final { return field_length; }
2727 bool str_needs_quotes() const final { return true; }
2728 uint is_equal(const Create_field *new_field) const final;
2730 return dec ? DECIMAL_RESULT : INT_RESULT;
2731 }
2732 enum Item_result cmp_type() const final { return INT_RESULT; }
2733 enum Derivation derivation() const final { return DERIVATION_NUMERIC; }
2734 uint repertoire() const final { return MY_REPERTOIRE_NUMERIC; }
2735 const CHARSET_INFO *charset() const final { return &my_charset_numeric; }
2736 bool can_be_compared_as_longlong() const final { return true; }
2737 bool binary() const final { return true; }
2738 type_conversion_status store(const char *str, size_t len,
2739 const CHARSET_INFO *cs) final;
2740 type_conversion_status store_decimal(const my_decimal *decimal) final;
2741 type_conversion_status store(longlong nr, bool unsigned_val) override;
2742 type_conversion_status store(double nr) final;
2743 double val_real() const override // FSP-enable types redefine it.
2744 {
2745 return (double)val_int();
2746 }
2747 [[nodiscard]] uint8 get_dec() const { return dec; }
2749 my_decimal *decimal_value) const override; // FSP types redefine it
2750};
2751
2752/**
2753 Abstract class for types with date
2754 with optional time, with or without fractional part:
2755 DATE, DATETIME, DATETIME(N), TIMESTAMP, TIMESTAMP(N).
2756*/
2758 protected:
2759 /**
2760 Low level function to get value into MYSQL_TIME,
2761 without checking for being valid.
2762 */
2763 virtual bool get_date_internal(MYSQL_TIME *ltime) const = 0;
2764
2765 virtual bool get_date_internal_at_utc(MYSQL_TIME *ltime) const {
2766 return get_date_internal(ltime);
2767 }
2768
2769 /**
2770 Get value into MYSQL_TIME and check TIME_NO_ZERO_DATE flag.
2771 @retval True on error: we get a zero value but flags disallow zero dates.
2772 @retval False on success.
2773 */
2774 bool get_internal_check_zero(MYSQL_TIME *ltime,
2775 my_time_flags_t fuzzydate) const;
2776
2777 type_conversion_status convert_number_to_TIME(longlong nr, bool unsigned_val,
2778 int nanoseconds,
2779 MYSQL_TIME *ltime,
2780 int *warning) final;
2781 bool convert_str_to_TIME(const char *str, size_t len, const CHARSET_INFO *cs,
2782 MYSQL_TIME *ltime, MYSQL_TIME_STATUS *status) final;
2783
2784 type_conversion_status store_internal_adjust_frac(MYSQL_TIME *ltime,
2785 int *warnings) final;
2787
2788 public:
2789 /**
2790 Constructor for Field_temporal
2791 @param ptr_arg See Field definition
2792 @param null_ptr_arg See Field definition
2793 @param null_bit_arg See Field definition
2794 @param auto_flags_arg See Field definition
2795 @param field_name_arg See Field definition
2796 @param int_length_arg Number of characters in the integer part.
2797 @param dec_arg Number of second fraction digits, 0..6.
2798 */
2799 Field_temporal_with_date(uchar *ptr_arg, uchar *null_ptr_arg,
2800 uchar null_bit_arg, uchar auto_flags_arg,
2801 const char *field_name_arg, uint8 int_length_arg,
2802 uint8 dec_arg)
2803 : Field_temporal(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2804 field_name_arg, int_length_arg, dec_arg) {}
2805 bool send_to_protocol(Protocol *protocol) const override;
2807 String *val_str(String *, String *) const override;
2808 longlong val_time_temporal() const override;
2809 longlong val_date_temporal() const override;
2810 longlong val_time_temporal_at_utc() const override;
2811 longlong val_date_temporal_at_utc() const override;
2812 bool get_time(MYSQL_TIME *ltime) const final {
2813 return get_date(ltime, TIME_FUZZY_DATE);
2814 }
2815 /* Validate the value stored in a field */
2817};
2818
2819/**
2820 Abstract class for types with date and time,
2821 with or without fractional part:
2822 DATETIME, DATETIME(N), TIMESTAMP, TIMESTAMP(N).
2823*/
2825 private:
2826 int do_save_field_metadata(uchar *metadata_ptr) const override {
2827 if (decimals()) {
2828 *metadata_ptr = decimals();
2829 return 1;
2830 }
2831 return 0;
2832 }
2833
2834 protected:
2835 /**
2836 Initialize flags for TIMESTAMP DEFAULT CURRENT_TIMESTAMP / ON UPDATE
2837 CURRENT_TIMESTAMP columns.
2838
2839 @todo get rid of TIMESTAMP_FLAG and ON_UPDATE_NOW_FLAG.
2840 */
2841 void init_timestamp_flags();
2842 /**
2843 Store "struct timeval" value into field.
2844 The value must be properly rounded or truncated according
2845 to the number of fractional second digits.
2846 */
2847 virtual void store_timestamp_internal(const my_timeval *tm) = 0;
2848 bool convert_TIME_to_timestamp(const MYSQL_TIME *ltime, const Time_zone &tz,
2849 my_timeval *tm, int *error);
2850
2851 public:
2852 /**
2853 Constructor for Field_temporal_with_date_and_time
2854 @param ptr_arg See Field definition
2855 @param null_ptr_arg See Field definition
2856 @param null_bit_arg See Field definition
2857 @param auto_flags_arg See Field definition
2858 @param field_name_arg See Field definition
2859 @param dec_arg Number of second fraction digits, 0..6.
2860 */
2862 uchar null_bit_arg, uchar auto_flags_arg,
2863 const char *field_name_arg, uint8 dec_arg)
2864 : Field_temporal_with_date(ptr_arg, null_ptr_arg, null_bit_arg,
2865 auto_flags_arg, field_name_arg,
2866 MAX_DATETIME_WIDTH, dec_arg) {}
2867 void store_timestamp(const my_timeval *tm) override;
2868};
2869
2870/**
2871 Abstract class for types with date and time, with fractional part:
2872 DATETIME, DATETIME(N), TIMESTAMP, TIMESTAMP(N).
2873*/
2876 private:
2877 int do_save_field_metadata(uchar *metadata_ptr) const final {
2878 *metadata_ptr = decimals();
2879 return 1;
2880 }
2881
2882 public:
2883 /**
2884 Constructor for Field_temporal_with_date_and_timef
2885 @param ptr_arg See Field definition
2886 @param null_ptr_arg See Field definition
2887 @param null_bit_arg See Field definition
2888 @param auto_flags_arg See Field definition
2889 @param field_name_arg See Field definition
2890 @param dec_arg Number of second fraction digits, 0..6.
2891 */
2893 uchar null_bit_arg, uchar auto_flags_arg,
2894 const char *field_name_arg, uint8 dec_arg)
2895 : Field_temporal_with_date_and_time(ptr_arg, null_ptr_arg, null_bit_arg,
2896 auto_flags_arg, field_name_arg,
2897 dec_arg) {}
2898
2899 uint decimals() const final { return dec; }
2900 const CHARSET_INFO *sort_charset() const final { return &my_charset_bin; }
2901 size_t make_sort_key(uchar *to, size_t length) const final {
2902 memcpy(to, ptr, length);
2903 return length;
2904 }
2905 int cmp(const uchar *a_ptr, const uchar *b_ptr) const final {
2906 return memcmp(a_ptr, b_ptr, pack_length());
2907 }
2908 uint row_pack_length() const final { return pack_length(); }
2909 double val_real() const final;
2910 longlong val_int() const final;
2911 my_decimal *val_decimal(my_decimal *decimal_value) const final;
2912};
2913
2914/*
2915 Field implementing TIMESTAMP data type without fractional seconds.
2916 We will be removed eventually.
2917*/
2919 protected:
2920 my_time_flags_t date_flags(const THD *thd) const final;
2921 type_conversion_status store_internal(const MYSQL_TIME *ltime,
2922 int *error) final;
2923 bool get_date_internal(MYSQL_TIME *ltime) const final;
2924 bool get_date_internal_at_utc(MYSQL_TIME *ltime) const final;
2925 void store_timestamp_internal(const my_timeval *tm) final;
2926
2927 public:
2928 static const int PACK_LENGTH = 4;
2929 Field_timestamp(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2930 uchar null_bit_arg, uchar auto_flags_arg,
2931 const char *field_name_arg);
2932 Field_timestamp(bool is_nullable_arg, const char *field_name_arg);
2934 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_ULONG_INT; }
2936 longlong val_int() const final;
2937 int cmp(const uchar *, const uchar *) const final;
2938 size_t make_sort_key(uchar *buff, size_t length) const final;
2939 uint32 pack_length() const final { return PACK_LENGTH; }
2940 void sql_type(String &str) const final;
2941 bool zero_pack() const final { return false; }
2942 /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
2943 bool get_timestamp(my_timeval *tm, int *warnings) const final;
2944 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
2946 assert(type() == MYSQL_TYPE_TIMESTAMP);
2947 return new (mem_root) Field_timestamp(*this);
2948 }
2949 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2950 return pack_int32(to, from, max_length);
2951 }
2952 const uchar *unpack(uchar *to, const uchar *from,
2953 uint param_data [[maybe_unused]]) final {
2954 return unpack_int32(to, from);
2955 }
2956 /* Validate the value stored in a field */
2958
2959 private:
2960 /**
2961 Retrieves a value from a record, without checking fuzzy date flags.
2962
2963 @param tz The time zone to convert to
2964 @param[out] ltime The timestamp value in the time zone.
2965
2966 @retval true Means that the timestamp value read is 0. ltime is not touched
2967 in this case.
2968 @retval false If timestamp is non-zero.
2969 */
2970 bool get_date_internal_at(const Time_zone *tz, MYSQL_TIME *ltime) const;
2971};
2972
2973/*
2974 Field implementing TIMESTAMP(N) data type, where N=0..6.
2975*/
2977 protected:
2978 bool get_date_internal(MYSQL_TIME *ltime) const final;
2979 bool get_date_internal_at_utc(MYSQL_TIME *ltime) const final;
2980 type_conversion_status store_internal(const MYSQL_TIME *ltime,
2981 int *error) final;
2982 my_time_flags_t date_flags(const THD *thd) const final;
2983 void store_timestamp_internal(const my_timeval *tm) override;
2984
2985 public:
2986 /**
2987 Field_timestampf constructor
2988 @param ptr_arg See Field definition
2989 @param null_ptr_arg See Field definition
2990 @param null_bit_arg See Field definition
2991 @param auto_flags_arg See Field definition
2992 @param field_name_arg See Field definition
2993 @param dec_arg Number of fractional second digits, 0..6.
2994 */
2995 Field_timestampf(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2996 uchar auto_flags_arg, const char *field_name_arg,
2997 uint8 dec_arg);
2998 /**
2999 Field_timestampf constructor
3000 @param is_nullable_arg See Field definition
3001 @param field_name_arg See Field definition
3002 @param dec_arg Number of fractional second digits, 0..6.
3003 */
3004 Field_timestampf(bool is_nullable_arg, const char *field_name_arg,
3005 uint8 dec_arg);
3007 assert(type() == MYSQL_TYPE_TIMESTAMP);
3008 return new (mem_root) Field_timestampf(*this);
3009 }
3010
3014 bool zero_pack() const final { return false; }
3015
3017 uint pack_length_from_metadata(uint field_metadata) const final {
3018 DBUG_TRACE;
3019 const uint tmp = my_timestamp_binary_length(field_metadata);
3020 return tmp;
3021 }
3022
3024 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
3025 void sql_type(String &str) const final;
3026
3027 bool get_timestamp(my_timeval *tm, int *warnings) const final;
3028 /* Validate the value stored in a field */
3030
3031 private:
3032 /**
3033 Retrieves a value from a record, without checking fuzzy date flags.
3034
3035 @param tz The time zone to convert to
3036 @param[out] ltime The timestamp value in the time zone.
3037
3038 @retval true Means that the timestamp value read is 0. ltime is not touched
3039 in this case.
3040 @retval false If timestamp is non-zero.
3041 */
3042 bool get_date_internal_at(const Time_zone *tz, MYSQL_TIME *ltime) const;
3043};
3044
3045class Field_year final : public Field_tiny {
3046 public:
3047 enum Limits { MIN_YEAR = 1901, MAX_YEAR = 2155 };
3048 Field_year(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3049 uchar auto_flags_arg, const char *field_name_arg)
3050 : Field_tiny(ptr_arg, 4, null_ptr_arg, null_bit_arg, auto_flags_arg,
3051 field_name_arg, true, true) {}
3052 Field_year(bool is_nullable_arg, const char *field_name_arg)
3053 : Field_tiny(nullptr, 4, is_nullable_arg ? &dummy_null_buffer : nullptr,
3054 0, NONE, field_name_arg, true, true) {}
3055 enum_field_types type() const final { return MYSQL_TYPE_YEAR; }
3056 type_conversion_status store(const char *to, size_t length,
3057 const CHARSET_INFO *charset) final;
3058 type_conversion_status store(double nr) final;
3059 type_conversion_status store(longlong nr, bool unsigned_val) final;
3061 double val_real() const final;
3062 longlong val_int() const final;
3063 String *val_str(String *, String *) const final;
3064 bool send_to_protocol(Protocol *protocol) const final;
3065 void sql_type(String &str) const final;
3066 bool can_be_compared_as_longlong() const final { return true; }
3068 assert(type() == MYSQL_TYPE_YEAR);
3069 return new (mem_root) Field_year(*this);
3070 }
3071};
3072
3074 protected:
3075 static const int PACK_LENGTH = 3;
3076 my_time_flags_t date_flags(const THD *thd) const final;
3077 bool get_date_internal(MYSQL_TIME *ltime) const final;
3078 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3079 int *error) final;
3080
3081 public:
3082 Field_newdate(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3083 uchar auto_flags_arg, const char *field_name_arg)
3084 : Field_temporal_with_date(ptr_arg, null_ptr_arg, null_bit_arg,
3085 auto_flags_arg, field_name_arg, MAX_DATE_WIDTH,
3086 0) {}
3087 Field_newdate(bool is_nullable_arg, const char *field_name_arg)
3089 is_nullable_arg ? &dummy_null_buffer : nullptr,
3090 0, NONE, field_name_arg, MAX_DATE_WIDTH, 0) {}
3091 enum_field_types type() const final { return MYSQL_TYPE_DATE; }
3093 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_UINT24; }
3095 longlong val_int() const final;
3096 longlong val_time_temporal() const final;
3097 longlong val_date_temporal() const final;
3098 String *val_str(String *, String *) const final;
3099 bool send_to_protocol(Protocol *protocol) const final;
3100 int cmp(const uchar *, const uchar *) const final;
3101 size_t make_sort_key(uchar *buff, size_t length) const final;
3102 uint32 pack_length() const final { return PACK_LENGTH; }
3103 void sql_type(String &str) const final;
3104 bool zero_pack() const final { return true; }
3105 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
3107 assert(type() == MYSQL_TYPE_DATE);
3108 assert(real_type() == MYSQL_TYPE_NEWDATE);
3109 return new (mem_root) Field_newdate(*this);
3110 }
3111};
3112
3113/**
3114 Abstract class for TIME and TIME(N).
3115*/
3117 protected:
3118 bool convert_str_to_TIME(const char *str, size_t len, const CHARSET_INFO *cs,
3119 MYSQL_TIME *ltime, MYSQL_TIME_STATUS *status) final;
3120 /**
3121 @todo: convert_number_to_TIME returns conversion status through
3122 two different interfaces: return value and warning. It should be
3123 refactored to only use return value.
3124 */
3125 type_conversion_status convert_number_to_TIME(longlong nr, bool unsigned_val,
3126 int nanoseconds,
3127 MYSQL_TIME *ltime,
3128 int *warning) final;
3129 /**
3130 Low-level function to store MYSQL_TIME value.
3131 The value must be rounded or truncated according to decimals().
3132 */
3134 int *error) override = 0;
3135 /**
3136 Function to store time value.
3137 The value is rounded/truncated according to decimals() and sql_mode.
3138 */
3139 type_conversion_status store_internal_adjust_frac(MYSQL_TIME *ltime,
3140 int *warnings) final;
3141
3142 my_time_flags_t date_flags(const THD *thd) const final;
3144
3145 public:
3146 /**
3147 Constructor for Field_time_common
3148 @param ptr_arg See Field definition
3149 @param null_ptr_arg See Field definition
3150 @param null_bit_arg See Field definition
3151 @param auto_flags_arg See Field definition
3152 @param field_name_arg See Field definition
3153 @param dec_arg Number of second fraction digits, 0..6.
3154 */
3155 Field_time_common(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3156 uchar auto_flags_arg, const char *field_name_arg,
3157 uint8 dec_arg)
3158 : Field_temporal(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3159 field_name_arg, MAX_TIME_WIDTH, dec_arg) {}
3161 String *val_str(String *, String *) const final;
3162 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
3163 longlong val_date_temporal() const final;
3164 bool send_to_protocol(Protocol *protocol) const final;
3165};
3166
3167/*
3168 Field implementing TIME data type without fractional seconds.
3169 It will be removed eventually.
3170*/
3171class Field_time final : public Field_time_common {
3172 protected:
3173 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3174 int *error) final;
3175
3176 public:
3177 Field_time(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3178 uchar auto_flags_arg, const char *field_name_arg)
3179 : Field_time_common(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3180 field_name_arg, 0) {}
3181 Field_time(const char *field_name_arg)
3182 : Field_time_common(nullptr, nullptr, 0, NONE, field_name_arg, 0) {}
3183 enum_field_types type() const final { return MYSQL_TYPE_TIME; }
3184 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_INT24; }
3186 longlong val_int() const final;
3187 longlong val_time_temporal() const final;
3188 bool get_time(MYSQL_TIME *ltime) const final;
3189 int cmp(const uchar *, const uchar *) const final;
3190 size_t make_sort_key(uchar *buff, size_t length) const final;
3191 uint32 pack_length() const final { return 3; }
3192 void sql_type(String &str) const final;
3193 bool zero_pack() const final { return true; }
3195 assert(type() == MYSQL_TYPE_TIME);
3196 return new (mem_root) Field_time(*this);
3197 }
3198};
3199
3200/*
3201 Field implementing TIME(N) data type, where N=0..6.
3202*/
3203class Field_timef final : public Field_time_common {
3204 private:
3205 int do_save_field_metadata(uchar *metadata_ptr) const final {
3206 *metadata_ptr = decimals();
3207 return 1;
3208 }
3209
3210 protected:
3211 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3212 int *error) final;
3213
3214 public:
3215 /**
3216 Constructor for Field_timef
3217 @param ptr_arg See Field definition
3218 @param null_ptr_arg See Field definition
3219 @param null_bit_arg See Field definition
3220 @param auto_flags_arg See Field definition
3221 @param field_name_arg See Field definition
3222 @param dec_arg Number of second fraction digits, 0..6.
3223 */
3224 Field_timef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3225 uchar auto_flags_arg, const char *field_name_arg, uint8 dec_arg)
3226 : Field_time_common(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3227 field_name_arg, dec_arg) {}
3228 /**
3229 Constructor for Field_timef
3230 @param is_nullable_arg See Field definition
3231 @param field_name_arg See Field definition
3232 @param dec_arg Number of second fraction digits, 0..6.
3233 */
3234 Field_timef(bool is_nullable_arg, const char *field_name_arg, uint8 dec_arg)
3236 is_nullable_arg ? &dummy_null_buffer : nullptr, 0,
3237 NONE, field_name_arg, dec_arg) {}
3239 assert(type() == MYSQL_TYPE_TIME);
3240 return new (mem_root) Field_timef(*this);
3241 }
3242 uint decimals() const final { return dec; }
3243 enum_field_types type() const final { return MYSQL_TYPE_TIME; }
3248 double val_real() const final;
3249 longlong val_int() const final;
3250 longlong val_time_temporal() const final;
3251 bool get_time(MYSQL_TIME *ltime) const final;
3252 my_decimal *val_decimal(my_decimal *) const final;
3253 uint32 pack_length() const final { return my_time_binary_length(dec); }
3254 uint pack_length_from_metadata(uint field_metadata) const final {
3255 DBUG_TRACE;
3256 const uint tmp = my_time_binary_length(field_metadata);
3257 return tmp;
3258 }
3259 uint row_pack_length() const final { return pack_length(); }
3260 void sql_type(String &str) const final;
3261 bool zero_pack() const final { return true; }
3262 const CHARSET_INFO *sort_charset() const final { return &my_charset_bin; }
3263 size_t make_sort_key(uchar *to, size_t length) const final {
3264 memcpy(to, ptr, length);
3265 return length;
3266 }
3267 int cmp(const uchar *a_ptr, const uchar *b_ptr) const final {
3268 return memcmp(a_ptr, b_ptr, pack_length());
3269 }
3270};
3271
3272/*
3273 Field implementing DATETIME data type without fractional seconds.
3274 We will be removed eventually.
3275*/
3277 protected:
3278 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3279 int *error) final;
3280 bool get_date_internal(MYSQL_TIME *ltime) const final;
3281 my_time_flags_t date_flags(const THD *thd) const final;
3282 void store_timestamp_internal(const my_timeval *tm) final;
3283
3284 public:
3285 static const int PACK_LENGTH = 8;
3286
3287 /**
3288 DATETIME columns can be defined as having CURRENT_TIMESTAMP as the
3289 default value on inserts or updates. This constructor accepts a
3290 auto_flags argument which controls the column default expressions.
3291
3292 For DATETIME columns this argument is a bitmap combining two flags:
3293
3294 - DEFAULT_NOW - means that column has DEFAULT CURRENT_TIMESTAMP attribute.
3295 - ON_UPDATE_NOW - means that column has ON UPDATE CURRENT_TIMESTAMP.
3296
3297 (these two flags can be used orthogonally to each other).
3298 */
3299 Field_datetime(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3300 uchar auto_flags_arg, const char *field_name_arg)
3301 : Field_temporal_with_date_and_time(ptr_arg, null_ptr_arg, null_bit_arg,
3302 auto_flags_arg, field_name_arg, 0) {}
3303 Field_datetime(const char *field_name_arg)
3305 field_name_arg, 0) {}
3307 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_ULONGLONG; }
3308 using Field_temporal_with_date_and_time::store; // Make -Woverloaded-virtual
3309 type_conversion_status store(longlong nr, bool unsigned_val) final;
3311 longlong val_int() const final;
3312 String *val_str(String *, String *) const final;
3313 int cmp(const uchar *, const uchar *) const final;
3314 size_t make_sort_key(uchar *buff, size_t length) const final;
3315 uint32 pack_length() const final { return PACK_LENGTH; }
3316 void sql_type(String &str) const final;
3317 bool zero_pack() const final { return true; }
3318 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
3320 assert(type() == MYSQL_TYPE_DATETIME);
3321 return new (mem_root) Field_datetime(*this);
3322 }
3323 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
3324 return pack_int64(to, from, max_length);
3325 }
3326 const uchar *unpack(uchar *to, const uchar *from,
3327 uint param_data [[maybe_unused]]) final {
3328 return unpack_int64(to, from);
3329 }
3330};
3331
3332/*
3333 Field implementing DATETIME(N) data type, where N=0..6.
3334*/
3336 protected:
3337 bool get_date_internal(MYSQL_TIME *ltime) const final;
3338 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3339 int *error) final;
3340 my_time_flags_t date_flags(const THD *thd) const final;
3341 void store_timestamp_internal(const my_timeval *tm) final;
3342
3343 public:
3344 /**
3345 Constructor for Field_datetimef
3346 @param ptr_arg See Field definition
3347 @param null_ptr_arg See Field definition
3348 @param null_bit_arg See Field definition
3349 @param auto_flags_arg See Field definition
3350 @param field_name_arg See Field definition
3351 @param dec_arg Number of second fraction digits, 0..6.
3352 */
3353 Field_datetimef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3354 uchar auto_flags_arg, const char *field_name_arg,
3355 uint8 dec_arg)
3356 : Field_temporal_with_date_and_timef(ptr_arg, null_ptr_arg, null_bit_arg,
3357 auto_flags_arg, field_name_arg,
3358 dec_arg) {}
3359 /**
3360 Constructor for Field_datetimef
3361 @param is_nullable_arg See Field definition
3362 @param field_name_arg See Field definition
3363 @param dec_arg Number of second fraction digits, 0..6.
3364 */
3365 Field_datetimef(bool is_nullable_arg, const char *field_name_arg,
3366 uint8 dec_arg)
3368 nullptr, is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
3369 field_name_arg, dec_arg) {}
3371 assert(type() == MYSQL_TYPE_DATETIME);
3372 return new (mem_root) Field_datetimef(*this);
3373 }
3374
3379 uint pack_length_from_metadata(uint field_metadata) const final {
3380 DBUG_TRACE;
3381 const uint tmp = my_datetime_binary_length(field_metadata);
3382 return tmp;
3383 }
3384 bool zero_pack() const final { return true; }
3385
3388 longlong val_date_temporal() const final;
3389 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
3390 void sql_type(String &str) const final;
3391};
3392
3394 public:
3395 Field_string(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
3396 uchar null_bit_arg, uchar auto_flags_arg,
3397 const char *field_name_arg, const CHARSET_INFO *cs)
3398 : Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
3399 auto_flags_arg, field_name_arg, cs) {}
3400 Field_string(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
3401 const CHARSET_INFO *cs)
3402 : Field_longstr(nullptr, len_arg,
3403 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
3404 field_name_arg, cs) {}
3405
3406 enum_field_types type() const final { return MYSQL_TYPE_STRING; }
3407 bool match_collation_to_optimize_range() const final { return true; }
3408 enum ha_base_keytype key_type() const final {
3410 }
3411 bool zero_pack() const final { return false; }
3413 charset()->cset->fill(charset(), (char *)ptr, field_length,
3414 (has_charset() ? ' ' : 0));
3415 return TYPE_OK;
3416 }
3417 type_conversion_status store(const char *to, size_t length,
3418 const CHARSET_INFO *charset) final;
3419 type_conversion_status store(longlong nr, bool unsigned_val) final;
3420 // Inherit the store() overloads that have not been overridden.
3422 double val_real() const final;
3423 longlong val_int() const final;
3424 String *val_str(String *, String *) const final;
3425 /**
3426 Get the C-string value, without using String class.
3427 @returns The C-string value of this field.
3428 */
3429 LEX_CSTRING val_str_quick() const {
3430 const char *string = pointer_cast<const char *>(ptr);
3431 return {string,
3433 }
3434 my_decimal *val_decimal(my_decimal *) const final;
3435 int cmp(const uchar *, const uchar *) const final;
3436 size_t make_sort_key(uchar *buff, size_t length) const final;
3437 void sql_type(String &str) const final;
3438 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final;
3439 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final;
3440 uint pack_length_from_metadata(uint field_metadata) const final {
3441 DBUG_PRINT("debug", ("field_metadata: 0x%04x", field_metadata));
3442 if (field_metadata == 0) return row_pack_length();
3443 return (((field_metadata >> 4) & 0x300) ^ 0x300) +
3444 (field_metadata & 0x00ff);
3445 }
3446 bool compatible_field_size(uint field_metadata, Relay_log_info *rli,
3447 uint16 mflags, int *order_var) const final;
3448 uint row_pack_length() const final { return field_length; }
3449 uint max_packed_col_length() const final;
3451 bool has_charset() const final {
3452 return charset() == &my_charset_bin ? false : true;
3453 }
3455 assert(real_type() == MYSQL_TYPE_STRING);
3456 return new (mem_root) Field_string(*this);
3457 }
3458 size_t get_key_image(uchar *buff, size_t length, imagetype type) const final;
3459 bool is_text_key_type() const final { return binary() ? false : true; }
3460
3461 private:
3462 int do_save_field_metadata(uchar *first_byte) const final;
3463};
3464
3466 public:
3467 Field_varstring(uchar *ptr_arg, uint32 len_arg, uint length_bytes_arg,
3468 uchar *null_ptr_arg, uchar null_bit_arg, uchar auto_flags_arg,
3469 const char *field_name_arg, TABLE_SHARE *share,
3470 const CHARSET_INFO *cs);
3471 Field_varstring(uint32 len_arg, bool is_nullable_arg,
3472 const char *field_name_arg, TABLE_SHARE *share,
3473 const CHARSET_INFO *cs);
3474
3475 enum_field_types type() const final { return MYSQL_TYPE_VARCHAR; }
3476 bool match_collation_to_optimize_range() const final { return true; }
3477 enum ha_base_keytype key_type() const final;
3478 uint row_pack_length() const final { return field_length; }
3479 bool zero_pack() const final { return false; }
3480 uint32 pack_length() const final {
3481 return (uint32)field_length + length_bytes;
3482 }
3483 uint32 key_length() const final { return (uint32)field_length; }
3484 type_conversion_status store(const char *to, size_t length,
3485 const CHARSET_INFO *charset) override;
3486 type_conversion_status store(longlong nr, bool unsigned_val) final;
3487 // Inherit the store() overloads that have not been overridden.
3489 double val_real() const final;
3490 longlong val_int() const final;
3491 String *val_str(String *, String *) const override;
3492 my_decimal *val_decimal(my_decimal *) const final;
3493 int cmp_max(const uchar *, const uchar *, uint max_length) const final;
3494 int cmp(const uchar *a, const uchar *b) const final {
3495 return cmp_max(a, b, ~0U);
3496 }
3497 size_t make_sort_key(uchar *buff, size_t length) const final;
3498 size_t get_key_image(uchar *buff, size_t length, imagetype type) const final;
3499 void set_key_image(const uchar *buff, size_t length) final;
3500 void sql_type(String &str) const final;
3501 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final;
3502 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final;
3503 int cmp_binary(const uchar *a, const uchar *b,
3504 uint32 max_length = ~0L) const final;
3505 int key_cmp(const uchar *, const uchar *) const final;
3506 int key_cmp(const uchar *str, uint length) const final;
3507
3508 uint32 data_length(ptrdiff_t row_offset = 0) const final;
3510 bool has_charset() const final {
3511 return charset() == &my_charset_bin ? false : true;
3512 }
3513 Field *new_field(MEM_ROOT *root, TABLE *new_table) const final;
3514 Field *new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr,
3515 uchar *new_null_ptr, uint new_null_bit) const final;
3517 assert(type() == MYSQL_TYPE_VARCHAR);
3518 assert(real_type() == MYSQL_TYPE_VARCHAR);
3519 return new (mem_root) Field_varstring(*this);
3520 }
3521 uint is_equal(const Create_field *new_field) const final;
3522 void hash(ulong *nr, ulong *nr2) const final;
3523 const uchar *data_ptr() const final { return ptr + length_bytes; }
3524 bool is_text_key_type() const final { return binary() ? false : true; }
3525 uint32 get_length_bytes() const override { return length_bytes; }
3526
3527 private:
3528 /* Store number of bytes used to store length (1 or 2) */
3530
3531 int do_save_field_metadata(uchar *first_byte) const final;
3532};
3533
3535 virtual type_conversion_status store_internal(const char *from, size_t length,
3536 const CHARSET_INFO *cs);
3537 /**
3538 Copy value to memory storage.
3539 */
3540 type_conversion_status store_to_mem(const char *from, size_t length,
3541 const CHARSET_INFO *cs, size_t max_length,
3543
3544 protected:
3545 /**
3546 The number of bytes used to represent the length of the blob.
3547 */
3549
3550 /**
3551 The 'value'-object is a cache fronting the storage engine.
3552 */
3554
3555 private:
3556 /**
3557 In order to support update of virtual generated columns of blob type,
3558 we need to allocate the space blob needs on server for old_row and
3559 new_row respectively. This variable is used to record the
3560 allocated blob space for old_row.
3561 */
3563
3564 /**
3565 Whether we need to move the content of 'value' to 'old_value' before
3566 updating the BLOB stored in 'value'. This needs to be done for
3567 updates of BLOB columns that are virtual since the storage engine
3568 does not have its own copy of the old 'value'. This variable is set
3569 to true when we read the data into 'value'. It is reset when we move
3570 'value' to 'old_value'. The purpose of having this is to avoid that we
3571 do the move operation from 'value' to 'old_value' more than one time per
3572 record.
3573 Currently, this variable is introduced because the following call in
3574 sql_data_change.cc:
3575 \/\**
3576 @todo combine this call to update_generated_write_fields() with the one
3577 in fill_record() to avoid updating virtual generated fields twice.
3578 *\/
3579 if (table->has_gcol())
3580 update_generated_write_fields(table->write_set, table);
3581 When the @todo is done, m_keep_old_value can be deleted.
3582 */
3584
3585 /**
3586 Backup String for table's blob fields.
3587 UPDATE of a virtual field (for index update) requires two values to be
3588 kept at the same time - 'new' and 'old' since SE (InnoDB) doesn't know the
3589 latter. In the case when there was an indexed record, it got deleted and
3590 When INSERT inserts into an index a record that coincides with a
3591 previously deleted one, InnoDB needs to recalculate value that was
3592 deleted in order to properly insert the new one.
3593 When two above overlap, a field have to keep 3 different values at the
3594 same time - 'new', 'old' and 'deleted'.
3595 This backup_value is used by @see my_eval_gcolumn_expr_helper() to save
3596 'new' and provide space for 'deleted' to avoid thrashing the former.
3597 Unlike the old_value, backup_value is allocated once and reused for each
3598 new re-calculation, to avoid excessive [re-]allocations. It's freed at the
3599 end of statement. Since InnoDB consumes calculated values only after all
3600 needed table's virtual fields were calculated, we have to have such backup
3601 buffer for each field.
3602 */
3604
3605#ifndef NDEBUG
3606 /**
3607 Whether the field uses table's backup value storage. @see
3608 TABLE::m_blob_backup. Used only for debug.
3609 */
3610 bool m_uses_backup{false};
3611#endif
3612
3613 protected:
3614 /**
3615 Store ptr and length.
3616 */
3617 void store_ptr_and_length(const char *from, uint32 length) {
3618 store_length(length);
3619 memmove(ptr + packlength, &from, sizeof(char *));
3620 }
3621
3622 public:
3623 Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3624 uchar auto_flags_arg, const char *field_name_arg,
3625 TABLE_SHARE *share, uint blob_pack_length, const CHARSET_INFO *cs);
3626
3627 Field_blob(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
3628 const CHARSET_INFO *cs, bool set_packlength)
3629 : Field_longstr(nullptr, len_arg,
3630 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
3631 field_name_arg, cs),
3632 packlength(4),
3633 m_keep_old_value(false) {
3635 if (set_packlength) {
3636 packlength = len_arg <= 255
3637 ? 1
3638 : len_arg <= 65535 ? 2 : len_arg <= 16777215 ? 3 : 4;
3639 }
3640 }
3641
3642 /// Copy static information and reset dynamic information.
3644 : Field_longstr(field),
3645 packlength(field.packlength),
3646 value(),
3647 old_value(),
3648 m_keep_old_value(field.m_keep_old_value),
3649 m_blob_backup() {
3650#ifndef NDEBUG
3651 m_uses_backup = field.m_uses_backup;
3652#endif
3653 }
3654
3655 explicit Field_blob(uint32 packlength_arg);
3656
3657 /* Note that the default copy constructor is used, in clone() */
3658 enum_field_types type() const override { return MYSQL_TYPE_BLOB; }
3659 bool match_collation_to_optimize_range() const override { return true; }
3660 enum ha_base_keytype key_type() const override {
3662 }
3663 type_conversion_status store(const char *to, size_t length,
3664 const CHARSET_INFO *charset) override;
3665 type_conversion_status store(double nr) override;
3666 type_conversion_status store(longlong nr, bool unsigned_val) override;
3667 type_conversion_status store(const Field *from);
3668 double val_real() const override;
3669 longlong val_int() const override;
3670 String *val_str(String *, String *) const override;
3671 my_decimal *val_decimal(my_decimal *) const override;
3672 int cmp_max(const uchar *, const uchar *, uint max_length) const final;
3673 int cmp(const uchar *a, const uchar *b) const final {
3674 return cmp_max(a, b, ~0U);
3675 }
3676 int cmp(const uchar *a, uint32 a_length, const uchar *b,
3677 uint32 b_length) const; // No override.
3678 int cmp_binary(const uchar *a, const uchar *b,
3679 uint32 max_length = ~0L) const override;
3680 int key_cmp(const uchar *, const uchar *) const override;
3681 int key_cmp(const uchar *str, uint length) const override;
3682 uint32 key_length() const override { return 0; }
3683 size_t make_sort_key(uchar *buff, size_t length) const override;
3684 uint32 pack_length() const final {
3685 return (uint32)(packlength + portable_sizeof_char_ptr);
3686 }
3687
3688 /**
3689 Return the packed length without the pointer size added.
3690
3691 This is used to determine the size of the actual data in the row
3692 buffer.
3693
3694 @returns The length of the raw data itself without the pointer.
3695 */
3696 uint32 pack_length_no_ptr() const { return (uint32)(packlength); }
3697 uint row_pack_length() const final { return pack_length_no_ptr(); }
3698 uint32 max_data_length() const final {
3699 return (uint32)(((ulonglong)1 << (packlength * 8)) - 1);
3700 }
3701 size_t get_field_buffer_size() { return value.alloced_length(); }
3702 void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number);
3703 inline void store_length(uint32 number) {
3704 store_length(ptr, packlength, number);
3705 }
3706 uint32 data_length(ptrdiff_t row_offset = 0) const final {
3707 return get_length(row_offset);
3708 }
3709 uint32 get_length(ptrdiff_t row_offset = 0) const;
3710 uint32 get_length(const uchar *ptr, uint packlength) const;
3711 uint32 get_length(const uchar *ptr_arg) const;
3712 /** Get a const pointer to the BLOB data of this field. */
3713 const uchar *get_blob_data() const { return get_blob_data(ptr + packlength); }
3714 /** Get a non-const pointer to the BLOB data of this field. */
3715 uchar *get_blob_data(ptrdiff_t row_offset = 0) {
3716 // row_offset is only used by NDB
3717 return get_blob_data(ptr + packlength + row_offset);
3718 }
3719 /** Get a const pointer to the BLOB data of this field. */
3720 const uchar *data_ptr() const final { return get_blob_data(); }
3721
3722 protected:
3723 /**
3724 Get the BLOB data pointer stored at the specified position in the record
3725 buffer.
3726 */
3727 static uchar *get_blob_data(const uchar *position) {
3728 uchar *data;
3729 memcpy(&data, position, sizeof(data));
3730 return data;
3731 }
3732
3733 public:
3734 void set_ptr(const uchar *length, const uchar *data) {
3735 memcpy(ptr, length, packlength);
3736 memcpy(ptr + packlength, &data, sizeof(char *));
3737 }
3738 void set_ptr_offset(ptrdiff_t ptr_diff, uint32 length, const uchar *data) {
3739 uchar *ptr_ofs = ptr + ptr_diff;
3740 store_length(ptr_ofs, packlength, length);
3741 memcpy(ptr_ofs + packlength, &data, sizeof(char *));
3742 }
3743 void set_ptr(uint32 length, const uchar *data) {
3744 set_ptr_offset(0, length, data);
3745 }
3746 size_t get_key_image(uchar *buff, size_t length,
3747 imagetype type) const override;
3748 void set_key_image(const uchar *buff, size_t length) final;
3749 void sql_type(String &str) const override;
3750 bool copy();
3751 Field_blob *clone(MEM_ROOT *mem_root) const override {
3752 assert(type() == MYSQL_TYPE_BLOB);
3753 return new (mem_root) Field_blob(*this);
3754 }
3755 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final;
3756 uchar *pack_with_metadata_bytes(uchar *to, const uchar *from,
3757 uint max_length) const final;
3758 const uchar *unpack(uchar *, const uchar *from, uint param_data) final;
3759 uint max_packed_col_length() const final;
3760 void mem_free() final {
3761 // Free all allocated space
3762 value.mem_free();
3763 old_value.mem_free();
3764 m_blob_backup.mem_free();
3765 }
3766 bool has_charset() const override {
3767 return charset() == &my_charset_bin ? false : true;
3768 }
3769 uint32 max_display_length() const final;
3770 uint32 char_length() const override;
3771 bool copy_blob_value(MEM_ROOT *mem_root);
3772 uint is_equal(const Create_field *new_field) const override;
3773 bool is_text_key_type() const final { return binary() ? false : true; }
3774
3775 /**
3776 Mark that the BLOB stored in value should be copied before updating it.
3777
3778 When updating virtual generated columns we need to keep the old
3779 'value' for BLOBs since this can be needed when the storage engine
3780 does the update. During read of the record the old 'value' for the
3781 BLOB is evaluated and stored in 'value'. This function is to be used
3782 to specify that we need to copy this BLOB 'value' into 'old_value'
3783 before we compute the new BLOB 'value'. For more information @see
3784 Field_blob::keep_old_value().
3785 */
3786 void set_keep_old_value(bool old_value_flag) {
3787 /*
3788 We should only need to keep a copy of the blob 'value' in the case
3789 where this is a virtual generated column (that is indexed).
3790 */
3791 assert(is_virtual_gcol());
3792
3793 /*
3794 If set to true, ensure that 'value' is copied to 'old_value' when
3795 keep_old_value() is called.
3796 */
3797 m_keep_old_value = old_value_flag;
3798 }
3799
3800 /**
3801 Save the current BLOB value to avoid that it gets overwritten.
3802
3803 This is used when updating virtual generated columns that are
3804 BLOBs. Some storage engines require that we have both the old and
3805 new BLOB value for virtual generated columns that are indexed in
3806 order for the storage engine to be able to maintain the index. This
3807 function will transfer the buffer storing the current BLOB value
3808 from 'value' to 'old_value'. This avoids that the current BLOB value
3809 is over-written when the new BLOB value is saved into this field.
3810
3811 The reason this requires special handling when updating/deleting
3812 virtual columns of BLOB type is that the BLOB value is not known to
3813 the storage engine. For stored columns, the "old" BLOB value is read
3814 by the storage engine, Field_blob is made to point to the engine's
3815 internal buffer; Field_blob's internal buffer (Field_blob::value)
3816 isn't used and remains available to store the "new" value. For
3817 virtual generated columns, the "old" value is written directly into
3818 Field_blob::value when reading the record to be
3819 updated/deleted. This is done in update_generated_read_fields().
3820 Since, in this case, the "old" value already occupies the place to
3821 store the "new" value, we must call this function before we write
3822 the "new" value into Field_blob::value object so that the "old"
3823 value does not get over-written. The table->record[1] buffer will
3824 have a pointer that points to the memory buffer inside
3825 old_value. The storage engine will use table->record[1] to read the
3826 old value for the BLOB and use table->record[0] to read the new
3827 value.
3828
3829 This function must be called before we store the new BLOB value in
3830 this field object.
3831 */
3833 /*
3834 We should only need to keep a copy of the blob value in the case
3835 where this is a virtual generated column (that is indexed).
3836 */
3837 assert(is_virtual_gcol());
3838
3839 // Transfer ownership of the current BLOB value to old_value
3840 if (m_keep_old_value) {
3841 old_value.takeover(value);
3842 m_keep_old_value = false;
3843 }
3844 }
3845
3846 /**
3847 Use to store the blob value into an allocated space.
3848 */
3849 void store_in_allocated_space(const char *from, uint32 length) {
3850 store_ptr_and_length(from, length);
3851 }
3852
3853 /**
3854 Backup data stored in 'value' into the backup_value
3855 @see Field_blob::backup_value
3856
3857 @returns
3858 true if backup fails
3859 false otherwise
3860 */
3861 bool backup_blob_field();
3862
3863 /**
3864 Restore backup value
3865 @see Field_blob::backup_value
3866 */
3867 void restore_blob_backup();
3868
3869 private:
3870 int do_save_field_metadata(uchar *first_byte) const override;
3871};
3872
3873class Field_geom final : public Field_blob {
3874 private:
3875 const std::optional<gis::srid_t> m_srid;
3876
3877 type_conversion_status store_internal(const char *from, size_t length,
3878 const CHARSET_INFO *cs) final;
3879
3880 public:
3882
3883 Field_geom(uchar *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
3884 uchar auto_flags_arg, const char *field_name_arg,
3885 TABLE_SHARE *share, uint blob_pack_length,
3886 enum geometry_type geom_type_arg, std::optional<gis::srid_t> srid)
3887 : Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3888 field_name_arg, share, blob_pack_length, &my_charset_bin),
3889 m_srid(srid),
3890 geom_type(geom_type_arg) {}
3891 Field_geom(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
3892 enum geometry_type geom_type_arg, std::optional<gis::srid_t> srid)
3893 : Field_blob(len_arg, is_nullable_arg, field_name_arg, &my_charset_bin,
3894 false),
3895 m_srid(srid),
3896 geom_type(geom_type_arg) {}
3897 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_VARBINARY2; }
3899 bool match_collation_to_optimize_range() const final { return false; }
3900 void sql_type(String &str) const final;
3901 using Field_blob::store;
3902 type_conversion_status store(double nr) final;
3903 type_conversion_status store(longlong nr, bool unsigned_val) final;
3905 type_conversion_status store(const char *from, size_t length,
3906 const CHARSET_INFO *cs) final;
3907
3908 /**
3909 Non-nullable GEOMETRY types cannot have defaults,
3910 but the underlying blob must still be reset.
3911 */
3914 if (res != TYPE_OK) return res;
3915 return (is_nullable() || table->is_nullable())
3916 ? TYPE_OK
3918 }
3919
3920 geometry_type get_geometry_type() const final { return geom_type; }
3922 assert(type() == MYSQL_TYPE_GEOMETRY);
3923 return new (mem_root) Field_geom(*this);
3924 }
3925 uint is_equal(const Create_field *new_field) const final;
3926
3927 std::optional<gis::srid_t> get_srid() const { return m_srid; }
3928};
3929
3930/// A field that stores a JSON value.
3931class Field_json : public Field_blob {
3932 type_conversion_status unsupported_conversion();
3933 type_conversion_status store_binary(const char *ptr, size_t length);
3934
3935 public:
3936 Field_json(uchar *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
3937 uchar auto_flags_arg, const char *field_name_arg,
3938 TABLE_SHARE *share, uint blob_pack_length)
3939 : Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3940 field_name_arg, share, blob_pack_length, &my_charset_bin) {}
3941
3942 Field_json(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg)
3943 : Field_blob(len_arg, is_nullable_arg, field_name_arg, &my_charset_bin,
3944 false) {}
3945
3946 enum_field_types type() const override { return MYSQL_TYPE_JSON; }
3947 void sql_type(String &str) const override;
3948 /**
3949 Return a text charset so that string functions automatically
3950 convert the field value to string and treat it as a non-binary
3951 string.
3952 */
3953 const CHARSET_INFO *charset() const override {
3954 return &my_charset_utf8mb4_bin;
3955 }
3956 /**
3957 Sort should treat the field as binary and not attempt any
3958 conversions.
3959 */
3960 const CHARSET_INFO *sort_charset() const final { return field_charset; }
3961 /**
3962 JSON columns don't have an associated charset. Returning false
3963 here prevents SHOW CREATE TABLE from attaching a CHARACTER SET
3964 clause to the column.
3965 */
3966 bool has_charset() const final { return false; }
3967 type_conversion_status store(const char *to, size_t length,
3968 const CHARSET_INFO *charset) override;
3969 type_conversion_status store(double nr) override;
3970 type_conversion_status store(longlong nr, bool unsigned_val) override;
3972 type_conversion_status store_json(const Json_wrapper *json);
3973 type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec_arg) final;
3975
3976 bool pack_diff(uchar **to, ulonglong value_options) const final;
3977 /**
3978 Return the length of this field, taking into consideration that it may be in
3979 partial format.
3980
3981 This is the format used when writing the binary log in row format
3982 and using a partial format according to
3983 @@session.binlog_row_value_options.
3984
3985 @param[in] value_options The value of binlog_row_value options.
3986
3987 @param[out] diff_vector_p If this is not NULL, the pointer it
3988 points to will be set to NULL if the field is to be stored in full
3989 format, or to the Json_diff_vector if the field is to be stored in
3990 partial format.
3991
3992 @return The number of bytes needed when writing to the binlog: the
3993 size of the full format if stored in full format and the size of
3994 the diffs if stored in partial format.
3995 */
3996 longlong get_diff_vector_and_length(
3997 ulonglong value_options,
3998 const Json_diff_vector **diff_vector_p = nullptr) const;
3999 /**
4000 Return true if the before-image and after-image for this field are
4001 equal.
4002 */
4003 bool is_before_image_equal_to_after_image() const;
4004 /**
4005 Read the binary diff from the given buffer, and apply it to this field.
4006
4007 @param[in,out] from Pointer to buffer where the binary diff is stored.
4008 This will be changed to point to the next byte after the field.
4009
4010 @retval false Success
4011 @retval true Error (e.g. failed to apply the diff). The error has
4012 been reported through my_error.
4013 */
4014 bool unpack_diff(const uchar **from);
4015
4016 /**
4017 Retrieve the field's value as a JSON wrapper. It
4018 there is an error, wr is not modified and we return
4019 false, else true.
4020
4021 @param[out] wr the JSON value
4022 @return true if a value is retrieved (or NULL), false if error
4023 */
4024 bool val_json(Json_wrapper *wr) const;
4025
4026 /**
4027 Retrieve the JSON as an int if possible. This requires a JSON scalar
4028 of suitable type.
4029
4030 @returns the JSON value as an int
4031 */
4032 longlong val_int() const final;
4033
4034 /**
4035 Retrieve the JSON as a double if possible. This requires a JSON scalar
4036 of suitable type.
4037
4038 @returns the JSON value as a double
4039 */
4040 double val_real() const final;
4041
4042 /**
4043 Retrieve the JSON value stored in this field as text
4044
4045 @param[in,out] buf1 string buffer for converting JSON value to string
4046 @param[in,out] buf2 unused
4047 */
4048 String *val_str(String *buf1, String *buf2) const final;
4049 my_decimal *val_decimal(my_decimal *m) const final;
4050 bool get_time(MYSQL_TIME *ltime) const final;
4051 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
4052 Field_json *clone(MEM_ROOT *mem_root) const override;
4053 uint is_equal(const Create_field *new_field) const final;
4054 Item_result cast_to_int_type() const final { return INT_RESULT; }
4055 int cmp_binary(const uchar *a, const uchar *b,
4056 uint32 max_length = ~0L) const final;
4057 size_t make_sort_key(uchar *to, size_t length) const override;
4058
4059 /**
4060 Make a hash key that can be used by sql_executor.cc/unique_hash
4061 in order to support SELECT DISTINCT
4062
4063 @param[in] hash_val An initial hash value.
4064 */
4065 ulonglong make_hash_key(ulonglong hash_val) const;
4066
4067 /**
4068 Get a read-only pointer to the binary representation of the JSON document
4069 in this field.
4070
4071 @param row_offset Field's data offset
4072 */
4073 const char *get_binary(ptrdiff_t row_offset = 0) const;
4074};
4075
4076/**
4077 Field that stores array of values of the same type.
4078
4079 This Field class is used together with Item_func_array_cast class
4080 (CAST( .. AS .. ARRAY) function) in implementation of multi-valued index.
4081 Effectively it's a JSON field that contains a single JSON array. When a
4082 JSON value is stored, it's checked to be either a scalar, or an array.
4083 All source values are converted using the internal conversion field and
4084 stored as an array. Field_typed_array ensures that all values stored
4085 in the array have the same type and precision - the one specified by user.
4086 This way InnoDB doesn't have to do the conversion on its own and can easily
4087 index them.
4088
4089 The Field_typed_array always reports type of its element and from this
4090 point of view it's undistinguishable from regular field having the same
4091 type. Due to that, fields are differentiated by is_array() property.
4092 Field_typed_array returns true, all other fields - false.
4093
4094 For conversion and index applicability tests, Field_typed_array employs a
4095 conversion field, which is a regular Field class of array's element type.
4096 It's stored in the m_conv_field. All Field_typed_array::store_*() methods
4097 store values to the conversion field. Conversion field and typed array
4098 field are sharing same field_index, to allow correct read/write_set
4099 checks. So the field always have to be marked for read in order to allow
4100 read of conversions' results.
4101
4102 @see Item_func_array_cast
4103*/
4104
4105class Field_typed_array final : public Field_json {
4106 /// Conversion item_field
4107 Item_field *m_conv_item{nullptr};
4108 /// The array element's real type.
4110 /// Element's decimals
4112 /// Element's charset
4114 const bool unsigned_flag;
4115
4116 public:
4117 /**
4118 Constructs a Field_typed_array that is a copy of another Field_typed_array.
4119 @param other the other Field_typed_array object
4120 */
4122 /**
4123 Constructs a Field_typed_array object.
4124 */
4125 Field_typed_array(enum_field_types elt_type, bool elt_is_unsigned,
4126 size_t elt_length, uint elt_decimals, uchar *ptr_arg,
4127 uchar *null_ptr_arg, uint null_bit_arg,
4128 uchar auto_flags_arg, const char *field_name_arg,
4129 TABLE_SHARE *share, uint blob_pack_length,
4130 const CHARSET_INFO *cs);
4131 uint32 char_length() const override {
4132 return field_length / charset()->mbmaxlen;
4133 }
4134 void init(TABLE *table_arg) override;
4135 enum_field_types type() const override {
4136 return real_type_to_type(m_elt_type);
4137 }
4138 enum_field_types real_type() const override { return m_elt_type; }
4141 }
4142 uint32 key_length() const override;
4143 Field_typed_array *clone(MEM_ROOT *mem_root) const override;
4144 bool is_unsigned() const final { return unsigned_flag; }
4145 bool is_array() const override { return true; }
4146 Item_result result_type() const override;
4147 uint decimals() const override { return m_elt_decimals; }
4148 bool binary() const override {
4149 return (m_elt_type != MYSQL_TYPE_VARCHAR ||
4150 m_elt_charset == &my_charset_bin);
4151 }
4152 const CHARSET_INFO *charset() const override { return m_elt_charset; }
4153 type_conversion_status store(const char *to, size_t length,
4154 const CHARSET_INFO *charset) override;
4155 type_conversion_status store(double nr) override;
4156 type_conversion_status store(longlong nr, bool unsigned_val) override;
4157 /**
4158 Store a value as an array.
4159 @param data the value to store as an array
4160 @param array scratch space for building the array to store
4161 @return the status of the operation
4162 */
4163 type_conversion_status store_array(const Json_wrapper *data,
4164 Json_array *array);
4165 size_t get_key_image(uchar *buff, size_t length,
4166 imagetype