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

unknown's avatar
unknown committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
7

unknown's avatar
unknown committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
12

unknown's avatar
unknown committed
13 14 15 16
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

unknown's avatar
unknown committed
17
/* sql_yacc.yy */
unknown's avatar
unknown committed
18 19

%{
20 21 22
/* thd is passed as an arg to yyparse(), and subsequently to yylex().
** The type will be void*, so it must be  cast to (THD*) when used.
** Use the YYTHD macro for this.
23 24
*/
#define YYPARSE_PARAM yythd
25
#define YYLEX_PARAM yythd
26 27
#define YYTHD ((THD *)yythd)

unknown's avatar
unknown committed
28 29 30
#define MYSQL_YACC
#define YYINITDEPTH 100
#define YYMAXDEPTH 3200				/* Because of 64K stack */
31
#define Lex (YYTHD->lex)
32
#define Select Lex->current_select
unknown's avatar
unknown committed
33
#include "mysql_priv.h"
unknown's avatar
unknown committed
34
#include "slave.h"
unknown's avatar
unknown committed
35
#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"
unknown's avatar
unknown committed
42
#include <myisam.h>
43
#include <myisammrg.h>
44

45 46 47 48 49 50
typedef struct p_elem_val
{ 
  longlong value;
  bool null_value;
} part_elem_value;

51
int yylex(void *yylval, void *yythd);
unknown's avatar
unknown committed
52

53 54
const LEX_STRING null_lex_str={0,0};

unknown's avatar
unknown committed
55
#define yyoverflow(A,B,C,D,E,F) {ulong val= *(F); if (my_yyoverflow((B), (D), &val)) { yyerror((char*) (A)); return 2; } else { *(F)= (YYSIZE_T)val; }}
unknown's avatar
unknown committed
56

57
#define YYERROR_UNLESS(A)               \
unknown's avatar
unknown committed
58
  if (!(A))                             \
unknown's avatar
unknown committed
59 60 61 62 63
  {					\
    yyerror(ER(ER_SYNTAX_ERROR));	\
    YYABORT;				\
  }

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

%}
%union {
  int  num;
  ulong ulong_num;
unknown's avatar
unknown committed
77
  ulonglong ulonglong_number;
unknown's avatar
unknown committed
78
  longlong longlong_number;
unknown's avatar
unknown committed
79 80 81 82 83 84
  LEX_STRING lex_str;
  LEX_STRING *lex_str_ptr;
  LEX_SYMBOL symbol;
  Table_ident *table;
  char *simple_string;
  Item *item;
85
  Item_num *item_num;
unknown's avatar
unknown committed
86 87
  List<Item> *item_list;
  List<String> *string_list;
unknown's avatar
unknown committed
88 89 90 91 92
  String *string;
  key_part_spec *key_part;
  TABLE_LIST *table_list;
  udf_func *udf;
  LEX_USER *lex_user;
unknown's avatar
unknown committed
93
  struct sys_var_with_base variable;
94
  enum enum_var_type var_type;
unknown's avatar
unknown committed
95
  Key::Keytype key_type;
unknown's avatar
unknown committed
96
  enum ha_key_alg key_alg;
unknown's avatar
unknown committed
97
  handlerton *db_type;
unknown's avatar
unknown committed
98
  enum row_type row_type;
unknown's avatar
unknown committed
99
  enum ha_rkey_function ha_rkey_mode;
unknown's avatar
unknown committed
100
  enum enum_tx_isolation tx_isolation;
101
  enum Cast_target cast_type;
unknown's avatar
unknown committed
102
  enum Item_udftype udf_type;
103
  CHARSET_INFO *charset;
unknown's avatar
unknown committed
104
  thr_lock_type lock_type;
105
  interval_type interval, interval_time_st;
106
  timestamp_type date_time_type;
unknown's avatar
unknown committed
107
  st_select_lex *select_lex;
unknown's avatar
unknown committed
108
  chooser_compare_func_creator boolfunc2creator;
109 110 111 112
  struct sp_cond_type *spcondtype;
  struct { int vars, conds, hndlrs, curs; } spblock;
  sp_name *spname;
  struct st_lex *lex;
unknown's avatar
unknown committed
113
  sp_head *sphead;
114
  struct p_elem_val *p_elem_value;
unknown's avatar
unknown committed
115 116 117
}

%{
118
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
unknown's avatar
unknown committed
119 120 121 122
%}

%pure_parser					/* We have threads */

unknown's avatar
unknown committed
123
%token  END_OF_INPUT
unknown's avatar
unknown committed
124

125
%token  ABORT_SYM
unknown's avatar
unknown committed
126
%token  ACCESSIBLE_SYM
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
%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
144
%token  AT_SYM
145
%token  ATAN
unknown's avatar
unknown committed
146
%token  AUTHORS_SYM
147
%token  AUTO_INC
unknown's avatar
unknown committed
148
%token  AUTOEXTEND_SIZE_SYM
149 150 151 152 153 154 155 156 157 158
%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
unknown's avatar
unknown committed
159
%token  BIN_NUM
160 161 162 163 164 165 166 167 168 169 170 171 172 173
%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
174
%token  CASCADED
175
%token  CAST_SYM
unknown's avatar
unknown committed
176
%token  CHAIN_SYM
177 178 179 180 181 182 183 184 185 186
%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
unknown's avatar
unknown committed
187
%token  CODE_SYM
188 189 190 191 192 193 194
%token  COLLATE_SYM
%token  COLLATION_SYM
%token  COLUMNS
%token  COLUMN_SYM
%token  COMMENT_SYM
%token  COMMITTED_SYM
%token  COMMIT_SYM
unknown's avatar
unknown committed
195
%token  COMPACT_SYM
196
%token  COMPLETION_SYM
197 198 199 200
%token  COMPRESSED_SYM
%token  CONCAT
%token  CONCAT_WS
%token  CONCURRENT
201
%token  CONDITION_SYM
202 203 204
%token  CONNECTION_SYM
%token  CONSISTENT_SYM
%token  CONSTRAINT
205 206
%token  CONTAINS_SYM
%token  CONTINUE_SYM
207 208 209 210 211 212 213
%token  CONVERT_SYM
%token  CONVERT_TZ_SYM
%token  COUNT_SYM
%token  CREATE
%token  CROSS
%token  CUBE_SYM
%token  CURDATE
214
%token  CURRENT_USER
215 216 217 218
%token  CURSOR_SYM
%token  CURTIME
%token  DATABASE
%token  DATABASES
unknown's avatar
unknown committed
219
%token  DATAFILE_SYM
220 221 222 223 224 225 226 227 228 229 230
%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
unknown's avatar
unknown committed
231
%token  DECIMAL_NUM
232
%token  DECIMAL_SYM
233
%token  DECLARE_SYM
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
%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
%token  DISCARD
unknown's avatar
unknown committed
249
%token  DISK_SYM
250 251 252 253 254 255 256
%token  DISTINCT
%token  DIV_SYM
%token  DOUBLE_SYM
%token  DO_SYM
%token  DROP
%token  DUAL_SYM
%token  DUMPFILE
unknown's avatar
unknown committed
257
%token  DUPLICATE_SYM
258
%token  DYNAMIC_SYM
259
%token  EACH_SYM
260 261 262 263 264 265 266
%token  ELSEIF_SYM
%token  ELT_FUNC
%token  ENABLE_SYM
%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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
426
%token  MAX_SIZE_SYM
427 428
%token  MAX_SYM
%token  MAX_UPDATES_PER_HOUR
unknown's avatar
unknown 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
unknown's avatar
unknown committed
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
unknown's avatar
unknown 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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
472
%token  NO_WAIT_SYM
473 474 475 476
%token  NO_WRITE_TO_BINLOG
%token  NULL_SYM
%token  NUM
%token  NUMERIC_SYM
unknown's avatar
unknown 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
%token  PARTITION_SYM
unknown's avatar
unknown committed
498
%token  PARTITIONING_SYM
499
%token  PARTITIONS_SYM
500
%token  PASSWORD
501
%token  PARAM_MARKER
502
%token  PHASE_SYM
503
%token  PLUGIN_SYM
504 505 506 507 508 509 510
%token  POINTFROMTEXT
%token  POINT_SYM
%token  POLYFROMTEXT
%token  POLYGON
%token  POSITION_SYM
%token  PRECISION
%token  PREPARE_SYM
511
%token  PRESERVE_SYM
512 513 514 515 516 517 518 519 520 521 522
%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  RAND
523
%token  RANGE_SYM
524
%token  READS_SYM
unknown's avatar
unknown committed
525
%token  READ_ONLY_SYM
526
%token  READ_SYM
unknown's avatar
unknown committed
527
%token  READ_WRITE_SYM
528
%token  REAL
unknown's avatar
unknown committed
529
%token  REBUILD_SYM
530
%token  RECOVER_SYM
unknown's avatar
unknown committed
531 532
%token  REDO_BUFFER_SIZE_SYM
%token  REDOFILE_SYM
unknown's avatar
unknown committed
533
%token  REDUNDANT_SYM
534 535 536 537 538
%token  REFERENCES
%token  REGEXP
%token  RELAY_LOG_FILE_SYM
%token  RELAY_LOG_POS_SYM
%token  RELAY_THREAD
unknown's avatar
unknown committed
539
%token  RELEASE_SYM
540
%token  RELOAD
unknown's avatar
unknown committed
541
%token  REMOVE_SYM
542
%token  RENAME
unknown's avatar
unknown committed
543
%token  REORGANIZE_SYM
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
%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
unknown's avatar
unknown committed
562
%token  ROUTINE_SYM
563 564 565 566 567 568
%token  ROWS_SYM
%token  ROW_COUNT_SYM
%token  ROW_FORMAT_SYM
%token  ROW_SYM
%token  RTREE_SYM
%token  SAVEPOINT_SYM
569
%token  SCHEDULE_SYM
570 571
%token  SECOND_MICROSECOND_SYM
%token  SECOND_SYM
572
%token  SECURITY_SYM
573 574
%token  SELECT_SYM
%token  SENSITIVE_SYM
575
%token  SEPARATOR_SYM
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
%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
591
%token  SONAME_SYM
592 593
%token  SOUNDS_SYM
%token  SPATIAL_SYM
594 595 596 597
%token  SPECIFIC_SYM
%token  SQLEXCEPTION_SYM
%token  SQLSTATE_SYM
%token  SQLWARNING_SYM
598 599 600 601 602 603 604 605
%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
606
%token  SSL_SYM
607 608
%token  STARTING
%token  START_SYM
609
%token  STARTS_SYM
610 611
%token  STATUS_SYM
%token  STD_SYM
612
%token  STDDEV_SAMP_SYM
613 614 615 616 617 618
%token  STOP_SYM
%token  STORAGE_SYM
%token  STRAIGHT_JOIN
%token  STRING_SYM
%token  SUBDATE_SYM
%token  SUBJECT_SYM
619 620
%token  SUBPARTITION_SYM
%token  SUBPARTITIONS_SYM
621 622 623 624 625
%token  SUBSTRING
%token  SUBSTRING_INDEX
%token  SUM_SYM
%token  SUPER_SYM
%token  SUSPEND_SYM
626
%token  SYSDATE
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
%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
642
%token  THAN_SYM
643 644 645
%token  TO_SYM
%token  TRAILING
%token  TRANSACTION_SYM
646
%token  TRIGGER_SYM
647
%token  TRIGGERS_SYM
648 649 650
%token  TRIM
%token  TRUE_SYM
%token  TRUNCATE_SYM
unknown's avatar
unknown committed
651
%token  TYPES_SYM
652 653 654 655 656
%token  TYPE_SYM
%token  UDF_RETURNS_SYM
%token  ULONGLONG_NUM
%token  UNCOMMITTED_SYM
%token  UNDEFINED_SYM
unknown's avatar
unknown committed
657 658
%token  UNDO_BUFFER_SIZE_SYM
%token  UNDOFILE_SYM
659
%token  UNDERSCORE_CHARSET
660
%token  UNDO_SYM
661
%token  UNICODE_SYM
662
%token  UNINSTALL_SYM
663 664 665 666 667 668 669 670 671
%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
unknown's avatar
unknown committed
672
%token  UPGRADE_SYM
673 674 675 676 677 678 679 680
%token  USAGE
%token  USER
%token  USE_FRM
%token  USE_SYM
%token  USING
%token  UTC_DATE_SYM
%token  UTC_TIMESTAMP_SYM
%token  UTC_TIME_SYM
681
%token  VAR_SAMP_SYM
682 683 684 685 686 687 688 689
%token  VALUES
%token  VALUE_SYM
%token  VARBINARY
%token  VARCHAR
%token  VARIABLES
%token  VARIANCE_SYM
%token  VARYING
%token  VIEW_SYM
unknown's avatar
unknown committed
690
%token  WAIT_SYM
691 692 693 694
%token  WARNINGS
%token  WEEK_SYM
%token  WHEN_SYM
%token  WHERE
695
%token  WHILE_SYM
696 697 698 699 700 701 702 703 704 705
%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
unknown's avatar
unknown committed
706

707

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

unknown's avatar
unknown committed
726
%type <lex_str>
unknown's avatar
unknown committed
727
        IDENT IDENT_QUOTED TEXT_STRING DECIMAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM
728
	LEX_HOSTNAME ULONGLONG_NUM field_ident select_alias ident ident_or_text
729
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
730
	NCHAR_STRING opt_component key_cache_name
unknown's avatar
unknown committed
731
        sp_opt_label BIN_NUM label_ident TEXT_STRING_filesystem
unknown's avatar
unknown committed
732 733

%type <lex_str_ptr>
734
	opt_table_alias opt_fulltext_parser
unknown's avatar
unknown committed
735 736

%type <table>
737
	table_ident table_ident_nodb references xid
unknown's avatar
unknown committed
738 739

%type <simple_string>
740
	remember_name remember_end opt_ident opt_db text_or_password
741
	opt_constraint constraint ident_or_empty
unknown's avatar
unknown committed
742 743

%type <string>
744
	text_string opt_gconcat_separator
unknown's avatar
unknown committed
745 746

%type <num>
unknown's avatar
unknown committed
747
	type int_type real_type order_dir lock_option
unknown's avatar
unknown committed
748
	udf_type if_exists opt_local opt_table_options table_options
749 750
        table_option opt_if_not_exists opt_no_write_to_binlog
        delete_option opt_temporary all_or_any opt_distinct
751
        opt_ignore_leaves fulltext_options spatial_type union_option
unknown's avatar
unknown committed
752
        start_transaction_opts opt_chain opt_release
753
        union_opt select_derived_init option_type2
754 755 756
        opt_natural_language_mode opt_query_expansion
        opt_ev_status opt_ev_on_completion ev_on_completion opt_ev_comment
        ev_alter_on_schedule_completion opt_ev_rename_to opt_ev_sql_stmt
unknown's avatar
unknown committed
757 758

%type <ulong_num>
759
	ulong_num merge_insert_types
unknown's avatar
unknown committed
760

unknown's avatar
unknown committed
761
%type <ulonglong_number>
unknown's avatar
unknown committed
762
	ulonglong_num size_number
unknown's avatar
unknown committed
763

764
%type <p_elem_value>
unknown's avatar
unknown committed
765 766
        part_bit_expr

unknown's avatar
unknown committed
767 768 769
%type <lock_type>
	replace_lock_option opt_low_priority insert_lock_option load_data_lock

unknown's avatar
unknown committed
770
%type <item>
unknown's avatar
unknown committed
771
	literal text_literal insert_ident order_ident
unknown's avatar
unknown committed
772
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
773 774 775
	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
unknown's avatar
unknown committed
776
	expr_or_default set_expr_or_default interval_expr
unknown's avatar
unknown committed
777
	param_marker singlerow_subselect singlerow_subselect_init
unknown's avatar
unknown committed
778
	exists_subselect exists_subselect_init geometry_function
779
	signed_literal now_or_signed_literal opt_escape
780 781
	sp_opt_default
	simple_ident_nospvar simple_ident_q
782
        field_or_var limit_option
unknown's avatar
unknown committed
783
        part_func_expr
784 785 786

%type <item_num>
	NUM_literal
unknown's avatar
unknown committed
787 788

%type <item_list>
789 790
	expr_list udf_expr_list udf_expr_list2 when_list
	ident_list ident_list_arg
unknown's avatar
unknown committed
791

792 793 794
%type <var_type>
        option_type opt_var_type opt_var_ident_type

unknown's avatar
unknown committed
795
%type <key_type>
796
	key_type opt_unique_or_fulltext constraint_key_type
unknown's avatar
unknown committed
797

unknown's avatar
unknown committed
798 799 800
%type <key_alg>
	key_alg opt_btree_or_rtree

unknown's avatar
unknown committed
801
%type <string_list>
unknown's avatar
unknown committed
802
	key_usage_list using_list
unknown's avatar
unknown committed
803 804 805 806 807

%type <key_part>
	key_part

%type <table_list>
808
	join_table_list  join_table
809
        table_factor table_ref
unknown's avatar
unknown committed
810
        select_derived derived_table_list
unknown's avatar
unknown committed
811

812
%type <date_time_type> date_time_type;
unknown's avatar
unknown committed
813 814
%type <interval> interval

815 816
%type <interval_time_st> interval_time_st

unknown's avatar
unknown committed
817
%type <db_type> storage_engines
unknown's avatar
unknown committed
818 819 820

%type <row_type> row_types

unknown's avatar
unknown committed
821
%type <tx_isolation> isolation_types
unknown's avatar
unknown committed
822

unknown's avatar
unknown committed
823 824
%type <ha_rkey_mode> handler_rkey_mode

825
%type <cast_type> cast_type
unknown's avatar
unknown committed
826

unknown's avatar
unknown committed
827 828
%type <udf_type> udf_func_type

829
%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword keyword_sp
unknown's avatar
unknown committed
830

831
%type <lex_user> user grant_user
unknown's avatar
unknown committed
832

833
%type <charset>
834
	opt_collate
835 836
	charset_name
	charset_name_or_default
unknown's avatar
unknown committed
837 838
	old_or_new_charset_name
	old_or_new_charset_name_or_default
839 840
	collation_name
	collation_name_or_default
841

unknown's avatar
unknown committed
842 843
%type <variable> internal_variable_name

unknown's avatar
unknown committed
844
%type <select_lex> in_subselect in_subselect_init
unknown's avatar
unknown committed
845
	get_select_lex
unknown's avatar
unknown committed
846

unknown's avatar
unknown committed
847 848
%type <boolfunc2creator> comp_op

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

892
%type <NONE> call sp_proc_stmts sp_proc_stmts1 sp_proc_stmt
893 894 895 896 897 898
%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

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

unknown's avatar
unknown committed
905 906
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
907
	',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_SYM BETWEEN_SYM CASE_SYM
908
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM OR2_SYM AND_AND_SYM
unknown's avatar
unknown committed
909 910 911 912 913
%%


query:
	END_OF_INPUT
unknown's avatar
unknown committed
914
	{
915
	   THD *thd= YYTHD;
916
	   if (!thd->bootstrap &&
917
	      (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
918
	   {
unknown's avatar
unknown committed
919
	     my_message(ER_EMPTY_QUERY, ER(ER_EMPTY_QUERY), MYF(0));
920
	     YYABORT;
921
	   }
922 923
	   else
	   {
924
	     thd->lex->sql_command= SQLCOM_EMPTY_QUERY;
925
	   }
unknown's avatar
unknown committed
926
	}
unknown's avatar
unknown committed
927
	| verb_clause END_OF_INPUT {};
unknown's avatar
unknown committed
928 929

verb_clause:
930 931 932 933 934 935
	  statement
	| begin
	;

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

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

1003 1004 1005 1006 1007 1008
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


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

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

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

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
1062 1063
        execute_var_list ',' execute_var_ident
        | execute_var_ident
unknown's avatar
unknown committed
1064 1065 1066 1067 1068 1069 1070 1071
        ;

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;
1072
        }
unknown's avatar
unknown committed
1073 1074
        ;

unknown's avatar
unknown committed
1075 1076
/* help */

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

/* change master */

change:
unknown's avatar
unknown committed
1096
       CHANGE MASTER_SYM TO_SYM
unknown's avatar
unknown committed
1097 1098 1099
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
unknown's avatar
unknown committed
1100
	  bzero((char*) &lex->mi, sizeof(lex->mi));
1101 1102 1103 1104
        }
       master_defs
	{}
       ;
unknown's avatar
unknown committed
1105 1106 1107

master_defs:
       master_def
unknown's avatar
unknown committed
1108
       | master_defs ',' master_def;
unknown's avatar
unknown committed
1109

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
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;
       }
       |
unknown's avatar
unknown committed
1126
       MASTER_PORT_SYM EQ ulong_num
1127 1128 1129 1130
       {
	 Lex->mi.port = $3;
       }
       |
unknown's avatar
unknown committed
1131
       MASTER_CONNECT_RETRY_SYM EQ ulong_num
1132 1133 1134
       {
	 Lex->mi.connect_retry = $3;
       }
unknown's avatar
unknown committed
1135
       | MASTER_SSL_SYM EQ ulong_num
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 1187 1188
         {
           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;
         }
unknown's avatar
unknown committed
1189
       | RELAY_LOG_POS_SYM EQ ulong_num
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
         {
           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;
unknown's avatar
unknown committed
1217
	  lex->create_info.db_type= lex->thd->variables.table_type;
1218
	  lex->create_info.default_table_charset= NULL;
unknown's avatar
unknown committed
1219 1220
	  lex->name= 0;
         lex->like_name= 0;
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
	}
	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;
	  }
1236
	   '(' key_list ')' opt_fulltext_parser
1237 1238
	  {
	    LEX *lex=Lex;
1239 1240 1241 1242 1243 1244
	    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));
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
	    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;
	  }
1259
	| CREATE EVENT_SYM opt_if_not_exists sp_name
1260 1261
          /*
             BE CAREFUL when you add a new rule to update the block where
1262 1263
             YYTHD->client_capabilities is set back to original value
          */
1264 1265 1266 1267 1268
          {
            LEX *lex=Lex;

            if (lex->et)
            {
unknown's avatar
unknown committed
1269 1270 1271 1272
              /*
                Recursive events are not possible because recursive SPs
                are not also possible. lex->sp_head is not stacked.
              */
1273 1274 1275 1276 1277
              // ToDo Andrey : Change the error message
              my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "EVENT");
              YYABORT;
            }

unknown's avatar
unknown committed
1278
            lex->create_info.options= $3;
1279

1280
            if (!(lex->et= new(YYTHD->mem_root) Event_timed())) // implicitly calls Event_timed::init()
unknown's avatar
unknown committed
1281
              YYABORT;
1282

1283 1284 1285 1286 1287
            /*
              We have to turn of CLIENT_MULTI_QUERIES while parsing a
              stored procedure, otherwise yylex will chop it into pieces
              at each ';'.
            */
unknown's avatar
unknown committed
1288 1289 1290
            $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
            YYTHD->client_capabilities &= (~CLIENT_MULTI_QUERIES);

1291 1292
            if (!lex->et_compile_phase)
            {
unknown's avatar
unknown committed
1293
              lex->et->init_name(YYTHD, $4);
1294 1295
              lex->et->init_definer(YYTHD);
            }
1296 1297
          }
          ON SCHEDULE_SYM ev_schedule_time
1298 1299 1300
          opt_ev_on_completion
          opt_ev_status
          opt_ev_comment
1301 1302
          DO_SYM ev_sql_stmt
          {
unknown's avatar
unknown committed
1303
            /*
1304 1305 1306 1307 1308 1309
              Restore flag if it was cleared above
              $1 - CREATE
              $2 - EVENT_SYM
              $3 - opt_if_not_exists
              $4 - sp_name
              $5 - the block above
unknown's avatar
unknown committed
1310 1311
            */
            YYTHD->client_capabilities |= $<ulong_num>5;
1312

1313 1314 1315 1316
            /*
              sql_command is set here because some rules in ev_sql_stmt
              can overwrite it
            */
unknown's avatar
unknown committed
1317
            Lex->sql_command= SQLCOM_CREATE_EVENT;
1318
          }
1319
	| CREATE
1320
	  {
1321 1322 1323
            Lex->create_view_mode= VIEW_CREATE_NEW;
            Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED;
            Lex->create_view_suid= TRUE;
1324
	  }
1325
	  view_or_trigger_or_sp
1326
	  {}
1327 1328 1329
	| CREATE USER clear_privileges grant_list
	  {
	    Lex->sql_command = SQLCOM_CREATE_USER;
1330
          }
unknown's avatar
unknown committed
1331
	| CREATE LOGFILE_SYM GROUP logfile_group_info 
1332 1333
          {
            LEX *lex= Lex;
unknown's avatar
unknown committed
1334
            lex->alter_tablespace_info->ts_cmd_type= CREATE_LOGFILE_GROUP;
1335
          }
unknown's avatar
unknown committed
1336
        | CREATE TABLESPACE tablespace_info
1337 1338
          {
            LEX *lex= Lex;
unknown's avatar
unknown committed
1339
            lex->alter_tablespace_info->ts_cmd_type= CREATE_TABLESPACE;
1340
          }
1341
	;
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354


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:
1355
                my_error(ER_EVENT_INTERVAL_NOT_POSITIVE_OR_TOO_BIG, MYF(0));
1356 1357
              case EVEX_MICROSECOND_UNSUP:
                my_error(ER_NOT_SUPPORTED_YET, MYF(0), "MICROSECOND");
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
                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;
1375 1376 1377 1378 1379 1380 1381 1382 1383
              case ER_WRONG_VALUE:
                {
                  char buff[120];
                  String str(buff,(uint32) sizeof(buff), system_charset_info);
                  String *str2= $2->val_str(&str);
                  my_error(ER_WRONG_VALUE, MYF(0), "AT",
                           str2? str2->c_ptr():"NULL");
                  YYABORT;
                  break;
1384
                }
1385 1386 1387
              case EVEX_BAD_PARAMS:
                my_error(ER_EVENT_EXEC_TIME_IN_THE_PAST, MYF(0));
                YYABORT;
1388
                break;
1389 1390 1391 1392
              }
            }
          }
      ;
1393 1394

opt_ev_status: /* empty */ { $$= 0; }
1395
        | ENABLE_SYM
1396 1397 1398 1399
          {
            LEX *lex=Lex;
            if (!lex->et_compile_phase)
              lex->et->status= MYSQL_EVENT_ENABLED;
1400
            $$= 1;
1401
          }
1402
        | DISABLE_SYM
1403 1404
          {
            LEX *lex=Lex;
1405

1406 1407
            if (!lex->et_compile_phase)
              lex->et->status= MYSQL_EVENT_DISABLED;
1408
            $$= 1;
1409 1410 1411 1412
          }
      ;

ev_starts: /* empty */
1413 1414 1415
          {
            Lex->et->init_starts(YYTHD, new Item_func_now_local());
          }
1416 1417 1418 1419
        | STARTS_SYM expr
          {
            LEX *lex= Lex;
            if (!lex->et_compile_phase)
1420
            {
1421

1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
              switch (lex->et->init_starts(YYTHD, $2)) {
              case EVEX_PARSE_ERROR:
                yyerror(ER(ER_SYNTAX_ERROR));
                YYABORT;
                break;
              case EVEX_BAD_PARAMS:
                {
                  char buff[20];
                  String str(buff,(uint32) sizeof(buff), system_charset_info);
                  String *str2= $2->val_str(&str);
                  my_error(ER_WRONG_VALUE, MYF(0), "STARTS", str2? str2->c_ptr():
                                                                   NULL);
                  YYABORT;
                  break;
                }
              }
            }
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
          }
      ;

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

1462
opt_ev_on_completion: /* empty */ { $$= 0; }
1463 1464 1465
        | ev_on_completion
      ;

1466
ev_on_completion:
1467
          ON COMPLETION_SYM PRESERVE_SYM
1468 1469 1470
          {
            LEX *lex=Lex;
            if (!lex->et_compile_phase)
unknown's avatar
unknown committed
1471
              lex->et->on_completion= MYSQL_EVENT_ON_COMPLETION_PRESERVE;
1472
            $$= 1;
1473 1474 1475 1476 1477
          }
        | ON COMPLETION_SYM NOT_SYM PRESERVE_SYM
          {
            LEX *lex=Lex;
            if (!lex->et_compile_phase)
unknown's avatar
unknown committed
1478
              lex->et->on_completion= MYSQL_EVENT_ON_COMPLETION_DROP;
1479
            $$= 1;
1480 1481
          }
      ;
1482

1483
opt_ev_comment: /* empty */ { $$= 0; }
1484 1485 1486 1487 1488 1489 1490 1491
        | COMMENT_SYM TEXT_STRING_sys
          {
            LEX *lex= Lex;
            if (!lex->et_compile_phase)
            {
              lex->comment= $2;
              lex->et->init_comment(YYTHD, &$2);
            }
1492
            $$= 1;
1493 1494 1495 1496 1497 1498 1499
          }
      ;

ev_sql_stmt:
          {
            LEX *lex= Lex;
            sp_head *sp;
1500

unknown's avatar
unknown committed
1501
            $<sphead>$= lex->sphead;
1502

unknown's avatar
unknown committed
1503 1504 1505 1506
            if (!lex->sphead)
            {
              if (!(sp= new sp_head()))
                YYABORT;
1507

unknown's avatar
unknown committed
1508 1509
              sp->reset_thd_mem_root(YYTHD);
              sp->init(lex);
1510

unknown's avatar
unknown committed
1511
              sp->m_type= TYPE_ENUM_PROCEDURE;
1512

unknown's avatar
unknown committed
1513
              lex->sphead= sp;
1514

unknown's avatar
unknown committed
1515 1516
              bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
              lex->sphead->m_chistics= &lex->sp_chistics;
1517

unknown's avatar
unknown committed
1518 1519 1520
              lex->sphead->m_body_begin= lex->ptr;
            }

1521
            if (!lex->et_compile_phase)
unknown's avatar
unknown committed
1522
              lex->et->body_begin= lex->ptr;
1523 1524 1525 1526
          }
          ev_sql_stmt_inner
          {
            LEX *lex=Lex;
1527

unknown's avatar
unknown committed
1528 1529 1530 1531 1532 1533
            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);
1534

unknown's avatar
unknown committed
1535
              lex->sp_chistics.suid= SP_IS_SUID;//always the definer!
1536

unknown's avatar
unknown committed
1537 1538 1539
              lex->et->sphead= lex->sphead;
              lex->sphead= NULL;
            }
1540 1541 1542 1543 1544 1545
            if (!lex->et_compile_phase)
            {
              lex->et->init_body(YYTHD);
            }
          }
      ;
1546

1547 1548 1549 1550 1551 1552
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
1553
        | sp_labeled_control
1554 1555 1556 1557 1558 1559 1560 1561 1562
        | 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
      ;
1563

1564

1565 1566 1567 1568 1569 1570 1571
clear_privileges:
        /* Nothing */
        {
          LEX *lex=Lex;
          lex->users_list.empty();
          lex->columns.empty();
          lex->grant= lex->grant_tot_col= 0;
1572
	  lex->all_privileges= 0;
1573 1574 1575 1576 1577 1578 1579
          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));
        }
        ;

1580
sp_name:
1581
	  ident '.' ident
1582
	  {
1583 1584 1585 1586 1587
            if (!$1.str || check_db_name($1.str))
            {
	      my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
	      YYABORT;
	    }
unknown's avatar
unknown committed
1588
	    if (check_routine_name($3))
1589 1590 1591 1592
            {
	      my_error(ER_SP_WRONG_NAME, MYF(0), $3.str);
	      YYABORT;
	    }
1593 1594 1595
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
1596
	| ident
1597
	  {
unknown's avatar
unknown committed
1598
	    if (check_routine_name($1))
1599 1600 1601 1602
            {
	      my_error(ER_SP_WRONG_NAME, MYF(0), $1.str);
	      YYABORT;
	    }
1603 1604 1605 1606 1607
	    $$= sp_name_current_db_new(YYTHD, $1);
	  }
	;

create_function_tail:
1608
	  RETURNS_SYM udf_type SONAME_SYM TEXT_STRING_sys
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
	  {
	    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;

1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
            /* 
              First check if AGGREGATE was used, in that case it's a
              syntax error.
            */
            if (lex->udf.type == UDFTYPE_AGGREGATE)
            {
              my_error(ER_SP_NO_AGGREGATE, MYF(0));
              YYABORT;
            }

1631 1632
	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
1633
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
	      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 ';'.
	     */
unknown's avatar
unknown committed
1648
            $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
	    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;
unknown's avatar
unknown committed
1661 1662 1663 1664
	    lex->charset= NULL;
	    lex->length= lex->dec= NULL;
	    lex->interval_list.empty();
	    lex->type= 0;
1665 1666 1667 1668 1669
	  }
	  type
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
unknown's avatar
unknown committed
1670

1671 1672 1673 1674
            if (sp->fill_field_definition(YYTHD, lex,
                                          (enum enum_field_types) $8,
                                          &sp->m_return_field_def))
              YYABORT;
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689

	    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;

1690 1691 1692
            if (sp->is_not_allowed_in_function("function"))
              YYABORT;

1693 1694 1695 1696
	    if (sp->check_backpatch(YYTHD))
	      YYABORT;
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
	    sp->init_strings(YYTHD, lex, lex->spname);
1697 1698 1699 1700 1701
            if (!(sp->m_flags & sp_head::HAS_RETURN))
            {
              my_error(ER_SP_NORETURN, MYF(0), sp->m_qname.str);
              YYABORT;
            }
1702
	    /* Restore flag if it was cleared above */
unknown's avatar
unknown committed
1703
	    YYTHD->client_capabilities |= $<ulong_num>2;
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
	    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; }
1740
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
	;

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();
1762
	    sp_add_used_routine(lex, YYTHD, $2, TYPE_ENUM_PROCEDURE);
1763 1764 1765 1766 1767 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
	  }
          '(' 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
	;

1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
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;
	  }
	;

1815
sp_fdparam:
1816
	  ident sp_init_param type
1817 1818 1819 1820 1821 1822
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
1823
	      my_error(ER_SP_DUP_PARAM, MYF(0), $1.str);
1824 1825
	      YYABORT;
	    }
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
	    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;
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
1852
	  sp_opt_inout sp_init_param ident type
1853 1854 1855 1856
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1857
	    if (spc->find_pvar(&$3, TRUE))
1858
	    {
1859
	      my_error(ER_SP_DUP_PARAM, MYF(0), $3.str);
1860 1861
	      YYABORT;
	    }
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
	    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;
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
	  }
	;

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 */ {}
1885
	| sp_proc_stmts  sp_proc_stmt ';'
1886 1887 1888 1889
	;

sp_proc_stmts1:
	  sp_proc_stmt ';' {}
1890
	| sp_proc_stmts1  sp_proc_stmt ';'
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
	;

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

sp_decl:
1924
          DECLARE_SYM sp_decl_idents
1925
          {
1926
            LEX *lex= Lex;
1927

1928 1929 1930
            lex->sphead->reset_lex(YYTHD);
            lex->spcont->declare_var_boundary($2);
          }
1931
          type
1932
          sp_opt_default
1933 1934
          {
            LEX *lex= Lex;
1935 1936 1937 1938 1939 1940 1941
            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)
1942
            {
1943 1944 1945 1946 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
              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)));
1973
            }
1974 1975

            pctx->declare_var_boundary(0);
1976
            lex->sphead->restore_lex(YYTHD);
1977

