sql_yacc.yy 251 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"
unknown's avatar
unknown committed
41
#include <myisam.h>
42
#include <myisammrg.h>
43

44
int yylex(void *yylval, void *yythd);
unknown's avatar
unknown committed
45

46 47
const LEX_STRING null_lex_str={0,0};

unknown's avatar
unknown committed
48
#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
49

unknown's avatar
unknown committed
50
#define WARN_DEPRECATED(A,B)                                        \
unknown's avatar
unknown committed
51
  push_warning_printf(((THD *)yythd), MYSQL_ERROR::WARN_LEVEL_WARN, \
unknown's avatar
unknown committed
52
		      ER_WARN_DEPRECATED_SYNTAX,                    \
53
		      ER(ER_WARN_DEPRECATED_SYNTAX), (A), (B));
unknown's avatar
unknown committed
54

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

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

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

%{
114
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
unknown's avatar
unknown committed
115 116 117 118
%}

%pure_parser					/* We have threads */

unknown's avatar
unknown committed
119
%token  END_OF_INPUT
unknown's avatar
unknown committed
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
%token  ABORT_SYM
%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
%token  ATAN
unknown's avatar
unknown committed
140
%token  AUTHORS_SYM
141 142 143 144 145 146 147 148 149 150 151
%token  AUTO_INC
%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
152
%token  BIN_NUM
153 154 155 156 157 158 159 160 161 162 163 164 165 166
%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
167
%token  CASCADED
168
%token  CAST_SYM
unknown's avatar
unknown committed
169
%token  CHAIN_SYM
170 171 172 173 174 175 176 177 178 179
%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
180
%token  CODE_SYM
181 182 183 184 185 186 187
%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
188
%token  COMPACT_SYM
189 190 191 192
%token  COMPRESSED_SYM
%token  CONCAT
%token  CONCAT_WS
%token  CONCURRENT
193
%token  CONDITION_SYM
194 195 196
%token  CONNECTION_SYM
%token  CONSISTENT_SYM
%token  CONSTRAINT
197 198
%token  CONTAINS_SYM
%token  CONTINUE_SYM
199 200 201 202 203 204 205
%token  CONVERT_SYM
%token  CONVERT_TZ_SYM
%token  COUNT_SYM
%token  CREATE
%token  CROSS
%token  CUBE_SYM
%token  CURDATE
206
%token  CURRENT_USER
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
%token  CURSOR_SYM
%token  CURTIME
%token  DATABASE
%token  DATABASES
%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
222
%token  DECIMAL_NUM
223
%token  DECIMAL_SYM
224
%token  DECLARE_SYM
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
%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
%token  DISTINCT
%token  DIV_SYM
%token  DOUBLE_SYM
%token  DO_SYM
%token  DROP
%token  DUAL_SYM
%token  DUMPFILE
unknown's avatar
unknown committed
247
%token  DUPLICATE_SYM
248
%token  DYNAMIC_SYM
249
%token  EACH_SYM
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
%token  ELSEIF_SYM
%token  ELT_FUNC
%token  ENABLE_SYM
%token  ENCLOSED
%token  ENCODE_SYM
%token  ENCRYPT
%token  END
%token  ENGINES_SYM
%token  ENGINE_SYM
%token  ENUM
%token  EQ
%token  EQUAL_SYM
%token  ERRORS
%token  ESCAPED
%token  ESCAPE_SYM
%token  EVENTS_SYM
%token  EXECUTE_SYM
%token  EXISTS
268
%token  EXIT_SYM
269 270 271 272 273 274
%token  EXPANSION_SYM
%token  EXPORT_SET
%token  EXTENDED_SYM
%token  EXTRACT_SYM
%token  FALSE_SYM
%token  FAST_SYM
275
%token  FETCH_SYM
276 277 278 279 280 281 282 283 284 285 286
%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
287
%token  FOUND_SYM
288 289 290 291 292 293 294 295 296 297 298 299 300 301 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
%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
%token  INNER_SYM
%token  INNOBASE_SYM
336
%token  INOUT_SYM
337 338 339
%token  INSENSITIVE_SYM
%token  INSERT
%token  INSERT_METHOD
340
%token  INSTALL_SYM
341 342 343
%token  INTERVAL_SYM
%token  INTO
%token  INT_SYM
344
%token  INVOKER_SYM
345 346 347 348 349 350 351 352 353 354
%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
355
%token  LANGUAGE_SYM
356 357 358 359 360 361 362 363
%token  LAST_INSERT_ID
%token  LAST_SYM
%token  LE
%token  LEADING
%token  LEAST_SYM
%token  LEAVES
%token  LEAVE_SYM
%token  LEFT
364
%token  LESS_SYM
365 366 367 368
%token  LEVEL_SYM
%token  LEX_HOSTNAME
%token  LIKE
%token  LIMIT
369
%token  LINEAR_SYM
370 371 372
%token  LINEFROMTEXT
%token  LINES
%token  LINESTRING
373
%token  LIST_SYM
374 375 376
%token  LOAD
%token  LOCAL_SYM
%token  LOCATE
377
%token  LOCATOR_SYM
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
%token  LOCKS_SYM
%token  LOCK_SYM
%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
%token  MAX_SYM
%token  MAX_UPDATES_PER_HOUR
unknown's avatar
unknown committed
412
%token  MAX_USER_CONNECTIONS_SYM
413
%token  MAX_VALUE_SYM
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
%token  MEDIUMBLOB
%token  MEDIUMINT
%token  MEDIUMTEXT
%token  MEDIUM_SYM
%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
437
%token  MUTEX_SYM
438 439 440 441 442 443
%token  NAMES_SYM
%token  NAME_SYM
%token  NATIONAL_SYM
%token  NATURAL
%token  NCHAR_STRING
%token  NCHAR_SYM
unknown's avatar
unknown committed
444
%token  NDBCLUSTER_SYM
445 446 447
%token  NE
%token  NEW_SYM
%token  NEXT_SYM
448
%token  NODEGROUP_SYM
449 450 451 452 453 454 455 456 457
%token  NONE_SYM
%token  NOT2_SYM
%token  NOT_SYM
%token  NOW_SYM
%token  NO_SYM
%token  NO_WRITE_TO_BINLOG
%token  NULL_SYM
%token  NUM
%token  NUMERIC_SYM
unknown's avatar
unknown committed
458
%token  NVARCHAR_SYM
459 460 461
%token  OFFSET_SYM
%token  OLD_PASSWORD
%token  ON
462
%token  ONE_SHOT_SYM
463 464 465 466 467 468 469 470 471 472 473
%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
474
%token  OUT_SYM
475
%token  PACK_KEYS_SYM
476
%token  PARSER_SYM
477
%token  PARTIAL
478 479
%token  PARTITION_SYM
%token  PARTITIONS_SYM
480
%token  PASSWORD
481
%token  PARAM_MARKER
482
%token  PHASE_SYM
483
%token  PLUGIN_SYM
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
%token  POINTFROMTEXT
%token  POINT_SYM
%token  POLYFROMTEXT
%token  POLYGON
%token  POSITION_SYM
%token  PRECISION
%token  PREPARE_SYM
%token  PREV_SYM
%token  PRIMARY_SYM
%token  PRIVILEGES
%token  PROCEDURE
%token  PROCESS
%token  PROCESSLIST_SYM
%token  PURGE
%token  QUARTER_SYM
%token  QUERY_SYM
%token  QUICK
%token  RAID_0_SYM
%token  RAID_CHUNKS
%token  RAID_CHUNKSIZE
%token  RAID_STRIPED_SYM
%token  RAID_TYPE
%token  RAND
507
%token  RANGE_SYM
508 509 510 511
%token  READS_SYM
%token  READ_SYM
%token  REAL
%token  RECOVER_SYM
unknown's avatar
unknown committed
512
%token  REDUNDANT_SYM
513 514 515 516 517
%token  REFERENCES
%token  REGEXP
%token  RELAY_LOG_FILE_SYM
%token  RELAY_LOG_POS_SYM
%token  RELAY_THREAD
unknown's avatar
unknown committed
518
%token  RELEASE_SYM
519 520
%token  RELOAD
%token  RENAME
unknown's avatar
unknown committed
521
%token  REORGANISE_SYM
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
%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
540
%token  ROUTINE_SYM
541 542 543 544 545 546 547 548
%token  ROWS_SYM
%token  ROW_COUNT_SYM
%token  ROW_FORMAT_SYM
%token  ROW_SYM
%token  RTREE_SYM
%token  SAVEPOINT_SYM
%token  SECOND_MICROSECOND_SYM
%token  SECOND_SYM
549
%token  SECURITY_SYM
550 551
%token  SELECT_SYM
%token  SENSITIVE_SYM
552
%token  SEPARATOR_SYM
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
%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
568
%token  SONAME_SYM
569 570
%token  SOUNDS_SYM
%token  SPATIAL_SYM
571 572 573 574
%token  SPECIFIC_SYM
%token  SQLEXCEPTION_SYM
%token  SQLSTATE_SYM
%token  SQLWARNING_SYM
575 576 577 578 579 580 581 582
%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
583
%token  SSL_SYM
584 585 586 587
%token  STARTING
%token  START_SYM
%token  STATUS_SYM
%token  STD_SYM
588
%token  STDDEV_SAMP_SYM
589 590 591 592 593 594
%token  STOP_SYM
%token  STORAGE_SYM
%token  STRAIGHT_JOIN
%token  STRING_SYM
%token  SUBDATE_SYM
%token  SUBJECT_SYM
595 596
%token  SUBPARTITION_SYM
%token  SUBPARTITIONS_SYM
597 598 599 600 601
%token  SUBSTRING
%token  SUBSTRING_INDEX
%token  SUM_SYM
%token  SUPER_SYM
%token  SUSPEND_SYM
602
%token  SYSDATE
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
%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
618
%token  THAN_SYM
619 620 621
%token  TO_SYM
%token  TRAILING
%token  TRANSACTION_SYM
622
%token  TRIGGER_SYM
623
%token  TRIGGERS_SYM
624 625 626
%token  TRIM
%token  TRUE_SYM
%token  TRUNCATE_SYM
unknown's avatar
unknown committed
627
%token  TYPES_SYM
628 629 630 631 632 633
%token  TYPE_SYM
%token  UDF_RETURNS_SYM
%token  ULONGLONG_NUM
%token  UNCOMMITTED_SYM
%token  UNDEFINED_SYM
%token  UNDERSCORE_CHARSET
634
%token  UNDO_SYM
635
%token  UNICODE_SYM
636
%token  UNINSTALL_SYM
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
%token  UNION_SYM
%token  UNIQUE_SYM
%token  UNIQUE_USERS
%token  UNIX_TIMESTAMP
%token  UNKNOWN_SYM
%token  UNLOCK_SYM
%token  UNSIGNED
%token  UNTIL_SYM
%token  UPDATE_SYM
%token  USAGE
%token  USER
%token  USE_FRM
%token  USE_SYM
%token  USING
%token  UTC_DATE_SYM
%token  UTC_TIMESTAMP_SYM
%token  UTC_TIME_SYM
654
%token  VAR_SAMP_SYM
655 656 657 658 659 660 661 662 663 664 665 666
%token  VALUES
%token  VALUE_SYM
%token  VARBINARY
%token  VARCHAR
%token  VARIABLES
%token  VARIANCE_SYM
%token  VARYING
%token  VIEW_SYM
%token  WARNINGS
%token  WEEK_SYM
%token  WHEN_SYM
%token  WHERE
667
%token  WHILE_SYM
668 669 670 671 672 673 674 675 676 677
%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
678

679
%left   JOIN_SYM INNER_SYM STRAIGHT_JOIN CROSS LEFT RIGHT
unknown's avatar
unknown committed
680 681
/* A dummy token to force the priority of table_ref production in a join. */
%left   TABLE_REF_PRIORITY
unknown's avatar
unknown committed
682
%left   SET_VAR
683 684
%left	OR_OR_SYM OR_SYM OR2_SYM XOR
%left	AND_SYM AND_AND_SYM
unknown's avatar
unknown committed
685 686 687 688 689 690
%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	'-' '+'
691
%left	'*' '/' '%' DIV_SYM MOD_SYM
unknown's avatar
unknown committed
692
%left   '^'
unknown's avatar
unknown committed
693
%left	NEG '~'
694
%right	NOT_SYM NOT2_SYM
unknown's avatar
unknown committed
695
%right	BINARY COLLATE_SYM
696

unknown's avatar
unknown committed
697
%type <lex_str>
unknown's avatar
unknown committed
698
        IDENT IDENT_QUOTED TEXT_STRING DECIMAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM
699
	LEX_HOSTNAME ULONGLONG_NUM field_ident select_alias ident ident_or_text
700
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
701
	NCHAR_STRING opt_component key_cache_name
702
        sp_opt_label BIN_NUM label_ident
unknown's avatar
unknown committed
703 704

%type <lex_str_ptr>
705
	opt_table_alias opt_fulltext_parser
unknown's avatar
unknown committed
706 707

%type <table>
708
	table_ident table_ident_nodb references xid
unknown's avatar
unknown committed
709 710

%type <simple_string>
711
	remember_name remember_end opt_ident opt_db text_or_password
712
	opt_constraint constraint ident_or_empty
unknown's avatar
unknown committed
713 714

%type <string>
715
	text_string opt_gconcat_separator
unknown's avatar
unknown committed
716 717

%type <num>
unknown's avatar
unknown committed
718
	type int_type real_type order_dir lock_option
unknown's avatar
unknown committed
719
	udf_type if_exists opt_local opt_table_options table_options
720 721
        table_option opt_if_not_exists opt_no_write_to_binlog
        delete_option opt_temporary all_or_any opt_distinct
722
        opt_ignore_leaves fulltext_options spatial_type union_option
unknown's avatar
unknown committed
723
        start_transaction_opts opt_chain opt_release
724
        union_opt select_derived_init option_type2
unknown's avatar
unknown committed
725 726

%type <ulong_num>
unknown's avatar
unknown committed
727
	ulong_num raid_types merge_insert_types
unknown's avatar
unknown committed
728

unknown's avatar
unknown committed
729 730
%type <ulonglong_number>
	ulonglong_num
unknown's avatar
unknown committed
731

unknown's avatar
unknown committed
732 733 734
%type <longlong_number>
        part_bit_expr

unknown's avatar
unknown committed
735 736 737
%type <lock_type>
	replace_lock_option opt_low_priority insert_lock_option load_data_lock

unknown's avatar
unknown committed
738
%type <item>
unknown's avatar
unknown committed
739
	literal text_literal insert_ident order_ident
unknown's avatar
unknown committed
740
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
741 742 743
	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
744
	expr_or_default set_expr_or_default interval_expr
unknown's avatar
unknown committed
745
	param_marker singlerow_subselect singlerow_subselect_init
unknown's avatar
unknown committed
746
	exists_subselect exists_subselect_init geometry_function
747
	signed_literal now_or_signed_literal opt_escape
748 749
	sp_opt_default
	simple_ident_nospvar simple_ident_q
750
        field_or_var limit_option
unknown's avatar
unknown committed
751
        part_func_expr
752 753 754

%type <item_num>
	NUM_literal
unknown's avatar
unknown committed
755 756

%type <item_list>
757 758
	expr_list udf_expr_list udf_expr_list2 when_list
	ident_list ident_list_arg
unknown's avatar
unknown committed
759

760 761 762
%type <var_type>
        option_type opt_var_type opt_var_ident_type

unknown's avatar
unknown committed
763
%type <key_type>
764
	key_type opt_unique_or_fulltext constraint_key_type
unknown's avatar
unknown committed
765

unknown's avatar
unknown committed
766 767 768
%type <key_alg>
	key_alg opt_btree_or_rtree

unknown's avatar
unknown committed
769
%type <string_list>
unknown's avatar
unknown committed
770
	key_usage_list using_list
unknown's avatar
unknown committed
771 772 773 774 775

%type <key_part>
	key_part

%type <table_list>
776
	join_table_list  join_table
777
        table_factor table_ref
unknown's avatar
unknown committed
778
        select_derived derived_table_list
unknown's avatar
unknown committed
779

780
%type <date_time_type> date_time_type;
unknown's avatar
unknown committed
781 782
%type <interval> interval

783 784
%type <interval_time_st> interval_time_st

unknown's avatar
unknown committed
785
%type <db_type> storage_engines
unknown's avatar
unknown committed
786 787 788

%type <row_type> row_types

unknown's avatar
unknown committed
789
%type <tx_isolation> isolation_types
unknown's avatar
unknown committed
790

unknown's avatar
unknown committed
791 792
%type <ha_rkey_mode> handler_rkey_mode

793
%type <cast_type> cast_type
unknown's avatar
unknown committed
794

unknown's avatar
unknown committed
795 796
%type <udf_type> udf_func_type

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

799
%type <lex_user> user grant_user get_definer
unknown's avatar
unknown committed
800

801
%type <charset>
802
	opt_collate
803 804
	charset_name
	charset_name_or_default
unknown's avatar
unknown committed
805 806
	old_or_new_charset_name
	old_or_new_charset_name_or_default
807 808
	collation_name
	collation_name_or_default
809

unknown's avatar
unknown committed
810 811
%type <variable> internal_variable_name

unknown's avatar
unknown committed
812
%type <select_lex> in_subselect in_subselect_init
unknown's avatar
unknown committed
813
	get_select_lex
unknown's avatar
unknown committed
814

unknown's avatar
unknown committed
815 816
%type <boolfunc2creator> comp_op

unknown's avatar
unknown committed
817
%type <NONE>
unknown's avatar
unknown committed
818
	query verb_clause create change select do drop insert replace insert2
819
	insert_values update delete truncate rename
unknown's avatar
unknown committed
820
	show describe load alter optimize keycache preload flush
unknown's avatar
unknown committed
821
	reset purge begin commit rollback savepoint release
822
	slave master_def master_defs master_file_def slave_until_opts
823
	repair restore backup analyze check start checksum
824
	field_list field_list_item field_spec kill column_def key_def
unknown's avatar
unknown committed
825
	keycache_list assign_to_keycache preload_list preload_keys
unknown's avatar
unknown committed
826
	select_item_list select_item values_list no_braces
827
	opt_limit_clause delete_limit_clause fields opt_values values
unknown's avatar
unknown committed
828
	procedure_list procedure_list2 procedure_item
829
	when_list2 expr_list2 udf_expr_list3 handler
unknown's avatar
unknown committed
830 831
	opt_precision opt_ignore opt_column opt_restrict
	grant revoke set lock unlock string_list field_options field_option
unknown's avatar
unknown committed
832
	field_opt_list opt_binary table_lock_list table_lock
833
	ref_list opt_on_delete opt_on_delete_list opt_on_delete_item use
unknown's avatar
unknown committed
834
	opt_delete_options opt_delete_option varchar nchar nvarchar
unknown's avatar
unknown committed
835
	opt_outer table_list table_name opt_option opt_place
unknown's avatar
unknown committed
836
	opt_attribute opt_attribute_list attribute column_list column_list_id
837
	opt_column_list grant_privileges grant_ident grant_list grant_option
838
	object_privilege object_privilege_list user_list rename_list
839
	clear_privileges flush_options flush_option
unknown's avatar
unknown committed
840 841
	equal optional_braces opt_key_definition key_usage_list2
	opt_mi_check_type opt_to mi_check_types normal_join
842
	table_to_table_list table_to_table opt_table_list opt_as
unknown's avatar
unknown committed
843
	handler_rkey_function handler_read_or_scan
unknown's avatar
unknown committed
844
	single_multi table_wild_list table_wild_one opt_wild
845
	union_clause union_list
846
	precision subselect_start opt_and charset
847
	subselect_end select_var_list select_var_list_init help opt_len
unknown's avatar
unknown committed
848
	opt_extended_describe
849
        prepare prepare_src execute deallocate
850
	statement sp_suid
851
	sp_c_chistics sp_a_chistics sp_chistic sp_c_chistic xa
unknown's avatar
unknown committed
852
        load_data opt_field_or_var_spec fields_or_vars opt_load_data_set_spec
853 854 855
        definer view_replace_or_algorithm view_replace view_algorithm_opt
        view_algorithm view_or_trigger_tail view_suid view_tail view_list_opt
        view_list view_select view_check_option trigger_tail
856
        install uninstall partition_entry
857
END_OF_INPUT
unknown's avatar
unknown committed
858

859 860 861 862 863 864 865
%type <NONE> call sp_proc_stmts sp_proc_stmts1 sp_proc_stmt
%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
866 867
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
868
	',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_SYM BETWEEN_SYM CASE_SYM
869
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM OR2_SYM AND_AND_SYM
unknown's avatar
unknown committed
870 871 872 873 874
%%


query:
	END_OF_INPUT
unknown's avatar
unknown committed
875
	{
876
	   THD *thd= YYTHD;
877
	   if (!thd->bootstrap &&
878
	      (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
879
	   {
unknown's avatar
unknown committed
880
	     my_message(ER_EMPTY_QUERY, ER(ER_EMPTY_QUERY), MYF(0));
881
	     YYABORT;
882
	   }
883 884
	   else
	   {
885
	     thd->lex->sql_command= SQLCOM_EMPTY_QUERY;
886
	   }
unknown's avatar
unknown committed
887
	}
unknown's avatar
unknown committed
888
	| verb_clause END_OF_INPUT {};
unknown's avatar
unknown committed
889 890

verb_clause:
891 892 893 894 895 896
	  statement
	| begin
	;

/* Verb clauses, except begin */
statement:
unknown's avatar
unknown committed
897 898
	  alter
	| analyze
899
	| backup
900
	| binlog_base64_event
901
	| call
unknown's avatar
unknown committed
902 903
	| change
	| check
904
	| checksum
unknown's avatar
unknown committed
905 906
	| commit
	| create
907
        | deallocate
unknown's avatar
unknown committed
908 909
	| delete
	| describe
unknown's avatar
unknown committed
910
	| do
unknown's avatar
unknown committed
911
	| drop
912
        | execute
unknown's avatar
unknown committed
913
	| flush
unknown's avatar
unknown committed
914
	| grant
unknown's avatar
unknown committed
915 916
	| handler
	| help
unknown's avatar
unknown committed
917
	| insert
918
        | install
unknown's avatar
unknown committed
919
	| kill
unknown's avatar
unknown committed
920 921 922
	| load
	| lock
	| optimize
unknown's avatar
unknown committed
923
        | keycache
924
        | partition_entry
unknown's avatar
unknown committed
925
	| preload
926
        | prepare
927
	| purge
unknown's avatar
unknown committed
928
	| release
929
	| rename
unknown's avatar
unknown committed
930
	| repair
unknown's avatar
unknown committed
931
	| replace
unknown's avatar
unknown committed
932
	| reset
933
	| restore
unknown's avatar
unknown committed
934 935
	| revoke
	| rollback
936
	| savepoint
unknown's avatar
unknown committed
937 938
	| select
	| set
unknown's avatar
unknown committed
939
	| show
unknown's avatar
unknown committed
940
	| slave
unknown's avatar
unknown committed
941
	| start
942
	| truncate
943
        | uninstall
unknown's avatar
unknown committed
944 945
	| unlock
	| update
unknown's avatar
unknown committed
946
	| use
947
	| xa
unknown's avatar
unknown committed
948
        ;
949

unknown's avatar
unknown committed
950
deallocate:
951
        deallocate_or_drop PREPARE_SYM ident
unknown's avatar
unknown committed
952 953
        {
          THD *thd=YYTHD;
954
          LEX *lex= thd->lex;
955
          if (lex->stmt_prepare_mode)
unknown's avatar
unknown committed
956 957 958 959
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
960
          lex->sql_command= SQLCOM_DEALLOCATE_PREPARE;
unknown's avatar
unknown committed
961 962 963
          lex->prepared_stmt_name= $3;
        };

964 965 966 967 968 969
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


unknown's avatar
unknown committed
970
prepare:
971
        PREPARE_SYM ident FROM prepare_src
unknown's avatar
unknown committed
972 973
        {
          THD *thd=YYTHD;
974
          LEX *lex= thd->lex;
975
          if (lex->stmt_prepare_mode)
unknown's avatar
unknown committed
976 977 978 979
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
980
          lex->sql_command= SQLCOM_PREPARE;
unknown's avatar
unknown committed
981 982 983
          lex->prepared_stmt_name= $2;
        };

984 985 986 987 988 989
prepare_src:
        TEXT_STRING_sys
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $1;
unknown's avatar
unknown committed
990
          lex->prepared_stmt_code_is_varref= FALSE;
991 992 993 994 995 996
        }
        | '@' ident_or_text
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $2;
unknown's avatar
unknown committed
997
          lex->prepared_stmt_code_is_varref= TRUE;
998
        };
999

unknown's avatar
unknown committed
1000 1001 1002 1003
execute:
        EXECUTE_SYM ident
        {
          THD *thd=YYTHD;
1004
          LEX *lex= thd->lex;
1005
          if (lex->stmt_prepare_mode)
unknown's avatar
unknown committed
1006 1007 1008 1009
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
1010
          lex->sql_command= SQLCOM_EXECUTE;
unknown's avatar
unknown committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
          lex->prepared_stmt_name= $2;
        }
        execute_using
        {}
        ;

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
1023 1024
        execute_var_list ',' execute_var_ident
        | execute_var_ident
unknown's avatar
unknown committed
1025 1026 1027 1028 1029 1030 1031 1032
        ;

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;
1033
        }
unknown's avatar
unknown committed
1034 1035
        ;

unknown's avatar
unknown committed
1036 1037
/* help */

1038
help:
1039 1040 1041 1042 1043 1044 1045 1046 1047
       HELP_SYM
       {
         if (Lex->sphead)
         {
           my_error(ER_SP_BADSTATEMENT, MYF(0), "HELP");
           YYABORT;
         }
       }
       ident_or_text
unknown's avatar
unknown committed
1048
       {
1049 1050
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
1051
	  lex->help_arg= $3.str;
1052
       };
unknown's avatar
unknown committed
1053 1054 1055 1056

/* change master */

change:
unknown's avatar
unknown committed
1057
       CHANGE MASTER_SYM TO_SYM
unknown's avatar
unknown committed
1058 1059 1060
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
unknown's avatar
unknown committed
1061
	  bzero((char*) &lex->mi, sizeof(lex->mi));
1062 1063 1064 1065
        }
       master_defs
	{}
       ;
unknown's avatar
unknown committed
1066 1067 1068

master_defs:
       master_def
unknown's avatar
unknown committed
1069
       | master_defs ',' master_def;
unknown's avatar
unknown committed
1070

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
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
1087
       MASTER_PORT_SYM EQ ulong_num
1088 1089 1090 1091
       {
	 Lex->mi.port = $3;
       }
       |
unknown's avatar
unknown committed
1092
       MASTER_CONNECT_RETRY_SYM EQ ulong_num
1093 1094 1095
       {
	 Lex->mi.connect_retry = $3;
       }
unknown's avatar
unknown committed
1096
       | MASTER_SSL_SYM EQ ulong_num
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
         {
           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
1150
       | RELAY_LOG_POS_SYM EQ ulong_num
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
         {
           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
1178
	  lex->create_info.db_type= lex->thd->variables.table_type;
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
	  lex->create_info.default_table_charset= NULL;
	  lex->name=0;
	}
	create2
	  { Lex->current_select= &Lex->select_lex; }
	| CREATE opt_unique_or_fulltext INDEX_SYM ident key_alg ON table_ident
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_CREATE_INDEX;
	    if (!lex->current_select->add_table_to_list(lex->thd, $7, NULL,
							TL_OPTION_UPDATING))
	      YYABORT;
	    lex->create_list.empty();
	    lex->key_list.empty();
	    lex->col_list.empty();
	    lex->change=NullS;
	  }
1196
	   '(' key_list ')' opt_fulltext_parser
1197 1198
	  {
	    LEX *lex=Lex;
1199 1200 1201 1202 1203 1204
	    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));
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
	    lex->col_list.empty();
	  }
	| CREATE DATABASE opt_if_not_exists ident
	  {
             Lex->create_info.default_table_charset= NULL;
             Lex->create_info.used_fields= 0;
          }
	  opt_create_database_options
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_CREATE_DB;
	    lex->name=$4.str;
            lex->create_info.options=$3;
	  }
	| CREATE udf_func_type FUNCTION_SYM sp_name
	  {
	    LEX *lex=Lex;
	    lex->spname= $4;
	    lex->udf.type= $2;
	  }
	  create_function_tail
	  {}
	| CREATE PROCEDURE sp_name
	  {
	    LEX *lex= Lex;
	    sp_head *sp;

	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
1234
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "PROCEDURE");
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
	      YYABORT;
	    }
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
	    sp->init(lex);

	    sp->m_type= TYPE_ENUM_PROCEDURE;
	    lex->sphead= sp;
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
	    sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
	    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;
	    /* Restore flag if it was cleared above */
	    if (sp->m_old_cmq)
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
	    sp->restore_thd_mem_root(YYTHD);
	  }
1287
	| CREATE
1288
	  {
1289 1290 1291
            Lex->create_view_mode= VIEW_CREATE_NEW;
            Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED;
            Lex->create_view_suid= TRUE;
1292
	  }
1293
	  view_or_trigger
1294
	  {}
1295 1296 1297 1298
	| CREATE USER clear_privileges grant_list
	  {
	    Lex->sql_command = SQLCOM_CREATE_USER;
          }
1299 1300
	;

1301 1302 1303 1304 1305 1306 1307
clear_privileges:
        /* Nothing */
        {
          LEX *lex=Lex;
          lex->users_list.empty();
          lex->columns.empty();
          lex->grant= lex->grant_tot_col= 0;
1308
	  lex->all_privileges= 0;
1309 1310 1311 1312 1313 1314 1315
          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));
        }
        ;

1316
sp_name:
1317
	  ident '.' ident
1318 1319 1320 1321
	  {
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
1322
	| ident
1323 1324 1325 1326 1327 1328
	  {
	    $$= sp_name_current_db_new(YYTHD, $1);
	  }
	;

create_function_tail:
1329
	  RETURNS_SYM udf_type SONAME_SYM TEXT_STRING_sys
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
	  {
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_CREATE_FUNCTION;
	    lex->udf.name = lex->spname->m_name;
	    lex->udf.returns=(Item_result) $2;
	    lex->udf.dl=$4.str;
	  }
	| '('
	  {
	    LEX *lex= Lex;
	    sp_head *sp;

	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
1344
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
	      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 ';'.
	     */
	    sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
	    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
1372 1373 1374 1375
	    lex->charset= NULL;
	    lex->length= lex->dec= NULL;
	    lex->interval_list.empty();
	    lex->type= 0;
1376 1377 1378 1379 1380
	  }
	  type
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
unknown's avatar
unknown committed
1381

1382 1383 1384 1385
            if (sp->fill_field_definition(YYTHD, lex,
                                          (enum enum_field_types) $8,
                                          &sp->m_return_field_def))
              YYABORT;
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400

	    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;

1401 1402 1403
            if (sp->is_not_allowed_in_function("function"))
              YYABORT;

1404 1405 1406 1407
	    if (sp->check_backpatch(YYTHD))
	      YYABORT;
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
	    sp->init_strings(YYTHD, lex, lex->spname);
1408 1409 1410 1411 1412
            if (!(sp->m_flags & sp_head::HAS_RETURN))
            {
              my_error(ER_SP_NORETURN, MYF(0), sp->m_qname.str);
              YYABORT;
            }
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
	    /* Restore flag if it was cleared above */
	    if (sp->m_old_cmq)
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
	    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; }
