sql_yacc.yy 112 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000-2001 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"
unknown's avatar
unknown committed
38
#include <myisam.h>
39
#include <myisammrg.h>
40

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

unknown's avatar
unknown committed
44
#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
45

46
inline Item *or_or_concat(THD *thd, Item* A, Item* B)
unknown's avatar
unknown committed
47
{
48
  return (thd->sql_mode & MODE_PIPES_AS_CONCAT ?
unknown's avatar
unknown committed
49 50 51 52 53 54 55
          (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
56
  ulonglong ulonglong_number;
unknown's avatar
unknown committed
57 58 59 60 61 62 63 64
  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
65 66 67 68 69 70
  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
71
  Key::Keytype key_type;
unknown's avatar
unknown committed
72
  enum ha_key_alg  key_alg;
unknown's avatar
unknown committed
73 74
  enum db_type db_type;
  enum row_type row_type;
unknown's avatar
unknown committed
75
  enum ha_rkey_function ha_rkey_mode;
unknown's avatar
unknown committed
76
  enum enum_tx_isolation tx_isolation;
unknown's avatar
unknown committed
77
  enum Item_cast cast_type;
unknown's avatar
unknown committed
78
  enum Item_udftype udf_type;
79
  CHARSET_INFO *charset;
unknown's avatar
unknown committed
80
  thr_lock_type lock_type;
unknown's avatar
unknown committed
81
  interval_type interval;
unknown's avatar
unknown committed
82
  st_select_lex *select_lex;
unknown's avatar
unknown committed
83
  chooser_compare_func_creator boolfunc2creator;
unknown's avatar
unknown committed
84 85 86 87 88 89 90 91 92 93
}

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

%pure_parser					/* We have threads */

%token	END_OF_INPUT

94 95 96 97 98 99
%token CLOSE_SYM
%token HANDLER_SYM
%token LAST_SYM
%token NEXT_SYM
%token PREV_SYM

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

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

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

unknown's avatar
unknown committed
381 382 383
%token  ERRORS
%token  WARNINGS

unknown's avatar
unknown committed
384 385 386
%token	BIGINT
%token	BLOB_SYM
%token	CHAR_SYM
unknown's avatar
unknown committed
387
%token	CHANGED
unknown's avatar
unknown committed
388 389 390 391 392 393
%token	COALESCE
%token	DATETIME
%token	DATE_SYM
%token	DECIMAL_SYM
%token	DOUBLE_SYM
%token	ENUM
394
%token	FAST_SYM
unknown's avatar
unknown committed
395
%token	FLOAT_SYM
unknown's avatar
unknown committed
396
%token  GEOMETRY_SYM
unknown's avatar
unknown committed
397 398 399 400 401 402 403 404 405
%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
406
%token	QUICK
unknown's avatar
unknown committed
407
%token	REAL
unknown's avatar
unknown committed
408
%token	SIGNED_SYM
unknown's avatar
unknown committed
409 410 411 412 413 414 415 416
%token	SMALLINT
%token	STRING_SYM
%token	TEXT_SYM
%token	TIMESTAMP
%token	TIME_SYM
%token	TINYBLOB
%token	TINYINT
%token	TINYTEXT
unknown's avatar
unknown committed
417
%token	ULONGLONG_NUM
unknown's avatar
unknown committed
418
%token	UNSIGNED
419 420 421
%token	VARBINARY
%token	VARCHAR
%token	VARYING
unknown's avatar
unknown committed
422 423
%token	ZEROFILL

unknown's avatar
unknown committed
424
%token	AGAINST
unknown's avatar
unknown committed
425 426 427 428 429 430
%token	ATAN
%token	BETWEEN_SYM
%token	BIT_AND
%token	BIT_OR
%token	CASE_SYM
%token	CONCAT
unknown's avatar
unknown committed
431
%token	CONCAT_WS
unknown's avatar
unknown committed
432 433 434 435 436 437 438 439 440 441
%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
442 443
%token	DES_ENCRYPT_SYM
%token	DES_DECRYPT_SYM
unknown's avatar
unknown committed
444 445 446 447 448 449 450 451 452 453
%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
454 455
%token	GEOMCOLLFROMTEXT
%token	GEOMFROMTEXT
unknown's avatar
unknown committed
456
%token  GEOMETRYCOLLECTION
unknown's avatar
unknown committed
457 458 459 460 461 462
%token	GROUP_UNIQUE_USERS
%token	HOUR_MINUTE_SYM
%token	HOUR_SECOND_SYM
%token	HOUR_SYM
%token	IDENTIFIED_SYM
%token	IF
463
%token	INSERT_METHOD
unknown's avatar
unknown committed
464 465 466
%token	INTERVAL_SYM
%token	LAST_INSERT_ID
%token	LEFT
unknown's avatar
unknown committed
467
%token	LINEFROMTEXT
unknown's avatar
unknown committed
468
%token  LINESTRING
unknown's avatar
unknown committed
469 470 471 472
%token	LOCATE
%token	MAKE_SET_SYM
%token	MINUTE_SECOND_SYM
%token	MINUTE_SYM
unknown's avatar
unknown committed
473
%token	MODE_SYM
unknown's avatar
unknown committed
474 475
%token	MODIFY_SYM
%token	MONTH_SYM
unknown's avatar
unknown committed
476 477 478
%token	MLINEFROMTEXT
%token	MPOINTFROMTEXT
%token	MPOLYFROMTEXT
unknown's avatar
unknown committed
479 480 481
%token  MULTILINESTRING
%token  MULTIPOINT
%token  MULTIPOLYGON
unknown's avatar
unknown committed
482 483
%token	NOW_SYM
%token	PASSWORD
unknown's avatar
unknown committed
484 485
%token	POINTFROMTEXT
%token	POLYFROMTEXT
unknown's avatar
unknown committed
486
%token  POLYGON
unknown's avatar
unknown committed
487 488 489 490 491 492 493
%token	POSITION_SYM
%token	PROCEDURE
%token	RAND
%token	REPLACE
%token	RIGHT
%token	ROUND
%token	SECOND_SYM
unknown's avatar
unknown committed
494
%token	SHARE_SYM
unknown's avatar
unknown committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508
%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
509
%token	WORK_SYM
unknown's avatar
unknown committed
510 511 512
%token	YEAR_MONTH_SYM
%token	YEAR_SYM
%token	YEARWEEK
unknown's avatar
unknown committed
513 514 515
%token	BENCHMARK_SYM
%token	END
%token	THEN_SYM
unknown's avatar
unknown committed
516

unknown's avatar
unknown committed
517 518 519 520
%token	SQL_BIG_RESULT
%token	SQL_CACHE_SYM
%token	SQL_CALC_FOUND_ROWS
%token	SQL_NO_CACHE_SYM
unknown's avatar
unknown committed
521
%token	SQL_SMALL_RESULT
unknown's avatar
unknown committed
522
%token  SQL_BUFFER_RESULT
unknown's avatar
unknown committed
523

unknown's avatar
unknown committed
524 525
%token  ISSUER_SYM
%token  SUBJECT_SYM
526
%token  CIPHER_SYM
unknown's avatar
unknown committed
527

unknown's avatar
unknown committed
528 529
%token  HELP

unknown's avatar
unknown committed
530 531 532 533 534 535 536 537 538
%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	'-' '+'
539
%left	'*' '/' '%' DIV_SYM MOD_SYM
unknown's avatar
unknown committed
540
%left	NEG '~'
unknown's avatar
unknown committed
541 542
%left   XOR
%left   '^'
unknown's avatar
unknown committed
543
%right	NOT
unknown's avatar
unknown committed
544
%right	BINARY COLLATE_SYM
545 546 547 548 549 550
/* 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
551 552
%type <lex_str>
	IDENT TEXT_STRING REAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM LEX_HOSTNAME
553
	ULONGLONG_NUM field_ident select_alias ident ident_or_text
unknown's avatar
unknown committed
554
        UNDERSCORE_CHARSET
unknown's avatar
unknown committed
555 556 557 558 559

%type <lex_str_ptr>
	opt_table_alias

%type <table>
560
	table_ident references
unknown's avatar
unknown committed
561 562

%type <simple_string>
563
	remember_name remember_end opt_ident opt_db text_or_password
unknown's avatar
unknown committed
564 565 566
	opt_escape

%type <string>
567
	text_string
unknown's avatar
unknown committed
568 569

%type <num>
unknown's avatar
unknown committed
570
	type int_type real_type order_dir opt_field_spec lock_option
unknown's avatar
unknown committed
571
	udf_type if_exists opt_local opt_table_options table_options
unknown's avatar
unknown committed
572
	table_option opt_if_not_exists opt_var_type opt_var_ident_type
unknown's avatar
unknown committed
573
	delete_option opt_temporary all_or_any
unknown's avatar
unknown committed
574 575

%type <ulong_num>
576
	ULONG_NUM raid_types merge_insert_types
unknown's avatar
unknown committed
577

unknown's avatar
unknown committed
578 579
%type <ulonglong_number>
	ulonglong_num
unknown's avatar
unknown committed
580

unknown's avatar
unknown committed
581 582 583
%type <lock_type>
	replace_lock_option opt_low_priority insert_lock_option load_data_lock

unknown's avatar
unknown committed
584
%type <item>
unknown's avatar
unknown committed
585
	literal text_literal insert_ident order_ident
unknown's avatar
unknown committed
586 587
	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
unknown's avatar
unknown committed
588
	using_list expr_or_default set_expr_or_default
unknown's avatar
unknown committed
589
	param_marker singleval_subselect singleval_subselect_init
unknown's avatar
unknown committed
590
	exists_subselect exists_subselect_init
unknown's avatar
unknown committed
591 592

%type <item_list>
unknown's avatar
unknown committed
593
	expr_list udf_expr_list when_list ident_list ident_list_arg
unknown's avatar
unknown committed
594 595

%type <key_type>
596
	key_type opt_unique_or_fulltext
unknown's avatar
unknown committed
597

unknown's avatar
unknown committed
598 599 600
%type <key_alg>
	key_alg opt_btree_or_rtree

unknown's avatar
unknown committed
601 602 603 604 605 606 607
%type <string_list>
	key_usage_list

%type <key_part>
	key_part

%type <table_list>
608
	join_table_list  join_table
unknown's avatar
unknown committed
609 610 611 612 613 614 615 616 617 618 619

%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
620
%type <tx_isolation> isolation_types
unknown's avatar
unknown committed
621

unknown's avatar
unknown committed
622 623
%type <ha_rkey_mode> handler_rkey_mode

unknown's avatar
unknown committed
624 625
%type <cast_type> cast_type

unknown's avatar
unknown committed
626 627 628 629 630 631
%type <udf_type> udf_func_type

%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword

%type <lex_user> user grant_user

632
%type <charset>
633 634 635 636
	charset_name
	charset_name_or_default
	opt_db_default_character_set

unknown's avatar
unknown committed
637 638
%type <variable> internal_variable_name

unknown's avatar
unknown committed
639 640
%type <select_lex> in_subselect in_subselect_init

unknown's avatar
unknown committed
641 642
%type <boolfunc2creator> comp_op

unknown's avatar
unknown committed
643
%type <NONE>
unknown's avatar
unknown committed
644
	query verb_clause create change select do drop insert replace insert2
645 646
	insert_values update delete truncate rename
	show describe load alter optimize flush
unknown's avatar
unknown committed
647
	reset purge begin commit rollback slave master_def master_defs
648
	repair restore backup analyze check
649
	field_list field_list_item field_spec kill
unknown's avatar
unknown committed
650
	select_item_list select_item values_list no_braces
651
	opt_limit_clause delete_limit_clause fields opt_values values
unknown's avatar
unknown committed
652
	procedure_list procedure_list2 procedure_item
653
	when_list2 expr_list2  handler
unknown's avatar
unknown committed
654 655 656
	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
657
	ref_list opt_on_delete opt_on_delete_list opt_on_delete_item use
658
	opt_delete_options opt_delete_option
unknown's avatar
unknown committed
659
	opt_outer table_list table_name opt_option opt_place
unknown's avatar
unknown committed
660 661
	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
662
	grant_privilege grant_privilege_list
unknown's avatar
unknown committed
663
	flush_options flush_option
unknown's avatar
unknown committed
664 665
	equal optional_braces opt_key_definition key_usage_list2
	opt_mi_check_type opt_to mi_check_types normal_join
666
	table_to_table_list table_to_table opt_table_list opt_as
unknown's avatar
unknown committed
667
	handler_rkey_function handler_read_or_scan
unknown's avatar
unknown committed
668
	single_multi table_wild_list table_wild_one opt_wild
669
	union_clause union_list union_option
670
	precision subselect_start opt_and
671
	subselect_end select_var_list select_var_list_init help opt_len
672
END_OF_INPUT
unknown's avatar
unknown committed
673 674 675

%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
676 677
	',' '!' '{' '}' '&' '|' AND OR OR_OR_CONCAT BETWEEN_SYM CASE_SYM
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM
unknown's avatar
unknown committed
678 679 680 681 682
%%


query:
	END_OF_INPUT
unknown's avatar
unknown committed
683
	{
684
	   THD *thd= YYTHD;
685
	   if (!thd->bootstrap &&
unknown's avatar
unknown committed
686
	      (!(thd->lex.select_lex.options & OPTION_FOUND_COMMENT)))
687
	   {
688
	     send_error(thd,ER_EMPTY_QUERY);
689
	     YYABORT;
690
	   }
691 692 693 694
	   else
	   {
	     thd->lex.sql_command = SQLCOM_EMPTY_QUERY;
	   }
unknown's avatar
unknown committed
695
	}
unknown's avatar
unknown committed
696
	| verb_clause END_OF_INPUT {};
unknown's avatar
unknown committed
697 698 699 700

verb_clause:
	  alter
	| analyze
701
	| backup
unknown's avatar
unknown committed
702 703 704 705 706 707 708
	| begin
	| change
	| check
	| commit
	| create
	| delete
	| describe
unknown's avatar
unknown committed
709
	| do
unknown's avatar
unknown committed
710 711 712 713 714 715 716 717
	| drop
	| grant
	| insert
	| flush
	| load
	| lock
	| kill
	| optimize
718
	| purge
719
	| rename
unknown's avatar
unknown committed
720 721
        | repair
	| replace
unknown's avatar
unknown committed
722
	| reset
723
	| restore
unknown's avatar
unknown committed
724 725 726 727
	| revoke
	| rollback
	| select
	| set
unknown's avatar
unknown committed
728
	| slave
unknown's avatar
unknown committed
729
	| show
730
	| truncate
731
	| handler
unknown's avatar
unknown committed
732 733
	| unlock
	| update
unknown's avatar
unknown committed
734 735
	| use
	| help;
736

unknown's avatar
unknown committed
737 738
/* help */

739
help:
unknown's avatar
unknown committed
740 741 742 743 744
       HELP TEXT_STRING
       {
       LEX *lex= Lex;
       lex->sql_command= SQLCOM_HELP;
       lex->help_arg= $2.str;
745
       };
unknown's avatar
unknown committed
746 747 748 749

/* change master */

change:
unknown's avatar
unknown committed
750
       CHANGE MASTER_SYM TO_SYM
unknown's avatar
unknown committed
751 752 753
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
unknown's avatar
unknown committed
754
	  bzero((char*) &lex->mi, sizeof(lex->mi));
755 756 757 758
        }
       master_defs
	{}
       ;
unknown's avatar
unknown committed
759 760 761

master_defs:
       master_def
unknown's avatar
unknown committed
762
       | master_defs ',' master_def;
unknown's avatar
unknown committed
763

unknown's avatar
unknown committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
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
785
       MASTER_PORT_SYM EQ ULONG_NUM
unknown's avatar
unknown committed
786 787 788 789
       {
	 Lex->mi.port = $3;
       }
       |
unknown's avatar
unknown committed
790
       MASTER_LOG_POS_SYM EQ ulonglong_num
unknown's avatar
unknown committed
791 792 793 794
       {
	 Lex->mi.pos = $3;
       }
       |
unknown's avatar
unknown committed
795
       MASTER_CONNECT_RETRY_SYM EQ ULONG_NUM
unknown's avatar
unknown committed
796 797 798
       {
	 Lex->mi.connect_retry = $3;
       }
799 800 801 802 803 804 805 806 807
       |
       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;
808 809
       }
       ;
unknown's avatar
unknown committed
810

unknown's avatar
unknown committed
811

unknown's avatar
unknown committed
812 813 814 815 816
/* create a table */

create:
	CREATE opt_table_options TABLE_SYM opt_if_not_exists table_ident
	{
817
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
818 819
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_CREATE_TABLE;
unknown's avatar
unknown committed
820
	  if (!lex->select_lex.add_table_to_list(thd,$5,
821
						 ($2 &
822 823
						  HA_LEX_CREATE_TMP_TABLE ?
						  &tmp_table_alias :
unknown's avatar
unknown committed
824 825 826 827
						  (LEX_STRING*) 0),1,
						 ((using_update_log)?
						  TL_READ_NO_INSERT:
						  TL_READ)))
unknown's avatar
unknown committed
828 829 830 831 832 833 834
	    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
835
	  lex->create_info.db_type= (enum db_type) lex->thd->variables.table_type;
836
	  lex->create_info.table_charset=thd->db_charset?thd->db_charset:default_charset_info;
unknown's avatar
unknown committed
837 838
	}
	create2
unknown's avatar
unknown committed
839
	{}
unknown's avatar
unknown committed
840
	| CREATE opt_unique_or_fulltext INDEX ident key_alg ON table_ident
unknown's avatar
unknown committed
841
	  {
unknown's avatar
unknown committed
842 843
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_CREATE_INDEX;
unknown's avatar
unknown committed
844
	    if (!lex->current_select->add_table_to_list(lex->thd, $7,NULL,1))
unknown's avatar
unknown committed
845
	      YYABORT;
unknown's avatar
unknown committed
846 847 848 849
	    lex->create_list.empty();
	    lex->key_list.empty();
	    lex->col_list.empty();
	    lex->change=NullS;
unknown's avatar
unknown committed
850
	  }
851
	   '(' key_list ')'
unknown's avatar
unknown committed
852
	  {
unknown's avatar
unknown committed
853
	    LEX *lex=Lex;
unknown's avatar
unknown committed
854

855
	    lex->key_list.push_back(new Key($2,$4.str, $5, lex->col_list));
unknown's avatar
unknown committed
856
	    lex->col_list.empty();
unknown's avatar
unknown committed
857
	  }
858
	| CREATE DATABASE opt_if_not_exists ident opt_db_default_character_set
unknown's avatar
unknown committed
859
	  {
unknown's avatar
unknown committed
860 861 862 863
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_CREATE_DB;
	    lex->name=$4.str;
            lex->create_info.options=$3;
864
	    lex->create_info.table_charset=$5;
unknown's avatar
unknown committed
865 866 867
	  }
	| CREATE udf_func_type UDF_SYM ident
	  {
unknown's avatar
unknown committed
868 869 870 871 872
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_CREATE_FUNCTION;
	    lex->udf.name=$4.str;
	    lex->udf.name_length=$4.length;
	    lex->udf.type= $2;
unknown's avatar
unknown committed
873 874 875
	  }
	  UDF_RETURNS_SYM udf_type UDF_SONAME_SYM TEXT_STRING
	  {
unknown's avatar
unknown committed
876 877 878
	    LEX *lex=Lex;
	    lex->udf.returns=(Item_result) $7;
	    lex->udf.dl=$9.str;
879 880
	  }
          ;
