MySQL  8.0.27
Source Code Documentation
sql_insert.cc File Reference
#include "sql/sql_insert.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <atomic>
#include <map>
#include <utility>
#include "lex_string.h"
#include "m_ctype.h"
#include "m_string.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_bitmap.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_sys.h"
#include "my_table_map.h"
#include "my_thread_local.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/psi/mysql_table.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/udf_registration_types.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "prealloced_array.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/auth_common.h"
#include "sql/binlog.h"
#include "sql/create_field.h"
#include "sql/dd/cache/dictionary_client.h"
#include "sql/dd/dd.h"
#include "sql/dd/dictionary.h"
#include "sql/dd/types/column.h"
#include "sql/dd/types/table.h"
#include "sql/dd_sql_view.h"
#include "sql/debug_sync.h"
#include "sql/derror.h"
#include "sql/discrete_interval.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/item.h"
#include "sql/key.h"
#include "sql/lock.h"
#include "sql/mdl.h"
#include "sql/mysqld.h"
#include "sql/nested_join.h"
#include "sql/opt_explain.h"
#include "sql/opt_explain_format.h"
#include "sql/partition_info.h"
#include "sql/protocol.h"
#include "sql/query_options.h"
#include "sql/rpl_replica.h"
#include "sql/rpl_rli.h"
#include "sql/sql_alter.h"
#include "sql/sql_array.h"
#include "sql/sql_base.h"
#include "sql/sql_class.h"
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_resolver.h"
#include "sql/sql_select.h"
#include "sql/sql_show.h"
#include "sql/sql_table.h"
#include "sql/sql_update.h"
#include "sql/sql_view.h"
#include "sql/system_variables.h"
#include "sql/table_trigger_dispatcher.h"
#include "sql/thd_raii.h"
#include "sql/thr_malloc.h"
#include "sql/transaction.h"
#include "sql/transaction_info.h"
#include "sql/trigger_def.h"
#include "sql_string.h"
#include "template_utils.h"
#include "thr_lock.h"

Namespaces

 dd
 The version of the current data dictionary table definitions.
 

Functions

static bool check_view_insertability (THD *thd, TABLE_LIST *view, const TABLE_LIST *insert_table_ref)
 Additional check for insertability for VIEW. More...
 
static void prepare_for_positional_update (TABLE *table, TABLE_LIST *tables)
 Get extra info for tables we insert into. More...
 
static bool check_single_table_insert (const mem_root_deque< Item * > &fields, TABLE_LIST *view, TABLE_LIST **insert_table_ref)
 Check that insert fields are from a single table of a multi-table view. More...
 
static bool check_insert_fields (THD *thd, TABLE_LIST *table_list, mem_root_deque< Item * > *fields)
 Check insert fields. More...
 
static bool check_valid_table_refs (const TABLE_LIST *view, const mem_root_deque< Item * > &values, table_map map)
 Check that table references are restricted to the supplied table map. More...
 
bool validate_default_values_of_unset_fields (THD *thd, TABLE *table)
 Validates default value of fields which are not specified in the column list of INSERT statement. More...
 
void prepare_triggers_for_insert_stmt (THD *thd, TABLE *table)
 Prepare triggers for INSERT-like statement. More...
 
static bool mysql_prepare_blob_values (THD *thd, const mem_root_deque< Item * > &fields, MEM_ROOT *mem_root)
 Setup data for field BLOB/GEOMETRY field types for execution of "INSERT...UPDATE" statement. More...
 
static bool fix_join_cond_for_insert (THD *thd, TABLE_LIST *tr)
 Recursive helper function for resolving join conditions for insertion into view for prepared statements. More...
 
static bool allocate_column_bitmap (THD *thd, TABLE *table, MY_BITMAP **bitmap)
 
static bool last_uniq_key (TABLE *table, uint keynr)
 Check if there are more unique keys after the current one. More...
 
bool write_record (THD *thd, TABLE *table, COPY_INFO *info, COPY_INFO *update)
 Write a record to table with optional deletion of conflicting records, invoke proper triggers if needed. More...
 
bool check_that_all_fields_are_given_values (THD *thd, TABLE *entry, TABLE_LIST *table_list)
 Check that all fields with aren't null_fields are used. More...
 