1452
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
	;

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();
1474
	    sp_add_used_routine(lex, YYTHD, $2, TYPE_ENUM_PROCEDURE);
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
	  }
          '(' 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
	;

1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
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;
	  }
	;

1527
sp_fdparam:
1528
	  ident sp_init_param type
1529 1530 1531 1532 1533 1534
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
1535
	      my_error(ER_SP_DUP_PARAM, MYF(0), $1.str);
1536 1537
	      YYABORT;
	    }
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
	    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;
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
1564
	  sp_opt_inout sp_init_param ident type
1565 1566 1567 1568
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1569
	    if (spc->find_pvar(&$3, TRUE))
1570
	    {
1571
	      my_error(ER_SP_DUP_PARAM, MYF(0), $3.str);
1572 1573
	      YYABORT;
	    }
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
	    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;
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
	  }
	;

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 */ {}
1597
	| sp_proc_stmts  sp_proc_stmt ';'
1598 1599 1600 1601
	;

sp_proc_stmts1:
	  sp_proc_stmt ';' {}
1602
	| sp_proc_stmts1  sp_proc_stmt ';'
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
	;

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
1618 1619
	      my_message(ER_SP_VARCOND_AFTER_CURSHNDLR,
                         ER(ER_SP_VARCOND_AFTER_CURSHNDLR), MYF(0));
1620 1621 1622 1623
	      YYABORT;
	    }
	    if ($2.curs && $1.hndlrs)
	    { /* Cursor following handler */
unknown's avatar
unknown committed
1624 1625
	      my_message(ER_SP_CURSOR_AFTER_HANDLER,
                         ER(ER_SP_CURSOR_AFTER_HANDLER), MYF(0));
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
	      YYABORT;
	    }
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
	    $$.curs= $1.curs + $2.curs;
	  }
	;

sp_decl:
1636
          DECLARE_SYM sp_decl_idents
1637
          {
1638
            LEX *lex= Lex;
1639

1640 1641 1642
            lex->sphead->reset_lex(YYTHD);
            lex->spcont->declare_var_boundary($2);
          }
1643
          type
1644
          sp_opt_default
1645 1646
          {
            LEX *lex= Lex;
1647 1648 1649 1650 1651 1652 1653
            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)
1654
            {
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
              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)));
1685
            }
1686 1687

            pctx->declare_var_boundary(0);
1688
            lex->sphead->restore_lex(YYTHD);
1689

1690 1691 1692
            $$.vars= $2;
            $$.conds= $$.hndlrs= $$.curs= 0;
          }
1693 1694 1695 1696 1697 1698 1699
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1700
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
	      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));
1718
	    sp->m_flags|= sp_head::IN_HANDLER;
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
	  }
	  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);
1742
	    sp->m_flags&= ~sp_head::IN_HANDLER;
1743 1744
	    $$.vars= $$.conds= $$.curs= 0;
	    $$.hndlrs= $6;
1745
	    ctx->add_handlers($6);
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
	  }
	| 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))
	    {
1757
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
1758 1759 1760
	      delete $5;
	      YYABORT;
	    }
unknown's avatar
unknown committed
1761 1762
            i= new sp_instr_cpush(sp->instructions(), ctx, $5,
                                  ctx->current_cursors());
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
	    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
1785 1786
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
1787 1788 1789 1790
	      YYABORT;
	    }
	    if (lex->result)
	    {
unknown's avatar
unknown committed
1791 1792
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
	      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;
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
	    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();
1823

1824 1825 1826 1827
	      i->add_condition($1);
	      ctx->push_handler($1);
	      $$= 1;
	    }
1828 1829 1830 1831 1832
	  }
	| sp_hcond_list ',' sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
	    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();
1844

1845 1846 1847 1848
	      i->add_condition($3);
	      ctx->push_handler($3);
	      $$= $1 + 1;
	    }
1849 1850 1851 1852
	  }
	;

sp_cond:
unknown's avatar
unknown committed
1853
	  ulong_num
1854 1855 1856 1857 1858 1859 1860
	  {			/* 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 */
1861 1862 1863 1864 1865
	    if (!sp_cond_check(&$3))
	    {
	      my_error(ER_SP_BAD_SQLSTATE, MYF(0), $3.str);
	      YYABORT;
	    }
1866 1867
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
1868 1869
	    memcpy($$->sqlstate, $3.str, 5);
	    $$->sqlstate[5]= '\0';
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
	  }
	;

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
1888
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
1889 1890 1891 1892 1893 1894 1895 1896
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
1897
	| not FOUND_SYM		/* SQLSTATEs 02??? */
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
	  {
	    $$= (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
	  {
1912 1913
            /* NOTE: field definition is filled in sp_decl section. */

1914 1915 1916 1917 1918
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
1919
	      my_error(ER_SP_DUP_VAR, MYF(0), $1.str);
1920 1921 1922 1923 1924 1925 1926
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum_field_types)0, sp_param_in);
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
1927 1928
            /* NOTE: field definition is filled in sp_decl section. */

1929 1930 1931 1932 1933
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$3, TRUE))
	    {
1934
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
	      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:
	  {
	    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;

1959
            sp->m_flags|= sp_get_flags_for_command(lex);
1960 1961
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
1962
	      my_error(ER_SP_BADSTATEMENT, MYF(0), "USE");
1963 1964
	      YYABORT;
	    }
1965 1966 1967 1968
	    /*
              Don't add an instruction for SET statements, since all
              instructions for them were already added during processing
              of "set" rule.
1969
	    */
1970 1971 1972
            DBUG_ASSERT(lex->sql_command != SQLCOM_SET_OPTION ||
                        lex->var_list.is_empty());
            if (lex->sql_command != SQLCOM_SET_OPTION)
1973
	    {
1974 1975
              sp_instr_stmt *i=new sp_instr_stmt(sp->instructions(),
                                                 lex->spcont, lex);
1976

1977
              /* Extract the query statement from the tokenizer:
1978
                 The end is either lex->tok_end or tok->ptr. */
1979 1980 1981 1982 1983 1984 1985 1986
              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);
1987 1988 1989
            }
	    sp->restore_lex(YYTHD);
          }
1990 1991 1992
          | RETURN_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr
1993 1994
	  {
	    LEX *lex= Lex;
1995
	    sp_head *sp= lex->sphead;
1996

1997
	    if (sp->m_type == TYPE_ENUM_PROCEDURE)
1998
	    {
unknown's avatar
unknown committed
1999
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
2000 2001 2002 2003 2004 2005
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_freturn *i;

2006 2007
	      i= new sp_instr_freturn(sp->instructions(), lex->spcont, $3,
                                      sp->m_return_field_def.sql_type, lex);
2008
	      sp->add_instr(i);
2009
	      sp->m_flags|= sp_head::HAS_RETURN;
2010
	    }
2011
	    sp->restore_lex(YYTHD);
2012 2013 2014 2015
	  }
	| IF sp_if END IF {}
	| CASE_SYM WHEN_SYM
	  {
2016
	    Lex->sphead->m_flags&= ~sp_head::IN_SIMPLE_CASE;
2017 2018
	  }
	  sp_case END CASE_SYM {}
2019 2020 2021
        | CASE_SYM
          { Lex->sphead->reset_lex(YYTHD); }
          expr WHEN_SYM
2022 2023
	  {
	    LEX *lex= Lex;
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
	    sp_head *sp= lex->sphead;
	    sp_pcontext *parsing_ctx= lex->spcont;
	    int case_expr_id= parsing_ctx->register_case_expr();
	    
	    if (parsing_ctx->push_case_expr_id(case_expr_id))
              YYABORT;
	    
	    sp->add_instr(
	      new sp_instr_set_case_expr(sp->instructions(),
	                                 parsing_ctx,
	                                 case_expr_id,
	                                 $3,
	                                 lex));
	    
	    sp->m_flags|= sp_head::IN_SIMPLE_CASE;
	    sp->restore_lex(YYTHD);
2040 2041 2042
	  }
	  sp_case END CASE_SYM
	  {
2043
	    Lex->spcont->pop_case_expr_id();
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
	  }
	| sp_labeled_control
	  {}
	| { /* Unlabeled controls get a secret label. */
	    LEX *lex= Lex;

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

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
2058
	| LEAVE_SYM label_ident
2059 2060 2061 2062 2063 2064 2065 2066
	  {
	    LEX *lex= Lex;
	    sp_head *sp = lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab)
	    {
2067
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
	      YYABORT;
	    }
	    else
	    {
	      uint ip= sp->instructions();
	      sp_instr_jump *i;
	      sp_instr_hpop *ih;
	      sp_instr_cpop *ic;

	      ih= new sp_instr_hpop(ip++, ctx, 0);
	      sp->push_backpatch(ih, lab);
	      sp->add_instr(ih);
	      ic= new sp_instr_cpop(ip++, ctx, 0);
	      sp->push_backpatch(ic, lab);
	      sp->add_instr(ic);
	      i= new sp_instr_jump(ip, ctx);
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
	    }
	  }
2088
	| ITERATE_SYM label_ident
2089 2090 2091 2092 2093 2094 2095 2096
	  {
	    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)
	    {
2097
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "ITERATE", $2.str);
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_jump *i;
	      uint ip= sp->instructions();
	      uint n;

	      n= ctx->diff_handlers(lab->ctx);
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
	      n= ctx->diff_cursors(lab->ctx);
	      if (n)
	        sp->add_instr(new sp_instr_cpop(ip++, ctx, n));
	      i= new sp_instr_jump(ip, ctx, lab->ip); /* Jump back */
              sp->add_instr(i);
	    }
	  }
	| LABEL_SYM IDENT
	  {
2118
#ifdef SP_GOTO
2119 2120 2121 2122 2123 2124 2125
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (lab)
	    {
2126
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $2.str);
2127 2128 2129 2130 2131 2132 2133 2134 2135
	      YYABORT;
	    }
	    else
	    {
	      lab= ctx->push_label($2.str, sp->instructions());
	      lab->type= SP_LAB_GOTO;
	      lab->ctx= ctx;
              sp->backpatch(lab);
	    }
2136 2137 2138 2139
#else
	    yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
#endif
2140 2141 2142
	  }
	| GOTO_SYM IDENT
	  {
2143
#ifdef SP_GOTO
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
	    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
2155
	      my_message(ER_SP_GOTO_IN_HNDLR, ER(ER_SP_GOTO_IN_HNDLR), MYF(0));
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
	      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);
	    }
2196 2197 2198 2199
#else
	    yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
#endif
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209
	  }
	| OPEN_SYM ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_copen *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2210
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
	      YYABORT;
	    }
	    i= new sp_instr_copen(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
	| FETCH_SYM sp_opt_fetch_noise ident INTO
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cfetch *i;

	    if (! lex->spcont->find_cursor(&$3, &offset))
	    {
2225
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $3.str);
2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
	      YYABORT;
	    }
	    i= new sp_instr_cfetch(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
	  sp_fetch_list
	  { }
	| CLOSE_SYM ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cclose *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2242
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265
	      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)))
	    {
2266
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
	      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)))
	    {
2287
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $3.str);
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	    }
	  }
	;

sp_if:
2301 2302
          { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2303 2304 2305 2306 2307
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= sp->instructions();
2308 2309
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, ctx,
                                                               $2, lex);
2310 2311 2312

	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
            sp->add_instr(i);
2313
            sp->restore_lex(YYTHD);
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340
	  }
	  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:
2341 2342
	  { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2343 2344 2345 2346 2347 2348 2349
	  {
            LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

2350
	    if (! (sp->m_flags & sp_head::IN_SIMPLE_CASE))
2351
	      i= new sp_instr_jump_if_not(ip, ctx, $2, lex);
2352 2353 2354
	    else
	    { /* Simple case: <caseval> = <whenval> */

2355 2356 2357 2358 2359
	      Item_case_expr *var;
              Item *expr;

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

unknown's avatar
unknown committed
2360
#ifndef DBUG_OFF
2361
              if (var)
2362
                var->m_sp= sp;
2363
#endif
2364 2365

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

2367
	      i= new sp_instr_jump_if_not(ip, ctx, expr, lex);
2368 2369 2370
	    }
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
            sp->add_instr(i);
2371
            sp->restore_lex(YYTHD);
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390
	  }
	  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
2391

2392 2393 2394 2395 2396 2397 2398
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
2399

2400 2401 2402 2403 2404
	    sp->add_instr(i);
	  }
	| ELSE sp_proc_stmts1 {}
	| WHEN_SYM sp_case {}
	;
unknown's avatar
unknown committed
2405

2406
sp_labeled_control:
2407
	  label_ident ':'
unknown's avatar
unknown committed
2408
	  {
2409 2410 2411 2412 2413 2414
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);

	    if (lab)
	    {
2415
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $1.str);
unknown's avatar
unknown committed
2416
	      YYABORT;
2417 2418 2419 2420 2421 2422 2423
	    }
	    else
	    {
	      lab= lex->spcont->push_label($1.str,
	                                   lex->sphead->instructions());
	      lab->type= SP_LAB_ITER;
	    }
unknown's avatar
unknown committed
2424
	  }
2425
	  sp_unlabeled_control sp_opt_label
unknown's avatar
unknown committed
2426
	  {
2427
	    LEX *lex= Lex;
unknown's avatar
unknown committed
2428

2429 2430 2431 2432 2433 2434 2435
	    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)
	      {
2436
	        my_error(ER_SP_LABEL_MISMATCH, MYF(0), $5.str);
2437 2438 2439 2440
	        YYABORT;
	      }
	    }
	    lex->sphead->backpatch(lex->spcont->pop_label());
unknown's avatar
unknown committed
2441
	  }
2442 2443 2444
	;

sp_opt_label:
2445
        /* Empty  */    { $$= null_lex_str; }
2446
        | label_ident   { $$= $1; }
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462
	;

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
2463
	  {
2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
	    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
2479
	  {
2480 2481 2482 2483 2484 2485
	    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
2486
	  }
2487 2488 2489
        | WHILE_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr DO_SYM
unknown's avatar
unknown committed
2490
	  {
2491 2492 2493 2494
	    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,
2495
							       $3, lex);
2496 2497 2498 2499

	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
            sp->add_instr(i);
2500
            sp->restore_lex(YYTHD);
unknown's avatar
unknown committed
2501
	  }
2502
	  sp_proc_stmts1 END WHILE_SYM
unknown's avatar
unknown committed
2503
	  {
2504 2505 2506 2507 2508 2509 2510
	    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);
	  }
2511 2512 2513
        | REPEAT_SYM sp_proc_stmts1 UNTIL_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr END REPEAT_SYM
2514 2515 2516 2517 2518
	  {
	    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,
2519 2520
                                                               $5, lab->ip,
                                                               lex);
2521
            lex->sphead->add_instr(i);
2522
            lex->sphead->restore_lex(YYTHD);
2523
	  }
2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
	;

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; }
2540
          ;
unknown's avatar
unknown committed
2541 2542

create2:
unknown's avatar
unknown committed
2543
        '(' create2a {}
2544 2545 2546
        | opt_create_table_options
          opt_partitioning {} 
          create3 {}
unknown's avatar
unknown committed
2547 2548 2549 2550
        | LIKE table_ident
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$2))
unknown's avatar
unknown committed
2551
              YYABORT;
unknown's avatar
unknown committed
2552 2553 2554 2555 2556
          }
        | '(' LIKE table_ident ')'
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$3))
2557
              YYABORT;
unknown's avatar
unknown committed
2558
          }
unknown's avatar
unknown committed
2559
        ;
unknown's avatar
unknown committed
2560

unknown's avatar
unknown committed
2561
create2a:
2562 2563 2564 2565 2566 2567
        field_list ')' opt_create_table_options
          opt_partitioning {}
          create3 {}
        |  opt_partitioning {}
           create_select ')'
           { Select->set_braces(1);} union_opt {}
2568
        ;
unknown's avatar
unknown committed
2569 2570

create3:
2571
	/* empty */ {}
unknown's avatar
unknown committed
2572
	| opt_duplicate opt_as     create_select
unknown's avatar
unknown committed
2573
          { Select->set_braces(0);} union_clause {}
unknown's avatar
unknown committed
2574
	| opt_duplicate opt_as '(' create_select ')'
unknown's avatar
unknown committed
2575
          { Select->set_braces(1);} union_opt {}
2576 2577
        ;

2578 2579 2580
/*
 This part of the parser is about handling of the partition information.

2581
 It's first version was written by Mikael Ronström with lots of answers to
2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608
 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
2609 2610 2611 2612 2613 2614 2615 2616 2617
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
            my_error(ER_OUTOFMEMORY, MYF(0), sizeof(partition_info));
            YYABORT;
          }
        }
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631
        partition
        ;

partition_entry:
        PARTITION_SYM
        {
          LEX *lex= Lex;
          if (lex->part_info)
          {
            /*
              We enter here when opening the frm file to translate
              partition info string into part_info data structure.
            */
            lex->part_info= new partition_info();
unknown's avatar
unknown committed
2632 2633 2634 2635 2636
            if (!lex->part_info)
            {
              my_error(ER_OUTOFMEMORY, MYF(0), sizeof(partition_info));
              YYABORT;
            }
2637 2638 2639 2640 2641 2642 2643
          }
          else
          {
            yyerror(ER(ER_PARTITION_ENTRY_ERROR));
            YYABORT;
          }
        }
unknown's avatar
unknown committed
2644 2645
        partition {}
        ;
2646 2647

partition:
unknown's avatar
unknown committed
2648 2649
        BY part_type_def opt_no_parts {} opt_sub_part {} part_defs
        ;
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665

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
2666 2667
        part_func {}
        ;
2668 2669 2670 2671

opt_linear:
        /* empty */ {}
        | LINEAR_SYM
unknown's avatar
unknown committed
2672 2673
        { Lex->part_info->linear_hash_ind= TRUE;}
        ;
2674 2675 2676

part_field_list:
        part_field_item {}
unknown's avatar
unknown committed
2677 2678
        | part_field_list ',' part_field_item {}
        ;
2679 2680 2681 2682 2683

part_field_item:
        ident
        {
          Lex->part_info->part_field_list.push_back($1.str);
unknown's avatar
unknown committed
2684 2685
        }
        ;
2686 2687 2688 2689 2690 2691 2692 2693 2694 2695

part_func:
        '(' remember_name part_func_expr remember_end ')'
        {
          LEX *lex= Lex;
          uint expr_len= (uint)($4 - $2) - 1;
          lex->part_info->list_of_part_fields= FALSE;
          lex->part_info->part_expr= $3;
          lex->part_info->part_func_string= $2+1;
          lex->part_info->part_func_len= expr_len;
unknown's avatar
unknown committed
2696 2697
        }
        ;
2698 2699 2700 2701 2702 2703 2704 2705 2706 2707

sub_part_func:
        '(' remember_name part_func_expr remember_end ')'
        {
          LEX *lex= Lex;
          uint expr_len= (uint)($4 - $2) - 1;
          lex->part_info->list_of_subpart_fields= FALSE;
          lex->part_info->subpart_expr= $3;
          lex->part_info->subpart_func_string= $2+1;
          lex->part_info->subpart_func_len= expr_len;
unknown's avatar
unknown committed
2708 2709
        }
        ;
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722


opt_no_parts:
        /* empty */ {}
        | PARTITIONS_SYM ulong_num 
        { 
          uint no_parts= $2;
          if (no_parts == 0)
          {
            my_error(ER_NO_PARTS_ERROR, MYF(0), "partitions");
            YYABORT;
          }
          Lex->part_info->no_parts= no_parts;
unknown's avatar
unknown committed
2723 2724
        }
        ;
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737

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
2738 2739
        opt_no_subparts {}
        ;
2740 2741 2742

sub_part_field_list:
        sub_part_field_item {}
unknown's avatar
unknown committed
2743 2744
        | sub_part_field_list ',' sub_part_field_item {}
        ;
2745 2746 2747

sub_part_field_item:
        ident
unknown's avatar
unknown committed
2748 2749
        { Lex->part_info->subpart_field_list.push_back($1.str); }
        ;
2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764

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
2765
        ;
2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777

opt_no_subparts:
        /* empty */ {}
        | SUBPARTITIONS_SYM ulong_num
        {
          uint no_parts= $2;
          if (no_parts == 0)
          {
            my_error(ER_NO_PARTS_ERROR, MYF(0), "subpartitions");
            YYABORT;
          }
          Lex->part_info->no_subparts= no_parts;
unknown's avatar
unknown committed
2778 2779
        }
        ;
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802

part_defs:
        /* empty */
        {}
        | '(' part_def_list ')'
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          if (part_info->no_parts != 0)
          {
            if (part_info->no_parts !=
                part_info->count_curr_parts)
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_PART_ERROR));
              YYABORT;
            }
          }
          else if (part_info->count_curr_parts > 0)
          {
            part_info->no_parts= part_info->count_curr_parts;
          }
          part_info->count_curr_subparts= 0;
          part_info->count_curr_parts= 0;
unknown's avatar
unknown committed
2803 2804
        }
        ;
2805 2806 2807

part_def_list:
        part_definition {}
unknown's avatar
unknown committed
2808 2809
        | part_def_list ',' part_definition {}
        ;
2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825

part_definition:
        PARTITION_SYM
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          partition_element *p_elem= new partition_element();
          if (!p_elem)
          {
            my_error(ER_OUTOFMEMORY, MYF(0), sizeof(partition_element));
            YYABORT;
          }
          part_info->curr_part_elem= p_elem;
          part_info->current_partition= p_elem;
          part_info->use_default_partitions= FALSE;
          part_info->partitions.push_back(p_elem);
unknown's avatar
unknown committed
2826
          p_elem->engine_type= NULL;
2827 2828 2829 2830 2831
          part_info->count_curr_parts++;
        }
        part_name {}
        opt_part_values {}
        opt_part_options {}
unknown's avatar
unknown committed
2832 2833
        opt_sub_partition {}
        ;
2834 2835 2836

part_name:
        ident_or_text
unknown's avatar
unknown committed
2837 2838
        { Lex->part_info->curr_part_elem->partition_name= $1.str; }
        ;
2839 2840 2841 2842 2843

opt_part_values:
        /* empty */
        {
          LEX *lex= Lex;
unknown's avatar
unknown committed
2844
          if (!is_partition_management(lex))
2845
          {
unknown's avatar
unknown committed
2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857
            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;
            }
2858 2859 2860 2861
          }
        }
        | VALUES LESS_SYM THAN_SYM part_func_max
        {
unknown's avatar
unknown committed
2862 2863
          LEX *lex= Lex;
          if (!is_partition_management(lex))
2864
          {
unknown's avatar
unknown committed
2865 2866 2867 2868 2869 2870
            if (Lex->part_info->part_type != RANGE_PARTITION)
            {
              my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
              "RANGE", "LESS THAN");
              YYABORT;
            }
2871 2872 2873 2874
          }
        }
        | VALUES IN_SYM '(' part_list_func ')'
        {
unknown's avatar
unknown committed
2875 2876
          LEX *lex= Lex;
          if (!is_partition_management(lex))
2877
          {
unknown's avatar
unknown committed
2878 2879 2880 2881 2882 2883
            if (Lex->part_info->part_type != LIST_PARTITION)
            {
              my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
              "LIST", "IN");
              YYABORT;
            }
2884
          }
unknown's avatar
unknown committed
2885 2886
        }
        ;
2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897

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
2898
          lex->part_info->curr_part_elem->range_value= LONGLONG_MAX;
2899 2900 2901 2902 2903 2904 2905 2906
        }
        | part_range_func
        {
          if (Lex->part_info->defined_max_value)
          {
            yyerror(ER(ER_PARTITION_MAXVALUE_ERROR));
            YYABORT;
          }
unknown's avatar
unknown committed
2907 2908
        }
        ;
2909 2910 2911 2912

part_range_func:
        '(' part_bit_expr ')' 
        {
unknown's avatar
unknown committed
2913
          Lex->part_info->curr_part_elem->range_value= $2;
unknown's avatar
unknown committed
2914 2915
        }
        ;
2916 2917 2918

part_list_func:
        part_list_item {}
unknown's avatar
unknown committed
2919 2920
        | part_list_func ',' part_list_item {}
        ;
2921 2922 2923 2924

part_list_item:
        part_bit_expr
        {
unknown's avatar
unknown committed
2925
          longlong *value_ptr;
unknown's avatar
unknown committed
2926
          if (!(value_ptr= (longlong*)sql_alloc(sizeof(longlong))))
unknown's avatar
unknown committed
2927 2928 2929 2930 2931 2932
          {
            my_error(ER_OUTOFMEMORY, MYF(0), sizeof(longlong));
            YYABORT;
          }
          *value_ptr= $1;
          Lex->part_info->curr_part_elem->list_val_list.push_back(value_ptr);
unknown's avatar
unknown committed
2933 2934
        }
        ;
2935 2936 2937 2938 2939 2940 2941

part_bit_expr:
        bit_expr
        {
          Item *part_expr= $1;
          bool not_corr_func;
          LEX *lex= Lex;
unknown's avatar
unknown committed
2942
          longlong item_value;
2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955
          Name_resolution_context *context= &lex->current_select->context;
          TABLE_LIST *save_list= context->table_list;

          context->table_list= 0;
          part_expr->fix_fields(YYTHD, (Item**)0);
          context->table_list= save_list;
          not_corr_func= !part_expr->const_item() ||
                         !lex->safe_to_cache_query;
          if (not_corr_func)
          {
            yyerror(ER(ER_NO_CONST_EXPR_IN_RANGE_OR_LIST_ERROR));
            YYABORT;
          }
unknown's avatar
unknown committed
2956 2957 2958 2959 2960 2961 2962
          if (part_expr->result_type() != INT_RESULT)
          {
            yyerror(ER(ER_INCONSISTENT_TYPE_OF_FUNCTIONS_ERROR));
            YYABORT;
          }
          item_value= part_expr->val_int();
          $$= item_value; 
2963
        }
unknown's avatar
unknown committed
2964
        ;
2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985

opt_sub_partition:
        /* empty */ {}
        | '(' sub_part_list ')'
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          if (part_info->no_subparts != 0)
          {
            if (part_info->no_subparts !=
                part_info->count_curr_subparts)
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_SUBPART_ERROR));
              YYABORT;
            }
          }
          else if (part_info->count_curr_subparts > 0)
          {
            part_info->no_subparts= part_info->count_curr_subparts;
          }
          part_info->count_curr_subparts= 0;
unknown's avatar
unknown committed
2986 2987
        }
        ;
2988 2989 2990

sub_part_list:
        sub_part_definition {}
unknown's avatar
unknown committed
2991 2992
        | sub_part_list ',' sub_part_definition {}
        ;
2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008

sub_part_definition:
        SUBPARTITION_SYM
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          partition_element *p_elem= new partition_element();
          if (!p_elem)
          {
            my_error(ER_OUTOFMEMORY, MYF(0), sizeof(partition_element));
            YYABORT;
          }
          part_info->curr_part_elem= p_elem;
          part_info->current_partition->subpartitions.push_back(p_elem);
          part_info->use_default_subpartitions= FALSE;
          part_info->count_curr_subparts++;
unknown's avatar
unknown committed
3009
          p_elem->engine_type= NULL;
3010
        }
unknown's avatar
unknown committed
3011 3012
        sub_name opt_part_options {}
        ;
3013 3014 3015

sub_name:
        ident_or_text
unknown's avatar
unknown committed
3016 3017
        { Lex->part_info->curr_part_elem->partition_name= $1.str; }
        ;
3018 3019 3020

opt_part_options:
       /* empty */ {}
unknown's avatar
unknown committed
3021 3022
       | opt_part_option_list {}
       ;
3023 3024 3025

opt_part_option_list:
       opt_part_option_list opt_part_option {}
unknown's avatar
unknown committed
3026 3027
       | opt_part_option {}
       ;
3028 3029 3030 3031 3032

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
3033 3034 3035 3036 3037
        {
          LEX *lex= Lex;
          lex->part_info->curr_part_elem->engine_type= $4;
          lex->part_info->default_engine_type= $4;
        }
3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048
        | 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
3049 3050
        { Lex->part_info->curr_part_elem->part_comment= $3.str; }
        ;
3051 3052 3053 3054 3055

/*
 End of partition parser part
*/

unknown's avatar
unknown committed
3056
create_select:
3057
          SELECT_SYM
unknown's avatar
unknown committed
3058
          {
unknown's avatar
unknown committed
3059
	    LEX *lex=Lex;
3060
	    lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
3061 3062 3063 3064
	    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;
3065 3066 3067 3068
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
unknown's avatar
unknown committed
3069
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
unknown's avatar
unknown committed
3070
	    mysql_init_select(lex);
3071
	    lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
3072
          }
unknown's avatar
unknown committed
3073 3074
          select_options select_item_list
	  {
3075
	    Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
3076
	  }
unknown's avatar
unknown committed
3077
	  opt_select_from
3078 3079 3080 3081 3082 3083 3084
	  {
	    /*
              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
3085
        ;
unknown's avatar
unknown committed
3086

3087 3088
opt_as:
	/* empty */ {}
unknown's avatar
unknown committed
3089
	| AS	    {};
3090

3091 3092 3093 3094 3095 3096 3097 3098 3099
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
3100 3101
	default_collation   {}
	| default_charset   {};
3102

unknown's avatar
unknown committed
3103
opt_table_options:
unknown's avatar
unknown committed
3104
	/* empty */	 { $$= 0; }
unknown's avatar
unknown committed
3105
	| table_options  { $$= $1;};
unknown's avatar
unknown committed
3106 3107 3108

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

unknown's avatar
unknown committed
3111
table_option:
unknown's avatar
unknown committed
3112
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
unknown's avatar
unknown committed
3113 3114

opt_if_not_exists:
unknown's avatar
unknown committed
3115
	/* empty */	 { $$= 0; }
3116
	| IF not EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
unknown's avatar
unknown committed
3117 3118 3119

opt_create_table_options:
	/* empty */
unknown's avatar
unknown committed
3120
	| create_table_options;
unknown's avatar
unknown committed
3121

unknown's avatar
unknown committed
3122 3123 3124 3125
create_table_options_space_separated:
	create_table_option
	| create_table_option create_table_options_space_separated;

unknown's avatar
unknown committed
3126 3127
create_table_options:
	create_table_option
3128
	| create_table_option     create_table_options
unknown's avatar
unknown committed
3129
	| create_table_option ',' create_table_options;
unknown's avatar
unknown committed
3130 3131

create_table_option:
3132 3133
	ENGINE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; }
	| TYPE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; WARN_DEPRECATED("TYPE=storage_engine","ENGINE=storage_engine");   Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; }
3134 3135
	| 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
3136
	| 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;}
3137 3138
	| 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; }
3139
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160
        | 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
3161 3162
	| 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; }
