sql_yacc.yy 280 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
#include "lex_symbol.h"
36
#include "item_create.h"
37 38 39 40
#include "sp_head.h"
#include "sp_pcontext.h"
#include "sp_rcontext.h"
#include "sp.h"
41
#include "event.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 48
const LEX_STRING null_lex_str={0,0};

monty@mishka.local's avatar
monty@mishka.local committed
49
#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
50

serg@serg.mylan's avatar
serg@serg.mylan committed
51
#define WARN_DEPRECATED(A,B)                                        \
52
  push_warning_printf(((THD *)yythd), MYSQL_ERROR::WARN_LEVEL_WARN, \
serg@serg.mylan's avatar
serg@serg.mylan committed
53
		      ER_WARN_DEPRECATED_SYNTAX,                    \
54
		      ER(ER_WARN_DEPRECATED_SYNTAX), (A), (B));
55

serg@serg.mylan's avatar
serg@serg.mylan committed
56
#define YYERROR_UNLESS(A)                  \
serg@serg.mylan's avatar
serg@serg.mylan committed
57
  if (!(A))                             \
58 59 60 61 62
  {					\
    yyerror(ER(ER_SYNTAX_ERROR));	\
    YYABORT;				\
  }

63 64
/* Helper for parsing "IS [NOT] truth_value" */
inline Item *is_truth_value(Item *A, bool v1, bool v2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
65
{
66 67
  return new Item_func_if(create_func_ifnull(A,
	new Item_int((char *) (v2 ? "TRUE" : "FALSE"), v2, 1)),
68
	new Item_int((char *) (v1 ? "TRUE" : "FALSE"), v1, 1),
69
	new Item_int((char *) (v1 ? "FALSE" : "TRUE"),!v1, 1));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
70 71 72 73 74 75
}

%}
%union {
  int  num;
  ulong ulong_num;
76
  ulonglong ulonglong_number;
77
  longlong longlong_number;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
78 79 80 81 82 83
  LEX_STRING lex_str;
  LEX_STRING *lex_str_ptr;
  LEX_SYMBOL symbol;
  Table_ident *table;
  char *simple_string;
  Item *item;
84
  Item_num *item_num;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
85 86
  List<Item> *item_list;
  List<String> *string_list;
87 88 89 90 91
  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
92
  struct sys_var_with_base variable;
93
  enum enum_var_type var_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
94
  Key::Keytype key_type;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
95
  enum ha_key_alg key_alg;
96
  handlerton *db_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
97
  enum row_type row_type;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
98
  enum ha_rkey_function ha_rkey_mode;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
99
  enum enum_tx_isolation tx_isolation;
100
  enum Cast_target cast_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
101
  enum Item_udftype udf_type;
102
  CHARSET_INFO *charset;
103
  thr_lock_type lock_type;
104
  interval_type interval, interval_time_st;
105
  timestamp_type date_time_type;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
106
  st_select_lex *select_lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
107
  chooser_compare_func_creator boolfunc2creator;
108 109 110 111
  struct sp_cond_type *spcondtype;
  struct { int vars, conds, hndlrs, curs; } spblock;
  sp_name *spname;
  struct st_lex *lex;
andrey@lmy004's avatar
andrey@lmy004 committed
112
  sp_head *sphead;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
113 114 115
}

%{
116
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
117 118 119 120
%}

%pure_parser					/* We have threads */

serg@serg.mylan's avatar
serg@serg.mylan committed
121
%token  END_OF_INPUT
bk@work.mysql.com's avatar
bk@work.mysql.com committed
122

123
%token  ABORT_SYM
124
%token  ACCESSIBLE_SYM
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
%token  ACTION
%token  ADD
%token  ADDDATE_SYM
%token  AFTER_SYM
%token  AGAINST
%token  AGGREGATE_SYM
%token  ALGORITHM_SYM
%token  ALL
%token  ALTER
%token  ANALYZE_SYM
%token  AND_AND_SYM
%token  AND_SYM
%token  ANY_SYM
%token  AS
%token  ASC
%token  ASCII_SYM
%token  ASENSITIVE_SYM
142
%token  AT_SYM
143
%token  ATAN
brian@grrr.local's avatar
brian@grrr.local committed
144
%token  AUTHORS_SYM
145
%token  AUTO_INC
146
%token  AUTOEXTEND_SIZE_SYM
147 148 149 150 151 152 153 154 155 156
%token  AVG_ROW_LENGTH
%token  AVG_SYM
%token  BACKUP_SYM
%token  BEFORE_SYM
%token  BEGIN_SYM
%token  BENCHMARK_SYM
%token  BERKELEY_DB_SYM
%token  BIGINT
%token  BINARY
%token  BINLOG_SYM
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
157
%token  BIN_NUM
158 159 160 161 162 163 164 165 166 167 168 169 170 171
%token  BIT_AND
%token  BIT_OR
%token  BIT_SYM
%token  BIT_XOR
%token  BLOB_SYM
%token  BOOLEAN_SYM
%token  BOOL_SYM
%token  BOTH
%token  BTREE_SYM
%token  BY
%token  BYTE_SYM
%token  CACHE_SYM
%token  CALL_SYM
%token  CASCADE
172
%token  CASCADED
173
%token  CAST_SYM
serg@serg.mylan's avatar
serg@serg.mylan committed
174
%token  CHAIN_SYM
175 176 177 178 179 180 181 182 183 184
%token  CHANGE
%token  CHANGED
%token  CHARSET
%token  CHAR_SYM
%token  CHECKSUM_SYM
%token  CHECK_SYM
%token  CIPHER_SYM
%token  CLIENT_SYM
%token  CLOSE_SYM
%token  COALESCE
pem@mysql.com's avatar
pem@mysql.com committed
185
%token  CODE_SYM
186 187 188 189 190 191 192
%token  COLLATE_SYM
%token  COLLATION_SYM
%token  COLUMNS
%token  COLUMN_SYM
%token  COMMENT_SYM
%token  COMMITTED_SYM
%token  COMMIT_SYM
serg@serg.mylan's avatar
serg@serg.mylan committed
193
%token  COMPACT_SYM
194
%token  COMPLETION_SYM
195 196 197 198
%token  COMPRESSED_SYM
%token  CONCAT
%token  CONCAT_WS
%token  CONCURRENT
199
%token  CONDITION_SYM
200 201 202
%token  CONNECTION_SYM
%token  CONSISTENT_SYM
%token  CONSTRAINT
203 204
%token  CONTAINS_SYM
%token  CONTINUE_SYM
205 206 207 208 209 210 211
%token  CONVERT_SYM
%token  CONVERT_TZ_SYM
%token  COUNT_SYM
%token  CREATE
%token  CROSS
%token  CUBE_SYM
%token  CURDATE
212
%token  CURRENT_USER
213 214 215 216
%token  CURSOR_SYM
%token  CURTIME
%token  DATABASE
%token  DATABASES
217
%token  DATAFILE_SYM
218 219 220 221 222 223 224 225 226 227 228
%token  DATA_SYM
%token  DATETIME
%token  DATE_ADD_INTERVAL
%token  DATE_SUB_INTERVAL
%token  DATE_SYM
%token  DAY_HOUR_SYM
%token  DAY_MICROSECOND_SYM
%token  DAY_MINUTE_SYM
%token  DAY_SECOND_SYM
%token  DAY_SYM
%token  DEALLOCATE_SYM
serg@serg.mylan's avatar
serg@serg.mylan committed
229
%token  DECIMAL_NUM
230
%token  DECIMAL_SYM
231
%token  DECLARE_SYM
232 233 234 235 236 237 238 239 240 241 242 243 244 245
%token  DECODE_SYM
%token  DEFAULT
%token  DEFINER_SYM
%token  DELAYED_SYM
%token  DELAY_KEY_WRITE_SYM
%token  DELETE_SYM
%token  DESC
%token  DESCRIBE
%token  DES_DECRYPT_SYM
%token  DES_ENCRYPT_SYM
%token  DES_KEY_FILE
%token  DETERMINISTIC_SYM
%token  DIRECTORY_SYM
%token  DISABLE_SYM
andrey@lmy004's avatar
andrey@lmy004 committed
246
%token  DISABLED_SYM
247
%token  DISCARD
248
%token  DISK_SYM
249 250 251 252 253 254 255
%token  DISTINCT
%token  DIV_SYM
%token  DOUBLE_SYM
%token  DO_SYM
%token  DROP
%token  DUAL_SYM
%token  DUMPFILE
256
%token  DUPLICATE_SYM
257
%token  DYNAMIC_SYM
258
%token  EACH_SYM
259 260 261
%token  ELSEIF_SYM
%token  ELT_FUNC
%token  ENABLE_SYM
andrey@lmy004's avatar
andrey@lmy004 committed
262
%token  ENABLED_SYM
263 264 265 266
%token  ENCLOSED
%token  ENCODE_SYM
%token  ENCRYPT
%token  END
267
%token  ENDS_SYM
268 269 270 271 272 273 274 275
%token  ENGINES_SYM
%token  ENGINE_SYM
%token  ENUM
%token  EQ
%token  EQUAL_SYM
%token  ERRORS
%token  ESCAPED
%token  ESCAPE_SYM
276
%token  EVENT_SYM
277
%token  EVENTS_SYM
278
%token  EVERY_SYM
279 280
%token  EXECUTE_SYM
%token  EXISTS
281
%token  EXIT_SYM
282 283 284
%token  EXPANSION_SYM
%token  EXPORT_SET
%token  EXTENDED_SYM
285
%token  EXTENT_SIZE_SYM
286 287 288
%token  EXTRACT_SYM
%token  FALSE_SYM
%token  FAST_SYM
289
%token  FETCH_SYM
290 291 292 293 294 295 296 297 298 299 300
%token  FIELD_FUNC
%token  FILE_SYM
%token  FIRST_SYM
%token  FIXED_SYM
%token  FLOAT_NUM
%token  FLOAT_SYM
%token  FLUSH_SYM
%token  FORCE_SYM
%token  FOREIGN
%token  FORMAT_SYM
%token  FOR_SYM
301
%token  FOUND_SYM
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
%token  FRAC_SECOND_SYM
%token  FROM
%token  FROM_UNIXTIME
%token  FULL
%token  FULLTEXT_SYM
%token  FUNCTION_SYM
%token  FUNC_ARG0
%token  FUNC_ARG1
%token  FUNC_ARG2
%token  FUNC_ARG3
%token  GE
%token  GEOMCOLLFROMTEXT
%token  GEOMETRYCOLLECTION
%token  GEOMETRY_SYM
%token  GEOMFROMTEXT
%token  GEOMFROMWKB
%token  GET_FORMAT
%token  GLOBAL_SYM
%token  GOTO_SYM
%token  GRANT
%token  GRANTS
%token  GREATEST_SYM
%token  GROUP
%token  GROUP_CONCAT_SYM
%token  GROUP_UNIQUE_USERS
%token  GT_SYM
%token  HANDLER_SYM
%token  HASH_SYM
%token  HAVING
%token  HELP_SYM
%token  HEX_NUM
%token  HIGH_PRIORITY
%token  HOSTS_SYM
%token  HOUR_MICROSECOND_SYM
%token  HOUR_MINUTE_SYM
%token  HOUR_SECOND_SYM
%token  HOUR_SYM
%token  IDENT
%token  IDENTIFIED_SYM
%token  IDENT_QUOTED
%token  IF
%token  IGNORE_SYM
%token  IMPORT
%token  INDEXES
%token  INDEX_SYM
%token  INFILE
348
%token  INITIAL_SIZE_SYM
349 350
%token  INNER_SYM
%token  INNOBASE_SYM
351
%token  INOUT_SYM
352 353 354
%token  INSENSITIVE_SYM
%token  INSERT
%token  INSERT_METHOD
355
%token  INSTALL_SYM
356 357 358
%token  INTERVAL_SYM
%token  INTO
%token  INT_SYM
359
%token  INVOKER_SYM
360 361 362 363 364 365 366 367 368 369
%token  IN_SYM
%token  IS
%token  ISOLATION
%token  ISSUER_SYM
%token  ITERATE_SYM
%token  JOIN_SYM
%token  KEYS
%token  KEY_SYM
%token  KILL_SYM
%token  LABEL_SYM
370
%token  LANGUAGE_SYM
371 372 373 374 375 376 377 378
%token  LAST_INSERT_ID
%token  LAST_SYM
%token  LE
%token  LEADING
%token  LEAST_SYM
%token  LEAVES
%token  LEAVE_SYM
%token  LEFT
379
%token  LESS_SYM
380 381 382 383
%token  LEVEL_SYM
%token  LEX_HOSTNAME
%token  LIKE
%token  LIMIT
384
%token  LINEAR_SYM
385 386 387
%token  LINEFROMTEXT
%token  LINES
%token  LINESTRING
388
%token  LIST_SYM
389 390 391
%token  LOAD
%token  LOCAL_SYM
%token  LOCATE
392
%token  LOCATOR_SYM
393 394
%token  LOCKS_SYM
%token  LOCK_SYM
395
%token  LOGFILE_SYM
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
%token  LOGS_SYM
%token  LOG_SYM
%token  LONGBLOB
%token  LONGTEXT
%token  LONG_NUM
%token  LONG_SYM
%token  LOOP_SYM
%token  LOW_PRIORITY
%token  LT
%token  MAKE_SET_SYM
%token  MASTER_CONNECT_RETRY_SYM
%token  MASTER_HOST_SYM
%token  MASTER_LOG_FILE_SYM
%token  MASTER_LOG_POS_SYM
%token  MASTER_PASSWORD_SYM
%token  MASTER_PORT_SYM
%token  MASTER_POS_WAIT
%token  MASTER_SERVER_ID_SYM
%token  MASTER_SSL_CAPATH_SYM
%token  MASTER_SSL_CA_SYM
%token  MASTER_SSL_CERT_SYM
%token  MASTER_SSL_CIPHER_SYM
%token  MASTER_SSL_KEY_SYM
%token  MASTER_SSL_SYM
%token  MASTER_SYM
%token  MASTER_USER_SYM
%token  MATCH
%token  MAX_CONNECTIONS_PER_HOUR
%token  MAX_QUERIES_PER_HOUR
%token  MAX_ROWS
426
%token  MAX_SIZE_SYM
427 428
%token  MAX_SYM
%token  MAX_UPDATES_PER_HOUR
serg@serg.mylan's avatar
serg@serg.mylan committed
429
%token  MAX_USER_CONNECTIONS_SYM
430
%token  MAX_VALUE_SYM
431 432 433 434
%token  MEDIUMBLOB
%token  MEDIUMINT
%token  MEDIUMTEXT
%token  MEDIUM_SYM
435
%token  MEMORY_SYM
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
%token  MERGE_SYM
%token  MICROSECOND_SYM
%token  MIGRATE_SYM
%token  MINUTE_MICROSECOND_SYM
%token  MINUTE_SECOND_SYM
%token  MINUTE_SYM
%token  MIN_ROWS
%token  MIN_SYM
%token  MLINEFROMTEXT
%token  MODE_SYM
%token  MODIFIES_SYM
%token  MODIFY_SYM
%token  MOD_SYM
%token  MONTH_SYM
%token  MPOINTFROMTEXT
%token  MPOLYFROMTEXT
%token  MULTILINESTRING
%token  MULTIPOINT
%token  MULTIPOLYGON
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
455
%token  MUTEX_SYM
456 457 458 459 460 461
%token  NAMES_SYM
%token  NAME_SYM
%token  NATIONAL_SYM
%token  NATURAL
%token  NCHAR_STRING
%token  NCHAR_SYM
462
%token  NDBCLUSTER_SYM
463 464 465
%token  NE
%token  NEW_SYM
%token  NEXT_SYM
466
%token  NODEGROUP_SYM
467 468 469 470 471
%token  NONE_SYM
%token  NOT2_SYM
%token  NOT_SYM
%token  NOW_SYM
%token  NO_SYM
472
%token  NO_WAIT_SYM
473 474 475 476
%token  NO_WRITE_TO_BINLOG
%token  NULL_SYM
%token  NUM
%token  NUMERIC_SYM
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
477
%token  NVARCHAR_SYM
478 479 480
%token  OFFSET_SYM
%token  OLD_PASSWORD
%token  ON
481
%token  ONE_SHOT_SYM
482 483 484 485 486 487 488 489 490 491 492
%token  ONE_SYM
%token  OPEN_SYM
%token  OPTIMIZE
%token  OPTION
%token  OPTIONALLY
%token  OR2_SYM
%token  ORDER_SYM
%token  OR_OR_SYM
%token  OR_SYM
%token  OUTER
%token  OUTFILE
493
%token  OUT_SYM
494
%token  PACK_KEYS_SYM
495
%token  PARSER_SYM
496
%token  PARTIAL
497 498
%token  PARTITION_SYM
%token  PARTITIONS_SYM
499
%token  PASSWORD
500
%token  PARAM_MARKER
501
%token  PHASE_SYM
502
%token  PLUGIN_SYM
503 504 505 506 507 508 509
%token  POINTFROMTEXT
%token  POINT_SYM
%token  POLYFROMTEXT
%token  POLYGON
%token  POSITION_SYM
%token  PRECISION
%token  PREPARE_SYM
510
%token  PRESERVE_SYM
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
%token  PREV_SYM
%token  PRIMARY_SYM
%token  PRIVILEGES
%token  PROCEDURE
%token  PROCESS
%token  PROCESSLIST_SYM
%token  PURGE
%token  QUARTER_SYM
%token  QUERY_SYM
%token  QUICK
%token  RAID_0_SYM
%token  RAID_CHUNKS
%token  RAID_CHUNKSIZE
%token  RAID_STRIPED_SYM
%token  RAID_TYPE
%token  RAND
527
%token  RANGE_SYM
528
%token  READS_SYM
529
%token  READ_ONLY_SYM
530
%token  READ_SYM
531
%token  READ_WRITE_SYM
532
%token  REAL
533
%token  REBUILD_SYM
534
%token  RECOVER_SYM
535 536
%token  REDO_BUFFER_SIZE_SYM
%token  REDOFILE_SYM
serg@serg.mylan's avatar
serg@serg.mylan committed
537
%token  REDUNDANT_SYM
538 539 540 541 542
%token  REFERENCES
%token  REGEXP
%token  RELAY_LOG_FILE_SYM
%token  RELAY_LOG_POS_SYM
%token  RELAY_THREAD
serg@serg.mylan's avatar
serg@serg.mylan committed
543
%token  RELEASE_SYM
544 545
%token  RELOAD
%token  RENAME
546
%token  REORGANIZE_SYM
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
%token  REPAIR
%token  REPEATABLE_SYM
%token  REPEAT_SYM
%token  REPLACE
%token  REPLICATION
%token  REQUIRE_SYM
%token  RESET_SYM
%token  RESOURCES
%token  RESTORE_SYM
%token  RESTRICT
%token  RESUME_SYM
%token  RETURNS_SYM
%token  RETURN_SYM
%token  REVOKE
%token  RIGHT
%token  ROLLBACK_SYM
%token  ROLLUP_SYM
%token  ROUND
serg@serg.mylan's avatar
serg@serg.mylan committed
565
%token  ROUTINE_SYM
566 567 568 569 570 571
%token  ROWS_SYM
%token  ROW_COUNT_SYM
%token  ROW_FORMAT_SYM
%token  ROW_SYM
%token  RTREE_SYM
%token  SAVEPOINT_SYM
572
%token  SCHEDULE_SYM
573 574
%token  SECOND_MICROSECOND_SYM
%token  SECOND_SYM
575
%token  SECURITY_SYM
576 577
%token  SELECT_SYM
%token  SENSITIVE_SYM
578
%token  SEPARATOR_SYM
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
%token  SERIALIZABLE_SYM
%token  SERIAL_SYM
%token  SESSION_SYM
%token  SET
%token  SET_VAR
%token  SHARE_SYM
%token  SHIFT_LEFT
%token  SHIFT_RIGHT
%token  SHOW
%token  SHUTDOWN
%token  SIGNED_SYM
%token  SIMPLE_SYM
%token  SLAVE
%token  SMALLINT
%token  SNAPSHOT_SYM
594
%token  SONAME_SYM
595 596
%token  SOUNDS_SYM
%token  SPATIAL_SYM
597 598 599 600
%token  SPECIFIC_SYM
%token  SQLEXCEPTION_SYM
%token  SQLSTATE_SYM
%token  SQLWARNING_SYM
601 602 603 604 605 606 607 608
%token  SQL_BIG_RESULT
%token  SQL_BUFFER_RESULT
%token  SQL_CACHE_SYM
%token  SQL_CALC_FOUND_ROWS
%token  SQL_NO_CACHE_SYM
%token  SQL_SMALL_RESULT
%token  SQL_SYM
%token  SQL_THREAD
609
%token  SSL_SYM
610 611
%token  STARTING
%token  START_SYM
612
%token  STARTS_SYM
613 614
%token  STATUS_SYM
%token  STD_SYM
615
%token  STDDEV_SAMP_SYM
616 617 618 619 620 621
%token  STOP_SYM
%token  STORAGE_SYM
%token  STRAIGHT_JOIN
%token  STRING_SYM
%token  SUBDATE_SYM
%token  SUBJECT_SYM
622 623
%token  SUBPARTITION_SYM
%token  SUBPARTITIONS_SYM
624 625 626 627 628
%token  SUBSTRING
%token  SUBSTRING_INDEX
%token  SUM_SYM
%token  SUPER_SYM
%token  SUSPEND_SYM
629
%token  SYSDATE
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
%token  TABLES
%token  TABLESPACE
%token  TABLE_SYM
%token  TEMPORARY
%token  TEMPTABLE_SYM
%token  TERMINATED
%token  TEXT_STRING
%token  TEXT_SYM
%token  TIMESTAMP
%token  TIMESTAMP_ADD
%token  TIMESTAMP_DIFF
%token  TIME_SYM
%token  TINYBLOB
%token  TINYINT
%token  TINYTEXT
645
%token  THAN_SYM
646 647 648
%token  TO_SYM
%token  TRAILING
%token  TRANSACTION_SYM
649
%token  TRIGGER_SYM
650
%token  TRIGGERS_SYM
651 652 653
%token  TRIM
%token  TRUE_SYM
%token  TRUNCATE_SYM
654
%token  TYPES_SYM
655 656 657 658 659
%token  TYPE_SYM
%token  UDF_RETURNS_SYM
%token  ULONGLONG_NUM
%token  UNCOMMITTED_SYM
%token  UNDEFINED_SYM
660 661
%token  UNDO_BUFFER_SIZE_SYM
%token  UNDOFILE_SYM
662
%token  UNDERSCORE_CHARSET
663
%token  UNDO_SYM
664
%token  UNICODE_SYM
665
%token  UNINSTALL_SYM
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
%token  UNION_SYM
%token  UNIQUE_SYM
%token  UNIQUE_USERS
%token  UNIX_TIMESTAMP
%token  UNKNOWN_SYM
%token  UNLOCK_SYM
%token  UNSIGNED
%token  UNTIL_SYM
%token  UPDATE_SYM
%token  USAGE
%token  USER
%token  USE_FRM
%token  USE_SYM
%token  USING
%token  UTC_DATE_SYM
%token  UTC_TIMESTAMP_SYM
%token  UTC_TIME_SYM
683
%token  VAR_SAMP_SYM
684 685 686 687 688 689 690 691
%token  VALUES
%token  VALUE_SYM
%token  VARBINARY
%token  VARCHAR
%token  VARIABLES
%token  VARIANCE_SYM
%token  VARYING
%token  VIEW_SYM
692
%token  WAIT_SYM
693 694 695 696
%token  WARNINGS
%token  WEEK_SYM
%token  WHEN_SYM
%token  WHERE
697
%token  WHILE_SYM
698 699 700 701 702 703 704 705 706 707
%token  WITH
%token  WORK_SYM
%token  WRITE_SYM
%token  X509_SYM
%token  XA_SYM
%token  XOR
%token  YEARWEEK
%token  YEAR_MONTH_SYM
%token  YEAR_SYM
%token  ZEROFILL
708

709

710
%left   JOIN_SYM INNER_SYM STRAIGHT_JOIN CROSS LEFT RIGHT
timour@mysql.com's avatar
timour@mysql.com committed
711 712
/* A dummy token to force the priority of table_ref production in a join. */
%left   TABLE_REF_PRIORITY
bk@work.mysql.com's avatar
bk@work.mysql.com committed
713
%left   SET_VAR
714 715
%left	OR_OR_SYM OR_SYM OR2_SYM XOR
%left	AND_SYM AND_AND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
716 717 718 719 720 721
%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	'-' '+'
722
%left	'*' '/' '%' DIV_SYM MOD_SYM
723
%left   '^'
monty@mysql.com's avatar
monty@mysql.com committed
724
%left	NEG '~'
725
%right	NOT_SYM NOT2_SYM
726
%right	BINARY COLLATE_SYM
727

bk@work.mysql.com's avatar
bk@work.mysql.com committed
728
%type <lex_str>
729
        IDENT IDENT_QUOTED TEXT_STRING DECIMAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM
730
	LEX_HOSTNAME ULONGLONG_NUM field_ident select_alias ident ident_or_text
731
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
732
	NCHAR_STRING opt_component key_cache_name
733
        sp_opt_label BIN_NUM label_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
734 735

%type <lex_str_ptr>
736
	opt_table_alias opt_fulltext_parser
bk@work.mysql.com's avatar
bk@work.mysql.com committed
737 738

%type <table>
739
	table_ident table_ident_nodb references xid
bk@work.mysql.com's avatar
bk@work.mysql.com committed
740 741

%type <simple_string>
742
	remember_name remember_end opt_ident opt_db text_or_password
743
	opt_constraint constraint ident_or_empty
bk@work.mysql.com's avatar
bk@work.mysql.com committed
744 745

%type <string>
746
	text_string opt_gconcat_separator
bk@work.mysql.com's avatar
bk@work.mysql.com committed
747 748

%type <num>
749
	type int_type real_type order_dir lock_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
750
	udf_type if_exists opt_local opt_table_options table_options
751 752
        table_option opt_if_not_exists opt_no_write_to_binlog
        delete_option opt_temporary all_or_any opt_distinct
753
        opt_ignore_leaves fulltext_options spatial_type union_option
serg@serg.mylan's avatar
serg@serg.mylan committed
754
        start_transaction_opts opt_chain opt_release
755
        union_opt select_derived_init option_type2
bk@work.mysql.com's avatar
bk@work.mysql.com committed
756 757

%type <ulong_num>
serg@serg.mylan's avatar
serg@serg.mylan committed
758
	ulong_num raid_types merge_insert_types
bk@work.mysql.com's avatar
bk@work.mysql.com committed
759

760
%type <ulonglong_number>
761
	ulonglong_num size_number
bk@work.mysql.com's avatar
bk@work.mysql.com committed
762

763 764 765
%type <longlong_number>
        part_bit_expr

766 767 768
%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
769
%type <item>
770
	literal text_literal insert_ident order_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
771
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
772 773 774
	bool_term bool_factor bool_test bool_pri 
	predicate bit_expr bit_term bit_factor value_expr term factor
	table_wild simple_expr udf_expr
775
	expr_or_default set_expr_or_default interval_expr
776
	param_marker singlerow_subselect singlerow_subselect_init
hf@deer.(none)'s avatar
hf@deer.(none) committed
777
	exists_subselect exists_subselect_init geometry_function
778
	signed_literal now_or_signed_literal opt_escape
779 780
	sp_opt_default
	simple_ident_nospvar simple_ident_q
781
        field_or_var limit_option
782
        part_func_expr
783 784 785

%type <item_num>
	NUM_literal
bk@work.mysql.com's avatar
bk@work.mysql.com committed
786 787

%type <item_list>
788 789
	expr_list udf_expr_list udf_expr_list2 when_list
	ident_list ident_list_arg
bk@work.mysql.com's avatar
bk@work.mysql.com committed
790

791 792 793
%type <var_type>
        option_type opt_var_type opt_var_ident_type

bk@work.mysql.com's avatar
bk@work.mysql.com committed
794
%type <key_type>
795
	key_type opt_unique_or_fulltext constraint_key_type
bk@work.mysql.com's avatar
bk@work.mysql.com committed
796

797 798 799
%type <key_alg>
	key_alg opt_btree_or_rtree

bk@work.mysql.com's avatar
bk@work.mysql.com committed
800
%type <string_list>
801
	key_usage_list using_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
802 803 804 805 806

%type <key_part>
	key_part

%type <table_list>
807
	join_table_list  join_table
808
        table_factor table_ref
809
        select_derived derived_table_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
810

811
%type <date_time_type> date_time_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
812 813
%type <interval> interval

814 815
%type <interval_time_st> interval_time_st

816
%type <db_type> storage_engines
bk@work.mysql.com's avatar
bk@work.mysql.com committed
817 818 819

%type <row_type> row_types

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

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

824
%type <cast_type> cast_type
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
825

bk@work.mysql.com's avatar
bk@work.mysql.com committed
826 827
%type <udf_type> udf_func_type

828
%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword keyword_sp
bk@work.mysql.com's avatar
bk@work.mysql.com committed
829

830
%type <lex_user> user grant_user get_definer
bk@work.mysql.com's avatar
bk@work.mysql.com committed
831

832
%type <charset>
833
	opt_collate
834 835
	charset_name
	charset_name_or_default
836 837
	old_or_new_charset_name
	old_or_new_charset_name_or_default
838 839
	collation_name
	collation_name_or_default
840

841 842
%type <variable> internal_variable_name

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
843
%type <select_lex> in_subselect in_subselect_init
844
	get_select_lex
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
845

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
848
%type <NONE>
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
849
	query verb_clause create change select do drop insert replace insert2
850
	insert_values update delete truncate rename
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
851
	show describe load alter optimize keycache preload flush
852
	reset purge begin commit rollback savepoint release
853
	slave master_def master_defs master_file_def slave_until_opts
854
	repair restore backup analyze check start checksum
855
	field_list field_list_item field_spec kill column_def key_def
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
856
	keycache_list assign_to_keycache preload_list preload_keys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
857
	select_item_list select_item values_list no_braces
858
	opt_limit_clause delete_limit_clause fields opt_values values
bk@work.mysql.com's avatar
bk@work.mysql.com committed
859
	procedure_list procedure_list2 procedure_item
860
	when_list2 expr_list2 udf_expr_list3 handler
bk@work.mysql.com's avatar
bk@work.mysql.com committed
861 862
	opt_precision opt_ignore opt_column opt_restrict
	grant revoke set lock unlock string_list field_options field_option
863
	field_opt_list opt_binary table_lock_list table_lock
864
	ref_list opt_on_delete opt_on_delete_list opt_on_delete_item use
865
	opt_delete_options opt_delete_option varchar nchar nvarchar
866
	opt_outer table_list table_name opt_option opt_place
bk@work.mysql.com's avatar
bk@work.mysql.com committed
867
	opt_attribute opt_attribute_list attribute column_list column_list_id
868
	opt_column_list grant_privileges grant_ident grant_list grant_option
869
	object_privilege object_privilege_list user_list rename_list
870
	clear_privileges flush_options flush_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
871 872
	equal optional_braces opt_key_definition key_usage_list2
	opt_mi_check_type opt_to mi_check_types normal_join
873
	table_to_table_list table_to_table opt_table_list opt_as
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
874
	handler_rkey_function handler_read_or_scan
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
875
	single_multi table_wild_list table_wild_one opt_wild
876
	union_clause union_list
877
	precision subselect_start opt_and charset
878
	subselect_end select_var_list select_var_list_init help opt_len
879
	opt_extended_describe
880
        prepare prepare_src execute deallocate
881
	statement sp_suid
882
	sp_c_chistics sp_a_chistics sp_chistic sp_c_chistic xa
883
        load_data opt_field_or_var_spec fields_or_vars opt_load_data_set_spec
884 885 886
        definer view_replace_or_algorithm view_replace view_algorithm_opt
        view_algorithm view_or_trigger_tail view_suid view_tail view_list_opt
        view_list view_select view_check_option trigger_tail
887
        install uninstall partition_entry binlog_base64_event
888
END_OF_INPUT
bk@work.mysql.com's avatar
bk@work.mysql.com committed
889

890
%type <NONE> call sp_proc_stmts sp_proc_stmts1 sp_proc_stmt
891 892 893 894 895 896
%type <NONE> sp_proc_stmt_statement sp_proc_stmt_return
%type <NONE> sp_proc_stmt_if sp_proc_stmt_case_simple sp_proc_stmt_case
%type <NONE> sp_labeled_control sp_proc_stmt_unlabeled sp_proc_stmt_leave
%type <NONE> sp_proc_stmt_iterate sp_proc_stmt_label sp_proc_stmt_goto
%type <NONE> sp_proc_stmt_open sp_proc_stmt_fetch sp_proc_stmt_close

897 898 899 900 901 902
%type <num>  sp_decl_idents sp_opt_inout sp_handler_type sp_hcond_list
%type <spcondtype> sp_cond sp_hcond
%type <spblock> sp_decls sp_decl
%type <lex> sp_cursor_stmt
%type <spname> sp_name

bk@work.mysql.com's avatar
bk@work.mysql.com committed
903 904
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
905
	',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_SYM BETWEEN_SYM CASE_SYM
906
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM OR2_SYM AND_AND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
907 908 909 910 911
%%


query:
	END_OF_INPUT
912
	{
913
	   THD *thd= YYTHD;
914
	   if (!thd->bootstrap &&
915
	      (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
916
	   {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
917
	     my_message(ER_EMPTY_QUERY, ER(ER_EMPTY_QUERY), MYF(0));
918
	     YYABORT;
919
	   }
920 921
	   else
	   {
922
	     thd->lex->sql_command= SQLCOM_EMPTY_QUERY;
923
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
924
	}
925
	| verb_clause END_OF_INPUT {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
926 927

verb_clause:
928 929 930 931 932 933
	  statement
	| begin
	;

/* Verb clauses, except begin */
statement:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
934 935
	  alter
	| analyze
936
	| backup
937
	| binlog_base64_event
938
	| call
bk@work.mysql.com's avatar
bk@work.mysql.com committed
939 940
	| change
	| check
941
	| checksum
bk@work.mysql.com's avatar
bk@work.mysql.com committed
942 943
	| commit
	| create
944
        | deallocate
bk@work.mysql.com's avatar
bk@work.mysql.com committed
945 946
	| delete
	| describe
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
947
	| do
bk@work.mysql.com's avatar
bk@work.mysql.com committed
948
	| drop
949
        | execute
serg@serg.mylan's avatar
serg@serg.mylan committed
950
	| flush
bk@work.mysql.com's avatar
bk@work.mysql.com committed
951
	| grant
serg@serg.mylan's avatar
serg@serg.mylan committed
952 953
	| handler
	| help
bk@work.mysql.com's avatar
bk@work.mysql.com committed
954
	| insert
955
        | install
serg@serg.mylan's avatar
serg@serg.mylan committed
956
	| kill
bk@work.mysql.com's avatar
bk@work.mysql.com committed
957 958 959
	| load
	| lock
	| optimize
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
960
        | keycache
961
        | partition_entry
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
962
	| preload
963
        | prepare
964
	| purge
965
	| release
966
	| rename
serg@serg.mylan's avatar
serg@serg.mylan committed
967
	| repair
bk@work.mysql.com's avatar
bk@work.mysql.com committed
968
	| replace
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
969
	| reset
970
	| restore
bk@work.mysql.com's avatar
bk@work.mysql.com committed
971 972
	| revoke
	| rollback
973
	| savepoint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
974 975
	| select
	| set
serg@serg.mylan's avatar
serg@serg.mylan committed
976
	| show
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
977
	| slave
978
	| start
979
	| truncate
980
        | uninstall
bk@work.mysql.com's avatar
bk@work.mysql.com committed
981 982
	| unlock
	| update
983
	| use
984
	| xa
serg@serg.mylan's avatar
serg@serg.mylan committed
985
        ;
986

sergefp@mysql.com's avatar
sergefp@mysql.com committed
987
deallocate:
988
        deallocate_or_drop PREPARE_SYM ident
sergefp@mysql.com's avatar
sergefp@mysql.com committed
989 990
        {
          THD *thd=YYTHD;
991
          LEX *lex= thd->lex;
992
          if (lex->stmt_prepare_mode)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
993 994 995 996
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
997
          lex->sql_command= SQLCOM_DEALLOCATE_PREPARE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
998 999 1000
          lex->prepared_stmt_name= $3;
        };

1001 1002 1003 1004 1005 1006
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


sergefp@mysql.com's avatar
sergefp@mysql.com committed
1007
prepare:
1008
        PREPARE_SYM ident FROM prepare_src
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1009 1010
        {
          THD *thd=YYTHD;
1011
          LEX *lex= thd->lex;
1012
          if (lex->stmt_prepare_mode)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1013 1014 1015 1016
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
1017
          lex->sql_command= SQLCOM_PREPARE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1018 1019 1020
          lex->prepared_stmt_name= $2;
        };

1021 1022 1023 1024 1025 1026
prepare_src:
        TEXT_STRING_sys
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $1;
1027
          lex->prepared_stmt_code_is_varref= FALSE;
1028 1029 1030 1031 1032 1033
        }
        | '@' ident_or_text
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $2;
1034
          lex->prepared_stmt_code_is_varref= TRUE;
1035
        };
1036

sergefp@mysql.com's avatar
sergefp@mysql.com committed
1037 1038 1039 1040
execute:
        EXECUTE_SYM ident
        {
          THD *thd=YYTHD;
1041
          LEX *lex= thd->lex;
1042
          if (lex->stmt_prepare_mode)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1043 1044 1045 1046
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
1047
          lex->sql_command= SQLCOM_EXECUTE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
          lex->prepared_stmt_name= $2;
        }
        execute_using
        {}
        ;

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
1060 1061
        execute_var_list ',' execute_var_ident
        | execute_var_ident
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1062 1063 1064 1065 1066 1067 1068 1069
        ;

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;
1070
        }
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1071 1072
        ;

1073 1074
/* help */

1075
help:
1076 1077 1078 1079 1080 1081 1082 1083 1084
       HELP_SYM
       {
         if (Lex->sphead)
         {
           my_error(ER_SP_BADSTATEMENT, MYF(0), "HELP");
           YYABORT;
         }
       }
       ident_or_text
1085
       {
1086 1087
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
1088
	  lex->help_arg= $3.str;
1089
       };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1090 1091 1092 1093

/* change master */

change:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1094
       CHANGE MASTER_SYM TO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1095 1096 1097
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
1098
	  bzero((char*) &lex->mi, sizeof(lex->mi));
1099 1100 1101 1102
        }
       master_defs
	{}
       ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1103 1104 1105

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

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
master_def:
       MASTER_HOST_SYM EQ TEXT_STRING_sys
       {
	 Lex->mi.host = $3.str;
       }
       |
       MASTER_USER_SYM EQ TEXT_STRING_sys
       {
	 Lex->mi.user = $3.str;
       }
       |
       MASTER_PASSWORD_SYM EQ TEXT_STRING_sys
       {
	 Lex->mi.password = $3.str;
       }
       |
