sql_yacc.yy 129 KB
Newer Older
1
/* Copyright (C) 2000-2003 MySQL AB
2

unknown's avatar
unknown committed
3 4 5 6
   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; either version 2 of the License, or
   (at your option) any later version.
7

unknown's avatar
unknown committed
8 9 10 11
   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.
12

unknown's avatar
unknown committed
13 14 15 16
   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 */

unknown's avatar
unknown committed
17
/* sql_yacc.yy */
unknown's avatar
unknown committed
18 19

%{
20 21 22
/* thd is passed as an arg to yyparse(), and subsequently to yylex().
** The type will be void*, so it must be  cast to (THD*) when used.
** Use the YYTHD macro for this.
23 24
*/
#define YYPARSE_PARAM yythd
25
#define YYLEX_PARAM yythd
26 27
#define YYTHD ((THD *)yythd)

unknown's avatar
unknown committed
28 29 30
#define MYSQL_YACC
#define YYINITDEPTH 100
#define YYMAXDEPTH 3200				/* Because of 64K stack */
31
#define Lex (&(YYTHD->lex))
32
#define Select Lex->current_select
unknown's avatar
unknown committed
33
#include "mysql_priv.h"
unknown's avatar
unknown committed
34
#include "slave.h"
unknown's avatar
unknown committed
35 36
#include "sql_acl.h"
#include "lex_symbol.h"
37
#include "item_create.h"
38 39
#include "sp_head.h"
#include "sp_pcontext.h"
unknown's avatar
unknown committed
40
#include <myisam.h>
41
#include <myisammrg.h>
42

unknown's avatar
unknown committed
43
extern void yyerror(const char*);
44
int yylex(void *yylval, void *yythd);
unknown's avatar
unknown committed
45

unknown's avatar
unknown committed
46
#define yyoverflow(A,B,C,D,E,F) if (my_yyoverflow((B),(D),(int*) (F))) { yyerror((char*) (A)); return 2; }
unknown's avatar
unknown committed
47

48
inline Item *or_or_concat(THD *thd, Item* A, Item* B)
unknown's avatar
unknown committed
49
{
50
  return (thd->variables.sql_mode & MODE_PIPES_AS_CONCAT ?
unknown's avatar
unknown committed
51 52 53 54 55 56 57
          (Item*) new Item_func_concat(A,B) : (Item*) new Item_cond_or(A,B));
}

%}
%union {
  int  num;
  ulong ulong_num;
unknown's avatar
unknown committed
58
  ulonglong ulonglong_number;
unknown's avatar
unknown committed
59 60 61 62 63 64 65 66
  LEX_STRING lex_str;
  LEX_STRING *lex_str_ptr;
  LEX_SYMBOL symbol;
  Table_ident *table;
  char *simple_string;
  Item *item;
  List<Item> *item_list;
  List<String> *string_list;
unknown's avatar
unknown committed
67 68 69 70 71 72
  String *string;
  key_part_spec *key_part;
  TABLE_LIST *table_list;
  udf_func *udf;
  LEX_USER *lex_user;
  sys_var *variable;
unknown's avatar
unknown committed
73
  Key::Keytype key_type;
unknown's avatar
unknown committed
74
  enum ha_key_alg  key_alg;
unknown's avatar
unknown committed
75 76
  enum db_type db_type;
  enum row_type row_type;
unknown's avatar
unknown committed
77
  enum ha_rkey_function ha_rkey_mode;
unknown's avatar
unknown committed
78
  enum enum_tx_isolation tx_isolation;
unknown's avatar
unknown committed
79
  enum Item_cast cast_type;
unknown's avatar
unknown committed
80
  enum Item_udftype udf_type;
81
  CHARSET_INFO *charset;
unknown's avatar
unknown committed
82
  thr_lock_type lock_type;
unknown's avatar
unknown committed
83
  interval_type interval;
unknown's avatar
unknown committed
84
  st_select_lex *select_lex;
unknown's avatar
unknown committed
85
  chooser_compare_func_creator boolfunc2creator;
unknown's avatar
unknown committed
86 87 88 89 90 91 92 93 94 95
}

%{
bool my_yyoverflow(short **a, YYSTYPE **b,int *yystacksize);
%}

%pure_parser					/* We have threads */

%token	END_OF_INPUT

96 97 98 99 100 101
%token CLOSE_SYM
%token HANDLER_SYM
%token LAST_SYM
%token NEXT_SYM
%token PREV_SYM

102
%token	DIV_SYM
unknown's avatar
unknown committed
103 104
%token	EQ
%token	EQUAL_SYM
unknown's avatar
unknown committed
105
%token	SOUNDS_SYM
unknown's avatar
unknown committed
106 107 108 109 110 111
%token	GE
%token	GT_SYM
%token	LE
%token	LT
%token	NE
%token	IS
112
%token	MOD_SYM
unknown's avatar
unknown committed
113 114 115 116
%token	SHIFT_LEFT
%token	SHIFT_RIGHT
%token  SET_VAR

unknown's avatar
unknown committed
117
%token	ABORT_SYM
unknown's avatar
unknown committed
118 119
%token	ADD
%token	AFTER_SYM
unknown's avatar
unknown committed
120 121
%token	ALTER
%token	ANALYZE_SYM
unknown's avatar
unknown committed
122
%token	ANY_SYM
unknown's avatar
unknown committed
123 124 125
%token	AVG_SYM
%token	BEGIN_SYM
%token	BINLOG_SYM
126
%token  CALL_SYM
unknown's avatar
unknown committed
127
%token	CHANGE
unknown's avatar
unknown committed
128 129 130 131
%token	CLIENT_SYM
%token	COMMENT_SYM
%token	COMMIT_SYM
%token	COUNT_SYM
unknown's avatar
unknown committed
132 133
%token	CREATE
%token	CROSS
134
%token  CUBE_SYM
unknown's avatar
unknown committed
135
%token	DELETE_SYM
136
%token	DUAL_SYM
unknown's avatar
unknown committed
137
%token	DO_SYM
unknown's avatar
unknown committed
138
%token	DROP
unknown's avatar
unknown committed
139 140
%token	EVENTS_SYM
%token	EXECUTE_SYM
unknown's avatar
unknown committed
141
%token	FLUSH_SYM
142
%token  HELP_SYM
unknown's avatar
unknown committed
143 144
%token	INSERT
%token	IO_THREAD
unknown's avatar
unknown committed
145 146
%token	KILL_SYM
%token	LOAD
unknown's avatar
unknown committed
147
%token	LOCKS_SYM
unknown's avatar
unknown committed
148 149 150 151
%token	LOCK_SYM
%token	MASTER_SYM
%token	MAX_SYM
%token	MIN_SYM
152
%token	NONE_SYM
unknown's avatar
unknown committed
153 154 155 156 157 158
%token	OPTIMIZE
%token	PURGE
%token	REPAIR
%token	REPLICATION
%token	RESET_SYM
%token	ROLLBACK_SYM
159
%token  ROLLUP_SYM
unknown's avatar
unknown committed
160 161 162 163 164 165
%token	SELECT_SYM
%token	SHOW
%token	SLAVE
%token	SQL_THREAD
%token	START_SYM
%token	STD_SYM
unknown's avatar
unknown committed
166
%token  VARIANCE_SYM
unknown's avatar
unknown committed
167 168 169 170
%token	STOP_SYM
%token	SUM_SYM
%token	SUPER_SYM
%token	TRUNCATE_SYM
unknown's avatar
unknown committed
171
%token	UNLOCK_SYM
unknown's avatar
unknown committed
172
%token	UPDATE_SYM
unknown's avatar
unknown committed
173 174 175 176 177 178 179

%token	ACTION
%token	AGGREGATE_SYM
%token	ALL
%token	AND
%token	AS
%token	ASC
180
%token	AUTO_INC
unknown's avatar
unknown committed
181
%token	AVG_ROW_LENGTH
unknown's avatar
unknown committed
182
%token	BACKUP_SYM
unknown's avatar
unknown committed
183 184 185 186
%token	BERKELEY_DB_SYM
%token	BINARY
%token	BIT_SYM
%token	BOOL_SYM
unknown's avatar
unknown committed
187
%token	BOOLEAN_SYM
unknown's avatar
unknown committed
188
%token	BOTH
unknown's avatar
unknown committed
189
%token	BTREE_SYM
unknown's avatar
unknown committed
190
%token	BY
191
%token	BYTE_SYM
unknown's avatar
unknown committed
192
%token	CACHE_SYM
unknown's avatar
unknown committed
193
%token	CASCADE
unknown's avatar
unknown committed
194
%token	CAST_SYM
195
%token	CHARSET
unknown's avatar
unknown committed
196 197
%token	CHECKSUM_SYM
%token	CHECK_SYM
unknown's avatar
unknown committed
198
%token	COMMITTED_SYM
unknown's avatar
unknown committed
199
%token	COLLATE_SYM
unknown's avatar
unknown committed
200 201
%token	COLUMNS
%token	COLUMN_SYM
unknown's avatar
unknown committed
202
%token	CONCURRENT
unknown's avatar
unknown committed
203
%token	CONSTRAINT
unknown's avatar
unknown committed
204
%token	CONVERT_SYM
unknown's avatar
unknown committed
205 206
%token	DATABASES
%token	DATA_SYM
207
%token  DECLARE_SYM
unknown's avatar
unknown committed
208 209 210 211 212
%token	DEFAULT
%token	DELAYED_SYM
%token	DELAY_KEY_WRITE_SYM
%token	DESC
%token	DESCRIBE
unknown's avatar
unknown committed
213
%token	DES_KEY_FILE
214
%token	DISABLE_SYM
unknown's avatar
unknown committed
215
%token	DISTINCT
unknown's avatar
unknown committed
216
%token  DUPLICATE_SYM
unknown's avatar
unknown committed
217
%token	DYNAMIC_SYM
218
%token	ENABLE_SYM
unknown's avatar
unknown committed
219 220
%token	ENCLOSED
%token	ESCAPED
unknown's avatar
unknown committed
221
%token	DIRECTORY_SYM
unknown's avatar
unknown committed
222 223 224
%token	ESCAPE_SYM
%token	EXISTS
%token	EXTENDED_SYM
225
%token	FALSE_SYM
unknown's avatar
unknown committed
226 227 228 229
%token	FILE_SYM
%token	FIRST_SYM
%token	FIXED_SYM
%token	FLOAT_NUM
230
%token	FORCE_SYM
unknown's avatar
unknown committed
231 232 233
%token	FOREIGN
%token	FROM
%token	FULL
unknown's avatar
unknown committed
234 235
%token	FULLTEXT_SYM
%token	GLOBAL_SYM
unknown's avatar
unknown committed
236 237 238 239 240
%token	GRANT
%token	GRANTS
%token	GREATEST_SYM
%token	GROUP
%token	HAVING
unknown's avatar
unknown committed
241
%token	HASH_SYM
unknown's avatar
unknown committed
242 243 244 245 246 247 248
%token	HEAP_SYM
%token	HEX_NUM
%token	HIGH_PRIORITY
%token	HOSTS_SYM
%token	IDENT
%token	IGNORE_SYM
%token	INDEX
unknown's avatar
unknown committed
249
%token	INDEXES
unknown's avatar
unknown committed
250 251
%token	INFILE
%token	INNER_SYM
unknown's avatar
unknown committed
252
%token	INNOBASE_SYM
253
%token  INOUT_SYM
unknown's avatar
unknown committed
254 255
%token	INTO
%token	IN_SYM
unknown's avatar
unknown committed
256
%token	ISOLATION
unknown's avatar
unknown committed
257 258 259 260 261 262
%token	ISAM_SYM
%token	JOIN_SYM
%token	KEYS
%token	KEY_SYM
%token	LEADING
%token	LEAST_SYM
unknown's avatar
unknown committed
263
%token	LEVEL_SYM
unknown's avatar
unknown committed
264 265 266 267
%token	LEX_HOSTNAME
%token	LIKE
%token	LINES
%token	LOCAL_SYM
268
%token  LOCATOR_SYM
269
%token	LOG_SYM
unknown's avatar
unknown committed
270 271 272 273
%token	LOGS_SYM
%token	LONG_NUM
%token	LONG_SYM
%token	LOW_PRIORITY
unknown's avatar
unknown committed
274 275 276 277 278 279 280 281 282 283
%token	MASTER_HOST_SYM
%token	MASTER_USER_SYM
%token	MASTER_LOG_FILE_SYM
%token	MASTER_LOG_POS_SYM
%token	MASTER_PASSWORD_SYM
%token	MASTER_PORT_SYM
%token	MASTER_CONNECT_RETRY_SYM
%token	MASTER_SERVER_ID_SYM
%token	RELAY_LOG_FILE_SYM
%token	RELAY_LOG_POS_SYM
unknown's avatar
unknown committed
284 285
%token	MATCH
%token	MAX_ROWS
unknown's avatar
unknown committed
286 287 288
%token	MAX_CONNECTIONS_PER_HOUR
%token	MAX_QUERIES_PER_HOUR
%token	MAX_UPDATES_PER_HOUR
289
%token	MEDIUM_SYM
unknown's avatar
unknown committed
290 291 292 293 294
%token	MERGE_SYM
%token	MIN_ROWS
%token	MYISAM_SYM
%token	NATIONAL_SYM
%token	NATURAL
unknown's avatar
unknown committed
295
%token	NEW_SYM
unknown's avatar
unknown committed
296 297 298 299 300
%token	NCHAR_SYM
%token	NOT
%token	NO_SYM
%token	NULL_SYM
%token	NUM
unknown's avatar
unknown committed
301
%token	OFFSET_SYM
unknown's avatar
unknown committed
302
%token	ON
303
%token	OPEN_SYM
unknown's avatar
unknown committed
304 305 306
%token	OPTION
%token	OPTIONALLY
%token	OR
307
%token	OR_OR_CONCAT
unknown's avatar
unknown committed
308
%token	ORDER_SYM
309
%token  OUT_SYM
unknown's avatar
unknown committed
310 311
%token	OUTER
%token	OUTFILE
unknown's avatar
unknown committed
312
%token	DUMPFILE
unknown's avatar
unknown committed
313 314 315 316 317 318
%token	PACK_KEYS_SYM
%token	PARTIAL
%token	PRIMARY_SYM
%token	PRIVILEGES
%token	PROCESS
%token	PROCESSLIST_SYM
unknown's avatar
unknown committed
319
%token	QUERY_SYM
unknown's avatar
unknown committed
320
%token	RAID_0_SYM
321 322
%token	RAID_STRIPED_SYM
%token	RAID_TYPE
unknown's avatar
unknown committed
323 324 325 326 327 328 329 330
%token	RAID_CHUNKS
%token	RAID_CHUNKSIZE
%token	READ_SYM
%token	REAL_NUM
%token	REFERENCES
%token	REGEXP
%token	RELOAD
%token	RENAME
unknown's avatar
unknown committed
331
%token	REPEATABLE_SYM
unknown's avatar
unknown committed
332
%token	REQUIRE_SYM
unknown's avatar
unknown committed
333 334
%token	RESOURCES
%token	RESTORE_SYM
unknown's avatar
unknown committed
335 336 337 338 339
%token	RESTRICT
%token	REVOKE
%token	ROWS_SYM
%token	ROW_FORMAT_SYM
%token	ROW_SYM
unknown's avatar
unknown committed
340
%token	RTREE_SYM
unknown's avatar
unknown committed
341
%token	SET
342
%token	SERIAL_SYM
unknown's avatar
unknown committed
343 344
%token	SERIALIZABLE_SYM
%token	SESSION_SYM
345
%token	SIMPLE_SYM
unknown's avatar
unknown committed
346
%token	SHUTDOWN
unknown's avatar
unknown committed
347
%token	SPATIAL_SYM
348
%token  SPECIFIC_SYM
349
%token  SSL_SYM
unknown's avatar
unknown committed
350 351 352
%token	STARTING
%token	STATUS_SYM
%token	STRAIGHT_JOIN
unknown's avatar
unknown committed
353
%token	SUBJECT_SYM
unknown's avatar
unknown committed
354 355 356 357 358
%token	TABLES
%token	TABLE_SYM
%token	TEMPORARY
%token	TERMINATED
%token	TEXT_STRING
359 360 361
%token	TO_SYM
%token	TRAILING
%token	TRANSACTION_SYM
362
%token	TRUE_SYM
363
%token	TYPE_SYM
unknown's avatar
unknown committed
364
%token  TYPES_SYM
unknown's avatar
unknown committed
365 366 367 368
%token	FUNC_ARG0
%token	FUNC_ARG1
%token	FUNC_ARG2
%token	FUNC_ARG3
369 370
%token	RETURN_SYM
%token	RETURNS_SYM
371
%token	UDF_SONAME_SYM
372
%token	FUNCTION_SYM
unknown's avatar
unknown committed
373
%token	UNCOMMITTED_SYM
374
%token	UNDERSCORE_CHARSET
unknown's avatar
unknown committed
375
%token	UNICODE_SYM
376 377 378
%token	UNION_SYM
%token	UNIQUE_SYM
%token	USAGE
unknown's avatar
unknown committed
379
%token	USE_FRM
380 381
%token	USE_SYM
%token	USING
382
%token	VALUE_SYM
383
%token	VALUES
unknown's avatar
unknown committed
384 385 386 387
%token	VARIABLES
%token	WHERE
%token	WITH
%token	WRITE_SYM
unknown's avatar
unknown committed
388
%token	X509_SYM
unknown's avatar
unknown committed
389
%token	XOR
unknown's avatar
unknown committed
390
%token	COMPRESSED_SYM
unknown's avatar
unknown committed
391

unknown's avatar
unknown committed
392 393 394
%token  ERRORS
%token  WARNINGS

unknown's avatar
unknown committed
395
%token	ASCII_SYM
unknown's avatar
unknown committed
396 397 398
%token	BIGINT
%token	BLOB_SYM
%token	CHAR_SYM
unknown's avatar
unknown committed
399
%token	CHANGED
unknown's avatar
unknown committed
400 401 402 403 404 405
%token	COALESCE
%token	DATETIME
%token	DATE_SYM
%token	DECIMAL_SYM
%token	DOUBLE_SYM
%token	ENUM
406
%token	FAST_SYM
unknown's avatar
unknown committed
407
%token	FLOAT_SYM
unknown's avatar
unknown committed
408
%token  GEOMETRY_SYM
unknown's avatar
unknown committed
409 410 411 412 413 414 415 416 417
%token	INT_SYM
%token	LIMIT
%token	LONGBLOB
%token	LONGTEXT
%token	MEDIUMBLOB
%token	MEDIUMINT
%token	MEDIUMTEXT
%token	NUMERIC_SYM
%token	PRECISION
unknown's avatar
unknown committed
418
%token	QUICK
unknown's avatar
unknown committed
419
%token	REAL
unknown's avatar
unknown committed
420
%token	SIGNED_SYM
unknown's avatar
unknown committed
421 422 423 424 425 426 427 428
%token	SMALLINT
%token	STRING_SYM
%token	TEXT_SYM
%token	TIMESTAMP
%token	TIME_SYM
%token	TINYBLOB
%token	TINYINT
%token	TINYTEXT
unknown's avatar
unknown committed
429
%token	ULONGLONG_NUM
unknown's avatar
unknown committed
430
%token	UNSIGNED
431 432 433
%token	VARBINARY
%token	VARCHAR
%token	VARYING
unknown's avatar
unknown committed
434 435
%token	ZEROFILL

unknown's avatar
unknown committed
436
%token	AGAINST
unknown's avatar
unknown committed
437 438 439 440 441 442
%token	ATAN
%token	BETWEEN_SYM
%token	BIT_AND
%token	BIT_OR
%token	CASE_SYM
%token	CONCAT
unknown's avatar
unknown committed
443
%token	CONCAT_WS
unknown's avatar
unknown committed
444 445 446 447 448 449 450 451 452 453
%token	CURDATE
%token	CURTIME
%token	DATABASE
%token	DATE_ADD_INTERVAL
%token	DATE_SUB_INTERVAL
%token	DAY_HOUR_SYM
%token	DAY_MINUTE_SYM
%token	DAY_SECOND_SYM
%token	DAY_SYM
%token	DECODE_SYM
unknown's avatar
unknown committed
454 455
%token	DES_ENCRYPT_SYM
%token	DES_DECRYPT_SYM
unknown's avatar
unknown committed
456 457 458 459 460 461 462 463 464 465
%token	ELSE
%token	ELT_FUNC
%token	ENCODE_SYM
%token	ENCRYPT
%token	EXPORT_SET
%token	EXTRACT_SYM
%token	FIELD_FUNC
%token	FORMAT_SYM
%token	FOR_SYM
%token	FROM_UNIXTIME
unknown's avatar
unknown committed
466 467
%token	GEOMCOLLFROMTEXT
%token	GEOMFROMTEXT
unknown's avatar
unknown committed
468
%token  GEOMETRYCOLLECTION
unknown's avatar
unknown committed
469 470 471 472 473 474
%token	GROUP_UNIQUE_USERS
%token	HOUR_MINUTE_SYM
%token	HOUR_SECOND_SYM
%token	HOUR_SYM
%token	IDENTIFIED_SYM
%token	IF
475
%token	INSERT_METHOD
unknown's avatar
unknown committed
476 477 478
%token	INTERVAL_SYM
%token	LAST_INSERT_ID
%token	LEFT
unknown's avatar
unknown committed
479
%token	LINEFROMTEXT
unknown's avatar
unknown committed
480
%token  LINESTRING
unknown's avatar
unknown committed
481 482
%token	LOCATE
%token	MAKE_SET_SYM
483
%token	MASTER_POS_WAIT
unknown's avatar
unknown committed
484 485
%token	MINUTE_SECOND_SYM
%token	MINUTE_SYM
unknown's avatar
unknown committed
486
%token	MODE_SYM
unknown's avatar
unknown committed
487 488
%token	MODIFY_SYM
%token	MONTH_SYM
unknown's avatar
unknown committed
489 490 491
%token	MLINEFROMTEXT
%token	MPOINTFROMTEXT
%token	MPOLYFROMTEXT
unknown's avatar
unknown committed
492 493 494
%token  MULTILINESTRING
%token  MULTIPOINT
%token  MULTIPOLYGON
unknown's avatar
unknown committed
495 496
%token	NOW_SYM
%token	PASSWORD
unknown's avatar
unknown committed
497
%token	POINTFROMTEXT
498
%token	POINT
unknown's avatar
unknown committed
499
%token	POLYFROMTEXT
unknown's avatar
unknown committed
500
%token  POLYGON
unknown's avatar
unknown committed
501 502 503 504 505 506 507
%token	POSITION_SYM
%token	PROCEDURE
%token	RAND
%token	REPLACE
%token	RIGHT
%token	ROUND
%token	SECOND_SYM
unknown's avatar
unknown committed
508
%token	SHARE_SYM
unknown's avatar
unknown committed
509 510 511 512 513 514 515 516 517 518 519 520 521 522
%token	SUBSTRING
%token	SUBSTRING_INDEX
%token	TRIM
%token	UDA_CHAR_SUM
%token	UDA_FLOAT_SUM
%token	UDA_INT_SUM
%token	UDF_CHAR_FUNC
%token	UDF_FLOAT_FUNC
%token	UDF_INT_FUNC
%token	UNIQUE_USERS
%token	UNIX_TIMESTAMP
%token	USER
%token	WEEK_SYM
%token	WHEN_SYM
unknown's avatar
unknown committed
523
%token	WORK_SYM
unknown's avatar
unknown committed
524 525 526
%token	YEAR_MONTH_SYM
%token	YEAR_SYM
%token	YEARWEEK
unknown's avatar
unknown committed
527 528 529
%token	BENCHMARK_SYM
%token	END
%token	THEN_SYM
unknown's avatar
unknown committed
530

unknown's avatar
unknown committed
531 532 533 534
%token	SQL_BIG_RESULT
%token	SQL_CACHE_SYM
%token	SQL_CALC_FOUND_ROWS
%token	SQL_NO_CACHE_SYM
unknown's avatar
unknown committed
535
%token	SQL_SMALL_RESULT
unknown's avatar
unknown committed
536
%token  SQL_BUFFER_RESULT
unknown's avatar
unknown committed
537

538 539 540 541 542
%token  CURSOR_SYM
%token  ELSEIF_SYM
%token  ITERATE_SYM
%token  LEAVE_SYM
%token  LOOP_SYM
543
%token  REPEAT_SYM
544 545 546 547 548
%token  UNTIL_SYM
%token  WHILE_SYM
%token  ASENSITIVE_SYM
%token  INSENSITIVE_SYM
%token  SENSITIVE_SYM
549

unknown's avatar
unknown committed
550 551
%token  ISSUER_SYM
%token  SUBJECT_SYM
552
%token  CIPHER_SYM
unknown's avatar
unknown committed
553

unknown's avatar
unknown committed
554 555 556 557 558 559 560 561 562
%left   SET_VAR
%left	OR_OR_CONCAT OR
%left	AND
%left	BETWEEN_SYM CASE_SYM WHEN_SYM THEN_SYM ELSE
%left	EQ EQUAL_SYM GE GT_SYM LE LT NE IS LIKE REGEXP IN_SYM
%left	'|'
%left	'&'
%left	SHIFT_LEFT SHIFT_RIGHT
%left	'-' '+'
563
%left	'*' '/' '%' DIV_SYM MOD_SYM
unknown's avatar
unknown committed
564
%left	NEG '~'
unknown's avatar
unknown committed
565 566
%left   XOR
%left   '^'
unknown's avatar
unknown committed
567
%right	NOT
unknown's avatar
unknown committed
568
%right	BINARY COLLATE_SYM
569 570 571 572 573 574
/* These don't actually affect the way the query is really evaluated, but
   they silence a few warnings for shift/reduce conflicts. */
%left	','
%left	STRAIGHT_JOIN JOIN_SYM
%nonassoc	CROSS INNER_SYM NATURAL LEFT RIGHT

unknown's avatar
unknown committed
575 576
%type <lex_str>
	IDENT TEXT_STRING REAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM LEX_HOSTNAME
577
	ULONGLONG_NUM field_ident select_alias ident ident_or_text
unknown's avatar
unknown committed
578
        UNDERSCORE_CHARSET
unknown's avatar
unknown committed
579 580 581 582 583

%type <lex_str_ptr>
	opt_table_alias

%type <table>
584
	table_ident references
unknown's avatar
unknown committed
585 586

%type <simple_string>
587
	remember_name remember_end opt_ident opt_db text_or_password
unknown's avatar
unknown committed
588 589 590
	opt_escape

%type <string>
591
	text_string
unknown's avatar
unknown committed
592 593

%type <num>
unknown's avatar
unknown committed
594
	type int_type real_type order_dir opt_field_spec lock_option
unknown's avatar
unknown committed
595
	udf_type if_exists opt_local opt_table_options table_options
unknown's avatar
unknown committed
596
	table_option opt_if_not_exists opt_var_type opt_var_ident_type
unknown's avatar
unknown committed
597
	delete_option opt_temporary all_or_any
unknown's avatar
unknown committed
598 599

%type <ulong_num>
600
	ULONG_NUM raid_types merge_insert_types
unknown's avatar
unknown committed
601

unknown's avatar
unknown committed
602 603
%type <ulonglong_number>
	ulonglong_num
unknown's avatar
unknown committed
604

unknown's avatar
unknown committed
605 606 607
%type <lock_type>
	replace_lock_option opt_low_priority insert_lock_option load_data_lock

unknown's avatar
unknown committed
608
%type <item>
unknown's avatar
unknown committed
609
	literal text_literal insert_ident order_ident
unknown's avatar
unknown committed
610 611
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
	table_wild opt_pad no_in_expr expr_expr simple_expr no_and_expr
612
	using_list expr_or_default set_expr_or_default interval_expr
unknown's avatar
unknown committed
613
	param_marker singlerow_subselect singlerow_subselect_init
