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

unknown's avatar
unknown committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
7

unknown's avatar
unknown committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
12

unknown's avatar
unknown committed
13 14 15 16
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

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

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

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

44
int yylex(void *yylval, void *yythd);
unknown's avatar
unknown committed
45

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

unknown's avatar
unknown committed
48 49 50 51 52
#define WARN_DEPRECATED(A,B) \
  push_warning_printf(((THD *)yythd), MYSQL_ERROR::WARN_LEVEL_WARN, \
		      ER_WARN_DEPRECATED_SYNTAX, \
		      ER(ER_WARN_DEPRECATED_SYNTAX), (A), (B)); 

53 54
/* Helper for parsing "IS [NOT] truth_value" */
inline Item *is_truth_value(Item *A, bool v1, bool v2)
unknown's avatar
unknown committed
55
{
56 57 58 59
  return new Item_func_if(create_func_ifnull(A,
	new Item_int((char *) (v2 ? "TRUE" : "FALSE"), v2, 1)),
  	new Item_int((char *) (v1 ? "TRUE" : "FALSE"), v1, 1),
	new Item_int((char *) (v1 ? "FALSE" : "TRUE"),!v1, 1));
unknown's avatar
unknown committed
60 61 62 63 64 65
}

%}
%union {
  int  num;
  ulong ulong_num;
unknown's avatar
unknown committed
66
  ulonglong ulonglong_number;
unknown's avatar
unknown committed
67 68 69 70 71 72
  LEX_STRING lex_str;
  LEX_STRING *lex_str_ptr;
  LEX_SYMBOL symbol;
  Table_ident *table;
  char *simple_string;
  Item *item;
73
  Item_num *item_num;
unknown's avatar
unknown committed
74 75
  List<Item> *item_list;
  List<String> *string_list;
unknown's avatar
unknown committed
76 77 78 79 80
  String *string;
  key_part_spec *key_part;
  TABLE_LIST *table_list;
  udf_func *udf;
  LEX_USER *lex_user;
unknown's avatar
unknown committed
81
  struct sys_var_with_base variable;
unknown's avatar
unknown committed
82
  Key::Keytype key_type;
unknown's avatar
unknown committed
83
  enum ha_key_alg key_alg;
unknown's avatar
unknown committed
84 85
  enum db_type db_type;
  enum row_type row_type;
unknown's avatar
unknown committed
86
  enum ha_rkey_function ha_rkey_mode;
unknown's avatar
unknown committed
87
  enum enum_tx_isolation tx_isolation;
88
  enum Cast_target cast_type;
unknown's avatar
unknown committed
89
  enum Item_udftype udf_type;
90
  CHARSET_INFO *charset;
unknown's avatar
unknown committed
91
  thr_lock_type lock_type;
92
  interval_type interval, interval_time_st;
93
  timestamp_type date_time_type;
unknown's avatar
unknown committed
94
  st_select_lex *select_lex;
unknown's avatar
unknown committed
95
  chooser_compare_func_creator boolfunc2creator;
96 97 98 99
  struct sp_cond_type *spcondtype;
  struct { int vars, conds, hndlrs, curs; } spblock;
  sp_name *spname;
  struct st_lex *lex;
unknown's avatar
unknown committed
100 101 102
}

%{
103
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
unknown's avatar
unknown committed
104 105 106 107 108 109
%}

%pure_parser					/* We have threads */

%token	END_OF_INPUT

110 111 112 113 114 115
%token CLOSE_SYM
%token HANDLER_SYM
%token LAST_SYM
%token NEXT_SYM
%token PREV_SYM

116
%token	DIV_SYM
unknown's avatar
unknown committed
117 118
%token	EQ
%token	EQUAL_SYM
unknown's avatar
unknown committed
119
%token	SOUNDS_SYM
unknown's avatar
unknown committed
120 121 122 123 124 125
%token	GE
%token	GT_SYM
%token	LE
%token	LT
%token	NE
%token	IS
126
%token	MOD_SYM
unknown's avatar
unknown committed
127 128 129 130
%token	SHIFT_LEFT
%token	SHIFT_RIGHT
%token  SET_VAR

unknown's avatar
unknown committed
131
%token	ABORT_SYM
unknown's avatar
unknown committed
132 133
%token	ADD
%token	AFTER_SYM
unknown's avatar
unknown committed
134 135
%token	ALTER
%token	ANALYZE_SYM
unknown's avatar
unknown committed
136
%token	ANY_SYM
unknown's avatar
unknown committed
137 138 139
%token	AVG_SYM
%token	BEGIN_SYM
%token	BINLOG_SYM
140
%token  CALL_SYM
unknown's avatar
unknown committed
141
%token	CHANGE
unknown's avatar
unknown committed
142 143 144
%token	CLIENT_SYM
%token	COMMENT_SYM
%token	COMMIT_SYM
145
%token  CONSISTENT_SYM
unknown's avatar
unknown committed
146
%token	COUNT_SYM
unknown's avatar
unknown committed
147 148
%token	CREATE
%token	CROSS
149
%token  CUBE_SYM
150
%token  DEFINER_SYM
unknown's avatar
unknown committed
151
%token	DELETE_SYM
152
%token  DETERMINISTIC_SYM
153
%token	DUAL_SYM
unknown's avatar
unknown committed
154
%token	DO_SYM
unknown's avatar
unknown committed
155
%token	DROP
unknown's avatar
unknown committed
156 157
%token	EVENTS_SYM
%token	EXECUTE_SYM
158
%token	EXPANSION_SYM
unknown's avatar
unknown committed
159
%token	FLUSH_SYM
160
%token  HELP_SYM
unknown's avatar
unknown committed
161
%token	INSERT
unknown's avatar
unknown committed
162
%token	RELAY_THREAD
unknown's avatar
unknown committed
163 164
%token	KILL_SYM
%token	LOAD
unknown's avatar
unknown committed
165
%token	LOCKS_SYM
unknown's avatar
unknown committed
166 167 168 169
%token	LOCK_SYM
%token	MASTER_SYM
%token	MAX_SYM
%token	MIN_SYM
170
%token	NONE_SYM
unknown's avatar
unknown committed
171 172 173 174 175 176
%token	OPTIMIZE
%token	PURGE
%token	REPAIR
%token	REPLICATION
%token	RESET_SYM
%token	ROLLBACK_SYM
177
%token  ROLLUP_SYM
178
%token	SAVEPOINT_SYM
unknown's avatar
unknown committed
179 180 181
%token	SELECT_SYM
%token	SHOW
%token	SLAVE
182
%token  SNAPSHOT_SYM
183
%token  SQL_SYM
unknown's avatar
unknown committed
184 185 186
%token	SQL_THREAD
%token	START_SYM
%token	STD_SYM
unknown's avatar
unknown committed
187
%token  VARIANCE_SYM
unknown's avatar
unknown committed
188 189
%token	STOP_SYM
%token	SUM_SYM
unknown's avatar
unknown committed
190
%token	ADDDATE_SYM
unknown's avatar
unknown committed
191 192
%token	SUPER_SYM
%token	TRUNCATE_SYM
unknown's avatar
unknown committed
193
%token	UNLOCK_SYM
194
%token	UNTIL_SYM
unknown's avatar
unknown committed
195
%token	UPDATE_SYM
unknown's avatar
unknown committed
196 197 198

%token	ACTION
%token	AGGREGATE_SYM
199
%token	ALGORITHM_SYM
unknown's avatar
unknown committed
200
%token	ALL
unknown's avatar
unknown committed
201
%token	AND_SYM
202
%token	AND_AND_SYM
unknown's avatar
unknown committed
203 204
%token	AS
%token	ASC
205
%token	AUTO_INC
unknown's avatar
unknown committed
206
%token	AVG_ROW_LENGTH
unknown's avatar
unknown committed
207
%token	BACKUP_SYM
unknown's avatar
unknown committed
208 209
%token	BERKELEY_DB_SYM
%token	BINARY
unknown's avatar
unknown committed
210
%token  BIN_NUM
unknown's avatar
unknown committed
211 212
%token	BIT_SYM
%token	BOOL_SYM
unknown's avatar
unknown committed
213
%token	BOOLEAN_SYM
unknown's avatar
unknown committed
214
%token	BOTH
unknown's avatar
unknown committed
215
%token	BTREE_SYM
unknown's avatar
unknown committed
216
%token	BY
217
%token	BYTE_SYM
unknown's avatar
unknown committed
218
%token	CACHE_SYM
unknown's avatar
unknown committed
219
%token	CASCADE
220
%token  CASCADED
unknown's avatar
unknown committed
221
%token	CAST_SYM
unknown's avatar
unknown committed
222
%token	CHAIN_SYM
223
%token	CHARSET
unknown's avatar
unknown committed
224 225
%token	CHECKSUM_SYM
%token	CHECK_SYM
unknown's avatar
unknown committed
226
%token	COMMITTED_SYM
unknown's avatar
unknown committed
227
%token	COLLATE_SYM
unknown's avatar
unknown committed
228
%token	COLLATION_SYM
unknown's avatar
unknown committed
229 230
%token	COLUMNS
%token	COLUMN_SYM
231
%token	COMPACT_SYM
unknown's avatar
unknown committed
232
%token	CONCURRENT
233 234
%token  CONDITION_SYM
%token	CONNECTION_SYM
unknown's avatar
unknown committed
235
%token	CONSTRAINT
236 237
%token  CONTAINS_SYM
%token  CONTINUE_SYM
unknown's avatar
unknown committed
238
%token	CONVERT_SYM
239
%token  CURRENT_USER
unknown's avatar
unknown committed
240 241
%token	DATABASES
%token	DATA_SYM
242
%token  DECLARE_SYM
unknown's avatar
unknown committed
243 244 245 246 247
%token	DEFAULT
%token	DELAYED_SYM
%token	DELAY_KEY_WRITE_SYM
%token	DESC
%token	DESCRIBE
unknown's avatar
unknown committed
248
%token	DES_KEY_FILE
249
%token	DISABLE_SYM
unknown's avatar
unknown committed
250
%token	DISCARD
unknown's avatar
unknown committed
251
%token	DISTINCT
unknown's avatar
unknown committed
252
%token  DUPLICATE_SYM
unknown's avatar
unknown committed
253
%token	DYNAMIC_SYM
254
%token  EACH_SYM
255
%token	ENABLE_SYM
unknown's avatar
unknown committed
256 257
%token	ENCLOSED
%token	ESCAPED
unknown's avatar
unknown committed
258
%token	DIRECTORY_SYM
unknown's avatar
unknown committed
259 260
%token	ESCAPE_SYM
%token	EXISTS
261
%token  EXIT_SYM
unknown's avatar
unknown committed
262
%token	EXTENDED_SYM
263
%token	FALSE_SYM
264
%token  FETCH_SYM
unknown's avatar
unknown committed
265 266 267 268
%token	FILE_SYM
%token	FIRST_SYM
%token	FIXED_SYM
%token	FLOAT_NUM
269
%token	FORCE_SYM
unknown's avatar
unknown committed
270
%token	FOREIGN
271
%token  FOUND_SYM
unknown's avatar
unknown committed
272 273
%token	FROM
%token	FULL
unknown's avatar
unknown committed
274 275
%token	FULLTEXT_SYM
%token	GLOBAL_SYM
unknown's avatar
unknown committed
276 277 278 279 280
%token	GRANT
%token	GRANTS
%token	GREATEST_SYM
%token	GROUP
%token	HAVING
unknown's avatar
unknown committed
281
%token	HASH_SYM
unknown's avatar
unknown committed
282 283 284 285
%token	HEX_NUM
%token	HIGH_PRIORITY
%token	HOSTS_SYM
%token	IDENT
286
%token	IDENT_QUOTED
unknown's avatar
unknown committed
287
%token	IGNORE_SYM
unknown's avatar
unknown committed
288
%token	IMPORT
unknown's avatar
unknown committed
289
%token	INDEX_SYM
unknown's avatar
unknown committed
290
%token	INDEXES
unknown's avatar
unknown committed
291 292
%token	INFILE
%token	INNER_SYM
unknown's avatar
unknown committed
293
%token	INNOBASE_SYM
294
%token  INOUT_SYM
unknown's avatar
unknown committed
295 296
%token	INTO
%token	IN_SYM
297
%token  INVOKER_SYM
unknown's avatar
unknown committed
298
%token	ISOLATION
unknown's avatar
unknown committed
299 300 301 302 303
%token	JOIN_SYM
%token	KEYS
%token	KEY_SYM
%token	LEADING
%token	LEAST_SYM
unknown's avatar
unknown committed
304
%token	LEAVES
unknown's avatar
unknown committed
305
%token	LEVEL_SYM
unknown's avatar
unknown committed
306
%token	LEX_HOSTNAME
307
%token  LANGUAGE_SYM
unknown's avatar
unknown committed
308 309 310
%token	LIKE
%token	LINES
%token	LOCAL_SYM
311
%token  LOCATOR_SYM
312
%token	LOG_SYM
unknown's avatar
unknown committed
313 314 315 316
%token	LOGS_SYM
%token	LONG_NUM
%token	LONG_SYM
%token	LOW_PRIORITY
317
%token	MERGE_SYM
unknown's avatar
unknown committed
318 319 320 321 322 323 324 325
%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
unknown's avatar
unknown committed
326 327 328 329 330 331
%token	MASTER_SSL_SYM
%token	MASTER_SSL_CA_SYM
%token	MASTER_SSL_CAPATH_SYM
%token	MASTER_SSL_CERT_SYM
%token	MASTER_SSL_CIPHER_SYM
%token	MASTER_SSL_KEY_SYM
unknown's avatar
unknown committed
332 333
%token	RELAY_LOG_FILE_SYM
%token	RELAY_LOG_POS_SYM
unknown's avatar
unknown committed
334 335
%token	MATCH
%token	MAX_ROWS
unknown's avatar
unknown committed
336 337 338
%token	MAX_CONNECTIONS_PER_HOUR
%token	MAX_QUERIES_PER_HOUR
%token	MAX_UPDATES_PER_HOUR
339
%token	MAX_USER_CONNECTIONS_SYM
340
%token	MEDIUM_SYM
unknown's avatar
unknown committed
341
%token	MIN_ROWS
unknown's avatar
unknown committed
342
%token  MUTEX_SYM
unknown's avatar
unknown committed
343
%token	NAMES_SYM
344
%token	NAME_SYM
unknown's avatar
unknown committed
345 346
%token	NATIONAL_SYM
%token	NATURAL
unknown's avatar
unknown committed
347
%token  NDBCLUSTER_SYM
unknown's avatar
unknown committed
348
%token	NEW_SYM
unknown's avatar
unknown committed
349
%token	NCHAR_SYM
unknown's avatar
unknown committed
350
%token	NCHAR_STRING
unknown's avatar
unknown committed
351
%token  NVARCHAR_SYM
352 353
%token	NOT_SYM
%token	NOT2_SYM
unknown's avatar
unknown committed
354 355 356
%token	NO_SYM
%token	NULL_SYM
%token	NUM
unknown's avatar
unknown committed
357
%token	OFFSET_SYM
unknown's avatar
unknown committed
358
%token	ON
359
%token  ONE_SHOT_SYM
360
%token	OPEN_SYM
unknown's avatar
unknown committed
361 362
%token	OPTION
%token	OPTIONALLY
unknown's avatar
unknown committed
363
%token	OR_SYM
364 365
%token	OR2_SYM
%token	OR_OR_SYM
unknown's avatar
unknown committed
366
%token	ORDER_SYM
367
%token  OUT_SYM
unknown's avatar
unknown committed
368 369
%token	OUTER
%token	OUTFILE
unknown's avatar
unknown committed
370
%token	DUMPFILE
unknown's avatar
unknown committed
371 372 373 374 375 376
%token	PACK_KEYS_SYM
%token	PARTIAL
%token	PRIMARY_SYM
%token	PRIVILEGES
%token	PROCESS
%token	PROCESSLIST_SYM
unknown's avatar
unknown committed
377
%token	QUERY_SYM
unknown's avatar
unknown committed
378
%token	RAID_0_SYM
379 380
%token	RAID_STRIPED_SYM
%token	RAID_TYPE
unknown's avatar
unknown committed
381 382 383
%token	RAID_CHUNKS
%token	RAID_CHUNKSIZE
%token	READ_SYM
384
%token	READS_SYM
unknown's avatar
unknown committed
385
%token	REAL_NUM
386
%token	REDUNDANT_SYM
unknown's avatar
unknown committed
387 388
%token	REFERENCES
%token	REGEXP
unknown's avatar
unknown committed
389
%token	RELEASE_SYM
unknown's avatar
unknown committed
390 391
%token	RELOAD
%token	RENAME
unknown's avatar
unknown committed
392
%token	REPEATABLE_SYM
unknown's avatar
unknown committed
393
%token	REQUIRE_SYM
unknown's avatar
unknown committed
394 395
%token	RESOURCES
%token	RESTORE_SYM
unknown's avatar
unknown committed
396 397
%token	RESTRICT
%token	REVOKE
398
%token	ROUTINE_SYM
unknown's avatar
unknown committed
399 400 401
%token	ROWS_SYM
%token	ROW_FORMAT_SYM
%token	ROW_SYM
unknown's avatar
unknown committed
402
%token	RTREE_SYM
403
%token  SECURITY_SYM
unknown's avatar
unknown committed
404
%token	SET
405
%token  SEPARATOR_SYM
406
%token	SERIAL_SYM
unknown's avatar
unknown committed
407 408
%token	SERIALIZABLE_SYM
%token	SESSION_SYM
409
%token	SIMPLE_SYM
unknown's avatar
unknown committed
410
%token	SHUTDOWN
unknown's avatar
unknown committed
411
%token	SPATIAL_SYM
412 413 414 415
%token  SPECIFIC_SYM
%token  SQLEXCEPTION_SYM
%token  SQLSTATE_SYM
%token  SQLWARNING_SYM
416
%token  SSL_SYM
unknown's avatar
unknown committed
417 418
%token	STARTING
%token	STATUS_SYM
unknown's avatar
unknown committed
419
%token	STORAGE_SYM
unknown's avatar
unknown committed
420
%token	STRAIGHT_JOIN
unknown's avatar
unknown committed
421
%token	SUBJECT_SYM
unknown's avatar
unknown committed
422 423
%token	TABLES
%token	TABLE_SYM
unknown's avatar
unknown committed
424
%token	TABLESPACE
unknown's avatar
unknown committed
425
%token	TEMPORARY
426
%token	TEMPTABLE_SYM
unknown's avatar
unknown committed
427 428
%token	TERMINATED
%token	TEXT_STRING
429 430 431
%token	TO_SYM
%token	TRAILING
%token	TRANSACTION_SYM
432
%token  TRIGGER_SYM
433
%token	TRUE_SYM
434
%token	TYPE_SYM
unknown's avatar
unknown committed
435
%token  TYPES_SYM
unknown's avatar
unknown committed
436 437 438 439
%token	FUNC_ARG0
%token	FUNC_ARG1
%token	FUNC_ARG2
%token	FUNC_ARG3
440 441
%token	RETURN_SYM
%token	RETURNS_SYM
442
%token	UDF_SONAME_SYM
443 444
%token	UDF_RETURNS_SYM
%token	FUNCTION_SYM
unknown's avatar
unknown committed
445
%token	UNCOMMITTED_SYM
446
%token	UNDEFINED_SYM
447
%token	UNDERSCORE_CHARSET
448
%token  UNDO_SYM
unknown's avatar
unknown committed
449
%token	UNICODE_SYM
450 451
%token	UNION_SYM
%token	UNIQUE_SYM
452
%token	UNKNOWN_SYM
453
%token	USAGE
unknown's avatar
unknown committed
454
%token	USE_FRM
455 456
%token	USE_SYM
%token	USING
457
%token	VALUE_SYM
458
%token	VALUES
unknown's avatar
unknown committed
459
%token	VARIABLES
460
%token	VIEW_SYM
unknown's avatar
unknown committed
461 462 463
%token	WHERE
%token	WITH
%token	WRITE_SYM
464
%token  NO_WRITE_TO_BINLOG
unknown's avatar
unknown committed
465
%token	X509_SYM
unknown's avatar
unknown committed
466
%token	XOR
unknown's avatar
unknown committed
467
%token	COMPRESSED_SYM
468
%token  ROW_COUNT_SYM
unknown's avatar
unknown committed
469

unknown's avatar
unknown committed
470 471 472
%token  ERRORS
%token  WARNINGS

unknown's avatar
unknown committed
473
%token	ASCII_SYM
unknown's avatar
unknown committed
474 475 476
%token	BIGINT
%token	BLOB_SYM
%token	CHAR_SYM
unknown's avatar
unknown committed
477
%token	CHANGED
unknown's avatar
unknown committed
478 479 480 481 482 483
%token	COALESCE
%token	DATETIME
%token	DATE_SYM
%token	DECIMAL_SYM
%token	DOUBLE_SYM
%token	ENUM
484
%token	FAST_SYM
unknown's avatar
unknown committed
485
%token	FLOAT_SYM
unknown's avatar
unknown committed
486
%token  GEOMETRY_SYM
unknown's avatar
unknown committed
487 488 489 490 491 492 493 494 495
%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
496 497
%token  PREPARE_SYM
%token  DEALLOCATE_SYM
unknown's avatar
unknown committed
498
%token	QUICK
unknown's avatar
unknown committed
499
%token	REAL
unknown's avatar
unknown committed
500
%token	SIGNED_SYM
unknown's avatar
unknown committed
501 502 503 504
%token	SMALLINT
%token	STRING_SYM
%token	TEXT_SYM
%token	TIMESTAMP
505 506
%token	TIMESTAMP_ADD
%token	TIMESTAMP_DIFF
unknown's avatar
unknown committed
507 508 509 510
%token	TIME_SYM
%token	TINYBLOB
%token	TINYINT
%token	TINYTEXT
unknown's avatar
unknown committed
511
%token	ULONGLONG_NUM
unknown's avatar
unknown committed
512
%token	UNSIGNED
513 514 515
%token	VARBINARY
%token	VARCHAR
%token	VARYING
unknown's avatar
unknown committed
516 517
%token	ZEROFILL

unknown's avatar
unknown committed
518
%token	ADDDATE_SYM
unknown's avatar
unknown committed
519
%token	AGAINST
unknown's avatar
unknown committed
520 521 522 523
%token	ATAN
%token	BETWEEN_SYM
%token	BIT_AND
%token	BIT_OR
524
%token  BIT_XOR
unknown's avatar
unknown committed
525 526
%token	CASE_SYM
%token	CONCAT
unknown's avatar
unknown committed
527
%token	CONCAT_WS
528
%token  CONVERT_TZ_SYM
unknown's avatar
unknown committed
529 530 531 532 533 534
%token	CURDATE
%token	CURTIME
%token	DATABASE
%token	DATE_ADD_INTERVAL
%token	DATE_SUB_INTERVAL
%token	DAY_HOUR_SYM
unknown's avatar
unknown committed
535
%token	DAY_MICROSECOND_SYM
unknown's avatar
unknown committed
536 537 538 539
%token	DAY_MINUTE_SYM
%token	DAY_SECOND_SYM
%token	DAY_SYM
%token	DECODE_SYM
unknown's avatar
unknown committed
540 541
%token	DES_ENCRYPT_SYM
%token	DES_DECRYPT_SYM
unknown's avatar
unknown committed
542 543 544
%token	ELSE
%token	ELT_FUNC
%token	ENCODE_SYM
unknown's avatar
unknown committed
545
%token	ENGINE_SYM
unknown's avatar
unknown committed
546
%token	ENGINES_SYM
unknown's avatar
unknown committed
547 548 549 550 551 552
%token	ENCRYPT
%token	EXPORT_SET
%token	EXTRACT_SYM
%token	FIELD_FUNC
%token	FORMAT_SYM
%token	FOR_SYM
553
%token  FRAC_SECOND_SYM
unknown's avatar
unknown committed
554
%token	FROM_UNIXTIME
unknown's avatar
unknown committed
555 556
%token	GEOMCOLLFROMTEXT
%token	GEOMFROMTEXT
unknown's avatar
unknown committed
557
%token	GEOMFROMWKB
unknown's avatar
unknown committed
558
%token  GEOMETRYCOLLECTION
559
%token  GROUP_CONCAT_SYM
unknown's avatar
unknown committed
560
%token	GROUP_UNIQUE_USERS
561
%token  GET_FORMAT
unknown's avatar
unknown committed
562
%token	HOUR_MICROSECOND_SYM
unknown's avatar
unknown committed
563 564 565 566 567
%token	HOUR_MINUTE_SYM
%token	HOUR_SECOND_SYM
%token	HOUR_SYM
%token	IDENTIFIED_SYM
%token	IF
568
%token	INSERT_METHOD
unknown's avatar
unknown committed
569 570 571
%token	INTERVAL_SYM
%token	LAST_INSERT_ID
%token	LEFT
unknown's avatar
unknown committed
572
%token	LINEFROMTEXT
unknown's avatar
unknown committed
573
%token  LINESTRING
unknown's avatar
unknown committed
574 575
%token	LOCATE
%token	MAKE_SET_SYM
576
%token	MASTER_POS_WAIT
unknown's avatar
unknown committed
577 578
%token  MICROSECOND_SYM
%token	MINUTE_MICROSECOND_SYM
unknown's avatar
unknown committed
579 580
%token	MINUTE_SECOND_SYM
%token	MINUTE_SYM
unknown's avatar
unknown committed
581
%token	MODE_SYM
582
%token	MODIFIES_SYM
unknown's avatar
unknown committed
583 584
%token	MODIFY_SYM
%token	MONTH_SYM
unknown's avatar
unknown committed
585 586 587
%token	MLINEFROMTEXT
%token	MPOINTFROMTEXT
%token	MPOLYFROMTEXT
unknown's avatar
unknown committed
588 589 590
%token  MULTILINESTRING
%token  MULTIPOINT
%token  MULTIPOLYGON
unknown's avatar
unknown committed
591
%token	NOW_SYM
unknown's avatar
unknown committed
592
%token	OLD_PASSWORD
unknown's avatar
unknown committed
593
%token	PASSWORD
unknown's avatar
unknown committed
594
%token	POINTFROMTEXT
595
%token	POINT_SYM
unknown's avatar
unknown committed
596
%token	POLYFROMTEXT
unknown's avatar
unknown committed
597
%token  POLYGON
unknown's avatar
unknown committed
598 599
%token	POSITION_SYM
%token	PROCEDURE
600
%token	QUARTER_SYM
unknown's avatar
unknown committed
601 602 603 604 605
%token	RAND
%token	REPLACE
%token	RIGHT
%token	ROUND
%token	SECOND_SYM
unknown's avatar
unknown committed
606
%token	SECOND_MICROSECOND_SYM
unknown's avatar
unknown committed
607
%token	SHARE_SYM
unknown's avatar
unknown committed
608
%token	SUBDATE_SYM
unknown's avatar
unknown committed
609 610 611 612 613 614
%token	SUBSTRING
%token	SUBSTRING_INDEX
%token	TRIM
%token	UNIQUE_USERS
%token	UNIX_TIMESTAMP
%token	USER
615 616 617
%token	UTC_DATE_SYM
%token	UTC_TIME_SYM
%token	UTC_TIMESTAMP_SYM
unknown's avatar
unknown committed
618 619
%token	WEEK_SYM
%token	WHEN_SYM
unknown's avatar
unknown committed
620
%token	WORK_SYM
unknown's avatar
unknown committed
621 622 623
%token	YEAR_MONTH_SYM
%token	YEAR_SYM
%token	YEARWEEK
unknown's avatar
unknown committed
624 625 626
%token	BENCHMARK_SYM
%token	END
%token	THEN_SYM
unknown's avatar
unknown committed
627

unknown's avatar
unknown committed
628 629 630 631
%token	SQL_BIG_RESULT
%token	SQL_CACHE_SYM
%token	SQL_CALC_FOUND_ROWS
%token	SQL_NO_CACHE_SYM
unknown's avatar
unknown committed
632
%token	SQL_SMALL_RESULT
unknown's avatar
unknown committed
633
%token  SQL_BUFFER_RESULT
unknown's avatar
unknown committed
634

635 636 637 638 639 640 641 642 643 644 645 646 647 648
%token  CURSOR_SYM
%token  ELSEIF_SYM
%token  ITERATE_SYM
%token  GOTO_SYM
%token  LABEL_SYM
%token  LEAVE_SYM
%token  LOOP_SYM
%token  REPEAT_SYM
%token  UNTIL_SYM
%token  WHILE_SYM
%token  ASENSITIVE_SYM
%token  INSENSITIVE_SYM
%token  SENSITIVE_SYM

unknown's avatar
unknown committed
649 650
%token  ISSUER_SYM
%token  SUBJECT_SYM
651
%token  CIPHER_SYM
unknown's avatar
unknown committed
652

653
%token  BEFORE_SYM
unknown's avatar
unknown committed
654
%left   SET_VAR
655 656
%left	OR_OR_SYM OR_SYM OR2_SYM XOR
%left	AND_SYM AND_AND_SYM
unknown's avatar
unknown committed
657 658 659 660 661 662
%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	'-' '+'
663
%left	'*' '/' '%' DIV_SYM MOD_SYM
unknown's avatar
unknown committed
664
%left   '^'
unknown's avatar
unknown committed
665
%left	NEG '~'
666
%right	NOT_SYM NOT2_SYM
unknown's avatar
unknown committed
667
%right	BINARY COLLATE_SYM
668

unknown's avatar
unknown committed
669
%type <lex_str>
670 671
	IDENT IDENT_QUOTED TEXT_STRING REAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM
	LEX_HOSTNAME ULONGLONG_NUM field_ident select_alias ident ident_or_text
672
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
673
	NCHAR_STRING opt_component key_cache_name
unknown's avatar
unknown committed
674
        sp_opt_label BIN_NUM
unknown's avatar
unknown committed
675 676 677 678 679

%type <lex_str_ptr>
	opt_table_alias

%type <table>
680
	table_ident table_ident_nodb references
unknown's avatar
unknown committed
681 682

%type <simple_string>
683
	remember_name remember_end opt_ident opt_db text_or_password
684
	opt_constraint constraint ident_or_empty
unknown's avatar
unknown committed
685 686

%type <string>
687
	text_string opt_gconcat_separator
unknown's avatar
unknown committed
688 689

%type <num>
unknown's avatar
unknown committed
690
	type int_type real_type order_dir opt_field_spec lock_option
unknown's avatar
unknown committed
691
	udf_type if_exists opt_local opt_table_options table_options
692 693
        table_option opt_if_not_exists opt_no_write_to_binlog opt_var_type
        opt_var_ident_type delete_option opt_temporary all_or_any opt_distinct
694
        opt_ignore_leaves fulltext_options spatial_type union_option
unknown's avatar
unknown committed
695
        start_transaction_opts opt_chain opt_work_and_chain opt_release
unknown's avatar
unknown committed
696 697

%type <ulong_num>
698
	ULONG_NUM raid_types merge_insert_types
unknown's avatar
unknown committed
699

unknown's avatar
unknown committed
700 701
%type <ulonglong_number>
	ulonglong_num
unknown's avatar
unknown committed
702

unknown's avatar
unknown committed
703 704 705
%type <lock_type>
	replace_lock_option opt_low_priority insert_lock_option load_data_lock

unknown's avatar
unknown committed
706
%type <item>
unknown's avatar
unknown committed
707
	literal text_literal insert_ident order_ident
unknown's avatar
unknown committed
708
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
709 710 711
	bool_term bool_factor bool_test bool_pri 
	predicate bit_expr bit_term bit_factor value_expr term factor
	table_wild simple_expr udf_expr
712
	using_list expr_or_default set_expr_or_default interval_expr
unknown's avatar
unknown committed
713
	param_marker singlerow_subselect singlerow_subselect_init
unknown's avatar
unknown committed
714
	exists_subselect exists_subselect_init geometry_function
715
	signed_literal now_or_signed_literal opt_escape
716 717
	sp_opt_default
	simple_ident_nospvar simple_ident_q
718 719 720

%type <item_num>
	NUM_literal
unknown's avatar
unknown committed
721 722

%type <item_list>
723 724
	expr_list udf_expr_list udf_expr_list2 when_list
	ident_list ident_list_arg
unknown's avatar
unknown committed
725 726

%type <key_type>
727
	key_type opt_unique_or_fulltext constraint_key_type
unknown's avatar
unknown committed
728

unknown's avatar
unknown committed
729 730 731
%type <key_alg>
	key_alg opt_btree_or_rtree

unknown's avatar
unknown committed
732 733 734 735 736 737 738
%type <string_list>
	key_usage_list

%type <key_part>
	key_part

%type <table_list>
739
	join_table_list  join_table
740
        table_factor table_ref
unknown's avatar
unknown committed
741

742
%type <date_time_type> date_time_type;
unknown's avatar
unknown committed
743 744
%type <interval> interval

745 746
%type <interval_time_st> interval_time_st

unknown's avatar
unknown committed
747
%type <db_type> storage_engines
unknown's avatar
unknown committed
748 749 750

%type <row_type> row_types

unknown's avatar
unknown committed
751
%type <tx_isolation> isolation_types
unknown's avatar
unknown committed
752

unknown's avatar
unknown committed
753 754
%type <ha_rkey_mode> handler_rkey_mode

755
%type <cast_type> cast_type
unknown's avatar
unknown committed
756

unknown's avatar
unknown committed
757 758 759 760 761 762
%type <udf_type> udf_func_type

