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

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

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

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

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

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

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

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

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

unknown's avatar
unknown committed
49 50 51 52 53
#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)); 

54
inline Item *or_or_concat(THD *thd, Item* A, Item* B)
unknown's avatar
unknown committed
55
{
56
  return (thd->variables.sql_mode & MODE_PIPES_AS_CONCAT ?
unknown's avatar
unknown committed
57 58 59 60 61 62 63
          (Item*) new Item_func_concat(A,B) : (Item*) new Item_cond_or(A,B));
}

%}
%union {
  int  num;
  ulong ulong_num;
unknown's avatar
unknown committed
64
  ulonglong ulonglong_number;
unknown's avatar
unknown committed
65 66 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;
  List<Item> *item_list;
  List<String> *string_list;
unknown's avatar
unknown committed
73 74 75 76 77
  String *string;
  key_part_spec *key_part;
  TABLE_LIST *table_list;
  udf_func *udf;
  LEX_USER *lex_user;
unknown's avatar
unknown committed
78
  struct sys_var_with_base variable;
unknown's avatar
unknown committed
79
  Key::Keytype key_type;
unknown's avatar
unknown committed
80
  enum ha_key_alg key_alg;
unknown's avatar
unknown committed
81 82
  enum db_type db_type;
  enum row_type row_type;
unknown's avatar
unknown committed
83
  enum ha_rkey_function ha_rkey_mode;
unknown's avatar
unknown committed
84
  enum enum_tx_isolation tx_isolation;
85
  enum Cast_target cast_type;
unknown's avatar
unknown committed
86
  enum Item_udftype udf_type;
87
  CHARSET_INFO *charset;
unknown's avatar
unknown committed
88
  thr_lock_type lock_type;
89
  interval_type interval, interval_time_st;
90
  timestamp_type date_time_type;
unknown's avatar
unknown committed
91
  st_select_lex *select_lex;
unknown's avatar
unknown committed
92
  chooser_compare_func_creator boolfunc2creator;
93
  struct sp_cond_type *spcondtype;
94 95
  struct { int vars, conds, hndlrs, curs; } spblock;
  struct st_lex *lex;
unknown's avatar
unknown committed
96 97 98 99 100 101 102 103 104 105
}

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

%pure_parser					/* We have threads */

%token	END_OF_INPUT

106 107 108 109 110 111
%token CLOSE_SYM
%token HANDLER_SYM
%token LAST_SYM
%token NEXT_SYM
%token PREV_SYM

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

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

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

unknown's avatar
unknown committed
437 438 439
%token  ERRORS
%token  WARNINGS

unknown's avatar
unknown committed
440
%token	ASCII_SYM
unknown's avatar
unknown committed
441 442 443
%token	BIGINT
%token	BLOB_SYM
%token	CHAR_SYM
unknown's avatar
unknown committed
444
%token	CHANGED
unknown's avatar
unknown committed
445 446 447 448 449 450
%token	COALESCE
%token	DATETIME
%token	DATE_SYM
%token	DECIMAL_SYM
%token	DOUBLE_SYM
%token	ENUM
451
%token	FAST_SYM
unknown's avatar
unknown committed
452
%token	FLOAT_SYM
unknown's avatar
unknown committed
453
%token  GEOMETRY_SYM
unknown's avatar
unknown committed
454 455 456 457 458 459 460 461 462
%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
463
%token	QUICK
unknown's avatar
unknown committed
464
%token	REAL
unknown's avatar
unknown committed
465
%token	SIGNED_SYM
unknown's avatar
unknown committed
466 467 468 469
%token	SMALLINT
%token	STRING_SYM
%token	TEXT_SYM
%token	TIMESTAMP
470 471
%token	TIMESTAMP_ADD
%token	TIMESTAMP_DIFF
unknown's avatar
unknown committed
472 473 474 475
%token	TIME_SYM
%token	TINYBLOB
%token	TINYINT
%token	TINYTEXT
unknown's avatar
unknown committed
476
%token	ULONGLONG_NUM
unknown's avatar
unknown committed
477
%token	UNSIGNED
478 479 480
%token	VARBINARY
%token	VARCHAR
%token	VARYING
unknown's avatar
unknown committed
481 482
%token	ZEROFILL

unknown's avatar
unknown committed
483
%token	ADDDATE_SYM
unknown's avatar
unknown committed
484
%token	AGAINST
unknown's avatar
unknown committed
485 486 487 488
%token	ATAN
%token	BETWEEN_SYM
%token	BIT_AND
%token	BIT_OR
489
%token  BIT_XOR
unknown's avatar
unknown committed
490 491
%token	CASE_SYM
%token	CONCAT
unknown's avatar
unknown committed
492
%token	CONCAT_WS
unknown's avatar
unknown committed
493 494 495 496 497 498
%token	CURDATE
%token	CURTIME
%token	DATABASE
%token	DATE_ADD_INTERVAL
%token	DATE_SUB_INTERVAL
%token	DAY_HOUR_SYM
unknown's avatar
unknown committed
499
%token	DAY_MICROSECOND_SYM
unknown's avatar
unknown committed
500 501 502 503
%token	DAY_MINUTE_SYM
%token	DAY_SECOND_SYM
%token	DAY_SYM
%token	DECODE_SYM
unknown's avatar
unknown committed
504 505
%token	DES_ENCRYPT_SYM
%token	DES_DECRYPT_SYM
unknown's avatar
unknown committed
506 507 508
%token	ELSE
%token	ELT_FUNC
%token	ENCODE_SYM
unknown's avatar
unknown committed
509
%token	ENGINE_SYM
unknown's avatar
unknown committed
510
%token	ENGINES_SYM
unknown's avatar
unknown committed
511 512 513 514 515 516
%token	ENCRYPT
%token	EXPORT_SET
%token	EXTRACT_SYM
%token	FIELD_FUNC
%token	FORMAT_SYM
%token	FOR_SYM
517
%token  FRAC_SECOND_SYM
unknown's avatar
unknown committed
518
%token	FROM_UNIXTIME
unknown's avatar
unknown committed
519 520
%token	GEOMCOLLFROMTEXT
%token	GEOMFROMTEXT
unknown's avatar
unknown committed
521
%token	GEOMFROMWKB
unknown's avatar
unknown committed
522
%token  GEOMETRYCOLLECTION
523
%token  GROUP_CONCAT_SYM
unknown's avatar
unknown committed
524
%token	GROUP_UNIQUE_USERS
525
%token  GET_FORMAT
unknown's avatar
unknown committed
526
%token	HOUR_MICROSECOND_SYM
unknown's avatar
unknown committed
527 528 529 530 531
%token	HOUR_MINUTE_SYM
%token	HOUR_SECOND_SYM
%token	HOUR_SYM
%token	IDENTIFIED_SYM
%token	IF
532
%token	INSERT_METHOD
unknown's avatar
unknown committed
533 534 535
%token	INTERVAL_SYM
%token	LAST_INSERT_ID
%token	LEFT
unknown's avatar
unknown committed
536
%token	LINEFROMTEXT
unknown's avatar
unknown committed
537
%token  LINESTRING
unknown's avatar
unknown committed
538 539
%token	LOCATE
%token	MAKE_SET_SYM
540
%token	MASTER_POS_WAIT
unknown's avatar
unknown committed
541 542
%token  MICROSECOND_SYM
%token	MINUTE_MICROSECOND_SYM
unknown's avatar
unknown committed
543 544
%token	MINUTE_SECOND_SYM
%token	MINUTE_SYM
unknown's avatar
unknown committed
545
%token	MODE_SYM
unknown's avatar
unknown committed
546 547
%token	MODIFY_SYM
%token	MONTH_SYM
unknown's avatar
unknown committed
548 549 550
%token	MLINEFROMTEXT
%token	MPOINTFROMTEXT
%token	MPOLYFROMTEXT
unknown's avatar
unknown committed
551 552 553
%token  MULTILINESTRING
%token  MULTIPOINT
%token  MULTIPOLYGON
unknown's avatar
unknown committed
554
%token	NOW_SYM
unknown's avatar
unknown committed
555
%token	OLD_PASSWORD
unknown's avatar
unknown committed
556
%token	PASSWORD
unknown's avatar
unknown committed
557
%token	POINTFROMTEXT
558
%token	POINT_SYM
unknown's avatar
unknown committed
559
%token	POLYFROMTEXT
unknown's avatar
unknown committed
560
%token  POLYGON
unknown's avatar
unknown committed
561 562
%token	POSITION_SYM
%token	PROCEDURE
563
%token	QUARTER_SYM
unknown's avatar
unknown committed
564 565 566 567 568
%token	RAND
%token	REPLACE
%token	RIGHT
%token	ROUND
%token	SECOND_SYM
unknown's avatar
unknown committed
569
%token	SECOND_MICROSECOND_SYM
unknown's avatar
unknown committed
570
%token	SHARE_SYM
571
%token  SP_FUNC
unknown's avatar
unknown committed
572
%token	SUBDATE_SYM
unknown's avatar
unknown committed
573 574 575 576 577 578 579 580 581 582 583 584
%token	SUBSTRING
%token	SUBSTRING_INDEX
%token	TRIM
%token	UDA_CHAR_SUM
%token	UDA_FLOAT_SUM
%token	UDA_INT_SUM
%token	UDF_CHAR_FUNC
%token	UDF_FLOAT_FUNC
%token	UDF_INT_FUNC
%token	UNIQUE_USERS
%token	UNIX_TIMESTAMP
%token	USER
585 586 587
%token	UTC_DATE_SYM
%token	UTC_TIME_SYM
%token	UTC_TIMESTAMP_SYM
unknown's avatar
unknown committed
588 589
%token	WEEK_SYM
%token	WHEN_SYM
unknown's avatar
unknown committed
590
%token	WORK_SYM
unknown's avatar
unknown committed
591 592 593
%token	YEAR_MONTH_SYM
%token	YEAR_SYM
%token	YEARWEEK
unknown's avatar
unknown committed
594 595 596
%token	BENCHMARK_SYM
%token	END
%token	THEN_SYM
unknown's avatar
unknown committed
597

unknown's avatar
unknown committed
598 599 600 601
%token	SQL_BIG_RESULT
%token	SQL_CACHE_SYM
%token	SQL_CALC_FOUND_ROWS
%token	SQL_NO_CACHE_SYM
unknown's avatar
unknown committed
602
%token	SQL_SMALL_RESULT
unknown's avatar
unknown committed
603
%token  SQL_BUFFER_RESULT
unknown's avatar
unknown committed
604

605 606 607 608 609
%token  CURSOR_SYM
%token  ELSEIF_SYM
%token  ITERATE_SYM
%token  LEAVE_SYM
%token  LOOP_SYM
610
%token  REPEAT_SYM
611 612 613 614 615
%token  UNTIL_SYM
%token  WHILE_SYM
%token  ASENSITIVE_SYM
%token  INSENSITIVE_SYM
%token  SENSITIVE_SYM
616

unknown's avatar
unknown committed
617 618
%token  ISSUER_SYM
%token  SUBJECT_SYM
619
%token  CIPHER_SYM
unknown's avatar
unknown committed
620

621
%token  BEFORE_SYM
unknown's avatar
unknown committed
622 623 624 625 626 627 628 629 630
%left   SET_VAR
%left	OR_OR_CONCAT OR
%left	AND
%left	BETWEEN_SYM CASE_SYM WHEN_SYM THEN_SYM ELSE
%left	EQ EQUAL_SYM GE GT_SYM LE LT NE IS LIKE REGEXP IN_SYM
%left	'|'
%left	'&'
%left	SHIFT_LEFT SHIFT_RIGHT
%left	'-' '+'
631
%left	'*' '/' '%' DIV_SYM MOD_SYM
unknown's avatar
unknown committed
632 633
%left   XOR
%left   '^'
unknown's avatar
unknown committed
634
%left	NEG '~'
unknown's avatar
unknown committed
635
%right	NOT
unknown's avatar
unknown committed
636
%right	BINARY COLLATE_SYM
637

unknown's avatar
unknown committed
638
%type <lex_str>
639 640
	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
641
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
642
	NCHAR_STRING opt_component key_cache_name
643
        SP_FUNC ident_or_spfunc sp_opt_label
unknown's avatar
unknown committed
644 645 646 647 648

%type <lex_str_ptr>
	opt_table_alias

%type <table>
649
	table_ident references
unknown's avatar
unknown committed
650 651

%type <simple_string>
652
	remember_name remember_end opt_ident opt_db text_or_password
653
	opt_escape opt_constraint
unknown's avatar
unknown committed
654 655

%type <string>
656
	text_string opt_gconcat_separator
unknown's avatar
unknown committed
657 658

%type <num>
unknown's avatar
unknown committed
659
	type int_type real_type order_dir opt_field_spec lock_option
unknown's avatar
unknown committed
660
	udf_type if_exists opt_local opt_table_options table_options
661 662 663
        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
        opt_ignore_leaves fulltext_options
unknown's avatar
unknown committed
664 665

%type <ulong_num>
666
	ULONG_NUM raid_types merge_insert_types
unknown's avatar
unknown committed
667

unknown's avatar
unknown committed
668 669
%type <ulonglong_number>
	ulonglong_num
unknown's avatar
unknown committed
670

unknown's avatar
unknown committed
671 672 673
%type <lock_type>
	replace_lock_option opt_low_priority insert_lock_option load_data_lock

unknown's avatar
unknown committed
674
%type <item>
unknown's avatar
unknown committed
675
	literal text_literal insert_ident order_ident
unknown's avatar
unknown committed
676
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
677
	table_wild no_in_expr expr_expr simple_expr no_and_expr udf_expr
678
	using_list expr_or_default set_expr_or_default interval_expr
unknown's avatar
unknown committed
679
	param_marker singlerow_subselect singlerow_subselect_init
unknown's avatar
unknown committed
680
	signed_literal NUM_literal
681
	exists_subselect exists_subselect_init sp_opt_default
unknown's avatar
unknown committed
682 683

%type <item_list>
684 685
	expr_list sp_expr_list udf_expr_list udf_expr_list2 when_list
	ident_list ident_list_arg
unknown's avatar
unknown committed
686 687

%type <key_type>
688
	key_type opt_unique_or_fulltext constraint_key_type
unknown's avatar
unknown committed
689

unknown's avatar
unknown committed
690 691 692
%type <key_alg>
	key_alg opt_btree_or_rtree

unknown's avatar
unknown committed
693 694 695 696 697 698 699
%type <string_list>
	key_usage_list

%type <key_part>
	key_part

%type <table_list>
700
	join_table_list  join_table
701
        table_factor table_ref
unknown's avatar
unknown committed
702 703 704 705 706

%type <udf>
	UDF_CHAR_FUNC UDF_FLOAT_FUNC UDF_INT_FUNC
	UDA_CHAR_SUM UDA_FLOAT_SUM UDA_INT_SUM

707
%type <date_time_type> date_time_type;
unknown's avatar
unknown committed
708 709
%type <interval> interval

710 711
%type <interval_time_st> interval_time_st

unknown's avatar
unknown committed
712
%type <db_type> storage_engines
unknown's avatar
unknown committed
713 714 715

%type <row_type> row_types

unknown's avatar
unknown committed
716
%type <tx_isolation> isolation_types
unknown's avatar
unknown committed
717

unknown's avatar
unknown committed
718 719
%type <ha_rkey_mode> handler_rkey_mode

720
%type <cast_type> cast_type cast_type_finalize
unknown's avatar
unknown committed
721

unknown's avatar
unknown committed
722 723 724 725 726 727
%type <udf_type> udf_func_type

%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword

%type <lex_user> user grant_user

728
%type <charset>
729
	opt_collate
730 731
	charset_name
	charset_name_or_default
unknown's avatar
unknown committed
732 733
	old_or_new_charset_name
	old_or_new_charset_name_or_default
734 735
	collation_name
	collation_name_or_default
736

unknown's avatar
unknown committed
737 738
%type <variable> internal_variable_name

unknown's avatar
unknown committed
739 740
%type <select_lex> in_subselect in_subselect_init

unknown's avatar
unknown committed
741 742
%type <boolfunc2creator> comp_op

unknown's avatar
unknown committed
743
%type <NONE>
unknown's avatar
unknown committed
744
	query verb_clause create change select do drop insert replace insert2
745
	insert_values update delete truncate rename
unknown's avatar
unknown committed
746
	show describe load alter optimize keycache preload flush
747
	reset purge begin commit rollback savepoint
748
	slave master_def master_defs master_file_def slave_until_opts
749
	repair restore backup analyze check start checksum
750
	field_list field_list_item field_spec kill column_def key_def
unknown's avatar
unknown committed
751
	keycache_list assign_to_keycache preload_list preload_keys
unknown's avatar
unknown committed
752
	select_item_list select_item values_list no_braces
753
	opt_limit_clause delete_limit_clause fields opt_values values
unknown's avatar
unknown committed
754
	procedure_list procedure_list2 procedure_item
755
	when_list2 expr_list2 udf_expr_list3 handler
unknown's avatar
unknown committed
756 757
	opt_precision opt_ignore opt_column opt_restrict
	grant revoke set lock unlock string_list field_options field_option
unknown's avatar
unknown committed
758
	field_opt_list opt_binary table_lock_list table_lock
759
	ref_list opt_on_delete opt_on_delete_list opt_on_delete_item use
unknown's avatar
unknown committed
760
	opt_delete_options opt_delete_option varchar nchar nvarchar
unknown's avatar
unknown committed
761
	opt_outer table_list table_name opt_option opt_place
unknown's avatar
unknown committed
762 763
	opt_attribute opt_attribute_list attribute column_list column_list_id
	opt_column_list grant_privileges opt_table user_list grant_option
unknown's avatar
unknown committed
764
	grant_privilege grant_privilege_list
unknown's avatar
unknown committed
765
	flush_options flush_option
unknown's avatar
unknown committed
766 767
	equal optional_braces opt_key_definition key_usage_list2
	opt_mi_check_type opt_to mi_check_types normal_join
768
	table_to_table_list table_to_table opt_table_list opt_as
unknown's avatar
unknown committed
769
	handler_rkey_function handler_read_or_scan
unknown's avatar
unknown committed
770
	single_multi table_wild_list table_wild_one opt_wild
771
	union_clause union_list union_option
772
	precision subselect_start opt_and charset
773
	subselect_end select_var_list select_var_list_init help opt_len
unknown's avatar
unknown committed
774
	opt_extended_describe
unknown's avatar
unknown committed
775
	statement sp_suid
776
	sp_c_chistics sp_a_chistics sp_chistic sp_c_chistic sp_a_chistic
777
END_OF_INPUT
unknown's avatar
unknown committed
778

779
%type <NONE> call sp_proc_stmts sp_proc_stmt
780 781 782
%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
783
%type <lex> sp_cursor_stmt
784

unknown's avatar
unknown committed
785 786
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
787 788
	',' '!' '{' '}' '&' '|' AND OR OR_OR_CONCAT BETWEEN_SYM CASE_SYM
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM
unknown's avatar
unknown committed
789 790 791 792 793
%%


query:
	END_OF_INPUT
unknown's avatar
unknown committed
794
	{
795
	   THD *thd= YYTHD;
796
	   if (!thd->bootstrap &&
unknown's avatar
unknown committed
797
	      (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
798
	   {
799
	     send_error(thd,ER_EMPTY_QUERY);
800
	     YYABORT;
801
	   }
802 803
	   else
	   {
804
	     thd->lex->sql_command= SQLCOM_EMPTY_QUERY;
805
	   }
unknown's avatar
unknown committed
806
	}
unknown's avatar
unknown committed
807
	| verb_clause END_OF_INPUT {};
unknown's avatar
unknown committed
808 809

verb_clause:
810 811 812 813 814 815
	  statement
	| begin
	;

/* Verb clauses, except begin */
statement:
unknown's avatar
unknown committed
816 817
	  alter
	| analyze
818
	| backup
819
	| call
unknown's avatar
unknown committed
820 821
	| change
	| check
822
	| checksum
unknown's avatar
unknown committed
823 824 825 826
	| commit
	| create
	| delete
	| describe
unknown's avatar
unknown committed
827
	| do
unknown's avatar
unknown committed
828
	| drop
unknown's avatar
unknown committed
829
	| flush
unknown's avatar
unknown committed
830
	| grant
unknown's avatar
unknown committed
831 832
	| handler
	| help
unknown's avatar
unknown committed
833
	| insert
unknown's avatar
unknown committed
834
	| kill
unknown's avatar
unknown committed
835 836 837
	| load
	| lock
	| optimize
unknown's avatar
unknown committed
838
        | keycache
unknown's avatar
unknown committed
839
	| preload
840
	| purge
841
	| rename
unknown's avatar
unknown committed
842
	| repair
unknown's avatar
unknown committed
843
	| replace
unknown's avatar
unknown committed
844
	| reset
845
	| restore
unknown's avatar
unknown committed
846 847
	| revoke
	| rollback
848
	| savepoint
unknown's avatar
unknown committed
849 850
	| select
	| set
unknown's avatar
unknown committed
851
	| show
unknown's avatar
unknown committed
852
	| slave
unknown's avatar
unknown committed
853
	| start
854
	| truncate
unknown's avatar
unknown committed
855 856
	| unlock
	| update
unknown's avatar
unknown committed
857
	| use
unknown's avatar
unknown committed
858
        ;
859

unknown's avatar
unknown committed
860 861
/* help */

862
help:
863
       HELP_SYM ident_or_text
unknown's avatar
unknown committed
864
       {
865 866 867
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
	  lex->help_arg= $2.str;
868
       };
unknown's avatar
unknown committed
869 870 871 872

/* change master */

change:
unknown's avatar
unknown committed
873
       CHANGE MASTER_SYM TO_SYM
unknown's avatar
unknown committed
874 875 876
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
unknown's avatar
unknown committed
877
	  bzero((char*) &lex->mi, sizeof(lex->mi));
878 879 880 881
        }
       master_defs
	{}
       ;
unknown's avatar
unknown committed
882 883 884

master_defs:
       master_def
unknown's avatar
unknown committed
885
       | master_defs ',' master_def;
unknown's avatar
unknown committed
886

unknown's avatar
unknown committed
887
master_def:
unknown's avatar
unknown committed
888
       MASTER_HOST_SYM EQ TEXT_STRING_sys
unknown's avatar
unknown committed
889 890 891 892
       {
	 Lex->mi.host = $3.str;
       }
       |
unknown's avatar
unknown committed
893
       MASTER_USER_SYM EQ TEXT_STRING_sys
unknown's avatar
unknown committed
894 895 896 897
       {
	 Lex->mi.user = $3.str;
       }
       |
unknown's avatar
unknown committed
898
       MASTER_PASSWORD_SYM EQ TEXT_STRING_sys
unknown's avatar
unknown committed
899 900 901 902
       {
	 Lex->mi.password = $3.str;
       }
       |
unknown's avatar
unknown committed
903
       MASTER_PORT_SYM EQ ULONG_NUM
unknown's avatar
unknown committed
904 905 906 907
       {
	 Lex->mi.port = $3;
       }
       |
unknown's avatar
unknown committed
908
       MASTER_CONNECT_RETRY_SYM EQ ULONG_NUM
unknown's avatar
unknown committed
909 910 911
       {
	 Lex->mi.connect_retry = $3;
       }
unknown's avatar
unknown committed
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
       | 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;
936
	 }
937
       |
938
         master_file_def
939
       ;
unknown's avatar
unknown committed
940

941 942
master_file_def:
       MASTER_LOG_FILE_SYM EQ TEXT_STRING_sys
943
       {
944
	 Lex->mi.log_file_name = $3.str;
945
       }
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
       | 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);
         }
972
       ;
unknown's avatar
unknown committed
973 974 975 976 977 978

/* create a table */

create:
	CREATE opt_table_options TABLE_SYM opt_if_not_exists table_ident
	{
979
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
980 981
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_CREATE_TABLE;
unknown's avatar
unknown committed
982
	  if (!lex->select_lex.add_table_to_list(thd,$5,
983
						 ($2 &
984 985
						  HA_LEX_CREATE_TMP_TABLE ?
						  &tmp_table_alias :
unknown's avatar
unknown committed
986 987
						  (LEX_STRING*) 0),
						 TL_OPTION_UPDATING,
unknown's avatar
unknown committed
988 989 990
						 ((using_update_log)?
						  TL_READ_NO_INSERT:
						  TL_READ)))
unknown's avatar
unknown committed
991 992 993 994 995 996 997
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
	  lex->change=NullS;
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	  lex->create_info.options=$2 | $4;
unknown's avatar
unknown committed
998
	  lex->create_info.db_type= (enum db_type) lex->thd->variables.table_type;
999
	  lex->create_info.default_table_charset= thd->variables.collation_database;
unknown's avatar
unknown committed
1000
	  lex->name=0;
unknown's avatar
unknown committed
1001 1002
	}
	create2
unknown's avatar
unknown committed
1003
	  { Lex->current_select= &Lex->select_lex; }
unknown's avatar
unknown committed
1004
	| CREATE opt_unique_or_fulltext INDEX ident key_alg ON table_ident
unknown's avatar
unknown committed
1005
	  {
unknown's avatar
unknown committed
1006 1007
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_CREATE_INDEX;
unknown's avatar
unknown committed
1008 1009
	    if (!lex->current_select->add_table_to_list(lex->thd, $7, NULL,
							TL_OPTION_UPDATING))
unknown's avatar
unknown committed
1010
	      YYABORT;
unknown's avatar
unknown committed
1011 1012 1013 1014
	    lex->create_list.empty();
	    lex->key_list.empty();
	    lex->col_list.empty();
	    lex->change=NullS;
unknown's avatar
unknown committed
1015
	  }
1016
	   '(' key_list ')'
unknown's avatar
unknown committed
1017
	  {
unknown's avatar
unknown committed
1018
	    LEX *lex=Lex;
unknown's avatar
unknown committed
1019

1020
	    lex->key_list.push_back(new Key($2,$4.str, $5, lex->col_list));
unknown's avatar
unknown committed
1021
	    lex->col_list.empty();
unknown's avatar
unknown committed
1022 1023
	  }
	| CREATE DATABASE opt_if_not_exists ident
1024
	  { Lex->create_info.default_table_charset=NULL; }
1025
	  opt_create_database_options
unknown's avatar
unknown committed
1026
	  {
unknown's avatar
unknown committed
1027 1028 1029 1030
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_CREATE_DB;
	    lex->name=$4.str;
            lex->create_info.options=$3;
unknown's avatar
unknown committed
1031
	  }
1032
	| CREATE udf_func_type FUNCTION_SYM ident_or_spfunc
unknown's avatar
unknown committed
1033
	  {
unknown's avatar
unknown committed
1034
	    LEX *lex=Lex;
1035
	    lex->udf.name = $4;
unknown's avatar
unknown committed
1036
	    lex->udf.type= $2;
unknown's avatar
unknown committed
1037
	  }
1038 1039
	  create_function_tail
	  {}
1040 1041 1042
	| CREATE PROCEDURE ident
	  {
	    LEX *lex= Lex;
1043
	    sp_head *sp;
1044

1045 1046 1047 1048 1049
	    if (lex->sphead)
	    {
	      net_printf(YYTHD, ER_SP_NO_RECURSIVE_CREATE, "PROCEDURE");
	      YYABORT;
	    }
1050 1051 1052
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
1053
	    sp->init(lex);
1054 1055 1056

	    sp->m_type= TYPE_ENUM_PROCEDURE;
	    lex->sphead= sp;
1057 1058 1059 1060 1061
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
1062
	    sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
1063
	    YYTHD->client_capabilities &= (~CLIENT_MULTI_QUERIES);
1064
	  }
1065
          '('
1066
	  {
1067 1068
	    LEX *lex= Lex;

1069 1070 1071 1072 1073 1074 1075 1076
	    lex->sphead->m_param_begin= lex->tok_start+1;
	  }
	  sp_pdparam_list
	  ')'
	  {
	    LEX *lex= Lex;

	    lex->sphead->m_param_end= lex->tok_start;
1077 1078
	    lex->spcont->set_params();
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
1079
	  }
1080
	  sp_c_chistics
unknown's avatar
unknown committed
1081
	  {
1082 1083 1084
	    LEX *lex= Lex;

	    lex->sphead->m_chistics= &lex->sp_chistics;
1085
	    lex->sphead->m_body_begin= lex->tok_start;
unknown's avatar
unknown committed
1086
	  }
1087
	  sp_proc_stmt
1088
	  {
1089 1090
	    LEX *lex= Lex;

unknown's avatar
unknown committed
1091
	    lex->sphead->init_strings(YYTHD, lex, &$3);
1092 1093 1094 1095
	    lex->sql_command= SQLCOM_CREATE_PROCEDURE;
	    /* Restore flag if it was cleared above */
	    if (lex->sphead->m_old_cmq)
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
1096
	    lex->sphead->restore_thd_mem_root(YYTHD);
1097 1098 1099
	  } 
	;

