sql_yacc.yy 186 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

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

47
#define yyoverflow(A,B,C,D,E,F) {ulong val= *(F); if(my_yyoverflow((B), (D), &val)) { yyerror((char*) (A)); return 2; } else { *(F)= (YYSIZE_T)val; }}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
48

49 50 51 52 53
#define WARN_DEPRECATED(A,B) \
  push_warning_printf(((THD *)yythd), MYSQL_ERROR::WARN_LEVEL_WARN, \
		      ER_WARN_DEPRECATED_SYNTAX, \
		      ER(ER_WARN_DEPRECATED_SYNTAX), (A), (B)); 

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

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

%{
101
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
102 103 104 105 106 107
%}

%pure_parser					/* We have threads */

%token	END_OF_INPUT

108 109 110 111 112 113
%token CLOSE_SYM
%token HANDLER_SYM
%token LAST_SYM
%token NEXT_SYM
%token PREV_SYM

114
%token	DIV_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
115 116
%token	EQ
%token	EQUAL_SYM
117
%token	SOUNDS_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
118 119 120 121 122 123
%token	GE
%token	GT_SYM
%token	LE
%token	LT
%token	NE
%token	IS
124
%token	MOD_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
125 126 127 128
%token	SHIFT_LEFT
%token	SHIFT_RIGHT
%token  SET_VAR

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

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

448 449 450
%token  ERRORS
%token  WARNINGS

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
451
%token	ASCII_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
452 453 454
%token	BIGINT
%token	BLOB_SYM
%token	CHAR_SYM
455
%token	CHANGED
bk@work.mysql.com's avatar
bk@work.mysql.com committed
456 457 458 459 460 461
%token	COALESCE
%token	DATETIME
%token	DATE_SYM
%token	DECIMAL_SYM
%token	DOUBLE_SYM
%token	ENUM
462
%token	FAST_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
463
%token	FLOAT_SYM
464
%token  GEOMETRY_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
465 466 467 468 469 470 471 472 473
%token	INT_SYM
%token	LIMIT
%token	LONGBLOB
%token	LONGTEXT
%token	MEDIUMBLOB
%token	MEDIUMINT
%token	MEDIUMTEXT
%token	NUMERIC_SYM
%token	PRECISION
sergefp@mysql.com's avatar
sergefp@mysql.com committed
474 475
%token  PREPARE_SYM
%token  DEALLOCATE_SYM
476
%token	QUICK
bk@work.mysql.com's avatar
bk@work.mysql.com committed
477
%token	REAL
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
478
%token	SIGNED_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
479 480 481 482
%token	SMALLINT
%token	STRING_SYM
%token	TEXT_SYM
%token	TIMESTAMP
483 484
%token	TIMESTAMP_ADD
%token	TIMESTAMP_DIFF
bk@work.mysql.com's avatar
bk@work.mysql.com committed
485 486 487 488
%token	TIME_SYM
%token	TINYBLOB
%token	TINYINT
%token	TINYTEXT
489
%token	ULONGLONG_NUM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
490
%token	UNSIGNED
491 492 493
%token	VARBINARY
%token	VARCHAR
%token	VARYING
bk@work.mysql.com's avatar
bk@work.mysql.com committed
494 495
%token	ZEROFILL

gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
496
%token	ADDDATE_SYM
497
%token	AGAINST
bk@work.mysql.com's avatar
bk@work.mysql.com committed
498 499 500 501
%token	ATAN
%token	BETWEEN_SYM
%token	BIT_AND
%token	BIT_OR
502
%token  BIT_XOR
bk@work.mysql.com's avatar
bk@work.mysql.com committed
503 504
%token	CASE_SYM
%token	CONCAT
505
%token	CONCAT_WS
bk@work.mysql.com's avatar
bk@work.mysql.com committed
506 507 508 509 510 511
%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
512
%token	DAY_MICROSECOND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
513 514 515 516
%token	DAY_MINUTE_SYM
%token	DAY_SECOND_SYM
%token	DAY_SYM
%token	DECODE_SYM
517 518
%token	DES_ENCRYPT_SYM
%token	DES_DECRYPT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
519 520 521
%token	ELSE
%token	ELT_FUNC
%token	ENCODE_SYM
522
%token	ENGINE_SYM
523
%token	ENGINES_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
524 525 526 527 528 529
%token	ENCRYPT
%token	EXPORT_SET
%token	EXTRACT_SYM
%token	FIELD_FUNC
%token	FORMAT_SYM
%token	FOR_SYM
530
%token  FRAC_SECOND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
531
%token	FROM_UNIXTIME
532 533
%token	GEOMCOLLFROMTEXT
%token	GEOMFROMTEXT
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
534
%token	GEOMFROMWKB
535
%token  GEOMETRYCOLLECTION
536
%token  GROUP_CONCAT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
537
%token	GROUP_UNIQUE_USERS
538
%token  GET_FORMAT
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
539
%token	HOUR_MICROSECOND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
540 541 542 543 544
%token	HOUR_MINUTE_SYM
%token	HOUR_SECOND_SYM
%token	HOUR_SYM
%token	IDENTIFIED_SYM
%token	IF
545
%token	INSERT_METHOD
bk@work.mysql.com's avatar
bk@work.mysql.com committed
546 547 548
%token	INTERVAL_SYM
%token	LAST_INSERT_ID
%token	LEFT
549
%token	LINEFROMTEXT
550
%token  LINESTRING
bk@work.mysql.com's avatar
bk@work.mysql.com committed
551 552
%token	LOCATE
%token	MAKE_SET_SYM
553
%token	MASTER_POS_WAIT
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
554 555
%token  MICROSECOND_SYM
%token	MINUTE_MICROSECOND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
556 557
%token	MINUTE_SECOND_SYM
%token	MINUTE_SYM
558
%token	MODE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
559 560
%token	MODIFY_SYM
%token	MONTH_SYM
561 562 563
%token	MLINEFROMTEXT
%token	MPOINTFROMTEXT
%token	MPOLYFROMTEXT
564 565 566
%token  MULTILINESTRING
%token  MULTIPOINT
%token  MULTIPOLYGON
bk@work.mysql.com's avatar
bk@work.mysql.com committed
567
%token	NOW_SYM
568
%token	OLD_PASSWORD
bk@work.mysql.com's avatar
bk@work.mysql.com committed
569
%token	PASSWORD
570
%token	POINTFROMTEXT
571
%token	POINT_SYM
572
%token	POLYFROMTEXT
573
%token  POLYGON
bk@work.mysql.com's avatar
bk@work.mysql.com committed
574 575
%token	POSITION_SYM
%token	PROCEDURE
576
%token	QUARTER_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
577 578 579 580 581
%token	RAND
%token	REPLACE
%token	RIGHT
%token	ROUND
%token	SECOND_SYM
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
582
%token	SECOND_MICROSECOND_SYM
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
583
%token	SHARE_SYM
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
584
%token	SUBDATE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
585 586 587 588 589 590
%token	SUBSTRING
%token	SUBSTRING_INDEX
%token	TRIM
%token	UNIQUE_USERS
%token	UNIX_TIMESTAMP
%token	USER
591 592 593
%token	UTC_DATE_SYM
%token	UTC_TIME_SYM
%token	UTC_TIMESTAMP_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
594 595
%token	WEEK_SYM
%token	WHEN_SYM
596
%token	WORK_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
597 598 599
%token	YEAR_MONTH_SYM
%token	YEAR_SYM
%token	YEARWEEK
600 601 602
%token	BENCHMARK_SYM
%token	END
%token	THEN_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
603

604 605 606 607
%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
608
%token	SQL_SMALL_RESULT
609
%token  SQL_BUFFER_RESULT
bk@work.mysql.com's avatar
bk@work.mysql.com committed
610

611 612 613
%token  CURSOR_SYM
%token  ELSEIF_SYM
%token  ITERATE_SYM
614 615
%token  GOTO_SYM
%token  LABEL_SYM
616 617
%token  LEAVE_SYM
%token  LOOP_SYM
618
%token  REPEAT_SYM
619 620 621 622 623
%token  UNTIL_SYM
%token  WHILE_SYM
%token  ASENSITIVE_SYM
%token  INSENSITIVE_SYM
%token  SENSITIVE_SYM
624

625 626
%token  ISSUER_SYM
%token  SUBJECT_SYM
627
%token  CIPHER_SYM
628

629
%token  BEFORE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
630
%left   SET_VAR
631 632
%left	OR_OR_CONCAT OR_SYM XOR
%left	AND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
633 634 635 636 637 638
%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	'-' '+'
639
%left	'*' '/' '%' DIV_SYM MOD_SYM
640
%left   '^'
monty@mysql.com's avatar
monty@mysql.com committed
641
%left	NEG '~'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
642
%right	NOT
643
%right	BINARY COLLATE_SYM
644

bk@work.mysql.com's avatar
bk@work.mysql.com committed
645
%type <lex_str>
646 647
	IDENT IDENT_QUOTED TEXT_STRING REAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM
	LEX_HOSTNAME ULONGLONG_NUM field_ident select_alias ident ident_or_text
648
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
649
	NCHAR_STRING opt_component key_cache_name
650
        sp_opt_label
bk@work.mysql.com's avatar
bk@work.mysql.com committed
651 652 653 654 655

%type <lex_str_ptr>
	opt_table_alias

%type <table>
serg@serg.mylan's avatar
serg@serg.mylan committed
656
	table_ident table_ident_nodb references
bk@work.mysql.com's avatar
bk@work.mysql.com committed
657 658

%type <simple_string>
659
	remember_name remember_end opt_ident opt_db text_or_password
660
	opt_constraint constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
661 662

%type <string>
663
	text_string opt_gconcat_separator
bk@work.mysql.com's avatar
bk@work.mysql.com committed
664 665

%type <num>
666
	type int_type real_type order_dir opt_field_spec lock_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
667
	udf_type if_exists opt_local opt_table_options table_options
668 669
        table_option opt_if_not_exists opt_no_write_to_binlog opt_var_type
        opt_var_ident_type delete_option opt_temporary all_or_any opt_distinct
670
        opt_ignore_leaves fulltext_options spatial_type union_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
671 672

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

675 676
%type <ulonglong_number>
	ulonglong_num
bk@work.mysql.com's avatar
bk@work.mysql.com committed
677

678 679 680
%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
681
%type <item>
682
	literal text_literal insert_ident order_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
683
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
684
	table_wild no_in_expr expr_expr simple_expr no_and_expr udf_expr
685
	using_list expr_or_default set_expr_or_default interval_expr
686
	param_marker singlerow_subselect singlerow_subselect_init
hf@deer.(none)'s avatar
hf@deer.(none) committed
687
	exists_subselect exists_subselect_init geometry_function
688
	signed_literal now_or_signed_literal opt_escape
pem@mysql.com's avatar
pem@mysql.com committed
689
	sp_opt_default
690
	simple_ident_nospvar simple_ident_q
bk@work.mysql.com's avatar
bk@work.mysql.com committed
691

692 693
%type <item_num>
	NUM_literal
bk@work.mysql.com's avatar
bk@work.mysql.com committed
694 695

%type <item_list>
696
	expr_list udf_expr_list udf_expr_list2 when_list
697
	ident_list ident_list_arg
bk@work.mysql.com's avatar
bk@work.mysql.com committed
698 699

%type <key_type>
700
	key_type opt_unique_or_fulltext constraint_key_type
bk@work.mysql.com's avatar
bk@work.mysql.com committed
701

702 703 704
%type <key_alg>
	key_alg opt_btree_or_rtree

bk@work.mysql.com's avatar
bk@work.mysql.com committed
705 706 707 708 709 710 711
%type <string_list>
	key_usage_list

%type <key_part>
	key_part

%type <table_list>
712
	join_table_list  join_table
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
713
        table_factor table_ref
bk@work.mysql.com's avatar
bk@work.mysql.com committed
714

715
%type <date_time_type> date_time_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
716 717
%type <interval> interval

718 719
%type <interval_time_st> interval_time_st

720
%type <db_type> storage_engines
bk@work.mysql.com's avatar
bk@work.mysql.com committed
721 722 723

%type <row_type> row_types

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

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

728
%type <cast_type> cast_type
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
729

bk@work.mysql.com's avatar
bk@work.mysql.com committed
730 731 732 733 734 735
%type <udf_type> udf_func_type

%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword

%type <lex_user> user grant_user

736
%type <charset>
737
	opt_collate
738 739
	charset_name
	charset_name_or_default
740 741
	old_or_new_charset_name
	old_or_new_charset_name_or_default
742 743
	collation_name
	collation_name_or_default
744

745 746
%type <variable> internal_variable_name

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

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
751
%type <NONE>
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
752
	query verb_clause create change select do drop insert replace insert2
753
	insert_values update delete truncate rename
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
754
	show describe load alter optimize keycache preload flush
755
	reset purge begin commit rollback savepoint
756
	slave master_def master_defs master_file_def slave_until_opts
757
	repair restore backup analyze check start checksum
758
	field_list field_list_item field_spec kill column_def key_def
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
759
	keycache_list assign_to_keycache preload_list preload_keys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
760
	select_item_list select_item values_list no_braces
761
	opt_limit_clause delete_limit_clause fields opt_values values
bk@work.mysql.com's avatar
bk@work.mysql.com committed
762
	procedure_list procedure_list2 procedure_item
763
	when_list2 expr_list2 udf_expr_list3 handler
bk@work.mysql.com's avatar
bk@work.mysql.com committed
764 765
	opt_precision opt_ignore opt_column opt_restrict
	grant revoke set lock unlock string_list field_options field_option
766
	field_opt_list opt_binary table_lock_list table_lock
767
	ref_list opt_on_delete opt_on_delete_list opt_on_delete_item use
768
	opt_delete_options opt_delete_option varchar nchar nvarchar
769
	opt_outer table_list table_name opt_option opt_place
bk@work.mysql.com's avatar
bk@work.mysql.com committed
770 771
	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
772
	grant_privilege grant_privilege_list
773
	flush_options flush_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
774 775
	equal optional_braces opt_key_definition key_usage_list2
	opt_mi_check_type opt_to mi_check_types normal_join
776
	table_to_table_list table_to_table opt_table_list opt_as
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
777
	handler_rkey_function handler_read_or_scan
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
778
	single_multi table_wild_list table_wild_one opt_wild
779
	union_clause union_list
780
	precision subselect_start opt_and charset
781
	subselect_end select_var_list select_var_list_init help opt_len
782
	opt_extended_describe
783
        prepare prepare_src execute deallocate 
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
784
	statement sp_suid opt_view_list view_list or_replace algorithm
785
	sp_c_chistics sp_a_chistics sp_chistic sp_c_chistic sp_a_chistic
786
END_OF_INPUT
bk@work.mysql.com's avatar
bk@work.mysql.com committed
787

788
%type <NONE> call sp_proc_stmts sp_proc_stmt
789 790 791
%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
792
%type <lex> sp_cursor_stmt
793
%type <spname> sp_name
794

bk@work.mysql.com's avatar
bk@work.mysql.com committed
795 796
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
797
	',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_CONCAT BETWEEN_SYM CASE_SYM
798
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
799 800 801 802 803
%%


query:
	END_OF_INPUT
804
	{
805
	   THD *thd= YYTHD;
806
	   if (!thd->bootstrap &&
807
	      (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
808
	   {
809
	     send_error(thd,ER_EMPTY_QUERY);
810
	     YYABORT;
811
	   }
812 813
	   else
	   {
814
	     thd->lex->sql_command= SQLCOM_EMPTY_QUERY;
815
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
816
	}
817
	| verb_clause END_OF_INPUT {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
818 819

verb_clause:
820 821 822 823 824 825
	  statement
	| begin
	;

/* Verb clauses, except begin */
statement:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
826 827
	  alter
	| analyze
828
	| backup
829
	| call
bk@work.mysql.com's avatar
bk@work.mysql.com committed
830 831
	| change
	| check
832
	| checksum
bk@work.mysql.com's avatar
bk@work.mysql.com committed
833 834
	| commit
	| create
835
        | deallocate
bk@work.mysql.com's avatar
bk@work.mysql.com committed
836 837
	| delete
	| describe
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
838
	| do
bk@work.mysql.com's avatar
bk@work.mysql.com committed
839
	| drop
840
        | execute
serg@serg.mylan's avatar
serg@serg.mylan committed
841
	| flush
bk@work.mysql.com's avatar
bk@work.mysql.com committed
842
	| grant
serg@serg.mylan's avatar
serg@serg.mylan committed
843 844
	| handler
	| help
bk@work.mysql.com's avatar
bk@work.mysql.com committed
845
	| insert
serg@serg.mylan's avatar
serg@serg.mylan committed
846
	| kill
bk@work.mysql.com's avatar
bk@work.mysql.com committed
847 848 849
	| load
	| lock
	| optimize
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
850
        | keycache
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
851
	| preload
852
        | prepare
853
	| purge
854
	| rename
serg@serg.mylan's avatar
serg@serg.mylan committed
855
	| repair
bk@work.mysql.com's avatar
bk@work.mysql.com committed
856
	| replace
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
857
	| reset
858
	| restore
bk@work.mysql.com's avatar
bk@work.mysql.com committed
859 860
	| revoke
	| rollback
861
	| savepoint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
862 863
	| select
	| set
serg@serg.mylan's avatar
serg@serg.mylan committed
864
	| show
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
865
	| slave
866
	| start
867
	| truncate
bk@work.mysql.com's avatar
bk@work.mysql.com committed
868 869
	| unlock
	| update
870
	| use
serg@serg.mylan's avatar
serg@serg.mylan committed
871
        ;
872

sergefp@mysql.com's avatar
sergefp@mysql.com committed
873
deallocate:
874
        deallocate_or_drop PREPARE_SYM ident
sergefp@mysql.com's avatar
sergefp@mysql.com committed
875 876
        {
          THD *thd=YYTHD;
877
          LEX *lex= thd->lex;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
878 879 880 881 882
          if (thd->command == COM_PREPARE)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
883
          lex->sql_command= SQLCOM_DEALLOCATE_PREPARE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
884 885 886
          lex->prepared_stmt_name= $3;
        };

887 888 889 890 891 892
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


sergefp@mysql.com's avatar
sergefp@mysql.com committed
893
prepare:
894
        PREPARE_SYM ident FROM prepare_src
sergefp@mysql.com's avatar
sergefp@mysql.com committed
895 896
        {
          THD *thd=YYTHD;
897
          LEX *lex= thd->lex;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
898 899 900 901 902
          if (thd->command == COM_PREPARE)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
903
          lex->sql_command= SQLCOM_PREPARE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
904 905 906
          lex->prepared_stmt_name= $2;
        };

907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
prepare_src:
        TEXT_STRING_sys
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $1;
          lex->prepared_stmt_code_is_varref= false;
        }
        | '@' ident_or_text
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $2;
          lex->prepared_stmt_code_is_varref= true;
        };
922

sergefp@mysql.com's avatar
sergefp@mysql.com committed
923 924 925 926
execute:
        EXECUTE_SYM ident
        {
          THD *thd=YYTHD;
927
          LEX *lex= thd->lex;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
928 929 930 931 932
          if (thd->command == COM_PREPARE)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
933
          lex->sql_command= SQLCOM_EXECUTE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
934 935 936 937 938 939 940 941 942 943 944 945
          lex->prepared_stmt_name= $2;
        }
        execute_using
        {}
        ;

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
946 947
        execute_var_list ',' execute_var_ident
        | execute_var_ident
sergefp@mysql.com's avatar
sergefp@mysql.com committed
948 949 950 951 952 953 954 955
        ;

execute_var_ident: '@' ident_or_text
        {
          LEX *lex=Lex;
          LEX_STRING *lexstr= (LEX_STRING*)sql_memdup(&$2, sizeof(LEX_STRING));
          if (!lexstr || lex->prepared_stmt_params.push_back(lexstr))
              YYABORT;
956
        }
sergefp@mysql.com's avatar
sergefp@mysql.com committed
957 958
        ;

959 960
/* help */

961
help:
962
       HELP_SYM ident_or_text
963
       {
964 965 966
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
	  lex->help_arg= $2.str;
967
       };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
968 969 970 971

/* change master */

change:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
972
       CHANGE MASTER_SYM TO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
973 974 975
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
976
	  bzero((char*) &lex->mi, sizeof(lex->mi));
977 978 979 980
        }
       master_defs
	{}
       ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
981 982 983

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
986
master_def:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
987
       MASTER_HOST_SYM EQ TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
988 989 990 991
       {
	 Lex->mi.host = $3.str;
       }
       |
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
992
       MASTER_USER_SYM EQ TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
993 994 995 996
       {
	 Lex->mi.user = $3.str;
       }
       |
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
997
       MASTER_PASSWORD_SYM EQ TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
998 999 1000 1001
       {
	 Lex->mi.password = $3.str;
       }
       |
1002
       MASTER_PORT_SYM EQ ULONG_NUM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1003 1004 1005 1006
       {
	 Lex->mi.port = $3;
       }
       |
1007
       MASTER_CONNECT_RETRY_SYM EQ ULONG_NUM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1008 1009 1010
       {
	 Lex->mi.connect_retry = $3;
       }
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
       | 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;
1035
	 }
1036
       |
1037
         master_file_def
1038
       ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1039

1040 1041
master_file_def:
       MASTER_LOG_FILE_SYM EQ TEXT_STRING_sys
1042
       {
1043
	 Lex->mi.log_file_name = $3.str;
1044
       }
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
       | 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);
         }
1071
       ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1072 1073 1074 1075 1076 1077

/* create a table */

