MySQL  8.0.19
Source Code Documentation
sql_executor.cc File Reference

Query execution. More...

#include "sql/sql_executor.h"
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <atomic>
#include <cmath>
#include <cstring>
#include <limits>
#include <map>
#include <memory>
#include <new>
#include <string>
#include <utility>
#include <vector>
#include "field_types.h"
#include "lex_string.h"
#include "m_ctype.h"
#include "map_helpers.h"
#include "memory_debugging.h"
#include "memroot_allocator.h"
#include "my_alloc.h"
#include "my_bitmap.h"
#include "my_byteorder.h"
#include "my_dbug.h"
#include "my_loglevel.h"
#include "my_macros.h"
#include "my_pointer_arithmetic.h"
#include "my_sqlcommand.h"
#include "my_sys.h"
#include "my_table_map.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/psi/psi_base.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "prealloced_array.h"
#include "sql/basic_row_iterators.h"
#include "sql/bka_iterator.h"
#include "sql/composite_iterators.h"
#include "sql/current_thd.h"
#include "sql/debug_sync.h"
#include "sql/enum_query_type.h"
#include "sql/field.h"
#include "sql/filesort.h"
#include "sql/handler.h"
#include "sql/hash_join_iterator.h"
#include "sql/item.h"
#include "sql/item_cmpfunc.h"
#include "sql/item_func.h"
#include "sql/item_sum.h"
#include "sql/json_dom.h"
#include "sql/key.h"
#include "sql/key_spec.h"
#include "sql/mem_root_array.h"
#include "sql/mysqld.h"
#include "sql/nested_join.h"
#include "sql/opt_costmodel.h"
#include "sql/opt_explain_format.h"
#include "sql/opt_range.h"
#include "sql/opt_trace.h"
#include "sql/opt_trace_context.h"
#include "sql/parse_tree_nodes.h"
#include "sql/pfs_batch_mode.h"
#include "sql/protocol.h"
#include "sql/psi_memory_key.h"
#include "sql/query_options.h"
#include "sql/query_result.h"
#include "sql/record_buffer.h"
#include "sql/records.h"
#include "sql/ref_row_iterators.h"
#include "sql/row_iterator.h"
#include "sql/sort_param.h"
#include "sql/sorting_iterator.h"
#include "sql/sql_base.h"
#include "sql/sql_bitmap.h"
#include "sql/sql_class.h"
#include "sql/sql_cmd.h"
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_join_buffer.h"
#include "sql/sql_list.h"
#include "sql/sql_optimizer.h"
#include "sql/sql_select.h"
#include "sql/sql_tmp_table.h"
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql/table_function.h"
#include "sql/temp_table_param.h"
#include "sql/thr_malloc.h"
#include "sql/timing_iterator.h"
#include "sql/window.h"
#include "sql/window_lex.h"
#include "sql_string.h"
#include "tables_contained_in.h"
#include "template_utils.h"
#include "thr_lock.h"

Classes

struct  PendingInvalidator
 Similar to PendingCondition, but for cache invalidator iterators. More...
 

Enumerations

enum  CallingContext { TOP_LEVEL, DIRECTLY_UNDER_SEMIJOIN, DIRECTLY_UNDER_OUTER_JOIN, DIRECTLY_UNDER_WEEDOUT }
 
enum  Substructure { Substructure::NONE, Substructure::OUTER_JOIN, Substructure::SEMIJOIN, Substructure::WEEDOUT }
 

Functions

static void return_zero_rows (JOIN *join, List< Item > &fields)
 For some reason, e.g. More...
 
static int do_select (JOIN *join)
 Make a join of all tables and write it on socket or to table. More...
 
static enum_nested_loop_state evaluate_join_record (JOIN *join, QEP_TAB *const qep_tab)
 Process one row of the nested loop join. More...
 
static enum_nested_loop_state evaluate_null_complemented_join_record (JOIN *join, QEP_TAB *qep_tab)
 
static enum_nested_loop_state end_send (JOIN *join, QEP_TAB *qep_tab, bool end_of_records)
 
static enum_nested_loop_state end_write (JOIN *join, QEP_TAB *qep_tab, bool end_of_records)
 
static enum_nested_loop_state end_write_wf (JOIN *join, QEP_TAB *const qep_tab, bool end_of_records)
 Similar to end_write, but used in the windowing tmp table steps. More...
 
static enum_nested_loop_state end_update (JOIN *join, QEP_TAB *const qep_tab, bool end_of_records)
 Group by searching after group record and updating it if possible. More...
 
static int read_system (TABLE *table)
 Read a constant table when there is at most one matching row, using a table scan. More...
 