1978 1979 1980
            $$.vars= $2;
            $$.conds= $$.hndlrs= $$.curs= 0;
          }
1981 1982 1983 1984 1985 1986 1987
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1988
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
	      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));
2006
	    sp->m_flags|= sp_head::IN_HANDLER;
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
	  }
	  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);
2030
	    sp->m_flags&= ~sp_head::IN_HANDLER;
2031 2032
	    $$.vars= $$.conds= $$.curs= 0;
	    $$.hndlrs= $6;
2033
	    ctx->add_handlers($6);
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
	  }
	| 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))
	    {
2045
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
2046 2047 2048
	      delete $5;
	      YYABORT;
	    }
unknown's avatar
unknown committed
2049 2050
            i= new sp_instr_cpush(sp->instructions(), ctx, $5,
                                  ctx->current_cursors());
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
	    sp->add_instr(i);
	    ctx->push_cursor(&$2);
	    $$.vars= $$.conds= $$.hndlrs= 0;
	    $$.curs= 1;
	  }
	;

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

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

	    if (lex->sql_command != SQLCOM_SELECT)
	    {
unknown's avatar
unknown committed
2073 2074
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
2075 2076 2077 2078
	      YYABORT;
	    }
	    if (lex->result)
	    {
unknown's avatar
unknown committed
2079 2080
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
	      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;
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
	    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();
2111

2112 2113 2114 2115
	      i->add_condition($1);
	      ctx->push_handler($1);
	      $$= 1;
	    }
2116 2117 2118 2119 2120
	  }
	| sp_hcond_list ',' sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
	    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();
2132

2133 2134 2135 2136
	      i->add_condition($3);
	      ctx->push_handler($3);
	      $$= $1 + 1;
	    }
2137 2138 2139 2140
	  }
	;

sp_cond:
unknown's avatar
unknown committed
2141
	  ulong_num
2142 2143 2144 2145 2146 2147 2148
	  {			/* 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 */
2149 2150 2151 2152 2153
	    if (!sp_cond_check(&$3))
	    {
	      my_error(ER_SP_BAD_SQLSTATE, MYF(0), $3.str);
	      YYABORT;
	    }
2154 2155
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
2156 2157
	    memcpy($$->sqlstate, $3.str, 5);
	    $$->sqlstate[5]= '\0';
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
	  }
	;

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
2176
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
2177 2178 2179 2180 2181 2182 2183 2184
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
2185
	| not FOUND_SYM		/* SQLSTATEs 02??? */
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
	  {
	    $$= (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
	  {
2200 2201
            /* NOTE: field definition is filled in sp_decl section. */

2202 2203 2204 2205 2206
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

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

2217 2218 2219 2220 2221
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$3, TRUE))
	    {
2222
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
	      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:
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
	  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:
2253 2254 2255
        IF { Lex->sphead->new_cont_backpatch(NULL); }
        sp_if END IF
        { Lex->sphead->do_cont_backpatch(); }
2256 2257 2258
        ;
        
sp_proc_stmt_statement:
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
	  {
	    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;

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

2288
              /* Extract the query statement from the tokenizer:
2289
                 The end is either lex->tok_end or tok->ptr. */
2290 2291 2292 2293 2294 2295 2296 2297
              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);
2298 2299 2300
            }
	    sp->restore_lex(YYTHD);
          }
2301 2302 2303 2304
        ;

sp_proc_stmt_return:
        RETURN_SYM 
2305 2306
          { Lex->sphead->reset_lex(YYTHD); }
          expr
2307 2308
	  {
	    LEX *lex= Lex;
2309
	    sp_head *sp= lex->sphead;
2310

2311
	    if (sp->m_type != TYPE_ENUM_FUNCTION)
2312
	    {
unknown's avatar
unknown committed
2313
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
2314 2315 2316 2317 2318 2319
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_freturn *i;

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

sp_proc_stmt_case_simple:
	CASE_SYM WHEN_SYM
2331
	  {
2332
	    Lex->sphead->m_flags&= ~sp_head::IN_SIMPLE_CASE;
2333
            Lex->sphead->new_cont_backpatch(NULL);
2334
	  }
2335
          sp_case END CASE_SYM { Lex->sphead->do_cont_backpatch(); }
2336 2337 2338 2339
        ;
        
sp_proc_stmt_case:
          CASE_SYM
2340 2341 2342 2343
          {
            Lex->sphead->reset_lex(YYTHD);
            Lex->sphead->new_cont_backpatch(NULL);
          }
2344
          expr WHEN_SYM
2345 2346
	  {
	    LEX *lex= Lex;
2347 2348 2349
	    sp_head *sp= lex->sphead;
	    sp_pcontext *parsing_ctx= lex->spcont;
	    int case_expr_id= parsing_ctx->register_case_expr();
2350
            sp_instr_set_case_expr *i;
2351 2352 2353
	    
	    if (parsing_ctx->push_case_expr_id(case_expr_id))
              YYABORT;
2354 2355 2356 2357 2358 2359 2360 2361

            i= new sp_instr_set_case_expr(sp->instructions(),
                                          parsing_ctx,
                                          case_expr_id,
                                          $3,
                                          lex);
            sp->add_cont_backpatch(i);
            sp->add_instr(i);
2362 2363
	    sp->m_flags|= sp_head::IN_SIMPLE_CASE;
	    sp->restore_lex(YYTHD);
2364 2365 2366
	  }
	  sp_case END CASE_SYM
	  {
2367
	    Lex->spcont->pop_case_expr_id();
2368
            Lex->sphead->do_cont_backpatch();
2369
	  }
2370 2371 2372 2373
        ;

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

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

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
2384 2385 2386 2387
        ;

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

	    if (! lab)
	    {
2396
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
2397 2398 2399 2400 2401
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_jump *i;
2402 2403 2404
	      uint ip= sp->instructions();
	      uint n;

2405
	      n= ctx->diff_handlers(lab->ctx, TRUE);  /* Exclusive the dest. */
2406 2407
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
2408
	      n= ctx->diff_cursors(lab->ctx, TRUE);  /* Exclusive the dest. */
2409 2410
	      if (n)
	        sp->add_instr(new sp_instr_cpop(ip++, ctx, n));
2411 2412 2413 2414 2415
	      i= new sp_instr_jump(ip, ctx);
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
	    }
	  }
2416 2417 2418 2419
        ;

sp_proc_stmt_iterate:
	  ITERATE_SYM label_ident
2420 2421 2422 2423 2424 2425 2426 2427
	  {
	    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)
	    {
2428
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "ITERATE", $2.str);
2429 2430 2431 2432 2433 2434 2435 2436
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_jump *i;
	      uint ip= sp->instructions();
	      uint n;

2437
	      n= ctx->diff_handlers(lab->ctx, FALSE);  /* Inclusive the dest. */
2438 2439
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
2440
	      n= ctx->diff_cursors(lab->ctx, FALSE);  /* Inclusive the dest. */
2441 2442 2443 2444 2445 2446
	      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);
	    }
	  }
2447 2448 2449 2450
        ;

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

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

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

	    if (sp->m_in_handler)
	    {
unknown's avatar
unknown committed
2492
	      my_message(ER_SP_GOTO_IN_HNDLR, ER(ER_SP_GOTO_IN_HNDLR), MYF(0));
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 2532
	      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);
	    }
2533 2534 2535 2536
#else
	    yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
#endif
2537
	  }
2538 2539 2540 2541
        ;

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

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

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

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

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

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2588
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
	      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)))
	    {
2612
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
	      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)))
	    {
2633
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $3.str);
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	    }
	  }
	;

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

	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
2658
            sp->add_cont_backpatch(i);
2659
            sp->add_instr(i);
2660
            sp->restore_lex(YYTHD);
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 2686 2687
	  }
	  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:
2688 2689
	  { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2690 2691 2692 2693 2694 2695 2696
	  {
            LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

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

2702 2703 2704 2705 2706
	      Item_case_expr *var;
              Item *expr;

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

unknown's avatar
unknown committed
2707
#ifndef DBUG_OFF
2708
              if (var)
2709
                var->m_sp= sp;
2710
#endif
2711 2712

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

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

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

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

2740 2741 2742 2743 2744 2745 2746
sp_whens:
	  /* Empty */
	  {
	    sp_head *sp= Lex->sphead;
	    uint ip= sp->instructions();
	    sp_instr_error *i= new sp_instr_error(ip, Lex->spcont,
						  ER_SP_CASE_NOT_FOUND);
unknown's avatar
unknown committed
2747

2748 2749 2750 2751 2752
	    sp->add_instr(i);
	  }
	| ELSE sp_proc_stmts1 {}
	| WHEN_SYM sp_case {}
	;
unknown's avatar
unknown committed
2753

2754
sp_labeled_control:
2755
	  label_ident ':'
unknown's avatar
unknown committed
2756
	  {
2757 2758 2759 2760 2761 2762
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);

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

2777 2778 2779 2780 2781 2782 2783
	    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)
	      {
2784
	        my_error(ER_SP_LABEL_MISMATCH, MYF(0), $5.str);
2785 2786 2787 2788
	        YYABORT;
	      }
	    }
	    lex->sphead->backpatch(lex->spcont->pop_label());
unknown's avatar
unknown committed
2789
	  }
2790 2791 2792
	;

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

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
2811
	  {
2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;

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

	    lex->sphead->add_instr(i);
unknown's avatar
unknown committed
2834
	  }
2835 2836 2837
        | WHILE_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr DO_SYM
unknown's avatar
unknown committed
2838
	  {
2839 2840 2841 2842
	    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,
2843
							       $3, lex);
2844 2845 2846

	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
2847
            sp->new_cont_backpatch(i);
2848
            sp->add_instr(i);
2849
            sp->restore_lex(YYTHD);
unknown's avatar
unknown committed
2850
	  }
2851
	  sp_proc_stmts1 END WHILE_SYM
unknown's avatar
unknown committed
2852
	  {
2853 2854 2855 2856 2857 2858
	    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);
2859
            lex->sphead->do_cont_backpatch();
2860
	  }
2861 2862 2863
        | REPEAT_SYM sp_proc_stmts1 UNTIL_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr END REPEAT_SYM
2864 2865 2866 2867 2868
	  {
	    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,
2869 2870
                                                               $5, lab->ip,
                                                               lex);
2871
            lex->sphead->add_instr(i);
2872
            lex->sphead->restore_lex(YYTHD);
2873 2874
            /* We can shortcut the cont_backpatch here */
            i->m_cont_dest= ip+1;
2875
	  }
2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891
	;

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; }
2892
          ;
unknown's avatar
unknown committed
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
/*
  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)
            {
unknown's avatar
merge  
unknown committed
3165
              my_error(ER_FILEGROUP_OPTION_ONLY_ONCE,MYF(0),"NODEGROUP");
unknown's avatar
unknown committed
3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176
              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)
            {
unknown's avatar
merge  
unknown committed
3177
              my_error(ER_FILEGROUP_OPTION_ONLY_ONCE,MYF(0),"COMMENT");
unknown's avatar
unknown committed
3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188
              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)
            {
unknown's avatar
merge  
unknown committed
3189
              my_error(ER_FILEGROUP_OPTION_ONLY_ONCE,MYF(0),
unknown's avatar
unknown committed
3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211
                       "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))
            {
unknown's avatar
merge  
unknown committed
3212
              my_error(ER_FILEGROUP_OPTION_ONLY_ONCE,MYF(0),"NO_WAIT");
unknown's avatar
unknown committed
3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226
              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 *start_ptr= $1.str;
            uint str_len= strlen(start_ptr);
3227
            char *end_ptr= start_ptr + str_len;
unknown's avatar
unknown committed
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 3262 3263 3264 3265 3266 3267 3268
            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
*/
unknown's avatar
unknown committed
3269 3270

create2:
unknown's avatar
unknown committed
3271
        '(' create2a {}
3272 3273 3274
        | opt_create_table_options
          opt_partitioning {} 
          create3 {}
unknown's avatar
unknown committed
3275 3276 3277
        | LIKE table_ident
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
3278
            if (!(lex->like_name= $2))
unknown's avatar
unknown committed
3279
              YYABORT;
unknown's avatar
unknown committed
3280 3281 3282 3283
          }
        | '(' LIKE table_ident ')'
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
3284
            if (!(lex->like_name= $3))
3285
              YYABORT;
unknown's avatar
unknown committed
3286
          }
unknown's avatar
unknown committed
3287
        ;
unknown's avatar
unknown committed
3288

unknown's avatar
unknown committed
3289
create2a:
3290 3291 3292 3293 3294 3295
        field_list ')' opt_create_table_options
          opt_partitioning {}
          create3 {}
        |  opt_partitioning {}
           create_select ')'
           { Select->set_braces(1);} union_opt {}
3296
        ;
unknown's avatar
unknown committed
3297 3298

create3:
3299
	/* empty */ {}
unknown's avatar
unknown committed
3300
	| opt_duplicate opt_as     create_select
unknown's avatar
unknown committed
3301
          { Select->set_braces(0);} union_clause {}
unknown's avatar
unknown committed
3302
	| opt_duplicate opt_as '(' create_select ')'
unknown's avatar
unknown committed
3303
          { Select->set_braces(1);} union_opt {}
3304 3305
        ;

3306 3307 3308
/*
 This part of the parser is about handling of the partition information.

3309
 It's first version was written by Mikael Ronström with lots of answers to
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336
 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
unknown's avatar
unknown committed
3337 3338 3339 3340 3341
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
unknown's avatar
unknown committed
3342
            mem_alloc_error(sizeof(partition_info));
unknown's avatar
unknown committed
3343 3344
            YYABORT;
          }
unknown's avatar
unknown committed
3345 3346 3347 3348
          if (lex->sql_command == SQLCOM_ALTER_TABLE)
          {
            lex->alter_info.flags|= ALTER_PARTITION;
          }
unknown's avatar
unknown committed
3349
        }
3350 3351 3352 3353 3354 3355 3356
        partition
        ;

partition_entry:
        PARTITION_SYM
        {
          LEX *lex= Lex;
unknown's avatar
unknown committed
3357
          if (!lex->part_info)
3358 3359 3360 3361
          {
            yyerror(ER(ER_PARTITION_ENTRY_ERROR));
            YYABORT;
          }
unknown's avatar
unknown committed
3362 3363 3364 3365
          /*
            We enter here when opening the frm file to translate
            partition info string into part_info data structure.
          */
3366
        }
unknown's avatar
unknown committed
3367 3368
        partition {}
        ;
3369 3370

partition:
unknown's avatar
unknown committed
3371 3372
        BY part_type_def opt_no_parts {} opt_sub_part {} part_defs
        ;
3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388

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; }
unknown's avatar
unknown committed
3389 3390
        part_func {}
        ;
3391 3392 3393 3394

opt_linear:
        /* empty */ {}
        | LINEAR_SYM
unknown's avatar
unknown committed
3395 3396
        { Lex->part_info->linear_hash_ind= TRUE;}
        ;
3397 3398

part_field_list:
unknown's avatar
unknown committed
3399 3400 3401 3402 3403
        /* empty */ {}
        | part_field_item_list {}
        ;

part_field_item_list:
3404
        part_field_item {}
unknown's avatar
unknown committed
3405
        | part_field_item_list ',' part_field_item {}
unknown's avatar
unknown committed
3406
        ;
3407 3408 3409 3410

part_field_item:
        ident
        {
unknown's avatar
unknown committed
3411 3412 3413 3414 3415
          if (Lex->part_info->part_field_list.push_back($1.str))
          {
            mem_alloc_error(1);
            YYABORT;
          }
unknown's avatar
unknown committed
3416 3417
        }
        ;
3418 3419 3420 3421 3422 3423 3424 3425

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;
3426
          lex->part_info->part_func_string= (char* ) sql_memdup($2+1, expr_len);
3427
          lex->part_info->part_func_len= expr_len;
unknown's avatar
unknown committed
3428 3429
        }
        ;
3430 3431 3432 3433 3434 3435 3436 3437

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;
3438
          lex->part_info->subpart_func_string= (char* ) sql_memdup($2+1, expr_len);        
3439
          lex->part_info->subpart_func_len= expr_len;
unknown's avatar
unknown committed
3440 3441
        }
        ;
3442 3443 3444 3445 3446 3447 3448


opt_no_parts:
        /* empty */ {}
        | PARTITIONS_SYM ulong_num 
        { 
          uint no_parts= $2;
unknown's avatar
unknown committed
3449
          LEX *lex= Lex;
3450 3451 3452 3453 3454
          if (no_parts == 0)
          {
            my_error(ER_NO_PARTS_ERROR, MYF(0), "partitions");
            YYABORT;
          }
unknown's avatar
unknown committed
3455 3456 3457

          lex->part_info->no_parts= no_parts;
          lex->part_info->use_default_no_partitions= FALSE;
unknown's avatar
unknown committed
3458 3459
        }
        ;
3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472

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;
        }
unknown's avatar
unknown committed
3473 3474
        opt_no_subparts {}
        ;
3475 3476 3477

sub_part_field_list:
        sub_part_field_item {}
unknown's avatar
unknown committed
3478 3479
        | sub_part_field_list ',' sub_part_field_item {}
        ;
3480 3481 3482

sub_part_field_item:
        ident
unknown's avatar
unknown committed
3483 3484 3485 3486 3487 3488 3489
        {
          if (Lex->part_info->subpart_field_list.push_back($1.str))
          {
            mem_alloc_error(1);
            YYABORT;
          }
        }
unknown's avatar
unknown committed
3490
        ;
3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505

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;
        }
unknown's avatar
unknown committed
3506
        ;
3507 3508 3509 3510 3511 3512

opt_no_subparts:
        /* empty */ {}
        | SUBPARTITIONS_SYM ulong_num
        {
          uint no_parts= $2;
unknown's avatar
unknown committed
3513
          LEX *lex= Lex;
3514 3515 3516 3517 3518
          if (no_parts == 0)
          {
            my_error(ER_NO_PARTS_ERROR, MYF(0), "subpartitions");
            YYABORT;
          }
unknown's avatar
unknown committed
3519 3520
          lex->part_info->no_subparts= no_parts;
          lex->part_info->use_default_no_subpartitions= FALSE;
unknown's avatar
unknown committed
3521 3522
        }
        ;
3523 3524 3525 3526 3527 3528 3529 3530

part_defs:
        /* empty */
        {}
        | '(' part_def_list ')'
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
unknown's avatar
unknown committed
3531
          uint count_curr_parts= part_info->partitions.elements;
3532 3533 3534
          if (part_info->no_parts != 0)
          {
            if (part_info->no_parts !=
unknown's avatar
unknown committed
3535
                count_curr_parts)
3536 3537 3538 3539 3540
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_PART_ERROR));
              YYABORT;
            }
          }
unknown's avatar
unknown committed
3541
          else if (count_curr_parts > 0)
3542
          {
unknown's avatar
unknown committed
3543
            part_info->no_parts= count_curr_parts;
3544 3545
          }
          part_info->count_curr_subparts= 0;
unknown's avatar
unknown committed
3546 3547
        }
        ;
3548 3549 3550

part_def_list:
        part_definition {}
unknown's avatar
unknown committed
3551 3552
        | part_def_list ',' part_definition {}
        ;
3553 3554 3555 3556 3557 3558 3559

part_definition:
        PARTITION_SYM
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          partition_element *p_elem= new partition_element();
unknown's avatar
unknown committed
3560 3561 3562 3563 3564 3565 3566 3567 3568
          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)
3569
          {
unknown's avatar
unknown committed
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 3620 3621 3622 3623 3624 3625 3626
            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;
3627
          }
unknown's avatar
unknown committed
3628
          p_elem->part_state= part_state;
3629 3630 3631
          part_info->curr_part_elem= p_elem;
          part_info->current_partition= p_elem;
          part_info->use_default_partitions= FALSE;
unknown's avatar
unknown committed
3632
          part_info->use_default_no_partitions= FALSE;
3633 3634 3635 3636
        }
        part_name {}
        opt_part_values {}
        opt_part_options {}
unknown's avatar
unknown committed
3637 3638
        opt_sub_partition {}
        ;
3639 3640 3641

part_name:
        ident_or_text
unknown's avatar
unknown committed
3642 3643 3644 3645 3646 3647
        {
          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;
        }
unknown's avatar
unknown committed
3648
        ;
3649 3650 3651 3652 3653

opt_part_values:
        /* empty */
        {
          LEX *lex= Lex;
unknown's avatar
unknown committed
3654
          if (!is_partition_management(lex))
3655
          {
unknown's avatar
unknown committed
3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667
            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;
            }
3668
          }
3669 3670
          else
            lex->part_info->part_type= HASH_PARTITION;
3671 3672 3673
        }
        | VALUES LESS_SYM THAN_SYM part_func_max
        {
unknown's avatar
unknown committed
3674 3675
          LEX *lex= Lex;
          if (!is_partition_management(lex))
3676
          {
unknown's avatar
unknown committed
3677 3678 3679 3680 3681 3682
            if (Lex->part_info->part_type != RANGE_PARTITION)
            {
              my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
              "RANGE", "LESS THAN");
              YYABORT;
            }
3683
          }
3684 3685
          else
            lex->part_info->part_type= RANGE_PARTITION;
3686 3687 3688
        }
        | VALUES IN_SYM '(' part_list_func ')'
        {
unknown's avatar
unknown committed
3689 3690
          LEX *lex= Lex;
          if (!is_partition_management(lex))
3691
          {
unknown's avatar
unknown committed
3692 3693 3694 3695 3696 3697
            if (Lex->part_info->part_type != LIST_PARTITION)
            {
              my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
              "LIST", "IN");
              YYABORT;
            }
3698
          }
3699 3700
          else
            lex->part_info->part_type= LIST_PARTITION;
unknown's avatar
unknown committed
3701 3702
        }
        ;
3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713

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;
unknown's avatar
unknown committed
3714
          lex->part_info->curr_part_elem->range_value= LONGLONG_MAX;
3715 3716 3717 3718 3719 3720 3721 3722
        }
        | part_range_func
        {
          if (Lex->part_info->defined_max_value)
          {
            yyerror(ER(ER_PARTITION_MAXVALUE_ERROR));
            YYABORT;
          }
unknown's avatar
unknown committed
3723 3724
        }
        ;
3725 3726 3727 3728

part_range_func:
        '(' part_bit_expr ')' 
        {
3729
          Lex->part_info->curr_part_elem->range_value= $2->value;
unknown's avatar
unknown committed
3730 3731
        }
        ;
3732 3733 3734

part_list_func:
        part_list_item {}
unknown's avatar
unknown committed
3735 3736
        | part_list_func ',' part_list_item {}
        ;
3737 3738 3739 3740

part_list_item:
        part_bit_expr
        {
3741 3742 3743 3744
          part_elem_value *value_ptr= $1;
          if (!value_ptr->null_value &&
             Lex->part_info->curr_part_elem->
              list_val_list.push_back((longlong*) &value_ptr->value))
unknown's avatar
unknown committed
3745
          {
3746
            mem_alloc_error(sizeof(part_elem_value));
unknown's avatar
unknown committed
3747 3748
            YYABORT;
          }
unknown's avatar
unknown committed
3749 3750
        }
        ;
3751 3752 3753 3754 3755 3756 3757

part_bit_expr:
        bit_expr
        {
          Item *part_expr= $1;
          bool not_corr_func;
          LEX *lex= Lex;
unknown's avatar
unknown committed
3758
          THD *thd= YYTHD;
unknown's avatar
unknown committed
3759
          longlong item_value;
3760 3761
          Name_resolution_context *context= &lex->current_select->context;
          TABLE_LIST *save_list= context->table_list;
unknown's avatar
unknown committed
3762
          const char *save_where= thd->where;
3763 3764

          context->table_list= 0;
unknown's avatar
unknown committed
3765
          thd->where= "partition function";
3766 3767 3768 3769 3770 3771 3772 3773 3774

          part_elem_value *value_ptr= 
            (part_elem_value*)sql_alloc(sizeof(part_elem_value));
          if (!value_ptr)
          {
            mem_alloc_error(sizeof(part_elem_value));
            YYABORT;
          }

unknown's avatar
unknown committed
3775 3776 3777 3778
          if (part_expr->fix_fields(YYTHD, (Item**)0) ||
              ((context->table_list= save_list), FALSE) ||
              (!part_expr->const_item()) ||
              (!lex->safe_to_cache_query))
3779 3780 3781 3782
          {
            yyerror(ER(ER_NO_CONST_EXPR_IN_RANGE_OR_LIST_ERROR));
            YYABORT;
          }
unknown's avatar
unknown committed
3783
          thd->where= save_where;
3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795
          value_ptr->value= part_expr->val_int();
          if ((value_ptr->null_value= part_expr->null_value))
          {
            if (Lex->part_info->curr_part_elem->has_null_value)
            {
              my_error(ER_MULTIPLE_DEF_CONST_IN_LIST_PART_ERROR, MYF(0));
              YYABORT;
            }
            Lex->part_info->curr_part_elem->has_null_value= TRUE;
          }
          else if (part_expr->result_type() != INT_RESULT &&
                   !part_expr->null_value)
unknown's avatar
unknown committed
3796 3797 3798 3799
          {
            yyerror(ER(ER_INCONSISTENT_TYPE_OF_FUNCTIONS_ERROR));
            YYABORT;
          }
3800
          $$= value_ptr; 
3801
        }
unknown's avatar
unknown committed
3802
        ;
3803 3804

opt_sub_partition:
3805 3806 3807 3808 3809 3810 3811 3812 3813
        /* empty */
        {
          if (Lex->part_info->no_subparts != 0 &&
              !Lex->part_info->use_default_subpartitions)
          {
            yyerror(ER(ER_PARTITION_WRONG_NO_SUBPART_ERROR));
            YYABORT;
          }
        }
3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828
        | '(' 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)
          {
3829 3830 3831 3832 3833
            if (part_info->partitions.elements > 1)
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_SUBPART_ERROR));
              YYABORT;
            }
3834 3835 3836
            part_info->no_subparts= part_info->count_curr_subparts;
          }
          part_info->count_curr_subparts= 0;
unknown's avatar
unknown committed
3837 3838
        }
        ;
3839 3840 3841

sub_part_list:
        sub_part_definition {}
unknown's avatar
unknown committed
3842 3843
        | sub_part_list ',' sub_part_definition {}
        ;
3844 3845 3846 3847 3848 3849 3850

sub_part_definition:
        SUBPARTITION_SYM
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          partition_element *p_elem= new partition_element();
unknown's avatar
unknown committed
3851 3852
          if (!p_elem ||
           part_info->current_partition->subpartitions.push_back(p_elem))
3853
          {
unknown's avatar
unknown committed
3854
            mem_alloc_error(sizeof(partition_element));
3855 3856 3857 3858
            YYABORT;
          }
          part_info->curr_part_elem= p_elem;
          part_info->use_default_subpartitions= FALSE;
unknown's avatar
unknown committed
3859
          part_info->use_default_no_subpartitions= FALSE;
3860 3861
          part_info->count_curr_subparts++;
        }
unknown's avatar
unknown committed
3862 3863
        sub_name opt_part_options {}
        ;
3864 3865 3866

sub_name:
        ident_or_text
unknown's avatar
unknown committed
3867 3868
        { Lex->part_info->curr_part_elem->partition_name= $1.str; }
        ;
3869 3870 3871

opt_part_options:
       /* empty */ {}
unknown's avatar
unknown committed
3872 3873
       | opt_part_option_list {}
       ;
3874 3875 3876

opt_part_option_list:
       opt_part_option_list opt_part_option {}
unknown's avatar
unknown committed
3877 3878
       | opt_part_option {}
       ;
3879 3880 3881 3882 3883

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
3884 3885 3886 3887 3888
        {
          LEX *lex= Lex;
          lex->part_info->curr_part_elem->engine_type= $4;
          lex->part_info->default_engine_type= $4;
        }
3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899
        | 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
unknown's avatar
unknown committed
3900 3901
        { Lex->part_info->curr_part_elem->part_comment= $3.str; }
        ;
3902 3903 3904 3905 3906

/*
 End of partition parser part
*/

unknown's avatar
unknown committed
3907
create_select:
3908
          SELECT_SYM
unknown's avatar
unknown committed
3909
          {
unknown's avatar
unknown committed
3910
	    LEX *lex=Lex;
3911
	    lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
3912 3913 3914 3915
	    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;
3916 3917 3918 3919
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
unknown's avatar
unknown committed
3920
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
unknown's avatar
unknown committed
3921
	    mysql_init_select(lex);
3922
	    lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
3923
          }
unknown's avatar
unknown committed
3924 3925
          select_options select_item_list
	  {
3926
	    Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
3927
	  }
unknown's avatar
unknown committed
3928
	  opt_select_from
3929 3930 3931 3932 3933 3934 3935
	  {
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
	    Lex->current_select->table_list.push_front(&Lex->save_list);
	  }
unknown's avatar
unknown committed
3936
        ;
unknown's avatar
unknown committed
3937

3938 3939
opt_as:
	/* empty */ {}
unknown's avatar
unknown committed
3940
	| AS	    {};
3941

3942 3943 3944 3945 3946 3947 3948 3949 3950
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
3951 3952
	default_collation   {}
	| default_charset   {};
3953

unknown's avatar
unknown committed
3954
opt_table_options:
unknown's avatar
unknown committed
3955
	/* empty */	 { $$= 0; }
unknown's avatar
unknown committed
3956
	| table_options  { $$= $1;};
unknown's avatar
unknown committed
3957 3958 3959

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

unknown's avatar
unknown committed
3962
table_option:
unknown's avatar
unknown committed
3963
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
unknown's avatar
unknown committed
3964 3965

opt_if_not_exists:
unknown's avatar
unknown committed
3966
	/* empty */	 { $$= 0; }
3967
	| IF not EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
unknown's avatar
unknown committed
3968 3969 3970

opt_create_table_options:
	/* empty */
unknown's avatar
unknown committed
3971
	| create_table_options;
unknown's avatar
unknown committed
3972

unknown's avatar
unknown committed
3973 3974 3975 3976
create_table_options_space_separated:
	create_table_option
	| create_table_option create_table_options_space_separated;

unknown's avatar
unknown committed
3977 3978
create_table_options:
	create_table_option
3979
	| create_table_option     create_table_options
unknown's avatar
unknown committed
3980
	| create_table_option ',' create_table_options;
unknown's avatar
unknown committed
3981 3982

create_table_option:
3983
	ENGINE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; }
3984 3985 3986 3987 3988 3989 3990
	| TYPE_SYM opt_equal storage_engines
          {
            Lex->create_info.db_type= $3;
            WARN_DEPRECATED(yythd, "5.2", "TYPE=storage_engine",
                            "'ENGINE=storage_engine'");
            Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE;
          }
3991 3992
	| 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;}
unknown's avatar
unknown committed
3993
	| 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;}
3994 3995
	| 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; }
3996
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017
        | 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;
          }
unknown's avatar
unknown committed
4018 4019
	| 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; }
4020
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
4021
	| UNION_SYM opt_equal '(' table_list ')'
4022 4023 4024
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
4025 4026
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
4027
	    lex->create_info.merge_list.elements--;
4028 4029
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
4030
	    lex->select_lex.table_list.elements=1;
4031 4032 4033
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
4034
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
4035
	  }
4036 4037
	| default_charset
	| default_collation
4038
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
4039 4040
	| DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.data_file_name= $4.str; Lex->create_info.used_fields|= HA_CREATE_USED_DATADIR; }
	| INDEX_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.index_file_name= $4.str;  Lex->create_info.used_fields|= HA_CREATE_USED_INDEXDIR; }
unknown's avatar
unknown committed
4041 4042 4043
        | 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;}
4044
	| 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; }
4045
        ;
unknown's avatar
unknown committed
4046

4047 4048 4049 4050 4051 4052 4053 4054
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))
          {
4055 4056 4057
            my_error(ER_CONFLICTING_DECLARATIONS, MYF(0),
                     "CHARACTER SET ", cinfo->default_table_charset->csname,
                     "CHARACTER SET ", $4->csname);
4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071
            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))
            {
4072 4073
              my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $4->name, cinfo->default_table_charset->csname);
4074 4075 4076 4077 4078 4079
              YYABORT;
            }
            Lex->create_info.default_table_charset= $4;
            Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

unknown's avatar
unknown committed
4080
storage_engines:
4081 4082
	ident_or_text
	{
unknown's avatar
unknown committed
4083 4084
	  $$ = ha_resolve_by_name(YYTHD, &$1);
	  if ($$ == NULL &&
4085 4086
	      test(YYTHD->variables.sql_mode & MODE_NO_ENGINE_SUBSTITUTION))
	  {
4087
	    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
4088 4089 4090
	    YYABORT;
	  }
	};
unknown's avatar
unknown committed
4091 4092 4093 4094 4095

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
4096 4097 4098
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; }
	| REDUNDANT_SYM	{ $$= ROW_TYPE_REDUNDANT; }
	| COMPACT_SYM	{ $$= ROW_TYPE_COMPACT; };
unknown's avatar
unknown committed
4099

4100 4101 4102
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
unknown's avatar
unknown committed
4103
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
4104

unknown's avatar
unknown committed
4105
opt_select_from:
4106
	opt_limit_clause {}
unknown's avatar
unknown committed
4107
	| select_from select_lock_type;
unknown's avatar
unknown committed
4108 4109

udf_func_type:
4110
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
unknown's avatar
unknown committed
4111
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
unknown's avatar
unknown committed
4112 4113 4114 4115

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
unknown's avatar
unknown committed
4116
        | DECIMAL_SYM {$$ = (int) DECIMAL_RESULT; }
unknown's avatar
unknown committed
4117
	| INT_SYM {$$ = (int) INT_RESULT; };
unknown's avatar
unknown committed
4118 4119 4120

field_list:
	  field_list_item
unknown's avatar
unknown committed
4121
	| field_list ',' field_list_item;
unknown's avatar
unknown committed
4122 4123 4124


field_list_item:
unknown's avatar
unknown committed
4125
	   column_def
4126 4127 4128 4129
         | key_def
         ;

column_def:
unknown's avatar
unknown committed
4130
	  field_spec opt_check_constraint
unknown's avatar
unknown committed
4131 4132 4133 4134
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
unknown's avatar
unknown committed
4135
	;
4136 4137

key_def:
4138
	key_type opt_ident key_alg '(' key_list ')' opt_fulltext_parser
unknown's avatar
unknown committed
4139
	  {
unknown's avatar
unknown committed
4140
	    LEX *lex=Lex;
4141 4142 4143 4144 4145 4146
	    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));
unknown's avatar
unknown committed
4147
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
4148
	  }
4149 4150 4151 4152
	| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
	  {
	    LEX *lex=Lex;
	    const char *key_name= $3 ? $3:$1;
4153 4154
	    lex->key_list.push_back(new Key($2, key_name, $4, 0,
				    lex->col_list));
4155 4156
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
4157
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
unknown's avatar
unknown committed
4158
	  {
4159
	    LEX *lex=Lex;
4160
	    lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
4161 4162 4163 4164 4165
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
4166 4167 4168
	    lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4 : $1,
					    HA_KEY_ALG_UNDEF, 1,
					    lex->col_list));
4169
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
4170
	  }
unknown's avatar
unknown committed
4171 4172 4173 4174
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
4175
	| opt_constraint check_constraint
unknown's avatar
unknown committed
4176 4177
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
4178 4179 4180
	  }
	;

4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194
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;
            }
          }
        ;

unknown's avatar
unknown committed
4195
opt_check_constraint:
4196
	/* empty */
