MySQL 8.0.39
Source Code Documentation
field.h File Reference
#include <assert.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include <optional>
#include "decimal.h"
#include "field_types.h"
#include "lex_string.h"
#include "libbinlogevents/export/binary_log_funcs.h"
#include "m_ctype.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_bitmap.h"
#include "my_dbug.h"
#include "my_double2ulonglong.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "my_time.h"
#include "mysql/udf_registration_types.h"
#include "mysql_com.h"
#include "mysql_time.h"
#include "mysqld_error.h"
#include "sql/dd/types/column.h"
#include "sql/field_common_properties.h"
#include "sql/gis/srid.h"
#include "sql/sql_bitmap.h"
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/table.h"
#include "sql_string.h"
#include "template_utils.h"

Go to the source code of this file.

Classes

class  Value_generator
 Used for storing information associated with generated column, default values generated from expression or check constraint expression. More...
 
class  Field
 
class  Create_field_wrapper
 This class is a substitute for the Field classes during CREATE TABLE. More...
 
class  Field_num
 
class  Field_str
 
class  Field_longstr
 
class  Field_real
 
class  Field_decimal
 
class  Field_new_decimal
 
class  Field_tiny
 
class  Field_short
 
class  Field_medium
 
class  Field_long
 
class  Field_longlong
 
class  Field_float
 
class  Field_double
 
class  Field_null
 
class  Field_temporal
 
class  Field_temporal_with_date
 Abstract class for types with date with optional time, with or without fractional part: DATE, DATETIME, DATETIME(N), TIMESTAMP, TIMESTAMP(N). More...
 
class  Field_temporal_with_date_and_time
 Abstract class for types with date and time, with or without fractional part: DATETIME, DATETIME(N), TIMESTAMP, TIMESTAMP(N). More...
 
class  Field_temporal_with_date_and_timef
 Abstract class for types with date and time, with fractional part: DATETIME, DATETIME(N), TIMESTAMP, TIMESTAMP(N). More...
 
class  Field_timestamp
 
class  Field_timestampf
 
class  Field_year
 
class  Field_newdate
 
class  Field_time_common
 Abstract class for TIME and TIME(N). More...
 
class  Field_time
 
class  Field_timef
 
class  Field_datetime
 
class  Field_datetimef
 
class  Field_string
 
class  Field_varstring
 
class  Field_blob
 
class  Field_geom
 
class  Field_json
 A field that stores a JSON value. More...
 
class  Field_typed_array
 Field that stores array of values of the same type. More...
 
class  Field_enum
 
class  Field_set
 
class  Field_bit
 
class  Field_bit_as_char
 BIT field represented as chars for non-MyISAM tables. More...
 
class  Send_field
 
class  Copy_field
 

Macros

#define portable_sizeof_char_ptr   8
 
#define IS_EQUAL_NO   0
 
#define IS_EQUAL_YES   1
 
#define IS_EQUAL_PACK_LENGTH   2
 
#define my_charset_numeric   my_charset_latin1
 
#define MY_REPERTOIRE_NUMERIC   MY_REPERTOIRE_ASCII
 
#define ASSERT_COLUMN_MARKED_FOR_READ
 
#define ASSERT_COLUMN_MARKED_FOR_WRITE
 

Enumerations

enum  enum_check_fields : int { CHECK_FIELD_IGNORE = 0 , CHECK_FIELD_WARN , CHECK_FIELD_ERROR_FOR_NULL }
 
enum  Derivation {
  DERIVATION_IGNORABLE = 6 , DERIVATION_NUMERIC = 5 , DERIVATION_COERCIBLE = 4 , DERIVATION_SYSCONST = 3 ,
  DERIVATION_IMPLICIT = 2 , DERIVATION_NONE = 1 , DERIVATION_EXPLICIT = 0
}
 For use. More...
 
enum  column_format_type { COLUMN_FORMAT_TYPE_DEFAULT = 0 , COLUMN_FORMAT_TYPE_FIXED = 1 , COLUMN_FORMAT_TYPE_DYNAMIC = 2 }
 
