MySQL 8.0.39
Source Code Documentation
field.cc File Reference
#include "sql/field.h"
#include <float.h>
#include <stddef.h>
#include "m_ctype.h"
#include "my_config.h"
#include <sys/time.h>
#include <algorithm>
#include <cmath>
#include <memory>
#include <optional>
#include "decimal.h"
#include "m_string.h"
#include "my_alloc.h"
#include "my_byteorder.h"
#include "my_compare.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_double2ulonglong.h"
#include "my_sqlcommand.h"
#include "myisampack.h"
#include "scope_guard.h"
#include "sql-common/json_binary.h"
#include "sql-common/json_dom.h"
#include "sql/create_field.h"
#include "sql/current_thd.h"
#include "sql/dd/cache/dictionary_client.h"
#include "sql/dd/types/table.h"
#include "sql/dd_table_share.h"
#include "sql/derror.h"
#include "sql/filesort.h"
#include "sql/gis/rtree_support.h"
#include "sql/gis/srid.h"
#include "sql/handler.h"
#include "sql/item.h"
#include "sql/item_json_func.h"
#include "sql/item_timefunc.h"
#include "sql/join_optimizer/bit_utils.h"
#include "sql/json_diff.h"
#include "sql/key.h"
#include "sql/log_event.h"
#include "sql/my_decimal.h"
#include "sql/mysqld.h"
#include "sql/protocol.h"
#include "sql/psi_memory_key.h"
#include "sql/rpl_rli.h"
#include "sql/spatial.h"
#include "sql/sql_class.h"
#include "sql/sql_exception_handler.h"
#include "sql/sql_lex.h"
#include "sql/sql_time.h"
#include "sql/sql_tmp_table.h"
#include "sql/srs_fetcher.h"
#include "sql/stateless_allocator.h"
#include "sql/strfunc.h"
#include "sql/system_variables.h"
#include "sql/time_zone_common.h"
#include "sql/transaction_info.h"
#include "sql/tztime.h"
#include "template_utils.h"
#include "typelib.h"

Classes

struct  Check_field_param
 

Namespaces

namespace  dd
 The version of the current data dictionary table definitions.
 
namespace  anonymous_namespace{field.cc}
 Function to compare two unsigned integers for their relative order.
 

Macros

#define FLAGSTR(V, F)   ((V) & (F) ? #F " " : "")
 
#define MAX_EXPONENT   1024
 
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE   FLOATING_POINT_BUFFER
 
#define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE   128
 
#define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE   128
 
#define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg)    ((ulong)((1LL << std::min(arg, 4U) * 8) - 1LL))
 
#define FIELDTYPE_TEAR_FROM   (MYSQL_TYPE_BIT + 1)
 
#define FIELDTYPE_TEAR_TO   (243 - 1)
 
#define FIELDTYPE_NUM   (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))
 

Functions

bool anonymous_namespace{field.cc}::sql_type_prevents_inplace (const Field &from, const Create_field &to)
 Predicate to determine if a field type change prevents alter from being done inplace. More...
 
bool anonymous_namespace{field.cc}::length_prevents_inplace (const Field &from, const Create_field &to)
 Predicate to determine if a length change prevents alter from being done inplace. More...
 
bool anonymous_namespace{field.cc}::charset_prevents_inplace (const Field_str &from, const Create_field &to)
 Predicate to determine if a charset change prevents alter from being done inplace. More...
 
bool anonymous_namespace{field.cc}::change_prevents_inplace (const Field_str &from, const Create_field &to)
 Predicate to determine if the difference between a Field and the new Create_field prevents alter from being done inplace. More...
 
int field_type2index (enum_field_types field_type)
 
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...
 
static Geometry::wkbType geometry_type_to_wkb_type (Field::geometry_type t)
 Convert Field::geometry_type to the corresponding Geometry::wkbType. More...
 
static bool test_if_important_data (const CHARSET_INFO *cs, const char *str, const char *strend)
 
int anonymous_namespace{field.cc}::compare (unsigned int a, unsigned int b)
 
static void push_numerical_conversion_warning (THD *thd, const char *str, uint length, const CHARSET_INFO *cs, const char *typestr, int error, const char *field_name="UNKNOWN", ulong row_num=0)
 Output a warning for erroneous conversion of strings to numerical values. More...
 
static void set_decimal_warning (THD *thd, Field_new_decimal *field, int dec_error, my_decimal *dec_value, const char *from, size_t length, const CHARSET_INFO *charset_arg)
 Emits a warning for the decimal conversion error. More...
 
static size_t field_well_formed_copy_nchars (const CHARSET_INFO *to_cs, char *to, size_t to_length, const CHARSET_INFO *from_cs, const char *from, size_t from_length, size_t nchars, const char **well_formed_error_pos, const char **cannot_convert_error_pos, const char **from_end_pos)
 Copy string with optional character set conversion. More...
 
