MySQL  8.0.27
Source Code Documentation
parse_tree_node_base.h
Go to the documentation of this file.
1 /* Copyright (c) 2013, 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 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 
31 #include "memory_debugging.h"
32 #include "my_alloc.h"
33 #include "my_compiler.h"
34 
35 #include "my_inttypes.h" // TODO: replace with cstdint
36 #include "sql/check_stack.h"
37 #include "sql/parse_location.h"
38 #include "sql/sql_const.h"
39 
40 class Query_block;
41 class THD;
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  Environment data for the contextualization phase
89 */
90 struct Parse_context {
91  THD *const thd; ///< Current thread handler
92  MEM_ROOT *mem_root; ///< Current MEM_ROOT
93  Query_block *select; ///< Current Query_block object
94 
96 };
97 
98 /**
99  Base class for parse tree nodes (excluding the Parse_tree_root hierarchy)
100 */
101 template <typename Context>
103  friend class Item; // for direct access to the "contextualized" field
104 
106  void operator=(const Parse_tree_node_tmpl &); // undefined
107 
108 #ifndef NDEBUG
109  private:
110  bool contextualized; // true if the node object is contextualized
111 #endif // NDEBUG
112 
113  public:
114  typedef Context context_t;
115 
116  static void *operator new(size_t size, MEM_ROOT *mem_root,
117  const std::nothrow_t &arg
118  [[maybe_unused]] = std::nothrow) noexcept {
119  return mem_root->Alloc(size);
120  }
121  static void operator delete(void *ptr [[maybe_unused]],
122  size_t size [[maybe_unused]]) {
123  TRASH(ptr, size);
124  }
125  static void operator delete(void *, MEM_ROOT *,
126  const std::nothrow_t &) noexcept {}
127 
128  protected:
130 #ifndef NDEBUG
131  contextualized = false;
132 #endif // NDEBUG
133  }
134 
135  public:
136  virtual ~Parse_tree_node_tmpl() = default;
137 
138 #ifndef NDEBUG
139  bool is_contextualized() const { return contextualized; }
140 #endif // NDEBUG
141 
142  /**
143  Do all context-sensitive things and mark the node as contextualized
144 
145  @param pc current parse context
146 
147  @retval false success
148  @retval true syntax/OOM/etc error
149  */
150  virtual bool contextualize(Context *pc) {
151  uchar dummy;
152  if (check_stack_overrun(pc->thd, STACK_MIN_SIZE, &dummy)) return true;
153 
154 #ifndef NDEBUG
155  assert(!contextualized);
156  contextualized = true;
157 #endif // NDEBUG
158 
159  return false;
160  }
161 
162  /**
163  syntax_error() function replacement for deferred reporting of syntax
164  errors
165 
166  @param pc Current parse context.
167  @param pos Location of the error in lexical scanner buffers.
168  */
169  void error(Context *pc, const POS &pos) const {
170  pc->thd->syntax_error_at(pos);
171  }
172 
173  /**
174  syntax_error() function replacement for deferred reporting of syntax
175  errors
176 
177  @param pc Current parse context.
178  @param pos Location of the error in lexical scanner buffers.
179  @param msg Error message.
180  */
181  void error(Context *pc, const POS &pos, const char *msg) const {
182  pc->thd->syntax_error_at(pos, "%s", msg);
183  }
184 
185  /**
186  syntax_error() function replacement for deferred reporting of syntax
187  errors
188 
189  @param pc Current parse context.
190  @param pos Location of the error in lexical scanner buffers.
191  @param format Error message format string with optional argument list.
192  */
193  void errorf(Context *pc, const POS &pos, const char *format, ...) const
194  MY_ATTRIBUTE((format(printf, 4, 5)));
195 };
196 
197 template <typename Context>
198 inline void Parse_tree_node_tmpl<Context>::errorf(Context *pc, const POS &pos,
199  const char *format,
200  ...) const {
201  va_list args;
202  va_start(args, format);
203  pc->thd->vsyntax_error_at(pos, format, args);
204  va_end(args);
205 }
206 
208 
209 #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:802
Base class for parse tree nodes (excluding the Parse_tree_root hierarchy)
Definition: parse_tree_node_base.h:102
Parse_tree_node_tmpl(const Parse_tree_node_tmpl &)
Context context_t
Definition: parse_tree_node_base.h:114
virtual bool contextualize(Context *pc)
Do all context-sensitive things and mark the node as contextualized.
Definition: parse_tree_node_base.h:150
bool is_contextualized() const
Definition: parse_tree_node_base.h:139
void operator=(const Parse_tree_node_tmpl &)
virtual ~Parse_tree_node_tmpl()=default
Parse_tree_node_tmpl()
Definition: parse_tree_node_base.h:129
void error(Context *pc, const POS &pos) const
syntax_error() function replacement for deferred reporting of syntax errors
Definition: parse_tree_node_base.h:169
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:181
bool contextualized
Definition: parse_tree_node_base.h:110
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:198
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1123
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
char * pos
Definition: do_ctype.cc:76
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:55
@ CTX_QEP_TAB
Definition: parse_tree_node_base.h:64
@ CTX_SIMPLE_DISTINCT
DISTINCT clause execution context.
Definition: parse_tree_node_base.h:77
@ CTX_ORDER_BY
ORDER BY clause execution context.
Definition: parse_tree_node_base.h:72
@ CTX_HAVING
Subquery in HAVING clause item tree.
Definition: parse_tree_node_base.h:71
@ CTX_BUFFER_RESULT
see SQL_BUFFER_RESULT in the manual
Definition: parse_tree_node_base.h:78
@ CTX_MESSAGE
"No tables used" messages etc.
Definition: parse_tree_node_base.h:57
@ CTX_UNION_RESULT
Pseudo-table context for UNION result.
Definition: parse_tree_node_base.h:83
@ CTX_DERIVED
"Derived" subquery
Definition: parse_tree_node_base.h:67
@ CTX_SELECT_LIST
SELECT (subquery), (subquery)...
Definition: parse_tree_node_base.h:59
@ CTX_ORDER_BY_SQ
Subquery in ORDER BY clause item tree.
Definition: parse_tree_node_base.h:79
@ CTX_SIMPLE_ORDER_BY
ORDER BY clause execution context.
Definition: parse_tree_node_base.h:74
@ CTX_SIMPLE_GROUP_BY
GROUP BY clause execution context.
Definition: parse_tree_node_base.h:75
@ CTX_INSERT_UPDATE
INSERT ... ON DUPLICATE KEY UPDATE ...
Definition: parse_tree_node_base.h:62
@ CTX_GROUP_BY_SQ
Subquery in GROUP BY clause item tree.
Definition: parse_tree_node_base.h:80
@ CTX_MATERIALIZATION
Definition: parse_tree_node_base.h:65
@ CTX_NONE
Empty value.
Definition: parse_tree_node_base.h:56
@ CTX_DISTINCT
DISTINCT clause execution context.
Definition: parse_tree_node_base.h:76
@ CTX_UPDATE_VALUE
UPDATE ... SET field=(subquery)...
Definition: parse_tree_node_base.h:60
@ CTX_DUPLICATES_WEEDOUT
Definition: parse_tree_node_base.h:66
@ CTX_JOIN
Definition: parse_tree_node_base.h:63
@ CTX_OPTIMIZED_AWAY_SUBQUERY
Subquery executed once during optimization.
Definition: parse_tree_node_base.h:81
@ CTX_GROUP_BY
GROUP BY clause execution context.
Definition: parse_tree_node_base.h:73
@ CTX_QUERY_SPEC
Inner SELECTs of UNION expression.
Definition: parse_tree_node_base.h:84
@ CTX_TABLE
for single-table UPDATE/DELETE/INSERT/REPLACE
Definition: parse_tree_node_base.h:58
@ CTX_INSERT_VALUES
INSERT ... VALUES.
Definition: parse_tree_node_base.h:61
@ CTX_WHERE
Subquery in WHERE clause item tree.
Definition: parse_tree_node_base.h:68
@ CTX_WINDOW
Named or unnamed window.
Definition: parse_tree_node_base.h:70
@ CTX_ON
ON clause context.
Definition: parse_tree_node_base.h:69
@ CTX_UNION
Definition: parse_tree_node_base.h:82
Parse_tree_node_tmpl< Parse_context > Parse_tree_node
Definition: parse_tree_node_base.h:207
File containing constants that can be used throughout the server.
#define STACK_MIN_SIZE
Definition: sql_const.h:195
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:78
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:135
Environment data for the contextualization phase.
Definition: parse_tree_node_base.h:90
Query_block * select
Current Query_block object.
Definition: parse_tree_node_base.h:93
Parse_context(THD *thd, Query_block *sl)
Definition: parse_tree_node_base.cc:27
THD *const thd
Current thread handler.
Definition: parse_tree_node_base.h:91
MEM_ROOT * mem_root
Current MEM_ROOT.
Definition: parse_tree_node_base.h:92
Bison "location" class.
Definition: parse_location.h:42