MySQL 8.0.29
Source Code Documentation
finalize_plan.cc File Reference
#include <assert.h>
#include <list>
#include <utility>
#include "mem_root_deque.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_inttypes.h"
#include "my_sqlcommand.h"
#include "prealloced_array.h"
#include "sql/filesort.h"
#include "sql/item.h"
#include "sql/item_sum.h"
#include "sql/join_optimizer/access_path.h"
#include "sql/join_optimizer/join_optimizer.h"
#include "sql/join_optimizer/materialize_path_parameters.h"
#include "sql/join_optimizer/node_map.h"
#include "sql/join_optimizer/relational_expression.h"
#include "sql/join_optimizer/replace_item.h"
#include "sql/join_optimizer/walk_access_paths.h"
#include "sql/mem_root_array.h"
#include "sql/sql_class.h"
#include "sql/sql_executor.h"
#include "sql/sql_insert.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_optimizer.h"
#include "sql/sql_resolver.h"
#include "sql/sql_select.h"
#include "sql/sql_tmp_table.h"
#include "sql/table.h"
#include "sql/temp_table_param.h"
#include "sql/visible_fields.h"
#include "sql/window.h"
#include "template_utils.h"

Functions

static void ReplaceUpdateValuesWithTempTableFields (Sql_cmd_insert_select *sql_cmd, Query_block *query_block, const mem_root_deque< Item * > &original_fields, const mem_root_deque< Item * > &temp_table_fields)
 Replaces field references in an ON DUPLICATE KEY UPDATE clause with references to corresponding fields in a temporary table. More...
 
static TABLECreateTemporaryTableFromSelectList (THD *thd, Query_block *query_block, Window *window, Temp_table_param **temp_table_param_arg, bool after_aggregation)
 Creates a temporary table with columns matching the SELECT list of the given query block. More...
 
static void ReplaceSelectListWithTempTableFields (THD *thd, JOIN *join, const Func_ptr_array &items_to_copy)
 Replaces the items in the SELECT list with items that point to fields in a temporary table. More...
 
void ReplaceOrderItemsWithTempTableFields (THD *thd, ORDER *order, const Func_ptr_array &items_to_copy)
 
static Temp_table_paramGetItemsToCopy (AccessPath *path)
 
static void UpdateReferencesToMaterializedItems (THD *thd, Query_block *query_block, AccessPath *path, bool after_aggregation, Mem_root_array< const Func_ptr_array * > *applied_replacements)
 See FinalizePlanForQueryBlock(). More...
 
static void DelayedCreateTemporaryTable (THD *thd, Query_block *query_block, AccessPath *path, bool after_aggregation, TABLE **last_window_temp_table, unsigned *num_windows_seen)
 If the given access path needs a temporary table, it instantiates said table (we cannot do this until we have a final access path list, where we know which temporary tables are created and in which order). More...
 
static void FinalizeWindowPath (THD *thd, Query_block *query_block, const mem_root_deque< Item * > &original_fields, const Mem_root_array< const Func_ptr_array * > &applied_replacements, AccessPath *path)
 See FinalizePlanForQueryBlock(). More...
 
static bool ContainsMaterialization (AccessPath *path)
 
static ItemAddCachesAroundConstantConditions (Item *item)
 
static bool AddCachesAroundConstantConditionsInPath (AccessPath *path)
 
bool FinalizePlanForQueryBlock (THD *thd, Query_block *query_block)
 

Function Documentation

◆ AddCachesAroundConstantConditions()

static Item * AddCachesAroundConstantConditions ( Item item)
static

◆ AddCachesAroundConstantConditionsInPath()

static bool AddCachesAroundConstantConditionsInPath ( AccessPath path)
static

◆ ContainsMaterialization()

static bool ContainsMaterialization ( AccessPath path)
static

◆ CreateTemporaryTableFromSelectList()

static TABLE * CreateTemporaryTableFromSelectList ( THD thd,
Query_block query_block,
Window window,
Temp_table_param **  temp_table_param_arg,
bool  after_aggregation 
)
static

Creates a temporary table with columns matching the SELECT list of the given query block.

(In FinalizePlanForQueryBlock(), the SELECT list of the query block is updated to point to the fields in the temporary table, but not here.)

This function is used for materializing the query result, either as an intermediate step before sorting the final result if the sort requires the rows to come from a single table instead of a join, or as the last step if the SQL_BUFFER_RESULT query option has been specified. It is also used for setting up the output temporary table for window functions.

NOTE: If after_aggregation = true, it is impossible to call this function again later with after_aggregation = false, as count_field_types() will remove item->has_aggregation() once called. Thus, we need to set up all these temporary tables in FinalizePlanForQueryBlock(), in the right order.

◆ DelayedCreateTemporaryTable()

static void DelayedCreateTemporaryTable ( THD thd,
Query_block query_block,
AccessPath path,
bool  after_aggregation,
TABLE **  last_window_temp_table,
unsigned *  num_windows_seen 
)
static

If the given access path needs a temporary table, it instantiates said table (we cannot do this until we have a final access path list, where we know which temporary tables are created and in which order).

For window functions, it also needs to forward this information to the materialization access path coming right after this window, if any, so it uses last_window_temp_table as a buffer to hold this.

◆ FinalizePlanForQueryBlock()

bool FinalizePlanForQueryBlock ( THD thd,
Query_block query_block 
)

◆ FinalizeWindowPath()

static void FinalizeWindowPath ( THD thd,
Query_block query_block,
const mem_root_deque< Item * > &  original_fields,
const Mem_root_array< const Func_ptr_array * > &  applied_replacements,
AccessPath path 
)
static

See FinalizePlanForQueryBlock().

◆ GetItemsToCopy()

static Temp_table_param * GetItemsToCopy ( AccessPath path)
static

◆ ReplaceOrderItemsWithTempTableFields()

void ReplaceOrderItemsWithTempTableFields ( THD thd,
ORDER order,
const Func_ptr_array items_to_copy 
)

◆ ReplaceSelectListWithTempTableFields()

static void ReplaceSelectListWithTempTableFields ( THD thd,
JOIN join,
const Func_ptr_array items_to_copy 
)
static

Replaces the items in the SELECT list with items that point to fields in a temporary table.

See FinalizePlanForQueryBlock() for more information.

◆ ReplaceUpdateValuesWithTempTableFields()

static void ReplaceUpdateValuesWithTempTableFields ( Sql_cmd_insert_select sql_cmd,
Query_block query_block,
const mem_root_deque< Item * > &  original_fields,
const mem_root_deque< Item * > &  temp_table_fields 
)
static

Replaces field references in an ON DUPLICATE KEY UPDATE clause with references to corresponding fields in a temporary table.

The changes will be rolled back at the end of execution and will have to be redone during optimization in the next execution.

◆ UpdateReferencesToMaterializedItems()

static void UpdateReferencesToMaterializedItems ( THD thd,
Query_block query_block,
AccessPath path,
bool  after_aggregation,
Mem_root_array< const Func_ptr_array * > *  applied_replacements 
)
static

See FinalizePlanForQueryBlock().