MySQL 8.4.3
Source Code Documentation
field_conv.cc File Reference

Functions to copy data to or from fields. More...

#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include <optional>
#include "field_types.h"
#include "my_byteorder.h"
#include "my_compare.h"
#include "my_compiler.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "my_time.h"
#include "mysql/strings/m_ctype.h"
#include "mysql/udf_registration_types.h"
#include "mysql_com.h"
#include "mysql_time.h"
#include "mysqld_error.h"
#include "sql-common/my_decimal.h"
#include "sql/current_thd.h"
#include "sql/field.h"
#include "sql/item_timefunc.h"
#include "sql/sql_class.h"
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_time.h"
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql_string.h"
#include "template_utils.h"

Functions

static bool is_subtype_of (Field::geometry_type sub, Field::geometry_type super)
 Check if geometry type sub is a subtype of super. More...
 
static void do_field_eq (Copy_field *, const Field *from_field, Field *to_field)
 
static void set_to_is_null (Field *to_field, bool is_null)
 
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...
 
static void do_skip (Copy_field *, const Field *, Field *)
 
static void do_copy_null (Copy_field *copy, const Field *from_field, Field *to_field)
 
static void do_copy_not_null (Copy_field *copy, const Field *from_field, Field *to_field)
 
static void do_copy_maybe_null (Copy_field *copy, const Field *from_field, Field *to_field)
 
static void do_copy_timestamp (Copy_field *copy, const Field *from_field, Field *to_field)
 
static void do_copy_next_number (Copy_field *copy, const Field *from_field, Field *to_field)
 
static void do_copy_blob (Copy_field *, const Field *from_field, Field *to_field)
 
static void do_conv_blob (Copy_field *copy, const Field *from_field, Field *to_field)
 
static void do_field_string (Copy_field *, const Field *from_field, Field *to_field)
 
static void do_field_enum (Copy_field *copy, const Field *from_field, Field *to_field)
 
static void do_field_varbinary_pre50 (Copy_field *copy, const Field *from_field, Field *to_field)
 
static void do_field_int (Copy_field *, const Field *from_field, Field *to_field)
 
static void do_field_real (Copy_field *, const Field *from_field, Field *to_field)
 
static void do_field_decimal (Copy_field *, const Field *from_field, Field *to_field)
 
type_conversion_status copy_time_to_time (const Field *from, Field *to)
 
static void do_field_time (Copy_field *, const Field *from_field, Field *to_field)
 Convert between fields using time representation. More...
 
static void do_cut_string (Copy_field *, const Field *from_field, Field *to_field)
 string copy for single byte characters set when to string is shorter than from string. More...
 
static void do_cut_string_complex (Copy_field *, const Field *from_field, Field *to_field)
 string copy for multi byte characters set when to string is shorter than from string. More...
 
static void do_expand_binary (Copy_field *, const Field *from_field, Field *to_field)
 
static void do_expand_string (Copy_field *, const Field *from_field, Field *to_field)
 
static void copy_field_varstring (Field_varstring *const to, const Field_varstring *const from)
 A variable length string field consists of: (a) 1 or 2 length bytes, depending on the VARCHAR column definition (b) as many relevant character bytes, as defined in the length byte(s) (c) unused padding up to the full length of the column. More...
 
static void do_varstring (Copy_field *, const Field *from_field, Field *to_field)
 
static bool is_blob_type (enum_field_types to_type)
 
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...
 

Detailed Description

Functions to copy data to or from fields.

Function Documentation

◆ copy_field_varstring()

static void copy_field_varstring ( Field_varstring *const  to,
const Field_varstring *const  from 
)
static

A variable length string field consists of: (a) 1 or 2 length bytes, depending on the VARCHAR column definition (b) as many relevant character bytes, as defined in the length byte(s) (c) unused padding up to the full length of the column.

This function only copies (a) and (b)

Condition for using this function: to and from must use the same number of bytes for length, i.e: to->length_bytes==from->length_bytes

Parameters
toVariable length field we're copying to
fromVariable length field we're copying from

◆ copy_time_to_time()

type_conversion_status copy_time_to_time ( const Field from,
Field to 
)
inline

◆ do_conv_blob()

static void do_conv_blob ( Copy_field copy,
const Field from_field,
Field to_field 
)
static

◆ do_copy_blob()

static void do_copy_blob ( Copy_field ,
const Field from_field,
Field to_field 
)
static

◆ do_copy_maybe_null()

static void do_copy_maybe_null ( Copy_field copy,
const Field from_field,
Field to_field 
)
static

◆ do_copy_next_number()

static void do_copy_next_number ( Copy_field copy,
const Field from_field,
Field to_field 
)
static

◆ do_copy_not_null()

static void do_copy_not_null ( Copy_field copy,
const Field from_field,
Field to_field 
)
static

◆ do_copy_null()

static void do_copy_null ( Copy_field copy,
const Field from_field,
Field to_field 
)
static

◆ do_copy_timestamp()

static void do_copy_timestamp ( Copy_field copy,
const Field from_field,
Field to_field 
)
static

◆ do_cut_string()

static void do_cut_string ( Copy_field ,
const Field from_field,
Field to_field 
)
static

string copy for single byte characters set when to string is shorter than from string.

◆ do_cut_string_complex()

static void do_cut_string_complex ( Copy_field ,
const Field from_field,
Field to_field 
)
static

string copy for multi byte characters set when to string is shorter than from string.

◆ do_expand_binary()

static void do_expand_binary ( Copy_field ,
const Field from_field,
Field to_field 
)
static

◆ do_expand_string()

static void do_expand_string ( Copy_field ,
const Field from_field,
Field to_field 
)
static

◆ do_field_decimal()

static void do_field_decimal ( Copy_field ,
const Field from_field,
Field to_field 
)
static

◆ do_field_enum()

static void do_field_enum ( Copy_field copy,
const Field from_field,
Field to_field 
)
static

◆ do_field_eq()

static void do_field_eq ( Copy_field ,
const Field from_field,
Field to_field 
)
static

◆ do_field_int()

static void do_field_int ( Copy_field ,
const Field from_field,
Field to_field 
)
static

◆ do_field_real()

static void do_field_real ( Copy_field ,
const Field from_field,
Field to_field 
)
static

◆ do_field_string()

static void do_field_string ( Copy_field ,
const Field from_field,
Field to_field 
)
static

◆ do_field_time()

static void do_field_time ( Copy_field ,
const Field from_field,
Field to_field 
)
static

Convert between fields using time representation.

◆ do_field_varbinary_pre50()

static void do_field_varbinary_pre50 ( Copy_field copy,
const Field from_field,
Field to_field 
)
static

◆ do_skip()

static void do_skip ( Copy_field ,
const Field ,
Field  
)
static

◆ do_varstring()

static void do_varstring ( Copy_field ,
const Field from_field,
Field to_field 
)
static

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

◆ is_blob_type()

static bool is_blob_type ( enum_field_types  to_type)
inlinestatic

◆ is_subtype_of()

static bool is_subtype_of ( Field::geometry_type  sub,
Field::geometry_type  super 
)
inlinestatic

Check if geometry type sub is a subtype of super.

Parameters
subThe type to check
superThe supertype
Returns
True if sub is a subtype of super

◆ 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

◆ set_to_is_null()

static void set_to_is_null ( Field to_field,
bool  is_null 
)
static