unknown's avatar
unknown committed
4197 4198 4199 4200 4201
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
4202
	;
unknown's avatar
unknown committed
4203 4204

opt_constraint:
4205
	/* empty */		{ $$=(char*) 0; }
unknown's avatar
unknown committed
4206 4207 4208 4209 4210 4211
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
unknown's avatar
unknown committed
4212 4213 4214 4215

field_spec:
	field_ident
	 {
unknown's avatar
unknown committed
4216
	   LEX *lex=Lex;
4217
	   lex->length=lex->dec=0; lex->type=0;
4218
	   lex->default_value= lex->on_update_value= 0;
4219
           lex->comment=null_lex_str;
4220
	   lex->charset=NULL;
unknown's avatar
unknown committed
4221 4222 4223
	 }
	type opt_attribute
	{
unknown's avatar
unknown committed
4224
	  LEX *lex=Lex;
unknown's avatar
unknown committed
4225
	  if (add_field_to_list(lex->thd, $1.str,
unknown's avatar
unknown committed
4226
				(enum enum_field_types) $3,
unknown's avatar
unknown committed
4227
				lex->length,lex->dec,lex->type,
4228
				lex->default_value, lex->on_update_value, 
4229
                                &lex->comment,
4230
				lex->change,&lex->interval_list,lex->charset,
unknown's avatar
unknown committed
4231
				lex->uint_geom_type))
unknown's avatar
unknown committed
4232
	    YYABORT;
unknown's avatar
unknown committed
4233
	};
unknown's avatar
unknown committed
4234 4235

type:
4236
	int_type opt_len field_options	{ $$=$1; }
unknown's avatar
unknown committed
4237 4238
	| real_type opt_precision field_options { $$=$1; }
	| FLOAT_SYM float_options field_options { $$=FIELD_TYPE_FLOAT; }
unknown's avatar
unknown committed
4239 4240 4241 4242
	| BIT_SYM			{ Lex->length= (char*) "1";
					  $$=FIELD_TYPE_BIT; }
	| BIT_SYM '(' NUM ')'		{ Lex->length= $3.str;
					  $$=FIELD_TYPE_BIT; }
unknown's avatar
unknown committed
4243 4244
	| BOOL_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
4245 4246
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
4247
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
unknown's avatar
unknown committed
4248 4249 4250
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
4251
	| nchar '(' NUM ')' opt_bin_mod	{ Lex->length=$3.str;
unknown's avatar
unknown committed
4252
					  $$=FIELD_TYPE_STRING;
4253
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
4254
	| nchar opt_bin_mod		{ Lex->length=(char*) "1";
unknown's avatar
unknown committed
4255
					  $$=FIELD_TYPE_STRING;
4256
					  Lex->charset=national_charset_info; }
4257
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
4258
					  Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
4259
					  $$=FIELD_TYPE_STRING; }
4260 4261 4262
	| BINARY			{ Lex->length= (char*) "1";
					  Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
4263
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
4264
					  $$= MYSQL_TYPE_VARCHAR; }
unknown's avatar
unknown committed
4265
	| nvarchar '(' NUM ')' opt_bin_mod { Lex->length=$3.str;
4266
					  $$= MYSQL_TYPE_VARCHAR;
4267
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
4268
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
unknown's avatar
unknown committed
4269
					  Lex->charset=&my_charset_bin;
4270
					  $$= MYSQL_TYPE_VARCHAR; }
4271
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
unknown's avatar
unknown committed
4272 4273
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
4274
	| TIMESTAMP opt_len
4275
	  {
4276
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
4277 4278
	      $$=FIELD_TYPE_DATETIME;
	    else
4279 4280 4281 4282 4283
            {
              /* 
                Unlike other types TIMESTAMP fields are NOT NULL by default.
              */
              Lex->type|= NOT_NULL_FLAG;
4284
	      $$=FIELD_TYPE_TIMESTAMP;
4285
            }
4286
	   }
unknown's avatar
unknown committed
4287
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
unknown's avatar
unknown committed
4288
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
4289
					  $$=FIELD_TYPE_TINY_BLOB; }
unknown's avatar
unknown committed
4290
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
4291
					  $$=FIELD_TYPE_BLOB; }
unknown's avatar
unknown committed
4292 4293
	| spatial_type
          {
unknown's avatar
unknown committed
4294
#ifdef HAVE_SPATIAL
unknown's avatar
unknown committed
4295 4296 4297
            Lex->charset=&my_charset_bin;
            Lex->uint_geom_type= (uint)$1;
            $$=FIELD_TYPE_GEOMETRY;
unknown's avatar
unknown committed
4298
#else
unknown's avatar
unknown committed
4299
            my_error(ER_FEATURE_DISABLED, MYF(0),
4300
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
4301
            YYABORT;
unknown's avatar
unknown committed
4302
#endif
unknown's avatar
unknown committed
4303
          }
unknown's avatar
unknown committed
4304
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
4305
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
4306
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
4307
					  $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
4308
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
4309
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
4310 4311
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
4312
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
4313 4314
	| MEDIUMTEXT opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| LONGTEXT opt_binary		{ $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
4315
	| DECIMAL_SYM float_options field_options
unknown's avatar
unknown committed
4316
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
unknown's avatar
unknown committed
4317
	| NUMERIC_SYM float_options field_options
unknown's avatar
unknown committed
4318
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
4319
	| FIXED_SYM float_options field_options
unknown's avatar
unknown committed
4320
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
4321
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
4322
	  { $$=FIELD_TYPE_ENUM; }
4323
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
4324
	  { $$=FIELD_TYPE_SET; }
4325
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
4326 4327 4328 4329 4330 4331
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
4332
	;
unknown's avatar
unknown committed
4333

unknown's avatar
unknown committed
4334 4335 4336
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
4337 4338 4339
	| POINT_SYM           { Lex->length= (char*)"21";
                                $$= Field::GEOM_POINT;
                              }
unknown's avatar
unknown committed
4340 4341 4342 4343 4344 4345 4346
	| MULTIPOINT          { $$= Field::GEOM_MULTIPOINT; }
	| LINESTRING          { $$= Field::GEOM_LINESTRING; }
	| MULTILINESTRING     { $$= Field::GEOM_MULTILINESTRING; }
	| POLYGON             { $$= Field::GEOM_POLYGON; }
	| MULTIPOLYGON        { $$= Field::GEOM_MULTIPOLYGON; }
	;

unknown's avatar
unknown committed
4347 4348
char:
	CHAR_SYM {}
unknown's avatar
unknown committed
4349 4350 4351 4352 4353 4354
	;

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

unknown's avatar
unknown committed
4356 4357 4358
varchar:
	char VARYING {}
	| VARCHAR {}
unknown's avatar
unknown committed
4359 4360 4361 4362
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
unknown's avatar
unknown committed
4363
	| NVARCHAR_SYM {}
unknown's avatar
unknown committed
4364 4365 4366 4367
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
unknown's avatar
unknown committed
4368 4369 4370 4371 4372 4373

int_type:
	INT_SYM		{ $$=FIELD_TYPE_LONG; }
	| TINYINT	{ $$=FIELD_TYPE_TINY; }
	| SMALLINT	{ $$=FIELD_TYPE_SHORT; }
	| MEDIUMINT	{ $$=FIELD_TYPE_INT24; }
unknown's avatar
unknown committed
4374
	| BIGINT	{ $$=FIELD_TYPE_LONGLONG; };
unknown's avatar
unknown committed
4375 4376

real_type:
4377
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
unknown's avatar
unknown committed
4378 4379
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
unknown's avatar
unknown committed
4380
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
unknown's avatar
unknown committed
4381 4382 4383


float_options:
unknown's avatar
unknown committed
4384 4385
        /* empty */		{ Lex->dec=Lex->length= (char*)0; }
        | '(' NUM ')'		{ Lex->length=$2.str; Lex->dec= (char*)0; }
unknown's avatar
unknown committed
4386
	| precision		{};
unknown's avatar
unknown committed
4387 4388 4389 4390 4391 4392

precision:
	'(' NUM ',' NUM ')'
	{
	  LEX *lex=Lex;
	  lex->length=$2.str; lex->dec=$4.str;
unknown's avatar
unknown committed
4393
	};
unknown's avatar
unknown committed
4394 4395 4396

field_options:
	/* empty */		{}
unknown's avatar
unknown committed
4397
	| field_opt_list	{};
unknown's avatar
unknown committed
4398 4399 4400

field_opt_list:
	field_opt_list field_option {}
unknown's avatar
unknown committed
4401
	| field_option {};
unknown's avatar
unknown committed
4402 4403

field_option:
unknown's avatar
unknown committed
4404
	SIGNED_SYM	{}
4405
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
unknown's avatar
unknown committed
4406
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
unknown's avatar
unknown committed
4407 4408

opt_len:
4409 4410
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
unknown's avatar
unknown committed
4411 4412 4413

opt_precision:
	/* empty */	{}
unknown's avatar
unknown committed
4414
	| precision	{};
unknown's avatar
unknown committed
4415 4416 4417

opt_attribute:
	/* empty */ {}
unknown's avatar
unknown committed
4418
	| opt_attribute_list {};
unknown's avatar
unknown committed
4419 4420 4421

opt_attribute_list:
	opt_attribute_list attribute {}
unknown's avatar
unknown committed
4422
	| attribute;
unknown's avatar
unknown committed
4423 4424 4425

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
4426
	| not NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
4427 4428 4429
	| DEFAULT now_or_signed_literal { Lex->default_value=$2; }
	| ON UPDATE_SYM NOW_SYM optional_braces 
          { Lex->on_update_value= new Item_func_now_local(); }
unknown's avatar
unknown committed
4430
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
4431
	| SERIAL_SYM DEFAULT VALUE_SYM
4432 4433 4434
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
4435
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4436 4437 4438 4439 4440
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
4441
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4442 4443 4444 4445 4446
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
4447
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4448 4449 4450 4451 4452
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
4453
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4454
	  }
4455
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= $2; }
unknown's avatar
unknown committed
4456 4457
	| COLLATE_SYM collation_name
	  {
unknown's avatar
unknown committed
4458
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
unknown's avatar
unknown committed
4459
	    {
4460 4461
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $2->name,Lex->charset->csname);
unknown's avatar
unknown committed
4462 4463 4464 4465 4466 4467 4468 4469
	      YYABORT;
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
unknown's avatar
unknown committed
4470

4471 4472 4473 4474 4475
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

4476 4477 4478 4479
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
unknown's avatar
unknown committed
4480

4481
charset_name:
4482
	ident_or_text
4483
	{
unknown's avatar
unknown committed
4484
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
4485
	  {
4486
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
4487 4488
	    YYABORT;
	  }
unknown's avatar
unknown committed
4489 4490 4491
	}
	| BINARY { $$= &my_charset_bin; }
	;
4492

4493 4494 4495
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
unknown's avatar
unknown committed
4496

unknown's avatar
unknown committed
4497 4498 4499 4500 4501 4502 4503

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)))
	  {
4504
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
unknown's avatar
unknown committed
4505 4506 4507 4508 4509 4510 4511 4512 4513 4514
	    YYABORT;
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

4515
collation_name:
4516
	ident_or_text
4517 4518 4519
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
4520
	    my_error(ER_UNKNOWN_COLLATION, MYF(0), $1.str);
4521 4522 4523 4524
	    YYABORT;
	  }
	};

4525 4526
opt_collate:
	/* empty */	{ $$=NULL; }
unknown's avatar
unknown committed
4527
	| COLLATE_SYM collation_name_or_default { $$=$2; }
4528 4529
	;

4530 4531 4532 4533
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

4534 4535 4536 4537
opt_default:
	/* empty */	{}
	| DEFAULT	{};

unknown's avatar
unknown committed
4538
opt_binary:
4539
	/* empty */			{ Lex->charset=NULL; }
unknown's avatar
unknown committed
4540
	| ASCII_SYM opt_bin_mod		{ Lex->charset=&my_charset_latin1; }
unknown's avatar
unknown committed
4541
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
unknown's avatar
unknown committed
4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560
	| 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; }
unknown's avatar
unknown committed
4561 4562
	| UNICODE_SYM
	{
unknown's avatar
unknown committed
4563 4564
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
unknown's avatar
unknown committed
4565
	  {
unknown's avatar
unknown committed
4566
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
unknown's avatar
unknown committed
4567 4568 4569
	    YYABORT;
	  }
	}
4570
	| charset charset_name	{ Lex->charset=$2; } ;
unknown's avatar
unknown committed
4571

unknown's avatar
unknown committed
4572 4573 4574
opt_primary:
	/* empty */
	| PRIMARY_SYM
4575
	;
unknown's avatar
unknown committed
4576

unknown's avatar
unknown committed
4577
references:
4578 4579
	REFERENCES table_ident
	{
4580
	  LEX *lex=Lex;
4581 4582 4583 4584 4585 4586
	  lex->fk_delete_opt= lex->fk_update_opt= lex->fk_match_option= 0;
	  lex->ref_list.empty();
	}
	opt_ref_list
	{
	  $$=$2;
unknown's avatar
unknown committed
4587
	};
4588

4589
opt_ref_list:
unknown's avatar
unknown committed
4590
	/* empty */ opt_on_delete {}
unknown's avatar
unknown committed
4591
	| '(' ref_list ')' opt_on_delete {};
4592 4593 4594

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

unknown's avatar
unknown committed
4597 4598 4599

opt_on_delete:
	/* empty */ {}
unknown's avatar
unknown committed
4600
	| opt_on_delete_list {};
unknown's avatar
unknown committed
4601 4602 4603

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
unknown's avatar
unknown committed
4604
	| opt_on_delete_item {};
unknown's avatar
unknown committed
4605 4606

opt_on_delete_item:
4607 4608 4609 4610
	ON DELETE_SYM delete_option   { Lex->fk_delete_opt= $3; }
	| ON UPDATE_SYM delete_option { Lex->fk_update_opt= $3; }
	| MATCH FULL	{ Lex->fk_match_option= foreign_key::FK_MATCH_FULL; }
	| MATCH PARTIAL { Lex->fk_match_option= foreign_key::FK_MATCH_PARTIAL; }
unknown's avatar
unknown committed
4611
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
unknown's avatar
unknown committed
4612 4613

delete_option:
4614 4615 4616 4617
	RESTRICT	 { $$= (int) foreign_key::FK_OPTION_RESTRICT; }
	| CASCADE	 { $$= (int) foreign_key::FK_OPTION_CASCADE; }
	| SET NULL_SYM   { $$= (int) foreign_key::FK_OPTION_SET_NULL; }
	| NO_SYM ACTION  { $$= (int) foreign_key::FK_OPTION_NO_ACTION; }
unknown's avatar
unknown committed
4618
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
unknown's avatar
unknown committed
4619 4620

key_type:
4621
	key_or_index			    { $$= Key::MULTIPLE; }
unknown's avatar
SCRUM  
unknown committed
4622 4623
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
unknown's avatar
unknown committed
4624 4625 4626 4627
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
4628 4629
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
4630 4631 4632
	    YYABORT;
#endif
	  };
4633 4634 4635

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
unknown's avatar
SCRUM  
unknown committed
4636
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
unknown's avatar
unknown committed
4637 4638 4639

key_or_index:
	KEY_SYM {}
unknown's avatar
unknown committed
4640
	| INDEX_SYM {};
unknown's avatar
unknown committed
4641

unknown's avatar
SCRUM  
unknown committed
4642 4643 4644 4645 4646
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

unknown's avatar
unknown committed
4647 4648
keys_or_index:
	KEYS {}
unknown's avatar
unknown committed
4649
	| INDEX_SYM {}
unknown's avatar
unknown committed
4650
	| INDEXES {};
unknown's avatar
unknown committed
4651

4652
opt_unique_or_fulltext:
unknown's avatar
unknown committed
4653 4654
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
unknown's avatar
unknown committed
4655
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
unknown's avatar
unknown committed
4656 4657 4658 4659 4660
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
unknown's avatar
unknown committed
4661
            my_error(ER_FEATURE_DISABLED, MYF(0),
unknown's avatar
unknown committed
4662
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
4663 4664 4665
	    YYABORT;
#endif
	  }
unknown's avatar
unknown committed
4666
        ;
unknown's avatar
unknown committed
4667 4668

key_alg:
4669
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
4670
	| USING opt_btree_or_rtree { $$= $2; }
unknown's avatar
unknown committed
4671
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
unknown's avatar
unknown committed
4672 4673 4674

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
unknown's avatar
unknown committed
4675 4676 4677 4678
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
unknown's avatar
unknown committed
4679
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
unknown's avatar
unknown committed
4680 4681 4682

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
unknown's avatar
unknown committed
4683
	| key_part order_dir		{ Lex->col_list.push_back($1); };
unknown's avatar
unknown committed
4684 4685 4686

key_part:
	ident			{ $$=new key_part_spec($1.str); }
4687 4688 4689 4690 4691
	| ident '(' NUM ')'	
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
4692
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
4693 4694 4695
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
unknown's avatar
unknown committed
4696 4697 4698

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

4701
opt_component:
4702 4703
        /* empty */      { $$= null_lex_str; }
        | '.' ident      { $$= $2; };
4704

unknown's avatar
unknown committed
4705 4706
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
unknown's avatar
unknown committed
4707
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
unknown's avatar
unknown committed
4708 4709 4710 4711 4712 4713 4714 4715

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
4716
	  THD *thd= YYTHD;
4717
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
4718
         lex->name= 0;
4719 4720
	  lex->sql_command= SQLCOM_ALTER_TABLE;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
4721 4722
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
unknown's avatar
unknown committed
4723 4724 4725 4726
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
4727
          lex->select_lex.init_order();
unknown's avatar
unknown committed
4728
	  lex->select_lex.db=lex->name= 0;
4729
	  lex->like_name= 0;
4730
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
unknown's avatar
unknown committed
4731
	  lex->create_info.db_type= (handlerton*) &default_hton;
4732
	  lex->create_info.default_table_charset= NULL;
4733
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
4734
	  lex->alter_info.reset();
4735
	  lex->alter_info.flags= 0;
4736
          lex->no_write_to_binlog= 0;
unknown's avatar
unknown committed
4737
	}
4738
	alter_commands
4739
	{}
4740
	| ALTER DATABASE ident_or_empty
4741 4742 4743 4744 4745
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
          opt_create_database_options
4746 4747 4748
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
4749
	    lex->name= $3;
4750 4751 4752 4753 4754
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

4755 4756 4757 4758 4759
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
	      YYABORT;
	    }
4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771
	    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;
4772

4773 4774 4775 4776 4777
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
	      YYABORT;
	    }
4778 4779 4780 4781 4782 4783 4784 4785 4786
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    LEX *lex=Lex;

	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
	    lex->spname= $3;
	  }
4787 4788
        | ALTER view_algorithm_opt definer view_suid
          VIEW_SYM table_ident
4789 4790 4791 4792 4793 4794
	  {
	    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 */
4795
	    lex->select_lex.add_table_to_list(thd, $6, NULL, 0);
4796
	  }
4797
	  view_list_opt AS view_select view_check_option
4798
	  {}
4799
	| ALTER EVENT_SYM sp_name
4800 4801
          /*
             BE CAREFUL when you add a new rule to update the block where
4802 4803
             YYTHD->client_capabilities is set back to original value
          */
4804 4805
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
4806
            Event_timed *et;
4807 4808 4809

            if (lex->et)
            {
unknown's avatar
unknown committed
4810 4811 4812 4813
              /*
                Recursive events are not possible because recursive SPs
                are not also possible. lex->sp_head is not stacked.
              */
4814 4815 4816
              my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "EVENT");
              YYABORT;
            }
unknown's avatar
unknown committed
4817
            lex->spname= 0;//defensive programming
4818

4819
            if (!(et= new (YYTHD->mem_root) Event_timed()))// implicitly calls Event_timed::init()
unknown's avatar
unknown committed
4820
              YYABORT;
4821 4822
            lex->et = et;

4823 4824 4825 4826 4827
            if (!lex->et_compile_phase)
            {
              et->init_definer(YYTHD);
              et->init_name(YYTHD, $3);
            }
4828

4829 4830 4831 4832 4833
            /*
                We have to turn of CLIENT_MULTI_QUERIES while parsing a
                stored procedure, otherwise yylex will chop it into pieces
                at each ';'.
            */
unknown's avatar
unknown committed
4834
            $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
4835 4836
            YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
          }
4837 4838 4839 4840 4841
          ev_alter_on_schedule_completion
          opt_ev_rename_to
          opt_ev_status
          opt_ev_comment
          opt_ev_sql_stmt
4842
          {
4843 4844 4845 4846 4847 4848 4849 4850
            /*
              $1 - ALTER
              $2 - EVENT_SYM
              $3 - sp_name
              $4 - the block above
            */
            YYTHD->client_capabilities |= $<ulong_num>4;

unknown's avatar
unknown committed
4851 4852 4853 4854
            /*
              sql_command is set here because some rules in ev_sql_stmt
              can overwrite it
            */
4855
            if (!($5 || $6 || $7 || $8 || $9))
unknown's avatar
unknown committed
4856 4857 4858 4859
            {
	      yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
unknown's avatar
unknown committed
4860
            Lex->sql_command= SQLCOM_ALTER_EVENT;
unknown's avatar
unknown committed
4861
          }
unknown's avatar
unknown committed
4862 4863 4864 4865 4866
        | ALTER TABLESPACE alter_tablespace_info
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= ALTER_TABLESPACE;
          }
4867
        | ALTER LOGFILE_SYM GROUP alter_logfile_group_info
unknown's avatar
unknown committed
4868 4869 4870 4871 4872 4873 4874 4875 4876
          {
            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;
          }
4877
        | ALTER TABLESPACE change_tablespace_access
unknown's avatar
unknown committed
4878 4879 4880 4881
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= ALTER_ACCESS_MODE_TABLESPACE;
          }
4882
	;
4883

4884 4885 4886 4887
ev_alter_on_schedule_completion: /* empty */ { $$= 0;}
        | ON SCHEDULE_SYM ev_schedule_time { $$= 1; }
        | ev_on_completion { $$= 1; }
        | ON SCHEDULE_SYM ev_schedule_time ev_on_completion { $$= 1; }
4888
      ;
4889

4890
opt_ev_rename_to: /* empty */ { $$= 0;}
4891 4892 4893 4894
        | RENAME TO_SYM sp_name
          {
            LEX *lex=Lex;
            lex->spname= $3; //use lex's spname to hold the new name
unknown's avatar
unknown committed
4895
	                     //the original name is in the Event_timed object
4896
            $$= 1;
4897 4898
          }
      ;
4899 4900 4901

opt_ev_sql_stmt: /* empty*/ { $$= 0;}
        | DO_SYM ev_sql_stmt { $$= 1; }
4902
        ;
unknown's avatar
unknown committed
4903

4904

4905 4906 4907 4908
ident_or_empty:
	/* empty */  { $$= 0; }
	| ident      { $$= $1.str; };

4909
alter_commands:
4910 4911
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
4912
        | alter_list
unknown's avatar
unknown committed
4913
          opt_partitioning
unknown's avatar
unknown committed
4914 4915 4916
        | alter_list
          remove_partitioning
        | remove_partitioning
4917
        | partitioning
unknown's avatar
unknown committed
4918
/*
4919
  This part was added for release 5.1 by Mikael Ronström.
unknown's avatar
unknown committed
4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930
  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;
          }
unknown's avatar
unknown committed
4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972
        | 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
unknown's avatar
unknown committed
4973 4974 4975
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_COALESCE_PARTITION;
unknown's avatar
unknown committed
4976 4977
            lex->no_write_to_binlog= $3;
	    lex->alter_info.no_parts= $4;
unknown's avatar
unknown committed
4978 4979
          }
        | reorg_partition_rule
4980 4981
        ;

unknown's avatar
unknown committed
4982 4983 4984 4985 4986 4987 4988
remove_partitioning:
        REMOVE_SYM PARTITIONING_SYM
        {
          Lex->alter_info.flags|= ALTER_REMOVE_PARTITIONING;
        }
        ;

unknown's avatar
unknown committed
4989
all_or_alt_part_name_list:
4990
        ALL
unknown's avatar
unknown committed
4991 4992 4993 4994 4995 4996
        {
	  Lex->alter_info.flags|= ALTER_ALL_PARTITION;
        }
        | alt_part_name_list
        ;

unknown's avatar
unknown committed
4997
add_partition_rule:
unknown's avatar
unknown committed
4998
        ADD PARTITION_SYM opt_no_write_to_binlog
unknown's avatar
unknown committed
4999 5000 5001 5002 5003
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
unknown's avatar
unknown committed
5004
            mem_alloc_error(sizeof(partition_info));
unknown's avatar
unknown committed
5005 5006 5007
            YYABORT;
          }
	  lex->alter_info.flags|= ALTER_ADD_PARTITION;
unknown's avatar
unknown committed
5008
          lex->no_write_to_binlog= $3;
unknown's avatar
unknown committed
5009 5010 5011 5012 5013 5014 5015 5016 5017
        }
        add_part_extra
        {}
        ;

add_part_extra:
        | '(' part_def_list ')'
        {
          LEX *lex= Lex;
unknown's avatar
unknown committed
5018
          lex->part_info->no_parts= lex->part_info->partitions.elements;
unknown's avatar
unknown committed
5019 5020 5021 5022 5023 5024 5025 5026 5027
        }
        | PARTITIONS_SYM ulong_num
        {
          LEX *lex= Lex;
          lex->part_info->no_parts= $2;
        }
        ;

reorg_partition_rule:
unknown's avatar
unknown committed
5028
        REORGANIZE_SYM PARTITION_SYM opt_no_write_to_binlog
unknown's avatar
unknown committed
5029 5030 5031 5032 5033
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
unknown's avatar
unknown committed
5034
            mem_alloc_error(sizeof(partition_info));
unknown's avatar
unknown committed
5035 5036
            YYABORT;
          }
unknown's avatar
unknown committed
5037
          lex->no_write_to_binlog= $3;
unknown's avatar
unknown committed
5038
        }
unknown's avatar
unknown committed
5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052
        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 ')'
unknown's avatar
unknown committed
5053 5054
        {
          LEX *lex= Lex;
unknown's avatar
unknown committed
5055
          lex->part_info->no_parts= lex->part_info->partitions.elements;
unknown's avatar
unknown committed
5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066
        }
        ;

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

alt_part_name_item:
        ident
        {
unknown's avatar
unknown committed
5067 5068 5069 5070 5071
          if (Lex->alter_info.partition_names.push_back($1.str))
          {
            mem_alloc_error(1);
            YYABORT;
          }
unknown's avatar
unknown committed
5072 5073 5074 5075 5076 5077 5078
        }
        ;

/*
  End of management of partition commands
*/

5079 5080 5081 5082
alter_list:
        alter_list_item
	| alter_list ',' alter_list_item
        ;
unknown's avatar
unknown committed
5083 5084

add_column:
5085
	ADD opt_column
5086 5087
	{
	  LEX *lex=Lex;
5088 5089
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
5090
	};
unknown's avatar
unknown committed
5091 5092

alter_list_item:
5093 5094 5095 5096
	add_column column_def opt_place { }
	| ADD key_def
	  {
	    Lex->alter_info.flags|= ALTER_ADD_INDEX;
5097
	  }
5098 5099 5100 5101
	| add_column '(' field_list ')'
          {
	    Lex->alter_info.flags|= ALTER_ADD_COLUMN | ALTER_ADD_INDEX;
          }
unknown's avatar
unknown committed
5102 5103 5104
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
5105
	     lex->change= $3.str;
5106
	     lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
5107
	  }
5108
          field_spec opt_place
unknown's avatar
unknown committed
5109 5110 5111
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
5112
            lex->length=lex->dec=0; lex->type=0;
5113
            lex->default_value= lex->on_update_value= 0;
5114
            lex->comment=null_lex_str;
5115
	    lex->charset= NULL;
5116
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
5117 5118 5119 5120
          }
          type opt_attribute
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
5121
            if (add_field_to_list(lex->thd,$3.str,
unknown's avatar
unknown committed
5122 5123
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
5124
                                  lex->default_value, lex->on_update_value,
5125
                                  &lex->comment,
5126
				  $3.str, &lex->interval_list, lex->charset,
unknown's avatar
unknown committed
5127
				  lex->uint_geom_type))
unknown's avatar
unknown committed
5128 5129 5130
	       YYABORT;
          }
          opt_place
unknown's avatar
unknown committed
5131
	| DROP opt_column field_ident opt_restrict
unknown's avatar
unknown committed
5132 5133
	  {
	    LEX *lex=Lex;
5134
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
5135
                                                               $3.str));
5136
	    lex->alter_info.flags|= ALTER_DROP_COLUMN;
unknown's avatar
unknown committed
5137
	  }
5138 5139 5140 5141
	| DROP FOREIGN KEY_SYM opt_ident
          {
	    Lex->alter_info.flags|= ALTER_DROP_INDEX;
          }
unknown's avatar
unknown committed
5142 5143 5144
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
5145 5146 5147
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
				               primary_key_name));
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
unknown's avatar
unknown committed
5148
	  }
unknown's avatar
unknown committed
5149
	| DROP key_or_index field_ident
unknown's avatar
unknown committed
5150 5151
	  {
	    LEX *lex=Lex;
5152 5153 5154
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
					                       $3.str));
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
unknown's avatar
unknown committed
5155
	  }
5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167
	| DISABLE_SYM KEYS
          {
	    LEX *lex=Lex;
            lex->alter_info.keys_onoff= DISABLE;
	    lex->alter_info.flags|= ALTER_KEYS_ONOFF;
          }
	| ENABLE_SYM KEYS
          {
	    LEX *lex=Lex;
            lex->alter_info.keys_onoff= ENABLE;
	    lex->alter_info.flags|= ALTER_KEYS_ONOFF;
          }
unknown's avatar
unknown committed
5168
	| ALTER opt_column field_ident SET DEFAULT signed_literal
unknown's avatar
unknown committed
5169 5170
	  {
	    LEX *lex=Lex;
5171
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,$6));
5172
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
5173
	  }
unknown's avatar
unknown committed
5174
	| ALTER opt_column field_ident DROP DEFAULT
unknown's avatar
unknown committed
5175 5176
	  {
	    LEX *lex=Lex;
5177 5178
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,
                                                                  (Item*) 0));
5179
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
5180
	  }
5181
	| RENAME opt_to table_ident
5182
	  {
5183
	    LEX *lex=Lex;
5184
	    lex->select_lex.db=$3->db.str;
5185
	    lex->name= $3->table.str;
5186 5187 5188
            if (check_table_name($3->table.str,$3->table.length) ||
                $3->db.str && check_db_name($3->db.str))
            {
5189
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3->table.str);
5190 5191
              YYABORT;
            }
5192
	    lex->alter_info.flags|= ALTER_RENAME;
5193
	  }
5194 5195 5196 5197 5198 5199 5200 5201 5202 5203
	| 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))
	    {
5204 5205
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $5->name, $4->csname);
5206 5207
	      YYABORT;
	    }
5208
	    LEX *lex= Lex;
5209
	    lex->create_info.table_charset=
unknown's avatar
unknown committed
5210 5211 5212
	      lex->create_info.default_table_charset= $5;
	    lex->create_info.used_fields|= (HA_CREATE_USED_CHARSET |
					    HA_CREATE_USED_DEFAULT_CHARSET);
5213
	    lex->alter_info.flags|= ALTER_CONVERT;
5214
	  }
5215
        | create_table_options_space_separated
5216 5217
	  {
	    LEX *lex=Lex;
5218
	    lex->alter_info.flags|= ALTER_OPTIONS;
5219
	  }
5220 5221 5222 5223
	| FORCE_SYM
	  {
	    Lex->alter_info.flags|= ALTER_FORCE;
	   }
5224
	| order_clause
5225 5226
	  {
	    LEX *lex=Lex;
5227
	    lex->alter_info.flags|= ALTER_ORDER;
5228
	  };
unknown's avatar
unknown committed
5229 5230 5231

opt_column:
	/* empty */	{}
unknown's avatar
unknown committed
5232
	| COLUMN_SYM	{};
unknown's avatar
unknown committed
5233 5234

opt_ignore:
5235 5236 5237
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
unknown's avatar
unknown committed
5238 5239

opt_restrict:
5240 5241 5242 5243
	/* empty */	{ Lex->drop_mode= DROP_DEFAULT; }
	| RESTRICT	{ Lex->drop_mode= DROP_RESTRICT; }
	| CASCADE	{ Lex->drop_mode= DROP_CASCADE; }
	;
unknown's avatar
unknown committed
5244 5245 5246 5247

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
unknown's avatar
unknown committed
5248
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
unknown's avatar
unknown committed
5249 5250 5251 5252

opt_to:
	/* empty */	{}
	| TO_SYM	{}
5253
	| EQ		{}
unknown's avatar
unknown committed
5254
	| AS		{};
unknown's avatar
unknown committed
5255

unknown's avatar
unknown committed
5256
/*
5257
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
unknown's avatar
unknown committed
5258 5259
*/

unknown's avatar
unknown committed
5260
slave:
5261
	  START_SYM SLAVE slave_thread_opts
5262 5263 5264 5265 5266 5267
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_START;
	    lex->type = 0;
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
unknown's avatar
unknown committed
5268
            /* If you change this code don't forget to update SLAVE START too */
5269 5270 5271
          }
          slave_until
          {}
unknown's avatar
unknown committed
5272 5273 5274 5275 5276
        | STOP_SYM SLAVE slave_thread_opts
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_STOP;
	    lex->type = 0;
unknown's avatar
unknown committed
5277
            /* If you change this code don't forget to update SLAVE STOP too */
unknown's avatar
unknown committed
5278
          }
5279 5280 5281 5282 5283
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
5284 5285 5286 5287 5288
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
5289 5290 5291 5292 5293 5294 5295 5296
	| SLAVE STOP_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_STOP;
	   lex->type = 0;
         }
        ;

unknown's avatar
unknown committed
5297

unknown's avatar
unknown committed
5298
start:
5299 5300
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
5301 5302 5303
          LEX *lex= Lex;
          lex->sql_command= SQLCOM_BEGIN;
          lex->start_transaction_opt= $3;
5304
        }
unknown's avatar
unknown committed
5305 5306
	;

5307 5308 5309 5310 5311 5312
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
5313
        ;
5314

unknown's avatar
unknown committed
5315
slave_thread_opts:
5316 5317
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
5318
        {}
unknown's avatar
unknown committed
5319
	;
5320 5321

slave_thread_opt_list:
unknown's avatar
unknown committed
5322
	slave_thread_opt
5323 5324
	| slave_thread_opt_list ',' slave_thread_opt
	;
5325 5326

slave_thread_opt:
5327
	/*empty*/	{}
unknown's avatar
unknown committed
5328
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
unknown's avatar
unknown committed
5329
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
unknown's avatar
unknown committed
5330
	;
5331

5332 5333 5334 5335 5336 5337 5338 5339 5340 5341
slave_until:
	/*empty*/	{}
	| UNTIL_SYM slave_until_opts
          {
            LEX *lex=Lex;
            if ((lex->mi.log_file_name || lex->mi.pos) &&
                (lex->mi.relay_log_name || lex->mi.relay_log_pos) ||
                !((lex->mi.log_file_name && lex->mi.pos) ||
                  (lex->mi.relay_log_name && lex->mi.relay_log_pos)))
            {
unknown's avatar
unknown committed
5342 5343
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


unknown's avatar
unknown committed
5355 5356 5357 5358
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
5359 5360
           WARN_DEPRECATED(yythd, "5.2", "RESTORE TABLE",
                           "MySQL Administrator (mysqldump, mysql)");
unknown's avatar
unknown committed
5361
	}
unknown's avatar
unknown committed
5362
	table_list FROM TEXT_STRING_sys
unknown's avatar
unknown committed
5363 5364
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
5365
        };
