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

bk@work.mysql.com's avatar
bk@work.mysql.com 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

bk@work.mysql.com's avatar
bk@work.mysql.com 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

bk@work.mysql.com's avatar
bk@work.mysql.com 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 */

tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
17
/* sql_yacc.yy */
bk@work.mysql.com's avatar
bk@work.mysql.com 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)

bk@work.mysql.com's avatar
bk@work.mysql.com 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
bk@work.mysql.com's avatar
bk@work.mysql.com committed
33
#include "mysql_priv.h"
34
#include "slave.h"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
35 36
#include "sql_acl.h"
#include "lex_symbol.h"
37
#include "item_create.h"
38 39
#include "sp_head.h"
#include "sp_pcontext.h"
40
#include "sp_rcontext.h"
41
#include "sp.h"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
42
#include <myisam.h>
43
#include <myisammrg.h>
44

bk@work.mysql.com's avatar
bk@work.mysql.com committed
45
extern void yyerror(const char*);
46
int yylex(void *yylval, void *yythd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
47

48
#define yyoverflow(A,B,C,D,E,F) if (my_yyoverflow((B),(D),(int*) (F))) { yyerror((char*) (A)); return 2; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
49

50
inline Item *or_or_concat(THD *thd, Item* A, Item* B)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
51
{
52
  return (thd->variables.sql_mode & MODE_PIPES_AS_CONCAT ?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
53 54 55 56 57 58 59
          (Item*) new Item_func_concat(A,B) : (Item*) new Item_cond_or(A,B));
}

%}
%union {
  int  num;
  ulong ulong_num;
60
  ulonglong ulonglong_number;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
61 62 63 64 65 66 67 68
  LEX_STRING lex_str;
  LEX_STRING *lex_str_ptr;
  LEX_SYMBOL symbol;
  Table_ident *table;
  char *simple_string;
  Item *item;
  List<Item> *item_list;
  List<String> *string_list;
69 70 71 72 73
  String *string;
  key_part_spec *key_part;
  TABLE_LIST *table_list;
  udf_func *udf;
  LEX_USER *lex_user;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
74
  struct sys_var_with_base variable;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
75
  Key::Keytype key_type;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
76
  enum ha_key_alg key_alg;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
77 78
  enum db_type db_type;
  enum row_type row_type;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
79
  enum ha_rkey_function ha_rkey_mode;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
80
  enum enum_tx_isolation tx_isolation;
81
  enum Cast_target cast_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
82
  enum Item_udftype udf_type;
83
  CHARSET_INFO *charset;
84
  thr_lock_type lock_type;
85
  interval_type interval;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
86
  st_select_lex *select_lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
87
  chooser_compare_func_creator boolfunc2creator;
88
  struct sp_cond_type *spcondtype;
89 90
  struct { int vars, conds, hndlrs, curs; } spblock;
  struct st_lex *lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
91 92 93 94 95 96 97 98 99 100
}

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

%pure_parser					/* We have threads */

%token	END_OF_INPUT

101 102 103 104 105 106
%token CLOSE_SYM
%token HANDLER_SYM
%token LAST_SYM
%token NEXT_SYM
%token PREV_SYM

107
%token	DIV_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
108 109
%token	EQ
%token	EQUAL_SYM
110
%token	SOUNDS_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
111 112 113 114 115 116
%token	GE
%token	GT_SYM
%token	LE
%token	LT
%token	NE
%token	IS
117
%token	MOD_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
118 119 120 121
%token	SHIFT_LEFT
%token	SHIFT_RIGHT
%token  SET_VAR

122
%token	ABORT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
123 124
%token	ADD
%token	AFTER_SYM
125 126
%token	ALTER
%token	ANALYZE_SYM
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
127
%token	ANY_SYM
128 129 130
%token	AVG_SYM
%token	BEGIN_SYM
%token	BINLOG_SYM
131
%token  CALL_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
132
%token	CHANGE
133 134 135 136
%token	CLIENT_SYM
%token	COMMENT_SYM
%token	COMMIT_SYM
%token	COUNT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
137 138
%token	CREATE
%token	CROSS
139
%token  CUBE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
140
%token	DELETE_SYM
141
%token	DUAL_SYM
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
142
%token	DO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
143
%token	DROP
144 145
%token	EVENTS_SYM
%token	EXECUTE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
146
%token	FLUSH_SYM
147
%token  HELP_SYM
148
%token	INSERT
149
%token	RELAY_THREAD
bk@work.mysql.com's avatar
bk@work.mysql.com committed
150 151
%token	KILL_SYM
%token	LOAD
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
152
%token	LOCKS_SYM
153 154 155 156
%token	LOCK_SYM
%token	MASTER_SYM
%token	MAX_SYM
%token	MIN_SYM
157
%token	NONE_SYM
158 159 160 161 162 163
%token	OPTIMIZE
%token	PURGE
%token	REPAIR
%token	REPLICATION
%token	RESET_SYM
%token	ROLLBACK_SYM
164
%token  ROLLUP_SYM
165
%token	SAVEPOINT_SYM
166 167 168 169 170 171
%token	SELECT_SYM
%token	SHOW
%token	SLAVE
%token	SQL_THREAD
%token	START_SYM
%token	STD_SYM
172
%token  VARIANCE_SYM
173 174
%token	STOP_SYM
%token	SUM_SYM
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
175
%token	ADDDATE_SYM
176 177
%token	SUPER_SYM
%token	TRUNCATE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
178
%token	UNLOCK_SYM
179
%token	UNTIL_SYM 
180
%token	UPDATE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
181 182 183 184 185 186 187

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

423 424 425
%token  ERRORS
%token  WARNINGS

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
426
%token	ASCII_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
427 428 429
%token	BIGINT
%token	BLOB_SYM
%token	CHAR_SYM
430
%token	CHANGED
bk@work.mysql.com's avatar
bk@work.mysql.com committed
431 432 433 434 435 436
%token	COALESCE
%token	DATETIME
%token	DATE_SYM
%token	DECIMAL_SYM
%token	DOUBLE_SYM
%token	ENUM
437
%token	FAST_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
438
%token	FLOAT_SYM
439
%token  GEOMETRY_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
440 441 442 443 444 445 446 447 448
%token	INT_SYM
%token	LIMIT
%token	LONGBLOB
%token	LONGTEXT
%token	MEDIUMBLOB
%token	MEDIUMINT
%token	MEDIUMTEXT
%token	NUMERIC_SYM
%token	PRECISION
449
%token	QUICK
bk@work.mysql.com's avatar
bk@work.mysql.com committed
450
%token	REAL
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
451
%token	SIGNED_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
452 453 454 455 456 457 458 459
%token	SMALLINT
%token	STRING_SYM
%token	TEXT_SYM
%token	TIMESTAMP
%token	TIME_SYM
%token	TINYBLOB
%token	TINYINT
%token	TINYTEXT
460
%token	ULONGLONG_NUM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
461
%token	UNSIGNED
462 463 464
%token	VARBINARY
%token	VARCHAR
%token	VARYING
bk@work.mysql.com's avatar
bk@work.mysql.com committed
465 466
%token	ZEROFILL

gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
467
%token	ADDDATE_SYM
468
%token	AGAINST
bk@work.mysql.com's avatar
bk@work.mysql.com committed
469 470 471 472 473 474
%token	ATAN
%token	BETWEEN_SYM
%token	BIT_AND
%token	BIT_OR
%token	CASE_SYM
%token	CONCAT
475
%token	CONCAT_WS
bk@work.mysql.com's avatar
bk@work.mysql.com committed
476 477 478 479 480 481
%token	CURDATE
%token	CURTIME
%token	DATABASE
%token	DATE_ADD_INTERVAL
%token	DATE_SUB_INTERVAL
%token	DAY_HOUR_SYM
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
482
%token	DAY_MICROSECOND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
483 484 485 486
%token	DAY_MINUTE_SYM
%token	DAY_SECOND_SYM
%token	DAY_SYM
%token	DECODE_SYM
487 488
%token	DES_ENCRYPT_SYM
%token	DES_DECRYPT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
489 490 491 492 493 494 495 496 497 498
%token	ELSE
%token	ELT_FUNC
%token	ENCODE_SYM
%token	ENCRYPT
%token	EXPORT_SET
%token	EXTRACT_SYM
%token	FIELD_FUNC
%token	FORMAT_SYM
%token	FOR_SYM
%token	FROM_UNIXTIME
499 500
%token	GEOMCOLLFROMTEXT
%token	GEOMFROMTEXT
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
501
%token	GEOMFROMWKB
502
%token  GEOMETRYCOLLECTION
503
%token  GROUP_CONCAT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
504
%token	GROUP_UNIQUE_USERS
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
505
%token	HOUR_MICROSECOND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
506 507 508 509 510
%token	HOUR_MINUTE_SYM
%token	HOUR_SECOND_SYM
%token	HOUR_SYM
%token	IDENTIFIED_SYM
%token	IF
511
%token	INSERT_METHOD
bk@work.mysql.com's avatar
bk@work.mysql.com committed
512 513 514
%token	INTERVAL_SYM
%token	LAST_INSERT_ID
%token	LEFT
515
%token	LINEFROMTEXT
516
%token  LINESTRING
bk@work.mysql.com's avatar
bk@work.mysql.com committed
517 518
%token	LOCATE
%token	MAKE_SET_SYM
519
%token	MASTER_POS_WAIT
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
520 521
%token  MICROSECOND_SYM
%token	MINUTE_MICROSECOND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
522 523
%token	MINUTE_SECOND_SYM
%token	MINUTE_SYM
524
%token	MODE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
525 526
%token	MODIFY_SYM
%token	MONTH_SYM
527 528 529
%token	MLINEFROMTEXT
%token	MPOINTFROMTEXT
%token	MPOLYFROMTEXT
530 531 532
%token  MULTILINESTRING
%token  MULTIPOINT
%token  MULTIPOLYGON
bk@work.mysql.com's avatar
bk@work.mysql.com committed
533
%token	NOW_SYM
534
%token	OLD_PASSWORD
bk@work.mysql.com's avatar
bk@work.mysql.com committed
535
%token	PASSWORD
536
%token	POINTFROMTEXT
537
%token	POINT_SYM
538
%token	POLYFROMTEXT
539
%token  POLYGON
bk@work.mysql.com's avatar
bk@work.mysql.com committed
540 541 542 543 544 545 546
%token	POSITION_SYM
%token	PROCEDURE
%token	RAND
%token	REPLACE
%token	RIGHT
%token	ROUND
%token	SECOND_SYM
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
547
%token	SECOND_MICROSECOND_SYM
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
548
%token	SHARE_SYM
549
%token  SP_FUNC
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
550
%token	SUBDATE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
551 552 553 554 555 556 557 558 559 560 561 562
%token	SUBSTRING
%token	SUBSTRING_INDEX
%token	TRIM
%token	UDA_CHAR_SUM
%token	UDA_FLOAT_SUM
%token	UDA_INT_SUM
%token	UDF_CHAR_FUNC
%token	UDF_FLOAT_FUNC
%token	UDF_INT_FUNC
%token	UNIQUE_USERS
%token	UNIX_TIMESTAMP
%token	USER
563 564 565
%token	UTC_DATE_SYM
%token	UTC_TIME_SYM
%token	UTC_TIMESTAMP_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
566 567
%token	WEEK_SYM
%token	WHEN_SYM
568
%token	WORK_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
569 570 571
%token	YEAR_MONTH_SYM
%token	YEAR_SYM
%token	YEARWEEK
572 573 574
%token	BENCHMARK_SYM
%token	END
%token	THEN_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
575

576 577 578 579
%token	SQL_BIG_RESULT
%token	SQL_CACHE_SYM
%token	SQL_CALC_FOUND_ROWS
%token	SQL_NO_CACHE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
580
%token	SQL_SMALL_RESULT
581
%token  SQL_BUFFER_RESULT
bk@work.mysql.com's avatar
bk@work.mysql.com committed
582

583 584 585 586 587
%token  CURSOR_SYM
%token  ELSEIF_SYM
%token  ITERATE_SYM
%token  LEAVE_SYM
%token  LOOP_SYM
588
%token  REPEAT_SYM
589 590 591 592 593
%token  UNTIL_SYM
%token  WHILE_SYM
%token  ASENSITIVE_SYM
%token  INSENSITIVE_SYM
%token  SENSITIVE_SYM
594

595 596
%token  ISSUER_SYM
%token  SUBJECT_SYM
597
%token  CIPHER_SYM
598

599
%token  BEFORE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
600 601 602 603 604 605 606 607 608
%left   SET_VAR
%left	OR_OR_CONCAT OR
%left	AND
%left	BETWEEN_SYM CASE_SYM WHEN_SYM THEN_SYM ELSE
%left	EQ EQUAL_SYM GE GT_SYM LE LT NE IS LIKE REGEXP IN_SYM
%left	'|'
%left	'&'
%left	SHIFT_LEFT SHIFT_RIGHT
%left	'-' '+'
609
%left	'*' '/' '%' DIV_SYM MOD_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
610
%left	NEG '~'
611 612
%left   XOR
%left   '^'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
613
%right	NOT
614
%right	BINARY COLLATE_SYM
615

bk@work.mysql.com's avatar
bk@work.mysql.com committed
616 617
%type <lex_str>
	IDENT TEXT_STRING REAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM LEX_HOSTNAME
618
	ULONGLONG_NUM field_ident select_alias ident ident_or_text
619
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
620
	NCHAR_STRING opt_component
pem@mysql.com's avatar
pem@mysql.com committed
621
        SP_FUNC ident_or_spfunc
bk@work.mysql.com's avatar
bk@work.mysql.com committed
622 623 624 625 626

%type <lex_str_ptr>
	opt_table_alias

%type <table>
627
	table_ident references
bk@work.mysql.com's avatar
bk@work.mysql.com committed
628 629

%type <simple_string>
630
	remember_name remember_end opt_ident opt_db text_or_password
bk@work.mysql.com's avatar
bk@work.mysql.com committed
631 632 633
	opt_escape

%type <string>
634
	text_string opt_gconcat_separator
bk@work.mysql.com's avatar
bk@work.mysql.com committed
635 636

%type <num>
637
	type int_type real_type order_dir opt_field_spec lock_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
638
	udf_type if_exists opt_local opt_table_options table_options
639
	table_option opt_if_not_exists opt_no_write_to_binlog opt_var_type opt_var_ident_type
640
	delete_option opt_temporary all_or_any opt_distinct opt_ignore_leaves
bk@work.mysql.com's avatar
bk@work.mysql.com committed
641 642

%type <ulong_num>
643
	ULONG_NUM raid_types merge_insert_types
bk@work.mysql.com's avatar
bk@work.mysql.com committed
644

645 646
%type <ulonglong_number>
	ulonglong_num
bk@work.mysql.com's avatar
bk@work.mysql.com committed
647

648 649 650
%type <lock_type>
	replace_lock_option opt_low_priority insert_lock_option load_data_lock

bk@work.mysql.com's avatar
bk@work.mysql.com committed
651
%type <item>
652
	literal text_literal insert_ident order_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
653
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
654
	table_wild no_in_expr expr_expr simple_expr no_and_expr
655
	using_list expr_or_default set_expr_or_default interval_expr
656
	param_marker singlerow_subselect singlerow_subselect_init
657
	exists_subselect exists_subselect_init sp_opt_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
658 659

%type <item_list>
660
	expr_list udf_expr_list when_list ident_list ident_list_arg
bk@work.mysql.com's avatar
bk@work.mysql.com committed
661 662

%type <key_type>
663
	key_type opt_unique_or_fulltext
bk@work.mysql.com's avatar
bk@work.mysql.com committed
664

665 666 667
%type <key_alg>
	key_alg opt_btree_or_rtree

bk@work.mysql.com's avatar
bk@work.mysql.com committed
668 669 670 671 672 673 674
%type <string_list>
	key_usage_list

%type <key_part>
	key_part

%type <table_list>
675
	join_table_list  join_table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
676 677 678 679 680 681 682 683 684 685 686

%type <udf>
	UDF_CHAR_FUNC UDF_FLOAT_FUNC UDF_INT_FUNC
	UDA_CHAR_SUM UDA_FLOAT_SUM UDA_INT_SUM

%type <interval> interval

%type <db_type> table_types

%type <row_type> row_types

687
%type <tx_isolation> isolation_types
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
688

monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
689 690
%type <ha_rkey_mode> handler_rkey_mode

691
%type <cast_type> cast_type cast_type_finalize
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
692

bk@work.mysql.com's avatar
bk@work.mysql.com committed
693 694 695 696 697 698
%type <udf_type> udf_func_type

%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword

%type <lex_user> user grant_user

699
%type <charset>
700
	opt_collate
701 702
	charset_name
	charset_name_or_default
703 704
	old_or_new_charset_name
	old_or_new_charset_name_or_default
705 706
	collation_name
	collation_name_or_default
707

708 709
%type <variable> internal_variable_name

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
710 711
%type <select_lex> in_subselect in_subselect_init

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
712 713
%type <boolfunc2creator> comp_op

bk@work.mysql.com's avatar
bk@work.mysql.com committed
714
%type <NONE>
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
715
	query verb_clause create change select do drop insert replace insert2
716
	insert_values update delete truncate rename
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
717
	show describe load alter optimize preload flush
718
	reset purge begin commit rollback savepoint
719
	slave master_def master_defs master_file_def
720
	repair restore backup analyze check start checksum
721
	field_list field_list_item field_spec kill column_def key_def
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
722
	preload_list preload_keys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
723
	select_item_list select_item values_list no_braces
724
	opt_limit_clause delete_limit_clause fields opt_values values
bk@work.mysql.com's avatar
bk@work.mysql.com committed
725
	procedure_list procedure_list2 procedure_item
726
	when_list2 expr_list2  handler
bk@work.mysql.com's avatar
bk@work.mysql.com committed
727 728
	opt_precision opt_ignore opt_column opt_restrict
	grant revoke set lock unlock string_list field_options field_option
729
	field_opt_list opt_binary table_lock_list table_lock
730
	ref_list opt_on_delete opt_on_delete_list opt_on_delete_item use
731
	opt_delete_options opt_delete_option varchar nchar nvarchar
732
	opt_outer table_list table_name opt_option opt_place
bk@work.mysql.com's avatar
bk@work.mysql.com committed
733 734
	opt_attribute opt_attribute_list attribute column_list column_list_id
	opt_column_list grant_privileges opt_table user_list grant_option
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
735
	grant_privilege grant_privilege_list
736
	flush_options flush_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
737 738
	equal optional_braces opt_key_definition key_usage_list2
	opt_mi_check_type opt_to mi_check_types normal_join
739
	table_to_table_list table_to_table opt_table_list opt_as
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
740
	handler_rkey_function handler_read_or_scan
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
741
	single_multi table_wild_list table_wild_one opt_wild
742
	union_clause union_list union_option
743
	precision subselect_start opt_and charset
744
	subselect_end select_var_list select_var_list_init help opt_len
745
	statement
746
END_OF_INPUT
bk@work.mysql.com's avatar
bk@work.mysql.com committed
747

748
%type <NONE> call sp_proc_stmts sp_proc_stmt
749 750 751
%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
752
%type <lex> sp_cursor_stmt
753

bk@work.mysql.com's avatar
bk@work.mysql.com committed
754 755
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
756 757
	',' '!' '{' '}' '&' '|' AND OR OR_OR_CONCAT BETWEEN_SYM CASE_SYM
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
758 759 760 761 762
%%


query:
	END_OF_INPUT
763
	{
764
	   THD *thd= YYTHD;
765
	   if (!thd->bootstrap &&
766
	      (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
767
	   {
768
	     send_error(thd,ER_EMPTY_QUERY);
769
	     YYABORT;
770
	   }
771 772
	   else
	   {
773
	     thd->lex->sql_command = SQLCOM_EMPTY_QUERY;
774
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
775
	}
776
	| verb_clause END_OF_INPUT {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
777 778

verb_clause:
779 780 781 782 783 784
	  statement
	| begin
	;

/* Verb clauses, except begin */
statement:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
785 786
	  alter
	| analyze
787
	| backup
788
	| call
bk@work.mysql.com's avatar
bk@work.mysql.com committed
789 790
	| change
	| check
791
	| checksum
bk@work.mysql.com's avatar
bk@work.mysql.com committed
792 793 794 795
	| commit
	| create
	| delete
	| describe
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
796
	| do
bk@work.mysql.com's avatar
bk@work.mysql.com committed
797
	| drop
serg@serg.mylan's avatar
serg@serg.mylan committed
798
	| flush
bk@work.mysql.com's avatar
bk@work.mysql.com committed
799
	| grant
serg@serg.mylan's avatar
serg@serg.mylan committed
800 801
	| handler
	| help
bk@work.mysql.com's avatar
bk@work.mysql.com committed
802
	| insert
serg@serg.mylan's avatar
serg@serg.mylan committed
803
	| kill
bk@work.mysql.com's avatar
bk@work.mysql.com committed
804 805 806
	| load
	| lock
	| optimize
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
807
	| preload
808
	| purge
809
	| rename
serg@serg.mylan's avatar
serg@serg.mylan committed
810
	| repair
bk@work.mysql.com's avatar
bk@work.mysql.com committed
811
	| replace
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
812
	| reset
813
	| restore
bk@work.mysql.com's avatar
bk@work.mysql.com committed
814 815
	| revoke
	| rollback
816
	| savepoint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
817 818
	| select
	| set
serg@serg.mylan's avatar
serg@serg.mylan committed
819
	| show
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
820
	| slave
821
	| start
822
	| truncate
bk@work.mysql.com's avatar
bk@work.mysql.com committed
823 824
	| unlock
	| update
825
	| use
serg@serg.mylan's avatar
serg@serg.mylan committed
826
        ;
827

828 829
/* help */

830
help:
831
       HELP_SYM ident_or_text
832
       {
833 834 835
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
	  lex->help_arg= $2.str;
836
       };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
837 838 839 840

/* change master */

change:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
841
       CHANGE MASTER_SYM TO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
842 843 844
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
845
	  bzero((char*) &lex->mi, sizeof(lex->mi));
846 847 848 849
        }
       master_defs
	{}
       ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
850 851 852

master_defs:
       master_def
853
       | master_defs ',' master_def;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
854

bk@work.mysql.com's avatar
bk@work.mysql.com committed
855
master_def:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
856
       MASTER_HOST_SYM EQ TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
857 858 859 860
       {
	 Lex->mi.host = $3.str;
       }
       |
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
861
       MASTER_USER_SYM EQ TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
862 863 864 865
       {
	 Lex->mi.user = $3.str;
       }
       |
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
866
       MASTER_PASSWORD_SYM EQ TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
867 868 869 870
       {
	 Lex->mi.password = $3.str;
       }
       |
871
       MASTER_PORT_SYM EQ ULONG_NUM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
872 873 874 875
       {
	 Lex->mi.port = $3;
       }
       |
876
       MASTER_CONNECT_RETRY_SYM EQ ULONG_NUM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
877 878 879
       {
	 Lex->mi.connect_retry = $3;
       }
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
       | 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;
904
	 }
905
       |
906
         master_file_def
907
       ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
908

909 910
master_file_def:
       MASTER_LOG_FILE_SYM EQ TEXT_STRING_sys
911
       {
912
	 Lex->mi.log_file_name = $3.str;
913
       }
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
       | 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);
         }