unknown's avatar
unknown committed
881 882

create2:
unknown's avatar
unknown committed
883
	'(' field_list ')' opt_create_table_options create3 {}
884 885
	| opt_create_table_options create3 {}
        ;
unknown's avatar
unknown committed
886 887

create3:
888 889
	/* empty */ {}
	| opt_duplicate opt_as SELECT_SYM
unknown's avatar
unknown committed
890
          {
unknown's avatar
unknown committed
891 892 893
	    LEX *lex=Lex;
	    lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
	    mysql_init_select(lex);
unknown's avatar
unknown committed
894
          }
895
          select_options select_item_list opt_select_from union_clause {}
896
          ;
unknown's avatar
unknown committed
897

898 899
opt_as:
	/* empty */ {}
unknown's avatar
unknown committed
900
	| AS	    {};
901

unknown's avatar
unknown committed
902
opt_table_options:
unknown's avatar
unknown committed
903
	/* empty */	 { $$= 0; }
unknown's avatar
unknown committed
904
	| table_options  { $$= $1;};
unknown's avatar
unknown committed
905 906 907

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

unknown's avatar
unknown committed
910
table_option:
unknown's avatar
unknown committed
911
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
unknown's avatar
unknown committed
912 913

opt_if_not_exists:
unknown's avatar
unknown committed
914
	/* empty */	 { $$= 0; }
unknown's avatar
unknown committed
915
	| IF NOT EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
unknown's avatar
unknown committed
916 917 918

opt_create_table_options:
	/* empty */
unknown's avatar
unknown committed
919
	| create_table_options;
unknown's avatar
unknown committed
920

unknown's avatar
unknown committed
921 922 923 924
create_table_options_space_separated:
	create_table_option
	| create_table_option create_table_options_space_separated;

unknown's avatar
unknown committed
925 926
create_table_options:
	create_table_option
927
	| create_table_option     create_table_options
unknown's avatar
unknown committed
928
	| create_table_option ',' create_table_options;
unknown's avatar
unknown committed
929 930

create_table_option:
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
	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 ')'
947 948 949
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
950 951
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
952 953
	    lex->create_info.merge_list.elements--;
	    lex->create_info.merge_list.first= (byte*) (table_list->next);
954 955
	    lex->select_lex.table_list.elements=1;
	    lex->select_lex.table_list.next= (byte**) &(table_list->next);
956
	    table_list->next=0;
957
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
958
	  }
959
	| opt_default CHARSET opt_equal charset_name_or_default
960
	  {
961 962 963
	    Lex->create_info.table_charset= $4;
	    Lex->create_info.used_fields|= HA_CREATE_USED_CHARSET;
	  }
964
	| opt_default CHAR_SYM SET opt_equal charset_name_or_default
965
	  {
966
	    Lex->create_info.table_charset= $5;
967 968
	    Lex->create_info.used_fields|= HA_CREATE_USED_CHARSET;
	  }
969 970 971
	| 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
972 973 974 975 976 977 978

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
979
	| INNOBASE_SYM  { $$= DB_TYPE_INNODB; };
unknown's avatar
unknown committed
980 981 982 983 984

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
unknown's avatar
unknown committed
985
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; };
unknown's avatar
unknown committed
986 987 988 989

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

992 993 994
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
unknown's avatar
unknown committed
995
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
996

unknown's avatar
unknown committed
997
opt_select_from:
998
	opt_limit_clause {}
unknown's avatar
unknown committed
999
	| select_from select_lock_type;
unknown's avatar
unknown committed
1000 1001 1002

udf_func_type:
	/* empty */ 	{ $$ = UDFTYPE_FUNCTION; }
unknown's avatar
unknown committed
1003
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
unknown's avatar
unknown committed
1004 1005 1006 1007

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
unknown's avatar
unknown committed
1008
	| INT_SYM {$$ = (int) INT_RESULT; };
unknown's avatar
unknown committed
1009 1010 1011

field_list:
	  field_list_item
unknown's avatar
unknown committed
1012
	| field_list ',' field_list_item;
unknown's avatar
unknown committed
1013 1014 1015


field_list_item:
1016
	  field_spec check_constraint
unknown's avatar
unknown committed
1017 1018 1019 1020
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
unknown's avatar
unknown committed
1021
	| key_type opt_ident key_alg '(' key_list ')'
unknown's avatar
unknown committed
1022
	  {
unknown's avatar
unknown committed
1023
	    LEX *lex=Lex;
1024
	    lex->key_list.push_back(new Key($1,$2, $3, lex->col_list));
unknown's avatar
unknown committed
1025
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
1026
	  }
1027
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
unknown's avatar
unknown committed
1028
	  {
1029 1030 1031 1032 1033 1034 1035 1036
	    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
1037
	  }
1038
	| opt_constraint check_constraint
unknown's avatar
unknown committed
1039 1040
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
1041 1042 1043 1044 1045 1046 1047
	  }
	;

check_constraint:
	/* empty */
	| CHECK_SYM expr
	;
unknown's avatar
unknown committed
1048 1049 1050

opt_constraint:
	/* empty */
unknown's avatar
unknown committed
1051
	| CONSTRAINT opt_ident;
unknown's avatar
unknown committed
1052 1053 1054 1055

field_spec:
	field_ident
	 {
unknown's avatar
unknown committed
1056 1057
	   LEX *lex=Lex;
	   lex->length=lex->dec=0; lex->type=0; lex->interval=0;
1058
	   lex->default_value=lex->comment=0;
1059
	   lex->charset=NULL;
unknown's avatar
unknown committed
1060 1061 1062
	 }
	type opt_attribute
	{
unknown's avatar
unknown committed
1063
	  LEX *lex=Lex;
unknown's avatar
unknown committed
1064
	  if (add_field_to_list(lex->thd, $1.str,
unknown's avatar
unknown committed
1065
				(enum enum_field_types) $3,
unknown's avatar
unknown committed
1066
				lex->length,lex->dec,lex->type,
1067
				lex->default_value, lex->comment,
1068
				lex->change,lex->interval,lex->charset))
unknown's avatar
unknown committed
1069
	    YYABORT;
unknown's avatar
unknown committed
1070
	};
unknown's avatar
unknown committed
1071 1072

type:
1073
	int_type opt_len field_options	{ $$=$1; }
unknown's avatar
unknown committed
1074 1075 1076 1077 1078 1079
	| 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; }
1080
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
unknown's avatar
unknown committed
1081 1082 1083
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
1084
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
1085
					  Lex->charset=my_charset_bin;
unknown's avatar
unknown committed
1086 1087 1088 1089
					  $$=FIELD_TYPE_STRING; }
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
					  $$=FIELD_TYPE_VAR_STRING; }
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
1090
					  Lex->charset=my_charset_bin;
unknown's avatar
unknown committed
1091
					  $$=FIELD_TYPE_VAR_STRING; }
1092
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
unknown's avatar
unknown committed
1093 1094
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
1095 1096
	| TIMESTAMP
	  {
1097
	    if (YYTHD->sql_mode & MODE_SAPDB)
1098 1099
	      $$=FIELD_TYPE_DATETIME;
	    else
1100
	      $$=FIELD_TYPE_TIMESTAMP;
1101
	   }
unknown's avatar
unknown committed
1102 1103 1104
	| TIMESTAMP '(' NUM ')'		{ Lex->length=$3.str;
					  $$=FIELD_TYPE_TIMESTAMP; }
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
1105
	| TINYBLOB			{ Lex->charset=my_charset_bin;
unknown's avatar
unknown committed
1106
					  $$=FIELD_TYPE_TINY_BLOB; }
1107
	| BLOB_SYM opt_len		{ Lex->charset=my_charset_bin;
unknown's avatar
unknown committed
1108
					  $$=FIELD_TYPE_BLOB; }
1109
	| GEOMETRY_SYM			{ Lex->charset=my_charset_bin;
unknown's avatar
unknown committed
1110
					  $$=FIELD_TYPE_GEOMETRY; }
1111
	| MEDIUMBLOB			{ Lex->charset=my_charset_bin;
unknown's avatar
unknown committed
1112
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
1113
	| LONGBLOB			{ Lex->charset=my_charset_bin;
unknown's avatar
unknown committed
1114
					  $$=FIELD_TYPE_LONG_BLOB; }
1115
	| LONG_SYM VARBINARY		{ Lex->charset=my_charset_bin;
unknown's avatar
unknown committed
1116
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
1117 1118
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
1119
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
1120 1121
	| MEDIUMTEXT opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| LONGTEXT opt_binary		{ $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
1122 1123 1124 1125
	| DECIMAL_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
	| NUMERIC_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
1126 1127
	| FIXED_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
1128
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
unknown's avatar
unknown committed
1129
	  {
unknown's avatar
unknown committed
1130 1131
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
unknown's avatar
unknown committed
1132 1133
	    $$=FIELD_TYPE_ENUM;
	  }
1134
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
unknown's avatar
unknown committed
1135
	  {
unknown's avatar
unknown committed
1136 1137
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
unknown's avatar
unknown committed
1138
	    $$=FIELD_TYPE_SET;
1139
	  }
1140
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
1141 1142 1143 1144 1145 1146
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
1147
	;
unknown's avatar
unknown committed
1148 1149 1150 1151

char:
	CHAR_SYM {}
	| NCHAR_SYM {}
unknown's avatar
unknown committed
1152
	| NATIONAL_SYM CHAR_SYM {};
unknown's avatar
unknown committed
1153

unknown's avatar
unknown committed
1154 1155 1156 1157
varchar:
	char VARYING {}
	| VARCHAR {}
	| NATIONAL_SYM VARCHAR {}
unknown's avatar
unknown committed
1158
	| NCHAR_SYM VARCHAR {};
unknown's avatar
unknown committed
1159 1160 1161 1162 1163 1164

int_type:
	INT_SYM		{ $$=FIELD_TYPE_LONG; }
	| TINYINT	{ $$=FIELD_TYPE_TINY; }
	| SMALLINT	{ $$=FIELD_TYPE_SHORT; }
	| MEDIUMINT	{ $$=FIELD_TYPE_INT24; }
unknown's avatar
unknown committed
1165
	| BIGINT	{ $$=FIELD_TYPE_LONGLONG; };
unknown's avatar
unknown committed
1166 1167

real_type:
1168
	REAL		{ $$= YYTHD->sql_mode & MODE_REAL_AS_FLOAT ?
unknown's avatar
unknown committed
1169 1170
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
unknown's avatar
unknown committed
1171
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
unknown's avatar
unknown committed
1172 1173 1174 1175 1176


float_options:
	/* empty */		{}
	| '(' NUM ')'		{ Lex->length=$2.str; }
unknown's avatar
unknown committed
1177
	| precision		{};
unknown's avatar
unknown committed
1178 1179 1180 1181 1182 1183

precision:
	'(' NUM ',' NUM ')'
	{
	  LEX *lex=Lex;
	  lex->length=$2.str; lex->dec=$4.str;
unknown's avatar
unknown committed
1184
	};
unknown's avatar
unknown committed
1185 1186 1187

field_options:
	/* empty */		{}
unknown's avatar
unknown committed
1188
	| field_opt_list	{};
unknown's avatar
unknown committed
1189 1190 1191

field_opt_list:
	field_opt_list field_option {}
unknown's avatar
unknown committed
1192
	| field_option {};
unknown's avatar
unknown committed
1193 1194

field_option:
unknown's avatar
unknown committed
1195
	SIGNED_SYM	{}
1196
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
unknown's avatar
unknown committed
1197
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
unknown's avatar
unknown committed
1198 1199

opt_len:
1200 1201
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
unknown's avatar
unknown committed
1202 1203 1204

opt_precision:
	/* empty */	{}
unknown's avatar
unknown committed
1205
	| precision	{};
unknown's avatar
unknown committed
1206 1207 1208

opt_attribute:
	/* empty */ {}
unknown's avatar
unknown committed
1209
	| opt_attribute_list {};
unknown's avatar
unknown committed
1210 1211 1212

opt_attribute_list:
	opt_attribute_list attribute {}
unknown's avatar
unknown committed
1213
	| attribute;
unknown's avatar
unknown committed
1214 1215 1216 1217 1218 1219

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; }
1220
	| SERIAL_SYM DEFAULT VALUE_SYM
unknown's avatar
unknown committed
1221 1222
	  { 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
1223
	| UNIQUE_SYM	  { Lex->type|= UNIQUE_FLAG; }
1224
	| UNIQUE_SYM KEY_SYM { Lex->type|= UNIQUE_KEY_FLAG; }
unknown's avatar
unknown committed
1225
	| COMMENT_SYM text_literal { Lex->comment= $2; };
unknown's avatar
unknown committed
1226

unknown's avatar
unknown committed
1227

1228
charset_name:
unknown's avatar
unknown committed
1229
	BINARY
1230
	{
unknown's avatar
unknown committed
1231 1232
	  if (!($$=get_charset_by_name("binary",MYF(0))))
	  {
1233
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,"binary");
unknown's avatar
unknown committed
1234 1235 1236
	    YYABORT;
	  }
	}
1237 1238
	| ident
	{
1239
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
1240
	  {
1241
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,$1.str);
1242 1243 1244 1245
	    YYABORT;
	  }
	};

1246 1247 1248
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
unknown's avatar
unknown committed
1249

1250 1251 1252 1253
opt_default:
	/* empty */	{}
	| DEFAULT	{};

1254
opt_db_default_character_set:
1255 1256 1257
	/* empty */	{ $$=default_charset_info; }
	| opt_default CHAR_SYM SET charset_name_or_default	{ $$=$4; }
	| opt_default CHARSET charset_name_or_default		{ $$=$3; };
unknown's avatar
unknown committed
1258 1259

opt_binary:
1260
	/* empty */			{ Lex->charset=NULL; }
1261
	| BYTE_SYM			{ Lex->charset=my_charset_bin; }
1262
	| BINARY			{ Lex->charset=my_charset_bin; }
unknown's avatar
unknown committed
1263 1264 1265 1266 1267 1268 1269 1270
	| UNICODE_SYM
	{
	  if (!(Lex->charset=get_charset_by_name("ucs2",MYF(0))))
	  {
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,"ucs2");
	    YYABORT;
	  }
	}
1271
	| CHAR_SYM SET charset_name	{ Lex->charset=$3; } ;
unknown's avatar
unknown committed
1272

unknown's avatar
unknown committed
1273 1274 1275 1276

opt_primary:
	/* empty */
	| PRIMARY_SYM
1277
	;
unknown's avatar
unknown committed
1278

unknown's avatar
unknown committed
1279
references:
1280 1281
	REFERENCES table_ident
	{
1282
	  LEX *lex=Lex;
1283 1284 1285 1286 1287 1288
	  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
1289
	};
1290

1291
opt_ref_list:
unknown's avatar
unknown committed
1292
	/* empty */ opt_on_delete {}
unknown's avatar
unknown committed
1293
	| '(' ref_list ')' opt_on_delete {};
1294 1295 1296

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

unknown's avatar
unknown committed
1299 1300 1301

opt_on_delete:
	/* empty */ {}
unknown's avatar
unknown committed
1302
	| opt_on_delete_list {};
unknown's avatar
unknown committed
1303 1304 1305

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
unknown's avatar
unknown committed
1306
	| opt_on_delete_item {};
unknown's avatar
unknown committed
1307 1308

opt_on_delete_item:
1309 1310 1311 1312
	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
1313
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
unknown's avatar
unknown committed
1314 1315

delete_option:
1316 1317 1318 1319
	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
1320
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
unknown's avatar
unknown committed
1321 1322 1323 1324

key_type:
	opt_constraint PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
	| key_or_index			    { $$= Key::MULTIPLE; }
1325 1326
	| FULLTEXT_SYM			    { $$= Key::FULLTEXT; }
	| FULLTEXT_SYM key_or_index	    { $$= Key::FULLTEXT; }
unknown's avatar
unknown committed
1327 1328
	| SPATIAL_SYM			    { $$= Key::SPATIAL; }
	| SPATIAL_SYM key_or_index	    { $$= Key::SPATIAL; }
unknown's avatar
unknown committed
1329
	| opt_constraint UNIQUE_SYM	    { $$= Key::UNIQUE; }
unknown's avatar
unknown committed
1330
	| opt_constraint UNIQUE_SYM key_or_index { $$= Key::UNIQUE; };
unknown's avatar
unknown committed
1331 1332 1333

key_or_index:
	KEY_SYM {}
unknown's avatar
unknown committed
1334
	| INDEX {};
unknown's avatar
unknown committed
1335 1336 1337 1338

keys_or_index:
	KEYS {}
	| INDEX {}
unknown's avatar
unknown committed
1339
	| INDEXES {};
unknown's avatar
unknown committed
1340

1341
opt_unique_or_fulltext:
unknown's avatar
unknown committed
1342 1343
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
unknown's avatar
unknown committed
1344
	| SPATIAL_SYM	{ $$= Key::SPATIAL; };
unknown's avatar
unknown committed
1345 1346

key_alg:
1347
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
unknown's avatar
unknown committed
1348
	| USING opt_btree_or_rtree { $$= $2; };
unknown's avatar
unknown committed
1349 1350 1351 1352

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
	| RTREE_SYM	{ $$= HA_KEY_ALG_RTREE; }
unknown's avatar
unknown committed
1353
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
unknown's avatar
unknown committed
1354 1355 1356

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
unknown's avatar
unknown committed
1357
	| key_part order_dir		{ Lex->col_list.push_back($1); };
unknown's avatar
unknown committed
1358 1359 1360

key_part:
	ident			{ $$=new key_part_spec($1.str); }
unknown's avatar
unknown committed
1361
	| ident '(' NUM ')'	{ $$=new key_part_spec($1.str,(uint) atoi($3.str)); };
unknown's avatar
unknown committed
1362 1363 1364

opt_ident:
	/* empty */	{ $$=(char*) 0; }	/* Defaultlength */
unknown's avatar
unknown committed
1365
	| field_ident	{ $$=$1.str; };
unknown's avatar
unknown committed
1366 1367 1368

string_list:
	text_string			{ Lex->interval_list.push_back($1); }
unknown's avatar
unknown committed
1369
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
unknown's avatar
unknown committed
1370 1371 1372 1373 1374 1375 1376 1377

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
1378
	  THD *thd= YYTHD;
1379
	  LEX *lex=&thd->lex;
unknown's avatar
unknown committed
1380 1381
	  lex->sql_command = SQLCOM_ALTER_TABLE;
	  lex->name=0;
unknown's avatar
unknown committed
1382
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,1))
unknown's avatar
unknown committed
1383 1384 1385 1386 1387 1388 1389
	    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();
1390
          lex->select_lex.init_order();
1391
	  lex->select_lex.db=lex->name=0;
1392
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
unknown's avatar
unknown committed
1393
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
1394
	  lex->create_info.table_charset=thd->db_charset?thd->db_charset:default_charset_info;
1395
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
1396 1397
          lex->alter_keys_onoff=LEAVE_AS_IS;
          lex->simple_alter=1;
unknown's avatar
unknown committed
1398
	}
1399 1400
	alter_list
	{}
1401
	| ALTER DATABASE ident opt_db_default_character_set