1100
ident_or_spfunc:
unknown's avatar
unknown committed
1101
	  IDENT_sys { $$= $1; }
1102 1103 1104
	| SP_FUNC { $$= $1; }
	;

1105
create_function_tail:
unknown's avatar
unknown committed
1106
	  RETURNS_SYM udf_type UDF_SONAME_SYM TEXT_STRING_sys
unknown's avatar
unknown committed
1107
	  {
unknown's avatar
unknown committed
1108
	    LEX *lex=Lex;
1109 1110 1111
	    lex->sql_command = SQLCOM_CREATE_FUNCTION;
	    lex->udf.returns=(Item_result) $2;
	    lex->udf.dl=$4.str;
1112
	  }
1113 1114 1115
	| '('
	  {
	    LEX *lex= Lex;
1116
	    sp_head *sp;
1117

1118 1119 1120 1121 1122
	    if (lex->sphead)
	    {
	      net_printf(YYTHD, ER_SP_NO_RECURSIVE_CREATE, "FUNCTION");
	      YYABORT;
	    }
1123 1124 1125
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
1126
	    sp->init(lex);
1127 1128 1129

	    sp->m_type= TYPE_ENUM_FUNCTION;
	    lex->sphead= sp;
1130 1131 1132 1133 1134
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
1135
	    sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
1136
	    YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
1137
	    lex->sphead->m_param_begin= lex->tok_start+1;
1138 1139 1140
	  }
          sp_fdparam_list ')'
	  {
1141 1142 1143 1144 1145 1146 1147 1148
	    LEX *lex= Lex;

	    lex->spcont->set_params();
	    lex->sphead->m_param_end= lex->tok_start;
	  }
	  RETURNS_SYM
	  {
	    Lex->sphead->m_returns_begin= Lex->tok_start;
1149
	  }
1150
	  type
1151
	  {
1152 1153
	    LEX *lex= Lex;

1154 1155
	    lex->sphead->m_returns_end= lex->tok_start;
	    lex->sphead->m_returns= (enum enum_field_types)$8;
1156
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
1157
	  }
1158
	  sp_c_chistics
unknown's avatar
unknown committed
1159
	  {
1160 1161 1162
	    LEX *lex= Lex;

	    lex->sphead->m_chistics= &lex->sp_chistics;
1163
	    lex->sphead->m_body_begin= lex->tok_start;
1164
	  }
1165
	  sp_proc_stmt
1166
	  {
1167
	    LEX *lex= Lex;
1168
	    sp_head *sp= lex->sphead;
1169

1170
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
unknown's avatar
unknown committed
1171
	    sp->init_strings(YYTHD, lex, &lex->udf.name);
1172
	    /* Restore flag if it was cleared above */
1173
	    if (sp->m_old_cmq)
1174
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
1175
	    sp->restore_thd_mem_root(YYTHD);
1176
	  }
1177 1178
	;

1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
sp_a_chistics:
	  /* Empty */ {}
	| sp_a_chistics sp_a_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; }
	| sp_suid { }
unknown's avatar
unknown committed
1193 1194
	;

1195 1196 1197 1198
/* Alter characteristics */
sp_a_chistic:
	  sp_chistic     { }
	| NAME_SYM ident { Lex->name= $2.str; }
unknown's avatar
unknown committed
1199 1200
	;

1201 1202 1203 1204 1205 1206 1207
/* Create characteristics */
sp_c_chistic:
	  sp_chistic            { }
	| LANGUAGE_SYM SQL_SYM  { }
	| DETERMINISTIC_SYM     { Lex->sp_chistics.detistic= TRUE; }
	| NOT DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
	;
unknown's avatar
unknown committed
1208 1209

sp_suid:
1210 1211 1212 1213 1214 1215 1216 1217
	  SQL_SYM SECURITY_SYM DEFINER_SYM
	  {
	    Lex->sp_chistics.suid= IS_SUID;
	  }
	| SQL_SYM SECURITY_SYM INVOKER_SYM
	  {
	    Lex->sp_chistics.suid= IS_NOT_SUID;
	  }
unknown's avatar
unknown committed
1218 1219
	;

1220
call:
1221
	  CALL_SYM ident_or_spfunc
1222 1223 1224 1225
	  {
	    LEX *lex = Lex;

	    lex->sql_command= SQLCOM_CALL;
1226
	    lex->udf.name= $2;
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	    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);
	  }
	;

1249 1250
/* Stored FUNCTION parameter declaration list */
sp_fdparam_list:
1251
	  /* Empty */
1252
	| sp_fdparams
1253 1254
	;

1255 1256 1257
sp_fdparams:
	  sp_fdparams ',' sp_fdparam
	| sp_fdparam
1258 1259
	;

1260 1261 1262
sp_fdparam:
	  ident type sp_opt_locator
	  {
unknown's avatar
unknown committed
1263 1264 1265 1266 1267
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
1268
	      net_printf(YYTHD, ER_SP_DUP_PARAM, $1.str);
unknown's avatar
unknown committed
1269 1270 1271
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum enum_field_types)$2, sp_param_in);
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
1287 1288
	  sp_opt_inout ident type sp_opt_locator
	  {
unknown's avatar
unknown committed
1289 1290 1291 1292 1293
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$2, TRUE))
	    {
1294
	      net_printf(YYTHD, ER_SP_DUP_PARAM, $2.str);
unknown's avatar
unknown committed
1295 1296 1297 1298
	      YYABORT;
	    }
	    spc->push_pvar(&$2, (enum enum_field_types)$3,
			   (sp_param_mode_t)$1);
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
	  }
	;

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

sp_opt_locator:
	  /* Empty */
	| AS LOCATOR_SYM
	;

sp_proc_stmts:
unknown's avatar
unknown committed
1315
	  /* Empty */ {}
1316
	| sp_proc_stmts sp_proc_stmt ';'
1317 1318 1319 1320 1321
	;

sp_decls:
	  /* Empty */
	  {
1322
	    $$.vars= $$.conds= $$.hndlrs= $$.curs= 0;
1323 1324 1325
	  }
	| sp_decls sp_decl ';'
	  {
1326 1327 1328
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
1329
	    $$.curs= $1.curs + $2.curs;
1330 1331 1332 1333
	  }
	;

sp_decl:
1334
	  DECLARE_SYM sp_decl_idents type sp_opt_default
1335 1336 1337
	  {
	    LEX *lex= Lex;
	    uint max= lex->spcont->current_framesize();
1338 1339
	    enum enum_field_types type= (enum enum_field_types)$3;
	    Item *it= $4;
1340 1341 1342

	    for (uint i = max-$2 ; i < max ; i++)
	    {
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
	      lex->spcont->set_type(i, type);
	      if (! it)
	        lex->spcont->set_isset(i, FALSE);
	      else
	      {
	        sp_instr_set *in= new sp_instr_set(lex->sphead->instructions(),
	                                           i, it, type);

	        lex->sphead->add_instr(in);
	        lex->spcont->set_isset(i, TRUE);
	      }
1354
	    }
1355
	    $$.vars= $2;
1356
	    $$.conds= $$.hndlrs= $$.curs= 0;
1357 1358 1359
	  }
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
unknown's avatar
unknown committed
1360 1361 1362 1363 1364
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1365
	      net_printf(YYTHD, ER_SP_DUP_COND, $2.str);
unknown's avatar
unknown committed
1366 1367
	      YYABORT;
	    }
1368
	    YYTHD->lex->spcont->push_cond(&$2, $5);
1369
	    $$.vars= $$.hndlrs= $$.curs= 0;
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
	    $$.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(), $2,
	                              ctx->current_framesize());

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

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

	      sp->add_instr(i);
	      sp->push_backpatch(i, lex->spcont->last_label()); /* Block end */
	    }
	    lex->sphead->backpatch(hlab);
1402
	    $$.vars= $$.conds= $$.curs= 0;
1403 1404 1405 1406
	    $$.hndlrs= $6;
	  }
	| DECLARE_SYM ident CURSOR_SYM FOR_SYM sp_cursor_stmt
	  {
1407 1408
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
unknown's avatar
unknown committed
1409 1410 1411
	    sp_pcontext *spc= lex->spcont;
	    uint offp;
	    sp_instr_cpush *i;
1412

unknown's avatar
unknown committed
1413 1414
	    if (spc->find_cursor(&$2, &offp, TRUE))
	    {
1415 1416
	      net_printf(YYTHD, ER_SP_DUP_CURS, $2.str);
	      delete $5;
unknown's avatar
unknown committed
1417 1418 1419
	      YYABORT;
	    }
            i= new sp_instr_cpush(sp->instructions(), $5);
1420 1421
	    sp->add_instr(i);
	    lex->spcont->push_cursor(&$2);
1422
	    $$.vars= $$.conds= $$.hndlrs= 0;
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
	    $$.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)
	    {
	      send_error(YYTHD, ER_SP_BAD_CURSOR_QUERY);
	      YYABORT;
	    }
	    if (lex->result)
	    {
	      send_error(YYTHD, ER_SP_BAD_CURSOR_SELECT);
	      YYABORT;
	    }
	    lex->sp_lex_in_use= TRUE;
	    $$= lex;
	    lex->sphead->restore_lex(YYTHD);
	  }
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
	;

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;
	  }
unknown's avatar
unknown committed
1490
	| SQLSTATE_SYM opt_value TEXT_STRING_literal
1491
	  {		/* SQLSTATE */
unknown's avatar
unknown committed
1492 1493
	    uint len= ($3.length < sizeof($$->sqlstate)-1 ?
                       $3.length : sizeof($$->sqlstate)-1);
1494 1495 1496

	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
unknown's avatar
unknown committed
1497
	    memcpy($$->sqlstate, $3.str, len);
1498 1499 1500 1501
	    $$->sqlstate[len]= '\0';
	  }
	;

unknown's avatar
unknown committed
1502 1503 1504 1505 1506
opt_value:
	  /* Empty */  {}
	| VALUE_SYM    {}
	;

1507 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 1533 1534
sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
	      net_printf(YYTHD, ER_SP_COND_MISMATCH, $1.str);
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
	| NOT FOUND_SYM		/* SQLSTATEs 02??? */
	  {
	    $$= (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;
1535 1536 1537 1538 1539 1540
	  }
	;

sp_decl_idents:
	  ident
	  {
unknown's avatar
unknown committed
1541 1542 1543 1544 1545
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
1546
	      net_printf(YYTHD, ER_SP_DUP_VAR, $1.str);
unknown's avatar
unknown committed
1547 1548 1549
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum_field_types)0, sp_param_in);
1550 1551 1552 1553
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
unknown's avatar
unknown committed
1554 1555 1556 1557 1558
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$3, TRUE))
	    {
1559
	      net_printf(YYTHD, ER_SP_DUP_VAR, $3.str);
unknown's avatar
unknown committed
1560 1561 1562
	      YYABORT;
	    }
	    spc->push_pvar(&$3, (enum_field_types)0, sp_param_in);
1563 1564 1565 1566
	    $$= $1 + 1;
	  }
	;

1567 1568 1569 1570 1571
sp_opt_default:
	  /* Empty */ { $$ = NULL; }
        | DEFAULT expr { $$ = $2; }
	;

1572
sp_proc_stmt:
1573 1574 1575
	  {
	    Lex->sphead->reset_lex(YYTHD);
	  }
1576 1577 1578 1579
	  statement
	  {
	    LEX *lex= Lex;

1580 1581
	    if (lex->sql_command == SQLCOM_SELECT && !lex->result)
	    {
1582
	      /* We maybe have one or more SELECT without INTO */
1583
	      lex->sphead->m_multi_results= TRUE;
1584
	    }
1585 1586 1587 1588 1589
	    /* 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 ||
1590
		! lex->var_list.is_empty())
1591
	    {
1592 1593 1594 1595 1596
	      /* Currently we can't handle queries inside a FUNCTION,
	      ** because of the way table locking works.
	      ** This is unfortunate, and limits the usefulness of functions
	      ** a great deal, but it's nothing we can do about this at the
	      ** moment.
1597
	      */
1598 1599
	      if (lex->sphead->m_type == TYPE_ENUM_FUNCTION &&
		  lex->sql_command != SQLCOM_SET_OPTION)
1600
	      {
1601
		send_error(YYTHD, ER_SP_BADSTATEMENT);
1602 1603 1604 1605 1606 1607 1608 1609
		YYABORT;
	      }
	      else
	      {
		sp_instr_stmt *i=new sp_instr_stmt(lex->sphead->instructions());

		i->set_lex(lex);
		lex->sphead->add_instr(i);
1610
		lex->sp_lex_in_use= TRUE;
1611 1612 1613
	      }
            }
	    lex->sphead->restore_lex(YYTHD);
1614
          }
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
	| RETURN_SYM expr
	  {
	    LEX *lex= Lex;

	    if (lex->sphead->m_type == TYPE_ENUM_PROCEDURE)
	    {
	      send_error(YYTHD, ER_SP_BADRETURN);
	      YYABORT;
	    }
	    else
	    {
1626
	      sp_instr_freturn *i;
1627

1628 1629 1630 1631 1632 1633 1634
	      if ($2->type() == Item::SUBSELECT_ITEM)
	      {  /* QQ For now, just disallow subselects as values */
	        send_error(lex->thd, ER_SP_BADSTATEMENT);
	        YYABORT;
	      }
	      i= new sp_instr_freturn(lex->sphead->instructions(),
		                      $2, lex->sphead->m_returns);
1635
	      lex->sphead->add_instr(i);
1636
	      lex->sphead->m_has_return= TRUE;
1637 1638
	    }
	  }
1639
	| IF sp_if END IF {}
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
	| CASE_SYM WHEN_SYM
	  {
	    Lex->sphead->m_simple_case= FALSE;
	  }
	  sp_case END CASE_SYM {}
	| CASE_SYM expr WHEN_SYM
	  {
	    /* We "fake" this by using an anonymous variable which we
	       set to the expression. Note that all WHENs are evaluate
	       at the same frame level, so we then know that it's the
	       top-most variable in the frame. */
	    LEX *lex= Lex;
	    uint offset= lex->spcont->current_framesize();
	    sp_instr_set *i = new sp_instr_set(lex->sphead->instructions(),
	                                       offset, $2, MYSQL_TYPE_STRING);
	    LEX_STRING dummy;

	    dummy.str= (char *)"";
	    dummy.length= 0;
1659
	    lex->spcont->push_pvar(&dummy, MYSQL_TYPE_STRING, sp_param_in);
1660 1661 1662 1663 1664
	    lex->sphead->add_instr(i);
	    lex->sphead->m_simple_case= TRUE;
	  }
	  sp_case END CASE_SYM
	  {
1665
	    Lex->spcont->pop_pvar();
1666
	  }
1667 1668 1669 1670 1671
	| sp_labeled_control
	  {}
	| { /* Unlabeled controls get a secret label. */
	    LEX *lex= Lex;

1672
	    lex->spcont->push_label((char *)"", lex->sphead->instructions());
1673 1674 1675
	  }
	  sp_unlabeled_control
	  {
1676 1677
	    LEX *lex= Lex;

1678
	    lex->sphead->backpatch(lex->spcont->pop_label());
1679 1680 1681 1682
	  }
	| LEAVE_SYM IDENT
	  {
	    LEX *lex= Lex;
1683
	    sp_head *sp = lex->sphead;
1684 1685 1686 1687
	    sp_label_t *lab= lex->spcont->find_label($2.str);

	    if (! lab)
	    {
1688
	      net_printf(YYTHD, ER_SP_LILABEL_MISMATCH, "LEAVE", $2.str);
1689 1690 1691 1692
	      YYABORT;
	    }
	    else
	    {
1693
	      sp_instr_jump *i= new sp_instr_jump(sp->instructions());
1694

1695 1696
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
1697 1698 1699 1700 1701 1702 1703
	    }
	  }
	| ITERATE_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->find_label($2.str);

unknown's avatar
unknown committed
1704
	    if (! lab || lab->isbegin)
1705
	    {
1706
	      net_printf(YYTHD, ER_SP_LILABEL_MISMATCH, "ITERATE", $2.str);
1707 1708 1709 1710 1711
	      YYABORT;
	    }
	    else
	    {
	      uint ip= lex->sphead->instructions();
1712
	      sp_instr_jump *i= new sp_instr_jump(ip, lab->ip); /* Jump back */
1713 1714 1715 1716

              lex->sphead->add_instr(i);
	    }
	  }
1717
	| OPEN_SYM ident
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
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_copen *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
	      net_printf(YYTHD, ER_SP_CURSOR_MISMATCH, $2.str);
	      YYABORT;
	    }
	    i= new sp_instr_copen(sp->instructions(), offset);
	    sp->add_instr(i);
	  }
	| FETCH_SYM ident INTO
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cfetch *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
	      net_printf(YYTHD, ER_SP_CURSOR_MISMATCH, $2.str);
	      YYABORT;
	    }
	    i= new sp_instr_cfetch(sp->instructions(), offset);
	    sp->add_instr(i);
	  }
	  sp_fetch_list
	  { }
1749
	| CLOSE_SYM ident
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 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cclose *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
	      net_printf(YYTHD, ER_SP_CURSOR_MISMATCH, $2.str);
	      YYABORT;
	    }
	    i= new sp_instr_cclose(sp->instructions(), offset);
	    sp->add_instr(i);
	  }
	;

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)))
	    {
	      net_printf(YYTHD, ER_SP_UNDECLARED_VAR, $1.str);
	      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)))
	    {
	      net_printf(YYTHD, ER_SP_UNDECLARED_VAR, $3.str);
	      YYABORT;
	    }
	    else
	    { /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	      spv->isset= TRUE;
	    }
	  }
1808
	;
unknown's avatar
unknown committed
1809

1810 1811 1812 1813 1814 1815 1816 1817
sp_if:
	  expr THEN_SYM
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, $1);

1818
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
            sp->add_instr(i);
	  }
	  sp_proc_stmts
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump *i = new sp_instr_jump(ip);

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

1836
	    lex->sphead->backpatch(lex->spcont->pop_label());
1837 1838 1839 1840 1841 1842 1843 1844 1845
	  }
	;

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

1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
sp_case:
	  expr THEN_SYM
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

	    if (! sp->m_simple_case)
	      i= new sp_instr_jump_if_not(ip, $1);
	    else
	    { /* Simple case: <caseval> = <whenval> */
unknown's avatar
unknown committed
1858 1859
	      LEX_STRING ivar;

unknown's avatar
unknown committed
1860
	      ivar.str= (char *)"_tmp_";
unknown's avatar
unknown committed
1861 1862 1863
	      ivar.length= 5;
	      Item *var= (Item*) new Item_splocal(ivar, 
						  ctx->current_framesize()-1);
unknown's avatar
unknown committed
1864
	      Item *expr= new Item_func_eq(var, $1);
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890

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

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

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

sp_whens:
1891 1892 1893
	  /* Empty */ {}
	| WHEN_SYM sp_case {}
	| ELSE sp_proc_stmts {}
1894 1895
	;

1896 1897 1898 1899 1900 1901 1902 1903
sp_labeled_control:
	  IDENT ':'
	  {
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->find_label($1.str);

	    if (lab)
	    {
1904
	      net_printf(YYTHD, ER_SP_LABEL_REDEFINE, $1.str);
1905 1906 1907
	      YYABORT;
	    }
	    else
1908
	    {
1909 1910
	      lex->spcont->push_label($1.str,
	                              lex->sphead->instructions());
1911
	    }
1912
	  }
unknown's avatar
unknown committed
1913
	  sp_unlabeled_control sp_opt_label
1914 1915 1916
	  {
	    LEX *lex= Lex;

unknown's avatar
unknown committed
1917
	    if ($5.str)
1918
	    {
unknown's avatar
unknown committed
1919 1920 1921 1922 1923 1924 1925 1926
	      sp_label_t *lab= lex->spcont->find_label($5.str);

	      if (!lab ||
	          my_strcasecmp(system_charset_info, $5.str, lab->name) != 0)
	      {
	        net_printf(YYTHD, ER_SP_LABEL_MISMATCH, $5.str);
	        YYABORT;
	      }
1927
	    }
unknown's avatar
unknown committed
1928
	    lex->sphead->backpatch(lex->spcont->pop_label());
1929 1930 1931
	  }
	;

unknown's avatar
unknown committed
1932 1933 1934 1935 1936 1937 1938
sp_opt_label:
	  /* Empty  */
	  { $$.str= NULL; $$.length= 0; }
	| IDENT
	  { $$= $1; }
	;

1939
sp_unlabeled_control:
1940 1941 1942 1943
	  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. */
unknown's avatar
unknown committed
1944 1945
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->last_label();
1946

unknown's avatar
unknown committed
1947 1948 1949
	    lab->isbegin= TRUE;
	    /* Scope duplicate checking */
	    lex->spcont->push_scope();
1950 1951 1952 1953 1954 1955 1956 1957 1958
	  }
	  sp_decls
	  sp_proc_stmts
	  END
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;

unknown's avatar
unknown committed
1959
  	    sp->backpatch(ctx->last_label());	/* We always has a label */
1960 1961
	    ctx->pop_pvar($3.vars);
	    ctx->pop_cond($3.conds);
1962
	    ctx->pop_cursor($3.curs);
1963
	    if ($3.hndlrs)
1964 1965 1966
	      sp->add_instr(new sp_instr_hpop(sp->instructions(),$3.hndlrs));
	    if ($3.curs)
	      sp->add_instr(new sp_instr_cpop(sp->instructions(), $3.curs));
unknown's avatar
unknown committed
1967
	    ctx->pop_scope();
1968 1969
	  }
	| LOOP_SYM
1970 1971 1972 1973 1974 1975 1976 1977 1978
	  sp_proc_stmts END LOOP_SYM
	  {
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lab->ip);

	    lex->sphead->add_instr(i);
	  }
1979
	| WHILE_SYM expr DO_SYM
1980 1981
	  {
	    LEX *lex= Lex;
1982 1983
	    sp_head *sp= lex->sphead;
	    uint ip= sp->instructions();
1984 1985
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, $2);

1986 1987 1988
	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
            sp->add_instr(i);
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
	  }
	  sp_proc_stmts END WHILE_SYM
	  {
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lab->ip);

	    lex->sphead->add_instr(i);
	  }
1999 2000
	| REPEAT_SYM sp_proc_stmts UNTIL_SYM expr END REPEAT_SYM
	  {
2001 2002 2003 2004 2005 2006 2007
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, $4, lab->ip);

            lex->sphead->add_instr(i);
	  }
2008
	;
unknown's avatar
unknown committed
2009 2010

create2:
unknown's avatar
unknown committed
2011
 	'(' create2a {}
2012
	| opt_create_table_options create3 {}
unknown's avatar
unknown committed
2013 2014 2015 2016 2017 2018
	| LIKE table_ident
      	  {
      	    LEX *lex=Lex;
     	    if (!(lex->name= (char *)$2))
              YYABORT;
    	  }
2019 2020 2021 2022 2023 2024
	| '(' LIKE table_ident ')'
      	  {
      	    LEX *lex=Lex;
     	    if (!(lex->name= (char *)$3))
              YYABORT;
    	  }
unknown's avatar
unknown committed
2025
        ;
unknown's avatar
unknown committed
2026

unknown's avatar
unknown committed
2027 2028
create2a:
        field_list ')' opt_create_table_options create3 {}
unknown's avatar
unknown committed
2029
	|  create_select ')' { Select->set_braces(1);} union_opt {}
2030
        ;
unknown's avatar
unknown committed
2031 2032

create3:
2033
	/* empty */ {}
unknown's avatar
unknown committed
2034
	| opt_duplicate opt_as     create_select
unknown's avatar
unknown committed
2035
          { Select->set_braces(0);} union_clause {}
unknown's avatar
unknown committed
2036
	| opt_duplicate opt_as '(' create_select ')'
unknown's avatar
unknown committed
2037
          { Select->set_braces(1);} union_opt {}
2038 2039
        ;

unknown's avatar
unknown committed
2040
create_select:
2041
          SELECT_SYM
unknown's avatar
unknown committed
2042
          {
unknown's avatar
unknown committed
2043 2044
	    LEX *lex=Lex;
	    lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
2045 2046 2047 2048
	    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;
unknown's avatar
unknown committed
2049
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
unknown's avatar
unknown committed
2050
	    mysql_init_select(lex);
unknown's avatar
unknown committed
2051
	    lex->current_select->parsing_place= SELECT_LEX_NODE::SELECT_LIST;
unknown's avatar
unknown committed
2052
          }
unknown's avatar
unknown committed
2053 2054 2055 2056
          select_options select_item_list
	  {
	    Select->parsing_place= SELECT_LEX_NODE::NO_MATTER;
	  }
unknown's avatar
unknown committed
2057
	  opt_select_from
unknown's avatar
unknown committed
2058
	  { Lex->current_select->table_list.push_front(&Lex->save_list); }
unknown's avatar
unknown committed
2059
        ;
unknown's avatar
unknown committed
2060

2061 2062
opt_as:
	/* empty */ {}
unknown's avatar
unknown committed
2063
	| AS	    {};
2064

2065 2066 2067 2068 2069 2070 2071 2072 2073
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
2074 2075
	  opt_default COLLATE_SYM collation_name_or_default	
	  { Lex->create_info.default_table_charset=$3; }
2076
	| opt_default charset charset_name_or_default
2077
	  { Lex->create_info.default_table_charset=$3; }
2078
	;
2079

unknown's avatar
unknown committed
2080
opt_table_options:
unknown's avatar
unknown committed
2081
	/* empty */	 { $$= 0; }
unknown's avatar
unknown committed
2082
	| table_options  { $$= $1;};
unknown's avatar
unknown committed
2083 2084 2085

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

unknown's avatar
unknown committed
2088
table_option:
unknown's avatar
unknown committed
2089
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
unknown's avatar
unknown committed
2090 2091

opt_if_not_exists:
unknown's avatar
unknown committed
2092
	/* empty */	 { $$= 0; }
unknown's avatar
unknown committed
2093
	| IF NOT EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
unknown's avatar
unknown committed
2094 2095 2096

opt_create_table_options:
	/* empty */
unknown's avatar
unknown committed
2097
	| create_table_options;
unknown's avatar
unknown committed
2098

unknown's avatar
unknown committed
2099 2100 2101 2102
create_table_options_space_separated:
	create_table_option
	| create_table_option create_table_options_space_separated;

unknown's avatar
unknown committed
2103 2104
create_table_options:
	create_table_option
2105
	| create_table_option     create_table_options
unknown's avatar
unknown committed
2106
	| create_table_option ',' create_table_options;
unknown's avatar
unknown committed
2107 2108

create_table_option:
unknown's avatar
unknown committed
2109 2110
	ENGINE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; }
	| TYPE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; WARN_DEPRECATED("TYPE=database_engine","ENGINE=database_engine"); }
2111 2112 2113
	| 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;}
unknown's avatar
unknown committed
2114 2115
	| PASSWORD opt_equal TEXT_STRING_sys	{ Lex->create_info.password=$3.str; }
	| COMMENT_SYM opt_equal TEXT_STRING_sys	{ Lex->create_info.comment=$3.str; }
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
	| PACK_KEYS_SYM opt_equal ULONG_NUM	{ Lex->create_info.table_options|= $3 ? HA_OPTION_PACK_KEYS : HA_OPTION_NO_PACK_KEYS; Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;}
	| PACK_KEYS_SYM opt_equal DEFAULT	{ Lex->create_info.table_options&= ~(HA_OPTION_PACK_KEYS | HA_OPTION_NO_PACK_KEYS); Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;}
	| CHECKSUM_SYM opt_equal ULONG_NUM	{ Lex->create_info.table_options|= $3 ? HA_OPTION_CHECKSUM : HA_OPTION_NO_CHECKSUM; }
	| DELAY_KEY_WRITE_SYM opt_equal ULONG_NUM { Lex->create_info.table_options|= $3 ? HA_OPTION_DELAY_KEY_WRITE : HA_OPTION_NO_DELAY_KEY_WRITE; }
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3; }
	| RAID_TYPE opt_equal raid_types	{ Lex->create_info.raid_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| RAID_CHUNKS opt_equal ULONG_NUM	{ Lex->create_info.raid_chunks= $3; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| RAID_CHUNKSIZE opt_equal ULONG_NUM	{ Lex->create_info.raid_chunksize= $3*RAID_BLOCK_SIZE; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| UNION_SYM opt_equal '(' table_list ')'
2126 2127 2128
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
2129 2130
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
2131 2132
	    lex->create_info.merge_list.elements--;
	    lex->create_info.merge_list.first= (byte*) (table_list->next);
2133 2134
	    lex->select_lex.table_list.elements=1;
	    lex->select_lex.table_list.next= (byte**) &(table_list->next);
2135
	    table_list->next=0;
2136
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
2137
	  }
2138
	| DEFAULT charset opt_equal charset_name_or_default
2139
	  {
2140 2141 2142 2143 2144 2145
	    Lex->create_info.default_table_charset= $4;
	    Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
	  }
	| charset opt_equal charset_name_or_default
	  {
	    Lex->create_info.table_charset= $3;
2146 2147
	    Lex->create_info.used_fields|= HA_CREATE_USED_CHARSET;
	  }
2148 2149 2150 2151 2152
	| DEFAULT COLLATE_SYM opt_equal collation_name_or_default
	  {
	    Lex->create_info.table_charset= $4;
	    Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
	  }
2153 2154 2155 2156 2157
	| COLLATE_SYM opt_equal collation_name_or_default
	  {
	    Lex->create_info.table_charset= $3;
	    Lex->create_info.used_fields|= HA_CREATE_USED_CHARSET;
	  }
2158
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
unknown's avatar
unknown committed
2159 2160 2161
	| DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys
	  { Lex->create_info.data_file_name= $4.str; }
	| INDEX DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.index_file_name= $4.str; };
unknown's avatar
unknown committed
2162

unknown's avatar
unknown committed
2163
storage_engines:
2164 2165 2166 2167
	ident_or_text
	{
	  $$ = ha_resolve_by_name($1.str,$1.length);
	  if ($$ == DB_TYPE_UNKNOWN) {
unknown's avatar
unknown committed
2168
	    net_printf(YYTHD, ER_UNKNOWN_STORAGE_ENGINE, $1.str);
2169 2170 2171
	    YYABORT;
	  }
	};
unknown's avatar
unknown committed
2172 2173 2174 2175 2176

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
unknown's avatar
unknown committed
2177
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; };
unknown's avatar
unknown committed
2178 2179 2180 2181

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

