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