unknown's avatar
unknown committed
5366

unknown's avatar
unknown committed
5367 5368 5369 5370
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
5371 5372
           WARN_DEPRECATED(yythd, "5.2", "BACKUP TABLE",
                           "MySQL Administrator (mysqldump, mysql)");
unknown's avatar
unknown committed
5373
	}
unknown's avatar
unknown committed
5374
	table_list TO_SYM TEXT_STRING_sys
unknown's avatar
unknown committed
5375 5376
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
5377
        };
unknown's avatar
unknown committed
5378

5379 5380 5381 5382 5383 5384
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
5385 5386
	table_list opt_checksum_type
        {}
5387 5388
	;

5389 5390 5391 5392 5393 5394
opt_checksum_type:
        /* nothing */  { Lex->check_opt.flags= 0; }
	| QUICK        { Lex->check_opt.flags= T_QUICK; }
	| EXTENDED_SYM { Lex->check_opt.flags= T_EXTEND; }
        ;

unknown's avatar
unknown committed
5395
repair:
5396
	REPAIR opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
5397
	{
unknown's avatar
unknown committed
5398 5399
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
5400
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
5401
	   lex->check_opt.init();
unknown's avatar
unknown committed
5402
	}
5403 5404 5405
	table_list opt_mi_repair_type
	{}
	;
unknown's avatar
unknown committed
5406

unknown's avatar
unknown committed
5407
opt_mi_repair_type:
unknown's avatar
unknown committed
5408
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
5409
	| mi_repair_types {};
unknown's avatar
unknown committed
5410

unknown's avatar
unknown committed
5411 5412
mi_repair_types:
	mi_repair_type {}
unknown's avatar
unknown committed
5413
	| mi_repair_type mi_repair_types {};
5414

unknown's avatar
unknown committed
5415
mi_repair_type:
unknown's avatar
unknown committed
5416
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
5417
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
unknown's avatar
unknown committed
5418
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
unknown's avatar
unknown committed
5419 5420

analyze:
5421
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
5422
	{
unknown's avatar
unknown committed
5423 5424
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
5425
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
5426
	   lex->check_opt.init();
unknown's avatar
unknown committed
5427
	}
5428 5429 5430
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
5431

5432 5433 5434 5435 5436 5437
binlog_base64_event:
        BINLOG_SYM TEXT_STRING_sys
        {
           Lex->sql_command = SQLCOM_BINLOG_BASE64_EVENT;
           Lex->comment= $2;
        }
5438
        ;
5439

unknown's avatar
unknown committed
5440
check:
unknown's avatar
unknown committed
5441
	CHECK_SYM table_or_tables
unknown's avatar
unknown committed
5442
	{
5443 5444 5445 5446 5447 5448 5449 5450 5451
	  LEX *lex=Lex;

	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "CHECK");
	    YYABORT;
	  }
	  lex->sql_command = SQLCOM_CHECK;
	  lex->check_opt.init();
unknown's avatar
unknown committed
5452
	}
5453 5454 5455
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
5456

unknown's avatar
unknown committed
5457 5458
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
5459
	| mi_check_types {};
unknown's avatar
unknown committed
5460 5461 5462

mi_check_types:
	mi_check_type {}
unknown's avatar
unknown committed
5463
	| mi_check_type mi_check_types {};
unknown's avatar
unknown committed
5464 5465 5466 5467 5468 5469

mi_check_type:
	QUICK      { Lex->check_opt.flags|= T_QUICK; }
	| FAST_SYM { Lex->check_opt.flags|= T_FAST; }
	| MEDIUM_SYM { Lex->check_opt.flags|= T_MEDIUM; }
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
unknown's avatar
unknown committed
5470 5471
	| CHANGED  { Lex->check_opt.flags|= T_CHECK_ONLY_CHANGED; }
        | FOR_SYM UPGRADE_SYM { Lex->check_opt.sql_flags|= TT_FOR_UPGRADE; };
unknown's avatar
unknown committed
5472

unknown's avatar
unknown committed
5473
optimize:
5474
	OPTIMIZE opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
5475
	{
unknown's avatar
unknown committed
5476 5477
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
unknown's avatar
unknown committed
5478
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
5479
	   lex->check_opt.init();
unknown's avatar
unknown committed
5480
	}
5481 5482 5483
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
5484

5485 5486 5487
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
5488
	| LOCAL_SYM  { $$= 1; }
5489 5490
	;

5491 5492 5493
rename:
	RENAME table_or_tables
	{
5494
          Lex->sql_command= SQLCOM_RENAME_TABLE;
5495
	}
5496 5497
	table_to_table_list
	{}
unknown's avatar
unknown committed
5498 5499 5500 5501 5502 5503 5504
	| RENAME DATABASE
          {
            Lex->db_list.empty();
            Lex->sql_command= SQLCOM_RENAME_DB;
          }
          db_to_db
          {}
5505 5506 5507 5508
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
5509
	;
5510

5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523
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;
          }
        ;

5524 5525
table_to_table_list:
	table_to_table
unknown's avatar
unknown committed
5526
	| table_to_table_list ',' table_to_table;
5527 5528 5529

table_to_table:
	table_ident TO_SYM table_ident
5530
	{
unknown's avatar
unknown committed
5531
	  LEX *lex=Lex;
unknown's avatar
unknown committed
5532
	  SELECT_LEX *sl= lex->current_select;
unknown's avatar
unknown committed
5533 5534 5535 5536
	  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))
5537
	    YYABORT;
5538
	};
5539

unknown's avatar
unknown committed
5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550
db_to_db:
	ident TO_SYM ident
	{
	  LEX *lex=Lex;
          if (Lex->db_list.push_back((LEX_STRING*)
                                     sql_memdup(&$1, sizeof(LEX_STRING))) ||
              Lex->db_list.push_back((LEX_STRING*)
                                     sql_memdup(&$3, sizeof(LEX_STRING))))
              YYABORT;
	};

unknown's avatar
unknown committed
5551
keycache:
unknown's avatar
unknown committed
5552
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
unknown's avatar
unknown committed
5553 5554
        {
          LEX *lex=Lex;
5555
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
5556
	  lex->ident= $5;
unknown's avatar
unknown committed
5557 5558 5559 5560 5561 5562 5563 5564
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
5565
        table_ident cache_keys_spec
unknown's avatar
unknown committed
5566 5567 5568 5569 5570 5571
        {
          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(),
5572
                                      (List<String> *)0))
unknown's avatar
unknown committed
5573 5574 5575 5576
            YYABORT;
        }
        ;

5577 5578 5579
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
5580
	;
5581

unknown's avatar
unknown committed
5582
preload:
unknown's avatar
unknown committed
5583
	LOAD INDEX_SYM INTO CACHE_SYM
unknown's avatar
unknown committed
5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596
	{
	  LEX *lex=Lex;
	  lex->sql_command=SQLCOM_PRELOAD_KEYS;
	}
	preload_list
	{}
	;

preload_list:
	preload_keys
	| preload_list ',' preload_keys;

preload_keys:
unknown's avatar
unknown committed
5597
	table_ident cache_keys_spec opt_ignore_leaves
unknown's avatar
unknown committed
5598 5599 5600
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
unknown's avatar
unknown committed
5601 5602
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
unknown's avatar
unknown committed
5603 5604 5605 5606 5607 5608
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

5609
cache_keys_spec:
5610
        { Select->interval_list.empty(); }
5611 5612 5613 5614 5615 5616 5617
        cache_key_list_or_empty
        {
          LEX *lex=Lex;
          SELECT_LEX *sel= &lex->select_lex;
          sel->use_index= sel->interval_list;
        }
        ;
unknown's avatar
unknown committed
5618

unknown's avatar
unknown committed
5619
cache_key_list_or_empty:
5620
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
5621
	| opt_key_or_index '(' key_usage_list2 ')'
5622 5623 5624 5625
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
5626 5627
	;

unknown's avatar
unknown committed
5628
opt_ignore_leaves:
unknown's avatar
unknown committed
5629 5630 5631 5632 5633
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

unknown's avatar
unknown committed
5634
/*
unknown's avatar
unknown committed
5635
  Select : retrieve data from table
unknown's avatar
unknown committed
5636 5637 5638 5639
*/


select:
5640 5641 5642 5643
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
unknown's avatar
unknown committed
5644 5645
	}
	;
unknown's avatar
unknown committed
5646

5647
/* Need select_init2 for subselects. */
unknown's avatar
unknown committed
5648
select_init:
5649
	SELECT_SYM select_init2
5650
	|
unknown's avatar
unknown committed
5651 5652 5653 5654
	'(' select_paren ')' union_opt;

select_paren:
	SELECT_SYM select_part2
5655
	  {
5656
	    LEX *lex= Lex;
unknown's avatar
unknown committed
5657
            SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
5658
	    if (sel->set_braces(1))
5659
	    {
5660
	      yyerror(ER(ER_SYNTAX_ERROR));
5661 5662
	      YYABORT;
	    }
5663 5664
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
5665
	  {
5666
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
5667 5668
	    YYABORT;
	  }
5669
            /* select in braces, can't contain global parameters */
unknown's avatar
unknown committed
5670 5671 5672
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
unknown's avatar
unknown committed
5673 5674
          }
	| '(' select_paren ')';
5675

5676 5677
select_init2:
	select_part2
5678
        {
5679
	  LEX *lex= Lex;
unknown's avatar
unknown committed
5680
          SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
5681
          if (lex->current_select->set_braces(0))
5682
	  {
5683
	    yyerror(ER(ER_SYNTAX_ERROR));
5684 5685
	    YYABORT;
	  }
5686 5687
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
5688
	  {
5689
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
5690 5691
	    YYABORT;
	  }
5692
	}
5693
	union_clause
5694 5695
	;

5696
select_part2:
unknown's avatar
unknown committed
5697
	{
unknown's avatar
unknown committed
5698 5699
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
5700 5701
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
5702
	  lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
5703 5704 5705
	}
	select_options select_item_list
	{
5706
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
5707
	}
unknown's avatar
unknown committed
5708
	select_into select_lock_type;
5709

unknown's avatar
unknown committed
5710
select_into:
unknown's avatar
unknown committed
5711
	opt_order_clause opt_limit_clause {}
5712
        | into
unknown's avatar
unknown committed
5713
	| select_from
5714 5715
	| into select_from
	| select_from into;
unknown's avatar
unknown committed
5716 5717

select_from:
5718 5719
	  FROM join_table_list where_clause group_clause having_clause
	       opt_order_clause opt_limit_clause procedure_clause
unknown's avatar
unknown committed
5720
        | FROM DUAL_SYM where_clause opt_limit_clause
unknown's avatar
unknown committed
5721 5722 5723 5724
          /* 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 :) */
5725
	;
unknown's avatar
unknown committed
5726 5727 5728

select_options:
	/* empty*/
unknown's avatar
unknown committed
5729 5730
	| select_option_list
	  {
unknown's avatar
unknown committed
5731
	    if (Select->options & SELECT_DISTINCT && Select->options & SELECT_ALL)
unknown's avatar
unknown committed
5732
	    {
unknown's avatar
unknown committed
5733
	      my_error(ER_WRONG_USAGE, MYF(0), "ALL", "DISTINCT");
unknown's avatar
unknown committed
5734 5735 5736
              YYABORT;
	    }
          }
unknown's avatar
unknown committed
5737
	  ;
unknown's avatar
unknown committed
5738 5739 5740

select_option_list:
	select_option_list select_option
unknown's avatar
unknown committed
5741
	| select_option;
unknown's avatar
unknown committed
5742 5743

select_option:
5744
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
5745 5746 5747 5748 5749 5750
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
unknown's avatar
unknown committed
5751
	| DISTINCT         { Select->options|= SELECT_DISTINCT; }
5752 5753
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765
	| 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;
	  }
5766
	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
5767 5768 5769 5770
	| SQL_CACHE_SYM
	  {
	    Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
	  }
unknown's avatar
unknown committed
5771
	| ALL		    { Select->options|= SELECT_ALL; }
5772
	;
unknown's avatar
unknown committed
5773

unknown's avatar
unknown committed
5774 5775 5776
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
5777 5778
	  {
	    LEX *lex=Lex;
5779
	    lex->current_select->set_lock_for_tables(TL_WRITE);
5780
	    lex->safe_to_cache_query=0;
5781
	  }
unknown's avatar
unknown committed
5782
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
5783 5784
	  {
	    LEX *lex=Lex;
5785 5786
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
5787
	    lex->safe_to_cache_query=0;
5788 5789
	  }
	;
unknown's avatar
unknown committed
5790

unknown's avatar
unknown committed
5791 5792 5793 5794 5795
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
5796
	    THD *thd= YYTHD;
5797 5798 5799 5800
	    if (add_item_to_list(thd,
                                 new Item_field(&thd->lex->current_select->
                                                context,
                                                NULL, NULL, "*")))
unknown's avatar
unknown committed
5801
	      YYABORT;
5802
	    (thd->lex->current_select->with_wild)++;
unknown's avatar
unknown committed
5803
	  };
unknown's avatar
unknown committed
5804 5805 5806 5807 5808


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
unknown's avatar
unknown committed
5809
	    if (add_item_to_list(YYTHD, $2))
unknown's avatar
unknown committed
5810 5811
	      YYABORT;
	    if ($4.str)
5812 5813 5814 5815
            {
	      $2->set_name($4.str, $4.length, system_charset_info);
              $2->is_autogenerated_name= FALSE;
            }
5816 5817 5818 5819 5820 5821
	    else if (!$2->name) {
	      char *str = $1;
	      if (str[-1] == '`')
	        str--;
	      $2->set_name(str,(uint) ($3 - str), YYTHD->charset());
	    }
unknown's avatar
unknown committed
5822
	  };
unknown's avatar
unknown committed
5823 5824

remember_name:
unknown's avatar
unknown committed
5825
	{ $$=(char*) Lex->tok_start; };
unknown's avatar
unknown committed
5826 5827

remember_end:
unknown's avatar
unknown committed
5828
	{ $$=(char*) Lex->tok_end; };
unknown's avatar
unknown committed
5829 5830 5831

select_item2:
	table_wild	{ $$=$1; } /* table.* */
unknown's avatar
unknown committed
5832
	| expr		{ $$=$1; };
unknown's avatar
unknown committed
5833 5834

select_alias:
5835
	/* empty */		{ $$=null_lex_str;}
unknown's avatar
unknown committed
5836 5837 5838 5839 5840
	| AS ident		{ $$=$2; }
	| AS TEXT_STRING_sys	{ $$=$2; }
	| ident			{ $$=$1; }
	| TEXT_STRING_sys	{ $$=$1; }
	;
unknown's avatar
unknown committed
5841 5842 5843

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

unknown's avatar
unknown committed
5846
/* all possible expressions */
unknown's avatar
SCRUM  
unknown committed
5847
expr:	
unknown's avatar
unknown committed
5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869
	  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); }
        ;
5870 5871

bool_term:
unknown's avatar
unknown committed
5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894
	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); }
        ;
5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911

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); }
5912 5913 5914 5915 5916
	| 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); }
5917 5918 5919 5920
	| predicate ;

predicate:
	 bit_expr IN_SYM '(' expr_list ')'
unknown's avatar
unknown committed
5921 5922 5923 5924 5925 5926 5927 5928 5929
	  { 
            if ($4->elements == 1)
              $$= new Item_func_eq($1, $4->head());
            else
            {
              $4->push_front($1);
              $$= new Item_func_in(*$4);
            }
          }
5930
	| bit_expr not IN_SYM '(' expr_list ')'
unknown's avatar
unknown committed
5931 5932 5933 5934 5935 5936
          {
            if ($5->elements == 1)
              $$= new Item_func_ne($1, $5->head());
            else
            {
              $5->push_front($1);
5937 5938 5939
              Item_func_in *item = new Item_func_in(*$5);
              item->negate();
              $$= item;
unknown's avatar
unknown committed
5940 5941
            }            
          }
5942 5943 5944
        | bit_expr IN_SYM in_subselect
	  { $$= new Item_in_subselect($1, $3); }
	| bit_expr not IN_SYM in_subselect
5945
          { $$= negate_expression(YYTHD, new Item_in_subselect($1, $4)); }
5946 5947 5948
	| 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
5949 5950 5951 5952 5953
    {
      Item_func_between *item= new Item_func_between($1,$4,$6);
      item->negate();
      $$= item;
    }
5954 5955 5956 5957
	| 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
5958
          { $$= new Item_func_like($1,$3,$4,Lex->escape_used); }
5959
	| bit_expr not LIKE simple_expr opt_escape
5960
          { $$= new Item_func_not(new Item_func_like($1,$4,$5, Lex->escape_used)); }
5961 5962
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
5963
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005
	| bit_expr ;

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

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

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

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

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

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

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

unknown's avatar
unknown committed
6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018
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; }
        ;

6019
interval_expr:
unknown's avatar
unknown committed
6020
         INTERVAL_SYM expr { $$=$2; }
6021 6022
        ;

unknown's avatar
unknown committed
6023 6024
simple_expr:
	simple_ident
6025
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
unknown's avatar
unknown committed
6026
	  {
unknown's avatar
unknown committed
6027 6028 6029
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
6030
                                                            YYTHD->charset()));
6031
	  }
unknown's avatar
unknown committed
6032
	| literal
unknown's avatar
unknown committed
6033
	| param_marker
unknown's avatar
unknown committed
6034
	| '@' ident_or_text SET_VAR expr
unknown's avatar
unknown committed
6035 6036
	  {
	    $$= new Item_func_set_user_var($2,$4);
6037 6038 6039
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
6040
	  }
6041
	| '@' ident_or_text
unknown's avatar
unknown committed
6042 6043
	  {
	    $$= new Item_func_get_user_var($2);
6044 6045 6046
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
6047
	  }
6048
	| '@' '@' opt_var_ident_type ident_or_text opt_component
unknown's avatar
unknown committed
6049
	  {
6050 6051 6052 6053 6054 6055

            if ($4.str && $5.str && check_reserved_words(&$4))
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
6056
	    if (!($$= get_system_var(YYTHD, $3, $4, $5)))
unknown's avatar
unknown committed
6057
	      YYABORT;
6058
	    Lex->variables_used= 1;
unknown's avatar
unknown committed
6059
	  }
unknown's avatar
unknown committed
6060
	| sum_expr
6061 6062 6063 6064 6065 6066
	| simple_expr OR_OR_SYM simple_expr
	  { $$= new Item_func_concat($1, $3); }
	| '+' simple_expr %prec NEG	{ $$= $2; }
	| '-' simple_expr %prec NEG	{ $$= new Item_func_neg($2); }
	| '~' simple_expr %prec NEG	{ $$= new Item_func_bit_neg($2); }
	| not2 simple_expr %prec NEG	{ $$= negate_expression(YYTHD, $2); }
unknown's avatar
unknown committed
6067
	| '(' expr ')'		{ $$= $2; }
6068 6069 6070 6071 6072
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
6073
	| ROW_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
6074
	  {
6075 6076
	    $5->push_front($3);
	    $$= new Item_row(*$5);
unknown's avatar
unknown committed
6077
	  }
unknown's avatar
unknown committed
6078
	| EXISTS exists_subselect { $$= $2; }
unknown's avatar
unknown committed
6079
	| singlerow_subselect   { $$= $1; }
unknown's avatar
unknown committed
6080
	| '{' ident expr '}'	{ $$= $3; }
6081
        | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
6082
          { $2->push_front($5);
6083 6084
            Select->add_ftfunc_to_list((Item_func_match*)
                                        ($$=new Item_func_match(*$2,$6))); }
unknown's avatar
unknown committed
6085
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
6086
	| BINARY simple_expr %prec NEG
unknown's avatar
unknown committed
6087
	  {
unknown's avatar
unknown committed
6088
            $$= create_func_cast($2, ITEM_CAST_CHAR, -1, 0, &my_charset_bin);
unknown's avatar
unknown committed
6089
	  }
6090
	| CAST_SYM '(' expr AS cast_type ')'
6091
	  {
unknown's avatar
unknown committed
6092
            LEX *lex= Lex;
6093
	    $$= create_func_cast($3, $5,
unknown's avatar
unknown committed
6094 6095 6096
                                 lex->length ? atoi(lex->length) : -1,
                                 lex->dec ? atoi(lex->dec) : 0,
                                 lex->charset);
6097
	  }
unknown's avatar
unknown committed
6098
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
6099
	  { $$= new Item_func_case(* $4, $2, $5 ); }
6100
	| CONVERT_SYM '(' expr ',' cast_type ')'
6101 6102 6103
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
unknown's avatar
unknown committed
6104
                                 Lex->dec ? atoi(Lex->dec) : 0,
6105 6106
				 Lex->charset);
	  }
6107 6108
	| CONVERT_SYM '(' expr USING charset_name ')'
	  { $$= new Item_func_conv_charset($3,$5); }
unknown's avatar
SCRUM  
unknown committed
6109
	| DEFAULT '(' simple_ident ')'
6110 6111 6112 6113 6114
	  {
	    if ($3->is_splocal())
	    {
	      Item_splocal *il= static_cast<Item_splocal *>($3);

6115
	      my_error(ER_WRONG_COLUMN_NAME, MYF(0), il->my_name()->str);
6116 6117
	      YYABORT;
	    }
unknown's avatar
unknown committed
6118
	    $$= new Item_default_value(Lex->current_context(), $3);
6119
	  }
6120
	| VALUES '(' simple_ident_nospvar ')'
unknown's avatar
unknown committed
6121
	  { $$= new Item_insert_value(Lex->current_context(), $3); }
unknown's avatar
unknown committed
6122
	| FUNC_ARG0 '(' ')'
unknown's avatar
unknown committed
6123 6124 6125
	  {
	    if (!$1.symbol->create_func)
	    {
6126 6127 6128
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
6129 6130 6131 6132
	      YYABORT;
	    }
	    $$= ((Item*(*)(void))($1.symbol->create_func))();
	  }
unknown's avatar
unknown committed
6133
	| FUNC_ARG1 '(' expr ')'
unknown's avatar
unknown committed
6134 6135 6136
	  {
	    if (!$1.symbol->create_func)
	    {
6137 6138 6139
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
6140 6141 6142 6143
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);
	  }
unknown's avatar
unknown committed
6144
	| FUNC_ARG2 '(' expr ',' expr ')'
unknown's avatar
unknown committed
6145 6146 6147
	  {
	    if (!$1.symbol->create_func)
	    {
6148 6149 6150
	      my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
6151 6152 6153 6154
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);
	  }
unknown's avatar
unknown committed
6155
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
unknown's avatar
unknown committed
6156 6157 6158
	  {
	    if (!$1.symbol->create_func)
	    {
6159 6160 6161
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
6162 6163 6164 6165
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*,Item*))($1.symbol->create_func))($3,$5,$7);
	  }
unknown's avatar
unknown committed
6166 6167 6168 6169
	| 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); }
6170 6171
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
unknown's avatar
unknown committed
6172 6173 6174 6175 6176 6177
	| 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); }
6178 6179
	| CHAR_SYM '(' expr_list USING charset_name ')'
	  { $$= new Item_func_char(*$3, $5); }
unknown's avatar
unknown committed
6180 6181
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
unknown's avatar
unknown committed
6182 6183
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
unknown's avatar
unknown committed
6184 6185
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
unknown's avatar
unknown committed
6186 6187 6188
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
6189
	  { $5->push_front($3); $$= new Item_func_concat_ws(*$5); }
6190 6191
	| CONVERT_TZ_SYM '(' expr ',' expr ',' expr ')'
	  {
6192 6193
            if (Lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
6194 6195
	    $$= new Item_func_convert_tz($3, $5, $7);
	  }
unknown's avatar
unknown committed
6196
	| CURDATE optional_braces
6197
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
6198
	| CURTIME optional_braces
6199
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
6200
	| CURTIME '(' expr ')'
6201
	  {
6202
	    $$= new Item_func_curtime_local($3);
6203
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
6204
	  }
unknown's avatar
unknown committed
6205
	| CURRENT_USER optional_braces
6206
          { $$= create_func_current_user(); }
6207 6208 6209 6210
	| DATE_ADD_INTERVAL '(' expr ',' interval_expr interval ')'
	  { $$= new Item_date_add_interval($3,$5,$6,0); }
	| DATE_SUB_INTERVAL '(' expr ',' interval_expr interval ')'
	  { $$= new Item_date_add_interval($3,$5,$6,1); }
unknown's avatar
unknown committed
6211
	| DATABASE '(' ')'
6212
	  {
unknown's avatar
unknown committed
6213
	    $$= new Item_func_database();
6214
            Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
6215
	  }
unknown's avatar
unknown committed
6216
	| DATE_SYM '(' expr ')'
unknown's avatar
unknown committed
6217
	  { $$= new Item_date_typecast($3); }
unknown's avatar
unknown committed
6218 6219
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
unknown's avatar
unknown committed
6220
	| ELT_FUNC '(' expr ',' expr_list ')'
6221
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
unknown's avatar
unknown committed
6222 6223
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
unknown's avatar
unknown committed
6224 6225 6226
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
6227
	    Lex->uncacheable(UNCACHEABLE_RAND);
unknown's avatar
unknown committed
6228
	  }
unknown's avatar
unknown committed
6229
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
6230
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
6231
	  { $$= new Item_func_decode($3,$5.str); }
6232
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
6233
	 { $$= new Item_func_encode($3,$5.str); }
unknown's avatar
unknown committed
6234
	| DES_DECRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
6235
        { $$= new Item_func_des_decrypt($3); }
unknown's avatar
unknown committed
6236
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
6237
        { $$= new Item_func_des_decrypt($3,$5); }
unknown's avatar
unknown committed
6238
	| DES_ENCRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
6239
        { $$= new Item_func_des_encrypt($3); }
unknown's avatar
unknown committed
6240
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
6241
        { $$= new Item_func_des_encrypt($3,$5); }
unknown's avatar
unknown committed
6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253
	| 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 ')'
	  {
unknown's avatar
unknown committed
6254
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
unknown's avatar
unknown committed
6255 6256
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
6257
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
unknown's avatar
unknown committed
6258 6259 6260 6261 6262
	| geometry_function
	  {
#ifdef HAVE_SPATIAL
	    $$= $1;
#else
6263 6264
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
6265 6266 6267
	    YYABORT;
#endif
	  }
6268
	| GET_FORMAT '(' date_time_type  ',' expr ')'
6269
	  { $$= new Item_func_get_format($3, $5); }
unknown's avatar
unknown committed
6270 6271 6272 6273 6274 6275
	| 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); }
6276
	| interval_expr interval '+' expr
unknown's avatar
unknown committed
6277
	  /* we cannot put interval before - */
6278 6279 6280 6281 6282
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
6283
              yyerror(ER(ER_SYNTAX_ERROR));
6284 6285 6286 6287
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
unknown's avatar
unknown committed
6288 6289
	| LAST_INSERT_ID '(' ')'
	  {
6290
	    $$= new Item_func_last_insert_id();
6291
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
6292 6293 6294
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
6295
	    $$= new Item_func_last_insert_id($3);
6296
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
6297 6298 6299 6300 6301 6302 6303
	  }
	| LEFT '(' expr ',' expr ')'
	  { $$= new Item_func_left($3,$5); }
	| LOCATE '(' expr ',' expr ')'
	  { $$= new Item_func_locate($5,$3); }
	| LOCATE '(' expr ',' expr ',' expr ')'
	  { $$= new Item_func_locate($5,$3,$7); }
unknown's avatar
unknown committed
6304
	| GREATEST_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
6305 6306 6307
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
6308
	| LOG_SYM '(' expr ')'
unknown's avatar
unknown committed
6309
	  { $$= new Item_func_log($3); }
6310
	| LOG_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
6311
	  { $$= new Item_func_log($3, $5); }
6312
	| MASTER_POS_WAIT '(' expr ',' expr ')'
unknown's avatar
unknown committed
6313
	  {
6314
	    $$= new Item_master_pos_wait($3, $5);
unknown's avatar
unknown committed
6315
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
6316
		  }
6317
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
unknown's avatar
unknown committed
6318
	  {
6319
	    $$= new Item_master_pos_wait($3, $5, $7);
unknown's avatar
unknown committed
6320
	    Lex->safe_to_cache_query=0;
6321
	  }
unknown's avatar
unknown committed
6322 6323
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
unknown's avatar
unknown committed
6324 6325
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
6326 6327
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
unknown's avatar
unknown committed
6328 6329 6330
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
	| NOW_SYM optional_braces
6331
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
6332
	| NOW_SYM '(' expr ')'
6333
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
6334
	| PASSWORD '(' expr ')'
6335
	  {
6336 6337
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
unknown's avatar
unknown committed
6338 6339 6340 6341
	      (Item *) new Item_func_password($3);
	  }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new Item_func_old_password($3); }
6342
	| POSITION_SYM '(' bit_expr IN_SYM expr ')'
unknown's avatar
unknown committed
6343
	  { $$ = new Item_func_locate($5,$3); }
6344 6345
	| QUARTER_SYM '(' expr ')'
	  { $$ = new Item_func_quarter($3); }
unknown's avatar
unknown committed
6346
	| RAND '(' expr ')'
6347
	  { $$= new Item_func_rand($3); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
6348
	| RAND '(' ')'
6349
	  { $$= new Item_func_rand(); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
6350 6351 6352 6353 6354 6355 6356
	| 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); }
6357 6358 6359 6360 6361
	| ROW_COUNT_SYM '(' ')'
	  {
	    $$= new Item_func_row_count();
	    Lex->safe_to_cache_query= 0;
	  }
unknown's avatar
unknown committed
6362 6363 6364 6365
	| SUBDATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_date_add_interval($3, $5, INTERVAL_DAY, 1);}
	| SUBDATE_SYM '(' expr ',' INTERVAL_SYM expr interval ')'
	  { $$= new Item_date_add_interval($3, $6, $7, 1); }
unknown's avatar
unknown committed
6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377
	| 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); }
6378
	| SYSDATE optional_braces
6379 6380 6381 6382 6383 6384
          {
            if (global_system_variables.sysdate_is_now == 0)
              $$= new Item_func_sysdate_local();
            else $$= new Item_func_now_local();
            Lex->safe_to_cache_query=0;
          }
6385
	| SYSDATE '(' expr ')'
6386 6387 6388 6389 6390 6391
          {
            if (global_system_variables.sysdate_is_now == 0)
              $$= new Item_func_sysdate_local($3);
            else $$= new Item_func_now_local($3);
            Lex->safe_to_cache_query=0;
          }
unknown's avatar
unknown committed
6392
	| TIME_SYM '(' expr ')'
unknown's avatar
unknown committed
6393 6394 6395
	  { $$= new Item_time_typecast($3); }
	| TIMESTAMP '(' expr ')'
	  { $$= new Item_datetime_typecast($3); }
unknown's avatar
unknown committed
6396
	| TIMESTAMP '(' expr ',' expr ')'
unknown's avatar
unknown committed
6397
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
6398 6399 6400 6401
	| TIMESTAMP_ADD '(' interval_time_st ',' expr ',' expr ')'
	  { $$= new Item_date_add_interval($7,$5,$3,0); }
	| TIMESTAMP_DIFF '(' interval_time_st ',' expr ',' expr ')'
	  { $$= new Item_func_timestamp_diff($5,$7,$3); }
unknown's avatar
unknown committed
6402
	| TRIM '(' expr ')'
6403 6404
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
unknown's avatar
unknown committed
6405
	  { $$= new Item_func_ltrim($6,$4); }
6406
	| TRIM '(' TRAILING expr FROM expr ')'
unknown's avatar
unknown committed
6407
	  { $$= new Item_func_rtrim($6,$4); }
6408
	| TRIM '(' BOTH expr FROM expr ')'
unknown's avatar
unknown committed
6409
	  { $$= new Item_func_trim($6,$4); }
6410 6411 6412 6413 6414 6415
	| TRIM '(' LEADING FROM expr ')'
	 { $$= new Item_func_ltrim($5); }
	| TRIM '(' TRAILING FROM expr ')'
	  { $$= new Item_func_rtrim($5); }
	| TRIM '(' BOTH FROM expr ')'
	  { $$= new Item_func_trim($5); }
unknown's avatar
unknown committed
6416 6417
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
6418 6419
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
6420
	| ident '.' ident '(' udf_expr_list ')'
unknown's avatar
unknown committed
6421
	  {
6422
	    LEX *lex= Lex;
6423 6424 6425
	    sp_name *name= new sp_name($1, $3);

	    name->init_qname(YYTHD);
6426
	    sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
6427
	    if ($5)
unknown's avatar
unknown committed
6428
	      $$= new Item_func_sp(Lex->current_context(), name, *$5);
unknown's avatar
unknown committed
6429
	    else
unknown's avatar
unknown committed
6430
	      $$= new Item_func_sp(Lex->current_context(), name);
6431
	    lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
6432
	  }
6433
	| IDENT_sys '(' 
6434 6435
          {
#ifdef HAVE_DLOPEN
6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454
            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;
6455
            SELECT_LEX *sel= Select;
6456

6457
            if (udf)
6458
            {
6459 6460 6461
              if (udf->type == UDFTYPE_AGGREGATE)
                Select->in_sum_expr--;

6462 6463
              Lex->binlog_row_based_if_mixed= 1;

6464 6465 6466 6467
              switch (udf->returns) {
              case STRING_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
6468 6469
                  if ($4 != NULL)
                    $$ = new Item_func_udf_str(udf, *$4);
6470 6471 6472 6473 6474
                  else
                    $$ = new Item_func_udf_str(udf);
                }
                else
                {
6475 6476
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_str(udf, *$4);
6477 6478 6479 6480 6481 6482 6483
                  else
                    $$ = new Item_sum_udf_str(udf);
                }
                break;
              case REAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
6484 6485
                  if ($4 != NULL)
                    $$ = new Item_func_udf_float(udf, *$4);
6486 6487 6488 6489 6490
                  else
                    $$ = new Item_func_udf_float(udf);
                }
                else
                {
6491 6492
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_float(udf, *$4);
6493 6494 6495 6496 6497 6498 6499
                  else
                    $$ = new Item_sum_udf_float(udf);
                }
                break;
              case INT_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
6500 6501
                  if ($4 != NULL)
                    $$ = new Item_func_udf_int(udf, *$4);
6502 6503 6504 6505 6506
                  else
                    $$ = new Item_func_udf_int(udf);
                }
                else
                {
6507 6508
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_int(udf, *$4);
6509 6510 6511 6512
                  else
                    $$ = new Item_sum_udf_int(udf);
                }
                break;
unknown's avatar
unknown committed
6513 6514 6515
              case DECIMAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
6516 6517
                  if ($4 != NULL)
                    $$ = new Item_func_udf_decimal(udf, *$4);
unknown's avatar
unknown committed
6518 6519 6520 6521 6522
                  else
                    $$ = new Item_func_udf_decimal(udf);
                }
                else
                {
6523 6524
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_decimal(udf, *$4);
unknown's avatar
unknown committed
6525 6526 6527 6528
                  else
                    $$ = new Item_sum_udf_decimal(udf);
                }
                break;
6529 6530 6531 6532 6533 6534 6535
              default:
                YYABORT;
              }
            }
            else