2184 2185 2186
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
unknown's avatar
unknown committed
2187
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
2188

unknown's avatar
unknown committed
2189
opt_select_from:
2190
	opt_limit_clause {}
unknown's avatar
unknown committed
2191
	| FROM DUAL_SYM {}
unknown's avatar
unknown committed
2192
	| select_from select_lock_type;
unknown's avatar
unknown committed
2193 2194

udf_func_type:
2195
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
unknown's avatar
unknown committed
2196
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
unknown's avatar
unknown committed
2197 2198 2199 2200

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
unknown's avatar
unknown committed
2201
	| INT_SYM {$$ = (int) INT_RESULT; };
unknown's avatar
unknown committed
2202 2203 2204

field_list:
	  field_list_item
unknown's avatar
unknown committed
2205
	| field_list ',' field_list_item;
unknown's avatar
unknown committed
2206 2207 2208


field_list_item:
unknown's avatar
unknown committed
2209
	   column_def
2210 2211 2212 2213
         | key_def
         ;

column_def:
2214
	  field_spec check_constraint
unknown's avatar
unknown committed
2215 2216 2217 2218
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
unknown's avatar
unknown committed
2219
	;
2220 2221

key_def:
unknown's avatar
unknown committed
2222
	key_type opt_ident key_alg '(' key_list ')'
unknown's avatar
unknown committed
2223
	  {
unknown's avatar
unknown committed
2224
	    LEX *lex=Lex;
2225
	    lex->key_list.push_back(new Key($1,$2, $3, lex->col_list));
unknown's avatar
unknown committed
2226
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
2227
	  }
2228 2229 2230 2231 2232 2233 2234
	| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
	  {
	    LEX *lex=Lex;
	    const char *key_name= $3 ? $3:$1;
	    lex->key_list.push_back(new Key($2, key_name, $4, lex->col_list));
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
2235
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
unknown's avatar
unknown committed
2236
	  {
2237 2238 2239 2240 2241 2242 2243 2244
	    LEX *lex=Lex;
	    lex->key_list.push_back(new foreign_key($4, lex->col_list,
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
2245
	  }
2246
	| opt_constraint check_constraint
unknown's avatar
unknown committed
2247 2248
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
2249 2250 2251 2252 2253 2254 2255
	  }
	;

check_constraint:
	/* empty */
	| CHECK_SYM expr
	;
unknown's avatar
unknown committed
2256 2257

opt_constraint:
2258 2259
	/* empty */		{ $$=(char*) 0; }
	| CONSTRAINT opt_ident	{ $$=$2; };
unknown's avatar
unknown committed
2260 2261 2262 2263

field_spec:
	field_ident
	 {
unknown's avatar
unknown committed
2264 2265
	   LEX *lex=Lex;
	   lex->length=lex->dec=0; lex->type=0; lex->interval=0;
unknown's avatar
unknown committed
2266 2267
	   lex->default_value=0;
	   lex->comment=0;
2268
	   lex->charset=NULL;
unknown's avatar
unknown committed
2269 2270 2271
	 }
	type opt_attribute
	{
unknown's avatar
unknown committed
2272
	  LEX *lex=Lex;
unknown's avatar
unknown committed
2273
	  if (add_field_to_list(lex->thd, $1.str,
unknown's avatar
unknown committed
2274
				(enum enum_field_types) $3,
unknown's avatar
unknown committed
2275
				lex->length,lex->dec,lex->type,
2276
				lex->default_value, lex->comment,
unknown's avatar
unknown committed
2277 2278
				lex->change,lex->interval,lex->charset,
				lex->uint_geom_type))
unknown's avatar
unknown committed
2279
	    YYABORT;
unknown's avatar
unknown committed
2280
	};
unknown's avatar
unknown committed
2281 2282

type:
2283
	int_type opt_len field_options	{ $$=$1; }
unknown's avatar
unknown committed
2284 2285 2286 2287 2288 2289
	| real_type opt_precision field_options { $$=$1; }
	| FLOAT_SYM float_options field_options { $$=FIELD_TYPE_FLOAT; }
	| BIT_SYM opt_len		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
	| BOOL_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
2290 2291
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
2292
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
unknown's avatar
unknown committed
2293 2294 2295
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
2296
	| nchar '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
2297
					  $$=FIELD_TYPE_STRING;
2298
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
2299
	| nchar				{ Lex->length=(char*) "1";
unknown's avatar
unknown committed
2300
					  $$=FIELD_TYPE_STRING;
2301
					  Lex->charset=national_charset_info; }
2302
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
2303
					  Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2304 2305 2306
					  $$=FIELD_TYPE_STRING; }
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
					  $$=FIELD_TYPE_VAR_STRING; }
unknown's avatar
unknown committed
2307 2308
	| nvarchar '(' NUM ')'		{ Lex->length=$3.str;
					  $$=FIELD_TYPE_VAR_STRING;
2309
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
2310
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
unknown's avatar
unknown committed
2311
					  Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2312
					  $$=FIELD_TYPE_VAR_STRING; }
2313
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
unknown's avatar
unknown committed
2314 2315
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
2316 2317
	| TIMESTAMP
	  {
2318
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
2319 2320
	      $$=FIELD_TYPE_DATETIME;
	    else
2321
	      $$=FIELD_TYPE_TIMESTAMP;
2322
	   }
unknown's avatar
unknown committed
2323 2324 2325
	| TIMESTAMP '(' NUM ')'		{ Lex->length=$3.str;
					  $$=FIELD_TYPE_TIMESTAMP; }
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
unknown's avatar
unknown committed
2326
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2327
					  $$=FIELD_TYPE_TINY_BLOB; }
unknown's avatar
unknown committed
2328
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2329
					  $$=FIELD_TYPE_BLOB; }
unknown's avatar
unknown committed
2330
	| GEOMETRY_SYM			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2331
					  Lex->uint_geom_type= (uint) Field::GEOM_GEOMETRY;
unknown's avatar
unknown committed
2332
					  $$=FIELD_TYPE_GEOMETRY; }
2333
	| GEOMETRYCOLLECTION		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2334
					  Lex->uint_geom_type= (uint) Field::GEOM_GEOMETRYCOLLECTION;
2335
					  $$=FIELD_TYPE_GEOMETRY; }
2336
	| POINT_SYM			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2337
					  Lex->uint_geom_type= (uint) Field::GEOM_POINT;
2338 2339
					  $$=FIELD_TYPE_GEOMETRY; }
	| MULTIPOINT			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2340
					  Lex->uint_geom_type= (uint) Field::GEOM_MULTIPOINT;
2341 2342
					  $$=FIELD_TYPE_GEOMETRY; }
	| LINESTRING			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2343
					  Lex->uint_geom_type= (uint) Field::GEOM_LINESTRING;
2344 2345
					  $$=FIELD_TYPE_GEOMETRY; }
	| MULTILINESTRING		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2346
					  Lex->uint_geom_type= (uint) Field::GEOM_MULTILINESTRING;
2347 2348
					  $$=FIELD_TYPE_GEOMETRY; }
	| POLYGON			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2349
					  Lex->uint_geom_type= (uint) Field::GEOM_POLYGON;
2350 2351
					  $$=FIELD_TYPE_GEOMETRY; }
	| MULTIPOLYGON			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2352
					  Lex->uint_geom_type= (uint) Field::GEOM_MULTIPOLYGON;
2353
					  $$=FIELD_TYPE_GEOMETRY; }
unknown's avatar
unknown committed
2354
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2355
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
2356
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2357
					  $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
2358
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2359
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
2360 2361
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
2362
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
2363 2364
	| MEDIUMTEXT opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| LONGTEXT opt_binary		{ $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
2365 2366 2367 2368
	| DECIMAL_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
	| NUMERIC_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
2369 2370
	| FIXED_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
2371
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
unknown's avatar
unknown committed
2372
	  {
unknown's avatar
unknown committed
2373 2374
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
unknown's avatar
unknown committed
2375 2376
	    $$=FIELD_TYPE_ENUM;
	  }
2377
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
unknown's avatar
unknown committed
2378
	  {
unknown's avatar
unknown committed
2379 2380
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
unknown's avatar
unknown committed
2381
	    $$=FIELD_TYPE_SET;
2382
	  }
2383
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
2384 2385 2386 2387 2388 2389
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
2390
	;
unknown's avatar
unknown committed
2391 2392 2393

char:
	CHAR_SYM {}
unknown's avatar
unknown committed
2394 2395 2396 2397 2398 2399
	;

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

unknown's avatar
unknown committed
2401 2402 2403
varchar:
	char VARYING {}
	| VARCHAR {}
unknown's avatar
unknown committed
2404 2405 2406 2407
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
unknown's avatar
unknown committed
2408
	| NVARCHAR_SYM {}
unknown's avatar
unknown committed
2409 2410 2411 2412
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
unknown's avatar
unknown committed
2413 2414 2415 2416 2417 2418

int_type:
	INT_SYM		{ $$=FIELD_TYPE_LONG; }
	| TINYINT	{ $$=FIELD_TYPE_TINY; }
	| SMALLINT	{ $$=FIELD_TYPE_SHORT; }
	| MEDIUMINT	{ $$=FIELD_TYPE_INT24; }
unknown's avatar
unknown committed
2419
	| BIGINT	{ $$=FIELD_TYPE_LONGLONG; };
unknown's avatar
unknown committed
2420 2421

real_type:
2422
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
unknown's avatar
unknown committed
2423 2424
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
unknown's avatar
unknown committed
2425
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
unknown's avatar
unknown committed
2426 2427 2428 2429 2430


float_options:
	/* empty */		{}
	| '(' NUM ')'		{ Lex->length=$2.str; }
unknown's avatar
unknown committed
2431
	| precision		{};
unknown's avatar
unknown committed
2432 2433 2434 2435 2436 2437

precision:
	'(' NUM ',' NUM ')'
	{
	  LEX *lex=Lex;
	  lex->length=$2.str; lex->dec=$4.str;
unknown's avatar
unknown committed
2438
	};
unknown's avatar
unknown committed
2439 2440 2441

field_options:
	/* empty */		{}
unknown's avatar
unknown committed
2442
	| field_opt_list	{};
unknown's avatar
unknown committed
2443 2444 2445

field_opt_list:
	field_opt_list field_option {}
unknown's avatar
unknown committed
2446
	| field_option {};
unknown's avatar
unknown committed
2447 2448

field_option:
unknown's avatar
unknown committed
2449
	SIGNED_SYM	{}
2450
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
unknown's avatar
unknown committed
2451
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
unknown's avatar
unknown committed
2452 2453

opt_len:
2454 2455
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
unknown's avatar
unknown committed
2456 2457 2458

opt_precision:
	/* empty */	{}
unknown's avatar
unknown committed
2459
	| precision	{};
unknown's avatar
unknown committed
2460 2461 2462

opt_attribute:
	/* empty */ {}
unknown's avatar
unknown committed
2463
	| opt_attribute_list {};
unknown's avatar
unknown committed
2464 2465 2466

opt_attribute_list:
	opt_attribute_list attribute {}
unknown's avatar
unknown committed
2467
	| attribute;
unknown's avatar
unknown committed
2468 2469 2470 2471

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
	| NOT NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
unknown's avatar
unknown committed
2472
	| DEFAULT signed_literal { Lex->default_value=$2; }
unknown's avatar
unknown committed
2473
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
2474
	| SERIAL_SYM DEFAULT VALUE_SYM
unknown's avatar
unknown committed
2475 2476
	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; }
	| opt_primary KEY_SYM { Lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; }
unknown's avatar
unknown committed
2477
	| UNIQUE_SYM	  { Lex->type|= UNIQUE_FLAG; }
2478
	| UNIQUE_SYM KEY_SYM { Lex->type|= UNIQUE_KEY_FLAG; }
unknown's avatar
unknown committed
2479
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= &$2; }
unknown's avatar
unknown committed
2480 2481
	| COLLATE_SYM collation_name
	  {
unknown's avatar
unknown committed
2482
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
unknown's avatar
unknown committed
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
	    {
	      net_printf(YYTHD,ER_COLLATION_CHARSET_MISMATCH,
			 $2->name,Lex->charset->csname);
	      YYABORT;
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
unknown's avatar
unknown committed
2494

2495 2496 2497 2498
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
unknown's avatar
unknown committed
2499

2500
charset_name:
2501
	ident_or_text
2502
	{
unknown's avatar
unknown committed
2503
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
2504
	  {
2505
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,$1.str);
2506 2507
	    YYABORT;
	  }
unknown's avatar
unknown committed
2508 2509 2510
	}
	| BINARY { $$= &my_charset_bin; }
	;
2511

2512 2513 2514
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
unknown's avatar
unknown committed
2515

unknown's avatar
unknown committed
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533

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)))
	  {
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,$1.str);
	    YYABORT;
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

2534
collation_name:
2535
	ident_or_text
2536 2537 2538
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
2539
	    net_printf(YYTHD,ER_UNKNOWN_COLLATION,$1.str);
2540 2541 2542 2543
	    YYABORT;
	  }
	};

2544 2545
opt_collate:
	/* empty */	{ $$=NULL; }
unknown's avatar
unknown committed
2546
	| COLLATE_SYM collation_name_or_default { $$=$2; }
2547 2548
	;

2549 2550 2551 2552
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

2553 2554 2555 2556
opt_default:
	/* empty */	{}
	| DEFAULT	{};

unknown's avatar
unknown committed
2557
opt_binary:
2558
	/* empty */			{ Lex->charset=NULL; }
unknown's avatar
unknown committed
2559 2560 2561
	| ASCII_SYM			{ Lex->charset=&my_charset_latin1; }
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
	| BINARY			{ Lex->charset=&my_charset_bin; }
unknown's avatar
unknown committed
2562 2563
	| UNICODE_SYM
	{
2564
	  if (!(Lex->charset=get_charset_by_csname("ucs2",MY_CS_PRIMARY,MYF(0))))
unknown's avatar
unknown committed
2565 2566 2567 2568 2569
	  {
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,"ucs2");
	    YYABORT;
	  }
	}
2570
	| charset charset_name	{ Lex->charset=$2; } ;
unknown's avatar
unknown committed
2571

unknown's avatar
unknown committed
2572 2573 2574
opt_primary:
	/* empty */
	| PRIMARY_SYM
2575
	;
unknown's avatar
unknown committed
2576

unknown's avatar
unknown committed
2577
references:
2578 2579
	REFERENCES table_ident
	{
2580
	  LEX *lex=Lex;
2581 2582 2583 2584 2585 2586
	  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
2587
	};
2588

2589
opt_ref_list:
unknown's avatar
unknown committed
2590
	/* empty */ opt_on_delete {}
unknown's avatar
unknown committed
2591
	| '(' ref_list ')' opt_on_delete {};
2592 2593 2594

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

unknown's avatar
unknown committed
2597 2598 2599

opt_on_delete:
	/* empty */ {}
unknown's avatar
unknown committed
2600
	| opt_on_delete_list {};
unknown's avatar
unknown committed
2601 2602 2603

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
unknown's avatar
unknown committed
2604
	| opt_on_delete_item {};
unknown's avatar
unknown committed
2605 2606

opt_on_delete_item:
2607 2608 2609 2610
	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
2611
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
unknown's avatar
unknown committed
2612 2613

delete_option:
2614 2615 2616 2617
	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
2618
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
unknown's avatar
unknown committed
2619 2620

key_type:
2621
	key_or_index			    { $$= Key::MULTIPLE; }
2622 2623
	| FULLTEXT_SYM			    { $$= Key::FULLTEXT; }
	| FULLTEXT_SYM key_or_index	    { $$= Key::FULLTEXT; }
unknown's avatar
unknown committed
2624
	| SPATIAL_SYM			    { $$= Key::SPATIAL; }
2625 2626 2627 2628 2629 2630
	| SPATIAL_SYM key_or_index	    { $$= Key::SPATIAL; };

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
	| UNIQUE_SYM	    { $$= Key::UNIQUE; }
	| UNIQUE_SYM key_or_index { $$= Key::UNIQUE; };
unknown's avatar
unknown committed
2631 2632 2633

key_or_index:
	KEY_SYM {}
unknown's avatar
unknown committed
2634
	| INDEX {};
unknown's avatar
unknown committed
2635

2636 2637 2638 2639 2640
opt_keys_or_index:
	/* empty */ {}
	| keys_or_index
	;

unknown's avatar
unknown committed
2641 2642 2643
keys_or_index:
	KEYS {}
	| INDEX {}
unknown's avatar
unknown committed
2644
	| INDEXES {};
unknown's avatar
unknown committed
2645

2646
opt_unique_or_fulltext:
unknown's avatar
unknown committed
2647 2648
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
unknown's avatar
unknown committed
2649 2650 2651
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
	| SPATIAL_SYM	{ $$= Key::SPATIAL; }
        ;
unknown's avatar
unknown committed
2652 2653

key_alg:
2654
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
2655
	| USING opt_btree_or_rtree { $$= $2; }
unknown's avatar
unknown committed
2656
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
unknown's avatar
unknown committed
2657 2658 2659 2660

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
	| RTREE_SYM	{ $$= HA_KEY_ALG_RTREE; }
unknown's avatar
unknown committed
2661
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
unknown's avatar
unknown committed
2662 2663 2664

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
unknown's avatar
unknown committed
2665
	| key_part order_dir		{ Lex->col_list.push_back($1); };
unknown's avatar
unknown committed
2666 2667 2668

key_part:
	ident			{ $$=new key_part_spec($1.str); }
unknown's avatar
unknown committed
2669
	| ident '(' NUM ')'	{ $$=new key_part_spec($1.str,(uint) atoi($3.str)); };
unknown's avatar
unknown committed
2670 2671 2672

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

2675 2676 2677
opt_component:
	/* empty */	 { $$.str= 0; $$.length= 0; }
	| '.' ident	 { $$=$2; };
2678

unknown's avatar
unknown committed
2679 2680
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
unknown's avatar
unknown committed
2681
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
unknown's avatar
unknown committed
2682 2683 2684 2685 2686 2687 2688 2689

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
2690
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
2691
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
2692 2693
	  lex->sql_command = SQLCOM_ALTER_TABLE;
	  lex->name=0;
unknown's avatar
unknown committed
2694 2695
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
unknown's avatar
unknown committed
2696 2697 2698 2699 2700 2701 2702
	    YYABORT;
	  lex->drop_primary=0;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
	  lex->drop_list.empty();
	  lex->alter_list.empty();
2703
          lex->select_lex.init_order();
2704
	  lex->select_lex.db=lex->name=0;
2705
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
unknown's avatar
unknown committed
2706
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
2707
	  lex->create_info.default_table_charset= thd->variables.collation_database;
2708
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
2709
          lex->alter_keys_onoff=LEAVE_AS_IS;
unknown's avatar
unknown committed
2710
	  lex->tablespace_op=NO_TABLESPACE_OP;
2711
          lex->simple_alter=1;
unknown's avatar
unknown committed
2712
	}
2713 2714
	alter_list
	{}
2715
	| ALTER DATABASE ident opt_create_database_options
2716 2717 2718 2719
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
	    lex->name=$3.str;
2720
	  }
2721 2722 2723 2724 2725
	| ALTER PROCEDURE ident
	  {
	    LEX *lex= Lex;

	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
2726
	    lex->name= 0;
2727 2728
          }
	  sp_a_chistics
2729
	  {
2730
	    THD *thd= YYTHD;
2731
	    LEX *lex=Lex;
2732

2733
	    lex->sql_command= SQLCOM_ALTER_PROCEDURE;
2734
	    lex->udf.name= $3;
2735
	  }
2736 2737 2738 2739 2740 2741 2742 2743
	| ALTER FUNCTION_SYM ident
	  {
	    LEX *lex= Lex;

	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	    lex->name= 0;
          }
	  sp_a_chistics
2744
	  {
2745
	    THD *thd= YYTHD;
2746
	    LEX *lex=Lex;
2747

2748 2749 2750
	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
	    lex->udf.name= $3;
	  }
2751
	;
2752

unknown's avatar
unknown committed
2753
alter_list:
unknown's avatar
unknown committed
2754 2755
	| DISCARD TABLESPACE { Lex->tablespace_op=DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->tablespace_op=IMPORT_TABLESPACE; }
unknown's avatar
unknown committed
2756
        | alter_list_item
unknown's avatar
unknown committed
2757
	| alter_list ',' alter_list_item;
unknown's avatar
unknown committed
2758 2759

add_column:
unknown's avatar
unknown committed
2760
	ADD opt_column { Lex->change=0; };
unknown's avatar
unknown committed
2761 2762

alter_list_item:
2763 2764
	add_column column_def opt_place { Lex->simple_alter=0; }
	| ADD key_def { Lex->simple_alter=0; }
2765
	| add_column '(' field_list ')'      { Lex->simple_alter=0; }
unknown's avatar
unknown committed
2766 2767 2768 2769 2770
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
	     lex->change= $3.str; lex->simple_alter=0;
	  }
2771
          field_spec opt_place
unknown's avatar
unknown committed
2772 2773 2774 2775
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
            lex->length=lex->dec=0; lex->type=0; lex->interval=0;
unknown's avatar
unknown committed
2776 2777
            lex->default_value=0;
	    lex->comment=0;
2778
	    lex->charset= NULL;
unknown's avatar
unknown committed
2779
            lex->simple_alter=0;
unknown's avatar
unknown committed
2780 2781 2782 2783
          }
          type opt_attribute
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
2784
            if (add_field_to_list(lex->thd,$3.str,
unknown's avatar
unknown committed
2785 2786 2787
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
                                  lex->default_value, lex->comment,
unknown's avatar
unknown committed
2788 2789
				  $3.str, lex->interval, lex->charset,
				  lex->uint_geom_type))
unknown's avatar
unknown committed
2790 2791 2792
	       YYABORT;
          }
          opt_place
unknown's avatar
unknown committed
2793
	| DROP opt_column field_ident opt_restrict
unknown's avatar
unknown committed
2794 2795 2796 2797 2798 2799 2800 2801 2802 2803
	  {
	    LEX *lex=Lex;
	    lex->drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
					    $3.str)); lex->simple_alter=0;
	  }
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
	    lex->drop_primary=1; lex->simple_alter=0;
	  }
2804
	| DROP FOREIGN KEY_SYM opt_ident { Lex->simple_alter=0; }
unknown's avatar
unknown committed
2805
	| DROP key_or_index field_ident
unknown's avatar
unknown committed
2806 2807 2808 2809 2810 2811
	  {
	    LEX *lex=Lex;
	    lex->drop_list.push_back(new Alter_drop(Alter_drop::KEY,
						    $3.str));
	    lex->simple_alter=0;
	  }
2812 2813
	| DISABLE_SYM KEYS { Lex->alter_keys_onoff=DISABLE; }
	| ENABLE_SYM KEYS  { Lex->alter_keys_onoff=ENABLE; }
unknown's avatar
unknown committed
2814
	| ALTER opt_column field_ident SET DEFAULT signed_literal
unknown's avatar
unknown committed
2815 2816 2817 2818 2819
	  {
	    LEX *lex=Lex;
	    lex->alter_list.push_back(new Alter_column($3.str,$6));
	    lex->simple_alter=0;
	  }
unknown's avatar
unknown committed
2820
	| ALTER opt_column field_ident DROP DEFAULT
unknown's avatar
unknown committed
2821 2822 2823 2824 2825
	  {
	    LEX *lex=Lex;
	    lex->alter_list.push_back(new Alter_column($3.str,(Item*) 0));
	    lex->simple_alter=0;
	  }
2826
	| RENAME opt_to table_ident
2827
	  {
2828
	    LEX *lex=Lex;
2829
	    lex->select_lex.db=$3->db.str;
2830
	    lex->name= $3->table.str;
2831
	  }
unknown's avatar
unknown committed
2832
        | create_table_options_space_separated { Lex->simple_alter=0; }
unknown's avatar
unknown committed
2833
	| order_clause         { Lex->simple_alter=0; };
unknown's avatar
unknown committed
2834 2835 2836

opt_column:
	/* empty */	{}
unknown's avatar
unknown committed
2837
	| COLUMN_SYM	{};
unknown's avatar
unknown committed
2838 2839 2840

opt_ignore:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
unknown's avatar
unknown committed
2841
	| IGNORE_SYM	{ Lex->duplicates=DUP_IGNORE; };
unknown's avatar
unknown committed
2842 2843 2844 2845

opt_restrict:
	/* empty */	{}
	| RESTRICT	{}
unknown's avatar
unknown committed
2846
	| CASCADE	{};
unknown's avatar
unknown committed
2847 2848 2849 2850

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
unknown's avatar
unknown committed
2851
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
unknown's avatar
unknown committed
2852 2853 2854 2855

opt_to:
	/* empty */	{}
	| TO_SYM	{}
2856
	| EQ		{}
unknown's avatar
unknown committed
2857
	| AS		{};
unknown's avatar
unknown committed
2858

unknown's avatar
unknown committed
2859
/*
2860 2861
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
  To use UNTIL, one must use START SLAVE, not SLAVE START.
unknown's avatar
unknown committed
2862 2863
*/

unknown's avatar
unknown committed
2864
slave:
2865 2866 2867 2868 2869 2870 2871 2872 2873 2874
	  START_SYM SLAVE slave_thread_opts 
          {
	    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));
          }
          slave_until
          {}
unknown's avatar
unknown committed
2875 2876 2877 2878 2879 2880
        | STOP_SYM SLAVE slave_thread_opts
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_STOP;
	    lex->type = 0;
          }
2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
         }
	| SLAVE STOP_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_STOP;
	   lex->type = 0;
         }
        ;

unknown's avatar
unknown committed
2895

unknown's avatar
unknown committed
2896 2897 2898 2899 2900
start:
	START_SYM TRANSACTION_SYM { Lex->sql_command = SQLCOM_BEGIN;}
	{}
	;

unknown's avatar
unknown committed
2901
slave_thread_opts:
2902 2903
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
2904
        {}
unknown's avatar
unknown committed
2905
	;
2906 2907

slave_thread_opt_list:
unknown's avatar
unknown committed
2908
	slave_thread_opt
2909 2910
	| slave_thread_opt_list ',' slave_thread_opt
	;
2911 2912

slave_thread_opt:
2913
	/*empty*/	{}
unknown's avatar
unknown committed
2914
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
unknown's avatar
unknown committed
2915
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
unknown's avatar
unknown committed
2916
	;
2917

2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939
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)))
            {
               send_error(lex->thd, ER_BAD_SLAVE_UNTIL_COND);
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


unknown's avatar
unknown committed
2940 2941 2942 2943 2944
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
	}
unknown's avatar
unknown committed
2945
	table_list FROM TEXT_STRING_sys
unknown's avatar
unknown committed
2946 2947
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
2948
        };
unknown's avatar
unknown committed
2949

unknown's avatar
unknown committed
2950 2951 2952 2953 2954
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
	}
unknown's avatar
unknown committed
2955
	table_list TO_SYM TEXT_STRING_sys
unknown's avatar
unknown committed
2956 2957
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
2958
        };
unknown's avatar
unknown committed
2959

2960 2961 2962 2963 2964 2965
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
2966 2967
	table_list opt_checksum_type
        {}
2968 2969
	;

2970 2971 2972 2973 2974 2975
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
2976
repair:
2977
	REPAIR opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
2978
	{
unknown's avatar
unknown committed
2979 2980
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
2981
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
2982
	   lex->check_opt.init();
unknown's avatar
unknown committed
2983
	}
2984 2985 2986
	table_list opt_mi_repair_type
	{}
	;
