MySQL  8.0.17
Source Code Documentation
SELECT_LEX_UNIT Class Reference

This class represents a query expression (one query block or several query blocks combined with UNION). More...

#include <sql_lex.h>

Public Types

enum  enum_clean_state { UC_DIRTY, UC_PART_CLEAN, UC_CLEAN }
 Values for SELECT_LEX_UNIT::cleaned. More...
 

Public Member Functions

 SELECT_LEX_UNIT (enum_parsing_context parsing_context)
 Construct and initialize SELECT_LEX_UNIT object. More...
 
bool is_simple () const
 
SELECT_LEXglobal_parameters () const
 Pointer to query block containing global parameters for query. More...
 
bool is_mergeable () const
 Return true if query expression can be merged into an outer query, based on technical constraints. More...
 
bool merge_heuristic (const LEX *lex) const
 True if heuristics suggest to merge this query expression. More...
 
SELECT_LEXouter_select () const
 
SELECT_LEXfirst_select () const
 
SELECT_LEX_UNITnext_unit () const
 
Query_resultquery_result () const
 
const Query_resultrecursive_result (SELECT_LEX *reader) const
 If this unit is recursive, then this returns the Query_result which holds the rows of the recursive reference read by 'reader': More...
 
void set_query_result (Query_result *res)
 Set new query result object for this query expression. More...
 
bool prepare (THD *thd, Query_result *result, ulonglong added_options, ulonglong removed_options)
 Prepares all query blocks of a query expression, including fake_select_lex. More...
 
bool optimize (THD *thd)
 Optimize all query blocks of a query expression, including fake_select_lex. More...
 
bool execute (THD *thd)
 Execute a query expression that may be a UNION and/or have an ordered result. More...
 
bool explain (THD *explain_thd, const THD *query_thd)
 Explain query starting from this unit. More...
 
bool cleanup (THD *thd, bool full)
 Cleanup this query expression object after preparation or one round of execution. More...
 
void unclean ()
 
void reinit_exec_mechanism ()
 
void print (const THD *thd, String *str, enum_query_type query_type)
 
bool accept (Select_lex_visitor *visitor)
 
bool add_fake_select_lex (THD *thd)
 Create a fake SELECT_LEX for a unit. More...
 
bool prepare_fake_select_lex (THD *thd)
 Prepare the fake_select_lex query block. More...
 
void set_prepared ()
 
void set_optimized ()
 
void set_executed ()
 
void reset_executed ()
 
bool is_prepared () const
 
bool is_optimized () const
 
bool is_executed () const
 
bool change_query_result (THD *thd, Query_result_interceptor *result, Query_result_interceptor *old_result)
 Change the query result object used to return the final result of the unit, replacing occurences of old_result with new_result. More...
 
bool prepare_limit (THD *thd, SELECT_LEX *provider)
 Prepare sources for offset and limit counters. More...
 
bool set_limit (THD *thd, SELECT_LEX *provider)
 Set limit and offset for query expression object. More...
 
bool is_union () const
 
bool union_needs_tmp_table (LEX *lex)
 Decide if a temporary table is needed for the UNION. More...
 
bool mixed_union_operators () const
 
void include_down (LEX *lex, SELECT_LEX *outer)
 Include a query expression below a query block. More...
 
void exclude_level ()
 Exclude this unit and immediately contained select_lex objects. More...
 
void exclude_tree (THD *thd)
 Exclude subtree of current unit from tree of SELECTs. More...
 
void renumber_selects (LEX *lex)
 Renumber query blocks of a query expression according to supplied LEX. More...
 
List< Item > * get_unit_column_types ()
 Get column type information for this query expression. More...
 
List< Item > * get_field_list ()
 Get field list for this query expression. More...
 
enum_parsing_context get_explain_marker (const THD *thd) const
 
void set_explain_marker (THD *thd, enum_parsing_context m)
 
void set_explain_marker_from (THD *thd, const SELECT_LEX_UNIT *u)
 
void assert_not_fully_clean ()
 Asserts that none of {this unit and its children units} is fully cleaned up. More...
 
void invalidate ()
 Invalidate by nulling out pointers to other SELECT_LEX_UNITs and SELECT_LEXes. More...
 
