sql_yacc.yy 199 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 40 41
#include "sp_head.h"
#include "sp_pcontext.h"
#include "sp_rcontext.h"
#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

47
#define yyoverflow(A,B,C,D,E,F) {ulong val= *(F); if(my_yyoverflow((B), (D), &val)) { yyerror((char*) (A)); return 2; } else { *(F)= (YYSIZE_T)val; }}
unknown's avatar
unknown committed
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 55
/* Helper for parsing "IS [NOT] truth_value" */
inline Item *is_truth_value(Item *A, bool v1, bool v2)
unknown's avatar
unknown committed
56
{
57 58 59 60
  return new Item_func_if(create_func_ifnull(A,
	new Item_int((char *) (v2 ? "TRUE" : "FALSE"), v2, 1)),
  	new Item_int((char *) (v1 ? "TRUE" : "FALSE"), v1, 1),
	new Item_int((char *) (v1 ? "FALSE" : "TRUE"),!v1, 1));
unknown's avatar
unknown committed
61 62 63 64 65 66
}

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

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

%pure_parser					/* We have threads */

%token	END_OF_INPUT

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

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

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

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

unknown's avatar
unknown committed
463 464 465
%token  ERRORS
%token  WARNINGS

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

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

unknown's avatar
unknown committed
621 622 623 624
%token	SQL_BIG_RESULT
%token	SQL_CACHE_SYM
%token	SQL_CALC_FOUND_ROWS
%token	SQL_NO_CACHE_SYM
unknown's avatar
unknown committed
625
%token	SQL_SMALL_RESULT
unknown's avatar
unknown committed
626
%token  SQL_BUFFER_RESULT
unknown's avatar
unknown committed
627

628 629 630 631 632 633 634 635 636 637 638 639 640 641
%token  CURSOR_SYM
%token  ELSEIF_SYM
%token  ITERATE_SYM
%token  GOTO_SYM
%token  LABEL_SYM
%token  LEAVE_SYM
%token  LOOP_SYM
%token  REPEAT_SYM
%token  UNTIL_SYM
%token  WHILE_SYM
%token  ASENSITIVE_SYM
%token  INSENSITIVE_SYM
%token  SENSITIVE_SYM

unknown's avatar
unknown committed
642 643
%token  ISSUER_SYM
%token  SUBJECT_SYM
644
%token  CIPHER_SYM
unknown's avatar
unknown committed
645

646
%token  BEFORE_SYM
unknown's avatar
unknown committed
647
%left   SET_VAR
648 649
%left	OR_OR_SYM OR_SYM OR2_SYM XOR
%left	AND_SYM AND_AND_SYM
unknown's avatar
unknown committed
650 651 652 653 654 655
%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	'-' '+'
656
%left	'*' '/' '%' DIV_SYM MOD_SYM
unknown's avatar
unknown committed
657
%left   '^'
unknown's avatar
unknown committed
658
%left	NEG '~'
659
%right	NOT_SYM NOT2_SYM
unknown's avatar
unknown committed
660
%right	BINARY COLLATE_SYM
661

unknown's avatar
unknown committed
662
%type <lex_str>
663 664
	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
665
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
666
	NCHAR_STRING opt_component key_cache_name
667
        sp_opt_label
unknown's avatar
unknown committed
668 669 670 671 672

%type <lex_str_ptr>
	opt_table_alias

%type <table>
673
	table_ident table_ident_nodb references from_table_ident
unknown's avatar
unknown committed
674 675

%type <simple_string>
676
	remember_name remember_end opt_ident opt_db text_or_password
677
	opt_constraint constraint
unknown's avatar
unknown committed
678 679

%type <string>
680
	text_string opt_gconcat_separator
unknown's avatar
unknown committed
681 682

%type <num>
unknown's avatar
unknown committed
683
	type int_type real_type order_dir opt_field_spec lock_option
unknown's avatar
unknown committed
684
	udf_type if_exists opt_local opt_table_options table_options
685 686
        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
687
        opt_ignore_leaves fulltext_options spatial_type union_option
688
        start_transaction_opts
unknown's avatar
unknown committed
689 690

%type <ulong_num>
691
	ULONG_NUM raid_types merge_insert_types
unknown's avatar
unknown committed
692

unknown's avatar
unknown committed
693 694
%type <ulonglong_number>
	ulonglong_num
unknown's avatar
unknown committed
695

unknown's avatar
unknown committed
696 697 698
%type <lock_type>
	replace_lock_option opt_low_priority insert_lock_option load_data_lock

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

%type <item_num>
	NUM_literal
unknown's avatar
unknown committed
714 715

%type <item_list>
716 717
	expr_list udf_expr_list udf_expr_list2 when_list
	ident_list ident_list_arg
unknown's avatar
unknown committed
718 719

%type <key_type>
720
	key_type opt_unique_or_fulltext constraint_key_type
unknown's avatar
unknown committed
721

unknown's avatar
unknown committed
722 723 724
%type <key_alg>
	key_alg opt_btree_or_rtree

unknown's avatar
unknown committed
725 726 727 728 729 730 731
%type <string_list>
	key_usage_list

%type <key_part>
	key_part

%type <table_list>
732
	join_table_list  join_table
733
        table_factor table_ref
unknown's avatar
unknown committed
734

735
%type <date_time_type> date_time_type;
unknown's avatar
unknown committed
736 737
%type <interval> interval

738 739
%type <interval_time_st> interval_time_st

unknown's avatar
unknown committed
740
%type <db_type> storage_engines
unknown's avatar
unknown committed
741 742 743

%type <row_type> row_types

unknown's avatar
unknown committed
744
%type <tx_isolation> isolation_types
unknown's avatar
unknown committed
745

unknown's avatar
unknown committed
746 747
%type <ha_rkey_mode> handler_rkey_mode

748
%type <cast_type> cast_type
unknown's avatar
unknown committed
749

unknown's avatar
unknown committed
750 751 752 753 754 755
%type <udf_type> udf_func_type

%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword

%type <lex_user> user grant_user

756
%type <charset>
757
	opt_collate
758 759
	charset_name
	charset_name_or_default
unknown's avatar
unknown committed
760 761
	old_or_new_charset_name
	old_or_new_charset_name_or_default
762 763
	collation_name
	collation_name_or_default
764

unknown's avatar
unknown committed
765 766
%type <variable> internal_variable_name

unknown's avatar
unknown committed
767 768
%type <select_lex> in_subselect in_subselect_init

unknown's avatar
unknown committed
769 770
%type <boolfunc2creator> comp_op

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

808 809 810 811 812 813 814
%type <NONE> call sp_proc_stmts sp_proc_stmts1 sp_proc_stmt
%type <num>  sp_decl_idents sp_opt_inout sp_handler_type sp_hcond_list
%type <spcondtype> sp_cond sp_hcond
%type <spblock> sp_decls sp_decl
%type <lex> sp_cursor_stmt
%type <spname> sp_name

unknown's avatar
unknown committed
815 816
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
817
	',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_SYM BETWEEN_SYM CASE_SYM
818
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM
unknown's avatar
unknown committed
819 820 821 822 823
%%


query:
	END_OF_INPUT
unknown's avatar
unknown committed
824
	{
825
	   THD *thd= YYTHD;
826
	   if (!thd->bootstrap &&
827
	      (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
828
	   {
unknown's avatar
unknown committed
829
	     my_message(ER_EMPTY_QUERY, ER(ER_EMPTY_QUERY), MYF(0));
830
	     YYABORT;
831
	   }
832 833
	   else
	   {
834
	     thd->lex->sql_command= SQLCOM_EMPTY_QUERY;
835
	   }
unknown's avatar
unknown committed
836
	}
unknown's avatar
unknown committed
837
	| verb_clause END_OF_INPUT {};
unknown's avatar
unknown committed
838 839

verb_clause:
840 841 842 843 844 845
	  statement
	| begin
	;

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

unknown's avatar
unknown committed
893
deallocate:
894
        deallocate_or_drop PREPARE_SYM ident
unknown's avatar
unknown committed
895 896
        {
          THD *thd=YYTHD;
897
          LEX *lex= thd->lex;
unknown's avatar
unknown committed
898 899 900 901 902
          if (thd->command == COM_PREPARE)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
903
          lex->sql_command= SQLCOM_DEALLOCATE_PREPARE;
unknown's avatar
unknown committed
904 905 906
          lex->prepared_stmt_name= $3;
        };

907 908 909 910 911 912
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


unknown's avatar
unknown committed
913
prepare:
914
        PREPARE_SYM ident FROM prepare_src
unknown's avatar
unknown committed
915 916
        {
          THD *thd=YYTHD;
917
          LEX *lex= thd->lex;
unknown's avatar
unknown committed
918 919 920 921 922
          if (thd->command == COM_PREPARE)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
923
          lex->sql_command= SQLCOM_PREPARE;
unknown's avatar
unknown committed
924 925 926
          lex->prepared_stmt_name= $2;
        };

927 928 929 930 931 932
prepare_src:
        TEXT_STRING_sys
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $1;
unknown's avatar
unknown committed
933
          lex->prepared_stmt_code_is_varref= FALSE;
934 935 936 937 938 939
        }
        | '@' ident_or_text
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $2;
unknown's avatar
unknown committed
940
          lex->prepared_stmt_code_is_varref= TRUE;
941
        };
942

unknown's avatar
unknown committed
943 944 945 946
execute:
        EXECUTE_SYM ident
        {
          THD *thd=YYTHD;
947
          LEX *lex= thd->lex;
unknown's avatar
unknown committed
948 949 950 951 952
          if (thd->command == COM_PREPARE)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
953
          lex->sql_command= SQLCOM_EXECUTE;
unknown's avatar
unknown committed
954 955 956 957 958 959 960 961 962 963 964 965
          lex->prepared_stmt_name= $2;
        }
        execute_using
        {}
        ;

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
966 967
        execute_var_list ',' execute_var_ident
        | execute_var_ident
unknown's avatar
unknown committed
968 969 970 971 972 973 974 975
        ;

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

unknown's avatar
unknown committed
979 980
/* help */

981
help:
982
       HELP_SYM ident_or_text
unknown's avatar
unknown committed
983
       {
984 985 986
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
	  lex->help_arg= $2.str;
987
       };
unknown's avatar
unknown committed
988 989 990 991

/* change master */

change:
unknown's avatar
unknown committed
992
       CHANGE MASTER_SYM TO_SYM
unknown's avatar
unknown committed
993 994 995
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
unknown's avatar
unknown committed
996
	  bzero((char*) &lex->mi, sizeof(lex->mi));
997 998 999 1000
        }
       master_defs
	{}
       ;
unknown's avatar
unknown committed
1001 1002 1003

master_defs:
       master_def
unknown's avatar
unknown committed
1004
       | master_defs ',' master_def;
unknown's avatar
unknown committed
1005

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

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

/* create a table */

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

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

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

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

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

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

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

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

            if (!(sp= new sp_head()))
              YYABORT;
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
            sp->reset_thd_mem_root(YYTHD);
            sp->init(lex);
            
            sp->m_type= TYPE_ENUM_TRIGGER;
            lex->sphead= sp;
            /*
              We have to turn of CLIENT_MULTI_QUERIES while parsing a
              stored procedure, otherwise yylex will chop it into pieces
              at each ';'.
            */
            sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
            YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
            
            bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
            lex->sphead->m_chistics= &lex->sp_chistics;
            lex->sphead->m_body_begin= lex->tok_start;
          }
          sp_proc_stmt
          {
            LEX *lex= Lex;
            sp_head *sp= lex->sphead;
            
            lex->sql_command= SQLCOM_CREATE_TRIGGER;
            sp->init_strings(YYTHD, lex, NULL);
            /* Restore flag if it was cleared above */
            if (sp->m_old_cmq)
              YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
            sp->restore_thd_mem_root(YYTHD);

            lex->name_and_length= $3;

            /*
              We have to do it after parsing trigger body, because some of
              sp_proc_stmt alternatives are not saving/restoring LEX, so
              lex->query_tables can be wiped out.
              
              QQ: What are other consequences of this?
              
              QQ: Could we loosen lock type in certain cases ?
            */
            if (!lex->select_lex.add_table_to_list(YYTHD, $7, 
                                                   (LEX_STRING*) 0,
                                                   TL_OPTION_UPDATING,
                                                   TL_WRITE))
              YYABORT;
          }
	;

sp_name:
	  IDENT_sys '.' IDENT_sys
	  {
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
	| IDENT_sys
	  {
	    $$= sp_name_current_db_new(YYTHD, $1);
	  }
	;

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

	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
1320
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
	      YYABORT;
	    }
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
	    sp->init(lex);

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

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

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

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

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

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

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

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

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

/* Create characteristics */
sp_c_chistic:
	  sp_chistic            { }
	| DETERMINISTIC_SYM     { Lex->sp_chistics.detistic= TRUE; }
1418
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
	;

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

call:
	  CALL_SYM sp_name
	  {
	    LEX *lex = Lex;

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

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

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

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

sp_fdparams:
	  sp_fdparams ',' sp_fdparam
	| sp_fdparam
	;

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

	    if (spc->find_pvar(&$1, TRUE))
	    {
1480
	      my_error(ER_SP_DUP_PARAM, MYF(0), $1.str);
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum enum_field_types)$2, sp_param_in);
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

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

	    if (spc->find_pvar(&$2, TRUE))
	    {
1506
	      my_error(ER_SP_DUP_PARAM, MYF(0), $2.str);
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 1535 1536 1537 1538 1539 1540 1541 1542 1543
	      YYABORT;
	    }
	    spc->push_pvar(&$2, (enum enum_field_types)$3,
			   (sp_param_mode_t)$1);
	  }
	;

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

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

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

sp_decls:
	  /* Empty */
	  {
	    $$.vars= $$.conds= $$.hndlrs= $$.curs= 0;
	  }
	| sp_decls sp_decl ';'
	  {
	    /* We check for declarations out of (standard) order this way
	       because letting the grammar rules reflect it caused tricky
	       shift/reduce conflicts with the wrong result. (And we get
	       better error handling this way.) */
	    if (($2.vars || $2.conds) && ($1.curs || $1.hndlrs))
	    { /* Variable or condition following cursor or handler */
unknown's avatar
unknown committed
1544 1545
	      my_message(ER_SP_VARCOND_AFTER_CURSHNDLR,
                         ER(ER_SP_VARCOND_AFTER_CURSHNDLR), MYF(0));
1546 1547 1548 1549
	      YYABORT;
	    }
	    if ($2.curs && $1.hndlrs)
	    { /* Cursor following handler */
unknown's avatar
unknown committed
1550 1551
	      my_message(ER_SP_CURSOR_AFTER_HANDLER,
                         ER(ER_SP_CURSOR_AFTER_HANDLER), MYF(0));
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
	      YYABORT;
	    }
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
	    $$.curs= $1.curs + $2.curs;
	  }
	;

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

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

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

	    if (spc->find_cond(&$2, TRUE))
	    {
1599
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
	      YYABORT;
	    }
	    YYTHD->lex->spcont->push_cond(&$2, $5);
	    $$.vars= $$.hndlrs= $$.curs= 0;
	    $$.conds= 1;
	  }
	| DECLARE_SYM sp_handler_type HANDLER_SYM FOR_SYM
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_instr_hpush_jump *i=
              new sp_instr_hpush_jump(sp->instructions(), ctx, $2,
	                              ctx->current_pvars());

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

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

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

	    if (ctx->find_cursor(&$2, &offp, TRUE))
	    {
1656
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
	      delete $5;
	      YYABORT;
	    }
            i= new sp_instr_cpush(sp->instructions(), ctx, $5);
	    sp->add_instr(i);
	    ctx->push_cursor(&$2);
	    $$.vars= $$.conds= $$.hndlrs= 0;
	    $$.curs= 1;
	  }
	;

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

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

	    if (lex->sql_command != SQLCOM_SELECT)
	    {
unknown's avatar
unknown committed
1683 1684
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
1685 1686 1687 1688
	      YYABORT;
	    }
	    if (lex->result)
	    {
unknown's avatar
unknown committed
1689 1690
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
	      YYABORT;
	    }
	    lex->sp_lex_in_use= TRUE;
	    $$= lex;
	    lex->sphead->restore_lex(YYTHD);
	  }
	;

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

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

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

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

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

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

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
1760
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
1761 1762 1763 1764 1765 1766 1767 1768
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
1769
	| not FOUND_SYM		/* SQLSTATEs 02??? */
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::notfound;
	  }
	| SQLEXCEPTION_SYM	/* All other SQLSTATEs */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::exception;
	  }
	;

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

	    if (spc->find_pvar(&$1, TRUE))
	    {
1789
	      my_error(ER_SP_DUP_VAR, MYF(0), $1.str);
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum_field_types)0, sp_param_in);
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$3, TRUE))
	    {
1802
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
	      YYABORT;
	    }
	    spc->push_pvar(&$3, (enum_field_types)0, sp_param_in);
	    $$= $1 + 1;
	  }
	;

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

sp_proc_stmt:
	  {
	    LEX *lex= Lex;

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

	    if ((lex->sql_command == SQLCOM_SELECT && !lex->result) ||
	        sp_multi_results_command(lex->sql_command))
	    {
	      /* We maybe have one or more SELECT without INTO */
	      sp->m_multi_results= TRUE;
	    }
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
unknown's avatar
unknown committed
1835
	      my_message(ER_SP_NO_USE, ER(ER_SP_NO_USE), MYF(0));
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
	      YYABORT;
	    }
	    /* Don't add an instruction for empty SET statements.
	    ** (This happens if the SET only contained local variables,
	    **  which get their set instructions generated separately.)
	    */
	    if (lex->sql_command != SQLCOM_SET_OPTION ||
		! lex->var_list.is_empty())
	    {
              /*
                Currently we can't handle queries inside a FUNCTION or
                TRIGGER, because of the way table locking works. This is 
                unfortunate, and limits the usefulness of functions and
                especially triggers a tremendously, but it's nothing we 
                can do about this at the moment.
              */
	      if (sp->m_type != TYPE_ENUM_PROCEDURE)
	      {
unknown's avatar
unknown committed
1854
		my_message(ER_SP_BADSTATEMENT, ER(ER_SP_BADSTATEMENT), MYF(0));
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
		YYABORT;
	      }
	      else
	      {
		sp_instr_stmt *i=new sp_instr_stmt(sp->instructions(),
						   lex->spcont);

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

	    if (lex->sphead->m_type == TYPE_ENUM_PROCEDURE)
	    {
unknown's avatar
unknown committed
1884
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
1885 1886 1887 1888 1889 1890 1891 1892
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_freturn *i;

	      if ($2->type() == Item::SUBSELECT_ITEM)
	      {  /* QQ For now, just disallow subselects as values */
unknown's avatar
unknown committed
1893
	        my_message(ER_SP_BADSTATEMENT, ER(ER_SP_BADSTATEMENT), MYF(0));
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
	        YYABORT;
	      }
	      i= new sp_instr_freturn(lex->sphead->instructions(),
				      lex->spcont,
		                      $2, lex->sphead->m_returns);
	      lex->sphead->add_instr(i);
	      lex->sphead->m_has_return= TRUE;
	    }
	  }
	| IF sp_if END IF {}
	| CASE_SYM WHEN_SYM
	  {
	    Lex->sphead->m_simple_case= FALSE;
	  }
	  sp_case END CASE_SYM {}
	| CASE_SYM expr WHEN_SYM
	  {
	    /* We "fake" this by using an anonymous variable which we
	       set to the expression. Note that all WHENs are evaluate
	       at the same frame level, so we then know that it's the
	       top-most variable in the frame. */
	    LEX *lex= Lex;
	    uint offset= lex->spcont->current_pvars();
	    sp_instr_set *i = new sp_instr_set(lex->sphead->instructions(),
					       lex->spcont,
	                                       offset, $2, MYSQL_TYPE_STRING);
	    LEX_STRING dummy;

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

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

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

	    if (! lab)
	    {
1956
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
	      YYABORT;
	    }
	    else
	    {
	      uint ip= sp->instructions();
	      sp_instr_jump *i;
	      sp_instr_hpop *ih;
	      sp_instr_cpop *ic;

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

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

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

	    if (lab)
	    {
2014
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $2.str);
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
	      YYABORT;
	    }
	    else
	    {
	      lab= ctx->push_label($2.str, sp->instructions());
	      lab->type= SP_LAB_GOTO;
	      lab->ctx= ctx;
              sp->backpatch(lab);
	    }
	  }
	| GOTO_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab;
	    sp_instr_jump *i;
	    sp_instr_hpop *ih;
	    sp_instr_cpop *ic;

	    if (sp->m_in_handler)
	    {
unknown's avatar
unknown committed
2038
	      my_message(ER_SP_GOTO_IN_HNDLR, ER(ER_SP_GOTO_IN_HNDLR), MYF(0));
2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
	      YYABORT;
	    }
	    lab= ctx->find_label($2.str);
	    if (! lab)
	    {
	      lab= (sp_label_t *)YYTHD->alloc(sizeof(sp_label_t));
	      lab->name= $2.str;
	      lab->ip= 0;
	      lab->type= SP_LAB_REF;
	      lab->ctx= ctx;

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

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

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2089
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
	      YYABORT;
	    }
	    i= new sp_instr_copen(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
	| FETCH_SYM sp_opt_fetch_noise ident INTO
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cfetch *i;

	    if (! lex->spcont->find_cursor(&$3, &offset))
	    {
2104
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $3.str);
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
	      YYABORT;
	    }
	    i= new sp_instr_cfetch(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
	  sp_fetch_list
	  { }
	| CLOSE_SYM ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cclose *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2121
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
	      YYABORT;
	    }
	    i= new sp_instr_cclose(sp->instructions(), lex->spcont,  offset);
	    sp->add_instr(i);
	  }
	;

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

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

	    if (!spc || !(spv = spc->find_pvar(&$1)))
	    {
2145
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
	      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)))
	    {
2167
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $3.str);
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

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

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

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

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

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

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

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

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

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

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

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

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

2268 2269 2270 2271 2272 2273 2274
sp_whens:
	  /* Empty */
	  {
	    sp_head *sp= Lex->sphead;
	    uint ip= sp->instructions();
	    sp_instr_error *i= new sp_instr_error(ip, Lex->spcont,
						  ER_SP_CASE_NOT_FOUND);
unknown's avatar
unknown committed
2275

2276 2277 2278 2279 2280
	    sp->add_instr(i);
	  }
	| ELSE sp_proc_stmts1 {}
	| WHEN_SYM sp_case {}
	;
unknown's avatar
unknown committed
2281

2282 2283
sp_labeled_control:
	  IDENT ':'
unknown's avatar
unknown committed
2284
	  {
2285 2286 2287 2288 2289 2290
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);

	    if (lab)
	    {
2291
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $1.str);
unknown's avatar
unknown committed
2292
	      YYABORT;
2293 2294 2295 2296 2297 2298 2299
	    }
	    else
	    {
	      lab= lex->spcont->push_label($1.str,
	                                   lex->sphead->instructions());
	      lab->type= SP_LAB_ITER;
	    }