unknown's avatar
unknown committed
614
	exists_subselect exists_subselect_init
unknown's avatar
unknown committed
615 616

%type <item_list>
unknown's avatar
unknown committed
617
	expr_list udf_expr_list when_list ident_list ident_list_arg
unknown's avatar
unknown committed
618 619

%type <key_type>
620
	key_type opt_unique_or_fulltext
unknown's avatar
unknown committed
621

unknown's avatar
unknown committed
622 623 624
%type <key_alg>
	key_alg opt_btree_or_rtree

unknown's avatar
unknown committed
625 626 627 628 629 630 631
%type <string_list>
	key_usage_list

%type <key_part>
	key_part

%type <table_list>
632
	join_table_list  join_table
unknown's avatar
unknown committed
633 634 635 636 637 638 639 640 641 642 643

%type <udf>
	UDF_CHAR_FUNC UDF_FLOAT_FUNC UDF_INT_FUNC
	UDA_CHAR_SUM UDA_FLOAT_SUM UDA_INT_SUM

%type <interval> interval

%type <db_type> table_types

%type <row_type> row_types

unknown's avatar
unknown committed
644
%type <tx_isolation> isolation_types
unknown's avatar
unknown committed
645

unknown's avatar
unknown committed
646 647
%type <ha_rkey_mode> handler_rkey_mode

unknown's avatar
unknown committed
648 649
%type <cast_type> cast_type

unknown's avatar
unknown committed
650 651 652 653 654 655
%type <udf_type> udf_func_type

%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword

%type <lex_user> user grant_user

656
%type <charset>
657 658
	charset_name
	charset_name_or_default
659 660
	collation_name
	collation_name_or_default
661

unknown's avatar
unknown committed
662 663
%type <variable> internal_variable_name

unknown's avatar
unknown committed
664 665
%type <select_lex> in_subselect in_subselect_init

unknown's avatar
unknown committed
666 667
%type <boolfunc2creator> comp_op

unknown's avatar
unknown committed
668
%type <NONE>
unknown's avatar
unknown committed
669
	query verb_clause create change select do drop insert replace insert2
670 671
	insert_values update delete truncate rename
	show describe load alter optimize flush
unknown's avatar
unknown committed
672
	reset purge begin commit rollback slave master_def master_defs
unknown's avatar
unknown committed
673
	repair restore backup analyze check start
674
	field_list field_list_item field_spec kill
unknown's avatar
unknown committed
675
	select_item_list select_item values_list no_braces
676
	opt_limit_clause delete_limit_clause fields opt_values values
unknown's avatar
unknown committed
677
	procedure_list procedure_list2 procedure_item
678
	when_list2 expr_list2  handler
unknown's avatar
unknown committed
679 680 681
	opt_precision opt_ignore opt_column opt_restrict
	grant revoke set lock unlock string_list field_options field_option
	field_opt_list opt_binary table_lock_list table_lock varchar
682
	ref_list opt_on_delete opt_on_delete_list opt_on_delete_item use
683
	opt_delete_options opt_delete_option
unknown's avatar
unknown committed
684
	opt_outer table_list table_name opt_option opt_place
unknown's avatar
unknown committed
685 686
	opt_attribute opt_attribute_list attribute column_list column_list_id
	opt_column_list grant_privileges opt_table user_list grant_option
unknown's avatar
unknown committed
687
	grant_privilege grant_privilege_list
unknown's avatar
unknown committed
688
	flush_options flush_option
unknown's avatar
unknown committed
689 690
	equal optional_braces opt_key_definition key_usage_list2
	opt_mi_check_type opt_to mi_check_types normal_join
691
	table_to_table_list table_to_table opt_table_list opt_as
unknown's avatar
unknown committed
692
	handler_rkey_function handler_read_or_scan
unknown's avatar
unknown committed
693
	single_multi table_wild_list table_wild_one opt_wild
694
	union_clause union_list union_option
695
	precision subselect_start opt_and
696
	subselect_end select_var_list select_var_list_init help opt_len
697
	statement
698
END_OF_INPUT
unknown's avatar
unknown committed
699

700
%type <NONE> call sp_proc_stmts sp_proc_stmt
701 702
%type <num>  sp_decls sp_decl sp_decl_idents sp_opt_inout

unknown's avatar
unknown committed
703 704
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
705 706
	',' '!' '{' '}' '&' '|' AND OR OR_OR_CONCAT BETWEEN_SYM CASE_SYM
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM
unknown's avatar
unknown committed
707 708 709 710 711
%%


query:
	END_OF_INPUT
unknown's avatar
unknown committed
712
	{
713
	   THD *thd= YYTHD;
714
	   if (!thd->bootstrap &&
unknown's avatar
unknown committed
715
	      (!(thd->lex.select_lex.options & OPTION_FOUND_COMMENT)))
716
	   {
717
	     send_error(thd,ER_EMPTY_QUERY);
718
	     YYABORT;
719
	   }
720 721 722 723
	   else
	   {
	     thd->lex.sql_command = SQLCOM_EMPTY_QUERY;
	   }
unknown's avatar
unknown committed
724
	}
unknown's avatar
unknown committed
725
	| verb_clause END_OF_INPUT {};
unknown's avatar
unknown committed
726 727

verb_clause:
728 729 730 731 732 733
	  statement
	| begin
	;

/* Verb clauses, except begin */
statement:
unknown's avatar
unknown committed
734 735
	  alter
	| analyze
736
	| backup
737
	| call
unknown's avatar
unknown committed
738 739 740 741 742 743
	| change
	| check
	| commit
	| create
	| delete
	| describe
unknown's avatar
unknown committed
744
	| do
unknown's avatar
unknown committed
745 746 747 748 749 750 751 752
	| drop
	| grant
	| insert
	| flush
	| load
	| lock
	| kill
	| optimize
753
	| purge
754
	| rename
unknown's avatar
unknown committed
755 756
        | repair
	| replace
unknown's avatar
unknown committed
757
	| reset
758
	| restore
unknown's avatar
unknown committed
759 760 761 762
	| revoke
	| rollback
	| select
	| set
unknown's avatar
unknown committed
763
	| slave
unknown's avatar
unknown committed
764
	| start
unknown's avatar
unknown committed
765
	| show
766
	| truncate
767
	| handler
unknown's avatar
unknown committed
768 769
	| unlock
	| update
unknown's avatar
unknown committed
770 771
	| use
	| help;
772

unknown's avatar
unknown committed
773 774
/* help */

775
help:
776
       HELP_SYM ident_or_text
unknown's avatar
unknown committed
777
       {
778 779 780
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
	  lex->help_arg= $2.str;
781
       };
unknown's avatar
unknown committed
782 783 784 785

/* change master */

change:
unknown's avatar
unknown committed
786
       CHANGE MASTER_SYM TO_SYM
unknown's avatar
unknown committed
787 788 789
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
unknown's avatar
unknown committed
790
	  bzero((char*) &lex->mi, sizeof(lex->mi));
791 792 793 794
        }
       master_defs
	{}
       ;
unknown's avatar
unknown committed
795 796 797

master_defs:
       master_def
unknown's avatar
unknown committed
798
       | master_defs ',' master_def;
unknown's avatar
unknown committed
799

unknown's avatar
unknown committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
master_def:
       MASTER_HOST_SYM EQ TEXT_STRING
       {
	 Lex->mi.host = $3.str;
       }
       |
       MASTER_USER_SYM EQ TEXT_STRING
       {
	 Lex->mi.user = $3.str;
       }
       |
       MASTER_PASSWORD_SYM EQ TEXT_STRING
       {
	 Lex->mi.password = $3.str;
       }
       |
       MASTER_LOG_FILE_SYM EQ TEXT_STRING
       {
	 Lex->mi.log_file_name = $3.str;
       }
       |
unknown's avatar
unknown committed
821
       MASTER_PORT_SYM EQ ULONG_NUM
unknown's avatar
unknown committed
822 823 824 825
       {
	 Lex->mi.port = $3;
       }
       |
unknown's avatar
unknown committed
826
       MASTER_LOG_POS_SYM EQ ulonglong_num
unknown's avatar
unknown committed
827 828 829 830
       {
	 Lex->mi.pos = $3;
       }
       |
unknown's avatar
unknown committed
831
       MASTER_CONNECT_RETRY_SYM EQ ULONG_NUM
unknown's avatar
unknown committed
832 833 834
       {
	 Lex->mi.connect_retry = $3;
       }
835 836 837 838 839 840 841 842 843
       |
       RELAY_LOG_FILE_SYM EQ TEXT_STRING
       {
	 Lex->mi.relay_log_name = $3.str;
       }
       |
       RELAY_LOG_POS_SYM EQ ULONG_NUM
       {
	 Lex->mi.relay_log_pos = $3;
844 845
       }
       ;
unknown's avatar
unknown committed
846

unknown's avatar
unknown committed
847

unknown's avatar
unknown committed
848 849 850 851 852
/* create a table */

create:
	CREATE opt_table_options TABLE_SYM opt_if_not_exists table_ident
	{
853
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
854 855
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_CREATE_TABLE;
unknown's avatar
unknown committed
856
	  if (!lex->select_lex.add_table_to_list(thd,$5,
857
						 ($2 &
858 859
						  HA_LEX_CREATE_TMP_TABLE ?
						  &tmp_table_alias :
unknown's avatar
unknown committed
860 861
						  (LEX_STRING*) 0),
						 TL_OPTION_UPDATING,
unknown's avatar
unknown committed
862 863 864
						 ((using_update_log)?
						  TL_READ_NO_INSERT:
						  TL_READ)))
unknown's avatar
unknown committed
865 866 867 868 869 870 871
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
	  lex->change=NullS;
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	  lex->create_info.options=$2 | $4;
unknown's avatar
unknown committed
872
	  lex->create_info.db_type= (enum db_type) lex->thd->variables.table_type;
873
	  lex->create_info.table_charset=thd->db_charset?thd->db_charset:default_charset_info;
unknown's avatar
unknown committed
874
	  lex->name=0;
unknown's avatar
unknown committed
875 876
	}
	create2
unknown's avatar
unknown committed
877
	{}
unknown's avatar
unknown committed
878
	| CREATE opt_unique_or_fulltext INDEX ident key_alg ON table_ident
unknown's avatar
unknown committed
879
	  {
unknown's avatar
unknown committed
880 881
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_CREATE_INDEX;
unknown's avatar
unknown committed
882 883
	    if (!lex->current_select->add_table_to_list(lex->thd, $7, NULL,
							TL_OPTION_UPDATING))
unknown's avatar
unknown committed
884
	      YYABORT;
unknown's avatar
unknown committed
885 886 887 888
	    lex->create_list.empty();
	    lex->key_list.empty();
	    lex->col_list.empty();
	    lex->change=NullS;
unknown's avatar
unknown committed
889
	  }
890
	   '(' key_list ')'
unknown's avatar
unknown committed
891
	  {
unknown's avatar
unknown committed
892
	    LEX *lex=Lex;
unknown's avatar
unknown committed
893

894
	    lex->key_list.push_back(new Key($2,$4.str, $5, lex->col_list));
unknown's avatar
unknown committed
895
	    lex->col_list.empty();
unknown's avatar
unknown committed
896
	  }
897 898 899
	| CREATE DATABASE opt_if_not_exists ident 
	  { Lex->create_info.table_charset=NULL; }
	  opt_create_database_options
unknown's avatar
unknown committed
900
	  {
unknown's avatar
unknown committed
901 902 903 904
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_CREATE_DB;
	    lex->name=$4.str;
            lex->create_info.options=$3;
unknown's avatar
unknown committed
905
	  }
906
	| CREATE udf_func_type FUNCTION_SYM IDENT
unknown's avatar
unknown committed
907
	  {
unknown's avatar
unknown committed
908
	    LEX *lex=Lex;
909
	    lex->udf.name = $4;
unknown's avatar
unknown committed
910
	    lex->udf.type= $2;
unknown's avatar
unknown committed
911
	  }
912 913
	  create_function_tail
	  {}
914 915 916 917
	| CREATE PROCEDURE ident
	  {
	    LEX *lex= Lex;

918 919 920
	    lex->spcont= new sp_pcontext();
	    lex->sphead= new sp_head(&$3, lex);
	    lex->sphead->m_type= TYPE_ENUM_PROCEDURE;
921
	  }
922
          '(' sp_pdparam_list ')'
923 924 925
	  {
	    Lex->spcont->set_params();
	  }
926
	  sp_proc_stmt
927 928 929 930 931
	  {
	    Lex->sql_command= SQLCOM_CREATE_PROCEDURE;
	  } 
	;

932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
create_function_tail:
	  RETURNS_SYM udf_type UDF_SONAME_SYM TEXT_STRING
	  {
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_CREATE_FUNCTION;
	    lex->udf.returns=(Item_result) $2;
	    lex->udf.dl=$4.str;
	  }
	| '('
	  {
	    LEX *lex= Lex;

	    lex->sql_command = SQLCOM_CREATE_PROCEDURE;
	    lex->spcont= new sp_pcontext();
	    lex->sphead= new sp_head(&lex->udf.name, lex);
	    lex->sphead->m_type= TYPE_ENUM_FUNCTION;
	  }
          sp_fdparam_list ')'
	  {
	    Lex->spcont->set_params();
	  }
	  RETURNS_SYM type
	  {
	    Lex->sphead->m_returns= (enum enum_field_types)$7;
	  }
	  sp_proc_stmt
	  {}
	;

961 962 963 964 965 966
call:
	  CALL_SYM ident
	  {
	    LEX *lex = Lex;

	    lex->sql_command= SQLCOM_CALL;
967
	    lex->udf.name= $2;
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
	    lex->value_list.empty();
	  }
          '(' sp_cparam_list ')' {}
	;

/* CALL parameters */
sp_cparam_list:
	  /* Empty */
	| sp_cparams
	;

sp_cparams:
	  sp_cparams ',' expr
	  {
	    Lex->value_list.push_back($3);
	  }
	| expr
	  {
	    Lex->value_list.push_back($1);
	  }
	;

990 991
/* Stored FUNCTION parameter declaration list */
sp_fdparam_list:
992
	  /* Empty */
993
	| sp_fdparams
994 995
	;

996 997 998
sp_fdparams:
	  sp_fdparams ',' sp_fdparam
	| sp_fdparam
999 1000
	;

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
sp_fdparam:
	  ident type sp_opt_locator
	  {
	    Lex->spcont->push(&$1, (enum enum_field_types)$2, sp_param_in);
	  }
	;

/* Stored PROCEDURE parameter declaration list */
sp_pdparam_list:
	  /* Empty */
	| sp_pdparams
	;

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
	  sp_opt_inout ident type sp_opt_locator
	  {
	    Lex->spcont->push(&$2,
	                      (enum enum_field_types)$3,
			      (sp_param_mode_t)$1);
	  }
	;

sp_opt_inout:
	  /* Empty */ { $$= sp_param_in; }
	| IN_SYM      { $$= sp_param_in; }
	| OUT_SYM     { $$= sp_param_out; }
	| INOUT_SYM   { $$= sp_param_inout; }
	;

sp_opt_locator:
	  /* Empty */
	| AS LOCATOR_SYM
	;

sp_proc_stmts:
1041 1042
	  sp_proc_stmt ';'
	| sp_proc_stmts sp_proc_stmt ';'
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 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
	;

sp_decls:
	  /* Empty */
	  {
	    $$= 0;
	  }
	| sp_decls sp_decl ';'
	  {
	    $$= $1 + $2;
	  }
	;

sp_decl:
	  DECLARE_SYM sp_decl_idents type
	  {
	    LEX *lex= Lex;
	    uint max= lex->spcont->current_framesize();

	    for (uint i = max-$2 ; i < max ; i++)
	    {
	      lex->spcont->set_type(i, (enum enum_field_types)$3);
	      lex->spcont->set_isset(i, FALSE);
	    }
	    $$= $2;
	  }
	;

sp_decl_idents:
	  ident
	  {
	    Lex->spcont->push(&$1, (enum_field_types)0, sp_param_in);
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
	    Lex->spcont->push(&$3, (enum_field_types)0, sp_param_in);
	    $$= $1 + 1;
	  }
	;

sp_proc_stmt:
1085 1086 1087
	  {
	    Lex->sphead->reset_lex(YYTHD);
	  }
1088 1089 1090 1091
	  statement
	  {
	    LEX *lex= Lex;

1092 1093 1094 1095 1096 1097 1098
	    if (lex->sql_command == SQLCOM_SELECT && !lex->result)
	    {
	      send_error(YYTHD, ER_SP_BADSELECT);
	      YYABORT;
	    }
	    else
	    {
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
	      /* Don't add an instruction for empty SET statements.
	      ** (This happens if the SET only contained local variables,
	      **  which get their set instructions generated separately.)
	      */
	      if (lex->sql_command != SQLCOM_SET_OPTION ||
	          !lex->var_list.is_empty())
	      {
	        sp_instr_stmt *i= new sp_instr_stmt(lex->sphead->instructions());

	        i->set_lex(lex);
	        lex->sphead->add_instr(i);
              }
1111 1112
	      lex->sphead->restore_lex(YYTHD);
	    }
1113
          }
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
	| RETURN_SYM expr
	  {
	    LEX *lex= Lex;

	    if (lex->sphead->m_type == TYPE_ENUM_PROCEDURE)
	    {
	      send_error(YYTHD, ER_SP_BADRETURN);
	      YYABORT;
	    }
	    else
	    {
	      /* QQ nothing yet */
	    }
	  }
1128
	| IF sp_if END IF {}
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
	| CASE_SYM WHEN_SYM
	  {
	    Lex->sphead->m_simple_case= FALSE;
	  }
	  sp_case END CASE_SYM {}
	| CASE_SYM expr WHEN_SYM
	  {
	    /* We "fake" this by using an anonymous variable which we
	       set to the expression. Note that all WHENs are evaluate
	       at the same frame level, so we then know that it's the
	       top-most variable in the frame. */
	    LEX *lex= Lex;
	    uint offset= lex->spcont->current_framesize();
	    sp_instr_set *i = new sp_instr_set(lex->sphead->instructions(),
	                                       offset, $2, MYSQL_TYPE_STRING);
	    LEX_STRING dummy;

	    dummy.str= (char *)"";
	    dummy.length= 0;
	    lex->spcont->push(&dummy, MYSQL_TYPE_STRING, sp_param_in);
	    lex->sphead->add_instr(i);
	    lex->sphead->m_simple_case= TRUE;
	  }
	  sp_case END CASE_SYM
	  {
	    Lex->spcont->pop();
	  }
1156 1157 1158 1159 1160
	| sp_labeled_control
	  {}
	| { /* Unlabeled controls get a secret label. */
	    LEX *lex= Lex;

1161
	    lex->spcont->push_label((char *)"", lex->sphead->instructions());
1162 1163 1164
	  }
	  sp_unlabeled_control
	  {
1165 1166
	    LEX *lex= Lex;

1167
	    lex->sphead->backpatch(lex->spcont->pop_label());
1168 1169 1170 1171
	  }
	| LEAVE_SYM IDENT
	  {
	    LEX *lex= Lex;
1172
	    sp_head *sp = lex->sphead;
1173 1174 1175 1176
	    sp_label_t *lab= lex->spcont->find_label($2.str);

	    if (! lab)
	    {
1177
	      send_error(YYTHD, ER_SP_LILABEL_MISMATCH, "LEAVE");
1178 1179 1180 1181
	      YYABORT;
	    }
	    else
	    {
1182
	      sp_instr_jump *i= new sp_instr_jump(sp->instructions());
1183

1184 1185
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
1186 1187 1188 1189 1190 1191 1192 1193 1194
	    }
	  }
	| ITERATE_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->find_label($2.str);

	    if (! lab)
	    {
1195
	      send_error(YYTHD, ER_SP_LILABEL_MISMATCH, "ITERATE");
1196 1197 1198 1199 1200
	      YYABORT;
	    }
	    else
	    {
	      uint ip= lex->sphead->instructions();
1201
	      sp_instr_jump *i= new sp_instr_jump(ip, lab->ip); /* Jump back */
1202 1203 1204 1205

              lex->sphead->add_instr(i);
	    }
	  }
1206
	;
unknown's avatar
unknown committed
1207

1208 1209 1210 1211 1212 1213 1214 1215
sp_if:
	  expr THEN_SYM
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, $1);

1216
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
            sp->add_instr(i);
	  }
	  sp_proc_stmts
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump *i = new sp_instr_jump(ip);

	    sp->add_instr(i);
	    sp->backpatch(ctx->pop_label());
1228
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
1229 1230 1231 1232 1233
	  }
	  sp_elseifs
	  {
	    LEX *lex= Lex;

1234
	    lex->sphead->backpatch(lex->spcont->pop_label());
1235 1236 1237 1238 1239 1240 1241 1242 1243
	  }
	;

sp_elseifs:
	  /* Empty */
	| ELSEIF_SYM sp_if
	| ELSE sp_proc_stmts
	;

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 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
sp_case:
	  expr THEN_SYM
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

	    if (! sp->m_simple_case)
	      i= new sp_instr_jump_if_not(ip, $1);
	    else
	    { /* Simple case: <caseval> = <whenval> */
	      Item *var= (Item*) new Item_splocal(ctx->current_framesize()-1);
	      Item *expr= Item_bool_func2::eq_creator(var, $1);

	      i= new sp_instr_jump_if_not(ip, expr);
	    }
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
            sp->add_instr(i);
	  }
	  sp_proc_stmts
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump *i = new sp_instr_jump(ip);

	    sp->add_instr(i);
	    sp->backpatch(ctx->pop_label());
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
	  }
	  sp_whens
	  {
	    LEX *lex= Lex;

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
	;

sp_whens:
1284 1285 1286
	  /* Empty */ {}
	| WHEN_SYM sp_case {}
	| ELSE sp_proc_stmts {}
1287 1288
	;

1289 1290 1291 1292 1293 1294 1295 1296
sp_labeled_control:
	  IDENT ':'
	  {
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->find_label($1.str);

	    if (lab)
	    {
1297
	      send_error(YYTHD, ER_SP_LABEL_REDEFINE);
1298 1299 1300
	      YYABORT;
	    }
	    else
1301
	    {
1302 1303
	      lex->spcont->push_label($1.str,
	                              lex->sphead->instructions());
1304
	    }
1305 1306 1307 1308 1309 1310
	  }
	  sp_unlabeled_control IDENT
	  {
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->find_label($5.str);

1311
	    if (! lab || strcasecmp($5.str, lab->name) != 0)
1312
	    {
1313
	      send_error(YYTHD, ER_SP_LABEL_MISMATCH);
1314 1315 1316 1317 1318
	      YYABORT;
	    }
	    else
	    {
	      lex->spcont->pop_label();
1319
	      lex->sphead->backpatch(lab);
1320 1321 1322 1323 1324
	    }
	  }
	;

sp_unlabeled_control:
1325
	  BEGIN_SYM
1326 1327 1328
	    sp_decls
	    sp_proc_stmts
	  END
1329 1330 1331
	  { /* QQ This is just a dummy for grouping declarations and statements
	       together. No [[NOT] ATOMIC] yet, and we need to figure out how
	       make it coexist with the existing BEGIN COMMIT/ROLLBACK. */
1332 1333 1334
	    Lex->spcont->pop($2);
	  }
	| LOOP_SYM
1335 1336 1337 1338 1339 1340 1341 1342 1343
	  sp_proc_stmts END LOOP_SYM
	  {
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lab->ip);

	    lex->sphead->add_instr(i);
	  }
1344
	| WHILE_SYM expr DO_SYM
1345 1346
	  {
	    LEX *lex= Lex;
1347 1348
	    sp_head *sp= lex->sphead;
	    uint ip= sp->instructions();
1349 1350
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, $2);

1351 1352 1353
	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
            sp->add_instr(i);
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
	  }
	  sp_proc_stmts END WHILE_SYM
	  {
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lab->ip);

	    lex->sphead->add_instr(i);
	  }
1364 1365
	| REPEAT_SYM sp_proc_stmts UNTIL_SYM expr END REPEAT_SYM
	  {
1366 1367 1368 1369 1370 1371 1372
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, $4, lab->ip);

            lex->sphead->add_instr(i);
	  }
1373
	;
unknown's avatar
unknown committed
1374 1375

create2:
unknown's avatar
unknown committed
1376
	'(' field_list ')' opt_create_table_options create3 {}
1377
	| opt_create_table_options create3 {}
unknown's avatar
unknown committed
1378 1379 1380 1381 1382 1383 1384
	| LIKE table_ident
      	  {
      	    LEX *lex=Lex;
     	    if (!(lex->name= (char *)$2))
              YYABORT;
    	  }
  	  ;
unknown's avatar
unknown committed
1385 1386

create3:
1387 1388
	/* empty */ {}
	| opt_duplicate opt_as SELECT_SYM
unknown's avatar
unknown committed
1389
          {
unknown's avatar
unknown committed
1390 1391 1392
	    LEX *lex=Lex;
	    lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
	    mysql_init_select(lex);
unknown's avatar
unknown committed
1393
          }
1394
          select_options select_item_list opt_select_from union_clause {}
1395
          ;
unknown's avatar
unknown committed
1396

1397 1398
opt_as:
	/* empty */ {}
unknown's avatar
unknown committed
1399
	| AS	    {};
1400

1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
	  COLLATE_SYM collation_name_or_default	
	  { Lex->create_info.table_charset=$2; }
	| opt_default CHAR_SYM SET charset_name_or_default
	  { Lex->create_info.table_charset=$4; }
	| opt_default CHARSET charset_name_or_default
	  { Lex->create_info.table_charset=$3; };

unknown's avatar
unknown committed
1417
opt_table_options:
unknown's avatar
unknown committed
1418
	/* empty */	 { $$= 0; }
unknown's avatar
unknown committed
1419
	| table_options  { $$= $1;};
unknown's avatar
unknown committed
1420 1421 1422

table_options:
	table_option	{ $$=$1; }
unknown's avatar
unknown committed
1423
	| table_option table_options { $$= $1 | $2; };
unknown's avatar
unknown committed
1424

unknown's avatar
unknown committed
1425
table_option:
unknown's avatar
unknown committed
1426
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
unknown's avatar
unknown committed
1427 1428

opt_if_not_exists:
unknown's avatar
unknown committed
1429
	/* empty */	 { $$= 0; }
unknown's avatar
unknown committed
1430
	| IF NOT EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
unknown's avatar
unknown committed
1431 1432 1433

opt_create_table_options:
	/* empty */
unknown's avatar
unknown committed
1434
	| create_table_options;
unknown's avatar
unknown committed
1435

unknown's avatar
unknown committed
1436 1437 1438 1439
create_table_options_space_separated:
	create_table_option
	| create_table_option create_table_options_space_separated;

unknown's avatar
unknown committed
1440 1441
create_table_options:
	create_table_option
1442
	| create_table_option     create_table_options
unknown's avatar
unknown committed
1443
	| create_table_option ',' create_table_options;
unknown's avatar
unknown committed
1444 1445