3163
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
unknown's avatar
unknown committed
3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178
	| RAID_TYPE opt_equal raid_types
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_TYPE", "PARTITION");
	    YYABORT;
	  }
	| RAID_CHUNKS opt_equal ulong_num
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_CHUNKS", "PARTITION");
	    YYABORT;
	  }
	| RAID_CHUNKSIZE opt_equal ulong_num
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_CHUNKSIZE", "PARTITION");
	    YYABORT;
	  }
3179
	| UNION_SYM opt_equal '(' table_list ')'
3180 3181 3182
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
3183 3184
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
3185
	    lex->create_info.merge_list.elements--;
3186 3187
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
3188
	    lex->select_lex.table_list.elements=1;
3189 3190 3191
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
3192
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
3193
	  }
3194 3195
	| default_charset
	| default_collation
3196
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
3197 3198
	| 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; }
3199
	| 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; }
3200
        ;
unknown's avatar
unknown committed
3201

3202 3203 3204 3205 3206 3207 3208 3209
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))
          {
3210 3211 3212
            my_error(ER_CONFLICTING_DECLARATIONS, MYF(0),
                     "CHARACTER SET ", cinfo->default_table_charset->csname,
                     "CHARACTER SET ", $4->csname);
3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226
            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))
            {
3227 3228
              my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $4->name, cinfo->default_table_charset->csname);
3229 3230 3231 3232 3233 3234
              YYABORT;
            }
            Lex->create_info.default_table_charset= $4;
            Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

unknown's avatar
unknown committed
3235
storage_engines:
3236 3237
	ident_or_text
	{
unknown's avatar
unknown committed
3238 3239
	  $$ = ha_resolve_by_name(YYTHD, &$1);
	  if ($$ == NULL &&
3240 3241
	      test(YYTHD->variables.sql_mode & MODE_NO_ENGINE_SUBSTITUTION))
	  {
3242
	    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
3243 3244 3245
	    YYABORT;
	  }
	};
unknown's avatar
unknown committed
3246 3247 3248 3249 3250

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
3251 3252 3253
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; }
	| REDUNDANT_SYM	{ $$= ROW_TYPE_REDUNDANT; }
	| COMPACT_SYM	{ $$= ROW_TYPE_COMPACT; };
unknown's avatar
unknown committed
3254 3255 3256 3257

raid_types:
	RAID_STRIPED_SYM { $$= RAID_TYPE_0; }
	| RAID_0_SYM	 { $$= RAID_TYPE_0; }
unknown's avatar
unknown committed
3258
	| ulong_num	 { $$=$1;};
unknown's avatar
unknown committed
3259

3260 3261 3262
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
unknown's avatar
unknown committed
3263
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
3264

unknown's avatar
unknown committed
3265
opt_select_from:
3266
	opt_limit_clause {}
unknown's avatar
unknown committed
3267
	| select_from select_lock_type;
unknown's avatar
unknown committed
3268 3269

udf_func_type:
3270
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
unknown's avatar
unknown committed
3271
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
unknown's avatar
unknown committed
3272 3273 3274 3275

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
unknown's avatar
unknown committed
3276
        | DECIMAL_SYM {$$ = (int) DECIMAL_RESULT; }
unknown's avatar
unknown committed
3277
	| INT_SYM {$$ = (int) INT_RESULT; };
unknown's avatar
unknown committed
3278 3279 3280

field_list:
	  field_list_item
unknown's avatar
unknown committed
3281
	| field_list ',' field_list_item;
unknown's avatar
unknown committed
3282 3283 3284


field_list_item:
unknown's avatar
unknown committed
3285
	   column_def
3286 3287 3288 3289
         | key_def
         ;

column_def:
unknown's avatar
unknown committed
3290
	  field_spec opt_check_constraint
unknown's avatar
unknown committed
3291 3292 3293 3294
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
unknown's avatar
unknown committed
3295
	;
3296 3297

key_def:
3298
	key_type opt_ident key_alg '(' key_list ')' opt_fulltext_parser
unknown's avatar
unknown committed
3299
	  {
unknown's avatar
unknown committed
3300
	    LEX *lex=Lex;
3301 3302 3303 3304 3305 3306
	    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
3307
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
3308
	  }
3309 3310 3311 3312
	| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
	  {
	    LEX *lex=Lex;
	    const char *key_name= $3 ? $3:$1;
3313 3314
	    lex->key_list.push_back(new Key($2, key_name, $4, 0,
				    lex->col_list));
3315 3316
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
3317
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
unknown's avatar
unknown committed
3318
	  {
3319
	    LEX *lex=Lex;
3320
	    lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
3321 3322 3323 3324 3325
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
3326 3327 3328
	    lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4 : $1,
					    HA_KEY_ALG_UNDEF, 1,
					    lex->col_list));
3329
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
3330
	  }
unknown's avatar
unknown committed
3331 3332 3333 3334
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
3335
	| opt_constraint check_constraint
unknown's avatar
unknown committed
3336 3337
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
3338 3339 3340
	  }
	;

3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354
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
3355
opt_check_constraint:
3356
	/* empty */
unknown's avatar
unknown committed
3357 3358 3359 3360 3361
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
3362
	;
unknown's avatar
unknown committed
3363 3364

opt_constraint:
3365
	/* empty */		{ $$=(char*) 0; }
unknown's avatar
unknown committed
3366 3367 3368 3369 3370 3371
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
unknown's avatar
unknown committed
3372 3373 3374 3375

field_spec:
	field_ident
	 {
unknown's avatar
unknown committed
3376
	   LEX *lex=Lex;
3377
	   lex->length=lex->dec=0; lex->type=0;
3378
	   lex->default_value= lex->on_update_value= 0;
3379
           lex->comment=null_lex_str;
3380
	   lex->charset=NULL;
unknown's avatar
unknown committed
3381 3382 3383
	 }
	type opt_attribute
	{
unknown's avatar
unknown committed
3384
	  LEX *lex=Lex;
unknown's avatar
unknown committed
3385
	  if (add_field_to_list(lex->thd, $1.str,
unknown's avatar
unknown committed
3386
				(enum enum_field_types) $3,
unknown's avatar
unknown committed
3387
				lex->length,lex->dec,lex->type,
3388
				lex->default_value, lex->on_update_value, 
3389
                                &lex->comment,
3390
				lex->change,&lex->interval_list,lex->charset,
unknown's avatar
unknown committed
3391
				lex->uint_geom_type))
unknown's avatar
unknown committed
3392
	    YYABORT;
unknown's avatar
unknown committed
3393
	};
unknown's avatar
unknown committed
3394 3395

type:
3396
	int_type opt_len field_options	{ $$=$1; }
unknown's avatar
unknown committed
3397 3398
	| real_type opt_precision field_options { $$=$1; }
	| FLOAT_SYM float_options field_options { $$=FIELD_TYPE_FLOAT; }
unknown's avatar
unknown committed
3399 3400 3401 3402
	| BIT_SYM			{ Lex->length= (char*) "1";
					  $$=FIELD_TYPE_BIT; }
	| BIT_SYM '(' NUM ')'		{ Lex->length= $3.str;
					  $$=FIELD_TYPE_BIT; }
unknown's avatar
unknown committed
3403 3404
	| BOOL_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
3405 3406
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
3407
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
unknown's avatar
unknown committed
3408 3409 3410
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
3411
	| nchar '(' NUM ')' opt_bin_mod	{ Lex->length=$3.str;
unknown's avatar
unknown committed
3412
					  $$=FIELD_TYPE_STRING;
3413
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
3414
	| nchar opt_bin_mod		{ Lex->length=(char*) "1";
unknown's avatar
unknown committed
3415
					  $$=FIELD_TYPE_STRING;
3416
					  Lex->charset=national_charset_info; }
3417
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
3418
					  Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
3419
					  $$=FIELD_TYPE_STRING; }
3420 3421 3422
	| BINARY			{ Lex->length= (char*) "1";
					  Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
3423
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
3424
					  $$= MYSQL_TYPE_VARCHAR; }
unknown's avatar
unknown committed
3425
	| nvarchar '(' NUM ')' opt_bin_mod { Lex->length=$3.str;
3426
					  $$= MYSQL_TYPE_VARCHAR;
3427
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
3428
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
unknown's avatar
unknown committed
3429
					  Lex->charset=&my_charset_bin;
3430
					  $$= MYSQL_TYPE_VARCHAR; }
3431
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
unknown's avatar
unknown committed
3432 3433
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
3434
	| TIMESTAMP opt_len
3435
	  {
3436
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
3437 3438
	      $$=FIELD_TYPE_DATETIME;
	    else
3439 3440 3441 3442 3443
            {
              /* 
                Unlike other types TIMESTAMP fields are NOT NULL by default.
              */
              Lex->type|= NOT_NULL_FLAG;
3444
	      $$=FIELD_TYPE_TIMESTAMP;
3445
            }
3446
	   }
unknown's avatar
unknown committed
3447
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
unknown's avatar
unknown committed
3448
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
3449
					  $$=FIELD_TYPE_TINY_BLOB; }
unknown's avatar
unknown committed
3450
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
3451
					  $$=FIELD_TYPE_BLOB; }
unknown's avatar
unknown committed
3452 3453
	| spatial_type
          {
unknown's avatar
unknown committed
3454
#ifdef HAVE_SPATIAL
unknown's avatar
unknown committed
3455 3456 3457
            Lex->charset=&my_charset_bin;
            Lex->uint_geom_type= (uint)$1;
            $$=FIELD_TYPE_GEOMETRY;
unknown's avatar
unknown committed
3458
#else
unknown's avatar
unknown committed
3459
            my_error(ER_FEATURE_DISABLED, MYF(0),
3460
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3461
            YYABORT;
unknown's avatar
unknown committed
3462
#endif
unknown's avatar
unknown committed
3463
          }
unknown's avatar
unknown committed
3464
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
3465
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
3466
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
3467
					  $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
3468
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
3469
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
3470 3471
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
3472
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
3473 3474
	| MEDIUMTEXT opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| LONGTEXT opt_binary		{ $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
3475
	| DECIMAL_SYM float_options field_options
unknown's avatar
unknown committed
3476
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
unknown's avatar
unknown committed
3477
	| NUMERIC_SYM float_options field_options
unknown's avatar
unknown committed
3478
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
3479
	| FIXED_SYM float_options field_options
unknown's avatar
unknown committed
3480
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
3481
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
3482
	  { $$=FIELD_TYPE_ENUM; }
3483
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
3484
	  { $$=FIELD_TYPE_SET; }
3485
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
3486 3487 3488 3489 3490 3491
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
3492
	;
unknown's avatar
unknown committed
3493

unknown's avatar
unknown committed
3494 3495 3496
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
3497 3498 3499
	| POINT_SYM           { Lex->length= (char*)"21";
                                $$= Field::GEOM_POINT;
                              }
unknown's avatar
unknown committed
3500 3501 3502 3503 3504 3505 3506
	| 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
3507 3508
char:
	CHAR_SYM {}
unknown's avatar
unknown committed
3509 3510 3511 3512 3513 3514
	;

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

unknown's avatar
unknown committed
3516 3517 3518
varchar:
	char VARYING {}
	| VARCHAR {}
unknown's avatar
unknown committed
3519 3520 3521 3522
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
unknown's avatar
unknown committed
3523
	| NVARCHAR_SYM {}
unknown's avatar
unknown committed
3524 3525 3526 3527
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
unknown's avatar
unknown committed
3528 3529 3530 3531 3532 3533

int_type:
	INT_SYM		{ $$=FIELD_TYPE_LONG; }
	| TINYINT	{ $$=FIELD_TYPE_TINY; }
	| SMALLINT	{ $$=FIELD_TYPE_SHORT; }
	| MEDIUMINT	{ $$=FIELD_TYPE_INT24; }
unknown's avatar
unknown committed
3534
	| BIGINT	{ $$=FIELD_TYPE_LONGLONG; };
unknown's avatar
unknown committed
3535 3536

real_type:
3537
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
unknown's avatar
unknown committed
3538 3539
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
unknown's avatar
unknown committed
3540
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
unknown's avatar
unknown committed
3541 3542 3543


float_options:
unknown's avatar
unknown committed
3544 3545
        /* empty */		{ Lex->dec=Lex->length= (char*)0; }
        | '(' NUM ')'		{ Lex->length=$2.str; Lex->dec= (char*)0; }
unknown's avatar
unknown committed
3546
	| precision		{};
unknown's avatar
unknown committed
3547 3548 3549 3550 3551 3552

precision:
	'(' NUM ',' NUM ')'
	{
	  LEX *lex=Lex;
	  lex->length=$2.str; lex->dec=$4.str;
unknown's avatar
unknown committed
3553
	};
unknown's avatar
unknown committed
3554 3555 3556

field_options:
	/* empty */		{}
unknown's avatar
unknown committed
3557
	| field_opt_list	{};
unknown's avatar
unknown committed
3558 3559 3560

field_opt_list:
	field_opt_list field_option {}
unknown's avatar
unknown committed
3561
	| field_option {};
unknown's avatar
unknown committed
3562 3563

field_option:
unknown's avatar
unknown committed
3564
	SIGNED_SYM	{}
3565
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
unknown's avatar
unknown committed
3566
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
unknown's avatar
unknown committed
3567 3568

opt_len:
3569 3570
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
unknown's avatar
unknown committed
3571 3572 3573

opt_precision:
	/* empty */	{}
unknown's avatar
unknown committed
3574
	| precision	{};
unknown's avatar
unknown committed
3575 3576 3577

opt_attribute:
	/* empty */ {}
unknown's avatar
unknown committed
3578
	| opt_attribute_list {};
unknown's avatar
unknown committed
3579 3580 3581

opt_attribute_list:
	opt_attribute_list attribute {}
unknown's avatar
unknown committed
3582
	| attribute;
unknown's avatar
unknown committed
3583 3584 3585

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
3586
	| not NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
3587 3588 3589
	| 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
3590
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
3591
	| SERIAL_SYM DEFAULT VALUE_SYM
3592 3593 3594
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
3595
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3596 3597 3598 3599 3600
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
3601
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3602 3603 3604 3605 3606
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
3607
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3608 3609 3610 3611 3612
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
3613
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3614
	  }
3615
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= $2; }
unknown's avatar
unknown committed
3616 3617
	| COLLATE_SYM collation_name
	  {
unknown's avatar
unknown committed
3618
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
unknown's avatar
unknown committed
3619
	    {
3620 3621
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $2->name,Lex->charset->csname);
unknown's avatar
unknown committed
3622 3623 3624 3625 3626 3627 3628 3629
	      YYABORT;
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
unknown's avatar
unknown committed
3630

3631 3632 3633 3634 3635
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

3636 3637 3638 3639
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
unknown's avatar
unknown committed
3640

3641
charset_name:
3642
	ident_or_text
3643
	{
unknown's avatar
unknown committed
3644
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
3645
	  {
3646
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
3647 3648
	    YYABORT;
	  }
unknown's avatar
unknown committed
3649 3650 3651
	}
	| BINARY { $$= &my_charset_bin; }
	;
3652

3653 3654 3655
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
unknown's avatar
unknown committed
3656

unknown's avatar
unknown committed
3657 3658 3659 3660 3661 3662 3663

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)))
	  {
3664
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
unknown's avatar
unknown committed
3665 3666 3667 3668 3669 3670 3671 3672 3673 3674
	    YYABORT;
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

3675
collation_name:
3676
	ident_or_text
3677 3678 3679
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
3680
	    my_error(ER_UNKNOWN_COLLATION, MYF(0), $1.str);
3681 3682 3683 3684
	    YYABORT;
	  }
	};

3685 3686
opt_collate:
	/* empty */	{ $$=NULL; }
unknown's avatar
unknown committed
3687
	| COLLATE_SYM collation_name_or_default { $$=$2; }
3688 3689
	;

3690 3691 3692 3693
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

3694 3695 3696 3697
opt_default:
	/* empty */	{}
	| DEFAULT	{};

unknown's avatar
unknown committed
3698
opt_binary:
3699
	/* empty */			{ Lex->charset=NULL; }
unknown's avatar
unknown committed
3700
	| ASCII_SYM opt_bin_mod		{ Lex->charset=&my_charset_latin1; }
unknown's avatar
unknown committed
3701
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
unknown's avatar
unknown committed
3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720
	| 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
3721 3722
	| UNICODE_SYM
	{
unknown's avatar
unknown committed
3723 3724
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
unknown's avatar
unknown committed
3725
	  {
unknown's avatar
unknown committed
3726
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
unknown's avatar
unknown committed
3727 3728 3729
	    YYABORT;
	  }
	}
3730
	| charset charset_name	{ Lex->charset=$2; } ;
unknown's avatar
unknown committed
3731

unknown's avatar
unknown committed
3732 3733 3734
opt_primary:
	/* empty */
	| PRIMARY_SYM
3735
	;
unknown's avatar
unknown committed
3736

unknown's avatar
unknown committed
3737
references:
3738 3739
	REFERENCES table_ident
	{
3740
	  LEX *lex=Lex;
3741 3742 3743 3744 3745 3746
	  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
3747
	};
3748

3749
opt_ref_list:
unknown's avatar
unknown committed
3750
	/* empty */ opt_on_delete {}
unknown's avatar
unknown committed
3751
	| '(' ref_list ')' opt_on_delete {};
3752 3753 3754

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

unknown's avatar
unknown committed
3757 3758 3759

opt_on_delete:
	/* empty */ {}
unknown's avatar
unknown committed
3760
	| opt_on_delete_list {};
unknown's avatar
unknown committed
3761 3762 3763

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
unknown's avatar
unknown committed
3764
	| opt_on_delete_item {};
unknown's avatar
unknown committed
3765 3766

opt_on_delete_item:
3767 3768 3769 3770
	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
3771
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
unknown's avatar
unknown committed
3772 3773

delete_option:
3774 3775 3776 3777
	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
3778
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
unknown's avatar
unknown committed
3779 3780

key_type:
3781
	key_or_index			    { $$= Key::MULTIPLE; }
unknown's avatar
SCRUM  
unknown committed
3782 3783
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
unknown's avatar
unknown committed
3784 3785 3786 3787
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
3788 3789
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3790 3791 3792
	    YYABORT;
#endif
	  };
3793 3794 3795

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
unknown's avatar
SCRUM  
unknown committed
3796
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
unknown's avatar
unknown committed
3797 3798 3799

key_or_index:
	KEY_SYM {}
unknown's avatar
unknown committed
3800
	| INDEX_SYM {};
unknown's avatar
unknown committed
3801

unknown's avatar
SCRUM  
unknown committed
3802 3803 3804 3805 3806
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

unknown's avatar
unknown committed
3807 3808
keys_or_index:
	KEYS {}
unknown's avatar
unknown committed
3809
	| INDEX_SYM {}
unknown's avatar
unknown committed
3810
	| INDEXES {};
unknown's avatar
unknown committed
3811

3812
opt_unique_or_fulltext:
unknown's avatar
unknown committed
3813 3814
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
unknown's avatar
unknown committed
3815
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
unknown's avatar
unknown committed
3816 3817 3818 3819 3820
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
unknown's avatar
unknown committed
3821
            my_error(ER_FEATURE_DISABLED, MYF(0),
unknown's avatar
unknown committed
3822
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3823 3824 3825
	    YYABORT;
#endif
	  }
unknown's avatar
unknown committed
3826
        ;
unknown's avatar
unknown committed
3827 3828

key_alg:
3829
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
3830
	| USING opt_btree_or_rtree { $$= $2; }
unknown's avatar
unknown committed
3831
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
unknown's avatar
unknown committed
3832 3833 3834

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
unknown's avatar
unknown committed
3835 3836 3837 3838
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
unknown's avatar
unknown committed
3839
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
unknown's avatar
unknown committed
3840 3841 3842

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
unknown's avatar
unknown committed
3843
	| key_part order_dir		{ Lex->col_list.push_back($1); };
unknown's avatar
unknown committed
3844 3845 3846

key_part:
	ident			{ $$=new key_part_spec($1.str); }
3847 3848 3849 3850 3851
	| ident '(' NUM ')'	
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
3852
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
3853 3854 3855
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
unknown's avatar
unknown committed
3856 3857 3858

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

3861
opt_component:
3862 3863
        /* empty */      { $$= null_lex_str; }
        | '.' ident      { $$= $2; };
3864

unknown's avatar
unknown committed
3865 3866
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
unknown's avatar
unknown committed
3867
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
unknown's avatar
unknown committed
3868 3869 3870 3871 3872 3873 3874 3875

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
3876
	  THD *thd= YYTHD;
3877
	  LEX *lex= thd->lex;
3878 3879 3880
	  lex->sql_command= SQLCOM_ALTER_TABLE;
	  lex->name= 0;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
3881 3882
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
unknown's avatar
unknown committed
3883 3884 3885 3886
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
3887
          lex->select_lex.init_order();
3888
	  lex->select_lex.db=lex->name=0;
3889
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
unknown's avatar
unknown committed
3890
	  lex->create_info.db_type= (handlerton*) &default_hton;
3891
	  lex->create_info.default_table_charset= NULL;
3892
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
3893
	  lex->alter_info.reset();
3894
	  lex->alter_info.flags= 0;
unknown's avatar
unknown committed
3895
	}
3896
	alter_commands
3897
	{}
3898
	| ALTER DATABASE ident_or_empty
3899 3900 3901 3902 3903
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
          opt_create_database_options
3904 3905 3906
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
3907
	    lex->name= $3;
3908 3909 3910 3911 3912
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

3913 3914 3915 3916 3917
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
	      YYABORT;
	    }
3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929
	    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;
3930

3931 3932 3933 3934 3935
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
	      YYABORT;
	    }
3936 3937 3938 3939 3940 3941 3942 3943 3944
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    LEX *lex=Lex;

	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
	    lex->spname= $3;
	  }
3945 3946
        | ALTER view_algorithm_opt definer view_suid
          VIEW_SYM table_ident
3947 3948 3949 3950 3951 3952
	  {
	    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 */
3953
	    lex->select_lex.add_table_to_list(thd, $6, NULL, 0);
3954
	  }
3955
	  view_list_opt AS view_select view_check_option
3956 3957
	  {}
	;
3958

3959 3960 3961 3962
ident_or_empty:
	/* empty */  { $$= 0; }
	| ident      { $$= $1.str; };

3963
alter_commands:
3964 3965
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
3966 3967 3968
        | alter_list
        opt_partitioning
        | partitioning
unknown's avatar
unknown committed
3969
/*
3970
  This part was added for release 5.1 by Mikael Ronström.
unknown's avatar
unknown committed
3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988
  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;
          }
        | COALESCE PARTITION_SYM ulong_num
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_COALESCE_PARTITION;
	    lex->alter_info.no_parts= $3;
          }
        | reorg_partition_rule
3989 3990
        ;

unknown's avatar
unknown committed
3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054
add_partition_rule:
        ADD PARTITION_SYM
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
            my_error(ER_OUTOFMEMORY, MYF(0), sizeof(partition_info));
            YYABORT;
          }
	  lex->alter_info.flags|= ALTER_ADD_PARTITION;
        }
        add_part_extra
        {}
        ;

add_part_extra:
        | '(' part_def_list ')'
        {
          LEX *lex= Lex;
          lex->part_info->no_parts= lex->part_info->count_curr_parts;
        }
        | PARTITIONS_SYM ulong_num
        {
          LEX *lex= Lex;
          lex->part_info->no_parts= $2;
        }
        ;

reorg_partition_rule:
        REORGANISE_SYM PARTITION_SYM
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
            my_error(ER_OUTOFMEMORY, MYF(0), sizeof(partition_info));
            YYABORT;
          }
	  lex->alter_info.flags|= ALTER_REORGANISE_PARTITION;
        }
        alt_part_name_list INTO '(' part_def_list ')'
        {
          LEX *lex= Lex;
          lex->part_info->no_parts= lex->part_info->count_curr_parts;
        }
        ;

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

alt_part_name_item:
        ident
        {
          Lex->alter_info.partition_names.push_back($1.str);
        }
        ;

/*
  End of management of partition commands
*/

4055 4056 4057 4058
alter_list:
        alter_list_item
	| alter_list ',' alter_list_item
        ;
unknown's avatar
unknown committed
4059 4060

add_column:
4061
	ADD opt_column
4062 4063
	{
	  LEX *lex=Lex;
4064 4065
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
4066
	};
unknown's avatar
unknown committed
4067 4068

alter_list_item:
4069 4070 4071 4072
	add_column column_def opt_place { }
	| ADD key_def
	  {
	    Lex->alter_info.flags|= ALTER_ADD_INDEX;
4073
	  }
4074 4075 4076 4077
	| add_column '(' field_list ')'
          {
	    Lex->alter_info.flags|= ALTER_ADD_COLUMN | ALTER_ADD_INDEX;
          }
unknown's avatar
unknown committed
4078 4079 4080
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
4081
	     lex->change= $3.str;
4082
	     lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
4083
	  }
4084
          field_spec opt_place
unknown's avatar
unknown committed
4085 4086 4087
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
4088
            lex->length=lex->dec=0; lex->type=0;
4089
            lex->default_value= lex->on_update_value= 0;
4090
            lex->comment=null_lex_str;
4091
	    lex->charset= NULL;
4092
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
4093 4094 4095 4096
          }
          type opt_attribute
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
4097
            if (add_field_to_list(lex->thd,$3.str,
unknown's avatar
unknown committed
4098 4099
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
4100
                                  lex->default_value, lex->on_update_value,
4101
                                  &lex->comment,
4102
				  $3.str, &lex->interval_list, lex->charset,
unknown's avatar
unknown committed
4103
				  lex->uint_geom_type))
unknown's avatar
unknown committed
4104 4105 4106
	       YYABORT;
          }
          opt_place
unknown's avatar
unknown committed
4107
	| DROP opt_column field_ident opt_restrict
unknown's avatar
unknown committed
4108 4109
	  {
	    LEX *lex=Lex;
4110
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
4111
                                                               $3.str));
4112
	    lex->alter_info.flags|= ALTER_DROP_COLUMN;
unknown's avatar
unknown committed
4113
	  }
4114 4115 4116 4117
	| DROP FOREIGN KEY_SYM opt_ident
          {
	    Lex->alter_info.flags|= ALTER_DROP_INDEX;
          }
unknown's avatar
unknown committed
4118 4119 4120
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
4121 4122 4123
	    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
4124
	  }
unknown's avatar
unknown committed
4125
	| DROP key_or_index field_ident
unknown's avatar
unknown committed
4126 4127
	  {
	    LEX *lex=Lex;
4128 4129 4130
	    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
4131
	  }
4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143
	| 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
4144
	| ALTER opt_column field_ident SET DEFAULT signed_literal
unknown's avatar
unknown committed
4145 4146
	  {
	    LEX *lex=Lex;
4147
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,$6));
4148
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
4149
	  }
unknown's avatar
unknown committed
4150
	| ALTER opt_column field_ident DROP DEFAULT
unknown's avatar
unknown committed
4151 4152
	  {
	    LEX *lex=Lex;
4153 4154
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,
                                                                  (Item*) 0));
4155
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
4156
	  }
4157
	| RENAME opt_to table_ident
4158
	  {
4159
	    LEX *lex=Lex;
4160
	    lex->select_lex.db=$3->db.str;
4161
	    lex->name= $3->table.str;
4162 4163 4164
            if (check_table_name($3->table.str,$3->table.length) ||
                $3->db.str && check_db_name($3->db.str))
            {
4165
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3->table.str);
4166 4167
              YYABORT;
            }
4168
	    lex->alter_info.flags|= ALTER_RENAME;
4169
	  }
4170 4171 4172 4173 4174 4175 4176 4177 4178 4179
	| 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))
	    {
4180 4181
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $5->name, $4->csname);
4182 4183
	      YYABORT;
	    }
4184
	    LEX *lex= Lex;
4185
	    lex->create_info.table_charset=
unknown's avatar
unknown committed
4186 4187 4188
	      lex->create_info.default_table_charset= $5;
	    lex->create_info.used_fields|= (HA_CREATE_USED_CHARSET |
					    HA_CREATE_USED_DEFAULT_CHARSET);
4189
	    lex->alter_info.flags|= ALTER_CONVERT;
4190
	  }
4191
        | create_table_options_space_separated
4192 4193
	  {
	    LEX *lex=Lex;
4194
	    lex->alter_info.flags|= ALTER_OPTIONS;
4195
	  }
4196 4197 4198 4199
	| FORCE_SYM
	  {
	    Lex->alter_info.flags|= ALTER_FORCE;
	   }
4200
	| order_clause
4201 4202
	  {
	    LEX *lex=Lex;
4203
	    lex->alter_info.flags|= ALTER_ORDER;
4204
	  };
unknown's avatar
unknown committed
4205 4206 4207

opt_column:
	/* empty */	{}
unknown's avatar
unknown committed
4208
	| COLUMN_SYM	{};
unknown's avatar
unknown committed
4209 4210

opt_ignore:
4211 4212 4213
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
unknown's avatar
unknown committed
4214 4215

opt_restrict:
4216 4217 4218 4219
	/* empty */	{ Lex->drop_mode= DROP_DEFAULT; }
	| RESTRICT	{ Lex->drop_mode= DROP_RESTRICT; }
	| CASCADE	{ Lex->drop_mode= DROP_CASCADE; }
	;
unknown's avatar
unknown committed
4220 4221 4222 4223

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
unknown's avatar
unknown committed
4224
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
unknown's avatar
unknown committed
4225 4226 4227 4228

opt_to:
	/* empty */	{}
	| TO_SYM	{}
4229
	| EQ		{}
unknown's avatar
unknown committed
4230
	| AS		{};
unknown's avatar
unknown committed
4231

unknown's avatar
unknown committed
4232
/*
4233
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
unknown's avatar
unknown committed
4234 4235
*/

unknown's avatar
unknown committed
4236
slave:
4237
	  START_SYM SLAVE slave_thread_opts
4238 4239 4240 4241 4242 4243
          {
	    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
4244
            /* If you change this code don't forget to update SLAVE START too */
4245 4246 4247
          }
          slave_until
          {}
unknown's avatar
unknown committed
4248 4249 4250 4251 4252
        | STOP_SYM SLAVE slave_thread_opts
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_STOP;
	    lex->type = 0;
unknown's avatar
unknown committed
4253
            /* If you change this code don't forget to update SLAVE STOP too */
unknown's avatar
unknown committed
4254
          }
4255 4256 4257 4258 4259
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
4260 4261 4262 4263 4264
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
4265 4266 4267 4268 4269 4270 4271 4272
	| SLAVE STOP_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_STOP;
	   lex->type = 0;
         }
        ;

unknown's avatar
unknown committed
4273

unknown's avatar
unknown committed
4274
start:
4275 4276
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
4277 4278 4279
          LEX *lex= Lex;
          lex->sql_command= SQLCOM_BEGIN;
          lex->start_transaction_opt= $3;
4280
        }
unknown's avatar
unknown committed
4281 4282
	;

4283 4284 4285 4286 4287 4288
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
4289
        ;
4290

unknown's avatar
unknown committed
4291
slave_thread_opts:
4292 4293
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
4294
        {}
unknown's avatar
unknown committed
4295
	;
4296 4297

slave_thread_opt_list:
unknown's avatar
unknown committed
4298
	slave_thread_opt
4299 4300
	| slave_thread_opt_list ',' slave_thread_opt
	;