1402 1403 1404 1405
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
	    lex->name=$3.str;
1406 1407
	    lex->create_info.table_charset=$4;
	  };
1408 1409


unknown's avatar
unknown committed
1410
alter_list:
unknown's avatar
unknown committed
1411
        | alter_list_item
unknown's avatar
unknown committed
1412
	| alter_list ',' alter_list_item;
unknown's avatar
unknown committed
1413 1414

add_column:
unknown's avatar
unknown committed
1415
	ADD opt_column { Lex->change=0; };
unknown's avatar
unknown committed
1416 1417

alter_list_item:
1418 1419
	add_column field_list_item opt_place { Lex->simple_alter=0; }
	| add_column '(' field_list ')'      { Lex->simple_alter=0; }
unknown's avatar
unknown committed
1420 1421 1422 1423 1424
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
	     lex->change= $3.str; lex->simple_alter=0;
	  }
1425
          field_spec opt_place
unknown's avatar
unknown committed
1426 1427 1428 1429 1430
        | 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
1431
            lex->simple_alter=0;
unknown's avatar
unknown committed
1432 1433 1434 1435
          }
          type opt_attribute
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
1436
            if (add_field_to_list(lex->thd,$3.str,
unknown's avatar
unknown committed
1437 1438 1439
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
                                  lex->default_value, lex->comment,
1440
				  $3.str, lex->interval, lex->charset))
unknown's avatar
unknown committed
1441 1442 1443
	       YYABORT;
          }
          opt_place
unknown's avatar
unknown committed
1444
	| DROP opt_column field_ident opt_restrict
unknown's avatar
unknown committed
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
	  {
	    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;
	  }
1455
	| DROP FOREIGN KEY_SYM opt_ident { Lex->simple_alter=0; }
unknown's avatar
unknown committed
1456
	| DROP key_or_index field_ident
unknown's avatar
unknown committed
1457 1458 1459 1460 1461 1462
	  {
	    LEX *lex=Lex;
	    lex->drop_list.push_back(new Alter_drop(Alter_drop::KEY,
						    $3.str));
	    lex->simple_alter=0;
	  }
1463 1464
	| DISABLE_SYM KEYS { Lex->alter_keys_onoff=DISABLE; }
	| ENABLE_SYM KEYS  { Lex->alter_keys_onoff=ENABLE; }
unknown's avatar
unknown committed
1465
	| ALTER opt_column field_ident SET DEFAULT literal
unknown's avatar
unknown committed
1466 1467 1468 1469 1470
	  {
	    LEX *lex=Lex;
	    lex->alter_list.push_back(new Alter_column($3.str,$6));
	    lex->simple_alter=0;
	  }
unknown's avatar
unknown committed
1471
	| ALTER opt_column field_ident DROP DEFAULT
unknown's avatar
unknown committed
1472 1473 1474 1475 1476
	  {
	    LEX *lex=Lex;
	    lex->alter_list.push_back(new Alter_column($3.str,(Item*) 0));
	    lex->simple_alter=0;
	  }
1477
	| RENAME opt_to table_ident
1478
	  {
1479
	    LEX *lex=Lex;
1480
	    lex->select_lex.db=$3->db.str;
1481
	    lex->name= $3->table.str;
1482
	  }
unknown's avatar
unknown committed
1483
        | create_table_options_space_separated { Lex->simple_alter=0; }
unknown's avatar
unknown committed
1484
	| order_clause         { Lex->simple_alter=0; };
unknown's avatar
unknown committed
1485 1486 1487

opt_column:
	/* empty */	{}
unknown's avatar
unknown committed
1488
	| COLUMN_SYM	{};
unknown's avatar
unknown committed
1489 1490 1491

opt_ignore:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
unknown's avatar
unknown committed
1492
	| IGNORE_SYM	{ Lex->duplicates=DUP_IGNORE; };
unknown's avatar
unknown committed
1493 1494 1495 1496

opt_restrict:
	/* empty */	{}
	| RESTRICT	{}
unknown's avatar
unknown committed
1497
	| CASCADE	{};
unknown's avatar
unknown committed
1498 1499 1500 1501

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
unknown's avatar
unknown committed
1502
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
unknown's avatar
unknown committed
1503 1504 1505 1506

opt_to:
	/* empty */	{}
	| TO_SYM	{}
1507
	| EQ		{}
unknown's avatar
unknown committed
1508
	| AS		{};
unknown's avatar
unknown committed
1509

unknown's avatar
unknown committed
1510
/*
unknown's avatar
unknown committed
1511 1512 1513
  The first two deprecate the last two--delete the last two for 4.1 release
*/

unknown's avatar
unknown committed
1514
slave:
1515
	START_SYM SLAVE slave_thread_opts
unknown's avatar
unknown committed
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
        {
	  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
1528

unknown's avatar
unknown committed
1529 1530 1531
slave_thread_opts:
	slave_thread_opt
	| slave_thread_opts ',' slave_thread_opt;
1532 1533

slave_thread_opt:
1534
	/*empty*/	{}
unknown's avatar
unknown committed
1535 1536 1537
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
	| IO_THREAD   	{ Lex->slave_thd_opt|=SLAVE_IO; }
	;
1538

unknown's avatar
unknown committed
1539 1540 1541 1542 1543 1544 1545 1546
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
1547
        };
unknown's avatar
unknown committed
1548

unknown's avatar
unknown committed
1549 1550 1551 1552 1553 1554 1555 1556
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
1557
        };
unknown's avatar
unknown committed
1558

unknown's avatar
unknown committed
1559 1560 1561
repair:
	REPAIR table_or_tables
	{
unknown's avatar
unknown committed
1562 1563 1564
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
	   lex->check_opt.init();
unknown's avatar
unknown committed
1565
	}
1566 1567 1568
	table_list opt_mi_repair_type
	{}
	;
unknown's avatar
unknown committed
1569

unknown's avatar
unknown committed
1570
opt_mi_repair_type:
unknown's avatar
unknown committed
1571
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
1572
	| mi_repair_types {};
unknown's avatar
unknown committed
1573

unknown's avatar
unknown committed
1574 1575
mi_repair_types:
	mi_repair_type {}
unknown's avatar
unknown committed
1576
	| mi_repair_type mi_repair_types {};
1577

unknown's avatar
unknown committed
1578
mi_repair_type:
unknown's avatar
unknown committed
1579
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
1580
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
unknown's avatar
unknown committed
1581
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
unknown's avatar
unknown committed
1582 1583

analyze:
1584
	ANALYZE_SYM table_or_tables
unknown's avatar
unknown committed
1585
	{
unknown's avatar
unknown committed
1586 1587 1588
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
	   lex->check_opt.init();
unknown's avatar
unknown committed
1589
	}
1590 1591 1592
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
1593 1594

check:
unknown's avatar
unknown committed
1595
	CHECK_SYM table_or_tables
unknown's avatar
unknown committed
1596
	{
unknown's avatar
unknown committed
1597 1598 1599
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECK;
	   lex->check_opt.init();
unknown's avatar
unknown committed
1600
	}
1601 1602 1603
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
1604

unknown's avatar
unknown committed
1605 1606
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
1607
	| mi_check_types {};
unknown's avatar
unknown committed
1608 1609 1610

mi_check_types:
	mi_check_type {}
unknown's avatar
unknown committed
1611
	| mi_check_type mi_check_types {};
unknown's avatar
unknown committed
1612 1613 1614 1615 1616 1617

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
1618
	| CHANGED  { Lex->check_opt.flags|= T_CHECK_ONLY_CHANGED; };
unknown's avatar
unknown committed
1619

unknown's avatar
unknown committed
1620
optimize:
1621
	OPTIMIZE table_or_tables
unknown's avatar
unknown committed
1622
	{
unknown's avatar
unknown committed
1623 1624 1625
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
	   lex->check_opt.init();
unknown's avatar
unknown committed
1626
	}
1627 1628 1629
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
1630

1631 1632 1633 1634 1635
rename:
	RENAME table_or_tables
	{
	   Lex->sql_command=SQLCOM_RENAME_TABLE;
	}
1636 1637 1638
	table_to_table_list
	{}
	;
1639 1640 1641

table_to_table_list:
	table_to_table
unknown's avatar
unknown committed
1642
	| table_to_table_list ',' table_to_table;
1643 1644 1645

table_to_table:
	table_ident TO_SYM table_ident
1646
	{
unknown's avatar
unknown committed
1647 1648 1649 1650
	  LEX *lex=Lex;	  
	  SELECT_LEX_NODE *sl= lex->current_select;
	  if (!sl->add_table_to_list(lex->thd, $1,NULL,1,TL_IGNORE) ||
	      !sl->add_table_to_list(lex->thd, $3,NULL,1,TL_IGNORE))
1651
	    YYABORT;
1652
	};
1653

unknown's avatar
unknown committed
1654
/*
unknown's avatar
unknown committed
1655
  Select : retrieve data from table
unknown's avatar
unknown committed
1656 1657 1658 1659
*/


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

1662
/* Need select_init2 for subselects. */
unknown's avatar
unknown committed
1663
select_init:
1664
	SELECT_SYM select_init2
1665
	|
1666 1667
	'(' SELECT_SYM select_part2 ')'
	  {
1668 1669
	    LEX *lex= Lex;
            SELECT_LEX_NODE * sel= lex->current_select;
unknown's avatar
unknown committed
1670
	    if (sel->set_braces(1))
1671 1672 1673 1674
	    {
	      send_error(lex->thd, ER_SYNTAX_ERROR);
	      YYABORT;
	    }
1675
            /* select in braces, can't contain global parameters */
1676
            sel->master_unit()->global_parameters=
1677
               sel->master_unit();
unknown's avatar
unknown committed
1678
          } union_opt;
1679

1680 1681
select_init2:
	select_part2
1682
        {
1683
	  LEX *lex= Lex;
unknown's avatar
unknown committed
1684
          if (lex->current_select->set_braces(0))
1685 1686 1687 1688 1689
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
	}
1690
	union_clause
1691 1692
	;

1693
select_part2:
unknown's avatar
unknown committed
1694 1695
	{
	  LEX *lex=Lex;
unknown's avatar
unknown committed
1696
	  if (lex->current_select == &lex->select_lex)
1697 1698
	    lex->lock_option= TL_READ; /* Only for global SELECT */
	  mysql_init_select(lex);
unknown's avatar
unknown committed
1699
	}
unknown's avatar
unknown committed
1700
	select_options select_item_list select_into select_lock_type;
1701

unknown's avatar
unknown committed
1702
select_into:
1703 1704 1705 1706 1707 1708
	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
1709
	| select_from
1710 1711
	| into select_from
	| select_from into;
unknown's avatar
unknown committed
1712 1713

select_from:
1714
	FROM join_table_list where_clause group_clause having_clause opt_order_clause opt_limit_clause procedure_clause;
unknown's avatar
unknown committed
1715 1716 1717

select_options:
	/* empty*/
unknown's avatar
unknown committed
1718
	| select_option_list;
unknown's avatar
unknown committed
1719 1720 1721

select_option_list:
	select_option_list select_option
unknown's avatar
unknown committed
1722
	| select_option;
unknown's avatar
unknown committed
1723 1724

select_option:
1725
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
1726 1727 1728 1729 1730 1731
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
1732 1733 1734
	| DISTINCT	{ Select->options|= SELECT_DISTINCT; }
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
	| 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;
	  }
1747
	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
1748 1749 1750
	| SQL_CACHE_SYM    { Select->options|= OPTION_TO_QUERY_CACHE; }
	| ALL		{}
	;
unknown's avatar
unknown committed
1751

unknown's avatar
unknown committed
1752 1753 1754
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
1755 1756
	  {
	    LEX *lex=Lex;
1757
	    lex->current_select->set_lock_for_tables(TL_WRITE);
1758
	    lex->safe_to_cache_query=0;
1759
	  }
unknown's avatar
unknown committed
1760
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
1761 1762
	  {
	    LEX *lex=Lex;
1763 1764
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
1765
	    lex->safe_to_cache_query=0;
1766 1767
	  }
	;
unknown's avatar
unknown committed
1768

unknown's avatar
unknown committed
1769 1770 1771 1772 1773
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
unknown's avatar
unknown committed
1774
	    if (add_item_to_list(YYTHD, new Item_field(NULL,NULL,"*")))
unknown's avatar
unknown committed
1775
	      YYABORT;
unknown's avatar
unknown committed
1776
	  };
unknown's avatar
unknown committed
1777 1778 1779 1780 1781


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
unknown's avatar
unknown committed
1782
	    if (add_item_to_list(YYTHD, $2))
unknown's avatar
unknown committed
1783 1784 1785 1786 1787
	      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
1788
	  };
unknown's avatar
unknown committed
1789 1790

remember_name:
unknown's avatar
unknown committed
1791
	{ $$=(char*) Lex->tok_start; };
unknown's avatar
unknown committed
1792 1793

remember_end:
unknown's avatar
unknown committed
1794
	{ $$=(char*) Lex->tok_end; };
unknown's avatar
unknown committed
1795 1796 1797

select_item2:
	table_wild	{ $$=$1; } /* table.* */
unknown's avatar
unknown committed
1798
	| expr		{ $$=$1; };
unknown's avatar
unknown committed
1799 1800 1801 1802 1803 1804

select_alias:
	{ $$.str=0;}
	| AS ident { $$=$2; }
	| AS TEXT_STRING  { $$=$2; }
	| ident { $$=$1; }
unknown's avatar
unknown committed
1805
	| TEXT_STRING  { $$=$1; };
unknown's avatar
unknown committed
1806 1807 1808

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

unknown's avatar
unknown committed
1811
/* all possible expressions */
1812
expr:	expr_expr	{ $$= $1; }
unknown's avatar
unknown committed
1813
	| simple_expr	{ $$= $1; };
unknown's avatar
unknown committed
1814

unknown's avatar
unknown committed
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
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
1827 1828
/* expressions that begin with 'expr' */
expr_expr:
unknown's avatar
unknown committed
1829
	  expr IN_SYM '(' expr_list ')'
unknown's avatar
unknown committed
1830 1831 1832
	  { $$= 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
1833
        | expr IN_SYM in_subselect
unknown's avatar
unknown committed
1834
          { $$= new Item_in_subselect(YYTHD, $1, $3); }
unknown's avatar
unknown committed
1835
	| expr NOT IN_SYM in_subselect
1836
          {
unknown's avatar
unknown committed
1837
            $$= new Item_func_not(new Item_in_subselect(YYTHD, $1, $4));
unknown's avatar
unknown committed
1838
          }
unknown's avatar
unknown committed
1839 1840 1841 1842
	| 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)); }
1843
	| expr OR_OR_CONCAT expr { $$= or_or_concat(YYTHD, $1,$3); }
unknown's avatar
unknown committed
1844
	| expr OR expr		{ $$= new Item_cond_or($1,$3); }
unknown's avatar
unknown committed
1845
        | expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
unknown's avatar
unknown committed
1846
	| expr AND expr		{ $$= new Item_cond_and($1,$3); }
unknown's avatar
unknown committed
1847
	| 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
1848 1849 1850 1851 1852 1853 1854
	| 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
1855 1856 1857
	| expr comp_op expr %prec EQ	{ $$= (*((*$2)(0)))($1,$3); }
	| expr comp_op all_or_any in_subselect %prec EQ
	{
1858
	  Item_allany_subselect *it=
unknown's avatar
unknown committed
1859
	    new Item_allany_subselect(YYTHD, $1, (*$2)($3), $4);
unknown's avatar
unknown committed
1860 1861 1862 1863 1864
	  if ($3)
	    $$ = new Item_func_not(it);	/* ALL */
	  else
	    $$ = it;			/* ANY/SOME */
	}
unknown's avatar
unknown committed
1865 1866 1867 1868 1869 1870
	| 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); }
1871 1872
	| 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
1873
	| expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
1874
        | expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
unknown's avatar
unknown committed
1875 1876 1877 1878 1879
	| expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| expr '%' expr		{ $$= new Item_func_mod($1,$3); }
	| expr '+' INTERVAL_SYM expr interval
	  { $$= new Item_date_add_interval($1,$4,$5,0); }
	| expr '-' INTERVAL_SYM expr interval
unknown's avatar
unknown committed
1880
	  { $$= new Item_date_add_interval($1,$4,$5,1); }
1881 1882
	| expr COLLATE_SYM charset_name
	  { $$= new Item_func_set_collation($1,$3); };
unknown's avatar
unknown committed
1883 1884 1885 1886 1887 1888 1889

/* 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)); }
1890
	| no_in_expr OR_OR_CONCAT expr	{ $$= or_or_concat(YYTHD, $1,$3); }
unknown's avatar
unknown committed
1891
	| no_in_expr OR expr		{ $$= new Item_cond_or($1,$3); }
unknown's avatar
unknown committed
1892
        | no_in_expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
unknown's avatar
unknown committed
1893
	| no_in_expr AND expr		{ $$= new Item_cond_and($1,$3); }
unknown's avatar
unknown committed
1894
	| 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
1895 1896 1897 1898 1899 1900 1901
	| 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
1902 1903 1904
	| no_in_expr comp_op expr %prec EQ	{ $$= (*((*$2)(0)))($1,$3); }
	| no_in_expr comp_op all_or_any in_subselect %prec EQ
	{
1905
	  Item_allany_subselect *it=
unknown's avatar
unknown committed
1906
	    new Item_allany_subselect(YYTHD, $1, (*$2)($3), $4);
unknown's avatar
unknown committed
1907 1908 1909 1910 1911
	  if ($3)
	    $$ = new Item_func_not(it);	/* ALL */
	  else
	    $$ = it;			/* ANY/SOME */
	}
unknown's avatar
unknown committed
1912 1913 1914 1915 1916 1917
	| 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); }
1918
	| no_in_expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
unknown's avatar
unknown committed
1919
	| no_in_expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
unknown's avatar
unknown committed
1920
        | no_in_expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
unknown's avatar
unknown committed
1921 1922
	| no_in_expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| no_in_expr '%' expr		{ $$= new Item_func_mod($1,$3); }
1923
	| no_in_expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
unknown's avatar
unknown committed
1924 1925 1926 1927
	| no_in_expr '+' INTERVAL_SYM expr interval
	  { $$= new Item_date_add_interval($1,$4,$5,0); }
	| no_in_expr '-' INTERVAL_SYM expr interval
	  { $$= new Item_date_add_interval($1,$4,$5,1); }
unknown's avatar
unknown committed
1928
	| simple_expr;
unknown's avatar
unknown committed
1929 1930 1931

/* expressions that begin with 'expr' that does NOT follow AND */
no_and_expr:
unknown's avatar
unknown committed
1932 1933
	  no_and_expr IN_SYM '(' expr_list ')'
	  { $$= new Item_func_in($1,*$4); }
unknown's avatar
unknown committed
1934 1935
	| no_and_expr NOT IN_SYM '(' expr_list ')'
	  { $$= new Item_func_not(new Item_func_in($1,*$5)); }
unknown's avatar
unknown committed
1936
        | no_and_expr IN_SYM in_subselect
unknown's avatar
unknown committed
1937
          { $$= new Item_in_subselect(YYTHD, $1, $3); }
unknown's avatar
unknown committed
1938
	| no_and_expr NOT IN_SYM in_subselect