940
       ;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
941

bk@work.mysql.com's avatar
bk@work.mysql.com committed
942 943 944 945 946
/* create a table */

create:
	CREATE opt_table_options TABLE_SYM opt_if_not_exists table_ident
	{
947
	  THD *thd= YYTHD;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
948 949
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_CREATE_TABLE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
950
	  if (!lex->select_lex.add_table_to_list(thd,$5,
951
						 ($2 &
952 953
						  HA_LEX_CREATE_TMP_TABLE ?
						  &tmp_table_alias :
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
954 955
						  (LEX_STRING*) 0),
						 TL_OPTION_UPDATING,
956 957 958
						 ((using_update_log)?
						  TL_READ_NO_INSERT:
						  TL_READ)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
959 960 961 962 963 964 965
	    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;
966
	  lex->create_info.db_type= (enum db_type) lex->thd->variables.table_type;
967
	  lex->create_info.table_charset= thd->variables.collation_database;
venu@myvenu.com's avatar
venu@myvenu.com committed
968
	  lex->name=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
969 970
	}
	create2
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
971
	  { Lex->current_select= &Lex->select_lex; }
972
	| CREATE opt_unique_or_fulltext INDEX ident key_alg ON table_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
973
	  {
974 975
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_CREATE_INDEX;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
976 977
	    if (!lex->current_select->add_table_to_list(lex->thd, $7, NULL,
							TL_OPTION_UPDATING))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
978
	      YYABORT;
979 980 981 982
	    lex->create_list.empty();
	    lex->key_list.empty();
	    lex->col_list.empty();
	    lex->change=NullS;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
983
	  }
984
	   '(' key_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
985
	  {
986
	    LEX *lex=Lex;
987

988
	    lex->key_list.push_back(new Key($2,$4.str, $5, lex->col_list));
989
	    lex->col_list.empty();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
990 991
	  }
	| CREATE DATABASE opt_if_not_exists ident
992 993
	  { Lex->create_info.table_charset=NULL; }
	  opt_create_database_options
bk@work.mysql.com's avatar
bk@work.mysql.com committed
994
	  {
995 996 997 998
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_CREATE_DB;
	    lex->name=$4.str;
            lex->create_info.options=$3;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
999
	  }
1000
	| CREATE udf_func_type FUNCTION_SYM ident_or_spfunc
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1001
	  {
1002
	    LEX *lex=Lex;
1003
	    lex->udf.name = $4;
1004
	    lex->udf.type= $2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1005
	  }
1006 1007
	  create_function_tail
	  {}
1008 1009 1010
	| CREATE PROCEDURE ident
	  {
	    LEX *lex= Lex;
1011
	    sp_head *sp;
1012

1013 1014 1015 1016 1017
	    if (lex->sphead)
	    {
	      net_printf(YYTHD, ER_SP_NO_RECURSIVE_CREATE, "PROCEDURE");
	      YYABORT;
	    }
1018 1019 1020 1021 1022 1023 1024
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
	    sp->init(&$3, lex, 0, 0);

	    sp->m_type= TYPE_ENUM_PROCEDURE;
	    lex->sphead= sp;
1025 1026 1027 1028 1029
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
1030
	    sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
1031
	    YYTHD->client_capabilities &= (~CLIENT_MULTI_QUERIES);
1032
	  }
1033
          '(' sp_pdparam_list ')'
1034 1035 1036
	  {
	    Lex->spcont->set_params();
	  }
1037
	  sp_proc_stmt
1038
	  {
1039 1040 1041 1042 1043 1044
	    LEX *lex= Lex;

	    lex->sql_command= SQLCOM_CREATE_PROCEDURE;
	    /* Restore flag if it was cleared above */
	    if (lex->sphead->m_old_cmq)
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
1045
	    lex->sphead->restore_thd_mem_root(YYTHD);
1046 1047 1048
	  } 
	;

1049
ident_or_spfunc:
pem@mysql.com's avatar
pem@mysql.com committed
1050
	  IDENT_sys { $$= $1; }
1051 1052 1053
	| SP_FUNC { $$= $1; }
	;

1054
create_function_tail:
pem@mysql.com's avatar
pem@mysql.com committed
1055
	  RETURNS_SYM udf_type UDF_SONAME_SYM TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1056
	  {
1057
	    LEX *lex=Lex;
1058 1059 1060
	    lex->sql_command = SQLCOM_CREATE_FUNCTION;
	    lex->udf.returns=(Item_result) $2;
	    lex->udf.dl=$4.str;
1061
	  }
1062 1063 1064
	| '('
	  {
	    LEX *lex= Lex;
1065
	    sp_head *sp;
1066

1067 1068 1069 1070 1071
	    if (lex->sphead)
	    {
	      net_printf(YYTHD, ER_SP_NO_RECURSIVE_CREATE, "FUNCTION");
	      YYABORT;
	    }
1072 1073 1074 1075 1076 1077 1078
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
	    sp->init(&lex->udf.name, lex, 0, 0);

	    sp->m_type= TYPE_ENUM_FUNCTION;
	    lex->sphead= sp;
1079 1080 1081 1082 1083
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
1084
	    sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
1085
	    YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
	  }
          sp_fdparam_list ')'
	  {
	    Lex->spcont->set_params();
	  }
	  RETURNS_SYM type
	  {
	    Lex->sphead->m_returns= (enum enum_field_types)$7;
	  }
	  sp_proc_stmt
1096
	  {
1097 1098 1099 1100 1101 1102
	    LEX *lex= Lex;

	    lex->sql_command = SQLCOM_CREATE_SPFUNCTION;
	    /* Restore flag if it was cleared above */
	    if (lex->sphead->m_old_cmq)
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
1103
	    lex->sphead->restore_thd_mem_root(YYTHD);
1104
	  }
1105 1106
	;

1107
call:
1108
	  CALL_SYM ident_or_spfunc
1109 1110 1111 1112
	  {
	    LEX *lex = Lex;

	    lex->sql_command= SQLCOM_CALL;
1113
	    lex->udf.name= $2;
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	    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);
	  }
	;

1136 1137
/* Stored FUNCTION parameter declaration list */
sp_fdparam_list:
1138
	  /* Empty */
1139
	| sp_fdparams
1140 1141
	;

1142 1143 1144
sp_fdparams:
	  sp_fdparams ',' sp_fdparam
	| sp_fdparam
1145 1146
	;

1147 1148 1149
sp_fdparam:
	  ident type sp_opt_locator
	  {
1150
	    Lex->spcont->push_pvar(&$1, (enum enum_field_types)$2, sp_param_in);
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
1166 1167
	  sp_opt_inout ident type sp_opt_locator
	  {
1168 1169 1170
	    Lex->spcont->push_pvar(&$2,
	                           (enum enum_field_types)$3,
			           (sp_param_mode_t)$1);
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	  }
	;

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

sp_opt_locator:
	  /* Empty */
	| AS LOCATOR_SYM
	;

sp_proc_stmts:
1187 1188
	  sp_proc_stmt ';'
	| sp_proc_stmts sp_proc_stmt ';'
1189 1190 1191 1192 1193
	;

sp_decls:
	  /* Empty */
	  {
1194
	    $$.vars= $$.conds= $$.hndlrs= $$.curs= 0;
1195 1196 1197
	  }
	| sp_decls sp_decl ';'
	  {
1198 1199 1200
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
1201
	    $$.curs= $1.curs + $2.curs;
1202 1203 1204 1205
	  }
	;

sp_decl:
1206
	  DECLARE_SYM sp_decl_idents type sp_opt_default
1207 1208 1209
	  {
	    LEX *lex= Lex;
	    uint max= lex->spcont->current_framesize();
1210 1211
	    enum enum_field_types type= (enum enum_field_types)$3;
	    Item *it= $4;
1212 1213 1214

	    for (uint i = max-$2 ; i < max ; i++)
	    {
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
	      lex->spcont->set_type(i, type);
	      if (! it)
	        lex->spcont->set_isset(i, FALSE);
	      else
	      {
	        sp_instr_set *in= new sp_instr_set(lex->sphead->instructions(),
	                                           i, it, type);

	        lex->sphead->add_instr(in);
	        lex->spcont->set_isset(i, TRUE);
	      }
1226
	    }
1227
	    $$.vars= $2;
1228
	    $$.conds= $$.hndlrs= $$.curs= 0;
1229 1230 1231 1232
	  }
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    YYTHD->lex->spcont->push_cond(&$2, $5);
1233
	    $$.vars= $$.hndlrs= $$.curs= 0;
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
	    $$.conds= 1;
	  }
	| DECLARE_SYM sp_handler_type HANDLER_SYM FOR_SYM
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_instr_hpush_jump *i=
              new sp_instr_hpush_jump(sp->instructions(), $2,
	                              ctx->current_framesize());

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

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

	      sp->add_instr(i);
	      sp->push_backpatch(i, lex->spcont->last_label()); /* Block end */
	    }
	    lex->sphead->backpatch(hlab);
1266
	    $$.vars= $$.conds= $$.curs= 0;
1267 1268 1269 1270
	    $$.hndlrs= $6;
	  }
	| DECLARE_SYM ident CURSOR_SYM FOR_SYM sp_cursor_stmt
	  {
1271 1272 1273 1274 1275 1276
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_instr_cpush *i= new sp_instr_cpush(sp->instructions(), $5);

	    sp->add_instr(i);
	    lex->spcont->push_cursor(&$2);
1277
	    $$.vars= $$.conds= $$.hndlrs= 0;
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
	    $$.curs= 1;
	  }
	;

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

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

	    if (lex->sql_command != SQLCOM_SELECT)
	    {
	      send_error(YYTHD, ER_SP_BAD_CURSOR_QUERY);
	      YYABORT;
	    }
	    if (lex->result)
	    {
	      send_error(YYTHD, ER_SP_BAD_CURSOR_SELECT);
	      YYABORT;
	    }
	    lex->sp_lex_in_use= TRUE;
	    $$= lex;
	    lex->sphead->restore_lex(YYTHD);
	  }
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 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
	;

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;
	  }
	| TEXT_STRING_literal
	  {		/* SQLSTATE */
	    uint len= ($1.length < sizeof($$->sqlstate)-1 ?
                       $1.length : sizeof($$->sqlstate)-1);

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
	      net_printf(YYTHD, ER_SP_COND_MISMATCH, $1.str);
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
	| NOT FOUND_SYM		/* SQLSTATEs 02??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::notfound;
	  }
	| SQLEXCEPTION_SYM	/* All other SQLSTATEs */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::exception;
1385 1386 1387 1388 1389 1390
	  }
	;

sp_decl_idents:
	  ident
	  {
1391
	    Lex->spcont->push_pvar(&$1, (enum_field_types)0, sp_param_in);
1392 1393 1394 1395
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
1396
	    Lex->spcont->push_pvar(&$3, (enum_field_types)0, sp_param_in);
1397 1398 1399 1400
	    $$= $1 + 1;
	  }
	;

1401 1402 1403 1404 1405
sp_opt_default:
	  /* Empty */ { $$ = NULL; }
        | DEFAULT expr { $$ = $2; }
	;

1406
sp_proc_stmt:
1407 1408 1409
	  {
	    Lex->sphead->reset_lex(YYTHD);
	  }
1410 1411 1412 1413
	  statement
	  {
	    LEX *lex= Lex;

1414 1415
	    if (lex->sql_command == SQLCOM_SELECT && !lex->result)
	    {
1416
	      /* We maybe have one or more SELECT without INTO */
1417
	      lex->sphead->m_multi_results= TRUE;
1418
	    }
1419 1420 1421 1422 1423
	    /* 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 ||
1424
		! lex->var_list.is_empty())
1425
	    {
1426 1427 1428 1429 1430
	      /* Currently we can't handle queries inside a FUNCTION,
	      ** because of the way table locking works.
	      ** This is unfortunate, and limits the usefulness of functions
	      ** a great deal, but it's nothing we can do about this at the
	      ** moment.
1431
	      */
1432 1433
	      if (lex->sphead->m_type == TYPE_ENUM_FUNCTION &&
		  lex->sql_command != SQLCOM_SET_OPTION)
1434
	      {
1435 1436 1437 1438 1439 1440 1441 1442 1443
		send_error(YYTHD, ER_SP_BADQUERY);
		YYABORT;
	      }
	      else
	      {
		sp_instr_stmt *i=new sp_instr_stmt(lex->sphead->instructions());

		i->set_lex(lex);
		lex->sphead->add_instr(i);
1444
		lex->sp_lex_in_use= TRUE;
1445 1446 1447
	      }
            }
	    lex->sphead->restore_lex(YYTHD);
1448
          }
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
	| RETURN_SYM expr
	  {
	    LEX *lex= Lex;

	    if (lex->sphead->m_type == TYPE_ENUM_PROCEDURE)
	    {
	      send_error(YYTHD, ER_SP_BADRETURN);
	      YYABORT;
	    }
	    else
	    {
1460 1461 1462
	      sp_instr_freturn *i=
	        new sp_instr_freturn(lex->sphead->instructions(),
		                     $2, lex->sphead->m_returns);
1463 1464

	      lex->sphead->add_instr(i);
1465
	      lex->sphead->m_has_return= TRUE;
1466 1467
	    }
	  }
1468
	| IF sp_if END IF {}
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
	| CASE_SYM WHEN_SYM
	  {
	    Lex->sphead->m_simple_case= FALSE;
	  }
	  sp_case END CASE_SYM {}
	| CASE_SYM expr WHEN_SYM
	  {
	    /* We "fake" this by using an anonymous variable which we
	       set to the expression. Note that all WHENs are evaluate
	       at the same frame level, so we then know that it's the
	       top-most variable in the frame. */
	    LEX *lex= Lex;
	    uint offset= lex->spcont->current_framesize();
	    sp_instr_set *i = new sp_instr_set(lex->sphead->instructions(),
	                                       offset, $2, MYSQL_TYPE_STRING);
	    LEX_STRING dummy;

	    dummy.str= (char *)"";
	    dummy.length= 0;
1488
	    lex->spcont->push_pvar(&dummy, MYSQL_TYPE_STRING, sp_param_in);
1489 1490 1491 1492 1493
	    lex->sphead->add_instr(i);
	    lex->sphead->m_simple_case= TRUE;
	  }
	  sp_case END CASE_SYM
	  {
1494
	    Lex->spcont->pop_pvar();
1495
	  }
1496 1497 1498 1499 1500
	| sp_labeled_control
	  {}
	| { /* Unlabeled controls get a secret label. */
	    LEX *lex= Lex;

1501
	    lex->spcont->push_label((char *)"", lex->sphead->instructions());
1502 1503 1504
	  }
	  sp_unlabeled_control
	  {
1505 1506
	    LEX *lex= Lex;

1507
	    lex->sphead->backpatch(lex->spcont->pop_label());
1508 1509 1510 1511
	  }
	| LEAVE_SYM IDENT
	  {
	    LEX *lex= Lex;
1512
	    sp_head *sp = lex->sphead;
1513 1514 1515 1516
	    sp_label_t *lab= lex->spcont->find_label($2.str);

	    if (! lab)
	    {
1517
	      net_printf(YYTHD, ER_SP_LILABEL_MISMATCH, "LEAVE", $2.str);
1518 1519 1520 1521
	      YYABORT;
	    }
	    else
	    {
1522
	      sp_instr_jump *i= new sp_instr_jump(sp->instructions());
1523

1524 1525
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
1526 1527 1528 1529 1530 1531 1532 1533 1534
	    }
	  }
	| ITERATE_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->find_label($2.str);

	    if (! lab)
	    {
1535
	      net_printf(YYTHD, ER_SP_LILABEL_MISMATCH, "ITERATE", $2.str);
1536 1537 1538 1539 1540
	      YYABORT;
	    }
	    else
	    {
	      uint ip= lex->sphead->instructions();
1541
	      sp_instr_jump *i= new sp_instr_jump(ip, lab->ip); /* Jump back */
1542 1543 1544 1545

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

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

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
	      net_printf(YYTHD, ER_SP_CURSOR_MISMATCH, $2.str);
	      YYABORT;
	    }
	    i= new sp_instr_cfetch(sp->instructions(), offset);
	    sp->add_instr(i);
	  }
	  sp_fetch_list
	  { }
1578
	| CLOSE_SYM ident
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cclose *i;

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

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

	    if (!spc || !(spv = spc->find_pvar(&$1)))
	    {
	      net_printf(YYTHD, ER_SP_UNDECLARED_VAR, $1.str);
	      YYABORT;
	    }
	    else
	    { /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

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

	    if (!spc || !(spv = spc->find_pvar(&$3)))
	    {
	      net_printf(YYTHD, ER_SP_UNDECLARED_VAR, $3.str);
	      YYABORT;
	    }
	    else
	    { /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	      spv->isset= TRUE;
	    }
	  }
1637
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1638

1639 1640 1641 1642 1643 1644 1645 1646
sp_if:
	  expr THEN_SYM
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, $1);

1647
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
            sp->add_instr(i);
	  }
	  sp_proc_stmts
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump *i = new sp_instr_jump(ip);

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

1665
	    lex->sphead->backpatch(lex->spcont->pop_label());
1666 1667 1668 1669 1670 1671 1672 1673 1674
	  }
	;

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

1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
sp_case:
	  expr THEN_SYM
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

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

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

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

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

sp_whens:
1715 1716 1717
	  /* Empty */ {}
	| WHEN_SYM sp_case {}
	| ELSE sp_proc_stmts {}
1718 1719
	;

1720 1721 1722 1723 1724 1725 1726 1727
sp_labeled_control:
	  IDENT ':'
	  {
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->find_label($1.str);

	    if (lab)
	    {
1728
	      net_printf(YYTHD, ER_SP_LABEL_REDEFINE, $1.str);
1729 1730 1731
	      YYABORT;
	    }
	    else
1732
	    {
1733 1734
	      lex->spcont->push_label($1.str,
	                              lex->sphead->instructions());
1735
	    }
1736 1737 1738 1739 1740 1741
	  }
	  sp_unlabeled_control IDENT
	  {
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->find_label($5.str);

pem@mysql.com's avatar
pem@mysql.com committed
1742 1743
	    if (!lab ||
	        my_strcasecmp(system_charset_info, $5.str, lab->name) != 0)
1744
	    {
1745
	      net_printf(YYTHD, ER_SP_LABEL_MISMATCH, $5.str);
1746 1747 1748 1749 1750
	      YYABORT;
	    }
	    else
	    {
	      lex->spcont->pop_label();
1751
	      lex->sphead->backpatch(lab);
1752 1753 1754 1755 1756
	    }
	  }
	;

sp_unlabeled_control:
1757 1758 1759 1760
	  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. */
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774

            Lex->spcont->push_label((char *)"", 0); /* For end of block */
	  }
	  sp_decls
	  sp_proc_stmts
	  END
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;

  	    sp->backpatch(ctx->pop_label());
	    ctx->pop_pvar($3.vars);
	    ctx->pop_cond($3.conds);
1775
	    ctx->pop_cursor($3.curs);
1776
	    if ($3.hndlrs)
1777 1778 1779
	      sp->add_instr(new sp_instr_hpop(sp->instructions(),$3.hndlrs));
	    if ($3.curs)
	      sp->add_instr(new sp_instr_cpop(sp->instructions(), $3.curs));
1780 1781
	  }
	| LOOP_SYM
1782 1783 1784 1785 1786 1787 1788 1789 1790
	  sp_proc_stmts END LOOP_SYM
	  {
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lab->ip);

	    lex->sphead->add_instr(i);
	  }
1791
	| WHILE_SYM expr DO_SYM
1792 1793
	  {
	    LEX *lex= Lex;
1794 1795
	    sp_head *sp= lex->sphead;
	    uint ip= sp->instructions();
1796 1797
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, $2);

1798 1799 1800
	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
            sp->add_instr(i);
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
	  }
	  sp_proc_stmts END WHILE_SYM
	  {
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lab->ip);

	    lex->sphead->add_instr(i);
	  }
1811 1812
	| REPEAT_SYM sp_proc_stmts UNTIL_SYM expr END REPEAT_SYM
	  {
1813 1814 1815 1816 1817 1818 1819
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, $4, lab->ip);

            lex->sphead->add_instr(i);
	  }
1820
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1821 1822

create2:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1823
 	'(' create2a {}
1824
	| opt_create_table_options create3 {}
venu@myvenu.com's avatar
venu@myvenu.com committed
1825 1826 1827 1828 1829 1830
	| LIKE table_ident
      	  {
      	    LEX *lex=Lex;
     	    if (!(lex->name= (char *)$2))
              YYABORT;
    	  }
