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