1939
          {
unknown's avatar
unknown committed
1940
            $$= new Item_func_not(new Item_in_subselect(YYTHD, $1, $4));
unknown's avatar
unknown committed
1941
          }
unknown's avatar
unknown committed
1942 1943 1944 1945
	| 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)); }
1946
	| no_and_expr OR_OR_CONCAT expr	{ $$= or_or_concat(YYTHD, $1,$3); }
unknown's avatar
unknown committed
1947
	| no_and_expr OR expr		{ $$= new Item_cond_or($1,$3); }
unknown's avatar
unknown committed
1948
        | no_and_expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
unknown's avatar
unknown committed
1949
	| 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
1950 1951 1952 1953 1954 1955 1956
	| 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
1957 1958 1959
	| no_and_expr comp_op expr %prec EQ { $$= (*((*$2)(0)))($1,$3); }
	| no_and_expr comp_op all_or_any in_subselect %prec EQ
	{
1960
	  Item_allany_subselect *it=
unknown's avatar
unknown committed
1961
	    new Item_allany_subselect(YYTHD, $1, (*$2)($3), $4);
unknown's avatar
unknown committed
1962 1963 1964 1965 1966
	  if ($3)
	    $$ = new Item_func_not(it);	/* ALL */
	  else
	    $$ = it;			/* ANY/SOME */
	}
unknown's avatar
unknown committed
1967 1968 1969 1970 1971 1972
	| 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); }
1973
	| no_and_expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
unknown's avatar
unknown committed
1974
	| no_and_expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
unknown's avatar
unknown committed
1975
        | no_and_expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
unknown's avatar
unknown committed
1976 1977
	| no_and_expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| no_and_expr '%' expr		{ $$= new Item_func_mod($1,$3); }
1978
	| no_and_expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
unknown's avatar
unknown committed
1979 1980 1981 1982
	| no_and_expr '+' INTERVAL_SYM expr interval
	  { $$= new Item_date_add_interval($1,$4,$5,0); }
	| no_and_expr '-' INTERVAL_SYM expr interval
	  { $$= new Item_date_add_interval($1,$4,$5,1); }
unknown's avatar
unknown committed
1983
	| simple_expr;
unknown's avatar
unknown committed
1984 1985 1986 1987

simple_expr:
	simple_ident
	| literal
unknown's avatar
unknown committed
1988
	| param_marker
unknown's avatar
unknown committed
1989
	| '@' ident_or_text SET_VAR expr
unknown's avatar
unknown committed
1990 1991
	  {
	    $$= new Item_func_set_user_var($2,$4);
1992
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
1993
	  }
1994
	| '@' ident_or_text
unknown's avatar
unknown committed
1995 1996
	  {
	    $$= new Item_func_get_user_var($2);
1997
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
1998
	  }
unknown's avatar
unknown committed
1999 2000 2001 2002
	| '@' '@' opt_var_ident_type ident_or_text
	  {
	    if (!($$= get_system_var((enum_var_type) $3, $4)))
	      YYABORT;
unknown's avatar
unknown committed
2003
	  }
unknown's avatar
unknown committed
2004 2005 2006 2007 2008 2009
	| 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; }
2010 2011 2012
          /* Note: In SQL-99 "ROW" is optional, but not having it mandatory
	     causes conflicts with the INTERVAL syntax. */
	| ROW_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
2013
	  {
2014 2015
	    $5->push_front($3);
	    $$= new Item_row(*$5);
unknown's avatar
unknown committed
2016
	  }
unknown's avatar
unknown committed
2017 2018
	| EXISTS exists_subselect { $$= $2; }
	| singleval_subselect   { $$= $1; }
unknown's avatar
unknown committed
2019
	| '{' ident expr '}'	{ $$= $3; }
unknown's avatar
unknown committed
2020
        | MATCH ident_list_arg AGAINST '(' expr ')'
2021
          { Select->add_ftfunc_to_list((Item_func_match *)
2022
                   ($$=new Item_func_match_nl(*$2,$5))); }
unknown's avatar
unknown committed
2023
        | MATCH ident_list_arg AGAINST '(' expr IN_SYM BOOLEAN_SYM MODE_SYM ')'
2024
          { Select->add_ftfunc_to_list((Item_func_match *)
2025
                   ($$=new Item_func_match_bool(*$2,$5))); }
2026
	| BINARY expr %prec NEG { $$= new Item_func_set_collation($2,my_charset_bin); }
unknown's avatar
unknown committed
2027
	| CAST_SYM '(' expr AS cast_type ')'  { $$= create_func_cast($3, $5); }
unknown's avatar
unknown committed
2028
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
2029
	  { $$= new Item_func_case(* $4, $2, $5 ); }
unknown's avatar
unknown committed
2030
	| CONVERT_SYM '(' expr ',' cast_type ')'  { $$= create_func_cast($3, $5); }
2031 2032
	| CONVERT_SYM '(' expr USING charset_name ')'
	  { $$= new Item_func_conv_charset($3,$5); }
2033
	| CONVERT_SYM '(' expr ',' expr ',' expr ')'
2034
	  { $$= new Item_func_conv_charset3($3,$7,$5); }
unknown's avatar
unknown committed
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
	| 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);}
	| 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
2049 2050
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
unknown's avatar
unknown committed
2051 2052 2053 2054 2055 2056 2057
	| 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
2058
	  { $$= new Item_func_curdate(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
2059
	| CURTIME optional_braces
2060
	  { $$= new Item_func_curtime(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
2061
	| CURTIME '(' expr ')'
2062 2063
	  {
	    $$= new Item_func_curtime($3);
2064
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
2065
	  }
unknown's avatar
unknown committed
2066 2067 2068 2069 2070
	| DATE_ADD_INTERVAL '(' expr ',' INTERVAL_SYM expr interval ')'
	  { $$= new Item_date_add_interval($3,$6,$7,0); }
	| DATE_SUB_INTERVAL '(' expr ',' INTERVAL_SYM expr interval ')'
	  { $$= new Item_date_add_interval($3,$6,$7,1); }
	| DATABASE '(' ')'
2071
	  {
unknown's avatar
unknown committed
2072
	    $$= new Item_func_database();
2073
            Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
2074
	  }
unknown's avatar
unknown committed
2075 2076 2077 2078
	| 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
2079 2080 2081
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
2082
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
2083
	  }
unknown's avatar
unknown committed
2084 2085 2086 2087 2088
	| 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
2089
	| DES_DECRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
2090
        { $$= new Item_func_des_decrypt($3); }
unknown's avatar
unknown committed
2091
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
2092
        { $$= new Item_func_des_decrypt($3,$5); }
unknown's avatar
unknown committed
2093
	| DES_ENCRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
2094
        { $$= new Item_func_des_encrypt($3); }
unknown's avatar
unknown committed
2095
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
2096
        { $$= new Item_func_des_encrypt($3,$5); }
unknown's avatar
unknown committed
2097 2098 2099 2100 2101 2102
	| 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); }
2103 2104
	| FALSE_SYM
	  { $$= new Item_int((char*) "FALSE",0,1); }
unknown's avatar
unknown committed
2105 2106 2107 2108 2109 2110
	| 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
2111
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
unknown's avatar
unknown committed
2112 2113 2114
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
	  { $$= new Item_func_field($3, *$5); }
unknown's avatar
unknown committed
2115
	| GEOMFROMTEXT '(' expr ')'
2116
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2117
	| GEOMFROMTEXT '(' expr ',' expr ')'
2118
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2119
	| GEOMETRYCOLLECTION '(' expr_list ')'
2120 2121
	  { $$= new Item_func_spatial_collection(* $3,
                       Geometry::wkbGeometryCollection,
unknown's avatar
unknown committed
2122
                       Geometry::wkbPoint); }
unknown's avatar
unknown committed
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
	| 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); }
	| INTERVAL_SYM expr interval '+' expr
	  /* we cannot put interval before - */
	  { $$= new Item_date_add_interval($5,$2,$3,0); }
	| INTERVAL_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_interval($3,* $5); }
	| LAST_INSERT_ID '(' ')'
	  {
2136 2137
    	    $$= get_system_var(OPT_SESSION, "last_insert_id", 14,
			      "last_insert_id()");
unknown's avatar
unknown committed
2138 2139 2140 2141
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
	    $$= new Item_func_set_last_insert_id($3);
2142
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
2143 2144 2145
	  }
	| LEFT '(' expr ',' expr ')'
	  { $$= new Item_func_left($3,$5); }
unknown's avatar
unknown committed
2146
	| LINESTRING '(' expr_list ')'
2147
	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
2148
               Geometry::wkbLineString, Geometry::wkbPoint); }
unknown's avatar
unknown committed
2149 2150 2151 2152
	| 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
2153
 	| GEOMCOLLFROMTEXT '(' expr ')'
2154
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2155
	| GEOMCOLLFROMTEXT '(' expr ',' expr ')'
2156
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2157
	| GREATEST_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
2158 2159 2160
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
2161
	| LOG_SYM '(' expr ')'
unknown's avatar
unknown committed
2162
	  { $$= new Item_func_log($3); }
2163
	| LOG_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
2164
	  { $$= new Item_func_log($3, $5); }
unknown's avatar
unknown committed
2165
 	| LINEFROMTEXT '(' expr ')'
2166
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2167
	| LINEFROMTEXT '(' expr ',' expr ')'
2168
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2169 2170
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
2171 2172
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
unknown's avatar
unknown committed
2173 2174
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
unknown's avatar
unknown committed
2175
 	| MULTILINESTRING '(' expr_list ')'
2176
 	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
2177
                    Geometry::wkbMultiLineString, Geometry::wkbLineString); }
unknown's avatar
unknown committed
2178
 	| MLINEFROMTEXT '(' expr ')'
2179
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2180
	| MLINEFROMTEXT '(' expr ',' expr ')'
2181
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2182
	| MPOINTFROMTEXT '(' expr ')'
2183
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2184
	| MPOINTFROMTEXT '(' expr ',' expr ')'
2185
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2186
	| MPOLYFROMTEXT '(' expr ')'
2187
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2188
	| MPOLYFROMTEXT '(' expr ',' expr ')'
2189
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2190
	| MULTIPOINT '(' expr_list ')'
2191
 	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
2192 2193
                    Geometry::wkbMultiPoint, Geometry::wkbPoint); }
 	| MULTIPOLYGON '(' expr_list ')'
2194
 	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
2195
                       Geometry::wkbMultiPolygon, Geometry::wkbPolygon ); }
unknown's avatar
unknown committed
2196
	| NOW_SYM optional_braces
2197
	  { $$= new Item_func_now(); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
2198
	| NOW_SYM '(' expr ')'
2199
	  { $$= new Item_func_now($3); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
2200 2201 2202 2203
	| PASSWORD '(' expr ')'
	  {
	    $$= new Item_func_password($3);
	   }
unknown's avatar
unknown committed
2204
 	| POINTFROMTEXT '(' expr ')'
2205
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2206
	| POINTFROMTEXT '(' expr ',' expr ')'
2207
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2208
	| POLYFROMTEXT '(' expr ')'
2209
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2210
	| POLYFROMTEXT '(' expr ',' expr ')'
2211
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
2212
	| POLYGON '(' expr_list ')'
2213
 	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
2214
			Geometry::wkbPolygon, Geometry::wkbLineString); }
unknown's avatar
unknown committed
2215 2216
	| POSITION_SYM '(' no_in_expr IN_SYM expr ')'
	  { $$ = new Item_func_locate($5,$3); }
unknown's avatar
unknown committed
2217
	| RAND '(' expr ')'
2218
	  { $$= new Item_func_rand($3); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
2219
	| RAND '(' ')'
2220
	  { $$= new Item_func_rand(); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
	| 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 ')'
2241
	  { $$= new Item_func_trim($3,new Item_string(" ",1,default_charset_info)); }
unknown's avatar
unknown committed
2242 2243 2244 2245 2246 2247 2248 2249
	| 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); }
2250 2251
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
2252 2253
	| TRUE_SYM
	  { $$= new Item_int((char*) "TRUE",1,1); }
unknown's avatar
unknown committed
2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
	| 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 ')'
2297
	  {
unknown's avatar
unknown committed
2298 2299
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
unknown's avatar
unknown committed
2300
	| UNIX_TIMESTAMP '(' ')'
unknown's avatar
unknown committed
2301 2302
	  {
	    $$= new Item_func_unix_timestamp();
2303
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
2304
	  }
unknown's avatar
unknown committed
2305 2306 2307
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
2308
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
	| 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 ')'
2320
	  {
unknown's avatar
unknown committed
2321
	    $$=new Item_func_benchmark($3,$5);
2322
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
2323
	  }
unknown's avatar
unknown committed
2324
	| EXTRACT_SYM '(' interval FROM expr ')'
unknown's avatar
unknown committed
2325
	{ $$=new Item_extract( $3, $5); };
unknown's avatar
unknown committed
2326 2327 2328

udf_expr_list:
	/* empty */	{ $$= NULL; }
unknown's avatar
unknown committed
2329
	| expr_list	{ $$= $1;};
unknown's avatar
unknown committed
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351

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); }
	| COUNT_SYM '(' '*' ')'
	  { $$=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
2352 2353
	| VARIANCE_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_variance($3); }
unknown's avatar
unknown committed
2354
	| SUM_SYM '(' in_sum_expr ')'
unknown's avatar
unknown committed
2355
	  { $$=new Item_sum_sum($3); };
unknown's avatar
unknown committed
2356 2357

in_sum_expr:
2358 2359 2360 2361 2362
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
2363
	    YYABORT;
2364 2365
	  }
	}
unknown's avatar
unknown committed
2366 2367
	expr
	{
unknown's avatar
unknown committed
2368
	  Select->select_lex()->in_sum_expr--;
unknown's avatar
unknown committed
2369
	  $$=$2;
unknown's avatar
unknown committed
2370
	};
unknown's avatar
unknown committed
2371

unknown's avatar
unknown committed
2372 2373
cast_type:
	BINARY 			{ $$=ITEM_CAST_BINARY; }
2374
	| CHAR_SYM		{ $$=ITEM_CAST_CHAR; }
unknown's avatar
unknown committed
2375 2376 2377 2378 2379 2380
	| 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; }
2381 2382
	| DATETIME		{ $$=ITEM_CAST_DATETIME; }
	;
unknown's avatar
unknown committed
2383

