MySQL 8.3.0
Source Code Documentation
sql_derived.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23#ifndef SQL_DERIVED_INCLUDED
24#define SQL_DERIVED_INCLUDED
25
26#include "sql/item.h"
28#include "sql/sql_const.h"
29#include "sql/sql_lex.h"
30struct NESTED_JOIN;
31/**
32 Class which handles pushing conditions down to a materialized derived
33 table. In Query_block::prepare, if it is the outermost query block, and
34 if we are at the end of preparation, a WHERE condition from the query
35 block is checked to see if it can be pushed to the materialized derived
36 table.
37 Query_block::prepare
38 push_conditions_to_derived_tables()
39 For every materialized derived table,
40 If there is a where condition in this query block,
41 Make condition that can be pushed down to the derived table.
42 Extract a part of the condition that has columns belonging to only
43 this derived table.
44 Check if this condition can be pushed past window functions if any to
45 the HAVING clause of the derived table.
46 Make a condition that could not be pushed past. This will remain in
47 the outer query block.
48 Check if this condition can be pushed past group by if present to the
49 WHERE clause of the derived table.
50 Make a condition that could not be pushed past. This will be part of
51 the HAVING clause of the derived table query.
52 Get the remainder condition which could not be pushed to the derived
53 table.
54 Push the condition down to derived table's query expression.
55 REPEAT THE ABOVE for the rest of the derived tables.
56 For every query expression inside the current query block
57 REPEAT THE ABOVE to keep pushing as far down as possible.
58*/
59
61 public:
62 Condition_pushdown(Item *cond, Table_ref *derived, THD *thd_arg,
63 Opt_trace_context *trace_arg)
64 : m_cond_to_check(cond),
65 m_derived_table(derived),
66 thd(thd_arg),
67 trace(trace_arg) {
68 Query_expression *derived_query_expression =
70 m_query_block = derived_query_expression->outer_query_block();
71 }
72
74 bool make_remainder_cond(Item *cond, Item **remainder_cond);
76
77 /// Used to pass information during condition pushdown.
79 public:
82 bool is_set_operation() const {
84 }
85
86 Derived_table_info(Table_ref *derived_table, Query_block *query_block)
87 : m_derived_table(derived_table), m_derived_query_block(query_block) {}
88 };
89
90 private:
92 bool replace_columns_in_cond(Item **cond, bool is_having);
94 bool push_past_group_by();
95 bool attach_cond_to_derived(Item *derived_cond, Item *cond_to_attach,
96 bool having);
97 void update_cond_count(Item *cond);
99 void remove_sj_exprs(Item *cond, NESTED_JOIN *sj_nest);
100
101 /// Condition that needs to be checked to push down to the derived table.
103
104 /// Derived table to push the condition to.
106
107 /**
108 Condition that is extracted from outer WHERE condition to be pushed to
109 the derived table. This will be a copy when a query expression has
110 multiple query blocks.
111 */
113
114 /**
115 set to m_cond_to_push before cloning (for query expressions with
116 multiple query blocks).
117 */
119
120 /**
121 Condition that would be attached to the HAVING clause of the derived
122 table. (For each query block in the derived table if UNIONS are present).
123 */
125
126 /**
127 Condition that would be attached to the WHERE clause of the derived table.
128 (For each query block in the derived table if UNIONS are present).
129 */
131
132 /**
133 Condition that would be left behind in the outer query block. This is the
134 condition that could not be pushed down to the derived table.
135 */
137
138 /**
139 Query block to which m_cond_to_push should be pushed.
140 */
142
143 /**
144 Enum that represents various stages of checking.
145 CHECK_FOR_DERIVED - Checking if a condition has only derived table
146 expressions.
147 CHECK_FOR_HAVING - Checking if condition could be pushed to HAVING
148 clause of the derived table.
149 CHECK_FOR_WHERE - Checking if condition could be pushed to WHERE
150 clause of the derived table.
151 */
156 };
158
159 /// Current thread
161
162 /// Optimizer trace context
164};
165
166#endif /* SQL_DERIVED_INCLUDED */
Used to pass information during condition pushdown.
Definition: sql_derived.h:78
Query_block * m_derived_query_block
Definition: sql_derived.h:81
Derived_table_info(Table_ref *derived_table, Query_block *query_block)
Definition: sql_derived.h:86
Table_ref * m_derived_table
Definition: sql_derived.h:80
bool is_set_operation() const
Definition: sql_derived.h:82
Class which handles pushing conditions down to a materialized derived table.
Definition: sql_derived.h:60
Item * m_having_cond
Condition that would be attached to the HAVING clause of the derived table.
Definition: sql_derived.h:124
enum_checking_purpose m_checking_purpose
Definition: sql_derived.h:157
Item * m_cond_to_check
Condition that needs to be checked to push down to the derived table.
Definition: sql_derived.h:102
Table_ref * m_derived_table
Derived table to push the condition to.
Definition: sql_derived.h:105
Item * m_where_cond
Condition that would be attached to the WHERE clause of the derived table.
Definition: sql_derived.h:130
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:1332
Item * m_cond_to_push
Condition that is extracted from outer WHERE condition to be pushed to the derived table.
Definition: sql_derived.h:112
Opt_trace_context * trace
Optimizer trace context.
Definition: sql_derived.h:163
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:118
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:1562
Item * get_remainder_cond()
Definition: sql_derived.h:75
bool make_remainder_cond(Item *cond, Item **remainder_cond)
Make the remainder condition.
Definition: sql_derived.cc:1368
bool make_cond_for_derived()
Make a condition that can be pushed down to the derived table, and push it.
Definition: sql_derived.cc:1064
Item * extract_cond_for_table(Item *cond)
This function is called multiple times to extract parts of a condition.
Definition: sql_derived.cc:1176
void check_and_remove_sj_exprs(Item *cond)
Check if this derived table is part of a semi-join.
Definition: sql_derived.cc:1464
THD * thd
Current thread.
Definition: sql_derived.h:160
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:1491
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:1416
Condition_pushdown(Item *cond, Table_ref *derived, THD *thd_arg, Opt_trace_context *trace_arg)
Definition: sql_derived.h:62
Item * m_remainder_cond
Condition that would be left behind in the outer query block.
Definition: sql_derived.h:136
bool push_past_window_functions()
Try to push past window functions into the HAVING clause of the derived table.
Definition: sql_derived.cc:1295
enum_checking_purpose
Enum that represents various stages of checking.
Definition: sql_derived.h:152
@ CHECK_FOR_WHERE
Definition: sql_derived.h:155
@ CHECK_FOR_DERIVED
Definition: sql_derived.h:153
@ CHECK_FOR_HAVING
Definition: sql_derived.h:154
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:1533
Query_block * m_query_block
Query block to which m_cond_to_push should be pushed.
Definition: sql_derived.h:141
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:933
A per-session context which is always available at any point of execution, because in practice it's a...
Definition: opt_trace_context.h:91
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1161
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:624
Query_block * outer_query_block() const
Definition: sql_lex.h:854
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:35
Definition: table.h:2853
Query_expression * derived_query_expression() const
Return the query expression of a derived table or view.
Definition: table.h:3327
bool is_set_operation() const
Definition: sql_lex.h:2458
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:77