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