unknown's avatar
unknown committed
2384
expr_list:
2385
	{ Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
2386
	expr_list2
unknown's avatar
unknown committed
2387
	{ $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
2388 2389

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

unknown's avatar
unknown committed
2393 2394
ident_list_arg:
          ident_list          { $$= $1; }
unknown's avatar
unknown committed
2395
        | '(' ident_list ')'  { $$= $2; };
unknown's avatar
unknown committed
2396

unknown's avatar
unknown committed
2397
ident_list:
2398
        { Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
2399
        ident_list2
unknown's avatar
unknown committed
2400
        { $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
2401 2402

ident_list2:
2403
        simple_ident { Select->expr_list.head()->push_back($1); }
unknown's avatar
unknown committed
2404
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
unknown's avatar
unknown committed
2405 2406 2407

opt_expr:
	/* empty */      { $$= NULL; }
unknown's avatar
unknown committed
2408
	| expr         	 { $$= $1; };
unknown's avatar
unknown committed
2409 2410 2411

opt_else:
	/* empty */    { $$= NULL; }
unknown's avatar
unknown committed
2412
	| ELSE expr    { $$= $2; };
unknown's avatar
unknown committed
2413 2414

when_list:
unknown's avatar
unknown committed
2415
        { Select->when_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
2416
	when_list2
unknown's avatar
unknown committed
2417
	{ $$= Select->when_list.pop(); };
unknown's avatar
unknown committed
2418 2419

when_list2:
unknown's avatar
unknown committed
2420
	expr THEN_SYM expr
unknown's avatar
unknown committed
2421
	  {
2422
	    SELECT_LEX_NODE *sel=Select;
unknown's avatar
unknown committed
2423 2424
	    sel->when_list.head()->push_back($1);
	    sel->when_list.head()->push_back($3);
unknown's avatar
unknown committed
2425
	}
unknown's avatar
unknown committed
2426
	| when_list2 WHEN_SYM expr THEN_SYM expr
unknown's avatar
unknown committed
2427
	  {
2428
	    SELECT_LEX_NODE *sel=Select;
unknown's avatar
unknown committed
2429 2430
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
unknown's avatar
unknown committed
2431
	  };
unknown's avatar
unknown committed
2432 2433

opt_pad:
2434
	/* empty */ { $$=new Item_string(" ",1,default_charset_info); }
unknown's avatar
unknown committed
2435
	| expr	    { $$=$1; };
unknown's avatar
unknown committed
2436 2437 2438 2439

join_table_list:
	'(' join_table_list ')'	{ $$=$2; }
	| join_table		{ $$=$1; }
2440
	| join_table_list normal_join join_table_list { $$=$3; }
2441 2442 2443
	| join_table_list STRAIGHT_JOIN join_table_list
	  { $$=$3 ; $$->straight=1; }
	| join_table_list INNER_SYM JOIN_SYM join_table_list ON expr
unknown's avatar
unknown committed
2444
	  { add_join_on($4,$6); $$=$4; }
2445
	| join_table_list INNER_SYM JOIN_SYM join_table_list
unknown's avatar
unknown committed
2446
	  {
unknown's avatar
unknown committed
2447
	    SELECT_LEX *sel= Select->select_lex();
2448 2449
	    sel->db1=$1->db; sel->table1=$1->alias;
	    sel->db2=$4->db; sel->table2=$4->alias;
unknown's avatar
unknown committed
2450
	  }
unknown's avatar
unknown committed
2451 2452
	  USING '(' using_list ')'
	  { add_join_on($4,$8); $$=$4; }
2453
	| join_table_list LEFT opt_outer JOIN_SYM join_table_list ON expr
2454
	  { add_join_on($5,$7); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
2455
	| join_table_list LEFT opt_outer JOIN_SYM join_table_list
unknown's avatar
unknown committed
2456
	  {
unknown's avatar
unknown committed
2457
	    SELECT_LEX *sel= Select->select_lex();
2458 2459
	    sel->db1=$1->db; sel->table1=$1->alias;
	    sel->db2=$5->db; sel->table2=$5->alias;
unknown's avatar
unknown committed
2460
	  }
unknown's avatar
unknown committed
2461
	  USING '(' using_list ')'
2462
	  { add_join_on($5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
2463
	| join_table_list NATURAL LEFT opt_outer JOIN_SYM join_table_list
2464
	  { add_join_natural($1,$6); $6->outer_join|=JOIN_TYPE_LEFT; $$=$6; }
2465
	| join_table_list RIGHT opt_outer JOIN_SYM join_table_list ON expr
2466
	  { add_join_on($1,$7); $1->outer_join|=JOIN_TYPE_RIGHT; $$=$1; }
2467
	| join_table_list RIGHT opt_outer JOIN_SYM join_table_list
unknown's avatar
unknown committed
2468
	  {
unknown's avatar
unknown committed
2469
	    SELECT_LEX *sel= Select->select_lex();
2470 2471
	    sel->db1=$1->db; sel->table1=$1->alias;
	    sel->db2=$5->db; sel->table2=$5->alias;
unknown's avatar
unknown committed
2472
	  }
2473 2474
	  USING '(' using_list ')'
	  { add_join_on($1,$9); $1->outer_join|=JOIN_TYPE_RIGHT; $$=$1; }
2475
	| join_table_list NATURAL RIGHT opt_outer JOIN_SYM join_table_list
2476
	  { add_join_natural($6,$1); $1->outer_join|=JOIN_TYPE_RIGHT; $$=$1; }
2477
	| join_table_list NATURAL JOIN_SYM join_table_list
unknown's avatar
unknown committed
2478
	  { add_join_natural($1,$4); $$=$4; };
unknown's avatar
unknown committed
2479 2480 2481 2482

normal_join:
	',' {}
	| JOIN_SYM {}
unknown's avatar
unknown committed
2483
	| CROSS JOIN_SYM {};
unknown's avatar
unknown committed
2484 2485

join_table:
unknown's avatar
unknown committed
2486
	{
unknown's avatar
unknown committed
2487
	  SELECT_LEX *sel= Select->select_lex();
unknown's avatar
unknown committed
2488 2489
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
	}
unknown's avatar
unknown committed
2490
        table_ident opt_table_alias opt_key_definition
unknown's avatar
unknown committed
2491
	{
unknown's avatar
unknown committed
2492 2493
	  LEX *lex= Lex;
	  SELECT_LEX_NODE *sel= lex->current_select;
unknown's avatar
unknown committed
2494 2495
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3, 0,
					   lex->lock_option,
2496 2497
					   sel->get_use_index(),
					   sel->get_ignore_index())))
2498
	    YYABORT;
unknown's avatar
unknown committed
2499
	}
unknown's avatar
unknown committed
2500
	| '{' ident join_table LEFT OUTER JOIN_SYM join_table ON expr '}'
2501
	  { add_join_on($7,$9); $7->outer_join|=JOIN_TYPE_LEFT; $$=$7; }
2502
        | '(' SELECT_SYM select_derived ')' opt_table_alias
unknown's avatar
unknown committed
2503 2504
	{
	  LEX *lex=Lex;
2505 2506 2507
	  SELECT_LEX_UNIT *unit= lex->current_select->master_unit();
	  lex->current_select= unit->outer_select();
	  if (!($$= lex->current_select->
unknown's avatar
unknown committed
2508
                add_table_to_list(lex->thd, new Table_ident(unit), $5, 0,
unknown's avatar
unknown committed
2509
				  lex->lock_option)))
unknown's avatar
unknown committed
2510
	    YYABORT;
unknown's avatar
unknown committed
2511
	};
unknown's avatar
unknown committed
2512

2513
select_derived:
unknown's avatar
unknown committed
2514
        {
unknown's avatar
unknown committed
2515
	  LEX *lex= Lex;
unknown's avatar
unknown committed
2516
	  lex->derived_tables= 1;
2517
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
unknown's avatar
unknown committed
2518
              mysql_new_select(lex, 1))
unknown's avatar
unknown committed
2519 2520
	    YYABORT;
	  mysql_init_select(lex);
2521
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
unknown's avatar
unknown committed
2522
	}
2523
        select_options select_item_list opt_select_from union_opt
2524
        ;
unknown's avatar
unknown committed
2525 2526 2527

opt_outer:
	/* empty */	{}
unknown's avatar
unknown committed
2528
	| OUTER		{};
unknown's avatar
unknown committed
2529 2530 2531 2532

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
unknown's avatar
unknown committed
2533
          {
unknown's avatar
unknown committed
2534
	    SELECT_LEX *sel= Select->select_lex();
unknown's avatar
unknown committed
2535 2536 2537
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
2538
	| IGNORE_SYM key_usage_list
unknown's avatar
unknown committed
2539
	  {
unknown's avatar
unknown committed
2540
	    SELECT_LEX *sel= Select->select_lex();
unknown's avatar
unknown committed
2541 2542
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
unknown's avatar
unknown committed
2543
	  };
unknown's avatar
unknown committed
2544 2545

key_usage_list:
unknown's avatar
unknown committed
2546
	key_or_index { Select->select_lex()->interval_list.empty(); }
2547
        '(' key_usage_list2 ')'
unknown's avatar
unknown committed
2548
        { $$= &Select->select_lex()->interval_list; };
unknown's avatar
unknown committed
2549 2550 2551

key_usage_list2:
	key_usage_list2 ',' ident
unknown's avatar
unknown committed
2552
        { Select->select_lex()->
2553 2554
	    interval_list.push_back(new String((const char*) $3.str, $3.length,
				    default_charset_info)); }
unknown's avatar
unknown committed
2555
	| ident
unknown's avatar
unknown committed
2556
        { Select->select_lex()->
2557 2558
	    interval_list.push_back(new String((const char*) $1.str, $1.length,
				    default_charset_info)); }
unknown's avatar
unknown committed
2559
	| PRIMARY_SYM
unknown's avatar
unknown committed
2560
        { Select->select_lex()->
2561 2562
	    interval_list.push_back(new String("PRIMARY", 7,
				    default_charset_info)); };
unknown's avatar
unknown committed
2563 2564 2565

using_list:
	ident
unknown's avatar
unknown committed
2566
	  {
unknown's avatar
unknown committed
2567
	    SELECT_LEX *sel= Select->select_lex();
2568 2569 2570 2571
	    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
2572 2573 2574 2575
	      YYABORT;
	  }
	| using_list ',' ident
	  {
unknown's avatar
unknown committed
2576
	    SELECT_LEX *sel= Select->select_lex();
unknown's avatar
unknown committed
2577
	    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
2578
	      YYABORT;
unknown's avatar
unknown committed
2579
	  };
unknown's avatar
unknown committed
2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593

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
2594
	| YEAR_SYM		{ $$=INTERVAL_YEAR; };
unknown's avatar
unknown committed
2595 2596 2597 2598

table_alias:
	/* empty */
	| AS
unknown's avatar
unknown committed
2599
	| EQ;
unknown's avatar
unknown committed
2600 2601 2602 2603

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


where_clause:
2608
	/* empty */  { Select->select_lex()->where= 0; }
2609 2610
	| WHERE expr
	  {
unknown's avatar
unknown committed
2611
	    Select->select_lex()->where= $2;
2612 2613 2614
	    if ($2)
	      $2->top_level_item();
	  }
unknown's avatar
unknown committed
2615
 	;
unknown's avatar
unknown committed
2616 2617 2618

having_clause:
	/* empty */
unknown's avatar
unknown committed
2619
	| HAVING { Select->select_lex()->create_refs= 1; } expr
unknown's avatar
unknown committed
2620 2621 2622 2623 2624 2625
	  {
	    SELECT_LEX *sel= Select->select_lex();
	    sel->having= $3; sel->create_refs=0;
	    if ($3)
	      $3->top_level_item();
	  }
2626
	;
unknown's avatar
unknown committed
2627 2628 2629

opt_escape:
	ESCAPE_SYM TEXT_STRING	{ $$= $2.str; }
unknown's avatar
unknown committed
2630
	| /* empty */		{ $$= (char*) "\\"; };
unknown's avatar
unknown committed
2631 2632 2633


/*
unknown's avatar
unknown committed
2634
   group by statement in select
unknown's avatar
unknown committed
2635 2636 2637 2638
*/

group_clause:
	/* empty */
2639
	| GROUP BY group_list olap_opt;
unknown's avatar
unknown committed
2640 2641

group_list:
unknown's avatar
unknown committed
2642
	group_list ',' order_ident order_dir
unknown's avatar
unknown committed
2643
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
2644
	| order_ident order_dir
unknown's avatar
unknown committed
2645
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
2646

2647 2648
olap_opt:
	/* empty */ {}
2649
	| WITH CUBE_SYM
2650
          {
2651
	    LEX *lex=Lex;
unknown's avatar
unknown committed
2652
	    lex->olap= 1;
2653 2654 2655 2656 2657 2658
	    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
2659
	    lex->current_select->select_lex()->olap= CUBE_TYPE;
2660
	    net_printf(lex->thd, ER_NOT_SUPPORTED_YET, "CUBE");
2661
	    YYABORT;	/* To be deleted in 4.1 */
2662
	  }
2663
	| WITH ROLLUP_SYM
2664
          {
2665
	    LEX *lex= Lex;
unknown's avatar
unknown committed
2666
	    lex->olap= 1;
2667 2668 2669 2670 2671 2672
	    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
2673
	    lex->current_select->select_lex()->olap= ROLLUP_TYPE;
2674
	    net_printf(lex->thd, ER_NOT_SUPPORTED_YET, "ROLLUP");
2675
	    YYABORT;	/* To be deleted in 4.1 */
2676
	  }
2677
	;
2678

unknown's avatar
unknown committed
2679
/*
unknown's avatar
unknown committed
2680
   Order by statement in select
unknown's avatar
unknown committed
2681 2682
*/

2683
opt_order_clause:
unknown's avatar
unknown committed
2684
	/* empty */
unknown's avatar
unknown committed
2685
	| order_clause;
2686 2687

order_clause:
2688 2689
	ORDER_SYM BY
        {
unknown's avatar
unknown committed
2690
	  LEX *lex=Lex;
2691
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
unknown's avatar
unknown committed
2692
	      lex->current_select->select_lex()->olap !=
2693
	      UNSPECIFIED_OLAP_TYPE)
2694
	  {
2695
	    net_printf(lex->thd, ER_WRONG_USAGE,
2696 2697 2698 2699
		       "CUBE/ROLLUP",
		       "ORDER BY");
	    YYABORT;
	  }
unknown's avatar
unknown committed
2700
	} order_list;
unknown's avatar
unknown committed
2701 2702 2703

order_list:
	order_list ',' order_ident order_dir
unknown's avatar
unknown committed
2704
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
2705
	| order_ident order_dir
unknown's avatar
unknown committed
2706
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
2707 2708 2709

order_dir:
	/* empty */ { $$ =  1; }
unknown's avatar
unknown committed
2710
	| ASC  { $$ =1; }
unknown's avatar
unknown committed
2711
	| DESC { $$ =0; };
unknown's avatar
unknown committed
2712 2713


2714
opt_limit_clause:
unknown's avatar
unknown committed
2715
	/* empty */ {}
2716 2717 2718 2719
	| limit_clause {}
	;

limit_clause:
2720
	LIMIT
unknown's avatar
unknown committed
2721
	  {
2722 2723
	    LEX *lex= Lex;
	    if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
unknown's avatar
unknown committed
2724
	        lex->current_select->select_lex()->olap !=
2725
		UNSPECIFIED_OLAP_TYPE)
2726
	    {
2727
	      net_printf(lex->thd, ER_WRONG_USAGE, "CUBE/ROLLUP",
2728
		        "LIMIT");
2729
	      YYABORT;
2730
	    }
unknown's avatar
unknown committed
2731
	  }
unknown's avatar
unknown committed
2732
	  limit_options
2733
	  {}
unknown's avatar
unknown committed
2734
	;
unknown's avatar
unknown committed
2735 2736 2737

limit_options:
	ULONG_NUM
unknown's avatar
unknown committed
2738
	  {
unknown's avatar
unknown committed
2739
            SELECT_LEX_NODE *sel= Select;
unknown's avatar
unknown committed
2740 2741 2742 2743 2744
            sel->select_limit= $1;
            sel->offset_limit= 0L;
	  }
	| ULONG_NUM ',' ULONG_NUM
	  {
2745
	    SELECT_LEX_NODE *sel= Select;
unknown's avatar
unknown committed
2746 2747
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
unknown's avatar
unknown committed
2748
	  }
unknown's avatar
unknown committed
2749
	| ULONG_NUM OFFSET_SYM ULONG_NUM
unknown's avatar
unknown committed
2750
	  {
unknown's avatar
unknown committed
2751
	    SELECT_LEX_NODE *sel= Select;
unknown's avatar
unknown committed
2752 2753 2754 2755
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
	  }
	;
unknown's avatar
unknown committed
2756 2757 2758 2759 2760


delete_limit_clause:
	/* empty */
	{
unknown's avatar
unknown committed
2761
	  LEX *lex=Lex;
2762
	  lex->current_select->select_limit= HA_POS_ERROR;
unknown's avatar
unknown committed
2763
	}
unknown's avatar
unknown committed
2764
	| LIMIT ulonglong_num
unknown's avatar
unknown committed
2765
	{ Select->select_limit= (ha_rows) $2; };
unknown's avatar
unknown committed
2766 2767

ULONG_NUM:
unknown's avatar
unknown committed
2768
	NUM	    { $$= strtoul($1.str,NULL,10); }
unknown's avatar
unknown committed
2769
	| ULONGLONG_NUM { $$= (ulong) strtoull($1.str,NULL,10); }
unknown's avatar
unknown committed
2770
	| REAL_NUM  { $$= strtoul($1.str,NULL,10); }
unknown's avatar
unknown committed
2771
	| FLOAT_NUM { $$= strtoul($1.str,NULL,10); };
unknown's avatar
unknown committed
2772

unknown's avatar
unknown committed
2773
ulonglong_num:
unknown's avatar
unknown committed
2774
	NUM	    { $$= (ulonglong) strtoul($1.str,NULL,10); }
unknown's avatar
unknown committed
2775 2776
	| ULONGLONG_NUM { $$= strtoull($1.str,NULL,10); }
	| LONG_NUM  { $$= (ulonglong) strtoul($1.str,NULL,10); }
unknown's avatar
unknown committed
2777
	| REAL_NUM  { $$= strtoull($1.str,NULL,10); }
unknown's avatar
unknown committed
2778
	| FLOAT_NUM { $$= strtoull($1.str,NULL,10); };
unknown's avatar
unknown committed
2779 2780 2781 2782 2783 2784

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
2785 2786 2787 2788 2789 2790 2791
	    if (&lex->select_lex != lex->current_select)
	    {
	      net_printf(lex->thd, ER_WRONG_USAGE,
			  "PROCEDURE",
			  "subquery");
	      YYABORT;
	    }
unknown's avatar
unknown committed
2792 2793 2794
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
unknown's avatar
unknown committed
2795
	    if (add_proc_to_list(lex->thd, new Item_field(NULL,NULL,$2.str)))
unknown's avatar
unknown committed
2796
	      YYABORT;
2797
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
2798
	  }
unknown's avatar
unknown committed
2799
	  '(' procedure_list ')';
unknown's avatar
unknown committed
2800 2801 2802 2803


procedure_list:
	/* empty */ {}
unknown's avatar
unknown committed
2804
	| procedure_list2 {};
unknown's avatar
unknown committed
2805 2806 2807

procedure_list2:
	procedure_list2 ',' procedure_item
unknown's avatar
unknown committed
2808
	| procedure_item;
unknown's avatar
unknown committed
2809 2810 2811 2812

procedure_item:
	  remember_name expr
	  {
unknown's avatar
unknown committed
2813 2814
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
unknown's avatar
unknown committed
2815 2816
	      YYABORT;
	    if (!$2->name)
unknown's avatar
unknown committed
2817
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1));
2818 2819 2820 2821 2822 2823
	  }
          ;


select_var_list_init:
	   {
unknown's avatar
unknown committed
2824 2825
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
2826 2827
	        YYABORT;
	   }
2828
	   select_var_list
2829
	   {}
2830
           ;
unknown's avatar
unknown committed
2831

unknown's avatar
unknown committed
2832
select_var_list:
2833 2834 2835 2836 2837
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

select_var_ident:  '@' ident_or_text
unknown's avatar
unknown committed
2838
           {
2839
             LEX *lex=Lex;
unknown's avatar
unknown committed
2840
	     if (lex->result && ((select_dumpvar *)lex->result)->var_list.push_back((LEX_STRING*) sql_memdup(&$2,sizeof(LEX_STRING))))
unknown's avatar
unknown committed
2841 2842
	       YYABORT;
	   }
unknown's avatar
unknown committed
2843
           ;
unknown's avatar
unknown committed
2844

2845
into:
unknown's avatar
unknown committed
2846
        INTO OUTFILE TEXT_STRING
unknown's avatar
unknown committed
2847
	{
2848
	  LEX *lex=Lex;
unknown's avatar
unknown committed
2849 2850 2851 2852 2853 2854 2855
	  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
2856 2857 2858 2859
	}
	opt_field_term opt_line_term
	| INTO DUMPFILE TEXT_STRING
	{
2860
	  LEX *lex=Lex;
unknown's avatar
unknown committed
2861 2862 2863 2864 2865 2866 2867
	  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
2868
	}
2869
        | INTO select_var_list_init
unknown's avatar
unknown committed
2870
	{
2871
	  Lex->safe_to_cache_query=0;
2872 2873
	}
        ;
unknown's avatar
unknown committed
2874

unknown's avatar
unknown committed
2875 2876 2877
/*
  DO statement
*/
unknown's avatar
unknown committed
2878

2879
do:	DO_SYM
unknown's avatar
unknown committed
2880 2881 2882 2883 2884 2885
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
	  if (!(lex->insert_list = new List_item))
	    YYABORT;
	}
2886 2887 2888 2889
	values
	{}
	;

unknown's avatar
unknown committed
2890
/*
unknown's avatar
unknown committed
2891
  Drop : delete tables or index
unknown's avatar
unknown committed
2892 2893 2894
*/

drop:
2895
	DROP opt_temporary TABLE_SYM if_exists table_list opt_restrict
unknown's avatar
unknown committed
2896
	{
unknown's avatar
unknown committed
2897 2898
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
2899 2900
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
unknown's avatar
unknown committed
2901 2902 2903
	}
	| DROP INDEX ident ON table_ident {}
	  {
unknown's avatar
unknown committed
2904 2905 2906 2907
	     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
2908
						     $3.str));
unknown's avatar
unknown committed
2909
	     if (!lex->current_select->add_table_to_list(lex->thd, $5,NULL, 1))
unknown's avatar
unknown committed
2910 2911 2912 2913
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
unknown's avatar
unknown committed
2914 2915 2916 2917
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
	    lex->name=$4.str;
unknown's avatar
unknown committed
2918 2919 2920
	 }
	| DROP UDF_SYM ident
	  {
unknown's avatar
unknown committed
2921 2922 2923
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
	    lex->udf.name=$3.str;
unknown's avatar
unknown committed
2924
	  };
unknown's avatar
unknown committed
2925 2926 2927


table_list:
2928
	table_name
unknown's avatar
unknown committed
2929
	| table_list ',' table_name;
unknown's avatar
unknown committed
2930

2931
table_name:
unknown's avatar
unknown committed
2932
	table_ident
unknown's avatar
unknown committed
2933
	{ if (!Select->add_table_to_list(YYTHD, $1, NULL, 1)) YYABORT; };
unknown's avatar
unknown committed
2934 2935

if_exists:
2936
	/* empty */ { $$= 0; }
2937 2938
	| IF EXISTS { $$= 1; }
	;
unknown's avatar
unknown committed
2939

2940 2941 2942 2943
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
unknown's avatar
unknown committed
2944 2945 2946 2947 2948
/*
** Insert : add new data to table
*/

insert:
2949 2950 2951 2952 2953 2954 2955
	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
2956
	opt_ignore insert2
unknown's avatar
unknown committed
2957
	{
unknown's avatar
unknown committed
2958
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
2959
	}
2960
	insert_field_spec opt_insert_update
2961
	{}
unknown's avatar
unknown committed
2962
	;
unknown's avatar
unknown committed
2963 2964

replace:
2965 2966
	REPLACE
	{
2967
	  LEX *lex=Lex;
2968 2969 2970
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
	}