static TABLEcreate_table_from_items (THD *thd, HA_CREATE_INFO *create_info, TABLE_LIST *create_table, Alter_info *alter_info, const mem_root_deque< Item * > &items, handlerton **post_ddl_ht)
 Create table from lists of fields and items (or just return TABLE object for pre-opened existing table). More...
 

Function Documentation

◆ allocate_column_bitmap()

static bool allocate_column_bitmap ( THD thd,
TABLE table,
MY_BITMAP **  bitmap 
)
static

◆ check_insert_fields()

static bool check_insert_fields ( THD thd,
TABLE_LIST table_list,
mem_root_deque< Item * > *  fields 
)
static

Check insert fields.

Parameters
thdThe current thread.
table_listThe table for insert.
fieldsThe insert fields.
Returns
false if success, true if error

Resolved reference to base table is returned in lex->insert_table_leaf.

◆ check_single_table_insert()

static bool check_single_table_insert ( const mem_root_deque< Item * > &  fields,
TABLE_LIST view,
TABLE_LIST **  insert_table_ref 
)
static

Check that insert fields are from a single table of a multi-table view.

Parameters
fieldsThe insert fields to be checked.
viewThe view for insert.
[out]insert_table_refReference to table to insert into

This function is called to check that the fields being inserted into are from a single base table. This must be checked when the table to be inserted into is a multi-table view.

Returns
false if success, true if an error was raised.

◆ check_that_all_fields_are_given_values()

bool check_that_all_fields_are_given_values ( THD thd,
TABLE entry,
TABLE_LIST table_list 
)

Check that all fields with aren't null_fields are used.

Parameters
thdthread handler
entrytable that's checked
table_listtop-level table or view, used for generating error or warning message
Return values
trueif all fields are given values

◆ check_valid_table_refs()

static bool check_valid_table_refs ( const TABLE_LIST view,
const mem_root_deque< Item * > &  values,
table_map  map 
)
static

Check that table references are restricted to the supplied table map.

The check can be skipped if the supplied table is a base table.

Parameters
viewTable being specified
valuesValues whose used tables are to be matched against table map
mapTable map to match against
Returns
false if success, true if error

◆ check_view_insertability()

static bool check_view_insertability ( THD thd,
TABLE_LIST view,
const TABLE_LIST insert_table_ref 
)
static

Additional check for insertability for VIEW.

A view is insertable if the following conditions are true:

  • All columns being inserted into are from a single table.
  • All not used columns in table have default values.
  • All columns in view are distinct (not referring to the same column).
  • All columns in view are insertable-into.
Parameters
thdthread handler
[in,out]viewreference to view being inserted into. view->contain_auto_increment is true if and only if the view contains an auto_increment field.
insert_table_refreference to underlying table being inserted into
Return values
falseif success
trueif table is not insertable-into (no error is reported)

◆ create_table_from_items()

static TABLE* create_table_from_items ( THD thd,
HA_CREATE_INFO create_info,
TABLE_LIST create_table,
Alter_info alter_info,
const mem_root_deque< Item * > &  items,
handlerton **  post_ddl_ht 
)
static

Create table from lists of fields and items (or just return TABLE object for pre-opened existing table).

Used by CREATE SELECT.

Let "source table" be the table in the SELECT part.

Let "source table columns" be the set of columns in the SELECT list.

An interesting peculiarity in the syntax CREATE TABLE (<columns>) SELECT is that function defaults are stripped from the the source table columns, but not from the additional columns defined in the CREATE TABLE part. The first TIMESTAMP column there is also subject to promotion to TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, as usual.