4301 4302

slave_thread_opt:
4303
	/*empty*/	{}
unknown's avatar
unknown committed
4304
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
unknown's avatar
unknown committed
4305
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
unknown's avatar
unknown committed
4306
	;
4307

4308 4309 4310 4311 4312 4313 4314 4315 4316 4317
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
4318 4319
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


unknown's avatar
unknown committed
4331 4332 4333 4334 4335
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
	}
unknown's avatar
unknown committed
4336
	table_list FROM TEXT_STRING_sys
unknown's avatar
unknown committed
4337 4338
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
4339
        };
unknown's avatar
unknown committed
4340

unknown's avatar
unknown committed
4341 4342 4343 4344 4345
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
	}
unknown's avatar
unknown committed
4346
	table_list TO_SYM TEXT_STRING_sys
unknown's avatar
unknown committed
4347 4348
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
4349
        };
unknown's avatar
unknown committed
4350

4351 4352 4353 4354 4355 4356
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
4357 4358
	table_list opt_checksum_type
        {}
4359 4360
	;

4361 4362 4363 4364 4365 4366
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
4367
repair:
4368
	REPAIR opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
4369
	{
unknown's avatar
unknown committed
4370 4371
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
4372
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
4373
	   lex->check_opt.init();
unknown's avatar
unknown committed
4374
	}
4375 4376 4377
	table_list opt_mi_repair_type
	{}
	;
unknown's avatar
unknown committed
4378

unknown's avatar
unknown committed
4379
opt_mi_repair_type:
unknown's avatar
unknown committed
4380
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
4381
	| mi_repair_types {};
unknown's avatar
unknown committed
4382

unknown's avatar
unknown committed
4383 4384
mi_repair_types:
	mi_repair_type {}
unknown's avatar
unknown committed
4385
	| mi_repair_type mi_repair_types {};
4386

unknown's avatar
unknown committed
4387
mi_repair_type:
unknown's avatar
unknown committed
4388
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
4389
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
unknown's avatar
unknown committed
4390
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
unknown's avatar
unknown committed
4391 4392

analyze:
4393
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
4394
	{
unknown's avatar
unknown committed
4395 4396
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
4397
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
4398
	   lex->check_opt.init();
unknown's avatar
unknown committed
4399
	}
4400 4401 4402
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
4403

4404 4405 4406 4407 4408 4409 4410
binlog_base64_event:
        BINLOG_SYM TEXT_STRING_sys
        {
           Lex->sql_command = SQLCOM_BINLOG_BASE64_EVENT;
           Lex->comment= $2;
        }

unknown's avatar
unknown committed
4411
check:
unknown's avatar
unknown committed
4412
	CHECK_SYM table_or_tables
unknown's avatar
unknown committed
4413
	{
4414 4415 4416 4417 4418 4419 4420 4421 4422
	  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
4423
	}
4424 4425 4426
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
4427

unknown's avatar
unknown committed
4428 4429
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
4430
	| mi_check_types {};
unknown's avatar
unknown committed
4431 4432 4433

mi_check_types:
	mi_check_type {}
unknown's avatar
unknown committed
4434
	| mi_check_type mi_check_types {};
unknown's avatar
unknown committed
4435 4436 4437 4438 4439 4440

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
4441
	| CHANGED  { Lex->check_opt.flags|= T_CHECK_ONLY_CHANGED; };
unknown's avatar
unknown committed
4442

unknown's avatar
unknown committed
4443
optimize:
4444
	OPTIMIZE opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
4445
	{
unknown's avatar
unknown committed
4446 4447
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
unknown's avatar
unknown committed
4448
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
4449
	   lex->check_opt.init();
unknown's avatar
unknown committed
4450
	}
4451 4452 4453
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
4454

4455 4456 4457
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
4458
	| LOCAL_SYM  { $$= 1; }
4459 4460
	;

4461 4462 4463
rename:
	RENAME table_or_tables
	{
4464
          Lex->sql_command= SQLCOM_RENAME_TABLE;
4465
	}
4466 4467
	table_to_table_list
	{}
4468 4469 4470 4471
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
4472
	;
4473

4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486
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;
          }
        ;

4487 4488
table_to_table_list:
	table_to_table
unknown's avatar
unknown committed
4489
	| table_to_table_list ',' table_to_table;
4490 4491 4492

table_to_table:
	table_ident TO_SYM table_ident
4493
	{
unknown's avatar
unknown committed
4494
	  LEX *lex=Lex;
unknown's avatar
unknown committed
4495
	  SELECT_LEX *sl= lex->current_select;
unknown's avatar
unknown committed
4496 4497 4498 4499
	  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))
4500
	    YYABORT;
4501
	};
4502

unknown's avatar
unknown committed
4503
keycache:
unknown's avatar
unknown committed
4504
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
unknown's avatar
unknown committed
4505 4506
        {
          LEX *lex=Lex;
4507
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
4508
	  lex->ident= $5;
unknown's avatar
unknown committed
4509 4510 4511 4512 4513 4514 4515 4516
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
4517
        table_ident cache_keys_spec
unknown's avatar
unknown committed
4518 4519 4520 4521 4522 4523
        {
          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(),
4524
                                      (List<String> *)0))
unknown's avatar
unknown committed
4525 4526 4527 4528
            YYABORT;
        }
        ;

4529 4530 4531
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
4532
	;
4533

unknown's avatar
unknown committed
4534
preload:
unknown's avatar
unknown committed
4535
	LOAD INDEX_SYM INTO CACHE_SYM
unknown's avatar
unknown committed
4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548
	{
	  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
4549
	table_ident cache_keys_spec opt_ignore_leaves
unknown's avatar
unknown committed
4550 4551 4552
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
unknown's avatar
unknown committed
4553 4554
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
unknown's avatar
unknown committed
4555 4556 4557 4558 4559 4560
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

4561
cache_keys_spec:
4562
        { Select->interval_list.empty(); }
4563 4564 4565 4566 4567 4568 4569
        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
4570

unknown's avatar
unknown committed
4571
cache_key_list_or_empty:
4572
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
4573
	| opt_key_or_index '(' key_usage_list2 ')'
4574 4575 4576 4577
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
4578 4579
	;

unknown's avatar
unknown committed
4580
opt_ignore_leaves:
unknown's avatar
unknown committed
4581 4582 4583 4584 4585
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

unknown's avatar
unknown committed
4586
/*
unknown's avatar
unknown committed
4587
  Select : retrieve data from table
unknown's avatar
unknown committed
4588 4589 4590 4591
*/


select:
4592 4593 4594 4595
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
unknown's avatar
unknown committed
4596 4597
	}
	;
unknown's avatar
unknown committed
4598

4599
/* Need select_init2 for subselects. */
unknown's avatar
unknown committed
4600
select_init:
4601
	SELECT_SYM select_init2
4602
	|
unknown's avatar
unknown committed
4603 4604 4605 4606
	'(' select_paren ')' union_opt;

select_paren:
	SELECT_SYM select_part2
4607
	  {
4608
	    LEX *lex= Lex;
unknown's avatar
unknown committed
4609
            SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
4610
	    if (sel->set_braces(1))
4611
	    {
4612
	      yyerror(ER(ER_SYNTAX_ERROR));
4613 4614
	      YYABORT;
	    }
4615 4616
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
4617
	  {
4618
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
4619 4620
	    YYABORT;
	  }
4621
            /* select in braces, can't contain global parameters */
unknown's avatar
unknown committed
4622 4623 4624
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
unknown's avatar
unknown committed
4625 4626
          }
	| '(' select_paren ')';
4627

4628 4629
select_init2:
	select_part2
4630
        {
4631
	  LEX *lex= Lex;
unknown's avatar
unknown committed
4632
          SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
4633
          if (lex->current_select->set_braces(0))
4634
	  {
4635
	    yyerror(ER(ER_SYNTAX_ERROR));
4636 4637
	    YYABORT;
	  }
4638 4639
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
4640
	  {
4641
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
4642 4643
	    YYABORT;
	  }
4644
	}
4645
	union_clause
4646 4647
	;

4648
select_part2:
unknown's avatar
unknown committed
4649
	{
unknown's avatar
unknown committed
4650 4651
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
4652 4653
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
4654
	  lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
4655 4656 4657
	}
	select_options select_item_list
	{
4658
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
4659
	}
unknown's avatar
unknown committed
4660
	select_into select_lock_type;
4661

unknown's avatar
unknown committed
4662
select_into:
unknown's avatar
unknown committed
4663
	opt_order_clause opt_limit_clause {}
4664
        | into
unknown's avatar
unknown committed
4665
	| select_from
4666 4667
	| into select_from
	| select_from into;
unknown's avatar
unknown committed
4668 4669

select_from:
4670 4671
	  FROM join_table_list where_clause group_clause having_clause
	       opt_order_clause opt_limit_clause procedure_clause
unknown's avatar
unknown committed
4672
        | FROM DUAL_SYM where_clause opt_limit_clause
unknown's avatar
unknown committed
4673 4674 4675 4676
          /* 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 :) */
4677
	;
unknown's avatar
unknown committed
4678 4679 4680

select_options:
	/* empty*/
unknown's avatar
unknown committed
4681 4682
	| select_option_list
	  {
unknown's avatar
unknown committed
4683
	    if (Select->options & SELECT_DISTINCT && Select->options & SELECT_ALL)
unknown's avatar
unknown committed
4684
	    {
unknown's avatar
unknown committed
4685
	      my_error(ER_WRONG_USAGE, MYF(0), "ALL", "DISTINCT");
unknown's avatar
unknown committed
4686 4687 4688
              YYABORT;
	    }
          }
unknown's avatar
unknown committed
4689
	  ;
unknown's avatar
unknown committed
4690 4691 4692

select_option_list:
	select_option_list select_option
unknown's avatar
unknown committed
4693
	| select_option;
unknown's avatar
unknown committed
4694 4695

select_option:
4696
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
4697 4698 4699 4700 4701 4702
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
unknown's avatar
unknown committed
4703
	| DISTINCT         { Select->options|= SELECT_DISTINCT; }
4704 4705
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717
	| 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;
	  }
4718
	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
4719 4720 4721 4722
	| SQL_CACHE_SYM
	  {
	    Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
	  }
unknown's avatar
unknown committed
4723
	| ALL		    { Select->options|= SELECT_ALL; }
4724
	;
unknown's avatar
unknown committed
4725

unknown's avatar
unknown committed
4726 4727 4728
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
4729 4730
	  {
	    LEX *lex=Lex;
4731
	    lex->current_select->set_lock_for_tables(TL_WRITE);
4732
	    lex->safe_to_cache_query=0;
4733
	  }
unknown's avatar
unknown committed
4734
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
4735 4736
	  {
	    LEX *lex=Lex;
4737 4738
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
4739
	    lex->safe_to_cache_query=0;
4740 4741
	  }
	;
unknown's avatar
unknown committed
4742

unknown's avatar
unknown committed
4743 4744 4745 4746 4747
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
4748
	    THD *thd= YYTHD;
4749 4750 4751 4752
	    if (add_item_to_list(thd,
                                 new Item_field(&thd->lex->current_select->
                                                context,
                                                NULL, NULL, "*")))
unknown's avatar
unknown committed
4753
	      YYABORT;
4754
	    (thd->lex->current_select->with_wild)++;
unknown's avatar
unknown committed
4755
	  };
unknown's avatar
unknown committed
4756 4757 4758 4759 4760


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
unknown's avatar
unknown committed
4761
	    if (add_item_to_list(YYTHD, $2))
unknown's avatar
unknown committed
4762 4763
	      YYABORT;
	    if ($4.str)
4764 4765 4766 4767
            {
	      $2->set_name($4.str, $4.length, system_charset_info);
              $2->is_autogenerated_name= FALSE;
            }
4768 4769 4770 4771 4772 4773
	    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
4774
	  };
unknown's avatar
unknown committed
4775 4776

remember_name:
unknown's avatar
unknown committed
4777
	{ $$=(char*) Lex->tok_start; };
unknown's avatar
unknown committed
4778 4779

remember_end:
unknown's avatar
unknown committed
4780
	{ $$=(char*) Lex->tok_end; };
unknown's avatar
unknown committed
4781 4782 4783

select_item2:
	table_wild	{ $$=$1; } /* table.* */
unknown's avatar
unknown committed
4784
	| expr		{ $$=$1; };
unknown's avatar
unknown committed
4785 4786

select_alias:
4787
	/* empty */		{ $$=null_lex_str;}
unknown's avatar
unknown committed
4788 4789 4790 4791 4792
	| AS ident		{ $$=$2; }
	| AS TEXT_STRING_sys	{ $$=$2; }
	| ident			{ $$=$1; }
	| TEXT_STRING_sys	{ $$=$1; }
	;
unknown's avatar
unknown committed
4793 4794 4795

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

unknown's avatar
unknown committed
4798
/* all possible expressions */
unknown's avatar
SCRUM  
unknown committed
4799
expr:	
unknown's avatar
unknown committed
4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821
	  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); }
        ;
4822 4823

bool_term:
unknown's avatar
unknown committed
4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846
	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); }
        ;
4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863

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); }
4864 4865 4866 4867 4868
	| 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); }
4869 4870 4871 4872
	| predicate ;

predicate:
	 bit_expr IN_SYM '(' expr_list ')'
unknown's avatar
unknown committed
4873 4874 4875 4876 4877 4878 4879 4880 4881
	  { 
            if ($4->elements == 1)
              $$= new Item_func_eq($1, $4->head());
            else
            {
              $4->push_front($1);
              $$= new Item_func_in(*$4);
            }
          }
4882
	| bit_expr not IN_SYM '(' expr_list ')'
unknown's avatar
unknown committed
4883 4884 4885 4886 4887 4888
          {
            if ($5->elements == 1)
              $$= new Item_func_ne($1, $5->head());
            else
            {
              $5->push_front($1);
4889 4890 4891
              Item_func_in *item = new Item_func_in(*$5);
              item->negate();
              $$= item;
unknown's avatar
unknown committed
4892 4893
            }            
          }
4894 4895 4896
        | bit_expr IN_SYM in_subselect
	  { $$= new Item_in_subselect($1, $3); }
	| bit_expr not IN_SYM in_subselect
4897
          { $$= negate_expression(YYTHD, new Item_in_subselect($1, $4)); }
4898 4899 4900
	| 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
4901 4902 4903 4904 4905
    {
      Item_func_between *item= new Item_func_between($1,$4,$6);
      item->negate();
      $$= item;
    }
4906 4907 4908 4909
	| 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
4910
          { $$= new Item_func_like($1,$3,$4,Lex->escape_used); }
4911
	| bit_expr not LIKE simple_expr opt_escape
4912
          { $$= new Item_func_not(new Item_func_like($1,$4,$5, Lex->escape_used)); }
4913 4914
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
4915
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 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
	| 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
4958

unknown's avatar
unknown committed
4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970
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; }
        ;

4971
interval_expr:
unknown's avatar
unknown committed
4972
         INTERVAL_SYM expr { $$=$2; }
4973 4974
        ;

unknown's avatar
unknown committed
4975 4976
simple_expr:
	simple_ident
4977
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
unknown's avatar
unknown committed
4978
	  {
unknown's avatar
unknown committed
4979 4980 4981
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
4982
                                                            YYTHD->charset()));
4983
	  }
unknown's avatar
unknown committed
4984
	| literal
unknown's avatar
unknown committed
4985
	| param_marker
unknown's avatar
unknown committed
4986
	| '@' ident_or_text SET_VAR expr
unknown's avatar
unknown committed
4987 4988
	  {
	    $$= new Item_func_set_user_var($2,$4);
4989 4990 4991
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4992
	  }
4993
	| '@' ident_or_text
unknown's avatar
unknown committed
4994 4995
	  {
	    $$= new Item_func_get_user_var($2);
4996 4997 4998
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4999
	  }
5000
	| '@' '@' opt_var_ident_type ident_or_text opt_component
unknown's avatar
unknown committed
5001
	  {
5002 5003 5004 5005 5006 5007

            if ($4.str && $5.str && check_reserved_words(&$4))
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
5008
	    if (!($$= get_system_var(YYTHD, $3, $4, $5)))
unknown's avatar
unknown committed
5009
	      YYABORT;
5010
	    Lex->variables_used= 1;
unknown's avatar
unknown committed
5011
	  }
unknown's avatar
unknown committed
5012
	| sum_expr
5013 5014 5015 5016 5017 5018
	| 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
5019
	| '(' expr ')'		{ $$= $2; }
5020 5021 5022 5023 5024
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
5025
	| ROW_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
5026
	  {
5027 5028
	    $5->push_front($3);
	    $$= new Item_row(*$5);
unknown's avatar
unknown committed
5029
	  }
unknown's avatar
unknown committed
5030
	| EXISTS exists_subselect { $$= $2; }
unknown's avatar
unknown committed
5031
	| singlerow_subselect   { $$= $1; }
unknown's avatar
unknown committed
5032
	| '{' ident expr '}'	{ $$= $3; }
5033
        | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
5034
          { $2->push_front($5);
5035 5036
            Select->add_ftfunc_to_list((Item_func_match*)
                                        ($$=new Item_func_match(*$2,$6))); }
unknown's avatar
unknown committed
5037
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
5038
	| BINARY simple_expr %prec NEG
unknown's avatar
unknown committed
5039
	  {
unknown's avatar
unknown committed
5040
            $$= create_func_cast($2, ITEM_CAST_CHAR, -1, 0, &my_charset_bin);
unknown's avatar
unknown committed
5041
	  }
5042
	| CAST_SYM '(' expr AS cast_type ')'
5043
	  {
unknown's avatar
unknown committed
5044
            LEX *lex= Lex;
5045
	    $$= create_func_cast($3, $5,
unknown's avatar
unknown committed
5046 5047 5048
                                 lex->length ? atoi(lex->length) : -1,
                                 lex->dec ? atoi(lex->dec) : 0,
                                 lex->charset);
5049
	  }
unknown's avatar
unknown committed
5050
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
5051
	  { $$= new Item_func_case(* $4, $2, $5 ); }
5052
	| CONVERT_SYM '(' expr ',' cast_type ')'
5053 5054 5055
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
unknown's avatar
unknown committed
5056
                                 Lex->dec ? atoi(Lex->dec) : 0,
5057 5058
				 Lex->charset);
	  }
5059 5060
	| CONVERT_SYM '(' expr USING charset_name ')'
	  { $$= new Item_func_conv_charset($3,$5); }
unknown's avatar
SCRUM  
unknown committed
5061
	| DEFAULT '(' simple_ident ')'
5062 5063 5064 5065 5066
	  {
	    if ($3->is_splocal())
	    {
	      Item_splocal *il= static_cast<Item_splocal *>($3);

5067
	      my_error(ER_WRONG_COLUMN_NAME, MYF(0), il->my_name()->str);
5068 5069
	      YYABORT;
	    }
unknown's avatar
unknown committed
5070
	    $$= new Item_default_value(Lex->current_context(), $3);
5071
	  }
5072
	| VALUES '(' simple_ident_nospvar ')'
unknown's avatar
unknown committed
5073
	  { $$= new Item_insert_value(Lex->current_context(), $3); }
unknown's avatar
unknown committed
5074
	| FUNC_ARG0 '(' ')'
unknown's avatar
unknown committed
5075 5076 5077
	  {
	    if (!$1.symbol->create_func)
	    {
5078 5079 5080
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
5081 5082 5083 5084
	      YYABORT;
	    }
	    $$= ((Item*(*)(void))($1.symbol->create_func))();
	  }
unknown's avatar
unknown committed
5085
	| FUNC_ARG1 '(' expr ')'
unknown's avatar
unknown committed
5086 5087 5088
	  {
	    if (!$1.symbol->create_func)
	    {
5089 5090 5091
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
5092 5093 5094 5095
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);
	  }
unknown's avatar
unknown committed
5096
	| FUNC_ARG2 '(' expr ',' expr ')'
unknown's avatar
unknown committed
5097 5098 5099
	  {
	    if (!$1.symbol->create_func)
	    {
5100 5101 5102
	      my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
5103 5104 5105 5106
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);
	  }
unknown's avatar
unknown committed
5107
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
unknown's avatar
unknown committed
5108 5109 5110
	  {
	    if (!$1.symbol->create_func)
	    {
5111 5112 5113
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
5114 5115 5116 5117
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*,Item*))($1.symbol->create_func))($3,$5,$7);
	  }
unknown's avatar
unknown committed
5118 5119 5120 5121
	| 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); }
5122 5123
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
unknown's avatar
unknown committed
5124 5125 5126 5127 5128 5129
	| 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); }
5130 5131
	| CHAR_SYM '(' expr_list USING charset_name ')'
	  { $$= new Item_func_char(*$3, $5); }
unknown's avatar
unknown committed
5132 5133
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
unknown's avatar
unknown committed
5134 5135
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
unknown's avatar
unknown committed
5136 5137
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
unknown's avatar
unknown committed
5138 5139 5140
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
5141
	  { $5->push_front($3); $$= new Item_func_concat_ws(*$5); }
5142 5143
	| CONVERT_TZ_SYM '(' expr ',' expr ',' expr ')'
	  {
5144 5145
            if (Lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
5146 5147
	    $$= new Item_func_convert_tz($3, $5, $7);
	  }
unknown's avatar
unknown committed
5148
	| CURDATE optional_braces
5149
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
5150
	| CURTIME optional_braces
5151
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
5152
	| CURTIME '(' expr ')'
5153
	  {
5154
	    $$= new Item_func_curtime_local($3);
5155
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
5156
	  }
unknown's avatar
unknown committed
5157
	| CURRENT_USER optional_braces
5158
          { $$= create_func_current_user(); }
5159 5160 5161 5162
	| 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
5163
	| DATABASE '(' ')'
5164
	  {
unknown's avatar
unknown committed
5165
	    $$= new Item_func_database();
5166
            Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
5167
	  }
unknown's avatar
unknown committed
5168
	| DATE_SYM '(' expr ')'
unknown's avatar
unknown committed
5169
	  { $$= new Item_date_typecast($3); }
unknown's avatar
unknown committed
5170 5171
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
unknown's avatar
unknown committed
5172
	| ELT_FUNC '(' expr ',' expr_list ')'
5173
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
unknown's avatar
unknown committed
5174 5175
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
unknown's avatar
unknown committed
5176 5177 5178
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
5179
	    Lex->uncacheable(UNCACHEABLE_RAND);
unknown's avatar
unknown committed
5180
	  }
unknown's avatar
unknown committed
5181
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
5182
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
5183
	  { $$= new Item_func_decode($3,$5.str); }
5184
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
5185
	 { $$= new Item_func_encode($3,$5.str); }
unknown's avatar
unknown committed
5186
	| DES_DECRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
5187
        { $$= new Item_func_des_decrypt($3); }
unknown's avatar
unknown committed
5188
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
5189
        { $$= new Item_func_des_decrypt($3,$5); }
unknown's avatar
unknown committed
5190
	| DES_ENCRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
5191
        { $$= new Item_func_des_encrypt($3); }
unknown's avatar
unknown committed
5192
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
5193
        { $$= new Item_func_des_encrypt($3,$5); }
unknown's avatar
unknown committed
5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205
	| 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
5206
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
unknown's avatar
unknown committed
5207 5208
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
5209
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
unknown's avatar
unknown committed
5210 5211 5212 5213 5214
	| geometry_function
	  {
#ifdef HAVE_SPATIAL
	    $$= $1;
#else
5215 5216
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
5217 5218 5219
	    YYABORT;
#endif
	  }
5220
	| GET_FORMAT '(' date_time_type  ',' expr ')'
5221
	  { $$= new Item_func_get_format($3, $5); }
unknown's avatar
unknown committed
5222 5223 5224 5225 5226 5227
	| 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); }
5228
	| interval_expr interval '+' expr
unknown's avatar
unknown committed
5229
	  /* we cannot put interval before - */
5230 5231 5232 5233 5234
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
5235
              yyerror(ER(ER_SYNTAX_ERROR));
5236 5237 5238 5239
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
unknown's avatar
unknown committed
5240 5241
	| LAST_INSERT_ID '(' ')'
	  {
5242
	    $$= new Item_func_last_insert_id();
5243
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
5244 5245 5246
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
5247
	    $$= new Item_func_last_insert_id($3);
5248
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
5249 5250 5251 5252 5253 5254 5255
	  }
	| 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
5256
	| GREATEST_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
5257 5258 5259
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
5260
	| LOG_SYM '(' expr ')'
unknown's avatar
unknown committed
5261
	  { $$= new Item_func_log($3); }
5262
	| LOG_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
5263
	  { $$= new Item_func_log($3, $5); }
5264
	| MASTER_POS_WAIT '(' expr ',' expr ')'
unknown's avatar
unknown committed
5265
	  {
5266
	    $$= new Item_master_pos_wait($3, $5);
unknown's avatar
unknown committed
5267
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
5268
		  }
5269
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
unknown's avatar
unknown committed
5270
	  {
5271
	    $$= new Item_master_pos_wait($3, $5, $7);
unknown's avatar
unknown committed
5272
	    Lex->safe_to_cache_query=0;
5273
	  }
unknown's avatar
unknown committed
5274 5275
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
unknown's avatar
unknown committed
5276 5277
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
5278 5279
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
unknown's avatar
unknown committed
5280 5281 5282
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
	| NOW_SYM optional_braces
5283
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
5284
	| NOW_SYM '(' expr ')'
5285
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
5286
	| PASSWORD '(' expr ')'
5287
	  {
5288 5289
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
unknown's avatar
unknown committed
5290 5291 5292 5293
	      (Item *) new Item_func_password($3);
	  }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new Item_func_old_password($3); }
5294
	| POSITION_SYM '(' bit_expr IN_SYM expr ')'
unknown's avatar
unknown committed
5295
	  { $$ = new Item_func_locate($5,$3); }
5296 5297
	| QUARTER_SYM '(' expr ')'
	  { $$ = new Item_func_quarter($3); }
unknown's avatar
unknown committed
5298
	| RAND '(' expr ')'
5299
	  { $$= new Item_func_rand($3); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
5300
	| RAND '(' ')'
5301
	  { $$= new Item_func_rand(); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
5302 5303 5304 5305 5306 5307 5308
	| 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); }
5309 5310 5311 5312 5313
	| ROW_COUNT_SYM '(' ')'
	  {
	    $$= new Item_func_row_count();
	    Lex->safe_to_cache_query= 0;
	  }
unknown's avatar
unknown committed
5314 5315 5316 5317
	| 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
5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329
	| 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); }
5330 5331 5332 5333
	| SYSDATE optional_braces
	  { $$= new Item_func_sysdate_local(); Lex->safe_to_cache_query=0;}
	| SYSDATE '(' expr ')'
	  { $$= new Item_func_sysdate_local($3); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
5334
	| TIME_SYM '(' expr ')'
unknown's avatar
unknown committed
5335 5336 5337
	  { $$= new Item_time_typecast($3); }
	| TIMESTAMP '(' expr ')'
	  { $$= new Item_datetime_typecast($3); }
unknown's avatar
unknown committed
5338
	| TIMESTAMP '(' expr ',' expr ')'
unknown's avatar
unknown committed
5339
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
5340 5341 5342 5343
	| 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
5344
	| TRIM '(' expr ')'
5345 5346
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
unknown's avatar
unknown committed
5347
	  { $$= new Item_func_ltrim($6,$4); }
5348
	| TRIM '(' TRAILING expr FROM expr ')'
unknown's avatar
unknown committed
5349
	  { $$= new Item_func_rtrim($6,$4); }
5350
	| TRIM '(' BOTH expr FROM expr ')'
unknown's avatar
unknown committed
5351
	  { $$= new Item_func_trim($6,$4); }
5352 5353 5354 5355 5356 5357
	| 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
5358 5359
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
5360 5361
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
5362
	| ident '.' ident '(' udf_expr_list ')'
unknown's avatar
unknown committed
5363
	  {
5364
	    LEX *lex= Lex;
5365 5366 5367
	    sp_name *name= new sp_name($1, $3);

	    name->init_qname(YYTHD);
5368
	    sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
5369
	    if ($5)
unknown's avatar
unknown committed
5370
	      $$= new Item_func_sp(Lex->current_context(), name, *$5);
unknown's avatar
unknown committed
5371
	    else
unknown's avatar
unknown committed
5372
	      $$= new Item_func_sp(Lex->current_context(), name);
5373
	    lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
5374
	  }
5375
	| IDENT_sys '(' 
5376 5377
          {
#ifdef HAVE_DLOPEN
5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396
            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;
5397
            SELECT_LEX *sel= Select;
5398

5399
            if (udf)
5400
            {
5401 5402 5403
              if (udf->type == UDFTYPE_AGGREGATE)
                Select->in_sum_expr--;

5404 5405 5406 5407
              switch (udf->returns) {
              case STRING_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
5408 5409
                  if ($4 != NULL)
                    $$ = new Item_func_udf_str(udf, *$4);
5410 5411 5412 5413 5414
                  else
                    $$ = new Item_func_udf_str(udf);
                }
                else
                {
5415 5416
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_str(udf, *$4);
5417 5418 5419 5420 5421 5422 5423
                  else
                    $$ = new Item_sum_udf_str(udf);
                }
                break;
              case REAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
5424 5425
                  if ($4 != NULL)
                    $$ = new Item_func_udf_float(udf, *$4);
5426 5427 5428 5429 5430
                  else
                    $$ = new Item_func_udf_float(udf);
                }
                else
                {
5431 5432
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_float(udf, *$4);
5433 5434 5435 5436 5437 5438 5439
                  else
                    $$ = new Item_sum_udf_float(udf);
                }
                break;
              case INT_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
5440 5441
                  if ($4 != NULL)
                    $$ = new Item_func_udf_int(udf, *$4);
5442 5443 5444 5445 5446
                  else
                    $$ = new Item_func_udf_int(udf);
                }
                else
                {
5447 5448
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_int(udf, *$4);
5449 5450 5451 5452
                  else
                    $$ = new Item_sum_udf_int(udf);
                }
                break;
unknown's avatar
unknown committed
5453 5454 5455
              case DECIMAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
5456 5457
                  if ($4 != NULL)
                    $$ = new Item_func_udf_decimal(udf, *$4);
unknown's avatar
unknown committed
5458 5459 5460 5461 5462
                  else
                    $$ = new Item_func_udf_decimal(udf);
                }
                else
                {
5463 5464
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_decimal(udf, *$4);
unknown's avatar
unknown committed
5465 5466 5467 5468
                  else
                    $$ = new Item_sum_udf_decimal(udf);
                }
                break;
5469 5470 5471 5472 5473 5474 5475
              default:
                YYABORT;
              }
            }
            else
#endif /* HAVE_DLOPEN */
            {
5476
	      LEX *lex= Lex;
5477 5478
              sp_name *name= sp_name_current_db_new(YYTHD, $1);

5479
              sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
5480 5481
              if ($4)
                $$= new Item_func_sp(Lex->current_context(), name, *$4);
5482
              else
unknown's avatar
unknown committed
5483
                $$= new Item_func_sp(Lex->current_context(), name);
5484
	      lex->safe_to_cache_query=0;
5485 5486
	    }
          }