unknown's avatar
unknown committed
2971 2972
	replace_lock_option insert2
	{
unknown's avatar
unknown committed
2973
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
2974 2975
	}
	insert_field_spec
2976
	{}
unknown's avatar
unknown committed
2977
	{}
unknown's avatar
unknown committed
2978
	;
unknown's avatar
unknown committed
2979 2980

insert_lock_option:
unknown's avatar
unknown committed
2981 2982 2983 2984
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
2985
	;
unknown's avatar
unknown committed
2986 2987

replace_lock_option:
unknown's avatar
unknown committed
2988 2989
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
unknown's avatar
unknown committed
2990 2991 2992

insert2:
	INTO insert_table {}
unknown's avatar
unknown committed
2993
	| insert_table {};
unknown's avatar
unknown committed
2994 2995

insert_table:
2996
	table_name
unknown's avatar
unknown committed
2997
	{
unknown's avatar
unknown committed
2998 2999 3000 3001
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
unknown's avatar
unknown committed
3002
	};
unknown's avatar
unknown committed
3003 3004 3005 3006 3007

insert_field_spec:
	opt_field_spec insert_values {}
	| SET
	  {
unknown's avatar
unknown committed
3008 3009 3010
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
3011 3012
	      YYABORT;
	   }
unknown's avatar
unknown committed
3013
	   ident_eq_list;
unknown's avatar
unknown committed
3014 3015 3016 3017

opt_field_spec:
	/* empty */	  { }
	| '(' fields ')'  { }
unknown's avatar
unknown committed
3018
	| '(' ')'	  { };
unknown's avatar
unknown committed
3019 3020 3021

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
unknown's avatar
unknown committed
3022
	| insert_ident		{ Lex->field_list.push_back($1); };
unknown's avatar
unknown committed
3023 3024 3025

insert_values:
	VALUES	values_list  {}
unknown's avatar
unknown committed
3026
	| VALUE_SYM values_list  {}
unknown's avatar
unknown committed
3027 3028 3029 3030 3031
	| SELECT_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command = (lex->sql_command == SQLCOM_INSERT ?
				SQLCOM_INSERT_SELECT : SQLCOM_REPLACE_SELECT);
unknown's avatar
unknown committed
3032
	    lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
3033
	    mysql_init_select(lex);
unknown's avatar
unknown committed
3034
	  }
unknown's avatar
unknown committed
3035
	  select_options select_item_list opt_select_from select_lock_type
3036
          union_clause {}
unknown's avatar
unknown committed
3037
	;
unknown's avatar
unknown committed
3038 3039 3040

values_list:
	values_list ','  no_braces
unknown's avatar
unknown committed
3041
	| no_braces;
unknown's avatar
unknown committed
3042 3043 3044 3045

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
unknown's avatar
unknown committed
3046
	ident_eq_value;
unknown's avatar
unknown committed
3047 3048

ident_eq_value:
unknown's avatar
unknown committed
3049
	simple_ident equal expr_or_default
unknown's avatar
unknown committed
3050
	 {
unknown's avatar
unknown committed
3051 3052 3053
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
unknown's avatar
unknown committed
3054
	    YYABORT;
unknown's avatar
unknown committed
3055
	 };
unknown's avatar
unknown committed
3056 3057

equal:	EQ		{}
unknown's avatar
unknown committed
3058 3059 3060 3061 3062 3063 3064
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
unknown's avatar
unknown committed
3065 3066 3067 3068 3069 3070 3071 3072 3073

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
unknown's avatar
unknown committed
3074 3075
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
3076
	    YYABORT;
unknown's avatar
unknown committed
3077
	 };
unknown's avatar
unknown committed
3078 3079 3080

opt_values:
	/* empty */ {}
unknown's avatar
unknown committed
3081
	| values;
unknown's avatar
unknown committed
3082 3083

values:
unknown's avatar
unknown committed
3084
	values ','  expr_or_default
unknown's avatar
unknown committed
3085 3086 3087 3088
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
unknown's avatar
unknown committed
3089 3090 3091 3092 3093 3094 3095 3096
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
3097
	expr	  { $$= $1;}
unknown's avatar
unknown committed
3098 3099
	| DEFAULT {$$= new Item_default(); }
	;
unknown's avatar
unknown committed
3100

3101 3102
opt_insert_update:
        /* empty */
3103
        | ON DUPLICATE
3104 3105 3106 3107 3108 3109 3110 3111 3112
          { /* 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;
            }
          }
3113
          KEY_SYM UPDATE_SYM update_list
3114 3115
        ;

unknown's avatar
unknown committed
3116 3117 3118
/* Update rows in a table */

update:
3119 3120
	UPDATE_SYM
	{
3121 3122
	  LEX *lex= Lex;
          lex->sql_command= SQLCOM_UPDATE;
3123
          lex->select_lex.init_order();
3124
        }
unknown's avatar
unknown committed
3125 3126 3127
        opt_low_priority opt_ignore join_table_list
	SET update_list where_clause opt_order_clause delete_limit_clause
	{
3128
	  LEX *lex= Lex;
unknown's avatar
unknown committed
3129
	  Select->set_lock_for_tables($3);
3130
          if (lex->select_lex.table_list.elements > 1)
3131
            lex->sql_command=SQLCOM_UPDATE_MULTI;
unknown's avatar
unknown committed
3132 3133
	}
	;
unknown's avatar
unknown committed
3134 3135 3136 3137

update_list:
	update_list ',' simple_ident equal expr
	{
unknown's avatar
unknown committed
3138
	  if (add_item_to_list(YYTHD, $3) || add_value_to_list(YYTHD, $5))
unknown's avatar
unknown committed
3139 3140 3141 3142
	    YYABORT;
	}
	| simple_ident equal expr
	  {
unknown's avatar
unknown committed
3143
	    if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
unknown's avatar
unknown committed
3144
	      YYABORT;
unknown's avatar
unknown committed
3145
	  };
unknown's avatar
unknown committed
3146 3147

opt_low_priority:
3148
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
3149
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
3150 3151 3152 3153

/* Delete rows from a table */

delete:
3154
	DELETE_SYM
3155
	{
3156 3157 3158
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
	  lex->select_lex.options= 0;
unknown's avatar
unknown committed
3159
	  lex->lock_option= lex->thd->update_lock_default;
3160
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
3161
	}
unknown's avatar
unknown committed
3162 3163
	opt_delete_options single_multi {}
	;
3164 3165

single_multi:
unknown's avatar
unknown committed
3166 3167
 	FROM table_ident
	{
unknown's avatar
unknown committed
3168
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, 1, Lex->lock_option))
unknown's avatar
unknown committed
3169 3170 3171
	    YYABORT;
	}
	where_clause opt_order_clause
3172
	delete_limit_clause {}
unknown's avatar
unknown committed
3173
	| table_wild_list
unknown's avatar
unknown committed
3174
	  { mysql_init_multi_delete(Lex); }
unknown's avatar
unknown committed
3175
          FROM join_table_list where_clause
unknown's avatar
unknown committed
3176 3177
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
3178 3179 3180
	  USING join_table_list where_clause
	  {}
	;
unknown's avatar
unknown committed
3181 3182 3183

table_wild_list:
	  table_wild_one {}
unknown's avatar
unknown committed
3184
	  | table_wild_list ',' table_wild_one {};
unknown's avatar
unknown committed
3185 3186

table_wild_one:
unknown's avatar
unknown committed
3187 3188
	ident opt_wild
	{
unknown's avatar
unknown committed
3189
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), NULL, 1,
unknown's avatar
unknown committed
3190 3191 3192 3193 3194
	      Lex->lock_option))
	    YYABORT;
        }
	| ident '.' ident opt_wild
	  {
unknown's avatar
unknown committed
3195 3196
	    if (!Select->add_table_to_list(YYTHD, new Table_ident($1, $3, 0),
					   NULL, 1, Lex->lock_option))
unknown's avatar
unknown committed
3197
	      YYABORT;
unknown's avatar
unknown committed
3198
	  }
unknown's avatar
unknown committed
3199
	;
unknown's avatar
unknown committed
3200 3201

opt_wild:
3202
	/* empty */	{}
unknown's avatar
unknown committed
3203
	| '.' '*'	{};
unknown's avatar
unknown committed
3204 3205


3206
opt_delete_options:
unknown's avatar
unknown committed
3207
	/* empty */	{}
unknown's avatar
unknown committed
3208
	| opt_delete_option opt_delete_options {};
3209 3210

opt_delete_option:
3211
	QUICK		{ Select->options|= OPTION_QUICK; }
unknown's avatar
unknown committed
3212
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; };
3213

3214
truncate:
unknown's avatar
unknown committed
3215
	TRUNCATE_SYM opt_table_sym table_name
3216
	{
3217
	  LEX* lex= Lex;
3218
	  lex->sql_command= SQLCOM_TRUNCATE;
3219
	  lex->select_lex.options= 0;
3220
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
3221 3222
	}
	;
3223

unknown's avatar
unknown committed
3224 3225
opt_table_sym:
	/* empty */
unknown's avatar
unknown committed
3226
	| TABLE_SYM;
3227

unknown's avatar
unknown committed
3228 3229
/* Show things */

3230 3231
show:	SHOW
	{
unknown's avatar
unknown committed
3232 3233 3234 3235
	  LEX *lex=Lex;
	  lex->wild=0;
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
unknown's avatar
unknown committed
3236
	show_param
3237 3238
	{}
	;
unknown's avatar
unknown committed
3239 3240 3241 3242 3243

show_param:
	DATABASES wild
	  { Lex->sql_command= SQLCOM_SHOW_DATABASES; }
	| TABLES opt_db wild
unknown's avatar
unknown committed
3244
	  {
3245
	    LEX *lex= Lex;
unknown's avatar
unknown committed
3246
	    lex->sql_command= SQLCOM_SHOW_TABLES;
3247 3248
	    lex->select_lex.db= $2;
	    lex->select_lex.options= 0;
unknown's avatar
unknown committed
3249
	   }
unknown's avatar
unknown committed
3250
	| TABLE_SYM STATUS_SYM opt_db wild
unknown's avatar
unknown committed
3251
	  {
3252
	    LEX *lex= Lex;
unknown's avatar
unknown committed
3253
	    lex->sql_command= SQLCOM_SHOW_TABLES;
3254 3255
	    lex->select_lex.options|= SELECT_DESCRIBE;
	    lex->select_lex.db= $3;
unknown's avatar
unknown committed
3256
	  }
3257
	| OPEN_SYM TABLES opt_db wild
unknown's avatar
unknown committed
3258
	  {
3259
	    LEX *lex= Lex;
unknown's avatar
unknown committed
3260
	    lex->sql_command= SQLCOM_SHOW_OPEN_TABLES;
3261 3262
	    lex->select_lex.db= $3;
	    lex->select_lex.options= 0;
3263
	  }
unknown's avatar
unknown committed
3264
	| opt_full COLUMNS from_or_in table_ident opt_db wild
unknown's avatar
unknown committed
3265 3266
	  {
	    Lex->sql_command= SQLCOM_SHOW_FIELDS;
unknown's avatar
unknown committed
3267 3268
	    if ($5)
	      $4->change_db($5);
unknown's avatar
unknown committed
3269
	    if (!Select->add_table_to_list(YYTHD, $4, NULL, 0))
unknown's avatar
unknown committed
3270 3271
	      YYABORT;
	  }
3272
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
3273 3274
	  TEXT_STRING AND MASTER_LOG_POS_SYM EQ ulonglong_num
	  AND MASTER_SERVER_ID_SYM EQ
unknown's avatar
unknown committed
3275 3276 3277 3278 3279
	ULONG_NUM
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
3280
	    Lex->mi.server_id = $16;
unknown's avatar
unknown committed
3281
          }
3282
        | BINARY LOGS_SYM
unknown's avatar
unknown committed
3283 3284
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
3285 3286 3287 3288 3289
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
unknown's avatar
unknown committed
3290
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
unknown's avatar
unknown committed
3291
          {
3292 3293 3294 3295
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
	    lex->select_lex.select_limit= lex->thd->variables.select_limit;
	    lex->select_lex.offset_limit= 0L;
3296
          } opt_limit_clause
unknown's avatar
unknown committed
3297 3298 3299 3300 3301
	| 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
3302
	    if (!Select->add_table_to_list(YYTHD, $3, NULL, 0))
unknown's avatar
unknown committed
3303 3304
	      YYABORT;
	  }
unknown's avatar
unknown committed
3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319
	| 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;
	  }
3320
        | COUNT_SYM '(' '*' ')' WARNINGS
3321
          { (void) create_select_for_variable("warning_count"); }
3322
        | COUNT_SYM '(' '*' ')' ERRORS
3323
	  { (void) create_select_for_variable("error_count"); }
3324
        | WARNINGS {Select->offset_limit=0L;} opt_limit_clause
unknown's avatar
unknown committed
3325
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
3326
        | ERRORS {Select->offset_limit=0L;} opt_limit_clause
3327
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
unknown's avatar
unknown committed
3328 3329
	| STATUS_SYM wild
	  { Lex->sql_command= SQLCOM_SHOW_STATUS; }
unknown's avatar
unknown committed
3330 3331
        | INNOBASE_SYM STATUS_SYM
          { Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;}
unknown's avatar
unknown committed
3332 3333
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
3334
	| opt_var_type VARIABLES wild
unknown's avatar
unknown committed
3335
	  {
3336
	    THD *thd= YYTHD;
3337
	    thd->lex.sql_command= SQLCOM_SHOW_VARIABLES;
unknown's avatar
unknown committed
3338
	    thd->lex.option_type= (enum_var_type) $1;
3339
	  }
3340 3341
	| CHAR_SYM SET wild
	  { Lex->sql_command= SQLCOM_SHOW_CHARSETS; }
unknown's avatar
unknown committed
3342 3343
	| LOGS_SYM
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; }
unknown's avatar
unknown committed
3344
	| GRANTS FOR_SYM user
unknown's avatar
unknown committed
3345 3346 3347 3348 3349 3350
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
	    lex->grant_user->password.str=NullS;
	  }
unknown's avatar
unknown committed
3351
	| CREATE DATABASE opt_if_not_exists ident
unknown's avatar
unknown committed
3352 3353
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
unknown's avatar
unknown committed
3354 3355
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
unknown's avatar
unknown committed
3356
	  }
unknown's avatar
unknown committed
3357 3358 3359
        | CREATE TABLE_SYM table_ident
          {
	    Lex->sql_command = SQLCOM_SHOW_CREATE;
unknown's avatar
unknown committed
3360
	    if(!Select->add_table_to_list(YYTHD, $3, NULL,0))
unknown's avatar
unknown committed
3361 3362 3363 3364 3365
	      YYABORT;
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
unknown's avatar
unknown committed
3366
          }
unknown's avatar
unknown committed
3367 3368 3369
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
unknown's avatar
unknown committed
3370
          };
unknown's avatar
unknown committed
3371 3372 3373

opt_db:
	/* empty */  { $$= 0; }
unknown's avatar
unknown committed
3374
	| from_or_in ident { $$= $2.str; };
unknown's avatar
unknown committed
3375 3376 3377

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

unknown's avatar
unknown committed
3380 3381
opt_full:
	/* empty */ { Lex->verbose=0; }
unknown's avatar
unknown committed
3382
	| FULL	    { Lex->verbose=1; };
unknown's avatar
unknown committed
3383

unknown's avatar
unknown committed
3384 3385
from_or_in:
	FROM
unknown's avatar
unknown committed
3386
	| IN_SYM;
unknown's avatar
unknown committed
3387

unknown's avatar
unknown committed
3388 3389
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
unknown's avatar
unknown committed
3390
        | IN_SYM TEXT_STRING { Lex->mi.log_file_name = $2.str; };
unknown's avatar
unknown committed
3391 3392 3393

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


unknown's avatar
unknown committed
3397 3398 3399 3400
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
unknown's avatar
unknown committed
3401 3402 3403 3404
	  LEX *lex=Lex;
	  lex->wild=0;
	  lex->verbose=0;
	  lex->sql_command=SQLCOM_SHOW_FIELDS;
unknown's avatar
unknown committed
3405
	  if (!Select->add_table_to_list(lex->thd, $2, NULL,0))
unknown's avatar
unknown committed
3406 3407
	    YYABORT;
	}
3408
	opt_describe_column {}
unknown's avatar
unknown committed
3409
	| describe_command { Lex->describe=1; } select
3410
          {
unknown's avatar
unknown committed
3411
	    LEX *lex=Lex;
3412
	    lex->select_lex.options|= SELECT_DESCRIBE;
unknown's avatar
unknown committed
3413 3414
	  }
	;
unknown's avatar
unknown committed
3415 3416 3417

describe_command:
	DESC
unknown's avatar
unknown committed
3418
	| DESCRIBE;
unknown's avatar
unknown committed
3419 3420 3421 3422

opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
unknown's avatar
unknown committed
3423
	| ident
unknown's avatar
unknown committed
3424
	  { Lex->wild= new String((const char*) $1.str,$1.length,default_charset_info); };
unknown's avatar
unknown committed
3425 3426 3427 3428 3429


/* flush things */

flush:
unknown's avatar
unknown committed
3430 3431 3432 3433 3434
	FLUSH_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
	}
3435 3436 3437
	flush_options
	{}
	;
unknown's avatar
unknown committed
3438 3439 3440

flush_options:
	flush_options ',' flush_option
unknown's avatar
unknown committed
3441
	| flush_option;
unknown's avatar
unknown committed
3442 3443

flush_option:
3444
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
unknown's avatar
unknown committed
3445
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
unknown's avatar
unknown committed
3446
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
unknown's avatar
unknown committed
3447 3448 3449 3450
	| 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
3451 3452
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
3453 3454
	| DES_KEY_FILE	{ Lex->type|= REFRESH_DES_KEY_FILE; }
 	| RESOURCES     { Lex->type|= REFRESH_USER_RESOURCES; };
unknown's avatar
unknown committed
3455

unknown's avatar
unknown committed
3456
opt_table_list:
3457 3458
	/* empty */  {;}
	| table_list {;};
unknown's avatar
unknown committed
3459

unknown's avatar
unknown committed
3460
reset:
unknown's avatar
unknown committed
3461 3462 3463 3464
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
3465 3466 3467 3468
	} reset_options
	{}
	;

unknown's avatar
unknown committed
3469 3470
reset_options:
	reset_options ',' reset_option
unknown's avatar
unknown committed
3471
	| reset_option;
unknown's avatar
unknown committed
3472 3473

reset_option:
unknown's avatar
unknown committed
3474 3475
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
unknown's avatar
unknown committed
3476
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
unknown's avatar
unknown committed
3477

unknown's avatar
unknown committed
3478
purge:
unknown's avatar
unknown committed
3479 3480 3481 3482 3483 3484
	PURGE
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_PURGE;
	  lex->type=0;
	}
unknown's avatar
unknown committed
3485 3486 3487
        MASTER_SYM LOGS_SYM TO_SYM TEXT_STRING
         {
	   Lex->to_log = $6.str;
unknown's avatar
unknown committed
3488
         } ;
unknown's avatar
unknown committed
3489

unknown's avatar
unknown committed
3490 3491 3492
/* kill threads */

kill:
3493
	KILL_SYM expr
