pars0pars.c 53.6 KB
Newer Older
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*****************************************************************************

Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

*****************************************************************************/

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
19 20
/**************************************************//**
@file pars/pars0pars.c
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
SQL parser

Created 11/19/1996 Heikki Tuuri
*******************************************************/

/* Historical note: Innobase executed its first SQL string (CREATE TABLE)
on 1/27/1998 */

#include "pars0pars.h"

#ifdef UNIV_NONINL
#include "pars0pars.ic"
#endif

#include "row0sel.h"
#include "row0ins.h"
#include "row0upd.h"
#include "dict0dict.h"
#include "dict0mem.h"
#include "dict0crea.h"
#include "que0que.h"
#include "pars0grm.h"
#include "pars0opt.h"
#include "data0data.h"
#include "data0type.h"
#include "trx0trx.h"
#include "trx0roll.h"
#include "lock0lock.h"
#include "eval0eval.h"

#ifdef UNIV_SQL_DEBUG
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
52
/** If the following is set TRUE, the lexer will print the SQL string
53
as it tokenizes it */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
54
UNIV_INTERN ibool	pars_print_lexed	= FALSE;
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#endif /* UNIV_SQL_DEBUG */

/* Global variable used while parsing a single procedure or query : the code is
NOT re-entrant */
UNIV_INTERN sym_tab_t*	pars_sym_tab_global;

/* Global variables used to denote certain reserved words, used in
constructing the parsing tree */