serg@serg.mylan's avatar
serg@serg.mylan committed
1124
       MASTER_PORT_SYM EQ ulong_num
1125 1126 1127 1128
       {
	 Lex->mi.port = $3;
       }
       |
serg@serg.mylan's avatar
serg@serg.mylan committed
1129
       MASTER_CONNECT_RETRY_SYM EQ ulong_num
1130 1131 1132
       {
	 Lex->mi.connect_retry = $3;
       }
serg@serg.mylan's avatar
serg@serg.mylan committed
1133
       | MASTER_SSL_SYM EQ ulong_num
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
         {
           Lex->mi.ssl= $3 ? 
               LEX_MASTER_INFO::SSL_ENABLE : LEX_MASTER_INFO::SSL_DISABLE;
         }
       | MASTER_SSL_CA_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_ca= $3.str;
         }
       | MASTER_SSL_CAPATH_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_capath= $3.str;
         }
       | MASTER_SSL_CERT_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_cert= $3.str;
         }
       | MASTER_SSL_CIPHER_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_cipher= $3.str;
         }
       | MASTER_SSL_KEY_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_key= $3.str;
	 }
       |
         master_file_def
       ;

master_file_def:
       MASTER_LOG_FILE_SYM EQ TEXT_STRING_sys
       {
	 Lex->mi.log_file_name = $3.str;
       }
       | MASTER_LOG_POS_SYM EQ ulonglong_num
         {
           Lex->mi.pos = $3;
           /* 
              If the user specified a value < BIN_LOG_HEADER_SIZE, adjust it
              instead of causing subsequent errors. 
              We need to do it in this file, because only there we know that 
              MASTER_LOG_POS has been explicitely specified. On the contrary
              in change_master() (sql_repl.cc) we cannot distinguish between 0
              (MASTER_LOG_POS explicitely specified as 0) and 0 (unspecified),
              whereas we want to distinguish (specified 0 means "read the binlog
              from 0" (4 in fact), unspecified means "don't change the position
              (keep the preceding value)").
           */
           Lex->mi.pos = max(BIN_LOG_HEADER_SIZE, Lex->mi.pos);
         }
       | RELAY_LOG_FILE_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.relay_log_name = $3.str;
         }
serg@serg.mylan's avatar
serg@serg.mylan committed
1187
       | RELAY_LOG_POS_SYM EQ ulong_num
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
         {
           Lex->mi.relay_log_pos = $3;
           /* Adjust if < BIN_LOG_HEADER_SIZE (same comment as Lex->mi.pos) */
           Lex->mi.relay_log_pos = max(BIN_LOG_HEADER_SIZE, Lex->mi.relay_log_pos);
         }
       ;

/* create a table */

create:
	CREATE opt_table_options TABLE_SYM opt_if_not_exists table_ident
	{
	  THD *thd= YYTHD;
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_CREATE_TABLE;
	  if (!lex->select_lex.add_table_to_list(thd, $5, NULL,
						 TL_OPTION_UPDATING,
						 (using_update_log ?
						  TL_READ_NO_INSERT:
						  TL_READ)))
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
	  lex->change=NullS;
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	  lex->create_info.options=$2 | $4;
1215
	  lex->create_info.db_type= lex->thd->variables.table_type;
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
	  lex->create_info.default_table_charset= NULL;
	  lex->name=0;
	}
	create2
	  { Lex->current_select= &Lex->select_lex; }
	| CREATE opt_unique_or_fulltext INDEX_SYM ident key_alg ON table_ident
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_CREATE_INDEX;
	    if (!lex->current_select->add_table_to_list(lex->thd, $7, NULL,
							TL_OPTION_UPDATING))
	      YYABORT;
	    lex->create_list.empty();
	    lex->key_list.empty();
	    lex->col_list.empty();
	    lex->change=NullS;
	  }
1233
	   '(' key_list ')' opt_fulltext_parser
1234 1235
	  {
	    LEX *lex=Lex;
1236 1237 1238 1239 1240 1241
	    if ($2 != Key::FULLTEXT && $12)
	    {
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
	    }
	    lex->key_list.push_back(new Key($2,$4.str,$5,0,lex->col_list,$12));
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
	    lex->col_list.empty();
	  }
	| CREATE DATABASE opt_if_not_exists ident
	  {
             Lex->create_info.default_table_charset= NULL;
             Lex->create_info.used_fields= 0;
          }
	  opt_create_database_options
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_CREATE_DB;
	    lex->name=$4.str;
            lex->create_info.options=$3;
	  }
	| CREATE udf_func_type FUNCTION_SYM sp_name
	  {
	    LEX *lex=Lex;
	    lex->spname= $4;
	    lex->udf.type= $2;
	  }
	  create_function_tail
	  {}
	| CREATE PROCEDURE sp_name
	  {
	    LEX *lex= Lex;
	    sp_head *sp;

	    if (lex->sphead)
	    {
1271
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "PROCEDURE");
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	      YYABORT;
	    }
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
	    sp->init(lex);

	    sp->m_type= TYPE_ENUM_PROCEDURE;
	    lex->sphead= sp;
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
andrey@lmy004's avatar
andrey@lmy004 committed
1286
            $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
	    YYTHD->client_capabilities &= (~CLIENT_MULTI_QUERIES);
	  }
          '('
	  {
	    LEX *lex= Lex;

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

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

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

	    if (sp->check_backpatch(YYTHD))
	      YYABORT;
	    sp->init_strings(YYTHD, lex, $3);
	    lex->sql_command= SQLCOM_CREATE_PROCEDURE;
andrey@lmy004's avatar
andrey@lmy004 committed
1319
            
1320 1321 1322 1323 1324 1325
	    /* 
              Restore flag if it was cleared above 
              Be careful with counting. the block where we save the value
              is $4.
            */
	    YYTHD->client_capabilities |= $<ulong_num>4;
1326 1327
	    sp->restore_thd_mem_root(YYTHD);
	  }
1328
	| CREATE EVENT_SYM opt_if_not_exists sp_name
1329 1330 1331 1332
          /* 
             BE CAREFUL when you add a new rule to update the block where 
             YYTHD->client_capabilities is set back to original value
          */
1333 1334 1335 1336 1337
          {
            LEX *lex=Lex;

            if (lex->et)
            {
andrey@lmy004's avatar
andrey@lmy004 committed
1338 1339 1340 1341
              /*
                Recursive events are not possible because recursive SPs
                are not also possible. lex->sp_head is not stacked.
              */
1342 1343 1344 1345 1346
              // ToDo Andrey : Change the error message
              my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "EVENT");
              YYABORT;
            }

andrey@lmy004's avatar
andrey@lmy004 committed
1347
            lex->create_info.options= $3;
1348
	    
andrey@lmy004's avatar
andrey@lmy004 committed
1349 1350
            if (!(lex->et= new event_timed())) // implicitly calls event_timed::init()
              YYABORT;
1351 1352 1353 1354 1355 1356
	    
            /*
              We have to turn of CLIENT_MULTI_QUERIES while parsing a
              stored procedure, otherwise yylex will chop it into pieces
              at each ';'.
            */
andrey@lmy004's avatar
andrey@lmy004 committed
1357 1358 1359 1360 1361
            $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
            YYTHD->client_capabilities &= (~CLIENT_MULTI_QUERIES);

            if (!lex->et_compile_phase) 
              lex->et->init_name(YYTHD, $4);
1362 1363 1364 1365 1366 1367 1368
          }
          ON SCHEDULE_SYM ev_schedule_time
          ev_on_completion
          ev_status
          ev_comment
          DO_SYM ev_sql_stmt
          {
andrey@lmy004's avatar
andrey@lmy004 committed
1369
            /*
1370 1371 1372 1373 1374 1375
              Restore flag if it was cleared above
              $1 - CREATE
              $2 - EVENT_SYM
              $3 - opt_if_not_exists
              $4 - sp_name
              $5 - the block above
andrey@lmy004's avatar
andrey@lmy004 committed
1376 1377
            */
            YYTHD->client_capabilities |= $<ulong_num>5;
1378

1379 1380 1381 1382
            /*
              sql_command is set here because some rules in ev_sql_stmt
              can overwrite it
            */
andrey@lmy004's avatar
andrey@lmy004 committed
1383
            Lex->sql_command= SQLCOM_CREATE_EVENT;
1384
          }
1385
	| CREATE
1386
	  {
1387 1388 1389
            Lex->create_view_mode= VIEW_CREATE_NEW;
            Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED;
            Lex->create_view_suid= TRUE;
1390
	  }
1391
	  view_or_trigger
1392
	  {}
1393 1394 1395
	| CREATE USER clear_privileges grant_list
	  {
	    Lex->sql_command = SQLCOM_CREATE_USER;
1396
          }
1397
	| CREATE LOGFILE_SYM GROUP logfile_group_info 
1398 1399
          {
            LEX *lex= Lex;
1400
            lex->alter_tablespace_info->ts_cmd_type= CREATE_LOGFILE_GROUP;
1401
          }
1402
        | CREATE TABLESPACE tablespace_info
1403 1404
          {
            LEX *lex= Lex;
1405
            lex->alter_tablespace_info->ts_cmd_type= CREATE_TABLESPACE;
1406
          }
1407
	;
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494


ev_schedule_time: EVERY_SYM expr interval
	  {
            LEX *lex=Lex;
            if (!lex->et_compile_phase)
            {
              switch (lex->et->init_interval(YYTHD , $2, $3)) {
              case EVEX_PARSE_ERROR:
                yyerror(ER(ER_SYNTAX_ERROR));
                YYABORT;
                break;
              case EVEX_BAD_PARAMS:
                my_error(ER_EVENT_INTERVAL_NOT_POSITIVE, MYF(0));
                YYABORT;
                break;
              }
            }
          }
          ev_starts
          ev_ends
        | AT_SYM expr
          {
            LEX *lex=Lex;
            if (!lex->et_compile_phase)
            {
              switch (lex->et->init_execute_at(YYTHD, $2)) {
              case EVEX_PARSE_ERROR:
                yyerror(ER(ER_SYNTAX_ERROR));
                YYABORT;  
                break;
              case EVEX_BAD_PARAMS:
                my_error(ER_EVENT_EXEC_TIME_IN_THE_PAST, MYF(0));
                YYABORT;
                break;             
              }
            }
          }
      ;
    
ev_status: /* empty */ {$<ulong_num>$= 0;}
        | ENABLED_SYM
          {
            LEX *lex=Lex;
            if (!lex->et_compile_phase)
              lex->et->status= MYSQL_EVENT_ENABLED;
            $<ulong_num>$= 1;	   
          }
        | DISABLED_SYM
          {
            LEX *lex=Lex;
            
            if (!lex->et_compile_phase)
              lex->et->status= MYSQL_EVENT_DISABLED;
            $<ulong_num>$= 1;
          }
      ;

ev_starts: /* empty */
        | STARTS_SYM expr
          {
            LEX *lex= Lex;
            if (!lex->et_compile_phase)
              lex->et->init_starts(YYTHD, $2);
          }
      ;

ev_ends: /* empty */
        | ENDS_SYM expr
          {
            LEX *lex= Lex;
            if (!lex->et_compile_phase)
            {
              switch (lex->et->init_ends(YYTHD, $2)) {
              case EVEX_PARSE_ERROR:
                yyerror(ER(ER_SYNTAX_ERROR));
                YYABORT;
                break;
              case EVEX_BAD_PARAMS:
                my_error(ER_EVENT_ENDS_BEFORE_STARTS, MYF(0));
                YYABORT;
                break;
              }
            }
          }
      ;

andrey@lmy004's avatar
andrey@lmy004 committed
1495
ev_on_completion: /* empty */ {$<ulong_num>$= 0;}
1496 1497 1498 1499
        | ON COMPLETION_SYM PRESERVE_SYM
          {
            LEX *lex=Lex;
            if (!lex->et_compile_phase)
andrey@lmy004's avatar
andrey@lmy004 committed
1500 1501
              lex->et->on_completion= MYSQL_EVENT_ON_COMPLETION_PRESERVE;
            $<ulong_num>$= 1;
1502 1503 1504 1505 1506
          }
        | ON COMPLETION_SYM NOT_SYM PRESERVE_SYM
          {
            LEX *lex=Lex;
            if (!lex->et_compile_phase)
andrey@lmy004's avatar
andrey@lmy004 committed
1507 1508
              lex->et->on_completion= MYSQL_EVENT_ON_COMPLETION_DROP;
            $<ulong_num>$= 1;
1509 1510
          }
      ;
1511

andrey@lmy004's avatar
andrey@lmy004 committed
1512
ev_comment: /* empty */ {$<ulong_num>$= 0;}
1513 1514 1515 1516 1517 1518 1519 1520
        | COMMENT_SYM TEXT_STRING_sys
          {
            LEX *lex= Lex;
            if (!lex->et_compile_phase)
            {
              lex->comment= $2;
              lex->et->init_comment(YYTHD, &$2);
            }
andrey@lmy004's avatar
andrey@lmy004 committed
1521
            $<ulong_num>$= 1;
1522 1523 1524 1525 1526 1527 1528
          }
      ;

ev_sql_stmt:
          {
            LEX *lex= Lex;
            sp_head *sp;
andrey@lmy004's avatar
andrey@lmy004 committed
1529 1530 1531 1532 1533 1534 1535
	    
            $<sphead>$= lex->sphead;
            
            if (!lex->sphead)
            {
              if (!(sp= new sp_head()))
                YYABORT;
1536

andrey@lmy004's avatar
andrey@lmy004 committed
1537 1538 1539 1540
              sp->reset_thd_mem_root(YYTHD);
              sp->init(lex);
            
              sp->m_type= TYPE_ENUM_PROCEDURE;
1541
            
andrey@lmy004's avatar
andrey@lmy004 committed
1542
              lex->sphead= sp;
1543

andrey@lmy004's avatar
andrey@lmy004 committed
1544 1545
              bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
              lex->sphead->m_chistics= &lex->sp_chistics;
1546
	
andrey@lmy004's avatar
andrey@lmy004 committed
1547 1548 1549
              lex->sphead->m_body_begin= lex->ptr;
            }

1550
            if (!lex->et_compile_phase)
1551
              lex->et->body_begin= lex->ptr;
1552 1553 1554 1555
          }
          ev_sql_stmt_inner
          {
            LEX *lex=Lex;
andrey@lmy004's avatar
andrey@lmy004 committed
1556 1557 1558 1559 1560 1561 1562
            
            if (!$<sphead>1)
            {
              sp_head *sp= lex->sphead;
              // return back to the original memory root ASAP
              sp->init_strings(YYTHD, lex, NULL);
              sp->restore_thd_mem_root(YYTHD);
1563
	    
andrey@lmy004's avatar
andrey@lmy004 committed
1564
              lex->sp_chistics.suid= SP_IS_SUID;//always the definer!
1565

andrey@lmy004's avatar
andrey@lmy004 committed
1566 1567 1568
              lex->et->sphead= lex->sphead;
              lex->sphead= NULL;
            }
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
            if (!lex->et_compile_phase)
            {
              lex->et->init_body(YYTHD);
              lex->et->init_definer(YYTHD);
            }
          }
      ;
	  
ev_sql_stmt_inner:
          sp_proc_stmt_statement
        | sp_proc_stmt_return
        | sp_proc_stmt_if
        | sp_proc_stmt_case_simple
        | sp_proc_stmt_case
        | sp_labeled_control {}
        | sp_proc_stmt_unlabeled
        | sp_proc_stmt_leave
        | sp_proc_stmt_iterate
        | sp_proc_stmt_label
        | sp_proc_stmt_goto
        | sp_proc_stmt_open
        | sp_proc_stmt_fetch
        | sp_proc_stmt_close
      ;
1593

1594

1595 1596 1597 1598 1599 1600 1601
clear_privileges:
        /* Nothing */
        {
          LEX *lex=Lex;
          lex->users_list.empty();
          lex->columns.empty();
          lex->grant= lex->grant_tot_col= 0;
1602
	  lex->all_privileges= 0;
1603 1604 1605 1606 1607 1608 1609
          lex->select_lex.db= 0;
          lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
          lex->ssl_cipher= lex->x509_subject= lex->x509_issuer= 0;
          bzero((char *)&(lex->mqh),sizeof(lex->mqh));
        }
        ;

1610
sp_name:
1611
	  ident '.' ident
1612 1613 1614 1615
	  {
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
1616
	| ident
1617 1618 1619 1620 1621 1622
	  {
	    $$= sp_name_current_db_new(YYTHD, $1);
	  }
	;

create_function_tail:
1623
	  RETURNS_SYM udf_type SONAME_SYM TEXT_STRING_sys
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
	  {
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_CREATE_FUNCTION;
	    lex->udf.name = lex->spname->m_name;
	    lex->udf.returns=(Item_result) $2;
	    lex->udf.dl=$4.str;
	  }
	| '('
	  {
	    LEX *lex= Lex;
	    sp_head *sp;

	    if (lex->sphead)
	    {
1638
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
	      YYABORT;
	    }
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
	    sp->init(lex);

	    sp->m_type= TYPE_ENUM_FUNCTION;
	    lex->sphead= sp;
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
andrey@lmy004's avatar
andrey@lmy004 committed
1653
            $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
	    YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
	    lex->sphead->m_param_begin= lex->tok_start+1;
	  }
          sp_fdparam_list ')'
	  {
	    LEX *lex= Lex;

	    lex->sphead->m_param_end= lex->tok_start;
	  }
	  RETURNS_SYM
	  {
	    LEX *lex= Lex;
1666 1667 1668 1669
	    lex->charset= NULL;
	    lex->length= lex->dec= NULL;
	    lex->interval_list.empty();
	    lex->type= 0;
1670 1671 1672 1673 1674
	  }
	  type
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
1675

1676 1677 1678 1679
            if (sp->fill_field_definition(YYTHD, lex,
                                          (enum enum_field_types) $8,
                                          &sp->m_return_field_def))
              YYABORT;
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694

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

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

1695 1696 1697
            if (sp->is_not_allowed_in_function("function"))
              YYABORT;

1698 1699 1700 1701
	    if (sp->check_backpatch(YYTHD))
	      YYABORT;
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
	    sp->init_strings(YYTHD, lex, lex->spname);
1702 1703 1704 1705 1706
            if (!(sp->m_flags & sp_head::HAS_RETURN))
            {
              my_error(ER_SP_NORETURN, MYF(0), sp->m_qname.str);
              YYABORT;
            }
1707
	    /* Restore flag if it was cleared above */
andrey@lmy004's avatar
andrey@lmy004 committed
1708
	    YYTHD->client_capabilities |= $<ulong_num>2;
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
	    sp->restore_thd_mem_root(YYTHD);
	  }
	;

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

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

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

/* Create characteristics */
sp_c_chistic:
	  sp_chistic            { }
	| DETERMINISTIC_SYM     { Lex->sp_chistics.detistic= TRUE; }
1745
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
	;

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

call:
	  CALL_SYM sp_name
	  {
	    LEX *lex = Lex;

	    lex->sql_command= SQLCOM_CALL;
	    lex->spname= $2;
	    lex->value_list.empty();
1767
	    sp_add_used_routine(lex, YYTHD, $2, TYPE_ENUM_PROCEDURE);
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
	  }
          '(' sp_cparam_list ')' {}
	;

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

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

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

sp_fdparams:
	  sp_fdparams ',' sp_fdparam
	| sp_fdparam
	;

1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
sp_init_param:
	  /* Empty */
	  {
	    LEX *lex= Lex;

	    lex->length= 0;
	    lex->dec= 0;
	    lex->type= 0;
	  
	    lex->default_value= 0;
	    lex->on_update_value= 0;
	  
	    lex->comment= null_lex_str;
	    lex->charset= NULL;
	  
	    lex->interval_list.empty();
	    lex->uint_geom_type= 0;
	  }
	;

1820
sp_fdparam:
1821
	  ident sp_init_param type
1822 1823 1824 1825 1826 1827
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
1828
	      my_error(ER_SP_DUP_PARAM, MYF(0), $1.str);
1829 1830
	      YYABORT;
	    }
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
	    sp_pvar_t *pvar= spc->push_pvar(&$1, (enum enum_field_types)$3,
                                            sp_param_in);

            if (lex->sphead->fill_field_definition(YYTHD, lex,
                                                   (enum enum_field_types) $3,
                                                   &pvar->field_def))
            {
              YYABORT;
            }
            pvar->field_def.field_name= pvar->name.str;
            pvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
1857
	  sp_opt_inout sp_init_param ident type
1858 1859 1860 1861
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1862
	    if (spc->find_pvar(&$3, TRUE))
1863
	    {
1864
	      my_error(ER_SP_DUP_PARAM, MYF(0), $3.str);
1865 1866
	      YYABORT;
	    }
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
	    sp_pvar_t *pvar= spc->push_pvar(&$3, (enum enum_field_types)$4,
			                    (sp_param_mode_t)$1);

            if (lex->sphead->fill_field_definition(YYTHD, lex,
                                                   (enum enum_field_types) $4,
                                                   &pvar->field_def))
            {
              YYABORT;
            }
            pvar->field_def.field_name= pvar->name.str;
            pvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
	  }
	;

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

sp_proc_stmts:
	  /* Empty */ {}
1890
	| sp_proc_stmts  sp_proc_stmt ';'
1891 1892 1893 1894
	;

sp_proc_stmts1:
	  sp_proc_stmt ';' {}
1895
	| sp_proc_stmts1  sp_proc_stmt ';'
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
	;

sp_decls:
	  /* Empty */
	  {
	    $$.vars= $$.conds= $$.hndlrs= $$.curs= 0;
	  }
	| sp_decls sp_decl ';'
	  {
	    /* We check for declarations out of (standard) order this way
	       because letting the grammar rules reflect it caused tricky
	       shift/reduce conflicts with the wrong result. (And we get
	       better error handling this way.) */
	    if (($2.vars || $2.conds) && ($1.curs || $1.hndlrs))
	    { /* Variable or condition following cursor or handler */
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1911 1912
	      my_message(ER_SP_VARCOND_AFTER_CURSHNDLR,
                         ER(ER_SP_VARCOND_AFTER_CURSHNDLR), MYF(0));
1913 1914 1915 1916
	      YYABORT;
	    }
	    if ($2.curs && $1.hndlrs)
	    { /* Cursor following handler */
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1917 1918
	      my_message(ER_SP_CURSOR_AFTER_HANDLER,
                         ER(ER_SP_CURSOR_AFTER_HANDLER), MYF(0));
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
	      YYABORT;
	    }
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
	    $$.curs= $1.curs + $2.curs;
	  }
	;

sp_decl:
1929
          DECLARE_SYM sp_decl_idents
1930
          {
1931
            LEX *lex= Lex;
1932

1933 1934 1935
            lex->sphead->reset_lex(YYTHD);
            lex->spcont->declare_var_boundary($2);
          }
1936
          type
1937
          sp_opt_default
1938 1939
          {
            LEX *lex= Lex;
1940 1941 1942 1943 1944 1945 1946
            sp_pcontext *pctx= lex->spcont;
            uint num_vars= pctx->context_pvars();
            enum enum_field_types var_type= (enum enum_field_types) $4;
            Item *dflt_value_item= $5;
            create_field *create_field_op;
            
            if (!dflt_value_item)
1947
            {
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
              dflt_value_item= new Item_null();
              /* QQ Set to the var_type with null_value? */
            }
            
            for (uint i = num_vars-$2 ; i < num_vars ; i++)
            {
              uint var_idx= pctx->pvar_context2index(i);
              sp_pvar_t *pvar= pctx->find_pvar(var_idx);
            
              if (!pvar)
                YYABORT;
            
              pvar->type= var_type;
              pvar->dflt= dflt_value_item;
            
              if (lex->sphead->fill_field_definition(YYTHD, lex, var_type,
                                                     &pvar->field_def))
              {
                YYABORT;
              }
            
              pvar->field_def.field_name= pvar->name.str;
              pvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
            
              /* The last instruction is responsible for freeing LEX. */

              lex->sphead->add_instr(
                new sp_instr_set(lex->sphead->instructions(), pctx, var_idx,
                                 dflt_value_item, var_type, lex,
                                 (i == num_vars - 1)));
1978
            }
1979 1980

            pctx->declare_var_boundary(0);
1981
            lex->sphead->restore_lex(YYTHD);
1982

1983 1984 1985
            $$.vars= $2;
            $$.conds= $$.hndlrs= $$.curs= 0;
          }
1986 1987 1988 1989 1990 1991 1992
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1993
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
	      YYABORT;
	    }
	    YYTHD->lex->spcont->push_cond(&$2, $5);
	    $$.vars= $$.hndlrs= $$.curs= 0;
	    $$.conds= 1;
	  }
	| DECLARE_SYM sp_handler_type HANDLER_SYM FOR_SYM
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_instr_hpush_jump *i=
              new sp_instr_hpush_jump(sp->instructions(), ctx, $2,
	                              ctx->current_pvars());

	    sp->add_instr(i);
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
2011
	    sp->m_flags|= sp_head::IN_HANDLER;
2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
	  }
	  sp_hcond_list sp_proc_stmt
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *hlab= lex->spcont->pop_label(); /* After this hdlr */
	    sp_instr_hreturn *i;

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

	      sp->add_instr(i);
	      sp->push_backpatch(i, lex->spcont->last_label()); /* Block end */
	    }
	    lex->sphead->backpatch(hlab);
2035
	    sp->m_flags&= ~sp_head::IN_HANDLER;
2036 2037
	    $$.vars= $$.conds= $$.curs= 0;
	    $$.hndlrs= $6;
2038
	    ctx->add_handlers($6);
2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
	  }
	| DECLARE_SYM ident CURSOR_SYM FOR_SYM sp_cursor_stmt
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint offp;
	    sp_instr_cpush *i;

	    if (ctx->find_cursor(&$2, &offp, TRUE))
	    {
2050
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
2051 2052 2053
	      delete $5;
	      YYABORT;
	    }
pem@mysql.com's avatar
pem@mysql.com committed
2054 2055
            i= new sp_instr_cpush(sp->instructions(), ctx, $5,
                                  ctx->current_cursors());
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
	    sp->add_instr(i);
	    ctx->push_cursor(&$2);
	    $$.vars= $$.conds= $$.hndlrs= 0;
	    $$.curs= 1;
	  }
	;

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

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

	    if (lex->sql_command != SQLCOM_SELECT)
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2078 2079
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
2080 2081 2082 2083
	      YYABORT;
	    }
	    if (lex->result)
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2084 2085
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
	      YYABORT;
	    }
	    lex->sp_lex_in_use= TRUE;
	    $$= lex;
	    lex->sphead->restore_lex(YYTHD);
	  }
	;

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

sp_hcond_list:
	  sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
	    sp_pcontext *ctx= lex->spcont;

	    if (ctx->find_handler($1))
	    {
	      my_message(ER_SP_DUP_HANDLER, ER(ER_SP_DUP_HANDLER), MYF(0));
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_hpush_jump *i=
                (sp_instr_hpush_jump *)sp->last_instruction();
2116

2117 2118 2119 2120
	      i->add_condition($1);
	      ctx->push_handler($1);
	      $$= 1;
	    }
2121 2122 2123 2124 2125
	  }
	| sp_hcond_list ',' sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
	    sp_pcontext *ctx= lex->spcont;

	    if (ctx->find_handler($3))
	    {
	      my_message(ER_SP_DUP_HANDLER, ER(ER_SP_DUP_HANDLER), MYF(0));
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_hpush_jump *i=
	        (sp_instr_hpush_jump *)sp->last_instruction();
2137

2138 2139 2140 2141
	      i->add_condition($3);
	      ctx->push_handler($3);
	      $$= $1 + 1;
	    }
2142 2143 2144 2145
	  }
	;

sp_cond:
serg@serg.mylan's avatar
serg@serg.mylan committed
2146
	  ulong_num
2147 2148 2149 2150 2151 2152 2153
	  {			/* mysql errno */
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::number;
	    $$->mysqlerr= $1;
	  }
	| SQLSTATE_SYM opt_value TEXT_STRING_literal
	  {		/* SQLSTATE */
2154 2155 2156 2157 2158
	    if (!sp_cond_check(&$3))
	    {
	      my_error(ER_SP_BAD_SQLSTATE, MYF(0), $3.str);
	      YYABORT;
	    }
2159 2160
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
2161 2162
	    memcpy($$->sqlstate, $3.str, 5);
	    $$->sqlstate[5]= '\0';
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
	  }
	;

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
2181
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
2182 2183 2184 2185 2186 2187 2188 2189
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
2190
	| not FOUND_SYM		/* SQLSTATEs 02??? */
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::notfound;
	  }
	| SQLEXCEPTION_SYM	/* All other SQLSTATEs */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::exception;
	  }
	;

sp_decl_idents:
	  ident
	  {
2205 2206
            /* NOTE: field definition is filled in sp_decl section. */

2207 2208 2209 2210 2211
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
2212
	      my_error(ER_SP_DUP_VAR, MYF(0), $1.str);
2213 2214 2215 2216 2217 2218 2219
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum_field_types)0, sp_param_in);
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
2220 2221
            /* NOTE: field definition is filled in sp_decl section. */

2222 2223 2224 2225 2226
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$3, TRUE))
	    {
2227
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
	      YYABORT;
	    }
	    spc->push_pvar(&$3, (enum_field_types)0, sp_param_in);
	    $$= $1 + 1;
	  }
	;

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

sp_proc_stmt:
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
	  sp_proc_stmt_statement
        | sp_proc_stmt_return
	| sp_proc_stmt_if
	| sp_proc_stmt_case_simple
        | sp_proc_stmt_case
	| sp_labeled_control
	  {}
	| sp_proc_stmt_unlabeled
	| sp_proc_stmt_leave
	| sp_proc_stmt_iterate
	| sp_proc_stmt_label
	| sp_proc_stmt_goto
	| sp_proc_stmt_open
	| sp_proc_stmt_fetch
        | sp_proc_stmt_close
        ;

sp_proc_stmt_if:
        IF sp_if END IF {}
        ;
        
sp_proc_stmt_statement:
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
	  {
	    LEX *lex= Lex;

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

2274
            sp->m_flags|= sp_get_flags_for_command(lex);
2275 2276
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
2277
	      my_error(ER_SP_BADSTATEMENT, MYF(0), "USE");
2278 2279
	      YYABORT;
	    }
2280 2281 2282 2283
	    /*
              Don't add an instruction for SET statements, since all
              instructions for them were already added during processing
              of "set" rule.
2284
	    */
2285 2286 2287
            DBUG_ASSERT(lex->sql_command != SQLCOM_SET_OPTION ||
                        lex->var_list.is_empty());
            if (lex->sql_command != SQLCOM_SET_OPTION)
2288
	    {
2289 2290
              sp_instr_stmt *i=new sp_instr_stmt(sp->instructions(),
                                                 lex->spcont, lex);
2291

2292
              /* Extract the query statement from the tokenizer:
2293
                 The end is either lex->tok_end or tok->ptr. */
2294 2295 2296 2297 2298 2299 2300 2301
              if (lex->ptr - lex->tok_end > 1)
                i->m_query.length= lex->ptr - sp->m_tmp_query;
              else
                i->m_query.length= lex->tok_end - sp->m_tmp_query;
              i->m_query.str= strmake_root(YYTHD->mem_root,
                                           (char *)sp->m_tmp_query,
                                           i->m_query.length);
              sp->add_instr(i);
2302 2303 2304
            }
	    sp->restore_lex(YYTHD);
          }
2305 2306 2307 2308
        ;

sp_proc_stmt_return:
        RETURN_SYM 
2309 2310
          { Lex->sphead->reset_lex(YYTHD); }
          expr
2311 2312
	  {
	    LEX *lex= Lex;
2313
	    sp_head *sp= lex->sphead;
2314

2315
	    if (sp->m_type == TYPE_ENUM_PROCEDURE)
2316
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2317
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
2318 2319 2320 2321 2322 2323
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_freturn *i;

2324 2325
	      i= new sp_instr_freturn(sp->instructions(), lex->spcont, $3,
                                      sp->m_return_field_def.sql_type, lex);
2326
	      sp->add_instr(i);
2327
	      sp->m_flags|= sp_head::HAS_RETURN;
2328
	    }
2329
	    sp->restore_lex(YYTHD);
2330
	  }
2331 2332 2333 2334
        ;

sp_proc_stmt_case_simple:
	CASE_SYM WHEN_SYM
2335
	  {
2336
	    Lex->sphead->m_flags&= ~sp_head::IN_SIMPLE_CASE;
2337 2338
	  }
	  sp_case END CASE_SYM {}
2339 2340 2341 2342
        ;
        
sp_proc_stmt_case:
          CASE_SYM
2343 2344
          { Lex->sphead->reset_lex(YYTHD); }
          expr WHEN_SYM
2345 2346
	  {
	    LEX *lex= Lex;
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
	    sp_head *sp= lex->sphead;
	    sp_pcontext *parsing_ctx= lex->spcont;
	    int case_expr_id= parsing_ctx->register_case_expr();
	    
	    if (parsing_ctx->push_case_expr_id(case_expr_id))
              YYABORT;
	    
	    sp->add_instr(
	      new sp_instr_set_case_expr(sp->instructions(),
	                                 parsing_ctx,
	                                 case_expr_id,
	                                 $3,
	                                 lex));
	    
	    sp->m_flags|= sp_head::IN_SIMPLE_CASE;
	    sp->restore_lex(YYTHD);
2363 2364 2365
	  }
	  sp_case END CASE_SYM
	  {
2366
	    Lex->spcont->pop_case_expr_id();
2367
	  }
2368 2369 2370 2371
        ;