unknown's avatar
unknown committed
2987

unknown's avatar
unknown committed
2988
opt_mi_repair_type:
unknown's avatar
unknown committed
2989
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
2990
	| mi_repair_types {};
unknown's avatar
unknown committed
2991

unknown's avatar
unknown committed
2992 2993
mi_repair_types:
	mi_repair_type {}
unknown's avatar
unknown committed
2994
	| mi_repair_type mi_repair_types {};
2995

unknown's avatar
unknown committed
2996
mi_repair_type:
unknown's avatar
unknown committed
2997
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
2998
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
unknown's avatar
unknown committed
2999
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
unknown's avatar
unknown committed
3000 3001

analyze:
3002
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3003
	{
unknown's avatar
unknown committed
3004 3005
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
3006
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3007
	   lex->check_opt.init();
unknown's avatar
unknown committed
3008
	}
3009 3010 3011
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3012 3013

check:
unknown's avatar
unknown committed
3014
	CHECK_SYM table_or_tables
unknown's avatar
unknown committed
3015
	{
unknown's avatar
unknown committed
3016 3017 3018
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECK;
	   lex->check_opt.init();
unknown's avatar
unknown committed
3019
	}
3020 3021 3022
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3023

unknown's avatar
unknown committed
3024 3025
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
3026
	| mi_check_types {};
unknown's avatar
unknown committed
3027 3028 3029

mi_check_types:
	mi_check_type {}
unknown's avatar
unknown committed
3030
	| mi_check_type mi_check_types {};
unknown's avatar
unknown committed
3031 3032 3033 3034 3035 3036

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

unknown's avatar
unknown committed
3039
optimize:
3040
	OPTIMIZE opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3041
	{
unknown's avatar
unknown committed
3042 3043
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
unknown's avatar
unknown committed
3044
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3045
	   lex->check_opt.init();
unknown's avatar
unknown committed
3046
	}
3047 3048 3049
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3050

3051 3052 3053
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
3054
	| LOCAL_SYM  { $$= 1; }
3055 3056
	;

3057 3058 3059 3060 3061
rename:
	RENAME table_or_tables
	{
	   Lex->sql_command=SQLCOM_RENAME_TABLE;
	}
3062 3063 3064
	table_to_table_list
	{}
	;
3065 3066 3067

table_to_table_list:
	table_to_table
unknown's avatar
unknown committed
3068
	| table_to_table_list ',' table_to_table;
3069 3070 3071

table_to_table:
	table_ident TO_SYM table_ident
3072
	{
unknown's avatar
unknown committed
3073
	  LEX *lex=Lex;
unknown's avatar
unknown committed
3074
	  SELECT_LEX *sl= lex->current_select;
unknown's avatar
unknown committed
3075 3076 3077 3078
	  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))
3079
	    YYABORT;
3080
	};
3081

unknown's avatar
unknown committed
3082
keycache:
3083
        CACHE_SYM INDEX keycache_list IN_SYM key_cache_name
unknown's avatar
unknown committed
3084 3085
        {
          LEX *lex=Lex;
3086 3087
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
	  lex->name_and_length= $5;
unknown's avatar
unknown committed
3088 3089 3090 3091 3092 3093 3094 3095
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
3096
        table_ident cache_keys_spec
unknown's avatar
unknown committed
3097 3098 3099 3100 3101 3102
        {
          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(),
3103
                                      (List<String> *)0))
unknown's avatar
unknown committed
3104 3105 3106 3107
            YYABORT;
        }
        ;

3108 3109 3110
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
3111
	;
3112

unknown's avatar
unknown committed
3113
preload:
unknown's avatar
unknown committed
3114
	LOAD INDEX INTO CACHE_SYM
unknown's avatar
unknown committed
3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127
	{
	  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
3128
	table_ident cache_keys_spec opt_ignore_leaves
unknown's avatar
unknown committed
3129 3130 3131
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
unknown's avatar
unknown committed
3132 3133
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
unknown's avatar
unknown committed
3134 3135 3136 3137 3138 3139
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

3140
cache_keys_spec:
3141
        { Select->interval_list.empty(); }
3142 3143 3144 3145 3146 3147 3148
        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
3149

unknown's avatar
unknown committed
3150
cache_key_list_or_empty:
3151 3152 3153 3154 3155 3156
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
	| opt_keys_or_index '(' key_usage_list2 ')'
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
3157 3158
	;

unknown's avatar
unknown committed
3159
opt_ignore_leaves:
unknown's avatar
unknown committed
3160 3161 3162 3163 3164
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

unknown's avatar
unknown committed
3165
/*
unknown's avatar
unknown committed
3166
  Select : retrieve data from table
unknown's avatar
unknown committed
3167 3168 3169 3170
*/


select:
3171 3172 3173 3174 3175
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
	  lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE;
unknown's avatar
unknown committed
3176 3177
	}
	;
unknown's avatar
unknown committed
3178

3179
/* Need select_init2 for subselects. */
unknown's avatar
unknown committed
3180
select_init:
3181
	SELECT_SYM select_init2
3182
	|
3183 3184
	'(' SELECT_SYM select_part2 ')'
	  {
3185
	    LEX *lex= Lex;
unknown's avatar
unknown committed
3186
            SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
3187
	    if (sel->set_braces(1))
3188 3189 3190 3191
	    {
	      send_error(lex->thd, ER_SYNTAX_ERROR);
	      YYABORT;
	    }
3192 3193
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
3194 3195 3196 3197
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
3198
            /* select in braces, can't contain global parameters */
unknown's avatar
unknown committed
3199 3200 3201
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
unknown's avatar
unknown committed
3202
          } union_opt;
3203

3204 3205
select_init2:
	select_part2
3206
        {
3207
	  LEX *lex= Lex;
unknown's avatar
unknown committed
3208
          SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
3209
          if (lex->current_select->set_braces(0))
3210 3211 3212 3213
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
3214 3215
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
3216 3217 3218 3219
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
3220
	}
3221
	union_clause
3222 3223
	;

3224
select_part2:
unknown's avatar
unknown committed
3225 3226
	{
	  LEX *lex=Lex;
unknown's avatar
unknown committed
3227
	  SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
3228
	  if (lex->current_select == &lex->select_lex)
3229
	    lex->lock_option= TL_READ; /* Only for global SELECT */
unknown's avatar
unknown committed
3230 3231
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
unknown's avatar
unknown committed
3232 3233 3234 3235 3236
	  lex->current_select->parsing_place= SELECT_LEX_NODE::SELECT_LIST;
	}
	select_options select_item_list
	{
	  Select->parsing_place= SELECT_LEX_NODE::NO_MATTER;
unknown's avatar
unknown committed
3237
	}
unknown's avatar
unknown committed
3238
	select_into select_lock_type;
3239

unknown's avatar
unknown committed
3240
select_into:
3241 3242 3243 3244 3245 3246
	opt_limit_clause {}
	| FROM DUAL_SYM /* oracle compatibility: oracle always requires FROM
                           clause, and DUAL is system table without fields.
                           Is "SELECT 1 FROM DUAL" any better than
                           "SELECT 1" ? Hmmm :) */
        | into
unknown's avatar
unknown committed
3247
	| select_from
3248 3249
	| into select_from
	| select_from into;
unknown's avatar
unknown committed
3250 3251

select_from:
3252
	FROM join_table_list where_clause group_clause having_clause opt_order_clause opt_limit_clause procedure_clause;
unknown's avatar
unknown committed
3253 3254 3255

select_options:
	/* empty*/
unknown's avatar
unknown committed
3256
	| select_option_list;
unknown's avatar
unknown committed
3257 3258 3259

select_option_list:
	select_option_list select_option
unknown's avatar
unknown committed
3260
	| select_option;
unknown's avatar
unknown committed
3261 3262

select_option:
3263
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
3264 3265 3266 3267 3268 3269
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
3270 3271 3272
	| DISTINCT	{ Select->options|= SELECT_DISTINCT; }
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284
	| 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;
	  }
3285
	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
3286 3287 3288 3289
	| SQL_CACHE_SYM
	  {
	    Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
	  }
3290 3291
	| ALL		{}
	;
unknown's avatar
unknown committed
3292

unknown's avatar
unknown committed
3293 3294 3295
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
3296 3297
	  {
	    LEX *lex=Lex;
3298
	    lex->current_select->set_lock_for_tables(TL_WRITE);
3299
	    lex->safe_to_cache_query=0;
3300
	  }
unknown's avatar
unknown committed
3301
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
3302 3303
	  {
	    LEX *lex=Lex;
3304 3305
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
3306
	    lex->safe_to_cache_query=0;
3307 3308
	  }
	;
unknown's avatar
unknown committed
3309

unknown's avatar
unknown committed
3310 3311 3312 3313 3314
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
3315 3316
	    THD *thd= YYTHD;
	    if (add_item_to_list(thd, new Item_field(NULL, NULL, "*")))
unknown's avatar
unknown committed
3317
	      YYABORT;
unknown's avatar
unknown committed
3318
	    (thd->lex->current_select->with_wild)++;
unknown's avatar
unknown committed
3319
	  };
unknown's avatar
unknown committed
3320 3321 3322 3323 3324


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
unknown's avatar
unknown committed
3325
	    if (add_item_to_list(YYTHD, $2))
unknown's avatar
unknown committed
3326 3327
	      YYABORT;
	    if ($4.str)
unknown's avatar
unknown committed
3328
	      $2->set_name($4.str,$4.length,system_charset_info);
unknown's avatar
unknown committed
3329
	    else if (!$2->name)
unknown's avatar
unknown committed
3330
	      $2->set_name($1,(uint) ($3 - $1), YYTHD->charset());
unknown's avatar
unknown committed
3331
	  };
unknown's avatar
unknown committed
3332 3333

remember_name:
unknown's avatar
unknown committed
3334
	{ $$=(char*) Lex->tok_start; };
unknown's avatar
unknown committed
3335 3336

remember_end:
unknown's avatar
unknown committed
3337
	{ $$=(char*) Lex->tok_end; };
unknown's avatar
unknown committed
3338 3339 3340

select_item2:
	table_wild	{ $$=$1; } /* table.* */
unknown's avatar
unknown committed
3341
	| expr		{ $$=$1; };
unknown's avatar
unknown committed
3342 3343

select_alias:
unknown's avatar
unknown committed
3344 3345 3346 3347 3348 3349
	/* empty */		{ $$.str=0;}
	| AS ident		{ $$=$2; }
	| AS TEXT_STRING_sys	{ $$=$2; }
	| ident			{ $$=$1; }
	| TEXT_STRING_sys	{ $$=$1; }
	;
unknown's avatar
unknown committed
3350 3351 3352

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

unknown's avatar
unknown committed
3355
/* all possible expressions */
unknown's avatar
SCRUM  
unknown committed
3356 3357 3358 3359
expr:	
	expr_expr	{ $$= $1; }
	| simple_expr	{ $$= $1; }
	;
unknown's avatar
unknown committed
3360

unknown's avatar
unknown committed
3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372
comp_op:  EQ		{ $$ = &comp_eq_creator; }
	| GE		{ $$ = &comp_ge_creator; }
	| GT_SYM	{ $$ = &comp_gt_creator; }
	| LE		{ $$ = &comp_le_creator; }
	| LT		{ $$ = &comp_lt_creator; }
	| NE		{ $$ = &comp_ne_creator; }
	;

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

unknown's avatar
unknown committed
3373 3374
/* expressions that begin with 'expr' */
expr_expr:
unknown's avatar
SCRUM  
unknown committed
3375
	 expr IN_SYM '(' expr_list ')'
3376
	  { $4->push_front($1); $$= new Item_func_in(*$4); }
unknown's avatar
unknown committed
3377
	| expr NOT IN_SYM '(' expr_list ')'
3378
	  { $5->push_front($1); $$= new Item_func_not(new Item_func_in(*$5)); }
unknown's avatar
unknown committed
3379
        | expr IN_SYM in_subselect
3380
          { $$= new Item_in_subselect($1, $3); }
unknown's avatar
unknown committed
3381
	| expr NOT IN_SYM in_subselect
3382
          {
3383
            $$= new Item_func_not(new Item_in_subselect($1, $4));
unknown's avatar
unknown committed
3384
          }
unknown's avatar
unknown committed
3385 3386 3387 3388
	| expr BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_between($1,$3,$5); }
	| expr NOT BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
3389
	| expr OR_OR_CONCAT expr { $$= or_or_concat(YYTHD, $1,$3); }
unknown's avatar
unknown committed
3390
	| expr OR expr		{ $$= new Item_cond_or($1,$3); }
unknown's avatar
unknown committed
3391
        | expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
unknown's avatar
unknown committed
3392
	| expr AND expr		{ $$= new Item_cond_and($1,$3); }
unknown's avatar
unknown committed
3393 3394 3395 3396 3397 3398 3399 3400 3401
	| expr SOUNDS_SYM LIKE expr
	  {
	    $$= new Item_func_eq(new Item_func_soundex($1),
				 new Item_func_soundex($4));
	  }
	| expr LIKE simple_expr opt_escape
          { $$= new Item_func_like($1,$3,$4); }
	| expr NOT LIKE simple_expr opt_escape
          { $$= new Item_func_not(new Item_func_like($1,$4,$5));}
unknown's avatar
unknown committed
3402
	| expr REGEXP expr { $$= new Item_func_regex($1,$3); }
unknown's avatar
unknown committed
3403 3404
	| expr NOT REGEXP expr
          { $$= new Item_func_not(new Item_func_regex($1,$4)); }
unknown's avatar
unknown committed
3405 3406 3407
	| expr IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| expr IS NOT NULL_SYM { $$= new Item_func_isnotnull($1); }
	| expr EQUAL_SYM expr	{ $$= new Item_func_equal($1,$3); }
unknown's avatar
unknown committed
3408
	| expr comp_op expr %prec EQ	{ $$= (*$2)(0)->create($1,$3); }
unknown's avatar
unknown committed
3409 3410
	| expr comp_op all_or_any in_subselect %prec EQ
	{
unknown's avatar
unknown committed
3411
	  $$= all_any_subquery_creator($1, $2, $3, $4);
unknown's avatar
unknown committed
3412
	}
unknown's avatar
unknown committed
3413 3414 3415 3416 3417 3418
	| expr SHIFT_LEFT expr	{ $$= new Item_func_shift_left($1,$3); }
	| expr SHIFT_RIGHT expr { $$= new Item_func_shift_right($1,$3); }
	| expr '+' expr		{ $$= new Item_func_plus($1,$3); }
	| expr '-' expr		{ $$= new Item_func_minus($1,$3); }
	| expr '*' expr		{ $$= new Item_func_mul($1,$3); }
	| expr '/' expr		{ $$= new Item_func_div($1,$3); }
3419 3420
	| expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
	| expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
unknown's avatar
unknown committed
3421
	| expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
3422
        | expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
unknown's avatar
unknown committed
3423 3424
	| expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| expr '%' expr		{ $$= new Item_func_mod($1,$3); }
3425 3426 3427 3428
	| expr '+' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,0); }
	| expr '-' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,1); }
unknown's avatar
unknown committed
3429
	;
unknown's avatar
unknown committed
3430 3431 3432 3433 3434 3435 3436

/* expressions that begin with 'expr' that do NOT follow IN_SYM */
no_in_expr:
	no_in_expr BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_between($1,$3,$5); }
	| no_in_expr NOT BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
3437
	| no_in_expr OR_OR_CONCAT expr	{ $$= or_or_concat(YYTHD, $1,$3); }
unknown's avatar
unknown committed
3438
	| no_in_expr OR expr		{ $$= new Item_cond_or($1,$3); }
unknown's avatar
unknown committed
3439
        | no_in_expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
unknown's avatar
unknown committed
3440
	| no_in_expr AND expr		{ $$= new Item_cond_and($1,$3); }
unknown's avatar
unknown committed
3441 3442 3443 3444 3445 3446 3447 3448 3449
	| no_in_expr SOUNDS_SYM LIKE expr
	  {
	    $$= new Item_func_eq(new Item_func_soundex($1),
				 new Item_func_soundex($4));
	  }
	| no_in_expr LIKE simple_expr opt_escape
	  { $$= new Item_func_like($1,$3,$4); }
	| no_in_expr NOT LIKE simple_expr opt_escape
	  { $$= new Item_func_not(new Item_func_like($1,$4,$5)); }
unknown's avatar
unknown committed
3450
	| no_in_expr REGEXP expr { $$= new Item_func_regex($1,$3); }
unknown's avatar
unknown committed
3451 3452
	| no_in_expr NOT REGEXP expr
	  { $$= new Item_func_not(new Item_func_regex($1,$4)); }
unknown's avatar
unknown committed
3453 3454 3455
	| no_in_expr IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| no_in_expr IS NOT NULL_SYM { $$= new Item_func_isnotnull($1); }
	| no_in_expr EQUAL_SYM expr	{ $$= new Item_func_equal($1,$3); }
unknown's avatar
unknown committed
3456
	| no_in_expr comp_op expr %prec EQ { $$= (*$2)(0)->create($1,$3); }
unknown's avatar
unknown committed
3457 3458
	| no_in_expr comp_op all_or_any in_subselect %prec EQ
	{
unknown's avatar
unknown committed
3459
	  all_any_subquery_creator($1, $2, $3, $4);
unknown's avatar
unknown committed
3460
	}
unknown's avatar
unknown committed
3461 3462 3463 3464 3465 3466
	| no_in_expr SHIFT_LEFT expr  { $$= new Item_func_shift_left($1,$3); }
	| no_in_expr SHIFT_RIGHT expr { $$= new Item_func_shift_right($1,$3); }
	| no_in_expr '+' expr		{ $$= new Item_func_plus($1,$3); }
	| no_in_expr '-' expr		{ $$= new Item_func_minus($1,$3); }
	| no_in_expr '*' expr		{ $$= new Item_func_mul($1,$3); }
	| no_in_expr '/' expr		{ $$= new Item_func_div($1,$3); }
3467
	| no_in_expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
unknown's avatar
unknown committed
3468
	| no_in_expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
unknown's avatar
unknown committed
3469
        | no_in_expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
unknown's avatar
unknown committed
3470 3471
	| no_in_expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| no_in_expr '%' expr		{ $$= new Item_func_mod($1,$3); }
3472
	| no_in_expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
3473 3474 3475 3476
	| no_in_expr '+' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,0); }
	| no_in_expr '-' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,1); }
unknown's avatar
unknown committed
3477
	| simple_expr;
unknown's avatar
unknown committed
3478 3479 3480

/* expressions that begin with 'expr' that does NOT follow AND */
no_and_expr:
unknown's avatar
unknown committed
3481
	  no_and_expr IN_SYM '(' expr_list ')'
3482
	  { $4->push_front($1); $$= new Item_func_in(*$4); }
unknown's avatar
unknown committed
3483
	| no_and_expr NOT IN_SYM '(' expr_list ')'
3484
	  { $5->push_front($1); $$= new Item_func_not(new Item_func_in(*$5)); }
unknown's avatar
unknown committed
3485
        | no_and_expr IN_SYM in_subselect
3486
          { $$= new Item_in_subselect($1, $3); }
unknown's avatar
unknown committed
3487
	| no_and_expr NOT IN_SYM in_subselect
3488
          {
3489
            $$= new Item_func_not(new Item_in_subselect($1, $4));
unknown's avatar
unknown committed
3490
          }
unknown's avatar
unknown committed
3491 3492 3493 3494
	| no_and_expr BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_between($1,$3,$5); }
	| no_and_expr NOT BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
3495
	| no_and_expr OR_OR_CONCAT expr	{ $$= or_or_concat(YYTHD, $1,$3); }
unknown's avatar
unknown committed
3496
	| no_and_expr OR expr		{ $$= new Item_cond_or($1,$3); }
unknown's avatar
unknown committed
3497
        | no_and_expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
unknown's avatar
unknown committed
3498 3499 3500 3501 3502 3503 3504 3505 3506
	| no_and_expr SOUNDS_SYM LIKE expr
	  {
	    $$= new Item_func_eq(new Item_func_soundex($1),
				 new Item_func_soundex($4));
	  }
	| no_and_expr LIKE simple_expr opt_escape
	  { $$= new Item_func_like($1,$3,$4); }
	| no_and_expr NOT LIKE simple_expr opt_escape
	  { $$= new Item_func_not(new Item_func_like($1,$4,$5)); }
unknown's avatar
unknown committed
3507
	| no_and_expr REGEXP expr { $$= new Item_func_regex($1,$3); }
unknown's avatar
unknown committed
3508 3509
	| no_and_expr NOT REGEXP expr
	  { $$= new Item_func_not(new Item_func_regex($1,$4)); }
unknown's avatar
unknown committed
3510 3511 3512
	| no_and_expr IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| no_and_expr IS NOT NULL_SYM { $$= new Item_func_isnotnull($1); }
	| no_and_expr EQUAL_SYM expr	{ $$= new Item_func_equal($1,$3); }
unknown's avatar
unknown committed
3513
	| no_and_expr comp_op expr %prec EQ { $$= (*$2)(0)->create($1,$3); }
unknown's avatar
unknown committed
3514 3515
	| no_and_expr comp_op all_or_any in_subselect %prec EQ
	{
unknown's avatar
unknown committed
3516
	  all_any_subquery_creator($1, $2, $3, $4);
unknown's avatar
unknown committed
3517
	}
unknown's avatar
unknown committed
3518 3519 3520 3521 3522 3523
	| no_and_expr SHIFT_LEFT expr  { $$= new Item_func_shift_left($1,$3); }
	| no_and_expr SHIFT_RIGHT expr { $$= new Item_func_shift_right($1,$3); }
	| no_and_expr '+' expr		{ $$= new Item_func_plus($1,$3); }
	| no_and_expr '-' expr		{ $$= new Item_func_minus($1,$3); }
	| no_and_expr '*' expr		{ $$= new Item_func_mul($1,$3); }
	| no_and_expr '/' expr		{ $$= new Item_func_div($1,$3); }
3524
	| no_and_expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
unknown's avatar
unknown committed
3525
	| no_and_expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
unknown's avatar
unknown committed
3526
        | no_and_expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
unknown's avatar
unknown committed
3527 3528
	| no_and_expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| no_and_expr '%' expr		{ $$= new Item_func_mod($1,$3); }
3529
	| no_and_expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
3530 3531 3532 3533
	| no_and_expr '+' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,0); }
	| no_and_expr '-' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,1); }
unknown's avatar
unknown committed
3534
	| simple_expr;
unknown's avatar
unknown committed
3535

3536
interval_expr:
unknown's avatar
unknown committed
3537
         INTERVAL_SYM expr { $$=$2; }
3538 3539
        ;

unknown's avatar
unknown committed
3540 3541
simple_expr:
	simple_ident
3542
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
unknown's avatar
unknown committed
3543
	  {
unknown's avatar
unknown committed
3544 3545 3546 3547
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
					    		    YYTHD->charset()));
3548
	  }
unknown's avatar
unknown committed
3549
	| literal
unknown's avatar
unknown committed
3550
	| param_marker
unknown's avatar
unknown committed
3551
	| '@' ident_or_text SET_VAR expr
unknown's avatar
unknown committed
3552 3553
	  {
	    $$= new Item_func_set_user_var($2,$4);
3554
	    Lex->uncacheable(UNCACHEABLE_RAND);
unknown's avatar
unknown committed
3555
	  }
3556
	| '@' ident_or_text
unknown's avatar
unknown committed
3557 3558
	  {
	    $$= new Item_func_get_user_var($2);
3559
	    Lex->uncacheable(UNCACHEABLE_RAND);
unknown's avatar
unknown committed
3560
	  }
3561
	| '@' '@' opt_var_ident_type ident_or_text opt_component
unknown's avatar
unknown committed
3562
	  {
3563
	    if (!($$= get_system_var(YYTHD, (enum_var_type) $3, $4, $5)))
unknown's avatar
unknown committed
3564
	      YYABORT;
unknown's avatar
unknown committed
3565
	  }
unknown's avatar
unknown committed
3566
	| sum_expr
3567 3568
	| '+' expr %prec NEG	{ $$= $2; }
	| '-' expr %prec NEG    { $$= new Item_func_neg($2); }
unknown's avatar
unknown committed
3569 3570 3571 3572
	| '~' expr %prec NEG	{ $$= new Item_func_bit_neg($2); }
	| NOT expr %prec NEG	{ $$= new Item_func_not($2); }
	| '!' expr %prec NEG	{ $$= new Item_func_not($2); }
	| '(' expr ')'		{ $$= $2; }
3573 3574 3575 3576 3577
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
3578
	| ROW_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
3579
	  {
3580 3581
	    $5->push_front($3);
	    $$= new Item_row(*$5);
unknown's avatar
unknown committed
3582
	  }
unknown's avatar
unknown committed
3583
	| EXISTS exists_subselect { $$= $2; }
unknown's avatar
unknown committed
3584
	| singlerow_subselect   { $$= $1; }
unknown's avatar
unknown committed
3585
	| '{' ident expr '}'	{ $$= $3; }
3586
        | MATCH ident_list_arg AGAINST '(' expr fulltext_options ')'
3587
          { $2->push_front($5);
3588 3589
            Select->add_ftfunc_to_list((Item_func_match*)
                                        ($$=new Item_func_match(*$2,$6))); }
unknown's avatar
unknown committed
3590
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
unknown's avatar
unknown committed
3591
	| BINARY expr %prec NEG
unknown's avatar
unknown committed
3592
	  {
unknown's avatar
unknown committed
3593 3594
	    $$= new Item_func_set_collation($2,new Item_string(binary_keyword,
					    6, &my_charset_latin1));
unknown's avatar
unknown committed
3595
	  }
3596
	| CAST_SYM '(' expr AS cast_type ')'
3597 3598
	  {
	    $$= create_func_cast($3, $5,
3599
				 Lex->length ? atoi(Lex->length) : -1,
3600
				 Lex->charset);
3601
	  }
unknown's avatar
unknown committed
3602
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
3603
	  { $$= new Item_func_case(* $4, $2, $5 ); }
3604
	| CONVERT_SYM '(' expr ',' cast_type ')'
3605 3606 3607 3608 3609
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
				 Lex->charset);
	  }
3610 3611
	| CONVERT_SYM '(' expr USING charset_name ')'
	  { $$= new Item_func_conv_charset($3,$5); }
3612
	| CONVERT_SYM '(' expr ',' expr ',' expr ')'
3613
	  { $$= new Item_func_conv_charset3($3,$7,$5); }
unknown's avatar
SCRUM  
unknown committed
3614 3615
	| DEFAULT '(' simple_ident ')'
	  { $$= new Item_default_value($3); }
unknown's avatar
unknown committed
3616 3617
	| VALUES '(' simple_ident ')'
	  { $$= new Item_insert_value($3); }
unknown's avatar
unknown committed
3618 3619 3620 3621 3622 3623 3624 3625
	| FUNC_ARG0 '(' ')'
	  { $$= ((Item*(*)(void))($1.symbol->create_func))();}
	| FUNC_ARG1 '(' expr ')'
	  { $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);}
	| FUNC_ARG2 '(' expr ',' expr ')'
	  { $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);}
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
	  { $$= ((Item*(*)(Item*,Item*,Item*))($1.symbol->create_func))($3,$5,$7);}
unknown's avatar
unknown committed
3626 3627 3628 3629
	| 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); }
3630 3631
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
unknown's avatar
unknown committed
3632 3633 3634 3635 3636 3637
	| 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
3638 3639
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
unknown's avatar
unknown committed
3640 3641
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
unknown's avatar
unknown committed
3642 3643
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
unknown's avatar
unknown committed
3644 3645 3646 3647 3648
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
	  { $$= new Item_func_concat_ws($3, *$5); }
	| CURDATE optional_braces
3649
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
3650
	| CURTIME optional_braces
3651
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
3652
	| CURTIME '(' expr ')'
3653
	  {
3654
	    $$= new Item_func_curtime_local($3);
3655
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
3656
	  }
3657 3658 3659 3660
	| 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
3661
	| DATABASE '(' ')'
3662
	  {
unknown's avatar
unknown committed
3663
	    $$= new Item_func_database();
3664
            Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
3665
	  }
unknown's avatar
unknown committed
3666
	| DATE_SYM '(' expr ')'
unknown's avatar
unknown committed
3667
	  { $$= new Item_date_typecast($3); }
unknown's avatar
unknown committed
3668 3669
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
unknown's avatar
unknown committed
3670
	| ELT_FUNC '(' expr ',' expr_list ')'
3671
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
unknown's avatar
unknown committed
3672 3673
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
unknown's avatar
unknown committed
3674 3675 3676
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
3677
	    Lex->uncacheable(UNCACHEABLE_RAND);
unknown's avatar
unknown committed
3678
	  }
unknown's avatar
unknown committed
3679
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
3680
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
3681
	  { $$= new Item_func_decode($3,$5.str); }
