sql_yacc.yy 183 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 614 615
%token  CURSOR_SYM
%token  ELSEIF_SYM
%token  ITERATE_SYM
%token  LEAVE_SYM
%token  LOOP_SYM
616
%token  REPEAT_SYM
617 618 619 620 621
%token  UNTIL_SYM
%token  WHILE_SYM
%token  ASENSITIVE_SYM
%token  INSENSITIVE_SYM
%token  SENSITIVE_SYM
622

623 624
%token  ISSUER_SYM
%token  SUBJECT_SYM
625
%token  CIPHER_SYM
626

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
643
%type <lex_str>
644 645
	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
646
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
647
	NCHAR_STRING opt_component key_cache_name
648
        sp_opt_label
bk@work.mysql.com's avatar
bk@work.mysql.com committed
649 650 651 652 653

%type <lex_str_ptr>
	opt_table_alias

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

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

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

%type <num>
664
	type int_type real_type order_dir opt_field_spec lock_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
665
	udf_type if_exists opt_local opt_table_options table_options
666 667
        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
668
        opt_ignore_leaves fulltext_options spatial_type union_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
669 670

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

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

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

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

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

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

700 701 702
%type <key_alg>
	key_alg opt_btree_or_rtree

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

%type <key_part>
	key_part

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

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

716 717
%type <interval_time_st> interval_time_st

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

%type <row_type> row_types

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

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

726
%type <cast_type> cast_type
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
727

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

%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword

%type <lex_user> user grant_user

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

743 744
%type <variable> internal_variable_name

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

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

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

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

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


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

verb_clause:
818 819 820 821 822 823
	  statement
	| begin
	;

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

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

885 886 887 888 889 890
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


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

905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
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;
        };
920

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

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

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

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;
954
        }
sergefp@mysql.com's avatar
sergefp@mysql.com committed
955 956
        ;

957 958
/* help */

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

/* change master */

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

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

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

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

/* create a table */

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

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

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

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

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

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

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

1184
	    lex->sphead->init_strings(YYTHD, lex, $3);
1185 1186 1187 1188
	    lex->sql_command= SQLCOM_CREATE_PROCEDURE;
	    /* Restore flag if it was cleared above */
	    if (lex->sphead->m_old_cmq)
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
1189
	    lex->sphead->restore_thd_mem_root(YYTHD);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
	  }
	| 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
	  {}
1203 1204
	;

1205
sp_name:
1206 1207 1208 1209 1210 1211 1212 1213 1214
	  IDENT_sys '.' IDENT_sys
	  {
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
	| IDENT_sys
	  {
	    $$= sp_name_current_db_new(YYTHD, $1);
	  }
1215 1216
	;

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

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

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

	    lex->spcont->set_params();
	    lex->sphead->m_param_end= lex->tok_start;
	  }
	  RETURNS_SYM
	  {
1261 1262 1263 1264 1265
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;

	    sp->m_returns_begin= lex->tok_start;
	    sp->m_returns_cs= lex->charset= NULL;
1266
	  }
1267
	  type
1268
	  {
1269
	    LEX *lex= Lex;
1270
	    sp_head *sp= lex->sphead;
1271

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

	    lex->sphead->m_chistics= &lex->sp_chistics;
1282
	    lex->sphead->m_body_begin= lex->tok_start;
1283
	  }
1284
	  sp_proc_stmt
1285
	  {
1286
	    LEX *lex= Lex;
1287
	    sp_head *sp= lex->sphead;
1288

1289
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
1290
	    sp->init_strings(YYTHD, lex, lex->spname);
1291
	    /* Restore flag if it was cleared above */
1292
	    if (sp->m_old_cmq)
1293
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
1294
	    sp->restore_thd_mem_root(YYTHD);
1295
	  }
1296 1297
	;

1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
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 { }
1312 1313
	;

1314 1315 1316 1317
/* Alter characteristics */
sp_a_chistic:
	  sp_chistic     { }
	| NAME_SYM ident { Lex->name= $2.str; }
1318 1319
	;

1320 1321 1322 1323 1324 1325 1326
/* 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; }
	;
1327 1328

sp_suid:
1329 1330 1331 1332 1333 1334 1335 1336
	  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;
	  }
1337 1338
	;

1339
call:
1340
	  CALL_SYM sp_name
1341 1342 1343 1344
	  {
	    LEX *lex = Lex;

	    lex->sql_command= SQLCOM_CALL;
1345
	    lex->spname= $2;
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
	    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);
	  }
	;

1368 1369
/* Stored FUNCTION parameter declaration list */
sp_fdparam_list:
1370
	  /* Empty */
1371
	| sp_fdparams
1372 1373
	;

1374 1375 1376
sp_fdparams:
	  sp_fdparams ',' sp_fdparam
	| sp_fdparam
1377 1378
	;

1379
sp_fdparam:
1380
	  ident type
1381
	  {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1382 1383 1384 1385 1386
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

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

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
1406
	  sp_opt_inout ident type
1407
	  {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1408 1409 1410 1411 1412
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

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

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
1429
	  /* Empty */ {}
1430
	| sp_proc_stmts sp_proc_stmt ';'
1431 1432 1433 1434 1435
	;

sp_decls:
	  /* Empty */
	  {
1436
	    $$.vars= $$.conds= $$.hndlrs= $$.curs= 0;
1437 1438 1439
	  }
	| sp_decls sp_decl ';'
	  {
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
	    /* 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;
	    }
1454 1455 1456
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
1457
	    $$.curs= $1.curs + $2.curs;
1458 1459 1460 1461
	  }
	;

sp_decl:
1462
	  DECLARE_SYM sp_decl_idents type sp_opt_default
1463 1464 1465
	  {
	    LEX *lex= Lex;
	    uint max= lex->spcont->current_framesize();
1466 1467
	    enum enum_field_types type= (enum enum_field_types)$3;
	    Item *it= $4;
1468 1469 1470

	    for (uint i = max-$2 ; i < max ; i++)
	    {
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
	      lex->spcont->set_type(i, type);
	      if (! it)
	        lex->spcont->set_isset(i, FALSE);
	      else
	      {
	        sp_instr_set *in= new sp_instr_set(lex->sphead->instructions(),
	                                           i, it, type);

	        lex->sphead->add_instr(in);
	        lex->spcont->set_isset(i, TRUE);
1481
		lex->spcont->set_default(i, it);
1482
	      }
1483
	    }
1484
	    $$.vars= $2;
1485
	    $$.conds= $$.hndlrs= $$.curs= 0;
1486 1487 1488
	  }
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1489 1490 1491 1492 1493
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1494
	      net_printf(YYTHD, ER_SP_DUP_COND, $2.str);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1495 1496
	      YYABORT;
	    }
1497
	    YYTHD->lex->spcont->push_cond(&$2, $5);
1498
	    $$.vars= $$.hndlrs= $$.curs= 0;
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
	    $$.conds= 1;
	  }
	| DECLARE_SYM sp_handler_type HANDLER_SYM FOR_SYM
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_instr_hpush_jump *i=
              new sp_instr_hpush_jump(sp->instructions(), $2,
	                              ctx->current_framesize());

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

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

	      sp->add_instr(i);
	      sp->push_backpatch(i, lex->spcont->last_label()); /* Block end */
	    }
	    lex->sphead->backpatch(hlab);
1531
	    $$.vars= $$.conds= $$.curs= 0;
1532 1533 1534 1535
	    $$.hndlrs= $6;
	  }
	| DECLARE_SYM ident CURSOR_SYM FOR_SYM sp_cursor_stmt
	  {
1536 1537
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1538 1539 1540
	    sp_pcontext *spc= lex->spcont;
	    uint offp;
	    sp_instr_cpush *i;
1541

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1542 1543
	    if (spc->find_cursor(&$2, &offp, TRUE))
	    {
1544 1545
	      net_printf(YYTHD, ER_SP_DUP_CURS, $2.str);
	      delete $5;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1546 1547 1548
	      YYABORT;
	    }
            i= new sp_instr_cpush(sp->instructions(), $5);
1549 1550
	    sp->add_instr(i);
	    lex->spcont->push_cursor(&$2);
1551
	    $$.vars= $$.conds= $$.hndlrs= 0;
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
	    $$.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);
	  }
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
	;

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
1619
	| SQLSTATE_SYM opt_value TEXT_STRING_literal
1620
	  {		/* SQLSTATE */
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1621 1622
	    uint len= ($3.length < sizeof($$->sqlstate)-1 ?
                       $3.length : sizeof($$->sqlstate)-1);
1623 1624 1625

	    $$= (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
1626
	    memcpy($$->sqlstate, $3.str, len);
1627 1628 1629 1630
	    $$->sqlstate[len]= '\0';
	  }
	;

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1631 1632 1633 1634 1635
opt_value:
	  /* Empty */  {}
	| VALUE_SYM    {}
	;

1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
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;
1664 1665 1666 1667 1668 1669
	  }
	;

sp_decl_idents:
	  ident
	  {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1670 1671 1672 1673 1674
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
1675
	      net_printf(YYTHD, ER_SP_DUP_VAR, $1.str);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1676 1677 1678
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum_field_types)0, sp_param_in);
1679 1680 1681 1682
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1683 1684 1685 1686 1687
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$3, TRUE))
	    {
1688
	      net_printf(YYTHD, ER_SP_DUP_VAR, $3.str);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1689 1690 1691
	      YYABORT;
	    }
	    spc->push_pvar(&$3, (enum_field_types)0, sp_param_in);
1692 1693 1694 1695
	    $$= $1 + 1;
	  }
	;

1696 1697 1698 1699 1700
sp_opt_default:
	  /* Empty */ { $$ = NULL; }
        | DEFAULT expr { $$ = $2; }
	;

1701
sp_proc_stmt:
1702 1703 1704
	  {
	    Lex->sphead->reset_lex(YYTHD);
	  }
1705 1706 1707 1708
	  statement
	  {
	    LEX *lex= Lex;

1709 1710 1711 1712 1713 1714
	    /* QQ What else? This doesn't seem to be a practical way,
	          but at the moment we can't think of anything better... */
	    if ((lex->sql_command == SQLCOM_SELECT && !lex->result) ||
	        lex->sql_command == SQLCOM_SHOW_CREATE_PROC ||
	        lex->sql_command == SQLCOM_SHOW_CREATE_FUNC ||
	        lex->sql_command == SQLCOM_SHOW_STATUS_PROC ||
1715 1716
	        lex->sql_command == SQLCOM_SHOW_STATUS_FUNC ||
		lex->sql_command == SQLCOM_ANALYZE)
1717
	    {
1718
	      /* We maybe have one or more SELECT without INTO */
1719
	      lex->sphead->m_multi_results= TRUE;
1720
	    }
1721 1722 1723 1724 1725
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
	      send_error(YYTHD, ER_SP_NO_USE);
	      YYABORT;
	    }
1726 1727 1728 1729 1730
	    /* 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 ||
1731
		! lex->var_list.is_empty())
1732
	    {
1733 1734 1735 1736 1737
	      /* 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.
1738
	      */
1739 1740
	      if (lex->sphead->m_type == TYPE_ENUM_FUNCTION &&
		  lex->sql_command != SQLCOM_SET_OPTION)
1741
	      {
1742
		send_error(YYTHD, ER_SP_BADSTATEMENT);
1743 1744 1745 1746 1747 1748 1749 1750
		YYABORT;
	      }
	      else
	      {
		sp_instr_stmt *i=new sp_instr_stmt(lex->sphead->instructions());

		i->set_lex(lex);
		lex->sphead->add_instr(i);
1751
		lex->sp_lex_in_use= TRUE;
1752 1753 1754
	      }
            }
	    lex->sphead->restore_lex(YYTHD);
1755
          }
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
	| RETURN_SYM expr
	  {
	    LEX *lex= Lex;

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

1769 1770 1771 1772 1773 1774 1775
	      if ($2->type() == Item::SUBSELECT_ITEM)
	      {  /* QQ For now, just disallow subselects as values */
	        send_error(lex->thd, ER_SP_BADSTATEMENT);
	        YYABORT;
	      }
	      i= new sp_instr_freturn(lex->sphead->instructions(),
		                      $2, lex->sphead->m_returns);
1776
	      lex->sphead->add_instr(i);
1777
	      lex->sphead->m_has_return= TRUE;
1778 1779
	    }
	  }
1780
	| IF sp_if END IF {}
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
	| CASE_SYM WHEN_SYM
	  {
	    Lex->sphead->m_simple_case= FALSE;
	  }
	  sp_case END CASE_SYM {}
	| CASE_SYM expr WHEN_SYM
	  {
	    /* We "fake" this by using an anonymous variable which we
	       set to the expression. Note that all WHENs are evaluate
	       at the same frame level, so we then know that it's the
	       top-most variable in the frame. */
	    LEX *lex= Lex;
	    uint offset= lex->spcont->current_framesize();
	    sp_instr_set *i = new sp_instr_set(lex->sphead->instructions(),
	                                       offset, $2, MYSQL_TYPE_STRING);
	    LEX_STRING dummy;

	    dummy.str= (char *)"";
	    dummy.length= 0;
1800
	    lex->spcont->push_pvar(&dummy, MYSQL_TYPE_STRING, sp_param_in);
1801 1802 1803 1804 1805
	    lex->sphead->add_instr(i);
	    lex->sphead->m_simple_case= TRUE;
	  }
	  sp_case END CASE_SYM
	  {
1806
	    Lex->spcont->pop_pvar();
1807
	  }
1808 1809 1810 1811 1812
	| sp_labeled_control
	  {}
	| { /* Unlabeled controls get a secret label. */
	    LEX *lex= Lex;

1813
	    lex->spcont->push_label((char *)"", lex->sphead->instructions());
1814 1815 1816
	  }
	  sp_unlabeled_control
	  {
1817 1818
	    LEX *lex= Lex;

1819
	    lex->sphead->backpatch(lex->spcont->pop_label());
1820 1821 1822 1823
	  }
	| LEAVE_SYM IDENT
	  {
	    LEX *lex= Lex;
1824
	    sp_head *sp = lex->sphead;
1825 1826 1827 1828
	    sp_label_t *lab= lex->spcont->find_label($2.str);

	    if (! lab)
	    {
1829
	      net_printf(YYTHD, ER_SP_LILABEL_MISMATCH, "LEAVE", $2.str);
1830 1831 1832 1833
	      YYABORT;
	    }
	    else
	    {
1834
	      sp_instr_jump *i= new sp_instr_jump(sp->instructions());
1835

1836 1837
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
1838 1839 1840 1841 1842 1843 1844
	    }
	  }
	| ITERATE_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->find_label($2.str);

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1845
	    if (! lab || lab->isbegin)
1846
	    {
1847
	      net_printf(YYTHD, ER_SP_LILABEL_MISMATCH, "ITERATE", $2.str);
1848 1849 1850 1851 1852
	      YYABORT;
	    }
	    else
	    {
	      uint ip= lex->sphead->instructions();
1853
	      sp_instr_jump *i= new sp_instr_jump(ip, lab->ip); /* Jump back */
1854 1855 1856 1857

              lex->sphead->add_instr(i);
	    }
	  }
1858
	| OPEN_SYM ident
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_copen *i;

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

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
	      net_printf(YYTHD, ER_SP_CURSOR_MISMATCH, $2.str);
	      YYABORT;
	    }
	    i= new sp_instr_cfetch(sp->instructions(), offset);
	    sp->add_instr(i);
	  }
	  sp_fetch_list
	  { }
1890
	| CLOSE_SYM ident
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cclose *i;

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

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

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

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

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

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

1951 1952 1953 1954 1955 1956 1957 1958
sp_if:
	  expr THEN_SYM
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, $1);

1959
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
            sp->add_instr(i);
	  }
	  sp_proc_stmts
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump *i = new sp_instr_jump(ip);

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

1977
	    lex->sphead->backpatch(lex->spcont->pop_label());
1978 1979 1980 1981 1982 1983 1984 1985 1986
	  }
	;

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

1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
sp_case:
	  expr THEN_SYM
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

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

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2001
	      ivar.str= (char *)"_tmp_";
2002 2003 2004
	      ivar.length= 5;
	      Item *var= (Item*) new Item_splocal(ivar, 
						  ctx->current_framesize()-1);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2005
	      Item *expr= new Item_func_eq(var, $1);
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031

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

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

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

sp_whens:
2032 2033 2034 2035 2036 2037 2038 2039
	  /* Empty */
	  {
	    sp_head *sp= Lex->sphead;
	    uint ip= sp->instructions();
	    sp_instr_error *i= new sp_instr_error(ip, ER_SP_CASE_NOT_FOUND);

	    sp->add_instr(i);
	  }
2040
	| ELSE sp_proc_stmts {}
2041
	| WHEN_SYM sp_case {}
2042 2043
	;