create:
	CREATE opt_table_options TABLE_SYM opt_if_not_exists table_ident
	{
1078
	  THD *thd= YYTHD;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1079 1080
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_CREATE_TABLE;
pem@mysql.com's avatar
pem@mysql.com committed
1081
	  if (!lex->select_lex.add_table_to_list(thd, $5, NULL,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1082
						 TL_OPTION_UPDATING,
pem@mysql.com's avatar
pem@mysql.com committed
1083
						 (using_update_log ?
1084 1085
						  TL_READ_NO_INSERT:
						  TL_READ)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1086 1087 1088 1089 1090 1091 1092
	    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;
1093
	  lex->create_info.db_type= (enum db_type) lex->thd->variables.table_type;
monty@mysql.com's avatar
monty@mysql.com committed
1094
	  lex->create_info.default_table_charset= NULL;
venu@myvenu.com's avatar
venu@myvenu.com committed
1095
	  lex->name=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1096 1097
	}
	create2
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1098
	  { Lex->current_select= &Lex->select_lex; }
monty@mysql.com's avatar
monty@mysql.com committed
1099
	| CREATE opt_unique_or_fulltext INDEX_SYM ident key_alg ON table_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1100
	  {
1101 1102
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_CREATE_INDEX;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1103 1104
	    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
1105
	      YYABORT;
1106 1107 1108 1109
	    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
1110
	  }
1111
	   '(' key_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1112
	  {
1113
	    LEX *lex=Lex;
1114

pem@mysql.com's avatar
pem@mysql.com committed
1115
	    lex->key_list.push_back(new Key($2,$4.str, $5, 0, lex->col_list));
1116
	    lex->col_list.empty();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1117 1118
	  }
	| CREATE DATABASE opt_if_not_exists ident
1119
	  { Lex->create_info.default_table_charset=NULL; }
1120
	  opt_create_database_options
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1121
	  {
1122 1123 1124 1125
	    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
1126
	  }
1127
	| CREATE udf_func_type FUNCTION_SYM sp_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1128
	  {
1129
	    LEX *lex=Lex;
1130
	    lex->spname= $4;
1131
	    lex->udf.type= $2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1132
	  }
1133 1134
	  create_function_tail
	  {}
1135
	| CREATE PROCEDURE sp_name
1136 1137
	  {
	    LEX *lex= Lex;
1138
	    sp_head *sp;
1139

1140 1141 1142 1143 1144
	    if (lex->sphead)
	    {
	      net_printf(YYTHD, ER_SP_NO_RECURSIVE_CREATE, "PROCEDURE");
	      YYABORT;
	    }
1145 1146 1147
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
1148
	    sp->init(lex);
1149 1150 1151

	    sp->m_type= TYPE_ENUM_PROCEDURE;
	    lex->sphead= sp;
1152 1153 1154 1155 1156
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
1157
	    sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
1158
	    YYTHD->client_capabilities &= (~CLIENT_MULTI_QUERIES);
1159
	  }
1160
          '('
1161
	  {
1162 1163
	    LEX *lex= Lex;

1164 1165 1166 1167 1168 1169 1170 1171
	    lex->sphead->m_param_begin= lex->tok_start+1;
	  }
	  sp_pdparam_list
	  ')'
	  {
	    LEX *lex= Lex;

	    lex->sphead->m_param_end= lex->tok_start;
1172
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
1173
	  }
1174
	  sp_c_chistics
1175
	  {
1176 1177 1178
	    LEX *lex= Lex;

	    lex->sphead->m_chistics= &lex->sp_chistics;
1179
	    lex->sphead->m_body_begin= lex->tok_start;
1180
	  }
1181
	  sp_proc_stmt
1182
	  {
1183
	    LEX *lex= Lex;
1184
	    sp_head *sp= lex->sphead;
1185

1186 1187 1188
	    if (sp->check_backpatch(YYTHD))
	      YYABORT;
	    sp->init_strings(YYTHD, lex, $3);
1189 1190
	    lex->sql_command= SQLCOM_CREATE_PROCEDURE;
	    /* Restore flag if it was cleared above */
1191
	    if (sp->m_old_cmq)
1192
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
1193
	    sp->restore_thd_mem_root(YYTHD);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
	  }
	| CREATE or_replace algorithm VIEW_SYM table_ident
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_CREATE_VIEW;
	    lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE;
	    /* first table in list is target VIEW name */
	    if (!lex->select_lex.add_table_to_list(thd, $5, NULL, 0))
              YYABORT;
	  }
	  opt_view_list AS select_init check_option
	  {}
1207 1208
	;

1209
sp_name:
1210 1211 1212 1213 1214 1215 1216 1217 1218
	  IDENT_sys '.' IDENT_sys
	  {
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
	| IDENT_sys
	  {
	    $$= sp_name_current_db_new(YYTHD, $1);
	  }
1219 1220
	;

1221
create_function_tail:
pem@mysql.com's avatar
pem@mysql.com committed
1222
	  RETURNS_SYM udf_type UDF_SONAME_SYM TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1223
	  {
1224
	    LEX *lex=Lex;
1225
	    lex->sql_command = SQLCOM_CREATE_FUNCTION;
1226
	    lex->udf.name = lex->spname->m_name;
1227 1228
	    lex->udf.returns=(Item_result) $2;
	    lex->udf.dl=$4.str;
1229
	  }
1230 1231 1232
	| '('
	  {
	    LEX *lex= Lex;
1233
	    sp_head *sp;
1234

1235 1236 1237 1238 1239
	    if (lex->sphead)
	    {
	      net_printf(YYTHD, ER_SP_NO_RECURSIVE_CREATE, "FUNCTION");
	      YYABORT;
	    }
1240 1241 1242
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
1243
	    sp->init(lex);
1244 1245 1246

	    sp->m_type= TYPE_ENUM_FUNCTION;
	    lex->sphead= sp;
1247 1248 1249 1250 1251
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
1252
	    sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
1253
	    YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
1254
	    lex->sphead->m_param_begin= lex->tok_start+1;
1255 1256 1257
	  }
          sp_fdparam_list ')'
	  {
1258 1259 1260 1261 1262 1263
	    LEX *lex= Lex;

	    lex->sphead->m_param_end= lex->tok_start;
	  }
	  RETURNS_SYM
	  {
1264 1265 1266 1267 1268
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;

	    sp->m_returns_begin= lex->tok_start;
	    sp->m_returns_cs= lex->charset= NULL;
1269
	  }
1270
	  type
1271
	  {
1272
	    LEX *lex= Lex;
1273
	    sp_head *sp= lex->sphead;
1274

1275 1276 1277
	    sp->m_returns_end= lex->tok_start;
	    sp->m_returns= (enum enum_field_types)$8;
	    sp->m_returns_cs= lex->charset;
1278
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
1279
	  }
1280
	  sp_c_chistics
1281
	  {
1282 1283 1284
	    LEX *lex= Lex;

	    lex->sphead->m_chistics= &lex->sp_chistics;
1285
	    lex->sphead->m_body_begin= lex->tok_start;
1286
	  }
1287
	  sp_proc_stmt
1288
	  {
1289
	    LEX *lex= Lex;
1290
	    sp_head *sp= lex->sphead;
1291

1292 1293
	    if (sp->check_backpatch(YYTHD))
	      YYABORT;
1294
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
1295
	    sp->init_strings(YYTHD, lex, lex->spname);
1296
	    /* Restore flag if it was cleared above */
1297
	    if (sp->m_old_cmq)
1298
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
1299
	    sp->restore_thd_mem_root(YYTHD);
1300
	  }
1301 1302
	;

1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
sp_a_chistics:
	  /* Empty */ {}
	| sp_a_chistics sp_a_chistic {}
	;

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

/* Characteristics for both create and alter */
sp_chistic:
	  COMMENT_SYM TEXT_STRING_sys { Lex->sp_chistics.comment= $2; }
	| sp_suid { }
1317 1318
	;

1319 1320 1321 1322
/* Alter characteristics */
sp_a_chistic:
	  sp_chistic     { }
	| NAME_SYM ident { Lex->name= $2.str; }
1323 1324
	;

1325 1326 1327 1328 1329 1330 1331
/* Create characteristics */
sp_c_chistic:
	  sp_chistic            { }
	| LANGUAGE_SYM SQL_SYM  { }
	| DETERMINISTIC_SYM     { Lex->sp_chistics.detistic= TRUE; }
	| NOT DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
	;
1332 1333

sp_suid:
1334 1335 1336 1337 1338 1339 1340 1341
	  SQL_SYM SECURITY_SYM DEFINER_SYM
	  {
	    Lex->sp_chistics.suid= IS_SUID;
	  }
	| SQL_SYM SECURITY_SYM INVOKER_SYM
	  {
	    Lex->sp_chistics.suid= IS_NOT_SUID;
	  }
1342 1343
	;

1344
call:
1345
	  CALL_SYM sp_name
1346 1347 1348 1349
	  {
	    LEX *lex = Lex;

	    lex->sql_command= SQLCOM_CALL;
1350
	    lex->spname= $2;
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
	    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);
	  }
	;

1373 1374
/* Stored FUNCTION parameter declaration list */
sp_fdparam_list:
1375
	  /* Empty */
1376
	| sp_fdparams
1377 1378
	;

1379 1380 1381
sp_fdparams:
	  sp_fdparams ',' sp_fdparam
	| sp_fdparam
1382 1383
	;

1384
sp_fdparam:
1385
	  ident type
1386
	  {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1387 1388 1389 1390 1391
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
1392
	      net_printf(YYTHD, ER_SP_DUP_PARAM, $1.str);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1393 1394 1395
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum enum_field_types)$2, sp_param_in);
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
1411
	  sp_opt_inout ident type
1412
	  {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1413 1414 1415 1416 1417
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$2, TRUE))
	    {
1418
	      net_printf(YYTHD, ER_SP_DUP_PARAM, $2.str);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1419 1420 1421 1422
	      YYABORT;
	    }
	    spc->push_pvar(&$2, (enum enum_field_types)$3,
			   (sp_param_mode_t)$1);
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
	  }
	;

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

sp_proc_stmts:
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1434
	  /* Empty */ {}
1435 1436
	| sp_proc_stmts  { Lex->query_tables= 0; } sp_proc_stmt ';'

1437 1438 1439 1440 1441
	;

sp_decls:
	  /* Empty */
	  {
1442
	    $$.vars= $$.conds= $$.hndlrs= $$.curs= 0;
1443 1444 1445
	  }
	| sp_decls sp_decl ';'
	  {
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
	    /* We check for declarations out of (standard) order this way
	       because letting the grammar rules reflect it caused tricky
	       shift/reduce conflicts with the wrong result. (And we get
	       better error handling this way.) */
	    if (($2.vars || $2.conds) && ($1.curs || $1.hndlrs))
	    { /* Variable or condition following cursor or handler */
	      send_error(YYTHD, ER_SP_VARCOND_AFTER_CURSHNDLR);
	      YYABORT;
	    }
	    if ($2.curs && $1.hndlrs)
	    { /* Cursor following handler */
	      send_error(YYTHD, ER_SP_CURSOR_AFTER_HANDLER);
	      YYABORT;
	    }
1460 1461 1462
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
1463
	    $$.curs= $1.curs + $2.curs;
1464 1465 1466 1467
	  }
	;

sp_decl:
1468
	  DECLARE_SYM sp_decl_idents type sp_opt_default
1469 1470
	  {
	    LEX *lex= Lex;
1471 1472
	    sp_pcontext *ctx= lex->spcont;
	    uint max= ctx->context_pvars();
1473 1474
	    enum enum_field_types type= (enum enum_field_types)$3;
	    Item *it= $4;
1475 1476 1477

	    for (uint i = max-$2 ; i < max ; i++)
	    {
1478
	      ctx->set_type(i, type);
1479
	      if (! it)
1480
	        ctx->set_isset(i, FALSE);
1481 1482 1483
	      else
	      {
	        sp_instr_set *in= new sp_instr_set(lex->sphead->instructions(),
1484 1485 1486
	                                           ctx,
						   ctx->pvar_context2index(i),
						   it, type);
1487

1488 1489
		in->tables= lex->query_tables;
		lex->query_tables= 0;
1490
	        lex->sphead->add_instr(in);
1491 1492
	        ctx->set_isset(i, TRUE);
		ctx->set_default(i, it);
1493
	      }
1494
	    }
1495
	    $$.vars= $2;
1496
	    $$.conds= $$.hndlrs= $$.curs= 0;
1497 1498 1499
	  }
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1500 1501 1502 1503 1504
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1505
	      net_printf(YYTHD, ER_SP_DUP_COND, $2.str);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1506 1507
	      YYABORT;
	    }
1508
	    YYTHD->lex->spcont->push_cond(&$2, $5);
1509
	    $$.vars= $$.hndlrs= $$.curs= 0;
1510 1511 1512 1513 1514 1515 1516 1517
	    $$.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=
1518 1519
              new sp_instr_hpush_jump(sp->instructions(), ctx, $2,
	                              ctx->current_pvars());
1520 1521 1522 1523

	    sp->add_instr(i);
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
	    ctx->add_handler();
1524
	    sp->m_in_handler= TRUE;
1525 1526 1527 1528 1529
	  }
	  sp_hcond_list sp_proc_stmt
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
1530
	    sp_pcontext *ctx= lex->spcont;
1531 1532 1533
	    sp_label_t *hlab= lex->spcont->pop_label(); /* After this hdlr */

	    if ($2 == SP_HANDLER_CONTINUE)
1534 1535
	      sp->add_instr(new sp_instr_hreturn(sp->instructions(), ctx,
	                                         ctx->current_pvars()));
1536 1537
	    else
	    {  /* EXIT or UNDO handler, just jump to the end of the block */
1538
	      sp_instr_jump *i= new sp_instr_jump(sp->instructions(), ctx);
1539 1540 1541 1542 1543

	      sp->add_instr(i);
	      sp->push_backpatch(i, lex->spcont->last_label()); /* Block end */
	    }
	    lex->sphead->backpatch(hlab);
1544
	    sp->m_in_handler= FALSE;
1545
	    $$.vars= $$.conds= $$.curs= 0;
1546 1547 1548 1549
	    $$.hndlrs= $6;
	  }
	| DECLARE_SYM ident CURSOR_SYM FOR_SYM sp_cursor_stmt
	  {
1550 1551
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
1552
	    sp_pcontext *ctx= lex->spcont;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1553 1554
	    uint offp;
	    sp_instr_cpush *i;
1555

1556
	    if (ctx->find_cursor(&$2, &offp, TRUE))
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1557
	    {
1558 1559
	      net_printf(YYTHD, ER_SP_DUP_CURS, $2.str);
	      delete $5;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1560 1561
	      YYABORT;
	    }
1562
            i= new sp_instr_cpush(sp->instructions(), ctx, $5);
1563
	    sp->add_instr(i);
1564
	    ctx->push_cursor(&$2);
1565
	    $$.vars= $$.conds= $$.hndlrs= 0;
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
	    $$.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);
	  }
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
	;

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;
	  }
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1633
	| SQLSTATE_SYM opt_value TEXT_STRING_literal
1634
	  {		/* SQLSTATE */
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1635 1636
	    uint len= ($3.length < sizeof($$->sqlstate)-1 ?
                       $3.length : sizeof($$->sqlstate)-1);
1637 1638 1639

	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1640
	    memcpy($$->sqlstate, $3.str, len);
1641 1642 1643 1644
	    $$->sqlstate[len]= '\0';
	  }
	;

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1645 1646 1647 1648 1649
opt_value:
	  /* Empty */  {}
	| VALUE_SYM    {}
	;

1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
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;
1678 1679 1680 1681 1682 1683
	  }
	;

sp_decl_idents:
	  ident
	  {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1684 1685 1686 1687 1688
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
1689
	      net_printf(YYTHD, ER_SP_DUP_VAR, $1.str);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1690 1691 1692
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum_field_types)0, sp_param_in);
1693 1694 1695 1696
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1697 1698 1699 1700 1701
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$3, TRUE))
	    {
1702
	      net_printf(YYTHD, ER_SP_DUP_VAR, $3.str);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1703 1704 1705
	      YYABORT;
	    }
	    spc->push_pvar(&$3, (enum_field_types)0, sp_param_in);
1706 1707 1708 1709
	    $$= $1 + 1;
	  }
	;

1710 1711 1712 1713 1714
sp_opt_default:
	  /* Empty */ { $$ = NULL; }
        | DEFAULT expr { $$ = $2; }
	;

1715
sp_proc_stmt:
1716 1717 1718
	  {
	    Lex->sphead->reset_lex(YYTHD);
	  }
1719 1720 1721 1722
	  statement
	  {
	    LEX *lex= Lex;

1723
	    if ((lex->sql_command == SQLCOM_SELECT && !lex->result) ||
1724
	        sp_multi_results_command(lex->sql_command))
1725
	    {
1726
	      /* We maybe have one or more SELECT without INTO */
1727
	      lex->sphead->m_multi_results= TRUE;
1728
	    }
1729 1730 1731 1732 1733
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
	      send_error(YYTHD, ER_SP_NO_USE);
	      YYABORT;
	    }
1734 1735 1736 1737 1738
	    /* 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 ||
1739
		! lex->var_list.is_empty())
1740
	    {
1741 1742 1743 1744 1745
	      /* 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.
1746
	      */
1747 1748
	      if (lex->sphead->m_type == TYPE_ENUM_FUNCTION &&
		  lex->sql_command != SQLCOM_SET_OPTION)
1749
	      {
1750
		send_error(YYTHD, ER_SP_BADSTATEMENT);
1751 1752 1753 1754
		YYABORT;
	      }
	      else
	      {
1755 1756
		sp_instr_stmt *i=new sp_instr_stmt(lex->sphead->instructions(),
						   lex->spcont);
1757 1758 1759

		i->set_lex(lex);
		lex->sphead->add_instr(i);
1760
		lex->sp_lex_in_use= TRUE;
1761 1762 1763
	      }
            }
	    lex->sphead->restore_lex(YYTHD);
1764
          }
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
	| RETURN_SYM expr
	  {
	    LEX *lex= Lex;

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

1778 1779 1780 1781 1782 1783
	      if ($2->type() == Item::SUBSELECT_ITEM)
	      {  /* QQ For now, just disallow subselects as values */
	        send_error(lex->thd, ER_SP_BADSTATEMENT);
	        YYABORT;
	      }
	      i= new sp_instr_freturn(lex->sphead->instructions(),
1784
				      lex->spcont,
1785
		                      $2, lex->sphead->m_returns);
1786
	      lex->sphead->add_instr(i);
1787
	      lex->sphead->m_has_return= TRUE;
1788 1789
	    }
	  }
1790
	| IF sp_if END IF {}
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
	| 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;
1803
	    uint offset= lex->spcont->current_pvars();
1804
	    sp_instr_set *i = new sp_instr_set(lex->sphead->instructions(),
1805
					       lex->spcont,
1806 1807 1808 1809 1810
	                                       offset, $2, MYSQL_TYPE_STRING);
	    LEX_STRING dummy;

	    dummy.str= (char *)"";
	    dummy.length= 0;
1811
	    lex->spcont->push_pvar(&dummy, MYSQL_TYPE_STRING, sp_param_in);
1812 1813
	    i->tables= lex->query_tables;
	    lex->query_tables= 0;
1814 1815 1816 1817 1818
	    lex->sphead->add_instr(i);
	    lex->sphead->m_simple_case= TRUE;
	  }
	  sp_case END CASE_SYM
	  {
1819
	    Lex->spcont->pop_pvar();
1820
	  }
1821 1822 1823 1824 1825
	| sp_labeled_control
	  {}
	| { /* Unlabeled controls get a secret label. */
	    LEX *lex= Lex;

1826
	    lex->spcont->push_label((char *)"", lex->sphead->instructions());
1827 1828 1829
	  }
	  sp_unlabeled_control
	  {
1830 1831
	    LEX *lex= Lex;

1832
	    lex->sphead->backpatch(lex->spcont->pop_label());
1833 1834 1835 1836
	  }
	| LEAVE_SYM IDENT
	  {
	    LEX *lex= Lex;
1837
	    sp_head *sp = lex->sphead;
1838 1839
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);
1840 1841 1842

	    if (! lab)
	    {
1843
	      net_printf(YYTHD, ER_SP_LILABEL_MISMATCH, "LEAVE", $2.str);
1844 1845 1846 1847
	      YYABORT;
	    }
	    else
	    {
1848 1849 1850 1851 1852
	      uint ip= sp->instructions();
	      sp_instr_jump *i;
	      sp_instr_hpop *ih;
	      sp_instr_cpop *ic;

1853
	      ih= new sp_instr_hpop(ip++, ctx, 0);
1854 1855
	      sp->push_backpatch(ih, lab);
	      sp->add_instr(ih);
1856
	      ic= new sp_instr_cpop(ip++, ctx, 0);
1857 1858
	      sp->push_backpatch(ic, lab);
	      sp->add_instr(ic);
1859
	      i= new sp_instr_jump(ip, ctx);
1860 1861
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
1862 1863 1864 1865 1866
	    }
	  }
	| ITERATE_SYM IDENT
	  {
	    LEX *lex= Lex;
1867 1868 1869
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);
1870

1871
	    if (! lab || lab->type != SP_LAB_ITER)
1872
	    {
1873
	      net_printf(YYTHD, ER_SP_LILABEL_MISMATCH, "ITERATE", $2.str);
1874 1875 1876 1877
	      YYABORT;
	    }
	    else
	    {
1878 1879
	      sp_instr_jump *i;
	      uint ip= sp->instructions();
1880 1881 1882 1883 1884 1885 1886 1887 1888
	      uint n;

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

1899 1900 1901 1902 1903 1904 1905
	    if (lab)
	    {
	      net_printf(YYTHD, ER_SP_LABEL_REDEFINE, $2.str);
	      YYABORT;
	    }
	    else
	    {
1906
	      lab= ctx->push_label($2.str, sp->instructions());
1907
	      lab->type= SP_LAB_GOTO;
1908
	      lab->ctx= ctx;
1909 1910 1911 1912 1913 1914 1915 1916 1917
              sp->backpatch(lab);
	    }
	  }
	| GOTO_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= lex->sphead->instructions();
1918
	    sp_label_t *lab;
1919 1920 1921 1922
	    sp_instr_jump *i;
	    sp_instr_hpop *ih;
	    sp_instr_cpop *ic;

1923 1924 1925 1926 1927 1928
	    if (sp->m_in_handler)
	    {
	      send_error(lex->thd, ER_SP_GOTO_IN_HNDLR);
	      YYABORT;
	    }
	    lab= ctx->find_label($2.str);
1929 1930 1931 1932 1933 1934
	    if (! lab)
	    {
	      lab= (sp_label_t *)YYTHD->alloc(sizeof(sp_label_t));
	      lab->name= $2.str;
	      lab->ip= 0;
	      lab->type= SP_LAB_REF;
1935
	      lab->ctx= ctx;
1936

1937
	      ih= new sp_instr_hpop(ip++, ctx, 0);
1938 1939
	      sp->push_backpatch(ih, lab);
	      sp->add_instr(ih);
1940
	      ic= new sp_instr_cpop(ip++, ctx, 0);
1941 1942
	      sp->add_instr(ic);
	      sp->push_backpatch(ic, lab);
1943
	      i= new sp_instr_jump(ip, ctx);
1944 1945 1946 1947 1948
	      sp->push_backpatch(i, lab);  /* Jumping forward */
	      sp->add_instr(i);
	    }
	    else
	    {
1949 1950 1951 1952
	      uint n;

	      n= ctx->diff_handlers(lab->ctx);
	      if (n)
1953
	      {
1954
	        ih= new sp_instr_hpop(ip++, ctx, n);
1955 1956
	        sp->add_instr(ih);
	      }
1957 1958
	      n= ctx->diff_cursors(lab->ctx);
	      if (n)
1959
	      {
1960
	        ic= new sp_instr_cpop(ip++, ctx, n);
1961 1962
	        sp->add_instr(ic);
	      }
1963
	      i= new sp_instr_jump(ip, ctx, lab->ip); /* Jump back */
1964
	      sp->add_instr(i);
1965 1966
	    }
	  }
1967
	| OPEN_SYM ident
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
	  {
	    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;
	    }
1979
	    i= new sp_instr_copen(sp->instructions(), lex->spcont, offset);
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
	    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;
	    }
1994
	    i= new sp_instr_cfetch(sp->instructions(), lex->spcont, offset);
1995 1996 1997 1998
	    sp->add_instr(i);
	  }
	  sp_fetch_list
	  { }
1999
	| CLOSE_SYM ident
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
	  {
	    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;
	    }
2011
	    i= new sp_instr_cclose(sp->instructions(), lex->spcont,  offset);