create_table_option:
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
	TYPE_SYM opt_equal table_types          { Lex->create_info.db_type= $3; }
	| MAX_ROWS opt_equal ulonglong_num	{ Lex->create_info.max_rows= $3; Lex->create_info.used_fields|= HA_CREATE_USED_MAX_ROWS;}
	| MIN_ROWS opt_equal ulonglong_num	{ Lex->create_info.min_rows= $3; Lex->create_info.used_fields|= HA_CREATE_USED_MIN_ROWS;}
	| AVG_ROW_LENGTH opt_equal ULONG_NUM	{ Lex->create_info.avg_row_length=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AVG_ROW_LENGTH;}
	| PASSWORD opt_equal TEXT_STRING	{ Lex->create_info.password=$3.str; }
	| COMMENT_SYM opt_equal TEXT_STRING	{ Lex->create_info.comment=$3.str; }
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
	| PACK_KEYS_SYM opt_equal ULONG_NUM	{ Lex->create_info.table_options|= $3 ? HA_OPTION_PACK_KEYS : HA_OPTION_NO_PACK_KEYS; Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;}
	| PACK_KEYS_SYM opt_equal DEFAULT	{ Lex->create_info.table_options&= ~(HA_OPTION_PACK_KEYS | HA_OPTION_NO_PACK_KEYS); Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;}
	| CHECKSUM_SYM opt_equal ULONG_NUM	{ Lex->create_info.table_options|= $3 ? HA_OPTION_CHECKSUM : HA_OPTION_NO_CHECKSUM; }
	| DELAY_KEY_WRITE_SYM opt_equal ULONG_NUM { Lex->create_info.table_options|= $3 ? HA_OPTION_DELAY_KEY_WRITE : HA_OPTION_NO_DELAY_KEY_WRITE; }
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3; }
	| RAID_TYPE opt_equal raid_types	{ Lex->create_info.raid_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| RAID_CHUNKS opt_equal ULONG_NUM	{ Lex->create_info.raid_chunks= $3; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| RAID_CHUNKSIZE opt_equal ULONG_NUM	{ Lex->create_info.raid_chunksize= $3*RAID_BLOCK_SIZE; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| UNION_SYM opt_equal '(' table_list ')'
1462 1463 1464
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
1465 1466
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
1467 1468
	    lex->create_info.merge_list.elements--;
	    lex->create_info.merge_list.first= (byte*) (table_list->next);
1469 1470
	    lex->select_lex.table_list.elements=1;
	    lex->select_lex.table_list.next= (byte**) &(table_list->next);
1471
	    table_list->next=0;
1472
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
1473
	  }
1474
	| opt_default CHARSET opt_equal charset_name_or_default
1475
	  {
1476 1477 1478
	    Lex->create_info.table_charset= $4;
	    Lex->create_info.used_fields|= HA_CREATE_USED_CHARSET;
	  }
1479
	| opt_default CHAR_SYM SET opt_equal charset_name_or_default
1480
	  {
1481
	    Lex->create_info.table_charset= $5;
1482 1483
	    Lex->create_info.used_fields|= HA_CREATE_USED_CHARSET;
	  }
1484 1485 1486 1487 1488
	| COLLATE_SYM opt_equal collation_name_or_default
	  {
	    Lex->create_info.table_charset= $3;
	    Lex->create_info.used_fields|= HA_CREATE_USED_CHARSET;
	  }
1489 1490 1491
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
	| DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING { Lex->create_info.data_file_name= $4.str; }
	| INDEX DIRECTORY_SYM opt_equal TEXT_STRING    { Lex->create_info.index_file_name= $4.str; };
unknown's avatar
unknown committed
1492 1493 1494 1495 1496 1497 1498

table_types:
	ISAM_SYM	{ $$= DB_TYPE_ISAM; }
	| MYISAM_SYM	{ $$= DB_TYPE_MYISAM; }
	| MERGE_SYM	{ $$= DB_TYPE_MRG_MYISAM; }
	| HEAP_SYM	{ $$= DB_TYPE_HEAP; }
	| BERKELEY_DB_SYM { $$= DB_TYPE_BERKELEY_DB; }
unknown's avatar
unknown committed
1499
	| INNOBASE_SYM  { $$= DB_TYPE_INNODB; };
unknown's avatar
unknown committed
1500 1501 1502 1503 1504

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
unknown's avatar
unknown committed
1505
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; };
unknown's avatar
unknown committed
1506 1507 1508 1509

raid_types:
	RAID_STRIPED_SYM { $$= RAID_TYPE_0; }
	| RAID_0_SYM	 { $$= RAID_TYPE_0; }
unknown's avatar
unknown committed
1510
	| ULONG_NUM	 { $$=$1;};
unknown's avatar
unknown committed
1511

1512 1513 1514
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
unknown's avatar
unknown committed
1515
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
1516

unknown's avatar
unknown committed
1517
opt_select_from:
1518
	opt_limit_clause {}
unknown's avatar
unknown committed
1519
	| FROM DUAL_SYM {}
unknown's avatar
unknown committed
1520
	| select_from select_lock_type;
unknown's avatar
unknown committed
1521 1522 1523

udf_func_type:
	/* empty */ 	{ $$ = UDFTYPE_FUNCTION; }
unknown's avatar
unknown committed
1524
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
unknown's avatar
unknown committed
1525 1526 1527 1528

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
unknown's avatar
unknown committed
1529
	| INT_SYM {$$ = (int) INT_RESULT; };
unknown's avatar
unknown committed
1530 1531 1532

field_list:
	  field_list_item
unknown's avatar
unknown committed
1533
	| field_list ',' field_list_item;
unknown's avatar
unknown committed
1534 1535 1536


field_list_item:
1537
	  field_spec check_constraint
unknown's avatar
unknown committed
1538 1539 1540 1541
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
unknown's avatar
unknown committed
1542
	| key_type opt_ident key_alg '(' key_list ')'
unknown's avatar
unknown committed
1543
	  {
unknown's avatar
unknown committed
1544
	    LEX *lex=Lex;
1545
	    lex->key_list.push_back(new Key($1,$2, $3, lex->col_list));
unknown's avatar
unknown committed
1546
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
1547
	  }
1548
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
unknown's avatar
unknown committed
1549
	  {
1550 1551 1552 1553 1554 1555 1556 1557
	    LEX *lex=Lex;
	    lex->key_list.push_back(new foreign_key($4, lex->col_list,
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
1558
	  }
1559
	| opt_constraint check_constraint
unknown's avatar
unknown committed
1560 1561
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
1562 1563 1564 1565 1566 1567 1568
	  }
	;

check_constraint:
	/* empty */
	| CHECK_SYM expr
	;
unknown's avatar
unknown committed
1569 1570 1571

opt_constraint:
	/* empty */
unknown's avatar
unknown committed
1572
	| CONSTRAINT opt_ident;
unknown's avatar
unknown committed
1573 1574 1575 1576

field_spec:
	field_ident
	 {
unknown's avatar
unknown committed
1577 1578
	   LEX *lex=Lex;
	   lex->length=lex->dec=0; lex->type=0; lex->interval=0;
1579
	   lex->default_value=lex->comment=0;
1580
	   lex->charset=NULL;
unknown's avatar
unknown committed
1581 1582 1583
	 }
	type opt_attribute
	{
unknown's avatar
unknown committed
1584
	  LEX *lex=Lex;
unknown's avatar
unknown committed
1585
	  if (add_field_to_list(lex->thd, $1.str,
unknown's avatar
unknown committed
1586
				(enum enum_field_types) $3,
unknown's avatar
unknown committed
1587
				lex->length,lex->dec,lex->type,
1588
				lex->default_value, lex->comment,
1589
				lex->change,lex->interval,lex->charset))
unknown's avatar
unknown committed
1590
	    YYABORT;
unknown's avatar
unknown committed
1591
	};
unknown's avatar
unknown committed
1592 1593

type:
1594
	int_type opt_len field_options	{ $$=$1; }
unknown's avatar
unknown committed
1595 1596 1597 1598 1599 1600
	| real_type opt_precision field_options { $$=$1; }
	| FLOAT_SYM float_options field_options { $$=FIELD_TYPE_FLOAT; }
	| BIT_SYM opt_len		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
	| BOOL_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
1601 1602
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
1603
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
unknown's avatar
unknown committed
1604 1605 1606
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
1607
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
1608
					  Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
1609 1610 1611 1612
					  $$=FIELD_TYPE_STRING; }
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
					  $$=FIELD_TYPE_VAR_STRING; }
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
unknown's avatar
unknown committed
1613
					  Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
1614
					  $$=FIELD_TYPE_VAR_STRING; }
1615
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
unknown's avatar
unknown committed
1616 1617
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
1618 1619
	| TIMESTAMP
	  {
1620
	    if (YYTHD->variables.sql_mode & MODE_SAPDB)
1621 1622
	      $$=FIELD_TYPE_DATETIME;
	    else
1623
	      $$=FIELD_TYPE_TIMESTAMP;
1624
	   }
unknown's avatar
unknown committed
1625 1626 1627
	| TIMESTAMP '(' NUM ')'		{ Lex->length=$3.str;
					  $$=FIELD_TYPE_TIMESTAMP; }
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
unknown's avatar
unknown committed
1628
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
1629
					  $$=FIELD_TYPE_TINY_BLOB; }
unknown's avatar
unknown committed
1630
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
1631
					  $$=FIELD_TYPE_BLOB; }
unknown's avatar
unknown committed
1632
	| GEOMETRY_SYM			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
1633
					  $$=FIELD_TYPE_GEOMETRY; }
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
	| POINT				{ Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_GEOMETRY; }
	| MULTIPOINT			{ Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_GEOMETRY; }
	| LINESTRING			{ Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_GEOMETRY; }
	| MULTILINESTRING		{ Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_GEOMETRY; }
	| POLYGON			{ Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_GEOMETRY; }
	| MULTIPOLYGON			{ Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_GEOMETRY; }
unknown's avatar
unknown committed
1646
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
1647
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
1648
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
1649
					  $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
1650
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
1651
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
1652 1653
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
1654
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
1655 1656
	| MEDIUMTEXT opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| LONGTEXT opt_binary		{ $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
1657 1658 1659 1660
	| DECIMAL_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
	| NUMERIC_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
1661 1662
	| FIXED_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
1663
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
unknown's avatar
unknown committed
1664
	  {
unknown's avatar
unknown committed
1665 1666
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
unknown's avatar
unknown committed
1667 1668
	    $$=FIELD_TYPE_ENUM;
	  }
1669
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
unknown's avatar
unknown committed
1670
	  {
unknown's avatar
unknown committed
1671 1672
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
unknown's avatar
unknown committed
1673
	    $$=FIELD_TYPE_SET;
1674
	  }
1675
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
1676 1677 1678 1679 1680 1681
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
1682
	;
unknown's avatar
unknown committed
1683 1684 1685 1686

char:
	CHAR_SYM {}
	| NCHAR_SYM {}
unknown's avatar
unknown committed
1687
	| NATIONAL_SYM CHAR_SYM {};
unknown's avatar
unknown committed
1688

unknown's avatar
unknown committed
1689 1690 1691 1692
varchar:
	char VARYING {}
	| VARCHAR {}
	| NATIONAL_SYM VARCHAR {}
unknown's avatar
unknown committed
1693
	| NCHAR_SYM VARCHAR {};
unknown's avatar
unknown committed
1694 1695 1696 1697 1698 1699

int_type:
	INT_SYM		{ $$=FIELD_TYPE_LONG; }
	| TINYINT	{ $$=FIELD_TYPE_TINY; }
	| SMALLINT	{ $$=FIELD_TYPE_SHORT; }
	| MEDIUMINT	{ $$=FIELD_TYPE_INT24; }
unknown's avatar
unknown committed
1700
	| BIGINT	{ $$=FIELD_TYPE_LONGLONG; };
unknown's avatar
unknown committed
1701 1702

real_type:
1703
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
unknown's avatar
unknown committed
1704 1705
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
unknown's avatar
unknown committed
1706
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
unknown's avatar
unknown committed
1707 1708 1709 1710 1711


float_options:
	/* empty */		{}
	| '(' NUM ')'		{ Lex->length=$2.str; }
unknown's avatar
unknown committed
1712
	| precision		{};
unknown's avatar
unknown committed
1713 1714 1715 1716 1717 1718

precision:
	'(' NUM ',' NUM ')'
	{
	  LEX *lex=Lex;
	  lex->length=$2.str; lex->dec=$4.str;
unknown's avatar
unknown committed
1719
	};
unknown's avatar
unknown committed
1720 1721 1722

field_options:
	/* empty */		{}
unknown's avatar
unknown committed
1723
	| field_opt_list	{};
unknown's avatar
unknown committed
1724 1725 1726

field_opt_list:
	field_opt_list field_option {}
unknown's avatar
unknown committed
1727
	| field_option {};
unknown's avatar
unknown committed
1728 1729

field_option:
unknown's avatar
unknown committed
1730
	SIGNED_SYM	{}
1731
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
unknown's avatar
unknown committed
1732
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
unknown's avatar
unknown committed
1733 1734

opt_len:
1735 1736
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
unknown's avatar
unknown committed
1737 1738 1739

opt_precision:
	/* empty */	{}
unknown's avatar
unknown committed
1740
	| precision	{};
unknown's avatar
unknown committed
1741 1742 1743

opt_attribute:
	/* empty */ {}
unknown's avatar
unknown committed
1744
	| opt_attribute_list {};
unknown's avatar
unknown committed
1745 1746 1747

opt_attribute_list:
	opt_attribute_list attribute {}
unknown's avatar
unknown committed
1748
	| attribute;
unknown's avatar
unknown committed
1749 1750 1751 1752 1753 1754

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
	| NOT NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
	| DEFAULT literal { Lex->default_value=$2; }
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
1755
	| SERIAL_SYM DEFAULT VALUE_SYM
unknown's avatar
unknown committed
1756 1757
	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; }
	| opt_primary KEY_SYM { Lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; }
unknown's avatar
unknown committed
1758
	| UNIQUE_SYM	  { Lex->type|= UNIQUE_FLAG; }
1759
	| UNIQUE_SYM KEY_SYM { Lex->type|= UNIQUE_KEY_FLAG; }
1760
	| COMMENT_SYM text_literal { Lex->comment= $2; }
1761
	| COLLATE_SYM collation_name { Lex->charset=$2; };
unknown's avatar
unknown committed
1762

unknown's avatar
unknown committed
1763

1764
charset_name:
unknown's avatar
unknown committed
1765
	ident
1766
	{
1767
	  if (!($$=get_charset_by_csname($1.str,MYF(0))))
1768
	  {
1769
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,$1.str);
1770 1771 1772 1773
	    YYABORT;
	  }
	};

1774 1775 1776
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
unknown's avatar
unknown committed
1777

1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
collation_name:
	ident
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,$1.str);
	    YYABORT;
	  }
	};

collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

1792 1793 1794 1795
opt_default:
	/* empty */	{}
	| DEFAULT	{};

unknown's avatar
unknown committed
1796
opt_binary:
1797
	/* empty */			{ Lex->charset=NULL; }
unknown's avatar
unknown committed
1798 1799 1800
	| ASCII_SYM			{ Lex->charset=&my_charset_latin1; }
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
	| BINARY			{ Lex->charset=&my_charset_bin; }
unknown's avatar
unknown committed
1801 1802 1803 1804 1805 1806 1807 1808
	| UNICODE_SYM
	{
	  if (!(Lex->charset=get_charset_by_name("ucs2",MYF(0))))
	  {
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,"ucs2");
	    YYABORT;
	  }
	}
1809
	| CHAR_SYM SET charset_name	{ Lex->charset=$3; } ;
unknown's avatar
unknown committed
1810

unknown's avatar
unknown committed
1811 1812 1813
opt_primary:
	/* empty */
	| PRIMARY_SYM
1814
	;
unknown's avatar
unknown committed
1815

unknown's avatar
unknown committed
1816
references:
1817 1818
	REFERENCES table_ident
	{
1819
	  LEX *lex=Lex;
1820 1821 1822 1823 1824 1825
	  lex->fk_delete_opt= lex->fk_update_opt= lex->fk_match_option= 0;
	  lex->ref_list.empty();
	}
	opt_ref_list
	{
	  $$=$2;
unknown's avatar
unknown committed
1826
	};
1827

1828
opt_ref_list:
unknown's avatar
unknown committed
1829
	/* empty */ opt_on_delete {}
unknown's avatar
unknown committed
1830
	| '(' ref_list ')' opt_on_delete {};
1831 1832 1833

ref_list:
	ref_list ',' ident	{ Lex->ref_list.push_back(new key_part_spec($3.str)); }
unknown's avatar
unknown committed
1834
	| ident			{ Lex->ref_list.push_back(new key_part_spec($1.str)); };
1835

unknown's avatar
unknown committed
1836 1837 1838

opt_on_delete:
	/* empty */ {}
unknown's avatar
unknown committed
1839
	| opt_on_delete_list {};
unknown's avatar
unknown committed
1840 1841 1842

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
unknown's avatar
unknown committed
1843
	| opt_on_delete_item {};
unknown's avatar
unknown committed
1844 1845

opt_on_delete_item:
1846 1847 1848 1849
	ON DELETE_SYM delete_option   { Lex->fk_delete_opt= $3; }
	| ON UPDATE_SYM delete_option { Lex->fk_update_opt= $3; }
	| MATCH FULL	{ Lex->fk_match_option= foreign_key::FK_MATCH_FULL; }
	| MATCH PARTIAL { Lex->fk_match_option= foreign_key::FK_MATCH_PARTIAL; }
unknown's avatar
unknown committed
1850
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
unknown's avatar
unknown committed
1851 1852

delete_option:
1853 1854 1855 1856
	RESTRICT	 { $$= (int) foreign_key::FK_OPTION_RESTRICT; }
	| CASCADE	 { $$= (int) foreign_key::FK_OPTION_CASCADE; }
	| SET NULL_SYM   { $$= (int) foreign_key::FK_OPTION_SET_NULL; }
	| NO_SYM ACTION  { $$= (int) foreign_key::FK_OPTION_NO_ACTION; }
unknown's avatar
unknown committed
1857
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
unknown's avatar
unknown committed
1858 1859 1860 1861

key_type:
	opt_constraint PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
	| key_or_index			    { $$= Key::MULTIPLE; }
1862 1863
	| FULLTEXT_SYM			    { $$= Key::FULLTEXT; }
	| FULLTEXT_SYM key_or_index	    { $$= Key::FULLTEXT; }
unknown's avatar
unknown committed
1864 1865
	| SPATIAL_SYM			    { $$= Key::SPATIAL; }
	| SPATIAL_SYM key_or_index	    { $$= Key::SPATIAL; }
unknown's avatar
unknown committed
1866
	| opt_constraint UNIQUE_SYM	    { $$= Key::UNIQUE; }
unknown's avatar
unknown committed
1867
	| opt_constraint UNIQUE_SYM key_or_index { $$= Key::UNIQUE; };
unknown's avatar
unknown committed
1868 1869 1870

key_or_index:
	KEY_SYM {}
unknown's avatar
unknown committed
1871
	| INDEX {};
unknown's avatar
unknown committed
1872 1873 1874 1875

keys_or_index:
	KEYS {}
	| INDEX {}
unknown's avatar
unknown committed
1876
	| INDEXES {};
unknown's avatar
unknown committed
1877

1878
opt_unique_or_fulltext:
unknown's avatar
unknown committed
1879 1880
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
unknown's avatar
unknown committed
1881
	| SPATIAL_SYM	{ $$= Key::SPATIAL; };
unknown's avatar
unknown committed
1882 1883

key_alg:
1884
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
1885
	| USING opt_btree_or_rtree { $$= $2; }
unknown's avatar
unknown committed
1886
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
unknown's avatar
unknown committed
1887 1888 1889 1890

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
	| RTREE_SYM	{ $$= HA_KEY_ALG_RTREE; }
unknown's avatar
unknown committed
1891
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
unknown's avatar
unknown committed
1892 1893 1894

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
unknown's avatar
unknown committed
1895
	| key_part order_dir		{ Lex->col_list.push_back($1); };
unknown's avatar
unknown committed
1896 1897 1898

key_part:
	ident			{ $$=new key_part_spec($1.str); }
unknown's avatar
unknown committed
1899
	| ident '(' NUM ')'	{ $$=new key_part_spec($1.str,(uint) atoi($3.str)); };
unknown's avatar
unknown committed
1900 1901 1902

opt_ident:
	/* empty */	{ $$=(char*) 0; }	/* Defaultlength */
unknown's avatar
unknown committed
1903
	| field_ident	{ $$=$1.str; };
unknown's avatar
unknown committed
1904 1905 1906

string_list:
	text_string			{ Lex->interval_list.push_back($1); }
unknown's avatar
unknown committed
1907
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
unknown's avatar
unknown committed
1908 1909 1910 1911 1912 1913 1914 1915

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
1916
	  THD *thd= YYTHD;
1917
	  LEX *lex=&thd->lex;
unknown's avatar
unknown committed
1918 1919
	  lex->sql_command = SQLCOM_ALTER_TABLE;
	  lex->name=0;
unknown's avatar
unknown committed
1920 1921
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
unknown's avatar
unknown committed
1922 1923 1924 1925 1926 1927 1928
	    YYABORT;
	  lex->drop_primary=0;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
	  lex->drop_list.empty();
	  lex->alter_list.empty();
1929
          lex->select_lex.init_order();
1930
	  lex->select_lex.db=lex->name=0;
1931
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
unknown's avatar
unknown committed
1932
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
1933
	  lex->create_info.table_charset=thd->db_charset?thd->db_charset:default_charset_info;
1934
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
1935 1936
          lex->alter_keys_onoff=LEAVE_AS_IS;
          lex->simple_alter=1;
unknown's avatar
unknown committed
1937
	}
1938 1939
	alter_list
	{}
1940
	| ALTER DATABASE ident opt_create_database_options
1941 1942 1943 1944
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
	    lex->name=$3.str;
1945
	  }
1946
	| ALTER PROCEDURE ident
1947 1948 1949 1950
	  /* QQ Characteristics missing for now */
	  opt_restrict
	  {
	    LEX *lex=Lex;
1951

1952 1953 1954
	    /* This is essensially an no-op right now, since we haven't
	       put the characteristics in yet. */
	    lex->sql_command= SQLCOM_ALTER_PROCEDURE;
1955
	    lex->udf.name= $3;
1956 1957
	  }
	;
1958

unknown's avatar
unknown committed
1959
alter_list:
unknown's avatar
unknown committed
1960
        | alter_list_item
unknown's avatar
unknown committed
1961
	| alter_list ',' alter_list_item;
unknown's avatar
unknown committed
1962 1963

add_column:
unknown's avatar
unknown committed
1964
	ADD opt_column { Lex->change=0; };
unknown's avatar
unknown committed
1965 1966

alter_list_item:
1967 1968
	add_column field_list_item opt_place { Lex->simple_alter=0; }
	| add_column '(' field_list ')'      { Lex->simple_alter=0; }
unknown's avatar
unknown committed
1969 1970 1971 1972 1973
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
	     lex->change= $3.str; lex->simple_alter=0;
	  }
1974
          field_spec opt_place
unknown's avatar
unknown committed
1975 1976 1977 1978 1979
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
            lex->length=lex->dec=0; lex->type=0; lex->interval=0;
            lex->default_value=lex->comment=0;
unknown's avatar
unknown committed
1980
            lex->simple_alter=0;
unknown's avatar
unknown committed
1981 1982 1983 1984
          }
          type opt_attribute
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
1985
            if (add_field_to_list(lex->thd,$3.str,
unknown's avatar
unknown committed
1986 1987 1988
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
                                  lex->default_value, lex->comment,
1989
				  $3.str, lex->interval, lex->charset))
unknown's avatar
unknown committed
1990 1991 1992
	       YYABORT;
          }
          opt_place
unknown's avatar
unknown committed
1993
	| DROP opt_column field_ident opt_restrict
unknown's avatar
unknown committed
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
	  {
	    LEX *lex=Lex;
	    lex->drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
					    $3.str)); lex->simple_alter=0;
	  }
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
	    lex->drop_primary=1; lex->simple_alter=0;
	  }
2004
	| DROP FOREIGN KEY_SYM opt_ident { Lex->simple_alter=0; }
unknown's avatar
unknown committed
2005
	| DROP key_or_index field_ident
unknown's avatar
unknown committed
2006 2007 2008 2009 2010 2011
	  {
	    LEX *lex=Lex;
	    lex->drop_list.push_back(new Alter_drop(Alter_drop::KEY,
						    $3.str));
	    lex->simple_alter=0;
	  }
2012 2013
	| DISABLE_SYM KEYS { Lex->alter_keys_onoff=DISABLE; }
	| ENABLE_SYM KEYS  { Lex->alter_keys_onoff=ENABLE; }
unknown's avatar
unknown committed
2014
	| ALTER opt_column field_ident SET DEFAULT literal
unknown's avatar
unknown committed
2015 2016 2017 2018 2019
	  {
	    LEX *lex=Lex;
	    lex->alter_list.push_back(new Alter_column($3.str,$6));
	    lex->simple_alter=0;
	  }
unknown's avatar
unknown committed
2020
	| ALTER opt_column field_ident DROP DEFAULT
unknown's avatar
unknown committed
2021 2022 2023 2024 2025
	  {
	    LEX *lex=Lex;
	    lex->alter_list.push_back(new Alter_column($3.str,(Item*) 0));
	    lex->simple_alter=0;
	  }
2026
	| RENAME opt_to table_ident
2027
	  {
2028
	    LEX *lex=Lex;
2029
	    lex->select_lex.db=$3->db.str;
2030
	    lex->name= $3->table.str;
2031
	  }
unknown's avatar
unknown committed
2032
        | create_table_options_space_separated { Lex->simple_alter=0; }
unknown's avatar
unknown committed
2033
	| order_clause         { Lex->simple_alter=0; };
unknown's avatar
unknown committed
2034 2035 2036

opt_column:
	/* empty */	{}
unknown's avatar
unknown committed
2037
	| COLUMN_SYM	{};
unknown's avatar
unknown committed
2038 2039 2040

opt_ignore:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
unknown's avatar
unknown committed
2041
	| IGNORE_SYM	{ Lex->duplicates=DUP_IGNORE; };
unknown's avatar
unknown committed
2042 2043 2044 2045

opt_restrict:
	/* empty */	{}
	| RESTRICT	{}
unknown's avatar
unknown committed
2046
	| CASCADE	{};
unknown's avatar
unknown committed
2047 2048 2049 2050

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
unknown's avatar
unknown committed
2051
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
unknown's avatar
unknown committed
2052 2053 2054 2055

opt_to:
	/* empty */	{}
	| TO_SYM	{}
2056
	| EQ		{}
unknown's avatar
unknown committed
2057
	| AS		{};
unknown's avatar
unknown committed
2058

unknown's avatar
unknown committed
2059
/*
unknown's avatar
unknown committed
2060 2061 2062
  The first two deprecate the last two--delete the last two for 4.1 release
*/

unknown's avatar
unknown committed
2063
slave:
2064
	START_SYM SLAVE slave_thread_opts
unknown's avatar
unknown committed
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076
        {
	  LEX *lex=Lex;
          lex->sql_command = SQLCOM_SLAVE_START;
	  lex->type = 0;
        }
        | STOP_SYM SLAVE slave_thread_opts
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_STOP;
	    lex->type = 0;
          }
	;
unknown's avatar
unknown committed
2077

unknown's avatar
unknown committed
2078 2079 2080 2081 2082
start:
	START_SYM TRANSACTION_SYM { Lex->sql_command = SQLCOM_BEGIN;}
	{}
	;

unknown's avatar
unknown committed
2083
slave_thread_opts:
2084 2085
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
unknown's avatar
unknown committed
2086
	;
2087 2088

slave_thread_opt_list:
unknown's avatar
unknown committed
2089
	slave_thread_opt
2090 2091
	| slave_thread_opt_list ',' slave_thread_opt
	;
2092 2093

slave_thread_opt:
2094
	/*empty*/	{}
unknown's avatar
unknown committed
2095 2096 2097
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
	| IO_THREAD   	{ Lex->slave_thd_opt|=SLAVE_IO; }
	;
2098

unknown's avatar
unknown committed
2099 2100 2101 2102 2103 2104 2105 2106
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
	}
	table_list FROM TEXT_STRING
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
2107
        };
unknown's avatar
unknown committed
2108

unknown's avatar
unknown committed
2109 2110 2111 2112 2113 2114 2115 2116
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
	}
	table_list TO_SYM TEXT_STRING
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
2117
        };
unknown's avatar
unknown committed
2118

unknown's avatar
unknown committed
2119 2120 2121
repair:
	REPAIR table_or_tables
	{
unknown's avatar
unknown committed
2122 2123 2124
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
	   lex->check_opt.init();
unknown's avatar
unknown committed
2125
	}
