MySQL 9.5.0
Source Code Documentation
sp_inl::sp_inline_instr Class Referenceabstract

Base class for instructions within stored programs (currently limited to stored functions) that are eligible for inlining. More...

Inheritance diagram for sp_inl::sp_inline_instr:
[legend]

Public Member Functions

 sp_inline_instr (sp_instr *sp_instr)
 
virtual ~sp_inline_instr ()
 
void set_is_validated_instr ()
 Sets m_is_validated_instr to true, indicating that the instruction has successfully passed the checks performed by validate(). More...
 
bool is_validated_instr ()
 Gets the value of m_is_validated_instr. More...
 
void set_is_redundant_instr ()
 Sets m_is_redundant_instr to true, indicating that the instruction is not required for computing the return result of a stored function. More...
 
bool is_redundant_instr ()
 Gets the value of m_is_redundant_instr. More...
 
uint get_ip ()
 Retrieves the instruction pointer (IP) for this instruction. More...
 
virtual bool check_redundant_instr_for_errors (THD *thd, std::string &err_reason)
 Checks for any errors that may occur during the preparation phase of a redundant instruction. More...
 
virtual bool validate (std::string &err_reason)=0
 Checks if this stored function instruction can be inlined. More...
 
virtual bool compute_is_redundant_and_collect_functions (THD *thd, std::unordered_set< uint > &offsets_live_variables, std::unordered_set< sp_head * > &used_sp_functions, std::string &err_reason)=0
 Determines whether the current stored function instruction is redundant based on the provided set of live variables. More...
 
virtual bool process (THD *thd, std::unordered_map< uint, Item * > &map_var_offset_to_value, Item **sp_args, uint sp_arg_count, sp_head *sp_head, Name_resolution_context *sp_name_resolution_ctx, Item **result_item)=0
 Processes an sp_inline_instr that is validated and non-redundant. More...
 

Static Public Member Functions

static void record_instruction_inlining_error (std::string &err_reason, uint ip)
 

Protected Member Functions

bool walk_and_collect_functions_and_variables (THD *thd, Item *item, std::unordered_set< uint > &var_offsets, std::unordered_set< sp_head * > &used_sp_functions, std::string &err_reason)
 Traverses the item tree and collects variable offsets for any encountered Item::ROUTINE_FIELD_ITEM instances. More...
 
bool collect_functions_and_variables_from_query_block (THD *thd, Query_block *qb, std::unordered_set< uint > &variable_offsets, std::unordered_set< sp_head * > &used_sp_functions, std::string &err_reason)
 Collects variable offsets for all encountered Item::ROUTINE_FIELD_ITEM instances across the various components of the query block. More...
 
Itemparse_and_inline_expression (THD *thd, Item *expr_item, std::unordered_map< uint, Item * > &map_var_offset_to_value, Item **sp_args, uint sp_arg_count)
 Creates a deep copy of the input expression and then in-place replaces the references to local variables with their values. More...
 
Item_singlerow_subselectcreate_and_inline_subquery (THD *thd, std::unordered_map< uint, Item * > &map_var_offset_to_value, Item **sp_args, uint sp_arg_count, sp_head *sp_head, Name_resolution_context *sp_name_resolution_ctx)
 Transform sp_inline_instr into a subquery with inlined references to local variables. More...
 

Protected Attributes

sp_instrm_sp_instr
 The original sp_instr which this class is meant to inline. More...
 

Private Member Functions

void inline_local_variables (Item **item, std::unordered_map< uint, Item * > &map_var_offset_to_value, Item **sp_args, uint sp_arg_count, Name_resolution_context *context)
 Transforms the input item by replacing the references to local variables with their values. More...
 
void inline_select_list (Query_block *qb, std::unordered_map< uint, Item * > &map_var_offset_to_value, Item **sp_args, uint sp_arg_count, Name_resolution_context *context)
 Inline local variables used in the select list of the query block. More...
 
bool inline_join_conditions (Query_block *qb, std::unordered_map< uint, Item * > &map_var_offset_to_value, Item **sp_args, uint sp_arg_count, Name_resolution_context *context)
 Inline local variables used in the join conditions of the query block. More...
 
void inline_where_cond (Query_block *qb, std::unordered_map< uint, Item * > &map_var_offset_to_value, Item **sp_args, uint sp_arg_count, Name_resolution_context *context)
 Inline local variables used in the where condition of the query block. More...
 
void inline_group_by (Query_block *qb, std::unordered_map< uint, Item * > &map_var_offset_to_value, Item **sp_args, uint sp_arg_count, Name_resolution_context *context)
 Inline local variables used in the group by clause of the query block. More...
 