sp_proc_stmt_unlabeled:
	  { /* Unlabeled controls get a secret label. */
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
	    LEX *lex= Lex;

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

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
2382 2383 2384 2385
        ;

sp_proc_stmt_leave:
	  LEAVE_SYM label_ident
2386 2387 2388 2389 2390 2391 2392 2393
	  {
	    LEX *lex= Lex;
	    sp_head *sp = lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab)
	    {
2394
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414
	      YYABORT;
	    }
	    else
	    {
	      uint ip= sp->instructions();
	      sp_instr_jump *i;
	      sp_instr_hpop *ih;
	      sp_instr_cpop *ic;

	      ih= new sp_instr_hpop(ip++, ctx, 0);
	      sp->push_backpatch(ih, lab);
	      sp->add_instr(ih);
	      ic= new sp_instr_cpop(ip++, ctx, 0);
	      sp->push_backpatch(ic, lab);
	      sp->add_instr(ic);
	      i= new sp_instr_jump(ip, ctx);
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
	    }
	  }
2415 2416 2417 2418
        ;

sp_proc_stmt_iterate:
	  ITERATE_SYM label_ident
2419 2420 2421 2422 2423 2424 2425 2426
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab || lab->type != SP_LAB_ITER)
	    {
2427
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "ITERATE", $2.str);
2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_jump *i;
	      uint ip= sp->instructions();
	      uint n;

	      n= ctx->diff_handlers(lab->ctx);
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
	      n= ctx->diff_cursors(lab->ctx);
	      if (n)
	        sp->add_instr(new sp_instr_cpop(ip++, ctx, n));
	      i= new sp_instr_jump(ip, ctx, lab->ip); /* Jump back */
              sp->add_instr(i);
	    }
	  }
2446 2447 2448 2449
        ;

sp_proc_stmt_label:
	  LABEL_SYM IDENT
2450
	  {
2451
#ifdef SP_GOTO
2452 2453 2454 2455 2456 2457 2458
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (lab)
	    {
2459
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $2.str);
2460 2461 2462 2463 2464 2465 2466 2467 2468
	      YYABORT;
	    }
	    else
	    {
	      lab= ctx->push_label($2.str, sp->instructions());
	      lab->type= SP_LAB_GOTO;
	      lab->ctx= ctx;
              sp->backpatch(lab);
	    }
2469 2470 2471 2472
#else
	    yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
#endif
2473
	  }
2474 2475 2476 2477
        ;

sp_proc_stmt_goto:
	  GOTO_SYM IDENT
2478
	  {
2479
#ifdef SP_GOTO
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab;
	    sp_instr_jump *i;
	    sp_instr_hpop *ih;
	    sp_instr_cpop *ic;

	    if (sp->m_in_handler)
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2491
	      my_message(ER_SP_GOTO_IN_HNDLR, ER(ER_SP_GOTO_IN_HNDLR), MYF(0));
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
	      YYABORT;
	    }
	    lab= ctx->find_label($2.str);
	    if (! lab)
	    {
	      lab= (sp_label_t *)YYTHD->alloc(sizeof(sp_label_t));
	      lab->name= $2.str;
	      lab->ip= 0;
	      lab->type= SP_LAB_REF;
	      lab->ctx= ctx;

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

	      n= ctx->diff_handlers(lab->ctx);
	      if (n)
	      {
	        ih= new sp_instr_hpop(ip++, ctx, n);
	        sp->add_instr(ih);
	      }
	      n= ctx->diff_cursors(lab->ctx);
	      if (n)
	      {
	        ic= new sp_instr_cpop(ip++, ctx, n);
	        sp->add_instr(ic);
	      }
	      i= new sp_instr_jump(ip, ctx, lab->ip); /* Jump back */
	      sp->add_instr(i);
	    }
2532 2533 2534 2535
#else
	    yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
#endif
2536
	  }
2537 2538 2539 2540
        ;

sp_proc_stmt_open:
	  OPEN_SYM ident
2541 2542 2543 2544 2545 2546 2547 2548
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_copen *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2549
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2550 2551 2552 2553 2554
	      YYABORT;
	    }
	    i= new sp_instr_copen(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
2555 2556 2557 2558
        ;

sp_proc_stmt_fetch:
	  FETCH_SYM sp_opt_fetch_noise ident INTO
2559 2560 2561 2562 2563 2564 2565 2566
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cfetch *i;

	    if (! lex->spcont->find_cursor(&$3, &offset))
	    {
2567
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $3.str);
2568 2569 2570 2571 2572 2573 2574
	      YYABORT;
	    }
	    i= new sp_instr_cfetch(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
	  sp_fetch_list
	  { }
2575 2576 2577 2578
        ;

sp_proc_stmt_close:
	  CLOSE_SYM ident
2579 2580 2581 2582 2583 2584 2585 2586
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cclose *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2587
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
	      YYABORT;
	    }
	    i= new sp_instr_cclose(sp->instructions(), lex->spcont,  offset);
	    sp->add_instr(i);
	  }
	;

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

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

	    if (!spc || !(spv = spc->find_pvar(&$1)))
	    {
2611
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	    }
	  }
	|
	  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)))
	    {
2632
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $3.str);
2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	    }
	  }
	;

sp_if:
2646 2647
          { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2648 2649 2650 2651 2652
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= sp->instructions();
2653 2654
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, ctx,
                                                               $2, lex);
2655 2656 2657

	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
            sp->add_instr(i);
2658
            sp->restore_lex(YYTHD);
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685
	  }
	  sp_proc_stmts1
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump *i = new sp_instr_jump(ip, ctx);

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

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

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

sp_case:
2686 2687
	  { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2688 2689 2690 2691 2692 2693 2694
	  {
            LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

2695
	    if (! (sp->m_flags & sp_head::IN_SIMPLE_CASE))
2696
	      i= new sp_instr_jump_if_not(ip, ctx, $2, lex);
2697 2698 2699
	    else
	    { /* Simple case: <caseval> = <whenval> */

2700 2701 2702 2703 2704
	      Item_case_expr *var;
              Item *expr;

              var= new Item_case_expr(ctx->get_current_case_expr_id());

2705
#ifndef DBUG_OFF
2706
              if (var)
2707
                var->m_sp= sp;
2708
#endif
2709 2710

	      expr= new Item_func_eq(var, $2);
2711

2712
	      i= new sp_instr_jump_if_not(ip, ctx, expr, lex);
2713 2714 2715
	    }
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
            sp->add_instr(i);
2716
            sp->restore_lex(YYTHD);
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
	  }
	  sp_proc_stmts1
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump *i = new sp_instr_jump(ip, ctx);

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

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2736

2737 2738 2739 2740 2741 2742 2743
sp_whens:
	  /* Empty */
	  {
	    sp_head *sp= Lex->sphead;
	    uint ip= sp->instructions();
	    sp_instr_error *i= new sp_instr_error(ip, Lex->spcont,
						  ER_SP_CASE_NOT_FOUND);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2744

2745 2746 2747 2748 2749
	    sp->add_instr(i);
	  }
	| ELSE sp_proc_stmts1 {}
	| WHEN_SYM sp_case {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2750

2751
sp_labeled_control:
2752
	  label_ident ':'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2753
	  {
2754 2755 2756 2757 2758 2759
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);

	    if (lab)
	    {
2760
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $1.str);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2761
	      YYABORT;
2762 2763 2764 2765 2766 2767 2768
	    }
	    else
	    {
	      lab= lex->spcont->push_label($1.str,
	                                   lex->sphead->instructions());
	      lab->type= SP_LAB_ITER;
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2769
	  }
2770
	  sp_unlabeled_control sp_opt_label
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2771
	  {
2772
	    LEX *lex= Lex;
2773

2774 2775 2776 2777 2778 2779 2780
	    if ($5.str)
	    {
	      sp_label_t *lab= lex->spcont->find_label($5.str);

	      if (!lab ||
	          my_strcasecmp(system_charset_info, $5.str, lab->name) != 0)
	      {
2781
	        my_error(ER_SP_LABEL_MISMATCH, MYF(0), $5.str);
2782 2783 2784 2785
	        YYABORT;
	      }
	    }
	    lex->sphead->backpatch(lex->spcont->pop_label());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2786
	  }
2787 2788 2789
	;

sp_opt_label:
2790
        /* Empty  */    { $$= null_lex_str; }
2791
        | label_ident   { $$= $1; }
2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807
	;

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

	    lab->type= SP_LAB_BEGIN;
	    lex->spcont= lex->spcont->push_context();
	  }
	  sp_decls
	  sp_proc_stmts
	  END
2808
	  {
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;

  	    sp->backpatch(ctx->last_label());	/* We always have a label */
	    if ($3.hndlrs)
	      sp->add_instr(new sp_instr_hpop(sp->instructions(), ctx,
					      $3.hndlrs));
	    if ($3.curs)
	      sp->add_instr(new sp_instr_cpop(sp->instructions(), ctx,
					      $3.curs));
	    lex->spcont= ctx->pop_context();
	  }
	| LOOP_SYM
	  sp_proc_stmts1 END LOOP_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2824
	  {
2825 2826 2827 2828 2829 2830
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lex->spcont, lab->ip);

	    lex->sphead->add_instr(i);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2831
	  }
2832 2833 2834
        | WHILE_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr DO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2835
	  {
2836 2837 2838 2839
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, lex->spcont,
2840
							       $3, lex);
2841 2842 2843 2844

	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
            sp->add_instr(i);
2845
            sp->restore_lex(YYTHD);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2846
	  }
2847
	  sp_proc_stmts1 END WHILE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2848
	  {
2849 2850 2851 2852 2853 2854 2855
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump *i = new sp_instr_jump(ip, lex->spcont, lab->ip);

	    lex->sphead->add_instr(i);
	  }
2856 2857 2858
        | REPEAT_SYM sp_proc_stmts1 UNTIL_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr END REPEAT_SYM
2859 2860 2861 2862 2863
	  {
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, lex->spcont,
2864 2865
                                                               $5, lab->ip,
                                                               lex);
2866
            lex->sphead->add_instr(i);
2867
            lex->sphead->restore_lex(YYTHD);
2868
	  }
2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884
	;

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

trg_event:
            INSERT 
            { Lex->trg_chistics.event= TRG_EVENT_INSERT; }
          | UPDATE_SYM
            { Lex->trg_chistics.event= TRG_EVENT_UPDATE; }
          | DELETE_SYM
            { Lex->trg_chistics.event= TRG_EVENT_DELETE; }
2885
          ;
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261
/*
  This part of the parser contains common code for all TABLESPACE
  commands.
  CREATE TABLESPACE name ...
  ALTER TABLESPACE name CHANGE DATAFILE ...
  ALTER TABLESPACE name ADD DATAFILE ...
  ALTER TABLESPACE name access_mode
  CREATE LOGFILE GROUP name ...
  ALTER LOGFILE GROUP name ADD UNDOFILE ..
  ALTER LOGFILE GROUP name ADD REDOFILE ..
  DROP TABLESPACE name
  DROP LOGFILE GROUP name
*/
change_tablespace_access:
          tablespace_name
          ts_access_mode
          ;

change_tablespace_info:
          tablespace_name
          CHANGE ts_datafile
          change_ts_option_list
          ;

tablespace_info:
          tablespace_name
          ADD ts_datafile
          opt_logfile_group_name
          tablespace_option_list
          ;

opt_logfile_group_name:
          /* empty */ {}
          | USE_SYM LOGFILE_SYM GROUP ident
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->logfile_group_name= $4.str;
          };

alter_tablespace_info:
          tablespace_name
          ADD ts_datafile
          alter_tablespace_option_list 
	  { 
	    Lex->alter_tablespace_info->ts_alter_tablespace_type= ALTER_TABLESPACE_ADD_FILE; 
          }
          |
          tablespace_name
          DROP ts_datafile
          alter_tablespace_option_list 
	  { 
	    Lex->alter_tablespace_info->ts_alter_tablespace_type= ALTER_TABLESPACE_DROP_FILE; 
          };

logfile_group_info:
          logfile_group_name
          add_log_file
          logfile_group_option_list
          ;

alter_logfile_group_info:
          logfile_group_name
          add_log_file
          alter_logfile_group_option_list
          ;

add_log_file:
          ADD lg_undofile
          | ADD lg_redofile
          ;

change_ts_option_list:
          /* empty */ {}
          change_ts_options
          ;

change_ts_options:
          change_ts_option
          | change_ts_options change_ts_option
          | change_ts_options ',' change_ts_option
          ;

change_ts_option:
          opt_ts_initial_size
          | opt_ts_autoextend_size
          | opt_ts_max_size
          ;

tablespace_option_list:
          /* empty */ {}
          tablespace_options
          ;

tablespace_options:
          tablespace_option
          | tablespace_options tablespace_option
          | tablespace_options ',' tablespace_option
          ;

tablespace_option:
          opt_ts_initial_size
          | opt_ts_autoextend_size
          | opt_ts_max_size
          | opt_ts_extent_size
          | opt_ts_nodegroup
          | opt_ts_engine
          | ts_wait
          | opt_ts_comment
          ;

alter_tablespace_option_list:
          /* empty */ {}
          alter_tablespace_options
          ;

alter_tablespace_options:
          alter_tablespace_option
          | alter_tablespace_options alter_tablespace_option
          | alter_tablespace_options ',' alter_tablespace_option
          ;

alter_tablespace_option:
          opt_ts_initial_size
          | opt_ts_autoextend_size
          | opt_ts_max_size
          | opt_ts_engine
          | ts_wait
          ;

logfile_group_option_list:
          /* empty */ {}
          logfile_group_options
          ;

logfile_group_options:
          logfile_group_option
          | logfile_group_options logfile_group_option
          | logfile_group_options ',' logfile_group_option
          ;

logfile_group_option:
          opt_ts_initial_size
          | opt_ts_undo_buffer_size
          | opt_ts_redo_buffer_size
          | opt_ts_nodegroup
          | opt_ts_engine
          | ts_wait
          | opt_ts_comment
          ;

alter_logfile_group_option_list:
          /* empty */ {}
          alter_logfile_group_options
          ;

alter_logfile_group_options:
          alter_logfile_group_option
          | alter_logfile_group_options alter_logfile_group_option
          | alter_logfile_group_options ',' alter_logfile_group_option
          ;

alter_logfile_group_option:
          opt_ts_initial_size
          | opt_ts_engine
          | ts_wait
          ;


ts_datafile:
          DATAFILE_SYM TEXT_STRING_sys
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->data_file_name= $2.str;
          };

lg_undofile:
          UNDOFILE_SYM TEXT_STRING_sys
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->undo_file_name= $2.str;
          };

lg_redofile:
          REDOFILE_SYM TEXT_STRING_sys
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->redo_file_name= $2.str;
          };

tablespace_name:
          ident
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info= new st_alter_tablespace();
            lex->alter_tablespace_info->tablespace_name= $1.str;
            lex->sql_command= SQLCOM_ALTER_TABLESPACE;
          };

logfile_group_name:
          ident
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info= new st_alter_tablespace();
            lex->alter_tablespace_info->logfile_group_name= $1.str;
            lex->sql_command= SQLCOM_ALTER_TABLESPACE;
          };

ts_access_mode:
          READ_ONLY_SYM
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_access_mode= TS_READ_ONLY;
          }
          | READ_WRITE_SYM
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_access_mode= TS_READ_WRITE;
          }
          | NOT_SYM ACCESSIBLE_SYM
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_access_mode= TS_NOT_ACCESSIBLE;
          };

opt_ts_initial_size:
          INITIAL_SIZE_SYM opt_equal size_number
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->initial_size= $3;
          };

opt_ts_autoextend_size:
          AUTOEXTEND_SIZE_SYM opt_equal size_number
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->autoextend_size= $3;
          };

opt_ts_max_size:
          MAX_SIZE_SYM opt_equal size_number
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->max_size= $3;
          };

opt_ts_extent_size:
          EXTENT_SIZE_SYM opt_equal size_number
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->extent_size= $3;
          };

opt_ts_undo_buffer_size:
          UNDO_BUFFER_SIZE_SYM opt_equal size_number
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->undo_buffer_size= $3;
          };

opt_ts_redo_buffer_size:
          REDO_BUFFER_SIZE_SYM opt_equal size_number
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->redo_buffer_size= $3;
          };

opt_ts_nodegroup:
          NODEGROUP_SYM opt_equal ulong_num
          {
            LEX *lex= Lex;
            if (lex->alter_tablespace_info->nodegroup_id != UNDEF_NODEGROUP)
            {
              my_error(ER_TABLESPACE_OPTION_ONLY_ONCE,MYF(0),"NODEGROUP");
              YYABORT;
            }
            lex->alter_tablespace_info->nodegroup_id= $3;
          };

opt_ts_comment:
          COMMENT_SYM opt_equal TEXT_STRING_sys
          {
            LEX *lex= Lex;
            if (lex->alter_tablespace_info->ts_comment != NULL)
            {
              my_error(ER_TABLESPACE_OPTION_ONLY_ONCE,MYF(0),"COMMENT");
              YYABORT;
            }
            lex->alter_tablespace_info->ts_comment= $3.str;
          };

opt_ts_engine:
          opt_storage ENGINE_SYM opt_equal storage_engines
          {
            LEX *lex= Lex;
            if (lex->alter_tablespace_info->storage_engine != DB_TYPE_UNKNOWN)
            {
              my_error(ER_TABLESPACE_OPTION_ONLY_ONCE,MYF(0),
                       "STORAGE ENGINE");
              YYABORT;
            }
            lex->alter_tablespace_info->storage_engine= $4->db_type;
          };

opt_ts_wait:
          /* empty */ 
          | ts_wait
          ;

ts_wait:
          WAIT_SYM
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->wait_until_completed= TRUE;
          }
          | NO_WAIT_SYM
          {
            LEX *lex= Lex;
            if (!(lex->alter_tablespace_info->wait_until_completed))
            {
              my_error(ER_TABLESPACE_OPTION_ONLY_ONCE,MYF(0),"NO_WAIT");
              YYABORT;
            }
            lex->alter_tablespace_info->wait_until_completed= FALSE;
          };

size_number:
          ulong_num { $$= $1;}
          | IDENT
          {
            ulonglong number, test_number;
            uint text_shift_number= 0;
            longlong prefix_number;
            char *end_ptr;
            char *start_ptr= $1.str;
            uint str_len= strlen(start_ptr);
            int error;
            prefix_number= my_strtoll10(start_ptr, &end_ptr, &error);
            if ((start_ptr + str_len - 1) == end_ptr)
            {
              switch (end_ptr[0])
              {
                case 'g':
                case 'G':
                  text_shift_number+=10;
                case 'm':
                case 'M':
                  text_shift_number+=10;
                case 'k':
                case 'K':
                  text_shift_number+=10;
                  break;
                default:
                {
                  my_error(ER_WRONG_SIZE_NUMBER, MYF(0));
                  YYABORT;
                }
              }
              if (prefix_number >> 31)
              {
                my_error(ER_SIZE_OVERFLOW_ERROR, MYF(0));
                YYABORT;
              }
              number= prefix_number << text_shift_number;
            }
            else
            {
              my_error(ER_WRONG_SIZE_NUMBER, MYF(0));
              YYABORT;
            }
            $$= number;
          }
          ;

/*
  End tablespace part
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3262 3263

create2:
serg@serg.mylan's avatar
serg@serg.mylan committed
3264
        '(' create2a {}
3265 3266 3267
        | opt_create_table_options
          opt_partitioning {} 
          create3 {}
serg@serg.mylan's avatar
serg@serg.mylan committed
3268 3269 3270 3271
        | LIKE table_ident
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$2))
venu@myvenu.com's avatar
venu@myvenu.com committed
3272
              YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
3273 3274 3275 3276 3277
          }
        | '(' LIKE table_ident ')'
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$3))
3278
              YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
3279
          }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3280
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3281

serg@serg.mylan's avatar
serg@serg.mylan committed
3282
create2a:
3283 3284 3285 3286 3287 3288
        field_list ')' opt_create_table_options
          opt_partitioning {}
          create3 {}
        |  opt_partitioning {}
           create_select ')'
           { Select->set_braces(1);} union_opt {}
3289
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3290 3291

create3:
3292
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
3293
	| opt_duplicate opt_as     create_select
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3294
          { Select->set_braces(0);} union_clause {}
serg@serg.mylan's avatar
serg@serg.mylan committed
3295
	| opt_duplicate opt_as '(' create_select ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3296
          { Select->set_braces(1);} union_opt {}
3297 3298
        ;

3299 3300 3301
/*
 This part of the parser is about handling of the partition information.

3302
 It's first version was written by Mikael Ronström with lots of answers to
3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329
 questions provided by Antony Curtis.

 The partition grammar can be called from three places.
 1) CREATE TABLE ... PARTITION ..
 2) ALTER TABLE table_name PARTITION ...
 3) PARTITION ...

 The first place is called when a new table is created from a MySQL client.
 The second place is called when a table is altered with the ALTER TABLE
 command from a MySQL client.
 The third place is called when opening an frm file and finding partition
 info in the .frm file. It is necessary to avoid allowing PARTITION to be
 an allowed entry point for SQL client queries. This is arranged by setting
 some state variables before arriving here.

 To be able to handle errors we will only set error code in this code
 and handle the error condition in the function calling the parser. This
 is necessary to ensure we can also handle errors when calling the parser
 from the openfrm function.
*/
opt_partitioning:
        /* empty */ {}
        | partitioning
        ;

partitioning:
        PARTITION_SYM
3330 3331 3332 3333 3334
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
3335
            mem_alloc_error(sizeof(partition_info));
3336 3337
            YYABORT;
          }
3338 3339 3340 3341
          if (lex->sql_command == SQLCOM_ALTER_TABLE)
          {
            lex->alter_info.flags|= ALTER_PARTITION;
          }
3342
        }
3343 3344 3345 3346 3347 3348 3349
        partition
        ;

partition_entry:
        PARTITION_SYM
        {
          LEX *lex= Lex;
3350
          if (!lex->part_info)
3351 3352 3353 3354
          {
            yyerror(ER(ER_PARTITION_ENTRY_ERROR));
            YYABORT;
          }
3355 3356 3357 3358
          /*
            We enter here when opening the frm file to translate
            partition info string into part_info data structure.
          */
3359
        }
3360 3361
        partition {}
        ;
3362 3363

partition:
3364 3365
        BY part_type_def opt_no_parts {} opt_sub_part {} part_defs
        ;
3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381

part_type_def:
        opt_linear KEY_SYM '(' part_field_list ')'
        {
          LEX *lex= Lex;
          lex->part_info->list_of_part_fields= TRUE;
          lex->part_info->part_type= HASH_PARTITION;
        }
        | opt_linear HASH_SYM
        { Lex->part_info->part_type= HASH_PARTITION; }
        part_func {}
        | RANGE_SYM
        { Lex->part_info->part_type= RANGE_PARTITION; }
        part_func {}
        | LIST_SYM
        { Lex->part_info->part_type= LIST_PARTITION; }
3382 3383
        part_func {}
        ;
3384 3385 3386 3387

opt_linear:
        /* empty */ {}
        | LINEAR_SYM
3388 3389
        { Lex->part_info->linear_hash_ind= TRUE;}
        ;
3390 3391

part_field_list:
3392 3393 3394 3395 3396
        /* empty */ {}
        | part_field_item_list {}
        ;

part_field_item_list:
3397
        part_field_item {}
3398
        | part_field_item_list ',' part_field_item {}
3399
        ;
3400 3401 3402 3403

part_field_item:
        ident
        {
3404 3405 3406 3407 3408
          if (Lex->part_info->part_field_list.push_back($1.str))
          {
            mem_alloc_error(1);
            YYABORT;
          }
3409 3410
        }
        ;
3411 3412 3413 3414 3415 3416 3417 3418 3419 3420

part_func:
        '(' remember_name part_func_expr remember_end ')'
        {
          LEX *lex= Lex;
          uint expr_len= (uint)($4 - $2) - 1;
          lex->part_info->list_of_part_fields= FALSE;
          lex->part_info->part_expr= $3;
          lex->part_info->part_func_string= $2+1;
          lex->part_info->part_func_len= expr_len;
3421 3422
        }
        ;
3423 3424 3425 3426 3427 3428 3429 3430 3431 3432

sub_part_func:
        '(' remember_name part_func_expr remember_end ')'
        {
          LEX *lex= Lex;
          uint expr_len= (uint)($4 - $2) - 1;
          lex->part_info->list_of_subpart_fields= FALSE;
          lex->part_info->subpart_expr= $3;
          lex->part_info->subpart_func_string= $2+1;
          lex->part_info->subpart_func_len= expr_len;
3433 3434
        }
        ;
3435 3436 3437 3438 3439 3440 3441


opt_no_parts:
        /* empty */ {}
        | PARTITIONS_SYM ulong_num 
        { 
          uint no_parts= $2;
3442
          LEX *lex= Lex;
3443 3444 3445 3446 3447
          if (no_parts == 0)
          {
            my_error(ER_NO_PARTS_ERROR, MYF(0), "partitions");
            YYABORT;
          }
3448 3449 3450

          lex->part_info->no_parts= no_parts;
          lex->part_info->use_default_no_partitions= FALSE;
3451 3452
        }
        ;
3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465

opt_sub_part:
        /* empty */ {}
        | SUBPARTITION_SYM BY opt_linear HASH_SYM sub_part_func
        { Lex->part_info->subpart_type= HASH_PARTITION; }
        opt_no_subparts {}
        | SUBPARTITION_SYM BY opt_linear KEY_SYM
          '(' sub_part_field_list ')'
        {
          LEX *lex= Lex;
          lex->part_info->subpart_type= HASH_PARTITION;
          lex->part_info->list_of_subpart_fields= TRUE;
        }
3466 3467
        opt_no_subparts {}
        ;
3468 3469 3470

sub_part_field_list:
        sub_part_field_item {}
3471 3472
        | sub_part_field_list ',' sub_part_field_item {}
        ;
3473 3474 3475

sub_part_field_item:
        ident
3476 3477 3478 3479 3480 3481 3482
        {
          if (Lex->part_info->subpart_field_list.push_back($1.str))
          {
            mem_alloc_error(1);
            YYABORT;
          }
        }
3483
        ;
3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498

part_func_expr:
        bit_expr
        {
          LEX *lex= Lex;
          bool not_corr_func;
          not_corr_func= !lex->safe_to_cache_query;
          lex->safe_to_cache_query= 1;
          if (not_corr_func)
          {
            yyerror(ER(ER_CONST_EXPR_IN_PARTITION_FUNC_ERROR));
            YYABORT;
          }
          $$=$1;
        }
3499
        ;
3500 3501 3502 3503 3504 3505

opt_no_subparts:
        /* empty */ {}
        | SUBPARTITIONS_SYM ulong_num
        {
          uint no_parts= $2;
3506
          LEX *lex= Lex;
3507 3508 3509 3510 3511
          if (no_parts == 0)
          {
            my_error(ER_NO_PARTS_ERROR, MYF(0), "subpartitions");
            YYABORT;
          }
3512 3513
          lex->part_info->no_subparts= no_parts;
          lex->part_info->use_default_no_subpartitions= FALSE;
3514 3515
        }
        ;
3516 3517 3518 3519 3520 3521 3522 3523

part_defs:
        /* empty */
        {}
        | '(' part_def_list ')'
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
3524
          uint count_curr_parts= part_info->partitions.elements;
3525 3526 3527
          if (part_info->no_parts != 0)
          {
            if (part_info->no_parts !=
3528
                count_curr_parts)
3529 3530 3531 3532 3533
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_PART_ERROR));
              YYABORT;
            }
          }
3534
          else if (count_curr_parts > 0)
3535
          {
3536
            part_info->no_parts= count_curr_parts;
3537 3538
          }
          part_info->count_curr_subparts= 0;
3539 3540
        }
        ;
3541 3542 3543

part_def_list:
        part_definition {}
3544 3545
        | part_def_list ',' part_definition {}
        ;
3546 3547 3548 3549 3550 3551 3552

part_definition:
        PARTITION_SYM
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          partition_element *p_elem= new partition_element();
3553 3554 3555 3556 3557 3558 3559 3560 3561
          uint part_id= part_info->partitions.elements +
                        part_info->temp_partitions.elements;
          enum partition_state part_state;

          if (part_info->part_state)
            part_state= (enum partition_state)part_info->part_state[part_id];
          else
            part_state= PART_NORMAL;
          switch (part_state)
3562
          {
3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619
            case PART_TO_BE_DROPPED:
            /*
              This part is currently removed so we keep it in a
              temporary list for REPAIR TABLE to be able to handle
              failures during drop partition process.
            */
            case PART_TO_BE_ADDED:
            /*
              This part is currently being added so we keep it in a
              temporary list for REPAIR TABLE to be able to handle
              failures during add partition process.
            */
              if (!p_elem || part_info->temp_partitions.push_back(p_elem))
              {
                mem_alloc_error(sizeof(partition_element));
                YYABORT;
              }
              break;
            case PART_IS_ADDED:
            /*
              Part has been added and is now a normal partition
            */
            case PART_TO_BE_REORGED:
            /*
              This part is currently reorganised, it is still however
              used so we keep it in the list of partitions. We do
              however need the state to be able to handle REPAIR TABLE
              after failures in the reorganisation process.
            */
            case PART_REORGED_DROPPED:
            /*
              This part is currently reorganised as part of a
              COALESCE PARTITION and it will be dropped without a new
              replacement partition after completing the reorganisation.
            */
            case PART_CHANGED:
            /*
              This part is currently split or merged as part of ADD
              PARTITION for a hash partition or as part of COALESCE
              PARTITION for a hash partitioned table.
            */
            case PART_IS_CHANGED:
            /*
              This part has been split or merged as part of ADD
              PARTITION for a hash partition or as part of COALESCE
              PARTITION for a hash partitioned table.
            */
            case PART_NORMAL:
              if (!p_elem || part_info->partitions.push_back(p_elem))
              {
                mem_alloc_error(sizeof(partition_element));
                YYABORT;
              }
              break;
            default:
              mem_alloc_error((part_id * 1000) + part_state);
              YYABORT;
3620
          }
3621
          p_elem->part_state= part_state;
3622 3623 3624
          part_info->curr_part_elem= p_elem;
          part_info->current_partition= p_elem;
          part_info->use_default_partitions= FALSE;
3625
          part_info->use_default_no_partitions= FALSE;
3626 3627 3628 3629
        }
        part_name {}
        opt_part_values {}
        opt_part_options {}
3630 3631
        opt_sub_partition {}
        ;
3632 3633 3634

part_name:
        ident_or_text
3635 3636 3637 3638 3639 3640
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          partition_element *p_elem= part_info->curr_part_elem;
          p_elem->partition_name= $1.str;
        }
3641
        ;
3642 3643 3644 3645 3646

opt_part_values:
        /* empty */
        {
          LEX *lex= Lex;
3647
          if (!is_partition_management(lex))
3648
          {
3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660
            if (lex->part_info->part_type == RANGE_PARTITION)
            {
              my_error(ER_PARTITION_REQUIRES_VALUES_ERROR, MYF(0),
              "RANGE", "LESS THAN");
              YYABORT;
            }
            if (lex->part_info->part_type == LIST_PARTITION)
            {
              my_error(ER_PARTITION_REQUIRES_VALUES_ERROR, MYF(0),
              "LIST", "IN");
              YYABORT;
            }
3661 3662 3663 3664
          }
        }
        | VALUES LESS_SYM THAN_SYM part_func_max
        {
3665 3666
          LEX *lex= Lex;
          if (!is_partition_management(lex))
3667
          {
3668 3669 3670 3671 3672 3673
            if (Lex->part_info->part_type != RANGE_PARTITION)
            {
              my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
              "RANGE", "LESS THAN");
              YYABORT;
            }
3674 3675 3676 3677
          }
        }
        | VALUES IN_SYM '(' part_list_func ')'
        {
3678 3679
          LEX *lex= Lex;
          if (!is_partition_management(lex))
3680
          {
3681 3682 3683 3684 3685 3686
            if (Lex->part_info->part_type != LIST_PARTITION)
            {
              my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
              "LIST", "IN");
              YYABORT;
            }
3687
          }
3688 3689
        }
        ;
3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700

part_func_max:
        MAX_VALUE_SYM
        {
          LEX *lex= Lex;
          if (lex->part_info->defined_max_value)
          {
            yyerror(ER(ER_PARTITION_MAXVALUE_ERROR));
            YYABORT;
          }
          lex->part_info->defined_max_value= TRUE;
3701
          lex->part_info->curr_part_elem->range_value= LONGLONG_MAX;
3702 3703 3704 3705 3706 3707 3708 3709
        }
        | part_range_func
        {
          if (Lex->part_info->defined_max_value)
          {
            yyerror(ER(ER_PARTITION_MAXVALUE_ERROR));
            YYABORT;
          }
3710 3711
        }
        ;
3712 3713 3714 3715

part_range_func:
        '(' part_bit_expr ')' 
        {
3716
          Lex->part_info->curr_part_elem->range_value= $2;
3717 3718
        }
        ;
3719 3720 3721

part_list_func:
        part_list_item {}
3722 3723
        | part_list_func ',' part_list_item {}
        ;
3724 3725 3726 3727

part_list_item:
        part_bit_expr
        {
3728
          longlong *value_ptr;
3729 3730 3731
          if (!(value_ptr= (longlong*)sql_alloc(sizeof(longlong))) ||
              ((*value_ptr= $1, FALSE) ||
     Lex->part_info->curr_part_elem->list_val_list.push_back(value_ptr)))
3732
          {
3733
            mem_alloc_error(sizeof(longlong));
3734 3735
            YYABORT;
          }
3736 3737
        }
        ;
3738 3739 3740 3741 3742 3743 3744

part_bit_expr:
        bit_expr
        {
          Item *part_expr= $1;
          bool not_corr_func;
          LEX *lex= Lex;
3745
          THD *thd= YYTHD;
3746
          longlong item_value;
3747 3748
          Name_resolution_context *context= &lex->current_select->context;
          TABLE_LIST *save_list= context->table_list;
3749
          const char *save_where= thd->where;
3750 3751

          context->table_list= 0;
3752 3753 3754 3755 3756
          thd->where= "partition function";
          if (part_expr->fix_fields(YYTHD, (Item**)0) ||
              ((context->table_list= save_list), FALSE) ||
              (!part_expr->const_item()) ||
              (!lex->safe_to_cache_query))
3757 3758 3759 3760
          {
            yyerror(ER(ER_NO_CONST_EXPR_IN_RANGE_OR_LIST_ERROR));
            YYABORT;
          }
3761
          thd->where= save_where;
3762 3763 3764 3765 3766 3767 3768
          if (part_expr->result_type() != INT_RESULT)
          {
            yyerror(ER(ER_INCONSISTENT_TYPE_OF_FUNCTIONS_ERROR));
            YYABORT;
          }
          item_value= part_expr->val_int();
          $$= item_value; 
3769
        }
3770
        ;
3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791

opt_sub_partition:
        /* empty */ {}
        | '(' sub_part_list ')'
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          if (part_info->no_subparts != 0)
          {
            if (part_info->no_subparts !=
                part_info->count_curr_subparts)
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_SUBPART_ERROR));
              YYABORT;
            }
          }
          else if (part_info->count_curr_subparts > 0)
          {
            part_info->no_subparts= part_info->count_curr_subparts;
          }
          part_info->count_curr_subparts= 0;
3792 3793
        }
        ;
3794 3795 3796

sub_part_list:
        sub_part_definition {}
3797 3798
        | sub_part_list ',' sub_part_definition {}
        ;
3799 3800 3801 3802 3803 3804 3805

sub_part_definition:
        SUBPARTITION_SYM
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          partition_element *p_elem= new partition_element();
3806 3807
          if (!p_elem ||
           part_info->current_partition->subpartitions.push_back(p_elem))
3808
          {
3809
            mem_alloc_error(sizeof(partition_element));
3810 3811 3812 3813
            YYABORT;
          }
          part_info->curr_part_elem= p_elem;
          part_info->use_default_subpartitions= FALSE;
3814
          part_info->use_default_no_subpartitions= FALSE;
3815 3816
          part_info->count_curr_subparts++;
        }
3817 3818
        sub_name opt_part_options {}
        ;
3819 3820 3821

sub_name:
        ident_or_text