unknown's avatar
unknown committed
2300
	  }
2301
	  sp_unlabeled_control sp_opt_label
unknown's avatar
unknown committed
2302
	  {
2303
	    LEX *lex= Lex;
unknown's avatar
unknown committed
2304

2305 2306 2307 2308 2309 2310 2311
	    if ($5.str)
	    {
	      sp_label_t *lab= lex->spcont->find_label($5.str);

	      if (!lab ||
	          my_strcasecmp(system_charset_info, $5.str, lab->name) != 0)
	      {
2312
	        my_error(ER_SP_LABEL_MISMATCH, MYF(0), $5.str);
2313 2314 2315 2316
	        YYABORT;
	      }
	    }
	    lex->sphead->backpatch(lex->spcont->pop_label());
unknown's avatar
unknown committed
2317
	  }
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340
	;

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

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

	    lab->type= SP_LAB_BEGIN;
	    lex->spcont= lex->spcont->push_context();
	  }
	  sp_decls
	  sp_proc_stmts
	  END
2341
	  {
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;

  	    sp->backpatch(ctx->last_label());	/* We always have a label */
	    if ($3.hndlrs)
	      sp->add_instr(new sp_instr_hpop(sp->instructions(), ctx,
					      $3.hndlrs));
	    if ($3.curs)
	      sp->add_instr(new sp_instr_cpop(sp->instructions(), ctx,
					      $3.curs));
	    lex->spcont= ctx->pop_context();
	  }
	| LOOP_SYM
	  sp_proc_stmts1 END LOOP_SYM
unknown's avatar
unknown committed
2357
	  {
2358 2359 2360 2361 2362 2363
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lex->spcont, lab->ip);

	    lex->sphead->add_instr(i);
unknown's avatar
unknown committed
2364
	  }
2365
	| WHILE_SYM expr DO_SYM
unknown's avatar
unknown committed
2366
	  {
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, lex->spcont,
							       $2);

	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
	    i->tables= lex->query_tables;
	    lex->query_tables= 0;
            sp->add_instr(i);
unknown's avatar
unknown committed
2378
	  }
2379
	  sp_proc_stmts1 END WHILE_SYM
unknown's avatar
unknown committed
2380
	  {
2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lex->spcont, lab->ip);

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

	    i->tables= lex->query_tables;
	    lex->query_tables= 0;
            lex->sphead->add_instr(i);
2399
	  }
2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415
	;

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

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

create2:
unknown's avatar
unknown committed
2419 2420 2421 2422 2423 2424
        '(' create2a {}
        | opt_create_table_options create3 {}
        | LIKE table_ident
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$2))
unknown's avatar
unknown committed
2425
              YYABORT;
unknown's avatar
unknown committed
2426 2427 2428 2429 2430
          }
        | '(' LIKE table_ident ')'
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$3))
2431
              YYABORT;
unknown's avatar
unknown committed
2432
          }
unknown's avatar
unknown committed
2433
        ;
unknown's avatar
unknown committed
2434

unknown's avatar
unknown committed
2435 2436
create2a:
        field_list ')' opt_create_table_options create3 {}
unknown's avatar
unknown committed
2437
	|  create_select ')' { Select->set_braces(1);} union_opt {}
2438
        ;
unknown's avatar
unknown committed
2439 2440

create3:
2441
	/* empty */ {}
unknown's avatar
unknown committed
2442
	| opt_duplicate opt_as     create_select
unknown's avatar
unknown committed
2443
          { Select->set_braces(0);} union_clause {}
unknown's avatar
unknown committed
2444
	| opt_duplicate opt_as '(' create_select ')'
unknown's avatar
unknown committed
2445
          { Select->set_braces(1);} union_opt {}
2446 2447
        ;

unknown's avatar
unknown committed
2448
create_select:
2449
          SELECT_SYM
unknown's avatar
unknown committed
2450
          {
unknown's avatar
unknown committed
2451 2452
	    LEX *lex=Lex;
	    lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
2453 2454 2455 2456
	    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;
2457 2458 2459 2460
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
unknown's avatar
unknown committed
2461
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
unknown's avatar
unknown committed
2462
	    mysql_init_select(lex);
2463
	    lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
2464
          }
unknown's avatar
unknown committed
2465 2466
          select_options select_item_list
	  {
2467
	    Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
2468
	  }
unknown's avatar
unknown committed
2469
	  opt_select_from
2470 2471 2472 2473 2474 2475 2476
	  {
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
	    Lex->current_select->table_list.push_front(&Lex->save_list);
	  }
unknown's avatar
unknown committed
2477
        ;
unknown's avatar
unknown committed
2478

2479 2480
opt_as:
	/* empty */ {}
unknown's avatar
unknown committed
2481
	| AS	    {};
2482

2483 2484 2485 2486 2487 2488 2489 2490 2491
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
2492 2493
	default_collation   {}
	| default_charset   {};
2494

unknown's avatar
unknown committed
2495
opt_table_options:
unknown's avatar
unknown committed
2496
	/* empty */	 { $$= 0; }
unknown's avatar
unknown committed
2497
	| table_options  { $$= $1;};
unknown's avatar
unknown committed
2498 2499 2500

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

unknown's avatar
unknown committed
2503
table_option:
unknown's avatar
unknown committed
2504
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
unknown's avatar
unknown committed
2505 2506

opt_if_not_exists:
unknown's avatar
unknown committed
2507
	/* empty */	 { $$= 0; }
2508
	| IF not EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
unknown's avatar
unknown committed
2509 2510 2511

opt_create_table_options:
	/* empty */
unknown's avatar
unknown committed
2512
	| create_table_options;
unknown's avatar
unknown committed
2513

unknown's avatar
unknown committed
2514 2515 2516 2517
create_table_options_space_separated:
	create_table_option
	| create_table_option create_table_options_space_separated;

unknown's avatar
unknown committed
2518 2519
create_table_options:
	create_table_option
2520
	| create_table_option     create_table_options
unknown's avatar
unknown committed
2521
	| create_table_option ',' create_table_options;
unknown's avatar
unknown committed
2522 2523

create_table_option:
2524 2525
	ENGINE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; }
	| TYPE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; WARN_DEPRECATED("TYPE=storage_engine","ENGINE=storage_engine");   Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; }
2526 2527 2528
	| 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;}
2529 2530
	| PASSWORD opt_equal TEXT_STRING_sys	{ Lex->create_info.password=$3.str; Lex->create_info.used_fields|= HA_CREATE_USED_PASSWORD; }
	| COMMENT_SYM opt_equal TEXT_STRING_sys	{ Lex->create_info.comment=$3.str; Lex->create_info.used_fields|= HA_CREATE_USED_COMMENT; }
2531 2532 2533
	| 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;}
2534 2535 2536
	| CHECKSUM_SYM opt_equal ULONG_NUM	{ Lex->create_info.table_options|= $3 ? HA_OPTION_CHECKSUM : HA_OPTION_NO_CHECKSUM; Lex->create_info.used_fields|= HA_CREATE_USED_CHECKSUM; }
	| DELAY_KEY_WRITE_SYM opt_equal ULONG_NUM { Lex->create_info.table_options|= $3 ? HA_OPTION_DELAY_KEY_WRITE : HA_OPTION_NO_DELAY_KEY_WRITE;  Lex->create_info.used_fields|= HA_CREATE_USED_DELAY_KEY_WRITE; }
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
2537 2538 2539 2540
	| 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 ')'
2541 2542 2543
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
2544 2545
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
2546
	    lex->create_info.merge_list.elements--;
2547 2548
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
2549
	    lex->select_lex.table_list.elements=1;
2550 2551 2552
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
2553
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
2554
	  }
2555 2556
	| default_charset
	| default_collation
2557
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
2558 2559 2560
	| DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.data_file_name= $4.str; Lex->create_info.used_fields|= HA_CREATE_USED_DATADIR; }
	| INDEX_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.index_file_name= $4.str;  Lex->create_info.used_fields|= HA_CREATE_USED_INDEXDIR; }
        ;
unknown's avatar
unknown committed
2561

2562 2563 2564 2565 2566 2567 2568 2569
default_charset:
        opt_default charset opt_equal charset_name_or_default
        {
          HA_CREATE_INFO *cinfo= &Lex->create_info;
          if ((cinfo->used_fields & HA_CREATE_USED_DEFAULT_CHARSET) &&
               cinfo->default_table_charset && $4 &&
               !my_charset_same(cinfo->default_table_charset,$4))
          {
2570 2571 2572
            my_error(ER_CONFLICTING_DECLARATIONS, MYF(0),
                     "CHARACTER SET ", cinfo->default_table_charset->csname,
                     "CHARACTER SET ", $4->csname);
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586
            YYABORT;
          }
	  Lex->create_info.default_table_charset= $4;
          Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

default_collation:
        opt_default COLLATE_SYM opt_equal collation_name_or_default
        {
          HA_CREATE_INFO *cinfo= &Lex->create_info;
          if ((cinfo->used_fields & HA_CREATE_USED_DEFAULT_CHARSET) &&
               cinfo->default_table_charset && $4 &&
               !my_charset_same(cinfo->default_table_charset,$4))
            {
2587 2588
              my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $4->name, cinfo->default_table_charset->csname);
2589 2590 2591 2592 2593 2594
              YYABORT;
            }
            Lex->create_info.default_table_charset= $4;
            Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

unknown's avatar
unknown committed
2595
storage_engines:
2596 2597 2598 2599
	ident_or_text
	{
	  $$ = ha_resolve_by_name($1.str,$1.length);
	  if ($$ == DB_TYPE_UNKNOWN) {
2600
	    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
2601 2602 2603
	    YYABORT;
	  }
	};
unknown's avatar
unknown committed
2604 2605 2606 2607 2608

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
unknown's avatar
unknown committed
2609
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; };
unknown's avatar
unknown committed
2610 2611 2612 2613

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

2616 2617 2618
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
unknown's avatar
unknown committed
2619
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
2620

unknown's avatar
unknown committed
2621
opt_select_from:
2622
	opt_limit_clause {}
unknown's avatar
unknown committed
2623
	| select_from select_lock_type;
unknown's avatar
unknown committed
2624 2625

udf_func_type:
2626
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
unknown's avatar
unknown committed
2627
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
unknown's avatar
unknown committed
2628 2629 2630 2631

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
unknown's avatar
unknown committed
2632
	| INT_SYM {$$ = (int) INT_RESULT; };
unknown's avatar
unknown committed
2633 2634 2635

field_list:
	  field_list_item
unknown's avatar
unknown committed
2636
	| field_list ',' field_list_item;
unknown's avatar
unknown committed
2637 2638 2639


field_list_item:
unknown's avatar
unknown committed
2640
	   column_def
2641 2642 2643 2644
         | key_def
         ;

column_def:
unknown's avatar
unknown committed
2645
	  field_spec opt_check_constraint
unknown's avatar
unknown committed
2646 2647 2648 2649
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
unknown's avatar
unknown committed
2650
	;
2651 2652

key_def:
unknown's avatar
unknown committed
2653
	key_type opt_ident key_alg '(' key_list ')'
unknown's avatar
unknown committed
2654
	  {
unknown's avatar
unknown committed
2655
	    LEX *lex=Lex;
2656
	    lex->key_list.push_back(new Key($1,$2, $3, 0, lex->col_list));
unknown's avatar
unknown committed
2657
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
2658
	  }
2659 2660 2661 2662
	| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
	  {
	    LEX *lex=Lex;
	    const char *key_name= $3 ? $3:$1;
2663 2664
	    lex->key_list.push_back(new Key($2, key_name, $4, 0,
				    lex->col_list));
2665 2666
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
2667
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
unknown's avatar
unknown committed
2668
	  {
2669
	    LEX *lex=Lex;
2670
	    lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
2671 2672 2673 2674 2675
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
2676 2677 2678
	    lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4 : $1,
					    HA_KEY_ALG_UNDEF, 1,
					    lex->col_list));
2679
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
2680
	  }
unknown's avatar
unknown committed
2681 2682 2683 2684
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
2685
	| opt_constraint check_constraint
unknown's avatar
unknown committed
2686 2687
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
2688 2689 2690
	  }
	;

unknown's avatar
unknown committed
2691
opt_check_constraint:
2692
	/* empty */
unknown's avatar
unknown committed
2693 2694 2695 2696 2697
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
2698
	;
unknown's avatar
unknown committed
2699 2700

opt_constraint:
2701
	/* empty */		{ $$=(char*) 0; }
unknown's avatar
unknown committed
2702 2703 2704 2705 2706 2707
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
unknown's avatar
unknown committed
2708 2709 2710 2711

field_spec:
	field_ident
	 {
unknown's avatar
unknown committed
2712 2713
	   LEX *lex=Lex;
	   lex->length=lex->dec=0; lex->type=0; lex->interval=0;
2714
	   lex->default_value= lex->on_update_value= 0;
unknown's avatar
unknown committed
2715
	   lex->comment=0;
2716
	   lex->charset=NULL;
unknown's avatar
unknown committed
2717 2718 2719
	 }
	type opt_attribute
	{
unknown's avatar
unknown committed
2720
	  LEX *lex=Lex;
unknown's avatar
unknown committed
2721
	  if (add_field_to_list(lex->thd, $1.str,
unknown's avatar
unknown committed
2722
				(enum enum_field_types) $3,
unknown's avatar
unknown committed
2723
				lex->length,lex->dec,lex->type,
2724 2725
				lex->default_value, lex->on_update_value, 
                                lex->comment,
unknown's avatar
unknown committed
2726 2727
				lex->change,lex->interval,lex->charset,
				lex->uint_geom_type))
unknown's avatar
unknown committed
2728
	    YYABORT;
unknown's avatar
unknown committed
2729
	};
unknown's avatar
unknown committed
2730 2731

type:
2732
	int_type opt_len field_options	{ $$=$1; }
unknown's avatar
unknown committed
2733 2734 2735 2736 2737 2738
	| 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; }
2739 2740
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
2741
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
unknown's avatar
unknown committed
2742 2743 2744
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
2745
	| nchar '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
2746
					  $$=FIELD_TYPE_STRING;
2747
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
2748
	| nchar				{ Lex->length=(char*) "1";
unknown's avatar
unknown committed
2749
					  $$=FIELD_TYPE_STRING;
2750
					  Lex->charset=national_charset_info; }
2751
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
2752
					  Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2753
					  $$=FIELD_TYPE_STRING; }
2754 2755 2756
	| BINARY			{ Lex->length= (char*) "1";
					  Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
2757 2758
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
					  $$=FIELD_TYPE_VAR_STRING; }
unknown's avatar
unknown committed
2759 2760
	| nvarchar '(' NUM ')'		{ Lex->length=$3.str;
					  $$=FIELD_TYPE_VAR_STRING;
2761
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
2762
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
unknown's avatar
unknown committed
2763
					  Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2764
					  $$=FIELD_TYPE_VAR_STRING; }
2765
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
unknown's avatar
unknown committed
2766 2767
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
2768 2769
	| TIMESTAMP
	  {
2770
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
2771 2772
	      $$=FIELD_TYPE_DATETIME;
	    else
2773 2774 2775 2776 2777
            {
              /* 
                Unlike other types TIMESTAMP fields are NOT NULL by default.
              */
              Lex->type|= NOT_NULL_FLAG;
2778
	      $$=FIELD_TYPE_TIMESTAMP;
2779
            }
2780
	   }
2781 2782 2783 2784 2785 2786 2787
	| TIMESTAMP '(' NUM ')'
          { 
            LEX *lex= Lex;
            lex->length= $3.str;
            lex->type|= NOT_NULL_FLAG;
            $$= FIELD_TYPE_TIMESTAMP;
          }
unknown's avatar
unknown committed
2788
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
unknown's avatar
unknown committed
2789
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2790
					  $$=FIELD_TYPE_TINY_BLOB; }
unknown's avatar
unknown committed
2791
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2792
					  $$=FIELD_TYPE_BLOB; }
unknown's avatar
unknown committed
2793 2794
	| spatial_type
          {
unknown's avatar
unknown committed
2795
#ifdef HAVE_SPATIAL
unknown's avatar
unknown committed
2796 2797 2798
            Lex->charset=&my_charset_bin;
            Lex->uint_geom_type= (uint)$1;
            $$=FIELD_TYPE_GEOMETRY;
unknown's avatar
unknown committed
2799
#else
2800 2801
            my_error(ER_FEATURE_DISABLED, MYF(0)
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
2802
            YYABORT;
unknown's avatar
unknown committed
2803
#endif
unknown's avatar
unknown committed
2804
          }
unknown's avatar
unknown committed
2805
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2806
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
2807
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2808
					  $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
2809
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2810
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
2811 2812
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
2813
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
2814 2815
	| MEDIUMTEXT opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| LONGTEXT opt_binary		{ $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
2816 2817 2818 2819
	| DECIMAL_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
	| NUMERIC_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
2820 2821
	| FIXED_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
2822
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
unknown's avatar
unknown committed
2823
	  {
unknown's avatar
unknown committed
2824 2825
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
unknown's avatar
unknown committed
2826 2827
	    $$=FIELD_TYPE_ENUM;
	  }
2828
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
unknown's avatar
unknown committed
2829
	  {
unknown's avatar
unknown committed
2830 2831
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
unknown's avatar
unknown committed
2832
	    $$=FIELD_TYPE_SET;
2833
	  }
2834
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
2835 2836 2837 2838 2839 2840
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
2841
	;
unknown's avatar
unknown committed
2842

unknown's avatar
unknown committed
2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
	| POINT_SYM           { $$= Field::GEOM_POINT; }
	| MULTIPOINT          { $$= Field::GEOM_MULTIPOINT; }
	| LINESTRING          { $$= Field::GEOM_LINESTRING; }
	| MULTILINESTRING     { $$= Field::GEOM_MULTILINESTRING; }
	| POLYGON             { $$= Field::GEOM_POLYGON; }
	| MULTIPOLYGON        { $$= Field::GEOM_MULTIPOLYGON; }
	;

unknown's avatar
unknown committed
2854 2855
char:
	CHAR_SYM {}
unknown's avatar
unknown committed
2856 2857 2858 2859 2860 2861
	;

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

unknown's avatar
unknown committed
2863 2864 2865
varchar:
	char VARYING {}
	| VARCHAR {}
unknown's avatar
unknown committed
2866 2867 2868 2869
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
unknown's avatar
unknown committed
2870
	| NVARCHAR_SYM {}
unknown's avatar
unknown committed
2871 2872 2873 2874
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
unknown's avatar
unknown committed
2875 2876 2877 2878 2879 2880

int_type:
	INT_SYM		{ $$=FIELD_TYPE_LONG; }
	| TINYINT	{ $$=FIELD_TYPE_TINY; }
	| SMALLINT	{ $$=FIELD_TYPE_SHORT; }
	| MEDIUMINT	{ $$=FIELD_TYPE_INT24; }
unknown's avatar
unknown committed
2881
	| BIGINT	{ $$=FIELD_TYPE_LONGLONG; };
unknown's avatar
unknown committed
2882 2883

real_type:
2884
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
unknown's avatar
unknown committed
2885 2886
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
unknown's avatar
unknown committed
2887
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
unknown's avatar
unknown committed
2888 2889 2890 2891 2892


float_options:
	/* empty */		{}
	| '(' NUM ')'		{ Lex->length=$2.str; }
unknown's avatar
unknown committed
2893
	| precision		{};
unknown's avatar
unknown committed
2894 2895 2896 2897 2898 2899

precision:
	'(' NUM ',' NUM ')'
	{
	  LEX *lex=Lex;
	  lex->length=$2.str; lex->dec=$4.str;
unknown's avatar
unknown committed
2900
	};
unknown's avatar
unknown committed
2901 2902 2903

field_options:
	/* empty */		{}
unknown's avatar
unknown committed
2904
	| field_opt_list	{};
unknown's avatar
unknown committed
2905 2906 2907

field_opt_list:
	field_opt_list field_option {}
unknown's avatar
unknown committed
2908
	| field_option {};
unknown's avatar
unknown committed
2909 2910

field_option:
unknown's avatar
unknown committed
2911
	SIGNED_SYM	{}
2912
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
unknown's avatar
unknown committed
2913
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
unknown's avatar
unknown committed
2914 2915

opt_len:
2916 2917
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
unknown's avatar
unknown committed
2918 2919 2920

opt_precision:
	/* empty */	{}
unknown's avatar
unknown committed
2921
	| precision	{};
unknown's avatar
unknown committed
2922 2923 2924

opt_attribute:
	/* empty */ {}
unknown's avatar
unknown committed
2925
	| opt_attribute_list {};
unknown's avatar
unknown committed
2926 2927 2928

opt_attribute_list:
	opt_attribute_list attribute {}
unknown's avatar
unknown committed
2929
	| attribute;
unknown's avatar
unknown committed
2930 2931 2932

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
2933
	| not NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
2934 2935 2936
	| DEFAULT now_or_signed_literal { Lex->default_value=$2; }
	| ON UPDATE_SYM NOW_SYM optional_braces 
          { Lex->on_update_value= new Item_func_now_local(); }
unknown's avatar
unknown committed
2937
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
2938
	| SERIAL_SYM DEFAULT VALUE_SYM
2939 2940 2941
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
2942
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2943 2944 2945 2946 2947
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
2948
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2949 2950 2951 2952 2953
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
2954
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2955 2956 2957 2958 2959
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
2960
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2961
	  }
unknown's avatar
unknown committed
2962
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= &$2; }
2963
	| BINARY { Lex->type|= BINCMP_FLAG; }
unknown's avatar
unknown committed
2964 2965
	| COLLATE_SYM collation_name
	  {
unknown's avatar
unknown committed
2966
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
unknown's avatar
unknown committed
2967
	    {
2968 2969
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $2->name,Lex->charset->csname);
unknown's avatar
unknown committed
2970 2971 2972 2973 2974 2975 2976 2977
	      YYABORT;
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
unknown's avatar
unknown committed
2978

2979 2980 2981 2982 2983
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

2984 2985 2986 2987
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
unknown's avatar
unknown committed
2988

2989
charset_name:
2990
	ident_or_text
2991
	{
unknown's avatar
unknown committed
2992
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
2993
	  {
2994
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
2995 2996
	    YYABORT;
	  }
unknown's avatar
unknown committed
2997 2998 2999
	}
	| BINARY { $$= &my_charset_bin; }
	;
3000

3001 3002 3003
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
unknown's avatar
unknown committed
3004

unknown's avatar
unknown committed
3005 3006 3007 3008 3009 3010 3011

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)))
	  {
3012
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
unknown's avatar
unknown committed
3013 3014 3015 3016 3017 3018 3019 3020 3021 3022
	    YYABORT;
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

3023
collation_name:
3024
	ident_or_text
3025 3026 3027
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
3028
	    my_error(ER_UNKNOWN_COLLATION, MYF(0), $1.str);
3029 3030 3031 3032
	    YYABORT;
	  }
	};

