MySQL 8.3.0
Source Code Documentation
field.h
Go to the documentation of this file.
1#ifndef FIELD_INCLUDED
2#define FIELD_INCLUDED
3
4/* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is also distributed with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26#include <assert.h>
27#include <limits.h>
28#include <stddef.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <sys/types.h>
33
34#include <algorithm>
35#include <optional>
36
37#include "decimal.h" // E_DEC_OOM
38#include "field_types.h" // enum_field_types
39#include "lex_string.h"
40#include "my_alloc.h"
41#include "my_base.h" // ha_storage_media
42#include "my_bitmap.h"
43#include "my_dbug.h"
44#include "my_double2ulonglong.h"
45#include "my_inttypes.h"
46#include "my_time.h" // MYSQL_TIME_NOTE_TRUNCATED
47#include "mysql/binlog/event/export/binary_log_funcs.h" // my_time_binary_length
48#include "mysql/strings/dtoa.h"
51#include "mysql_com.h"
52#include "mysql_time.h"
53#include "sql/dd/types/column.h"
55#include "sql/gis/srid.h"
56#include "sql/sql_bitmap.h"
57#include "sql/sql_const.h"
58#include "sql/sql_error.h" // Sql_condition
59#include "sql/table.h"
60#include "sql_string.h" // String
61#include "template_utils.h"
62
63class Create_field;
64class CostOfItem;
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 my_timeval;
110struct TYPELIB;
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 const 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; }
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 /// Update '*cost' with the fact that this Field is accessed.
744 virtual void add_to_cost(CostOfItem *cost) const;
745
746 private:
748 uint16 m_field_index; // field number in fields array
749
750 public:
751 bool is_flag_set(unsigned flag) const { return flags & flag; }
752 void set_flag(unsigned flag) { flags |= flag; }
753 void clear_flag(unsigned flag) { flags &= ~flag; }
754 // Avoid using this function as it makes it harder to change the internal
755 // representation.
756 uint32 all_flags() const { return flags; }
757 uchar null_bit; // Bit used to test null bit
758 /**
759 Bitmap of flags indicating if field value is auto-generated by default
760 and/or on update, and in which way.
761
762 @sa Field::enum_auto_flags for possible options.
763
764 @sa Field::utype and Field::unireg_check in pre-8.0 versions of server
765 for historical perspective.
766 */
768 /**
769 If true, this field was created in create_tmp_field_from_item from a NULL
770 value. This means that the type of the field is just a guess, and the type
771 may be freely coerced to another type.
772
773 @see create_tmp_field_from_item
774 @see Item_type_holder::get_real_type
775
776 */
778 /**
779 True if this field belongs to some index (unlike part_of_key, the index
780 might have only a prefix).
781 */
783
786
787 private:
792 };
793
794 /*
795 Bitmask specifying which warnings have been already pushed in order
796 not to repeat the same warning for the collmn multiple times.
797 Uses values of enum_pushed_warnings to control pushed warnings.
798 */
799 unsigned int m_warnings_pushed;
800
801 public:
802 /* Generated column data */
804 /**
805 Indication that the field is physically stored in tables
806 rather than just generated on SQL queries.
807 As of now, false can only be set for virtual generated columns.
808 */
810 /**
811 Whether the field is signed or not. Meaningful only for numeric fields
812 and numeric arrays.
813 */
814 virtual bool is_unsigned() const { return false; }
815 bool is_gcol() const { return gcol_info; }
816 bool is_virtual_gcol() const { return gcol_info && !stored_in_db; }
817
818 /// Holds the expression to be used to generate default values.
820
821 /**
822 Sets the hidden type for this field.
823
824 @param hidden the new hidden type to set.
825 */
827
828 /// @returns the hidden type for this field.
830
831 /**
832 @retval true if this field should be hidden away from users.
833 @retval false is this field is visible to the user.
834 */
835 bool is_hidden() const {
837 DBUG_EVALUATE_IF("show_hidden_columns", false, true);
838 }
839
840 /**
841 @retval true If this column is hidden either in the storage engine
842 or SQL layer. Either way, it is completely hidden from
843 the user.
844 @retval false Otherwise.
845 */
846 bool is_hidden_by_system() const {
849 DBUG_EVALUATE_IF("show_hidden_columns", false, true);
850 }
851
852 /**
853 @retval true If this column is hidden by the user.
854 @retval false otherwise.
855 */
856 bool is_hidden_by_user() const {
858 }
859
860 /**
861 @returns true if this is a hidden field that is used for implementing
862 functional indexes. Note that if we need different types of hidden
863 fields in the future (like invisible columns), this function needs
864 to be changed so it can distinguish between the different "types"
865 of hidden.
866 */
869 gcol_info != nullptr;
870 }
871
872 Field(uchar *ptr_arg, uint32 length_arg, uchar *null_ptr_arg,
873 uchar null_bit_arg, uchar auto_flags_arg, const char *field_name_arg);
874
875 virtual ~Field() = default;
876
878
879 /**
880 Turn on temporary nullability for the field.
881 */
883
884 /**
885 Turn off temporary nullability for the field.
886 */
888
889 /**
890 Reset temporary NULL value for field
891 */
892 void reset_tmp_null() { m_is_tmp_null = false; }
893
894 void set_tmp_null();
895
896 /**
897 @return temporary NULL-ability flag.
898 @retval true if NULL can be assigned temporary to the Field.
899 @retval false if NULL can not be assigned even temporary to the Field.
900 */
901 bool is_tmp_nullable() const { return m_is_tmp_nullable; }
902
903 /**
904 @return whether Field has temporary value NULL.
905 @retval true if the Field has temporary value NULL.
906 @retval false if the Field's value is NOT NULL, or if the temporary
907 NULL-ability flag is reset.
908 */
909 bool is_tmp_null() const { return is_tmp_nullable() && m_is_tmp_null; }
910
911 /* Store functions returns 1 on overflow and -1 on fatal error */
912 virtual type_conversion_status store(const char *to, size_t length,
913 const CHARSET_INFO *cs) = 0;
914 virtual type_conversion_status store(double nr) = 0;
915 virtual type_conversion_status store(longlong nr, bool unsigned_val) = 0;
916 /**
917 Store a temporal value in packed longlong format into a field.
918 The packed value is compatible with TIME_to_longlong_time_packed(),
919 TIME_to_longlong_date_packed() or TIME_to_longlong_datetime_packed().
920 Note, the value must be properly rounded or truncated according
921 according to field->decimals().
922
923 @param nr temporal value in packed longlong format.
924 @retval false on success
925 @retval true on error
926 */
928 return store(nr, false);
929 }
931 /**
932 Store MYSQL_TIME value with the given amount of decimal digits
933 into a field.
934
935 Note, the "dec" parameter represents number of digits of the Item
936 that previously created the MYSQL_TIME value. It's needed when we
937 store the value into a CHAR/VARCHAR/TEXT field to display
938 the proper amount of fractional digits.
939 For other field types the "dec" value does not matter and is ignored.
940
941 @param ltime Time, date or datetime value.
942 @param dec_arg Number of decimals in ltime.
943 @retval false on success
944 @retval true on error
945 */
946 virtual type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec_arg);
947 /**
948 Store MYSQL_TYPE value into a field when the number of fractional
949 digits is not important or is not know.
950
951 @param ltime Time, date or datetime value.
952 @retval false on success
953 @retval true on error
954 */
956 return store_time(ltime, 0);
957 }
958 type_conversion_status store(const char *to, size_t length,
959 const CHARSET_INFO *cs,
960 enum_check_fields check_level);
961 virtual double val_real() const = 0;
962 virtual longlong val_int() const = 0;
963 /**
964 Returns TIME value in packed longlong format.
965 This method should not be called for non-temporal types.
966 Temporal field types override the default method.
967 */
968 virtual longlong val_time_temporal() const {
969 assert(0);
970 return 0;
971 }
972 /**
973 Returns DATE/DATETIME value in packed longlong format.
974 This method should not be called for non-temporal types.
975 Temporal field types override the default method.
976 */
977 virtual longlong val_date_temporal() const {
978 assert(0);
979 return 0;
980 }
981
983 return val_time_temporal();
984 }
985
987 return val_date_temporal();
988 }
989
990 /**
991 Returns "native" packed longlong representation of
992 a TIME or DATE/DATETIME field depending on field type.
993 */
995 // Return longlong TIME or DATETIME representation, depending on field type
996 const enum_field_types field_type = type();
997 if (field_type == MYSQL_TYPE_TIME) return val_time_temporal();
998 assert(is_temporal_type_with_date(field_type));
999 return val_date_temporal();
1000 }
1001 virtual my_decimal *val_decimal(my_decimal *) const = 0;
1002 String *val_str(String *str) const { return val_str(str, str); }
1003 /*
1004 val_str(buf1, buf2) gets two buffers and should use them as follows:
1005 if it needs a temp buffer to convert result to string - use buf1
1006 example Field_tiny::val_str()
1007 if the value exists as a string already - use buf2
1008 example Field_string::val_str()
1009 consequently, buf2 may be created as 'String buf;' - no memory
1010 will be allocated for it. buf1 will be allocated to hold a
1011 value if it's too small. Using allocated buffer for buf2 may result in
1012 an unnecessary free (and later, may be an alloc).
1013 This trickery is used to decrease a number of malloc calls.
1014 */
1015 virtual String *val_str(String *, String *) const = 0;
1016 String *val_int_as_str(String *val_buffer, bool unsigned_flag) const;
1017 /*
1018 str_needs_quotes() returns true if the value returned by val_str() needs
1019 to be quoted when used in constructing an SQL query.
1020 */
1021 virtual bool str_needs_quotes() const { return false; }
1022 virtual Item_result result_type() const = 0;
1023 /**
1024 Returns Item_result type of a field when it appears
1025 in numeric context such as:
1026 SELECT time_column + 1;
1027 SELECT SUM(time_column);
1028 Examples:
1029 - a column of type TIME, DATETIME, TIMESTAMP act as INT.
1030 - a column of type TIME(1), DATETIME(1), TIMESTAMP(1)
1031 act as DECIMAL with 1 fractional digits.
1032 */
1034 return result_type();
1035 }
1036 virtual Item_result cmp_type() const { return result_type(); }
1037 virtual Item_result cast_to_int_type() const { return result_type(); }
1041 bool gcol_expr_is_equal(const Create_field *field) const;
1042 virtual bool eq(const Field *field) const {
1043 return (ptr == field->ptr && m_null_ptr == field->m_null_ptr &&
1044 null_bit == field->null_bit && field->type() == type());
1045 }
1046 virtual bool eq_def(const Field *field) const;
1047
1048 /*
1049 pack_length() returns size (in bytes) used to store field data in memory
1050 (i.e. it returns the maximum size of the field in a row of the table,
1051 which is located in RAM).
1052 */
1053 virtual uint32 pack_length() const { return (uint32)field_length; }
1054
1055 /*
1056 pack_length_in_rec() returns size (in bytes) used to store field data on
1057 storage (i.e. it returns the maximal size of the field in a row of the
1058 table, which is located on disk).
1059 */
1060 virtual uint32 pack_length_in_rec() const { return pack_length(); }
1061 virtual bool compatible_field_size(uint metadata, Relay_log_info *, uint16,
1062 int *order) const;
1063 virtual uint pack_length_from_metadata(uint field_metadata) const {
1064 DBUG_TRACE;
1065 return field_metadata;
1066 }
1067 virtual uint row_pack_length() const { return 0; }
1068 int save_field_metadata(uchar *first_byte) {
1069 return do_save_field_metadata(first_byte);
1070 }
1071
1072 /*
1073 data_length() return the "real size" of the data in memory.
1074 Useful only for variable length datatypes where it's overloaded.
1075 By default assume the length is constant.
1076 */
1077 virtual uint32 data_length(ptrdiff_t row_offset [[maybe_unused]] = 0) const {
1078 return pack_length();
1079 }
1080
1081 /**
1082 Get the maximum size of the data in packed format.
1083
1084 @return Maximum data length of the field when packed using the
1085 Field::pack() function.
1086 */
1087 virtual uint32 max_data_length() const { return pack_length(); }
1088
1090 memset(ptr, 0, pack_length());
1091 return TYPE_OK;
1092 }
1093 /**
1094 Returns a UTC component in `struct timeval` format. This interface
1095 makes any column appear to be `TIMESTAMP`, i.e. stored in UTC, and
1096 returns the UTC component in (optionally fractional) seconds. This means
1097 converting _to_ UTC from the current session's time zone for types other
1098 than `TIMESTAMP`.
1099
1100 This method was expressly written for `SELECT UNIX_TIMESTAMP(field)`
1101 to avoid conversion from timestamp to MYSQL_TIME and back.
1102 */
1103 virtual bool get_timestamp(my_timeval *tm, int *warnings) const;
1104 /**
1105 Stores a timestamp value in timeval format in a field.
1106
1107 @note
1108 - store_timestamp(), get_timestamp() and store_time() do not depend on
1109 timezone and always work "in UTC".
1110
1111 - The default implementation of this interface expects that storing the
1112 value will not fail. For most Field descendent classes, this is not the
1113 case. However, this interface is only used when the function
1114 CURRENT_TIMESTAMP is used as a column default expression, and currently we
1115 only allow TIMESTAMP and DATETIME columns to be declared with this as the
1116 column default. Hence it is enough that the classes implementing columns
1117 with these types either override this interface, or that
1118 store_time(MYSQL_TIME*, uint8) does not fail.
1119
1120 - The column types above interpret decimals() to mean the scale of the
1121 fractional seconds.
1122
1123 - We also have the limitation that the scale of a column must be the same as
1124 the scale of the CURRENT_TIMESTAMP. I.e. we only allow
1125
1126 @code
1127
1128 [ TIMESTAMP | DATETIME ] (n) [ DEFAULT | ON UPDATE ] CURRENT_TIMESTAMP (n)
1129
1130 @endcode
1131
1132 Since this interface relies on the caller to truncate the value according to
1133 this Field's scale, it will work with all constructs that we currently allow.
1134 */
1135 virtual void store_timestamp(const my_timeval *) { assert(false); }
1136
1137 virtual void set_default();
1138
1139 /**
1140 Evaluates the @c INSERT default function and stores the result in the
1141 field. If no such function exists for the column, or the function is not
1142 valid for the column's data type, invoking this function has no effect.
1143 */
1145
1146 /**
1147 Evaluates the @c UPDATE default function, if one exists, and stores the
1148 result in the record buffer. If no such function exists for the column,
1149 or the function is not valid for the column's data type, invoking this
1150 function has no effect.
1151 */
1153 virtual bool binary() const { return true; }
1154 virtual bool zero_pack() const { return true; }
1155 virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
1156 virtual uint32 key_length() const { return pack_length(); }
1157 virtual enum_field_types type() const = 0;
1158 virtual enum_field_types real_type() const { return type(); }
1160 /*
1161 Binlog stores field->type() as type code by default.
1162 This puts MYSQL_TYPE_STRING in case of CHAR, VARCHAR, SET and ENUM,
1163 with extra data type details put into metadata.
1164
1165 We cannot store field->type() in case of temporal types with
1166 fractional seconds: TIME(n), DATETIME(n) and TIMESTAMP(n),
1167 because binlog records with MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME
1168 type codes do not have metadata.
1169 So for temporal data types with fractional seconds we'll store
1170 real_type() type codes instead, i.e.
1171 MYSQL_TYPE_TIME2, MYSQL_TYPE_DATETIME2, MYSQL_TYPE_TIMESTAMP2,
1172 and put precision into metatada.
1173
1174 Note: perhaps binlog should eventually be modified to store
1175 real_type() instead of type() for all column types.
1176 */
1177 return type();
1178 }
1179 int cmp(const uchar *str) const { return cmp(ptr, str); }
1180 virtual int cmp_max(const uchar *a, const uchar *b,
1181 uint max_len [[maybe_unused]]) const {
1182 return cmp(a, b);
1183 }
1184 virtual int cmp(const uchar *, const uchar *) const = 0;
1185 virtual int cmp_binary(const uchar *a, const uchar *b,
1186 uint32 max_length [[maybe_unused]] = ~0L) const {
1187 return memcmp(a, b, pack_length());
1188 }
1189 virtual int cmp_offset(ptrdiff_t row_offset) const {
1190 return cmp(ptr, ptr + row_offset);
1191 }
1192 virtual int cmp_binary_offset(ptrdiff_t row_offset) const {
1193 return cmp_binary(ptr, ptr + row_offset);
1194 }
1195 virtual int key_cmp(const uchar *a, const uchar *b) const {
1196 return cmp(a, b);
1197 }
1198 virtual int key_cmp(const uchar *str, uint length [[maybe_unused]]) const {
1199 return cmp(ptr, str);
1200 }
1201 virtual uint decimals() const { return 0; }
1202 virtual bool is_text_key_type() const { return false; }
1203
1204 /*
1205 Caller beware: sql_type can change str.Ptr, so check
1206 ptr() to see if it changed if you are using your own buffer
1207 in str and restore it with set() if needed
1208 */
1209 virtual void sql_type(String &str) const = 0;
1210
1211 /**
1212 Check whether the full table's row is NULL or the Field has value NULL.
1213
1214 @return true if the full table's row is NULL or the Field has value NULL
1215 false if neither table's row nor the Field has value NULL
1216 */
1217 bool is_null(ptrdiff_t row_offset = 0) const {
1218 /*
1219 if the field is NULLable, it returns NULLity based
1220 on m_null_ptr[row_offset] value. Otherwise it returns
1221 NULL flag depending on TABLE::has_null_row() value.
1222
1223 The table may have been marked as containing only NULL values
1224 for all fields if it is a NULL-complemented row of an OUTER JOIN
1225 or if the query is an implicitly grouped query (has aggregate
1226 functions but no GROUP BY clause) with no qualifying rows. If
1227 this is the case (in which TABLE::has_null_row() is true) and the
1228 field is not nullable, the field is considered to be NULL.
1229
1230 Do not change the order of testing. Fields may be associated
1231 with a TABLE object without being part of the current row.
1232 For NULL value check to work for these fields, they must
1233 have a valid m_null_ptr, and this pointer must be checked before
1234 TABLE::has_null_row().
1235 */
1236 if (is_nullable()) return (m_null_ptr[row_offset] & null_bit);
1237
1238 if (is_tmp_nullable()) return m_is_tmp_null;
1239
1240 return table->has_null_row();
1241 }
1242
1243 /**
1244 Check whether the Field has value NULL (temporary or actual).
1245
1246 @return true if the Field has value NULL (temporary or actual)
1247 false if the Field has value NOT NULL.
1248 */
1249 bool is_real_null(ptrdiff_t row_offset = 0) const {
1250 if (is_nullable()) return (m_null_ptr[row_offset] & null_bit);
1251
1252 if (is_tmp_nullable()) return m_is_tmp_null;
1253
1254 return false;
1255 }
1256
1257 /**
1258 Check if the Field has value NULL or the record specified by argument
1259 has value NULL for this Field.
1260
1261 @return true if the Field has value NULL or the record has value NULL
1262 for thois Field.
1263 */
1264 bool is_null_in_record(const uchar *record) const {
1265 if (is_nullable()) return (record[null_offset()] & null_bit);
1266
1267 return is_tmp_nullable() ? m_is_tmp_null : false;
1268 }
1269
1270 void set_null(ptrdiff_t row_offset = 0);
1271
1272 void set_notnull(ptrdiff_t row_offset = 0);
1273
1274 // Cannot be const as it calls set_warning
1276
1277 /**
1278 Remember the value of THD::check_for_truncated_fields to handle possible
1279 NOT-NULL constraint errors after BEFORE-trigger execution is finished.
1280 We should save the value of THD::check_for_truncated_fields before starting
1281 BEFORE-trigger processing since during triggers execution the
1282 value of THD::check_for_truncated_fields could be changed.
1283 */
1285 enum_check_fields check_for_truncated_fields) {
1286 m_check_for_truncated_fields_saved = check_for_truncated_fields;
1287 }
1288
1289 /// @return true if this field is NULL-able, false otherwise.
1290 bool is_nullable() const { return m_null_ptr != nullptr; }
1291
1292 uint null_offset(const uchar *record) const {
1293 return (uint)(m_null_ptr - record);
1294 }
1295
1296 uint null_offset() const;
1297
1298 void set_null_ptr(uchar *p_null_ptr, uint p_null_bit) {
1299 m_null_ptr = p_null_ptr;
1300 null_bit = p_null_bit;
1301 }
1302
1303 /**
1304 Populates a Send_field object with metadata about the column represented by
1305 this Field object. The Send_field object is used for sending column metadata
1306 to the client.
1307
1308 @param[out] send_field the Send_field object to populate
1309 */
1310 virtual void make_send_field(Send_field *send_field) const;
1311
1312 /**
1313 Writes a copy of the current value in the record buffer, suitable for
1314 sorting using byte-by-byte comparison. Integers are always in big-endian
1315 regardless of hardware architecture. At most length bytes are written
1316 into the buffer.
1317
1318 @param buff The buffer, assumed to be at least length bytes.
1319
1320 @param length Number of bytes to write.
1321
1322 @retval The number of bytes actually written.
1323
1324 @note This is now only used by replication; filesort makes its own
1325 sort keys based off of Items, not Fields.
1326 */
1327 virtual size_t make_sort_key(uchar *buff, size_t length) const = 0;
1328
1329 /**
1330 Writes a copy of the current value in the record buffer, suitable for
1331 sorting using byte-by-byte comparison. Integers are always in big-endian
1332 regardless of hardware architecture. At most length bytes are written
1333 into the buffer. Field_string, Field_varstring and Field_blob classes
1334 are truncated after pos number of characters.
1335
1336 @param buff The buffer, assumed to be at least length bytes.
1337
1338 @param length Number of bytes to write.
1339
1340 @param trunc_pos Number of characters which should be included before
1341 truncation.
1342
1343 @retval The number of bytes actually written.
1344
1345 @note This is now only used by replication; filesort makes its own
1346 sort keys based off of Items, not Fields.
1347 */
1348 virtual size_t make_sort_key(uchar *buff, size_t length,
1349 size_t trunc_pos [[maybe_unused]]) const {
1350 return make_sort_key(buff, length);
1351 }
1352
1353 /**
1354 Whether this field can be used for index range scans when in
1355 the given keypart of the given index.
1356 */
1357 virtual bool optimize_range(uint idx, uint part) const;
1358 /*
1359 This should be true for fields which, when compared with constant
1360 items, can be casted to longlong. In this case we will at 'fix_fields'
1361 stage cast the constant items to longlongs and at the execution stage
1362 use field->val_int() for comparison. Used to optimize clauses like
1363 'a_column BETWEEN date_const, date_const'.
1364 */
1365 virtual bool can_be_compared_as_longlong() const { return false; }
1366 virtual void mem_free() {}
1367
1368 virtual Field *new_field(MEM_ROOT *root, TABLE *new_table) const;
1369
1370 Field *new_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr,
1371 uchar *new_null_ptr, uint new_null_bit) const {
1372 Field *field = new_field(root, new_table);
1373 field->move_field(new_ptr, new_null_ptr, new_null_bit);
1374 return field;
1375 }
1376
1377 virtual Field *new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr,
1378 uchar *new_null_ptr, uint new_null_bit) const;
1379
1380 Field *new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr) const {
1381 return new_key_field(root, new_table, new_ptr, m_null_ptr, null_bit);
1382 }
1383
1384 /**
1385 Makes a shallow copy of the Field object.
1386
1387 @note This member function must be overridden in all concrete
1388 subclasses. Several of the Field subclasses are concrete even though they
1389 are not leaf classes, so the compiler will not always catch this.
1390
1391 @param mem_root MEM_ROOT to use for memory allocation.
1392 @retval NULL If memory allocation failed.
1393 */
1394 virtual Field *clone(MEM_ROOT *mem_root) const = 0;
1395
1396 void move_field(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg) {
1397 ptr = ptr_arg;
1398 m_null_ptr = null_ptr_arg;
1399 null_bit = null_bit_arg;
1400 }
1401
1402 virtual void move_field_offset(ptrdiff_t ptr_diff) {
1403 ptr += ptr_diff;
1404 if (is_nullable()) m_null_ptr += ptr_diff;
1405 }
1406
1407 virtual void get_image(uchar *buff, size_t length,
1408 const CHARSET_INFO *) const {
1409 memcpy(buff, ptr, length);
1410 }
1411
1412 virtual void set_image(const uchar *buff, size_t length,
1413 const CHARSET_INFO *) {
1414 memcpy(ptr, buff, length);
1415 }
1416
1417 /*
1418 Copy a field part into an output buffer.
1419
1420 SYNOPSIS
1421 Field::get_key_image()
1422 buff [out] output buffer
1423 length output buffer size
1424 type itMBR for geometry blobs, otherwise itRAW
1425
1426 DESCRIPTION
1427 This function makes a copy of field part of size equal to or
1428 less than "length" parameter value.
1429 For fields of string types (CHAR, VARCHAR, TEXT) the rest of buffer
1430 is padded by zero byte.
1431
1432 NOTES
1433 For variable length character fields (i.e. UTF-8) the "length"
1434 parameter means a number of output buffer bytes as if all field
1435 characters have maximal possible size (mbmaxlen). In the other words,
1436 "length" parameter is a number of characters multiplied by
1437 field_charset->mbmaxlen.
1438
1439 RETURN
1440 Number of copied bytes (excluding padded zero bytes -- see above).
1441 */
1442
1443 virtual size_t get_key_image(uchar *buff, size_t length,
1444 imagetype type [[maybe_unused]]) const {
1446 return length;
1447 }
1448 virtual void set_key_image(const uchar *buff, size_t length) {
1450 }
1451 longlong val_int_offset(ptrdiff_t row_offset) {
1452 ptr += row_offset;
1453 const longlong tmp = val_int();
1454 ptr -= row_offset;
1455 return tmp;
1456 }
1458 uchar *old_ptr = ptr;
1459 longlong return_value;
1460 ptr = new_ptr;
1461 return_value = val_int();
1462 ptr = old_ptr;
1463 return return_value;
1464 }
1466 uchar *old_ptr = ptr;
1467 ptr = new_ptr;
1468 val_str(str);
1469 ptr = old_ptr;
1470 return str;
1471 }
1472
1473 /**
1474 Send the value of this field over the protocol using the correct
1475 Protocol::store*() function which matches the type of the field.
1476 */
1477 virtual bool send_to_protocol(Protocol *protocol) const;
1478
1479 /**
1480 Pack the field into a format suitable for storage and transfer.
1481
1482 To implement packing functionality, only the virtual function
1483 should be overridden. The other functions are just convenience
1484 functions and hence should not be overridden.
1485
1486 The actual format is opaque and will vary between types of Field
1487 (it is meant to be unpacked by unpack(), but be aware that it is
1488 used among others in the replication log, so you cannot change it
1489 without incurring a format break.
1490
1491 @note The default implementation just copies the raw bytes
1492 of the record into the destination, but never more than
1493 <code>max_length</code> characters.
1494
1495 @param to
1496 Pointer to memory area where representation of field should be put.
1497
1498 @param from
1499 Pointer to memory area where record representation of field is
1500 stored, typically field->field_ptr().
1501
1502 @param max_length
1503 Available space in “to”, in bytes. pack() will not write more bytes than
1504 this; if the field is too short, the contents _are not unpackable by
1505 unpack()_. (It is nominally supposed to be a prefix of what would have
1506 been written with a full buffer, ie., the same as packing and then
1507 truncating the output, but not all Field classes follow this.)
1508
1509 @return The byte after the last byte in “to” written to. If the return
1510 value is equal to (to + max_length), it could either be that the value
1511 fit exactly, or that the buffer was too small; you cannot distinguish
1512 between the two cases based on the return value alone.
1513 */
1514 virtual uchar *pack(uchar *to, const uchar *from, size_t max_length) const;
1515
1516 uchar *pack(uchar *to) const { return pack(to, ptr, UINT_MAX); }
1517
1518 virtual const uchar *unpack(uchar *to, const uchar *from, uint param_data);
1519
1520 const uchar *unpack(const uchar *from) { return unpack(ptr, from, 0U); }
1521
1522 /**
1523 This function does the same thing as pack(), except for the difference
1524 that max_length does not mean the number of bytes in the output, but the
1525 maximum field length from the input (which must be exactly
1526 field->max_field_length()). The difference is currently only relevant for
1527 Field_blob, but can be summed up as follows:
1528
1529 - If the actual field length is longer than "max_length", by way of
1530 software bug or otherwise, the function may behave as if it were shorter,
1531 and write something that is still readable by unpack().
1532 - There is no bounds checking; the caller must verify that there is
1533 sufficient space in "to". Even in the case of truncation, "to" must
1534 be long enough to hold the untruncated field, as the return pointer
1535 would otherwise be invalid, causing undefined behavior as per the C++
1536 standard.
1537 */
1538 virtual uchar *pack_with_metadata_bytes(uchar *to, const uchar *from,
1539 uint max_length) const {
1540 return pack(to, from, max_length);
1541 }
1542
1543 /**
1544 Write the field for the binary log in diff format.
1545
1546 This should only write the field if the diff format is smaller
1547 than the full format. Otherwise it should leave the buffer
1548 untouched.
1549
1550 @param[in,out] to Pointer to buffer where the field will be
1551 written. This will be changed to point to the next byte after the
1552 last byte that was written.
1553
1554 @param value_options bitmap that indicates if full or partial
1555 JSON format is to be used.
1556
1557 @retval true The field was not written, either because the data
1558 type does not support it, or because it was disabled according to
1559 value_options, or because there was no diff information available
1560 from the optimizer, or because the the diff format was bigger than
1561 the full format. The 'to' parameter is unchanged in this case.
1562
1563 @retval false The field was written.
1564 */
1565 virtual bool pack_diff(uchar **to [[maybe_unused]],
1566 ulonglong value_options [[maybe_unused]]) const {
1567 return true;
1568 }
1569
1570 /**
1571 This is a wrapper around pack_length() used by filesort() to determine
1572 how many bytes we need for packing "addon fields".
1573 @returns maximum size of a row when stored in the filesort buffer.
1574 */
1575
1576 virtual uint max_packed_col_length() const { return pack_length(); }
1577
1578 uint offset(uchar *record) const { return (uint)(ptr - record); }
1579
1580 void copy_data(ptrdiff_t src_record_offset);
1581
1582 virtual bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const;
1583
1584 virtual bool get_time(MYSQL_TIME *ltime) const;
1585
1586 virtual const CHARSET_INFO *charset() const { return &my_charset_bin; }
1587
1589 return binary() ? &my_charset_bin : charset();
1590 }
1591 virtual const CHARSET_INFO *sort_charset() const { return charset(); }
1592 virtual bool has_charset() const { return false; }
1593 /*
1594 match_collation_to_optimize_range() is to distinguish in
1595 range optimizer (see opt_range.cc) between real string types:
1596 CHAR, VARCHAR, TEXT
1597 and the other string-alike types with result_type() == STRING_RESULT:
1598 DATE, TIME, DATETIME, TIMESTAMP
1599 We need it to decide whether to test if collation of the operation
1600 matches collation of the field (needed only for real string types).
1601 QQ: shouldn't DATE/TIME types have their own XXX_RESULT types eventually?
1602 */
1603
1604 virtual bool match_collation_to_optimize_range() const { return false; }
1605 virtual enum Derivation derivation() const { return DERIVATION_IMPLICIT; }
1606 virtual uint repertoire() const { return MY_REPERTOIRE_UNICODE30; }
1607 virtual void set_derivation(enum Derivation) {}
1608
1609 /**
1610 Produce warning or note about data saved into field.
1611
1612 @param level - level of message (Note/Warning/Error)
1613 @param code - error code of message to be produced
1614 @param cut_increment - whenever we should increase cut fields count
1615
1616 @note
1617 This function won't produce warning and increase cut fields counter
1618 if check_for_truncated_fields == CHECK_FIELD_IGNORE for current thread.
1619
1620 if check_for_truncated_fields == CHECK_FIELD_IGNORE then we ignore notes.
1621 This allows us to avoid notes in optimization, like
1622 convert_constant_item().
1623
1624 @retval
1625 1 if check_for_truncated_fields == CHECK_FIELD_IGNORE and error level
1626 is not NOTE
1627 @retval
1628 0 otherwise
1629 */
1631 int cut_increment) {
1632 return set_warning(level, code, cut_increment, nullptr, nullptr);
1633 }
1634
1635 bool set_warning(Sql_condition::enum_severity_level level, uint code,
1636 int cut_increment, const char *view_db,
1637 const char *view_name);
1638
1639 bool warn_if_overflow(int op_result);
1640 virtual void init(TABLE *table_arg);
1641
1642 /* maximum possible display length */
1643 virtual uint32 max_display_length() const = 0;
1644
1645 /**
1646 Whether a field being created is type-compatible with an existing one.
1647
1648 Used by the ALTER TABLE code to evaluate whether the new definition
1649 of a table is compatible with the old definition so that it can
1650 determine if data needs to be copied over (table data change).
1651 Constraints and generation clause (default value, generation expression)
1652 are not checked by this function.
1653
1654 @param new_field new field definition from alter.
1655 @retval IS_EQUAL_YES if there is no change.
1656 @retval IS_EQUAL_PACK_LENGTH if the data are unchanged, but the length
1657 requirements have changed
1658 @retval IS_EQUAL_NO if there is an incompatible change requiring copy.
1659 */
1660
1661 virtual uint is_equal(const Create_field *new_field) const;
1662
1663 /* convert decimal to longlong with overflow check */
1664 longlong convert_decimal2longlong(const my_decimal *val, bool unsigned_flag,
1665 bool *has_overflow);
1666 /* The max. number of characters */
1667 virtual uint32 char_length() const {
1668 return field_length / charset()->mbmaxlen;
1669 }
1670
1672 /* shouldn't get here. */
1673 assert(0);
1674 return GEOM_GEOMETRY;
1675 }
1676#ifndef NDEBUG
1677 /* Print field value into debug trace, in NULL-aware way. */
1678 void dbug_print() const {
1679 if (is_real_null())
1680 fprintf(DBUG_FILE, "NULL");
1681 else {
1682 char buf[256];
1683 String str(buf, sizeof(buf), &my_charset_bin);
1684 str.length(0);
1685 String *pstr;
1686 pstr = val_str(&str);
1687 fprintf(DBUG_FILE, "'%s'", pstr->c_ptr_safe());
1688 }
1689 }
1690#endif
1691
1694 }
1695
1696 void set_storage_type(ha_storage_media storage_type_arg) {
1697 assert(field_storage_type() == HA_SM_DEFAULT);
1698 flags |= (storage_type_arg << FIELD_FLAGS_STORAGE_MEDIA);
1699 }
1700
1703 }
1704
1705 void set_column_format(column_format_type column_format_arg) {
1707 flags |= (column_format_arg << FIELD_FLAGS_COLUMN_FORMAT);
1708 }
1709
1710 /* Validate the value stored in a field */
1712 [[maybe_unused]]) {
1713 return TYPE_OK;
1714 }
1715
1716 /* Hash value */
1717 virtual void hash(ulong *nr, ulong *nr2) const;
1718
1719 /**
1720 Get the upper limit of the MySQL integral and floating-point type.
1721
1722 @return maximum allowed value for the field
1723 */
1725 assert(false);
1726 return 0ULL;
1727 }
1728
1729 /**
1730 Return a const pointer to the actual data in the record buffer.
1731
1732 For most fields, this is the same as field_ptr(), but BLOBs and VARCHARs
1733 it is not. Ideally this function should not be used as it makes it hard
1734 to change the internal representation of Field.
1735 */
1736 virtual const uchar *data_ptr() const { return ptr; }
1737
1738 /**
1739 Return a const pointer to where the field is stored in the record buffer.
1740
1741 Ideally this function should not be used as it makes it hard
1742 to change the internal representation of Field.
1743 */
1744 const uchar *field_ptr() const { return ptr; }
1745
1746 /**
1747 Return a pointer to where the field is stored in the record buffer.
1748
1749 Ideally this function should not be used as it makes it hard
1750 to change the internal representation of Field.
1751 */
1752 uchar *field_ptr() { return ptr; }
1753
1754 void set_field_ptr(uchar *ptr_arg) { ptr = ptr_arg; }
1755
1756 /**
1757 Checks whether a string field is part of write_set.
1758
1759 @return
1760 false - If field is not char/varchar/....
1761 - If field is char/varchar/.. and is not part of write set.
1762 true - If field is char/varchar/.. and is part of write set.
1763 */
1764 virtual bool is_updatable() const { return false; }
1765
1766 /**
1767 Check whether field is part of the index taking the index extensions flag
1768 into account. Index extensions are also not applicable to UNIQUE indexes
1769 for loose index scans.
1770
1771 @param[in] thd THD object
1772 @param[in] cur_index Index of the key
1773 @param[in] cur_index_info key_info object
1774
1775 @retval true Field is part of the key
1776 @retval false otherwise
1777
1778 */
1779
1780 bool is_part_of_actual_key(THD *thd, uint cur_index,
1781 KEY *cur_index_info) const;
1782
1783 /**
1784 Get covering prefix keys.
1785
1786 @retval covering prefix keys.
1787 */
1789
1790 /// Whether the field is a typed array
1791 virtual bool is_array() const { return false; }
1792
1793 /**
1794 Return number of bytes the field's length takes
1795
1796 Valid only for varchar and typed arrays of varchar
1797 */
1798 virtual uint32 get_length_bytes() const {
1799 assert(0);
1800 return 0;
1801 }
1802
1803 /**
1804 Whether field's old valued have to be handled.
1805
1806 @returns
1807 true if field is virtual an either one of BLOB types or typed array
1808 false otherwise
1809 */
1810 bool handle_old_value() const {
1811 return (is_flag_set(BLOB_FLAG) || is_array()) && is_virtual_gcol();
1812 }
1813
1814 /**
1815 Sets field index.
1816
1817 @param[in] field_index Field index.
1818 */
1821 }
1822
1823 /**
1824 Returns field index.
1825
1826 @returns Field index.
1827 */
1829
1830 private:
1831 /**
1832 Retrieve the field metadata for fields.
1833
1834 This default implementation returns 0 and saves 0 in the metadata_ptr
1835 value.
1836
1837 @param metadata_ptr First byte of field metadata
1838
1839 @returns 0 no bytes written.
1840 */
1841 virtual int do_save_field_metadata(uchar *metadata_ptr
1842 [[maybe_unused]]) const {
1843 return 0;
1844 }
1845
1846 protected:
1847 uchar *pack_int16(uchar *to, const uchar *from, size_t max_length) const;
1848
1849 const uchar *unpack_int16(uchar *to, const uchar *from) const;
1850
1851 uchar *pack_int24(uchar *to, const uchar *from, size_t max_length) const;
1852
1853 const uchar *unpack_int24(uchar *to, const uchar *from) const;
1854
1855 uchar *pack_int32(uchar *to, const uchar *from, size_t max_length) const;
1856
1857 const uchar *unpack_int32(uchar *to, const uchar *from) const;
1858
1859 uchar *pack_int64(uchar *to, const uchar *from, size_t max_length) const;
1860
1861 const uchar *unpack_int64(uchar *to, const uchar *from) const;
1862};
1863
1864/**
1865 This class is a substitute for the Field classes during CREATE TABLE
1866
1867 When adding a functional index at table creation, we need to resolve the
1868 expression we are indexing. All functions that references one or more
1869 columns expect a Field to be available. But during CREATE TABLE, we only
1870 have access to Create_field. So this class acts as a substitute for the
1871 Field classes so that expressions can be properly resolved. Thus, trying
1872 to call store or val_* on this class will cause an assertion.
1873*/
1874class Create_field_wrapper final : public Field {
1876
1877 public:
1879 Item_result result_type() const final;
1881 enum_field_types type() const final;
1882 uint32 max_display_length() const final;
1883
1884 const CHARSET_INFO *charset() const final;
1885
1886 uint32 pack_length() const final;
1887
1888 // Since it's not a real field, functions below shouldn't be used.
1889 /* purecov: begin deadcode */
1890 type_conversion_status store(const char *, size_t,
1891 const CHARSET_INFO *) final {
1892 assert(false);
1893 return TYPE_ERR_BAD_VALUE;
1894 }
1896 assert(false);
1897 return TYPE_ERR_BAD_VALUE;
1898 }
1900 assert(false);
1901 return TYPE_ERR_BAD_VALUE;
1902 }
1904 assert(false);
1905 return TYPE_ERR_BAD_VALUE;
1906 }
1907 double val_real(void) const final {
1908 assert(false);
1909 return 0.0;
1910 }
1911 longlong val_int(void) const final {
1912 assert(false);
1913 return 0;
1914 }
1916 assert(false);
1917 return nullptr;
1918 }
1919 String *val_str(String *, String *) const final {
1920 assert(false);
1921 return nullptr;
1922 }
1923 int cmp(const uchar *, const uchar *) const final {
1924 assert(false);
1925 return -1;
1926 }
1927 void sql_type(String &) const final { assert(false); }
1929 size_t make_sort_key(uchar *, size_t) const final {
1930 assert(false);
1931 return 0;
1932 }
1933 Field *clone(MEM_ROOT *mem_root) const final {
1934 return new (mem_root) Create_field_wrapper(*this);
1935 }
1936 /* purecov: end */
1937};
1938
1939class Field_num : public Field {
1940 private:
1941 /**
1942 Whether the field is signed or not. Meaningful only for numeric fields
1943 and numeric arrays.
1944 */
1945 const bool unsigned_flag;
1946
1947 public:
1948 const uint8 dec;
1949 /**
1950 True if the column was declared with the ZEROFILL attribute. If it has the
1951 attribute, values should be zero-padded up to the declared display width
1952 when they are converted to strings.
1953 */
1954 bool zerofill; // Purify cannot handle bit fields
1955 Field_num(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1956 uchar null_bit_arg, uchar auto_flags_arg,
1957 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
1958 bool unsigned_arg);
1959 bool is_unsigned() const final { return unsigned_flag; }
1960 Item_result result_type() const override { return REAL_RESULT; }
1961 enum Derivation derivation() const final { return DERIVATION_NUMERIC; }
1962 uint repertoire() const final { return MY_REPERTOIRE_NUMERIC; }
1963 const CHARSET_INFO *charset() const final { return &my_charset_numeric; }
1964 void prepend_zeros(String *value) const;
1965 uint decimals() const final { return (uint)dec; }
1966 bool eq_def(const Field *field) const final;
1969 my_decimal *val_decimal(my_decimal *) const override;
1970 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const override;
1971 bool get_time(MYSQL_TIME *ltime) const override;
1972 uint is_equal(const Create_field *new_field) const override;
1973 uint row_pack_length() const final { return pack_length(); }
1974 uint32 pack_length_from_metadata(uint) const override {
1975 return pack_length();
1976 }
1978 size_t length, const char *int_end,
1979 int error);
1980 type_conversion_status get_int(const CHARSET_INFO *cs, const char *from,
1981 size_t len, longlong *rnd,
1982 ulonglong unsigned_max, longlong signed_min,
1983 longlong signed_max);
1984};
1985
1986class Field_str : public Field {
1987 protected:
1990
1991 public:
1992 Field_str(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
1993 uchar null_bit_arg, uchar auto_flags_arg,
1994 const char *field_name_arg, const CHARSET_INFO *charset);
1995 Item_result result_type() const override { return STRING_RESULT; }
1997 uint decimals() const override { return DECIMAL_NOT_SPECIFIED; }
1998 void make_send_field(Send_field *field) const override;
1999 type_conversion_status store(double nr) override;
2000 type_conversion_status store(longlong nr, bool unsigned_val) override = 0;
2002 type_conversion_status store(const char *to, size_t length,
2003 const CHARSET_INFO *cs) override = 0;
2004
2005 uint repertoire() const final { return my_charset_repertoire(field_charset); }
2006 const CHARSET_INFO *charset() const override { return field_charset; }
2007 void set_charset(const CHARSET_INFO *charset_arg) {
2008 field_charset = charset_arg;
2010 }
2014 }
2015 enum Derivation derivation() const final { return field_derivation; }
2016 void set_derivation(enum Derivation derivation_arg) final {
2017 field_derivation = derivation_arg;
2018 }
2019 bool binary() const override { return field_charset == &my_charset_bin; }
2020 uint32 max_display_length() const override { return field_length; }
2021 bool str_needs_quotes() const final { return true; }
2022 uint is_equal(const Create_field *new_field) const override;
2023
2024 void add_to_cost(CostOfItem *cost) const override;
2025
2026 // An always-updated cache of the result of char_length(), because
2027 // dividing by charset()->mbmaxlen can be surprisingly costly compared
2028 // to the rest of e.g. make_sort_key().
2030};
2031
2032/* base class for Field_string, Field_varstring and Field_blob */
2033
2034class Field_longstr : public Field_str {
2035 private:
2037 const char *end,
2038 bool count_spaces);
2039
2040 protected:
2042 const char *well_formed_error_pos, const char *cannot_convert_error_pos,
2043 const char *from_end_pos, const char *end, bool count_spaces,
2044 const CHARSET_INFO *cs);
2045
2046 public:
2047 Field_longstr(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2048 uchar null_bit_arg, uchar auto_flags_arg,
2049 const char *field_name_arg, const CHARSET_INFO *charset_arg)
2050 : Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2051 field_name_arg, charset_arg) {}
2052
2054 uint32 max_data_length() const override;
2055 bool is_updatable() const final;
2056};
2057
2058/* base class for float and double and decimal (old one) */
2059class Field_real : public Field_num {
2060 public:
2063 TR_OK = 0,
2064 TR_POSITIVE_OVERFLOW = 1,
2065 TR_NEGATIVE_OVERFLOW = 2
2067
2068 Field_real(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2069 uchar null_bit_arg, uchar auto_flags_arg,
2070 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2071 bool unsigned_arg)
2072 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2073 field_name_arg, dec_arg, zero_arg, unsigned_arg),
2074 not_fixed(dec_arg >= DECIMAL_NOT_SPECIFIED) {}
2077 my_decimal *val_decimal(my_decimal *) const final;
2078 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
2079 bool get_time(MYSQL_TIME *ltime) const final;
2080 Truncate_result truncate(double *nr, double max_length);
2081 Truncate_result truncate(double *nr, double max_length) const;
2082 uint32 max_display_length() const final { return field_length; }
2083 const uchar *unpack(uchar *to, const uchar *from, uint param_data) override;
2084 uchar *pack(uchar *to, const uchar *from, size_t max_length) const override;
2085};
2086
2087class Field_decimal final : public Field_real {
2088 public:
2089 Field_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2090 uchar null_bit_arg, uchar auto_flags_arg,
2091 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2092 bool unsigned_arg)
2093 : Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2094 field_name_arg, dec_arg, zero_arg, unsigned_arg) {}
2095 enum_field_types type() const final { return MYSQL_TYPE_DECIMAL; }
2096 enum ha_base_keytype key_type() const final {
2098 }
2099 type_conversion_status store(const char *to, size_t length,
2100 const CHARSET_INFO *charset) final;
2101 type_conversion_status store(double nr) final;
2102 type_conversion_status store(longlong nr, bool unsigned_val) final;
2103 double val_real() const final;
2104 longlong val_int() const final;
2105 String *val_str(String *, String *) const final;
2106 int cmp(const uchar *, const uchar *) const final;
2108 size_t make_sort_key(uchar *buff, size_t length) const final;
2109 void overflow(bool negative);
2110 bool zero_pack() const final { return false; }
2111 void sql_type(String &str) const final;
2113 assert(type() == MYSQL_TYPE_DECIMAL);
2114 return new (mem_root) Field_decimal(*this);
2115 }
2116 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final {
2117 return Field::unpack(to, from, param_data);
2118 }
2119 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2120 return Field::pack(to, from, max_length);
2121 }
2122};
2123
2124/* New decimal/numeric field which use fixed point arithmetic */
2126 private:
2127 /**
2128 Normally, the underlying decimal code will degrade values' excessive
2129 precision. E.g. value 0.0 stored as decimal(10,4) will be returned as
2130 decimal(4,4). This is fine for general purpose, but isn't usable for
2131 multi-valued index. Field_typed_array uses a field for conversion and it
2132 expects the value read from it to be exactly same as it would be stored
2133 in SE, i.e with preserved precision. Otherwise, SE won't be able to
2134 index it.
2135 TRUE here tells underlying DECIMAL reading code to keep the precision as
2136 is.
2137 */
2138 bool m_keep_precision{false};
2139 int do_save_field_metadata(uchar *first_byte) const final;
2140
2141 public:
2142 /* The maximum number of decimal digits can be stored */
2145 /*
2146 Constructors take max_length of the field as a parameter - not the
2147 precision as the number of decimal digits allowed.
2148 So for example we need to count length from precision handling
2149 CREATE TABLE ( DECIMAL(x,y))
2150 */
2151 Field_new_decimal(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2152 uchar null_bit_arg, uchar auto_flags_arg,
2153 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2154 bool unsigned_arg);
2155 Field_new_decimal(uint32 len_arg, bool is_nullable_arg,
2156 const char *field_name_arg, uint8 dec_arg,
2157 bool unsigned_arg);
2159 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_BINARY; }
2160 Item_result result_type() const final { return DECIMAL_RESULT; }
2162 type_conversion_status store_value(const my_decimal *decimal_value);
2163 void set_value_on_overflow(my_decimal *decimal_value, bool sign) const;
2164 type_conversion_status store(const char *to, size_t length,
2165 const CHARSET_INFO *charset) final;
2166 type_conversion_status store(double nr) final;
2167 type_conversion_status store(longlong nr, bool unsigned_val) final;
2170 double val_real() const final;
2171 longlong val_int() const final;
2172 my_decimal *val_decimal(my_decimal *) const final;
2173 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
2174 bool get_time(MYSQL_TIME *ltime) const final;
2175 String *val_str(String *, String *) const final;
2176 int cmp(const uchar *, const uchar *) const final;
2177 using Field_num::make_sort_key;
2178 size_t make_sort_key(uchar *buff, size_t length) const final;
2179 bool zero_pack() const final { return false; }
2180 void sql_type(String &str) const final;
2181 uint32 max_display_length() const final { return field_length; }
2182 uint32 pack_length() const final { return (uint32)bin_size; }
2183 uint pack_length_from_metadata(uint field_metadata) const final;
2184 bool compatible_field_size(uint field_metadata, Relay_log_info *, uint16,
2185 int *order_var) const final;
2186 uint is_equal(const Create_field *new_field) const final;
2188 assert(type() == MYSQL_TYPE_NEWDECIMAL);
2189 return new (mem_root) Field_new_decimal(*this);
2190 }
2191 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final;
2192 static Field *create_from_item(const Item *item);
2193 bool send_to_protocol(Protocol *protocol) const final;
2194 void set_keep_precision(bool arg) { m_keep_precision = arg; }
2195};
2196
2197class Field_tiny : public Field_num {
2198 public:
2199 Field_tiny(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2200 uchar null_bit_arg, uchar auto_flags_arg,
2201 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2202 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2203 field_name_arg, 0, zero_arg, unsigned_arg) {}
2204 Field_tiny(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2205 bool unsigned_arg)
2206 : Field_num(nullptr, len_arg,
2207 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2208 field_name_arg, 0, false, unsigned_arg) {}
2209 enum Item_result result_type() const final { return INT_RESULT; }
2210 enum_field_types type() const override { return MYSQL_TYPE_TINY; }
2211 enum ha_base_keytype key_type() const final {
2213 }
2214 type_conversion_status store(const char *to, size_t length,
2215 const CHARSET_INFO *charset) override;
2216 type_conversion_status store(double nr) override;
2217 type_conversion_status store(longlong nr, bool unsigned_val) override;
2218 double val_real() const override;
2219 longlong val_int() const override;
2220 String *val_str(String *, String *) const override;
2221 bool send_to_protocol(Protocol *protocol) const override;
2222 int cmp(const uchar *, const uchar *) const final;
2224 size_t make_sort_key(uchar *buff, size_t length) const final;
2225 uint32 pack_length() const final { return 1; }
2226 void sql_type(String &str) const override;
2227 uint32 max_display_length() const final { return 4; }
2228 Field_tiny *clone(MEM_ROOT *mem_root) const override {
2229 assert(type() == MYSQL_TYPE_TINY);
2230 return new (mem_root) Field_tiny(*this);
2231 }
2232 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2233 if (max_length > 0) *to = *from;
2234 return to + 1;
2235 }
2236
2237 const uchar *unpack(uchar *to, const uchar *from,
2238 uint param_data [[maybe_unused]]) final {
2239 *to = *from;
2240 return from + 1;
2241 }
2242
2244 return is_unsigned() ? 0xFFULL : 0x7FULL;
2245 }
2246};
2247
2248class Field_short final : public Field_num {
2249 public:
2250 Field_short(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2251 uchar null_bit_arg, uchar auto_flags_arg,
2252 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2253 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2254 field_name_arg, 0, zero_arg, unsigned_arg) {}
2255 Field_short(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2256 bool unsigned_arg)
2257 : Field_num(nullptr, len_arg,
2258 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2259 field_name_arg, 0, false, unsigned_arg) {}
2260 Field_short(uint32 len_arg, const char *field_name_arg, bool unsigned_arg)
2261 : Field_short(len_arg, false, field_name_arg, unsigned_arg) {}
2262 enum Item_result result_type() const final { return INT_RESULT; }
2263 enum_field_types type() const final { return MYSQL_TYPE_SHORT; }
2264 enum ha_base_keytype key_type() const final {
2266 }
2267 type_conversion_status store(const char *to, size_t length,
2268 const CHARSET_INFO *charset) final;
2269 type_conversion_status store(double nr) final;
2270 type_conversion_status store(longlong nr, bool unsigned_val) final;
2271 double val_real() const final;
2272 longlong val_int() const final;
2273 String *val_str(String *, String *) const final;
2274 bool send_to_protocol(Protocol *protocol) const final;
2275 int cmp(const uchar *, const uchar *) const final;
2276 using Field_num::make_sort_key;
2277 size_t make_sort_key(uchar *buff, size_t length) const final;
2278 uint32 pack_length() const final { return 2; }
2279 void sql_type(String &str) const final;
2280 uint32 max_display_length() const final { return 6; }
2282 assert(type() == MYSQL_TYPE_SHORT);
2283 return new (mem_root) Field_short(*this);
2284 }
2285 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2286 return pack_int16(to, from, max_length);
2287 }
2288
2289 const uchar *unpack(uchar *to, const uchar *from,
2290 uint param_data [[maybe_unused]]) final {
2291 return unpack_int16(to, from);
2292 }
2293
2295 return is_unsigned() ? 0xFFFFULL : 0x7FFFULL;
2296 }
2297};
2298
2299class Field_medium final : public Field_num {
2300 public:
2301 Field_medium(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2302 uchar null_bit_arg, uchar auto_flags_arg,
2303 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2304 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2305 field_name_arg, 0, zero_arg, unsigned_arg) {}
2306 Field_medium(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2307 bool unsigned_arg)
2308 : Field_num(nullptr, len_arg,
2309 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2310 field_name_arg, 0, false, unsigned_arg) {}
2311 enum Item_result result_type() const final { return INT_RESULT; }
2312 enum_field_types type() const final { return MYSQL_TYPE_INT24; }
2313 enum ha_base_keytype key_type() const final {
2315 }
2316 type_conversion_status store(const char *to, size_t length,
2317 const CHARSET_INFO *charset) final;
2318 type_conversion_status store(double nr) final;
2319 type_conversion_status store(longlong nr, bool unsigned_val) final;
2320 double val_real() const final;
2321 longlong val_int() const final;
2322 String *val_str(String *, String *) const final;
2323 bool send_to_protocol(Protocol *protocol) const final;
2324 int cmp(const uchar *, const uchar *) const final;
2325 using Field_num::make_sort_key;
2326 size_t make_sort_key(uchar *buff, size_t length) const final;
2327 uint32 pack_length() const final { return 3; }
2328 void sql_type(String &str) const final;
2329 uint32 max_display_length() const final { return 8; }
2331 assert(type() == MYSQL_TYPE_INT24);
2332 return new (mem_root) Field_medium(*this);
2333 }
2335 return is_unsigned() ? 0xFFFFFFULL : 0x7FFFFFULL;
2336 }
2337};
2338
2339class Field_long : public Field_num {
2340 public:
2341 static const int PACK_LENGTH = 4;
2342
2343 Field_long(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2344 uchar null_bit_arg, uchar auto_flags_arg,
2345 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2346 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2347 field_name_arg, 0, zero_arg, unsigned_arg) {}
2348 Field_long(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2349 bool unsigned_arg)
2350 : Field_num(nullptr, len_arg,
2351 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2352 field_name_arg, 0, false, unsigned_arg) {}
2353 enum Item_result result_type() const final { return INT_RESULT; }
2354 enum_field_types type() const final { return MYSQL_TYPE_LONG; }
2355 enum ha_base_keytype key_type() const final {
2357 }
2358 type_conversion_status store(const char *to, size_t length,
2359 const CHARSET_INFO *charset) final;
2360 type_conversion_status store(double nr) final;
2361 type_conversion_status store(longlong nr, bool unsigned_val) override;
2362 double val_real() const final;
2363 longlong val_int() const final;
2364 bool send_to_protocol(Protocol *protocol) const final;
2365 String *val_str(String *, String *) const final;
2366 int cmp(const uchar *, const uchar *) const final;
2367 using Field_num::make_sort_key;
2368 size_t make_sort_key(uchar *buff, size_t length) const final;
2369 uint32 pack_length() const final { return PACK_LENGTH; }
2370 void sql_type(String &str) const final;
2373 }
2375 assert(type() == MYSQL_TYPE_LONG);
2376 return new (mem_root) Field_long(*this);
2377 }
2378 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2379 return pack_int32(to, from, max_length);
2380 }
2381 const uchar *unpack(uchar *to, const uchar *from,
2382 uint param_data [[maybe_unused]]) final {
2383 return unpack_int32(to, from);
2384 }
2385
2387 return is_unsigned() ? 0xFFFFFFFFULL : 0x7FFFFFFFULL;
2388 }
2389};
2390
2392 public:
2393 static const int PACK_LENGTH = 8;
2394
2395 Field_longlong(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2396 uchar null_bit_arg, uchar auto_flags_arg,
2397 const char *field_name_arg, bool zero_arg, bool unsigned_arg)
2398 : Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2399 field_name_arg, 0, zero_arg, unsigned_arg) {}
2400 Field_longlong(uint32 len_arg, bool is_nullable_arg,
2401 const char *field_name_arg, bool unsigned_arg)
2402 : Field_num(nullptr, len_arg,
2403 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2404 field_name_arg, 0, false, unsigned_arg) {}
2405 enum Item_result result_type() const final { return INT_RESULT; }
2407 enum ha_base_keytype key_type() const final {
2409 }
2410 type_conversion_status store(const char *to, size_t length,
2411 const CHARSET_INFO *charset) final;
2412 type_conversion_status store(double nr) final;
2413 type_conversion_status store(longlong nr, bool unsigned_val) override;
2414 double val_real() const final;
2415 longlong val_int() const override;
2416 String *val_str(String *, String *) const final;
2417 bool send_to_protocol(Protocol *protocol) const final;
2418 int cmp(const uchar *, const uchar *) const final;
2419 using Field_num::make_sort_key;
2420 size_t make_sort_key(uchar *buff, size_t length) const final;
2421 uint32 pack_length() const final { return PACK_LENGTH; }
2422 void sql_type(String &str) const final;
2423 bool can_be_compared_as_longlong() const final { return true; }
2424 uint32 max_display_length() const final { return 20; }
2426 assert(type() == MYSQL_TYPE_LONGLONG);
2427 return new (mem_root) Field_longlong(*this);
2428 }
2429 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2430 return pack_int64(to, from, max_length);
2431 }
2432 const uchar *unpack(uchar *to, const uchar *from,
2433 uint param_data [[maybe_unused]]) final {
2434 return unpack_int64(to, from);
2435 }
2436
2438 return is_unsigned() ? 0xFFFFFFFFFFFFFFFFULL : 0x7FFFFFFFFFFFFFFFULL;
2439 }
2440};
2441
2442class Field_float final : public Field_real {
2443 public:
2444 Field_float(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2445 uchar null_bit_arg, uchar auto_flags_arg,
2446 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2447 bool unsigned_arg)
2448 : Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2449 field_name_arg, dec_arg, zero_arg, unsigned_arg) {}
2450 Field_float(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2451 uint8 dec_arg, bool unsigned_arg)
2452 : Field_real(nullptr, len_arg,
2453 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2454 field_name_arg, dec_arg, false, unsigned_arg) {}
2455 enum_field_types type() const final { return MYSQL_TYPE_FLOAT; }
2456 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_FLOAT; }
2457 type_conversion_status store(const char *to, size_t length,
2458 const CHARSET_INFO *charset) final;
2459 type_conversion_status store(double nr) final;
2460 type_conversion_status store(longlong nr, bool unsigned_val) final;
2461 double val_real() const final;
2462 longlong val_int() const final;
2463 String *val_str(String *, String *) const final;
2464 bool send_to_protocol(Protocol *protocol) const final;
2465 int cmp(const uchar *, const uchar *) const final;
2467 size_t make_sort_key(uchar *buff, size_t length) const final;
2468 uint32 pack_length() const final { return sizeof(float); }
2469 void sql_type(String &str) const final;
2471 assert(type() == MYSQL_TYPE_FLOAT);
2472 return new (mem_root) Field_float(*this);
2473 }
2474
2476 /*
2477 We use the maximum as per IEEE754-2008 standard, 2^24
2478 */
2479 return 0x1000000ULL;
2480 }
2481
2482 private:
2483 int do_save_field_metadata(uchar *first_byte) const final;
2484};
2485
2486class Field_double final : public Field_real {
2487 public:
2488 Field_double(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2489 uchar null_bit_arg, uchar auto_flags_arg,
2490 const char *field_name_arg, uint8 dec_arg, bool zero_arg,
2491 bool unsigned_arg)
2492 : Field_real(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2493 field_name_arg, dec_arg, zero_arg, unsigned_arg) {}
2494 Field_double(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2495 uint8 dec_arg)
2496 : Field_real(nullptr, len_arg,
2497 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2498 field_name_arg, dec_arg, false, false) {}
2499 Field_double(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2500 uint8 dec_arg, bool unsigned_arg)
2501 : Field_real(nullptr, len_arg,
2502 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2503 field_name_arg, dec_arg, false, unsigned_arg) {}
2504 Field_double(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
2505 uint8 dec_arg, bool unsigned_arg, bool not_fixed_arg)
2506 : Field_real(nullptr, len_arg,
2507 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
2508 field_name_arg, dec_arg, false, unsigned_arg) {
2509 not_fixed = not_fixed_arg;
2510 }
2511 enum_field_types type() const final { return MYSQL_TYPE_DOUBLE; }
2512 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_DOUBLE; }
2513 type_conversion_status store(const char *to, size_t length,
2514 const CHARSET_INFO *charset) final;
2515 type_conversion_status store(double nr) final;
2516 type_conversion_status store(longlong nr, bool unsigned_val) final;
2517 double val_real() const final;
2518 longlong val_int() const final;
2519 String *val_str(String *, String *) const final;
2520 bool send_to_protocol(Protocol *protocol) const final;
2521 int cmp(const uchar *, const uchar *) const final;
2523 size_t make_sort_key(uchar *buff, size_t length) const final;
2524 uint32 pack_length() const final { return sizeof(double); }
2525 void sql_type(String &str) const final;
2527 assert(type() == MYSQL_TYPE_DOUBLE);
2528 return new (mem_root) Field_double(*this);
2529 }
2530
2532 /*
2533 We use the maximum as per IEEE754-2008 standard, 2^53
2534 */
2535 return 0x20000000000000ULL;
2536 }
2537
2538 private:
2539 int do_save_field_metadata(uchar *first_byte) const final;
2540};
2541
2542/* Everything saved in this will disappear. It will always return NULL */
2543
2544class Field_null final : public Field_str {
2545 public:
2546 Field_null(uchar *ptr_arg, uint32 len_arg, uchar auto_flags_arg,
2547 const char *field_name_arg, const CHARSET_INFO *cs)
2548 // (dummy_null_buffer & 32) is true, so is_null() always returns true.
2549 : Field_str(ptr_arg, len_arg, &dummy_null_buffer, 32, auto_flags_arg,
2550 field_name_arg, cs) {}
2551 enum_field_types type() const final { return MYSQL_TYPE_NULL; }
2552 type_conversion_status store(const char *, size_t,
2553 const CHARSET_INFO *) final {
2554 return TYPE_OK;
2555 }
2556 type_conversion_status store(double) final { return TYPE_OK; }
2559 return TYPE_OK;
2560 }
2562 double val_real() const final { return 0.0; }
2563 longlong val_int() const final { return 0; }
2564 my_decimal *val_decimal(my_decimal *) const final { return nullptr; }
2565 String *val_str(String *, String *value2) const final {
2566 value2->length(0);
2567 return value2;
2568 }
2569 int cmp(const uchar *, const uchar *) const final { return 0; }
2571 size_t make_sort_key(uchar *, size_t len) const final { return len; }
2572 uint32 pack_length() const final { return 0; }
2573 void sql_type(String &str) const final;
2574 uint32 max_display_length() const final { return 4; }
2576 assert(type() == MYSQL_TYPE_NULL);
2577 return new (mem_root) Field_null(*this);
2578 }
2579};
2580
2581/*
2582 Abstract class for TIME, DATE, DATETIME, TIMESTAMP
2583 with and without fractional part.
2584*/
2585class Field_temporal : public Field {
2586 protected:
2587 uint8 dec; // Number of fractional digits
2588
2589 /**
2590 Adjust number of decimal digits from DECIMAL_NOT_SPECIFIED to
2591 DATETIME_MAX_DECIMALS
2592 */
2593 static uint8 normalize_dec(uint8 dec_arg) {
2594 return dec_arg == DECIMAL_NOT_SPECIFIED ? DATETIME_MAX_DECIMALS : dec_arg;
2595 }
2596
2597 /**
2598 Low level routine to store a MYSQL_TIME value into a field.
2599 The value must be already properly rounded or truncated
2600 and checked for being a valid TIME/DATE/DATETIME value.
2601
2602 @param[in] ltime MYSQL_TIME value.
2603 @param[out] error Error flag vector, set in case of error.
2604 @retval false In case of success.
2605 @retval true In case of error.
2606 */
2608 int *error) = 0;
2609
2610 /**
2611 Low level routine to store a MYSQL_TIME value into a field
2612 with rounding/truncation according to the field decimals() value and
2613 sql_mode.
2614
2615 @param[in] ltime MYSQL_TIME value.
2616 @param[out] warnings Error flag vector, set in case of error.
2617 @retval false In case of success.
2618 @retval true In case of error.
2619 */
2621 int *warnings) = 0;
2622
2623 /**
2624 Store a temporal value in lldiv_t into a field,
2625 with rounding according to the field decimals() value.
2626
2627 @param[in] lld Temporal value.
2628 @param[out] warning Warning flag vector.
2629 @retval false In case of success.
2630 @retval true In case of error.
2631 */
2632 type_conversion_status store_lldiv_t(const lldiv_t *lld, int *warning);
2633
2634 /**
2635 Convert a string to MYSQL_TIME, according to the field type.
2636
2637 @param[in] str String
2638 @param[in] len String length
2639 @param[in] cs String character set
2640 @param[out] ltime The value is stored here
2641 @param[out] status Conversion status
2642 @retval false Conversion went fine, ltime contains a valid time
2643 @retval true Conversion failed, ltime was reset and contains nothing
2644 */
2645 virtual bool convert_str_to_TIME(const char *str, size_t len,
2646 const CHARSET_INFO *cs, MYSQL_TIME *ltime,
2648 /**
2649 Convert a number with fractional part with nanosecond precision
2650 into MYSQL_TIME, according to the field type. Nanoseconds
2651 are rounded to milliseconds and added to ltime->second_part.
2652
2653 @param[in] nr Number
2654 @param[in] unsigned_val SIGNED/UNSIGNED flag
2655 @param[in] nanoseconds Fractional part in nanoseconds
2656 @param[out] ltime The value is stored here
2657 @param[in,out] warning Warnings found during execution
2658
2659 @return Conversion status
2660 @retval false On success
2661 @retval true On error
2662 */
2664 bool unsigned_val,
2665 int nanoseconds,
2666 MYSQL_TIME *ltime,
2667 int *warning) = 0;
2668
2669 /**
2670 Convert an integer number into MYSQL_TIME, according to the field type.
2671
2672 @param[in] nr Number
2673 @param[in] unsigned_val SIGNED/UNSIGNED flag
2674 @param[out] ltime The value is stored here
2675 @param[in,out] warnings Warnings found during execution
2676
2677 @retval false On success
2678 @retval true On error
2679 */
2680 longlong convert_number_to_datetime(longlong nr, bool unsigned_val,
2681 MYSQL_TIME *ltime, int *warnings);
2682
2683 /**
2684 Set warnings from a warning vector.
2685 Note, multiple warnings can be set at the same time.
2686 Every warning in the bit vector is set by an individual
2687 set_datetime_warning() call.
2688
2689 @param str Value.
2690 @param warnings Warning vector.
2691
2692 @retval false Function reported warning
2693 @retval true Function reported error
2694
2695 @note STRICT mode can convert warnings to error.
2696 */
2697 [[nodiscard]] bool set_warnings(const ErrConvString &str, int warnings);
2698
2699 /**
2700 Flags that are passed as "flag" argument to
2701 check_date(), number_to_datetime(), str_to_datetime().
2702
2703 Flags depend on the session sql_mode settings, such as
2704 MODE_NO_ZERO_DATE, MODE_NO_ZERO_IN_DATE.
2705 Also, Field_newdate, Field_datetime, Field_datetimef add TIME_FUZZY_DATE
2706 to the session sql_mode settings, to allow relaxed date format,
2707 while Field_timestamp, Field_timestampf do not.
2708
2709 @param thd THD
2710 @retval sql_mode flags mixed with the field type flags.
2711 */
2712 virtual my_time_flags_t date_flags(const THD *thd [[maybe_unused]]) const {
2713 return 0;
2714 }
2715
2716 /**
2717 Flags that are passed as "flag" argument to
2718 check_date(), number_to_datetime(), str_to_datetime().
2719 Similar to the above when we don't have a THD value.
2720 */
2721 my_time_flags_t date_flags() const;
2722
2723 /**
2724 Produce warning or note about double datetime data saved into field.
2725
2726 @param level level of message (Note/Warning/Error)
2727 @param code error code of message to be produced
2728 @param val error parameter (the value)
2729 @param ts_type type of datetime value (datetime/date/time)
2730 @param truncate_increment whether we should increase truncated fields count
2731
2732 @retval false Function reported warning
2733 @retval true Function reported error
2734
2735 @note
2736 This function will always produce some warning but won't increase
2737 truncated fields counter if check_for_truncated_fields == FIELD_CHECK_IGNORE
2738 for current thread.
2739 */
2740 [[nodiscard]] bool set_datetime_warning(
2741 Sql_condition::enum_severity_level level, uint code,
2742 const ErrConvString &val, enum_mysql_timestamp_type ts_type,
2743 int truncate_increment);
2744
2745 public:
2746 /**
2747 Constructor for Field_temporal
2748 @param ptr_arg See Field definition
2749 @param null_ptr_arg See Field definition
2750 @param null_bit_arg See Field definition
2751 @param auto_flags_arg See Field definition
2752 @param field_name_arg See Field definition
2753 @param len_arg Number of characters in the integer part.
2754 @param dec_arg Number of second fraction digits, 0..6.
2755 */
2756 Field_temporal(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
2757 uchar auto_flags_arg, const char *field_name_arg,
2758 uint32 len_arg, uint8 dec_arg)
2759 : Field(ptr_arg,
2760 len_arg +
2761 ((normalize_dec(dec_arg)) ? normalize_dec(dec_arg) + 1 : 0),
2762 null_ptr_arg, null_bit_arg, auto_flags_arg, field_name_arg) {
2764 dec = normalize_dec(dec_arg);
2765 }
2766 Item_result result_type() const final { return STRING_RESULT; }
2767 uint32 max_display_length() const final { return field_length; }
2768 bool str_needs_quotes() const final { return true; }
2769 uint is_equal(const Create_field *new_field) const final;
2771 return dec ? DECIMAL_RESULT : INT_RESULT;
2772 }
2773 enum Item_result cmp_type() const final { return INT_RESULT; }
2774 enum Derivation derivation() const final { return DERIVATION_NUMERIC; }
2775 uint repertoire() const final { return MY_REPERTOIRE_NUMERIC; }
2776 const CHARSET_INFO *charset() const final { return &my_charset_numeric; }
2777 bool can_be_compared_as_longlong() const final { return true; }
2778 bool binary() const final { return true; }
2779 type_conversion_status store(const char *str, size_t len,
2780 const CHARSET_INFO *cs) final;
2781 type_conversion_status store_decimal(const my_decimal *decimal) final;
2782 type_conversion_status store(longlong nr, bool unsigned_val) override;
2783 type_conversion_status store(double nr) final;
2784 double val_real() const override // FSP-enable types redefine it.
2785 {
2786 return (double)val_int();
2787 }
2788 [[nodiscard]] uint8 get_dec() const { return dec; }
2790 my_decimal *decimal_value) const override; // FSP types redefine it
2791
2793 return date_flags(thd);
2794 }
2795
2796 uint8 get_fractional_digits() const { return dec; }
2797};
2798
2799/**
2800 Abstract class for types with date
2801 with optional time, with or without fractional part:
2802 DATE, DATETIME, DATETIME(N), TIMESTAMP, TIMESTAMP(N).
2803*/
2805 protected:
2806 /**
2807 Low level function to get value into MYSQL_TIME,
2808 without checking for being valid.
2809 */
2810 virtual bool get_date_internal(MYSQL_TIME *ltime) const = 0;
2811
2812 virtual bool get_date_internal_at_utc(MYSQL_TIME *ltime) const {
2813 return get_date_internal(ltime);
2814 }
2815
2816 /**
2817 Get value into MYSQL_TIME and check TIME_NO_ZERO_DATE flag.
2818 @retval True on error: we get a zero value but flags disallow zero dates.
2819 @retval False on success.
2820 */
2821 bool get_internal_check_zero(MYSQL_TIME *ltime,
2822 my_time_flags_t fuzzydate) const;
2823
2824 type_conversion_status convert_number_to_TIME(longlong nr, bool unsigned_val,
2825 int nanoseconds,
2826 MYSQL_TIME *ltime,
2827 int *warning) final;
2828 bool convert_str_to_TIME(const char *str, size_t len, const CHARSET_INFO *cs,
2829 MYSQL_TIME *ltime, MYSQL_TIME_STATUS *status) final;
2830
2831 type_conversion_status store_internal_adjust_frac(MYSQL_TIME *ltime,
2832 int *warnings) final;
2834
2835 public:
2836 /**
2837 Constructor for Field_temporal
2838 @param ptr_arg See Field definition
2839 @param null_ptr_arg See Field definition
2840 @param null_bit_arg See Field definition
2841 @param auto_flags_arg See Field definition
2842 @param field_name_arg See Field definition
2843 @param int_length_arg Number of characters in the integer part.
2844 @param dec_arg Number of second fraction digits, 0..6.
2845 */
2846 Field_temporal_with_date(uchar *ptr_arg, uchar *null_ptr_arg,
2847 uchar null_bit_arg, uchar auto_flags_arg,
2848 const char *field_name_arg, uint8 int_length_arg,
2849 uint8 dec_arg)
2850 : Field_temporal(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
2851 field_name_arg, int_length_arg, dec_arg) {}
2852 bool send_to_protocol(Protocol *protocol) const override;
2854 String *val_str(String *, String *) const override;
2855 longlong val_time_temporal() const override;
2856 longlong val_date_temporal() const override;
2857 longlong val_time_temporal_at_utc() const override;
2858 longlong val_date_temporal_at_utc() const override;
2859 bool get_time(MYSQL_TIME *ltime) const final {
2860 return get_date(ltime, TIME_FUZZY_DATE);
2861 }
2862 /* Validate the value stored in a field */
2864};
2865
2866/**
2867 Abstract class for types with date and time,
2868 with or without fractional part:
2869 DATETIME, DATETIME(N), TIMESTAMP, TIMESTAMP(N).
2870*/
2872 private:
2873 int do_save_field_metadata(uchar *metadata_ptr) const override {
2874 if (decimals()) {
2875 *metadata_ptr = decimals();
2876 return 1;
2877 }
2878 return 0;
2879 }
2880
2881 protected:
2882 /**
2883 Initialize flags for TIMESTAMP DEFAULT CURRENT_TIMESTAMP / ON UPDATE
2884 CURRENT_TIMESTAMP columns.
2885
2886 @todo get rid of TIMESTAMP_FLAG and ON_UPDATE_NOW_FLAG.
2887 */
2888 void init_timestamp_flags();
2889 /**
2890 Store "struct timeval" value into field.
2891 The value must be properly rounded or truncated according
2892 to the number of fractional second digits.
2893 */
2894 virtual void store_timestamp_internal(const my_timeval *tm) = 0;
2895 bool convert_TIME_to_timestamp(const MYSQL_TIME *ltime, const Time_zone &tz,
2896 my_timeval *tm, int *error);
2897
2898 public:
2899 /**
2900 Constructor for Field_temporal_with_date_and_time
2901 @param ptr_arg See Field definition
2902 @param null_ptr_arg See Field definition
2903 @param null_bit_arg See Field definition
2904 @param auto_flags_arg See Field definition
2905 @param field_name_arg See Field definition
2906 @param dec_arg Number of second fraction digits, 0..6.
2907 */
2909 uchar null_bit_arg, uchar auto_flags_arg,
2910 const char *field_name_arg, uint8 dec_arg)
2911 : Field_temporal_with_date(ptr_arg, null_ptr_arg, null_bit_arg,
2912 auto_flags_arg, field_name_arg,
2913 MAX_DATETIME_WIDTH, dec_arg) {}
2914 void store_timestamp(const my_timeval *tm) override;
2915};
2916
2917/**
2918 Abstract class for types with date and time, with fractional part:
2919 DATETIME, DATETIME(N), TIMESTAMP, TIMESTAMP(N).
2920*/
2923 private:
2924 int do_save_field_metadata(uchar *metadata_ptr) const final {
2925 *metadata_ptr = decimals();
2926 return 1;
2927 }
2928
2929 public:
2930 /**
2931 Constructor for Field_temporal_with_date_and_timef
2932 @param ptr_arg See Field definition
2933 @param null_ptr_arg See Field definition
2934 @param null_bit_arg See Field definition
2935 @param auto_flags_arg See Field definition
2936 @param field_name_arg See Field definition
2937 @param dec_arg Number of second fraction digits, 0..6.
2938 */
2940 uchar null_bit_arg, uchar auto_flags_arg,
2941 const char *field_name_arg, uint8 dec_arg)
2942 : Field_temporal_with_date_and_time(ptr_arg, null_ptr_arg, null_bit_arg,
2943 auto_flags_arg, field_name_arg,
2944 dec_arg) {}
2945
2946 uint decimals() const final { return dec; }
2947 const CHARSET_INFO *sort_charset() const final { return &my_charset_bin; }
2949 size_t make_sort_key(uchar *to, size_t length) const final {
2950 memcpy(to, ptr, length);
2951 return length;
2952 }
2953 int cmp(const uchar *a_ptr, const uchar *b_ptr) const final {
2954 return memcmp(a_ptr, b_ptr, pack_length());
2955 }
2956 uint row_pack_length() const final { return pack_length(); }
2957 double val_real() const final;
2958 longlong val_int() const final;
2959 my_decimal *val_decimal(my_decimal *decimal_value) const final;
2960};
2961
2962/*
2963 Field implementing TIMESTAMP data type without fractional seconds.
2964 We will be removed eventually.
2965*/
2967 protected:
2968 my_time_flags_t date_flags(const THD *thd) const final;
2969 type_conversion_status store_internal(const MYSQL_TIME *ltime,
2970 int *error) final;
2971 bool get_date_internal(MYSQL_TIME *ltime) const final;
2972 bool get_date_internal_at_utc(MYSQL_TIME *ltime) const final;
2973 void store_timestamp_internal(const my_timeval *tm) final;
2974
2975 public:
2976 static const int PACK_LENGTH = 4;
2977 Field_timestamp(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
2978 uchar null_bit_arg, uchar auto_flags_arg,
2979 const char *field_name_arg);
2980 Field_timestamp(bool is_nullable_arg, const char *field_name_arg);
2982 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_ULONG_INT; }
2984 longlong val_int() const final;
2985 int cmp(const uchar *, const uchar *) const final;
2987 size_t make_sort_key(uchar *buff, size_t length) const final;
2988 uint32 pack_length() const final { return PACK_LENGTH; }
2989 void sql_type(String &str) const final;
2990 bool zero_pack() const final { return false; }
2991 /* Get TIMESTAMP field value as seconds since begging of Unix Epoch */
2992 bool get_timestamp(my_timeval *tm, int *warnings) const final;
2993 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
2995 assert(type() == MYSQL_TYPE_TIMESTAMP);
2996 return new (mem_root) Field_timestamp(*this);
2997 }
2998 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
2999 return pack_int32(to, from, max_length);
3000 }
3001 const uchar *unpack(uchar *to, const uchar *from,
3002 uint param_data [[maybe_unused]]) final {
3003 return unpack_int32(to, from);
3004 }
3005 /* Validate the value stored in a field */
3007
3008 private:
3009 /**
3010 Retrieves a value from a record, without checking fuzzy date flags.
3011
3012 @param tz The time zone to convert to
3013 @param[out] ltime The timestamp value in the time zone.
3014
3015 @retval true Means that the timestamp value read is 0. ltime is not touched
3016 in this case.
3017 @retval false If timestamp is non-zero.
3018 */
3019 bool get_date_internal_at(const Time_zone *tz, MYSQL_TIME *ltime) const;
3020};
3021
3022/*
3023 Field implementing TIMESTAMP(N) data type, where N=0..6.
3024*/
3026 protected:
3027 bool get_date_internal(MYSQL_TIME *ltime) const final;
3028 bool get_date_internal_at_utc(MYSQL_TIME *ltime) const final;
3029 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3030 int *error) final;
3031 my_time_flags_t date_flags(const THD *thd) const final;
3032 void store_timestamp_internal(const my_timeval *tm) override;
3033
3034 public:
3035 /**
3036 Field_timestampf constructor
3037 @param ptr_arg See Field definition
3038 @param null_ptr_arg See Field definition
3039 @param null_bit_arg See Field definition
3040 @param auto_flags_arg See Field definition
3041 @param field_name_arg See Field definition
3042 @param dec_arg Number of fractional second digits, 0..6.
3043 */
3044 Field_timestampf(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3045 uchar auto_flags_arg, const char *field_name_arg,
3046 uint8 dec_arg);
3047 /**
3048 Field_timestampf constructor
3049 @param is_nullable_arg See Field definition
3050 @param field_name_arg See Field definition
3051 @param dec_arg Number of fractional second digits, 0..6.
3052 */
3053 Field_timestampf(bool is_nullable_arg, const char *field_name_arg,
3054 uint8 dec_arg);
3056 assert(type() == MYSQL_TYPE_TIMESTAMP);
3057 return new (mem_root) Field_timestampf(*this);
3058 }
3059
3063 bool zero_pack() const final { return false; }
3064
3066 uint pack_length_from_metadata(uint field_metadata) const final {
3067 DBUG_TRACE;
3068 const uint tmp = my_timestamp_binary_length(field_metadata);
3069 return tmp;
3070 }
3071
3073 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
3074 void sql_type(String &str) const final;
3075
3076 bool get_timestamp(my_timeval *tm, int *warnings) const final;
3077 /* Validate the value stored in a field */
3079
3080 private:
3081 /**
3082 Retrieves a value from a record, without checking fuzzy date flags.
3083
3084 @param tz The time zone to convert to
3085 @param[out] ltime The timestamp value in the time zone.
3086
3087 @retval true Means that the timestamp value read is 0. ltime is not touched
3088 in this case.
3089 @retval false If timestamp is non-zero.
3090 */
3091 bool get_date_internal_at(const Time_zone *tz, MYSQL_TIME *ltime) const;
3092};
3093
3094class Field_year final : public Field_tiny {
3095 public:
3096 enum Limits { MIN_YEAR = 1901, MAX_YEAR = 2155 };
3097 Field_year(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3098 uchar auto_flags_arg, const char *field_name_arg)
3099 : Field_tiny(ptr_arg, 4, null_ptr_arg, null_bit_arg, auto_flags_arg,
3100 field_name_arg, true, true) {}
3101 Field_year(bool is_nullable_arg, const char *field_name_arg)
3102 : Field_tiny(nullptr, 4, is_nullable_arg ? &dummy_null_buffer : nullptr,
3103 0, NONE, field_name_arg, true, true) {}
3104 enum_field_types type() const final { return MYSQL_TYPE_YEAR; }
3105 type_conversion_status store(const char *to, size_t length,
3106 const CHARSET_INFO *charset) final;
3107 type_conversion_status store(double nr) final;
3108 type_conversion_status store(longlong nr, bool unsigned_val) final;
3110 double val_real() const final;
3111 longlong val_int() const final;
3112 String *val_str(String *, String *) const final;
3113 bool send_to_protocol(Protocol *protocol) const final;
3114 void sql_type(String &str) const final;
3115 bool can_be_compared_as_longlong() const final { return true; }
3117 assert(type() == MYSQL_TYPE_YEAR);
3118 return new (mem_root) Field_year(*this);
3119 }
3120};
3121
3123 protected:
3124 static const int PACK_LENGTH = 3;
3125 my_time_flags_t date_flags(const THD *thd) const final;
3126 bool get_date_internal(MYSQL_TIME *ltime) const final;
3127 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3128 int *error) final;
3129
3130 public:
3131 Field_newdate(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3132 uchar auto_flags_arg, const char *field_name_arg)
3133 : Field_temporal_with_date(ptr_arg, null_ptr_arg, null_bit_arg,
3134 auto_flags_arg, field_name_arg, MAX_DATE_WIDTH,
3135 0) {}
3136 Field_newdate(bool is_nullable_arg, const char *field_name_arg)
3138 is_nullable_arg ? &dummy_null_buffer : nullptr,
3139 0, NONE, field_name_arg, MAX_DATE_WIDTH, 0) {}
3140 enum_field_types type() const final { return MYSQL_TYPE_DATE; }
3142 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_UINT24; }
3144 longlong val_int() const final;
3145 longlong val_time_temporal() const final;
3146 longlong val_date_temporal() const final;
3147 String *val_str(String *, String *) const final;
3148 bool send_to_protocol(Protocol *protocol) const final;
3149 int cmp(const uchar *, const uchar *) const final;
3151 size_t make_sort_key(uchar *buff, size_t length) const final;
3152 uint32 pack_length() const final { return PACK_LENGTH; }
3153 void sql_type(String &str) const final;
3154 bool zero_pack() const final { return true; }
3155 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
3157 assert(type() == MYSQL_TYPE_DATE);
3158 assert(real_type() == MYSQL_TYPE_NEWDATE);
3159 return new (mem_root) Field_newdate(*this);
3160 }
3161};
3162
3163/**
3164 Abstract class for TIME and TIME(N).
3165*/
3167 protected:
3168 bool convert_str_to_TIME(const char *str, size_t len, const CHARSET_INFO *cs,
3169 MYSQL_TIME *ltime, MYSQL_TIME_STATUS *status) final;
3170 /**
3171 @todo: convert_number_to_TIME returns conversion status through
3172 two different interfaces: return value and warning. It should be
3173 refactored to only use return value.
3174 */
3175 type_conversion_status convert_number_to_TIME(longlong nr, bool unsigned_val,
3176 int nanoseconds,
3177 MYSQL_TIME *ltime,
3178 int *warning) final;
3179 /**
3180 Low-level function to store MYSQL_TIME value.
3181 The value must be rounded or truncated according to decimals().
3182 */
3184 int *error) override = 0;
3185 /**
3186 Function to store time value.
3187 The value is rounded/truncated according to decimals() and sql_mode.
3188 */
3189 type_conversion_status store_internal_adjust_frac(MYSQL_TIME *ltime,
3190 int *warnings) final;
3191
3192 my_time_flags_t date_flags(const THD *thd) const final;
3194
3195 public:
3196 /**
3197 Constructor for Field_time_common
3198 @param ptr_arg See Field definition
3199 @param null_ptr_arg See Field definition
3200 @param null_bit_arg See Field definition
3201 @param auto_flags_arg See Field definition
3202 @param field_name_arg See Field definition
3203 @param dec_arg Number of second fraction digits, 0..6.
3204 */
3205 Field_time_common(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3206 uchar auto_flags_arg, const char *field_name_arg,
3207 uint8 dec_arg)
3208 : Field_temporal(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3209 field_name_arg, MAX_TIME_WIDTH, dec_arg) {}
3211 String *val_str(String *, String *) const final;
3212 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
3213 longlong val_date_temporal() const final;
3214 bool send_to_protocol(Protocol *protocol) const final;
3215};
3216
3217/*
3218 Field implementing TIME data type without fractional seconds.
3219 It will be removed eventually.
3220*/
3221class Field_time final : public Field_time_common {
3222 protected:
3223 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3224 int *error) final;
3225
3226 public:
3227 Field_time(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3228 uchar auto_flags_arg, const char *field_name_arg)
3229 : Field_time_common(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3230 field_name_arg, 0) {}
3231 Field_time(const char *field_name_arg)
3232 : Field_time_common(nullptr, nullptr, 0, NONE, field_name_arg, 0) {}
3233 enum_field_types type() const final { return MYSQL_TYPE_TIME; }
3234 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_INT24; }
3236 longlong val_int() const final;
3237 longlong val_time_temporal() const final;
3238 bool get_time(MYSQL_TIME *ltime) const final;
3239 int cmp(const uchar *, const uchar *) const final;
3241 size_t make_sort_key(uchar *buff, size_t length) const final;
3242 uint32 pack_length() const final { return 3; }
3243 void sql_type(String &str) const final;
3244 bool zero_pack() const final { return true; }
3246 assert(type() == MYSQL_TYPE_TIME);
3247 return new (mem_root) Field_time(*this);
3248 }
3249};
3250
3251/*
3252 Field implementing TIME(N) data type, where N=0..6.
3253*/
3254class Field_timef final : public Field_time_common {
3255 private:
3256 int do_save_field_metadata(uchar *metadata_ptr) const final {
3257 *metadata_ptr = decimals();
3258 return 1;
3259 }
3260
3261 protected:
3262 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3263 int *error) final;
3264
3265 public:
3266 /**
3267 Constructor for Field_timef
3268 @param ptr_arg See Field definition
3269 @param null_ptr_arg See Field definition
3270 @param null_bit_arg See Field definition
3271 @param auto_flags_arg See Field definition
3272 @param field_name_arg See Field definition
3273 @param dec_arg Number of second fraction digits, 0..6.
3274 */
3275 Field_timef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3276 uchar auto_flags_arg, const char *field_name_arg, uint8 dec_arg)
3277 : Field_time_common(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3278 field_name_arg, dec_arg) {}
3279 /**
3280 Constructor for Field_timef
3281 @param is_nullable_arg See Field definition
3282 @param field_name_arg See Field definition
3283 @param dec_arg Number of second fraction digits, 0..6.
3284 */
3285 Field_timef(bool is_nullable_arg, const char *field_name_arg, uint8 dec_arg)
3287 is_nullable_arg ? &dummy_null_buffer : nullptr, 0,
3288 NONE, field_name_arg, dec_arg) {}
3290 assert(type() == MYSQL_TYPE_TIME);
3291 return new (mem_root) Field_timef(*this);
3292 }
3293 uint decimals() const final { return dec; }
3294 enum_field_types type() const final { return MYSQL_TYPE_TIME; }
3299 double val_real() const final;
3300 longlong val_int() const final;
3301 longlong val_time_temporal() const final;
3302 bool get_time(MYSQL_TIME *ltime) const final;
3303 my_decimal *val_decimal(my_decimal *) const final;
3304 uint32 pack_length() const final { return my_time_binary_length(dec); }
3305 uint pack_length_from_metadata(uint field_metadata) const final {
3306 DBUG_TRACE;
3307 const uint tmp = my_time_binary_length(field_metadata);
3308 return tmp;
3309 }
3310 uint row_pack_length() const final { return pack_length(); }
3311 void sql_type(String &str) const final;
3312 bool zero_pack() const final { return true; }
3313 const CHARSET_INFO *sort_charset() const final { return &my_charset_bin; }
3315 size_t make_sort_key(uchar *to, size_t length) const final {
3316 memcpy(to, ptr, length);
3317 return length;
3318 }
3319 int cmp(const uchar *a_ptr, const uchar *b_ptr) const final {
3320 return memcmp(a_ptr, b_ptr, pack_length());
3321 }
3322};
3323
3324/*
3325 Field implementing DATETIME data type without fractional seconds.
3326 We will be removed eventually.
3327*/
3329 protected:
3330 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3331 int *error) final;
3332 bool get_date_internal(MYSQL_TIME *ltime) const final;
3333 my_time_flags_t date_flags(const THD *thd) const final;
3334 void store_timestamp_internal(const my_timeval *tm) final;
3335
3336 public:
3337 static const int PACK_LENGTH = 8;
3338
3339 /**
3340 DATETIME columns can be defined as having CURRENT_TIMESTAMP as the
3341 default value on inserts or updates. This constructor accepts a
3342 auto_flags argument which controls the column default expressions.
3343
3344 For DATETIME columns this argument is a bitmap combining two flags:
3345
3346 - DEFAULT_NOW - means that column has DEFAULT CURRENT_TIMESTAMP attribute.
3347 - ON_UPDATE_NOW - means that column has ON UPDATE CURRENT_TIMESTAMP.
3348
3349 (these two flags can be used orthogonally to each other).
3350 */
3351 Field_datetime(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3352 uchar auto_flags_arg, const char *field_name_arg)
3353 : Field_temporal_with_date_and_time(ptr_arg, null_ptr_arg, null_bit_arg,
3354 auto_flags_arg, field_name_arg, 0) {}
3355 Field_datetime(const char *field_name_arg)
3357 field_name_arg, 0) {}
3359 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_ULONGLONG; }
3360 using Field_temporal_with_date_and_time::store; // Make -Woverloaded-virtual
3361 type_conversion_status store(longlong nr, bool unsigned_val) final;
3363 longlong val_int() const final;
3364 String *val_str(String *, String *) const final;
3365 int cmp(const uchar *, const uchar *) const final;
3367 size_t make_sort_key(uchar *buff, size_t length) const final;
3368 uint32 pack_length() const final { return PACK_LENGTH; }
3369 void sql_type(String &str) const final;
3370 bool zero_pack() const final { return true; }
3371 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
3373 assert(type() == MYSQL_TYPE_DATETIME);
3374 return new (mem_root) Field_datetime(*this);
3375 }
3376 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final {
3377 return pack_int64(to, from, max_length);
3378 }
3379 const uchar *unpack(uchar *to, const uchar *from,
3380 uint param_data [[maybe_unused]]) final {
3381 return unpack_int64(to, from);
3382 }
3383};
3384
3385/*
3386 Field implementing DATETIME(N) data type, where N=0..6.
3387*/
3389 protected:
3390 bool get_date_internal(MYSQL_TIME *ltime) const final;
3391 type_conversion_status store_internal(const MYSQL_TIME *ltime,
3392 int *error) final;
3393 my_time_flags_t date_flags(const THD *thd) const final;
3394 void store_timestamp_internal(const my_timeval *tm) final;
3395
3396 public:
3397 /**
3398 Constructor for Field_datetimef
3399 @param ptr_arg See Field definition
3400 @param null_ptr_arg See Field definition
3401 @param null_bit_arg See Field definition
3402 @param auto_flags_arg See Field definition
3403 @param field_name_arg See Field definition
3404 @param dec_arg Number of second fraction digits, 0..6.
3405 */
3406 Field_datetimef(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3407 uchar auto_flags_arg, const char *field_name_arg,
3408 uint8 dec_arg)
3409 : Field_temporal_with_date_and_timef(ptr_arg, null_ptr_arg, null_bit_arg,
3410 auto_flags_arg, field_name_arg,
3411 dec_arg) {}
3412 /**
3413 Constructor for Field_datetimef
3414 @param is_nullable_arg See Field definition
3415 @param field_name_arg See Field definition
3416 @param dec_arg Number of second fraction digits, 0..6.
3417 */
3418 Field_datetimef(bool is_nullable_arg, const char *field_name_arg,
3419 uint8 dec_arg)
3421 nullptr, is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
3422 field_name_arg, dec_arg) {}
3424 assert(type() == MYSQL_TYPE_DATETIME);
3425 return new (mem_root) Field_datetimef(*this);
3426 }
3427
3432 uint pack_length_from_metadata(uint field_metadata) const final {
3433 DBUG_TRACE;
3434 const uint tmp = my_datetime_binary_length(field_metadata);
3435 return tmp;
3436 }
3437 bool zero_pack() const final { return true; }
3438
3441 longlong val_date_temporal() const final;
3442 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
3443 void sql_type(String &str) const final;
3444};
3445
3447 public:
3448 Field_string(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
3449 uchar null_bit_arg, uchar auto_flags_arg,
3450 const char *field_name_arg, const CHARSET_INFO *cs)
3451 : Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
3452 auto_flags_arg, field_name_arg, cs) {}
3453 Field_string(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
3454 const CHARSET_INFO *cs)
3455 : Field_longstr(nullptr, len_arg,
3456 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
3457 field_name_arg, cs) {}
3458
3459 enum_field_types type() const final { return MYSQL_TYPE_STRING; }
3460 bool match_collation_to_optimize_range() const final { return true; }
3461 enum ha_base_keytype key_type() const final {
3463 }
3464 bool zero_pack() const final { return false; }
3466 charset()->cset->fill(charset(), (char *)ptr, field_length,
3467 (has_charset() ? ' ' : 0));
3468 return TYPE_OK;
3469 }
3470 type_conversion_status store(const char *to, size_t length,
3471 const CHARSET_INFO *charset) final;
3472 type_conversion_status store(longlong nr, bool unsigned_val) final;
3473 // Inherit the store() overloads that have not been overridden.
3475 double val_real() const final;
3476 longlong val_int() const final;
3477 String *val_str(String *, String *) const final;
3478 /**
3479 Get the C-string value, without using String class.
3480 @returns The C-string value of this field.
3481 */
3482 LEX_CSTRING val_str_quick() const {
3483 const char *string = pointer_cast<const char *>(ptr);
3484 return {string,
3486 }
3487 my_decimal *val_decimal(my_decimal *) const final;
3488 int cmp(const uchar *, const uchar *) const final;
3489 size_t make_sort_key(uchar *buff, size_t length) const final;
3490 size_t make_sort_key(uchar *to, size_t length, size_t trunc_pos) const final;
3491 void sql_type(String &str) const final;
3492 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final;
3493 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final;
3494 uint pack_length_from_metadata(uint field_metadata) const final {
3495 DBUG_PRINT("debug", ("field_metadata: 0x%04x", field_metadata));
3496 if (field_metadata == 0) return row_pack_length();
3497 return (((field_metadata >> 4) & 0x300) ^ 0x300) +
3498 (field_metadata & 0x00ff);
3499 }
3500 bool compatible_field_size(uint field_metadata, Relay_log_info *rli,
3501 uint16 mflags, int *order_var) const final;
3502 uint row_pack_length() const final { return field_length; }
3503 uint max_packed_col_length() const final;
3505 bool has_charset() const final {
3506 return charset() == &my_charset_bin ? false : true;
3507 }
3509 assert(real_type() == MYSQL_TYPE_STRING);
3510 return new (mem_root) Field_string(*this);
3511 }
3512 size_t get_key_image(uchar *buff, size_t length, imagetype type) const final;
3513 bool is_text_key_type() const final { return binary() ? false : true; }
3514
3515 private:
3516 int do_save_field_metadata(uchar *first_byte) const final;
3517};
3518
3520 public:
3521 Field_varstring(uchar *ptr_arg, uint32 len_arg, uint length_bytes_arg,
3522 uchar *null_ptr_arg, uchar null_bit_arg, uchar auto_flags_arg,
3523 const char *field_name_arg, TABLE_SHARE *share,
3524 const CHARSET_INFO *cs);
3525 Field_varstring(uint32 len_arg, bool is_nullable_arg,
3526 const char *field_name_arg, TABLE_SHARE *share,
3527 const CHARSET_INFO *cs);
3528
3529 enum_field_types type() const final { return MYSQL_TYPE_VARCHAR; }
3530 bool match_collation_to_optimize_range() const final { return true; }
3531 enum ha_base_keytype key_type() const final;
3532 uint row_pack_length() const final { return field_length; }
3533 bool zero_pack() const final { return false; }
3534 uint32 pack_length() const final {
3535 return (uint32)field_length + length_bytes;
3536 }
3537 uint32 key_length() const final { return (uint32)field_length; }
3538 type_conversion_status store(const char *to, size_t length,
3539 const CHARSET_INFO *charset) override;
3540 type_conversion_status store(longlong nr, bool unsigned_val) final;
3541 // Inherit the store() overloads that have not been overridden.
3543 double val_real() const final;
3544 longlong val_int() const final;
3545 String *val_str(String *, String *) const override;
3546 my_decimal *val_decimal(my_decimal *) const final;
3547 int cmp_max(const uchar *, const uchar *, uint max_length) const final;
3548 int cmp(const uchar *a, const uchar *b) const final {
3549 return cmp_max(a, b, ~0U);
3550 }
3551 size_t make_sort_key(uchar *buff, size_t length) const final;
3552 size_t make_sort_key(uchar *to, size_t length, size_t trunc_pos) const final;
3553 size_t get_key_image(uchar *buff, size_t length, imagetype type) const final;
3554 void set_key_image(const uchar *buff, size_t length) final;
3555 void sql_type(String &str) const final;
3556 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final;
3557 const uchar *unpack(uchar *to, const uchar *from, uint param_data) final;
3558 int cmp_binary(const uchar *a, const uchar *b,
3559 uint32 max_length = ~0L) const final;
3560 int key_cmp(const uchar *, const uchar *) const final;
3561 int key_cmp(const uchar *str, uint length) const final;
3562
3563 uint32 data_length(ptrdiff_t row_offset = 0) const final;
3565 bool has_charset() const final {
3566 return charset() == &my_charset_bin ? false : true;
3567 }
3568 Field *new_field(MEM_ROOT *root, TABLE *new_table) const final;
3569 Field *new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr,
3570 uchar *new_null_ptr, uint new_null_bit) const final;
3572 assert(type() == MYSQL_TYPE_VARCHAR);
3573 assert(real_type() == MYSQL_TYPE_VARCHAR);
3574 return new (mem_root) Field_varstring(*this);
3575 }
3576 uint is_equal(const Create_field *new_field) const final;
3577 void hash(ulong *nr, ulong *nr2) const final;
3578 const uchar *data_ptr() const final { return ptr + length_bytes; }
3579 bool is_text_key_type() const final { return binary() ? false : true; }
3580 uint32 get_length_bytes() const override { return length_bytes; }
3581
3582 private:
3583 /* Store number of bytes used to store length (1 or 2) */
3585
3586 int do_save_field_metadata(uchar *first_byte) const final;
3587};
3588
3590 virtual type_conversion_status store_internal(const char *from, size_t length,
3591 const CHARSET_INFO *cs);
3592 /**
3593 Copy value to memory storage.
3594 */
3595 type_conversion_status store_to_mem(const char *from, size_t length,
3596 const CHARSET_INFO *cs, size_t max_length,
3598
3599 protected:
3600 /**
3601 The number of bytes used to represent the length of the blob.
3602 */
3604
3605 /**
3606 The 'value'-object is a cache fronting the storage engine.
3607 */
3609
3610 private:
3611 /**
3612 In order to support update of virtual generated columns of blob type,
3613 we need to allocate the space blob needs on server for old_row and
3614 new_row respectively. This variable is used to record the
3615 allocated blob space for old_row.
3616 */
3618
3619 /**
3620 Whether we need to move the content of 'value' to 'old_value' before
3621 updating the BLOB stored in 'value'. This needs to be done for
3622 updates of BLOB columns that are virtual since the storage engine
3623 does not have its own copy of the old 'value'. This variable is set
3624 to true when we read the data into 'value'. It is reset when we move
3625 'value' to 'old_value'. The purpose of having this is to avoid that we
3626 do the move operation from 'value' to 'old_value' more than one time per
3627 record.
3628 Currently, this variable is introduced because the following call in
3629 sql_data_change.cc:
3630 \/\**
3631 @todo combine this call to update_generated_write_fields() with the one
3632 in fill_record() to avoid updating virtual generated fields twice.
3633 *\/
3634 if (table->has_gcol())
3635 update_generated_write_fields(table->write_set, table);
3636 When the @todo is done, m_keep_old_value can be deleted.
3637 */
3639
3640 /**
3641 Backup String for table's blob fields.
3642 UPDATE of a virtual field (for index update) requires two values to be
3643 kept at the same time - 'new' and 'old' since SE (InnoDB) doesn't know the
3644 latter. In the case when there was an indexed record, it got deleted and
3645 When INSERT inserts into an index a record that coincides with a
3646 previously deleted one, InnoDB needs to recalculate value that was
3647 deleted in order to properly insert the new one.
3648 When two above overlap, a field have to keep 3 different values at the
3649 same time - 'new', 'old' and 'deleted'.
3650 This backup_value is used by @see my_eval_gcolumn_expr_helper() to save
3651 'new' and provide space for 'deleted' to avoid thrashing the former.
3652 Unlike the old_value, backup_value is allocated once and reused for each
3653 new re-calculation, to avoid excessive [re-]allocations. It's freed at the
3654 end of statement. Since InnoDB consumes calculated values only after all
3655 needed table's virtual fields were calculated, we have to have such backup
3656 buffer for each field.
3657
3658 Also used for set operation hashing: we need to compare the new
3659 and the existing record with the same hash.
3660 */
3662
3663#ifndef NDEBUG
3664 /**
3665 Whether the field uses table's backup value storage. @see
3666 TABLE::m_blob_backup. Used only for debug.
3667 */
3668 bool m_uses_backup{false};
3669#endif
3670
3671 protected:
3672 /**
3673 Store ptr and length.
3674 */
3675 void store_ptr_and_length(const char *from, uint32 length) {
3676 store_length(length);
3677 memmove(ptr + packlength, &from, sizeof(char *));
3678 }
3679
3680 public:
3681 Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
3682 uchar auto_flags_arg, const char *field_name_arg,
3683 TABLE_SHARE *share, uint blob_pack_length, const CHARSET_INFO *cs);
3684
3685 Field_blob(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
3686 const CHARSET_INFO *cs, bool set_packlength)
3687 : Field_longstr(nullptr, len_arg,
3688 is_nullable_arg ? &dummy_null_buffer : nullptr, 0, NONE,
3689 field_name_arg, cs),
3690 packlength(4),
3691 m_keep_old_value(false) {
3693 if (set_packlength) {
3694 packlength = len_arg <= 255
3695 ? 1
3696 : len_arg <= 65535 ? 2 : len_arg <= 16777215 ? 3 : 4;
3697 }
3698 }
3699
3700 /// Copy static information and reset dynamic information.
3702 : Field_longstr(field),
3703 packlength(field.packlength),
3704 value(),
3705 old_value(),
3706 m_keep_old_value(field.m_keep_old_value),
3707 m_blob_backup() {
3708#ifndef NDEBUG
3709 m_uses_backup = field.m_uses_backup;
3710#endif
3711 }
3712
3713 explicit Field_blob(uint32 packlength_arg);
3714
3715 /* Note that the default copy constructor is used, in clone() */
3716 enum_field_types type() const override { return MYSQL_TYPE_BLOB; }
3717 bool match_collation_to_optimize_range() const override { return true; }
3718 enum ha_base_keytype key_type() const override {
3720 }
3721 type_conversion_status store(const char *to, size_t length,
3722 const CHARSET_INFO *charset) override;
3723 type_conversion_status store(double nr) override;
3724 type_conversion_status store(longlong nr, bool unsigned_val) override;
3725 type_conversion_status store(const Field *from);
3726 double val_real() const override;
3727 longlong val_int() const override;
3728 String *val_str(String *, String *) const override;
3729 my_decimal *val_decimal(my_decimal *) const override;
3730 int cmp_max(const uchar *, const uchar *, uint max_length) const final;
3731 int cmp(const uchar *a, const uchar *b) const final {
3732 return cmp_max(a, b, ~0U);
3733 }
3734 int cmp(const uchar *a, uint32 a_length, const uchar *b,
3735 uint32 b_length) const; // No override.
3736 int cmp_binary(const uchar *a, const uchar *b,
3737 uint32 max_length = ~0L) const override;
3738 int key_cmp(const uchar *, const uchar *) const override;
3739 int key_cmp(const uchar *str, uint length) const override;
3740 uint32 key_length() const override { return 0; }
3741 size_t make_sort_key(uchar *buff, size_t length) const override;
3742 size_t make_sort_key(uchar *to, size_t length, size_t trunc_pos) const final;
3743 uint32 pack_length() const final {
3744 return (uint32)(packlength + portable_sizeof_char_ptr);
3745 }
3746
3747 /**
3748 Return the packed length without the pointer size added.
3749
3750 This is used to determine the size of the actual data in the row
3751 buffer.
3752
3753 @returns The length of the raw data itself without the pointer.
3754 */
3755 uint32 pack_length_no_ptr() const { return (uint32)(packlength); }
3756 uint row_pack_length() const final { return pack_length_no_ptr(); }
3757 uint32 max_data_length() const final {
3758 return (uint32)(((ulonglong)1 << (packlength * 8)) - 1);
3759 }
3760 size_t get_field_buffer_size() { return value.alloced_length(); }
3761 void store_length(uchar *i_ptr, uint i_packlength, uint32 i_number);
3762 inline void store_length(uint32 number) {
3763 store_length(ptr, packlength, number);
3764 }
3765 uint32 data_length(ptrdiff_t row_offset = 0) const final {
3766 return get_length(row_offset);
3767 }
3768 uint32 get_length(ptrdiff_t row_offset = 0) const;
3769 uint32 get_length(const uchar *ptr, uint packlength) const;
3770 uint32 get_length(const uchar *ptr_arg) const;
3771 /** Get a const pointer to the BLOB data of this field. */
3772 const uchar *get_blob_data() const { return get_blob_data(ptr + packlength); }
3773 /** Get a non-const pointer to the BLOB data of this field. */
3774 uchar *get_blob_data(ptrdiff_t row_offset = 0) {
3775 // row_offset is only used by NDB
3776 return get_blob_data(ptr + packlength + row_offset);
3777 }
3778 /** Get a const pointer to the BLOB data of this field. */
3779 const uchar *data_ptr() const final { return get_blob_data(); }
3780
3781 protected:
3782 /**
3783 Get the BLOB data pointer stored at the specified position in the record
3784 buffer.
3785 */
3786 static uchar *get_blob_data(const uchar *position) {
3787 uchar *data;
3788 memcpy(&data, position, sizeof(data));
3789 return data;
3790 }
3791
3792 public:
3793 void set_ptr(const uchar *length, const uchar *data) {
3794 memcpy(ptr, length, packlength);
3795 memcpy(ptr + packlength, &data, sizeof(char *));
3796 }
3797 void set_ptr_offset(ptrdiff_t ptr_diff, uint32 length, const uchar *data) {
3798 uchar *ptr_ofs = ptr + ptr_diff;
3799 store_length(ptr_ofs, packlength, length);
3800 memcpy(ptr_ofs + packlength, &data, sizeof(char *));
3801 }
3802 void set_ptr(uint32 length, const uchar *data) {
3803 set_ptr_offset(0, length, data);
3804 }
3805 size_t get_key_image(uchar *buff, size_t length,
3806 imagetype type) const override;
3807 void set_key_image(const uchar *buff, size_t length) final;
3808 void sql_type(String &str) const override;
3809 bool copy();
3810 Field_blob *clone(MEM_ROOT *mem_root) const override {
3811 assert(type() == MYSQL_TYPE_BLOB);
3812 return new (mem_root) Field_blob(*this);
3813 }
3814 uchar *pack(uchar *to, const uchar *from, size_t max_length) const final;
3815 uchar *pack_with_metadata_bytes(uchar *to, const uchar *from,
3816 uint max_length) const final;
3817 const uchar *unpack(uchar *, const uchar *from, uint param_data) final;
3818 uint max_packed_col_length() const final;
3819 void mem_free() final {
3820 // Free all allocated space
3821 value.mem_free();
3822 old_value.mem_free();
3823 m_blob_backup.mem_free();
3824 }
3825 bool has_charset() const override {
3826 return charset() == &my_charset_bin ? false : true;
3827 }
3828 uint32 max_display_length() const final;
3829 uint32 char_length() const override;
3830 bool copy_blob_value(MEM_ROOT *mem_root);
3831 uint is_equal(const Create_field *new_field) const override;
3832 bool is_text_key_type() const final { return binary() ? false : true; }
3833
3834 /**
3835 Mark that the BLOB stored in value should be copied before updating it.
3836
3837 When updating virtual generated columns we need to keep the old
3838 'value' for BLOBs since this can be needed when the storage engine
3839 does the update. During read of the record the old 'value' for the
3840 BLOB is evaluated and stored in 'value'. This function is to be used
3841 to specify that we need to copy this BLOB 'value' into 'old_value'
3842 before we compute the new BLOB 'value'. For more information @see
3843 Field_blob::keep_old_value().
3844 */
3845 void set_keep_old_value(bool old_value_flag) {
3846 /*
3847 We should only need to keep a copy of the blob 'value' in the case
3848 where this is a virtual generated column (that is indexed).
3849 */
3850 assert(is_virtual_gcol());
3851
3852 /*
3853 If set to true, ensure that 'value' is copied to 'old_value' when
3854 keep_old_value() is called.
3855 */
3856 m_keep_old_value = old_value_flag;
3857 }
3858
3859 /**
3860 Save the current BLOB value to avoid that it gets overwritten.
3861
3862 This is used when updating virtual generated columns that are
3863 BLOBs. Some storage engines require that we have both the old and
3864 new BLOB value for virtual generated columns that are indexed in
3865 order for the storage engine to be able to maintain the index. This
3866 function will transfer the buffer storing the current BLOB value
3867 from 'value' to 'old_value'. This avoids that the current BLOB value
3868 is over-written when the new BLOB value is saved into this field.
3869
3870 The reason this requires special handling when updating/deleting
3871 virtual columns of BLOB type is that the BLOB value is not known to
3872 the storage engine. For stored columns, the "old" BLOB value is read
3873 by the storage engine, Field_blob is made to point to the engine's
3874 internal buffer; Field_blob's internal buffer (Field_blob::value)
3875 isn't used and remains available to store the "new" value. For
3876 virtual generated columns, the "old" value is written directly into
3877 Field_blob::value when reading the record to be
3878 updated/deleted. This is done in update_generated_read_fields().
3879 Since, in this case, the "old" value already occupies the place to
3880 store the "new" value, we must call this function before we write
3881 the "new" value into Field_blob::value object so that the "old"
3882 value does not get over-written. The table->record[1] buffer will
3883 have a pointer that points to the memory buffer inside
3884 old_value. The storage engine will use table->record[1] to read the
3885 old value for the BLOB and use table->record[0] to read the new
3886 value.
3887
3888 This function must be called before we store the new BLOB value in
3889 this field object.
3890 */
3892 /*
3893 We should only need to keep a copy of the blob value in the case
3894 where this is a virtual generated column (that is indexed).
3895 */
3896 assert(is_virtual_gcol());
3897
3898 // Transfer ownership of the current BLOB value to old_value
3899 if (m_keep_old_value) {
3900 old_value.takeover(value);
3901 m_keep_old_value = false;
3902 }
3903 }
3904
3905 /**
3906 Use to store the blob value into an allocated space.
3907 */
3908 void store_in_allocated_space(const char *from, uint32 length) {
3909 store_ptr_and_length(from, length);
3910 }
3911
3912 /**
3913 Backup data stored in 'value' into the backup_value
3914 @see Field_blob::backup_value
3915
3916 @returns
3917 true if backup fails
3918 false otherwise
3919 */
3920 bool backup_blob_field();
3921
3922 /**
3923 Restore backup value
3924 @see Field_blob::backup_value
3925 */
3926 void restore_blob_backup();
3927
3928 private:
3929 int do_save_field_metadata(uchar *first_byte) const override;
3930};
3931
3932class Field_geom final : public Field_blob {
3933 private:
3934 const std::optional<gis::srid_t> m_srid;
3935
3936 type_conversion_status store_internal(const char *from, size_t length,
3937 const CHARSET_INFO *cs) final;
3938
3939 public:
3941
3942 Field_geom(uchar *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
3943 uchar auto_flags_arg, const char *field_name_arg,
3944 TABLE_SHARE *share, uint blob_pack_length,
3945 enum geometry_type geom_type_arg, std::optional<gis::srid_t> srid)
3946 : Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3947 field_name_arg, share, blob_pack_length, &my_charset_bin),
3948 m_srid(srid),
3949 geom_type(geom_type_arg) {}
3950 Field_geom(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg,
3951 enum geometry_type geom_type_arg, std::optional<gis::srid_t> srid)
3952 : Field_blob(len_arg, is_nullable_arg, field_name_arg, &my_charset_bin,
3953 false),
3954 m_srid(srid),
3955 geom_type(geom_type_arg) {}
3956 enum ha_base_keytype key_type() const final { return HA_KEYTYPE_VARBINARY2; }
3958 bool match_collation_to_optimize_range() const final { return false; }
3959 void sql_type(String &str) const final;
3960 using Field_blob::store;
3961 type_conversion_status store(double nr) final;
3962 type_conversion_status store(longlong nr, bool unsigned_val) final;
3964 type_conversion_status store(const char *from, size_t length,
3965 const CHARSET_INFO *cs) final;
3966
3967 /**
3968 Non-nullable GEOMETRY types cannot have defaults,
3969 but the underlying blob must still be reset.
3970 */
3973 if (res != TYPE_OK) return res;
3974 return (is_nullable() || table->is_nullable())
3975 ? TYPE_OK
3977 }
3978
3979 geometry_type get_geometry_type() const final { return geom_type; }
3981 assert(type() == MYSQL_TYPE_GEOMETRY);
3982 return new (mem_root) Field_geom(*this);
3983 }
3984 uint is_equal(const Create_field *new_field) const final;
3985
3986 std::optional<gis::srid_t> get_srid() const { return m_srid; }
3987};
3988
3989/// A field that stores a JSON value.
3990class Field_json : public Field_blob {
3991 type_conversion_status unsupported_conversion();
3992 type_conversion_status store_binary(const char *ptr, size_t length);
3993
3994 public:
3995 Field_json(uchar *ptr_arg, uchar *null_ptr_arg, uint null_bit_arg,
3996 uchar auto_flags_arg, const char *field_name_arg,
3997 TABLE_SHARE *share, uint blob_pack_length)
3998 : Field_blob(ptr_arg, null_ptr_arg, null_bit_arg, auto_flags_arg,
3999 field_name_arg, share, blob_pack_length, &my_charset_bin) {}
4000
4001 Field_json(uint32 len_arg, bool is_nullable_arg, const char *field_name_arg)
4002 : Field_blob(len_arg, is_nullable_arg, field_name_arg, &my_charset_bin,
4003 false) {}
4004
4005 enum_field_types type() const override { return MYSQL_TYPE_JSON; }
4006 void sql_type(String &str) const override;
4007 /**
4008 Return a text charset so that string functions automatically
4009 convert the field value to string and treat it as a non-binary
4010 string.
4011 */
4012 const CHARSET_INFO *charset() const override {
4013 return &my_charset_utf8mb4_bin;
4014 }
4015 /**
4016 Sort should treat the field as binary and not attempt any
4017 conversions.
4018 */
4019 const CHARSET_INFO *sort_charset() const final { return field_charset; }
4020 /**
4021 JSON columns don't have an associated charset. Returning false
4022 here prevents SHOW CREATE TABLE from attaching a CHARACTER SET
4023 clause to the column.
4024 */
4025 bool has_charset() const final { return false; }
4026 type_conversion_status store(const char *to, size_t length,
4027 const CHARSET_INFO *charset) override;
4028 type_conversion_status store(double nr) override;
4029 type_conversion_status store(longlong nr, bool unsigned_val) override;
4031 type_conversion_status store_json(const Json_wrapper *json);
4032 type_conversion_status store_time(MYSQL_TIME *ltime, uint8 dec_arg) final;
4034
4035 bool pack_diff(uchar **to, ulonglong value_options) const final;
4036 /**
4037 Return the length of this field, taking into consideration that it may be in
4038 partial format.
4039
4040 This is the format used when writing the binary log in row format
4041 and using a partial format according to
4042 @@session.binlog_row_value_options.
4043
4044 @param[in] value_options The value of binlog_row_value options.
4045
4046 @param[out] diff_vector_p If this is not NULL, the pointer it
4047 points to will be set to NULL if the field is to be stored in full
4048 format, or to the Json_diff_vector if the field is to be stored in
4049 partial format.
4050
4051 @return The number of bytes needed when writing to the binlog: the
4052 size of the full format if stored in full format and the size of
4053 the diffs if stored in partial format.
4054 */
4055 longlong get_diff_vector_and_length(
4056 ulonglong value_options,
4057 const Json_diff_vector **diff_vector_p = nullptr) const;
4058 /**
4059 Return true if the before-image and after-image for this field are
4060 equal.
4061 */
4062 bool is_before_image_equal_to_after_image() const;
4063 /**
4064 Read the binary diff from the given buffer, and apply it to this field.
4065
4066 @param[in,out] from Pointer to buffer where the binary diff is stored.
4067 This will be changed to point to the next byte after the field.
4068
4069 @retval false Success
4070 @retval true Error (e.g. failed to apply the diff). The error has
4071 been reported through my_error.
4072 */
4073 bool unpack_diff(const uchar **from);
4074
4075 /**
4076 Retrieve the field's value as a JSON wrapper. It
4077 there is an error, wr is not modified and we return
4078 false, else true.
4079
4080 @param[out] wr the JSON value
4081 @return true if a value is retrieved (or NULL), false if error
4082 */
4083 bool val_json(Json_wrapper *wr) const;
4084
4085 /**
4086 Retrieve the JSON as an int if possible. This requires a JSON scalar
4087 of suitable type.
4088
4089 @returns the JSON value as an int
4090 */
4091 longlong val_int() const final;
4092
4093 /**
4094 Retrieve the JSON as a double if possible. This requires a JSON scalar
4095 of suitable type.
4096
4097 @returns the JSON value as a double
4098 */
4099 double val_real() const final;
4100
4101 /**
4102 Retrieve the JSON value stored in this field as text
4103
4104 @param[in,out] buf1 string buffer for converting JSON value to string
4105 @param[in,out] buf2 unused
4106 */
4107 String *val_str(String *buf1, String *buf2) const final;
4108 my_decimal *val_decimal(my_decimal *m) const final;
4109 bool get_time(MYSQL_TIME *ltime) const final;
4110 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) const final;
4111 Field_json *clone(MEM_ROOT *mem_root) const override;
4112 uint is_equal(const Create_field *new_field) const final;
4113 Item_result cast_to_int_type() const final { return INT_RESULT; }
4114 int cmp_binary(const uchar *a, const uchar *b,
4115 uint32 max_length = ~0L) const final;
4117 size_t make_sort_key(uchar *to, size_t length) const override;
4118
4119 /**
4120 Make a hash key that can be used by sql_executor.cc/unique_hash
4121 in order to support SELECT DISTINCT
4122
4123 @param[in] hash_val An initial hash value.
4124 */
4125 ulonglong make_hash_key(ulonglong hash_val) const;
4126
4127 /**
4128 Get a read-only pointer to the binary representation of the JSON document
4129 in this field.
4130
4131 @param row_offset Field's data offset
4132 */
4133 const char *get_binary(ptrdiff_t row_offset = 0) const;
4134};
4135
4136/**
4137 Field that stores array of values of the same type.
4138
4139 This Field class is used together with Item_func_array_cast class
4140 (CAST( .. AS .. ARRAY) function) in implementation of multi-valued index.
4141 Effectively it's a JSON field that contains a single JSON array. When a
4142 JSON value is stored, it's checked to be either a scalar, or an array.
4143 All source values are converted using the internal conversion field and
4144 stored as an array. Field_typed_array ensures that all values stored
4145 in the array have the same type and precision - the one specified by user.
4146 This way InnoDB doesn't have to do the conversion on its own and can easily
4147 index them.
4148
4149 The Field_typed_array always reports type of its element and from this
4150 point of view it's undistinguishable from regular field having the same
4151 type. Due to that, fields are differentiated by is_array() property.
4152 Field_typed_array returns true, all other fields - false.
4153
4154 For conversion and index applicability tests, Field_typed_array employs a
4155 conversion field, which is a regular Field class of array's element type.
4156 It's stored in the m_conv_field. All Field_typed_array::store_*() methods
4157 store values to the conversion field. Conversion field and typed array
4158 field are sharing same field_index, to allow correct read/write_set
4159 checks. So the field always have to be marked for read in order to allow
4160 read of conversions' results.
4161
4162 @see Item_func_array_cast
4163*/
4164
4165class Field_typed_array final : public Field_json {
4166 /// Conversion item_field
4167 Item_field *m_conv_item{nullptr};
4168 /// The array element's real type.
4170 /// Element's decimals
4172 /// Element's charset
4174 const bool