2044 2045 2046 2047 2048 2049 2050 2051
sp_labeled_control:
	  IDENT ':'
	  {
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->find_label($1.str);

	    if (lab)
	    {
2052
	      net_printf(YYTHD, ER_SP_LABEL_REDEFINE, $1.str);
2053 2054 2055
	      YYABORT;
	    }
	    else
2056
	    {
2057 2058
	      lex->spcont->push_label($1.str,
	                              lex->sphead->instructions());
2059
	    }
2060
	  }
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2061
	  sp_unlabeled_control sp_opt_label
2062 2063 2064
	  {
	    LEX *lex= Lex;

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2065
	    if ($5.str)
2066
	    {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2067 2068 2069 2070 2071 2072 2073 2074
	      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;
	      }
2075
	    }
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2076
	    lex->sphead->backpatch(lex->spcont->pop_label());
2077 2078 2079
	  }
	;

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2080 2081 2082 2083 2084 2085 2086
sp_opt_label:
	  /* Empty  */
	  { $$.str= NULL; $$.length= 0; }
	| IDENT
	  { $$= $1; }
	;

2087
sp_unlabeled_control:
2088 2089 2090 2091
	  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
2092 2093
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->last_label();
2094

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2095 2096 2097
	    lab->isbegin= TRUE;
	    /* Scope duplicate checking */
	    lex->spcont->push_scope();
2098 2099 2100 2101 2102 2103 2104 2105 2106
	  }
	  sp_decls
	  sp_proc_stmts
	  END
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2107
  	    sp->backpatch(ctx->last_label());	/* We always has a label */
2108 2109
	    ctx->pop_pvar($3.vars);
	    ctx->pop_cond($3.conds);
2110
	    ctx->pop_cursor($3.curs);
2111
	    if ($3.hndlrs)
2112 2113 2114
	      sp->add_instr(new sp_instr_hpop(sp->instructions(),$3.hndlrs));
	    if ($3.curs)
	      sp->add_instr(new sp_instr_cpop(sp->instructions(), $3.curs));
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2115
	    ctx->pop_scope();
2116 2117
	  }
	| LOOP_SYM
2118 2119 2120 2121 2122 2123 2124 2125 2126
	  sp_proc_stmts END LOOP_SYM
	  {
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lab->ip);

	    lex->sphead->add_instr(i);
	  }
2127
	| WHILE_SYM expr DO_SYM
2128 2129
	  {
	    LEX *lex= Lex;
2130 2131
	    sp_head *sp= lex->sphead;
	    uint ip= sp->instructions();
2132 2133
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, $2);

2134 2135 2136
	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
            sp->add_instr(i);
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
	  }
	  sp_proc_stmts END WHILE_SYM
	  {
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lab->ip);

	    lex->sphead->add_instr(i);
	  }
2147 2148
	| REPEAT_SYM sp_proc_stmts UNTIL_SYM expr END REPEAT_SYM
	  {
2149 2150 2151 2152 2153 2154 2155
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, $4, lab->ip);

            lex->sphead->add_instr(i);
	  }
2156
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2157 2158

create2:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2159
 	'(' create2a {}
2160
	| opt_create_table_options create3 {}
venu@myvenu.com's avatar
venu@myvenu.com committed
2161 2162 2163 2164 2165 2166
	| LIKE table_ident
      	  {
      	    LEX *lex=Lex;
     	    if (!(lex->name= (char *)$2))
              YYABORT;
    	  }
2167 2168 2169 2170 2171 2172
	| '(' LIKE table_ident ')'
      	  {
      	    LEX *lex=Lex;
     	    if (!(lex->name= (char *)$3))
              YYABORT;
    	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2173
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2174

serg@serg.mylan's avatar
serg@serg.mylan committed
2175 2176
create2a:
        field_list ')' opt_create_table_options create3 {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2177
	|  create_select ')' { Select->set_braces(1);} union_opt {}
2178
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2179 2180

create3:
2181
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
2182
	| opt_duplicate opt_as     create_select
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2183
          { Select->set_braces(0);} union_clause {}
serg@serg.mylan's avatar
serg@serg.mylan committed
2184
	| opt_duplicate opt_as '(' create_select ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2185
          { Select->set_braces(1);} union_opt {}
2186 2187
        ;

serg@serg.mylan's avatar
serg@serg.mylan committed
2188
create_select:
2189
          SELECT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2190
          {
2191 2192
	    LEX *lex=Lex;
	    lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
2193 2194 2195 2196
	    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
2197 2198 2199 2200
	    /*
	      following work only with local list, global list is
	      created correctly in this case
	    */
2201
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
2202
	    mysql_init_select(lex);
2203
	    lex->current_select->parsing_place= SELECT_LEX_NODE::SELECT_LIST;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2204
          }
2205 2206 2207 2208
          select_options select_item_list
	  {
	    Select->parsing_place= SELECT_LEX_NODE::NO_MATTER;
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2209
	  opt_select_from
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
2210 2211 2212 2213 2214 2215 2216
	  {
	    /*
	      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
2217
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2218

2219 2220
opt_as:
	/* empty */ {}
2221
	| AS	    {};
2222

2223 2224 2225 2226 2227 2228 2229 2230 2231
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
2232 2233
	  opt_default COLLATE_SYM collation_name_or_default	
	  { Lex->create_info.default_table_charset=$3; }
2234
	| opt_default charset charset_name_or_default
2235
	  { Lex->create_info.default_table_charset=$3; }
2236
	;
2237

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2238
opt_table_options:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2239
	/* empty */	 { $$= 0; }
2240
	| table_options  { $$= $1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2241 2242 2243

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2246
table_option:
2247
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2248 2249

opt_if_not_exists:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2250
	/* empty */	 { $$= 0; }
2251
	| IF NOT EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2252 2253 2254

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

2257 2258 2259 2260
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
2261 2262
create_table_options:
	create_table_option
2263
	| create_table_option     create_table_options
2264
	| create_table_option ',' create_table_options;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2265 2266

create_table_option:
2267
	ENGINE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; }
2268
	| TYPE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; WARN_DEPRECATED("TYPE=storage_engine","ENGINE=storage_engine"); }
2269 2270 2271
	| 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
2272 2273
	| 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; }
2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
	| 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 ')'
2284 2285 2286
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
2287 2288
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
2289
	    lex->create_info.merge_list.elements--;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
2290 2291
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
2292
	    lex->select_lex.table_list.elements=1;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
2293 2294 2295
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
2296
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
2297
	  }
2298
	| opt_default charset opt_equal charset_name_or_default
2299
	  {
2300 2301 2302
	    Lex->create_info.default_table_charset= $4;
	    Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
	  }
2303
	| opt_default COLLATE_SYM opt_equal collation_name_or_default
2304
	  {
2305
	    Lex->create_info.default_table_charset= $4;
2306 2307
	    Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
	  }
2308
	| 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
2309 2310
	| DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys
	  { Lex->create_info.data_file_name= $4.str; }
2311
	| 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
2312

2313
storage_engines:
2314 2315 2316 2317
	ident_or_text
	{
	  $$ = ha_resolve_by_name($1.str,$1.length);
	  if ($$ == DB_TYPE_UNKNOWN) {
2318
	    net_printf(YYTHD, ER_UNKNOWN_STORAGE_ENGINE, $1.str);
2319 2320 2321
	    YYABORT;
	  }
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2322 2323 2324 2325 2326

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
2327
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2328 2329 2330 2331

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

2334 2335 2336
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
2337
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
2338

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2339
opt_select_from:
2340
	opt_limit_clause {}
2341
	| select_from select_lock_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2342 2343

udf_func_type:
2344
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
2345
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2346 2347 2348 2349

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
2350
	| INT_SYM {$$ = (int) INT_RESULT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2351 2352 2353

field_list:
	  field_list_item
2354
	| field_list ',' field_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2355 2356 2357


field_list_item:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2358
	   column_def
2359 2360 2361 2362
         | key_def
         ;

column_def:
2363
	  field_spec opt_check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2364 2365 2366 2367
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2368
	;
2369 2370

key_def:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2371
	key_type opt_ident key_alg '(' key_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2372
	  {
2373
	    LEX *lex=Lex;
2374
	    lex->key_list.push_back(new Key($1,$2, $3, 0, lex->col_list));
2375
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2376
	  }
2377 2378 2379 2380
	| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
	  {
	    LEX *lex=Lex;
	    const char *key_name= $3 ? $3:$1;
2381 2382
	    lex->key_list.push_back(new Key($2, key_name, $4, 0,
				    lex->col_list));
2383 2384
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
2385
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2386
	  {
2387
	    LEX *lex=Lex;
2388
	    lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
2389 2390 2391 2392 2393
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
2394 2395 2396
	    lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4 : $1,
					    HA_KEY_ALG_UNDEF, 1,
					    lex->col_list));
2397
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2398
	  }
2399 2400 2401 2402
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
2403
	| opt_constraint check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2404 2405
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
2406 2407 2408
	  }
	;

2409
opt_check_constraint:
2410
	/* empty */
2411 2412 2413 2414 2415
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
2416
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2417 2418

opt_constraint:
2419
	/* empty */		{ $$=(char*) 0; }
monty@mysql.com's avatar
monty@mysql.com committed
2420 2421 2422 2423 2424 2425
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2426 2427 2428 2429

field_spec:
	field_ident
	 {
2430 2431
	   LEX *lex=Lex;
	   lex->length=lex->dec=0; lex->type=0; lex->interval=0;
2432
	   lex->default_value= lex->on_update_value= 0;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2433
	   lex->comment=0;
2434
	   lex->charset=NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2435 2436 2437
	 }
	type opt_attribute
	{
2438
	  LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2439
	  if (add_field_to_list(lex->thd, $1.str,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2440
				(enum enum_field_types) $3,
2441
				lex->length,lex->dec,lex->type,
2442 2443
				lex->default_value, lex->on_update_value, 
                                lex->comment,
2444 2445
				lex->change,lex->interval,lex->charset,
				lex->uint_geom_type))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2446
	    YYABORT;
2447
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2448 2449

type:
2450
	int_type opt_len field_options	{ $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2451 2452 2453 2454 2455 2456
	| 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; }
2457 2458
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
2459
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2460 2461 2462
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
2463
	| nchar '(' NUM ')'		{ Lex->length=$3.str;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2464
					  $$=FIELD_TYPE_STRING;
2465
					  Lex->charset=national_charset_info; }
2466
	| nchar				{ Lex->length=(char*) "1";
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2467
					  $$=FIELD_TYPE_STRING;
2468
					  Lex->charset=national_charset_info; }
2469
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
2470
					  Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2471 2472 2473
					  $$=FIELD_TYPE_STRING; }
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
					  $$=FIELD_TYPE_VAR_STRING; }
2474 2475
	| nvarchar '(' NUM ')'		{ Lex->length=$3.str;
					  $$=FIELD_TYPE_VAR_STRING;
2476
					  Lex->charset=national_charset_info; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2477
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
2478
					  Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2479
					  $$=FIELD_TYPE_VAR_STRING; }
2480
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2481 2482
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
2483 2484
	| TIMESTAMP
	  {
2485
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
2486 2487
	      $$=FIELD_TYPE_DATETIME;
	    else
2488
	      $$=FIELD_TYPE_TIMESTAMP;
2489
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2490 2491 2492
	| TIMESTAMP '(' NUM ')'		{ Lex->length=$3.str;
					  $$=FIELD_TYPE_TIMESTAMP; }
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
2493
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2494
					  $$=FIELD_TYPE_TINY_BLOB; }
2495
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2496
					  $$=FIELD_TYPE_BLOB; }
hf@deer.(none)'s avatar
hf@deer.(none) committed
2497 2498 2499 2500 2501 2502 2503
	| 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
2504 2505
			                             sym_group_geom.name,
	                                             sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
2506 2507 2508
					  YYABORT;
#endif
					}
2509
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2510
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
2511
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2512
					  $$=FIELD_TYPE_LONG_BLOB; }
2513
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2514
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
2515 2516
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
2517
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
2518 2519
	| 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
2520 2521 2522 2523
	| DECIMAL_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
	| NUMERIC_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
2524 2525
	| FIXED_SYM float_options field_options
					{ $$=FIELD_TYPE_DECIMAL;}
2526
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2527
	  {
2528 2529
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2530 2531
	    $$=FIELD_TYPE_ENUM;
	  }
2532
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2533
	  {
2534 2535
	    LEX *lex=Lex;
	    lex->interval=typelib(lex->interval_list);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2536
	    $$=FIELD_TYPE_SET;
2537
	  }
2538
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
2539 2540 2541 2542 2543 2544
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
2545
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2546

hf@deer.(none)'s avatar
hf@deer.(none) committed
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
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
2558 2559
char:
	CHAR_SYM {}
2560 2561 2562 2563 2564 2565
	;

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2567 2568 2569
varchar:
	char VARYING {}
	| VARCHAR {}
2570 2571 2572 2573
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2574
	| NVARCHAR_SYM {}
2575 2576 2577 2578
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2579 2580 2581 2582 2583 2584

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

real_type:
2588
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2589 2590
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
2591
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2592 2593 2594 2595 2596


float_options:
	/* empty */		{}
	| '(' NUM ')'		{ Lex->length=$2.str; }
2597
	| precision		{};
2598 2599 2600 2601 2602 2603

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

field_options:
	/* empty */		{}
2608
	| field_opt_list	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2609 2610 2611

field_opt_list:
	field_opt_list field_option {}
2612
	| field_option {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2613 2614

field_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2615
	SIGNED_SYM	{}
2616
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
2617
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2618 2619

opt_len:
2620 2621
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2622 2623 2624

opt_precision:
	/* empty */	{}
2625
	| precision	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2626 2627 2628

opt_attribute:
	/* empty */ {}
2629
	| opt_attribute_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2630 2631 2632

opt_attribute_list:
	opt_attribute_list attribute {}
2633
	| attribute;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2634 2635 2636 2637

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
	| NOT NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
2638 2639 2640
	| 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
2641
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
2642
	| SERIAL_SYM DEFAULT VALUE_SYM
2643 2644 2645
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
2646
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2647 2648 2649 2650 2651
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
2652
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2653 2654 2655 2656 2657
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
2658
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2659 2660 2661 2662 2663
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
2664
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2665
	  }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2666
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= &$2; }
2667
	| BINARY { Lex->type|= BINCMP_FLAG; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2668 2669
	| COLLATE_SYM collation_name
	  {
2670
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
	    {
	      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
2682

2683 2684 2685 2686 2687
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

2688 2689 2690 2691
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
2692

2693
charset_name:
2694
	ident_or_text
2695
	{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2696
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
2697
	  {
2698
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,$1.str);
2699 2700
	    YYABORT;
	  }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2701 2702 2703
	}
	| BINARY { $$= &my_charset_bin; }
	;
2704

2705 2706 2707
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2708

2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726

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

2727
collation_name:
2728
	ident_or_text
2729 2730 2731
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
2732
	    net_printf(YYTHD,ER_UNKNOWN_COLLATION,$1.str);
2733 2734 2735 2736
	    YYABORT;
	  }
	};

2737 2738
opt_collate:
	/* empty */	{ $$=NULL; }
2739
	| COLLATE_SYM collation_name_or_default { $$=$2; }
2740 2741
	;

2742 2743 2744 2745
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

2746 2747 2748 2749
opt_default:
	/* empty */	{}
	| DEFAULT	{};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2750
opt_binary:
2751
	/* empty */			{ Lex->charset=NULL; }
2752 2753
	| ASCII_SYM			{ Lex->charset=&my_charset_latin1; }
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
2754 2755
	| UNICODE_SYM
	{
2756
	  if (!(Lex->charset=get_charset_by_csname("ucs2",MY_CS_PRIMARY,MYF(0))))
2757 2758 2759 2760 2761
	  {
	    net_printf(YYTHD,ER_UNKNOWN_CHARACTER_SET,"ucs2");
	    YYABORT;
	  }
	}