3033 3034
opt_collate:
	/* empty */	{ $$=NULL; }
unknown's avatar
unknown committed
3035
	| COLLATE_SYM collation_name_or_default { $$=$2; }
3036 3037
	;

3038 3039 3040 3041
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

3042 3043 3044 3045
opt_default:
	/* empty */	{}
	| DEFAULT	{};

unknown's avatar
unknown committed
3046
opt_binary:
3047
	/* empty */			{ Lex->charset=NULL; }
unknown's avatar
unknown committed
3048 3049
	| ASCII_SYM			{ Lex->charset=&my_charset_latin1; }
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
unknown's avatar
unknown committed
3050 3051
	| UNICODE_SYM
	{
unknown's avatar
unknown committed
3052 3053
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
unknown's avatar
unknown committed
3054
	  {
unknown's avatar
unknown committed
3055
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
unknown's avatar
unknown committed
3056 3057 3058
	    YYABORT;
	  }
	}
3059
	| charset charset_name	{ Lex->charset=$2; } ;
unknown's avatar
unknown committed
3060

unknown's avatar
unknown committed
3061 3062 3063
opt_primary:
	/* empty */
	| PRIMARY_SYM
3064
	;
unknown's avatar
unknown committed
3065

unknown's avatar
unknown committed
3066
references:
3067 3068
	REFERENCES table_ident
	{
3069
	  LEX *lex=Lex;
3070 3071 3072 3073 3074 3075
	  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
3076
	};
3077

3078
opt_ref_list:
unknown's avatar
unknown committed
3079
	/* empty */ opt_on_delete {}
unknown's avatar
unknown committed
3080
	| '(' ref_list ')' opt_on_delete {};
3081 3082 3083

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

unknown's avatar
unknown committed
3086 3087 3088

opt_on_delete:
	/* empty */ {}
unknown's avatar
unknown committed
3089
	| opt_on_delete_list {};
unknown's avatar
unknown committed
3090 3091 3092

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
unknown's avatar
unknown committed
3093
	| opt_on_delete_item {};
unknown's avatar
unknown committed
3094 3095

opt_on_delete_item:
3096 3097 3098 3099
	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
3100
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
unknown's avatar
unknown committed
3101 3102

delete_option:
3103 3104 3105 3106
	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
3107
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
unknown's avatar
unknown committed
3108 3109

key_type:
3110
	key_or_index			    { $$= Key::MULTIPLE; }
unknown's avatar
SCRUM  
unknown committed
3111 3112
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
unknown's avatar
unknown committed
3113 3114 3115 3116
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
3117 3118
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3119 3120 3121
	    YYABORT;
#endif
	  };
3122 3123 3124

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
unknown's avatar
SCRUM  
unknown committed
3125
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
unknown's avatar
unknown committed
3126 3127 3128

key_or_index:
	KEY_SYM {}
unknown's avatar
unknown committed
3129
	| INDEX_SYM {};
unknown's avatar
unknown committed
3130

unknown's avatar
SCRUM  
unknown committed
3131 3132 3133 3134 3135
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

unknown's avatar
unknown committed
3136 3137
keys_or_index:
	KEYS {}
unknown's avatar
unknown committed
3138
	| INDEX_SYM {}
unknown's avatar
unknown committed
3139
	| INDEXES {};
unknown's avatar
unknown committed
3140

3141
opt_unique_or_fulltext:
unknown's avatar
unknown committed
3142 3143
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
unknown's avatar
unknown committed
3144
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
unknown's avatar
unknown committed
3145 3146 3147 3148 3149
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
unknown's avatar
unknown committed
3150
	    my_message(ER_FEATURE_DISABLED, ER(ER_FEATURE_DISABLED), MYF(0),
unknown's avatar
unknown committed
3151
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3152 3153 3154
	    YYABORT;
#endif
	  }
unknown's avatar
unknown committed
3155
        ;
unknown's avatar
unknown committed
3156 3157

key_alg:
3158
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
3159
	| USING opt_btree_or_rtree { $$= $2; }
unknown's avatar
unknown committed
3160
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
unknown's avatar
unknown committed
3161 3162 3163

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
unknown's avatar
unknown committed
3164 3165 3166 3167
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
unknown's avatar
unknown committed
3168
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
unknown's avatar
unknown committed
3169 3170 3171

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
unknown's avatar
unknown committed
3172
	| key_part order_dir		{ Lex->col_list.push_back($1); };
unknown's avatar
unknown committed
3173 3174 3175

key_part:
	ident			{ $$=new key_part_spec($1.str); }
3176 3177 3178 3179 3180
	| ident '(' NUM ')'	
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
3181
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
3182 3183 3184
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
unknown's avatar
unknown committed
3185 3186 3187

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

3190 3191 3192
opt_component:
	/* empty */	 { $$.str= 0; $$.length= 0; }
	| '.' ident	 { $$=$2; };
3193

unknown's avatar
unknown committed
3194 3195
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
unknown's avatar
unknown committed
3196
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
unknown's avatar
unknown committed
3197 3198 3199 3200 3201 3202 3203 3204

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
3205
	  THD *thd= YYTHD;
3206
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
3207 3208
	  lex->sql_command = SQLCOM_ALTER_TABLE;
	  lex->name=0;
unknown's avatar
unknown committed
3209 3210
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
unknown's avatar
unknown committed
3211 3212 3213 3214
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
3215
          lex->select_lex.init_order();
3216
	  lex->select_lex.db=lex->name=0;
3217
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
unknown's avatar
unknown committed
3218
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
3219
	  lex->create_info.default_table_charset= NULL;
3220
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
3221
	  lex->alter_info.reset();
3222
	  lex->alter_info.flags= 0;
unknown's avatar
unknown committed
3223
	}
3224 3225
	alter_list
	{}
3226 3227 3228 3229 3230 3231
	| ALTER DATABASE ident
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
          opt_create_database_options
3232 3233 3234 3235
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
	    lex->name=$3.str;
3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

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

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

3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    THD *thd= YYTHD;
	    LEX *lex=Lex;

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

unknown's avatar
unknown committed
3279
alter_list:
3280 3281
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
unknown's avatar
unknown committed
3282
        | alter_list_item
unknown's avatar
unknown committed
3283
	| alter_list ',' alter_list_item;
unknown's avatar
unknown committed
3284 3285

add_column:
3286
	ADD opt_column
3287 3288
	{
	  LEX *lex=Lex;
3289 3290
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
3291
	};
unknown's avatar
unknown committed
3292 3293

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

opt_column:
	/* empty */	{}
unknown's avatar
unknown committed
3429
	| COLUMN_SYM	{};
unknown's avatar
unknown committed
3430 3431 3432

opt_ignore:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
unknown's avatar
unknown committed
3433
	| IGNORE_SYM	{ Lex->duplicates=DUP_IGNORE; };
unknown's avatar
unknown committed
3434 3435

opt_restrict:
3436 3437 3438 3439
	/* empty */	{ Lex->drop_mode= DROP_DEFAULT; }
	| RESTRICT	{ Lex->drop_mode= DROP_RESTRICT; }
	| CASCADE	{ Lex->drop_mode= DROP_CASCADE; }
	;
unknown's avatar
unknown committed
3440 3441 3442 3443

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
unknown's avatar
unknown committed
3444
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
unknown's avatar
unknown committed
3445 3446 3447 3448

opt_to:
	/* empty */	{}
	| TO_SYM	{}
3449
	| EQ		{}
unknown's avatar
unknown committed
3450
	| AS		{};
unknown's avatar
unknown committed
3451

unknown's avatar
unknown committed
3452
/*
3453
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
unknown's avatar
unknown committed
3454 3455
*/

unknown's avatar
unknown committed
3456
slave:
3457
	  START_SYM SLAVE slave_thread_opts
3458 3459 3460 3461 3462 3463
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_START;
	    lex->type = 0;
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
unknown's avatar
unknown committed
3464
            /* If you change this code don't forget to update SLAVE START too */
3465 3466 3467
          }
          slave_until
          {}
unknown's avatar
unknown committed
3468 3469 3470 3471 3472
        | STOP_SYM SLAVE slave_thread_opts
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_STOP;
	    lex->type = 0;
unknown's avatar
unknown committed
3473
            /* If you change this code don't forget to update SLAVE STOP too */
unknown's avatar
unknown committed
3474
          }
3475 3476 3477 3478 3479
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
3480 3481 3482 3483 3484
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
3485 3486 3487 3488 3489 3490 3491 3492
	| SLAVE STOP_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_STOP;
	   lex->type = 0;
         }
        ;

unknown's avatar
unknown committed
3493

unknown's avatar
unknown committed
3494
start:
3495 3496 3497 3498 3499
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
           Lex->sql_command = SQLCOM_BEGIN;
           Lex->start_transaction_opt= $3;
        }
unknown's avatar
unknown committed
3500 3501
	;

3502 3503 3504 3505 3506 3507
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
3508
        ;
3509

unknown's avatar
unknown committed
3510
slave_thread_opts:
3511 3512
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
3513
        {}
unknown's avatar
unknown committed
3514
	;
3515 3516

slave_thread_opt_list:
unknown's avatar
unknown committed
3517
	slave_thread_opt
3518 3519
	| slave_thread_opt_list ',' slave_thread_opt
	;
3520 3521

slave_thread_opt:
3522
	/*empty*/	{}
unknown's avatar
unknown committed
3523
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
unknown's avatar
unknown committed
3524
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
unknown's avatar
unknown committed
3525
	;
3526

3527 3528 3529 3530 3531 3532 3533 3534 3535 3536
slave_until:
	/*empty*/	{}
	| UNTIL_SYM slave_until_opts
          {
            LEX *lex=Lex;
            if ((lex->mi.log_file_name || lex->mi.pos) &&
                (lex->mi.relay_log_name || lex->mi.relay_log_pos) ||
                !((lex->mi.log_file_name && lex->mi.pos) ||
                  (lex->mi.relay_log_name && lex->mi.relay_log_pos)))
            {
unknown's avatar
unknown committed
3537 3538
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


unknown's avatar
unknown committed
3550 3551 3552 3553 3554
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
	}
unknown's avatar
unknown committed
3555
	table_list FROM TEXT_STRING_sys
unknown's avatar
unknown committed
3556 3557
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
3558
        };
unknown's avatar
unknown committed
3559

unknown's avatar
unknown committed
3560 3561 3562 3563 3564
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
	}
unknown's avatar
unknown committed
3565
	table_list TO_SYM TEXT_STRING_sys
unknown's avatar
unknown committed
3566 3567
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
3568
        };
unknown's avatar
unknown committed
3569

3570 3571 3572 3573 3574 3575
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
3576 3577
	table_list opt_checksum_type
        {}
3578 3579
	;

3580 3581 3582 3583 3584 3585
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
3586
repair:
3587
	REPAIR opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3588
	{
unknown's avatar
unknown committed
3589 3590
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
3591
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3592
	   lex->check_opt.init();
unknown's avatar
unknown committed
3593
	}
3594 3595 3596
	table_list opt_mi_repair_type
	{}
	;
unknown's avatar
unknown committed
3597

unknown's avatar
unknown committed
3598
opt_mi_repair_type:
unknown's avatar
unknown committed
3599
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
3600
	| mi_repair_types {};
unknown's avatar
unknown committed
3601

unknown's avatar
unknown committed
3602 3603
mi_repair_types:
	mi_repair_type {}
unknown's avatar
unknown committed
3604
	| mi_repair_type mi_repair_types {};
3605

unknown's avatar
unknown committed
3606
mi_repair_type:
unknown's avatar
unknown committed
3607
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
3608
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
unknown's avatar
unknown committed
3609
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
unknown's avatar
unknown committed
3610 3611

analyze:
3612
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3613
	{
unknown's avatar
unknown committed
3614 3615
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
3616
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3617
	   lex->check_opt.init();
unknown's avatar
unknown committed
3618
	}
3619 3620 3621
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3622 3623

check:
unknown's avatar
unknown committed
3624
	CHECK_SYM table_or_tables
unknown's avatar
unknown committed
3625
	{
unknown's avatar
unknown committed
3626 3627 3628
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECK;
	   lex->check_opt.init();
unknown's avatar
unknown committed
3629
	}
3630 3631 3632
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3633

unknown's avatar
unknown committed
3634 3635
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
3636
	| mi_check_types {};
unknown's avatar
unknown committed
3637 3638 3639

mi_check_types:
	mi_check_type {}
unknown's avatar
unknown committed
3640
	| mi_check_type mi_check_types {};
unknown's avatar
unknown committed
3641 3642 3643 3644 3645 3646

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

unknown's avatar
unknown committed
3649
optimize:
3650
	OPTIMIZE opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3651
	{
unknown's avatar
unknown committed
3652 3653
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
unknown's avatar
unknown committed
3654
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3655
	   lex->check_opt.init();
unknown's avatar
unknown committed
3656
	}
3657 3658 3659
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3660

3661 3662 3663
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
3664
	| LOCAL_SYM  { $$= 1; }
3665 3666
	;

3667 3668 3669 3670 3671
rename:
	RENAME table_or_tables
	{
	   Lex->sql_command=SQLCOM_RENAME_TABLE;
	}
3672 3673 3674
	table_to_table_list
	{}
	;
3675 3676 3677

table_to_table_list:
	table_to_table
unknown's avatar
unknown committed
3678
	| table_to_table_list ',' table_to_table;
3679 3680 3681

table_to_table:
	table_ident TO_SYM table_ident
3682
	{
unknown's avatar
unknown committed
3683
	  LEX *lex=Lex;
unknown's avatar
unknown committed
3684
	  SELECT_LEX *sl= lex->current_select;
unknown's avatar
unknown committed
3685 3686 3687 3688
	  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))
3689
	    YYABORT;
3690
	};
3691

unknown's avatar
unknown committed
3692
keycache:
unknown's avatar
unknown committed
3693
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
unknown's avatar
unknown committed
3694 3695
        {
          LEX *lex=Lex;
3696 3697
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
	  lex->name_and_length= $5;
unknown's avatar
unknown committed
3698 3699 3700 3701 3702 3703 3704 3705
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
3706
        table_ident cache_keys_spec
unknown's avatar
unknown committed
3707 3708 3709 3710 3711 3712
        {
          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(),
3713
                                      (List<String> *)0))
unknown's avatar
unknown committed
3714 3715 3716 3717
            YYABORT;
        }
        ;

3718 3719 3720
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
3721
	;
3722

unknown's avatar
unknown committed
3723
preload:
unknown's avatar
unknown committed
3724
	LOAD INDEX_SYM INTO CACHE_SYM
unknown's avatar
unknown committed
3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737
	{
	  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
3738
	table_ident cache_keys_spec opt_ignore_leaves
unknown's avatar
unknown committed
3739 3740 3741
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
unknown's avatar
unknown committed
3742 3743
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
unknown's avatar
unknown committed
3744 3745 3746 3747 3748 3749
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

3750
cache_keys_spec:
3751
        { Select->interval_list.empty(); }
3752 3753 3754 3755 3756 3757 3758
        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
3759

unknown's avatar
unknown committed
3760
cache_key_list_or_empty:
3761
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
3762
	| opt_key_or_index '(' key_usage_list2 ')'
3763 3764 3765 3766
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
3767 3768
	;

unknown's avatar
unknown committed
3769
opt_ignore_leaves:
unknown's avatar
unknown committed
3770 3771 3772 3773 3774
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

unknown's avatar
unknown committed
3775
/*
unknown's avatar
unknown committed
3776
  Select : retrieve data from table
unknown's avatar
unknown committed
3777 3778 3779 3780
*/


select:
3781 3782 3783 3784 3785
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
	  lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE;
unknown's avatar
unknown committed
3786 3787
	}
	;
unknown's avatar
unknown committed
3788

3789
/* Need select_init2 for subselects. */
unknown's avatar
unknown committed
3790
select_init:
3791
	SELECT_SYM select_init2
3792
	|
3793 3794
	'(' SELECT_SYM select_part2 ')'
	  {
3795
	    LEX *lex= Lex;
unknown's avatar
unknown committed
3796
            SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
3797
	    if (sel->set_braces(1))
3798
	    {
3799
	      yyerror(ER(ER_SYNTAX_ERROR));
3800 3801
	      YYABORT;
	    }
3802 3803
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
3804
	  {
3805
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
3806 3807
	    YYABORT;
	  }
3808
            /* select in braces, can't contain global parameters */
unknown's avatar
unknown committed
3809 3810 3811
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
unknown's avatar
unknown committed
3812
          } union_opt;
3813

3814 3815
select_init2:
	select_part2
3816
        {
3817
	  LEX *lex= Lex;
unknown's avatar
unknown committed
3818
          SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
3819
          if (lex->current_select->set_braces(0))
3820
	  {
3821
	    yyerror(ER(ER_SYNTAX_ERROR));
3822 3823
	    YYABORT;
	  }
3824 3825
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
3826
	  {
3827
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
3828 3829
	    YYABORT;
	  }
3830
	}
3831
	union_clause
3832 3833
	;

3834
select_part2:
unknown's avatar
unknown committed
3835
	{
unknown's avatar
unknown committed
3836 3837 3838
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
	  lex->lock_option= TL_READ;
unknown's avatar
unknown committed
3839 3840
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
3841
	  lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
3842 3843 3844
	}
	select_options select_item_list
	{
3845
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
3846
	}
unknown's avatar
unknown committed
3847
	select_into select_lock_type;
3848

unknown's avatar
unknown committed
3849
select_into:
3850 3851
	opt_limit_clause {}
        | into
unknown's avatar
unknown committed
3852
	| select_from
3853 3854
	| into select_from
	| select_from into;
unknown's avatar
unknown committed
3855 3856

select_from:
3857 3858 3859 3860 3861 3862 3863
	  FROM join_table_list where_clause group_clause having_clause
	       opt_order_clause opt_limit_clause procedure_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 :) */
	;
unknown's avatar
unknown committed
3864 3865 3866

select_options:
	/* empty*/
unknown's avatar
unknown committed
3867
	| select_option_list;
unknown's avatar
unknown committed
3868 3869 3870

select_option_list:
	select_option_list select_option
unknown's avatar
unknown committed
3871
	| select_option;
unknown's avatar
unknown committed
3872 3873

select_option:
3874
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
3875 3876 3877 3878 3879 3880
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
3881 3882 3883
	| DISTINCT	{ Select->options|= SELECT_DISTINCT; }
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895
	| 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;
	  }
3896
	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
3897 3898 3899 3900
	| SQL_CACHE_SYM
	  {
	    Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
	  }
3901 3902
	| ALL		{}
	;
unknown's avatar
unknown committed
3903

unknown's avatar
unknown committed
3904 3905 3906
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
3907 3908
	  {
	    LEX *lex=Lex;
3909
	    lex->current_select->set_lock_for_tables(TL_WRITE);
3910
	    lex->safe_to_cache_query=0;
3911
	  }
unknown's avatar
unknown committed
3912
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
3913 3914
	  {
	    LEX *lex=Lex;
3915 3916
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
3917
	    lex->safe_to_cache_query=0;
3918 3919
	  }
	;
unknown's avatar
unknown committed
3920

unknown's avatar
unknown committed
3921 3922 3923 3924 3925
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
3926 3927
	    THD *thd= YYTHD;
	    if (add_item_to_list(thd, new Item_field(NULL, NULL, "*")))
unknown's avatar
unknown committed
3928
	      YYABORT;
3929
	    (thd->lex->current_select->with_wild)++;
unknown's avatar
unknown committed
3930
	  };
unknown's avatar
unknown committed
3931 3932 3933 3934 3935


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
unknown's avatar
unknown committed
3936
	    if (add_item_to_list(YYTHD, $2))
unknown's avatar
unknown committed
3937 3938
	      YYABORT;
	    if ($4.str)
unknown's avatar
unknown committed
3939
	      $2->set_name($4.str,$4.length,system_charset_info);
3940 3941 3942 3943 3944 3945
	    else if (!$2->name) {
	      char *str = $1;
	      if (str[-1] == '`')
	        str--;
	      $2->set_name(str,(uint) ($3 - str), YYTHD->charset());
	    }
unknown's avatar
unknown committed
3946
	  };
unknown's avatar
unknown committed
3947 3948

remember_name:
unknown's avatar
unknown committed
3949
	{ $$=(char*) Lex->tok_start; };
unknown's avatar
unknown committed
3950 3951

remember_end:
unknown's avatar
unknown committed
3952
	{ $$=(char*) Lex->tok_end; };
unknown's avatar
unknown committed
3953 3954 3955

select_item2:
	table_wild	{ $$=$1; } /* table.* */
unknown's avatar
unknown committed
3956
	| expr		{ $$=$1; };
unknown's avatar
unknown committed
3957 3958

select_alias:
unknown's avatar
unknown committed
3959 3960 3961 3962 3963 3964
	/* empty */		{ $$.str=0;}
	| AS ident		{ $$=$2; }
	| AS TEXT_STRING_sys	{ $$=$2; }
	| ident			{ $$=$1; }
	| TEXT_STRING_sys	{ $$=$1; }
	;
unknown's avatar
unknown committed
3965 3966 3967

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

unknown's avatar
unknown committed
3970
/* all possible expressions */
unknown's avatar
SCRUM  
unknown committed
3971
expr:	
3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998
	expr or bool_term	{ $$= new Item_cond_or($1,$3); }
	| expr XOR bool_term	{ $$= new Item_cond_xor($1,$3); }
	| bool_term ;

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

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

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

bool_pri:
	bool_pri IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| bool_pri IS not NULL_SYM { $$= new Item_func_isnotnull($1); }
	| predicate BETWEEN_SYM bit_expr AND_SYM bool_pri
	  { $$= new Item_func_between($1,$3,$5); }
	| predicate not BETWEEN_SYM bit_expr AND_SYM bool_pri
3999
	  { $$= negate_expression(YYTHD, new Item_func_between($1,$4,$6)); }
4000 4001 4002 4003 4004 4005
	| predicate ;

predicate:
	 bit_expr IN_SYM '(' expr_list ')'
	  { $4->push_front($1); $$= new Item_func_in(*$4); }
	| bit_expr not IN_SYM '(' expr_list ')'
4006
	  { $5->push_front($1); $$= negate_expression(YYTHD, new Item_func_in(*$5)); }
4007 4008 4009
        | bit_expr IN_SYM in_subselect
	  { $$= new Item_in_subselect($1, $3); }
	| bit_expr not IN_SYM in_subselect
4010
          { $$= negate_expression(YYTHD, new Item_in_subselect($1, $4)); }
