MySQL  8.0.15
Source Code Documentation
field.h File Reference
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include <string>
#include "binary_log_funcs.h"
#include "binary_log_types.h"
#include "decimal.h"
#include "lex_string.h"
#include "m_ctype.h"
#include "m_string.h"
#include "my_base.h"
#include "my_bitmap.h"
#include "my_byteorder.h"
#include "my_compiler.h"
#include "my_dbug.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 "nullable.h"
#include "sql/dd/types/column.h"
#include "sql/gis/srid.h"
#include "sql/sql_bitmap.h"
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_list.h"
#include "sql/thr_malloc.h"
#include "sql_string.h"

Go to the source code of this file.

Classes

class  Value_generator
 Used for storing information associated with generated column or default values generated from expression. More...
 
class  Proto_field
 
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_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
 Constitutes a mapping from columns of tables in the from clause to aggregated columns. More...
 

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, 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
}
 
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...
 

Functions

type_conversion_status field_conv (Field *to, Field *from)
 Simple quick field convert that is called on insert. 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_integer_type (enum_field_types type)
 Tests if field type is an integer. More...
 
bool is_numeric_type (enum_field_types type)
 Tests if field type is a numeric type. More...
 
bool is_temporal_type (enum_field_types type)
 Tests if field type is temporal, i.e. More...
 
bool is_temporal_real_type (enum_field_types type)
 Tests if field real type is temporal, i.e. More...
 
bool is_temporal_type_with_time (enum_field_types type)
 Tests if field type is temporal and has time part, i.e. More...
 
bool is_temporal_type_with_date (enum_field_types type)
 Tests if field type is temporal and has date part, i.e. More...
 
bool is_temporal_type_with_date_and_time (enum_field_types type)
 Tests if field type is temporal and has date and time parts, 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...
 
bool is_timestamp_type (enum_field_types type)
 Recognizer for concrete data type (called real_type for some reason), returning true if it is one of the TIMESTAMP types. More...
 
enum_field_types real_type_to_type (enum_field_types real_type)
 Convert temporal real types as retuned 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 (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 maybe_null, bool is_zerofill, bool is_unsigned, uint decimals, bool treat_bit_as_char, uint pack_length_override, Nullable< gis::srid_t > srid)
 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 *null_pos)
 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 *item, TABLE *tmp_table)
 Generate a Create_field, based on 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 *column_name, bool is_gen_col)
 

Macro Definition Documentation

◆ ASSERT_COLUMN_MARKED_FOR_READ

#define ASSERT_COLUMN_MARKED_FOR_READ
Value:
DBUG_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:103
#define DBUG_ASSERT(A)
Definition: my_dbug.h:132

◆ ASSERT_COLUMN_MARKED_FOR_WRITE

#define ASSERT_COLUMN_MARKED_FOR_WRITE
Value:
DBUG_ASSERT(!table || (!table->write_set || \
bitmap_is_set(table->write_set, field_index)))
static bool bitmap_is_set(const MY_BITMAP *map, uint bit)
Definition: my_bitmap.h:103
#define DBUG_ASSERT(A)
Definition: my_dbug.h:132

◆ 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
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.

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 ( enum_field_types  type,
size_t  length 
)

◆ calc_pack_length() [2/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.

◆ 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()

type_conversion_status field_conv ( Field to,
Field from 
)

Simple quick field convert that is called on insert.

◆ generate_create_field()

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

Generate a Create_field, based on an Item.

This function will generate a Create_field based on an existing Item. This is used for multiple purposes, including CREATE TABLE AS SELECT and creating hidden generated columns for functional indexes.

Parameters
thdThread handler
itemThe Item to generate a Create_field from
tmp_tableA temporary TABLE object that is used for holding Field objects that are created
Returns
A Create_field allocated on the THDs MEM_ROOT.

Generate a Create_field, based on 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
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_integer_type()

bool is_integer_type ( enum_field_types  type)
inline

Tests if field type is an integer.

Parameters
typeField type, as returned by field->type()
Returns
true if integer type, false otherwise

◆ is_numeric_type()

bool is_numeric_type ( enum_field_types  type)
inline

Tests if field type is a numeric type.

Parameters
typeField type, as returned by field->type()
Returns
true if numeric type, false otherwise

◆ 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

◆ is_temporal_type()

bool is_temporal_type ( enum_field_types  type)
inline

Tests if field type is temporal, i.e.

represents DATE, TIME, DATETIME or TIMESTAMP types in SQL.

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

◆ is_temporal_type_with_date()

bool is_temporal_type_with_date ( enum_field_types  type)
inline

Tests if field type is temporal and has date part, i.e.

represents DATE, DATETIME or TIMESTAMP types in SQL.

Parameters
typeField type, as returned by field->type().
Return values
trueIf field type is temporal type with date part.
falseIf field type is not temporal type with date part.

◆ is_temporal_type_with_date_and_time()

bool is_temporal_type_with_date_and_time ( enum_field_types  type)
inline

Tests if field type is temporal and has date and time parts, i.e.

represents DATETIME or TIMESTAMP types in SQL.

Parameters
typeField type, as returned by field->type().
Return values
trueIf field type is temporal type with date and time parts.
falseIf field type is not temporal type with date and time parts.

◆ is_temporal_type_with_time()

bool is_temporal_type_with_time ( enum_field_types  type)
inline

Tests if field type is temporal and has time part, i.e.

represents TIME, DATETIME or TIMESTAMP types in SQL.

Parameters
typeField type, as returned by field->type().
Return values
trueIf field type is temporal type with time part.
falseIf field type is not temporal type with time part.

◆ is_timestamp_type()

bool is_timestamp_type ( enum_field_types  type)
inline

Recognizer for concrete data type (called real_type for some reason), returning true if it is one of the TIMESTAMP types.

◆ make_field() [1/4]

Field* make_field ( 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  maybe_null,
bool  is_zerofill,
bool  is_unsigned,
uint  decimals,
bool  treat_bit_as_char,
uint  pack_length_override,
Nullable< gis::srid_t srid 
)

This function should only be called from legacy code.

◆ make_field() [2/4]

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

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.
null_posThe address of the 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 ( 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.

◆ pre_validate_value_generator_expr()

bool pre_validate_value_generator_expr ( Item expression,
const char *  column_name,
bool  is_gen_col 
)

◆ real_type_to_type()

enum_field_types real_type_to_type ( enum_field_types  real_type)
inline

Convert temporal real types as retuned 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.