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