4011 4012 4013 4014 4015 4016 4017 4018 4019
	| bit_expr SOUNDS_SYM LIKE bit_expr
	  { $$= new Item_func_eq(new Item_func_soundex($1),
				 new Item_func_soundex($4)); }
	| bit_expr LIKE simple_expr opt_escape
          { $$= new Item_func_like($1,$3,$4); }
	| bit_expr not LIKE simple_expr opt_escape
          { $$= new Item_func_not(new Item_func_like($1,$4,$5)); }
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
4020
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067
	| bit_expr EQUAL_SYM bit_expr	{ $$= new Item_func_equal($1,$3); }
	| bit_expr comp_op bit_expr %prec EQ
	  { $$= (*$2)(0)->create($1,$3); }
	| bit_expr comp_op all_or_any in_subselect %prec EQ
	  { $$= all_any_subquery_creator($1, $2, $3, $4); }
	| bit_expr ;

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

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

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

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

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

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

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

unknown's avatar
unknown committed
4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080
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; }
        ;

4081
interval_expr:
unknown's avatar
unknown committed
4082
         INTERVAL_SYM expr { $$=$2; }
4083 4084
        ;

unknown's avatar
unknown committed
4085 4086
simple_expr:
	simple_ident
4087
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
unknown's avatar
unknown committed
4088
	  {
unknown's avatar
unknown committed
4089 4090 4091
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
4092
                                                            YYTHD->charset()));
4093
	  }
unknown's avatar
unknown committed
4094
	| literal
unknown's avatar
unknown committed
4095
	| param_marker
unknown's avatar
unknown committed
4096
	| '@' ident_or_text SET_VAR expr
unknown's avatar
unknown committed
4097 4098
	  {
	    $$= new Item_func_set_user_var($2,$4);
4099 4100 4101
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4102
	  }
4103
	| '@' ident_or_text
unknown's avatar
unknown committed
4104 4105
	  {
	    $$= new Item_func_get_user_var($2);
4106 4107 4108
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4109
	  }
4110
	| '@' '@' opt_var_ident_type ident_or_text opt_component
unknown's avatar
unknown committed
4111
	  {
4112 4113 4114 4115 4116 4117

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

	    name->init_qname(YYTHD);
	    sp_add_fun_to_lex(Lex, name);
	    if ($5)
	      $$= new Item_func_sp(name, *$5);
unknown's avatar
unknown committed
4468
	    else
4469
	      $$= new Item_func_sp(name);
unknown's avatar
unknown committed
4470
	  }
4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542
	| IDENT_sys '(' udf_expr_list ')'
          {
#ifdef HAVE_DLOPEN
            udf_func *udf;

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

              sp_add_fun_to_lex(Lex, name);
              if ($3)
                $$= new Item_func_sp(name, *$3);
              else
                $$= new Item_func_sp(name);
	    }
          }
unknown's avatar
unknown committed
4543
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
4544
	  {
unknown's avatar
unknown committed
4545 4546
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
unknown's avatar
unknown committed
4547
	| UNIX_TIMESTAMP '(' ')'
unknown's avatar
unknown committed
4548 4549
	  {
	    $$= new Item_func_unix_timestamp();
4550
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4551
	  }
unknown's avatar
unknown committed
4552 4553 4554
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
4555
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
4556 4557 4558 4559 4560 4561
	| 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
4562
	| WEEK_SYM '(' expr ')'
unknown's avatar
unknown committed
4563
	  {
4564
            $$= new Item_func_week($3,new Item_int((char*) "0",
unknown's avatar
unknown committed
4565
				   YYTHD->variables.default_week_format,1));
4566
          }
unknown's avatar
unknown committed
4567 4568 4569 4570 4571 4572 4573 4574 4575
	| 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 ')'
4576
	  {
unknown's avatar
unknown committed
4577
	    $$=new Item_func_benchmark($3,$5);
4578
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
4579
	  }
unknown's avatar
unknown committed
4580
	| EXTRACT_SYM '(' interval FROM expr ')'
unknown's avatar
unknown committed
4581
	{ $$=new Item_extract( $3, $5); };
unknown's avatar
unknown committed
4582

unknown's avatar
unknown committed
4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593
geometry_function:
	GEOMFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| GEOMFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| GEOMFROMWKB '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_wkb($3)); }
	| GEOMFROMWKB '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_wkb($3, $5)); }
	| GEOMETRYCOLLECTION '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4594 4595
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
unknown's avatar
unknown committed
4596 4597
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4598
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
unknown's avatar
unknown committed
4599 4600
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
4601
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615
 	| MLINEFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| MLINEFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| MPOINTFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| MPOINTFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| MPOLYFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| MPOLYFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| MULTIPOINT '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4616
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
unknown's avatar
unknown committed
4617 4618
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4619
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
unknown's avatar
unknown committed
4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631
	| POINT_SYM '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_point($3,$5)); }
 	| POINTFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| POINTFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| POLYFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| POLYFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| POLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4632
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
4633 4634 4635 4636 4637 4638 4639 4640 4641 4642
 	| GEOMCOLLFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| GEOMCOLLFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
 	| LINEFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| LINEFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	;

4643 4644 4645 4646 4647 4648
fulltext_options:
        /* nothing */                   { $$= FT_NL;  }
        | WITH QUERY_SYM EXPANSION_SYM  { $$= FT_NL | FT_EXPAND; }
        | IN_SYM BOOLEAN_SYM MODE_SYM   { $$= FT_BOOL; }
        ;

unknown's avatar
unknown committed
4649
udf_expr_list:
4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680
	/* 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
4681 4682 4683 4684 4685 4686 4687 4688

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); }
4689 4690
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
unknown's avatar
unknown committed
4691
	| COUNT_SYM '(' opt_all '*' ')'
unknown's avatar
unknown committed
4692 4693 4694
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
4695
	| COUNT_SYM '(' DISTINCT
unknown's avatar
unknown committed
4696
	  { Select->in_sum_expr++; }
4697
	   expr_list
unknown's avatar
unknown committed
4698
	  { Select->in_sum_expr--; }
4699 4700
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
unknown's avatar
unknown committed
4701 4702 4703 4704
	| 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); }
4705 4706 4707 4708 4709 4710 4711
/*
   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
4712 4713
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
4714 4715
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
unknown's avatar
unknown committed
4716 4717
	| STD_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_std($3); }
unknown's avatar
unknown committed
4718 4719
	| VARIANCE_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_variance($3); }
unknown's avatar
unknown committed
4720
	| SUM_SYM '(' in_sum_expr ')'
4721
	  { $$=new Item_sum_sum($3); }
4722 4723
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
unknown's avatar
unknown committed
4724 4725 4726 4727 4728
	| GROUP_CONCAT_SYM '(' opt_distinct
	  { Select->in_sum_expr++; }
	  expr_list opt_gorder_clause
	  opt_gconcat_separator
	 ')'
unknown's avatar
unknown committed
4729
	  {
unknown's avatar
unknown committed
4730 4731 4732
	    Select->in_sum_expr--;
	    $$=new Item_func_group_concat($3,$5,Select->gorder_list,$7);
	    $5->empty();
4733 4734 4735 4736 4737 4738 4739
	  };

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

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

4743 4744

opt_gorder_clause:
4745 4746
	  /* empty */
	  {
unknown's avatar
unknown committed
4747
            Select->gorder_list = NULL;
4748 4749 4750
	  }
	| order_clause
          {
unknown's avatar
unknown committed
4751 4752 4753
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
4754
				     sizeof(st_sql_list));
unknown's avatar
unknown committed
4755
	    select->order_list.empty();
4756
	  };
unknown's avatar
unknown committed
4757

unknown's avatar
unknown committed
4758 4759

in_sum_expr:
unknown's avatar
unknown committed
4760
	opt_all
4761 4762 4763 4764
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
4765
	    yyerror(ER(ER_SYNTAX_ERROR));
4766
	    YYABORT;
4767 4768
	  }
	}
unknown's avatar
unknown committed
4769 4770
	expr
	{
unknown's avatar
unknown committed
4771
	  Select->in_sum_expr--;
4772
	  $$= $3;
unknown's avatar
unknown committed
4773
	};
unknown's avatar
unknown committed
4774

4775
cast_type:
4776
	BINARY opt_len		{ $$=ITEM_CAST_CHAR; Lex->charset= &my_charset_bin; }
4777 4778
	| CHAR_SYM opt_len opt_binary	{ $$=ITEM_CAST_CHAR; }
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; }
unknown's avatar
unknown committed
4779 4780 4781 4782 4783 4784 4785
	| SIGNED_SYM		{ $$=ITEM_CAST_SIGNED_INT; Lex->charset= NULL; Lex->length= (char*)0; }
	| SIGNED_SYM INT_SYM	{ $$=ITEM_CAST_SIGNED_INT; Lex->charset= NULL; Lex->length= (char*)0; }
	| UNSIGNED		{ $$=ITEM_CAST_UNSIGNED_INT; Lex->charset= NULL; Lex->length= (char*)0; }
	| UNSIGNED INT_SYM	{ $$=ITEM_CAST_UNSIGNED_INT; Lex->charset= NULL; Lex->length= (char*)0; }
	| DATE_SYM		{ $$=ITEM_CAST_DATE; Lex->charset= NULL; Lex->length= (char*)0; }
	| TIME_SYM		{ $$=ITEM_CAST_TIME; Lex->charset= NULL; Lex->length= (char*)0; }
	| DATETIME		{ $$=ITEM_CAST_DATETIME; Lex->charset= NULL; Lex->length= (char*)0; }
4786 4787
	;

unknown's avatar
unknown committed
4788
expr_list:
4789
	{ Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
4790
	expr_list2
unknown's avatar
unknown committed
4791
	{ $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
4792 4793

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

unknown's avatar
unknown committed
4797 4798
ident_list_arg:
          ident_list          { $$= $1; }
unknown's avatar
unknown committed
4799
        | '(' ident_list ')'  { $$= $2; };
unknown's avatar
unknown committed
4800

unknown's avatar
unknown committed
4801
ident_list:
4802
        { Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
4803
        ident_list2
unknown's avatar
unknown committed
4804
        { $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
4805 4806

ident_list2:
4807
        simple_ident { Select->expr_list.head()->push_back($1); }
unknown's avatar
unknown committed
4808
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
unknown's avatar
unknown committed
4809 4810 4811

opt_expr:
	/* empty */      { $$= NULL; }
4812
	| expr           { $$= $1; };
unknown's avatar
unknown committed
4813 4814 4815

opt_else:
	/* empty */    { $$= NULL; }
unknown's avatar
unknown committed
4816
	| ELSE expr    { $$= $2; };
unknown's avatar
unknown committed
4817 4818

when_list:
unknown's avatar
unknown committed
4819
        { Select->when_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
4820
	when_list2
unknown's avatar
unknown committed
4821
	{ $$= Select->when_list.pop(); };
unknown's avatar
unknown committed
4822 4823

when_list2:
unknown's avatar
unknown committed
4824
	expr THEN_SYM expr
unknown's avatar
unknown committed
4825
	  {
unknown's avatar
unknown committed
4826
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
4827 4828
	    sel->when_list.head()->push_back($1);
	    sel->when_list.head()->push_back($3);
unknown's avatar
unknown committed
4829
	}
unknown's avatar
unknown committed
4830
	| when_list2 WHEN_SYM expr THEN_SYM expr
unknown's avatar
unknown committed
4831
	  {
unknown's avatar
unknown committed
4832
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
4833 4834
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
unknown's avatar
unknown committed
4835
	  };
unknown's avatar
unknown committed
4836

4837 4838 4839
table_ref:
        table_factor            { $$=$1; }
        | join_table            { $$=$1; }
4840
          {
4841 4842 4843
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
4844
          }
4845 4846
        ;

unknown's avatar
unknown committed
4847
join_table_list:
4848 4849 4850 4851 4852 4853 4854 4855 4856
        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
4857
	  { add_join_on($3,$5); $$=$3; }
4858
	| table_ref normal_join table_ref
unknown's avatar
unknown committed
4859
	  USING
unknown's avatar
unknown committed
4860
	  {
unknown's avatar
unknown committed
4861
	    SELECT_LEX *sel= Select;
4862
            sel->save_names_for_using_list($1, $3);
unknown's avatar
unknown committed
4863
	  }
unknown's avatar
unknown committed
4864 4865 4866
	  '(' using_list ')'
	  { add_join_on($3,$7); $$=$3; }

4867
	| table_ref LEFT opt_outer JOIN_SYM table_ref ON expr
4868
	  { add_join_on($5,$7); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
4869
	| table_ref LEFT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
4870
	  {
unknown's avatar
unknown committed
4871
	    SELECT_LEX *sel= Select;
4872
            sel->save_names_for_using_list($1, $5);
unknown's avatar
unknown committed
4873
	  }
unknown's avatar
unknown committed
4874
	  USING '(' using_list ')'
4875
	  { add_join_on($5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
4876
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
4877
	  {
4878 4879
	    add_join_natural($1,$6);
	    $6->outer_join|=JOIN_TYPE_LEFT;
4880 4881
	    $$=$6;
	  }
4882 4883 4884 4885 4886 4887 4888 4889
	| table_ref RIGHT opt_outer JOIN_SYM table_ref ON expr
          { 
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
            add_join_on($$, $7);
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
4890
	  {
unknown's avatar
unknown committed
4891
	    SELECT_LEX *sel= Select;
4892
            sel->save_names_for_using_list($1, $5);
unknown's avatar
unknown committed
4893
	  }
4894
	  USING '(' using_list ')'
4895 4896 4897 4898 4899 4900 4901
          { 
	    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
4902
	  {
4903 4904 4905 4906
	    add_join_natural($6,$1);
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
4907
	  }
4908 4909 4910
	| table_ref NATURAL JOIN_SYM table_factor
	  { add_join_natural($1,$4); $$=$4; };
        
unknown's avatar
unknown committed
4911 4912

normal_join:
unknown's avatar
unknown committed
4913 4914 4915 4916
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
unknown's avatar
unknown committed
4917

4918
table_factor:
unknown's avatar
unknown committed
4919
	{
unknown's avatar
unknown committed
4920
	  SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
4921
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
unknown's avatar
unknown committed
4922
	  sel->table_join_options= 0;
unknown's avatar
unknown committed
4923
	}
unknown's avatar
unknown committed
4924
        table_ident opt_table_alias opt_key_definition
unknown's avatar
unknown committed
4925
	{
unknown's avatar
unknown committed
4926
	  LEX *lex= Lex;
unknown's avatar
unknown committed
4927
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
4928
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
unknown's avatar
unknown committed
4929
					   sel->get_table_join_options(),
unknown's avatar
unknown committed
4930
					   lex->lock_option,
4931 4932
					   sel->get_use_index(),
					   sel->get_ignore_index())))
4933
	    YYABORT;
4934
          sel->add_joined_table($$);
unknown's avatar
unknown committed
4935
	}
4936
        | '('
4937
          {
4938 4939 4940
            LEX *lex= Lex;
            if (lex->current_select->init_nested_join(lex->thd))
              YYABORT;
4941
          }
4942 4943 4944 4945 4946
          join_table_list ')'
          {
            LEX *lex= Lex;
            if (!($$= lex->current_select->end_nested_join(lex->thd)))
              YYABORT;
4947
          }
4948
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref ON expr '}'
4949
	  { add_join_on($7,$9); $7->outer_join|=JOIN_TYPE_LEFT; $$=$7; }
4950
        | '(' SELECT_SYM select_derived ')' opt_table_alias
unknown's avatar
unknown committed
4951 4952
	{
	  LEX *lex=Lex;
4953 4954 4955
	  SELECT_LEX_UNIT *unit= lex->current_select->master_unit();
	  lex->current_select= unit->outer_select();
	  if (!($$= lex->current_select->
unknown's avatar
unknown committed
4956
                add_table_to_list(lex->thd, new Table_ident(unit), $5, 0,
4957
				  TL_READ,(List<String> *)0,
unknown's avatar
unknown committed
4958 4959
	                          (List<String> *)0)))

unknown's avatar
unknown committed
4960
	    YYABORT;
4961
          lex->current_select->add_joined_table($$);           
unknown's avatar
unknown committed
4962
	};
unknown's avatar
unknown committed
4963

4964
select_derived:
unknown's avatar
unknown committed
4965
        {
unknown's avatar
unknown committed
4966
	  LEX *lex= Lex;
4967
	  lex->derived_tables|= DERIVED_SUBQUERY;
unknown's avatar
unknown committed
4968 4969 4970
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
4971 4972
	  {
	    yyerror(ER(ER_SYNTAX_ERROR));
4973 4974
	    YYABORT;
	  }
4975
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
unknown's avatar
unknown committed
4976
              mysql_new_select(lex, 1))
unknown's avatar
unknown committed
4977 4978
	    YYABORT;
	  mysql_init_select(lex);
4979
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
4980
	  lex->current_select->parsing_place= SELECT_LIST;
4981 4982 4983
	}
        select_options select_item_list
	{
4984
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
4985
	}
4986
	opt_select_from union_opt
4987
        ;
unknown's avatar
unknown committed
4988 4989 4990

opt_outer:
	/* empty */	{}
unknown's avatar
unknown committed
4991
	| OUTER		{};
unknown's avatar
unknown committed
4992 4993 4994 4995

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
unknown's avatar
unknown committed
4996
          {
unknown's avatar
unknown committed
4997
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
4998 4999 5000
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
5001 5002
	| FORCE_SYM key_usage_list
          {
unknown's avatar
unknown committed
5003
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5004 5005 5006 5007
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
unknown's avatar
unknown committed
5008
	| IGNORE_SYM key_usage_list
unknown's avatar
unknown committed
5009
	  {
unknown's avatar
unknown committed
5010
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5011 5012
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
unknown's avatar
unknown committed
5013
	  };
unknown's avatar
unknown committed
5014 5015

key_usage_list:
unknown's avatar
unknown committed
5016
	key_or_index { Select->interval_list.empty(); }
unknown's avatar
unknown committed
5017
        '(' key_list_or_empty ')'
unknown's avatar
unknown committed
5018
        { $$= &Select->interval_list; }
unknown's avatar
unknown committed
5019 5020 5021 5022 5023 5024
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
unknown's avatar
unknown committed
5025 5026 5027

key_usage_list2:
	key_usage_list2 ',' ident
unknown's avatar
unknown committed
5028
        { Select->
unknown's avatar
unknown committed
5029
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $3.str, $3.length,
5030
				    system_charset_info)); }
unknown's avatar
unknown committed
5031
	| ident
unknown's avatar
unknown committed
5032
        { Select->
unknown's avatar
unknown committed
5033
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $1.str, $1.length,
5034
				    system_charset_info)); }
unknown's avatar
unknown committed
5035
	| PRIMARY_SYM
unknown's avatar
unknown committed
5036
        { Select->
unknown's avatar
unknown committed
5037
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
5038
				    system_charset_info)); };
unknown's avatar
unknown committed
5039 5040 5041

using_list:
	ident
unknown's avatar
unknown committed
5042
	  {
unknown's avatar
unknown committed
5043
	    SELECT_LEX *sel= Select;
5044 5045 5046 5047
	    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
5048 5049 5050 5051
	      YYABORT;
	  }
	| using_list ',' ident
	  {
unknown's avatar
unknown committed
5052
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5053
	    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
5054
	      YYABORT;
unknown's avatar
unknown committed
5055
	  };
unknown's avatar
unknown committed
5056 5057

interval:
5058 5059
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
unknown's avatar
unknown committed
5060
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
unknown's avatar
unknown committed
5061 5062
	| DAY_MINUTE_SYM	{ $$=INTERVAL_DAY_MINUTE; }
	| DAY_SECOND_SYM	{ $$=INTERVAL_DAY_SECOND; }
unknown's avatar
unknown committed
5063
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
unknown's avatar
unknown committed
5064 5065
	| HOUR_MINUTE_SYM	{ $$=INTERVAL_HOUR_MINUTE; }
	| HOUR_SECOND_SYM	{ $$=INTERVAL_HOUR_SECOND; }
unknown's avatar
unknown committed
5066 5067
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
unknown's avatar
unknown committed
5068
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
5069 5070 5071 5072 5073 5074 5075 5076
	| 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
5077 5078
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
5079
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
unknown's avatar
unknown committed
5080
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
5081 5082
	| YEAR_SYM		{ $$=INTERVAL_YEAR; }
        ;
unknown's avatar
unknown committed
5083

5084
date_time_type:
5085 5086 5087 5088 5089
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
5090

unknown's avatar
unknown committed
5091 5092 5093
table_alias:
	/* empty */
	| AS
unknown's avatar
unknown committed
5094
	| EQ;
unknown's avatar
unknown committed
5095 5096 5097 5098

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

unknown's avatar
unknown committed
5101 5102 5103 5104
opt_all:
	/* empty */
	| ALL
	;
unknown's avatar
unknown committed
5105 5106

where_clause:
unknown's avatar
unknown committed
5107
	/* empty */  { Select->where= 0; }
unknown's avatar
unknown committed
5108 5109 5110 5111 5112
	| WHERE
          {
            Select->parsing_place= IN_WHERE;
          }
          expr
5113
	  {
unknown's avatar
unknown committed
5114 5115 5116 5117 5118
            SELECT_LEX *select= Select;
	    select->where= $3;
            select->parsing_place= NO_MATTER;
	    if ($3)
	      $3->top_level_item();
5119
	  }
unknown's avatar
unknown committed
5120
 	;
unknown's avatar
unknown committed
5121 5122 5123

having_clause:
	/* empty */
unknown's avatar
unknown committed
5124 5125
	| HAVING
	  {
5126
	    Select->parsing_place= IN_HAVING;
unknown's avatar
unknown committed
5127 5128
          }
	  expr
unknown's avatar
unknown committed
5129
	  {
unknown's avatar
unknown committed
5130
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5131
	    sel->having= $3;
5132
	    sel->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
5133 5134 5135
	    if ($3)
	      $3->top_level_item();
	  }
5136
	;
unknown's avatar
unknown committed
5137 5138

opt_escape:
5139 5140
	ESCAPE_SYM simple_expr { $$= $2; }
	| /* empty */
5141 5142 5143 5144 5145
          {

            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
5146 5147
          }
        ;
unknown's avatar
unknown committed
5148 5149 5150


/*
unknown's avatar
unknown committed
5151
   group by statement in select
unknown's avatar
unknown committed
5152 5153 5154 5155
*/

group_clause:
	/* empty */
5156
	| GROUP BY group_list olap_opt;
unknown's avatar
unknown committed
5157 5158

group_list:
unknown's avatar
unknown committed
5159
	group_list ',' order_ident order_dir
unknown's avatar
unknown committed
5160
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
5161
	| order_ident order_dir
unknown's avatar
unknown committed
5162
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
5163

5164 5165
olap_opt:
	/* empty */ {}
5166
	| WITH CUBE_SYM
5167
          {
5168
	    LEX *lex=Lex;
5169 5170
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
5171
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
5172 5173 5174
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
5175
	    lex->current_select->olap= CUBE_TYPE;
unknown's avatar
unknown committed
5176
	    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
5177
	    YYABORT;	/* To be deleted in 5.1 */
5178
	  }
5179
	| WITH ROLLUP_SYM
5180
          {
5181 5182 5183
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
5184
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
5185 5186 5187
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
5188
	    lex->current_select->olap= ROLLUP_TYPE;
5189
	  }
5190
	;
5191

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

5196
opt_order_clause:
unknown's avatar
unknown committed
5197
	/* empty */
unknown's avatar
unknown committed
5198
	| order_clause;
5199 5200

order_clause:
5201 5202
	ORDER_SYM BY
        {
unknown's avatar
unknown committed
5203
	  LEX *lex=Lex;
5204
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
unknown's avatar
unknown committed
5205
	      lex->current_select->olap !=
5206
	      UNSPECIFIED_OLAP_TYPE)
5207
	  {
unknown's avatar
unknown committed
5208 5209
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
5210 5211
	    YYABORT;
	  }
unknown's avatar
unknown committed
5212
	} order_list;
unknown's avatar
unknown committed
5213 5214 5215

order_list:
	order_list ',' order_ident order_dir
unknown's avatar
unknown committed
5216
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
5217
	| order_ident order_dir
unknown's avatar
unknown committed
5218
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
5219 5220 5221

order_dir:
	/* empty */ { $$ =  1; }
unknown's avatar
unknown committed
5222
	| ASC  { $$ =1; }
unknown's avatar
unknown committed
5223
	| DESC { $$ =0; };
unknown's avatar
unknown committed
5224 5225


5226 5227 5228
opt_limit_clause_init:
	/* empty */
	{
5229 5230
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
5231
          sel->offset_limit= 0L;
5232
          sel->select_limit= HA_POS_ERROR;
5233
	}
5234 5235 5236
	| limit_clause {}
	;

5237 5238 5239 5240 5241
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

5242
limit_clause:
5243
	LIMIT limit_options {}
unknown's avatar
unknown committed
5244
	;
unknown's avatar
unknown committed
5245 5246 5247

limit_options:
	ULONG_NUM
unknown's avatar
unknown committed
5248
	  {
unknown's avatar
unknown committed
5249
            SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5250 5251
            sel->select_limit= $1;
            sel->offset_limit= 0L;
5252
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5253 5254 5255
	  }
	| ULONG_NUM ',' ULONG_NUM
	  {
unknown's avatar
unknown committed
5256
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5257 5258
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
5259
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5260
	  }
unknown's avatar
unknown committed
5261
	| ULONG_NUM OFFSET_SYM ULONG_NUM
unknown's avatar
unknown committed
5262
	  {
unknown's avatar
unknown committed
5263
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5264 5265
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
5266
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5267 5268
	  }
	;
unknown's avatar
unknown committed
5269 5270 5271 5272 5273


delete_limit_clause:
	/* empty */
	{
unknown's avatar
unknown committed
5274
	  LEX *lex=Lex;
5275
	  lex->current_select->select_limit= HA_POS_ERROR;
unknown's avatar
unknown committed
5276
	}
unknown's avatar
unknown committed
5277
	| LIMIT ulonglong_num
5278 5279 5280 5281 5282
	{
	  SELECT_LEX *sel= Select;
	  sel->select_limit= (ha_rows) $2;
	  sel->explicit_limit= 1;
	};
unknown's avatar
unknown committed
5283 5284

ULONG_NUM:
unknown's avatar
unknown committed
5285 5286 5287 5288 5289 5290
	NUM	        { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| LONG_NUM      { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| ULONGLONG_NUM { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| REAL_NUM 	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
5291

unknown's avatar
unknown committed
5292
ulonglong_num:
unknown's avatar
unknown committed
5293 5294 5295 5296 5297 5298
	NUM	    { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| ULONGLONG_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| LONG_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| REAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
5299 5300 5301 5302 5303 5304

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
5305 5306
	    if (&lex->select_lex != lex->current_select)
	    {
unknown's avatar
unknown committed
5307
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
5308 5309
	      YYABORT;
	    }
unknown's avatar
unknown committed
5310 5311 5312
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
unknown's avatar
unknown committed
5313
	    if (add_proc_to_list(lex->thd, new Item_field(NULL,NULL,$2.str)))
unknown's avatar
unknown committed
5314
	      YYABORT;
5315
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
5316
	  }
unknown's avatar
unknown committed
5317
	  '(' procedure_list ')';
unknown's avatar
unknown committed
5318 5319 5320 5321


procedure_list:
	/* empty */ {}
unknown's avatar
unknown committed
5322
	| procedure_list2 {};
unknown's avatar
unknown committed
5323 5324 5325

procedure_list2:
	procedure_list2 ',' procedure_item
unknown's avatar
unknown committed
5326
	| procedure_item;
unknown's avatar
unknown committed
5327 5328 5329 5330

procedure_item:
	  remember_name expr
	  {
unknown's avatar
unknown committed
5331 5332
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
unknown's avatar
unknown committed
5333 5334
	      YYABORT;
	    if (!$2->name)
unknown's avatar
unknown committed
5335
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1), YYTHD->charset());
5336 5337 5338 5339 5340 5341
	  }
          ;


select_var_list_init:
	   {
unknown's avatar
unknown committed
5342 5343
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
5344 5345
	        YYABORT;
	   }
5346
	   select_var_list
5347
	   {}
5348
           ;
unknown's avatar
unknown committed
5349

unknown's avatar
unknown committed
5350
select_var_list:
5351 5352 5353 5354
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

5355 5356 5357 5358 5359 5360 5361 5362 5363 5364
select_var_ident:  
	   '@' ident_or_text
           {
             LEX *lex=Lex;
	     if (lex->result) 
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($2,0,0,(enum_field_types)0));
	     else
	       YYABORT;
	   }
           | ident_or_text
unknown's avatar
unknown committed
5365
           {
5366
             LEX *lex=Lex;
5367 5368 5369 5370
	     sp_pvar_t *t;

	     if (!lex->spcont || !(t=lex->spcont->find_pvar(&$1)))
	     {
5371
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
unknown's avatar
unknown committed
5372
	       YYABORT;
5373 5374 5375 5376 5377 5378 5379 5380
	     }
	     if (! lex->result)
	       YYABORT;
	     else
	     {
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($1,1,t->offset,t->type));
	       t->isset= TRUE;
	     }
unknown's avatar
unknown committed
5381
	   }