2126 2127 2128
	table_list opt_mi_repair_type
	{}
	;
unknown's avatar
unknown committed
2129

unknown's avatar
unknown committed
2130
opt_mi_repair_type:
unknown's avatar
unknown committed
2131
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
2132
	| mi_repair_types {};
unknown's avatar
unknown committed
2133

unknown's avatar
unknown committed
2134 2135
mi_repair_types:
	mi_repair_type {}
unknown's avatar
unknown committed
2136
	| mi_repair_type mi_repair_types {};
2137

unknown's avatar
unknown committed
2138
mi_repair_type:
unknown's avatar
unknown committed
2139
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
2140
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
unknown's avatar
unknown committed
2141
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
unknown's avatar
unknown committed
2142 2143

analyze:
2144
	ANALYZE_SYM table_or_tables
unknown's avatar
unknown committed
2145
	{
unknown's avatar
unknown committed
2146 2147 2148
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
	   lex->check_opt.init();
unknown's avatar
unknown committed
2149
	}
2150 2151 2152
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
2153 2154

check:
unknown's avatar
unknown committed
2155
	CHECK_SYM table_or_tables
unknown's avatar
unknown committed
2156
	{
unknown's avatar
unknown committed
2157 2158 2159
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECK;
	   lex->check_opt.init();
unknown's avatar
unknown committed
2160
	}
2161 2162 2163
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
2164

unknown's avatar
unknown committed
2165 2166
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
2167
	| mi_check_types {};
unknown's avatar
unknown committed
2168 2169 2170

mi_check_types:
	mi_check_type {}
unknown's avatar
unknown committed
2171
	| mi_check_type mi_check_types {};
unknown's avatar
unknown committed
2172 2173 2174 2175 2176 2177

mi_check_type:
	QUICK      { Lex->check_opt.flags|= T_QUICK; }
	| FAST_SYM { Lex->check_opt.flags|= T_FAST; }
	| MEDIUM_SYM { Lex->check_opt.flags|= T_MEDIUM; }
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
unknown's avatar
unknown committed
2178
	| CHANGED  { Lex->check_opt.flags|= T_CHECK_ONLY_CHANGED; };
unknown's avatar
unknown committed
2179

unknown's avatar
unknown committed
2180
optimize:
2181
	OPTIMIZE table_or_tables
unknown's avatar
unknown committed
2182
	{
unknown's avatar
unknown committed
2183 2184 2185
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
	   lex->check_opt.init();
unknown's avatar
unknown committed
2186
	}
2187 2188 2189
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
2190

2191 2192 2193 2194 2195
rename:
	RENAME table_or_tables
	{
	   Lex->sql_command=SQLCOM_RENAME_TABLE;
	}
2196 2197 2198
	table_to_table_list
	{}
	;
2199 2200 2201

table_to_table_list:
	table_to_table
unknown's avatar
unknown committed
2202
	| table_to_table_list ',' table_to_table;
2203 2204 2205

table_to_table:
	table_ident TO_SYM table_ident
2206
	{
unknown's avatar
unknown committed
2207 2208
	  LEX *lex=Lex;	  
	  SELECT_LEX_NODE *sl= lex->current_select;
unknown's avatar
unknown committed
2209 2210 2211 2212
	  if (!sl->add_table_to_list(lex->thd, $1,NULL,TL_OPTION_UPDATING,
				     TL_IGNORE) ||
	      !sl->add_table_to_list(lex->thd, $3,NULL,TL_OPTION_UPDATING,
				     TL_IGNORE))
2213
	    YYABORT;
2214
	};
2215

unknown's avatar
unknown committed
2216
/*
unknown's avatar
unknown committed
2217
  Select : retrieve data from table
unknown's avatar
unknown committed
2218 2219 2220 2221
*/


select:
unknown's avatar
unknown committed
2222
	select_init { Lex->sql_command=SQLCOM_SELECT; };
unknown's avatar
unknown committed
2223

2224
/* Need select_init2 for subselects. */
unknown's avatar
unknown committed
2225
select_init:
2226
	SELECT_SYM select_init2
2227
	|
2228 2229
	'(' SELECT_SYM select_part2 ')'
	  {
2230
	    LEX *lex= Lex;
unknown's avatar
unknown committed
2231
            SELECT_LEX * sel= lex->current_select->select_lex();
unknown's avatar
unknown committed
2232
	    if (sel->set_braces(1))
2233 2234 2235 2236
	    {
	      send_error(lex->thd, ER_SYNTAX_ERROR);
	      YYABORT;
	    }
2237 2238
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
2239 2240 2241 2242
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
2243
            /* select in braces, can't contain global parameters */
2244
            sel->master_unit()->global_parameters=
2245
               sel->master_unit();
unknown's avatar
unknown committed
2246
          } union_opt;
2247

2248 2249
select_init2:
	select_part2
2250
        {
2251
	  LEX *lex= Lex;
unknown's avatar
unknown committed
2252
          SELECT_LEX * sel= lex->current_select->select_lex();
unknown's avatar
unknown committed
2253
          if (lex->current_select->set_braces(0))
2254 2255 2256 2257
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
2258 2259
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
2260 2261 2262 2263
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
2264
	}
2265
	union_clause
2266 2267
	;

2268
select_part2:
unknown's avatar
unknown committed
2269 2270
	{
	  LEX *lex=Lex;
unknown's avatar
unknown committed
2271
	  SELECT_LEX * sel= lex->current_select->select_lex();
unknown's avatar
unknown committed
2272
	  if (lex->current_select == &lex->select_lex)
2273 2274
	    lex->lock_option= TL_READ; /* Only for global SELECT */
	  mysql_init_select(lex);
unknown's avatar
unknown committed
2275
	}
unknown's avatar
unknown committed
2276
	select_options select_item_list select_into select_lock_type;
2277

unknown's avatar
unknown committed
2278
select_into:
2279 2280 2281 2282 2283 2284
	opt_limit_clause {}
	| FROM DUAL_SYM /* oracle compatibility: oracle always requires FROM
                           clause, and DUAL is system table without fields.
                           Is "SELECT 1 FROM DUAL" any better than
                           "SELECT 1" ? Hmmm :) */
        | into
unknown's avatar
unknown committed
2285
	| select_from
2286 2287
	| into select_from
	| select_from into;
unknown's avatar
unknown committed
2288 2289

select_from:
2290
	FROM join_table_list where_clause group_clause having_clause opt_order_clause opt_limit_clause procedure_clause;
unknown's avatar
unknown committed
2291 2292 2293

select_options:
	/* empty*/
unknown's avatar
unknown committed
2294
	| select_option_list;
unknown's avatar
unknown committed
2295 2296 2297

select_option_list:
	select_option_list select_option
unknown's avatar
unknown committed
2298
	| select_option;
unknown's avatar
unknown committed
2299 2300

select_option:
2301
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
2302 2303 2304 2305 2306 2307
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
2308 2309 2310
	| DISTINCT	{ Select->options|= SELECT_DISTINCT; }
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322
	| SQL_BUFFER_RESULT
	  {
	    if (check_simple_select())
	      YYABORT;
	    Select->options|= OPTION_BUFFER_RESULT;
	  }
	| SQL_CALC_FOUND_ROWS
	  {
	    if (check_simple_select())
	      YYABORT;
	    Select->options|= OPTION_FOUND_ROWS;
	  }
unknown's avatar
unknown committed
2323
	| SQL_NO_CACHE_SYM { Lex->uncacheable(); }
2324 2325 2326
	| SQL_CACHE_SYM    { Select->options|= OPTION_TO_QUERY_CACHE; }
	| ALL		{}
	;
unknown's avatar
unknown committed
2327

unknown's avatar
unknown committed
2328 2329 2330
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
2331 2332
	  {
	    LEX *lex=Lex;
2333
	    lex->current_select->set_lock_for_tables(TL_WRITE);
2334
	    lex->safe_to_cache_query=0;
2335
	  }
unknown's avatar
unknown committed
2336
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
2337 2338
	  {
	    LEX *lex=Lex;
2339 2340
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
2341
	    lex->safe_to_cache_query=0;
2342 2343
	  }
	;
unknown's avatar
unknown committed
2344

unknown's avatar
unknown committed
2345 2346 2347 2348 2349
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
2350 2351
	    THD *thd= YYTHD;
	    if (add_item_to_list(thd, new Item_field(NULL, NULL, "*")))
unknown's avatar
unknown committed
2352
	      YYABORT;
2353
	    (thd->lex.current_select->select_lex()->with_wild)++;
unknown's avatar
unknown committed
2354
	  };
unknown's avatar
unknown committed
2355 2356 2357 2358 2359


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
unknown's avatar
unknown committed
2360
	    if (add_item_to_list(YYTHD, $2))
unknown's avatar
unknown committed
2361 2362 2363 2364 2365
	      YYABORT;
	    if ($4.str)
	      $2->set_name($4.str);
	    else if (!$2->name)
	      $2->set_name($1,(uint) ($3 - $1));
unknown's avatar
unknown committed
2366
	  };
unknown's avatar
unknown committed
2367 2368

remember_name:
unknown's avatar
unknown committed
2369
	{ $$=(char*) Lex->tok_start; };
unknown's avatar
unknown committed
2370 2371

remember_end:
unknown's avatar
unknown committed
2372
	{ $$=(char*) Lex->tok_end; };
unknown's avatar
unknown committed
2373 2374 2375

select_item2:
	table_wild	{ $$=$1; } /* table.* */
unknown's avatar
unknown committed
2376
	| expr		{ $$=$1; };
unknown's avatar
unknown committed
2377 2378 2379 2380 2381 2382

select_alias:
	{ $$.str=0;}
	| AS ident { $$=$2; }
	| AS TEXT_STRING  { $$=$2; }
	| ident { $$=$1; }
unknown's avatar
unknown committed
2383
	| TEXT_STRING  { $$=$1; };
unknown's avatar
unknown committed
2384 2385 2386

optional_braces:
	/* empty */ {}
unknown's avatar
unknown committed
2387
	| '(' ')' {};
unknown's avatar
unknown committed
2388

unknown's avatar
unknown committed
2389
/* all possible expressions */
unknown's avatar
SCRUM  
unknown committed
2390 2391 2392 2393
expr:	
	expr_expr	{ $$= $1; }
	| simple_expr	{ $$= $1; }
	;
unknown's avatar
unknown committed
2394

unknown's avatar
unknown committed
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
comp_op:  EQ		{ $$ = &comp_eq_creator; }
	| GE		{ $$ = &comp_ge_creator; }
	| GT_SYM	{ $$ = &comp_gt_creator; }
	| LE		{ $$ = &comp_le_creator; }
	| LT		{ $$ = &comp_lt_creator; }
	| NE		{ $$ = &comp_ne_creator; }
	;

all_or_any: ALL     { $$ = 1; }
        |   ANY_SYM { $$ = 0; }
        ;

unknown's avatar
unknown committed
2407 2408
/* expressions that begin with 'expr' */
expr_expr:
unknown's avatar
SCRUM  
unknown committed
2409
	 expr IN_SYM '(' expr_list ')'
unknown's avatar
unknown committed
2410 2411 2412
	  { $$= new Item_func_in($1,*$4); }
	| expr NOT IN_SYM '(' expr_list ')'
	  { $$= new Item_func_not(new Item_func_in($1,*$5)); }
unknown's avatar
unknown committed
2413
        | expr IN_SYM in_subselect
unknown's avatar
unknown committed
2414
          { $$= new Item_in_subselect(YYTHD, $1, $3); }
unknown's avatar
unknown committed
2415
	| expr NOT IN_SYM in_subselect
2416
          {
unknown's avatar
unknown committed
2417
            $$= new Item_func_not(new Item_in_subselect(YYTHD, $1, $4));
unknown's avatar
unknown committed
2418
          }
unknown's avatar
unknown committed
2419 2420 2421 2422
	| expr BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_between($1,$3,$5); }
	| expr NOT BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
2423
	| expr OR_OR_CONCAT expr { $$= or_or_concat(YYTHD, $1,$3); }
unknown's avatar
unknown committed
2424
	| expr OR expr		{ $$= new Item_cond_or($1,$3); }
unknown's avatar
unknown committed
2425
        | expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
unknown's avatar
unknown committed
2426
	| expr AND expr		{ $$= new Item_cond_and($1,$3); }
unknown's avatar
unknown committed
2427
	| expr SOUNDS_SYM LIKE expr { $$= Item_bool_func2::eq_creator(new Item_func_soundex($1), new Item_func_soundex($4));}
unknown's avatar
unknown committed
2428 2429 2430 2431 2432 2433 2434
	| expr LIKE simple_expr opt_escape { $$= new Item_func_like($1,$3,$4); }
	| expr NOT LIKE simple_expr opt_escape	{ $$= new Item_func_not(new Item_func_like($1,$4,$5));}
	| expr REGEXP expr { $$= new Item_func_regex($1,$3); }
	| expr NOT REGEXP expr { $$= new Item_func_not(new Item_func_regex($1,$4)); }
	| expr IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| expr IS NOT NULL_SYM { $$= new Item_func_isnotnull($1); }
	| expr EQUAL_SYM expr	{ $$= new Item_func_equal($1,$3); }
unknown's avatar
unknown committed
2435 2436 2437
	| expr comp_op expr %prec EQ	{ $$= (*((*$2)(0)))($1,$3); }
	| expr comp_op all_or_any in_subselect %prec EQ
	{
2438
	  Item_allany_subselect *it=
unknown's avatar
unknown committed
2439
	    new Item_allany_subselect(YYTHD, $1, (*$2)($3), $4);
unknown's avatar
unknown committed
2440 2441 2442 2443 2444
	  if ($3)
	    $$ = new Item_func_not(it);	/* ALL */
	  else
	    $$ = it;			/* ANY/SOME */
	}
unknown's avatar
unknown committed
2445 2446 2447 2448 2449 2450
	| expr SHIFT_LEFT expr	{ $$= new Item_func_shift_left($1,$3); }
	| expr SHIFT_RIGHT expr { $$= new Item_func_shift_right($1,$3); }
	| expr '+' expr		{ $$= new Item_func_plus($1,$3); }
	| expr '-' expr		{ $$= new Item_func_minus($1,$3); }
	| expr '*' expr		{ $$= new Item_func_mul($1,$3); }
	| expr '/' expr		{ $$= new Item_func_div($1,$3); }
2451 2452
	| expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
	| expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
unknown's avatar
unknown committed
2453
	| expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
2454
        | expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
unknown's avatar
unknown committed
2455 2456
	| expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| expr '%' expr		{ $$= new Item_func_mod($1,$3); }
2457 2458 2459 2460
	| expr '+' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,0); }
	| expr '-' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,1); }
2461
	| expr COLLATE_SYM collation_name
2462
	  { $$= new Item_func_set_collation($1,$3); };
unknown's avatar
unknown committed
2463 2464 2465 2466 2467 2468 2469

/* expressions that begin with 'expr' that do NOT follow IN_SYM */
no_in_expr:
	no_in_expr BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_between($1,$3,$5); }
	| no_in_expr NOT BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
2470
	| no_in_expr OR_OR_CONCAT expr	{ $$= or_or_concat(YYTHD, $1,$3); }
unknown's avatar
unknown committed
2471
	| no_in_expr OR expr		{ $$= new Item_cond_or($1,$3); }
unknown's avatar
unknown committed
2472
        | no_in_expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
unknown's avatar
unknown committed
2473
	| no_in_expr AND expr		{ $$= new Item_cond_and($1,$3); }
unknown's avatar
unknown committed
2474
	| no_in_expr SOUNDS_SYM LIKE expr { $$= Item_bool_func2::eq_creator(new Item_func_soundex($1), new Item_func_soundex($4));}
unknown's avatar
unknown committed
2475 2476 2477 2478 2479 2480 2481
	| no_in_expr LIKE simple_expr opt_escape { $$= new Item_func_like($1,$3,$4); }
	| no_in_expr NOT LIKE simple_expr opt_escape { $$= new Item_func_not(new Item_func_like($1,$4,$5)); }
	| no_in_expr REGEXP expr { $$= new Item_func_regex($1,$3); }
	| no_in_expr NOT REGEXP expr { $$= new Item_func_not(new Item_func_regex($1,$4)); }
	| no_in_expr IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| no_in_expr IS NOT NULL_SYM { $$= new Item_func_isnotnull($1); }
	| no_in_expr EQUAL_SYM expr	{ $$= new Item_func_equal($1,$3); }
unknown's avatar
unknown committed
2482 2483 2484
	| no_in_expr comp_op expr %prec EQ	{ $$= (*((*$2)(0)))($1,$3); }
	| no_in_expr comp_op all_or_any in_subselect %prec EQ
	{
2485
	  Item_allany_subselect *it=
unknown's avatar
unknown committed
2486
	    new Item_allany_subselect(YYTHD, $1, (*$2)($3), $4);
unknown's avatar
unknown committed
2487 2488 2489 2490 2491
	  if ($3)
	    $$ = new Item_func_not(it);	/* ALL */
	  else
	    $$ = it;			/* ANY/SOME */
	}
unknown's avatar
unknown committed
2492 2493 2494 2495 2496 2497
	| no_in_expr SHIFT_LEFT expr  { $$= new Item_func_shift_left($1,$3); }
	| no_in_expr SHIFT_RIGHT expr { $$= new Item_func_shift_right($1,$3); }
	| no_in_expr '+' expr		{ $$= new Item_func_plus($1,$3); }
	| no_in_expr '-' expr		{ $$= new Item_func_minus($1,$3); }
	| no_in_expr '*' expr		{ $$= new Item_func_mul($1,$3); }
	| no_in_expr '/' expr		{ $$= new Item_func_div($1,$3); }
2498
	| no_in_expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
unknown's avatar
unknown committed
2499
	| no_in_expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
unknown's avatar
unknown committed
2500
        | no_in_expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
unknown's avatar
unknown committed
2501 2502
	| no_in_expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| no_in_expr '%' expr		{ $$= new Item_func_mod($1,$3); }
2503
	| no_in_expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
2504 2505 2506 2507
	| no_in_expr '+' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,0); }
	| no_in_expr '-' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,1); }
unknown's avatar
unknown committed
2508
	| simple_expr;
unknown's avatar
unknown committed
2509 2510 2511

/* expressions that begin with 'expr' that does NOT follow AND */
no_and_expr:
unknown's avatar
unknown committed
2512 2513
	  no_and_expr IN_SYM '(' expr_list ')'
	  { $$= new Item_func_in($1,*$4); }
unknown's avatar
unknown committed
2514 2515
	| no_and_expr NOT IN_SYM '(' expr_list ')'
	  { $$= new Item_func_not(new Item_func_in($1,*$5)); }
unknown's avatar
unknown committed
2516
        | no_and_expr IN_SYM in_subselect
unknown's avatar
unknown committed
2517
          { $$= new Item_in_subselect(YYTHD, $1, $3); }
unknown's avatar
unknown committed
2518
	| no_and_expr NOT IN_SYM in_subselect
2519
          {
unknown's avatar
unknown committed
2520
            $$= new Item_func_not(new Item_in_subselect(YYTHD, $1, $4));
unknown's avatar
unknown committed
2521
          }
unknown's avatar
unknown committed
2522 2523 2524 2525
	| no_and_expr BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_between($1,$3,$5); }
	| no_and_expr NOT BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
2526
	| no_and_expr OR_OR_CONCAT expr	{ $$= or_or_concat(YYTHD, $1,$3); }
unknown's avatar
unknown committed
2527
	| no_and_expr OR expr		{ $$= new Item_cond_or($1,$3); }
unknown's avatar
unknown committed
2528
        | no_and_expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
unknown's avatar
unknown committed
2529
	| no_and_expr SOUNDS_SYM LIKE expr { $$= Item_bool_func2::eq_creator(new Item_func_soundex($1), new Item_func_soundex($4));}
unknown's avatar
unknown committed
2530 2531 2532 2533 2534 2535 2536
	| no_and_expr LIKE simple_expr opt_escape { $$= new Item_func_like($1,$3,$4); }
	| no_and_expr NOT LIKE simple_expr opt_escape	{ $$= new Item_func_not(new Item_func_like($1,$4,$5)); }
	| no_and_expr REGEXP expr { $$= new Item_func_regex($1,$3); }
	| no_and_expr NOT REGEXP expr { $$= new Item_func_not(new Item_func_regex($1,$4)); }
	| no_and_expr IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| no_and_expr IS NOT NULL_SYM { $$= new Item_func_isnotnull($1); }
	| no_and_expr EQUAL_SYM expr	{ $$= new Item_func_equal($1,$3); }
unknown's avatar
unknown committed
2537 2538 2539
	| no_and_expr comp_op expr %prec EQ { $$= (*((*$2)(0)))($1,$3); }
	| no_and_expr comp_op all_or_any in_subselect %prec EQ
	{
2540
	  Item_allany_subselect *it=
unknown's avatar
unknown committed
2541
	    new Item_allany_subselect(YYTHD, $1, (*$2)($3), $4);
unknown's avatar
unknown committed
2542 2543 2544 2545 2546
	  if ($3)
	    $$ = new Item_func_not(it);	/* ALL */
	  else
	    $$ = it;			/* ANY/SOME */
	}
unknown's avatar
unknown committed
2547 2548 2549 2550 2551 2552
	| no_and_expr SHIFT_LEFT expr  { $$= new Item_func_shift_left($1,$3); }
	| no_and_expr SHIFT_RIGHT expr { $$= new Item_func_shift_right($1,$3); }
	| no_and_expr '+' expr		{ $$= new Item_func_plus($1,$3); }
	| no_and_expr '-' expr		{ $$= new Item_func_minus($1,$3); }
	| no_and_expr '*' expr		{ $$= new Item_func_mul($1,$3); }
	| no_and_expr '/' expr		{ $$= new Item_func_div($1,$3); }
2553
	| no_and_expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
unknown's avatar
unknown committed
2554
	| no_and_expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
unknown's avatar
unknown committed
2555
        | no_and_expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
unknown's avatar
unknown committed
2556 2557
	| no_and_expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| no_and_expr '%' expr		{ $$= new Item_func_mod($1,$3); }
2558
	| no_and_expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
2559 2560 2561 2562
	| no_and_expr '+' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,0); }
	| no_and_expr '-' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,1); }
unknown's avatar
unknown committed
2563
	| simple_expr;
unknown's avatar
unknown committed
2564

2565
interval_expr:
unknown's avatar
unknown committed
2566
         INTERVAL_SYM expr { $$=$2; }
2567 2568
        ;

unknown's avatar
unknown committed
2569 2570 2571
simple_expr:
	simple_ident
	| literal
unknown's avatar
unknown committed
2572
	| param_marker
unknown's avatar
unknown committed
2573
	| '@' ident_or_text SET_VAR expr
unknown's avatar
unknown committed
2574 2575
	  {
	    $$= new Item_func_set_user_var($2,$4);
2576
	    Lex->uncacheable();;
unknown's avatar
unknown committed
2577
	  }
2578
	| '@' ident_or_text
unknown's avatar
unknown committed
2579 2580
	  {
	    $$= new Item_func_get_user_var($2);
2581
	    Lex->uncacheable();;
unknown's avatar
unknown committed
2582
	  }
unknown's avatar
unknown committed
2583 2584 2585 2586
	| '@' '@' opt_var_ident_type ident_or_text
	  {
	    if (!($$= get_system_var((enum_var_type) $3, $4)))
	      YYABORT;
unknown's avatar
unknown committed
2587
	  }
unknown's avatar
unknown committed
2588 2589 2590 2591 2592 2593
	| sum_expr
	| '-' expr %prec NEG	{ $$= new Item_func_neg($2); }
	| '~' expr %prec NEG	{ $$= new Item_func_bit_neg($2); }
	| NOT expr %prec NEG	{ $$= new Item_func_not($2); }
	| '!' expr %prec NEG	{ $$= new Item_func_not($2); }
	| '(' expr ')'		{ $$= $2; }
2594 2595 2596 2597 2598
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
2599
	| ROW_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
2600
	  {
2601 2602
	    $5->push_front($3);
	    $$= new Item_row(*$5);
unknown's avatar
unknown committed
2603
	  }
unknown's avatar
unknown committed
2604
	| EXISTS exists_subselect { $$= $2; }
unknown's avatar
unknown committed
2605
	| singlerow_subselect   { $$= $1; }
unknown's avatar
unknown committed
2606
	| '{' ident expr '}'	{ $$= $3; }
unknown's avatar
unknown committed
2607
        | MATCH ident_list_arg AGAINST '(' expr ')'
2608
          { Select->add_ftfunc_to_list((Item_func_match *)
2609
                   ($$=new Item_func_match_nl(*$2,$5))); }
unknown's avatar
unknown committed
2610
        | MATCH ident_list_arg AGAINST '(' expr IN_SYM BOOLEAN_SYM MODE_SYM ')'
2611
          { Select->add_ftfunc_to_list((Item_func_match *)
2612
                   ($$=new Item_func_match_bool(*$2,$5))); }
unknown's avatar
unknown committed
2613
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
unknown's avatar
unknown committed
2614
	| BINARY expr %prec NEG { $$= new Item_func_set_collation($2,&my_charset_bin); }
unknown's avatar
unknown committed
2615
	| CAST_SYM '(' expr AS cast_type ')'  { $$= create_func_cast($3, $5); }
unknown's avatar
unknown committed
2616
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
2617
	  { $$= new Item_func_case(* $4, $2, $5 ); }
unknown's avatar
unknown committed
2618
	| CONVERT_SYM '(' expr ',' cast_type ')'  { $$= create_func_cast($3, $5); }
2619 2620
	| CONVERT_SYM '(' expr USING charset_name ')'
	  { $$= new Item_func_conv_charset($3,$5); }
2621
	| CONVERT_SYM '(' expr ',' expr ',' expr ')'
2622
	  { $$= new Item_func_conv_charset3($3,$7,$5); }
unknown's avatar
SCRUM  
unknown committed
2623 2624
	| DEFAULT '(' simple_ident ')'
	  { $$= new Item_default_value($3); }
unknown's avatar
unknown committed
2625 2626 2627 2628 2629 2630 2631 2632
	| FUNC_ARG0 '(' ')'
	  { $$= ((Item*(*)(void))($1.symbol->create_func))();}
	| FUNC_ARG1 '(' expr ')'
	  { $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);}
	| FUNC_ARG2 '(' expr ',' expr ')'
	  { $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);}
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
	  { $$= ((Item*(*)(Item*,Item*,Item*))($1.symbol->create_func))($3,$5,$7);}
2633 2634
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
unknown's avatar
unknown committed
2635 2636 2637 2638 2639 2640
	| ATAN	'(' expr ')'
	  { $$= new Item_func_atan($3); }
	| ATAN	'(' expr ',' expr ')'
	  { $$= new Item_func_atan($3,$5); }
	| CHAR_SYM '(' expr_list ')'
	  { $$= new Item_func_char(*$3); }
unknown's avatar
unknown committed
2641 2642
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
unknown's avatar
unknown committed
2643 2644 2645 2646 2647 2648 2649
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
	  { $$= new Item_func_concat_ws($3, *$5); }
	| CURDATE optional_braces