enum  type_conversion_status {
  TYPE_OK = 0 , TYPE_NOTE_TIME_TRUNCATED , TYPE_NOTE_TRUNCATED , TYPE_WARN_OUT_OF_RANGE ,
  TYPE_WARN_TRUNCATED , TYPE_WARN_INVALID_STRING , TYPE_ERR_NULL_CONSTRAINT_VIOLATION , TYPE_ERR_BAD_VALUE ,
  TYPE_ERR_OOM
}
 Status when storing a value in a field or converting from one datatype to another. More...
 
enum  Value_generator_source : short { VGS_GENERATED_COLUMN = 0 , VGS_DEFAULT_EXPRESSION , VGS_CHECK_CONSTRAINT }
 Enum to indicate source for which value generator is used. More...
 

Functions

bool fields_are_memcpyable (const Field *to, const Field *from)
 Check if one can copy from “from” to “to” with a simple memcpy(), with pack_length() as the length. More...
 
type_conversion_status field_conv_slow (Field *to, const Field *from)
 Copy the value in "from" (assumed to be non-NULL) to "to", doing any required conversions in the process. More...
 
uint get_enum_pack_length (int elements)
 
uint get_set_pack_length (int elements)
 
type_conversion_status decimal_err_to_type_conv_status (int dec_error)
 
type_conversion_status time_warning_to_type_conversion_status (const int warn)
 Convert warnings returned from str_to_time() and str_to_datetime() to their corresponding type_conversion_status codes. More...
 
bool is_temporal_real_type (enum_field_types type)
 Tests if field real type is temporal, i.e. More...
 
bool real_type_with_now_as_default (enum_field_types type)
 Tests if field real type can have "DEFAULT CURRENT_TIMESTAMP", i.e. More...
 
bool real_type_with_now_on_update (enum_field_types type)
 Tests if field real type can have "ON UPDATE CURRENT_TIMESTAMP", i.e. More...
 
enum_field_types real_type_to_type (enum_field_types real_type)
 Convert temporal real types as returned by field->real_type() to field type as returned by field->type(). More...
 
enum_field_types blob_type_from_pack_length (uint pack_length)
 Return the appropriate MYSQL_TYPE_X_BLOB value based on the pack_length. More...
 
template<bool Is_big_endian>
void copy_integer (uchar *to, size_t to_length, const uchar *from, size_t from_length, bool is_unsigned)
 Copies an integer value to a format comparable with memcmp(). More...
 
Fieldmake_field (MEM_ROOT *mem_root_arg, TABLE_SHARE *share, uchar *ptr, size_t field_length, uchar *null_pos, uchar null_bit, enum_field_types field_type, const CHARSET_INFO *field_charset, Field::geometry_type geom_type, uchar auto_flags, TYPELIB *interval, const char *field_name, bool is_nullable, bool is_zerofill, bool is_unsigned, uint decimals, bool treat_bit_as_char, uint pack_length_override, std::optional< gis::srid_t > srid, bool is_array)
 This function should only be called from legacy code. More...
 
Fieldmake_field (const Create_field &create_field, TABLE_SHARE *share, const char *field_name, size_t field_length, uchar *ptr, uchar *null_pos, size_t null_bit)
 Instantiates a Field object with the given name and record buffer values. More...
 
Fieldmake_field (const Create_field &create_field, TABLE_SHARE *share, uchar *ptr, uchar *null_pos, size_t null_bit)
 Instantiates a Field object with the given record buffer values. More...
 
Fieldmake_field (const Create_field &create_field, TABLE_SHARE *share)
 Instantiates a Field object without a record buffer. More...
 
enum_field_types get_blob_type_from_length (size_t length)
 
size_t calc_pack_length (enum_field_types type, size_t length)
 
size_t calc_pack_length (dd::enum_column_types type, size_t char_length, size_t elements_count, bool treat_bit_as_char, uint numeric_scale, bool is_unsigned)
 Calculate the length of the in-memory representation of the column from information which can be retrieved from dd::Column or Ha_fk_column_type describing it. More...
 