static void append_zerofill_and_unsigned (const Field_num *field, String *res)
 Appends the UNSIGNED and ZEROFILL attributes to a String if a Field_num has these attributes. More...
 
static void integer_sql_type (const Field_num *field, const char *type_name, String *res)
 Writes an integer type specification to a string. More...
 
type_conversion_status store_internal_with_error_check (Field_new_decimal *field, int err, my_decimal *value)
 
static int my_datetime_number_to_str (char *pos, longlong tmp)
 Convert a number in format YYMMDDhhmmss to string. More...
 
static type_conversion_status datetime_store_internal (TABLE *table, ulonglong tmp, uchar *ptr)
 Store a DATETIME in a 8-byte integer to record. More...
 
static longlong datetime_get_internal (TABLE *table, uchar *ptr)
 Read a DATETIME from record to a 8-byte integer. More...
 
static void store_blob_length (uchar *i_ptr, uint i_packlength, uint32 i_number)
 
static longlong enum_val_int (const uchar *ptr, uint packlength, bool low_byte_first)
 
static bool compare_type_names (const CHARSET_INFO *charset, TYPELIB *t1, TYPELIB *t2)
 Compare the first t1::count type names. 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...
 
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...
 
Fieldmake_field (MEM_ROOT *mem_root, 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...
 
static void handle_int16 (uchar *to, const uchar *from, size_t max_length, bool low_byte_first_from, bool low_byte_first_to)
 
static void handle_int24 (uchar *to, const uchar *from, size_t max_length, bool low_byte_first_from, bool low_byte_first_to)
 
static void handle_int32 (uchar *to, const uchar *from, size_t max_length, bool low_byte_first_from, bool low_byte_first_to)
 
static void handle_int64 (uchar *to, const uchar *from, size_t max_length, bool low_byte_first_from, bool low_byte_first_to)
 
Create_fieldgenerate_create_field (THD *thd, Item *source_item, TABLE *tmp_table)
 Generate a Create_field from an Item. More...
 
const char * get_field_name_or_expression (THD *thd, const Field *field)
 

Variables

const char field_separator = ','
 Static variables. More...
 
static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]
 
static Item_result field_types_result_type [FIELDTYPE_NUM]
 

Macro Definition Documentation

◆ BLOB_PACK_LENGTH_TO_MAX_LENGH

#define BLOB_PACK_LENGTH_TO_MAX_LENGH (   arg)     ((ulong)((1LL << std::min(arg, 4U) * 8) - 1LL))

◆ DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE

#define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE   128

◆ DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE

#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE   FLOATING_POINT_BUFFER

◆ FIELDTYPE_NUM

#define FIELDTYPE_NUM   (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))

◆ FIELDTYPE_TEAR_FROM

#define FIELDTYPE_TEAR_FROM   (MYSQL_TYPE_BIT + 1)

◆ FIELDTYPE_TEAR_TO

#define FIELDTYPE_TEAR_TO   (243 - 1)

◆ FLAGSTR