2650
	  { $$= new Item_func_curdate(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
2651
	| CURTIME optional_braces
2652
	  { $$= new Item_func_curtime(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
2653
	| CURTIME '(' expr ')'
2654 2655
	  {
	    $$= new Item_func_curtime($3);
2656
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
2657
	  }
2658 2659 2660 2661
	| DATE_ADD_INTERVAL '(' expr ',' interval_expr interval ')'
	  { $$= new Item_date_add_interval($3,$5,$6,0); }
	| DATE_SUB_INTERVAL '(' expr ',' interval_expr interval ')'
	  { $$= new Item_date_add_interval($3,$5,$6,1); }
unknown's avatar
unknown committed
2662
	| DATABASE '(' ')'
2663
	  {
unknown's avatar
unknown committed
2664
	    $$= new Item_func_database();
2665
            Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
2666
	  }
unknown's avatar
unknown committed
2667 2668 2669 2670
	| ELT_FUNC '(' expr ',' expr_list ')'
	  { $$= new Item_func_elt($3, *$5); }
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
unknown's avatar
unknown committed
2671 2672 2673
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
2674
	    Lex->uncacheable();;
unknown's avatar
unknown committed
2675
	  }
unknown's avatar
unknown committed
2676 2677 2678 2679 2680
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
	| DECODE_SYM '(' expr ',' TEXT_STRING ')'
	  { $$= new Item_func_decode($3,$5.str); }
	| ENCODE_SYM '(' expr ',' TEXT_STRING ')'
	 { $$= new Item_func_encode($3,$5.str); }
unknown's avatar
unknown committed
2681
	| DES_DECRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
2682
        { $$= new Item_func_des_decrypt($3); }
unknown's avatar
unknown committed
2683
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
2684
        { $$= new Item_func_des_decrypt($3,$5); }
unknown's avatar
unknown committed
2685
	| DES_ENCRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
2686
        { $$= new Item_func_des_encrypt($3); }
unknown's avatar
unknown committed
2687
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
2688
        { $$= new Item_func_des_encrypt($3,$5); }
unknown's avatar
unknown committed
2689 2690 2691 2692 2693 2694
	| EXPORT_SET '(' expr ',' expr ',' expr ')'
		{ $$= new Item_func_export_set($3, $5, $7); }
	| EXPORT_SET '(' expr ',' expr ',' expr ',' expr ')'
		{ $$= new Item_func_export_set($3, $5, $7, $9); }
	| EXPORT_SET '(' expr ',' expr ',' expr ',' expr ',' expr ')'
		{ $$= new Item_func_export_set($3, $5, $7, $9, $11); }
2695 2696
	| FALSE_SYM
	  { $$= new Item_int((char*) "FALSE",0,1); }
unknown's avatar
unknown committed
2697 2698 2699 2700 2701 2702
	| FORMAT_SYM '(' expr ',' NUM ')'
	  { $$= new Item_func_format($3,atoi($5.str)); }
	| FROM_UNIXTIME '(' expr ')'
	  { $$= new Item_func_from_unixtime($3); }
	| FROM_UNIXTIME '(' expr ',' expr ')'
	  {
unknown's avatar
unknown committed
2703
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
unknown's avatar
unknown committed
2704 2705 2706
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
	  { $$= new Item_func_field($3, *$5); }
unknown's avatar
unknown committed
2707
	| GEOMFROMTEXT '(' expr ')'
2708
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2709
	| GEOMFROMTEXT '(' expr ',' expr ')'
2710
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2711
	| GEOMETRYCOLLECTION '(' expr_list ')'
2712 2713
	  { $$= new Item_func_spatial_collection(* $3,
                       Geometry::wkbGeometryCollection,
unknown's avatar
unknown committed
2714
                       Geometry::wkbPoint); }
unknown's avatar
unknown committed
2715 2716 2717 2718 2719 2720
	| HOUR_SYM '(' expr ')'
	  { $$= new Item_func_hour($3); }
	| IF '(' expr ',' expr ',' expr ')'
	  { $$= new Item_func_if($3,$5,$7); }
	| INSERT '(' expr ',' expr ',' expr ',' expr ')'
	  { $$= new Item_func_insert($3,$5,$7,$9); }
2721
	| interval_expr interval '+' expr
unknown's avatar
unknown committed
2722
	  /* we cannot put interval before - */
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
              send_error(Lex->thd, ER_SYNTAX_ERROR);
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
unknown's avatar
unknown committed
2733 2734
	| LAST_INSERT_ID '(' ')'
	  {
2735
	    $$= get_system_var(OPT_SESSION, "last_insert_id", 14,
2736
			      "last_insert_id()");
2737
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
2738 2739 2740 2741
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
	    $$= new Item_func_set_last_insert_id($3);
2742
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
2743 2744 2745
	  }
	| LEFT '(' expr ',' expr ')'
	  { $$= new Item_func_left($3,$5); }
unknown's avatar
unknown committed
2746
	| LINESTRING '(' expr_list ')'
2747
	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
2748
               Geometry::wkbLineString, Geometry::wkbPoint); }
unknown's avatar
unknown committed
2749 2750 2751 2752
	| LOCATE '(' expr ',' expr ')'
	  { $$= new Item_func_locate($5,$3); }
	| LOCATE '(' expr ',' expr ',' expr ')'
	  { $$= new Item_func_locate($5,$3,$7); }
unknown's avatar
unknown committed
2753
 	| GEOMCOLLFROMTEXT '(' expr ')'
2754
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2755
	| GEOMCOLLFROMTEXT '(' expr ',' expr ')'
2756
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2757
	| GREATEST_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
2758 2759 2760
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
2761
	| LOG_SYM '(' expr ')'
unknown's avatar
unknown committed
2762
	  { $$= new Item_func_log($3); }
2763
	| LOG_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
2764
	  { $$= new Item_func_log($3, $5); }
unknown's avatar
unknown committed
2765
 	| LINEFROMTEXT '(' expr ')'
2766
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2767
	| LINEFROMTEXT '(' expr ',' expr ')'
2768
	  { $$= new Item_func_geometry_from_text($3); }
2769 2770 2771
	| MASTER_POS_WAIT '(' expr ',' expr ')'
	  { 
	    $$= new Item_master_pos_wait($3, $5);
unknown's avatar
unknown committed
2772 2773
	    Lex->safe_to_cache_query=0; 
		  }
2774 2775 2776
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
	  { 
	    $$= new Item_master_pos_wait($3, $5, $7);
unknown's avatar
unknown committed
2777
	    Lex->safe_to_cache_query=0; 
2778
	  }
unknown's avatar
unknown committed
2779 2780
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
2781 2782
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
unknown's avatar
unknown committed
2783 2784
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
unknown's avatar
unknown committed
2785
 	| MULTILINESTRING '(' expr_list ')'
2786
 	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
2787
                    Geometry::wkbMultiLineString, Geometry::wkbLineString); }
unknown's avatar
unknown committed
2788
 	| MLINEFROMTEXT '(' expr ')'
2789
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2790
	| MLINEFROMTEXT '(' expr ',' expr ')'
2791
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2792
	| MPOINTFROMTEXT '(' expr ')'
2793
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2794
	| MPOINTFROMTEXT '(' expr ',' expr ')'
2795
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2796
	| MPOLYFROMTEXT '(' expr ')'
2797
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2798
	| MPOLYFROMTEXT '(' expr ',' expr ')'
2799
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2800
	| MULTIPOINT '(' expr_list ')'
2801
	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
2802 2803
                    Geometry::wkbMultiPoint, Geometry::wkbPoint); }
 	| MULTIPOLYGON '(' expr_list ')'
2804
	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
2805
                       Geometry::wkbMultiPolygon, Geometry::wkbPolygon ); }
unknown's avatar
unknown committed
2806
	| NOW_SYM optional_braces
2807
	  { $$= new Item_func_now(); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
2808
	| NOW_SYM '(' expr ')'
2809
	  { $$= new Item_func_now($3); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
2810
	| PASSWORD '(' expr ')'
unknown's avatar
SCRUM  
unknown committed
2811 2812 2813
	  { $$= new Item_func_password($3); }
        | PASSWORD '(' expr ',' expr ')'
          { $$= new Item_func_password($3,$5); }
2814 2815
	| POINT '(' expr ',' expr ')'
	  { $$= new Item_func_point($3,$5); }
unknown's avatar
unknown committed
2816
 	| POINTFROMTEXT '(' expr ')'
2817
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2818
	| POINTFROMTEXT '(' expr ',' expr ')'
2819
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2820
	| POLYFROMTEXT '(' expr ')'
2821
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2822
	| POLYFROMTEXT '(' expr ',' expr ')'
2823
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2824
	| POLYGON '(' expr_list ')'
2825
	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
2826
			Geometry::wkbPolygon, Geometry::wkbLineString); }
unknown's avatar
unknown committed
2827 2828
	| POSITION_SYM '(' no_in_expr IN_SYM expr ')'
	  { $$ = new Item_func_locate($5,$3); }
unknown's avatar
unknown committed
2829
	| RAND '(' expr ')'
2830
	  { $$= new Item_func_rand($3); Lex->uncacheable();}
unknown's avatar
unknown committed
2831
	| RAND '(' ')'
2832
	  { $$= new Item_func_rand(); Lex->uncacheable();}
unknown's avatar
unknown committed
2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852
	| REPLACE '(' expr ',' expr ',' expr ')'
	  { $$= new Item_func_replace($3,$5,$7); }
	| RIGHT '(' expr ',' expr ')'
	  { $$= new Item_func_right($3,$5); }
	| ROUND '(' expr ')'
	  { $$= new Item_func_round($3, new Item_int((char*)"0",0,1),0); }
	| ROUND '(' expr ',' expr ')' { $$= new Item_func_round($3,$5,0); }
	| SECOND_SYM '(' expr ')'
	  { $$= new Item_func_second($3); }
	| SUBSTRING '(' expr ',' expr ',' expr ')'
	  { $$= new Item_func_substr($3,$5,$7); }
	| SUBSTRING '(' expr ',' expr ')'
	  { $$= new Item_func_substr($3,$5); }
	| SUBSTRING '(' expr FROM expr FOR_SYM expr ')'
	  { $$= new Item_func_substr($3,$5,$7); }
	| SUBSTRING '(' expr FROM expr ')'
	  { $$= new Item_func_substr($3,$5); }
	| SUBSTRING_INDEX '(' expr ',' expr ',' expr ')'
	  { $$= new Item_func_substr_index($3,$5,$7); }
	| TRIM '(' expr ')'
2853
	  { $$= new Item_func_trim($3,new Item_string(" ",1,default_charset_info)); }
unknown's avatar
unknown committed
2854 2855 2856 2857 2858 2859 2860 2861
	| TRIM '(' LEADING opt_pad FROM expr ')'
	  { $$= new Item_func_ltrim($6,$4); }
	| TRIM '(' TRAILING opt_pad FROM expr ')'
	  { $$= new Item_func_rtrim($6,$4); }
	| TRIM '(' BOTH opt_pad FROM expr ')'
	  { $$= new Item_func_trim($6,$4); }
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
2862 2863
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
2864 2865
	| TRUE_SYM
	  { $$= new Item_int((char*) "TRUE",1,1); }
unknown's avatar
unknown committed
2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908
	| UDA_CHAR_SUM '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_sum_udf_str($1, *$3);
	    else
	      $$ = new Item_sum_udf_str($1);
	  }
	| UDA_FLOAT_SUM '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_sum_udf_float($1, *$3);
	    else
	      $$ = new Item_sum_udf_float($1);
	  }
	| UDA_INT_SUM '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_sum_udf_int($1, *$3);
	    else
	      $$ = new Item_sum_udf_int($1);
	  }
	| UDF_CHAR_FUNC '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_func_udf_str($1, *$3);
	    else
	      $$ = new Item_func_udf_str($1);
	  }
	| UDF_FLOAT_FUNC '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_func_udf_float($1, *$3);
	    else
	      $$ = new Item_func_udf_float($1);
	  }
	| UDF_INT_FUNC '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_func_udf_int($1, *$3);
	    else
	      $$ = new Item_func_udf_int($1);
	  }
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
2909
	  {
unknown's avatar
unknown committed
2910 2911
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
unknown's avatar
unknown committed
2912
	| UNIX_TIMESTAMP '(' ')'
unknown's avatar
unknown committed
2913 2914
	  {
	    $$= new Item_func_unix_timestamp();
2915
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
2916
	  }
unknown's avatar
unknown committed
2917 2918 2919
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
2920
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931
	| WEEK_SYM '(' expr ')'
	  { $$= new Item_func_week($3,new Item_int((char*) "0",0,1)); }
	| WEEK_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_week($3,$5); }
	| YEAR_SYM '(' expr ')'
	  { $$= new Item_func_year($3); }
	| YEARWEEK '(' expr ')'
	  { $$= new Item_func_yearweek($3,new Item_int((char*) "0",0,1)); }
	| YEARWEEK '(' expr ',' expr ')'
	  { $$= new Item_func_yearweek($3, $5); }
	| BENCHMARK_SYM '(' ULONG_NUM ',' expr ')'
2932
	  {
unknown's avatar
unknown committed
2933
	    $$=new Item_func_benchmark($3,$5);
2934
	    Lex->uncacheable();
unknown's avatar
unknown committed
2935
	  }
unknown's avatar
unknown committed
2936
	| EXTRACT_SYM '(' interval FROM expr ')'
unknown's avatar
unknown committed
2937
	{ $$=new Item_extract( $3, $5); };
unknown's avatar
unknown committed
2938 2939 2940

udf_expr_list:
	/* empty */	{ $$= NULL; }
unknown's avatar
unknown committed
2941
	| expr_list	{ $$= $1;};
unknown's avatar
unknown committed
2942 2943 2944 2945 2946 2947 2948 2949

sum_expr:
	AVG_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_avg($3); }
	| BIT_AND  '(' in_sum_expr ')'
	  { $$=new Item_sum_and($3); }
	| BIT_OR  '(' in_sum_expr ')'
	  { $$=new Item_sum_or($3); }
unknown's avatar
unknown committed
2950
	| COUNT_SYM '(' opt_all '*' ')'
unknown's avatar
unknown committed
2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
	| COUNT_SYM '(' DISTINCT expr_list ')'
	  { $$=new Item_sum_count_distinct(* $4); }
	| GROUP_UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' in_sum_expr ')'
	  { $$= new Item_sum_unique_users($3,atoi($5.str),atoi($7.str),$9); }
	| MIN_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_min($3); }
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
	| STD_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_std($3); }
unknown's avatar
unknown committed
2964 2965
	| VARIANCE_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_variance($3); }
unknown's avatar
unknown committed
2966
	| SUM_SYM '(' in_sum_expr ')'
unknown's avatar
unknown committed
2967
	  { $$=new Item_sum_sum($3); };
unknown's avatar
unknown committed
2968 2969

in_sum_expr:
unknown's avatar
unknown committed
2970
	opt_all
2971 2972 2973 2974 2975
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
2976
	    YYABORT;
2977 2978
	  }
	}
unknown's avatar
unknown committed
2979 2980
	expr
	{
unknown's avatar
unknown committed
2981
	  Select->select_lex()->in_sum_expr--;
2982
	  $$= $3;
unknown's avatar
unknown committed
2983
	};
unknown's avatar
unknown committed
2984

unknown's avatar
unknown committed
2985
cast_type:
2986
	BINARY			{ $$=ITEM_CAST_BINARY; }
2987
	| CHAR_SYM		{ $$=ITEM_CAST_CHAR; }
unknown's avatar
unknown committed
2988 2989 2990 2991 2992 2993
	| SIGNED_SYM		{ $$=ITEM_CAST_SIGNED_INT; }
	| SIGNED_SYM INT_SYM	{ $$=ITEM_CAST_SIGNED_INT; }
	| UNSIGNED		{ $$=ITEM_CAST_UNSIGNED_INT; }
	| UNSIGNED INT_SYM	{ $$=ITEM_CAST_UNSIGNED_INT; }
	| DATE_SYM		{ $$=ITEM_CAST_DATE; }
	| TIME_SYM		{ $$=ITEM_CAST_TIME; }
2994 2995
	| DATETIME		{ $$=ITEM_CAST_DATETIME; }
	;
unknown's avatar
unknown committed
2996

unknown's avatar
unknown committed
2997
expr_list:
2998
	{ Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
2999
	expr_list2
unknown's avatar
unknown committed
3000
	{ $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
3001 3002

expr_list2:
3003
	expr { Select->expr_list.head()->push_back($1); }
unknown's avatar
unknown committed
3004
	| expr_list2 ',' expr { Select->expr_list.head()->push_back($3); };
unknown's avatar
unknown committed
3005

unknown's avatar
unknown committed
3006 3007
ident_list_arg:
          ident_list          { $$= $1; }
unknown's avatar
unknown committed
3008
        | '(' ident_list ')'  { $$= $2; };
unknown's avatar
unknown committed
3009

unknown's avatar
unknown committed
3010
ident_list:
3011
        { Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
3012
        ident_list2
unknown's avatar
unknown committed
3013
        { $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
3014 3015

ident_list2:
3016
        simple_ident { Select->expr_list.head()->push_back($1); }
unknown's avatar
unknown committed
3017
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
unknown's avatar
unknown committed
3018 3019 3020

opt_expr:
	/* empty */      { $$= NULL; }
unknown's avatar
unknown committed
3021
	| expr         	 { $$= $1; };
unknown's avatar
unknown committed
3022 3023 3024

opt_else:
	/* empty */    { $$= NULL; }
unknown's avatar
unknown committed
3025
	| ELSE expr    { $$= $2; };
unknown's avatar
unknown committed
3026 3027

when_list:
unknown's avatar
unknown committed
3028
        { Select->when_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
3029
	when_list2
unknown's avatar
unknown committed
3030
	{ $$= Select->when_list.pop(); };
unknown's avatar
unknown committed
3031 3032

when_list2:
unknown's avatar
unknown committed
3033
	expr THEN_SYM expr
unknown's avatar
unknown committed
3034
	  {
3035
	    SELECT_LEX_NODE *sel=Select;
unknown's avatar
unknown committed
3036 3037
	    sel->when_list.head()->push_back($1);
	    sel->when_list.head()->push_back($3);
unknown's avatar
unknown committed
3038
	}
unknown's avatar
unknown committed
3039
	| when_list2 WHEN_SYM expr THEN_SYM expr
unknown's avatar
unknown committed
3040
	  {
3041
	    SELECT_LEX_NODE *sel=Select;
unknown's avatar
unknown committed
3042 3043
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
unknown's avatar
unknown committed
3044
	  };
unknown's avatar
unknown committed
3045 3046

opt_pad:
3047
	/* empty */ { $$=new Item_string(" ",1,default_charset_info); }
unknown's avatar
unknown committed
3048
	| expr	    { $$=$1; };
unknown's avatar
unknown committed
3049 3050 3051 3052

join_table_list:
	'(' join_table_list ')'	{ $$=$2; }
	| join_table		{ $$=$1; }
unknown's avatar
unknown committed
3053
	| join_table_list ',' join_table_list { $$=$3; }
3054
	| join_table_list normal_join join_table_list { $$=$3; }
3055 3056
	| join_table_list STRAIGHT_JOIN join_table_list
	  { $$=$3 ; $$->straight=1; }
unknown's avatar
unknown committed
3057 3058 3059 3060
	| join_table_list normal_join join_table_list ON expr
	  { add_join_on($3,$5); $$=$3; }
	| join_table_list normal_join join_table_list
	  USING 
unknown's avatar
unknown committed
3061
	  {
unknown's avatar
unknown committed
3062
	    SELECT_LEX *sel= Select->select_lex();
3063
	    sel->db1=$1->db; sel->table1=$1->alias;
unknown's avatar
unknown committed
3064
	    sel->db2=$3->db; sel->table2=$3->alias;
unknown's avatar
unknown committed
3065
	  }
unknown's avatar
unknown committed
3066 3067 3068
	  '(' using_list ')'
	  { add_join_on($3,$7); $$=$3; }

3069
	| join_table_list LEFT opt_outer JOIN_SYM join_table_list ON expr
3070
	  { add_join_on($5,$7); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
3071
	| join_table_list LEFT opt_outer JOIN_SYM join_table_list
unknown's avatar
unknown committed
3072
	  {
unknown's avatar
unknown committed
3073
	    SELECT_LEX *sel= Select->select_lex();
3074 3075
	    sel->db1=$1->db; sel->table1=$1->alias;
	    sel->db2=$5->db; sel->table2=$5->alias;
unknown's avatar
unknown committed
3076
	  }
unknown's avatar
unknown committed
3077
	  USING '(' using_list ')'
3078
	  { add_join_on($5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
3079
	| join_table_list NATURAL LEFT opt_outer JOIN_SYM join_table_list
3080
	  { add_join_natural($1,$6); $6->outer_join|=JOIN_TYPE_LEFT; $$=$6; }
3081
	| join_table_list RIGHT opt_outer JOIN_SYM join_table_list ON expr
3082
	  { add_join_on($1,$7); $1->outer_join|=JOIN_TYPE_RIGHT; $$=$1; }
3083
	| join_table_list RIGHT opt_outer JOIN_SYM join_table_list
unknown's avatar
unknown committed
3084
	  {
unknown's avatar
unknown committed
3085
	    SELECT_LEX *sel= Select->select_lex();
3086 3087
	    sel->db1=$1->db; sel->table1=$1->alias;
	    sel->db2=$5->db; sel->table2=$5->alias;
unknown's avatar
unknown committed
3088
	  }
3089 3090
	  USING '(' using_list ')'
	  { add_join_on($1,$9); $1->outer_join|=JOIN_TYPE_RIGHT; $$=$1; }
3091
	| join_table_list NATURAL RIGHT opt_outer JOIN_SYM join_table_list
3092
	  { add_join_natural($6,$1); $1->outer_join|=JOIN_TYPE_RIGHT; $$=$1; }
3093
	| join_table_list NATURAL JOIN_SYM join_table_list
unknown's avatar
unknown committed
3094
	  { add_join_natural($1,$4); $$=$4; };
unknown's avatar
unknown committed
3095 3096

normal_join:
unknown's avatar
unknown committed
3097 3098 3099 3100
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
unknown's avatar
unknown committed
3101 3102

join_table:
unknown's avatar
unknown committed
3103
	{
unknown's avatar
unknown committed
3104
	  SELECT_LEX *sel= Select->select_lex();
unknown's avatar
unknown committed
3105
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
unknown's avatar
unknown committed
3106
	  sel->table_join_options= 0;
unknown's avatar
unknown committed
3107
	}
unknown's avatar
unknown committed
3108
        table_ident opt_table_alias opt_key_definition
unknown's avatar
unknown committed
3109
	{
unknown's avatar
unknown committed
3110 3111
	  LEX *lex= Lex;
	  SELECT_LEX_NODE *sel= lex->current_select;
unknown's avatar
unknown committed
3112
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
unknown's avatar
unknown committed
3113
					   sel->get_table_join_options(),
unknown's avatar
unknown committed
3114
					   lex->lock_option,
3115 3116
					   sel->get_use_index(),
					   sel->get_ignore_index())))
3117
	    YYABORT;
unknown's avatar
unknown committed
3118
	}
unknown's avatar
unknown committed
3119
	| '{' ident join_table LEFT OUTER JOIN_SYM join_table ON expr '}'
3120
	  { add_join_on($7,$9); $7->outer_join|=JOIN_TYPE_LEFT; $$=$7; }
3121
        | '(' SELECT_SYM select_derived ')' opt_table_alias
unknown's avatar
unknown committed
3122 3123
	{
	  LEX *lex=Lex;
3124 3125 3126
	  SELECT_LEX_UNIT *unit= lex->current_select->master_unit();
	  lex->current_select= unit->outer_select();
	  if (!($$= lex->current_select->
unknown's avatar
unknown committed
3127
                add_table_to_list(lex->thd, new Table_ident(unit), $5, 0,
3128
				  TL_READ,(List<String> *)0,
unknown's avatar
unknown committed
3129 3130
	                          (List<String> *)0)))

unknown's avatar
unknown committed
3131
	    YYABORT;
unknown's avatar
unknown committed
3132
	};
unknown's avatar
unknown committed
3133

3134
select_derived:
unknown's avatar
unknown committed
3135
        {
unknown's avatar
unknown committed
3136
	  LEX *lex= Lex;
unknown's avatar
unknown committed
3137
	  lex->derived_tables= 1;
3138 3139 3140 3141 3142 3143
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN && 
	       lex->sql_command <= (int)SQLCOM_HA_READ) || lex->sql_command == (int)SQLCOM_KILL)
	  {	
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
3144
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
unknown's avatar
unknown committed
3145
              mysql_new_select(lex, 1))
unknown's avatar
unknown committed
3146 3147
	    YYABORT;
	  mysql_init_select(lex);
3148
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
unknown's avatar
unknown committed
3149
	}
3150
        select_options select_item_list opt_select_from union_opt
3151
        ;
unknown's avatar
unknown committed
3152 3153 3154

opt_outer:
	/* empty */	{}
unknown's avatar
unknown committed
3155
	| OUTER		{};
unknown's avatar
unknown committed
3156 3157 3158 3159

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
unknown's avatar
unknown committed
3160
          {
unknown's avatar
unknown committed
3161
	    SELECT_LEX *sel= Select->select_lex();
unknown's avatar
unknown committed
3162 3163 3164
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
3165 3166 3167 3168 3169 3170 3171
	| FORCE_SYM key_usage_list
          {
	    SELECT_LEX *sel= Select->select_lex();
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
unknown's avatar
unknown committed
3172
	| IGNORE_SYM key_usage_list
unknown's avatar
unknown committed
3173
	  {
unknown's avatar
unknown committed
3174
	    SELECT_LEX *sel= Select->select_lex();
unknown's avatar
unknown committed
3175 3176
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
unknown's avatar
unknown committed
3177
	  };
unknown's avatar
unknown committed
3178 3179

key_usage_list:
unknown's avatar
unknown committed
3180
	key_or_index { Select->select_lex()->interval_list.empty(); }
unknown's avatar
unknown committed
3181 3182 3183 3184 3185 3186 3187 3188
        '(' key_list_or_empty ')'
        { $$= &Select->select_lex()->interval_list; }
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
unknown's avatar
unknown committed
3189 3190 3191

key_usage_list2:
	key_usage_list2 ',' ident
unknown's avatar
unknown committed
3192
        { Select->select_lex()->
3193 3194
	    interval_list.push_back(new String((const char*) $3.str, $3.length,
				    default_charset_info)); }
unknown's avatar
unknown committed
3195
	| ident
unknown's avatar
unknown committed
3196
        { Select->select_lex()->
3197 3198
	    interval_list.push_back(new String((const char*) $1.str, $1.length,
				    default_charset_info)); }
unknown's avatar
unknown committed
3199
	| PRIMARY_SYM
unknown's avatar
unknown committed
3200
        { Select->select_lex()->
3201 3202
	    interval_list.push_back(new String("PRIMARY", 7,
				    default_charset_info)); };
unknown's avatar
unknown committed
3203 3204 3205

using_list:
	ident
unknown's avatar
unknown committed
3206
	  {
unknown's avatar
unknown committed
3207
	    SELECT_LEX *sel= Select->select_lex();
3208 3209 3210 3211
	    if (!($$= new Item_func_eq(new Item_field(sel->db1, sel->table1,
						      $1.str),
				       new Item_field(sel->db2, sel->table2,
						      $1.str))))
unknown's avatar
unknown committed
3212 3213 3214 3215
	      YYABORT;
	  }
	| using_list ',' ident
	  {
unknown's avatar
unknown committed
3216
	    SELECT_LEX *sel= Select->select_lex();
unknown's avatar
unknown committed
3217
	    if (!($$= new Item_cond_and(new Item_func_eq(new Item_field(sel->db1,sel->table1,$3.str), new Item_field(sel->db2,sel->table2,$3.str)), $1)))
unknown's avatar
unknown committed
3218
	      YYABORT;
unknown's avatar
unknown committed
3219
	  };
unknown's avatar
unknown committed
3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233

interval:
	 DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
	| DAY_MINUTE_SYM	{ $$=INTERVAL_DAY_MINUTE; }
	| DAY_SECOND_SYM	{ $$=INTERVAL_DAY_SECOND; }
	| DAY_SYM		{ $$=INTERVAL_DAY; }
	| HOUR_MINUTE_SYM	{ $$=INTERVAL_HOUR_MINUTE; }
	| HOUR_SECOND_SYM	{ $$=INTERVAL_HOUR_SECOND; }
	| HOUR_SYM		{ $$=INTERVAL_HOUR; }
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
	| YEAR_MONTH_SYM	{ $$=INTERVAL_YEAR_MONTH; }
unknown's avatar
unknown committed
3234
	| YEAR_SYM		{ $$=INTERVAL_YEAR; };
unknown's avatar
unknown committed
3235 3236 3237 3238

table_alias:
	/* empty */
	| AS
unknown's avatar
unknown committed
3239
	| EQ;
unknown's avatar
unknown committed
3240 3241 3242 3243

opt_table_alias:
	/* empty */		{ $$=0; }
	| table_alias ident
unknown's avatar
unknown committed
3244
	  { $$= (LEX_STRING*) sql_memdup(&$2,sizeof(LEX_STRING)); };
unknown's avatar
unknown committed
3245

unknown's avatar
unknown committed
3246 3247 3248 3249
opt_all:
	/* empty */
	| ALL
	;
unknown's avatar
unknown committed
3250 3251

where_clause:
3252
	/* empty */  { Select->select_lex()->where= 0; }
3253 3254
	| WHERE expr
	  {
unknown's avatar
unknown committed
3255
	    Select->select_lex()->where= $2;
3256 3257 3258
	    if ($2)
	      $2->top_level_item();
	  }
unknown's avatar
unknown committed
3259
 	;
unknown's avatar
unknown committed
3260 3261 3262

having_clause:
	/* empty */
unknown's avatar
unknown committed
3263
	| HAVING { Select->select_lex()->create_refs= 1; } expr
unknown's avatar
unknown committed
3264 3265 3266 3267 3268 3269
	  {
	    SELECT_LEX *sel= Select->select_lex();
	    sel->having= $3; sel->create_refs=0;
	    if ($3)
	      $3->top_level_item();
	  }
3270
	;
unknown's avatar
unknown committed
3271 3272 3273

opt_escape:
	ESCAPE_SYM TEXT_STRING	{ $$= $2.str; }
unknown's avatar
unknown committed
3274
	| /* empty */		{ $$= (char*) "\\"; };
unknown's avatar
unknown committed
3275 3276 3277


/*
unknown's avatar
unknown committed
3278
   group by statement in select
unknown's avatar
unknown committed
3279 3280 3281 3282
*/

group_clause:
	/* empty */
3283
	| GROUP BY group_list olap_opt;
unknown's avatar
unknown committed
3284 3285

group_list:
unknown's avatar
unknown committed
3286
	group_list ',' order_ident order_dir
unknown's avatar
unknown committed
3287
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
3288
	| order_ident order_dir
unknown's avatar
unknown committed
3289
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
3290

3291 3292
olap_opt:
	/* empty */ {}
3293
	| WITH CUBE_SYM
3294
          {
3295
	    LEX *lex=Lex;
3296 3297 3298 3299 3300 3301
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
	      net_printf(lex->thd, ER_WRONG_USAGE, "WITH CUBE",
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
3302
	    lex->current_select->select_lex()->olap= CUBE_TYPE;
3303
	    net_printf(lex->thd, ER_NOT_SUPPORTED_YET, "CUBE");
3304
	    YYABORT;	/* To be deleted in 4.1 */
3305
	  }
3306
	| WITH ROLLUP_SYM
3307
          {
3308 3309 3310 3311 3312 3313 3314
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
	      net_printf(lex->thd, ER_WRONG_USAGE, "WITH ROLLUP",
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
3315
	    lex->current_select->select_lex()->olap= ROLLUP_TYPE;
3316
	    net_printf(lex->thd, ER_NOT_SUPPORTED_YET, "ROLLUP");
3317
	    YYABORT;	/* To be deleted in 4.1 */
3318
	  }
3319
	;
3320

unknown's avatar
unknown committed
3321
/*
unknown's avatar
unknown committed
3322
   Order by statement in select
unknown's avatar
unknown committed
3323 3324
*/

3325
opt_order_clause:
unknown's avatar
unknown committed
3326
	/* empty */
unknown's avatar
unknown committed
3327
	| order_clause;
3328 3329

order_clause:
3330 3331
	ORDER_SYM BY
        {
unknown's avatar
unknown committed
3332
	  LEX *lex=Lex;
3333
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
unknown's avatar
unknown committed
3334
	      lex->current_select->select_lex()->olap !=
3335
	      UNSPECIFIED_OLAP_TYPE)
3336
	  {
3337
	    net_printf(lex->thd, ER_WRONG_USAGE,
3338 3339 3340 3341
		       "CUBE/ROLLUP",
		       "ORDER BY");
	    YYABORT;
	  }
unknown's avatar
unknown committed
3342
	} order_list;
unknown's avatar
unknown committed
3343 3344 3345

order_list:
	order_list ',' order_ident order_dir
unknown's avatar
unknown committed
3346
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
3347
	| order_ident order_dir
unknown's avatar
unknown committed
3348
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
3349 3350 3351

order_dir:
	/* empty */ { $$ =  1; }
unknown's avatar
unknown committed
3352
	| ASC  { $$ =1; }
unknown's avatar
unknown committed
3353
	| DESC { $$ =0; };
unknown's avatar
unknown committed
3354 3355


3356 3357 3358 3359 3360 3361 3362
opt_limit_clause_init:
	/* empty */
	{
	  SELECT_LEX_NODE *sel= Select;
          sel->offset_limit= 0L;
          sel->select_limit= Lex->thd->variables.select_limit;
	}
3363 3364 3365
	| limit_clause {}
	;

3366 3367 3368 3369 3370
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

3371
limit_clause:
3372
	LIMIT
unknown's avatar
unknown committed
3373
	  {
3374 3375
	    LEX *lex= Lex;
	    if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
unknown's avatar
unknown committed
3376
	        lex->current_select->select_lex()->olap !=
3377
		UNSPECIFIED_OLAP_TYPE)
3378
	    {
3379
	      net_printf(lex->thd, ER_WRONG_USAGE, "CUBE/ROLLUP",
3380
		        "LIMIT");
3381
	      YYABORT;
3382
	    }
unknown's avatar
unknown committed
3383
	  }
unknown's avatar
unknown committed
3384
	  limit_options
3385
	  {}
unknown's avatar
unknown committed
3386
	;
unknown's avatar
unknown committed
3387 3388 3389

limit_options:
	ULONG_NUM
unknown's avatar
unknown committed
3390
	  {
unknown's avatar
unknown committed
3391
            SELECT_LEX_NODE *sel= Select;
unknown's avatar
unknown committed
3392 3393 3394 3395 3396
            sel->select_limit= $1;
            sel->offset_limit= 0L;
	  }
	| ULONG_NUM ',' ULONG_NUM
	  {
3397
	    SELECT_LEX_NODE *sel= Select;
unknown's avatar
unknown committed
3398 3399
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
unknown's avatar
unknown committed
3400
	  }
unknown's avatar
unknown committed
3401
	| ULONG_NUM OFFSET_SYM ULONG_NUM
unknown's avatar
unknown committed
3402
	  {
unknown's avatar
unknown committed
3403
	    SELECT_LEX_NODE *sel= Select;
unknown's avatar
unknown committed
3404 3405 3406 3407
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
	  }
	;
unknown's avatar
unknown committed
3408 3409 3410 3411 3412


delete_limit_clause:
	/* empty */
	{
unknown's avatar
unknown committed
3413
	  LEX *lex=Lex;
3414
	  lex->current_select->select_limit= HA_POS_ERROR;
unknown's avatar
unknown committed
3415
	}
unknown's avatar
unknown committed
3416
	| LIMIT ulonglong_num
unknown's avatar
unknown committed
3417
	{ Select->select_limit= (ha_rows) $2; };
unknown's avatar
unknown committed
3418 3419

ULONG_NUM:
unknown's avatar
unknown committed
3420
	NUM	    { $$= strtoul($1.str,NULL,10); }
3421
	| LONG_NUM  { $$= (ulonglong) strtoll($1.str,NULL,10); }
unknown's avatar
unknown committed
3422
	| ULONGLONG_NUM { $$= (ulong) strtoull($1.str,NULL,10); }
unknown's avatar
unknown committed
3423
	| REAL_NUM  { $$= strtoul($1.str,NULL,10); }
unknown's avatar
unknown committed
3424
	| FLOAT_NUM { $$= strtoul($1.str,NULL,10); };
unknown's avatar
unknown committed
3425

unknown's avatar
unknown committed
3426
ulonglong_num:
unknown's avatar
unknown committed
3427
	NUM	    { $$= (ulonglong) strtoul($1.str,NULL,10); }
unknown's avatar
unknown committed
3428
	| ULONGLONG_NUM { $$= strtoull($1.str,NULL,10); }
3429
	| LONG_NUM  { $$= (ulonglong) strtoll($1.str,NULL,10); }
unknown's avatar
unknown committed
3430
	| REAL_NUM  { $$= strtoull($1.str,NULL,10); }
unknown's avatar
unknown committed
3431
	| FLOAT_NUM { $$= strtoull($1.str,NULL,10); };
unknown's avatar
unknown committed
3432 3433 3434 3435 3436 3437

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
3438 3439 3440 3441 3442 3443 3444
	    if (&lex->select_lex != lex->current_select)
	    {
	      net_printf(lex->thd, ER_WRONG_USAGE,
			  "PROCEDURE",
			  "subquery");
	      YYABORT;
	    }
unknown's avatar
unknown committed
3445 3446 3447
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
unknown's avatar
unknown committed
3448
	    if (add_proc_to_list(lex->thd, new Item_field(NULL,NULL,$2.str)))
unknown's avatar
unknown committed
3449
	      YYABORT;
3450
	    Lex->uncacheable();
unknown's avatar
unknown committed
3451
	  }
unknown's avatar
unknown committed
3452
	  '(' procedure_list ')';
unknown's avatar
unknown committed
3453 3454 3455 3456


procedure_list:
	/* empty */ {}
unknown's avatar
unknown committed
3457
	| procedure_list2 {};
unknown's avatar
unknown committed
3458 3459 3460

procedure_list2:
	procedure_list2 ',' procedure_item
unknown's avatar
unknown committed
3461
	| procedure_item;
unknown's avatar
unknown committed
3462 3463 3464 3465

procedure_item:
	  remember_name expr
	  {
unknown's avatar
unknown committed
3466 3467
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
unknown's avatar
unknown committed
3468 3469
	      YYABORT;
	    if (!$2->name)
unknown's avatar
unknown committed
3470
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1));
3471 3472 3473 3474 3475 3476
	  }
          ;


select_var_list_init:
	   {
unknown's avatar
unknown committed
3477 3478
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
3479 3480
	        YYABORT;
	   }
3481
	   select_var_list
3482
	   {}
3483
           ;
unknown's avatar
unknown committed
3484

unknown's avatar
unknown committed
3485
select_var_list:
3486 3487 3488 3489
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

unknown's avatar
unknown committed
3490 3491
select_var_ident:  
	   '@' ident_or_text
unknown's avatar
unknown committed
3492
           {
3493
             LEX *lex=Lex;
unknown's avatar
unknown committed
3494 3495 3496 3497 3498 3499
	     if (lex->result) 
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($2,0,0));
	     else
	       YYABORT;
	   }
           | ident_or_text
unknown's avatar
unknown committed
3500
           {
3501
             LEX *lex=Lex;
unknown's avatar
unknown committed
3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512
	     if (!lex->spcont)
	       YYABORT;
	     sp_pvar_t *t;
	     if (!(t=lex->spcont->find_pvar(&$1)))
	     {
	       send_error(lex->thd, ER_SYNTAX_ERROR);
	       YYABORT;
	     }
	     if (lex->result)
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($1,1,t->offset));
	     else
unknown's avatar
unknown committed
3513 3514
	       YYABORT;
	   }