#endif /* HAVE_DLOPEN */
            {
6536
	      LEX *lex= Lex;
6537 6538
              sp_name *name= sp_name_current_db_new(YYTHD, $1);

6539
              sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
6540 6541
              if ($4)
                $$= new Item_func_sp(Lex->current_context(), name, *$4);
6542
              else
unknown's avatar
unknown committed
6543
                $$= new Item_func_sp(Lex->current_context(), name);
6544
	      lex->safe_to_cache_query=0;
6545 6546
	    }
          }
unknown's avatar
unknown committed
6547
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
6548
	  {
unknown's avatar
unknown committed
6549 6550
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
unknown's avatar
unknown committed
6551
	| UNIX_TIMESTAMP '(' ')'
unknown's avatar
unknown committed
6552 6553
	  {
	    $$= new Item_func_unix_timestamp();
6554
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
6555
	  }
unknown's avatar
unknown committed
6556 6557 6558
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
6559
	  { $$= new Item_func_user(FALSE); Lex->safe_to_cache_query=0; }
6560 6561 6562 6563 6564 6565
	| UTC_DATE_SYM optional_braces
	  { $$= new Item_func_curdate_utc(); Lex->safe_to_cache_query=0;}
	| UTC_TIME_SYM optional_braces
	  { $$= new Item_func_curtime_utc(); Lex->safe_to_cache_query=0;}
	| UTC_TIMESTAMP_SYM optional_braces
	  { $$= new Item_func_now_utc(); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
6566
	| WEEK_SYM '(' expr ')'
unknown's avatar
unknown committed
6567
	  {
6568
            $$= new Item_func_week($3,new Item_int((char*) "0",
unknown's avatar
unknown committed
6569
				   YYTHD->variables.default_week_format,1));
6570
          }
unknown's avatar
unknown committed
6571 6572 6573 6574 6575 6576 6577 6578
	| 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); }
unknown's avatar
unknown committed
6579
	| BENCHMARK_SYM '(' ulong_num ',' expr ')'
6580
	  {
unknown's avatar
unknown committed
6581
	    $$=new Item_func_benchmark($3,$5);
6582
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
6583
	  }
unknown's avatar
unknown committed
6584
	| EXTRACT_SYM '(' interval FROM expr ')'
unknown's avatar
unknown committed
6585
	{ $$=new Item_extract( $3, $5); };
unknown's avatar
unknown committed
6586

unknown's avatar
unknown committed
6587
geometry_function:
unknown's avatar
unknown committed
6588 6589 6590
	  CONTAINS_SYM '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_spatial_rel($3, $5, Item_func::SP_CONTAINS_FUNC)); }
	| GEOMFROMTEXT '(' expr ')'
unknown's avatar
unknown committed
6591 6592 6593 6594 6595 6596 6597 6598 6599
	  { $$= 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,
6600 6601
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
unknown's avatar
unknown committed
6602 6603
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
6604
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
unknown's avatar
unknown committed
6605 6606
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
6607
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621
 	| 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,
6622
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
unknown's avatar
unknown committed
6623 6624
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
6625
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
unknown's avatar
unknown committed
6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637
	| 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,
6638
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
6639 6640 6641 6642 6643 6644 6645 6646 6647 6648
 	| 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)); }
	;

6649
fulltext_options:
6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663
          opt_natural_language_mode opt_query_expansion
          { $$= $1 | $2; }
        | IN_SYM BOOLEAN_SYM MODE_SYM
          { $$= FT_BOOL; }
        ;

opt_natural_language_mode:
        /* nothing */                           { $$= FT_NL;  }
        | IN_SYM NATURAL LANGUAGE_SYM MODE_SYM  { $$= FT_NL; }
        ;

opt_query_expansion:
        /* nothing */                           { $$= 0;         }
        | WITH QUERY_SYM EXPANSION_SYM          { $$= FT_EXPAND; }
6664 6665
        ;

unknown's avatar
unknown committed
6666
udf_expr_list:
6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691
	/* 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)
6692 6693 6694 6695
          {
	    $2->set_name($4.str, $4.length, system_charset_info);
            $2->is_autogenerated_name= FALSE;
          }
6696
	  else
6697
	    $2->set_name($1, (uint) ($3 - $1), YYTHD->charset());
6698 6699 6700
	  $$= $2;
	}
	;
unknown's avatar
unknown committed
6701 6702 6703 6704

sum_expr:
	AVG_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_avg($3); }
6705 6706
	| AVG_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_avg_distinct($4); }
unknown's avatar
unknown committed
6707 6708 6709 6710
	| BIT_AND  '(' in_sum_expr ')'
	  { $$=new Item_sum_and($3); }
	| BIT_OR  '(' in_sum_expr ')'
	  { $$=new Item_sum_or($3); }
6711 6712
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
unknown's avatar
unknown committed
6713
	| COUNT_SYM '(' opt_all '*' ')'
unknown's avatar
unknown committed
6714 6715 6716
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
6717
	| COUNT_SYM '(' DISTINCT
unknown's avatar
unknown committed
6718
	  { Select->in_sum_expr++; }
6719
	   expr_list
unknown's avatar
unknown committed
6720
	  { Select->in_sum_expr--; }
6721 6722
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
unknown's avatar
unknown committed
6723 6724 6725 6726
	| 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); }
6727 6728 6729 6730 6731 6732 6733
/*
   According to ANSI SQL, DISTINCT is allowed and has
   no sence inside MIN and MAX grouping functions; so MIN|MAX(DISTINCT ...)
   is processed like an ordinary MIN | MAX()
 */
	| MIN_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_min($4); }
unknown's avatar
unknown committed
6734 6735
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
6736 6737
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
unknown's avatar
unknown committed
6738
	| STD_SYM '(' in_sum_expr ')'
6739
	  { $$=new Item_sum_std($3, 0); }
unknown's avatar
unknown committed
6740
	| VARIANCE_SYM '(' in_sum_expr ')'
6741 6742 6743 6744 6745
	  { $$=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); }
unknown's avatar
unknown committed
6746
	| SUM_SYM '(' in_sum_expr ')'
6747
	  { $$=new Item_sum_sum($3); }
6748 6749
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
unknown's avatar
unknown committed
6750 6751 6752 6753 6754
	| GROUP_CONCAT_SYM '(' opt_distinct
	  { Select->in_sum_expr++; }
	  expr_list opt_gorder_clause
	  opt_gconcat_separator
	 ')'
unknown's avatar
unknown committed
6755
	  {
6756 6757
            SELECT_LEX *sel= Select;
	    sel->in_sum_expr--;
unknown's avatar
unknown committed
6758
	    $$=new Item_func_group_concat(Lex->current_context(), $3, $5,
6759
                                          sel->gorder_list, $7);
unknown's avatar
unknown committed
6760
	    $5->empty();
6761 6762 6763 6764 6765 6766 6767
	  };

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

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

6771 6772

opt_gorder_clause:
6773 6774
	  /* empty */
	  {
unknown's avatar
unknown committed
6775
            Select->gorder_list = NULL;
6776 6777 6778
	  }
	| order_clause
          {
unknown's avatar
unknown committed
6779 6780 6781
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
6782
				     sizeof(st_sql_list));
unknown's avatar
unknown committed
6783
	    select->order_list.empty();
6784
	  };
unknown's avatar
unknown committed
6785

unknown's avatar
unknown committed
6786 6787

in_sum_expr:
unknown's avatar
unknown committed
6788
	opt_all
6789 6790 6791 6792
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
6793
	    yyerror(ER(ER_SYNTAX_ERROR));
6794
	    YYABORT;
6795 6796
	  }
	}
unknown's avatar
unknown committed
6797 6798
	expr
	{
unknown's avatar
unknown committed
6799
	  Select->in_sum_expr--;
6800
	  $$= $3;
unknown's avatar
unknown committed
6801
	};
unknown's avatar
unknown committed
6802

6803
cast_type:
unknown's avatar
unknown committed
6804 6805
        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; }
6806
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; Lex->dec=0; }
unknown's avatar
unknown committed
6807 6808 6809 6810 6811 6812 6813 6814
        | 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; }
6815 6816
	;

unknown's avatar
unknown committed
6817
expr_list:
6818
	{ Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
6819
	expr_list2
unknown's avatar
unknown committed
6820
	{ $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
6821 6822

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

unknown's avatar
unknown committed
6826 6827
ident_list_arg:
          ident_list          { $$= $1; }
unknown's avatar
unknown committed
6828
        | '(' ident_list ')'  { $$= $2; };
unknown's avatar
unknown committed
6829

unknown's avatar
unknown committed
6830
ident_list:
6831
        { Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
6832
        ident_list2
unknown's avatar
unknown committed
6833
        { $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
6834 6835

ident_list2:
6836
        simple_ident { Select->expr_list.head()->push_back($1); }
unknown's avatar
unknown committed
6837
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
unknown's avatar
unknown committed
6838 6839 6840

opt_expr:
	/* empty */      { $$= NULL; }
6841
	| expr           { $$= $1; };
unknown's avatar
unknown committed
6842 6843 6844

opt_else:
	/* empty */    { $$= NULL; }
unknown's avatar
unknown committed
6845
	| ELSE expr    { $$= $2; };
unknown's avatar
unknown committed
6846 6847

when_list:
unknown's avatar
unknown committed
6848
        { Select->when_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
6849
	when_list2
unknown's avatar
unknown committed
6850
	{ $$= Select->when_list.pop(); };
unknown's avatar
unknown committed
6851 6852

when_list2:
unknown's avatar
unknown committed
6853
	expr THEN_SYM expr
unknown's avatar
unknown committed
6854
	  {
unknown's avatar
unknown committed
6855
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
6856 6857
	    sel->when_list.head()->push_back($1);
	    sel->when_list.head()->push_back($3);
unknown's avatar
unknown committed
6858
	}
unknown's avatar
unknown committed
6859
	| when_list2 WHEN_SYM expr THEN_SYM expr
unknown's avatar
unknown committed
6860
	  {
unknown's avatar
unknown committed
6861
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
6862 6863
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
unknown's avatar
unknown committed
6864
	  };
unknown's avatar
unknown committed
6865

unknown's avatar
unknown committed
6866
/* Warning - may return NULL in case of incomplete SELECT */
6867 6868 6869
table_ref:
        table_factor            { $$=$1; }
        | join_table            { $$=$1; }
6870
          {
6871 6872 6873
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
6874
          }
6875 6876
        ;

unknown's avatar
unknown committed
6877
join_table_list:
unknown's avatar
unknown committed
6878
	derived_table_list		{ YYERROR_UNLESS($$=$1); }
unknown's avatar
unknown committed
6879
	;
unknown's avatar
unknown committed
6880

unknown's avatar
unknown committed
6881 6882
/* Warning - may return NULL in case of incomplete SELECT */
derived_table_list:
6883
        table_ref { $$=$1; }
unknown's avatar
unknown committed
6884 6885
        | derived_table_list ',' table_ref
          {
unknown's avatar
unknown committed
6886
            YYERROR_UNLESS($1 && ($$=$3));
unknown's avatar
unknown committed
6887
          }
6888 6889
        ;

6890 6891 6892 6893 6894 6895 6896
/*
  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.
*/
6897
join_table:
6898
/* INNER JOIN variants */
unknown's avatar
unknown committed
6899
        /*
6900 6901 6902
          Use %prec to evaluate production 'table_ref' before 'normal_join'
          so that [INNER | CROSS] JOIN is properly nested as other
          left-associative joins.
unknown's avatar
unknown committed
6903 6904
        */
        table_ref %prec TABLE_REF_PRIORITY normal_join table_ref
6905
          { YYERROR_UNLESS($1 && ($$=$3)); }
6906
	| table_ref STRAIGHT_JOIN table_factor
unknown's avatar
unknown committed
6907
	  { YYERROR_UNLESS($1 && ($$=$3)); $3->straight=1; }
unknown's avatar
unknown committed
6908 6909 6910 6911 6912
	| table_ref normal_join table_ref
          ON
          {
            YYERROR_UNLESS($1 && ($$=$3));
            /* Change the current name resolution context to a local context. */
6913
            if (push_new_name_resolution_context(YYTHD, $1, $3))
unknown's avatar
unknown committed
6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925
              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. */
6926
            if (push_new_name_resolution_context(YYTHD, $1, $3))
unknown's avatar
unknown committed
6927 6928 6929 6930 6931 6932 6933 6934
              YYABORT;
          }
          expr
          {
            $3->straight=1;
            add_join_on($3,$6);
            Lex->pop_context();
          }
6935
	| table_ref normal_join table_ref
unknown's avatar
unknown committed
6936
	  USING
unknown's avatar
unknown committed
6937
	  {
unknown's avatar
unknown committed
6938
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
6939
            YYERROR_UNLESS($1 && $3);
unknown's avatar
unknown committed
6940
	  }
unknown's avatar
unknown committed
6941
	  '(' using_list ')'
unknown's avatar
unknown committed
6942
          { add_join_natural($1,$3,$7); $$=$3; }
6943 6944 6945 6946 6947 6948 6949
	| table_ref NATURAL JOIN_SYM table_factor
	  {
            YYERROR_UNLESS($1 && ($$=$4));
            add_join_natural($1,$4,NULL);
          }

/* LEFT JOIN variants */
unknown's avatar
unknown committed
6950 6951 6952
	| table_ref LEFT opt_outer JOIN_SYM table_ref
          ON
          {
6953
            YYERROR_UNLESS($1 && $5);
unknown's avatar
unknown committed
6954
            /* Change the current name resolution context to a local context. */
6955
            if (push_new_name_resolution_context(YYTHD, $1, $5))
unknown's avatar
unknown committed
6956 6957 6958 6959 6960 6961 6962 6963 6964
              YYABORT;
          }
          expr
	  {
            add_join_on($5,$8);
            Lex->pop_context();
            $5->outer_join|=JOIN_TYPE_LEFT;
            $$=$5;
          }
6965
	| table_ref LEFT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
6966
	  {
unknown's avatar
unknown committed
6967
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
6968
            YYERROR_UNLESS($1 && $5);
unknown's avatar
unknown committed
6969
	  }
unknown's avatar
unknown committed
6970
	  USING '(' using_list ')'
unknown's avatar
unknown committed
6971
          { add_join_natural($1,$5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
6972
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
6973
	  {
unknown's avatar
unknown committed
6974
            YYERROR_UNLESS($1 && $6);
unknown's avatar
unknown committed
6975
 	    add_join_natural($1,$6,NULL);
6976
	    $6->outer_join|=JOIN_TYPE_LEFT;
6977 6978
	    $$=$6;
	  }
6979 6980

/* RIGHT JOIN variants */
unknown's avatar
unknown committed
6981 6982 6983
	| table_ref RIGHT opt_outer JOIN_SYM table_ref
          ON
          {
6984
            YYERROR_UNLESS($1 && $5);
unknown's avatar
unknown committed
6985
            /* Change the current name resolution context to a local context. */
6986
            if (push_new_name_resolution_context(YYTHD, $1, $5))
unknown's avatar
unknown committed
6987 6988 6989
              YYABORT;
          }
          expr
unknown's avatar
unknown committed
6990
          {
6991 6992 6993
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
unknown's avatar
unknown committed
6994 6995
            add_join_on($$, $8);
            Lex->pop_context();
6996 6997
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
6998
	  {
unknown's avatar
unknown committed
6999
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7000
            YYERROR_UNLESS($1 && $5);
unknown's avatar
unknown committed
7001
	  }
7002
	  USING '(' using_list ')'
unknown's avatar
unknown committed
7003
          {
7004 7005 7006
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
unknown's avatar
unknown committed
7007
            add_join_natural($$,$5,$9);
7008 7009
          }
	| table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor
7010
	  {
unknown's avatar
unknown committed
7011
            YYERROR_UNLESS($1 && $6);
unknown's avatar
unknown committed
7012
	    add_join_natural($6,$1,NULL);
7013 7014 7015
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
7016
	  };
unknown's avatar
unknown committed
7017 7018

normal_join:
unknown's avatar
unknown committed
7019 7020 7021 7022
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
unknown's avatar
unknown committed
7023

unknown's avatar
unknown committed
7024
/* Warning - may return NULL in case of incomplete SELECT */
7025
table_factor:
unknown's avatar
unknown committed
7026
	{
unknown's avatar
unknown committed
7027
	  SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7028
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
unknown's avatar
unknown committed
7029
	  sel->table_join_options= 0;
unknown's avatar
unknown committed
7030
	}
unknown's avatar
unknown committed
7031
        table_ident opt_table_alias opt_key_definition
unknown's avatar
unknown committed
7032
	{
unknown's avatar
unknown committed
7033
	  LEX *lex= Lex;
unknown's avatar
unknown committed
7034
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
7035
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
unknown's avatar
unknown committed
7036
					   sel->get_table_join_options(),
unknown's avatar
unknown committed
7037
					   lex->lock_option,
7038 7039
					   sel->get_use_index(),
					   sel->get_ignore_index())))
7040
	    YYABORT;
7041
          sel->add_joined_table($$);
unknown's avatar
unknown committed
7042
	}
unknown's avatar
unknown committed
7043 7044 7045 7046
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref
          ON
          {
            /* Change the current name resolution context to a local context. */
7047
            if (push_new_name_resolution_context(YYTHD, $3, $7))
unknown's avatar
unknown committed
7048
              YYABORT;
7049

unknown's avatar
unknown committed
7050 7051 7052 7053 7054 7055 7056 7057 7058
          }
          expr '}'
	  {
            YYERROR_UNLESS($3 && $7);
            add_join_on($7,$10);
            Lex->pop_context();
            $7->outer_join|=JOIN_TYPE_LEFT;
            $$=$7;
          }
unknown's avatar
unknown committed
7059
	| select_derived_init get_select_lex select_derived2
7060
          {
7061
            LEX *lex= Lex;
unknown's avatar
unknown committed
7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075
            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))
unknown's avatar
unknown committed
7076
              YYABORT;
unknown's avatar
unknown committed
7077
            $$= 0;
unknown's avatar
unknown committed
7078
            /* incomplete derived tables return NULL, we must be
unknown's avatar
unknown committed
7079
               nested in select_derived rule to be here. */
7080
          }
unknown's avatar
unknown committed
7081 7082 7083 7084 7085
	| '(' 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. */

unknown's avatar
unknown committed
7086
          if (!($3 || $6) && $2->embedding &&
unknown's avatar
unknown committed
7087
              !$2->embedding->nested_join->join_list.elements)
7088
          {
unknown's avatar
unknown committed
7089 7090 7091 7092 7093
            /* 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;
7094
          }
unknown's avatar
unknown committed
7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108
          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)))
unknown's avatar
unknown committed
7109

unknown's avatar
unknown committed
7110
	      YYABORT;
unknown's avatar
unknown committed
7111
            sel->add_joined_table($$);
unknown's avatar
unknown committed
7112
            lex->pop_context();
unknown's avatar
unknown committed
7113 7114 7115 7116 7117 7118
          }
	  else
          if ($4 || $6)
	  {
            /* simple nested joins cannot have aliases or unions */
            yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
7119
	    YYABORT;
unknown's avatar
unknown committed
7120 7121 7122 7123 7124
	  }
          else
            $$= $3;
	}
        ;
unknown's avatar
unknown committed
7125

unknown's avatar
unknown committed
7126
/* handle contents of parentheses in join expression */
7127
select_derived:
unknown's avatar
unknown committed
7128
	  get_select_lex
unknown's avatar
unknown committed
7129
	  {
unknown's avatar
unknown committed
7130 7131 7132 7133 7134 7135 7136 7137 7138
            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 */
unknown's avatar
unknown committed
7139

unknown's avatar
unknown committed
7140 7141 7142 7143
            if (!($$= $1->end_nested_join(lex->thd)) && $3)
              YYABORT;
            if (!$3 && $$)
            {
unknown's avatar
unknown committed
7144 7145
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
unknown's avatar
unknown committed
7146 7147
            }
          }
unknown's avatar
unknown committed
7148
 	;
unknown's avatar
unknown committed
7149

unknown's avatar
unknown committed
7150
select_derived2:
unknown's avatar
unknown committed
7151
        {
unknown's avatar
unknown committed
7152
	  LEX *lex= Lex;
7153
	  lex->derived_tables|= DERIVED_SUBQUERY;
7154 7155
          if (lex->sql_command == (int)SQLCOM_HA_READ ||
              lex->sql_command == (int)SQLCOM_KILL)
7156 7157
	  {
	    yyerror(ER(ER_SYNTAX_ERROR));
7158 7159
	    YYABORT;
	  }
7160
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
unknown's avatar
unknown committed
7161
              mysql_new_select(lex, 1))
unknown's avatar
unknown committed
7162 7163
	    YYABORT;
	  mysql_init_select(lex);
7164
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
7165
	  lex->current_select->parsing_place= SELECT_LIST;
7166 7167 7168
	}
        select_options select_item_list
	{
7169
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
7170
	}
unknown's avatar
unknown committed
7171
	opt_select_from
7172
        ;
unknown's avatar
unknown committed
7173

unknown's avatar
unknown committed
7174 7175 7176 7177 7178 7179 7180 7181
get_select_lex:
	/* Empty */ { $$= Select; }
        ;

select_derived_init:
          SELECT_SYM
          {
            LEX *lex= Lex;
unknown's avatar
unknown committed
7182
            SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196
            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 */
          }
        ;

unknown's avatar
unknown committed
7197 7198
opt_outer:
	/* empty */	{}
unknown's avatar
unknown committed
7199
	| OUTER		{};
unknown's avatar
unknown committed
7200 7201 7202 7203

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
unknown's avatar
unknown committed
7204
          {
unknown's avatar
unknown committed
7205
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7206 7207 7208
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
7209 7210
	| FORCE_SYM key_usage_list
          {
unknown's avatar
unknown committed
7211
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7212 7213 7214 7215
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
unknown's avatar
unknown committed
7216
	| IGNORE_SYM key_usage_list
unknown's avatar
unknown committed
7217
	  {
unknown's avatar
unknown committed
7218
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7219 7220
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
unknown's avatar
unknown committed
7221
	  };
unknown's avatar
unknown committed
7222 7223

key_usage_list:
unknown's avatar
unknown committed
7224
	key_or_index { Select->interval_list.empty(); }
unknown's avatar
unknown committed
7225
        '(' key_list_or_empty ')'
unknown's avatar
unknown committed
7226
        { $$= &Select->interval_list; }
unknown's avatar
unknown committed
7227 7228 7229 7230 7231 7232
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
unknown's avatar
unknown committed
7233 7234 7235

key_usage_list2:
	key_usage_list2 ',' ident
unknown's avatar
unknown committed
7236
        { Select->
unknown's avatar
unknown committed
7237
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $3.str, $3.length,
7238
				    system_charset_info)); }
unknown's avatar
unknown committed
7239
	| ident
unknown's avatar
unknown committed
7240
        { Select->
unknown's avatar
unknown committed
7241
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $1.str, $1.length,
7242
				    system_charset_info)); }
unknown's avatar
unknown committed
7243
	| PRIMARY_SYM
unknown's avatar
unknown committed
7244
        { Select->
unknown's avatar
unknown committed
7245
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
7246
				    system_charset_info)); };
unknown's avatar
unknown committed
7247 7248 7249

using_list:
	ident
unknown's avatar
unknown committed
7250
	  {
unknown's avatar
unknown committed
7251
            if (!($$= new List<String>))
unknown's avatar
unknown committed
7252
	      YYABORT;
unknown's avatar
unknown committed
7253 7254 7255
            $$->push_back(new (YYTHD->mem_root)
                              String((const char *) $1.str, $1.length,
                                      system_charset_info));
unknown's avatar
unknown committed
7256 7257 7258
	  }
	| using_list ',' ident
	  {
unknown's avatar
unknown committed
7259 7260 7261 7262
            $1->push_back(new (YYTHD->mem_root)
                              String((const char *) $3.str, $3.length,
                                      system_charset_info));
            $$= $1;
unknown's avatar
unknown committed
7263
	  };
unknown's avatar
unknown committed
7264 7265

interval:
7266 7267
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
unknown's avatar
unknown committed
7268
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
unknown's avatar
unknown committed
7269 7270
	| DAY_MINUTE_SYM	{ $$=INTERVAL_DAY_MINUTE; }
	| DAY_SECOND_SYM	{ $$=INTERVAL_DAY_SECOND; }
unknown's avatar
unknown committed
7271
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
unknown's avatar
unknown committed
7272 7273
	| HOUR_MINUTE_SYM	{ $$=INTERVAL_HOUR_MINUTE; }
	| HOUR_SECOND_SYM	{ $$=INTERVAL_HOUR_SECOND; }
unknown's avatar
unknown committed
7274 7275
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
unknown's avatar
unknown committed
7276
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
7277 7278 7279 7280 7281 7282 7283 7284
	| SECOND_MICROSECOND_SYM	{ $$=INTERVAL_SECOND_MICROSECOND; }
	| YEAR_MONTH_SYM	{ $$=INTERVAL_YEAR_MONTH; };

interval_time_st:
	DAY_SYM			{ $$=INTERVAL_DAY; }
	| WEEK_SYM		{ $$=INTERVAL_WEEK; }
	| HOUR_SYM		{ $$=INTERVAL_HOUR; }
	| FRAC_SECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
unknown's avatar
unknown committed
7285 7286
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
7287
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
unknown's avatar
unknown committed
7288
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
7289 7290
	| YEAR_SYM		{ $$=INTERVAL_YEAR; }
        ;
unknown's avatar
unknown committed
7291

7292
date_time_type:
7293 7294 7295 7296 7297
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
7298

unknown's avatar
unknown committed
7299 7300 7301
table_alias:
	/* empty */
	| AS
unknown's avatar
unknown committed
7302
	| EQ;
unknown's avatar
unknown committed
7303 7304 7305 7306

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

unknown's avatar
unknown committed
7309 7310 7311 7312
opt_all:
	/* empty */
	| ALL
	;
unknown's avatar
unknown committed
7313 7314

where_clause:
unknown's avatar
unknown committed
7315
	/* empty */  { Select->where= 0; }
unknown's avatar
unknown committed
7316 7317 7318 7319 7320
	| WHERE
          {
            Select->parsing_place= IN_WHERE;
          }
          expr
7321
	  {
unknown's avatar
unknown committed
7322 7323 7324 7325 7326
            SELECT_LEX *select= Select;
	    select->where= $3;
            select->parsing_place= NO_MATTER;
	    if ($3)
	      $3->top_level_item();
7327
	  }
unknown's avatar
unknown committed
7328
 	;
unknown's avatar
unknown committed
7329 7330 7331

having_clause:
	/* empty */
unknown's avatar
unknown committed
7332 7333
	| HAVING
	  {
7334
	    Select->parsing_place= IN_HAVING;
unknown's avatar
unknown committed
7335 7336
          }
	  expr
unknown's avatar
unknown committed
7337
	  {
unknown's avatar
unknown committed
7338
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7339
	    sel->having= $3;
7340
	    sel->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
7341 7342 7343
	    if ($3)
	      $3->top_level_item();
	  }
7344
	;
unknown's avatar
unknown committed
7345 7346

opt_escape:
7347 7348 7349 7350 7351
	ESCAPE_SYM simple_expr 
          {
            Lex->escape_used= TRUE;
            $$= $2;
          }
7352
	| /* empty */
7353
          {
7354
            Lex->escape_used= FALSE;
7355 7356 7357
            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
7358 7359
          }
        ;
unknown's avatar
unknown committed
7360 7361 7362


/*
unknown's avatar
unknown committed
7363
   group by statement in select
unknown's avatar
unknown committed
7364 7365 7366 7367
*/

group_clause:
	/* empty */
7368
	| GROUP BY group_list olap_opt;
unknown's avatar
unknown committed
7369 7370

group_list:
unknown's avatar
unknown committed
7371
	group_list ',' order_ident order_dir
unknown's avatar
unknown committed
7372
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
7373
	| order_ident order_dir
unknown's avatar
unknown committed
7374
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
7375

7376 7377
olap_opt:
	/* empty */ {}
7378
	| WITH CUBE_SYM
7379
          {
7380
	    LEX *lex=Lex;
7381 7382
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
7383
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
7384 7385 7386
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
7387
	    lex->current_select->olap= CUBE_TYPE;
unknown's avatar
unknown committed
7388
	    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
7389
	    YYABORT;	/* To be deleted in 5.1 */
7390
	  }
7391
	| WITH ROLLUP_SYM
7392
          {
7393 7394 7395
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
7396
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
7397 7398 7399
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
7400
	    lex->current_select->olap= ROLLUP_TYPE;
7401
	  }
7402
	;
7403

unknown's avatar
unknown committed
7404
/*
unknown's avatar
unknown committed
7405
   Order by statement in select
unknown's avatar
unknown committed
7406 7407
*/

7408
opt_order_clause:
unknown's avatar
unknown committed
7409
	/* empty */
unknown's avatar
unknown committed
7410
	| order_clause;
7411 7412

order_clause:
7413 7414
	ORDER_SYM BY
        {
unknown's avatar
unknown committed
7415
	  LEX *lex=Lex;
7416
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
unknown's avatar
unknown committed
7417
	      lex->current_select->olap !=
7418
	      UNSPECIFIED_OLAP_TYPE)
7419
	  {
unknown's avatar
unknown committed
7420 7421
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
7422 7423
	    YYABORT;
	  }
unknown's avatar
unknown committed
7424
	} order_list;
unknown's avatar
unknown committed
7425 7426 7427

order_list:
	order_list ',' order_ident order_dir
unknown's avatar
unknown committed
7428
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
7429
	| order_ident order_dir
unknown's avatar
unknown committed
7430
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
7431 7432 7433

order_dir:
	/* empty */ { $$ =  1; }
unknown's avatar
unknown committed
7434
	| ASC  { $$ =1; }
unknown's avatar
unknown committed
7435
	| DESC { $$ =0; };
unknown's avatar
unknown committed
7436 7437


7438 7439 7440
opt_limit_clause_init:
	/* empty */
	{
7441 7442
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
7443 7444
          sel->offset_limit= 0;
          sel->select_limit= 0;
7445
	}
7446 7447 7448
	| limit_clause {}
	;

7449 7450 7451 7452 7453
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

7454
limit_clause:
7455
	LIMIT limit_options {}
unknown's avatar
unknown committed
7456
	;
unknown's avatar
unknown committed
7457 7458

limit_options:
7459
	limit_option
unknown's avatar
unknown committed
7460
	  {
unknown's avatar
unknown committed
7461
            SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7462
            sel->select_limit= $1;
7463
            sel->offset_limit= 0;
7464
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
7465
	  }
7466
	| limit_option ',' limit_option
unknown's avatar
unknown committed
7467
	  {
unknown's avatar
unknown committed
7468
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7469 7470
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
7471
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
7472
	  }
7473
	| limit_option OFFSET_SYM limit_option
unknown's avatar
unknown committed
7474
	  {
unknown's avatar
unknown committed
7475
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7476 7477
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
7478
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
7479 7480
	  }
	;
7481 7482 7483 7484 7485
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); }
unknown's avatar
unknown committed
7486
        ;
unknown's avatar
unknown committed
7487 7488 7489 7490

delete_limit_clause:
	/* empty */
	{
unknown's avatar
unknown committed
7491
	  LEX *lex=Lex;
7492
	  lex->current_select->select_limit= 0;
unknown's avatar
unknown committed
7493
	}
7494
	| LIMIT limit_option
7495 7496
	{
	  SELECT_LEX *sel= Select;
7497
	  sel->select_limit= $2;
7498 7499
	  sel->explicit_limit= 1;
	};
unknown's avatar
unknown committed
7500

unknown's avatar
unknown committed
7501 7502
ulong_num:
          NUM           { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
7503
	| HEX_NUM       { $$= (ulong) strtol($1.str, (char**) 0, 16); }
unknown's avatar
unknown committed
7504 7505
	| LONG_NUM      { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| ULONGLONG_NUM { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
7506
        | DECIMAL_NUM   { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
7507 7508
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
7509

unknown's avatar
unknown committed
7510
ulonglong_num:
unknown's avatar
unknown committed
7511 7512 7513
	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); }
unknown's avatar
unknown committed
7514
        | DECIMAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
7515 7516
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
7517 7518 7519 7520 7521 7522

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
7523 7524
	    if (&lex->select_lex != lex->current_select)
	    {
unknown's avatar
unknown committed
7525
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
7526 7527
	      YYABORT;
	    }
unknown's avatar
unknown committed
7528 7529 7530
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
7531 7532 7533 7534
	    if (add_proc_to_list(lex->thd, new Item_field(&lex->
                                                          current_select->
                                                          context,
                                                          NULL,NULL,$2.str)))
unknown's avatar
unknown committed
7535
	      YYABORT;
7536
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
7537
	  }
unknown's avatar
unknown committed
7538
	  '(' procedure_list ')';
unknown's avatar
unknown committed
7539 7540 7541 7542


procedure_list:
	/* empty */ {}
unknown's avatar
unknown committed
7543
	| procedure_list2 {};
unknown's avatar
unknown committed
7544 7545 7546

procedure_list2:
	procedure_list2 ',' procedure_item
unknown's avatar
unknown committed
7547
	| procedure_item;
unknown's avatar
unknown committed
7548 7549 7550 7551

procedure_item:
	  remember_name expr
	  {
unknown's avatar
unknown committed
7552 7553
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
unknown's avatar
unknown committed
7554 7555
	      YYABORT;
	    if (!$2->name)
7556 7557
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1),
                           YYTHD->charset());
7558 7559 7560 7561 7562 7563
	  }
          ;


select_var_list_init:
	   {
unknown's avatar
unknown committed
7564 7565
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
7566 7567
	        YYABORT;
	   }
7568
	   select_var_list
7569
	   {}
7570
           ;
unknown's avatar
unknown committed
7571

unknown's avatar
unknown committed
7572
select_var_list:
7573 7574 7575 7576
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

7577 7578 7579 7580 7581 7582 7583 7584 7585 7586
select_var_ident:  
	   '@' ident_or_text
           {
             LEX *lex=Lex;
	     if (lex->result) 
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($2,0,0,(enum_field_types)0));
	     else
	       YYABORT;
	   }
           | ident_or_text
unknown's avatar
unknown committed
7587
           {
7588
             LEX *lex=Lex;
7589 7590 7591 7592
	     sp_pvar_t *t;

	     if (!lex->spcont || !(t=lex->spcont->find_pvar(&$1)))
	     {
7593
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
unknown's avatar
unknown committed
7594
	       YYABORT;
7595 7596 7597 7598 7599
	     }
	     if (! lex->result)
	       YYABORT;
	     else
	     {
7600 7601 7602
               my_var *var;
	       ((select_dumpvar *)lex->result)->
                 var_list.push_back(var= new my_var($1,1,t->offset,t->type));
unknown's avatar
unknown committed
7603
#ifndef DBUG_OFF
7604
	       if (var)
7605
		 var->sp= lex->sphead;
7606
#endif
7607
	     }
unknown's avatar
unknown committed
7608
	   }
unknown's avatar
unknown committed
7609
           ;
unknown's avatar
unknown committed
7610

7611
into:
unknown's avatar
unknown committed
7612
        INTO OUTFILE TEXT_STRING_filesystem
unknown's avatar
unknown committed
7613
	{
unknown's avatar
unknown committed
7614
          LEX *lex= Lex;
7615 7616 7617 7618
          lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
          if (!(lex->exchange= new sql_exchange($3.str, 0)) ||
              !(lex->result= new select_export(lex->exchange)))
            YYABORT;
unknown's avatar
unknown committed
7619 7620
	}
	opt_field_term opt_line_term
