MySQL  8.0.17
Source Code Documentation
parse_tree_node_base.h
Go to the documentation of this file.
1 /* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
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 <stdarg.h>
27 #include <cstdlib>
28 #include <new>
29 
30 #include "memory_debugging.h"
31 #include "my_compiler.h"
32 #include "my_dbug.h"
33 #include "my_inttypes.h"
34 #include "sql/check_stack.h"
35 #include "sql/mem_root_array.h"
36 #include "sql/parse_location.h"
37 #include "sql/sql_const.h"
38 
39 class SELECT_LEX;
40 class THD;
41 struct MEM_ROOT;
42 
43 // uncachable cause
44 #define UNCACHEABLE_DEPENDENT 1
45 #define UNCACHEABLE_RAND 2
46 #define UNCACHEABLE_SIDEEFFECT 4
47 /* For uncorrelated SELECT in an UNION with some correlated SELECTs */
48 #define UNCACHEABLE_UNITED 8
49 #define UNCACHEABLE_CHECKOPTION 16
50 
51 /**
52  Names for different query parse tree parts
53 */
54 
56  CTX_NONE = 0, ///< Empty value
57  CTX_MESSAGE, ///< "No tables used" messages etc.
58  CTX_TABLE, ///< for single-table UPDATE/DELETE/INSERT/REPLACE
59  CTX_SELECT_LIST, ///< SELECT (subquery), (subquery)...
60  CTX_UPDATE_VALUE, ///< UPDATE ... SET field=(subquery)...
61  CTX_INSERT_VALUES, ///< INSERT ... VALUES
62  CTX_INSERT_UPDATE, ///< INSERT ... ON DUPLICATE KEY UPDATE ...
67  CTX_DERIVED, ///< "Derived" subquery
68  CTX_WHERE, ///< Subquery in WHERE clause item tree
69  CTX_ON, ///< ON clause context
70  CTX_WINDOW, ///< Named or unnamed window
71  CTX_HAVING, ///< Subquery in HAVING clause item tree
72  CTX_ORDER_BY, ///< ORDER BY clause execution context
73  CTX_GROUP_BY, ///< GROUP BY clause execution context
74  CTX_SIMPLE_ORDER_BY, ///< ORDER BY clause execution context
75  CTX_SIMPLE_GROUP_BY, ///< GROUP BY clause execution context
76  CTX_DISTINCT, ///< DISTINCT clause execution context
77  CTX_SIMPLE_DISTINCT, ///< DISTINCT clause execution context
78  CTX_BUFFER_RESULT, ///< see SQL_BUFFER_RESULT in the manual
79  CTX_ORDER_BY_SQ, ///< Subquery in ORDER BY clause item tree
80  CTX_GROUP_BY_SQ, ///< Subquery in GROUP BY clause item tree
81  CTX_OPTIMIZED_AWAY_SUBQUERY, ///< Subquery executed once during optimization
83  CTX_UNION_RESULT, ///< Pseudo-table context for UNION result
84  CTX_QUERY_SPEC ///< Inner SELECTs of UNION expression
85 };
86 
87 /*
88  Note: YYLTYPE doesn't overload a default constructor (as well an underlying
89  Symbol_location).
90  OTOH if we need a zero-initialized POS, YYLTYPE or Symbol_location object,
91  we can simply call POS(), YYLTYPE() or Symbol_location(): C++ does
92  value-initialization in that case.
93 */
94 typedef YYLTYPE POS;
95 
96 /**
97  Environment data for the contextualization phase
98 */
99 struct Parse_context {
100  THD *const thd; ///< Current thread handler
101  MEM_ROOT *mem_root; ///< Current MEM_ROOT
102  SELECT_LEX *select; ///< Current SELECT_LEX object
103 
105 };
106 
107 /**
108  Base class for parse tree nodes (excluding the Parse_tree_root hierarchy)
109 */
110 template <typename Context>
112  friend class Item; // for direct access to the "contextualized" field
113 
114  Parse_tree_node_tmpl(const Parse_tree_node_tmpl &); // undefined
115  void operator=(const Parse_tree_node_tmpl &); // undefined
116 
117 #ifndef DBUG_OFF
118  private:
119  bool contextualized; // true if the node object is contextualized
120  bool transitional; // TODO: remove that after parser refactoring
121 #endif // DBUG_OFF
122 
123  public:
124  typedef Context context_t;
125 
126  static void *operator new(size_t size, MEM_ROOT *mem_root,
127  const std::nothrow_t &arg MY_ATTRIBUTE((unused)) =
128  std::nothrow) noexcept {
129  return mem_root->Alloc(size);
130  }
131  static void operator delete(void *ptr MY_ATTRIBUTE((unused)),
132  size_t size MY_ATTRIBUTE((unused))) {
133  TRASH(ptr, size);
134  }
135  static void operator delete(void *, MEM_ROOT *,
136  const std::nothrow_t &)noexcept {}
137 
138  protected:
140 #ifndef DBUG_OFF
141  contextualized = false;
142  transitional = false;
143 #endif // DBUG_OFF
144  }
145 
146  public:
148 
149 #ifndef DBUG_OFF
150  bool is_contextualized() const { return contextualized; }
151 #endif // DBUG_OFF
152 
153  /**
154  Do all context-sensitive things and mark the node as contextualized
155 
156  @param pc current parse context
157 
158  @retval false success
159  @retval true syntax/OOM/etc error
160  */
161  virtual bool contextualize(Context *pc) {
162 #ifndef DBUG_OFF
163  if (transitional) {
165  return false;
166  }
167 #endif // DBUG_OFF
168 
169  uchar dummy;
170  if (check_stack_overrun(pc->thd, STACK_MIN_SIZE, &dummy)) return true;
171 
172 #ifndef DBUG_OFF
174  contextualized = true;
175 #endif // DBUG_OFF
176 
177  return false;
178  }
179 
180  /**
181  Intermediate version of the contextualize() function
182 
183  This function is intended to resolve parser grammar loops.
184 
185  During the step-by-step refactoring of the parser grammar we wrap
186  each context-sensitive semantic action with 3 calls:
187  1. Parse_tree_node_tmpl() context-independent constructor call,
188  2. contextualize_() function call to evaluate all context-sensitive things
189  from the former context-sensitive semantic action code.
190  3. Call of dummy contextualize() function.
191 
192  Then we lift the contextualize() function call to outer grammar rules but
193  save the contextualize_() function call untouched.
194 
195  When all loops in the grammar rules are resolved (i.e. transformed
196  as described above) we:
197  a. remove all contextualize_() function calls and
198  b. rename all contextualize_() function definitions to contextualize()
199  function definitions.
200 
201  Note: it's not necessary to transform the whole grammar and remove
202  this function calls in one pass: it's possible to transform the
203  grammar statement by statement in a way described above.
204 
205  Note: remove this function together with Item::contextualize_().
206  */
207  virtual bool contextualize_(Context *) {
208 #ifndef DBUG_OFF
210  transitional = true;
211  contextualized = true;
212 #endif // DBUG_OFF
213  return false;
214  }
215 
216  /**
217  syntax_error() function replacement for deferred reporting of syntax
218  errors
219 
220  @param pc Current parse context.
221  @param pos Location of the error in lexical scanner buffers.
222  */
223  void error(Context *pc, const POS &pos) const {
224  pc->thd->syntax_error_at(pos);
225  }
226 
227  /**
228  syntax_error() function replacement for deferred reporting of syntax
229  errors
230 
231  @param pc Current parse context.
232  @param pos Location of the error in lexical scanner buffers.
233  @param msg Error message.
234  */
235  void error(Context *pc, const POS &pos, const char *msg) const {
236  pc->thd->syntax_error_at(pos, "%s", msg);
237  }
238 
239  /**
240  syntax_error() function replacement for deferred reporting of syntax
241  errors
242 
243  @param pc Current parse context.
244  @param pos Location of the error in lexical scanner buffers.
245  @param format Error message format string with optional argument list.
246  */
247  void errorf(Context *pc, const POS &pos, const char *format, ...) const
248  MY_ATTRIBUTE((format(printf, 4, 5)));
249 };
250 
251 template <typename Context>
252 inline void Parse_tree_node_tmpl<Context>::errorf(Context *pc, const POS &pos,
253  const char *format,
254  ...) const {
255  va_list args;
256  va_start(args, format);
257  pc->thd->vsyntax_error_at(pos, format, args);
258  va_end(args);
259 }
260 
262 
263 #endif /* PARSE_TREE_NODE_BASE_INCLUDED */
#define TRASH(A, B)
Put bad content in memory to be sure it will segfault if dereferenced.
Definition: memory_debugging.h:70
UPDATE ... SET field=(subquery)...
Definition: parse_tree_node_base.h:60
MEM_ROOT * mem_root
Current MEM_ROOT.
Definition: parse_tree_node_base.h:101
ORDER BY clause execution context.
Definition: parse_tree_node_base.h:74
unsigned char uchar
Definition: my_inttypes.h:51
t pos
Definition: dbug_analyze.cc:148
Subquery executed once during optimization.
Definition: parse_tree_node_base.h:81
Definition: parse_tree_node_base.h:63
SELECT_LEX * select
Current SELECT_LEX object.
Definition: parse_tree_node_base.h:102
Definition: parse_tree_node_base.h:65
File containing constants that can be used throughout the server.
enum_parsing_context
Names for different query parse tree parts.
Definition: parse_tree_node_base.h:55
Some integer typedefs for easier portability.
see SQL_BUFFER_RESULT in the manual
Definition: parse_tree_node_base.h:78
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:847
Inner SELECTs of UNION expression.
Definition: parse_tree_node_base.h:84
GROUP BY clause execution context.
Definition: parse_tree_node_base.h:75
printf("== done ==\)
YYLTYPE POS
Definition: parse_tree_node_base.h:94
"Derived" subquery
Definition: parse_tree_node_base.h:67
for single-table UPDATE/DELETE/INSERT/REPLACE
Definition: parse_tree_node_base.h:58
#define STACK_MIN_SIZE
Definition: sql_const.h:199
Various macros useful for communicating with memory debuggers, such as Valgrind.
bool transitional
Definition: parse_tree_node_base.h:120
Subquery in HAVING clause item tree.
Definition: parse_tree_node_base.h:71
#define DBUG_ASSERT(A)
Definition: my_dbug.h:183
Context context_t
Definition: parse_tree_node_base.h:124
ON clause context.
Definition: parse_tree_node_base.h:69
INSERT ... VALUES.
Definition: parse_tree_node_base.h:61
INSERT ... ON DUPLICATE KEY UPDATE ...
Definition: parse_tree_node_base.h:62
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:235
Definition: parse_tree_node_base.h:64
Parse_context(THD *thd, SELECT_LEX *sl)
Definition: parse_tree_node_base.cc:28
Bison "location" class.
Definition: parse_location.h:43
bool is_contextualized() const
Definition: parse_tree_node_base.h:150
void error(Context *pc, const POS &pos) const
syntax_error() function replacement for deferred reporting of syntax errors
Definition: parse_tree_node_base.h:223
void operator=(const Parse_tree_node_tmpl &)
Subquery in ORDER BY clause item tree.
Definition: parse_tree_node_base.h:79
virtual bool contextualize_(Context *)
Intermediate version of the contextualize() function
Definition: parse_tree_node_base.h:207
Header for compiler-dependent features.
Definition: item.h:666
Base class for parse tree nodes (excluding the Parse_tree_root hierarchy)
Definition: parse_tree_node_base.h:111
THD *const thd
Current thread handler.
Definition: parse_tree_node_base.h:100
DISTINCT clause execution context.
Definition: parse_tree_node_base.h:77
SELECT (subquery), (subquery)...
Definition: parse_tree_node_base.h:59
Definition: parse_tree_node_base.h:82
char msg[1024]
Definition: test_sql_9_sessions.cc:282
ORDER BY clause execution context.
Definition: parse_tree_node_base.h:72
Environment data for the contextualization phase.
Definition: parse_tree_node_base.h:99
static MEM_ROOT mem_root
Definition: client_plugin.cc:107
bool contextualized
Definition: parse_tree_node_base.h:119
"No tables used" messages etc.
Definition: parse_tree_node_base.h:57
Subquery in WHERE clause item tree.
Definition: parse_tree_node_base.h:68
virtual bool contextualize(Context *pc)
Do all context-sensitive things and mark the node as contextualized.
Definition: parse_tree_node_base.h:161
Parse_tree_node_tmpl()
Definition: parse_tree_node_base.h:139
Named or unnamed window.
Definition: parse_tree_node_base.h:70
Pseudo-table context for UNION result.
Definition: parse_tree_node_base.h:83
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:134
bool check_stack_overrun(const THD *thd, long margin, unsigned char *buf)
Check stack for a overrun.
Definition: check_stack.cc:71
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
Parse_tree_node_tmpl< Parse_context > Parse_tree_node
Definition: parse_tree_node_base.h:261
GROUP BY clause execution context.
Definition: parse_tree_node_base.h:73
Subquery in GROUP BY clause item tree.
Definition: parse_tree_node_base.h:80
DISTINCT clause execution context.
Definition: parse_tree_node_base.h:76
Empty value.
Definition: parse_tree_node_base.h:56
Definition: parse_tree_node_base.h:66
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:252
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:777
virtual ~Parse_tree_node_tmpl()
Definition: parse_tree_node_base.h:147