static int read_const (TABLE *table, TABLE_REF *ref)
 
static bool remove_dup_with_compare (THD *thd, TABLE *entry, Field **field, ulong offset, Item *having)
 
static bool remove_dup_with_hash_index (THD *thd, TABLE *table, Field **first_field, const size_t *field_lengths, size_t key_length, Item *having)
 Generate a hash index for each row to quickly find duplicate rows. More...
 
static int do_sj_reset (SJ_TMP_TABLE *sj_tbl)
 SemiJoinDuplicateElimination: Reset the temporary table. More...
 
static bool alloc_group_fields (JOIN *join, ORDER *group)
 Get a list of buffers for saveing last group. More...
 
static void SetCostOnTableIterator (const Cost_model_server &cost_model, const POSITION *pos, bool is_after_filter, RowIterator *iterator)
 
static bool having_is_true (Item *h)
 Evaluates HAVING condition. More...
 
string RefToString (const TABLE_REF &ref, const KEY *key, bool include_nulls)
 
bool has_rollup_result (Item *item)
 Checks if an item has a ROLLUP NULL which needs to be written to temp table. More...
 
bool prepare_sum_aggregators (Item_sum **func_ptr, bool need_distinct)
 
bool setup_sum_funcs (THD *thd, Item_sum **func_ptr)
 Call setup() for all sum functions. More...
 
void init_tmptable_sum_functions (Item_sum **func_ptr)
 
void update_tmptable_sum_func (Item_sum **func_ptr, TABLE *tmp_table)
 Update record 0 in tmp_table from record 1. More...
 
void copy_sum_funcs (Item_sum **func_ptr, Item_sum **end_ptr)
 Copy result of sum functions to record in tmp_table. More...
 
bool init_sum_functions (Item_sum **func_ptr, Item_sum **end_ptr)
 
bool update_sum_func (Item_sum **func_ptr)
 
bool copy_funcs (Temp_table_param *param, const THD *thd, Copy_func_type type)
 Copy result of functions to record in tmp_table. More...
 
static enum_nested_loop_state end_sj_materialize (JOIN *join, QEP_TAB *qep_tab, bool end_of_records)
 
static bool update_const_equal_items (THD *thd, Item *cond, JOIN_TAB *tab)
 Check appearance of new constant items in multiple equalities of a condition after reading a constant table. More...
 
void setup_tmptable_write_func (QEP_TAB *tab, Opt_trace_object *trace)
 Setup write_func of QEP_tmp_table object. More...
 
static size_t record_prefix_size (const QEP_TAB *qep_tab)
 Find out how many bytes it takes to store the smallest prefix which covers all the columns that will be read from a table. More...
 
bool set_record_buffer (const QEP_TAB *tab)
 Allocate a data buffer that the storage engine can use for fetching batches of records. More...
 
static void ExtractConditions (Item *condition, vector< Item * > *condition_parts)
 Split AND conditions into their constituent parts, recursively. More...
 
unique_ptr_destroy_only< RowIteratorPossiblyAttachFilterIterator (unique_ptr_destroy_only< RowIterator > iterator, const vector< Item * > &conditions, THD *thd)
 Return a new iterator that wraps "iterator" and that tests all of the given conditions (if any), ANDed together. More...
 
unique_ptr_destroy_only< RowIteratorCreateNestedLoopIterator (THD *thd, unique_ptr_destroy_only< RowIterator > left_iterator, unique_ptr_destroy_only< RowIterator > right_iterator, JoinType join_type, bool pfs_batch_mode)
 
static unique_ptr_destroy_only< RowIteratorCreateInvalidatorIterator (THD *thd, QEP_TAB *qep_tab, unique_ptr_destroy_only< RowIterator > iterator)
 
static unique_ptr_destroy_only< RowIteratorPossiblyAttachFilterIterator (unique_ptr_destroy_only< RowIterator > iterator, const vector< PendingCondition > &conditions, THD *thd)
 
static Item_func_trig_condGetTriggerCondOrNull (Item *item)
 
void ConvertItemsToCopy (List< Item > *items, Field **fields, Temp_table_param *param, JOIN *join)
 For historical reasons, derived table materialization and temporary table materialization didn't specify the fields to materialize in the same way. More...
 
void SplitConditions (Item *condition, vector< Item * > *predicates_below_join, vector< PendingCondition > *predicates_above_join)
 
static void MarkUnhandledDuplicates (SJ_TMP_TABLE *weedout, plan_idx weedout_start, plan_idx weedout_end, qep_tab_map *unhandled_duplicates)
 For a given duplicate weedout operation, figure out which tables are supposed to be deduplicated by it, and add those to unhandled_duplicates. More...
 