3682
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
3683
	 { $$= new Item_func_encode($3,$5.str); }
unknown's avatar
unknown committed
3684
	| DES_DECRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
3685
        { $$= new Item_func_des_decrypt($3); }
unknown's avatar
unknown committed
3686
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
3687
        { $$= new Item_func_des_decrypt($3,$5); }
unknown's avatar
unknown committed
3688
	| DES_ENCRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
3689
        { $$= new Item_func_des_encrypt($3); }
unknown's avatar
unknown committed
3690
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
3691
        { $$= new Item_func_des_encrypt($3,$5); }
unknown's avatar
unknown committed
3692 3693 3694 3695 3696 3697
	| 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); }
3698 3699
	| FALSE_SYM
	  { $$= new Item_int((char*) "FALSE",0,1); }
unknown's avatar
unknown committed
3700 3701 3702 3703 3704 3705
	| 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
3706
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
unknown's avatar
unknown committed
3707 3708
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
3709
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
unknown's avatar
unknown committed
3710
	| GEOMFROMTEXT '(' expr ')'
3711
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
3712
	| GEOMFROMTEXT '(' expr ',' expr ')'
unknown's avatar
unknown committed
3713 3714 3715 3716 3717
	  { $$= new Item_func_geometry_from_text($3, $5); }
	| GEOMFROMWKB '(' expr ')'
	  { $$= new Item_func_geometry_from_wkb($3); }
	| GEOMFROMWKB '(' expr ',' expr ')'
	  { $$= new Item_func_geometry_from_wkb($3, $5); }
unknown's avatar
unknown committed
3718
	| GEOMETRYCOLLECTION '(' expr_list ')'
3719 3720
	  { $$= new Item_func_spatial_collection(* $3,
                       Geometry::wkbGeometryCollection,
unknown's avatar
unknown committed
3721
                       Geometry::wkbPoint); }
3722
	| GET_FORMAT '(' date_time_type  ',' expr ')'
3723
	  { $$= new Item_func_get_format($3, $5); }
unknown's avatar
unknown committed
3724 3725 3726 3727 3728 3729
	| 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); }
3730
	| interval_expr interval '+' expr
unknown's avatar
unknown committed
3731
	  /* we cannot put interval before - */
3732 3733 3734 3735 3736 3737 3738 3739 3740 3741
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
              send_error(Lex->thd, ER_SYNTAX_ERROR);
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
unknown's avatar
unknown committed
3742 3743
	| LAST_INSERT_ID '(' ')'
	  {
3744
	    $$= get_system_var(YYTHD, OPT_SESSION, "last_insert_id", 14,
3745
			      "last_insert_id()");
3746
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
3747 3748 3749 3750
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
	    $$= new Item_func_set_last_insert_id($3);
3751
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
3752 3753 3754
	  }
	| LEFT '(' expr ',' expr ')'
	  { $$= new Item_func_left($3,$5); }
unknown's avatar
unknown committed
3755
	| LINESTRING '(' expr_list ')'
3756
	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
3757
               Geometry::wkbLineString, Geometry::wkbPoint); }
unknown's avatar
unknown committed
3758 3759 3760 3761
	| 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
3762
 	| GEOMCOLLFROMTEXT '(' expr ')'
3763
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
3764
	| GEOMCOLLFROMTEXT '(' expr ',' expr ')'
unknown's avatar
unknown committed
3765
	  { $$= new Item_func_geometry_from_text($3, $5); }
unknown's avatar
unknown committed
3766
	| GREATEST_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
3767 3768 3769
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
3770
	| LOG_SYM '(' expr ')'
unknown's avatar
unknown committed
3771
	  { $$= new Item_func_log($3); }
3772
	| LOG_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
3773
	  { $$= new Item_func_log($3, $5); }
unknown's avatar
unknown committed
3774
 	| LINEFROMTEXT '(' expr ')'
3775
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
3776
	| LINEFROMTEXT '(' expr ',' expr ')'
unknown's avatar
unknown committed
3777
	  { $$= new Item_func_geometry_from_text($3, $5); }
3778
	| MASTER_POS_WAIT '(' expr ',' expr ')'
unknown's avatar
unknown committed
3779
	  {
3780
	    $$= new Item_master_pos_wait($3, $5);
unknown's avatar
unknown committed
3781
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
3782
		  }
3783
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
unknown's avatar
unknown committed
3784
	  {
3785
	    $$= new Item_master_pos_wait($3, $5, $7);
unknown's avatar
unknown committed
3786
	    Lex->safe_to_cache_query=0;
3787
	  }
unknown's avatar
unknown committed
3788 3789
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
unknown's avatar
unknown committed
3790 3791
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
3792 3793
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
unknown's avatar
unknown committed
3794 3795
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
unknown's avatar
unknown committed
3796
 	| MULTILINESTRING '(' expr_list ')'
3797
 	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
3798
                    Geometry::wkbMultiLineString, Geometry::wkbLineString); }
unknown's avatar
unknown committed
3799
 	| MLINEFROMTEXT '(' expr ')'
3800
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
3801
	| MLINEFROMTEXT '(' expr ',' expr ')'
unknown's avatar
unknown committed
3802
	  { $$= new Item_func_geometry_from_text($3, $5); }
unknown's avatar
unknown committed
3803
	| MPOINTFROMTEXT '(' expr ')'
3804
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
3805
	| MPOINTFROMTEXT '(' expr ',' expr ')'
unknown's avatar
unknown committed
3806
	  { $$= new Item_func_geometry_from_text($3, $5); }
unknown's avatar
unknown committed
3807
	| MPOLYFROMTEXT '(' expr ')'
3808
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
3809
	| MPOLYFROMTEXT '(' expr ',' expr ')'
unknown's avatar
unknown committed
3810
	  { $$= new Item_func_geometry_from_text($3, $5); }
unknown's avatar
unknown committed
3811
	| MULTIPOINT '(' expr_list ')'
3812
	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
3813 3814
                    Geometry::wkbMultiPoint, Geometry::wkbPoint); }
 	| MULTIPOLYGON '(' expr_list ')'
3815
	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
3816
                       Geometry::wkbMultiPolygon, Geometry::wkbPolygon ); }
unknown's avatar
unknown committed
3817
	| NOW_SYM optional_braces
3818
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
3819
	| NOW_SYM '(' expr ')'
3820
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
3821
	| PASSWORD '(' expr ')'
3822
	  {
3823 3824
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
unknown's avatar
unknown committed
3825 3826 3827 3828
	      (Item *) new Item_func_password($3);
	  }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new Item_func_old_password($3); }
3829
	| POINT_SYM '(' expr ',' expr ')'
3830
	  { $$= new Item_func_point($3,$5); }
unknown's avatar
unknown committed
3831
 	| POINTFROMTEXT '(' expr ')'
3832
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
3833
	| POINTFROMTEXT '(' expr ',' expr ')'
unknown's avatar
unknown committed
3834
	  { $$= new Item_func_geometry_from_text($3, $5); }
unknown's avatar
unknown committed
3835
	| POLYFROMTEXT '(' expr ')'
3836
	  { $$= new Item_func_geometry_from_text($3); }
unknown's avatar
unknown committed
3837
	| POLYFROMTEXT '(' expr ',' expr ')'
unknown's avatar
unknown committed
3838
	  { $$= new Item_func_geometry_from_text($3, $5); }
unknown's avatar
unknown committed
3839
	| POLYGON '(' expr_list ')'
3840
	  { $$= new Item_func_spatial_collection(* $3,
unknown's avatar
unknown committed
3841
			Geometry::wkbPolygon, Geometry::wkbLineString); }
unknown's avatar
unknown committed
3842 3843
	| POSITION_SYM '(' no_in_expr IN_SYM expr ')'
	  { $$ = new Item_func_locate($5,$3); }
3844 3845
	| QUARTER_SYM '(' expr ')'
	  { $$ = new Item_func_quarter($3); }
unknown's avatar
unknown committed
3846
	| RAND '(' expr ')'
3847
	  { $$= new Item_func_rand($3); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
3848
	| RAND '(' ')'
3849
	  { $$= new Item_func_rand(); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
3850 3851 3852 3853 3854 3855 3856
	| 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); }
unknown's avatar
unknown committed
3857 3858 3859 3860
	| 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
3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872
	| 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
3873
	| TIME_SYM '(' expr ')'
unknown's avatar
unknown committed
3874 3875 3876
	  { $$= new Item_time_typecast($3); }
	| TIMESTAMP '(' expr ')'
	  { $$= new Item_datetime_typecast($3); }
unknown's avatar
unknown committed
3877
	| TIMESTAMP '(' expr ',' expr ')'
unknown's avatar
unknown committed
3878
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
3879 3880 3881 3882
	| 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
3883
	| TRIM '(' expr ')'
3884 3885
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
unknown's avatar
unknown committed
3886
	  { $$= new Item_func_ltrim($6,$4); }
3887
	| TRIM '(' TRAILING expr FROM expr ')'
unknown's avatar
unknown committed
3888
	  { $$= new Item_func_rtrim($6,$4); }
3889
	| TRIM '(' BOTH expr FROM expr ')'
unknown's avatar
unknown committed
3890
	  { $$= new Item_func_trim($6,$4); }
3891 3892 3893 3894 3895 3896
	| 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
3897 3898
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
3899 3900
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
3901 3902
	| TRUE_SYM
	  { $$= new Item_int((char*) "TRUE",1,1); }
3903
	| SP_FUNC '(' sp_expr_list ')'
3904
	  {
3905
	    sp_add_fun_to_lex(Lex, $1);
3906 3907 3908 3909 3910
	    if ($3)
	      $$= new Item_func_sp($1, *$3);
	    else
	      $$= new Item_func_sp($1);
	  }
unknown's avatar
unknown committed
3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953
	| UDA_CHAR_SUM '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_sum_udf_str($1, *$3);
	    else
	      $$ = new Item_sum_udf_str($1);
	  }
	| UDA_FLOAT_SUM '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_sum_udf_float($1, *$3);
	    else
	      $$ = new Item_sum_udf_float($1);
	  }
	| UDA_INT_SUM '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_sum_udf_int($1, *$3);
	    else
	      $$ = new Item_sum_udf_int($1);
	  }
	| UDF_CHAR_FUNC '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_func_udf_str($1, *$3);
	    else
	      $$ = new Item_func_udf_str($1);
	  }
	| UDF_FLOAT_FUNC '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_func_udf_float($1, *$3);
	    else
	      $$ = new Item_func_udf_float($1);
	  }
	| UDF_INT_FUNC '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_func_udf_int($1, *$3);
	    else
	      $$ = new Item_func_udf_int($1);
	  }
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
3954
	  {
unknown's avatar
unknown committed
3955 3956
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
unknown's avatar
unknown committed
3957
	| UNIX_TIMESTAMP '(' ')'
unknown's avatar
unknown committed
3958 3959
	  {
	    $$= new Item_func_unix_timestamp();
3960
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
3961
	  }
unknown's avatar
unknown committed
3962 3963 3964
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
3965
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
3966 3967 3968 3969 3970 3971
	| 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
3972
	| WEEK_SYM '(' expr ')'
unknown's avatar
unknown committed
3973
	  {
3974
            $$= new Item_func_week($3,new Item_int((char*) "0",
unknown's avatar
unknown committed
3975
				   YYTHD->variables.default_week_format,1));
3976
          }
unknown's avatar
unknown committed
3977 3978 3979 3980 3981 3982 3983 3984 3985
	| 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 ')'
3986
	  {
unknown's avatar
unknown committed
3987
	    $$=new Item_func_benchmark($3,$5);
3988
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
3989
	  }
unknown's avatar
unknown committed
3990
	| EXTRACT_SYM '(' interval FROM expr ')'
unknown's avatar
unknown committed
3991
	{ $$=new Item_extract( $3, $5); };
unknown's avatar
unknown committed
3992

3993 3994 3995 3996 3997 3998
fulltext_options:
        /* nothing */                   { $$= FT_NL;  }
        | WITH QUERY_SYM EXPANSION_SYM  { $$= FT_NL | FT_EXPAND; }
        | IN_SYM BOOLEAN_SYM MODE_SYM   { $$= FT_BOOL; }
        ;

3999
sp_expr_list:
unknown's avatar
unknown committed
4000
	/* empty */	{ $$= NULL; }
unknown's avatar
unknown committed
4001
	| expr_list	{ $$= $1;};
unknown's avatar
unknown committed
4002

4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035
udf_expr_list:
	/* 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
4036 4037 4038 4039 4040 4041 4042
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); }
4043 4044
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
unknown's avatar
unknown committed
4045
	| COUNT_SYM '(' opt_all '*' ')'
unknown's avatar
unknown committed
4046 4047 4048
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
4049
	| COUNT_SYM '(' DISTINCT
unknown's avatar
unknown committed
4050
	  { Select->in_sum_expr++; }
4051
	   expr_list
unknown's avatar
unknown committed
4052
	  { Select->in_sum_expr--; }
4053 4054
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
unknown's avatar
unknown committed
4055 4056 4057 4058
	| 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); }
4059 4060 4061 4062 4063 4064 4065
/*
   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
4066 4067
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
4068 4069
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
unknown's avatar
unknown committed
4070 4071
	| STD_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_std($3); }
unknown's avatar
unknown committed
4072 4073
	| VARIANCE_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_variance($3); }
unknown's avatar
unknown committed
4074
	| SUM_SYM '(' in_sum_expr ')'
4075
	  { $$=new Item_sum_sum($3); }
4076 4077
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
unknown's avatar
unknown committed
4078 4079 4080 4081
	| GROUP_CONCAT_SYM '(' opt_distinct expr_list opt_gorder_clause
	  opt_gconcat_separator ')'
	  {
	    $$=new Item_func_group_concat($3,$4,Lex->gorder_list,$6);
4082 4083 4084 4085 4086 4087 4088 4089
	    $4->empty();
	  };

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

opt_gconcat_separator:
4090
    /* empty */        { $$ = new String(",",1,default_charset_info); }
4091
    |SEPARATOR_SYM text_string  { $$ = $2; };
unknown's avatar
unknown committed
4092

4093 4094

opt_gorder_clause:
4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107
	  /* empty */
	  {
            LEX *lex=Lex;
            lex->gorder_list = NULL;
	  }
	| order_clause
          {
            LEX *lex=Lex;
            lex->gorder_list= 
	      (SQL_LIST*) sql_memdup((char*) &lex->current_select->order_list,
				     sizeof(st_sql_list));
	    lex->current_select->order_list.empty();
	  };
unknown's avatar
unknown committed
4108

unknown's avatar
unknown committed
4109 4110

in_sum_expr:
unknown's avatar
unknown committed
4111
	opt_all
4112 4113 4114 4115 4116
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
4117
	    YYABORT;
4118 4119
	  }
	}
unknown's avatar
unknown committed
4120 4121
	expr
	{
unknown's avatar
unknown committed
4122
	  Select->in_sum_expr--;
4123
	  $$= $3;
unknown's avatar
unknown committed
4124
	};
unknown's avatar
unknown committed
4125

4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142
cast_type_init:
	{ Lex->charset= NULL; Lex->length= (char*)0; }
	;

cast_type_finalize:
	BINARY			{ $$=ITEM_CAST_BINARY; }
	| CHAR_SYM opt_len opt_binary	{ $$=ITEM_CAST_CHAR; }
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; }
	| SIGNED_SYM		{ $$=ITEM_CAST_SIGNED_INT; }
	| SIGNED_SYM INT_SYM	{ $$=ITEM_CAST_SIGNED_INT; }
	| UNSIGNED		{ $$=ITEM_CAST_UNSIGNED_INT; }
	| UNSIGNED INT_SYM	{ $$=ITEM_CAST_UNSIGNED_INT; }
	| DATE_SYM		{ $$=ITEM_CAST_DATE; }
	| TIME_SYM		{ $$=ITEM_CAST_TIME; }
	| DATETIME		{ $$=ITEM_CAST_DATETIME; }
	;

unknown's avatar
unknown committed
4143
cast_type:
4144
	cast_type_init cast_type_finalize { $$= $2; }
4145
	;
unknown's avatar
unknown committed
4146