unknown's avatar
unknown committed
5487
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
5488
	  {
unknown's avatar
unknown committed
5489 5490
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
unknown's avatar
unknown committed
5491
	| UNIX_TIMESTAMP '(' ')'
unknown's avatar
unknown committed
5492 5493
	  {
	    $$= new Item_func_unix_timestamp();
5494
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
5495
	  }
unknown's avatar
unknown committed
5496 5497 5498
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
5499
	  { $$= new Item_func_user(FALSE); Lex->safe_to_cache_query=0; }
5500 5501 5502 5503 5504 5505
	| 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
5506
	| WEEK_SYM '(' expr ')'
unknown's avatar
unknown committed
5507
	  {
5508
            $$= new Item_func_week($3,new Item_int((char*) "0",
unknown's avatar
unknown committed
5509
				   YYTHD->variables.default_week_format,1));
5510
          }
unknown's avatar
unknown committed
5511 5512 5513 5514 5515 5516 5517 5518
	| 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
5519
	| BENCHMARK_SYM '(' ulong_num ',' expr ')'
5520
	  {
unknown's avatar
unknown committed
5521
	    $$=new Item_func_benchmark($3,$5);
5522
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
5523
	  }
unknown's avatar
unknown committed
5524
	| EXTRACT_SYM '(' interval FROM expr ')'
unknown's avatar
unknown committed
5525
	{ $$=new Item_extract( $3, $5); };
unknown's avatar
unknown committed
5526

unknown's avatar
unknown committed
5527
geometry_function:
unknown's avatar
unknown committed
5528 5529 5530
	  CONTAINS_SYM '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_spatial_rel($3, $5, Item_func::SP_CONTAINS_FUNC)); }
	| GEOMFROMTEXT '(' expr ')'
unknown's avatar
unknown committed
5531 5532 5533 5534 5535 5536 5537 5538 5539
	  { $$= 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,
5540 5541
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
unknown's avatar
unknown committed
5542 5543
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
5544
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
unknown's avatar
unknown committed
5545 5546
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
5547
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561
 	| 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,
5562
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
unknown's avatar
unknown committed
5563 5564
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
5565
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
unknown's avatar
unknown committed
5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577
	| 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,
5578
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
5579 5580 5581 5582 5583 5584 5585 5586 5587 5588
 	| 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)); }
	;

5589 5590 5591 5592 5593 5594
fulltext_options:
        /* nothing */                   { $$= FT_NL;  }
        | WITH QUERY_SYM EXPANSION_SYM  { $$= FT_NL | FT_EXPAND; }
        | IN_SYM BOOLEAN_SYM MODE_SYM   { $$= FT_BOOL; }
        ;

unknown's avatar
unknown committed
5595
udf_expr_list:
5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620
	/* 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)
5621 5622 5623 5624
          {
	    $2->set_name($4.str, $4.length, system_charset_info);
            $2->is_autogenerated_name= FALSE;
          }
5625
	  else
5626
	    $2->set_name($1, (uint) ($3 - $1), YYTHD->charset());
5627 5628 5629
	  $$= $2;
	}
	;
unknown's avatar
unknown committed
5630 5631 5632 5633

sum_expr:
	AVG_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_avg($3); }
5634 5635
	| AVG_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_avg_distinct($4); }
unknown's avatar
unknown committed
5636 5637 5638 5639
	| BIT_AND  '(' in_sum_expr ')'
	  { $$=new Item_sum_and($3); }
	| BIT_OR  '(' in_sum_expr ')'
	  { $$=new Item_sum_or($3); }
5640 5641
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
unknown's avatar
unknown committed
5642
	| COUNT_SYM '(' opt_all '*' ')'
unknown's avatar
unknown committed
5643 5644 5645
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
5646
	| COUNT_SYM '(' DISTINCT
unknown's avatar
unknown committed
5647
	  { Select->in_sum_expr++; }
5648
	   expr_list
unknown's avatar
unknown committed
5649
	  { Select->in_sum_expr--; }
5650 5651
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
unknown's avatar
unknown committed
5652 5653 5654 5655
	| 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); }
5656 5657 5658 5659 5660 5661 5662
/*
   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
5663 5664
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
5665 5666
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
unknown's avatar
unknown committed
5667
	| STD_SYM '(' in_sum_expr ')'
5668
	  { $$=new Item_sum_std($3, 0); }
unknown's avatar
unknown committed
5669
	| VARIANCE_SYM '(' in_sum_expr ')'
5670 5671 5672 5673 5674
	  { $$=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
5675
	| SUM_SYM '(' in_sum_expr ')'
5676
	  { $$=new Item_sum_sum($3); }
5677 5678
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
unknown's avatar
unknown committed
5679 5680 5681 5682 5683
	| GROUP_CONCAT_SYM '(' opt_distinct
	  { Select->in_sum_expr++; }
	  expr_list opt_gorder_clause
	  opt_gconcat_separator
	 ')'
unknown's avatar
unknown committed
5684
	  {
5685 5686
            SELECT_LEX *sel= Select;
	    sel->in_sum_expr--;
unknown's avatar
unknown committed
5687
	    $$=new Item_func_group_concat(Lex->current_context(), $3, $5,
5688
                                          sel->gorder_list, $7);
unknown's avatar
unknown committed
5689
	    $5->empty();
5690 5691 5692 5693 5694 5695 5696
	  };

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

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

5700 5701

opt_gorder_clause:
5702 5703
	  /* empty */
	  {
unknown's avatar
unknown committed
5704
            Select->gorder_list = NULL;
5705 5706 5707
	  }
	| order_clause
          {
unknown's avatar
unknown committed
5708 5709 5710
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
5711
				     sizeof(st_sql_list));
unknown's avatar
unknown committed
5712
	    select->order_list.empty();
5713
	  };
unknown's avatar
unknown committed
5714

unknown's avatar
unknown committed
5715 5716

in_sum_expr:
unknown's avatar
unknown committed
5717
	opt_all
5718 5719 5720 5721
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
5722
	    yyerror(ER(ER_SYNTAX_ERROR));
5723
	    YYABORT;
5724 5725
	  }
	}
unknown's avatar
unknown committed
5726 5727
	expr
	{
unknown's avatar
unknown committed
5728
	  Select->in_sum_expr--;
5729
	  $$= $3;
unknown's avatar
unknown committed
5730
	};
unknown's avatar
unknown committed
5731

5732
cast_type:
unknown's avatar
unknown committed
5733 5734
        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; }
5735
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; Lex->dec=0; }
unknown's avatar
unknown committed
5736 5737 5738 5739 5740 5741 5742 5743
        | 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; }
5744 5745
	;

unknown's avatar
unknown committed
5746
expr_list:
5747
	{ Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
5748
	expr_list2
unknown's avatar
unknown committed
5749
	{ $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
5750 5751

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

unknown's avatar
unknown committed
5755 5756
ident_list_arg:
          ident_list          { $$= $1; }
unknown's avatar
unknown committed
5757
        | '(' ident_list ')'  { $$= $2; };
unknown's avatar
unknown committed
5758

unknown's avatar
unknown committed
5759
ident_list:
5760
        { Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
5761
        ident_list2
unknown's avatar
unknown committed
5762
        { $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
5763 5764

ident_list2:
5765
        simple_ident { Select->expr_list.head()->push_back($1); }
unknown's avatar
unknown committed
5766
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
unknown's avatar
unknown committed
5767 5768 5769

opt_expr:
	/* empty */      { $$= NULL; }
5770
	| expr           { $$= $1; };
unknown's avatar
unknown committed
5771 5772 5773

opt_else:
	/* empty */    { $$= NULL; }
unknown's avatar
unknown committed
5774
	| ELSE expr    { $$= $2; };
unknown's avatar
unknown committed
5775 5776

when_list:
unknown's avatar
unknown committed
5777
        { Select->when_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
5778
	when_list2
unknown's avatar
unknown committed
5779
	{ $$= Select->when_list.pop(); };
unknown's avatar
unknown committed
5780 5781

when_list2:
unknown's avatar
unknown committed
5782
	expr THEN_SYM expr
unknown's avatar
unknown committed
5783
	  {
unknown's avatar
unknown committed
5784
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
5785 5786
	    sel->when_list.head()->push_back($1);
	    sel->when_list.head()->push_back($3);
unknown's avatar
unknown committed
5787
	}
unknown's avatar
unknown committed
5788
	| when_list2 WHEN_SYM expr THEN_SYM expr
unknown's avatar
unknown committed
5789
	  {
unknown's avatar
unknown committed
5790
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
5791 5792
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
unknown's avatar
unknown committed
5793
	  };
unknown's avatar
unknown committed
5794

unknown's avatar
unknown committed
5795
/* Warning - may return NULL in case of incomplete SELECT */
5796 5797 5798
table_ref:
        table_factor            { $$=$1; }
        | join_table            { $$=$1; }
5799
          {
5800 5801 5802
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
5803
          }
5804 5805
        ;

unknown's avatar
unknown committed
5806
join_table_list:
unknown's avatar
unknown committed
5807
	derived_table_list		{ YYERROR_UNLESS($$=$1); }
unknown's avatar
unknown committed
5808
	;
unknown's avatar
unknown committed
5809

unknown's avatar
unknown committed
5810 5811
/* Warning - may return NULL in case of incomplete SELECT */
derived_table_list:
5812
        table_ref { $$=$1; }
unknown's avatar
unknown committed
5813 5814
        | derived_table_list ',' table_ref
          {
unknown's avatar
unknown committed
5815
            YYERROR_UNLESS($1 && ($$=$3));
unknown's avatar
unknown committed
5816
          }
5817 5818
        ;

5819 5820 5821 5822 5823 5824 5825
/*
  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.
*/
5826
join_table:
5827
/* INNER JOIN variants */
unknown's avatar
unknown committed
5828
        /*
5829 5830 5831
          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
5832 5833
        */
        table_ref %prec TABLE_REF_PRIORITY normal_join table_ref
5834
          { YYERROR_UNLESS($1 && ($$=$3)); }
5835
	| table_ref STRAIGHT_JOIN table_factor
unknown's avatar
unknown committed
5836
	  { YYERROR_UNLESS($1 && ($$=$3)); $3->straight=1; }
unknown's avatar
unknown committed
5837 5838 5839 5840 5841
	| table_ref normal_join table_ref
          ON
          {
            YYERROR_UNLESS($1 && ($$=$3));
            /* Change the current name resolution context to a local context. */
5842
            if (push_new_name_resolution_context(YYTHD, $1, $3))
unknown's avatar
unknown committed
5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854
              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. */
5855
            if (push_new_name_resolution_context(YYTHD, $1, $3))
unknown's avatar
unknown committed
5856 5857 5858 5859 5860 5861 5862 5863
              YYABORT;
          }
          expr
          {
            $3->straight=1;
            add_join_on($3,$6);
            Lex->pop_context();
          }
5864
	| table_ref normal_join table_ref
unknown's avatar
unknown committed
5865
	  USING
unknown's avatar
unknown committed
5866
	  {
unknown's avatar
unknown committed
5867
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5868
            YYERROR_UNLESS($1 && $3);
unknown's avatar
unknown committed
5869
	  }
unknown's avatar
unknown committed
5870
	  '(' using_list ')'
unknown's avatar
unknown committed
5871
          { add_join_natural($1,$3,$7); $$=$3; }
5872 5873 5874 5875 5876 5877 5878
	| 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
5879 5880 5881 5882
	| table_ref LEFT opt_outer JOIN_SYM table_ref
          ON
          {
            /* Change the current name resolution context to a local context. */
5883
            if (push_new_name_resolution_context(YYTHD, $1, $5))
unknown's avatar
unknown committed
5884 5885 5886 5887 5888 5889 5890 5891 5892 5893
              YYABORT;
          }
          expr
	  {
            YYERROR_UNLESS($1 && $5);
            add_join_on($5,$8);
            Lex->pop_context();
            $5->outer_join|=JOIN_TYPE_LEFT;
            $$=$5;
          }
5894
	| table_ref LEFT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
5895
	  {
unknown's avatar
unknown committed
5896
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5897
            YYERROR_UNLESS($1 && $5);
unknown's avatar
unknown committed
5898
	  }
unknown's avatar
unknown committed
5899
	  USING '(' using_list ')'
unknown's avatar
unknown committed
5900
          { add_join_natural($1,$5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
5901
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
5902
	  {
unknown's avatar
unknown committed
5903
            YYERROR_UNLESS($1 && $6);
unknown's avatar
unknown committed
5904
 	    add_join_natural($1,$6,NULL);
5905
	    $6->outer_join|=JOIN_TYPE_LEFT;
5906 5907
	    $$=$6;
	  }
5908 5909

/* RIGHT JOIN variants */
unknown's avatar
unknown committed
5910 5911 5912 5913
	| table_ref RIGHT opt_outer JOIN_SYM table_ref
          ON
          {
            /* Change the current name resolution context to a local context. */
5914
            if (push_new_name_resolution_context(YYTHD, $1, $5))
unknown's avatar
unknown committed
5915 5916 5917
              YYABORT;
          }
          expr
unknown's avatar
unknown committed
5918
          {
5919
	    LEX *lex= Lex;
unknown's avatar
unknown committed
5920
            YYERROR_UNLESS($1 && $5);
5921 5922
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
unknown's avatar
unknown committed
5923 5924
            add_join_on($$, $8);
            Lex->pop_context();
5925 5926
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
5927
	  {
unknown's avatar
unknown committed
5928
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5929
            YYERROR_UNLESS($1 && $5);
unknown's avatar
unknown committed
5930
	  }
5931
	  USING '(' using_list ')'
unknown's avatar
unknown committed
5932
          {
5933 5934 5935
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
unknown's avatar
unknown committed
5936
            add_join_natural($$,$5,$9);
5937 5938
          }
	| table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor
5939
	  {
unknown's avatar
unknown committed
5940
            YYERROR_UNLESS($1 && $6);
unknown's avatar
unknown committed
5941
	    add_join_natural($6,$1,NULL);
5942 5943 5944
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
5945
	  };
unknown's avatar
unknown committed
5946 5947

normal_join:
unknown's avatar
unknown committed
5948 5949 5950 5951
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
unknown's avatar
unknown committed
5952

unknown's avatar
unknown committed
5953
/* Warning - may return NULL in case of incomplete SELECT */
5954
table_factor:
unknown's avatar
unknown committed
5955
	{
unknown's avatar
unknown committed
5956
	  SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5957
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
unknown's avatar
unknown committed
5958
	  sel->table_join_options= 0;
unknown's avatar
unknown committed
5959
	}
unknown's avatar
unknown committed
5960
        table_ident opt_table_alias opt_key_definition
unknown's avatar
unknown committed
5961
	{
unknown's avatar
unknown committed
5962
	  LEX *lex= Lex;
unknown's avatar
unknown committed
5963
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
5964
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
unknown's avatar
unknown committed
5965
					   sel->get_table_join_options(),
unknown's avatar
unknown committed
5966
					   lex->lock_option,
5967 5968
					   sel->get_use_index(),
					   sel->get_ignore_index())))
5969
	    YYABORT;
5970
          sel->add_joined_table($$);
unknown's avatar
unknown committed
5971
	}
unknown's avatar
unknown committed
5972 5973 5974 5975
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref
          ON
          {
            /* Change the current name resolution context to a local context. */
5976
            if (push_new_name_resolution_context(YYTHD, $3, $7))
unknown's avatar
unknown committed
5977
              YYABORT;
5978

unknown's avatar
unknown committed
5979 5980 5981 5982 5983 5984 5985 5986 5987
          }
          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
5988
	| select_derived_init get_select_lex select_derived2
5989
          {
5990
            LEX *lex= Lex;
unknown's avatar
unknown committed
5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004
            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
6005
              YYABORT;
unknown's avatar
unknown committed
6006
            $$= 0;
unknown's avatar
unknown committed
6007
            /* incomplete derived tables return NULL, we must be
unknown's avatar
unknown committed
6008
               nested in select_derived rule to be here. */
6009
          }
unknown's avatar
unknown committed
6010 6011 6012 6013 6014
	| '(' 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
6015
          if (!($3 || $6) && $2->embedding &&
unknown's avatar
unknown committed
6016
              !$2->embedding->nested_join->join_list.elements)
6017
          {
unknown's avatar
unknown committed
6018 6019 6020 6021 6022
            /* 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;
6023
          }
unknown's avatar
unknown committed
6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037
          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
6038

unknown's avatar
unknown committed
6039
	      YYABORT;
unknown's avatar
unknown committed
6040
            sel->add_joined_table($$);
unknown's avatar
unknown committed
6041
            lex->pop_context();
unknown's avatar
unknown committed
6042 6043 6044 6045 6046 6047
          }
	  else
          if ($4 || $6)
	  {
            /* simple nested joins cannot have aliases or unions */
            yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
6048
	    YYABORT;
unknown's avatar
unknown committed
6049 6050 6051 6052 6053
	  }
          else
            $$= $3;
	}
        ;
unknown's avatar
unknown committed
6054

unknown's avatar
unknown committed
6055
/* handle contents of parentheses in join expression */
6056
select_derived:
unknown's avatar
unknown committed
6057
	  get_select_lex
unknown's avatar
unknown committed
6058
	  {
unknown's avatar
unknown committed
6059 6060 6061 6062 6063 6064 6065 6066 6067
            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
6068

unknown's avatar
unknown committed
6069 6070 6071 6072
            if (!($$= $1->end_nested_join(lex->thd)) && $3)
              YYABORT;
            if (!$3 && $$)
            {
unknown's avatar
unknown committed
6073 6074
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
unknown's avatar
unknown committed
6075 6076
            }
          }
unknown's avatar
unknown committed
6077
 	;
unknown's avatar
unknown committed
6078

unknown's avatar
unknown committed
6079
select_derived2:
unknown's avatar
unknown committed
6080
        {
unknown's avatar
unknown committed
6081
	  LEX *lex= Lex;
6082
	  lex->derived_tables|= DERIVED_SUBQUERY;
unknown's avatar
unknown committed
6083 6084 6085
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
6086 6087
	  {
	    yyerror(ER(ER_SYNTAX_ERROR));
6088 6089
	    YYABORT;
	  }
6090
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
unknown's avatar
unknown committed
6091
              mysql_new_select(lex, 1))
unknown's avatar
unknown committed
6092 6093
	    YYABORT;
	  mysql_init_select(lex);
6094
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
6095
	  lex->current_select->parsing_place= SELECT_LIST;
6096 6097 6098
	}
        select_options select_item_list
	{
6099
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
6100
	}
unknown's avatar
unknown committed
6101
	opt_select_from
6102
        ;
unknown's avatar
unknown committed
6103

unknown's avatar
unknown committed
6104 6105 6106 6107 6108 6109 6110 6111
get_select_lex:
	/* Empty */ { $$= Select; }
        ;

select_derived_init:
          SELECT_SYM
          {
            LEX *lex= Lex;
unknown's avatar
unknown committed
6112
            SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126
            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
6127 6128
opt_outer:
	/* empty */	{}
unknown's avatar
unknown committed
6129
	| OUTER		{};
unknown's avatar
unknown committed
6130 6131 6132 6133

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
unknown's avatar
unknown committed
6134
          {
unknown's avatar
unknown committed
6135
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
6136 6137 6138
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
6139 6140
	| FORCE_SYM key_usage_list
          {
unknown's avatar
unknown committed
6141
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
6142 6143 6144 6145
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
unknown's avatar
unknown committed
6146
	| IGNORE_SYM key_usage_list
unknown's avatar
unknown committed
6147
	  {
unknown's avatar
unknown committed
6148
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
6149 6150
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
unknown's avatar
unknown committed
6151
	  };
unknown's avatar
unknown committed
6152 6153

key_usage_list:
unknown's avatar
unknown committed
6154
	key_or_index { Select->interval_list.empty(); }
unknown's avatar
unknown committed
6155
        '(' key_list_or_empty ')'
unknown's avatar
unknown committed
6156
        { $$= &Select->interval_list; }
unknown's avatar
unknown committed
6157 6158 6159 6160 6161 6162
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
unknown's avatar
unknown committed
6163 6164 6165

key_usage_list2:
	key_usage_list2 ',' ident
unknown's avatar
unknown committed
6166
        { Select->
unknown's avatar
unknown committed
6167
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $3.str, $3.length,
6168
				    system_charset_info)); }
unknown's avatar
unknown committed
6169
	| ident
unknown's avatar
unknown committed
6170
        { Select->
unknown's avatar
unknown committed
6171
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $1.str, $1.length,
6172
				    system_charset_info)); }
unknown's avatar
unknown committed
6173
	| PRIMARY_SYM
unknown's avatar
unknown committed
6174
        { Select->
unknown's avatar
unknown committed
6175
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
6176
				    system_charset_info)); };
unknown's avatar
unknown committed
6177 6178 6179

using_list:
	ident
unknown's avatar
unknown committed
6180
	  {
unknown's avatar
unknown committed
6181
            if (!($$= new List<String>))
unknown's avatar
unknown committed
6182
	      YYABORT;
unknown's avatar
unknown committed
6183 6184 6185
            $$->push_back(new (YYTHD->mem_root)
                              String((const char *) $1.str, $1.length,
                                      system_charset_info));
unknown's avatar
unknown committed
6186 6187 6188
	  }
	| using_list ',' ident
	  {
unknown's avatar
unknown committed
6189 6190 6191 6192
            $1->push_back(new (YYTHD->mem_root)
                              String((const char *) $3.str, $3.length,
                                      system_charset_info));
            $$= $1;
unknown's avatar
unknown committed
6193
	  };
unknown's avatar
unknown committed
6194 6195

interval:
6196 6197
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
unknown's avatar
unknown committed
6198
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
unknown's avatar
unknown committed
6199 6200
	| DAY_MINUTE_SYM	{ $$=INTERVAL_DAY_MINUTE; }
	| DAY_SECOND_SYM	{ $$=INTERVAL_DAY_SECOND; }
unknown's avatar
unknown committed
6201
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
unknown's avatar
unknown committed
6202 6203
	| HOUR_MINUTE_SYM	{ $$=INTERVAL_HOUR_MINUTE; }
	| HOUR_SECOND_SYM	{ $$=INTERVAL_HOUR_SECOND; }
unknown's avatar
unknown committed
6204 6205
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
unknown's avatar
unknown committed
6206
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
6207 6208 6209 6210 6211 6212 6213 6214
	| 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
6215 6216
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
6217
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
unknown's avatar
unknown committed
6218
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
6219 6220
	| YEAR_SYM		{ $$=INTERVAL_YEAR; }
        ;
unknown's avatar
unknown committed
6221

6222
date_time_type:
6223 6224 6225 6226 6227
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
6228

unknown's avatar
unknown committed
6229 6230 6231
table_alias:
	/* empty */
	| AS
unknown's avatar
unknown committed
6232
	| EQ;
unknown's avatar
unknown committed
6233 6234 6235 6236

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

unknown's avatar
unknown committed
6239 6240 6241 6242
opt_all:
	/* empty */
	| ALL
	;
unknown's avatar
unknown committed
6243 6244

where_clause:
unknown's avatar
unknown committed
6245
	/* empty */  { Select->where= 0; }
unknown's avatar
unknown committed
6246 6247 6248 6249 6250
	| WHERE
          {
            Select->parsing_place= IN_WHERE;
          }
          expr
6251
	  {
unknown's avatar
unknown committed
6252 6253 6254 6255 6256
            SELECT_LEX *select= Select;
	    select->where= $3;
            select->parsing_place= NO_MATTER;
	    if ($3)
	      $3->top_level_item();
6257
	  }
unknown's avatar
unknown committed
6258
 	;
unknown's avatar
unknown committed
6259 6260 6261

having_clause:
	/* empty */
unknown's avatar
unknown committed
6262 6263
	| HAVING
	  {
6264
	    Select->parsing_place= IN_HAVING;
unknown's avatar
unknown committed
6265 6266
          }
	  expr
unknown's avatar
unknown committed
6267
	  {
unknown's avatar
unknown committed
6268
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
6269
	    sel->having= $3;
6270
	    sel->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
6271 6272 6273
	    if ($3)
	      $3->top_level_item();
	  }
6274
	;
unknown's avatar
unknown committed
6275 6276

opt_escape:
6277 6278 6279 6280 6281
	ESCAPE_SYM simple_expr 
          {
            Lex->escape_used= TRUE;
            $$= $2;
          }
6282
	| /* empty */
6283
          {
6284
            Lex->escape_used= FALSE;
6285 6286 6287
            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
6288 6289
          }
        ;
unknown's avatar
unknown committed
6290 6291 6292


/*
unknown's avatar
unknown committed
6293
   group by statement in select
unknown's avatar
unknown committed
6294 6295 6296 6297
*/

group_clause:
	/* empty */
6298
	| GROUP BY group_list olap_opt;
unknown's avatar
unknown committed
6299 6300

group_list:
unknown's avatar
unknown committed
6301
	group_list ',' order_ident order_dir
unknown's avatar
unknown committed
6302
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
6303
	| order_ident order_dir
unknown's avatar
unknown committed
6304
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
6305

6306 6307
olap_opt:
	/* empty */ {}
6308
	| WITH CUBE_SYM
6309
          {
6310
	    LEX *lex=Lex;
6311 6312
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
6313
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
6314 6315 6316
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
6317
	    lex->current_select->olap= CUBE_TYPE;
unknown's avatar
unknown committed
6318
	    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
6319
	    YYABORT;	/* To be deleted in 5.1 */
6320
	  }
6321
	| WITH ROLLUP_SYM
6322
          {
6323 6324 6325
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
6326
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
6327 6328 6329
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
6330
	    lex->current_select->olap= ROLLUP_TYPE;
6331
	  }
6332
	;
6333

unknown's avatar
unknown committed
6334
/*
unknown's avatar
unknown committed
6335
   Order by statement in select
unknown's avatar
unknown committed
6336 6337
*/

6338
opt_order_clause:
unknown's avatar
unknown committed
6339
	/* empty */
unknown's avatar
unknown committed
6340
	| order_clause;
6341 6342

order_clause:
6343 6344
	ORDER_SYM BY
        {
unknown's avatar
unknown committed
6345
	  LEX *lex=Lex;
6346
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
unknown's avatar
unknown committed
6347
	      lex->current_select->olap !=
6348
	      UNSPECIFIED_OLAP_TYPE)
6349
	  {
unknown's avatar
unknown committed
6350 6351
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
6352 6353
	    YYABORT;
	  }
unknown's avatar
unknown committed
6354
	} order_list;
unknown's avatar
unknown committed
6355 6356 6357

order_list:
	order_list ',' order_ident order_dir
unknown's avatar
unknown committed
6358
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
6359
	| order_ident order_dir
unknown's avatar
unknown committed
6360
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
6361 6362 6363

order_dir:
	/* empty */ { $$ =  1; }
unknown's avatar
unknown committed
6364
	| ASC  { $$ =1; }
unknown's avatar
unknown committed
6365
	| DESC { $$ =0; };
unknown's avatar
unknown committed
6366 6367


6368 6369 6370
opt_limit_clause_init:
	/* empty */
	{
6371 6372
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
6373 6374
          sel->offset_limit= 0;
          sel->select_limit= 0;
6375
	}
6376 6377 6378
	| limit_clause {}
	;

6379 6380 6381 6382 6383
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

6384
limit_clause:
6385
	LIMIT limit_options {}
unknown's avatar
unknown committed
6386
	;
unknown's avatar
unknown committed
6387 6388

limit_options:
6389
	limit_option
unknown's avatar
unknown committed
6390
	  {
unknown's avatar
unknown committed
6391
            SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
6392
            sel->select_limit= $1;
6393
            sel->offset_limit= 0;
6394
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
6395
	  }
6396
	| limit_option ',' limit_option
unknown's avatar
unknown committed
6397
	  {
unknown's avatar
unknown committed
6398
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
6399 6400
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
6401
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
6402
	  }
6403
	| limit_option OFFSET_SYM limit_option
unknown's avatar
unknown committed
6404
	  {
unknown's avatar
unknown committed
6405
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
6406 6407
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
6408
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
6409 6410
	  }
	;
6411 6412 6413 6414 6415
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
6416
        ;
unknown's avatar
unknown committed
6417 6418 6419 6420

delete_limit_clause:
	/* empty */
	{
unknown's avatar
unknown committed
6421
	  LEX *lex=Lex;
6422
	  lex->current_select->select_limit= 0;
unknown's avatar
unknown committed
6423
	}
6424
	| LIMIT limit_option
6425 6426
	{
	  SELECT_LEX *sel= Select;
6427
	  sel->select_limit= $2;
6428 6429
	  sel->explicit_limit= 1;
	};
unknown's avatar
unknown committed
6430

unknown's avatar
unknown committed
6431 6432
ulong_num:
          NUM           { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
6433
	| HEX_NUM       { $$= (ulong) strtol($1.str, (char**) 0, 16); }
unknown's avatar
unknown committed
6434 6435
	| 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
6436
        | DECIMAL_NUM   { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
6437 6438
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
6439

unknown's avatar
unknown committed
6440
ulonglong_num:
unknown's avatar
unknown committed
6441 6442 6443
	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
6444
        | DECIMAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
6445 6446
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
6447 6448 6449 6450 6451 6452

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
6453 6454
	    if (&lex->select_lex != lex->current_select)
	    {
unknown's avatar
unknown committed
6455
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
6456 6457
	      YYABORT;
	    }
unknown's avatar
unknown committed
6458 6459 6460
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
6461 6462 6463 6464
	    if (add_proc_to_list(lex->thd, new Item_field(&lex->
                                                          current_select->
                                                          context,
                                                          NULL,NULL,$2.str)))
unknown's avatar
unknown committed
6465
	      YYABORT;
6466
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
6467
	  }
unknown's avatar
unknown committed
6468
	  '(' procedure_list ')';
unknown's avatar
unknown committed
6469 6470 6471 6472


procedure_list:
	/* empty */ {}
unknown's avatar
unknown committed
6473
	| procedure_list2 {};
unknown's avatar
unknown committed
6474 6475 6476

procedure_list2:
	procedure_list2 ',' procedure_item
unknown's avatar
unknown committed
6477
	| procedure_item;
unknown's avatar
unknown committed
6478 6479 6480 6481

procedure_item:
	  remember_name expr
	  {
unknown's avatar
unknown committed
6482 6483
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
unknown's avatar
unknown committed
6484 6485
	      YYABORT;
	    if (!$2->name)
6486 6487
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1),
                           YYTHD->charset());
6488 6489 6490 6491 6492 6493
	  }
          ;


select_var_list_init:
	   {
unknown's avatar
unknown committed
6494 6495
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
6496 6497
	        YYABORT;
	   }
6498
	   select_var_list
6499
	   {}
6500
           ;
unknown's avatar
unknown committed
6501

unknown's avatar
unknown committed
6502
select_var_list:
6503 6504 6505 6506
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

6507 6508 6509 6510 6511 6512 6513 6514 6515 6516
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
6517
           {
6518
             LEX *lex=Lex;
6519 6520 6521 6522
	     sp_pvar_t *t;

	     if (!lex->spcont || !(t=lex->spcont->find_pvar(&$1)))
	     {
6523
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
unknown's avatar
unknown committed
6524
	       YYABORT;
6525 6526 6527 6528 6529
	     }
	     if (! lex->result)
	       YYABORT;
	     else
	     {
6530 6531 6532
               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
6533
#ifndef DBUG_OFF
6534
	       if (var)
6535
		 var->sp= lex->sphead;
6536
#endif
6537
	     }
unknown's avatar
unknown committed
6538
	   }