unknown's avatar
unknown committed
3494
	{
unknown's avatar
unknown committed
3495
	  LEX *lex=Lex;
unknown's avatar
unknown committed
3496
	  if ($2->check_cols(1) || $2->fix_fields(lex->thd, 0, &$2))
3497
	  {
3498
	    send_error(lex->thd, ER_SET_CONSTANTS_ONLY);
unknown's avatar
unknown committed
3499 3500 3501 3502
	    YYABORT;
	  }
          lex->sql_command=SQLCOM_KILL;
	  lex->thread_id= (ulong) $2->val_int();
unknown's avatar
unknown committed
3503
	};
unknown's avatar
unknown committed
3504 3505 3506 3507

/* change database */

use:	USE_SYM ident
unknown's avatar
unknown committed
3508 3509
	{
	  LEX *lex=Lex;
3510 3511
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
unknown's avatar
unknown committed
3512
	};
unknown's avatar
unknown committed
3513 3514 3515

/* import, export of files */

3516
load:	LOAD DATA_SYM load_data_lock opt_local INFILE TEXT_STRING
unknown's avatar
unknown committed
3517
	{
unknown's avatar
unknown committed
3518 3519
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
unknown's avatar
unknown committed
3520 3521
	  lex->lock_option= $3;
	  lex->local_file=  $4;
unknown's avatar
unknown committed
3522
	  if (!(lex->exchange= new sql_exchange($6.str,0)))
unknown's avatar
unknown committed
3523
	    YYABORT;
unknown's avatar
unknown committed
3524
	  lex->field_list.empty();
unknown's avatar
unknown committed
3525 3526 3527 3528
	}
	opt_duplicate INTO TABLE_SYM table_ident opt_field_term opt_line_term
	opt_ignore_lines opt_field_spec
	{
unknown's avatar
unknown committed
3529
	  if (!Select->add_table_to_list(YYTHD, $11, NULL, 1))
unknown's avatar
unknown committed
3530 3531 3532
	    YYABORT;
	}
        |
unknown's avatar
unknown committed
3533
	LOAD TABLE_SYM table_ident FROM MASTER_SYM
unknown's avatar
unknown committed
3534 3535
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
unknown's avatar
unknown committed
3536
	  if (!Select->add_table_to_list(YYTHD, $3, NULL, 1))
unknown's avatar
unknown committed
3537
	    YYABORT;
unknown's avatar
unknown committed
3538 3539

        }
3540 3541 3542 3543
        |
	LOAD DATA_SYM FROM MASTER_SYM
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
unknown's avatar
unknown committed
3544
        };
unknown's avatar
unknown committed
3545 3546 3547

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

unknown's avatar
unknown committed
3550
load_data_lock:
3551
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
3552 3553
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
3554 3555


unknown's avatar
unknown committed
3556 3557 3558
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
unknown's avatar
unknown committed
3559
	| IGNORE_SYM	{ Lex->duplicates=DUP_IGNORE; };
unknown's avatar
unknown committed
3560 3561 3562

opt_field_term:
	/* empty */
unknown's avatar
unknown committed
3563
	| COLUMNS field_term_list;
unknown's avatar
unknown committed
3564 3565 3566

field_term_list:
	field_term_list field_term
unknown's avatar
unknown committed
3567
	| field_term;
unknown's avatar
unknown committed
3568 3569 3570 3571

field_term:
	TERMINATED BY text_string { Lex->exchange->field_term= $3;}
	| OPTIONALLY ENCLOSED BY text_string
unknown's avatar
unknown committed
3572 3573 3574 3575 3576
	  {
	    LEX *lex=Lex;
	    lex->exchange->enclosed= $4;
	    lex->exchange->opt_enclosed=1;
	  }
unknown's avatar
unknown committed
3577
	| ENCLOSED BY text_string { Lex->exchange->enclosed= $3;}
unknown's avatar
unknown committed
3578
	| ESCAPED BY text_string  { Lex->exchange->escaped= $3;};
unknown's avatar
unknown committed
3579 3580 3581

opt_line_term:
	/* empty */
unknown's avatar
unknown committed
3582
	| LINES line_term_list;
unknown's avatar
unknown committed
3583 3584 3585

line_term_list:
	line_term_list line_term
unknown's avatar
unknown committed
3586
	| line_term;
unknown's avatar
unknown committed
3587 3588 3589

line_term:
	TERMINATED BY text_string { Lex->exchange->line_term= $3;}
unknown's avatar
unknown committed
3590
	| STARTING BY text_string { Lex->exchange->line_start= $3;};
unknown's avatar
unknown committed
3591 3592 3593 3594

opt_ignore_lines:
	/* empty */
	| IGNORE_SYM NUM LINES
unknown's avatar
unknown committed
3595
	  { Lex->exchange->skip_lines=atol($2.str); };
unknown's avatar
unknown committed
3596 3597 3598 3599

/* Common definitions */

text_literal:
3600
	TEXT_STRING { $$ = new Item_string($1.str,$1.length,YYTHD->thd_charset); }
3601
	| UNDERSCORE_CHARSET TEXT_STRING { $$ = new Item_string($2.str,$2.length,Lex->charset); }
unknown's avatar
unknown committed
3602
	| text_literal TEXT_STRING
unknown's avatar
unknown committed
3603
	{ ((Item_string*) $1)->append($2.str,$2.length); };
unknown's avatar
unknown committed
3604 3605

text_string:
3606
	TEXT_STRING	{ $$=  new String($1.str,$1.length,YYTHD->thd_charset); }
unknown's avatar
unknown committed
3607 3608
	| HEX_NUM
	  {
3609
	    Item *tmp = new Item_varbinary($1.str,$1.length);
unknown's avatar
unknown committed
3610
	    $$= tmp ? tmp->val_str((String*) 0) : (String*) 0;
unknown's avatar
unknown committed
3611
	  };
unknown's avatar
unknown committed
3612
param_marker:
3613
        '?'
unknown's avatar
unknown committed
3614
        {
3615
	  LEX *lex=Lex;
3616
          if (YYTHD->prepare_command)
3617 3618
          {
            lex->param_list.push_back($$=new Item_param());
3619
            lex->param_count++;
unknown's avatar
unknown committed
3620
          }
3621
          else
unknown's avatar
unknown committed
3622 3623 3624 3625
          {
            yyerror("You have an error in your SQL syntax");
            YYABORT;
          }
unknown's avatar
unknown committed
3626
        };
unknown's avatar
unknown committed
3627 3628 3629 3630
literal:
	text_literal	{ $$ =	$1; }
	| NUM		{ $$ =	new Item_int($1.str, (longlong) atol($1.str),$1.length); }
	| LONG_NUM	{ $$ =	new Item_int($1.str); }
unknown's avatar
unknown committed
3631
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
unknown's avatar
unknown committed
3632 3633 3634 3635
	| 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;}
3636
	| HEX_NUM	{ $$ =	new Item_varbinary($1.str,$1.length);}
unknown's avatar
unknown committed
3637 3638
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
unknown's avatar
unknown committed
3639
	| TIMESTAMP text_literal { $$ = $2; };
unknown's avatar
unknown committed
3640 3641 3642 3643 3644 3645 3646

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

insert_ident:
	simple_ident	 { $$=$1; }
unknown's avatar
unknown committed
3647
	| table_wild	 { $$=$1; };
unknown's avatar
unknown committed
3648 3649 3650 3651

table_wild:
	ident '.' '*' { $$ = new Item_field(NullS,$1.str,"*"); }
	| ident '.' ident '.' '*'
3652
	{ $$ = new Item_field((YYTHD->client_capabilities &
unknown's avatar
unknown committed
3653
   CLIENT_NO_SCHEMA ? NullS : $1.str),$3.str,"*"); };
unknown's avatar
unknown committed
3654 3655

order_ident:
unknown's avatar
unknown committed
3656
	expr { $$=$1; };
unknown's avatar
unknown committed
3657 3658 3659

simple_ident:
	ident
unknown's avatar
unknown committed
3660
	{
3661 3662
	  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
3663
	}
unknown's avatar
unknown committed
3664
	| ident '.' ident
unknown's avatar
unknown committed
3665
	{
3666 3667
	  SELECT_LEX_NODE *sel=Select;
	  $$ = !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
3668
	}
unknown's avatar
unknown committed
3669
	| '.' ident '.' ident
unknown's avatar
unknown committed
3670
	{
3671 3672
	  SELECT_LEX_NODE *sel=Select;
	  $$ = !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
3673
	}
unknown's avatar
unknown committed
3674
	| ident '.' ident '.' ident
unknown's avatar
unknown committed
3675
	{
3676
	  SELECT_LEX_NODE *sel=Select;
3677
	  $$ = !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
3678
	};
unknown's avatar
unknown committed
3679 3680 3681 3682 3683


field_ident:
	ident			{ $$=$1;}
	| ident '.' ident	{ $$=$3;}	/* Skipp schema name in create*/
unknown's avatar
unknown committed
3684
	| '.' ident		{ $$=$2;}	/* For Delphi */;
unknown's avatar
unknown committed
3685 3686 3687 3688

table_ident:
	ident			{ $$=new Table_ident($1); }
	| ident '.' ident	{ $$=new Table_ident($1,$3,0);}
unknown's avatar
unknown committed
3689 3690
	| '.' ident		{ $$=new Table_ident($2);}
   /* For Delphi */;
unknown's avatar
unknown committed
3691 3692 3693 3694 3695

ident:
	IDENT	    { $$=$1; }
	| keyword
	{
3696 3697
	  LEX *lex= Lex;
	  $$.str= lex->thd->strmake($1.str,$1.length);
unknown's avatar
unknown committed
3698
	  $$.length=$1.length;
3699
	  if (lex->next_state != STATE_END)
unknown's avatar
unknown committed
3700
	    lex->next_state=STATE_OPERATOR_OR_IDENT;
3701 3702
	}
	;
unknown's avatar
unknown committed
3703 3704 3705

ident_or_text:
	ident 		{ $$=$1;}
unknown's avatar
unknown committed
3706
	| TEXT_STRING	{ $$=$1;}
unknown's avatar
unknown committed
3707
	| LEX_HOSTNAME	{ $$=$1;};
unknown's avatar
unknown committed
3708 3709 3710 3711

user:
	ident_or_text
	{
unknown's avatar
unknown committed
3712 3713
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
3714 3715 3716 3717 3718
	    YYABORT;
	  $$->user = $1; $$->host.str=NullS;
	  }
	| ident_or_text '@' ident_or_text
	  {
unknown's avatar
unknown committed
3719 3720
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
3721 3722
	      YYABORT;
	    $$->user = $1; $$->host=$3;
unknown's avatar
unknown committed
3723
	  };
unknown's avatar
unknown committed
3724 3725 3726 3727 3728 3729

/* Keyword that we allow for identifiers */

keyword:
	ACTION			{}
	| AFTER_SYM		{}
3730
	| AGAINST		{}
unknown's avatar
unknown committed
3731
	| AGGREGATE_SYM		{}
unknown's avatar
unknown committed
3732
	| ANY_SYM		{}
unknown's avatar
unknown committed
3733
	| AUTO_INC		{}
unknown's avatar
unknown committed
3734 3735
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
unknown's avatar
unknown committed
3736
	| BACKUP_SYM		{}
unknown's avatar
unknown committed
3737
	| BEGIN_SYM		{}
unknown's avatar
unknown committed
3738
	| BERKELEY_DB_SYM	{}
unknown's avatar
unknown committed
3739
	| BINLOG_SYM		{}
unknown's avatar
unknown committed
3740 3741
	| BIT_SYM		{}
	| BOOL_SYM		{}
unknown's avatar
unknown committed
3742
	| BOOLEAN_SYM		{}
3743
	| BYTE_SYM		{}
unknown's avatar
unknown committed
3744
	| CACHE_SYM		{}
3745
	| CHANGED		{}
3746
	| CHARSET		{}
unknown's avatar
unknown committed
3747
	| CHECKSUM_SYM		{}
3748
	| CIPHER_SYM		{}
unknown's avatar
unknown committed
3749
	| CLIENT_SYM		{}
3750
	| CLOSE_SYM		{}
unknown's avatar
unknown committed
3751
	| COMMENT_SYM		{}
unknown's avatar
unknown committed
3752
	| COMMITTED_SYM		{}
unknown's avatar
unknown committed
3753
	| COMMIT_SYM		{}
unknown's avatar
unknown committed
3754
	| COMPRESSED_SYM	{}
unknown's avatar
unknown committed
3755
	| CONCURRENT		{}
3756
	| CUBE_SYM		{}
unknown's avatar
unknown committed
3757 3758 3759 3760 3761
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
	| DELAY_KEY_WRITE_SYM	{}
unknown's avatar
unknown committed
3762 3763
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
unknown's avatar
unknown committed
3764
	| DO_SYM		{}
unknown's avatar
unknown committed
3765
	| DUMPFILE		{}
3766
	| DUAL_SYM		{}
unknown's avatar
unknown committed
3767 3768 3769 3770
	| DYNAMIC_SYM		{}
	| END			{}
	| ENUM			{}
	| ESCAPE_SYM		{}
unknown's avatar
unknown committed
3771
	| EVENTS_SYM		{}
unknown's avatar
unknown committed
3772
	| EXECUTE_SYM		{}
unknown's avatar
unknown committed
3773
	| EXTENDED_SYM		{}
3774
	| FAST_SYM		{}
unknown's avatar
unknown committed
3775 3776
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
unknown's avatar
unknown committed
3777
	| FULL			{}
unknown's avatar
unknown committed
3778 3779 3780 3781
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
	| FLUSH_SYM		{}
unknown's avatar
unknown committed
3782
	| GRANTS		{}
unknown's avatar
unknown committed
3783
	| GLOBAL_SYM		{}
unknown's avatar
unknown committed
3784
	| HEAP_SYM		{}
3785
	| HANDLER_SYM		{}
unknown's avatar
unknown committed
3786 3787 3788
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
unknown's avatar
unknown committed
3789
	| INDEXES		{}
unknown's avatar
unknown committed
3790
	| ISOLATION		{}
unknown's avatar
unknown committed
3791
	| ISAM_SYM		{}
unknown's avatar
unknown committed
3792
	| ISSUER_SYM		{}
unknown's avatar
unknown committed
3793
	| INNOBASE_SYM		{}
3794
	| INSERT_METHOD		{}
unknown's avatar
unknown committed
3795
	| IO_THREAD		{}
3796
	| LAST_SYM		{}
unknown's avatar
unknown committed
3797
	| LEVEL_SYM		{}
unknown's avatar
unknown committed
3798
	| LOCAL_SYM		{}
unknown's avatar
unknown committed
3799
	| LOCKS_SYM		{}
unknown's avatar
unknown committed
3800 3801 3802 3803 3804 3805 3806 3807 3808 3809
	| 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
3810 3811 3812
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
3813
	| MEDIUM_SYM		{}
unknown's avatar
unknown committed
3814 3815 3816 3817
	| MERGE_SYM		{}
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
unknown's avatar
unknown committed
3818
	| MODE_SYM		{}
unknown's avatar
unknown committed
3819 3820 3821 3822
	| MONTH_SYM		{}
	| MYISAM_SYM		{}
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
3823
	| NEXT_SYM		{}
unknown's avatar
unknown committed
3824
	| NEW_SYM		{}
unknown's avatar
unknown committed
3825
	| NO_SYM		{}
3826
	| NONE_SYM		{}
unknown's avatar
unknown committed
3827
	| OFFSET_SYM		{}
3828
	| OPEN_SYM		{}
unknown's avatar
unknown committed
3829
	| PACK_KEYS_SYM		{}
3830
	| PARTIAL		{}
unknown's avatar
unknown committed
3831
	| PASSWORD		{}
3832
	| PREV_SYM		{}
unknown's avatar
unknown committed
3833 3834
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
unknown's avatar
unknown committed
3835
	| QUERY_SYM		{}
3836
	| QUICK			{}
unknown's avatar
unknown committed
3837
	| RAID_0_SYM		{}
unknown's avatar
unknown committed
3838 3839
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
unknown's avatar
unknown committed
3840
	| RAID_STRIPED_SYM	{}
unknown's avatar
unknown committed
3841
	| RAID_TYPE		{}
unknown's avatar
unknown committed
3842 3843
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
unknown's avatar
unknown committed
3844 3845
	| RELOAD		{}
	| REPAIR		{}
unknown's avatar
unknown committed
3846
	| REPEATABLE_SYM	{}
unknown's avatar
unknown committed
3847
	| REPLICATION		{}
unknown's avatar
unknown committed
3848
	| RESET_SYM		{}
3849
	| RESOURCES		{}
unknown's avatar
unknown committed
3850
	| RESTORE_SYM		{}
unknown's avatar
unknown committed
3851
	| ROLLBACK_SYM		{}
3852
	| ROLLUP_SYM		{}
unknown's avatar
unknown committed
3853 3854 3855 3856
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
	| SECOND_SYM		{}
3857
	| SERIAL_SYM		{}
unknown's avatar
unknown committed
3858 3859
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
unknown's avatar
unknown committed
3860
	| SIGNED_SYM		{}
3861
	| SIMPLE_SYM		{}
unknown's avatar
unknown committed
3862
	| SHARE_SYM		{}
unknown's avatar
unknown committed
3863
	| SHUTDOWN		{}
unknown's avatar
unknown committed
3864
	| SLAVE			{}
unknown's avatar
unknown committed
3865
	| SQL_CACHE_SYM		{}
unknown's avatar
unknown committed
3866
	| SQL_BUFFER_RESULT	{}
unknown's avatar
unknown committed
3867
	| SQL_NO_CACHE_SYM	{}
unknown's avatar
unknown committed
3868
	| SQL_THREAD		{}
unknown's avatar
unknown committed
3869 3870 3871 3872
	| START_SYM		{}
	| STATUS_SYM		{}
	| STOP_SYM		{}
	| STRING_SYM		{}
unknown's avatar
unknown committed
3873
	| SUBJECT_SYM		{}
unknown's avatar
unknown committed
3874
	| SUPER_SYM		{}
unknown's avatar
unknown committed
3875 3876
	| TEMPORARY		{}
	| TEXT_SYM		{}
unknown's avatar
unknown committed
3877
	| TRANSACTION_SYM	{}
3878
	| TRUNCATE_SYM		{}
unknown's avatar
unknown committed
3879 3880 3881 3882
	| TIMESTAMP		{}
	| TIME_SYM		{}
	| TYPE_SYM		{}
	| UDF_SYM		{}
unknown's avatar
unknown committed
3883
	| UNCOMMITTED_SYM	{}
unknown's avatar
unknown committed
3884
	| UNICODE_SYM		{}
unknown's avatar
unknown committed
3885
	| USE_FRM		{}
unknown's avatar
unknown committed
3886
	| VARIABLES		{}
3887
	| VALUE_SYM		{}
unknown's avatar
unknown committed
3888
	| WORK_SYM		{}
unknown's avatar
unknown committed
3889
	| YEAR_SYM		{}
unknown's avatar
unknown committed
3890
	| SOUNDS_SYM            {}
unknown's avatar
unknown committed
3891
	;
unknown's avatar
unknown committed
3892 3893 3894 3895 3896 3897

/* Option functions */

set:
	SET opt_option
	{
unknown's avatar
unknown committed
3898 3899
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
unknown's avatar
unknown committed
3900 3901
	  lex->option_type=OPT_DEFAULT;
	  lex->var_list.empty();
unknown's avatar
unknown committed
3902
	}
