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