uint32 calc_key_length (enum_field_types sql_type, uint32 length, uint32 decimals, bool is_unsigned, uint32 elements)
 Calculate key length for field from its type, length and other attributes. More...
 
type_conversion_status set_field_to_null (Field *field)
 
type_conversion_status set_field_to_null_with_conversions (Field *field, bool no_conversions)
 Set field to NULL or TIMESTAMP or to next auto_increment number. More...
 
type_conversion_status store_internal_with_error_check (Field_new_decimal *field, int conversion_err, my_decimal *value)
 
Create_fieldgenerate_create_field (THD *thd, Item *source_item, TABLE *tmp_table)
 Generate a Create_field from an Item. More...
 
bool is_blob (enum_field_types sql_type)
 
const char * get_field_name_or_expression (THD *thd, const Field *field)
 
bool pre_validate_value_generator_expr (Item *expression, const char *name, Value_generator_source source)
 Perform per item-type checks to determine if the expression is allowed for a generated column, default value expression, a functional index or a check constraint. More...
 

Macro Definition Documentation

◆ ASSERT_COLUMN_MARKED_FOR_READ

#define ASSERT_COLUMN_MARKED_FOR_READ
Value:
assert(!table || \
(!table->read_set || bitmap_is_set(table->read_set, field_index())))
static bool bitmap_is_set(const MY_BITMAP *map, uint bit)
Definition: my_bitmap.h:95

◆ ASSERT_COLUMN_MARKED_FOR_WRITE

#define ASSERT_COLUMN_MARKED_FOR_WRITE
Value:
assert(!table || (!table->write_set || \
bitmap_is_set(table->write_set, field_index())))

◆ IS_EQUAL_NO

#define IS_EQUAL_NO   0

◆ IS_EQUAL_PACK_LENGTH

#define IS_EQUAL_PACK_LENGTH   2

◆ IS_EQUAL_YES

#define IS_EQUAL_YES   1

◆ my_charset_numeric

#define my_charset_numeric   my_charset_latin1

◆ MY_REPERTOIRE_NUMERIC

#define MY_REPERTOIRE_NUMERIC   MY_REPERTOIRE_ASCII

◆ portable_sizeof_char_ptr

#define portable_sizeof_char_ptr   8

Enumeration Type Documentation

◆ column_format_type

Enumerator
COLUMN_FORMAT_TYPE_DEFAULT 
COLUMN_FORMAT_TYPE_FIXED 
COLUMN_FORMAT_TYPE_DYNAMIC 

◆ Derivation

enum Derivation

For use.

See also
DTCollation::aggregate()
Enumerator
DERIVATION_IGNORABLE 
DERIVATION_NUMERIC 
DERIVATION_COERCIBLE 
DERIVATION_SYSCONST 
DERIVATION_IMPLICIT 
DERIVATION_NONE 
DERIVATION_EXPLICIT 

◆ enum_check_fields

enum enum_check_fields : int
Enumerator
CHECK_FIELD_IGNORE 
CHECK_FIELD_WARN 
CHECK_FIELD_ERROR_FOR_NULL 

◆ type_conversion_status

Status when storing a value in a field or converting from one datatype to another.

The values should be listed in order of increasing seriousness so that if two type_conversion_status variables are compared, the bigger one is most serious.

Enumerator
TYPE_OK 

Storage/conversion went fine.

TYPE_NOTE_TIME_TRUNCATED 

A minor problem when converting between temporal values, e.g.

if datetime is converted to date the time information is lost.

TYPE_NOTE_TRUNCATED 

Value was stored, but something was cut.

What was cut is considered insignificant enough to only issue a note. Example: trying to store a number with 5 decimal places into a field that can only store 3 decimals. The number rounded to 3 decimal places should be stored. Another example: storing the string "foo " into a VARCHAR(3). The string "foo" is stored in this case, so only whitespace is cut.

TYPE_WARN_OUT_OF_RANGE 

