MySQL  8.0.19
Source Code Documentation
parse_tree_helpers.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_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 /**
50  Base class for parse-time Item objects
51 
52  Parse-time Item objects are placeholders for real Item objects: in some
53  cases it is not easy or even possible to decide what exact Item class object
54  we need to allocate in the parser. Parse-time Item objects are intended
55  to defer real Item object allocation to the contextualization phase (see
56  the Item::itemize() function).
57 
58  This wrapper class overrides abstract virtual functions of the parent
59  class with dummy wrappers to make C++ compiler happy.
60 */
61 class Parse_tree_item : public Item {
62  public:
63  explicit Parse_tree_item(const POS &pos) : Item(pos) {}
64 
65  enum Type type() const override { return INVALID_ITEM; }
66  double val_real() override {
67  DBUG_ASSERT(0);
68  return 0;
69  }
70  longlong val_int() override {
71  DBUG_ASSERT(0);
72  return 0;
73  }
74  String *val_str(String *) override {
75  DBUG_ASSERT(0);
76  return NULL;
77  }
79  DBUG_ASSERT(0);
80  return NULL;
81  }
82  bool get_date(MYSQL_TIME *, uint) override {
83  DBUG_ASSERT(0);
84  return false;
85  }
86  bool get_time(MYSQL_TIME *) override {
87  DBUG_ASSERT(0);
88  return false;
89  }
90 };
91 
92 /**
93  Wrapper class for an Item list head, used to allocate Item lists in the parser
94  in a context-independent way
95 */
96 class PT_item_list : public Parse_tree_node {
98 
99  public:
101 
102  bool contextualize(Parse_context *pc) override {
103  if (super::contextualize(pc)) return true;
105  Item *item;
106  while ((item = it++)) {
107  if (item->itemize(pc, &item)) return true;
108  it.replace(item);
109  }
110  return false;
111  }
112 
113  bool is_empty() const { return value.is_empty(); }
114  uint elements() const { return value.elements; }
115 
116  bool push_back(Item *item) {
117  /*
118  Item may be NULL in case of OOM: just ignore it and check thd->is_error()
119  in the caller code.
120  */
121  return item == NULL || value.push_back(item);
122  }
123 
124  bool push_front(Item *item) {
125  /*
126  Item may be NULL in case of OOM: just ignore it and check thd->is_error()
127  in the caller code.
128  */
129  return item == NULL || value.push_front(item);
130  }
131 
133  DBUG_ASSERT(!is_empty());
134  return value.pop();
135  }
136 
137  Item *operator[](uint index) const { return value[index]; }
138 };
139 
140 /**
141  Contextualize a Mem_root_array of parse tree nodes of the type PTN
142 
143  @tparam Context Parse context.
144  @tparam Array Array of parse tree nodes.
145 
146  @param[in,out] pc Parse context.
147  @param[in,out] array Array of nodes to contextualize.
148 
149  @return false on success.
150 */
151 template <typename Context, typename Array>
152 bool contextualize_array(Context *pc, Array *array) {
153  for (auto it : *array) {
154  if (pc->thd->lex->will_contextualize && it->contextualize(pc)) return true;
155  }
156  return false;
157 }
158 
159 /**
160  Helper function to imitate dynamic_cast for Item_cond hierarchy.
161 
162  Template parameter @p To is the destination type (@c Item_cond_and etc.)
163  Template parameter @p Tag is the Functype tag to compare from->functype() with
164 
165  @param from source item
166 
167  @return typecasted item of the type To or NULL
168 */
169 template <class To, Item_func::Functype Tag>
170 To *item_cond_cast(Item *const from) {
171  return ((from->type() == Item::COND_ITEM &&
172  static_cast<Item_func *>(from)->functype() == Tag)
173  ? static_cast<To *>(from)
174  : NULL);
175 }
176 
177 /**
178  Flatten associative operators at parse time
179 
180  This function flattens AND and OR operators at parse time if applicable,
181  otherwise it creates new Item_cond_and or Item_cond_or respectively.
182 
183  Template parameter @p Class is @c Item_cond_and or @c Item_cond_or
184  Template parameter @p Tag is @c COND_AND_FUNC (for @c Item_cond_and) or @c
185  COND_OR_FUNC otherwise
186 
187  @param mem_root MEM_ROOT
188  @param pos parse location
189  @param left left argument of the operator
190  @param right right argument of the operator
191 
192  @return resulting parse tree Item
193 */
194 template <class Class, Item_func::Functype Tag>
196  Item *left, Item *right) {
197  if (left == NULL || right == NULL) return NULL;
198  Class *left_func = item_cond_cast<Class, Tag>(left);
199  Class *right_func = item_cond_cast<Class, Tag>(right);
200  if (left_func) {
201  if (right_func) {
202  // (X1 op X2) op (Y1 op Y2) ==> op (X1, X2, Y1, Y2)
203  right_func->add_at_head(left_func->argument_list());
204  return right;
205  } else {
206  // (X1 op X2) op Y ==> op (X1, X2, Y)
207  left_func->add(right);
208  return left;
209  }
210  } else if (right_func) {
211  // X op (Y1 op Y2) ==> op (X, Y1, Y2)
212  right_func->add_at_head(left);
213  return right;
214  } else {
215  /* X op Y */
216  return new (mem_root) Class(pos, left, right);
217  }
218 }
219 
221  class sp_variable *spv,
222  const char *query_start_ptr,
223  const char *start, const char *end);
224 
225 bool find_sys_var_null_base(THD *thd, struct sys_var_with_base *tmp);
226 bool set_system_variable(THD *thd, struct sys_var_with_base *tmp,
227  enum enum_var_type var_type, Item *val);
228 LEX_CSTRING make_string(THD *thd, const char *start_ptr, const char *end_ptr);
229 bool set_trigger_new_row(Parse_context *pc, LEX_CSTRING trigger_field_name,
230  Item *expr_item, LEX_CSTRING expr_query);
231 void sp_create_assignment_lex(THD *thd, const char *option_ptr);
232 bool sp_create_assignment_instr(THD *thd, const char *expr_end_ptr);
233 bool resolve_engine(THD *thd, const LEX_CSTRING &name, bool is_temp_table,
234  bool strict, handlerton **ret);
235 bool apply_privileges(
237 
238 inline bool is_identifier(const char *str, const char *ident) {
239  return !my_strcasecmp(system_charset_info, str, ident);
240 }
241 
242 inline bool is_identifier(const LEX_STRING &str, const char *ident) {
243  return is_identifier(str.str, ident);
244 }
245 bool is_key_cache_variable_suffix(const char *suffix);
246 
247 bool validate_vcpu_range(const resourcegroups::Range &range);
248 bool validate_resource_group_priority(THD *thd, int *priority,
249  const LEX_CSTRING &name,
250  const resourcegroups::Type &type);
254 #endif /* PARSE_TREE_HELPERS_INCLUDED */
MYSQL_LEX_CSTRING
Definition: mysql_lex_string.h:39
Item
Definition: item.h:665
THD
Definition: sql_class.h:764
make_string
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:180
Mem_root_array
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:398
PT_item_list::value
List< Item > value
Definition: parse_tree_helpers.h:100
Parse_tree_item::Parse_tree_item
Parse_tree_item(const POS &pos)
Definition: parse_tree_helpers.h:63
PT_item_list::contextualize
bool contextualize(Parse_context *pc) override
Definition: parse_tree_helpers.h:102
Parse_tree_item::val_str
String * val_str(String *) override
Definition: parse_tree_helpers.h:74
Item_splocal
Definition: item.h:2932
PT_item_list::operator[]
Item * operator[](uint index) const
Definition: parse_tree_helpers.h:137
flatten_associative_operator
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:195
NULL
#define NULL
Definition: types.h:55
Parse_tree_item::get_date
bool get_date(MYSQL_TIME *, uint) override
Definition: parse_tree_helpers.h:82
PT_item_list::super
Parse_tree_node super
Definition: parse_tree_helpers.h:97
check_resource_group_support
bool check_resource_group_support()
Definition: parse_tree_helpers.cc:502
Parse_tree_item::val_int
longlong val_int() override
Definition: parse_tree_helpers.h:70
handlerton
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2271
pos
char * pos
Definition: do_ctype.cc:76
Parse_tree_node_tmpl
Base class for parse tree nodes (excluding the Parse_tree_root hierarchy)
Definition: parse_tree_node_base.h:111
my_dbug.h
String
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:164
Item::Type
Type
Definition: item.h:701
enum_var_type
enum_var_type
Definition: set_var.h:87
longlong
long long int longlong
Definition: my_inttypes.h:54
sp_create_assignment_instr
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:293
List_iterator
Definition: sql_base.h:66
Parse_tree_item::get_time
bool get_time(MYSQL_TIME *) override
Definition: parse_tree_helpers.h:86
mem_root
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
Parse_tree_item::type
enum Type type() const override
Definition: parse_tree_helpers.h:65
contextualize_array
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:152
resourcegroups::Range
Definition: resource_group_basic_types.h:33
resolve_engine
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:379
item_func.h
PT_item_list::pop_front
Item * pop_front()
Definition: parse_tree_helpers.h:132
Sql_condition::enum_severity_level
enum_severity_level
Enumeration value describing the severity of the condition.
Definition: sql_error.h:58
Item::type
virtual enum Type type() const =0
create_item_for_sp_var
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:68
resource_group_basic_types.h
sp_create_assignment_lex
void sp_create_assignment_lex(THD *thd, const char *option_ptr)
Definition: parse_tree_helpers.cc:230
Item::COND_ITEM
@ COND_ITEM
Definition: item.h:715
resourcegroups::Type
Type
Definition: resource_group_basic_types.h:32
Parse_tree_item
Base class for parse-time Item objects.
Definition: parse_tree_helpers.h:61
my_inttypes.h
List_iterator::replace
T * replace(T *a)
Definition: sql_list.h:573
PT_item_list::push_back
bool push_back(Item *item)
Definition: parse_tree_helpers.h:116
m_ctype.h
MEM_ROOT
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:77
lex_string.h
is_key_cache_variable_suffix
bool is_key_cache_variable_suffix(const char *suffix)
Definition: my_getopt.cc:116
sys_var_with_base
used by the parser to store internal variable name
Definition: parser_yystype.h:72
set_var.h
Parse_tree_item::val_decimal
my_decimal * val_decimal(my_decimal *) override
Definition: parse_tree_helpers.h:78
Item::INVALID_ITEM
@ INVALID_ITEM
Definition: item.h:702
uint
unsigned int uint
Definition: uca-dump.cc:29
is_identifier
bool is_identifier(const char *str, const char *ident)
Definition: parse_tree_helpers.h:238
List::push_back
bool push_back(T *a)
Definition: sql_list.h:442
PT_item_list::push_front
bool push_front(Item *item)
Definition: parse_tree_helpers.h:124
system_charset_info
MYSQL_PLUGIN_IMPORT CHARSET_INFO * system_charset_info
Definition: mysqld.cc:1316
rules_table_service::end
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:188
my_decimal
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:91
Item_func
Definition: item_func.h:83
check_resource_group_name_len
bool check_resource_group_name_len(const LEX_CSTRING &name, Sql_condition::enum_severity_level severity)
Definition: parse_tree_helpers.cc:512
Parse_tree_item::val_real
double val_real() override
Definition: parse_tree_helpers.h:66
PT_item_list
Wrapper class for an Item list head, used to allocate Item lists in the parser in a context-independe...
Definition: parse_tree_helpers.h:96
name
const string name("\"Name\"")
Item::itemize
virtual bool itemize(Parse_context *pc, Item **res)
The same as contextualize() but with additional parameter.
Definition: item.cc:588
set_system_variable
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:134
MYSQL_TIME
Definition: mysql_time.h:81
MYSQL_LEX_STRING
Definition: mysql_lex_string.h:34
YYLTYPE
Bison "location" class.
Definition: parse_location.h:43
set_trigger_new_row
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:197
item_cond_cast
To * item_cond_cast(Item *const from)
Helper function to imitate dynamic_cast for Item_cond hierarchy.
Definition: parse_tree_helpers.h:170
Parse_tree_node_tmpl::contextualize
virtual bool contextualize(Context *pc)
Do all context-sensitive things and mark the node as contextualized.
Definition: parse_tree_node_base.h:159
sql_list.h
DBUG_ASSERT
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
PT_item_list::is_empty
bool is_empty() const
Definition: parse_tree_helpers.h:113
HttpMethod::type
int type
Definition: http_common.h:411
Parse_context
Environment data for the contextualization phase.
Definition: parse_tree_node_base.h:99
find_sys_var_null_base
bool find_sys_var_null_base(THD *thd, struct sys_var_with_base *tmp)
Definition: parse_tree_helpers.cc:111
List::pop
T * pop()
Definition: sql_list.h:459
parse_tree_node_base.h
MYSQL_LEX_STRING::str
char * str
Definition: mysql_lex_string.h:35
start
static void start(PluginFuncEnv *env)
Definition: http_server_plugin.cc:572
List< Item >
apply_privileges
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:408
my_strcasecmp
#define my_strcasecmp(s, a, b)
Definition: m_ctype.h:678
sp_variable
This class represents a stored program variable or a parameter (also referenced as 'SP-variable').
Definition: sp_pcontext.h:47
validate_vcpu_range
bool validate_vcpu_range(const resourcegroups::Range &range)
Definition: parse_tree_helpers.cc:464
Item_func::functype
virtual enum Functype functype() const
Definition: item_func.h:233
mem_root_array.h
validate_resource_group_priority
bool validate_resource_group_priority(THD *thd, int *priority, const LEX_CSTRING &name, const resourcegroups::Type &type)
Definition: parse_tree_helpers.cc:475
List::push_front
bool push_front(T *a)
Definition: sql_list.h:448
PT_item_list::elements
uint elements() const
Definition: parse_tree_helpers.h:114
index
char * index(const char *, int c)
Definition: mysql.cc:2875
mysql_time.h
item.h