void inline_window_functions (Query_block *qb, std::unordered_map< uint, Item * > &map_var_offset_to_value, Item **sp_args, uint sp_arg_count, Name_resolution_context *context)
 Inline local variables used in window functions of the query block. More...
 
void inline_order_by (Query_block *qb, std::unordered_map< uint, Item * > &map_var_offset_to_value, Item **sp_args, uint sp_arg_count, Name_resolution_context *context)
 Inline local variables used in the order by clause of the query block. More...
 
void inline_limit_offset (Query_block *qb, std::unordered_map< uint, Item * > &map_var_offset_to_value, Item **sp_args, uint sp_arg_count)
 Inline local variables used in limit and offset of the query block. More...
 

Private Attributes

bool m_is_redundant_instr
 Indicates whether this sp_inline_instr instance is required to compute the RETURN result of the stored function. More...
 
bool m_is_validated_instr
 Indicates whether this sp_inline_instr instance has passed the checks in validate. More...
 

Detailed Description

Base class for instructions within stored programs (currently limited to stored functions) that are eligible for inlining.

The sp_inline_instr class defines the interface and provides the foundational implementation for inlining stored function instructions.

Constructor & Destructor Documentation

◆ sp_inline_instr()

sp_inl::sp_inline_instr::sp_inline_instr ( sp_instr sp_instr)
inline

◆ ~sp_inline_instr()

virtual sp_inl::sp_inline_instr::~sp_inline_instr ( )
inlinevirtual

Member Function Documentation

◆ check_redundant_instr_for_errors()

virtual bool sp_inl::sp_inline_instr::check_redundant_instr_for_errors ( THD thd,
std::string &  err_reason 
)
inlinevirtual

Checks for any errors that may occur during the preparation phase of a redundant instruction.

Parameters
thdthread context
[out]err_reasonerror reason if it occurs
Returns
false if success, true if error

Reimplemented in sp_inl::sp_inline_instr_stmt.

◆ collect_functions_and_variables_from_query_block()

bool sp_inl::sp_inline_instr::collect_functions_and_variables_from_query_block ( THD thd,
Query_block qb,
std::unordered_set< uint > &  variable_offsets,
std::unordered_set< sp_head * > &  used_sp_functions,
std::string &  err_reason 
)
protected

Collects variable offsets for all encountered Item::ROUTINE_FIELD_ITEM instances across the various components of the query block.

Also gathers the set of stored functions invoked directly within this query block.

Parameters
[in]thdThread context
[in]qbQuery block
[out]variable_offsetsSet of variable offsets used in the query block
[out]used_sp_functionsSet of stored functions called directly within this query block
[out]err_reasonPopulated with the error reason if an error occurs
Returns
false if success, true if error

◆ compute_is_redundant_and_collect_functions()

virtual bool sp_inl::sp_inline_instr::compute_is_redundant_and_collect_functions ( THD thd,
std::unordered_set< uint > &  offsets_live_variables,
std::unordered_set< sp_head * > &  used_sp_functions,
std::string &  err_reason 
)
pure virtual

Determines whether the current stored function instruction is redundant based on the provided set of live variables.

A live variable is one that is required to compute the final result of the stored function. An instruction is considered redundant if it does not contribute to this result. If the instruction is determined to be non-redundant, this function updates the set of live variables with those used by the instruction. Additionally, the function collects instances of stored functions referenced in this instruction in order to use them later for recursion detection.

Parameters
[in]thdThread context.
[in,out]offsets_live_variablesCurrent set of live variable offsets.
[out]used_sp_functionsSet of stored function instances used in this instruction.
[out]err_reasonPopulated with the error reason if an error occurs.
Returns
false if the computation is successful; true otherwise.

Implemented in sp_inl::sp_inline_instr_set, sp_inl::sp_inline_instr_stmt, and sp_inl::sp_inline_instr_freturn.

◆ create_and_inline_subquery()

Item_singlerow_subselect * sp_inl::sp_inline_instr::create_and_inline_subquery ( THD thd,
std::unordered_map< uint, Item * > &  map_var_offset_to_value,
Item **  sp_args,
uint  sp_arg_count,
sp_head sp_head,
Name_resolution_context sp_name_resolution_ctx 
)
protected

Transform sp_inline_instr into a subquery with inlined references to local variables.

Parameters
[in]thdThread context
[in]map_var_offset_to_valueMap of variable offsets to their current values
[in]sp_argsInput arguments of the stored function
[in]sp_arg_countNumber of arguments of the stored function
[in]sp_headInstance of the stored function
[in]sp_name_resolution_ctxName resolution context of the stored function

◆ get_ip()

uint sp_inl::sp_inline_instr::get_ip ( )
inline