static unique_ptr_destroy_only< RowIteratorCreateWeedoutIterator (THD *thd, unique_ptr_destroy_only< RowIterator > iterator, SJ_TMP_TABLE *weedout_table)
 
static unique_ptr_destroy_only< RowIteratorCreateWeedoutIteratorForTables (THD *thd, const qep_tab_map tables_to_deduplicate, QEP_TAB *qep_tabs, uint primary_tables, unique_ptr_destroy_only< RowIterator > iterator)
 
static Substructure FindSubstructure (QEP_TAB *qep_tabs, const plan_idx first_idx, const plan_idx this_idx, const plan_idx last_idx, CallingContext calling_context, bool *add_limit_1, plan_idx *substructure_end, qep_tab_map *unhandled_duplicates)
 Given a range of tables (where we assume that we've already handled first_idx..(this_idx-1) as inner joins), figure out whether this is a semijoin, an outer join or a weedout. More...
 
unique_ptr_destroy_only< RowIteratorGetTableIterator (THD *thd, QEP_TAB *qep_tab, QEP_TAB *qep_tabs)
 Get the RowIterator used for scanning the given table, with any required materialization operations done first. More...
 
void SetCostOnNestedLoopIterator (const Cost_model_server &cost_model, const POSITION *pos_right, RowIterator *iterator)
 
void SetCostOnHashJoinIterator (const Cost_model_server &cost_model, const POSITION *pos_right, RowIterator *iterator)
 
static void ExtractHashJoinConditions (const QEP_TAB *current_table, qep_tab_map left_tables, vector< Item * > *predicates, vector< Item_func_eq * > *hash_join_conditions, vector< Item * > *conditions_after_hash_join)
 
static unique_ptr_destroy_only< RowIteratorConnectJoins (plan_idx first_idx, plan_idx last_idx, QEP_TAB *qep_tabs, THD *thd, CallingContext calling_context, vector< PendingCondition > *pending_conditions, vector< PendingInvalidator > *pending_invalidators, qep_tab_map *unhandled_duplicates)
 For a given slice of the table list, build up the iterator tree corresponding to the tables in that slice. More...
 
static int ExecuteIteratorQuery (JOIN *join)
 
enum_nested_loop_state sub_select_op (JOIN *join, QEP_TAB *qep_tab, bool end_of_records)
 Accumulate full or partial join result in operation and send operation's result further. More...
 
enum_nested_loop_state sub_select (JOIN *join, QEP_TAB *const qep_tab, bool end_of_records)
 Retrieve records ends with a given beginning from the result of a join. More...
 
int do_sj_dups_weedout (THD *thd, SJ_TMP_TABLE *sjtbl)
 SemiJoinDuplicateElimination: Weed out duplicate row combinations. More...
 
int report_handler_error (TABLE *table, int error)
 Help function when we get some an error from the table handler. More...
 
static bool init_index_and_record_buffer (const QEP_TAB *qep_tab, handler *file, uint idx, bool sorted)
 Initialize an index scan and the record buffer to use in the scan. More...
 
int safe_index_read (QEP_TAB *tab)
 
int join_read_const_table (JOIN_TAB *tab, POSITION *pos)
 Reads content of constant table. More...
 
void join_setup_iterator (QEP_TAB *tab)
 Prepare table for reading rows and read first record. More...
 
int join_materialize_table_function (QEP_TAB *tab)
 
int join_materialize_derived (QEP_TAB *tab)
 
int join_materialize_semijoin (QEP_TAB *tab)
 
ulonglong get_exact_record_count (QEP_TAB *qep_tab, uint table_count, int *error)
 Get exact count of rows in all tables. More...
 
enum_nested_loop_state end_send_count (JOIN *join, QEP_TAB *qep_tab)
 
enum_nested_loop_state end_send_group (JOIN *join, QEP_TAB *qep_tab, bool end_of_records)
 
static bool cmp_field_value (Field *field, ptrdiff_t diff)
 
static bool group_rec_cmp (ORDER *group, uchar *rec0, uchar *rec1)
 Compare GROUP BY in from tmp table's record[0] and record[1]. More...
 
static bool table_rec_cmp (TABLE *table)
 Compare GROUP BY in from tmp table's record[0] and record[1]. More...
 
ulonglong unique_hash (const Field *field, ulonglong *hash_val)
 Generate hash for a field. More...
 
static ulonglong unique_hash_group (ORDER *group)
 Generate hash for unique constraint according to group-by list. More...
 
static ulonglong unique_hash_fields (TABLE *table)
 
bool check_unique_constraint (TABLE *table)
 Check unique_constraint. More...
 
static void reset_wf_states (Func_ptr_array *func_ptr, bool framing)
 Minion for reset_framing_wf_states and reset_non_framing_wf_state, q.v. More...
 
static void reset_framing_wf_states (Func_ptr_array *func_ptr)
 Walk the function calls and reset any framing window function's window state. More...
 
static void reset_non_framing_wf_state (Func_ptr_array *func_ptr)
 Walk the function calls and reset any non-framing window function's window state. More...
 
static bool buffer_record_somewhere (THD *thd, Window *w, int64 rowno)
 Save a window frame buffer to frame buffer temporary table. More...
 
bool buffer_windowing_record (THD *thd, Temp_table_param *param, bool *new_partition)
 If we cannot evaluate all window functions for a window on the fly, buffer the current row for later processing by process_buffered_windowing_record. More...
 
static bool read_frame_buffer_row (int64 rowno, Window *w, bool for_nth_value)
 Read row rowno from frame buffer tmp file using cached row positions to minimize positioning work. More...
 
static void dbug_allow_write_all_columns (Temp_table_param *param, std::map< TABLE *, my_bitmap_map * > &map)
 
static void dbug_restore_all_columns (std::map< TABLE *, my_bitmap_map * > &map)
 
bool bring_back_frame_row (THD *thd, Window *w, Temp_table_param *out_param, int64 rowno, Window_retrieve_cached_row_reason reason, int fno)
 Bring back buffered data to the record of qep_tab-1 [1], and optionally execute copy_fields() to the OUT table. More...
 
static bool process_wfs_needing_card (THD *thd, Temp_table_param *param, const Window::st_nth &have_nth_value, const Window::st_lead_lag &have_lead_lag, const int64 current_row, Window *w, Window_retrieve_cached_row_reason current_row_reason)
 Process window functions that need partition cardinality. More...
 
bool process_buffered_windowing_record (THD *thd, Temp_table_param *param, const bool new_partition_or_eof, bool *output_row_ready)
 While there are more unprocessed rows ready to process given the current partition/frame state, process such buffered rows by evaluating/aggregating the window functions defined over this window on the current frame, moving the frame if required. More...
 
static enum_nested_loop_state write_or_send_row (JOIN *join, QEP_TAB *const qep_tab, TABLE *const table, Temp_table_param *const out_tbl)
 The last step in a series of windows do not need to write a tmp file if both a) and b) holds: More...
 
