MySQL  8.0.20
Source Code Documentation File Reference

Query execution. More...

#include "sql/sql_executor.h"
#include <assert.h>
#include <inttypes.h>
#include <stdint.h>
#include <algorithm>
#include <atomic>
#include <cmath>
#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 "mem_root_allocator.h"
#include "mem_root_deque.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_bitmap.h"
#include "my_byteorder.h"
#include "my_dbug.h"
#include "my_loglevel.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_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/query_options.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_const.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/temp_table_param.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"


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


enum  Substructure { Substructure::NONE, Substructure::OUTER_JOIN, Substructure::SEMIJOIN, Substructure::WEEDOUT }


static void join_setup_iterator (QEP_TAB *tab)
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 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 pair< uchar *, key_part_mapFindKeyBufferAndMap (const TABLE_REF *ref)
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 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, table_map *conditions_depend_on_outer_tables)
 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 table_map ConvertQepTabMapToTableMap (JOIN *join, qep_tab_map tables)
unique_ptr_destroy_only< RowIteratorCreateBKAIterator (THD *thd, JOIN *join, unique_ptr_destroy_only< RowIterator > iterator, qep_tab_map left_tables, unique_ptr_destroy_only< RowIterator > subtree_iterator, qep_tab_map right_tables, TABLE *table, TABLE_LIST *table_list, TABLE_REF *ref, MultiRangeRowIterator *mrr_iterator, JoinType join_type)
static unique_ptr_destroy_only< RowIteratorPossiblyAttachFilterIterator (unique_ptr_destroy_only< RowIterator > iterator, const vector< PendingCondition > &conditions, THD *thd, table_map *conditions_depend_on_outer_tables)
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...
static bool IsJoinCondition (const Item *item, const QEP_TAB *qep_tab)
static ItemGetInnermostCondition (Item *item)
void SplitConditions (Item *condition, QEP_TAB *current_table, vector< Item *> *predicates_below_join, vector< PendingCondition > *predicates_above_join, vector< PendingCondition > *join_conditions)
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< RowIteratorGetIteratorForDerivedTable (THD *thd, QEP_TAB *qep_tab)
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 bool ConditionIsAlwaysTrue (Item *item)
static bool ItemRefersToOneSideOnly (Item *item, table_map left_side, table_map right_side)
static unique_ptr_destroy_only< RowIteratorCreateHashJoinIterator (THD *thd, QEP_TAB *qep_tab, unique_ptr_destroy_only< RowIterator > build_iterator, qep_tab_map build_tables, unique_ptr_destroy_only< RowIterator > probe_iterator, qep_tab_map probe_tables, JoinType join_type, vector< Item *> *join_conditions, table_map *conditions_depend_on_outer_tables)
static void ExtractJoinConditions (const QEP_TAB *current_table, vector< Item *> *predicates, vector< Item *> *join_conditions)
static bool SubtreeHasIncompletePushedJoin (JOIN *join, qep_tab_map subtree)
static bool PushedJoinRejectsHashJoin (JOIN *join, qep_tab_map left_subtree, qep_tab_map right_subtree, JoinType join_type)
static bool UseHashJoin (QEP_TAB *qep_tab)
static bool UseBKA (QEP_TAB *qep_tab)
static bool QueryMixesOuterBKAAndBNL (JOIN *join)
static bool InsideOuterOrAntiJoin (QEP_TAB *qep_tab)
template<class T >
void PickOutConditionsForTableIndex (int table_idx, vector< T > *from, vector< T > *to)
void PickOutConditionsForTableIndex (int table_idx, vector< PendingCondition > *from, vector< Item *> *to)
unique_ptr_destroy_only< RowIteratorFinishPendingOperations (THD *thd, unique_ptr_destroy_only< RowIterator > iterator, QEP_TAB *remove_duplicates_loose_scan_qep_tab, const vector< PendingCondition > &pending_conditions, const vector< PendingInvalidator > &pending_invalidators, table_map *conditions_depend_on_outer_tables)
static unique_ptr_destroy_only< RowIteratorConnectJoins (plan_idx upper_first_idx, 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, vector< PendingCondition > *pending_join_conditions, qep_tab_map *unhandled_duplicates, table_map *conditions_depend_on_outer_tables)
 For a given slice of the table list, build up the iterator tree corresponding to the tables in that slice. 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...
ulonglong get_exact_record_count (QEP_TAB *qep_tab, uint table_count, int *error)
 Get exact count of rows in all tables. More...
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...
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...


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.