2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
	    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;
	    }
	  }
2058
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2059

2060 2061 2062
sp_if:
	  expr THEN_SYM
	  {
2063 2064 2065
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
2066
	    uint ip= sp->instructions();
2067
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, ctx, $1);
2068

2069 2070
	    i->tables= lex->query_tables;
	    lex->query_tables= 0;
2071
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
2072 2073 2074 2075 2076 2077 2078
            sp->add_instr(i);
	  }
	  sp_proc_stmts
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
2079
	    sp_instr_jump *i = new sp_instr_jump(ip, ctx);
2080 2081 2082

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

2089
	    lex->sphead->backpatch(lex->spcont->pop_label());
2090 2091 2092 2093 2094 2095 2096 2097 2098
	  }
	;

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

2099 2100 2101
sp_case:
	  expr THEN_SYM
	  {
2102 2103
            LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
2104 2105 2106 2107 2108
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

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

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2114
	      ivar.str= (char *)"_tmp_";
2115 2116
	      ivar.length= 5;
	      Item *var= (Item*) new Item_splocal(ivar, 
2117
						  ctx->current_pvars()-1);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2118
	      Item *expr= new Item_func_eq(var, $1);
2119

2120
	      i= new sp_instr_jump_if_not(ip, ctx, expr);
2121
              lex->variables_used= 1;
2122 2123
	    }
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
2124 2125
	    i->tables= lex->query_tables;
	    lex->query_tables= 0;
2126 2127 2128 2129 2130 2131 2132
            sp->add_instr(i);
	  }
	  sp_proc_stmts
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
2133
	    sp_instr_jump *i = new sp_instr_jump(ip, ctx);
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147

	    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:
2148 2149 2150 2151
	  /* Empty */
	  {
	    sp_head *sp= Lex->sphead;
	    uint ip= sp->instructions();
2152 2153
	    sp_instr_error *i= new sp_instr_error(ip, Lex->spcont,
						  ER_SP_CASE_NOT_FOUND);
2154 2155 2156

	    sp->add_instr(i);
	  }
2157
	| ELSE sp_proc_stmts {}
2158
	| WHEN_SYM sp_case {}
2159 2160
	;

2161 2162 2163 2164
sp_labeled_control:
	  IDENT ':'
	  {
	    LEX *lex= Lex;
2165 2166
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);
2167 2168 2169

	    if (lab)
	    {
2170
	      net_printf(YYTHD, ER_SP_LABEL_REDEFINE, $1.str);
2171 2172 2173
	      YYABORT;
	    }
	    else
2174
	    {
2175 2176 2177
	      lab= lex->spcont->push_label($1.str,
	                                   lex->sphead->instructions());
	      lab->type= SP_LAB_ITER;
2178
	    }
2179
	  }
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2180
	  sp_unlabeled_control sp_opt_label
2181 2182 2183
	  {
	    LEX *lex= Lex;

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2184
	    if ($5.str)
2185
	    {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2186 2187 2188 2189 2190 2191 2192 2193
	      sp_label_t *lab= lex->spcont->find_label($5.str);

	      if (!lab ||
	          my_strcasecmp(system_charset_info, $5.str, lab->name) != 0)
	      {
	        net_printf(YYTHD, ER_SP_LABEL_MISMATCH, $5.str);
	        YYABORT;
	      }
2194
	    }
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2195
	    lex->sphead->backpatch(lex->spcont->pop_label());
2196 2197 2198
	  }
	;

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2199 2200 2201 2202 2203 2204 2205
sp_opt_label:
	  /* Empty  */
	  { $$.str= NULL; $$.length= 0; }
	| IDENT
	  { $$= $1; }
	;

2206
sp_unlabeled_control:
2207 2208 2209 2210
	  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. */
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2211 2212
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->last_label();
2213

2214
	    lab->type= SP_LAB_BEGIN;
2215
	    lex->spcont= lex->spcont->push_context();
2216 2217 2218 2219 2220 2221 2222 2223 2224
	  }
	  sp_decls
	  sp_proc_stmts
	  END
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;

2225
  	    sp->backpatch(ctx->last_label());	/* We always have a label */
2226
	    if ($3.hndlrs)
2227 2228
	      sp->add_instr(new sp_instr_hpop(sp->instructions(), ctx,
					      $3.hndlrs));
2229
	    if ($3.curs)
2230 2231 2232
	      sp->add_instr(new sp_instr_cpop(sp->instructions(), ctx,
					      $3.curs));
	    lex->spcont= ctx->pop_context();
2233 2234
	  }
	| LOOP_SYM
2235 2236 2237 2238 2239
	  sp_proc_stmts END LOOP_SYM
	  {
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
2240
	    sp_instr_jump *i = new sp_instr_jump(ip, lex->spcont, lab->ip);
2241 2242 2243

	    lex->sphead->add_instr(i);
	  }
2244
	| WHILE_SYM expr DO_SYM
2245 2246
	  {
	    LEX *lex= Lex;
2247 2248
	    sp_head *sp= lex->sphead;
	    uint ip= sp->instructions();
2249 2250
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, lex->spcont,
							       $2);
2251

2252 2253
	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
2254 2255
	    i->tables= lex->query_tables;
	    lex->query_tables= 0;
2256
            sp->add_instr(i);
2257 2258 2259 2260 2261 2262
	  }
	  sp_proc_stmts END WHILE_SYM
	  {
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
2263
	    sp_instr_jump *i = new sp_instr_jump(ip, lex->spcont, lab->ip);
2264 2265 2266

	    lex->sphead->add_instr(i);
	  }
2267 2268
	| REPEAT_SYM sp_proc_stmts UNTIL_SYM expr END REPEAT_SYM
	  {
2269 2270 2271
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
2272 2273
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, lex->spcont,
							       $4, lab->ip);
2274

2275 2276
	    i->tables= lex->query_tables;
	    lex->query_tables= 0;
2277 2278
            lex->sphead->add_instr(i);
	  }
2279
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2280 2281

create2:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2282
 	'(' create2a {}
2283
	| opt_create_table_options create3 {}
venu@myvenu.com's avatar
venu@myvenu.com committed
2284 2285 2286 2287 2288 2289
	| LIKE table_ident
      	  {
      	    LEX *lex=Lex;
     	    if (!(lex->name= (char *)$2))
              YYABORT;
    	  }
2290 2291 2292 2293 2294 2295
	| '(' LIKE table_ident ')'
      	  {
      	    LEX *lex=Lex;
     	    if (!(lex->name= (char *)$3))
              YYABORT;
    	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2296
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2297

serg@serg.mylan's avatar
serg@serg.mylan committed
2298 2299
create2a:
        field_list ')' opt_create_table_options create3 {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2300
	|  create_select ')' { Select->set_braces(1);} union_opt {}
2301
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2302 2303

create3:
2304
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
2305
	| opt_duplicate opt_as     create_select
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2306
          { Select->set_braces(0);} union_clause {}
serg@serg.mylan's avatar
serg@serg.mylan committed
2307
	| opt_duplicate opt_as '(' create_select ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2308
          { Select->set_braces(1);} union_opt {}
2309 2310
        ;

serg@serg.mylan's avatar
serg@serg.mylan committed
2311
create_select:
2312
          SELECT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2313
          {
2314 2315
	    LEX *lex=Lex;
	    lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
2316 2317 2318 2319
	    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;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
2320 2321 2322 2323
	    /*
	      following work only with local list, global list is
	      created correctly in this case
	    */
2324
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
2325
	    mysql_init_select(lex);
2326
	    lex->current_select->parsing_place= SELECT_LEX_NODE::SELECT_LIST;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2327
          }
2328 2329 2330 2331
          select_options select_item_list
	  {
	    Select->parsing_place= SELECT_LEX_NODE::NO_MATTER;
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2332
	  opt_select_from
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
2333 2334 2335 2336 2337 2338 2339
	  {
	    /*
	      following work only with local list, global list is
	      created correctly in this case
	    */
	    Lex->current_select->table_list.push_front(&Lex->save_list);
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2340
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2341

2342 2343
opt_as:
	/* empty */ {}
2344
	| AS	    {};
2345

2346 2347 2348 2349 2350 2351 2352 2353 2354
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
2355 2356
	  opt_default COLLATE_SYM collation_name_or_default	
	  { Lex->create_info.default_table_charset=$3; }
2357
	| opt_default charset charset_name_or_default
2358
	  { Lex->create_info.default_table_charset=$3; }
2359
	;
2360

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2361
opt_table_options:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2362
	/* empty */	 { $$= 0; }
2363
	| table_options  { $$= $1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2364 2365 2366

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2369
table_option:
2370
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2371 2372

opt_if_not_exists:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2373
	/* empty */	 { $$= 0; }
2374
	| IF NOT EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2375 2376 2377

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

2380 2381 2382 2383
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
2384 2385
create_table_options:
	create_table_option
2386
	| create_table_option     create_table_options
2387
	| create_table_option ',' create_table_options;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2388 2389

create_table_option:
2390
	ENGINE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; }
2391
	| TYPE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; WARN_DEPRECATED("TYPE=storage_engine","ENGINE=storage_engine"); }
2392 2393 2394
	| 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
2395 2396
	| 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; }
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
	| 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 ')'
2407 2408 2409
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
2410 2411
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
2412
	    lex->create_info.merge_list.elements--;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
2413 2414
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
2415
	    lex->select_lex.table_list.elements=1;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
2416 2417 2418
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
2419
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
2420
	  }
2421
	| opt_default charset opt_equal charset_name_or_default
2422
	  {
2423 2424 2425
	    Lex->create_info.default_table_charset= $4;
	    Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
	  }
2426
	| opt_default COLLATE_SYM opt_equal collation_name_or_default
2427
	  {
2428
	    Lex->create_info.default_table_charset= $4;
2429 2430
	    Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
	  }
2431
	| 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
2432 2433
	| DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys
	  { Lex->create_info.data_file_name= $4.str; }
2434
	| INDEX_SYM 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
2435

2436
storage_engines:
2437 2438 2439 2440
	ident_or_text
	{
	  $$ = ha_resolve_by_name($1.str,$1.length);
	  if ($$ == DB_TYPE_UNKNOWN) {
2441
	    net_printf(YYTHD, ER_UNKNOWN_STORAGE_ENGINE, $1.str);
2442 2443 2444
	    YYABORT;
	  }
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2445 2446 2447 2448 2449

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
2450
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2451 2452 2453 2454

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

2457 2458 2459
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
2460
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
2461

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2462
opt_select_from:
2463
	opt_limit_clause {}
2464
	| select_from select_lock_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2465 2466

udf_func_type:
2467
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
2468
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2469 2470 2471 2472

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
2473
	| INT_SYM {$$ = (int) INT_RESULT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2474 2475 2476

field_list:
	  field_list_item
2477
	| field_list ',' field_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2478 2479 2480


field_list_item:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2481
	   column_def
2482 2483 2484 2485
         | key_def
         ;

column_def:
2486
	  field_spec opt_check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2487 2488 2489 2490
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2491
	;
2492 2493

key_def:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2494
	key_type opt_ident key_alg '(' key_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2495
	  {
2496
	    LEX *lex=Lex;
2497
	    lex->key_list.push_back(new Key($1,$2, $3, 0, lex->col_list));
2498
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2499
	  }
2500 2501 2502 2503
	| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
	  {
	    LEX *lex=Lex;
	    const char *key_name= $3 ? $3:$1;
2504 2505
	    lex->key_list.push_back(new Key($2, key_name, $4, 0,
				    lex->col_list));
2506 2507
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
2508
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2509
	  {
2510
	    LEX *lex=Lex;
2511
	    lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
2512 2513 2514 2515 2516
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
2517 2518 2519
	    lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4 : $1,
					    HA_KEY_ALG_UNDEF, 1,
					    lex->col_list));
2520
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2521
	  }
2522 2523 2524 2525
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
2526
	| opt_constraint check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2527 2528
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
2529 2530 2531
	  }
	;

2532
opt_check_constraint:
2533
	/* empty */
2534 2535 2536 2537 2538
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
2539
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2540 2541

opt_constraint:
2542
	/* empty */		{ $$=(char*) 0; }
monty@mysql.com's avatar
monty@mysql.com committed
2543 2544 2545 2546 2547 2548
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2549 2550 2551 2552

field_spec:
	field_ident
	 {
2553 2554
	   LEX *lex=Lex;
	   lex->length=lex->dec=0; lex->type=0; lex->interval=0;
2555
	   lex->default_value= lex->on_update_value= 0;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2556
	   lex->comment=0;
2557
	   lex->charset=NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2558 2559 2560
	 }
	type opt_attribute
	{
2561
	  LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2562
	  if (add_field_to_list(lex->thd, $1.str,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2563
				(enum enum_field_types) $3,
2564
				lex->length,lex->dec,lex->type,
2565 2566
				lex->default_value, lex->on_update_value, 
                                lex->comment,
2567 2568
				lex->change,lex->interval,lex->charset,
				lex->uint_geom_type))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2569
	    YYABORT;
2570
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2571 2572

type:
2573
	int_type opt_len field_options	{ $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2574 2575 2576 2577 2578 2579
	| 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; }
2580 2581
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
2582
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2583 2584 2585
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
2586
	| nchar '(' NUM ')'		{ Lex->length=$3.str;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2587
					  $$=FIELD_TYPE_STRING;
2588
					  Lex->charset=national_charset_info; }
2589
	| nchar				{ Lex->length=(char*) "1";
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2590
					  $$=FIELD_TYPE_STRING;
2591
					  Lex->charset=national_charset_info; }
2592
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
2593
					  Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2594 2595 2596
					  $$=FIELD_TYPE_STRING; }
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
					  $$=FIELD_TYPE_VAR_STRING; }
2597 2598
	| nvarchar '(' NUM ')'		{ Lex->length=$3.str;
					  $$=FIELD_TYPE_VAR_STRING;
2599
					  Lex->charset=national_charset_info; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2600
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
2601
					  Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2602
					  $$=FIELD_TYPE_VAR_STRING; }
2603
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2604 2605
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
2606 2607
	| TIMESTAMP
	  {
2608
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
2609 2610
	      $$=FIELD_TYPE_DATETIME;
	    else
2611
	      $$=FIELD_TYPE_TIMESTAMP;
2612
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2613 2614 2615
	| TIMESTAMP '(' NUM ')'		{ Lex->length=$3.str;
					  $$=FIELD_TYPE_TIMESTAMP; }
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
2616
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2617
					  $$=FIELD_TYPE_TINY_BLOB; }
2618
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2619
					  $$=FIELD_TYPE_BLOB; }
hf@deer.(none)'s avatar
hf@deer.(none) committed
2620 2621 2622 2623 2624 2625 2626
	| spatial_type			{ 
#ifdef HAVE_SPATIAL
					  Lex->charset=&my_charset_bin;
					  Lex->uint_geom_type= (uint)$1;
					  $$=FIELD_TYPE_GEOMETRY;
#else
	                                  net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
2627 2628
			                             sym_group_geom.name,
	                                             sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
2629 2630 2631
					  YYABORT;
#endif
					}
2632
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2633
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
2634
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2635
					  $$=FIELD_TYPE_LONG_BLOB; }
2636
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2637
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
2638 2639
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
2640
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
2641 2642
	| 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
2643 2644 2645 2646
	| DECIMAL_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
	| NUMERIC_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
2647 2648
	| FIXED_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
2649
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2650
	  {
2651 2652
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2653 2654
	    $$=FIELD_TYPE_ENUM;
	  }
2655
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2656
	  {
2657 2658
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2659
	    $$=FIELD_TYPE_SET;
2660
	  }
2661
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
2662 2663 2664 2665 2666 2667
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
2668
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2669

hf@deer.(none)'s avatar
hf@deer.(none) committed
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
	| POINT_SYM           { $$= Field::GEOM_POINT; }
	| MULTIPOINT          { $$= Field::GEOM_MULTIPOINT; }
	| LINESTRING          { $$= Field::GEOM_LINESTRING; }
	| MULTILINESTRING     { $$= Field::GEOM_MULTILINESTRING; }
	| POLYGON             { $$= Field::GEOM_POLYGON; }
	| MULTIPOLYGON        { $$= Field::GEOM_MULTIPOLYGON; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2681 2682
char:
	CHAR_SYM {}
2683 2684 2685 2686 2687 2688
	;

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2690 2691 2692
varchar:
	char VARYING {}
	| VARCHAR {}
2693 2694 2695 2696
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2697
	| NVARCHAR_SYM {}
2698 2699 2700 2701
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2702 2703 2704 2705 2706 2707

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

real_type:
2711
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2712 2713
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
2714
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2715 2716 2717 2718 2719


float_options:
	/* empty */		{}
	| '(' NUM ')'		{ Lex->length=$2.str; }
2720
	| precision		{};
2721 2722 2723 2724 2725 2726

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

field_options:
	/* empty */		{}
2731
	| field_opt_list	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2732 2733 2734

field_opt_list:
	field_opt_list field_option {}
2735
	| field_option {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2736 2737

field_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2738
	SIGNED_SYM	{}
2739
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
2740
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2741 2742

opt_len:
2743 2744
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2745 2746 2747

opt_precision:
	/* empty */	{}
2748
	| precision	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2749 2750 2751

opt_attribute:
	/* empty */ {}
2752
	| opt_attribute_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2753 2754 2755

opt_attribute_list:
	opt_attribute_list attribute {}
2756
	| attribute;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2757 2758 2759 2760

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
	| NOT NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
2761 2762 2763
	| DEFAULT now_or_signed_literal { Lex->default_value=$2; }
	| ON UPDATE_SYM NOW_SYM optional_braces 
          { Lex->on_update_value= new Item_func_now_local(); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2764
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
2765
	| SERIAL_SYM DEFAULT VALUE_SYM
2766 2767 2768
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
2769
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2770 2771 2772 2773 2774
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
2775
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2776 2777 2778 2779 2780
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
2781
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2782 2783 2784 2785 2786
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
2787
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2788
	  }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2789
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= &$2; }
2790
	| BINARY { Lex->type|= BINCMP_FLAG; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2791 2792
	| COLLATE_SYM collation_name
	  {
2793
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
	    {
	      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
2805

2806 2807 2808 2809 2810
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

2811 2812 2813 2814
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
2815

2816
charset_name:
2817
	ident_or_text
2818
	{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2819
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
2820
	  {
2821
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,$1.str);
2822 2823
	    YYABORT;
	  }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2824 2825 2826
	}
	| BINARY { $$= &my_charset_bin; }
	;
2827

2828 2829 2830
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2831

2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849

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

2850
collation_name:
2851
	ident_or_text
2852 2853 2854
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
2855
	    net_printf(YYTHD,ER_UNKNOWN_COLLATION,$1.str);
2856 2857 2858 2859
	    YYABORT;
	  }
	};

2860 2861
opt_collate:
	/* empty */	{ $$=NULL; }
2862
	| COLLATE_SYM collation_name_or_default { $$=$2; }
2863 2864
	;

2865 2866 2867 2868
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

2869 2870 2871 2872
opt_default:
	/* empty */	{}
	| DEFAULT	{};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2873
opt_binary:
2874
	/* empty */			{ Lex->charset=NULL; }
2875 2876
	| ASCII_SYM			{ Lex->charset=&my_charset_latin1; }
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
2877 2878
	| UNICODE_SYM
	{
2879
	  if (!(Lex->charset=get_charset_by_csname("ucs2",MY_CS_PRIMARY,MYF(0))))
2880 2881 2882 2883 2884
	  {
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,"ucs2");
	    YYABORT;
	  }
	}
2885
	| charset charset_name	{ Lex->charset=$2; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2886

2887 2888 2889
opt_primary:
	/* empty */
	| PRIMARY_SYM
2890
	;
2891

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2892
references:
2893 2894
	REFERENCES table_ident
	{
2895
	  LEX *lex=Lex;
2896 2897 2898 2899 2900 2901
	  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
2902
	};
2903

2904
opt_ref_list:
2905
	/* empty */ opt_on_delete {}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2906
	| '(' ref_list ')' opt_on_delete {};
2907 2908 2909

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
2910
	| ident			{ Lex->ref_list.push_back(new key_part_spec($1.str)); };
2911

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2912 2913 2914

opt_on_delete:
	/* empty */ {}
2915
	| opt_on_delete_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2916 2917 2918

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
2919
	| opt_on_delete_item {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2920 2921

opt_on_delete_item:
2922 2923 2924 2925
	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
2926
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2927 2928

delete_option:
2929 2930 2931 2932
	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
2933
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2934 2935

key_type:
2936
	key_or_index			    { $$= Key::MULTIPLE; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
2937 2938
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
hf@deer.(none)'s avatar
hf@deer.(none) committed
2939 2940 2941 2942 2943
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
	    net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
2944
		       sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
2945 2946 2947
	    YYABORT;
#endif
	  };
2948 2949 2950

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
2951
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2952 2953 2954

key_or_index:
	KEY_SYM {}
2955
	| INDEX_SYM {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2956

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
2957
opt_key_or_index:
2958
	/* empty */ {}
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
2959
	| key_or_index
2960 2961
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2962 2963
keys_or_index:
	KEYS {}
2964
	| INDEX_SYM {}
2965
	| INDEXES {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2966

2967
opt_unique_or_fulltext:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2968 2969
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
serg@serg.mylan's avatar
serg@serg.mylan committed
2970
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
hf@deer.(none)'s avatar
hf@deer.(none) committed
2971 2972 2973 2974 2975 2976
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
	    net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
2977
	               sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
2978 2979 2980
	    YYABORT;
#endif
	  }
serg@serg.mylan's avatar
serg@serg.mylan committed
2981
        ;
2982 2983

key_alg:
2984
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
2985
	| USING opt_btree_or_rtree { $$= $2; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2986
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
2987 2988 2989

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
hf@deer.(none)'s avatar
hf@deer.(none) committed
2990 2991 2992 2993
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2994
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2995 2996 2997

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
2998
	| key_part order_dir		{ Lex->col_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2999 3000 3001

key_part:
	ident			{ $$=new key_part_spec($1.str); }
3002
	| ident '(' NUM ')'	{ $$=new key_part_spec($1.str,(uint) atoi($3.str)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3003 3004 3005

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

3008 3009 3010
opt_component:
	/* empty */	 { $$.str= 0; $$.length= 0; }
	| '.' ident	 { $$=$2; };
3011

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3012 3013
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
3014
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3015 3016 3017 3018 3019 3020 3021 3022

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
3023
	  THD *thd= YYTHD;
3024
	  LEX *lex= thd->lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3025 3026
	  lex->sql_command = SQLCOM_ALTER_TABLE;
	  lex->name=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3027 3028
	  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
3029 3030 3031 3032
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
3033
          lex->select_lex.init_order();
3034
	  lex->select_lex.db=lex->name=0;
3035
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3036
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
3037
	  lex->create_info.default_table_charset= NULL;
3038
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
3039
	  lex->alter_info.reset();          
3040 3041
	  lex->alter_info.is_simple= 1;
	  lex->alter_info.flags= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3042
	}
3043 3044
	alter_list
	{}
3045
	| ALTER DATABASE ident opt_create_database_options
3046 3047 3048 3049
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
	    lex->name=$3.str;
3050
	  }
3051
	| ALTER PROCEDURE sp_name
3052 3053 3054 3055
	  {
	    LEX *lex= Lex;

	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
3056
	    lex->name= 0;
3057 3058
          }
	  sp_a_chistics
3059
	  {
3060
	    THD *thd= YYTHD;
3061
	    LEX *lex=Lex;
3062

3063
	    lex->sql_command= SQLCOM_ALTER_PROCEDURE;
3064
	    lex->spname= $3;
3065
	  }
3066
	| ALTER FUNCTION_SYM sp_name
3067 3068 3069 3070 3071 3072 3073
	  {
	    LEX *lex= Lex;

	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	    lex->name= 0;
          }
	  sp_a_chistics
3074
	  {
3075
	    THD *thd= YYTHD;
3076
	    LEX *lex=Lex;
3077

3078
	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
3079
	    lex->spname= $3;
3080
	  }
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092
	| ALTER VIEW_SYM table_ident
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_CREATE_VIEW;
	    lex->create_view_mode= VIEW_ALTER;
	    lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE;
	    /* first table in list is target VIEW name */
	    lex->select_lex.add_table_to_list(thd, $3, NULL, 0);
	  }
	  opt_view_list AS select_init
	  {}
3093
	;
3094

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3095
alter_list:
3096 3097
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
3098
        | alter_list_item
3099
	| alter_list ',' alter_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3100 3101

add_column:
3102 3103 3104 3105
	ADD opt_column 
	{
	  LEX *lex=Lex;
	  lex->change=0; 
3106
	  lex->alter_info.flags|= ALTER_ADD_COLUMN; 
3107
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3108 3109

alter_list_item:
3110
	add_column column_def opt_place { Lex->alter_info.is_simple= 0; }
3111 3112 3113
	| ADD key_def 
	  { 
	    LEX *lex=Lex;
3114 3115
	    lex->alter_info.is_simple= 0; 
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3116
	  }
3117
	| add_column '(' field_list ')'      { Lex->alter_info.is_simple= 0; }
3118 3119 3120
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
3121
	     lex->change= $3.str; 
3122 3123
	     lex->alter_info.is_simple= 0;
	     lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
3124
	  }
3125
          field_spec opt_place
3126 3127 3128 3129
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
            lex->length=lex->dec=0; lex->type=0; lex->interval=0;
3130
            lex->default_value= lex->on_update_value= 0;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3131
	    lex->comment=0;
3132
	    lex->charset= NULL;
3133 3134
            lex->alter_info.is_simple= 0;
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
3135 3136 3137 3138
          }
          type opt_attribute
          {
            LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3139
            if (add_field_to_list(lex->thd,$3.str,
3140 3141
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
3142 3143
                                  lex->default_value, lex->on_update_value,
                                  lex->comment,
3144 3145
				  $3.str, lex->interval, lex->charset,
				  lex->uint_geom_type))
3146 3147 3148
	       YYABORT;
          }
          opt_place
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3149
	| DROP opt_column field_ident opt_restrict
3150 3151
	  {
	    LEX *lex=Lex;
3152 3153 3154 3155
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
	    			                               $3.str)); 
	    lex->alter_info.is_simple= 0;
	    lex->alter_info.flags|= ALTER_DROP_COLUMN;
3156
	  }
3157
	| DROP FOREIGN KEY_SYM opt_ident { Lex->alter_info.is_simple= 0; }
3158 3159 3160
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
3161 3162 3163 3164
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
				               primary_key_name));
	    lex->alter_info.is_simple= 0;
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
3165
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3166
	| DROP key_or_index field_ident