3822 3823
        { Lex->part_info->curr_part_elem->partition_name= $1.str; }
        ;
3824 3825 3826

opt_part_options:
       /* empty */ {}
3827 3828
       | opt_part_option_list {}
       ;
3829 3830 3831

opt_part_option_list:
       opt_part_option_list opt_part_option {}
3832 3833
       | opt_part_option {}
       ;
3834 3835 3836 3837 3838

opt_part_option:
        TABLESPACE opt_equal ident_or_text
        { Lex->part_info->curr_part_elem->tablespace_name= $3.str; }
        | opt_storage ENGINE_SYM opt_equal storage_engines
3839 3840 3841 3842 3843
        {
          LEX *lex= Lex;
          lex->part_info->curr_part_elem->engine_type= $4;
          lex->part_info->default_engine_type= $4;
        }
3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854
        | NODEGROUP_SYM opt_equal ulong_num
        { Lex->part_info->curr_part_elem->nodegroup_id= $3; }
        | MAX_ROWS opt_equal ulonglong_num
        { Lex->part_info->curr_part_elem->part_max_rows= $3; }
        | MIN_ROWS opt_equal ulonglong_num
        { Lex->part_info->curr_part_elem->part_min_rows= $3; }
        | DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys
        { Lex->part_info->curr_part_elem->data_file_name= $4.str; }
        | INDEX_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys
        { Lex->part_info->curr_part_elem->index_file_name= $4.str; }
        | COMMENT_SYM opt_equal TEXT_STRING_sys
3855 3856
        { Lex->part_info->curr_part_elem->part_comment= $3.str; }
        ;
3857 3858 3859 3860 3861

/*
 End of partition parser part
*/

serg@serg.mylan's avatar
serg@serg.mylan committed
3862
create_select:
3863
          SELECT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3864
          {
3865
	    LEX *lex=Lex;
3866
	    lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
3867 3868 3869 3870
	    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;
3871 3872 3873 3874
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
3875
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
3876
	    mysql_init_select(lex);
3877
	    lex->current_select->parsing_place= SELECT_LIST;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3878
          }
3879 3880
          select_options select_item_list
	  {
3881
	    Select->parsing_place= NO_MATTER;
3882
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3883
	  opt_select_from
3884 3885 3886 3887 3888 3889 3890
	  {
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
	    Lex->current_select->table_list.push_front(&Lex->save_list);
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3891
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3892

3893 3894
opt_as:
	/* empty */ {}
3895
	| AS	    {};
3896

3897 3898 3899 3900 3901 3902 3903 3904 3905
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
3906 3907
	default_collation   {}
	| default_charset   {};
3908

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3909
opt_table_options:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3910
	/* empty */	 { $$= 0; }
3911
	| table_options  { $$= $1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3912 3913 3914

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3917
table_option:
3918
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3919 3920

opt_if_not_exists:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3921
	/* empty */	 { $$= 0; }
3922
	| IF not EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3923 3924 3925

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

3928 3929 3930 3931
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
3932 3933
create_table_options:
	create_table_option
3934
	| create_table_option     create_table_options
3935
	| create_table_option ',' create_table_options;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3936 3937

create_table_option:
3938 3939
	ENGINE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; }
	| TYPE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; WARN_DEPRECATED("TYPE=storage_engine","ENGINE=storage_engine");   Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; }
3940 3941
	| 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;}
serg@serg.mylan's avatar
serg@serg.mylan committed
3942
	| 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;}
3943 3944
	| PASSWORD opt_equal TEXT_STRING_sys	{ Lex->create_info.password=$3.str; Lex->create_info.used_fields|= HA_CREATE_USED_PASSWORD; }
	| COMMENT_SYM opt_equal TEXT_STRING_sys	{ Lex->create_info.comment=$3.str; Lex->create_info.used_fields|= HA_CREATE_USED_COMMENT; }
3945
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966
        | PACK_KEYS_SYM opt_equal ulong_num
          {
            switch($3) {
            case 0:
                Lex->create_info.table_options|= HA_OPTION_NO_PACK_KEYS;
                break;
            case 1:
                Lex->create_info.table_options|= HA_OPTION_PACK_KEYS;
                break;
            default:
                yyerror(ER(ER_SYNTAX_ERROR));
                YYABORT;
            }
            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;
          }
serg@serg.mylan's avatar
serg@serg.mylan committed
3967 3968
	| CHECKSUM_SYM opt_equal ulong_num	{ Lex->create_info.table_options|= $3 ? HA_OPTION_CHECKSUM : HA_OPTION_NO_CHECKSUM; Lex->create_info.used_fields|= HA_CREATE_USED_CHECKSUM; }
	| DELAY_KEY_WRITE_SYM opt_equal ulong_num { Lex->create_info.table_options|= $3 ? HA_OPTION_DELAY_KEY_WRITE : HA_OPTION_NO_DELAY_KEY_WRITE;  Lex->create_info.used_fields|= HA_CREATE_USED_DELAY_KEY_WRITE; }
3969
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984
	| RAID_TYPE opt_equal raid_types
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_TYPE", "PARTITION");
	    YYABORT;
	  }
	| RAID_CHUNKS opt_equal ulong_num
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_CHUNKS", "PARTITION");
	    YYABORT;
	  }
	| RAID_CHUNKSIZE opt_equal ulong_num
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_CHUNKSIZE", "PARTITION");
	    YYABORT;
	  }
3985
	| UNION_SYM opt_equal '(' table_list ')'
3986 3987 3988
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
3989 3990
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
3991
	    lex->create_info.merge_list.elements--;
3992 3993
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
3994
	    lex->select_lex.table_list.elements=1;
3995 3996 3997
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
3998
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
3999
	  }
4000 4001
	| default_charset
	| default_collation
4002
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
4003 4004
	| DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.data_file_name= $4.str; Lex->create_info.used_fields|= HA_CREATE_USED_DATADIR; }
	| INDEX_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.index_file_name= $4.str;  Lex->create_info.used_fields|= HA_CREATE_USED_INDEXDIR; }
4005 4006 4007
        | TABLESPACE ident {Lex->create_info.tablespace= $2.str;}
        | STORAGE_SYM DISK_SYM {Lex->create_info.store_on_disk= TRUE;}
        | STORAGE_SYM MEMORY_SYM {Lex->create_info.store_on_disk= FALSE;}
4008
	| CONNECTION_SYM opt_equal TEXT_STRING_sys { Lex->create_info.connect_string.str= $3.str; Lex->create_info.connect_string.length= $3.length;  Lex->create_info.used_fields|= HA_CREATE_USED_CONNECTION; }
4009
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4010

4011 4012 4013 4014 4015 4016 4017 4018
default_charset:
        opt_default charset opt_equal charset_name_or_default
        {
          HA_CREATE_INFO *cinfo= &Lex->create_info;
          if ((cinfo->used_fields & HA_CREATE_USED_DEFAULT_CHARSET) &&
               cinfo->default_table_charset && $4 &&
               !my_charset_same(cinfo->default_table_charset,$4))
          {
4019 4020 4021
            my_error(ER_CONFLICTING_DECLARATIONS, MYF(0),
                     "CHARACTER SET ", cinfo->default_table_charset->csname,
                     "CHARACTER SET ", $4->csname);
4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035
            YYABORT;
          }
	  Lex->create_info.default_table_charset= $4;
          Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

default_collation:
        opt_default COLLATE_SYM opt_equal collation_name_or_default
        {
          HA_CREATE_INFO *cinfo= &Lex->create_info;
          if ((cinfo->used_fields & HA_CREATE_USED_DEFAULT_CHARSET) &&
               cinfo->default_table_charset && $4 &&
               !my_charset_same(cinfo->default_table_charset,$4))
            {
4036 4037
              my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $4->name, cinfo->default_table_charset->csname);
4038 4039 4040 4041 4042 4043
              YYABORT;
            }
            Lex->create_info.default_table_charset= $4;
            Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

4044
storage_engines:
4045 4046
	ident_or_text
	{
4047 4048
	  $$ = ha_resolve_by_name(YYTHD, &$1);
	  if ($$ == NULL &&
4049 4050
	      test(YYTHD->variables.sql_mode & MODE_NO_ENGINE_SUBSTITUTION))
	  {
4051
	    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
4052 4053 4054
	    YYABORT;
	  }
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4055 4056 4057 4058 4059

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
4060 4061 4062
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; }
	| REDUNDANT_SYM	{ $$= ROW_TYPE_REDUNDANT; }
	| COMPACT_SYM	{ $$= ROW_TYPE_COMPACT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4063 4064 4065 4066

raid_types:
	RAID_STRIPED_SYM { $$= RAID_TYPE_0; }
	| RAID_0_SYM	 { $$= RAID_TYPE_0; }
serg@serg.mylan's avatar
serg@serg.mylan committed
4067
	| ulong_num	 { $$=$1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4068

4069 4070 4071
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
4072
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
4073

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4074
opt_select_from:
4075
	opt_limit_clause {}
4076
	| select_from select_lock_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4077 4078

udf_func_type:
4079
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
4080
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4081 4082 4083 4084

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
4085
        | DECIMAL_SYM {$$ = (int) DECIMAL_RESULT; }
4086
	| INT_SYM {$$ = (int) INT_RESULT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4087 4088 4089

field_list:
	  field_list_item
4090
	| field_list ',' field_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4091 4092 4093


field_list_item:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4094
	   column_def
4095 4096 4097 4098
         | key_def
         ;

column_def:
4099
	  field_spec opt_check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4100 4101 4102 4103
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4104
	;
4105 4106

key_def:
4107
	key_type opt_ident key_alg '(' key_list ')' opt_fulltext_parser
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4108
	  {
4109
	    LEX *lex=Lex;
4110 4111 4112 4113 4114 4115
	    if ($1 != Key::FULLTEXT && $7)
	    {
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
	    }
	    lex->key_list.push_back(new Key($1,$2, $3, 0, lex->col_list, $7));
4116
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4117
	  }
4118 4119 4120 4121
	| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
	  {
	    LEX *lex=Lex;
	    const char *key_name= $3 ? $3:$1;
4122 4123
	    lex->key_list.push_back(new Key($2, key_name, $4, 0,
				    lex->col_list));
4124 4125
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
4126
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4127
	  {
4128
	    LEX *lex=Lex;
4129
	    lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
4130 4131 4132 4133 4134
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
4135 4136 4137
	    lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4 : $1,
					    HA_KEY_ALG_UNDEF, 1,
					    lex->col_list));
4138
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4139
	  }
4140 4141 4142 4143
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
4144
	| opt_constraint check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4145 4146
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
4147 4148 4149
	  }
	;

4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163
opt_fulltext_parser:
        /* empty */               { $$= (LEX_STRING *)0; }
	| WITH PARSER_SYM IDENT_sys
          {
            if (plugin_is_ready(&$3, MYSQL_FTPARSER_PLUGIN))
              $$= (LEX_STRING *)sql_memdup(&$3, sizeof(LEX_STRING));
            else
            {
              my_error(ER_FUNCTION_NOT_DEFINED, MYF(0), $3.str);
              YYABORT;
            }
          }
        ;

4164
opt_check_constraint:
4165
	/* empty */
4166 4167 4168 4169 4170
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
4171
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4172 4173

opt_constraint:
4174
	/* empty */		{ $$=(char*) 0; }
monty@mysql.com's avatar
monty@mysql.com committed
4175 4176 4177 4178 4179 4180
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4181 4182 4183 4184

field_spec:
	field_ident
	 {
4185
	   LEX *lex=Lex;
4186
	   lex->length=lex->dec=0; lex->type=0;
4187
	   lex->default_value= lex->on_update_value= 0;
4188
           lex->comment=null_lex_str;
4189
	   lex->charset=NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4190 4191 4192
	 }
	type opt_attribute
	{
4193
	  LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4194
	  if (add_field_to_list(lex->thd, $1.str,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4195
				(enum enum_field_types) $3,
4196
				lex->length,lex->dec,lex->type,
4197
				lex->default_value, lex->on_update_value, 
4198
                                &lex->comment,
4199
				lex->change,&lex->interval_list,lex->charset,
4200
				lex->uint_geom_type))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4201
	    YYABORT;
4202
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4203 4204

type:
4205
	int_type opt_len field_options	{ $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4206 4207
	| real_type opt_precision field_options { $$=$1; }
	| FLOAT_SYM float_options field_options { $$=FIELD_TYPE_FLOAT; }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
4208 4209 4210 4211
	| BIT_SYM			{ Lex->length= (char*) "1";
					  $$=FIELD_TYPE_BIT; }
	| BIT_SYM '(' NUM ')'		{ Lex->length= $3.str;
					  $$=FIELD_TYPE_BIT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4212 4213
	| BOOL_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
4214 4215
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
4216
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4217 4218 4219
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4220
	| nchar '(' NUM ')' opt_bin_mod	{ Lex->length=$3.str;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4221
					  $$=FIELD_TYPE_STRING;
4222
					  Lex->charset=national_charset_info; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4223
	| nchar opt_bin_mod		{ Lex->length=(char*) "1";
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4224
					  $$=FIELD_TYPE_STRING;
4225
					  Lex->charset=national_charset_info; }
4226
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
4227
					  Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4228
					  $$=FIELD_TYPE_STRING; }
4229 4230 4231
	| BINARY			{ Lex->length= (char*) "1";
					  Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_STRING; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4232
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
4233
					  $$= MYSQL_TYPE_VARCHAR; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4234
	| nvarchar '(' NUM ')' opt_bin_mod { Lex->length=$3.str;
4235
					  $$= MYSQL_TYPE_VARCHAR;
4236
					  Lex->charset=national_charset_info; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4237
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
4238
					  Lex->charset=&my_charset_bin;
4239
					  $$= MYSQL_TYPE_VARCHAR; }
4240
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4241 4242
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
4243
	| TIMESTAMP opt_len
4244
	  {
4245
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
4246 4247
	      $$=FIELD_TYPE_DATETIME;
	    else
4248 4249 4250 4251 4252
            {
              /* 
                Unlike other types TIMESTAMP fields are NOT NULL by default.
              */
              Lex->type|= NOT_NULL_FLAG;
4253
	      $$=FIELD_TYPE_TIMESTAMP;
4254
            }
4255
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4256
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
4257
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4258
					  $$=FIELD_TYPE_TINY_BLOB; }
4259
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4260
					  $$=FIELD_TYPE_BLOB; }
4261 4262
	| spatial_type
          {
hf@deer.(none)'s avatar
hf@deer.(none) committed
4263
#ifdef HAVE_SPATIAL
4264 4265 4266
            Lex->charset=&my_charset_bin;
            Lex->uint_geom_type= (uint)$1;
            $$=FIELD_TYPE_GEOMETRY;
hf@deer.(none)'s avatar
hf@deer.(none) committed
4267
#else
serg@serg.mylan's avatar
serg@serg.mylan committed
4268
            my_error(ER_FEATURE_DISABLED, MYF(0),
4269
                     sym_group_geom.name, sym_group_geom.needed_define);
4270
            YYABORT;
hf@deer.(none)'s avatar
hf@deer.(none) committed
4271
#endif
4272
          }
4273
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4274
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
4275
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4276
					  $$=FIELD_TYPE_LONG_BLOB; }
4277
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4278
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
4279 4280
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
4281
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
4282 4283
	| 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
4284
	| DECIMAL_SYM float_options field_options
4285
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4286
	| NUMERIC_SYM float_options field_options
4287
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
4288
	| FIXED_SYM float_options field_options
4289
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
4290
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
4291
	  { $$=FIELD_TYPE_ENUM; }
4292
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
4293
	  { $$=FIELD_TYPE_SET; }
4294
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
4295 4296 4297 4298 4299 4300
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
4301
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4302

hf@deer.(none)'s avatar
hf@deer.(none) committed
4303 4304 4305
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
4306 4307 4308
	| POINT_SYM           { Lex->length= (char*)"21";
                                $$= Field::GEOM_POINT;
                              }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4309 4310 4311 4312 4313 4314 4315
	| 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
4316 4317
char:
	CHAR_SYM {}
4318 4319 4320 4321 4322 4323
	;

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4325 4326 4327
varchar:
	char VARYING {}
	| VARCHAR {}
4328 4329 4330 4331
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4332
	| NVARCHAR_SYM {}
4333 4334 4335 4336
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4337 4338 4339 4340 4341 4342

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

real_type:
4346
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4347 4348
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
4349
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4350 4351 4352


float_options:
4353 4354
        /* empty */		{ Lex->dec=Lex->length= (char*)0; }
        | '(' NUM ')'		{ Lex->length=$2.str; Lex->dec= (char*)0; }
4355
	| precision		{};
4356 4357 4358 4359 4360 4361

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

field_options:
	/* empty */		{}
4366
	| field_opt_list	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4367 4368 4369

field_opt_list:
	field_opt_list field_option {}
4370
	| field_option {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4371 4372

field_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4373
	SIGNED_SYM	{}
4374
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
4375
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4376 4377

opt_len:
4378 4379
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4380 4381 4382

opt_precision:
	/* empty */	{}
4383
	| precision	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4384 4385 4386

opt_attribute:
	/* empty */ {}
4387
	| opt_attribute_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4388 4389 4390

opt_attribute_list:
	opt_attribute_list attribute {}
4391
	| attribute;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4392 4393 4394

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
4395
	| not NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
4396 4397 4398
	| 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
4399
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
4400
	| SERIAL_SYM DEFAULT VALUE_SYM
4401 4402 4403
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
4404
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4405 4406 4407 4408 4409
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
4410
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4411 4412 4413 4414 4415
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
4416
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4417 4418 4419 4420 4421
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
4422
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4423
	  }
4424
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= $2; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4425 4426
	| COLLATE_SYM collation_name
	  {
4427
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4428
	    {
4429 4430
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $2->name,Lex->charset->csname);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4431 4432 4433 4434 4435 4436 4437 4438
	      YYABORT;
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4439

4440 4441 4442 4443 4444
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

4445 4446 4447 4448
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
4449

4450
charset_name:
4451
	ident_or_text
4452
	{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4453
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
4454
	  {
4455
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
4456 4457
	    YYABORT;
	  }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4458 4459 4460
	}
	| BINARY { $$= &my_charset_bin; }
	;
4461

4462 4463 4464
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4465

4466 4467 4468 4469 4470 4471 4472

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)))
	  {
4473
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
4474 4475 4476 4477 4478 4479 4480 4481 4482 4483
	    YYABORT;
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

4484
collation_name:
4485
	ident_or_text
4486 4487 4488
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
4489
	    my_error(ER_UNKNOWN_COLLATION, MYF(0), $1.str);
4490 4491 4492 4493
	    YYABORT;
	  }
	};

4494 4495
opt_collate:
	/* empty */	{ $$=NULL; }
4496
	| COLLATE_SYM collation_name_or_default { $$=$2; }
4497 4498
	;

4499 4500 4501 4502
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

4503 4504 4505 4506
opt_default:
	/* empty */	{}
	| DEFAULT	{};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4507
opt_binary:
4508
	/* empty */			{ Lex->charset=NULL; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4509
	| ASCII_SYM opt_bin_mod		{ Lex->charset=&my_charset_latin1; }
4510
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529
	| UNICODE_SYM opt_bin_mod
	{
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
	  {
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
	    YYABORT;
	  }
	}
	| charset charset_name opt_bin_mod	{ Lex->charset=$2; }
        | BINARY opt_bin_charset { Lex->type|= BINCMP_FLAG; };

opt_bin_mod:
	/* empty */ { }
	| BINARY { Lex->type|= BINCMP_FLAG; };

opt_bin_charset:
	/* empty */ { }
	| ASCII_SYM	{ Lex->charset=&my_charset_latin1; }
4530 4531
	| UNICODE_SYM
	{
4532 4533
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
4534
	  {
4535
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
4536 4537 4538
	    YYABORT;
	  }
	}
4539
	| charset charset_name	{ Lex->charset=$2; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4540

4541 4542 4543
opt_primary:
	/* empty */
	| PRIMARY_SYM
4544
	;
4545

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4546
references:
4547 4548
	REFERENCES table_ident
	{
4549
	  LEX *lex=Lex;
4550 4551 4552 4553 4554 4555
	  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
4556
	};
4557

4558
opt_ref_list:
4559
	/* empty */ opt_on_delete {}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4560
	| '(' ref_list ')' opt_on_delete {};
4561 4562 4563

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4566 4567 4568

opt_on_delete:
	/* empty */ {}
4569
	| opt_on_delete_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4570 4571 4572

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
4573
	| opt_on_delete_item {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4574 4575

opt_on_delete_item:
4576 4577 4578 4579
	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
4580
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4581 4582

delete_option:
4583 4584 4585 4586
	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
4587
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4588 4589

key_type:
4590
	key_or_index			    { $$= Key::MULTIPLE; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4591 4592
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
hf@deer.(none)'s avatar
hf@deer.(none) committed
4593 4594 4595 4596
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
4597 4598
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
4599 4600 4601
	    YYABORT;
#endif
	  };
4602 4603 4604

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4605
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4606 4607 4608

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

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4611 4612 4613 4614 4615
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4616 4617
keys_or_index:
	KEYS {}
4618
	| INDEX_SYM {}
4619
	| INDEXES {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4620

4621
opt_unique_or_fulltext:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4622 4623
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
serg@serg.mylan's avatar
serg@serg.mylan committed
4624
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
hf@deer.(none)'s avatar
hf@deer.(none) committed
4625 4626 4627 4628 4629
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
serg@serg.mylan's avatar
serg@serg.mylan committed
4630
            my_error(ER_FEATURE_DISABLED, MYF(0),
4631
                     sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
4632 4633 4634
	    YYABORT;
#endif
	  }
serg@serg.mylan's avatar
serg@serg.mylan committed
4635
        ;
4636 4637

key_alg:
4638
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
4639
	| USING opt_btree_or_rtree { $$= $2; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4640
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
4641 4642 4643

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4644 4645 4646 4647
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4648
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4649 4650 4651

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
4652
	| key_part order_dir		{ Lex->col_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4653 4654 4655

key_part:
	ident			{ $$=new key_part_spec($1.str); }
4656 4657 4658 4659 4660
	| ident '(' NUM ')'	
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
4661
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
4662 4663 4664
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4665 4666 4667

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

4670
opt_component:
4671 4672
        /* empty */      { $$= null_lex_str; }
        | '.' ident      { $$= $2; };
4673

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4674 4675
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
4676
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4677 4678 4679 4680 4681 4682 4683 4684

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
4685
	  THD *thd= YYTHD;
4686
	  LEX *lex= thd->lex;
4687 4688 4689
	  lex->sql_command= SQLCOM_ALTER_TABLE;
	  lex->name= 0;
	  lex->duplicates= DUP_ERROR; 
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4690 4691
	  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
4692 4693 4694 4695
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
4696
          lex->select_lex.init_order();
4697
	  lex->select_lex.db=lex->name=0;
4698
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
4699
	  lex->create_info.db_type= (handlerton*) &default_hton;
4700
	  lex->create_info.default_table_charset= NULL;
4701
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
4702
	  lex->alter_info.reset();
4703
	  lex->alter_info.flags= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4704
	}
4705
	alter_commands
4706
	{}
4707
	| ALTER DATABASE ident_or_empty
4708 4709 4710 4711 4712
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
          opt_create_database_options
4713 4714 4715
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
4716
	    lex->name= $3;
4717 4718 4719 4720 4721
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

4722 4723 4724 4725 4726
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
	      YYABORT;
	    }
4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    LEX *lex=Lex;

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

4740 4741 4742 4743 4744
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
	      YYABORT;
	    }
4745 4746 4747 4748 4749 4750 4751 4752 4753
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    LEX *lex=Lex;

	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
	    lex->spname= $3;
	  }
4754 4755
        | ALTER view_algorithm_opt definer view_suid
          VIEW_SYM table_ident
4756 4757 4758 4759 4760 4761
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_CREATE_VIEW;
	    lex->create_view_mode= VIEW_ALTER;
	    /* first table in list is target VIEW name */
4762
	    lex->select_lex.add_table_to_list(thd, $6, NULL, 0);
4763
	  }
4764
	  view_list_opt AS view_select view_check_option
4765
	  {}
4766
	| ALTER EVENT_SYM sp_name
4767 4768 4769 4770
          /* 
             BE CAREFUL when you add a new rule to update the block where 
             YYTHD->client_capabilities is set back to original value
          */
4771 4772 4773 4774 4775 4776
          {
            LEX *lex=Lex;
            event_timed *et;

            if (lex->et)
            {
andrey@lmy004's avatar
andrey@lmy004 committed
4777 4778 4779 4780
              /*
                Recursive events are not possible because recursive SPs
                are not also possible. lex->sp_head is not stacked.
              */
4781 4782 4783
              my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "EVENT");
              YYABORT;
            }
andrey@lmy004's avatar
andrey@lmy004 committed
4784
            lex->spname= 0;//defensive programming
4785
	    
andrey@lmy004's avatar
andrey@lmy004 committed
4786 4787
            if (!(et= new event_timed()))// implicitly calls event_timed::init()
              YYABORT;
4788 4789 4790 4791 4792 4793 4794 4795
            lex->et = et;
            et->init_name(YYTHD, $3);

            /*
                We have to turn of CLIENT_MULTI_QUERIES while parsing a
                stored procedure, otherwise yylex will chop it into pieces
                at each ';'.
            */
andrey@lmy004's avatar
andrey@lmy004 committed
4796
            $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
4797 4798 4799 4800 4801 4802 4803 4804 4805 4806
            YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
	    
          }
          ev_on_schedule
          ev_rename_to
          ev_on_completion
          ev_status
          ev_comment
          ev_opt_sql_stmt
          {
4807 4808 4809 4810 4811 4812 4813 4814
            /*
              $1 - ALTER
              $2 - EVENT_SYM
              $3 - sp_name
              $4 - the block above
            */
            YYTHD->client_capabilities |= $<ulong_num>4;

andrey@lmy004's avatar
andrey@lmy004 committed
4815 4816 4817 4818
            /*
              sql_command is set here because some rules in ev_sql_stmt
              can overwrite it
            */
andrey@lmy004's avatar
andrey@lmy004 committed
4819
            if (!($<ulong_num>5 || $<ulong_num>6 || $<ulong_num>7 ||
andrey@lmy004's avatar
andrey@lmy004 committed
4820
                  $<ulong_num>8 || $<ulong_num>9 || $<ulong_num>10))
andrey@lmy004's avatar
andrey@lmy004 committed
4821 4822 4823 4824
            {
	      yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
andrey@lmy004's avatar
andrey@lmy004 committed
4825
            Lex->sql_command= SQLCOM_ALTER_EVENT;
andrey@lmy004's avatar
andrey@lmy004 committed
4826
          }
4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846
        | ALTER TABLESPACE alter_tablespace_info
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= ALTER_TABLESPACE;
          }
        | ALTER LOGFILE_SYM GROUP alter_logfile_group_info 
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= ALTER_LOGFILE_GROUP;
          }
        | ALTER TABLESPACE change_tablespace_info
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= CHANGE_FILE_TABLESPACE;
          }
        | ALTER TABLESPACE change_tablespace_access 
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= ALTER_ACCESS_MODE_TABLESPACE;
          }
4847
	;
4848

4849

andrey@lmy004's avatar
andrey@lmy004 committed
4850 4851 4852 4853 4854 4855
ev_on_schedule: /* empty */ { $<ulong_num>$= 0;}
        | ON SCHEDULE_SYM ev_schedule_time
          {
            $<ulong_num>$= 1;
          }
        ;
4856

andrey@lmy004's avatar
andrey@lmy004 committed
4857
ev_rename_to: /* empty */ { $<ulong_num>$= 0;}
4858 4859 4860 4861 4862
        | RENAME TO_SYM sp_name
          {
            LEX *lex=Lex;
            lex->spname= $3; //use lex's spname to hold the new name
	                     //the original name is in the event_timed object
andrey@lmy004's avatar
andrey@lmy004 committed
4863
            $<ulong_num>$= 1;
4864 4865
          }
      ;
4866 4867 4868 4869 4870 4871 4872
 
ev_opt_sql_stmt: /* empty*/ { $<ulong_num>$= 0;}
        | DO_SYM ev_sql_stmt
          {
            $<ulong_num>$= 1;
          }
        ;
andrey@lmy004's avatar
andrey@lmy004 committed
4873

4874

4875 4876 4877 4878
ident_or_empty:
	/* empty */  { $$= 0; }
	| ident      { $$= $1.str; };

4879
alter_commands:
4880 4881
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
4882
        | alter_list
4883
          opt_partitioning
4884
        | partitioning
4885
/*
4886
  This part was added for release 5.1 by Mikael Ronström.
4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897
  From here we insert a number of commands to manage the partitions of a
  partitioned table such as adding partitions, dropping partitions,
  reorganising partitions in various manners. In future releases the list
  will be longer and also include moving partitions to a
  new table and so forth.
*/
        | add_partition_rule
        | DROP PARTITION_SYM alt_part_name_list
          {
	    Lex->alter_info.flags|= ALTER_DROP_PARTITION;
          }
4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939
        | REBUILD_SYM PARTITION_SYM opt_no_write_to_binlog
          all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_REBUILD_PARTITION;
            lex->no_write_to_binlog= $3;
          }
        | OPTIMIZE PARTITION_SYM opt_no_write_to_binlog
          all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_OPTIMIZE_PARTITION;
            lex->no_write_to_binlog= $3;
            lex->check_opt.init();
          }
          opt_no_write_to_binlog opt_mi_check_type
        | ANALYZE_SYM PARTITION_SYM opt_no_write_to_binlog
          all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_ANALYZE_PARTITION;
            lex->no_write_to_binlog= $3;
            lex->check_opt.init();
          }
          opt_mi_check_type
        | CHECK_SYM PARTITION_SYM all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_CHECK_PARTITION;
            lex->check_opt.init();
          }
          opt_mi_check_type
        | REPAIR PARTITION_SYM opt_no_write_to_binlog
          all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_REPAIR_PARTITION;
            lex->no_write_to_binlog= $3;
            lex->check_opt.init();
          }
          opt_mi_repair_type
        | COALESCE PARTITION_SYM opt_no_write_to_binlog ulong_num
4940 4941 4942
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_COALESCE_PARTITION;
4943 4944
            lex->no_write_to_binlog= $3;
	    lex->alter_info.no_parts= $4;
4945 4946
          }
        | reorg_partition_rule
4947 4948
        ;

4949 4950 4951 4952 4953 4954 4955 4956
all_or_alt_part_name_list:
        | ALL
        {
	  Lex->alter_info.flags|= ALTER_ALL_PARTITION;
        }
        | alt_part_name_list
        ;

4957
add_partition_rule:
4958
        ADD PARTITION_SYM opt_no_write_to_binlog
4959 4960 4961 4962 4963
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
4964
            mem_alloc_error(sizeof(partition_info));
4965 4966 4967
            YYABORT;
          }
	  lex->alter_info.flags|= ALTER_ADD_PARTITION;
4968
          lex->no_write_to_binlog= $3;
4969 4970 4971 4972 4973 4974 4975 4976 4977
        }
        add_part_extra
        {}
        ;

add_part_extra:
        | '(' part_def_list ')'
        {
          LEX *lex= Lex;
4978
          lex->part_info->no_parts= lex->part_info->partitions.elements;
4979 4980 4981 4982 4983 4984 4985 4986 4987
        }
        | PARTITIONS_SYM ulong_num
        {
          LEX *lex= Lex;
          lex->part_info->no_parts= $2;
        }
        ;

reorg_partition_rule:
4988
        REORGANIZE_SYM PARTITION_SYM opt_no_write_to_binlog
4989 4990 4991 4992 4993
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
4994
            mem_alloc_error(sizeof(partition_info));
4995 4996
            YYABORT;
          }
4997
          lex->no_write_to_binlog= $3;
4998
        }
4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012
        reorg_parts_rule
        ;

reorg_parts_rule:
        /* empty */
        {
	  Lex->alter_info.flags|= ALTER_TABLE_REORG;
        }
        |
        alt_part_name_list
        {
	  Lex->alter_info.flags|= ALTER_REORGANIZE_PARTITION;
        }
        INTO '(' part_def_list ')'
5013 5014
        {
          LEX *lex= Lex;
5015
          lex->part_info->no_parts= lex->part_info->partitions.elements;
5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026
        }
        ;

alt_part_name_list:
        alt_part_name_item {}
        | alt_part_name_list ',' alt_part_name_item {}
        ;

alt_part_name_item:
        ident
        {
5027 5028 5029 5030 5031
          if (Lex->alter_info.partition_names.push_back($1.str))
          {
            mem_alloc_error(1);
            YYABORT;
          }
5032 5033 5034 5035 5036 5037 5038
        }
        ;

/*
  End of management of partition commands
*/

5039 5040 5041 5042
alter_list:
        alter_list_item
	| alter_list ',' alter_list_item
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5043 5044

add_column:
5045
	ADD opt_column
5046 5047
	{
	  LEX *lex=Lex;
5048 5049
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
5050
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5051 5052

alter_list_item:
5053 5054 5055 5056
	add_column column_def opt_place { }
	| ADD key_def
	  {
	    Lex->alter_info.flags|= ALTER_ADD_INDEX;
5057
	  }
5058 5059 5060 5061
	| add_column '(' field_list ')'
          {
	    Lex->alter_info.flags|= ALTER_ADD_COLUMN | ALTER_ADD_INDEX;
          }
5062 5063 5064
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
5065
	     lex->change= $3.str;
5066
	     lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
5067
	  }
5068
          field_spec opt_place
5069 5070 5071
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
5072
            lex->length=lex->dec=0; lex->type=0;
5073
            lex->default_value= lex->on_update_value= 0;
5074
            lex->comment=null_lex_str;
5075
	    lex->charset= NULL;
5076
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
5077 5078 5079 5080
          }
          type opt_attribute
          {
            LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5081
            if (add_field_to_list(lex->thd,$3.str,
5082 5083
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
5084
                                  lex->default_value, lex->on_update_value,
5085
                                  &lex->comment,
5086
				  $3.str, &lex->interval_list, lex->charset,
5087
				  lex->uint_geom_type))
5088 5089 5090
	       YYABORT;
          }
          opt_place
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5091
	| DROP opt_column field_ident opt_restrict
5092 5093
	  {
	    LEX *lex=Lex;
5094
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
5095
                                                               $3.str));
5096
	    lex->alter_info.flags|= ALTER_DROP_COLUMN;
5097
	  }
5098 5099 5100 5101
	| DROP FOREIGN KEY_SYM opt_ident
          {
	    Lex->alter_info.flags|= ALTER_DROP_INDEX;
          }
5102 5103 5104
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
5105 5106 5107
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
				               primary_key_name));
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
5108
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5109
	| DROP key_or_index field_ident
5110 5111
	  {
	    LEX *lex=Lex;
5112 5113 5114
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
					                       $3.str));
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
5115
	  }
5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127
	| DISABLE_SYM KEYS
          {
	    LEX *lex=Lex;
            lex->alter_info.keys_onoff= DISABLE;
	    lex->alter_info.flags|= ALTER_KEYS_ONOFF;
          }
	| ENABLE_SYM KEYS
          {
	    LEX *lex=Lex;
            lex->alter_info.keys_onoff= ENABLE;
	    lex->alter_info.flags|= ALTER_KEYS_ONOFF;
          }
5128
	| ALTER opt_column field_ident SET DEFAULT signed_literal
5129 5130
	  {
	    LEX *lex=Lex;
5131
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,$6));
5132
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
5133
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5134
	| ALTER opt_column field_ident DROP DEFAULT
5135 5136
	  {
	    LEX *lex=Lex;
5137 5138
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,
                                                                  (Item*) 0));
5139
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
5140
	  }
5141
	| RENAME opt_to table_ident
5142
	  {
5143
	    LEX *lex=Lex;
5144
	    lex->select_lex.db=$3->db.str;
5145
	    lex->name= $3->table.str;
5146 5147 5148
            if (check_table_name($3->table.str,$3->table.length) ||
                $3->db.str && check_db_name($3->db.str))
            {
5149
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3->table.str);
5150 5151
              YYABORT;
            }
5152
	    lex->alter_info.flags|= ALTER_RENAME;
5153
	  }
5154 5155 5156 5157 5158 5159 5160 5161 5162 5163
	| 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))
	    {
5164 5165
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $5->name, $4->csname);
5166 5167
	      YYABORT;
	    }