3903 3904 3905
	option_value_list
	{}
	;
unknown's avatar
unknown committed
3906 3907 3908

opt_option:
	/* empty */ {}
unknown's avatar
unknown committed
3909
	| OPTION {};
unknown's avatar
unknown committed
3910 3911

option_value_list:
unknown's avatar
unknown committed
3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923
	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
3924
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
3925 3926 3927 3928 3929 3930
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

opt_var_ident_type:
	/* empty */		{ $$=OPT_DEFAULT; }
unknown's avatar
unknown committed
3931
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
3932 3933 3934
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
unknown's avatar
unknown committed
3935 3936

option_value:
unknown's avatar
unknown committed
3937
	'@' ident_or_text equal expr
unknown's avatar
unknown committed
3938
	{
unknown's avatar
unknown committed
3939
	  Lex->var_list.push_back(new set_var_user(new Item_func_set_user_var($2,$4)));
unknown's avatar
unknown committed
3940
	}
unknown's avatar
unknown committed
3941
	| internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
3942
	  {
unknown's avatar
unknown committed
3943 3944
	    LEX *lex=Lex;
	    lex->var_list.push_back(new set_var(lex->option_type, $1, $3));
unknown's avatar
unknown committed
3945
	  }
unknown's avatar
unknown committed
3946
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
3947 3948 3949 3950 3951 3952 3953 3954
	  {
	    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
3955
						find_sys_var("tx_isolation"),
unknown's avatar
unknown committed
3956
						new Item_int((int32) $4)));
unknown's avatar
unknown committed
3957
	  }
unknown's avatar
unknown committed
3958
	| CHAR_SYM SET opt_equal set_expr_or_default
unknown's avatar
unknown committed
3959
	{
unknown's avatar
unknown committed
3960 3961 3962 3963
	  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
3964 3965
	}
	| PASSWORD equal text_or_password
unknown's avatar
unknown committed
3966
	  {
3967
	    THD *thd=YYTHD;
unknown's avatar
unknown committed
3968
	    LEX_USER *user;
unknown's avatar
unknown committed
3969
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
unknown's avatar
unknown committed
3970 3971 3972 3973 3974 3975
	      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
3976
	  {
unknown's avatar
unknown committed
3977 3978
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
3979
	;
unknown's avatar
unknown committed
3980

unknown's avatar
unknown committed
3981 3982 3983 3984 3985 3986 3987 3988
internal_variable_name:
	ident
	{
	  sys_var *tmp=find_sys_var($1.str, $1.length);
	  if (!tmp)
	    YYABORT;
	  $$=tmp;
	}
3989
        ;
unknown's avatar
unknown committed
3990 3991 3992 3993 3994 3995 3996

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; }
	;
3997

unknown's avatar
unknown committed
3998 3999 4000 4001 4002 4003 4004 4005
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
	    if (!$3.length)
	      $$=$3.str;
	    else
	    {
unknown's avatar
unknown committed
4006
	      char *buff=(char*) YYTHD->alloc(HASH_PASSWORD_LENGTH+1);
4007
	      make_scrambled_password(buff,$3.str,opt_old_passwords,&current_thd->rand);
unknown's avatar
unknown committed
4008 4009
	      $$=buff;
	    }
4010 4011
	  }
          ;
unknown's avatar
unknown committed
4012

unknown's avatar
unknown committed
4013

unknown's avatar
unknown committed
4014
set_expr_or_default:
unknown's avatar
unknown committed
4015 4016
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
4017 4018
	| ON	  { $$=new Item_string("ON",  2, system_charset_info); }
	| ALL	  { $$=new Item_string("ALL", 3, system_charset_info); }
unknown's avatar
unknown committed
4019
	;
unknown's avatar
unknown committed
4020 4021


unknown's avatar
unknown committed
4022 4023 4024 4025 4026 4027 4028
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
	  Lex->sql_command=SQLCOM_LOCK_TABLES;
	}
4029 4030 4031
	table_lock_list
	{}
	;
unknown's avatar
unknown committed
4032 4033 4034

table_or_tables:
	TABLE_SYM
unknown's avatar
unknown committed
4035
	| TABLES;
unknown's avatar
unknown committed
4036 4037 4038

table_lock_list:
	table_lock
unknown's avatar
unknown committed
4039
	| table_lock_list ',' table_lock;
unknown's avatar
unknown committed
4040 4041 4042

table_lock:
	table_ident opt_table_alias lock_option
4043
	{
unknown's avatar
unknown committed
4044
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
4045 4046
	   YYABORT;
	}
4047
        ;
unknown's avatar
unknown committed
4048 4049 4050

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
4051
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
unknown's avatar
unknown committed
4052
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
4053 4054
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
unknown's avatar
unknown committed
4055 4056

unlock:
4057 4058
	UNLOCK_SYM table_or_tables { Lex->sql_command=SQLCOM_UNLOCK_TABLES; }
        ;
unknown's avatar
unknown committed
4059 4060


4061
/*
unknown's avatar
unknown committed
4062
** Handler: direct access to ISAM functions
4063 4064 4065 4066 4067
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
4068 4069
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_OPEN;
unknown's avatar
unknown committed
4070
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
4071 4072 4073 4074
	    YYABORT;
	}
	| HANDLER_SYM table_ident CLOSE_SYM
	{
4075 4076
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_CLOSE;
unknown's avatar
unknown committed
4077
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
4078 4079
	    YYABORT;
	}
unknown's avatar
unknown committed
4080
	| HANDLER_SYM table_ident READ_SYM
4081
	{
unknown's avatar
unknown committed
4082 4083 4084
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
4085 4086
	  lex->current_select->select_limit= 1;
	  lex->current_select->offset_limit= 0L;
unknown's avatar
unknown committed
4087
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
4088
	    YYABORT;
unknown's avatar
unknown committed
4089
        }
4090
        handler_read_or_scan where_clause opt_limit_clause { }
4091
        ;
4092

unknown's avatar
unknown committed
4093 4094
handler_read_or_scan:
	handler_scan_function         { Lex->backup_dir= 0; }
4095 4096
        | ident handler_rkey_function { Lex->backup_dir= $1.str; }
        ;
unknown's avatar
unknown committed
4097 4098 4099

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
4100 4101
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
unknown's avatar
unknown committed
4102 4103 4104 4105 4106 4107

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;  }
4108 4109
	| handler_rkey_mode
	{
unknown's avatar
unknown committed
4110 4111
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
unknown's avatar
unknown committed
4112
	  lex->ha_rkey_mode=$1;
unknown's avatar
unknown committed
4113
	  if (!(lex->insert_list = new List_item))
4114
	    YYABORT;
4115 4116
	} '(' values ')' { }
        ;
4117 4118

handler_rkey_mode:
unknown's avatar
unknown committed
4119 4120 4121 4122
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
4123 4124
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
4125

unknown's avatar
unknown committed
4126 4127 4128 4129 4130
/* GRANT / REVOKE */

revoke:
	REVOKE
	{
unknown's avatar
unknown committed
4131 4132 4133 4134 4135
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_REVOKE;
	  lex->users_list.empty();
	  lex->columns.empty();
	  lex->grant= lex->grant_tot_col=0;
4136
	  lex->select_lex.db=0;
4137 4138
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
4139
	  bzero((char*) &lex->mqh, sizeof(lex->mqh));
unknown's avatar
unknown committed
4140
	}
4141 4142 4143
	grant_privileges ON opt_table FROM user_list
	{}
	;
unknown's avatar
unknown committed
4144 4145 4146 4147

grant:
	GRANT
	{
unknown's avatar
unknown committed
4148 4149 4150
	  LEX *lex=Lex;
	  lex->users_list.empty();
	  lex->columns.empty();
4151 4152
	  lex->sql_command = SQLCOM_GRANT;
	  lex->grant= lex->grant_tot_col= 0;
4153
	  lex->select_lex.db= 0;
4154 4155
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
4156
	  bzero(&(lex->mqh),sizeof(lex->mqh));
unknown's avatar
unknown committed
4157 4158
	}
	grant_privileges ON opt_table TO_SYM user_list
4159 4160 4161
	require_clause grant_options
	{}
	;
unknown's avatar
unknown committed
4162 4163 4164

grant_privileges:
	grant_privilege_list {}
unknown's avatar
unknown committed
4165
	| ALL PRIVILEGES	{ Lex->grant = GLOBAL_ACLS;}
4166 4167
	| ALL			{ Lex->grant = GLOBAL_ACLS;}
        ;
unknown's avatar
unknown committed
4168 4169 4170

grant_privilege_list:
	grant_privilege
unknown's avatar
unknown committed
4171
	| grant_privilege_list ',' grant_privilege;
unknown's avatar
unknown committed
4172 4173

grant_privilege:
4174 4175 4176 4177
	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
4178 4179
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
unknown's avatar
unknown committed
4180 4181 4182 4183
	| 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
4184
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
unknown's avatar
unknown committed
4185 4186 4187 4188
	| 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
4189 4190 4191 4192 4193 4194 4195 4196
	| 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
4197

unknown's avatar
unknown committed
4198

4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210
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
4211 4212 4213 4214
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
4215
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "SUBJECT");
unknown's avatar
unknown committed
4216 4217 4218 4219 4220 4221 4222 4223 4224
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
4225
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "ISSUER");
unknown's avatar
unknown committed
4226 4227 4228 4229 4230 4231 4232 4233 4234
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
4235
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "CIPHER");
unknown's avatar
unknown committed
4236 4237 4238 4239 4240
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
4241

unknown's avatar
unknown committed
4242 4243 4244
opt_table:
	'*'
	  {
4245
	    LEX *lex= Lex;
unknown's avatar
unknown committed
4246
	    lex->current_select->select_lex()->db= lex->thd->db;
unknown's avatar
unknown committed
4247
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
4248 4249
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
4250
	    {
4251
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
4252
	      YYABORT;
unknown's avatar
unknown committed
4253
	    }
unknown's avatar
unknown committed
4254 4255 4256
	  }
	| ident '.' '*'
	  {
4257
	    LEX *lex= Lex;
unknown's avatar
unknown committed
4258
	    lex->current_select->select_lex()->db = $1.str;
unknown's avatar
unknown committed
4259
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
4260 4261
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
4262
	    {
4263
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
unknown's avatar
unknown committed
4264 4265 4266 4267 4268
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
4269
	    LEX *lex= Lex;
unknown's avatar
unknown committed
4270
	    lex->current_select->select_lex()->db = NULL;
unknown's avatar
unknown committed
4271 4272
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
unknown's avatar
unknown committed
4273
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
4274
	    {
4275
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
unknown's avatar
unknown committed
4276 4277 4278 4279 4280
	      YYABORT;
	    }
	  }
	| table_ident
	  {
unknown's avatar
unknown committed
4281
	    LEX *lex=Lex;
unknown's avatar
unknown committed
4282
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
unknown's avatar
unknown committed
4283
	      YYABORT;
unknown's avatar
unknown committed
4284
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
4285
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
4286 4287
	  }
          ;
unknown's avatar
unknown committed
4288 4289 4290


user_list:
4291 4292 4293 4294 4295 4296 4297
	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
4298 4299 4300 4301 4302 4303 4304 4305


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
unknown's avatar
unknown committed
4306
	     char *buff=(char*) YYTHD->alloc(HASH_PASSWORD_LENGTH+1);
unknown's avatar
unknown committed
4307 4308
	     if (buff)
	     {
4309
	       make_scrambled_password(buff,$4.str,opt_old_passwords,&current_thd->rand);
unknown's avatar
unknown committed
4310 4311 4312 4313 4314 4315 4316 4317
	       $1->password.str=buff;
	       $1->password.length=HASH_PASSWORD_LENGTH;
	     }
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
	  { $$=$1; $1->password=$5 ; }
	| user
4318 4319
	  { $$=$1; $1->password.str=NullS; }
        ;
unknown's avatar
unknown committed
4320 4321 4322


opt_column_list:
unknown's avatar
unknown committed
4323 4324 4325 4326 4327
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
unknown's avatar
unknown committed
4328
	| '(' column_list ')';
unknown's avatar
unknown committed
4329 4330 4331

column_list:
	column_list ',' column_list_id
unknown's avatar
unknown committed
4332
	| column_list_id;
unknown's avatar
unknown committed
4333 4334 4335 4336

column_list_id:
	ident
	{
4337
	  String *new_str = new String((const char*) $1.str,$1.length,default_charset_info);
unknown's avatar
unknown committed
4338 4339
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
unknown's avatar
unknown committed
4340
	  LEX *lex=Lex;
unknown's avatar
unknown committed
4341 4342
	  while ((point=iter++))
	  {
4343 4344
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
unknown's avatar
unknown committed
4345 4346
		break;
	  }
unknown's avatar
unknown committed
4347
	  lex->grant_tot_col|= lex->which_columns;
unknown's avatar
unknown committed
4348
	  if (point)
unknown's avatar
unknown committed
4349
	    point->rights |= lex->which_columns;
unknown's avatar
unknown committed
4350
	  else
unknown's avatar
unknown committed
4351
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
4352 4353
	}
        ;
unknown's avatar
unknown committed
4354

unknown's avatar
unknown committed
4355 4356

require_clause: /* empty */
4357
        | REQUIRE_SYM require_list
4358 4359 4360
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
4361
        | REQUIRE_SYM SSL_SYM
4362 4363 4364
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
4365
        | REQUIRE_SYM X509_SYM
4366 4367 4368 4369 4370 4371 4372
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
4373
          ;
unknown's avatar
unknown committed
4374

unknown's avatar
unknown committed
4375
grant_options:
unknown's avatar
unknown committed
4376
	/* empty */ {}
unknown's avatar
unknown committed
4377
	| WITH grant_option_list;
unknown's avatar
unknown committed
4378

unknown's avatar
unknown committed
4379 4380
grant_option_list:
	grant_option_list grant_option {}
4381 4382
	| grant_option {}
        ;
unknown's avatar
unknown committed
4383 4384 4385

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
4386
        | MAX_QUERIES_PER_HOUR ULONG_NUM
unknown's avatar
unknown committed
4387
        {
4388
	  Lex->mqh.questions=$2;
4389
	  Lex->mqh.bits |= 1;
4390
	}
4391
        | MAX_UPDATES_PER_HOUR ULONG_NUM
4392
        {
4393
	  Lex->mqh.updates=$2;
4394
	  Lex->mqh.bits |= 2;
4395
	}
4396
        | MAX_CONNECTIONS_PER_HOUR ULONG_NUM
4397
        {
4398
	  Lex->mqh.connections=$2;
4399
	  Lex->mqh.bits |= 4;
4400 4401
	}
        ;
4402

unknown's avatar
unknown committed
4403
begin:
4404 4405
	BEGIN_SYM   { Lex->sql_command = SQLCOM_BEGIN;} opt_work {}
	;
unknown's avatar
unknown committed
4406 4407 4408

opt_work:
	/* empty */ {}
4409 4410
	| WORK_SYM {;}
        ;
unknown's avatar
unknown committed
4411 4412

commit:
unknown's avatar
unknown committed
4413
	COMMIT_SYM   { Lex->sql_command = SQLCOM_COMMIT;};
unknown's avatar
unknown committed
4414 4415

rollback:
unknown's avatar
unknown committed
4416
	ROLLBACK_SYM { Lex->sql_command = SQLCOM_ROLLBACK;};
4417 4418 4419


/*
unknown's avatar
unknown committed
4420
   UNIONS : glue selects together
4421 4422 4423
*/


4424
union_clause:
4425
	/* empty */ {}
unknown's avatar
unknown committed
4426 4427
	| union_list
	;
4428 4429

union_list:
unknown's avatar
unknown committed
4430
	UNION_SYM    union_option
4431 4432 4433 4434 4435
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
4436
	    net_printf(lex->thd, ER_WRONG_USAGE, "UNION", "INTO");
4437
	    YYABORT;
4438
	  }
4439
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
4440
	  {
4441
	    send_error(lex->thd, ER_SYNTAX_ERROR);
4442 4443
	    YYABORT;
	  }
4444
	  if (mysql_new_select(lex, 0))
4445
	    YYABORT;
4446
	  lex->current_select->linkage=UNION_TYPE;
unknown's avatar
unknown committed
4447
	}
4448
	select_init {}
4449
	;
4450 4451

union_opt:
unknown's avatar
unknown committed
4452
	union_list {}
unknown's avatar
unknown committed
4453 4454
	| optional_order_or_limit {}
	;
4455 4456

optional_order_or_limit:
4457
      	/* Empty */ {}
4458 4459
	|
	  {
unknown's avatar
unknown committed
4460
	    LEX *lex=Lex;
4461
	    if (!lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
4462
	    {
4463
	      send_error(lex->thd, ER_SYNTAX_ERROR);
4464 4465
	      YYABORT;
	    }
unknown's avatar
unknown committed
4466
	    SELECT_LEX *sel= lex->current_select->select_lex();
4467
	    sel->master_unit()->global_parameters=
4468 4469 4470 4471
	      sel->master_unit();
	    lex->current_select= sel->master_unit();
	    lex->current_select->select_limit=
	      lex->thd->variables.select_limit;
4472
	  }
4473 4474 4475 4476 4477 4478 4479
	order_or_limit
	;

order_or_limit:
	order_clause opt_limit_clause
	|
	limit_clause
4480
	;
unknown's avatar
unknown committed
4481 4482

union_option:
4483
	/* empty */ {}
unknown's avatar
unknown committed
4484
	| ALL {Select->master_unit()->union_option= 1;};
4485

unknown's avatar
unknown committed
4486
singleval_subselect:
unknown's avatar
unknown committed
4487 4488 4489 4490 4491
	subselect_start singleval_subselect_init
	subselect_end
	{
	  $$= $2;
	};
4492

unknown's avatar
unknown committed
4493
singleval_subselect_init:
4494
	select_init2
unknown's avatar
unknown committed
4495
	{
4496
	  $$= new Item_singleval_subselect(YYTHD,
4497 4498
					   Lex->current_select->master_unit()->
                                             first_select());
unknown's avatar
unknown committed
4499
	};
unknown's avatar
unknown committed
4500 4501

exists_subselect:
unknown's avatar
unknown committed
4502 4503 4504 4505 4506
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
unknown's avatar
unknown committed
4507 4508

exists_subselect_init:
4509
	select_init2
unknown's avatar
unknown committed
4510
	{
4511
	  $$= new Item_exists_subselect(YYTHD,
4512 4513
					Lex->current_select->master_unit()->
					  first_select());
unknown's avatar
unknown committed
4514
	};
4515

unknown's avatar
unknown committed
4516 4517 4518 4519 4520 4521 4522 4523
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
4524
  select_init2
unknown's avatar
unknown committed
4525
  {
unknown's avatar
unknown committed
4526
    $$= Lex->current_select->master_unit()->first_select();
unknown's avatar
unknown committed
4527 4528
  };

4529
subselect_start:
4530
	'(' SELECT_SYM
unknown's avatar
unknown committed
4531 4532 4533 4534
	{
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
4535 4536

subselect_end:
unknown's avatar
unknown committed
4537 4538 4539
	')'
	{
	  LEX *lex=Lex;
4540
	  lex->current_select = lex->current_select->outer_select();
unknown's avatar
unknown committed
4541
	};