Value outside min/max limit of datatype.

The min/max value is stored by Field::store() instead (if applicable)

TYPE_WARN_TRUNCATED 

Value was stored, but something was cut.

What was cut is considered significant enough to issue a warning. Example: storing the string "foo" into a VARCHAR(2). The string "fo" is stored in this case. Another example: storing the string "2010-01-01foo" into a DATE. The garbage in the end of the string is cut in this case.

TYPE_WARN_INVALID_STRING 

Value has invalid string data.

When present in a predicate with equality operator, range optimizer returns an impossible where.

TYPE_ERR_NULL_CONSTRAINT_VIOLATION 

Trying to store NULL in a NOT NULL field.

TYPE_ERR_BAD_VALUE 

Store/convert incompatible values, like converting "foo" to a date.

TYPE_ERR_OOM 

Out of memory.

◆ Value_generator_source

enum Value_generator_source : short

Enum to indicate source for which value generator is used.

This is needed while unpacking value generator expression and pre-validating the expression for generated column, default expression or check constraint.

Enumerator
VGS_GENERATED_COLUMN 
VGS_DEFAULT_EXPRESSION 
VGS_CHECK_CONSTRAINT 

Function Documentation

◆ blob_type_from_pack_length()

enum_field_types blob_type_from_pack_length ( uint  pack_length)
inline

Return the appropriate MYSQL_TYPE_X_BLOB value based on the pack_length.

Parameters
pack_lengthpack_length for BLOB
Return values
MYSQL_TYPE_X_BLOBcorresponding to pack_length.

◆ calc_key_length()

uint32 calc_key_length ( enum_field_types  sql_type,
uint32  length,
uint32  decimals,
bool  is_unsigned,
uint32  elements 
)

Calculate key length for field from its type, length and other attributes.

Note
for string fields "length" parameter is assumed to take into account character set.

TODO: Get rid of this function as its code is redundant with Field::key_length() code. However creation of Field object using make_field() just to call Field::key_length() is probably overkill.

◆ calc_pack_length() [1/2]

size_t calc_pack_length ( dd::enum_column_types  type,
size_t  char_length,
size_t  elements_count,
bool  treat_bit_as_char,
uint  numeric_scale,
bool  is_unsigned 
)

Calculate the length of the in-memory representation of the column from information which can be retrieved from dd::Column or Ha_fk_column_type describing it.

This function calculates the amount of memory necessary to store values in the record buffer. It is used in cases when we want to calculate this value from the description of column in the form compatible with dd::Column without constructing full-blown Field object.

Note
The implementation is based on Create_field::init() and Create_field::create_length_to_internal_length().
Parameters
typeColumn DD type.
char_lengthColumn length as stored in DD.
elements_countNumber of elements in column of ENUM/SET type.
treat_bit_as_charIndicates whether this BIT column is represented as char column internally.
numeric_scaleColumn numeric scale as stored in DD.
is_unsignedColumn unsignedness.

◆ calc_pack_length() [2/2]

size_t calc_pack_length ( enum_field_types  type,
size_t  length 
)

◆ copy_integer()

template<bool Is_big_endian>
void copy_integer ( uchar to,
size_t  to_length,
const uchar from,
size_t  from_length,
bool  is_unsigned 
)

Copies an integer value to a format comparable with memcmp().

The format is characterized by the following:

  • The sign bit goes first and is unset for negative values.
  • The representation is big endian.

The function template can be instantiated to copy from little or big endian values.

Template Parameters
Is_big_endianTrue if the source integer is big endian.
Parameters
toWhere to write the integer.
to_lengthSize in bytes of the destination buffer.
fromWhere to read the integer.
from_lengthSize in bytes of the source integer
is_unsignedTrue if the source integer is an unsigned value.

◆ decimal_err_to_type_conv_status()

type_conversion_status decimal_err_to_type_conv_status ( int  dec_error)
inline

◆ field_conv_slow()

type_conversion_status field_conv_slow ( Field to,
const Field from 
)