Parameters
[in]thdThread object
[in]create_infoCreate information (like MAX_ROWS, ENGINE or temporary table flag)
[in]create_tablePointer to TABLE_LIST object providing database and name for table to be created or to be open
[in,out]alter_infoInitial list of columns and indexes for the table to be created
[in]itemsThe source table columns. Corresponding column definitions (Create_field's) will be added to the end of alter_info->create_list.
[out]post_ddl_htSet to handlerton for table's SE, if this SE supports atomic DDL, so caller can call SE post DDL hook after committing transaction.
Note
This function assumes that either table exists and was pre-opened and locked at open_and_lock_tables() stage (and in this case we just emit error or warning and return pre-opened TABLE object) or an exclusive metadata lock was acquired on table so we can safely create, open and lock table in it (we don't acquire metadata lock if this create is for temporary table).
Since this function contains some logic specific to CREATE TABLE ... SELECT it should be changed before it can be used in other contexts.
Return values
non-zeroPointer to TABLE object for table created or opened
0Error

◆ fix_join_cond_for_insert()

static bool fix_join_cond_for_insert ( THD thd,
TABLE_LIST tr 
)
static

Recursive helper function for resolving join conditions for insertion into view for prepared statements.

Parameters
thdThread handler
trTable structure which is traversed recursively
Returns
false if success, true if error

◆ last_uniq_key()

static bool last_uniq_key ( TABLE table,
uint  keynr 
)
static

Check if there are more unique keys after the current one.

Parameters
tabletable that keys are checked for
keynrcurrent key number
Returns
true if there are unique keys after the specified one

◆ mysql_prepare_blob_values()

static bool mysql_prepare_blob_values ( THD thd,
const mem_root_deque< Item * > &  fields,
MEM_ROOT mem_root 
)
static

Setup data for field BLOB/GEOMETRY field types for execution of "INSERT...UPDATE" statement.

For a expression in 'UPDATE' clause like "a= VALUES(a)", let as call Field* referring 'a' as LHS_FIELD and Field* referring field 'a' in "VALUES(a)" as RHS_FIELD

This function creates a separate copy of the blob value for RHS_FIELD, if the field is updated as well as accessed through VALUES() function in 'UPDATE' clause of "INSERT...UPDATE" statement.

Parameters
[in]thdPointer to THD object.
[in]fieldsList of fields representing LHS_FIELD of all expressions in 'UPDATE' clause.
[in]mem_rootMEM_ROOT for blob copy.
Returns
- Can fail only when we are out of memory.
Return values
falseSuccess
trueFailure

◆ prepare_for_positional_update()

static void prepare_for_positional_update ( TABLE table,
TABLE_LIST tables 
)
static

Get extra info for tables we insert into.

Parameters
tabletable(TABLE object) we insert into, might be NULL in case of view
tables(TABLE_LIST object) or view we insert into

◆ prepare_triggers_for_insert_stmt()

void prepare_triggers_for_insert_stmt ( THD thd,
TABLE table 
)

Prepare triggers for INSERT-like statement.

Parameters
thdThread handler
tableTable to which insert will happen
Note
Prepare triggers for INSERT-like statement by marking fields used by triggers and inform handlers that batching of UPDATE/DELETE cannot be done if there are BEFORE UPDATE/DELETE triggers.

◆ validate_default_values_of_unset_fields()

bool validate_default_values_of_unset_fields ( THD thd,
TABLE table 
)

Validates default value of fields which are not specified in the column list of INSERT statement.

Note
table->record[0] should be be populated with default values before calling this function.
Parameters
thdthread context
tabletable to which values are inserted.
Returns
false if success, true if error

◆ write_record()

bool write_record ( THD thd,
TABLE table,
COPY_INFO info,
COPY_INFO update 
)

Write a record to table with optional deletion of conflicting records, invoke proper triggers if needed.

Parameters
thdthread context
tabletable to which record should be written
infoCOPY_INFO structure describing handling of duplicates and which is used for counting number of records inserted and deleted.
updateCOPY_INFO structure describing the UPDATE part (only used for INSERT ON DUPLICATE KEY UPDATE)

Once this record is written to the table buffer, any AFTER INSERT trigger will be invoked. If instead of inserting a new record we end up updating an old one, both ON UPDATE triggers will fire instead. Similarly both ON DELETE triggers will be invoked if are to delete conflicting records.

Call thd->transaction.stmt.mark_modified_non_trans_table() if table is a non-transactional table.

Note
In ON DUPLICATE KEY UPDATE case this call may set TABLE::autoinc_field_has_explicit_non_null_value flag to true (even in case of failure) so its caller should make sure that it is reset appropriately (
See also
fill_record()).
Returns
false if success, true if error

< Flag for fatal errors