#define FLAGSTR (   V,
 
)    ((V) & (F) ? #F " " : "")

◆ LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE

#define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE   128

◆ MAX_EXPONENT

#define MAX_EXPONENT   1024

Function Documentation

◆ append_zerofill_and_unsigned()

static void append_zerofill_and_unsigned ( const Field_num field,
String res 
)
static

Appends the UNSIGNED and ZEROFILL attributes to a String if a Field_num has these attributes.

Parameters
fieldthe field with the attributes to append
[in,out]resthe String to append to

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

◆ compare_type_names()

static bool compare_type_names ( const CHARSET_INFO charset,
TYPELIB t1,
TYPELIB t2 
)
static

Compare the first t1::count type names.

Returns
true if the type names of t1 match those of t2. false otherwise.

◆ datetime_get_internal()

static longlong datetime_get_internal ( TABLE table,
uchar ptr 
)
inlinestatic

Read a DATETIME from record to a 8-byte integer.

Parameters
tableTable
ptrWhere to read from
Return values
Aninteger in format YYYYMMDDhhmmss

◆ datetime_store_internal()

static type_conversion_status datetime_store_internal ( TABLE table,
ulonglong  tmp,
uchar ptr 
)
inlinestatic

Store a DATETIME in a 8-byte integer to record.

Parameters
tableTable
tmpThe number, in YYYYMMDDhhmmss format
ptrWhere to store to

◆ enum_val_int()

static longlong enum_val_int ( const uchar ptr,
uint  packlength,
bool  low_byte_first 
)
static

◆ field_type2index()

int field_type2index ( enum_field_types  field_type)
inline

◆ field_well_formed_copy_nchars()

static size_t field_well_formed_copy_nchars ( const CHARSET_INFO to_cs,
char *  to,
size_t  to_length,
const CHARSET_INFO from_cs,
const char *  from,
size_t  from_length,
size_t  nchars,
const char **  well_formed_error_pos,
const char **  cannot_convert_error_pos,
const char **  from_end_pos 
)
static

Copy string with optional character set conversion.

This calls helper function well_formed_copy_nchars to copy string with optional character set conversion. Specially, it checks if the ASCII code point exceeds code range. If YES, it allows the input but raises a warning.

Parameters
to_csCharacter set of "to" string
toStore result here
to_lengthMaximum length of "to" string
from_csFrom character set
fromCopy from here
from_lengthLength of from string
ncharsCopy not more that nchars characters
well_formed_error_posReturn position when "from" is not well formed or NULL otherwise.
cannot_convert_error_posReturn position where a not convertible character met, or NULL otherwise.
from_end_posReturn position where scanning of "from" string stopped.
Return values
lengthof bytes copied to 'to'

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

◆ geometry_type_to_wkb_type()

static Geometry::wkbType geometry_type_to_wkb_type ( Field::geometry_type  t)
static

Convert Field::geometry_type to the corresponding Geometry::wkbType.

Parameters
tThe geometry_type to convert
Returns
The corresponding Geometry::wkbType, or Geometry::wkb_invalid_type if there's not suitable type.

◆ get_blob_type_from_length()

enum_field_types get_blob_type_from_length ( size_t  length)

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

◆ handle_int16()

static void handle_int16 ( uchar to,
const uchar from,
size_t  max_length,
bool  low_byte_first_from,
bool  low_byte_first_to 
)
inlinestatic

◆ handle_int24()

static void handle_int24 ( uchar to,
const uchar from,
size_t  max_length,
bool  low_byte_first_from,
bool  low_byte_first_to 
)
inlinestatic

◆ handle_int32()

static void handle_int32 ( uchar to,
const uchar from,
size_t  max_length,
bool  low_byte_first_from,
bool  low_byte_first_to 
)
inlinestatic

◆ handle_int64()

static void handle_int64 ( uchar to,
const uchar from,
size_t  max_length,
bool  low_byte_first_from,
bool  low_byte_first_to 
)
inlinestatic

◆ integer_sql_type()

static void integer_sql_type ( const Field_num field,
const char *  type_name,
String res 
)
static

Writes an integer type specification to a string.

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

◆ my_datetime_number_to_str()

static int my_datetime_number_to_str ( char *  pos,
longlong  tmp 
)
inlinestatic

Convert a number in format YYMMDDhhmmss to string.

Straight coded to avoid problem with slow longlong arithmetic and sprintf.

Parameters
[out]pospointer to convert to.
tmpnumber with datetime value.

◆ 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

◆ push_numerical_conversion_warning()

static void push_numerical_conversion_warning ( THD thd,
const char *  str,
uint  length,
const CHARSET_INFO cs,
const char *  typestr,
int  error,
const char *  field_name = "UNKNOWN",
ulong  row_num = 0 
)
static

Output a warning for erroneous conversion of strings to numerical values.

For use with ER_TRUNCATED_WRONG_VALUE[_FOR_FIELD]

Parameters
thdTHD object
strpointer to string that failed to be converted
lengthlength of string
cscharset for string
typestrstring describing type converted to
errorerror value to output
field_name(for *_FOR_FIELD) name of field
row_num(for *_FOR_FIELD) row number

◆ set_decimal_warning()

static void set_decimal_warning ( THD thd,
Field_new_decimal field,
int  dec_error,
my_decimal dec_value,
const char *  from,
size_t  length,
const CHARSET_INFO charset_arg 
)
static

Emits a warning for the decimal conversion error.

May modify dec_value if there was conversion overflow or bad number.

Parameters
thdThread handler
fieldField to operate on
dec_errordecimal library return code (E_DEC_* see include/decimal.h)
[in,out]dec_valueDecimal value returned by conversion function.
fromValue converted from
lengthLength of 'from'
charset_argCharset of 'from'

◆ store_blob_length()

static void store_blob_length ( uchar i_ptr,
uint  i_packlength,
uint32  i_number 
)
static

◆ store_internal_with_error_check()

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

◆ test_if_important_data()

static bool test_if_important_data ( const CHARSET_INFO cs,
const char *  str,
const char *  strend 
)
static

Variable Documentation

◆ field_separator

const char field_separator = ','

Static variables.

◆ field_types_merge_rules

enum_field_types field_types_merge_rules[FIELDTYPE_NUM][FIELDTYPE_NUM]
static

◆ field_types_result_type

Item_result field_types_result_type[FIELDTYPE_NUM]
static