Copy the value in "from" (assumed to be non-NULL) to "to", doing any required conversions in the process.

Note that you should only call this if fields_are_memcpyable() is false, since it does an actual conversion on the slow path (and it is not properly tested whether it gives the correct result in all cases if fields_are_memcpyable() is true).

You should never call this with to == from, as they are no-ops.

◆ fields_are_memcpyable()

bool fields_are_memcpyable ( const Field to,
const Field from 
)

Check if one can copy from “from” to “to” with a simple memcpy(), with pack_length() as the length.

This is the case if the types of the two fields are the same and we don't have special copying rules for the type (e.g., blobs, which require allocation, or time functions that require checking for special SQL modes).

You should never call this with to == from, as such copies are no-ops and memcpy() has undefined behavior with overlapping memory areas.

◆ generate_create_field()

Create_field * generate_create_field ( THD thd,
Item source_item,
TABLE tmp_table 
)

Generate a Create_field from an Item.

This function generates a Create_field from an Item by first creating a temporary table Field from the Item, and then creating the Create_field from this Field (there is currently no way to go directly from Item to Create_field). It is used several places:

  • In CREATE TABLE AS SELECT for creating the target table definition.
  • In functional indexes for creating the hidden generated column from the indexed expression.
Parameters
thdThread handler
source_itemThe item to generate a Create_field from
tmp_tableA table object which is used to generate a temporary table field, as described above. This doesn't need to be an existing table.
Returns
A Create_field generated from the input item, or nullptr in case of errors.

◆ get_blob_type_from_length()

enum_field_types get_blob_type_from_length ( size_t  length)

◆ get_enum_pack_length()

uint get_enum_pack_length ( int  elements)
inline

◆ get_field_name_or_expression()

const char * get_field_name_or_expression ( THD thd,
const Field field 
)
Returns
the expression if the input field is a hidden generated column that represents a functional key part. If not, return the field name. In case of a functional index; the expression is allocated on the THD's MEM_ROOT.

◆ get_set_pack_length()

uint get_set_pack_length ( int  elements)
inline

◆ is_blob()

bool is_blob ( enum_field_types  sql_type)
inline

◆ is_temporal_real_type()

bool is_temporal_real_type ( enum_field_types  type)
inline

Tests if field real type is temporal, i.e.

represents all existing implementations of DATE, TIME, DATETIME or TIMESTAMP types in SQL.

Parameters
typeField real type, as returned by field->real_type()
Return values
trueIf field real type is temporal
falseIf field real type is not temporal

◆ make_field() [1/4]

Field * make_field ( const Create_field create_field,
TABLE_SHARE share 
)

Instantiates a Field object without a record buffer.

Parameters
create_fieldThe column meta data.
shareThe table share object.

◆ make_field() [2/4]

Field * make_field ( const Create_field create_field,
TABLE_SHARE share,
const char *  field_name,
size_t  field_length,
uchar ptr,
uchar null_pos,
size_t  null_bit 
)

Instantiates a Field object with the given name and record buffer values.

Parameters
create_fieldThe column meta data.
shareThe table share object.
field_nameCreate_field::field_name is overridden with this value when instantiating the Field object.
field_lengthCreate_field::length is overridden with this value when instantiating the Field object.
ptrThe address of the data bytes.
null_posThe address of the null bytes.
null_bitThe position of the column's null bit within the row's null bytes.

◆ make_field() [3/4]

Field * make_field ( const Create_field create_field,
TABLE_SHARE share,
uchar ptr,
uchar null_pos,
size_t  null_bit 
)

Instantiates a Field object with the given record buffer values.

Parameters
create_fieldThe column meta data.
shareThe table share object.
ptrThe start of the record buffer.
null_posThe address of the null bytes.
null_bitThe position of the column's null bit within the row's null bytes.

◆ make_field() [4/4]