unknown's avatar
unknown committed
3515
           ;
unknown's avatar
unknown committed
3516

3517
into:
unknown's avatar
unknown committed
3518
        INTO OUTFILE TEXT_STRING
unknown's avatar
unknown committed
3519
	{
3520
	  LEX *lex=Lex;
unknown's avatar
unknown committed
3521 3522 3523 3524 3525 3526 3527
	  if (!lex->describe)
	  {
	    if (!(lex->exchange= new sql_exchange($3.str,0)))
	      YYABORT;
	    if (!(lex->result= new select_export(lex->exchange)))
	      YYABORT;
	  }
unknown's avatar
unknown committed
3528 3529 3530 3531
	}
	opt_field_term opt_line_term
	| INTO DUMPFILE TEXT_STRING
	{
3532
	  LEX *lex=Lex;
unknown's avatar
unknown committed
3533 3534 3535 3536 3537 3538 3539
	  if (!lex->describe)
	  {
	    if (!(lex->exchange= new sql_exchange($3.str,1)))
	      YYABORT;
	    if (!(lex->result= new select_dump(lex->exchange)))
	      YYABORT;
	  }
unknown's avatar
unknown committed
3540
	}
3541
        | INTO select_var_list_init
unknown's avatar
unknown committed
3542
	{
3543
	  Lex->uncacheable();
3544 3545
	}
        ;
unknown's avatar
unknown committed
3546

unknown's avatar
unknown committed
3547 3548 3549
/*
  DO statement
*/
unknown's avatar
unknown committed
3550

3551
do:	DO_SYM
unknown's avatar
unknown committed
3552 3553 3554 3555 3556 3557
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
	  if (!(lex->insert_list = new List_item))
	    YYABORT;
	}
3558 3559 3560 3561
	values
	{}
	;

unknown's avatar
unknown committed
3562
/*
unknown's avatar
unknown committed
3563
  Drop : delete tables or index
unknown's avatar
unknown committed
3564 3565 3566
*/

drop:
3567
	DROP opt_temporary TABLE_SYM if_exists table_list opt_restrict
unknown's avatar
unknown committed
3568
	{
unknown's avatar
unknown committed
3569 3570
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
3571 3572
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
unknown's avatar
unknown committed
3573 3574 3575
	}
	| DROP INDEX ident ON table_ident {}
	  {
unknown's avatar
unknown committed
3576 3577 3578 3579
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
	     lex->drop_list.empty();
	     lex->drop_list.push_back(new Alter_drop(Alter_drop::KEY,
unknown's avatar
unknown committed
3580
						     $3.str));
unknown's avatar
unknown committed
3581 3582
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
unknown's avatar
unknown committed
3583 3584 3585 3586
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
unknown's avatar
unknown committed
3587 3588 3589 3590
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
	    lex->name=$4.str;
unknown's avatar
unknown committed
3591
	 }
3592
	| DROP FUNCTION_SYM IDENT opt_restrict
unknown's avatar
unknown committed
3593
	  {
unknown's avatar
unknown committed
3594 3595
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
3596
	    lex->udf.name= $3;
3597 3598 3599 3600 3601
	  }
	| DROP PROCEDURE ident opt_restrict
	  {
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
3602
	    lex->udf.name= $3;
3603 3604
	  }
	;
unknown's avatar
unknown committed
3605 3606

table_list:
3607
	table_name
unknown's avatar
unknown committed
3608
	| table_list ',' table_name;
unknown's avatar
unknown committed
3609

3610
table_name:
unknown's avatar
unknown committed
3611
	table_ident
unknown's avatar
unknown committed
3612 3613 3614 3615 3616
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
unknown's avatar
unknown committed
3617 3618

if_exists:
3619
	/* empty */ { $$= 0; }
3620 3621
	| IF EXISTS { $$= 1; }
	;
unknown's avatar
unknown committed
3622

3623 3624 3625 3626
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
unknown's avatar
unknown committed
3627 3628 3629 3630 3631
/*
** Insert : add new data to table
*/

insert:
3632 3633 3634 3635 3636 3637 3638
	INSERT
	{
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_INSERT;
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
	} insert_lock_option
3639
	opt_ignore insert2
unknown's avatar
unknown committed
3640
	{
unknown's avatar
unknown committed
3641
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
3642
	}
3643
	insert_field_spec opt_insert_update
3644
	{}
unknown's avatar
unknown committed
3645
	;
unknown's avatar
unknown committed
3646 3647

replace:
3648 3649
	REPLACE
	{
3650
	  LEX *lex=Lex;
3651 3652 3653
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
	}
unknown's avatar
unknown committed
3654 3655
	replace_lock_option insert2
	{
unknown's avatar
unknown committed
3656
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
3657 3658
	}
	insert_field_spec
3659
	{}
unknown's avatar
unknown committed
3660
	;
unknown's avatar
unknown committed
3661 3662

insert_lock_option:
unknown's avatar
unknown committed
3663 3664 3665 3666
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
3667
	;
unknown's avatar
unknown committed
3668 3669

replace_lock_option:
unknown's avatar
unknown committed
3670 3671
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
unknown's avatar
unknown committed
3672 3673 3674

insert2:
	INTO insert_table {}
unknown's avatar
unknown committed
3675
	| insert_table {};
unknown's avatar
unknown committed
3676 3677

insert_table:
3678
	table_name
unknown's avatar
unknown committed
3679
	{
unknown's avatar
unknown committed
3680 3681 3682 3683
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
unknown's avatar
unknown committed
3684
	};
unknown's avatar
unknown committed
3685 3686 3687 3688 3689

insert_field_spec:
	opt_field_spec insert_values {}
	| SET
	  {
unknown's avatar
unknown committed
3690 3691 3692
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
3693 3694
	      YYABORT;
	   }
unknown's avatar
unknown committed
3695
	   ident_eq_list;
unknown's avatar
unknown committed
3696 3697 3698 3699

opt_field_spec:
	/* empty */	  { }
	| '(' fields ')'  { }
unknown's avatar
unknown committed
3700
	| '(' ')'	  { };
unknown's avatar
unknown committed
3701 3702 3703

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
unknown's avatar
unknown committed
3704
	| insert_ident		{ Lex->field_list.push_back($1); };
unknown's avatar
unknown committed
3705 3706 3707

insert_values:
	VALUES	values_list  {}
unknown's avatar
unknown committed
3708
	| VALUE_SYM values_list  {}
unknown's avatar
unknown committed
3709 3710 3711 3712 3713
	| SELECT_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command = (lex->sql_command == SQLCOM_INSERT ?
				SQLCOM_INSERT_SELECT : SQLCOM_REPLACE_SELECT);
unknown's avatar
unknown committed
3714
	    lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
3715
	    mysql_init_select(lex);
unknown's avatar
unknown committed
3716
	  }
unknown's avatar
unknown committed
3717
	  select_options select_item_list opt_select_from select_lock_type
3718
          union_clause {}
unknown's avatar
unknown committed
3719
	;
unknown's avatar
unknown committed
3720 3721 3722

values_list:
	values_list ','  no_braces
unknown's avatar
unknown committed
3723
	| no_braces;
unknown's avatar
unknown committed
3724 3725 3726 3727

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
unknown's avatar
unknown committed
3728
	ident_eq_value;
unknown's avatar
unknown committed
3729 3730

ident_eq_value:
unknown's avatar
unknown committed
3731
	simple_ident equal expr_or_default
unknown's avatar
unknown committed
3732
	 {
unknown's avatar
unknown committed
3733 3734 3735
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
unknown's avatar
unknown committed
3736
	    YYABORT;
unknown's avatar
unknown committed
3737
	 };
unknown's avatar
unknown committed
3738 3739

equal:	EQ		{}
unknown's avatar
unknown committed
3740 3741 3742 3743 3744 3745 3746
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
unknown's avatar
unknown committed
3747 3748 3749 3750 3751 3752 3753 3754 3755

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
unknown's avatar
unknown committed
3756 3757
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
3758
	    YYABORT;
unknown's avatar
unknown committed
3759
	 };
unknown's avatar
unknown committed
3760 3761 3762

opt_values:
	/* empty */ {}
unknown's avatar
unknown committed
3763
	| values;
unknown's avatar
unknown committed
3764 3765

values:
unknown's avatar
unknown committed
3766
	values ','  expr_or_default
unknown's avatar
unknown committed
3767 3768 3769 3770
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
unknown's avatar
unknown committed
3771 3772 3773 3774 3775 3776 3777 3778
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
3779
	expr	  { $$= $1;}
unknown's avatar
SCRUM  
unknown committed
3780
	| DEFAULT {$$= new Item_default_value(); }
unknown's avatar
unknown committed
3781
	;
unknown's avatar
unknown committed
3782

3783 3784
opt_insert_update:
        /* empty */
unknown's avatar
unknown committed
3785
        | ON DUPLICATE_SYM
3786 3787 3788 3789 3790 3791 3792 3793 3794
          { /* for simplisity, let's forget about
               INSERT ... SELECT ... UPDATE
               for a moment */
	    if (Lex->sql_command != SQLCOM_INSERT)
            {
	      send_error(Lex->thd, ER_SYNTAX_ERROR);
              YYABORT;
            }
          }
3795
          KEY_SYM UPDATE_SYM update_list
3796 3797
        ;

unknown's avatar
unknown committed
3798 3799 3800
/* Update rows in a table */

update:
3801 3802
	UPDATE_SYM
	{
3803 3804
	  LEX *lex= Lex;
          lex->sql_command= SQLCOM_UPDATE;
3805
          lex->select_lex.init_order();
3806
        }
unknown's avatar
unknown committed
3807 3808 3809
        opt_low_priority opt_ignore join_table_list
	SET update_list where_clause opt_order_clause delete_limit_clause
	{
3810
	  LEX *lex= Lex;
unknown's avatar
unknown committed
3811
	  Select->set_lock_for_tables($3);
3812
          if (lex->select_lex.table_list.elements > 1)
3813
            lex->sql_command=SQLCOM_UPDATE_MULTI;
unknown's avatar
unknown committed
3814 3815
	}
	;
unknown's avatar
unknown committed
3816 3817

update_list:
unknown's avatar
SCRUM  
unknown committed
3818
	update_list ',' simple_ident equal expr_or_default
unknown's avatar
unknown committed
3819
	{
unknown's avatar
unknown committed
3820
	  if (add_item_to_list(YYTHD, $3) || add_value_to_list(YYTHD, $5))
unknown's avatar
unknown committed
3821 3822
	    YYABORT;
	}
unknown's avatar
SCRUM  
unknown committed
3823
	| simple_ident equal expr_or_default
unknown's avatar
unknown committed
3824
	  {
unknown's avatar
unknown committed
3825
	    if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
unknown's avatar
unknown committed
3826
	      YYABORT;
unknown's avatar
unknown committed
3827
	  };
unknown's avatar
unknown committed
3828 3829

opt_low_priority:
3830
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
3831
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
3832 3833 3834 3835

/* Delete rows from a table */

delete:
3836
	DELETE_SYM
3837
	{
3838 3839 3840
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
	  lex->select_lex.options= 0;
unknown's avatar
unknown committed
3841
	  lex->lock_option= lex->thd->update_lock_default;
3842
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
3843
	}
unknown's avatar
unknown committed
3844 3845
	opt_delete_options single_multi {}
	;
3846 3847

single_multi:
unknown's avatar
unknown committed
3848 3849
 	FROM table_ident
	{
unknown's avatar
unknown committed
3850 3851
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
unknown's avatar
unknown committed
3852 3853 3854
	    YYABORT;
	}
	where_clause opt_order_clause
3855
	delete_limit_clause {}
unknown's avatar
unknown committed
3856
	| table_wild_list
unknown's avatar
unknown committed
3857
	  { mysql_init_multi_delete(Lex); }
unknown's avatar
unknown committed
3858
          FROM join_table_list where_clause
unknown's avatar
unknown committed
3859 3860
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
3861 3862 3863
	  USING join_table_list where_clause
	  {}
	;
unknown's avatar
unknown committed
3864 3865 3866

table_wild_list:
	  table_wild_one {}
unknown's avatar
unknown committed
3867
	  | table_wild_list ',' table_wild_one {};
unknown's avatar
unknown committed
3868 3869

table_wild_one:
unknown's avatar
unknown committed
3870
	ident opt_wild opt_table_alias
unknown's avatar
unknown committed
3871
	{
unknown's avatar
unknown committed
3872 3873
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
					 TL_OPTION_UPDATING, Lex->lock_option))
unknown's avatar
unknown committed
3874 3875
	    YYABORT;
        }
unknown's avatar
unknown committed
3876
	| ident '.' ident opt_wild opt_table_alias
unknown's avatar
unknown committed
3877
	  {
unknown's avatar
unknown committed
3878
	    if (!Select->add_table_to_list(YYTHD, new Table_ident($1, $3, 0),
unknown's avatar
unknown committed
3879 3880
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
unknown's avatar
unknown committed
3881
	      YYABORT;
unknown's avatar
unknown committed
3882
	  }
unknown's avatar
unknown committed
3883
	;
unknown's avatar
unknown committed
3884 3885

opt_wild:
3886
	/* empty */	{}
unknown's avatar
unknown committed
3887
	| '.' '*'	{};
unknown's avatar
unknown committed
3888 3889


3890
opt_delete_options:
unknown's avatar
unknown committed
3891
	/* empty */	{}
unknown's avatar
unknown committed
3892
	| opt_delete_option opt_delete_options {};
3893 3894

opt_delete_option:
3895
	QUICK		{ Select->options|= OPTION_QUICK; }
unknown's avatar
unknown committed
3896
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; };
3897

3898
truncate:
unknown's avatar
unknown committed
3899
	TRUNCATE_SYM opt_table_sym table_name
3900
	{
3901
	  LEX* lex= Lex;
3902
	  lex->sql_command= SQLCOM_TRUNCATE;
3903
	  lex->select_lex.options= 0;
3904
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
3905 3906
	}
	;
3907

unknown's avatar
unknown committed
3908 3909
opt_table_sym:
	/* empty */
unknown's avatar
unknown committed
3910
	| TABLE_SYM;
3911

unknown's avatar
unknown committed
3912 3913
/* Show things */

3914 3915
show:	SHOW
	{
unknown's avatar
unknown committed
3916 3917 3918 3919
	  LEX *lex=Lex;
	  lex->wild=0;
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
unknown's avatar
unknown committed
3920
	show_param
3921 3922
	{}
	;
unknown's avatar
unknown committed
3923 3924 3925 3926 3927

show_param:
	DATABASES wild
	  { Lex->sql_command= SQLCOM_SHOW_DATABASES; }
	| TABLES opt_db wild
unknown's avatar
unknown committed
3928
	  {
3929
	    LEX *lex= Lex;
unknown's avatar
unknown committed
3930
	    lex->sql_command= SQLCOM_SHOW_TABLES;
3931 3932
	    lex->select_lex.db= $2;
	    lex->select_lex.options= 0;
unknown's avatar
unknown committed
3933
	   }
unknown's avatar
unknown committed
3934
	| TABLE_SYM STATUS_SYM opt_db wild
unknown's avatar
unknown committed
3935
	  {
3936
	    LEX *lex= Lex;
unknown's avatar
unknown committed
3937
	    lex->sql_command= SQLCOM_SHOW_TABLES;
3938 3939
	    lex->select_lex.options|= SELECT_DESCRIBE;
	    lex->select_lex.db= $3;
unknown's avatar
unknown committed
3940
	  }
3941
	| OPEN_SYM TABLES opt_db wild
unknown's avatar
unknown committed
3942
	  {
3943
	    LEX *lex= Lex;
unknown's avatar
unknown committed
3944
	    lex->sql_command= SQLCOM_SHOW_OPEN_TABLES;
3945 3946
	    lex->select_lex.db= $3;
	    lex->select_lex.options= 0;
3947
	  }
unknown's avatar
unknown committed
3948
	| opt_full COLUMNS from_or_in table_ident opt_db wild
unknown's avatar
unknown committed
3949 3950
	  {
	    Lex->sql_command= SQLCOM_SHOW_FIELDS;
unknown's avatar
unknown committed
3951 3952
	    if ($5)
	      $4->change_db($5);
unknown's avatar
unknown committed
3953
	    if (!Select->add_table_to_list(YYTHD, $4, NULL, 0))
unknown's avatar
unknown committed
3954 3955
	      YYABORT;
	  }
3956
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
3957 3958
	  TEXT_STRING AND MASTER_LOG_POS_SYM EQ ulonglong_num
	  AND MASTER_SERVER_ID_SYM EQ
unknown's avatar
unknown committed
3959 3960 3961 3962 3963
	ULONG_NUM
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
3964
	    Lex->mi.server_id = $16;
unknown's avatar
unknown committed
3965
          }
3966
        | BINARY LOGS_SYM
unknown's avatar
unknown committed
3967 3968
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
3969 3970 3971 3972 3973
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
unknown's avatar
unknown committed
3974
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
unknown's avatar
unknown committed
3975
          {
3976 3977
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
3978
          } opt_limit_clause_init
unknown's avatar
unknown committed
3979 3980 3981 3982 3983
	| keys_or_index FROM table_ident opt_db
	  {
	    Lex->sql_command= SQLCOM_SHOW_KEYS;
	    if ($4)
	      $3->change_db($4);
unknown's avatar
unknown committed
3984
	    if (!Select->add_table_to_list(YYTHD, $3, NULL, 0))
unknown's avatar
unknown committed
3985 3986
	      YYABORT;
	  }
unknown's avatar
unknown committed
3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001
	| COLUMN_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_COLUMN_TYPES;
	  }
	| TABLE_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_TABLE_TYPES;
	  }
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
4002
        | COUNT_SYM '(' '*' ')' WARNINGS