unknown's avatar
unknown committed
5382
           ;
unknown's avatar
unknown committed
5383

5384
into:
unknown's avatar
unknown committed
5385
        INTO OUTFILE TEXT_STRING_sys
unknown's avatar
unknown committed
5386
	{
unknown's avatar
unknown committed
5387
          LEX *lex= Lex;
5388 5389 5390 5391
          lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
          if (!(lex->exchange= new sql_exchange($3.str, 0)) ||
              !(lex->result= new select_export(lex->exchange)))
            YYABORT;
unknown's avatar
unknown committed
5392 5393
	}
	opt_field_term opt_line_term
unknown's avatar
unknown committed
5394
	| INTO DUMPFILE TEXT_STRING_sys
unknown's avatar
unknown committed
5395
	{
5396
	  LEX *lex=Lex;
unknown's avatar
unknown committed
5397 5398
	  if (!lex->describe)
	  {
5399
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
5400 5401 5402 5403 5404
	    if (!(lex->exchange= new sql_exchange($3.str,1)))
	      YYABORT;
	    if (!(lex->result= new select_dump(lex->exchange)))
	      YYABORT;
	  }
unknown's avatar
unknown committed
5405
	}
5406
        | INTO select_var_list_init
unknown's avatar
unknown committed
5407
	{
5408
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5409 5410
	}
        ;
unknown's avatar
unknown committed
5411

unknown's avatar
unknown committed
5412 5413 5414
/*
  DO statement
*/
unknown's avatar
unknown committed
5415

5416
do:	DO_SYM
unknown's avatar
unknown committed
5417 5418 5419
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
5420 5421 5422 5423 5424
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
unknown's avatar
unknown committed
5425
	}
5426 5427
	;

unknown's avatar
unknown committed
5428
/*
5429
  Drop : delete tables or index or user
unknown's avatar
unknown committed
5430 5431 5432
*/

drop:
unknown's avatar
unknown committed
5433
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
unknown's avatar
unknown committed
5434
	{
unknown's avatar
unknown committed
5435 5436
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
5437 5438
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
unknown's avatar
unknown committed
5439
	}
unknown's avatar
unknown committed
5440
	| DROP INDEX_SYM ident ON table_ident {}
unknown's avatar
unknown committed
5441
	  {
unknown's avatar
unknown committed
5442 5443
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
5444 5445 5446
	     lex->alter_info.drop_list.empty();
	     lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
                                                                $3.str));
unknown's avatar
unknown committed
5447 5448
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
unknown's avatar
unknown committed
5449 5450 5451 5452
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
unknown's avatar
unknown committed
5453 5454 5455 5456
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
	    lex->name=$4.str;
unknown's avatar
unknown committed
5457
	 }
5458
	| DROP FUNCTION_SYM if_exists sp_name
unknown's avatar
unknown committed
5459
	  {
unknown's avatar
unknown committed
5460
	    LEX *lex=Lex;
5461 5462
	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
5463
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
5464 5465
	      YYABORT;
	    }
unknown's avatar
unknown committed
5466
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
5467 5468 5469 5470 5471 5472 5473 5474
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
	  }
	| DROP PROCEDURE if_exists sp_name
	  {
	    LEX *lex=Lex;
	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
5475
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
5476 5477 5478 5479 5480
	      YYABORT;
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
5481 5482 5483 5484 5485 5486
	  }
	| DROP USER
	  {
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_DROP_USER;
	    lex->users_list.empty();
unknown's avatar
unknown committed
5487
	  }
5488 5489
	  user_list
	  {}
5490 5491 5492 5493 5494 5495 5496 5497 5498 5499
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
        | DROP TRIGGER_SYM ident '.' ident
          {
            LEX *lex= Lex;
unknown's avatar
unknown committed
5500

5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511
            lex->sql_command= SQLCOM_DROP_TRIGGER;
            /* QQ: Could we loosen lock type in certain cases ? */
            if (!lex->select_lex.add_table_to_list(YYTHD,
                                                   new Table_ident($3),
                                                   (LEX_STRING*) 0,
                                                   TL_OPTION_UPDATING,
                                                   TL_WRITE))
              YYABORT;
            lex->name_and_length= $5;
          }
	;
unknown's avatar
unknown committed
5512 5513

table_list:
5514
	table_name
unknown's avatar
unknown committed
5515
	| table_list ',' table_name;
unknown's avatar
unknown committed
5516

5517
table_name:
unknown's avatar
unknown committed
5518
	table_ident
unknown's avatar
unknown committed
5519 5520 5521 5522 5523
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
unknown's avatar
unknown committed
5524 5525

if_exists:
5526
	/* empty */ { $$= 0; }
5527 5528
	| IF EXISTS { $$= 1; }
	;
unknown's avatar
unknown committed
5529

5530 5531 5532 5533
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
unknown's avatar
unknown committed
5534 5535 5536 5537 5538
/*
** Insert : add new data to table
*/

insert:
5539 5540 5541 5542 5543 5544
	INSERT
	{
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_INSERT;
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
5545
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
5546
	} insert_lock_option
5547
	opt_ignore insert2
unknown's avatar
unknown committed
5548
	{
unknown's avatar
unknown committed
5549
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
5550
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
5551
	}
5552
	insert_field_spec opt_insert_update
5553
	{}
unknown's avatar
unknown committed
5554
	;
unknown's avatar
unknown committed
5555 5556

replace:
5557 5558
	REPLACE
	{
5559
	  LEX *lex=Lex;
5560 5561
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
5562
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
5563
	}
unknown's avatar
unknown committed
5564 5565
	replace_lock_option insert2
	{
unknown's avatar
unknown committed
5566
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
5567
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
5568 5569
	}
	insert_field_spec
5570
	{}
unknown's avatar
unknown committed
5571
	;
unknown's avatar
unknown committed
5572 5573

insert_lock_option:
unknown's avatar
unknown committed
5574 5575 5576 5577
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
5578
	;
unknown's avatar
unknown committed
5579 5580

replace_lock_option:
unknown's avatar
unknown committed
5581 5582
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
unknown's avatar
unknown committed
5583 5584 5585

insert2:
	INTO insert_table {}
unknown's avatar
unknown committed
5586
	| insert_table {};
unknown's avatar
unknown committed
5587 5588

insert_table:
5589
	table_name
unknown's avatar
unknown committed
5590
	{
unknown's avatar
unknown committed
5591 5592 5593 5594
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
unknown's avatar
unknown committed
5595
	};
unknown's avatar
unknown committed
5596 5597

insert_field_spec:
unknown's avatar
unknown committed
5598 5599 5600
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
unknown's avatar
unknown committed
5601 5602
	| SET
	  {
unknown's avatar
unknown committed
5603 5604 5605
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
5606 5607
	      YYABORT;
	   }
unknown's avatar
unknown committed
5608
	   ident_eq_list;
unknown's avatar
unknown committed
5609 5610 5611 5612

opt_field_spec:
	/* empty */	  { }
	| '(' fields ')'  { }
unknown's avatar
unknown committed
5613
	| '(' ')'	  { };
unknown's avatar
unknown committed
5614 5615 5616

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
unknown's avatar
unknown committed
5617
	| insert_ident		{ Lex->field_list.push_back($1); };
unknown's avatar
unknown committed
5618 5619 5620

insert_values:
	VALUES	values_list  {}
unknown's avatar
unknown committed
5621
	| VALUE_SYM values_list  {}
unknown's avatar
unknown committed
5622 5623
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
5624
        ;
unknown's avatar
unknown committed
5625 5626 5627

values_list:
	values_list ','  no_braces
unknown's avatar
unknown committed
5628
	| no_braces;
unknown's avatar
unknown committed
5629 5630 5631 5632

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
unknown's avatar
unknown committed
5633
	ident_eq_value;
unknown's avatar
unknown committed
5634 5635

ident_eq_value:
5636
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
5637
	 {
unknown's avatar
unknown committed
5638 5639 5640
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
unknown's avatar
unknown committed
5641
	    YYABORT;
unknown's avatar
unknown committed
5642
	 };
unknown's avatar
unknown committed
5643 5644

equal:	EQ		{}
unknown's avatar
unknown committed
5645 5646 5647 5648 5649 5650 5651
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
unknown's avatar
unknown committed
5652 5653 5654 5655 5656 5657 5658 5659 5660

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
unknown's avatar
unknown committed
5661 5662
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
5663
	    YYABORT;
unknown's avatar
unknown committed
5664
	 };
unknown's avatar
unknown committed
5665 5666 5667

opt_values:
	/* empty */ {}
unknown's avatar
unknown committed
5668
	| values;
unknown's avatar
unknown committed
5669 5670

values:
unknown's avatar
unknown committed
5671
	values ','  expr_or_default
unknown's avatar
unknown committed
5672 5673 5674 5675
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
unknown's avatar
unknown committed
5676 5677 5678 5679 5680 5681 5682 5683
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
5684
	expr	  { $$= $1;}
unknown's avatar
SCRUM  
unknown committed
5685
	| DEFAULT {$$= new Item_default_value(); }
unknown's avatar
unknown committed
5686
	;
unknown's avatar
unknown committed
5687

5688 5689
opt_insert_update:
        /* empty */
unknown's avatar
unknown committed
5690
        | ON DUPLICATE_SYM
5691 5692 5693 5694 5695 5696 5697
          { 
            LEX *lex= Lex;
            /*
              For simplicity, let's forget about INSERT ... SELECT ... UPDATE
              for a moment.
            */
	    if (lex->sql_command != SQLCOM_INSERT)
5698
            {
5699
	      yyerror(ER(ER_SYNTAX_ERROR));
5700 5701 5702
              YYABORT;
            }
          }
5703
          KEY_SYM UPDATE_SYM update_list
5704 5705
        ;

unknown's avatar
unknown committed
5706 5707 5708
/* Update rows in a table */

update:
5709 5710
	UPDATE_SYM
	{
5711
	  LEX *lex= Lex;
unknown's avatar
unknown committed
5712
	  mysql_init_select(lex);
5713
          lex->sql_command= SQLCOM_UPDATE;
unknown's avatar
unknown committed
5714
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
5715
        }
unknown's avatar
unknown committed
5716
        opt_low_priority opt_ignore join_table_list
unknown's avatar
unknown committed
5717
	SET update_list
unknown's avatar
unknown committed
5718
	{
5719 5720
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
5721
	  {
unknown's avatar
unknown committed
5722
            lex->sql_command= SQLCOM_UPDATE_MULTI;
unknown's avatar
unknown committed
5723
	    lex->multi_lock_option= $3;
5724
	  }
5725 5726 5727
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
5728 5729
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
5730 5731
	    YYABORT;
	  }
5732
	  else
unknown's avatar
unknown committed
5733
	    Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
5734
	}
5735
	where_clause opt_order_clause delete_limit_clause {}
unknown's avatar
unknown committed
5736
	;
unknown's avatar
unknown committed
5737 5738

update_list:
5739
	update_list ',' simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
5740
	{
unknown's avatar
unknown committed
5741
	  if (add_item_to_list(YYTHD, $3) || add_value_to_list(YYTHD, $5))
unknown's avatar
unknown committed
5742 5743
	    YYABORT;
	}
5744
	| simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
5745
	  {
unknown's avatar
unknown committed
5746
	    if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
unknown's avatar
unknown committed
5747
	      YYABORT;
unknown's avatar
unknown committed
5748
	  };
unknown's avatar
unknown committed
5749 5750

opt_low_priority:
5751
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
5752
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
5753 5754 5755 5756

/* Delete rows from a table */

delete:
5757
	DELETE_SYM
5758
	{
5759 5760
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
unknown's avatar
unknown committed
5761
	  lex->lock_option= lex->thd->update_lock_default;
5762
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
5763
	}
unknown's avatar
unknown committed
5764 5765
	opt_delete_options single_multi {}
	;
5766 5767

single_multi:
unknown's avatar
unknown committed
5768 5769
 	FROM table_ident
	{
unknown's avatar
unknown committed
5770 5771
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
unknown's avatar
unknown committed
5772 5773 5774
	    YYABORT;
	}
	where_clause opt_order_clause
5775
	delete_limit_clause {}
unknown's avatar
unknown committed
5776
	| table_wild_list
unknown's avatar
unknown committed
5777
	  { mysql_init_multi_delete(Lex); }
unknown's avatar
unknown committed
5778
          FROM join_table_list where_clause
unknown's avatar
unknown committed
5779 5780
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
5781 5782 5783
	  USING join_table_list where_clause
	  {}
	;
unknown's avatar
unknown committed
5784 5785 5786

table_wild_list:
	  table_wild_one {}
unknown's avatar
unknown committed
5787
	  | table_wild_list ',' table_wild_one {};
unknown's avatar
unknown committed
5788 5789

table_wild_one:
unknown's avatar
unknown committed
5790
	ident opt_wild opt_table_alias
unknown's avatar
unknown committed
5791
	{
unknown's avatar
unknown committed
5792 5793
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
					 TL_OPTION_UPDATING, Lex->lock_option))
unknown's avatar
unknown committed
5794 5795
	    YYABORT;
        }
unknown's avatar
unknown committed
5796
	| ident '.' ident opt_wild opt_table_alias
unknown's avatar
unknown committed
5797
	  {
unknown's avatar
unknown committed
5798 5799
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
unknown's avatar
unknown committed
5800 5801
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
unknown's avatar
unknown committed
5802
	      YYABORT;
unknown's avatar
unknown committed
5803
	  }
unknown's avatar
unknown committed
5804
	;
unknown's avatar
unknown committed
5805 5806

opt_wild:
5807
	/* empty */	{}
unknown's avatar
unknown committed
5808
	| '.' '*'	{};
unknown's avatar
unknown committed
5809 5810


5811
opt_delete_options:
unknown's avatar
unknown committed
5812
	/* empty */	{}
unknown's avatar
unknown committed
5813
	| opt_delete_option opt_delete_options {};
5814 5815

opt_delete_option:
5816
	QUICK		{ Select->options|= OPTION_QUICK; }
5817 5818
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
	| IGNORE_SYM	{ Lex->duplicates= DUP_IGNORE; };
5819

5820
truncate:
unknown's avatar
unknown committed
5821
	TRUNCATE_SYM opt_table_sym table_name
5822
	{
5823
	  LEX* lex= Lex;
5824
	  lex->sql_command= SQLCOM_TRUNCATE;
5825
	  lex->select_lex.options= 0;
5826
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
5827 5828
	}
	;
5829

unknown's avatar
unknown committed
5830 5831
opt_table_sym:
	/* empty */
unknown's avatar
unknown committed
5832
	| TABLE_SYM;
5833

unknown's avatar
unknown committed
5834 5835
/* Show things */

5836 5837
show:	SHOW
	{
unknown's avatar
unknown committed
5838 5839 5840 5841
	  LEX *lex=Lex;
	  lex->wild=0;
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
unknown's avatar
unknown committed
5842
	show_param
5843 5844
	{}
	;
unknown's avatar
unknown committed
5845 5846

show_param:
5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872
         DATABASES ext_select_item_list wild_and_where
         {
           LEX *lex= Lex;
           lex->sql_command= SQLCOM_SELECT;
           lex->orig_sql_command= SQLCOM_SHOW_DATABASES;
           if (prepare_schema_table(YYTHD, lex, 0, SCH_SCHEMATA))
             YYABORT;
         }
         | opt_full TABLES ext_select_item_list opt_db wild_and_where
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLES;
             lex->select_lex.db= $4;
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLE_NAMES))
               YYABORT;
           }
         | TABLE_SYM STATUS_SYM ext_select_item_list opt_db wild_and_where
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLE_STATUS;
             lex->select_lex.db= $4;
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLES))
               YYABORT;
           }
5873
	| OPEN_SYM TABLES opt_db wild
unknown's avatar
unknown committed
5874
	  {
5875
	    LEX *lex= Lex;
unknown's avatar
unknown committed
5876
	    lex->sql_command= SQLCOM_SHOW_OPEN_TABLES;
5877
	    lex->select_lex.db= $3;
5878
	  }
unknown's avatar
unknown committed
5879
	| ENGINE_SYM storage_engines 
unknown's avatar
unknown committed
5880 5881
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
5882
	| opt_full COLUMNS ext_select_item_list from_table_ident opt_db wild_and_where
unknown's avatar
unknown committed
5883
	  {
5884 5885 5886
 	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SELECT;
	    lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
unknown's avatar
unknown committed
5887 5888
	    if ($5)
	      $4->change_db($5);
5889
	    if (prepare_schema_table(YYTHD, lex, $4, SCH_COLUMNS))
unknown's avatar
unknown committed
5890 5891
	      YYABORT;
	  }
5892
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
unknown's avatar
unknown committed
5893 5894
	  TEXT_STRING_sys AND_SYM MASTER_LOG_POS_SYM EQ ulonglong_num
	  AND_SYM MASTER_SERVER_ID_SYM EQ
unknown's avatar
unknown committed
5895 5896 5897 5898 5899
	ULONG_NUM
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
5900
	    Lex->mi.server_id = $16;
unknown's avatar
unknown committed
5901
          }