5168
	    LEX *lex= Lex;
5169
	    lex->create_info.table_charset=
monty@mysql.com's avatar
monty@mysql.com committed
5170 5171 5172
	      lex->create_info.default_table_charset= $5;
	    lex->create_info.used_fields|= (HA_CREATE_USED_CHARSET |
					    HA_CREATE_USED_DEFAULT_CHARSET);
5173
	    lex->alter_info.flags|= ALTER_CONVERT;
5174
	  }
5175
        | create_table_options_space_separated
5176 5177
	  {
	    LEX *lex=Lex;
5178
	    lex->alter_info.flags|= ALTER_OPTIONS;
5179
	  }
5180 5181 5182 5183
	| FORCE_SYM
	  {
	    Lex->alter_info.flags|= ALTER_FORCE;
	   }
5184
	| order_clause
5185 5186
	  {
	    LEX *lex=Lex;
5187
	    lex->alter_info.flags|= ALTER_ORDER;
5188
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5189 5190 5191

opt_column:
	/* empty */	{}
5192
	| COLUMN_SYM	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5193 5194

opt_ignore:
5195 5196 5197
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5198 5199

opt_restrict:
5200 5201 5202 5203
	/* 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
5204 5205 5206 5207

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
5208
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5209 5210 5211 5212

opt_to:
	/* empty */	{}
	| TO_SYM	{}
5213
	| EQ		{}
5214
	| AS		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5215

nick@mysql.com's avatar
nick@mysql.com committed
5216
/*
5217
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5218 5219
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5220
slave:
5221
	  START_SYM SLAVE slave_thread_opts
5222 5223 5224 5225 5226 5227
          {
	    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
5228
            /* If you change this code don't forget to update SLAVE START too */
5229 5230 5231
          }
          slave_until
          {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5232 5233 5234 5235 5236
        | 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
5237
            /* If you change this code don't forget to update SLAVE STOP too */
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5238
          }
5239 5240 5241 5242 5243
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
5244 5245 5246 5247 5248
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
5249 5250 5251 5252 5253 5254 5255 5256
	| 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
5257

5258
start:
5259 5260
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
5261 5262 5263
          LEX *lex= Lex;
          lex->sql_command= SQLCOM_BEGIN;
          lex->start_transaction_opt= $3;
5264
        }
5265 5266
	;

5267 5268 5269 5270 5271 5272
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
5273
        ;
5274

5275
slave_thread_opts:
5276 5277
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
5278
        {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5279
	;
5280 5281

slave_thread_opt_list:
5282
	slave_thread_opt
5283 5284
	| slave_thread_opt_list ',' slave_thread_opt
	;
5285 5286

slave_thread_opt:
5287
	/*empty*/	{}
5288
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
5289
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
5290
	;
5291

5292 5293 5294 5295 5296 5297 5298 5299 5300 5301
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)))
            {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5302 5303
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


5315 5316 5317 5318 5319
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
5320
	table_list FROM TEXT_STRING_sys
5321 5322
        {
	  Lex->backup_dir = $6.str;
5323
        };
5324

5325 5326 5327 5328 5329
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
5330
	table_list TO_SYM TEXT_STRING_sys
5331 5332
        {
	  Lex->backup_dir = $6.str;
5333
        };
5334

5335 5336 5337 5338 5339 5340
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
5341 5342
	table_list opt_checksum_type
        {}
5343 5344
	;

5345 5346 5347 5348 5349 5350
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
5351
repair:
5352
	REPAIR opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5353
	{
5354 5355
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
5356
           lex->no_write_to_binlog= $2;
5357
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5358
	}
5359 5360 5361
	table_list opt_mi_repair_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5362

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5363
opt_mi_repair_type:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5364
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
5365
	| mi_repair_types {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5366

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5367 5368
mi_repair_types:
	mi_repair_type {}
5369
	| mi_repair_type mi_repair_types {};
5370

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5371
mi_repair_type:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5372
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
5373
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
5374
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5375 5376

analyze:
5377
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5378
	{
5379 5380
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
5381
           lex->no_write_to_binlog= $2;
5382
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5383
	}
5384 5385 5386
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5387

5388 5389 5390 5391 5392 5393
binlog_base64_event:
        BINLOG_SYM TEXT_STRING_sys
        {
           Lex->sql_command = SQLCOM_BINLOG_BASE64_EVENT;
           Lex->comment= $2;
        }
5394
        ;
5395

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5396
check:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5397
	CHECK_SYM table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5398
	{
5399 5400 5401 5402 5403 5404 5405 5406 5407
	  LEX *lex=Lex;

	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "CHECK");
	    YYABORT;
	  }
	  lex->sql_command = SQLCOM_CHECK;
	  lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5408
	}
5409 5410 5411
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5412

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5413 5414
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
5415
	| mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5416 5417 5418

mi_check_types:
	mi_check_type {}
5419
	| mi_check_type mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5420 5421 5422 5423 5424 5425

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5428
optimize:
5429
	OPTIMIZE opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5430
	{
5431 5432
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5433
           lex->no_write_to_binlog= $2;
5434
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5435
	}
5436 5437 5438
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5439

5440 5441 5442
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
5443
	| LOCAL_SYM  { $$= 1; }
5444 5445
	;

5446 5447 5448
rename:
	RENAME table_or_tables
	{
5449
          Lex->sql_command= SQLCOM_RENAME_TABLE;
5450
	}
5451 5452
	table_to_table_list
	{}
5453 5454 5455 5456
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
5457
	;
5458

5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471
rename_list:
        user TO_SYM user
        {
          if (Lex->users_list.push_back($1) || Lex->users_list.push_back($3))
            YYABORT;
        }
        | rename_list ',' user TO_SYM user
          {
            if (Lex->users_list.push_back($3) || Lex->users_list.push_back($5))
              YYABORT;
          }
        ;

5472 5473
table_to_table_list:
	table_to_table
5474
	| table_to_table_list ',' table_to_table;
5475 5476 5477

table_to_table:
	table_ident TO_SYM table_ident
5478
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5479
	  LEX *lex=Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5480
	  SELECT_LEX *sl= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5481 5482 5483 5484
	  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))
5485
	    YYABORT;
5486
	};
5487

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5488
keycache:
5489
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5490 5491
        {
          LEX *lex=Lex;
5492
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
5493
	  lex->ident= $5;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5494 5495 5496 5497 5498 5499 5500 5501
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
5502
        table_ident cache_keys_spec
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5503 5504 5505 5506 5507 5508
        {
          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(),
5509
                                      (List<String> *)0))
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5510 5511 5512 5513
            YYABORT;
        }
        ;

5514 5515 5516
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
5517
	;
5518

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5519
preload:
5520
	LOAD INDEX_SYM INTO CACHE_SYM
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533
	{
	  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
5534
	table_ident cache_keys_spec opt_ignore_leaves
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5535 5536 5537
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5538 5539
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5540 5541 5542 5543 5544 5545
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

5546
cache_keys_spec:
5547
        { Select->interval_list.empty(); }
5548 5549 5550 5551 5552 5553 5554
        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
5555

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5556
cache_key_list_or_empty:
5557
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
5558
	| opt_key_or_index '(' key_usage_list2 ')'
5559 5560 5561 5562
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5563 5564
	;

5565
opt_ignore_leaves:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5566 5567 5568 5569 5570
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5571
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5572
  Select : retrieve data from table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5573 5574 5575 5576
*/


select:
5577 5578 5579 5580
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5581 5582
	}
	;
5583

5584
/* Need select_init2 for subselects. */
5585
select_init:
5586
	SELECT_SYM select_init2
5587
	|
5588 5589 5590 5591
	'(' select_paren ')' union_opt;

select_paren:
	SELECT_SYM select_part2
5592
	  {
5593
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5594
            SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5595
	    if (sel->set_braces(1))
5596
	    {
5597
	      yyerror(ER(ER_SYNTAX_ERROR));
5598 5599
	      YYABORT;
	    }
5600 5601
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5602
	  {
5603
	    yyerror(ER(ER_SYNTAX_ERROR));
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5604 5605
	    YYABORT;
	  }
5606
            /* select in braces, can't contain global parameters */
5607 5608 5609
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
5610 5611
          }
	| '(' select_paren ')';
5612

5613 5614
select_init2:
	select_part2
5615
        {
5616
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5617
          SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5618
          if (lex->current_select->set_braces(0))
5619
	  {
5620
	    yyerror(ER(ER_SYNTAX_ERROR));
5621 5622
	    YYABORT;
	  }
5623 5624
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5625
	  {
5626
	    yyerror(ER(ER_SYNTAX_ERROR));
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5627 5628
	    YYABORT;
	  }
5629
	}
5630
	union_clause
5631 5632
	;

5633
select_part2:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5634
	{
5635 5636
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
5637 5638
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
5639
	  lex->current_select->parsing_place= SELECT_LIST;
5640 5641 5642
	}
	select_options select_item_list
	{
5643
	  Select->parsing_place= NO_MATTER;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5644
	}
5645
	select_into select_lock_type;
5646

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5647
select_into:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5648
	opt_order_clause opt_limit_clause {}
5649
        | into
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5650
	| select_from
5651 5652
	| into select_from
	| select_from into;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5653 5654

select_from:
5655 5656
	  FROM join_table_list where_clause group_clause having_clause
	       opt_order_clause opt_limit_clause procedure_clause
5657
        | FROM DUAL_SYM where_clause opt_limit_clause
timour@mysql.com's avatar
timour@mysql.com committed
5658 5659 5660 5661
          /* 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 :) */
5662
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5663 5664 5665

select_options:
	/* empty*/
monty@mysql.com's avatar
monty@mysql.com committed
5666 5667
	| select_option_list
	  {
joreland@mysql.com's avatar
joreland@mysql.com committed
5668
	    if (Select->options & SELECT_DISTINCT && Select->options & SELECT_ALL)
monty@mysql.com's avatar
monty@mysql.com committed
5669
	    {
monty@mysql.com's avatar
monty@mysql.com committed
5670
	      my_error(ER_WRONG_USAGE, MYF(0), "ALL", "DISTINCT");
monty@mysql.com's avatar
monty@mysql.com committed
5671 5672 5673
              YYABORT;
	    }
          }
monty@mysql.com's avatar
monty@mysql.com committed
5674
	  ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5675 5676 5677

select_option_list:
	select_option_list select_option
5678
	| select_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5679 5680

select_option:
5681
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
5682 5683 5684 5685 5686 5687
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
monty@mysql.com's avatar
monty@mysql.com committed
5688
	| DISTINCT         { Select->options|= SELECT_DISTINCT; }
5689 5690
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702
	| 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;
	  }
5703
	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
5704 5705 5706 5707
	| SQL_CACHE_SYM
	  {
	    Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
	  }
joreland@mysql.com's avatar
joreland@mysql.com committed
5708
	| ALL		    { Select->options|= SELECT_ALL; }
5709
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5710

monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5711 5712 5713
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
5714 5715
	  {
	    LEX *lex=Lex;
5716
	    lex->current_select->set_lock_for_tables(TL_WRITE);
5717
	    lex->safe_to_cache_query=0;
5718
	  }
5719
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
5720 5721
	  {
	    LEX *lex=Lex;
5722 5723
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
5724
	    lex->safe_to_cache_query=0;
5725 5726
	  }
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5727

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5728 5729 5730 5731 5732
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
5733
	    THD *thd= YYTHD;
5734 5735 5736 5737
	    if (add_item_to_list(thd,
                                 new Item_field(&thd->lex->current_select->
                                                context,
                                                NULL, NULL, "*")))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5738
	      YYABORT;
5739
	    (thd->lex->current_select->with_wild)++;