%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword

%type <lex_user> user grant_user

763
%type <charset>
764
	opt_collate
765 766
	charset_name
	charset_name_or_default
unknown's avatar
unknown committed
767 768
	old_or_new_charset_name
	old_or_new_charset_name_or_default
769 770
	collation_name
	collation_name_or_default
771

unknown's avatar
unknown committed
772 773
%type <variable> internal_variable_name

unknown's avatar
unknown committed
774 775
%type <select_lex> in_subselect in_subselect_init

unknown's avatar
unknown committed
776 777
%type <boolfunc2creator> comp_op

unknown's avatar
unknown committed
778
%type <NONE>
unknown's avatar
unknown committed
779
	query verb_clause create change select do drop insert replace insert2
780
	insert_values update delete truncate rename
unknown's avatar
unknown committed
781
	show describe load alter optimize keycache preload flush
unknown's avatar
unknown committed
782
	reset purge begin commit rollback savepoint release
783
	slave master_def master_defs master_file_def slave_until_opts
784
	repair restore backup analyze check start checksum
785
	field_list field_list_item field_spec kill column_def key_def
unknown's avatar
unknown committed
786
	keycache_list assign_to_keycache preload_list preload_keys
unknown's avatar
unknown committed
787
	select_item_list select_item values_list no_braces
788
	opt_limit_clause delete_limit_clause fields opt_values values
unknown's avatar
unknown committed
789
	procedure_list procedure_list2 procedure_item
790
	when_list2 expr_list2 udf_expr_list3 handler
unknown's avatar
unknown committed
791 792
	opt_precision opt_ignore opt_column opt_restrict
	grant revoke set lock unlock string_list field_options field_option
unknown's avatar
unknown committed
793
	field_opt_list opt_binary table_lock_list table_lock
794
	ref_list opt_on_delete opt_on_delete_list opt_on_delete_item use
unknown's avatar
unknown committed
795
	opt_delete_options opt_delete_option varchar nchar nvarchar
unknown's avatar
unknown committed
796
	opt_outer table_list table_name opt_option opt_place
unknown's avatar
unknown committed
797
	opt_attribute opt_attribute_list attribute column_list column_list_id
798
	opt_column_list grant_privileges opt_table grant_list grant_option
799
	object_privilege object_privilege_list user_list rename_list
800
	clear_privileges flush_options flush_option
unknown's avatar
unknown committed
801 802
	equal optional_braces opt_key_definition key_usage_list2
	opt_mi_check_type opt_to mi_check_types normal_join
803
	table_to_table_list table_to_table opt_table_list opt_as
unknown's avatar
unknown committed
804
	handler_rkey_function handler_read_or_scan
unknown's avatar
unknown committed
805
	single_multi table_wild_list table_wild_one opt_wild
806
	union_clause union_list
807
	precision subselect_start opt_and charset
808
	subselect_end select_var_list select_var_list_init help opt_len
unknown's avatar
unknown committed
809
	opt_extended_describe
810
        prepare prepare_src execute deallocate 
811 812
	statement sp_suid opt_view_list view_list or_replace algorithm
	sp_c_chistics sp_a_chistics sp_chistic sp_c_chistic
813
END_OF_INPUT
unknown's avatar
unknown committed
814

815 816 817 818 819 820 821
%type <NONE> call sp_proc_stmts sp_proc_stmts1 sp_proc_stmt
%type <num>  sp_decl_idents sp_opt_inout sp_handler_type sp_hcond_list
%type <spcondtype> sp_cond sp_hcond
%type <spblock> sp_decls sp_decl
%type <lex> sp_cursor_stmt
%type <spname> sp_name

unknown's avatar
unknown committed
822 823
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
824
	',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_SYM BETWEEN_SYM CASE_SYM
825
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM
unknown's avatar
unknown committed
826 827 828 829 830
%%


query:
	END_OF_INPUT
unknown's avatar
unknown committed
831
	{
832
	   THD *thd= YYTHD;
833
	   if (!thd->bootstrap &&
834
	      (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
835
	   {
unknown's avatar
unknown committed
836
	     my_message(ER_EMPTY_QUERY, ER(ER_EMPTY_QUERY), MYF(0));
837
	     YYABORT;
838
	   }
839 840
	   else
	   {
841
	     thd->lex->sql_command= SQLCOM_EMPTY_QUERY;
842
	   }
unknown's avatar
unknown committed
843
	}
unknown's avatar
unknown committed
844
	| verb_clause END_OF_INPUT {};
unknown's avatar
unknown committed
845 846

verb_clause:
847 848 849 850 851 852
	  statement
	| begin
	;

/* Verb clauses, except begin */
statement:
unknown's avatar
unknown committed
853 854
	  alter
	| analyze
855
	| backup
856
	| call
unknown's avatar
unknown committed
857 858
	| change
	| check
859
	| checksum
unknown's avatar
unknown committed
860 861
	| commit
	| create
862
        | deallocate
unknown's avatar
unknown committed
863 864
	| delete
	| describe
unknown's avatar
unknown committed
865
	| do
unknown's avatar
unknown committed
866
	| drop
867
        | execute
unknown's avatar
unknown committed
868
	| flush
unknown's avatar
unknown committed
869
	| grant
unknown's avatar
unknown committed
870 871
	| handler
	| help
unknown's avatar
unknown committed
872
	| insert
unknown's avatar
unknown committed
873
	| kill
unknown's avatar
unknown committed
874 875 876
	| load
	| lock
	| optimize
unknown's avatar
unknown committed
877
        | keycache
unknown's avatar
unknown committed
878
	| preload
879
        | prepare
880
	| purge
unknown's avatar
unknown committed
881
	| release
882
	| rename
unknown's avatar
unknown committed
883
	| repair
unknown's avatar
unknown committed
884
	| replace
unknown's avatar
unknown committed
885
	| reset
886
	| restore
unknown's avatar
unknown committed
887 888
	| revoke
	| rollback
889
	| savepoint
unknown's avatar
unknown committed
890 891
	| select
	| set
unknown's avatar
unknown committed
892
	| show
unknown's avatar
unknown committed
893
	| slave
unknown's avatar
unknown committed
894
	| start
895
	| truncate
unknown's avatar
unknown committed
896 897
	| unlock
	| update
unknown's avatar
unknown committed
898
	| use
unknown's avatar
unknown committed
899
        ;
900

unknown's avatar
unknown committed
901
deallocate:
902
        deallocate_or_drop PREPARE_SYM ident
unknown's avatar
unknown committed
903 904
        {
          THD *thd=YYTHD;
905
          LEX *lex= thd->lex;
unknown's avatar
unknown committed
906 907 908 909 910
          if (thd->command == COM_PREPARE)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
911
          lex->sql_command= SQLCOM_DEALLOCATE_PREPARE;
unknown's avatar
unknown committed
912 913 914
          lex->prepared_stmt_name= $3;
        };

915 916 917 918 919 920
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


unknown's avatar
unknown committed
921
prepare:
922
        PREPARE_SYM ident FROM prepare_src
unknown's avatar
unknown committed
923 924
        {
          THD *thd=YYTHD;
925
          LEX *lex= thd->lex;
unknown's avatar
unknown committed
926 927 928 929 930
          if (thd->command == COM_PREPARE)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
931
          lex->sql_command= SQLCOM_PREPARE;
unknown's avatar
unknown committed
932 933 934
          lex->prepared_stmt_name= $2;
        };

935 936 937 938 939 940
prepare_src:
        TEXT_STRING_sys
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $1;
unknown's avatar
unknown committed
941
          lex->prepared_stmt_code_is_varref= FALSE;
942 943 944 945 946 947
        }
        | '@' ident_or_text
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $2;
unknown's avatar
unknown committed
948
          lex->prepared_stmt_code_is_varref= TRUE;
949
        };
950

unknown's avatar
unknown committed
951 952 953 954
execute:
        EXECUTE_SYM ident
        {
          THD *thd=YYTHD;
955
          LEX *lex= thd->lex;
unknown's avatar
unknown committed
956 957 958 959 960
          if (thd->command == COM_PREPARE)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
961
          lex->sql_command= SQLCOM_EXECUTE;
unknown's avatar
unknown committed
962 963 964 965 966 967 968 969 970 971 972 973
          lex->prepared_stmt_name= $2;
        }
        execute_using
        {}
        ;

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
974 975
        execute_var_list ',' execute_var_ident
        | execute_var_ident
unknown's avatar
unknown committed
976 977 978 979 980 981 982 983
        ;

execute_var_ident: '@' ident_or_text
        {
          LEX *lex=Lex;
          LEX_STRING *lexstr= (LEX_STRING*)sql_memdup(&$2, sizeof(LEX_STRING));
          if (!lexstr || lex->prepared_stmt_params.push_back(lexstr))
              YYABORT;
984
        }
unknown's avatar
unknown committed
985 986
        ;

unknown's avatar
unknown committed
987 988
/* help */

989
help:
990
       HELP_SYM ident_or_text
unknown's avatar
unknown committed
991
       {
992 993 994
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
	  lex->help_arg= $2.str;
995
       };
unknown's avatar
unknown committed
996 997 998 999

/* change master */

change:
unknown's avatar
unknown committed
1000
       CHANGE MASTER_SYM TO_SYM
unknown's avatar
unknown committed
1001 1002 1003
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
unknown's avatar
unknown committed
1004
	  bzero((char*) &lex->mi, sizeof(lex->mi));
1005 1006 1007 1008
        }
       master_defs
	{}
       ;
unknown's avatar
unknown committed
1009 1010 1011

master_defs:
       master_def
unknown's avatar
unknown committed
1012
       | master_defs ',' master_def;
unknown's avatar
unknown committed
1013

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
master_def:
       MASTER_HOST_SYM EQ TEXT_STRING_sys
       {
	 Lex->mi.host = $3.str;
       }
       |
       MASTER_USER_SYM EQ TEXT_STRING_sys
       {
	 Lex->mi.user = $3.str;
       }
       |
       MASTER_PASSWORD_SYM EQ TEXT_STRING_sys
       {
	 Lex->mi.password = $3.str;
       }
       |
       MASTER_PORT_SYM EQ ULONG_NUM
       {
	 Lex->mi.port = $3;
       }
       |
       MASTER_CONNECT_RETRY_SYM EQ ULONG_NUM
       {
	 Lex->mi.connect_retry = $3;
       }
       | MASTER_SSL_SYM EQ ULONG_NUM
         {
           Lex->mi.ssl= $3 ? 
               LEX_MASTER_INFO::SSL_ENABLE : LEX_MASTER_INFO::SSL_DISABLE;
         }
       | MASTER_SSL_CA_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_ca= $3.str;
         }
       | MASTER_SSL_CAPATH_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_capath= $3.str;
         }
       | MASTER_SSL_CERT_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_cert= $3.str;
         }
       | MASTER_SSL_CIPHER_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_cipher= $3.str;
         }
       | MASTER_SSL_KEY_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_key= $3.str;
	 }
       |
         master_file_def
       ;

master_file_def:
       MASTER_LOG_FILE_SYM EQ TEXT_STRING_sys
       {
	 Lex->mi.log_file_name = $3.str;
       }
       | MASTER_LOG_POS_SYM EQ ulonglong_num
         {
           Lex->mi.pos = $3;
           /* 
              If the user specified a value < BIN_LOG_HEADER_SIZE, adjust it
              instead of causing subsequent errors. 
              We need to do it in this file, because only there we know that 
              MASTER_LOG_POS has been explicitely specified. On the contrary
              in change_master() (sql_repl.cc) we cannot distinguish between 0
              (MASTER_LOG_POS explicitely specified as 0) and 0 (unspecified),
              whereas we want to distinguish (specified 0 means "read the binlog
              from 0" (4 in fact), unspecified means "don't change the position
              (keep the preceding value)").
           */
           Lex->mi.pos = max(BIN_LOG_HEADER_SIZE, Lex->mi.pos);
         }
       | RELAY_LOG_FILE_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.relay_log_name = $3.str;
         }
       | RELAY_LOG_POS_SYM EQ ULONG_NUM
         {
           Lex->mi.relay_log_pos = $3;
           /* Adjust if < BIN_LOG_HEADER_SIZE (same comment as Lex->mi.pos) */
           Lex->mi.relay_log_pos = max(BIN_LOG_HEADER_SIZE, Lex->mi.relay_log_pos);
         }
       ;

/* create a table */

create:
	CREATE opt_table_options TABLE_SYM opt_if_not_exists table_ident
	{
	  THD *thd= YYTHD;
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_CREATE_TABLE;
	  if (!lex->select_lex.add_table_to_list(thd, $5, NULL,
						 TL_OPTION_UPDATING,
						 (using_update_log ?
						  TL_READ_NO_INSERT:
						  TL_READ)))
	    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;
	  lex->create_info.db_type= (enum db_type) lex->thd->variables.table_type;
	  lex->create_info.default_table_charset= NULL;
	  lex->name=0;
	}
	create2
	  { Lex->current_select= &Lex->select_lex; }
	| CREATE opt_unique_or_fulltext INDEX_SYM ident key_alg ON table_ident
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_CREATE_INDEX;
	    if (!lex->current_select->add_table_to_list(lex->thd, $7, NULL,
							TL_OPTION_UPDATING))
	      YYABORT;
	    lex->create_list.empty();
	    lex->key_list.empty();
	    lex->col_list.empty();
	    lex->change=NullS;
	  }
	   '(' key_list ')'
	  {
	    LEX *lex=Lex;

	    lex->key_list.push_back(new Key($2,$4.str, $5, 0, lex->col_list));
	    lex->col_list.empty();
	  }
	| CREATE DATABASE opt_if_not_exists ident
	  {
             Lex->create_info.default_table_charset= NULL;
             Lex->create_info.used_fields= 0;
          }
	  opt_create_database_options
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_CREATE_DB;
	    lex->name=$4.str;
            lex->create_info.options=$3;
	  }
	| CREATE udf_func_type FUNCTION_SYM sp_name
	  {
	    LEX *lex=Lex;
	    lex->spname= $4;
	    lex->udf.type= $2;
	  }
	  create_function_tail
	  {}
	| CREATE PROCEDURE sp_name
	  {
	    LEX *lex= Lex;
	    sp_head *sp;

	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
1173
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "PROCEDURE");
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
	      YYABORT;
	    }
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
	    sp->init(lex);

	    sp->m_type= TYPE_ENUM_PROCEDURE;
	    lex->sphead= sp;
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
	    sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
	    YYTHD->client_capabilities &= (~CLIENT_MULTI_QUERIES);
	  }
          '('
	  {
	    LEX *lex= Lex;

	    lex->sphead->m_param_begin= lex->tok_start+1;
	  }
	  sp_pdparam_list
	  ')'
	  {
	    LEX *lex= Lex;

	    lex->sphead->m_param_end= lex->tok_start;
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	  }
	  sp_c_chistics
	  {
	    LEX *lex= Lex;

	    lex->sphead->m_chistics= &lex->sp_chistics;
	    lex->sphead->m_body_begin= lex->tok_start;
	  }
	  sp_proc_stmt
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;

	    if (sp->check_backpatch(YYTHD))
	      YYABORT;
	    sp->init_strings(YYTHD, lex, $3);
	    lex->sql_command= SQLCOM_CREATE_PROCEDURE;
	    /* Restore flag if it was cleared above */
	    if (sp->m_old_cmq)
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
	    sp->restore_thd_mem_root(YYTHD);
	  }
	| CREATE or_replace algorithm VIEW_SYM table_ident
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_CREATE_VIEW;
	    lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE;
	    /* first table in list is target VIEW name */
	    if (!lex->select_lex.add_table_to_list(thd, $5, NULL, 0))
              YYABORT;
	  }
	  opt_view_list AS select_init check_option
	  {}
        | CREATE TRIGGER_SYM ident trg_action_time trg_event 
          ON table_ident FOR_SYM EACH_SYM ROW_SYM
          {
            LEX *lex= Lex;
            sp_head *sp;
           
            if (lex->sphead)
            {
unknown's avatar
unknown committed
1246
              my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "TRIGGER");
1247 1248
              YYABORT;
            }
1249 1250 1251

            if (!(sp= new sp_head()))
              YYABORT;
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
            sp->reset_thd_mem_root(YYTHD);
            sp->init(lex);
            
            sp->m_type= TYPE_ENUM_TRIGGER;
            lex->sphead= sp;
            /*
              We have to turn of CLIENT_MULTI_QUERIES while parsing a
              stored procedure, otherwise yylex will chop it into pieces
              at each ';'.
            */
            sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
            YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
            
            bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
            lex->sphead->m_chistics= &lex->sp_chistics;
            lex->sphead->m_body_begin= lex->tok_start;
          }
          sp_proc_stmt
          {
            LEX *lex= Lex;
            sp_head *sp= lex->sphead;
            
            lex->sql_command= SQLCOM_CREATE_TRIGGER;
            sp->init_strings(YYTHD, lex, NULL);
            /* Restore flag if it was cleared above */
            if (sp->m_old_cmq)
              YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
            sp->restore_thd_mem_root(YYTHD);

            lex->name_and_length= $3;

            /*
              We have to do it after parsing trigger body, because some of
              sp_proc_stmt alternatives are not saving/restoring LEX, so
              lex->query_tables can be wiped out.
              
              QQ: What are other consequences of this?
              
              QQ: Could we loosen lock type in certain cases ?
            */
            if (!lex->select_lex.add_table_to_list(YYTHD, $7, 
                                                   (LEX_STRING*) 0,
                                                   TL_OPTION_UPDATING,
                                                   TL_WRITE))
              YYABORT;
          }
1298 1299 1300 1301
	| CREATE USER clear_privileges grant_list
	  {
	    Lex->sql_command = SQLCOM_CREATE_USER;
          }
1302 1303
	;

1304 1305 1306 1307 1308 1309 1310
clear_privileges:
        /* Nothing */
        {
          LEX *lex=Lex;
          lex->users_list.empty();
          lex->columns.empty();
          lex->grant= lex->grant_tot_col= 0;
1311
	  lex->all_privileges= 0;
1312 1313 1314 1315 1316 1317 1318
          lex->select_lex.db= 0;
          lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
          lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
          bzero((char *)&(lex->mqh),sizeof(lex->mqh));
        }
        ;

1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
sp_name:
	  IDENT_sys '.' IDENT_sys
	  {
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
	| IDENT_sys
	  {
	    $$= sp_name_current_db_new(YYTHD, $1);
	  }
	;

create_function_tail:
	  RETURNS_SYM udf_type UDF_SONAME_SYM TEXT_STRING_sys
	  {
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_CREATE_FUNCTION;
	    lex->udf.name = lex->spname->m_name;
	    lex->udf.returns=(Item_result) $2;
	    lex->udf.dl=$4.str;
	  }
	| '('
	  {
	    LEX *lex= Lex;
	    sp_head *sp;

	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
1347
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
	      YYABORT;
	    }
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
	    sp->init(lex);

	    sp->m_type= TYPE_ENUM_FUNCTION;
	    lex->sphead= sp;
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
	    sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
	    YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
	    lex->sphead->m_param_begin= lex->tok_start+1;
	  }
          sp_fdparam_list ')'
	  {
	    LEX *lex= Lex;

	    lex->sphead->m_param_end= lex->tok_start;
	  }
	  RETURNS_SYM
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;

	    sp->m_returns_begin= lex->tok_start;
	    sp->m_returns_cs= lex->charset= NULL;
	  }
	  type
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;

	    sp->m_returns_end= lex->tok_start;
	    sp->m_returns= (enum enum_field_types)$8;
	    sp->m_returns_cs= lex->charset;
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	  }
	  sp_c_chistics
	  {
	    LEX *lex= Lex;

	    lex->sphead->m_chistics= &lex->sp_chistics;
	    lex->sphead->m_body_begin= lex->tok_start;
	  }
	  sp_proc_stmt
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;

	    if (sp->check_backpatch(YYTHD))
	      YYABORT;
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
	    sp->init_strings(YYTHD, lex, lex->spname);
	    /* Restore flag if it was cleared above */
	    if (sp->m_old_cmq)
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
	    sp->restore_thd_mem_root(YYTHD);
	  }
	;

sp_a_chistics:
	  /* Empty */ {}
	| sp_a_chistics sp_chistic {}
	;

sp_c_chistics:
	  /* Empty */ {}
	| sp_c_chistics sp_c_chistic {}
	;

/* Characteristics for both create and alter */
sp_chistic:
	  COMMENT_SYM TEXT_STRING_sys
	  { Lex->sp_chistics.comment= $2; }
	| LANGUAGE_SYM SQL_SYM
	  { /* Just parse it, we only have one language for now. */ }
	| NO_SYM SQL_SYM
	  { Lex->sp_chistics.daccess= SP_NO_SQL; }
	| CONTAINS_SYM SQL_SYM
	  { Lex->sp_chistics.daccess= SP_CONTAINS_SQL; }
	| READS_SYM SQL_SYM DATA_SYM
	  { Lex->sp_chistics.daccess= SP_READS_SQL_DATA; }
	| MODIFIES_SYM SQL_SYM DATA_SYM
	  { Lex->sp_chistics.daccess= SP_MODIFIES_SQL_DATA; }
	| sp_suid
	  { }
	;

/* Create characteristics */
sp_c_chistic:
	  sp_chistic            { }
	| DETERMINISTIC_SYM     { Lex->sp_chistics.detistic= TRUE; }
1445
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
	;

sp_suid:
	  SQL_SYM SECURITY_SYM DEFINER_SYM
	  {
	    Lex->sp_chistics.suid= SP_IS_SUID;
	  }
	| SQL_SYM SECURITY_SYM INVOKER_SYM
	  {
	    Lex->sp_chistics.suid= SP_IS_NOT_SUID;
	  }
	;

call:
	  CALL_SYM sp_name
	  {
	    LEX *lex = Lex;

	    lex->sql_command= SQLCOM_CALL;
	    lex->spname= $2;
	    lex->value_list.empty();
	  }
          '(' sp_cparam_list ')' {}
	;

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

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

/* Stored FUNCTION parameter declaration list */
sp_fdparam_list:
	  /* Empty */
	| sp_fdparams
	;

sp_fdparams:
	  sp_fdparams ',' sp_fdparam
	| sp_fdparam
	;

sp_fdparam:
	  ident type
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
1507
	      my_error(ER_SP_DUP_PARAM, MYF(0), $1.str);
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum enum_field_types)$2, sp_param_in);
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
	  sp_opt_inout ident type
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$2, TRUE))
	    {
1533
	      my_error(ER_SP_DUP_PARAM, MYF(0), $2.str);
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
	      YYABORT;
	    }
	    spc->push_pvar(&$2, (enum enum_field_types)$3,
			   (sp_param_mode_t)$1);
	  }
	;

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

sp_proc_stmts:
	  /* Empty */ {}
	| sp_proc_stmts  { Lex->query_tables= 0; } sp_proc_stmt ';'
	;

sp_proc_stmts1:
	  sp_proc_stmt ';' {}
	| sp_proc_stmts1  { Lex->query_tables= 0; } sp_proc_stmt ';'
	;

sp_decls:
	  /* Empty */
	  {
	    $$.vars= $$.conds= $$.hndlrs= $$.curs= 0;
	  }
	| sp_decls sp_decl ';'
	  {
	    /* We check for declarations out of (standard) order this way
	       because letting the grammar rules reflect it caused tricky
	       shift/reduce conflicts with the wrong result. (And we get
	       better error handling this way.) */
	    if (($2.vars || $2.conds) && ($1.curs || $1.hndlrs))
	    { /* Variable or condition following cursor or handler */
unknown's avatar
unknown committed
1571 1572
	      my_message(ER_SP_VARCOND_AFTER_CURSHNDLR,
                         ER(ER_SP_VARCOND_AFTER_CURSHNDLR), MYF(0));
1573 1574 1575 1576
	      YYABORT;
	    }
	    if ($2.curs && $1.hndlrs)
	    { /* Cursor following handler */
unknown's avatar
unknown committed
1577 1578
	      my_message(ER_SP_CURSOR_AFTER_HANDLER,
                         ER(ER_SP_CURSOR_AFTER_HANDLER), MYF(0));
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
	      YYABORT;
	    }
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
	    $$.curs= $1.curs + $2.curs;
	  }
	;

sp_decl:
	  DECLARE_SYM sp_decl_idents type sp_opt_default
	  {
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    uint max= ctx->context_pvars();
	    enum enum_field_types type= (enum enum_field_types)$3;
	    Item *it= $4;

	    for (uint i = max-$2 ; i < max ; i++)
	    {
	      ctx->set_type(i, type);
	      if (! it)
	        ctx->set_isset(i, FALSE);
	      else
	      {
	        sp_instr_set *in= new sp_instr_set(lex->sphead->instructions(),
	                                           ctx,
						   ctx->pvar_context2index(i),
						   it, type);

		in->tables= lex->query_tables;
		lex->query_tables= 0;
	        lex->sphead->add_instr(in);
	        ctx->set_isset(i, TRUE);
		ctx->set_default(i, it);
	      }
	    }
	    $$.vars= $2;
	    $$.conds= $$.hndlrs= $$.curs= 0;
	  }
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1626
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
	      YYABORT;
	    }
	    YYTHD->lex->spcont->push_cond(&$2, $5);
	    $$.vars= $$.hndlrs= $$.curs= 0;
	    $$.conds= 1;
	  }
	| DECLARE_SYM sp_handler_type HANDLER_SYM FOR_SYM
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_instr_hpush_jump *i=
              new sp_instr_hpush_jump(sp->instructions(), ctx, $2,
	                              ctx->current_pvars());

	    sp->add_instr(i);
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
	    ctx->add_handler();
	    sp->m_in_handler= TRUE;
	  }
	  sp_hcond_list sp_proc_stmt
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *hlab= lex->spcont->pop_label(); /* After this hdlr */
	    sp_instr_hreturn *i;

	    if ($2 == SP_HANDLER_CONTINUE)
	    {
	      i= new sp_instr_hreturn(sp->instructions(), ctx,
	                              ctx->current_pvars());
	      sp->add_instr(i);
	    }
	    else
	    {  /* EXIT or UNDO handler, just jump to the end of the block */
	      i= new sp_instr_hreturn(sp->instructions(), ctx, 0);

	      sp->add_instr(i);
	      sp->push_backpatch(i, lex->spcont->last_label()); /* Block end */
	    }
	    lex->sphead->backpatch(hlab);
	    sp->m_in_handler= FALSE;
	    $$.vars= $$.conds= $$.curs= 0;
	    $$.hndlrs= $6;
	  }
	| DECLARE_SYM ident CURSOR_SYM FOR_SYM sp_cursor_stmt
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint offp;
	    sp_instr_cpush *i;

	    if (ctx->find_cursor(&$2, &offp, TRUE))
	    {
1683
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
	      delete $5;
	      YYABORT;
	    }
            i= new sp_instr_cpush(sp->instructions(), ctx, $5);
	    sp->add_instr(i);
	    ctx->push_cursor(&$2);
	    $$.vars= $$.conds= $$.hndlrs= 0;
	    $$.curs= 1;
	  }
	;

sp_cursor_stmt:
	  {
	    Lex->sphead->reset_lex(YYTHD);

	    /* We use statement here just be able to get a better
	       error message. Using 'select' works too, but will then
	       result in a generic "syntax error" if a non-select
	       statement is given. */
	  }
	  statement
	  {
	    LEX *lex= Lex;

	    if (lex->sql_command != SQLCOM_SELECT)
	    {
unknown's avatar
unknown committed
1710 1711
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
1712 1713 1714 1715
	      YYABORT;
	    }
	    if (lex->result)
	    {
unknown's avatar
unknown committed
1716 1717
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
	      YYABORT;
	    }
	    lex->sp_lex_in_use= TRUE;
	    $$= lex;
	    lex->sphead->restore_lex(YYTHD);
	  }
	;

sp_handler_type:
	  EXIT_SYM      { $$= SP_HANDLER_EXIT; }
	| CONTINUE_SYM  { $$= SP_HANDLER_CONTINUE; }
/*	| UNDO_SYM      { QQ No yet } */
	;

sp_hcond_list:
	  sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_instr_hpush_jump *i= (sp_instr_hpush_jump *)sp->last_instruction();

	    i->add_condition($1);
	    $$= 1;
	  }
	| sp_hcond_list ',' sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_instr_hpush_jump *i= (sp_instr_hpush_jump *)sp->last_instruction();

	    i->add_condition($3);
	    $$= $1 + 1;
	  }
	;

sp_cond:
	  ULONG_NUM
	  {			/* mysql errno */
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::number;
	    $$->mysqlerr= $1;
	  }
	| SQLSTATE_SYM opt_value TEXT_STRING_literal
	  {		/* SQLSTATE */
	    uint len= ($3.length < sizeof($$->sqlstate)-1 ?
                       $3.length : sizeof($$->sqlstate)-1);

	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
	    memcpy($$->sqlstate, $3.str, len);
	    $$->sqlstate[len]= '\0';
	  }
	;

opt_value:
	  /* Empty */  {}
	| VALUE_SYM    {}
	;

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
1787
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
1788 1789 1790 1791 1792 1793 1794 1795
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
1796
	| not FOUND_SYM		/* SQLSTATEs 02??? */
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::notfound;
	  }
	| SQLEXCEPTION_SYM	/* All other SQLSTATEs */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::exception;
	  }
	;

sp_decl_idents:
	  ident
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
1816
	      my_error(ER_SP_DUP_VAR, MYF(0), $1.str);
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum_field_types)0, sp_param_in);
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$3, TRUE))
	    {
1829
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
	      YYABORT;
	    }
	    spc->push_pvar(&$3, (enum_field_types)0, sp_param_in);
	    $$= $1 + 1;
	  }
	;

sp_opt_default:
	  /* Empty */ { $$ = NULL; }
        | DEFAULT expr { $$ = $2; }
	;