5902
        | master_or_binary LOGS_SYM
unknown's avatar
unknown committed
5903 5904
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
5905 5906 5907 5908 5909
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
unknown's avatar
unknown committed
5910
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
unknown's avatar
unknown committed
5911
          {
5912 5913
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
5914
          } opt_limit_clause_init
5915 5916 5917 5918 5919
        | keys_or_index ext_select_item_list from_table_ident opt_db where_clause
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_KEYS;
unknown's avatar
unknown committed
5920 5921
	    if ($4)
	      $3->change_db($4);
5922 5923
            if (prepare_schema_table(YYTHD, lex, $3, SCH_STATISTICS))
              YYABORT;
unknown's avatar
unknown committed
5924
	  }
unknown's avatar
unknown committed
5925 5926 5927 5928 5929 5930 5931 5932
	| 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
5933 5934 5935 5936 5937 5938 5939
	    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
5940 5941 5942 5943 5944 5945
	  }
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
5946
        | COUNT_SYM '(' '*' ')' WARNINGS
5947
          { (void) create_select_for_variable("warning_count"); }
5948
        | COUNT_SYM '(' '*' ')' ERRORS
5949
	  { (void) create_select_for_variable("error_count"); }
5950
        | WARNINGS opt_limit_clause_init
unknown's avatar
unknown committed
5951
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
5952
        | ERRORS opt_limit_clause_init
5953
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
5954 5955 5956 5957 5958 5959
	| opt_var_type STATUS_SYM wild
          {
	    THD *thd= YYTHD;
	    thd->lex->sql_command= SQLCOM_SHOW_STATUS;
	    thd->lex->option_type= (enum_var_type) $1;
	  }	
unknown's avatar
unknown committed
5960
        | INNOBASE_SYM STATUS_SYM
unknown's avatar
unknown committed
5961
          { Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS; WARN_DEPRECATED("SHOW INNODB STATUS", "SHOW ENGINE INNODB STATUS"); }
unknown's avatar
unknown committed
5962 5963
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
5964
	| opt_var_type VARIABLES wild
unknown's avatar
unknown committed
5965
	  {
5966
	    THD *thd= YYTHD;
5967 5968
	    thd->lex->sql_command= SQLCOM_SHOW_VARIABLES;
	    thd->lex->option_type= (enum_var_type) $1;
5969
	  }
5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985
        | charset ext_select_item_list wild_and_where
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_CHARSETS;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_CHARSETS))
              YYABORT;
          }
        | COLLATION_SYM ext_select_item_list wild_and_where
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_COLLATIONS;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_COLLATIONS))
              YYABORT;
          }
5986
	| BERKELEY_DB_SYM LOGS_SYM
unknown's avatar
unknown committed
5987
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW BDB LOGS", "SHOW ENGINE BDB LOGS"); }
unknown's avatar
unknown committed
5988
	| LOGS_SYM
unknown's avatar
unknown committed
5989
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW LOGS", "SHOW ENGINE BDB LOGS"); }
5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012
	| GRANTS
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    THD *thd= lex->thd;
	    LEX_USER *curr_user;
            if (!(curr_user= (LEX_USER*) thd->alloc(sizeof(st_lex_user))))
              YYABORT;
            curr_user->user.str= thd->priv_user;
            curr_user->user.length= strlen(thd->priv_user);
            if (*thd->priv_host != 0)
            {
              curr_user->host.str= thd->priv_host;
              curr_user->host.length= strlen(thd->priv_host);
            }
            else
            {
              curr_user->host.str= (char *) "%";
              curr_user->host.length= 1;
            }
            curr_user->password.str=NullS;
	    lex->grant_user= curr_user;
	  }
unknown's avatar
unknown committed
6013
	| GRANTS FOR_SYM user
unknown's avatar
unknown committed
6014 6015 6016 6017 6018 6019
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
	    lex->grant_user->password.str=NullS;
	  }
unknown's avatar
unknown committed
6020
	| CREATE DATABASE opt_if_not_exists ident
unknown's avatar
unknown committed
6021 6022
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
unknown's avatar
unknown committed
6023 6024
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
unknown's avatar
unknown committed
6025
	  }
unknown's avatar
unknown committed
6026 6027
        | CREATE TABLE_SYM table_ident
          {
6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038
            LEX *lex= Lex;
	    lex->sql_command = SQLCOM_SHOW_CREATE;
	    if (!lex->select_lex.add_table_to_list(YYTHD, $3, NULL,0))
	      YYABORT;
            lex->only_view= 0;
	  }
        | CREATE VIEW_SYM table_ident
          {
            LEX *lex= Lex;
	    lex->sql_command = SQLCOM_SHOW_CREATE;
	    if (!lex->select_lex.add_table_to_list(YYTHD, $3, NULL, 0))
unknown's avatar
unknown committed
6039
	      YYABORT;
6040
            lex->only_view= 1;
unknown's avatar
unknown committed
6041 6042 6043 6044
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
unknown's avatar
unknown committed
6045
          }
unknown's avatar
unknown committed
6046 6047 6048
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063
          }
	| CREATE PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

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

	    lex->sql_command = SQLCOM_SHOW_CREATE_FUNC;
	    lex->spname= $3;
	  }
6064
	| PROCEDURE STATUS_SYM ext_select_item_list wild_and_where
6065
	  {
6066 6067 6068 6069 6070
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_PROC;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
6071
	  }
6072
	| FUNCTION_SYM STATUS_SYM ext_select_item_list wild_and_where
6073
	  {
6074 6075 6076 6077 6078
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_FUNC;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
6079
	  };
unknown's avatar
unknown committed
6080

unknown's avatar
unknown committed
6081 6082 6083 6084 6085 6086 6087 6088
show_engine_param:
	STATUS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_INNODB:
	      Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;
	      break;
	    default:
unknown's avatar
unknown committed
6089
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "STATUS");
unknown's avatar
unknown committed
6090 6091 6092 6093 6094 6095 6096 6097 6098 6099
	      YYABORT;
	    }
	  }
	| LOGS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_BERKELEY_DB:
	      Lex->sql_command = SQLCOM_SHOW_LOGS;
	      break;
	    default:
unknown's avatar
unknown committed
6100
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "LOGS");
unknown's avatar
unknown committed
6101 6102 6103 6104
	      YYABORT;
	    }
	  };

6105 6106 6107 6108
master_or_binary:
	MASTER_SYM
	| BINARY;

unknown's avatar
unknown committed
6109 6110 6111 6112
opt_storage:
	/* empty */
	| STORAGE_SYM;

unknown's avatar
unknown committed
6113 6114
opt_db:
	/* empty */  { $$= 0; }
unknown's avatar
unknown committed
6115
	| from_or_in ident { $$= $2.str; };
unknown's avatar
unknown committed
6116 6117 6118

wild:
	/* empty */
6119
	| LIKE TEXT_STRING_sys
unknown's avatar
unknown committed
6120
	  { Lex->wild=  new (YYTHD->mem_root) String($2.str, $2.length,
6121
                                                      system_charset_info); };
unknown's avatar
unknown committed
6122

unknown's avatar
unknown committed
6123 6124
opt_full:
	/* empty */ { Lex->verbose=0; }
unknown's avatar
unknown committed
6125
	| FULL	    { Lex->verbose=1; };
unknown's avatar
unknown committed
6126

unknown's avatar
unknown committed
6127 6128
from_or_in:
	FROM
unknown's avatar
unknown committed
6129
	| IN_SYM;
unknown's avatar
unknown committed
6130

unknown's avatar
unknown committed
6131 6132
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
unknown's avatar
unknown committed
6133
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
unknown's avatar
unknown committed
6134 6135 6136

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

6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167
from_table_ident:
      /* empty */       { $$= 0; }
      | from_or_in table_ident  { $$= $2; }
      ;

wild_and_where:
      /* empty */
      | LIKE TEXT_STRING_sys
	{ Lex->wild=  new (YYTHD->mem_root) String($2.str, $2.length,
                                                   system_charset_info); }
      | WHERE expr
        {
          Select->where= $2;
          if ($2)
            $2->top_level_item();
        }
      ;

ext_select_item_list:
      {
        LEX *lex=Lex;
        SELECT_LEX *sel= lex->current_select;
        lex->lock_option= TL_READ;
        mysql_init_select(lex);
        lex->current_select->parsing_place= SELECT_LIST;
      }
      /* empty */
      | select_item_list {};

unknown's avatar
unknown committed
6168

unknown's avatar
unknown committed
6169 6170 6171 6172
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
6173 6174 6175 6176 6177 6178 6179 6180 6181
          LEX *lex= Lex;
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
          lex->sql_command= SQLCOM_SELECT;
          lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
          lex->select_lex.db= 0;
          lex->verbose= 0;
          if (prepare_schema_table(YYTHD, lex, $2, SCH_COLUMNS))
unknown's avatar
unknown committed
6182 6183
	    YYABORT;
	}
6184
	opt_describe_column {}
unknown's avatar
unknown committed
6185 6186 6187
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
6188
          {
unknown's avatar
unknown committed
6189
	    LEX *lex=Lex;
6190
	    lex->select_lex.options|= SELECT_DESCRIBE;
unknown's avatar
unknown committed
6191 6192
	  }
	;
unknown's avatar
unknown committed
6193 6194 6195

describe_command:
	DESC
unknown's avatar
unknown committed
6196
	| DESCRIBE;
unknown's avatar
unknown committed
6197

unknown's avatar
unknown committed
6198 6199 6200 6201 6202
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
	;

unknown's avatar
unknown committed
6203 6204 6205
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
unknown's avatar
unknown committed
6206
	| ident
unknown's avatar
unknown committed
6207
	  { Lex->wild= new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info); };
unknown's avatar
unknown committed
6208 6209 6210 6211 6212


/* flush things */

flush:
6213
	FLUSH_SYM opt_no_write_to_binlog
unknown's avatar
unknown committed
6214 6215 6216
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
unknown's avatar
unknown committed
6217
          lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
6218
	}
6219 6220 6221
	flush_options
	{}
	;
unknown's avatar
unknown committed
6222 6223 6224

flush_options:
	flush_options ',' flush_option
unknown's avatar
unknown committed
6225
	| flush_option;
unknown's avatar
unknown committed
6226 6227

flush_option:
6228
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
unknown's avatar
unknown committed
6229
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
unknown's avatar
unknown committed
6230
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
unknown's avatar
unknown committed
6231 6232 6233 6234
	| 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
6235 6236
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
6237 6238
	| DES_KEY_FILE	{ Lex->type|= REFRESH_DES_KEY_FILE; }
 	| RESOURCES     { Lex->type|= REFRESH_USER_RESOURCES; };
unknown's avatar
unknown committed
6239

unknown's avatar
unknown committed
6240
opt_table_list:
6241 6242
	/* empty */  {;}
	| table_list {;};
unknown's avatar
unknown committed
6243

unknown's avatar
unknown committed
6244
reset:
unknown's avatar
unknown committed
6245 6246 6247 6248
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
6249 6250 6251 6252
	} reset_options
	{}
	;

unknown's avatar
unknown committed
6253 6254
reset_options:
	reset_options ',' reset_option
unknown's avatar
unknown committed
6255
	| reset_option;
unknown's avatar
unknown committed
6256 6257

reset_option:
unknown's avatar
unknown committed
6258 6259
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
unknown's avatar
unknown committed
6260
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
unknown's avatar
unknown committed
6261

unknown's avatar
unknown committed
6262
purge:
unknown's avatar
unknown committed
6263 6264 6265 6266
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
6267 6268 6269 6270 6271
	} purge_options
	{}
	;

purge_options:
6272
	master_or_binary LOGS_SYM purge_option
unknown's avatar
unknown committed
6273
	;
6274 6275

purge_option:
unknown's avatar
unknown committed
6276
        TO_SYM TEXT_STRING_sys
6277 6278 6279 6280 6281 6282
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
6283 6284 6285 6286
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
unknown's avatar
unknown committed
6287
	}
unknown's avatar
unknown committed
6288
	;
unknown's avatar
unknown committed
6289

unknown's avatar
unknown committed
6290 6291 6292
/* kill threads */

kill:
6293
	KILL_SYM kill_option expr
unknown's avatar
unknown committed
6294
	{
unknown's avatar
unknown committed
6295
	  LEX *lex=Lex;
6296 6297 6298
	  lex->value_list.empty();
	  lex->value_list.push_front($3);
          lex->sql_command= SQLCOM_KILL;
unknown's avatar
unknown committed
6299
	};
unknown's avatar
unknown committed
6300

6301 6302 6303 6304 6305
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; };

unknown's avatar
unknown committed
6306 6307 6308
/* change database */

use:	USE_SYM ident
unknown's avatar
unknown committed
6309 6310
	{
	  LEX *lex=Lex;
6311 6312
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
unknown's avatar
unknown committed
6313
	};
unknown's avatar
unknown committed
6314 6315 6316

/* import, export of files */

unknown's avatar
unknown committed
6317
load:	LOAD DATA_SYM load_data_lock opt_local INFILE TEXT_STRING_sys
unknown's avatar
unknown committed
6318
	{
unknown's avatar
unknown committed
6319 6320
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
unknown's avatar
unknown committed
6321 6322
	  lex->lock_option= $3;
	  lex->local_file=  $4;
unknown's avatar
unknown committed
6323
	  if (!(lex->exchange= new sql_exchange($6.str,0)))
unknown's avatar
unknown committed
6324
	    YYABORT;
unknown's avatar
unknown committed
6325
	  lex->field_list.empty();
unknown's avatar
unknown committed
6326 6327 6328 6329
	}
	opt_duplicate INTO TABLE_SYM table_ident opt_field_term opt_line_term
	opt_ignore_lines opt_field_spec
	{
unknown's avatar
unknown committed
6330
	  if (!Select->add_table_to_list(YYTHD, $11, NULL, TL_OPTION_UPDATING))
unknown's avatar
unknown committed
6331 6332 6333
	    YYABORT;
	}
        |
unknown's avatar
unknown committed
6334
	LOAD TABLE_SYM table_ident FROM MASTER_SYM
unknown's avatar
unknown committed
6335 6336
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
unknown's avatar
unknown committed
6337
	  if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
unknown's avatar
unknown committed
6338
	    YYABORT;
unknown's avatar
unknown committed
6339 6340

        }
6341 6342 6343 6344
        |
	LOAD DATA_SYM FROM MASTER_SYM
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
unknown's avatar
unknown committed
6345
        };
unknown's avatar
unknown committed
6346 6347 6348

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

unknown's avatar
unknown committed
6351
load_data_lock:
6352
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
6353 6354
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
6355 6356


unknown's avatar
unknown committed
6357 6358 6359
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
unknown's avatar
unknown committed
6360
	| IGNORE_SYM	{ Lex->duplicates=DUP_IGNORE; };
unknown's avatar
unknown committed
6361 6362 6363

opt_field_term:
	/* empty */
unknown's avatar
unknown committed
6364
	| COLUMNS field_term_list;
unknown's avatar
unknown committed
6365 6366 6367

field_term_list:
	field_term_list field_term
unknown's avatar
unknown committed
6368
	| field_term;
unknown's avatar
unknown committed
6369 6370

field_term:
6371 6372 6373 6374 6375
	TERMINATED BY text_string 
          {
            DBUG_ASSERT(Lex->exchange);
            Lex->exchange->field_term= $3;
          }
unknown's avatar
unknown committed
6376
	| OPTIONALLY ENCLOSED BY text_string
unknown's avatar
unknown committed
6377
	  {
unknown's avatar
unknown committed
6378
            LEX *lex= Lex;
6379
            DBUG_ASSERT(lex->exchange);
unknown's avatar
unknown committed
6380 6381
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
unknown's avatar
unknown committed
6382
	  }
unknown's avatar
unknown committed
6383
        | ENCLOSED BY text_string
6384 6385 6386 6387
          {
            DBUG_ASSERT(Lex->exchange);
            Lex->exchange->enclosed= $3;
          }
unknown's avatar
unknown committed
6388
        | ESCAPED BY text_string
6389 6390 6391 6392
          {
            DBUG_ASSERT(Lex->exchange);
            Lex->exchange->escaped= $3;
          };
unknown's avatar
unknown committed
6393 6394 6395

opt_line_term:
	/* empty */
unknown's avatar
unknown committed
6396
	| LINES line_term_list;
unknown's avatar
unknown committed
6397 6398 6399

line_term_list:
	line_term_list line_term
unknown's avatar
unknown committed
6400
	| line_term;
unknown's avatar
unknown committed
6401 6402

line_term:
unknown's avatar
unknown committed
6403
        TERMINATED BY text_string
6404 6405 6406 6407
          {
            DBUG_ASSERT(Lex->exchange);
            Lex->exchange->line_term= $3;
          }
unknown's avatar
unknown committed
6408
        | STARTING BY text_string
6409 6410 6411 6412
          {
            DBUG_ASSERT(Lex->exchange);
            Lex->exchange->line_start= $3;
          };
unknown's avatar
unknown committed
6413 6414 6415

opt_ignore_lines:
	/* empty */
unknown's avatar
unknown committed
6416 6417
        | IGNORE_SYM NUM LINES
          {
6418 6419 6420
            DBUG_ASSERT(Lex->exchange);
            Lex->exchange->skip_lines= atol($2.str);
          };
unknown's avatar
unknown committed
6421 6422 6423 6424

/* Common definitions */

text_literal:
6425
	TEXT_STRING_literal
unknown's avatar
unknown committed
6426 6427
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
6428
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
unknown's avatar
unknown committed
6429
	}
unknown's avatar
unknown committed
6430
	| NCHAR_STRING
6431
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
6432
	| UNDERSCORE_CHARSET TEXT_STRING
6433
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
6434
	| text_literal TEXT_STRING_literal
unknown's avatar
unknown committed
6435 6436
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
unknown's avatar
unknown committed
6437 6438

text_string:
6439
	TEXT_STRING_literal
unknown's avatar
unknown committed
6440
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
unknown's avatar
unknown committed
6441 6442
	| HEX_NUM
	  {
6443
	    Item *tmp = new Item_varbinary($1.str,$1.length);
6444
	    /*
unknown's avatar
unknown committed
6445
	      it is OK only emulate fix_fieds, because we need only
6446 6447 6448
              value of constant
	    */
	    $$= tmp ?
unknown's avatar
unknown committed
6449
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6450
	      (String*) 0;
6451 6452 6453
	  }
	;

unknown's avatar
unknown committed
6454
param_marker:
6455
        '?'
unknown's avatar
unknown committed
6456
        {
6457 6458 6459
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
          if (thd->command == COM_PREPARE)
6460
          {
6461 6462 6463 6464
            Item_param *item= new Item_param((uint) (lex->tok_start -
                                                     (uchar *) thd->query));
            if (!($$= item) || lex->param_list.push_back(item))
            {
unknown's avatar
unknown committed
6465
	      my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
6466 6467
	      YYABORT;
            }
unknown's avatar
unknown committed
6468
          }
6469
          else
unknown's avatar
unknown committed
6470
          {
6471
            yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
6472 6473
            YYABORT;
          }
6474 6475 6476
        }
	;

unknown's avatar
unknown committed
6477 6478 6479
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
6480 6481
	| '-' NUM_literal
	  {
6482
	    $2->max_length++;
unknown's avatar
unknown committed
6483
	    $$= $2->neg();
6484
	  }
unknown's avatar
unknown committed
6485 6486 6487
	;


unknown's avatar
unknown committed
6488 6489
literal:
	text_literal	{ $$ =	$1; }
unknown's avatar
unknown committed
6490
	| NUM_literal	{ $$ = $1; }
unknown's avatar
unknown committed
6491
	| NULL_SYM	{ $$ =	new Item_null();
6492
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
6493
	| HEX_NUM	{ $$ =	new Item_varbinary($1.str,$1.length);}
6494
	| UNDERSCORE_CHARSET HEX_NUM
6495
	  {
6496
	    Item *tmp= new Item_varbinary($2.str,$2.length);
6497
	    /*
unknown's avatar
unknown committed
6498
	      it is OK only emulate fix_fieds, because we need only
6499 6500 6501
              value of constant
	    */
	    String *str= tmp ?
unknown's avatar
unknown committed
6502
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6503
	      (String*) 0;
unknown's avatar
unknown committed
6504 6505 6506
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
6507
	  }
unknown's avatar
unknown committed
6508 6509
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
unknown's avatar
unknown committed
6510
	| TIMESTAMP text_literal { $$ = $2; };
unknown's avatar
unknown committed
6511

6512
NUM_literal:
6513 6514
	NUM		{ int error; $$ = new Item_int($1.str, (longlong) my_strtoll10($1.str, NULL, &error), $1.length); }
	| LONG_NUM	{ int error; $$ = new Item_int($1.str, (longlong) my_strtoll10($1.str, NULL, &error), $1.length); }
6515
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531
	| REAL_NUM
	{
	   $$= new Item_real($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
6532 6533
	;
	
unknown's avatar
unknown committed
6534 6535 6536 6537 6538
/**********************************************************************
** Createing different items.
**********************************************************************/

insert_ident:
6539
	simple_ident_nospvar { $$=$1; }
unknown's avatar
unknown committed
6540
	| table_wild	 { $$=$1; };
unknown's avatar
unknown committed
6541 6542

table_wild:
unknown's avatar
unknown committed
6543
	ident '.' '*'
6544 6545
	{
	  $$ = new Item_field(NullS,$1.str,"*");
unknown's avatar
unknown committed
6546
	  Lex->current_select->with_wild++;
6547
	}
unknown's avatar
unknown committed
6548
	| ident '.' ident '.' '*'
6549 6550 6551 6552
	{
	  $$ = new Item_field((YYTHD->client_capabilities &
   			     CLIENT_NO_SCHEMA ? NullS : $1.str),
			     $3.str,"*");
unknown's avatar
unknown committed
6553
	  Lex->current_select->with_wild++;
6554 6555
	}
	;
unknown's avatar
unknown committed
6556 6557

order_ident:
unknown's avatar
unknown committed
6558
	expr { $$=$1; };
unknown's avatar
unknown committed
6559 6560

simple_ident:
6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585
	ident
	{
	  sp_pvar_t *spv;
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;

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

simple_ident_nospvar:
unknown's avatar
unknown committed
6586
	ident
unknown's avatar
unknown committed
6587
	{
unknown's avatar
unknown committed
6588
	  SELECT_LEX *sel=Select;
6589
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
6590 6591
	       sel->get_in_sum_expr() > 0) ?
              (Item*) new Item_field(NullS,NullS,$1.str) :
6592
	      (Item*) new Item_ref(NullS,NullS,$1.str);
unknown's avatar
unknown committed
6593
	}
6594 6595 6596 6597 6598
	| simple_ident_q { $$= $1; }
	;	

simple_ident_q:
	ident '.' ident
unknown's avatar
unknown committed
6599
	{
unknown's avatar
unknown committed
6600
	  THD *thd= YYTHD;
6601
	  LEX *lex= thd->lex;
6602 6603 6604 6605 6606 6607 6608 6609 6610 6611
         
          /*
            FIXME This will work ok in simple_ident_nospvar case because
            we can't meet simple_ident_nospvar in trigger now. But it
            should be changed in future.
          */
          if (lex->sphead && lex->sphead->m_type == TYPE_ENUM_TRIGGER &&
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") || 
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
6612
            Item_trigger_field *trg_fld;
6613 6614 6615 6616 6617
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
            
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
unknown's avatar
unknown committed
6618
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
6619 6620 6621 6622 6623 6624
              YYABORT;
            }
            
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
unknown's avatar
unknown committed
6625
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
6626 6627 6628
              YYABORT;
            }
            
6629 6630 6631 6632
            if (!(trg_fld= new Item_trigger_field(new_row ?
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
                                                  $3.str)))
6633
              YYABORT;
6634 6635 6636 6637 6638 6639 6640
          
            /*
              Let us add this item to list of all Item_trigger_field objects
              in trigger.
            */
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
              (byte**)&trg_fld->next_trg_field);
6641 6642 6643 6644 6645 6646 6647 6648
            
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
6649 6650
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
6651 6652 6653 6654 6655 6656 6657
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
	        (Item*) new Item_field(NullS,$1.str,$3.str) :
	        (Item*) new Item_ref(NullS,$1.str,$3.str);
          }
        }
