MySQL 9.5.0
Source Code Documentation
parse_tree_helpers.h
Go to the documentation of this file.
1/* Copyright (c) 2013, 2025, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef PARSE_TREE_HELPERS_INCLUDED
25#define PARSE_TREE_HELPERS_INCLUDED
26
27#include <assert.h>
28#include <sys/types.h> // TODO: replace with cstdint
29#include <new>
30
31#include "lex_string.h"
32
33#include "my_inttypes.h" // TODO: replace with cstdint
35#include "mysql_time.h"
36#include "sql/item.h"
37#include "sql/item_func.h" // Item etc.
38#include "sql/parse_location.h" // POS
40#include "sql/resourcegroups/resource_group_basic_types.h" // resourcegroups::Range
41#include "sql/set_var.h" // enum_var_type
42#include "sql/sql_error.h"
43#include "sql/sql_list.h"
44
47class String;
48class THD;
49class my_decimal;
50enum class Set_operator;
52struct MEM_ROOT;
53struct handlerton;
54
55template <typename Element_type>
56class Mem_root_array;
57
58/**
59 Base class for parse-time Item objects
60
61 Parse-time Item objects are placeholders for real Item objects: in some
62 cases it is not easy or even possible to decide what exact Item class object
63 we need to allocate in the parser. Parse-time Item objects are intended
64 to defer real Item object allocation to the contextualization phase (see
65 the Item::itemize() function).
66
67 This wrapper class overrides abstract virtual functions of the parent
68 class with dummy wrappers to make C++ compiler happy.
69*/
70class Parse_tree_item : public Item {
71 public:
72 explicit Parse_tree_item(const POS &pos) : Item(pos) {}
73
74 enum Type type() const override { return INVALID_ITEM; }
75 double val_real() override {
76 assert(false);
77 return 0;
78 }
79 longlong val_int() override {
80 assert(false);
81 return 0;
82 }
83 String *val_str(String *) override {
84 assert(false);
85 return nullptr;
86 }
88 assert(false);
89 return nullptr;
90 }
91 bool val_date(Date_val *, uint) override {
92 assert(false);
93 return false;
94 }
95 bool val_time(Time_val *) override {
96 assert(false);
97 return false;
98 }
99 bool val_datetime(Datetime_val *, uint) override {
100 assert(false);
101 return false;
102 }
103};
104
105/**
106 Wrapper class for an Item list head, used to allocate Item lists in the parser
107 in a context-independent way
108*/
111
112 public:
113 explicit PT_item_list(const POS &pos) : super(pos), value(*THR_MALLOC) {}
114 explicit PT_item_list(const POS &start_pos, const POS &end_pos)
115 : super(start_pos, end_pos), value(*THR_MALLOC) {}
116
118
119 bool do_contextualize(Parse_context *pc) override {
120 if (super::do_contextualize(pc)) return true;
121 for (Item *&item : value) {
122 if (item->itemize(pc, &item)) return true;
123 }
124 return false;
125 }
126
127 bool is_empty() const { return value.empty(); }
128 uint elements() const { return value.size(); }
129
130 bool push_back(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_back(item);
137 return false;
138 }
139
140 bool push_front(Item *item) {
141 /*
142 Item may be NULL in case of OOM: just ignore it and check thd->is_error()
143 in the caller code.
144 */
145 if (item == nullptr) return true;
146 value.push_front(item);
147 return false;
148 }
149
151 assert(!is_empty());
152 Item *ret = value.front();
153 value.pop_front();
154 return ret;
155 }
156
157 Item *operator[](uint index) const { return value[index]; }
158};
159
160/**
161 Contextualize a Mem_root_array of parse tree nodes of the type PTN
162
163 @tparam Context Parse context.
164 @tparam Array Array of parse tree nodes.
165
166 @param[in,out] pc Parse context.
167 @param[in,out] array Array of nodes to contextualize.
168
169 @return false on success.
170*/
171template <typename Context, typename Array>
172bool contextualize_array(Context *pc, Array *array) {
173 for (auto it : *array) {
174 if (pc->thd->lex->will_contextualize && it->contextualize(pc)) return true;
175 }
176 return false;
177}
178
179/**
180 Helper function to imitate dynamic_cast for Item_cond hierarchy.
181
182 Template parameter @p To is the destination type (@c Item_cond_and etc.)
183 Template parameter @p Tag is the Functype tag to compare from->functype() with
184
185 @param from source item
186
187 @return typecasted item of the type To or NULL
188*/
189template <class To, Item_func::Functype Tag>
190To *item_cond_cast(Item *const from) {
191 return ((from->type() == Item::COND_ITEM &&
192 static_cast<Item_func *>(from)->functype() == Tag)
193 ? static_cast<To *>(from)
194 : nullptr);
195}
196
197/**
198 Flatten associative operators at parse time
199
200 This function flattens AND and OR operators at parse time if applicable,
201 otherwise it creates new Item_cond_and or Item_cond_or respectively.
202
203 Template parameter @p Class is @c Item_cond_and or @c Item_cond_or
204 Template parameter @p Tag is @c COND_AND_FUNC (for @c Item_cond_and) or @c
205 COND_OR_FUNC otherwise
206
207 @param mem_root MEM_ROOT
208 @param pos parse location
209 @param left left argument of the operator
210 @param right right argument of the operator
211
212 @return resulting parse tree Item
213*/
214template <class Class, Item_func::Functype Tag>
216 Item *left, Item *right) {
217 if (left == nullptr || right == nullptr) return nullptr;
218 Class *left_func = item_cond_cast<Class, Tag>(left);
219 Class *right_func = item_cond_cast<Class, Tag>(right);
220 if (left_func) {
221 if (right_func) {
222 // (X1 op X2) op (Y1 op Y2) ==> op (X1, X2, Y1, Y2)
223 right_func->add_at_head(left_func->argument_list());
224 return right;
225 } else {
226 // (X1 op X2) op Y ==> op (X1, X2, Y)
227 left_func->add(right);
228 return left;
229 }
230 } else if (right_func) {
231 // X op (Y1 op Y2) ==> op (X, Y1, Y2)
232 right_func->add_at_head(left);
233 return right;
234 } else {
235 /* X op Y */
236 return new (mem_root) Class(pos, left, right);
237 }
238}
239
241 class sp_variable *spv,
242 const char *query_start_ptr,
243 const char *start, const char *end);
244
245LEX_CSTRING make_string(THD *thd, const char *start_ptr, const char *end_ptr);
246void sp_create_assignment_lex(THD *thd, const char *option_ptr);
247bool sp_create_assignment_instr(THD *thd, const char *expr_end_ptr);
248bool resolve_engine(THD *thd, const LEX_CSTRING &name, bool is_temp_table,
249 bool strict, handlerton **ret);
252
253inline bool is_identifier(const char *str, const char *ident) {
254 return !my_strcasecmp(system_charset_info, str, ident);
255}
256
257inline bool is_identifier(const LEX_STRING &str, const char *ident) {
258 return is_identifier(str.str, ident);
259}
260
263 const LEX_CSTRING &name,
268
270
271#endif /* PARSE_TREE_HELPERS_INCLUDED */
Definition: my_temporal.h:395
Definition: my_temporal.h:339
Definition: item_func.h:101
virtual enum Functype functype() const
Definition: item_func.h:374
Definition: item.h:4014
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:928
Type
Definition: item.h:963
@ INVALID_ITEM
Definition: item.h:964
@ COND_ITEM
An AND or OR condition.
Definition: item.h:976
virtual enum Type type() const =0
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:432
Wrapper class for an Item list head, used to allocate Item lists in the parser in a context-independe...
Definition: parse_tree_helpers.h:109
bool is_empty() const
Definition: parse_tree_helpers.h:127
Item * operator[](uint index) const
Definition: parse_tree_helpers.h:157
mem_root_deque< Item * > value
Definition: parse_tree_helpers.h:117
Parse_tree_node super
Definition: parse_tree_helpers.h:110
Item * pop_front()
Definition: parse_tree_helpers.h:150
PT_item_list(const POS &pos)
Definition: parse_tree_helpers.h:113
bool push_front(Item *item)
Definition: parse_tree_helpers.h:140
bool push_back(Item *item)
Definition: parse_tree_helpers.h:130
PT_item_list(const POS &start_pos, const POS &end_pos)
Definition: parse_tree_helpers.h:114
uint elements() const
Definition: parse_tree_helpers.h:128
bool do_contextualize(Parse_context *pc) override
Definition: parse_tree_helpers.h:119
Definition: parse_tree_nodes.h:822
Definition: parse_tree_nodes.h:1515
Base class for parse-time Item objects.
Definition: parse_tree_helpers.h:70
enum Type type() const override
Definition: parse_tree_helpers.h:74
Parse_tree_item(const POS &pos)
Definition: parse_tree_helpers.h:72
double val_real() override
Definition: parse_tree_helpers.h:75
my_decimal * val_decimal(my_decimal *) override
Definition: parse_tree_helpers.h:87
bool val_date(Date_val *, uint) override
Definition: parse_tree_helpers.h:91
String * val_str(String *) override
Definition: parse_tree_helpers.h:83
bool val_time(Time_val *) override
Evaluate the item and return result as a time value.
Definition: parse_tree_helpers.h:95
bool val_datetime(Datetime_val *, uint) override
Definition: parse_tree_helpers.h:99
longlong val_int() override
Definition: parse_tree_helpers.h:79
Base class for parse tree nodes (excluding the Parse_tree_root hierarchy)
Definition: parse_tree_node_base.h:231
virtual bool do_contextualize(Context *pc)
Do all context-sensitive things and mark the node as contextualized.
Definition: parse_tree_node_base.h:284
enum_severity_level
Enumeration value describing the severity of the condition.
Definition: sql_error.h:66
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:169
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Time_val is a temporal type that represents only time.
Definition: my_temporal.h:55
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:111
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:97
This class represents a stored program variable or a parameter (also referenced as 'SP-variable').
Definition: sp_pcontext.h:49
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
A better implementation of the UNIX ctype(3) library.
int my_strcasecmp(const CHARSET_INFO *cs, const char *s1, const char *s2)
Definition: m_ctype.h:651
MYSQL_PLUGIN_IMPORT CHARSET_INFO * system_charset_info
Definition: mysqld.cc:1574
Some integer typedefs for easier portability.
long long int longlong
Definition: my_inttypes.h:55
thread_local MEM_ROOT ** THR_MALLOC
Definition: mysqld.cc:1589
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1078
bool is_temp_table(const HA_CREATE_INFO &ci)
Definition: sql_table.cc:236
void right(std::string *to_trim)
Definition: trim.h:41
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:76
void left(std::string *to_trim)
Definition: trim.h:35
Type
Definition: resource_group_basic_types.h:33
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
@ Array
C++ Object.
Definition: jit_executor_value.h:67
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:137
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:82
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:205
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:320
void sp_create_assignment_lex(THD *thd, const char *option_ptr)
Definition: parse_tree_helpers.cc:142
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:215
bool validate_vcpu_range(const resourcegroups::Range &range)
Definition: parse_tree_helpers.cc:376
bool check_resource_group_name_len(const LEX_CSTRING &name, Sql_condition::enum_severity_level severity)
Definition: parse_tree_helpers.cc:424
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:291
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:172
To * item_cond_cast(Item *const from)
Helper function to imitate dynamic_cast for Item_cond hierarchy.
Definition: parse_tree_helpers.h:190
bool validate_resource_group_priority(THD *thd, int *priority, const LEX_CSTRING &name, const resourcegroups::Type &type)
Definition: parse_tree_helpers.cc:387
bool check_resource_group_support()
Definition: parse_tree_helpers.cc:414
bool is_identifier(const char *str, const char *ident)
Definition: parse_tree_helpers.h:253
void move_cf_appliers(Parse_context *tddlpc, Column_parse_context *cpc)
Definition: parse_tree_helpers.cc:439
Set_operator
Definition: parser_yystype.h:346
required uint32 priority
Definition: replication_group_member_actions.proto:35
required string type
Definition: replication_group_member_actions.proto:34
"public" interface to sys_var - server configuration variables.
case opt name
Definition: sslopt-case.h:29
Parse context for column type attribute specific parse tree nodes.
Definition: parse_tree_column_attrs.h:73
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: mysql_lex_string.h:40
Definition: mysql_lex_string.h:35
Bison "location" class.
Definition: parse_location.h:43
Environment data for the contextualization phase.
Definition: parse_tree_node_base.h:422
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2851
Definition: gen_lex_token.cc:149
Definition: resource_group_basic_types.h:34