3167 3168
	  {
	    LEX *lex=Lex;
3169 3170 3171 3172
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
					                       $3.str));
	    lex->alter_info.is_simple= 0;
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
3173
	  }
3174 3175
	| DISABLE_SYM KEYS { Lex->alter_info.keys_onoff= DISABLE; }
	| ENABLE_SYM KEYS  { Lex->alter_info.keys_onoff= ENABLE; }
3176
	| ALTER opt_column field_ident SET DEFAULT signed_literal
3177 3178
	  {
	    LEX *lex=Lex;
3179 3180 3181
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,$6));
	    lex->alter_info.is_simple= 0;
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
3182
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3183
	| ALTER opt_column field_ident DROP DEFAULT
3184 3185
	  {
	    LEX *lex=Lex;
3186 3187 3188 3189
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,
                                                                  (Item*) 0));
	    lex->alter_info.is_simple= 0;
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
3190
	  }
3191
	| RENAME opt_to table_ident
3192
	  {
3193
	    LEX *lex=Lex;
3194
	    lex->select_lex.db=$3->db.str;
3195
	    lex->name= $3->table.str;
3196
	    lex->alter_info.flags|= ALTER_RENAME;
3197
	  }
3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211
	| CONVERT_SYM TO_SYM charset charset_name_or_default opt_collate
	  {
	    if (!$4)
	    {
	      THD *thd= YYTHD;
	      $4= thd->variables.collation_database;
	    }
	    $5= $5 ? $5 : $4;
	    if (!my_charset_same($4,$5))
	    {
	      net_printf(YYTHD,ER_COLLATION_CHARSET_MISMATCH,
			 $5->name,$4->csname);
	      YYABORT;
	    }
3212
	    LEX *lex= Lex;
monty@mysql.com's avatar
monty@mysql.com committed
3213 3214 3215 3216
	    lex->create_info.table_charset= 
	      lex->create_info.default_table_charset= $5;
	    lex->create_info.used_fields|= (HA_CREATE_USED_CHARSET |
					    HA_CREATE_USED_DEFAULT_CHARSET);
3217
	    lex->alter_info.is_simple= 0;
3218
	  }
3219 3220 3221
        | create_table_options_space_separated 
	  {
	    LEX *lex=Lex;
3222 3223
	    lex->alter_info.is_simple= 0; 
	    lex->alter_info.flags|= ALTER_OPTIONS;
3224 3225 3226 3227
	  }
	| order_clause         
	  {
	    LEX *lex=Lex;
3228 3229
	    lex->alter_info.is_simple= 0; 
	    lex->alter_info.flags|= ALTER_ORDER;
3230
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3231 3232 3233

opt_column:
	/* empty */	{}
3234
	| COLUMN_SYM	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3235 3236 3237

opt_ignore:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
3238
	| IGNORE_SYM	{ Lex->duplicates=DUP_IGNORE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3239 3240

opt_restrict:
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
3241 3242 3243 3244
	/* empty */	{ Lex->drop_mode= DROP_DEFAULT; }
	| RESTRICT	{ Lex->drop_mode= DROP_RESTRICT; }
	| CASCADE	{ Lex->drop_mode= DROP_CASCADE; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3245 3246 3247 3248

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
3249
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3250 3251 3252 3253

opt_to:
	/* empty */	{}
	| TO_SYM	{}
3254
	| EQ		{}
3255
	| AS		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3256

nick@mysql.com's avatar
nick@mysql.com committed
3257
/*
3258
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3259 3260
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3261
slave:
3262 3263 3264 3265 3266 3267 3268
	  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));
guilhem@gbichot2.local's avatar
guilhem@gbichot2.local committed
3269
            /* If you change this code don't forget to update SLAVE START too */
3270 3271 3272
          }
          slave_until
          {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3273 3274 3275 3276 3277
        | STOP_SYM SLAVE slave_thread_opts
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_STOP;
	    lex->type = 0;
guilhem@gbichot2.local's avatar
guilhem@gbichot2.local committed
3278
            /* If you change this code don't forget to update SLAVE STOP too */
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3279
          }
3280 3281 3282 3283 3284
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
3285 3286 3287 3288 3289
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
3290 3291 3292 3293 3294 3295 3296 3297
	| 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
3298

3299 3300 3301 3302 3303
start:
	START_SYM TRANSACTION_SYM { Lex->sql_command = SQLCOM_BEGIN;}
	{}
	;

3304
slave_thread_opts:
3305 3306
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
3307
        {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3308
	;
3309 3310

slave_thread_opt_list:
3311
	slave_thread_opt
3312 3313
	| slave_thread_opt_list ',' slave_thread_opt
	;
3314 3315

slave_thread_opt:
3316
	/*empty*/	{}
3317
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
3318
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
3319
	;
3320

3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342
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 ;


3343 3344 3345 3346 3347
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
3348
	table_list FROM TEXT_STRING_sys
3349 3350
        {
	  Lex->backup_dir = $6.str;
3351
        };
3352

3353 3354 3355 3356 3357
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
3358
	table_list TO_SYM TEXT_STRING_sys
3359 3360
        {
	  Lex->backup_dir = $6.str;
3361
        };
3362

3363 3364 3365 3366 3367 3368
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
3369 3370
	table_list opt_checksum_type
        {}
3371 3372
	;

3373 3374 3375 3376 3377 3378
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
3379
repair:
3380
	REPAIR opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3381
	{
3382 3383
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
3384
           lex->no_write_to_binlog= $2;
3385
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3386
	}
3387 3388 3389
	table_list opt_mi_repair_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3390

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3391
opt_mi_repair_type:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3392
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
3393
	| mi_repair_types {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3394

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3395 3396
mi_repair_types:
	mi_repair_type {}
3397
	| mi_repair_type mi_repair_types {};
3398

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3399
mi_repair_type:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3400
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
3401
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
3402
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3403 3404

analyze:
3405
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3406
	{
3407 3408
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
3409
           lex->no_write_to_binlog= $2;
3410
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3411
	}
3412 3413 3414
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3415 3416

check:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3417
	CHECK_SYM table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3418
	{
3419 3420 3421
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECK;
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3422
	}
3423 3424 3425
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3426

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3427 3428
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
3429
	| mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3430 3431 3432

mi_check_types:
	mi_check_type {}
3433
	| mi_check_type mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3434 3435 3436 3437 3438 3439

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3442
optimize:
3443
	OPTIMIZE opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3444
	{
3445 3446
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3447
           lex->no_write_to_binlog= $2;
3448
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3449
	}
3450 3451 3452
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3453

3454 3455 3456
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
3457
	| LOCAL_SYM  { $$= 1; }
3458 3459
	;

3460 3461 3462 3463 3464
rename:
	RENAME table_or_tables
	{
	   Lex->sql_command=SQLCOM_RENAME_TABLE;
	}
3465 3466 3467
	table_to_table_list
	{}
	;
3468 3469 3470

table_to_table_list:
	table_to_table
3471
	| table_to_table_list ',' table_to_table;
3472 3473 3474

table_to_table:
	table_ident TO_SYM table_ident
3475
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3476
	  LEX *lex=Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3477
	  SELECT_LEX *sl= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3478 3479 3480 3481
	  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))
3482
	    YYABORT;
3483
	};
3484

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3485
keycache:
3486
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3487 3488
        {
          LEX *lex=Lex;
3489 3490
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
	  lex->name_and_length= $5;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3491 3492 3493 3494 3495 3496 3497 3498
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
3499
        table_ident cache_keys_spec
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3500 3501 3502 3503 3504 3505
        {
          LEX *lex=Lex;
          SELECT_LEX *sel= &lex->select_lex;
          if (!sel->add_table_to_list(lex->thd, $1, NULL, 0,
                                      TL_READ,
                                      sel->get_use_index(),
3506
                                      (List<String> *)0))
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3507 3508 3509 3510
            YYABORT;
        }
        ;

3511 3512 3513
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
3514
	;
3515

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3516
preload:
3517
	LOAD INDEX_SYM INTO CACHE_SYM
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530
	{
	  LEX *lex=Lex;
	  lex->sql_command=SQLCOM_PRELOAD_KEYS;
	}
	preload_list
	{}
	;

preload_list:
	preload_keys
	| preload_list ',' preload_keys;

preload_keys:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3531
	table_ident cache_keys_spec opt_ignore_leaves
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3532 3533 3534
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3535 3536
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3537 3538 3539 3540 3541 3542
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

3543
cache_keys_spec:
3544
        { Select->interval_list.empty(); }
3545 3546 3547 3548 3549 3550 3551
        cache_key_list_or_empty
        {
          LEX *lex=Lex;
          SELECT_LEX *sel= &lex->select_lex;
          sel->use_index= sel->interval_list;
        }
        ;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3552

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3553
cache_key_list_or_empty:
3554
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
3555
	| opt_key_or_index '(' key_usage_list2 ')'
3556 3557 3558 3559
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3560 3561
	;

3562
opt_ignore_leaves:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3563 3564 3565 3566 3567
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3568
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3569
  Select : retrieve data from table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3570 3571 3572 3573
*/


select:
3574 3575 3576 3577 3578
	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
3579 3580
	}
	;
3581

3582
/* Need select_init2 for subselects. */
3583
select_init:
3584
	SELECT_SYM select_init2
3585
	|
3586 3587
	'(' SELECT_SYM select_part2 ')'
	  {
3588
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3589
            SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3590
	    if (sel->set_braces(1))
3591
	    {
3592
	      yyerror(ER(ER_SYNTAX_ERROR));
3593 3594
	      YYABORT;
	    }
3595 3596
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3597
	  {
3598
	    yyerror(ER(ER_SYNTAX_ERROR));
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3599 3600
	    YYABORT;
	  }
3601
            /* select in braces, can't contain global parameters */
3602 3603 3604
	    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
3605
          } union_opt;
3606

3607 3608
select_init2:
	select_part2
3609
        {
3610
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3611
          SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3612
          if (lex->current_select->set_braces(0))
3613
	  {
3614
	    yyerror(ER(ER_SYNTAX_ERROR));
3615 3616
	    YYABORT;
	  }
3617 3618
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3619
	  {
3620
	    yyerror(ER(ER_SYNTAX_ERROR));
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3621 3622
	    YYABORT;
	  }
3623
	}
3624
	union_clause
3625 3626
	;

3627
select_part2:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3628
	{
3629 3630 3631
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
	  lex->lock_option= TL_READ;
3632 3633
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
3634 3635 3636 3637 3638
	  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
3639
	}
3640
	select_into select_lock_type;
3641

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3642
select_into:
3643 3644
	opt_limit_clause {}
        | into
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3645
	| select_from
3646 3647
	| into select_from
	| select_from into;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3648 3649

select_from:
3650 3651 3652 3653 3654 3655 3656
	  FROM join_table_list where_clause group_clause having_clause
	       opt_order_clause opt_limit_clause procedure_clause
        | FROM DUAL_SYM /* oracle compatibility: oracle always requires FROM
                           clause, and DUAL is system table without fields.
                           Is "SELECT 1 FROM DUAL" any better than
                           "SELECT 1" ? Hmmm :) */
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3657 3658 3659

select_options:
	/* empty*/
3660
	| select_option_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3661 3662 3663

select_option_list:
	select_option_list select_option
3664
	| select_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3665 3666

select_option:
3667
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
3668 3669 3670 3671 3672 3673
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
3674 3675 3676
	| DISTINCT	{ Select->options|= SELECT_DISTINCT; }
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688
	| 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;
	  }
3689
	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
3690 3691 3692 3693
	| SQL_CACHE_SYM
	  {
	    Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
	  }
3694 3695
	| ALL		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3696

monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
3697 3698 3699
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
3700 3701
	  {
	    LEX *lex=Lex;
3702
	    lex->current_select->set_lock_for_tables(TL_WRITE);
3703
	    lex->safe_to_cache_query=0;
3704
	  }
3705
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
3706 3707
	  {
	    LEX *lex=Lex;
3708 3709
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
3710
	    lex->safe_to_cache_query=0;
3711 3712
	  }
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
3713

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3714 3715 3716 3717 3718
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
3719 3720
	    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
3721
	      YYABORT;
pem@mysql.com's avatar
pem@mysql.com committed
3722
	    (thd->lex->current_select->with_wild)++;