2762
	| charset charset_name	{ Lex->charset=$2; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2763

2764 2765 2766
opt_primary:
	/* empty */
	| PRIMARY_SYM
2767
	;
2768

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2769
references:
2770 2771
	REFERENCES table_ident
	{
2772
	  LEX *lex=Lex;
2773 2774 2775 2776 2777 2778
	  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
2779
	};
2780

2781
opt_ref_list:
2782
	/* empty */ opt_on_delete {}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2783
	| '(' ref_list ')' opt_on_delete {};
2784 2785 2786

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2789 2790 2791

opt_on_delete:
	/* empty */ {}
2792
	| opt_on_delete_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2793 2794 2795

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
2796
	| opt_on_delete_item {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2797 2798

opt_on_delete_item:
2799 2800 2801 2802
	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
2803
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2804 2805

delete_option:
2806 2807 2808 2809
	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
2810
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2811 2812

key_type:
2813
	key_or_index			    { $$= Key::MULTIPLE; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
2814 2815
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
hf@deer.(none)'s avatar
hf@deer.(none) committed
2816 2817 2818 2819 2820
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
	    net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
2821
		       sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
2822 2823 2824
	    YYABORT;
#endif
	  };
2825 2826 2827

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
2828
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2829 2830 2831

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

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
2834
opt_key_or_index:
2835
	/* empty */ {}
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
2836
	| key_or_index
2837 2838
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2839 2840
keys_or_index:
	KEYS {}
2841
	| INDEX_SYM {}
2842
	| INDEXES {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2843

2844
opt_unique_or_fulltext:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2845 2846
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
serg@serg.mylan's avatar
serg@serg.mylan committed
2847
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
hf@deer.(none)'s avatar
hf@deer.(none) committed
2848 2849 2850 2851 2852 2853
	| 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
2854
	               sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
2855 2856 2857
	    YYABORT;
#endif
	  }
serg@serg.mylan's avatar
serg@serg.mylan committed
2858
        ;
2859 2860

key_alg:
2861
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
2862
	| USING opt_btree_or_rtree { $$= $2; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2863
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
2864 2865 2866

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
hf@deer.(none)'s avatar
hf@deer.(none) committed
2867 2868 2869 2870
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2871
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2872 2873 2874

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
2875
	| key_part order_dir		{ Lex->col_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2876 2877 2878

key_part:
	ident			{ $$=new key_part_spec($1.str); }
2879
	| ident '(' NUM ')'	{ $$=new key_part_spec($1.str,(uint) atoi($3.str)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2880 2881 2882

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

2885 2886 2887
opt_component:
	/* empty */	 { $$.str= 0; $$.length= 0; }
	| '.' ident	 { $$=$2; };
2888

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2889 2890
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
2891
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2892 2893 2894 2895 2896 2897 2898 2899

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
2900
	  THD *thd= YYTHD;
2901
	  LEX *lex= thd->lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2902 2903
	  lex->sql_command = SQLCOM_ALTER_TABLE;
	  lex->name=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2904 2905
	  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
2906 2907 2908 2909
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
2910
          lex->select_lex.init_order();
2911
	  lex->select_lex.db=lex->name=0;
2912
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2913
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
2914
	  lex->create_info.default_table_charset= NULL;
2915
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
2916
	  lex->alter_info.reset();          
2917 2918
	  lex->alter_info.is_simple= 1;
	  lex->alter_info.flags= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2919
	}
2920 2921
	alter_list
	{}
2922
	| ALTER DATABASE ident opt_create_database_options
2923 2924 2925 2926
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
	    lex->name=$3.str;
2927
	  }
2928
	| ALTER PROCEDURE sp_name
2929 2930 2931 2932
	  {
	    LEX *lex= Lex;

	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
2933
	    lex->name= 0;
2934 2935
          }
	  sp_a_chistics
2936
	  {
2937
	    THD *thd= YYTHD;
2938
	    LEX *lex=Lex;
2939

2940
	    lex->sql_command= SQLCOM_ALTER_PROCEDURE;
2941
	    lex->spname= $3;
2942
	  }
2943
	| ALTER FUNCTION_SYM sp_name
2944 2945 2946 2947 2948 2949 2950
	  {
	    LEX *lex= Lex;

	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	    lex->name= 0;
          }
	  sp_a_chistics
2951
	  {
2952
	    THD *thd= YYTHD;
2953
	    LEX *lex=Lex;
2954

2955
	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
2956
	    lex->spname= $3;
2957
	  }
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969
	| 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
	  {}
2970
	;
2971

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2972
alter_list:
2973 2974
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
2975
        | alter_list_item
2976
	| alter_list ',' alter_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2977 2978

add_column:
2979 2980 2981 2982
	ADD opt_column 
	{
	  LEX *lex=Lex;
	  lex->change=0; 
2983
	  lex->alter_info.flags|= ALTER_ADD_COLUMN; 
2984
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2985 2986

alter_list_item:
2987
	add_column column_def opt_place { Lex->alter_info.is_simple= 0; }
2988 2989 2990
	| ADD key_def 
	  { 
	    LEX *lex=Lex;
2991 2992
	    lex->alter_info.is_simple= 0; 
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2993
	  }
2994
	| add_column '(' field_list ')'      { Lex->alter_info.is_simple= 0; }
2995 2996 2997
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
2998
	     lex->change= $3.str; 
2999 3000
	     lex->alter_info.is_simple= 0;
	     lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
3001
	  }
3002
          field_spec opt_place
3003 3004 3005 3006
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
            lex->length=lex->dec=0; lex->type=0; lex->interval=0;
3007
            lex->default_value= lex->on_update_value= 0;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3008
	    lex->comment=0;
3009
	    lex->charset= NULL;
3010 3011
            lex->alter_info.is_simple= 0;
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
3012 3013 3014 3015
          }
          type opt_attribute
          {
            LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3016
            if (add_field_to_list(lex->thd,$3.str,
3017 3018
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
3019 3020
                                  lex->default_value, lex->on_update_value,
                                  lex->comment,
3021 3022
				  $3.str, lex->interval, lex->charset,
				  lex->uint_geom_type))
3023 3024 3025
	       YYABORT;
          }
          opt_place
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3026
	| DROP opt_column field_ident opt_restrict
3027 3028
	  {
	    LEX *lex=Lex;
3029 3030 3031 3032
	    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;
3033
	  }
3034
	| DROP FOREIGN KEY_SYM opt_ident { Lex->alter_info.is_simple= 0; }
3035 3036 3037
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
3038 3039 3040 3041
	    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;
3042
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3043
	| DROP key_or_index field_ident
3044 3045
	  {
	    LEX *lex=Lex;
3046 3047 3048 3049
	    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;
3050
	  }
3051 3052
	| DISABLE_SYM KEYS { Lex->alter_info.keys_onoff= DISABLE; }
	| ENABLE_SYM KEYS  { Lex->alter_info.keys_onoff= ENABLE; }
3053
	| ALTER opt_column field_ident SET DEFAULT signed_literal
3054 3055
	  {
	    LEX *lex=Lex;
3056 3057 3058
	    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;
3059
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3060
	| ALTER opt_column field_ident DROP DEFAULT
3061 3062
	  {
	    LEX *lex=Lex;
3063 3064 3065 3066
	    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;
3067
	  }
3068
	| RENAME opt_to table_ident
3069
	  {
3070
	    LEX *lex=Lex;
3071
	    lex->select_lex.db=$3->db.str;
3072
	    lex->name= $3->table.str;
3073
	    lex->alter_info.flags|= ALTER_RENAME;
3074
	  }
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
	| 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;
	    }
3089
	    LEX *lex= Lex;
monty@mysql.com's avatar
monty@mysql.com committed
3090 3091 3092 3093
	    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);
3094
	    lex->alter_info.is_simple= 0;
3095
	  }
3096 3097 3098
        | create_table_options_space_separated 
	  {
	    LEX *lex=Lex;
3099 3100
	    lex->alter_info.is_simple= 0; 
	    lex->alter_info.flags|= ALTER_OPTIONS;
3101 3102 3103 3104
	  }
	| order_clause         
	  {
	    LEX *lex=Lex;
3105 3106
	    lex->alter_info.is_simple= 0; 
	    lex->alter_info.flags|= ALTER_ORDER;
3107
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3108 3109 3110

opt_column:
	/* empty */	{}
3111
	| COLUMN_SYM	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3112 3113 3114

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

opt_restrict:
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
3118 3119 3120 3121
	/* 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
3122 3123 3124 3125

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
3126
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3127 3128 3129 3130

opt_to:
	/* empty */	{}
	| TO_SYM	{}
3131
	| EQ		{}
3132
	| AS		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3133

nick@mysql.com's avatar
nick@mysql.com committed
3134
/*
3135
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3136 3137
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3138
slave:
3139 3140 3141 3142 3143 3144 3145
	  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
3146
            /* If you change this code don't forget to update SLAVE START too */
3147 3148 3149
          }
          slave_until
          {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3150 3151 3152 3153 3154
        | 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
3155
            /* If you change this code don't forget to update SLAVE STOP too */
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3156
          }
3157 3158 3159 3160 3161
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
3162 3163 3164 3165 3166
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
3167 3168 3169 3170 3171 3172 3173 3174
	| 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
3175

3176 3177 3178 3179 3180
start:
	START_SYM TRANSACTION_SYM { Lex->sql_command = SQLCOM_BEGIN;}
	{}
	;

3181
slave_thread_opts:
3182 3183
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
3184
        {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3185
	;
3186 3187

slave_thread_opt_list:
3188
	slave_thread_opt
3189 3190
	| slave_thread_opt_list ',' slave_thread_opt
	;
3191 3192

slave_thread_opt:
3193
	/*empty*/	{}
3194
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
3195
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
3196
	;
3197

3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219
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 ;


3220 3221 3222 3223 3224
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
3225
	table_list FROM TEXT_STRING_sys
3226 3227
        {
	  Lex->backup_dir = $6.str;
3228
        };
3229

3230 3231 3232 3233 3234
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
3235
	table_list TO_SYM TEXT_STRING_sys
3236 3237
        {
	  Lex->backup_dir = $6.str;
3238
        };
3239

3240 3241 3242 3243 3244 3245
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
3246 3247
	table_list opt_checksum_type
        {}
3248 3249
	;

3250 3251 3252 3253 3254 3255
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
3256
repair:
3257
	REPAIR opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3258
	{
3259 3260
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
3261
           lex->no_write_to_binlog= $2;
3262
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3263
	}
3264 3265 3266
	table_list opt_mi_repair_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3267

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3268
opt_mi_repair_type:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3269
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
3270
	| mi_repair_types {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3271

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3272 3273
mi_repair_types:
	mi_repair_type {}
3274
	| mi_repair_type mi_repair_types {};
3275

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3276
mi_repair_type:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3277
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
3278
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
3279
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3280 3281

analyze:
3282
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3283
	{
3284 3285
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
3286
           lex->no_write_to_binlog= $2;
3287
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3288
	}
3289 3290 3291
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3292 3293

check:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3294
	CHECK_SYM table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3295
	{
3296 3297 3298
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECK;
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3299
	}
3300 3301 3302
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3303

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3304 3305
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
3306
	| mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3307 3308 3309

mi_check_types:
	mi_check_type {}
3310
	| mi_check_type mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3311 3312 3313 3314 3315 3316

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3319
optimize:
3320
	OPTIMIZE opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3321
	{
3322 3323
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3324
           lex->no_write_to_binlog= $2;
3325
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3326
	}
3327 3328 3329
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3330

3331 3332 3333
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
3334
	| LOCAL_SYM  { $$= 1; }
3335 3336
	;

3337 3338 3339 3340 3341
rename:
	RENAME table_or_tables
	{
	   Lex->sql_command=SQLCOM_RENAME_TABLE;
	}
3342 3343 3344
	table_to_table_list
	{}
	;
3345 3346 3347

table_to_table_list:
	table_to_table
3348
	| table_to_table_list ',' table_to_table;
3349 3350 3351

table_to_table:
	table_ident TO_SYM table_ident
3352
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3353
	  LEX *lex=Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3354
	  SELECT_LEX *sl= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3355 3356 3357 3358
	  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))
3359
	    YYABORT;
3360
	};
3361

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3362
keycache:
3363
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3364 3365
        {
          LEX *lex=Lex;
3366 3367
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
	  lex->name_and_length= $5;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3368 3369 3370 3371 3372 3373 3374 3375
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
3376
        table_ident cache_keys_spec
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3377 3378 3379 3380 3381 3382
        {
          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(),
3383
                                      (List<String> *)0))
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3384 3385 3386 3387
            YYABORT;
        }
        ;

3388 3389 3390
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
3391
	;
3392

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3393
preload:
3394
	LOAD INDEX_SYM INTO CACHE_SYM
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407
	{
	  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
3408
	table_ident cache_keys_spec opt_ignore_leaves
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3409 3410 3411
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3412 3413
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3414 3415 3416 3417 3418 3419
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

3420
cache_keys_spec:
3421
        { Select->interval_list.empty(); }
3422 3423 3424 3425 3426 3427 3428
        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
3429

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3430
cache_key_list_or_empty:
3431
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
3432
	| opt_key_or_index '(' key_usage_list2 ')'
3433 3434 3435 3436
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3437 3438
	;

3439
opt_ignore_leaves:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3440 3441 3442 3443 3444
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3445
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3446
  Select : retrieve data from table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3447 3448 3449 3450
*/


select:
3451 3452 3453 3454 3455
	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
3456 3457
	}
	;
3458

3459
/* Need select_init2 for subselects. */
3460
select_init:
3461
	SELECT_SYM select_init2
3462
	|
3463 3464
	'(' SELECT_SYM select_part2 ')'
	  {
3465
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3466
            SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3467
	    if (sel->set_braces(1))
3468
	    {
3469
	      yyerror(ER(ER_SYNTAX_ERROR));
3470 3471
	      YYABORT;
	    }
3472 3473
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3474
	  {
3475
	    yyerror(ER(ER_SYNTAX_ERROR));
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3476 3477
	    YYABORT;
	  }
3478
            /* select in braces, can't contain global parameters */
3479 3480 3481
	    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
3482
          } union_opt;
3483

3484 3485
select_init2:
	select_part2
3486
        {
3487
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3488
          SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3489
          if (lex->current_select->set_braces(0))
3490
	  {
3491
	    yyerror(ER(ER_SYNTAX_ERROR));
3492 3493
	    YYABORT;
	  }
3494 3495
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3496
	  {
3497
	    yyerror(ER(ER_SYNTAX_ERROR));
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3498 3499
	    YYABORT;
	  }
3500
	}
3501
	union_clause
3502 3503
	;

3504
select_part2:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3505
	{
3506 3507 3508
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
	  lex->lock_option= TL_READ;
3509 3510
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
3511 3512 3513 3514 3515
	  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
3516
	}
3517
	select_into select_lock_type;
3518

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3519
select_into:
3520 3521
	opt_limit_clause {}
        | into
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3522
	| select_from
3523 3524
	| into select_from
	| select_from into;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3525 3526

select_from:
3527 3528 3529 3530 3531 3532 3533
	  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
3534 3535 3536

select_options:
	/* empty*/
3537
	| select_option_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3538 3539 3540

select_option_list:
	select_option_list select_option
3541
	| select_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3542 3543

select_option:
3544
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
3545 3546 3547 3548 3549 3550
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
3551 3552 3553
	| DISTINCT	{ Select->options|= SELECT_DISTINCT; }
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565
	| 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;
	  }
3566
	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
3567 3568 3569 3570
	| SQL_CACHE_SYM
	  {
	    Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
	  }
3571 3572
	| ALL		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3573

monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
3574 3575 3576
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
3577 3578
	  {
	    LEX *lex=Lex;
3579
	    lex->current_select->set_lock_for_tables(TL_WRITE);
3580
	    lex->safe_to_cache_query=0;
3581
	  }
3582
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
3583 3584
	  {
	    LEX *lex=Lex;
3585 3586
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
3587
	    lex->safe_to_cache_query=0;
3588 3589
	  }
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
3590

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3591 3592 3593 3594 3595
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
3596 3597
	    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
3598
	      YYABORT;
pem@mysql.com's avatar
pem@mysql.com committed
3599
	    (thd->lex->current_select->with_wild)++;