sp_proc_stmt:
	  {
	    LEX *lex= Lex;

	    lex->sphead->reset_lex(YYTHD);
	    lex->sphead->m_tmp_query= lex->tok_start;
	  }
	  statement
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;

	    if ((lex->sql_command == SQLCOM_SELECT && !lex->result) ||
	        sp_multi_results_command(lex->sql_command))
	    {
	      /* We maybe have one or more SELECT without INTO */
	      sp->m_multi_results= TRUE;
	    }
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
unknown's avatar
unknown committed
1862
	      my_message(ER_SP_NO_USE, ER(ER_SP_NO_USE), MYF(0));
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
	      YYABORT;
	    }
	    /* Don't add an instruction for empty SET statements.
	    ** (This happens if the SET only contained local variables,
	    **  which get their set instructions generated separately.)
	    */
	    if (lex->sql_command != SQLCOM_SET_OPTION ||
		! lex->var_list.is_empty())
	    {
              /*
                Currently we can't handle queries inside a FUNCTION or
                TRIGGER, because of the way table locking works. This is 
                unfortunate, and limits the usefulness of functions and
                especially triggers a tremendously, but it's nothing we 
                can do about this at the moment.
              */
	      if (sp->m_type != TYPE_ENUM_PROCEDURE)
	      {
unknown's avatar
unknown committed
1881
		my_message(ER_SP_BADSTATEMENT, ER(ER_SP_BADSTATEMENT), MYF(0));
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
		YYABORT;
	      }
	      else
	      {
		sp_instr_stmt *i=new sp_instr_stmt(sp->instructions(),
						   lex->spcont);

		/* Extract the query statement from the tokenizer:
                   The end is either lex->tok_end or tok->ptr. */
		if (lex->ptr - lex->tok_end > 1)
		  i->m_query.length= lex->ptr - sp->m_tmp_query;
		else
		  i->m_query.length= lex->tok_end - sp->m_tmp_query;
		i->m_query.str= strmake_root(YYTHD->mem_root,
					     (char *)sp->m_tmp_query,
					     i->m_query.length);
		i->set_lex(lex);
		sp->add_instr(i);
		lex->sp_lex_in_use= TRUE;
	      }
            }
	    sp->restore_lex(YYTHD);
          }
	| RETURN_SYM expr
	  {
	    LEX *lex= Lex;

	    if (lex->sphead->m_type == TYPE_ENUM_PROCEDURE)
	    {
unknown's avatar
unknown committed
1911
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
1912 1913 1914 1915 1916 1917 1918 1919
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_freturn *i;

	      if ($2->type() == Item::SUBSELECT_ITEM)
	      {  /* QQ For now, just disallow subselects as values */
unknown's avatar
unknown committed
1920
	        my_message(ER_SP_BADSTATEMENT, ER(ER_SP_BADSTATEMENT), MYF(0));
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
	        YYABORT;
	      }
	      i= new sp_instr_freturn(lex->sphead->instructions(),
				      lex->spcont,
		                      $2, lex->sphead->m_returns);
	      lex->sphead->add_instr(i);
	      lex->sphead->m_has_return= TRUE;
	    }
	  }
	| IF sp_if END IF {}
	| CASE_SYM WHEN_SYM
	  {
	    Lex->sphead->m_simple_case= FALSE;
	  }
	  sp_case END CASE_SYM {}
	| CASE_SYM expr WHEN_SYM
	  {
	    /* We "fake" this by using an anonymous variable which we
	       set to the expression. Note that all WHENs are evaluate
	       at the same frame level, so we then know that it's the
	       top-most variable in the frame. */
	    LEX *lex= Lex;
	    uint offset= lex->spcont->current_pvars();
	    sp_instr_set *i = new sp_instr_set(lex->sphead->instructions(),
					       lex->spcont,
	                                       offset, $2, MYSQL_TYPE_STRING);
	    LEX_STRING dummy;

	    dummy.str= (char *)"";
	    dummy.length= 0;
	    lex->spcont->push_pvar(&dummy, MYSQL_TYPE_STRING, sp_param_in);
	    i->tables= lex->query_tables;
	    lex->query_tables= 0;
	    lex->sphead->add_instr(i);
	    lex->sphead->m_simple_case= TRUE;
	  }
	  sp_case END CASE_SYM
	  {
	    Lex->spcont->pop_pvar();
	  }
	| sp_labeled_control
	  {}
	| { /* Unlabeled controls get a secret label. */
	    LEX *lex= Lex;

	    lex->spcont->push_label((char *)"", lex->sphead->instructions());
	  }
	  sp_unlabeled_control
	  {
	    LEX *lex= Lex;

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
	| LEAVE_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_head *sp = lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab)
	    {
1983
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
	      YYABORT;
	    }
	    else
	    {
	      uint ip= sp->instructions();
	      sp_instr_jump *i;
	      sp_instr_hpop *ih;
	      sp_instr_cpop *ic;

	      ih= new sp_instr_hpop(ip++, ctx, 0);
	      sp->push_backpatch(ih, lab);
	      sp->add_instr(ih);
	      ic= new sp_instr_cpop(ip++, ctx, 0);
	      sp->push_backpatch(ic, lab);
	      sp->add_instr(ic);
	      i= new sp_instr_jump(ip, ctx);
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
	    }
	  }
	| ITERATE_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab || lab->type != SP_LAB_ITER)
	    {
2013
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "ITERATE", $2.str);
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_jump *i;
	      uint ip= sp->instructions();
	      uint n;

	      n= ctx->diff_handlers(lab->ctx);
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
	      n= ctx->diff_cursors(lab->ctx);
	      if (n)
	        sp->add_instr(new sp_instr_cpop(ip++, ctx, n));
	      i= new sp_instr_jump(ip, ctx, lab->ip); /* Jump back */
              sp->add_instr(i);
	    }
	  }
	| LABEL_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (lab)
	    {
2041
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $2.str);
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
	      YYABORT;
	    }
	    else
	    {
	      lab= ctx->push_label($2.str, sp->instructions());
	      lab->type= SP_LAB_GOTO;
	      lab->ctx= ctx;
              sp->backpatch(lab);
	    }
	  }
	| GOTO_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab;
	    sp_instr_jump *i;
	    sp_instr_hpop *ih;
	    sp_instr_cpop *ic;

	    if (sp->m_in_handler)
	    {
unknown's avatar
unknown committed
2065
	      my_message(ER_SP_GOTO_IN_HNDLR, ER(ER_SP_GOTO_IN_HNDLR), MYF(0));
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
	      YYABORT;
	    }
	    lab= ctx->find_label($2.str);
	    if (! lab)
	    {
	      lab= (sp_label_t *)YYTHD->alloc(sizeof(sp_label_t));
	      lab->name= $2.str;
	      lab->ip= 0;
	      lab->type= SP_LAB_REF;
	      lab->ctx= ctx;

	      ih= new sp_instr_hpop(ip++, ctx, 0);
	      sp->push_backpatch(ih, lab);
	      sp->add_instr(ih);
	      ic= new sp_instr_cpop(ip++, ctx, 0);
	      sp->add_instr(ic);
	      sp->push_backpatch(ic, lab);
	      i= new sp_instr_jump(ip, ctx);
	      sp->push_backpatch(i, lab);  /* Jumping forward */
	      sp->add_instr(i);
	    }
	    else
	    {
	      uint n;

	      n= ctx->diff_handlers(lab->ctx);
	      if (n)
	      {
	        ih= new sp_instr_hpop(ip++, ctx, n);
	        sp->add_instr(ih);
	      }
	      n= ctx->diff_cursors(lab->ctx);
	      if (n)
	      {
	        ic= new sp_instr_cpop(ip++, ctx, n);
	        sp->add_instr(ic);
	      }
	      i= new sp_instr_jump(ip, ctx, lab->ip); /* Jump back */
	      sp->add_instr(i);
	    }
	  }
	| OPEN_SYM ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_copen *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2116
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
	      YYABORT;
	    }
	    i= new sp_instr_copen(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
	| FETCH_SYM sp_opt_fetch_noise ident INTO
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cfetch *i;

	    if (! lex->spcont->find_cursor(&$3, &offset))
	    {
2131
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $3.str);
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
	      YYABORT;
	    }
	    i= new sp_instr_cfetch(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
	  sp_fetch_list
	  { }
	| CLOSE_SYM ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cclose *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2148
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
	      YYABORT;
	    }
	    i= new sp_instr_cclose(sp->instructions(), lex->spcont,  offset);
	    sp->add_instr(i);
	  }
	;

sp_opt_fetch_noise:
	  /* Empty */
	| NEXT_SYM FROM
	| FROM
	;

sp_fetch_list:
	  ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *spc= lex->spcont;
	    sp_pvar_t *spv;

	    if (!spc || !(spv = spc->find_pvar(&$1)))
	    {
2172
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	      spv->isset= TRUE;
	    }
	  }
	|
	  sp_fetch_list ',' ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *spc= lex->spcont;
	    sp_pvar_t *spv;

	    if (!spc || !(spv = spc->find_pvar(&$3)))
	    {
2194
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $3.str);
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 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
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	      spv->isset= TRUE;
	    }
	  }
	;

sp_if:
	  expr THEN_SYM
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, ctx, $1);

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

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

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

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

sp_case:
	  expr THEN_SYM
	  {
            LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

	    if (! sp->m_simple_case)
	      i= new sp_instr_jump_if_not(ip, ctx, $1);
	    else
	    { /* Simple case: <caseval> = <whenval> */
	      LEX_STRING ivar;

	      ivar.str= (char *)"_tmp_";
	      ivar.length= 5;
	      Item *var= (Item*) new Item_splocal(ivar, 
						  ctx->current_pvars()-1);
	      Item *expr= new Item_func_eq(var, $1);

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

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

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
	;
unknown's avatar
unknown committed
2294

2295 2296 2297 2298 2299 2300 2301
sp_whens:
	  /* Empty */
	  {
	    sp_head *sp= Lex->sphead;
	    uint ip= sp->instructions();
	    sp_instr_error *i= new sp_instr_error(ip, Lex->spcont,
						  ER_SP_CASE_NOT_FOUND);
unknown's avatar
unknown committed
2302

2303 2304 2305 2306 2307
	    sp->add_instr(i);
	  }
	| ELSE sp_proc_stmts1 {}
	| WHEN_SYM sp_case {}
	;
unknown's avatar
unknown committed
2308

2309 2310
sp_labeled_control:
	  IDENT ':'
unknown's avatar
unknown committed
2311
	  {
2312 2313 2314 2315 2316 2317
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);

	    if (lab)
	    {
2318
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $1.str);
unknown's avatar
unknown committed
2319
	      YYABORT;
2320 2321 2322 2323 2324 2325 2326
	    }
	    else
	    {
	      lab= lex->spcont->push_label($1.str,
	                                   lex->sphead->instructions());
	      lab->type= SP_LAB_ITER;
	    }
unknown's avatar
unknown committed
2327
	  }
2328
	  sp_unlabeled_control sp_opt_label
unknown's avatar
unknown committed
2329
	  {
2330
	    LEX *lex= Lex;
unknown's avatar
unknown committed
2331

2332 2333 2334 2335 2336 2337 2338
	    if ($5.str)
	    {
	      sp_label_t *lab= lex->spcont->find_label($5.str);

	      if (!lab ||
	          my_strcasecmp(system_charset_info, $5.str, lab->name) != 0)
	      {
2339
	        my_error(ER_SP_LABEL_MISMATCH, MYF(0), $5.str);
2340 2341 2342 2343
	        YYABORT;
	      }
	    }
	    lex->sphead->backpatch(lex->spcont->pop_label());
unknown's avatar
unknown committed
2344
	  }
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367
	;

sp_opt_label:
	  /* Empty  */
	  { $$.str= NULL; $$.length= 0; }
	| IDENT
	  { $$= $1; }
	;

sp_unlabeled_control:
	  BEGIN_SYM
	  { /* QQ This is just a dummy for grouping declarations and statements
	       together. No [[NOT] ATOMIC] yet, and we need to figure out how
	       make it coexist with the existing BEGIN COMMIT/ROLLBACK. */
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->last_label();

	    lab->type= SP_LAB_BEGIN;
	    lex->spcont= lex->spcont->push_context();
	  }
	  sp_decls
	  sp_proc_stmts
	  END
2368
	  {
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;

  	    sp->backpatch(ctx->last_label());	/* We always have a label */
	    if ($3.hndlrs)
	      sp->add_instr(new sp_instr_hpop(sp->instructions(), ctx,
					      $3.hndlrs));
	    if ($3.curs)
	      sp->add_instr(new sp_instr_cpop(sp->instructions(), ctx,
					      $3.curs));
	    lex->spcont= ctx->pop_context();
	  }
	| LOOP_SYM
	  sp_proc_stmts1 END LOOP_SYM
unknown's avatar
unknown committed
2384
	  {
2385 2386 2387 2388 2389 2390
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lex->spcont, lab->ip);

	    lex->sphead->add_instr(i);
unknown's avatar
unknown committed
2391
	  }
2392
	| WHILE_SYM expr DO_SYM
unknown's avatar
unknown committed
2393
	  {
2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, lex->spcont,
							       $2);

	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
	    i->tables= lex->query_tables;
	    lex->query_tables= 0;
            sp->add_instr(i);
unknown's avatar
unknown committed
2405
	  }
2406
	  sp_proc_stmts1 END WHILE_SYM
unknown's avatar
unknown committed
2407
	  {
2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lex->spcont, lab->ip);

	    lex->sphead->add_instr(i);
	  }
	| REPEAT_SYM sp_proc_stmts1 UNTIL_SYM expr END REPEAT_SYM
	  {
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, lex->spcont,
							       $4, lab->ip);

	    i->tables= lex->query_tables;
	    lex->query_tables= 0;
            lex->sphead->add_instr(i);
2426
	  }
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
	;

trg_action_time:
            BEFORE_SYM 
            { Lex->trg_chistics.action_time= TRG_ACTION_BEFORE; }
          | AFTER_SYM 
            { Lex->trg_chistics.action_time= TRG_ACTION_AFTER; }
          ;

trg_event:
            INSERT 
            { Lex->trg_chistics.event= TRG_EVENT_INSERT; }
          | UPDATE_SYM
            { Lex->trg_chistics.event= TRG_EVENT_UPDATE; }
          | DELETE_SYM
            { Lex->trg_chistics.event= TRG_EVENT_DELETE; }
2443
          ;
unknown's avatar
unknown committed
2444 2445

create2:
unknown's avatar
unknown committed
2446 2447 2448 2449 2450 2451
        '(' create2a {}
        | opt_create_table_options create3 {}
        | LIKE table_ident
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$2))
unknown's avatar
unknown committed
2452
              YYABORT;
unknown's avatar
unknown committed
2453 2454 2455 2456 2457
          }
        | '(' LIKE table_ident ')'
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$3))
2458
              YYABORT;
unknown's avatar
unknown committed
2459
          }
unknown's avatar
unknown committed
2460
        ;
unknown's avatar
unknown committed
2461

unknown's avatar
unknown committed
2462 2463
create2a:
        field_list ')' opt_create_table_options create3 {}
unknown's avatar
unknown committed
2464
	|  create_select ')' { Select->set_braces(1);} union_opt {}
2465
        ;
unknown's avatar
unknown committed
2466 2467

create3:
2468
	/* empty */ {}
unknown's avatar
unknown committed
2469
	| opt_duplicate opt_as     create_select
unknown's avatar
unknown committed
2470
          { Select->set_braces(0);} union_clause {}
unknown's avatar
unknown committed
2471
	| opt_duplicate opt_as '(' create_select ')'
unknown's avatar
unknown committed
2472
          { Select->set_braces(1);} union_opt {}
2473 2474
        ;

unknown's avatar
unknown committed
2475
create_select:
2476
          SELECT_SYM
unknown's avatar
unknown committed
2477
          {
unknown's avatar
unknown committed
2478 2479
	    LEX *lex=Lex;
	    lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
2480 2481 2482 2483
	    if (lex->sql_command == SQLCOM_INSERT)
	      lex->sql_command= SQLCOM_INSERT_SELECT;
	    else if (lex->sql_command == SQLCOM_REPLACE)
	      lex->sql_command= SQLCOM_REPLACE_SELECT;
2484 2485 2486 2487
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
unknown's avatar
unknown committed
2488
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
unknown's avatar
unknown committed
2489
	    mysql_init_select(lex);
2490
	    lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
2491
          }
unknown's avatar
unknown committed
2492 2493
          select_options select_item_list
	  {
2494
	    Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
2495
	  }
unknown's avatar
unknown committed
2496
	  opt_select_from
2497 2498 2499 2500 2501 2502 2503
	  {
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
	    Lex->current_select->table_list.push_front(&Lex->save_list);
	  }
unknown's avatar
unknown committed
2504
        ;
unknown's avatar
unknown committed
2505

2506 2507
opt_as:
	/* empty */ {}
unknown's avatar
unknown committed
2508
	| AS	    {};
2509

2510 2511 2512 2513 2514 2515 2516 2517 2518
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
2519 2520
	default_collation   {}
	| default_charset   {};
2521

unknown's avatar
unknown committed
2522
opt_table_options:
unknown's avatar
unknown committed
2523
	/* empty */	 { $$= 0; }
unknown's avatar
unknown committed
2524
	| table_options  { $$= $1;};
unknown's avatar
unknown committed
2525 2526 2527

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

unknown's avatar
unknown committed
2530
table_option:
unknown's avatar
unknown committed
2531
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
unknown's avatar
unknown committed
2532 2533

opt_if_not_exists:
unknown's avatar
unknown committed
2534
	/* empty */	 { $$= 0; }
2535
	| IF not EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
unknown's avatar
unknown committed
2536 2537 2538

opt_create_table_options:
	/* empty */
unknown's avatar
unknown committed
2539
	| create_table_options;
unknown's avatar
unknown committed
2540

unknown's avatar
unknown committed
2541 2542 2543 2544
create_table_options_space_separated:
	create_table_option
	| create_table_option create_table_options_space_separated;

unknown's avatar
unknown committed
2545 2546
create_table_options:
	create_table_option
2547
	| create_table_option     create_table_options
unknown's avatar
unknown committed
2548
	| create_table_option ',' create_table_options;
unknown's avatar
unknown committed
2549 2550

create_table_option:
2551 2552
	ENGINE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; }
	| TYPE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; WARN_DEPRECATED("TYPE=storage_engine","ENGINE=storage_engine");   Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; }
2553 2554 2555
	| 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;}
2556 2557
	| PASSWORD opt_equal TEXT_STRING_sys	{ Lex->create_info.password=$3.str; Lex->create_info.used_fields|= HA_CREATE_USED_PASSWORD; }
	| COMMENT_SYM opt_equal TEXT_STRING_sys	{ Lex->create_info.comment=$3.str; Lex->create_info.used_fields|= HA_CREATE_USED_COMMENT; }
2558 2559 2560
	| 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;}
2561 2562 2563
	| CHECKSUM_SYM opt_equal ULONG_NUM	{ Lex->create_info.table_options|= $3 ? HA_OPTION_CHECKSUM : HA_OPTION_NO_CHECKSUM; Lex->create_info.used_fields|= HA_CREATE_USED_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;  Lex->create_info.used_fields|= HA_CREATE_USED_DELAY_KEY_WRITE; }
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
2564 2565 2566 2567
	| 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 ')'
2568 2569 2570
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
2571 2572
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
2573
	    lex->create_info.merge_list.elements--;
2574 2575
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
2576
	    lex->select_lex.table_list.elements=1;
2577 2578 2579
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
2580
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
2581
	  }
2582 2583
	| default_charset
	| default_collation
2584
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
2585 2586 2587
	| DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.data_file_name= $4.str; Lex->create_info.used_fields|= HA_CREATE_USED_DATADIR; }
	| INDEX_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.index_file_name= $4.str;  Lex->create_info.used_fields|= HA_CREATE_USED_INDEXDIR; }
        ;
unknown's avatar
unknown committed
2588

2589 2590 2591 2592 2593 2594 2595 2596
default_charset:
        opt_default charset opt_equal charset_name_or_default
        {
          HA_CREATE_INFO *cinfo= &Lex->create_info;
          if ((cinfo->used_fields & HA_CREATE_USED_DEFAULT_CHARSET) &&
               cinfo->default_table_charset && $4 &&
               !my_charset_same(cinfo->default_table_charset,$4))
          {
2597 2598 2599
            my_error(ER_CONFLICTING_DECLARATIONS, MYF(0),
                     "CHARACTER SET ", cinfo->default_table_charset->csname,
                     "CHARACTER SET ", $4->csname);
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
            YYABORT;
          }
	  Lex->create_info.default_table_charset= $4;
          Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

default_collation:
        opt_default COLLATE_SYM opt_equal collation_name_or_default
        {
          HA_CREATE_INFO *cinfo= &Lex->create_info;
          if ((cinfo->used_fields & HA_CREATE_USED_DEFAULT_CHARSET) &&
               cinfo->default_table_charset && $4 &&
               !my_charset_same(cinfo->default_table_charset,$4))
            {
2614 2615
              my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $4->name, cinfo->default_table_charset->csname);
2616 2617 2618 2619 2620 2621
              YYABORT;
            }
            Lex->create_info.default_table_charset= $4;
            Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

unknown's avatar
unknown committed
2622
storage_engines:
2623 2624 2625 2626
	ident_or_text
	{
	  $$ = ha_resolve_by_name($1.str,$1.length);
	  if ($$ == DB_TYPE_UNKNOWN) {
2627
	    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
2628 2629 2630
	    YYABORT;
	  }
	};
unknown's avatar
unknown committed
2631 2632 2633 2634 2635

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
2636 2637 2638
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; }
	| REDUNDANT_SYM	{ $$= ROW_TYPE_REDUNDANT; }
	| COMPACT_SYM	{ $$= ROW_TYPE_COMPACT; };
unknown's avatar
unknown committed
2639 2640 2641 2642

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

2645 2646 2647
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
unknown's avatar
unknown committed
2648
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
2649

unknown's avatar
unknown committed
2650
opt_select_from:
2651
	opt_limit_clause {}
unknown's avatar
unknown committed
2652
	| select_from select_lock_type;
unknown's avatar
unknown committed
2653 2654

udf_func_type:
2655
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
unknown's avatar
unknown committed
2656
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
unknown's avatar
unknown committed
2657 2658 2659 2660

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
unknown's avatar
unknown committed
2661
	| INT_SYM {$$ = (int) INT_RESULT; };
unknown's avatar
unknown committed
2662 2663 2664

field_list:
	  field_list_item
unknown's avatar
unknown committed
2665
	| field_list ',' field_list_item;
unknown's avatar
unknown committed
2666 2667 2668


field_list_item:
unknown's avatar
unknown committed
2669
	   column_def
2670 2671 2672 2673
         | key_def
         ;

column_def:
unknown's avatar
unknown committed
2674
	  field_spec opt_check_constraint
unknown's avatar
unknown committed
2675 2676 2677 2678
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
unknown's avatar
unknown committed
2679
	;
2680 2681

key_def:
unknown's avatar
unknown committed
2682
	key_type opt_ident key_alg '(' key_list ')'
unknown's avatar
unknown committed
2683
	  {
unknown's avatar
unknown committed
2684
	    LEX *lex=Lex;
2685
	    lex->key_list.push_back(new Key($1,$2, $3, 0, lex->col_list));
unknown's avatar
unknown committed
2686
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
2687
	  }
2688 2689 2690 2691
	| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
	  {
	    LEX *lex=Lex;
	    const char *key_name= $3 ? $3:$1;
2692 2693
	    lex->key_list.push_back(new Key($2, key_name, $4, 0,
				    lex->col_list));
2694 2695
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
2696
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
unknown's avatar
unknown committed
2697
	  {
2698
	    LEX *lex=Lex;
2699
	    lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
2700 2701 2702 2703 2704
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
2705 2706 2707
	    lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4 : $1,
					    HA_KEY_ALG_UNDEF, 1,
					    lex->col_list));
2708
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
2709
	  }
unknown's avatar
unknown committed
2710 2711 2712 2713
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
2714
	| opt_constraint check_constraint
unknown's avatar
unknown committed
2715 2716
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
2717 2718 2719
	  }
	;

unknown's avatar
unknown committed
2720
opt_check_constraint:
2721
	/* empty */
unknown's avatar
unknown committed
2722 2723 2724 2725 2726
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
2727
	;
unknown's avatar
unknown committed
2728 2729

opt_constraint:
2730
	/* empty */		{ $$=(char*) 0; }
unknown's avatar
unknown committed
2731 2732 2733 2734 2735 2736
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
unknown's avatar
unknown committed
2737 2738 2739 2740

field_spec:
	field_ident
	 {
unknown's avatar
unknown committed
2741
	   LEX *lex=Lex;
2742
	   lex->length=lex->dec=0; lex->type=0;
2743
	   lex->default_value= lex->on_update_value= 0;
unknown's avatar
unknown committed
2744
	   lex->comment=0;
2745
	   lex->charset=NULL;
unknown's avatar
unknown committed
2746 2747 2748
	 }
	type opt_attribute
	{
unknown's avatar
unknown committed
2749
	  LEX *lex=Lex;
unknown's avatar
unknown committed
2750
	  if (add_field_to_list(lex->thd, $1.str,
unknown's avatar
unknown committed
2751
				(enum enum_field_types) $3,
unknown's avatar
unknown committed
2752
				lex->length,lex->dec,lex->type,
2753 2754
				lex->default_value, lex->on_update_value, 
                                lex->comment,
2755
				lex->change,&lex->interval_list,lex->charset,
unknown's avatar
unknown committed
2756
				lex->uint_geom_type))
unknown's avatar
unknown committed
2757
	    YYABORT;
unknown's avatar
unknown committed
2758
	};
unknown's avatar
unknown committed
2759 2760

type:
2761
	int_type opt_len field_options	{ $$=$1; }
unknown's avatar
unknown committed
2762 2763
	| real_type opt_precision field_options { $$=$1; }
	| FLOAT_SYM float_options field_options { $$=FIELD_TYPE_FLOAT; }
unknown's avatar
unknown committed
2764 2765 2766 2767
	| BIT_SYM			{ Lex->length= (char*) "1";
					  $$=FIELD_TYPE_BIT; }
	| BIT_SYM '(' NUM ')'		{ Lex->length= $3.str;
					  $$=FIELD_TYPE_BIT; }
unknown's avatar
unknown committed
2768 2769
	| BOOL_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
2770 2771
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
2772
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
unknown's avatar
unknown committed
2773 2774 2775
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
2776
	| nchar '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
2777
					  $$=FIELD_TYPE_STRING;
2778
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
2779
	| nchar				{ Lex->length=(char*) "1";
unknown's avatar
unknown committed
2780
					  $$=FIELD_TYPE_STRING;
2781
					  Lex->charset=national_charset_info; }
2782
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
2783
					  Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2784
					  $$=FIELD_TYPE_STRING; }
2785 2786 2787
	| BINARY			{ Lex->length= (char*) "1";
					  Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
2788
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
2789
					  $$= MYSQL_TYPE_VARCHAR; }
unknown's avatar
unknown committed
2790
	| nvarchar '(' NUM ')'		{ Lex->length=$3.str;
2791
					  $$= MYSQL_TYPE_VARCHAR;
2792
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
2793
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
unknown's avatar
unknown committed
2794
					  Lex->charset=&my_charset_bin;
2795
					  $$= MYSQL_TYPE_VARCHAR; }
2796
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
unknown's avatar
unknown committed
2797 2798
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
2799
	| TIMESTAMP opt_len
2800
	  {
2801
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
2802 2803
	      $$=FIELD_TYPE_DATETIME;
	    else
2804 2805 2806 2807 2808
            {
              /* 
                Unlike other types TIMESTAMP fields are NOT NULL by default.
              */
              Lex->type|= NOT_NULL_FLAG;
2809
	      $$=FIELD_TYPE_TIMESTAMP;
2810
            }
2811
	   }
unknown's avatar
unknown committed
2812
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
unknown's avatar
unknown committed
2813
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2814
					  $$=FIELD_TYPE_TINY_BLOB; }
unknown's avatar
unknown committed
2815
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2816
					  $$=FIELD_TYPE_BLOB; }
unknown's avatar
unknown committed
2817 2818
	| spatial_type
          {
unknown's avatar
unknown committed
2819
#ifdef HAVE_SPATIAL
unknown's avatar
unknown committed
2820 2821 2822
            Lex->charset=&my_charset_bin;
            Lex->uint_geom_type= (uint)$1;
            $$=FIELD_TYPE_GEOMETRY;
unknown's avatar
unknown committed
2823
#else
2824 2825
            my_error(ER_FEATURE_DISABLED, MYF(0)
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
2826
            YYABORT;
unknown's avatar
unknown committed
2827
#endif
unknown's avatar
unknown committed
2828
          }
unknown's avatar
unknown committed
2829
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2830
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
2831
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2832
					  $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
2833
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2834
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
2835 2836
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
2837
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
2838 2839
	| MEDIUMTEXT opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| LONGTEXT opt_binary		{ $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
2840 2841 2842 2843
	| DECIMAL_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
	| NUMERIC_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
2844 2845
	| FIXED_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
2846
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
2847
	  { $$=FIELD_TYPE_ENUM; }
2848
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
2849
	  { $$=FIELD_TYPE_SET; }
2850
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
2851 2852 2853 2854 2855 2856
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
2857
	;
unknown's avatar
unknown committed
2858

unknown's avatar
unknown committed
2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
	| POINT_SYM           { $$= Field::GEOM_POINT; }
	| MULTIPOINT          { $$= Field::GEOM_MULTIPOINT; }
	| LINESTRING          { $$= Field::GEOM_LINESTRING; }
	| MULTILINESTRING     { $$= Field::GEOM_MULTILINESTRING; }
	| POLYGON             { $$= Field::GEOM_POLYGON; }
	| MULTIPOLYGON        { $$= Field::GEOM_MULTIPOLYGON; }
	;

unknown's avatar
unknown committed
2870 2871
char:
	CHAR_SYM {}
unknown's avatar
unknown committed
2872 2873 2874 2875 2876 2877
	;

nchar:
	NCHAR_SYM {}
	| NATIONAL_SYM CHAR_SYM {}
	;
unknown's avatar
unknown committed
2878

unknown's avatar
unknown committed
2879 2880 2881
varchar:
	char VARYING {}
	| VARCHAR {}
unknown's avatar
unknown committed
2882 2883 2884 2885
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
unknown's avatar
unknown committed
2886
	| NVARCHAR_SYM {}
unknown's avatar
unknown committed
2887 2888 2889 2890
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
unknown's avatar
unknown committed
2891 2892 2893 2894 2895 2896

int_type:
	INT_SYM		{ $$=FIELD_TYPE_LONG; }
	| TINYINT	{ $$=FIELD_TYPE_TINY; }
	| SMALLINT	{ $$=FIELD_TYPE_SHORT; }
	| MEDIUMINT	{ $$=FIELD_TYPE_INT24; }
unknown's avatar
unknown committed
2897
	| BIGINT	{ $$=FIELD_TYPE_LONGLONG; };
unknown's avatar
unknown committed
2898 2899

real_type:
2900
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
unknown's avatar
unknown committed
2901 2902
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
unknown's avatar
unknown committed
2903
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
unknown's avatar
unknown committed
2904 2905 2906 2907 2908


float_options:
	/* empty */		{}
	| '(' NUM ')'		{ Lex->length=$2.str; }
unknown's avatar
unknown committed
2909
	| precision		{};
unknown's avatar
unknown committed
2910 2911 2912 2913 2914 2915

precision:
	'(' NUM ',' NUM ')'
	{
	  LEX *lex=Lex;
	  lex->length=$2.str; lex->dec=$4.str;
unknown's avatar
unknown committed
2916
	};
unknown's avatar
unknown committed
2917 2918 2919

field_options:
	/* empty */		{}
unknown's avatar
unknown committed
2920
	| field_opt_list	{};
unknown's avatar
unknown committed
2921 2922 2923

field_opt_list:
	field_opt_list field_option {}
unknown's avatar
unknown committed
2924
	| field_option {};
unknown's avatar
unknown committed
2925 2926

field_option:
unknown's avatar
unknown committed
2927
	SIGNED_SYM	{}
2928
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
unknown's avatar
unknown committed
2929
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
unknown's avatar
unknown committed
2930 2931

opt_len:
2932 2933
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
unknown's avatar
unknown committed
2934 2935 2936

opt_precision:
	/* empty */	{}
unknown's avatar
unknown committed
2937
	| precision	{};
unknown's avatar
unknown committed
2938 2939 2940

opt_attribute:
	/* empty */ {}
unknown's avatar
unknown committed
2941
	| opt_attribute_list {};
unknown's avatar
unknown committed
2942 2943 2944

opt_attribute_list:
	opt_attribute_list attribute {}
unknown's avatar
unknown committed
2945
	| attribute;
unknown's avatar
unknown committed
2946 2947 2948

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
2949
	| not NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
2950 2951 2952
	| DEFAULT now_or_signed_literal { Lex->default_value=$2; }
	| ON UPDATE_SYM NOW_SYM optional_braces 
          { Lex->on_update_value= new Item_func_now_local(); }
unknown's avatar
unknown committed
2953
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
2954
	| SERIAL_SYM DEFAULT VALUE_SYM
2955 2956 2957
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
2958
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2959 2960 2961 2962 2963
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
2964
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2965 2966 2967 2968 2969
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
2970
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2971 2972 2973 2974 2975
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
2976
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2977
	  }
unknown's avatar
unknown committed
2978
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= &$2; }
2979
	| BINARY { Lex->type|= BINCMP_FLAG; }
unknown's avatar
unknown committed
2980 2981
	| COLLATE_SYM collation_name
	  {
unknown's avatar
unknown committed
2982
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
unknown's avatar
unknown committed
2983
	    {
2984 2985
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $2->name,Lex->charset->csname);
unknown's avatar
unknown committed
2986 2987 2988 2989 2990 2991 2992 2993
	      YYABORT;
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
unknown's avatar
unknown committed
2994

2995 2996 2997 2998 2999
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

3000 3001 3002 3003
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
unknown's avatar
unknown committed
3004

3005
charset_name:
3006
	ident_or_text
3007
	{
unknown's avatar
unknown committed
3008
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
3009
	  {
3010
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
3011 3012
	    YYABORT;
	  }
unknown's avatar
unknown committed
3013 3014 3015
	}
	| BINARY { $$= &my_charset_bin; }
	;
3016

3017 3018 3019
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
unknown's avatar
unknown committed
3020

unknown's avatar
unknown committed
3021 3022 3023 3024 3025 3026 3027

old_or_new_charset_name:
	ident_or_text
	{
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))) &&
	      !($$=get_old_charset_by_name($1.str)))
	  {
3028
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
unknown's avatar
unknown committed
3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
	    YYABORT;
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

old_or_new_charset_name_or_default:
	old_or_new_charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

3039
collation_name:
3040
	ident_or_text
3041 3042 3043
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
3044
	    my_error(ER_UNKNOWN_COLLATION, MYF(0), $1.str);
3045 3046 3047 3048
	    YYABORT;
	  }
	};

3049 3050
opt_collate:
	/* empty */	{ $$=NULL; }
unknown's avatar
unknown committed
3051
	| COLLATE_SYM collation_name_or_default { $$=$2; }
3052 3053
	;

3054 3055 3056 3057
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

3058 3059 3060 3061
opt_default:
	/* empty */	{}
	| DEFAULT	{};

unknown's avatar
unknown committed
3062
opt_binary:
3063
	/* empty */			{ Lex->charset=NULL; }
unknown's avatar
unknown committed
3064 3065
	| ASCII_SYM			{ Lex->charset=&my_charset_latin1; }
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
unknown's avatar
unknown committed
3066 3067
	| UNICODE_SYM
	{
unknown's avatar
unknown committed
3068 3069
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
unknown's avatar
unknown committed
3070
	  {
unknown's avatar
unknown committed
3071
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
unknown's avatar
unknown committed
3072 3073 3074
	    YYABORT;
	  }
	}
3075
	| charset charset_name	{ Lex->charset=$2; } ;
unknown's avatar
unknown committed
3076

unknown's avatar
unknown committed
3077 3078 3079
opt_primary:
	/* empty */
	| PRIMARY_SYM
3080
	;
unknown's avatar
unknown committed
3081

unknown's avatar
unknown committed
3082
references:
3083 3084
	REFERENCES table_ident
	{
3085
	  LEX *lex=Lex;
3086 3087 3088 3089 3090 3091
	  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
3092
	};
3093

3094
opt_ref_list:
unknown's avatar
unknown committed
3095
	/* empty */ opt_on_delete {}
unknown's avatar
unknown committed
3096
	| '(' ref_list ')' opt_on_delete {};
3097 3098 3099

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

unknown's avatar
unknown committed
3102 3103 3104

opt_on_delete:
	/* empty */ {}
unknown's avatar
unknown committed
3105
	| opt_on_delete_list {};
unknown's avatar
unknown committed
3106 3107 3108

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
unknown's avatar
unknown committed
3109
	| opt_on_delete_item {};
