MySQL 8.4.3
Source Code Documentation
sql_union.cc File Reference
#include "sql/sql_union.h"
#include <sys/types.h>
#include <algorithm>
#include <atomic>
#include <cassert>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "mem_root_deque.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_dbug.h"
#include "my_sqlcommand.h"
#include "my_sys.h"
#include "mysql/udf_registration_types.h"
#include "mysqld_error.h"
#include "prealloced_array.h"
#include "scope_guard.h"
#include "sql/auth/auth_acls.h"
#include "sql/current_thd.h"
#include "sql/debug_sync.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/item.h"
#include "sql/item_subselect.h"
#include "sql/iterators/row_iterator.h"
#include "sql/join_optimizer/access_path.h"
#include "sql/join_optimizer/bit_utils.h"
#include "sql/join_optimizer/explain_access_path.h"
#include "sql/join_optimizer/join_optimizer.h"
#include "sql/join_optimizer/materialize_path_parameters.h"
#include "sql/mem_root_array.h"
#include "sql/mysqld.h"
#include "sql/opt_explain.h"
#include "sql/opt_explain_format.h"
#include "sql/opt_trace.h"
#include "sql/parse_tree_node_base.h"
#include "sql/parse_tree_nodes.h"
#include "sql/parser_yystype.h"
#include "sql/pfs_batch_mode.h"
#include "sql/protocol.h"
#include "sql/query_options.h"
#include "sql/sql_base.h"
#include "sql/sql_class.h"
#include "sql/sql_cmd.h"
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_executor.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_optimizer.h"
#include "sql/sql_select.h"
#include "sql/sql_tmp_table.h"
#include "sql/table_function.h"
#include "sql/thd_raii.h"
#include "sql/visible_fields.h"
#include "sql/window.h"
#include "template_utils.h"

Classes

class  Query_result_union_direct
 This class is effectively dead. More...
 
class  Change_current_query_block
 RAII class to automate saving/restoring of current_query_block() More...
 

Functions

static bool create_tmp_table_for_set_op (THD *thd, Query_term *qt, mem_root_deque< Item * > &types, ulonglong create_options)
 Create a tmp table for a set operation. More...
 
static bool finalize_full_text_functions (THD *thd, Query_expression *query_expression)
 Finalizes the initialization of all the full-text functions used in the given query expression, and recursively in every query expression inner to the given one. More...
 
static bool optimize_set_operand (THD *thd, Query_expression *qe, Query_term *qt)
 Optimize the post processing query blocks of the query expression's query term tree recursively. More...
 
static bool contributes_to_rowcount_estimate (Query_block *qb)
 Determine if we should set or add the contribution of the given query block to the total row count estimate for the query expression. More...
 
static bool use_iterator (TABLE *materialize_destination, Query_term *query_term)
 
static AccessPathadd_materialized_access_path (THD *thd, Query_term *qt, Mem_root_array< MaterializePathParameters::Operand > &operands, TABLE *dest, ha_rows limit=HA_POS_ERROR)
 Helper method: create a materialized access path, estimate its cost and move it to the best place, cf. More...
 
AccessPathmake_set_op_access_path (THD *thd, Query_term_set_op *parent, Query_term *qt, Mem_root_array< AppendPathParameters > *union_all_subpaths, bool calc_found_rows)
 Recursively constructs the access path of the set operation, possibly materializing in a tmp table if needed, cf. More...
 
static void cleanup_tmp_tables (Table_ref *list)
 Closes (and, if last reference, drops) temporary tables created to materialize derived tables, schema tables and CTEs. More...
 
static void destroy_tmp_tables (Table_ref *list)
 Destroy temporary tables created to materialize derived tables, schema tables and CTEs. More...
 

Function Documentation

◆ add_materialized_access_path()

static AccessPath * add_materialized_access_path ( THD thd,
Query_term qt,
Mem_root_array< MaterializePathParameters::Operand > &  operands,
TABLE dest,
ha_rows  limit = HA_POS_ERROR 
)
static

Helper method: create a materialized access path, estimate its cost and move it to the best place, cf.

doc for MoveCompositeIteratorsFromTablePath

Parameters
thdsession state
qtquery term for which we want to create a materialized access path
operandsthe constituent operands (query blocks) we want to materialize
destthe destination temporary (materialized) table
limitIf not HA_POS_ERROR, the maximum number of rows allowed in the materialized table
Returns
non-empty access path. If empty, this is an error

◆ cleanup_tmp_tables()

static void cleanup_tmp_tables ( Table_ref list)
static

Closes (and, if last reference, drops) temporary tables created to materialize derived tables, schema tables and CTEs.

Parameters
listList of tables to search in

◆ contributes_to_rowcount_estimate()

static bool contributes_to_rowcount_estimate ( Query_block qb)
static

Determine if we should set or add the contribution of the given query block to the total row count estimate for the query expression.

If we have INTERSECT or EXCEPT, only set row estimate for left side since the total number of rows in the result set can only decrease as a result of the set operation.

Parameters
qbquery block
Returns
true if the estimate should be added

◆ create_tmp_table_for_set_op()

static bool create_tmp_table_for_set_op ( THD thd,
Query_term qt,
mem_root_deque< Item * > &  types,
ulonglong  create_options 
)
static

Create a tmp table for a set operation.

Parameters
thdsession context
qtquery term holding the query result to hold the tmp table
typesthe fields of the tmp table, inherited from Query_expression::types
create_optionscreate options for create_tmp_table
Returns
false on success, true on error

◆ destroy_tmp_tables()

static void destroy_tmp_tables ( Table_ref list)
static

Destroy temporary tables created to materialize derived tables, schema tables and CTEs.

Parameters
listList of tables to search in

◆ finalize_full_text_functions()

static bool finalize_full_text_functions ( THD thd,
Query_expression query_expression 
)
static

Finalizes the initialization of all the full-text functions used in the given query expression, and recursively in every query expression inner to the given one.

We do this fairly late, since we need to know whether or not the full-text function is to be used for a full-text index scan, and whether or not that scan is sorted. When the iterators have been created, we know that the final decision has been made, so we do it right after the iterators have been created.

◆ make_set_op_access_path()

AccessPath * make_set_op_access_path ( THD thd,
Query_term_set_op parent,
Query_term qt,
Mem_root_array< AppendPathParameters > *  union_all_subpaths,
bool  calc_found_rows 
)

Recursively constructs the access path of the set operation, possibly materializing in a tmp table if needed, cf.

Query_term_set_op::m_is_materialized

Parameters
thdsession context
parentthe parent of qt
qtthe query term at this level of the tree
union_all_subpathsif not nullptr, we are part of a UNION all, add constructed access to it.
calc_found_rowsif true, do allow for calculation of number of found rows even in presence of LIMIT.
Returns
access path, if nullptr, this is an error

◆ optimize_set_operand()

static bool optimize_set_operand ( THD thd,
Query_expression qe,
Query_term qt 
)
static

Optimize the post processing query blocks of the query expression's query term tree recursively.

Parameters
thdsession context
qethe owning query expression
qtthe current query term to optimize
Returns
false on success, true on error

◆ use_iterator()

static bool use_iterator ( TABLE materialize_destination,
Query_term query_term 
)
static