unknown's avatar
unknown committed
7621
	| INTO DUMPFILE TEXT_STRING_filesystem
unknown's avatar
unknown committed
7622
	{
7623
	  LEX *lex=Lex;
unknown's avatar
unknown committed
7624 7625
	  if (!lex->describe)
	  {
7626
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
7627 7628 7629 7630 7631
	    if (!(lex->exchange= new sql_exchange($3.str,1)))
	      YYABORT;
	    if (!(lex->result= new select_dump(lex->exchange)))
	      YYABORT;
	  }
unknown's avatar
unknown committed
7632
	}
7633
        | INTO select_var_list_init
unknown's avatar
unknown committed
7634
	{
7635
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
7636 7637
	}
        ;
unknown's avatar
unknown committed
7638

unknown's avatar
unknown committed
7639 7640 7641
/*
  DO statement
*/
unknown's avatar
unknown committed
7642

7643
do:	DO_SYM
unknown's avatar
unknown committed
7644 7645 7646
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
7647 7648 7649 7650 7651
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
unknown's avatar
unknown committed
7652
	}
7653 7654
	;

unknown's avatar
unknown committed
7655
/*
7656
  Drop : delete tables or index or user
unknown's avatar
unknown committed
7657 7658 7659
*/

drop:
unknown's avatar
unknown committed
7660
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
unknown's avatar
unknown committed
7661
	{
unknown's avatar
unknown committed
7662 7663
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
7664 7665
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
unknown's avatar
unknown committed
7666
	}
unknown's avatar
unknown committed
7667
	| DROP INDEX_SYM ident ON table_ident {}
unknown's avatar
unknown committed
7668
	  {
unknown's avatar
unknown committed
7669 7670
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
7671 7672 7673
	     lex->alter_info.drop_list.empty();
	     lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
                                                                $3.str));
unknown's avatar
unknown committed
7674 7675
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
unknown's avatar
unknown committed
7676 7677 7678 7679
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
unknown's avatar
unknown committed
7680 7681 7682 7683
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
	    lex->name=$4.str;
unknown's avatar
unknown committed
7684
	 }
7685
	| DROP FUNCTION_SYM if_exists sp_name
unknown's avatar
unknown committed
7686
	  {
unknown's avatar
unknown committed
7687
	    LEX *lex=Lex;
7688 7689
	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
7690
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
7691 7692
	      YYABORT;
	    }
unknown's avatar
unknown committed
7693
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
7694 7695 7696 7697 7698 7699 7700 7701
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
	  }
	| DROP PROCEDURE if_exists sp_name
	  {
	    LEX *lex=Lex;
	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
7702
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
7703 7704 7705 7706 7707
	      YYABORT;
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
7708
	  }
7709
	| DROP USER clear_privileges user_list
7710
	  {
7711 7712
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
7713 7714
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
7715
	    LEX *lex= Lex;
7716 7717 7718
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
7719 7720 7721 7722 7723 7724
        | DROP EVENT_SYM if_exists sp_name
          {
            LEX *lex=Lex;

            if (lex->et)
            {
unknown's avatar
unknown committed
7725 7726 7727 7728
              /*
                Recursive events are not possible because recursive SPs
                are not also possible. lex->sp_head is not stacked.
              */
7729 7730 7731 7732
              my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "EVENT");
              YYABORT;
            }

7733
            if (!(lex->et= new (YYTHD->mem_root) Event_timed()))
unknown's avatar
unknown committed
7734
              YYABORT;
7735
	  
7736 7737 7738 7739 7740 7741
            if (!lex->et_compile_phase)
            {
              lex->et->init_name(YYTHD, $4);
              lex->et->init_definer(YYTHD);
            }

7742 7743 7744
            lex->sql_command = SQLCOM_DROP_EVENT;
            lex->drop_if_exists= $3;
          }
7745
        | DROP TRIGGER_SYM sp_name
7746 7747 7748
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_DROP_TRIGGER;
7749
            lex->spname= $3;
unknown's avatar
unknown committed
7750 7751 7752 7753 7754 7755 7756 7757 7758 7759
	  }
        | 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;
7760 7761
          }
	;
unknown's avatar
unknown committed
7762 7763

table_list:
7764
	table_name
unknown's avatar
unknown committed
7765
	| table_list ',' table_name;
unknown's avatar
unknown committed
7766

7767
table_name:
unknown's avatar
unknown committed
7768
	table_ident
unknown's avatar
unknown committed
7769 7770 7771 7772 7773
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
unknown's avatar
unknown committed
7774 7775

if_exists:
7776
	/* empty */ { $$= 0; }
7777 7778
	| IF EXISTS { $$= 1; }
	;
unknown's avatar
unknown committed
7779

7780 7781 7782 7783
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
unknown's avatar
unknown committed
7784 7785 7786 7787 7788
/*
** Insert : add new data to table
*/

insert:
7789 7790 7791
	INSERT
	{
	  LEX *lex= Lex;
7792 7793
	  lex->sql_command= SQLCOM_INSERT;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
7794
	  mysql_init_select(lex);
7795 7796 7797
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
	} insert_lock_option
7798
	opt_ignore insert2
unknown's avatar
unknown committed
7799
	{
unknown's avatar
unknown committed
7800
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
7801
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
7802
	}
7803
	insert_field_spec opt_insert_update
7804
	{}
unknown's avatar
unknown committed
7805
	;
unknown's avatar
unknown committed
7806 7807

replace:
7808 7809
	REPLACE
	{
7810
	  LEX *lex=Lex;
7811 7812
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
unknown's avatar
unknown committed
7813
	  mysql_init_select(lex);
7814
	}
unknown's avatar
unknown committed
7815 7816
	replace_lock_option insert2
	{
unknown's avatar
unknown committed
7817
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
7818
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
7819 7820
	}
	insert_field_spec
7821
	{}
unknown's avatar
unknown committed
7822
	;
unknown's avatar
unknown committed
7823 7824

insert_lock_option:
7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837
	/* empty */
          {
#ifdef HAVE_QUERY_CACHE
            /*
              If it is SP we do not allow insert optimisation whan result of
              insert visible only after the table unlocking but everyone can
              read table.
            */
            $$= (Lex->sphead ? TL_WRITE :TL_WRITE_CONCURRENT_INSERT);
#else
            $$= TL_WRITE_CONCURRENT_INSERT;
#endif
          }
unknown's avatar
unknown committed
7838 7839 7840
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
7841
	;
unknown's avatar
unknown committed
7842 7843

replace_lock_option:
unknown's avatar
unknown committed
7844 7845
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
unknown's avatar
unknown committed
7846 7847 7848

insert2:
	INTO insert_table {}
unknown's avatar
unknown committed
7849
	| insert_table {};
unknown's avatar
unknown committed
7850 7851

insert_table:
7852
	table_name
unknown's avatar
unknown committed
7853
	{
unknown's avatar
unknown committed
7854 7855 7856 7857
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
unknown's avatar
unknown committed
7858
	};
unknown's avatar
unknown committed
7859 7860

insert_field_spec:
unknown's avatar
unknown committed
7861 7862 7863
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
unknown's avatar
unknown committed
7864 7865
	| SET
	  {
unknown's avatar
unknown committed
7866 7867 7868
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
7869 7870
	      YYABORT;
	   }
unknown's avatar
unknown committed
7871
	   ident_eq_list;
unknown's avatar
unknown committed
7872 7873 7874

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
unknown's avatar
unknown committed
7875
	| insert_ident		{ Lex->field_list.push_back($1); };
unknown's avatar
unknown committed
7876 7877 7878

insert_values:
	VALUES	values_list  {}
unknown's avatar
unknown committed
7879
	| VALUE_SYM values_list  {}
unknown's avatar
unknown committed
7880 7881
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
7882
        ;
unknown's avatar
unknown committed
7883 7884 7885

values_list:
	values_list ','  no_braces
unknown's avatar
unknown committed
7886
	| no_braces;
unknown's avatar
unknown committed
7887 7888 7889 7890

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
unknown's avatar
unknown committed
7891
	ident_eq_value;
unknown's avatar
unknown committed
7892 7893

ident_eq_value:
7894
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
7895
	 {
unknown's avatar
unknown committed
7896 7897 7898
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
unknown's avatar
unknown committed
7899
	    YYABORT;
unknown's avatar
unknown committed
7900
	 };
unknown's avatar
unknown committed
7901 7902

equal:	EQ		{}
unknown's avatar
unknown committed
7903 7904 7905 7906 7907 7908 7909
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
unknown's avatar
unknown committed
7910 7911 7912 7913 7914 7915 7916 7917 7918

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
unknown's avatar
unknown committed
7919 7920
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
7921
	    YYABORT;
unknown's avatar
unknown committed
7922
	 };
unknown's avatar
unknown committed
7923 7924 7925

opt_values:
	/* empty */ {}
unknown's avatar
unknown committed
7926
	| values;
unknown's avatar
unknown committed
7927 7928

values:
unknown's avatar
unknown committed
7929
	values ','  expr_or_default
unknown's avatar
unknown committed
7930 7931 7932 7933
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
unknown's avatar
unknown committed
7934 7935 7936 7937 7938 7939 7940 7941
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
7942
	expr	  { $$= $1;}
unknown's avatar
unknown committed
7943
	| DEFAULT {$$= new Item_default_value(Lex->current_context()); }
unknown's avatar
unknown committed
7944
	;
unknown's avatar
unknown committed
7945

7946 7947
opt_insert_update:
        /* empty */
unknown's avatar
unknown committed
7948
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
unknown's avatar
unknown committed
7949
          KEY_SYM UPDATE_SYM insert_update_list
7950 7951
        ;

unknown's avatar
unknown committed
7952 7953 7954
/* Update rows in a table */

update:
7955 7956
	UPDATE_SYM
	{
7957
	  LEX *lex= Lex;
unknown's avatar
unknown committed
7958
	  mysql_init_select(lex);
7959
          lex->sql_command= SQLCOM_UPDATE;
unknown's avatar
unknown committed
7960
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
7961
	  lex->duplicates= DUP_ERROR; 
7962
        }
unknown's avatar
unknown committed
7963
        opt_low_priority opt_ignore join_table_list
unknown's avatar
unknown committed
7964
	SET update_list
unknown's avatar
unknown committed
7965
	{
7966 7967
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
unknown's avatar
unknown committed
7968
            lex->sql_command= SQLCOM_UPDATE_MULTI;
7969 7970 7971
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
7972 7973
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
7974 7975
	    YYABORT;
	  }
7976 7977 7978 7979 7980 7981
          /*
            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);
unknown's avatar
unknown committed
7982
	}
7983
	where_clause opt_order_clause delete_limit_clause {}
unknown's avatar
unknown committed
7984
	;
unknown's avatar
unknown committed
7985 7986

update_list:
unknown's avatar
unknown committed
7987 7988 7989 7990
	update_list ',' update_elem
	| update_elem;

update_elem:
unknown's avatar
unknown committed
7991
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
7992
	{
unknown's avatar
unknown committed
7993
	  if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
unknown's avatar
unknown committed
7994
	    YYABORT;
unknown's avatar
unknown committed
7995 7996 7997 7998 7999 8000 8001
	};

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
unknown's avatar
unknown committed
8002
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
8003
	  {
unknown's avatar
unknown committed
8004 8005 8006
	  LEX *lex= Lex;
	  if (lex->update_list.push_back($1) || 
	      lex->value_list.push_back($3))
unknown's avatar
unknown committed
8007
	      YYABORT;
unknown's avatar
unknown committed
8008
	  };
unknown's avatar
unknown committed
8009 8010

opt_low_priority:
8011
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
8012
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
8013 8014 8015 8016

/* Delete rows from a table */

delete:
8017
	DELETE_SYM
8018
	{
8019 8020
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
unknown's avatar
unknown committed
8021
	  mysql_init_select(lex);
unknown's avatar
unknown committed
8022
	  lex->lock_option= lex->thd->update_lock_default;
8023
	  lex->ignore= 0;
8024
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
8025
	}
unknown's avatar
unknown committed
8026 8027
	opt_delete_options single_multi {}
	;
8028 8029

single_multi:
unknown's avatar
unknown committed
8030 8031
 	FROM table_ident
	{
unknown's avatar
unknown committed
8032 8033
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
unknown's avatar
unknown committed
8034 8035 8036
	    YYABORT;
	}
	where_clause opt_order_clause
8037
	delete_limit_clause {}
unknown's avatar
unknown committed
8038
	| table_wild_list
unknown's avatar
unknown committed
8039
	  { mysql_init_multi_delete(Lex); }
unknown's avatar
unknown committed
8040
          FROM join_table_list where_clause
8041 8042 8043 8044
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
unknown's avatar
unknown committed
8045 8046
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
8047
	  USING join_table_list where_clause
8048 8049 8050 8051
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
8052
	;
unknown's avatar
unknown committed
8053 8054 8055

table_wild_list:
	  table_wild_one {}
unknown's avatar
unknown committed
8056
	  | table_wild_list ',' table_wild_one {};
unknown's avatar
unknown committed
8057 8058

table_wild_one:
unknown's avatar
unknown committed
8059
	ident opt_wild opt_table_alias
unknown's avatar
unknown committed
8060
	{
unknown's avatar
unknown committed
8061 8062
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
					 TL_OPTION_UPDATING, Lex->lock_option))
unknown's avatar
unknown committed
8063 8064
	    YYABORT;
        }
unknown's avatar
unknown committed
8065
	| ident '.' ident opt_wild opt_table_alias
unknown's avatar
unknown committed
8066
	  {
unknown's avatar
unknown committed
8067 8068
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
unknown's avatar
unknown committed
8069 8070
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
unknown's avatar
unknown committed
8071
	      YYABORT;
unknown's avatar
unknown committed
8072
	  }
unknown's avatar
unknown committed
8073
	;
unknown's avatar
unknown committed
8074 8075

opt_wild:
8076
	/* empty */	{}
unknown's avatar
unknown committed
8077
	| '.' '*'	{};
unknown's avatar
unknown committed
8078 8079


8080
opt_delete_options:
unknown's avatar
unknown committed
8081
	/* empty */	{}
unknown's avatar
unknown committed
8082
	| opt_delete_option opt_delete_options {};
8083 8084

opt_delete_option:
8085
	QUICK		{ Select->options|= OPTION_QUICK; }
8086
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
8087
	| IGNORE_SYM	{ Lex->ignore= 1; };
8088

8089
truncate:
unknown's avatar
unknown committed
8090
	TRUNCATE_SYM opt_table_sym table_name
8091
	{
8092
	  LEX* lex= Lex;
8093
	  lex->sql_command= SQLCOM_TRUNCATE;
8094
	  lex->select_lex.options= 0;
8095
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
8096 8097
	}
	;
8098

unknown's avatar
unknown committed
8099 8100
opt_table_sym:
	/* empty */
unknown's avatar
unknown committed
8101
	| TABLE_SYM;
8102

unknown's avatar
unknown committed
8103 8104
/* Show things */

8105 8106
show:	SHOW
	{
unknown's avatar
unknown committed
8107 8108
	  LEX *lex=Lex;
	  lex->wild=0;
8109 8110 8111
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
8112 8113
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
unknown's avatar
unknown committed
8114
	show_param
8115 8116
	{}
	;
unknown's avatar
unknown committed
8117 8118

show_param:
8119
         DATABASES wild_and_where
8120 8121 8122 8123 8124 8125 8126
         {
           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;
         }
8127
         | opt_full TABLES opt_db wild_and_where
8128 8129 8130 8131
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLES;
8132
             lex->select_lex.db= $3;
8133 8134 8135
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLE_NAMES))
               YYABORT;
           }
8136 8137 8138 8139 8140 8141 8142 8143 8144
         | 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;
           }
8145 8146 8147 8148 8149 8150 8151 8152 8153
         | opt_full EVENTS_SYM opt_db wild_and_where
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_EVENTS;
             lex->select_lex.db= $3;
             if (prepare_schema_table(YYTHD, lex, 0, SCH_EVENTS))
               YYABORT;
           }
8154
         | TABLE_SYM STATUS_SYM opt_db wild_and_where
8155 8156 8157 8158
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLE_STATUS;
8159
             lex->select_lex.db= $3;
8160 8161 8162
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLES))
               YYABORT;
           }
8163
        | OPEN_SYM TABLES opt_db wild_and_where
unknown's avatar
unknown committed
8164
	  {
8165
	    LEX *lex= Lex;
8166 8167
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_OPEN_TABLES;
8168
	    lex->select_lex.db= $3;
8169 8170
            if (prepare_schema_table(YYTHD, lex, 0, SCH_OPEN_TABLES))
              YYABORT;
8171
	  }
unknown's avatar
unknown committed
8172 8173 8174 8175 8176 8177 8178 8179
        | 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;
	  }
unknown's avatar
unknown committed
8180
	| ENGINE_SYM storage_engines 
unknown's avatar
unknown committed
8181 8182
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
8183
	| ENGINE_SYM ALL 
unknown's avatar
unknown committed
8184
	  { Lex->create_info.db_type= NULL; }
8185
	  show_engine_param
8186
	| opt_full COLUMNS from_or_in table_ident opt_db wild_and_where
unknown's avatar
unknown committed
8187
	  {
8188 8189 8190
 	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SELECT;
	    lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
8191 8192 8193
	    if ($5)
	      $4->change_db($5);
	    if (prepare_schema_table(YYTHD, lex, $4, SCH_COLUMNS))
unknown's avatar
unknown committed
8194 8195
	      YYABORT;
	  }
8196
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
unknown's avatar
unknown committed
8197 8198
	  TEXT_STRING_sys AND_SYM MASTER_LOG_POS_SYM EQ ulonglong_num
	  AND_SYM MASTER_SERVER_ID_SYM EQ
unknown's avatar
unknown committed
8199
	ulong_num
unknown's avatar
unknown committed
8200 8201 8202 8203
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
8204
	    Lex->mi.server_id = $16;
unknown's avatar
unknown committed
8205
          }
8206
        | master_or_binary LOGS_SYM
unknown's avatar
unknown committed
8207 8208
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
8209 8210 8211 8212 8213
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
unknown's avatar
unknown committed
8214
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
unknown's avatar
unknown committed
8215
          {
8216 8217
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
8218
          } opt_limit_clause_init
8219
        | keys_or_index from_or_in table_ident opt_db where_clause
8220 8221 8222 8223
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_KEYS;
8224 8225 8226
	    if ($4)
	      $3->change_db($4);
            if (prepare_schema_table(YYTHD, lex, $3, SCH_STATISTICS))
8227
              YYABORT;
unknown's avatar
unknown committed
8228
	  }
unknown's avatar
unknown committed
8229 8230 8231 8232 8233 8234 8235 8236
	| COLUMN_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_COLUMN_TYPES;
	  }
	| TABLE_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
unknown's avatar
unknown committed
8237
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
8238
	    WARN_DEPRECATED(yythd, "5.2", "SHOW TABLE TYPES", "'SHOW [STORAGE] ENGINES'");
unknown's avatar
unknown committed
8239 8240 8241 8242 8243
	  }
	| opt_storage ENGINES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
unknown's avatar
unknown committed
8244 8245 8246
            lex->orig_sql_command= SQLCOM_SHOW_AUTHORS;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_ENGINES))
              YYABORT;
unknown's avatar
unknown committed
8247
	  }
unknown's avatar
unknown committed
8248 8249 8250 8251 8252
	| AUTHORS_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_AUTHORS;
	  }
unknown's avatar
unknown committed
8253 8254 8255 8256 8257
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
8258
        | COUNT_SYM '(' '*' ')' WARNINGS
8259
          { (void) create_select_for_variable("warning_count"); }
8260
        | COUNT_SYM '(' '*' ')' ERRORS
8261
	  { (void) create_select_for_variable("error_count"); }
8262
        | WARNINGS opt_limit_clause_init
unknown's avatar
unknown committed
8263
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
8264
        | ERRORS opt_limit_clause_init
8265
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
8266
        | opt_var_type STATUS_SYM wild_and_where
8267
          {
8268 8269 8270
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS;
8271
            lex->option_type= $1;
8272 8273
            if (prepare_schema_table(YYTHD, lex, 0, SCH_STATUS))
              YYABORT;
8274
	  }
unknown's avatar
unknown committed
8275
        | INNOBASE_SYM STATUS_SYM
8276 8277 8278
          {
            LEX *lex= Lex;
            lex->sql_command = SQLCOM_SHOW_ENGINE_STATUS;
unknown's avatar
unknown committed
8279 8280 8281 8282 8283 8284
            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;
            }
8285
            WARN_DEPRECATED(yythd, "5.2", "SHOW INNODB STATUS", "'SHOW ENGINE INNODB STATUS'");
8286
	  }
unknown's avatar
unknown committed
8287
        | MUTEX_SYM STATUS_SYM
8288 8289
          {
	    LEX *lex= Lex;
8290
            lex->sql_command = SQLCOM_SHOW_ENGINE_MUTEX;
unknown's avatar
unknown committed
8291 8292 8293 8294 8295 8296
            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;
            }
8297
            WARN_DEPRECATED(yythd, "5.2", "SHOW MUTEX STATUS", "'SHOW ENGINE INNODB MUTEX'");
8298
	  }
unknown's avatar
unknown committed
8299 8300
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
8301
        | opt_var_type  VARIABLES wild_and_where
unknown's avatar
unknown committed
8302
	  {
8303 8304 8305
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_VARIABLES;
8306
            lex->option_type= $1;
8307 8308
            if (prepare_schema_table(YYTHD, lex, 0, SCH_VARIABLES))
              YYABORT;
8309
	  }
8310
        | charset wild_and_where
8311 8312 8313 8314 8315 8316 8317
          {
            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;
          }
8318
        | COLLATION_SYM wild_and_where
8319 8320 8321 8322 8323 8324 8325
          {
            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;
          }
8326
	| BERKELEY_DB_SYM LOGS_SYM
8327 8328 8329
	  {
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_ENGINE_LOGS;
unknown's avatar
unknown committed
8330 8331 8332 8333 8334 8335
            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;
            }
8336
	    WARN_DEPRECATED(yythd, "5.2", "SHOW BDB LOGS", "'SHOW ENGINE BDB LOGS'");
8337
	  }
unknown's avatar
unknown committed
8338
	| LOGS_SYM
8339 8340 8341
	  {
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_ENGINE_LOGS;
unknown's avatar
unknown committed
8342 8343 8344 8345 8346 8347
            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;
            }
8348
	    WARN_DEPRECATED(yythd, "5.2", "SHOW LOGS", "'SHOW ENGINE BDB LOGS'");
8349
	  }
8350 8351 8352 8353 8354
	| GRANTS
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    THD *thd= lex->thd;
8355
            Security_context *sctx= thd->security_ctx;
8356 8357 8358
	    LEX_USER *curr_user;
            if (!(curr_user= (LEX_USER*) thd->alloc(sizeof(st_lex_user))))
              YYABORT;
8359 8360 8361
            curr_user->user.str= sctx->priv_user;
            curr_user->user.length= strlen(sctx->priv_user);
            if (*sctx->priv_host != 0)
8362
            {
8363 8364
              curr_user->host.str= sctx->priv_host;
              curr_user->host.length= strlen(sctx->priv_host);
8365 8366 8367 8368 8369 8370
            }
            else
            {
              curr_user->host.str= (char *) "%";
              curr_user->host.length= 1;
            }
8371
            curr_user->password=null_lex_str;
8372 8373
	    lex->grant_user= curr_user;
	  }
unknown's avatar
unknown committed
8374
	| GRANTS FOR_SYM user
unknown's avatar
unknown committed
8375 8376 8377 8378
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
8379
	    lex->grant_user->password=null_lex_str;
unknown's avatar
unknown committed
8380
	  }
unknown's avatar
unknown committed
8381
	| CREATE DATABASE opt_if_not_exists ident
unknown's avatar
unknown committed
8382 8383
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
unknown's avatar
unknown committed
8384 8385
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
unknown's avatar
unknown committed
8386
	  }
unknown's avatar
unknown committed
8387 8388
        | CREATE TABLE_SYM table_ident
          {
8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399
            LEX *lex= Lex;
	    lex->sql_command = SQLCOM_SHOW_CREATE;
	    if (!lex->select_lex.add_table_to_list(YYTHD, $3, NULL,0))
	      YYABORT;
            lex->only_view= 0;
	  }
        | CREATE VIEW_SYM table_ident
          {
            LEX *lex= Lex;
	    lex->sql_command = SQLCOM_SHOW_CREATE;
	    if (!lex->select_lex.add_table_to_list(YYTHD, $3, NULL, 0))
unknown's avatar
unknown committed
8400
	      YYABORT;
8401
            lex->only_view= 1;
unknown's avatar
unknown committed
8402 8403 8404 8405
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
unknown's avatar
unknown committed
8406
          }
unknown's avatar
unknown committed
8407 8408 8409
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424
          }
	| 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;
	  }
8425
	| PROCEDURE STATUS_SYM wild_and_where
8426
	  {
8427 8428 8429
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_PROC;
8430 8431
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
8432 8433
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
8434
	  }
8435
	| FUNCTION_SYM STATUS_SYM wild_and_where
8436
	  {
8437 8438 8439
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_FUNC;
8440 8441
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
8442 8443
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
unknown's avatar
unknown committed
8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464
	  }
        | 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
          }
8465 8466
        | CREATE EVENT_SYM sp_name
          {
unknown's avatar
unknown committed
8467 8468
            Lex->sql_command = SQLCOM_SHOW_CREATE_EVENT;
            Lex->spname= $3;
8469
            Lex->et= new (YYTHD->mem_root) Event_timed();
8470 8471 8472
            if (!Lex->et)
              YYABORT;
            Lex->et->init_definer(YYTHD);
8473 8474
          }
      ;
unknown's avatar
unknown committed
8475

unknown's avatar
unknown committed
8476 8477
show_engine_param:
	STATUS_SYM
8478 8479 8480
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_STATUS; }
	| MUTEX_SYM
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_MUTEX; }
unknown's avatar
unknown committed
8481
	| LOGS_SYM
8482
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_LOGS; };
unknown's avatar
unknown committed
8483

8484 8485 8486 8487
master_or_binary:
	MASTER_SYM
	| BINARY;

unknown's avatar
unknown committed
8488 8489 8490 8491
opt_storage:
	/* empty */
	| STORAGE_SYM;

unknown's avatar
unknown committed
8492 8493
opt_db:
	/* empty */  { $$= 0; }
unknown's avatar
unknown committed
8494
	| from_or_in ident { $$= $2.str; };
unknown's avatar
unknown committed
8495

unknown's avatar
unknown committed
8496 8497
opt_full:
	/* empty */ { Lex->verbose=0; }
unknown's avatar
unknown committed
8498
	| FULL	    { Lex->verbose=1; };
unknown's avatar
unknown committed
8499

unknown's avatar
unknown committed
8500 8501
from_or_in:
	FROM
unknown's avatar
unknown committed
8502
	| IN_SYM;
unknown's avatar
unknown committed
8503

unknown's avatar
unknown committed
8504 8505
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
unknown's avatar
unknown committed
8506
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
unknown's avatar
unknown committed
8507 8508 8509

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

8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524
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();
        }
      ;

unknown's avatar
unknown committed
8525

unknown's avatar
unknown committed
8526 8527 8528 8529
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
8530 8531 8532 8533 8534 8535 8536 8537 8538
          LEX *lex= Lex;
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
          lex->sql_command= SQLCOM_SELECT;
          lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
          lex->select_lex.db= 0;
          lex->verbose= 0;
          if (prepare_schema_table(YYTHD, lex, $2, SCH_COLUMNS))
unknown's avatar
unknown committed
8539 8540
	    YYABORT;
	}
8541
	opt_describe_column {}
unknown's avatar
unknown committed
8542 8543 8544
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
8545
          {
unknown's avatar
unknown committed
8546
	    LEX *lex=Lex;
8547
	    lex->select_lex.options|= SELECT_DESCRIBE;
unknown's avatar
unknown committed
8548 8549
	  }
	;
unknown's avatar
unknown committed
8550 8551 8552

describe_command:
	DESC
unknown's avatar
unknown committed
8553
	| DESCRIBE;
unknown's avatar
unknown committed
8554

unknown's avatar
unknown committed
8555 8556 8557
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
unknown's avatar
unknown committed
8558
	| PARTITIONS_SYM { Lex->describe|= DESCRIBE_PARTITIONS; }
unknown's avatar
unknown committed
8559 8560
	;

unknown's avatar
unknown committed
8561

unknown's avatar
unknown committed
8562 8563 8564
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
unknown's avatar
unknown committed
8565
	| ident
unknown's avatar
unknown committed
8566
	  { Lex->wild= new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info); };
unknown's avatar
unknown committed
8567 8568 8569 8570 8571


/* flush things */

flush:
8572
	FLUSH_SYM opt_no_write_to_binlog
unknown's avatar
unknown committed
8573 8574
	{
	  LEX *lex=Lex;
8575
	  if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
8576
	  {
8577 8578 8579 8580 8581 8582
            /*
              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");
8583 8584
	    YYABORT;
	  }
unknown's avatar
unknown committed
8585
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
unknown's avatar
unknown committed
8586
          lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
8587
	}
8588 8589 8590
	flush_options
	{}
	;
unknown's avatar
unknown committed
8591 8592 8593

flush_options:
	flush_options ',' flush_option
unknown's avatar
unknown committed
8594
	| flush_option;
unknown's avatar
unknown committed
8595 8596

flush_option:
8597
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
unknown's avatar
unknown committed
8598
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
unknown's avatar
unknown committed
8599
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
unknown's avatar
unknown committed
8600 8601 8602 8603
	| HOSTS_SYM	{ Lex->type|= REFRESH_HOSTS; }
	| PRIVILEGES	{ Lex->type|= REFRESH_GRANT; }
	| LOGS_SYM	{ Lex->type|= REFRESH_LOG; }
	| STATUS_SYM	{ Lex->type|= REFRESH_STATUS; }
unknown's avatar
unknown committed
8604 8605
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
8606 8607
	| DES_KEY_FILE	{ Lex->type|= REFRESH_DES_KEY_FILE; }
 	| RESOURCES     { Lex->type|= REFRESH_USER_RESOURCES; };
unknown's avatar
unknown committed
8608

unknown's avatar
unknown committed
8609
opt_table_list:
8610 8611
	/* empty */  {;}
	| table_list {;};
unknown's avatar
unknown committed
8612

unknown's avatar
unknown committed
8613
reset:
unknown's avatar
unknown committed
8614 8615 8616 8617
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
8618 8619 8620 8621
	} reset_options
	{}
	;

unknown's avatar
unknown committed
8622 8623
reset_options:
	reset_options ',' reset_option
unknown's avatar
unknown committed
8624
	| reset_option;
unknown's avatar
unknown committed
8625 8626

reset_option:
unknown's avatar
unknown committed
8627 8628
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
unknown's avatar
unknown committed
8629
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
unknown's avatar
unknown committed
8630

unknown's avatar
unknown committed
8631
purge:
unknown's avatar
unknown committed
8632 8633 8634 8635
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
8636 8637 8638 8639 8640
	} purge_options
	{}
	;

purge_options:
8641
	master_or_binary LOGS_SYM purge_option
unknown's avatar
unknown committed
8642
	;
8643 8644

purge_option:
unknown's avatar
unknown committed
8645
        TO_SYM TEXT_STRING_sys
8646 8647 8648 8649 8650 8651
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
8652 8653 8654 8655
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
unknown's avatar
unknown committed
8656
	}
unknown's avatar
unknown committed
8657
	;
unknown's avatar
unknown committed
8658

unknown's avatar
unknown committed
8659 8660 8661
/* kill threads */

kill:
unknown's avatar
unknown committed
8662
	KILL_SYM { Lex->sql_command= SQLCOM_KILL; } kill_option expr
unknown's avatar
unknown committed
8663
	{
unknown's avatar
unknown committed
8664
	  LEX *lex=Lex;
8665
	  lex->value_list.empty();
unknown's avatar
unknown committed
8666
	  lex->value_list.push_front($4);
unknown's avatar
unknown committed
8667
	};
unknown's avatar
unknown committed
8668

8669 8670 8671
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
unknown's avatar
unknown committed
8672 8673
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; }
        ;
8674

unknown's avatar
unknown committed
8675 8676 8677
/* change database */

use:	USE_SYM ident
unknown's avatar
unknown committed
8678 8679
	{
	  LEX *lex=Lex;
8680 8681
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
unknown's avatar
unknown committed
8682
	};
unknown's avatar
unknown committed
8683 8684 8685

/* import, export of files */

unknown's avatar
unknown committed
8686
load:   LOAD DATA_SYM
unknown's avatar
unknown committed
8687 8688
        {
          LEX *lex=Lex;
8689 8690 8691 8692 8693
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD DATA");
	    YYABORT;
	  }
unknown's avatar
unknown committed
8694 8695 8696 8697 8698 8699 8700
          lex->fname_start= lex->ptr;
        }
        load_data
        {}
        |
        LOAD TABLE_SYM table_ident FROM MASTER_SYM
        {
8701
	  LEX *lex=Lex;
8702 8703
          WARN_DEPRECATED(yythd, "5.2", "LOAD TABLE FROM MASTER",
                          "MySQL Administrator (mysqldump, mysql)");
8704
          if (lex->sphead)
8705 8706 8707 8708 8709
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD TABLE");
	    YYABORT;
	  }
          lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
unknown's avatar
unknown committed
8710 8711 8712 8713 8714
          if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
            YYABORT;
        };

load_data:
unknown's avatar
unknown committed
8715
	load_data_lock opt_local INFILE TEXT_STRING_filesystem
unknown's avatar
unknown committed
8716
	{
unknown's avatar
unknown committed
8717 8718
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
unknown's avatar
unknown committed
8719 8720
	  lex->lock_option= $1;
	  lex->local_file=  $2;
8721 8722
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
unknown's avatar
unknown committed
8723
	  if (!(lex->exchange= new sql_exchange($4.str, 0)))
unknown's avatar
unknown committed
8724
	    YYABORT;
unknown's avatar
unknown committed
8725 8726 8727 8728 8729
        }
        opt_duplicate INTO
        {
	  LEX *lex=Lex;
	  lex->fname_end= lex->ptr;
unknown's avatar
unknown committed
8730
	}
8731
        TABLE_SYM table_ident
unknown's avatar
unknown committed
8732
        {
8733 8734 8735 8736 8737 8738 8739
          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();
unknown's avatar
unknown committed
8740
        }
8741 8742 8743
        opt_field_term opt_line_term opt_ignore_lines opt_field_or_var_spec
        opt_load_data_set_spec
        {}
8744
        |
unknown's avatar
unknown committed
8745
	FROM MASTER_SYM
8746 8747
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
unknown's avatar
unknown committed
8748
        };
unknown's avatar
unknown committed
8749 8750 8751

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

unknown's avatar
unknown committed
8754
load_data_lock:
8755
	/* empty */	{ $$= YYTHD->update_lock_default; }
8756 8757 8758 8759 8760 8761 8762 8763 8764 8765
	| CONCURRENT
          {
#ifdef HAVE_QUERY_CACHE
            /*
              Ignore this option in SP to avoid problem with query cache
            */
            if (Lex->sphead != 0)
#endif
              $$= TL_WRITE_CONCURRENT_INSERT;
          }
