MySQL 8.0.32
Source Code Documentation
parse_tree_node_base.h
Go to the documentation of this file.
1/* Copyright (c) 2013, 2022, 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 PARSE_TREE_NODE_BASE_INCLUDED
24#define PARSE_TREE_NODE_BASE_INCLUDED
25
26#include <assert.h>
27#include <cstdarg>
28#include <cstdlib>
29#include <new>
30#include <queue>
31
32#include "memory_debugging.h"
33#include "my_alloc.h"
34#include "my_compiler.h"
35
36#include "mem_root_deque.h"
37#include "my_inttypes.h" // TODO: replace with cstdint
38#include "sql/check_stack.h"
39#include "sql/parse_location.h"
40#include "sql/sql_const.h"
41
42class Query_block;
43class THD;
44
45// uncachable cause
46#define UNCACHEABLE_DEPENDENT 1
47#define UNCACHEABLE_RAND 2
48#define UNCACHEABLE_SIDEEFFECT 4
49/* For uncorrelated SELECT in an UNION with some correlated SELECTs */
50#define UNCACHEABLE_UNITED 8
51#define UNCACHEABLE_CHECKOPTION 16
52
53/**
54 Names for different query parse tree parts
55*/
56
58 CTX_NONE = 0, ///< Empty value
59 CTX_MESSAGE, ///< "No tables used" messages etc.
60 CTX_TABLE, ///< for single-table UPDATE/DELETE/INSERT/REPLACE
61 CTX_SELECT_LIST, ///< SELECT (subquery), (subquery)...
62 CTX_UPDATE_VALUE, ///< UPDATE ... SET field=(subquery)...
63 CTX_INSERT_VALUES, ///< INSERT ... VALUES
64 CTX_INSERT_UPDATE, ///< INSERT ... ON DUPLICATE KEY UPDATE ...
69 CTX_DERIVED, ///< "Derived" subquery
70 CTX_WHERE, ///< Subquery in WHERE clause item tree
71 CTX_ON, ///< ON clause context
72 CTX_WINDOW, ///< Named or unnamed window
73 CTX_HAVING, ///< Subquery in HAVING clause item tree
74 CTX_ORDER_BY, ///< ORDER BY clause execution context
75 CTX_GROUP_BY, ///< GROUP BY clause execution context
76 CTX_SIMPLE_ORDER_BY, ///< ORDER BY clause execution context
77 CTX_SIMPLE_GROUP_BY, ///< GROUP BY clause execution context
78 CTX_DISTINCT, ///< DISTINCT clause execution context
79 CTX_SIMPLE_DISTINCT, ///< DISTINCT clause execution context
80 CTX_BUFFER_RESULT, ///< see SQL_BUFFER_RESULT in the manual
81 CTX_ORDER_BY_SQ, ///< Subquery in ORDER BY clause item tree
82 CTX_GROUP_BY_SQ, ///< Subquery in GROUP BY clause item tree
83 CTX_OPTIMIZED_AWAY_SUBQUERY, ///< Subquery executed once during optimization
85 CTX_UNION_RESULT, ///< Pseudo-table context for UNION result
87 CTX_INTERSECT_RESULT, ///< Pseudo-table context
89 CTX_EXCEPT_RESULT, ///< Pseudo-table context
91 CTX_UNARY_RESULT, ///< Pseudo-table context
92 CTX_QUERY_SPEC ///< Inner SELECTs of UNION expression
93};
94
95class Query_term;
109
113 bool m_has_order{false};
114 QueryLevel(MEM_ROOT *mem_root, Surrounding_context sc, bool has_order = false)
115 : m_type(sc), m_elts(mem_root), m_has_order(has_order) {}
116};
117/**
118 Environment data for the contextualization phase
119*/
121 THD *const thd; ///< Current thread handler
122 MEM_ROOT *mem_root; ///< Current MEM_ROOT
123 Query_block *select; ///< Current Query_block object
124 mem_root_deque<QueryLevel> m_stack; ///< Aids query term tree construction
125 /// Call upon parse completion.
126 /// @returns true on error, else false
130 Surrounding_context op); ///< Determine if there is anything but
131 ///< UNION ALL above in m_stack
132};
133
134/**
135 Base class for parse tree nodes (excluding the Parse_tree_root hierarchy)
136*/
137template <typename Context>
139 friend class Item; // for direct access to the "contextualized" field
140
142 void operator=(const Parse_tree_node_tmpl &); // undefined
143
144#ifndef NDEBUG
145 private:
146 bool contextualized; // true if the node object is contextualized
147#endif // NDEBUG
148
149 public:
150 typedef Context context_t;
151
152 static void *operator new(size_t size, MEM_ROOT *mem_root,
153 const std::nothrow_t &arg
154 [[maybe_unused]] = std::nothrow) noexcept {
155 return mem_root->Alloc(size);
156 }
157 static void operator delete(void *ptr [[maybe_unused]],
158 size_t size [[maybe_unused]]) {
159 TRASH(ptr, size);
160 }
161 static void operator delete(void *, MEM_ROOT *,
162 const std::nothrow_t &) noexcept {}
163
164 protected:
166#ifndef NDEBUG
167 contextualized = false;
168#endif // NDEBUG
169 }
170
171 public:
172 virtual ~Parse_tree_node_tmpl() = default;
173
174#ifndef NDEBUG
175 bool is_contextualized() const { return contextualized; }
176#endif // NDEBUG
177
178 /**
179 Do all context-sensitive things and mark the node as contextualized
180
181 @param pc current parse context
182
183 @retval false success
184 @retval true syntax/OOM/etc error
185 */
186 virtual bool contextualize(Context *pc) {
187 uchar dummy;
188 if (check_stack_overrun(pc->thd, STACK_MIN_SIZE, &dummy)) return true;
189
190#ifndef NDEBUG
191 assert(!contextualized);
192 contextualized = true;
193#endif // NDEBUG
194
195 return false;
196 }
197
198 /**
199 syntax_error() function replacement for deferred reporting of syntax
200 errors
201
202 @param pc Current parse context.
203 @param pos Location of the error in lexical scanner buffers.
204 */
205 void error(Context *pc, const POS &pos) const {
206 pc->thd->syntax_error_at(pos);
207 }
208
209 /**
210 syntax_error() function replacement for deferred reporting of syntax
211 errors
212
213 @param pc Current parse context.
214 @param pos Location of the error in lexical scanner buffers.
215 @param msg Error message.
216 */
217 void error(Context *pc, const POS &pos, const char *msg) const {
218 pc->thd->syntax_error_at(pos, "%s", msg);
219 }
220
221 /**
222 syntax_error() function replacement for deferred reporting of syntax
223 errors
224
225 @param pc Current parse context.
226 @param pos Location of the error in lexical scanner buffers.
227 @param format Error message format string with optional argument list.
228 */
229 void errorf(Context *pc, const POS &pos, const char *format, ...) const
230 MY_ATTRIBUTE((format(printf, 4, 5)));
231};
232
233template <typename Context>
234inline void Parse_tree_node_tmpl<Context>::errorf(Context *pc, const POS &pos,
235 const char *format,
236 ...) const {
237 va_list args;
238 va_start(args, format);
239 pc->thd->vsyntax_error_at(pos, format, args);
240 va_end(args);
241}
242
244
245#endif /* PARSE_TREE_NODE_BASE_INCLUDED */
bool check_stack_overrun(const THD *thd, long margin, unsigned char *buf)
Check stack for a overrun.
Definition: check_stack.cc:74
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:850
Base class for parse tree nodes (excluding the Parse_tree_root hierarchy)
Definition: parse_tree_node_base.h:138
Parse_tree_node_tmpl(const Parse_tree_node_tmpl &)
Context context_t
Definition: parse_tree_node_base.h:150
virtual bool contextualize(Context *pc)
Do all context-sensitive things and mark the node as contextualized.
Definition: parse_tree_node_base.h:186
bool is_contextualized() const
Definition: parse_tree_node_base.h:175
void operator=(const Parse_tree_node_tmpl &)
virtual ~Parse_tree_node_tmpl()=default
Parse_tree_node_tmpl()
Definition: parse_tree_node_base.h:165
void error(Context *pc, const POS &pos) const
syntax_error() function replacement for deferred reporting of syntax errors
Definition: parse_tree_node_base.h:205
void error(Context *pc, const POS &pos, const char *msg) const
syntax_error() function replacement for deferred reporting of syntax errors
Definition: parse_tree_node_base.h:217
bool contextualized
Definition: parse_tree_node_base.h:146
void errorf(Context *pc, const POS &pos, const char *format,...) const
syntax_error() function replacement for deferred reporting of syntax errors
Definition: parse_tree_node_base.h:234
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1153
Query term tree structure.
Definition: query_term.h:208
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:33
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:109
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
Various macros useful for communicating with memory debuggers, such as Valgrind.
void TRASH(void *ptr, size_t length)
Put bad content in memory to be sure it will segfault if dereferenced.
Definition: memory_debugging.h:70
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
Header for compiler-dependent features.
Some integer typedefs for easier portability.
unsigned char uchar
Definition: my_inttypes.h:51
enum_parsing_context
Names for different query parse tree parts.
Definition: parse_tree_node_base.h:57
@ CTX_QEP_TAB
Definition: parse_tree_node_base.h:66
@ CTX_SIMPLE_DISTINCT
DISTINCT clause execution context.
Definition: parse_tree_node_base.h:79
@ CTX_ORDER_BY
ORDER BY clause execution context.
Definition: parse_tree_node_base.h:74
@ CTX_HAVING
Subquery in HAVING clause item tree.
Definition: parse_tree_node_base.h:73
@ CTX_UNARY_RESULT
Pseudo-table context.
Definition: parse_tree_node_base.h:91
@ CTX_BUFFER_RESULT
see SQL_BUFFER_RESULT in the manual
Definition: parse_tree_node_base.h:80
@ CTX_MESSAGE
"No tables used" messages etc.
Definition: parse_tree_node_base.h:59
@ CTX_UNION_RESULT
Pseudo-table context for UNION result.
Definition: parse_tree_node_base.h:85
@ CTX_DERIVED
"Derived" subquery
Definition: parse_tree_node_base.h:69
@ CTX_EXCEPT
Definition: parse_tree_node_base.h:88
@ CTX_SELECT_LIST
SELECT (subquery), (subquery)...
Definition: parse_tree_node_base.h:61
@ CTX_ORDER_BY_SQ
Subquery in ORDER BY clause item tree.
Definition: parse_tree_node_base.h:81
@ CTX_UNARY
Definition: parse_tree_node_base.h:90
@ CTX_SIMPLE_ORDER_BY
ORDER BY clause execution context.
Definition: parse_tree_node_base.h:76
@ CTX_SIMPLE_GROUP_BY
GROUP BY clause execution context.
Definition: parse_tree_node_base.h:77
@ CTX_INTERSECT
Definition: parse_tree_node_base.h:86
@ CTX_INSERT_UPDATE
INSERT ... ON DUPLICATE KEY UPDATE ...
Definition: parse_tree_node_base.h:64
@ CTX_GROUP_BY_SQ
Subquery in GROUP BY clause item tree.
Definition: parse_tree_node_base.h:82
@ CTX_INTERSECT_RESULT
Pseudo-table context.
Definition: parse_tree_node_base.h:87
@ CTX_MATERIALIZATION
Definition: parse_tree_node_base.h:67
@ CTX_NONE
Empty value.
Definition: parse_tree_node_base.h:58
@ CTX_DISTINCT
DISTINCT clause execution context.
Definition: parse_tree_node_base.h:78
@ CTX_UPDATE_VALUE
UPDATE ... SET field=(subquery)...
Definition: parse_tree_node_base.h:62
@ CTX_DUPLICATES_WEEDOUT
Definition: parse_tree_node_base.h:68
@ CTX_JOIN
Definition: parse_tree_node_base.h:65
@ CTX_OPTIMIZED_AWAY_SUBQUERY
Subquery executed once during optimization.
Definition: parse_tree_node_base.h:83
@ CTX_GROUP_BY
GROUP BY clause execution context.
Definition: parse_tree_node_base.h:75
@ CTX_QUERY_SPEC
Inner SELECTs of UNION expression.
Definition: parse_tree_node_base.h:92
@ CTX_TABLE
for single-table UPDATE/DELETE/INSERT/REPLACE
Definition: parse_tree_node_base.h:60
@ CTX_INSERT_VALUES
INSERT ... VALUES.
Definition: parse_tree_node_base.h:63
@ CTX_WHERE
Subquery in WHERE clause item tree.
Definition: parse_tree_node_base.h:70
@ CTX_WINDOW
Named or unnamed window.
Definition: parse_tree_node_base.h:72
@ CTX_ON
ON clause context.
Definition: parse_tree_node_base.h:71
@ CTX_UNION
Definition: parse_tree_node_base.h:84
@ CTX_EXCEPT_RESULT
Pseudo-table context.
Definition: parse_tree_node_base.h:89
Surrounding_context
Definition: parse_tree_node_base.h:96
@ SC_SUBQUERY
Definition: parse_tree_node_base.h:101
@ SC_QUERY_EXPRESSION
Definition: parse_tree_node_base.h:100
@ SC_EXCEPT_ALL
Definition: parse_tree_node_base.h:107
@ SC_TABLE_VALUE_CONSTRUCTOR
Definition: parse_tree_node_base.h:99
@ SC_TOP
Definition: parse_tree_node_base.h:97
@ SC_INTERSECT_ALL
Definition: parse_tree_node_base.h:105
@ SC_QUERY_SPECIFICATION
Definition: parse_tree_node_base.h:98
@ SC_EXCEPT_DISTINCT
Definition: parse_tree_node_base.h:106
@ SC_UNION_DISTINCT
Definition: parse_tree_node_base.h:102
@ SC_UNION_ALL
Definition: parse_tree_node_base.h:103
@ SC_INTERSECT_DISTINCT
Definition: parse_tree_node_base.h:104
Parse_tree_node_tmpl< Parse_context > Parse_tree_node
Definition: parse_tree_node_base.h:243
File containing constants that can be used throughout the server.
constexpr const long STACK_MIN_SIZE
Stack reservation.
Definition: sql_const.h:142
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:82
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:144
Environment data for the contextualization phase.
Definition: parse_tree_node_base.h:120
Query_block * select
Current Query_block object.
Definition: parse_tree_node_base.h:123
Parse_context(THD *thd, Query_block *sl)
Definition: parse_tree_node_base.cc:28
bool is_top_level_union_all(Surrounding_context op)
Determine if there is anything but UNION ALL above in m_stack.
Definition: parse_tree_node_base.cc:52
bool finalize_query_expression()
Call upon parse completion.
Definition: parse_tree_node_base.cc:41
THD *const thd
Current thread handler.
Definition: parse_tree_node_base.h:121
MEM_ROOT * mem_root
Current MEM_ROOT.
Definition: parse_tree_node_base.h:122
mem_root_deque< QueryLevel > m_stack
Aids query term tree construction.
Definition: parse_tree_node_base.h:124
Definition: parse_tree_node_base.h:110
QueryLevel(MEM_ROOT *mem_root, Surrounding_context sc, bool has_order=false)
Definition: parse_tree_node_base.h:114
mem_root_deque< Query_term * > m_elts
Definition: parse_tree_node_base.h:112
bool m_has_order
Definition: parse_tree_node_base.h:113
Surrounding_context m_type
Definition: parse_tree_node_base.h:111
Bison "location" class.
Definition: parse_location.h:42