MySQL  8.0.27
Source Code Documentation
sql_derived.h
Go to the documentation of this file.
1 /* Copyright (c) 2006, 2021, 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"
27 #include "sql/opt_trace_context.h"
28 #include "sql/sql_const.h"
29 struct NESTED_JOIN;
30 /**
31  Class which handles pushing conditions down to a materialized derived
32  table. In Query_block::prepare, if it is the outermost query block, and
33  if we are at the end of preparation, a WHERE condition from the query
34  block is checked to see if it can be pushed to the materialized derived
35  table.
36  Query_block::prepare
37  push_conditions_to_derived_tables()
38  For every materialized derived table,
39  If there is a where condition in this query block,
40  Make condition that can be pushed down to the derived table.
41  Extract a part of the condition that has columns belonging to only
42  this derived table.
43  Check if this condition can be pushed past window functions if any to
44  the HAVING clause of the derived table
45  Make a condition that could not be pushed past. This will remain in
46  the outer query block.
47  Check if this condition can be pushed past group by if present to the
48  WHERE clause of the derived table.
49  Make a condition that could not be pushed past. This will be part of
50  the HAVING clause of the derived table query.
51  Get the remainder condition which could not be pushed to the derived
52  the derived table.
53  Push the condition down to derived table's query expression
54  REPEAT THE ABOVE for the rest of the derived tables.
55  For every query expression inside the current query block
56  REPEAT THE ABOVE to keep pushing as far down as possible.
57 */
58 
60  public:
61  Condition_pushdown(Item *cond, TABLE_LIST *derived, THD *thd_arg,
62  Opt_trace_context *trace_arg)
63  : m_cond_to_check(cond),
64  m_derived_table(derived),
65  thd(thd_arg),
66  trace(trace_arg) {}
67 
68  bool make_cond_for_derived();
71 
72  private:
74  bool replace_columns_in_cond(Item **cond, bool is_having);
76  void push_past_group_by();
77  bool attach_cond_to_derived(Item *derived_cond, Item *cond_to_attach,
78  bool having);
79  void update_between_count(Item *cond);
80  void check_and_remove_sj_exprs(Item *cond);
81  void remove_sj_exprs(Item *cond, NESTED_JOIN *sj_nest);
82 
83  /// Condition that needs to be checked to push down to the derived table.
85 
86  /// Derived table to push the condition to.
88 
89  /**
90  Condition that is extracted from outer WHERE condition to be pushed to
91  the derived table.
92  */
93  Item *m_cond_to_push{nullptr};
94 
95  /**
96  Condition that would be attached to the HAVING clause of the derived
97  table.
98  */
99  Item *m_having_cond{nullptr};
100 
101  /**
102  Condition that would be attached to the WHERE clause of the derived table.
103  */
104  Item *m_where_cond{nullptr};
105 
106  /**
107  Condition that would be left behind in the outer select. This is the
108  condition that could not be pushed down to the derived table.
109  */
111 
112  /**
113  Enum that represents various stages of checking.
114  CHECK_FOR_DERIVED - Checking if a condition has only derived table
115  expressions.
116  CHECK_FOR_HAVING - Checking if condition could be pushed to HAVING
117  clause of the derived table.
118  CHECK_FOR_WHERE - Checking if condition could be pushed to WHERE
119  clause of the derived table.
120  */
125  };
127 
128  /// Current thread
130 
131  /// Optimizer trace context
133 };
134 
135 #endif /* SQL_DERIVED_INCLUDED */
Class which handles pushing conditions down to a materialized derived table.
Definition: sql_derived.h:59
Item * m_having_cond
Condition that would be attached to the HAVING clause of the derived table.
Definition: sql_derived.h:99
enum_checking_purpose m_checking_purpose
Definition: sql_derived.h:126
Item * m_cond_to_check
Condition that needs to be checked to push down to the derived table.
Definition: sql_derived.h:84
Item * m_where_cond
Condition that would be attached to the WHERE clause of the derived table.
Definition: sql_derived.h:104
Item * m_cond_to_push
Condition that is extracted from outer WHERE condition to be pushed to the derived table.
Definition: sql_derived.h:93
Opt_trace_context * trace
Optimizer trace context.
Definition: sql_derived.h:132
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:1295
Item * make_remainder_cond(Item *cond)
Make the remainder condition.
Definition: sql_derived.cc:1113
void push_past_window_functions()
Try to push past window functions into the HAVING clause of the derived table.
Definition: sql_derived.cc:1053
Condition_pushdown(Item *cond, TABLE_LIST *derived, THD *thd_arg, Opt_trace_context *trace_arg)
Definition: sql_derived.h:61
bool make_cond_for_derived()
Make a condition that can be pushed down to the derived table, and push it.
Definition: sql_derived.cc:850
TABLE_LIST * m_derived_table
Derived table to push the condition to.
Definition: sql_derived.h:87
void update_between_count(Item *cond)
Increment between_count in the derived table query block based on the number of BETWEEN functions pus...
Definition: sql_derived.cc:1267
Item * extract_cond_for_table(Item *cond)
This function is called multiple times to extract parts of a condition.
Definition: sql_derived.cc:952
void check_and_remove_sj_exprs(Item *cond)
Check if this derived table is part of a semi-join.
Definition: sql_derived.cc:1199
THD * thd
Current thread.
Definition: sql_derived.h:129
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:1226
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:1153
Item * get_remainder_cond()
Definition: sql_derived.h:70
Item * m_remainder_cond
Condition that would be left behind in the outer select.
Definition: sql_derived.h:110
enum_checking_purpose
Enum that represents various stages of checking.
Definition: sql_derived.h:121
@ CHECK_FOR_WHERE
Definition: sql_derived.h:124
@ CHECK_FOR_DERIVED
Definition: sql_derived.h:122
@ CHECK_FOR_HAVING
Definition: sql_derived.h:123
void push_past_group_by()
Try to push the condition past GROUP BY into the WHERE clause of the derived table.
Definition: sql_derived.cc:1078
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
A per-session context which is always available at any point of execution, because in practice it's a...
Definition: opt_trace_context.h:89
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
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
Definition: table.h:2694