unknown's avatar
unknown committed
3110 3111

opt_on_delete_item:
3112 3113 3114 3115
	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
3116
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
unknown's avatar
unknown committed
3117 3118

delete_option:
3119 3120 3121 3122
	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
3123
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
unknown's avatar
unknown committed
3124 3125

key_type:
3126
	key_or_index			    { $$= Key::MULTIPLE; }
unknown's avatar
SCRUM  
unknown committed
3127 3128
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
unknown's avatar
unknown committed
3129 3130 3131 3132
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
3133 3134
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3135 3136 3137
	    YYABORT;
#endif
	  };
3138 3139 3140

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
unknown's avatar
SCRUM  
unknown committed
3141
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
unknown's avatar
unknown committed
3142 3143 3144

key_or_index:
	KEY_SYM {}
unknown's avatar
unknown committed
3145
	| INDEX_SYM {};
unknown's avatar
unknown committed
3146

unknown's avatar
SCRUM  
unknown committed
3147 3148 3149 3150 3151
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

unknown's avatar
unknown committed
3152 3153
keys_or_index:
	KEYS {}
unknown's avatar
unknown committed
3154
	| INDEX_SYM {}
unknown's avatar
unknown committed
3155
	| INDEXES {};
unknown's avatar
unknown committed
3156

3157
opt_unique_or_fulltext:
unknown's avatar
unknown committed
3158 3159
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
unknown's avatar
unknown committed
3160
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
unknown's avatar
unknown committed
3161 3162 3163 3164 3165
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
unknown's avatar
unknown committed
3166
	    my_message(ER_FEATURE_DISABLED, ER(ER_FEATURE_DISABLED), MYF(0),
unknown's avatar
unknown committed
3167
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3168 3169 3170
	    YYABORT;
#endif
	  }
unknown's avatar
unknown committed
3171
        ;
unknown's avatar
unknown committed
3172 3173

key_alg:
3174
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
3175
	| USING opt_btree_or_rtree { $$= $2; }
unknown's avatar
unknown committed
3176
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
unknown's avatar
unknown committed
3177 3178 3179

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
unknown's avatar
unknown committed
3180 3181 3182 3183
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
unknown's avatar
unknown committed
3184
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
unknown's avatar
unknown committed
3185 3186 3187

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
unknown's avatar
unknown committed
3188
	| key_part order_dir		{ Lex->col_list.push_back($1); };
unknown's avatar
unknown committed
3189 3190 3191

key_part:
	ident			{ $$=new key_part_spec($1.str); }
3192 3193 3194 3195 3196
	| ident '(' NUM ')'	
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
3197
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
3198 3199 3200
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
unknown's avatar
unknown committed
3201 3202 3203

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

3206 3207 3208
opt_component:
	/* empty */	 { $$.str= 0; $$.length= 0; }
	| '.' ident	 { $$=$2; };
3209

unknown's avatar
unknown committed
3210 3211
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
unknown's avatar
unknown committed
3212
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
unknown's avatar
unknown committed
3213 3214 3215 3216 3217 3218 3219 3220

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
3221
	  THD *thd= YYTHD;
3222
	  LEX *lex= thd->lex;
3223 3224 3225
	  lex->sql_command= SQLCOM_ALTER_TABLE;
	  lex->name= 0;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
3226 3227
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
unknown's avatar
unknown committed
3228 3229 3230 3231
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
3232
          lex->select_lex.init_order();
3233
	  lex->select_lex.db=lex->name=0;
3234
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
unknown's avatar
unknown committed
3235
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
3236
	  lex->create_info.default_table_charset= NULL;
3237
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
3238
	  lex->alter_info.reset();
3239
	  lex->alter_info.flags= 0;
unknown's avatar
unknown committed
3240
	}
3241 3242
	alter_list
	{}
3243
	| ALTER DATABASE ident_or_empty
3244 3245 3246 3247 3248
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
          opt_create_database_options
3249 3250 3251
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
3252
	    lex->name= $3;
3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    THD *thd= YYTHD;
	    LEX *lex=Lex;

	    lex->sql_command= SQLCOM_ALTER_PROCEDURE;
	    lex->spname= $3;
	  }
	| ALTER FUNCTION_SYM sp_name
	  {
	    LEX *lex= Lex;
3271

3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    THD *thd= YYTHD;
	    LEX *lex=Lex;

	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
	    lex->spname= $3;
	  }
	| ALTER algorithm VIEW_SYM table_ident
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_CREATE_VIEW;
	    lex->create_view_mode= VIEW_ALTER;
	    lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE;
	    /* first table in list is target VIEW name */
	    lex->select_lex.add_table_to_list(thd, $4, NULL, 0);
	  }
	  opt_view_list AS select_init check_option
	  {}
	;
3295

3296 3297 3298 3299
ident_or_empty:
	/* empty */  { $$= 0; }
	| ident      { $$= $1.str; };

unknown's avatar
unknown committed
3300
alter_list:
3301 3302
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
unknown's avatar
unknown committed
3303
        | alter_list_item
unknown's avatar
unknown committed
3304
	| alter_list ',' alter_list_item;
unknown's avatar
unknown committed
3305 3306

add_column:
3307
	ADD opt_column
3308 3309
	{
	  LEX *lex=Lex;
3310 3311
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
3312
	};
unknown's avatar
unknown committed
3313 3314

alter_list_item:
3315 3316 3317 3318
	add_column column_def opt_place { }
	| ADD key_def
	  {
	    Lex->alter_info.flags|= ALTER_ADD_INDEX;
3319
	  }
3320 3321 3322 3323
	| add_column '(' field_list ')'
          {
	    Lex->alter_info.flags|= ALTER_ADD_COLUMN | ALTER_ADD_INDEX;
          }
unknown's avatar
unknown committed
3324 3325 3326
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
3327
	     lex->change= $3.str;
3328
	     lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
3329
	  }
3330
          field_spec opt_place
unknown's avatar
unknown committed
3331 3332 3333
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
3334
            lex->length=lex->dec=0; lex->type=0;
3335
            lex->default_value= lex->on_update_value= 0;
unknown's avatar
unknown committed
3336
	    lex->comment=0;
3337
	    lex->charset= NULL;
3338
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
3339 3340 3341 3342
          }
          type opt_attribute
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
3343
            if (add_field_to_list(lex->thd,$3.str,
unknown's avatar
unknown committed
3344 3345
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
3346 3347
                                  lex->default_value, lex->on_update_value,
                                  lex->comment,
3348
				  $3.str, &lex->interval_list, lex->charset,
unknown's avatar
unknown committed
3349
				  lex->uint_geom_type))
unknown's avatar
unknown committed
3350 3351 3352
	       YYABORT;
          }
          opt_place
unknown's avatar
unknown committed
3353
	| DROP opt_column field_ident opt_restrict
unknown's avatar
unknown committed
3354 3355
	  {
	    LEX *lex=Lex;
3356
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
3357
                                                               $3.str));
3358
	    lex->alter_info.flags|= ALTER_DROP_COLUMN;
unknown's avatar
unknown committed
3359
	  }
3360 3361 3362 3363
	| DROP FOREIGN KEY_SYM opt_ident
          {
	    Lex->alter_info.flags|= ALTER_DROP_INDEX;
          }
unknown's avatar
unknown committed
3364 3365 3366
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
3367 3368 3369
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
				               primary_key_name));
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
unknown's avatar
unknown committed
3370
	  }
unknown's avatar
unknown committed
3371
	| DROP key_or_index field_ident
unknown's avatar
unknown committed
3372 3373
	  {
	    LEX *lex=Lex;
3374 3375 3376
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
					                       $3.str));
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
unknown's avatar
unknown committed
3377
	  }
3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389
	| DISABLE_SYM KEYS
          {
	    LEX *lex=Lex;
            lex->alter_info.keys_onoff= DISABLE;
	    lex->alter_info.flags|= ALTER_KEYS_ONOFF;
          }
	| ENABLE_SYM KEYS
          {
	    LEX *lex=Lex;
            lex->alter_info.keys_onoff= ENABLE;
	    lex->alter_info.flags|= ALTER_KEYS_ONOFF;
          }
unknown's avatar
unknown committed
3390
	| ALTER opt_column field_ident SET DEFAULT signed_literal
unknown's avatar
unknown committed
3391 3392
	  {
	    LEX *lex=Lex;
3393
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,$6));
3394
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
3395
	  }
unknown's avatar
unknown committed
3396
	| ALTER opt_column field_ident DROP DEFAULT
unknown's avatar
unknown committed
3397 3398
	  {
	    LEX *lex=Lex;
3399 3400
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,
                                                                  (Item*) 0));
3401
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
3402
	  }
3403
	| RENAME opt_to table_ident
3404
	  {
3405
	    LEX *lex=Lex;
3406
	    lex->select_lex.db=$3->db.str;
3407
	    lex->name= $3->table.str;
3408 3409 3410
            if (check_table_name($3->table.str,$3->table.length) ||
                $3->db.str && check_db_name($3->db.str))
            {
3411
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3->table.str);
3412 3413
              YYABORT;
            }
3414
	    lex->alter_info.flags|= ALTER_RENAME;
3415
	  }
3416 3417 3418 3419 3420 3421 3422 3423 3424 3425
	| CONVERT_SYM TO_SYM charset charset_name_or_default opt_collate
	  {
	    if (!$4)
	    {
	      THD *thd= YYTHD;
	      $4= thd->variables.collation_database;
	    }
	    $5= $5 ? $5 : $4;
	    if (!my_charset_same($4,$5))
	    {
3426 3427
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $5->name, $4->csname);
3428 3429
	      YYABORT;
	    }
3430
	    LEX *lex= Lex;
3431
	    lex->create_info.table_charset=
unknown's avatar
unknown committed
3432 3433 3434
	      lex->create_info.default_table_charset= $5;
	    lex->create_info.used_fields|= (HA_CREATE_USED_CHARSET |
					    HA_CREATE_USED_DEFAULT_CHARSET);
3435
	    lex->alter_info.flags|= ALTER_CONVERT;
3436
	  }
3437
        | create_table_options_space_separated
3438 3439
	  {
	    LEX *lex=Lex;
3440
	    lex->alter_info.flags|= ALTER_OPTIONS;
3441
	  }
3442
	| order_clause
3443 3444
	  {
	    LEX *lex=Lex;
3445
	    lex->alter_info.flags|= ALTER_ORDER;
3446
	  };
unknown's avatar
unknown committed
3447 3448 3449

opt_column:
	/* empty */	{}
unknown's avatar
unknown committed
3450
	| COLUMN_SYM	{};
unknown's avatar
unknown committed
3451 3452

opt_ignore:
3453 3454 3455
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
unknown's avatar
unknown committed
3456 3457

opt_restrict:
3458 3459 3460 3461
	/* empty */	{ Lex->drop_mode= DROP_DEFAULT; }
	| RESTRICT	{ Lex->drop_mode= DROP_RESTRICT; }
	| CASCADE	{ Lex->drop_mode= DROP_CASCADE; }
	;
unknown's avatar
unknown committed
3462 3463 3464 3465

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
unknown's avatar
unknown committed
3466
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
unknown's avatar
unknown committed
3467 3468 3469 3470

opt_to:
	/* empty */	{}
	| TO_SYM	{}
3471
	| EQ		{}
unknown's avatar
unknown committed
3472
	| AS		{};
unknown's avatar
unknown committed
3473

unknown's avatar
unknown committed
3474
/*
3475
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
unknown's avatar
unknown committed
3476 3477
*/

unknown's avatar
unknown committed
3478
slave:
3479
	  START_SYM SLAVE slave_thread_opts
3480 3481 3482 3483 3484 3485
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_START;
	    lex->type = 0;
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
unknown's avatar
unknown committed
3486
            /* If you change this code don't forget to update SLAVE START too */
3487 3488 3489
          }
          slave_until
          {}
unknown's avatar
unknown committed
3490 3491 3492 3493 3494
        | STOP_SYM SLAVE slave_thread_opts
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_STOP;
	    lex->type = 0;
unknown's avatar
unknown committed
3495
            /* If you change this code don't forget to update SLAVE STOP too */
unknown's avatar
unknown committed
3496
          }
3497 3498 3499 3500 3501
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
3502 3503 3504 3505 3506
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
3507 3508 3509 3510 3511 3512 3513 3514
	| SLAVE STOP_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_STOP;
	   lex->type = 0;
         }
        ;

unknown's avatar
unknown committed
3515

unknown's avatar
unknown committed
3516
start:
3517 3518 3519 3520 3521
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
           Lex->sql_command = SQLCOM_BEGIN;
           Lex->start_transaction_opt= $3;
        }
unknown's avatar
unknown committed
3522 3523
	;

3524 3525 3526 3527 3528 3529
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
3530
        ;
3531

unknown's avatar
unknown committed
3532
slave_thread_opts:
3533 3534
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
3535
        {}
unknown's avatar
unknown committed
3536
	;
3537 3538

slave_thread_opt_list:
unknown's avatar
unknown committed
3539
	slave_thread_opt
3540 3541
	| slave_thread_opt_list ',' slave_thread_opt
	;
3542 3543

slave_thread_opt:
3544
	/*empty*/	{}
unknown's avatar
unknown committed
3545
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
unknown's avatar
unknown committed
3546
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
unknown's avatar
unknown committed
3547
	;
3548

3549 3550 3551 3552 3553 3554 3555 3556 3557 3558
slave_until:
	/*empty*/	{}
	| UNTIL_SYM slave_until_opts
          {
            LEX *lex=Lex;
            if ((lex->mi.log_file_name || lex->mi.pos) &&
                (lex->mi.relay_log_name || lex->mi.relay_log_pos) ||
                !((lex->mi.log_file_name && lex->mi.pos) ||
                  (lex->mi.relay_log_name && lex->mi.relay_log_pos)))
            {
unknown's avatar
unknown committed
3559 3560
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


unknown's avatar
unknown committed
3572 3573 3574 3575 3576
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
	}
unknown's avatar
unknown committed
3577
	table_list FROM TEXT_STRING_sys
unknown's avatar
unknown committed
3578 3579
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
3580
        };
unknown's avatar
unknown committed
3581

unknown's avatar
unknown committed
3582 3583 3584 3585 3586
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
	}
unknown's avatar
unknown committed
3587
	table_list TO_SYM TEXT_STRING_sys
unknown's avatar
unknown committed
3588 3589
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
3590
        };
unknown's avatar
unknown committed
3591

3592 3593 3594 3595 3596 3597
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
3598 3599
	table_list opt_checksum_type
        {}
3600 3601
	;

3602 3603 3604 3605 3606 3607
opt_checksum_type:
        /* nothing */  { Lex->check_opt.flags= 0; }
	| QUICK        { Lex->check_opt.flags= T_QUICK; }
	| EXTENDED_SYM { Lex->check_opt.flags= T_EXTEND; }
        ;

unknown's avatar
unknown committed
3608
repair:
3609
	REPAIR opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3610
	{
unknown's avatar
unknown committed
3611 3612
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
3613
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3614
	   lex->check_opt.init();
unknown's avatar
unknown committed
3615
	}
3616 3617 3618
	table_list opt_mi_repair_type
	{}
	;
unknown's avatar
unknown committed
3619

unknown's avatar
unknown committed
3620
opt_mi_repair_type:
unknown's avatar
unknown committed
3621
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
3622
	| mi_repair_types {};
unknown's avatar
unknown committed
3623

unknown's avatar
unknown committed
3624 3625
mi_repair_types:
	mi_repair_type {}
unknown's avatar
unknown committed
3626
	| mi_repair_type mi_repair_types {};
3627

unknown's avatar
unknown committed
3628
mi_repair_type:
unknown's avatar
unknown committed
3629
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
3630
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
unknown's avatar
unknown committed
3631
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
unknown's avatar
unknown committed
3632 3633

analyze:
3634
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3635
	{
unknown's avatar
unknown committed
3636 3637
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
3638
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3639
	   lex->check_opt.init();
unknown's avatar
unknown committed
3640
	}
3641 3642 3643
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3644 3645

check:
unknown's avatar
unknown committed
3646
	CHECK_SYM table_or_tables
unknown's avatar
unknown committed
3647
	{
unknown's avatar
unknown committed
3648 3649 3650
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECK;
	   lex->check_opt.init();
unknown's avatar
unknown committed
3651
	}
3652 3653 3654
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3655

unknown's avatar
unknown committed
3656 3657
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
3658
	| mi_check_types {};
unknown's avatar
unknown committed
3659 3660 3661

mi_check_types:
	mi_check_type {}
unknown's avatar
unknown committed
3662
	| mi_check_type mi_check_types {};
unknown's avatar
unknown committed
3663 3664 3665 3666 3667 3668

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

unknown's avatar
unknown committed
3671
optimize:
3672
	OPTIMIZE opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3673
	{
unknown's avatar
unknown committed
3674 3675
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
unknown's avatar
unknown committed
3676
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3677
	   lex->check_opt.init();
unknown's avatar
unknown committed
3678
	}
3679 3680 3681
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3682

3683 3684 3685
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
3686
	| LOCAL_SYM  { $$= 1; }
3687 3688
	;

3689 3690 3691 3692 3693
rename:
	RENAME table_or_tables
	{
	   Lex->sql_command=SQLCOM_RENAME_TABLE;
	}
3694 3695
	table_to_table_list
	{}
3696 3697 3698 3699
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
3700
	;
3701

3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714
rename_list:
        user TO_SYM user
        {
          if (Lex->users_list.push_back($1) || Lex->users_list.push_back($3))
            YYABORT;
        }
        | rename_list ',' user TO_SYM user
          {
            if (Lex->users_list.push_back($3) || Lex->users_list.push_back($5))
              YYABORT;
          }
        ;

3715 3716
table_to_table_list:
	table_to_table
unknown's avatar
unknown committed
3717
	| table_to_table_list ',' table_to_table;
3718 3719 3720

table_to_table:
	table_ident TO_SYM table_ident
3721
	{
unknown's avatar
unknown committed
3722
	  LEX *lex=Lex;
unknown's avatar
unknown committed
3723
	  SELECT_LEX *sl= lex->current_select;
unknown's avatar
unknown committed
3724 3725 3726 3727
	  if (!sl->add_table_to_list(lex->thd, $1,NULL,TL_OPTION_UPDATING,
				     TL_IGNORE) ||
	      !sl->add_table_to_list(lex->thd, $3,NULL,TL_OPTION_UPDATING,
				     TL_IGNORE))
3728
	    YYABORT;
3729
	};
3730

unknown's avatar
unknown committed
3731
keycache:
unknown's avatar
unknown committed
3732
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
unknown's avatar
unknown committed
3733 3734
        {
          LEX *lex=Lex;
3735 3736
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
	  lex->name_and_length= $5;
unknown's avatar
unknown committed
3737 3738 3739 3740 3741 3742 3743 3744
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
3745
        table_ident cache_keys_spec
unknown's avatar
unknown committed
3746 3747 3748 3749 3750 3751
        {
          LEX *lex=Lex;
          SELECT_LEX *sel= &lex->select_lex;
          if (!sel->add_table_to_list(lex->thd, $1, NULL, 0,
                                      TL_READ,
                                      sel->get_use_index(),
3752
                                      (List<String> *)0))
unknown's avatar
unknown committed
3753 3754 3755 3756
            YYABORT;
        }
        ;

3757 3758 3759
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
3760
	;
3761

unknown's avatar
unknown committed
3762
preload:
unknown's avatar
unknown committed
3763
	LOAD INDEX_SYM INTO CACHE_SYM
unknown's avatar
unknown committed
3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776
	{
	  LEX *lex=Lex;
	  lex->sql_command=SQLCOM_PRELOAD_KEYS;
	}
	preload_list
	{}
	;

preload_list:
	preload_keys
	| preload_list ',' preload_keys;

preload_keys:
unknown's avatar
unknown committed
3777
	table_ident cache_keys_spec opt_ignore_leaves
unknown's avatar
unknown committed
3778 3779 3780
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
unknown's avatar
unknown committed
3781 3782
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
unknown's avatar
unknown committed
3783 3784 3785 3786 3787 3788
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

3789
cache_keys_spec:
3790
        { Select->interval_list.empty(); }
3791 3792 3793 3794 3795 3796 3797
        cache_key_list_or_empty
        {
          LEX *lex=Lex;
          SELECT_LEX *sel= &lex->select_lex;
          sel->use_index= sel->interval_list;
        }
        ;
unknown's avatar
unknown committed
3798

unknown's avatar
unknown committed
3799
cache_key_list_or_empty:
3800
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
3801
	| opt_key_or_index '(' key_usage_list2 ')'
3802 3803 3804 3805
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
3806 3807
	;

unknown's avatar
unknown committed
3808
opt_ignore_leaves:
unknown's avatar
unknown committed
3809 3810 3811 3812 3813
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

unknown's avatar
unknown committed
3814
/*
unknown's avatar
unknown committed
3815
  Select : retrieve data from table
unknown's avatar
unknown committed
3816 3817 3818 3819
*/


select:
3820 3821 3822 3823 3824
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
	  lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE;
unknown's avatar
unknown committed
3825 3826
	}
	;
unknown's avatar
unknown committed
3827

3828
/* Need select_init2 for subselects. */
unknown's avatar
unknown committed
3829
select_init:
3830
	SELECT_SYM select_init2
3831
	|
3832 3833
	'(' SELECT_SYM select_part2 ')'
	  {
3834
	    LEX *lex= Lex;
unknown's avatar
unknown committed
3835
            SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
3836
	    if (sel->set_braces(1))
3837
	    {
3838
	      yyerror(ER(ER_SYNTAX_ERROR));
3839 3840
	      YYABORT;
	    }
3841 3842
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
3843
	  {
3844
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
3845 3846
	    YYABORT;
	  }
3847
            /* select in braces, can't contain global parameters */
unknown's avatar
unknown committed
3848 3849 3850
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
unknown's avatar
unknown committed
3851
          } union_opt;
3852

3853 3854
select_init2:
	select_part2
3855
        {
3856
	  LEX *lex= Lex;
unknown's avatar
unknown committed
3857
          SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
3858
          if (lex->current_select->set_braces(0))
3859
	  {
3860
	    yyerror(ER(ER_SYNTAX_ERROR));
3861 3862
	    YYABORT;
	  }
3863 3864
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
3865
	  {
3866
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
3867 3868
	    YYABORT;
	  }
3869
	}
3870
	union_clause
3871 3872
	;

3873
select_part2:
unknown's avatar
unknown committed
3874
	{
unknown's avatar
unknown committed
3875 3876 3877
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
	  lex->lock_option= TL_READ;
unknown's avatar
unknown committed
3878 3879
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
3880
	  lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
3881 3882 3883
	}
	select_options select_item_list
	{
3884
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
3885
	}
unknown's avatar
unknown committed
3886
	select_into select_lock_type;
3887

unknown's avatar
unknown committed
3888
select_into:
3889 3890
	opt_limit_clause {}
        | into
unknown's avatar
unknown committed
3891
	| select_from
3892 3893
	| into select_from
	| select_from into;
unknown's avatar
unknown committed
3894 3895

select_from:
3896 3897
	  FROM join_table_list where_clause group_clause having_clause
	       opt_order_clause opt_limit_clause procedure_clause
unknown's avatar
unknown committed
3898 3899 3900 3901 3902
        | FROM DUAL_SYM opt_limit_clause
          /* 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 :) */
3903
	;
unknown's avatar
unknown committed
3904 3905 3906

select_options:
	/* empty*/
unknown's avatar
unknown committed
3907
	| select_option_list;
unknown's avatar
unknown committed
3908 3909 3910

select_option_list:
	select_option_list select_option
unknown's avatar
unknown committed
3911
	| select_option;
unknown's avatar
unknown committed
3912 3913

select_option:
3914
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
3915 3916 3917 3918 3919 3920
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
3921 3922 3923
	| DISTINCT	{ Select->options|= SELECT_DISTINCT; }
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935
	| 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;
	  }
3936
	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
3937 3938 3939 3940
	| SQL_CACHE_SYM
	  {
	    Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
	  }
3941 3942
	| ALL		{}
	;
unknown's avatar
unknown committed
3943

unknown's avatar
unknown committed
3944 3945 3946
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
3947 3948
	  {
	    LEX *lex=Lex;
3949
	    lex->current_select->set_lock_for_tables(TL_WRITE);
3950
	    lex->safe_to_cache_query=0;
3951
	  }
unknown's avatar
unknown committed
3952
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
3953 3954
	  {
	    LEX *lex=Lex;
3955 3956
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
3957
	    lex->safe_to_cache_query=0;
3958 3959
	  }
	;
unknown's avatar
unknown committed
3960

unknown's avatar
unknown committed
3961 3962 3963 3964 3965
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
3966 3967
	    THD *thd= YYTHD;
	    if (add_item_to_list(thd, new Item_field(NULL, NULL, "*")))
unknown's avatar
unknown committed
3968
	      YYABORT;
3969
	    (thd->lex->current_select->with_wild)++;
unknown's avatar
unknown committed
3970
	  };
unknown's avatar
unknown committed
3971 3972 3973 3974 3975


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
unknown's avatar
unknown committed
3976
	    if (add_item_to_list(YYTHD, $2))
unknown's avatar
unknown committed
3977 3978
	      YYABORT;
	    if ($4.str)
unknown's avatar
unknown committed
3979
	      $2->set_name($4.str,$4.length,system_charset_info);
3980 3981 3982 3983 3984 3985
	    else if (!$2->name) {
	      char *str = $1;
	      if (str[-1] == '`')
	        str--;
	      $2->set_name(str,(uint) ($3 - str), YYTHD->charset());
	    }
unknown's avatar
unknown committed
3986
	  };
unknown's avatar
unknown committed
3987 3988

remember_name:
unknown's avatar
unknown committed
3989
	{ $$=(char*) Lex->tok_start; };
unknown's avatar
unknown committed
3990 3991

remember_end:
unknown's avatar
unknown committed
3992
	{ $$=(char*) Lex->tok_end; };
unknown's avatar
unknown committed
3993 3994 3995

select_item2:
	table_wild	{ $$=$1; } /* table.* */
unknown's avatar
unknown committed
3996
	| expr		{ $$=$1; };
unknown's avatar
unknown committed
3997 3998

select_alias:
unknown's avatar
unknown committed
3999 4000 4001 4002 4003 4004
	/* empty */		{ $$.str=0;}
	| AS ident		{ $$=$2; }
	| AS TEXT_STRING_sys	{ $$=$2; }
	| ident			{ $$=$1; }
	| TEXT_STRING_sys	{ $$=$1; }
	;
unknown's avatar
unknown committed
4005 4006 4007

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

unknown's avatar
unknown committed
4010
/* all possible expressions */
unknown's avatar
SCRUM  
unknown committed
4011
expr:	
4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035
	expr or bool_term	{ $$= new Item_cond_or($1,$3); }
	| expr XOR bool_term	{ $$= new Item_cond_xor($1,$3); }
	| bool_term ;

bool_term:
	bool_term and bool_factor { $$= new Item_cond_and($1,$3); }
	| bool_factor ;

bool_factor:
	NOT_SYM bool_factor	{ $$= negate_expression(YYTHD, $2); }
	| bool_test ;

bool_test:
	bool_pri IS TRUE_SYM	{ $$= is_truth_value($1,1,0); }
	| bool_pri IS not TRUE_SYM { $$= is_truth_value($1,0,0); }
	| bool_pri IS FALSE_SYM	{ $$= is_truth_value($1,0,1); }
	| bool_pri IS not FALSE_SYM { $$= is_truth_value($1,1,1); }
	| bool_pri IS UNKNOWN_SYM { $$= new Item_func_isnull($1); }
	| bool_pri IS not UNKNOWN_SYM { $$= new Item_func_isnotnull($1); }
	| bool_pri ;

bool_pri:
	bool_pri IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| bool_pri IS not NULL_SYM { $$= new Item_func_isnotnull($1); }
4036 4037 4038 4039 4040
	| bool_pri EQUAL_SYM predicate	{ $$= new Item_func_equal($1,$3); }
	| bool_pri comp_op predicate %prec EQ
	  { $$= (*$2)(0)->create($1,$3); }
	| bool_pri comp_op all_or_any in_subselect %prec EQ
	  { $$= all_any_subquery_creator($1, $2, $3, $4); }
4041 4042 4043
	| predicate ;

predicate:
4044
	bit_expr IN_SYM '(' expr_list ')'
4045 4046
	  { $4->push_front($1); $$= new Item_func_in(*$4); }
	| bit_expr not IN_SYM '(' expr_list ')'
4047
	  { $5->push_front($1); $$= negate_expression(YYTHD, new Item_func_in(*$5)); }
4048 4049 4050
        | bit_expr IN_SYM in_subselect
	  { $$= new Item_in_subselect($1, $3); }
	| bit_expr not IN_SYM in_subselect
4051
          { $$= negate_expression(YYTHD, new Item_in_subselect($1, $4)); }
4052 4053 4054 4055
	| bit_expr BETWEEN_SYM bit_expr AND_SYM predicate
	  { $$= new Item_func_between($1,$3,$5); }
	| bit_expr not BETWEEN_SYM bit_expr AND_SYM predicate
	  { $$= negate_expression(YYTHD, new Item_func_between($1,$4,$6)); }
4056 4057 4058 4059 4060 4061 4062 4063 4064
	| bit_expr SOUNDS_SYM LIKE bit_expr
	  { $$= new Item_func_eq(new Item_func_soundex($1),
				 new Item_func_soundex($4)); }
	| bit_expr LIKE simple_expr opt_escape
          { $$= new Item_func_like($1,$3,$4); }
	| bit_expr not LIKE simple_expr opt_escape
          { $$= new Item_func_not(new Item_func_like($1,$4,$5)); }
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
4065
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107
	| bit_expr ;

bit_expr:
	bit_expr '|' bit_term	{ $$= new Item_func_bit_or($1,$3); }
	| bit_term ;

bit_term:
	bit_term '&' bit_factor	{ $$= new Item_func_bit_and($1,$3); }
	| bit_factor ;

bit_factor:
	bit_factor SHIFT_LEFT value_expr
	  { $$= new Item_func_shift_left($1,$3); }
	| bit_factor SHIFT_RIGHT value_expr 
	  { $$= new Item_func_shift_right($1,$3); }
	| value_expr ;

value_expr:
	value_expr '+' term	{ $$= new Item_func_plus($1,$3); }
	| value_expr '-' term	{ $$= new Item_func_minus($1,$3); }
	| value_expr '+' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,0); }
	| value_expr '-' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,1); }
	| term ;

term:
	term '*' factor		{ $$= new Item_func_mul($1,$3); }
	| term '/' factor	{ $$= new Item_func_div($1,$3); }
	| term '%' factor	{ $$= new Item_func_mod($1,$3); }
	| term DIV_SYM factor	{ $$= new Item_func_int_div($1,$3); }
	| term MOD_SYM factor	{ $$= new Item_func_mod($1,$3); }
	| factor ;

factor:
        factor '^' simple_expr	{ $$= new Item_func_bit_xor($1,$3); }
	| simple_expr ;

or:	OR_SYM | OR2_SYM;
and:	AND_SYM | AND_AND_SYM;
not:	NOT_SYM | NOT2_SYM;
not2:	'!' | NOT2_SYM;
unknown's avatar
unknown committed
4108

unknown's avatar
unknown committed
4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120
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; }
        ;

4121
interval_expr:
unknown's avatar
unknown committed
4122
         INTERVAL_SYM expr { $$=$2; }
4123 4124
        ;

unknown's avatar
unknown committed
4125 4126
simple_expr:
	simple_ident
4127
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
unknown's avatar
unknown committed
4128
	  {
unknown's avatar
unknown committed
4129 4130 4131
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
4132
                                                            YYTHD->charset()));
4133
	  }
unknown's avatar
unknown committed
4134
	| literal
unknown's avatar
unknown committed
4135
	| param_marker
unknown's avatar
unknown committed
4136
	| '@' ident_or_text SET_VAR expr
unknown's avatar
unknown committed
4137 4138
	  {
	    $$= new Item_func_set_user_var($2,$4);
4139 4140 4141
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4142
	  }
4143
	| '@' ident_or_text
unknown's avatar
unknown committed
4144 4145
	  {
	    $$= new Item_func_get_user_var($2);
4146 4147 4148
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4149
	  }
4150
	| '@' '@' opt_var_ident_type ident_or_text opt_component
unknown's avatar
unknown committed
4151
	  {
4152 4153 4154 4155 4156 4157

            if ($4.str && $5.str && check_reserved_words(&$4))
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
4158
	    if (!($$= get_system_var(YYTHD, (enum_var_type) $3, $4, $5)))
unknown's avatar
unknown committed
4159
	      YYABORT;
4160
	    Lex->variables_used= 1;
unknown's avatar
unknown committed
4161
	  }
unknown's avatar
unknown committed
4162
	| sum_expr
4163 4164 4165 4166 4167 4168
	| simple_expr OR_OR_SYM simple_expr
	  { $$= new Item_func_concat($1, $3); }
	| '+' simple_expr %prec NEG	{ $$= $2; }
	| '-' simple_expr %prec NEG	{ $$= new Item_func_neg($2); }
	| '~' simple_expr %prec NEG	{ $$= new Item_func_bit_neg($2); }
	| not2 simple_expr %prec NEG	{ $$= negate_expression(YYTHD, $2); }