3600
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3601 3602 3603 3604 3605


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3606
	    if (add_item_to_list(YYTHD, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3607 3608
	      YYABORT;
	    if ($4.str)
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3609
	      $2->set_name($4.str,$4.length,system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3610
	    else if (!$2->name)
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3611
	      $2->set_name($1,(uint) ($3 - $1), YYTHD->charset());
3612
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3613 3614

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

remember_end:
3618
	{ $$=(char*) Lex->tok_end; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3619 3620 3621

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

select_alias:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3625 3626 3627 3628 3629 3630
	/* 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
3631 3632 3633

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3636
/* all possible expressions */
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
3637 3638 3639 3640
expr:	
	expr_expr	{ $$= $1; }
	| simple_expr	{ $$= $1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3641

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653
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
3654 3655
/* expressions that begin with 'expr' */
expr_expr:
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
3656
	 expr IN_SYM '(' expr_list ')'
3657
	  { $4->push_front($1); $$= new Item_func_in(*$4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3658
	| expr NOT IN_SYM '(' expr_list ')'
3659
	  { $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
3660
        | expr IN_SYM in_subselect
3661
          { $$= new Item_in_subselect($1, $3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3662
	| expr NOT IN_SYM in_subselect
3663
          {
3664
            $$= new Item_func_not(new Item_in_subselect($1, $4));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3665
          }
3666
	| expr BETWEEN_SYM no_and_expr AND_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3667
	  { $$= new Item_func_between($1,$3,$5); }
3668
	| expr NOT BETWEEN_SYM no_and_expr AND_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3669
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
3670
	| expr OR_OR_CONCAT expr { $$= or_or_concat(YYTHD, $1,$3); }
3671
	| expr OR_SYM expr	{ $$= new Item_cond_or($1,$3); }
3672
        | expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
3673
	| expr AND_SYM expr	{ $$= new Item_cond_and($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3674 3675 3676 3677 3678 3679 3680 3681 3682
	| 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
3683
	| expr REGEXP expr { $$= new Item_func_regex($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3684 3685
	| 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
3686 3687 3688
	| 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
3689
	| expr comp_op expr %prec EQ	{ $$= (*$2)(0)->create($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3690 3691
	| expr comp_op all_or_any in_subselect %prec EQ
	{
3692
	  $$= all_any_subquery_creator($1, $2, $3, $4);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3693
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3694 3695 3696 3697 3698 3699
	| 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); }
3700 3701
	| 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
3702
	| expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
3703
        | expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3704 3705
	| expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| expr '%' expr		{ $$= new Item_func_mod($1,$3); }
3706 3707 3708 3709
	| 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
3710
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3711 3712 3713

/* expressions that begin with 'expr' that do NOT follow IN_SYM */
no_in_expr:
3714
	no_in_expr BETWEEN_SYM no_and_expr AND_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3715
	  { $$= new Item_func_between($1,$3,$5); }
3716
	| no_in_expr NOT BETWEEN_SYM no_and_expr AND_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3717
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
3718
	| no_in_expr OR_OR_CONCAT expr	{ $$= or_or_concat(YYTHD, $1,$3); }
3719
	| no_in_expr OR_SYM expr	{ $$= new Item_cond_or($1,$3); }
3720
        | no_in_expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
3721
	| 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
3722 3723 3724 3725 3726 3727 3728 3729 3730
	| 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
3731
	| no_in_expr REGEXP expr { $$= new Item_func_regex($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3732 3733
	| 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
3734 3735 3736
	| 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
3737
	| 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
3738 3739
	| no_in_expr comp_op all_or_any in_subselect %prec EQ
	{
3740
	  all_any_subquery_creator($1, $2, $3, $4);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3741
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3742 3743 3744 3745 3746 3747
	| 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); }
3748
	| no_in_expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3749
	| no_in_expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
3750
        | no_in_expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3751 3752
	| no_in_expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| no_in_expr '%' expr		{ $$= new Item_func_mod($1,$3); }
3753
	| no_in_expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
3754 3755 3756 3757
	| 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); }
3758
	| simple_expr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3759 3760 3761

/* 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
3762
	  no_and_expr IN_SYM '(' expr_list ')'
3763
	  { $4->push_front($1); $$= new Item_func_in(*$4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3764
	| no_and_expr NOT IN_SYM '(' expr_list ')'
3765
	  { $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
3766
        | no_and_expr IN_SYM in_subselect
3767
          { $$= new Item_in_subselect($1, $3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3768
	| no_and_expr NOT IN_SYM in_subselect
3769
          {
3770
            $$= new Item_func_not(new Item_in_subselect($1, $4));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3771
          }
3772
	| no_and_expr BETWEEN_SYM no_and_expr AND_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3773
	  { $$= new Item_func_between($1,$3,$5); }
3774
	| no_and_expr NOT BETWEEN_SYM no_and_expr AND_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3775
	  { $$= new Item_func_not(new Item_func_between($1,$4,$6)); }
3776
	| no_and_expr OR_OR_CONCAT expr	{ $$= or_or_concat(YYTHD, $1,$3); }
3777
	| no_and_expr OR_SYM expr	{ $$= new Item_cond_or($1,$3); }
3778
        | no_and_expr XOR expr		{ $$= new Item_cond_xor($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3779 3780 3781 3782 3783 3784 3785 3786 3787
	| 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
3788
	| no_and_expr REGEXP expr { $$= new Item_func_regex($1,$3); }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3789 3790
	| 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
3791 3792 3793
	| 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
3794
	| 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
3795 3796
	| no_and_expr comp_op all_or_any in_subselect %prec EQ
	{
3797
	  all_any_subquery_creator($1, $2, $3, $4);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3798
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3799 3800 3801 3802 3803 3804
	| 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); }
3805
	| no_and_expr DIV_SYM expr	{ $$= new Item_func_int_div($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3806
	| no_and_expr '|' expr		{ $$= new Item_func_bit_or($1,$3); }
3807
        | no_and_expr '^' expr		{ $$= new Item_func_bit_xor($1,$3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3808 3809
	| no_and_expr '&' expr		{ $$= new Item_func_bit_and($1,$3); }
	| no_and_expr '%' expr		{ $$= new Item_func_mod($1,$3); }
3810
	| no_and_expr MOD_SYM expr	{ $$= new Item_func_mod($1,$3); }
3811 3812 3813 3814
	| 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); }
3815
	| simple_expr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3816

3817
interval_expr:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3818
         INTERVAL_SYM expr { $$=$2; }
3819 3820
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3821 3822
simple_expr:
	simple_ident
3823
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3824
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3825 3826 3827
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
3828
                                                            YYTHD->charset()));
3829
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3830
	| literal
3831
	| param_marker
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3832
	| '@' ident_or_text SET_VAR expr
3833 3834
	  {
	    $$= new Item_func_set_user_var($2,$4);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
3835 3836 3837
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3838
	  }
3839
	| '@' ident_or_text
3840 3841
	  {
	    $$= new Item_func_get_user_var($2);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
3842 3843 3844
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3845
	  }
3846
	| '@' '@' opt_var_ident_type ident_or_text opt_component
3847
	  {
3848 3849 3850 3851 3852 3853

            if ($4.str && $5.str && check_reserved_words(&$4))
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
3854
	    if (!($$= get_system_var(YYTHD, (enum_var_type) $3, $4, $5)))
3855
	      YYABORT;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
3856
	    Lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3857
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3858
	| sum_expr
3859 3860
	| '+' expr %prec NEG	{ $$= $2; }
	| '-' expr %prec NEG    { $$= new Item_func_neg($2); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3861 3862 3863 3864
	| '~' 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; }
3865 3866 3867 3868 3869
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
3870
	| ROW_SYM '(' expr ',' expr_list ')'
3871
	  {
3872 3873
	    $5->push_front($3);
	    $$= new Item_row(*$5);
3874
	  }
3875
	| EXISTS exists_subselect { $$= $2; }
3876
	| singlerow_subselect   { $$= $1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3877
	| '{' ident expr '}'	{ $$= $3; }
3878
        | MATCH ident_list_arg AGAINST '(' expr fulltext_options ')'
3879
          { $2->push_front($5);
3880 3881
            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
3882
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3883
	| BINARY expr %prec NEG
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3884
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3885 3886
	    $$= 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
3887
	  }
3888
	| CAST_SYM '(' expr AS cast_type ')'
3889 3890
	  {
	    $$= create_func_cast($3, $5,
3891
				 Lex->length ? atoi(Lex->length) : -1,
3892
				 Lex->charset);
3893
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3894
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
3895
	  { $$= new Item_func_case(* $4, $2, $5 ); }
3896
	| CONVERT_SYM '(' expr ',' cast_type ')'
3897 3898 3899 3900 3901
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
				 Lex->charset);
	  }
3902 3903
	| 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
3904 3905
	| DEFAULT '(' simple_ident ')'
	  { $$= new Item_default_value($3); }
3906 3907
	| VALUES '(' simple_ident ')'
	  { $$= new Item_insert_value($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3908
	| FUNC_ARG0 '(' ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
3909 3910 3911 3912
	  {
	    if (!$1.symbol->create_func)
	    {
	      net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
3913
			 $1.symbol->group->name,
hf@deer.(none)'s avatar
hf@deer.(none) committed
3914 3915 3916 3917 3918
	                 $1.symbol->group->needed_define);
	      YYABORT;
	    }
	    $$= ((Item*(*)(void))($1.symbol->create_func))();
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3919
	| FUNC_ARG1 '(' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
3920 3921 3922 3923
	  {
	    if (!$1.symbol->create_func)
	    {
	      net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
3924
			 $1.symbol->group->name,
hf@deer.(none)'s avatar
hf@deer.(none) committed
3925 3926 3927 3928 3929
	                 $1.symbol->group->needed_define);
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3930
	| FUNC_ARG2 '(' expr ',' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
3931 3932 3933 3934
	  {
	    if (!$1.symbol->create_func)
	    {
	      net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
3935
			 $1.symbol->group->name,
hf@deer.(none)'s avatar
hf@deer.(none) committed
3936 3937 3938 3939 3940
	                 $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
3941
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
3942 3943 3944 3945
	  {
	    if (!$1.symbol->create_func)
	    {
	      net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
3946
			 $1.symbol->group->name,
hf@deer.(none)'s avatar
hf@deer.(none) committed
3947 3948 3949 3950 3951
	                 $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
3952 3953 3954 3955
	| 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); }
3956 3957
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3958 3959 3960 3961 3962 3963
	| 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); }
3964 3965
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3966 3967
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
3968 3969
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3970 3971 3972 3973 3974
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
	  { $$= new Item_func_concat_ws($3, *$5); }
	| CURDATE optional_braces
3975
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3976
	| CURTIME optional_braces
3977
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3978
	| CURTIME '(' expr ')'
3979
	  {
3980
	    $$= new Item_func_curtime_local($3);
3981
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3982
	  }
3983
	| CURRENT_USER optional_braces
3984
          { $$= create_func_current_user(); }
3985 3986 3987 3988
	| 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
3989
	| DATABASE '(' ')'
3990
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3991
	    $$= new Item_func_database();
3992
            Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3993
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3994
	| DATE_SYM '(' expr ')'
3995
	  { $$= new Item_date_typecast($3); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3996 3997
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3998
	| ELT_FUNC '(' expr ',' expr_list ')'
3999
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4000 4001
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4002 4003 4004
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
4005
	    Lex->uncacheable(UNCACHEABLE_RAND);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4006
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4007
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
4008
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4009
	  { $$= new Item_func_decode($3,$5.str); }
4010
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4011
	 { $$= new Item_func_encode($3,$5.str); }
4012
	| DES_DECRYPT_SYM '(' expr ')'
4013
        { $$= new Item_func_des_decrypt($3); }
4014
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
4015
        { $$= new Item_func_des_decrypt($3,$5); }
4016
	| DES_ENCRYPT_SYM '(' expr ')'
4017
        { $$= new Item_func_des_encrypt($3); }
4018
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
4019
        { $$= new Item_func_des_encrypt($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4020 4021 4022 4023 4024 4025
	| 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); }
4026 4027
	| FALSE_SYM
	  { $$= new Item_int((char*) "FALSE",0,1); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4028 4029 4030 4031 4032 4033
	| 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
4034
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4035 4036
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
4037
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4038 4039 4040 4041 4042 4043
	| geometry_function
	  {
#ifdef HAVE_SPATIAL
	    $$= $1;
#else
	    net_printf(Lex->thd, ER_FEATURE_DISABLED,
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4044
	               sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
4045 4046 4047
	    YYABORT;
#endif
	  }
4048
	| GET_FORMAT '(' date_time_type  ',' expr ')'
4049
	  { $$= new Item_func_get_format($3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4050 4051 4052 4053 4054 4055
	| 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); }
4056
	| interval_expr interval '+' expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4057
	  /* we cannot put interval before - */
4058 4059 4060 4061 4062
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
4063
              yyerror(ER(ER_SYNTAX_ERROR));
4064 4065 4066 4067
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4068 4069
	| LAST_INSERT_ID '(' ')'
	  {
4070
	    $$= new Item_func_last_insert_id();
4071
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4072 4073 4074
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
4075
	    $$= new Item_func_last_insert_id($3);
4076
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4077 4078 4079 4080 4081 4082 4083
	  }
	| 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); }
4084
	| GREATEST_SYM '(' expr ',' expr_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4085 4086 4087
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
4088
	| LOG_SYM '(' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4089
	  { $$= new Item_func_log($3); }
4090
	| LOG_SYM '(' expr ',' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4091
	  { $$= new Item_func_log($3, $5); }
4092
	| MASTER_POS_WAIT '(' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4093
	  {
4094
	    $$= new Item_master_pos_wait($3, $5);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4095
	    Lex->safe_to_cache_query=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4096
		  }
4097
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4098
	  {
4099
	    $$= new Item_master_pos_wait($3, $5, $7);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4100
	    Lex->safe_to_cache_query=0;
4101
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4102 4103
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4104 4105
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
4106 4107
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4108 4109 4110
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
	| NOW_SYM optional_braces
4111
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4112
	| NOW_SYM '(' expr ')'
4113
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4114
	| PASSWORD '(' expr ')'
4115
	  {
4116 4117
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
4118 4119 4120 4121
	      (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
4122 4123
	| POSITION_SYM '(' no_in_expr IN_SYM expr ')'
	  { $$ = new Item_func_locate($5,$3); }
4124 4125
	| QUARTER_SYM '(' expr ')'
	  { $$ = new Item_func_quarter($3); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4126
	| RAND '(' expr ')'
4127
	  { $$= new Item_func_rand($3); Lex->uncacheable(UNCACHEABLE_RAND);}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4128
	| RAND '(' ')'
4129
	  { $$= new Item_func_rand(); Lex->uncacheable(UNCACHEABLE_RAND);}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4130 4131 4132 4133 4134 4135 4136
	| 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); }
4137 4138 4139 4140 4141
	| 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
4142 4143 4144 4145
	| 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
4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157
	| 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
4158
	| TIME_SYM '(' expr ')'
4159 4160 4161
	  { $$= 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
4162
	| TIMESTAMP '(' expr ',' expr ')'
4163
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
4164 4165 4166 4167
	| 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
4168
	| TRIM '(' expr ')'
4169 4170
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4171
	  { $$= new Item_func_ltrim($6,$4); }
4172
	| TRIM '(' TRAILING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4173
	  { $$= new Item_func_rtrim($6,$4); }
4174
	| TRIM '(' BOTH expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4175
	  { $$= new Item_func_trim($6,$4); }
4176 4177 4178 4179 4180 4181
	| 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
4182 4183
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
4184 4185
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
4186 4187
	| TRUE_SYM
	  { $$= new Item_int((char*) "TRUE",1,1); }
4188
	| ident '.' ident '(' udf_expr_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4189
	  {
4190 4191
	    LEX *lex= Lex;
	    sp_name *name= new sp_name($1, $3);
4192

4193 4194 4195 4196
	    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
4197
	    else
4198 4199 4200 4201
	      $$= new Item_func_sp(name);
	  }
	| IDENT_sys '(' udf_expr_list ')'
          {
4202
#ifdef HAVE_DLOPEN
4203
            udf_func *udf;
4204

4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260
            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
4261
#endif /* HAVE_DLOPEN */
4262 4263 4264 4265 4266 4267 4268 4269
            {
              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);
4270
	    }
4271
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4272
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
4273
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4274 4275
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4276
	| UNIX_TIMESTAMP '(' ')'
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4277 4278
	  {
	    $$= new Item_func_unix_timestamp();
4279
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4280
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4281 4282 4283
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
4284
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
4285 4286 4287 4288 4289 4290
	| 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
4291
	| WEEK_SYM '(' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4292
	  {
4293
            $$= new Item_func_week($3,new Item_int((char*) "0",
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4294
				   YYTHD->variables.default_week_format,1));
4295
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4296 4297 4298 4299 4300 4301 4302 4303 4304
	| 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 ')'
4305
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4306
	    $$=new Item_func_benchmark($3,$5);
4307
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4308
	  }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4309
	| EXTRACT_SYM '(' interval FROM expr ')'
4310
	{ $$=new Item_extract( $3, $5); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4311

hf@deer.(none)'s avatar
hf@deer.(none) committed
4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322
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,
4323 4324
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4325 4326
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4327
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4328 4329
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
4330
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344
 	| 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,
4345
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4346 4347
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4348
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360
	| 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,
4361
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4362 4363 4364 4365 4366 4367 4368 4369 4370 4371
 	| 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)); }
	;

4372 4373 4374 4375 4376 4377
fulltext_options:
        /* nothing */                   { $$= FT_NL;  }
        | WITH QUERY_SYM EXPANSION_SYM  { $$= FT_NL | FT_EXPAND; }
        | IN_SYM BOOLEAN_SYM MODE_SYM   { $$= FT_BOOL; }
        ;

4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410
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
4411 4412 4413 4414 4415 4416 4417
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); }
4418 4419
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
4420
	| COUNT_SYM '(' opt_all '*' ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4421 4422 4423
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
4424
	| COUNT_SYM '(' DISTINCT
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4425
	  { Select->in_sum_expr++; }
4426
	   expr_list
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4427
	  { Select->in_sum_expr--; }
4428 4429
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4430 4431 4432 4433
	| 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); }
4434 4435 4436 4437 4438 4439 4440
/*
   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
4441 4442
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
4443 4444
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4445 4446
	| STD_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_std($3); }
4447 4448
	| VARIANCE_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_variance($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4449
	| SUM_SYM '(' in_sum_expr ')'
4450
	  { $$=new Item_sum_sum($3); }
4451 4452
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
monty@mysql.com's avatar
monty@mysql.com committed
4453 4454 4455 4456 4457
	| 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
4458
	  {
monty@mysql.com's avatar
monty@mysql.com committed
4459 4460 4461
	    Select->in_sum_expr--;
	    $$=new Item_func_group_concat($3,$5,Select->gorder_list,$7);
	    $5->empty();
4462 4463 4464 4465 4466 4467 4468
	  };

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

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

4472 4473

opt_gorder_clause:
4474 4475
	  /* empty */
	  {
4476
            Select->gorder_list = NULL;
4477 4478 4479
	  }
	| order_clause
          {
4480 4481 4482
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
4483
				     sizeof(st_sql_list));
4484
	    select->order_list.empty();
4485
	  };
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4486

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4487 4488

in_sum_expr:
4489
	opt_all
4490 4491 4492 4493
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
4494
	    yyerror(ER(ER_SYNTAX_ERROR));
4495
	    YYABORT;
4496 4497
	  }
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4498 4499
	expr
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4500
	  Select->in_sum_expr--;
4501
	  $$= $3;
4502
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4503

4504
cast_type:
4505
	BINARY opt_len		{ $$=ITEM_CAST_CHAR; Lex->charset= &my_charset_bin; }
4506 4507
	| CHAR_SYM opt_len opt_binary	{ $$=ITEM_CAST_CHAR; }
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; }
4508 4509 4510 4511 4512 4513 4514
	| 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; }
4515
	;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4516

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4517
expr_list:
4518
	{ Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4519
	expr_list2
4520
	{ $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4521 4522

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

4526 4527
ident_list_arg:
          ident_list          { $$= $1; }
4528
        | '(' ident_list ')'  { $$= $2; };
4529

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4530
ident_list:
4531
        { Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4532
        ident_list2
4533
        { $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4534 4535

ident_list2:
4536
        simple_ident { Select->expr_list.head()->push_back($1); }
4537
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4538 4539 4540

opt_expr:
	/* empty */      { $$= NULL; }
4541
	| expr           { $$= $1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4542 4543 4544

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

when_list:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4548
        { Select->when_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4549
	when_list2
4550
	{ $$= Select->when_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4551 4552

when_list2:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4553
	expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4554
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4555
	    SELECT_LEX *sel=Select;
4556 4557
	    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
4558
	}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4559
	| when_list2 WHEN_SYM expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4560
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4561
	    SELECT_LEX *sel=Select;
4562 4563
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
4564
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4565

igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4566 4567 4568 4569 4570 4571 4572 4573 4574 4575
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
4576
join_table_list:
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4577 4578 4579 4580 4581 4582 4583 4584 4585
        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
4586
	  { add_join_on($3,$5); $$=$3; }
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4587
	| table_ref normal_join table_ref
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4588
	  USING
4589
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4590
	    SELECT_LEX *sel= Select;
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4591
            sel->save_names_for_using_list($1, $3);
4592
	  }
4593 4594 4595
	  '(' using_list ')'
	  { add_join_on($3,$7); $$=$3; }

4596
	| table_ref LEFT opt_outer JOIN_SYM table_ref ON expr
4597
	  { add_join_on($5,$7); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4598
	| table_ref LEFT opt_outer JOIN_SYM table_factor
4599
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4600
	    SELECT_LEX *sel= Select;
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4601
            sel->save_names_for_using_list($1, $5);
4602
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4603
	  USING '(' using_list ')'
4604
	  { add_join_on($5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4605
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
4606
	  {
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4607 4608
	    add_join_natural($1,$6);
	    $6->outer_join|=JOIN_TYPE_LEFT;
4609 4610
	    $$=$6;
	  }
4611
	| table_ref RIGHT opt_outer JOIN_SYM table_ref ON expr
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4612 4613 4614 4615 4616 4617 4618
          { 
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
            add_join_on($$, $7);
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
4619
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4620
	    SELECT_LEX *sel= Select;
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4621
            sel->save_names_for_using_list($1, $5);
4622
	  }
4623
	  USING '(' using_list ')'
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4624 4625 4626 4627 4628 4629 4630
          { 
	    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
4631
	  {
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4632 4633 4634 4635
	    add_join_natural($6,$1);
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
4636
	  }
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4637 4638 4639
	| table_ref NATURAL JOIN_SYM table_factor
	  { add_join_natural($1,$4); $$=$4; };
        
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4640 4641

normal_join:
4642 4643 4644 4645
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4646

igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4647
table_factor:
4648
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4649
	  SELECT_LEX *sel= Select;
4650
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4651
	  sel->table_join_options= 0;
4652
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4653
        table_ident opt_table_alias opt_key_definition
4654
	{
4655
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4656
	  SELECT_LEX *sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4657
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4658
					   sel->get_table_join_options(),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4659
					   lex->lock_option,
4660 4661
					   sel->get_use_index(),
					   sel->get_ignore_index())))
4662
	    YYABORT;
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4663
          sel->add_joined_table($$); 
4664
	}
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677
        | '('
          { 
            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 '}'
4678
	  { add_join_on($7,$9); $7->outer_join|=JOIN_TYPE_LEFT; $$=$7; }
4679
        | '(' SELECT_SYM select_derived ')' opt_table_alias
4680 4681
	{
	  LEX *lex=Lex;
4682 4683 4684
	  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
4685
                add_table_to_list(lex->thd, new Table_ident(unit), $5, 0,
4686
				  TL_READ,(List<String> *)0,
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4687 4688
	                          (List<String> *)0)))

4689
	    YYABORT;
igor@rurik.mysql.com's avatar
af  
igor@rurik.mysql.com committed
4690
          lex->current_select->add_joined_table($$);           
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4691
	};
4692

4693
select_derived:
4694
        {
4695
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
4696
	  lex->derived_tables|= DERIVED_SUBQUERY;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4697 4698 4699
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
4700 4701
	  {
	    yyerror(ER(ER_SYNTAX_ERROR));
4702 4703
	    YYABORT;
	  }
4704
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
4705
              mysql_new_select(lex, 1))
4706 4707
	    YYABORT;
	  mysql_init_select(lex);
4708
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
4709 4710 4711 4712 4713
	  lex->current_select->parsing_place= SELECT_LEX_NODE::SELECT_LIST;
	}
        select_options select_item_list
	{
	  Select->parsing_place= SELECT_LEX_NODE::NO_MATTER;
4714
	}
4715
	opt_select_from union_opt
4716
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4717 4718 4719

opt_outer:
	/* empty */	{}
4720
	| OUTER		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4721 4722 4723 4724

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
4725
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4726
	    SELECT_LEX *sel= Select;
4727 4728 4729
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4730 4731
	| FORCE_SYM key_usage_list
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4732
	    SELECT_LEX *sel= Select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4733 4734 4735 4736
	    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
4737
	| IGNORE_SYM key_usage_list
4738
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4739
	    SELECT_LEX *sel= Select;
4740 4741
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
4742
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4743 4744

key_usage_list:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4745
	key_or_index { Select->interval_list.empty(); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4746
        '(' key_list_or_empty ')'
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4747
        { $$= &Select->interval_list; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4748 4749 4750 4751 4752 4753
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4754 4755 4756

key_usage_list2:
	key_usage_list2 ',' ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4757
        { Select->
monty@mysql.com's avatar
monty@mysql.com committed
4758
	    interval_list.push_back(new (&YYTHD->mem_root) String((const char*) $3.str, $3.length,
4759
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4760
	| ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4761
        { Select->
monty@mysql.com's avatar
monty@mysql.com committed
4762
	    interval_list.push_back(new (&YYTHD->mem_root) String((const char*) $1.str, $1.length,
4763
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4764
	| PRIMARY_SYM
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4765
        { Select->
monty@mysql.com's avatar
monty@mysql.com committed
4766
	    interval_list.push_back(new (&YYTHD->mem_root) String("PRIMARY", 7,
4767
				    system_charset_info)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4768 4769 4770

using_list:
	ident
4771
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4772
	    SELECT_LEX *sel= Select;
4773 4774 4775 4776
	    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
4777 4778 4779 4780
	      YYABORT;
	  }
	| using_list ',' ident
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4781
	    SELECT_LEX *sel= Select;
4782
	    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
4783
	      YYABORT;
4784
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4785 4786

interval:
4787 4788
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4789
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4790 4791
	| 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
4792
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4793 4794
	| 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
4795 4796
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4797
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
4798 4799 4800 4801 4802 4803 4804 4805
	| 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
4806 4807
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
4808
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4809
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
4810
	| YEAR_SYM		{ $$=INTERVAL_YEAR; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4811

4812
date_time_type:
4813 4814 4815
	DATE_SYM		{$$=MYSQL_TIMESTAMP_DATE;}
	| TIME_SYM		{$$=MYSQL_TIMESTAMP_TIME;}
	| DATETIME		{$$=MYSQL_TIMESTAMP_DATETIME;};
4816

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4817 4818 4819
table_alias:
	/* empty */
	| AS
4820
	| EQ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4821 4822 4823 4824

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

4827 4828 4829 4830
opt_all:
	/* empty */
	| ALL
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4831 4832

where_clause:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4833
	/* empty */  { Select->where= 0; }
4834 4835
	| WHERE expr
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4836
	    Select->where= $2;
4837 4838 4839
	    if ($2)
	      $2->top_level_item();
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4840
 	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4841 4842 4843

having_clause:
	/* empty */
4844 4845
	| HAVING
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4846
	    Select->parsing_place= SELECT_LEX_NODE::IN_HAVING;
4847 4848
          }
	  expr
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4849
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4850
	    SELECT_LEX *sel= Select;
4851 4852
	    sel->having= $3;
	    sel->parsing_place= SELECT_LEX_NODE::NO_MATTER;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4853 4854 4855
	    if ($3)
	      $3->top_level_item();
	  }
4856
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4857 4858

opt_escape:
4859 4860
	ESCAPE_SYM simple_expr { $$= $2; }
	| /* empty */
monty@mysql.com's avatar
Merge  
monty@mysql.com committed
4861 4862 4863 4864 4865
          {

            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
4866 4867
          }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4868 4869 4870


/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4871
   group by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4872 4873 4874 4875
*/

group_clause:
	/* empty */
4876
	| GROUP BY group_list olap_opt;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4877 4878

group_list:
4879
	group_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4880
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
4881
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4882
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4883

4884 4885
olap_opt:
	/* empty */ {}
4886
	| WITH CUBE_SYM
4887
          {
4888
	    LEX *lex=Lex;
4889 4890 4891 4892 4893 4894
	    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
4895
	    lex->current_select->olap= CUBE_TYPE;
4896
	    net_printf(lex->thd, ER_NOT_SUPPORTED_YET, "CUBE");
4897
	    YYABORT;	/* To be deleted in 5.1 */
4898
	  }
4899
	| WITH ROLLUP_SYM
4900
          {
4901 4902 4903 4904 4905 4906 4907
	    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
4908
	    lex->current_select->olap= ROLLUP_TYPE;
4909
	  }
4910
	;
4911

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4912
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4913
   Order by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4914 4915
*/

4916
opt_order_clause:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4917
	/* empty */
4918
	| order_clause;
4919 4920

order_clause:
4921 4922
	ORDER_SYM BY
        {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4923
	  LEX *lex=Lex;
4924
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4925
	      lex->current_select->olap !=
4926
	      UNSPECIFIED_OLAP_TYPE)
4927
	  {
4928
	    net_printf(lex->thd, ER_WRONG_USAGE,
4929 4930 4931 4932
		       "CUBE/ROLLUP",
		       "ORDER BY");
	    YYABORT;
	  }
4933
	} order_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4934 4935 4936

order_list:
	order_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4937
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4938
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4939
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4940 4941 4942

order_dir:
	/* empty */ { $$ =  1; }
4943
	| ASC  { $$ =1; }
4944
	| DESC { $$ =0; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4945 4946


4947 4948 4949
opt_limit_clause_init:
	/* empty */
	{
4950 4951
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
4952
          sel->offset_limit= 0L;
4953
          sel->select_limit= HA_POS_ERROR;
4954
	}
4955 4956 4957
	| limit_clause {}
	;

4958 4959 4960 4961 4962
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

4963
limit_clause:
4964
	LIMIT limit_options {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4965
	;
4966 4967 4968

limit_options:
	ULONG_NUM
4969
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4970
            SELECT_LEX *sel= Select;
4971 4972
            sel->select_limit= $1;
            sel->offset_limit= 0L;
4973
	    sel->explicit_limit= 1;
4974 4975 4976
	  }
	| ULONG_NUM ',' ULONG_NUM
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4977
	    SELECT_LEX *sel= Select;
4978 4979
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
4980
	    sel->explicit_limit= 1;
4981
	  }
4982
	| ULONG_NUM OFFSET_SYM ULONG_NUM
4983
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4984
	    SELECT_LEX *sel= Select;
4985 4986
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
4987
	    sel->explicit_limit= 1;
4988 4989
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4990 4991 4992 4993 4994


delete_limit_clause:
	/* empty */
	{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4995
	  LEX *lex=Lex;
4996
	  lex->current_select->select_limit= HA_POS_ERROR;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4997
	}
4998
	| LIMIT ulonglong_num
4999 5000 5001 5002 5003
	{
	  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
5004 5005

ULONG_NUM:
monty@mysql.com's avatar
monty@mysql.com committed
5006 5007 5008 5009 5010 5011
	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
5012

5013
ulonglong_num:
monty@mysql.com's avatar
monty@mysql.com committed
5014 5015 5016 5017 5018 5019
	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
5020 5021 5022 5023 5024 5025

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
5026 5027 5028 5029 5030 5031 5032
	    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
5033 5034 5035
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
5036
	    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
5037
	      YYABORT;
5038
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5039
	  }
5040
	  '(' procedure_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5041 5042 5043 5044


procedure_list:
	/* empty */ {}
5045
	| procedure_list2 {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5046 5047 5048

procedure_list2:
	procedure_list2 ',' procedure_item
5049
	| procedure_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5050 5051 5052 5053

procedure_item:
	  remember_name expr
	  {
5054 5055
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5056 5057
	      YYABORT;
	    if (!$2->name)
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5058
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1), YYTHD->charset());
5059 5060 5061 5062 5063 5064
	  }
          ;


select_var_list_init:
	   {
5065 5066
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
5067 5068
	        YYABORT;
	   }
5069
	   select_var_list
5070
	   {}
5071
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5072

Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5073
select_var_list:
5074 5075 5076 5077
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

5078 5079
select_var_ident:  
	   '@' ident_or_text
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5080
           {
5081
             LEX *lex=Lex;
5082
	     if (lex->result) 
5083
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($2,0,0,(enum_field_types)0));
5084
	     else
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5085 5086
	       YYABORT;
	   }
5087
           | ident_or_text
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5088
           {
5089
             LEX *lex=Lex;
5090
	     sp_pvar_t *t;
5091 5092

	     if (!lex->spcont || !(t=lex->spcont->find_pvar(&$1)))
5093
	     {
5094
	       net_printf(YYTHD, ER_SP_UNDECLARED_VAR, $1.str);
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5095
	       YYABORT;
5096
	     }
5097
	     if (! lex->result)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5098
	       YYABORT;
5099 5100
	     else
	     {
5101
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($1,1,t->offset,t->type));
5102 5103
	       t->isset= TRUE;
	     }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5104
	   }
5105
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5106

5107
into:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5108
        INTO OUTFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5109
	{
5110
	  LEX *lex=Lex;
5111 5112
	  if (!lex->describe)
	  {
5113
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5114 5115 5116 5117 5118
	    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
5119 5120
	}
	opt_field_term opt_line_term
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5121
	| INTO DUMPFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5122
	{
5123
	  LEX *lex=Lex;
5124 5125
	  if (!lex->describe)
	  {
5126
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5127 5128 5129 5130 5131
	    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
5132
	}
5133
        | INTO select_var_list_init
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5134
	{
5135
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5136 5137
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5138

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5139 5140 5141
/*
  DO statement
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5142

5143
do:	DO_SYM
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5144 5145 5146 5147 5148 5149
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
	  if (!(lex->insert_list = new List_item))
	    YYABORT;
	}
5150 5151 5152 5153
	values
	{}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5154
/*
5155
  Drop : delete tables or index or user
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5156 5157 5158
*/

drop:
serg@serg.mylan's avatar
serg@serg.mylan committed
5159
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5160
	{
5161 5162
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
5163 5164
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5165
	}
5166
	| DROP INDEX_SYM ident ON table_ident {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5167
	  {
5168 5169
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
5170 5171 5172
	     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
5173 5174
	     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
5175 5176 5177 5178
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
5179 5180 5181 5182
	    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
5183
	 }
5184
	| DROP FUNCTION_SYM if_exists sp_name opt_restrict
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5185
	  {
5186 5187
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
5188
	    lex->drop_if_exists= $3;
5189
	    lex->spname= $4;
5190
	  }
5191
	| DROP PROCEDURE if_exists sp_name opt_restrict
5192 5193 5194
	  {
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
5195
	    lex->drop_if_exists= $3;
5196
	    lex->spname= $4;
5197 5198 5199 5200 5201 5202
	  }
	| 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
5203
	  }
5204 5205
	  user_list
	  {}
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
5206 5207 5208 5209 5210 5211 5212
	| 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;
	  }
5213
	  ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5214 5215

table_list:
5216
	table_name
5217
	| table_list ',' table_name;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5218

5219
table_name:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5220
	table_ident
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5221 5222 5223 5224 5225
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5226 5227

if_exists:
5228
	/* empty */ { $$= 0; }
5229 5230
	| IF EXISTS { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5231

5232 5233 5234 5235
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5236 5237 5238 5239 5240
/*
** Insert : add new data to table
*/

insert:
5241 5242 5243 5244 5245 5246
	INSERT
	{
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_INSERT;
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
5247
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
5248
	} insert_lock_option
5249
	opt_ignore insert2
5250
	{
5251
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5252
	  Lex->current_select= &Lex->select_lex;
5253
	}
5254
	insert_field_spec opt_insert_update
5255
	{}
5256
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5257 5258

replace:
5259 5260
	REPLACE
	{
5261
	  LEX *lex=Lex;
5262 5263
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
5264
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
5265
	}
5266 5267
	replace_lock_option insert2
	{
5268
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5269
	  Lex->current_select= &Lex->select_lex;
5270 5271
	}
	insert_field_spec
5272
	{}
5273
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5274 5275

insert_lock_option:
5276 5277 5278 5279
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
5280
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5281 5282

replace_lock_option:
5283 5284
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5285 5286 5287

insert2:
	INTO insert_table {}
5288
	| insert_table {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5289 5290

insert_table:
5291
	table_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5292
	{
5293 5294 5295 5296
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
5297
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5298 5299

insert_field_spec:
serg@serg.mylan's avatar
serg@serg.mylan committed
5300 5301 5302
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5303 5304
	| SET
	  {
5305 5306 5307
	    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
5308 5309
	      YYABORT;
	   }
5310
	   ident_eq_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5311 5312 5313 5314

opt_field_spec:
	/* empty */	  { }
	| '(' fields ')'  { }
5315
	| '(' ')'	  { };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5316 5317 5318

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
5319
	| insert_ident		{ Lex->field_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5320 5321 5322

insert_values:
	VALUES	values_list  {}
5323
	| VALUE_SYM values_list  {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5324 5325
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
5326
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5327 5328 5329

values_list:
	values_list ','  no_braces
5330
	| no_braces;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5331 5332 5333 5334

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
5335
	ident_eq_value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5336 5337

ident_eq_value:
5338
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5339
	 {
5340 5341 5342
	  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
5343
	    YYABORT;
5344
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5345 5346

equal:	EQ		{}
5347 5348 5349 5350 5351 5352 5353
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5354 5355 5356 5357 5358 5359 5360 5361 5362

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
5363 5364
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5365
	    YYABORT;
5366
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5367 5368 5369

opt_values:
	/* empty */ {}
5370
	| values;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5371 5372

values:
5373
	values ','  expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5374 5375 5376 5377
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
5378 5379 5380 5381 5382 5383 5384 5385
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
5386
	expr	  { $$= $1;}
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
5387
	| DEFAULT {$$= new Item_default_value(); }
5388
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5389

5390 5391
opt_insert_update:
        /* empty */
5392
        | ON DUPLICATE_SYM
5393 5394 5395 5396 5397
          { /* for simplisity, let's forget about
               INSERT ... SELECT ... UPDATE
               for a moment */
	    if (Lex->sql_command != SQLCOM_INSERT)
            {
5398
	      yyerror(ER(ER_SYNTAX_ERROR));
5399 5400 5401
              YYABORT;
            }
          }
5402
          KEY_SYM UPDATE_SYM update_list
5403 5404
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5405 5406 5407
/* Update rows in a table */

update:
5408 5409
	UPDATE_SYM
	{
5410
	  LEX *lex= Lex;
5411
	  mysql_init_select(lex);
5412
          lex->sql_command= SQLCOM_UPDATE;
5413
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
5414
        }
5415 5416 5417
        opt_low_priority opt_ignore join_table_list
	SET update_list where_clause opt_order_clause delete_limit_clause
	{
5418
	  LEX *lex= Lex;
5419
	  Select->set_lock_for_tables($3);
5420
          if (lex->select_lex.table_list.elements > 1)
5421
            lex->sql_command= SQLCOM_UPDATE_MULTI;
5422 5423 5424 5425 5426 5427 5428
	  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;
	  }
5429 5430
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5431 5432

update_list:
5433
	update_list ',' simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5434
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5435
	  if (add_item_to_list(YYTHD, $3) || add_value_to_list(YYTHD, $5))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5436 5437
	    YYABORT;
	}
5438
	| simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5439
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5440
	    if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5441
	      YYABORT;
5442
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5443 5444

opt_low_priority:
5445
	/* empty */	{ $$= YYTHD->update_lock_default; }
5446
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5447 5448 5449 5450

/* Delete rows from a table */

delete:
5451
	DELETE_SYM
5452
	{
5453 5454
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
5455
	  lex->lock_option= lex->thd->update_lock_default;
5456
	  lex->select_lex.init_order();
5457
	}
5458 5459
	opt_delete_options single_multi {}
	;
5460 5461

single_multi:
5462 5463
 	FROM table_ident
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5464 5465
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
5466 5467 5468
	    YYABORT;
	}
	where_clause opt_order_clause
5469
	delete_limit_clause {}
5470
	| table_wild_list
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5471
	  { mysql_init_multi_delete(Lex); }
5472
          FROM join_table_list where_clause
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5473 5474
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
5475 5476 5477
	  USING join_table_list where_clause
	  {}
	;
5478 5479 5480

table_wild_list:
	  table_wild_one {}
5481
	  | table_wild_list ',' table_wild_one {};
5482 5483

table_wild_one:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5484
	ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5485
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5486 5487
	  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
5488 5489
	    YYABORT;
        }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5490
	| ident '.' ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5491
	  {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5492 5493
	    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
5494 5495
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
5496
	      YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5497
	  }
5498
	;
5499 5500

opt_wild:
5501
	/* empty */	{}
5502
	| '.' '*'	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5503 5504


5505
opt_delete_options:
5506
	/* empty */	{}
5507
	| opt_delete_option opt_delete_options {};
5508 5509

opt_delete_option:
5510
	QUICK		{ Select->options|= OPTION_QUICK; }
5511 5512
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
	| IGNORE_SYM	{ Lex->duplicates= DUP_IGNORE; };
5513

5514
truncate:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5515
	TRUNCATE_SYM opt_table_sym table_name
5516
	{
5517
	  LEX* lex= Lex;
5518
	  lex->sql_command= SQLCOM_TRUNCATE;
5519
	  lex->select_lex.options= 0;
5520
	  lex->select_lex.init_order();
5521 5522
	}
	;
5523

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5524 5525
opt_table_sym:
	/* empty */
5526
	| TABLE_SYM;
5527

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5528 5529
/* Show things */

5530 5531
show:	SHOW
	{
5532 5533 5534 5535
	  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
5536
	show_param
5537 5538
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5539 5540 5541 5542 5543

show_param:
	DATABASES wild
	  { Lex->sql_command= SQLCOM_SHOW_DATABASES; }
	| TABLES opt_db wild
5544
	  {
5545
	    LEX *lex= Lex;
5546
	    lex->sql_command= SQLCOM_SHOW_TABLES;
5547
	    lex->select_lex.db= $2;
5548
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5549
	| TABLE_SYM STATUS_SYM opt_db wild
5550
	  {
5551
	    LEX *lex= Lex;
5552
	    lex->sql_command= SQLCOM_SHOW_TABLES;
5553
	    lex->describe= DESCRIBE_EXTENDED;
5554
	    lex->select_lex.db= $3;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5555
	  }
5556
	| OPEN_SYM TABLES opt_db wild
5557
	  {
5558
	    LEX *lex= Lex;
5559
	    lex->sql_command= SQLCOM_SHOW_OPEN_TABLES;
5560
	    lex->select_lex.db= $3;
5561
	  }
5562
	| ENGINE_SYM storage_engines 
5563 5564
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
5565
	| opt_full COLUMNS from_or_in table_ident opt_db wild
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5566 5567
	  {
	    Lex->sql_command= SQLCOM_SHOW_FIELDS;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5568 5569
	    if ($5)
	      $4->change_db($5);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5570
	    if (!Select->add_table_to_list(YYTHD, $4, NULL, 0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5571 5572
	      YYABORT;
	  }
5573
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
5574 5575
	  TEXT_STRING_sys AND_SYM MASTER_LOG_POS_SYM EQ ulonglong_num
	  AND_SYM MASTER_SERVER_ID_SYM EQ
5576 5577 5578 5579 5580
	ULONG_NUM
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
5581
	    Lex->mi.server_id = $16;
5582
          }
5583
        | master_or_binary LOGS_SYM
5584 5585
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
5586 5587 5588 5589 5590
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
5591
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
5592
          {
5593 5594
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
5595
          } opt_limit_clause_init
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5596 5597 5598 5599 5600
	| 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
5601
	    if (!Select->add_table_to_list(YYTHD, $3, NULL, 0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5602 5603
	      YYABORT;
	  }
5604 5605 5606 5607 5608 5609 5610 5611
	| COLUMN_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_COLUMN_TYPES;
	  }
	| TABLE_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
5612 5613 5614 5615 5616 5617 5618
	    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;
5619 5620 5621 5622 5623 5624
	  }
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
5625
        | COUNT_SYM '(' '*' ')' WARNINGS
5626
          { (void) create_select_for_variable("warning_count"); }
5627
        | COUNT_SYM '(' '*' ')' ERRORS
5628
	  { (void) create_select_for_variable("error_count"); }
5629
        | WARNINGS opt_limit_clause_init
5630
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
5631
        | ERRORS opt_limit_clause_init
5632
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5633 5634
	| STATUS_SYM wild
	  { Lex->sql_command= SQLCOM_SHOW_STATUS; }
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
5635
        | INNOBASE_SYM STATUS_SYM
5636
          { 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
5637 5638
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
5639
	| opt_var_type VARIABLES wild
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5640
	  {
5641
	    THD *thd= YYTHD;
5642 5643
	    thd->lex->sql_command= SQLCOM_SHOW_VARIABLES;
	    thd->lex->option_type= (enum_var_type) $1;
5644
	  }
5645
	| charset wild
5646
	  { Lex->sql_command= SQLCOM_SHOW_CHARSETS; }
5647 5648
	| COLLATION_SYM wild
	  { Lex->sql_command= SQLCOM_SHOW_COLLATIONS; }
5649
	| BERKELEY_DB_SYM LOGS_SYM
5650
	  { 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
5651
	| LOGS_SYM
5652
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW LOGS", "SHOW ENGINE BDB LOGS"); }
5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675
	| 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
5676
	| GRANTS FOR_SYM user
5677 5678 5679 5680 5681 5682
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
	    lex->grant_user->password.str=NullS;
	  }
5683
	| CREATE DATABASE opt_if_not_exists ident
5684 5685
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
5686 5687
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
5688
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5689 5690
        | CREATE TABLE_SYM table_ident
          {
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701
            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
5702
	      YYABORT;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
5703
            lex->only_view= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5704 5705 5706 5707
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5708
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5709 5710 5711
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
5712
          }
5713
	| CREATE PROCEDURE sp_name
5714
	  {
5715 5716 5717 5718
	    LEX *lex= Lex;

	    lex->sql_command = SQLCOM_SHOW_CREATE_PROC;
	    lex->spname= $3;
5719
	  }
5720
	| CREATE FUNCTION_SYM sp_name
5721
	  {
5722 5723 5724 5725
	    LEX *lex= Lex;

	    lex->sql_command = SQLCOM_SHOW_CREATE_FUNC;
	    lex->spname= $3;
5726 5727 5728 5729 5730 5731 5732 5733 5734 5735
	  }
	| PROCEDURE STATUS_SYM wild
	  {
	    Lex->sql_command = SQLCOM_SHOW_STATUS_PROC;
	  }
	| FUNCTION_SYM STATUS_SYM wild
	  {
	    Lex->sql_command = SQLCOM_SHOW_STATUS_FUNC;
	  };

5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758
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
5759

5760 5761 5762 5763
master_or_binary:
	MASTER_SYM
	| BINARY;

5764 5765 5766 5767
opt_storage:
	/* empty */
	| STORAGE_SYM;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5768 5769
opt_db:
	/* empty */  { $$= 0; }
5770
	| from_or_in ident { $$= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5771 5772 5773

wild:
	/* empty */
5774 5775 5776
	| 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
5777

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5778 5779
opt_full:
	/* empty */ { Lex->verbose=0; }
5780
	| FULL	    { Lex->verbose=1; };
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5781

5782 5783
from_or_in:
	FROM
5784
	| IN_SYM;
5785

5786 5787
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5788
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
5789 5790 5791

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


bk@work.mysql.com's avatar
bk@work.mysql.com committed
5795 5796 5797 5798
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
5799 5800 5801 5802
	  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
5803
	  if (!Select->add_table_to_list(lex->thd, $2, NULL,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5804 5805
	    YYABORT;
	}
5806
	opt_describe_column {}
5807 5808 5809
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
5810
          {
5811
	    LEX *lex=Lex;
5812
	    lex->select_lex.options|= SELECT_DESCRIBE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5813 5814
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5815 5816 5817

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

5820 5821 5822 5823 5824
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5825 5826 5827
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
5828
	| ident
monty@mysql.com's avatar
monty@mysql.com committed
5829
	  { 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
5830 5831 5832 5833 5834


/* flush things */

flush:
5835
	FLUSH_SYM opt_no_write_to_binlog
5836 5837 5838
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5839
          lex->no_write_to_binlog= $2;
5840
	}
5841 5842 5843
	flush_options
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5844 5845 5846

flush_options:
	flush_options ',' flush_option
5847
	| flush_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5848 5849

flush_option:
5850
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5851
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5852
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5853 5854 5855 5856
	| 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
5857 5858
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
5859 5860
	| 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
5861

5862
opt_table_list:
5863 5864
	/* empty */  {;}
	| table_list {;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5865

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5866
reset:
5867 5868 5869 5870
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
5871 5872 5873 5874
	} reset_options
	{}
	;

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5875 5876
reset_options:
	reset_options ',' reset_option
5877
	| reset_option;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5878 5879

reset_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5880 5881
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
5882
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5883

5884
purge:
5885 5886 5887 5888
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
5889 5890 5891 5892 5893
	} purge_options
	{}
	;

purge_options:
5894
	master_or_binary LOGS_SYM purge_option
5895
	;
5896 5897

purge_option:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5898
        TO_SYM TEXT_STRING_sys
5899 5900 5901 5902 5903 5904 5905 5906 5907
        {
	   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
5908
	    YYABORT;
5909 5910
	  }
	  Item *tmp= new Item_func_unix_timestamp($2);
5911
	  /*
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5912
	    it is OK only emulate fix_fieds, because we need only
5913 5914
            value of constant
	  */
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5915
	  tmp->quick_fix_field();
5916
	  Lex->sql_command = SQLCOM_PURGE_BEFORE;
5917
	  Lex->purge_time= (ulong) tmp->val_int();
5918
	}
5919
	;
5920

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5921 5922 5923
/* kill threads */

kill:
hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
5924
	KILL_SYM kill_option expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5925
	{
5926
	  LEX *lex=Lex;
hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
5927
	  if ($3->fix_fields(lex->thd, 0, &$3) || $3->check_cols(1))
5928
	  {
5929
	    send_error(lex->thd, ER_SET_CONSTANTS_ONLY);
5930 5931 5932
	    YYABORT;
	  }
          lex->sql_command=SQLCOM_KILL;
hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
5933
	  lex->thread_id= (ulong) $3->val_int();
5934
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5935

hf@genie.(none)'s avatar
SCRUM  
hf@genie.(none) committed
5936 5937 5938 5939 5940
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
5941 5942 5943
/* change database */

use:	USE_SYM ident
5944 5945
	{
	  LEX *lex=Lex;
5946 5947
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
5948
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5949 5950 5951

/* import, export of files */

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5952
load:	LOAD DATA_SYM load_data_lock opt_local INFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5953
	{
5954 5955
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
5956 5957
	  lex->lock_option= $3;
	  lex->local_file=  $4;
5958
	  if (!(lex->exchange= new sql_exchange($6.str,0)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5959
	    YYABORT;
5960
	  lex->field_list.empty();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5961 5962 5963 5964
	}
	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
5965
	  if (!Select->add_table_to_list(YYTHD, $11, NULL, TL_OPTION_UPDATING))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5966 5967 5968
	    YYABORT;
	}
        |
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5969
	LOAD TABLE_SYM table_ident FROM MASTER_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5970 5971
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5972
	  if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5973
	    YYABORT;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5974 5975

        }
5976 5977 5978 5979
        |
	LOAD DATA_SYM FROM MASTER_SYM
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
5980
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5981 5982 5983

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

5986
load_data_lock:
5987
	/* empty */	{ $$= YYTHD->update_lock_default; }
5988 5989
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
5990 5991


bk@work.mysql.com's avatar
bk@work.mysql.com committed
5992 5993 5994
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
5995
	| IGNORE_SYM	{ Lex->duplicates=DUP_IGNORE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5996 5997 5998

opt_field_term:
	/* empty */
5999
	| COLUMNS field_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6000 6001 6002

field_term_list:
	field_term_list field_term
6003
	| field_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6004 6005 6006 6007

field_term:
	TERMINATED BY text_string { Lex->exchange->field_term= $3;}
	| OPTIONALLY ENCLOSED BY text_string
6008 6009 6010 6011 6012
	  {
	    LEX *lex=Lex;
	    lex->exchange->enclosed= $4;
	    lex->exchange->opt_enclosed=1;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6013
	| ENCLOSED BY text_string { Lex->exchange->enclosed= $3;}
6014
	| ESCAPED BY text_string  { Lex->exchange->escaped= $3;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6015 6016 6017

opt_line_term:
	/* empty */
6018
	| LINES line_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6019 6020 6021

line_term_list:
	line_term_list line_term
6022
	| line_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6023 6024 6025

line_term:
	TERMINATED BY text_string { Lex->exchange->line_term= $3;}
6026
	| STARTING BY text_string { Lex->exchange->line_start= $3;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6027 6028 6029 6030

opt_ignore_lines:
	/* empty */
	| IGNORE_SYM NUM LINES
6031
	  { Lex->exchange->skip_lines=atol($2.str); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6032 6033 6034 6035

/* Common definitions */

text_literal:
6036
	TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6037 6038
	{
	  THD *thd= YYTHD;
6039
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6040
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6041
	| NCHAR_STRING
6042
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
6043
	| UNDERSCORE_CHARSET TEXT_STRING
6044
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
6045
	| text_literal TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6046 6047
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6048 6049

text_string:
6050
	TEXT_STRING_literal
monty@mysql.com's avatar
monty@mysql.com committed
6051
	{ $$=  new (&YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6052 6053
	| HEX_NUM
	  {
6054
	    Item *tmp = new Item_varbinary($1.str,$1.length);
6055
	    /*
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6056
	      it is OK only emulate fix_fieds, because we need only
6057 6058 6059
              value of constant
	    */
	    $$= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6060
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6061
	      (String*) 0;
6062 6063 6064
	  }
	;

6065
param_marker:
6066
        '?'
6067
        {
6068 6069 6070
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
          if (thd->command == COM_PREPARE)
6071
          {
6072 6073 6074 6075 6076 6077 6078
            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;
            }
6079
          }
6080
          else
6081
          {
6082
            yyerror(ER(ER_SYNTAX_ERROR));
6083 6084
            YYABORT;
          }
6085 6086 6087
        }
	;

6088 6089 6090
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
6091 6092
	| '-' NUM_literal
	  {
6093
	    $2->max_length++;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6094
	    $$= $2->neg();
6095
	  }
6096 6097 6098
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6099 6100
literal:
	text_literal	{ $$ =	$1; }
6101
	| NUM_literal	{ $$ = $1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6102
	| NULL_SYM	{ $$ =	new Item_null();
6103
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
6104
	| HEX_NUM	{ $$ =	new Item_varbinary($1.str,$1.length);}
6105
	| UNDERSCORE_CHARSET HEX_NUM
6106
	  {
6107
	    Item *tmp= new Item_varbinary($2.str,$2.length);
6108
	    /*
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6109
	      it is OK only emulate fix_fieds, because we need only
6110 6111 6112
              value of constant
	    */
	    String *str= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6113
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6114
	      (String*) 0;
6115 6116 6117
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
6118
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6119 6120
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
6121
	| TIMESTAMP text_literal { $$ = $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6122

6123
NUM_literal:
6124 6125
	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); }
6126 6127 6128 6129 6130
	| 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
6131 6132 6133 6134 6135
/**********************************************************************
** Createing different items.
**********************************************************************/

insert_ident:
6136
	simple_ident_nospvar { $$=$1; }
6137
	| table_wild	 { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6138 6139

table_wild:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6140
	ident '.' '*'
6141 6142
	{
	  $$ = new Item_field(NullS,$1.str,"*");
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6143
	  Lex->current_select->with_wild++;
6144
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6145
	| ident '.' ident '.' '*'
6146 6147 6148 6149
	{
	  $$ = 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
6150
	  Lex->current_select->with_wild++;
6151 6152
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6153 6154

order_ident:
6155
	expr { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6156 6157 6158

simple_ident:
	ident
6159
	{
6160 6161 6162 6163 6164 6165 6166 6167
	  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)
	    {
6168 6169 6170
	      push_warning_printf(YYTHD, MYSQL_ERROR::WARN_LEVEL_WARN,
	                          ER_SP_UNINIT_VAR, ER(ER_SP_UNINIT_VAR),
				  $1.str);
6171
	    }
6172
	    $$ = (Item*) new Item_splocal($1, spv->offset);
6173 6174 6175
	  }
	  else
	  {
pem@mysql.com's avatar
pem@mysql.com committed
6176
	    SELECT_LEX *sel=Select;
6177 6178 6179
	    $$= (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
6180
	         (Item*) new Item_ref(0,0, NullS,NullS,$1.str);
6181
	  }
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
6182
        }
6183 6184 6185 6186 6187 6188 6189 6190 6191 6192
        | 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
6193
	      (Item*) new Item_ref(0,0,NullS,NullS,$1.str);
6194 6195 6196 6197 6198 6199
	}
	| simple_ident_q { $$= $1; }
	;	

simple_ident_q:
	ident '.' ident
6200
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6201
	  THD *thd= YYTHD;
6202
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6203
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6204 6205
	  if (sel->no_table_names_allowed)
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6206
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6207 6208 6209
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $1.str, thd->where);
	  }
6210 6211 6212
	  $$= (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
6213
	      (Item*) new Item_ref(0,0,NullS,$1.str,$3.str);
6214
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6215
	| '.' ident '.' ident
6216
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6217
	  THD *thd= YYTHD;
6218
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6219
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6220 6221
	  if (sel->no_table_names_allowed)
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6222
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6223 6224 6225
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $2.str, thd->where);
	  }
6226 6227 6228
	  $$= (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
6229
              (Item*) new Item_ref(0,0,NullS,$2.str,$4.str);
6230
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6231
	| ident '.' ident '.' ident
6232
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6233
	  THD *thd= YYTHD;
6234
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6235
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6236 6237
	  if (sel->no_table_names_allowed)
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6238
	    my_printf_error(ER_TABLENAME_NOT_ALLOWED_HERE,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6239 6240 6241
			    ER(ER_TABLENAME_NOT_ALLOWED_HERE),
			    MYF(0), $3.str, thd->where);
	  }
6242 6243 6244 6245 6246
	  $$= (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
6247 6248
	      (Item*) new Item_ref(0,0,(YYTHD->client_capabilities &
				        CLIENT_NO_SCHEMA ? NullS : $1.str),
6249
                                   $3.str, $5.str);
6250
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6251 6252 6253 6254


field_ident:
	ident			{ $$=$1;}
6255
	| ident '.' ident	{ $$=$3;}	/* Skip schema name in create*/
6256
	| '.' ident		{ $$=$2;}	/* For Delphi */;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6257 6258 6259

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

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

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6268
IDENT_sys:
6269 6270 6271 6272 6273
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284
            {
              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;
              }
6285
	      $$= $1;
6286
            }
6287 6288 6289 6290
	    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
6291 6292 6293 6294 6295 6296
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6297 6298
	  if (thd->charset_is_system_charset)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6299
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6300 6301
	    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
6302 6303 6304
	}
	;

6305
TEXT_STRING_literal:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6306 6307 6308
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6309 6310
	  if (thd->charset_is_collation_connection)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6311
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6312 6313
	    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
6314 6315 6316 6317
	}
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6318
ident:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6319
	IDENT_sys	    { $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6320 6321
	| keyword
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6322 6323 6324
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
6325 6326
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6327 6328

ident_or_text:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6329 6330 6331
	ident 			{ $$=$1;}
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6332 6333 6334 6335

user:
	ident_or_text
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6336 6337
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6338
	    YYABORT;
6339 6340 6341 6342
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6343 6344
	| ident_or_text '@' ident_or_text
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6345 6346
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6347 6348
	      YYABORT;
	    $$->user = $1; $$->host=$3;
6349
	  }
6350
	| CURRENT_USER optional_braces
6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367
	{
          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
6368 6369 6370 6371 6372

/* Keyword that we allow for identifiers */

keyword:
	ACTION			{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6373
	| ADDDATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6374
	| AFTER_SYM		{}
6375
	| AGAINST		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6376
	| AGGREGATE_SYM		{}
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
6377
	| ALGORITHM_SYM		{}
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6378
	| ANY_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6379
	| ASCII_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6380
	| AUTO_INC		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6381 6382
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
6383
	| BACKUP_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6384
	| BEGIN_SYM		{}
6385
	| BERKELEY_DB_SYM	{}
6386
	| BINLOG_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6387 6388
	| BIT_SYM		{}
	| BOOL_SYM		{}
6389
	| BOOLEAN_SYM		{}
6390
	| BYTE_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6391
	| BTREE_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6392
	| CACHE_SYM		{}
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
6393
	| CASCADED              {}
6394
	| CHANGED		{}
6395
	| CHARSET		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6396
	| CHECKSUM_SYM		{}
6397
	| CIPHER_SYM		{}
6398
	| CLIENT_SYM		{}
6399
	| CLOSE_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6400
	| COLLATION_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6401
	| COMMENT_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6402
	| COMMITTED_SYM		{}
6403
	| COMMIT_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6404
	| COMPRESSED_SYM	{}
6405
	| CONCURRENT		{}
6406
	| CUBE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6407 6408 6409 6410
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
sergefp@mysql.com's avatar
sergefp@mysql.com committed
6411
        | DEALLOCATE_SYM        {}
6412
	| DEFINER_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6413
	| DELAY_KEY_WRITE_SYM	{}
6414 6415
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
6416
	| DISCARD		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6417
	| DO_SYM		{}
6418 6419
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6420 6421 6422
	| DYNAMIC_SYM		{}
	| END			{}
	| ENUM			{}
6423
	| ENGINE_SYM		{}
6424
	| ENGINES_SYM		{}
6425
	| ERRORS		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6426
	| ESCAPE_SYM		{}
6427
	| EVENTS_SYM		{}
6428
	| EXECUTE_SYM		{}
6429
        | EXPANSION_SYM         {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6430
	| EXTENDED_SYM		{}
6431
	| FAST_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6432 6433
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6434
	| FULL			{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6435 6436 6437 6438
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
	| FLUSH_SYM		{}
6439
	| GEOMETRY_SYM		{}
6440
	| GEOMETRYCOLLECTION	{}
6441
	| GET_FORMAT		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6442
	| GRANTS		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6443
	| GLOBAL_SYM		{}
6444
	| HANDLER_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6445
	| HASH_SYM		{}
6446
	| HELP_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6447 6448 6449
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
6450
	| INVOKER_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
6451
	| IMPORT		{}
6452
	| INDEXES		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6453
	| ISOLATION		{}
6454
	| ISSUER_SYM		{}
6455
	| INNOBASE_SYM		{}
6456
	| INSERT_METHOD		{}
6457
	| RELAY_THREAD		{}
6458
	| LANGUAGE_SYM          {}
6459
	| LAST_SYM		{}
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6460
	| LEAVES                {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6461
	| LEVEL_SYM		{}
6462
	| LINESTRING		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6463
	| LOCAL_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6464
	| LOCKS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6465 6466 6467 6468 6469 6470 6471 6472 6473
	| 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	{}
6474
	| MASTER_SERVER_ID_SYM  {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6475
	| MASTER_CONNECT_RETRY_SYM	{}
6476 6477 6478 6479 6480 6481
	| 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
6482 6483 6484
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
6485
	| MEDIUM_SYM		{}
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
6486
	| MERGE_SYM		{}
6487
	| MICROSECOND_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6488 6489 6490
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6491
	| MODE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6492
	| MONTH_SYM		{}
6493 6494 6495
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
6496
	| NAME_SYM              {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6497
	| NAMES_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6498 6499
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
6500
	| NDBCLUSTER_SYM	{}
6501
	| NEXT_SYM		{}
6502
	| NEW_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6503
	| NO_SYM		{}
6504
	| NONE_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6505
	| NVARCHAR_SYM		{}
6506
	| OFFSET_SYM		{}
6507
	| OLD_PASSWORD		{}
6508
	| ONE_SHOT_SYM		{}
6509
	| OPEN_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6510
	| PACK_KEYS_SYM		{}
6511
	| PARTIAL		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6512
	| PASSWORD		{}
6513
	| POINT_SYM		{}
6514
	| POLYGON		{}
sergefp@mysql.com's avatar
sergefp@mysql.com committed
6515
        | PREPARE_SYM           {}
6516
	| PREV_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6517 6518
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
6519
	| QUARTER_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6520
	| QUERY_SYM		{}
6521
	| QUICK			{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6522
	| RAID_0_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6523 6524
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6525
	| RAID_STRIPED_SYM	{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6526
	| RAID_TYPE		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6527 6528
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6529 6530
	| RELOAD		{}
	| REPAIR		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6531
	| REPEATABLE_SYM	{}
6532
	| REPLICATION		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6533
	| RESET_SYM		{}
6534
	| RESOURCES		{}
6535
	| RESTORE_SYM		{}
6536
	| RETURNS_SYM           {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6537
	| ROLLBACK_SYM		{}
6538
	| ROLLUP_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6539 6540 6541
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6542
	| RTREE_SYM		{}
6543
	| SAVEPOINT_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6544
	| SECOND_SYM		{}
6545
	| SECURITY_SYM		{}
6546
	| SERIAL_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6547 6548
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6549
	| SIGNED_SYM		{}
6550
	| SIMPLE_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6551
	| SHARE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6552
	| SHUTDOWN		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6553
	| SLAVE			{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6554
	| SOUNDS_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6555
	| SQL_CACHE_SYM		{}
6556
	| SQL_BUFFER_RESULT	{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6557
	| SQL_NO_CACHE_SYM	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6558
	| SQL_THREAD		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6559 6560 6561
	| START_SYM		{}
	| STATUS_SYM		{}
	| STOP_SYM		{}
6562
	| STORAGE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6563
	| STRING_SYM		{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6564
	| SUBDATE_SYM		{}
6565
	| SUBJECT_SYM		{}
6566
	| SUPER_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
6567
	| TABLESPACE		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6568
	| TEMPORARY		{}
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
6569
	| TEMPTABLE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6570
	| TEXT_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6571
	| TRANSACTION_SYM	{}
6572
	| TRUNCATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6573
	| TIMESTAMP		{}
6574 6575
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6576 6577
	| TIME_SYM		{}
	| TYPE_SYM		{}
6578
	| TYPES_SYM		{}
6579
	| FUNCTION_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6580
	| UNCOMMITTED_SYM	{}
6581
	| UNICODE_SYM		{}
6582
	| UNTIL_SYM		{}
6583
	| USER			{}
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
6584
	| USE_FRM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6585
	| VARIABLES		{}
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
6586
	| VIEW_SYM		{}
6587
	| VALUE_SYM		{}
6588
	| WARNINGS		{}
6589
	| WEEK_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6590
	| WORK_SYM		{}
6591
	| X509_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6592 6593
	| YEAR_SYM		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6594 6595 6596 6597 6598 6599

/* Option functions */

set:
	SET opt_option
	{
6600 6601
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
6602
	  lex->option_type=OPT_SESSION;
6603
	  lex->var_list.empty();
6604
          lex->one_shot_set= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6605
	}
6606 6607 6608
	option_value_list
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6609 6610 6611

opt_option:
	/* empty */ {}
6612
	| OPTION {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6613 6614

option_value_list:
6615 6616 6617 6618 6619 6620 6621 6622
	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; }
6623
	| ONE_SHOT_SYM	{ Lex->option_type= OPT_SESSION; Lex->one_shot_set= 1; }
6624 6625 6626 6627
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6628
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
6629 6630 6631 6632 6633 6634
	| 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
6635
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
6636 6637 6638
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6639 6640

option_value:
6641
	  '@' ident_or_text equal expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6642
	  {
pem@mysql.telia.com's avatar
pem@mysql.telia.com committed
6643 6644
	    Lex->var_list.push_back(new set_var_user(new Item_func_set_user_var($2,$4)));
	  }
6645
	| internal_variable_name equal set_expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6646
	  {
6647
	    LEX *lex=Lex;
pem@mysql.telia.com's avatar
pem@mysql.telia.com committed
6648 6649 6650 6651 6652 6653 6654 6655

	    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 */
6656 6657 6658 6659
	      sp_pvar_t *spv;
              sp_instr_set *i;
	      Item *it;

6660 6661 6662 6663 6664
	      if ($3 && $3->type() == Item::SUBSELECT_ITEM)
	      {  /* QQ For now, just disallow subselects as values */
	        send_error(lex->thd, ER_SP_SUBSELECT_NYI);
	        YYABORT;
	      }
6665
	      spv= lex->spcont->find_pvar(&$1.base_name);
pem@mysql.telia.com's avatar
pem@mysql.telia.com committed
6666

6667 6668 6669 6670 6671 6672 6673 6674
	      if ($3)
	        it= $3;
	      else if (spv->dflt)
	        it= spv->dflt;
	      else
	        it= new Item_null();
              i= new sp_instr_set(lex->sphead->instructions(),
	                          spv->offset, it, spv->type);
pem@mysql.telia.com's avatar
pem@mysql.telia.com committed
6675 6676 6677
	      lex->sphead->add_instr(i);
	      spv->isset= TRUE;
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6678
	  }
6679
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
6680 6681
	  {
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6682 6683
	    lex->var_list.push_back(new set_var((enum_var_type) $3, $4.var,
						&$4.base_name, $6));
6684 6685 6686 6687
	  }
	| TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	  {
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6688 6689 6690
	    LEX_STRING tmp;
	    tmp.str=0;
	    tmp.length=0;
6691
	    lex->var_list.push_back(new set_var(lex->option_type,
6692
						find_sys_var("tx_isolation"),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6693
						&tmp,
6694
						new Item_int((int32) $4)));
6695
	  }
6696
	| charset old_or_new_charset_name_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6697
	{
6698
	  THD *thd= YYTHD;
6699
	  LEX *lex= Lex;
6700
	  $2= $2 ? $2: global_system_variables.character_set_client;
6701
	  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
6702
	}
6703
	| NAMES_SYM charset_name_or_default opt_collate
6704
	{
6705
	  THD *thd= YYTHD;
6706
	  LEX *lex= Lex;
6707
	  $2= $2 ? $2 : global_system_variables.character_set_client;
6708 6709
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
6710
	  {
6711 6712
	    net_printf(thd,ER_COLLATION_CHARSET_MISMATCH,$3->name,$2->csname);
	    YYABORT;
6713
	  }
6714
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
6715
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6716
	| PASSWORD equal text_or_password
6717
	  {
6718
	    THD *thd=YYTHD;
6719
	    LEX_USER *user;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6720
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
6721 6722 6723
	      YYABORT;
	    user->host.str=0;
	    user->user.str=thd->priv_user;
6724
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
6725 6726
	  }
	| PASSWORD FOR_SYM user equal text_or_password
6727
	  {
6728 6729
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
6730
	;
6731

6732 6733 6734
internal_variable_name:
	ident
	{
pem@mysql.telia.com's avatar
pem@mysql.telia.com committed
6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753
	  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;
	  }
6754
	}
6755 6756
	| ident '.' ident
	  {
6757 6758
            if (check_reserved_words(&$1))
            {
6759
	      yyerror(ER(ER_SYNTAX_ERROR));
6760 6761
              YYABORT;
            }
6762 6763 6764 6765 6766
	    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
6767 6768
	    $$.var= tmp;
	    $$.base_name= $1;
6769 6770 6771 6772 6773 6774 6775 6776
	  }
	| 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
6777 6778 6779
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
6780
	  }
6781
        ;
6782 6783 6784 6785 6786 6787 6788

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6790 6791 6792 6793
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
6794
	    $$= $3.length ? YYTHD->variables.old_passwords ?
6795 6796 6797 6798 6799 6800 6801 6802
	        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;
6803 6804
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6805

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

6807
set_expr_or_default:
6808 6809
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
6810 6811
	| 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
6812
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
6813
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
6814 6815


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6816 6817 6818 6819 6820 6821 6822
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
	  Lex->sql_command=SQLCOM_LOCK_TABLES;
	}
6823 6824 6825
	table_lock_list
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6826 6827 6828

table_or_tables:
	TABLE_SYM
6829
	| TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6830 6831 6832

table_lock_list:
	table_lock
6833
	| table_lock_list ',' table_lock;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6834 6835 6836

table_lock:
	table_ident opt_table_alias lock_option
6837
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6838
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
6839 6840
	   YYABORT;
	}
6841
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6842 6843 6844

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
6845
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6846
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
6847 6848
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6849 6850

unlock:
6851 6852
	UNLOCK_SYM table_or_tables { Lex->sql_command=SQLCOM_UNLOCK_TABLES; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6853 6854


6855
/*
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
6856
** Handler: direct access to ISAM functions
6857 6858 6859 6860 6861
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
6862 6863
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_OPEN;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6864
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
6865 6866
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
6867
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
6868
	{
6869 6870
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_CLOSE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6871
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
6872 6873
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
6874
	| HANDLER_SYM table_ident_nodb READ_SYM
6875
	{
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
6876 6877 6878
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
6879 6880
	  lex->current_select->select_limit= 1;
	  lex->current_select->offset_limit= 0L;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6881
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
6882
	    YYABORT;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
6883
        }
6884
        handler_read_or_scan where_clause opt_limit_clause {}
6885
        ;
6886

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
6887 6888
handler_read_or_scan:
	handler_scan_function         { Lex->backup_dir= 0; }
6889 6890
        | ident handler_rkey_function { Lex->backup_dir= $1.str; }
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
6891 6892 6893

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
6894 6895
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
6896 6897 6898 6899 6900 6901

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;  }
6902 6903
	| handler_rkey_mode
	{
6904 6905
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
6906
	  lex->ha_rkey_mode=$1;
6907
	  if (!(lex->insert_list = new List_item))
6908
	    YYABORT;
6909 6910
	} '(' values ')' { }
        ;
6911 6912

handler_rkey_mode:
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
6913 6914 6915 6916
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
6917 6918
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
6919

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6920 6921 6922 6923 6924
/* GRANT / REVOKE */

revoke:
	REVOKE
	{
6925 6926 6927 6928 6929
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_REVOKE;
	  lex->users_list.empty();
	  lex->columns.empty();
	  lex->grant= lex->grant_tot_col=0;
6930
	  lex->select_lex.db=0;
6931 6932
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
6933
	  bzero((char*) &lex->mqh, sizeof(lex->mqh));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6934
	}
6935 6936 6937 6938 6939
	revoke_command
	{}
        ;

revoke_command:
6940 6941
	grant_privileges ON opt_table FROM user_list
	{}
6942
	|
6943
	ALL PRIVILEGES ',' GRANT OPTION FROM user_list
6944 6945 6946
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
6947
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6948 6949 6950 6951

grant:
	GRANT
	{
6952 6953 6954
	  LEX *lex=Lex;
	  lex->users_list.empty();
	  lex->columns.empty();
6955 6956
	  lex->sql_command = SQLCOM_GRANT;
	  lex->grant= lex->grant_tot_col= 0;
6957
	  lex->select_lex.db= 0;
6958 6959
	  lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
	  lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
6960
	  bzero((char *)&(lex->mqh),sizeof(lex->mqh));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6961 6962
	}
	grant_privileges ON opt_table TO_SYM user_list
6963 6964 6965
	require_clause grant_options
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6966 6967 6968

grant_privileges:
	grant_privilege_list {}
6969
	| ALL PRIVILEGES	{ Lex->grant = GLOBAL_ACLS;}
6970 6971
	| ALL			{ Lex->grant = GLOBAL_ACLS;}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6972 6973 6974

grant_privilege_list:
	grant_privilege
6975
	| grant_privilege_list ',' grant_privilege;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6976 6977

grant_privilege:
serg@serg.mylan's avatar
serg@serg.mylan committed
6978
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
6979 6980 6981
	| 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 {}
6982 6983
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
6984
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6985 6986 6987
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
6988
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6989 6990 6991 6992
	| RELOAD	{ Lex->grant |= RELOAD_ACL;}
	| SHUTDOWN	{ Lex->grant |= SHUTDOWN_ACL;}
	| PROCESS	{ Lex->grant |= PROCESS_ACL;}
	| FILE_SYM	{ Lex->grant |= FILE_ACL;}
6993 6994 6995 6996 6997
	| 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
6998 6999 7000 7001
	| 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; }
7002
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7003

7004

7005 7006
opt_and:
	/* empty */	{}
7007
	| AND_SYM	{}
7008 7009 7010 7011 7012 7013 7014 7015 7016
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
7017 7018 7019 7020
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
7021
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "SUBJECT");
7022 7023 7024 7025 7026 7027 7028 7029 7030
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
7031
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "ISSUER");
7032 7033 7034 7035 7036 7037 7038 7039 7040
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
7041
	    net_printf(lex->thd,ER_DUP_ARGUMENT, "CIPHER");
7042 7043 7044 7045 7046
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
7047

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7048 7049 7050
opt_table:
	'*'
	  {
7051
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7052
	    lex->current_select->db= lex->thd->db;
7053
	    if (lex->grant == GLOBAL_ACLS)
7054 7055
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7056
	    {
7057
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
7058
	      YYABORT;
7059
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7060 7061 7062
	  }
	| ident '.' '*'
	  {
7063
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7064
	    lex->current_select->db = $1.str;
7065
	    if (lex->grant == GLOBAL_ACLS)
7066 7067
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7068
	    {
7069
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7070 7071 7072 7073 7074
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
7075
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7076
	    lex->current_select->db = NULL;
7077 7078
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
7079
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7080
	    {
7081
	      send_error(lex->thd,ER_ILLEGAL_GRANT_FOR_TABLE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7082 7083 7084 7085 7086
	      YYABORT;
	    }
	  }
	| table_ident
	  {
7087
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7088
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7089
	      YYABORT;
7090
	    if (lex->grant == GLOBAL_ACLS)
7091
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
7092 7093
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7094 7095 7096


user_list:
7097 7098 7099 7100 7101 7102 7103
	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
7104 7105 7106 7107 7108 7109 7110 7111


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
7112
             if (YYTHD->variables.old_passwords)
7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129
             {
               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
7130 7131 7132 7133 7134
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
	  { $$=$1; $1->password=$5 ; }
	| user
7135 7136
	  { $$=$1; $1->password.str=NullS; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7137 7138 7139


opt_column_list:
7140 7141 7142 7143 7144
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
7145
	| '(' column_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7146 7147 7148

column_list:
	column_list ',' column_list_id
7149
	| column_list_id;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7150 7151 7152 7153

column_list_id:
	ident
	{
monty@mysql.com's avatar
monty@mysql.com committed
7154
	  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
7155 7156
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
7157
	  LEX *lex=Lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7158 7159
	  while ((point=iter++))
	  {
7160 7161
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7162 7163
		break;
	  }
7164
	  lex->grant_tot_col|= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7165
	  if (point)
7166
	    point->rights |= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7167
	  else
7168
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
7169 7170
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7171

tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
7172 7173

require_clause: /* empty */
7174
        | REQUIRE_SYM require_list
7175 7176 7177
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
7178
        | REQUIRE_SYM SSL_SYM
7179 7180 7181
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
7182
        | REQUIRE_SYM X509_SYM
7183 7184 7185 7186 7187 7188 7189
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
7190
          ;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
7191

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7192
grant_options:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7193
	/* empty */ {}
7194
	| WITH grant_option_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7195

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7196 7197
grant_option_list:
	grant_option_list grant_option {}
7198 7199
	| grant_option {}
        ;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7200 7201 7202

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
7203
        | MAX_QUERIES_PER_HOUR ULONG_NUM
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7204
        {
7205
	  Lex->mqh.questions=$2;
7206
	  Lex->mqh.bits |= 1;
7207
	}
7208
        | MAX_UPDATES_PER_HOUR ULONG_NUM
7209
        {
7210
	  Lex->mqh.updates=$2;
7211
	  Lex->mqh.bits |= 2;
7212
	}
7213
        | MAX_CONNECTIONS_PER_HOUR ULONG_NUM
7214
        {
7215
	  Lex->mqh.connections=$2;
7216
	  Lex->mqh.bits |= 4;
7217 7218
	}
        ;
7219

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7220
begin:
7221 7222
	BEGIN_SYM   { Lex->sql_command = SQLCOM_BEGIN;} opt_work {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7223 7224 7225

opt_work:
	/* empty */ {}
7226 7227
	| WORK_SYM {;}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7228 7229

commit:
7230
	COMMIT_SYM   { Lex->sql_command = SQLCOM_COMMIT;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7231 7232

rollback:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7233
	ROLLBACK_SYM
7234 7235 7236 7237 7238
	{
	  Lex->sql_command = SQLCOM_ROLLBACK;
	}
	| ROLLBACK_SYM TO_SYM SAVEPOINT_SYM ident
	{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
7239
	  Lex->sql_command = SQLCOM_ROLLBACK_TO_SAVEPOINT;
7240 7241 7242 7243 7244 7245 7246 7247
	  Lex->savepoint_name = $4.str;
	};
savepoint:
	SAVEPOINT_SYM ident
	{
	  Lex->sql_command = SQLCOM_SAVEPOINT;
	  Lex->savepoint_name = $2.str;
	};
7248 7249

/*
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7250
   UNIONS : glue selects together
7251 7252 7253
*/


7254
union_clause:
7255
	/* empty */ {}
7256 7257
	| union_list
	;
7258 7259

union_list:
7260
	UNION_SYM union_option
7261 7262 7263 7264 7265
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
7266
	    net_printf(lex->thd, ER_WRONG_USAGE, "UNION", "INTO");
7267
	    YYABORT;
7268
	  }
7269
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
7270
	  {
7271
            yyerror(ER(ER_SYNTAX_ERROR));
7272 7273
	    YYABORT;
	  }
7274
	  if (mysql_new_select(lex, 0))
7275
	    YYABORT;
7276
          mysql_init_select(lex);
7277
	  lex->current_select->linkage=UNION_TYPE;
7278 7279 7280
          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
7281
	}
7282
	select_init {}
7283
	;
7284 7285

union_opt:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7286
	union_list {}
7287 7288
	| optional_order_or_limit {}
	;
7289 7290

optional_order_or_limit:
7291
	/* Empty */ {}
7292 7293
	|
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7294
	    THD *thd= YYTHD;
7295
	    LEX *lex= thd->lex;
7296
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7297
	    SELECT_LEX *sel= lex->current_select;
7298
	    SELECT_LEX_UNIT *unit= sel->master_unit();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7299
	    SELECT_LEX *fake= unit->fake_select_lex;
7300 7301 7302 7303 7304 7305
	    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
7306
	    thd->where= "global ORDER clause";
7307
	  }
7308
	order_or_limit
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7309 7310
          {
	    THD *thd= YYTHD;
7311
	    thd->lex->current_select->no_table_names_allowed= 0;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7312 7313
	    thd->where= "";
          }
7314 7315 7316
	;

order_or_limit:
7317 7318
	order_clause opt_limit_clause_init
	| limit_clause
7319
	;
7320 7321

union_option:
7322 7323 7324 7325
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
7326

7327 7328
singlerow_subselect:
	subselect_start singlerow_subselect_init
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7329 7330 7331 7332
	subselect_end
	{
	  $$= $2;
	};
7333

7334
singlerow_subselect_init:
7335
	select_init2
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7336
	{
7337 7338
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7339
	};
7340 7341

exists_subselect:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7342 7343 7344 7345 7346
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
7347 7348

exists_subselect_init:
7349
	select_init2
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7350
	{
7351 7352
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7353
	};
7354

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7355 7356 7357 7358 7359 7360 7361 7362
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
7363
  select_init2
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7364
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7365
    $$= Lex->current_select->master_unit()->first_select();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7366 7367
  };

7368
subselect_start:
7369
	'(' SELECT_SYM
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7370
	{
7371
	  LEX *lex=Lex;
7372
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
7373 7374
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
7375 7376
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
7377 7378
	    YYABORT;
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7379 7380 7381
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
7382 7383

subselect_end:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7384 7385 7386
	')'
	{
	  LEX *lex=Lex;
7387
	  lex->current_select = lex->current_select->return_after_parsing();
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7388
	};
serg@serg.mylan's avatar
serg@serg.mylan committed
7389

bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427
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
	  { Lex->create_view_algorithm= VIEW_ALGORITHM_TMEPTABLE; }
	;
check_option:
        /* empty */ {}
        | WITH CHECK_SYM OPTION {}
        | WITH CASCADED CHECK_SYM OPTION {}
        | WITH LOCAL_SYM CHECK_SYM OPTION {}
        ;