4003
          { (void) create_select_for_variable("warning_count"); }
4004
        | COUNT_SYM '(' '*' ')' ERRORS
4005
	  { (void) create_select_for_variable("error_count"); }
4006
        | WARNINGS opt_limit_clause_init
unknown's avatar
unknown committed
4007
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
4008
        | ERRORS opt_limit_clause_init
4009
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
unknown's avatar
unknown committed
4010 4011
	| STATUS_SYM wild
	  { Lex->sql_command= SQLCOM_SHOW_STATUS; }
unknown's avatar
unknown committed
4012 4013
        | INNOBASE_SYM STATUS_SYM
          { Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;}
unknown's avatar
unknown committed
4014 4015
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
4016
	| opt_var_type VARIABLES wild
unknown's avatar
unknown committed
4017
	  {
4018
	    THD *thd= YYTHD;
4019
	    thd->lex.sql_command= SQLCOM_SHOW_VARIABLES;
unknown's avatar
unknown committed
4020
	    thd->lex.option_type= (enum_var_type) $1;
4021
	  }
4022 4023
	| CHAR_SYM SET wild
	  { Lex->sql_command= SQLCOM_SHOW_CHARSETS; }
unknown's avatar
unknown committed
4024 4025
	| LOGS_SYM
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; }
unknown's avatar
unknown committed
4026
	| GRANTS FOR_SYM user
unknown's avatar
unknown committed
4027 4028 4029 4030 4031 4032
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
	    lex->grant_user->password.str=NullS;
	  }
unknown's avatar
unknown committed
4033
	| CREATE DATABASE opt_if_not_exists ident
unknown's avatar
unknown committed
4034 4035
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
unknown's avatar
unknown committed
4036 4037
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
unknown's avatar
unknown committed
4038
	  }
unknown's avatar
unknown committed
4039 4040 4041
        | CREATE TABLE_SYM table_ident
          {
	    Lex->sql_command = SQLCOM_SHOW_CREATE;
unknown's avatar
unknown committed
4042
	    if (!Select->add_table_to_list(YYTHD, $3, NULL,0))
unknown's avatar
unknown committed
4043 4044 4045 4046 4047
	      YYABORT;
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
unknown's avatar
unknown committed
4048
          }
unknown's avatar
unknown committed
4049 4050 4051
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
unknown's avatar
unknown committed
4052
          };
unknown's avatar
unknown committed
4053 4054 4055

opt_db:
	/* empty */  { $$= 0; }
unknown's avatar
unknown committed
4056
	| from_or_in ident { $$= $2.str; };
unknown's avatar
unknown committed
4057 4058 4059

wild:
	/* empty */
unknown's avatar
unknown committed
4060
	| LIKE text_string { Lex->wild= $2; };
unknown's avatar
unknown committed
4061

unknown's avatar
unknown committed
4062 4063
opt_full:
	/* empty */ { Lex->verbose=0; }
unknown's avatar
unknown committed
4064
	| FULL	    { Lex->verbose=1; };
unknown's avatar
unknown committed
4065

unknown's avatar
unknown committed
4066 4067
from_or_in:
	FROM
unknown's avatar
unknown committed
4068
	| IN_SYM;
unknown's avatar
unknown committed
4069

unknown's avatar
unknown committed
4070 4071
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
unknown's avatar
unknown committed
4072
        | IN_SYM TEXT_STRING { Lex->mi.log_file_name = $2.str; };
unknown's avatar
unknown committed
4073 4074 4075

binlog_from:
	/* empty */ { Lex->mi.pos = 4; /* skip magic number */ }
unknown's avatar
unknown committed
4076
        | FROM ulonglong_num { Lex->mi.pos = $2; };
unknown's avatar
unknown committed
4077 4078


unknown's avatar
unknown committed
4079 4080 4081 4082
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
unknown's avatar
unknown committed
4083 4084 4085 4086
	  LEX *lex=Lex;
	  lex->wild=0;
	  lex->verbose=0;
	  lex->sql_command=SQLCOM_SHOW_FIELDS;
unknown's avatar
unknown committed
4087
	  if (!Select->add_table_to_list(lex->thd, $2, NULL,0))
unknown's avatar
unknown committed
4088 4089
	    YYABORT;
	}
4090
	opt_describe_column {}
unknown's avatar
unknown committed
4091
	| describe_command { Lex->describe=1; } select
4092
          {
unknown's avatar
unknown committed
4093
	    LEX *lex=Lex;
4094
	    lex->select_lex.options|= SELECT_DESCRIBE;
unknown's avatar
unknown committed
4095 4096
	  }
	;
unknown's avatar
unknown committed
4097 4098 4099

describe_command:
	DESC
unknown's avatar
unknown committed
4100
	| DESCRIBE;
unknown's avatar
unknown committed
4101 4102 4103 4104

opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
unknown's avatar
unknown committed
4105
	| ident
unknown's avatar
unknown committed
4106
	  { Lex->wild= new String((const char*) $1.str,$1.length,default_charset_info); };
unknown's avatar
unknown committed
4107 4108 4109 4110 4111


/* flush things */

flush:
unknown's avatar
unknown committed
4112 4113 4114 4115 4116
	FLUSH_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
	}
4117 4118 4119
	flush_options
	{}
	;
unknown's avatar
unknown committed
4120 4121 4122

flush_options:
	flush_options ',' flush_option
unknown's avatar
unknown committed
4123
	| flush_option;
unknown's avatar
unknown committed
4124 4125

flush_option:
4126
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
unknown's avatar
unknown committed
4127
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
unknown's avatar
unknown committed
4128
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
unknown's avatar
unknown committed
4129 4130 4131 4132
	| HOSTS_SYM	{ Lex->type|= REFRESH_HOSTS; }
	| PRIVILEGES	{ Lex->type|= REFRESH_GRANT; }
	| LOGS_SYM	{ Lex->type|= REFRESH_LOG; }
	| STATUS_SYM	{ Lex->type|= REFRESH_STATUS; }
unknown's avatar
unknown committed
4133 4134
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
4135 4136
	| DES_KEY_FILE	{ Lex->type|= REFRESH_DES_KEY_FILE; }
 	| RESOURCES     { Lex->type|= REFRESH_USER_RESOURCES; };
unknown's avatar
unknown committed
4137

unknown's avatar
unknown committed
4138
opt_table_list:
4139 4140
	/* empty */  {;}
	| table_list {;};
unknown's avatar
unknown committed
4141

unknown's avatar
unknown committed
4142
reset:
unknown's avatar
unknown committed
4143 4144 4145 4146
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
4147 4148 4149 4150
	} reset_options
	{}
	;

unknown's avatar
unknown committed
4151 4152
reset_options:
	reset_options ',' reset_option
unknown's avatar
unknown committed
4153
	| reset_option;
unknown's avatar
unknown committed
4154 4155

reset_option:
unknown's avatar
unknown committed
4156 4157
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
unknown's avatar
unknown committed
4158
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
unknown's avatar
unknown committed
4159

unknown's avatar
unknown committed
4160
purge:
unknown's avatar
unknown committed
4161 4162 4163 4164 4165 4166
	PURGE
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_PURGE;
	  lex->type=0;
	}
unknown's avatar
unknown committed
4167 4168 4169
        MASTER_SYM LOGS_SYM TO_SYM TEXT_STRING
         {
	   Lex->to_log = $6.str;
unknown's avatar
unknown committed
4170
         } ;
unknown's avatar
unknown committed
4171

unknown's avatar
unknown committed
4172 4173 4174
/* kill threads */

kill:
4175
	KILL_SYM expr
unknown's avatar
unknown committed
4176
	{
unknown's avatar
unknown committed
4177
	  LEX *lex=Lex;
unknown's avatar
unknown committed
4178
	  if ($2->fix_fields(lex->thd, 0, &$2) || $2->check_cols(1))
4179
	  {
4180
	    send_error(lex->thd, ER_SET_CONSTANTS_ONLY);
unknown's avatar
unknown committed
4181 4182 4183 4184
	    YYABORT;
	  }
          lex->sql_command=SQLCOM_KILL;
	  lex->thread_id= (ulong) $2->val_int();
unknown's avatar
unknown committed
4185
	};
unknown's avatar
unknown committed
4186 4187 4188 4189

/* change database */

use:	USE_SYM ident
unknown's avatar
unknown committed
4190 4191
	{
	  LEX *lex=Lex;
4192 4193
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
unknown's avatar
unknown committed
4194
	};
unknown's avatar
unknown committed
4195 4196 4197

/* import, export of files */

4198
load:	LOAD DATA_SYM load_data_lock opt_local INFILE TEXT_STRING
unknown's avatar
unknown committed
4199
	{
unknown's avatar
unknown committed
4200 4201
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
unknown's avatar
unknown committed
4202 4203
	  lex->lock_option= $3;
	  lex->local_file=  $4;
unknown's avatar
unknown committed
4204
	  if (!(lex->exchange= new sql_exchange($6.str,0)))
unknown's avatar
unknown committed
4205
	    YYABORT;
unknown's avatar
unknown committed
4206
	  lex->field_list.empty();
unknown's avatar
unknown committed
4207 4208 4209 4210
	}
	opt_duplicate INTO TABLE_SYM table_ident opt_field_term opt_line_term
	opt_ignore_lines opt_field_spec
	{
unknown's avatar
unknown committed
4211
	  if (!Select->add_table_to_list(YYTHD, $11, NULL, TL_OPTION_UPDATING))
unknown's avatar
unknown committed
4212 4213 4214
	    YYABORT;
	}
        |
unknown's avatar
unknown committed
4215
	LOAD TABLE_SYM table_ident FROM MASTER_SYM
unknown's avatar
unknown committed
4216 4217
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
unknown's avatar
unknown committed
4218
	  if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
unknown's avatar
unknown committed
4219
	    YYABORT;
unknown's avatar
unknown committed
4220 4221

        }
4222 4223 4224 4225
        |
	LOAD DATA_SYM FROM MASTER_SYM
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
unknown's avatar
unknown committed
4226
        };
unknown's avatar
unknown committed
4227 4228 4229

opt_local:
	/* empty */	{ $$=0;}
unknown's avatar
unknown committed
4230
	| LOCAL_SYM	{ $$=1;};
unknown's avatar
unknown committed
4231

unknown's avatar
unknown committed
4232
load_data_lock:
4233
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
4234 4235
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
4236 4237


unknown's avatar
unknown committed
4238 4239 4240
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
unknown's avatar
unknown committed
4241
	| IGNORE_SYM	{ Lex->duplicates=DUP_IGNORE; };
unknown's avatar
unknown committed
4242 4243 4244

opt_field_term:
	/* empty */
unknown's avatar
unknown committed
4245
	| COLUMNS field_term_list;
unknown's avatar
unknown committed
4246 4247 4248

field_term_list:
	field_term_list field_term
unknown's avatar
unknown committed
4249
	| field_term;
unknown's avatar
unknown committed
4250 4251 4252 4253

field_term:
	TERMINATED BY text_string { Lex->exchange->field_term= $3;}
	| OPTIONALLY ENCLOSED BY text_string
unknown's avatar
unknown committed
4254 4255 4256 4257 4258
	  {
	    LEX *lex=Lex;
	    lex->exchange->enclosed= $4;
	    lex->exchange->opt_enclosed=1;
	  }
unknown's avatar
unknown committed
4259
	| ENCLOSED BY text_string { Lex->exchange->enclosed= $3;}
unknown's avatar
unknown committed
4260
	| ESCAPED BY text_string  { Lex->exchange->escaped= $3;};
unknown's avatar
unknown committed
4261 4262 4263

opt_line_term:
	/* empty */
unknown's avatar
unknown committed
4264
	| LINES line_term_list;
unknown's avatar
unknown committed
4265 4266 4267

line_term_list:
	line_term_list line_term
unknown's avatar
unknown committed
4268
	| line_term;
unknown's avatar
unknown committed
4269 4270 4271

line_term:
	TERMINATED BY text_string { Lex->exchange->line_term= $3;}
unknown's avatar
unknown committed
4272
	| STARTING BY text_string { Lex->exchange->line_start= $3;};
unknown's avatar
unknown committed
4273 4274 4275 4276

opt_ignore_lines:
	/* empty */
	| IGNORE_SYM NUM LINES
unknown's avatar
unknown committed
4277
	  { Lex->exchange->skip_lines=atol($2.str); };
unknown's avatar
unknown committed
4278 4279 4280 4281

/* Common definitions */

text_literal:
4282 4283 4284 4285 4286
	TEXT_STRING
	{ $$ = new Item_string($1.str,$1.length,
			       YYTHD->variables.thd_charset); }
	| UNDERSCORE_CHARSET TEXT_STRING
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
unknown's avatar
unknown committed
4287
	| text_literal TEXT_STRING
4288
	  { ((Item_string*) $1)->append($2.str,$2.length); };
unknown's avatar
unknown committed
4289 4290

text_string:
4291 4292
	TEXT_STRING
	{ $$=  new String($1.str,$1.length,YYTHD->variables.thd_charset); }
unknown's avatar
unknown committed
4293 4294
	| HEX_NUM
	  {
4295
	    Item *tmp = new Item_varbinary($1.str,$1.length);
unknown's avatar
unknown committed
4296
	    $$= tmp ? tmp->val_str((String*) 0) : (String*) 0;
4297 4298 4299
	  }
	;

unknown's avatar
unknown committed
4300
param_marker:
4301
        '?'
unknown's avatar
unknown committed
4302
        {
4303
	  LEX *lex=Lex;
4304
          if (YYTHD->prepare_command)
4305 4306
          {
            lex->param_list.push_back($$=new Item_param());
4307
            lex->param_count++;
unknown's avatar
unknown committed
4308
          }
4309
          else
unknown's avatar
unknown committed
4310 4311 4312 4313
          {
            yyerror("You have an error in your SQL syntax");
            YYABORT;
          }
4314 4315 4316
        }
	;

unknown's avatar
unknown committed
4317 4318
literal:
	text_literal	{ $$ =	$1; }
4319 4320
	| NUM		{ $$ =	new Item_int($1.str, (longlong) strtol($1.str, NULL, 10),$1.length); }
	| LONG_NUM	{ $$ =	new Item_int($1.str, (longlong) strtoll($1.str,NULL,10), $1.length); }
unknown's avatar
unknown committed
4321
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
unknown's avatar
unknown committed
4322 4323 4324 4325
	| REAL_NUM	{ $$ =	new Item_real($1.str, $1.length); }
	| FLOAT_NUM	{ $$ =	new Item_float($1.str, $1.length); }
	| NULL_SYM	{ $$ =	new Item_null();
			  Lex->next_state=STATE_OPERATOR_OR_IDENT;}
4326
	| HEX_NUM	{ $$ =	new Item_varbinary($1.str,$1.length);}
unknown's avatar
unknown committed
4327 4328
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
unknown's avatar
unknown committed
4329
	| TIMESTAMP text_literal { $$ = $2; };
unknown's avatar
unknown committed
4330 4331 4332 4333 4334 4335 4336

/**********************************************************************
** Createing different items.
**********************************************************************/

insert_ident:
	simple_ident	 { $$=$1; }
unknown's avatar
unknown committed
4337
	| table_wild	 { $$=$1; };
unknown's avatar
unknown committed
4338 4339

table_wild:
4340 4341 4342 4343 4344
	ident '.' '*' 
	{
	  $$ = new Item_field(NullS,$1.str,"*");
	  Lex->current_select->select_lex()->with_wild++;
	}
unknown's avatar
unknown committed
4345
	| ident '.' ident '.' '*'
4346 4347 4348 4349 4350 4351 4352
	{
	  $$ = new Item_field((YYTHD->client_capabilities &
   			     CLIENT_NO_SCHEMA ? NullS : $1.str),
			     $3.str,"*");
	  Lex->current_select->select_lex()->with_wild++;
	}
	;
unknown's avatar
unknown committed
4353 4354

order_ident:
unknown's avatar
unknown committed
4355
	expr { $$=$1; };
unknown's avatar
unknown committed
4356 4357 4358

simple_ident:
	ident
unknown's avatar
unknown committed
4359
	{
4360 4361 4362 4363 4364 4365 4366 4367
	  sp_pvar_t *spv;
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;

	  if (spc && (spv = spc->find_pvar(&$1)))
	  { /* We're compiling a stored procedure and found a variable */
	    if (lex->sql_command != SQLCOM_CALL && ! spv->isset)
	    {
4368 4369
	      send_error(YYTHD, ER_SP_UNINIT_VAR);
	      YYABORT;
4370 4371 4372 4373 4374 4375 4376 4377 4378
	    }
	    else
	      $$ = (Item*) new Item_splocal(spv->offset);
	  }
	  else
	  {
	    SELECT_LEX_NODE *sel=Select;
	    $$ = !sel->create_refs || sel->get_in_sum_expr() > 0 ? (Item*) new Item_field(NullS,NullS,$1.str) : (Item*) new Item_ref(NullS,NullS,$1.str);
	  }
unknown's avatar
unknown committed
4379
	}
unknown's avatar
unknown committed
4380
	| ident '.' ident
unknown's avatar
unknown committed
4381
	{
unknown's avatar
unknown committed
4382 4383 4384 4385 4386 4387 4388 4389 4390
	  THD *thd= YYTHD;
	  LEX *lex= &thd->lex;
	  SELECT_LEX_NODE *sel= lex->current_select;
	  if (sel->no_table_names_allowed)
	  {
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE, 
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $1.str, thd->where);
	  }
4391
	  $$ = !sel->create_refs || sel->get_in_sum_expr() > 0 ? (Item*) new Item_field(NullS,$1.str,$3.str) : (Item*) new Item_ref(NullS,$1.str,$3.str);
unknown's avatar
unknown committed
4392
	}
unknown's avatar
unknown committed
4393
	| '.' ident '.' ident
unknown's avatar
unknown committed
4394
	{
unknown's avatar
unknown committed
4395 4396 4397 4398 4399 4400 4401 4402 4403
	  THD *thd= YYTHD;
	  LEX *lex= &thd->lex;
	  SELECT_LEX_NODE *sel= lex->current_select;
	  if (sel->no_table_names_allowed)
	  {
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE, 
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $2.str, thd->where);
	  }
4404
	  $$ = !sel->create_refs || sel->get_in_sum_expr() > 0 ? (Item*) new Item_field(NullS,$2.str,$4.str) : (Item*) new Item_ref(NullS,$2.str,$4.str);
unknown's avatar
unknown committed
4405
	}
unknown's avatar
unknown committed
4406
	| ident '.' ident '.' ident
unknown's avatar
unknown committed
4407
	{
unknown's avatar
unknown committed
4408 4409 4410 4411 4412 4413 4414 4415 4416
	  THD *thd= YYTHD;
	  LEX *lex= &thd->lex;
	  SELECT_LEX_NODE *sel= lex->current_select;
	  if (sel->no_table_names_allowed)
	  {
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE, 
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $3.str, thd->where);
	  }
4417
	  $$ = !sel->create_refs || sel->get_in_sum_expr() > 0 ? (Item*) new Item_field((YYTHD->client_capabilities & CLIENT_NO_SCHEMA ? NullS :$1.str),$3.str,$5.str) : (Item*) new Item_ref((YYTHD->client_capabilities & CLIENT_NO_SCHEMA ? NullS :$1.str),$3.str,$5.str);
unknown's avatar
unknown committed
4418
	};
unknown's avatar
unknown committed
4419 4420 4421 4422 4423


field_ident:
	ident			{ $$=$1;}
	| ident '.' ident	{ $$=$3;}	/* Skipp schema name in create*/
unknown's avatar
unknown committed
4424
	| '.' ident		{ $$=$2;}	/* For Delphi */;
unknown's avatar
unknown committed
4425 4426 4427 4428

table_ident:
	ident			{ $$=new Table_ident($1); }
	| ident '.' ident	{ $$=new Table_ident($1,$3,0);}
unknown's avatar
unknown committed
4429 4430
	| '.' ident		{ $$=new Table_ident($2);}
   /* For Delphi */;
unknown's avatar
unknown committed
4431 4432 4433 4434 4435

ident:
	IDENT	    { $$=$1; }
	| keyword
	{
4436 4437
	  LEX *lex= Lex;
	  $$.str= lex->thd->strmake($1.str,$1.length);
unknown's avatar
unknown committed
4438
	  $$.length=$1.length;
4439
	  if (lex->next_state != STATE_END)
unknown's avatar
unknown committed
4440
	    lex->next_state=STATE_OPERATOR_OR_IDENT;
4441 4442
	}
	;
unknown's avatar
unknown committed
4443 4444 4445

ident_or_text:
	ident 		{ $$=$1;}
unknown's avatar
unknown committed
4446
	| TEXT_STRING	{ $$=$1;}
unknown's avatar
unknown committed
4447
	| LEX_HOSTNAME	{ $$=$1;};
unknown's avatar
unknown committed
4448 4449 4450 4451

user:
	ident_or_text
	{
unknown's avatar
unknown committed
4452 4453
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
4454 4455 4456 4457 4458
	    YYABORT;
	  $$->user = $1; $$->host.str=NullS;
	  }
	| ident_or_text '@' ident_or_text
	  {
unknown's avatar
unknown committed
4459 4460
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
4461 4462
	      YYABORT;
	    $$->user = $1; $$->host=$3;
unknown's avatar
unknown committed
4463
	  };
unknown's avatar
unknown committed
4464 4465 4466 4467 4468 4469

/* Keyword that we allow for identifiers */

keyword:
	ACTION			{}
	| AFTER_SYM		{}
4470
	| AGAINST		{}
unknown's avatar
unknown committed
4471
	| AGGREGATE_SYM		{}
unknown's avatar
unknown committed
4472
	| ANY_SYM		{}
unknown's avatar
unknown committed
4473
	| ASCII_SYM		{}
unknown's avatar
unknown committed
4474
	| AUTO_INC		{}
unknown's avatar
unknown committed
4475 4476
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
unknown's avatar
unknown committed
4477
	| BACKUP_SYM		{}
unknown's avatar
unknown committed
4478
	| BEGIN_SYM		{}
unknown's avatar
unknown committed
4479
	| BERKELEY_DB_SYM	{}
unknown's avatar
unknown committed
4480
	| BINLOG_SYM		{}
unknown's avatar
unknown committed
4481 4482
	| BIT_SYM		{}
	| BOOL_SYM		{}
unknown's avatar
unknown committed
4483
	| BOOLEAN_SYM		{}
4484
	| BYTE_SYM		{}
unknown's avatar
unknown committed
4485
	| CACHE_SYM		{}
4486
	| CHANGED		{}
4487
	| CHARSET		{}
unknown's avatar
unknown committed
4488
	| CHECKSUM_SYM		{}
4489
	| CIPHER_SYM		{}
unknown's avatar
unknown committed
4490
	| CLIENT_SYM		{}
4491
	| CLOSE_SYM		{}
unknown's avatar
unknown committed
4492
	| COMMENT_SYM		{}
unknown's avatar
unknown committed
4493
	| COMMITTED_SYM		{}
unknown's avatar
unknown committed
4494
	| COMMIT_SYM		{}
unknown's avatar
unknown committed
4495
	| COMPRESSED_SYM	{}
unknown's avatar
unknown committed
4496
	| CONCURRENT		{}
4497
	| CUBE_SYM		{}
unknown's avatar
unknown committed
4498 4499 4500 4501 4502
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
	| DELAY_KEY_WRITE_SYM	{}
unknown's avatar
unknown committed
4503 4504
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
unknown's avatar
unknown committed
4505
	| DO_SYM		{}
4506
	| DUAL_SYM		{}
unknown's avatar
unknown committed
4507 4508
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
unknown's avatar
unknown committed
4509 4510 4511 4512
	| DYNAMIC_SYM		{}
	| END			{}
	| ENUM			{}
	| ESCAPE_SYM		{}
unknown's avatar
unknown committed
4513
	| EVENTS_SYM		{}
unknown's avatar
unknown committed
4514
	| EXECUTE_SYM		{}
unknown's avatar
unknown committed
4515
	| EXTENDED_SYM		{}
4516
	| FAST_SYM		{}
unknown's avatar
unknown committed
4517 4518
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
unknown's avatar
unknown committed
4519
	| FULL			{}
unknown's avatar
unknown committed
4520 4521 4522 4523
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
	| FLUSH_SYM		{}
4524
	| GEOMETRY_SYM		{}
unknown's avatar
unknown committed
4525
	| GRANTS		{}
unknown's avatar
unknown committed
4526
	| GLOBAL_SYM		{}
4527
	| HANDLER_SYM		{}
4528 4529
	| HEAP_SYM		{}
	| HELP_SYM		{}
unknown's avatar
unknown committed
4530 4531 4532
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
unknown's avatar
unknown committed
4533
	| INDEXES		{}
unknown's avatar
unknown committed
4534
	| ISOLATION		{}
unknown's avatar
unknown committed
4535
	| ISAM_SYM		{}
unknown's avatar
unknown committed
4536
	| ISSUER_SYM		{}
unknown's avatar
unknown committed
4537
	| INNOBASE_SYM		{}
4538
	| INSERT_METHOD		{}
unknown's avatar
unknown committed
4539
	| IO_THREAD		{}
4540
	| LAST_SYM		{}
unknown's avatar
unknown committed
4541
	| LEVEL_SYM		{}
4542
	| LINESTRING		{}
unknown's avatar
unknown committed
4543
	| LOCAL_SYM		{}
unknown's avatar
unknown committed
4544
	| LOCKS_SYM		{}
unknown's avatar
unknown committed
4545 4546 4547 4548 4549 4550 4551 4552 4553 4554
	| LOGS_SYM		{}
	| MAX_ROWS		{}
	| MASTER_SYM		{}
	| MASTER_HOST_SYM	{}
	| MASTER_PORT_SYM	{}
	| MASTER_LOG_FILE_SYM	{}
	| MASTER_LOG_POS_SYM	{}
	| MASTER_USER_SYM	{}
	| MASTER_PASSWORD_SYM	{}
	| MASTER_CONNECT_RETRY_SYM	{}
unknown's avatar
unknown committed
4555 4556 4557
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
4558
	| MEDIUM_SYM		{}
unknown's avatar
unknown committed
4559 4560 4561 4562
	| MERGE_SYM		{}
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
unknown's avatar
unknown committed
4563
	| MODE_SYM		{}
unknown's avatar
unknown committed
4564
	| MONTH_SYM		{}
4565 4566 4567
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
unknown's avatar
unknown committed
4568 4569 4570
	| MYISAM_SYM		{}
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
4571
	| NEXT_SYM		{}