unknown's avatar
unknown committed
4169
	| '(' expr ')'		{ $$= $2; }
4170 4171 4172 4173 4174
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
4175
	| ROW_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
4176
	  {
4177 4178
	    $5->push_front($3);
	    $$= new Item_row(*$5);
unknown's avatar
unknown committed
4179
	  }
unknown's avatar
unknown committed
4180
	| EXISTS exists_subselect { $$= $2; }
unknown's avatar
unknown committed
4181
	| singlerow_subselect   { $$= $1; }
unknown's avatar
unknown committed
4182
	| '{' ident expr '}'	{ $$= $3; }
4183
        | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
4184
          { $2->push_front($5);
4185 4186
            Select->add_ftfunc_to_list((Item_func_match*)
                                        ($$=new Item_func_match(*$2,$6))); }
unknown's avatar
unknown committed
4187
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
4188
	| BINARY simple_expr %prec NEG
unknown's avatar
unknown committed
4189
	  {
4190
	    $$= create_func_cast($2, ITEM_CAST_CHAR, -1, &my_charset_bin);
unknown's avatar
unknown committed
4191
	  }
4192
	| CAST_SYM '(' expr AS cast_type ')'
4193 4194
	  {
	    $$= create_func_cast($3, $5,
4195
				 Lex->length ? atoi(Lex->length) : -1,
4196
				 Lex->charset);
4197
	  }
unknown's avatar
unknown committed
4198
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
4199
	  { $$= new Item_func_case(* $4, $2, $5 ); }
4200
	| CONVERT_SYM '(' expr ',' cast_type ')'
4201 4202 4203 4204 4205
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
				 Lex->charset);
	  }
4206 4207
	| CONVERT_SYM '(' expr USING charset_name ')'
	  { $$= new Item_func_conv_charset($3,$5); }
unknown's avatar
SCRUM  
unknown committed
4208 4209
	| DEFAULT '(' simple_ident ')'
	  { $$= new Item_default_value($3); }
unknown's avatar
unknown committed
4210 4211
	| VALUES '(' simple_ident ')'
	  { $$= new Item_insert_value($3); }
unknown's avatar
unknown committed
4212
	| FUNC_ARG0 '(' ')'
unknown's avatar
unknown committed
4213 4214 4215
	  {
	    if (!$1.symbol->create_func)
	    {
4216 4217 4218
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
4219 4220 4221 4222
	      YYABORT;
	    }
	    $$= ((Item*(*)(void))($1.symbol->create_func))();
	  }
unknown's avatar
unknown committed
4223
	| FUNC_ARG1 '(' expr ')'
unknown's avatar
unknown committed
4224 4225 4226
	  {
	    if (!$1.symbol->create_func)
	    {
4227 4228 4229
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
4230 4231 4232 4233
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);
	  }
unknown's avatar
unknown committed
4234
	| FUNC_ARG2 '(' expr ',' expr ')'
unknown's avatar
unknown committed
4235 4236 4237
	  {
	    if (!$1.symbol->create_func)
	    {
4238 4239 4240
	      my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
4241 4242 4243 4244
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);
	  }
unknown's avatar
unknown committed
4245
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
unknown's avatar
unknown committed
4246 4247 4248
	  {
	    if (!$1.symbol->create_func)
	    {
4249 4250 4251
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
4252 4253 4254 4255
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*,Item*))($1.symbol->create_func))($3,$5,$7);
	  }
unknown's avatar
unknown committed
4256 4257 4258 4259
	| ADDDATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_date_add_interval($3, $5, INTERVAL_DAY, 0);}
	| ADDDATE_SYM '(' expr ',' INTERVAL_SYM expr interval ')'
	  { $$= new Item_date_add_interval($3, $6, $7, 0); }
4260 4261
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
unknown's avatar
unknown committed
4262 4263 4264 4265 4266 4267
	| 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
4268 4269
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
unknown's avatar
unknown committed
4270 4271
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
unknown's avatar
unknown committed
4272 4273
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
unknown's avatar
unknown committed
4274 4275 4276
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
4277
	  { $5->push_front($3); $$= new Item_func_concat_ws(*$5); }
4278 4279
	| CONTAINS_SYM '(' expr ',' expr ')'
	  { $$= create_func_contains($3, $5); }
4280 4281
	| CONVERT_TZ_SYM '(' expr ',' expr ',' expr ')'
	  {
4282 4283
            if (Lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
4284 4285
	    $$= new Item_func_convert_tz($3, $5, $7);
	  }
unknown's avatar
unknown committed
4286
	| CURDATE optional_braces
4287
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
4288
	| CURTIME optional_braces
4289
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
4290
	| CURTIME '(' expr ')'
4291
	  {
4292
	    $$= new Item_func_curtime_local($3);
4293
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4294
	  }
unknown's avatar
unknown committed
4295
	| CURRENT_USER optional_braces
4296
          { $$= create_func_current_user(); }
4297 4298 4299 4300
	| DATE_ADD_INTERVAL '(' expr ',' interval_expr interval ')'
	  { $$= new Item_date_add_interval($3,$5,$6,0); }
	| DATE_SUB_INTERVAL '(' expr ',' interval_expr interval ')'
	  { $$= new Item_date_add_interval($3,$5,$6,1); }
unknown's avatar
unknown committed
4301
	| DATABASE '(' ')'
4302
	  {
unknown's avatar
unknown committed
4303
	    $$= new Item_func_database();
4304
            Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4305
	  }
unknown's avatar
unknown committed
4306
	| DATE_SYM '(' expr ')'
unknown's avatar
unknown committed
4307
	  { $$= new Item_date_typecast($3); }
unknown's avatar
unknown committed
4308 4309
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
unknown's avatar
unknown committed
4310
	| ELT_FUNC '(' expr ',' expr_list ')'
4311
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
unknown's avatar
unknown committed
4312 4313
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
unknown's avatar
unknown committed
4314 4315 4316
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
4317
	    Lex->uncacheable(UNCACHEABLE_RAND);
unknown's avatar
unknown committed
4318
	  }
unknown's avatar
unknown committed
4319
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
4320
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
4321
	  { $$= new Item_func_decode($3,$5.str); }
4322
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
4323
	 { $$= new Item_func_encode($3,$5.str); }
unknown's avatar
unknown committed
4324
	| DES_DECRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
4325
        { $$= new Item_func_des_decrypt($3); }
unknown's avatar
unknown committed
4326
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
4327
        { $$= new Item_func_des_decrypt($3,$5); }
unknown's avatar
unknown committed
4328
	| DES_ENCRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
4329
        { $$= new Item_func_des_encrypt($3); }
unknown's avatar
unknown committed
4330
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
4331
        { $$= new Item_func_des_encrypt($3,$5); }
unknown's avatar
unknown committed
4332 4333 4334 4335 4336 4337
	| 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); }
4338 4339
	| FALSE_SYM
	  { $$= new Item_int((char*) "FALSE",0,1); }
unknown's avatar
unknown committed
4340 4341 4342 4343 4344 4345
	| 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
4346
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
unknown's avatar
unknown committed
4347 4348
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
4349
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
unknown's avatar
unknown committed
4350 4351 4352 4353 4354
	| geometry_function
	  {
#ifdef HAVE_SPATIAL
	    $$= $1;
#else
4355 4356
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
4357 4358 4359
	    YYABORT;
#endif
	  }
4360
	| GET_FORMAT '(' date_time_type  ',' expr ')'
4361
	  { $$= new Item_func_get_format($3, $5); }
unknown's avatar
unknown committed
4362 4363 4364 4365 4366 4367
	| 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); }
4368
	| interval_expr interval '+' expr
unknown's avatar
unknown committed
4369
	  /* we cannot put interval before - */
4370 4371 4372 4373 4374
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
4375
              yyerror(ER(ER_SYNTAX_ERROR));
4376 4377 4378 4379
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
unknown's avatar
unknown committed
4380 4381
	| LAST_INSERT_ID '(' ')'
	  {
4382
	    $$= new Item_func_last_insert_id();
4383
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
4384 4385 4386
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
4387
	    $$= new Item_func_last_insert_id($3);
4388
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
4389 4390 4391 4392 4393 4394 4395
	  }
	| LEFT '(' expr ',' expr ')'
	  { $$= new Item_func_left($3,$5); }
	| 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
4396
	| GREATEST_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
4397 4398 4399
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
4400
	| LOG_SYM '(' expr ')'
unknown's avatar
unknown committed
4401
	  { $$= new Item_func_log($3); }
4402
	| LOG_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
4403
	  { $$= new Item_func_log($3, $5); }
4404
	| MASTER_POS_WAIT '(' expr ',' expr ')'
unknown's avatar
unknown committed
4405
	  {
4406
	    $$= new Item_master_pos_wait($3, $5);
unknown's avatar
unknown committed
4407
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4408
		  }
4409
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
unknown's avatar
unknown committed
4410
	  {
4411
	    $$= new Item_master_pos_wait($3, $5, $7);
unknown's avatar
unknown committed
4412
	    Lex->safe_to_cache_query=0;
4413
	  }
unknown's avatar
unknown committed
4414 4415
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
unknown's avatar
unknown committed
4416 4417
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
4418 4419
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
unknown's avatar
unknown committed
4420 4421 4422
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
	| NOW_SYM optional_braces
4423
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
4424
	| NOW_SYM '(' expr ')'
4425
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
4426
	| PASSWORD '(' expr ')'
4427
	  {
4428 4429
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
unknown's avatar
unknown committed
4430 4431 4432 4433
	      (Item *) new Item_func_password($3);
	  }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new Item_func_old_password($3); }
4434
	| POSITION_SYM '(' bit_expr IN_SYM expr ')'
unknown's avatar
unknown committed
4435
	  { $$ = new Item_func_locate($5,$3); }
4436 4437
	| QUARTER_SYM '(' expr ')'
	  { $$ = new Item_func_quarter($3); }
unknown's avatar
unknown committed
4438
	| RAND '(' expr ')'
4439
	  { $$= new Item_func_rand($3); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
4440
	| RAND '(' ')'
4441
	  { $$= new Item_func_rand(); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
4442 4443 4444 4445 4446 4447 4448
	| 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); }
4449 4450 4451 4452 4453
	| ROW_COUNT_SYM '(' ')'
	  {
	    $$= new Item_func_row_count();
	    Lex->safe_to_cache_query= 0;
	  }
unknown's avatar
unknown committed
4454 4455 4456 4457
	| SUBDATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_date_add_interval($3, $5, INTERVAL_DAY, 1);}
	| SUBDATE_SYM '(' expr ',' INTERVAL_SYM expr interval ')'
	  { $$= new Item_date_add_interval($3, $6, $7, 1); }
unknown's avatar
unknown committed
4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469
	| 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); }
unknown's avatar
unknown committed
4470
	| TIME_SYM '(' expr ')'
unknown's avatar
unknown committed
4471 4472 4473
	  { $$= new Item_time_typecast($3); }
	| TIMESTAMP '(' expr ')'
	  { $$= new Item_datetime_typecast($3); }
unknown's avatar
unknown committed
4474
	| TIMESTAMP '(' expr ',' expr ')'
unknown's avatar
unknown committed
4475
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
4476 4477 4478 4479
	| TIMESTAMP_ADD '(' interval_time_st ',' expr ',' expr ')'
	  { $$= new Item_date_add_interval($7,$5,$3,0); }
	| TIMESTAMP_DIFF '(' interval_time_st ',' expr ',' expr ')'
	  { $$= new Item_func_timestamp_diff($5,$7,$3); }
unknown's avatar
unknown committed
4480
	| TRIM '(' expr ')'
4481 4482
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
unknown's avatar
unknown committed
4483
	  { $$= new Item_func_ltrim($6,$4); }
4484
	| TRIM '(' TRAILING expr FROM expr ')'
unknown's avatar
unknown committed
4485
	  { $$= new Item_func_rtrim($6,$4); }
4486
	| TRIM '(' BOTH expr FROM expr ')'
unknown's avatar
unknown committed
4487
	  { $$= new Item_func_trim($6,$4); }
4488 4489 4490 4491 4492 4493
	| TRIM '(' LEADING FROM expr ')'
	 { $$= new Item_func_ltrim($5); }
	| TRIM '(' TRAILING FROM expr ')'
	  { $$= new Item_func_rtrim($5); }
	| TRIM '(' BOTH FROM expr ')'
	  { $$= new Item_func_trim($5); }
unknown's avatar
unknown committed
4494 4495
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
4496 4497
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
4498 4499 4500
	| TRUE_SYM
	  { $$= new Item_int((char*) "TRUE",1,1); }
	| ident '.' ident '(' udf_expr_list ')'
unknown's avatar
unknown committed
4501
	  {
4502 4503 4504 4505 4506 4507 4508
	    LEX *lex= Lex;
	    sp_name *name= new sp_name($1, $3);

	    name->init_qname(YYTHD);
	    sp_add_fun_to_lex(Lex, name);
	    if ($5)
	      $$= new Item_func_sp(name, *$5);
unknown's avatar
unknown committed
4509
	    else
4510
	      $$= new Item_func_sp(name);
unknown's avatar
unknown committed
4511
	  }
4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583
	| IDENT_sys '(' udf_expr_list ')'
          {
#ifdef HAVE_DLOPEN
            udf_func *udf;

            if (using_udf_functions && (udf=find_udf($1.str, $1.length)))
            {
              switch (udf->returns) {
              case STRING_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_str(udf, *$3);
                  else
                    $$ = new Item_func_udf_str(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_str(udf, *$3);
                  else
                    $$ = new Item_sum_udf_str(udf);
                }
                break;
              case REAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_float(udf, *$3);
                  else
                    $$ = new Item_func_udf_float(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_float(udf, *$3);
                  else
                    $$ = new Item_sum_udf_float(udf);
                }
                break;
              case INT_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_int(udf, *$3);
                  else
                    $$ = new Item_func_udf_int(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_int(udf, *$3);
                  else
                    $$ = new Item_sum_udf_int(udf);
                }
                break;
              default:
                YYABORT;
              }
            }
            else
#endif /* HAVE_DLOPEN */
            {
              sp_name *name= sp_name_current_db_new(YYTHD, $1);

              sp_add_fun_to_lex(Lex, name);
              if ($3)
                $$= new Item_func_sp(name, *$3);
              else
                $$= new Item_func_sp(name);
	    }
          }
unknown's avatar
unknown committed
4584
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
4585
	  {
unknown's avatar
unknown committed
4586 4587
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
unknown's avatar
unknown committed
4588
	| UNIX_TIMESTAMP '(' ')'
unknown's avatar
unknown committed
4589 4590
	  {
	    $$= new Item_func_unix_timestamp();
4591
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4592
	  }
unknown's avatar
unknown committed
4593 4594 4595
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
4596
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
4597 4598 4599 4600 4601 4602
	| UTC_DATE_SYM optional_braces
	  { $$= new Item_func_curdate_utc(); Lex->safe_to_cache_query=0;}
	| UTC_TIME_SYM optional_braces
	  { $$= new Item_func_curtime_utc(); Lex->safe_to_cache_query=0;}
	| UTC_TIMESTAMP_SYM optional_braces
	  { $$= new Item_func_now_utc(); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
4603
	| WEEK_SYM '(' expr ')'
unknown's avatar
unknown committed
4604
	  {
4605
            $$= new Item_func_week($3,new Item_int((char*) "0",
unknown's avatar
unknown committed
4606
				   YYTHD->variables.default_week_format,1));
4607
          }
unknown's avatar
unknown committed
4608 4609 4610 4611 4612 4613 4614 4615 4616
	| 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 ')'
4617
	  {
unknown's avatar
unknown committed
4618
	    $$=new Item_func_benchmark($3,$5);
4619
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
4620
	  }
unknown's avatar
unknown committed
4621
	| EXTRACT_SYM '(' interval FROM expr ')'
unknown's avatar
unknown committed
4622
	{ $$=new Item_extract( $3, $5); };
unknown's avatar
unknown committed
4623

unknown's avatar
unknown committed
4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634
geometry_function:
	GEOMFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| GEOMFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| GEOMFROMWKB '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_wkb($3)); }
	| GEOMFROMWKB '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_wkb($3, $5)); }
	| GEOMETRYCOLLECTION '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4635 4636
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
unknown's avatar
unknown committed
4637 4638
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4639
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
unknown's avatar
unknown committed
4640 4641
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
4642
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656
 	| MLINEFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| MLINEFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| MPOINTFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| MPOINTFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| MPOLYFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| MPOLYFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| MULTIPOINT '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4657
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
unknown's avatar
unknown committed
4658 4659
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4660
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
unknown's avatar
unknown committed
4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672
	| POINT_SYM '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_point($3,$5)); }
 	| POINTFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| POINTFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| POLYFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| POLYFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| POLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4673
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
4674 4675 4676 4677 4678 4679 4680 4681 4682 4683
 	| GEOMCOLLFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| GEOMCOLLFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
 	| LINEFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| LINEFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	;

4684 4685 4686 4687 4688 4689
fulltext_options:
        /* nothing */                   { $$= FT_NL;  }
        | WITH QUERY_SYM EXPANSION_SYM  { $$= FT_NL | FT_EXPAND; }
        | IN_SYM BOOLEAN_SYM MODE_SYM   { $$= FT_BOOL; }
        ;

unknown's avatar
unknown committed
4690
udf_expr_list:
4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721
	/* empty */	 { $$= NULL; }
	| udf_expr_list2 { $$= $1;}
	;

udf_expr_list2:
	{ Select->expr_list.push_front(new List<Item>); }
	udf_expr_list3
	{ $$= Select->expr_list.pop(); }
	;

udf_expr_list3:
	udf_expr 
	  {
	    Select->expr_list.head()->push_back($1);
	  }
	| udf_expr_list3 ',' udf_expr 
	  {
	    Select->expr_list.head()->push_back($3);
	  }
	;

udf_expr:
	remember_name expr remember_end select_alias
	{
	  if ($4.str)
	    $2->set_name($4.str,$4.length,system_charset_info);
	  else
	    $2->set_name($1,(uint) ($3 - $1), YYTHD->charset());
	  $$= $2;
	}
	;
unknown's avatar
unknown committed
4722 4723 4724 4725 4726 4727 4728 4729

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); }
4730 4731
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
unknown's avatar
unknown committed
4732
	| COUNT_SYM '(' opt_all '*' ')'
unknown's avatar
unknown committed
4733 4734 4735
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
4736
	| COUNT_SYM '(' DISTINCT
unknown's avatar
unknown committed
4737
	  { Select->in_sum_expr++; }
4738
	   expr_list
unknown's avatar
unknown committed
4739
	  { Select->in_sum_expr--; }
4740 4741
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
unknown's avatar
unknown committed
4742 4743 4744 4745
	| 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); }
4746 4747 4748 4749 4750 4751 4752
/*
   According to ANSI SQL, DISTINCT is allowed and has
   no sence inside MIN and MAX grouping functions; so MIN|MAX(DISTINCT ...)
   is processed like an ordinary MIN | MAX()
 */
	| MIN_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_min($4); }
unknown's avatar
unknown committed
4753 4754
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
4755 4756
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
unknown's avatar
unknown committed
4757 4758
	| STD_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_std($3); }
unknown's avatar
unknown committed
4759 4760
	| VARIANCE_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_variance($3); }
unknown's avatar
unknown committed
4761
	| SUM_SYM '(' in_sum_expr ')'
4762
	  { $$=new Item_sum_sum($3); }
4763 4764
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
unknown's avatar
unknown committed
4765 4766 4767 4768 4769
	| GROUP_CONCAT_SYM '(' opt_distinct
	  { Select->in_sum_expr++; }
	  expr_list opt_gorder_clause
	  opt_gconcat_separator
	 ')'
unknown's avatar
unknown committed
4770
	  {
unknown's avatar
unknown committed
4771 4772 4773
	    Select->in_sum_expr--;
	    $$=new Item_func_group_concat($3,$5,Select->gorder_list,$7);
	    $5->empty();
4774 4775 4776 4777 4778 4779 4780
	  };

opt_distinct:
    /* empty */ { $$ = 0; }
    |DISTINCT   { $$ = 1; };

opt_gconcat_separator:
unknown's avatar
unknown committed
4781
    /* empty */        { $$ = new (YYTHD->mem_root) String(",",1,default_charset_info); }
4782
    |SEPARATOR_SYM text_string  { $$ = $2; };
unknown's avatar
unknown committed
4783

4784 4785

opt_gorder_clause:
4786 4787
	  /* empty */
	  {
unknown's avatar
unknown committed
4788
            Select->gorder_list = NULL;
4789 4790 4791
	  }
	| order_clause
          {
unknown's avatar
unknown committed
4792 4793 4794
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
4795
				     sizeof(st_sql_list));
unknown's avatar
unknown committed
4796
	    select->order_list.empty();
4797
	  };
unknown's avatar
unknown committed
4798

unknown's avatar
unknown committed
4799 4800

in_sum_expr:
unknown's avatar
unknown committed
4801
	opt_all
4802 4803 4804 4805
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
4806
	    yyerror(ER(ER_SYNTAX_ERROR));
4807
	    YYABORT;
4808 4809
	  }
	}
unknown's avatar
unknown committed
4810 4811
	expr
	{
unknown's avatar
unknown committed
4812
	  Select->in_sum_expr--;
4813
	  $$= $3;
unknown's avatar
unknown committed
4814
	};
unknown's avatar
unknown committed
4815

4816
cast_type:
4817
	BINARY opt_len		{ $$=ITEM_CAST_CHAR; Lex->charset= &my_charset_bin; }
4818 4819
	| CHAR_SYM opt_len opt_binary	{ $$=ITEM_CAST_CHAR; }
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; }
unknown's avatar
unknown committed
4820 4821 4822 4823 4824 4825 4826
	| SIGNED_SYM		{ $$=ITEM_CAST_SIGNED_INT; Lex->charset= NULL; Lex->length= (char*)0; }
	| SIGNED_SYM INT_SYM	{ $$=ITEM_CAST_SIGNED_INT; Lex->charset= NULL; Lex->length= (char*)0; }
	| UNSIGNED		{ $$=ITEM_CAST_UNSIGNED_INT; Lex->charset= NULL; Lex->length= (char*)0; }
	| UNSIGNED INT_SYM	{ $$=ITEM_CAST_UNSIGNED_INT; Lex->charset= NULL; Lex->length= (char*)0; }
	| DATE_SYM		{ $$=ITEM_CAST_DATE; Lex->charset= NULL; Lex->length= (char*)0; }
	| TIME_SYM		{ $$=ITEM_CAST_TIME; Lex->charset= NULL; Lex->length= (char*)0; }
	| DATETIME		{ $$=ITEM_CAST_DATETIME; Lex->charset= NULL; Lex->length= (char*)0; }
4827 4828
	;

unknown's avatar
unknown committed
4829
expr_list:
4830
	{ Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
4831
	expr_list2
unknown's avatar
unknown committed
4832
	{ $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
4833 4834

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

unknown's avatar
unknown committed
4838 4839
ident_list_arg:
          ident_list          { $$= $1; }
unknown's avatar
unknown committed
4840
        | '(' ident_list ')'  { $$= $2; };
unknown's avatar
unknown committed
4841

unknown's avatar
unknown committed
4842
ident_list:
4843
        { Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
4844
        ident_list2
unknown's avatar
unknown committed
4845
        { $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
4846 4847

ident_list2:
4848
        simple_ident { Select->expr_list.head()->push_back($1); }
unknown's avatar
unknown committed
4849
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
unknown's avatar
unknown committed
4850 4851 4852

opt_expr:
	/* empty */      { $$= NULL; }
4853
	| expr           { $$= $1; };
unknown's avatar
unknown committed
4854 4855 4856

opt_else:
	/* empty */    { $$= NULL; }
unknown's avatar
unknown committed
4857
	| ELSE expr    { $$= $2; };
unknown's avatar
unknown committed
4858 4859

when_list:
unknown's avatar
unknown committed
4860
        { Select->when_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
4861
	when_list2
unknown's avatar
unknown committed
4862
	{ $$= Select->when_list.pop(); };
unknown's avatar
unknown committed
4863 4864

when_list2:
unknown's avatar
unknown committed
4865
	expr THEN_SYM expr
unknown's avatar
unknown committed
4866
	  {
unknown's avatar
unknown committed
4867
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
4868 4869
	    sel->when_list.head()->push_back($1);
	    sel->when_list.head()->push_back($3);
unknown's avatar
unknown committed
4870
	}
unknown's avatar
unknown committed
4871
	| when_list2 WHEN_SYM expr THEN_SYM expr
unknown's avatar
unknown committed
4872
	  {
unknown's avatar
unknown committed
4873
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
4874 4875
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
unknown's avatar
unknown committed
4876
	  };
unknown's avatar
unknown committed
4877

4878 4879 4880
table_ref:
        table_factor            { $$=$1; }
        | join_table            { $$=$1; }
4881
          {
4882 4883 4884
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
4885
          }
4886 4887
        ;

unknown's avatar
unknown committed
4888
join_table_list:
4889 4890 4891 4892 4893 4894 4895 4896 4897
        table_ref { $$=$1; }
        | join_table_list ',' table_ref { $$=$3; }
        ;

join_table:
        table_ref normal_join table_ref { $$=$3; }
	| table_ref STRAIGHT_JOIN table_factor
	  { $3->straight=1; $$=$3 ; }
	| table_ref normal_join table_ref ON expr
unknown's avatar
unknown committed
4898
	  { add_join_on($3,$5); $$=$3; }
4899
	| table_ref normal_join table_ref
unknown's avatar
unknown committed
4900
	  USING
unknown's avatar
unknown committed
4901
	  {
unknown's avatar
unknown committed
4902
	    SELECT_LEX *sel= Select;
4903
            sel->save_names_for_using_list($1, $3);
unknown's avatar
unknown committed
4904
	  }
unknown's avatar
unknown committed
4905 4906 4907
	  '(' using_list ')'
	  { add_join_on($3,$7); $$=$3; }

4908
	| table_ref LEFT opt_outer JOIN_SYM table_ref ON expr
4909
	  { add_join_on($5,$7); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
4910
	| table_ref LEFT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
4911
	  {
unknown's avatar
unknown committed
4912
	    SELECT_LEX *sel= Select;
4913
            sel->save_names_for_using_list($1, $5);
unknown's avatar
unknown committed
4914
	  }
unknown's avatar
unknown committed
4915
	  USING '(' using_list ')'
4916
	  { add_join_on($5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
4917
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
4918
	  {
4919 4920
	    add_join_natural($1,$6);
	    $6->outer_join|=JOIN_TYPE_LEFT;
4921 4922
	    $$=$6;
	  }
4923 4924 4925 4926 4927 4928 4929 4930
	| table_ref RIGHT opt_outer JOIN_SYM table_ref ON expr
          { 
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
            add_join_on($$, $7);
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
4931
	  {
unknown's avatar
unknown committed
4932
	    SELECT_LEX *sel= Select;
4933
            sel->save_names_for_using_list($1, $5);
unknown's avatar
unknown committed
4934
	  }
4935
	  USING '(' using_list ')'
4936 4937 4938 4939 4940 4941 4942
          { 
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
            add_join_on($$, $9);
          }
	| table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor
4943
	  {
4944 4945 4946 4947
	    add_join_natural($6,$1);
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
4948
	  }
4949 4950 4951
	| table_ref NATURAL JOIN_SYM table_factor
	  { add_join_natural($1,$4); $$=$4; };
        
unknown's avatar
unknown committed
4952 4953

normal_join:
unknown's avatar
unknown committed
4954 4955 4956 4957
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
unknown's avatar
unknown committed
4958

4959
table_factor:
unknown's avatar
unknown committed
4960
	{
unknown's avatar
unknown committed
4961
	  SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
4962
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
unknown's avatar
unknown committed
4963
	  sel->table_join_options= 0;
unknown's avatar
unknown committed
4964
	}
unknown's avatar
unknown committed
4965
        table_ident opt_table_alias opt_key_definition
unknown's avatar
unknown committed
4966
	{
unknown's avatar
unknown committed
4967
	  LEX *lex= Lex;
unknown's avatar
unknown committed
4968
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
4969
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
unknown's avatar
unknown committed
4970
					   sel->get_table_join_options(),
unknown's avatar
unknown committed
4971
					   lex->lock_option,
4972 4973
					   sel->get_use_index(),
					   sel->get_ignore_index())))
4974
	    YYABORT;
4975
          sel->add_joined_table($$);
unknown's avatar
unknown committed
4976
	}
4977
        | '('
4978
          {
4979 4980 4981
            LEX *lex= Lex;
            if (lex->current_select->init_nested_join(lex->thd))
              YYABORT;
4982
          }
4983 4984 4985 4986 4987
          join_table_list ')'
          {
            LEX *lex= Lex;
            if (!($$= lex->current_select->end_nested_join(lex->thd)))
              YYABORT;
4988
          }
4989
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref ON expr '}'
4990
	  { add_join_on($7,$9); $7->outer_join|=JOIN_TYPE_LEFT; $$=$7; }
4991
        | '(' SELECT_SYM select_derived ')' opt_table_alias
unknown's avatar
unknown committed
4992 4993
	{
	  LEX *lex=Lex;
4994 4995 4996
	  SELECT_LEX_UNIT *unit= lex->current_select->master_unit();
	  lex->current_select= unit->outer_select();
	  if (!($$= lex->current_select->
unknown's avatar
unknown committed
4997
                add_table_to_list(lex->thd, new Table_ident(unit), $5, 0,
4998
				  TL_READ,(List<String> *)0,
unknown's avatar
unknown committed
4999 5000
	                          (List<String> *)0)))

unknown's avatar
unknown committed
5001
	    YYABORT;
5002
          lex->current_select->add_joined_table($$);           
unknown's avatar
unknown committed
5003
	};
unknown's avatar
unknown committed
5004

5005
select_derived:
unknown's avatar
unknown committed
5006
        {
unknown's avatar
unknown committed
5007
	  LEX *lex= Lex;
5008
	  lex->derived_tables|= DERIVED_SUBQUERY;
unknown's avatar
unknown committed
5009 5010 5011
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
5012 5013
	  {
	    yyerror(ER(ER_SYNTAX_ERROR));
5014 5015
	    YYABORT;
	  }
5016
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
unknown's avatar
unknown committed
5017
              mysql_new_select(lex, 1))
unknown's avatar
unknown committed
5018 5019
	    YYABORT;
	  mysql_init_select(lex);
5020
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
5021
	  lex->current_select->parsing_place= SELECT_LIST;
5022 5023 5024
	}
        select_options select_item_list
	{
5025
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
5026
	}
5027
	opt_select_from union_opt
5028
        ;
unknown's avatar
unknown committed
5029 5030 5031

opt_outer:
	/* empty */	{}
unknown's avatar
unknown committed
5032
	| OUTER		{};
unknown's avatar
unknown committed
5033 5034 5035 5036

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
unknown's avatar
unknown committed
5037
          {
unknown's avatar
unknown committed
5038
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5039 5040 5041
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
5042 5043
	| FORCE_SYM key_usage_list
          {
unknown's avatar
unknown committed
5044
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5045 5046 5047 5048
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
unknown's avatar
unknown committed
5049
	| IGNORE_SYM key_usage_list
unknown's avatar
unknown committed
5050
	  {
unknown's avatar
unknown committed
5051
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5052 5053
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
unknown's avatar
unknown committed
5054
	  };
unknown's avatar
unknown committed
5055 5056

key_usage_list:
unknown's avatar
unknown committed
5057
	key_or_index { Select->interval_list.empty(); }
unknown's avatar
unknown committed
5058
        '(' key_list_or_empty ')'
unknown's avatar
unknown committed
5059
        { $$= &Select->interval_list; }
unknown's avatar
unknown committed
5060 5061 5062 5063 5064 5065
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
unknown's avatar
unknown committed
5066 5067 5068

key_usage_list2:
	key_usage_list2 ',' ident
unknown's avatar
unknown committed
5069
        { Select->
unknown's avatar
unknown committed
5070
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $3.str, $3.length,
5071
				    system_charset_info)); }
unknown's avatar
unknown committed
5072
	| ident
unknown's avatar
unknown committed
5073
        { Select->
unknown's avatar
unknown committed
5074
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $1.str, $1.length,
5075
				    system_charset_info)); }
unknown's avatar
unknown committed
5076
	| PRIMARY_SYM
unknown's avatar
unknown committed
5077
        { Select->
unknown's avatar
unknown committed
5078
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
5079
				    system_charset_info)); };
unknown's avatar
unknown committed
5080 5081 5082

using_list:
	ident
unknown's avatar
unknown committed
5083
	  {
unknown's avatar
unknown committed
5084
	    SELECT_LEX *sel= Select;
5085 5086 5087 5088
	    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
5089 5090 5091 5092
	      YYABORT;
	  }
	| using_list ',' ident
	  {
unknown's avatar
unknown committed
5093
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5094
	    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
5095
	      YYABORT;
unknown's avatar
unknown committed
5096
	  };
unknown's avatar
unknown committed
5097 5098

interval:
5099 5100
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
unknown's avatar
unknown committed
5101
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
unknown's avatar
unknown committed
5102 5103
	| DAY_MINUTE_SYM	{ $$=INTERVAL_DAY_MINUTE; }
	| DAY_SECOND_SYM	{ $$=INTERVAL_DAY_SECOND; }
unknown's avatar
unknown committed
5104
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
unknown's avatar
unknown committed
5105 5106
	| HOUR_MINUTE_SYM	{ $$=INTERVAL_HOUR_MINUTE; }
	| HOUR_SECOND_SYM	{ $$=INTERVAL_HOUR_SECOND; }
unknown's avatar
unknown committed
5107 5108
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
unknown's avatar
unknown committed
5109
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
5110 5111 5112 5113 5114 5115 5116 5117
	| SECOND_MICROSECOND_SYM	{ $$=INTERVAL_SECOND_MICROSECOND; }
	| YEAR_MONTH_SYM	{ $$=INTERVAL_YEAR_MONTH; };

interval_time_st:
	DAY_SYM			{ $$=INTERVAL_DAY; }
	| WEEK_SYM		{ $$=INTERVAL_WEEK; }
	| HOUR_SYM		{ $$=INTERVAL_HOUR; }
	| FRAC_SECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
unknown's avatar
unknown committed
5118 5119
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
5120
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
unknown's avatar
unknown committed
5121
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
5122 5123
	| YEAR_SYM		{ $$=INTERVAL_YEAR; }
        ;
unknown's avatar
unknown committed
5124

5125
date_time_type:
5126 5127 5128 5129 5130
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
5131

unknown's avatar
unknown committed
5132 5133 5134
table_alias:
	/* empty */
	| AS
unknown's avatar
unknown committed
5135
	| EQ;
unknown's avatar
unknown committed
5136 5137 5138 5139

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

unknown's avatar
unknown committed
5142 5143 5144 5145
opt_all:
	/* empty */
	| ALL
	;
unknown's avatar
unknown committed
5146 5147

where_clause:
unknown's avatar
unknown committed
5148
	/* empty */  { Select->where= 0; }
unknown's avatar
unknown committed
5149 5150 5151 5152 5153
	| WHERE
          {
            Select->parsing_place= IN_WHERE;
          }
          expr
5154
	  {
unknown's avatar
unknown committed
5155 5156 5157 5158 5159
            SELECT_LEX *select= Select;
	    select->where= $3;
            select->parsing_place= NO_MATTER;
	    if ($3)
	      $3->top_level_item();
5160
	  }
unknown's avatar
unknown committed
5161
 	;
unknown's avatar
unknown committed
5162 5163 5164

having_clause:
	/* empty */
unknown's avatar
unknown committed
5165 5166
	| HAVING
	  {
5167
	    Select->parsing_place= IN_HAVING;
unknown's avatar
unknown committed
5168 5169
          }
	  expr
unknown's avatar
unknown committed
5170
	  {
unknown's avatar
unknown committed
5171
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5172
	    sel->having= $3;
5173
	    sel->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
5174 5175 5176
	    if ($3)
	      $3->top_level_item();
	  }
5177
	;
unknown's avatar
unknown committed
5178 5179

opt_escape:
5180 5181
	ESCAPE_SYM simple_expr { $$= $2; }
	| /* empty */
5182 5183 5184 5185 5186
          {

            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
5187 5188
          }
        ;
unknown's avatar
unknown committed
5189 5190 5191


/*
unknown's avatar
unknown committed
5192
   group by statement in select
unknown's avatar
unknown committed
5193 5194 5195 5196
*/

group_clause:
	/* empty */
5197
	| GROUP BY group_list olap_opt;
unknown's avatar
unknown committed
5198 5199

group_list:
unknown's avatar
unknown committed
5200
	group_list ',' order_ident order_dir
unknown's avatar
unknown committed
5201
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
5202
	| order_ident order_dir
unknown's avatar
unknown committed
5203
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
5204

5205 5206
olap_opt:
	/* empty */ {}
5207
	| WITH CUBE_SYM
5208
          {
5209
	    LEX *lex=Lex;
5210 5211
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
5212
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
5213 5214 5215
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
5216
	    lex->current_select->olap= CUBE_TYPE;
unknown's avatar
unknown committed
5217
	    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
5218
	    YYABORT;	/* To be deleted in 5.1 */
5219
	  }
5220
	| WITH ROLLUP_SYM
5221
          {
5222 5223 5224
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
5225
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
5226 5227 5228
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
5229
	    lex->current_select->olap= ROLLUP_TYPE;
5230
	  }
5231
	;
5232

unknown's avatar
unknown committed
5233
/*
unknown's avatar
unknown committed
5234
   Order by statement in select
unknown's avatar
unknown committed
5235 5236
*/

5237
opt_order_clause:
unknown's avatar
unknown committed
5238
	/* empty */
unknown's avatar
unknown committed
5239
	| order_clause;
5240 5241

order_clause:
5242 5243
	ORDER_SYM BY
        {
unknown's avatar
unknown committed
5244
	  LEX *lex=Lex;
5245
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
unknown's avatar
unknown committed
5246
	      lex->current_select->olap !=
5247
	      UNSPECIFIED_OLAP_TYPE)
5248
	  {
unknown's avatar
unknown committed
5249 5250
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
5251 5252
	    YYABORT;
	  }
unknown's avatar
unknown committed
5253
	} order_list;
