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