Retrieves the instruction pointer (IP) for this instruction.

The IP represents the order number of the instruction within the stored function.

◆ inline_group_by()

void sp_inl::sp_inline_instr::inline_group_by ( Query_block qb,
std::unordered_map< uint, Item * > &  map_var_offset_to_value,
Item **  sp_args,
uint  sp_arg_count,
Name_resolution_context context 
)
private

Inline local variables used in the group by clause of the query block.

Parameters
[in,out]qbQuery block
[in]map_var_offset_to_valueMap of variable offsets to their current values
[in]sp_argsInput arguments of the stored function
[in]sp_arg_countNumber of arguments of the stored function
[in]contextName resolution context of the stored function

◆ inline_join_conditions()

bool sp_inl::sp_inline_instr::inline_join_conditions ( Query_block qb,
std::unordered_map< uint, Item * > &  map_var_offset_to_value,
Item **  sp_args,
uint  sp_arg_count,
Name_resolution_context context 
)
private

Inline local variables used in the join conditions of the query block.

Parameters
[in,out]qbQuery block
[in]map_var_offset_to_valueMap of variable offsets to their current values
[in]sp_argsInput arguments of the stored function
[in]sp_arg_countNumber of arguments of the stored function
[in]contextName resolution context of the stored function
Returns
false if success, true if error

◆ inline_limit_offset()

void sp_inl::sp_inline_instr::inline_limit_offset ( Query_block qb,
std::unordered_map< uint, Item * > &  map_var_offset_to_value,
Item **  sp_args,
uint  sp_arg_count 
)
private

Inline local variables used in limit and offset of the query block.

Parameters
[in,out]qbQuery block
[in]map_var_offset_to_valueMap of variable offsets to their current values
[in]sp_argsInput arguments of the stored function
[in]sp_arg_countNumber of arguments of the stored function

◆ inline_local_variables()

void sp_inl::sp_inline_instr::inline_local_variables ( Item **  item,
std::unordered_map< uint, Item * > &  map_var_offset_to_value,
Item **  sp_args,
uint  sp_arg_count,
Name_resolution_context context 
)
private

Transforms the input item by replacing the references to local variables with their values.

If the value is a FIELD_ITEM, m_was_sp_local_variable is set to true.

Parameters
[in,out]itemItem to transform
[in]map_var_offset_to_valueMap of variable offsets to their current values
[in]sp_argsInput arguments of the stored function
[in]sp_arg_countNumber of arguments of the stored function
[in]contextName resolution context of the stored function

◆ inline_order_by()

void sp_inl::sp_inline_instr::inline_order_by ( Query_block qb,
std::unordered_map< uint, Item * > &  map_var_offset_to_value,
Item **  sp_args,
uint  sp_arg_count,
Name_resolution_context context 
)
private

Inline local variables used in the order by clause of the query block.

Parameters
[in,out]qbQuery block
[in]map_var_offset_to_valueMap of variable offsets to their current values
[in]sp_argsInput arguments of the stored function
[in]sp_arg_countNumber of arguments of the stored function
[in]contextName resolution context of the stored function

◆ inline_select_list()

void sp_inl::sp_inline_instr::inline_select_list ( Query_block qb,
std::unordered_map< uint, Item * > &  map_var_offset_to_value,
Item **  sp_args,
uint  sp_arg_count,
Name_resolution_context context 
)
private

Inline local variables used in the select list of the query block.

Parameters
[in,out]qbQuery block
[in]map_var_offset_to_valueMap of variable offsets to their current values
[in]sp_argsInput arguments of the stored function
[in]sp_arg_countNumber of arguments of the stored function
[in]contextName resolution context of the stored function

◆ inline_where_cond()

void sp_inl::sp_inline_instr::inline_where_cond ( Query_block qb,
std::unordered_map< uint, Item * > &  map_var_offset_to_value,
Item **  sp_args,
uint  sp_arg_count,
Name_resolution_context context 
)
private

Inline local variables used in the where condition of the query block.

Parameters
[in,out]qbQuery block
[in]map_var_offset_to_valueMap of variable offsets to their current values
[in]sp_argsInput arguments of the stored function
[in]sp_arg_countNumber of arguments of the stored function
[in]contextName resolution context of the stored function

◆ inline_window_functions()

void sp_inl::sp_inline_instr::inline_window_functions ( Query_block qb,
std::unordered_map< uint, Item * > &  map_var_offset_to_value,
Item **  sp_args,
uint  sp_arg_count,
Name_resolution_context context 
)
private

Inline local variables used in window functions of the query block.