unknown's avatar
unknown committed
6658
	| '.' ident '.' ident
unknown's avatar
unknown committed
6659
	{
unknown's avatar
unknown committed
6660
	  THD *thd= YYTHD;
6661
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
6662
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
6663 6664
	  if (sel->no_table_names_allowed)
	  {
6665 6666
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $2.str, thd->where);
unknown's avatar
unknown committed
6667
	  }
6668
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
6669 6670
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field(NullS,$2.str,$4.str) :
6671
              (Item*) new Item_ref(NullS, $2.str, $4.str);
unknown's avatar
unknown committed
6672
	}
unknown's avatar
unknown committed
6673
	| ident '.' ident '.' ident
unknown's avatar
unknown committed
6674
	{
unknown's avatar
unknown committed
6675
	  THD *thd= YYTHD;
6676
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
6677
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
6678 6679
	  if (sel->no_table_names_allowed)
	  {
6680 6681
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $3.str, thd->where);
unknown's avatar
unknown committed
6682
	  }
6683
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
6684 6685 6686 6687
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field((YYTHD->client_capabilities &
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
6688 6689
	      (Item*) new Item_ref((YYTHD->client_capabilities &
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
unknown's avatar
unknown committed
6690
                                   $3.str, $5.str);
unknown's avatar
unknown committed
6691
	};
unknown's avatar
unknown committed
6692 6693 6694 6695


field_ident:
	ident			{ $$=$1;}
6696
	| ident '.' ident	{ $$=$3;}	/* Skip schema name in create*/
unknown's avatar
unknown committed
6697
	| '.' ident		{ $$=$2;}	/* For Delphi */;
unknown's avatar
unknown committed
6698 6699 6700

table_ident:
	ident			{ $$=new Table_ident($1); }
unknown's avatar
unknown committed
6701
	| ident '.' ident	{ $$=new Table_ident(YYTHD, $1,$3,0);}
6702 6703 6704
	| '.' ident		{ $$=new Table_ident($2);} /* For Delphi */
        ;

unknown's avatar
unknown committed
6705
table_ident_nodb:
unknown's avatar
unknown committed
6706
	ident			{ LEX_STRING db={(char*) any_db,3}; $$=new Table_ident(YYTHD, db,$1,0); }
6707
        ;
unknown's avatar
unknown committed
6708

unknown's avatar
unknown committed
6709
IDENT_sys:
6710 6711 6712 6713 6714
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
6715 6716 6717 6718 6719 6720 6721
            {
              CHARSET_INFO *cs= system_charset_info;
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
                                                   $1.length);
              if (wlen < $1.length)
              {
6722 6723
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
6724 6725
                YYABORT;
              }
6726
	      $$= $1;
6727
            }
6728 6729 6730 6731
	    else
	      thd->convert_string(&$$, system_charset_info,
				  $1.str, $1.length, thd->charset());
	  }
unknown's avatar
unknown committed
6732 6733 6734 6735 6736 6737
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
6738 6739
	  if (thd->charset_is_system_charset)
	    $$= $1;
unknown's avatar
unknown committed
6740
	  else
unknown's avatar
unknown committed
6741 6742
	    thd->convert_string(&$$, system_charset_info,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
6743 6744 6745
	}
	;

6746
TEXT_STRING_literal:
unknown's avatar
unknown committed
6747 6748 6749
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
6750 6751
	  if (thd->charset_is_collation_connection)
	    $$= $1;
unknown's avatar
unknown committed
6752
	  else
unknown's avatar
unknown committed
6753 6754
	    thd->convert_string(&$$, thd->variables.collation_connection,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
6755 6756 6757 6758
	}
	;


unknown's avatar
unknown committed
6759
ident:
unknown's avatar
unknown committed
6760
	IDENT_sys	    { $$=$1; }
unknown's avatar
unknown committed
6761 6762
	| keyword
	{
unknown's avatar
unknown committed
6763 6764 6765
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
6766 6767
	}
	;
unknown's avatar
unknown committed
6768 6769

ident_or_text:
unknown's avatar
unknown committed
6770 6771 6772
	ident 			{ $$=$1;}
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
unknown's avatar
unknown committed
6773 6774 6775 6776

user:
	ident_or_text
	{
unknown's avatar
unknown committed
6777 6778
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
6779
	    YYABORT;
6780 6781 6782 6783
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
unknown's avatar
unknown committed
6784 6785
	| ident_or_text '@' ident_or_text
	  {
unknown's avatar
unknown committed
6786 6787
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
6788 6789
	      YYABORT;
	    $$->user = $1; $$->host=$3;
6790
	  }
unknown's avatar
unknown committed
6791
	| CURRENT_USER optional_braces
6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808
	{
          THD *thd= YYTHD;
          if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
            YYABORT;
          $$->user.str= thd->priv_user;
          $$->user.length= strlen(thd->priv_user);
          if (*thd->priv_host != 0)
          {
            $$->host.str= thd->priv_host;
            $$->host.length= strlen(thd->priv_host);
          }
          else
          {
            $$->host.str= (char *) "%";
            $$->host.length= 1;
          }
	};
unknown's avatar
unknown committed
6809 6810 6811 6812 6813

/* Keyword that we allow for identifiers */

keyword:
	ACTION			{}
unknown's avatar
unknown committed
6814
	| ADDDATE_SYM		{}
unknown's avatar
unknown committed
6815
	| AFTER_SYM		{}
6816
	| AGAINST		{}
unknown's avatar
unknown committed
6817
	| AGGREGATE_SYM		{}
6818
	| ALGORITHM_SYM		{}
unknown's avatar
unknown committed
6819
	| ANY_SYM		{}
unknown's avatar
unknown committed
6820
	| ASCII_SYM		{}
unknown's avatar
unknown committed
6821
	| AUTO_INC		{}
unknown's avatar
unknown committed
6822 6823
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
unknown's avatar
unknown committed
6824
	| BACKUP_SYM		{}
unknown's avatar
unknown committed
6825
	| BEGIN_SYM		{}
unknown's avatar
unknown committed
6826
	| BERKELEY_DB_SYM	{}
unknown's avatar
unknown committed
6827
	| BINLOG_SYM		{}
unknown's avatar
unknown committed
6828 6829
	| BIT_SYM		{}
	| BOOL_SYM		{}
unknown's avatar
unknown committed
6830
	| BOOLEAN_SYM		{}
6831
	| BYTE_SYM		{}
unknown's avatar
unknown committed
6832
	| BTREE_SYM		{}
unknown's avatar
unknown committed
6833
	| CACHE_SYM		{}
6834
	| CASCADED              {}
6835
	| CHANGED		{}
6836
	| CHARSET		{}
unknown's avatar
unknown committed
6837
	| CHECKSUM_SYM		{}
6838
	| CIPHER_SYM		{}
unknown's avatar
unknown committed
6839
	| CLIENT_SYM		{}
6840
	| CLOSE_SYM		{}
unknown's avatar
unknown committed
6841
	| COLLATION_SYM		{}
unknown's avatar
unknown committed
6842
	| COMMENT_SYM		{}
unknown's avatar
unknown committed
6843
	| COMMITTED_SYM		{}
unknown's avatar
unknown committed
6844
	| COMMIT_SYM		{}
unknown's avatar
unknown committed
6845
	| COMPRESSED_SYM	{}
unknown's avatar
unknown committed
6846
	| CONCURRENT		{}
6847
	| CONSISTENT_SYM	{}
6848
	| CONTAINS_SYM          {}
6849
	| CUBE_SYM		{}
unknown's avatar
unknown committed
6850 6851 6852 6853
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
unknown's avatar
unknown committed
6854
        | DEALLOCATE_SYM        {}
6855
	| DEFINER_SYM		{}
unknown's avatar
unknown committed
6856
	| DELAY_KEY_WRITE_SYM	{}
unknown's avatar
unknown committed
6857 6858
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
unknown's avatar
unknown committed
6859
	| DISCARD		{}
unknown's avatar
unknown committed
6860
	| DO_SYM		{}
unknown's avatar
unknown committed
6861 6862
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
unknown's avatar
unknown committed
6863 6864 6865
	| DYNAMIC_SYM		{}
	| END			{}
	| ENUM			{}
unknown's avatar
unknown committed
6866
	| ENGINE_SYM		{}
unknown's avatar
unknown committed
6867
	| ENGINES_SYM		{}
6868
	| ERRORS		{}
unknown's avatar
unknown committed
6869
	| ESCAPE_SYM		{}
unknown's avatar
unknown committed
6870
	| EVENTS_SYM		{}
unknown's avatar
unknown committed
6871
	| EXECUTE_SYM		{}
6872
        | EXPANSION_SYM         {}
unknown's avatar
unknown committed
6873
	| EXTENDED_SYM		{}
6874
	| FAST_SYM		{}
6875
	| FOUND_SYM		{}
unknown's avatar
unknown committed
6876 6877
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
unknown's avatar
unknown committed
6878
	| FULL			{}
unknown's avatar
unknown committed
6879 6880 6881 6882
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
	| FLUSH_SYM		{}
6883
	| FRAC_SECOND_SYM	{}
6884
	| GEOMETRY_SYM		{}
6885
	| GEOMETRYCOLLECTION	{}
6886
	| GET_FORMAT		{}
unknown's avatar
unknown committed
6887
	| GRANTS		{}
unknown's avatar
unknown committed
6888
	| GLOBAL_SYM		{}
6889
	| HANDLER_SYM		{}
unknown's avatar
unknown committed
6890
	| HASH_SYM		{}
6891
	| HELP_SYM		{}
unknown's avatar
unknown committed
6892 6893 6894
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
6895
	| INVOKER_SYM		{}
unknown's avatar
unknown committed
6896
	| IMPORT		{}
unknown's avatar
unknown committed
6897
	| INDEXES		{}
unknown's avatar
unknown committed
6898
	| ISOLATION		{}
unknown's avatar
unknown committed
6899
	| ISSUER_SYM		{}
unknown's avatar
unknown committed
6900
	| INNOBASE_SYM		{}
6901
	| INSERT_METHOD		{}
unknown's avatar
unknown committed
6902
	| RELAY_THREAD		{}
6903 6904
	| LABEL_SYM             {}
	| LANGUAGE_SYM          {}
6905
	| LAST_SYM		{}
unknown's avatar
unknown committed
6906
	| LEAVES                {}
unknown's avatar
unknown committed
6907
	| LEVEL_SYM		{}
6908
	| LINESTRING		{}
unknown's avatar
unknown committed
6909
	| LOCAL_SYM		{}
unknown's avatar
unknown committed
6910
	| LOCKS_SYM		{}
unknown's avatar
unknown committed
6911 6912 6913 6914 6915 6916 6917 6918 6919
	| LOGS_SYM		{}
	| MAX_ROWS		{}
	| MASTER_SYM		{}
	| MASTER_HOST_SYM	{}
	| MASTER_PORT_SYM	{}
	| MASTER_LOG_FILE_SYM	{}
	| MASTER_LOG_POS_SYM	{}
	| MASTER_USER_SYM	{}
	| MASTER_PASSWORD_SYM	{}
unknown's avatar
unknown committed
6920
	| MASTER_SERVER_ID_SYM  {}
unknown's avatar
unknown committed
6921
	| MASTER_CONNECT_RETRY_SYM	{}
unknown's avatar
unknown committed
6922 6923 6924 6925 6926 6927
	| 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
6928 6929 6930
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
6931
	| MEDIUM_SYM		{}
6932
	| MERGE_SYM		{}
unknown's avatar
unknown committed
6933
	| MICROSECOND_SYM	{}
unknown's avatar
unknown committed
6934 6935 6936
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
unknown's avatar
unknown committed
6937
	| MODE_SYM		{}
unknown's avatar
unknown committed
6938
	| MONTH_SYM		{}
6939 6940 6941
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
6942
	| NAME_SYM              {}
unknown's avatar
unknown committed
6943
	| NAMES_SYM		{}
unknown's avatar
unknown committed
6944 6945
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
unknown's avatar
unknown committed
6946
	| NDBCLUSTER_SYM	{}
6947
	| NEXT_SYM		{}
unknown's avatar
unknown committed
6948
	| NEW_SYM		{}
unknown's avatar
unknown committed
6949
	| NO_SYM		{}
6950
	| NONE_SYM		{}
unknown's avatar
unknown committed
6951
	| NVARCHAR_SYM		{}
unknown's avatar
unknown committed
6952
	| OFFSET_SYM		{}
unknown's avatar
unknown committed
6953
	| OLD_PASSWORD		{}
6954
	| ONE_SHOT_SYM		{}
6955
	| OPEN_SYM		{}
unknown's avatar
unknown committed
6956
	| PACK_KEYS_SYM		{}
6957
	| PARTIAL		{}
unknown's avatar
unknown committed
6958
	| PASSWORD		{}
6959
	| POINT_SYM		{}
6960
	| POLYGON		{}
unknown's avatar
unknown committed
6961
        | PREPARE_SYM           {}
6962
	| PREV_SYM		{}
unknown's avatar
unknown committed
6963 6964
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
6965
	| QUARTER_SYM		{}
unknown's avatar
unknown committed
6966
	| QUERY_SYM		{}
6967
	| QUICK			{}
unknown's avatar
unknown committed
6968
	| RAID_0_SYM		{}
unknown's avatar
unknown committed
6969 6970
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
unknown's avatar
unknown committed
6971
	| RAID_STRIPED_SYM	{}
unknown's avatar
unknown committed
6972
	| RAID_TYPE		{}
unknown's avatar
unknown committed
6973 6974
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
unknown's avatar
unknown committed
6975 6976
	| RELOAD		{}
	| REPAIR		{}
unknown's avatar
unknown committed
6977
	| REPEATABLE_SYM	{}
unknown's avatar
unknown committed
6978
	| REPLICATION		{}
unknown's avatar
unknown committed
6979
	| RESET_SYM		{}
6980
	| RESOURCES		{}
unknown's avatar
unknown committed
6981
	| RESTORE_SYM		{}
6982
	| RETURNS_SYM           {}
unknown's avatar
unknown committed
6983
	| ROLLBACK_SYM		{}
6984
	| ROLLUP_SYM		{}
unknown's avatar
unknown committed
6985 6986 6987
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
unknown's avatar
unknown committed
6988
	| RTREE_SYM		{}
6989
	| SAVEPOINT_SYM		{}
unknown's avatar
unknown committed
6990
	| SECOND_SYM		{}
6991
	| SECURITY_SYM		{}
6992
	| SERIAL_SYM		{}
unknown's avatar
unknown committed
6993 6994
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
unknown's avatar
unknown committed
6995
	| SIGNED_SYM		{}
6996
	| SIMPLE_SYM		{}
unknown's avatar
unknown committed
6997
	| SHARE_SYM		{}
unknown's avatar
unknown committed
6998
	| SHUTDOWN		{}
unknown's avatar
unknown committed
6999
	| SLAVE			{}
7000
	| SNAPSHOT_SYM		{}
unknown's avatar
unknown committed
7001
	| SOUNDS_SYM		{}
unknown's avatar
unknown committed
7002
	| SQL_CACHE_SYM		{}
unknown's avatar
unknown committed
7003
	| SQL_BUFFER_RESULT	{}
unknown's avatar
unknown committed
7004
	| SQL_NO_CACHE_SYM	{}
unknown's avatar
unknown committed
7005
	| SQL_THREAD		{}
unknown's avatar
unknown committed
7006 7007 7008
	| START_SYM		{}
	| STATUS_SYM		{}
	| STOP_SYM		{}
unknown's avatar
unknown committed
7009
	| STORAGE_SYM		{}
unknown's avatar
unknown committed
7010
	| STRING_SYM		{}
unknown's avatar
unknown committed
7011
	| SUBDATE_SYM		{}
unknown's avatar
unknown committed
7012
	| SUBJECT_SYM		{}
unknown's avatar
unknown committed
7013
	| SUPER_SYM		{}
unknown's avatar
unknown committed
7014
	| TABLESPACE		{}
unknown's avatar
unknown committed
7015
	| TEMPORARY		{}
7016
	| TEMPTABLE_SYM		{}
unknown's avatar
unknown committed
7017
	| TEXT_SYM		{}
unknown's avatar
unknown committed
7018
	| TRANSACTION_SYM	{}
7019
	| TRUNCATE_SYM		{}
unknown's avatar
unknown committed
7020
	| TIMESTAMP		{}
7021 7022
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
unknown's avatar
unknown committed
7023 7024
	| TIME_SYM		{}
	| TYPE_SYM		{}
unknown's avatar
unknown committed
7025
	| TYPES_SYM		{}
unknown's avatar
unknown committed
7026
        | UDF_RETURNS_SYM       {}
7027
	| FUNCTION_SYM		{}
unknown's avatar
unknown committed
7028
	| UNCOMMITTED_SYM	{}
7029
	| UNDEFINED_SYM		{}
unknown's avatar
unknown committed
7030
	| UNICODE_SYM		{}
7031
	| UNKNOWN_SYM		{}
7032
	| UNTIL_SYM		{}
7033
	| USER			{}
unknown's avatar
unknown committed
7034
	| USE_FRM		{}
unknown's avatar
unknown committed
7035
	| VARIABLES		{}
7036
	| VIEW_SYM		{}
7037
	| VALUE_SYM		{}
7038
	| WARNINGS		{}
7039
	| WEEK_SYM		{}
unknown's avatar
unknown committed
7040
	| WORK_SYM		{}
7041
	| X509_SYM		{}
unknown's avatar
unknown committed
7042 7043
	| YEAR_SYM		{}
	;
unknown's avatar
unknown committed
7044 7045 7046 7047 7048 7049

/* Option functions */

set:
	SET opt_option
	{
unknown's avatar
unknown committed
7050 7051
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
7052
	  lex->option_type=OPT_SESSION;
unknown's avatar
unknown committed
7053
	  lex->var_list.empty();
7054
          lex->one_shot_set= 0;
unknown's avatar
unknown committed
7055
	}
7056 7057 7058
	option_value_list
	{}
	;
unknown's avatar
unknown committed
7059 7060 7061

opt_option:
	/* empty */ {}
unknown's avatar
unknown committed
7062
	| OPTION {};
unknown's avatar
unknown committed
7063 7064

option_value_list:
unknown's avatar
unknown committed
7065 7066 7067 7068 7069 7070 7071 7072
	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; }
7073
	| ONE_SHOT_SYM	{ Lex->option_type= OPT_SESSION; Lex->one_shot_set= 1; }
unknown's avatar
unknown committed
7074 7075 7076 7077
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
unknown's avatar
unknown committed
7078
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
7079 7080 7081 7082 7083 7084
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

opt_var_ident_type:
	/* empty */		{ $$=OPT_DEFAULT; }
unknown's avatar
unknown committed
7085
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
7086 7087 7088
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
unknown's avatar
unknown committed
7089 7090

option_value:
7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106
	  '@' ident_or_text equal expr
	  {
            LEX *lex= Lex;

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

                We also do not want to allow expression with subselects in
                this case.
              */
              if (lex->query_tables)
              {
unknown's avatar
unknown committed
7107 7108
                my_message(ER_SP_SUBSELECT_NYI, ER(ER_SP_SUBSELECT_NYI),
                           MYF(0));
7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119
                YYABORT;
              }
              sp_instr_set_user_var *i= 
                new sp_instr_set_user_var(lex->sphead->instructions(),
                                          lex->spcont, $2, $4);
	      lex->sphead->add_instr(i);
            }
            else
              lex->var_list.push_back(new set_var_user(new Item_func_set_user_var($2,$4)));
              
	  }
unknown's avatar
unknown committed
7120
	| internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
7121
	  {
unknown's avatar
unknown committed
7122
	    LEX *lex=Lex;
7123 7124 7125 7126 7127 7128 7129 7130

            if ($1.var == &trg_new_row_fake_var)
            {
              /* We are in trigger and assigning value to field of new row */
              Item *it;
              sp_instr_set_trigger_field *i;
              if (lex->query_tables)
              {
unknown's avatar
unknown committed
7131 7132
                my_message(ER_SP_SUBSELECT_NYI, ER(ER_SP_SUBSELECT_NYI),
                           MYF(0));
7133 7134 7135 7136 7137 7138 7139 7140 7141
                YYABORT;
              }
              if ($3)
                it= $3;
              else
              {
                /* QQ: Shouldn't this be field's default value ? */
                it= new Item_null();
              }
7142 7143 7144 7145
              
              if (!(i= new sp_instr_set_trigger_field(
                             lex->sphead->instructions(), lex->spcont,
                             $1.base_name, it)))
7146
                YYABORT;
7147 7148 7149 7150 7151 7152 7153 7154
              
              /*
                Let us add this item to list of all Item_trigger_field
                objects in trigger.
              */
              lex->trg_table_fields.link_in_list((byte *)&i->trigger_field,
                     (byte **)&i->trigger_field.next_trg_field);

7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184
              lex->sphead->add_instr(i);
            }
            else if ($1.var)
	    { /* System variable */
	      lex->var_list.push_back(new set_var(lex->option_type, $1.var,
						  &$1.base_name, $3));
	    }
            else
	    {
	      /* An SP local variable */
	      sp_pcontext *ctx= lex->spcont;
	      sp_pvar_t *spv;
              sp_instr_set *i;
	      Item *it;

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

	      if ($3)
	        it= $3;
	      else if (spv->dflt)
	        it= spv->dflt;
	      else
	        it= new Item_null();
              i= new sp_instr_set(lex->sphead->instructions(), ctx,
	                          spv->offset, it, spv->type);
	      i->tables= lex->query_tables;
	      lex->query_tables= 0;
	      lex->sphead->add_instr(i);
	      spv->isset= TRUE;
	    }
unknown's avatar
unknown committed
7185
	  }