bool is_recursive () const
 
bool check_materialized_derived_query_blocks (THD *thd)
 Sets up query blocks belonging to the query expression of a materialized derived table. More...
 
bool clear_corr_ctes ()
 Empties all correlated CTEs defined in the unit's WITH clause. More...
 
void fix_after_pullout (SELECT_LEX *parent_select, SELECT_LEX *removed_select)
 Fix used tables information for a subquery after query transformations. More...
 
void accumulate_used_tables (table_map map)
 If unit is a subquery, which forms an object of the upper level (an Item_subselect, a derived TABLE_LIST), adds to this object a map of tables of the upper level which the unit references. More...
 
enum_parsing_context place () const
 If unit is a subquery, which forms an object of the upper level (an Item_subselect, a derived TABLE_LIST), returns the place of this object in the upper level query block. More...
 
bool walk (Item_processor processor, enum_walk walk, uchar *arg)
 

Public Attributes

uint8 uncacheable
 result of this query can't be cached, bit field, can be : UNCACHEABLE_DEPENDENT UNCACHEABLE_RAND UNCACHEABLE_SIDEEFFECT More...
 
enum_clean_state cleaned
 cleanliness state More...
 
List< Itemitem_list
 
List< Itemtypes
 
ha_rows select_limit_cnt
 
ha_rows offset_limit_cnt
 
Item_subselectitem
 Points to subquery if this query expression is used in one, otherwise NULL. More...
 
SELECT_LEXfake_select_lex
 Helper query block for query expression with UNION or multi-level ORDER BY/LIMIT. More...
 
SELECT_LEXsaved_fake_select_lex
 SELECT_LEX that stores LIMIT and OFFSET for UNION ALL when no fake_select_lex is used. More...
 
SELECT_LEXunion_distinct
 Points to last query block which has UNION DISTINCT on its left. More...
 
PT_with_clausem_with_clause
 The WITH clause which is the first part of this query expression. More...
 
TABLE_LISTderived_table
 If this query expression is underlying of a derived table, the derived table. More...
 
SELECT_LEXfirst_recursive
 First query block (in this UNION) which references the CTE. More...
 
table_map m_lateral_deps
 If 'this' is body of lateral derived table: map of tables in the same FROM clause as this derived table, and to which the derived table's body makes references. More...
 
bool got_all_recursive_rows
 True if the with-recursive algorithm has produced the complete result. More...
 

Private Attributes

SELECT_LEX_UNITnext
 Intrusive double-linked list of all query expressions immediately contained within the same query block. More...
 
SELECT_LEX_UNIT ** prev
 
SELECT_LEXmaster
 The query block wherein this query expression is contained, NULL if the query block is the outer-most one. More...
 
SELECT_LEXslave
 The first query block in this query expression. More...
 
enum_parsing_context explain_marker
 
Marker for subqueries in WHERE, HAVING, ORDER BY, GROUP BY and SELECT item lists. More...
 
bool prepared
 All query blocks in query expression are prepared. More...
 
bool optimized
 All query blocks in query expression are optimized. More...
 
bool executed
 Query expression has been executed. More...
 
TABLE_LIST result_table_list
 
Query_result_unionunion_result
 
TABLEtable
 
Query_resultm_query_result
 Object to which the result for this query expression is sent. More...
 

Friends

class SELECT_LEX
 
bool parse_view_definition (THD *thd, TABLE_LIST *view_ref)
 Parse a view definition. More...
 

Detailed Description

This class represents a query expression (one query block or several query blocks combined with UNION).

Member Enumeration Documentation

◆ enum_clean_state

Values for SELECT_LEX_UNIT::cleaned.

Enumerator
UC_DIRTY 

Unit isn't cleaned.

UC_PART_CLEAN 

Unit were cleaned, except JOIN and JOIN_TABs were kept for possible EXPLAIN.

UC_CLEAN 

Unit completely cleaned, all underlying JOINs were freed.

Constructor & Destructor Documentation

◆ SELECT_LEX_UNIT()

SELECT_LEX_UNIT::SELECT_LEX_UNIT ( enum_parsing_context  parsing_context)
explicit

Construct and initialize SELECT_LEX_UNIT object.

Member Function Documentation