unknown's avatar
unknown committed
6539
           ;
unknown's avatar
unknown committed
6540

6541
into:
unknown's avatar
unknown committed
6542
        INTO OUTFILE TEXT_STRING_sys
unknown's avatar
unknown committed
6543
	{
unknown's avatar
unknown committed
6544
          LEX *lex= Lex;
6545 6546 6547 6548
          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
6549 6550
	}
	opt_field_term opt_line_term
unknown's avatar
unknown committed
6551
	| INTO DUMPFILE TEXT_STRING_sys
unknown's avatar
unknown committed
6552
	{
6553
	  LEX *lex=Lex;
unknown's avatar
unknown committed
6554 6555
	  if (!lex->describe)
	  {
6556
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
6557 6558 6559 6560 6561
	    if (!(lex->exchange= new sql_exchange($3.str,1)))
	      YYABORT;
	    if (!(lex->result= new select_dump(lex->exchange)))
	      YYABORT;
	  }
unknown's avatar
unknown committed
6562
	}
6563
        | INTO select_var_list_init
unknown's avatar
unknown committed
6564
	{
6565
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
6566 6567
	}
        ;
unknown's avatar
unknown committed
6568

unknown's avatar
unknown committed
6569 6570 6571
/*
  DO statement
*/
unknown's avatar
unknown committed
6572

6573
do:	DO_SYM
unknown's avatar
unknown committed
6574 6575 6576
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
6577 6578 6579 6580 6581
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
unknown's avatar
unknown committed
6582
	}
6583 6584
	;

unknown's avatar
unknown committed
6585
/*
6586
  Drop : delete tables or index or user
unknown's avatar
unknown committed
6587 6588 6589
*/

drop:
unknown's avatar
unknown committed
6590
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
unknown's avatar
unknown committed
6591
	{
unknown's avatar
unknown committed
6592 6593
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
6594 6595
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
unknown's avatar
unknown committed
6596
	}
unknown's avatar
unknown committed
6597
	| DROP INDEX_SYM ident ON table_ident {}
unknown's avatar
unknown committed
6598
	  {
unknown's avatar
unknown committed
6599 6600
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
6601 6602 6603
	     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
6604 6605
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
unknown's avatar
unknown committed
6606 6607 6608 6609
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
unknown's avatar
unknown committed
6610 6611 6612 6613
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
	    lex->name=$4.str;
unknown's avatar
unknown committed
6614
	 }
6615
	| DROP FUNCTION_SYM if_exists sp_name
unknown's avatar
unknown committed
6616
	  {
unknown's avatar
unknown committed
6617
	    LEX *lex=Lex;
6618 6619
	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
6620
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
6621 6622
	      YYABORT;
	    }
unknown's avatar
unknown committed
6623
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
6624 6625 6626 6627 6628 6629 6630 6631
	    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
6632
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
6633 6634 6635 6636 6637
	      YYABORT;
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
6638
	  }
6639
	| DROP USER clear_privileges user_list
6640
	  {
6641 6642
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
6643 6644
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
6645
	    LEX *lex= Lex;
6646 6647 6648
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
6649
        | DROP TRIGGER_SYM sp_name
6650 6651 6652
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_DROP_TRIGGER;
6653
            lex->spname= $3;
6654 6655
          }
	;
unknown's avatar
unknown committed
6656 6657

table_list:
6658
	table_name
unknown's avatar
unknown committed
6659
	| table_list ',' table_name;
unknown's avatar
unknown committed
6660

6661
table_name:
unknown's avatar
unknown committed
6662
	table_ident
unknown's avatar
unknown committed
6663 6664 6665 6666 6667
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
unknown's avatar
unknown committed
6668 6669

if_exists:
6670
	/* empty */ { $$= 0; }
6671 6672
	| IF EXISTS { $$= 1; }
	;
unknown's avatar
unknown committed
6673

6674 6675 6676 6677
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
unknown's avatar
unknown committed
6678 6679 6680 6681 6682
/*
** Insert : add new data to table
*/

insert:
6683 6684 6685
	INSERT
	{
	  LEX *lex= Lex;
6686 6687
	  lex->sql_command= SQLCOM_INSERT;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
6688
	  mysql_init_select(lex);
6689 6690 6691
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
	} insert_lock_option
6692
	opt_ignore insert2
unknown's avatar
unknown committed
6693
	{
unknown's avatar
unknown committed
6694
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
6695
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
6696
	}
6697
	insert_field_spec opt_insert_update
6698
	{}
unknown's avatar
unknown committed
6699
	;
unknown's avatar
unknown committed
6700 6701

replace:
6702 6703
	REPLACE
	{
6704
	  LEX *lex=Lex;
6705 6706
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
unknown's avatar
unknown committed
6707
	  mysql_init_select(lex);
6708
	}
unknown's avatar
unknown committed
6709 6710
	replace_lock_option insert2
	{
unknown's avatar
unknown committed
6711
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
6712
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
6713 6714
	}
	insert_field_spec
6715
	{}
unknown's avatar
unknown committed
6716
	;
unknown's avatar
unknown committed
6717 6718

insert_lock_option:
unknown's avatar
unknown committed
6719 6720 6721 6722
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
6723
	;
unknown's avatar
unknown committed
6724 6725

replace_lock_option:
unknown's avatar
unknown committed
6726 6727
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
unknown's avatar
unknown committed
6728 6729 6730

insert2:
	INTO insert_table {}
unknown's avatar
unknown committed
6731
	| insert_table {};
unknown's avatar
unknown committed
6732 6733

insert_table:
6734
	table_name
unknown's avatar
unknown committed
6735
	{
unknown's avatar
unknown committed
6736 6737 6738 6739
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
unknown's avatar
unknown committed
6740
	};
unknown's avatar
unknown committed
6741 6742

insert_field_spec:
unknown's avatar
unknown committed
6743 6744 6745
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
unknown's avatar
unknown committed
6746 6747
	| SET
	  {
unknown's avatar
unknown committed
6748 6749 6750
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
6751 6752
	      YYABORT;
	   }
unknown's avatar
unknown committed
6753
	   ident_eq_list;
unknown's avatar
unknown committed
6754 6755 6756

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
unknown's avatar
unknown committed
6757
	| insert_ident		{ Lex->field_list.push_back($1); };
unknown's avatar
unknown committed
6758 6759 6760

insert_values:
	VALUES	values_list  {}
unknown's avatar
unknown committed
6761
	| VALUE_SYM values_list  {}
unknown's avatar
unknown committed
6762 6763
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
6764
        ;
unknown's avatar
unknown committed
6765 6766 6767

values_list:
	values_list ','  no_braces
unknown's avatar
unknown committed
6768
	| no_braces;
unknown's avatar
unknown committed
6769 6770 6771 6772

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
unknown's avatar
unknown committed
6773
	ident_eq_value;
unknown's avatar
unknown committed
6774 6775

ident_eq_value:
6776
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
6777
	 {
unknown's avatar
unknown committed
6778 6779 6780
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
unknown's avatar
unknown committed
6781
	    YYABORT;
unknown's avatar
unknown committed
6782
	 };
unknown's avatar
unknown committed
6783 6784

equal:	EQ		{}
unknown's avatar
unknown committed
6785 6786 6787 6788 6789 6790 6791
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
unknown's avatar
unknown committed
6792 6793 6794 6795 6796 6797 6798 6799 6800

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
unknown's avatar
unknown committed
6801 6802
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
6803
	    YYABORT;
unknown's avatar
unknown committed
6804
	 };
unknown's avatar
unknown committed
6805 6806 6807

opt_values:
	/* empty */ {}
unknown's avatar
unknown committed
6808
	| values;
unknown's avatar
unknown committed
6809 6810

values:
unknown's avatar
unknown committed
6811
	values ','  expr_or_default
unknown's avatar
unknown committed
6812 6813 6814 6815
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
unknown's avatar
unknown committed
6816 6817 6818 6819 6820 6821 6822 6823
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
6824
	expr	  { $$= $1;}
unknown's avatar
unknown committed
6825
	| DEFAULT {$$= new Item_default_value(Lex->current_context()); }
unknown's avatar
unknown committed
6826
	;
unknown's avatar
unknown committed
6827

6828 6829
opt_insert_update:
        /* empty */
unknown's avatar
unknown committed
6830
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
unknown's avatar
unknown committed
6831
          KEY_SYM UPDATE_SYM insert_update_list
6832 6833
        ;

unknown's avatar
unknown committed
6834 6835 6836
/* Update rows in a table */

update:
6837 6838
	UPDATE_SYM
	{
6839
	  LEX *lex= Lex;
unknown's avatar
unknown committed
6840
	  mysql_init_select(lex);
6841
          lex->sql_command= SQLCOM_UPDATE;
unknown's avatar
unknown committed
6842
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
6843
	  lex->duplicates= DUP_ERROR; 
6844
        }
unknown's avatar
unknown committed
6845
        opt_low_priority opt_ignore join_table_list
unknown's avatar
unknown committed
6846
	SET update_list
unknown's avatar
unknown committed
6847
	{
6848 6849
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
unknown's avatar
unknown committed
6850
            lex->sql_command= SQLCOM_UPDATE_MULTI;
6851 6852 6853
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
6854 6855
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
6856 6857
	    YYABORT;
	  }
6858 6859 6860 6861 6862 6863
          /*
            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
6864
	}
6865
	where_clause opt_order_clause delete_limit_clause {}
unknown's avatar
unknown committed
6866
	;
unknown's avatar
unknown committed
6867 6868

update_list:
unknown's avatar
unknown committed
6869 6870 6871 6872
	update_list ',' update_elem
	| update_elem;

update_elem:
unknown's avatar
unknown committed
6873
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
6874
	{
unknown's avatar
unknown committed
6875
	  if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
unknown's avatar
unknown committed
6876
	    YYABORT;
unknown's avatar
unknown committed
6877 6878 6879 6880 6881 6882 6883
	};

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
unknown's avatar
unknown committed
6884
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
6885
	  {
unknown's avatar
unknown committed
6886 6887 6888
	  LEX *lex= Lex;
	  if (lex->update_list.push_back($1) || 
	      lex->value_list.push_back($3))
unknown's avatar
unknown committed
6889
	      YYABORT;
unknown's avatar
unknown committed
6890
	  };
unknown's avatar
unknown committed
6891 6892

opt_low_priority:
6893
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
6894
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
6895 6896 6897 6898

/* Delete rows from a table */

delete:
6899
	DELETE_SYM
6900
	{
6901 6902
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
unknown's avatar
unknown committed
6903
	  mysql_init_select(lex);
unknown's avatar
unknown committed
6904
	  lex->lock_option= lex->thd->update_lock_default;
6905
	  lex->ignore= 0;
6906
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
6907
	}
unknown's avatar
unknown committed
6908 6909
	opt_delete_options single_multi {}
	;
6910 6911

single_multi:
unknown's avatar
unknown committed
6912 6913
 	FROM table_ident
	{
unknown's avatar
unknown committed
6914 6915
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
unknown's avatar
unknown committed
6916 6917 6918
	    YYABORT;
	}
	where_clause opt_order_clause
6919
	delete_limit_clause {}
unknown's avatar
unknown committed
6920
	| table_wild_list
unknown's avatar
unknown committed
6921
	  { mysql_init_multi_delete(Lex); }
unknown's avatar
unknown committed
6922
          FROM join_table_list where_clause
6923 6924 6925 6926
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
unknown's avatar
unknown committed
6927 6928
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
6929
	  USING join_table_list where_clause
6930 6931 6932 6933
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
6934
	;
unknown's avatar
unknown committed
6935 6936 6937

table_wild_list:
	  table_wild_one {}
unknown's avatar
unknown committed
6938
	  | table_wild_list ',' table_wild_one {};
unknown's avatar
unknown committed
6939 6940

table_wild_one:
unknown's avatar
unknown committed
6941
	ident opt_wild opt_table_alias
unknown's avatar
unknown committed
6942
	{
unknown's avatar
unknown committed
6943 6944
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
					 TL_OPTION_UPDATING, Lex->lock_option))
unknown's avatar
unknown committed
6945 6946
	    YYABORT;
        }
unknown's avatar
unknown committed
6947
	| ident '.' ident opt_wild opt_table_alias
unknown's avatar
unknown committed
6948
	  {
unknown's avatar
unknown committed
6949 6950
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
unknown's avatar
unknown committed
6951 6952
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
unknown's avatar
unknown committed
6953
	      YYABORT;
unknown's avatar
unknown committed
6954
	  }
unknown's avatar
unknown committed
6955
	;
unknown's avatar
unknown committed
6956 6957

opt_wild:
6958
	/* empty */	{}
unknown's avatar
unknown committed
6959
	| '.' '*'	{};
unknown's avatar
unknown committed
6960 6961


6962
opt_delete_options:
unknown's avatar
unknown committed
6963
	/* empty */	{}
unknown's avatar
unknown committed
6964
	| opt_delete_option opt_delete_options {};
6965 6966

opt_delete_option:
6967
	QUICK		{ Select->options|= OPTION_QUICK; }
6968
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
6969
	| IGNORE_SYM	{ Lex->ignore= 1; };
6970

6971
truncate:
unknown's avatar
unknown committed
6972
	TRUNCATE_SYM opt_table_sym table_name
6973
	{
6974
	  LEX* lex= Lex;
6975
	  lex->sql_command= SQLCOM_TRUNCATE;
6976
	  lex->select_lex.options= 0;
6977
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
6978 6979
	}
	;
6980

unknown's avatar
unknown committed
6981 6982
opt_table_sym:
	/* empty */
unknown's avatar
unknown committed
6983
	| TABLE_SYM;
6984

unknown's avatar
unknown committed
6985 6986
/* Show things */

6987 6988
show:	SHOW
	{
unknown's avatar
unknown committed
6989 6990
	  LEX *lex=Lex;
	  lex->wild=0;
6991 6992 6993
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
6994 6995
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
unknown's avatar
unknown committed
6996
	show_param
6997 6998
	{}
	;
unknown's avatar
unknown committed
6999 7000

show_param:
7001
         DATABASES wild_and_where
7002 7003 7004 7005 7006 7007 7008
         {
           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;
         }
7009
         | opt_full TABLES opt_db wild_and_where
7010 7011 7012 7013
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLES;
7014
             lex->select_lex.db= $3;
7015 7016 7017
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLE_NAMES))
               YYABORT;
           }
7018 7019 7020 7021 7022 7023 7024 7025 7026
         | 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;
           }
7027
         | TABLE_SYM STATUS_SYM opt_db wild_and_where
7028 7029 7030 7031
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLE_STATUS;
7032
             lex->select_lex.db= $3;
7033 7034 7035
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLES))
               YYABORT;
           }
7036
        | OPEN_SYM TABLES opt_db wild_and_where
unknown's avatar
unknown committed
7037
	  {
7038
	    LEX *lex= Lex;
7039 7040
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_OPEN_TABLES;
7041
	    lex->select_lex.db= $3;
7042 7043
            if (prepare_schema_table(YYTHD, lex, 0, SCH_OPEN_TABLES))
              YYABORT;
7044
	  }
unknown's avatar
unknown committed
7045 7046 7047 7048 7049 7050 7051 7052
        | 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
7053
	| ENGINE_SYM storage_engines 
unknown's avatar
unknown committed
7054 7055
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
7056
	| ENGINE_SYM ALL 
unknown's avatar
unknown committed
7057
	  { Lex->create_info.db_type= NULL; }
7058
	  show_engine_param
7059
	| opt_full COLUMNS from_or_in table_ident opt_db wild_and_where
unknown's avatar
unknown committed
7060
	  {
7061 7062 7063
 	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SELECT;
	    lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
7064 7065 7066
	    if ($5)
	      $4->change_db($5);
	    if (prepare_schema_table(YYTHD, lex, $4, SCH_COLUMNS))
unknown's avatar
unknown committed
7067 7068
	      YYABORT;
	  }
7069
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
unknown's avatar
unknown committed
7070 7071
	  TEXT_STRING_sys AND_SYM MASTER_LOG_POS_SYM EQ ulonglong_num
	  AND_SYM MASTER_SERVER_ID_SYM EQ
unknown's avatar
unknown committed
7072
	ulong_num
unknown's avatar
unknown committed
7073 7074 7075 7076
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
7077
	    Lex->mi.server_id = $16;
unknown's avatar
unknown committed
7078
          }
7079
        | master_or_binary LOGS_SYM
unknown's avatar
unknown committed
7080 7081
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
7082 7083 7084 7085 7086
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
unknown's avatar
unknown committed
7087
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
unknown's avatar
unknown committed
7088
          {
7089 7090
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
7091
          } opt_limit_clause_init
7092
        | keys_or_index from_or_in table_ident opt_db where_clause
7093 7094 7095 7096
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_KEYS;
7097 7098 7099
	    if ($4)
	      $3->change_db($4);
            if (prepare_schema_table(YYTHD, lex, $3, SCH_STATISTICS))
7100
              YYABORT;
unknown's avatar
unknown committed
7101
	  }
unknown's avatar
unknown committed
7102 7103 7104 7105 7106 7107 7108 7109
	| 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
7110 7111 7112 7113 7114 7115 7116
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
	    WARN_DEPRECATED("SHOW TABLE TYPES", "SHOW [STORAGE] ENGINES");
	  }
	| opt_storage ENGINES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
unknown's avatar
unknown committed
7117 7118 7119
            lex->orig_sql_command= SQLCOM_SHOW_AUTHORS;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_ENGINES))
              YYABORT;
unknown's avatar
unknown committed
7120
	  }
unknown's avatar
unknown committed
7121 7122 7123 7124 7125
	| AUTHORS_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_AUTHORS;
	  }
unknown's avatar
unknown committed
7126 7127 7128 7129 7130
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
7131
        | COUNT_SYM '(' '*' ')' WARNINGS
7132
          { (void) create_select_for_variable("warning_count"); }
7133
        | COUNT_SYM '(' '*' ')' ERRORS
7134
	  { (void) create_select_for_variable("error_count"); }
7135
        | WARNINGS opt_limit_clause_init
unknown's avatar
unknown committed
7136
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
7137
        | ERRORS opt_limit_clause_init
7138
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
7139
        | opt_var_type STATUS_SYM wild_and_where
7140
          {
7141 7142 7143
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS;
7144
            lex->option_type= $1;
7145 7146
            if (prepare_schema_table(YYTHD, lex, 0, SCH_STATUS))
              YYABORT;
7147
	  }	
unknown's avatar
unknown committed
7148
        | INNOBASE_SYM STATUS_SYM
7149 7150 7151
          {
            LEX *lex= Lex;
            lex->sql_command = SQLCOM_SHOW_ENGINE_STATUS;
unknown's avatar
unknown committed
7152 7153 7154 7155 7156 7157
            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;
            }
7158 7159
            WARN_DEPRECATED("SHOW INNODB STATUS", "SHOW ENGINE INNODB STATUS");
	  }
unknown's avatar
unknown committed
7160
        | MUTEX_SYM STATUS_SYM
7161 7162 7163
          {
	    LEX *lex= Lex;
            lex->sql_command = SQLCOM_SHOW_ENGINE_MUTEX; 
unknown's avatar
unknown committed
7164 7165 7166 7167 7168 7169
            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;
            }
7170 7171
            WARN_DEPRECATED("SHOW MUTEX STATUS", "SHOW ENGINE INNODB MUTEX");
	  }
unknown's avatar
unknown committed
7172 7173
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
7174
        | opt_var_type  VARIABLES wild_and_where
unknown's avatar
unknown committed
7175
	  {
7176 7177 7178
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_VARIABLES;
7179
            lex->option_type= $1;
7180 7181
            if (prepare_schema_table(YYTHD, lex, 0, SCH_VARIABLES))
              YYABORT;
7182
	  }
7183
        | charset wild_and_where
7184 7185 7186 7187 7188 7189 7190
          {
            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;
          }
7191
        | COLLATION_SYM wild_and_where
7192 7193 7194 7195 7196 7197 7198
          {
            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;
          }
7199
	| BERKELEY_DB_SYM LOGS_SYM
7200 7201 7202
	  {
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_ENGINE_LOGS;
unknown's avatar
unknown committed
7203 7204 7205 7206 7207 7208
            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;
            }
7209 7210
	    WARN_DEPRECATED("SHOW BDB LOGS", "SHOW ENGINE BDB LOGS");
	  }
unknown's avatar
unknown committed
7211
	| LOGS_SYM
7212 7213 7214
	  {
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_ENGINE_LOGS;
unknown's avatar
unknown committed
7215 7216 7217 7218 7219 7220
            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;
            }
7221 7222
	    WARN_DEPRECATED("SHOW LOGS", "SHOW ENGINE BDB LOGS");
	  }
7223 7224 7225 7226 7227
	| GRANTS
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    THD *thd= lex->thd;
7228
            Security_context *sctx= thd->security_ctx;
7229 7230 7231
	    LEX_USER *curr_user;
            if (!(curr_user= (LEX_USER*) thd->alloc(sizeof(st_lex_user))))
              YYABORT;
7232 7233 7234
            curr_user->user.str= sctx->priv_user;
            curr_user->user.length= strlen(sctx->priv_user);
            if (*sctx->priv_host != 0)
7235
            {
7236 7237
              curr_user->host.str= sctx->priv_host;
              curr_user->host.length= strlen(sctx->priv_host);
7238 7239 7240 7241 7242 7243
            }
            else
            {
              curr_user->host.str= (char *) "%";
              curr_user->host.length= 1;
            }
7244
            curr_user->password=null_lex_str;
7245 7246
	    lex->grant_user= curr_user;
	  }
unknown's avatar
unknown committed
7247
	| GRANTS FOR_SYM user
unknown's avatar
unknown committed
7248 7249 7250 7251
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
7252
	    lex->grant_user->password=null_lex_str;
unknown's avatar
unknown committed
7253
	  }
unknown's avatar
unknown committed
7254
	| CREATE DATABASE opt_if_not_exists ident
unknown's avatar
unknown committed
7255 7256
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
unknown's avatar
unknown committed
7257 7258
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
unknown's avatar
unknown committed
7259
	  }
unknown's avatar
unknown committed
7260 7261
        | CREATE TABLE_SYM table_ident
          {
7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272
            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
7273
	      YYABORT;
7274
            lex->only_view= 1;
unknown's avatar
unknown committed
7275 7276 7277 7278
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
unknown's avatar
unknown committed
7279
          }
unknown's avatar
unknown committed
7280 7281 7282
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297
          }
	| 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;
	  }
7298
	| PROCEDURE STATUS_SYM wild_and_where
7299
	  {
7300 7301 7302
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_PROC;
7303 7304
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
7305 7306
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
7307
	  }
7308
	| FUNCTION_SYM STATUS_SYM wild_and_where
7309
	  {
7310 7311 7312
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_FUNC;
7313 7314
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
7315 7316
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
unknown's avatar
unknown committed
7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338
	  }
        | 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
          }
        ;
unknown's avatar
unknown committed
7339

unknown's avatar
unknown committed
7340 7341
show_engine_param:
	STATUS_SYM
7342 7343 7344
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_STATUS; }
	| MUTEX_SYM
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_MUTEX; }
unknown's avatar
unknown committed
7345
	| LOGS_SYM
7346
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_LOGS; };
unknown's avatar
unknown committed
7347

7348 7349 7350 7351
master_or_binary:
	MASTER_SYM
	| BINARY;

unknown's avatar
unknown committed
7352 7353 7354 7355
opt_storage:
	/* empty */
	| STORAGE_SYM;

unknown's avatar
unknown committed
7356 7357
opt_db:
	/* empty */  { $$= 0; }
unknown's avatar
unknown committed
7358
	| from_or_in ident { $$= $2.str; };
unknown's avatar
unknown committed
7359

unknown's avatar
unknown committed
7360 7361
opt_full:
	/* empty */ { Lex->verbose=0; }
unknown's avatar
unknown committed
7362
	| FULL	    { Lex->verbose=1; };
unknown's avatar
unknown committed
7363

unknown's avatar
unknown committed
7364 7365
from_or_in:
	FROM
unknown's avatar
unknown committed
7366
	| IN_SYM;
unknown's avatar
unknown committed
7367

unknown's avatar
unknown committed
7368 7369
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
unknown's avatar
unknown committed
7370
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
unknown's avatar
unknown committed
7371 7372 7373

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

7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388
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
7389

unknown's avatar
unknown committed
7390 7391 7392 7393
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
7394 7395 7396 7397 7398 7399 7400 7401 7402
          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
7403 7404
	    YYABORT;
	}
7405
	opt_describe_column {}
unknown's avatar
unknown committed
7406 7407 7408
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
7409
          {
unknown's avatar
unknown committed
7410
	    LEX *lex=Lex;
7411
	    lex->select_lex.options|= SELECT_DESCRIBE;
unknown's avatar
unknown committed
7412 7413
	  }
	;
unknown's avatar
unknown committed
7414 7415 7416

describe_command:
	DESC
unknown's avatar
unknown committed
7417
	| DESCRIBE;
unknown's avatar
unknown committed
7418

unknown's avatar
unknown committed
7419 7420 7421
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
unknown's avatar
unknown committed
7422
	| PARTITIONS_SYM { Lex->describe|= DESCRIBE_PARTITIONS; }
unknown's avatar
unknown committed
7423 7424
	;

unknown's avatar
unknown committed
7425

unknown's avatar
unknown committed
7426 7427 7428
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
unknown's avatar
unknown committed
7429
	| ident
unknown's avatar
unknown committed
7430
	  { Lex->wild= new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info); };
unknown's avatar
unknown committed
7431 7432 7433 7434 7435


/* flush things */

flush:
7436
	FLUSH_SYM opt_no_write_to_binlog
unknown's avatar
unknown committed
7437 7438
	{
	  LEX *lex=Lex;
7439
	  if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
7440
	  {
7441 7442 7443 7444 7445 7446
            /*
              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");
7447 7448
	    YYABORT;
	  }
unknown's avatar
unknown committed
7449
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
unknown's avatar
unknown committed
7450
          lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
7451
	}
7452 7453 7454
	flush_options
	{}
	;
unknown's avatar
unknown committed
7455 7456 7457

flush_options:
	flush_options ',' flush_option
unknown's avatar
unknown committed
7458
	| flush_option;
unknown's avatar
unknown committed
7459 7460

flush_option:
7461
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
unknown's avatar
unknown committed
7462
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
unknown's avatar
unknown committed
7463
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
unknown's avatar
unknown committed
7464 7465 7466 7467
	| 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
7468 7469
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
7470 7471
	| DES_KEY_FILE	{ Lex->type|= REFRESH_DES_KEY_FILE; }
 	| RESOURCES     { Lex->type|= REFRESH_USER_RESOURCES; };
unknown's avatar
unknown committed
7472

unknown's avatar
unknown committed
7473
opt_table_list:
7474 7475
	/* empty */  {;}
	| table_list {;};
unknown's avatar
unknown committed
7476

unknown's avatar
unknown committed
7477
reset:
unknown's avatar
unknown committed
7478 7479 7480 7481
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
7482 7483 7484 7485
	} reset_options
	{}
	;

unknown's avatar
unknown committed
7486 7487
reset_options:
	reset_options ',' reset_option
unknown's avatar
unknown committed
7488
	| reset_option;
unknown's avatar
unknown committed
7489 7490

reset_option:
unknown's avatar
unknown committed
7491 7492
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
unknown's avatar
unknown committed
7493
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
unknown's avatar
unknown committed
7494

unknown's avatar
unknown committed
7495
purge:
unknown's avatar
unknown committed
7496 7497 7498 7499
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
7500 7501 7502 7503 7504
	} purge_options
	{}
	;

purge_options:
7505
	master_or_binary LOGS_SYM purge_option
unknown's avatar
unknown committed
7506
	;
7507 7508

purge_option:
unknown's avatar
unknown committed
7509
        TO_SYM TEXT_STRING_sys
7510 7511 7512 7513 7514 7515
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
7516 7517 7518 7519
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
unknown's avatar
unknown committed
7520
	}
unknown's avatar
unknown committed
7521
	;
unknown's avatar
unknown committed
7522

unknown's avatar
unknown committed
7523 7524 7525
/* kill threads */

kill:
7526
	KILL_SYM kill_option expr
unknown's avatar
unknown committed
7527
	{
unknown's avatar
unknown committed
7528
	  LEX *lex=Lex;
7529 7530 7531
	  lex->value_list.empty();
	  lex->value_list.push_front($3);
          lex->sql_command= SQLCOM_KILL;
unknown's avatar
unknown committed
7532
	};
unknown's avatar
unknown committed
7533

7534 7535 7536 7537 7538
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; };

unknown's avatar
unknown committed
7539 7540 7541
/* change database */

use:	USE_SYM ident
unknown's avatar
unknown committed
7542 7543
	{
	  LEX *lex=Lex;
7544 7545
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
unknown's avatar
unknown committed
7546
	};
unknown's avatar
unknown committed
7547 7548 7549

/* import, export of files */

unknown's avatar
unknown committed
7550 7551 7552
load:   LOAD DATA_SYM 
        {
          LEX *lex=Lex;
7553 7554 7555 7556 7557
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD DATA");
	    YYABORT;
	  }
unknown's avatar
unknown committed
7558 7559 7560 7561 7562 7563 7564
          lex->fname_start= lex->ptr;
        }
        load_data
        {}
        |
        LOAD TABLE_SYM table_ident FROM MASTER_SYM
        {
7565 7566 7567 7568 7569 7570 7571
	  LEX *lex=Lex;
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD TABLE");
	    YYABORT;
	  }
          lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
unknown's avatar
unknown committed
7572 7573 7574 7575 7576 7577
          if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
            YYABORT;
        };

load_data:
	load_data_lock opt_local INFILE TEXT_STRING_sys
unknown's avatar
unknown committed
7578
	{
unknown's avatar
unknown committed
7579 7580
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
unknown's avatar
unknown committed
7581 7582
	  lex->lock_option= $1;
	  lex->local_file=  $2;
7583 7584
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
unknown's avatar
unknown committed
7585
	  if (!(lex->exchange= new sql_exchange($4.str, 0)))
unknown's avatar
unknown committed
7586
	    YYABORT;
unknown's avatar
unknown committed
7587 7588 7589 7590 7591
        }
        opt_duplicate INTO
        {
	  LEX *lex=Lex;
	  lex->fname_end= lex->ptr;
unknown's avatar
unknown committed
7592
	}
7593
        TABLE_SYM table_ident
unknown's avatar
unknown committed
7594
        {
7595 7596 7597 7598 7599 7600 7601
          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
7602
        }
7603 7604 7605
        opt_field_term opt_line_term opt_ignore_lines opt_field_or_var_spec
        opt_load_data_set_spec
        {}
7606
        |
unknown's avatar
unknown committed
7607
	FROM MASTER_SYM
7608 7609
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
unknown's avatar
unknown committed
7610
        };
unknown's avatar
unknown committed
7611 7612 7613

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