unknown's avatar
unknown committed
8766
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
8767 8768


unknown's avatar
unknown committed
8769 8770 8771
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
8772
	| IGNORE_SYM	{ Lex->ignore= 1; };
unknown's avatar
unknown committed
8773 8774 8775

opt_field_term:
	/* empty */
unknown's avatar
unknown committed
8776
	| COLUMNS field_term_list;
unknown's avatar
unknown committed
8777 8778 8779

field_term_list:
	field_term_list field_term
unknown's avatar
unknown committed
8780
	| field_term;
unknown's avatar
unknown committed
8781 8782

field_term:
8783 8784
	TERMINATED BY text_string 
          {
8785
            DBUG_ASSERT(Lex->exchange != 0);
8786 8787
            Lex->exchange->field_term= $3;
          }
unknown's avatar
unknown committed
8788
	| OPTIONALLY ENCLOSED BY text_string
unknown's avatar
unknown committed
8789
	  {
unknown's avatar
unknown committed
8790
            LEX *lex= Lex;
8791
            DBUG_ASSERT(lex->exchange != 0);
unknown's avatar
unknown committed
8792 8793
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
unknown's avatar
unknown committed
8794
	  }
unknown's avatar
unknown committed
8795
        | ENCLOSED BY text_string
8796
          {
8797
            DBUG_ASSERT(Lex->exchange != 0);
8798 8799
            Lex->exchange->enclosed= $3;
          }
unknown's avatar
unknown committed
8800
        | ESCAPED BY text_string
8801
          {
8802
            DBUG_ASSERT(Lex->exchange != 0);
8803 8804
            Lex->exchange->escaped= $3;
          };
unknown's avatar
unknown committed
8805 8806 8807

opt_line_term:
	/* empty */
unknown's avatar
unknown committed
8808
	| LINES line_term_list;
unknown's avatar
unknown committed
8809 8810 8811

line_term_list:
	line_term_list line_term
unknown's avatar
unknown committed
8812
	| line_term;
unknown's avatar
unknown committed
8813 8814

line_term:
unknown's avatar
unknown committed
8815
        TERMINATED BY text_string
8816
          {
8817
            DBUG_ASSERT(Lex->exchange != 0);
8818 8819
            Lex->exchange->line_term= $3;
          }
unknown's avatar
unknown committed
8820
        | STARTING BY text_string
8821
          {
8822
            DBUG_ASSERT(Lex->exchange != 0);
8823 8824
            Lex->exchange->line_start= $3;
          };
unknown's avatar
unknown committed
8825 8826 8827

opt_ignore_lines:
	/* empty */
unknown's avatar
unknown committed
8828 8829
        | IGNORE_SYM NUM LINES
          {
8830
            DBUG_ASSERT(Lex->exchange != 0);
8831 8832
            Lex->exchange->skip_lines= atol($2.str);
          };
unknown's avatar
unknown committed
8833

unknown's avatar
unknown committed
8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844
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); }
        ;
unknown's avatar
unknown committed
8845

unknown's avatar
unknown committed
8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856
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  { };


unknown's avatar
unknown committed
8857 8858 8859
/* Common definitions */

text_literal:
8860
	TEXT_STRING_literal
unknown's avatar
unknown committed
8861 8862
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
8863
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
unknown's avatar
unknown committed
8864
	}
unknown's avatar
unknown committed
8865
	| NCHAR_STRING
8866
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
8867
	| UNDERSCORE_CHARSET TEXT_STRING
8868
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
8869
	| text_literal TEXT_STRING_literal
unknown's avatar
unknown committed
8870 8871
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
unknown's avatar
unknown committed
8872 8873

text_string:
8874
	TEXT_STRING_literal
unknown's avatar
unknown committed
8875
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
unknown's avatar
unknown committed
8876 8877
	| HEX_NUM
	  {
unknown's avatar
unknown committed
8878
	    Item *tmp= new Item_hex_string($1.str, $1.length);
8879
	    /*
unknown's avatar
unknown committed
8880
	      it is OK only emulate fix_fields, because we need only
8881 8882 8883
              value of constant
	    */
	    $$= tmp ?
unknown's avatar
unknown committed
8884
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
8885
	      (String*) 0;
8886
	  }
unknown's avatar
unknown committed
8887 8888 8889 8890 8891 8892 8893 8894 8895 8896
        | 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;
          }
8897 8898
	;

unknown's avatar
unknown committed
8899
param_marker:
8900
        PARAM_MARKER
unknown's avatar
unknown committed
8901
        {
8902 8903
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
8904 8905 8906
          Item_param *item= new Item_param((uint) (lex->tok_start -
                                                   (uchar *) thd->query));
          if (!($$= item) || lex->param_list.push_back(item))
unknown's avatar
unknown committed
8907
          {
8908
            my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
unknown's avatar
unknown committed
8909 8910
            YYABORT;
          }
8911 8912 8913
        }
	;

unknown's avatar
unknown committed
8914 8915 8916
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
8917 8918
	| '-' NUM_literal
	  {
8919
	    $2->max_length++;
unknown's avatar
unknown committed
8920
	    $$= $2->neg();
8921
	  }
unknown's avatar
unknown committed
8922 8923 8924
	;


unknown's avatar
unknown committed
8925 8926
literal:
	text_literal	{ $$ =	$1; }
unknown's avatar
unknown committed
8927
	| NUM_literal	{ $$ = $1; }
unknown's avatar
unknown committed
8928
	| NULL_SYM	{ $$ =	new Item_null();
8929
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
8930 8931
	| FALSE_SYM	{ $$= new Item_int((char*) "FALSE",0,1); }
	| TRUE_SYM	{ $$= new Item_int((char*) "TRUE",1,1); }
unknown's avatar
unknown committed
8932 8933
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
8934
	| UNDERSCORE_CHARSET HEX_NUM
8935
	  {
unknown's avatar
unknown committed
8936
	    Item *tmp= new Item_hex_string($2.str, $2.length);
8937
	    /*
unknown's avatar
unknown committed
8938
	      it is OK only emulate fix_fieds, because we need only
8939 8940 8941
              value of constant
	    */
	    String *str= tmp ?
unknown's avatar
unknown committed
8942
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
8943
	      (String*) 0;
unknown's avatar
unknown committed
8944 8945 8946
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
8947
	  }
unknown's avatar
unknown committed
8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961
	| 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);
          }
unknown's avatar
unknown committed
8962 8963
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
unknown's avatar
unknown committed
8964
	| TIMESTAMP text_literal { $$ = $2; };
unknown's avatar
unknown committed
8965

8966
NUM_literal:
8967 8968
	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); }
8969
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
unknown's avatar
unknown committed
8970
        | DECIMAL_NUM
8971
	{
unknown's avatar
unknown committed
8972
           $$= new Item_decimal($1.str, $1.length, YYTHD->charset());
8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
8986
	;
unknown's avatar
unknown committed
8987

unknown's avatar
unknown committed
8988
/**********************************************************************
unknown's avatar
unknown committed
8989
** Creating different items.
unknown's avatar
unknown committed
8990 8991 8992
**********************************************************************/

insert_ident:
8993
	simple_ident_nospvar { $$=$1; }
unknown's avatar
unknown committed
8994
	| table_wild	 { $$=$1; };
unknown's avatar
unknown committed
8995 8996

table_wild:
unknown's avatar
unknown committed
8997
	ident '.' '*'
8998
	{
8999
          SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
9000
	  $$ = new Item_field(Lex->current_context(), NullS, $1.str, "*");
9001
	  sel->with_wild++;
9002
	}
unknown's avatar
unknown committed
9003
	| ident '.' ident '.' '*'
9004
	{
9005
          SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
9006
	  $$ = new Item_field(Lex->current_context(), (YYTHD->client_capabilities &
unknown's avatar
unknown committed
9007 9008
                             CLIENT_NO_SCHEMA ? NullS : $1.str),
                             $3.str,"*");
9009
	  sel->with_wild++;
9010 9011
	}
	;
unknown's avatar
unknown committed
9012 9013

order_ident:
unknown's avatar
unknown committed
9014
	expr { $$=$1; };
unknown's avatar
unknown committed
9015 9016

simple_ident:
9017 9018 9019 9020 9021 9022
	ident
	{
	  sp_pvar_t *spv;
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;
	  if (spc && (spv = spc->find_pvar(&$1)))
9023 9024 9025
	  {
            /* We're compiling a stored procedure and found a variable */
            Item_splocal *splocal;
9026 9027
            splocal= new Item_splocal($1, spv->offset, spv->type,
                                      lex->tok_start_prev - 
9028
                                      lex->sphead->m_tmp_query);
unknown's avatar
unknown committed
9029
#ifndef DBUG_OFF
9030
            if (splocal)
9031
              splocal->m_sp= lex->sphead;
9032
#endif
9033
	    $$ = (Item*) splocal;
9034 9035 9036 9037 9038 9039 9040 9041
            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) ?
unknown's avatar
unknown committed
9042 9043
                 (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	         (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
9044 9045 9046 9047 9048 9049
	  }
        }
        | simple_ident_q { $$= $1; }
	;

simple_ident_nospvar:
unknown's avatar
unknown committed
9050
	ident
unknown's avatar
unknown committed
9051
	{
unknown's avatar
unknown committed
9052
	  SELECT_LEX *sel=Select;
9053
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
9054
	       sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
9055 9056
              (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	      (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
unknown's avatar
unknown committed
9057
	}
9058
	| simple_ident_q { $$= $1; }
unknown's avatar
unknown committed
9059
	;
9060 9061 9062

simple_ident_q:
	ident '.' ident
unknown's avatar
unknown committed
9063
	{
unknown's avatar
unknown committed
9064
	  THD *thd= YYTHD;
9065
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
9066

9067 9068 9069 9070 9071 9072
          /*
            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 &&
unknown's avatar
unknown committed
9073
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") ||
9074 9075
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
9076
            Item_trigger_field *trg_fld;
9077
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
unknown's avatar
unknown committed
9078

9079 9080 9081
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
unknown's avatar
unknown committed
9082
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
9083 9084
              YYABORT;
            }
unknown's avatar
unknown committed
9085

9086 9087 9088
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
unknown's avatar
unknown committed
9089
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
9090 9091
              YYABORT;
            }
unknown's avatar
unknown committed
9092

unknown's avatar
unknown committed
9093
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
9094
                                                  new_row ?
9095 9096
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
unknown's avatar
unknown committed
9097 9098
                                                  $3.str,
                                                  Item_trigger_field::AT_READ)))
9099
              YYABORT;
unknown's avatar
unknown committed
9100

9101 9102 9103 9104 9105 9106
            /*
              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);
unknown's avatar
unknown committed
9107

9108 9109 9110 9111 9112 9113 9114
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
9115 9116
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
9117 9118 9119
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
9120 9121
	        (Item*) new Item_field(Lex->current_context(), NullS, $1.str, $3.str) :
	        (Item*) new Item_ref(Lex->current_context(), NullS, $1.str, $3.str);
9122 9123
          }
        }
unknown's avatar
unknown committed
9124
	| '.' ident '.' ident
unknown's avatar
unknown committed
9125
	{
unknown's avatar
unknown committed
9126
	  THD *thd= YYTHD;
9127
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
9128
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
9129 9130
	  if (sel->no_table_names_allowed)
	  {
9131 9132
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $2.str, thd->where);
unknown's avatar
unknown committed
9133
	  }
9134
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
9135
	       sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
9136 9137
	      (Item*) new Item_field(Lex->current_context(), NullS, $2.str, $4.str) :
              (Item*) new Item_ref(Lex->current_context(), NullS, $2.str, $4.str);
unknown's avatar
unknown committed
9138
	}
unknown's avatar
unknown committed
9139
	| ident '.' ident '.' ident
unknown's avatar
unknown committed
9140
	{
unknown's avatar
unknown committed
9141
	  THD *thd= YYTHD;
9142
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
9143
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
9144 9145
	  if (sel->no_table_names_allowed)
	  {
9146 9147
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $3.str, thd->where);
unknown's avatar
unknown committed
9148
	  }
9149
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
9150
	       sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
9151
	      (Item*) new Item_field(Lex->current_context(),
9152
                                     (YYTHD->client_capabilities &
unknown's avatar
unknown committed
9153 9154
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
unknown's avatar
unknown committed
9155
	      (Item*) new Item_ref(Lex->current_context(),
9156
                                   (YYTHD->client_capabilities &
9157
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
unknown's avatar
unknown committed
9158
                                   $3.str, $5.str);
unknown's avatar
unknown committed
9159
	};
unknown's avatar
unknown committed
9160 9161 9162 9163


field_ident:
	ident			{ $$=$1;}
9164 9165 9166 9167 9168
	| ident '.' ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->db))
            {
9169
              my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
9170 9171
              YYABORT;
            }
9172 9173
            if (my_strcasecmp(table_alias_charset, $3.str,
                              table->table_name))
9174
            {
9175
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3.str);
9176 9177 9178 9179 9180 9181 9182 9183 9184
              YYABORT;
            }
            $$=$5;
          }
	| ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->alias))
            {
9185
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $1.str);
9186 9187 9188 9189
              YYABORT;
            }
            $$=$3;
          }
unknown's avatar
unknown committed
9190
	| '.' ident		{ $$=$2;}	/* For Delphi */;
unknown's avatar
unknown committed
9191 9192 9193

table_ident:
	ident			{ $$=new Table_ident($1); }
unknown's avatar
unknown committed
9194
	| ident '.' ident	{ $$=new Table_ident(YYTHD, $1,$3,0);}
9195 9196 9197
	| '.' ident		{ $$=new Table_ident($2);} /* For Delphi */
        ;

unknown's avatar
unknown committed
9198
table_ident_nodb:
unknown's avatar
unknown committed
9199
	ident			{ LEX_STRING db={(char*) any_db,3}; $$=new Table_ident(YYTHD, db,$1,0); }
9200
        ;
unknown's avatar
unknown committed
9201

unknown's avatar
unknown committed
9202
IDENT_sys:
9203 9204 9205 9206 9207
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
9208 9209
            {
              CHARSET_INFO *cs= system_charset_info;
9210
              int dummy_error;
9211 9212
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
9213
                                                   $1.length, &dummy_error);
9214 9215
              if (wlen < $1.length)
              {
9216 9217
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
9218 9219
                YYABORT;
              }
9220
	      $$= $1;
9221
            }
9222 9223 9224 9225
	    else
	      thd->convert_string(&$$, system_charset_info,
				  $1.str, $1.length, thd->charset());
	  }
unknown's avatar
unknown committed
9226 9227 9228 9229 9230 9231
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
9232 9233
	  if (thd->charset_is_system_charset)
	    $$= $1;
unknown's avatar
unknown committed
9234
	  else
unknown's avatar
unknown committed
9235 9236
	    thd->convert_string(&$$, system_charset_info,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
9237 9238 9239
	}
	;

9240
TEXT_STRING_literal:
unknown's avatar
unknown committed
9241 9242 9243
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
9244 9245
	  if (thd->charset_is_collation_connection)
	    $$= $1;
unknown's avatar
unknown committed
9246
	  else
unknown's avatar
unknown committed
9247 9248
	    thd->convert_string(&$$, thd->variables.collation_connection,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
9249 9250 9251 9252
	}
	;


unknown's avatar
unknown committed
9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264
TEXT_STRING_filesystem:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
	  if (thd->charset_is_character_set_filesystem)
	    $$= $1;
	  else
	    thd->convert_string(&$$, thd->variables.character_set_filesystem,
				$1.str, $1.length, thd->charset());
	}
	;

unknown's avatar
unknown committed
9265
ident:
unknown's avatar
unknown committed
9266
	IDENT_sys	    { $$=$1; }
unknown's avatar
unknown committed
9267 9268 9269 9270 9271 9272
	| READ_ONLY_SYM
	{
	  THD *thd= YYTHD;
	  $$.str= thd->strmake("read_only",9);
	  $$.length= 9;
	}
unknown's avatar
unknown committed
9273 9274
	| keyword
	{
unknown's avatar
unknown committed
9275 9276 9277
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
9278 9279
	}
	;
unknown's avatar
unknown committed
9280

9281 9282 9283 9284 9285 9286 9287 9288 9289 9290
label_ident:
	IDENT_sys	    { $$=$1; }
	| keyword_sp
	{
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
	}
	;

unknown's avatar
unknown committed
9291
ident_or_text:
unknown's avatar
unknown committed
9292
        ident                   { $$=$1;}
unknown's avatar
unknown committed
9293 9294
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
unknown's avatar
unknown committed
9295 9296 9297 9298

user:
	ident_or_text
	{
unknown's avatar
unknown committed
9299 9300
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
9301
	    YYABORT;
9302 9303 9304 9305
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
unknown's avatar
unknown committed
9306 9307
	| ident_or_text '@' ident_or_text
	  {
unknown's avatar
unknown committed
9308 9309
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
9310 9311
	      YYABORT;
	    $$->user = $1; $$->host=$3;
9312
	  }
unknown's avatar
unknown committed
9313
	| CURRENT_USER optional_braces
9314 9315
	{
          THD *thd= YYTHD;
9316
          Security_context *sctx= thd->security_ctx;
9317 9318
          if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
            YYABORT;
9319 9320 9321
          $$->user.str= sctx->priv_user;
          $$->user.length= strlen(sctx->priv_user);
          if (*sctx->priv_host != 0)
9322
          {
9323 9324
            $$->host.str= sctx->priv_host;
            $$->host.length= strlen(sctx->priv_host);
9325 9326 9327 9328 9329 9330 9331
          }
          else
          {
            $$->host.str= (char *) "%";
            $$->host.length= 1;
          }
	};
unknown's avatar
unknown committed
9332

9333
/* Keyword that we allow for identifiers (except SP labels) */
unknown's avatar
unknown committed
9334
keyword:
9335 9336
	keyword_sp		{}
	| ASCII_SYM		{}
unknown's avatar
unknown committed
9337
	| AUTHORS_SYM		{}
9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354
	| 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		{}
9355
        | INSTALL_SYM           {}
9356 9357 9358
	| LANGUAGE_SYM          {}
	| NO_SYM		{}
	| OPEN_SYM		{}
9359
        | PARSER_SYM            {}
9360
	| PARTITION_SYM		{}
9361
        | PLUGIN_SYM            {}
9362
        | PREPARE_SYM           {}
unknown's avatar
unknown committed
9363
	| REMOVE_SYM		{}
9364 9365 9366 9367 9368 9369 9370 9371
	| REPAIR		{}
	| RESET_SYM		{}
	| RESTORE_SYM		{}
	| ROLLBACK_SYM		{}
	| SAVEPOINT_SYM		{}
	| SECURITY_SYM		{}
	| SIGNED_SYM		{}
	| SLAVE			{}
9372
        | SONAME_SYM            {}
9373 9374 9375 9376
	| START_SYM		{}
	| STOP_SYM		{}
	| TRUNCATE_SYM		{}
	| UNICODE_SYM		{}
9377
        | UNINSTALL_SYM         {}
9378 9379 9380 9381 9382
        | XA_SYM                {}
	;

/*
 * Keywords that we allow for labels in SPs.
unknown's avatar
unknown committed
9383 9384 9385
 * Anything that's the beginning of a statement or characteristics
 * must be in keyword above, otherwise we get (harmful) shift/reduce
 * conflicts.
9386 9387
 */
keyword_sp:
unknown's avatar
unknown committed
9388
	ACTION			{}
unknown's avatar
unknown committed
9389
	| ADDDATE_SYM		{}
unknown's avatar
unknown committed
9390
	| AFTER_SYM		{}
9391
	| AGAINST		{}
unknown's avatar
unknown committed
9392
	| AGGREGATE_SYM		{}
9393
	| ALGORITHM_SYM		{}
unknown's avatar
unknown committed
9394
	| ANY_SYM		{}
unknown's avatar
unknown committed
9395
	| AT_SYM                {}
unknown's avatar
unknown committed
9396
	| AUTO_INC		{}
unknown's avatar
unknown committed
9397
	| AUTOEXTEND_SIZE_SYM   {}
unknown's avatar
unknown committed
9398 9399
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
unknown's avatar
unknown committed
9400
	| BERKELEY_DB_SYM	{}
unknown's avatar
unknown committed
9401
	| BINLOG_SYM		{}
unknown's avatar
unknown committed
9402 9403
	| BIT_SYM		{}
	| BOOL_SYM		{}
unknown's avatar
unknown committed
9404
	| BOOLEAN_SYM		{}
unknown's avatar
unknown committed
9405
	| BTREE_SYM		{}
9406
	| CASCADED              {}
unknown's avatar
unknown committed
9407
	| CHAIN_SYM		{}
9408
	| CHANGED		{}
9409
	| CIPHER_SYM		{}
unknown's avatar
unknown committed
9410
	| CLIENT_SYM		{}
unknown's avatar
unknown committed
9411
	| COALESCE		{}
9412
	| CODE_SYM              {}
unknown's avatar
unknown committed
9413
	| COLLATION_SYM		{}
9414
        | COLUMNS               {}
unknown's avatar
unknown committed
9415
	| COMMITTED_SYM		{}
9416
	| COMPACT_SYM		{}
9417
	| COMPLETION_SYM	{}
unknown's avatar
unknown committed
9418
	| COMPRESSED_SYM	{}
unknown's avatar
unknown committed
9419
	| CONCURRENT		{}
9420
	| CONSISTENT_SYM	{}
9421
	| CUBE_SYM		{}
unknown's avatar
unknown committed
9422
	| DATA_SYM		{}
unknown's avatar
unknown committed
9423
	| DATAFILE_SYM          {}
unknown's avatar
unknown committed
9424 9425 9426
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
9427
	| DEFINER_SYM		{}
unknown's avatar
unknown committed
9428
	| DELAY_KEY_WRITE_SYM	{}
unknown's avatar
unknown committed
9429 9430
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
9431
	| DISABLE_SYM		{}
unknown's avatar
unknown committed
9432
	| DISCARD		{}
unknown's avatar
unknown committed
9433
	| DISK_SYM              {}
unknown's avatar
unknown committed
9434 9435
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
unknown's avatar
unknown committed
9436
	| DYNAMIC_SYM		{}
9437
	| ENDS_SYM		{}
unknown's avatar
unknown committed
9438
	| ENUM			{}
unknown's avatar
unknown committed
9439
	| ENGINE_SYM		{}
unknown's avatar
unknown committed
9440
	| ENGINES_SYM		{}
9441
	| ERRORS		{}
unknown's avatar
unknown committed
9442
	| ESCAPE_SYM		{}
9443
	| EVENT_SYM		{}
unknown's avatar
unknown committed
9444
	| EVENTS_SYM		{}
unknown's avatar
unknown committed
9445 9446
	| EVERY_SYM             {}
	| EXPANSION_SYM         {}
unknown's avatar
unknown committed
9447
	| EXTENDED_SYM		{}
unknown's avatar
unknown committed
9448
	| EXTENT_SIZE_SYM       {}
9449
	| FAST_SYM		{}
9450
	| FOUND_SYM		{}
unknown's avatar
unknown committed
9451
	| ENABLE_SYM		{}
unknown's avatar
unknown committed
9452
	| FULL			{}
unknown's avatar
unknown committed
9453 9454 9455
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
9456
	| FRAC_SECOND_SYM	{}
9457
	| GEOMETRY_SYM		{}
9458
	| GEOMETRYCOLLECTION	{}
9459
	| GET_FORMAT		{}
unknown's avatar
unknown committed
9460
	| GRANTS		{}
unknown's avatar
unknown committed
9461
	| GLOBAL_SYM		{}
unknown's avatar
unknown committed
9462
	| HASH_SYM		{}
unknown's avatar
unknown committed
9463 9464 9465
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
9466
	| INVOKER_SYM		{}
unknown's avatar
unknown committed
9467
	| IMPORT		{}
unknown's avatar
unknown committed
9468
	| INDEXES		{}
unknown's avatar
unknown committed
9469
	| INITIAL_SIZE_SYM      {}
unknown's avatar
unknown committed
9470
	| ISOLATION		{}
unknown's avatar
unknown committed
9471
	| ISSUER_SYM		{}
unknown's avatar
unknown committed
9472
	| INNOBASE_SYM		{}
9473
	| INSERT_METHOD		{}
unknown's avatar
unknown committed
9474
	| RELAY_THREAD		{}
9475
	| LAST_SYM		{}
unknown's avatar
unknown committed
9476
	| LEAVES                {}
9477
	| LESS_SYM		{}
unknown's avatar
unknown committed
9478
	| LEVEL_SYM		{}
9479
	| LINESTRING		{}
9480
	| LIST_SYM		{}
unknown's avatar
unknown committed
9481
	| LOCAL_SYM		{}
unknown's avatar
unknown committed
9482
	| LOCKS_SYM		{}
unknown's avatar
unknown committed
9483
	| LOGFILE_SYM           {}
unknown's avatar
unknown committed
9484 9485 9486 9487 9488 9489 9490 9491 9492
	| LOGS_SYM		{}
	| MAX_ROWS		{}
	| MASTER_SYM		{}
	| MASTER_HOST_SYM	{}
	| MASTER_PORT_SYM	{}
	| MASTER_LOG_FILE_SYM	{}
	| MASTER_LOG_POS_SYM	{}
	| MASTER_USER_SYM	{}
	| MASTER_PASSWORD_SYM	{}
unknown's avatar
unknown committed
9493
	| MASTER_SERVER_ID_SYM  {}
unknown's avatar
unknown committed
9494
	| MASTER_CONNECT_RETRY_SYM	{}
unknown's avatar
unknown committed
9495 9496 9497 9498 9499 9500
	| MASTER_SSL_SYM	{}
	| MASTER_SSL_CA_SYM	{}
	| MASTER_SSL_CAPATH_SYM	{}
	| MASTER_SSL_CERT_SYM	{}
	| MASTER_SSL_CIPHER_SYM	{}
	| MASTER_SSL_KEY_SYM	{}
unknown's avatar
unknown committed
9501 9502
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
unknown's avatar
unknown committed
9503
	| MAX_SIZE_SYM          {}
unknown's avatar
unknown committed
9504
	| MAX_UPDATES_PER_HOUR	{}
9505
	| MAX_USER_CONNECTIONS_SYM {}
9506
	| MAX_VALUE_SYM         {}
9507
	| MEDIUM_SYM		{}
unknown's avatar
unknown committed
9508
	| MEMORY_SYM		{}
9509
	| MERGE_SYM		{}
unknown's avatar
unknown committed
9510
	| MICROSECOND_SYM	{}
9511
        | MIGRATE_SYM           {}
unknown's avatar
unknown committed
9512 9513 9514
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
unknown's avatar
unknown committed
9515
	| MODE_SYM		{}
unknown's avatar
unknown committed
9516
	| MONTH_SYM		{}
9517 9518 9519
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
9520
        | MUTEX_SYM             {}
9521
	| NAME_SYM              {}
unknown's avatar
unknown committed
9522
	| NAMES_SYM		{}
unknown's avatar
unknown committed
9523 9524
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
unknown's avatar
unknown committed
9525
	| NDBCLUSTER_SYM	{}
9526
	| NEXT_SYM		{}
unknown's avatar
unknown committed
9527
	| NEW_SYM		{}
unknown's avatar
unknown committed
9528 9529
	| NO_WAIT_SYM           {}
	| NODEGROUP_SYM         {}
9530
	| NONE_SYM		{}
unknown's avatar
unknown committed
9531
	| NVARCHAR_SYM		{}
unknown's avatar
unknown committed
9532
	| OFFSET_SYM		{}
unknown's avatar
unknown committed
9533
	| OLD_PASSWORD		{}
9534
	| ONE_SHOT_SYM		{}
9535
        | ONE_SYM               {}
unknown's avatar
unknown committed
9536
	| PACK_KEYS_SYM		{}
9537
	| PARTIAL		{}
unknown's avatar
unknown committed
9538
	| PARTITIONING_SYM	{}
9539
	| PARTITIONS_SYM	{}
unknown's avatar
unknown committed
9540
	| PASSWORD		{}
9541
        | PHASE_SYM             {}
9542
	| POINT_SYM		{}
9543
	| POLYGON		{}
9544
        | PRESERVE_SYM          {}
9545
	| PREV_SYM		{}
9546
        | PRIVILEGES            {}
unknown's avatar
unknown committed
9547 9548
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
9549
	| QUARTER_SYM		{}
unknown's avatar
unknown committed
9550
	| QUERY_SYM		{}
9551
	| QUICK			{}
unknown's avatar
unknown committed
9552
        | REBUILD_SYM           {}
9553
        | RECOVER_SYM           {}
unknown's avatar
unknown committed
9554 9555
	| REDO_BUFFER_SIZE_SYM	{}
	| REDOFILE_SYM  	{}
unknown's avatar
unknown committed
9556
        | REDUNDANT_SYM         {}
unknown's avatar
unknown committed
9557 9558
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
unknown's avatar
unknown committed
9559
	| RELOAD		{}
unknown's avatar
unknown committed
9560
	| REORGANIZE_SYM	{}
unknown's avatar
unknown committed
9561
	| REPEATABLE_SYM	{}
unknown's avatar
unknown committed
9562
	| REPLICATION		{}
9563
	| RESOURCES		{}
9564
        | RESUME_SYM            {}
9565
	| RETURNS_SYM           {}
9566
	| ROLLUP_SYM		{}
9567
	| ROUTINE_SYM		{}
unknown's avatar
unknown committed
9568 9569 9570
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
unknown's avatar
unknown committed
9571
	| RTREE_SYM		{}
unknown's avatar
unknown committed
9572
	| SCHEDULE_SYM		{}	
unknown's avatar
unknown committed
9573
	| SECOND_SYM		{}
9574
	| SERIAL_SYM		{}
unknown's avatar
unknown committed
9575 9576
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
9577
	| SIMPLE_SYM		{}
unknown's avatar
unknown committed
9578
	| SHARE_SYM		{}
unknown's avatar
unknown committed
9579
	| SHUTDOWN		{}
9580
	| SNAPSHOT_SYM		{}
unknown's avatar
unknown committed
9581
	| SOUNDS_SYM		{}
unknown's avatar
unknown committed
9582
	| SQL_CACHE_SYM		{}
unknown's avatar
unknown committed
9583
	| SQL_BUFFER_RESULT	{}
unknown's avatar
unknown committed
9584
	| SQL_NO_CACHE_SYM	{}
unknown's avatar
unknown committed
9585
	| SQL_THREAD		{}
9586
	| STARTS_SYM		{}
unknown's avatar
unknown committed
9587
	| STATUS_SYM		{}
unknown's avatar
unknown committed
9588
	| STORAGE_SYM		{}
unknown's avatar
unknown committed
9589
	| STRING_SYM		{}
unknown's avatar
unknown committed
9590
	| SUBDATE_SYM		{}
unknown's avatar
unknown committed
9591
	| SUBJECT_SYM		{}
9592 9593
	| SUBPARTITION_SYM	{}
	| SUBPARTITIONS_SYM	{}
unknown's avatar
unknown committed
9594
	| SUPER_SYM		{}
9595
        | SUSPEND_SYM           {}
9596
        | TABLES                {}
unknown's avatar
unknown committed
9597
	| TABLESPACE		{}
unknown's avatar
unknown committed
9598
	| TEMPORARY		{}
9599
	| TEMPTABLE_SYM		{}
unknown's avatar
unknown committed
9600
	| TEXT_SYM		{}
9601
	| THAN_SYM		{}
unknown's avatar
unknown committed
9602
	| TRANSACTION_SYM	{}
9603
	| TRIGGERS_SYM		{}
unknown's avatar
unknown committed
9604
	| TIMESTAMP		{}
9605 9606
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
unknown's avatar
unknown committed
9607
	| TIME_SYM		{}
unknown's avatar
unknown committed
9608
	| TYPES_SYM		{}
9609
        | TYPE_SYM              {}
unknown's avatar
unknown committed
9610
        | UDF_RETURNS_SYM       {}
9611
	| FUNCTION_SYM		{}
unknown's avatar
unknown committed
9612
	| UNCOMMITTED_SYM	{}
9613
	| UNDEFINED_SYM		{}
unknown's avatar
unknown committed
9614 9615
	| UNDO_BUFFER_SIZE_SYM	{}
	| UNDOFILE_SYM  	{}
9616
	| UNKNOWN_SYM		{}
9617
	| UNTIL_SYM		{}
9618
	| USER			{}
unknown's avatar
unknown committed
9619
	| USE_FRM		{}
unknown's avatar
unknown committed
9620
	| VARIABLES		{}
9621
	| VIEW_SYM		{}
9622
	| VALUE_SYM		{}
9623
	| WARNINGS		{}
unknown's avatar
unknown committed
9624
	| WAIT_SYM              {}
9625
	| WEEK_SYM		{}
unknown's avatar
unknown committed
9626
	| WORK_SYM		{}
9627
	| X509_SYM		{}
unknown's avatar
unknown committed
9628 9629
	| YEAR_SYM		{}
	;
unknown's avatar
unknown committed
9630 9631 9632 9633 9634 9635

/* Option functions */

set:
	SET opt_option
	{
unknown's avatar
unknown committed
9636 9637
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
unknown's avatar
unknown committed
9638
	  mysql_init_select(lex);
9639
	  lex->option_type=OPT_SESSION;
unknown's avatar
unknown committed
9640
	  lex->var_list.empty();
9641
          lex->one_shot_set= 0;
unknown's avatar
unknown committed
9642
	}
9643 9644 9645
	option_value_list
	{}
	;
unknown's avatar
unknown committed
9646 9647 9648

opt_option:
	/* empty */ {}
unknown's avatar
unknown committed
9649
	| OPTION {};
unknown's avatar
unknown committed
9650 9651

option_value_list:
9652 9653 9654 9655 9656 9657 9658 9659 9660 9661
	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
unknown's avatar
unknown committed
9662 9663
              and sp_instr_set_trigger instructions share one LEX.
              (Well, it is theoretically possible but adds some extra
9664 9665 9666 9667 9668 9669 9670 9671
               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;
unknown's avatar
unknown committed
9672

9673 9674 9675 9676 9677 9678 9679 9680 9681
            /* 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;
          }
        }
9682
	ext_option_value
9683 9684
        {
          LEX *lex= Lex;
unknown's avatar
unknown committed
9685

9686 9687 9688
          if (lex->sphead)
          {
            sp_head *sp= lex->sphead;
unknown's avatar
unknown committed
9689

9690 9691 9692 9693 9694 9695 9696 9697 9698
	    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;
unknown's avatar
unknown committed
9699

9700 9701 9702
              if (!(i= new sp_instr_stmt(sp->instructions(), lex->spcont,
                                         lex)))
                YYABORT;
unknown's avatar
unknown committed
9703

9704 9705 9706 9707
              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;
unknown's avatar
unknown committed
9708

9709 9710
              if (!(qbuff.str= alloc_root(YYTHD->mem_root, qbuff.length + 5)))
                YYABORT;
unknown's avatar
unknown committed
9711

9712 9713 9714 9715 9716 9717 9718 9719 9720
              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);
          }
        };
unknown's avatar
unknown committed
9721 9722

option_type:
9723 9724 9725 9726 9727 9728 9729 9730 9731
        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; }
unknown's avatar
unknown committed
9732 9733 9734 9735
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
unknown's avatar
unknown committed
9736
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
9737 9738 9739 9740 9741 9742
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

opt_var_ident_type:
	/* empty */		{ $$=OPT_DEFAULT; }
unknown's avatar
unknown committed
9743
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
9744 9745 9746
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
unknown's avatar
unknown committed
9747

9748 9749 9750
ext_option_value:
        sys_option_value
        | option_type2 option_value;
9751

9752 9753 9754 9755
sys_option_value:
        option_type internal_variable_name equal set_expr_or_default
        {
          LEX *lex=Lex;
9756

9757
          if ($2.var == trg_new_row_fake_var)
9758 9759 9760
          {
            /* We are in trigger and assigning value to field of new row */
            Item *it;
9761
            Item_trigger_field *trg_fld;
9762 9763
            sp_instr_set_trigger_field *sp_fld;
	    LINT_INIT(sp_fld);
9764 9765 9766 9767
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
9768
            }
9769 9770
            if ($4)
              it= $4;
9771
            else
9772 9773 9774 9775
            {
              /* QQ: Shouldn't this be field's default value ? */
              it= new Item_null();
            }
9776

unknown's avatar
unknown committed
9777
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
9778
                                                  Item_trigger_field::NEW_ROW,
unknown's avatar
unknown committed
9779 9780 9781
                                                  $2.base_name.str,
                                                  Item_trigger_field::AT_UPDATE)
                                                  ) ||
9782 9783 9784 9785 9786
                !(sp_fld= new sp_instr_set_trigger_field(lex->sphead->
                          	                         instructions(),
                                	                 lex->spcont,
							 trg_fld,
                                        	         it, lex)))
9787
              YYABORT;
9788

9789 9790 9791 9792
            /*
              Let us add this item to list of all Item_trigger_field
              objects in trigger.
            */
9793 9794
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
                                    (byte **)&trg_fld->next_trg_field);
9795

9796
            lex->sphead->add_instr(sp_fld);
9797 9798 9799 9800
          }
          else if ($2.var)
          { /* System variable */
            if ($1)
9801
              lex->option_type= $1;
9802 9803 9804 9805 9806 9807 9808 9809
            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;
9810
            sp_instr_set *sp_set;
9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825
            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();
9826 9827 9828
            sp_set= new sp_instr_set(lex->sphead->instructions(), ctx,
                                     spv->offset, it, spv->type, lex, TRUE);
            lex->sphead->add_instr(sp_set);
9829 9830 9831 9832 9833
          }
        }
        | option_type TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	{
	  LEX *lex=Lex;
9834
	  lex->option_type= $1;
9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846
	  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)));
	}
