Debian Codegraph @

storage/innobase/pars/pars0pars.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1996, 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/pars0pars.cc
29 SQL parser
30
31 Created 11/19/1996 Heikki Tuuri
32 *******************************************************/
33
34 /* Historical note: Innobase executed its first SQL string (CREATE TABLE)
35 on 1/27/1998 */
36
37 #include "current_thd.h"
38 #include "ha_prototypes.h"
39
40 #include "data0data.h"
41 #include "data0type.h"
42 #include "dict0crea.h"
43 #include "dict0dd.h"
44 #include "dict0dict.h"
45 #include "dict0mem.h"
46 #include "eval0eval.h"
47 #include "ha_prototypes.h"
48 #include "lock0lock.h"
49 #include "pars0grm.h"
50 #include "pars0opt.h"
51 #include "pars0pars.h"
52 #include "que0que.h"
53 #include "row0ins.h"
54 #include "row0sel.h"
55 #include "row0upd.h"
56 #include "trx0roll.h"
57 #include "trx0trx.h"
58
59 #include "my_dbug.h"
60
61 /* Global variable used while parsing a single procedure or query : the code is
62 NOT re-entrant */
63 sym_tab_t *pars_sym_tab_global;
64
65 /* Global variables used to denote certain reserved words, used in
66 constructing the parsing tree */
67
68 pars_res_word_t pars_to_binary_token = {PARS_TO_BINARY_TOKEN};
69 pars_res_word_t pars_substr_token = {PARS_SUBSTR_TOKEN};
70 pars_res_word_t pars_concat_token = {PARS_CONCAT_TOKEN};
71 pars_res_word_t pars_instr_token = {PARS_INSTR_TOKEN};
72 pars_res_word_t pars_length_token = {PARS_LENGTH_TOKEN};
73 pars_res_word_t pars_count_token = {PARS_COUNT_TOKEN};
74 pars_res_word_t pars_sum_token = {PARS_SUM_TOKEN};
75 pars_res_word_t pars_distinct_token = {PARS_DISTINCT_TOKEN};
76 pars_res_word_t pars_binary_token = {PARS_BINARY_TOKEN};
77 pars_res_word_t pars_blob_token = {PARS_BLOB_TOKEN};
78 pars_res_word_t pars_int_token = {PARS_INT_TOKEN};
79 pars_res_word_t pars_bigint_token = {PARS_BIGINT_TOKEN};
80 pars_res_word_t pars_char_token = {PARS_CHAR_TOKEN};
81 pars_res_word_t pars_float_token = {PARS_FLOAT_TOKEN};
82 pars_res_word_t pars_update_token = {PARS_UPDATE_TOKEN};
83 pars_res_word_t pars_asc_token = {PARS_ASC_TOKEN};
84 pars_res_word_t pars_desc_token = {PARS_DESC_TOKEN};
85 pars_res_word_t pars_open_token = {PARS_OPEN_TOKEN};
86 pars_res_word_t pars_close_token = {PARS_CLOSE_TOKEN};
87 pars_res_word_t pars_share_token = {PARS_SHARE_TOKEN};
88 pars_res_word_t pars_unique_token = {PARS_UNIQUE_TOKEN};
89 pars_res_word_t pars_clustered_token = {PARS_CLUSTERED_TOKEN};
90
91 /** Global variable used to denote the '*' in SELECT * FROM. */
92 ulint pars_star_denoter = 12345678;
93
94 /** Mutex to protect the sql parser */
95 ib_mutex_t pars_mutex;
96
97 /** Initialize for the internal parser */
98 void pars_init() { mutex_create(LATCH_ID_PARSER, &pars_mutex); }
99
100 /** Clean up the internal parser */
101 void pars_close() { mutex_free(&pars_mutex); }
102
103 /********************************************************************
104 Get user function with the given name.*/
105 static inline pars_user_func_t *pars_info_lookup_user_func(
106 /* out: user func, or NULL if not
107 found */
108 pars_info_t *info, /* in: info struct */
109 const char *name) /* in: function name to find*/
110 {
111 if (info && info->funcs) {
112 ulint i;
113 ib_vector_t *vec = info->funcs;
114
115 for (i = 0; i < ib_vector_size(vec); i++) {
116 pars_user_func_t *puf;
117
118 puf = static_cast<pars_user_func_t *>(ib_vector_get(vec, i));
119
120 if (strcmp(puf->name, name) == 0) {
121 return (puf);
122 }
123 }
124 }
125
126 return (nullptr);
127 }
128
129 /********************************************************************
130 Get bound identifier with the given name.*/
131 static inline pars_bound_id_t *pars_info_lookup_bound_id(
132 /* out: bound literal, or NULL if
133 not found */
134 pars_info_t *info, /* in: info struct */
135 const char *name) /* in: bound literal name to find */
136 {
137 if (info && info->bound_ids) {
138 ulint i;
139 ib_vector_t *vec = info->bound_ids;
140
141 for (i = 0; i < ib_vector_size(vec); i++) {
142 pars_bound_id_t *bid;
143
144 bid = static_cast<pars_bound_id_t *>(ib_vector_get(vec, i));
145
146 if (strcmp(bid->name, name) == 0) {
147 return (bid);
148 }
149 }
150 }
151
152 return (nullptr);
153 }
154
155 /********************************************************************
156 Get bound literal with the given name.*/
157 static inline pars_bound_lit_t *pars_info_lookup_bound_lit(
158 /* out: bound literal, or NULL if
159 not found */
160 pars_info_t *info, /* in: info struct */
161 const char *name) /* in: bound literal name to find */
162 {
163 if (info && info->bound_lits) {
164 ulint i;
165 ib_vector_t *vec = info->bound_lits;
166
167 for (i = 0; i < ib_vector_size(vec); i++) {
168 pars_bound_lit_t *pbl;
169
170 pbl = static_cast<pars_bound_lit_t *>(ib_vector_get(vec, i));
171
172 if (strcmp(pbl->name, name) == 0) {
173 return (pbl);
174 }
175 }
176 }
177
178 return (nullptr);
179 }
180
181 /** Determines the class of a function code.
182 @return function class: PARS_FUNC_ARITH, ... */
183 static ulint pars_func_get_class(
184 int func) /*!< in: function code: '=', PARS_GE_TOKEN, ... */
185 {
186 switch (func) {
187 case '+':
188 case '-':
189 case '*':
190 case '/':
191 return (PARS_FUNC_ARITH);
192
193 case '=':
194 case '<':
195 case '>':
196 case PARS_GE_TOKEN:
197 case PARS_LE_TOKEN:
198 case PARS_NE_TOKEN:
199 return (PARS_FUNC_CMP);
200
201 case PARS_AND_TOKEN:
202 case PARS_OR_TOKEN:
203 case PARS_NOT_TOKEN:
204 return (PARS_FUNC_LOGICAL);
205
206 case PARS_COUNT_TOKEN:
207 case PARS_SUM_TOKEN:
208 return (PARS_FUNC_AGGREGATE);
209
210 case PARS_TO_BINARY_TOKEN:
211 case PARS_SUBSTR_TOKEN:
212 case PARS_CONCAT_TOKEN:
213 case PARS_LENGTH_TOKEN:
214 case PARS_INSTR_TOKEN:
215 case PARS_NOTFOUND_TOKEN:
216 return (PARS_FUNC_PREDEFINED);
217
218 default:
219 return (PARS_FUNC_OTHER);
220 }
221 }
222
223 /** Parses an operator or predefined function expression.
224 @return own: function node in a query tree */
225 static func_node_t *pars_func_low(
226 int func, /*!< in: function token code */
227 que_node_t *arg) /*!< in: first argument in the argument list */
228 {
229 func_node_t *node;
230
231 node = static_cast<func_node_t *>(
232 mem_heap_alloc(pars_sym_tab_global->heap, sizeof(func_node_t)));
233
234 node->common.type = QUE_NODE_FUNC;
235 dfield_set_data(&(node->common.val), nullptr, 0);
236 node->common.val_buf_size = 0;
237
238 node->func = func;
239
240 node->fclass = pars_func_get_class(func);
241
242 node->args = arg;
243
244 UT_LIST_ADD_LAST(pars_sym_tab_global->func_node_list, node);
245
246 return (node);
247 }
248
249 /** Parses a function expression.
250 @return own: function node in a query tree */
251 func_node_t *pars_func(
252 que_node_t *res_word, /*!< in: function name reserved word */
253 que_node_t *arg) /*!< in: first argument in the argument list */
254 {
255 return (pars_func_low(((pars_res_word_t *)res_word)->code, arg));
256 }
257
258 /*************************************************************************
259 Rebind a LIKE search string. NOTE: We ignore any '%' characters embedded
260 within the search string.*/
261 int pars_like_rebind(
262 /* out, own: function node in a query tree */
263 sym_node_t *node, /* in: The search string node.*/
264 const byte *ptr, /* in: literal to (re) bind */
265 ulint ptr_len) /* in: length of literal to (re) bind*/
266 {
267 dtype_t *dtype;
268 dfield_t *dfield;
269 ib_like_t op_check;
270 sym_node_t *like_node;
271 sym_node_t *str_node = nullptr;
272 ib_like_t op = IB_LIKE_EXACT;
273 int func = PARS_LIKE_TOKEN_EXACT;
274
275 /* Is this a STRING% ? */
276 if (ptr[ptr_len - 1] == '%') {
277 op = IB_LIKE_PREFIX;
278 }
279
280 /* Is this a '%STRING' or %STRING% ?*/
281 ut_ad(*ptr != '%');
282
283 if (node->like_node == nullptr) {
284 /* Add the LIKE operator info node to the node list.
285 This will be used during the comparison phase to determine
286 how to match.*/
287 like_node = sym_tab_add_int_lit(node->sym_table, op);
288 que_node_list_add_last(nullptr, like_node);
289 node->like_node = like_node;
290 str_node = sym_tab_add_str_lit(node->sym_table, ptr, ptr_len);
291 que_node_list_add_last(like_node, str_node);
292 } else {
293 like_node = node->like_node;
294
295 /* Change the value of the string in the existing
296 string node of like node */
297 str_node = static_cast<sym_node_t *>(que_node_list_get_last(like_node));
298
299 /* Must find the string node */
300 ut_a(str_node);
301 ut_a(str_node != like_node);
302 ut_a(str_node->token_type == SYM_LIT);
303
304 dfield = que_node_get_val(str_node);
305 dfield_set_data(dfield, ptr, ptr_len);
306 }
307
308 dfield = que_node_get_val(like_node);
309 dtype = dfield_get_type(dfield);
310
311 ut_a(dtype_get_mtype(dtype) == DATA_INT);
312 op_check = static_cast<ib_like_t>(
313 mach_read_from_4(static_cast<byte *>(dfield_get_data(dfield))));
314
315 switch (op_check) {
316 case IB_LIKE_PREFIX:
317 case IB_LIKE_EXACT:
318 break;
319
320 default:
321 ut_error;
322 }
323
324 mach_write_to_4(static_cast<byte *>(dfield_get_data(dfield)), op);
325
326 dfield = que_node_get_val(node);
327
328 /* Adjust the length of the search value so the '%' is not
329 visible. Then create and add a search string node to the
330 search value node. Searching for %SUFFIX and %SUBSTR% requires
331 a full table scan and so we set the search value to ''.
332 For PREFIX% we simply remove the trailing '%'.*/
333
334 switch (op) {
335 case IB_LIKE_EXACT:
336 dfield = que_node_get_val(str_node);
337 dtype = dfield_get_type(dfield);
338
339 ut_a(dtype_get_mtype(dtype) == DATA_VARCHAR);
340
341 dfield_set_data(dfield, ptr, ptr_len);
342 break;
343
344 case IB_LIKE_PREFIX:
345 func = PARS_LIKE_TOKEN_PREFIX;
346
347 /* Modify the original node */
348 dfield_set_len(dfield, ptr_len - 1);
349
350 dfield = que_node_get_val(str_node);
351 dtype = dfield_get_type(dfield);
352
353 ut_a(dtype_get_mtype(dtype) == DATA_VARCHAR);
354
355 dfield_set_data(dfield, ptr, ptr_len - 1);
356 break;
357
358 default:
359 ut_error;
360 }
361
362 return (func);
363 }
364
365 /*************************************************************************
366 Parses a LIKE operator expression. */
367 static int pars_like_op(
368 /* out, own: function node in a query tree */
369 que_node_t *arg) /* in: LIKE comparison string.*/
370 {
371 char *ptr;
372 ulint ptr_len;
373 int func = PARS_LIKE_TOKEN_EXACT;
374 dfield_t *dfield = que_node_get_val(arg);
375 dtype_t *dtype = dfield_get_type(dfield);
376
377 ut_a(dtype_get_mtype(dtype) == DATA_CHAR ||
378 dtype_get_mtype(dtype) == DATA_VARCHAR);
379
380 ptr = static_cast<char *>(dfield_get_data(dfield));
381 ptr_len = strlen(ptr);
382
383 if (ptr_len) {
384 func =
385 pars_like_rebind(static_cast<sym_node_t *>(arg), (byte *)ptr, ptr_len);
386 }
387
388 return (func);
389 }
390 /** Parses an operator expression.
391 @return own: function node in a query tree */
392 func_node_t *pars_op(int func, /*!< in: operator token code */
393 que_node_t *arg1, /*!< in: first argument */
394 que_node_t *arg2) /*!< in: second argument or NULL for an
395 unary operator */
396 {
397 que_node_list_add_last(nullptr, arg1);
398
399 if (arg2) {
400 que_node_list_add_last(arg1, arg2);
401 }
402
403 /* We need to parse the string and determine whether it's a
404 PREFIX, SUFFIX or SUBSTRING comparison */
405 if (func == PARS_LIKE_TOKEN) {
406 ut_a(que_node_get_type(arg2) == QUE_NODE_SYMBOL);
407
408 func = pars_like_op(arg2);
409
410 ut_a(func == PARS_LIKE_TOKEN_EXACT || func == PARS_LIKE_TOKEN_PREFIX ||
411 func == PARS_LIKE_TOKEN_SUFFIX || func == PARS_LIKE_TOKEN_SUBSTR);
412 }
413
414 return (pars_func_low(func, arg1));
415 }
416
417 /** Parses an ORDER BY clause. Order by a single column only is supported.
418 @return own: order-by node in a query tree */
419 order_node_t *pars_order_by(
420 sym_node_t *column, /*!< in: column name */
421 pars_res_word_t *asc) /*!< in: &pars_asc_token or pars_desc_token */
422 {
423 order_node_t *node;
424
425 node = static_cast<order_node_t *>(
426 mem_heap_alloc(pars_sym_tab_global->heap, sizeof(order_node_t)));
427
428 node->common.type = QUE_NODE_ORDER;
429
430 node->column = column;
431
432 if (asc == &pars_asc_token) {
433 node->asc = true;
434 } else {
435 ut_a(asc == &pars_desc_token);
436 node->asc = false;
437 }
438
439 return (node);
440 }
441
442 /** Determine if a data type is a built-in string data type of the InnoDB
443 SQL parser.
444 @return true if string data type */
445 static bool pars_is_string_type(ulint mtype) /*!< in: main data type */
446 {
447 switch (mtype) {
448 case DATA_VARCHAR:
449 case DATA_CHAR:
450 case DATA_FIXBINARY:
451 case DATA_BINARY:
452 return true;
453 }
454
455 return false;
456 }
457
458 /** Resolves the data type of a function in an expression. The argument data
459 types must already be resolved. */
460 static void pars_resolve_func_data_type(
461 func_node_t *node) /*!< in: function node */
462 {
463 que_node_t *arg;
464
465 ut_a(que_node_get_type(node) == QUE_NODE_FUNC);
466
467 arg = node->args;
468
469 switch (node->func) {
470 case PARS_SUM_TOKEN:
471 case '+':
472 case '-':
473 case '*':
474 case '/':
475 /* Inherit the data type from the first argument (which must
476 not be the SQL null literal whose type is DATA_ERROR) */
477
478 dtype_copy(que_node_get_data_type(node), que_node_get_data_type(arg));
479
480 ut_a(dtype_get_mtype(que_node_get_data_type(node)) == DATA_INT);
481 break;
482
483 case PARS_COUNT_TOKEN:
484 ut_a(arg);
485 dtype_set(que_node_get_data_type(node), DATA_INT, 0, 4);
486 break;
487
488 case PARS_TO_BINARY_TOKEN:
489 if (dtype_get_mtype(que_node_get_data_type(arg)) == DATA_INT) {
490 dtype_set(que_node_get_data_type(node), DATA_VARCHAR, DATA_ENGLISH, 0);
491 } else {
492 dtype_set(que_node_get_data_type(node), DATA_BINARY, 0, 0);
493 }
494 break;
495
496 case PARS_LENGTH_TOKEN:
497 case PARS_INSTR_TOKEN:
498 ut_a(pars_is_string_type(que_node_get_data_type(arg)->mtype));
499 dtype_set(que_node_get_data_type(node), DATA_INT, 0, 4);
500 break;
501
502 case PARS_SUBSTR_TOKEN:
503 case PARS_CONCAT_TOKEN:
504 ut_a(pars_is_string_type(que_node_get_data_type(arg)->mtype));
505 dtype_set(que_node_get_data_type(node), DATA_VARCHAR, DATA_ENGLISH, 0);
506 break;
507
508 case '>':
509 case '<':
510 case '=':
511 case PARS_GE_TOKEN:
512 case PARS_LE_TOKEN:
513 case PARS_NE_TOKEN:
514 case PARS_AND_TOKEN:
515 case PARS_OR_TOKEN:
516 case PARS_NOT_TOKEN:
517 case PARS_NOTFOUND_TOKEN:
518
519 /* We currently have no iboolean type: use integer type */
520 dtype_set(que_node_get_data_type(node), DATA_INT, 0, 4);
521 break;
522
523 case PARS_LIKE_TOKEN_EXACT:
524 case PARS_LIKE_TOKEN_PREFIX:
525 case PARS_LIKE_TOKEN_SUFFIX:
526 case PARS_LIKE_TOKEN_SUBSTR:
527 dtype_set(que_node_get_data_type(node), DATA_VARCHAR, DATA_ENGLISH, 0);
528 break;
529
530 default:
531 ut_error;
532 }
533 }
534
535 /** Resolves the meaning of variables in an expression and the data types of
536 functions. It is an error if some identifier cannot be resolved here. */
537 static void pars_resolve_exp_variables_and_types(
538 sel_node_t *select_node, /*!< in: select node or NULL; if
539 this is not NULL then the variable
540 sym nodes are added to the
541 copy_variables list of select_node */
542 que_node_t *exp_node) /*!< in: expression */
543 {
544 func_node_t *func_node;
545 que_node_t *arg;
546 sym_node_t *sym_node;
547 sym_node_t *node;
548
549 ut_a(exp_node);
550
551 if (que_node_get_type(exp_node) == QUE_NODE_FUNC) {
552 func_node = static_cast<func_node_t *>(exp_node);
553
554 arg = func_node->args;
555
556 while (arg) {
557 pars_resolve_exp_variables_and_types(select_node, arg);
558
559 arg = que_node_get_next(arg);
560 }
561
562 pars_resolve_func_data_type(func_node);
563
564 return;
565 }
566
567 ut_a(que_node_get_type(exp_node) == QUE_NODE_SYMBOL);
568
569 sym_node = static_cast<sym_node_t *>(exp_node);
570
571 if (sym_node->resolved) {
572 return;
573 }
574
575 /* Not resolved yet: look in the symbol table for a variable
576 or a cursor or a function with the same name */
577
578 node = UT_LIST_GET_FIRST(pars_sym_tab_global->sym_list);
579
580 while (node) {
581 if (node->resolved &&
582 ((node->token_type == SYM_VAR) || (node->token_type == SYM_CURSOR) ||
583 (node->token_type == SYM_FUNCTION)) &&
584 node->name && (sym_node->name_len == node->name_len) &&
585 (ut_memcmp(sym_node->name, node->name, node->name_len) == 0)) {
586 /* Found a variable or a cursor declared with
587 the same name */
588
589 break;
590 }
591
592 node = UT_LIST_GET_NEXT(sym_list, node);
593 }
594
595 if (!node) {
596 fprintf(stderr, "PARSER ERROR: Unresolved identifier %s\n", sym_node->name);
597 }
598
599 ut_a(node);
600
601 sym_node->resolved = true;
602 sym_node->token_type = SYM_IMPLICIT_VAR;
603 sym_node->alias = node;
604 sym_node->indirection = node;
605
606 if (select_node) {
607 UT_LIST_ADD_LAST(select_node->copy_variables, sym_node);
608 }
609
610 dfield_set_type(que_node_get_val(sym_node), que_node_get_data_type(node));
611 }
612
613 /** Resolves the meaning of variables in an expression list. It is an error if
614 some identifier cannot be resolved here. Resolves also the data types of
615 functions. */
616 static void pars_resolve_exp_list_variables_and_types(
617 sel_node_t *select_node, /*!< in: select node or NULL */
618 que_node_t *exp_node) /*!< in: expression list first node, or
619 NULL */
620 {
621 while (exp_node) {
622 pars_resolve_exp_variables_and_types(select_node, exp_node);
623
624 exp_node = que_node_get_next(exp_node);
625 }
626 }
627
628 /** Resolves the columns in an expression. */
629 static void pars_resolve_exp_columns(
630 sym_node_t *table_node, /*!< in: first node in a table list */
631 que_node_t *exp_node) /*!< in: expression */
632 {
633 func_node_t *func_node;
634 que_node_t *arg;
635 sym_node_t *sym_node;
636 dict_table_t *table;
637 sym_node_t *t_node;
638 ulint n_cols;
639 ulint i;
640
641 ut_a(exp_node);
642
643 if (que_node_get_type(exp_node) == QUE_NODE_FUNC) {
644 func_node = static_cast<func_node_t *>(exp_node);
645
646 arg = func_node->args;
647
648 while (arg) {
649 pars_resolve_exp_columns(table_node, arg);
650
651 arg = que_node_get_next(arg);
652 }
653
654 return;
655 }
656
657 ut_a(que_node_get_type(exp_node) == QUE_NODE_SYMBOL);
658
659 sym_node = static_cast<sym_node_t *>(exp_node);
660
661 if (sym_node->resolved) {
662 return;
663 }
664
665 /* Not resolved yet: look in the table list for a column with the
666 same name */
667
668 t_node = table_node;
669
670 while (t_node) {
671 table = t_node->table;
672
673 n_cols = table->get_n_cols();
674
675 for (i = 0; i < n_cols; i++) {
676 const dict_col_t *col = table->get_col(i);
677 const char *col_name = table->get_col_name(i);
678
679 if ((sym_node->name_len == ut_strlen(col_name)) &&
680 (0 == ut_memcmp(sym_node->name, col_name, sym_node->name_len))) {
681 /* Found */
682 sym_node->resolved = true;
683 sym_node->token_type = SYM_COLUMN;
684 sym_node->table = table;
685 sym_node->col_no = i;
686 sym_node->prefetch_buf = nullptr;
687
688 col->copy_type(dfield_get_type(&sym_node->common.val));
689
690 return;
691 }
692 }
693
694 t_node = static_cast<sym_node_t *>(que_node_get_next(t_node));
695 }
696 }
697
698 /** Resolves the meaning of columns in an expression list. */
699 static void pars_resolve_exp_list_columns(
700 sym_node_t *table_node, /*!< in: first node in a table list */
701 que_node_t *exp_node) /*!< in: expression list first node, or
702 NULL */
703 {
704 while (exp_node) {
705 pars_resolve_exp_columns(table_node, exp_node);
706
707 exp_node = que_node_get_next(exp_node);
708 }
709 }
710
711 /** Retrieves the table definition for a table name id. */
712 static void pars_retrieve_table_def(sym_node_t *sym_node) /*!< in: table node */
713 {
714 ut_a(sym_node);
715 ut_a(que_node_get_type(sym_node) == QUE_NODE_SYMBOL);
716
717 /* Open the table only if it is not already opened. */
718 if (sym_node->token_type != SYM_TABLE_REF_COUNTED) {
719 ut_a(sym_node->table == nullptr);
720
721 sym_node->resolved = true;
722 sym_node->token_type = SYM_TABLE_REF_COUNTED;
723
724 THD *thd = current_thd;
725
726 sym_node->mdl = nullptr;
727 sym_node->table = dd_table_open_on_name_in_mem(sym_node->name, false);
728
729 if (sym_node->table == nullptr) {
730 sym_node->table = dd_table_open_on_name(
731 thd, &sym_node->mdl, sym_node->name, false, DICT_ERR_IGNORE_NONE);
732 }
733
734 ut_a(sym_node->table != nullptr);
735 }
736 }
737
738 /** Retrieves the table definitions for a list of table name ids.
739 @return number of tables */
740 static ulint pars_retrieve_table_list_defs(
741 sym_node_t *sym_node) /*!< in: first table node in list */
742 {
743 ulint count = 0;
744
745 if (sym_node == nullptr) {
746 return (count);
747 }
748
749 while (sym_node) {
750 pars_retrieve_table_def(sym_node);
751
752 count++;
753
754 sym_node = static_cast<sym_node_t *>(que_node_get_next(sym_node));
755 }
756
757 return (count);
758 }
759
760 /** Adds all columns to the select list if the query is SELECT * FROM ... */
761 static void pars_select_all_columns(
762 sel_node_t *select_node) /*!< in: select node already containing
763 the table list */
764 {
765 sym_node_t *col_node;
766 sym_node_t *table_node;
767 dict_table_t *table;
768 ulint i;
769
770 select_node->select_list = nullptr;
771
772 table_node = select_node->table_list;
773
774 while (table_node) {
775 table = table_node->table;
776
777 for (i = 0; i < table->get_n_user_cols(); i++) {
778 const char *col_name = table->get_col_name(i);
779
780 col_node = sym_tab_add_id(pars_sym_tab_global, (byte *)col_name,
781 ut_strlen(col_name));
782
783 select_node->select_list =
784 que_node_list_add_last(select_node->select_list, col_node);
785 }
786
787 table_node = static_cast<sym_node_t *>(que_node_get_next(table_node));
788 }
789 }
790
791 /** Parses a select list; creates a query graph node for the whole SELECT
792 statement.
793 @return own: select node in a query tree */
794 sel_node_t *pars_select_list(
795 que_node_t *select_list, /*!< in: select list */
796 sym_node_t *into_list) /*!< in: variables list or NULL */
797 {
798 sel_node_t *node;
799
800 node = sel_node_create(pars_sym_tab_global->heap);
801
802 node->select_list = select_list;
803 node->into_list = into_list;
804
805 pars_resolve_exp_list_variables_and_types(nullptr, into_list);
806
807 return (node);
808 }
809
810 /** Checks if the query is an aggregate query, in which case the selct list must
811 contain only aggregate function items. */
812 static void pars_check_aggregate(
813 sel_node_t *select_node) /*!< in: select node already containing
814 the select list */
815 {
816 que_node_t *exp_node;
817 func_node_t *func_node;
818 ulint n_nodes = 0;
819 ulint n_aggregate_nodes = 0;
820
821 exp_node = select_node->select_list;
822
823 while (exp_node) {
824 n_nodes++;
825
826 if (que_node_get_type(exp_node) == QUE_NODE_FUNC) {
827 func_node = static_cast<func_node_t *>(exp_node);
828
829 if (func_node->fclass == PARS_FUNC_AGGREGATE) {
830 n_aggregate_nodes++;
831 }
832 }
833
834 exp_node = que_node_get_next(exp_node);
835 }
836
837 if (n_aggregate_nodes > 0) {
838 ut_a(n_nodes == n_aggregate_nodes);
839
840 select_node->is_aggregate = true;
841 } else {
842 select_node->is_aggregate = false;
843 }
844 }
845
846 /** Parses a select statement.
847 @return own: select node in a query tree */
848 sel_node_t *pars_select_statement(
849 sel_node_t *select_node, /*!< in: select node already containing
850 the select list */
851 sym_node_t *table_list, /*!< in: table list */
852 que_node_t *search_cond, /*!< in: search condition or NULL */
853 pars_res_word_t *for_update, /*!< in: NULL or &pars_update_token */
854 pars_res_word_t *lock_shared, /*!< in: NULL or &pars_share_token */
855 order_node_t *order_by) /*!< in: NULL or an order-by node */
856 {
857 select_node->state = SEL_NODE_OPEN;
858
859 select_node->table_list = table_list;
860 select_node->n_tables = pars_retrieve_table_list_defs(table_list);
861
862 if (select_node->select_list == &pars_star_denoter) {
863 /* SELECT * FROM ... */
864 pars_select_all_columns(select_node);
865 }
866
867 if (select_node->into_list) {
868 ut_a(que_node_list_get_len(select_node->into_list) ==
869 que_node_list_get_len(select_node->select_list));
870 }
871
872 UT_LIST_INIT(select_node->copy_variables);
873
874 pars_resolve_exp_list_columns(table_list, select_node->select_list);
875 pars_resolve_exp_list_variables_and_types(select_node,
876 select_node->select_list);
877 pars_check_aggregate(select_node);
878
879 select_node->search_cond = search_cond;
880
881 if (search_cond) {
882 pars_resolve_exp_columns(table_list, search_cond);
883 pars_resolve_exp_variables_and_types(select_node, search_cond);
884 }
885
886 if (for_update) {
887 ut_a(!lock_shared);
888
889 select_node->set_x_locks = true;
890 select_node->row_lock_mode = LOCK_X;
891
892 select_node->consistent_read = false;
893 select_node->read_view = nullptr;
894 } else if (lock_shared) {
895 select_node->set_x_locks = false;
896 select_node->row_lock_mode = LOCK_S;
897
898 select_node->consistent_read = false;
899 select_node->read_view = nullptr;
900 } else {
901 select_node->set_x_locks = false;
902 select_node->row_lock_mode = LOCK_S;
903
904 select_node->consistent_read = true;
905 }
906
907 select_node->order_by = order_by;
908
909 if (order_by) {
910 pars_resolve_exp_columns(table_list, order_by->column);
911 }
912
913 /* The final value of the following fields depend on the environment
914 where the select statement appears: */
915
916 select_node->can_get_updated = false;
917 select_node->explicit_cursor = nullptr;
918
919 opt_search_plan(select_node);
920
921 return (select_node);
922 }
923
924 /** Parses a cursor declaration.
925 @return sym_node */
926 que_node_t *pars_cursor_declaration(
927 sym_node_t *sym_node, /*!< in: cursor id node in the symbol
928 table */
929 sel_node_t *select_node) /*!< in: select node */
930 {
931 sym_node->resolved = true;
932 sym_node->token_type = SYM_CURSOR;
933 sym_node->cursor_def = select_node;
934
935 select_node->state = SEL_NODE_CLOSED;
936 select_node->explicit_cursor = sym_node;
937
938 return (sym_node);
939 }
940
941 /** Parses a function declaration.
942 @return sym_node */
943 que_node_t *pars_function_declaration(
944 sym_node_t *sym_node) /*!< in: function id node in the symbol
945 table */
946 {
947 sym_node->resolved = true;
948 sym_node->token_type = SYM_FUNCTION;
949
950 /* Check that the function exists. */
951 ut_a(pars_info_lookup_user_func(pars_sym_tab_global->info, sym_node->name));
952
953 return (sym_node);
954 }
955
956 /** Parses a delete or update statement start.
957 @return own: update node in a query tree */
958 upd_node_t *pars_update_statement_start(
959 bool is_delete, /*!< in: true if delete */
960 sym_node_t *table_sym, /*!< in: table name node */
961 col_assign_node_t *col_assign_list) /*!< in: column assignment list, NULL
962 if delete */
963 {
964 upd_node_t *node;
965
966 node = upd_node_create(pars_sym_tab_global->heap);
967
968 node->is_delete = is_delete;
969
970 node->table_sym = table_sym;
971 node->col_assign_list = col_assign_list;
972
973 return (node);
974 }
975
976 /** Parses a column assignment in an update.
977 @return column assignment node */
978 col_assign_node_t *pars_column_assignment(
979 sym_node_t *column, /*!< in: column to assign */
980 que_node_t *exp) /*!< in: value to assign */
981 {
982 col_assign_node_t *node;
983
984 node = static_cast<col_assign_node_t *>(
985 mem_heap_alloc(pars_sym_tab_global->heap, sizeof(col_assign_node_t)));
986 node->common.type = QUE_NODE_COL_ASSIGNMENT;
987
988 node->col = column;
989 node->val = exp;
990
991 return (node);
992 }
993
994 /** Processes an update node assignment list. */
995 static void pars_process_assign_list(upd_node_t *node) /*!< in: update node */
996 {
997 col_assign_node_t *col_assign_list;
998 sym_node_t *table_sym;
999 col_assign_node_t *assign_node;
1000 upd_field_t *upd_field;
1001 dict_index_t *clust_index;
1002 sym_node_t *col_sym;
1003 ulint changes_ord_field;
1004 ulint changes_field_size;
1005 ulint n_assigns;
1006 ulint i;
1007
1008 table_sym = node->table_sym;
1009 col_assign_list = static_cast<col_assign_node_t *>(node->col_assign_list);
1010 clust_index = node->table->first_index();
1011
1012 assign_node = col_assign_list;
1013 n_assigns = 0;
1014
1015 while (assign_node) {
1016 pars_resolve_exp_columns(table_sym, assign_node->col);
1017 pars_resolve_exp_columns(table_sym, assign_node->val);
1018 pars_resolve_exp_variables_and_types(nullptr, assign_node->val);
1019 #if 0
1020 ut_a(dtype_get_mtype(
1021 dfield_get_type(que_node_get_val(
1022 assign_node->col)))
1023 == dtype_get_mtype(
1024 dfield_get_type(que_node_get_val(
1025 assign_node->val))));
1026 #endif
1027
1028 /* Add to the update node all the columns found in assignment
1029 values as columns to copy: therefore, true */
1030
1031 opt_find_all_cols(true, clust_index, &(node->columns), nullptr,
1032 assign_node->val);
1033 n_assigns++;
1034
1035 assign_node =
1036 static_cast<col_assign_node_t *>(que_node_get_next(assign_node));
1037 }
1038
1039 node->update = upd_create(n_assigns, pars_sym_tab_global->heap);
1040
1041 assign_node = col_assign_list;
1042
1043 changes_field_size = UPD_NODE_NO_SIZE_CHANGE;
1044
1045 for (i = 0; i < n_assigns; i++) {
1046 upd_field = upd_get_nth_field(node->update, i);
1047
1048 col_sym = assign_node->col;
1049
1050 const dict_col_t *const col = node->table->get_col(col_sym->col_no);
1051 upd_field_set_field_no(upd_field, dict_col_get_clust_pos(col, clust_index),
1052 clust_index);
1053 upd_field->exp = assign_node->val;
1054 col->copy_type(dfield_get_type(&upd_field->new_val));
1055
1056 if (!col->get_fixed_size(dict_table_is_comp(node->table))) {
1057 changes_field_size = 0;
1058 }
1059
1060 assign_node =
1061 static_cast<col_assign_node_t *>(que_node_get_next(assign_node));
1062 }
1063
1064 /* Find out if the update can modify an ordering field in any index */
1065
1066 changes_ord_field = UPD_NODE_NO_ORD_CHANGE;
1067
1068 if (row_upd_changes_some_index_ord_field_binary(node->table, node->update)) {
1069 changes_ord_field = 0;
1070 }
1071
1072 node->cmpl_info = changes_ord_field | changes_field_size;
1073 }
1074
1075 /** Parses an update or delete statement.
1076 @return own: update node in a query tree */
1077 upd_node_t *pars_update_statement(
1078 upd_node_t *node, /*!< in: update node */
1079 sym_node_t *cursor_sym, /*!< in: pointer to a cursor entry in
1080 the symbol table or NULL */
1081 que_node_t *search_cond) /*!< in: search condition or NULL */
1082 {
1083 sym_node_t *table_sym;
1084 sel_node_t *sel_node;
1085 plan_t *plan;
1086
1087 table_sym = node->table_sym;
1088
1089 pars_retrieve_table_def(table_sym);
1090 node->table = table_sym->table;
1091
1092 UT_LIST_INIT(node->columns);
1093
1094 /* Make the single table node into a list of table nodes of length 1 */
1095
1096 que_node_list_add_last(nullptr, table_sym);
1097
1098 if (cursor_sym) {
1099 pars_resolve_exp_variables_and_types(nullptr, cursor_sym);
1100
1101 sel_node = cursor_sym->alias->cursor_def;
1102
1103 node->searched_update = false;
1104 } else {
1105 sel_node = pars_select_list(nullptr, nullptr);
1106
1107 pars_select_statement(sel_node, table_sym, search_cond, nullptr,
1108 &pars_share_token, nullptr);
1109 node->searched_update = true;
1110 sel_node->common.parent = node;
1111 }
1112
1113 node->select = sel_node;
1114
1115 ut_a(!node->is_delete || (node->col_assign_list == nullptr));
1116 ut_a(node->is_delete || (node->col_assign_list != nullptr));
1117
1118 if (node->is_delete) {
1119 node->cmpl_info = 0;
1120 } else {
1121 pars_process_assign_list(node);
1122 }
1123
1124 if (node->searched_update) {
1125 node->has_clust_rec_x_lock = true;
1126 sel_node->set_x_locks = true;
1127 sel_node->row_lock_mode = LOCK_X;
1128 } else {
1129 node->has_clust_rec_x_lock = sel_node->set_x_locks;
1130 }
1131
1132 ut_a(sel_node->n_tables == 1);
1133 ut_a(sel_node->consistent_read == false);
1134 ut_a(sel_node->order_by == nullptr);
1135 ut_a(sel_node->is_aggregate == false);
1136
1137 sel_node->can_get_updated = true;
1138
1139 node->state = UPD_NODE_UPDATE_CLUSTERED;
1140
1141 plan = sel_node_get_nth_plan(sel_node, 0);
1142
1143 plan->no_prefetch = true;
1144
1145 if (!plan->index->is_clustered()) {
1146 plan->must_get_clust = true;
1147
1148 node->pcur = &(plan->clust_pcur);
1149 } else {
1150 node->pcur = &(plan->pcur);
1151 }
1152
1153 return (node);
1154 }
1155
1156 /** Parses an insert statement.
1157 @return own: update node in a query tree */
1158 ins_node_t *pars_insert_statement(
1159 sym_node_t *table_sym, /*!< in: table name node */
1160 que_node_t *values_list, /*!< in: value expression list or NULL */
1161 sel_node_t *select) /*!< in: select condition or NULL */
1162 {
1163 ins_node_t *node;
1164 dtuple_t *row;
1165 ulint ins_type;
1166
1167 ut_a(values_list || select);
1168 ut_a(!values_list || !select);
1169
1170 if (values_list) {
1171 ins_type = INS_VALUES;
1172 } else {
1173 ins_type = INS_SEARCHED;
1174 }
1175
1176 pars_retrieve_table_def(table_sym);
1177
1178 node = ins_node_create(ins_type, table_sym->table, pars_sym_tab_global->heap);
1179
1180 row = dtuple_create(pars_sym_tab_global->heap, node->table->get_n_cols());
1181
1182 dict_table_copy_types(row, table_sym->table);
1183
1184 ins_node_set_new_row(node, row);
1185
1186 node->select = select;
1187
1188 if (select) {
1189 select->common.parent = node;
1190
1191 ut_a(que_node_list_get_len(select->select_list) ==
1192 table_sym->table->get_n_user_cols());
1193 }
1194
1195 node->values_list = values_list;
1196
1197 if (node->values_list) {
1198 pars_resolve_exp_list_variables_and_types(nullptr, values_list);
1199
1200 ut_a(que_node_list_get_len(values_list) ==
1201 table_sym->table->get_n_user_cols());
1202 }
1203
1204 return (node);
1205 }
1206
1207 /** Set the type of a dfield. */
1208 static void pars_set_dfield_type(dfield_t *dfield, /*!< in: dfield */
1209 pars_res_word_t *type, /*!< in: pointer to a
1210 type token */
1211 ulint len, /*!< in: length, or 0 */
1212 bool is_unsigned, /*!< in: if true, column is
1213 UNSIGNED. */
1214 bool is_not_null) /*!< in: if true, column is
1215 NOT NULL. */
1216 {
1217 ulint flags = 0;
1218
1219 if (is_not_null) {
1220 flags |= DATA_NOT_NULL;
1221 }
1222
1223 if (is_unsigned) {
1224 flags |= DATA_UNSIGNED;
1225 }
1226
1227 if (type == &pars_bigint_token) {
1228 ut_a(len == 0);
1229
1230 dtype_set(dfield_get_type(dfield), DATA_INT, flags, 8);
1231 } else if (type == &pars_int_token) {
1232 ut_a(len == 0);
1233
1234 dtype_set(dfield_get_type(dfield), DATA_INT, flags, 4);
1235
1236 } else if (type == &pars_char_token) {
1237 // ut_a(len == 0);
1238
1239 dtype_set(dfield_get_type(dfield), DATA_VARCHAR, DATA_ENGLISH | flags, len);
1240 } else if (type == &pars_binary_token) {
1241 ut_a(len != 0);
1242
1243 dtype_set(dfield_get_type(dfield), DATA_FIXBINARY, DATA_BINARY_TYPE | flags,
1244 len);
1245 } else if (type == &pars_blob_token) {
1246 ut_a(len == 0);
1247
1248 dtype_set(dfield_get_type(dfield), DATA_BLOB, DATA_BINARY_TYPE | flags, 0);
1249 } else {
1250 ut_error;
1251 }
1252 }
1253
1254 /** Parses a variable declaration.
1255 @return own: symbol table node of type SYM_VAR */
1256 sym_node_t *pars_variable_declaration(
1257 sym_node_t *node, /*!< in: symbol table node allocated for the
1258 id of the variable */
1259 pars_res_word_t *type) /*!< in: pointer to a type token */
1260 {
1261 node->resolved = true;
1262 node->token_type = SYM_VAR;
1263
1264 node->param_type = PARS_NOT_PARAM;
1265
1266 pars_set_dfield_type(que_node_get_val(node), type, 0, false, false);
1267
1268 return (node);
1269 }
1270
1271 /** Parses a procedure parameter declaration.
1272 @return own: symbol table node of type SYM_VAR */
1273 sym_node_t *pars_parameter_declaration(
1274 sym_node_t *node, /*!< in: symbol table node allocated for the
1275 id of the parameter */
1276 ulint param_type,
1277 /*!< in: PARS_INPUT or PARS_OUTPUT */
1278 pars_res_word_t *type) /*!< in: pointer to a type token */
1279 {
1280 ut_a((param_type == PARS_INPUT) || (param_type == PARS_OUTPUT));
1281
1282 pars_variable_declaration(node, type);
1283
1284 node->param_type = param_type;
1285
1286 return (node);
1287 }
1288
1289 /** Sets the parent field in a query node list. */
1290 static void pars_set_parent_in_list(
1291 que_node_t *node_list, /*!< in: first node in a list */
1292 que_node_t *parent) /*!< in: parent value to set in all
1293 nodes of the list */
1294 {
1295 que_common_t *common;
1296
1297 common = static_cast<que_common_t *>(node_list);
1298
1299 while (common) {
1300 common->parent = parent;
1301
1302 common = static_cast<que_common_t *>(que_node_get_next(common));
1303 }
1304 }
1305
1306 /** Parses an elsif element.
1307 @return elsif node */
1308 elsif_node_t *pars_elsif_element(
1309 que_node_t *cond, /*!< in: if-condition */
1310 que_node_t *stat_list) /*!< in: statement list */
1311 {
1312 elsif_node_t *node;
1313
1314 node = static_cast<elsif_node_t *>(
1315 mem_heap_alloc(pars_sym_tab_global->heap, sizeof(elsif_node_t)));
1316
1317 node->common.type = QUE_NODE_ELSIF;
1318
1319 node->cond = cond;
1320
1321 pars_resolve_exp_variables_and_types(nullptr, cond);
1322
1323 node->stat_list = stat_list;
1324
1325 return (node);
1326 }
1327
1328 /** Parses an if-statement.
1329 @return if-statement node */
1330 if_node_t *pars_if_statement(
1331 que_node_t *cond, /*!< in: if-condition */
1332 que_node_t *stat_list, /*!< in: statement list */
1333 que_node_t *else_part) /*!< in: else-part statement list
1334 or elsif element list */
1335 {
1336 if_node_t *node;
1337 elsif_node_t *elsif_node;
1338
1339 node = static_cast<if_node_t *>(
1340 mem_heap_alloc(pars_sym_tab_global->heap, sizeof(if_node_t)));
1341
1342 node->common.type = QUE_NODE_IF;
1343
1344 node->cond = cond;
1345
1346 pars_resolve_exp_variables_and_types(nullptr, cond);
1347
1348 node->stat_list = stat_list;
1349
1350 if (else_part && (que_node_get_type(else_part) == QUE_NODE_ELSIF)) {
1351 /* There is a list of elsif conditions */
1352
1353 node->else_part = nullptr;
1354 node->elsif_list = static_cast<elsif_node_t *>(else_part);
1355
1356 elsif_node = static_cast<elsif_node_t *>(else_part);
1357
1358 while (elsif_node) {
1359 pars_set_parent_in_list(elsif_node->stat_list, node);
1360
1361 elsif_node = static_cast<elsif_node_t *>(que_node_get_next(elsif_node));
1362 }
1363 } else {
1364 node->else_part = else_part;
1365 node->elsif_list = nullptr;
1366
1367 pars_set_parent_in_list(else_part, node);
1368 }
1369
1370 pars_set_parent_in_list(stat_list, node);
1371
1372 return (node);
1373 }
1374
1375 /** Parses a while-statement.
1376 @return while-statement node */
1377 while_node_t *pars_while_statement(
1378 que_node_t *cond, /*!< in: while-condition */
1379 que_node_t *stat_list) /*!< in: statement list */
1380 {
1381 while_node_t *node;
1382
1383 node = static_cast<while_node_t *>(
1384 mem_heap_alloc(pars_sym_tab_global->heap, sizeof(while_node_t)));
1385
1386 node->common.type = QUE_NODE_WHILE;
1387
1388 node->cond = cond;
1389
1390 pars_resolve_exp_variables_and_types(nullptr, cond);
1391
1392 node->stat_list = stat_list;
1393
1394 pars_set_parent_in_list(stat_list, node);
1395
1396 return (node);
1397 }
1398
1399 /** Parses a for-loop-statement.
1400 @return for-statement node */
1401 for_node_t *pars_for_statement(
1402 sym_node_t *loop_var, /*!< in: loop variable */
1403 que_node_t *loop_start_limit, /*!< in: loop start expression */
1404 que_node_t *loop_end_limit, /*!< in: loop end expression */
1405 que_node_t *stat_list) /*!< in: statement list */
1406 {
1407 for_node_t *node;
1408
1409 node = static_cast<for_node_t *>(
1410 mem_heap_alloc(pars_sym_tab_global->heap, sizeof(for_node_t)));
1411
1412 node->common.type = QUE_NODE_FOR;
1413
1414 pars_resolve_exp_variables_and_types(nullptr, loop_var);
1415 pars_resolve_exp_variables_and_types(nullptr, loop_start_limit);
1416 pars_resolve_exp_variables_and_types(nullptr, loop_end_limit);
1417
1418 node->loop_var = loop_var->indirection;
1419
1420 ut_a(loop_var->indirection);
1421
1422 node->loop_start_limit = loop_start_limit;
1423 node->loop_end_limit = loop_end_limit;
1424
1425 node->stat_list = stat_list;
1426
1427 pars_set_parent_in_list(stat_list, node);
1428
1429 return (node);
1430 }
1431
1432 /** Parses an exit statement.
1433 @return exit statement node */
1434 exit_node_t *pars_exit_statement(void) {
1435 exit_node_t *node;
1436
1437 node = static_cast<exit_node_t *>(
1438 mem_heap_alloc(pars_sym_tab_global->heap, sizeof(exit_node_t)));
1439 node->common.type = QUE_NODE_EXIT;
1440
1441 return (node);
1442 }
1443
1444 /** Parses a return-statement.
1445 @return return-statement node */
1446 return_node_t *pars_return_statement(void) {
1447 return_node_t *node;
1448
1449 node = static_cast<return_node_t *>(
1450 mem_heap_alloc(pars_sym_tab_global->heap, sizeof(return_node_t)));
1451 node->common.type = QUE_NODE_RETURN;
1452
1453 return (node);
1454 }
1455
1456 /** Parses an assignment statement.
1457 @return assignment statement node */
1458 assign_node_t *pars_assignment_statement(
1459 sym_node_t *var, /*!< in: variable to assign */
1460 que_node_t *val) /*!< in: value to assign */
1461 {
1462 assign_node_t *node;
1463
1464 node = static_cast<assign_node_t *>(
1465 mem_heap_alloc(pars_sym_tab_global->heap, sizeof(assign_node_t)));
1466 node->common.type = QUE_NODE_ASSIGNMENT;
1467
1468 node->var = var;
1469 node->val = val;
1470
1471 pars_resolve_exp_variables_and_types(nullptr, var);
1472 pars_resolve_exp_variables_and_types(nullptr, val);
1473
1474 ut_a(dtype_get_mtype(dfield_get_type(que_node_get_val(var))) ==
1475 dtype_get_mtype(dfield_get_type(que_node_get_val(val))));
1476
1477 return (node);
1478 }
1479
1480 /** Parses a fetch statement. into_list or user_func (but not both) must be
1481 non-NULL.
1482 @return fetch statement node */
1483 fetch_node_t *pars_fetch_statement(
1484 sym_node_t *cursor, /*!< in: cursor node */
1485 sym_node_t *into_list, /*!< in: variables to set, or NULL */
1486 sym_node_t *user_func) /*!< in: user function name, or NULL */
1487 {
1488 sym_node_t *cursor_decl;
1489 fetch_node_t *node;
1490
1491 /* Logical XOR. */
1492 ut_a(!into_list != !user_func);
1493
1494 node = static_cast<fetch_node_t *>(
1495 mem_heap_alloc(pars_sym_tab_global->heap, sizeof(fetch_node_t)));
1496
1497 node->common.type = QUE_NODE_FETCH;
1498
1499 pars_resolve_exp_variables_and_types(nullptr, cursor);
1500
1501 if (into_list) {
1502 pars_resolve_exp_list_variables_and_types(nullptr, into_list);
1503 node->into_list = into_list;
1504 node->func = nullptr;
1505 } else {
1506 pars_resolve_exp_variables_and_types(nullptr, user_func);
1507
1508 node->func =
1509 pars_info_lookup_user_func(pars_sym_tab_global->info, user_func->name);
1510
1511 ut_a(node->func);
1512
1513 node->into_list = nullptr;
1514 }
1515
1516 cursor_decl = cursor->alias;
1517
1518 ut_a(cursor_decl->token_type == SYM_CURSOR);
1519
1520 node->cursor_def = cursor_decl->cursor_def;
1521
1522 if (into_list) {
1523 ut_a(que_node_list_get_len(into_list) ==
1524 que_node_list_get_len(node->cursor_def->select_list));
1525 }
1526
1527 return (node);
1528 }
1529
1530 /** Parses an open or close cursor statement.
1531 @return fetch statement node */
1532 open_node_t *pars_open_statement(ulint type, /*!< in: ROW_SEL_OPEN_CURSOR
1533 or ROW_SEL_CLOSE_CURSOR */
1534 sym_node_t *cursor) /*!< in: cursor node */
1535 {
1536 sym_node_t *cursor_decl;
1537 open_node_t *node;
1538
1539 node = static_cast<open_node_t *>(
1540 mem_heap_alloc(pars_sym_tab_global->heap, sizeof(open_node_t)));
1541
1542 node->common.type = QUE_NODE_OPEN;
1543
1544 pars_resolve_exp_variables_and_types(nullptr, cursor);
1545
1546 cursor_decl = cursor->alias;
1547
1548 ut_a(cursor_decl->token_type == SYM_CURSOR);
1549
1550 node->op_type = static_cast<open_node_op>(type);
1551 node->cursor_def = cursor_decl->cursor_def;
1552
1553 return (node);
1554 }
1555
1556 /** Parses a commit statement.
1557 @return own: commit node struct */
1558 commit_node_t *pars_commit_statement(void) {
1559 return (trx_commit_node_create(pars_sym_tab_global->heap));
1560 }
1561
1562 /** Parses a rollback statement.
1563 @return own: rollback node struct */
1564 roll_node_t *pars_rollback_statement(void) {
1565 return (roll_node_create(pars_sym_tab_global->heap));
1566 }
1567
1568 /** Parses a column definition at a table creation.
1569 @return column sym table node */
1570 sym_node_t *pars_column_def(sym_node_t *sym_node, /*!< in: column node in the
1571 symbol table */
1572 pars_res_word_t *type, /*!< in: data type */
1573 sym_node_t *len, /*!< in: length of column, or
1574 NULL */
1575 void *is_unsigned, /*!< in: if not NULL, column
1576 is of type UNSIGNED. */
1577 void *is_not_null) /*!< in: if not NULL, column
1578 is of type NOT NULL. */
1579 {
1580 ulint len2;
1581
1582 if (len) {
1583 len2 = eval_node_get_int_val(len);
1584 } else {
1585 len2 = 0;
1586 }
1587
1588 pars_set_dfield_type(que_node_get_val(sym_node), type, len2,
1589 is_unsigned != nullptr, is_not_null != nullptr);
1590
1591 return (sym_node);
1592 }
1593
1594 tab_node_t *pars_create_table(sym_node_t *, sym_node_t *, sym_node_t *,
1595 sym_node_t *, void *) {
1596 return (nullptr);
1597 }
1598
1599 ind_node_t *pars_create_index(pars_res_word_t *, pars_res_word_t *,
1600 sym_node_t *, sym_node_t *, sym_node_t *) {
1601 return (nullptr);
1602 }
1603
1604 /** Parses a procedure definition.
1605 @return query fork node */
1606 que_fork_t *pars_procedure_definition(
1607 sym_node_t *sym_node, /*!< in: procedure id node in the symbol
1608 table */
1609 sym_node_t *param_list, /*!< in: parameter declaration list */
1610 que_node_t *stat_list) /*!< in: statement list */
1611 {
1612 proc_node_t *node;
1613 que_fork_t *fork;
1614 que_thr_t *thr;
1615 mem_heap_t *heap;
1616
1617 heap = pars_sym_tab_global->heap;
1618
1619 fork = que_fork_create(nullptr, nullptr, QUE_FORK_PROCEDURE, heap);
1620 fork->trx = nullptr;
1621
1622 thr = que_thr_create(fork, heap, nullptr);
1623
1624 node = static_cast<proc_node_t *>(mem_heap_alloc(heap, sizeof(proc_node_t)));
1625
1626 node->common.type = QUE_NODE_PROC;
1627 node->common.parent = thr;
1628
1629 sym_node->token_type = SYM_PROCEDURE_NAME;
1630 sym_node->resolved = true;
1631
1632 node->proc_id = sym_node;
1633 node->param_list = param_list;
1634 node->stat_list = stat_list;
1635
1636 pars_set_parent_in_list(stat_list, node);
1637
1638 node->sym_tab = pars_sym_tab_global;
1639
1640 thr->child = node;
1641
1642 pars_sym_tab_global->query_graph = fork;
1643
1644 return (fork);
1645 }
1646
1647 /** Retrieves characters to the lexical analyzer.
1648 @param[in,out] buf Buffer where to copy
1649 @param[in] max_size Maximum number of characters which fit in the buffer
1650 @return number of characters copied or 0 on EOF
1651 */
1652 int pars_get_lex_chars(char *buf, size_t max_size) {
1653 int len;
1654
1655 len = static_cast<int>(pars_sym_tab_global->string_len -
1656 pars_sym_tab_global->next_char_pos);
1657 if (len == 0) {
1658 return (0);
1659 }
1660
1661 if (len > static_cast<int>(max_size)) {
1662 len = static_cast<int>(max_size);
1663 }
1664
1665 ut_memcpy(
1666 buf, pars_sym_tab_global->sql_string + pars_sym_tab_global->next_char_pos,
1667 len);
1668
1669 pars_sym_tab_global->next_char_pos += len;
1670
1671 return (len);
1672 }
1673
1674 /** Called by yyparse on error. */
1675 void yyerror(const char *s [[maybe_unused]])
1676 /*!< in: error message string */
1677 {
1678 ut_ad(s);
1679
1680 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_917)
1681 << "PARSER: Syntax error in SQL string";
1682 }
1683
1684 /** Parses an SQL string returning the query graph.
1685 @return own: the query graph */
1686 que_t *pars_sql(pars_info_t *info, /*!< in: extra information, or NULL */
1687 const char *str) /*!< in: SQL string */
1688 {
1689 mem_heap_t *heap;
1690 que_t *graph;
1691
1692 ut_ad(str);
1693
1694 heap = mem_heap_create(16000, UT_LOCATION_HERE);
1695
1696 /* Currently, the parser is not reentrant: */
1697 ut_ad(mutex_own(&pars_mutex));
1698 ut_ad(!dict_sys_mutex_own());
1699
1700 pars_sym_tab_global = sym_tab_create(heap);
1701
1702 pars_sym_tab_global->string_len = strlen(str);
1703 pars_sym_tab_global->sql_string = static_cast<char *>(
1704 mem_heap_dup(heap, str, pars_sym_tab_global->string_len + 1));
1705 pars_sym_tab_global->next_char_pos = 0;
1706 pars_sym_tab_global->info = info;
1707
1708 yyparse();
1709
1710 for (auto sym_node : pars_sym_tab_global->sym_list) {
1711 ut_a(sym_node->resolved);
1712 }
1713
1714 graph = pars_sym_tab_global->query_graph;
1715
1716 graph->sym_tab = pars_sym_tab_global;
1717 graph->info = info;
1718
1719 pars_sym_tab_global = nullptr;
1720
1721 /* fprintf(stderr, "SQL graph size %lu\n", mem_heap_get_size(heap)); */
1722
1723 return (graph);
1724 }
1725
1726 /** Completes a query graph by adding query thread and fork nodes
1727 above it and prepares the graph for running. The fork created is of
1728 type QUE_FORK_MYSQL_INTERFACE.
1729 @param[in] node root node for an incomplete query
1730 graph, or NULL for dummy graph
1731 @param[in] trx transaction handle
1732 @param[in] heap memory heap from which allocated
1733 @param[in] prebuilt row prebuilt structure
1734 @return query thread node to run */
1735 que_thr_t *pars_complete_graph_for_exec(que_node_t *node, trx_t *trx,
1736 mem_heap_t *heap,
1737 row_prebuilt_t *prebuilt) {
1738 que_fork_t *fork;
1739 que_thr_t *thr;
1740
1741 fork = que_fork_create(nullptr, nullptr, QUE_FORK_MYSQL_INTERFACE, heap);
1742 fork->trx = trx;
1743
1744 thr = que_thr_create(fork, heap, prebuilt);
1745
1746 thr->child = node;
1747
1748 if (node) {
1749 que_node_set_parent(node, thr);
1750 }
1751
1752 trx->graph = nullptr;
1753
1754 return (thr);
1755 }
1756
1757 /** Create parser info struct.
1758 @return own: info struct */
1759 pars_info_t *pars_info_create(void) {
1760 pars_info_t *info;
1761 mem_heap_t *heap;
1762
1763 heap = mem_heap_create(512, UT_LOCATION_HERE);
1764
1765 info = static_cast<pars_info_t *>(mem_heap_alloc(heap, sizeof(*info)));
1766
1767 info->heap = heap;
1768 info->funcs = nullptr;
1769 info->bound_lits = nullptr;
1770 info->bound_ids = nullptr;
1771 info->graph_owns_us = true;
1772
1773 return (info);
1774 }
1775
1776 /** Free info struct and everything it contains. */
1777 void pars_info_free(pars_info_t *info) /*!< in, own: info struct */
1778 {
1779 mem_heap_free(info->heap);
1780 }
1781
1782 /** Add bound literal.
1783 @param[in] info Info struct
1784 @param[in] name Name
1785 @param[in] address Address
1786 @param[in] length Length of data
1787 @param[in] type Type, e.g. data_fixbinary
1788 @param[in] prtype Precise type, e.g. data_unsigned */
1789 void pars_info_add_literal(pars_info_t *info, const char *name,
1790 const void *address, ulint length, ulint type,
1791 ulint prtype) {
1792 pars_bound_lit_t *pbl;
1793
1794 ut_ad(!pars_info_get_bound_lit(info, name));
1795
1796 pbl =
1797 static_cast<pars_bound_lit_t *>(mem_heap_alloc(info->heap, sizeof(*pbl)));
1798
1799 pbl->name = name;
1800
1801 pbl->address = address;
1802 pbl->length = length;
1803 pbl->type = type;
1804 pbl->prtype = prtype;
1805
1806 if (!info->bound_lits) {
1807 ib_alloc_t *heap_alloc;
1808
1809 heap_alloc = ib_heap_allocator_create(info->heap);
1810
1811 info->bound_lits = ib_vector_create(heap_alloc, sizeof(*pbl), 8);
1812 }
1813
1814 ib_vector_push(info->bound_lits, pbl);
1815 }
1816
1817 /** Equivalent to pars_info_add_literal(info, name, str, strlen(str),
1818 DATA_VARCHAR, DATA_ENGLISH). */
1819 void pars_info_add_str_literal(pars_info_t *info, /*!< in: info struct */
1820 const char *name, /*!< in: name */
1821 const char *str) /*!< in: string */
1822 {
1823 pars_info_add_literal(info, name, str, strlen(str), DATA_VARCHAR,
1824 DATA_ENGLISH);
1825 }
1826
1827 /********************************************************************
1828 If the literal value already exists then it rebinds otherwise it
1829 creates a new entry.*/
1830 void pars_info_bind_literal(pars_info_t *info, /* in: info struct */
1831 const char *name, /* in: name */
1832 const void *address, /* in: address */
1833 ulint length, /* in: length of data */
1834 ulint type, /* in: type, e.g. DATA_FIXBINARY */
1835 ulint prtype) /* in: precise type, e.g. */
1836 {
1837 pars_bound_lit_t *pbl;
1838
1839 pbl = pars_info_lookup_bound_lit(info, name);
1840
1841 if (!pbl) {
1842 pars_info_add_literal(info, name, address, length, type, prtype);
1843 } else {
1844 pbl->address = address;
1845 pbl->length = length;
1846
1847 sym_tab_rebind_lit(pbl->node, address, length);
1848 }
1849 }
1850
1851 /** If the literal value already exists then it rebinds otherwise it
1852 creates a new entry.
1853 @param[in] info Info struct
1854 @param[in] name Name
1855 @param[in] str String
1856 @param[in] str_len String length */
1857 void pars_info_bind_varchar_literal(pars_info_t *info, const char *name,
1858 const byte *str, ulint str_len) {
1859 pars_bound_lit_t *pbl;
1860
1861 pbl = pars_info_lookup_bound_lit(info, name);
1862
1863 if (!pbl) {
1864 pars_info_add_literal(info, name, str, str_len, DATA_VARCHAR, DATA_ENGLISH);
1865 } else {
1866 pbl->address = str;
1867 pbl->length = str_len;
1868
1869 sym_tab_rebind_lit(pbl->node, str, str_len);
1870 }
1871 }
1872
1873 /** Equivalent to:
1874
1875 char buf[4];
1876 mach_write_to_4(buf, val);
1877 pars_info_add_literal(info, name, buf, 4, DATA_INT, 0);
1878
1879 except that the buffer is dynamically allocated from the info struct's
1880 heap. */
1881 void pars_info_add_int4_literal(pars_info_t *info, /*!< in: info struct */
1882 const char *name, /*!< in: name */
1883 lint val) /*!< in: value */
1884 {
1885 byte *buf = static_cast<byte *>(mem_heap_alloc(info->heap, 4));
1886
1887 mach_write_to_4(buf, val);
1888 pars_info_add_literal(info, name, buf, 4, DATA_INT, 0);
1889 }
1890
1891 /** If the literal value already exists then it rebinds otherwise it creates a
1892 new entry.
1893 @param[in] info Info struct
1894 @param[in] name Name
1895 @param[in] val Value */
1896 void pars_info_bind_int4_literal(pars_info_t *info, const char *name,
1897 const uint32_t *val) {
1898 pars_bound_lit_t *pbl;
1899
1900 pbl = pars_info_lookup_bound_lit(info, name);
1901
1902 if (!pbl) {
1903 pars_info_add_literal(info, name, val, 4, DATA_INT, 0);
1904 } else {
1905 pbl->address = val;
1906 pbl->length = sizeof(*val);
1907
1908 sym_tab_rebind_lit(pbl->node, val, sizeof(*val));
1909 }
1910 }
1911
1912 /** If the literal value already exists then it rebinds otherwise it creates a
1913 new entry.
1914 @param[in] info Info struct
1915 @param[in] name Name
1916 @param[in] val Value */
1917 void pars_info_bind_int8_literal(pars_info_t *info, const char *name,
1918 const uint64_t *val) {
1919 pars_bound_lit_t *pbl;
1920
1921 pbl = pars_info_lookup_bound_lit(info, name);
1922
1923 if (!pbl) {
1924 pars_info_add_literal(info, name, val, sizeof(*val), DATA_INT, 0);
1925 } else {
1926 pbl->address = val;
1927 pbl->length = sizeof(*val);
1928
1929 sym_tab_rebind_lit(pbl->node, val, sizeof(*val));
1930 }
1931 }
1932
1933 /** Equivalent to:
1934
1935 char buf[8];
1936 mach_write_to_8(buf, val);
1937 pars_info_add_literal(info, name, buf, 8, DATA_FIXBINARY, 0);
1938
1939 except that the buffer is dynamically allocated from the info struct's
1940 heap. */
1941 void pars_info_add_ull_literal(pars_info_t *info, /*!< in: info struct */
1942 const char *name, /*!< in: name */
1943 uint64_t val) /*!< in: value */
1944 {
1945 byte *buf = static_cast<byte *>(mem_heap_alloc(info->heap, 8));
1946
1947 mach_write_to_8(buf, val);
1948
1949 pars_info_add_literal(info, name, buf, 8, DATA_FIXBINARY, 0);
1950 }
1951
1952 /** If the literal value already exists then it rebinds otherwise it
1953 creates a new entry.
1954 @param[in] info Info struct
1955 @param[in] name Name
1956 @param[in] val Value */
1957 void pars_info_bind_ull_literal(pars_info_t *info, const char *name,
1958 const uint64_t *val) {
1959 pars_bound_lit_t *pbl;
1960
1961 pbl = pars_info_lookup_bound_lit(info, name);
1962
1963 if (!pbl) {
1964 pars_info_add_literal(info, name, val, sizeof(*val), DATA_FIXBINARY, 0);
1965 } else {
1966 pbl->address = val;
1967 pbl->length = sizeof(*val);
1968
1969 sym_tab_rebind_lit(pbl->node, val, sizeof(*val));
1970 }
1971 }
1972
1973 /** Add user function.
1974 @param[in] info Info struct
1975 @param[in] name Function name
1976 @param[in] func Function address
1977 @param[in] arg User-supplied argument */
1978 void pars_info_bind_function(pars_info_t *info, const char *name,
1979 pars_user_func_cb_t func, void *arg) {
1980 pars_user_func_t *puf;
1981
1982 puf = pars_info_lookup_user_func(info, name);
1983
1984 if (!puf) {
1985 if (!info->funcs) {
1986 ib_alloc_t *heap_alloc;
1987
1988 heap_alloc = ib_heap_allocator_create(info->heap);
1989
1990 info->funcs = ib_vector_create(heap_alloc, sizeof(*puf), 8);
1991 }
1992
1993 /* Create a "new" element */
1994 puf = static_cast<pars_user_func_t *>(ib_vector_push(info->funcs, nullptr));
1995 puf->name = name;
1996 }
1997
1998 puf->arg = arg;
1999 puf->func = func;
2000 }
2001
2002 /** Add bound id.
2003 @param[in] info info struct
2004 @param[in] copy_name copy name if true
2005 @param[in] name name
2006 @param[in] id id */
2007 void pars_info_bind_id(pars_info_t *info, bool copy_name, const char *name,
2008 const char *id) {
2009 pars_bound_id_t *bid;
2010
2011 bid = pars_info_lookup_bound_id(info, name);
2012
2013 if (!bid) {
2014 if (!info->bound_ids) {
2015 ib_alloc_t *heap_alloc;
2016
2017 heap_alloc = ib_heap_allocator_create(info->heap);
2018
2019 info->bound_ids = ib_vector_create(heap_alloc, sizeof(*bid), 8);
2020 }
2021
2022 /* Create a "new" element */
2023 bid = static_cast<pars_bound_id_t *>(
2024 ib_vector_push(info->bound_ids, nullptr));
2025
2026 bid->name = copy_name ? mem_heap_strdup(info->heap, name) : name;
2027 }
2028
2029 bid->id = id;
2030 }
2031
2032 /** Get bound identifier with the given name.
2033 @param[in] info info struct
2034 @param[in] name bound id name to find
2035 @return bound id, or NULL if not found */
2036 pars_bound_id_t *pars_info_get_bound_id(pars_info_t *info, const char *name) {
2037 return (pars_info_lookup_bound_id(info, name));
2038 }
2039
2040 /** Get bound literal with the given name.
2041 @return bound literal, or NULL if not found */
2042 pars_bound_lit_t *pars_info_get_bound_lit(
2043 pars_info_t *info, /*!< in: info struct */
2044 const char *name) /*!< in: bound literal name to find */
2045 {
2046 return (pars_info_lookup_bound_lit(info, name));
2047 }