unknown's avatar
unknown committed
7616
load_data_lock:
7617
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
7618 7619
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
7620 7621


unknown's avatar
unknown committed
7622 7623 7624
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
7625
	| IGNORE_SYM	{ Lex->ignore= 1; };
unknown's avatar
unknown committed
7626 7627 7628

opt_field_term:
	/* empty */
unknown's avatar
unknown committed
7629
	| COLUMNS field_term_list;
unknown's avatar
unknown committed
7630 7631 7632

field_term_list:
	field_term_list field_term
unknown's avatar
unknown committed
7633
	| field_term;
unknown's avatar
unknown committed
7634 7635

field_term:
7636 7637
	TERMINATED BY text_string 
          {
7638
            DBUG_ASSERT(Lex->exchange != 0);
7639 7640
            Lex->exchange->field_term= $3;
          }
unknown's avatar
unknown committed
7641
	| OPTIONALLY ENCLOSED BY text_string
unknown's avatar
unknown committed
7642
	  {
unknown's avatar
unknown committed
7643
            LEX *lex= Lex;
7644
            DBUG_ASSERT(lex->exchange != 0);
unknown's avatar
unknown committed
7645 7646
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
unknown's avatar
unknown committed
7647
	  }
unknown's avatar
unknown committed
7648
        | ENCLOSED BY text_string
7649
          {
7650
            DBUG_ASSERT(Lex->exchange != 0);
7651 7652
            Lex->exchange->enclosed= $3;
          }
unknown's avatar
unknown committed
7653
        | ESCAPED BY text_string
7654
          {
7655
            DBUG_ASSERT(Lex->exchange != 0);
7656 7657
            Lex->exchange->escaped= $3;
          };
unknown's avatar
unknown committed
7658 7659 7660

opt_line_term:
	/* empty */
unknown's avatar
unknown committed
7661
	| LINES line_term_list;
unknown's avatar
unknown committed
7662 7663 7664

line_term_list:
	line_term_list line_term
unknown's avatar
unknown committed
7665
	| line_term;
unknown's avatar
unknown committed
7666 7667

line_term:
unknown's avatar
unknown committed
7668
        TERMINATED BY text_string
7669
          {
7670
            DBUG_ASSERT(Lex->exchange != 0);
7671 7672
            Lex->exchange->line_term= $3;
          }
unknown's avatar
unknown committed
7673
        | STARTING BY text_string
7674
          {
7675
            DBUG_ASSERT(Lex->exchange != 0);
7676 7677
            Lex->exchange->line_start= $3;
          };
unknown's avatar
unknown committed
7678 7679 7680

opt_ignore_lines:
	/* empty */
unknown's avatar
unknown committed
7681 7682
        | IGNORE_SYM NUM LINES
          {
7683
            DBUG_ASSERT(Lex->exchange != 0);
7684 7685
            Lex->exchange->skip_lines= atol($2.str);
          };
unknown's avatar
unknown committed
7686

unknown's avatar
unknown committed
7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697
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
7698

unknown's avatar
unknown committed
7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709
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
7710 7711 7712
/* Common definitions */

text_literal:
7713
	TEXT_STRING_literal
unknown's avatar
unknown committed
7714 7715
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7716
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
unknown's avatar
unknown committed
7717
	}
unknown's avatar
unknown committed
7718
	| NCHAR_STRING
7719
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
7720
	| UNDERSCORE_CHARSET TEXT_STRING
7721
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
7722
	| text_literal TEXT_STRING_literal
unknown's avatar
unknown committed
7723 7724
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
unknown's avatar
unknown committed
7725 7726

text_string:
7727
	TEXT_STRING_literal
unknown's avatar
unknown committed
7728
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
unknown's avatar
unknown committed
7729 7730
	| HEX_NUM
	  {
unknown's avatar
unknown committed
7731
	    Item *tmp= new Item_hex_string($1.str, $1.length);
7732
	    /*
unknown's avatar
unknown committed
7733
	      it is OK only emulate fix_fields, because we need only
7734 7735 7736
              value of constant
	    */
	    $$= tmp ?
unknown's avatar
unknown committed
7737
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
7738
	      (String*) 0;
7739
	  }
unknown's avatar
unknown committed
7740 7741 7742 7743 7744 7745 7746 7747 7748 7749
        | 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;
          }
7750 7751
	;

unknown's avatar
unknown committed
7752
param_marker:
7753
        PARAM_MARKER
unknown's avatar
unknown committed
7754
        {
7755 7756
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
7757 7758 7759
          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
7760
          {
7761
            my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
unknown's avatar
unknown committed
7762 7763
            YYABORT;
          }
7764 7765 7766
        }
	;

unknown's avatar
unknown committed
7767 7768 7769
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
7770 7771
	| '-' NUM_literal
	  {
7772
	    $2->max_length++;
unknown's avatar
unknown committed
7773
	    $$= $2->neg();
7774
	  }
unknown's avatar
unknown committed
7775 7776 7777
	;


unknown's avatar
unknown committed
7778 7779
literal:
	text_literal	{ $$ =	$1; }
unknown's avatar
unknown committed
7780
	| NUM_literal	{ $$ = $1; }
unknown's avatar
unknown committed
7781
	| NULL_SYM	{ $$ =	new Item_null();
7782
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
7783 7784
	| FALSE_SYM	{ $$= new Item_int((char*) "FALSE",0,1); }
	| TRUE_SYM	{ $$= new Item_int((char*) "TRUE",1,1); }
unknown's avatar
unknown committed
7785 7786
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
7787
	| UNDERSCORE_CHARSET HEX_NUM
7788
	  {
unknown's avatar
unknown committed
7789
	    Item *tmp= new Item_hex_string($2.str, $2.length);
7790
	    /*
unknown's avatar
unknown committed
7791
	      it is OK only emulate fix_fieds, because we need only
7792 7793 7794
              value of constant
	    */
	    String *str= tmp ?
unknown's avatar
unknown committed
7795
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
7796
	      (String*) 0;
unknown's avatar
unknown committed
7797 7798 7799
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
7800
	  }
unknown's avatar
unknown committed
7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814
	| 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
7815 7816
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
unknown's avatar
unknown committed
7817
	| TIMESTAMP text_literal { $$ = $2; };
unknown's avatar
unknown committed
7818

7819
NUM_literal:
7820 7821
	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); }
7822
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
unknown's avatar
unknown committed
7823
        | DECIMAL_NUM
7824
	{
unknown's avatar
unknown committed
7825
           $$= new Item_decimal($1.str, $1.length, YYTHD->charset());
7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
7839
	;
unknown's avatar
unknown committed
7840

unknown's avatar
unknown committed
7841
/**********************************************************************
unknown's avatar
unknown committed
7842
** Creating different items.
unknown's avatar
unknown committed
7843 7844 7845
**********************************************************************/

insert_ident:
7846
	simple_ident_nospvar { $$=$1; }
unknown's avatar
unknown committed
7847
	| table_wild	 { $$=$1; };
unknown's avatar
unknown committed
7848 7849

table_wild:
unknown's avatar
unknown committed
7850
	ident '.' '*'
7851
	{
7852
          SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7853
	  $$ = new Item_field(Lex->current_context(), NullS, $1.str, "*");
7854
	  sel->with_wild++;
7855
	}
unknown's avatar
unknown committed
7856
	| ident '.' ident '.' '*'
7857
	{
7858
          SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7859
	  $$ = new Item_field(Lex->current_context(), (YYTHD->client_capabilities &
unknown's avatar
unknown committed
7860 7861
                             CLIENT_NO_SCHEMA ? NullS : $1.str),
                             $3.str,"*");
7862
	  sel->with_wild++;
7863 7864
	}
	;
unknown's avatar
unknown committed
7865 7866

order_ident:
unknown's avatar
unknown committed
7867
	expr { $$=$1; };
unknown's avatar
unknown committed
7868 7869

simple_ident:
7870 7871 7872 7873 7874 7875
	ident
	{
	  sp_pvar_t *spv;
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;
	  if (spc && (spv = spc->find_pvar(&$1)))
7876 7877 7878
	  {
            /* We're compiling a stored procedure and found a variable */
            Item_splocal *splocal;
7879 7880
            splocal= new Item_splocal($1, spv->offset, spv->type,
                                      lex->tok_start_prev - 
7881
                                      lex->sphead->m_tmp_query);
unknown's avatar
unknown committed
7882
#ifndef DBUG_OFF
7883
            if (splocal)
7884
              splocal->m_sp= lex->sphead;
7885
#endif
7886
	    $$ = (Item*) splocal;
7887 7888 7889 7890 7891 7892 7893 7894
            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
7895 7896
                 (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	         (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
7897 7898 7899 7900 7901 7902
	  }
        }
        | simple_ident_q { $$= $1; }
	;

simple_ident_nospvar:
unknown's avatar
unknown committed
7903
	ident
unknown's avatar
unknown committed
7904
	{
unknown's avatar
unknown committed
7905
	  SELECT_LEX *sel=Select;
7906
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
7907
	       sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
7908 7909
              (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
7910
	}
7911
	| simple_ident_q { $$= $1; }
unknown's avatar
unknown committed
7912
	;
7913 7914 7915

simple_ident_q:
	ident '.' ident
unknown's avatar
unknown committed
7916
	{
unknown's avatar
unknown committed
7917
	  THD *thd= YYTHD;
7918
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
7919

7920 7921 7922 7923 7924 7925
          /*
            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
7926
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") ||
7927 7928
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
7929
            Item_trigger_field *trg_fld;
7930
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
unknown's avatar
unknown committed
7931

7932 7933 7934
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
unknown's avatar
unknown committed
7935
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
7936 7937
              YYABORT;
            }
unknown's avatar
unknown committed
7938

7939 7940 7941
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
unknown's avatar
unknown committed
7942
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
7943 7944
              YYABORT;
            }
unknown's avatar
unknown committed
7945

unknown's avatar
unknown committed
7946
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
7947
                                                  new_row ?
7948 7949 7950
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
                                                  $3.str)))
7951
              YYABORT;
unknown's avatar
unknown committed
7952

7953 7954 7955 7956 7957 7958
            /*
              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
7959

7960 7961 7962 7963 7964 7965 7966
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
7967 7968
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
7969 7970 7971
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
7972 7973
	        (Item*) new Item_field(Lex->current_context(), NullS, $1.str, $3.str) :
	        (Item*) new Item_ref(Lex->current_context(), NullS, $1.str, $3.str);
7974 7975
          }
        }
unknown's avatar
unknown committed
7976
	| '.' ident '.' ident
unknown's avatar
unknown committed
7977
	{
unknown's avatar
unknown committed
7978
	  THD *thd= YYTHD;
7979
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
7980
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
7981 7982
	  if (sel->no_table_names_allowed)
	  {
7983 7984
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $2.str, thd->where);
unknown's avatar
unknown committed
7985
	  }
7986
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
7987
	       sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
7988 7989
	      (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
7990
	}
unknown's avatar
unknown committed
7991
	| ident '.' ident '.' ident
unknown's avatar
unknown committed
7992
	{
unknown's avatar
unknown committed
7993
	  THD *thd= YYTHD;
7994
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
7995
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
7996 7997
	  if (sel->no_table_names_allowed)
	  {
7998 7999
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $3.str, thd->where);
unknown's avatar
unknown committed
8000
	  }
8001
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
8002
	       sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
8003
	      (Item*) new Item_field(Lex->current_context(),
8004
                                     (YYTHD->client_capabilities &
unknown's avatar
unknown committed
8005 8006
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
unknown's avatar
unknown committed
8007
	      (Item*) new Item_ref(Lex->current_context(),
8008
                                   (YYTHD->client_capabilities &
8009
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
unknown's avatar
unknown committed
8010
                                   $3.str, $5.str);
unknown's avatar
unknown committed
8011
	};
unknown's avatar
unknown committed
8012 8013 8014 8015


field_ident:
	ident			{ $$=$1;}
8016 8017 8018 8019 8020
	| ident '.' ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->db))
            {
8021
              my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
8022 8023
              YYABORT;
            }
8024 8025
            if (my_strcasecmp(table_alias_charset, $3.str,
                              table->table_name))
8026
            {
8027
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3.str);
8028 8029 8030 8031 8032 8033 8034 8035 8036
              YYABORT;
            }
            $$=$5;
          }
	| ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->alias))
            {
8037
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $1.str);
8038 8039 8040 8041
              YYABORT;
            }
            $$=$3;
          }
unknown's avatar
unknown committed
8042
	| '.' ident		{ $$=$2;}	/* For Delphi */;
unknown's avatar
unknown committed
8043 8044 8045

table_ident:
	ident			{ $$=new Table_ident($1); }
unknown's avatar
unknown committed
8046
	| ident '.' ident	{ $$=new Table_ident(YYTHD, $1,$3,0);}
8047 8048 8049
	| '.' ident		{ $$=new Table_ident($2);} /* For Delphi */
        ;

unknown's avatar
unknown committed
8050
table_ident_nodb:
unknown's avatar
unknown committed
8051
	ident			{ LEX_STRING db={(char*) any_db,3}; $$=new Table_ident(YYTHD, db,$1,0); }
8052
        ;
unknown's avatar
unknown committed
8053

unknown's avatar
unknown committed
8054
IDENT_sys:
8055 8056 8057 8058 8059
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
8060 8061
            {
              CHARSET_INFO *cs= system_charset_info;
8062
              int dummy_error;
8063 8064
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
8065
                                                   $1.length, &dummy_error);
8066 8067
              if (wlen < $1.length)
              {
8068 8069
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
8070 8071
                YYABORT;
              }
8072
	      $$= $1;
8073
            }
8074 8075 8076 8077
	    else
	      thd->convert_string(&$$, system_charset_info,
				  $1.str, $1.length, thd->charset());
	  }
unknown's avatar
unknown committed
8078 8079 8080 8081 8082 8083
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
8084 8085
	  if (thd->charset_is_system_charset)
	    $$= $1;
unknown's avatar
unknown committed
8086
	  else
unknown's avatar
unknown committed
8087 8088
	    thd->convert_string(&$$, system_charset_info,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
8089 8090 8091
	}
	;

8092
TEXT_STRING_literal:
unknown's avatar
unknown committed
8093 8094 8095
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
8096 8097
	  if (thd->charset_is_collation_connection)
	    $$= $1;
unknown's avatar
unknown committed
8098
	  else
unknown's avatar
unknown committed
8099 8100
	    thd->convert_string(&$$, thd->variables.collation_connection,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
8101 8102 8103 8104
	}
	;


unknown's avatar
unknown committed
8105
ident:
unknown's avatar
unknown committed
8106
	IDENT_sys	    { $$=$1; }
unknown's avatar
unknown committed
8107 8108
	| keyword
	{
unknown's avatar
unknown committed
8109 8110 8111
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
8112 8113
	}
	;
unknown's avatar
unknown committed
8114

8115 8116 8117 8118 8119 8120 8121 8122 8123 8124
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
8125
ident_or_text:
unknown's avatar
unknown committed
8126
        ident                   { $$=$1;}
unknown's avatar
unknown committed
8127 8128
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
unknown's avatar
unknown committed
8129 8130 8131 8132

user:
	ident_or_text
	{
unknown's avatar
unknown committed
8133 8134
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
8135
	    YYABORT;
8136 8137 8138 8139
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
unknown's avatar
unknown committed
8140 8141
	| ident_or_text '@' ident_or_text
	  {
unknown's avatar
unknown committed
8142 8143
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
8144 8145
	      YYABORT;
	    $$->user = $1; $$->host=$3;
8146
	  }
unknown's avatar
unknown committed
8147
	| CURRENT_USER optional_braces
8148 8149
	{
          THD *thd= YYTHD;
8150
          Security_context *sctx= thd->security_ctx;
8151 8152
          if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
            YYABORT;
8153 8154 8155
          $$->user.str= sctx->priv_user;
          $$->user.length= strlen(sctx->priv_user);
          if (*sctx->priv_host != 0)
8156
          {
8157 8158
            $$->host.str= sctx->priv_host;
            $$->host.length= strlen(sctx->priv_host);
8159 8160 8161 8162 8163 8164 8165
          }
          else
          {
            $$->host.str= (char *) "%";
            $$->host.length= 1;
          }
	};
unknown's avatar
unknown committed
8166

8167
/* Keyword that we allow for identifiers (except SP labels) */
unknown's avatar
unknown committed
8168
keyword:
8169 8170
	keyword_sp		{}
	| ASCII_SYM		{}
unknown's avatar
unknown committed
8171
	| AUTHORS_SYM		{}
8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188
	| 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		{}
8189
        | INSTALL_SYM           {}
8190 8191 8192
	| LANGUAGE_SYM          {}
	| NO_SYM		{}
	| OPEN_SYM		{}
8193
        | PARSER_SYM            {}
8194
	| PARTITION_SYM		{}
8195
        | PLUGIN_SYM            {}
8196 8197 8198 8199 8200 8201 8202 8203 8204
        | PREPARE_SYM           {}
	| REPAIR		{}
	| RESET_SYM		{}
	| RESTORE_SYM		{}
	| ROLLBACK_SYM		{}
	| SAVEPOINT_SYM		{}
	| SECURITY_SYM		{}
	| SIGNED_SYM		{}
	| SLAVE			{}
8205
        | SONAME_SYM            {}
8206 8207 8208 8209
	| START_SYM		{}
	| STOP_SYM		{}
	| TRUNCATE_SYM		{}
	| UNICODE_SYM		{}
8210
        | UNINSTALL_SYM         {}
8211 8212 8213 8214 8215
        | XA_SYM                {}
	;

/*
 * Keywords that we allow for labels in SPs.
unknown's avatar
unknown committed
8216 8217 8218
 * Anything that's the beginning of a statement or characteristics
 * must be in keyword above, otherwise we get (harmful) shift/reduce
 * conflicts.
8219 8220
 */
keyword_sp:
unknown's avatar
unknown committed
8221
	ACTION			{}
unknown's avatar
unknown committed
8222
	| ADDDATE_SYM		{}
unknown's avatar
unknown committed
8223
	| AFTER_SYM		{}
8224
	| AGAINST		{}
unknown's avatar
unknown committed
8225
	| AGGREGATE_SYM		{}
8226
	| ALGORITHM_SYM		{}
unknown's avatar
unknown committed
8227
	| ANY_SYM		{}
unknown's avatar
unknown committed
8228
	| AUTO_INC		{}
unknown's avatar
unknown committed
8229 8230
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
unknown's avatar
unknown committed
8231
	| BERKELEY_DB_SYM	{}
unknown's avatar
unknown committed
8232
	| BINLOG_SYM		{}
unknown's avatar
unknown committed
8233 8234
	| BIT_SYM		{}
	| BOOL_SYM		{}
unknown's avatar
unknown committed
8235
	| BOOLEAN_SYM		{}
unknown's avatar
unknown committed
8236
	| BTREE_SYM		{}
8237
	| CASCADED              {}
unknown's avatar
unknown committed
8238
	| CHAIN_SYM		{}
8239
	| CHANGED		{}
8240
	| CIPHER_SYM		{}
unknown's avatar
unknown committed
8241
	| CLIENT_SYM		{}
unknown's avatar
unknown committed
8242
	| COALESCE		{}
8243
	| CODE_SYM              {}
unknown's avatar
unknown committed
8244
	| COLLATION_SYM		{}
8245
        | COLUMNS               {}
unknown's avatar
unknown committed
8246
	| COMMITTED_SYM		{}
8247
	| COMPACT_SYM		{}
unknown's avatar
unknown committed
8248
	| COMPRESSED_SYM	{}
unknown's avatar
unknown committed
8249
	| CONCURRENT		{}
8250
	| CONSISTENT_SYM	{}
8251
	| CUBE_SYM		{}
unknown's avatar
unknown committed
8252 8253 8254 8255
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
8256
	| DEFINER_SYM		{}
unknown's avatar
unknown committed
8257
	| DELAY_KEY_WRITE_SYM	{}
unknown's avatar
unknown committed
8258 8259
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
unknown's avatar
unknown committed
8260
	| DISCARD		{}
unknown's avatar
unknown committed
8261 8262
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
unknown's avatar
unknown committed
8263 8264
	| DYNAMIC_SYM		{}
	| ENUM			{}
unknown's avatar
unknown committed
8265
	| ENGINE_SYM		{}
unknown's avatar
unknown committed
8266
	| ENGINES_SYM		{}
8267
	| ERRORS		{}
unknown's avatar
unknown committed
8268
	| ESCAPE_SYM		{}
unknown's avatar
unknown committed
8269
	| EVENTS_SYM		{}
8270
        | EXPANSION_SYM         {}
unknown's avatar
unknown committed
8271
	| EXTENDED_SYM		{}
8272
	| FAST_SYM		{}
8273
	| FOUND_SYM		{}
unknown's avatar
unknown committed
8274 8275
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
unknown's avatar
unknown committed
8276
	| FULL			{}
unknown's avatar
unknown committed
8277 8278 8279
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
8280
	| FRAC_SECOND_SYM	{}
8281
	| GEOMETRY_SYM		{}
8282
	| GEOMETRYCOLLECTION	{}
8283
	| GET_FORMAT		{}
unknown's avatar
unknown committed
8284
	| GRANTS		{}
unknown's avatar
unknown committed
8285
	| GLOBAL_SYM		{}
unknown's avatar
unknown committed
8286
	| HASH_SYM		{}
unknown's avatar
unknown committed
8287 8288 8289
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
8290
	| INVOKER_SYM		{}
unknown's avatar
unknown committed
8291
	| IMPORT		{}
unknown's avatar
unknown committed
8292
	| INDEXES		{}
unknown's avatar
unknown committed
8293
	| ISOLATION		{}
unknown's avatar
unknown committed
8294
	| ISSUER_SYM		{}
unknown's avatar
unknown committed
8295
	| INNOBASE_SYM		{}
8296
	| INSERT_METHOD		{}
unknown's avatar
unknown committed
8297
	| RELAY_THREAD		{}
8298
	| LAST_SYM		{}
unknown's avatar
unknown committed
8299
	| LEAVES                {}
8300
	| LESS_SYM		{}
unknown's avatar
unknown committed
8301
	| LEVEL_SYM		{}
8302
	| LINESTRING		{}
8303
	| LIST_SYM		{}
unknown's avatar
unknown committed
8304
	| LOCAL_SYM		{}
unknown's avatar
unknown committed
8305
	| LOCKS_SYM		{}
unknown's avatar
unknown committed
8306 8307 8308 8309 8310 8311 8312 8313 8314
	| 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
8315
	| MASTER_SERVER_ID_SYM  {}
unknown's avatar
unknown committed
8316
	| MASTER_CONNECT_RETRY_SYM	{}
unknown's avatar
unknown committed
8317 8318 8319 8320 8321 8322
	| 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
8323 8324 8325
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
8326
	| MAX_USER_CONNECTIONS_SYM {}
8327
	| MAX_VALUE_SYM         {}
8328
	| MEDIUM_SYM		{}
8329
	| MERGE_SYM		{}
unknown's avatar
unknown committed
8330
	| MICROSECOND_SYM	{}
8331
        | MIGRATE_SYM           {}
unknown's avatar
unknown committed
8332 8333 8334
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
unknown's avatar
unknown committed
8335
	| MODE_SYM		{}
unknown's avatar
unknown committed
8336
	| MONTH_SYM		{}
8337 8338 8339
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
8340
        | MUTEX_SYM             {}
8341
	| NAME_SYM              {}
unknown's avatar
unknown committed
8342
	| NAMES_SYM		{}
unknown's avatar
unknown committed
8343 8344
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
unknown's avatar
unknown committed
8345
	| NDBCLUSTER_SYM	{}
8346
	| NEXT_SYM		{}
unknown's avatar
unknown committed
8347
	| NEW_SYM		{}
8348
	| NODEGROUP_SYM		{}
8349
	| NONE_SYM		{}
unknown's avatar
unknown committed
8350
	| NVARCHAR_SYM		{}
unknown's avatar
unknown committed
8351
	| OFFSET_SYM		{}
unknown's avatar
unknown committed
8352
	| OLD_PASSWORD		{}
8353
	| ONE_SHOT_SYM		{}
8354
        | ONE_SYM               {}
unknown's avatar
unknown committed
8355
	| PACK_KEYS_SYM		{}
8356
	| PARTIAL		{}
8357
	| PARTITIONS_SYM	{}
unknown's avatar
unknown committed
8358
	| PASSWORD		{}
8359
        | PHASE_SYM             {}
8360
	| POINT_SYM		{}
8361
	| POLYGON		{}
8362
	| PREV_SYM		{}
8363
        | PRIVILEGES            {}
unknown's avatar
unknown committed
8364 8365
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
8366
	| QUARTER_SYM		{}
unknown's avatar
unknown committed
8367
	| QUERY_SYM		{}
8368
	| QUICK			{}
unknown's avatar
unknown committed
8369
	| RAID_0_SYM		{}
unknown's avatar
unknown committed
8370 8371
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
unknown's avatar
unknown committed
8372
	| RAID_STRIPED_SYM	{}
unknown's avatar
unknown committed
8373
	| RAID_TYPE		{}
8374
        | RECOVER_SYM           {}
unknown's avatar
unknown committed
8375
        | REDUNDANT_SYM         {}
unknown's avatar
unknown committed
8376 8377
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
unknown's avatar
unknown committed
8378
	| RELOAD		{}
unknown's avatar
unknown committed
8379
	| REORGANISE_SYM	{}
unknown's avatar
unknown committed
8380
	| REPEATABLE_SYM	{}
unknown's avatar
unknown committed
8381
	| REPLICATION		{}
8382
	| RESOURCES		{}
8383
        | RESUME_SYM            {}
8384
	| RETURNS_SYM           {}
8385
	| ROLLUP_SYM		{}
8386
	| ROUTINE_SYM		{}
unknown's avatar
unknown committed
8387 8388 8389
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
unknown's avatar
unknown committed
8390
	| RTREE_SYM		{}
unknown's avatar
unknown committed
8391
	| SECOND_SYM		{}
8392
	| SERIAL_SYM		{}
unknown's avatar
unknown committed
8393 8394
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
8395
	| SIMPLE_SYM		{}
unknown's avatar
unknown committed
8396
	| SHARE_SYM		{}
unknown's avatar
unknown committed
8397
	| SHUTDOWN		{}
8398
	| SNAPSHOT_SYM		{}
unknown's avatar
unknown committed
8399
	| SOUNDS_SYM		{}
unknown's avatar
unknown committed
8400
	| SQL_CACHE_SYM		{}
unknown's avatar
unknown committed
8401
	| SQL_BUFFER_RESULT	{}
unknown's avatar
unknown committed
8402
	| SQL_NO_CACHE_SYM	{}
unknown's avatar
unknown committed
8403
	| SQL_THREAD		{}
unknown's avatar
unknown committed
8404
	| STATUS_SYM		{}
unknown's avatar
unknown committed
8405
	| STORAGE_SYM		{}
unknown's avatar
unknown committed
8406
	| STRING_SYM		{}
unknown's avatar
unknown committed
8407
	| SUBDATE_SYM		{}
unknown's avatar
unknown committed
8408
	| SUBJECT_SYM		{}
8409 8410
	| SUBPARTITION_SYM	{}
	| SUBPARTITIONS_SYM	{}
unknown's avatar
unknown committed
8411
	| SUPER_SYM		{}
8412
        | SUSPEND_SYM           {}
8413
        | TABLES                {}
unknown's avatar
unknown committed
8414
	| TABLESPACE		{}
unknown's avatar
unknown committed
8415
	| TEMPORARY		{}
8416
	| TEMPTABLE_SYM		{}
unknown's avatar
unknown committed
8417
	| TEXT_SYM		{}
8418
	| THAN_SYM		{}
unknown's avatar
unknown committed
8419
	| TRANSACTION_SYM	{}
8420
	| TRIGGERS_SYM		{}
unknown's avatar
unknown committed
8421
	| TIMESTAMP		{}
8422 8423
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
unknown's avatar
unknown committed
8424
	| TIME_SYM		{}
unknown's avatar
unknown committed
8425
	| TYPES_SYM		{}
8426
        | TYPE_SYM              {}
unknown's avatar
unknown committed
8427
        | UDF_RETURNS_SYM       {}
8428
	| FUNCTION_SYM		{}
unknown's avatar
unknown committed
8429
	| UNCOMMITTED_SYM	{}
8430
	| UNDEFINED_SYM		{}
8431
	| UNKNOWN_SYM		{}
8432
	| UNTIL_SYM		{}
8433
	| USER			{}
unknown's avatar
unknown committed
8434
	| USE_FRM		{}
unknown's avatar
unknown committed
8435
	| VARIABLES		{}
8436
	| VIEW_SYM		{}
8437
	| VALUE_SYM		{}
8438
	| WARNINGS		{}
8439
	| WEEK_SYM		{}
unknown's avatar
unknown committed
8440
	| WORK_SYM		{}
8441
	| X509_SYM		{}
unknown's avatar
unknown committed
8442 8443
	| YEAR_SYM		{}
	;
unknown's avatar
unknown committed
8444 8445 8446 8447 8448 8449

/* Option functions */

set:
	SET opt_option
	{
unknown's avatar
unknown committed
8450 8451
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
unknown's avatar
unknown committed
8452
	  mysql_init_select(lex);
8453
	  lex->option_type=OPT_SESSION;
unknown's avatar
unknown committed
8454
	  lex->var_list.empty();
8455
          lex->one_shot_set= 0;
unknown's avatar
unknown committed
8456
	}
8457 8458 8459
	option_value_list
	{}
	;
unknown's avatar
unknown committed
8460 8461 8462

opt_option:
	/* empty */ {}
unknown's avatar
unknown committed
8463
	| OPTION {};
unknown's avatar
unknown committed
8464 8465

option_value_list:
8466 8467 8468 8469 8470 8471 8472 8473 8474 8475
	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
8476 8477
              and sp_instr_set_trigger instructions share one LEX.
              (Well, it is theoretically possible but adds some extra
8478 8479 8480 8481 8482 8483 8484 8485
               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
8486

8487 8488 8489 8490 8491 8492 8493 8494 8495
            /* 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;
          }
        }
8496
	ext_option_value
8497 8498
        {
          LEX *lex= Lex;
unknown's avatar
unknown committed
8499

8500 8501 8502
          if (lex->sphead)
          {
            sp_head *sp= lex->sphead;
unknown's avatar
unknown committed
8503

8504 8505 8506 8507 8508 8509 8510 8511 8512
	    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
8513

8514 8515 8516
              if (!(i= new sp_instr_stmt(sp->instructions(), lex->spcont,
                                         lex)))
                YYABORT;
unknown's avatar
unknown committed
8517

8518 8519 8520 8521
              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
8522

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

8526 8527 8528 8529 8530 8531 8532 8533 8534
              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
8535 8536

option_type:
8537 8538 8539 8540 8541 8542 8543 8544 8545
        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
8546 8547 8548 8549
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
unknown's avatar
unknown committed
8550
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
8551 8552 8553 8554 8555 8556
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

opt_var_ident_type:
	/* empty */		{ $$=OPT_DEFAULT; }
unknown's avatar
unknown committed
8557
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
8558 8559 8560
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
unknown's avatar
unknown committed
8561

8562 8563 8564
ext_option_value:
        sys_option_value
        | option_type2 option_value;
8565

8566 8567 8568 8569
sys_option_value:
        option_type internal_variable_name equal set_expr_or_default
        {
          LEX *lex=Lex;
8570

8571
          if ($2.var == trg_new_row_fake_var)
8572 8573 8574
          {
            /* We are in trigger and assigning value to field of new row */
            Item *it;
8575
            Item_trigger_field *trg_fld;
8576 8577
            sp_instr_set_trigger_field *sp_fld;
	    LINT_INIT(sp_fld);
8578 8579 8580 8581
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
8582
            }
8583 8584
            if ($4)
              it= $4;
8585
            else
8586 8587 8588 8589
            {
              /* QQ: Shouldn't this be field's default value ? */
              it= new Item_null();
            }
8590

unknown's avatar
unknown committed
8591
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
8592
                                                  Item_trigger_field::NEW_ROW,
8593
                                                  $2.base_name.str)) ||
8594 8595 8596 8597 8598
                !(sp_fld= new sp_instr_set_trigger_field(lex->sphead->
                          	                         instructions(),
                                	                 lex->spcont,
							 trg_fld,
                                        	         it, lex)))
8599
              YYABORT;
8600

8601 8602 8603 8604
            /*
              Let us add this item to list of all Item_trigger_field
              objects in trigger.
            */
8605 8606
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
                                    (byte **)&trg_fld->next_trg_field);
8607

8608
            lex->sphead->add_instr(sp_fld);
8609 8610 8611 8612
          }
          else if ($2.var)
          { /* System variable */
            if ($1)
8613
              lex->option_type= $1;
8614 8615 8616 8617 8618 8619 8620 8621
            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;
8622
            sp_instr_set *sp_set;
8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637
            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();
8638 8639 8640
            sp_set= new sp_instr_set(lex->sphead->instructions(), ctx,
                                     spv->offset, it, spv->type, lex, TRUE);
            lex->sphead->add_instr(sp_set);
8641 8642 8643 8644 8645
          }
        }
        | option_type TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	{
	  LEX *lex=Lex;
8646
          if ($1)
8647
            lex->option_type= $1;
8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659
	  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
8660
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
8661 8662
	  {
	    LEX *lex=Lex;
8663
	    lex->var_list.push_back(new set_var($3, $4.var, &$4.base_name, $6));
unknown's avatar
unknown committed
8664
	  }
8665
	| charset old_or_new_charset_name_or_default
unknown's avatar
unknown committed
8666
	{
unknown's avatar
unknown committed
8667
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
8668
	  LEX *lex= Lex;
8669
	  $2= $2 ? $2: global_system_variables.character_set_client;
8670
	  lex->var_list.push_back(new set_var_collation_client($2,thd->variables.collation_database,$2));
unknown's avatar
unknown committed
8671
	}
8672 8673 8674 8675 8676 8677 8678 8679 8680 8681
        | 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);
