MySQL  8.0.27
Source Code Documentation
parse_tree_helpers.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_HELPERS_INCLUDED
24 #define PARSE_TREE_HELPERS_INCLUDED
25 
26 #include <assert.h>
27 #include <sys/types.h> // TODO: replace with cstdint
28 #include <new>
29 
30 #include "lex_string.h"
31 #include "m_ctype.h"
32 
33 #include "my_inttypes.h" // TODO: replace with cstdint
34 #include "mysql_time.h"
35 #include "sql/item.h"
36 #include "sql/item_func.h" // Item etc.
37 #include "sql/parse_location.h" // POS
39 #include "sql/resourcegroups/resource_group_basic_types.h" // resourcegroups::Range
40 #include "sql/set_var.h" // enum_var_type
41 #include "sql/sql_error.h"
42 #include "sql/sql_list.h"
43 
44 class String;
45 class THD;
46 class my_decimal;
48 struct MEM_ROOT;
49 struct handlerton;
50 
51 template <typename Element_type>
52 class Mem_root_array;
53 
54 /**
55  Base class for parse-time Item objects
56 
57  Parse-time Item objects are placeholders for real Item objects: in some
58  cases it is not easy or even possible to decide what exact Item class object
59  we need to allocate in the parser. Parse-time Item objects are intended
60  to defer real Item object allocation to the contextualization phase (see
61  the Item::itemize() function).
62 
63  This wrapper class overrides abstract virtual functions of the parent
64  class with dummy wrappers to make C++ compiler happy.
65 */
66 class Parse_tree_item : public Item {
67  public:
68  explicit Parse_tree_item(const POS &pos) : Item(pos) {}
69 
70  enum Type type() const override { return INVALID_ITEM; }
71  double val_real() override {
72  assert(0);
73  return 0;
74  }
75  longlong val_int() override {
76  assert(0);
77  return 0;
78  }
79  String *val_str(String *) override {
80  assert(0);
81  return nullptr;
82  }
84  assert(0);
85  return nullptr;
86  }
87  bool get_date(MYSQL_TIME *, uint) override {
88  assert(0);
89  return false;
90  }
91  bool get_time(MYSQL_TIME *) override {
92  assert(0);
93  return false;
94  }
95 };
96 
97 /**
98  Wrapper class for an Item list head, used to allocate Item lists in the parser
99  in a context-independent way
100 */
103 
104  public:
106 
108 
109  bool contextualize(Parse_context *pc) override {
110  if (super::contextualize(pc)) return true;
111  for (Item *&item : value) {
112  if (item->itemize(pc, &item)) return true;
113  }
114  return false;
115  }
116 
117  bool is_empty() const { return value.empty(); }
118  uint elements() const { return value.size(); }
119 
120  bool push_back(Item *item) {
121  /*
122  Item may be NULL in case of OOM: just ignore it and check thd->is_error()
123  in the caller code.
124  */
125  if (item == nullptr) return true;
126  value.push_back(item);
127  return false;
128  }
129 
130  bool push_front(Item *item) {
131  /*
132  Item may be NULL in case of OOM: just ignore it and check thd->is_error()
133  in the caller code.
134  */
135  if (item == nullptr) return true;
136  value.push_front(item);
137  return false;
138  }
139 
141  assert(!is_empty());
142  Item *ret = value.front();
143  value.pop_front();
144  return ret;
145  }
146 
147  Item *operator[](uint index) const { return value[index]; }
148 };
149 
150 /**
151  Contextualize a Mem_root_array of parse tree nodes of the type PTN
152 
153  @tparam Context Parse context.
154  @tparam Array Array of parse tree nodes.
155 
156  @param[in,out] pc Parse context.
157  @param[in,out] array Array of nodes to contextualize.
158 
159  @return false on success.
160 */
161 template <typename Context, typename Array>
162 bool contextualize_array(Context *pc, Array *array) {
163  for (auto it : *array) {
164  if (pc->thd->lex->will_contextualize && it->contextualize(pc)) return true;
165  }
166  return false;
167 }
168 
169 /**
170  Helper function to imitate dynamic_cast for Item_cond hierarchy.
171 
172  Template parameter @p To is the destination type (@c Item_cond_and etc.)
173  Template parameter @p Tag is the Functype tag to compare from->functype() with
174 
175  @param from source item
176 
177  @return typecasted item of the type To or NULL
178 */
179 template <class To, Item_func::Functype Tag>
180 To *item_cond_cast(Item *const from) {
181  return ((from->type() == Item::COND_ITEM &&
182  static_cast<Item_func *>(from)->functype() == Tag)
183  ? static_cast<To *>(from)
184  : nullptr);
185 }
186 
187 /**
188  Flatten associative operators at parse time
189 
190  This function flattens AND and OR operators at parse time if applicable,
191  otherwise it creates new Item_cond_and or Item_cond_or respectively.
192 
193  Template parameter @p Class is @c Item_cond_and or @c Item_cond_or
194  Template parameter @p Tag is @c COND_AND_FUNC (for @c Item_cond_and) or @c
195  COND_OR_FUNC otherwise
196 
197  @param mem_root MEM_ROOT
198  @param pos parse location
199  @param left left argument of the operator
200  @param right right argument of the operator
201 
202  @return resulting parse tree Item
203 */
204 template <class Class, Item_func::Functype Tag>
206  Item *left, Item *right) {
207  if (left == nullptr || right == nullptr) return nullptr;
208  Class *left_func = item_cond_cast<Class, Tag>(left);
209  Class *right_func = item_cond_cast<Class, Tag>(right);
210  if (left_func) {
211  if (right_func) {
212  // (X1 op X2) op (Y1 op Y2) ==> op (X1, X2, Y1, Y2)
213  right_func->add_at_head(left_func->argument_list());
214  return right;
215  } else {
216  // (X1 op X2) op Y ==> op (X1, X2, Y)
217  left_func->add(right);
218  return left;
219  }
220  } else if (right_func) {
221  // X op (Y1 op Y2) ==> op (X, Y1, Y2)
222  right_func->add_at_head(left);
223  return right;
224  } else {
225  /* X op Y */
226  return new (mem_root) Class(pos, left, right);
227  }
228 }
229 
231  class sp_variable *spv,
232  const char *query_start_ptr,
233  const char *start, const char *end);
234 
235 bool find_sys_var_null_base(THD *thd, struct sys_var_with_base *tmp);
236 bool set_system_variable(THD *thd, struct sys_var_with_base *tmp,
237  enum enum_var_type var_type, Item *val);
238 LEX_CSTRING make_string(THD *thd, const char *start_ptr, const char *end_ptr);
239 bool set_trigger_new_row(Parse_context *pc, LEX_CSTRING trigger_field_name,
240  Item *expr_item, LEX_CSTRING expr_query);
241 void sp_create_assignment_lex(THD *thd, const char *option_ptr);
242 bool sp_create_assignment_instr(THD *thd, const char *expr_end_ptr);
243 bool resolve_engine(THD *thd, const LEX_CSTRING &name, bool is_temp_table,
244  bool strict, handlerton **ret);
245 bool apply_privileges(
247 
248 inline bool is_identifier(const char *str, const char *ident) {
249  return !my_strcasecmp(system_charset_info, str, ident);
250 }
251 
252 inline bool is_identifier(const LEX_STRING &str, const char *ident) {
253  return is_identifier(str.str, ident);
254 }
255 bool is_key_cache_variable_suffix(const char *suffix);
256 
259  const LEX_CSTRING &name,
260  const resourcegroups::Type &type);
264 
266 #endif /* PARSE_TREE_HELPERS_INCLUDED */
Definition: item_func.h:93
virtual enum Functype functype() const
Definition: item_func.h:305
Definition: item.h:3575
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
Type
Definition: item.h:838
@ INVALID_ITEM
Definition: item.h:839
@ COND_ITEM
Definition: item.h:852
virtual enum Type type() const =0
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:418
Wrapper class for an Item list head, used to allocate Item lists in the parser in a context-independe...
Definition: parse_tree_helpers.h:101
Item * pop_front()
Definition: parse_tree_helpers.h:140
bool is_empty() const
Definition: parse_tree_helpers.h:117
mem_root_deque< Item * > value
Definition: parse_tree_helpers.h:107
Parse_tree_node super
Definition: parse_tree_helpers.h:102
Item * operator[](uint index) const
Definition: parse_tree_helpers.h:147
PT_item_list()
Definition: parse_tree_helpers.h:105
bool push_front(Item *item)
Definition: parse_tree_helpers.h:130
bool contextualize(Parse_context *pc) override
Definition: parse_tree_helpers.h:109
bool push_back(Item *item)
Definition: parse_tree_helpers.h:120
uint elements() const
Definition: parse_tree_helpers.h:118
Base class for parse-time Item objects.
Definition: parse_tree_helpers.h:66
enum Type type() const override
Definition: parse_tree_helpers.h:70
Parse_tree_item(const POS &pos)
Definition: parse_tree_helpers.h:68
double val_real() override
Definition: parse_tree_helpers.h:71
bool get_date(MYSQL_TIME *, uint) override
Definition: parse_tree_helpers.h:87
bool get_time(MYSQL_TIME *) override
Definition: parse_tree_helpers.h:91
String * val_str(String *) override
Definition: parse_tree_helpers.h:79
my_decimal * val_decimal(my_decimal *) override
Definition: parse_tree_helpers.h:83
longlong val_int() override
Definition: parse_tree_helpers.h:75
Base class for parse tree nodes (excluding the Parse_tree_root hierarchy)
Definition: parse_tree_node_base.h:102
virtual bool contextualize(Context *pc)
Do all context-sensitive things and mark the node as contextualized.
Definition: parse_tree_node_base.h:150
enum_severity_level
Enumeration value describing the severity of the condition.
Definition: sql_error.h:59
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:165
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
Element_type & front()
Returns the first element in the deque.
Definition: mem_root_deque.h:253
size_t size() const
Definition: mem_root_deque.h:458
bool push_back(const Element_type &element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:176
bool push_front(const Element_type &element)
Adds the given element to the beginning of the deque.
Definition: mem_root_deque.h:210
void pop_front()
Removes the first element from the deque.
Definition: mem_root_deque.h:246
bool empty() const
Definition: mem_root_deque.h:459
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:92
This class represents a stored program variable or a parameter (also referenced as 'SP-variable').
Definition: sp_pcontext.h:48
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
char * pos
Definition: do_ctype.cc:76
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:165
A better implementation of the UNIX ctype(3) library.
MYSQL_PLUGIN_IMPORT CHARSET_INFO * system_charset_info
Definition: mysqld.cc:1512
#define my_strcasecmp(s, a, b)
Definition: m_ctype.h:677
Some integer typedefs for easier portability.
long long int longlong
Definition: my_inttypes.h:54
Time declarations shared between the server and client API: you should not add anything to this heade...
thread_local MEM_ROOT ** THR_MALLOC
Definition: mysqld.cc:1527
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1056
bool is_temp_table(const HA_CREATE_INFO &ci)
Definition: sql_table.cc:205
Type
Definition: resource_group_basic_types.h:32
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:191
LEX_CSTRING make_string(THD *thd, const char *start_ptr, const char *end_ptr)
Make a new string allocated on THD's mem-root.
Definition: parse_tree_helpers.cc:192
bool find_sys_var_null_base(THD *thd, struct sys_var_with_base *tmp)
Definition: parse_tree_helpers.cc:123
bool sp_create_assignment_instr(THD *thd, const char *expr_end_ptr)
Create a SP instruction for a SET assignment.
Definition: parse_tree_helpers.cc:306
bool apply_privileges(THD *thd, const Mem_root_array< class PT_role_or_privilege * > &privs)
This helper function is responsible for aggregating grants from parser tokens to containers and masks...
Definition: parse_tree_helpers.cc:421
void sp_create_assignment_lex(THD *thd, const char *option_ptr)
Definition: parse_tree_helpers.cc:243
bool is_key_cache_variable_suffix(const char *suffix)
Definition: my_getopt.cc:116
Item_splocal * create_item_for_sp_var(THD *thd, LEX_CSTRING name, class sp_variable *spv, const char *query_start_ptr, const char *start, const char *end)
Create an object to represent a SP variable in the Item-hierarchy.
Definition: parse_tree_helpers.cc:80
bool set_trigger_new_row(Parse_context *pc, LEX_CSTRING trigger_field_name, Item *expr_item, LEX_CSTRING expr_query)
Helper action for a SET statement.
Definition: parse_tree_helpers.cc:209
bool validate_vcpu_range(const resourcegroups::Range &range)
Definition: parse_tree_helpers.cc:477
bool check_resource_group_name_len(const LEX_CSTRING &name, Sql_condition::enum_severity_level severity)
Definition: parse_tree_helpers.cc:525
bool resolve_engine(THD *thd, const LEX_CSTRING &name, bool is_temp_table, bool strict, handlerton **ret)
Resolve engine by its name.
Definition: parse_tree_helpers.cc:392
bool set_system_variable(THD *thd, struct sys_var_with_base *tmp, enum enum_var_type var_type, Item *val)
Helper action for a SET statement.
Definition: parse_tree_helpers.cc:146
Item * flatten_associative_operator(MEM_ROOT *mem_root, const POS &pos, Item *left, Item *right)
Flatten associative operators at parse time.
Definition: parse_tree_helpers.h:205
bool contextualize_array(Context *pc, Array *array)
Contextualize a Mem_root_array of parse tree nodes of the type PTN.
Definition: parse_tree_helpers.h:162
bool validate_resource_group_priority(THD *thd, int *priority, const LEX_CSTRING &name, const resourcegroups::Type &type)
Definition: parse_tree_helpers.cc:488
To * item_cond_cast(Item *const from)
Helper function to imitate dynamic_cast for Item_cond hierarchy.
Definition: parse_tree_helpers.h:180
bool check_resource_group_support()
Definition: parse_tree_helpers.cc:515
bool is_identifier(const char *str, const char *ident)
Definition: parse_tree_helpers.h:248
void move_cf_appliers(Parse_context *tddlpc, Column_parse_context *cpc)
Definition: parse_tree_helpers.cc:540
required uint32 priority
Definition: replication_group_member_actions.proto:34
required string type
Definition: replication_group_member_actions.proto:33
"public" interface to sys_var - server configuration variables.
enum_var_type
Definition: set_var.h:87
case opt name
Definition: sslopt-case.h:32
Parse context for column type attribyte specific parse tree nodes.
Definition: parse_tree_column_attrs.h:72
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:78
Definition: mysql_lex_string.h:39
Definition: mysql_lex_string.h:34
Definition: mysql_time.h:81
Environment data for the contextualization phase.
Definition: parse_tree_node_base.h:90
Bison "location" class.
Definition: parse_location.h:42
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2391
Definition: gen_lex_token.cc:151
Definition: resource_group_basic_types.h:33
used by the parser to store internal variable name
Definition: parser_yystype.h:167
unsigned int uint
Definition: uca-dump.cc:29