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