unknown's avatar
unknown committed
4147
expr_list:
4148
	{ Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
4149
	expr_list2
unknown's avatar
unknown committed
4150
	{ $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
4151 4152

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

unknown's avatar
unknown committed
4156 4157
ident_list_arg:
          ident_list          { $$= $1; }
unknown's avatar
unknown committed
4158
        | '(' ident_list ')'  { $$= $2; };
unknown's avatar
unknown committed
4159

unknown's avatar
unknown committed
4160
ident_list:
4161
        { Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
4162
        ident_list2
unknown's avatar
unknown committed
4163
        { $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
4164 4165

ident_list2:
4166
        simple_ident { Select->expr_list.head()->push_back($1); }
unknown's avatar
unknown committed
4167
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
unknown's avatar
unknown committed
4168 4169 4170

opt_expr:
	/* empty */      { $$= NULL; }
4171
	| expr           { $$= $1; };
unknown's avatar
unknown committed
4172 4173 4174

opt_else:
	/* empty */    { $$= NULL; }
unknown's avatar
unknown committed
4175
	| ELSE expr    { $$= $2; };
unknown's avatar
unknown committed
4176 4177

when_list:
unknown's avatar
unknown committed
4178
        { Select->when_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
4179
	when_list2
unknown's avatar
unknown committed
4180
	{ $$= Select->when_list.pop(); };
unknown's avatar
unknown committed
4181 4182

when_list2:
unknown's avatar
unknown committed
4183
	expr THEN_SYM expr
unknown's avatar
unknown committed
4184
	  {
unknown's avatar
unknown committed
4185
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
4186 4187
	    sel->when_list.head()->push_back($1);
	    sel->when_list.head()->push_back($3);
unknown's avatar
unknown committed
4188
	}
unknown's avatar
unknown committed
4189
	| when_list2 WHEN_SYM expr THEN_SYM expr
unknown's avatar
unknown committed
4190
	  {
unknown's avatar
unknown committed
4191
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
4192 4193
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
unknown's avatar
unknown committed
4194
	  };
unknown's avatar
unknown committed
4195

4196 4197 4198 4199 4200 4201 4202 4203 4204 4205
table_ref:
        table_factor            { $$=$1; }
        | join_table            { $$=$1; }
          { 
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
          }     	        
        ;

unknown's avatar
unknown committed
4206
join_table_list:
4207 4208 4209 4210 4211 4212 4213 4214 4215
        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
4216
	  { add_join_on($3,$5); $$=$3; }
4217
	| table_ref normal_join table_ref
unknown's avatar
unknown committed
4218
	  USING
unknown's avatar
unknown committed
4219
	  {
unknown's avatar
unknown committed
4220
	    SELECT_LEX *sel= Select;
4221
            sel->save_names_for_using_list($1, $3);
unknown's avatar
unknown committed
4222
	  }
unknown's avatar
unknown committed
4223 4224 4225
	  '(' using_list ')'
	  { add_join_on($3,$7); $$=$3; }

4226
	| table_ref LEFT opt_outer JOIN_SYM table_ref ON expr
4227
	  { add_join_on($5,$7); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
4228
	| table_ref LEFT opt_outer JOIN_SYM table_ref
unknown's avatar
unknown committed
4229
	  {
unknown's avatar
unknown committed
4230
	    SELECT_LEX *sel= Select;
4231
            sel->save_names_for_using_list($1, $5);
unknown's avatar
unknown committed
4232
	  }
unknown's avatar
unknown committed
4233
	  USING '(' using_list ')'
4234
	  { add_join_on($5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
4235
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
4236
	  {
4237 4238
	    add_join_natural($1,$6);
	    $6->outer_join|=JOIN_TYPE_LEFT;
4239 4240
	    $$=$6;
	  }
4241 4242 4243 4244 4245 4246 4247 4248
	| 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_ref
unknown's avatar
unknown committed
4249
	  {
unknown's avatar
unknown committed
4250
	    SELECT_LEX *sel= Select;
4251
            sel->save_names_for_using_list($1, $5);
unknown's avatar
unknown committed
4252
	  }
4253
	  USING '(' using_list ')'
4254 4255 4256 4257 4258 4259 4260
          { 
	    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
4261
	  {
4262 4263 4264 4265
	    add_join_natural($6,$1);
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
4266
	  }
4267 4268 4269
	| table_ref NATURAL JOIN_SYM table_factor
	  { add_join_natural($1,$4); $$=$4; };
        
unknown's avatar
unknown committed
4270 4271

normal_join:
unknown's avatar
unknown committed
4272 4273 4274 4275
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
unknown's avatar
unknown committed
4276

4277
table_factor:
unknown's avatar
unknown committed
4278
	{
unknown's avatar
unknown committed
4279
	  SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
4280
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
unknown's avatar
unknown committed
4281
	  sel->table_join_options= 0;
unknown's avatar
unknown committed
4282
	}
unknown's avatar
unknown committed
4283
        table_ident opt_table_alias opt_key_definition
unknown's avatar
unknown committed
4284
	{
unknown's avatar
unknown committed
4285
	  LEX *lex= Lex;
unknown's avatar
unknown committed
4286
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
4287
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
unknown's avatar
unknown committed
4288
					   sel->get_table_join_options(),
unknown's avatar
unknown committed
4289
					   lex->lock_option,
4290 4291
					   sel->get_use_index(),
					   sel->get_ignore_index())))
4292
	    YYABORT;
4293
          sel->add_joined_table($$); 
unknown's avatar
unknown committed
4294
	}
4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307
        | '('
          { 
            LEX *lex= Lex;
            if (lex->current_select->init_nested_join(lex->thd))
              YYABORT;
          }            
          join_table_list ')'
          {
            LEX *lex= Lex;
            if (!($$= lex->current_select->end_nested_join(lex->thd)))
              YYABORT;
          }	
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref ON expr '}'
4308
	  { add_join_on($7,$9); $7->outer_join|=JOIN_TYPE_LEFT; $$=$7; }
4309
        | '(' SELECT_SYM select_derived ')' opt_table_alias
unknown's avatar
unknown committed
4310 4311
	{
	  LEX *lex=Lex;
unknown's avatar
unknown committed
4312 4313
	  if (lex->sql_command == SQLCOM_UPDATE &&
	      &lex->select_lex == lex->current_select->outer_select())
4314 4315 4316 4317 4318
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }

4319 4320 4321
	  SELECT_LEX_UNIT *unit= lex->current_select->master_unit();
	  lex->current_select= unit->outer_select();
	  if (!($$= lex->current_select->
unknown's avatar
unknown committed
4322
                add_table_to_list(lex->thd, new Table_ident(unit), $5, 0,
4323
				  TL_READ,(List<String> *)0,
unknown's avatar
unknown committed
4324 4325
	                          (List<String> *)0)))

unknown's avatar
unknown committed
4326
	    YYABORT;
4327
          lex->current_select->add_joined_table($$);           
unknown's avatar
unknown committed
4328
	};
unknown's avatar
unknown committed
4329

4330
select_derived:
unknown's avatar
unknown committed
4331
        {
unknown's avatar
unknown committed
4332
	  LEX *lex= Lex;
unknown's avatar
unknown committed
4333
	  lex->derived_tables= 1;
unknown's avatar
unknown committed
4334 4335 4336
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
4337 4338 4339 4340
	  {	
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
4341
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
unknown's avatar
unknown committed
4342
              mysql_new_select(lex, 1))
unknown's avatar
unknown committed
4343 4344
	    YYABORT;
	  mysql_init_select(lex);
4345
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
unknown's avatar
unknown committed
4346
	}
4347
        select_options select_item_list opt_select_from union_opt
4348
        ;
unknown's avatar
unknown committed
4349 4350 4351

opt_outer:
	/* empty */	{}
unknown's avatar
unknown committed
4352
	| OUTER		{};
unknown's avatar
unknown committed
4353 4354 4355 4356

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
unknown's avatar
unknown committed
4357
          {
unknown's avatar
unknown committed
4358
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
4359 4360 4361
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
4362 4363
	| FORCE_SYM key_usage_list
          {
unknown's avatar
unknown committed
4364
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
4365 4366 4367 4368
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
unknown's avatar
unknown committed
4369
	| IGNORE_SYM key_usage_list
unknown's avatar
unknown committed
4370
	  {
unknown's avatar
unknown committed
4371
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
4372 4373
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
unknown's avatar
unknown committed
4374
	  };
unknown's avatar
unknown committed
4375 4376

key_usage_list:
unknown's avatar
unknown committed
4377
	key_or_index { Select->interval_list.empty(); }
unknown's avatar
unknown committed
4378
        '(' key_list_or_empty ')'
unknown's avatar
unknown committed
4379
        { $$= &Select->interval_list; }
unknown's avatar
unknown committed
4380 4381 4382 4383 4384 4385
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
unknown's avatar
unknown committed
4386 4387 4388

key_usage_list2:
	key_usage_list2 ',' ident
unknown's avatar
unknown committed
4389
        { Select->
4390
	    interval_list.push_back(new String((const char*) $3.str, $3.length,
4391
				    system_charset_info)); }
unknown's avatar
unknown committed
4392
	| ident
unknown's avatar
unknown committed
4393
        { Select->
4394
	    interval_list.push_back(new String((const char*) $1.str, $1.length,
4395
				    system_charset_info)); }
unknown's avatar
unknown committed
4396
	| PRIMARY_SYM
unknown's avatar
unknown committed
4397
        { Select->
4398
	    interval_list.push_back(new String("PRIMARY", 7,
4399
				    system_charset_info)); };
unknown's avatar
unknown committed
4400 4401 4402

using_list:
	ident
unknown's avatar
unknown committed
4403
	  {
unknown's avatar
unknown committed
4404
	    SELECT_LEX *sel= Select;
4405 4406 4407 4408
	    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
4409 4410 4411 4412
	      YYABORT;
	  }
	| using_list ',' ident
	  {
unknown's avatar
unknown committed
4413
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
4414
	    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
4415
	      YYABORT;
unknown's avatar
unknown committed
4416
	  };
unknown's avatar
unknown committed
4417 4418

interval:
4419 4420
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
unknown's avatar
unknown committed
4421
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
unknown's avatar
unknown committed
4422 4423
	| DAY_MINUTE_SYM	{ $$=INTERVAL_DAY_MINUTE; }
	| DAY_SECOND_SYM	{ $$=INTERVAL_DAY_SECOND; }
unknown's avatar
unknown committed
4424
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
unknown's avatar
unknown committed
4425 4426
	| HOUR_MINUTE_SYM	{ $$=INTERVAL_HOUR_MINUTE; }
	| HOUR_SECOND_SYM	{ $$=INTERVAL_HOUR_SECOND; }
unknown's avatar
unknown committed
4427 4428
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
unknown's avatar
unknown committed
4429
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
4430 4431 4432 4433 4434 4435 4436 4437
	| 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
4438 4439
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
4440
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
unknown's avatar
unknown committed
4441
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
unknown's avatar
unknown committed
4442
	| YEAR_SYM		{ $$=INTERVAL_YEAR; };
unknown's avatar
unknown committed
4443

4444 4445 4446 4447
date_time_type:
	DATE_SYM		{$$=TIMESTAMP_DATE;}
	| TIME_SYM		{$$=TIMESTAMP_TIME;}
	| DATETIME		{$$=TIMESTAMP_DATETIME;};
4448

unknown's avatar
unknown committed
4449 4450 4451
table_alias:
	/* empty */
	| AS
unknown's avatar
unknown committed
4452
	| EQ;
unknown's avatar
unknown committed
4453 4454 4455 4456

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

unknown's avatar
unknown committed
4459 4460 4461 4462
opt_all:
	/* empty */
	| ALL
	;
unknown's avatar
unknown committed
4463 4464

where_clause:
unknown's avatar
unknown committed
4465
	/* empty */  { Select->where= 0; }
4466 4467
	| WHERE expr
	  {
unknown's avatar
unknown committed
4468
	    Select->where= $2;
4469 4470 4471
	    if ($2)
	      $2->top_level_item();
	  }
unknown's avatar
unknown committed
4472
 	;
unknown's avatar
unknown committed
4473 4474 4475

having_clause:
	/* empty */
unknown's avatar
unknown committed
4476 4477
	| HAVING
	  {
unknown's avatar
unknown committed
4478
	    Select->parsing_place= SELECT_LEX_NODE::IN_HAVING;
unknown's avatar
unknown committed
4479 4480
          }
	  expr
unknown's avatar
unknown committed
4481
	  {
unknown's avatar
unknown committed
4482
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
4483 4484
	    sel->having= $3;
	    sel->parsing_place= SELECT_LEX_NODE::NO_MATTER;
unknown's avatar
unknown committed
4485 4486 4487
	    if ($3)
	      $3->top_level_item();
	  }
4488
	;
unknown's avatar
unknown committed
4489 4490

opt_escape:
4491
	ESCAPE_SYM TEXT_STRING_literal	{ $$= $2.str; }
unknown's avatar
unknown committed
4492
	| /* empty */			{ $$= (char*) "\\"; };
unknown's avatar
unknown committed
4493 4494 4495


/*
unknown's avatar
unknown committed
4496
   group by statement in select
unknown's avatar
unknown committed
4497 4498 4499 4500
*/

group_clause:
	/* empty */
4501
	| GROUP BY group_list olap_opt;
unknown's avatar
unknown committed
4502 4503

group_list:
unknown's avatar
unknown committed
4504
	group_list ',' order_ident order_dir
unknown's avatar
unknown committed
4505
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
4506
	| order_ident order_dir
unknown's avatar
unknown committed
4507
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
4508

4509 4510
olap_opt:
	/* empty */ {}
4511
	| WITH CUBE_SYM
4512
          {
4513
	    LEX *lex=Lex;
4514 4515 4516 4517 4518 4519
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
	      net_printf(lex->thd, ER_WRONG_USAGE, "WITH CUBE",
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
4520
	    lex->current_select->olap= CUBE_TYPE;
4521
	    net_printf(lex->thd, ER_NOT_SUPPORTED_YET, "CUBE");
4522
	    YYABORT;	/* To be deleted in 5.1 */
4523
	  }
4524
	| WITH ROLLUP_SYM
4525
          {
4526 4527 4528 4529 4530 4531 4532
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
	      net_printf(lex->thd, ER_WRONG_USAGE, "WITH ROLLUP",
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
4533
	    lex->current_select->olap= ROLLUP_TYPE;
4534
	  }
4535
	;
4536

unknown's avatar
unknown committed
4537
/*
unknown's avatar
unknown committed
4538
   Order by statement in select
unknown's avatar
unknown committed
4539 4540
*/

4541
opt_order_clause:
unknown's avatar
unknown committed
4542
	/* empty */
unknown's avatar
unknown committed
4543
	| order_clause;
4544 4545

order_clause:
4546 4547
	ORDER_SYM BY
        {
unknown's avatar
unknown committed
4548
	  LEX *lex=Lex;
4549
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
unknown's avatar
unknown committed
4550
	      lex->current_select->olap !=
4551
	      UNSPECIFIED_OLAP_TYPE)
4552
	  {
4553
	    net_printf(lex->thd, ER_WRONG_USAGE,
4554 4555 4556 4557
		       "CUBE/ROLLUP",
		       "ORDER BY");
	    YYABORT;
	  }
unknown's avatar
unknown committed
4558
	} order_list;
unknown's avatar
unknown committed
4559 4560 4561

order_list:
	order_list ',' order_ident order_dir
unknown's avatar
unknown committed
4562
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
4563
	| order_ident order_dir
unknown's avatar
unknown committed
4564
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
4565 4566 4567

order_dir:
	/* empty */ { $$ =  1; }
unknown's avatar
unknown committed
4568
	| ASC  { $$ =1; }
unknown's avatar
unknown committed
4569
	| DESC { $$ =0; };
unknown's avatar
unknown committed
4570 4571


4572 4573 4574
opt_limit_clause_init:
	/* empty */
	{
unknown's avatar
unknown committed
4575
	  SELECT_LEX *sel= Select;
4576 4577 4578
          sel->offset_limit= 0L;
          sel->select_limit= Lex->thd->variables.select_limit;
	}
4579 4580 4581
	| limit_clause {}
	;

4582 4583 4584 4585 4586
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

4587
limit_clause:
4588
	LIMIT limit_options {}
unknown's avatar
unknown committed
4589
	;
unknown's avatar
unknown committed
4590 4591 4592

limit_options:
	ULONG_NUM
unknown's avatar
unknown committed
4593
	  {
unknown's avatar
unknown committed
4594
            SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
4595 4596 4597 4598 4599
            sel->select_limit= $1;
            sel->offset_limit= 0L;
	  }
	| ULONG_NUM ',' ULONG_NUM
	  {
unknown's avatar
unknown committed
4600
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
4601 4602
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
unknown's avatar
unknown committed
4603
	  }
unknown's avatar
unknown committed
4604
	| ULONG_NUM OFFSET_SYM ULONG_NUM
unknown's avatar
unknown committed
4605
	  {
unknown's avatar
unknown committed
4606
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
4607 4608 4609 4610
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
	  }
	;
unknown's avatar
unknown committed
4611 4612 4613 4614 4615


delete_limit_clause:
	/* empty */
	{
unknown's avatar
unknown committed
4616
	  LEX *lex=Lex;
4617
	  lex->current_select->select_limit= HA_POS_ERROR;
unknown's avatar
unknown committed
4618
	}
unknown's avatar
unknown committed
4619
	| LIMIT ulonglong_num
unknown's avatar
unknown committed
4620
	{ Select->select_limit= (ha_rows) $2; };
unknown's avatar
unknown committed
4621 4622

ULONG_NUM:
unknown's avatar
unknown committed
4623
	NUM	    { $$= strtoul($1.str,NULL,10); }
4624
	| LONG_NUM  { $$= (ulong) strtoll($1.str,NULL,10); }
unknown's avatar
unknown committed
4625
	| ULONGLONG_NUM { $$= (ulong) strtoull($1.str,NULL,10); }
unknown's avatar
unknown committed
4626
	| REAL_NUM  { $$= strtoul($1.str,NULL,10); }
unknown's avatar
unknown committed
4627
	| FLOAT_NUM { $$= strtoul($1.str,NULL,10); };
unknown's avatar
unknown committed
4628

unknown's avatar
unknown committed
4629
ulonglong_num:
unknown's avatar
unknown committed
4630
	NUM	    { $$= (ulonglong) strtoul($1.str,NULL,10); }
unknown's avatar
unknown committed
4631
	| ULONGLONG_NUM { $$= strtoull($1.str,NULL,10); }
4632
	| LONG_NUM  { $$= (ulonglong) strtoll($1.str,NULL,10); }
unknown's avatar
unknown committed
4633
	| REAL_NUM  { $$= strtoull($1.str,NULL,10); }
unknown's avatar
unknown committed
4634
	| FLOAT_NUM { $$= strtoull($1.str,NULL,10); };
unknown's avatar
unknown committed
4635 4636 4637 4638 4639 4640

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
4641 4642 4643 4644 4645 4646 4647
	    if (&lex->select_lex != lex->current_select)
	    {
	      net_printf(lex->thd, ER_WRONG_USAGE,
			  "PROCEDURE",
			  "subquery");
	      YYABORT;
	    }
unknown's avatar
unknown committed
4648 4649 4650
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
unknown's avatar
unknown committed
4651
	    if (add_proc_to_list(lex->thd, new Item_field(NULL,NULL,$2.str)))
unknown's avatar
unknown committed
4652
	      YYABORT;
4653
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
4654
	  }
unknown's avatar
unknown committed
4655
	  '(' procedure_list ')';
unknown's avatar
unknown committed
4656 4657 4658 4659


procedure_list:
	/* empty */ {}
unknown's avatar
unknown committed
4660
	| procedure_list2 {};
unknown's avatar
unknown committed
4661 4662 4663

procedure_list2:
	procedure_list2 ',' procedure_item
unknown's avatar
unknown committed
4664
	| procedure_item;
unknown's avatar
unknown committed
4665 4666 4667 4668

procedure_item:
	  remember_name expr
	  {
unknown's avatar
unknown committed
4669 4670
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
unknown's avatar
unknown committed
4671 4672
	      YYABORT;
	    if (!$2->name)
unknown's avatar
unknown committed
4673
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1), YYTHD->charset());
4674 4675 4676 4677 4678 4679
	  }
          ;


select_var_list_init:
	   {
unknown's avatar
unknown committed
4680 4681
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
4682 4683
	        YYABORT;
	   }
4684
	   select_var_list
4685
	   {}
4686
           ;
unknown's avatar
unknown committed
4687

unknown's avatar
unknown committed
4688
select_var_list:
4689 4690 4691 4692
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

unknown's avatar
unknown committed
4693 4694
select_var_ident:  
	   '@' ident_or_text
unknown's avatar
unknown committed
4695
           {
4696
             LEX *lex=Lex;
unknown's avatar
unknown committed
4697
	     if (lex->result) 
4698
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($2,0,0,(enum_field_types)0));
unknown's avatar
unknown committed
4699
	     else
unknown's avatar
unknown committed
4700 4701
	       YYABORT;
	   }
unknown's avatar
unknown committed
4702
           | ident_or_text
unknown's avatar
unknown committed
4703
           {
4704
             LEX *lex=Lex;
unknown's avatar
unknown committed
4705
	     sp_pvar_t *t;
4706 4707

	     if (!lex->spcont || !(t=lex->spcont->find_pvar(&$1)))
unknown's avatar
unknown committed
4708
	     {
4709
	       net_printf(YYTHD, ER_SP_UNDECLARED_VAR, $1.str);
unknown's avatar
unknown committed
4710
	       YYABORT;
unknown's avatar
unknown committed
4711
	     }
4712
	     if (! lex->result)
unknown's avatar
unknown committed
4713
	       YYABORT;
4714 4715
	     else
	     {
4716
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($1,1,t->offset,t->type));
4717 4718
	       t->isset= TRUE;
	     }
unknown's avatar
unknown committed
4719
	   }
unknown's avatar
unknown committed
4720
           ;
unknown's avatar
unknown committed
4721

4722
into:
unknown's avatar
unknown committed
4723
        INTO OUTFILE TEXT_STRING_sys
unknown's avatar
unknown committed
4724
	{
4725
	  LEX *lex=Lex;
unknown's avatar
unknown committed
4726 4727
	  if (!lex->describe)
	  {
4728
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
4729 4730 4731 4732 4733
	    if (!(lex->exchange= new sql_exchange($3.str,0)))
	      YYABORT;
	    if (!(lex->result= new select_export(lex->exchange)))
	      YYABORT;
	  }
unknown's avatar
unknown committed
4734 4735
	}
	opt_field_term opt_line_term
unknown's avatar
unknown committed
4736
	| INTO DUMPFILE TEXT_STRING_sys
unknown's avatar
unknown committed
4737
	{
4738
	  LEX *lex=Lex;
unknown's avatar
unknown committed
4739 4740
	  if (!lex->describe)
	  {
4741
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
4742 4743 4744 4745 4746
	    if (!(lex->exchange= new sql_exchange($3.str,1)))
	      YYABORT;
	    if (!(lex->result= new select_dump(lex->exchange)))
	      YYABORT;
	  }
unknown's avatar
unknown committed
4747
	}
4748
        | INTO select_var_list_init
unknown's avatar
unknown committed
4749
	{
4750
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
4751 4752
	}
        ;
unknown's avatar
unknown committed
4753

unknown's avatar
unknown committed
4754 4755 4756
/*
  DO statement
*/
unknown's avatar
unknown committed
4757

4758
do:	DO_SYM
unknown's avatar
unknown committed
4759 4760 4761 4762 4763 4764
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
	  if (!(lex->insert_list = new List_item))
	    YYABORT;
	}
4765 4766 4767 4768
	values
	{}
	;

unknown's avatar
unknown committed
4769
/*
4770
  Drop : delete tables or index or user
unknown's avatar
unknown committed
4771 4772 4773
*/

drop:
unknown's avatar
unknown committed
4774
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
unknown's avatar
unknown committed
4775
	{
unknown's avatar
unknown committed
4776 4777
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
4778 4779
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
unknown's avatar
unknown committed
4780 4781 4782
	}
	| DROP INDEX ident ON table_ident {}
	  {
unknown's avatar
unknown committed
4783 4784 4785 4786
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
	     lex->drop_list.empty();
	     lex->drop_list.push_back(new Alter_drop(Alter_drop::KEY,
unknown's avatar
unknown committed
4787
						     $3.str));
unknown's avatar
unknown committed
4788 4789
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
unknown's avatar
unknown committed
4790 4791 4792 4793
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
unknown's avatar
unknown committed
4794 4795 4796 4797
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
	    lex->name=$4.str;
unknown's avatar
unknown committed
4798
	 }
4799
	| DROP FUNCTION_SYM if_exists IDENT_sys opt_restrict
unknown's avatar
unknown committed
4800
	  {
unknown's avatar
unknown committed
4801 4802
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
4803 4804
	    lex->drop_if_exists= $3;
	    lex->udf.name= $4;
4805
	  }
4806
	| DROP PROCEDURE if_exists IDENT_sys opt_restrict
4807 4808 4809
	  {
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
4810 4811
	    lex->drop_if_exists= $3;
	    lex->udf.name= $4;
4812 4813 4814 4815 4816 4817
	  }
	| DROP USER
	  {
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_DROP_USER;
	    lex->users_list.empty();
unknown's avatar
unknown committed
4818
	  }
4819 4820 4821
	  user_list
	  {}
	  ;
unknown's avatar
unknown committed
4822 4823

table_list:
4824
	table_name
unknown's avatar
unknown committed
4825
	| table_list ',' table_name;
unknown's avatar
unknown committed
4826

4827
table_name:
unknown's avatar
unknown committed
4828
	table_ident
unknown's avatar
unknown committed
4829 4830 4831 4832 4833
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
unknown's avatar
unknown committed
4834 4835

if_exists:
4836
	/* empty */ { $$= 0; }
4837 4838
	| IF EXISTS { $$= 1; }
	;
unknown's avatar
unknown committed
4839

4840 4841 4842 4843
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
unknown's avatar
unknown committed
4844 4845 4846 4847 4848
/*
** Insert : add new data to table
*/

insert:
4849 4850 4851 4852 4853 4854
	INSERT
	{
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_INSERT;
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
4855
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
4856
	} insert_lock_option
4857
	opt_ignore insert2
unknown's avatar
unknown committed
4858
	{
unknown's avatar
unknown committed
4859
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
4860
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
4861
	}
4862
	insert_field_spec opt_insert_update
4863
	{}
unknown's avatar
unknown committed
4864
	;
unknown's avatar
unknown committed
4865 4866

replace:
4867 4868
	REPLACE
	{
4869
	  LEX *lex=Lex;
4870 4871
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
4872
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
4873
	}
unknown's avatar
unknown committed
4874 4875
	replace_lock_option insert2
	{
unknown's avatar
unknown committed
4876
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
4877
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
4878 4879
	}
	insert_field_spec
4880
	{}
unknown's avatar
unknown committed
4881
	;
unknown's avatar
unknown committed
4882 4883

insert_lock_option:
unknown's avatar
unknown committed
4884 4885 4886 4887
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
4888
	;
unknown's avatar
unknown committed
4889 4890

replace_lock_option:
unknown's avatar
unknown committed
4891 4892
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
unknown's avatar
unknown committed
4893 4894 4895

insert2:
	INTO insert_table {}
unknown's avatar
unknown committed
4896
	| insert_table {};
unknown's avatar
unknown committed
4897 4898

insert_table:
4899
	table_name
unknown's avatar
unknown committed
4900
	{
unknown's avatar
unknown committed
4901 4902 4903 4904
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
unknown's avatar
unknown committed
4905
	};
unknown's avatar
unknown committed
4906 4907

insert_field_spec:
unknown's avatar
unknown committed
4908 4909 4910
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
unknown's avatar
unknown committed
4911 4912
	| SET
	  {
unknown's avatar
unknown committed
4913 4914 4915
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
4916 4917
	      YYABORT;
	   }
unknown's avatar
unknown committed
4918
	   ident_eq_list;
unknown's avatar
unknown committed
4919 4920 4921 4922

opt_field_spec:
	/* empty */	  { }
	| '(' fields ')'  { }
unknown's avatar
unknown committed
4923
	| '(' ')'	  { };
unknown's avatar
unknown committed
4924 4925 4926

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
unknown's avatar
unknown committed
4927
	| insert_ident		{ Lex->field_list.push_back($1); };
unknown's avatar
unknown committed
4928 4929 4930

insert_values:
	VALUES	values_list  {}
unknown's avatar
unknown committed
4931
	| VALUE_SYM values_list  {}
unknown's avatar
unknown committed
4932 4933
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
4934
        ;
unknown's avatar
unknown committed
4935 4936 4937

values_list:
	values_list ','  no_braces
unknown's avatar
unknown committed
4938
	| no_braces;
unknown's avatar
unknown committed
4939 4940 4941 4942

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
unknown's avatar
unknown committed
4943
	ident_eq_value;
unknown's avatar
unknown committed
4944 4945

ident_eq_value:
unknown's avatar
unknown committed
4946
	simple_ident equal expr_or_default
unknown's avatar
unknown committed
4947
	 {
unknown's avatar
unknown committed
4948 4949 4950
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
unknown's avatar
unknown committed
4951
	    YYABORT;
unknown's avatar
unknown committed
4952
	 };
unknown's avatar
unknown committed
4953 4954

equal:	EQ		{}
unknown's avatar
unknown committed
4955 4956 4957 4958 4959 4960 4961
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
unknown's avatar
unknown committed
4962 4963 4964 4965 4966 4967 4968 4969 4970

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
unknown's avatar
unknown committed
4971 4972
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
4973
	    YYABORT;
unknown's avatar
unknown committed
4974
	 };
unknown's avatar
unknown committed
4975 4976 4977

opt_values:
	/* empty */ {}
unknown's avatar
unknown committed
4978
	| values;
unknown's avatar
unknown committed
4979 4980

values:
unknown's avatar
unknown committed
4981
	values ','  expr_or_default
unknown's avatar
unknown committed
4982 4983 4984 4985
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
unknown's avatar
unknown committed
4986 4987 4988 4989 4990 4991 4992 4993
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
4994
	expr	  { $$= $1;}
unknown's avatar
SCRUM  
unknown committed
4995
	| DEFAULT {$$= new Item_default_value(); }
unknown's avatar
unknown committed
4996
	;
unknown's avatar
unknown committed
4997

4998 4999
opt_insert_update:
        /* empty */
unknown's avatar
unknown committed
5000
        | ON DUPLICATE_SYM
5001 5002 5003 5004 5005 5006 5007 5008 5009
          { /* for simplisity, let's forget about
               INSERT ... SELECT ... UPDATE
               for a moment */
	    if (Lex->sql_command != SQLCOM_INSERT)
            {
	      send_error(Lex->thd, ER_SYNTAX_ERROR);
              YYABORT;
            }
          }
5010
          KEY_SYM UPDATE_SYM update_list
5011 5012
        ;

unknown's avatar
unknown committed
5013 5014 5015
/* Update rows in a table */

update:
5016 5017
	UPDATE_SYM
	{
5018
	  LEX *lex= Lex;
unknown's avatar
unknown committed
5019
	  mysql_init_select(lex);
5020
          lex->sql_command= SQLCOM_UPDATE;
5021
        }
unknown's avatar
unknown committed
5022 5023 5024
        opt_low_priority opt_ignore join_table_list
	SET update_list where_clause opt_order_clause delete_limit_clause
	{
5025
	  LEX *lex= Lex;
unknown's avatar
unknown committed
5026
	  Select->set_lock_for_tables($3);
5027
          if (lex->select_lex.table_list.elements > 1)
unknown's avatar
unknown committed
5028
            lex->sql_command= SQLCOM_UPDATE_MULTI;
unknown's avatar
unknown committed
5029 5030
	}
	;
unknown's avatar
unknown committed
5031 5032

update_list:
unknown's avatar
SCRUM  
unknown committed
5033
	update_list ',' simple_ident equal expr_or_default
unknown's avatar
unknown committed
5034
	{
unknown's avatar
unknown committed
5035
	  if (add_item_to_list(YYTHD, $3) || add_value_to_list(YYTHD, $5))
unknown's avatar
unknown committed
5036 5037
	    YYABORT;
	}
unknown's avatar
SCRUM  
unknown committed
5038
	| simple_ident equal expr_or_default
unknown's avatar
unknown committed
5039
	  {
unknown's avatar
unknown committed
5040
	    if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
unknown's avatar
unknown committed
5041
	      YYABORT;
unknown's avatar
unknown committed
5042
	  };
unknown's avatar
unknown committed
5043 5044

opt_low_priority:
5045
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
5046
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
5047 5048 5049 5050

/* Delete rows from a table */

delete:
5051
	DELETE_SYM
5052
	{
5053 5054
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
unknown's avatar
unknown committed
5055
	  lex->lock_option= lex->thd->update_lock_default;
5056
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
5057
	}
unknown's avatar
unknown committed
5058 5059
	opt_delete_options single_multi {}
	;
5060 5061

single_multi:
unknown's avatar
unknown committed
5062 5063
 	FROM table_ident
	{
unknown's avatar
unknown committed
5064 5065
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
unknown's avatar
unknown committed
5066 5067 5068
	    YYABORT;
	}
	where_clause opt_order_clause
5069
	delete_limit_clause {}
unknown's avatar
unknown committed
5070
	| table_wild_list
unknown's avatar
unknown committed
5071
	  { mysql_init_multi_delete(Lex); }
unknown's avatar
unknown committed
5072
          FROM join_table_list where_clause
unknown's avatar
unknown committed
5073 5074
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
5075 5076 5077
	  USING join_table_list where_clause
	  {}
	;
unknown's avatar
unknown committed
5078 5079 5080

table_wild_list:
	  table_wild_one {}
unknown's avatar
unknown committed
5081
	  | table_wild_list ',' table_wild_one {};
unknown's avatar
unknown committed
5082 5083

table_wild_one:
unknown's avatar
unknown committed
5084
	ident opt_wild opt_table_alias
unknown's avatar
unknown committed
5085
	{
unknown's avatar
unknown committed
5086 5087
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
					 TL_OPTION_UPDATING, Lex->lock_option))
unknown's avatar
unknown committed
5088 5089
	    YYABORT;
        }
unknown's avatar
unknown committed
5090
	| ident '.' ident opt_wild opt_table_alias
unknown's avatar
unknown committed
5091
	  {
unknown's avatar
unknown committed
5092 5093
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
unknown's avatar
unknown committed
5094 5095
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
unknown's avatar
unknown committed
5096
	      YYABORT;
unknown's avatar
unknown committed
5097
	  }
unknown's avatar
unknown committed
5098
	;
unknown's avatar
unknown committed
5099 5100

opt_wild:
5101
	/* empty */	{}
unknown's avatar
unknown committed
5102
	| '.' '*'	{};
unknown's avatar
unknown committed
5103 5104


5105
opt_delete_options:
unknown's avatar
unknown committed
5106
	/* empty */	{}
unknown's avatar
unknown committed
5107
	| opt_delete_option opt_delete_options {};
5108 5109

opt_delete_option:
5110
	QUICK		{ Select->options|= OPTION_QUICK; }
5111 5112
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
	| IGNORE_SYM	{ Lex->duplicates= DUP_IGNORE; };
5113

5114
truncate:
unknown's avatar
unknown committed
5115
	TRUNCATE_SYM opt_table_sym table_name
5116
	{
5117
	  LEX* lex= Lex;
5118
	  lex->sql_command= SQLCOM_TRUNCATE;
5119
	  lex->select_lex.options= 0;
5120
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
5121 5122
	}
	;
5123

unknown's avatar
unknown committed
5124 5125
opt_table_sym:
	/* empty */
unknown's avatar
unknown committed
5126
	| TABLE_SYM;
5127

unknown's avatar
unknown committed
5128 5129
/* Show things */

5130 5131
show:	SHOW
	{
unknown's avatar
unknown committed
5132 5133 5134 5135
	  LEX *lex=Lex;
	  lex->wild=0;
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
unknown's avatar
unknown committed
5136
	show_param
5137 5138
	{}
	;
unknown's avatar
unknown committed
5139 5140 5141 5142 5143

show_param:
	DATABASES wild
	  { Lex->sql_command= SQLCOM_SHOW_DATABASES; }
	| TABLES opt_db wild
unknown's avatar
unknown committed
5144
	  {
5145
	    LEX *lex= Lex;
unknown's avatar
unknown committed
5146
	    lex->sql_command= SQLCOM_SHOW_TABLES;
5147 5148
	    lex->select_lex.db= $2;
	    lex->select_lex.options= 0;
unknown's avatar
unknown committed
5149
	   }
unknown's avatar
unknown committed
5150
	| TABLE_SYM STATUS_SYM opt_db wild
unknown's avatar
unknown committed
5151
	  {
5152
	    LEX *lex= Lex;
unknown's avatar
unknown committed
5153
	    lex->sql_command= SQLCOM_SHOW_TABLES;
5154 5155
	    lex->select_lex.options|= SELECT_DESCRIBE;
	    lex->select_lex.db= $3;
unknown's avatar
unknown committed
5156
	  }
5157
	| OPEN_SYM TABLES opt_db wild
unknown's avatar
unknown committed
5158
	  {
5159
	    LEX *lex= Lex;
unknown's avatar
unknown committed
5160
	    lex->sql_command= SQLCOM_SHOW_OPEN_TABLES;
5161 5162
	    lex->select_lex.db= $3;
	    lex->select_lex.options= 0;
5163
	  }
unknown's avatar
unknown committed
5164
	| ENGINE_SYM storage_engines 
unknown's avatar
unknown committed
5165 5166
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
unknown's avatar
unknown committed
5167
	| opt_full COLUMNS from_or_in table_ident opt_db wild
unknown's avatar
unknown committed
5168 5169
	  {
	    Lex->sql_command= SQLCOM_SHOW_FIELDS;
unknown's avatar
unknown committed
5170 5171
	    if ($5)
	      $4->change_db($5);
unknown's avatar
unknown committed
5172
	    if (!Select->add_table_to_list(YYTHD, $4, NULL, 0))
unknown's avatar
unknown committed
5173 5174
	      YYABORT;
	  }
5175
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
unknown's avatar
unknown committed
5176
	  TEXT_STRING_sys AND MASTER_LOG_POS_SYM EQ ulonglong_num
5177
	  AND MASTER_SERVER_ID_SYM EQ
unknown's avatar
unknown committed
5178 5179 5180 5181 5182
	ULONG_NUM
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
5183
	    Lex->mi.server_id = $16;
unknown's avatar
unknown committed
5184
          }
5185
        | master_or_binary LOGS_SYM
unknown's avatar
unknown committed
5186 5187
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
5188 5189 5190 5191 5192
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
unknown's avatar
unknown committed
5193
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
unknown's avatar
unknown committed
5194
          {
5195 5196
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
5197
          } opt_limit_clause_init
unknown's avatar
unknown committed
5198 5199 5200 5201 5202
	| keys_or_index FROM table_ident opt_db
	  {
	    Lex->sql_command= SQLCOM_SHOW_KEYS;
	    if ($4)
	      $3->change_db($4);
unknown's avatar
unknown committed
5203
	    if (!Select->add_table_to_list(YYTHD, $3, NULL, 0))
unknown's avatar
unknown committed
5204 5205
	      YYABORT;
	  }
unknown's avatar
unknown committed
5206 5207 5208 5209 5210 5211 5212 5213
	| 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
5214 5215 5216 5217 5218 5219 5220
	    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
5221 5222 5223 5224 5225 5226
	  }
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
5227
        | COUNT_SYM '(' '*' ')' WARNINGS
5228
          { (void) create_select_for_variable("warning_count"); }
5229
        | COUNT_SYM '(' '*' ')' ERRORS
5230
	  { (void) create_select_for_variable("error_count"); }
5231
        | WARNINGS opt_limit_clause_init
unknown's avatar
unknown committed
5232
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
5233
        | ERRORS opt_limit_clause_init
5234
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
unknown's avatar
unknown committed
5235 5236
	| STATUS_SYM wild
	  { Lex->sql_command= SQLCOM_SHOW_STATUS; }
unknown's avatar
unknown committed
5237
        | INNOBASE_SYM STATUS_SYM
unknown's avatar
unknown committed
5238
          { Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS; WARN_DEPRECATED("SHOW INNODB STATUS", "SHOW ENGINE INNODB STATUS"); }
unknown's avatar
unknown committed
5239 5240
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
5241
	| opt_var_type VARIABLES wild
unknown's avatar
unknown committed
5242
	  {
5243
	    THD *thd= YYTHD;
unknown's avatar
unknown committed
5244 5245
	    thd->lex->sql_command= SQLCOM_SHOW_VARIABLES;
	    thd->lex->option_type= (enum_var_type) $1;
5246
	  }
5247
	| charset wild
5248
	  { Lex->sql_command= SQLCOM_SHOW_CHARSETS; }
5249 5250
	| COLLATION_SYM wild
	  { Lex->sql_command= SQLCOM_SHOW_COLLATIONS; }
5251
	| BERKELEY_DB_SYM LOGS_SYM
unknown's avatar
unknown committed
5252
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW BDB LOGS", "SHOW ENGINE BDB LOGS"); }
unknown's avatar
unknown committed
5253
	| LOGS_SYM
unknown's avatar
unknown committed
5254
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW LOGS", "SHOW ENGINE BDB LOGS"); }
unknown's avatar
unknown committed
5255
	| GRANTS FOR_SYM user
unknown's avatar
unknown committed
5256 5257 5258 5259 5260 5261
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
	    lex->grant_user->password.str=NullS;
	  }
unknown's avatar
unknown committed
5262
	| CREATE DATABASE opt_if_not_exists ident
unknown's avatar
unknown committed
5263 5264
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
unknown's avatar
unknown committed
5265 5266
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
unknown's avatar
unknown committed
5267
	  }
unknown's avatar
unknown committed
5268 5269 5270
        | CREATE TABLE_SYM table_ident
          {
	    Lex->sql_command = SQLCOM_SHOW_CREATE;
unknown's avatar
unknown committed
5271
	    if (!Select->add_table_to_list(YYTHD, $3, NULL,0))
unknown's avatar
unknown committed
5272 5273 5274 5275 5276
	      YYABORT;
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
unknown's avatar
unknown committed
5277
          }
unknown's avatar
unknown committed
5278 5279 5280
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
unknown's avatar
unknown committed
5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300
          }
	| CREATE PROCEDURE ident
	  {
	    Lex->sql_command = SQLCOM_SHOW_CREATE_PROC;
	    Lex->udf.name= $3;
	  }
	| CREATE FUNCTION_SYM ident
	  {
	    Lex->sql_command = SQLCOM_SHOW_CREATE_FUNC;
	    Lex->udf.name= $3;
	  }
	| PROCEDURE STATUS_SYM wild
	  {
	    Lex->sql_command = SQLCOM_SHOW_STATUS_PROC;
	  }
	| FUNCTION_SYM STATUS_SYM wild
	  {
	    Lex->sql_command = SQLCOM_SHOW_STATUS_FUNC;
	  };

unknown's avatar
unknown committed
5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323
show_engine_param:
	STATUS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_INNODB:
	      Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;
	      break;
	    default:
	      net_printf(YYTHD, ER_NOT_SUPPORTED_YET, "STATUS");
	      YYABORT;
	    }
	  }
	| LOGS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_BERKELEY_DB:
	      Lex->sql_command = SQLCOM_SHOW_LOGS;
	      break;
	    default:
	      net_printf(YYTHD, ER_NOT_SUPPORTED_YET, "LOGS");
	      YYABORT;
	    }
	  };
unknown's avatar
unknown committed
5324

5325 5326 5327 5328
master_or_binary:
	MASTER_SYM
	| BINARY;

unknown's avatar
unknown committed
5329 5330 5331 5332
opt_storage:
	/* empty */
	| STORAGE_SYM;

unknown's avatar
unknown committed
5333 5334
opt_db:
	/* empty */  { $$= 0; }
unknown's avatar
unknown committed
5335
	| from_or_in ident { $$= $2.str; };
unknown's avatar
unknown committed
5336 5337 5338

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

unknown's avatar
unknown committed
5341 5342
opt_full:
	/* empty */ { Lex->verbose=0; }
unknown's avatar
unknown committed
5343
	| FULL	    { Lex->verbose=1; };
unknown's avatar
unknown committed
5344

unknown's avatar
unknown committed
5345 5346
from_or_in:
	FROM
unknown's avatar
unknown committed
5347
	| IN_SYM;
unknown's avatar
unknown committed
5348

unknown's avatar
unknown committed
5349 5350
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
unknown's avatar
unknown committed
5351
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
unknown's avatar
unknown committed
5352 5353 5354

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


unknown's avatar
unknown committed
5358 5359 5360 5361
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
unknown's avatar
unknown committed
5362 5363 5364 5365
	  LEX *lex=Lex;
	  lex->wild=0;
	  lex->verbose=0;
	  lex->sql_command=SQLCOM_SHOW_FIELDS;
unknown's avatar
unknown committed
5366
	  if (!Select->add_table_to_list(lex->thd, $2, NULL,0))
unknown's avatar
unknown committed
5367 5368
	    YYABORT;
	}
5369
	opt_describe_column {}
unknown's avatar
unknown committed
5370 5371 5372
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
5373
          {
unknown's avatar
unknown committed
5374
	    LEX *lex=Lex;
5375
	    lex->select_lex.options|= SELECT_DESCRIBE;
unknown's avatar
unknown committed
5376 5377
	  }
	;
unknown's avatar
unknown committed
5378 5379 5380

describe_command:
	DESC
unknown's avatar
unknown committed
5381
	| DESCRIBE;
unknown's avatar
unknown committed
5382

unknown's avatar
unknown committed
5383 5384 5385 5386 5387
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
	;

unknown's avatar
unknown committed
5388 5389 5390
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
unknown's avatar
unknown committed
5391
	| ident
5392
	  { Lex->wild= new String((const char*) $1.str,$1.length,system_charset_info); };
unknown's avatar
unknown committed
5393 5394 5395 5396 5397


/* flush things */

flush:
5398
	FLUSH_SYM opt_no_write_to_binlog
unknown's avatar
unknown committed
5399 5400 5401
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
unknown's avatar
unknown committed
5402
          lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
5403
	}
5404 5405 5406
	flush_options
	{}
	;
unknown's avatar
unknown committed
5407 5408 5409

flush_options:
	flush_options ',' flush_option
unknown's avatar
unknown committed
5410
	| flush_option;
unknown's avatar
unknown committed
5411 5412

flush_option:
5413
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
unknown's avatar
unknown committed
5414
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
unknown's avatar
unknown committed
5415
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
unknown's avatar
unknown committed
5416 5417 5418 5419
	| 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
5420 5421
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
5422 5423
	| DES_KEY_FILE	{ Lex->type|= REFRESH_DES_KEY_FILE; }
 	| RESOURCES     { Lex->type|= REFRESH_USER_RESOURCES; };
unknown's avatar
unknown committed
5424

unknown's avatar
unknown committed
5425
opt_table_list:
5426 5427
	/* empty */  {;}
	| table_list {;};
unknown's avatar
unknown committed
5428

unknown's avatar
unknown committed
5429
reset:
unknown's avatar
unknown committed
5430 5431 5432 5433
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
5434 5435 5436 5437
	} reset_options
	{}
	;

unknown's avatar
unknown committed
5438 5439
reset_options:
	reset_options ',' reset_option
unknown's avatar
unknown committed
5440
	| reset_option;
unknown's avatar
unknown committed
5441 5442

reset_option:
unknown's avatar
unknown committed
5443 5444
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
unknown's avatar
unknown committed
5445
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
unknown's avatar
unknown committed
5446

unknown's avatar
unknown committed
5447
purge:
unknown's avatar
unknown committed
5448 5449 5450 5451
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
5452 5453 5454 5455 5456
	} purge_options
	{}
	;

purge_options:
5457
	master_or_binary LOGS_SYM purge_option
unknown's avatar
unknown committed
5458
	;
5459 5460

purge_option:
unknown's avatar
unknown committed
5461
        TO_SYM TEXT_STRING_sys
5462 5463 5464 5465 5466 5467 5468 5469 5470
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
	  if ($2->check_cols(1) || $2->fix_fields(Lex->thd, 0, &$2))
	  {
	    net_printf(Lex->thd, ER_WRONG_ARGUMENTS, "PURGE LOGS BEFORE");
unknown's avatar
unknown committed
5471
	    YYABORT;
5472 5473 5474
	  }
	  Item *tmp= new Item_func_unix_timestamp($2);
	  Lex->sql_command = SQLCOM_PURGE_BEFORE;
5475
	  Lex->purge_time= (ulong) tmp->val_int();
unknown's avatar
unknown committed
5476
	}
unknown's avatar
unknown committed
5477
	;
unknown's avatar
unknown committed
5478

unknown's avatar
unknown committed
5479 5480 5481
/* kill threads */

kill:
unknown's avatar
SCRUM  
unknown committed
5482
	KILL_SYM kill_option expr
unknown's avatar
unknown committed
5483
	{
unknown's avatar
unknown committed
5484
	  LEX *lex=Lex;
unknown's avatar
SCRUM  
unknown committed
5485
	  if ($3->fix_fields(lex->thd, 0, &$3) || $3->check_cols(1))
5486
	  {
5487
	    send_error(lex->thd, ER_SET_CONSTANTS_ONLY);
unknown's avatar
unknown committed
5488 5489 5490
	    YYABORT;
	  }
          lex->sql_command=SQLCOM_KILL;
unknown's avatar
SCRUM  
unknown committed
5491
	  lex->thread_id= (ulong) $3->val_int();
unknown's avatar
unknown committed
5492
	};
unknown's avatar
unknown committed
5493

unknown's avatar
SCRUM  
unknown committed
5494 5495 5496 5497 5498
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; };

unknown's avatar
unknown committed
5499 5500 5501
/* change database */

use:	USE_SYM ident
unknown's avatar
unknown committed
5502 5503
	{
	  LEX *lex=Lex;
5504 5505
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
unknown's avatar
unknown committed
5506
	};
unknown's avatar
unknown committed
5507 5508 5509

/* import, export of files */

unknown's avatar
unknown committed
5510
load:	LOAD DATA_SYM load_data_lock opt_local INFILE TEXT_STRING_sys
unknown's avatar
unknown committed
5511
	{
unknown's avatar
unknown committed
5512 5513
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
unknown's avatar
unknown committed
5514 5515
	  lex->lock_option= $3;
	  lex->local_file=  $4;
unknown's avatar
unknown committed
5516
	  if (!(lex->exchange= new sql_exchange($6.str,0)))
unknown's avatar
unknown committed
5517
	    YYABORT;
unknown's avatar
unknown committed
5518
	  lex->field_list.empty();
unknown's avatar
unknown committed
5519 5520 5521 5522
	}
	opt_duplicate INTO TABLE_SYM table_ident opt_field_term opt_line_term
	opt_ignore_lines opt_field_spec
	{
unknown's avatar
unknown committed
5523
	  if (!Select->add_table_to_list(YYTHD, $11, NULL, TL_OPTION_UPDATING))
unknown's avatar
unknown committed
5524 5525 5526
	    YYABORT;
	}
        |
unknown's avatar
unknown committed
5527
	LOAD TABLE_SYM table_ident FROM MASTER_SYM
unknown's avatar
unknown committed
5528 5529
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
unknown's avatar
unknown committed
5530
	  if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
unknown's avatar
unknown committed
5531
	    YYABORT;
unknown's avatar
unknown committed
5532 5533

        }
5534 5535 5536 5537
        |
	LOAD DATA_SYM FROM MASTER_SYM
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
unknown's avatar
unknown committed
5538
        };
unknown's avatar
unknown committed
5539 5540 5541

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

unknown's avatar
unknown committed
5544
load_data_lock:
5545
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
5546 5547
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
5548 5549


unknown's avatar
unknown committed
5550 5551 5552
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
unknown's avatar
unknown committed
5553
	| IGNORE_SYM	{ Lex->duplicates=DUP_IGNORE; };
unknown's avatar
unknown committed
5554 5555 5556

opt_field_term:
	/* empty */
unknown's avatar
unknown committed
5557
	| COLUMNS field_term_list;
unknown's avatar
unknown committed
5558 5559 5560

field_term_list:
	field_term_list field_term
unknown's avatar
unknown committed
5561
	| field_term;
unknown's avatar
unknown committed
5562 5563 5564 5565

field_term:
	TERMINATED BY text_string { Lex->exchange->field_term= $3;}
	| OPTIONALLY ENCLOSED BY text_string
unknown's avatar
unknown committed
5566 5567 5568 5569 5570
	  {
	    LEX *lex=Lex;
	    lex->exchange->enclosed= $4;
	    lex->exchange->opt_enclosed=1;
	  }
unknown's avatar
unknown committed
5571
	| ENCLOSED BY text_string { Lex->exchange->enclosed= $3;}
unknown's avatar
unknown committed
5572
	| ESCAPED BY text_string  { Lex->exchange->escaped= $3;};
unknown's avatar
unknown committed
5573 5574 5575

opt_line_term:
	/* empty */
unknown's avatar
unknown committed
5576
	| LINES line_term_list;
unknown's avatar
unknown committed
5577 5578 5579

line_term_list:
	line_term_list line_term
unknown's avatar
unknown committed
5580
	| line_term;
unknown's avatar
unknown committed
5581 5582 5583

line_term:
	TERMINATED BY text_string { Lex->exchange->line_term= $3;}
unknown's avatar
unknown committed
5584
	| STARTING BY text_string { Lex->exchange->line_start= $3;};
unknown's avatar
unknown committed
5585 5586 5587 5588

opt_ignore_lines:
	/* empty */
	| IGNORE_SYM NUM LINES
unknown's avatar
unknown committed
5589
	  { Lex->exchange->skip_lines=atol($2.str); };
unknown's avatar
unknown committed
5590 5591 5592 5593

/* Common definitions */

text_literal:
5594
	TEXT_STRING_literal
unknown's avatar
unknown committed
5595 5596
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
5597
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
unknown's avatar
unknown committed
5598
	}
unknown's avatar
unknown committed
5599
	| NCHAR_STRING
5600
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
5601
	| UNDERSCORE_CHARSET TEXT_STRING
5602
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
5603
	| text_literal TEXT_STRING_literal
unknown's avatar
unknown committed
5604 5605
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
unknown's avatar
unknown committed
5606 5607

text_string:
5608
	TEXT_STRING_literal
unknown's avatar
unknown committed
5609
	{ $$=  new String($1.str,$1.length,YYTHD->variables.collation_connection); }
unknown's avatar
unknown committed
5610 5611
	| HEX_NUM
	  {
5612
	    Item *tmp = new Item_varbinary($1.str,$1.length);
unknown's avatar
unknown committed
5613
	    $$= tmp ? tmp->val_str((String*) 0) : (String*) 0;
5614 5615 5616
	  }
	;

unknown's avatar
unknown committed
5617
param_marker:
5618
        '?'
unknown's avatar
unknown committed
5619
        {
5620
	  LEX *lex=Lex;
unknown's avatar
unknown committed
5621
          if (YYTHD->command == COM_PREPARE)
5622
          {
5623
            lex->param_list.push_back($$=new Item_param((uint)(lex->tok_start-(uchar *)YYTHD->query)));
5624
            lex->param_count++;
unknown's avatar
unknown committed
5625
          }
5626
          else
unknown's avatar
unknown committed
5627 5628 5629 5630
          {
            yyerror("You have an error in your SQL syntax");
            YYABORT;
          }
5631 5632 5633
        }
	;

unknown's avatar
unknown committed
5634 5635 5636 5637 5638 5639 5640
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
	| '-' NUM_literal { $$ = new Item_func_neg($2); }
	;


unknown's avatar
unknown committed
5641 5642
literal:
	text_literal	{ $$ =	$1; }
unknown's avatar
unknown committed
5643
	| NUM_literal	{ $$ = $1; }