3723
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3724 3725 3726 3727 3728


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3729
	    if (add_item_to_list(YYTHD, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3730 3731
	      YYABORT;
	    if ($4.str)
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3732
	      $2->set_name($4.str,$4.length,system_charset_info);
3733 3734 3735 3736 3737 3738
	    else if (!$2->name) {
	      char *str = $1;
	      if (str[-1] == '`')
	        str--;
	      $2->set_name(str,(uint) ($3 - str), YYTHD->charset());
	    }
3739
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3740 3741

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

remember_end:
3745
	{ $$=(char*) Lex->tok_end; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3746 3747 3748

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

select_alias:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3752 3753 3754 3755 3756 3757
	/* 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
3758 3759 3760

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3763
/* all possible expressions */
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
3764 3765 3766 3767
expr:	
	expr_expr	{ $$= $1; }
	| simple_expr	{ $$= $1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3768

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780
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
3781 3782
/* expressions that begin with 'expr' */
expr_expr:
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
3783
	 expr IN_SYM '(' expr_list ')'
3784
	  { $4->push_front($1); $$= new Item_func_in(*$4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3785
	| expr NOT IN_SYM '(' expr_list ')'
3786
	  { $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
3787
        | expr IN_SYM in_subselect
3788
          { $$= new Item_in_subselect($1, $3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3789
	| expr NOT IN_SYM in_subselect
3790
          {
3791
            $$= new Item_func_not(new Item_in_subselect($1, $4));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3792
          }
3793
	| expr BETWEEN_SYM no_and_expr AND_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3794
	  { $$= new Item_func_between($1,$3,$5); }
3795
	| expr NOT BETWEEN_SYM no_and_expr AND_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3796
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
3797
	| expr OR_OR_CONCAT expr { $$= or_or_concat(YYTHD, $1,$3); }
3798
	| expr OR_SYM expr	{ $$= new Item_cond_or($1,$3); }
3799
        | expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
3800
	| expr AND_SYM expr	{ $$= new Item_cond_and($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3801 3802 3803 3804 3805 3806 3807 3808 3809
	| expr SOUNDS_SYM LIKE expr
	  {
	    $$= new Item_func_eq(new Item_func_soundex($1),
				 new Item_func_soundex($4));
	  }
	| expr LIKE simple_expr opt_escape
          { $$= new Item_func_like($1,$3,$4); }
	| expr NOT LIKE simple_expr opt_escape
          { $$= new Item_func_not(new Item_func_like($1,$4,$5));}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3810
	| expr REGEXP expr { $$= new Item_func_regex($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3811 3812
	| expr NOT REGEXP expr
          { $$= new Item_func_not(new Item_func_regex($1,$4)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3813 3814 3815
	| 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
3816
	| expr comp_op expr %prec EQ	{ $$= (*$2)(0)->create($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3817 3818
	| expr comp_op all_or_any in_subselect %prec EQ
	{
3819
	  $$= all_any_subquery_creator($1, $2, $3, $4);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3820
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3821 3822 3823 3824 3825 3826
	| 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); }
3827 3828
	| 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
3829
	| expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
3830
        | expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3831 3832
	| expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| expr '%' expr		{ $$= new Item_func_mod($1,$3); }
3833 3834 3835 3836
	| 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
3837
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3838 3839 3840

/* expressions that begin with 'expr' that do NOT follow IN_SYM */
no_in_expr:
3841
	no_in_expr BETWEEN_SYM no_and_expr AND_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3842
	  { $$= new Item_func_between($1,$3,$5); }
3843
	| no_in_expr NOT BETWEEN_SYM no_and_expr AND_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3844
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
3845
	| no_in_expr OR_OR_CONCAT expr	{ $$= or_or_concat(YYTHD, $1,$3); }
3846
	| no_in_expr OR_SYM expr	{ $$= new Item_cond_or($1,$3); }
3847
        | no_in_expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
3848
	| no_in_expr AND_SYM expr	{ $$= new Item_cond_and($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3849 3850 3851 3852 3853 3854 3855 3856 3857
	| no_in_expr SOUNDS_SYM LIKE expr
	  {
	    $$= new Item_func_eq(new Item_func_soundex($1),
				 new Item_func_soundex($4));
	  }
	| no_in_expr LIKE simple_expr opt_escape
	  { $$= new Item_func_like($1,$3,$4); }
	| no_in_expr NOT LIKE simple_expr opt_escape
	  { $$= new Item_func_not(new Item_func_like($1,$4,$5)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3858
	| no_in_expr REGEXP expr { $$= new Item_func_regex($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3859 3860
	| no_in_expr NOT REGEXP expr
	  { $$= new Item_func_not(new Item_func_regex($1,$4)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3861 3862 3863
	| 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
3864
	| no_in_expr comp_op expr %prec EQ { $$= (*$2)(0)->create($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3865 3866
	| no_in_expr comp_op all_or_any in_subselect %prec EQ
	{
3867
	  all_any_subquery_creator($1, $2, $3, $4);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3868
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3869 3870 3871 3872 3873 3874
	| 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); }
3875
	| no_in_expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3876
	| no_in_expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
3877
        | no_in_expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3878 3879
	| no_in_expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| no_in_expr '%' expr		{ $$= new Item_func_mod($1,$3); }
3880
	| no_in_expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
3881 3882 3883 3884
	| 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); }
3885
	| simple_expr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3886 3887 3888

/* 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
3889
	  no_and_expr IN_SYM '(' expr_list ')'
3890
	  { $4->push_front($1); $$= new Item_func_in(*$4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3891
	| no_and_expr NOT IN_SYM '(' expr_list ')'
3892
	  { $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
3893
        | no_and_expr IN_SYM in_subselect
3894
          { $$= new Item_in_subselect($1, $3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3895
	| no_and_expr NOT IN_SYM in_subselect
3896
          {
3897
            $$= new Item_func_not(new Item_in_subselect($1, $4));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3898
          }
3899
	| no_and_expr BETWEEN_SYM no_and_expr AND_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3900
	  { $$= new Item_func_between($1,$3,$5); }
3901
	| no_and_expr NOT BETWEEN_SYM no_and_expr AND_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3902
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
3903
	| no_and_expr OR_OR_CONCAT expr	{ $$= or_or_concat(YYTHD, $1,$3); }
3904
	| no_and_expr OR_SYM expr	{ $$= new Item_cond_or($1,$3); }
3905
        | no_and_expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3906 3907 3908 3909 3910 3911 3912 3913 3914
	| no_and_expr SOUNDS_SYM LIKE expr
	  {
	    $$= new Item_func_eq(new Item_func_soundex($1),
				 new Item_func_soundex($4));
	  }
	| no_and_expr LIKE simple_expr opt_escape
	  { $$= new Item_func_like($1,$3,$4); }
	| no_and_expr NOT LIKE simple_expr opt_escape
	  { $$= new Item_func_not(new Item_func_like($1,$4,$5)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3915
	| no_and_expr REGEXP expr { $$= new Item_func_regex($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3916 3917
	| no_and_expr NOT REGEXP expr
	  { $$= new Item_func_not(new Item_func_regex($1,$4)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3918 3919 3920
	| 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
3921
	| no_and_expr comp_op expr %prec EQ { $$= (*$2)(0)->create($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3922 3923
	| no_and_expr comp_op all_or_any in_subselect %prec EQ
	{
3924
	  all_any_subquery_creator($1, $2, $3, $4);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3925
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3926 3927 3928 3929 3930 3931
	| 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); }
3932
	| no_and_expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3933
	| no_and_expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
3934
        | no_and_expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3935 3936
	| no_and_expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| no_and_expr '%' expr		{ $$= new Item_func_mod($1,$3); }
3937
	| no_and_expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
3938 3939 3940 3941
	| 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); }
3942
	| simple_expr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3943

3944
interval_expr:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3945
         INTERVAL_SYM expr { $$=$2; }
3946 3947
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3948 3949
simple_expr:
	simple_ident
3950
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3951
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3952 3953 3954
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
3955
                                                            YYTHD->charset()));
3956
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3957
	| literal
3958
	| param_marker
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3959
	| '@' ident_or_text SET_VAR expr
3960 3961
	  {
	    $$= new Item_func_set_user_var($2,$4);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
3962 3963 3964
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3965
	  }
3966
	| '@' ident_or_text
3967 3968
	  {
	    $$= new Item_func_get_user_var($2);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
3969 3970 3971
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3972
	  }
3973
	| '@' '@' opt_var_ident_type ident_or_text opt_component
3974
	  {
3975 3976 3977 3978 3979 3980

            if ($4.str && $5.str && check_reserved_words(&$4))
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
3981
	    if (!($$= get_system_var(YYTHD, (enum_var_type) $3, $4, $5)))
3982
	      YYABORT;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
3983
	    Lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3984
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3985
	| sum_expr
3986 3987
	| '+' expr %prec NEG	{ $$= $2; }
	| '-' expr %prec NEG    { $$= new Item_func_neg($2); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3988 3989 3990 3991
	| '~' 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; }
3992 3993 3994 3995 3996
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
3997
	| ROW_SYM '(' expr ',' expr_list ')'
3998
	  {
3999 4000
	    $5->push_front($3);
	    $$= new Item_row(*$5);
4001
	  }
4002
	| EXISTS exists_subselect { $$= $2; }
4003
	| singlerow_subselect   { $$= $1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4004
	| '{' ident expr '}'	{ $$= $3; }
4005
        | MATCH ident_list_arg AGAINST '(' expr fulltext_options ')'
4006
          { $2->push_front($5);
4007 4008
            Select->add_ftfunc_to_list((Item_func_match*)
                                        ($$=new Item_func_match(*$2,$6))); }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4009
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4010
	| BINARY expr %prec NEG
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4011
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4012 4013
	    $$= 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
4014
	  }
4015
	| CAST_SYM '(' expr AS cast_type ')'
4016 4017
	  {
	    $$= create_func_cast($3, $5,
4018
				 Lex->length ? atoi(Lex->length) : -1,
4019
				 Lex->charset);
4020
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4021
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
4022
	  { $$= new Item_func_case(* $4, $2, $5 ); }
4023
	| CONVERT_SYM '(' expr ',' cast_type ')'
4024 4025 4026 4027 4028
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
				 Lex->charset);
	  }
4029 4030
	| CONVERT_SYM '(' expr USING charset_name ')'
	  { $$= new Item_func_conv_charset($3,$5); }
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
4031 4032
	| DEFAULT '(' simple_ident ')'
	  { $$= new Item_default_value($3); }
4033 4034
	| VALUES '(' simple_ident ')'
	  { $$= new Item_insert_value($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4035
	| FUNC_ARG0 '(' ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
4036 4037 4038 4039
	  {
	    if (!$1.symbol->create_func)
	    {
	      net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4040
			 $1.symbol->group->name,
hf@deer.(none)'s avatar
hf@deer.(none) committed
4041 4042 4043 4044 4045
	                 $1.symbol->group->needed_define);
	      YYABORT;
	    }
	    $$= ((Item*(*)(void))($1.symbol->create_func))();
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4046
	| FUNC_ARG1 '(' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
4047 4048 4049 4050
	  {
	    if (!$1.symbol->create_func)
	    {
	      net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4051
			 $1.symbol->group->name,
hf@deer.(none)'s avatar
hf@deer.(none) committed
4052 4053 4054 4055 4056
	                 $1.symbol->group->needed_define);
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4057
	| FUNC_ARG2 '(' expr ',' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
4058 4059 4060 4061
	  {
	    if (!$1.symbol->create_func)
	    {
	      net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4062
			 $1.symbol->group->name,
hf@deer.(none)'s avatar
hf@deer.(none) committed
4063 4064 4065 4066 4067
	                 $1.symbol->group->needed_define);
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4068
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
4069 4070 4071 4072
	  {
	    if (!$1.symbol->create_func)
	    {
	      net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4073
			 $1.symbol->group->name,
hf@deer.(none)'s avatar
hf@deer.(none) committed
4074 4075 4076 4077 4078
	                 $1.symbol->group->needed_define);
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*,Item*))($1.symbol->create_func))($3,$5,$7);
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4079 4080 4081 4082
	| 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); }
4083 4084
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4085 4086 4087 4088 4089 4090
	| 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); }
4091 4092
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4093 4094
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
4095 4096
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4097 4098 4099 4100 4101
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
	  { $$= new Item_func_concat_ws($3, *$5); }
	| CURDATE optional_braces
4102
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4103
	| CURTIME optional_braces
4104
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4105
	| CURTIME '(' expr ')'
4106
	  {
4107
	    $$= new Item_func_curtime_local($3);
4108
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4109
	  }
4110
	| CURRENT_USER optional_braces
4111
          { $$= create_func_current_user(); }
4112 4113 4114 4115
	| 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
4116
	| DATABASE '(' ')'
4117
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4118
	    $$= new Item_func_database();
4119
            Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4120
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4121
	| DATE_SYM '(' expr ')'
4122
	  { $$= new Item_date_typecast($3); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4123 4124
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4125
	| ELT_FUNC '(' expr ',' expr_list ')'
4126
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4127 4128
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4129 4130 4131
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
4132
	    Lex->uncacheable(UNCACHEABLE_RAND);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4133
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4134
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
4135
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4136
	  { $$= new Item_func_decode($3,$5.str); }
4137
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4138
	 { $$= new Item_func_encode($3,$5.str); }
4139
	| DES_DECRYPT_SYM '(' expr ')'
4140
        { $$= new Item_func_des_decrypt($3); }
4141
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
4142
        { $$= new Item_func_des_decrypt($3,$5); }
4143
	| DES_ENCRYPT_SYM '(' expr ')'
4144
        { $$= new Item_func_des_encrypt($3); }
4145
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
4146
        { $$= new Item_func_des_encrypt($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4147 4148 4149 4150 4151 4152
	| 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); }
4153 4154
	| FALSE_SYM
	  { $$= new Item_int((char*) "FALSE",0,1); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4155 4156 4157 4158 4159 4160
	| 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
4161
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4162 4163
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
4164
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4165 4166 4167 4168 4169 4170
	| geometry_function
	  {
#ifdef HAVE_SPATIAL
	    $$= $1;
#else
	    net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4171
	               sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
4172 4173 4174
	    YYABORT;
#endif
	  }
4175
	| GET_FORMAT '(' date_time_type  ',' expr ')'
4176
	  { $$= new Item_func_get_format($3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4177 4178 4179 4180 4181 4182
	| 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); }
4183
	| interval_expr interval '+' expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4184
	  /* we cannot put interval before - */
4185 4186 4187 4188 4189
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
4190
              yyerror(ER(ER_SYNTAX_ERROR));
4191 4192 4193 4194
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4195 4196
	| LAST_INSERT_ID '(' ')'
	  {
4197
	    $$= new Item_func_last_insert_id();
4198
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4199 4200 4201
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
4202
	    $$= new Item_func_last_insert_id($3);
4203
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4204 4205 4206 4207 4208 4209 4210
	  }
	| LEFT '(' expr ',' expr ')'
	  { $$= new Item_func_left($3,$5); }
	| LOCATE '(' expr ',' expr ')'
	  { $$= new Item_func_locate($5,$3); }
	| LOCATE '(' expr ',' expr ',' expr ')'
	  { $$= new Item_func_locate($5,$3,$7); }
4211
	| GREATEST_SYM '(' expr ',' expr_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4212 4213 4214
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
4215
	| LOG_SYM '(' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4216
	  { $$= new Item_func_log($3); }
4217
	| LOG_SYM '(' expr ',' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4218
	  { $$= new Item_func_log($3, $5); }
4219
	| MASTER_POS_WAIT '(' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4220
	  {
4221
	    $$= new Item_master_pos_wait($3, $5);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4222
	    Lex->safe_to_cache_query=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4223
		  }
4224
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4225
	  {
4226
	    $$= new Item_master_pos_wait($3, $5, $7);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4227
	    Lex->safe_to_cache_query=0;
4228
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4229 4230
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4231 4232
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
4233 4234
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4235 4236 4237
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
	| NOW_SYM optional_braces
4238
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4239
	| NOW_SYM '(' expr ')'
4240
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4241
	| PASSWORD '(' expr ')'
4242
	  {
4243 4244
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
4245 4246 4247 4248
	      (Item *) new Item_func_password($3);
	  }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new Item_func_old_password($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4249 4250
	| POSITION_SYM '(' no_in_expr IN_SYM expr ')'
	  { $$ = new Item_func_locate($5,$3); }
4251 4252
	| QUARTER_SYM '(' expr ')'
	  { $$ = new Item_func_quarter($3); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4253
	| RAND '(' expr ')'
4254
	  { $$= new Item_func_rand($3); Lex->uncacheable(UNCACHEABLE_RAND);}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4255
	| RAND '(' ')'
4256
	  { $$= new Item_func_rand(); Lex->uncacheable(UNCACHEABLE_RAND);}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4257 4258 4259 4260 4261 4262 4263
	| 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); }
4264 4265 4266 4267 4268
	| ROW_COUNT_SYM '(' ')'
	  {
	    $$= new Item_func_row_count();
	    Lex->safe_to_cache_query= 0;
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4269 4270 4271 4272
	| 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
4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284
	| 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
4285
	| TIME_SYM '(' expr ')'
4286 4287 4288
	  { $$= 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
4289
	| TIMESTAMP '(' expr ',' expr ')'
4290
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
4291 4292 4293 4294
	| TIMESTAMP_ADD '(' interval_time_st ',' expr ',' expr ')'
	  { $$= new Item_date_add_interval($7,$5,$3,0); }
	| TIMESTAMP_DIFF '(' interval_time_st ',' expr ',' expr ')'
	  { $$= new Item_func_timestamp_diff($5,$7,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4295
	| TRIM '(' expr ')'
4296 4297
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4298
	  { $$= new Item_func_ltrim($6,$4); }
4299
	| TRIM '(' TRAILING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4300
	  { $$= new Item_func_rtrim($6,$4); }
4301
	| TRIM '(' BOTH expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4302
	  { $$= new Item_func_trim($6,$4); }
4303 4304 4305 4306 4307 4308
	| 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
4309 4310
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
4311 4312
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
4313 4314
	| TRUE_SYM
	  { $$= new Item_int((char*) "TRUE",1,1); }
4315
	| ident '.' ident '(' udf_expr_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4316
	  {
4317 4318
	    LEX *lex= Lex;
	    sp_name *name= new sp_name($1, $3);
4319

4320 4321 4322 4323
	    name->init_qname(YYTHD);
	    sp_add_fun_to_lex(Lex, name);
	    if ($5)
	      $$= new Item_func_sp(name, *$5);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4324
	    else
4325 4326 4327 4328
	      $$= new Item_func_sp(name);
	  }
	| IDENT_sys '(' udf_expr_list ')'
          {
4329
#ifdef HAVE_DLOPEN
4330
            udf_func *udf;
4331

4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387
            if (using_udf_functions && (udf=find_udf($1.str, $1.length)))
            {
              switch (udf->returns) {
              case STRING_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_str(udf, *$3);
                  else
                    $$ = new Item_func_udf_str(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_str(udf, *$3);
                  else
                    $$ = new Item_sum_udf_str(udf);
                }
                break;
              case REAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_float(udf, *$3);
                  else
                    $$ = new Item_func_udf_float(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_float(udf, *$3);
                  else
                    $$ = new Item_sum_udf_float(udf);
                }
                break;
              case INT_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_int(udf, *$3);
                  else
                    $$ = new Item_func_udf_int(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_int(udf, *$3);
                  else
                    $$ = new Item_sum_udf_int(udf);
                }
                break;
              default:
                YYABORT;
              }
            }
            else
4388
#endif /* HAVE_DLOPEN */
4389 4390 4391 4392 4393 4394 4395 4396
            {
              sp_name *name= sp_name_current_db_new(YYTHD, $1);

              sp_add_fun_to_lex(Lex, name);
              if ($3)
                $$= new Item_func_sp(name, *$3);
              else
                $$= new Item_func_sp(name);
4397
	    }
4398
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4399
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
4400
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4401 4402
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4403
	| UNIX_TIMESTAMP '(' ')'
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4404 4405
	  {
	    $$= new Item_func_unix_timestamp();
4406
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4407
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4408 4409 4410
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
4411
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
4412 4413 4414 4415 4416 4417
	| 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
4418
	| WEEK_SYM '(' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4419
	  {
4420
            $$= new Item_func_week($3,new Item_int((char*) "0",
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4421
				   YYTHD->variables.default_week_format,1));
4422
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4423 4424 4425 4426 4427 4428 4429 4430 4431
	| 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 ')'
4432
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4433
	    $$=new Item_func_benchmark($3,$5);
4434
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4435
	  }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4436
	| EXTRACT_SYM '(' interval FROM expr ')'
4437
	{ $$=new Item_extract( $3, $5); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4438

hf@deer.(none)'s avatar
hf@deer.(none) committed
4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449
geometry_function:
	GEOMFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| GEOMFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| GEOMFROMWKB '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_wkb($3)); }
	| GEOMFROMWKB '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_wkb($3, $5)); }
	| GEOMETRYCOLLECTION '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4450 4451
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4452 4453
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4454
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4455 4456
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
4457
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471
 	| MLINEFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| MLINEFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| MPOINTFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| MPOINTFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| MPOLYFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| MPOLYFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| MULTIPOINT '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4472
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4473 4474
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4475
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487
	| POINT_SYM '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_point($3,$5)); }
 	| POINTFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| POINTFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| POLYFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| POLYFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	| POLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4488
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4489 4490 4491 4492 4493 4494 4495 4496 4497 4498
 	| GEOMCOLLFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| GEOMCOLLFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
 	| LINEFROMTEXT '(' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3)); }
	| LINEFROMTEXT '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_geometry_from_text($3, $5)); }
	;

4499 4500 4501 4502 4503 4504
fulltext_options:
        /* nothing */                   { $$= FT_NL;  }
        | WITH QUERY_SYM EXPANSION_SYM  { $$= FT_NL | FT_EXPAND; }
        | IN_SYM BOOLEAN_SYM MODE_SYM   { $$= FT_BOOL; }
        ;

4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537
udf_expr_list:
	/* empty */	 { $$= NULL; }
	| udf_expr_list2 { $$= $1;}
	;

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

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

udf_expr:
	remember_name expr remember_end select_alias
	{
	  if ($4.str)
	    $2->set_name($4.str,$4.length,system_charset_info);
	  else
	    $2->set_name($1,(uint) ($3 - $1), YYTHD->charset());
	  $$= $2;
	}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4538 4539 4540 4541 4542 4543 4544
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); }
4545 4546
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
4547
	| COUNT_SYM '(' opt_all '*' ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4548 4549 4550
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
4551
	| COUNT_SYM '(' DISTINCT
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4552
	  { Select->in_sum_expr++; }
4553
	   expr_list
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4554
	  { Select->in_sum_expr--; }
4555 4556
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4557 4558 4559 4560
	| 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); }
4561 4562 4563 4564 4565 4566 4567
/*
   According to ANSI SQL, DISTINCT is allowed and has
   no sence inside MIN and MAX grouping functions; so MIN|MAX(DISTINCT ...)
   is processed like an ordinary MIN | MAX()
 */
	| MIN_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_min($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4568 4569
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
4570 4571
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4572 4573
	| STD_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_std($3); }
4574 4575
	| VARIANCE_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_variance($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4576
	| SUM_SYM '(' in_sum_expr ')'
4577
	  { $$=new Item_sum_sum($3); }
4578 4579
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
monty@mysql.com's avatar
monty@mysql.com committed
4580 4581 4582 4583 4584
	| GROUP_CONCAT_SYM '(' opt_distinct
	  { Select->in_sum_expr++; }
	  expr_list opt_gorder_clause
	  opt_gconcat_separator
	 ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4585
	  {
monty@mysql.com's avatar
monty@mysql.com committed
4586 4587 4588
	    Select->in_sum_expr--;
	    $$=new Item_func_group_concat($3,$5,Select->gorder_list,$7);
	    $5->empty();
4589 4590 4591 4592 4593 4594 4595
	  };

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

opt_gconcat_separator:
monty@mysql.com's avatar
monty@mysql.com committed
4596
    /* empty */        { $$ = new (&YYTHD->mem_root) String(",",1,default_charset_info); }
4597
    |SEPARATOR_SYM text_string  { $$ = $2; };
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4598

4599 4600

opt_gorder_clause:
4601 4602
	  /* empty */
	  {
4603
            Select->gorder_list = NULL;
4604 4605 4606
	  }
	| order_clause
          {
4607 4608 4609
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
4610
				     sizeof(st_sql_list));
4611
	    select->order_list.empty();
4612
	  };
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4613

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4614 4615

in_sum_expr:
4616
	opt_all
4617 4618 4619 4620
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
4621
	    yyerror(ER(ER_SYNTAX_ERROR));
4622
	    YYABORT;
4623 4624
	  }
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4625 4626
	expr
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4627
	  Select->in_sum_expr--;
4628
	  $$= $3;
4629
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4630

4631
cast_type:
4632
	BINARY opt_len		{ $$=ITEM_CAST_CHAR; Lex->charset= &my_charset_bin; }
4633 4634
	| CHAR_SYM opt_len opt_binary	{ $$=ITEM_CAST_CHAR; }
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; }
4635 4636 4637 4638 4639 4640 4641
	| SIGNED_SYM		{ $$=ITEM_CAST_SIGNED_INT; Lex->charset= NULL; Lex->length= (char*)0; }
	| SIGNED_SYM INT_SYM	{ $$=ITEM_CAST_SIGNED_INT; Lex->charset= NULL; Lex->length= (char*)0; }
	| UNSIGNED		{ $$=ITEM_CAST_UNSIGNED_INT; Lex->charset= NULL; Lex->length= (char*)0; }
	| UNSIGNED INT_SYM	{ $$=ITEM_CAST_UNSIGNED_INT; Lex->charset= NULL; Lex->length= (char*)0; }
	| DATE_SYM		{ $$=ITEM_CAST_DATE; Lex->charset= NULL; Lex->length= (char*)0; }
	| TIME_SYM		{ $$=ITEM_CAST_TIME; Lex->charset= NULL; Lex->length= (char*)0; }
	| DATETIME		{ $$=ITEM_CAST_DATETIME; Lex->charset= NULL; Lex->length= (char*)0; }
4642
	;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4643

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4644
expr_list:
4645
	{ Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4646
	expr_list2
4647
	{ $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4648 4649

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

4653 4654
ident_list_arg:
          ident_list          { $$= $1; }
4655
        | '(' ident_list ')'  { $$= $2; };
4656

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4657
ident_list:
4658
        { Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4659
        ident_list2
4660
        { $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4661 4662

ident_list2:
4663
        simple_ident { Select->expr_list.head()->push_back($1); }
4664
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4665 4666 4667

opt_expr:
	/* empty */      { $$= NULL; }
4668
	| expr           { $$= $1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4669 4670 4671

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

when_list:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4675
        { Select->when_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4676
	when_list2
4677
	{ $$= Select->when_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4678 4679

when_list2:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4680
	expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4681
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4682
	    SELECT_LEX *sel=Select;
4683 4684
	    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
4685
	}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4686
	| when_list2 WHEN_SYM expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4687
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4688
	    SELECT_LEX *sel=Select;
4689 4690
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
4691
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4692

igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4693 4694 4695 4696 4697 4698 4699 4700 4701 4702
table_ref:
        table_factor            { $$=$1; }
        | join_table            { $$=$1; }
          { 
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
          }     	        
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4703
join_table_list:
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4704 4705 4706 4707 4708 4709 4710 4711 4712
        table_ref { $$=$1; }
        | join_table_list ',' table_ref { $$=$3; }
        ;

join_table:
        table_ref normal_join table_ref { $$=$3; }
	| table_ref STRAIGHT_JOIN table_factor
	  { $3->straight=1; $$=$3 ; }
	| table_ref normal_join table_ref ON expr
4713
	  { add_join_on($3,$5); $$=$3; }
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4714
	| table_ref normal_join table_ref
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4715
	  USING
4716
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4717
	    SELECT_LEX *sel= Select;
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4718
            sel->save_names_for_using_list($1, $3);
4719
	  }
4720 4721 4722
	  '(' using_list ')'
	  { add_join_on($3,$7); $$=$3; }

4723
	| table_ref LEFT opt_outer JOIN_SYM table_ref ON expr
4724
	  { add_join_on($5,$7); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4725
	| table_ref LEFT opt_outer JOIN_SYM table_factor
4726
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4727
	    SELECT_LEX *sel= Select;
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4728
            sel->save_names_for_using_list($1, $5);
4729
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4730
	  USING '(' using_list ')'
4731
	  { add_join_on($5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4732
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
4733
	  {
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4734 4735
	    add_join_natural($1,$6);
	    $6->outer_join|=JOIN_TYPE_LEFT;
4736 4737
	    $$=$6;
	  }
4738
	| table_ref RIGHT opt_outer JOIN_SYM table_ref ON expr
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4739 4740 4741 4742 4743 4744 4745
          { 
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
            add_join_on($$, $7);
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
4746
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4747
	    SELECT_LEX *sel= Select;
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4748
            sel->save_names_for_using_list($1, $5);
4749
	  }
4750
	  USING '(' using_list ')'
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4751 4752 4753 4754 4755 4756 4757
          { 
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
            add_join_on($$, $9);
          }
	| table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor
4758
	  {
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4759 4760 4761 4762
	    add_join_natural($6,$1);
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
4763
	  }
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4764 4765 4766
	| table_ref NATURAL JOIN_SYM table_factor
	  { add_join_natural($1,$4); $$=$4; };
        
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4767 4768

normal_join:
4769 4770 4771 4772
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4773

igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4774
table_factor:
4775
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4776
	  SELECT_LEX *sel= Select;
4777
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4778
	  sel->table_join_options= 0;
4779
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4780
        table_ident opt_table_alias opt_key_definition
4781
	{
4782
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4783
	  SELECT_LEX *sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4784
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4785
					   sel->get_table_join_options(),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4786
					   lex->lock_option,
4787 4788
					   sel->get_use_index(),
					   sel->get_ignore_index())))
4789
	    YYABORT;
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4790
          sel->add_joined_table($$); 
4791
	}
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804
        | '('
          { 
            LEX *lex= Lex;
            if (lex->current_select->init_nested_join(lex->thd))
              YYABORT;
          }            
          join_table_list ')'
          {
            LEX *lex= Lex;
            if (!($$= lex->current_select->end_nested_join(lex->thd)))
              YYABORT;
          }	
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref ON expr '}'
4805
	  { add_join_on($7,$9); $7->outer_join|=JOIN_TYPE_LEFT; $$=$7; }
4806
        | '(' SELECT_SYM select_derived ')' opt_table_alias
4807 4808
	{
	  LEX *lex=Lex;
4809 4810 4811
	  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
4812
                add_table_to_list(lex->thd, new Table_ident(unit), $5, 0,
4813
				  TL_READ,(List<String> *)0,
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4814 4815
	                          (List<String> *)0)))

4816
	    YYABORT;
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4817
          lex->current_select->add_joined_table($$);           
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4818
	};
4819

4820
select_derived:
4821
        {
4822
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
4823
	  lex->derived_tables|= DERIVED_SUBQUERY;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4824 4825 4826
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
4827 4828
	  {
	    yyerror(ER(ER_SYNTAX_ERROR));
4829 4830
	    YYABORT;
	  }
4831
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
4832
              mysql_new_select(lex, 1))
4833 4834
	    YYABORT;
	  mysql_init_select(lex);
4835
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
4836 4837 4838 4839 4840
	  lex->current_select->parsing_place= SELECT_LEX_NODE::SELECT_LIST;
	}
        select_options select_item_list
	{
	  Select->parsing_place= SELECT_LEX_NODE::NO_MATTER;
4841
	}
4842
	opt_select_from union_opt
4843
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4844 4845 4846

opt_outer:
	/* empty */	{}
4847
	| OUTER		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4848 4849 4850 4851

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
4852
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4853
	    SELECT_LEX *sel= Select;
4854 4855 4856
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4857 4858
	| FORCE_SYM key_usage_list
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4859
	    SELECT_LEX *sel= Select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4860 4861 4862 4863
	    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
4864
	| IGNORE_SYM key_usage_list
4865
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4866
	    SELECT_LEX *sel= Select;
4867 4868
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
4869
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4870 4871

key_usage_list:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4872
	key_or_index { Select->interval_list.empty(); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4873
        '(' key_list_or_empty ')'
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4874
        { $$= &Select->interval_list; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4875 4876 4877 4878 4879 4880
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4881 4882 4883

key_usage_list2:
	key_usage_list2 ',' ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4884
        { Select->
monty@mysql.com's avatar
monty@mysql.com committed
4885
	    interval_list.push_back(new (&YYTHD->mem_root) String((const char*) $3.str, $3.length,
4886
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4887
	| ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4888
        { Select->
monty@mysql.com's avatar
monty@mysql.com committed
4889
	    interval_list.push_back(new (&YYTHD->mem_root) String((const char*) $1.str, $1.length,
4890
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4891
	| PRIMARY_SYM
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4892
        { Select->
monty@mysql.com's avatar
monty@mysql.com committed
4893
	    interval_list.push_back(new (&YYTHD->mem_root) String("PRIMARY", 7,
4894
				    system_charset_info)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4895 4896 4897

using_list:
	ident
4898
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4899
	    SELECT_LEX *sel= Select;
4900 4901 4902 4903
	    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
4904 4905 4906 4907
	      YYABORT;
	  }
	| using_list ',' ident
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4908
	    SELECT_LEX *sel= Select;
4909
	    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
4910
	      YYABORT;
4911
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4912 4913

interval:
4914 4915
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4916
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4917 4918
	| DAY_MINUTE_SYM	{ $$=INTERVAL_DAY_MINUTE; }
	| DAY_SECOND_SYM	{ $$=INTERVAL_DAY_SECOND; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4919
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4920 4921
	| HOUR_MINUTE_SYM	{ $$=INTERVAL_HOUR_MINUTE; }
	| HOUR_SECOND_SYM	{ $$=INTERVAL_HOUR_SECOND; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4922 4923
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4924
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
4925 4926 4927 4928 4929 4930 4931 4932
	| SECOND_MICROSECOND_SYM	{ $$=INTERVAL_SECOND_MICROSECOND; }
	| YEAR_MONTH_SYM	{ $$=INTERVAL_YEAR_MONTH; };

interval_time_st:
	DAY_SYM			{ $$=INTERVAL_DAY; }
	| WEEK_SYM		{ $$=INTERVAL_WEEK; }
	| HOUR_SYM		{ $$=INTERVAL_HOUR; }
	| FRAC_SECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4933 4934
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
4935
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4936
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
4937
	| YEAR_SYM		{ $$=INTERVAL_YEAR; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4938

4939
date_time_type:
4940 4941 4942
	DATE_SYM		{$$=MYSQL_TIMESTAMP_DATE;}
	| TIME_SYM		{$$=MYSQL_TIMESTAMP_TIME;}
	| DATETIME		{$$=MYSQL_TIMESTAMP_DATETIME;};
4943

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4944 4945 4946
table_alias:
	/* empty */
	| AS
4947
	| EQ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4948 4949 4950 4951

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

4954 4955 4956 4957
opt_all:
	/* empty */
	| ALL
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4958 4959

where_clause:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4960
	/* empty */  { Select->where= 0; }
4961 4962
	| WHERE expr
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4963
	    Select->where= $2;
4964 4965 4966
	    if ($2)
	      $2->top_level_item();
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4967
 	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4968 4969 4970

having_clause:
	/* empty */
4971 4972
	| HAVING
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4973
	    Select->parsing_place= SELECT_LEX_NODE::IN_HAVING;
4974 4975
          }
	  expr
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4976
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4977
	    SELECT_LEX *sel= Select;
4978 4979
	    sel->having= $3;
	    sel->parsing_place= SELECT_LEX_NODE::NO_MATTER;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4980 4981 4982
	    if ($3)
	      $3->top_level_item();
	  }
4983
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4984 4985

opt_escape:
4986 4987
	ESCAPE_SYM simple_expr { $$= $2; }
	| /* empty */
monty@mysql.com's avatar
Merge  
monty@mysql.com committed
4988 4989 4990 4991 4992
          {

            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
4993 4994
          }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4995 4996 4997


/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4998
   group by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4999 5000 5001 5002
*/

group_clause:
	/* empty */
5003
	| GROUP BY group_list olap_opt;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5004 5005

group_list:
5006
	group_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5007
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
5008
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5009
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5010

5011 5012
olap_opt:
	/* empty */ {}
5013
	| WITH CUBE_SYM
5014
          {
5015
	    LEX *lex=Lex;
5016 5017 5018 5019 5020 5021
	    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
5022
	    lex->current_select->olap= CUBE_TYPE;
5023
	    net_printf(lex->thd, ER_NOT_SUPPORTED_YET, "CUBE");
5024
	    YYABORT;	/* To be deleted in 5.1 */
5025
	  }
5026
	| WITH ROLLUP_SYM
5027
          {
5028 5029 5030 5031 5032 5033 5034
	    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
5035
	    lex->current_select->olap= ROLLUP_TYPE;
5036
	  }
5037
	;
5038

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5039
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5040
   Order by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5041 5042
*/

5043
opt_order_clause:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5044
	/* empty */
5045
	| order_clause;
5046 5047

order_clause:
5048 5049
	ORDER_SYM BY
        {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5050
	  LEX *lex=Lex;
5051
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5052
	      lex->current_select->olap !=
5053
	      UNSPECIFIED_OLAP_TYPE)
5054
	  {
5055
	    net_printf(lex->thd, ER_WRONG_USAGE,
5056 5057 5058 5059
		       "CUBE/ROLLUP",
		       "ORDER BY");
	    YYABORT;
	  }
5060
	} order_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5061 5062 5063

order_list:
	order_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5064
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5065
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5066
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5067 5068 5069

order_dir:
	/* empty */ { $$ =  1; }
5070
	| ASC  { $$ =1; }
5071
	| DESC { $$ =0; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5072 5073


5074 5075 5076
opt_limit_clause_init:
	/* empty */
	{
5077 5078
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
5079
          sel->offset_limit= 0L;
5080
          sel->select_limit= HA_POS_ERROR;
5081
	}
5082 5083 5084
	| limit_clause {}
	;

5085 5086 5087 5088 5089
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

5090
limit_clause:
5091
	LIMIT limit_options {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5092
	;
5093 5094 5095

limit_options:
	ULONG_NUM
5096
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5097
            SELECT_LEX *sel= Select;
5098 5099
            sel->select_limit= $1;
            sel->offset_limit= 0L;
5100
	    sel->explicit_limit= 1;
5101 5102 5103
	  }
	| ULONG_NUM ',' ULONG_NUM
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5104
	    SELECT_LEX *sel= Select;
5105 5106
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
5107
	    sel->explicit_limit= 1;
5108
	  }
5109
	| ULONG_NUM OFFSET_SYM ULONG_NUM
5110
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5111
	    SELECT_LEX *sel= Select;
5112 5113
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
5114
	    sel->explicit_limit= 1;
5115 5116
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5117 5118 5119 5120 5121


delete_limit_clause:
	/* empty */
	{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5122
	  LEX *lex=Lex;
5123
	  lex->current_select->select_limit= HA_POS_ERROR;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5124
	}
5125
	| LIMIT ulonglong_num
5126 5127 5128 5129 5130
	{
	  SELECT_LEX *sel= Select;
	  sel->select_limit= (ha_rows) $2;
	  sel->explicit_limit= 1;
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5131 5132

ULONG_NUM:
monty@mysql.com's avatar
monty@mysql.com committed
5133 5134 5135 5136 5137 5138
	NUM	        { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| LONG_NUM      { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| ULONGLONG_NUM { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| REAL_NUM 	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5139

5140
ulonglong_num:
monty@mysql.com's avatar
monty@mysql.com committed
5141 5142 5143 5144 5145 5146
	NUM	    { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| ULONGLONG_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| LONG_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| REAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5147 5148 5149 5150 5151 5152

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
5153 5154 5155 5156 5157 5158 5159
	    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
5160 5161 5162
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
5163
	    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
5164
	      YYABORT;
5165
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5166
	  }
5167
	  '(' procedure_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5168 5169 5170 5171


procedure_list:
	/* empty */ {}
5172
	| procedure_list2 {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5173 5174 5175

procedure_list2:
	procedure_list2 ',' procedure_item
5176
	| procedure_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5177 5178 5179 5180

procedure_item:
	  remember_name expr
	  {
5181 5182
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5183 5184
	      YYABORT;
	    if (!$2->name)
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5185
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1), YYTHD->charset());
5186 5187 5188 5189 5190 5191
	  }
          ;


select_var_list_init:
	   {
5192 5193
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
5194 5195
	        YYABORT;
	   }
5196
	   select_var_list
5197
	   {}
5198
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5199

Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5200
select_var_list:
5201 5202 5203 5204
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

5205 5206
select_var_ident:  
	   '@' ident_or_text
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5207
           {
5208
             LEX *lex=Lex;
5209
	     if (lex->result) 
5210
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($2,0,0,(enum_field_types)0));
5211
	     else
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5212 5213
	       YYABORT;
	   }
5214
           | ident_or_text
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5215
           {
5216
             LEX *lex=Lex;
5217
	     sp_pvar_t *t;
5218 5219

	     if (!lex->spcont || !(t=lex->spcont->find_pvar(&$1)))
5220
	     {
5221
	       net_printf(YYTHD, ER_SP_UNDECLARED_VAR, $1.str);
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5222
	       YYABORT;
5223
	     }
5224
	     if (! lex->result)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5225
	       YYABORT;
5226 5227
	     else
	     {
5228
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($1,1,t->offset,t->type));
5229 5230
	       t->isset= TRUE;
	     }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5231
	   }
5232
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5233

5234
into:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5235
        INTO OUTFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5236
	{
5237
	  LEX *lex=Lex;
5238 5239
	  if (!lex->describe)
	  {
5240
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5241 5242 5243 5244 5245
	    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
5246 5247
	}
	opt_field_term opt_line_term
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5248
	| INTO DUMPFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5249
	{
5250
	  LEX *lex=Lex;
5251 5252
	  if (!lex->describe)
	  {
5253
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5254 5255 5256 5257 5258
	    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
5259
	}
5260
        | INTO select_var_list_init
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5261
	{
5262
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5263 5264
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5265

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5266 5267 5268
/*
  DO statement
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5269

5270
do:	DO_SYM
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5271 5272 5273 5274 5275 5276
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
	  if (!(lex->insert_list = new List_item))
	    YYABORT;
	}
5277 5278 5279 5280
	values
	{}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5281
/*
5282
  Drop : delete tables or index or user
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5283 5284 5285
*/

drop:
serg@serg.mylan's avatar
serg@serg.mylan committed
5286
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5287
	{
5288 5289
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
5290 5291
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5292
	}
5293
	| DROP INDEX_SYM ident ON table_ident {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5294
	  {
5295 5296
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
5297 5298 5299
	     lex->alter_info.drop_list.empty();
	     lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
                                                                $3.str));
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5300 5301
	     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
5302 5303 5304 5305
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
5306 5307 5308 5309
	    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
5310
	 }
5311
	| DROP FUNCTION_SYM if_exists sp_name opt_restrict
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5312
	  {
5313
	    LEX *lex=Lex;
5314 5315 5316 5317 5318
	    if (lex->sphead)
	    {
	      net_printf(YYTHD, ER_SP_NO_DROP_SP, "FUNCTION");
	      YYABORT;
	    }
5319
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
5320
	    lex->drop_if_exists= $3;
5321
	    lex->spname= $4;
5322
	  }
5323
	| DROP PROCEDURE if_exists sp_name opt_restrict
5324 5325
	  {
	    LEX *lex=Lex;
5326 5327 5328 5329 5330
	    if (lex->sphead)
	    {
	      net_printf(YYTHD, ER_SP_NO_DROP_SP, "PROCEDURE");
	      YYABORT;
	    }
5331
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
5332
	    lex->drop_if_exists= $3;
5333
	    lex->spname= $4;
5334 5335 5336 5337 5338 5339
	  }
	| 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
5340
	  }
5341 5342
	  user_list
	  {}
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
5343 5344 5345 5346 5347 5348 5349
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
5350
	  ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5351 5352

table_list:
5353
	table_name
5354
	| table_list ',' table_name;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5355

5356
table_name:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5357
	table_ident
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5358 5359 5360 5361 5362
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5363 5364

if_exists:
5365
	/* empty */ { $$= 0; }
5366 5367
	| IF EXISTS { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5368

5369 5370 5371 5372
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5373 5374 5375 5376 5377
/*
** Insert : add new data to table
*/

insert:
5378 5379 5380 5381 5382 5383
	INSERT
	{
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_INSERT;
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
5384
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
5385
	} insert_lock_option
5386
	opt_ignore insert2
5387
	{
5388
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5389
	  Lex->current_select= &Lex->select_lex;
5390
	}
5391
	insert_field_spec opt_insert_update
5392
	{}
5393
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5394 5395

replace:
5396 5397
	REPLACE
	{
5398
	  LEX *lex=Lex;
5399 5400
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
5401
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
5402
	}
5403 5404
	replace_lock_option insert2
	{
5405
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5406
	  Lex->current_select= &Lex->select_lex;
5407 5408
	}
	insert_field_spec
5409
	{}
5410
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5411 5412

insert_lock_option:
5413 5414 5415 5416
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
5417
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5418 5419

replace_lock_option:
5420 5421
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5422 5423 5424

insert2:
	INTO insert_table {}
5425
	| insert_table {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5426 5427

insert_table:
5428
	table_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5429
	{
5430 5431 5432 5433
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
5434
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5435 5436

insert_field_spec:
serg@serg.mylan's avatar
serg@serg.mylan committed
5437 5438 5439
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5440 5441
	| SET
	  {
5442 5443 5444
	    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
5445 5446
	      YYABORT;
	   }
5447
	   ident_eq_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5448 5449 5450 5451

opt_field_spec:
	/* empty */	  { }
	| '(' fields ')'  { }
5452
	| '(' ')'	  { };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5453 5454 5455

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
5456
	| insert_ident		{ Lex->field_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5457 5458 5459

insert_values:
	VALUES	values_list  {}
5460
	| VALUE_SYM values_list  {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5461 5462
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
5463
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5464 5465 5466

values_list:
	values_list ','  no_braces
5467
	| no_braces;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5468 5469 5470 5471

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
5472
	ident_eq_value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5473 5474

ident_eq_value:
5475
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5476
	 {
5477 5478 5479
	  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
5480
	    YYABORT;
5481
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5482 5483

equal:	EQ		{}
5484 5485 5486 5487 5488 5489 5490
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5491 5492 5493 5494 5495 5496 5497 5498 5499

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
5500 5501
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5502
	    YYABORT;
5503
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5504 5505 5506

opt_values:
	/* empty */ {}
5507
	| values;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5508 5509

values:
5510
	values ','  expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5511 5512 5513 5514
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
5515 5516 5517 5518 5519 5520 5521 5522
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
5523
	expr	  { $$= $1;}
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
5524
	| DEFAULT {$$= new Item_default_value(); }
5525
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5526

5527 5528
opt_insert_update:
        /* empty */
5529
        | ON DUPLICATE_SYM
5530 5531 5532 5533 5534
          { /* for simplisity, let's forget about
               INSERT ... SELECT ... UPDATE
               for a moment */
	    if (Lex->sql_command != SQLCOM_INSERT)
            {
5535
	      yyerror(ER(ER_SYNTAX_ERROR));
5536 5537 5538
              YYABORT;
            }
          }
5539
          KEY_SYM UPDATE_SYM update_list
5540 5541
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5542 5543 5544
/* Update rows in a table */

update:
5545 5546
	UPDATE_SYM
	{
5547
	  LEX *lex= Lex;
5548
	  mysql_init_select(lex);
5549
          lex->sql_command= SQLCOM_UPDATE;
5550
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
5551
        }
5552 5553 5554
        opt_low_priority opt_ignore join_table_list
	SET update_list where_clause opt_order_clause delete_limit_clause
	{
5555
	  LEX *lex= Lex;
5556
	  Select->set_lock_for_tables($3);
5557
          if (lex->select_lex.table_list.elements > 1)
5558
            lex->sql_command= SQLCOM_UPDATE_MULTI;
5559 5560 5561 5562 5563 5564 5565
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
	    net_printf(lex->thd, ER_NON_UPDATABLE_TABLE,
		       lex->select_lex.get_table_list()->alias, "UPDATE");
	    YYABORT;
	  }
5566 5567
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5568 5569

update_list:
5570
	update_list ',' simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5571
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5572
	  if (add_item_to_list(YYTHD, $3) || add_value_to_list(YYTHD, $5))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5573 5574
	    YYABORT;
	}
5575
	| simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5576
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5577
	    if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5578
	      YYABORT;
5579
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5580 5581

opt_low_priority:
5582
	/* empty */	{ $$= YYTHD->update_lock_default; }
5583
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5584 5585 5586 5587

/* Delete rows from a table */

delete:
5588
	DELETE_SYM
5589
	{
5590 5591
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
5592
	  lex->lock_option= lex->thd->update_lock_default;
5593
	  lex->select_lex.init_order();
5594
	}
5595 5596
	opt_delete_options single_multi {}
	;
5597 5598

single_multi:
5599 5600
 	FROM table_ident
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5601 5602
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
5603 5604 5605
	    YYABORT;
	}
	where_clause opt_order_clause
5606
	delete_limit_clause {}
5607
	| table_wild_list
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5608
	  { mysql_init_multi_delete(Lex); }
5609
          FROM join_table_list where_clause
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5610 5611
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
5612 5613 5614
	  USING join_table_list where_clause
	  {}
	;
5615 5616 5617

table_wild_list:
	  table_wild_one {}
5618
	  | table_wild_list ',' table_wild_one {};
5619 5620

table_wild_one:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5621
	ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5622
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5623 5624
	  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
5625 5626
	    YYABORT;
        }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5627
	| ident '.' ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5628
	  {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5629 5630
	    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
5631 5632
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
5633
	      YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5634
	  }
5635
	;
5636 5637

opt_wild:
5638
	/* empty */	{}
5639
	| '.' '*'	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5640 5641


5642
opt_delete_options:
5643
	/* empty */	{}
5644
	| opt_delete_option opt_delete_options {};
5645 5646

opt_delete_option:
5647
	QUICK		{ Select->options|= OPTION_QUICK; }
5648 5649
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
	| IGNORE_SYM	{ Lex->duplicates= DUP_IGNORE; };
5650

5651
truncate:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5652
	TRUNCATE_SYM opt_table_sym table_name
5653
	{
5654
	  LEX* lex= Lex;
5655
	  lex->sql_command= SQLCOM_TRUNCATE;
5656
	  lex->select_lex.options= 0;
5657
	  lex->select_lex.init_order();
5658 5659
	}
	;
5660

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5661 5662
opt_table_sym:
	/* empty */
5663
	| TABLE_SYM;
5664

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5665 5666
/* Show things */

5667 5668
show:	SHOW
	{
5669 5670 5671 5672
	  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
5673
	show_param
5674 5675
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5676 5677 5678 5679 5680

show_param:
	DATABASES wild
	  { Lex->sql_command= SQLCOM_SHOW_DATABASES; }
	| TABLES opt_db wild
5681
	  {
5682
	    LEX *lex= Lex;
5683
	    lex->sql_command= SQLCOM_SHOW_TABLES;
5684
	    lex->select_lex.db= $2;
5685
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5686
	| TABLE_SYM STATUS_SYM opt_db wild
5687
	  {
5688
	    LEX *lex= Lex;
5689
	    lex->sql_command= SQLCOM_SHOW_TABLES;
5690
	    lex->describe= DESCRIBE_EXTENDED;
5691
	    lex->select_lex.db= $3;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5692
	  }
5693
	| OPEN_SYM TABLES opt_db wild
5694
	  {
5695
	    LEX *lex= Lex;
5696
	    lex->sql_command= SQLCOM_SHOW_OPEN_TABLES;
5697
	    lex->select_lex.db= $3;
5698
	  }
5699
	| ENGINE_SYM storage_engines 
5700 5701
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
5702
	| opt_full COLUMNS from_or_in table_ident opt_db wild
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5703 5704
	  {
	    Lex->sql_command= SQLCOM_SHOW_FIELDS;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5705 5706
	    if ($5)
	      $4->change_db($5);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5707
	    if (!Select->add_table_to_list(YYTHD, $4, NULL, 0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5708 5709
	      YYABORT;
	  }
5710
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
5711 5712
	  TEXT_STRING_sys AND_SYM MASTER_LOG_POS_SYM EQ ulonglong_num
	  AND_SYM MASTER_SERVER_ID_SYM EQ
5713 5714 5715 5716 5717
	ULONG_NUM
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
5718
	    Lex->mi.server_id = $16;
5719
          }
5720
        | master_or_binary LOGS_SYM
5721 5722
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
5723 5724 5725 5726 5727
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
5728
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
5729
          {
5730 5731
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
5732
          } opt_limit_clause_init
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5733 5734 5735 5736 5737
	| 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
5738
	    if (!Select->add_table_to_list(YYTHD, $3, NULL, 0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5739 5740
	      YYABORT;
	  }
5741 5742 5743 5744 5745 5746 5747 5748
	| COLUMN_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_COLUMN_TYPES;
	  }
	| TABLE_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
5749 5750 5751 5752 5753 5754 5755
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
	    WARN_DEPRECATED("SHOW TABLE TYPES", "SHOW [STORAGE] ENGINES");
	  }
	| opt_storage ENGINES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
5756 5757 5758 5759 5760 5761
	  }
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
5762
        | COUNT_SYM '(' '*' ')' WARNINGS
5763
          { (void) create_select_for_variable("warning_count"); }
5764
        | COUNT_SYM '(' '*' ')' ERRORS
5765
	  { (void) create_select_for_variable("error_count"); }
5766
        | WARNINGS opt_limit_clause_init
5767
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
5768
        | ERRORS opt_limit_clause_init
5769
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5770 5771
	| STATUS_SYM wild
	  { Lex->sql_command= SQLCOM_SHOW_STATUS; }
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
5772
        | INNOBASE_SYM STATUS_SYM
5773
          { Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS; WARN_DEPRECATED("SHOW INNODB STATUS", "SHOW ENGINE INNODB STATUS"); }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5774 5775
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
5776
	| opt_var_type VARIABLES wild
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5777
	  {
5778
	    THD *thd= YYTHD;
5779 5780
	    thd->lex->sql_command= SQLCOM_SHOW_VARIABLES;
	    thd->lex->option_type= (enum_var_type) $1;
5781
	  }
5782
	| charset wild
5783
	  { Lex->sql_command= SQLCOM_SHOW_CHARSETS; }
5784 5785
	| COLLATION_SYM wild
	  { Lex->sql_command= SQLCOM_SHOW_COLLATIONS; }
5786
	| BERKELEY_DB_SYM LOGS_SYM
5787
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW BDB LOGS", "SHOW ENGINE BDB LOGS"); }
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
5788
	| LOGS_SYM
5789
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW LOGS", "SHOW ENGINE BDB LOGS"); }
5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812
	| GRANTS
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    THD *thd= lex->thd;
	    LEX_USER *curr_user;
            if (!(curr_user= (LEX_USER*) thd->alloc(sizeof(st_lex_user))))
              YYABORT;
            curr_user->user.str= thd->priv_user;
            curr_user->user.length= strlen(thd->priv_user);
            if (*thd->priv_host != 0)
            {
              curr_user->host.str= thd->priv_host;
              curr_user->host.length= strlen(thd->priv_host);
            }
            else
            {
              curr_user->host.str= (char *) "%";
              curr_user->host.length= 1;
            }
            curr_user->password.str=NullS;
	    lex->grant_user= curr_user;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5813
	| GRANTS FOR_SYM user
5814 5815 5816 5817 5818 5819
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
	    lex->grant_user->password.str=NullS;
	  }
5820
	| CREATE DATABASE opt_if_not_exists ident
5821 5822
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
5823 5824
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
5825
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5826 5827
        | CREATE TABLE_SYM table_ident
          {
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838
            LEX *lex= Lex;
	    lex->sql_command = SQLCOM_SHOW_CREATE;
	    if (!lex->select_lex.add_table_to_list(YYTHD, $3, NULL,0))
	      YYABORT;
            lex->only_view= 0;
	  }
        | CREATE VIEW_SYM table_ident
          {
            LEX *lex= Lex;
	    lex->sql_command = SQLCOM_SHOW_CREATE;
	    if (!lex->select_lex.add_table_to_list(YYTHD, $3, NULL, 0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5839
	      YYABORT;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
5840
            lex->only_view= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5841 5842 5843 5844
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5845
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5846 5847 5848
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
5849
          }
5850
	| CREATE PROCEDURE sp_name
5851
	  {
5852 5853 5854 5855
	    LEX *lex= Lex;

	    lex->sql_command = SQLCOM_SHOW_CREATE_PROC;
	    lex->spname= $3;
5856
	  }
5857
	| CREATE FUNCTION_SYM sp_name
5858
	  {
5859 5860 5861 5862
	    LEX *lex= Lex;

	    lex->sql_command = SQLCOM_SHOW_CREATE_FUNC;
	    lex->spname= $3;
5863 5864 5865 5866 5867 5868 5869 5870 5871 5872
	  }
	| PROCEDURE STATUS_SYM wild
	  {
	    Lex->sql_command = SQLCOM_SHOW_STATUS_PROC;
	  }
	| FUNCTION_SYM STATUS_SYM wild
	  {
	    Lex->sql_command = SQLCOM_SHOW_STATUS_FUNC;
	  };

5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895
show_engine_param:
	STATUS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_INNODB:
	      Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;
	      break;
	    default:
	      net_printf(YYTHD, ER_NOT_SUPPORTED_YET, "STATUS");
	      YYABORT;
	    }
	  }
	| LOGS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_BERKELEY_DB:
	      Lex->sql_command = SQLCOM_SHOW_LOGS;
	      break;
	    default:
	      net_printf(YYTHD, ER_NOT_SUPPORTED_YET, "LOGS");
	      YYABORT;
	    }
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5896

5897 5898 5899 5900
master_or_binary:
	MASTER_SYM
	| BINARY;

5901 5902 5903 5904
opt_storage:
	/* empty */
	| STORAGE_SYM;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5905 5906
opt_db:
	/* empty */  { $$= 0; }
5907
	| from_or_in ident { $$= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5908 5909 5910

wild:
	/* empty */
5911 5912 5913
	| LIKE TEXT_STRING_sys
	  { Lex->wild=  new (&YYTHD->mem_root) String($2.str, $2.length,
                                                      system_charset_info); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5914

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5915 5916
opt_full:
	/* empty */ { Lex->verbose=0; }
5917
	| FULL	    { Lex->verbose=1; };
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5918

5919 5920
from_or_in:
	FROM
5921
	| IN_SYM;
5922

5923 5924
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5925
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
5926 5927 5928

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


bk@work.mysql.com's avatar
bk@work.mysql.com committed
5932 5933 5934 5935
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
5936 5937 5938 5939
	  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
5940
	  if (!Select->add_table_to_list(lex->thd, $2, NULL,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5941 5942
	    YYABORT;
	}
5943
	opt_describe_column {}
5944 5945 5946
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
5947
          {
5948
	    LEX *lex=Lex;
5949
	    lex->select_lex.options|= SELECT_DESCRIBE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5950 5951
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5952 5953 5954

describe_command:
	DESC
5955
	| DESCRIBE;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5956

5957 5958 5959 5960 5961
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5962 5963 5964
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
5965
	| ident
monty@mysql.com's avatar
monty@mysql.com committed
5966
	  { Lex->wild= new (&YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5967 5968 5969 5970 5971


/* flush things */

flush:
5972
	FLUSH_SYM opt_no_write_to_binlog
5973 5974 5975
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5976
          lex->no_write_to_binlog= $2;
5977
	}
5978 5979 5980
	flush_options
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5981 5982 5983

flush_options:
	flush_options ',' flush_option
5984
	| flush_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5985 5986

flush_option:
5987
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5988
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5989
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5990 5991 5992 5993
	| 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
5994 5995
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
5996 5997
	| 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
5998

5999
opt_table_list:
6000 6001
	/* empty */  {;}
	| table_list {;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6002

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6003
reset:
6004 6005 6006 6007
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
6008 6009 6010 6011
	} reset_options
	{}
	;

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6012 6013
reset_options:
	reset_options ',' reset_option
6014
	| reset_option;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6015 6016

reset_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6017 6018
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
6019
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6020

6021
purge:
6022 6023 6024 6025
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
6026 6027 6028 6029 6030
	} purge_options
	{}
	;

purge_options:
6031
	master_or_binary LOGS_SYM purge_option
6032
	;
6033 6034

purge_option:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6035
        TO_SYM TEXT_STRING_sys
6036 6037 6038 6039 6040 6041 6042 6043 6044
        {
	   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
6045
	    YYABORT;
6046 6047
	  }
	  Item *tmp= new Item_func_unix_timestamp($2);
6048
	  /*
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6049
	    it is OK only emulate fix_fieds, because we need only
6050 6051
            value of constant
	  */
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6052
	  tmp->quick_fix_field();
6053
	  Lex->sql_command = SQLCOM_PURGE_BEFORE;
6054
	  Lex->purge_time= (ulong) tmp->val_int();
6055
	}
6056
	;
6057

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6058 6059 6060
/* kill threads */

kill:
hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
6061
	KILL_SYM kill_option expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6062
	{
6063
	  LEX *lex=Lex;
hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
6064
	  if ($3->fix_fields(lex->thd, 0, &$3) || $3->check_cols(1))
6065
	  {
6066
	    send_error(lex->thd, ER_SET_CONSTANTS_ONLY);
6067 6068 6069
	    YYABORT;
	  }
          lex->sql_command=SQLCOM_KILL;
hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
6070
	  lex->thread_id= (ulong) $3->val_int();
6071
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6072

hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
6073 6074 6075 6076 6077
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
6078 6079 6080
/* change database */

use:	USE_SYM ident
6081 6082
	{
	  LEX *lex=Lex;
6083 6084
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
6085
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6086 6087 6088

/* import, export of files */

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6089
load:	LOAD DATA_SYM load_data_lock opt_local INFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6090
	{
6091 6092
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
6093 6094
	  lex->lock_option= $3;
	  lex->local_file=  $4;
6095
	  if (!(lex->exchange= new sql_exchange($6.str,0)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6096
	    YYABORT;
6097
	  lex->field_list.empty();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6098 6099 6100 6101
	}
	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
6102
	  if (!Select->add_table_to_list(YYTHD, $11, NULL, TL_OPTION_UPDATING))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6103 6104 6105
	    YYABORT;
	}
        |
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6106
	LOAD TABLE_SYM table_ident FROM MASTER_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6107 6108
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6109
	  if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6110
	    YYABORT;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6111 6112

        }
6113 6114 6115 6116
        |
	LOAD DATA_SYM FROM MASTER_SYM
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
6117
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6118 6119 6120

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

6123
load_data_lock:
6124
	/* empty */	{ $$= YYTHD->update_lock_default; }
6125 6126
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
6127 6128


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6129 6130 6131
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
6132
	| IGNORE_SYM	{ Lex->duplicates=DUP_IGNORE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6133 6134 6135

opt_field_term:
	/* empty */
6136
	| COLUMNS field_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6137 6138 6139

field_term_list:
	field_term_list field_term
6140
	| field_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6141 6142 6143 6144

field_term:
	TERMINATED BY text_string { Lex->exchange->field_term= $3;}
	| OPTIONALLY ENCLOSED BY text_string
6145 6146 6147 6148 6149
	  {
	    LEX *lex=Lex;
	    lex->exchange->enclosed= $4;
	    lex->exchange->opt_enclosed=1;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6150
	| ENCLOSED BY text_string { Lex->exchange->enclosed= $3;}
6151
	| ESCAPED BY text_string  { Lex->exchange->escaped= $3;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6152 6153 6154

opt_line_term:
	/* empty */
6155
	| LINES line_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6156 6157 6158

line_term_list:
	line_term_list line_term
6159
	| line_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6160 6161 6162

line_term:
	TERMINATED BY text_string { Lex->exchange->line_term= $3;}
6163
	| STARTING BY text_string { Lex->exchange->line_start= $3;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6164 6165 6166 6167

opt_ignore_lines:
	/* empty */
	| IGNORE_SYM NUM LINES
6168
	  { Lex->exchange->skip_lines=atol($2.str); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6169 6170 6171 6172

/* Common definitions */

text_literal:
6173
	TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6174 6175
	{
	  THD *thd= YYTHD;
6176
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6177
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6178
	| NCHAR_STRING
6179
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
6180
	| UNDERSCORE_CHARSET TEXT_STRING
6181
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
6182
	| text_literal TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6183 6184
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6185 6186

text_string:
6187
	TEXT_STRING_literal
monty@mysql.com's avatar
monty@mysql.com committed
6188
	{ $$=  new (&YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6189 6190
	| HEX_NUM
	  {
6191
	    Item *tmp = new Item_varbinary($1.str,$1.length);
6192
	    /*
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6193
	      it is OK only emulate fix_fieds, because we need only
6194 6195 6196
              value of constant
	    */
	    $$= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6197
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6198
	      (String*) 0;
6199 6200 6201
	  }
	;

6202
param_marker:
6203
        '?'
6204
        {
6205 6206 6207
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
          if (thd->command == COM_PREPARE)
6208
          {
6209 6210 6211 6212 6213 6214 6215
            Item_param *item= new Item_param((uint) (lex->tok_start -
                                                     (uchar *) thd->query));
            if (!($$= item) || lex->param_list.push_back(item))
            {
	      send_error(thd, ER_OUT_OF_RESOURCES);
	      YYABORT;
            }
6216
          }
6217
          else
6218
          {
6219
            yyerror(ER(ER_SYNTAX_ERROR));
6220 6221
            YYABORT;
          }
6222 6223 6224
        }
	;

6225 6226 6227
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
6228 6229
	| '-' NUM_literal
	  {
6230
	    $2->max_length++;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6231
	    $$= $2->neg();
6232
	  }
6233 6234 6235
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6236 6237
literal:
	text_literal	{ $$ =	$1; }
6238
	| NUM_literal	{ $$ = $1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6239
	| NULL_SYM	{ $$ =	new Item_null();
6240
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
6241
	| HEX_NUM	{ $$ =	new Item_varbinary($1.str,$1.length);}
6242
	| UNDERSCORE_CHARSET HEX_NUM
6243
	  {
6244
	    Item *tmp= new Item_varbinary($2.str,$2.length);
6245
	    /*
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6246
	      it is OK only emulate fix_fieds, because we need only
6247 6248 6249
              value of constant
	    */
	    String *str= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6250
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6251
	      (String*) 0;
6252 6253 6254
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
6255
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6256 6257
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
6258
	| TIMESTAMP text_literal { $$ = $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6259

6260
NUM_literal:
6261 6262
	NUM		{ int error; $$ = new Item_int($1.str, (longlong) my_strtoll10($1.str, NULL, &error), $1.length); }
	| LONG_NUM	{ int error; $$ = new Item_int($1.str, (longlong) my_strtoll10($1.str, NULL, &error), $1.length); }
6263 6264 6265 6266 6267
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
	| REAL_NUM	{ $$ =	new Item_real($1.str, $1.length); }
	| FLOAT_NUM	{ $$ =	new Item_float($1.str, $1.length); }
	;
	
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6268 6269 6270 6271 6272
/**********************************************************************
** Createing different items.
**********************************************************************/

insert_ident:
6273
	simple_ident_nospvar { $$=$1; }
6274
	| table_wild	 { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6275 6276

table_wild:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6277
	ident '.' '*'
6278 6279
	{
	  $$ = new Item_field(NullS,$1.str,"*");
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6280
	  Lex->current_select->with_wild++;
6281
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6282
	| ident '.' ident '.' '*'
6283 6284 6285 6286
	{
	  $$ = 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
6287
	  Lex->current_select->with_wild++;
6288 6289
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6290 6291

order_ident:
6292
	expr { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6293 6294 6295

simple_ident:
	ident
6296
	{
6297 6298 6299 6300 6301 6302 6303 6304
	  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)
	    {
6305 6306 6307
	      push_warning_printf(YYTHD, MYSQL_ERROR::WARN_LEVEL_WARN,
	                          ER_SP_UNINIT_VAR, ER(ER_SP_UNINIT_VAR),
				  $1.str);
6308
	    }
6309
	    $$ = (Item*) new Item_splocal($1, spv->offset);
6310
            lex->variables_used= 1;
6311 6312 6313
	  }
	  else
	  {
pem@mysql.com's avatar
pem@mysql.com committed
6314
	    SELECT_LEX *sel=Select;
6315 6316 6317
	    $$= (sel->parsing_place != SELECT_LEX_NODE::IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
                 (Item*) new Item_field(NullS,NullS,$1.str) :
pem@mysql.com's avatar
pem@mysql.com committed
6318
	         (Item*) new Item_ref(0,0, NullS,NullS,$1.str);
6319
	  }
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
6320
        }
6321 6322 6323 6324 6325 6326 6327 6328 6329 6330
        | simple_ident_q { $$= $1; }
	;

simple_ident_nospvar:
	ident
	{
	  SELECT_LEX *sel=Select;
	  $$= (sel->parsing_place != SELECT_LEX_NODE::IN_HAVING ||
	       sel->get_in_sum_expr() > 0) ?
              (Item*) new Item_field(NullS,NullS,$1.str) :
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
6331
	      (Item*) new Item_ref(0,0,NullS,NullS,$1.str);
6332 6333 6334 6335 6336 6337
	}
	| simple_ident_q { $$= $1; }
	;	

simple_ident_q:
	ident '.' ident
6338
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6339
	  THD *thd= YYTHD;
6340
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6341
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6342 6343
	  if (sel->no_table_names_allowed)
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6344
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6345 6346 6347
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $1.str, thd->where);
	  }
6348 6349 6350
	  $$= (sel->parsing_place != SELECT_LEX_NODE::IN_HAVING ||
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field(NullS,$1.str,$3.str) :
hf@deer.(none)'s avatar
hf@deer.(none) committed
6351
	      (Item*) new Item_ref(0,0,NullS,$1.str,$3.str);
6352
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6353
	| '.' ident '.' ident
6354
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6355
	  THD *thd= YYTHD;
6356
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6357
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6358 6359
	  if (sel->no_table_names_allowed)
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6360
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6361 6362 6363
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $2.str, thd->where);
	  }
6364 6365 6366
	  $$= (sel->parsing_place != SELECT_LEX_NODE::IN_HAVING ||
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field(NullS,$2.str,$4.str) :
hf@deer.(none)'s avatar
hf@deer.(none) committed
6367
              (Item*) new Item_ref(0,0,NullS,$2.str,$4.str);
6368
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6369
	| ident '.' ident '.' ident
6370
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6371
	  THD *thd= YYTHD;
6372
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6373
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6374 6375
	  if (sel->no_table_names_allowed)
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6376
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6377 6378 6379
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $3.str, thd->where);
	  }
6380 6381 6382 6383 6384
	  $$= (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) :
hf@deer.(none)'s avatar
hf@deer.(none) committed
6385 6386
	      (Item*) new Item_ref(0,0,(YYTHD->client_capabilities &
				        CLIENT_NO_SCHEMA ? NullS : $1.str),
6387
                                   $3.str, $5.str);
6388
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6389 6390 6391 6392


field_ident:
	ident			{ $$=$1;}
6393
	| ident '.' ident	{ $$=$3;}	/* Skip schema name in create*/
6394
	| '.' ident		{ $$=$2;}	/* For Delphi */;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6395 6396 6397

table_ident:
	ident			{ $$=new Table_ident($1); }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6398
	| ident '.' ident	{ $$=new Table_ident(YYTHD, $1,$3,0);}
6399 6400 6401
	| '.' ident		{ $$=new Table_ident($2);} /* For Delphi */
        ;

serg@serg.mylan's avatar
serg@serg.mylan committed
6402
table_ident_nodb:
monty@mysql.com's avatar
monty@mysql.com committed
6403
	ident			{ LEX_STRING db={(char*) any_db,3}; $$=new Table_ident(YYTHD, db,$1,0); }
6404
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6405

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6406
IDENT_sys:
6407 6408 6409 6410 6411
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422
            {
              CHARSET_INFO *cs= system_charset_info;
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
                                                   $1.length);
              if (wlen < $1.length)
              {
                net_printf(YYTHD, ER_INVALID_CHARACTER_STRING, cs->csname,
                           $1.str + wlen);
                YYABORT;
              }
6423
	      $$= $1;
6424
            }
6425 6426 6427 6428
	    else
	      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
6429 6430 6431 6432 6433 6434
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6435 6436
	  if (thd->charset_is_system_charset)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6437
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6438 6439
	    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
6440 6441 6442
	}
	;

6443
TEXT_STRING_literal:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6444 6445 6446
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6447 6448
	  if (thd->charset_is_collation_connection)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6449
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6450 6451
	    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
6452 6453 6454 6455
	}
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6456
ident:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6457
	IDENT_sys	    { $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6458 6459
	| keyword
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6460 6461 6462
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
6463 6464
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6465 6466

ident_or_text:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6467 6468 6469
	ident 			{ $$=$1;}
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6470 6471 6472 6473

user:
	ident_or_text
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6474 6475
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6476
	    YYABORT;
6477 6478 6479 6480
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6481 6482
	| ident_or_text '@' ident_or_text
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6483 6484
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6485 6486
	      YYABORT;
	    $$->user = $1; $$->host=$3;
6487
	  }
6488
	| CURRENT_USER optional_braces
6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505
	{
          THD *thd= YYTHD;
          if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
            YYABORT;
          $$->user.str= thd->priv_user;
          $$->user.length= strlen(thd->priv_user);
          if (*thd->priv_host != 0)
          {
            $$->host.str= thd->priv_host;
            $$->host.length= strlen(thd->priv_host);
          }
          else
          {
            $$->host.str= (char *) "%";
            $$->host.length= 1;
          }
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6506 6507 6508 6509 6510

/* Keyword that we allow for identifiers */

keyword:
	ACTION			{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6511
	| ADDDATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6512
	| AFTER_SYM		{}
6513
	| AGAINST		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6514
	| AGGREGATE_SYM		{}
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
6515
	| ALGORITHM_SYM		{}
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6516
	| ANY_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6517
	| ASCII_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6518
	| AUTO_INC		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6519 6520
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
6521
	| BACKUP_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6522
	| BEGIN_SYM		{}
6523
	| BERKELEY_DB_SYM	{}
6524
	| BINLOG_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6525 6526
	| BIT_SYM		{}
	| BOOL_SYM		{}
6527
	| BOOLEAN_SYM		{}
6528
	| BYTE_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6529
	| BTREE_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6530
	| CACHE_SYM		{}
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
6531
	| CASCADED              {}
6532
	| CHANGED		{}
6533
	| CHARSET		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6534
	| CHECKSUM_SYM		{}
6535
	| CIPHER_SYM		{}
6536
	| CLIENT_SYM		{}
6537
	| CLOSE_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6538
	| COLLATION_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6539
	| COMMENT_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6540
	| COMMITTED_SYM		{}
6541
	| COMMIT_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6542
	| COMPRESSED_SYM	{}
6543
	| CONCURRENT		{}
6544
	| CUBE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6545 6546 6547 6548
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
sergefp@mysql.com's avatar
sergefp@mysql.com committed
6549
        | DEALLOCATE_SYM        {}
6550
	| DEFINER_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6551
	| DELAY_KEY_WRITE_SYM	{}
6552 6553
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
6554
	| DISCARD		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6555
	| DO_SYM		{}
6556 6557
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6558 6559 6560
	| DYNAMIC_SYM		{}
	| END			{}
	| ENUM			{}
6561
	| ENGINE_SYM		{}
6562
	| ENGINES_SYM		{}
6563
	| ERRORS		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6564
	| ESCAPE_SYM		{}
6565
	| EVENTS_SYM		{}
6566
	| EXECUTE_SYM		{}
6567
        | EXPANSION_SYM         {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6568
	| EXTENDED_SYM		{}
6569
	| FAST_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6570 6571
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6572
	| FULL			{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6573 6574 6575 6576
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
	| FLUSH_SYM		{}
6577
	| GEOMETRY_SYM		{}
6578
	| GEOMETRYCOLLECTION	{}
6579
	| GET_FORMAT		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6580
	| GRANTS		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6581
	| GLOBAL_SYM		{}
6582
	| HANDLER_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6583
	| HASH_SYM		{}
6584
	| HELP_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6585 6586 6587
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
6588
	| INVOKER_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
6589
	| IMPORT		{}
6590
	| INDEXES		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6591
	| ISOLATION		{}
6592
	| ISSUER_SYM		{}
6593
	| INNOBASE_SYM		{}
6594
	| INSERT_METHOD		{}
6595
	| RELAY_THREAD		{}
6596
	| LABEL_SYM             {}
6597
	| LANGUAGE_SYM          {}
6598
	| LAST_SYM		{}
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6599
	| LEAVES                {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6600
	| LEVEL_SYM		{}
6601
	| LINESTRING		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6602
	| LOCAL_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6603
	| LOCKS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6604 6605 6606 6607 6608 6609 6610 6611 6612
	| 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	{}
6613
	| MASTER_SERVER_ID_SYM  {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6614
	| MASTER_CONNECT_RETRY_SYM	{}
6615 6616 6617 6618 6619 6620
	| 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
6621 6622 6623
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
6624
	| MEDIUM_SYM		{}
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
6625
	| MERGE_SYM		{}
6626
	| MICROSECOND_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6627 6628 6629
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6630
	| MODE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6631
	| MONTH_SYM		{}
6632 6633 6634
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
6635
	| NAME_SYM              {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6636
	| NAMES_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6637 6638
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
6639
	| NDBCLUSTER_SYM	{}
6640
	| NEXT_SYM		{}
6641
	| NEW_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6642
	| NO_SYM		{}
6643
	| NONE_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6644
	| NVARCHAR_SYM		{}
6645
	| OFFSET_SYM		{}
6646
	| OLD_PASSWORD		{}
6647
	| ONE_SHOT_SYM		{}
6648
	| OPEN_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6649
	| PACK_KEYS_SYM		{}
6650
	| PARTIAL		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6651
	| PASSWORD		{}
6652
	| POINT_SYM		{}
6653
	| POLYGON		{}
sergefp@mysql.com's avatar
sergefp@mysql.com committed
6654
        | PREPARE_SYM           {}
6655
	| PREV_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6656 6657
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
6658
	| QUARTER_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6659
	| QUERY_SYM		{}
6660
	| QUICK			{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6661
	| RAID_0_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6662 6663
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6664
	| RAID_STRIPED_SYM	{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6665
	| RAID_TYPE		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6666 6667
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6668 6669
	| RELOAD		{}
	| REPAIR		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6670
	| REPEATABLE_SYM	{}
6671
	| REPLICATION		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6672
	| RESET_SYM		{}
6673
	| RESOURCES		{}
6674
	| RESTORE_SYM		{}
6675
	| RETURNS_SYM           {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6676
	| ROLLBACK_SYM		{}
6677
	| ROLLUP_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6678 6679 6680
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6681
	| RTREE_SYM		{}
6682
	| SAVEPOINT_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6683
	| SECOND_SYM		{}
6684
	| SECURITY_SYM		{}
6685
	| SERIAL_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6686 6687
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6688
	| SIGNED_SYM		{}
6689
	| SIMPLE_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6690
	| SHARE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6691
	| SHUTDOWN		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6692
	| SLAVE			{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6693
	| SOUNDS_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6694
	| SQL_CACHE_SYM		{}
6695
	| SQL_BUFFER_RESULT	{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6696
	| SQL_NO_CACHE_SYM	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6697
	| SQL_THREAD		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6698 6699 6700
	| START_SYM		{}
	| STATUS_SYM		{}
	| STOP_SYM		{}
6701
	| STORAGE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6702
	| STRING_SYM		{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6703
	| SUBDATE_SYM		{}
6704
	| SUBJECT_SYM		{}
6705
	| SUPER_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
6706
	| TABLESPACE		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6707
	| TEMPORARY		{}
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
6708
	| TEMPTABLE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6709
	| TEXT_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6710
	| TRANSACTION_SYM	{}
6711
	| TRUNCATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6712
	| TIMESTAMP		{}
6713 6714
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6715 6716
	| TIME_SYM		{}
	| TYPE_SYM		{}
6717
	| TYPES_SYM		{}
6718
	| FUNCTION_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6719
	| UNCOMMITTED_SYM	{}
6720
	| UNICODE_SYM		{}
6721
	| UNTIL_SYM		{}
6722
	| USER			{}
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
6723
	| USE_FRM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6724
	| VARIABLES		{}
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
6725
	| VIEW_SYM		{}
6726
	| VALUE_SYM		{}
6727
	| WARNINGS		{}
6728
	| WEEK_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6729
	| WORK_SYM		{}
6730
	| X509_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6731 6732
	| YEAR_SYM		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6733 6734 6735 6736 6737 6738

/* Option functions */

set:
	SET opt_option
	{
6739 6740
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
6741
	  lex->option_type=OPT_SESSION;
6742
	  lex->var_list.empty();
6743
          lex->one_shot_set= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6744
	}
6745 6746 6747
	option_value_list
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6748 6749 6750

opt_option:
	/* empty */ {}
6751
	| OPTION {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6752 6753

option_value_list:
6754 6755 6756 6757 6758 6759 6760 6761
	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; }
6762
	| ONE_SHOT_SYM	{ Lex->option_type= OPT_SESSION; Lex->one_shot_set= 1; }
6763 6764 6765 6766
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6767
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
6768 6769 6770 6771 6772 6773
	| 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
6774
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
6775 6776 6777
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6778 6779

option_value:
6780
	  '@' ident_or_text equal expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6781
	  {
pem@mysql.telia.com's avatar
pem@mysql.telia.com committed
6782 6783
	    Lex->var_list.push_back(new set_var_user(new Item_func_set_user_var($2,$4)));
	  }
6784
	| internal_variable_name equal set_expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6785
	  {
6786
	    LEX *lex=Lex;
pem@mysql.telia.com's avatar
pem@mysql.telia.com committed
6787 6788 6789 6790 6791 6792 6793 6794

	    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 */
6795
	      sp_pcontext *ctx= lex->spcont;
6796 6797 6798 6799
	      sp_pvar_t *spv;
              sp_instr_set *i;
	      Item *it;

6800
	      spv= ctx->find_pvar(&$1.base_name);
pem@mysql.telia.com's avatar
pem@mysql.telia.com committed
6801

6802 6803 6804 6805 6806 6807
	      if ($3)
	        it= $3;
	      else if (spv->dflt)
	        it= spv->dflt;
	      else
	        it= new Item_null();
6808
              i= new sp_instr_set(lex->sphead->instructions(), ctx,
6809
	                          spv->offset, it, spv->type);
6810 6811
	      i->tables= lex->query_tables;
	      lex->query_tables= 0;
pem@mysql.telia.com's avatar
pem@mysql.telia.com committed
6812 6813 6814
	      lex->sphead->add_instr(i);
	      spv->isset= TRUE;
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6815
	  }
6816
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
6817 6818
	  {
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6819 6820
	    lex->var_list.push_back(new set_var((enum_var_type) $3, $4.var,
						&$4.base_name, $6));
6821 6822 6823 6824
	  }
	| TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	  {
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6825 6826 6827
	    LEX_STRING tmp;
	    tmp.str=0;
	    tmp.length=0;
6828
	    lex->var_list.push_back(new set_var(lex->option_type,
6829
						find_sys_var("tx_isolation"),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6830
						&tmp,
6831
						new Item_int((int32) $4)));
6832
	  }
6833
	| charset old_or_new_charset_name_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6834
	{
6835
	  THD *thd= YYTHD;
6836
	  LEX *lex= Lex;
6837
	  $2= $2 ? $2: global_system_variables.character_set_client;
6838
	  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
6839
	}
6840
	| NAMES_SYM charset_name_or_default opt_collate
6841
	{
6842
	  THD *thd= YYTHD;
6843
	  LEX *lex= Lex;
6844
	  $2= $2 ? $2 : global_system_variables.character_set_client;
6845 6846
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
6847
	  {
6848 6849
	    net_printf(thd,ER_COLLATION_CHARSET_MISMATCH,$3->name,$2->csname);
	    YYABORT;
6850
	  }
6851
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
6852
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6853
	| PASSWORD equal text_or_password
6854
	  {
6855
	    THD *thd=YYTHD;
6856
	    LEX_USER *user;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6857
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
6858 6859 6860
	      YYABORT;
	    user->host.str=0;
	    user->user.str=thd->priv_user;
6861
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
6862 6863
	  }
	| PASSWORD FOR_SYM user equal text_or_password
6864
	  {
6865 6866
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
6867
	;
6868

6869 6870 6871
internal_variable_name:
	ident
	{
pem@mysql.telia.com's avatar
pem@mysql.telia.com committed
6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890
	  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;
	  }
6891
	}
6892 6893
	| ident '.' ident
	  {
6894 6895
            if (check_reserved_words(&$1))
            {
6896
	      yyerror(ER(ER_SYNTAX_ERROR));
6897 6898
              YYABORT;
            }
6899 6900 6901 6902 6903
	    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
6904 6905
	    $$.var= tmp;
	    $$.base_name= $1;
6906 6907 6908 6909 6910 6911 6912 6913
	  }
	| 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
6914 6915 6916
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
6917
	  }
6918
        ;
6919 6920 6921 6922 6923 6924 6925

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6927 6928 6929 6930
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
6931
	    $$= $3.length ? YYTHD->variables.old_passwords ?
6932 6933 6934 6935 6936 6937 6938 6939
	        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;
6940 6941
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6942

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

6944
set_expr_or_default:
6945 6946
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
6947 6948
	| 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
6949
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
6950
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6951 6952


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6953 6954 6955 6956 6957 6958 6959
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
	  Lex->sql_command=SQLCOM_LOCK_TABLES;
	}
6960 6961 6962
	table_lock_list
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6963 6964 6965

table_or_tables:
	TABLE_SYM
6966
	| TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6967 6968 6969

table_lock_list:
	table_lock
6970
	| table_lock_list ',' table_lock;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6971 6972 6973

table_lock:
	table_ident opt_table_alias lock_option
6974
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6975
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
6976 6977
	   YYABORT;
	}
6978
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6979 6980 6981

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
6982
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6983
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
6984 6985
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6986 6987

unlock:
6988 6989
	UNLOCK_SYM table_or_tables { Lex->sql_command=SQLCOM_UNLOCK_TABLES; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6990 6991


6992
/*
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
6993
** Handler: direct access to ISAM functions
6994 6995 6996 6997 6998
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
6999 7000
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_OPEN;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7001
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
7002 7003
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
7004
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
7005
	{
7006 7007
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_CLOSE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7008
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
7009 7010
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
7011
	| HANDLER_SYM table_ident_nodb READ_SYM
7012
	{
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
7013 7014 7015
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
7016 7017
	  lex->current_select->select_limit= 1;
	  lex->current_select->offset_limit= 0L;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7018
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
7019
	    YYABORT;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7020
        }
7021
        handler_read_or_scan where_clause opt_limit_clause {}
7022
        ;
7023

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7024 7025
handler_read_or_scan:
	handler_scan_function         { Lex->backup_dir= 0; }
7026 7027
        | ident handler_rkey_function { Lex->backup_dir= $1.str; }
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7028 7029 7030

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
7031 7032
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7033 7034 7035 7036 7037 7038

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;  }
7039 7040
	| handler_rkey_mode
	{
7041 7042
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
7043
	  lex->ha_rkey_mode=$1;
7044
	  if (!(lex->insert_list = new List_item))
7045
	    YYABORT;
7046 7047
	} '(' values ')' { }
        ;
7048 7049

handler_rkey_mode:
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
7050 7051 7052 7053
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
7054 7055
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
7056

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7057 7058 7059 7060 7061
/* GRANT / REVOKE */

revoke:
	REVOKE
	{
7062 7063 7064 7065 7066
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_REVOKE;
	  lex->users_list.empty();
	  lex->columns.empty();
	  lex->grant= lex->grant_tot_col=0;
7067
	  lex->select_lex.db=0;
7068 7069
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
7070
	  bzero((char*) &lex->mqh, sizeof(lex->mqh));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7071
	}
7072 7073 7074 7075 7076
	revoke_command
	{}
        ;

revoke_command:
7077 7078
	grant_privileges ON opt_table FROM user_list
	{}
7079
	|
7080
	ALL PRIVILEGES ',' GRANT OPTION FROM user_list
7081 7082 7083
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
7084
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7085 7086 7087 7088

grant:
	GRANT
	{
7089 7090 7091
	  LEX *lex=Lex;
	  lex->users_list.empty();
	  lex->columns.empty();
7092 7093
	  lex->sql_command = SQLCOM_GRANT;
	  lex->grant= lex->grant_tot_col= 0;
7094
	  lex->select_lex.db= 0;
7095 7096
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
7097
	  bzero((char *)&(lex->mqh),sizeof(lex->mqh));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7098 7099
	}
	grant_privileges ON opt_table TO_SYM user_list
7100 7101 7102
	require_clause grant_options
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7103 7104 7105

grant_privileges:
	grant_privilege_list {}
7106
	| ALL PRIVILEGES	{ Lex->grant = GLOBAL_ACLS;}
7107 7108
	| ALL			{ Lex->grant = GLOBAL_ACLS;}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7109 7110 7111

grant_privilege_list:
	grant_privilege
7112
	| grant_privilege_list ',' grant_privilege;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7113 7114

grant_privilege:
serg@serg.mylan's avatar
serg@serg.mylan committed
7115
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
7116 7117 7118
	| 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 {}
7119 7120
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
7121
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7122 7123 7124
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
7125
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7126 7127 7128 7129
	| RELOAD	{ Lex->grant |= RELOAD_ACL;}
	| SHUTDOWN	{ Lex->grant |= SHUTDOWN_ACL;}
	| PROCESS	{ Lex->grant |= PROCESS_ACL;}
	| FILE_SYM	{ Lex->grant |= FILE_ACL;}
7130 7131 7132 7133 7134
	| 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; }
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
7135 7136 7137 7138
	| REPLICATION SLAVE  { Lex->grant |= REPL_SLAVE_ACL; }
	| REPLICATION CLIENT_SYM { Lex->grant |= REPL_CLIENT_ACL; }
	| CREATE VIEW_SYM { Lex->grant |= CREATE_VIEW_ACL; }
	| SHOW VIEW_SYM { Lex->grant |= SHOW_VIEW_ACL; }
7139
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7140

7141

7142 7143
opt_and:
	/* empty */	{}
7144
	| AND_SYM	{}
7145 7146 7147 7148 7149 7150 7151 7152 7153
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
7154 7155 7156 7157
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
7158
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "SUBJECT");
7159 7160 7161 7162 7163 7164 7165 7166 7167
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
7168
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "ISSUER");
7169 7170 7171 7172 7173 7174 7175 7176 7177
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
7178
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "CIPHER");
7179 7180 7181 7182 7183
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
7184

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7185 7186 7187
opt_table:
	'*'
	  {
7188
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7189
	    lex->current_select->db= lex->thd->db;
7190
	    if (lex->grant == GLOBAL_ACLS)
7191 7192
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7193
	    {
7194
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
7195
	      YYABORT;
7196
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7197 7198 7199
	  }
	| ident '.' '*'
	  {
7200
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7201
	    lex->current_select->db = $1.str;
7202
	    if (lex->grant == GLOBAL_ACLS)
7203 7204
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7205
	    {
7206
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7207 7208 7209 7210 7211
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
7212
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7213
	    lex->current_select->db = NULL;
7214 7215
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
7216
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7217
	    {
7218
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7219 7220 7221 7222 7223
	      YYABORT;
	    }
	  }
	| table_ident
	  {
7224
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7225
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7226
	      YYABORT;
7227
	    if (lex->grant == GLOBAL_ACLS)
7228
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
7229 7230
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7231 7232 7233


user_list:
7234 7235 7236 7237 7238 7239 7240
	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
7241 7242 7243 7244 7245 7246 7247 7248


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
7249
             if (YYTHD->variables.old_passwords)
7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266
             {
               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
7267 7268 7269 7270 7271
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
	  { $$=$1; $1->password=$5 ; }
	| user
7272 7273
	  { $$=$1; $1->password.str=NullS; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7274 7275 7276


opt_column_list:
7277 7278 7279 7280 7281
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
7282
	| '(' column_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7283 7284 7285

column_list:
	column_list ',' column_list_id
7286
	| column_list_id;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7287 7288 7289 7290

column_list_id:
	ident
	{
monty@mysql.com's avatar
monty@mysql.com committed
7291
	  String *new_str = new (&YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7292 7293
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
7294
	  LEX *lex=Lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7295 7296
	  while ((point=iter++))
	  {
7297 7298
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7299 7300
		break;
	  }
7301
	  lex->grant_tot_col|= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7302
	  if (point)
7303
	    point->rights |= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7304
	  else
7305
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
7306 7307
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7308

tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
7309 7310

require_clause: /* empty */
7311
        | REQUIRE_SYM require_list
7312 7313 7314
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
7315
        | REQUIRE_SYM SSL_SYM
7316 7317 7318
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
7319
        | REQUIRE_SYM X509_SYM
7320 7321 7322 7323 7324 7325 7326
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
7327
          ;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
7328

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7329
grant_options:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7330
	/* empty */ {}
7331
	| WITH grant_option_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7332

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7333 7334
grant_option_list:
	grant_option_list grant_option {}
7335 7336
	| grant_option {}
        ;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7337 7338 7339

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
7340
        | MAX_QUERIES_PER_HOUR ULONG_NUM
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7341
        {
7342
	  Lex->mqh.questions=$2;
7343
	  Lex->mqh.bits |= 1;
7344
	}
7345
        | MAX_UPDATES_PER_HOUR ULONG_NUM
7346
        {
7347
	  Lex->mqh.updates=$2;
7348
	  Lex->mqh.bits |= 2;
7349
	}
7350
        | MAX_CONNECTIONS_PER_HOUR ULONG_NUM
7351
        {
7352
	  Lex->mqh.connections=$2;
7353
	  Lex->mqh.bits |= 4;
7354 7355
	}
        ;
7356

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7357
begin:
7358 7359
	BEGIN_SYM   { Lex->sql_command = SQLCOM_BEGIN;} opt_work {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7360 7361 7362

opt_work:
	/* empty */ {}
7363 7364
	| WORK_SYM {;}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7365 7366

commit:
7367
	COMMIT_SYM   { Lex->sql_command = SQLCOM_COMMIT;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7368 7369

rollback:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7370
	ROLLBACK_SYM
7371 7372 7373 7374 7375
	{
	  Lex->sql_command = SQLCOM_ROLLBACK;
	}
	| ROLLBACK_SYM TO_SYM SAVEPOINT_SYM ident
	{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
7376
	  Lex->sql_command = SQLCOM_ROLLBACK_TO_SAVEPOINT;
7377 7378 7379 7380 7381 7382 7383 7384
	  Lex->savepoint_name = $4.str;
	};
savepoint:
	SAVEPOINT_SYM ident
	{
	  Lex->sql_command = SQLCOM_SAVEPOINT;
	  Lex->savepoint_name = $2.str;
	};
7385 7386

/*
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7387
   UNIONS : glue selects together
7388 7389 7390
*/


7391
union_clause:
7392
	/* empty */ {}
7393 7394
	| union_list
	;
7395 7396

union_list:
7397
	UNION_SYM union_option
7398 7399 7400 7401 7402
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
7403
	    net_printf(lex->thd, ER_WRONG_USAGE, "UNION", "INTO");
7404
	    YYABORT;
7405
	  }
7406
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
7407
	  {
7408
            yyerror(ER(ER_SYNTAX_ERROR));
7409 7410
	    YYABORT;
	  }
7411
	  if (mysql_new_select(lex, 0))
7412
	    YYABORT;
7413
          mysql_init_select(lex);
7414
	  lex->current_select->linkage=UNION_TYPE;
7415 7416 7417
          if ($2) /* UNION DISTINCT - remember position */
            lex->current_select->master_unit()->union_distinct=
                                                      lex->current_select;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7418
	}
7419
	select_init {}
7420
	;
7421 7422

union_opt:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7423
	union_list {}
7424 7425
	| optional_order_or_limit {}
	;
7426 7427

optional_order_or_limit:
7428
	/* Empty */ {}
7429 7430
	|
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7431
	    THD *thd= YYTHD;
7432
	    LEX *lex= thd->lex;
7433
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7434
	    SELECT_LEX *sel= lex->current_select;
7435
	    SELECT_LEX_UNIT *unit= sel->master_unit();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7436
	    SELECT_LEX *fake= unit->fake_select_lex;
7437 7438 7439 7440 7441 7442
	    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
7443
	    thd->where= "global ORDER clause";
7444
	  }
7445
	order_or_limit
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7446 7447
          {
	    THD *thd= YYTHD;
7448
	    thd->lex->current_select->no_table_names_allowed= 0;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7449 7450
	    thd->where= "";
          }
7451 7452 7453
	;

order_or_limit:
7454 7455
	order_clause opt_limit_clause_init
	| limit_clause
7456
	;
7457 7458

union_option:
7459 7460 7461 7462
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
7463

7464 7465
singlerow_subselect:
	subselect_start singlerow_subselect_init
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7466 7467 7468 7469
	subselect_end
	{
	  $$= $2;
	};
7470

7471
singlerow_subselect_init:
7472
	select_init2
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7473
	{
7474 7475
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7476
	};
7477 7478

exists_subselect:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7479 7480 7481 7482 7483
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
7484 7485

exists_subselect_init:
7486
	select_init2
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7487
	{
7488 7489
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7490
	};
7491

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7492 7493 7494 7495 7496 7497 7498 7499
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
7500
  select_init2
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7501
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7502
    $$= Lex->current_select->master_unit()->first_select();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7503 7504
  };

7505
subselect_start:
7506
	'(' SELECT_SYM
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7507
	{
7508
	  LEX *lex=Lex;
7509
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
7510 7511
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
7512 7513
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
7514 7515
	    YYABORT;
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7516 7517 7518
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
7519 7520

subselect_end:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7521 7522 7523
	')'
	{
	  LEX *lex=Lex;
7524
	  lex->current_select = lex->current_select->return_after_parsing();
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7525
	};
serg@serg.mylan's avatar
serg@serg.mylan committed
7526

bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555
opt_view_list:
	/* empty */ {}
	| '(' view_list ')'
	;

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

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

algorithm:
	/* empty */
	  { Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED; }
	| ALGORITHM_SYM EQ MERGE_SYM
	  { Lex->create_view_algorithm= VIEW_ALGORITHM_MERGE; }
	| ALGORITHM_SYM EQ TEMPTABLE_SYM
7556
	  { Lex->create_view_algorithm= VIEW_ALGORITHM_TMPTABLE; }
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
7557 7558 7559 7560 7561 7562 7563 7564
	;
check_option:
        /* empty */ {}
        | WITH CHECK_SYM OPTION {}
        | WITH CASCADED CHECK_SYM OPTION {}
        | WITH LOCAL_SYM CHECK_SYM OPTION {}
        ;