UNIV_INTERN pars_res_word_t	pars_to_char_token = {PARS_TO_CHAR_TOKEN};
UNIV_INTERN pars_res_word_t	pars_to_number_token = {PARS_TO_NUMBER_TOKEN};
UNIV_INTERN pars_res_word_t	pars_to_binary_token = {PARS_TO_BINARY_TOKEN};
UNIV_INTERN pars_res_word_t	pars_binary_to_number_token = {PARS_BINARY_TO_NUMBER_TOKEN};
UNIV_INTERN pars_res_word_t	pars_substr_token = {PARS_SUBSTR_TOKEN};
UNIV_INTERN pars_res_word_t	pars_replstr_token = {PARS_REPLSTR_TOKEN};
UNIV_INTERN pars_res_word_t	pars_concat_token = {PARS_CONCAT_TOKEN};
UNIV_INTERN pars_res_word_t	pars_instr_token = {PARS_INSTR_TOKEN};
UNIV_INTERN pars_res_word_t	pars_length_token = {PARS_LENGTH_TOKEN};
UNIV_INTERN pars_res_word_t	pars_sysdate_token = {PARS_SYSDATE_TOKEN};
UNIV_INTERN pars_res_word_t	pars_printf_token = {PARS_PRINTF_TOKEN};
UNIV_INTERN pars_res_word_t	pars_assert_token = {PARS_ASSERT_TOKEN};
UNIV_INTERN pars_res_word_t	pars_rnd_token = {PARS_RND_TOKEN};
UNIV_INTERN pars_res_word_t	pars_rnd_str_token = {PARS_RND_STR_TOKEN};
UNIV_INTERN pars_res_word_t	pars_count_token = {PARS_COUNT_TOKEN};
UNIV_INTERN pars_res_word_t	pars_sum_token = {PARS_SUM_TOKEN};
UNIV_INTERN pars_res_word_t	pars_distinct_token = {PARS_DISTINCT_TOKEN};
UNIV_INTERN pars_res_word_t	pars_binary_token = {PARS_BINARY_TOKEN};
UNIV_INTERN pars_res_word_t	pars_blob_token = {PARS_BLOB_TOKEN};
UNIV_INTERN pars_res_word_t	pars_int_token = {PARS_INT_TOKEN};
UNIV_INTERN pars_res_word_t	pars_char_token = {PARS_CHAR_TOKEN};
UNIV_INTERN pars_res_word_t	pars_float_token = {PARS_FLOAT_TOKEN};
UNIV_INTERN pars_res_word_t	pars_update_token = {PARS_UPDATE_TOKEN};
UNIV_INTERN pars_res_word_t	pars_asc_token = {PARS_ASC_TOKEN};
UNIV_INTERN pars_res_word_t	pars_desc_token = {PARS_DESC_TOKEN};
UNIV_INTERN pars_res_word_t	pars_open_token = {PARS_OPEN_TOKEN};
UNIV_INTERN pars_res_word_t	pars_close_token = {PARS_CLOSE_TOKEN};
UNIV_INTERN pars_res_word_t	pars_share_token = {PARS_SHARE_TOKEN};
UNIV_INTERN pars_res_word_t	pars_unique_token = {PARS_UNIQUE_TOKEN};
UNIV_INTERN pars_res_word_t	pars_clustered_token = {PARS_CLUSTERED_TOKEN};

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
95 96
/** Global variable used to denote the '*' in SELECT * FROM.. */
UNIV_INTERN ulint	pars_star_denoter	= 12345678;
97 98


Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
99 100 101
/*********************************************************************//**
Determines the class of a function code.
@return	function class: PARS_FUNC_ARITH, ... */
102 103 104 105
static
ulint
pars_func_get_class(
/*================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
106
	int	func)	/*!< in: function code: '=', PARS_GE_TOKEN, ... */
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
{
	switch (func) {
	case '+': case '-': case '*': case '/':
		return(PARS_FUNC_ARITH);

	case '=': case '<': case '>':
	case PARS_GE_TOKEN: case PARS_LE_TOKEN: case PARS_NE_TOKEN:
		return(PARS_FUNC_CMP);

	case PARS_AND_TOKEN: case PARS_OR_TOKEN: case PARS_NOT_TOKEN:
		return(PARS_FUNC_LOGICAL);

	case PARS_COUNT_TOKEN: case PARS_SUM_TOKEN:
		return(PARS_FUNC_AGGREGATE);

	case PARS_TO_CHAR_TOKEN:
	case PARS_TO_NUMBER_TOKEN:
	case PARS_TO_BINARY_TOKEN:
	case PARS_BINARY_TO_NUMBER_TOKEN:
	case PARS_SUBSTR_TOKEN:
	case PARS_CONCAT_TOKEN:
	case PARS_LENGTH_TOKEN:
	case PARS_INSTR_TOKEN:
	case PARS_SYSDATE_TOKEN:
	case PARS_NOTFOUND_TOKEN:
	case PARS_PRINTF_TOKEN:
	case PARS_ASSERT_TOKEN:
	case PARS_RND_TOKEN:
	case PARS_RND_STR_TOKEN:
	case PARS_REPLSTR_TOKEN:
		return(PARS_FUNC_PREDEFINED);

	default:
		return(PARS_FUNC_OTHER);
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
144 145 146
/*********************************************************************//**
Parses an operator or predefined function expression.
@return	own: function node in a query tree */
147 148 149 150
static
func_node_t*
pars_func_low(
/*==========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
151 152
	int		func,	/*!< in: function token code */
	que_node_t*	arg)	/*!< in: first argument in the argument list */
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
{
	func_node_t*	node;

	node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(func_node_t));

	node->common.type = QUE_NODE_FUNC;
	dfield_set_data(&(node->common.val), NULL, 0);
	node->common.val_buf_size = 0;

	node->func = func;

	node->class = pars_func_get_class(func);

	node->args = arg;

	UT_LIST_ADD_LAST(func_node_list, pars_sym_tab_global->func_node_list,
			 node);
	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
173 174 175
/*********************************************************************//**
Parses a function expression.
@return	own: function node in a query tree */
176 177 178 179
UNIV_INTERN
func_node_t*
pars_func(
/*======*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
180 181
	que_node_t*	res_word,/*!< in: function name reserved word */
	que_node_t*	arg)	/*!< in: first argument in the argument list */
182 183 184 185
{
	return(pars_func_low(((pars_res_word_t*)res_word)->code, arg));
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
186 187 188
/*********************************************************************//**
Parses an operator expression.
@return	own: function node in a query tree */
189 190 191 192
UNIV_INTERN
func_node_t*
pars_op(
/*====*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
193 194 195
	int		func,	/*!< in: operator token code */
	que_node_t*	arg1,	/*!< in: first argument */
	que_node_t*	arg2)	/*!< in: second argument or NULL for an unary
196 197 198 199 200 201 202 203 204 205 206
				operator */
{
	que_node_list_add_last(NULL, arg1);

	if (arg2) {
		que_node_list_add_last(arg1, arg2);
	}

	return(pars_func_low(func, arg1));
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
207 208 209
/*********************************************************************//**
Parses an ORDER BY clause. Order by a single column only is supported.
@return	own: order-by node in a query tree */
210 211 212 213
UNIV_INTERN
order_node_t*
pars_order_by(
/*==========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
214 215
	sym_node_t*	column,	/*!< in: column name */
	pars_res_word_t* asc)	/*!< in: &pars_asc_token or pars_desc_token */
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
{
	order_node_t*	node;

	node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(order_node_t));

	node->common.type = QUE_NODE_ORDER;

	node->column = column;

	if (asc == &pars_asc_token) {
		node->asc = TRUE;
	} else {
		ut_a(asc == &pars_desc_token);
		node->asc = FALSE;
	}

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
235
/*********************************************************************//**
236
Determine if a data type is a built-in string data type of the InnoDB
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
237 238
SQL parser.
@return	TRUE if string data type */
239 240 241 242
static
ibool
pars_is_string_type(
/*================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
243
	ulint	mtype)	/*!< in: main data type */
244 245 246 247 248 249 250 251 252 253
{
	switch (mtype) {
	case DATA_VARCHAR: case DATA_CHAR:
	case DATA_FIXBINARY: case DATA_BINARY:
		return(TRUE);
	}

	return(FALSE);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
254
/*********************************************************************//**
255 256 257 258 259 260
Resolves the data type of a function in an expression. The argument data
types must already be resolved. */
static
void
pars_resolve_func_data_type(
/*========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
261
	func_node_t*	node)	/*!< in: function node */
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
{
	que_node_t*	arg;

	ut_a(que_node_get_type(node) == QUE_NODE_FUNC);

	arg = node->args;

	switch (node->func) {
	case PARS_SUM_TOKEN:
	case '+': case '-': case '*': case '/':
		/* Inherit the data type from the first argument (which must
		not be the SQL null literal whose type is DATA_ERROR) */

		dtype_copy(que_node_get_data_type(node),
			   que_node_get_data_type(arg));

		ut_a(dtype_get_mtype(que_node_get_data_type(node))
		     == DATA_INT);
		break;

	case PARS_COUNT_TOKEN:
		ut_a(arg);
		dtype_set(que_node_get_data_type(node), DATA_INT, 0, 4);
		break;

	case PARS_TO_CHAR_TOKEN:
	case PARS_RND_STR_TOKEN:
		ut_a(dtype_get_mtype(que_node_get_data_type(arg)) == DATA_INT);
		dtype_set(que_node_get_data_type(node), DATA_VARCHAR,
			  DATA_ENGLISH, 0);
		break;

	case PARS_TO_BINARY_TOKEN:
		if (dtype_get_mtype(que_node_get_data_type(arg)) == DATA_INT) {
			dtype_set(que_node_get_data_type(node), DATA_VARCHAR,
				  DATA_ENGLISH, 0);
		} else {
			dtype_set(que_node_get_data_type(node), DATA_BINARY,
				  0, 0);
		}
		break;

	case PARS_TO_NUMBER_TOKEN:
	case PARS_BINARY_TO_NUMBER_TOKEN:
	case PARS_LENGTH_TOKEN:
	case PARS_INSTR_TOKEN:
		ut_a(pars_is_string_type(que_node_get_data_type(arg)->mtype));
		dtype_set(que_node_get_data_type(node), DATA_INT, 0, 4);
		break;

	case PARS_SYSDATE_TOKEN:
		ut_a(arg == NULL);
		dtype_set(que_node_get_data_type(node), DATA_INT, 0, 4);
		break;

	case PARS_SUBSTR_TOKEN:
	case PARS_CONCAT_TOKEN:
		ut_a(pars_is_string_type(que_node_get_data_type(arg)->mtype));
		dtype_set(que_node_get_data_type(node), DATA_VARCHAR,
			  DATA_ENGLISH, 0);
		break;

	case '>': case '<': case '=':
	case PARS_GE_TOKEN:
	case PARS_LE_TOKEN:
	case PARS_NE_TOKEN:
	case PARS_AND_TOKEN:
	case PARS_OR_TOKEN:
	case PARS_NOT_TOKEN:
	case PARS_NOTFOUND_TOKEN:

		/* We currently have no iboolean type: use integer type */
		dtype_set(que_node_get_data_type(node), DATA_INT, 0, 4);
		break;

	case PARS_RND_TOKEN:
		ut_a(dtype_get_mtype(que_node_get_data_type(arg)) == DATA_INT);
		dtype_set(que_node_get_data_type(node), DATA_INT, 0, 4);
		break;

	default:
		ut_error;
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
347
/*********************************************************************//**
348 349 350 351 352 353
Resolves the meaning of variables in an expression and the data types of
functions. It is an error if some identifier cannot be resolved here. */
static
void
pars_resolve_exp_variables_and_types(
/*=================================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
354
	sel_node_t*	select_node,	/*!< in: select node or NULL; if
355 356 357
					this is not NULL then the variable
					sym nodes are added to the
					copy_variables list of select_node */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
358
	que_node_t*	exp_node)	/*!< in: expression */
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
{
	func_node_t*	func_node;
	que_node_t*	arg;
	sym_node_t*	sym_node;
	sym_node_t*	node;

	ut_a(exp_node);

	if (que_node_get_type(exp_node) == QUE_NODE_FUNC) {
		func_node = exp_node;

		arg = func_node->args;

		while (arg) {
			pars_resolve_exp_variables_and_types(select_node, arg);

			arg = que_node_get_next(arg);
		}

		pars_resolve_func_data_type(func_node);

		return;
	}

	ut_a(que_node_get_type(exp_node) == QUE_NODE_SYMBOL);

	sym_node = exp_node;

	if (sym_node->resolved) {

		return;
	}

	/* Not resolved yet: look in the symbol table for a variable
	or a cursor or a function with the same name */

	node = UT_LIST_GET_FIRST(pars_sym_tab_global->sym_list);

	while (node) {
		if (node->resolved
		    && ((node->token_type == SYM_VAR)
			|| (node->token_type == SYM_CURSOR)
			|| (node->token_type == SYM_FUNCTION))
		    && node->name
		    && (sym_node->name_len == node->name_len)
		    && (ut_memcmp(sym_node->name, node->name,
				  node->name_len) == 0)) {

			/* Found a variable or a cursor declared with
			the same name */

			break;
		}

		node = UT_LIST_GET_NEXT(sym_list, node);
	}

	if (!node) {
		fprintf(stderr, "PARSER ERROR: Unresolved identifier %s\n",
			sym_node->name);
	}

	ut_a(node);

	sym_node->resolved = TRUE;
	sym_node->token_type = SYM_IMPLICIT_VAR;
	sym_node->alias = node;
	sym_node->indirection = node;

	if (select_node) {
		UT_LIST_ADD_LAST(col_var_list, select_node->copy_variables,
				 sym_node);
	}

	dfield_set_type(que_node_get_val(sym_node),
			que_node_get_data_type(node));
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
437
/*********************************************************************//**
438 439 440 441 442 443 444
Resolves the meaning of variables in an expression list. It is an error if
some identifier cannot be resolved here. Resolves also the data types of
functions. */
static
void
pars_resolve_exp_list_variables_and_types(
/*======================================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
445 446
	sel_node_t*	select_node,	/*!< in: select node or NULL */
	que_node_t*	exp_node)	/*!< in: expression list first node, or
447 448 449 450 451 452 453 454 455
					NULL */
{
	while (exp_node) {
		pars_resolve_exp_variables_and_types(select_node, exp_node);

		exp_node = que_node_get_next(exp_node);
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
456
/*********************************************************************//**
457 458 459 460 461
Resolves the columns in an expression. */
static
void
pars_resolve_exp_columns(
/*=====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
462 463
	sym_node_t*	table_node,	/*!< in: first node in a table list */
	que_node_t*	exp_node)	/*!< in: expression */
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
{
	func_node_t*	func_node;
	que_node_t*	arg;
	sym_node_t*	sym_node;
	dict_table_t*	table;
	sym_node_t*	t_node;
	ulint		n_cols;
	ulint		i;

	ut_a(exp_node);

	if (que_node_get_type(exp_node) == QUE_NODE_FUNC) {
		func_node = exp_node;

		arg = func_node->args;

		while (arg) {
			pars_resolve_exp_columns(table_node, arg);

			arg = que_node_get_next(arg);
		}

		return;
	}

	ut_a(que_node_get_type(exp_node) == QUE_NODE_SYMBOL);

	sym_node = exp_node;

	if (sym_node->resolved) {

		return;
	}

	/* Not resolved yet: look in the table list for a column with the
	same name */

	t_node = table_node;

	while (t_node) {
		table = t_node->table;

		n_cols = dict_table_get_n_cols(table);

		for (i = 0; i < n_cols; i++) {
			const dict_col_t*	col
				= dict_table_get_nth_col(table, i);
			const char*		col_name
				= dict_table_get_col_name(table, i);

			if ((sym_node->name_len == ut_strlen(col_name))
			    && (0 == ut_memcmp(sym_node->name, col_name,
					       sym_node->name_len))) {
				/* Found */
				sym_node->resolved = TRUE;
				sym_node->token_type = SYM_COLUMN;
				sym_node->table = table;
				sym_node->col_no = i;
				sym_node->prefetch_buf = NULL;

				dict_col_copy_type(
					col,
					dfield_get_type(&sym_node
							->common.val));

				return;
			}
		}

		t_node = que_node_get_next(t_node);
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
537
/*********************************************************************//**
538 539 540 541 542
Resolves the meaning of columns in an expression list. */
static
void
pars_resolve_exp_list_columns(
/*==========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
543 544
	sym_node_t*	table_node,	/*!< in: first node in a table list */
	que_node_t*	exp_node)	/*!< in: expression list first node, or
545 546 547 548 549 550 551 552 553
					NULL */
{
	while (exp_node) {
		pars_resolve_exp_columns(table_node, exp_node);

		exp_node = que_node_get_next(exp_node);
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
554
/*********************************************************************//**
555 556 557 558 559
Retrieves the table definition for a table name id. */
static
void
pars_retrieve_table_def(
/*====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
560
	sym_node_t*	sym_node)	/*!< in: table node */
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
{
	const char*	table_name;

	ut_a(sym_node);
	ut_a(que_node_get_type(sym_node) == QUE_NODE_SYMBOL);

	sym_node->resolved = TRUE;
	sym_node->token_type = SYM_TABLE;

	table_name = (const char*) sym_node->name;

	sym_node->table = dict_table_get_low(table_name);

	ut_a(sym_node->table);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
577 578 579
/*********************************************************************//**
Retrieves the table definitions for a list of table name ids.
@return	number of tables */
580 581 582 583
static
ulint
pars_retrieve_table_list_defs(
/*==========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
584
	sym_node_t*	sym_node)	/*!< in: first table node in list */
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
{
	ulint		count		= 0;

	if (sym_node == NULL) {

		return(count);
	}

	while (sym_node) {
		pars_retrieve_table_def(sym_node);

		count++;

		sym_node = que_node_get_next(sym_node);
	}

	return(count);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
604
/*********************************************************************//**
605 606 607 608 609
Adds all columns to the select list if the query is SELECT * FROM ... */
static
void
pars_select_all_columns(
/*====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
610
	sel_node_t*	select_node)	/*!< in: select node already containing
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
					the table list */
{
	sym_node_t*	col_node;
	sym_node_t*	table_node;
	dict_table_t*	table;
	ulint		i;

	select_node->select_list = NULL;

	table_node = select_node->table_list;

	while (table_node) {
		table = table_node->table;

		for (i = 0; i < dict_table_get_n_user_cols(table); i++) {
			const char*	col_name = dict_table_get_col_name(
				table, i);

			col_node = sym_tab_add_id(pars_sym_tab_global,
						  (byte*)col_name,
						  ut_strlen(col_name));

			select_node->select_list = que_node_list_add_last(
				select_node->select_list, col_node);
		}

		table_node = que_node_get_next(table_node);
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
641
/*********************************************************************//**
642
Parses a select list; creates a query graph node for the whole SELECT
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
643 644
statement.
@return	own: select node in a query tree */
645 646 647 648
UNIV_INTERN
sel_node_t*
pars_select_list(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
649 650
	que_node_t*	select_list,	/*!< in: select list */
	sym_node_t*	into_list)	/*!< in: variables list or NULL */
651 652 653 654 655 656 657 658 659 660 661 662 663
{
	sel_node_t*	node;

	node = sel_node_create(pars_sym_tab_global->heap);

	node->select_list = select_list;
	node->into_list = into_list;

	pars_resolve_exp_list_variables_and_types(NULL, into_list);

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
664
/*********************************************************************//**
665 666 667 668 669 670
Checks if the query is an aggregate query, in which case the selct list must
contain only aggregate function items. */
static
void
pars_check_aggregate(
/*=================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
671
	sel_node_t*	select_node)	/*!< in: select node already containing
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
					the select list */
{
	que_node_t*	exp_node;
	func_node_t*	func_node;
	ulint		n_nodes			= 0;
	ulint		n_aggregate_nodes	= 0;

	exp_node = select_node->select_list;

	while (exp_node) {

		n_nodes++;

		if (que_node_get_type(exp_node) == QUE_NODE_FUNC) {

			func_node = exp_node;

			if (func_node->class == PARS_FUNC_AGGREGATE) {

				n_aggregate_nodes++;
			}
		}

		exp_node = que_node_get_next(exp_node);
	}

	if (n_aggregate_nodes > 0) {
		ut_a(n_nodes == n_aggregate_nodes);

		select_node->is_aggregate = TRUE;
	} else {
		select_node->is_aggregate = FALSE;
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
707 708 709
/*********************************************************************//**
Parses a select statement.
@return	own: select node in a query tree */
710 711 712 713
UNIV_INTERN
sel_node_t*
pars_select_statement(
/*==================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
714
	sel_node_t*	select_node,	/*!< in: select node already containing
715
					the select list */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
716 717 718 719 720
	sym_node_t*	table_list,	/*!< in: table list */
	que_node_t*	search_cond,	/*!< in: search condition or NULL */
	pars_res_word_t* for_update,	/*!< in: NULL or &pars_update_token */
	pars_res_word_t* lock_shared,	/*!< in: NULL or &pars_share_token */
	order_node_t*	order_by)	/*!< in: NULL or an order-by node */
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
{
	select_node->state = SEL_NODE_OPEN;

	select_node->table_list = table_list;
	select_node->n_tables = pars_retrieve_table_list_defs(table_list);

	if (select_node->select_list == &pars_star_denoter) {

		/* SELECT * FROM ... */
		pars_select_all_columns(select_node);
	}

	if (select_node->into_list) {
		ut_a(que_node_list_get_len(select_node->into_list)
		     == que_node_list_get_len(select_node->select_list));
	}

	UT_LIST_INIT(select_node->copy_variables);

	pars_resolve_exp_list_columns(table_list, select_node->select_list);
	pars_resolve_exp_list_variables_and_types(select_node,
						  select_node->select_list);
	pars_check_aggregate(select_node);

	select_node->search_cond = search_cond;

	if (search_cond) {
		pars_resolve_exp_columns(table_list, search_cond);
		pars_resolve_exp_variables_and_types(select_node, search_cond);
	}

	if (for_update) {
		ut_a(!lock_shared);

		select_node->set_x_locks = TRUE;
		select_node->row_lock_mode = LOCK_X;

		select_node->consistent_read = FALSE;
		select_node->read_view = NULL;
	} else if (lock_shared){
		select_node->set_x_locks = FALSE;
		select_node->row_lock_mode = LOCK_S;

		select_node->consistent_read = FALSE;
		select_node->read_view = NULL;
	} else {
		select_node->set_x_locks = FALSE;
		select_node->row_lock_mode = LOCK_S;

		select_node->consistent_read = TRUE;
	}

	select_node->order_by = order_by;

	if (order_by) {
		pars_resolve_exp_columns(table_list, order_by->column);
	}

	/* The final value of the following fields depend on the environment
	where the select statement appears: */

	select_node->can_get_updated = FALSE;
	select_node->explicit_cursor = NULL;

	opt_search_plan(select_node);

	return(select_node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
790 791 792
/*********************************************************************//**
Parses a cursor declaration.
@return	sym_node */
793 794 795 796
UNIV_INTERN
que_node_t*
pars_cursor_declaration(
/*====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
797
	sym_node_t*	sym_node,	/*!< in: cursor id node in the symbol
798
					table */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
799
	sel_node_t*	select_node)	/*!< in: select node */
800 801 802 803 804 805 806 807 808 809 810
{
	sym_node->resolved = TRUE;
	sym_node->token_type = SYM_CURSOR;
	sym_node->cursor_def = select_node;

	select_node->state = SEL_NODE_CLOSED;
	select_node->explicit_cursor = sym_node;

	return(sym_node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
811 812 813
/*********************************************************************//**
Parses a function declaration.
@return	sym_node */
814 815 816 817
UNIV_INTERN
que_node_t*
pars_function_declaration(
/*======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
818
	sym_node_t*	sym_node)	/*!< in: function id node in the symbol
819 820 821 822 823 824 825 826 827 828 829 830
					table */
{
	sym_node->resolved = TRUE;
	sym_node->token_type = SYM_FUNCTION;

	/* Check that the function exists. */
	ut_a(pars_info_get_user_func(pars_sym_tab_global->info,
				     sym_node->name));

	return(sym_node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
831 832 833
/*********************************************************************//**
Parses a delete or update statement start.
@return	own: update node in a query tree */
834 835 836 837
UNIV_INTERN
upd_node_t*
pars_update_statement_start(
/*========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
838 839 840
	ibool		is_delete,	/*!< in: TRUE if delete */
	sym_node_t*	table_sym,	/*!< in: table name node */
	col_assign_node_t* col_assign_list)/*!< in: column assignment list, NULL
841 842 843 844 845 846 847 848 849 850 851 852 853 854
					if delete */
{
	upd_node_t*	node;

	node = upd_node_create(pars_sym_tab_global->heap);

	node->is_delete = is_delete;

	node->table_sym = table_sym;
	node->col_assign_list = col_assign_list;

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
855 856 857
/*********************************************************************//**
Parses a column assignment in an update.
@return	column assignment node */
858 859 860 861
UNIV_INTERN
col_assign_node_t*
pars_column_assignment(
/*===================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
862 863
	sym_node_t*	column,	/*!< in: column to assign */
	que_node_t*	exp)	/*!< in: value to assign */
864 865 866 867 868 869 870 871 872 873 874 875 876
{
	col_assign_node_t*	node;

	node = mem_heap_alloc(pars_sym_tab_global->heap,
			      sizeof(col_assign_node_t));
	node->common.type = QUE_NODE_COL_ASSIGNMENT;

	node->col = column;
	node->val = exp;

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
877
/*********************************************************************//**
878 879 880 881 882
Processes an update node assignment list. */
static
void
pars_process_assign_list(
/*=====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
883
	upd_node_t*	node)	/*!< in: update node */
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
{
	col_assign_node_t*	col_assign_list;
	sym_node_t*		table_sym;
	col_assign_node_t*	assign_node;
	upd_field_t*		upd_field;
	dict_index_t*		clust_index;
	sym_node_t*		col_sym;
	ulint			changes_ord_field;
	ulint			changes_field_size;
	ulint			n_assigns;
	ulint			i;

	table_sym = node->table_sym;
	col_assign_list = node->col_assign_list;
	clust_index = dict_table_get_first_index(node->table);

	assign_node = col_assign_list;
	n_assigns = 0;

	while (assign_node) {
		pars_resolve_exp_columns(table_sym, assign_node->col);
		pars_resolve_exp_columns(table_sym, assign_node->val);
		pars_resolve_exp_variables_and_types(NULL, assign_node->val);
#if 0
		ut_a(dtype_get_mtype(
			     dfield_get_type(que_node_get_val(
						     assign_node->col)))
		     == dtype_get_mtype(
			     dfield_get_type(que_node_get_val(
						     assign_node->val))));
#endif

		/* Add to the update node all the columns found in assignment
		values as columns to copy: therefore, TRUE */

		opt_find_all_cols(TRUE, clust_index, &(node->columns), NULL,
				  assign_node->val);
		n_assigns++;

		assign_node = que_node_get_next(assign_node);
	}

	node->update = upd_create(n_assigns, pars_sym_tab_global->heap);

	assign_node = col_assign_list;

	changes_field_size = UPD_NODE_NO_SIZE_CHANGE;

	for (i = 0; i < n_assigns; i++) {
		upd_field = upd_get_nth_field(node->update, i);

		col_sym = assign_node->col;

		upd_field_set_field_no(upd_field, dict_index_get_nth_col_pos(
					       clust_index, col_sym->col_no),
				       clust_index, NULL);
		upd_field->exp = assign_node->val;

		if (!dict_col_get_fixed_size(
			    dict_index_get_nth_col(clust_index,
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
944 945
						   upd_field->field_no),
			    dict_table_is_comp(node->table))) {
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
			changes_field_size = 0;
		}

		assign_node = que_node_get_next(assign_node);
	}

	/* Find out if the update can modify an ordering field in any index */

	changes_ord_field = UPD_NODE_NO_ORD_CHANGE;

	if (row_upd_changes_some_index_ord_field_binary(node->table,
							node->update)) {
		changes_ord_field = 0;
	}

	node->cmpl_info = changes_ord_field | changes_field_size;
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
964 965 966
/*********************************************************************//**
Parses an update or delete statement.
@return	own: update node in a query tree */
967 968 969 970
UNIV_INTERN
upd_node_t*
pars_update_statement(
/*==================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
971 972
	upd_node_t*	node,		/*!< in: update node */
	sym_node_t*	cursor_sym,	/*!< in: pointer to a cursor entry in
973
					the symbol table or NULL */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
974
	que_node_t*	search_cond)	/*!< in: search condition or NULL */
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
{
	sym_node_t*	table_sym;
	sel_node_t*	sel_node;
	plan_t*		plan;

	table_sym = node->table_sym;

	pars_retrieve_table_def(table_sym);
	node->table = table_sym->table;

	UT_LIST_INIT(node->columns);

	/* Make the single table node into a list of table nodes of length 1 */

	que_node_list_add_last(NULL, table_sym);

	if (cursor_sym) {
		pars_resolve_exp_variables_and_types(NULL, cursor_sym);

		sel_node = cursor_sym->alias->cursor_def;

		node->searched_update = FALSE;
	} else {
		sel_node = pars_select_list(NULL, NULL);

		pars_select_statement(sel_node, table_sym, search_cond, NULL,
				      &pars_share_token, NULL);
		node->searched_update = TRUE;
		sel_node->common.parent = node;
	}

	node->select = sel_node;

	ut_a(!node->is_delete || (node->col_assign_list == NULL));
	ut_a(node->is_delete || (node->col_assign_list != NULL));

	if (node->is_delete) {
		node->cmpl_info = 0;
	} else {
		pars_process_assign_list(node);
	}

	if (node->searched_update) {
		node->has_clust_rec_x_lock = TRUE;
		sel_node->set_x_locks = TRUE;
		sel_node->row_lock_mode = LOCK_X;
	} else {
		node->has_clust_rec_x_lock = sel_node->set_x_locks;
	}

	ut_a(sel_node->n_tables == 1);
	ut_a(sel_node->consistent_read == FALSE);
	ut_a(sel_node->order_by == NULL);
	ut_a(sel_node->is_aggregate == FALSE);

	sel_node->can_get_updated = TRUE;

	node->state = UPD_NODE_UPDATE_CLUSTERED;

	plan = sel_node_get_nth_plan(sel_node, 0);

	plan->no_prefetch = TRUE;

	if (!dict_index_is_clust(plan->index)) {

		plan->must_get_clust = TRUE;

		node->pcur = &(plan->clust_pcur);
	} else {
		node->pcur = &(plan->pcur);
	}

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1050 1051 1052
/*********************************************************************//**
Parses an insert statement.
@return	own: update node in a query tree */
1053 1054 1055 1056
UNIV_INTERN
ins_node_t*
pars_insert_statement(
/*==================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1057 1058 1059
	sym_node_t*	table_sym,	/*!< in: table name node */
	que_node_t*	values_list,	/*!< in: value expression list or NULL */
	sel_node_t*	select)		/*!< in: select condition or NULL */
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
{
	ins_node_t*	node;
	dtuple_t*	row;
	ulint		ins_type;

	ut_a(values_list || select);
	ut_a(!values_list || !select);

	if (values_list) {
		ins_type = INS_VALUES;
	} else {
		ins_type = INS_SEARCHED;
	}

	pars_retrieve_table_def(table_sym);

	node = ins_node_create(ins_type, table_sym->table,
			       pars_sym_tab_global->heap);

	row = dtuple_create(pars_sym_tab_global->heap,
			    dict_table_get_n_cols(node->table));

	dict_table_copy_types(row, table_sym->table);

	ins_node_set_new_row(node, row);

	node->select = select;

	if (select) {
		select->common.parent = node;

		ut_a(que_node_list_get_len(select->select_list)
		     == dict_table_get_n_user_cols(table_sym->table));
	}

	node->values_list = values_list;

	if (node->values_list) {
		pars_resolve_exp_list_variables_and_types(NULL, values_list);

		ut_a(que_node_list_get_len(values_list)
		     == dict_table_get_n_user_cols(table_sym->table));
	}

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1107
/*********************************************************************//**
1108 1109 1110 1111 1112
Set the type of a dfield. */
static
void
pars_set_dfield_type(
/*=================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1113 1114
	dfield_t*		dfield,		/*!< in: dfield */
	pars_res_word_t*	type,		/*!< in: pointer to a type
1115
						token */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1116 1117
	ulint			len,		/*!< in: length, or 0 */
	ibool			is_unsigned,	/*!< in: if TRUE, column is
1118
						UNSIGNED. */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1119
	ibool			is_not_null)	/*!< in: if TRUE, column is
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
						NOT NULL. */
{
	ulint flags = 0;

	if (is_not_null) {
		flags |= DATA_NOT_NULL;
	}

	if (is_unsigned) {
		flags |= DATA_UNSIGNED;
	}

	if (type == &pars_int_token) {
		ut_a(len == 0);

		dtype_set(dfield_get_type(dfield), DATA_INT, flags, 4);

	} else if (type == &pars_char_token) {
		ut_a(len == 0);

		dtype_set(dfield_get_type(dfield), DATA_VARCHAR,
			  DATA_ENGLISH | flags, 0);
	} else if (type == &pars_binary_token) {
		ut_a(len != 0);

		dtype_set(dfield_get_type(dfield), DATA_FIXBINARY,
			  DATA_BINARY_TYPE | flags, len);
	} else if (type == &pars_blob_token) {
		ut_a(len == 0);

		dtype_set(dfield_get_type(dfield), DATA_BLOB,
			  DATA_BINARY_TYPE | flags, 0);
	} else {
		ut_error;
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1157 1158 1159
/*********************************************************************//**
Parses a variable declaration.
@return	own: symbol table node of type SYM_VAR */
1160 1161 1162 1163
UNIV_INTERN
sym_node_t*
pars_variable_declaration(
/*======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1164
	sym_node_t*	node,	/*!< in: symbol table node allocated for the
1165
				id of the variable */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1166
	pars_res_word_t* type)	/*!< in: pointer to a type token */
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
{
	node->resolved = TRUE;
	node->token_type = SYM_VAR;

	node->param_type = PARS_NOT_PARAM;

	pars_set_dfield_type(que_node_get_val(node), type, 0, FALSE, FALSE);

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1178 1179 1180
/*********************************************************************//**
Parses a procedure parameter declaration.
@return	own: symbol table node of type SYM_VAR */
1181 1182 1183 1184
UNIV_INTERN
sym_node_t*
pars_parameter_declaration(
/*=======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1185
	sym_node_t*	node,	/*!< in: symbol table node allocated for the
1186 1187
				id of the parameter */
	ulint		param_type,
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1188 1189
				/*!< in: PARS_INPUT or PARS_OUTPUT */
	pars_res_word_t* type)	/*!< in: pointer to a type token */
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
{
	ut_a((param_type == PARS_INPUT) || (param_type == PARS_OUTPUT));

	pars_variable_declaration(node, type);

	node->param_type = param_type;

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1200
/*********************************************************************//**
1201 1202 1203 1204 1205
Sets the parent field in a query node list. */
static
void
pars_set_parent_in_list(
/*====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1206 1207
	que_node_t*	node_list,	/*!< in: first node in a list */
	que_node_t*	parent)		/*!< in: parent value to set in all
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
					nodes of the list */
{
	que_common_t*	common;

	common = node_list;

	while (common) {
		common->parent = parent;

		common = que_node_get_next(common);
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1221 1222 1223
/*********************************************************************//**
Parses an elsif element.
@return	elsif node */
1224 1225 1226 1227
UNIV_INTERN
elsif_node_t*
pars_elsif_element(
/*===============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1228 1229
	que_node_t*	cond,		/*!< in: if-condition */
	que_node_t*	stat_list)	/*!< in: statement list */
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
{
	elsif_node_t*	node;

	node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(elsif_node_t));

	node->common.type = QUE_NODE_ELSIF;

	node->cond = cond;

	pars_resolve_exp_variables_and_types(NULL, cond);

	node->stat_list = stat_list;

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1246 1247 1248
/*********************************************************************//**
Parses an if-statement.
@return	if-statement node */
1249 1250 1251 1252
UNIV_INTERN
if_node_t*
pars_if_statement(
/*==============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1253 1254 1255
	que_node_t*	cond,		/*!< in: if-condition */
	que_node_t*	stat_list,	/*!< in: statement list */
	que_node_t*	else_part)	/*!< in: else-part statement list
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
					or elsif element list */
{
	if_node_t*	node;
	elsif_node_t*	elsif_node;

	node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(if_node_t));

	node->common.type = QUE_NODE_IF;

	node->cond = cond;

	pars_resolve_exp_variables_and_types(NULL, cond);

	node->stat_list = stat_list;

	if (else_part && (que_node_get_type(else_part) == QUE_NODE_ELSIF)) {

		/* There is a list of elsif conditions */

		node->else_part = NULL;
		node->elsif_list = else_part;

		elsif_node = else_part;

		while (elsif_node) {
			pars_set_parent_in_list(elsif_node->stat_list, node);

			elsif_node = que_node_get_next(elsif_node);
		}
	} else {
		node->else_part = else_part;
		node->elsif_list = NULL;

		pars_set_parent_in_list(else_part, node);
	}

	pars_set_parent_in_list(stat_list, node);

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1297 1298 1299
/*********************************************************************//**
Parses a while-statement.
@return	while-statement node */
1300 1301 1302 1303
UNIV_INTERN
while_node_t*
pars_while_statement(
/*=================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1304 1305
	que_node_t*	cond,		/*!< in: while-condition */
	que_node_t*	stat_list)	/*!< in: statement list */
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
{
	while_node_t*	node;

	node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(while_node_t));

	node->common.type = QUE_NODE_WHILE;

	node->cond = cond;

	pars_resolve_exp_variables_and_types(NULL, cond);

	node->stat_list = stat_list;

	pars_set_parent_in_list(stat_list, node);

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1324 1325 1326
/*********************************************************************//**
Parses a for-loop-statement.
@return	for-statement node */
1327 1328 1329 1330
UNIV_INTERN
for_node_t*
pars_for_statement(
/*===============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1331 1332 1333 1334
	sym_node_t*	loop_var,	/*!< in: loop variable */
	que_node_t*	loop_start_limit,/*!< in: loop start expression */
	que_node_t*	loop_end_limit,	/*!< in: loop end expression */
	que_node_t*	stat_list)	/*!< in: statement list */
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
{
	for_node_t*	node;

	node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(for_node_t));

	node->common.type = QUE_NODE_FOR;

	pars_resolve_exp_variables_and_types(NULL, loop_var);
	pars_resolve_exp_variables_and_types(NULL, loop_start_limit);
	pars_resolve_exp_variables_and_types(NULL, loop_end_limit);

	node->loop_var = loop_var->indirection;

	ut_a(loop_var->indirection);

	node->loop_start_limit = loop_start_limit;
	node->loop_end_limit = loop_end_limit;

	node->stat_list = stat_list;

	pars_set_parent_in_list(stat_list, node);

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1360 1361 1362
/*********************************************************************//**
Parses an exit statement.
@return	exit statement node */
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
UNIV_INTERN
exit_node_t*
pars_exit_statement(void)
/*=====================*/
{
	exit_node_t*	node;

	node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(exit_node_t));
	node->common.type = QUE_NODE_EXIT;

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1376 1377 1378
/*********************************************************************//**
Parses a return-statement.
@return	return-statement node */
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
UNIV_INTERN
return_node_t*
pars_return_statement(void)
/*=======================*/
{
	return_node_t*	node;

	node = mem_heap_alloc(pars_sym_tab_global->heap,
			      sizeof(return_node_t));
	node->common.type = QUE_NODE_RETURN;

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1393 1394 1395
/*********************************************************************//**
Parses an assignment statement.
@return	assignment statement node */
1396 1397 1398 1399
UNIV_INTERN
assign_node_t*
pars_assignment_statement(
/*======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1400 1401
	sym_node_t*	var,	/*!< in: variable to assign */
	que_node_t*	val)	/*!< in: value to assign */
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
{
	assign_node_t*	node;

	node = mem_heap_alloc(pars_sym_tab_global->heap,
			      sizeof(assign_node_t));
	node->common.type = QUE_NODE_ASSIGNMENT;

	node->var = var;
	node->val = val;

	pars_resolve_exp_variables_and_types(NULL, var);
	pars_resolve_exp_variables_and_types(NULL, val);

	ut_a(dtype_get_mtype(dfield_get_type(que_node_get_val(var)))
	     == dtype_get_mtype(dfield_get_type(que_node_get_val(val))));

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1421 1422 1423
/*********************************************************************//**
Parses a procedure call.
@return	function node */
1424 1425 1426 1427
UNIV_INTERN
func_node_t*
pars_procedure_call(
/*================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1428 1429
	que_node_t*	res_word,/*!< in: procedure name reserved word */
	que_node_t*	args)	/*!< in: argument list */
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
{
	func_node_t*	node;

	node = pars_func(res_word, args);

	pars_resolve_exp_list_variables_and_types(NULL, args);

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1440
/*********************************************************************//**
1441
Parses a fetch statement. into_list or user_func (but not both) must be
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1442 1443
non-NULL.
@return	fetch statement node */
1444 1445 1446 1447
UNIV_INTERN
fetch_node_t*
pars_fetch_statement(
/*=================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1448 1449 1450
	sym_node_t*	cursor,		/*!< in: cursor node */
	sym_node_t*	into_list,	/*!< in: variables to set, or NULL */
	sym_node_t*	user_func)	/*!< in: user function name, or NULL */
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
{
	sym_node_t*	cursor_decl;
	fetch_node_t*	node;

	/* Logical XOR. */
	ut_a(!into_list != !user_func);

	node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(fetch_node_t));

	node->common.type = QUE_NODE_FETCH;

	pars_resolve_exp_variables_and_types(NULL, cursor);

	if (into_list) {
		pars_resolve_exp_list_variables_and_types(NULL, into_list);
		node->into_list = into_list;
		node->func = NULL;
	} else {
		pars_resolve_exp_variables_and_types(NULL, user_func);

		node->func = pars_info_get_user_func(pars_sym_tab_global->info,
						     user_func->name);
		ut_a(node->func);

		node->into_list = NULL;
	}

	cursor_decl = cursor->alias;

	ut_a(cursor_decl->token_type == SYM_CURSOR);

	node->cursor_def = cursor_decl->cursor_def;

	if (into_list) {
		ut_a(que_node_list_get_len(into_list)
		     == que_node_list_get_len(node->cursor_def->select_list));
	}

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1492 1493 1494
/*********************************************************************//**
Parses an open or close cursor statement.
@return	fetch statement node */
1495 1496 1497 1498
UNIV_INTERN
open_node_t*
pars_open_statement(
/*================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1499
	ulint		type,	/*!< in: ROW_SEL_OPEN_CURSOR
1500
				or ROW_SEL_CLOSE_CURSOR */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1501
	sym_node_t*	cursor)	/*!< in: cursor node */
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
{
	sym_node_t*	cursor_decl;
	open_node_t*	node;

	node = mem_heap_alloc(pars_sym_tab_global->heap, sizeof(open_node_t));

	node->common.type = QUE_NODE_OPEN;

	pars_resolve_exp_variables_and_types(NULL, cursor);

	cursor_decl = cursor->alias;

	ut_a(cursor_decl->token_type == SYM_CURSOR);

	node->op_type = type;
	node->cursor_def = cursor_decl->cursor_def;

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1522 1523 1524
/*********************************************************************//**
Parses a row_printf-statement.
@return	row_printf-statement node */
1525 1526 1527 1528
UNIV_INTERN
row_printf_node_t*
pars_row_printf_statement(
/*======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1529
	sel_node_t*	sel_node)	/*!< in: select node */
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
{
	row_printf_node_t*	node;

	node = mem_heap_alloc(pars_sym_tab_global->heap,
			      sizeof(row_printf_node_t));
	node->common.type = QUE_NODE_ROW_PRINTF;

	node->sel_node = sel_node;

	sel_node->common.parent = node;

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1544 1545 1546
/*********************************************************************//**
Parses a commit statement.
@return	own: commit node struct */
1547 1548 1549 1550 1551 1552 1553 1554
UNIV_INTERN
commit_node_t*
pars_commit_statement(void)
/*=======================*/
{
	return(commit_node_create(pars_sym_tab_global->heap));
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1555 1556 1557
/*********************************************************************//**
Parses a rollback statement.
@return	own: rollback node struct */
1558 1559 1560 1561 1562 1563 1564 1565
UNIV_INTERN
roll_node_t*
pars_rollback_statement(void)
/*=========================*/
{
	return(roll_node_create(pars_sym_tab_global->heap));
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1566 1567 1568
/*********************************************************************//**
Parses a column definition at a table creation.
@return	column sym table node */
1569 1570 1571 1572
UNIV_INTERN
sym_node_t*
pars_column_def(
/*============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1573
	sym_node_t*		sym_node,	/*!< in: column node in the
1574
						symbol table */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1575 1576
	pars_res_word_t*	type,		/*!< in: data type */
	sym_node_t*		len,		/*!< in: length of column, or
1577
						NULL */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1578
	void*			is_unsigned,	/*!< in: if not NULL, column
1579
						is of type UNSIGNED. */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1580
	void*			is_not_null)	/*!< in: if not NULL, column
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
						is of type NOT NULL. */
{
	ulint len2;

	if (len) {
		len2 = eval_node_get_int_val(len);
	} else {
		len2 = 0;
	}

	pars_set_dfield_type(que_node_get_val(sym_node), type, len2,
			     is_unsigned != NULL, is_not_null != NULL);

	return(sym_node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1597 1598 1599
/*********************************************************************//**
Parses a table creation operation.
@return	table create subgraph */
1600 1601 1602 1603
UNIV_INTERN
tab_node_t*
pars_create_table(
/*==============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1604
	sym_node_t*	table_sym,	/*!< in: table name node in the symbol
1605
					table */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1606
	sym_node_t*	column_defs,	/*!< in: list of column names */
1607
	void*		not_fit_in_memory __attribute__((unused)))
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1608
					/*!< in: a non-NULL pointer means that
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
					this is a table which in simulations
					should be simulated as not fitting
					in memory; thread is put to sleep
					to simulate disk accesses; NOTE that
					this flag is not stored to the data
					dictionary on disk, and the database
					will forget about non-NULL value if
					it has to reload the table definition
					from disk */
{
	dict_table_t*	table;
	sym_node_t*	column;
	tab_node_t*	node;
	const dtype_t*	dtype;
	ulint		n_cols;

	n_cols = que_node_list_get_len(column_defs);

	/* As the InnoDB SQL parser is for internal use only,
	for creating some system tables, this function will only
	create tables in the old (not compact) record format. */
	table = dict_mem_table_create(table_sym->name, 0, n_cols, 0);

#ifdef UNIV_DEBUG
	if (not_fit_in_memory != NULL) {
		table->does_not_fit_in_memory = TRUE;
	}
#endif /* UNIV_DEBUG */
	column = column_defs;

	while (column) {
		dtype = dfield_get_type(que_node_get_val(column));

		dict_mem_table_add_col(table, table->heap,
				       column->name, dtype->mtype,
				       dtype->prtype, dtype->len);
		column->resolved = TRUE;
		column->token_type = SYM_COLUMN;

		column = que_node_get_next(column);
	}

	node = tab_create_graph_create(table, pars_sym_tab_global->heap);

	table_sym->resolved = TRUE;
	table_sym->token_type = SYM_TABLE;

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1659 1660 1661
/*********************************************************************//**
Parses an index creation operation.
@return	index create subgraph */
1662 1663 1664 1665
UNIV_INTERN
ind_node_t*
pars_create_index(
/*==============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1666 1667 1668
	pars_res_word_t* unique_def,	/*!< in: not NULL if a unique index */
	pars_res_word_t* clustered_def,	/*!< in: not NULL if a clustered index */
	sym_node_t*	index_sym,	/*!< in: index name node in the symbol
1669
					table */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1670
	sym_node_t*	table_sym,	/*!< in: table name node in the symbol
1671
					table */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1672
	sym_node_t*	column_list)	/*!< in: list of column names */
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
{
	dict_index_t*	index;
	sym_node_t*	column;
	ind_node_t*	node;
	ulint		n_fields;
	ulint		ind_type;

	n_fields = que_node_list_get_len(column_list);

	ind_type = 0;

	if (unique_def) {
		ind_type = ind_type | DICT_UNIQUE;
	}

	if (clustered_def) {
		ind_type = ind_type | DICT_CLUSTERED;
	}

	index = dict_mem_index_create(table_sym->name, index_sym->name, 0,
				      ind_type, n_fields);
	column = column_list;

	while (column) {
		dict_mem_index_add_field(index, column->name, 0);

		column->resolved = TRUE;
		column->token_type = SYM_COLUMN;

		column = que_node_get_next(column);
	}

	node = ind_create_graph_create(index, pars_sym_tab_global->heap);

	table_sym->resolved = TRUE;
	table_sym->token_type = SYM_TABLE;

	index_sym->resolved = TRUE;
	index_sym->token_type = SYM_TABLE;

	return(node);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1716 1717 1718
/*********************************************************************//**
Parses a procedure definition.
@return	query fork node */
1719 1720 1721 1722
UNIV_INTERN
que_fork_t*
pars_procedure_definition(
/*======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1723
	sym_node_t*	sym_node,	/*!< in: procedure id node in the symbol
1724
					table */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1725 1726
	sym_node_t*	param_list,	/*!< in: parameter declaration list */
	que_node_t*	stat_list)	/*!< in: statement list */
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
{
	proc_node_t*	node;
	que_fork_t*	fork;
	que_thr_t*	thr;
	mem_heap_t*	heap;

	heap = pars_sym_tab_global->heap;

	fork = que_fork_create(NULL, NULL, QUE_FORK_PROCEDURE, heap);
	fork->trx = NULL;

	thr = que_thr_create(fork, heap);

	node = mem_heap_alloc(heap, sizeof(proc_node_t));

	node->common.type = QUE_NODE_PROC;
	node->common.parent = thr;

	sym_node->token_type = SYM_PROCEDURE_NAME;
	sym_node->resolved = TRUE;

	node->proc_id = sym_node;
	node->param_list = param_list;
	node->stat_list = stat_list;

	pars_set_parent_in_list(stat_list, node);

	node->sym_tab = pars_sym_tab_global;

	thr->child = node;

	pars_sym_tab_global->query_graph = fork;

	return(fork);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1763
/*************************************************************//**
1764 1765 1766
Parses a stored procedure call, when this is not within another stored
procedure, that is, the client issues a procedure call directly.
In MySQL/InnoDB, stored InnoDB procedures are invoked via the
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1767 1768
parsed procedure tree, not via InnoDB SQL, so this function is not used.
@return	query graph */
1769 1770 1771 1772 1773
UNIV_INTERN
que_fork_t*
pars_stored_procedure_call(
/*=======================*/
	sym_node_t*	sym_node __attribute__((unused)))
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1774
					/*!< in: stored procedure name */
1775 1776 1777 1778 1779
{
	ut_error;
	return(NULL);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1780
/*************************************************************//**
1781 1782 1783 1784 1785
Retrieves characters to the lexical analyzer. */
UNIV_INTERN
void
pars_get_lex_chars(
/*===============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1786 1787 1788
	char*	buf,		/*!< in/out: buffer where to copy */
	int*	result,		/*!< out: number of characters copied or EOF */
	int	max_size)	/*!< in: maximum number of characters which fit
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
				in the buffer */
{
	int	len;

	len = pars_sym_tab_global->string_len
		- pars_sym_tab_global->next_char_pos;
	if (len == 0) {
#ifdef YYDEBUG
		/* fputs("SQL string ends\n", stderr); */
#endif
		*result = 0;

		return;
	}

	if (len > max_size) {
		len = max_size;
	}

#ifdef UNIV_SQL_DEBUG
	if (pars_print_lexed) {

		if (len >= 5) {
			len = 5;
		}

		fwrite(pars_sym_tab_global->sql_string
		       + pars_sym_tab_global->next_char_pos,
		       1, len, stderr);
	}
#endif /* UNIV_SQL_DEBUG */

	ut_memcpy(buf, pars_sym_tab_global->sql_string
		  + pars_sym_tab_global->next_char_pos, len);
	*result = len;

	pars_sym_tab_global->next_char_pos += len;
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1828
/*************************************************************//**
1829 1830 1831 1832 1833 1834
Called by yyparse on error. */
UNIV_INTERN
void
yyerror(
/*====*/
	const char*	s __attribute__((unused)))
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1835
				/*!< in: error message string */
1836 1837 1838 1839 1840 1841 1842 1843
{
	ut_ad(s);

	fputs("PARSER ERROR: Syntax error in SQL string\n", stderr);

	ut_error;
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1844 1845 1846
/*************************************************************//**
Parses an SQL string returning the query graph.
@return	own: the query graph */
1847 1848 1849 1850
UNIV_INTERN
que_t*
pars_sql(
/*=====*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1851 1852
	pars_info_t*	info,	/*!< in: extra information, or NULL */
	const char*	str)	/*!< in: SQL string */
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
{
	sym_node_t*	sym_node;
	mem_heap_t*	heap;
	que_t*		graph;

	ut_ad(str);

	heap = mem_heap_create(256);

	/* Currently, the parser is not reentrant: */
	ut_ad(mutex_own(&(dict_sys->mutex)));

	pars_sym_tab_global = sym_tab_create(heap);

	pars_sym_tab_global->string_len = strlen(str);
	pars_sym_tab_global->sql_string = mem_heap_dup(
		heap, str, pars_sym_tab_global->string_len + 1);
	pars_sym_tab_global->next_char_pos = 0;
	pars_sym_tab_global->info = info;

	yyparse();

	sym_node = UT_LIST_GET_FIRST(pars_sym_tab_global->sym_list);

	while (sym_node) {
		ut_a(sym_node->resolved);

		sym_node = UT_LIST_GET_NEXT(sym_list, sym_node);
	}

	graph = pars_sym_tab_global->query_graph;

	graph->sym_tab = pars_sym_tab_global;
	graph->info = info;

	/* fprintf(stderr, "SQL graph size %lu\n", mem_heap_get_size(heap)); */

	return(graph);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1893
/******************************************************************//**
1894 1895
Completes a query graph by adding query thread and fork nodes
above it and prepares the graph for running. The fork created is of
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1896 1897
type QUE_FORK_MYSQL_INTERFACE.
@return	query thread node to run */
1898 1899 1900 1901
UNIV_INTERN
que_thr_t*
pars_complete_graph_for_exec(
/*=========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1902
	que_node_t*	node,	/*!< in: root node for an incomplete
1903
				query graph */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1904 1905
	trx_t*		trx,	/*!< in: transaction handle */
	mem_heap_t*	heap)	/*!< in: memory heap from which allocated */
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
{
	que_fork_t*	fork;
	que_thr_t*	thr;

	fork = que_fork_create(NULL, NULL, QUE_FORK_MYSQL_INTERFACE, heap);
	fork->trx = trx;

	thr = que_thr_create(fork, heap);

	thr->child = node;

	que_node_set_parent(node, thr);

	trx->graph = NULL;

	return(thr);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1924 1925 1926
/****************************************************************//**
Create parser info struct.
@return	own: info struct */
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
UNIV_INTERN
pars_info_t*
pars_info_create(void)
/*==================*/
{
	pars_info_t*	info;
	mem_heap_t*	heap;

	heap = mem_heap_create(512);

	info = mem_heap_alloc(heap, sizeof(*info));

	info->heap = heap;
	info->funcs = NULL;
	info->bound_lits = NULL;
	info->bound_ids = NULL;
	info->graph_owns_us = TRUE;

	return(info);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1948 1949
/****************************************************************//**
Free info struct and everything it contains. */
1950 1951 1952 1953
UNIV_INTERN
void
pars_info_free(
/*===========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1954
	pars_info_t*	info)	/*!< in, own: info struct */
1955 1956 1957 1958
{
	mem_heap_free(info->heap);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1959
/****************************************************************//**
1960 1961 1962 1963 1964
Add bound literal. */
UNIV_INTERN
void
pars_info_add_literal(
/*==================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1965 1966 1967 1968 1969 1970
	pars_info_t*	info,		/*!< in: info struct */
	const char*	name,		/*!< in: name */
	const void*	address,	/*!< in: address */
	ulint		length,		/*!< in: length of data */
	ulint		type,		/*!< in: type, e.g. DATA_FIXBINARY */
	ulint		prtype)		/*!< in: precise type, e.g.
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
					DATA_UNSIGNED */
{
	pars_bound_lit_t*	pbl;

	ut_ad(!pars_info_get_bound_lit(info, name));

	pbl = mem_heap_alloc(info->heap, sizeof(*pbl));

	pbl->name = name;
	pbl->address = address;
	pbl->length = length;
	pbl->type = type;
	pbl->prtype = prtype;

	if (!info->bound_lits) {
		info->bound_lits = ib_vector_create(info->heap, 8);
	}

	ib_vector_push(info->bound_lits, pbl);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1992
/****************************************************************//**
1993 1994 1995 1996 1997 1998
Equivalent to pars_info_add_literal(info, name, str, strlen(str),
DATA_VARCHAR, DATA_ENGLISH). */
UNIV_INTERN
void
pars_info_add_str_literal(
/*======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1999 2000 2001
	pars_info_t*	info,		/*!< in: info struct */
	const char*	name,		/*!< in: name */
	const char*	str)		/*!< in: string */
2002 2003 2004 2005 2006
{
	pars_info_add_literal(info, name, str, strlen(str),
			      DATA_VARCHAR, DATA_ENGLISH);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2007
/****************************************************************//**
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
Equivalent to:

char buf[4];
mach_write_to_4(buf, val);
pars_info_add_literal(info, name, buf, 4, DATA_INT, 0);

except that the buffer is dynamically allocated from the info struct's
heap. */
UNIV_INTERN
void
pars_info_add_int4_literal(
/*=======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2020 2021 2022
	pars_info_t*	info,		/*!< in: info struct */
	const char*	name,		/*!< in: name */
	lint		val)		/*!< in: value */
2023 2024 2025 2026 2027 2028 2029
{
	byte*	buf = mem_heap_alloc(info->heap, 4);

	mach_write_to_4(buf, val);
	pars_info_add_literal(info, name, buf, 4, DATA_INT, 0);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2030
/****************************************************************//**
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
Equivalent to:

char buf[8];
mach_write_to_8(buf, val);
pars_info_add_literal(info, name, buf, 8, DATA_FIXBINARY, 0);

except that the buffer is dynamically allocated from the info struct's
heap. */
UNIV_INTERN
void
pars_info_add_dulint_literal(
/*=========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2043 2044 2045
	pars_info_t*	info,		/*!< in: info struct */
	const char*	name,		/*!< in: name */
	dulint		val)		/*!< in: value */
2046 2047 2048 2049 2050 2051 2052 2053
{
	byte*	buf = mem_heap_alloc(info->heap, 8);

	mach_write_to_8(buf, val);

	pars_info_add_literal(info, name, buf, 8, DATA_FIXBINARY, 0);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2054
/****************************************************************//**
2055 2056 2057 2058 2059
Add user function. */
UNIV_INTERN
void
pars_info_add_function(
/*===================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2060 2061 2062 2063
	pars_info_t*		info,	/*!< in: info struct */
	const char*		name,	/*!< in: function name */
	pars_user_func_cb_t	func,	/*!< in: function address */
	void*			arg)	/*!< in: user-supplied argument */
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081
{
	pars_user_func_t*	puf;

	ut_ad(!pars_info_get_user_func(info, name));

	puf = mem_heap_alloc(info->heap, sizeof(*puf));

	puf->name = name;
	puf->func = func;
	puf->arg = arg;

	if (!info->funcs) {
		info->funcs = ib_vector_create(info->heap, 8);
	}

	ib_vector_push(info->funcs, puf);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2082
/****************************************************************//**
2083 2084 2085 2086 2087
Add bound id. */
UNIV_INTERN
void
pars_info_add_id(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2088 2089 2090
	pars_info_t*	info,		/*!< in: info struct */
	const char*	name,		/*!< in: name */
	const char*	id)		/*!< in: id */
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
{
	pars_bound_id_t*	bid;

	ut_ad(!pars_info_get_bound_id(info, name));

	bid = mem_heap_alloc(info->heap, sizeof(*bid));

	bid->name = name;
	bid->id = id;

	if (!info->bound_ids) {
		info->bound_ids = ib_vector_create(info->heap, 8);
	}

	ib_vector_push(info->bound_ids, bid);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2108 2109 2110
/****************************************************************//**
Get user function with the given name.
@return	user func, or NULL if not found */
2111 2112 2113 2114
UNIV_INTERN
pars_user_func_t*
pars_info_get_user_func(
/*====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2115 2116
	pars_info_t*		info,	/*!< in: info struct */
	const char*		name)	/*!< in: function name to find*/
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
{
	ulint		i;
	ib_vector_t*	vec;

	if (!info || !info->funcs) {
		return(NULL);
	}

	vec = info->funcs;

	for (i = 0; i < ib_vector_size(vec); i++) {
		pars_user_func_t*	puf = ib_vector_get(vec, i);

		if (strcmp(puf->name, name) == 0) {
			return(puf);
		}
	}

	return(NULL);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2138 2139 2140
/****************************************************************//**
Get bound literal with the given name.
@return	bound literal, or NULL if not found */
2141 2142 2143 2144
UNIV_INTERN
pars_bound_lit_t*
pars_info_get_bound_lit(
/*====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2145 2146
	pars_info_t*		info,	/*!< in: info struct */
	const char*		name)	/*!< in: bound literal name to find */
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
{
	ulint		i;
	ib_vector_t*	vec;

	if (!info || !info->bound_lits) {
		return(NULL);
	}

	vec = info->bound_lits;

	for (i = 0; i < ib_vector_size(vec); i++) {
		pars_bound_lit_t*	pbl = ib_vector_get(vec, i);

		if (strcmp(pbl->name, name) == 0) {
			return(pbl);
		}
	}

	return(NULL);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2168 2169 2170
/****************************************************************//**
Get bound id with the given name.
@return	bound id, or NULL if not found */
2171 2172 2173 2174
UNIV_INTERN
pars_bound_id_t*
pars_info_get_bound_id(
/*===================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2175 2176
	pars_info_t*		info,	/*!< in: info struct */
	const char*		name)	/*!< in: bound id name to find */
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
{
	ulint		i;
	ib_vector_t*	vec;

	if (!info || !info->bound_ids) {
		return(NULL);
	}

	vec = info->bound_ids;

	for (i = 0; i < ib_vector_size(vec); i++) {
		pars_bound_id_t*	bid = ib_vector_get(vec, i);

		if (strcmp(bid->name, name) == 0) {
			return(bid);
		}
	}

	return(NULL);
}