unknown's avatar
unknown committed
5644
	| NULL_SYM	{ $$ =	new Item_null();
5645
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
5646
	| HEX_NUM	{ $$ =	new Item_varbinary($1.str,$1.length);}
5647
	| UNDERSCORE_CHARSET HEX_NUM
5648
	  {
5649 5650
	    Item *tmp= new Item_varbinary($2.str,$2.length);
	    String *str= tmp ? tmp->val_str((String*) 0) : (String*) 0;
unknown's avatar
unknown committed
5651 5652 5653
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
5654
	  }
unknown's avatar
unknown committed
5655 5656
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
unknown's avatar
unknown committed
5657
	| TIMESTAMP text_literal { $$ = $2; };
unknown's avatar
unknown committed
5658

5659 5660 5661 5662 5663 5664 5665 5666
NUM_literal:
	NUM		{ $$ =	new Item_int($1.str, (longlong) strtol($1.str, NULL, 10),$1.length); }
	| LONG_NUM	{ $$ =	new Item_int($1.str, (longlong) strtoll($1.str,NULL,10), $1.length); }
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
	| REAL_NUM	{ $$ =	new Item_real($1.str, $1.length); }
	| FLOAT_NUM	{ $$ =	new Item_float($1.str, $1.length); }
	;
	
unknown's avatar
unknown committed
5667 5668 5669 5670 5671 5672
/**********************************************************************
** Createing different items.
**********************************************************************/

insert_ident:
	simple_ident	 { $$=$1; }
unknown's avatar
unknown committed
5673
	| table_wild	 { $$=$1; };
unknown's avatar
unknown committed
5674 5675

table_wild:
unknown's avatar
unknown committed
5676
	ident '.' '*'
5677 5678
	{
	  $$ = new Item_field(NullS,$1.str,"*");
unknown's avatar
unknown committed
5679
	  Lex->current_select->with_wild++;
5680
	}
unknown's avatar
unknown committed
5681
	| ident '.' ident '.' '*'
5682 5683 5684 5685
	{
	  $$ = new Item_field((YYTHD->client_capabilities &
   			     CLIENT_NO_SCHEMA ? NullS : $1.str),
			     $3.str,"*");
unknown's avatar
unknown committed
5686
	  Lex->current_select->with_wild++;
5687 5688
	}
	;
unknown's avatar
unknown committed
5689 5690

order_ident:
unknown's avatar
unknown committed
5691
	expr { $$=$1; };
unknown's avatar
unknown committed
5692 5693 5694

simple_ident:
	ident
unknown's avatar
unknown committed
5695
	{
5696 5697 5698 5699 5700 5701 5702 5703
	  sp_pvar_t *spv;
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;

	  if (spc && (spv = spc->find_pvar(&$1)))
	  { /* We're compiling a stored procedure and found a variable */
	    if (lex->sql_command != SQLCOM_CALL && ! spv->isset)
	    {
5704 5705 5706
	      push_warning_printf(YYTHD, MYSQL_ERROR::WARN_LEVEL_WARN,
	                          ER_SP_UNINIT_VAR, ER(ER_SP_UNINIT_VAR),
				  $1.str);
5707
	    }
5708
	    $$ = (Item*) new Item_splocal($1, spv->offset);
5709 5710 5711
	  }
	  else
	  {
unknown's avatar
unknown committed
5712
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
5713 5714 5715 5716
	    $$= (sel->parsing_place != SELECT_LEX_NODE::IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
                 (Item*) new Item_field(NullS,NullS,$1.str) :
	         (Item*) new Item_ref(NullS,NullS,$1.str);
5717
	  }
unknown's avatar
unknown committed
5718
	}
unknown's avatar
unknown committed
5719
	| ident '.' ident
unknown's avatar
unknown committed
5720
	{
unknown's avatar
unknown committed
5721
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
5722
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
5723
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
5724 5725
	  if (sel->no_table_names_allowed)
	  {
unknown's avatar
unknown committed
5726
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE,
unknown's avatar
unknown committed
5727 5728 5729
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $1.str, thd->where);
	  }
unknown's avatar
unknown committed
5730 5731 5732 5733
	  $$= (sel->parsing_place != SELECT_LEX_NODE::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
5734
	}
unknown's avatar
unknown committed
5735
	| '.' ident '.' ident
unknown's avatar
unknown committed
5736
	{
unknown's avatar
unknown committed
5737
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
5738
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
5739
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
5740 5741
	  if (sel->no_table_names_allowed)
	  {
unknown's avatar
unknown committed
5742
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE,
unknown's avatar
unknown committed
5743 5744 5745
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $2.str, thd->where);
	  }
unknown's avatar
unknown committed
5746 5747 5748 5749
	  $$= (sel->parsing_place != SELECT_LEX_NODE::IN_HAVING ||
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field(NullS,$2.str,$4.str) :
              (Item*) new Item_ref(NullS,$2.str,$4.str);
unknown's avatar
unknown committed
5750
	}
unknown's avatar
unknown committed
5751
	| ident '.' ident '.' ident
unknown's avatar
unknown committed
5752
	{
unknown's avatar
unknown committed
5753
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
5754
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
5755
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
5756 5757
	  if (sel->no_table_names_allowed)
	  {
unknown's avatar
unknown committed
5758
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE,
unknown's avatar
unknown committed
5759 5760 5761
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $3.str, thd->where);
	  }
unknown's avatar
unknown committed
5762 5763 5764 5765 5766 5767 5768 5769
	  $$= (sel->parsing_place != SELECT_LEX_NODE::IN_HAVING ||
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field((YYTHD->client_capabilities &
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
	      (Item*) new Item_ref((YYTHD->client_capabilities &
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
                                   $3.str, $5.str);
unknown's avatar
unknown committed
5770
	};
unknown's avatar
unknown committed
5771 5772 5773 5774 5775


field_ident:
	ident			{ $$=$1;}
	| ident '.' ident	{ $$=$3;}	/* Skipp schema name in create*/
unknown's avatar
unknown committed
5776
	| '.' ident		{ $$=$2;}	/* For Delphi */;
unknown's avatar
unknown committed
5777 5778 5779

table_ident:
	ident			{ $$=new Table_ident($1); }
unknown's avatar
unknown committed
5780
	| ident '.' ident	{ $$=new Table_ident(YYTHD, $1,$3,0);}
unknown's avatar
unknown committed
5781 5782
	| '.' ident		{ $$=new Table_ident($2);}
   /* For Delphi */;
unknown's avatar
unknown committed
5783

unknown's avatar
unknown committed
5784
IDENT_sys:
5785 5786 5787 5788 5789 5790 5791 5792 5793 5794
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
	      $$= $1;
	    else
	      thd->convert_string(&$$, system_charset_info,
				  $1.str, $1.length, thd->charset());
	  }
unknown's avatar
unknown committed
5795 5796 5797 5798 5799 5800
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
5801 5802
	  if (thd->charset_is_system_charset)
	    $$= $1;
unknown's avatar
unknown committed
5803
	  else
unknown's avatar
unknown committed
5804 5805
	    thd->convert_string(&$$, system_charset_info,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
5806 5807 5808
	}
	;

5809
TEXT_STRING_literal:
unknown's avatar
unknown committed
5810 5811 5812
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
5813 5814
	  if (thd->charset_is_collation_connection)
	    $$= $1;
unknown's avatar
unknown committed
5815
	  else
unknown's avatar
unknown committed
5816 5817
	    thd->convert_string(&$$, thd->variables.collation_connection,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
5818 5819 5820 5821
	}
	;


unknown's avatar
unknown committed
5822
ident:
unknown's avatar
unknown committed
5823
	IDENT_sys	    { $$=$1; }
unknown's avatar
unknown committed
5824 5825
	| keyword
	{
unknown's avatar
unknown committed
5826 5827 5828
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
5829 5830
	}
	;
unknown's avatar
unknown committed
5831 5832

ident_or_text:
unknown's avatar
unknown committed
5833 5834 5835
	ident 			{ $$=$1;}
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
unknown's avatar
unknown committed
5836 5837 5838 5839

user:
	ident_or_text
	{
unknown's avatar
unknown committed
5840 5841
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
5842
	    YYABORT;
5843 5844 5845 5846
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
unknown's avatar
unknown committed
5847 5848
	| ident_or_text '@' ident_or_text
	  {
unknown's avatar
unknown committed
5849 5850
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
5851 5852
	      YYABORT;
	    $$->user = $1; $$->host=$3;
unknown's avatar
unknown committed
5853
	  };
unknown's avatar
unknown committed
5854 5855 5856 5857 5858

/* Keyword that we allow for identifiers */

keyword:
	ACTION			{}
unknown's avatar
unknown committed
5859
	| ADDDATE_SYM		{}
unknown's avatar
unknown committed
5860
	| AFTER_SYM		{}
5861
	| AGAINST		{}
unknown's avatar
unknown committed
5862
	| AGGREGATE_SYM		{}
unknown's avatar
unknown committed
5863
	| ANY_SYM		{}
unknown's avatar
unknown committed
5864
	| ASCII_SYM		{}
unknown's avatar
unknown committed
5865
	| AUTO_INC		{}
unknown's avatar
unknown committed
5866 5867
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
unknown's avatar
unknown committed
5868
	| BACKUP_SYM		{}
unknown's avatar
unknown committed
5869
	| BEGIN_SYM		{}
unknown's avatar
unknown committed
5870
	| BERKELEY_DB_SYM	{}
unknown's avatar
unknown committed
5871
	| BINLOG_SYM		{}
unknown's avatar
unknown committed
5872 5873
	| BIT_SYM		{}
	| BOOL_SYM		{}
unknown's avatar
unknown committed
5874
	| BOOLEAN_SYM		{}
5875
	| BYTE_SYM		{}
unknown's avatar
unknown committed
5876
	| BTREE_SYM		{}
unknown's avatar
unknown committed
5877
	| CACHE_SYM		{}
5878
	| CHANGED		{}
5879
	| CHARSET		{}
unknown's avatar
unknown committed
5880
	| CHECKSUM_SYM		{}
5881
	| CIPHER_SYM		{}
unknown's avatar
unknown committed
5882
	| CLIENT_SYM		{}
5883
	| CLOSE_SYM		{}
unknown's avatar
unknown committed
5884
	| COLLATION_SYM		{}
unknown's avatar
unknown committed
5885
	| COMMENT_SYM		{}
unknown's avatar
unknown committed
5886
	| COMMITTED_SYM		{}
unknown's avatar
unknown committed
5887
	| COMMIT_SYM		{}
unknown's avatar
unknown committed
5888
	| COMPRESSED_SYM	{}
unknown's avatar
unknown committed
5889
	| CONCURRENT		{}
5890
	| CUBE_SYM		{}
unknown's avatar
unknown committed
5891 5892 5893 5894
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
5895
	| DEFINER_SYM		{}
unknown's avatar
unknown committed
5896
	| DELAY_KEY_WRITE_SYM	{}
unknown's avatar
unknown committed
5897 5898
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
unknown's avatar
unknown committed
5899
	| DISCARD		{}
unknown's avatar
unknown committed
5900
	| DO_SYM		{}
5901
	| DUAL_SYM		{}
unknown's avatar
unknown committed
5902 5903
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
unknown's avatar
unknown committed
5904 5905 5906
	| DYNAMIC_SYM		{}
	| END			{}
	| ENUM			{}
unknown's avatar
unknown committed
5907
	| ENGINE_SYM		{}
unknown's avatar
unknown committed
5908
	| ENGINES_SYM		{}
5909
	| ERRORS		{}
unknown's avatar
unknown committed
5910
	| ESCAPE_SYM		{}
unknown's avatar
unknown committed
5911
	| EVENTS_SYM		{}
unknown's avatar
unknown committed
5912
	| EXECUTE_SYM		{}
5913
        | EXPANSION_SYM         {}
unknown's avatar
unknown committed
5914
	| EXTENDED_SYM		{}
5915
	| FAST_SYM		{}
unknown's avatar
unknown committed
5916 5917
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
unknown's avatar
unknown committed
5918
	| FULL			{}
unknown's avatar
unknown committed
5919 5920 5921 5922
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
	| FLUSH_SYM		{}
5923
	| GEOMETRY_SYM		{}
5924
	| GEOMETRYCOLLECTION	{}
5925
	| GET_FORMAT		{}
unknown's avatar
unknown committed
5926
	| GRANTS		{}
unknown's avatar
unknown committed
5927
	| GLOBAL_SYM		{}
5928
	| HANDLER_SYM		{}
unknown's avatar
unknown committed
5929
	| HASH_SYM		{}
5930
	| HELP_SYM		{}
unknown's avatar
unknown committed
5931 5932 5933
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
5934
	| INVOKER_SYM		{}
unknown's avatar
unknown committed
5935
	| IMPORT		{}
unknown's avatar
unknown committed
5936
	| INDEXES		{}
unknown's avatar
unknown committed
5937
	| ISOLATION		{}
unknown's avatar
unknown committed
5938
	| ISSUER_SYM		{}
unknown's avatar
unknown committed
5939
	| INNOBASE_SYM		{}
5940
	| INSERT_METHOD		{}
unknown's avatar
unknown committed
5941
	| RELAY_THREAD		{}
5942
	| LANGUAGE_SYM          {}
5943
	| LAST_SYM		{}
unknown's avatar
unknown committed
5944
	| LEAVES                {}
unknown's avatar
unknown committed
5945
	| LEVEL_SYM		{}
5946
	| LINESTRING		{}
unknown's avatar
unknown committed
5947
	| LOCAL_SYM		{}
unknown's avatar
unknown committed
5948
	| LOCKS_SYM		{}
unknown's avatar
unknown committed
5949 5950 5951 5952 5953 5954 5955 5956 5957 5958
	| LOGS_SYM		{}
	| MAX_ROWS		{}
	| MASTER_SYM		{}
	| MASTER_HOST_SYM	{}
	| MASTER_PORT_SYM	{}
	| MASTER_LOG_FILE_SYM	{}
	| MASTER_LOG_POS_SYM	{}
	| MASTER_USER_SYM	{}
	| MASTER_PASSWORD_SYM	{}
	| MASTER_CONNECT_RETRY_SYM	{}
unknown's avatar
unknown committed
5959 5960 5961 5962 5963 5964
	| 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
5965 5966 5967
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
5968
	| MEDIUM_SYM		{}
unknown's avatar
unknown committed
5969
	| MICROSECOND_SYM	{}
unknown's avatar
unknown committed
5970 5971 5972
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
unknown's avatar
unknown committed
5973
	| MODE_SYM		{}
unknown's avatar
unknown committed
5974
	| MONTH_SYM		{}
5975 5976 5977
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
unknown's avatar
unknown committed
5978
	| NAME_SYM              {}
unknown's avatar
unknown committed
5979
	| NAMES_SYM		{}
unknown's avatar
unknown committed
5980 5981
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
5982
	| NEXT_SYM		{}
unknown's avatar
unknown committed
5983
	| NEW_SYM		{}
unknown's avatar
unknown committed
5984
	| NO_SYM		{}
5985
	| NONE_SYM		{}
unknown's avatar
unknown committed
5986
	| NVARCHAR_SYM		{}
unknown's avatar
unknown committed
5987
	| OFFSET_SYM		{}
unknown's avatar
unknown committed
5988
	| OLD_PASSWORD		{}
5989
	| OPEN_SYM		{}
unknown's avatar
unknown committed
5990
	| PACK_KEYS_SYM		{}
5991
	| PARTIAL		{}
unknown's avatar
unknown committed
5992
	| PASSWORD		{}
5993
	| POINT_SYM		{}
5994
	| POLYGON		{}
5995
	| PREV_SYM		{}
unknown's avatar
unknown committed
5996 5997
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
5998
	| QUARTER_SYM		{}
unknown's avatar
unknown committed
5999
	| QUERY_SYM		{}
6000
	| QUICK			{}
unknown's avatar
unknown committed
6001
	| RAID_0_SYM		{}
unknown's avatar
unknown committed
6002 6003
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
unknown's avatar
unknown committed
6004
	| RAID_STRIPED_SYM	{}
unknown's avatar
unknown committed
6005
	| RAID_TYPE		{}
unknown's avatar
unknown committed
6006 6007
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
unknown's avatar
unknown committed
6008 6009
	| RELOAD		{}
	| REPAIR		{}
unknown's avatar
unknown committed
6010
	| REPEATABLE_SYM	{}
unknown's avatar
unknown committed
6011
	| REPLICATION		{}
unknown's avatar
unknown committed
6012
	| RESET_SYM		{}
6013
	| RESOURCES		{}
unknown's avatar
unknown committed
6014
	| RESTORE_SYM		{}
6015
	| RETURNS_SYM           {}
unknown's avatar
unknown committed
6016
	| ROLLBACK_SYM		{}
6017
	| ROLLUP_SYM		{}
unknown's avatar
unknown committed
6018 6019 6020
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
unknown's avatar
unknown committed
6021
	| RTREE_SYM		{}
6022
	| SAVEPOINT_SYM		{}
unknown's avatar
unknown committed
6023
	| SECOND_SYM		{}
6024
	| SECURITY_SYM		{}
6025
	| SERIAL_SYM		{}
unknown's avatar
unknown committed
6026 6027
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
unknown's avatar
unknown committed
6028
	| SIGNED_SYM		{}
6029
	| SIMPLE_SYM		{}
unknown's avatar
unknown committed
6030
	| SHARE_SYM		{}
unknown's avatar
unknown committed
6031
	| SHUTDOWN		{}
unknown's avatar
unknown committed
6032
	| SLAVE			{}
unknown's avatar
unknown committed
6033
	| SOUNDS_SYM		{}
unknown's avatar
unknown committed
6034
	| SQL_CACHE_SYM		{}
unknown's avatar
unknown committed
6035
	| SQL_BUFFER_RESULT	{}
unknown's avatar
unknown committed
6036
	| SQL_NO_CACHE_SYM	{}
unknown's avatar
unknown committed
6037
	| SQL_THREAD		{}
unknown's avatar
unknown committed
6038 6039 6040
	| START_SYM		{}
	| STATUS_SYM		{}
	| STOP_SYM		{}
unknown's avatar
unknown committed
6041
	| STORAGE_SYM		{}
unknown's avatar
unknown committed
6042
	| STRING_SYM		{}
unknown's avatar
unknown committed
6043
	| SUBDATE_SYM		{}
unknown's avatar
unknown committed
6044
	| SUBJECT_SYM		{}
unknown's avatar
unknown committed
6045
	| SUPER_SYM		{}
unknown's avatar
unknown committed
6046
	| TABLESPACE		{}
unknown's avatar
unknown committed
6047 6048
	| TEMPORARY		{}
	| TEXT_SYM		{}
unknown's avatar
unknown committed
6049
	| TRANSACTION_SYM	{}
6050
	| TRUNCATE_SYM		{}
unknown's avatar
unknown committed
6051
	| TIMESTAMP		{}
6052 6053
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
unknown's avatar
unknown committed
6054 6055
	| TIME_SYM		{}
	| TYPE_SYM		{}
unknown's avatar
unknown committed
6056
	| TYPES_SYM		{}
6057
	| FUNCTION_SYM		{}
unknown's avatar
unknown committed
6058
	| UNCOMMITTED_SYM	{}
unknown's avatar
unknown committed
6059
	| UNICODE_SYM		{}
6060
	| UNTIL_SYM		{}
6061
	| USER			{}
unknown's avatar
unknown committed
6062
	| USE_FRM		{}
unknown's avatar
unknown committed
6063
	| VARIABLES		{}
6064
	| VALUE_SYM		{}
6065
	| WARNINGS		{}
6066
	| WEEK_SYM		{}
unknown's avatar
unknown committed
6067
	| WORK_SYM		{}
6068
	| X509_SYM		{}
unknown's avatar
unknown committed
6069 6070
	| YEAR_SYM		{}
	;
unknown's avatar
unknown committed
6071 6072 6073 6074 6075 6076

/* Option functions */

set:
	SET opt_option
	{
unknown's avatar
unknown committed
6077 6078
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
unknown's avatar
unknown committed
6079 6080
	  lex->option_type=OPT_DEFAULT;
	  lex->var_list.empty();
unknown's avatar
unknown committed
6081
	}
6082 6083 6084
	option_value_list
	{}
	;
unknown's avatar
unknown committed
6085 6086 6087

opt_option:
	/* empty */ {}
unknown's avatar
unknown committed
6088
	| OPTION {};
unknown's avatar
unknown committed
6089 6090

option_value_list:
unknown's avatar
unknown committed
6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102
	option_type option_value
	| option_value_list ',' option_type option_value;

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

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
unknown's avatar
unknown committed
6103
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
6104 6105 6106 6107 6108 6109
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

opt_var_ident_type:
	/* empty */		{ $$=OPT_DEFAULT; }
unknown's avatar
unknown committed
6110
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
6111 6112 6113
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
unknown's avatar
unknown committed
6114 6115

option_value:
unknown's avatar
unknown committed
6116
	  '@' ident_or_text equal expr
unknown's avatar
unknown committed
6117
	  {
unknown's avatar
unknown committed
6118 6119
	    Lex->var_list.push_back(new set_var_user(new Item_func_set_user_var($2,$4)));
	  }
unknown's avatar
unknown committed
6120
	| internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
6121
	  {
unknown's avatar
unknown committed
6122
	    LEX *lex=Lex;
unknown's avatar
unknown committed
6123 6124 6125 6126 6127 6128 6129 6130

	    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 */
6131 6132 6133 6134 6135
	      if ($3 && $3->type() == Item::SUBSELECT_ITEM)
	      {  /* QQ For now, just disallow subselects as values */
	        send_error(lex->thd, ER_SP_SUBSELECT_NYI);
	        YYABORT;
	      }
unknown's avatar
unknown committed
6136 6137 6138 6139 6140 6141 6142
	      sp_pvar_t *spv= lex->spcont->find_pvar(&$1.base_name);
              sp_instr_set *i= new sp_instr_set(lex->sphead->instructions(),
	                                        spv->offset, $3, spv->type);

	      lex->sphead->add_instr(i);
	      spv->isset= TRUE;
	    }
unknown's avatar
unknown committed
6143
	  }
unknown's avatar
unknown committed
6144
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
6145 6146
	  {
	    LEX *lex=Lex;
unknown's avatar
unknown committed
6147 6148
	    lex->var_list.push_back(new set_var((enum_var_type) $3, $4.var,
						&$4.base_name, $6));
unknown's avatar
unknown committed
6149 6150 6151 6152
	  }
	| TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	  {
	    LEX *lex=Lex;
unknown's avatar
unknown committed
6153 6154 6155
	    LEX_STRING tmp;
	    tmp.str=0;
	    tmp.length=0;
unknown's avatar
unknown committed
6156
	    lex->var_list.push_back(new set_var(lex->option_type,
unknown's avatar
unknown committed
6157
						find_sys_var("tx_isolation"),
unknown's avatar
unknown committed
6158
						&tmp,
unknown's avatar
unknown committed
6159
						new Item_int((int32) $4)));
unknown's avatar
unknown committed
6160
	  }
6161
	| charset old_or_new_charset_name_or_default
unknown's avatar
unknown committed
6162
	{
unknown's avatar
unknown committed
6163
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
6164
	  LEX *lex= Lex;
6165
	  $2= $2 ? $2: global_system_variables.character_set_client;
6166
	  lex->var_list.push_back(new set_var_collation_client($2,thd->variables.collation_database,$2));
unknown's avatar
unknown committed
6167
	}
6168
	| NAMES_SYM charset_name_or_default opt_collate
6169
	{
unknown's avatar
unknown committed
6170
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
6171
	  LEX *lex= Lex;
6172
	  $2= $2 ? $2 : global_system_variables.character_set_client;
6173 6174
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
6175
	  {
unknown's avatar
unknown committed
6176 6177
	    net_printf(thd,ER_COLLATION_CHARSET_MISMATCH,$3->name,$2->csname);
	    YYABORT;
6178
	  }
6179
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
6180
	}
unknown's avatar
unknown committed
6181
	| PASSWORD equal text_or_password
unknown's avatar
unknown committed
6182
	  {
6183
	    THD *thd=YYTHD;
unknown's avatar
unknown committed
6184
	    LEX_USER *user;
unknown's avatar
unknown committed
6185
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
unknown's avatar
unknown committed
6186 6187 6188
	      YYABORT;
	    user->host.str=0;
	    user->user.str=thd->priv_user;
unknown's avatar
unknown committed
6189
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
unknown's avatar
unknown committed
6190 6191
	  }
	| PASSWORD FOR_SYM user equal text_or_password
unknown's avatar
unknown committed
6192
	  {
unknown's avatar
unknown committed
6193 6194
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
6195
	;
unknown's avatar
unknown committed
6196

unknown's avatar
unknown committed
6197 6198 6199
internal_variable_name:
	ident
	{
unknown's avatar
unknown committed
6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218
	  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;
	  }
	  else
	  { /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
unknown's avatar
unknown committed
6219
	}
6220 6221 6222 6223 6224 6225 6226
	| ident '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
	      net_printf(YYTHD, ER_VARIABLE_IS_NOT_STRUCT, $3.str);
unknown's avatar
unknown committed
6227 6228
	    $$.var= tmp;
	    $$.base_name= $1;
6229 6230 6231 6232 6233 6234 6235 6236
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
	      net_printf(YYTHD, ER_VARIABLE_IS_NOT_STRUCT, $3.str);
unknown's avatar
unknown committed
6237 6238 6239
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
6240
	  }
6241
        ;
unknown's avatar
unknown committed
6242 6243 6244 6245 6246 6247 6248

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

unknown's avatar
unknown committed
6250 6251 6252 6253
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
6254
	    $$= $3.length ? YYTHD->variables.old_passwords ?
unknown's avatar
unknown committed
6255 6256 6257 6258 6259 6260 6261 6262
	        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;
6263 6264
	  }
          ;
unknown's avatar
unknown committed
6265

unknown's avatar
unknown committed
6266

unknown's avatar
unknown committed
6267
set_expr_or_default:
unknown's avatar
unknown committed
6268 6269
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
6270 6271
	| ON	  { $$=new Item_string("ON",  2, system_charset_info); }
	| ALL	  { $$=new Item_string("ALL", 3, system_charset_info); }
unknown's avatar
unknown committed
6272
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
unknown's avatar
unknown committed
6273
	;
unknown's avatar
unknown committed
6274 6275


unknown's avatar
unknown committed
6276 6277 6278 6279 6280 6281 6282
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
	  Lex->sql_command=SQLCOM_LOCK_TABLES;
	}
6283 6284 6285
	table_lock_list
	{}
	;
unknown's avatar
unknown committed
6286 6287 6288

table_or_tables:
	TABLE_SYM
unknown's avatar
unknown committed
6289
	| TABLES;
unknown's avatar
unknown committed
6290 6291 6292

table_lock_list:
	table_lock
unknown's avatar
unknown committed
6293
	| table_lock_list ',' table_lock;
unknown's avatar
unknown committed
6294 6295 6296

table_lock:
	table_ident opt_table_alias lock_option
6297
	{
unknown's avatar
unknown committed
6298
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
6299 6300
	   YYABORT;
	}
6301
        ;
unknown's avatar
unknown committed
6302 6303 6304

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
6305
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
unknown's avatar
unknown committed
6306
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
6307 6308
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
unknown's avatar
unknown committed
6309 6310

unlock:
6311 6312
	UNLOCK_SYM table_or_tables { Lex->sql_command=SQLCOM_UNLOCK_TABLES; }
        ;
unknown's avatar
unknown committed
6313 6314


6315
/*
unknown's avatar
unknown committed
6316
** Handler: direct access to ISAM functions
6317 6318 6319 6320 6321
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
6322 6323
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_OPEN;
unknown's avatar
unknown committed
6324
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
6325 6326 6327 6328
	    YYABORT;
	}
	| HANDLER_SYM table_ident CLOSE_SYM
	{
6329 6330
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_CLOSE;
unknown's avatar
unknown committed
6331
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
6332 6333
	    YYABORT;
	}
unknown's avatar
unknown committed
6334
	| HANDLER_SYM table_ident READ_SYM
6335
	{
unknown's avatar
unknown committed
6336 6337 6338
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
6339 6340
	  lex->current_select->select_limit= 1;
	  lex->current_select->offset_limit= 0L;
unknown's avatar
unknown committed
6341
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
6342
	    YYABORT;
unknown's avatar
unknown committed
6343
        }
6344
        handler_read_or_scan where_clause opt_limit_clause {}
6345
        ;
6346

unknown's avatar
unknown committed
6347 6348
handler_read_or_scan:
	handler_scan_function         { Lex->backup_dir= 0; }
6349 6350
        | ident handler_rkey_function { Lex->backup_dir= $1.str; }
        ;
unknown's avatar
unknown committed
6351 6352 6353

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
6354 6355
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
unknown's avatar
unknown committed
6356 6357 6358 6359 6360 6361

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;  }
6362 6363
	| handler_rkey_mode
	{
unknown's avatar
unknown committed
6364 6365
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
unknown's avatar
unknown committed
6366
	  lex->ha_rkey_mode=$1;
unknown's avatar
unknown committed
6367
	  if (!(lex->insert_list = new List_item))
6368
	    YYABORT;
6369 6370
	} '(' values ')' { }
        ;
6371 6372

handler_rkey_mode:
unknown's avatar
unknown committed
6373 6374 6375 6376
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
6377 6378
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
6379

unknown's avatar
unknown committed
6380 6381 6382 6383 6384
/* GRANT / REVOKE */

revoke:
	REVOKE
	{
unknown's avatar
unknown committed
6385 6386 6387 6388 6389
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_REVOKE;
	  lex->users_list.empty();
	  lex->columns.empty();
	  lex->grant= lex->grant_tot_col=0;
6390
	  lex->select_lex.db=0;
6391 6392
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
6393
	  bzero((char*) &lex->mqh, sizeof(lex->mqh));
unknown's avatar
unknown committed
6394
	}
6395 6396 6397 6398 6399
	revoke_command
	{}
        ;

revoke_command:
6400 6401
	grant_privileges ON opt_table FROM user_list
	{}
6402 6403 6404 6405 6406
	|
	ALL PRIVILEGES ',' GRANT FROM user_list
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
6407
	;
unknown's avatar
unknown committed
6408 6409 6410 6411

grant:
	GRANT
	{
unknown's avatar
unknown committed
6412 6413 6414
	  LEX *lex=Lex;
	  lex->users_list.empty();
	  lex->columns.empty();
6415 6416
	  lex->sql_command = SQLCOM_GRANT;
	  lex->grant= lex->grant_tot_col= 0;
6417
	  lex->select_lex.db= 0;
6418 6419
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
6420
	  bzero((char *)&(lex->mqh),sizeof(lex->mqh));
unknown's avatar
unknown committed
6421 6422
	}
	grant_privileges ON opt_table TO_SYM user_list
6423 6424 6425
	require_clause grant_options
	{}
	;
unknown's avatar
unknown committed
6426 6427 6428

grant_privileges:
	grant_privilege_list {}
unknown's avatar
unknown committed
6429
	| ALL PRIVILEGES	{ Lex->grant = GLOBAL_ACLS;}
6430 6431
	| ALL			{ Lex->grant = GLOBAL_ACLS;}
        ;
unknown's avatar
unknown committed
6432 6433 6434

grant_privilege_list:
	grant_privilege
unknown's avatar
unknown committed
6435
	| grant_privilege_list ',' grant_privilege;
unknown's avatar
unknown committed
6436 6437

grant_privilege:
unknown's avatar
unknown committed
6438
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
6439 6440 6441
	| 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
6442 6443
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
unknown's avatar
unknown committed
6444 6445 6446 6447
	| INDEX		{ Lex->grant |= INDEX_ACL;}
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
unknown's avatar
unknown committed
6448
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
unknown's avatar
unknown committed
6449 6450 6451 6452
	| 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
6453 6454 6455 6456 6457 6458 6459 6460
	| GRANT OPTION  { Lex->grant |= GRANT_ACL;}
	| SHOW DATABASES { Lex->grant |= SHOW_DB_ACL;}
	| SUPER_SYM	{ Lex->grant |= SUPER_ACL;}
	| CREATE TEMPORARY TABLES { Lex->grant |= CREATE_TMP_ACL;}
	| LOCK_SYM TABLES   { Lex->grant |= LOCK_TABLES_ACL; }
	| REPLICATION SLAVE  { Lex->grant |= REPL_SLAVE_ACL;}
	| REPLICATION CLIENT_SYM { Lex->grant |= REPL_CLIENT_ACL;}
	;
unknown's avatar
unknown committed
6461

unknown's avatar
unknown committed
6462

6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474
opt_and:
	/* empty */	{}
	| AND		{}
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
unknown's avatar
unknown committed
6475 6476 6477 6478
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
6479
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "SUBJECT");
unknown's avatar
unknown committed
6480 6481 6482 6483 6484 6485 6486 6487 6488
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
6489
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "ISSUER");
unknown's avatar
unknown committed
6490 6491 6492 6493 6494 6495 6496 6497 6498
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
6499
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "CIPHER");
unknown's avatar
unknown committed
6500 6501 6502 6503 6504
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
6505

unknown's avatar
unknown committed
6506 6507 6508
opt_table:
	'*'
	  {
6509
	    LEX *lex= Lex;
unknown's avatar
unknown committed
6510
	    lex->current_select->db= lex->thd->db;
unknown's avatar
unknown committed
6511
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
6512 6513
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
6514
	    {
6515
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
6516
	      YYABORT;
unknown's avatar
unknown committed
6517
	    }
unknown's avatar
unknown committed
6518 6519 6520
	  }
	| ident '.' '*'
	  {
6521
	    LEX *lex= Lex;
unknown's avatar
unknown committed
6522
	    lex->current_select->db = $1.str;
unknown's avatar
unknown committed
6523
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
6524 6525
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
6526
	    {
6527
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
unknown's avatar
unknown committed
6528 6529 6530 6531 6532
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
6533
	    LEX *lex= Lex;
unknown's avatar
unknown committed
6534
	    lex->current_select->db = NULL;
unknown's avatar
unknown committed
6535 6536
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
unknown's avatar
unknown committed
6537
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
6538
	    {
6539
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
unknown's avatar
unknown committed
6540 6541 6542 6543 6544
	      YYABORT;
	    }
	  }
	| table_ident
	  {
unknown's avatar
unknown committed
6545
	    LEX *lex=Lex;
unknown's avatar
unknown committed
6546
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
unknown's avatar
unknown committed
6547
	      YYABORT;
unknown's avatar
unknown committed
6548
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
6549
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
6550 6551
	  }
          ;
unknown's avatar
unknown committed
6552 6553 6554


user_list:
6555 6556 6557 6558 6559 6560 6561
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' grant_user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
unknown's avatar
unknown committed
6562 6563 6564 6565 6566 6567 6568 6569


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
6570
             if (YYTHD->variables.old_passwords)
6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587
             {
               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
6588 6589 6590 6591 6592
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
	  { $$=$1; $1->password=$5 ; }
	| user
6593 6594
	  { $$=$1; $1->password.str=NullS; }
        ;
unknown's avatar
unknown committed
6595 6596 6597


opt_column_list:
unknown's avatar
unknown committed
6598 6599 6600 6601 6602
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
unknown's avatar
unknown committed
6603
	| '(' column_list ')';
unknown's avatar
unknown committed
6604 6605 6606

column_list:
	column_list ',' column_list_id
unknown's avatar
unknown committed
6607
	| column_list_id;
unknown's avatar
unknown committed
6608 6609 6610 6611

column_list_id:
	ident
	{
6612
	  String *new_str = new String((const char*) $1.str,$1.length,system_charset_info);
unknown's avatar
unknown committed
6613 6614
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
unknown's avatar
unknown committed
6615
	  LEX *lex=Lex;
unknown's avatar
unknown committed
6616 6617
	  while ((point=iter++))
	  {
6618 6619
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
unknown's avatar
unknown committed
6620 6621
		break;
	  }
unknown's avatar
unknown committed
6622
	  lex->grant_tot_col|= lex->which_columns;
unknown's avatar
unknown committed
6623
	  if (point)
unknown's avatar
unknown committed
6624
	    point->rights |= lex->which_columns;
unknown's avatar
unknown committed
6625
	  else
unknown's avatar
unknown committed
6626
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
6627 6628
	}
        ;
unknown's avatar
unknown committed
6629

unknown's avatar
unknown committed
6630 6631

require_clause: /* empty */
6632
        | REQUIRE_SYM require_list
6633 6634 6635
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
6636
        | REQUIRE_SYM SSL_SYM
6637 6638 6639
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
6640
        | REQUIRE_SYM X509_SYM
6641 6642 6643 6644 6645 6646 6647
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
6648
          ;
unknown's avatar
unknown committed
6649

unknown's avatar
unknown committed
6650
grant_options:
unknown's avatar
unknown committed
6651
	/* empty */ {}
unknown's avatar
unknown committed
6652
	| WITH grant_option_list;
unknown's avatar
unknown committed
6653

unknown's avatar
unknown committed
6654 6655
grant_option_list:
	grant_option_list grant_option {}
6656 6657
	| grant_option {}
        ;
unknown's avatar
unknown committed
6658 6659 6660

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
6661
        | MAX_QUERIES_PER_HOUR ULONG_NUM
unknown's avatar
unknown committed
6662
        {
6663
	  Lex->mqh.questions=$2;
6664
	  Lex->mqh.bits |= 1;
6665
	}
6666
        | MAX_UPDATES_PER_HOUR ULONG_NUM
6667
        {
6668
	  Lex->mqh.updates=$2;
6669
	  Lex->mqh.bits |= 2;
6670
	}
6671
        | MAX_CONNECTIONS_PER_HOUR ULONG_NUM
6672
        {
6673
	  Lex->mqh.connections=$2;
6674
	  Lex->mqh.bits |= 4;
6675 6676
	}
        ;
6677

unknown's avatar
unknown committed
6678
begin:
6679 6680
	BEGIN_SYM   { Lex->sql_command = SQLCOM_BEGIN;} opt_work {}
	;
unknown's avatar
unknown committed
6681 6682 6683

opt_work:
	/* empty */ {}
6684 6685
	| WORK_SYM {;}
        ;
unknown's avatar
unknown committed
6686 6687

commit:
unknown's avatar
unknown committed
6688
	COMMIT_SYM   { Lex->sql_command = SQLCOM_COMMIT;};
unknown's avatar
unknown committed
6689 6690

rollback:
unknown's avatar
unknown committed
6691
	ROLLBACK_SYM
6692 6693 6694 6695 6696
	{
	  Lex->sql_command = SQLCOM_ROLLBACK;
	}
	| ROLLBACK_SYM TO_SYM SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
6697
	  Lex->sql_command = SQLCOM_ROLLBACK_TO_SAVEPOINT;
6698 6699 6700 6701 6702 6703 6704 6705
	  Lex->savepoint_name = $4.str;
	};
savepoint:
	SAVEPOINT_SYM ident
	{
	  Lex->sql_command = SQLCOM_SAVEPOINT;
	  Lex->savepoint_name = $2.str;
	};
6706 6707

/*
unknown's avatar
unknown committed
6708
   UNIONS : glue selects together
6709 6710 6711
*/


6712
union_clause:
6713
	/* empty */ {}
unknown's avatar
unknown committed
6714 6715
	| union_list
	;
6716 6717

union_list:
unknown's avatar
unknown committed
6718
	UNION_SYM    union_option
6719 6720 6721 6722 6723
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
6724
	    net_printf(lex->thd, ER_WRONG_USAGE, "UNION", "INTO");
6725
	    YYABORT;
6726
	  }
6727
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
6728
	  {
6729
	    send_error(lex->thd, ER_SYNTAX_ERROR);
6730 6731
	    YYABORT;
	  }
6732
	  if (mysql_new_select(lex, 0))
6733
	    YYABORT;
unknown's avatar
unknown committed
6734
          mysql_init_select(lex);
6735
	  lex->current_select->linkage=UNION_TYPE;
unknown's avatar
unknown committed
6736
	}
6737
	select_init {}
6738
	;
6739 6740

union_opt:
unknown's avatar
unknown committed
6741
	union_list {}
unknown's avatar
unknown committed
6742 6743
	| optional_order_or_limit {}
	;
6744 6745

optional_order_or_limit:
6746
	/* Empty */ {}
6747 6748
	|
	  {
unknown's avatar
unknown committed
6749
	    THD *thd= YYTHD;
unknown's avatar
unknown committed
6750
	    LEX *lex= thd->lex;
unknown's avatar
unknown committed
6751
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
unknown's avatar
unknown committed
6752
	    SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
6753
	    SELECT_LEX_UNIT *unit= sel->master_unit();
unknown's avatar
unknown committed
6754
	    SELECT_LEX *fake= unit->fake_select_lex;
unknown's avatar
unknown committed
6755 6756 6757 6758 6759 6760
	    if (fake)
	    {
	      unit->global_parameters= fake;
	      fake->no_table_names_allowed= 1;
	      lex->current_select= fake;
	    }
unknown's avatar
unknown committed
6761
	    thd->where= "global ORDER clause";
6762
	  }
6763
	order_or_limit
unknown's avatar
unknown committed
6764 6765
          {
	    THD *thd= YYTHD;
unknown's avatar
unknown committed
6766
	    thd->lex->current_select->no_table_names_allowed= 0;
unknown's avatar
unknown committed
6767 6768
	    thd->where= "";
          }
6769 6770 6771
	;

order_or_limit:
6772 6773
	order_clause opt_limit_clause_init
	| limit_clause
6774
	;
unknown's avatar
unknown committed
6775 6776

union_option:
6777
	/* empty */ {}
6778
	| DISTINCT {}
unknown's avatar
unknown committed
6779
	| ALL {Select->master_unit()->union_option|= UNION_ALL;};
6780

unknown's avatar
unknown committed
6781 6782
singlerow_subselect:
	subselect_start singlerow_subselect_init
unknown's avatar
unknown committed
6783 6784 6785 6786
	subselect_end
	{
	  $$= $2;
	};
6787

unknown's avatar
unknown committed
6788
singlerow_subselect_init:
6789
	select_init2
unknown's avatar
unknown committed
6790
	{
6791 6792
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
unknown's avatar
unknown committed
6793
	};
unknown's avatar
unknown committed
6794 6795

exists_subselect:
unknown's avatar
unknown committed
6796 6797 6798 6799 6800
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
unknown's avatar
unknown committed
6801 6802

exists_subselect_init:
6803
	select_init2
unknown's avatar
unknown committed
6804
	{
6805 6806
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
unknown's avatar
unknown committed
6807
	};
6808

unknown's avatar
unknown committed
6809 6810 6811 6812 6813 6814 6815 6816
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
6817
  select_init2
unknown's avatar
unknown committed
6818
  {
unknown's avatar
unknown committed
6819
    $$= Lex->current_select->master_unit()->first_select();
unknown's avatar
unknown committed
6820 6821
  };

6822
subselect_start:
6823
	'(' SELECT_SYM
unknown's avatar
unknown committed
6824
	{
6825
	  LEX *lex=Lex;
6826
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
6827 6828 6829
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
	  {	
6830 6831 6832
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
unknown's avatar
unknown committed
6833 6834 6835
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
6836 6837

subselect_end:
unknown's avatar
unknown committed
6838 6839 6840
	')'
	{
	  LEX *lex=Lex;
6841
	  lex->current_select = lex->current_select->return_after_parsing();
unknown's avatar
unknown committed
6842
	};
unknown's avatar
unknown committed
6843