◆ accept()

bool SELECT_LEX_UNIT::accept ( Select_lex_visitor visitor)

◆ accumulate_used_tables()

void SELECT_LEX_UNIT::accumulate_used_tables ( table_map  map)
inline

If unit is a subquery, which forms an object of the upper level (an Item_subselect, a derived TABLE_LIST), adds to this object a map of tables of the upper level which the unit references.

◆ assert_not_fully_clean()

void SELECT_LEX_UNIT::assert_not_fully_clean ( )

Asserts that none of {this unit and its children units} is fully cleaned up.

◆ change_query_result()

bool SELECT_LEX_UNIT::change_query_result ( THD thd,
Query_result_interceptor new_result,
Query_result_interceptor old_result 
)

Change the query result object used to return the final result of the unit, replacing occurences of old_result with new_result.

Parameters
thdThread handle
new_resultNew query result object
old_resultOld query result object
Return values
falseSuccess
trueError

◆ check_materialized_derived_query_blocks()

bool SELECT_LEX_UNIT::check_materialized_derived_query_blocks ( THD thd_arg)

Sets up query blocks belonging to the query expression of a materialized derived table.

Parameters
thd_argTHD pointer
Returns
false if successful, true if error

◆ cleanup()

bool SELECT_LEX_UNIT::cleanup ( THD thd,
bool  full 
)

Cleanup this query expression object after preparation or one round of execution.

After the cleanup, the object can be reused for a new round of execution, but a new optimization will be needed before the execution.

Returns
false if previous execution was successful, and true otherwise

◆ clear_corr_ctes()

bool SELECT_LEX_UNIT::clear_corr_ctes ( )

Empties all correlated CTEs defined in the unit's WITH clause.

◆ exclude_level()

void SELECT_LEX_UNIT::exclude_level ( )

Exclude this unit and immediately contained select_lex objects.

Exclude this unit and its immediately contained select_lex objects from query expression / query block chain.

Note
Units that belong to the select_lex objects of the current unit will be brought up one level and will replace the current unit in the list of units.

◆ exclude_tree()

void SELECT_LEX_UNIT::exclude_tree ( THD thd)

Exclude subtree of current unit from tree of SELECTs.

◆ execute()

bool SELECT_LEX_UNIT::execute ( THD thd)

Execute a query expression that may be a UNION and/or have an ordered result.

Parameters
thdthread handle
Returns
false if success, true if error

◆ explain()

bool SELECT_LEX_UNIT::explain ( THD explain_thd,
const THD query_thd 
)

Explain query starting from this unit.

Parameters
explain_thdthread handle for the connection doing explain
query_thdthread handle for the connection being explained
Returns
false if success, true if error

◆ first_select()

SELECT_LEX* SELECT_LEX_UNIT::first_select ( ) const
inline
Returns
the first query block inside this query expression

◆ fix_after_pullout()

void SELECT_LEX_UNIT::fix_after_pullout ( SELECT_LEX parent_select,
SELECT_LEX removed_select 
)

Fix used tables information for a subquery after query transformations.

Most actions here involve re-resolving information for conditions and items belonging to the subquery. Notice that the usage information from underlying expressions is not propagated to the subquery's predicate/table, as it belongs to inner layers of the query operator structure. However, when underlying expressions contain outer references into a select_lex on this level, the relevant information must be updated when these expressions are resolved.

◆ get_explain_marker()

enum_parsing_context SELECT_LEX_UNIT::get_explain_marker ( const THD thd) const

◆ get_field_list()

List< Item > * SELECT_LEX_UNIT::get_field_list ( )

Get field list for this query expression.

For a UNION of query blocks, return the field list generated during prepare. For a single query block, return the field list after all possible intermediate query processing steps are done (optimization is complete).

Returns
List containing fields of the query expression.

◆ get_unit_column_types()

List< Item > * SELECT_LEX_UNIT::get_unit_column_types ( )

Get column type information for this query expression.

For a single query block the column types are taken from the list of selected items of this block.

For a union this function assumes that SELECT_LEX_UNIT::prepare() has been called and returns the type holders that were created for unioned column types of all query blocks.

Note
The implementation of this function should be in sync with SELECT_LEX_UNIT::prepare()
Returns
List of items as specified in function description