unknown's avatar
unknown committed
9847
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
9848 9849
	  {
	    LEX *lex=Lex;
9850
	    lex->var_list.push_back(new set_var($3, $4.var, &$4.base_name, $6));
unknown's avatar
unknown committed
9851
	  }
9852
	| charset old_or_new_charset_name_or_default
unknown's avatar
unknown committed
9853
	{
unknown's avatar
unknown committed
9854
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
9855
	  LEX *lex= Lex;
9856
	  $2= $2 ? $2: global_system_variables.character_set_client;
9857
	  lex->var_list.push_back(new set_var_collation_client($2,thd->variables.collation_database,$2));
unknown's avatar
unknown committed
9858
	}
9859 9860 9861 9862 9863 9864 9865 9866 9867 9868
        | 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);
9869 9870 9871
            else
              yyerror(ER(ER_SYNTAX_ERROR));

9872 9873
	    YYABORT;
	  }
9874
	| NAMES_SYM charset_name_or_default opt_collate
9875
	{
unknown's avatar
unknown committed
9876
	  LEX *lex= Lex;
9877
	  $2= $2 ? $2 : global_system_variables.character_set_client;
9878 9879
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
9880
	  {
9881 9882
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
unknown's avatar
unknown committed
9883
	    YYABORT;
9884
	  }
9885
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
9886
	}
unknown's avatar
unknown committed
9887
	| PASSWORD equal text_or_password
unknown's avatar
unknown committed
9888
	  {
9889
	    THD *thd=YYTHD;
unknown's avatar
unknown committed
9890
	    LEX_USER *user;
9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901
	    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;
	    }
unknown's avatar
unknown committed
9902
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
unknown's avatar
unknown committed
9903
	      YYABORT;
9904
	    user->host=null_lex_str;
9905
	    user->user.str=thd->security_ctx->priv_user;
9906
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
unknown's avatar
unknown committed
9907 9908
	  }
	| PASSWORD FOR_SYM user equal text_or_password
unknown's avatar
unknown committed
9909
	  {
unknown's avatar
unknown committed
9910 9911
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
9912
	;
unknown's avatar
unknown committed
9913

unknown's avatar
unknown committed
9914 9915 9916
internal_variable_name:
	ident
	{
9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928
	  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;
9929
	    $$.base_name= null_lex_str;
9930 9931 9932 9933
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
9934 9935 9936
            if (tmp == &sys_time_zone &&
                lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
9937 9938 9939 9940 9941 9942 9943 9944
            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;
            }
9945 9946 9947 9948 9949 9950 9951
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
unknown's avatar
unknown committed
9952
	}
9953 9954
	| ident '.' ident
	  {
9955
            LEX *lex= Lex;
9956 9957
            if (check_reserved_words(&$1))
            {
9958
	      yyerror(ER(ER_SYNTAX_ERROR));
9959 9960
              YYABORT;
            }
9961 9962 9963 9964 9965 9966
            if (lex->sphead && lex->sphead->m_type == TYPE_ENUM_TRIGGER &&
                (!my_strcasecmp(system_charset_info, $1.str, "NEW") || 
                 !my_strcasecmp(system_charset_info, $1.str, "OLD")))
            {
              if ($1.str[0]=='O' || $1.str[0]=='o')
              {
unknown's avatar
unknown committed
9967
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
9968 9969 9970 9971
                YYABORT;
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
unknown's avatar
unknown committed
9972 9973
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
9974 9975 9976 9977
                YYABORT;
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
unknown's avatar
unknown committed
9978
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
9979 9980 9981
                YYABORT;
              }
              /* This special combination will denote field of NEW row */
9982
              $$.var= trg_new_row_fake_var;
9983 9984 9985 9986 9987 9988 9989 9990
              $$.base_name= $3;
            }
            else
            {
              sys_var *tmp=find_sys_var($3.str, $3.length);
              if (!tmp)
                YYABORT;
              if (!tmp->is_struct())
9991
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
9992 9993 9994
              $$.var= tmp;
              $$.base_name= $1;
            }
9995 9996 9997 9998 9999 10000 10001
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
10002
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
unknown's avatar
unknown committed
10003 10004 10005
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
10006
	  }
10007
        ;
unknown's avatar
unknown committed
10008 10009 10010 10011 10012 10013 10014

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

unknown's avatar
unknown committed
10016 10017 10018 10019
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
10020
	    $$= $3.length ? YYTHD->variables.old_passwords ?
unknown's avatar
unknown committed
10021 10022 10023 10024 10025 10026 10027 10028
	        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;
10029 10030
	  }
          ;
unknown's avatar
unknown committed
10031

unknown's avatar
unknown committed
10032

unknown's avatar
unknown committed
10033
set_expr_or_default:
unknown's avatar
unknown committed
10034 10035
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
10036 10037
	| ON	  { $$=new Item_string("ON",  2, system_charset_info); }
	| ALL	  { $$=new Item_string("ALL", 3, system_charset_info); }
unknown's avatar
unknown committed
10038
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
unknown's avatar
unknown committed
10039
	;
unknown's avatar
unknown committed
10040 10041


unknown's avatar
unknown committed
10042 10043 10044 10045 10046
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
10047 10048 10049 10050
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
10051
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOCK");
10052 10053 10054
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_LOCK_TABLES;
unknown's avatar
unknown committed
10055
	}
10056 10057
	table_lock_list
	{}
10058
	;
unknown's avatar
unknown committed
10059 10060 10061

table_or_tables:
	TABLE_SYM
unknown's avatar
unknown committed
10062
	| TABLES;
unknown's avatar
unknown committed
10063 10064 10065

table_lock_list:
	table_lock
unknown's avatar
unknown committed
10066
	| table_lock_list ',' table_lock;
unknown's avatar
unknown committed
10067 10068 10069

table_lock:
	table_ident opt_table_alias lock_option
10070
	{
unknown's avatar
unknown committed
10071
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
10072 10073
	   YYABORT;
	}
10074
        ;
unknown's avatar
unknown committed
10075 10076 10077

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
10078
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
unknown's avatar
unknown committed
10079
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
10080 10081
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
unknown's avatar
unknown committed
10082 10083

unlock:
10084 10085 10086 10087 10088 10089
	UNLOCK_SYM
	{
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
10090
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "UNLOCK");
10091 10092 10093 10094 10095 10096
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_UNLOCK_TABLES;
	}
	table_or_tables
	{}
10097
        ;
unknown's avatar
unknown committed
10098 10099


10100
/*
unknown's avatar
unknown committed
10101
** Handler: direct access to ISAM functions
10102 10103 10104 10105 10106
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
10107
	  LEX *lex= Lex;
10108 10109 10110 10111 10112
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
10113
	  lex->sql_command = SQLCOM_HA_OPEN;
unknown's avatar
unknown committed
10114
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
10115 10116
	    YYABORT;
	}
unknown's avatar
unknown committed
10117
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
10118
	{
10119
	  LEX *lex= Lex;
10120 10121 10122 10123 10124
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
10125
	  lex->sql_command = SQLCOM_HA_CLOSE;
unknown's avatar
unknown committed
10126
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
10127 10128
	    YYABORT;
	}
unknown's avatar
unknown committed
10129
	| HANDLER_SYM table_ident_nodb READ_SYM
10130
	{
unknown's avatar
unknown committed
10131
	  LEX *lex=Lex;
10132 10133 10134 10135 10136
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
unknown's avatar
unknown committed
10137 10138
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
10139
	  lex->current_select->select_limit= new Item_int((int32) 1);
10140
	  lex->current_select->offset_limit= 0;
unknown's avatar
unknown committed
10141
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
10142
	    YYABORT;
unknown's avatar
unknown committed
10143
        }
10144
        handler_read_or_scan where_clause opt_limit_clause {}
10145
        ;
10146

unknown's avatar
unknown committed
10147
handler_read_or_scan:
10148 10149
	handler_scan_function         { Lex->ident= null_lex_str; }
        | ident handler_rkey_function { Lex->ident= $1; }
10150
        ;
unknown's avatar
unknown committed
10151 10152 10153

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
10154 10155
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
unknown's avatar
unknown committed
10156 10157 10158 10159 10160 10161

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;  }
10162 10163
	| handler_rkey_mode
	{
unknown's avatar
unknown committed
10164 10165
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
unknown's avatar
unknown committed
10166
	  lex->ha_rkey_mode=$1;
unknown's avatar
unknown committed
10167
	  if (!(lex->insert_list = new List_item))
10168
	    YYABORT;
10169 10170
	} '(' values ')' { }
        ;
10171 10172

handler_rkey_mode:
unknown's avatar
unknown committed
10173 10174 10175 10176
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
10177 10178
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
10179

unknown's avatar
unknown committed
10180 10181 10182
/* GRANT / REVOKE */

revoke:
10183
	REVOKE clear_privileges revoke_command
10184 10185 10186 10187
	{}
        ;

revoke_command:
10188
	grant_privileges ON opt_table grant_ident FROM grant_list
10189
	{
10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217
          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;
10218
        }
10219
	|
10220
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
10221 10222 10223
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
10224
	;
unknown's avatar
unknown committed
10225 10226

grant:
10227 10228 10229 10230 10231 10232
	GRANT clear_privileges grant_command
	{}
        ;

grant_command:
	grant_privileges ON opt_table grant_ident TO_SYM grant_list
10233
	require_clause grant_options
10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265
	{
          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;
        }
        ;
unknown's avatar
unknown committed
10266

10267 10268 10269 10270
opt_table:
	/* Empty */
	| TABLE_SYM ;
        
unknown's avatar
unknown committed
10271
grant_privileges:
10272 10273 10274 10275 10276 10277
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
10278
        ;
unknown's avatar
unknown committed
10279

unknown's avatar
unknown committed
10280 10281 10282 10283 10284
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

10285 10286 10287
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
unknown's avatar
unknown committed
10288

10289
object_privilege:
unknown's avatar
unknown committed
10290
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
10291 10292 10293
	| INSERT	{ Lex->which_columns = INSERT_ACL;} opt_column_list {}
	| UPDATE_SYM	{ Lex->which_columns = UPDATE_ACL; } opt_column_list {}
	| REFERENCES	{ Lex->which_columns = REFERENCES_ACL;} opt_column_list {}
unknown's avatar
unknown committed
10294 10295
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
unknown's avatar
unknown committed
10296
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
unknown's avatar
unknown committed
10297 10298 10299
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
unknown's avatar
unknown committed
10300
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
unknown's avatar
unknown committed
10301 10302 10303 10304
	| RELOAD	{ Lex->grant |= RELOAD_ACL;}
	| SHUTDOWN	{ Lex->grant |= SHUTDOWN_ACL;}
	| PROCESS	{ Lex->grant |= PROCESS_ACL;}
	| FILE_SYM	{ Lex->grant |= FILE_ACL;}
unknown's avatar
unknown committed
10305 10306 10307 10308 10309
	| 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; }
10310 10311 10312 10313
	| 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; }
10314 10315
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
10316
	| CREATE USER { Lex->grant |= CREATE_USER_ACL; }
10317
        | EVENT_SYM { Lex->grant |= EVENT_ACL;}
10318
        | TRIGGER_SYM { Lex->grant |= TRIGGER_ACL; }
unknown's avatar
unknown committed
10319
	;
unknown's avatar
unknown committed
10320

unknown's avatar
unknown committed
10321

10322 10323
opt_and:
	/* empty */	{}
unknown's avatar
unknown committed
10324
	| AND_SYM	{}
10325 10326 10327 10328 10329 10330 10331 10332 10333
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
unknown's avatar
unknown committed
10334 10335 10336 10337
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
unknown's avatar
unknown committed
10338
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
unknown's avatar
unknown committed
10339 10340 10341 10342 10343 10344 10345 10346 10347
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
unknown's avatar
unknown committed
10348
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
unknown's avatar
unknown committed
10349 10350 10351 10352 10353 10354 10355 10356 10357
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
unknown's avatar
unknown committed
10358
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
unknown's avatar
unknown committed
10359 10360 10361 10362 10363
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
10364

10365
grant_ident:
unknown's avatar
unknown committed
10366 10367
	'*'
	  {
10368
	    LEX *lex= Lex;
unknown's avatar
unknown committed
10369
	    lex->current_select->db= lex->thd->db;
unknown's avatar
unknown committed
10370
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
10371 10372
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
10373
	    {
unknown's avatar
unknown committed
10374 10375
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
10376
	      YYABORT;
unknown's avatar
unknown committed
10377
	    }
unknown's avatar
unknown committed
10378 10379 10380
	  }
	| ident '.' '*'
	  {
10381
	    LEX *lex= Lex;
unknown's avatar
unknown committed
10382
	    lex->current_select->db = $1.str;
unknown's avatar
unknown committed
10383
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
10384 10385
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
10386
	    {
unknown's avatar
unknown committed
10387 10388
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
10389 10390 10391 10392 10393
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
10394
	    LEX *lex= Lex;
unknown's avatar
unknown committed
10395
	    lex->current_select->db = NULL;
unknown's avatar
unknown committed
10396 10397
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
unknown's avatar
unknown committed
10398
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
10399
	    {
unknown's avatar
unknown committed
10400 10401
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
10402 10403 10404 10405 10406
	      YYABORT;
	    }
	  }
	| table_ident
	  {
unknown's avatar
unknown committed
10407
	    LEX *lex=Lex;
unknown's avatar
unknown committed
10408
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
unknown's avatar
unknown committed
10409
	      YYABORT;
unknown's avatar
unknown committed
10410
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
10411
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
10412 10413
	  }
          ;
unknown's avatar
unknown committed
10414 10415 10416


user_list:
10417 10418 10419 10420 10421 10422 10423 10424 10425 10426
	user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;


grant_list:
10427
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
10428
	| grant_list ',' grant_user
10429 10430 10431 10432 10433
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
unknown's avatar
unknown committed
10434 10435 10436 10437 10438 10439 10440 10441


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
10442
             if (YYTHD->variables.old_passwords)
10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459
             {
               char *buff= 
                 (char *) YYTHD->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH_323+1);
               if (buff)
                 make_scrambled_password_323(buff, $4.str);
               $1->password.str= buff;
               $1->password.length= SCRAMBLED_PASSWORD_CHAR_LENGTH_323;
             }
             else
             {
               char *buff= 
                 (char *) YYTHD->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH+1);
               if (buff)
                 make_scrambled_password(buff, $4.str);
               $1->password.str= buff;
               $1->password.length= SCRAMBLED_PASSWORD_CHAR_LENGTH;
             }
unknown's avatar
unknown committed
10460 10461 10462
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
10463
	  { $$= $1; $1->password= $5; }
unknown's avatar
unknown committed
10464
	| user
10465
	  { $$= $1; $1->password= null_lex_str; }
10466
        ;
unknown's avatar
unknown committed
10467 10468 10469


opt_column_list:
unknown's avatar
unknown committed
10470 10471 10472 10473 10474
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
unknown's avatar
unknown committed
10475
	| '(' column_list ')';
unknown's avatar
unknown committed
10476 10477 10478

column_list:
	column_list ',' column_list_id
unknown's avatar
unknown committed
10479
	| column_list_id;
unknown's avatar
unknown committed
10480 10481 10482 10483

column_list_id:
	ident
	{
unknown's avatar
unknown committed
10484
	  String *new_str = new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info);
unknown's avatar
unknown committed
10485 10486
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
unknown's avatar
unknown committed
10487
	  LEX *lex=Lex;
unknown's avatar
unknown committed
10488 10489
	  while ((point=iter++))
	  {
10490 10491
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
unknown's avatar
unknown committed
10492 10493
		break;
	  }
unknown's avatar
unknown committed
10494
	  lex->grant_tot_col|= lex->which_columns;
unknown's avatar
unknown committed
10495
	  if (point)
unknown's avatar
unknown committed
10496
	    point->rights |= lex->which_columns;
unknown's avatar
unknown committed
10497
	  else
unknown's avatar
unknown committed
10498
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
10499 10500
	}
        ;
unknown's avatar
unknown committed
10501

unknown's avatar
unknown committed
10502 10503

require_clause: /* empty */
10504
        | REQUIRE_SYM require_list
10505 10506 10507
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
10508
        | REQUIRE_SYM SSL_SYM
10509 10510 10511
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
10512
        | REQUIRE_SYM X509_SYM
10513 10514 10515 10516 10517 10518 10519
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
10520
          ;
unknown's avatar
unknown committed
10521

unknown's avatar
unknown committed
10522
grant_options:
unknown's avatar
unknown committed
10523
	/* empty */ {}
unknown's avatar
unknown committed
10524
	| WITH grant_option_list;
unknown's avatar
unknown committed
10525

unknown's avatar
unknown committed
10526 10527
grant_option_list:
	grant_option_list grant_option {}
10528 10529
	| grant_option {}
        ;
unknown's avatar
unknown committed
10530 10531 10532

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
unknown's avatar
unknown committed
10533
        | MAX_QUERIES_PER_HOUR ulong_num
unknown's avatar
unknown committed
10534
        {
unknown's avatar
unknown committed
10535 10536 10537
	  LEX *lex=Lex;
	  lex->mqh.questions=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
10538
	}
unknown's avatar
unknown committed
10539
        | MAX_UPDATES_PER_HOUR ulong_num
10540
        {
unknown's avatar
unknown committed
10541 10542 10543
	  LEX *lex=Lex;
	  lex->mqh.updates=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
10544
	}
unknown's avatar
unknown committed
10545
        | MAX_CONNECTIONS_PER_HOUR ulong_num
10546
        {
unknown's avatar
unknown committed
10547 10548 10549
	  LEX *lex=Lex;
	  lex->mqh.conn_per_hour= $2;
	  lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
10550
	}
unknown's avatar
unknown committed
10551
        | MAX_USER_CONNECTIONS_SYM ulong_num
10552
        {
unknown's avatar
unknown committed
10553 10554 10555
	  LEX *lex=Lex;
          lex->mqh.user_conn= $2;
          lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
10556 10557
	}
        ;
10558

unknown's avatar
unknown committed
10559
begin:
unknown's avatar
unknown committed
10560 10561 10562 10563 10564 10565 10566
	BEGIN_SYM  
        {
	  LEX *lex=Lex;
          lex->sql_command = SQLCOM_BEGIN;
          lex->start_transaction_opt= 0;
        }
        opt_work {}
10567
	;
unknown's avatar
unknown committed
10568 10569 10570

opt_work:
	/* empty */ {}
unknown's avatar
unknown committed
10571
	| WORK_SYM  {}
10572
        ;
unknown's avatar
unknown committed
10573

unknown's avatar
unknown committed
10574
opt_chain:
unknown's avatar
unknown committed
10575
	/* empty */ { $$= (YYTHD->variables.completion_type == 1); }
unknown's avatar
unknown committed
10576 10577 10578 10579 10580
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
unknown's avatar
unknown committed
10581
	/* empty */ { $$= (YYTHD->variables.completion_type == 2); }
unknown's avatar
unknown committed
10582 10583 10584 10585 10586 10587 10588 10589 10590
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

unknown's avatar
unknown committed
10591
commit:
unknown's avatar
unknown committed
10592
	COMMIT_SYM opt_work opt_chain opt_release
unknown's avatar
unknown committed
10593
	{
unknown's avatar
unknown committed
10594 10595 10596 10597
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_COMMIT;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
unknown's avatar
unknown committed
10598 10599
	}
	;
unknown's avatar
unknown committed
10600 10601

rollback:
unknown's avatar
unknown committed
10602
	ROLLBACK_SYM opt_work opt_chain opt_release
10603
	{
unknown's avatar
unknown committed
10604 10605 10606 10607
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
10608
	}
unknown's avatar
unknown committed
10609 10610
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
10611
	{
unknown's avatar
unknown committed
10612 10613 10614
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK_TO_SAVEPOINT;
	  lex->ident= $5;
unknown's avatar
unknown committed
10615 10616 10617
	}
	;

10618 10619 10620
savepoint:
	SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
10621 10622 10623
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SAVEPOINT;
	  lex->ident= $2;
unknown's avatar
unknown committed
10624 10625 10626 10627 10628 10629
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
10630 10631 10632
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RELEASE_SAVEPOINT;
	  lex->ident= $3;
unknown's avatar
unknown committed
10633 10634
	}
	;
unknown's avatar
unknown committed
10635
  
10636
/*
unknown's avatar
unknown committed
10637
   UNIONS : glue selects together
10638 10639 10640
*/


10641
union_clause:
10642
	/* empty */ {}
unknown's avatar
unknown committed
10643 10644
	| union_list
	;
10645 10646

union_list:
10647
	UNION_SYM union_option
10648 10649 10650 10651 10652
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
unknown's avatar
unknown committed
10653
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
10654
	    YYABORT;
10655
	  }
10656
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
10657
	  {
10658
            yyerror(ER(ER_SYNTAX_ERROR));
10659 10660
	    YYABORT;
	  }
10661
	  if (mysql_new_select(lex, 0))
10662
	    YYABORT;
unknown's avatar
unknown committed
10663
          mysql_init_select(lex);
10664
	  lex->current_select->linkage=UNION_TYPE;
10665 10666 10667
          if ($2) /* UNION DISTINCT - remember position */
            lex->current_select->master_unit()->union_distinct=
                                                      lex->current_select;
unknown's avatar
unknown committed
10668
	}
unknown's avatar
unknown committed
10669 10670
	select_init
        {
10671 10672 10673 10674
          /*
	    Remove from the name resolution context stack the context of the
            last select in the union.
	  */
unknown's avatar
unknown committed
10675 10676
          Lex->pop_context();
        }
10677
	;
10678 10679

union_opt:
unknown's avatar
unknown committed
10680 10681 10682
	/* Empty */ { $$= 0; }
	| union_list { $$= 1; }
	| union_order_or_limit { $$= 1; }
unknown's avatar
unknown committed
10683
	;
10684

unknown's avatar
unknown committed
10685
union_order_or_limit:
10686
	  {
unknown's avatar
unknown committed
10687
	    THD *thd= YYTHD;
10688
	    LEX *lex= thd->lex;
unknown's avatar
unknown committed
10689
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
unknown's avatar
unknown committed
10690
	    SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
10691
	    SELECT_LEX_UNIT *unit= sel->master_unit();
unknown's avatar
unknown committed
10692
	    SELECT_LEX *fake= unit->fake_select_lex;
unknown's avatar
unknown committed
10693 10694 10695 10696 10697 10698
	    if (fake)
	    {
	      unit->global_parameters= fake;
	      fake->no_table_names_allowed= 1;
	      lex->current_select= fake;
	    }
unknown's avatar
unknown committed
10699
	    thd->where= "global ORDER clause";
10700
	  }
10701
	order_or_limit
unknown's avatar
unknown committed
10702 10703
          {
	    THD *thd= YYTHD;
10704
	    thd->lex->current_select->no_table_names_allowed= 0;
unknown's avatar
unknown committed
10705 10706
	    thd->where= "";
          }
10707 10708 10709
	;

order_or_limit:
10710 10711
	order_clause opt_limit_clause_init
	| limit_clause
10712
	;
unknown's avatar
unknown committed
10713 10714

union_option:
10715 10716 10717 10718
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
10719

unknown's avatar
unknown committed
10720 10721
singlerow_subselect:
	subselect_start singlerow_subselect_init
unknown's avatar
unknown committed
10722 10723 10724 10725
	subselect_end
	{
	  $$= $2;
	};
10726

unknown's avatar
unknown committed
10727
singlerow_subselect_init:
10728
	select_init2
unknown's avatar
unknown committed
10729
	{
10730 10731
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
unknown's avatar
unknown committed
10732
	};
unknown's avatar
unknown committed
10733 10734

exists_subselect:
unknown's avatar
unknown committed
10735 10736 10737 10738 10739
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
unknown's avatar
unknown committed
10740 10741

exists_subselect_init:
10742
	select_init2
unknown's avatar
unknown committed
10743
	{
10744 10745
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
unknown's avatar
unknown committed
10746
	};
10747

unknown's avatar
unknown committed
10748 10749 10750 10751 10752 10753 10754 10755
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
10756
  select_init2
unknown's avatar
unknown committed
10757
  {
unknown's avatar
unknown committed
10758
    $$= Lex->current_select->master_unit()->first_select();
unknown's avatar
unknown committed
10759 10760
  };

10761
subselect_start:
10762
	'(' SELECT_SYM
unknown's avatar
unknown committed
10763
	{
10764
	  LEX *lex=Lex;
10765 10766
          if (lex->sql_command == (int)SQLCOM_HA_READ ||
              lex->sql_command == (int)SQLCOM_KILL)
10767 10768
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
10769 10770
	    YYABORT;
	  }
unknown's avatar
unknown committed
10771 10772 10773
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
10774 10775

subselect_end:
unknown's avatar
unknown committed
10776 10777 10778
	')'
	{
	  LEX *lex=Lex;
unknown's avatar
unknown committed
10779
          lex->pop_context();
10780
	  lex->current_select = lex->current_select->return_after_parsing();
unknown's avatar
unknown committed
10781
          lex->nest_level--;
unknown's avatar
unknown committed
10782
	};
unknown's avatar
unknown committed
10783

10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811
/**************************************************************************

 CREATE VIEW | TRIGGER | PROCEDURE statements.

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

view_or_trigger_or_sp:
	definer view_or_trigger_or_sp_tail
	{}
	| view_replace_or_algorithm definer view_tail
	{}
	;

view_or_trigger_or_sp_tail:
	view_tail
	{}
	| trigger_tail
	{}
	| sp_tail
	{}
	;

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

 DEFINER clause support.

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

10812
definer:
10813
	/* empty */
10814
	{
10815 10816 10817 10818 10819 10820 10821 10822
          /*
            We have to distinguish missing DEFINER-clause from case when
            CURRENT_USER specified as definer explicitly in order to properly
            handle CREATE TRIGGER statements which come to replication thread
            from older master servers (i.e. to create non-suid trigger in this
            case).
           */
          YYTHD->lex->definer= 0;
10823
	}
10824
	| DEFINER_SYM EQ CURRENT_USER optional_braces
10825
	{
10826 10827
          if (! (YYTHD->lex->definer= create_default_definer(YYTHD)))
            YYABORT;
10828 10829 10830
	}
	| DEFINER_SYM EQ ident_or_text '@' ident_or_text
	{
10831 10832
          if (!(YYTHD->lex->definer= create_definer(YYTHD, &$3, &$5)))
            YYABORT;
10833 10834 10835 10836 10837
	}
	;

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

10838
 CREATE VIEW statement parts.
10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897

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

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_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 */
	{}
10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913
	| '(' 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)));
	  }
	;

10914 10915 10916
view_select:
	SELECT_SYM remember_name select_init2
	{
10917 10918 10919 10920 10921 10922
          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;
10923 10924 10925
	}
	| '(' remember_name select_paren ')' union_opt
	{
10926 10927 10928 10929 10930 10931
          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;
10932
	}
10933 10934
	;

10935
view_check_option:
10936
	/* empty */
10937 10938 10939 10940 10941 10942 10943
	{ 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; }
10944
	;
10945

10946 10947 10948 10949 10950 10951 10952
/**************************************************************************

 CREATE TRIGGER statement parts.

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

trigger_tail:
10953
	TRIGGER_SYM remember_name sp_name trg_action_time trg_event
10954
	ON remember_name table_ident FOR_SYM remember_name EACH_SYM ROW_SYM
10955 10956 10957
	{
	  LEX *lex= Lex;
	  sp_head *sp;
10958

10959
	  if (lex->sphead)
10960
	  {
10961 10962
	    my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "TRIGGER");
	    YYABORT;
10963
	  }
10964

10965 10966 10967 10968
	  if (!(sp= new sp_head()))
	    YYABORT;
	  sp->reset_thd_mem_root(YYTHD);
	  sp->init(lex);
10969

10970
	  lex->stmt_definition_begin= $2;
10971
          lex->ident.str= $7;
10972
          lex->ident.length= $10 - $7;
10973

10974 10975 10976 10977 10978 10979 10980 10981
	  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 ';'.
	  */
unknown's avatar
unknown committed
10982
	  $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
10983
	  YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
10984

10985 10986 10987
	  bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	  lex->sphead->m_chistics= &lex->sp_chistics;
	  lex->sphead->m_body_begin= lex->ptr;
10988 10989
          while (my_isspace(system_charset_info, lex->sphead->m_body_begin[0]))
            ++lex->sphead->m_body_begin;
10990 10991 10992 10993 10994
	}
	sp_proc_stmt
	{
	  LEX *lex= Lex;
	  sp_head *sp= lex->sphead;
10995

10996 10997 10998
	  lex->sql_command= SQLCOM_CREATE_TRIGGER;
	  sp->init_strings(YYTHD, lex, $3);
	  /* Restore flag if it was cleared above */
unknown's avatar
unknown committed
10999

11000
	  YYTHD->client_capabilities |= $<ulong_num>13;
11001
	  sp->restore_thd_mem_root(YYTHD);
11002

11003 11004
	  if (sp->is_not_allowed_in_function("trigger"))
	      YYABORT;
11005

11006 11007 11008 11009 11010
	  /*
	    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.
	  */
11011
	  if (!lex->select_lex.add_table_to_list(YYTHD, $8,
11012 11013
	                                         (LEX_STRING*) 0,
	                                         TL_OPTION_UPDATING,
11014
                                                 TL_IGNORE))
11015 11016
	    YYABORT;
	}
11017 11018
	;

11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059
/**************************************************************************

 CREATE FUNCTION | PROCEDURE statements parts.

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

sp_tail:
	udf_func_type remember_name FUNCTION_SYM sp_name
	{
	  LEX *lex=Lex;
	  lex->udf.type= $1;
	  lex->stmt_definition_begin= $2;
	  lex->spname= $4;
	}
	create_function_tail
	{}
	| PROCEDURE remember_name sp_name
	{
	  LEX *lex= Lex;
	  sp_head *sp;

	  if (lex->sphead)
	  {
	    my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "PROCEDURE");
	    YYABORT;
	  }
	  
	  lex->stmt_definition_begin= $2;
	  
	  /* 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 ';'.
	   */
11060
          $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092
	  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;
11093 11094 11095 11096 11097 11098
          /*
            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;
11099 11100 11101 11102
	  sp->restore_thd_mem_root(YYTHD);
	}
	;

11103
/*************************************************************************/
11104

11105 11106
xa: XA_SYM begin_or_start xid opt_join_or_resume
      {
unknown's avatar
unknown committed
11107
        Lex->sql_command = SQLCOM_XA_START;
11108
      }
11109
    | XA_SYM END xid opt_suspend
11110
      {
unknown's avatar
unknown committed
11111
        Lex->sql_command = SQLCOM_XA_END;
11112 11113 11114
      }
    | XA_SYM PREPARE_SYM xid
      {
unknown's avatar
unknown committed
11115
        Lex->sql_command = SQLCOM_XA_PREPARE;
11116 11117 11118
      }
    | XA_SYM COMMIT_SYM xid opt_one_phase
      {
unknown's avatar
unknown committed
11119
        Lex->sql_command = SQLCOM_XA_COMMIT;
11120 11121 11122
      }
    | XA_SYM ROLLBACK_SYM xid
      {
unknown's avatar
unknown committed
11123
        Lex->sql_command = SQLCOM_XA_ROLLBACK;
11124 11125 11126
      }
    | XA_SYM RECOVER_SYM
      {
unknown's avatar
unknown committed
11127
        Lex->sql_command = SQLCOM_XA_RECOVER;
11128 11129 11130
      }
    ;

unknown's avatar
unknown committed
11131 11132
xid: text_string
     {
unknown's avatar
unknown committed
11133
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE);
unknown's avatar
unknown committed
11134 11135 11136 11137 11138 11139
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set(1L, $1->ptr(), $1->length(), 0, 0);
     }
     | text_string ',' text_string
     {
unknown's avatar
unknown committed
11140
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
unknown's avatar
unknown committed
11141 11142 11143 11144 11145 11146
       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
     {
unknown's avatar
unknown committed
11147
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
unknown's avatar
unknown committed
11148 11149 11150 11151 11152
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set($5, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     ;
11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168

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

11169
opt_suspend:
11170 11171
    /* nothing */           { Lex->xa_opt=XA_NONE;        }
    | SUSPEND_SYM           { Lex->xa_opt=XA_SUSPEND;     }
11172 11173 11174 11175 11176
      opt_migrate
    ;

opt_migrate:
    /* nothing */           { }
11177 11178 11179
    | FOR_SYM MIGRATE_SYM   { Lex->xa_opt=XA_FOR_MIGRATE; }
    ;

11180
install:
unknown's avatar
unknown committed
11181
  INSTALL_SYM PLUGIN_SYM ident SONAME_SYM TEXT_STRING_sys
11182 11183 11184 11185 11186 11187
  {
    LEX *lex= Lex;
    lex->sql_command= SQLCOM_INSTALL_PLUGIN;
    lex->comment= $3;
    lex->ident= $5;
  };
11188

11189
uninstall:
unknown's avatar
unknown committed
11190
  UNINSTALL_SYM PLUGIN_SYM ident
11191 11192 11193 11194 11195
  {
    LEX *lex= Lex;
    lex->sql_command= SQLCOM_UNINSTALL_PLUGIN;
    lex->comment= $3;
  };