unknown's avatar
unknown committed
7186
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
7187 7188
	  {
	    LEX *lex=Lex;
unknown's avatar
unknown committed
7189 7190
	    lex->var_list.push_back(new set_var((enum_var_type) $3, $4.var,
						&$4.base_name, $6));
unknown's avatar
unknown committed
7191 7192 7193 7194
	  }
	| TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	  {
	    LEX *lex=Lex;
unknown's avatar
unknown committed
7195 7196 7197
	    LEX_STRING tmp;
	    tmp.str=0;
	    tmp.length=0;
unknown's avatar
unknown committed
7198
	    lex->var_list.push_back(new set_var(lex->option_type,
unknown's avatar
unknown committed
7199
						find_sys_var("tx_isolation"),
unknown's avatar
unknown committed
7200
						&tmp,
unknown's avatar
unknown committed
7201
						new Item_int((int32) $4)));
unknown's avatar
unknown committed
7202
	  }
7203
	| charset old_or_new_charset_name_or_default
unknown's avatar
unknown committed
7204
	{
unknown's avatar
unknown committed
7205
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7206
	  LEX *lex= Lex;
7207
	  $2= $2 ? $2: global_system_variables.character_set_client;
7208
	  lex->var_list.push_back(new set_var_collation_client($2,thd->variables.collation_database,$2));
unknown's avatar
unknown committed
7209
	}
7210
	| NAMES_SYM charset_name_or_default opt_collate
7211
	{
unknown's avatar
unknown committed
7212
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7213
	  LEX *lex= Lex;
7214
	  $2= $2 ? $2 : global_system_variables.character_set_client;
7215 7216
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
7217
	  {
7218 7219
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
unknown's avatar
unknown committed
7220
	    YYABORT;
7221
	  }
7222
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
7223
	}
unknown's avatar
unknown committed
7224
	| PASSWORD equal text_or_password
unknown's avatar
unknown committed
7225
	  {
7226
	    THD *thd=YYTHD;
unknown's avatar
unknown committed
7227
	    LEX_USER *user;
unknown's avatar
unknown committed
7228
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
unknown's avatar
unknown committed
7229 7230 7231
	      YYABORT;
	    user->host.str=0;
	    user->user.str=thd->priv_user;
7232
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
unknown's avatar
unknown committed
7233 7234
	  }
	| PASSWORD FOR_SYM user equal text_or_password
unknown's avatar
unknown committed
7235
	  {
unknown's avatar
unknown committed
7236 7237
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
7238
	;
unknown's avatar
unknown committed
7239

unknown's avatar
unknown committed
7240 7241 7242
internal_variable_name:
	ident
	{
7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269
	  LEX *lex= Lex;
          sp_pcontext *spc= lex->spcont;
	  sp_pvar_t *spv;

	  /* We have to lookup here since local vars can shadow sysvars */
	  if (!spc || !(spv = spc->find_pvar(&$1)))
	  {
            /* Not an SP local variable */
	    sys_var *tmp=find_sys_var($1.str, $1.length);
	    if (!tmp)
	      YYABORT;
	    $$.var= tmp;
	    $$.base_name.str=0;
	    $$.base_name.length=0;
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
            if (tmp == &sys_time_zone)
	      Lex->time_zone_tables_used= &fake_time_zone_tables_list;
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
unknown's avatar
unknown committed
7270
	}
7271 7272
	| ident '.' ident
	  {
7273
            LEX *lex= Lex;
7274 7275
            if (check_reserved_words(&$1))
            {
7276
	      yyerror(ER(ER_SYNTAX_ERROR));
7277 7278
              YYABORT;
            }
7279 7280 7281 7282 7283 7284
            if (lex->sphead && lex->sphead->m_type == TYPE_ENUM_TRIGGER &&
                (!my_strcasecmp(system_charset_info, $1.str, "NEW") || 
                 !my_strcasecmp(system_charset_info, $1.str, "OLD")))
            {
              if ($1.str[0]=='O' || $1.str[0]=='o')
              {
unknown's avatar
unknown committed
7285
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
7286 7287 7288 7289
                YYABORT;
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
unknown's avatar
unknown committed
7290 7291
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
7292 7293 7294 7295
                YYABORT;
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
unknown's avatar
unknown committed
7296
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308
                YYABORT;
              }
              /* This special combination will denote field of NEW row */
              $$.var= &trg_new_row_fake_var;
              $$.base_name= $3;
            }
            else
            {
              sys_var *tmp=find_sys_var($3.str, $3.length);
              if (!tmp)
                YYABORT;
              if (!tmp->is_struct())
7309
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
7310 7311 7312
              $$.var= tmp;
              $$.base_name= $1;
            }
7313 7314 7315 7316 7317 7318 7319
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
7320
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
unknown's avatar
unknown committed
7321 7322 7323
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
7324
	  }
7325
        ;
unknown's avatar
unknown committed
7326 7327 7328 7329 7330 7331 7332

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

unknown's avatar
unknown committed
7334 7335 7336 7337
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
7338
	    $$= $3.length ? YYTHD->variables.old_passwords ?
unknown's avatar
unknown committed
7339 7340 7341 7342 7343 7344 7345 7346
	        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;
7347 7348
	  }
          ;
unknown's avatar
unknown committed
7349

unknown's avatar
unknown committed
7350

unknown's avatar
unknown committed
7351
set_expr_or_default:
unknown's avatar
unknown committed
7352 7353
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
7354 7355
	| ON	  { $$=new Item_string("ON",  2, system_charset_info); }
	| ALL	  { $$=new Item_string("ALL", 3, system_charset_info); }
unknown's avatar
unknown committed
7356
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
unknown's avatar
unknown committed
7357
	;
unknown's avatar
unknown committed
7358 7359


unknown's avatar
unknown committed
7360 7361 7362 7363 7364 7365 7366
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
	  Lex->sql_command=SQLCOM_LOCK_TABLES;
	}
7367 7368 7369
	table_lock_list
	{}
	;
unknown's avatar
unknown committed
7370 7371 7372

table_or_tables:
	TABLE_SYM
unknown's avatar
unknown committed
7373
	| TABLES;
unknown's avatar
unknown committed
7374 7375 7376

table_lock_list:
	table_lock
unknown's avatar
unknown committed
7377
	| table_lock_list ',' table_lock;
unknown's avatar
unknown committed
7378 7379 7380

table_lock:
	table_ident opt_table_alias lock_option
7381
	{
unknown's avatar
unknown committed
7382
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
7383 7384
	   YYABORT;
	}
7385
        ;
unknown's avatar
unknown committed
7386 7387 7388

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
7389
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
unknown's avatar
unknown committed
7390
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
7391 7392
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
unknown's avatar
unknown committed
7393 7394

unlock:
7395 7396
	UNLOCK_SYM table_or_tables { Lex->sql_command=SQLCOM_UNLOCK_TABLES; }
        ;
unknown's avatar
unknown committed
7397 7398


7399
/*
unknown's avatar
unknown committed
7400
** Handler: direct access to ISAM functions
7401 7402 7403 7404 7405
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
7406 7407
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_OPEN;
unknown's avatar
unknown committed
7408
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
7409 7410
	    YYABORT;
	}
unknown's avatar
unknown committed
7411
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
7412
	{
7413 7414
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_CLOSE;
unknown's avatar
unknown committed
7415
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
7416 7417
	    YYABORT;
	}
unknown's avatar
unknown committed
7418
	| HANDLER_SYM table_ident_nodb READ_SYM
7419
	{
unknown's avatar
unknown committed
7420 7421 7422
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
7423 7424
	  lex->current_select->select_limit= 1;
	  lex->current_select->offset_limit= 0L;
unknown's avatar
unknown committed
7425
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
7426
	    YYABORT;
unknown's avatar
unknown committed
7427
        }
7428
        handler_read_or_scan where_clause opt_limit_clause {}
7429
        ;
7430

unknown's avatar
unknown committed
7431 7432
handler_read_or_scan:
	handler_scan_function         { Lex->backup_dir= 0; }
7433 7434
        | ident handler_rkey_function { Lex->backup_dir= $1.str; }
        ;
unknown's avatar
unknown committed
7435 7436 7437

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
7438 7439
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
unknown's avatar
unknown committed
7440 7441 7442 7443 7444 7445

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;  }
7446 7447
	| handler_rkey_mode
	{
unknown's avatar
unknown committed
7448 7449
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
unknown's avatar
unknown committed
7450
	  lex->ha_rkey_mode=$1;
unknown's avatar
unknown committed
7451
	  if (!(lex->insert_list = new List_item))
7452
	    YYABORT;
7453 7454
	} '(' values ')' { }
        ;
7455 7456

handler_rkey_mode:
unknown's avatar
unknown committed
7457 7458 7459 7460
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
7461 7462
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
7463

unknown's avatar
unknown committed
7464 7465 7466 7467 7468
/* GRANT / REVOKE */

revoke:
	REVOKE
	{
unknown's avatar
unknown committed
7469 7470 7471 7472 7473
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_REVOKE;
	  lex->users_list.empty();
	  lex->columns.empty();
	  lex->grant= lex->grant_tot_col=0;
7474
	  lex->select_lex.db=0;
7475 7476
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
7477
	  bzero((char*) &lex->mqh, sizeof(lex->mqh));
unknown's avatar
unknown committed
7478
	}
7479 7480 7481 7482 7483
	revoke_command
	{}
        ;

revoke_command:
7484 7485
	grant_privileges ON opt_table FROM user_list
	{}
7486
	|
unknown's avatar
unknown committed
7487
	ALL opt_privileges ',' GRANT OPTION FROM user_list
7488 7489 7490
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
7491
	;
unknown's avatar
unknown committed
7492 7493 7494 7495

grant:
	GRANT
	{
unknown's avatar
unknown committed
7496 7497 7498
	  LEX *lex=Lex;
	  lex->users_list.empty();
	  lex->columns.empty();
7499 7500
	  lex->sql_command = SQLCOM_GRANT;
	  lex->grant= lex->grant_tot_col= 0;
7501
	  lex->select_lex.db= 0;
7502 7503
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
7504
	  bzero((char *)&(lex->mqh),sizeof(lex->mqh));
unknown's avatar
unknown committed
7505 7506
	}
	grant_privileges ON opt_table TO_SYM user_list
7507 7508 7509
	require_clause grant_options
	{}
	;
unknown's avatar
unknown committed
7510 7511 7512

grant_privileges:
	grant_privilege_list {}
unknown's avatar
unknown committed
7513
	| ALL opt_privileges	{ Lex->grant = GLOBAL_ACLS;}
7514
        ;
unknown's avatar
unknown committed
7515

unknown's avatar
unknown committed
7516 7517 7518 7519 7520
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

unknown's avatar
unknown committed
7521 7522
grant_privilege_list:
	grant_privilege
unknown's avatar
unknown committed
7523
	| grant_privilege_list ',' grant_privilege;
unknown's avatar
unknown committed
7524 7525

grant_privilege:
unknown's avatar
unknown committed
7526
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
7527 7528 7529
	| 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
7530 7531
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
unknown's avatar
unknown committed
7532
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
unknown's avatar
unknown committed
7533 7534 7535
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
unknown's avatar
unknown committed
7536
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
unknown's avatar
unknown committed
7537 7538 7539 7540
	| 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
7541 7542 7543 7544 7545
	| 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; }
7546 7547 7548 7549
	| REPLICATION SLAVE  { Lex->grant |= REPL_SLAVE_ACL; }
	| REPLICATION CLIENT_SYM { Lex->grant |= REPL_CLIENT_ACL; }
	| CREATE VIEW_SYM { Lex->grant |= CREATE_VIEW_ACL; }
	| SHOW VIEW_SYM { Lex->grant |= SHOW_VIEW_ACL; }
unknown's avatar
unknown committed
7550
	;
unknown's avatar
unknown committed
7551

unknown's avatar
unknown committed
7552

7553 7554
opt_and:
	/* empty */	{}
unknown's avatar
unknown committed
7555
	| AND_SYM	{}
7556 7557 7558 7559 7560 7561 7562 7563 7564
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
unknown's avatar
unknown committed
7565 7566 7567 7568
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
unknown's avatar
unknown committed
7569
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
unknown's avatar
unknown committed
7570 7571 7572 7573 7574 7575 7576 7577 7578
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
unknown's avatar
unknown committed
7579
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
unknown's avatar
unknown committed
7580 7581 7582 7583 7584 7585 7586 7587 7588
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
unknown's avatar
unknown committed
7589
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
unknown's avatar
unknown committed
7590 7591 7592 7593 7594
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
7595

unknown's avatar
unknown committed
7596 7597 7598
opt_table:
	'*'
	  {
7599
	    LEX *lex= Lex;
unknown's avatar
unknown committed
7600
	    lex->current_select->db= lex->thd->db;
unknown's avatar
unknown committed
7601
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
7602 7603
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
7604
	    {
unknown's avatar
unknown committed
7605 7606
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
7607
	      YYABORT;
unknown's avatar
unknown committed
7608
	    }
unknown's avatar
unknown committed
7609 7610 7611
	  }
	| ident '.' '*'
	  {
7612
	    LEX *lex= Lex;
unknown's avatar
unknown committed
7613
	    lex->current_select->db = $1.str;
unknown's avatar
unknown committed
7614
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
7615 7616
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
7617
	    {
unknown's avatar
unknown committed
7618 7619
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
7620 7621 7622 7623 7624
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
7625
	    LEX *lex= Lex;
unknown's avatar
unknown committed
7626
	    lex->current_select->db = NULL;
unknown's avatar
unknown committed
7627 7628
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
unknown's avatar
unknown committed
7629
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
7630
	    {
unknown's avatar
unknown committed
7631 7632
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
7633 7634 7635 7636 7637
	      YYABORT;
	    }
	  }
	| table_ident
	  {
unknown's avatar
unknown committed
7638
	    LEX *lex=Lex;
unknown's avatar
unknown committed
7639
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
unknown's avatar
unknown committed
7640
	      YYABORT;
unknown's avatar
unknown committed
7641
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
7642
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
7643 7644
	  }
          ;
unknown's avatar
unknown committed
7645 7646 7647


user_list:
7648 7649 7650 7651 7652 7653 7654
	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
7655 7656 7657 7658 7659 7660 7661 7662


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
7663
             if (YYTHD->variables.old_passwords)
7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680
             {
               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
7681 7682 7683 7684 7685
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
	  { $$=$1; $1->password=$5 ; }
	| user
7686 7687
	  { $$=$1; $1->password.str=NullS; }
        ;
unknown's avatar
unknown committed
7688 7689 7690


opt_column_list:
unknown's avatar
unknown committed
7691 7692 7693 7694 7695
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
unknown's avatar
unknown committed
7696
	| '(' column_list ')';
unknown's avatar
unknown committed
7697 7698 7699

column_list:
	column_list ',' column_list_id
unknown's avatar
unknown committed
7700
	| column_list_id;
unknown's avatar
unknown committed
7701 7702 7703 7704

column_list_id:
	ident
	{
unknown's avatar
unknown committed
7705
	  String *new_str = new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info);
unknown's avatar
unknown committed
7706 7707
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
unknown's avatar
unknown committed
7708
	  LEX *lex=Lex;
unknown's avatar
unknown committed
7709 7710
	  while ((point=iter++))
	  {
7711 7712
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
unknown's avatar
unknown committed
7713 7714
		break;
	  }
unknown's avatar
unknown committed
7715
	  lex->grant_tot_col|= lex->which_columns;
unknown's avatar
unknown committed
7716
	  if (point)
unknown's avatar
unknown committed
7717
	    point->rights |= lex->which_columns;
unknown's avatar
unknown committed
7718
	  else
unknown's avatar
unknown committed
7719
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
7720 7721
	}
        ;
unknown's avatar
unknown committed
7722

unknown's avatar
unknown committed
7723 7724

require_clause: /* empty */
7725
        | REQUIRE_SYM require_list
7726 7727 7728
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
7729
        | REQUIRE_SYM SSL_SYM
7730 7731 7732
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
7733
        | REQUIRE_SYM X509_SYM
7734 7735 7736 7737 7738 7739 7740
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
7741
          ;
unknown's avatar
unknown committed
7742

unknown's avatar
unknown committed
7743
grant_options:
unknown's avatar
unknown committed
7744
	/* empty */ {}
unknown's avatar
unknown committed
7745
	| WITH grant_option_list;
unknown's avatar
unknown committed
7746

unknown's avatar
unknown committed
7747 7748
grant_option_list:
	grant_option_list grant_option {}
7749 7750
	| grant_option {}
        ;
unknown's avatar
unknown committed
7751 7752 7753

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
7754
        | MAX_QUERIES_PER_HOUR ULONG_NUM
unknown's avatar
unknown committed
7755
        {
7756
	  Lex->mqh.questions=$2;
7757
	  Lex->mqh.bits |= 1;
7758
	}
7759
        | MAX_UPDATES_PER_HOUR ULONG_NUM
7760
        {
7761
	  Lex->mqh.updates=$2;
7762
	  Lex->mqh.bits |= 2;
7763
	}
7764
        | MAX_CONNECTIONS_PER_HOUR ULONG_NUM
7765
        {
7766
	  Lex->mqh.connections=$2;
7767
	  Lex->mqh.bits |= 4;
7768 7769
	}
        ;
7770

unknown's avatar
unknown committed
7771
begin:
7772
	BEGIN_SYM   { Lex->sql_command = SQLCOM_BEGIN; Lex->start_transaction_opt= 0;} opt_work {}
7773
	;
unknown's avatar
unknown committed
7774 7775 7776

opt_work:
	/* empty */ {}
7777 7778
	| WORK_SYM {;}
        ;
unknown's avatar
unknown committed
7779 7780

commit:
unknown's avatar
unknown committed
7781
	COMMIT_SYM   { Lex->sql_command = SQLCOM_COMMIT;};
unknown's avatar
unknown committed
7782 7783

rollback:
unknown's avatar
unknown committed
7784
	ROLLBACK_SYM
7785 7786 7787 7788 7789
	{
	  Lex->sql_command = SQLCOM_ROLLBACK;
	}
	| ROLLBACK_SYM TO_SYM SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
7790
	  Lex->sql_command = SQLCOM_ROLLBACK_TO_SAVEPOINT;
7791 7792 7793 7794 7795 7796 7797 7798
	  Lex->savepoint_name = $4.str;
	};
savepoint:
	SAVEPOINT_SYM ident
	{
	  Lex->sql_command = SQLCOM_SAVEPOINT;
	  Lex->savepoint_name = $2.str;
	};
7799 7800

/*
unknown's avatar
unknown committed
7801
   UNIONS : glue selects together
7802 7803 7804
*/


7805
union_clause:
7806
	/* empty */ {}
unknown's avatar
unknown committed
7807 7808
	| union_list
	;
7809 7810

union_list:
7811
	UNION_SYM union_option
7812 7813 7814 7815 7816
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
unknown's avatar
unknown committed
7817
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
7818
	    YYABORT;
7819
	  }
7820
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
7821
	  {
7822
            yyerror(ER(ER_SYNTAX_ERROR));
7823 7824
	    YYABORT;
	  }
7825
	  if (mysql_new_select(lex, 0))
7826
	    YYABORT;
unknown's avatar
unknown committed
7827
          mysql_init_select(lex);
7828
	  lex->current_select->linkage=UNION_TYPE;
7829 7830 7831
          if ($2) /* UNION DISTINCT - remember position */
            lex->current_select->master_unit()->union_distinct=
                                                      lex->current_select;
unknown's avatar
unknown committed
7832
	}
7833
	select_init {}
7834
	;
7835 7836

union_opt:
unknown's avatar
unknown committed
7837
	union_list {}
unknown's avatar
unknown committed
7838 7839
	| optional_order_or_limit {}
	;
7840 7841

optional_order_or_limit:
7842
	/* Empty */ {}
7843 7844
	|
	  {
unknown's avatar
unknown committed
7845
	    THD *thd= YYTHD;
7846
	    LEX *lex= thd->lex;
unknown's avatar
unknown committed
7847
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
unknown's avatar
unknown committed
7848
	    SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
7849
	    SELECT_LEX_UNIT *unit= sel->master_unit();
unknown's avatar
unknown committed
7850
	    SELECT_LEX *fake= unit->fake_select_lex;
unknown's avatar
unknown committed
7851 7852 7853 7854 7855 7856
	    if (fake)
	    {
	      unit->global_parameters= fake;
	      fake->no_table_names_allowed= 1;
	      lex->current_select= fake;
	    }
unknown's avatar
unknown committed
7857
	    thd->where= "global ORDER clause";
7858
	  }
7859
	order_or_limit
unknown's avatar
unknown committed
7860 7861
          {
	    THD *thd= YYTHD;
7862
	    thd->lex->current_select->no_table_names_allowed= 0;
unknown's avatar
unknown committed
7863 7864
	    thd->where= "";
          }
7865 7866 7867
	;

order_or_limit:
7868 7869
	order_clause opt_limit_clause_init
	| limit_clause
7870
	;
unknown's avatar
unknown committed
7871 7872

union_option:
7873 7874 7875 7876
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
7877

unknown's avatar
unknown committed
7878 7879
singlerow_subselect:
	subselect_start singlerow_subselect_init
unknown's avatar
unknown committed
7880 7881 7882 7883
	subselect_end
	{
	  $$= $2;
	};
7884

unknown's avatar
unknown committed
7885
singlerow_subselect_init:
7886
	select_init2
unknown's avatar
unknown committed
7887
	{
7888 7889
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
unknown's avatar
unknown committed
7890
	};
unknown's avatar
unknown committed
7891 7892

exists_subselect:
unknown's avatar
unknown committed
7893 7894 7895 7896 7897
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
unknown's avatar
unknown committed
7898 7899

exists_subselect_init:
7900
	select_init2
unknown's avatar
unknown committed
7901
	{
7902 7903
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
unknown's avatar
unknown committed
7904
	};
7905

unknown's avatar
unknown committed
7906 7907 7908 7909 7910 7911 7912 7913
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
7914
  select_init2
unknown's avatar
unknown committed
7915
  {
unknown's avatar
unknown committed
7916
    $$= Lex->current_select->master_unit()->first_select();
unknown's avatar
unknown committed
7917 7918
  };

7919
subselect_start:
7920
	'(' SELECT_SYM
unknown's avatar
unknown committed
7921
	{
7922
	  LEX *lex=Lex;
7923
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
7924 7925
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
7926 7927
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
7928 7929
	    YYABORT;
	  }
unknown's avatar
unknown committed
7930 7931 7932
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
7933 7934

subselect_end:
unknown's avatar
unknown committed
7935 7936 7937
	')'
	{
	  LEX *lex=Lex;
7938
	  lex->current_select = lex->current_select->return_after_parsing();
unknown's avatar
unknown committed
7939
	};
unknown's avatar
unknown committed
7940

7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984
opt_view_list:
	/* empty */ {}
	| '(' view_list ')'
	;

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

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

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