5740
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5741 5742 5743 5744 5745


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5746
	    if (add_item_to_list(YYTHD, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5747 5748
	      YYABORT;
	    if ($4.str)
5749 5750 5751 5752
            {
	      $2->set_name($4.str, $4.length, system_charset_info);
              $2->is_autogenerated_name= FALSE;
            }
5753 5754 5755 5756 5757 5758
	    else if (!$2->name) {
	      char *str = $1;
	      if (str[-1] == '`')
	        str--;
	      $2->set_name(str,(uint) ($3 - str), YYTHD->charset());
	    }
5759
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5760 5761

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

remember_end:
5765
	{ $$=(char*) Lex->tok_end; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5766 5767 5768

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

select_alias:
5772
	/* empty */		{ $$=null_lex_str;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5773 5774 5775 5776 5777
	| 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
5778 5779 5780

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5783
/* all possible expressions */
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
5784
expr:	
5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806
	  bool_term { Select->expr_list.push_front(new List<Item>); }
          bool_or_expr
          {
            List<Item> *list= Select->expr_list.pop();
            if (list->elements)
            {
              list->push_front($1);
              $$= new Item_cond_or(*list);
              /* optimize construction of logical OR to reduce
                 amount of objects for complex expressions */
            }
            else
              $$= $1;
            delete list;
          }
	;

bool_or_expr:
	/* empty */
        | bool_or_expr or bool_term
          { Select->expr_list.head()->push_back($3); }
        ;
5807 5808

bool_term:
5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831
	bool_term XOR bool_term { $$= new Item_cond_xor($1,$3); }
	| bool_factor { Select->expr_list.push_front(new List<Item>); }
          bool_and_expr
          {
            List<Item> *list= Select->expr_list.pop();
            if (list->elements)
            {
              list->push_front($1);
              $$= new Item_cond_and(*list);
              /* optimize construction of logical AND to reduce
                 amount of objects for complex expressions */
            }
            else
              $$= $1;
            delete list;
          }
	;

bool_and_expr:
	/* empty */
        | bool_and_expr and bool_factor
          { Select->expr_list.head()->push_back($3); }
        ;
5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848

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

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

bool_pri:
	bool_pri IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| bool_pri IS not NULL_SYM { $$= new Item_func_isnotnull($1); }
5849 5850 5851 5852 5853
	| bool_pri EQUAL_SYM predicate	{ $$= new Item_func_equal($1,$3); }
	| bool_pri comp_op predicate %prec EQ
	  { $$= (*$2)(0)->create($1,$3); }
	| bool_pri comp_op all_or_any in_subselect %prec EQ
	  { $$= all_any_subquery_creator($1, $2, $3, $4); }
5854 5855 5856 5857
	| predicate ;

predicate:
	 bit_expr IN_SYM '(' expr_list ')'
5858 5859 5860 5861 5862 5863 5864 5865 5866
	  { 
            if ($4->elements == 1)
              $$= new Item_func_eq($1, $4->head());
            else
            {
              $4->push_front($1);
              $$= new Item_func_in(*$4);
            }
          }
5867
	| bit_expr not IN_SYM '(' expr_list ')'
5868 5869 5870 5871 5872 5873
          {
            if ($5->elements == 1)
              $$= new Item_func_ne($1, $5->head());
            else
            {
              $5->push_front($1);
5874 5875 5876
              Item_func_in *item = new Item_func_in(*$5);
              item->negate();
              $$= item;
5877 5878
            }            
          }
5879 5880 5881
        | bit_expr IN_SYM in_subselect
	  { $$= new Item_in_subselect($1, $3); }
	| bit_expr not IN_SYM in_subselect
5882
          { $$= negate_expression(YYTHD, new Item_in_subselect($1, $4)); }
5883 5884 5885
	| bit_expr BETWEEN_SYM bit_expr AND_SYM predicate
	  { $$= new Item_func_between($1,$3,$5); }
	| bit_expr not BETWEEN_SYM bit_expr AND_SYM predicate
5886 5887 5888 5889 5890
    {
      Item_func_between *item= new Item_func_between($1,$4,$6);
      item->negate();
      $$= item;
    }
5891 5892 5893 5894
	| bit_expr SOUNDS_SYM LIKE bit_expr
	  { $$= new Item_func_eq(new Item_func_soundex($1),
				 new Item_func_soundex($4)); }
	| bit_expr LIKE simple_expr opt_escape
5895
          { $$= new Item_func_like($1,$3,$4,Lex->escape_used); }
5896
	| bit_expr not LIKE simple_expr opt_escape
5897
          { $$= new Item_func_not(new Item_func_like($1,$4,$5, Lex->escape_used)); }
5898 5899
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
5900
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942
	| bit_expr ;

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

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

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

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

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

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

or:	OR_SYM | OR2_SYM;
and:	AND_SYM | AND_AND_SYM;
not:	NOT_SYM | NOT2_SYM;
not2:	'!' | NOT2_SYM;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5943

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955
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; }
        ;

5956
interval_expr:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5957
         INTERVAL_SYM expr { $$=$2; }
5958 5959
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5960 5961
simple_expr:
	simple_ident
5962
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5963
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5964 5965 5966
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
5967
                                                            YYTHD->charset()));
5968
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5969
	| literal
5970
	| param_marker
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5971
	| '@' ident_or_text SET_VAR expr
5972 5973
	  {
	    $$= new Item_func_set_user_var($2,$4);
5974 5975 5976
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5977
	  }
5978
	| '@' ident_or_text
5979 5980
	  {
	    $$= new Item_func_get_user_var($2);
5981 5982 5983
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5984
	  }
5985
	| '@' '@' opt_var_ident_type ident_or_text opt_component
5986
	  {
5987 5988 5989 5990 5991 5992

            if ($4.str && $5.str && check_reserved_words(&$4))
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
5993
	    if (!($$= get_system_var(YYTHD, $3, $4, $5)))
5994
	      YYABORT;
5995
	    Lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5996
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5997
	| sum_expr
5998 5999 6000 6001 6002 6003
	| simple_expr OR_OR_SYM simple_expr
	  { $$= new Item_func_concat($1, $3); }
	| '+' simple_expr %prec NEG	{ $$= $2; }
	| '-' simple_expr %prec NEG	{ $$= new Item_func_neg($2); }
	| '~' simple_expr %prec NEG	{ $$= new Item_func_bit_neg($2); }
	| not2 simple_expr %prec NEG	{ $$= negate_expression(YYTHD, $2); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6004
	| '(' expr ')'		{ $$= $2; }
6005 6006 6007 6008 6009
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
6010
	| ROW_SYM '(' expr ',' expr_list ')'
6011
	  {
6012 6013
	    $5->push_front($3);
	    $$= new Item_row(*$5);
6014
	  }
6015
	| EXISTS exists_subselect { $$= $2; }
6016
	| singlerow_subselect   { $$= $1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6017
	| '{' ident expr '}'	{ $$= $3; }
6018
        | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
6019
          { $2->push_front($5);
6020 6021
            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
6022
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
6023
	| BINARY simple_expr %prec NEG
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6024
	  {
6025
            $$= create_func_cast($2, ITEM_CAST_CHAR, -1, 0, &my_charset_bin);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6026
	  }
6027
	| CAST_SYM '(' expr AS cast_type ')'
6028
	  {
6029
            LEX *lex= Lex;
6030
	    $$= create_func_cast($3, $5,
6031 6032 6033
                                 lex->length ? atoi(lex->length) : -1,
                                 lex->dec ? atoi(lex->dec) : 0,
                                 lex->charset);
6034
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6035
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
6036
	  { $$= new Item_func_case(* $4, $2, $5 ); }
6037
	| CONVERT_SYM '(' expr ',' cast_type ')'
6038 6039 6040
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
6041
                                 Lex->dec ? atoi(Lex->dec) : 0,
6042 6043
				 Lex->charset);
	  }
6044 6045
	| 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
6046
	| DEFAULT '(' simple_ident ')'
6047 6048 6049 6050 6051
	  {
	    if ($3->is_splocal())
	    {
	      Item_splocal *il= static_cast<Item_splocal *>($3);

6052
	      my_error(ER_WRONG_COLUMN_NAME, MYF(0), il->my_name()->str);
6053 6054
	      YYABORT;
	    }
6055
	    $$= new Item_default_value(Lex->current_context(), $3);
6056
	  }
6057
	| VALUES '(' simple_ident_nospvar ')'
6058
	  { $$= new Item_insert_value(Lex->current_context(), $3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6059
	| FUNC_ARG0 '(' ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
6060 6061 6062
	  {
	    if (!$1.symbol->create_func)
	    {
6063 6064 6065
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
6066 6067 6068 6069
	      YYABORT;
	    }
	    $$= ((Item*(*)(void))($1.symbol->create_func))();
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6070
	| FUNC_ARG1 '(' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
6071 6072 6073
	  {
	    if (!$1.symbol->create_func)
	    {
6074 6075 6076
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
6077 6078 6079 6080
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6081
	| FUNC_ARG2 '(' expr ',' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
6082 6083 6084
	  {
	    if (!$1.symbol->create_func)
	    {
6085 6086 6087
	      my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
6088 6089 6090 6091
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6092
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
6093 6094 6095
	  {
	    if (!$1.symbol->create_func)
	    {
6096 6097 6098
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
6099 6100 6101 6102
	      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
6103 6104 6105 6106
	| 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); }
6107 6108
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6109 6110 6111 6112 6113 6114
	| 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); }
6115 6116
	| CHAR_SYM '(' expr_list USING charset_name ')'
	  { $$= new Item_func_char(*$3, $5); }
6117 6118
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6119 6120
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
6121 6122
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6123 6124 6125
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
6126
	  { $5->push_front($3); $$= new Item_func_concat_ws(*$5); }
6127 6128
	| CONVERT_TZ_SYM '(' expr ',' expr ',' expr ')'
	  {
6129 6130
            if (Lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
6131 6132
	    $$= new Item_func_convert_tz($3, $5, $7);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6133
	| CURDATE optional_braces
6134
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6135
	| CURTIME optional_braces
6136
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6137
	| CURTIME '(' expr ')'
6138
	  {
6139
	    $$= new Item_func_curtime_local($3);
6140
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6141
	  }
6142
	| CURRENT_USER optional_braces
6143
          { $$= create_func_current_user(); }
6144 6145 6146 6147
	| 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
6148
	| DATABASE '(' ')'
6149
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6150
	    $$= new Item_func_database();
6151
            Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6152
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6153
	| DATE_SYM '(' expr ')'
6154
	  { $$= new Item_date_typecast($3); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6155 6156
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6157
	| ELT_FUNC '(' expr ',' expr_list ')'
6158
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6159 6160
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6161 6162 6163
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
6164
	    Lex->uncacheable(UNCACHEABLE_RAND);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6165
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6166
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
6167
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6168
	  { $$= new Item_func_decode($3,$5.str); }
6169
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6170
	 { $$= new Item_func_encode($3,$5.str); }
6171
	| DES_DECRYPT_SYM '(' expr ')'
6172
        { $$= new Item_func_des_decrypt($3); }
6173
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
6174
        { $$= new Item_func_des_decrypt($3,$5); }
6175
	| DES_ENCRYPT_SYM '(' expr ')'
6176
        { $$= new Item_func_des_encrypt($3); }
6177
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
6178
        { $$= new Item_func_des_encrypt($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190
	| 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); }
	| 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
6191
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6192 6193
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
6194
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6195 6196 6197 6198 6199
	| geometry_function
	  {
#ifdef HAVE_SPATIAL
	    $$= $1;
#else
6200 6201
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
6202 6203 6204
	    YYABORT;
#endif
	  }
6205
	| GET_FORMAT '(' date_time_type  ',' expr ')'
6206
	  { $$= new Item_func_get_format($3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6207 6208 6209 6210 6211 6212
	| 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); }
6213
	| interval_expr interval '+' expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6214
	  /* we cannot put interval before - */
6215 6216 6217 6218 6219
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
6220
              yyerror(ER(ER_SYNTAX_ERROR));
6221 6222 6223 6224
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6225 6226
	| LAST_INSERT_ID '(' ')'
	  {
6227
	    $$= new Item_func_last_insert_id();
6228
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6229 6230 6231
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
6232
	    $$= new Item_func_last_insert_id($3);
6233
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6234 6235 6236 6237 6238 6239 6240
	  }
	| 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); }
6241
	| GREATEST_SYM '(' expr ',' expr_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6242 6243 6244
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
6245
	| LOG_SYM '(' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6246
	  { $$= new Item_func_log($3); }
6247
	| LOG_SYM '(' expr ',' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6248
	  { $$= new Item_func_log($3, $5); }
6249
	| MASTER_POS_WAIT '(' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6250
	  {
6251
	    $$= new Item_master_pos_wait($3, $5);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6252
	    Lex->safe_to_cache_query=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6253
		  }
6254
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6255
	  {
6256
	    $$= new Item_master_pos_wait($3, $5, $7);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6257
	    Lex->safe_to_cache_query=0;
6258
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6259 6260
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6261 6262
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
6263 6264
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6265 6266 6267
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
	| NOW_SYM optional_braces
6268
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6269
	| NOW_SYM '(' expr ')'
6270
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6271
	| PASSWORD '(' expr ')'
6272
	  {
6273 6274
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
6275 6276 6277 6278
	      (Item *) new Item_func_password($3);
	  }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new Item_func_old_password($3); }
6279
	| POSITION_SYM '(' bit_expr IN_SYM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6280
	  { $$ = new Item_func_locate($5,$3); }
6281 6282
	| QUARTER_SYM '(' expr ')'
	  { $$ = new Item_func_quarter($3); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6283
	| RAND '(' expr ')'
6284
	  { $$= new Item_func_rand($3); Lex->uncacheable(UNCACHEABLE_RAND);}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6285
	| RAND '(' ')'
6286
	  { $$= new Item_func_rand(); Lex->uncacheable(UNCACHEABLE_RAND);}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6287 6288 6289 6290 6291 6292 6293
	| 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); }
6294 6295 6296 6297 6298
	| 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
6299 6300 6301 6302
	| 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
6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314
	| 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); }
6315 6316 6317 6318
	| SYSDATE optional_braces
	  { $$= new Item_func_sysdate_local(); Lex->safe_to_cache_query=0;}
	| SYSDATE '(' expr ')'
	  { $$= new Item_func_sysdate_local($3); Lex->safe_to_cache_query=0;}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6319
	| TIME_SYM '(' expr ')'
6320 6321 6322
	  { $$= 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
6323
	| TIMESTAMP '(' expr ',' expr ')'
6324
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
6325 6326 6327 6328
	| 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
6329
	| TRIM '(' expr ')'
6330 6331
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6332
	  { $$= new Item_func_ltrim($6,$4); }
6333
	| TRIM '(' TRAILING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6334
	  { $$= new Item_func_rtrim($6,$4); }
6335
	| TRIM '(' BOTH expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6336
	  { $$= new Item_func_trim($6,$4); }
6337 6338 6339 6340 6341 6342
	| 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
6343 6344
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
6345 6346
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
6347
	| ident '.' ident '(' udf_expr_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6348
	  {
6349
	    LEX *lex= Lex;
6350 6351 6352
	    sp_name *name= new sp_name($1, $3);

	    name->init_qname(YYTHD);
6353
	    sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
6354
	    if ($5)
6355
	      $$= new Item_func_sp(Lex->current_context(), name, *$5);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6356
	    else
6357
	      $$= new Item_func_sp(Lex->current_context(), name);
6358
	    lex->safe_to_cache_query=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6359
	  }
6360
	| IDENT_sys '(' 
6361 6362
          {
#ifdef HAVE_DLOPEN
6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381
            udf_func *udf= 0;
            if (using_udf_functions &&
                (udf= find_udf($1.str, $1.length)) &&
                udf->type == UDFTYPE_AGGREGATE)
            {
              LEX *lex= Lex;
              if (lex->current_select->inc_in_sum_expr())
              {
                yyerror(ER(ER_SYNTAX_ERROR));
                YYABORT;
              }
            }
            $<udf>$= udf;
#endif
          }
          udf_expr_list ')'
          {
#ifdef HAVE_DLOPEN
            udf_func *udf= $<udf>3;
6382
            SELECT_LEX *sel= Select;
6383

6384
            if (udf)
6385
            {
6386 6387 6388
              if (udf->type == UDFTYPE_AGGREGATE)
                Select->in_sum_expr--;

6389 6390 6391 6392
              switch (udf->returns) {
              case STRING_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
6393 6394
                  if ($4 != NULL)
                    $$ = new Item_func_udf_str(udf, *$4);
6395 6396 6397 6398 6399
                  else
                    $$ = new Item_func_udf_str(udf);
                }
                else
                {
6400 6401
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_str(udf, *$4);
6402 6403 6404 6405 6406 6407 6408
                  else
                    $$ = new Item_sum_udf_str(udf);
                }
                break;
              case REAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
6409 6410
                  if ($4 != NULL)
                    $$ = new Item_func_udf_float(udf, *$4);
6411 6412 6413 6414 6415
                  else
                    $$ = new Item_func_udf_float(udf);
                }
                else
                {
6416 6417
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_float(udf, *$4);
6418 6419 6420 6421 6422 6423 6424
                  else
                    $$ = new Item_sum_udf_float(udf);
                }
                break;
              case INT_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
6425 6426
                  if ($4 != NULL)
                    $$ = new Item_func_udf_int(udf, *$4);
6427 6428 6429 6430 6431
                  else
                    $$ = new Item_func_udf_int(udf);
                }
                else
                {
6432 6433
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_int(udf, *$4);
6434 6435 6436 6437
                  else
                    $$ = new Item_sum_udf_int(udf);
                }
                break;
6438 6439 6440
              case DECIMAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
6441 6442
                  if ($4 != NULL)
                    $$ = new Item_func_udf_decimal(udf, *$4);
6443 6444 6445 6446 6447
                  else
                    $$ = new Item_func_udf_decimal(udf);
                }
                else
                {
6448 6449
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_decimal(udf, *$4);
6450 6451 6452 6453
                  else
                    $$ = new Item_sum_udf_decimal(udf);
                }
                break;
6454 6455 6456 6457 6458 6459 6460
              default:
                YYABORT;
              }
            }
            else
#endif /* HAVE_DLOPEN */
            {
6461
	      LEX *lex= Lex;
6462 6463
              sp_name *name= sp_name_current_db_new(YYTHD, $1);

6464
              sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
6465 6466
              if ($4)
                $$= new Item_func_sp(Lex->current_context(), name, *$4);
6467
              else
6468
                $$= new Item_func_sp(Lex->current_context(), name);
6469
	      lex->safe_to_cache_query=0;
6470 6471
	    }
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6472
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
6473
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6474 6475
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6476
	| UNIX_TIMESTAMP '(' ')'
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6477 6478
	  {
	    $$= new Item_func_unix_timestamp();
6479
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6480
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6481 6482 6483
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
6484
	  { $$= new Item_func_user(FALSE); Lex->safe_to_cache_query=0; }
6485 6486 6487 6488 6489 6490
	| 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
6491
	| WEEK_SYM '(' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6492
	  {
6493
            $$= new Item_func_week($3,new Item_int((char*) "0",
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6494
				   YYTHD->variables.default_week_format,1));
6495
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6496 6497 6498 6499 6500 6501 6502 6503
	| 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); }
serg@serg.mylan's avatar
serg@serg.mylan committed
6504
	| BENCHMARK_SYM '(' ulong_num ',' expr ')'
6505
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6506
	    $$=new Item_func_benchmark($3,$5);
6507
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6508
	  }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6509
	| EXTRACT_SYM '(' interval FROM expr ')'
6510
	{ $$=new Item_extract( $3, $5); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6511

hf@deer.(none)'s avatar
hf@deer.(none) committed
6512
geometry_function:
serg@serg.mylan's avatar
serg@serg.mylan committed
6513 6514 6515
	  CONTAINS_SYM '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_spatial_rel($3, $5, Item_func::SP_CONTAINS_FUNC)); }
	| GEOMFROMTEXT '(' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
6516 6517 6518 6519 6520 6521 6522 6523 6524
	  { $$= 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,
6525 6526
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6527 6528
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
6529
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6530 6531
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
6532
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546
 	| 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,
6547
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6548 6549
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
6550
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562
	| 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,
6563
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6564 6565 6566 6567 6568 6569 6570 6571 6572 6573
 	| 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)); }
	;

6574 6575 6576 6577 6578 6579
fulltext_options:
        /* nothing */                   { $$= FT_NL;  }
        | WITH QUERY_SYM EXPANSION_SYM  { $$= FT_NL | FT_EXPAND; }
        | IN_SYM BOOLEAN_SYM MODE_SYM   { $$= FT_BOOL; }
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6580
udf_expr_list:
6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605
	/* 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)
6606 6607 6608 6609
          {
	    $2->set_name($4.str, $4.length, system_charset_info);
            $2->is_autogenerated_name= FALSE;
          }
6610
	  else
6611
	    $2->set_name($1, (uint) ($3 - $1), YYTHD->charset());
6612 6613 6614
	  $$= $2;
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6615 6616 6617 6618

sum_expr:
	AVG_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_avg($3); }
6619 6620
	| AVG_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_avg_distinct($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6621 6622 6623 6624
	| BIT_AND  '(' in_sum_expr ')'
	  { $$=new Item_sum_and($3); }
	| BIT_OR  '(' in_sum_expr ')'
	  { $$=new Item_sum_or($3); }
6625 6626
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
6627
	| COUNT_SYM '(' opt_all '*' ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6628 6629 6630
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
6631
	| COUNT_SYM '(' DISTINCT
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6632
	  { Select->in_sum_expr++; }
6633
	   expr_list
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6634
	  { Select->in_sum_expr--; }
6635 6636
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6637 6638 6639 6640
	| 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); }
6641 6642 6643 6644 6645 6646 6647
/*
   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
6648 6649
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
6650 6651
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6652
	| STD_SYM '(' in_sum_expr ')'
6653
	  { $$=new Item_sum_std($3, 0); }
6654
	| VARIANCE_SYM '(' in_sum_expr ')'
6655 6656 6657 6658 6659
	  { $$=new Item_sum_variance($3, 0); }
	| STDDEV_SAMP_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_std($3, 1); }
	| VAR_SAMP_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_variance($3, 1); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6660
	| SUM_SYM '(' in_sum_expr ')'
6661
	  { $$=new Item_sum_sum($3); }
6662 6663
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
monty@mysql.com's avatar
monty@mysql.com committed
6664 6665 6666 6667 6668
	| 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
6669
	  {
6670 6671
            SELECT_LEX *sel= Select;
	    sel->in_sum_expr--;
6672
	    $$=new Item_func_group_concat(Lex->current_context(), $3, $5,
6673
                                          sel->gorder_list, $7);
monty@mysql.com's avatar
monty@mysql.com committed
6674
	    $5->empty();
6675 6676 6677 6678 6679 6680 6681
	  };

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

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

6685 6686

opt_gorder_clause:
6687 6688
	  /* empty */
	  {
6689
            Select->gorder_list = NULL;
6690 6691 6692
	  }
	| order_clause
          {
6693 6694 6695
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
6696
				     sizeof(st_sql_list));
6697
	    select->order_list.empty();
6698
	  };
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6699

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6700 6701

in_sum_expr:
6702
	opt_all
6703 6704 6705 6706
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
6707
	    yyerror(ER(ER_SYNTAX_ERROR));
6708
	    YYABORT;
6709 6710
	  }
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6711 6712
	expr
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6713
	  Select->in_sum_expr--;
6714
	  $$= $3;
6715
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6716

6717
cast_type:
6718 6719
        BINARY opt_len		{ $$=ITEM_CAST_CHAR; Lex->charset= &my_charset_bin; Lex->dec= 0; }
        | CHAR_SYM opt_len opt_binary	{ $$=ITEM_CAST_CHAR; Lex->dec= 0; }
6720
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; Lex->dec=0; }
6721 6722 6723 6724 6725 6726 6727 6728
        | SIGNED_SYM		{ $$=ITEM_CAST_SIGNED_INT; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | SIGNED_SYM INT_SYM	{ $$=ITEM_CAST_SIGNED_INT; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | UNSIGNED		{ $$=ITEM_CAST_UNSIGNED_INT; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | UNSIGNED INT_SYM	{ $$=ITEM_CAST_UNSIGNED_INT; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | DATE_SYM		{ $$=ITEM_CAST_DATE; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | TIME_SYM		{ $$=ITEM_CAST_TIME; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | DATETIME		{ $$=ITEM_CAST_DATETIME; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | DECIMAL_SYM float_options { $$=ITEM_CAST_DECIMAL; Lex->charset= NULL; }
6729 6730
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6731
expr_list:
6732
	{ Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6733
	expr_list2
6734
	{ $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6735 6736

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

6740 6741
ident_list_arg:
          ident_list          { $$= $1; }
6742
        | '(' ident_list ')'  { $$= $2; };
6743

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6744
ident_list:
6745
        { Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6746
        ident_list2
6747
        { $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6748 6749

ident_list2:
6750
        simple_ident { Select->expr_list.head()->push_back($1); }
6751
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6752 6753 6754

opt_expr:
	/* empty */      { $$= NULL; }
6755
	| expr           { $$= $1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6756 6757 6758

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

when_list:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6762
        { Select->when_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6763
	when_list2
6764
	{ $$= Select->when_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6765 6766

when_list2:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6767
	expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6768
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6769
	    SELECT_LEX *sel=Select;
6770 6771
	    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
6772
	}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6773
	| when_list2 WHEN_SYM expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6774
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6775
	    SELECT_LEX *sel=Select;
6776 6777
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
6778
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6779

6780
/* Warning - may return NULL in case of incomplete SELECT */
6781 6782 6783
table_ref:
        table_factor            { $$=$1; }
        | join_table            { $$=$1; }
6784
          {
6785 6786 6787
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
6788
          }
6789 6790
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6791
join_table_list:
serg@serg.mylan's avatar
serg@serg.mylan committed
6792
	derived_table_list		{ YYERROR_UNLESS($$=$1); }
6793
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
6794

6795 6796
/* Warning - may return NULL in case of incomplete SELECT */
derived_table_list:
6797
        table_ref { $$=$1; }
6798 6799
        | derived_table_list ',' table_ref
          {
serg@serg.mylan's avatar
serg@serg.mylan committed
6800
            YYERROR_UNLESS($1 && ($$=$3));
6801
          }
6802 6803
        ;

6804 6805 6806 6807 6808 6809 6810
/*
  Notice that JOIN is a left-associative operation, and it must be parsed
  as such, that is, the parser must process first the left join operand
  then the right one. Such order of processing ensures that the parser
  produces correct join trees which is essential for semantic analysis
  and subsequent optimization phases.
*/
6811
join_table:
6812
/* INNER JOIN variants */
timour@mysql.com's avatar
timour@mysql.com committed
6813
        /*
6814 6815 6816
          Use %prec to evaluate production 'table_ref' before 'normal_join'
          so that [INNER | CROSS] JOIN is properly nested as other
          left-associative joins.
timour@mysql.com's avatar
timour@mysql.com committed
6817 6818
        */
        table_ref %prec TABLE_REF_PRIORITY normal_join table_ref
6819
          { YYERROR_UNLESS($1 && ($$=$3)); }
6820
	| table_ref STRAIGHT_JOIN table_factor
serg@serg.mylan's avatar
serg@serg.mylan committed
6821
	  { YYERROR_UNLESS($1 && ($$=$3)); $3->straight=1; }
6822 6823 6824 6825 6826
	| table_ref normal_join table_ref
          ON
          {
            YYERROR_UNLESS($1 && ($$=$3));
            /* Change the current name resolution context to a local context. */
6827
            if (push_new_name_resolution_context(YYTHD, $1, $3))
6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839
              YYABORT;
          }
          expr
	  {
            add_join_on($3,$6);
            Lex->pop_context();
          }
        | table_ref STRAIGHT_JOIN table_factor
          ON
          {
            YYERROR_UNLESS($1 && ($$=$3));
            /* Change the current name resolution context to a local context. */
6840
            if (push_new_name_resolution_context(YYTHD, $1, $3))
6841 6842 6843 6844 6845 6846 6847 6848
              YYABORT;
          }
          expr
          {
            $3->straight=1;
            add_join_on($3,$6);
            Lex->pop_context();
          }
6849
	| table_ref normal_join table_ref
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6850
	  USING
6851
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6852
	    SELECT_LEX *sel= Select;
serg@serg.mylan's avatar
serg@serg.mylan committed
6853
            YYERROR_UNLESS($1 && $3);
6854
	  }
6855
	  '(' using_list ')'
6856
          { add_join_natural($1,$3,$7); $$=$3; }
6857 6858 6859 6860 6861 6862 6863
	| table_ref NATURAL JOIN_SYM table_factor
	  {
            YYERROR_UNLESS($1 && ($$=$4));
            add_join_natural($1,$4,NULL);
          }

/* LEFT JOIN variants */
6864 6865 6866
	| table_ref LEFT opt_outer JOIN_SYM table_ref
          ON
          {
6867
            YYERROR_UNLESS($1 && $5);
6868
            /* Change the current name resolution context to a local context. */
6869
            if (push_new_name_resolution_context(YYTHD, $1, $5))
6870 6871 6872 6873 6874 6875 6876 6877 6878
              YYABORT;
          }
          expr
	  {
            add_join_on($5,$8);
            Lex->pop_context();
            $5->outer_join|=JOIN_TYPE_LEFT;
            $$=$5;
          }
6879
	| table_ref LEFT opt_outer JOIN_SYM table_factor
6880
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6881
	    SELECT_LEX *sel= Select;
serg@serg.mylan's avatar
serg@serg.mylan committed
6882
            YYERROR_UNLESS($1 && $5);
6883
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6884
	  USING '(' using_list ')'
6885
          { add_join_natural($1,$5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
6886
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
6887
	  {
serg@serg.mylan's avatar
serg@serg.mylan committed
6888
            YYERROR_UNLESS($1 && $6);
6889
 	    add_join_natural($1,$6,NULL);
6890
	    $6->outer_join|=JOIN_TYPE_LEFT;
6891 6892
	    $$=$6;
	  }
6893 6894

/* RIGHT JOIN variants */
6895 6896 6897
	| table_ref RIGHT opt_outer JOIN_SYM table_ref
          ON
          {
6898
            YYERROR_UNLESS($1 && $5);
6899
            /* Change the current name resolution context to a local context. */
6900
            if (push_new_name_resolution_context(YYTHD, $1, $5))
6901 6902 6903
              YYABORT;
          }
          expr
serg@serg.mylan's avatar
serg@serg.mylan committed
6904
          {
6905 6906 6907
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
6908 6909
            add_join_on($$, $8);
            Lex->pop_context();
6910 6911
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
6912
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6913
	    SELECT_LEX *sel= Select;
serg@serg.mylan's avatar
serg@serg.mylan committed
6914
            YYERROR_UNLESS($1 && $5);
6915
	  }
6916
	  USING '(' using_list ')'
serg@serg.mylan's avatar
serg@serg.mylan committed
6917
          {
6918 6919 6920
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
6921
            add_join_natural($$,$5,$9);
6922 6923
          }
	| table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor
6924
	  {
serg@serg.mylan's avatar
serg@serg.mylan committed
6925
            YYERROR_UNLESS($1 && $6);
6926
	    add_join_natural($6,$1,NULL);
6927 6928 6929
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
6930
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6931 6932

normal_join:
6933 6934 6935 6936
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6937

6938
/* Warning - may return NULL in case of incomplete SELECT */
6939
table_factor:
6940
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6941
	  SELECT_LEX *sel= Select;
6942
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6943
	  sel->table_join_options= 0;
6944
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6945
        table_ident opt_table_alias opt_key_definition
6946
	{
6947
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6948
	  SELECT_LEX *sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6949
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6950
					   sel->get_table_join_options(),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6951
					   lex->lock_option,
6952 6953
					   sel->get_use_index(),
					   sel->get_ignore_index())))
6954
	    YYABORT;
6955
          sel->add_joined_table($$);
6956
	}
6957 6958 6959 6960
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref
          ON
          {
            /* Change the current name resolution context to a local context. */
6961
            if (push_new_name_resolution_context(YYTHD, $3, $7))
6962
              YYABORT;
6963

6964 6965 6966 6967 6968 6969 6970 6971 6972
          }
          expr '}'
	  {
            YYERROR_UNLESS($3 && $7);
            add_join_on($7,$10);
            Lex->pop_context();
            $7->outer_join|=JOIN_TYPE_LEFT;
            $$=$7;
          }
6973
	| select_derived_init get_select_lex select_derived2
6974
          {
6975
            LEX *lex= Lex;
6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989
            SELECT_LEX *sel= lex->current_select;
            if ($1)
            {
	      if (sel->set_braces(1))
	      {
	        yyerror(ER(ER_SYNTAX_ERROR));
	        YYABORT;
	      }
              /* select in braces, can't contain global parameters */
	      if (sel->master_unit()->fake_select_lex)
                sel->master_unit()->global_parameters=
                   sel->master_unit()->fake_select_lex;
            }
            if ($2->init_nested_join(lex->thd))
serg@serg.mylan's avatar
serg@serg.mylan committed
6990
              YYABORT;
6991
            $$= 0;
serg@serg.mylan's avatar
serg@serg.mylan committed
6992
            /* incomplete derived tables return NULL, we must be
6993
               nested in select_derived rule to be here. */
6994
          }
6995 6996 6997 6998 6999
	| '(' get_select_lex select_derived union_opt ')' opt_table_alias
	{
          /* Use $2 instead of Lex->current_select as derived table will
             alter value of Lex->current_select. */

serg@serg.mylan's avatar
serg@serg.mylan committed
7000
          if (!($3 || $6) && $2->embedding &&
7001
              !$2->embedding->nested_join->join_list.elements)
7002
          {
7003 7004 7005 7006 7007
            /* we have a derived table ($3 == NULL) but no alias,
               Since we are nested in further parentheses so we
               can pass NULL to the outer level parentheses
               Permits parsing of "((((select ...))) as xyz)" */
            $$= 0;
7008
          }
7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022
          else
          if (!$3)
          {
            /* Handle case of derived table, alias may be NULL if there
               are no outer parentheses, add_table_to_list() will throw
               error in this case */
	    LEX *lex=Lex;
            SELECT_LEX *sel= lex->current_select;
	    SELECT_LEX_UNIT *unit= sel->master_unit();
	    lex->current_select= sel= unit->outer_select();
	    if (!($$= sel->
                  add_table_to_list(lex->thd, new Table_ident(unit), $6, 0,
				    TL_READ,(List<String> *)0,
	                            (List<String> *)0)))
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7023

7024
	      YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
7025
            sel->add_joined_table($$);
7026
            lex->pop_context();
7027 7028 7029 7030 7031 7032
          }
	  else
          if ($4 || $6)
	  {
            /* simple nested joins cannot have aliases or unions */
            yyerror(ER(ER_SYNTAX_ERROR));
7033
	    YYABORT;
7034 7035 7036 7037 7038
	  }
          else
            $$= $3;
	}
        ;
7039

7040
/* handle contents of parentheses in join expression */
7041
select_derived:
7042
	  get_select_lex
7043
	  {
7044 7045 7046 7047 7048 7049 7050 7051 7052
            LEX *lex= Lex;
            if ($1->init_nested_join(lex->thd))
              YYABORT;
          }
          derived_table_list
          {
            LEX *lex= Lex;
            /* for normal joins, $3 != NULL and end_nested_join() != NULL,
               for derived tables, both must equal NULL */
serg@serg.mylan's avatar
serg@serg.mylan committed
7053

7054 7055 7056 7057
            if (!($$= $1->end_nested_join(lex->thd)) && $3)
              YYABORT;
            if (!$3 && $$)
            {
7058 7059
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
7060 7061
            }
          }
monty@mysql.com's avatar
monty@mysql.com committed
7062
 	;
7063

monty@mysql.com's avatar
monty@mysql.com committed
7064
select_derived2:
7065
        {
7066
	  LEX *lex= Lex;
7067
	  lex->derived_tables|= DERIVED_SUBQUERY;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7068 7069 7070
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
7071 7072
	  {
	    yyerror(ER(ER_SYNTAX_ERROR));
7073 7074
	    YYABORT;
	  }
7075
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
7076
              mysql_new_select(lex, 1))
7077 7078
	    YYABORT;
	  mysql_init_select(lex);
7079
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
7080
	  lex->current_select->parsing_place= SELECT_LIST;
7081 7082 7083
	}
        select_options select_item_list
	{
7084
	  Select->parsing_place= NO_MATTER;
7085
	}
7086
	opt_select_from
7087
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7088

7089 7090 7091 7092 7093 7094 7095 7096
get_select_lex:
	/* Empty */ { $$= Select; }
        ;

select_derived_init:
          SELECT_SYM
          {
            LEX *lex= Lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
7097
            SELECT_LEX *sel= lex->current_select;
7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111
            TABLE_LIST *embedding;
            if (!sel->embedding || sel->end_nested_join(lex->thd))
	    {
              /* we are not in parentheses */
              yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
	    }
            embedding= Select->embedding;
            $$= embedding &&
                !embedding->nested_join->join_list.elements;
            /* return true if we are deeply nested */
          }
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7112 7113
opt_outer:
	/* empty */	{}
7114
	| OUTER		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7115 7116 7117 7118

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
7119
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7120
	    SELECT_LEX *sel= Select;
7121 7122 7123
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7124 7125
	| FORCE_SYM key_usage_list
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7126
	    SELECT_LEX *sel= Select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7127 7128 7129 7130
	    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
7131
	| IGNORE_SYM key_usage_list
7132
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7133
	    SELECT_LEX *sel= Select;
7134 7135
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
7136
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7137 7138

key_usage_list:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7139
	key_or_index { Select->interval_list.empty(); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7140
        '(' key_list_or_empty ')'
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7141
        { $$= &Select->interval_list; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7142 7143 7144 7145 7146 7147
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7148 7149 7150

key_usage_list2:
	key_usage_list2 ',' ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7151
        { Select->
7152
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $3.str, $3.length,
7153
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7154
	| ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7155
        { Select->
7156
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $1.str, $1.length,
7157
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7158
	| PRIMARY_SYM
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7159
        { Select->
7160
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
7161
				    system_charset_info)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7162 7163 7164

using_list:
	ident
7165
	  {
7166
            if (!($$= new List<String>))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7167
	      YYABORT;
7168 7169 7170
            $$->push_back(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
7171 7172 7173
	  }
	| using_list ',' ident
	  {
7174 7175 7176 7177
            $1->push_back(new (YYTHD->mem_root)
                              String((const char *) $3.str, $3.length,
                                      system_charset_info));
            $$= $1;
7178
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7179 7180

interval:
7181 7182
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
7183
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7184 7185
	| 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
7186
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7187 7188
	| 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
7189 7190
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7191
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
7192 7193 7194 7195 7196 7197 7198 7199
	| 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
7200 7201
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
7202
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7203
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
7204 7205
	| YEAR_SYM		{ $$=INTERVAL_YEAR; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7206

7207
date_time_type:
7208 7209 7210 7211 7212
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
7213

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7214 7215 7216
table_alias:
	/* empty */
	| AS
7217
	| EQ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7218 7219 7220 7221

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

7224 7225 7226 7227
opt_all:
	/* empty */
	| ALL
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7228 7229

where_clause:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7230
	/* empty */  { Select->where= 0; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7231 7232 7233 7234 7235
	| WHERE
          {
            Select->parsing_place= IN_WHERE;
          }
          expr
7236
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7237 7238 7239 7240 7241
            SELECT_LEX *select= Select;
	    select->where= $3;
            select->parsing_place= NO_MATTER;
	    if ($3)
	      $3->top_level_item();
7242
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7243
 	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7244 7245 7246

having_clause:
	/* empty */
7247 7248
	| HAVING
	  {
7249
	    Select->parsing_place= IN_HAVING;
7250 7251
          }
	  expr
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7252
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7253
	    SELECT_LEX *sel= Select;
7254
	    sel->having= $3;
7255
	    sel->parsing_place= NO_MATTER;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7256 7257 7258
	    if ($3)
	      $3->top_level_item();
	  }
7259
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7260 7261

opt_escape:
7262 7263 7264 7265 7266
	ESCAPE_SYM simple_expr 
          {
            Lex->escape_used= TRUE;
            $$= $2;
          }
7267
	| /* empty */
7268
          {
7269
            Lex->escape_used= FALSE;
7270 7271 7272
            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
7273 7274
          }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7275 7276 7277


/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7278
   group by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7279 7280 7281 7282
*/

group_clause:
	/* empty */
7283
	| GROUP BY group_list olap_opt;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7284 7285

group_list:
7286
	group_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7287
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
7288
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7289
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7290

7291 7292
olap_opt:
	/* empty */ {}
7293
	| WITH CUBE_SYM
7294
          {
7295
	    LEX *lex=Lex;
7296 7297
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
7298
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
7299 7300 7301
		       "global union parameters");
	      YYABORT;
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7302
	    lex->current_select->olap= CUBE_TYPE;
7303
	    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
7304
	    YYABORT;	/* To be deleted in 5.1 */
7305
	  }
7306
	| WITH ROLLUP_SYM
7307
          {
7308 7309 7310
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
7311
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
7312 7313 7314
		       "global union parameters");
	      YYABORT;
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7315
	    lex->current_select->olap= ROLLUP_TYPE;
7316
	  }
7317
	;
7318

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7319
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7320
   Order by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7321 7322
*/

7323
opt_order_clause:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7324
	/* empty */
7325
	| order_clause;
7326 7327

order_clause:
7328 7329
	ORDER_SYM BY
        {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7330
	  LEX *lex=Lex;
7331
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7332
	      lex->current_select->olap !=
7333
	      UNSPECIFIED_OLAP_TYPE)
7334
	  {
7335 7336
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
7337 7338
	    YYABORT;
	  }
7339
	} order_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7340 7341 7342

order_list:
	order_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7343
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7344
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7345
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7346 7347 7348

order_dir:
	/* empty */ { $$ =  1; }
7349
	| ASC  { $$ =1; }
7350
	| DESC { $$ =0; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7351 7352


7353 7354 7355
opt_limit_clause_init:
	/* empty */
	{
7356 7357
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
7358 7359
          sel->offset_limit= 0;
          sel->select_limit= 0;
7360
	}
7361 7362 7363
	| limit_clause {}
	;

7364 7365 7366 7367 7368
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

7369
limit_clause:
7370
	LIMIT limit_options {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7371
	;
7372 7373

limit_options:
7374
	limit_option
7375
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7376
            SELECT_LEX *sel= Select;
7377
            sel->select_limit= $1;
7378
            sel->offset_limit= 0;
7379
	    sel->explicit_limit= 1;
7380
	  }
7381
	| limit_option ',' limit_option
7382
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7383
	    SELECT_LEX *sel= Select;
7384 7385
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
7386
	    sel->explicit_limit= 1;
7387
	  }
7388
	| limit_option OFFSET_SYM limit_option
7389
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7390
	    SELECT_LEX *sel= Select;
7391 7392
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
7393
	    sel->explicit_limit= 1;
7394 7395
	  }
	;
7396 7397 7398 7399 7400
limit_option:
        param_marker
        | ULONGLONG_NUM { $$= new Item_uint($1.str, $1.length); }
        | LONG_NUM     { $$= new Item_uint($1.str, $1.length); }
        | NUM           { $$= new Item_uint($1.str, $1.length); }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
7401
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7402 7403 7404 7405

delete_limit_clause:
	/* empty */
	{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7406
	  LEX *lex=Lex;
7407
	  lex->current_select->select_limit= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7408
	}
7409
	| LIMIT limit_option
7410 7411
	{
	  SELECT_LEX *sel= Select;
7412
	  sel->select_limit= $2;
7413 7414
	  sel->explicit_limit= 1;
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7415

serg@serg.mylan's avatar
serg@serg.mylan committed
7416 7417
ulong_num:
          NUM           { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
7418
	| HEX_NUM       { $$= (ulong) strtol($1.str, (char**) 0, 16); }
monty@mysql.com's avatar
monty@mysql.com committed
7419 7420
	| LONG_NUM      { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| ULONGLONG_NUM { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
serg@serg.mylan's avatar
serg@serg.mylan committed
7421
        | DECIMAL_NUM   { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
monty@mysql.com's avatar
monty@mysql.com committed
7422 7423
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7424

7425
ulonglong_num:
monty@mysql.com's avatar
monty@mysql.com committed
7426 7427 7428
	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); }
7429
        | DECIMAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
monty@mysql.com's avatar
monty@mysql.com committed
7430 7431
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7432 7433 7434 7435 7436 7437

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
7438 7439
	    if (&lex->select_lex != lex->current_select)
	    {
7440
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
7441 7442
	      YYABORT;
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7443 7444 7445
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
7446 7447 7448 7449
	    if (add_proc_to_list(lex->thd, new Item_field(&lex->
                                                          current_select->
                                                          context,
                                                          NULL,NULL,$2.str)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7450
	      YYABORT;
7451
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7452
	  }
7453
	  '(' procedure_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7454 7455 7456 7457


procedure_list:
	/* empty */ {}
7458
	| procedure_list2 {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7459 7460 7461

procedure_list2:
	procedure_list2 ',' procedure_item
7462
	| procedure_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7463 7464 7465 7466

procedure_item:
	  remember_name expr
	  {
7467 7468
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7469 7470
	      YYABORT;
	    if (!$2->name)
7471 7472
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1),
                           YYTHD->charset());
7473 7474 7475 7476 7477 7478
	  }
          ;


select_var_list_init:
	   {
7479 7480
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
7481 7482
	        YYABORT;
	   }
7483
	   select_var_list
7484
	   {}
7485
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7486

Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7487
select_var_list:
7488 7489 7490 7491
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

7492 7493 7494 7495 7496 7497 7498 7499 7500 7501
select_var_ident:  
	   '@' ident_or_text
           {
             LEX *lex=Lex;
	     if (lex->result) 
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($2,0,0,(enum_field_types)0));
	     else
	       YYABORT;
	   }
           | ident_or_text
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7502
           {
7503
             LEX *lex=Lex;
7504 7505 7506 7507
	     sp_pvar_t *t;

	     if (!lex->spcont || !(t=lex->spcont->find_pvar(&$1)))
	     {
7508
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7509
	       YYABORT;
7510 7511 7512 7513 7514
	     }
	     if (! lex->result)
	       YYABORT;
	     else
	     {
7515 7516 7517
               my_var *var;
	       ((select_dumpvar *)lex->result)->
                 var_list.push_back(var= new my_var($1,1,t->offset,t->type));
7518
#ifndef DBUG_OFF
7519
	       if (var)
7520
		 var->sp= lex->sphead;
7521
#endif
7522
	     }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7523
	   }
7524
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7525

7526
into:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7527
        INTO OUTFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7528
	{
7529
          LEX *lex= Lex;
7530 7531 7532 7533
          lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
          if (!(lex->exchange= new sql_exchange($3.str, 0)) ||
              !(lex->result= new select_export(lex->exchange)))
            YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7534 7535
	}
	opt_field_term opt_line_term
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7536
	| INTO DUMPFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7537
	{
7538
	  LEX *lex=Lex;
7539 7540
	  if (!lex->describe)
	  {
7541
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
7542 7543 7544 7545 7546
	    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
7547
	}
7548
        | INTO select_var_list_init
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7549
	{
7550
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
7551 7552
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7553

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7554 7555 7556
/*
  DO statement
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7557

7558
do:	DO_SYM
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7559 7560 7561
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
7562 7563 7564 7565 7566
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7567
	}
7568 7569
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7570
/*
7571
  Drop : delete tables or index or user
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7572 7573 7574
*/

drop:
serg@serg.mylan's avatar
serg@serg.mylan committed
7575
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7576
	{
7577 7578
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
7579 7580
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7581
	}
7582
	| DROP INDEX_SYM ident ON table_ident {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7583
	  {
7584 7585
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
7586 7587 7588
	     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
7589 7590
	     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
7591 7592 7593 7594
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
7595 7596 7597 7598
	    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
7599
	 }
7600
	| DROP FUNCTION_SYM if_exists sp_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7601
	  {
7602
	    LEX *lex=Lex;
7603 7604
	    if (lex->sphead)
	    {
7605
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
7606 7607
	      YYABORT;
	    }
7608
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
7609 7610 7611 7612 7613 7614 7615 7616
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
	  }
	| DROP PROCEDURE if_exists sp_name
	  {
	    LEX *lex=Lex;
	    if (lex->sphead)
	    {
7617
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
7618 7619 7620 7621 7622
	      YYABORT;
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
7623
	  }
7624
	| DROP USER clear_privileges user_list
7625
	  {
7626 7627
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
7628 7629
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
7630
	    LEX *lex= Lex;
7631 7632 7633
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
7634 7635 7636 7637 7638 7639 7640
        | DROP EVENT_SYM if_exists sp_name
          {
            LEX *lex=Lex;

            if (lex->et)
            {
              // ToDo Andrey : Change the error message
andrey@lmy004's avatar
andrey@lmy004 committed
7641 7642 7643 7644
              /*
                Recursive events are not possible because recursive SPs
                are not also possible. lex->sp_head is not stacked.
              */
7645 7646 7647 7648
              my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "EVENT");
              YYABORT;
            }

andrey@lmy004's avatar
andrey@lmy004 committed
7649 7650 7651
            if (!(lex->et= new event_timed()))
              YYABORT;
            lex->et->init_name(YYTHD, $4);
7652 7653 7654 7655
	  
            lex->sql_command = SQLCOM_DROP_EVENT;
            lex->drop_if_exists= $3;
          }
7656
        | DROP TRIGGER_SYM sp_name
7657 7658 7659
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_DROP_TRIGGER;
7660
            lex->spname= $3;
7661 7662 7663 7664 7665 7666 7667 7668 7669 7670
	  }
        | DROP TABLESPACE tablespace_name opt_ts_engine opt_ts_wait
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= DROP_TABLESPACE;
          }
        | DROP LOGFILE_SYM GROUP logfile_group_name opt_ts_engine opt_ts_wait
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= DROP_LOGFILE_GROUP;
7671 7672
          }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7673 7674

table_list:
7675
	table_name
7676
	| table_list ',' table_name;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7677

7678
table_name:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7679
	table_ident
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7680 7681 7682 7683 7684
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7685 7686

if_exists:
7687
	/* empty */ { $$= 0; }
7688 7689
	| IF EXISTS { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7690

7691 7692 7693 7694
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7695 7696 7697 7698 7699
/*
** Insert : add new data to table
*/

insert:
7700 7701 7702
	INSERT
	{
	  LEX *lex= Lex;
7703 7704
	  lex->sql_command= SQLCOM_INSERT;
	  lex->duplicates= DUP_ERROR; 
7705
	  mysql_init_select(lex);
7706 7707 7708
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
	} insert_lock_option
7709
	opt_ignore insert2
7710
	{
7711
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7712
	  Lex->current_select= &Lex->select_lex;
7713
	}
7714
	insert_field_spec opt_insert_update
7715
	{}
7716
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7717 7718

replace:
7719 7720
	REPLACE
	{
7721
	  LEX *lex=Lex;
7722 7723
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
7724
	  mysql_init_select(lex);
7725
	}
7726 7727
	replace_lock_option insert2
	{
7728
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7729
	  Lex->current_select= &Lex->select_lex;
7730 7731
	}
	insert_field_spec
7732
	{}
7733
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7734 7735

insert_lock_option:
7736 7737 7738 7739
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
7740
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7741 7742

replace_lock_option:
7743 7744
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7745 7746 7747

insert2:
	INTO insert_table {}
7748
	| insert_table {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7749 7750

insert_table:
7751
	table_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7752
	{
7753 7754 7755 7756
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
7757
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7758 7759

insert_field_spec:
serg@serg.mylan's avatar
serg@serg.mylan committed
7760 7761 7762
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7763 7764
	| SET
	  {
7765 7766 7767
	    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
7768 7769
	      YYABORT;
	   }
7770
	   ident_eq_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7771 7772 7773

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
7774
	| insert_ident		{ Lex->field_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7775 7776 7777

insert_values:
	VALUES	values_list  {}
7778
	| VALUE_SYM values_list  {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7779 7780
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
7781
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7782 7783 7784

values_list:
	values_list ','  no_braces
7785
	| no_braces;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7786 7787 7788 7789

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
7790
	ident_eq_value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7791 7792

ident_eq_value:
7793
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7794
	 {
7795 7796 7797
	  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
7798
	    YYABORT;
7799
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7800 7801

equal:	EQ		{}
7802 7803 7804 7805 7806 7807 7808
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7809 7810 7811 7812 7813 7814 7815 7816 7817

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
7818 7819
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7820
	    YYABORT;
7821
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7822 7823 7824

opt_values:
	/* empty */ {}
7825
	| values;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7826 7827

values:
7828
	values ','  expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7829 7830 7831 7832
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
7833 7834 7835 7836 7837 7838 7839 7840
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
7841
	expr	  { $$= $1;}
7842
	| DEFAULT {$$= new Item_default_value(Lex->current_context()); }
7843
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7844

7845 7846
opt_insert_update:
        /* empty */
monty@mysql.com's avatar
monty@mysql.com committed
7847
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
7848
          KEY_SYM UPDATE_SYM insert_update_list
7849 7850
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7851 7852 7853
/* Update rows in a table */

update:
7854 7855
	UPDATE_SYM
	{
7856
	  LEX *lex= Lex;
7857
	  mysql_init_select(lex);
7858
          lex->sql_command= SQLCOM_UPDATE;
7859
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
7860
	  lex->duplicates= DUP_ERROR; 
7861
        }
7862
        opt_low_priority opt_ignore join_table_list
7863
	SET update_list
7864
	{
7865 7866
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
7867
            lex->sql_command= SQLCOM_UPDATE_MULTI;
7868 7869 7870
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
7871 7872
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
7873 7874
	    YYABORT;
	  }
7875 7876 7877 7878 7879 7880
          /*
            In case of multi-update setting write lock for all tables may
            be too pessimistic. We will decrease lock level if possible in
            mysql_multi_update().
          */
          Select->set_lock_for_tables($3);
7881
	}
7882
	where_clause opt_order_clause delete_limit_clause {}
7883
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7884 7885

update_list:
7886 7887 7888 7889
	update_list ',' update_elem
	| update_elem;

update_elem:
monty@mishka.local's avatar
monty@mishka.local committed
7890
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7891
	{
monty@mysql.com's avatar
monty@mysql.com committed
7892
	  if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7893
	    YYABORT;
7894 7895 7896 7897 7898 7899 7900
	};

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
monty@mishka.local's avatar
monty@mishka.local committed
7901
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7902
	  {
7903 7904 7905
	  LEX *lex= Lex;
	  if (lex->update_list.push_back($1) || 
	      lex->value_list.push_back($3))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7906
	      YYABORT;
7907
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7908 7909

opt_low_priority:
7910
	/* empty */	{ $$= YYTHD->update_lock_default; }
7911
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7912 7913 7914 7915

/* Delete rows from a table */

delete:
7916
	DELETE_SYM
7917
	{
7918 7919
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
7920
	  mysql_init_select(lex);
7921
	  lex->lock_option= lex->thd->update_lock_default;
7922
	  lex->ignore= 0;
7923
	  lex->select_lex.init_order();
7924
	}
7925 7926
	opt_delete_options single_multi {}
	;
7927 7928

single_multi:
7929 7930
 	FROM table_ident
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7931 7932
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
7933 7934 7935
	    YYABORT;
	}
	where_clause opt_order_clause
7936
	delete_limit_clause {}
7937
	| table_wild_list
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7938
	  { mysql_init_multi_delete(Lex); }
7939
          FROM join_table_list where_clause
7940 7941 7942 7943
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7944 7945
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
7946
	  USING join_table_list where_clause
7947 7948 7949 7950
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
7951
	;
7952 7953 7954

table_wild_list:
	  table_wild_one {}
7955
	  | table_wild_list ',' table_wild_one {};
7956 7957

table_wild_one:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7958
	ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7959
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7960 7961
	  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
7962 7963
	    YYABORT;
        }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7964
	| ident '.' ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7965
	  {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7966 7967
	    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
7968 7969
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
7970
	      YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7971
	  }
7972
	;
7973 7974

opt_wild:
7975
	/* empty */	{}
7976
	| '.' '*'	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7977 7978


7979
opt_delete_options:
7980
	/* empty */	{}
7981
	| opt_delete_option opt_delete_options {};
7982 7983

opt_delete_option:
7984
	QUICK		{ Select->options|= OPTION_QUICK; }
7985
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
7986
	| IGNORE_SYM	{ Lex->ignore= 1; };
7987

7988
truncate:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7989
	TRUNCATE_SYM opt_table_sym table_name
7990
	{
7991
	  LEX* lex= Lex;
7992
	  lex->sql_command= SQLCOM_TRUNCATE;
7993
	  lex->select_lex.options= 0;
7994
	  lex->select_lex.init_order();
7995 7996
	}
	;
7997

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7998 7999
opt_table_sym:
	/* empty */
8000
	| TABLE_SYM;
8001

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8002 8003
/* Show things */

8004 8005
show:	SHOW
	{
8006 8007
	  LEX *lex=Lex;
	  lex->wild=0;
8008 8009 8010
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
8011 8012
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8013
	show_param
8014 8015
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8016 8017

show_param:
8018
         DATABASES wild_and_where
8019 8020 8021 8022 8023 8024 8025
         {
           LEX *lex= Lex;
           lex->sql_command= SQLCOM_SELECT;
           lex->orig_sql_command= SQLCOM_SHOW_DATABASES;
           if (prepare_schema_table(YYTHD, lex, 0, SCH_SCHEMATA))
             YYABORT;
         }
8026
         | opt_full TABLES opt_db wild_and_where
8027 8028 8029 8030
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLES;
8031
             lex->select_lex.db= $3;
8032 8033 8034
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLE_NAMES))
               YYABORT;
           }
8035 8036 8037 8038 8039 8040 8041 8042 8043
         | opt_full TRIGGERS_SYM opt_db wild_and_where
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TRIGGERS;
             lex->select_lex.db= $3;
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TRIGGERS))
               YYABORT;
           }
8044
         | TABLE_SYM STATUS_SYM opt_db wild_and_where
8045 8046 8047 8048
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLE_STATUS;
8049
             lex->select_lex.db= $3;
8050 8051 8052
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLES))
               YYABORT;
           }
8053
        | OPEN_SYM TABLES opt_db wild_and_where
8054
	  {
8055
	    LEX *lex= Lex;
8056 8057
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_OPEN_TABLES;
8058
	    lex->select_lex.db= $3;
8059 8060
            if (prepare_schema_table(YYTHD, lex, 0, SCH_OPEN_TABLES))
              YYABORT;
8061
	  }
8062 8063 8064 8065 8066 8067 8068 8069
        | PLUGIN_SYM
	  {
	    LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_PLUGINS;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PLUGINS))
              YYABORT;
	  }
8070
	| ENGINE_SYM storage_engines 
8071 8072
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
8073
	| ENGINE_SYM ALL 
8074
	  { Lex->create_info.db_type= NULL; }
8075
	  show_engine_param
8076
	| opt_full COLUMNS from_or_in table_ident opt_db wild_and_where
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8077
	  {
8078 8079 8080
 	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SELECT;
	    lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
8081 8082 8083
	    if ($5)
	      $4->change_db($5);
	    if (prepare_schema_table(YYTHD, lex, $4, SCH_COLUMNS))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8084 8085
	      YYABORT;
	  }
8086
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
8087 8088
	  TEXT_STRING_sys AND_SYM MASTER_LOG_POS_SYM EQ ulonglong_num
	  AND_SYM MASTER_SERVER_ID_SYM EQ
serg@serg.mylan's avatar
serg@serg.mylan committed
8089
	ulong_num
8090 8091 8092 8093
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
8094
	    Lex->mi.server_id = $16;
8095
          }
8096
        | master_or_binary LOGS_SYM
8097 8098
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
8099 8100 8101 8102 8103
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
8104
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
8105
          {
8106 8107
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
8108
          } opt_limit_clause_init
8109
        | keys_or_index from_or_in table_ident opt_db where_clause
8110 8111 8112 8113
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_KEYS;
8114 8115 8116
	    if ($4)
	      $3->change_db($4);
            if (prepare_schema_table(YYTHD, lex, $3, SCH_STATISTICS))
8117
              YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8118
	  }
8119 8120 8121 8122 8123 8124 8125 8126
	| COLUMN_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_COLUMN_TYPES;
	  }
	| TABLE_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
8127 8128 8129 8130 8131 8132 8133
	    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;
8134 8135 8136
            lex->orig_sql_command= SQLCOM_SHOW_AUTHORS;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_ENGINES))
              YYABORT;
8137
	  }
brian@grrr.local's avatar
brian@grrr.local committed
8138 8139 8140 8141 8142
	| AUTHORS_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_AUTHORS;
	  }
8143 8144 8145 8146 8147
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
8148
        | COUNT_SYM '(' '*' ')' WARNINGS
8149
          { (void) create_select_for_variable("warning_count"); }
8150
        | COUNT_SYM '(' '*' ')' ERRORS
8151
	  { (void) create_select_for_variable("error_count"); }
8152
        | WARNINGS opt_limit_clause_init
8153
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
8154
        | ERRORS opt_limit_clause_init
8155
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
8156
        | opt_var_type STATUS_SYM wild_and_where
8157
          {
8158 8159 8160
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS;
8161
            lex->option_type= $1;
8162 8163
            if (prepare_schema_table(YYTHD, lex, 0, SCH_STATUS))
              YYABORT;
8164
	  }	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
8165
        | INNOBASE_SYM STATUS_SYM
8166 8167 8168
          {
            LEX *lex= Lex;
            lex->sql_command = SQLCOM_SHOW_ENGINE_STATUS;
8169 8170 8171 8172 8173 8174
            if (!(lex->create_info.db_type=
                  ha_resolve_by_legacy_type(YYTHD, DB_TYPE_INNODB)))
            {
	      my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), "InnoDB");
	      YYABORT;
            }
8175 8176
            WARN_DEPRECATED("SHOW INNODB STATUS", "SHOW ENGINE INNODB STATUS");
	  }
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
8177
        | MUTEX_SYM STATUS_SYM
8178 8179 8180
          {
	    LEX *lex= Lex;
            lex->sql_command = SQLCOM_SHOW_ENGINE_MUTEX; 
8181 8182 8183 8184 8185 8186
            if (!(lex->create_info.db_type=
                  ha_resolve_by_legacy_type(YYTHD, DB_TYPE_INNODB)))
            {
	      my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), "InnoDB");
	      YYABORT;
            }
8187 8188
            WARN_DEPRECATED("SHOW MUTEX STATUS", "SHOW ENGINE INNODB MUTEX");
	  }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8189 8190
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
8191
        | opt_var_type  VARIABLES wild_and_where
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8192
	  {
8193 8194 8195
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_VARIABLES;
8196
            lex->option_type= $1;
8197 8198
            if (prepare_schema_table(YYTHD, lex, 0, SCH_VARIABLES))
              YYABORT;
8199
	  }
8200
        | charset wild_and_where
8201 8202 8203 8204 8205 8206 8207
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_CHARSETS;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_CHARSETS))
              YYABORT;
          }