unknown's avatar
unknown committed
4572
	| NEW_SYM		{}
unknown's avatar
unknown committed
4573
	| NO_SYM		{}
4574
	| NONE_SYM		{}
unknown's avatar
unknown committed
4575
	| OFFSET_SYM		{}
4576
	| OPEN_SYM		{}
unknown's avatar
unknown committed
4577
	| PACK_KEYS_SYM		{}
4578
	| PARTIAL		{}
unknown's avatar
unknown committed
4579
	| PASSWORD		{}
4580 4581
	| POINT			{}
	| POLYGON		{}
4582
	| PREV_SYM		{}
unknown's avatar
unknown committed
4583 4584
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
unknown's avatar
unknown committed
4585
	| QUERY_SYM		{}
4586
	| QUICK			{}
unknown's avatar
unknown committed
4587
	| RAID_0_SYM		{}
unknown's avatar
unknown committed
4588 4589
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
unknown's avatar
unknown committed
4590
	| RAID_STRIPED_SYM	{}
unknown's avatar
unknown committed
4591
	| RAID_TYPE		{}
unknown's avatar
unknown committed
4592 4593
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
unknown's avatar
unknown committed
4594 4595
	| RELOAD		{}
	| REPAIR		{}
unknown's avatar
unknown committed
4596
	| REPEATABLE_SYM	{}
unknown's avatar
unknown committed
4597
	| REPLICATION		{}
unknown's avatar
unknown committed
4598
	| RESET_SYM		{}
4599
	| RESOURCES		{}
unknown's avatar
unknown committed
4600
	| RESTORE_SYM		{}
unknown's avatar
unknown committed
4601
	| ROLLBACK_SYM		{}
4602
	| ROLLUP_SYM		{}
unknown's avatar
unknown committed
4603 4604 4605 4606
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
	| SECOND_SYM		{}
4607
	| SERIAL_SYM		{}
unknown's avatar
unknown committed
4608 4609
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
unknown's avatar
unknown committed
4610
	| SIGNED_SYM		{}
4611
	| SIMPLE_SYM		{}
unknown's avatar
unknown committed
4612
	| SHARE_SYM		{}
unknown's avatar
unknown committed
4613
	| SHUTDOWN		{}
unknown's avatar
unknown committed
4614
	| SLAVE			{}
unknown's avatar
unknown committed
4615
	| SQL_CACHE_SYM		{}
unknown's avatar
unknown committed
4616
	| SQL_BUFFER_RESULT	{}
unknown's avatar
unknown committed
4617
	| SQL_NO_CACHE_SYM	{}
unknown's avatar
unknown committed
4618
	| SQL_THREAD		{}
unknown's avatar
unknown committed
4619 4620 4621 4622
	| START_SYM		{}
	| STATUS_SYM		{}
	| STOP_SYM		{}
	| STRING_SYM		{}
unknown's avatar
unknown committed
4623
	| SUBJECT_SYM		{}
unknown's avatar
unknown committed
4624
	| SUPER_SYM		{}
unknown's avatar
unknown committed
4625 4626
	| TEMPORARY		{}
	| TEXT_SYM		{}
unknown's avatar
unknown committed
4627
	| TRANSACTION_SYM	{}
4628
	| TRUNCATE_SYM		{}
unknown's avatar
unknown committed
4629 4630 4631
	| TIMESTAMP		{}
	| TIME_SYM		{}
	| TYPE_SYM		{}
4632
	| FUNCTION_SYM		{}
unknown's avatar
unknown committed
4633
	| UNCOMMITTED_SYM	{}
unknown's avatar
unknown committed
4634
	| UNICODE_SYM		{}
unknown's avatar
unknown committed
4635
	| USE_FRM		{}
unknown's avatar
unknown committed
4636
	| VARIABLES		{}
4637
	| VALUE_SYM		{}
unknown's avatar
unknown committed
4638
	| WORK_SYM		{}
unknown's avatar
unknown committed
4639
	| YEAR_SYM		{}
unknown's avatar
unknown committed
4640
	| SOUNDS_SYM            {}
unknown's avatar
unknown committed
4641
	;
unknown's avatar
unknown committed
4642 4643 4644 4645 4646 4647

/* Option functions */

set:
	SET opt_option
	{
unknown's avatar
unknown committed
4648 4649
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
unknown's avatar
unknown committed
4650 4651
	  lex->option_type=OPT_DEFAULT;
	  lex->var_list.empty();
unknown's avatar
unknown committed
4652
	}
4653 4654 4655
	option_value_list
	{}
	;
unknown's avatar
unknown committed
4656 4657 4658

opt_option:
	/* empty */ {}
unknown's avatar
unknown committed
4659
	| OPTION {};
unknown's avatar
unknown committed
4660 4661

option_value_list:
unknown's avatar
unknown committed
4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673
	option_type option_value
	| option_value_list ',' option_type option_value;

option_type:
	/* empty */	{}
	| GLOBAL_SYM	{ Lex->option_type= OPT_GLOBAL; }
	| LOCAL_SYM	{ Lex->option_type= OPT_SESSION; }
	| SESSION_SYM	{ Lex->option_type= OPT_SESSION; }
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
unknown's avatar
unknown committed
4674
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
4675 4676 4677 4678 4679 4680
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

opt_var_ident_type:
	/* empty */		{ $$=OPT_DEFAULT; }
unknown's avatar
unknown committed
4681
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
4682 4683 4684
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
unknown's avatar
unknown committed
4685 4686

option_value:
unknown's avatar
unknown committed
4687
	  '@' ident_or_text equal expr
unknown's avatar
unknown committed
4688
	  {
unknown's avatar
unknown committed
4689
	    Lex->var_list.push_back(new set_var_user(new Item_func_set_user_var($2,$4)));
unknown's avatar
unknown committed
4690
	  }
unknown's avatar
unknown committed
4691 4692
	| internal_or_splocal
	  {}
unknown's avatar
unknown committed
4693
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
4694 4695 4696 4697 4698 4699 4700 4701
	  {
	    LEX *lex=Lex;
	    lex->var_list.push_back(new set_var((enum_var_type) $3, $4, $6));
	  }
	| TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	  {
	    LEX *lex=Lex;
	    lex->var_list.push_back(new set_var(lex->option_type,
unknown's avatar
unknown committed
4702
						find_sys_var("tx_isolation"),
unknown's avatar
unknown committed
4703
						new Item_int((int32) $4)));
unknown's avatar
unknown committed
4704
	  }
unknown's avatar
unknown committed
4705
	| CHAR_SYM SET opt_equal set_expr_or_default
unknown's avatar
unknown committed
4706 4707 4708 4709 4710 4711
	  {
	    LEX *lex=Lex;
	    lex->var_list.push_back(new set_var(lex->option_type,
	    			                find_sys_var("convert_character_set"),
						$4));
	  }
unknown's avatar
unknown committed
4712
	| PASSWORD equal text_or_password
unknown's avatar
unknown committed
4713
	  {
4714
	    THD *thd=YYTHD;
unknown's avatar
unknown committed
4715
	    LEX_USER *user;
unknown's avatar
unknown committed
4716
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
unknown's avatar
unknown committed
4717 4718 4719 4720 4721 4722
	      YYABORT;
	    user->host.str=0;
	    user->user.str=thd->priv_user;
	    thd->lex.var_list.push_back(new set_var_password(user, $3));
	  }
	| PASSWORD FOR_SYM user equal text_or_password
unknown's avatar
unknown committed
4723
	  {
unknown's avatar
unknown committed
4724 4725
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
4726
	;
unknown's avatar
unknown committed
4727

unknown's avatar
unknown committed
4728 4729 4730 4731 4732 4733 4734 4735
internal_variable_name:
	ident
	{
	  sys_var *tmp=find_sys_var($1.str, $1.length);
	  if (!tmp)
	    YYABORT;
	  $$=tmp;
	}
4736
        ;
unknown's avatar
unknown committed
4737

unknown's avatar
unknown committed
4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763
internal_or_splocal:
	ident equal set_expr_or_default
	{
	  LEX *lex= Lex;
          sp_pcontext *spc= lex->spcont;
	  sp_pvar_t *spv;

	  if (!spc || !(spv = spc->find_pvar(&$1)))
	  { /* Not an SP local variable */
	    sys_var *tmp= find_sys_var($1.str, $1.length);

	    if (!tmp)
	      YYABORT;
	    lex->var_list.push_back(new set_var(lex->option_type, tmp, $3));
	  }
	  else
	  { /* An SP local variable */
	    sp_instr_set *i= new sp_instr_set(lex->sphead->instructions(),
	                                      spv->offset, $3, spv->type);

	    lex->sphead->add_instr(i);
	    spv->isset= TRUE;
	  }
	}
	;

unknown's avatar
unknown committed
4764 4765 4766 4767 4768 4769
isolation_types:
	READ_SYM UNCOMMITTED_SYM	{ $$= ISO_READ_UNCOMMITTED; }
	| READ_SYM COMMITTED_SYM	{ $$= ISO_READ_COMMITTED; }
	| REPEATABLE_SYM READ_SYM	{ $$= ISO_REPEATABLE_READ; }
	| SERIALIZABLE_SYM		{ $$= ISO_SERIALIZABLE; }
	;
4770

unknown's avatar
unknown committed
4771 4772 4773 4774 4775 4776 4777 4778
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
	    if (!$3.length)
	      $$=$3.str;
	    else
	    {
unknown's avatar
unknown committed
4779
	      char *buff=(char*) YYTHD->alloc(HASH_PASSWORD_LENGTH+1);
4780 4781
	      make_scrambled_password(buff,$3.str,use_old_passwords,
				      &YYTHD->rand);
unknown's avatar
unknown committed
4782 4783
	      $$=buff;
	    }
4784 4785
	  }
          ;
unknown's avatar
unknown committed
4786

unknown's avatar
unknown committed
4787

unknown's avatar
unknown committed
4788
set_expr_or_default:
unknown's avatar
unknown committed
4789 4790
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
4791 4792
	| ON	  { $$=new Item_string("ON",  2, system_charset_info); }
	| ALL	  { $$=new Item_string("ALL", 3, system_charset_info); }
unknown's avatar
unknown committed
4793
	;
unknown's avatar
unknown committed
4794 4795


unknown's avatar
unknown committed
4796 4797 4798 4799 4800 4801 4802
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
	  Lex->sql_command=SQLCOM_LOCK_TABLES;
	}
4803 4804 4805
	table_lock_list
	{}
	;
unknown's avatar
unknown committed
4806 4807 4808

table_or_tables:
	TABLE_SYM
unknown's avatar
unknown committed
4809
	| TABLES;
unknown's avatar
unknown committed
4810 4811 4812

table_lock_list:
	table_lock
unknown's avatar
unknown committed
4813
	| table_lock_list ',' table_lock;
unknown's avatar
unknown committed
4814 4815 4816

table_lock:
	table_ident opt_table_alias lock_option
4817
	{
unknown's avatar
unknown committed
4818
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
4819 4820
	   YYABORT;
	}
4821
        ;
unknown's avatar
unknown committed
4822 4823 4824

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
4825
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
unknown's avatar
unknown committed
4826
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
4827 4828
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
unknown's avatar
unknown committed
4829 4830

unlock:
4831 4832
	UNLOCK_SYM table_or_tables { Lex->sql_command=SQLCOM_UNLOCK_TABLES; }
        ;
unknown's avatar
unknown committed
4833 4834


4835
/*
unknown's avatar
unknown committed
4836
** Handler: direct access to ISAM functions
4837 4838 4839 4840 4841
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
4842 4843
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_OPEN;
unknown's avatar
unknown committed
4844
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
4845 4846 4847 4848
	    YYABORT;
	}
	| HANDLER_SYM table_ident CLOSE_SYM
	{
4849 4850
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_CLOSE;
unknown's avatar
unknown committed
4851
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
4852 4853
	    YYABORT;
	}
unknown's avatar
unknown committed
4854
	| HANDLER_SYM table_ident READ_SYM
4855
	{
unknown's avatar
unknown committed
4856 4857 4858
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
4859 4860
	  lex->current_select->select_limit= 1;
	  lex->current_select->offset_limit= 0L;
unknown's avatar
unknown committed
4861
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
4862
	    YYABORT;
unknown's avatar
unknown committed
4863
        }
4864
        handler_read_or_scan where_clause opt_limit_clause {}
4865
        ;
4866

unknown's avatar
unknown committed
4867 4868
handler_read_or_scan:
	handler_scan_function         { Lex->backup_dir= 0; }
4869 4870
        | ident handler_rkey_function { Lex->backup_dir= $1.str; }
        ;
unknown's avatar
unknown committed
4871 4872 4873

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
4874 4875
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
unknown's avatar
unknown committed
4876 4877 4878 4879 4880 4881

handler_rkey_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
	| PREV_SYM { Lex->ha_read_mode = RPREV;  }
	| LAST_SYM { Lex->ha_read_mode = RLAST;  }
4882 4883
	| handler_rkey_mode
	{
unknown's avatar
unknown committed
4884 4885
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
unknown's avatar
unknown committed
4886
	  lex->ha_rkey_mode=$1;
unknown's avatar
unknown committed
4887
	  if (!(lex->insert_list = new List_item))
4888
	    YYABORT;
4889 4890
	} '(' values ')' { }
        ;
4891 4892

handler_rkey_mode:
unknown's avatar
unknown committed
4893 4894 4895 4896
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
4897 4898
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
4899

unknown's avatar
unknown committed
4900 4901 4902 4903 4904
/* GRANT / REVOKE */

revoke:
	REVOKE
	{
unknown's avatar
unknown committed
4905 4906 4907 4908 4909
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_REVOKE;
	  lex->users_list.empty();
	  lex->columns.empty();
	  lex->grant= lex->grant_tot_col=0;
4910
	  lex->select_lex.db=0;
4911 4912
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
4913
	  bzero((char*) &lex->mqh, sizeof(lex->mqh));
unknown's avatar
unknown committed
4914
	}
4915 4916 4917
	grant_privileges ON opt_table FROM user_list
	{}
	;
unknown's avatar
unknown committed
4918 4919 4920 4921

grant:
	GRANT
	{
unknown's avatar
unknown committed
4922 4923 4924
	  LEX *lex=Lex;
	  lex->users_list.empty();
	  lex->columns.empty();
4925 4926
	  lex->sql_command = SQLCOM_GRANT;
	  lex->grant= lex->grant_tot_col= 0;
4927
	  lex->select_lex.db= 0;
4928 4929
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
4930
	  bzero(&(lex->mqh),sizeof(lex->mqh));
unknown's avatar
unknown committed
4931 4932
	}
	grant_privileges ON opt_table TO_SYM user_list
4933 4934 4935
	require_clause grant_options
	{}
	;
unknown's avatar
unknown committed
4936 4937 4938

grant_privileges:
	grant_privilege_list {}
unknown's avatar
unknown committed
4939
	| ALL PRIVILEGES	{ Lex->grant = GLOBAL_ACLS;}
4940 4941
	| ALL			{ Lex->grant = GLOBAL_ACLS;}
        ;
unknown's avatar
unknown committed
4942 4943 4944

grant_privilege_list:
	grant_privilege
unknown's avatar
unknown committed
4945
	| grant_privilege_list ',' grant_privilege;
unknown's avatar
unknown committed
4946 4947

grant_privilege:
4948 4949 4950 4951
	SELECT_SYM 	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
	| INSERT	{ Lex->which_columns = INSERT_ACL;} opt_column_list {}
	| UPDATE_SYM	{ Lex->which_columns = UPDATE_ACL; } opt_column_list {}
	| REFERENCES	{ Lex->which_columns = REFERENCES_ACL;} opt_column_list {}
unknown's avatar
unknown committed
4952 4953
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
unknown's avatar
unknown committed
4954 4955 4956 4957
	| INDEX		{ Lex->grant |= INDEX_ACL;}
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
unknown's avatar
unknown committed
4958
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
unknown's avatar
unknown committed
4959 4960 4961 4962
	| RELOAD	{ Lex->grant |= RELOAD_ACL;}
	| SHUTDOWN	{ Lex->grant |= SHUTDOWN_ACL;}
	| PROCESS	{ Lex->grant |= PROCESS_ACL;}
	| FILE_SYM	{ Lex->grant |= FILE_ACL;}
unknown's avatar
unknown committed
4963 4964 4965 4966 4967 4968 4969 4970
	| GRANT OPTION  { Lex->grant |= GRANT_ACL;}
	| SHOW DATABASES { Lex->grant |= SHOW_DB_ACL;}
	| SUPER_SYM	{ Lex->grant |= SUPER_ACL;}
	| CREATE TEMPORARY TABLES { Lex->grant |= CREATE_TMP_ACL;}
	| LOCK_SYM TABLES   { Lex->grant |= LOCK_TABLES_ACL; }
	| REPLICATION SLAVE  { Lex->grant |= REPL_SLAVE_ACL;}
	| REPLICATION CLIENT_SYM { Lex->grant |= REPL_CLIENT_ACL;}
	;
unknown's avatar
unknown committed
4971

unknown's avatar
unknown committed
4972

4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984
opt_and:
	/* empty */	{}
	| AND		{}
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
unknown's avatar
unknown committed
4985 4986 4987 4988
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
4989
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "SUBJECT");
unknown's avatar
unknown committed
4990 4991 4992 4993 4994 4995 4996 4997 4998
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
4999
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "ISSUER");
unknown's avatar
unknown committed
5000 5001 5002 5003 5004 5005 5006 5007 5008
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
5009
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "CIPHER");
unknown's avatar
unknown committed
5010 5011 5012 5013 5014
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
5015

unknown's avatar
unknown committed
5016 5017 5018
opt_table:
	'*'
	  {
5019
	    LEX *lex= Lex;
unknown's avatar
unknown committed
5020
	    lex->current_select->select_lex()->db= lex->thd->db;
unknown's avatar
unknown committed
5021
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
5022 5023
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
5024
	    {
5025
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
5026
	      YYABORT;
unknown's avatar
unknown committed
5027
	    }
unknown's avatar
unknown committed
5028 5029 5030
	  }
	| ident '.' '*'
	  {
5031
	    LEX *lex= Lex;
unknown's avatar
unknown committed
5032
	    lex->current_select->select_lex()->db = $1.str;
unknown's avatar
unknown committed
5033
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
5034 5035
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
5036
	    {
5037
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
unknown's avatar
unknown committed
5038 5039 5040 5041 5042
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
5043
	    LEX *lex= Lex;
unknown's avatar
unknown committed
5044
	    lex->current_select->select_lex()->db = NULL;
unknown's avatar
unknown committed
5045 5046
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
unknown's avatar
unknown committed
5047
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
5048
	    {
5049
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
unknown's avatar
unknown committed
5050 5051 5052 5053 5054
	      YYABORT;
	    }
	  }
	| table_ident
	  {
unknown's avatar
unknown committed
5055
	    LEX *lex=Lex;
unknown's avatar
unknown committed
5056
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
unknown's avatar
unknown committed
5057
	      YYABORT;
unknown's avatar
unknown committed
5058
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
5059
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
5060 5061
	  }
          ;
unknown's avatar
unknown committed
5062 5063 5064


user_list:
5065 5066 5067 5068 5069 5070 5071
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' grant_user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
unknown's avatar
unknown committed
5072 5073 5074 5075 5076 5077 5078 5079


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
unknown's avatar
unknown committed
5080
	     char *buff=(char*) YYTHD->alloc(HASH_PASSWORD_LENGTH+1);
unknown's avatar
unknown committed
5081 5082
	     if (buff)
	     {
5083 5084
	       make_scrambled_password(buff,$4.str,use_old_passwords,
				       &YYTHD->rand);
unknown's avatar
unknown committed
5085 5086 5087 5088 5089 5090 5091 5092
	       $1->password.str=buff;
	       $1->password.length=HASH_PASSWORD_LENGTH;
	     }
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
	  { $$=$1; $1->password=$5 ; }
	| user
5093 5094
	  { $$=$1; $1->password.str=NullS; }
        ;
unknown's avatar
unknown committed
5095 5096 5097


opt_column_list:
unknown's avatar
unknown committed
5098 5099 5100 5101 5102
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
unknown's avatar
unknown committed
5103
	| '(' column_list ')';
unknown's avatar
unknown committed
5104 5105 5106

column_list:
	column_list ',' column_list_id
unknown's avatar
unknown committed
5107
	| column_list_id;
unknown's avatar
unknown committed
5108 5109 5110 5111

column_list_id:
	ident
	{
5112
	  String *new_str = new String((const char*) $1.str,$1.length,default_charset_info);
unknown's avatar
unknown committed
5113 5114
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
unknown's avatar
unknown committed
5115
	  LEX *lex=Lex;
unknown's avatar
unknown committed
5116 5117
	  while ((point=iter++))
	  {
5118 5119
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
unknown's avatar
unknown committed
5120 5121
		break;
	  }
unknown's avatar
unknown committed
5122
	  lex->grant_tot_col|= lex->which_columns;
unknown's avatar
unknown committed
5123
	  if (point)
unknown's avatar
unknown committed
5124
	    point->rights |= lex->which_columns;
unknown's avatar
unknown committed
5125
	  else
unknown's avatar
unknown committed
5126
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
5127 5128
	}
        ;
unknown's avatar
unknown committed
5129

unknown's avatar
unknown committed
5130 5131

require_clause: /* empty */
5132
        | REQUIRE_SYM require_list
5133 5134 5135
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
5136
        | REQUIRE_SYM SSL_SYM
5137 5138 5139
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
5140
        | REQUIRE_SYM X509_SYM
5141 5142 5143 5144 5145 5146 5147
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
5148
          ;
unknown's avatar
unknown committed
5149

unknown's avatar
unknown committed
5150
grant_options:
unknown's avatar
unknown committed
5151
	/* empty */ {}
unknown's avatar
unknown committed
5152
	| WITH grant_option_list;
unknown's avatar
unknown committed
5153

unknown's avatar
unknown committed
5154 5155
grant_option_list:
	grant_option_list grant_option {}
5156 5157
	| grant_option {}
        ;
unknown's avatar
unknown committed
5158 5159 5160

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
5161
        | MAX_QUERIES_PER_HOUR ULONG_NUM
unknown's avatar
unknown committed
5162
        {
5163
	  Lex->mqh.questions=$2;
5164
	  Lex->mqh.bits |= 1;
5165
	}
5166
        | MAX_UPDATES_PER_HOUR ULONG_NUM
5167
        {
5168
	  Lex->mqh.updates=$2;
5169
	  Lex->mqh.bits |= 2;
5170
	}
5171
        | MAX_CONNECTIONS_PER_HOUR ULONG_NUM
5172
        {
5173
	  Lex->mqh.connections=$2;
5174
	  Lex->mqh.bits |= 4;
5175 5176
	}
        ;
5177

unknown's avatar
unknown committed
5178
begin:
5179 5180
	BEGIN_SYM   { Lex->sql_command = SQLCOM_BEGIN;} opt_work {}
	;
unknown's avatar
unknown committed
5181 5182 5183

opt_work:
	/* empty */ {}
5184 5185
	| WORK_SYM {;}
        ;
unknown's avatar
unknown committed
5186 5187

commit:
unknown's avatar
unknown committed
5188
	COMMIT_SYM   { Lex->sql_command = SQLCOM_COMMIT;};
unknown's avatar
unknown committed
5189 5190

rollback:
unknown's avatar
unknown committed
5191
	ROLLBACK_SYM { Lex->sql_command = SQLCOM_ROLLBACK;};
5192 5193 5194


/*
unknown's avatar
unknown committed
5195
   UNIONS : glue selects together
5196 5197 5198
*/


5199
union_clause:
5200
	/* empty */ {}
unknown's avatar
unknown committed
5201 5202
	| union_list
	;
5203 5204

union_list:
unknown's avatar
unknown committed
5205
	UNION_SYM    union_option
5206 5207 5208 5209 5210
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
5211
	    net_printf(lex->thd, ER_WRONG_USAGE, "UNION", "INTO");
5212
	    YYABORT;
5213
	  }
5214
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
5215
	  {
5216
	    send_error(lex->thd, ER_SYNTAX_ERROR);
5217 5218
	    YYABORT;
	  }
5219
	  if (mysql_new_select(lex, 0))
5220
	    YYABORT;
5221
	  lex->current_select->linkage=UNION_TYPE;
unknown's avatar
unknown committed
5222
	}
5223
	select_init {}
5224
	;
5225 5226

union_opt:
unknown's avatar
unknown committed
5227
	union_list {}
unknown's avatar
unknown committed
5228 5229
	| optional_order_or_limit {}
	;
5230 5231

optional_order_or_limit:
5232
      	/* Empty */ {}
5233 5234
	|
	  {
unknown's avatar
unknown committed
5235 5236
	    THD *thd= YYTHD;
	    LEX *lex= &thd->lex;
unknown's avatar
unknown committed
5237
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
unknown's avatar
unknown committed
5238
	    SELECT_LEX *sel= lex->current_select->select_lex();
5239
	    sel->master_unit()->global_parameters=
5240 5241
	      sel->master_unit();
	    lex->current_select= sel->master_unit();
unknown's avatar
unknown committed
5242 5243
	    lex->current_select->no_table_names_allowed= 1;
	    thd->where= "global ORDER clause";
5244
	  }
5245
	order_or_limit
unknown's avatar
unknown committed
5246 5247 5248 5249 5250
          {
	    THD *thd= YYTHD;
	    thd->lex.current_select->no_table_names_allowed= 0;
	    thd->where= "";
          }
5251 5252 5253
	;

order_or_limit:
5254 5255
	order_clause opt_limit_clause_init
	| limit_clause
5256
	;
unknown's avatar
unknown committed
5257 5258

union_option:
5259
	/* empty */ {}
unknown's avatar
unknown committed
5260
	| ALL {Select->master_unit()->union_option= 1;};
5261

unknown's avatar
unknown committed
5262 5263
singlerow_subselect:
	subselect_start singlerow_subselect_init
unknown's avatar
unknown committed
5264 5265 5266 5267
	subselect_end
	{
	  $$= $2;
	};
5268

unknown's avatar
unknown committed
5269
singlerow_subselect_init:
5270
	select_init2
unknown's avatar
unknown committed
5271
	{
unknown's avatar
unknown committed
5272
	  $$= new Item_singlerow_subselect(YYTHD,
5273 5274
					   Lex->current_select->master_unit()->
                                             first_select());
unknown's avatar
unknown committed
5275
	};
unknown's avatar
unknown committed
5276 5277

exists_subselect:
unknown's avatar
unknown committed
5278 5279 5280 5281 5282
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
unknown's avatar
unknown committed
5283 5284

exists_subselect_init:
5285
	select_init2
unknown's avatar
unknown committed
5286
	{
5287
	  $$= new Item_exists_subselect(YYTHD,
5288 5289
					Lex->current_select->master_unit()->
					  first_select());
unknown's avatar
unknown committed
5290
	};
5291

unknown's avatar
unknown committed
5292 5293 5294 5295 5296 5297 5298 5299
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
5300
  select_init2
unknown's avatar
unknown committed
5301
  {
unknown's avatar
unknown committed
5302
    $$= Lex->current_select->master_unit()->first_select();
unknown's avatar
unknown committed
5303 5304
  };

5305
subselect_start:
5306
	'(' SELECT_SYM
unknown's avatar
unknown committed
5307
	{
5308
	  LEX *lex=Lex;
5309
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
5310 5311 5312 5313
	       lex->sql_command <= (int)SQLCOM_HA_READ) || lex->sql_command == (int)SQLCOM_KILL)	  {	
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
unknown's avatar
unknown committed
5314 5315 5316
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
5317 5318

subselect_end:
unknown's avatar
unknown committed
5319 5320 5321
	')'
	{
	  LEX *lex=Lex;
5322
	  lex->current_select = lex->current_select->outer_select();
unknown's avatar
unknown committed
5323
	};