unknown's avatar
unknown committed
5254 5255 5256

order_list:
	order_list ',' order_ident order_dir
unknown's avatar
unknown committed
5257
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
5258
	| order_ident order_dir
unknown's avatar
unknown committed
5259
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
5260 5261 5262

order_dir:
	/* empty */ { $$ =  1; }
unknown's avatar
unknown committed
5263
	| ASC  { $$ =1; }
unknown's avatar
unknown committed
5264
	| DESC { $$ =0; };
unknown's avatar
unknown committed
5265 5266


5267 5268 5269
opt_limit_clause_init:
	/* empty */
	{
5270 5271
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
5272
          sel->offset_limit= 0L;
5273
          sel->select_limit= HA_POS_ERROR;
5274
	}
5275 5276 5277
	| limit_clause {}
	;

5278 5279 5280 5281 5282
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

5283
limit_clause:
5284
	LIMIT limit_options {}
unknown's avatar
unknown committed
5285
	;
unknown's avatar
unknown committed
5286 5287 5288

limit_options:
	ULONG_NUM
unknown's avatar
unknown committed
5289
	  {
unknown's avatar
unknown committed
5290
            SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5291 5292
            sel->select_limit= $1;
            sel->offset_limit= 0L;
5293
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5294 5295 5296
	  }
	| ULONG_NUM ',' ULONG_NUM
	  {
unknown's avatar
unknown committed
5297
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5298 5299
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
5300
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5301
	  }
unknown's avatar
unknown committed
5302
	| ULONG_NUM OFFSET_SYM ULONG_NUM
unknown's avatar
unknown committed
5303
	  {
unknown's avatar
unknown committed
5304
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5305 5306
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
5307
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5308 5309
	  }
	;
unknown's avatar
unknown committed
5310 5311 5312 5313 5314


delete_limit_clause:
	/* empty */
	{
unknown's avatar
unknown committed
5315
	  LEX *lex=Lex;
5316
	  lex->current_select->select_limit= HA_POS_ERROR;
unknown's avatar
unknown committed
5317
	}
unknown's avatar
unknown committed
5318
	| LIMIT ulonglong_num
5319 5320 5321 5322 5323
	{
	  SELECT_LEX *sel= Select;
	  sel->select_limit= (ha_rows) $2;
	  sel->explicit_limit= 1;
	};
unknown's avatar
unknown committed
5324 5325

ULONG_NUM:
unknown's avatar
unknown committed
5326 5327 5328 5329 5330 5331
	NUM	        { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| LONG_NUM      { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| ULONGLONG_NUM { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| REAL_NUM 	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
5332

unknown's avatar
unknown committed
5333
ulonglong_num:
unknown's avatar
unknown committed
5334 5335 5336 5337 5338 5339
	NUM	    { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| ULONGLONG_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| LONG_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| REAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
5340 5341 5342 5343 5344 5345

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
5346 5347
	    if (&lex->select_lex != lex->current_select)
	    {
unknown's avatar
unknown committed
5348
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
5349 5350
	      YYABORT;
	    }
unknown's avatar
unknown committed
5351 5352 5353
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
unknown's avatar
unknown committed
5354
	    if (add_proc_to_list(lex->thd, new Item_field(NULL,NULL,$2.str)))
unknown's avatar
unknown committed
5355
	      YYABORT;
5356
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
5357
	  }
unknown's avatar
unknown committed
5358
	  '(' procedure_list ')';
unknown's avatar
unknown committed
5359 5360 5361 5362


procedure_list:
	/* empty */ {}
unknown's avatar
unknown committed
5363
	| procedure_list2 {};
unknown's avatar
unknown committed
5364 5365 5366

procedure_list2:
	procedure_list2 ',' procedure_item
unknown's avatar
unknown committed
5367
	| procedure_item;
unknown's avatar
unknown committed
5368 5369 5370 5371

procedure_item:
	  remember_name expr
	  {
unknown's avatar
unknown committed
5372 5373
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
unknown's avatar
unknown committed
5374 5375
	      YYABORT;
	    if (!$2->name)
unknown's avatar
unknown committed
5376
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1), YYTHD->charset());
5377 5378 5379 5380 5381 5382
	  }
          ;


select_var_list_init:
	   {
unknown's avatar
unknown committed
5383 5384
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
5385 5386
	        YYABORT;
	   }
5387
	   select_var_list
5388
	   {}
5389
           ;
unknown's avatar
unknown committed
5390

unknown's avatar
unknown committed
5391
select_var_list:
5392 5393 5394 5395
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

5396 5397 5398 5399 5400 5401 5402 5403 5404 5405
select_var_ident:  
	   '@' ident_or_text
           {
             LEX *lex=Lex;
	     if (lex->result) 
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($2,0,0,(enum_field_types)0));
	     else
	       YYABORT;
	   }
           | ident_or_text
unknown's avatar
unknown committed
5406
           {
5407
             LEX *lex=Lex;
5408 5409 5410 5411
	     sp_pvar_t *t;

	     if (!lex->spcont || !(t=lex->spcont->find_pvar(&$1)))
	     {
5412
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
unknown's avatar
unknown committed
5413
	       YYABORT;
5414 5415 5416 5417 5418 5419 5420 5421
	     }
	     if (! lex->result)
	       YYABORT;
	     else
	     {
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($1,1,t->offset,t->type));
	       t->isset= TRUE;
	     }
unknown's avatar
unknown committed
5422
	   }
unknown's avatar
unknown committed
5423
           ;
unknown's avatar
unknown committed
5424

5425
into:
unknown's avatar
unknown committed
5426
        INTO OUTFILE TEXT_STRING_sys
unknown's avatar
unknown committed
5427
	{
unknown's avatar
unknown committed
5428
          LEX *lex= Lex;
5429 5430 5431 5432
          lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
          if (!(lex->exchange= new sql_exchange($3.str, 0)) ||
              !(lex->result= new select_export(lex->exchange)))
            YYABORT;
unknown's avatar
unknown committed
5433 5434
	}
	opt_field_term opt_line_term
unknown's avatar
unknown committed
5435
	| INTO DUMPFILE TEXT_STRING_sys
unknown's avatar
unknown committed
5436
	{
5437
	  LEX *lex=Lex;
unknown's avatar
unknown committed
5438 5439
	  if (!lex->describe)
	  {
5440
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
5441 5442 5443 5444 5445
	    if (!(lex->exchange= new sql_exchange($3.str,1)))
	      YYABORT;
	    if (!(lex->result= new select_dump(lex->exchange)))
	      YYABORT;
	  }
unknown's avatar
unknown committed
5446
	}
5447
        | INTO select_var_list_init
unknown's avatar
unknown committed
5448
	{
5449
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5450 5451
	}
        ;
unknown's avatar
unknown committed
5452

unknown's avatar
unknown committed
5453 5454 5455
/*
  DO statement
*/
unknown's avatar
unknown committed
5456

5457
do:	DO_SYM
unknown's avatar
unknown committed
5458 5459 5460
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
5461 5462 5463 5464 5465
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
unknown's avatar
unknown committed
5466
	}
5467 5468
	;

unknown's avatar
unknown committed
5469
/*
5470
  Drop : delete tables or index or user
unknown's avatar
unknown committed
5471 5472 5473
*/

drop:
unknown's avatar
unknown committed
5474
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
unknown's avatar
unknown committed
5475
	{
unknown's avatar
unknown committed
5476 5477
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
5478 5479
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
unknown's avatar
unknown committed
5480
	}
unknown's avatar
unknown committed
5481
	| DROP INDEX_SYM ident ON table_ident {}
unknown's avatar
unknown committed
5482
	  {
unknown's avatar
unknown committed
5483 5484
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
5485 5486 5487
	     lex->alter_info.drop_list.empty();
	     lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
                                                                $3.str));
unknown's avatar
unknown committed
5488 5489
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
unknown's avatar
unknown committed
5490 5491 5492 5493
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
unknown's avatar
unknown committed
5494 5495 5496 5497
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
	    lex->name=$4.str;
unknown's avatar
unknown committed
5498
	 }
5499
	| DROP FUNCTION_SYM if_exists sp_name
unknown's avatar
unknown committed
5500
	  {
unknown's avatar
unknown committed
5501
	    LEX *lex=Lex;
5502 5503
	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
5504
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
5505 5506
	      YYABORT;
	    }
unknown's avatar
unknown committed
5507
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
5508 5509 5510 5511 5512 5513 5514 5515
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
	  }
	| DROP PROCEDURE if_exists sp_name
	  {
	    LEX *lex=Lex;
	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
5516
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
5517 5518 5519 5520 5521
	      YYABORT;
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
5522
	  }
5523
	| DROP USER clear_privileges user_list
5524
	  {
5525 5526
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
5527 5528 5529 5530 5531 5532 5533 5534 5535 5536
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
        | DROP TRIGGER_SYM ident '.' ident
          {
            LEX *lex= Lex;
unknown's avatar
unknown committed
5537

5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548
            lex->sql_command= SQLCOM_DROP_TRIGGER;
            /* QQ: Could we loosen lock type in certain cases ? */
            if (!lex->select_lex.add_table_to_list(YYTHD,
                                                   new Table_ident($3),
                                                   (LEX_STRING*) 0,
                                                   TL_OPTION_UPDATING,
                                                   TL_WRITE))
              YYABORT;
            lex->name_and_length= $5;
          }
	;
unknown's avatar
unknown committed
5549 5550

table_list:
5551
	table_name
unknown's avatar
unknown committed
5552
	| table_list ',' table_name;
unknown's avatar
unknown committed
5553

5554
table_name:
unknown's avatar
unknown committed
5555
	table_ident
unknown's avatar
unknown committed
5556 5557 5558 5559 5560
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
unknown's avatar
unknown committed
5561 5562

if_exists:
5563
	/* empty */ { $$= 0; }
5564 5565
	| IF EXISTS { $$= 1; }
	;
unknown's avatar
unknown committed
5566

5567 5568 5569 5570
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
unknown's avatar
unknown committed
5571 5572 5573 5574 5575
/*
** Insert : add new data to table
*/

insert:
5576 5577 5578
	INSERT
	{
	  LEX *lex= Lex;
5579 5580
	  lex->sql_command= SQLCOM_INSERT;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
5581
	  mysql_init_select(lex);
5582 5583
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
5584
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
5585
	} insert_lock_option
5586
	opt_ignore insert2
unknown's avatar
unknown committed
5587
	{
unknown's avatar
unknown committed
5588
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
5589
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
5590
	}
5591
	insert_field_spec opt_insert_update
5592
	{}
unknown's avatar
unknown committed
5593
	;
unknown's avatar
unknown committed
5594 5595

replace:
5596 5597
	REPLACE
	{
5598
	  LEX *lex=Lex;
5599 5600
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
unknown's avatar
unknown committed
5601
	  mysql_init_select(lex);
5602
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
5603
	}
unknown's avatar
unknown committed
5604 5605
	replace_lock_option insert2
	{
unknown's avatar
unknown committed
5606
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
5607
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
5608 5609
	}
	insert_field_spec
5610
	{}
unknown's avatar
unknown committed
5611
	;
unknown's avatar
unknown committed
5612 5613

insert_lock_option:
unknown's avatar
unknown committed
5614 5615 5616 5617
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
5618
	;
unknown's avatar
unknown committed
5619 5620

replace_lock_option:
unknown's avatar
unknown committed
5621 5622
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
unknown's avatar
unknown committed
5623 5624 5625

insert2:
	INTO insert_table {}
unknown's avatar
unknown committed
5626
	| insert_table {};
unknown's avatar
unknown committed
5627 5628

insert_table:
5629
	table_name
unknown's avatar
unknown committed
5630
	{
unknown's avatar
unknown committed
5631 5632 5633 5634
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
unknown's avatar
unknown committed
5635
	};
unknown's avatar
unknown committed
5636 5637

insert_field_spec:
unknown's avatar
unknown committed
5638 5639 5640
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
unknown's avatar
unknown committed
5641 5642
	| SET
	  {
unknown's avatar
unknown committed
5643 5644 5645
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
5646 5647
	      YYABORT;
	   }
unknown's avatar
unknown committed
5648
	   ident_eq_list;
unknown's avatar
unknown committed
5649 5650 5651 5652

opt_field_spec:
	/* empty */	  { }
	| '(' fields ')'  { }
unknown's avatar
unknown committed
5653
	| '(' ')'	  { };
unknown's avatar
unknown committed
5654 5655 5656

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
unknown's avatar
unknown committed
5657
	| insert_ident		{ Lex->field_list.push_back($1); };
unknown's avatar
unknown committed
5658 5659 5660

insert_values:
	VALUES	values_list  {}
unknown's avatar
unknown committed
5661
	| VALUE_SYM values_list  {}
unknown's avatar
unknown committed
5662 5663
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
5664
        ;
unknown's avatar
unknown committed
5665 5666 5667

values_list:
	values_list ','  no_braces
unknown's avatar
unknown committed
5668
	| no_braces;
unknown's avatar
unknown committed
5669 5670 5671 5672

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
unknown's avatar
unknown committed
5673
	ident_eq_value;
unknown's avatar
unknown committed
5674 5675

ident_eq_value:
5676
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
5677
	 {
unknown's avatar
unknown committed
5678 5679 5680
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
unknown's avatar
unknown committed
5681
	    YYABORT;
unknown's avatar
unknown committed
5682
	 };
unknown's avatar
unknown committed
5683 5684

equal:	EQ		{}
unknown's avatar
unknown committed
5685 5686 5687 5688 5689 5690 5691
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
unknown's avatar
unknown committed
5692 5693 5694 5695 5696 5697 5698 5699 5700

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
unknown's avatar
unknown committed
5701 5702
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
5703
	    YYABORT;
unknown's avatar
unknown committed
5704
	 };
unknown's avatar
unknown committed
5705 5706 5707

opt_values:
	/* empty */ {}
unknown's avatar
unknown committed
5708
	| values;
unknown's avatar
unknown committed
5709 5710

values:
unknown's avatar
unknown committed
5711
	values ','  expr_or_default
unknown's avatar
unknown committed
5712 5713 5714 5715
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
unknown's avatar
unknown committed
5716 5717 5718 5719 5720 5721 5722 5723
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
5724
	expr	  { $$= $1;}
unknown's avatar
SCRUM  
unknown committed
5725
	| DEFAULT {$$= new Item_default_value(); }
unknown's avatar
unknown committed
5726
	;
unknown's avatar
unknown committed
5727

5728 5729
opt_insert_update:
        /* empty */
unknown's avatar
unknown committed
5730
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
unknown's avatar
unknown committed
5731
          KEY_SYM UPDATE_SYM insert_update_list
5732 5733
        ;

unknown's avatar
unknown committed
5734 5735 5736
/* Update rows in a table */

update:
5737 5738
	UPDATE_SYM
	{
5739
	  LEX *lex= Lex;
unknown's avatar
unknown committed
5740
	  mysql_init_select(lex);
5741
          lex->sql_command= SQLCOM_UPDATE;
unknown's avatar
unknown committed
5742
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
5743
	  lex->duplicates= DUP_ERROR; 
5744
        }
unknown's avatar
unknown committed
5745
        opt_low_priority opt_ignore join_table_list
unknown's avatar
unknown committed
5746
	SET update_list
unknown's avatar
unknown committed
5747
	{
5748 5749
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
5750
	  {
unknown's avatar
unknown committed
5751
            lex->sql_command= SQLCOM_UPDATE_MULTI;
unknown's avatar
unknown committed
5752
	    lex->multi_lock_option= $3;
5753
	  }
5754 5755 5756
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
5757 5758
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
5759 5760
	    YYABORT;
	  }
5761
	  else
unknown's avatar
unknown committed
5762
	    Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
5763
	}
5764
	where_clause opt_order_clause delete_limit_clause {}
unknown's avatar
unknown committed
5765
	;
unknown's avatar
unknown committed
5766 5767

update_list:
unknown's avatar
unknown committed
5768 5769 5770 5771
	update_list ',' update_elem
	| update_elem;

update_elem:
unknown's avatar
unknown committed
5772
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
5773
	{
unknown's avatar
unknown committed
5774
	  if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
unknown's avatar
unknown committed
5775
	    YYABORT;
unknown's avatar
unknown committed
5776 5777 5778 5779 5780 5781 5782
	};

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
unknown's avatar
unknown committed
5783
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
5784 5785 5786 5787
	{
	  LEX *lex= Lex;
	  if (lex->update_list.push_back($1) || 
	      lex->value_list.push_back($3))
unknown's avatar
unknown committed
5788
	    YYABORT;
unknown's avatar
unknown committed
5789
	};
unknown's avatar
unknown committed
5790 5791

opt_low_priority:
5792
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
5793
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
5794 5795 5796 5797

/* Delete rows from a table */

delete:
5798
	DELETE_SYM
5799
	{
5800 5801
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
unknown's avatar
unknown committed
5802
	  mysql_init_select(lex);
unknown's avatar
unknown committed
5803
	  lex->lock_option= lex->thd->update_lock_default;
5804
	  lex->ignore= 0;
5805
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
5806
	}
unknown's avatar
unknown committed
5807 5808
	opt_delete_options single_multi {}
	;
5809 5810

single_multi:
unknown's avatar
unknown committed
5811 5812
 	FROM table_ident
	{
unknown's avatar
unknown committed
5813 5814
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
unknown's avatar
unknown committed
5815 5816 5817
	    YYABORT;
	}
	where_clause opt_order_clause
5818
	delete_limit_clause {}
unknown's avatar
unknown committed
5819
	| table_wild_list
unknown's avatar
unknown committed
5820
	  { mysql_init_multi_delete(Lex); }
unknown's avatar
unknown committed
5821
          FROM join_table_list where_clause
unknown's avatar
unknown committed
5822 5823
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
5824 5825 5826
	  USING join_table_list where_clause
	  {}
	;
unknown's avatar
unknown committed
5827 5828 5829

table_wild_list:
	  table_wild_one {}
unknown's avatar
unknown committed
5830
	  | table_wild_list ',' table_wild_one {};
unknown's avatar
unknown committed
5831 5832

table_wild_one:
unknown's avatar
unknown committed
5833
	ident opt_wild opt_table_alias
unknown's avatar
unknown committed
5834
	{
unknown's avatar
unknown committed
5835 5836
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
					 TL_OPTION_UPDATING, Lex->lock_option))
unknown's avatar
unknown committed
5837 5838
	    YYABORT;
        }
unknown's avatar
unknown committed
5839
	| ident '.' ident opt_wild opt_table_alias
unknown's avatar
unknown committed
5840
	  {
unknown's avatar
unknown committed
5841 5842
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
unknown's avatar
unknown committed
5843 5844
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
unknown's avatar
unknown committed
5845
	      YYABORT;
unknown's avatar
unknown committed
5846
	  }
unknown's avatar
unknown committed
5847
	;
unknown's avatar
unknown committed
5848 5849

opt_wild:
5850
	/* empty */	{}
unknown's avatar
unknown committed
5851
	| '.' '*'	{};
unknown's avatar
unknown committed
5852 5853


5854
opt_delete_options:
unknown's avatar
unknown committed
5855
	/* empty */	{}
unknown's avatar
unknown committed
5856
	| opt_delete_option opt_delete_options {};
5857 5858

opt_delete_option:
5859
	QUICK		{ Select->options|= OPTION_QUICK; }
5860
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
5861
	| IGNORE_SYM	{ Lex->ignore= 1; };
5862

5863
truncate:
unknown's avatar
unknown committed
5864
	TRUNCATE_SYM opt_table_sym table_name
5865
	{
5866
	  LEX* lex= Lex;
5867
	  lex->sql_command= SQLCOM_TRUNCATE;
5868
	  lex->select_lex.options= 0;
5869
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
5870 5871
	}
	;
5872

unknown's avatar
unknown committed
5873 5874
opt_table_sym:
	/* empty */
unknown's avatar
unknown committed
5875
	| TABLE_SYM;
5876

unknown's avatar
unknown committed
5877 5878
/* Show things */

5879 5880
show:	SHOW
	{
unknown's avatar
unknown committed
5881 5882 5883 5884
	  LEX *lex=Lex;
	  lex->wild=0;
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
unknown's avatar
unknown committed
5885
	show_param
5886 5887
	{}
	;
unknown's avatar
unknown committed
5888 5889

show_param:
5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915
         DATABASES ext_select_item_list wild_and_where
         {
           LEX *lex= Lex;
           lex->sql_command= SQLCOM_SELECT;
           lex->orig_sql_command= SQLCOM_SHOW_DATABASES;
           if (prepare_schema_table(YYTHD, lex, 0, SCH_SCHEMATA))
             YYABORT;
         }
         | opt_full TABLES ext_select_item_list opt_db wild_and_where
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLES;
             lex->select_lex.db= $4;
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLE_NAMES))
               YYABORT;
           }
         | TABLE_SYM STATUS_SYM ext_select_item_list opt_db wild_and_where
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLE_STATUS;
             lex->select_lex.db= $4;
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLES))
               YYABORT;
           }
5916 5917 5918 5919 5920 5921 5922 5923 5924
        | OPEN_SYM TABLES ext_select_item_list opt_db wild_and_where
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_OPEN_TABLES;
            lex->select_lex.db= $4;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_OPEN_TABLES))
              YYABORT;
          }
unknown's avatar
unknown committed
5925
	| ENGINE_SYM storage_engines 
unknown's avatar
unknown committed
5926 5927
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
5928
	| opt_full COLUMNS ext_select_item_list from_or_in table_ident opt_db wild_and_where
unknown's avatar
unknown committed
5929
	  {
5930 5931 5932
 	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SELECT;
	    lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
5933 5934 5935
	    if ($6)
	      $5->change_db($6);
	    if (prepare_schema_table(YYTHD, lex, $5, SCH_COLUMNS))
unknown's avatar
unknown committed
5936 5937
	      YYABORT;
	  }
5938
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
unknown's avatar
unknown committed
5939 5940
	  TEXT_STRING_sys AND_SYM MASTER_LOG_POS_SYM EQ ulonglong_num
	  AND_SYM MASTER_SERVER_ID_SYM EQ
unknown's avatar
unknown committed
5941 5942 5943 5944 5945
	ULONG_NUM
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
5946
	    Lex->mi.server_id = $16;
unknown's avatar
unknown committed
5947
          }
5948
        | master_or_binary LOGS_SYM
unknown's avatar
unknown committed
5949 5950
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
5951 5952 5953 5954 5955
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
unknown's avatar
unknown committed
5956
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
unknown's avatar
unknown committed
5957
          {
5958 5959
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
5960
          } opt_limit_clause_init
5961
        | keys_or_index ext_select_item_list from_or_in table_ident opt_db where_clause
5962 5963 5964 5965
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_KEYS;
5966 5967 5968
	    if ($5)
	      $4->change_db($5);
            if (prepare_schema_table(YYTHD, lex, $4, SCH_STATISTICS))
5969
              YYABORT;
unknown's avatar
unknown committed
5970
	  }
unknown's avatar
unknown committed
5971 5972 5973 5974 5975 5976 5977 5978
	| COLUMN_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_COLUMN_TYPES;
	  }
	| TABLE_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
unknown's avatar
unknown committed
5979 5980 5981 5982 5983 5984 5985
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
	    WARN_DEPRECATED("SHOW TABLE TYPES", "SHOW [STORAGE] ENGINES");
	  }
	| opt_storage ENGINES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
unknown's avatar
unknown committed
5986 5987 5988 5989 5990 5991
	  }
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
5992
        | COUNT_SYM '(' '*' ')' WARNINGS
5993
          { (void) create_select_for_variable("warning_count"); }
5994
        | COUNT_SYM '(' '*' ')' ERRORS
5995
	  { (void) create_select_for_variable("error_count"); }
5996
        | WARNINGS opt_limit_clause_init
unknown's avatar
unknown committed
5997
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
5998
        | ERRORS opt_limit_clause_init
5999
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
6000
        | opt_var_type STATUS_SYM ext_select_item_list wild_and_where
6001
          {
6002 6003 6004 6005 6006 6007 6008
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS;
            lex->option_type= (enum_var_type) $1;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_STATUS))
              YYABORT;
          }
unknown's avatar
unknown committed
6009
        | INNOBASE_SYM STATUS_SYM
unknown's avatar
unknown committed
6010
          { Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS; WARN_DEPRECATED("SHOW INNODB STATUS", "SHOW ENGINE INNODB STATUS"); }
unknown's avatar
unknown committed
6011 6012
        | MUTEX_SYM STATUS_SYM
          { Lex->sql_command = SQLCOM_SHOW_MUTEX_STATUS; }
unknown's avatar
unknown committed
6013 6014
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
6015 6016 6017 6018 6019 6020 6021 6022 6023
        | opt_var_type  VARIABLES ext_select_item_list wild_and_where
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_VARIABLES;
            lex->option_type= (enum_var_type) $1;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_VARIABLES))
              YYABORT;
          }
6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039
        | charset ext_select_item_list wild_and_where
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_CHARSETS;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_CHARSETS))
              YYABORT;
          }
        | COLLATION_SYM ext_select_item_list wild_and_where
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_COLLATIONS;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_COLLATIONS))
              YYABORT;
          }
6040
	| BERKELEY_DB_SYM LOGS_SYM
unknown's avatar
unknown committed
6041
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW BDB LOGS", "SHOW ENGINE BDB LOGS"); }
unknown's avatar
unknown committed
6042
	| LOGS_SYM
unknown's avatar
unknown committed
6043
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW LOGS", "SHOW ENGINE BDB LOGS"); }
6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066
	| GRANTS
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    THD *thd= lex->thd;
	    LEX_USER *curr_user;
            if (!(curr_user= (LEX_USER*) thd->alloc(sizeof(st_lex_user))))
              YYABORT;
            curr_user->user.str= thd->priv_user;
            curr_user->user.length= strlen(thd->priv_user);
            if (*thd->priv_host != 0)
            {
              curr_user->host.str= thd->priv_host;
              curr_user->host.length= strlen(thd->priv_host);
            }
            else
            {
              curr_user->host.str= (char *) "%";
              curr_user->host.length= 1;
            }
            curr_user->password.str=NullS;
	    lex->grant_user= curr_user;
	  }
unknown's avatar
unknown committed
6067
	| GRANTS FOR_SYM user
unknown's avatar
unknown committed
6068 6069 6070 6071 6072 6073
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
	    lex->grant_user->password.str=NullS;
	  }
unknown's avatar
unknown committed
6074
	| CREATE DATABASE opt_if_not_exists ident
unknown's avatar
unknown committed
6075 6076
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
unknown's avatar
unknown committed
6077 6078
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
unknown's avatar
unknown committed
6079
	  }
unknown's avatar
unknown committed
6080 6081
        | CREATE TABLE_SYM table_ident
          {
6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092
            LEX *lex= Lex;
	    lex->sql_command = SQLCOM_SHOW_CREATE;
	    if (!lex->select_lex.add_table_to_list(YYTHD, $3, NULL,0))
	      YYABORT;
            lex->only_view= 0;
	  }
        | CREATE VIEW_SYM table_ident
          {
            LEX *lex= Lex;
	    lex->sql_command = SQLCOM_SHOW_CREATE;
	    if (!lex->select_lex.add_table_to_list(YYTHD, $3, NULL, 0))
unknown's avatar
unknown committed
6093
	      YYABORT;
6094
            lex->only_view= 1;
unknown's avatar
unknown committed
6095 6096 6097 6098
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
unknown's avatar
unknown committed
6099
          }
unknown's avatar
unknown committed
6100 6101 6102
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117
          }
	| CREATE PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

	    lex->sql_command = SQLCOM_SHOW_CREATE_PROC;
	    lex->spname= $3;
	  }
	| CREATE FUNCTION_SYM sp_name
	  {
	    LEX *lex= Lex;

	    lex->sql_command = SQLCOM_SHOW_CREATE_FUNC;
	    lex->spname= $3;
	  }
6118
	| PROCEDURE STATUS_SYM ext_select_item_list wild_and_where
6119
	  {
6120 6121 6122 6123 6124
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_PROC;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
6125
	  }
6126
	| FUNCTION_SYM STATUS_SYM ext_select_item_list wild_and_where
6127
	  {
6128 6129 6130 6131 6132
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_FUNC;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
6133
	  };
unknown's avatar
unknown committed
6134

unknown's avatar
unknown committed
6135 6136 6137 6138 6139 6140 6141 6142
show_engine_param:
	STATUS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_INNODB:
	      Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;
	      break;
	    default:
unknown's avatar
unknown committed
6143
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "STATUS");
unknown's avatar
unknown committed
6144 6145 6146 6147 6148 6149 6150 6151 6152 6153
	      YYABORT;
	    }
	  }
	| LOGS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_BERKELEY_DB:
	      Lex->sql_command = SQLCOM_SHOW_LOGS;
	      break;
	    default:
unknown's avatar
unknown committed
6154
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "LOGS");
unknown's avatar
unknown committed
6155 6156 6157 6158
	      YYABORT;
	    }
	  };

6159 6160 6161 6162
master_or_binary:
	MASTER_SYM
	| BINARY;

unknown's avatar
unknown committed
6163 6164 6165 6166
opt_storage:
	/* empty */
	| STORAGE_SYM;

unknown's avatar
unknown committed
6167 6168
opt_db:
	/* empty */  { $$= 0; }
unknown's avatar
unknown committed
6169
	| from_or_in ident { $$= $2.str; };
unknown's avatar
unknown committed
6170

unknown's avatar
unknown committed
6171 6172
opt_full:
	/* empty */ { Lex->verbose=0; }
unknown's avatar
unknown committed
6173
	| FULL	    { Lex->verbose=1; };
unknown's avatar
unknown committed
6174

unknown's avatar
unknown committed
6175 6176
from_or_in:
	FROM
unknown's avatar
unknown committed
6177
	| IN_SYM;
unknown's avatar
unknown committed
6178

unknown's avatar
unknown committed
6179 6180
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
unknown's avatar
unknown committed
6181
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
unknown's avatar
unknown committed
6182 6183 6184

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

6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207
wild_and_where:
      /* empty */
      | LIKE TEXT_STRING_sys
	{ Lex->wild=  new (YYTHD->mem_root) String($2.str, $2.length,
                                                   system_charset_info); }
      | WHERE expr
        {
          Select->where= $2;
          if ($2)
            $2->top_level_item();
        }
      ;

ext_select_item_list:
      {
        LEX *lex=Lex;
        SELECT_LEX *sel= lex->current_select;
        lex->lock_option= TL_READ;
        mysql_init_select(lex);
        lex->current_select->parsing_place= SELECT_LIST;
      }
6208 6209 6210 6211
      ext_select_item_list2;

ext_select_item_list2:
      /* empty */        {}
6212 6213
      | select_item_list {};

unknown's avatar
unknown committed
6214

unknown's avatar
unknown committed
6215 6216 6217 6218
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
6219 6220 6221 6222 6223 6224 6225 6226 6227
          LEX *lex= Lex;
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
          lex->sql_command= SQLCOM_SELECT;
          lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
          lex->select_lex.db= 0;
          lex->verbose= 0;
          if (prepare_schema_table(YYTHD, lex, $2, SCH_COLUMNS))
unknown's avatar
unknown committed
6228 6229
	    YYABORT;
	}
6230
	opt_describe_column {}
unknown's avatar
unknown committed
6231 6232 6233
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
6234
          {
unknown's avatar
unknown committed
6235
	    LEX *lex=Lex;
6236
	    lex->select_lex.options|= SELECT_DESCRIBE;
unknown's avatar
unknown committed
6237 6238
	  }
	;
unknown's avatar
unknown committed
6239 6240 6241

describe_command:
	DESC
unknown's avatar
unknown committed
6242
	| DESCRIBE;
unknown's avatar
unknown committed
6243

unknown's avatar
unknown committed
6244 6245 6246 6247 6248
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
	;

unknown's avatar
unknown committed
6249 6250 6251
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
unknown's avatar
unknown committed
6252
	| ident
unknown's avatar
unknown committed
6253
	  { Lex->wild= new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info); };
unknown's avatar
unknown committed
6254 6255 6256 6257 6258


/* flush things */

flush:
6259
	FLUSH_SYM opt_no_write_to_binlog
unknown's avatar
unknown committed
6260 6261 6262
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
unknown's avatar
unknown committed
6263
          lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
6264
	}
6265 6266 6267
	flush_options
	{}
	;
unknown's avatar
unknown committed
6268 6269 6270

flush_options:
	flush_options ',' flush_option
unknown's avatar
unknown committed
6271
	| flush_option;
unknown's avatar
unknown committed
6272 6273

flush_option:
6274
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
unknown's avatar
unknown committed
6275
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
unknown's avatar
unknown committed
6276
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
unknown's avatar
unknown committed
6277 6278 6279 6280
	| 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
6281 6282
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
6283 6284
	| DES_KEY_FILE	{ Lex->type|= REFRESH_DES_KEY_FILE; }
 	| RESOURCES     { Lex->type|= REFRESH_USER_RESOURCES; };
unknown's avatar
unknown committed
6285

unknown's avatar
unknown committed
6286
opt_table_list:
6287 6288
	/* empty */  {;}
	| table_list {;};
unknown's avatar
unknown committed
6289

unknown's avatar
unknown committed
6290
reset:
unknown's avatar
unknown committed
6291 6292 6293 6294
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
6295 6296 6297 6298
	} reset_options
	{}
	;

unknown's avatar
unknown committed
6299 6300
reset_options:
	reset_options ',' reset_option
unknown's avatar
unknown committed
6301
	| reset_option;
unknown's avatar
unknown committed
6302 6303

reset_option:
unknown's avatar
unknown committed
6304 6305
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
unknown's avatar
unknown committed
6306
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
unknown's avatar
unknown committed
6307

unknown's avatar
unknown committed
6308
purge:
unknown's avatar
unknown committed
6309 6310 6311 6312
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
6313 6314 6315 6316 6317
	} purge_options
	{}
	;

purge_options:
6318
	master_or_binary LOGS_SYM purge_option
unknown's avatar
unknown committed
6319
	;
6320 6321

purge_option:
unknown's avatar
unknown committed
6322
        TO_SYM TEXT_STRING_sys
6323 6324 6325 6326 6327 6328
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
6329 6330 6331 6332
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
unknown's avatar
unknown committed
6333
	}
unknown's avatar
unknown committed
6334
	;
unknown's avatar
unknown committed
6335

unknown's avatar
unknown committed
6336 6337 6338
/* kill threads */

kill:
6339
	KILL_SYM kill_option expr
unknown's avatar
unknown committed
6340
	{
unknown's avatar
unknown committed
6341
	  LEX *lex=Lex;
6342 6343 6344
	  lex->value_list.empty();
	  lex->value_list.push_front($3);
          lex->sql_command= SQLCOM_KILL;
unknown's avatar
unknown committed
6345
	};
unknown's avatar
unknown committed
6346

6347 6348 6349 6350 6351
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; };

unknown's avatar
unknown committed
6352 6353 6354
/* change database */

use:	USE_SYM ident
unknown's avatar
unknown committed
6355 6356
	{
	  LEX *lex=Lex;
6357 6358
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
unknown's avatar
unknown committed
6359
	};
unknown's avatar
unknown committed
6360 6361 6362

/* import, export of files */

unknown's avatar
unknown committed
6363
load:	LOAD DATA_SYM load_data_lock opt_local INFILE TEXT_STRING_sys
unknown's avatar
unknown committed
6364
	{
unknown's avatar
unknown committed
6365 6366
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
unknown's avatar
unknown committed
6367 6368
	  lex->lock_option= $3;
	  lex->local_file=  $4;
6369 6370
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
unknown's avatar
unknown committed
6371
	  if (!(lex->exchange= new sql_exchange($6.str,0)))
unknown's avatar
unknown committed
6372
	    YYABORT;
unknown's avatar
unknown committed
6373
	  lex->field_list.empty();
unknown's avatar
unknown committed
6374 6375 6376 6377
	}
	opt_duplicate INTO TABLE_SYM table_ident opt_field_term opt_line_term
	opt_ignore_lines opt_field_spec
	{
unknown's avatar
unknown committed
6378
	  if (!Select->add_table_to_list(YYTHD, $11, NULL, TL_OPTION_UPDATING))
unknown's avatar
unknown committed
6379 6380 6381
	    YYABORT;
	}
        |
unknown's avatar
unknown committed
6382
	LOAD TABLE_SYM table_ident FROM MASTER_SYM
unknown's avatar
unknown committed
6383 6384
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
unknown's avatar
unknown committed
6385
	  if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
unknown's avatar
unknown committed
6386
	    YYABORT;
unknown's avatar
unknown committed
6387 6388

        }
6389 6390 6391 6392
        |
	LOAD DATA_SYM FROM MASTER_SYM
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
unknown's avatar
unknown committed
6393
        };
unknown's avatar
unknown committed
6394 6395 6396

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

unknown's avatar
unknown committed
6399
load_data_lock:
6400
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
6401 6402
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
6403 6404


unknown's avatar
unknown committed
6405 6406 6407
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
6408
	| IGNORE_SYM	{ Lex->ignore= 1; };
unknown's avatar
unknown committed
6409 6410 6411

opt_field_term:
	/* empty */
unknown's avatar
unknown committed
6412
	| COLUMNS field_term_list;
unknown's avatar
unknown committed
6413 6414 6415

field_term_list:
	field_term_list field_term
unknown's avatar
unknown committed
6416
	| field_term;
unknown's avatar
unknown committed
6417 6418

field_term:
6419 6420 6421 6422 6423
	TERMINATED BY text_string 
          {
            DBUG_ASSERT(Lex->exchange);
            Lex->exchange->field_term= $3;
          }
unknown's avatar
unknown committed
6424
	| OPTIONALLY ENCLOSED BY text_string
unknown's avatar
unknown committed
6425
	  {
unknown's avatar
unknown committed
6426
            LEX *lex= Lex;
6427
            DBUG_ASSERT(lex->exchange);
unknown's avatar
unknown committed
6428 6429
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
unknown's avatar
unknown committed
6430
	  }
unknown's avatar
unknown committed
6431
        | ENCLOSED BY text_string
6432 6433 6434 6435
          {
            DBUG_ASSERT(Lex->exchange);
            Lex->exchange->enclosed= $3;
          }
unknown's avatar
unknown committed
6436
        | ESCAPED BY text_string
6437 6438 6439 6440
          {
            DBUG_ASSERT(Lex->exchange);
            Lex->exchange->escaped= $3;
          };
unknown's avatar
unknown committed
6441 6442 6443

opt_line_term:
	/* empty */
unknown's avatar
unknown committed
6444
	| LINES line_term_list;
unknown's avatar
unknown committed
6445 6446 6447

line_term_list:
	line_term_list line_term
unknown's avatar
unknown committed
6448
	| line_term;
unknown's avatar
unknown committed
6449 6450

line_term:
unknown's avatar
unknown committed
6451
        TERMINATED BY text_string
6452 6453 6454 6455
          {
            DBUG_ASSERT(Lex->exchange);
            Lex->exchange->line_term= $3;
          }
unknown's avatar
unknown committed
6456
        | STARTING BY text_string
6457 6458 6459 6460
          {
            DBUG_ASSERT(Lex->exchange);
            Lex->exchange->line_start= $3;
          };
unknown's avatar
unknown committed
6461 6462 6463

opt_ignore_lines:
	/* empty */
unknown's avatar
unknown committed
6464 6465
        | IGNORE_SYM NUM LINES
          {
6466 6467 6468
            DBUG_ASSERT(Lex->exchange);
            Lex->exchange->skip_lines= atol($2.str);
          };
unknown's avatar
unknown committed
6469 6470 6471 6472

/* Common definitions */

text_literal:
6473
	TEXT_STRING_literal
unknown's avatar
unknown committed
6474 6475
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
6476
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
unknown's avatar
unknown committed
6477
	}
unknown's avatar
unknown committed
6478
	| NCHAR_STRING
6479
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
6480
	| UNDERSCORE_CHARSET TEXT_STRING
6481
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
6482
	| text_literal TEXT_STRING_literal
unknown's avatar
unknown committed
6483 6484
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
unknown's avatar
unknown committed
6485 6486

text_string:
6487
	TEXT_STRING_literal
unknown's avatar
unknown committed
6488
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
unknown's avatar
unknown committed
6489 6490
	| HEX_NUM
	  {
unknown's avatar
unknown committed
6491
	    Item *tmp= new Item_hex_string($1.str, $1.length);
6492
	    /*
unknown's avatar
unknown committed
6493
	      it is OK only emulate fix_fields, because we need only
6494 6495 6496
              value of constant
	    */
	    $$= tmp ?
unknown's avatar
unknown committed
6497
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6498
	      (String*) 0;
6499
	  }
unknown's avatar
unknown committed
6500 6501 6502 6503 6504 6505 6506 6507 6508 6509
        | BIN_NUM
          {
	    Item *tmp= new Item_bin_string($1.str, $1.length);
	    /*
	      it is OK only emulate fix_fields, because we need only
              value of constant
	    */
	    $$= tmp ? tmp->quick_fix_field(), tmp->val_str((String*) 0) :
		      (String*) 0;
          }
6510 6511
	;

unknown's avatar
unknown committed
6512
param_marker:
6513
        '?'
unknown's avatar
unknown committed
6514
        {
6515 6516 6517
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
          if (thd->command == COM_PREPARE)
6518
          {
6519 6520 6521 6522
            Item_param *item= new Item_param((uint) (lex->tok_start -
                                                     (uchar *) thd->query));
            if (!($$= item) || lex->param_list.push_back(item))
            {
unknown's avatar
unknown committed
6523
	      my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
6524 6525
	      YYABORT;
            }
unknown's avatar
unknown committed
6526
          }
6527
          else
unknown's avatar
unknown committed
6528
          {
6529
            yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
6530 6531
            YYABORT;
          }
6532 6533 6534
        }
	;

unknown's avatar
unknown committed
6535 6536 6537
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
6538 6539
	| '-' NUM_literal
	  {
6540
	    $2->max_length++;
unknown's avatar
unknown committed
6541
	    $$= $2->neg();
6542
	  }
unknown's avatar
unknown committed
6543 6544 6545
	;


unknown's avatar
unknown committed
6546 6547
literal:
	text_literal	{ $$ =	$1; }
unknown's avatar
unknown committed
6548
	| NUM_literal	{ $$ = $1; }
unknown's avatar
unknown committed
6549
	| NULL_SYM	{ $$ =	new Item_null();
6550
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
unknown's avatar
unknown committed
6551 6552
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
6553
	| UNDERSCORE_CHARSET HEX_NUM
6554
	  {
unknown's avatar
unknown committed
6555
	    Item *tmp= new Item_hex_string($2.str, $2.length);
6556
	    /*
unknown's avatar
unknown committed
6557
	      it is OK only emulate fix_fieds, because we need only
6558 6559 6560
              value of constant
	    */
	    String *str= tmp ?
unknown's avatar
unknown committed
6561
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6562
	      (String*) 0;
unknown's avatar
unknown committed
6563 6564 6565
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
6566
	  }
unknown's avatar
unknown committed
6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580
	| UNDERSCORE_CHARSET BIN_NUM
          {
	    Item *tmp= new Item_bin_string($2.str, $2.length);
	    /*
	      it is OK only emulate fix_fieds, because we need only
              value of constant
	    */
	    String *str= tmp ?
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
	      (String*) 0;
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
          }
unknown's avatar
unknown committed
6581 6582
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
unknown's avatar
unknown committed
6583
	| TIMESTAMP text_literal { $$ = $2; };
unknown's avatar
unknown committed
6584

6585
NUM_literal:
6586 6587
	NUM		{ int error; $$ = new Item_int($1.str, (longlong) my_strtoll10($1.str, NULL, &error), $1.length); }
	| LONG_NUM	{ int error; $$ = new Item_int($1.str, (longlong) my_strtoll10($1.str, NULL, &error), $1.length); }
6588
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604
	| REAL_NUM
	{
	   $$= new Item_real($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
6605 6606
	;
	
unknown's avatar
unknown committed
6607 6608 6609 6610 6611
/**********************************************************************
** Createing different items.
**********************************************************************/

insert_ident:
6612
	simple_ident_nospvar { $$=$1; }
unknown's avatar
unknown committed
6613
	| table_wild	 { $$=$1; };
unknown's avatar
unknown committed
6614 6615

table_wild:
unknown's avatar
unknown committed
6616
	ident '.' '*'
6617 6618
	{
	  $$ = new Item_field(NullS,$1.str,"*");
unknown's avatar
unknown committed
6619
	  Lex->current_select->with_wild++;
6620
	}
unknown's avatar
unknown committed
6621
	| ident '.' ident '.' '*'
6622 6623 6624 6625
	{
	  $$ = new Item_field((YYTHD->client_capabilities &
   			     CLIENT_NO_SCHEMA ? NullS : $1.str),
			     $3.str,"*");
unknown's avatar
unknown committed
6626
	  Lex->current_select->with_wild++;
6627 6628
	}
	;
unknown's avatar
unknown committed
6629 6630

order_ident:
unknown's avatar
unknown committed
6631
	expr { $$=$1; };
unknown's avatar
unknown committed
6632 6633

simple_ident:
6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658
	ident
	{
	  sp_pvar_t *spv;
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;

	  if (spc && (spv = spc->find_pvar(&$1)))
	  { /* We're compiling a stored procedure and found a variable */
	    $$ = (Item*) new Item_splocal($1, spv->offset);
            lex->variables_used= 1;
	    lex->safe_to_cache_query=0;
	  }
	  else
	  {
	    SELECT_LEX *sel=Select;
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
                 (Item*) new Item_field(NullS,NullS,$1.str) :
	         (Item*) new Item_ref(NullS,NullS,$1.str);
	  }
        }
        | simple_ident_q { $$= $1; }
	;

simple_ident_nospvar:
unknown's avatar
unknown committed
6659
	ident
unknown's avatar
unknown committed
6660
	{
unknown's avatar
unknown committed
6661
	  SELECT_LEX *sel=Select;
6662
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
6663 6664
	       sel->get_in_sum_expr() > 0) ?
              (Item*) new Item_field(NullS,NullS,$1.str) :
6665
	      (Item*) new Item_ref(NullS,NullS,$1.str);
unknown's avatar
unknown committed
6666
	}
6667 6668 6669 6670 6671
	| simple_ident_q { $$= $1; }
	;	

simple_ident_q:
	ident '.' ident
unknown's avatar
unknown committed
6672
	{
unknown's avatar
unknown committed
6673
	  THD *thd= YYTHD;
6674
	  LEX *lex= thd->lex;
6675 6676 6677 6678 6679 6680 6681 6682 6683 6684
         
          /*
            FIXME This will work ok in simple_ident_nospvar case because
            we can't meet simple_ident_nospvar in trigger now. But it
            should be changed in future.
          */
          if (lex->sphead && lex->sphead->m_type == TYPE_ENUM_TRIGGER &&
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") || 
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
6685
            Item_trigger_field *trg_fld;
6686 6687 6688 6689 6690
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
            
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
unknown's avatar
unknown committed
6691
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
6692 6693 6694 6695 6696 6697
              YYABORT;
            }
            
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
unknown's avatar
unknown committed
6698
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
6699 6700 6701
              YYABORT;
            }
            
6702 6703 6704 6705
            if (!(trg_fld= new Item_trigger_field(new_row ?
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
                                                  $3.str)))
6706
              YYABORT;
6707 6708 6709 6710 6711 6712 6713
          
            /*
              Let us add this item to list of all Item_trigger_field objects
              in trigger.
            */
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
              (byte**)&trg_fld->next_trg_field);
6714 6715 6716 6717 6718 6719 6720 6721
            
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
6722 6723
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
6724 6725 6726 6727 6728 6729 6730
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         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
6731
	| '.' ident '.' ident
unknown's avatar
unknown committed
6732
	{
unknown's avatar
unknown committed
6733
	  THD *thd= YYTHD;
6734
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
6735
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
6736 6737
	  if (sel->no_table_names_allowed)
	  {
6738 6739
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $2.str, thd->where);
unknown's avatar
unknown committed
6740
	  }
6741
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
6742 6743
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field(NullS,$2.str,$4.str) :
6744
              (Item*) new Item_ref(NullS, $2.str, $4.str);
unknown's avatar
unknown committed
6745
	}
unknown's avatar
unknown committed
6746
	| ident '.' ident '.' ident
unknown's avatar
unknown committed
6747
	{
unknown's avatar
unknown committed
6748
	  THD *thd= YYTHD;
6749
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
6750
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
6751 6752
	  if (sel->no_table_names_allowed)
	  {
6753 6754
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $3.str, thd->where);
unknown's avatar
unknown committed
6755
	  }
6756
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
6757 6758 6759 6760
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field((YYTHD->client_capabilities &
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
6761 6762
	      (Item*) new Item_ref((YYTHD->client_capabilities &
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
unknown's avatar
unknown committed
6763
                                   $3.str, $5.str);
unknown's avatar
unknown committed
6764
	};
unknown's avatar
unknown committed
6765 6766 6767 6768


field_ident:
	ident			{ $$=$1;}
6769
	| ident '.' ident	{ $$=$3;}	/* Skip schema name in create*/
unknown's avatar
unknown committed
6770
	| '.' ident		{ $$=$2;}	/* For Delphi */;
unknown's avatar
unknown committed
6771 6772 6773

table_ident:
	ident			{ $$=new Table_ident($1); }
unknown's avatar
unknown committed
6774
	| ident '.' ident	{ $$=new Table_ident(YYTHD, $1,$3,0);}
6775 6776 6777
	| '.' ident		{ $$=new Table_ident($2);} /* For Delphi */
        ;

unknown's avatar
unknown committed
6778
table_ident_nodb:
unknown's avatar
unknown committed
6779
	ident			{ LEX_STRING db={(char*) any_db,3}; $$=new Table_ident(YYTHD, db,$1,0); }
6780
        ;
unknown's avatar
unknown committed
6781

unknown's avatar
unknown committed
6782
IDENT_sys:
6783 6784 6785 6786 6787
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
6788 6789 6790 6791 6792 6793 6794
            {
              CHARSET_INFO *cs= system_charset_info;
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
                                                   $1.length);
              if (wlen < $1.length)
              {
6795 6796
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
6797 6798
                YYABORT;
              }
6799
	      $$= $1;
6800
            }
6801 6802 6803 6804
	    else
	      thd->convert_string(&$$, system_charset_info,
				  $1.str, $1.length, thd->charset());
	  }
unknown's avatar
unknown committed
6805 6806 6807 6808 6809 6810
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
6811 6812
	  if (thd->charset_is_system_charset)
	    $$= $1;
unknown's avatar
unknown committed
6813
	  else
unknown's avatar
unknown committed
6814 6815
	    thd->convert_string(&$$, system_charset_info,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
6816 6817 6818
	}
	;

6819
TEXT_STRING_literal:
unknown's avatar
unknown committed
6820 6821 6822
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
6823 6824
	  if (thd->charset_is_collation_connection)
	    $$= $1;
unknown's avatar
unknown committed
6825
	  else
unknown's avatar
unknown committed
6826 6827
	    thd->convert_string(&$$, thd->variables.collation_connection,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
6828 6829 6830 6831
	}
	;


unknown's avatar
unknown committed
6832
ident:
unknown's avatar
unknown committed
6833
	IDENT_sys	    { $$=$1; }
unknown's avatar
unknown committed
6834 6835
	| keyword
	{
unknown's avatar
unknown committed
6836 6837 6838
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
6839 6840
	}
	;
unknown's avatar
unknown committed
6841 6842

ident_or_text:
unknown's avatar
unknown committed
6843 6844 6845
	ident 			{ $$=$1;}
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
unknown's avatar
unknown committed
6846 6847 6848 6849

user:
	ident_or_text
	{
unknown's avatar
unknown committed
6850 6851
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
6852
	    YYABORT;
6853 6854 6855 6856
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
unknown's avatar
unknown committed
6857 6858
	| ident_or_text '@' ident_or_text
	  {
unknown's avatar
unknown committed
6859 6860
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
6861 6862
	      YYABORT;
	    $$->user = $1; $$->host=$3;
6863
	  }
unknown's avatar
unknown committed
6864
	| CURRENT_USER optional_braces
6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881
	{
          THD *thd= YYTHD;
          if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
            YYABORT;
          $$->user.str= thd->priv_user;
          $$->user.length= strlen(thd->priv_user);
          if (*thd->priv_host != 0)
          {
            $$->host.str= thd->priv_host;
            $$->host.length= strlen(thd->priv_host);
          }
          else
          {
            $$->host.str= (char *) "%";
            $$->host.length= 1;
          }
	};
unknown's avatar
unknown committed
6882 6883 6884 6885 6886

/* Keyword that we allow for identifiers */

keyword:
	ACTION			{}
unknown's avatar
unknown committed
6887
	| ADDDATE_SYM		{}
unknown's avatar
unknown committed
6888
	| AFTER_SYM		{}
6889
	| AGAINST		{}
unknown's avatar
unknown committed
6890
	| AGGREGATE_SYM		{}
6891
	| ALGORITHM_SYM		{}
unknown's avatar
unknown committed
6892
	| ANY_SYM		{}
unknown's avatar
unknown committed
6893
	| ASCII_SYM		{}
unknown's avatar
unknown committed
6894
	| AUTO_INC		{}
unknown's avatar
unknown committed
6895 6896
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
unknown's avatar
unknown committed
6897
	| BACKUP_SYM		{}
unknown's avatar
unknown committed
6898
	| BEGIN_SYM		{}
unknown's avatar
unknown committed
6899
	| BERKELEY_DB_SYM	{}
unknown's avatar
unknown committed
6900
	| BINLOG_SYM		{}
unknown's avatar
unknown committed
6901 6902
	| BIT_SYM		{}
	| BOOL_SYM		{}
unknown's avatar
unknown committed
6903
	| BOOLEAN_SYM		{}
6904
	| BYTE_SYM		{}
unknown's avatar
unknown committed
6905
	| BTREE_SYM		{}
unknown's avatar
unknown committed
6906
	| CACHE_SYM		{}
6907
	| CASCADED              {}
unknown's avatar
unknown committed
6908
	| CHAIN_SYM		{}
6909
	| CHANGED		{}
6910
	| CHARSET		{}
unknown's avatar
unknown committed
6911
	| CHECKSUM_SYM		{}
6912
	| CIPHER_SYM		{}
unknown's avatar
unknown committed
6913
	| CLIENT_SYM		{}
6914
	| CLOSE_SYM		{}
unknown's avatar
unknown committed
6915
	| COLLATION_SYM		{}
6916
        | COLUMNS               {}
unknown's avatar
unknown committed
6917
	| COMMENT_SYM		{}
unknown's avatar
unknown committed
6918
	| COMMITTED_SYM		{}
unknown's avatar
unknown committed
6919
	| COMMIT_SYM		{}
6920
	| COMPACT_SYM		{}
unknown's avatar
unknown committed
6921
	| COMPRESSED_SYM	{}
unknown's avatar
unknown committed
6922
	| CONCURRENT		{}
6923
	| CONSISTENT_SYM	{}
6924
	| CONTAINS_SYM          {}
6925
	| CUBE_SYM		{}
unknown's avatar
unknown committed
6926 6927 6928 6929
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
unknown's avatar
unknown committed
6930
        | DEALLOCATE_SYM        {}
6931
	| DEFINER_SYM		{}
unknown's avatar
unknown committed
6932
	| DELAY_KEY_WRITE_SYM	{}
unknown's avatar
unknown committed
6933 6934
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
unknown's avatar
unknown committed
6935
	| DISCARD		{}
unknown's avatar
unknown committed
6936
	| DO_SYM		{}
unknown's avatar
unknown committed
6937 6938
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
unknown's avatar
unknown committed
6939 6940 6941
	| DYNAMIC_SYM		{}
	| END			{}
	| ENUM			{}
unknown's avatar
unknown committed
6942
	| ENGINE_SYM		{}
unknown's avatar
unknown committed
6943
	| ENGINES_SYM		{}
6944
	| ERRORS		{}
unknown's avatar
unknown committed
6945
	| ESCAPE_SYM		{}
unknown's avatar
unknown committed
6946
	| EVENTS_SYM		{}
unknown's avatar
unknown committed
6947
	| EXECUTE_SYM		{}
6948
        | EXPANSION_SYM         {}
unknown's avatar
unknown committed
6949
	| EXTENDED_SYM		{}
6950
	| FAST_SYM		{}
6951
	| FOUND_SYM		{}
unknown's avatar
unknown committed
6952 6953
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
unknown's avatar
unknown committed
6954
	| FULL			{}
unknown's avatar
unknown committed
6955 6956 6957 6958
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
	| FLUSH_SYM		{}
6959
	| FRAC_SECOND_SYM	{}
6960
	| GEOMETRY_SYM		{}
6961
	| GEOMETRYCOLLECTION	{}
6962
	| GET_FORMAT		{}
unknown's avatar
unknown committed
6963
	| GRANTS		{}
unknown's avatar
unknown committed
6964
	| GLOBAL_SYM		{}
6965
	| HANDLER_SYM		{}
unknown's avatar
unknown committed
6966
	| HASH_SYM		{}
6967
	| HELP_SYM		{}
unknown's avatar
unknown committed
6968 6969 6970
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
6971
	| INVOKER_SYM		{}
unknown's avatar
unknown committed
6972
	| IMPORT		{}
unknown's avatar
unknown committed
6973
	| INDEXES		{}
unknown's avatar
unknown committed
6974
	| ISOLATION		{}
unknown's avatar
unknown committed
6975
	| ISSUER_SYM		{}
unknown's avatar
unknown committed
6976
	| INNOBASE_SYM		{}
6977
	| INSERT_METHOD		{}
unknown's avatar
unknown committed
6978
	| RELAY_THREAD		{}
6979 6980
	| LABEL_SYM             {}
	| LANGUAGE_SYM          {}
6981
	| LAST_SYM		{}
unknown's avatar
unknown committed
6982
	| LEAVES                {}
unknown's avatar
unknown committed
6983
	| LEVEL_SYM		{}
6984
	| LINESTRING		{}
unknown's avatar
unknown committed
6985
	| LOCAL_SYM		{}
unknown's avatar
unknown committed
6986
	| LOCKS_SYM		{}
unknown's avatar
unknown committed
6987 6988 6989 6990 6991 6992 6993 6994 6995
	| 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	{}
unknown's avatar
unknown committed
6996
	| MASTER_SERVER_ID_SYM  {}
unknown's avatar
unknown committed
6997
	| MASTER_CONNECT_RETRY_SYM	{}
unknown's avatar
unknown committed
6998 6999 7000 7001 7002 7003
	| MASTER_SSL_SYM	{}
	| MASTER_SSL_CA_SYM	{}
	| MASTER_SSL_CAPATH_SYM	{}
	| MASTER_SSL_CERT_SYM	{}
	| MASTER_SSL_CIPHER_SYM	{}
	| MASTER_SSL_KEY_SYM	{}
unknown's avatar
unknown committed
7004 7005 7006
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
7007
	| MAX_USER_CONNECTIONS_SYM {}
7008
	| MEDIUM_SYM		{}
7009
	| MERGE_SYM		{}
unknown's avatar
unknown committed
7010
	| MICROSECOND_SYM	{}
unknown's avatar
unknown committed
7011 7012 7013
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
unknown's avatar
unknown committed
7014
	| MODE_SYM		{}
unknown's avatar
unknown committed
7015
	| MONTH_SYM		{}
7016 7017 7018
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
unknown's avatar
unknown committed
7019
  | MUTEX_SYM   {}
7020
	| NAME_SYM              {}
unknown's avatar
unknown committed
7021
	| NAMES_SYM		{}
unknown's avatar
unknown committed
7022 7023
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
unknown's avatar
unknown committed
7024
	| NDBCLUSTER_SYM	{}
7025
	| NEXT_SYM		{}
unknown's avatar
unknown committed
7026
	| NEW_SYM		{}
unknown's avatar
unknown committed
7027
	| NO_SYM		{}
7028
	| NONE_SYM		{}
unknown's avatar
unknown committed
7029
	| NVARCHAR_SYM		{}
unknown's avatar
unknown committed
7030
	| OFFSET_SYM		{}
unknown's avatar
unknown committed
7031
	| OLD_PASSWORD		{}
7032
	| ONE_SHOT_SYM		{}
7033
	| OPEN_SYM		{}
unknown's avatar
unknown committed
7034
	| PACK_KEYS_SYM		{}
7035
	| PARTIAL		{}
unknown's avatar
unknown committed
7036
	| PASSWORD		{}
7037
	| POINT_SYM		{}
7038
	| POLYGON		{}
unknown's avatar
unknown committed
7039
        | PREPARE_SYM           {}
7040
	| PREV_SYM		{}
7041
        | PRIVILEGES            {}
unknown's avatar
unknown committed
7042 7043
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
7044
	| QUARTER_SYM		{}
unknown's avatar
unknown committed
7045
	| QUERY_SYM		{}
7046
	| QUICK			{}
unknown's avatar
unknown committed
7047
	| RAID_0_SYM		{}
unknown's avatar
unknown committed
7048 7049
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
unknown's avatar
unknown committed
7050
	| RAID_STRIPED_SYM	{}
unknown's avatar
unknown committed
7051
	| RAID_TYPE		{}
7052
	| REDUNDANT_SYM		{}
unknown's avatar
unknown committed
7053 7054
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
unknown's avatar
unknown committed
7055 7056
	| RELOAD		{}
	| REPAIR		{}
unknown's avatar
unknown committed
7057
	| REPEATABLE_SYM	{}
unknown's avatar
unknown committed
7058
	| REPLICATION		{}
unknown's avatar
unknown committed
7059
	| RESET_SYM		{}
7060
	| RESOURCES		{}
unknown's avatar
unknown committed
7061
	| RESTORE_SYM		{}
7062
	| RETURNS_SYM           {}
unknown's avatar
unknown committed
7063
	| ROLLBACK_SYM		{}
7064
	| ROLLUP_SYM		{}
7065
	| ROUTINE_SYM		{}
unknown's avatar
unknown committed
7066 7067 7068
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
unknown's avatar
unknown committed
7069
	| RTREE_SYM		{}
7070
	| SAVEPOINT_SYM		{}
unknown's avatar
unknown committed
7071
	| SECOND_SYM		{}
7072
	| SECURITY_SYM		{}
7073
	| SERIAL_SYM		{}
unknown's avatar
unknown committed
7074 7075
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
unknown's avatar
unknown committed
7076
	| SIGNED_SYM		{}
7077
	| SIMPLE_SYM		{}
unknown's avatar
unknown committed
7078
	| SHARE_SYM		{}
unknown's avatar
unknown committed
7079
	| SHUTDOWN		{}
unknown's avatar
unknown committed
7080
	| SLAVE			{}
7081
	| SNAPSHOT_SYM		{}
unknown's avatar
unknown committed
7082
	| SOUNDS_SYM		{}
unknown's avatar
unknown committed
7083
	| SQL_CACHE_SYM		{}
unknown's avatar
unknown committed
7084
	| SQL_BUFFER_RESULT	{}
unknown's avatar
unknown committed
7085
	| SQL_NO_CACHE_SYM	{}
unknown's avatar
unknown committed
7086
	| SQL_THREAD		{}
unknown's avatar
unknown committed
7087 7088 7089
	| START_SYM		{}
	| STATUS_SYM		{}
	| STOP_SYM		{}
unknown's avatar
unknown committed
7090
	| STORAGE_SYM		{}
unknown's avatar
unknown committed
7091
	| STRING_SYM		{}
unknown's avatar
unknown committed
7092
	| SUBDATE_SYM		{}
unknown's avatar
unknown committed
7093
	| SUBJECT_SYM		{}
unknown's avatar
unknown committed
7094
	| SUPER_SYM		{}
7095
        | TABLES                {}
unknown's avatar
unknown committed
7096
	| TABLESPACE		{}
unknown's avatar
unknown committed
7097
	| TEMPORARY		{}
7098
	| TEMPTABLE_SYM		{}
unknown's avatar
unknown committed
7099
	| TEXT_SYM		{}
unknown's avatar
unknown committed
7100
	| TRANSACTION_SYM	{}
7101
	| TRUNCATE_SYM		{}
unknown's avatar
unknown committed
7102
	| TIMESTAMP		{}
7103 7104
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
unknown's avatar
unknown committed
7105 7106
	| TIME_SYM		{}
	| TYPE_SYM		{}
unknown's avatar
unknown committed
7107
	| TYPES_SYM		{}
unknown's avatar
unknown committed
7108
        | UDF_RETURNS_SYM       {}
7109
	| FUNCTION_SYM		{}
unknown's avatar
unknown committed
7110
	| UNCOMMITTED_SYM	{}
7111
	| UNDEFINED_SYM		{}
unknown's avatar
unknown committed
7112
	| UNICODE_SYM		{}
7113
	| UNKNOWN_SYM		{}
7114
	| UNTIL_SYM		{}
7115
	| USER			{}
unknown's avatar
unknown committed
7116
	| USE_FRM		{}
unknown's avatar
unknown committed
7117
	| VARIABLES		{}
7118
	| VIEW_SYM		{}
7119
	| VALUE_SYM		{}
7120
	| WARNINGS		{}
7121
	| WEEK_SYM		{}
unknown's avatar
unknown committed
7122
	| WORK_SYM		{}
7123
	| X509_SYM		{}
unknown's avatar
unknown committed
7124 7125
	| YEAR_SYM		{}
	;
unknown's avatar
unknown committed
7126 7127 7128 7129 7130 7131

/* Option functions */

set:
	SET opt_option
	{
unknown's avatar
unknown committed
7132 7133
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
unknown's avatar
unknown committed
7134
	  mysql_init_select(lex);
7135
	  lex->option_type=OPT_SESSION;
unknown's avatar
unknown committed
7136
	  lex->var_list.empty();
7137
          lex->one_shot_set= 0;
unknown's avatar
unknown committed
7138
	}
7139 7140 7141
	option_value_list
	{}
	;
unknown's avatar
unknown committed
7142 7143 7144

opt_option:
	/* empty */ {}
unknown's avatar
unknown committed
7145
	| OPTION {};
unknown's avatar
unknown committed
7146 7147

option_value_list:
unknown's avatar
unknown committed
7148 7149 7150 7151 7152 7153 7154 7155
	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; }
7156
	| ONE_SHOT_SYM	{ Lex->option_type= OPT_SESSION; Lex->one_shot_set= 1; }
unknown's avatar
unknown committed
7157 7158 7159 7160
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
unknown's avatar
unknown committed
7161
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
7162 7163 7164 7165 7166 7167
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

opt_var_ident_type:
	/* empty */		{ $$=OPT_DEFAULT; }
unknown's avatar
unknown committed
7168
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
7169 7170 7171
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
unknown's avatar
unknown committed
7172 7173

option_value:
7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189
	  '@' ident_or_text equal expr
	  {
            LEX *lex= Lex;

            if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
            {
              /*
                We have to use special instruction in functions and triggers
                because sp_instr_stmt will close all tables and thus ruin
                execution of statement invoking function or trigger.

                We also do not want to allow expression with subselects in
                this case.
              */
              if (lex->query_tables)
              {
unknown's avatar
unknown committed
7190 7191
                my_message(ER_SP_SUBSELECT_NYI, ER(ER_SP_SUBSELECT_NYI),
                           MYF(0));
7192 7193 7194 7195 7196 7197 7198 7199 7200 7201
                YYABORT;
              }
              sp_instr_set_user_var *i= 
                new sp_instr_set_user_var(lex->sphead->instructions(),
                                          lex->spcont, $2, $4);
	      lex->sphead->add_instr(i);
            }
            else
              lex->var_list.push_back(new set_var_user(new Item_func_set_user_var($2,$4)));
	  }
unknown's avatar
unknown committed
7202
	| internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
7203
	  {
unknown's avatar
unknown committed
7204
	    LEX *lex=Lex;
7205 7206 7207 7208 7209 7210 7211 7212

            if ($1.var == &trg_new_row_fake_var)
            {
              /* We are in trigger and assigning value to field of new row */
              Item *it;
              sp_instr_set_trigger_field *i;
              if (lex->query_tables)
              {
unknown's avatar
unknown committed
7213 7214
                my_message(ER_SP_SUBSELECT_NYI, ER(ER_SP_SUBSELECT_NYI),
                           MYF(0));
7215 7216 7217 7218 7219 7220 7221 7222 7223
                YYABORT;
              }
              if ($3)
                it= $3;
              else
              {
                /* QQ: Shouldn't this be field's default value ? */
                it= new Item_null();
              }
7224 7225 7226 7227
              
              if (!(i= new sp_instr_set_trigger_field(
                             lex->sphead->instructions(), lex->spcont,
                             $1.base_name, it)))
7228
                YYABORT;
7229 7230 7231 7232 7233 7234 7235 7236
              
              /*
                Let us add this item to list of all Item_trigger_field
                objects in trigger.
              */
              lex->trg_table_fields.link_in_list((byte *)&i->trigger_field,
                     (byte **)&i->trigger_field.next_trg_field);

7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266
              lex->sphead->add_instr(i);
            }
            else if ($1.var)
	    { /* System variable */
	      lex->var_list.push_back(new set_var(lex->option_type, $1.var,
						  &$1.base_name, $3));
	    }
            else
	    {
	      /* An SP local variable */
	      sp_pcontext *ctx= lex->spcont;
	      sp_pvar_t *spv;
              sp_instr_set *i;
	      Item *it;

	      spv= ctx->find_pvar(&$1.base_name);

	      if ($3)
	        it= $3;
	      else if (spv->dflt)
	        it= spv->dflt;
	      else
	        it= new Item_null();
              i= new sp_instr_set(lex->sphead->instructions(), ctx,
	                          spv->offset, it, spv->type);
	      i->tables= lex->query_tables;
	      lex->query_tables= 0;
	      lex->sphead->add_instr(i);
	      spv->isset= TRUE;
	    }
unknown's avatar
unknown committed
7267
	  }
unknown's avatar
unknown committed
7268
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
7269 7270
	  {
	    LEX *lex=Lex;
unknown's avatar
unknown committed
7271 7272
	    lex->var_list.push_back(new set_var((enum_var_type) $3, $4.var,
						&$4.base_name, $6));
unknown's avatar
unknown committed
7273 7274 7275 7276
	  }
	| TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	  {
	    LEX *lex=Lex;
unknown's avatar
unknown committed
7277 7278 7279
	    LEX_STRING tmp;
	    tmp.str=0;
	    tmp.length=0;
unknown's avatar
unknown committed
7280
	    lex->var_list.push_back(new set_var(lex->option_type,
unknown's avatar
unknown committed
7281
						find_sys_var("tx_isolation"),
unknown's avatar
unknown committed
7282
						&tmp,
unknown's avatar
unknown committed
7283
						new Item_int((int32) $4)));
unknown's avatar
unknown committed
7284
	  }
7285
	| charset old_or_new_charset_name_or_default
unknown's avatar
unknown committed
7286
	{
unknown's avatar
unknown committed
7287
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7288
	  LEX *lex= Lex;
7289
	  $2= $2 ? $2: global_system_variables.character_set_client;
7290
	  lex->var_list.push_back(new set_var_collation_client($2,thd->variables.collation_database,$2));
unknown's avatar
unknown committed
7291
	}
7292
	| NAMES_SYM charset_name_or_default opt_collate
7293
	{
unknown's avatar
unknown committed
7294
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7295
	  LEX *lex= Lex;
7296
	  $2= $2 ? $2 : global_system_variables.character_set_client;
7297 7298
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
7299
	  {
7300 7301
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
unknown's avatar
unknown committed
7302
	    YYABORT;
7303
	  }
7304
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
7305
	}
unknown's avatar
unknown committed
7306
	| PASSWORD equal text_or_password
unknown's avatar
unknown committed
7307
	  {
7308
	    THD *thd=YYTHD;
unknown's avatar
unknown committed
7309
	    LEX_USER *user;
unknown's avatar
unknown committed
7310
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
unknown's avatar
unknown committed
7311 7312 7313
	      YYABORT;
	    user->host.str=0;
	    user->user.str=thd->priv_user;
7314
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
unknown's avatar
unknown committed
7315 7316
	  }
	| PASSWORD FOR_SYM user equal text_or_password
unknown's avatar
unknown committed
7317
	  {
unknown's avatar
unknown committed
7318 7319
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
7320
	;
unknown's avatar
unknown committed
7321

unknown's avatar
unknown committed
7322 7323 7324
internal_variable_name:
	ident
	{
7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342
	  LEX *lex= Lex;
          sp_pcontext *spc= lex->spcont;
	  sp_pvar_t *spv;

	  /* We have to lookup here since local vars can shadow sysvars */
	  if (!spc || !(spv = spc->find_pvar(&$1)))
	  {
            /* Not an SP local variable */
	    sys_var *tmp=find_sys_var($1.str, $1.length);
	    if (!tmp)
	      YYABORT;
	    $$.var= tmp;
	    $$.base_name.str=0;
	    $$.base_name.length=0;
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
7343 7344 7345
            if (tmp == &sys_time_zone &&
                lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
7346 7347 7348 7349 7350 7351 7352
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
unknown's avatar
unknown committed
7353
	}
7354 7355
	| ident '.' ident
	  {
7356
            LEX *lex= Lex;
7357 7358
            if (check_reserved_words(&$1))
            {
7359
	      yyerror(ER(ER_SYNTAX_ERROR));
7360 7361
              YYABORT;
            }
7362 7363 7364 7365 7366 7367
            if (lex->sphead && lex->sphead->m_type == TYPE_ENUM_TRIGGER &&
                (!my_strcasecmp(system_charset_info, $1.str, "NEW") || 
                 !my_strcasecmp(system_charset_info, $1.str, "OLD")))
            {
              if ($1.str[0]=='O' || $1.str[0]=='o')
              {
unknown's avatar
unknown committed
7368
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
7369 7370 7371 7372
                YYABORT;
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
unknown's avatar
unknown committed
7373 7374
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
7375 7376 7377 7378
                YYABORT;
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
unknown's avatar
unknown committed
7379
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391
                YYABORT;
              }
              /* This special combination will denote field of NEW row */
              $$.var= &trg_new_row_fake_var;
              $$.base_name= $3;
            }
            else
            {
              sys_var *tmp=find_sys_var($3.str, $3.length);
              if (!tmp)
                YYABORT;
              if (!tmp->is_struct())
7392
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
7393 7394 7395
              $$.var= tmp;
              $$.base_name= $1;
            }
7396 7397 7398 7399 7400 7401 7402
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
7403
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
unknown's avatar
unknown committed
7404 7405 7406
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
7407
	  }
7408
        ;
unknown's avatar
unknown committed
7409 7410 7411 7412 7413 7414 7415

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

unknown's avatar
unknown committed
7417 7418 7419 7420
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
7421
	    $$= $3.length ? YYTHD->variables.old_passwords ?
unknown's avatar
unknown committed
7422 7423 7424 7425 7426 7427 7428 7429
	        Item_func_old_password::alloc(YYTHD, $3.str) :
	        Item_func_password::alloc(YYTHD, $3.str) :
	      $3.str;
	  }
	| OLD_PASSWORD '(' TEXT_STRING ')'
	  {
	    $$= $3.length ? Item_func_old_password::alloc(YYTHD, $3.str) :
	      $3.str;
7430 7431
	  }
          ;
unknown's avatar
unknown committed
7432

unknown's avatar
unknown committed
7433

unknown's avatar
unknown committed
7434
set_expr_or_default:
unknown's avatar
unknown committed
7435 7436
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
7437 7438
	| ON	  { $$=new Item_string("ON",  2, system_charset_info); }
	| ALL	  { $$=new Item_string("ALL", 3, system_charset_info); }
unknown's avatar
unknown committed
7439
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
unknown's avatar
unknown committed
7440
	;
unknown's avatar
unknown committed
7441 7442


unknown's avatar
unknown committed
7443 7444 7445 7446 7447 7448 7449
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
	  Lex->sql_command=SQLCOM_LOCK_TABLES;
	}
7450 7451
	table_lock_list
	{}
7452
	;
unknown's avatar
unknown committed
7453 7454 7455

table_or_tables:
	TABLE_SYM
unknown's avatar
unknown committed
7456
	| TABLES;
unknown's avatar
unknown committed
7457 7458 7459

table_lock_list:
	table_lock
unknown's avatar
unknown committed
7460
	| table_lock_list ',' table_lock;
unknown's avatar
unknown committed
7461 7462 7463

table_lock:
	table_ident opt_table_alias lock_option
7464
	{
unknown's avatar
unknown committed
7465
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
7466 7467
	   YYABORT;
	}
7468
        ;
unknown's avatar
unknown committed
7469 7470 7471

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
7472
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
unknown's avatar
unknown committed
7473
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
7474 7475
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
unknown's avatar
unknown committed
7476 7477

unlock:
7478 7479
	UNLOCK_SYM table_or_tables { Lex->sql_command=SQLCOM_UNLOCK_TABLES; }
        ;
unknown's avatar
unknown committed
7480 7481


7482
/*
unknown's avatar
unknown committed
7483
** Handler: direct access to ISAM functions
7484 7485 7486 7487 7488
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
7489 7490
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_OPEN;
unknown's avatar
unknown committed
7491
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
7492 7493
	    YYABORT;
	}
unknown's avatar
unknown committed
7494
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
7495
	{
7496 7497
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_CLOSE;
unknown's avatar
unknown committed
7498
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
7499 7500
	    YYABORT;
	}
unknown's avatar
unknown committed
7501
	| HANDLER_SYM table_ident_nodb READ_SYM
7502
	{
unknown's avatar
unknown committed
7503 7504 7505
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
7506 7507
	  lex->current_select->select_limit= 1;
	  lex->current_select->offset_limit= 0L;
unknown's avatar
unknown committed
7508
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
7509
	    YYABORT;
unknown's avatar
unknown committed
7510
        }
7511
        handler_read_or_scan where_clause opt_limit_clause {}
7512
        ;
7513

unknown's avatar
unknown committed
7514 7515
handler_read_or_scan:
	handler_scan_function         { Lex->backup_dir= 0; }
7516 7517
        | ident handler_rkey_function { Lex->backup_dir= $1.str; }
        ;
unknown's avatar
unknown committed
7518 7519 7520

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
7521 7522
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
unknown's avatar
unknown committed
7523 7524 7525 7526 7527 7528

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;  }
7529 7530
	| handler_rkey_mode
	{
unknown's avatar
unknown committed
7531 7532
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
unknown's avatar
unknown committed
7533
	  lex->ha_rkey_mode=$1;
unknown's avatar
unknown committed
7534
	  if (!(lex->insert_list = new List_item))
7535
	    YYABORT;
7536 7537
	} '(' values ')' { }
        ;
7538 7539

handler_rkey_mode:
unknown's avatar
unknown committed
7540 7541 7542 7543
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
7544 7545
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
7546

unknown's avatar
unknown committed
7547 7548 7549
/* GRANT / REVOKE */

revoke:
7550
	REVOKE clear_privileges revoke_command
7551 7552 7553 7554
	{}
        ;

revoke_command:
7555 7556 7557 7558
	grant_privileges ON opt_table FROM grant_list
	{
	  Lex->sql_command = SQLCOM_REVOKE;
        }
7559
	|
7560
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
7561 7562 7563
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
7564
	;
unknown's avatar
unknown committed
7565 7566

grant:
7567
	GRANT clear_privileges grant_privileges ON opt_table TO_SYM grant_list
7568
	require_clause grant_options
7569
	{ Lex->sql_command= SQLCOM_GRANT; }
7570
	;
unknown's avatar
unknown committed
7571 7572

grant_privileges:
7573 7574 7575 7576 7577 7578
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
7579
        ;
unknown's avatar
unknown committed
7580

unknown's avatar
unknown committed
7581 7582 7583 7584 7585
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

7586 7587 7588
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
unknown's avatar
unknown committed
7589

7590
object_privilege:
unknown's avatar
unknown committed
7591
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
7592 7593 7594
	| 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
7595 7596
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
unknown's avatar
unknown committed
7597
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
unknown's avatar
unknown committed
7598 7599 7600
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
unknown's avatar
unknown committed
7601
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
unknown's avatar
unknown committed
7602 7603 7604 7605
	| 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
7606 7607 7608 7609 7610
	| 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; }
7611 7612 7613 7614
	| REPLICATION SLAVE  { Lex->grant |= REPL_SLAVE_ACL; }
	| REPLICATION CLIENT_SYM { Lex->grant |= REPL_CLIENT_ACL; }
	| CREATE VIEW_SYM { Lex->grant |= CREATE_VIEW_ACL; }
	| SHOW VIEW_SYM { Lex->grant |= SHOW_VIEW_ACL; }
7615 7616
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
unknown's avatar
unknown committed
7617
	;
unknown's avatar
unknown committed
7618

unknown's avatar
unknown committed
7619

7620 7621
opt_and:
	/* empty */	{}
unknown's avatar
unknown committed
7622
	| AND_SYM	{}
7623 7624 7625 7626 7627 7628 7629 7630 7631
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
unknown's avatar
unknown committed
7632 7633 7634 7635
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
unknown's avatar
unknown committed
7636
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
unknown's avatar
unknown committed
7637 7638 7639 7640 7641 7642 7643 7644 7645
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
unknown's avatar
unknown committed
7646
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
unknown's avatar
unknown committed
7647 7648 7649 7650 7651 7652 7653 7654 7655
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
unknown's avatar
unknown committed
7656
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
unknown's avatar
unknown committed
7657 7658 7659 7660 7661
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
7662

unknown's avatar
unknown committed
7663 7664 7665
opt_table:
	'*'
	  {
7666
	    LEX *lex= Lex;
unknown's avatar
unknown committed
7667
	    lex->current_select->db= lex->thd->db;
unknown's avatar
unknown committed
7668
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
7669 7670
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
7671
	    {
unknown's avatar
unknown committed
7672 7673
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
7674
	      YYABORT;
unknown's avatar
unknown committed
7675
	    }
unknown's avatar
unknown committed
7676 7677 7678
	  }
	| ident '.' '*'
	  {
7679
	    LEX *lex= Lex;
unknown's avatar
unknown committed
7680
	    lex->current_select->db = $1.str;
unknown's avatar
unknown committed
7681
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
7682 7683
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
7684
	    {
unknown's avatar
unknown committed
7685 7686
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
7687 7688 7689 7690 7691
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
7692
	    LEX *lex= Lex;
unknown's avatar
unknown committed
7693
	    lex->current_select->db = NULL;
unknown's avatar
unknown committed
7694 7695
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
unknown's avatar
unknown committed
7696
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
7697
	    {
unknown's avatar
unknown committed
7698 7699
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
7700 7701 7702 7703 7704
	      YYABORT;
	    }
	  }
	| table_ident
	  {
unknown's avatar
unknown committed
7705
	    LEX *lex=Lex;
unknown's avatar
unknown committed
7706
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
unknown's avatar
unknown committed
7707
	      YYABORT;
unknown's avatar
unknown committed
7708
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
7709
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
7710 7711
	  }
          ;
unknown's avatar
unknown committed
7712 7713 7714


user_list:
7715 7716 7717 7718 7719 7720 7721 7722 7723 7724
	user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;


grant_list:
7725
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
7726
	| grant_list ',' grant_user
7727 7728 7729 7730 7731
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
unknown's avatar
unknown committed
7732 7733 7734 7735 7736 7737 7738 7739


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
7740
             if (YYTHD->variables.old_passwords)
7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757
             {
               char *buff= 
                 (char *) YYTHD->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH_323+1);
               if (buff)
                 make_scrambled_password_323(buff, $4.str);
               $1->password.str= buff;
               $1->password.length= SCRAMBLED_PASSWORD_CHAR_LENGTH_323;
             }
             else
             {
               char *buff= 
                 (char *) YYTHD->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH+1);
               if (buff)
                 make_scrambled_password(buff, $4.str);
               $1->password.str= buff;
               $1->password.length= SCRAMBLED_PASSWORD_CHAR_LENGTH;
             }
unknown's avatar
unknown committed
7758 7759 7760 7761 7762
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
	  { $$=$1; $1->password=$5 ; }
	| user
7763 7764
	  { $$=$1; $1->password.str=NullS; }
        ;
unknown's avatar
unknown committed
7765 7766 7767


opt_column_list:
unknown's avatar
unknown committed
7768 7769 7770 7771 7772
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
unknown's avatar
unknown committed
7773
	| '(' column_list ')';
unknown's avatar
unknown committed
7774 7775 7776

column_list:
	column_list ',' column_list_id
unknown's avatar
unknown committed
7777
	| column_list_id;
unknown's avatar
unknown committed
7778 7779 7780 7781

column_list_id:
	ident
	{
unknown's avatar
unknown committed
7782
	  String *new_str = new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info);
unknown's avatar
unknown committed
7783 7784
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
unknown's avatar
unknown committed
7785
	  LEX *lex=Lex;
unknown's avatar
unknown committed
7786 7787
	  while ((point=iter++))
	  {
7788 7789
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
unknown's avatar
unknown committed
7790 7791
		break;
	  }
unknown's avatar
unknown committed
7792
	  lex->grant_tot_col|= lex->which_columns;
unknown's avatar
unknown committed
7793
	  if (point)
unknown's avatar
unknown committed
7794
	    point->rights |= lex->which_columns;
unknown's avatar
unknown committed
7795
	  else
unknown's avatar
unknown committed
7796
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
7797 7798
	}
        ;
unknown's avatar
unknown committed
7799

unknown's avatar
unknown committed
7800 7801

require_clause: /* empty */
7802
        | REQUIRE_SYM require_list
7803 7804 7805
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
7806
        | REQUIRE_SYM SSL_SYM
7807 7808 7809
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
7810
        | REQUIRE_SYM X509_SYM
7811 7812 7813 7814 7815 7816 7817
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
7818
          ;
unknown's avatar
unknown committed
7819

unknown's avatar
unknown committed
7820
grant_options:
unknown's avatar
unknown committed
7821
	/* empty */ {}
unknown's avatar
unknown committed
7822
	| WITH grant_option_list;
unknown's avatar
unknown committed
7823

unknown's avatar
unknown committed
7824 7825
grant_option_list:
	grant_option_list grant_option {}
7826 7827
	| grant_option {}
        ;
unknown's avatar
unknown committed
7828 7829 7830

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
7831
        | MAX_QUERIES_PER_HOUR ULONG_NUM
unknown's avatar
unknown committed
7832
        {
7833
	  Lex->mqh.questions=$2;
7834
	  Lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
7835
	}
7836
        | MAX_UPDATES_PER_HOUR ULONG_NUM
7837
        {
7838
	  Lex->mqh.updates=$2;
7839
	  Lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
7840
	}
7841
        | MAX_CONNECTIONS_PER_HOUR ULONG_NUM
7842
        {
7843 7844
	  Lex->mqh.conn_per_hour= $2;
	  Lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
7845
	}
7846 7847 7848 7849 7850
        | MAX_USER_CONNECTIONS_SYM ULONG_NUM
        {
          Lex->mqh.user_conn= $2;
          Lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
        }
7851
        ;
7852

unknown's avatar
unknown committed
7853
begin:
7854
	BEGIN_SYM   { Lex->sql_command = SQLCOM_BEGIN; Lex->start_transaction_opt= 0;} opt_work {}
7855
	;
unknown's avatar
unknown committed
7856 7857 7858

opt_work:
	/* empty */ {}
7859 7860
	| WORK_SYM {;}
        ;
unknown's avatar
unknown committed
7861

unknown's avatar
unknown committed
7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882
opt_chain:
	/* empty */ { $$= (Lex->thd->variables.completion_type == 1); }
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
	/* empty */ { $$= (Lex->thd->variables.completion_type == 2); }
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_work_and_chain:
	opt_work opt_chain 		{ $$=$2; }
	;

opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

unknown's avatar
unknown committed
7883
commit:
unknown's avatar
unknown committed
7884 7885 7886 7887 7888 7889 7890
	COMMIT_SYM opt_work_and_chain opt_release
	{
	  Lex->sql_command= SQLCOM_COMMIT;
	  Lex->tx_chain= $2; 
	  Lex->tx_release= $3;
	}
	;
unknown's avatar
unknown committed
7891 7892

rollback:
unknown's avatar
unknown committed
7893 7894 7895 7896 7897
	ROLLBACK_SYM opt_work_and_chain opt_release
	{ 
	  Lex->sql_command= SQLCOM_ROLLBACK;
	  Lex->tx_chain= $2; 
	  Lex->tx_release= $3;
7898
	}
unknown's avatar
unknown committed
7899 7900
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
7901
	{
unknown's avatar
unknown committed
7902
	  Lex->sql_command = SQLCOM_ROLLBACK_TO_SAVEPOINT;
unknown's avatar
unknown committed
7903 7904 7905 7906
	  Lex->savepoint_name = $5.str;
	}
	;

7907 7908 7909 7910 7911
savepoint:
	SAVEPOINT_SYM ident
	{
	  Lex->sql_command = SQLCOM_SAVEPOINT;
	  Lex->savepoint_name = $2.str;
unknown's avatar
unknown committed
7912 7913 7914 7915 7916 7917 7918 7919 7920 7921
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
	  Lex->sql_command = SQLCOM_RELEASE_SAVEPOINT;
	  Lex->savepoint_name = $3.str;
	}
	;
7922 7923

/*
unknown's avatar
unknown committed
7924
   UNIONS : glue selects together
7925 7926 7927
*/


7928
union_clause:
7929
	/* empty */ {}
unknown's avatar
unknown committed
7930 7931
	| union_list
	;
7932 7933

union_list:
7934
	UNION_SYM union_option
7935 7936 7937 7938 7939
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
unknown's avatar
unknown committed
7940
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
7941
	    YYABORT;
7942
	  }
7943
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
7944
	  {
7945
            yyerror(ER(ER_SYNTAX_ERROR));
7946 7947
	    YYABORT;
	  }
7948
	  if (mysql_new_select(lex, 0))
7949
	    YYABORT;
unknown's avatar
unknown committed
7950
          mysql_init_select(lex);
7951
	  lex->current_select->linkage=UNION_TYPE;
7952 7953 7954
          if ($2) /* UNION DISTINCT - remember position */
            lex->current_select->master_unit()->union_distinct=
                                                      lex->current_select;
unknown's avatar
unknown committed
7955
	}
7956
	select_init {}
7957
	;
7958 7959

union_opt:
unknown's avatar
unknown committed
7960
	union_list {}
unknown's avatar
unknown committed
7961 7962
	| optional_order_or_limit {}
	;
7963 7964

optional_order_or_limit:
7965
	/* Empty */ {}
7966 7967
	|
	  {
unknown's avatar
unknown committed
7968
	    THD *thd= YYTHD;
7969
	    LEX *lex= thd->lex;
unknown's avatar
unknown committed
7970
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
unknown's avatar
unknown committed
7971
	    SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
7972
	    SELECT_LEX_UNIT *unit= sel->master_unit();
unknown's avatar
unknown committed
7973
	    SELECT_LEX *fake= unit->fake_select_lex;
unknown's avatar
unknown committed
7974 7975 7976 7977 7978 7979
	    if (fake)
	    {
	      unit->global_parameters= fake;
	      fake->no_table_names_allowed= 1;
	      lex->current_select= fake;
	    }
unknown's avatar
unknown committed
7980
	    thd->where= "global ORDER clause";
7981
	  }
7982
	order_or_limit
unknown's avatar
unknown committed
7983 7984
          {
	    THD *thd= YYTHD;
7985
	    thd->lex->current_select->no_table_names_allowed= 0;
unknown's avatar
unknown committed
7986 7987
	    thd->where= "";
          }
7988 7989 7990
	;

order_or_limit:
7991 7992
	order_clause opt_limit_clause_init
	| limit_clause
7993
	;
unknown's avatar
unknown committed
7994 7995

union_option:
7996 7997 7998 7999
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
8000

unknown's avatar
unknown committed
8001 8002
singlerow_subselect:
	subselect_start singlerow_subselect_init
unknown's avatar
unknown committed
8003 8004 8005 8006
	subselect_end
	{
	  $$= $2;
	};
8007

unknown's avatar
unknown committed
8008
singlerow_subselect_init:
8009
	select_init2
unknown's avatar
unknown committed
8010
	{
8011 8012
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
unknown's avatar
unknown committed
8013
	};
unknown's avatar
unknown committed
8014 8015

exists_subselect:
unknown's avatar
unknown committed
8016 8017 8018 8019 8020
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
unknown's avatar
unknown committed
8021 8022

exists_subselect_init:
8023
	select_init2
unknown's avatar
unknown committed
8024
	{
8025 8026
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
unknown's avatar
unknown committed
8027
	};
8028

unknown's avatar
unknown committed
8029 8030 8031 8032 8033 8034 8035 8036
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
8037
  select_init2
unknown's avatar
unknown committed
8038
  {
unknown's avatar
unknown committed
8039
    $$= Lex->current_select->master_unit()->first_select();
unknown's avatar
unknown committed
8040 8041
  };

8042
subselect_start:
8043
	'(' SELECT_SYM
unknown's avatar
unknown committed
8044
	{
8045
	  LEX *lex=Lex;
8046
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
8047 8048
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
8049 8050
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
8051 8052
	    YYABORT;
	  }
unknown's avatar
unknown committed
8053 8054 8055
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
8056 8057

subselect_end:
unknown's avatar
unknown committed
8058 8059 8060
	')'
	{
	  LEX *lex=Lex;
8061
	  lex->current_select = lex->current_select->return_after_parsing();
unknown's avatar
unknown committed
8062
	};
unknown's avatar
unknown committed
8063

8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107
opt_view_list:
	/* empty */ {}
	| '(' view_list ')'
	;

view_list:
	ident 
	  {
	    Lex->view_list.push_back((LEX_STRING*)
				     sql_memdup(&$1, sizeof(LEX_STRING)));
	  }
	| view_list ',' ident
	  {
	    Lex->view_list.push_back((LEX_STRING*)
				     sql_memdup(&$3, sizeof(LEX_STRING)));
	  }
	;

or_replace:
	/* empty */	 { Lex->create_view_mode= VIEW_CREATE_NEW; }
	| OR_SYM REPLACE { Lex->create_view_mode= VIEW_CREATE_OR_REPLACE; }
	;

algorithm:
	/* empty */
	  { Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED; }
	| ALGORITHM_SYM EQ UNDEFINED_SYM
	  { Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED; }
	| ALGORITHM_SYM EQ MERGE_SYM
	  { Lex->create_view_algorithm= VIEW_ALGORITHM_MERGE; }
	| ALGORITHM_SYM EQ TEMPTABLE_SYM
	  { Lex->create_view_algorithm= VIEW_ALGORITHM_TMPTABLE; }
	;
check_option:
        /* empty */
          { Lex->create_view_check= VIEW_CHECK_NONE; }
        | WITH CHECK_SYM OPTION
          { Lex->create_view_check= VIEW_CHECK_CASCADED; }
        | WITH CASCADED CHECK_SYM OPTION
          { Lex->create_view_check= VIEW_CHECK_CASCADED; }
        | WITH LOCAL_SYM CHECK_SYM OPTION
          { Lex->create_view_check= VIEW_CHECK_LOCAL; }
        ;