Debian Codegraph @

storage/innobase/pars/pars0sym.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1997, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file pars/pars0sym.cc
29 SQL parser symbol table
30
31 Created 12/15/1997 Heikki Tuuri
32 *******************************************************/
33 #include "current_thd.h"
34
35 #include "pars0sym.h"
36
37 #include "data0data.h"
38 #include "data0type.h"
39 #include "dict0dd.h"
40 #include "eval0eval.h"
41 #include "mem0mem.h"
42 #include "pars0grm.h"
43 #include "pars0pars.h"
44 #include "que0que.h"
45 #include "row0sel.h"
46
47 /** Creates a symbol table for a single stored procedure or query.
48 @return own: symbol table */
49 sym_tab_t *sym_tab_create(
50 mem_heap_t *heap) /*!< in: memory heap where to create */
51 {
52 sym_tab_t *sym_tab;
53
54 sym_tab = static_cast<sym_tab_t *>(mem_heap_alloc(heap, sizeof(sym_tab_t)));
55
56 UT_LIST_INIT(sym_tab->sym_list);
57 UT_LIST_INIT(sym_tab->func_node_list);
58
59 sym_tab->heap = heap;
60
61 return (sym_tab);
62 }
63
64 /** Frees the memory allocated dynamically AFTER parsing phase for variables
65 etc. in the symbol table. Does not free the mem heap where the table was
66 originally created. Frees also SQL explicit cursor definitions. */
67 void sym_tab_free_private(sym_tab_t *sym_tab) /*!< in, own: symbol table */
68 {
69 THD *thd = current_thd;
70
71 for (auto sym : sym_tab->sym_list) {
72 /* Close the tables opened in pars_retrieve_table_def(). */
73
74 if (sym->token_type == SYM_TABLE_REF_COUNTED) {
75 if (sym->mdl != nullptr) {
76 dd_table_close(sym->table, thd, &sym->mdl, false);
77 } else {
78 dd_table_close(sym->table, nullptr, nullptr, false);
79 }
80
81 sym->table = nullptr;
82 sym->resolved = false;
83 sym->token_type = SYM_UNSET;
84 sym->mdl = nullptr;
85 }
86
87 eval_node_free_val_buf(sym);
88
89 if (sym->prefetch_buf) {
90 sel_col_prefetch_buf_free(sym->prefetch_buf);
91 }
92
93 if (sym->cursor_def) {
94 que_graph_free_recursive(sym->cursor_def);
95 }
96 }
97
98 for (auto func : sym_tab->func_node_list) {
99 eval_node_free_val_buf(func);
100 }
101 }
102
103 /** Adds an integer literal to a symbol table.
104 @return symbol table node */
105 sym_node_t *sym_tab_add_int_lit(sym_tab_t *sym_tab, /*!< in: symbol table */
106 ulint val) /*!< in: integer value */
107 {
108 sym_node_t *node;
109 byte *data;
110
111 node = static_cast<sym_node_t *>(
112 mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t)));
113
114 node->common.type = QUE_NODE_SYMBOL;
115
116 node->table = nullptr;
117 node->resolved = true;
118 node->token_type = SYM_LIT;
119
120 node->indirection = nullptr;
121
122 dtype_set(dfield_get_type(&node->common.val), DATA_INT, 0, 4);
123
124 data = static_cast<byte *>(mem_heap_alloc(sym_tab->heap, 4));
125 mach_write_to_4(data, val);
126
127 dfield_set_data(&(node->common.val), data, 4);
128
129 node->common.val_buf_size = 0;
130 node->prefetch_buf = nullptr;
131 node->cursor_def = nullptr;
132
133 UT_LIST_ADD_LAST(sym_tab->sym_list, node);
134
135 node->like_node = nullptr;
136
137 node->sym_table = sym_tab;
138
139 return (node);
140 }
141
142 /** Adds a string literal to a symbol table.
143 @return symbol table node */
144 sym_node_t *sym_tab_add_str_lit(sym_tab_t *sym_tab, /*!< in: symbol table */
145 const byte *str, /*!< in: string with no quotes
146 around it */
147 ulint len) /*!< in: string length */
148 {
149 sym_node_t *node;
150 byte *data;
151
152 node = static_cast<sym_node_t *>(
153 mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t)));
154
155 node->common.type = QUE_NODE_SYMBOL;
156
157 node->table = nullptr;
158 node->resolved = true;
159 node->token_type = SYM_LIT;
160
161 node->indirection = nullptr;
162
163 dtype_set(dfield_get_type(&node->common.val), DATA_VARCHAR, DATA_ENGLISH, 0);
164
165 data = (len) ? static_cast<byte *>(mem_heap_dup(sym_tab->heap, str, len))
166 : nullptr;
167
168 dfield_set_data(&(node->common.val), data, len);
169
170 node->common.val_buf_size = 0;
171 node->prefetch_buf = nullptr;
172 node->cursor_def = nullptr;
173
174 UT_LIST_ADD_LAST(sym_tab->sym_list, node);
175
176 node->like_node = nullptr;
177
178 node->sym_table = sym_tab;
179
180 return (node);
181 }
182
183 /** Add a bound literal to a symbol table.
184 @return symbol table node */
185 sym_node_t *sym_tab_add_bound_lit(
186 sym_tab_t *sym_tab, /*!< in: symbol table */
187 const char *name, /*!< in: name of bound literal */
188 ulint *lit_type) /*!< out: type of literal (PARS_*_LIT) */
189 {
190 sym_node_t *node;
191 pars_bound_lit_t *blit;
192 ulint len = 0;
193
194 blit = pars_info_get_bound_lit(sym_tab->info, name);
195 ut_a(blit);
196
197 node = static_cast<sym_node_t *>(
198 mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t)));
199
200 node->common.type = QUE_NODE_SYMBOL;
201 node->common.brother = node->common.parent = nullptr;
202
203 node->table = nullptr;
204 node->resolved = true;
205 node->token_type = SYM_LIT;
206
207 node->indirection = nullptr;
208
209 switch (blit->type) {
210 case DATA_FIXBINARY:
211 len = blit->length;
212 *lit_type = PARS_FIXBINARY_LIT;
213 break;
214
215 case DATA_BLOB:
216 *lit_type = PARS_BLOB_LIT;
217 break;
218
219 case DATA_VARCHAR:
220 *lit_type = PARS_STR_LIT;
221 break;
222
223 case DATA_CHAR:
224 ut_a(blit->length > 0);
225
226 len = blit->length;
227 *lit_type = PARS_STR_LIT;
228 break;
229
230 case DATA_INT:
231 ut_a(blit->length > 0);
232 ut_a(blit->length <= 8);
233
234 len = blit->length;
235 *lit_type = PARS_INT_LIT;
236 break;
237
238 default:
239 ut_error;
240 }
241
242 dtype_set(dfield_get_type(&node->common.val), blit->type, blit->prtype, len);
243
244 dfield_set_data(&(node->common.val), blit->address, blit->length);
245
246 node->common.val_buf_size = 0;
247 node->prefetch_buf = nullptr;
248 node->cursor_def = nullptr;
249
250 UT_LIST_ADD_LAST(sym_tab->sym_list, node);
251
252 blit->node = node;
253 node->like_node = nullptr;
254 node->sym_table = sym_tab;
255
256 return (node);
257 }
258
259 /**********************************************************************
260 Rebind literal to a node in the symbol table. */
261 sym_node_t *sym_tab_rebind_lit(
262 /* out: symbol table node */
263 sym_node_t *node, /* in: node that is bound to literal*/
264 const void *address, /* in: pointer to data */
265 ulint length) /* in: length of data */
266 {
267 dfield_t *dfield = que_node_get_val(node);
268 dtype_t *dtype = dfield_get_type(dfield);
269
270 ut_a(node->token_type == SYM_LIT);
271
272 dfield_set_data(&node->common.val, address, length);
273
274 if (node->like_node) {
275 ut_a(dtype_get_mtype(dtype) == DATA_CHAR ||
276 dtype_get_mtype(dtype) == DATA_VARCHAR);
277
278 /* Don't force [false] creation of sub-nodes (for LIKE) */
279 pars_like_rebind(node, static_cast<const byte *>(address), length);
280 }
281
282 /* FIXME: What's this ? */
283 node->common.val_buf_size = 0;
284
285 if (node->prefetch_buf) {
286 sel_col_prefetch_buf_free(node->prefetch_buf);
287 node->prefetch_buf = nullptr;
288 }
289
290 if (node->cursor_def) {
291 que_graph_free_recursive(node->cursor_def);
292 node->cursor_def = nullptr;
293 }
294
295 return (node);
296 }
297
298 /** Adds an SQL null literal to a symbol table.
299 @return symbol table node */
300 sym_node_t *sym_tab_add_null_lit(sym_tab_t *sym_tab) /*!< in: symbol table */
301 {
302 sym_node_t *node;
303
304 node = static_cast<sym_node_t *>(
305 mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t)));
306
307 node->common.type = QUE_NODE_SYMBOL;
308
309 node->table = nullptr;
310 node->resolved = true;
311 node->token_type = SYM_LIT;
312
313 node->indirection = nullptr;
314
315 dfield_get_type(&node->common.val)->mtype = DATA_ERROR;
316
317 dfield_set_null(&node->common.val);
318
319 node->common.val_buf_size = 0;
320 node->prefetch_buf = nullptr;
321 node->cursor_def = nullptr;
322
323 UT_LIST_ADD_LAST(sym_tab->sym_list, node);
324
325 node->like_node = nullptr;
326
327 node->sym_table = sym_tab;
328
329 return (node);
330 }
331
332 /** Adds an identifier to a symbol table.
333 @return symbol table node */
334 sym_node_t *sym_tab_add_id(sym_tab_t *sym_tab, /*!< in: symbol table */
335 byte *name, /*!< in: identifier name */
336 ulint len) /*!< in: identifier length */
337 {
338 sym_node_t *node;
339
340 node =
341 static_cast<sym_node_t *>(mem_heap_zalloc(sym_tab->heap, sizeof(*node)));
342
343 node->common.type = QUE_NODE_SYMBOL;
344
345 node->name = mem_heap_strdupl(sym_tab->heap, (char *)name, len);
346 node->name_len = len;
347
348 UT_LIST_ADD_LAST(sym_tab->sym_list, node);
349
350 dfield_set_null(&node->common.val);
351
352 node->sym_table = sym_tab;
353
354 return (node);
355 }
356
357 /** Add a bound identifier to a symbol table.
358 @return symbol table node */
359 sym_node_t *sym_tab_add_bound_id(sym_tab_t *sym_tab, /*!< in: symbol table */
360 const char *name) /*!< in: name of bound id */
361 {
362 sym_node_t *node;
363 pars_bound_id_t *bid;
364
365 bid = pars_info_get_bound_id(sym_tab->info, name);
366 ut_a(bid);
367
368 node = static_cast<sym_node_t *>(
369 mem_heap_alloc(sym_tab->heap, sizeof(sym_node_t)));
370
371 node->common.type = QUE_NODE_SYMBOL;
372
373 node->table = nullptr;
374 node->resolved = false;
375 node->token_type = SYM_UNSET;
376 node->indirection = nullptr;
377
378 node->name = mem_heap_strdup(sym_tab->heap, bid->id);
379 node->name_len = strlen(node->name);
380
381 UT_LIST_ADD_LAST(sym_tab->sym_list, node);
382
383 dfield_set_null(&node->common.val);
384
385 node->common.val_buf_size = 0;
386 node->prefetch_buf = nullptr;
387 node->cursor_def = nullptr;
388
389 node->like_node = nullptr;
390
391 node->sym_table = sym_tab;
392
393 return (node);
394 }