8208
        | COLLATION_SYM wild_and_where
8209 8210 8211 8212 8213 8214 8215
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_COLLATIONS;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_COLLATIONS))
              YYABORT;
          }
8216
	| BERKELEY_DB_SYM LOGS_SYM
8217 8218 8219
	  {
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_ENGINE_LOGS;
8220 8221 8222 8223 8224 8225
            if (!(lex->create_info.db_type=
                  ha_resolve_by_legacy_type(YYTHD, DB_TYPE_BERKELEY_DB)))
            {
	      my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), "BerkeleyDB");
	      YYABORT;
            }
8226 8227
	    WARN_DEPRECATED("SHOW BDB LOGS", "SHOW ENGINE BDB LOGS");
	  }
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
8228
	| LOGS_SYM
8229 8230 8231
	  {
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_ENGINE_LOGS;
8232 8233 8234 8235 8236 8237
            if (!(lex->create_info.db_type=
                  ha_resolve_by_legacy_type(YYTHD, DB_TYPE_BERKELEY_DB)))
            {
	      my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), "BerkeleyDB");
	      YYABORT;
            }
8238 8239
	    WARN_DEPRECATED("SHOW LOGS", "SHOW ENGINE BDB LOGS");
	  }
8240 8241 8242 8243 8244
	| GRANTS
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    THD *thd= lex->thd;
8245
            Security_context *sctx= thd->security_ctx;
8246 8247 8248
	    LEX_USER *curr_user;
            if (!(curr_user= (LEX_USER*) thd->alloc(sizeof(st_lex_user))))
              YYABORT;
8249 8250 8251
            curr_user->user.str= sctx->priv_user;
            curr_user->user.length= strlen(sctx->priv_user);
            if (*sctx->priv_host != 0)
8252
            {
8253 8254
              curr_user->host.str= sctx->priv_host;
              curr_user->host.length= strlen(sctx->priv_host);
8255 8256 8257 8258 8259 8260
            }
            else
            {
              curr_user->host.str= (char *) "%";
              curr_user->host.length= 1;
            }
8261
            curr_user->password=null_lex_str;
8262 8263
	    lex->grant_user= curr_user;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8264
	| GRANTS FOR_SYM user
8265 8266 8267 8268
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
8269
	    lex->grant_user->password=null_lex_str;
8270
	  }
8271
	| CREATE DATABASE opt_if_not_exists ident
8272 8273
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
8274 8275
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
8276
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8277 8278
        | CREATE TABLE_SYM table_ident
          {
8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289
            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
8290
	      YYABORT;
8291
            lex->only_view= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8292 8293 8294 8295
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8296
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8297 8298 8299
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314
          }
	| CREATE PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

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

	    lex->sql_command = SQLCOM_SHOW_CREATE_FUNC;
	    lex->spname= $3;
	  }
8315
	| PROCEDURE STATUS_SYM wild_and_where
8316
	  {
8317 8318 8319
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_PROC;
8320 8321
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
8322 8323
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
8324
	  }
8325
	| FUNCTION_SYM STATUS_SYM wild_and_where
8326
	  {
8327 8328 8329
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_FUNC;
8330 8331
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
8332 8333
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
pem@mysql.com's avatar
pem@mysql.com committed
8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354
	  }
        | PROCEDURE CODE_SYM sp_name
          {
#ifdef DBUG_OFF
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
#else
            Lex->sql_command= SQLCOM_SHOW_PROC_CODE;
	    Lex->spname= $3;
#endif
          }
        | FUNCTION_SYM CODE_SYM sp_name
          {
#ifdef DBUG_OFF
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
#else
            Lex->sql_command= SQLCOM_SHOW_FUNC_CODE;
	    Lex->spname= $3;
#endif
          }
8355 8356
        | CREATE EVENT_SYM sp_name
          {
andrey@lmy004's avatar
andrey@lmy004 committed
8357 8358
            Lex->sql_command = SQLCOM_SHOW_CREATE_EVENT;
            Lex->spname= $3;
8359 8360
          }
      ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8361

8362 8363
show_engine_param:
	STATUS_SYM
8364 8365 8366
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_STATUS; }
	| MUTEX_SYM
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_MUTEX; }
8367
	| LOGS_SYM
8368
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_LOGS; };
8369

8370 8371 8372 8373
master_or_binary:
	MASTER_SYM
	| BINARY;

8374 8375 8376 8377
opt_storage:
	/* empty */
	| STORAGE_SYM;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8378 8379
opt_db:
	/* empty */  { $$= 0; }
8380
	| from_or_in ident { $$= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8381

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8382 8383
opt_full:
	/* empty */ { Lex->verbose=0; }
8384
	| FULL	    { Lex->verbose=1; };
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8385

8386 8387
from_or_in:
	FROM
8388
	| IN_SYM;
8389

8390 8391
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8392
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
8393 8394 8395

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

8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410
wild_and_where:
      /* empty */
      | LIKE TEXT_STRING_sys
	{ Lex->wild=  new (YYTHD->mem_root) String($2.str, $2.length,
                                                   system_charset_info); }
      | WHERE expr
        {
          Select->where= $2;
          if ($2)
            $2->top_level_item();
        }
      ;

8411

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8412 8413 8414 8415
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
8416 8417 8418 8419 8420 8421 8422 8423 8424
          LEX *lex= Lex;
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
          lex->sql_command= SQLCOM_SELECT;
          lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
          lex->select_lex.db= 0;
          lex->verbose= 0;
          if (prepare_schema_table(YYTHD, lex, $2, SCH_COLUMNS))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8425 8426
	    YYABORT;
	}
8427
	opt_describe_column {}
8428 8429 8430
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
8431
          {
8432
	    LEX *lex=Lex;
8433
	    lex->select_lex.options|= SELECT_DESCRIBE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8434 8435
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8436 8437 8438

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

8441 8442 8443
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
8444
	| PARTITIONS_SYM { Lex->describe|= DESCRIBE_PARTITIONS; }
8445 8446
	;

8447

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8448 8449 8450
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
8451
	| ident
8452
	  { 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
8453 8454 8455 8456 8457


/* flush things */

flush:
8458
	FLUSH_SYM opt_no_write_to_binlog
8459 8460
	{
	  LEX *lex=Lex;
8461
	  if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
8462
	  {
8463 8464 8465 8466 8467 8468
            /*
              Note that both FLUSH TABLES and FLUSH PRIVILEGES will break
              execution in prelocked mode. So it is better to disable
              FLUSH in stored functions and triggers completely.
            */
            my_error(ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0), "FLUSH");
8469 8470
	    YYABORT;
	  }
8471
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8472
          lex->no_write_to_binlog= $2;
8473
	}
8474 8475 8476
	flush_options
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8477 8478 8479

flush_options:
	flush_options ',' flush_option
8480
	| flush_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8481 8482

flush_option:
8483
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8484
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8485
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8486 8487 8488 8489
	| 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
8490 8491
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
8492 8493
	| 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
8494

8495
opt_table_list:
8496 8497
	/* empty */  {;}
	| table_list {;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8498

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8499
reset:
8500 8501 8502 8503
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
8504 8505 8506 8507
	} reset_options
	{}
	;

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8508 8509
reset_options:
	reset_options ',' reset_option
8510
	| reset_option;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8511 8512

reset_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8513 8514
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
8515
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8516

8517
purge:
8518 8519 8520 8521
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
8522 8523 8524 8525 8526
	} purge_options
	{}
	;

purge_options:
8527
	master_or_binary LOGS_SYM purge_option
8528
	;
8529 8530

purge_option:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8531
        TO_SYM TEXT_STRING_sys
8532 8533 8534 8535 8536 8537
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
8538 8539 8540 8541
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
8542
	}
8543
	;
8544

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8545 8546 8547
/* kill threads */

kill:
8548
	KILL_SYM kill_option expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8549
	{
8550
	  LEX *lex=Lex;
8551 8552 8553
	  lex->value_list.empty();
	  lex->value_list.push_front($3);
          lex->sql_command= SQLCOM_KILL;
8554
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8555

8556 8557 8558 8559 8560
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
8561 8562 8563
/* change database */

use:	USE_SYM ident
8564 8565
	{
	  LEX *lex=Lex;
8566 8567
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
8568
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8569 8570 8571

/* import, export of files */

8572 8573 8574
load:   LOAD DATA_SYM 
        {
          LEX *lex=Lex;
8575 8576 8577 8578 8579
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD DATA");
	    YYABORT;
	  }
8580 8581 8582 8583 8584 8585 8586
          lex->fname_start= lex->ptr;
        }
        load_data
        {}
        |
        LOAD TABLE_SYM table_ident FROM MASTER_SYM
        {
8587 8588 8589 8590 8591 8592 8593
	  LEX *lex=Lex;
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD TABLE");
	    YYABORT;
	  }
          lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
8594 8595 8596 8597 8598 8599
          if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
            YYABORT;
        };

