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