1831 1832 1833 1834 1835 1836
	| '(' LIKE table_ident ')'
      	  {
      	    LEX *lex=Lex;
     	    if (!(lex->name= (char *)$3))
              YYABORT;
    	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1837
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1838

serg@serg.mylan's avatar
serg@serg.mylan committed
1839 1840
create2a:
        field_list ')' opt_create_table_options create3 {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1841
	|  create_select ')' { Select->set_braces(1);} union_opt {}
1842
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1843 1844

create3:
1845
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
1846
	| opt_duplicate opt_as     create_select
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1847
          { Select->set_braces(0);} union_clause {}
serg@serg.mylan's avatar
serg@serg.mylan committed
1848
	| opt_duplicate opt_as '(' create_select ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1849
          { Select->set_braces(1);} union_opt {}
1850 1851
        ;

serg@serg.mylan's avatar
serg@serg.mylan committed
1852
create_select:
1853
          SELECT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1854
          {
1855 1856
	    LEX *lex=Lex;
	    lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
1857 1858 1859 1860
	    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;
1861
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
1862
	    mysql_init_select(lex);
1863
	    lex->current_select->parsing_place= SELECT_LEX_NODE::SELECT_LIST;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1864
          }
1865 1866 1867 1868
          select_options select_item_list
	  {
	    Select->parsing_place= SELECT_LEX_NODE::NO_MATTER;
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1869
	  opt_select_from
1870
	  { Lex->current_select->table_list.push_front(&Lex->save_list); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1871
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1872

1873 1874
opt_as:
	/* empty */ {}
1875
	| AS	    {};
1876

1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
	  COLLATE_SYM collation_name_or_default	
	  { Lex->create_info.table_charset=$2; }
1888 1889 1890
	| opt_default charset charset_name_or_default
	  { Lex->create_info.table_charset=$3; }
	;
1891

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1892
opt_table_options:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1893
	/* empty */	 { $$= 0; }
1894
	| table_options  { $$= $1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1895 1896 1897

table_options:
	table_option	{ $$=$1; }
1898
	| table_option table_options { $$= $1 | $2; };
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1899

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1900
table_option:
1901
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1902 1903

opt_if_not_exists:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1904
	/* empty */	 { $$= 0; }
1905
	| IF NOT EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1906 1907 1908

opt_create_table_options:
	/* empty */
1909
	| create_table_options;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1910

1911 1912 1913 1914
create_table_options_space_separated:
	create_table_option
	| create_table_option create_table_options_space_separated;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1915 1916
create_table_options:
	create_table_option
1917
	| create_table_option     create_table_options
1918
	| create_table_option ',' create_table_options;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1919 1920

create_table_option:
1921 1922 1923 1924
	TYPE_SYM opt_equal table_types          { Lex->create_info.db_type= $3; }
	| MAX_ROWS opt_equal ulonglong_num	{ Lex->create_info.max_rows= $3; Lex->create_info.used_fields|= HA_CREATE_USED_MAX_ROWS;}
	| MIN_ROWS opt_equal ulonglong_num	{ Lex->create_info.min_rows= $3; Lex->create_info.used_fields|= HA_CREATE_USED_MIN_ROWS;}
	| AVG_ROW_LENGTH opt_equal ULONG_NUM	{ Lex->create_info.avg_row_length=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AVG_ROW_LENGTH;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
1925 1926
	| PASSWORD opt_equal TEXT_STRING_sys	{ Lex->create_info.password=$3.str; }
	| COMMENT_SYM opt_equal TEXT_STRING_sys	{ Lex->create_info.comment=$3.str; }
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
	| PACK_KEYS_SYM opt_equal ULONG_NUM	{ Lex->create_info.table_options|= $3 ? HA_OPTION_PACK_KEYS : HA_OPTION_NO_PACK_KEYS; Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;}
	| PACK_KEYS_SYM opt_equal DEFAULT	{ Lex->create_info.table_options&= ~(HA_OPTION_PACK_KEYS | HA_OPTION_NO_PACK_KEYS); Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;}
	| CHECKSUM_SYM opt_equal ULONG_NUM	{ Lex->create_info.table_options|= $3 ? HA_OPTION_CHECKSUM : HA_OPTION_NO_CHECKSUM; }
	| DELAY_KEY_WRITE_SYM opt_equal ULONG_NUM { Lex->create_info.table_options|= $3 ? HA_OPTION_DELAY_KEY_WRITE : HA_OPTION_NO_DELAY_KEY_WRITE; }
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3; }
	| RAID_TYPE opt_equal raid_types	{ Lex->create_info.raid_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| RAID_CHUNKS opt_equal ULONG_NUM	{ Lex->create_info.raid_chunks= $3; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| RAID_CHUNKSIZE opt_equal ULONG_NUM	{ Lex->create_info.raid_chunksize= $3*RAID_BLOCK_SIZE; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| UNION_SYM opt_equal '(' table_list ')'
1937 1938 1939
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
1940 1941
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
1942 1943
	    lex->create_info.merge_list.elements--;
	    lex->create_info.merge_list.first= (byte*) (table_list->next);
1944 1945
	    lex->select_lex.table_list.elements=1;
	    lex->select_lex.table_list.next= (byte**) &(table_list->next);
1946
	    table_list->next=0;
1947
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
1948
	  }
1949
	| opt_default charset opt_equal charset_name_or_default
1950
	  {
1951 1952 1953
	    Lex->create_info.table_charset= $4;
	    Lex->create_info.used_fields|= HA_CREATE_USED_CHARSET;
	  }
1954 1955 1956 1957 1958
	| COLLATE_SYM opt_equal collation_name_or_default
	  {
	    Lex->create_info.table_charset= $3;
	    Lex->create_info.used_fields|= HA_CREATE_USED_CHARSET;
	  }
1959
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
1960 1961 1962
	| DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys
	  { Lex->create_info.data_file_name= $4.str; }
	| INDEX DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.index_file_name= $4.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1963 1964 1965 1966 1967 1968

table_types:
	ISAM_SYM	{ $$= DB_TYPE_ISAM; }
	| MYISAM_SYM	{ $$= DB_TYPE_MYISAM; }
	| MERGE_SYM	{ $$= DB_TYPE_MRG_MYISAM; }
	| HEAP_SYM	{ $$= DB_TYPE_HEAP; }
1969
	| MEMORY_SYM	{ $$= DB_TYPE_HEAP; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1970
	| BERKELEY_DB_SYM { $$= DB_TYPE_BERKELEY_DB; }
1971
	| INNOBASE_SYM  { $$= DB_TYPE_INNODB; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1972 1973 1974 1975 1976

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
1977
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1978 1979 1980 1981

raid_types:
	RAID_STRIPED_SYM { $$= RAID_TYPE_0; }
	| RAID_0_SYM	 { $$= RAID_TYPE_0; }
1982
	| ULONG_NUM	 { $$=$1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1983

1984 1985 1986
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
1987
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
1988

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1989
opt_select_from:
1990
	opt_limit_clause {}
1991
	| FROM DUAL_SYM {}
1992
	| select_from select_lock_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1993 1994

udf_func_type:
1995
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
1996
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1997 1998 1999 2000

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
2001
	| INT_SYM {$$ = (int) INT_RESULT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2002 2003 2004

field_list:
	  field_list_item
2005
	| field_list ',' field_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2006 2007 2008


field_list_item:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2009
	   column_def
2010 2011 2012 2013
         | key_def
         ;

column_def:
2014
	  field_spec check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2015 2016 2017 2018
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2019
	;
2020 2021

key_def:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2022
	key_type opt_ident key_alg '(' key_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2023
	  {
2024
	    LEX *lex=Lex;
2025
	    lex->key_list.push_back(new Key($1,$2, $3, lex->col_list));
2026
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2027
	  }
2028
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2029
	  {
2030 2031 2032 2033 2034 2035 2036 2037
	    LEX *lex=Lex;
	    lex->key_list.push_back(new foreign_key($4, lex->col_list,
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2038
	  }
2039
	| opt_constraint check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2040 2041
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
2042 2043 2044 2045 2046 2047 2048
	  }
	;

check_constraint:
	/* empty */
	| CHECK_SYM expr
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2049 2050 2051

opt_constraint:
	/* empty */
2052
	| CONSTRAINT opt_ident;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2053 2054 2055 2056

field_spec:
	field_ident
	 {
2057 2058
	   LEX *lex=Lex;
	   lex->length=lex->dec=0; lex->type=0; lex->interval=0;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2059 2060
	   lex->default_value=0;
	   lex->comment=0;
2061
	   lex->charset=NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2062 2063 2064
	 }
	type opt_attribute
	{
2065
	  LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2066
	  if (add_field_to_list(lex->thd, $1.str,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2067
				(enum enum_field_types) $3,
2068
				lex->length,lex->dec,lex->type,
2069
				lex->default_value, lex->comment,
2070 2071
				lex->change,lex->interval,lex->charset,
				lex->uint_geom_type))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2072
	    YYABORT;
2073
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2074 2075

type:
2076
	int_type opt_len field_options	{ $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2077 2078 2079 2080 2081 2082
	| 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; }
2083 2084
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
2085
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2086 2087 2088
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
2089
	| nchar '(' NUM ')'		{ Lex->length=$3.str;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2090
					  $$=FIELD_TYPE_STRING;
2091
					  Lex->charset=national_charset_info; }
2092
	| nchar				{ Lex->length=(char*) "1";
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2093
					  $$=FIELD_TYPE_STRING;
2094
					  Lex->charset=national_charset_info; }
2095
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
2096
					  Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2097 2098 2099
					  $$=FIELD_TYPE_STRING; }
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
					  $$=FIELD_TYPE_VAR_STRING; }
2100 2101
	| nvarchar '(' NUM ')'		{ Lex->length=$3.str;
					  $$=FIELD_TYPE_VAR_STRING;
2102
					  Lex->charset=national_charset_info; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2103
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
2104
					  Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2105
					  $$=FIELD_TYPE_VAR_STRING; }
2106
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2107 2108
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
2109 2110
	| TIMESTAMP
	  {
2111
	    if (YYTHD->variables.sql_mode & MODE_SAPDB)
2112 2113
	      $$=FIELD_TYPE_DATETIME;
	    else
2114
	      $$=FIELD_TYPE_TIMESTAMP;
2115
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2116 2117 2118
	| TIMESTAMP '(' NUM ')'		{ Lex->length=$3.str;
					  $$=FIELD_TYPE_TIMESTAMP; }
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
2119
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2120
					  $$=FIELD_TYPE_TINY_BLOB; }
2121
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2122
					  $$=FIELD_TYPE_BLOB; }
2123
	| GEOMETRY_SYM			{ Lex->charset=&my_charset_bin;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2124
					  Lex->uint_geom_type= (uint) Field::GEOM_GEOMETRY;
2125
					  $$=FIELD_TYPE_GEOMETRY; }
2126
	| GEOMETRYCOLLECTION		{ Lex->charset=&my_charset_bin;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2127
					  Lex->uint_geom_type= (uint) Field::GEOM_GEOMETRYCOLLECTION;
2128
					  $$=FIELD_TYPE_GEOMETRY; }
2129
	| POINT_SYM			{ Lex->charset=&my_charset_bin;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2130
					  Lex->uint_geom_type= (uint) Field::GEOM_POINT;
2131 2132
					  $$=FIELD_TYPE_GEOMETRY; }
	| MULTIPOINT			{ Lex->charset=&my_charset_bin;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2133
					  Lex->uint_geom_type= (uint) Field::GEOM_MULTIPOINT;
2134 2135
					  $$=FIELD_TYPE_GEOMETRY; }
	| LINESTRING			{ Lex->charset=&my_charset_bin;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2136
					  Lex->uint_geom_type= (uint) Field::GEOM_LINESTRING;
2137 2138
					  $$=FIELD_TYPE_GEOMETRY; }
	| MULTILINESTRING		{ Lex->charset=&my_charset_bin;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2139
					  Lex->uint_geom_type= (uint) Field::GEOM_MULTILINESTRING;
2140 2141
					  $$=FIELD_TYPE_GEOMETRY; }
	| POLYGON			{ Lex->charset=&my_charset_bin;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2142
					  Lex->uint_geom_type= (uint) Field::GEOM_POLYGON;
2143 2144
					  $$=FIELD_TYPE_GEOMETRY; }
	| MULTIPOLYGON			{ Lex->charset=&my_charset_bin;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2145
					  Lex->uint_geom_type= (uint) Field::GEOM_MULTIPOLYGON;
2146
					  $$=FIELD_TYPE_GEOMETRY; }
2147
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2148
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
2149
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2150
					  $$=FIELD_TYPE_LONG_BLOB; }
2151
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2152
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
2153 2154
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
2155
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
2156 2157
	| MEDIUMTEXT opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| LONGTEXT opt_binary		{ $$=FIELD_TYPE_LONG_BLOB; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2158 2159 2160 2161
	| DECIMAL_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
	| NUMERIC_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
2162 2163
	| FIXED_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
2164
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2165
	  {
2166 2167
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2168 2169
	    $$=FIELD_TYPE_ENUM;
	  }
2170
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2171
	  {
2172 2173
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2174
	    $$=FIELD_TYPE_SET;
2175
	  }
2176
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
2177 2178 2179 2180 2181 2182
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
2183
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2184 2185 2186

char:
	CHAR_SYM {}
2187 2188 2189 2190 2191 2192
	;

nchar:
	NCHAR_SYM {}
	| NATIONAL_SYM CHAR_SYM {}
	;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2193

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2194 2195 2196
varchar:
	char VARYING {}
	| VARCHAR {}
2197 2198 2199 2200
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2201
	| NVARCHAR_SYM {}
2202 2203 2204 2205
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2206 2207 2208 2209 2210 2211

int_type:
	INT_SYM		{ $$=FIELD_TYPE_LONG; }
	| TINYINT	{ $$=FIELD_TYPE_TINY; }
	| SMALLINT	{ $$=FIELD_TYPE_SHORT; }
	| MEDIUMINT	{ $$=FIELD_TYPE_INT24; }
2212
	| BIGINT	{ $$=FIELD_TYPE_LONGLONG; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2213 2214

real_type:
2215
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2216 2217
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
2218
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2219 2220 2221 2222 2223


float_options:
	/* empty */		{}
	| '(' NUM ')'		{ Lex->length=$2.str; }
2224
	| precision		{};
2225 2226 2227 2228 2229 2230

precision:
	'(' NUM ',' NUM ')'
	{
	  LEX *lex=Lex;
	  lex->length=$2.str; lex->dec=$4.str;
2231
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2232 2233 2234

field_options:
	/* empty */		{}
2235
	| field_opt_list	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2236 2237 2238

field_opt_list:
	field_opt_list field_option {}
2239
	| field_option {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2240 2241

field_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2242
	SIGNED_SYM	{}
2243
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
2244
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2245 2246

opt_len:
2247 2248
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2249 2250 2251

opt_precision:
	/* empty */	{}
2252
	| precision	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2253 2254 2255

opt_attribute:
	/* empty */ {}
2256
	| opt_attribute_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2257 2258 2259

opt_attribute_list:
	opt_attribute_list attribute {}
2260
	| attribute;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2261 2262 2263 2264 2265 2266

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
	| NOT NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
	| DEFAULT literal { Lex->default_value=$2; }
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
2267
	| SERIAL_SYM DEFAULT VALUE_SYM
2268 2269
	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; }
	| opt_primary KEY_SYM { Lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2270
	| UNIQUE_SYM	  { Lex->type|= UNIQUE_FLAG; }
2271
	| UNIQUE_SYM KEY_SYM { Lex->type|= UNIQUE_KEY_FLAG; }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2272
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= &$2; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2273 2274
	| COLLATE_SYM collation_name
	  {
2275
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
	    {
	      net_printf(YYTHD,ER_COLLATION_CHARSET_MISMATCH,
			 $2->name,Lex->charset->csname);
	      YYABORT;
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2287

2288 2289 2290 2291
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
2292

2293
charset_name:
2294
	ident_or_text
2295
	{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2296
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
2297
	  {
2298
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,$1.str);
2299 2300
	    YYABORT;
	  }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2301 2302 2303
	}
	| BINARY { $$= &my_charset_bin; }
	;
2304

2305 2306 2307
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2308

2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326

old_or_new_charset_name:
	ident_or_text
	{
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))) &&
	      !($$=get_old_charset_by_name($1.str)))
	  {
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,$1.str);
	    YYABORT;
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

2327
collation_name:
2328
	ident_or_text
2329 2330 2331
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
2332
	    net_printf(YYTHD,ER_UNKNOWN_COLLATION,$1.str);
2333 2334 2335 2336
	    YYABORT;
	  }
	};

2337 2338
opt_collate:
	/* empty */	{ $$=NULL; }
2339
	| COLLATE_SYM collation_name_or_default { $$=$2; }
2340 2341
	;

2342 2343 2344 2345
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

2346 2347 2348 2349
opt_default:
	/* empty */	{}
	| DEFAULT	{};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2350
opt_binary:
2351
	/* empty */			{ Lex->charset=NULL; }
2352 2353 2354
	| ASCII_SYM			{ Lex->charset=&my_charset_latin1; }
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
	| BINARY			{ Lex->charset=&my_charset_bin; }
2355 2356
	| UNICODE_SYM
	{
2357
	  if (!(Lex->charset=get_charset_by_csname("ucs2",MY_CS_PRIMARY,MYF(0))))
2358 2359 2360 2361 2362
	  {
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,"ucs2");
	    YYABORT;
	  }
	}
2363
	| charset charset_name	{ Lex->charset=$2; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2364

2365 2366 2367
opt_primary:
	/* empty */
	| PRIMARY_SYM
2368
	;
2369

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2370
references:
2371 2372
	REFERENCES table_ident
	{
2373
	  LEX *lex=Lex;
2374 2375 2376 2377 2378 2379
	  lex->fk_delete_opt= lex->fk_update_opt= lex->fk_match_option= 0;
	  lex->ref_list.empty();
	}
	opt_ref_list
	{
	  $$=$2;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2380
	};
2381

2382
opt_ref_list:
2383
	/* empty */ opt_on_delete {}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2384
	| '(' ref_list ')' opt_on_delete {};
2385 2386 2387

ref_list:
	ref_list ',' ident	{ Lex->ref_list.push_back(new key_part_spec($3.str)); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2388
	| ident			{ Lex->ref_list.push_back(new key_part_spec($1.str)); };
2389

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2390 2391 2392

opt_on_delete:
	/* empty */ {}
2393
	| opt_on_delete_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2394 2395 2396

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
2397
	| opt_on_delete_item {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2398 2399

opt_on_delete_item:
2400 2401 2402 2403
	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; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2404
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2405 2406

delete_option:
2407 2408 2409 2410
	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; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2411
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2412 2413 2414 2415

key_type:
	opt_constraint PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
	| key_or_index			    { $$= Key::MULTIPLE; }
2416 2417
	| FULLTEXT_SYM			    { $$= Key::FULLTEXT; }
	| FULLTEXT_SYM key_or_index	    { $$= Key::FULLTEXT; }
2418 2419
	| SPATIAL_SYM			    { $$= Key::SPATIAL; }
	| SPATIAL_SYM key_or_index	    { $$= Key::SPATIAL; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2420
	| opt_constraint UNIQUE_SYM	    { $$= Key::UNIQUE; }
2421
	| opt_constraint UNIQUE_SYM key_or_index { $$= Key::UNIQUE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2422 2423 2424

key_or_index:
	KEY_SYM {}
2425
	| INDEX {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2426 2427 2428 2429

keys_or_index:
	KEYS {}
	| INDEX {}
2430
	| INDEXES {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2431

2432
opt_unique_or_fulltext:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2433 2434
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
serg@serg.mylan's avatar
serg@serg.mylan committed
2435 2436 2437
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
	| SPATIAL_SYM	{ $$= Key::SPATIAL; }
        ;
2438 2439

key_alg:
2440
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
2441
	| USING opt_btree_or_rtree { $$= $2; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2442
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
2443 2444 2445 2446

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
	| RTREE_SYM	{ $$= HA_KEY_ALG_RTREE; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2447
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2448 2449 2450

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
2451
	| key_part order_dir		{ Lex->col_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2452 2453 2454

key_part:
	ident			{ $$=new key_part_spec($1.str); }
2455
	| ident '(' NUM ')'	{ $$=new key_part_spec($1.str,(uint) atoi($3.str)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2456 2457 2458

opt_ident:
	/* empty */	{ $$=(char*) 0; }	/* Defaultlength */
2459
	| field_ident	{ $$=$1.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2460

2461 2462 2463
opt_component:
	/* empty */	 { $$.str= 0; $$.length= 0; }
	| '.' ident	 { $$=$2; };
2464

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2465 2466
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
2467
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2468 2469 2470 2471 2472 2473 2474 2475

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
2476
	  THD *thd= YYTHD;
2477
	  LEX *lex= thd->lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2478 2479
	  lex->sql_command = SQLCOM_ALTER_TABLE;
	  lex->name=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2480 2481
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2482 2483 2484 2485 2486 2487 2488
	    YYABORT;
	  lex->drop_primary=0;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
	  lex->drop_list.empty();
	  lex->alter_list.empty();
2489
          lex->select_lex.init_order();
2490
	  lex->select_lex.db=lex->name=0;
2491
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2492
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
2493
	  lex->create_info.table_charset= thd->variables.collation_database;
2494
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
2495 2496
          lex->alter_keys_onoff=LEAVE_AS_IS;
          lex->simple_alter=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2497
	}
2498 2499
	alter_list
	{}
2500
	| ALTER DATABASE ident opt_create_database_options
2501 2502 2503 2504
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
	    lex->name=$3.str;
2505
	  }
2506
	| ALTER PROCEDURE ident
2507 2508 2509 2510
	  /* QQ Characteristics missing for now */
	  opt_restrict
	  {
	    LEX *lex=Lex;
2511

2512 2513 2514
	    /* This is essensially an no-op right now, since we haven't
	       put the characteristics in yet. */
	    lex->sql_command= SQLCOM_ALTER_PROCEDURE;
2515
	    lex->udf.name= $3;
2516
	  }
2517 2518 2519 2520 2521
	| ALTER FUNCTION_SYM ident
	  /* QQ Characteristics missing for now */
	  opt_restrict
	  {
	    LEX *lex=Lex;
2522

2523 2524 2525 2526 2527
	    /* This is essensially an no-op right now, since we haven't
	       put the characteristics in yet. */
	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
	    lex->udf.name= $3;
	  }
2528
	;
2529

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2530
alter_list:
2531
        | alter_list_item
2532
	| alter_list ',' alter_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2533 2534

add_column:
2535
	ADD opt_column { Lex->change=0; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2536 2537

alter_list_item:
2538 2539
	add_column column_def opt_place { Lex->simple_alter=0; }
	| ADD key_def { Lex->simple_alter=0; }
2540
	| add_column '(' field_list ')'      { Lex->simple_alter=0; }
2541 2542 2543 2544 2545
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
	     lex->change= $3.str; lex->simple_alter=0;
	  }
2546
          field_spec opt_place
2547 2548 2549 2550
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
            lex->length=lex->dec=0; lex->type=0; lex->interval=0;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2551 2552
            lex->default_value=0;
	    lex->comment=0;
2553
	    lex->charset= NULL;
2554
            lex->simple_alter=0;
2555 2556 2557 2558
          }
          type opt_attribute
          {
            LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2559
            if (add_field_to_list(lex->thd,$3.str,
2560 2561 2562
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
                                  lex->default_value, lex->comment,
2563 2564
				  $3.str, lex->interval, lex->charset,
				  lex->uint_geom_type))
2565 2566 2567
	       YYABORT;
          }
          opt_place
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2568
	| DROP opt_column field_ident opt_restrict
2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
	  {
	    LEX *lex=Lex;
	    lex->drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
					    $3.str)); lex->simple_alter=0;
	  }
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
	    lex->drop_primary=1; lex->simple_alter=0;
	  }
2579
	| DROP FOREIGN KEY_SYM opt_ident { Lex->simple_alter=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2580
	| DROP key_or_index field_ident
2581 2582 2583 2584 2585 2586
	  {
	    LEX *lex=Lex;
	    lex->drop_list.push_back(new Alter_drop(Alter_drop::KEY,
						    $3.str));
	    lex->simple_alter=0;
	  }
2587 2588
	| DISABLE_SYM KEYS { Lex->alter_keys_onoff=DISABLE; }
	| ENABLE_SYM KEYS  { Lex->alter_keys_onoff=ENABLE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2589
	| ALTER opt_column field_ident SET DEFAULT literal
2590 2591 2592 2593 2594
	  {
	    LEX *lex=Lex;
	    lex->alter_list.push_back(new Alter_column($3.str,$6));
	    lex->simple_alter=0;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2595
	| ALTER opt_column field_ident DROP DEFAULT
2596 2597 2598 2599 2600
	  {
	    LEX *lex=Lex;
	    lex->alter_list.push_back(new Alter_column($3.str,(Item*) 0));
	    lex->simple_alter=0;
	  }
2601
	| RENAME opt_to table_ident
2602
	  {
2603
	    LEX *lex=Lex;
2604
	    lex->select_lex.db=$3->db.str;
2605
	    lex->name= $3->table.str;
2606
	  }
2607
        | create_table_options_space_separated { Lex->simple_alter=0; }
2608
	| order_clause         { Lex->simple_alter=0; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2609 2610 2611

opt_column:
	/* empty */	{}
2612
	| COLUMN_SYM	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2613 2614 2615

opt_ignore:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
2616
	| IGNORE_SYM	{ Lex->duplicates=DUP_IGNORE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2617 2618 2619 2620

opt_restrict:
	/* empty */	{}
	| RESTRICT	{}
2621
	| CASCADE	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2622 2623 2624 2625

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
2626
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2627 2628 2629 2630

opt_to:
	/* empty */	{}
	| TO_SYM	{}
2631
	| EQ		{}
2632
	| AS		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2633

nick@mysql.com's avatar
nick@mysql.com committed
2634
/*
2635 2636
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
  To use UNTIL, one must use START SLAVE, not SLAVE START.
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2637 2638
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2639
slave:
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649
	  START_SYM SLAVE slave_thread_opts 
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_START;
	    lex->type = 0;
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2650 2651 2652 2653 2654 2655
        | STOP_SYM SLAVE slave_thread_opts
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_STOP;
	    lex->type = 0;
          }
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
         }
	| SLAVE STOP_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_STOP;
	   lex->type = 0;
         }
        ;

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2670

2671 2672 2673 2674 2675
start:
	START_SYM TRANSACTION_SYM { Lex->sql_command = SQLCOM_BEGIN;}
	{}
	;

2676
slave_thread_opts:
2677 2678
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
2679
        {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2680
	;
2681 2682

slave_thread_opt_list:
2683
	slave_thread_opt
2684 2685
	| slave_thread_opt_list ',' slave_thread_opt
	;
2686 2687

slave_thread_opt:
2688
	/*empty*/	{}
2689
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
2690
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
2691
	;
2692

2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714
slave_until:
	/*empty*/	{}
	| UNTIL_SYM slave_until_opts
          {
            LEX *lex=Lex;
            if ((lex->mi.log_file_name || lex->mi.pos) &&
                (lex->mi.relay_log_name || lex->mi.relay_log_pos) ||
                !((lex->mi.log_file_name && lex->mi.pos) ||
                  (lex->mi.relay_log_name && lex->mi.relay_log_pos)))
            {
               send_error(lex->thd, ER_BAD_SLAVE_UNTIL_COND);
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


2715 2716 2717 2718 2719
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2720
	table_list FROM TEXT_STRING_sys
2721 2722
        {
	  Lex->backup_dir = $6.str;
2723
        };
2724

2725 2726 2727 2728 2729
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2730
	table_list TO_SYM TEXT_STRING_sys
2731 2732
        {
	  Lex->backup_dir = $6.str;
2733
        };
2734

2735 2736 2737 2738 2739 2740
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
2741 2742
	table_list opt_checksum_type
        {}
2743 2744
	;

2745 2746 2747 2748 2749 2750
opt_checksum_type:
        /* nothing */  { Lex->check_opt.flags= 0; }
	| QUICK        { Lex->check_opt.flags= T_QUICK; }
	| EXTENDED_SYM { Lex->check_opt.flags= T_EXTEND; }
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2751
repair:
2752
	REPAIR opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2753
	{
2754 2755
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
2756
           lex->no_write_to_binlog= $2;
2757
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2758
	}
2759 2760 2761
	table_list opt_mi_repair_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2762

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
2763
opt_mi_repair_type:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2764
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
2765
	| mi_repair_types {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2766

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
2767 2768
mi_repair_types:
	mi_repair_type {}
2769
	| mi_repair_type mi_repair_types {};
2770

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
2771
mi_repair_type:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
2772
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
2773
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
2774
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2775 2776

analyze:
2777
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2778
	{
2779 2780
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
2781
           lex->no_write_to_binlog= $2;
2782
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2783
	}
2784 2785 2786
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2787 2788

check:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2789
	CHECK_SYM table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2790
	{
2791 2792 2793
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECK;
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2794
	}
2795 2796 2797
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2798

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
2799 2800
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
2801
	| mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
2802 2803 2804

mi_check_types:
	mi_check_type {}
2805
	| mi_check_type mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
2806 2807 2808 2809 2810 2811

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; }
2812
	| CHANGED  { Lex->check_opt.flags|= T_CHECK_ONLY_CHANGED; };
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
2813

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2814
optimize:
2815
	OPTIMIZE opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2816
	{
2817 2818
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2819
           lex->no_write_to_binlog= $2;
2820
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2821
	}
2822 2823 2824
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2825

2826 2827 2828
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
2829
	| LOCAL_SYM  { $$= 1; }
2830 2831
	;

2832 2833 2834 2835 2836
rename:
	RENAME table_or_tables
	{
	   Lex->sql_command=SQLCOM_RENAME_TABLE;
	}
2837 2838 2839
	table_to_table_list
	{}
	;
2840 2841 2842

table_to_table_list:
	table_to_table
2843
	| table_to_table_list ',' table_to_table;
2844 2845 2846

table_to_table:
	table_ident TO_SYM table_ident
2847
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2848
	  LEX *lex=Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2849
	  SELECT_LEX *sl= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2850 2851 2852 2853
	  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))
2854
	    YYABORT;
2855
	};
2856

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2857
preload:
2858
	LOAD INDEX INTO CACHE_SYM
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871
	{
	  LEX *lex=Lex;
	  lex->sql_command=SQLCOM_PRELOAD_KEYS;
	}
	preload_list
	{}
	;

preload_list:
	preload_keys
	| preload_list ',' preload_keys;

preload_keys:
2872
	table_ident preload_keys_spec opt_ignore_leaves
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2873 2874 2875
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2876 2877
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2878 2879 2880 2881 2882 2883 2884
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

preload_keys_spec:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2885
	keys_or_index { Select->interval_list.empty(); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2886
	preload_key_list_or_empty
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2887 2888 2889 2890 2891
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
	  sel->use_index= sel->interval_list;
	  sel->use_index_ptr= &sel->use_index;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2892
	}
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2893 2894 2895 2896 2897 2898 2899
	;

preload_key_list_or_empty:
	/* empty */
	| '(' key_usage_list2 ')' {}
	;

2900
opt_ignore_leaves:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2901 2902 2903 2904 2905
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2906
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2907
  Select : retrieve data from table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2908 2909 2910 2911
*/


select:
2912 2913 2914 2915 2916
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
	  lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2917 2918
	}
	;
2919

2920
/* Need select_init2 for subselects. */
2921
select_init:
2922
	SELECT_SYM select_init2
2923
	|
2924 2925
	'(' SELECT_SYM select_part2 ')'
	  {
2926
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2927
            SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2928
	    if (sel->set_braces(1))
2929 2930 2931 2932
	    {
	      send_error(lex->thd, ER_SYNTAX_ERROR);
	      YYABORT;
	    }
2933 2934
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
2935 2936 2937 2938
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
2939
            /* select in braces, can't contain global parameters */
2940 2941 2942
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2943
          } union_opt;
2944

2945 2946
select_init2:
	select_part2
2947
        {
2948
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2949
          SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2950
          if (lex->current_select->set_braces(0))
2951 2952 2953 2954
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
2955 2956
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
2957 2958 2959 2960
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
2961
	}
2962
	union_clause
2963 2964
	;

2965
select_part2:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2966 2967
	{
	  LEX *lex=Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2968
	  SELECT_LEX * sel= lex->current_select;
2969
	  if (lex->current_select == &lex->select_lex)
2970
	    lex->lock_option= TL_READ; /* Only for global SELECT */
2971 2972
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
2973 2974 2975 2976 2977
	  lex->current_select->parsing_place= SELECT_LEX_NODE::SELECT_LIST;
	}
	select_options select_item_list
	{
	  Select->parsing_place= SELECT_LEX_NODE::NO_MATTER;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2978
	}
2979
	select_into select_lock_type;
2980

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2981
select_into:
2982 2983 2984 2985 2986 2987
	opt_limit_clause {}
	| FROM DUAL_SYM /* oracle compatibility: oracle always requires FROM
                           clause, and DUAL is system table without fields.
                           Is "SELECT 1 FROM DUAL" any better than
                           "SELECT 1" ? Hmmm :) */
        | into
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2988
	| select_from
2989 2990
	| into select_from
	| select_from into;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2991 2992

select_from:
2993
	FROM join_table_list where_clause group_clause having_clause opt_order_clause opt_limit_clause procedure_clause;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2994 2995 2996

select_options:
	/* empty*/
2997
	| select_option_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2998 2999 3000

select_option_list:
	select_option_list select_option
3001
	| select_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3002 3003

select_option:
3004
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
3005 3006 3007 3008 3009 3010
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
3011 3012 3013
	| DISTINCT	{ Select->options|= SELECT_DISTINCT; }
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025
	| 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;
	  }
3026
	| SQL_NO_CACHE_SYM { Lex->uncacheable(); }
3027 3028 3029 3030
	| SQL_CACHE_SYM
	  {
	    Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
	  }
3031 3032
	| ALL		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3033

monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
3034 3035 3036
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
3037 3038
	  {
	    LEX *lex=Lex;
3039
	    lex->current_select->set_lock_for_tables(TL_WRITE);
3040
	    lex->safe_to_cache_query=0;
3041
	  }
3042
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
3043 3044
	  {
	    LEX *lex=Lex;
3045 3046
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
3047
	    lex->safe_to_cache_query=0;
3048 3049
	  }
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
3050

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3051 3052 3053 3054 3055
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
3056 3057
	    THD *thd= YYTHD;
	    if (add_item_to_list(thd, new Item_field(NULL, NULL, "*")))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3058
	      YYABORT;
pem@mysql.com's avatar
pem@mysql.com committed
3059
	    (thd->lex->current_select->with_wild)++;
3060
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3061 3062 3063 3064 3065


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3066
	    if (add_item_to_list(YYTHD, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3067 3068
	      YYABORT;
	    if ($4.str)
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3069
	      $2->set_name($4.str,$4.length,system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3070
	    else if (!$2->name)
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3071
	      $2->set_name($1,(uint) ($3 - $1), YYTHD->charset());
3072
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3073 3074

remember_name:
3075
	{ $$=(char*) Lex->tok_start; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3076 3077

remember_end:
3078
	{ $$=(char*) Lex->tok_end; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3079 3080 3081

select_item2:
	table_wild	{ $$=$1; } /* table.* */
3082
	| expr		{ $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3083 3084

select_alias:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3085 3086 3087 3088 3089 3090
	/* empty */		{ $$.str=0;}
	| AS ident		{ $$=$2; }
	| AS TEXT_STRING_sys	{ $$=$2; }
	| ident			{ $$=$1; }
	| TEXT_STRING_sys	{ $$=$1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3091 3092 3093

optional_braces:
	/* empty */ {}
3094
	| '(' ')' {};
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3095

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3096
/* all possible expressions */
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
3097 3098 3099 3100
expr:	
	expr_expr	{ $$= $1; }
	| simple_expr	{ $$= $1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3101

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
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; }
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3114 3115
/* expressions that begin with 'expr' */
expr_expr:
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
3116
	 expr IN_SYM '(' expr_list ')'
3117
	  { $4->push_front($1); $$= new Item_func_in(*$4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3118
	| expr NOT IN_SYM '(' expr_list ')'
3119
	  { $5->push_front($1); $$= new Item_func_not(new Item_func_in(*$5)); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3120
        | expr IN_SYM in_subselect
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3121
          { $$= new Item_in_subselect(YYTHD, $1, $3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3122
	| expr NOT IN_SYM in_subselect
3123
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3124
            $$= new Item_func_not(new Item_in_subselect(YYTHD, $1, $4));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3125
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3126 3127 3128 3129
	| expr BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_between($1,$3,$5); }
	| expr NOT BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
3130
	| expr OR_OR_CONCAT expr { $$= or_or_concat(YYTHD, $1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3131
	| expr OR expr		{ $$= new Item_cond_or($1,$3); }
3132
        | expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3133
	| expr AND expr		{ $$= new Item_cond_and($1,$3); }
3134
	| expr SOUNDS_SYM LIKE expr { $$= Item_bool_func2::eq_creator(new Item_func_soundex($1), new Item_func_soundex($4));}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3135 3136 3137 3138 3139 3140 3141
	| expr LIKE simple_expr opt_escape { $$= new Item_func_like($1,$3,$4); }
	| expr NOT LIKE simple_expr opt_escape	{ $$= new Item_func_not(new Item_func_like($1,$4,$5));}
	| expr REGEXP expr { $$= new Item_func_regex($1,$3); }
	| expr NOT REGEXP expr { $$= new Item_func_not(new Item_func_regex($1,$4)); }
	| expr IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| expr IS NOT NULL_SYM { $$= new Item_func_isnotnull($1); }
	| expr EQUAL_SYM expr	{ $$= new Item_func_equal($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3142 3143 3144
	| expr comp_op expr %prec EQ	{ $$= (*((*$2)(0)))($1,$3); }
	| expr comp_op all_or_any in_subselect %prec EQ
	{
3145
	  Item_allany_subselect *it=
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3146
	    new Item_allany_subselect(YYTHD, $1, (*$2)($3), $4);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3147 3148 3149 3150 3151
	  if ($3)
	    $$ = new Item_func_not(it);	/* ALL */
	  else
	    $$ = it;			/* ANY/SOME */
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3152 3153 3154 3155 3156 3157
	| expr SHIFT_LEFT expr	{ $$= new Item_func_shift_left($1,$3); }
	| expr SHIFT_RIGHT expr { $$= new Item_func_shift_right($1,$3); }
	| expr '+' expr		{ $$= new Item_func_plus($1,$3); }
	| expr '-' expr		{ $$= new Item_func_minus($1,$3); }
	| expr '*' expr		{ $$= new Item_func_mul($1,$3); }
	| expr '/' expr		{ $$= new Item_func_div($1,$3); }
3158 3159
	| expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
	| expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3160
	| expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
3161
        | expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3162 3163
	| expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| expr '%' expr		{ $$= new Item_func_mod($1,$3); }
3164 3165 3166 3167
	| expr '+' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,0); }
	| expr '-' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,1); }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3168
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3169 3170 3171 3172 3173 3174 3175

/* expressions that begin with 'expr' that do NOT follow IN_SYM */
no_in_expr:
	no_in_expr BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_between($1,$3,$5); }
	| no_in_expr NOT BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
3176
	| no_in_expr OR_OR_CONCAT expr	{ $$= or_or_concat(YYTHD, $1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3177
	| no_in_expr OR expr		{ $$= new Item_cond_or($1,$3); }
3178
        | no_in_expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3179
	| no_in_expr AND expr		{ $$= new Item_cond_and($1,$3); }
3180
	| no_in_expr SOUNDS_SYM LIKE expr { $$= Item_bool_func2::eq_creator(new Item_func_soundex($1), new Item_func_soundex($4));}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3181 3182 3183 3184 3185 3186 3187
	| no_in_expr LIKE simple_expr opt_escape { $$= new Item_func_like($1,$3,$4); }
	| no_in_expr NOT LIKE simple_expr opt_escape { $$= new Item_func_not(new Item_func_like($1,$4,$5)); }
	| no_in_expr REGEXP expr { $$= new Item_func_regex($1,$3); }
	| no_in_expr NOT REGEXP expr { $$= new Item_func_not(new Item_func_regex($1,$4)); }
	| no_in_expr IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| no_in_expr IS NOT NULL_SYM { $$= new Item_func_isnotnull($1); }
	| no_in_expr EQUAL_SYM expr	{ $$= new Item_func_equal($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3188 3189 3190
	| no_in_expr comp_op expr %prec EQ	{ $$= (*((*$2)(0)))($1,$3); }
	| no_in_expr comp_op all_or_any in_subselect %prec EQ
	{
3191
	  Item_allany_subselect *it=
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3192
	    new Item_allany_subselect(YYTHD, $1, (*$2)($3), $4);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3193 3194 3195 3196 3197
	  if ($3)
	    $$ = new Item_func_not(it);	/* ALL */
	  else
	    $$ = it;			/* ANY/SOME */
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3198 3199 3200 3201 3202 3203
	| no_in_expr SHIFT_LEFT expr  { $$= new Item_func_shift_left($1,$3); }
	| no_in_expr SHIFT_RIGHT expr { $$= new Item_func_shift_right($1,$3); }
	| no_in_expr '+' expr		{ $$= new Item_func_plus($1,$3); }
	| no_in_expr '-' expr		{ $$= new Item_func_minus($1,$3); }
	| no_in_expr '*' expr		{ $$= new Item_func_mul($1,$3); }
	| no_in_expr '/' expr		{ $$= new Item_func_div($1,$3); }
3204
	| no_in_expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3205
	| no_in_expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
3206
        | no_in_expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3207 3208
	| no_in_expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| no_in_expr '%' expr		{ $$= new Item_func_mod($1,$3); }
3209
	| no_in_expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
3210 3211 3212 3213
	| no_in_expr '+' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,0); }
	| no_in_expr '-' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,1); }
3214
	| simple_expr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3215 3216 3217

/* expressions that begin with 'expr' that does NOT follow AND */
no_and_expr:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3218
	  no_and_expr IN_SYM '(' expr_list ')'
3219
	  { $4->push_front($1); $$= new Item_func_in(*$4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3220
	| no_and_expr NOT IN_SYM '(' expr_list ')'
3221
	  { $5->push_front($1); $$= new Item_func_not(new Item_func_in(*$5)); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3222
        | no_and_expr IN_SYM in_subselect
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3223
          { $$= new Item_in_subselect(YYTHD, $1, $3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3224
	| no_and_expr NOT IN_SYM in_subselect
3225
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3226
            $$= new Item_func_not(new Item_in_subselect(YYTHD, $1, $4));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3227
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3228 3229 3230 3231
	| no_and_expr BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_between($1,$3,$5); }
	| no_and_expr NOT BETWEEN_SYM no_and_expr AND expr
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
3232
	| no_and_expr OR_OR_CONCAT expr	{ $$= or_or_concat(YYTHD, $1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3233
	| no_and_expr OR expr		{ $$= new Item_cond_or($1,$3); }
3234
        | no_and_expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
3235
	| no_and_expr SOUNDS_SYM LIKE expr { $$= Item_bool_func2::eq_creator(new Item_func_soundex($1), new Item_func_soundex($4));}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3236 3237 3238 3239 3240 3241 3242
	| no_and_expr LIKE simple_expr opt_escape { $$= new Item_func_like($1,$3,$4); }
	| no_and_expr NOT LIKE simple_expr opt_escape	{ $$= new Item_func_not(new Item_func_like($1,$4,$5)); }
	| no_and_expr REGEXP expr { $$= new Item_func_regex($1,$3); }
	| no_and_expr NOT REGEXP expr { $$= new Item_func_not(new Item_func_regex($1,$4)); }
	| no_and_expr IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| no_and_expr IS NOT NULL_SYM { $$= new Item_func_isnotnull($1); }
	| no_and_expr EQUAL_SYM expr	{ $$= new Item_func_equal($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3243 3244 3245
	| no_and_expr comp_op expr %prec EQ { $$= (*((*$2)(0)))($1,$3); }
	| no_and_expr comp_op all_or_any in_subselect %prec EQ
	{
3246
	  Item_allany_subselect *it=
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3247
	    new Item_allany_subselect(YYTHD, $1, (*$2)($3), $4);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3248 3249 3250 3251 3252
	  if ($3)
	    $$ = new Item_func_not(it);	/* ALL */
	  else
	    $$ = it;			/* ANY/SOME */
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3253 3254 3255 3256 3257 3258
	| no_and_expr SHIFT_LEFT expr  { $$= new Item_func_shift_left($1,$3); }
	| no_and_expr SHIFT_RIGHT expr { $$= new Item_func_shift_right($1,$3); }
	| no_and_expr '+' expr		{ $$= new Item_func_plus($1,$3); }
	| no_and_expr '-' expr		{ $$= new Item_func_minus($1,$3); }
	| no_and_expr '*' expr		{ $$= new Item_func_mul($1,$3); }
	| no_and_expr '/' expr		{ $$= new Item_func_div($1,$3); }
3259
	| no_and_expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3260
	| no_and_expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
3261
        | no_and_expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3262 3263
	| no_and_expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| no_and_expr '%' expr		{ $$= new Item_func_mod($1,$3); }
3264
	| no_and_expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
3265 3266 3267 3268
	| no_and_expr '+' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,0); }
	| no_and_expr '-' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,1); }
3269
	| simple_expr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3270

3271
interval_expr:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3272
         INTERVAL_SYM expr { $$=$2; }
3273 3274
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3275 3276
simple_expr:
	simple_ident
3277
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3278
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3279 3280 3281 3282
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
					    		    YYTHD->charset()));
3283
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3284
	| literal
3285
	| param_marker
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3286
	| '@' ident_or_text SET_VAR expr
3287 3288
	  {
	    $$= new Item_func_set_user_var($2,$4);
3289
	    Lex->uncacheable();
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3290
	  }
3291
	| '@' ident_or_text
3292 3293
	  {
	    $$= new Item_func_get_user_var($2);
3294
	    Lex->uncacheable();
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3295
	  }
3296
	| '@' '@' opt_var_ident_type ident_or_text opt_component
3297
	  {
3298
	    if (!($$= get_system_var(YYTHD, (enum_var_type) $3, $4, $5)))
3299
	      YYABORT;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3300
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3301
	| sum_expr
3302 3303
	| '+' expr %prec NEG	{ $$= $2; }
	| '-' expr %prec NEG    { $$= new Item_func_neg($2); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3304 3305 3306 3307
	| '~' expr %prec NEG	{ $$= new Item_func_bit_neg($2); }
	| NOT expr %prec NEG	{ $$= new Item_func_not($2); }
	| '!' expr %prec NEG	{ $$= new Item_func_not($2); }
	| '(' expr ')'		{ $$= $2; }
3308 3309 3310 3311 3312
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
3313
	| ROW_SYM '(' expr ',' expr_list ')'
3314
	  {
3315 3316
	    $5->push_front($3);
	    $$= new Item_row(*$5);
3317
	  }
3318
	| EXISTS exists_subselect { $$= $2; }
3319
	| singlerow_subselect   { $$= $1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3320
	| '{' ident expr '}'	{ $$= $3; }
3321
        | MATCH ident_list_arg AGAINST '(' expr ')'
3322
          { Select->add_ftfunc_to_list((Item_func_match *)
3323
                   ($$=new Item_func_match_nl(*$2,$5))); }
3324
        | MATCH ident_list_arg AGAINST '(' expr IN_SYM BOOLEAN_SYM MODE_SYM ')'
3325
          { Select->add_ftfunc_to_list((Item_func_match *)
3326
                   ($$=new Item_func_match_bool(*$2,$5))); }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3327
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3328
	| BINARY expr %prec NEG
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3329
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3330 3331
	    $$= new Item_func_set_collation($2,new Item_string(binary_keyword,
					    6, &my_charset_latin1));
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3332
	  }
3333
	| CAST_SYM '(' expr AS cast_type ')'
3334 3335 3336 3337 3338
	  { 
	    $$= create_func_cast($3, $5, 
				 Lex->length ? atoi(Lex->length) : -1,
				 Lex->charset); 
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3339
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
3340
	  { $$= new Item_func_case(* $4, $2, $5 ); }
3341
	| CONVERT_SYM '(' expr ',' cast_type ')'
3342 3343 3344 3345 3346
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
				 Lex->charset);
	  }
3347 3348
	| CONVERT_SYM '(' expr USING charset_name ')'
	  { $$= new Item_func_conv_charset($3,$5); }
3349
	| CONVERT_SYM '(' expr ',' expr ',' expr ')'
3350
	  { $$= new Item_func_conv_charset3($3,$7,$5); }
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
3351 3352
	| DEFAULT '(' simple_ident ')'
	  { $$= new Item_default_value($3); }
3353 3354
	| VALUES '(' simple_ident ')'
	  { $$= new Item_insert_value($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3355 3356 3357 3358 3359 3360 3361 3362
	| FUNC_ARG0 '(' ')'
	  { $$= ((Item*(*)(void))($1.symbol->create_func))();}
	| FUNC_ARG1 '(' expr ')'
	  { $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);}
	| FUNC_ARG2 '(' expr ',' expr ')'
	  { $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);}
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
	  { $$= ((Item*(*)(Item*,Item*,Item*))($1.symbol->create_func))($3,$5,$7);}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3363 3364 3365 3366
	| 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); }
3367 3368
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3369 3370 3371 3372 3373 3374
	| 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); }
3375 3376
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3377 3378
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
3379 3380
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3381 3382 3383 3384 3385
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
	  { $$= new Item_func_concat_ws($3, *$5); }
	| CURDATE optional_braces
3386
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3387
	| CURTIME optional_braces
3388
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3389
	| CURTIME '(' expr ')'
3390
	  {
3391
	    $$= new Item_func_curtime_local($3);
3392
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3393
	  }
3394 3395 3396 3397
	| 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); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3398
	| DATABASE '(' ')'
3399
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3400
	    $$= new Item_func_database();
3401
            Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3402
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3403
	| DATE_SYM '(' expr ')'
3404
	  { $$= new Item_date_typecast($3); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3405 3406
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3407
	| ELT_FUNC '(' expr ',' expr_list ')'
3408
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3409 3410
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3411 3412 3413
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
3414
	    Lex->uncacheable();
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3415
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3416
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
3417
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3418
	  { $$= new Item_func_decode($3,$5.str); }
3419
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3420
	 { $$= new Item_func_encode($3,$5.str); }
3421
	| DES_DECRYPT_SYM '(' expr ')'
3422
        { $$= new Item_func_des_decrypt($3); }
3423
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
3424
        { $$= new Item_func_des_decrypt($3,$5); }
3425
	| DES_ENCRYPT_SYM '(' expr ')'
3426
        { $$= new Item_func_des_encrypt($3); }
3427
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
3428
        { $$= new Item_func_des_encrypt($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3429 3430 3431 3432 3433 3434
	| 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); }
3435 3436
	| FALSE_SYM
	  { $$= new Item_int((char*) "FALSE",0,1); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3437 3438 3439 3440 3441 3442
	| 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 ')'
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3443
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3444 3445
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
3446
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
3447
	| GEOMFROMTEXT '(' expr ')'
3448
	  { $$= new Item_func_geometry_from_text($3); }
3449
	| GEOMFROMTEXT '(' expr ',' expr ')'
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
3450 3451 3452 3453 3454
	  { $$= new Item_func_geometry_from_text($3, $5); }
	| GEOMFROMWKB '(' expr ')'
	  { $$= new Item_func_geometry_from_wkb($3); }
	| GEOMFROMWKB '(' expr ',' expr ')'
	  { $$= new Item_func_geometry_from_wkb($3, $5); }
3455
	| GEOMETRYCOLLECTION '(' expr_list ')'
3456 3457
	  { $$= new Item_func_spatial_collection(* $3,
                       Geometry::wkbGeometryCollection,
3458
                       Geometry::wkbPoint); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3459 3460 3461 3462 3463 3464
	| 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); }
3465
	| interval_expr interval '+' expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3466
	  /* we cannot put interval before - */
3467 3468 3469 3470 3471 3472 3473 3474 3475 3476
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
              send_error(Lex->thd, ER_SYNTAX_ERROR);
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3477 3478
	| LAST_INSERT_ID '(' ')'
	  {
3479
	    $$= get_system_var(YYTHD, OPT_SESSION, "last_insert_id", 14,
3480
			      "last_insert_id()");
3481
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3482 3483 3484 3485
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
	    $$= new Item_func_set_last_insert_id($3);
3486
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3487 3488 3489
	  }
	| LEFT '(' expr ',' expr ')'
	  { $$= new Item_func_left($3,$5); }
3490
	| LINESTRING '(' expr_list ')'
3491
	  { $$= new Item_func_spatial_collection(* $3,
3492
               Geometry::wkbLineString, Geometry::wkbPoint); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3493 3494 3495 3496
	| LOCATE '(' expr ',' expr ')'
	  { $$= new Item_func_locate($5,$3); }
	| LOCATE '(' expr ',' expr ',' expr ')'
	  { $$= new Item_func_locate($5,$3,$7); }
3497
 	| GEOMCOLLFROMTEXT '(' expr ')'
3498
	  { $$= new Item_func_geometry_from_text($3); }
3499
	| GEOMCOLLFROMTEXT '(' expr ',' expr ')'
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
3500
	  { $$= new Item_func_geometry_from_text($3, $5); }
3501
	| GREATEST_SYM '(' expr ',' expr_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3502 3503 3504
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
3505
	| LOG_SYM '(' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3506
	  { $$= new Item_func_log($3); }
3507
	| LOG_SYM '(' expr ',' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3508
	  { $$= new Item_func_log($3, $5); }
3509
 	| LINEFROMTEXT '(' expr ')'
3510
	  { $$= new Item_func_geometry_from_text($3); }
3511
	| LINEFROMTEXT '(' expr ',' expr ')'
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
3512
	  { $$= new Item_func_geometry_from_text($3, $5); }
3513
	| MASTER_POS_WAIT '(' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3514
	  {
3515
	    $$= new Item_master_pos_wait($3, $5);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3516
	    Lex->safe_to_cache_query=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3517
		  }
3518
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3519
	  {
3520
	    $$= new Item_master_pos_wait($3, $5, $7);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3521
	    Lex->safe_to_cache_query=0;
3522
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3523 3524
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3525 3526
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
3527 3528
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3529 3530
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
3531
 	| MULTILINESTRING '(' expr_list ')'
3532
 	  { $$= new Item_func_spatial_collection(* $3,
3533
                    Geometry::wkbMultiLineString, Geometry::wkbLineString); }
3534
 	| MLINEFROMTEXT '(' expr ')'
3535
	  { $$= new Item_func_geometry_from_text($3); }
3536
	| MLINEFROMTEXT '(' expr ',' expr ')'
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
3537
	  { $$= new Item_func_geometry_from_text($3, $5); }
3538
	| MPOINTFROMTEXT '(' expr ')'
3539
	  { $$= new Item_func_geometry_from_text($3); }
3540
	| MPOINTFROMTEXT '(' expr ',' expr ')'
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
3541
	  { $$= new Item_func_geometry_from_text($3, $5); }
3542
	| MPOLYFROMTEXT '(' expr ')'
3543
	  { $$= new Item_func_geometry_from_text($3); }
3544
	| MPOLYFROMTEXT '(' expr ',' expr ')'
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
3545
	  { $$= new Item_func_geometry_from_text($3, $5); }
3546
	| MULTIPOINT '(' expr_list ')'
3547
	  { $$= new Item_func_spatial_collection(* $3,
3548 3549
                    Geometry::wkbMultiPoint, Geometry::wkbPoint); }
 	| MULTIPOLYGON '(' expr_list ')'
3550
	  { $$= new Item_func_spatial_collection(* $3,
3551
                       Geometry::wkbMultiPolygon, Geometry::wkbPolygon ); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3552
	| NOW_SYM optional_braces
3553
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3554
	| NOW_SYM '(' expr ')'
3555
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3556
	| PASSWORD '(' expr ')'
3557
	  {
3558 3559
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
3560 3561 3562 3563
	      (Item *) new Item_func_password($3);
	  }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new Item_func_old_password($3); }
3564
	| POINT_SYM '(' expr ',' expr ')'
3565
	  { $$= new Item_func_point($3,$5); }
3566
 	| POINTFROMTEXT '(' expr ')'
3567
	  { $$= new Item_func_geometry_from_text($3); }
3568
	| POINTFROMTEXT '(' expr ',' expr ')'
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
3569
	  { $$= new Item_func_geometry_from_text($3, $5); }
3570
	| POLYFROMTEXT '(' expr ')'
3571
	  { $$= new Item_func_geometry_from_text($3); }
3572
	| POLYFROMTEXT '(' expr ',' expr ')'
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
3573
	  { $$= new Item_func_geometry_from_text($3, $5); }
3574
	| POLYGON '(' expr_list ')'
3575
	  { $$= new Item_func_spatial_collection(* $3,
3576
			Geometry::wkbPolygon, Geometry::wkbLineString); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3577 3578
	| POSITION_SYM '(' no_in_expr IN_SYM expr ')'
	  { $$ = new Item_func_locate($5,$3); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3579
	| RAND '(' expr ')'
3580
	  { $$= new Item_func_rand($3); Lex->uncacheable();}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3581
	| RAND '(' ')'
3582
	  { $$= new Item_func_rand(); Lex->uncacheable();}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3583 3584 3585 3586 3587 3588 3589
	| 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); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3590 3591 3592 3593
	| 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); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605
	| 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); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3606
	| TIME_SYM '(' expr ')'
3607 3608 3609
	  { $$= new Item_time_typecast($3); }
	| TIMESTAMP '(' expr ')'
	  { $$= new Item_datetime_typecast($3); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3610
	| TIMESTAMP '(' expr ',' expr ')'
3611
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3612
	| TRIM '(' expr ')'
3613 3614
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3615
	  { $$= new Item_func_ltrim($6,$4); }
3616
	| TRIM '(' TRAILING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3617
	  { $$= new Item_func_rtrim($6,$4); }
3618
	| TRIM '(' BOTH expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3619
	  { $$= new Item_func_trim($6,$4); }
3620 3621 3622 3623 3624 3625
	| 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); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3626 3627
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
3628 3629
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
3630 3631
	| TRUE_SYM
	  { $$= new Item_int((char*) "TRUE",1,1); }
3632 3633
	| SP_FUNC '(' udf_expr_list ')'
	  {
3634
	    sp_add_fun_to_lex(Lex, $1);
3635 3636 3637 3638 3639
	    if ($3)
	      $$= new Item_func_sp($1, *$3);
	    else
	      $$= new Item_func_sp($1);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682
	| UDA_CHAR_SUM '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_sum_udf_str($1, *$3);
	    else
	      $$ = new Item_sum_udf_str($1);
	  }
	| UDA_FLOAT_SUM '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_sum_udf_float($1, *$3);
	    else
	      $$ = new Item_sum_udf_float($1);
	  }
	| UDA_INT_SUM '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_sum_udf_int($1, *$3);
	    else
	      $$ = new Item_sum_udf_int($1);
	  }
	| UDF_CHAR_FUNC '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_func_udf_str($1, *$3);
	    else
	      $$ = new Item_func_udf_str($1);
	  }
	| UDF_FLOAT_FUNC '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_func_udf_float($1, *$3);
	    else
	      $$ = new Item_func_udf_float($1);
	  }
	| UDF_INT_FUNC '(' udf_expr_list ')'
	  {
	    if ($3 != NULL)
	      $$ = new Item_func_udf_int($1, *$3);
	    else
	      $$ = new Item_func_udf_int($1);
	  }
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
3683
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3684 3685
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3686
	| UNIX_TIMESTAMP '(' ')'
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3687 3688
	  {
	    $$= new Item_func_unix_timestamp();
3689
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3690
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3691 3692 3693
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
3694
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
3695 3696 3697 3698 3699 3700
	| 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;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3701
	| WEEK_SYM '(' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3702
	  {
3703
            $$= new Item_func_week($3,new Item_int((char*) "0",
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3704
				   YYTHD->variables.default_week_format,1));
3705
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3706 3707 3708 3709 3710 3711 3712 3713 3714
	| 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 ')'
3715
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3716
	    $$=new Item_func_benchmark($3,$5);
3717
	    Lex->uncacheable();
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3718
	  }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3719
	| EXTRACT_SYM '(' interval FROM expr ')'
3720
	{ $$=new Item_extract( $3, $5); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3721 3722 3723

udf_expr_list:
	/* empty */	{ $$= NULL; }
3724
	| expr_list	{ $$= $1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3725 3726 3727 3728 3729 3730 3731 3732

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); }
3733
	| COUNT_SYM '(' opt_all '*' ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3734 3735 3736
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
3737
	| COUNT_SYM '(' DISTINCT
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3738
	  { Select->in_sum_expr++; }
3739
	   expr_list
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3740
	  { Select->in_sum_expr--; }
3741 3742
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3743 3744 3745 3746 3747 3748 3749 3750
	| GROUP_UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' in_sum_expr ')'
	  { $$= new Item_sum_unique_users($3,atoi($5.str),atoi($7.str),$9); }
	| MIN_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_min($3); }
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
	| STD_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_std($3); }
3751 3752
	| VARIANCE_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_variance($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3753
	| SUM_SYM '(' in_sum_expr ')'
3754
	  { $$=new Item_sum_sum($3); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3755 3756 3757 3758
	| GROUP_CONCAT_SYM '(' opt_distinct expr_list opt_gorder_clause
	  opt_gconcat_separator ')'
	  {
	    $$=new Item_func_group_concat($3,$4,Lex->gorder_list,$6);
3759 3760 3761 3762 3763 3764 3765 3766
	    $4->empty();
	  };

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

opt_gconcat_separator:
3767
    /* empty */        { $$ = new String(",",1,default_charset_info); }
3768
    |SEPARATOR_SYM text_string  { $$ = $2; };
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3769

3770 3771

opt_gorder_clause:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3772
    /* empty */
3773 3774 3775 3776 3777 3778 3779 3780 3781 3782
      {
        LEX *lex=Lex;
        lex->gorder_list = NULL;
      }
    | order_clause
      {
        LEX *lex=Lex;
        lex->gorder_list= (SQL_LIST*) sql_memdup((char*) &lex->current_select->order_list,sizeof(st_sql_list));
        lex->current_select->order_list.empty();
      };
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3783

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3784 3785

in_sum_expr:
3786
	opt_all
3787 3788 3789 3790 3791
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
	    send_error(lex->thd, ER_SYNTAX_ERROR);
3792
	    YYABORT;
3793 3794
	  }
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3795 3796
	expr
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3797
	  Select->in_sum_expr--;
3798
	  $$= $3;
3799
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3800

3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817
cast_type_init:
	{ Lex->charset= NULL; Lex->length= (char*)0; }
	;

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

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3818
cast_type:
3819
	cast_type_init cast_type_finalize { $$= $2; }
3820
	;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3821

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3822
expr_list:
3823
	{ Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3824
	expr_list2
3825
	{ $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3826 3827

expr_list2:
3828
	expr { Select->expr_list.head()->push_back($1); }
3829
	| expr_list2 ',' expr { Select->expr_list.head()->push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3830

3831 3832
ident_list_arg:
          ident_list          { $$= $1; }
3833
        | '(' ident_list ')'  { $$= $2; };
3834

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3835
ident_list:
3836
        { Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3837
        ident_list2
3838
        { $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3839 3840

ident_list2:
3841
        simple_ident { Select->expr_list.head()->push_back($1); }
3842
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3843 3844 3845

opt_expr:
	/* empty */      { $$= NULL; }
3846
	| expr         	 { $$= $1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3847 3848 3849

opt_else:
	/* empty */    { $$= NULL; }
3850
	| ELSE expr    { $$= $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3851 3852

when_list:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3853
        { Select->when_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3854
	when_list2
3855
	{ $$= Select->when_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3856 3857

when_list2:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3858
	expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3859
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3860
	    SELECT_LEX *sel=Select;
3861 3862
	    sel->when_list.head()->push_back($1);
	    sel->when_list.head()->push_back($3);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3863
	}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3864
	| when_list2 WHEN_SYM expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3865
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3866
	    SELECT_LEX *sel=Select;
3867 3868
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
3869
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3870 3871 3872 3873

join_table_list:
	'(' join_table_list ')'	{ $$=$2; }
	| join_table		{ $$=$1; }
3874
	| join_table_list ',' join_table_list { $$=$3; }
3875
	| join_table_list normal_join join_table_list { $$=$3; }
3876
	| join_table_list STRAIGHT_JOIN join_table_list
3877
	  { $$=$3 ; $1->next->straight=1; }
3878 3879 3880
	| join_table_list normal_join join_table_list ON expr
	  { add_join_on($3,$5); $$=$3; }
	| join_table_list normal_join join_table_list
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3881
	  USING
3882
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3883
	    SELECT_LEX *sel= Select;
3884
	    sel->db1=$1->db; sel->table1=$1->alias;
3885
	    sel->db2=$3->db; sel->table2=$3->alias;
3886
	  }
3887 3888 3889
	  '(' using_list ')'
	  { add_join_on($3,$7); $$=$3; }

3890
	| join_table_list LEFT opt_outer JOIN_SYM join_table_list ON expr
3891
	  { add_join_on($5,$7); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
3892
	| join_table_list LEFT opt_outer JOIN_SYM join_table_list
3893
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3894
	    SELECT_LEX *sel= Select;
3895 3896
	    sel->db1=$1->db; sel->table1=$1->alias;
	    sel->db2=$5->db; sel->table2=$5->alias;
3897
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3898
	  USING '(' using_list ')'
3899
	  { add_join_on($5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
3900
	| join_table_list NATURAL LEFT opt_outer JOIN_SYM join_table_list
3901 3902 3903 3904 3905
	  {
	    add_join_natural($1,$1->next);
	    $1->next->outer_join|=JOIN_TYPE_LEFT;
	    $$=$6;
	  }
3906
	| join_table_list RIGHT opt_outer JOIN_SYM join_table_list ON expr
3907
	  { add_join_on($1,$7); $1->outer_join|=JOIN_TYPE_RIGHT; $$=$5; }
3908
	| join_table_list RIGHT opt_outer JOIN_SYM join_table_list
3909
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3910
	    SELECT_LEX *sel= Select;
3911 3912
	    sel->db1=$1->db; sel->table1=$1->alias;
	    sel->db2=$5->db; sel->table2=$5->alias;
3913
	  }
3914
	  USING '(' using_list ')'
3915
	  { add_join_on($1,$9); $1->outer_join|=JOIN_TYPE_RIGHT; $$=$5; }
3916
	| join_table_list NATURAL RIGHT opt_outer JOIN_SYM join_table_list
3917 3918 3919 3920 3921
	  {
	    add_join_natural($1->next,$1);
	    $1->outer_join|=JOIN_TYPE_RIGHT;
	    $$=$6;
	  }
3922
	| join_table_list NATURAL JOIN_SYM join_table_list
3923
	  { add_join_natural($1,$1->next); $$=$4; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3924 3925

normal_join:
3926 3927 3928 3929
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3930 3931

join_table:
3932
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3933
	  SELECT_LEX *sel= Select;
3934
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3935
	  sel->table_join_options= 0;
3936
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3937
        table_ident opt_table_alias opt_key_definition
3938
	{
3939
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3940
	  SELECT_LEX *sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3941
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3942
					   sel->get_table_join_options(),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3943
					   lex->lock_option,
3944 3945
					   sel->get_use_index(),
					   sel->get_ignore_index())))
3946
	    YYABORT;
3947
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3948
	| '{' ident join_table LEFT OUTER JOIN_SYM join_table ON expr '}'
3949
	  { add_join_on($7,$9); $7->outer_join|=JOIN_TYPE_LEFT; $$=$7; }
3950
        | '(' SELECT_SYM select_derived ')' opt_table_alias
3951 3952
	{
	  LEX *lex=Lex;
3953 3954 3955
	  SELECT_LEX_UNIT *unit= lex->current_select->master_unit();
	  lex->current_select= unit->outer_select();
	  if (!($$= lex->current_select->
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3956
                add_table_to_list(lex->thd, new Table_ident(unit), $5, 0,
3957
				  TL_READ,(List<String> *)0,
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3958 3959
	                          (List<String> *)0)))

3960
	    YYABORT;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3961
	};
3962

3963
select_derived:
3964
        {
3965
	  LEX *lex= Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3966
	  lex->derived_tables= 1;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3967 3968 3969
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
3970 3971 3972 3973
	  {	
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
3974
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
3975
              mysql_new_select(lex, 1))
3976 3977
	    YYABORT;
	  mysql_init_select(lex);
3978
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
3979
	}
3980
        select_options select_item_list opt_select_from union_opt
3981
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3982 3983 3984

opt_outer:
	/* empty */	{}
3985
	| OUTER		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3986 3987 3988 3989

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
3990
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3991
	    SELECT_LEX *sel= Select;
3992 3993 3994
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3995 3996
	| FORCE_SYM key_usage_list
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3997
	    SELECT_LEX *sel= Select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3998 3999 4000 4001
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4002
	| IGNORE_SYM key_usage_list
4003
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4004
	    SELECT_LEX *sel= Select;
4005 4006
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
4007
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4008 4009

key_usage_list:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4010
	key_or_index { Select->interval_list.empty(); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4011
        '(' key_list_or_empty ')'
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4012
        { $$= &Select->interval_list; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4013 4014 4015 4016 4017 4018
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4019 4020 4021

key_usage_list2:
	key_usage_list2 ',' ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4022
        { Select->
4023
	    interval_list.push_back(new String((const char*) $3.str, $3.length,
4024
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4025
	| ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4026
        { Select->
4027
	    interval_list.push_back(new String((const char*) $1.str, $1.length,
4028
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4029
	| PRIMARY_SYM
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4030
        { Select->
4031
	    interval_list.push_back(new String("PRIMARY", 7,
4032
				    system_charset_info)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4033 4034 4035

using_list:
	ident
4036
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4037
	    SELECT_LEX *sel= Select;
4038 4039 4040 4041
	    if (!($$= new Item_func_eq(new Item_field(sel->db1, sel->table1,
						      $1.str),
				       new Item_field(sel->db2, sel->table2,
						      $1.str))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4042 4043 4044 4045
	      YYABORT;
	  }
	| using_list ',' ident
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4046
	    SELECT_LEX *sel= Select;
4047
	    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)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4048
	      YYABORT;
4049
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4050 4051 4052

interval:
	 DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4053
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4054 4055 4056
	| DAY_MINUTE_SYM	{ $$=INTERVAL_DAY_MINUTE; }
	| DAY_SECOND_SYM	{ $$=INTERVAL_DAY_SECOND; }
	| DAY_SYM		{ $$=INTERVAL_DAY; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4057
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4058 4059 4060
	| HOUR_MINUTE_SYM	{ $$=INTERVAL_HOUR_MINUTE; }
	| HOUR_SECOND_SYM	{ $$=INTERVAL_HOUR_SECOND; }
	| HOUR_SYM		{ $$=INTERVAL_HOUR; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4061 4062
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4063 4064 4065
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4066
	| SECOND_MICROSECOND_SYM	{ $$=INTERVAL_SECOND_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4067 4068
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
	| YEAR_MONTH_SYM	{ $$=INTERVAL_YEAR_MONTH; }
4069
	| YEAR_SYM		{ $$=INTERVAL_YEAR; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4070 4071 4072 4073

table_alias:
	/* empty */
	| AS
4074
	| EQ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4075 4076 4077 4078

opt_table_alias:
	/* empty */		{ $$=0; }
	| table_alias ident
4079
	  { $$= (LEX_STRING*) sql_memdup(&$2,sizeof(LEX_STRING)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4080

4081 4082 4083 4084
opt_all:
	/* empty */
	| ALL
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4085 4086

where_clause:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4087
	/* empty */  { Select->where= 0; }
4088 4089
	| WHERE expr
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4090
	    Select->where= $2;
4091 4092 4093
	    if ($2)
	      $2->top_level_item();
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4094
 	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4095 4096 4097

having_clause:
	/* empty */
4098 4099
	| HAVING
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4100
	    Select->parsing_place= SELECT_LEX_NODE::IN_HAVING;
4101 4102
          }
	  expr
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4103
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4104
	    SELECT_LEX *sel= Select;
4105 4106
	    sel->having= $3;
	    sel->parsing_place= SELECT_LEX_NODE::NO_MATTER;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4107 4108 4109
	    if ($3)
	      $3->top_level_item();
	  }
4110
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4111 4112

opt_escape:
4113
	ESCAPE_SYM TEXT_STRING_literal	{ $$= $2.str; }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4114
	| /* empty */			{ $$= (char*) "\\"; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4115 4116 4117


/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4118
   group by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4119 4120 4121 4122
*/

group_clause:
	/* empty */
4123
	| GROUP BY group_list olap_opt;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4124 4125

group_list:
4126
	group_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4127
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
4128
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4129
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4130

4131 4132
olap_opt:
	/* empty */ {}
4133
	| WITH CUBE_SYM
4134
          {
4135
	    LEX *lex=Lex;
4136 4137 4138 4139 4140 4141
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
	      net_printf(lex->thd, ER_WRONG_USAGE, "WITH CUBE",
		       "global union parameters");
	      YYABORT;
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4142
	    lex->current_select->olap= CUBE_TYPE;
4143
	    net_printf(lex->thd, ER_NOT_SUPPORTED_YET, "CUBE");
4144
	    YYABORT;	/* To be deleted in 5.1 */
4145
	  }
4146
	| WITH ROLLUP_SYM
4147
          {
4148 4149 4150 4151 4152 4153 4154
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
	      net_printf(lex->thd, ER_WRONG_USAGE, "WITH ROLLUP",
		       "global union parameters");
	      YYABORT;
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4155
	    lex->current_select->olap= ROLLUP_TYPE;
4156
	  }
4157
	;
4158

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4159
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4160
   Order by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4161 4162
*/

4163
opt_order_clause:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4164
	/* empty */
4165
	| order_clause;
4166 4167

order_clause:
4168 4169
	ORDER_SYM BY
        {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4170
	  LEX *lex=Lex;
4171
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4172
	      lex->current_select->olap !=
4173
	      UNSPECIFIED_OLAP_TYPE)
4174
	  {
4175
	    net_printf(lex->thd, ER_WRONG_USAGE,
4176 4177 4178 4179
		       "CUBE/ROLLUP",
		       "ORDER BY");
	    YYABORT;
	  }
4180
	} order_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4181 4182 4183

order_list:
	order_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4184
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4185
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4186
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4187 4188 4189

order_dir:
	/* empty */ { $$ =  1; }
4190
	| ASC  { $$ =1; }
4191
	| DESC { $$ =0; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4192 4193


4194 4195 4196
opt_limit_clause_init:
	/* empty */
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4197
	  SELECT_LEX *sel= Select;
4198 4199 4200
          sel->offset_limit= 0L;
          sel->select_limit= Lex->thd->variables.select_limit;
	}
4201 4202 4203
	| limit_clause {}
	;

4204 4205 4206 4207 4208
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

4209
limit_clause:
4210
	LIMIT limit_options {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4211
	;
4212 4213 4214

limit_options:
	ULONG_NUM
4215
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4216
            SELECT_LEX *sel= Select;
4217 4218 4219 4220 4221
            sel->select_limit= $1;
            sel->offset_limit= 0L;
	  }
	| ULONG_NUM ',' ULONG_NUM
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4222
	    SELECT_LEX *sel= Select;
4223 4224
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
4225
	  }
4226
	| ULONG_NUM OFFSET_SYM ULONG_NUM
4227
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4228
	    SELECT_LEX *sel= Select;
4229 4230 4231 4232
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4233 4234 4235 4236 4237


delete_limit_clause:
	/* empty */
	{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4238
	  LEX *lex=Lex;
4239
	  lex->current_select->select_limit= HA_POS_ERROR;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4240
	}
4241
	| LIMIT ulonglong_num
4242
	{ Select->select_limit= (ha_rows) $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4243 4244

ULONG_NUM:
4245
	NUM	    { $$= strtoul($1.str,NULL,10); }
4246
	| LONG_NUM  { $$= (ulong) strtoll($1.str,NULL,10); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4247
	| ULONGLONG_NUM { $$= (ulong) strtoull($1.str,NULL,10); }
4248
	| REAL_NUM  { $$= strtoul($1.str,NULL,10); }
4249
	| FLOAT_NUM { $$= strtoul($1.str,NULL,10); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4250

4251
ulonglong_num:
4252
	NUM	    { $$= (ulonglong) strtoul($1.str,NULL,10); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4253
	| ULONGLONG_NUM { $$= strtoull($1.str,NULL,10); }
4254
	| LONG_NUM  { $$= (ulonglong) strtoll($1.str,NULL,10); }
4255
	| REAL_NUM  { $$= strtoull($1.str,NULL,10); }
4256
	| FLOAT_NUM { $$= strtoull($1.str,NULL,10); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4257 4258 4259 4260 4261 4262

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
4263 4264 4265 4266 4267 4268 4269
	    if (&lex->select_lex != lex->current_select)
	    {
	      net_printf(lex->thd, ER_WRONG_USAGE,
			  "PROCEDURE",
			  "subquery");
	      YYABORT;
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4270 4271 4272
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
4273
	    if (add_proc_to_list(lex->thd, new Item_field(NULL,NULL,$2.str)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4274
	      YYABORT;
4275
	    Lex->uncacheable();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4276
	  }
4277
	  '(' procedure_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4278 4279 4280 4281


procedure_list:
	/* empty */ {}
4282
	| procedure_list2 {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4283 4284 4285

procedure_list2:
	procedure_list2 ',' procedure_item
4286
	| procedure_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4287 4288 4289 4290

procedure_item:
	  remember_name expr
	  {
4291 4292
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4293 4294
	      YYABORT;
	    if (!$2->name)
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4295
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1), YYTHD->charset());
4296 4297 4298 4299 4300 4301
	  }
          ;


select_var_list_init:
	   {
4302 4303
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
4304 4305
	        YYABORT;
	   }
4306
	   select_var_list
4307
	   {}
4308
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4309

Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4310
select_var_list:
4311 4312 4313 4314
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

4315 4316
select_var_ident:  
	   '@' ident_or_text
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4317
           {
4318
             LEX *lex=Lex;
4319 4320 4321
	     if (lex->result) 
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($2,0,0));
	     else
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4322 4323
	       YYABORT;
	   }
4324
           | ident_or_text
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4325
           {
4326
             LEX *lex=Lex;
4327 4328 4329 4330 4331
	     if (!lex->spcont)
	       YYABORT;
	     sp_pvar_t *t;
	     if (!(t=lex->spcont->find_pvar(&$1)))
	     {
4332
	       send_error(lex->thd, ER_SP_UNDECLARED_VAR);
4333 4334
	       YYABORT;
	     }
4335
	     if (! lex->result)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4336
	       YYABORT;
4337 4338 4339 4340 4341
	     else
	     {
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($1,1,t->offset));
	       t->isset= TRUE;
	     }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4342
	   }
4343
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4344

4345
into:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4346
        INTO OUTFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4347
	{
4348
	  LEX *lex=Lex;
4349 4350
	  if (!lex->describe)
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4351
	    lex->uncacheable();
4352 4353 4354 4355 4356
	    if (!(lex->exchange= new sql_exchange($3.str,0)))
	      YYABORT;
	    if (!(lex->result= new select_export(lex->exchange)))
	      YYABORT;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4357 4358
	}
	opt_field_term opt_line_term
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4359
	| INTO DUMPFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4360
	{
4361
	  LEX *lex=Lex;
4362 4363
	  if (!lex->describe)
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4364
	    lex->uncacheable();
4365 4366 4367 4368 4369
	    if (!(lex->exchange= new sql_exchange($3.str,1)))
	      YYABORT;
	    if (!(lex->result= new select_dump(lex->exchange)))
	      YYABORT;
	  }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4370
	}
4371
        | INTO select_var_list_init
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4372
	{
4373
	  Lex->uncacheable();
4374 4375
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4376

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4377 4378 4379
/*
  DO statement
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4380

4381
do:	DO_SYM
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4382 4383 4384 4385 4386 4387
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
	  if (!(lex->insert_list = new List_item))
	    YYABORT;
	}
4388 4389 4390 4391
	values
	{}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4392
/*
4393
  Drop : delete tables or index or user
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4394 4395 4396
*/

drop:
serg@serg.mylan's avatar
serg@serg.mylan committed
4397
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4398
	{
4399 4400
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
4401 4402
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4403 4404 4405
	}
	| DROP INDEX ident ON table_ident {}
	  {
4406 4407 4408 4409
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
	     lex->drop_list.empty();
	     lex->drop_list.push_back(new Alter_drop(Alter_drop::KEY,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4410
						     $3.str));
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4411 4412
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4413 4414 4415 4416
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
4417 4418 4419 4420
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
	    lex->name=$4.str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4421
	 }
4422
	| DROP FUNCTION_SYM if_exists IDENT_sys opt_restrict
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4423
	  {
4424 4425
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
4426 4427
	    lex->drop_if_exists= $3;
	    lex->udf.name= $4;
4428
	  }
4429
	| DROP PROCEDURE if_exists IDENT_sys opt_restrict
4430 4431 4432
	  {
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
4433 4434
	    lex->drop_if_exists= $3;
	    lex->udf.name= $4;
4435 4436 4437 4438 4439 4440
	  }
	| DROP USER
	  {
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_DROP_USER;
	    lex->users_list.empty();
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4441
	  }
4442 4443 4444
	  user_list
	  {}
	  ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4445 4446

table_list:
4447
	table_name
4448
	| table_list ',' table_name;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4449

4450
table_name:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4451
	table_ident
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4452 4453 4454 4455 4456
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4457 4458

if_exists:
4459
	/* empty */ { $$= 0; }
4460 4461
	| IF EXISTS { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4462

4463 4464 4465 4466
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4467 4468 4469 4470 4471
/*
** Insert : add new data to table
*/

insert:
4472 4473 4474 4475 4476 4477
	INSERT
	{
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_INSERT;
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
4478
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
4479
	} insert_lock_option
4480
	opt_ignore insert2
4481
	{
4482
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4483
	  Lex->current_select= &Lex->select_lex;
4484
	}
4485
	insert_field_spec opt_insert_update
4486
	{}
4487
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4488 4489

replace:
4490 4491
	REPLACE
	{
4492
	  LEX *lex=Lex;
4493 4494
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
4495
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
4496
	}
4497 4498
	replace_lock_option insert2
	{
4499
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4500
	  Lex->current_select= &Lex->select_lex;
4501 4502
	}
	insert_field_spec
4503
	{}
4504
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4505 4506

insert_lock_option:
4507 4508 4509 4510
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
4511
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4512 4513

replace_lock_option:
4514 4515
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4516 4517 4518

insert2:
	INTO insert_table {}
4519
	| insert_table {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4520 4521

insert_table:
4522
	table_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4523
	{
4524 4525 4526 4527
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
4528
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4529 4530

insert_field_spec:
serg@serg.mylan's avatar
serg@serg.mylan committed
4531 4532 4533
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4534 4535
	| SET
	  {
4536 4537 4538
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4539 4540
	      YYABORT;
	   }
4541
	   ident_eq_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4542 4543 4544 4545

opt_field_spec:
	/* empty */	  { }
	| '(' fields ')'  { }
4546
	| '(' ')'	  { };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4547 4548 4549

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
4550
	| insert_ident		{ Lex->field_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4551 4552 4553

insert_values:
	VALUES	values_list  {}
4554
	| VALUE_SYM values_list  {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4555 4556
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
4557
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4558 4559 4560

values_list:
	values_list ','  no_braces
4561
	| no_braces;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4562 4563 4564 4565

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
4566
	ident_eq_value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4567 4568

ident_eq_value:
4569
	simple_ident equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4570
	 {
4571 4572 4573
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4574
	    YYABORT;
4575
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4576 4577

equal:	EQ		{}
4578 4579 4580 4581 4582 4583 4584
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4585 4586 4587 4588 4589 4590 4591 4592 4593

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
4594 4595
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4596
	    YYABORT;
4597
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4598 4599 4600

opt_values:
	/* empty */ {}
4601
	| values;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4602 4603

values:
4604
	values ','  expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4605 4606 4607 4608
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
4609 4610 4611 4612 4613 4614 4615 4616
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
4617
	expr	  { $$= $1;}
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
4618
	| DEFAULT {$$= new Item_default_value(); }
4619
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4620

4621 4622
opt_insert_update:
        /* empty */
4623
        | ON DUPLICATE_SYM
4624 4625 4626 4627 4628 4629 4630 4631 4632
          { /* for simplisity, let's forget about
               INSERT ... SELECT ... UPDATE
               for a moment */
	    if (Lex->sql_command != SQLCOM_INSERT)
            {
	      send_error(Lex->thd, ER_SYNTAX_ERROR);
              YYABORT;
            }
          }
4633
          KEY_SYM UPDATE_SYM update_list
4634 4635
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4636 4637 4638
/* Update rows in a table */

update:
4639 4640
	UPDATE_SYM
	{
4641
	  LEX *lex= Lex;
4642
	  mysql_init_select(lex);
4643
          lex->sql_command= SQLCOM_UPDATE;
4644
        }
4645 4646 4647
        opt_low_priority opt_ignore join_table_list
	SET update_list where_clause opt_order_clause delete_limit_clause
	{
4648
	  LEX *lex= Lex;
4649
	  Select->set_lock_for_tables($3);
4650
          if (lex->select_lex.table_list.elements > 1)
4651
            lex->sql_command= SQLCOM_UPDATE_MULTI;
4652 4653
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4654 4655

update_list:
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
4656
	update_list ',' simple_ident equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4657
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4658
	  if (add_item_to_list(YYTHD, $3) || add_value_to_list(YYTHD, $5))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4659 4660
	    YYABORT;
	}
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
4661
	| simple_ident equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4662
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4663
	    if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4664
	      YYABORT;
4665
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4666 4667

opt_low_priority:
4668
	/* empty */	{ $$= YYTHD->update_lock_default; }
4669
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4670 4671 4672 4673

/* Delete rows from a table */

delete:
4674
	DELETE_SYM
4675
	{
4676 4677
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
4678
	  lex->lock_option= lex->thd->update_lock_default;
4679
	  lex->select_lex.init_order();
4680
	}
4681 4682
	opt_delete_options single_multi {}
	;
4683 4684

single_multi:
4685 4686
 	FROM table_ident
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4687 4688
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
4689 4690 4691
	    YYABORT;
	}
	where_clause opt_order_clause
4692
	delete_limit_clause {}
4693
	| table_wild_list
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4694
	  { mysql_init_multi_delete(Lex); }
4695
          FROM join_table_list where_clause
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4696 4697
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
4698 4699 4700
	  USING join_table_list where_clause
	  {}
	;
4701 4702 4703

table_wild_list:
	  table_wild_one {}
4704
	  | table_wild_list ',' table_wild_one {};
4705 4706

table_wild_one:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4707
	ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4708
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4709 4710
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
					 TL_OPTION_UPDATING, Lex->lock_option))
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4711 4712
	    YYABORT;
        }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4713
	| ident '.' ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4714
	  {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4715 4716
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4717 4718
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
4719
	      YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4720
	  }
4721
	;
4722 4723

opt_wild:
4724
	/* empty */	{}
4725
	| '.' '*'	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4726 4727


4728
opt_delete_options:
4729
	/* empty */	{}
4730
	| opt_delete_option opt_delete_options {};
4731 4732

opt_delete_option:
4733
	QUICK		{ Select->options|= OPTION_QUICK; }
4734
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; };
4735

4736
truncate:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4737
	TRUNCATE_SYM opt_table_sym table_name
4738
	{
4739
	  LEX* lex= Lex;
4740
	  lex->sql_command= SQLCOM_TRUNCATE;
4741
	  lex->select_lex.options= 0;
4742
	  lex->select_lex.init_order();
4743 4744
	}
	;
4745

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4746 4747
opt_table_sym:
	/* empty */
4748
	| TABLE_SYM;
4749

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4750 4751
/* Show things */

4752 4753
show:	SHOW
	{
4754 4755 4756 4757
	  LEX *lex=Lex;
	  lex->wild=0;
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4758
	show_param
4759 4760
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4761 4762 4763 4764 4765

show_param:
	DATABASES wild
	  { Lex->sql_command= SQLCOM_SHOW_DATABASES; }
	| TABLES opt_db wild
4766
	  {
4767
	    LEX *lex= Lex;
4768
	    lex->sql_command= SQLCOM_SHOW_TABLES;
4769 4770
	    lex->select_lex.db= $2;
	    lex->select_lex.options= 0;
4771
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4772
	| TABLE_SYM STATUS_SYM opt_db wild
4773
	  {
4774
	    LEX *lex= Lex;
4775
	    lex->sql_command= SQLCOM_SHOW_TABLES;
4776 4777
	    lex->select_lex.options|= SELECT_DESCRIBE;
	    lex->select_lex.db= $3;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4778
	  }
4779
	| OPEN_SYM TABLES opt_db wild
4780
	  {
4781
	    LEX *lex= Lex;
4782
	    lex->sql_command= SQLCOM_SHOW_OPEN_TABLES;
4783 4784
	    lex->select_lex.db= $3;
	    lex->select_lex.options= 0;
4785
	  }
4786
	| opt_full COLUMNS from_or_in table_ident opt_db wild
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4787 4788
	  {
	    Lex->sql_command= SQLCOM_SHOW_FIELDS;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4789 4790
	    if ($5)
	      $4->change_db($5);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4791
	    if (!Select->add_table_to_list(YYTHD, $4, NULL, 0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4792 4793
	      YYABORT;
	  }
4794
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4795
	  TEXT_STRING_sys AND MASTER_LOG_POS_SYM EQ ulonglong_num
4796
	  AND MASTER_SERVER_ID_SYM EQ
4797 4798 4799 4800 4801
	ULONG_NUM
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
4802
	    Lex->mi.server_id = $16;
4803
          }
4804
        | master_or_binary LOGS_SYM
4805 4806
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
4807 4808 4809 4810 4811
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
4812
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
4813
          {
4814 4815
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
4816
          } opt_limit_clause_init
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4817 4818 4819 4820 4821
	| keys_or_index FROM table_ident opt_db
	  {
	    Lex->sql_command= SQLCOM_SHOW_KEYS;
	    if ($4)
	      $3->change_db($4);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4822
	    if (!Select->add_table_to_list(YYTHD, $3, NULL, 0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4823 4824
	      YYABORT;
	  }
4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839
	| COLUMN_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_COLUMN_TYPES;
	  }
	| TABLE_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_TABLE_TYPES;
	  }
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
4840
        | COUNT_SYM '(' '*' ')' WARNINGS
4841
          { (void) create_select_for_variable("warning_count"); }
4842
        | COUNT_SYM '(' '*' ')' ERRORS
4843
	  { (void) create_select_for_variable("error_count"); }
4844
        | WARNINGS opt_limit_clause_init
4845
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
4846
        | ERRORS opt_limit_clause_init
4847
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4848 4849
	| STATUS_SYM wild
	  { Lex->sql_command= SQLCOM_SHOW_STATUS; }
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
4850 4851
        | INNOBASE_SYM STATUS_SYM
          { Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4852 4853
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
4854
	| opt_var_type VARIABLES wild
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4855
	  {
4856
	    THD *thd= YYTHD;
4857 4858
	    thd->lex->sql_command= SQLCOM_SHOW_VARIABLES;
	    thd->lex->option_type= (enum_var_type) $1;
4859
	  }
4860
	| charset wild
4861
	  { Lex->sql_command= SQLCOM_SHOW_CHARSETS; }
4862 4863
	| COLLATION_SYM wild
	  { Lex->sql_command= SQLCOM_SHOW_COLLATIONS; }
4864 4865
	| BERKELEY_DB_SYM LOGS_SYM
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; }
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
4866 4867
	| LOGS_SYM
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4868
	| GRANTS FOR_SYM user
4869 4870 4871 4872 4873 4874
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
	    lex->grant_user->password.str=NullS;
	  }
4875
	| CREATE DATABASE opt_if_not_exists ident
4876 4877
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
4878 4879
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
4880
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4881 4882 4883
        | CREATE TABLE_SYM table_ident
          {
	    Lex->sql_command = SQLCOM_SHOW_CREATE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4884
	    if (!Select->add_table_to_list(YYTHD, $3, NULL,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4885 4886 4887 4888 4889
	      YYABORT;
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4890
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4891 4892 4893
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
4894
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4895

4896 4897 4898 4899
master_or_binary:
	MASTER_SYM
	| BINARY;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4900 4901
opt_db:
	/* empty */  { $$= 0; }
4902
	| from_or_in ident { $$= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4903 4904 4905

wild:
	/* empty */
4906
	| LIKE text_string { Lex->wild= $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4907

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4908 4909
opt_full:
	/* empty */ { Lex->verbose=0; }
4910
	| FULL	    { Lex->verbose=1; };
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4911

4912 4913
from_or_in:
	FROM
4914
	| IN_SYM;
4915

4916 4917
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4918
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
4919 4920 4921

binlog_from:
	/* empty */ { Lex->mi.pos = 4; /* skip magic number */ }
4922
        | FROM ulonglong_num { Lex->mi.pos = $2; };
4923 4924


bk@work.mysql.com's avatar
bk@work.mysql.com committed
4925 4926 4927 4928
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
4929 4930 4931 4932
	  LEX *lex=Lex;
	  lex->wild=0;
	  lex->verbose=0;
	  lex->sql_command=SQLCOM_SHOW_FIELDS;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4933
	  if (!Select->add_table_to_list(lex->thd, $2, NULL,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4934 4935
	    YYABORT;
	}
4936
	opt_describe_column {}
4937
	| describe_command { Lex->describe=1; } select
4938
          {
4939
	    LEX *lex=Lex;
4940
	    lex->select_lex.options|= SELECT_DESCRIBE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4941 4942
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4943 4944 4945

describe_command:
	DESC
4946
	| DESCRIBE;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4947 4948 4949 4950

opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
4951
	| ident
4952
	  { Lex->wild= new String((const char*) $1.str,$1.length,system_charset_info); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4953 4954 4955 4956 4957


/* flush things */

flush:
4958
	FLUSH_SYM opt_no_write_to_binlog
4959 4960 4961
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4962
          lex->no_write_to_binlog= $2;
4963
	}
4964 4965 4966
	flush_options
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4967 4968 4969

flush_options:
	flush_options ',' flush_option
4970
	| flush_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4971 4972

flush_option:
4973
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4974
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4975
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4976 4977 4978 4979
	| HOSTS_SYM	{ Lex->type|= REFRESH_HOSTS; }
	| PRIVILEGES	{ Lex->type|= REFRESH_GRANT; }
	| LOGS_SYM	{ Lex->type|= REFRESH_LOG; }
	| STATUS_SYM	{ Lex->type|= REFRESH_STATUS; }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4980 4981
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
4982 4983
	| DES_KEY_FILE	{ Lex->type|= REFRESH_DES_KEY_FILE; }
 	| RESOURCES     { Lex->type|= REFRESH_USER_RESOURCES; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4984

4985
opt_table_list:
4986 4987
	/* empty */  {;}
	| table_list {;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4988

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4989
reset:
4990 4991 4992 4993
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
4994 4995 4996 4997
	} reset_options
	{}
	;

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4998 4999
reset_options:
	reset_options ',' reset_option
5000
	| reset_option;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5001 5002

reset_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5003 5004
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
5005
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5006

5007
purge:
5008 5009 5010 5011
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
5012 5013 5014 5015 5016
	} purge_options
	{}
	;

purge_options:
5017
	master_or_binary LOGS_SYM purge_option
5018
	;
5019 5020

purge_option:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5021
        TO_SYM TEXT_STRING_sys
5022 5023 5024 5025 5026 5027 5028 5029 5030
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
	  if ($2->check_cols(1) || $2->fix_fields(Lex->thd, 0, &$2))
	  {
	    net_printf(Lex->thd, ER_WRONG_ARGUMENTS, "PURGE LOGS BEFORE");
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5031
	    YYABORT;
5032 5033 5034
	  }
	  Item *tmp= new Item_func_unix_timestamp($2);
	  Lex->sql_command = SQLCOM_PURGE_BEFORE;
5035
	  Lex->purge_time= (ulong) tmp->val_int();
5036
	}
5037
	;
5038

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5039 5040 5041
/* kill threads */

kill:
hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
5042
	KILL_SYM kill_option expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5043
	{
5044
	  LEX *lex=Lex;
hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
5045
	  if ($3->fix_fields(lex->thd, 0, &$3) || $3->check_cols(1))
5046
	  {
5047
	    send_error(lex->thd, ER_SET_CONSTANTS_ONLY);
5048 5049 5050
	    YYABORT;
	  }
          lex->sql_command=SQLCOM_KILL;
hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
5051
	  lex->thread_id= (ulong) $3->val_int();
5052
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5053

hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
5054 5055 5056 5057 5058
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; };

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5059 5060 5061
/* change database */

use:	USE_SYM ident
5062 5063
	{
	  LEX *lex=Lex;
5064 5065
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
5066
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5067 5068 5069

/* import, export of files */

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5070
load:	LOAD DATA_SYM load_data_lock opt_local INFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5071
	{
5072 5073
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
5074 5075
	  lex->lock_option= $3;
	  lex->local_file=  $4;
5076
	  if (!(lex->exchange= new sql_exchange($6.str,0)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5077
	    YYABORT;
5078
	  lex->field_list.empty();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5079 5080 5081 5082
	}
	opt_duplicate INTO TABLE_SYM table_ident opt_field_term opt_line_term
	opt_ignore_lines opt_field_spec
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5083
	  if (!Select->add_table_to_list(YYTHD, $11, NULL, TL_OPTION_UPDATING))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5084 5085 5086
	    YYABORT;
	}
        |
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5087
	LOAD TABLE_SYM table_ident FROM MASTER_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5088 5089
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5090
	  if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5091
	    YYABORT;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5092 5093

        }
5094 5095 5096 5097
        |
	LOAD DATA_SYM FROM MASTER_SYM
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
5098
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5099 5100 5101

opt_local:
	/* empty */	{ $$=0;}
5102
	| LOCAL_SYM	{ $$=1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5103

5104
load_data_lock:
5105
	/* empty */	{ $$= YYTHD->update_lock_default; }
5106 5107
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
5108 5109


bk@work.mysql.com's avatar
bk@work.mysql.com committed
5110 5111 5112
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
5113
	| IGNORE_SYM	{ Lex->duplicates=DUP_IGNORE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5114 5115 5116

opt_field_term:
	/* empty */
5117
	| COLUMNS field_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5118 5119 5120

field_term_list:
	field_term_list field_term
5121
	| field_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5122 5123 5124 5125

field_term:
	TERMINATED BY text_string { Lex->exchange->field_term= $3;}
	| OPTIONALLY ENCLOSED BY text_string
5126 5127 5128 5129 5130
	  {
	    LEX *lex=Lex;
	    lex->exchange->enclosed= $4;
	    lex->exchange->opt_enclosed=1;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5131
	| ENCLOSED BY text_string { Lex->exchange->enclosed= $3;}
5132
	| ESCAPED BY text_string  { Lex->exchange->escaped= $3;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5133 5134 5135

opt_line_term:
	/* empty */
5136
	| LINES line_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5137 5138 5139

line_term_list:
	line_term_list line_term
5140
	| line_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5141 5142 5143

line_term:
	TERMINATED BY text_string { Lex->exchange->line_term= $3;}
5144
	| STARTING BY text_string { Lex->exchange->line_start= $3;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5145 5146 5147 5148

opt_ignore_lines:
	/* empty */
	| IGNORE_SYM NUM LINES
5149
	  { Lex->exchange->skip_lines=atol($2.str); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5150 5151 5152 5153

/* Common definitions */

text_literal:
5154
	TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5155 5156
	{
	  THD *thd= YYTHD;
5157
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5158
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5159
	| NCHAR_STRING
5160
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
5161
	| UNDERSCORE_CHARSET TEXT_STRING
5162
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
5163
	| text_literal TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5164 5165
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5166 5167

text_string:
5168
	TEXT_STRING_literal
5169
	{ $$=  new String($1.str,$1.length,YYTHD->variables.collation_connection); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5170 5171
	| HEX_NUM
	  {
5172
	    Item *tmp = new Item_varbinary($1.str,$1.length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5173
	    $$= tmp ? tmp->val_str((String*) 0) : (String*) 0;
5174 5175 5176
	  }
	;

5177
param_marker:
5178
        '?'
5179
        {
5180
	  LEX *lex=Lex;
5181
          if (YYTHD->prepare_command)
5182
          {
5183
            lex->param_list.push_back($$=new Item_param((uint)(lex->tok_start-(uchar *)YYTHD->query)));
5184
            lex->param_count++;
5185
          }
5186
          else
5187 5188 5189 5190
          {
            yyerror("You have an error in your SQL syntax");
            YYABORT;
          }
5191 5192 5193
        }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5194 5195
literal:
	text_literal	{ $$ =	$1; }
5196 5197
	| NUM		{ $$ =	new Item_int($1.str, (longlong) strtol($1.str, NULL, 10),$1.length); }
	| LONG_NUM	{ $$ =	new Item_int($1.str, (longlong) strtoll($1.str,NULL,10), $1.length); }
5198
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5199 5200 5201
	| REAL_NUM	{ $$ =	new Item_real($1.str, $1.length); }
	| FLOAT_NUM	{ $$ =	new Item_float($1.str, $1.length); }
	| NULL_SYM	{ $$ =	new Item_null();
5202
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
5203
	| HEX_NUM	{ $$ =	new Item_varbinary($1.str,$1.length);}
5204
	| UNDERSCORE_CHARSET HEX_NUM
5205
	  {
5206 5207
	    Item *tmp= new Item_varbinary($2.str,$2.length);
	    String *str= tmp ? tmp->val_str((String*) 0) : (String*) 0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5208 5209
	    $$ = new Item_string(str ? str->ptr() : "", str ? str->length() :
				 0, Lex->charset);
5210
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5211 5212
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
5213
	| TIMESTAMP text_literal { $$ = $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5214 5215 5216 5217 5218 5219 5220

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

insert_ident:
	simple_ident	 { $$=$1; }
5221
	| table_wild	 { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5222 5223

table_wild:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5224
	ident '.' '*'
5225 5226
	{
	  $$ = new Item_field(NullS,$1.str,"*");
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5227
	  Lex->current_select->with_wild++;
5228
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5229
	| ident '.' ident '.' '*'
5230 5231 5232 5233
	{
	  $$ = new Item_field((YYTHD->client_capabilities &
   			     CLIENT_NO_SCHEMA ? NullS : $1.str),
			     $3.str,"*");
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5234
	  Lex->current_select->with_wild++;
5235 5236
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5237 5238

order_ident:
5239
	expr { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5240 5241 5242

simple_ident:
	ident
5243
	{
5244 5245 5246 5247 5248 5249 5250 5251
	  sp_pvar_t *spv;
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;

	  if (spc && (spv = spc->find_pvar(&$1)))
	  { /* We're compiling a stored procedure and found a variable */
	    if (lex->sql_command != SQLCOM_CALL && ! spv->isset)
	    {
5252
	      net_printf(YYTHD, ER_SP_UNINIT_VAR, $1.str);
5253
	      YYABORT;
5254 5255 5256 5257 5258 5259
	    }
	    else
	      $$ = (Item*) new Item_splocal(spv->offset);
	  }
	  else
	  {
pem@mysql.com's avatar
pem@mysql.com committed
5260
	    SELECT_LEX *sel=Select;
5261 5262 5263 5264
	    $$= (sel->parsing_place != SELECT_LEX_NODE::IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
                 (Item*) new Item_field(NullS,NullS,$1.str) :
	         (Item*) new Item_ref(NullS,NullS,$1.str);
5265
	  }
5266
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5267
	| ident '.' ident
5268
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5269
	  THD *thd= YYTHD;
5270
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5271
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5272 5273
	  if (sel->no_table_names_allowed)
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5274
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5275 5276 5277
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $1.str, thd->where);
	  }
5278 5279 5280 5281
	  $$= (sel->parsing_place != SELECT_LEX_NODE::IN_HAVING ||
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field(NullS,$1.str,$3.str) :
	      (Item*) new Item_ref(NullS,$1.str,$3.str);
5282
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5283
	| '.' ident '.' ident
5284
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5285
	  THD *thd= YYTHD;
5286
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5287
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5288 5289
	  if (sel->no_table_names_allowed)
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5290
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5291 5292 5293
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $2.str, thd->where);
	  }
5294 5295 5296 5297
	  $$= (sel->parsing_place != SELECT_LEX_NODE::IN_HAVING ||
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field(NullS,$2.str,$4.str) :
              (Item*) new Item_ref(NullS,$2.str,$4.str);
5298
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5299
	| ident '.' ident '.' ident
5300
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5301
	  THD *thd= YYTHD;
5302
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5303
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5304 5305
	  if (sel->no_table_names_allowed)
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5306
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5307 5308 5309
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $3.str, thd->where);
	  }
5310 5311 5312 5313 5314 5315 5316 5317
	  $$= (sel->parsing_place != SELECT_LEX_NODE::IN_HAVING ||
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field((YYTHD->client_capabilities &
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
	      (Item*) new Item_ref((YYTHD->client_capabilities &
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
                                   $3.str, $5.str);
5318
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5319 5320 5321 5322 5323


field_ident:
	ident			{ $$=$1;}
	| ident '.' ident	{ $$=$3;}	/* Skipp schema name in create*/
5324
	| '.' ident		{ $$=$2;}	/* For Delphi */;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5325 5326 5327

table_ident:
	ident			{ $$=new Table_ident($1); }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5328
	| ident '.' ident	{ $$=new Table_ident(YYTHD, $1,$3,0);}
5329 5330
	| '.' ident		{ $$=new Table_ident($2);}
   /* For Delphi */;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5331

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5332 5333 5334 5335
IDENT_sys:
	IDENT
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5336 5337
	  if (thd->charset_is_system_charset)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5338
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5339 5340
	    thd->convert_string(&$$, system_charset_info,
				$1.str, $1.length, thd->charset());
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5341 5342 5343 5344 5345 5346 5347
	}
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5348 5349
	  if (thd->charset_is_system_charset)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5350
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5351 5352
	    thd->convert_string(&$$, system_charset_info,
				$1.str, $1.length, thd->charset());
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5353 5354 5355
	}
	;

5356
TEXT_STRING_literal:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5357 5358 5359
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5360 5361
	  if (thd->charset_is_collation_connection)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5362
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5363 5364
	    thd->convert_string(&$$, thd->variables.collation_connection,
				$1.str, $1.length, thd->charset());
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5365 5366 5367 5368
	}
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
5369
ident:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5370
	IDENT_sys	    { $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5371 5372
	| keyword
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5373 5374 5375
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
5376 5377
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5378 5379

ident_or_text:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5380 5381 5382
	ident 			{ $$=$1;}
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5383 5384 5385 5386

user:
	ident_or_text
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5387 5388
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5389
	    YYABORT;
5390 5391 5392 5393
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5394 5395
	| ident_or_text '@' ident_or_text
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5396 5397
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5398 5399
	      YYABORT;
	    $$->user = $1; $$->host=$3;
5400
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5401 5402 5403 5404 5405

/* Keyword that we allow for identifiers */

keyword:
	ACTION			{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
5406
	| ADDDATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5407
	| AFTER_SYM		{}
5408
	| AGAINST		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5409
	| AGGREGATE_SYM		{}
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5410
	| ANY_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5411
	| ASCII_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5412
	| AUTO_INC		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5413 5414
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
5415
	| BACKUP_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5416
	| BEGIN_SYM		{}
5417
	| BERKELEY_DB_SYM	{}
5418
	| BINLOG_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5419 5420
	| BIT_SYM		{}
	| BOOL_SYM		{}
5421
	| BOOLEAN_SYM		{}
5422
	| BYTE_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5423
	| BTREE_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5424
	| CACHE_SYM		{}
5425
	| CHANGED		{}
5426
	| CHARSET		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5427
	| CHECKSUM_SYM		{}
5428
	| CIPHER_SYM		{}
5429
	| CLIENT_SYM		{}
5430
	| CLOSE_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5431
	| COLLATION_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5432
	| COMMENT_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5433
	| COMMITTED_SYM		{}
5434
	| COMMIT_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5435
	| COMPRESSED_SYM	{}
5436
	| CONCURRENT		{}
5437
	| CUBE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5438 5439 5440 5441 5442
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
	| DELAY_KEY_WRITE_SYM	{}
5443 5444
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5445
	| DO_SYM		{}
5446
	| DUAL_SYM		{}
5447 5448
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5449 5450 5451
	| DYNAMIC_SYM		{}
	| END			{}
	| ENUM			{}
5452
	| ERRORS		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5453
	| ESCAPE_SYM		{}
5454
	| EVENTS_SYM		{}
5455
	| EXECUTE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5456
	| EXTENDED_SYM		{}
5457
	| FAST_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5458 5459
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5460
	| FULL			{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5461 5462 5463 5464
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
	| FLUSH_SYM		{}
5465
	| GEOMETRY_SYM		{}
5466
	| GEOMETRYCOLLECTION	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5467
	| GRANTS		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5468
	| GLOBAL_SYM		{}
5469
	| HANDLER_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5470
	| HASH_SYM		{}
5471 5472
	| HEAP_SYM		{}
	| HELP_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5473 5474 5475
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
5476
	| INDEXES		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5477
	| ISOLATION		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5478
	| ISAM_SYM		{}
5479
	| ISSUER_SYM		{}
5480
	| INNOBASE_SYM		{}
5481
	| INSERT_METHOD		{}
5482
	| RELAY_THREAD		{}
5483
	| LAST_SYM		{}
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5484
	| LEAVES                {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5485
	| LEVEL_SYM		{}
5486
	| LINESTRING		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5487
	| LOCAL_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5488
	| LOCKS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5489 5490 5491 5492 5493 5494 5495 5496 5497 5498
	| LOGS_SYM		{}
	| MAX_ROWS		{}
	| MASTER_SYM		{}
	| MASTER_HOST_SYM	{}
	| MASTER_PORT_SYM	{}
	| MASTER_LOG_FILE_SYM	{}
	| MASTER_LOG_POS_SYM	{}
	| MASTER_USER_SYM	{}
	| MASTER_PASSWORD_SYM	{}
	| MASTER_CONNECT_RETRY_SYM	{}
5499 5500 5501 5502 5503 5504
	| MASTER_SSL_SYM	{}
	| MASTER_SSL_CA_SYM	{}
	| MASTER_SSL_CAPATH_SYM	{}
	| MASTER_SSL_CERT_SYM	{}
	| MASTER_SSL_CIPHER_SYM	{}
	| MASTER_SSL_KEY_SYM	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5505 5506 5507
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
5508
	| MEDIUM_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5509
	| MERGE_SYM		{}
5510
	| MEMORY_SYM		{}
5511
	| MICROSECOND_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5512 5513 5514
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5515
	| MODE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5516
	| MONTH_SYM		{}
5517 5518 5519
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5520
	| MYISAM_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5521
	| NAMES_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5522 5523
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
5524
	| NEXT_SYM		{}
5525
	| NEW_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5526
	| NO_SYM		{}
5527
	| NONE_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5528
	| NVARCHAR_SYM		{}
5529
	| OFFSET_SYM		{}
5530
	| OLD_PASSWORD		{}
5531
	| OPEN_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5532
	| PACK_KEYS_SYM		{}
5533
	| PARTIAL		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5534
	| PASSWORD		{}
5535
	| POINT_SYM		{}
5536
	| POLYGON		{}
5537
	| PREV_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5538 5539
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5540
	| QUERY_SYM		{}
5541
	| QUICK			{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5542
	| RAID_0_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5543 5544
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5545
	| RAID_STRIPED_SYM	{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5546
	| RAID_TYPE		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5547 5548
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5549 5550
	| RELOAD		{}
	| REPAIR		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5551
	| REPEATABLE_SYM	{}
5552
	| REPLICATION		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5553
	| RESET_SYM		{}
5554
	| RESOURCES		{}
5555
	| RESTORE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5556
	| ROLLBACK_SYM		{}
5557
	| ROLLUP_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5558 5559 5560
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5561
	| RTREE_SYM		{}
5562
	| SAVEPOINT_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5563
	| SECOND_SYM		{}
5564
	| SERIAL_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5565 5566
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5567
	| SIGNED_SYM		{}
5568
	| SIMPLE_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5569
	| SHARE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5570
	| SHUTDOWN		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5571
	| SLAVE			{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5572
	| SOUNDS_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5573
	| SQL_CACHE_SYM		{}
5574
	| SQL_BUFFER_RESULT	{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5575
	| SQL_NO_CACHE_SYM	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5576
	| SQL_THREAD		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5577 5578 5579 5580
	| START_SYM		{}
	| STATUS_SYM		{}
	| STOP_SYM		{}
	| STRING_SYM		{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
5581
	| SUBDATE_SYM		{}
5582
	| SUBJECT_SYM		{}
5583
	| SUPER_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5584 5585
	| TEMPORARY		{}
	| TEXT_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5586
	| TRANSACTION_SYM	{}
5587
	| TRUNCATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5588 5589 5590
	| TIMESTAMP		{}
	| TIME_SYM		{}
	| TYPE_SYM		{}
5591
	| FUNCTION_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5592
	| UNCOMMITTED_SYM	{}
5593
	| UNICODE_SYM		{}
5594
	| UNTIL_SYM		{}
5595
	| USER			{}
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5596
	| USE_FRM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5597
	| VARIABLES		{}
5598
	| VALUE_SYM		{}
5599
	| WARNINGS		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5600
	| WORK_SYM		{}
5601
	| X509_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5602 5603
	| YEAR_SYM		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5604 5605 5606 5607 5608 5609

/* Option functions */

set:
	SET opt_option
	{
5610 5611
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
5612 5613
	  lex->option_type=OPT_DEFAULT;
	  lex->var_list.empty();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5614
	}
5615 5616 5617
	option_value_list
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5618 5619 5620

opt_option:
	/* empty */ {}
5621
	| OPTION {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5622 5623

option_value_list:
5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635
	option_type option_value
	| option_value_list ',' option_type option_value;

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

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5636
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
5637 5638 5639 5640 5641 5642
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

opt_var_ident_type:
	/* empty */		{ $$=OPT_DEFAULT; }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5643
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
5644 5645 5646
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5647 5648

option_value:
5649
	  '@' ident_or_text equal expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5650
	  {
pem@mysql.telia.com's avatar
pem@mysql.telia.com committed
5651 5652
	    Lex->var_list.push_back(new set_var_user(new Item_func_set_user_var($2,$4)));
	  }
5653
	| internal_variable_name equal set_expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5654
	  {
5655
	    LEX *lex=Lex;
pem@mysql.telia.com's avatar
pem@mysql.telia.com committed
5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670

	    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_pvar_t *spv= lex->spcont->find_pvar(&$1.base_name);
              sp_instr_set *i= new sp_instr_set(lex->sphead->instructions(),
	                                        spv->offset, $3, spv->type);

	      lex->sphead->add_instr(i);
	      spv->isset= TRUE;
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5671
	  }
5672
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
5673 5674
	  {
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5675 5676
	    lex->var_list.push_back(new set_var((enum_var_type) $3, $4.var,
						&$4.base_name, $6));
5677 5678 5679 5680
	  }
	| TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	  {
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5681 5682 5683
	    LEX_STRING tmp;
	    tmp.str=0;
	    tmp.length=0;
5684
	    lex->var_list.push_back(new set_var(lex->option_type,
5685
						find_sys_var("tx_isolation"),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5686
						&tmp,
5687
						new Item_int((int32) $4)));
5688
	  }
5689
	| charset old_or_new_charset_name_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5690
	{
5691
	  THD *thd= YYTHD;
5692
	  LEX *lex= Lex;
5693
	  $2= $2 ? $2: global_system_variables.character_set_client;
5694
	  lex->var_list.push_back(new set_var_collation_client($2,thd->variables.collation_database,$2));
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5695
	}
5696
	| NAMES_SYM charset_name_or_default opt_collate
5697
	{
5698
	  THD *thd= YYTHD;
5699
	  LEX *lex= Lex;
5700
	  $2= $2 ? $2 : global_system_variables.character_set_client;
5701 5702
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
5703
	  {
5704 5705
	    net_printf(thd,ER_COLLATION_CHARSET_MISMATCH,$3->name,$2->csname);
	    YYABORT;
5706
	  }
5707
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
5708
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5709
	| PASSWORD equal text_or_password
5710
	  {
5711
	    THD *thd=YYTHD;
5712
	    LEX_USER *user;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5713
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
5714 5715 5716
	      YYABORT;
	    user->host.str=0;
	    user->user.str=thd->priv_user;
5717
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
5718 5719
	  }
	| PASSWORD FOR_SYM user equal text_or_password
5720
	  {
5721 5722
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
5723
	;
5724

5725 5726 5727
internal_variable_name:
	ident
	{
pem@mysql.telia.com's avatar
pem@mysql.telia.com committed
5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746
	  LEX *lex= Lex;
          sp_pcontext *spc= lex->spcont;
	  sp_pvar_t *spv;

	  /* We have to lookup here since local vars can shadow sysvars */
	  if (!spc || !(spv = spc->find_pvar(&$1)))
	  { /* Not an SP local variable */
	    sys_var *tmp=find_sys_var($1.str, $1.length);
	    if (!tmp)
	      YYABORT;
	    $$.var= tmp;
	    $$.base_name.str=0;
	    $$.base_name.length=0;
	  }
	  else
	  { /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
5747
	}
5748 5749 5750 5751 5752 5753 5754
	| ident '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
	      net_printf(YYTHD, ER_VARIABLE_IS_NOT_STRUCT, $3.str);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5755 5756
	    $$.var= tmp;
	    $$.base_name= $1;
5757 5758 5759 5760 5761 5762 5763 5764
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
	      net_printf(YYTHD, ER_VARIABLE_IS_NOT_STRUCT, $3.str);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5765 5766 5767
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
5768
	  }
5769
        ;
5770 5771 5772 5773 5774 5775 5776

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5778 5779 5780 5781
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
5782
	    $$= $3.length ? YYTHD->variables.old_passwords ?
5783 5784 5785 5786 5787 5788 5789 5790
	        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;
5791 5792
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5793

monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5794

5795
set_expr_or_default:
5796 5797
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
5798 5799
	| ON	  { $$=new Item_string("ON",  2, system_charset_info); }
	| ALL	  { $$=new Item_string("ALL", 3, system_charset_info); }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5800
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
5801
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5802 5803


bk@work.mysql.com's avatar
bk@work.mysql.com committed
5804 5805 5806 5807 5808 5809 5810
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
	  Lex->sql_command=SQLCOM_LOCK_TABLES;
	}
5811 5812 5813
	table_lock_list
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5814 5815 5816

table_or_tables:
	TABLE_SYM
5817
	| TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5818 5819 5820

table_lock_list:
	table_lock
5821
	| table_lock_list ',' table_lock;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5822 5823 5824

table_lock:
	table_ident opt_table_alias lock_option
5825
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5826
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
5827 5828
	   YYABORT;
	}
5829
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5830 5831 5832

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
5833
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5834
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
5835 5836
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5837 5838

unlock:
5839 5840
	UNLOCK_SYM table_or_tables { Lex->sql_command=SQLCOM_UNLOCK_TABLES; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5841 5842


5843
/*
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5844
** Handler: direct access to ISAM functions
5845 5846 5847 5848 5849
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
5850 5851
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_OPEN;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5852
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
5853 5854 5855 5856
	    YYABORT;
	}
	| HANDLER_SYM table_ident CLOSE_SYM
	{
5857 5858
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_CLOSE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5859
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
5860 5861
	    YYABORT;
	}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5862
	| HANDLER_SYM table_ident READ_SYM
5863
	{
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
5864 5865 5866
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
5867 5868
	  lex->current_select->select_limit= 1;
	  lex->current_select->offset_limit= 0L;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5869
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
5870
	    YYABORT;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5871
        }
5872
        handler_read_or_scan where_clause opt_limit_clause {}
5873
        ;
5874

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5875 5876
handler_read_or_scan:
	handler_scan_function         { Lex->backup_dir= 0; }
5877 5878
        | ident handler_rkey_function { Lex->backup_dir= $1.str; }
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5879 5880 5881

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
5882 5883
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5884 5885 5886 5887 5888 5889

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;  }
5890 5891
	| handler_rkey_mode
	{
5892 5893
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
5894
	  lex->ha_rkey_mode=$1;
5895
	  if (!(lex->insert_list = new List_item))
5896
	    YYABORT;
5897 5898
	} '(' values ')' { }
        ;
5899 5900

handler_rkey_mode:
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
5901 5902 5903 5904
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
5905 5906
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
5907

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5908 5909 5910 5911 5912
/* GRANT / REVOKE */

revoke:
	REVOKE
	{
5913 5914 5915 5916 5917
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_REVOKE;
	  lex->users_list.empty();
	  lex->columns.empty();
	  lex->grant= lex->grant_tot_col=0;
5918
	  lex->select_lex.db=0;
5919 5920
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
5921
	  bzero((char*) &lex->mqh, sizeof(lex->mqh));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5922
	}
5923 5924 5925 5926 5927
	revoke_command
	{}
        ;

revoke_command:
5928 5929
	grant_privileges ON opt_table FROM user_list
	{}
5930 5931 5932 5933 5934
	|
	ALL PRIVILEGES ',' GRANT FROM user_list
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
5935
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5936 5937 5938 5939

grant:
	GRANT
	{
5940 5941 5942
	  LEX *lex=Lex;
	  lex->users_list.empty();
	  lex->columns.empty();
5943 5944
	  lex->sql_command = SQLCOM_GRANT;
	  lex->grant= lex->grant_tot_col= 0;
5945
	  lex->select_lex.db= 0;
5946 5947
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
5948
	  bzero((char *)&(lex->mqh),sizeof(lex->mqh));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5949 5950
	}
	grant_privileges ON opt_table TO_SYM user_list
5951 5952 5953
	require_clause grant_options
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5954 5955 5956

grant_privileges:
	grant_privilege_list {}
5957
	| ALL PRIVILEGES	{ Lex->grant = GLOBAL_ACLS;}
5958 5959
	| ALL			{ Lex->grant = GLOBAL_ACLS;}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5960 5961 5962

grant_privilege_list:
	grant_privilege
5963
	| grant_privilege_list ',' grant_privilege;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5964 5965

grant_privilege:
serg@serg.mylan's avatar
serg@serg.mylan committed
5966
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
5967 5968 5969
	| 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 {}
5970 5971
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5972 5973 5974 5975
	| INDEX		{ Lex->grant |= INDEX_ACL;}
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
5976
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5977 5978 5979 5980
	| RELOAD	{ Lex->grant |= RELOAD_ACL;}
	| SHUTDOWN	{ Lex->grant |= SHUTDOWN_ACL;}
	| PROCESS	{ Lex->grant |= PROCESS_ACL;}
	| FILE_SYM	{ Lex->grant |= FILE_ACL;}
5981 5982 5983 5984 5985 5986 5987 5988
	| GRANT OPTION  { Lex->grant |= GRANT_ACL;}
	| SHOW DATABASES { Lex->grant |= SHOW_DB_ACL;}
	| SUPER_SYM	{ Lex->grant |= SUPER_ACL;}
	| CREATE TEMPORARY TABLES { Lex->grant |= CREATE_TMP_ACL;}
	| LOCK_SYM TABLES   { Lex->grant |= LOCK_TABLES_ACL; }
	| REPLICATION SLAVE  { Lex->grant |= REPL_SLAVE_ACL;}
	| REPLICATION CLIENT_SYM { Lex->grant |= REPL_CLIENT_ACL;}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5989

5990

5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002
opt_and:
	/* empty */	{}
	| AND		{}
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
6003 6004 6005 6006
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
6007
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "SUBJECT");
6008 6009 6010 6011 6012 6013 6014 6015 6016
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
6017
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "ISSUER");
6018 6019 6020 6021 6022 6023 6024 6025 6026
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
6027
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "CIPHER");
6028 6029 6030 6031 6032
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
6033

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6034 6035 6036
opt_table:
	'*'
	  {
6037
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6038
	    lex->current_select->db= lex->thd->db;
6039
	    if (lex->grant == GLOBAL_ACLS)
6040 6041
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6042
	    {
6043
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
6044
	      YYABORT;
6045
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6046 6047 6048
	  }
	| ident '.' '*'
	  {
6049
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6050
	    lex->current_select->db = $1.str;
6051
	    if (lex->grant == GLOBAL_ACLS)
6052 6053
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6054
	    {
6055
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6056 6057 6058 6059 6060
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
6061
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6062
	    lex->current_select->db = NULL;
6063 6064
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
6065
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6066
	    {
6067
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6068 6069 6070 6071 6072
	      YYABORT;
	    }
	  }
	| table_ident
	  {
6073
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6074
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6075
	      YYABORT;
6076
	    if (lex->grant == GLOBAL_ACLS)
6077
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
6078 6079
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6080 6081 6082


user_list:
6083 6084 6085 6086 6087 6088 6089
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' grant_user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6090 6091 6092 6093 6094 6095 6096 6097


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
6098
             if (YYTHD->variables.old_passwords)
6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115
             {
               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;
             }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6116 6117 6118 6119 6120
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
	  { $$=$1; $1->password=$5 ; }
	| user
6121 6122
	  { $$=$1; $1->password.str=NullS; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6123 6124 6125


opt_column_list:
6126 6127 6128 6129 6130
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
6131
	| '(' column_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6132 6133 6134

column_list:
	column_list ',' column_list_id
6135
	| column_list_id;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6136 6137 6138 6139

column_list_id:
	ident
	{
6140
	  String *new_str = new String((const char*) $1.str,$1.length,system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6141 6142
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
6143
	  LEX *lex=Lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6144 6145
	  while ((point=iter++))
	  {
6146 6147
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6148 6149
		break;
	  }
6150
	  lex->grant_tot_col|= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6151
	  if (point)
6152
	    point->rights |= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6153
	  else
6154
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
6155 6156
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6157

tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
6158 6159

require_clause: /* empty */
6160
        | REQUIRE_SYM require_list
6161 6162 6163
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
6164
        | REQUIRE_SYM SSL_SYM
6165 6166 6167
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
6168
        | REQUIRE_SYM X509_SYM
6169 6170 6171 6172 6173 6174 6175
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
6176
          ;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
6177

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6178
grant_options:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6179
	/* empty */ {}
6180
	| WITH grant_option_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6181

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6182 6183
grant_option_list:
	grant_option_list grant_option {}
6184 6185
	| grant_option {}
        ;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6186 6187 6188

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
6189
        | MAX_QUERIES_PER_HOUR ULONG_NUM
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6190
        {
6191
	  Lex->mqh.questions=$2;
6192
	  Lex->mqh.bits |= 1;
6193
	}
6194
        | MAX_UPDATES_PER_HOUR ULONG_NUM
6195
        {
6196
	  Lex->mqh.updates=$2;
6197
	  Lex->mqh.bits |= 2;
6198
	}
6199
        | MAX_CONNECTIONS_PER_HOUR ULONG_NUM
6200
        {
6201
	  Lex->mqh.connections=$2;
6202
	  Lex->mqh.bits |= 4;
6203 6204
	}
        ;
6205

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6206
begin:
6207 6208
	BEGIN_SYM   { Lex->sql_command = SQLCOM_BEGIN;} opt_work {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6209 6210 6211

opt_work:
	/* empty */ {}
6212 6213
	| WORK_SYM {;}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6214 6215

commit:
6216
	COMMIT_SYM   { Lex->sql_command = SQLCOM_COMMIT;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6217 6218

rollback:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6219
	ROLLBACK_SYM
6220 6221 6222 6223 6224
	{
	  Lex->sql_command = SQLCOM_ROLLBACK;
	}
	| ROLLBACK_SYM TO_SYM SAVEPOINT_SYM ident
	{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
6225
	  Lex->sql_command = SQLCOM_ROLLBACK_TO_SAVEPOINT;
6226 6227 6228 6229 6230 6231 6232 6233
	  Lex->savepoint_name = $4.str;
	};
savepoint:
	SAVEPOINT_SYM ident
	{
	  Lex->sql_command = SQLCOM_SAVEPOINT;
	  Lex->savepoint_name = $2.str;
	};
6234 6235

/*
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6236
   UNIONS : glue selects together
6237 6238 6239
*/


6240
union_clause:
6241
	/* empty */ {}
6242 6243
	| union_list
	;
6244 6245

union_list:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6246
	UNION_SYM    union_option
6247 6248 6249 6250 6251
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
6252
	    net_printf(lex->thd, ER_WRONG_USAGE, "UNION", "INTO");
6253
	    YYABORT;
6254
	  }
6255
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
6256
	  {
6257
	    send_error(lex->thd, ER_SYNTAX_ERROR);
6258 6259
	    YYABORT;
	  }
6260
	  if (mysql_new_select(lex, 0))
6261
	    YYABORT;
6262
          mysql_init_select(lex);
6263
	  lex->current_select->linkage=UNION_TYPE;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6264
	}
6265
	select_init {}
6266
	;
6267 6268

union_opt:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
6269
	union_list {}
6270 6271
	| optional_order_or_limit {}
	;
6272 6273

optional_order_or_limit:
6274
	/* Empty */ {}
6275 6276
	|
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6277
	    THD *thd= YYTHD;
6278
	    LEX *lex= thd->lex;
6279
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6280
	    SELECT_LEX *sel= lex->current_select;
6281
	    SELECT_LEX_UNIT *unit= sel->master_unit();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6282
	    SELECT_LEX *fake= unit->fake_select_lex;
6283 6284 6285 6286 6287 6288
	    if (fake)
	    {
	      unit->global_parameters= fake;
	      fake->no_table_names_allowed= 1;
	      lex->current_select= fake;
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6289
	    thd->where= "global ORDER clause";
6290
	  }
6291
	order_or_limit
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6292 6293
          {
	    THD *thd= YYTHD;
6294
	    thd->lex->current_select->no_table_names_allowed= 0;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6295 6296
	    thd->where= "";
          }
6297 6298 6299
	;

order_or_limit:
6300 6301
	order_clause opt_limit_clause_init
	| limit_clause
6302
	;
6303 6304

union_option:
6305
	/* empty */ {}
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6306
	| ALL {Select->master_unit()->union_option= 1;};
6307

6308 6309
singlerow_subselect:
	subselect_start singlerow_subselect_init
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6310 6311 6312 6313
	subselect_end
	{
	  $$= $2;
	};
6314

6315
singlerow_subselect_init:
6316
	select_init2
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6317
	{
6318
	  $$= new Item_singlerow_subselect(YYTHD,
6319 6320
					   Lex->current_select->master_unit()->
                                             first_select());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6321
	};
6322 6323

exists_subselect:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6324 6325 6326 6327 6328
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
6329 6330

exists_subselect_init:
6331
	select_init2
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6332
	{
6333
	  $$= new Item_exists_subselect(YYTHD,
6334 6335
					Lex->current_select->master_unit()->
					  first_select());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6336
	};
6337

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6338 6339 6340 6341 6342 6343 6344 6345
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
6346
  select_init2
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6347
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6348
    $$= Lex->current_select->master_unit()->first_select();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6349 6350
  };

6351
subselect_start:
6352
	'(' SELECT_SYM
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6353
	{
6354
	  LEX *lex=Lex;
6355
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
6356 6357 6358
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
	  {	
6359 6360 6361
	    send_error(lex->thd, ER_SYNTAX_ERROR);
	    YYABORT;
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6362 6363 6364
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
6365 6366

subselect_end:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6367 6368 6369
	')'
	{
	  LEX *lex=Lex;
6370
	  lex->current_select = lex->current_select->return_after_parsing();
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6371
	};
serg@serg.mylan's avatar
serg@serg.mylan committed
6372