1
/*****************************************************************************
3
Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
17
*****************************************************************************/
19
/******************************************************
22
Created 11/19/1996 Heikki Tuuri
23
*******************************************************/
25
/* Historical note: Innobase executed its first SQL string (CREATE TABLE)
28
#include "pars0pars.h"
31
#include "pars0pars.ic"
37
#include "dict0dict.h"
39
#include "dict0crea.h"
43
#include "data0data.h"
44
#include "data0type.h"
47
#include "lock0lock.h"
48
#include "eval0eval.h"
51
/* If the following is set TRUE, the lexer will print the SQL string
54
ibool pars_print_lexed = FALSE;
55
#endif /* UNIV_SQL_DEBUG */
57
/* Global variable used while parsing a single procedure or query : the code is
59
UNIV_INTERN sym_tab_t* pars_sym_tab_global;
61
/* Global variables used to denote certain reserved words, used in
62
constructing the parsing tree */
64
UNIV_INTERN pars_res_word_t pars_to_char_token = {PARS_TO_CHAR_TOKEN};
65
UNIV_INTERN pars_res_word_t pars_to_number_token = {PARS_TO_NUMBER_TOKEN};
66
UNIV_INTERN pars_res_word_t pars_to_binary_token = {PARS_TO_BINARY_TOKEN};
67
UNIV_INTERN pars_res_word_t pars_binary_to_number_token = {PARS_BINARY_TO_NUMBER_TOKEN};
68
UNIV_INTERN pars_res_word_t pars_substr_token = {PARS_SUBSTR_TOKEN};
69
UNIV_INTERN pars_res_word_t pars_replstr_token = {PARS_REPLSTR_TOKEN};
70
UNIV_INTERN pars_res_word_t pars_concat_token = {PARS_CONCAT_TOKEN};
71
UNIV_INTERN pars_res_word_t pars_instr_token = {PARS_INSTR_TOKEN};
72
UNIV_INTERN pars_res_word_t pars_length_token = {PARS_LENGTH_TOKEN};
73
UNIV_INTERN pars_res_word_t pars_sysdate_token = {PARS_SYSDATE_TOKEN};
74
UNIV_INTERN pars_res_word_t pars_printf_token = {PARS_PRINTF_TOKEN};
75
UNIV_INTERN pars_res_word_t pars_assert_token = {PARS_ASSERT_TOKEN};
76
UNIV_INTERN pars_res_word_t pars_rnd_token = {PARS_RND_TOKEN};
77
UNIV_INTERN pars_res_word_t pars_rnd_str_token = {PARS_RND_STR_TOKEN};
78
UNIV_INTERN pars_res_word_t pars_count_token = {PARS_COUNT_TOKEN};
79
UNIV_INTERN pars_res_word_t pars_sum_token = {PARS_SUM_TOKEN};
80
UNIV_INTERN pars_res_word_t pars_distinct_token = {PARS_DISTINCT_TOKEN};
81
UNIV_INTERN pars_res_word_t pars_binary_token = {PARS_BINARY_TOKEN};
82
UNIV_INTERN pars_res_word_t pars_blob_token = {PARS_BLOB_TOKEN};
83
UNIV_INTERN pars_res_word_t pars_int_token = {PARS_INT_TOKEN};
84
UNIV_INTERN pars_res_word_t pars_char_token = {PARS_CHAR_TOKEN};
85
UNIV_INTERN pars_res_word_t pars_float_token = {PARS_FLOAT_TOKEN};
86
UNIV_INTERN pars_res_word_t pars_update_token = {PARS_UPDATE_TOKEN};
87
UNIV_INTERN pars_res_word_t pars_asc_token = {PARS_ASC_TOKEN};
88
UNIV_INTERN pars_res_word_t pars_desc_token = {PARS_DESC_TOKEN};
89
UNIV_INTERN pars_res_word_t pars_open_token = {PARS_OPEN_TOKEN};
90
UNIV_INTERN pars_res_word_t pars_close_token = {PARS_CLOSE_TOKEN};
91
UNIV_INTERN pars_res_word_t pars_share_token = {PARS_SHARE_TOKEN};
92
UNIV_INTERN pars_res_word_t pars_unique_token = {PARS_UNIQUE_TOKEN};
93
UNIV_INTERN pars_res_word_t pars_clustered_token = {PARS_CLUSTERED_TOKEN};
95
/* Global variable used to denote the '*' in SELECT * FROM.. */
96
#define PARS_STAR_DENOTER 12345678
97
UNIV_INTERN ulint pars_star_denoter = PARS_STAR_DENOTER;
99
/*************************************************************************
100
Reset and check parser variables. */
106
#ifdef UNIV_SQL_DEBUG
107
pars_print_lexed = FALSE;
108
#endif /* UNIV_SQL_DEBUG */
110
pars_lexer_var_init();
112
pars_sym_tab_global = NULL;
114
/* These should really be const, so we simply check that they
115
are set to the correct value. */
116
ut_a(pars_to_char_token.code == PARS_TO_CHAR_TOKEN);
117
ut_a(pars_to_number_token.code == PARS_TO_NUMBER_TOKEN);
118
ut_a(pars_to_binary_token.code == PARS_TO_BINARY_TOKEN);
119
ut_a(pars_binary_to_number_token.code == PARS_BINARY_TO_NUMBER_TOKEN);
120
ut_a(pars_substr_token.code == PARS_SUBSTR_TOKEN);
121
ut_a(pars_replstr_token.code == PARS_REPLSTR_TOKEN);
122
ut_a(pars_concat_token.code == PARS_CONCAT_TOKEN);
123
ut_a(pars_instr_token.code == PARS_INSTR_TOKEN);
124
ut_a(pars_length_token.code == PARS_LENGTH_TOKEN);
125
ut_a(pars_sysdate_token.code == PARS_SYSDATE_TOKEN);
126
ut_a(pars_printf_token.code == PARS_PRINTF_TOKEN);
127
ut_a(pars_assert_token.code == PARS_ASSERT_TOKEN);
128
ut_a(pars_rnd_token.code == PARS_RND_TOKEN);
129
ut_a(pars_rnd_str_token.code == PARS_RND_STR_TOKEN);
130
ut_a(pars_count_token.code == PARS_COUNT_TOKEN);
131
ut_a(pars_sum_token.code == PARS_SUM_TOKEN);
132
ut_a(pars_distinct_token.code == PARS_DISTINCT_TOKEN);
133
ut_a(pars_binary_token.code == PARS_BINARY_TOKEN);
134
ut_a(pars_blob_token.code == PARS_BLOB_TOKEN);
135
ut_a(pars_int_token.code == PARS_INT_TOKEN);
136
ut_a(pars_char_token.code == PARS_CHAR_TOKEN);
137
ut_a(pars_float_token.code == PARS_FLOAT_TOKEN);
138
ut_a(pars_update_token.code == PARS_UPDATE_TOKEN);
139
ut_a(pars_asc_token.code == PARS_ASC_TOKEN);
140
ut_a(pars_desc_token.code == PARS_DESC_TOKEN);
141
ut_a(pars_open_token.code == PARS_OPEN_TOKEN);
142
ut_a(pars_close_token.code == PARS_CLOSE_TOKEN);
143
ut_a(pars_share_token.code == PARS_SHARE_TOKEN);
144
ut_a(pars_unique_token.code == PARS_UNIQUE_TOKEN);
145
ut_a(pars_clustered_token.code == PARS_CLUSTERED_TOKEN);
147
pars_star_denoter = PARS_STAR_DENOTER;
150
/*************************************************************************
151
Determines the class of a function code. */
156
/* out: function class: PARS_FUNC_ARITH, ... */
157
int func) /* in: function code: '=', PARS_GE_TOKEN, ... */
160
case '+': case '-': case '*': case '/':
161
return(PARS_FUNC_ARITH);
163
case '=': case '<': case '>':
164
case PARS_GE_TOKEN: case PARS_LE_TOKEN: case PARS_NE_TOKEN:
165
return(PARS_FUNC_CMP);
167
case PARS_AND_TOKEN: case PARS_OR_TOKEN: case PARS_NOT_TOKEN:
168
return(PARS_FUNC_LOGICAL);
170
case PARS_COUNT_TOKEN: case PARS_SUM_TOKEN:
171
return(PARS_FUNC_AGGREGATE);
173
case PARS_TO_CHAR_TOKEN:
174
case PARS_TO_NUMBER_TOKEN:
175
case PARS_TO_BINARY_TOKEN:
176
case PARS_BINARY_TO_NUMBER_TOKEN:
177
case PARS_SUBSTR_TOKEN:
178
case PARS_CONCAT_TOKEN:
179
case PARS_LENGTH_TOKEN:
180
case PARS_INSTR_TOKEN:
181
case PARS_SYSDATE_TOKEN:
182
case PARS_NOTFOUND_TOKEN:
183
case PARS_PRINTF_TOKEN:
184
case PARS_ASSERT_TOKEN:
186
case PARS_RND_STR_TOKEN:
187
case PARS_REPLSTR_TOKEN:
188
return(PARS_FUNC_PREDEFINED);
191
return(PARS_FUNC_OTHER);
195
/*************************************************************************
196
Parses an operator or predefined function expression. */
201
/* out, own: function node in a query tree */
202
int func, /* in: function token code */
203
que_node_t* arg) /* in: first argument in the argument list */
207
node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(func_node_t));
209
node->common.type = QUE_NODE_FUNC;
210
dfield_set_data(&(node->common.val), NULL, 0);
211
node->common.val_buf_size = 0;
215
node->class = pars_func_get_class(func);
219
UT_LIST_ADD_LAST(func_node_list, pars_sym_tab_global->func_node_list,
224
/*************************************************************************
225
Parses a function expression. */
230
/* out, own: function node in a query tree */
231
que_node_t* res_word,/* in: function name reserved word */
232
que_node_t* arg) /* in: first argument in the argument list */
234
return(pars_func_low(((pars_res_word_t*)res_word)->code, arg));
237
/*************************************************************************
238
Parses an operator expression. */
243
/* out, own: function node in a query tree */
244
int func, /* in: operator token code */
245
que_node_t* arg1, /* in: first argument */
246
que_node_t* arg2) /* in: second argument or NULL for an unary
249
que_node_list_add_last(NULL, arg1);
252
que_node_list_add_last(arg1, arg2);
255
return(pars_func_low(func, arg1));
258
/*************************************************************************
259
Parses an ORDER BY clause. Order by a single column only is supported. */
264
/* out, own: order-by node in a query tree */
265
sym_node_t* column, /* in: column name */
266
pars_res_word_t* asc) /* in: &pars_asc_token or pars_desc_token */
270
node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(order_node_t));
272
node->common.type = QUE_NODE_ORDER;
274
node->column = column;
276
if (asc == &pars_asc_token) {
279
ut_a(asc == &pars_desc_token);
286
/*************************************************************************
287
Determine if a data type is a built-in string data type of the InnoDB
293
/* out: TRUE if string data type */
294
ulint mtype) /* in: main data type */
297
case DATA_VARCHAR: case DATA_CHAR:
298
case DATA_FIXBINARY: case DATA_BINARY:
305
/*************************************************************************
306
Resolves the data type of a function in an expression. The argument data
307
types must already be resolved. */
310
pars_resolve_func_data_type(
311
/*========================*/
312
func_node_t* node) /* in: function node */
316
ut_a(que_node_get_type(node) == QUE_NODE_FUNC);
320
switch (node->func) {
322
case '+': case '-': case '*': case '/':
323
/* Inherit the data type from the first argument (which must
324
not be the SQL null literal whose type is DATA_ERROR) */
326
dtype_copy(que_node_get_data_type(node),
327
que_node_get_data_type(arg));
329
ut_a(dtype_get_mtype(que_node_get_data_type(node))
333
case PARS_COUNT_TOKEN:
335
dtype_set(que_node_get_data_type(node), DATA_INT, 0, 4);
338
case PARS_TO_CHAR_TOKEN:
339
case PARS_RND_STR_TOKEN:
340
ut_a(dtype_get_mtype(que_node_get_data_type(arg)) == DATA_INT);
341
dtype_set(que_node_get_data_type(node), DATA_VARCHAR,
345
case PARS_TO_BINARY_TOKEN:
346
if (dtype_get_mtype(que_node_get_data_type(arg)) == DATA_INT) {
347
dtype_set(que_node_get_data_type(node), DATA_VARCHAR,
350
dtype_set(que_node_get_data_type(node), DATA_BINARY,
355
case PARS_TO_NUMBER_TOKEN:
356
case PARS_BINARY_TO_NUMBER_TOKEN:
357
case PARS_LENGTH_TOKEN:
358
case PARS_INSTR_TOKEN:
359
ut_a(pars_is_string_type(que_node_get_data_type(arg)->mtype));
360
dtype_set(que_node_get_data_type(node), DATA_INT, 0, 4);
363
case PARS_SYSDATE_TOKEN:
365
dtype_set(que_node_get_data_type(node), DATA_INT, 0, 4);
368
case PARS_SUBSTR_TOKEN:
369
case PARS_CONCAT_TOKEN:
370
ut_a(pars_is_string_type(que_node_get_data_type(arg)->mtype));
371
dtype_set(que_node_get_data_type(node), DATA_VARCHAR,
375
case '>': case '<': case '=':
382
case PARS_NOTFOUND_TOKEN:
384
/* We currently have no iboolean type: use integer type */
385
dtype_set(que_node_get_data_type(node), DATA_INT, 0, 4);
389
ut_a(dtype_get_mtype(que_node_get_data_type(arg)) == DATA_INT);
390
dtype_set(que_node_get_data_type(node), DATA_INT, 0, 4);
398
/*************************************************************************
399
Resolves the meaning of variables in an expression and the data types of
400
functions. It is an error if some identifier cannot be resolved here. */
403
pars_resolve_exp_variables_and_types(
404
/*=================================*/
405
sel_node_t* select_node, /* in: select node or NULL; if
406
this is not NULL then the variable
407
sym nodes are added to the
408
copy_variables list of select_node */
409
que_node_t* exp_node) /* in: expression */
411
func_node_t* func_node;
413
sym_node_t* sym_node;
418
if (que_node_get_type(exp_node) == QUE_NODE_FUNC) {
419
func_node = exp_node;
421
arg = func_node->args;
424
pars_resolve_exp_variables_and_types(select_node, arg);
426
arg = que_node_get_next(arg);
429
pars_resolve_func_data_type(func_node);
434
ut_a(que_node_get_type(exp_node) == QUE_NODE_SYMBOL);
438
if (sym_node->resolved) {
443
/* Not resolved yet: look in the symbol table for a variable
444
or a cursor or a function with the same name */
446
node = UT_LIST_GET_FIRST(pars_sym_tab_global->sym_list);
450
&& ((node->token_type == SYM_VAR)
451
|| (node->token_type == SYM_CURSOR)
452
|| (node->token_type == SYM_FUNCTION))
454
&& (sym_node->name_len == node->name_len)
455
&& (ut_memcmp(sym_node->name, node->name,
456
node->name_len) == 0)) {
458
/* Found a variable or a cursor declared with
464
node = UT_LIST_GET_NEXT(sym_list, node);
468
fprintf(stderr, "PARSER ERROR: Unresolved identifier %s\n",
474
sym_node->resolved = TRUE;
475
sym_node->token_type = SYM_IMPLICIT_VAR;
476
sym_node->alias = node;
477
sym_node->indirection = node;
480
UT_LIST_ADD_LAST(col_var_list, select_node->copy_variables,
484
dfield_set_type(que_node_get_val(sym_node),
485
que_node_get_data_type(node));
488
/*************************************************************************
489
Resolves the meaning of variables in an expression list. It is an error if
490
some identifier cannot be resolved here. Resolves also the data types of
494
pars_resolve_exp_list_variables_and_types(
495
/*======================================*/
496
sel_node_t* select_node, /* in: select node or NULL */
497
que_node_t* exp_node) /* in: expression list first node, or
501
pars_resolve_exp_variables_and_types(select_node, exp_node);
503
exp_node = que_node_get_next(exp_node);
507
/*************************************************************************
508
Resolves the columns in an expression. */
511
pars_resolve_exp_columns(
512
/*=====================*/
513
sym_node_t* table_node, /* in: first node in a table list */
514
que_node_t* exp_node) /* in: expression */
516
func_node_t* func_node;
518
sym_node_t* sym_node;
526
if (que_node_get_type(exp_node) == QUE_NODE_FUNC) {
527
func_node = exp_node;
529
arg = func_node->args;
532
pars_resolve_exp_columns(table_node, arg);
534
arg = que_node_get_next(arg);
540
ut_a(que_node_get_type(exp_node) == QUE_NODE_SYMBOL);
544
if (sym_node->resolved) {
549
/* Not resolved yet: look in the table list for a column with the
555
table = t_node->table;
557
n_cols = dict_table_get_n_cols(table);
559
for (i = 0; i < n_cols; i++) {
560
const dict_col_t* col
561
= dict_table_get_nth_col(table, i);
563
= dict_table_get_col_name(table, i);
565
if ((sym_node->name_len == ut_strlen(col_name))
566
&& (0 == ut_memcmp(sym_node->name, col_name,
567
sym_node->name_len))) {
569
sym_node->resolved = TRUE;
570
sym_node->token_type = SYM_COLUMN;
571
sym_node->table = table;
572
sym_node->col_no = i;
573
sym_node->prefetch_buf = NULL;
577
dfield_get_type(&sym_node
584
t_node = que_node_get_next(t_node);
588
/*************************************************************************
589
Resolves the meaning of columns in an expression list. */
592
pars_resolve_exp_list_columns(
593
/*==========================*/
594
sym_node_t* table_node, /* in: first node in a table list */
595
que_node_t* exp_node) /* in: expression list first node, or
599
pars_resolve_exp_columns(table_node, exp_node);
601
exp_node = que_node_get_next(exp_node);
605
/*************************************************************************
606
Retrieves the table definition for a table name id. */
609
pars_retrieve_table_def(
610
/*====================*/
611
sym_node_t* sym_node) /* in: table node */
613
const char* table_name;
616
ut_a(que_node_get_type(sym_node) == QUE_NODE_SYMBOL);
618
sym_node->resolved = TRUE;
619
sym_node->token_type = SYM_TABLE;
621
table_name = (const char*) sym_node->name;
623
sym_node->table = dict_table_get_low(table_name);
625
ut_a(sym_node->table);
628
/*************************************************************************
629
Retrieves the table definitions for a list of table name ids. */
632
pars_retrieve_table_list_defs(
633
/*==========================*/
634
/* out: number of tables */
635
sym_node_t* sym_node) /* in: first table node in list */
639
if (sym_node == NULL) {
645
pars_retrieve_table_def(sym_node);
649
sym_node = que_node_get_next(sym_node);
655
/*************************************************************************
656
Adds all columns to the select list if the query is SELECT * FROM ... */
659
pars_select_all_columns(
660
/*====================*/
661
sel_node_t* select_node) /* in: select node already containing
664
sym_node_t* col_node;
665
sym_node_t* table_node;
669
select_node->select_list = NULL;
671
table_node = select_node->table_list;
674
table = table_node->table;
676
for (i = 0; i < dict_table_get_n_user_cols(table); i++) {
677
const char* col_name = dict_table_get_col_name(
680
col_node = sym_tab_add_id(pars_sym_tab_global,
682
ut_strlen(col_name));
684
select_node->select_list = que_node_list_add_last(
685
select_node->select_list, col_node);
688
table_node = que_node_get_next(table_node);
692
/*************************************************************************
693
Parses a select list; creates a query graph node for the whole SELECT
699
/* out, own: select node in a query
701
que_node_t* select_list, /* in: select list */
702
sym_node_t* into_list) /* in: variables list or NULL */
706
node = sel_node_create(pars_sym_tab_global->heap);
708
node->select_list = select_list;
709
node->into_list = into_list;
711
pars_resolve_exp_list_variables_and_types(NULL, into_list);
716
/*************************************************************************
717
Checks if the query is an aggregate query, in which case the selct list must
718
contain only aggregate function items. */
721
pars_check_aggregate(
722
/*=================*/
723
sel_node_t* select_node) /* in: select node already containing
726
que_node_t* exp_node;
727
func_node_t* func_node;
729
ulint n_aggregate_nodes = 0;
731
exp_node = select_node->select_list;
737
if (que_node_get_type(exp_node) == QUE_NODE_FUNC) {
739
func_node = exp_node;
741
if (func_node->class == PARS_FUNC_AGGREGATE) {
747
exp_node = que_node_get_next(exp_node);
750
if (n_aggregate_nodes > 0) {
751
ut_a(n_nodes == n_aggregate_nodes);
753
select_node->is_aggregate = TRUE;
755
select_node->is_aggregate = FALSE;
759
/*************************************************************************
760
Parses a select statement. */
763
pars_select_statement(
764
/*==================*/
765
/* out, own: select node in a query
767
sel_node_t* select_node, /* in: select node already containing
769
sym_node_t* table_list, /* in: table list */
770
que_node_t* search_cond, /* in: search condition or NULL */
771
pars_res_word_t* for_update, /* in: NULL or &pars_update_token */
772
pars_res_word_t* lock_shared, /* in: NULL or &pars_share_token */
773
order_node_t* order_by) /* in: NULL or an order-by node */
775
select_node->state = SEL_NODE_OPEN;
777
select_node->table_list = table_list;
778
select_node->n_tables = pars_retrieve_table_list_defs(table_list);
780
if (select_node->select_list == &pars_star_denoter) {
782
/* SELECT * FROM ... */
783
pars_select_all_columns(select_node);
786
if (select_node->into_list) {
787
ut_a(que_node_list_get_len(select_node->into_list)
788
== que_node_list_get_len(select_node->select_list));
791
UT_LIST_INIT(select_node->copy_variables);
793
pars_resolve_exp_list_columns(table_list, select_node->select_list);
794
pars_resolve_exp_list_variables_and_types(select_node,
795
select_node->select_list);
796
pars_check_aggregate(select_node);
798
select_node->search_cond = search_cond;
801
pars_resolve_exp_columns(table_list, search_cond);
802
pars_resolve_exp_variables_and_types(select_node, search_cond);
808
select_node->set_x_locks = TRUE;
809
select_node->row_lock_mode = LOCK_X;
811
select_node->consistent_read = FALSE;
812
select_node->read_view = NULL;
813
} else if (lock_shared){
814
select_node->set_x_locks = FALSE;
815
select_node->row_lock_mode = LOCK_S;
817
select_node->consistent_read = FALSE;
818
select_node->read_view = NULL;
820
select_node->set_x_locks = FALSE;
821
select_node->row_lock_mode = LOCK_S;
823
select_node->consistent_read = TRUE;
826
select_node->order_by = order_by;
829
pars_resolve_exp_columns(table_list, order_by->column);
832
/* The final value of the following fields depend on the environment
833
where the select statement appears: */
835
select_node->can_get_updated = FALSE;
836
select_node->explicit_cursor = NULL;
838
opt_search_plan(select_node);
843
/*************************************************************************
844
Parses a cursor declaration. */
847
pars_cursor_declaration(
848
/*====================*/
850
sym_node_t* sym_node, /* in: cursor id node in the symbol
852
sel_node_t* select_node) /* in: select node */
854
sym_node->resolved = TRUE;
855
sym_node->token_type = SYM_CURSOR;
856
sym_node->cursor_def = select_node;
858
select_node->state = SEL_NODE_CLOSED;
859
select_node->explicit_cursor = sym_node;
864
/*************************************************************************
865
Parses a function declaration. */
868
pars_function_declaration(
869
/*======================*/
871
sym_node_t* sym_node) /* in: function id node in the symbol
874
sym_node->resolved = TRUE;
875
sym_node->token_type = SYM_FUNCTION;
877
/* Check that the function exists. */
878
ut_a(pars_info_get_user_func(pars_sym_tab_global->info,
884
/*************************************************************************
885
Parses a delete or update statement start. */
888
pars_update_statement_start(
889
/*========================*/
890
/* out, own: update node in a query
892
ibool is_delete, /* in: TRUE if delete */
893
sym_node_t* table_sym, /* in: table name node */
894
col_assign_node_t* col_assign_list)/* in: column assignment list, NULL
899
node = upd_node_create(pars_sym_tab_global->heap);
901
node->is_delete = is_delete;
903
node->table_sym = table_sym;
904
node->col_assign_list = col_assign_list;
909
/*************************************************************************
910
Parses a column assignment in an update. */
913
pars_column_assignment(
914
/*===================*/
915
/* out: column assignment node */
916
sym_node_t* column, /* in: column to assign */
917
que_node_t* exp) /* in: value to assign */
919
col_assign_node_t* node;
921
node = mem_heap_alloc(pars_sym_tab_global->heap,
922
sizeof(col_assign_node_t));
923
node->common.type = QUE_NODE_COL_ASSIGNMENT;
931
/*************************************************************************
932
Processes an update node assignment list. */
935
pars_process_assign_list(
936
/*=====================*/
937
upd_node_t* node) /* in: update node */
939
col_assign_node_t* col_assign_list;
940
sym_node_t* table_sym;
941
col_assign_node_t* assign_node;
942
upd_field_t* upd_field;
943
dict_index_t* clust_index;
945
ulint changes_ord_field;
946
ulint changes_field_size;
950
table_sym = node->table_sym;
951
col_assign_list = node->col_assign_list;
952
clust_index = dict_table_get_first_index(node->table);
954
assign_node = col_assign_list;
957
while (assign_node) {
958
pars_resolve_exp_columns(table_sym, assign_node->col);
959
pars_resolve_exp_columns(table_sym, assign_node->val);
960
pars_resolve_exp_variables_and_types(NULL, assign_node->val);
962
ut_a(dtype_get_mtype(
963
dfield_get_type(que_node_get_val(
966
dfield_get_type(que_node_get_val(
967
assign_node->val))));
970
/* Add to the update node all the columns found in assignment
971
values as columns to copy: therefore, TRUE */
973
opt_find_all_cols(TRUE, clust_index, &(node->columns), NULL,
977
assign_node = que_node_get_next(assign_node);
980
node->update = upd_create(n_assigns, pars_sym_tab_global->heap);
982
assign_node = col_assign_list;
984
changes_field_size = UPD_NODE_NO_SIZE_CHANGE;
986
for (i = 0; i < n_assigns; i++) {
987
upd_field = upd_get_nth_field(node->update, i);
989
col_sym = assign_node->col;
991
upd_field_set_field_no(upd_field, dict_index_get_nth_col_pos(
992
clust_index, col_sym->col_no),
994
upd_field->exp = assign_node->val;
996
if (!dict_col_get_fixed_size(
997
dict_index_get_nth_col(clust_index,
998
upd_field->field_no),
999
dict_table_is_comp(node->table))) {
1000
changes_field_size = 0;
1003
assign_node = que_node_get_next(assign_node);
1006
/* Find out if the update can modify an ordering field in any index */
1008
changes_ord_field = UPD_NODE_NO_ORD_CHANGE;
1010
if (row_upd_changes_some_index_ord_field_binary(node->table,
1012
changes_ord_field = 0;
1015
node->cmpl_info = changes_ord_field | changes_field_size;
1018
/*************************************************************************
1019
Parses an update or delete statement. */
1022
pars_update_statement(
1023
/*==================*/
1024
/* out, own: update node in a query
1026
upd_node_t* node, /* in: update node */
1027
sym_node_t* cursor_sym, /* in: pointer to a cursor entry in
1028
the symbol table or NULL */
1029
que_node_t* search_cond) /* in: search condition or NULL */
1031
sym_node_t* table_sym;
1032
sel_node_t* sel_node;
1035
table_sym = node->table_sym;
1037
pars_retrieve_table_def(table_sym);
1038
node->table = table_sym->table;
1040
UT_LIST_INIT(node->columns);
1042
/* Make the single table node into a list of table nodes of length 1 */
1044
que_node_list_add_last(NULL, table_sym);
1047
pars_resolve_exp_variables_and_types(NULL, cursor_sym);
1049
sel_node = cursor_sym->alias->cursor_def;
1051
node->searched_update = FALSE;
1053
sel_node = pars_select_list(NULL, NULL);
1055
pars_select_statement(sel_node, table_sym, search_cond, NULL,
1056
&pars_share_token, NULL);
1057
node->searched_update = TRUE;
1058
sel_node->common.parent = node;
1061
node->select = sel_node;
1063
ut_a(!node->is_delete || (node->col_assign_list == NULL));
1064
ut_a(node->is_delete || (node->col_assign_list != NULL));
1066
if (node->is_delete) {
1067
node->cmpl_info = 0;
1069
pars_process_assign_list(node);
1072
if (node->searched_update) {
1073
node->has_clust_rec_x_lock = TRUE;
1074
sel_node->set_x_locks = TRUE;
1075
sel_node->row_lock_mode = LOCK_X;
1077
node->has_clust_rec_x_lock = sel_node->set_x_locks;
1080
ut_a(sel_node->n_tables == 1);
1081
ut_a(sel_node->consistent_read == FALSE);
1082
ut_a(sel_node->order_by == NULL);
1083
ut_a(sel_node->is_aggregate == FALSE);
1085
sel_node->can_get_updated = TRUE;
1087
node->state = UPD_NODE_UPDATE_CLUSTERED;
1089
plan = sel_node_get_nth_plan(sel_node, 0);
1091
plan->no_prefetch = TRUE;
1093
if (!dict_index_is_clust(plan->index)) {
1095
plan->must_get_clust = TRUE;
1097
node->pcur = &(plan->clust_pcur);
1099
node->pcur = &(plan->pcur);
1105
/*************************************************************************
1106
Parses an insert statement. */
1109
pars_insert_statement(
1110
/*==================*/
1111
/* out, own: update node in a query
1113
sym_node_t* table_sym, /* in: table name node */
1114
que_node_t* values_list, /* in: value expression list or NULL */
1115
sel_node_t* select) /* in: select condition or NULL */
1121
ut_a(values_list || select);
1122
ut_a(!values_list || !select);
1125
ins_type = INS_VALUES;
1127
ins_type = INS_SEARCHED;
1130
pars_retrieve_table_def(table_sym);
1132
node = row_ins_node_create(ins_type, table_sym->table,
1133
pars_sym_tab_global->heap);
1135
row = dtuple_create(pars_sym_tab_global->heap,
1136
dict_table_get_n_cols(node->table));
1138
dict_table_copy_types(row, table_sym->table);
1140
ins_node_set_new_row(node, row);
1142
node->select = select;
1145
select->common.parent = node;
1147
ut_a(que_node_list_get_len(select->select_list)
1148
== dict_table_get_n_user_cols(table_sym->table));
1151
node->values_list = values_list;
1153
if (node->values_list) {
1154
pars_resolve_exp_list_variables_and_types(NULL, values_list);
1156
ut_a(que_node_list_get_len(values_list)
1157
== dict_table_get_n_user_cols(table_sym->table));
1163
/*************************************************************************
1164
Set the type of a dfield. */
1167
pars_set_dfield_type(
1168
/*=================*/
1169
dfield_t* dfield, /* in: dfield */
1170
pars_res_word_t* type, /* in: pointer to a type
1172
ulint len, /* in: length, or 0 */
1173
ibool is_unsigned, /* in: if TRUE, column is
1175
ibool is_not_null) /* in: if TRUE, column is
1181
flags |= DATA_NOT_NULL;
1185
flags |= DATA_UNSIGNED;
1188
if (type == &pars_int_token) {
1191
dtype_set(dfield_get_type(dfield), DATA_INT, flags, 4);
1193
} else if (type == &pars_char_token) {
1196
dtype_set(dfield_get_type(dfield), DATA_VARCHAR,
1197
DATA_ENGLISH | flags, 0);
1198
} else if (type == &pars_binary_token) {
1201
dtype_set(dfield_get_type(dfield), DATA_FIXBINARY,
1202
DATA_BINARY_TYPE | flags, len);
1203
} else if (type == &pars_blob_token) {
1206
dtype_set(dfield_get_type(dfield), DATA_BLOB,
1207
DATA_BINARY_TYPE | flags, 0);
1213
/*************************************************************************
1214
Parses a variable declaration. */
1217
pars_variable_declaration(
1218
/*======================*/
1219
/* out, own: symbol table node of type
1221
sym_node_t* node, /* in: symbol table node allocated for the
1222
id of the variable */
1223
pars_res_word_t* type) /* in: pointer to a type token */
1225
node->resolved = TRUE;
1226
node->token_type = SYM_VAR;
1228
node->param_type = PARS_NOT_PARAM;
1230
pars_set_dfield_type(que_node_get_val(node), type, 0, FALSE, FALSE);
1235
/*************************************************************************
1236
Parses a procedure parameter declaration. */
1239
pars_parameter_declaration(
1240
/*=======================*/
1241
/* out, own: symbol table node of type
1243
sym_node_t* node, /* in: symbol table node allocated for the
1244
id of the parameter */
1246
/* in: PARS_INPUT or PARS_OUTPUT */
1247
pars_res_word_t* type) /* in: pointer to a type token */
1249
ut_a((param_type == PARS_INPUT) || (param_type == PARS_OUTPUT));
1251
pars_variable_declaration(node, type);
1253
node->param_type = param_type;
1258
/*************************************************************************
1259
Sets the parent field in a query node list. */
1262
pars_set_parent_in_list(
1263
/*====================*/
1264
que_node_t* node_list, /* in: first node in a list */
1265
que_node_t* parent) /* in: parent value to set in all
1266
nodes of the list */
1268
que_common_t* common;
1273
common->parent = parent;
1275
common = que_node_get_next(common);
1279
/*************************************************************************
1280
Parses an elsif element. */
1285
/* out: elsif node */
1286
que_node_t* cond, /* in: if-condition */
1287
que_node_t* stat_list) /* in: statement list */
1291
node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(elsif_node_t));
1293
node->common.type = QUE_NODE_ELSIF;
1297
pars_resolve_exp_variables_and_types(NULL, cond);
1299
node->stat_list = stat_list;
1304
/*************************************************************************
1305
Parses an if-statement. */
1310
/* out: if-statement node */
1311
que_node_t* cond, /* in: if-condition */
1312
que_node_t* stat_list, /* in: statement list */
1313
que_node_t* else_part) /* in: else-part statement list
1314
or elsif element list */
1317
elsif_node_t* elsif_node;
1319
node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(if_node_t));
1321
node->common.type = QUE_NODE_IF;
1325
pars_resolve_exp_variables_and_types(NULL, cond);
1327
node->stat_list = stat_list;
1329
if (else_part && (que_node_get_type(else_part) == QUE_NODE_ELSIF)) {
1331
/* There is a list of elsif conditions */
1333
node->else_part = NULL;
1334
node->elsif_list = else_part;
1336
elsif_node = else_part;
1338
while (elsif_node) {
1339
pars_set_parent_in_list(elsif_node->stat_list, node);
1341
elsif_node = que_node_get_next(elsif_node);
1344
node->else_part = else_part;
1345
node->elsif_list = NULL;
1347
pars_set_parent_in_list(else_part, node);
1350
pars_set_parent_in_list(stat_list, node);
1355
/*************************************************************************
1356
Parses a while-statement. */
1359
pars_while_statement(
1360
/*=================*/
1361
/* out: while-statement node */
1362
que_node_t* cond, /* in: while-condition */
1363
que_node_t* stat_list) /* in: statement list */
1367
node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(while_node_t));
1369
node->common.type = QUE_NODE_WHILE;
1373
pars_resolve_exp_variables_and_types(NULL, cond);
1375
node->stat_list = stat_list;
1377
pars_set_parent_in_list(stat_list, node);
1382
/*************************************************************************
1383
Parses a for-loop-statement. */
1388
/* out: for-statement node */
1389
sym_node_t* loop_var, /* in: loop variable */
1390
que_node_t* loop_start_limit,/* in: loop start expression */
1391
que_node_t* loop_end_limit, /* in: loop end expression */
1392
que_node_t* stat_list) /* in: statement list */
1396
node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(for_node_t));
1398
node->common.type = QUE_NODE_FOR;
1400
pars_resolve_exp_variables_and_types(NULL, loop_var);
1401
pars_resolve_exp_variables_and_types(NULL, loop_start_limit);
1402
pars_resolve_exp_variables_and_types(NULL, loop_end_limit);
1404
node->loop_var = loop_var->indirection;
1406
ut_a(loop_var->indirection);
1408
node->loop_start_limit = loop_start_limit;
1409
node->loop_end_limit = loop_end_limit;
1411
node->stat_list = stat_list;
1413
pars_set_parent_in_list(stat_list, node);
1418
/*************************************************************************
1419
Parses an exit statement. */
1422
pars_exit_statement(void)
1423
/*=====================*/
1424
/* out: exit statement node */
1428
node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(exit_node_t));
1429
node->common.type = QUE_NODE_EXIT;
1434
/*************************************************************************
1435
Parses a return-statement. */
1438
pars_return_statement(void)
1439
/*=======================*/
1440
/* out: return-statement node */
1442
return_node_t* node;
1444
node = mem_heap_alloc(pars_sym_tab_global->heap,
1445
sizeof(return_node_t));
1446
node->common.type = QUE_NODE_RETURN;
1451
/*************************************************************************
1452
Parses an assignment statement. */
1455
pars_assignment_statement(
1456
/*======================*/
1457
/* out: assignment statement node */
1458
sym_node_t* var, /* in: variable to assign */
1459
que_node_t* val) /* in: value to assign */
1461
assign_node_t* node;
1463
node = mem_heap_alloc(pars_sym_tab_global->heap,
1464
sizeof(assign_node_t));
1465
node->common.type = QUE_NODE_ASSIGNMENT;
1470
pars_resolve_exp_variables_and_types(NULL, var);
1471
pars_resolve_exp_variables_and_types(NULL, val);
1473
ut_a(dtype_get_mtype(dfield_get_type(que_node_get_val(var)))
1474
== dtype_get_mtype(dfield_get_type(que_node_get_val(val))));
1479
/*************************************************************************
1480
Parses a procedure call. */
1483
pars_procedure_call(
1484
/*================*/
1485
/* out: function node */
1486
que_node_t* res_word,/* in: procedure name reserved word */
1487
que_node_t* args) /* in: argument list */
1491
node = pars_func(res_word, args);
1493
pars_resolve_exp_list_variables_and_types(NULL, args);
1498
/*************************************************************************
1499
Parses a fetch statement. into_list or user_func (but not both) must be
1503
pars_fetch_statement(
1504
/*=================*/
1505
/* out: fetch statement node */
1506
sym_node_t* cursor, /* in: cursor node */
1507
sym_node_t* into_list, /* in: variables to set, or NULL */
1508
sym_node_t* user_func) /* in: user function name, or NULL */
1510
sym_node_t* cursor_decl;
1514
ut_a(!into_list != !user_func);
1516
node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(fetch_node_t));
1518
node->common.type = QUE_NODE_FETCH;
1520
pars_resolve_exp_variables_and_types(NULL, cursor);
1523
pars_resolve_exp_list_variables_and_types(NULL, into_list);
1524
node->into_list = into_list;
1527
pars_resolve_exp_variables_and_types(NULL, user_func);
1529
node->func = pars_info_get_user_func(pars_sym_tab_global->info,
1533
node->into_list = NULL;
1536
cursor_decl = cursor->alias;
1538
ut_a(cursor_decl->token_type == SYM_CURSOR);
1540
node->cursor_def = cursor_decl->cursor_def;
1543
ut_a(que_node_list_get_len(into_list)
1544
== que_node_list_get_len(node->cursor_def->select_list));
1550
/*************************************************************************
1551
Parses an open or close cursor statement. */
1554
pars_open_statement(
1555
/*================*/
1556
/* out: fetch statement node */
1557
ulint type, /* in: ROW_SEL_OPEN_CURSOR
1558
or ROW_SEL_CLOSE_CURSOR */
1559
sym_node_t* cursor) /* in: cursor node */
1561
sym_node_t* cursor_decl;
1564
node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(open_node_t));
1566
node->common.type = QUE_NODE_OPEN;
1568
pars_resolve_exp_variables_and_types(NULL, cursor);
1570
cursor_decl = cursor->alias;
1572
ut_a(cursor_decl->token_type == SYM_CURSOR);
1574
node->op_type = type;
1575
node->cursor_def = cursor_decl->cursor_def;
1580
/*************************************************************************
1581
Parses a row_printf-statement. */
1584
pars_row_printf_statement(
1585
/*======================*/
1586
/* out: row_printf-statement node */
1587
sel_node_t* sel_node) /* in: select node */
1589
row_printf_node_t* node;
1591
node = mem_heap_alloc(pars_sym_tab_global->heap,
1592
sizeof(row_printf_node_t));
1593
node->common.type = QUE_NODE_ROW_PRINTF;
1595
node->sel_node = sel_node;
1597
sel_node->common.parent = node;
1602
/*************************************************************************
1603
Parses a commit statement. */
1606
pars_commit_statement(void)
1607
/*=======================*/
1609
return(commit_node_create(pars_sym_tab_global->heap));
1612
/*************************************************************************
1613
Parses a rollback statement. */
1616
pars_rollback_statement(void)
1617
/*=========================*/
1619
return(roll_node_create(pars_sym_tab_global->heap));
1622
/*************************************************************************
1623
Parses a column definition at a table creation. */
1628
/* out: column sym table
1630
sym_node_t* sym_node, /* in: column node in the
1632
pars_res_word_t* type, /* in: data type */
1633
sym_node_t* len, /* in: length of column, or
1635
void* is_unsigned, /* in: if not NULL, column
1636
is of type UNSIGNED. */
1637
void* is_not_null) /* in: if not NULL, column
1638
is of type NOT NULL. */
1643
len2 = eval_node_get_int_val(len);
1648
pars_set_dfield_type(que_node_get_val(sym_node), type, len2,
1649
is_unsigned != NULL, is_not_null != NULL);
1654
/*************************************************************************
1655
Parses a table creation operation. */
1660
/* out: table create subgraph */
1661
sym_node_t* table_sym, /* in: table name node in the symbol
1663
sym_node_t* column_defs, /* in: list of column names */
1664
void* not_fit_in_memory __attribute__((unused)))
1665
/* in: a non-NULL pointer means that
1666
this is a table which in simulations
1667
should be simulated as not fitting
1668
in memory; thread is put to sleep
1669
to simulate disk accesses; NOTE that
1670
this flag is not stored to the data
1671
dictionary on disk, and the database
1672
will forget about non-NULL value if
1673
it has to reload the table definition
1676
dict_table_t* table;
1679
const dtype_t* dtype;
1682
n_cols = que_node_list_get_len(column_defs);
1684
/* As the InnoDB SQL parser is for internal use only,
1685
for creating some system tables, this function will only
1686
create tables in the old (not compact) record format. */
1687
table = dict_mem_table_create(table_sym->name, 0, n_cols, 0);
1690
if (not_fit_in_memory != NULL) {
1691
table->does_not_fit_in_memory = TRUE;
1693
#endif /* UNIV_DEBUG */
1694
column = column_defs;
1697
dtype = dfield_get_type(que_node_get_val(column));
1699
dict_mem_table_add_col(table, table->heap,
1700
column->name, dtype->mtype,
1701
dtype->prtype, dtype->len);
1702
column->resolved = TRUE;
1703
column->token_type = SYM_COLUMN;
1705
column = que_node_get_next(column);
1708
node = tab_create_graph_create(table, pars_sym_tab_global->heap);
1710
table_sym->resolved = TRUE;
1711
table_sym->token_type = SYM_TABLE;
1716
/*************************************************************************
1717
Parses an index creation operation. */
1722
/* out: index create subgraph */
1723
pars_res_word_t* unique_def, /* in: not NULL if a unique index */
1724
pars_res_word_t* clustered_def, /* in: not NULL if a clustered index */
1725
sym_node_t* index_sym, /* in: index name node in the symbol
1727
sym_node_t* table_sym, /* in: table name node in the symbol
1729
sym_node_t* column_list) /* in: list of column names */
1731
dict_index_t* index;
1737
n_fields = que_node_list_get_len(column_list);
1742
ind_type = ind_type | DICT_UNIQUE;
1745
if (clustered_def) {
1746
ind_type = ind_type | DICT_CLUSTERED;
1749
index = dict_mem_index_create(table_sym->name, index_sym->name, 0,
1750
ind_type, n_fields);
1751
column = column_list;
1754
dict_mem_index_add_field(index, column->name, 0);
1756
column->resolved = TRUE;
1757
column->token_type = SYM_COLUMN;
1759
column = que_node_get_next(column);
1762
node = ind_create_graph_create(index, pars_sym_tab_global->heap);
1764
table_sym->resolved = TRUE;
1765
table_sym->token_type = SYM_TABLE;
1767
index_sym->resolved = TRUE;
1768
index_sym->token_type = SYM_TABLE;
1773
/*************************************************************************
1774
Parses a procedure definition. */
1777
pars_procedure_definition(
1778
/*======================*/
1779
/* out: query fork node */
1780
sym_node_t* sym_node, /* in: procedure id node in the symbol
1782
sym_node_t* param_list, /* in: parameter declaration list */
1783
que_node_t* stat_list) /* in: statement list */
1790
heap = pars_sym_tab_global->heap;
1792
fork = que_fork_create(NULL, NULL, QUE_FORK_PROCEDURE, heap);
1795
thr = que_thr_create(fork, heap);
1797
node = mem_heap_alloc(heap, sizeof(proc_node_t));
1799
node->common.type = QUE_NODE_PROC;
1800
node->common.parent = thr;
1802
sym_node->token_type = SYM_PROCEDURE_NAME;
1803
sym_node->resolved = TRUE;
1805
node->proc_id = sym_node;
1806
node->param_list = param_list;
1807
node->stat_list = stat_list;
1809
pars_set_parent_in_list(stat_list, node);
1811
node->sym_tab = pars_sym_tab_global;
1815
pars_sym_tab_global->query_graph = fork;
1820
/*****************************************************************
1821
Parses a stored procedure call, when this is not within another stored
1822
procedure, that is, the client issues a procedure call directly.
1823
In InnoDB, stored InnoDB procedures are invoked via the
1824
parsed procedure tree, not via InnoDB SQL, so this function is not used. */
1827
pars_stored_procedure_call(
1828
/*=======================*/
1829
/* out: query graph */
1830
sym_node_t* sym_node __attribute__((unused)))
1831
/* in: stored procedure name */
1837
/*****************************************************************
1838
Retrieves characters to the lexical analyzer. */
1843
char* buf, /* in/out: buffer where to copy */
1844
int* result, /* out: number of characters copied or EOF */
1845
int max_size) /* in: maximum number of characters which fit
1850
len = pars_sym_tab_global->string_len
1851
- pars_sym_tab_global->next_char_pos;
1854
/* fputs("SQL string ends\n", stderr); */
1861
if (len > max_size) {
1865
#ifdef UNIV_SQL_DEBUG
1866
if (pars_print_lexed) {
1872
fwrite(pars_sym_tab_global->sql_string
1873
+ pars_sym_tab_global->next_char_pos,
1876
#endif /* UNIV_SQL_DEBUG */
1878
ut_memcpy(buf, pars_sym_tab_global->sql_string
1879
+ pars_sym_tab_global->next_char_pos, len);
1882
pars_sym_tab_global->next_char_pos += len;
1885
/*****************************************************************
1886
Called by yyparse on error. */
1891
const char* s __attribute__((unused)))
1892
/* in: error message string */
1896
fputs("PARSER ERROR: Syntax error in SQL string\n", stderr);
1901
/*****************************************************************
1902
Parses an SQL string returning the query graph. */
1907
/* out, own: the query graph */
1908
pars_info_t* info, /* in: extra information, or NULL */
1909
const char* str) /* in: SQL string */
1911
sym_node_t* sym_node;
1917
heap = mem_heap_create(256);
1919
/* Currently, the parser is not reentrant: */
1920
ut_ad(mutex_own(&(dict_sys->mutex)));
1922
pars_sym_tab_global = sym_tab_create(heap);
1924
pars_sym_tab_global->string_len = strlen(str);
1925
pars_sym_tab_global->sql_string = mem_heap_dup(
1926
heap, str, pars_sym_tab_global->string_len + 1);
1927
pars_sym_tab_global->next_char_pos = 0;
1928
pars_sym_tab_global->info = info;
1932
sym_node = UT_LIST_GET_FIRST(pars_sym_tab_global->sym_list);
1935
ut_a(sym_node->resolved);
1937
sym_node = UT_LIST_GET_NEXT(sym_list, sym_node);
1940
graph = pars_sym_tab_global->query_graph;
1942
graph->sym_tab = pars_sym_tab_global;
1945
/* fprintf(stderr, "SQL graph size %lu\n", mem_heap_get_size(heap)); */
1950
/**********************************************************************
1951
Completes a query graph by adding query thread and fork nodes
1952
above it and prepares the graph for running. The fork created is of
1953
type QUE_FORK_USER_INTERFACE. */
1956
pars_complete_graph_for_exec(
1957
/*=========================*/
1958
/* out: query thread node to run */
1959
que_node_t* node, /* in: root node for an incomplete
1961
trx_t* trx, /* in: transaction handle */
1962
mem_heap_t* heap) /* in: memory heap from which allocated */
1967
fork = que_fork_create(NULL, NULL, QUE_FORK_USER_INTERFACE, heap);
1970
thr = que_thr_create(fork, heap);
1974
que_node_set_parent(node, thr);
1981
/********************************************************************
1982
Create parser info struct.*/
1985
pars_info_create(void)
1986
/*==================*/
1987
/* out, own: info struct */
1992
heap = mem_heap_create(512);
1994
info = mem_heap_alloc(heap, sizeof(*info));
1998
info->bound_lits = NULL;
1999
info->bound_ids = NULL;
2000
info->graph_owns_us = TRUE;
2005
/********************************************************************
2006
Free info struct and everything it contains.*/
2011
pars_info_t* info) /* in: info struct */
2013
mem_heap_free(info->heap);
2016
/********************************************************************
2017
Add bound literal. */
2020
pars_info_add_literal(
2021
/*==================*/
2022
pars_info_t* info, /* in: info struct */
2023
const char* name, /* in: name */
2024
const void* address, /* in: address */
2025
ulint length, /* in: length of data */
2026
ulint type, /* in: type, e.g. DATA_FIXBINARY */
2027
ulint prtype) /* in: precise type, e.g.
2030
pars_bound_lit_t* pbl;
2032
ut_ad(!pars_info_get_bound_lit(info, name));
2034
pbl = mem_heap_alloc(info->heap, sizeof(*pbl));
2037
pbl->address = address;
2038
pbl->length = length;
2040
pbl->prtype = prtype;
2042
if (!info->bound_lits) {
2043
info->bound_lits = ib_vector_create(info->heap, 8);
2046
ib_vector_push(info->bound_lits, pbl);
2049
/********************************************************************
2050
Equivalent to pars_info_add_literal(info, name, str, strlen(str),
2051
DATA_VARCHAR, DATA_ENGLISH). */
2054
pars_info_add_str_literal(
2055
/*======================*/
2056
pars_info_t* info, /* in: info struct */
2057
const char* name, /* in: name */
2058
const char* str) /* in: string */
2060
pars_info_add_literal(info, name, str, strlen(str),
2061
DATA_VARCHAR, DATA_ENGLISH);
2064
/********************************************************************
2068
mach_write_to_4(buf, val);
2069
pars_info_add_literal(info, name, buf, 4, DATA_INT, 0);
2071
except that the buffer is dynamically allocated from the info struct's
2075
pars_info_add_int4_literal(
2076
/*=======================*/
2077
pars_info_t* info, /* in: info struct */
2078
const char* name, /* in: name */
2079
lint val) /* in: value */
2081
byte* buf = mem_heap_alloc(info->heap, 4);
2083
mach_write_to_4(buf, val);
2084
pars_info_add_literal(info, name, buf, 4, DATA_INT, 0);
2087
/********************************************************************
2091
mach_write_ull(buf, val);
2092
pars_info_add_literal(info, name, buf, 8, DATA_INT, 0);
2094
except that the buffer is dynamically allocated from the info struct's
2098
pars_info_add_int8_literal(
2099
/*=======================*/
2100
pars_info_t* info, /* in: info struct */
2101
const char* name, /* in: name */
2102
ib_uint64_t val) /* in: value */
2104
byte* buf = mem_heap_alloc(info->heap, sizeof(val));
2106
mach_write_ull(buf, val);
2107
pars_info_add_literal(info, name, buf, sizeof(val), DATA_INT, 0);
2110
/********************************************************************
2114
mach_write_to_8(buf, val);
2115
pars_info_add_literal(info, name, buf, 8, DATA_FIXBINARY, 0);
2117
except that the buffer is dynamically allocated from the info struct's
2121
pars_info_add_dulint_literal(
2122
/*=========================*/
2123
pars_info_t* info, /* in: info struct */
2124
const char* name, /* in: name */
2125
dulint val) /* in: value */
2127
byte* buf = mem_heap_alloc(info->heap, 8);
2129
mach_write_to_8(buf, val);
2131
pars_info_add_literal(info, name, buf, 8, DATA_FIXBINARY, 0);
2134
/********************************************************************
2135
Add user function. */
2138
pars_info_add_function(
2139
/*===================*/
2140
pars_info_t* info, /* in: info struct */
2141
const char* name, /* in: function name */
2142
pars_user_func_cb_t func, /* in: function address */
2143
void* arg) /* in: user-supplied argument */
2145
pars_user_func_t* puf;
2147
ut_ad(!pars_info_get_user_func(info, name));
2149
puf = mem_heap_alloc(info->heap, sizeof(*puf));
2156
info->funcs = ib_vector_create(info->heap, 8);
2159
ib_vector_push(info->funcs, puf);
2162
/********************************************************************
2168
pars_info_t* info, /* in: info struct */
2169
const char* name, /* in: name */
2170
const char* id) /* in: id */
2172
pars_bound_id_t* bid;
2174
ut_ad(!pars_info_get_bound_id(info, name));
2176
bid = mem_heap_alloc(info->heap, sizeof(*bid));
2181
if (!info->bound_ids) {
2182
info->bound_ids = ib_vector_create(info->heap, 8);
2185
ib_vector_push(info->bound_ids, bid);
2188
/********************************************************************
2189
Get user function with the given name.*/
2192
pars_info_get_user_func(
2193
/*====================*/
2194
/* out: user func, or NULL if not
2196
pars_info_t* info, /* in: info struct */
2197
const char* name) /* in: function name to find*/
2202
if (!info || !info->funcs) {
2208
for (i = 0; i < ib_vector_size(vec); i++) {
2209
pars_user_func_t* puf = ib_vector_get(vec, i);
2211
if (strcmp(puf->name, name) == 0) {
2219
/********************************************************************
2220
Get bound literal with the given name.*/
2223
pars_info_get_bound_lit(
2224
/*====================*/
2225
/* out: bound literal, or NULL if
2227
pars_info_t* info, /* in: info struct */
2228
const char* name) /* in: bound literal name to find */
2233
if (!info || !info->bound_lits) {
2237
vec = info->bound_lits;
2239
for (i = 0; i < ib_vector_size(vec); i++) {
2240
pars_bound_lit_t* pbl = ib_vector_get(vec, i);
2242
if (strcmp(pbl->name, name) == 0) {
2250
/********************************************************************
2251
Get bound id with the given name.*/
2254
pars_info_get_bound_id(
2255
/*===================*/
2256
/* out: bound id, or NULL if not
2258
pars_info_t* info, /* in: info struct */
2259
const char* name) /* in: bound id name to find */
2264
if (!info || !info->bound_ids) {
2268
vec = info->bound_ids;
2270
for (i = 0; i < ib_vector_size(vec); i++) {
2271
pars_bound_id_t* bid = ib_vector_get(vec, i);
2273
if (strcmp(bid->name, name) == 0) {