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