Field * make_field ( MEM_ROOT mem_root_arg,
TABLE_SHARE share,
uchar ptr,
size_t  field_length,
uchar null_pos,
uchar  null_bit,
enum_field_types  field_type,
const CHARSET_INFO field_charset,
Field::geometry_type  geom_type,
uchar  auto_flags,
TYPELIB interval,
const char *  field_name,
bool  is_nullable,
bool  is_zerofill,
bool  is_unsigned,
uint  decimals,
bool  treat_bit_as_char,
uint  pack_length_override,
std::optional< gis::srid_t srid,
bool  is_array 
)

This function should only be called from legacy code.

◆ pre_validate_value_generator_expr()

bool pre_validate_value_generator_expr ( Item expression,
const char *  name,
Value_generator_source  source 
)

Perform per item-type checks to determine if the expression is allowed for a generated column, default value expression, a functional index or a check constraint.

Note that validation of the specific function is done later in procedures open_table_from_share and fix_value_generator_fields.

Parameters
expressionthe expression to check for validity
nameused for error reporting
sourceSource of value generator(a generated column, a regular column with generated default value or a check constraint).
Returns
false if ok, true otherwise

◆ real_type_to_type()

enum_field_types real_type_to_type ( enum_field_types  real_type)
inline

Convert temporal real types as returned by field->real_type() to field type as returned by field->type().

Parameters
real_typeReal type.
Return values
Fieldtype.

◆ real_type_with_now_as_default()

bool real_type_with_now_as_default ( enum_field_types  type)
inline

Tests if field real type can have "DEFAULT CURRENT_TIMESTAMP", i.e.

represents TIMESTAMP types in SQL.

Parameters
typeField type, as returned by field->real_type().
Return values
trueIf field real type can have "DEFAULT CURRENT_TIMESTAMP".
falseIf field real type can not have "DEFAULT CURRENT_TIMESTAMP".

◆ real_type_with_now_on_update()

bool real_type_with_now_on_update ( enum_field_types  type)
inline

Tests if field real type can have "ON UPDATE CURRENT_TIMESTAMP", i.e.

represents TIMESTAMP types in SQL.

Parameters
typeField type, as returned by field->real_type().
Return values
trueIf field real type can have "ON UPDATE CURRENT_TIMESTAMP".
falseIf field real type can not have "ON UPDATE CURRENT_TIMESTAMP".

◆ set_field_to_null()

type_conversion_status set_field_to_null ( Field field)

The following piece of code is run for the case when a BLOB column that has value NULL is queried with GROUP BY NULL and the result is inserted into a some table's column declared having primitive type (e.g. INT) and NOT NULL.

For example, the following test case will hit this piece of code: CREATE TABLE t1 (a BLOB); CREATE TABLE t2 (a INT NOT NULL);

INSERT t1 VALUES (NULL); INSERT INTO t2(a) SELECT a FROM t1 GROUP BY NULL; <<== Hit here

In general, when set_field_to_null() is called a Field has to be either declared as NULL-able or be marked as temporary NULL-able. But in case of INSERT SELECT from a BLOB field and when GROUP BY NULL is specified the Field object for a destination column doesn't set neither NULL-able nor temporary NULL-able (see setup_copy_fields()).

◆ set_field_to_null_with_conversions()

type_conversion_status set_field_to_null_with_conversions ( Field field,
bool  no_conversions 
)

Set field to NULL or TIMESTAMP or to next auto_increment number.

Parameters
fieldField to update
no_conversionsSet to 1 if we should return 1 if field can't take null values. If set to 0 we will do store the 'default value' if the field is a special field. If not we will give an error.
Return values
0Field could take 0 or an automatic conversion was used
-1Field could not take NULL and no conversion was used. If no_conversion was not set, an error message is printed

◆ store_internal_with_error_check()

type_conversion_status store_internal_with_error_check ( Field_new_decimal field,
int  conversion_err,
my_decimal value 
)

◆ time_warning_to_type_conversion_status()

type_conversion_status time_warning_to_type_conversion_status ( const int  warn)
inline

Convert warnings returned from str_to_time() and str_to_datetime() to their corresponding type_conversion_status codes.