Parameters
[in,out]qbQuery block
[in]map_var_offset_to_valueMap of variable offsets to their current values
[in]sp_argsInput arguments of the stored function
[in]sp_arg_countNumber of arguments of the stored function
[in]contextName resolution context of the stored function

◆ is_redundant_instr()

bool sp_inl::sp_inline_instr::is_redundant_instr ( )
inline

Gets the value of m_is_redundant_instr.

◆ is_validated_instr()

bool sp_inl::sp_inline_instr::is_validated_instr ( )
inline

Gets the value of m_is_validated_instr.

◆ parse_and_inline_expression()

Item * sp_inl::sp_inline_instr::parse_and_inline_expression ( THD thd,
Item expr_item,
std::unordered_map< uint, Item * > &  map_var_offset_to_value,
Item **  sp_args,
uint  sp_arg_count 
)
protected

Creates a deep copy of the input expression and then in-place replaces the references to local variables with their values.

Parameters
[in]thdThread context
[in]expr_itemThe original item tree
[in]map_var_offset_to_valueMap of variable offsets to their current values
[in]sp_argsInput arguments of the stored function
[in]sp_arg_countNumber of arguments of the stored function
Returns
A copy of expr_item with inlined variable contents

◆ process()

virtual bool sp_inl::sp_inline_instr::process ( THD thd,
std::unordered_map< uint, Item * > &  map_var_offset_to_value,
Item **  sp_args,
uint  sp_arg_count,
sp_head sp_head,
Name_resolution_context sp_name_resolution_ctx,
Item **  result_item 
)
pure virtual

Processes an sp_inline_instr that is validated and non-redundant.

This method either updates the current mapping of local variables to their values or returns the final result through the result argument.

Parameters
[in]thdThread context.
[in,out]map_var_offset_to_valueCurrent mapping of local variables to their values.
[in]sp_argsInput arguments of the stored function.
[in]sp_arg_countNumber of arguments for the stored function.
[in]sp_headStored function instance.
[in]sp_name_resolution_ctxName resolution context for the stored function.
[out]result_itemResult of a processed return instruction, or nullptr if not applicable.
Returns
false if success, true if error

Implemented in sp_inl::sp_inline_instr_set, sp_inl::sp_inline_instr_stmt, and sp_inl::sp_inline_instr_freturn.

◆ record_instruction_inlining_error()

static void sp_inl::sp_inline_instr::record_instruction_inlining_error ( std::string &  err_reason,
uint  ip 
)
inlinestatic

◆ set_is_redundant_instr()

void sp_inl::sp_inline_instr::set_is_redundant_instr ( )
inline

Sets m_is_redundant_instr to true, indicating that the instruction is not required for computing the return result of a stored function.

◆ set_is_validated_instr()

void sp_inl::sp_inline_instr::set_is_validated_instr ( )
inline

Sets m_is_validated_instr to true, indicating that the instruction has successfully passed the checks performed by validate().

◆ validate()

virtual bool sp_inl::sp_inline_instr::validate ( std::string &  err_reason)
pure virtual

Checks if this stored function instruction can be inlined.

Parameters
[out]err_reasonif the function cannot be inlined err_reason returns the error reason
Returns
"false" if validation successful, "true" otherwise

Implemented in sp_inl::sp_inline_instr_set, sp_inl::sp_inline_instr_stmt, and sp_inl::sp_inline_instr_freturn.

◆ walk_and_collect_functions_and_variables()

bool sp_inl::sp_inline_instr::walk_and_collect_functions_and_variables ( THD thd,
Item item,
std::unordered_set< uint > &  var_offsets,
std::unordered_set< sp_head * > &  used_sp_functions,
std::string &  err_reason 
)
protected

Traverses the item tree and collects variable offsets for any encountered Item::ROUTINE_FIELD_ITEM instances.

Additionally, collects instances of stored functions identified during the traversal.

Parameters
[in]thdThread context
[in]itemItem to traverse
[out]var_offsetsSet of variable offsets found within the item
[out]used_sp_functionsSet of stored function instances found in the item
[out]err_reasonPopulated with the error reason if an error occurs
Returns
false if success, true if error

Member Data Documentation

◆ m_is_redundant_instr

bool sp_inl::sp_inline_instr::m_is_redundant_instr
private

Indicates whether this sp_inline_instr instance is required to compute the RETURN result of the stored function.

Computed during compute_is_redundant_and_collect_functions.

◆ m_is_validated_instr

bool sp_inl::sp_inline_instr::m_is_validated_instr
private

Indicates whether this sp_inline_instr instance has passed the checks in validate.

◆ m_sp_instr

sp_instr* sp_inl::sp_inline_instr::m_sp_instr
protected

The original sp_instr which this class is meant to inline.


The documentation for this class was generated from the following file: