MySQL 8.4.0
Source Code Documentation
sql_derived.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_DERIVED_INCLUDED
25#define SQL_DERIVED_INCLUDED
26
27#include "sql/item.h"
29#include "sql/sql_const.h"
30#include "sql/sql_lex.h"
31struct NESTED_JOIN;
32/**
33 Class which handles pushing conditions down to a materialized derived
34 table. In Query_block::prepare, if it is the outermost query block, and
35 if we are at the end of preparation, a WHERE condition from the query
36 block is checked to see if it can be pushed to the materialized derived
37 table.
38 Query_block::prepare
39 push_conditions_to_derived_tables()
40 For every materialized derived table,
41 If there is a where condition in this query block,
42 Make condition that can be pushed down to the derived table.
43 Extract a part of the condition that has columns belonging to only
44 this derived table.
45 Check if this condition can be pushed past window functions if any to
46 the HAVING clause of the derived table.
47 Make a condition that could not be pushed past. This will remain in
48 the outer query block.
49 Check if this condition can be pushed past group by if present to the
50 WHERE clause of the derived table.
51 Make a condition that could not be pushed past. This will be part of
52 the HAVING clause of the derived table query.
53 Get the remainder condition which could not be pushed to the derived
54 table.
55 Push the condition down to derived table's query expression.
56 REPEAT THE ABOVE for the rest of the derived tables.
57 For every query expression inside the current query block
58 REPEAT THE ABOVE to keep pushing as far down as possible.
59*/
60
62 public:
63 Condition_pushdown(Item *cond, Table_ref *derived, THD *thd_arg,
64 Opt_trace_context *trace_arg)
65 : m_cond_to_check(cond),
66 m_derived_table(derived),
67 thd(thd_arg),
68 trace(trace_arg) {
69 Query_expression *derived_query_expression =
71 m_query_block = derived_query_expression->outer_query_block();
72 }
73
75 bool make_remainder_cond(Item *cond, Item **remainder_cond);
77
78 /// Used to pass information during condition pushdown.
80 public:
83 bool is_set_operation() const {
85 }
86
87 Derived_table_info(Table_ref *derived_table, Query_block *query_block)
88 : m_derived_table(derived_table), m_derived_query_block(query_block) {}
89 };
90
91 private:
93 bool replace_columns_in_cond(Item **cond, bool is_having);
95 bool push_past_group_by();
96 bool attach_cond_to_derived(Item *derived_cond, Item *cond_to_attach,
97 bool having);
98 void update_cond_count(Item *cond);
100 void remove_sj_exprs(Item *cond, NESTED_JOIN *sj_nest);
101
102 /// Condition that needs to be checked to push down to the derived table.
104
105 /// Derived table to push the condition to.
107
108 /**
109 Condition that is extracted from outer WHERE condition to be pushed to
110 the derived table. This will be a copy when a query expression has
111 multiple query blocks.
112 */
114
115 /**
116 set to m_cond_to_push before cloning (for query expressions with
117 multiple query blocks).
118 */
120
121 /**
122 Condition that would be attached to the HAVING clause of the derived
123 table. (For each query block in the derived table if UNIONS are present).
124 */
126
127 /**
128 Condition that would be attached to the WHERE clause of the derived table.
129 (For each query block in the derived table if UNIONS are present).
130 */
132
133 /**
134 Condition that would be left behind in the outer query block. This is the
135 condition that could not be pushed down to the derived table.
136 */
138
139 /**
140 Query block to which m_cond_to_push should be pushed.
141 */
143
144 /**
145 Enum that represents various stages of checking.
146 CHECK_FOR_DERIVED - Checking if a condition has only derived table
147 expressions.
148 CHECK_FOR_HAVING - Checking if condition could be pushed to HAVING
149 clause of the derived table.
150 CHECK_FOR_WHERE - Checking if condition could be pushed to WHERE
151 clause of the derived table.
152 */
157 };
159
160 /// Current thread
162
163 /// Optimizer trace context
165};
166
167#endif /* SQL_DERIVED_INCLUDED */
Used to pass information during condition pushdown.
Definition: sql_derived.h:79
Query_block * m_derived_query_block
Definition: sql_derived.h:82
Derived_table_info(Table_ref *derived_table, Query_block *query_block)
Definition: sql_derived.h:87
Table_ref * m_derived_table
Definition: sql_derived.h:81
bool is_set_operation() const
Definition: sql_derived.h:83
Class which handles pushing conditions down to a materialized derived table.
Definition: sql_derived.h:61
Item * m_having_cond
Condition that would be attached to the HAVING clause of the derived table.
Definition: sql_derived.h:125
enum_checking_purpose m_checking_purpose
Definition: sql_derived.h:158
Item * m_cond_to_check
Condition that needs to be checked to push down to the derived table.
Definition: sql_derived.h:103
Table_ref * m_derived_table
Derived table to push the condition to.
Definition: sql_derived.h:106
Item * m_where_cond
Condition that would be attached to the WHERE clause of the derived table.
Definition: sql_derived.h:131
bool push_past_group_by()
Try to push the condition or parts of the condition past GROUP BY into the WHERE clause of the derive...
Definition: sql_derived.cc:1363
Item * m_cond_to_push
Condition that is extracted from outer WHERE condition to be pushed to the derived table.
Definition: sql_derived.h:113
Opt_trace_context * trace
Optimizer trace context.
Definition: sql_derived.h:164
Item * m_orig_cond_to_push
set to m_cond_to_push before cloning (for query expressions with multiple query blocks).
Definition: sql_derived.h:119
bool attach_cond_to_derived(Item *derived_cond, Item *cond_to_attach, bool having)
Attach condition to derived table query block.
Definition: sql_derived.cc:1593
Item * get_remainder_cond()
Definition: sql_derived.h:76
bool make_remainder_cond(Item *cond, Item **remainder_cond)
Make the remainder condition.
Definition: sql_derived.cc:1399
bool make_cond_for_derived()
Make a condition that can be pushed down to the derived table, and push it.
Definition: sql_derived.cc:1095
Item * extract_cond_for_table(Item *cond)
This function is called multiple times to extract parts of a condition.
Definition: sql_derived.cc:1207
void check_and_remove_sj_exprs(Item *cond)
Check if this derived table is part of a semi-join.
Definition: sql_derived.cc:1495
THD * thd
Current thread.
Definition: sql_derived.h:161
void remove_sj_exprs(Item *cond, NESTED_JOIN *sj_nest)
This function examines the condition that is being pushed down to see if the expressions from the con...
Definition: sql_derived.cc:1522
bool replace_columns_in_cond(Item **cond, bool is_having)
Replace columns in a condition that will be pushed to this derived table with the derived table expre...
Definition: sql_derived.cc:1447
Condition_pushdown(Item *cond, Table_ref *derived, THD *thd_arg, Opt_trace_context *trace_arg)
Definition: sql_derived.h:63
Item * m_remainder_cond
Condition that would be left behind in the outer query block.
Definition: sql_derived.h:137
bool push_past_window_functions()
Try to push past window functions into the HAVING clause of the derived table.
Definition: sql_derived.cc:1326
enum_checking_purpose
Enum that represents various stages of checking.
Definition: sql_derived.h:153
@ CHECK_FOR_WHERE
Definition: sql_derived.h:156
@ CHECK_FOR_DERIVED
Definition: sql_derived.h:154
@ CHECK_FOR_HAVING
Definition: sql_derived.h:155
void update_cond_count(Item *cond)
Increment cond_count and between_count in the derived table query block based on the number of BETWEE...
Definition: sql_derived.cc:1564
Query_block * m_query_block
Query block to which m_cond_to_push should be pushed.
Definition: sql_derived.h:142
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:934
A per-session context which is always available at any point of execution, because in practice it's a...
Definition: opt_trace_context.h:93
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1163
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:626
Query_block * outer_query_block() const
Definition: sql_lex.h:856
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: table.h:2863
Query_expression * derived_query_expression() const
Return the query expression of a derived table or view.
Definition: table.h:3337
bool is_set_operation() const
Definition: sql_lex.h:2489
This contains the declaration of class Opt_trace_context, which is needed to declare THD.
File containing constants that can be used throughout the server.
Struct NESTED_JOIN is used to represent how tables are connected through outer join operations and se...
Definition: nested_join.h:78