8682 8683 8684
            else
              yyerror(ER(ER_SYNTAX_ERROR));

8685 8686
	    YYABORT;
	  }
8687
	| NAMES_SYM charset_name_or_default opt_collate
8688
	{
unknown's avatar
unknown committed
8689
	  LEX *lex= Lex;
8690
	  $2= $2 ? $2 : global_system_variables.character_set_client;
8691 8692
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
8693
	  {
8694 8695
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
unknown's avatar
unknown committed
8696
	    YYABORT;
8697
	  }
8698
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
8699
	}
unknown's avatar
unknown committed
8700
	| PASSWORD equal text_or_password
unknown's avatar
unknown committed
8701
	  {
8702
	    THD *thd=YYTHD;
unknown's avatar
unknown committed
8703
	    LEX_USER *user;
8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714
	    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
8715
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
unknown's avatar
unknown committed
8716
	      YYABORT;
8717
	    user->host=null_lex_str;
8718
	    user->user.str=thd->security_ctx->priv_user;
8719
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
unknown's avatar
unknown committed
8720 8721
	  }
	| PASSWORD FOR_SYM user equal text_or_password
unknown's avatar
unknown committed
8722
	  {
unknown's avatar
unknown committed
8723 8724
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
8725
	;
unknown's avatar
unknown committed
8726

unknown's avatar
unknown committed
8727 8728 8729
internal_variable_name:
	ident
	{
8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741
	  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;
8742
	    $$.base_name= null_lex_str;
8743 8744 8745 8746
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
8747 8748 8749
            if (tmp == &sys_time_zone &&
                lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
8750 8751 8752 8753 8754 8755 8756 8757
            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;
            }
8758 8759 8760 8761 8762 8763 8764
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
unknown's avatar
unknown committed
8765
	}
8766 8767
	| ident '.' ident
	  {
8768
            LEX *lex= Lex;
8769 8770
            if (check_reserved_words(&$1))
            {
8771
	      yyerror(ER(ER_SYNTAX_ERROR));
8772 8773
              YYABORT;
            }
8774 8775 8776 8777 8778 8779
            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
8780
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
8781 8782 8783 8784
                YYABORT;
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
unknown's avatar
unknown committed
8785 8786
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
8787 8788 8789 8790
                YYABORT;
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
unknown's avatar
unknown committed
8791
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
8792 8793 8794
                YYABORT;
              }
              /* This special combination will denote field of NEW row */
8795
              $$.var= trg_new_row_fake_var;
8796 8797 8798 8799 8800 8801 8802 8803
              $$.base_name= $3;
            }
            else
            {
              sys_var *tmp=find_sys_var($3.str, $3.length);
              if (!tmp)
                YYABORT;
              if (!tmp->is_struct())
8804
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
8805 8806 8807
              $$.var= tmp;
              $$.base_name= $1;
            }
8808 8809 8810 8811 8812 8813 8814
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
8815
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
unknown's avatar
unknown committed
8816 8817 8818
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
8819
	  }
8820
        ;
unknown's avatar
unknown committed
8821 8822 8823 8824 8825 8826 8827

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

unknown's avatar
unknown committed
8829 8830 8831 8832
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
8833
	    $$= $3.length ? YYTHD->variables.old_passwords ?
unknown's avatar
unknown committed
8834 8835 8836 8837 8838 8839 8840 8841
	        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;
8842 8843
	  }
          ;
unknown's avatar
unknown committed
8844

unknown's avatar
unknown committed
8845

unknown's avatar
unknown committed
8846
set_expr_or_default:
unknown's avatar
unknown committed
8847 8848
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
8849 8850
	| ON	  { $$=new Item_string("ON",  2, system_charset_info); }
	| ALL	  { $$=new Item_string("ALL", 3, system_charset_info); }
unknown's avatar
unknown committed
8851
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
unknown's avatar
unknown committed
8852
	;
unknown's avatar
unknown committed
8853 8854


unknown's avatar
unknown committed
8855 8856 8857 8858 8859
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
8860 8861 8862 8863
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
8864
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOCK");
8865 8866 8867
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_LOCK_TABLES;
unknown's avatar
unknown committed
8868
	}
8869 8870
	table_lock_list
	{}
8871
	;
unknown's avatar
unknown committed
8872 8873 8874

table_or_tables:
	TABLE_SYM
unknown's avatar
unknown committed
8875
	| TABLES;
unknown's avatar
unknown committed
8876 8877 8878

table_lock_list:
	table_lock
unknown's avatar
unknown committed
8879
	| table_lock_list ',' table_lock;
unknown's avatar
unknown committed
8880 8881 8882

table_lock:
	table_ident opt_table_alias lock_option
8883
	{
unknown's avatar
unknown committed
8884
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
8885 8886
	   YYABORT;
	}
8887
        ;
unknown's avatar
unknown committed
8888 8889 8890

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
8891
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
unknown's avatar
unknown committed
8892
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
8893 8894
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
unknown's avatar
unknown committed
8895 8896

unlock:
8897 8898 8899 8900 8901 8902
	UNLOCK_SYM
	{
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
8903
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "UNLOCK");
8904 8905 8906 8907 8908 8909
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_UNLOCK_TABLES;
	}
	table_or_tables
	{}
8910
        ;
unknown's avatar
unknown committed
8911 8912


8913
/*
unknown's avatar
unknown committed
8914
** Handler: direct access to ISAM functions
8915 8916 8917 8918 8919
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
8920
	  LEX *lex= Lex;
8921 8922 8923 8924 8925
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
8926
	  lex->sql_command = SQLCOM_HA_OPEN;
unknown's avatar
unknown committed
8927
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
8928 8929
	    YYABORT;
	}
unknown's avatar
unknown committed
8930
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
8931
	{
8932
	  LEX *lex= Lex;
8933 8934 8935 8936 8937
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
8938
	  lex->sql_command = SQLCOM_HA_CLOSE;
unknown's avatar
unknown committed
8939
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
8940 8941
	    YYABORT;
	}
unknown's avatar
unknown committed
8942
	| HANDLER_SYM table_ident_nodb READ_SYM
8943
	{
unknown's avatar
unknown committed
8944
	  LEX *lex=Lex;
8945 8946 8947 8948 8949
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
unknown's avatar
unknown committed
8950 8951
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
8952
	  lex->current_select->select_limit= new Item_int((int32) 1);
8953
	  lex->current_select->offset_limit= 0;
unknown's avatar
unknown committed
8954
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
8955
	    YYABORT;
unknown's avatar
unknown committed
8956
        }
8957
        handler_read_or_scan where_clause opt_limit_clause {}
8958
        ;
8959

unknown's avatar
unknown committed
8960
handler_read_or_scan:
8961 8962
	handler_scan_function         { Lex->ident= null_lex_str; }
        | ident handler_rkey_function { Lex->ident= $1; }
8963
        ;
unknown's avatar
unknown committed
8964 8965 8966

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
8967 8968
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
unknown's avatar
unknown committed
8969 8970 8971 8972 8973 8974

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;  }
8975 8976
	| handler_rkey_mode
	{
unknown's avatar
unknown committed
8977 8978
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
unknown's avatar
unknown committed
8979
	  lex->ha_rkey_mode=$1;
unknown's avatar
unknown committed
8980
	  if (!(lex->insert_list = new List_item))
8981
	    YYABORT;
8982 8983
	} '(' values ')' { }
        ;
8984 8985

handler_rkey_mode:
unknown's avatar
unknown committed
8986 8987 8988 8989
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
8990 8991
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
8992

unknown's avatar
unknown committed
8993 8994 8995
/* GRANT / REVOKE */

revoke:
8996
	REVOKE clear_privileges revoke_command
8997 8998 8999 9000
	{}
        ;

revoke_command:
9001
	grant_privileges ON opt_table grant_ident FROM grant_list
9002
	{
9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030
          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;
9031
        }
9032
	|
9033
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
9034 9035 9036
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
9037
	;
unknown's avatar
unknown committed
9038 9039

grant:
9040 9041 9042 9043 9044 9045
	GRANT clear_privileges grant_command
	{}
        ;

grant_command:
	grant_privileges ON opt_table grant_ident TO_SYM grant_list
9046
	require_clause grant_options
9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078
	{
          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
9079

9080 9081 9082 9083
opt_table:
	/* Empty */
	| TABLE_SYM ;
        
unknown's avatar
unknown committed
9084
grant_privileges:
9085 9086 9087 9088 9089 9090
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
9091
        ;
unknown's avatar
unknown committed
9092

unknown's avatar
unknown committed
9093 9094 9095 9096 9097
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

9098 9099 9100
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
unknown's avatar
unknown committed
9101

9102
object_privilege:
unknown's avatar
unknown committed
9103
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
9104 9105 9106
	| 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
9107 9108
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
unknown's avatar
unknown committed
9109
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
unknown's avatar
unknown committed
9110 9111 9112
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
unknown's avatar
unknown committed
9113
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
unknown's avatar
unknown committed
9114 9115 9116 9117
	| 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
9118 9119 9120 9121 9122
	| 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; }
9123 9124 9125 9126
	| 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; }
9127 9128
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
9129
	| CREATE USER { Lex->grant |= CREATE_USER_ACL; }
unknown's avatar
unknown committed
9130
	;
unknown's avatar
unknown committed
9131

unknown's avatar
unknown committed
9132

9133 9134
opt_and:
	/* empty */	{}
unknown's avatar
unknown committed
9135
	| AND_SYM	{}
9136 9137 9138 9139 9140 9141 9142 9143 9144
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
unknown's avatar
unknown committed
9145 9146 9147 9148
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
unknown's avatar
unknown committed
9149
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
unknown's avatar
unknown committed
9150 9151 9152 9153 9154 9155 9156 9157 9158
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
unknown's avatar
unknown committed
9159
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
unknown's avatar
unknown committed
9160 9161 9162 9163 9164 9165 9166 9167 9168
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
unknown's avatar
unknown committed
9169
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
unknown's avatar
unknown committed
9170 9171 9172 9173 9174
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
9175

9176
grant_ident:
unknown's avatar
unknown committed
9177 9178
	'*'
	  {
9179
	    LEX *lex= Lex;
unknown's avatar
unknown committed
9180
	    lex->current_select->db= lex->thd->db;
unknown's avatar
unknown committed
9181
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
9182 9183
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
9184
	    {
unknown's avatar
unknown committed
9185 9186
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
9187
	      YYABORT;
unknown's avatar
unknown committed
9188
	    }
unknown's avatar
unknown committed
9189 9190 9191
	  }
	| ident '.' '*'
	  {
9192
	    LEX *lex= Lex;
unknown's avatar
unknown committed
9193
	    lex->current_select->db = $1.str;
unknown's avatar
unknown committed
9194
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
9195 9196
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
9197
	    {
unknown's avatar
unknown committed
9198 9199
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
9200 9201 9202 9203 9204
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
9205
	    LEX *lex= Lex;
unknown's avatar
unknown committed
9206
	    lex->current_select->db = NULL;
unknown's avatar
unknown committed
9207 9208
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
unknown's avatar
unknown committed
9209
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
9210
	    {
unknown's avatar
unknown committed
9211 9212
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
9213 9214 9215 9216 9217
	      YYABORT;
	    }
	  }
	| table_ident
	  {
unknown's avatar
unknown committed
9218
	    LEX *lex=Lex;
unknown's avatar
unknown committed
9219
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
unknown's avatar
unknown committed
9220
	      YYABORT;
unknown's avatar
unknown committed
9221
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
9222
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
9223 9224
	  }
          ;
unknown's avatar
unknown committed
9225 9226 9227


user_list:
9228 9229 9230 9231 9232 9233 9234 9235 9236 9237
	user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;


grant_list:
9238
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
9239
	| grant_list ',' grant_user
9240 9241 9242 9243 9244
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
unknown's avatar
unknown committed
9245 9246 9247 9248 9249 9250 9251 9252


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
9253
             if (YYTHD->variables.old_passwords)
9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270
             {
               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
9271 9272 9273
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
9274
	  { $$= $1; $1->password= $5; }
unknown's avatar
unknown committed
9275
	| user
9276
	  { $$= $1; $1->password= null_lex_str; }
9277
        ;
unknown's avatar
unknown committed
9278 9279 9280


opt_column_list:
unknown's avatar
unknown committed
9281 9282 9283 9284 9285
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
unknown's avatar
unknown committed
9286
	| '(' column_list ')';
unknown's avatar
unknown committed
9287 9288 9289

column_list:
	column_list ',' column_list_id
unknown's avatar
unknown committed
9290
	| column_list_id;
unknown's avatar
unknown committed
9291 9292 9293 9294

column_list_id:
	ident
	{
unknown's avatar
unknown committed
9295
	  String *new_str = new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info);
unknown's avatar
unknown committed
9296 9297
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
unknown's avatar
unknown committed
9298
	  LEX *lex=Lex;
unknown's avatar
unknown committed
9299 9300
	  while ((point=iter++))
	  {
9301 9302
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
unknown's avatar
unknown committed
9303 9304
		break;
	  }
unknown's avatar
unknown committed
9305
	  lex->grant_tot_col|= lex->which_columns;
unknown's avatar
unknown committed
9306
	  if (point)
unknown's avatar
unknown committed
9307
	    point->rights |= lex->which_columns;
unknown's avatar
unknown committed
9308
	  else
unknown's avatar
unknown committed
9309
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
9310 9311
	}
        ;
unknown's avatar
unknown committed
9312

unknown's avatar
unknown committed
9313 9314

require_clause: /* empty */
9315
        | REQUIRE_SYM require_list
9316 9317 9318
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
9319
        | REQUIRE_SYM SSL_SYM
9320 9321 9322
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
9323
        | REQUIRE_SYM X509_SYM
9324 9325 9326 9327 9328 9329 9330
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
9331
          ;
unknown's avatar
unknown committed
9332

unknown's avatar
unknown committed
9333
grant_options:
unknown's avatar
unknown committed
9334
	/* empty */ {}
unknown's avatar
unknown committed
9335
	| WITH grant_option_list;
unknown's avatar
unknown committed
9336

unknown's avatar
unknown committed
9337 9338
grant_option_list:
	grant_option_list grant_option {}
9339 9340
	| grant_option {}
        ;
unknown's avatar
unknown committed
9341 9342 9343

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
unknown's avatar
unknown committed
9344
        | MAX_QUERIES_PER_HOUR ulong_num
unknown's avatar
unknown committed
9345
        {
unknown's avatar
unknown committed
9346 9347 9348
	  LEX *lex=Lex;
	  lex->mqh.questions=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
9349
	}
unknown's avatar
unknown committed
9350
        | MAX_UPDATES_PER_HOUR ulong_num
9351
        {
unknown's avatar
unknown committed
9352 9353 9354
	  LEX *lex=Lex;
	  lex->mqh.updates=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
9355
	}
unknown's avatar
unknown committed
9356
        | MAX_CONNECTIONS_PER_HOUR ulong_num
9357
        {
unknown's avatar
unknown committed
9358 9359 9360
	  LEX *lex=Lex;
	  lex->mqh.conn_per_hour= $2;
	  lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
9361
	}
unknown's avatar
unknown committed
9362
        | MAX_USER_CONNECTIONS_SYM ulong_num
9363
        {
unknown's avatar
unknown committed
9364 9365 9366
	  LEX *lex=Lex;
          lex->mqh.user_conn= $2;
          lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
9367 9368
	}
        ;
9369

unknown's avatar
unknown committed
9370
begin:
unknown's avatar
unknown committed
9371 9372 9373 9374 9375 9376 9377
	BEGIN_SYM  
        {
	  LEX *lex=Lex;
          lex->sql_command = SQLCOM_BEGIN;
          lex->start_transaction_opt= 0;
        }
        opt_work {}
9378
	;
unknown's avatar
unknown committed
9379 9380 9381

opt_work:
	/* empty */ {}
unknown's avatar
unknown committed
9382
	| WORK_SYM  {}
9383
        ;
unknown's avatar
unknown committed
9384

unknown's avatar
unknown committed
9385
opt_chain:
unknown's avatar
unknown committed
9386
	/* empty */ { $$= (YYTHD->variables.completion_type == 1); }
unknown's avatar
unknown committed
9387 9388 9389 9390 9391
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
unknown's avatar
unknown committed
9392
	/* empty */ { $$= (YYTHD->variables.completion_type == 2); }
unknown's avatar
unknown committed
9393 9394 9395 9396 9397 9398 9399 9400 9401
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

unknown's avatar
unknown committed
9402
commit:
unknown's avatar
unknown committed
9403
	COMMIT_SYM opt_work opt_chain opt_release
unknown's avatar
unknown committed
9404
	{
unknown's avatar
unknown committed
9405 9406 9407 9408
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_COMMIT;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
unknown's avatar
unknown committed
9409 9410
	}
	;
unknown's avatar
unknown committed
9411 9412

rollback:
unknown's avatar
unknown committed
9413
	ROLLBACK_SYM opt_work opt_chain opt_release
9414
	{
unknown's avatar
unknown committed
9415 9416 9417 9418
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
9419
	}
unknown's avatar
unknown committed
9420 9421
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
9422
	{
unknown's avatar
unknown committed
9423 9424 9425
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK_TO_SAVEPOINT;
	  lex->ident= $5;
unknown's avatar
unknown committed
9426 9427 9428
	}
	;

9429 9430 9431
savepoint:
	SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
9432 9433 9434
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SAVEPOINT;
	  lex->ident= $2;
unknown's avatar
unknown committed
9435 9436 9437 9438 9439 9440
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
9441 9442 9443
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RELEASE_SAVEPOINT;
	  lex->ident= $3;
unknown's avatar
unknown committed
9444 9445
	}
	;
unknown's avatar
unknown committed
9446
  
9447
/*
unknown's avatar
unknown committed
9448
   UNIONS : glue selects together
9449 9450 9451
*/


9452
union_clause:
9453
	/* empty */ {}
unknown's avatar
unknown committed
9454 9455
	| union_list
	;
9456 9457

union_list:
9458
	UNION_SYM union_option
9459 9460 9461 9462 9463
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
unknown's avatar
unknown committed
9464
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
9465
	    YYABORT;
9466
	  }
9467
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
9468
	  {
9469
            yyerror(ER(ER_SYNTAX_ERROR));
9470 9471
	    YYABORT;
	  }
9472
	  if (mysql_new_select(lex, 0))
9473
	    YYABORT;
unknown's avatar
unknown committed
9474
          mysql_init_select(lex);
9475
	  lex->current_select->linkage=UNION_TYPE;
9476 9477 9478
          if ($2) /* UNION DISTINCT - remember position */
            lex->current_select->master_unit()->union_distinct=
                                                      lex->current_select;
unknown's avatar
unknown committed
9479
	}
unknown's avatar
unknown committed
9480 9481
	select_init
        {
9482 9483 9484 9485
          /*
	    Remove from the name resolution context stack the context of the
            last select in the union.
	  */
unknown's avatar
unknown committed
9486 9487
          Lex->pop_context();
        }
9488
	;
9489 9490

union_opt:
unknown's avatar
unknown committed
9491 9492 9493
	/* Empty */ { $$= 0; }
	| union_list { $$= 1; }
	| union_order_or_limit { $$= 1; }
unknown's avatar
unknown committed
9494
	;
9495

unknown's avatar
unknown committed
9496
union_order_or_limit:
9497
	  {
unknown's avatar
unknown committed
9498
	    THD *thd= YYTHD;
9499
	    LEX *lex= thd->lex;
unknown's avatar
unknown committed
9500
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
unknown's avatar
unknown committed
9501
	    SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
9502
	    SELECT_LEX_UNIT *unit= sel->master_unit();
unknown's avatar
unknown committed
9503
	    SELECT_LEX *fake= unit->fake_select_lex;
unknown's avatar
unknown committed
9504 9505 9506 9507 9508 9509
	    if (fake)
	    {
	      unit->global_parameters= fake;
	      fake->no_table_names_allowed= 1;
	      lex->current_select= fake;
	    }
unknown's avatar
unknown committed
9510
	    thd->where= "global ORDER clause";
9511
	  }
9512
	order_or_limit
unknown's avatar
unknown committed
9513 9514
          {
	    THD *thd= YYTHD;
9515
	    thd->lex->current_select->no_table_names_allowed= 0;
unknown's avatar
unknown committed
9516 9517
	    thd->where= "";
          }
9518 9519 9520
	;

order_or_limit:
9521 9522
	order_clause opt_limit_clause_init
	| limit_clause
9523
	;
unknown's avatar
unknown committed
9524 9525

union_option:
9526 9527 9528 9529
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
9530

unknown's avatar
unknown committed
9531 9532
singlerow_subselect:
	subselect_start singlerow_subselect_init
unknown's avatar
unknown committed
9533 9534 9535 9536
	subselect_end
	{
	  $$= $2;
	};
9537

unknown's avatar
unknown committed
9538
singlerow_subselect_init:
9539
	select_init2
unknown's avatar
unknown committed
9540
	{
9541 9542
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
unknown's avatar
unknown committed
9543
	};
unknown's avatar
unknown committed
9544 9545

exists_subselect:
unknown's avatar
unknown committed
9546 9547 9548 9549 9550
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
unknown's avatar
unknown committed
9551 9552

exists_subselect_init:
9553
	select_init2
unknown's avatar
unknown committed
9554
	{
9555 9556
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
unknown's avatar
unknown committed
9557
	};
9558

unknown's avatar
unknown committed
9559 9560 9561 9562 9563 9564 9565 9566
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
9567
  select_init2
unknown's avatar
unknown committed
9568
  {
unknown's avatar
unknown committed
9569
    $$= Lex->current_select->master_unit()->first_select();
unknown's avatar
unknown committed
9570 9571
  };

9572
subselect_start:
9573
	'(' SELECT_SYM
unknown's avatar
unknown committed
9574
	{
9575
	  LEX *lex=Lex;
9576
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
9577 9578
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
9579 9580
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
9581 9582
	    YYABORT;
	  }
unknown's avatar
unknown committed
9583 9584 9585
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
9586 9587

subselect_end:
unknown's avatar
unknown committed
9588 9589 9590
	')'
	{
	  LEX *lex=Lex;
unknown's avatar
unknown committed
9591
          lex->pop_context();
9592
	  lex->current_select = lex->current_select->return_after_parsing();
unknown's avatar
unknown committed
9593
          lex->nest_level--;
unknown's avatar
unknown committed
9594
	};
unknown's avatar
unknown committed
9595

9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707
definer:
	get_definer
	{
	  THD *thd= YYTHD;
	  
	  if (! (thd->lex->definer= create_definer(thd, &$1->user, &$1->host)))
	    YYABORT;
	}
	;

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

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

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

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

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

 CREATE VIEW statement options.

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

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

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

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

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

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

view_or_trigger_tail:
	view_tail
	{}
	| trigger_tail
	{}
	;

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

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

view_list_opt:
	/* empty */
	{}
9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723
	| '(' 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)));
	  }
	;

9724 9725 9726
view_select:
	SELECT_SYM remember_name select_init2
	{
9727 9728 9729 9730 9731 9732
          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;
9733 9734 9735
	}
	| '(' remember_name select_paren ')' union_opt
	{
9736 9737 9738 9739 9740 9741
          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;
9742
	}
9743 9744
	;

9745
view_check_option:
9746
	/* empty */
9747 9748 9749 9750 9751 9752 9753
	{ 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; }
9754
	;
9755

9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769
/**************************************************************************

 CREATE TRIGGER statement parts.

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

trigger_tail:
	TRIGGER_SYM remember_name sp_name trg_action_time trg_event 
	ON table_ident FOR_SYM EACH_SYM ROW_SYM
	{
	  LEX *lex= Lex;
	  sp_head *sp;
	 
	  if (lex->sphead)
9770
	  {
9771 9772
	    my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "TRIGGER");
	    YYABORT;
9773
	  }
9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826
	
	  if (!(sp= new sp_head()))
	    YYABORT;
	  sp->reset_thd_mem_root(YYTHD);
	  sp->init(lex);
	
	  lex->trigger_definition_begin= $2;
	  
	  sp->m_type= TYPE_ENUM_TRIGGER;
	  lex->sphead= sp;
	  lex->spname= $3;
	  /*
	    We have to turn of CLIENT_MULTI_QUERIES while parsing a
	    stored procedure, otherwise yylex will chop it into pieces
	    at each ';'.
	  */
	  sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
	  YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
	  
	  bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	  lex->sphead->m_chistics= &lex->sp_chistics;
	  lex->sphead->m_body_begin= lex->ptr;
	}
	sp_proc_stmt
	{
	  LEX *lex= Lex;
	  sp_head *sp= lex->sphead;
	  
	  lex->sql_command= SQLCOM_CREATE_TRIGGER;
	  sp->init_strings(YYTHD, lex, $3);
	  /* Restore flag if it was cleared above */
	  if (sp->m_old_cmq)
	    YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
	  sp->restore_thd_mem_root(YYTHD);
	
	  if (sp->is_not_allowed_in_function("trigger"))
	      YYABORT;
	
	  /*
	    We have to do it after parsing trigger body, because some of
	    sp_proc_stmt alternatives are not saving/restoring LEX, so
	    lex->query_tables can be wiped out.
	    
	    QQ: What are other consequences of this?
	    
	    QQ: Could we loosen lock type in certain cases ?
	  */
	  if (!lex->select_lex.add_table_to_list(YYTHD, $7, 
	                                         (LEX_STRING*) 0,
	                                         TL_OPTION_UPDATING,
	                                         TL_WRITE))
	    YYABORT;
	}
9827 9828
	;

9829
/*************************************************************************/
9830

9831 9832
xa: XA_SYM begin_or_start xid opt_join_or_resume
      {
unknown's avatar
unknown committed
9833
        Lex->sql_command = SQLCOM_XA_START;
9834
      }
9835
    | XA_SYM END xid opt_suspend
9836
      {
unknown's avatar
unknown committed
9837
        Lex->sql_command = SQLCOM_XA_END;
9838 9839 9840
      }
    | XA_SYM PREPARE_SYM xid
      {
unknown's avatar
unknown committed
9841
        Lex->sql_command = SQLCOM_XA_PREPARE;
9842 9843 9844
      }
    | XA_SYM COMMIT_SYM xid opt_one_phase
      {
unknown's avatar
unknown committed
9845
        Lex->sql_command = SQLCOM_XA_COMMIT;
9846 9847 9848
      }
    | XA_SYM ROLLBACK_SYM xid
      {
unknown's avatar
unknown committed
9849
        Lex->sql_command = SQLCOM_XA_ROLLBACK;
9850 9851 9852
      }
    | XA_SYM RECOVER_SYM
      {
unknown's avatar
unknown committed
9853
        Lex->sql_command = SQLCOM_XA_RECOVER;
9854 9855 9856
      }
    ;

unknown's avatar
unknown committed
9857 9858
xid: text_string
     {
unknown's avatar
unknown committed
9859
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE);
unknown's avatar
unknown committed
9860 9861 9862 9863 9864 9865
       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
9866
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
unknown's avatar
unknown committed
9867 9868 9869 9870 9871 9872
       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
9873
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
unknown's avatar
unknown committed
9874 9875 9876 9877 9878
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set($5, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     ;
9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894

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

9895
opt_suspend:
9896 9897
    /* nothing */           { Lex->xa_opt=XA_NONE;        }
    | SUSPEND_SYM           { Lex->xa_opt=XA_SUSPEND;     }
9898 9899 9900 9901 9902
      opt_migrate
    ;

opt_migrate:
    /* nothing */           { }
9903 9904 9905
    | FOR_SYM MIGRATE_SYM   { Lex->xa_opt=XA_FOR_MIGRATE; }
    ;

9906
install:
unknown's avatar
unknown committed
9907
  INSTALL_SYM PLUGIN_SYM ident SONAME_SYM TEXT_STRING_sys
9908 9909 9910 9911 9912 9913
  {
    LEX *lex= Lex;
    lex->sql_command= SQLCOM_INSTALL_PLUGIN;
    lex->comment= $3;
    lex->ident= $5;
  };
9914

9915
uninstall:
unknown's avatar
unknown committed
9916
  UNINSTALL_SYM PLUGIN_SYM ident
9917 9918 9919 9920 9921
  {
    LEX *lex= Lex;
    lex->sql_command= SQLCOM_UNINSTALL_PLUGIN;
    lex->comment= $3;
  };