enum_nested_loop_state end_write_group (JOIN *join, QEP_TAB *const qep_tab, bool end_of_records)
 
static bool compare_record (TABLE *table, Field **ptr)
 
static bool copy_blobs (Field **ptr)
 
static void free_blobs (Field **ptr)
 
static size_t compute_field_lengths (Field **first_field, size_t *field_lengths)
 For a set of fields, compute how many bytes their respective sort keys need. More...
 
bool construct_lookup_ref (THD *thd, TABLE *table, TABLE_REF *ref)
 Copy the lookup key into the table ref's key buffer. More...
 
bool make_group_fields (JOIN *main_join, JOIN *curr_join)
 allocate group fields or take prepared (cached). More...
 
int update_item_cache_if_changed (List< Cached_item > &list)
 
bool setup_copy_fields (List< Item > &all_fields, size_t num_select_elements, THD *thd, Temp_table_param *param, Ref_item_array ref_item_array, List< Item > *res_selected_fields, List< Item > *res_all_fields)
 Sets up caches for holding the values of non-aggregated expressions. More...
 
bool copy_fields (Temp_table_param *param, const THD *thd, bool reverse_copy)
 Make a copy of all simple SELECT'ed fields. More...
 
bool copy_fields_and_funcs (Temp_table_param *param, const THD *thd, Copy_func_type type)
 
bool change_to_use_tmp_fields (List< Item > &all_fields, size_t num_select_elements, THD *thd, Ref_item_array ref_item_array, List< Item > *res_selected_fields, List< Item > *res_all_fields)
 Change all funcs and sum_funcs to fields in tmp table, and create new list of all items. More...
 
bool change_refs_to_tmp_fields (List< Item > &all_fields, size_t num_select_elements, THD *thd, Ref_item_array ref_item_array, List< Item > *res_selected_fields, List< Item > *res_all_fields)
 Change all sum_func refs to fields to point at fields in tmp table. More...
 

Variables

static constexpr size_t MAX_RECORD_BUFFER_SIZE = 128 * 1024
 Maximum amount of space (in bytes) to allocate for a Record_buffer. More...
 

Detailed Description

Query execution.