◆ global_parameters()

SELECT_LEX* SELECT_LEX_UNIT::global_parameters ( ) const
inline

Pointer to query block containing global parameters for query.

Global parameters may include ORDER BY, LIMIT and OFFSET.

If this is a union of multiple query blocks, the global parameters are stored in fake_select_lex. If the union doesn't use a temporary table, SELECT_LEX_UNIT::prepare() nulls out fake_select_lex, but saves a copy in saved_fake_select_lex in order to preserve the global parameters.

If this is not a union, and the query expression has no multi-level ORDER BY/LIMIT, global parameters are in the single query block.

Returns
query block containing the global parameters

◆ include_down()

void SELECT_LEX_UNIT::include_down ( LEX lex,
SELECT_LEX outer 
)

Include a query expression below a query block.

Parameters
lexContaining LEX object
outerThe query block that this query expression is included below.

◆ invalidate()

void SELECT_LEX_UNIT::invalidate ( )

Invalidate by nulling out pointers to other SELECT_LEX_UNITs and SELECT_LEXes.

◆ is_executed()

bool SELECT_LEX_UNIT::is_executed ( ) const
inline

◆ is_mergeable()

bool SELECT_LEX_UNIT::is_mergeable ( ) const

Return true if query expression can be merged into an outer query, based on technical constraints.

Returns
true if query expression can be merged into an outer query

Being mergeable also means that derived table/view is updatable.

A view/derived table is not mergeable if it is one of the following:

  • A union (implementation restriction).
  • An aggregated query, or has HAVING, or has DISTINCT (A general aggregated query cannot be merged with a non-aggregated one).
  • A table-less query (unimportant special case).
  • A query with a LIMIT (limit applies to subquery, so the implementation strategy is to materialize this subquery, including row count constraint).
  • It has windows

◆ is_optimized()

bool SELECT_LEX_UNIT::is_optimized ( ) const
inline

◆ is_prepared()

bool SELECT_LEX_UNIT::is_prepared ( ) const
inline

◆ is_recursive()

bool SELECT_LEX_UNIT::is_recursive ( ) const
inline

◆ is_simple()

bool SELECT_LEX_UNIT::is_simple ( ) const
inline
Returns
true for a query expression without UNION or multi-level ORDER

◆ merge_heuristic()

bool SELECT_LEX_UNIT::merge_heuristic ( const LEX lex) const

True if heuristics suggest to merge this query expression.

Returns
true if query expression is recommended to be merged

A view/derived table is not suggested for merging if it contains subqueries in the SELECT list that depend on columns from itself. Merging such objects is possible, but we assume they are made derived tables because the user wants them to be materialized, for performance reasons.

One possible case is a derived table with dependent subqueries in the select list, used as the inner table of a left outer join. Such tables will always be read as many times as there are qualifying rows in the outer table, and the select list subqueries are evaluated for each row combination. The select list subqueries are evaluated the same number of times also with join buffering enabled, even though the table then only will be read once.

Another case is, a query that modifies variables: then try to preserve the original structure of the query. This is less likely to cause changes in variable assignment order.

◆ mixed_union_operators()

bool SELECT_LEX_UNIT::mixed_union_operators ( ) const
Returns
true if mixes UNION DISTINCT and UNION ALL

◆ next_unit()

SELECT_LEX_UNIT* SELECT_LEX_UNIT::next_unit ( ) const
inline
Returns
the next query expression within same query block (next subquery)

◆ optimize()

bool SELECT_LEX_UNIT::optimize ( THD thd)

Optimize all query blocks of a query expression, including fake_select_lex.

Parameters
thdthread handler
Returns
false if optimization successful, true if error

◆ outer_select()

SELECT_LEX* SELECT_LEX_UNIT::outer_select ( ) const
inline
Returns
the query block this query expression belongs to as subquery

◆ place()

enum_parsing_context SELECT_LEX_UNIT::place ( ) const
inline

If unit is a subquery, which forms an object of the upper level (an Item_subselect, a derived TABLE_LIST), returns the place of this object in the upper level query block.

◆ prepare()

bool SELECT_LEX_UNIT::prepare ( THD thd,
Query_result sel_result,
ulonglong  added_options,
ulonglong  removed_options 
)

Prepares all query blocks of a query expression, including fake_select_lex.

If a recursive query expression, this also creates the materialized temporary table.

Parameters
thdThread handler
sel_resultResult object where the unit's output should go.
added_optionsThese options will be added to the query blocks.
removed_optionsOptions that cannot be used for this query
Returns
false if success, true if error

◆ prepare_fake_select_lex()

bool SELECT_LEX_UNIT::prepare_fake_select_lex ( THD thd_arg)

Prepare the fake_select_lex query block.

Parameters
thd_argThread handler
Returns
false if success, true if error

◆ prepare_limit()

bool SELECT_LEX_UNIT::prepare_limit ( THD thd,
SELECT_LEX provider 
)

Prepare sources for offset and limit counters.

Parameters
thdthread handler
providerSELECT_LEX to get offset and limit from.
Returns
false if success, true if error

◆ print()

void SELECT_LEX_UNIT::print ( const THD thd,
String str,
enum_query_type  query_type 
)

◆ query_result()

Query_result* SELECT_LEX_UNIT::query_result ( ) const
inline
Returns
the query result object in use for this query expression

◆ recursive_result()

const Query_result * SELECT_LEX_UNIT::recursive_result ( SELECT_LEX reader) const

If this unit is recursive, then this returns the Query_result which holds the rows of the recursive reference read by 'reader':

  • fake_select_lex reads rows from the union's result
  • other recursive query blocks read rows from the derived table's result.
    Parameters
    readerRecursive query block belonging to this unit

◆ reinit_exec_mechanism()

void SELECT_LEX_UNIT::reinit_exec_mechanism ( )

◆ renumber_selects()

void SELECT_LEX_UNIT::renumber_selects ( LEX lex)

Renumber query blocks of a query expression according to supplied LEX.

Renumber contained select_lex objects.

Parameters
lexContaining LEX object

◆ reset_executed()

void SELECT_LEX_UNIT::reset_executed ( )
inline

◆ set_executed()

void SELECT_LEX_UNIT::set_executed ( )
inline

◆ set_explain_marker()

void SELECT_LEX_UNIT::set_explain_marker ( THD thd,
enum_parsing_context  m 
)

◆ set_explain_marker_from()

void SELECT_LEX_UNIT::set_explain_marker_from ( THD thd,
const SELECT_LEX_UNIT u 
)

◆ set_limit()

bool SELECT_LEX_UNIT::set_limit ( THD thd,
SELECT_LEX provider 
)

Set limit and offset for query expression object.

Parameters
thdthread handler
providerSELECT_LEX to get offset and limit from.
Returns
false if success, true if error

◆ set_optimized()

void SELECT_LEX_UNIT::set_optimized ( )
inline

◆ set_prepared()

void SELECT_LEX_UNIT::set_prepared ( )
inline

◆ set_query_result()

void SELECT_LEX_UNIT::set_query_result ( Query_result res)
inline

Set new query result object for this query expression.

◆ unclean()

void SELECT_LEX_UNIT::unclean ( )
inline

◆ union_needs_tmp_table()

bool SELECT_LEX_UNIT::union_needs_tmp_table ( LEX lex)

Decide if a temporary table is needed for the UNION.

Return values
trueA temporary table is needed.
falseA temporary table is not needed.

◆ walk()

bool SELECT_LEX_UNIT::walk ( Item_processor  processor,
enum_walk  walk,
uchar arg 
)

Friends And Related Function Documentation

◆ parse_view_definition

bool parse_view_definition ( THD thd,
TABLE_LIST view_ref 
)
friend

Parse a view definition.

Among other effects, it adds underlying tables to the global list of tables, so the next iteration in open_tables() will open them.

Parameters
[in]thdThread handler
[in,out]view_refTABLE_LIST structure for view reference
Returns
false-in case of success, true-in case of error.
Note
In case true value returned an error has been already set in DA.

◆ SELECT_LEX

friend class SELECT_LEX
friend

Member Data Documentation

◆ cleaned

enum_clean_state SELECT_LEX_UNIT::cleaned

cleanliness state

◆ derived_table

TABLE_LIST* SELECT_LEX_UNIT::derived_table

If this query expression is underlying of a derived table, the derived table.

NULL if none.

◆ executed

bool SELECT_LEX_UNIT::executed
private

Query expression has been executed.

◆ explain_marker

enum_parsing_context SELECT_LEX_UNIT::explain_marker
private


Marker for subqueries in WHERE, HAVING, ORDER BY, GROUP BY and SELECT item lists.

Must be read/written when holding LOCK_query_plan.

See Item_subselect::explain_subquery_checker

◆ fake_select_lex

SELECT_LEX* SELECT_LEX_UNIT::fake_select_lex

Helper query block for query expression with UNION or multi-level ORDER BY/LIMIT.

◆ first_recursive

SELECT_LEX* SELECT_LEX_UNIT::first_recursive

First query block (in this UNION) which references the CTE.

NULL if not the query expression of a recursive CTE.

◆ got_all_recursive_rows

bool SELECT_LEX_UNIT::got_all_recursive_rows

True if the with-recursive algorithm has produced the complete result.

In a recursive CTE, a JOIN is executed several times in a loop, and should not be cleaned up (e.g. by join_free()) before all iterations of the loop are done (i.e. before the CTE's result is complete).

◆ item

Item_subselect* SELECT_LEX_UNIT::item

Points to subquery if this query expression is used in one, otherwise NULL.

◆ item_list

List<Item> SELECT_LEX_UNIT::item_list

◆ m_lateral_deps

table_map SELECT_LEX_UNIT::m_lateral_deps

If 'this' is body of lateral derived table: map of tables in the same FROM clause as this derived table, and to which the derived table's body makes references.

In pre-resolution stages, this is OUTER_REF_TABLE_BIT, just to indicate that this has LATERAL; after resolution, which has found references in the body, this is the proper map (with no PSEUDO_TABLE_BITS anymore).

◆ m_query_result

Query_result* SELECT_LEX_UNIT::m_query_result
private

Object to which the result for this query expression is sent.

◆ m_with_clause

PT_with_clause* SELECT_LEX_UNIT::m_with_clause

The WITH clause which is the first part of this query expression.

NULL if none.

◆ master

SELECT_LEX* SELECT_LEX_UNIT::master
private

The query block wherein this query expression is contained, NULL if the query block is the outer-most one.

◆ next

SELECT_LEX_UNIT* SELECT_LEX_UNIT::next
private

Intrusive double-linked list of all query expressions immediately contained within the same query block.

◆ offset_limit_cnt

ha_rows SELECT_LEX_UNIT::offset_limit_cnt

◆ optimized

bool SELECT_LEX_UNIT::optimized
private

All query blocks in query expression are optimized.

◆ prepared

bool SELECT_LEX_UNIT::prepared
private

All query blocks in query expression are prepared.

◆ prev

SELECT_LEX_UNIT** SELECT_LEX_UNIT::prev
private

◆ result_table_list

TABLE_LIST SELECT_LEX_UNIT::result_table_list
private

◆ saved_fake_select_lex

SELECT_LEX* SELECT_LEX_UNIT::saved_fake_select_lex

SELECT_LEX that stores LIMIT and OFFSET for UNION ALL when no fake_select_lex is used.

◆ select_limit_cnt

ha_rows SELECT_LEX_UNIT::select_limit_cnt

◆ slave

SELECT_LEX* SELECT_LEX_UNIT::slave
private

The first query block in this query expression.

◆ table

TABLE* SELECT_LEX_UNIT::table
private

◆ types

List<Item> SELECT_LEX_UNIT::types

◆ uncacheable

uint8 SELECT_LEX_UNIT::uncacheable

result of this query can't be cached, bit field, can be : UNCACHEABLE_DEPENDENT UNCACHEABLE_RAND UNCACHEABLE_SIDEEFFECT

◆ union_distinct

SELECT_LEX* SELECT_LEX_UNIT::union_distinct

Points to last query block which has UNION DISTINCT on its left.

In a list of UNIONed blocks, UNION is left-associative; so UNION DISTINCT eliminates duplicates in all blocks up to the first one on its right included. Which is why we only need to remember that query block.

◆ union_result

Query_result_union* SELECT_LEX_UNIT::union_result
private

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