load_data:
	load_data_lock opt_local INFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8600
	{
8601 8602
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
8603 8604
	  lex->lock_option= $1;
	  lex->local_file=  $2;
8605 8606
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
8607
	  if (!(lex->exchange= new sql_exchange($4.str, 0)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8608
	    YYABORT;
8609 8610 8611 8612 8613
        }
        opt_duplicate INTO
        {
	  LEX *lex=Lex;
	  lex->fname_end= lex->ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8614
	}
8615
        TABLE_SYM table_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8616
        {
8617 8618 8619 8620 8621 8622 8623
          LEX *lex=Lex;
          if (!Select->add_table_to_list(YYTHD, $10, NULL, TL_OPTION_UPDATING,
                                         lex->lock_option))
            YYABORT;
          lex->field_list.empty();
          lex->update_list.empty();
          lex->value_list.empty();
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8624
        }
8625 8626 8627
        opt_field_term opt_line_term opt_ignore_lines opt_field_or_var_spec
        opt_load_data_set_spec
        {}
8628
        |
8629
	FROM MASTER_SYM
8630 8631
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
8632
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8633 8634 8635

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

8638
load_data_lock:
8639
	/* empty */	{ $$= YYTHD->update_lock_default; }
8640 8641
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
8642 8643


bk@work.mysql.com's avatar
bk@work.mysql.com committed
8644 8645 8646
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
8647
	| IGNORE_SYM	{ Lex->ignore= 1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8648 8649 8650

opt_field_term:
	/* empty */
8651
	| COLUMNS field_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8652 8653 8654

field_term_list:
	field_term_list field_term
8655
	| field_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8656 8657

field_term:
8658 8659
	TERMINATED BY text_string 
          {
8660
            DBUG_ASSERT(Lex->exchange != 0);
8661 8662
            Lex->exchange->field_term= $3;
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8663
	| OPTIONALLY ENCLOSED BY text_string
8664
	  {
8665
            LEX *lex= Lex;
8666
            DBUG_ASSERT(lex->exchange != 0);
8667 8668
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
8669
	  }
8670
        | ENCLOSED BY text_string
8671
          {
8672
            DBUG_ASSERT(Lex->exchange != 0);
8673 8674
            Lex->exchange->enclosed= $3;
          }
8675
        | ESCAPED BY text_string
8676
          {
8677
            DBUG_ASSERT(Lex->exchange != 0);
8678 8679
            Lex->exchange->escaped= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8680 8681 8682

opt_line_term:
	/* empty */
8683
	| LINES line_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8684 8685 8686

line_term_list:
	line_term_list line_term
8687
	| line_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8688 8689

line_term:
8690
        TERMINATED BY text_string
8691
          {
8692
            DBUG_ASSERT(Lex->exchange != 0);
8693 8694
            Lex->exchange->line_term= $3;
          }
8695
        | STARTING BY text_string
8696
          {
8697
            DBUG_ASSERT(Lex->exchange != 0);
8698 8699
            Lex->exchange->line_start= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8700 8701 8702

opt_ignore_lines:
	/* empty */
8703 8704
        | IGNORE_SYM NUM LINES
          {
8705
            DBUG_ASSERT(Lex->exchange != 0);
8706 8707
            Lex->exchange->skip_lines= atol($2.str);
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8708

8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719
opt_field_or_var_spec:
	/* empty */	          { }
	| '(' fields_or_vars ')'  { }
	| '(' ')'	          { };

fields_or_vars:
        fields_or_vars ',' field_or_var
          { Lex->field_list.push_back($3); }
        | field_or_var
          { Lex->field_list.push_back($1); }
        ;
serg@serg.mylan's avatar
serg@serg.mylan committed
8720

8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731
field_or_var:
        simple_ident_nospvar {$$= $1;}
        | '@' ident_or_text
          { $$= new Item_user_var_as_out_param($2); }
        ;

opt_load_data_set_spec:
        /* empty */           { }
        | SET insert_update_list  { };


bk@work.mysql.com's avatar
bk@work.mysql.com committed
8732 8733 8734
/* Common definitions */

text_literal:
8735
	TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8736 8737
	{
	  THD *thd= YYTHD;
8738
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8739
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8740
	| NCHAR_STRING
8741
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
8742
	| UNDERSCORE_CHARSET TEXT_STRING
8743
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
8744
	| text_literal TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8745 8746
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8747 8748

text_string:
8749
	TEXT_STRING_literal
8750
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8751 8752
	| HEX_NUM
	  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8753
	    Item *tmp= new Item_hex_string($1.str, $1.length);
8754
	    /*
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8755
	      it is OK only emulate fix_fields, because we need only
8756 8757 8758
              value of constant
	    */
	    $$= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8759
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
8760
	      (String*) 0;
8761
	  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8762 8763 8764 8765 8766 8767 8768 8769 8770 8771
        | BIN_NUM
          {
	    Item *tmp= new Item_bin_string($1.str, $1.length);
	    /*
	      it is OK only emulate fix_fields, because we need only
              value of constant
	    */
	    $$= tmp ? tmp->quick_fix_field(), tmp->val_str((String*) 0) :
		      (String*) 0;
          }
8772 8773
	;

8774
param_marker:
8775
        PARAM_MARKER
8776
        {
8777 8778
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
8779 8780 8781
          Item_param *item= new Item_param((uint) (lex->tok_start -
                                                   (uchar *) thd->query));
          if (!($$= item) || lex->param_list.push_back(item))
8782
          {
8783
            my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
8784 8785
            YYABORT;
          }
8786 8787 8788
        }
	;

8789 8790 8791
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
8792 8793
	| '-' NUM_literal
	  {
8794
	    $2->max_length++;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8795
	    $$= $2->neg();
8796
	  }
8797 8798 8799
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
8800 8801
literal:
	text_literal	{ $$ =	$1; }
8802
	| NUM_literal	{ $$ = $1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8803
	| NULL_SYM	{ $$ =	new Item_null();
8804
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
8805 8806
	| FALSE_SYM	{ $$= new Item_int((char*) "FALSE",0,1); }
	| TRUE_SYM	{ $$= new Item_int((char*) "TRUE",1,1); }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8807 8808
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
8809
	| UNDERSCORE_CHARSET HEX_NUM
8810
	  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8811
	    Item *tmp= new Item_hex_string($2.str, $2.length);
8812
	    /*
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8813
	      it is OK only emulate fix_fieds, because we need only
8814 8815 8816
              value of constant
	    */
	    String *str= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8817
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
8818
	      (String*) 0;
8819 8820 8821
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
8822
	  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836
	| UNDERSCORE_CHARSET BIN_NUM
          {
	    Item *tmp= new Item_bin_string($2.str, $2.length);
	    /*
	      it is OK only emulate fix_fieds, because we need only
              value of constant
	    */
	    String *str= tmp ?
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
	      (String*) 0;
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8837 8838
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
8839
	| TIMESTAMP text_literal { $$ = $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8840

8841
NUM_literal:
8842 8843
	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); }
8844
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
8845
        | DECIMAL_NUM
8846
	{
8847
           $$= new Item_decimal($1.str, $1.length, YYTHD->charset());
8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
8861
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
8862

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8863
/**********************************************************************
serg@serg.mylan's avatar
serg@serg.mylan committed
8864
** Creating different items.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8865 8866 8867
**********************************************************************/

insert_ident:
8868
	simple_ident_nospvar { $$=$1; }
8869
	| table_wild	 { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8870 8871

table_wild:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8872
	ident '.' '*'
8873
	{
8874
          SELECT_LEX *sel= Select;
8875
	  $$ = new Item_field(Lex->current_context(), NullS, $1.str, "*");
8876
	  sel->with_wild++;
8877
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8878
	| ident '.' ident '.' '*'
8879
	{
8880
          SELECT_LEX *sel= Select;
8881
	  $$ = new Item_field(Lex->current_context(), (YYTHD->client_capabilities &
serg@serg.mylan's avatar
serg@serg.mylan committed
8882 8883
                             CLIENT_NO_SCHEMA ? NullS : $1.str),
                             $3.str,"*");
8884
	  sel->with_wild++;
8885 8886
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8887 8888

order_ident:
8889
	expr { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8890 8891

simple_ident:
8892 8893 8894 8895 8896 8897
	ident
	{
	  sp_pvar_t *spv;
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;
	  if (spc && (spv = spc->find_pvar(&$1)))
8898 8899 8900
	  {
            /* We're compiling a stored procedure and found a variable */
            Item_splocal *splocal;
8901 8902
            splocal= new Item_splocal($1, spv->offset, spv->type,
                                      lex->tok_start_prev - 
8903
                                      lex->sphead->m_tmp_query);
8904
#ifndef DBUG_OFF
8905
            if (splocal)
8906
              splocal->m_sp= lex->sphead;
8907
#endif
8908
	    $$ = (Item*) splocal;
8909 8910 8911 8912 8913 8914 8915 8916
            lex->variables_used= 1;
	    lex->safe_to_cache_query=0;
	  }
	  else
	  {
	    SELECT_LEX *sel=Select;
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
8917 8918
                 (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	         (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
8919 8920 8921 8922 8923 8924
	  }
        }
        | simple_ident_q { $$= $1; }
	;

simple_ident_nospvar:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8925
	ident
8926
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8927
	  SELECT_LEX *sel=Select;
8928
	  $$= (sel->parsing_place != IN_HAVING ||
8929
	       sel->get_in_sum_expr() > 0) ?
8930 8931
              (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	      (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
8932
	}
8933
	| simple_ident_q { $$= $1; }
serg@serg.mylan's avatar
serg@serg.mylan committed
8934
	;
8935 8936 8937

simple_ident_q:
	ident '.' ident
8938
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8939
	  THD *thd= YYTHD;
8940
	  LEX *lex= thd->lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
8941

8942 8943 8944 8945 8946 8947
          /*
            FIXME This will work ok in simple_ident_nospvar case because
            we can't meet simple_ident_nospvar in trigger now. But it
            should be changed in future.
          */
          if (lex->sphead && lex->sphead->m_type == TYPE_ENUM_TRIGGER &&
serg@serg.mylan's avatar
serg@serg.mylan committed
8948
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") ||
8949 8950
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
8951
            Item_trigger_field *trg_fld;
8952
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
serg@serg.mylan's avatar
serg@serg.mylan committed
8953

8954 8955 8956
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
8957
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
8958 8959
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
8960

8961 8962 8963
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
8964
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
8965 8966
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
8967

8968
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
8969
                                                  new_row ?
8970 8971 8972
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
                                                  $3.str)))
8973
              YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
8974

8975 8976 8977 8978 8979 8980
            /*
              Let us add this item to list of all Item_trigger_field objects
              in trigger.
            */
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
              (byte**)&trg_fld->next_trg_field);
serg@serg.mylan's avatar
serg@serg.mylan committed
8981

8982 8983 8984 8985 8986 8987 8988
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
8989 8990
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
8991 8992 8993
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
8994 8995
	        (Item*) new Item_field(Lex->current_context(), NullS, $1.str, $3.str) :
	        (Item*) new Item_ref(Lex->current_context(), NullS, $1.str, $3.str);
8996 8997
          }
        }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8998
	| '.' ident '.' ident
8999
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9000
	  THD *thd= YYTHD;
9001
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9002
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9003 9004
	  if (sel->no_table_names_allowed)
	  {
9005 9006
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $2.str, thd->where);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9007
	  }
9008
	  $$= (sel->parsing_place != IN_HAVING ||
9009
	       sel->get_in_sum_expr() > 0) ?
9010 9011
	      (Item*) new Item_field(Lex->current_context(), NullS, $2.str, $4.str) :
              (Item*) new Item_ref(Lex->current_context(), NullS, $2.str, $4.str);
9012
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9013
	| ident '.' ident '.' ident
9014
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9015
	  THD *thd= YYTHD;
9016
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9017
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9018 9019
	  if (sel->no_table_names_allowed)
	  {
9020 9021
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $3.str, thd->where);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9022
	  }
9023
	  $$= (sel->parsing_place != IN_HAVING ||
9024
	       sel->get_in_sum_expr() > 0) ?
9025
	      (Item*) new Item_field(Lex->current_context(),
9026
                                     (YYTHD->client_capabilities &
9027 9028
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
9029
	      (Item*) new Item_ref(Lex->current_context(),
9030
                                   (YYTHD->client_capabilities &
9031
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
9032
                                   $3.str, $5.str);
9033
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9034 9035 9036 9037


field_ident:
	ident			{ $$=$1;}
9038 9039 9040 9041 9042
	| ident '.' ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->db))
            {
9043
              my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
9044 9045
              YYABORT;
            }
9046 9047
            if (my_strcasecmp(table_alias_charset, $3.str,
                              table->table_name))
9048
            {
9049
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3.str);
9050 9051 9052 9053 9054 9055 9056 9057 9058
              YYABORT;
            }
            $$=$5;
          }
	| ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->alias))
            {
9059
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $1.str);
9060 9061 9062 9063
              YYABORT;
            }
            $$=$3;
          }
9064
	| '.' ident		{ $$=$2;}	/* For Delphi */;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9065 9066 9067

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

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

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9076
IDENT_sys:
9077 9078 9079 9080 9081
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
9082 9083
            {
              CHARSET_INFO *cs= system_charset_info;
9084
              int dummy_error;
9085 9086
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
9087
                                                   $1.length, &dummy_error);
9088 9089
              if (wlen < $1.length)
              {
9090 9091
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
9092 9093
                YYABORT;
              }
9094
	      $$= $1;
9095
            }
9096 9097 9098 9099
	    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
9100 9101 9102 9103 9104 9105
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9106 9107
	  if (thd->charset_is_system_charset)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9108
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9109 9110
	    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
9111 9112 9113
	}
	;

9114
TEXT_STRING_literal:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9115 9116 9117
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9118 9119
	  if (thd->charset_is_collation_connection)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9120
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9121 9122
	    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
9123 9124 9125 9126
	}
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
9127
ident:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9128
	IDENT_sys	    { $$=$1; }
9129 9130 9131 9132 9133 9134
	| READ_ONLY_SYM
	{
	  THD *thd= YYTHD;
	  $$.str= thd->strmake("read_only",9);
	  $$.length= 9;
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9135 9136
	| keyword
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9137 9138 9139
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
9140 9141
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9142

9143 9144 9145 9146 9147 9148 9149 9150 9151 9152
label_ident:
	IDENT_sys	    { $$=$1; }
	| keyword_sp
	{
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
	}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
9153
ident_or_text:
serg@serg.mylan's avatar
serg@serg.mylan committed
9154
        ident                   { $$=$1;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9155 9156
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9157 9158 9159 9160

user:
	ident_or_text
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9161 9162
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9163
	    YYABORT;
9164 9165 9166 9167
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9168 9169
	| ident_or_text '@' ident_or_text
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9170 9171
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9172 9173
	      YYABORT;
	    $$->user = $1; $$->host=$3;
9174
	  }
9175
	| CURRENT_USER optional_braces
9176 9177
	{
          THD *thd= YYTHD;
9178
          Security_context *sctx= thd->security_ctx;
9179 9180
          if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
            YYABORT;
9181 9182 9183
          $$->user.str= sctx->priv_user;
          $$->user.length= strlen(sctx->priv_user);
          if (*sctx->priv_host != 0)
9184
          {
9185 9186
            $$->host.str= sctx->priv_host;
            $$->host.length= strlen(sctx->priv_host);
9187 9188 9189 9190 9191 9192 9193
          }
          else
          {
            $$->host.str= (char *) "%";
            $$->host.length= 1;
          }
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9194

9195
/* Keyword that we allow for identifiers (except SP labels) */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9196
keyword:
9197 9198
	keyword_sp		{}
	| ASCII_SYM		{}
brian@grrr.local's avatar
brian@grrr.local committed
9199
	| AUTHORS_SYM		{}
9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216
	| BACKUP_SYM		{}
	| BEGIN_SYM		{}
	| BYTE_SYM		{}
	| CACHE_SYM		{}
	| CHARSET		{}
	| CHECKSUM_SYM		{}
	| CLOSE_SYM		{}
	| COMMENT_SYM		{}
	| COMMIT_SYM		{}
	| CONTAINS_SYM          {}
        | DEALLOCATE_SYM        {}
	| DO_SYM		{}
	| END			{}
	| EXECUTE_SYM		{}
	| FLUSH_SYM		{}
	| HANDLER_SYM		{}
	| HELP_SYM		{}
9217
        | INSTALL_SYM           {}
9218 9219 9220
	| LANGUAGE_SYM          {}
	| NO_SYM		{}
	| OPEN_SYM		{}
9221
        | PARSER_SYM            {}
9222
	| PARTITION_SYM		{}
9223
        | PLUGIN_SYM            {}
9224 9225 9226 9227 9228 9229 9230 9231 9232
        | PREPARE_SYM           {}
	| REPAIR		{}
	| RESET_SYM		{}
	| RESTORE_SYM		{}
	| ROLLBACK_SYM		{}
	| SAVEPOINT_SYM		{}
	| SECURITY_SYM		{}
	| SIGNED_SYM		{}
	| SLAVE			{}
9233
        | SONAME_SYM            {}
9234 9235 9236 9237
	| START_SYM		{}
	| STOP_SYM		{}
	| TRUNCATE_SYM		{}
	| UNICODE_SYM		{}
9238
        | UNINSTALL_SYM         {}
9239 9240 9241 9242 9243
        | XA_SYM                {}
	;

/*
 * Keywords that we allow for labels in SPs.
pem@mysql.com's avatar
pem@mysql.com committed
9244 9245 9246
 * Anything that's the beginning of a statement or characteristics
 * must be in keyword above, otherwise we get (harmful) shift/reduce
 * conflicts.
9247 9248
 */
keyword_sp:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9249
	ACTION			{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
9250
	| ADDDATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9251
	| AFTER_SYM		{}
9252
	| AGAINST		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9253
	| AGGREGATE_SYM		{}
9254
	| ALGORITHM_SYM		{}
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9255
	| ANY_SYM		{}
andrey@lmy004's avatar
andrey@lmy004 committed
9256
	| AT_SYM                {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9257
	| AUTO_INC		{}
9258
	| AUTOEXTEND_SIZE_SYM   {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9259 9260
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
9261
	| BERKELEY_DB_SYM	{}
9262
	| BINLOG_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9263 9264
	| BIT_SYM		{}
	| BOOL_SYM		{}
9265
	| BOOLEAN_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9266
	| BTREE_SYM		{}
9267
	| CASCADED              {}
9268
	| CHAIN_SYM		{}
9269
	| CHANGED		{}
9270
	| CIPHER_SYM		{}
9271
	| CLIENT_SYM		{}
9272
	| COALESCE		{}
9273
	| CODE_SYM              {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9274
	| COLLATION_SYM		{}
9275
        | COLUMNS               {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9276
	| COMMITTED_SYM		{}
9277
	| COMPACT_SYM		{}
9278
	| COMPLETION_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9279
	| COMPRESSED_SYM	{}
9280
	| CONCURRENT		{}
9281
	| CONSISTENT_SYM	{}
9282
	| CUBE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9283
	| DATA_SYM		{}
9284
	| DATAFILE_SYM          {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9285 9286 9287
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
9288
	| DEFINER_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9289
	| DELAY_KEY_WRITE_SYM	{}
9290 9291
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
andrey@lmy004's avatar
andrey@lmy004 committed
9292
	| DISABLED_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
9293
	| DISCARD		{}
9294
	| DISK_SYM              {}
9295 9296
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9297
	| DYNAMIC_SYM		{}
9298
	| ENDS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9299
	| ENUM			{}
9300
	| ENGINE_SYM		{}
9301
	| ENGINES_SYM		{}
9302
	| ERRORS		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9303
	| ESCAPE_SYM		{}
9304
	| EVENT_SYM		{}
9305
	| EVENTS_SYM		{}
andrey@lmy004's avatar
andrey@lmy004 committed
9306 9307
	| EVERY_SYM             {}
	| EXPANSION_SYM         {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9308
	| EXTENDED_SYM		{}
9309
	| EXTENT_SIZE_SYM       {}
9310
	| FAST_SYM		{}
9311
	| FOUND_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9312 9313
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
andrey@lmy004's avatar
andrey@lmy004 committed
9314
	| ENABLED_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
9315
	| FULL			{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9316 9317 9318
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
9319
	| FRAC_SECOND_SYM	{}
9320
	| GEOMETRY_SYM		{}
9321
	| GEOMETRYCOLLECTION	{}
9322
	| GET_FORMAT		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9323
	| GRANTS		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9324
	| GLOBAL_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9325
	| HASH_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9326 9327 9328
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
9329
	| INVOKER_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
9330
	| IMPORT		{}
9331
	| INDEXES		{}
9332
	| INITIAL_SIZE_SYM      {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9333
	| ISOLATION		{}
9334
	| ISSUER_SYM		{}
9335
	| INNOBASE_SYM		{}
9336
	| INSERT_METHOD		{}
9337
	| RELAY_THREAD		{}
9338
	| LAST_SYM		{}
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
9339
	| LEAVES                {}
9340
	| LESS_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9341
	| LEVEL_SYM		{}
9342
	| LINESTRING		{}
9343
	| LIST_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9344
	| LOCAL_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9345
	| LOCKS_SYM		{}
9346
	| LOGFILE_SYM           {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9347 9348 9349 9350 9351 9352 9353 9354 9355
	| 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	{}
9356
	| MASTER_SERVER_ID_SYM  {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9357
	| MASTER_CONNECT_RETRY_SYM	{}
9358 9359 9360 9361 9362 9363
	| 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
9364 9365
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
9366
	| MAX_SIZE_SYM          {}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9367
	| MAX_UPDATES_PER_HOUR	{}
9368
	| MAX_USER_CONNECTIONS_SYM {}
9369
	| MAX_VALUE_SYM         {}
9370
	| MEDIUM_SYM		{}
9371
	| MEMORY_SYM		{}
9372
	| MERGE_SYM		{}
9373
	| MICROSECOND_SYM	{}
9374
        | MIGRATE_SYM           {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9375 9376 9377
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9378
	| MODE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9379
	| MONTH_SYM		{}
9380 9381 9382
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
9383
        | MUTEX_SYM             {}
9384
	| NAME_SYM              {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9385
	| NAMES_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9386 9387
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
9388
	| NDBCLUSTER_SYM	{}
9389
	| NEXT_SYM		{}
9390
	| NEW_SYM		{}
9391 9392
	| NO_WAIT_SYM           {}
	| NODEGROUP_SYM         {}
9393
	| NONE_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9394
	| NVARCHAR_SYM		{}
9395
	| OFFSET_SYM		{}
9396
	| OLD_PASSWORD		{}
9397
	| ONE_SHOT_SYM		{}
9398
        | ONE_SYM               {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9399
	| PACK_KEYS_SYM		{}
9400
	| PARTIAL		{}
9401
	| PARTITIONS_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9402
	| PASSWORD		{}
9403
        | PHASE_SYM             {}
9404
	| POINT_SYM		{}
9405
	| POLYGON		{}
9406
        | PRESERVE_SYM          {}
9407
	| PREV_SYM		{}
9408
        | PRIVILEGES            {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9409 9410
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
9411
	| QUARTER_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9412
	| QUERY_SYM		{}
9413
	| QUICK			{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9414
	| RAID_0_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
9415 9416
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9417
	| RAID_STRIPED_SYM	{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
9418
	| RAID_TYPE		{}
9419
        | REBUILD_SYM           {}
9420
        | RECOVER_SYM           {}
9421 9422
	| REDO_BUFFER_SIZE_SYM	{}
	| REDOFILE_SYM  	{}
serg@serg.mylan's avatar
serg@serg.mylan committed
9423
        | REDUNDANT_SYM         {}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9424 9425
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9426
	| RELOAD		{}
9427
	| REORGANIZE_SYM	{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9428
	| REPEATABLE_SYM	{}
9429
	| REPLICATION		{}
9430
	| RESOURCES		{}
9431
        | RESUME_SYM            {}
9432
	| RETURNS_SYM           {}
9433
	| ROLLUP_SYM		{}
9434
	| ROUTINE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9435 9436 9437
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9438
	| RTREE_SYM		{}
andrey@lmy004's avatar
andrey@lmy004 committed
9439
	| SCHEDULE_SYM		{}	
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9440
	| SECOND_SYM		{}
9441
	| SERIAL_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9442 9443
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
9444
	| SIMPLE_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9445
	| SHARE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9446
	| SHUTDOWN		{}
9447
	| SNAPSHOT_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9448
	| SOUNDS_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9449
	| SQL_CACHE_SYM		{}
9450
	| SQL_BUFFER_RESULT	{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9451
	| SQL_NO_CACHE_SYM	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9452
	| SQL_THREAD		{}
9453
	| STARTS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9454
	| STATUS_SYM		{}
9455
	| STORAGE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9456
	| STRING_SYM		{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
9457
	| SUBDATE_SYM		{}
9458
	| SUBJECT_SYM		{}
9459 9460
	| SUBPARTITION_SYM	{}
	| SUBPARTITIONS_SYM	{}
9461
	| SUPER_SYM		{}
9462
        | SUSPEND_SYM           {}
9463
        | TABLES                {}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
9464
	| TABLESPACE		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9465
	| TEMPORARY		{}
9466
	| TEMPTABLE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9467
	| TEXT_SYM		{}
9468
	| THAN_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9469
	| TRANSACTION_SYM	{}
9470
	| TRIGGERS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9471
	| TIMESTAMP		{}
9472 9473
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9474
	| TIME_SYM		{}
9475
	| TYPES_SYM		{}
9476
        | TYPE_SYM              {}
serg@serg.mylan's avatar
serg@serg.mylan committed
9477
        | UDF_RETURNS_SYM       {}
9478
	| FUNCTION_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9479
	| UNCOMMITTED_SYM	{}
9480
	| UNDEFINED_SYM		{}
9481 9482
	| UNDO_BUFFER_SIZE_SYM	{}
	| UNDOFILE_SYM  	{}
9483
	| UNKNOWN_SYM		{}
9484
	| UNTIL_SYM		{}
9485
	| USER			{}
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
9486
	| USE_FRM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9487
	| VARIABLES		{}
9488
	| VIEW_SYM		{}
9489
	| VALUE_SYM		{}
9490
	| WARNINGS		{}
9491
	| WAIT_SYM              {}
9492
	| WEEK_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9493
	| WORK_SYM		{}
9494
	| X509_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9495 9496
	| YEAR_SYM		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9497 9498 9499 9500 9501 9502

/* Option functions */

set:
	SET opt_option
	{
9503 9504
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
9505
	  mysql_init_select(lex);
9506
	  lex->option_type=OPT_SESSION;
9507
	  lex->var_list.empty();
9508
          lex->one_shot_set= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9509
	}
9510 9511 9512
	option_value_list
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9513 9514 9515

opt_option:
	/* empty */ {}
9516
	| OPTION {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9517 9518

option_value_list:
9519 9520 9521 9522 9523 9524 9525 9526 9527 9528
	option_type_value
	| option_value_list ',' option_type_value;

option_type_value:
        {
          if (Lex->sphead)
          {
            /*
              If we are in SP we want have own LEX for each assignment.
              This is mostly because it is hard for several sp_instr_set
serg@serg.mylan's avatar
serg@serg.mylan committed
9529 9530
              and sp_instr_set_trigger instructions share one LEX.
              (Well, it is theoretically possible but adds some extra
9531 9532 9533 9534 9535 9536 9537 9538
               overhead on preparation for execution stage and IMO less
               robust).

              QQ: May be we should simply prohibit group assignments in SP?
            */
            LEX *lex;
            Lex->sphead->reset_lex(YYTHD);
            lex= Lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
9539

9540 9541 9542 9543 9544 9545 9546 9547 9548
            /* Set new LEX as if we at start of set rule. */
	    lex->sql_command= SQLCOM_SET_OPTION;
	    mysql_init_select(lex);
	    lex->option_type=OPT_SESSION;
	    lex->var_list.empty();
            lex->one_shot_set= 0;
	    lex->sphead->m_tmp_query= lex->tok_start;
          }
        }
9549
	ext_option_value
9550 9551
        {
          LEX *lex= Lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
9552

9553 9554 9555
          if (lex->sphead)
          {
            sp_head *sp= lex->sphead;
serg@serg.mylan's avatar
serg@serg.mylan committed
9556

9557 9558 9559 9560 9561 9562 9563 9564 9565
	    if (!lex->var_list.is_empty())
	    {
              /*
                We have assignment to user or system variable or
                option setting, so we should construct sp_instr_stmt
                for it.
              */
              LEX_STRING qbuff;
	      sp_instr_stmt *i;
serg@serg.mylan's avatar
serg@serg.mylan committed
9566

9567 9568 9569
              if (!(i= new sp_instr_stmt(sp->instructions(), lex->spcont,
                                         lex)))
                YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
9570

9571 9572 9573 9574
              if (lex->ptr - lex->tok_end > 1)
                qbuff.length= lex->ptr - sp->m_tmp_query;
              else
                qbuff.length= lex->tok_end - sp->m_tmp_query;
serg@serg.mylan's avatar
serg@serg.mylan committed
9575

9576 9577
              if (!(qbuff.str= alloc_root(YYTHD->mem_root, qbuff.length + 5)))
                YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
9578

9579 9580 9581 9582 9583 9584 9585 9586 9587
              strmake(strmake(qbuff.str, "SET ", 4), (char *)sp->m_tmp_query,
                      qbuff.length);
              qbuff.length+= 4;
              i->m_query= qbuff;
              sp->add_instr(i);
            }
            lex->sphead->restore_lex(YYTHD);
          }
        };
9588 9589

option_type:
9590 9591 9592 9593 9594 9595 9596 9597 9598
        option_type2    {}
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

option_type2:
	/* empty */	{ $$= OPT_DEFAULT; }
	| ONE_SHOT_SYM	{ Lex->one_shot_set= 1; $$= OPT_SESSION; }
9599 9600 9601 9602
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9603
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
9604 9605 9606 9607 9608 9609
	| 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
9610
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
9611 9612 9613
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9614

9615 9616 9617
ext_option_value:
        sys_option_value
        | option_type2 option_value;
9618

9619 9620 9621 9622
sys_option_value:
        option_type internal_variable_name equal set_expr_or_default
        {
          LEX *lex=Lex;
9623

9624
          if ($2.var == trg_new_row_fake_var)
9625 9626 9627
          {
            /* We are in trigger and assigning value to field of new row */
            Item *it;
9628
            Item_trigger_field *trg_fld;
9629 9630
            sp_instr_set_trigger_field *sp_fld;
	    LINT_INIT(sp_fld);
9631 9632 9633 9634
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
9635
            }
9636 9637
            if ($4)
              it= $4;
9638
            else
9639 9640 9641 9642
            {
              /* QQ: Shouldn't this be field's default value ? */
              it= new Item_null();
            }
9643

9644
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
9645
                                                  Item_trigger_field::NEW_ROW,
9646
                                                  $2.base_name.str)) ||
9647 9648 9649 9650 9651
                !(sp_fld= new sp_instr_set_trigger_field(lex->sphead->
                          	                         instructions(),
                                	                 lex->spcont,
							 trg_fld,
                                        	         it, lex)))
9652
              YYABORT;
9653

9654 9655 9656 9657
            /*
              Let us add this item to list of all Item_trigger_field
              objects in trigger.
            */
9658 9659
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
                                    (byte **)&trg_fld->next_trg_field);
9660

9661
            lex->sphead->add_instr(sp_fld);
9662 9663 9664 9665
          }
          else if ($2.var)
          { /* System variable */
            if ($1)
9666
              lex->option_type= $1;
9667 9668 9669 9670 9671 9672 9673 9674
            lex->var_list.push_back(new set_var(lex->option_type, $2.var,
                                    &$2.base_name, $4));
          }
          else
          {
            /* An SP local variable */
            sp_pcontext *ctx= lex->spcont;
            sp_pvar_t *spv;
9675
            sp_instr_set *sp_set;
9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690
            Item *it;
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }

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

            if ($4)
              it= $4;
            else if (spv->dflt)
              it= spv->dflt;
            else
              it= new Item_null();
9691 9692 9693
            sp_set= new sp_instr_set(lex->sphead->instructions(), ctx,
                                     spv->offset, it, spv->type, lex, TRUE);
            lex->sphead->add_instr(sp_set);
9694 9695 9696 9697 9698
          }
        }
        | option_type TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	{
	  LEX *lex=Lex;
9699
          if ($1)
9700
            lex->option_type= $1;
9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712
	  lex->var_list.push_back(new set_var(lex->option_type,
                                              find_sys_var("tx_isolation"),
                                              &null_lex_str,
                                              new Item_int((int32) $5)));
	}
        ;

option_value:
	'@' ident_or_text equal expr
	{
          Lex->var_list.push_back(new set_var_user(new Item_func_set_user_var($2,$4)));
	}
9713
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
9714 9715
	  {
	    LEX *lex=Lex;
9716
	    lex->var_list.push_back(new set_var($3, $4.var, &$4.base_name, $6));
9717
	  }
9718
	| charset old_or_new_charset_name_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9719
	{
9720
	  THD *thd= YYTHD;
9721
	  LEX *lex= Lex;
9722
	  $2= $2 ? $2: global_system_variables.character_set_client;
9723
	  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
9724
	}
9725 9726 9727 9728 9729 9730 9731 9732 9733 9734
        | NAMES_SYM equal expr
	  {
	    LEX *lex= Lex;
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING names;

	    names.str= (char *)"names";
	    names.length= 5;
	    if (spc && spc->find_pvar(&names))
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), names.str);
9735 9736 9737
            else
              yyerror(ER(ER_SYNTAX_ERROR));

9738 9739
	    YYABORT;
	  }
9740
	| NAMES_SYM charset_name_or_default opt_collate
9741
	{
9742
	  LEX *lex= Lex;
9743
	  $2= $2 ? $2 : global_system_variables.character_set_client;
9744 9745
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
9746
	  {
9747 9748
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
9749
	    YYABORT;
9750
	  }
9751
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
9752
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9753
	| PASSWORD equal text_or_password
9754
	  {
9755
	    THD *thd=YYTHD;
9756
	    LEX_USER *user;
9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767
	    LEX *lex= Lex;	    
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING pw;

	    pw.str= (char *)"password";
	    pw.length= 8;
	    if (spc && spc->find_pvar(&pw))
	    {
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), pw.str);
	      YYABORT;
	    }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9768
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
9769
	      YYABORT;
9770
	    user->host=null_lex_str;
9771
	    user->user.str=thd->security_ctx->priv_user;
9772
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
9773 9774
	  }
	| PASSWORD FOR_SYM user equal text_or_password
9775
	  {
9776 9777
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
9778
	;
9779

9780 9781 9782
internal_variable_name:
	ident
	{
9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794
	  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;
9795
	    $$.base_name= null_lex_str;
9796 9797 9798 9799
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
9800 9801 9802
            if (tmp == &sys_time_zone &&
                lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
9803 9804 9805 9806 9807 9808 9809 9810
            else if (spc && tmp == &sys_autocommit)
            {
              /*
                We don't allow setting AUTOCOMMIT from a stored function
		or trigger.
              */
              lex->sphead->m_flags|= sp_head::HAS_SET_AUTOCOMMIT_STMT;
            }
9811 9812 9813 9814 9815 9816 9817
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
9818
	}
9819 9820
	| ident '.' ident
	  {
9821
            LEX *lex= Lex;
9822 9823
            if (check_reserved_words(&$1))
            {
9824
	      yyerror(ER(ER_SYNTAX_ERROR));
9825 9826
              YYABORT;
            }
9827 9828 9829 9830 9831 9832
            if (lex->sphead && lex->sphead->m_type == TYPE_ENUM_TRIGGER &&
                (!my_strcasecmp(system_charset_info, $1.str, "NEW") || 
                 !my_strcasecmp(system_charset_info, $1.str, "OLD")))
            {
              if ($1.str[0]=='O' || $1.str[0]=='o')
              {
9833
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
9834 9835 9836 9837
                YYABORT;
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
9838 9839
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
9840 9841 9842 9843
                YYABORT;
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
9844
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
9845 9846 9847
                YYABORT;
              }
              /* This special combination will denote field of NEW row */
9848
              $$.var= trg_new_row_fake_var;
9849 9850 9851 9852 9853 9854 9855 9856
              $$.base_name= $3;
            }
            else
            {
              sys_var *tmp=find_sys_var($3.str, $3.length);
              if (!tmp)
                YYABORT;
              if (!tmp->is_struct())
9857
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
9858 9859 9860
              $$.var= tmp;
              $$.base_name= $1;
            }
9861 9862 9863 9864 9865 9866 9867
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
9868
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9869 9870 9871
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
9872
	  }
9873
        ;
9874 9875 9876 9877 9878 9879 9880

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
9882 9883 9884 9885
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
9886
	    $$= $3.length ? YYTHD->variables.old_passwords ?
9887 9888 9889 9890 9891 9892 9893 9894
	        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;
9895 9896
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9897

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

9899
set_expr_or_default:
9900 9901
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
9902 9903
	| 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
9904
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
9905
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9906 9907


bk@work.mysql.com's avatar
bk@work.mysql.com committed
9908 9909 9910 9911 9912
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
9913 9914 9915 9916
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
9917
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOCK");
9918 9919 9920
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_LOCK_TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9921
	}
9922 9923
	table_lock_list
	{}
9924
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9925 9926 9927

table_or_tables:
	TABLE_SYM
9928
	| TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9929 9930 9931

table_lock_list:
	table_lock
9932
	| table_lock_list ',' table_lock;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9933 9934 9935

table_lock:
	table_ident opt_table_alias lock_option
9936
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9937
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
9938 9939
	   YYABORT;
	}
9940
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9941 9942 9943

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
9944
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9945
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
9946 9947
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9948 9949

unlock:
9950 9951 9952 9953 9954 9955
	UNLOCK_SYM
	{
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
9956
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "UNLOCK");
9957 9958 9959 9960 9961 9962
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_UNLOCK_TABLES;
	}
	table_or_tables
	{}
9963
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9964 9965


9966
/*
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
9967
** Handler: direct access to ISAM functions
9968 9969 9970 9971 9972
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
9973
	  LEX *lex= Lex;
9974 9975 9976 9977 9978
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
9979
	  lex->sql_command = SQLCOM_HA_OPEN;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9980
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
9981 9982
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
9983
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
9984
	{
9985
	  LEX *lex= Lex;
9986 9987 9988 9989 9990
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
9991
	  lex->sql_command = SQLCOM_HA_CLOSE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9992
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
9993 9994
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
9995
	| HANDLER_SYM table_ident_nodb READ_SYM
9996
	{
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
9997
	  LEX *lex=Lex;
9998 9999 10000 10001 10002
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
10003 10004
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
10005
	  lex->current_select->select_limit= new Item_int((int32) 1);
10006
	  lex->current_select->offset_limit= 0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
10007
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
10008
	    YYABORT;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10009
        }
10010
        handler_read_or_scan where_clause opt_limit_clause {}
10011
        ;
10012

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10013
handler_read_or_scan:
10014 10015
	handler_scan_function         { Lex->ident= null_lex_str; }
        | ident handler_rkey_function { Lex->ident= $1; }
10016
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10017 10018 10019

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
10020 10021
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10022 10023 10024 10025 10026 10027

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;  }
10028 10029
	| handler_rkey_mode
	{
10030 10031
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
10032
	  lex->ha_rkey_mode=$1;
10033
	  if (!(lex->insert_list = new List_item))
10034
	    YYABORT;
10035 10036
	} '(' values ')' { }
        ;
10037 10038

handler_rkey_mode:
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
10039 10040 10041 10042
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
10043 10044
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
10045

bk@work.mysql.com's avatar
bk@work.mysql.com committed
10046 10047 10048
/* GRANT / REVOKE */

revoke:
10049
	REVOKE clear_privileges revoke_command
10050 10051 10052 10053
	{}
        ;

revoke_command:
10054
	grant_privileges ON opt_table grant_ident FROM grant_list
10055
	{
10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083
          LEX *lex= Lex;
	  lex->sql_command= SQLCOM_REVOKE;
          lex->type= 0;
        }
        |
        grant_privileges ON FUNCTION_SYM grant_ident FROM grant_list
        {
          LEX *lex= Lex;
          if (lex->columns.elements)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
          }
	  lex->sql_command= SQLCOM_REVOKE;
          lex->type= TYPE_ENUM_FUNCTION;
          
        }
	|
        grant_privileges ON PROCEDURE grant_ident FROM grant_list
        {
          LEX *lex= Lex;
          if (lex->columns.elements)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
          }
	  lex->sql_command= SQLCOM_REVOKE;
          lex->type= TYPE_ENUM_PROCEDURE;
10084
        }
10085
	|
10086
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
10087 10088 10089
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
10090
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10091 10092

grant:
10093 10094 10095 10096 10097 10098
	GRANT clear_privileges grant_command
	{}
        ;

grant_command:
	grant_privileges ON opt_table grant_ident TO_SYM grant_list
10099
	require_clause grant_options
10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131
	{
          LEX *lex= Lex;
          lex->sql_command= SQLCOM_GRANT;
          lex->type= 0;
        }
        |
	grant_privileges ON FUNCTION_SYM grant_ident TO_SYM grant_list
	require_clause grant_options
	{
          LEX *lex= Lex;
          if (lex->columns.elements)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
          }
          lex->sql_command= SQLCOM_GRANT;
          lex->type= TYPE_ENUM_FUNCTION;
        }
        |
	grant_privileges ON PROCEDURE grant_ident TO_SYM grant_list
	require_clause grant_options
	{
          LEX *lex= Lex;
          if (lex->columns.elements)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
          }
          lex->sql_command= SQLCOM_GRANT;
          lex->type= TYPE_ENUM_PROCEDURE;
        }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10132

10133 10134 10135 10136
opt_table:
	/* Empty */
	| TABLE_SYM ;
        
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10137
grant_privileges:
10138 10139 10140 10141 10142 10143
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
10144
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10145

10146 10147 10148 10149 10150
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

10151 10152 10153
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10154

10155
object_privilege:
serg@serg.mylan's avatar
serg@serg.mylan committed
10156
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
10157 10158 10159
	| 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 {}
10160 10161
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
10162
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10163 10164 10165
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
10166
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10167 10168 10169 10170
	| RELOAD	{ Lex->grant |= RELOAD_ACL;}
	| SHUTDOWN	{ Lex->grant |= SHUTDOWN_ACL;}
	| PROCESS	{ Lex->grant |= PROCESS_ACL;}
	| FILE_SYM	{ Lex->grant |= FILE_ACL;}
10171 10172 10173 10174 10175
	| 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; }
10176 10177 10178 10179
	| 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; }
10180 10181
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
10182
	| CREATE USER { Lex->grant |= CREATE_USER_ACL; }
10183
        | EVENT_SYM { Lex->grant |= EVENT_ACL;}
10184
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10185

10186

10187 10188
opt_and:
	/* empty */	{}
10189
	| AND_SYM	{}
10190 10191 10192 10193 10194 10195 10196 10197 10198
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
10199 10200 10201 10202
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
10203
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
10204 10205 10206 10207 10208 10209 10210 10211 10212
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
10213
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
10214 10215 10216 10217 10218 10219 10220 10221 10222
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
10223
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
10224 10225 10226 10227 10228
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
10229

10230
grant_ident:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10231 10232
	'*'
	  {
10233
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10234
	    lex->current_select->db= lex->thd->db;
10235
	    if (lex->grant == GLOBAL_ACLS)
10236 10237
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10238
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10239 10240
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
10241
	      YYABORT;
10242
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10243 10244 10245
	  }
	| ident '.' '*'
	  {
10246
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10247
	    lex->current_select->db = $1.str;
10248
	    if (lex->grant == GLOBAL_ACLS)
10249 10250
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10251
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10252 10253
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10254 10255 10256 10257 10258
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
10259
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10260
	    lex->current_select->db = NULL;
10261 10262
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
10263
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10264
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10265 10266
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10267 10268 10269 10270 10271
	      YYABORT;
	    }
	  }
	| table_ident
	  {
10272
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
10273
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10274
	      YYABORT;
10275
	    if (lex->grant == GLOBAL_ACLS)
10276
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
10277 10278
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10279 10280 10281


user_list:
10282 10283 10284 10285 10286 10287 10288 10289 10290 10291
	user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;


grant_list:
10292
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
10293
	| grant_list ',' grant_user
10294 10295 10296 10297 10298
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10299 10300 10301 10302 10303 10304 10305 10306


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
10307
             if (YYTHD->variables.old_passwords)
10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324
             {
               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
10325 10326 10327
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
10328
	  { $$= $1; $1->password= $5; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10329
	| user
10330
	  { $$= $1; $1->password= null_lex_str; }
10331
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10332 10333 10334


opt_column_list:
10335 10336 10337 10338 10339
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
10340
	| '(' column_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10341 10342 10343

column_list:
	column_list ',' column_list_id
10344
	| column_list_id;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10345 10346 10347 10348

column_list_id:
	ident
	{
10349
	  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
10350 10351
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
10352
	  LEX *lex=Lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10353 10354
	  while ((point=iter++))
	  {
10355 10356
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10357 10358
		break;
	  }
10359
	  lex->grant_tot_col|= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10360
	  if (point)
10361
	    point->rights |= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10362
	  else
10363
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
10364 10365
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10366

tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
10367 10368

require_clause: /* empty */
10369
        | REQUIRE_SYM require_list
10370 10371 10372
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
10373
        | REQUIRE_SYM SSL_SYM
10374 10375 10376
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
10377
        | REQUIRE_SYM X509_SYM
10378 10379 10380 10381 10382 10383 10384
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
10385
          ;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
10386

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10387
grant_options:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10388
	/* empty */ {}
10389
	| WITH grant_option_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10390

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10391 10392
grant_option_list:
	grant_option_list grant_option {}
10393 10394
	| grant_option {}
        ;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10395 10396 10397

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
serg@serg.mylan's avatar
serg@serg.mylan committed
10398
        | MAX_QUERIES_PER_HOUR ulong_num
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10399
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
10400 10401 10402
	  LEX *lex=Lex;
	  lex->mqh.questions=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
10403
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
10404
        | MAX_UPDATES_PER_HOUR ulong_num
10405
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
10406 10407 10408
	  LEX *lex=Lex;
	  lex->mqh.updates=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
10409
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
10410
        | MAX_CONNECTIONS_PER_HOUR ulong_num
10411
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
10412 10413 10414
	  LEX *lex=Lex;
	  lex->mqh.conn_per_hour= $2;
	  lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
10415
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
10416
        | MAX_USER_CONNECTIONS_SYM ulong_num
10417
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
10418 10419 10420
	  LEX *lex=Lex;
          lex->mqh.user_conn= $2;
          lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
10421 10422
	}
        ;
10423

bk@work.mysql.com's avatar
bk@work.mysql.com committed
10424
begin:
serg@serg.mylan's avatar
serg@serg.mylan committed
10425 10426 10427 10428 10429 10430 10431
	BEGIN_SYM  
        {
	  LEX *lex=Lex;
          lex->sql_command = SQLCOM_BEGIN;
          lex->start_transaction_opt= 0;
        }
        opt_work {}
10432
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10433 10434 10435

opt_work:
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
10436
	| WORK_SYM  {}
10437
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10438

10439
opt_chain:
serg@serg.mylan's avatar
serg@serg.mylan committed
10440
	/* empty */ { $$= (YYTHD->variables.completion_type == 1); }
10441 10442 10443 10444 10445
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
serg@serg.mylan's avatar
serg@serg.mylan committed
10446
	/* empty */ { $$= (YYTHD->variables.completion_type == 2); }
10447 10448 10449 10450 10451 10452 10453 10454 10455
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
10456
commit:
serg@serg.mylan's avatar
serg@serg.mylan committed
10457
	COMMIT_SYM opt_work opt_chain opt_release
10458
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10459 10460 10461 10462
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_COMMIT;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
10463 10464
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10465 10466

rollback:
serg@serg.mylan's avatar
serg@serg.mylan committed
10467
	ROLLBACK_SYM opt_work opt_chain opt_release
10468
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10469 10470 10471 10472
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
10473
	}
10474 10475
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
10476
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10477 10478 10479
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK_TO_SAVEPOINT;
	  lex->ident= $5;
10480 10481 10482
	}
	;

10483 10484 10485
savepoint:
	SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10486 10487 10488
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SAVEPOINT;
	  lex->ident= $2;
10489 10490 10491 10492 10493 10494
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10495 10496 10497
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RELEASE_SAVEPOINT;
	  lex->ident= $3;
10498 10499
	}
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
10500
  
10501
/*
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10502
   UNIONS : glue selects together
10503 10504 10505
*/


10506
union_clause:
10507
	/* empty */ {}
10508 10509
	| union_list
	;
10510 10511

union_list:
10512
	UNION_SYM union_option
10513 10514 10515 10516 10517
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
10518
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
10519
	    YYABORT;
10520
	  }
10521
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
10522
	  {
10523
            yyerror(ER(ER_SYNTAX_ERROR));
10524 10525
	    YYABORT;
	  }
10526
	  if (mysql_new_select(lex, 0))
10527
	    YYABORT;
10528
          mysql_init_select(lex);
10529
	  lex->current_select->linkage=UNION_TYPE;
10530 10531 10532
          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
10533
	}
10534 10535
	select_init
        {
10536 10537 10538 10539
          /*
	    Remove from the name resolution context stack the context of the
            last select in the union.
	  */
10540 10541
          Lex->pop_context();
        }
10542
	;
10543 10544

union_opt:
10545 10546 10547
	/* Empty */ { $$= 0; }
	| union_list { $$= 1; }
	| union_order_or_limit { $$= 1; }
10548
	;
10549

10550
union_order_or_limit:
10551
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10552
	    THD *thd= YYTHD;
10553
	    LEX *lex= thd->lex;
10554
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10555
	    SELECT_LEX *sel= lex->current_select;
10556
	    SELECT_LEX_UNIT *unit= sel->master_unit();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10557
	    SELECT_LEX *fake= unit->fake_select_lex;
10558 10559 10560 10561 10562 10563
	    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
10564
	    thd->where= "global ORDER clause";
10565
	  }
10566
	order_or_limit
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10567 10568
          {
	    THD *thd= YYTHD;
10569
	    thd->lex->current_select->no_table_names_allowed= 0;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10570 10571
	    thd->where= "";
          }
10572 10573 10574
	;

order_or_limit:
10575 10576
	order_clause opt_limit_clause_init
	| limit_clause
10577
	;
10578 10579

union_option:
10580 10581 10582 10583
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
10584

10585 10586
singlerow_subselect:
	subselect_start singlerow_subselect_init
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10587 10588 10589 10590
	subselect_end
	{
	  $$= $2;
	};
10591

10592
singlerow_subselect_init:
10593
	select_init2
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10594
	{
10595 10596
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10597
	};
10598 10599

exists_subselect:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10600 10601 10602 10603 10604
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
10605 10606

exists_subselect_init:
10607
	select_init2
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10608
	{
10609 10610
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10611
	};
10612

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10613 10614 10615 10616 10617 10618 10619 10620
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
10621
  select_init2
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10622
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10623
    $$= Lex->current_select->master_unit()->first_select();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10624 10625
  };

10626
subselect_start:
10627
	'(' SELECT_SYM
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10628
	{
10629
	  LEX *lex=Lex;
10630
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
10631 10632
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
10633 10634
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
10635 10636
	    YYABORT;
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10637 10638 10639
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
10640 10641

subselect_end:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10642 10643 10644
	')'
	{
	  LEX *lex=Lex;
10645
          lex->pop_context();
10646
	  lex->current_select = lex->current_select->return_after_parsing();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
10647
          lex->nest_level--;
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
10648
	};
serg@serg.mylan's avatar
serg@serg.mylan committed
10649

10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761
definer:
	get_definer
	{
	  THD *thd= YYTHD;
	  
	  if (! (thd->lex->definer= create_definer(thd, &$1->user, &$1->host)))
	    YYABORT;
	}
	;

get_definer:
	opt_current_definer
	{
	  THD *thd= YYTHD;
          
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
	    YYABORT;

	  if (get_default_definer(thd, $$))
	    YYABORT;
	}
	| DEFINER_SYM EQ ident_or_text '@' ident_or_text
	{
	  if (!($$=(LEX_USER*) YYTHD->alloc(sizeof(st_lex_user))))
	    YYABORT;

	  $$->user= $3;
	  $$->host= $5;
	}
	;

opt_current_definer:
	/* empty */
	| DEFINER_SYM EQ CURRENT_USER optional_braces
	;

/**************************************************************************

 CREATE VIEW statement options.

**************************************************************************/

view_replace_or_algorithm:
	view_replace
	{}
	| view_replace view_algorithm
	{}
	| view_algorithm
	{}
	;

view_replace:
	OR_SYM REPLACE
	{ Lex->create_view_mode= VIEW_CREATE_OR_REPLACE; }
	;

view_algorithm:
	ALGORITHM_SYM EQ UNDEFINED_SYM
	{ Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED; }
	| ALGORITHM_SYM EQ MERGE_SYM
	{ Lex->create_view_algorithm= VIEW_ALGORITHM_MERGE; }
	| ALGORITHM_SYM EQ TEMPTABLE_SYM
	{ Lex->create_view_algorithm= VIEW_ALGORITHM_TMPTABLE; }
	;

view_algorithm_opt:
	/* empty */
	{ Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED; }
	| view_algorithm
	{}
	;

view_or_trigger:
	definer view_or_trigger_tail
	{}
	| view_replace_or_algorithm definer view_tail
	{}
	;

view_or_trigger_tail:
	view_tail
	{}
	| trigger_tail
	{}
	;

view_suid:
	/* empty */
	{ Lex->create_view_suid= TRUE; }
	| SQL_SYM SECURITY_SYM DEFINER_SYM
	{ Lex->create_view_suid= TRUE; }
	| SQL_SYM SECURITY_SYM INVOKER_SYM
	{ Lex->create_view_suid= FALSE; }
	;

view_tail:
	view_suid VIEW_SYM table_ident
	{
	  THD *thd= YYTHD;
	  LEX *lex= thd->lex;
	  lex->sql_command= SQLCOM_CREATE_VIEW;
	  /* first table in list is target VIEW name */
	  if (!lex->select_lex.add_table_to_list(thd, $3, NULL, 0))
	    YYABORT;
	}
	view_list_opt AS view_select view_check_option
	{}
	;

view_list_opt:
	/* empty */
	{}
10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777
	| '(' 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)));
	  }
	;

10778 10779 10780
view_select:
	SELECT_SYM remember_name select_init2
	{
10781 10782 10783 10784 10785 10786
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          char *stmt_beg= (lex->sphead ?
                           (char *)lex->sphead->m_tmp_query :
                           thd->query);
	  lex->create_view_select_start= $2 - stmt_beg;
10787 10788 10789
	}
	| '(' remember_name select_paren ')' union_opt
	{
10790 10791 10792 10793 10794 10795
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          char *stmt_beg= (lex->sphead ?
                           (char *)lex->sphead->m_tmp_query :
                           thd->query);
	  lex->create_view_select_start= $2 - stmt_beg;
10796
	}
10797 10798
	;

10799
view_check_option:
10800
	/* empty */
10801 10802 10803 10804 10805 10806 10807
	{ Lex->create_view_check= VIEW_CHECK_NONE; }
	| WITH CHECK_SYM OPTION
	{ Lex->create_view_check= VIEW_CHECK_CASCADED; }
	| WITH CASCADED CHECK_SYM OPTION
	{ Lex->create_view_check= VIEW_CHECK_CASCADED; }
	| WITH LOCAL_SYM CHECK_SYM OPTION
	{ Lex->create_view_check= VIEW_CHECK_LOCAL; }
10808
	;
10809

10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823
/**************************************************************************

 CREATE TRIGGER statement parts.

**************************************************************************/

trigger_tail:
	TRIGGER_SYM remember_name sp_name trg_action_time trg_event 
	ON table_ident FOR_SYM EACH_SYM ROW_SYM
	{
	  LEX *lex= Lex;
	  sp_head *sp;
	 
	  if (lex->sphead)
10824
	  {
10825 10826
	    my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "TRIGGER");
	    YYABORT;
10827
	  }
10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843
	
	  if (!(sp= new sp_head()))
	    YYABORT;
	  sp->reset_thd_mem_root(YYTHD);
	  sp->init(lex);
	
	  lex->trigger_definition_begin= $2;
	  
	  sp->m_type= TYPE_ENUM_TRIGGER;
	  lex->sphead= sp;
	  lex->spname= $3;
	  /*
	    We have to turn of CLIENT_MULTI_QUERIES while parsing a
	    stored procedure, otherwise yylex will chop it into pieces
	    at each ';'.
	  */
andrey@lmy004's avatar
andrey@lmy004 committed
10844
	  $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
10845 10846 10847 10848 10849
	  YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
	  
	  bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	  lex->sphead->m_chistics= &lex->sp_chistics;
	  lex->sphead->m_body_begin= lex->ptr;
10850 10851
          while (my_isspace(system_charset_info, lex->sphead->m_body_begin[0]))
            ++lex->sphead->m_body_begin;
10852 10853 10854 10855 10856 10857 10858 10859 10860
	}
	sp_proc_stmt
	{
	  LEX *lex= Lex;
	  sp_head *sp= lex->sphead;
	  
	  lex->sql_command= SQLCOM_CREATE_TRIGGER;
	  sp->init_strings(YYTHD, lex, $3);
	  /* Restore flag if it was cleared above */
andrey@lmy004's avatar
andrey@lmy004 committed
10861 10862

	  YYTHD->client_capabilities |= $<ulong_num>11;
10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882
	  sp->restore_thd_mem_root(YYTHD);
	
	  if (sp->is_not_allowed_in_function("trigger"))
	      YYABORT;
	
	  /*
	    We have to do it after parsing trigger body, because some of
	    sp_proc_stmt alternatives are not saving/restoring LEX, so
	    lex->query_tables can be wiped out.
	    
	    QQ: What are other consequences of this?
	    
	    QQ: Could we loosen lock type in certain cases ?
	  */
	  if (!lex->select_lex.add_table_to_list(YYTHD, $7, 
	                                         (LEX_STRING*) 0,
	                                         TL_OPTION_UPDATING,
	                                         TL_WRITE))
	    YYABORT;
	}
10883 10884
	;

10885
/*************************************************************************/
10886

10887 10888
xa: XA_SYM begin_or_start xid opt_join_or_resume
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
10889
        Lex->sql_command = SQLCOM_XA_START;
10890
      }
10891
    | XA_SYM END xid opt_suspend
10892
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
10893
        Lex->sql_command = SQLCOM_XA_END;
10894 10895 10896
      }
    | XA_SYM PREPARE_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
10897
        Lex->sql_command = SQLCOM_XA_PREPARE;
10898 10899 10900
      }
    | XA_SYM COMMIT_SYM xid opt_one_phase
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
10901
        Lex->sql_command = SQLCOM_XA_COMMIT;
10902 10903 10904
      }
    | XA_SYM ROLLBACK_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
10905
        Lex->sql_command = SQLCOM_XA_ROLLBACK;
10906 10907 10908
      }
    | XA_SYM RECOVER_SYM
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
10909
        Lex->sql_command = SQLCOM_XA_RECOVER;
10910 10911 10912
      }
    ;

serg@serg.mylan's avatar
serg@serg.mylan committed
10913 10914
xid: text_string
     {
serg@serg.mylan's avatar
serg@serg.mylan committed
10915
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
10916 10917 10918 10919 10920 10921
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set(1L, $1->ptr(), $1->length(), 0, 0);
     }
     | text_string ',' text_string
     {
serg@serg.mylan's avatar
serg@serg.mylan committed
10922
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
10923 10924 10925 10926 10927 10928
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set(1L, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     | text_string ',' text_string ',' ulong_num
     {
serg@serg.mylan's avatar
serg@serg.mylan committed
10929
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
10930 10931 10932 10933 10934
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set($5, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     ;
10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950

begin_or_start:   BEGIN_SYM {}
    |             START_SYM {}
    ;

opt_join_or_resume:
    /* nothing */           { Lex->xa_opt=XA_NONE;        }
    | JOIN_SYM              { Lex->xa_opt=XA_JOIN;        }
    | RESUME_SYM            { Lex->xa_opt=XA_RESUME;      }
    ;

opt_one_phase:
    /* nothing */           { Lex->xa_opt=XA_NONE;        }
    | ONE_SYM PHASE_SYM     { Lex->xa_opt=XA_ONE_PHASE;   }
    ;

10951
opt_suspend:
10952 10953
    /* nothing */           { Lex->xa_opt=XA_NONE;        }
    | SUSPEND_SYM           { Lex->xa_opt=XA_SUSPEND;     }
10954 10955 10956 10957 10958
      opt_migrate
    ;

opt_migrate:
    /* nothing */           { }
10959 10960 10961
    | FOR_SYM MIGRATE_SYM   { Lex->xa_opt=XA_FOR_MIGRATE; }
    ;

10962
install:
10963
  INSTALL_SYM PLUGIN_SYM ident SONAME_SYM TEXT_STRING_sys
10964 10965 10966 10967 10968 10969
  {
    LEX *lex= Lex;
    lex->sql_command= SQLCOM_INSTALL_PLUGIN;
    lex->comment= $3;
    lex->ident= $5;
  };
10970

10971
uninstall:
10972
  UNINSTALL_SYM PLUGIN_SYM ident
10973 10974 10975 10976 10977
  {
    LEX *lex= Lex;
    lex->sql_command= SQLCOM_UNINSTALL_PLUGIN;
    lex->comment= $3;
  };