sql_yacc.yy 214 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};

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 51 52
#define WARN_DEPRECATED(A,B) \
  push_warning_printf(((THD *)yythd), MYSQL_ERROR::WARN_LEVEL_WARN, \
		      ER_WARN_DEPRECATED_SYNTAX, \
53
		      ER(ER_WARN_DEPRECATED_SYNTAX), (A), (B));
unknown's avatar
unknown committed
54

unknown's avatar
unknown committed
55 56 57 58 59 60 61
#define TEST_ASSERT(A) \
  if (!(A)) \
  {					\
    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 77 78 79 80 81
  LEX_STRING lex_str;
  LEX_STRING *lex_str_ptr;
  LEX_SYMBOL symbol;
  Table_ident *table;
  char *simple_string;
  Item *item;
82
  Item_num *item_num;
unknown's avatar
unknown committed
83 84
  List<Item> *item_list;
  List<String> *string_list;
unknown's avatar
unknown committed
85 86 87 88 89
  String *string;
  key_part_spec *key_part;
  TABLE_LIST *table_list;
  udf_func *udf;
  LEX_USER *lex_user;
unknown's avatar
unknown committed
90
  struct sys_var_with_base variable;
unknown's avatar
unknown committed
91
  Key::Keytype key_type;
unknown's avatar
unknown committed
92
  enum ha_key_alg key_alg;
unknown's avatar
unknown committed
93 94
  enum db_type db_type;
  enum row_type row_type;
unknown's avatar
unknown committed
95
  enum ha_rkey_function ha_rkey_mode;
unknown's avatar
unknown committed
96
  enum enum_tx_isolation tx_isolation;
97
  enum Cast_target cast_type;
unknown's avatar
unknown committed
98
  enum Item_udftype udf_type;
99
  CHARSET_INFO *charset;
unknown's avatar
unknown committed
100
  thr_lock_type lock_type;
101
  interval_type interval, interval_time_st;
102
  timestamp_type date_time_type;
unknown's avatar
unknown committed
103
  st_select_lex *select_lex;
unknown's avatar
unknown committed
104
  chooser_compare_func_creator boolfunc2creator;
105 106 107 108
  struct sp_cond_type *spcondtype;
  struct { int vars, conds, hndlrs, curs; } spblock;
  sp_name *spname;
  struct st_lex *lex;
unknown's avatar
unknown committed
109 110 111
}

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

%pure_parser					/* We have threads */

unknown's avatar
unknown committed
117
%token  END_OF_INPUT
unknown's avatar
unknown committed
118

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
%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
%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
149
%token  BIN_NUM
150 151 152 153 154 155 156 157 158 159 160 161 162 163
%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
164
%token  CASCADED
165
%token  CAST_SYM
unknown's avatar
unknown committed
166
%token  CHAIN_SYM
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
%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
%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
184
%token  COMPACT_SYM
185 186 187 188
%token  COMPRESSED_SYM
%token  CONCAT
%token  CONCAT_WS
%token  CONCURRENT
189
%token  CONDITION_SYM
190 191 192
%token  CONNECTION_SYM
%token  CONSISTENT_SYM
%token  CONSTRAINT
193 194
%token  CONTAINS_SYM
%token  CONTINUE_SYM
195 196 197 198 199 200 201
%token  CONVERT_SYM
%token  CONVERT_TZ_SYM
%token  COUNT_SYM
%token  CREATE
%token  CROSS
%token  CUBE_SYM
%token  CURDATE
202
%token  CURRENT_USER
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
%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
218
%token  DECIMAL_NUM
219
%token  DECIMAL_SYM
220
%token  DECLARE_SYM
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
%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
243
%token  DUPLICATE_SYM
244
%token  DYNAMIC_SYM
245
%token  EACH_SYM
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
%token  EALLOCATE_SYM
%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
265
%token  EXIT_SYM
266 267 268 269 270 271
%token  EXPANSION_SYM
%token  EXPORT_SET
%token  EXTENDED_SYM
%token  EXTRACT_SYM
%token  FALSE_SYM
%token  FAST_SYM
272
%token  FETCH_SYM
273 274 275 276 277 278 279 280 281 282 283
%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
284
%token  FOUND_SYM
285 286 287 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
%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
333
%token  INOUT_SYM
334 335 336 337 338 339
%token  INSENSITIVE_SYM
%token  INSERT
%token  INSERT_METHOD
%token  INTERVAL_SYM
%token  INTO
%token  INT_SYM
340
%token  INVOKER_SYM
341 342 343 344 345 346 347 348 349 350
%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
351
%token  LANGUAGE_SYM
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
%token  LAST_INSERT_ID
%token  LAST_SYM
%token  LE
%token  LEADING
%token  LEAST_SYM
%token  LEAVES
%token  LEAVE_SYM
%token  LEFT
%token  LEVEL_SYM
%token  LEX_HOSTNAME
%token  LIKE
%token  LIMIT
%token  LINEFROMTEXT
%token  LINES
%token  LINESTRING
%token  LOAD
%token  LOCAL_SYM
%token  LOCATE
370
%token  LOCATOR_SYM
371 372 373 374 375 376 377 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
%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
405
%token  MAX_USER_CONNECTIONS_SYM
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
%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
429
%token  MUTEX_SYM
430 431 432 433 434 435
%token  NAMES_SYM
%token  NAME_SYM
%token  NATIONAL_SYM
%token  NATURAL
%token  NCHAR_STRING
%token  NCHAR_SYM
unknown's avatar
unknown committed
436
%token  NDBCLUSTER_SYM
437 438 439 440 441 442 443 444 445 446 447 448
%token  NE
%token  NEW_SYM
%token  NEXT_SYM
%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
449
%token  NVARCHAR_SYM
450 451 452
%token  OFFSET_SYM
%token  OLD_PASSWORD
%token  ON
453
%token  ONE_SHOT_SYM
454 455 456 457 458 459 460 461 462 463 464
%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
465
%token  OUT_SYM
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
%token  PACK_KEYS_SYM
%token  PARTIAL
%token  PASSWORD
%token  PHASE_SYM
%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
%token  READS_SYM
%token  READ_SYM
%token  REAL
%token  RECOVER_SYM
unknown's avatar
unknown committed
497
%token  REDUNDANT_SYM
498 499 500 501 502
%token  REFERENCES
%token  REGEXP
%token  RELAY_LOG_FILE_SYM
%token  RELAY_LOG_POS_SYM
%token  RELAY_THREAD
unknown's avatar
unknown committed
503
%token  RELEASE_SYM
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
%token  RELOAD
%token  RENAME
%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
524
%token  ROUTINE_SYM
525 526 527 528 529 530 531 532
%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
533
%token  SECURITY_SYM
534 535
%token  SELECT_SYM
%token  SENSITIVE_SYM
536
%token  SEPARATOR_SYM
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
%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
%token  SOUNDS_SYM
%token  SPATIAL_SYM
554 555 556 557
%token  SPECIFIC_SYM
%token  SQLEXCEPTION_SYM
%token  SQLSTATE_SYM
%token  SQLWARNING_SYM
558 559 560 561 562 563 564 565
%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
566
%token  SSL_SYM
567 568 569 570
%token  STARTING
%token  START_SYM
%token  STATUS_SYM
%token  STD_SYM
571
%token  STDDEV_SAMP_SYM
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
%token  STOP_SYM
%token  STORAGE_SYM
%token  STRAIGHT_JOIN
%token  STRING_SYM
%token  SUBDATE_SYM
%token  SUBJECT_SYM
%token  SUBSTRING
%token  SUBSTRING_INDEX
%token  SUM_SYM
%token  SUPER_SYM
%token  SUSPEND_SYM
%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
%token  TO_SYM
%token  TRAILING
%token  TRANSACTION_SYM
601
%token  TRIGGER_SYM
602 603 604
%token  TRIM
%token  TRUE_SYM
%token  TRUNCATE_SYM
unknown's avatar
unknown committed
605
%token  TYPES_SYM
606 607 608 609 610 611 612
%token  TYPE_SYM
%token  UDF_RETURNS_SYM
%token  UDF_SONAME_SYM
%token  ULONGLONG_NUM
%token  UNCOMMITTED_SYM
%token  UNDEFINED_SYM
%token  UNDERSCORE_CHARSET
613
%token  UNDO_SYM
614 615 616 617 618 619 620 621 622 623
%token  UNICODE_SYM
%token  UNION_SYM
%token  UNIQUE_SYM
%token  UNIQUE_USERS
%token  UNIX_TIMESTAMP
%token  UNKNOWN_SYM
%token  UNLOCK_SYM
%token  UNLOCK_SYM
%token  UNSIGNED
%token  UNTIL_SYM
624
%token  UNTIL_SYM
625 626 627 628 629 630 631 632 633 634
%token  UPDATE_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
635
%token  VAR_SAMP_SYM
636 637 638 639 640 641 642 643 644 645 646 647
%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
648
%token  WHILE_SYM
649 650 651 652 653 654 655 656 657 658
%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
659

unknown's avatar
unknown committed
660
%left   SET_VAR
661 662
%left	OR_OR_SYM OR_SYM OR2_SYM XOR
%left	AND_SYM AND_AND_SYM
unknown's avatar
unknown committed
663 664 665 666 667 668
%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	'-' '+'
669
%left	'*' '/' '%' DIV_SYM MOD_SYM
unknown's avatar
unknown committed
670
%left   '^'
unknown's avatar
unknown committed
671
%left	NEG '~'
672
%right	NOT_SYM NOT2_SYM
unknown's avatar
unknown committed
673
%right	BINARY COLLATE_SYM
674

unknown's avatar
unknown committed
675
%type <lex_str>
unknown's avatar
unknown committed
676
        IDENT IDENT_QUOTED TEXT_STRING DECIMAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM
677
	LEX_HOSTNAME ULONGLONG_NUM field_ident select_alias ident ident_or_text
678
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
679
	NCHAR_STRING opt_component key_cache_name
unknown's avatar
unknown committed
680
        sp_opt_label BIN_NUM
unknown's avatar
unknown committed
681 682 683 684 685

%type <lex_str_ptr>
	opt_table_alias

%type <table>
686
	table_ident table_ident_nodb references xid
unknown's avatar
unknown committed
687 688

%type <simple_string>
689
	remember_name remember_end opt_ident opt_db text_or_password
690
	opt_constraint constraint ident_or_empty
unknown's avatar
unknown committed
691 692

%type <string>
693
	text_string opt_gconcat_separator
unknown's avatar
unknown committed
694 695

%type <num>
unknown's avatar
unknown committed
696
	type int_type real_type order_dir lock_option
unknown's avatar
unknown committed
697
	udf_type if_exists opt_local opt_table_options table_options
698 699
        table_option opt_if_not_exists opt_no_write_to_binlog opt_var_type
        opt_var_ident_type delete_option opt_temporary all_or_any opt_distinct
700
        opt_ignore_leaves fulltext_options spatial_type union_option
unknown's avatar
unknown committed
701
        start_transaction_opts opt_chain opt_release
unknown's avatar
unknown committed
702
        union_opt select_derived_init
unknown's avatar
unknown committed
703 704

%type <ulong_num>
705
	ULONG_NUM raid_types merge_insert_types
unknown's avatar
unknown committed
706

unknown's avatar
unknown committed
707 708
%type <ulonglong_number>
	ulonglong_num
unknown's avatar
unknown committed
709

unknown's avatar
unknown committed
710 711 712
%type <lock_type>
	replace_lock_option opt_low_priority insert_lock_option load_data_lock

unknown's avatar
unknown committed
713
%type <item>
unknown's avatar
unknown committed
714
	literal text_literal insert_ident order_ident
unknown's avatar
unknown committed
715
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
716 717 718
	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
719
	using_list expr_or_default set_expr_or_default interval_expr
unknown's avatar
unknown committed
720
	param_marker singlerow_subselect singlerow_subselect_init
unknown's avatar
unknown committed
721
	exists_subselect exists_subselect_init geometry_function
722
	signed_literal now_or_signed_literal opt_escape
723 724
	sp_opt_default
	simple_ident_nospvar simple_ident_q
unknown's avatar
unknown committed
725
        field_or_var
726 727 728

%type <item_num>
	NUM_literal
unknown's avatar
unknown committed
729 730

%type <item_list>
731 732
	expr_list udf_expr_list udf_expr_list2 when_list
	ident_list ident_list_arg
unknown's avatar
unknown committed
733 734

%type <key_type>
735
	key_type opt_unique_or_fulltext constraint_key_type
unknown's avatar
unknown committed
736

unknown's avatar
unknown committed
737 738 739
%type <key_alg>
	key_alg opt_btree_or_rtree

unknown's avatar
unknown committed
740 741 742 743 744 745 746
%type <string_list>
	key_usage_list

%type <key_part>
	key_part

%type <table_list>
747
	join_table_list  join_table
748
        table_factor table_ref
unknown's avatar
unknown committed
749
        select_derived derived_table_list
unknown's avatar
unknown committed
750

751
%type <date_time_type> date_time_type;
unknown's avatar
unknown committed
752 753
%type <interval> interval

754 755
%type <interval_time_st> interval_time_st

unknown's avatar
unknown committed
756
%type <db_type> storage_engines
unknown's avatar
unknown committed
757 758 759

%type <row_type> row_types

unknown's avatar
unknown committed
760
%type <tx_isolation> isolation_types
unknown's avatar
unknown committed
761

unknown's avatar
unknown committed
762 763
%type <ha_rkey_mode> handler_rkey_mode

764
%type <cast_type> cast_type
unknown's avatar
unknown committed
765

unknown's avatar
unknown committed
766 767 768 769 770 771
%type <udf_type> udf_func_type

%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword

%type <lex_user> user grant_user

772
%type <charset>
773
	opt_collate
774 775
	charset_name
	charset_name_or_default
unknown's avatar
unknown committed
776 777
	old_or_new_charset_name
	old_or_new_charset_name_or_default
778 779
	collation_name
	collation_name_or_default
780

unknown's avatar
unknown committed
781 782
%type <variable> internal_variable_name

unknown's avatar
unknown committed
783
%type <select_lex> in_subselect in_subselect_init
unknown's avatar
unknown committed
784
	get_select_lex
unknown's avatar
unknown committed
785

unknown's avatar
unknown committed
786 787
%type <boolfunc2creator> comp_op

unknown's avatar
unknown committed
788
%type <NONE>
unknown's avatar
unknown committed
789
	query verb_clause create change select do drop insert replace insert2
790
	insert_values update delete truncate rename
unknown's avatar
unknown committed
791
	show describe load alter optimize keycache preload flush
unknown's avatar
unknown committed
792
	reset purge begin commit rollback savepoint release
793
	slave master_def master_defs master_file_def slave_until_opts
794
	repair restore backup analyze check start checksum
795
	field_list field_list_item field_spec kill column_def key_def
unknown's avatar
unknown committed
796
	keycache_list assign_to_keycache preload_list preload_keys
unknown's avatar
unknown committed
797
	select_item_list select_item values_list no_braces
798
	opt_limit_clause delete_limit_clause fields opt_values values
unknown's avatar
unknown committed
799
	procedure_list procedure_list2 procedure_item
800
	when_list2 expr_list2 udf_expr_list3 handler
unknown's avatar
unknown committed
801 802
	opt_precision opt_ignore opt_column opt_restrict
	grant revoke set lock unlock string_list field_options field_option
unknown's avatar
unknown committed
803
	field_opt_list opt_binary table_lock_list table_lock
804
	ref_list opt_on_delete opt_on_delete_list opt_on_delete_item use
unknown's avatar
unknown committed
805
	opt_delete_options opt_delete_option varchar nchar nvarchar
unknown's avatar
unknown committed
806
	opt_outer table_list table_name opt_option opt_place
unknown's avatar
unknown committed
807
	opt_attribute opt_attribute_list attribute column_list column_list_id
808
	opt_column_list grant_privileges opt_table grant_list grant_option
809
	object_privilege object_privilege_list user_list rename_list
810
	clear_privileges flush_options flush_option
unknown's avatar
unknown committed
811 812
	equal optional_braces opt_key_definition key_usage_list2
	opt_mi_check_type opt_to mi_check_types normal_join
813
	table_to_table_list table_to_table opt_table_list opt_as
unknown's avatar
unknown committed
814
	handler_rkey_function handler_read_or_scan
unknown's avatar
unknown committed
815
	single_multi table_wild_list table_wild_one opt_wild
816
	union_clause union_list
817
	precision subselect_start opt_and charset
818
	subselect_end select_var_list select_var_list_init help opt_len
unknown's avatar
unknown committed
819
	opt_extended_describe
820
        prepare prepare_src execute deallocate 
821
	statement sp_suid opt_view_list view_list or_replace algorithm
822
	sp_c_chistics sp_a_chistics sp_chistic sp_c_chistic xa
unknown's avatar
unknown committed
823
        load_data opt_field_or_var_spec fields_or_vars opt_load_data_set_spec
824
END_OF_INPUT
unknown's avatar
unknown committed
825

826 827 828 829 830 831 832
%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
833 834
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
835
	',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_SYM BETWEEN_SYM CASE_SYM
836
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM OR2_SYM AND_AND_SYM
unknown's avatar
unknown committed
837 838 839 840 841
%%


query:
	END_OF_INPUT
unknown's avatar
unknown committed
842
	{
843
	   THD *thd= YYTHD;
844
	   if (!thd->bootstrap &&
845
	      (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
846
	   {
unknown's avatar
unknown committed
847
	     my_message(ER_EMPTY_QUERY, ER(ER_EMPTY_QUERY), MYF(0));
848
	     YYABORT;
849
	   }
850 851
	   else
	   {
852
	     thd->lex->sql_command= SQLCOM_EMPTY_QUERY;
853
	   }
unknown's avatar
unknown committed
854
	}
unknown's avatar
unknown committed
855
	| verb_clause END_OF_INPUT {};
unknown's avatar
unknown committed
856 857

verb_clause:
858 859 860 861 862 863
	  statement
	| begin
	;

/* Verb clauses, except begin */
statement:
unknown's avatar
unknown committed
864 865
	  alter
	| analyze
866
	| backup
867
	| call
unknown's avatar
unknown committed
868 869
	| change
	| check
870
	| checksum
unknown's avatar
unknown committed
871 872
	| commit
	| create
873
        | deallocate
unknown's avatar
unknown committed
874 875
	| delete
	| describe
unknown's avatar
unknown committed
876
	| do
unknown's avatar
unknown committed
877
	| drop
878
        | execute
unknown's avatar
unknown committed
879
	| flush
unknown's avatar
unknown committed
880
	| grant
unknown's avatar
unknown committed
881 882
	| handler
	| help
unknown's avatar
unknown committed
883
	| insert
unknown's avatar
unknown committed
884
	| kill
unknown's avatar
unknown committed
885 886 887
	| load
	| lock
	| optimize
unknown's avatar
unknown committed
888
        | keycache
unknown's avatar
unknown committed
889
	| preload
890
        | prepare
891
	| purge
unknown's avatar
unknown committed
892
	| release
893
	| rename
unknown's avatar
unknown committed
894
	| repair
unknown's avatar
unknown committed
895
	| replace
unknown's avatar
unknown committed
896
	| reset
897
	| restore
unknown's avatar
unknown committed
898 899
	| revoke
	| rollback
900
	| savepoint
unknown's avatar
unknown committed
901 902
	| select
	| set
unknown's avatar
unknown committed
903
	| show
unknown's avatar
unknown committed
904
	| slave
unknown's avatar
unknown committed
905
	| start
906
	| truncate
unknown's avatar
unknown committed
907 908
	| unlock
	| update
unknown's avatar
unknown committed
909
	| use
910
	| xa
unknown's avatar
unknown committed
911
        ;
912

unknown's avatar
unknown committed
913
deallocate:
914
        deallocate_or_drop PREPARE_SYM ident
unknown's avatar
unknown committed
915 916
        {
          THD *thd=YYTHD;
917
          LEX *lex= thd->lex;
unknown's avatar
unknown committed
918 919 920 921 922
          if (thd->command == COM_PREPARE)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
923
          lex->sql_command= SQLCOM_DEALLOCATE_PREPARE;
unknown's avatar
unknown committed
924 925 926
          lex->prepared_stmt_name= $3;
        };

927 928 929 930 931 932
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


unknown's avatar
unknown committed
933
prepare:
934
        PREPARE_SYM ident FROM prepare_src
unknown's avatar
unknown committed
935 936
        {
          THD *thd=YYTHD;
937
          LEX *lex= thd->lex;
unknown's avatar
unknown committed
938 939 940 941 942
          if (thd->command == COM_PREPARE)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
943
          lex->sql_command= SQLCOM_PREPARE;
unknown's avatar
unknown committed
944 945 946
          lex->prepared_stmt_name= $2;
        };

947 948 949 950 951 952
prepare_src:
        TEXT_STRING_sys
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $1;
unknown's avatar
unknown committed
953
          lex->prepared_stmt_code_is_varref= FALSE;
954 955 956 957 958 959
        }
        | '@' ident_or_text
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $2;
unknown's avatar
unknown committed
960
          lex->prepared_stmt_code_is_varref= TRUE;
961
        };
962

unknown's avatar
unknown committed
963 964 965 966
execute:
        EXECUTE_SYM ident
        {
          THD *thd=YYTHD;
967
          LEX *lex= thd->lex;
unknown's avatar
unknown committed
968 969 970 971 972
          if (thd->command == COM_PREPARE)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
973
          lex->sql_command= SQLCOM_EXECUTE;
unknown's avatar
unknown committed
974 975 976 977 978 979 980 981 982 983 984 985
          lex->prepared_stmt_name= $2;
        }
        execute_using
        {}
        ;

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
986 987
        execute_var_list ',' execute_var_ident
        | execute_var_ident
unknown's avatar
unknown committed
988 989 990 991 992 993 994 995
        ;

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;
996
        }
unknown's avatar
unknown committed
997 998
        ;

unknown's avatar
unknown committed
999 1000
/* help */

1001
help:
1002
       HELP_SYM ident_or_text
unknown's avatar
unknown committed
1003
       {
1004 1005 1006
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
	  lex->help_arg= $2.str;
1007
       };
unknown's avatar
unknown committed
1008 1009 1010 1011

/* change master */

change:
unknown's avatar
unknown committed
1012
       CHANGE MASTER_SYM TO_SYM
unknown's avatar
unknown committed
1013 1014 1015
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
unknown's avatar
unknown committed
1016
	  bzero((char*) &lex->mi, sizeof(lex->mi));
1017 1018 1019 1020
        }
       master_defs
	{}
       ;
unknown's avatar
unknown committed
1021 1022 1023

master_defs:
       master_def
unknown's avatar
unknown committed
1024
       | master_defs ',' master_def;
unknown's avatar
unknown committed
1025

1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 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 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
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;
       }
       |
       MASTER_PORT_SYM EQ ULONG_NUM
       {
	 Lex->mi.port = $3;
       }
       |
       MASTER_CONNECT_RETRY_SYM EQ ULONG_NUM
       {
	 Lex->mi.connect_retry = $3;
       }
       | MASTER_SSL_SYM EQ ULONG_NUM
         {
           Lex->mi.ssl= $3 ? 
               LEX_MASTER_INFO::SSL_ENABLE : LEX_MASTER_INFO::SSL_DISABLE;
         }
       | MASTER_SSL_CA_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_ca= $3.str;
         }
       | MASTER_SSL_CAPATH_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_capath= $3.str;
         }
       | MASTER_SSL_CERT_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_cert= $3.str;
         }
       | MASTER_SSL_CIPHER_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_cipher= $3.str;
         }
       | MASTER_SSL_KEY_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_key= $3.str;
	 }
       |
         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;
         }
       | RELAY_LOG_POS_SYM EQ ULONG_NUM
         {
           Lex->mi.relay_log_pos = $3;
           /* Adjust if < BIN_LOG_HEADER_SIZE (same comment as Lex->mi.pos) */
           Lex->mi.relay_log_pos = max(BIN_LOG_HEADER_SIZE, Lex->mi.relay_log_pos);
         }
       ;

/* 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;
	  lex->create_info.db_type= (enum db_type) lex->thd->variables.table_type;
	  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;
	  }
	   '(' key_list ')'
	  {
	    LEX *lex=Lex;

	    lex->key_list.push_back(new Key($2,$4.str, $5, 0, lex->col_list));
	    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
1185
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "PROCEDURE");
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
	      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);
	  }
	| CREATE or_replace algorithm VIEW_SYM table_ident
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_CREATE_VIEW;
	    lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE;
	    /* first table in list is target VIEW name */
	    if (!lex->select_lex.add_table_to_list(thd, $5, NULL, 0))
              YYABORT;
	  }
	  opt_view_list AS select_init check_option
	  {}
        | CREATE TRIGGER_SYM ident trg_action_time trg_event 
          ON table_ident FOR_SYM EACH_SYM ROW_SYM
          {
            LEX *lex= Lex;
            sp_head *sp;
           
            if (lex->sphead)
            {
unknown's avatar
unknown committed
1258
              my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "TRIGGER");
1259 1260
              YYABORT;
            }
1261 1262 1263

            if (!(sp= new sp_head()))
              YYABORT;
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
            sp->reset_thd_mem_root(YYTHD);
            sp->init(lex);
            
            sp->m_type= TYPE_ENUM_TRIGGER;
            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;
            
            bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
            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;
            
            lex->sql_command= SQLCOM_CREATE_TRIGGER;
            sp->init_strings(YYTHD, lex, NULL);
            /* Restore flag if it was cleared above */
            if (sp->m_old_cmq)
              YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
            sp->restore_thd_mem_root(YYTHD);

1293
            lex->ident= $3;
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309

            /*
              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;
          }
1310 1311 1312 1313
	| CREATE USER clear_privileges grant_list
	  {
	    Lex->sql_command = SQLCOM_CREATE_USER;
          }
1314 1315
	;

1316 1317 1318 1319 1320 1321 1322
clear_privileges:
        /* Nothing */
        {
          LEX *lex=Lex;
          lex->users_list.empty();
          lex->columns.empty();
          lex->grant= lex->grant_tot_col= 0;
1323
	  lex->all_privileges= 0;
1324 1325 1326 1327 1328 1329 1330
          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));
        }
        ;

1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
sp_name:
	  IDENT_sys '.' IDENT_sys
	  {
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
	| IDENT_sys
	  {
	    $$= sp_name_current_db_new(YYTHD, $1);
	  }
	;

create_function_tail:
	  RETURNS_SYM udf_type UDF_SONAME_SYM TEXT_STRING_sys
	  {
	    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
1359
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
	      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
1387 1388 1389 1390
	    lex->charset= NULL;
	    lex->length= lex->dec= NULL;
	    lex->interval_list.empty();
	    lex->type= 0;
1391 1392 1393 1394 1395
	  }
	  type
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
unknown's avatar
unknown committed
1396 1397 1398 1399 1400
            LEX_STRING cmt = { 0, 0 };
	    create_field *new_field;
	    uint unused1= 0;
	    int unused2= 0;

unknown's avatar
unknown committed
1401 1402 1403 1404 1405 1406 1407 1408
	    if (!(new_field= new_create_field(YYTHD, "",
					      (enum enum_field_types)$8,
			  		      lex->length, lex->dec, lex->type,
			  		      (Item *)0, (Item *) 0, &cmt, 0,
					      &lex->interval_list, 
			  		      (lex->charset ? lex->charset :
					       default_charset_info),
					      lex->uint_geom_type)))
unknown's avatar
unknown committed
1409 1410
	      YYABORT;

unknown's avatar
unknown committed
1411 1412
	    if (prepare_create_field(new_field, &unused1, &unused2, &unused2,
				     0))
unknown's avatar
unknown committed
1413 1414 1415 1416 1417 1418 1419 1420
	      YYABORT;

	    sp->m_returns= new_field->sql_type;
	    sp->m_returns_cs= new_field->charset;
	    sp->m_returns_len= new_field->length;
	    sp->m_returns_pack= new_field->pack_flag;
	    sp->m_returns_typelib= 
              sp->create_typelib(&new_field->interval_list);
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478

	    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;
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
	    sp->init_strings(YYTHD, lex, lex->spname);
	    /* 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; }
1479
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
	;

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();
1501
	    sp_add_to_hash(&lex->spprocs, $2);
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
	  }
          '(' 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
	;

sp_fdparam:
	  ident type
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
1542
	      my_error(ER_SP_DUP_PARAM, MYF(0), $1.str);
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum enum_field_types)$2, sp_param_in);
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
	  sp_opt_inout ident type
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$2, TRUE))
	    {
1568
	      my_error(ER_SP_DUP_PARAM, MYF(0), $2.str);
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
	      YYABORT;
	    }
	    spc->push_pvar(&$2, (enum enum_field_types)$3,
			   (sp_param_mode_t)$1);
	  }
	;

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 */ {}
1585
	| sp_proc_stmts  sp_proc_stmt ';'
1586 1587 1588 1589
	;

sp_proc_stmts1:
	  sp_proc_stmt ';' {}
1590
	| sp_proc_stmts1  sp_proc_stmt ';'
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
	;

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
1606 1607
	      my_message(ER_SP_VARCOND_AFTER_CURSHNDLR,
                         ER(ER_SP_VARCOND_AFTER_CURSHNDLR), MYF(0));
1608 1609 1610 1611
	      YYABORT;
	    }
	    if ($2.curs && $1.hndlrs)
	    { /* Cursor following handler */
unknown's avatar
unknown committed
1612 1613
	      my_message(ER_SP_CURSOR_AFTER_HANDLER,
                         ER(ER_SP_CURSOR_AFTER_HANDLER), MYF(0));
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
	      YYABORT;
	    }
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
	    $$.curs= $1.curs + $2.curs;
	  }
	;

sp_decl:
1624 1625 1626
	  DECLARE_SYM sp_decl_idents type 
          { Lex->sphead->reset_lex(YYTHD); }
          sp_opt_default
1627 1628 1629 1630 1631
	  {
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    uint max= ctx->context_pvars();
	    enum enum_field_types type= (enum enum_field_types)$3;
1632
	    Item *it= $5;
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643

	    for (uint i = max-$2 ; i < max ; i++)
	    {
	      ctx->set_type(i, type);
	      if (! it)
	        ctx->set_isset(i, FALSE);
	      else
	      {
	        sp_instr_set *in= new sp_instr_set(lex->sphead->instructions(),
	                                           ctx,
						   ctx->pvar_context2index(i),
1644 1645
                                                   it, type, lex,
                                                   (i == max - 1));
1646

1647 1648 1649 1650
                /*
                  The last instruction is assigned to be responsible for
                  freeing LEX.
                */
1651 1652 1653 1654 1655
	        lex->sphead->add_instr(in);
	        ctx->set_isset(i, TRUE);
		ctx->set_default(i, it);
	      }
	    }
1656
            lex->sphead->restore_lex(YYTHD);
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
	    $$.vars= $2;
	    $$.conds= $$.hndlrs= $$.curs= 0;
	  }
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1667
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
	      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));
	    ctx->add_handler();
	    sp->m_in_handler= TRUE;
	  }
	  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);
	    sp->m_in_handler= FALSE;
	    $$.vars= $$.conds= $$.curs= 0;
	    $$.hndlrs= $6;
	  }
	| 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))
	    {
1724
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
	      delete $5;
	      YYABORT;
	    }
            i= new sp_instr_cpush(sp->instructions(), ctx, $5);
	    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
1751 1752
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
1753 1754 1755 1756
	      YYABORT;
	    }
	    if (lex->result)
	    {
unknown's avatar
unknown committed
1757 1758
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
	      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;
	    sp_instr_hpush_jump *i= (sp_instr_hpush_jump *)sp->last_instruction();

	    i->add_condition($1);
	    $$= 1;
	  }
	| sp_hcond_list ',' sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_instr_hpush_jump *i= (sp_instr_hpush_jump *)sp->last_instruction();

	    i->add_condition($3);
	    $$= $1 + 1;
	  }
	;

sp_cond:
	  ULONG_NUM
	  {			/* mysql errno */
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::number;
	    $$->mysqlerr= $1;
	  }
	| SQLSTATE_SYM opt_value TEXT_STRING_literal
	  {		/* SQLSTATE */
1803 1804 1805 1806 1807
	    if (!sp_cond_check(&$3))
	    {
	      my_error(ER_SP_BAD_SQLSTATE, MYF(0), $3.str);
	      YYABORT;
	    }
1808 1809
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
1810 1811
	    memcpy($$->sqlstate, $3.str, 5);
	    $$->sqlstate[5]= '\0';
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
	  }
	;

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
1830
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
1831 1832 1833 1834 1835 1836 1837 1838
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
1839
	| not FOUND_SYM		/* SQLSTATEs 02??? */
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
	  {
	    $$= (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
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$1, TRUE))
	    {
1859
	      my_error(ER_SP_DUP_VAR, MYF(0), $1.str);
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum_field_types)0, sp_param_in);
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$3, TRUE))
	    {
1872
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
	      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;

	    if ((lex->sql_command == SQLCOM_SELECT && !lex->result) ||
	        sp_multi_results_command(lex->sql_command))
	    {
	      /* We maybe have one or more SELECT without INTO */
	      sp->m_multi_results= TRUE;
	    }
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
unknown's avatar
unknown committed
1905
	      my_message(ER_SP_NO_USE, ER(ER_SP_NO_USE), MYF(0));
1906 1907
	      YYABORT;
	    }
1908 1909 1910 1911
	    /*
              Don't add an instruction for SET statements, since all
              instructions for them were already added during processing
              of "set" rule.
1912
	    */
1913 1914 1915
            DBUG_ASSERT(lex->sql_command != SQLCOM_SET_OPTION ||
                        lex->var_list.is_empty());
            if (lex->sql_command != SQLCOM_SET_OPTION)
1916
	    {
1917 1918
              sp_instr_stmt *i=new sp_instr_stmt(sp->instructions(),
                                                 lex->spcont, lex);
1919

1920
              /* Extract the query statement from the tokenizer:
1921
                 The end is either lex->tok_end or tok->ptr. */
1922 1923 1924 1925 1926 1927 1928 1929
              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);
1930 1931 1932
            }
	    sp->restore_lex(YYTHD);
          }
1933 1934 1935
          | RETURN_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr
1936 1937
	  {
	    LEX *lex= Lex;
1938
	    sp_head *sp= lex->sphead;
1939

1940
	    if (sp->m_type == TYPE_ENUM_PROCEDURE)
1941
	    {
unknown's avatar
unknown committed
1942
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
1943 1944 1945 1946 1947 1948
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_freturn *i;

1949 1950 1951 1952
	      i= new sp_instr_freturn(sp->instructions(), lex->spcont,
		                      $3, sp->m_returns, lex);
	      sp->add_instr(i);
	      sp->m_has_return= TRUE;
1953
	    }
1954
	    sp->restore_lex(YYTHD);
1955 1956 1957 1958 1959 1960 1961
	  }
	| IF sp_if END IF {}
	| CASE_SYM WHEN_SYM
	  {
	    Lex->sphead->m_simple_case= FALSE;
	  }
	  sp_case END CASE_SYM {}
1962 1963 1964
        | CASE_SYM
          { Lex->sphead->reset_lex(YYTHD); }
          expr WHEN_SYM
1965 1966 1967 1968 1969 1970 1971 1972
	  {
	    /* We "fake" this by using an anonymous variable which we
	       set to the expression. Note that all WHENs are evaluate
	       at the same frame level, so we then know that it's the
	       top-most variable in the frame. */
	    LEX *lex= Lex;
	    uint offset= lex->spcont->current_pvars();
	    sp_instr_set *i = new sp_instr_set(lex->sphead->instructions(),
1973 1974
                                               lex->spcont, offset, $3,
                                               MYSQL_TYPE_STRING, lex, TRUE);
1975
	    LEX_STRING dummy={(char*)"", 0};
1976 1977 1978 1979

	    lex->spcont->push_pvar(&dummy, MYSQL_TYPE_STRING, sp_param_in);
	    lex->sphead->add_instr(i);
	    lex->sphead->m_simple_case= TRUE;
1980
            lex->sphead->restore_lex(YYTHD);
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
	  }
	  sp_case END CASE_SYM
	  {
	    Lex->spcont->pop_pvar();
	  }
	| 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());
	  }
	| LEAVE_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_head *sp = lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab)
	    {
2008
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
	      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);
	    }
	  }
	| ITERATE_SYM IDENT
	  {
	    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)
	    {
2038
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "ITERATE", $2.str);
2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
	      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
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (lab)
	    {
2066
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $2.str);
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
	      YYABORT;
	    }
	    else
	    {
	      lab= ctx->push_label($2.str, sp->instructions());
	      lab->type= SP_LAB_GOTO;
	      lab->ctx= ctx;
              sp->backpatch(lab);
	    }
	  }
	| GOTO_SYM IDENT
	  {
	    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
2090
	      my_message(ER_SP_GOTO_IN_HNDLR, ER(ER_SP_GOTO_IN_HNDLR), MYF(0));
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
	      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);
	    }
	  }
	| OPEN_SYM ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_copen *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2141
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
	      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))
	    {
2156
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $3.str);
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
	      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))
	    {
2173
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
	      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)))
	    {
2197
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

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

	    if (!spc || !(spv = spc->find_pvar(&$3)))
	    {
2219
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $3.str);
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	      spv->isset= TRUE;
	    }
	  }
	;

sp_if:
2234 2235
          { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2236 2237 2238 2239 2240
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= sp->instructions();
2241 2242
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, ctx,
                                                               $2, lex);
2243 2244 2245

	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
            sp->add_instr(i);
2246
            sp->restore_lex(YYTHD);
2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
	  }
	  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:
2274 2275
	  { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2276 2277 2278 2279 2280 2281 2282 2283
	  {
            LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

	    if (! sp->m_simple_case)
2284
	      i= new sp_instr_jump_if_not(ip, ctx, $2, lex);
2285 2286 2287 2288 2289 2290 2291 2292
	    else
	    { /* Simple case: <caseval> = <whenval> */
	      LEX_STRING ivar;

	      ivar.str= (char *)"_tmp_";
	      ivar.length= 5;
	      Item *var= (Item*) new Item_splocal(ivar, 
						  ctx->current_pvars()-1);
2293
	      Item *expr= new Item_func_eq(var, $2);
2294

2295
	      i= new sp_instr_jump_if_not(ip, ctx, expr, lex);
2296 2297 2298 2299
              lex->variables_used= 1;
	    }
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
            sp->add_instr(i);
2300
            sp->restore_lex(YYTHD);
2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
	  }
	  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
2320

2321 2322 2323 2324 2325 2326 2327
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
2328

2329 2330 2331 2332 2333
	    sp->add_instr(i);
	  }
	| ELSE sp_proc_stmts1 {}
	| WHEN_SYM sp_case {}
	;
unknown's avatar
unknown committed
2334

2335 2336
sp_labeled_control:
	  IDENT ':'
unknown's avatar
unknown committed
2337
	  {
2338 2339 2340 2341 2342 2343
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);

	    if (lab)
	    {
2344
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $1.str);
unknown's avatar
unknown committed
2345
	      YYABORT;
2346 2347 2348 2349 2350 2351 2352
	    }
	    else
	    {
	      lab= lex->spcont->push_label($1.str,
	                                   lex->sphead->instructions());
	      lab->type= SP_LAB_ITER;
	    }
unknown's avatar
unknown committed
2353
	  }
2354
	  sp_unlabeled_control sp_opt_label
unknown's avatar
unknown committed
2355
	  {
2356
	    LEX *lex= Lex;
unknown's avatar
unknown committed
2357

2358 2359 2360 2361 2362 2363 2364
	    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)
	      {
2365
	        my_error(ER_SP_LABEL_MISMATCH, MYF(0), $5.str);
2366 2367 2368 2369
	        YYABORT;
	      }
	    }
	    lex->sphead->backpatch(lex->spcont->pop_label());
unknown's avatar
unknown committed
2370
	  }
2371 2372 2373
	;

sp_opt_label:
2374 2375
        /* Empty  */    { $$= null_lex_str; }
        | IDENT         { $$= $1; }
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
	;

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
2392
	  {
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
	    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
2408
	  {
2409 2410 2411 2412 2413 2414
	    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
2415
	  }
2416 2417 2418
        | WHILE_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr DO_SYM
unknown's avatar
unknown committed
2419
	  {
2420 2421 2422 2423
	    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,
2424
							       $3, lex);
2425 2426 2427 2428

	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
            sp->add_instr(i);
2429
            sp->restore_lex(YYTHD);
unknown's avatar
unknown committed
2430
	  }
2431
	  sp_proc_stmts1 END WHILE_SYM
unknown's avatar
unknown committed
2432
	  {
2433 2434 2435 2436 2437 2438 2439
	    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);
	  }
2440 2441 2442
        | REPEAT_SYM sp_proc_stmts1 UNTIL_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr END REPEAT_SYM
2443 2444 2445 2446 2447
	  {
	    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,
2448 2449
                                                               $5, lab->ip,
                                                               lex);
2450
            lex->sphead->add_instr(i);
2451
            lex->sphead->restore_lex(YYTHD);
2452
	  }
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
	;

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; }
2469
          ;
unknown's avatar
unknown committed
2470 2471

create2:
unknown's avatar
unknown committed
2472 2473 2474 2475 2476 2477
        '(' create2a {}
        | opt_create_table_options create3 {}
        | LIKE table_ident
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$2))
unknown's avatar
unknown committed
2478
              YYABORT;
unknown's avatar
unknown committed
2479 2480 2481 2482 2483
          }
        | '(' LIKE table_ident ')'
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$3))
2484
              YYABORT;
unknown's avatar
unknown committed
2485
          }
unknown's avatar
unknown committed
2486
        ;
unknown's avatar
unknown committed
2487

unknown's avatar
unknown committed
2488 2489
create2a:
        field_list ')' opt_create_table_options create3 {}
unknown's avatar
unknown committed
2490
	|  create_select ')' { Select->set_braces(1);} union_opt {}
2491
        ;
unknown's avatar
unknown committed
2492 2493

create3:
2494
	/* empty */ {}
unknown's avatar
unknown committed
2495
	| opt_duplicate opt_as     create_select
unknown's avatar
unknown committed
2496
          { Select->set_braces(0);} union_clause {}
unknown's avatar
unknown committed
2497
	| opt_duplicate opt_as '(' create_select ')'
unknown's avatar
unknown committed
2498
          { Select->set_braces(1);} union_opt {}
2499 2500
        ;

unknown's avatar
unknown committed
2501
create_select:
2502
          SELECT_SYM
unknown's avatar
unknown committed
2503
          {
unknown's avatar
unknown committed
2504
	    LEX *lex=Lex;
2505
	    lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
2506 2507 2508 2509
	    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;
2510 2511 2512 2513
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
unknown's avatar
unknown committed
2514
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
unknown's avatar
unknown committed
2515
	    mysql_init_select(lex);
2516
	    lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
2517
          }
unknown's avatar
unknown committed
2518 2519
          select_options select_item_list
	  {
2520
	    Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
2521
	  }
unknown's avatar
unknown committed
2522
	  opt_select_from
2523 2524 2525 2526 2527 2528 2529
	  {
	    /*
              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
2530
        ;
unknown's avatar
unknown committed
2531

2532 2533
opt_as:
	/* empty */ {}
unknown's avatar
unknown committed
2534
	| AS	    {};
2535

2536 2537 2538 2539 2540 2541 2542 2543 2544
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
2545 2546
	default_collation   {}
	| default_charset   {};
2547

unknown's avatar
unknown committed
2548
opt_table_options:
unknown's avatar
unknown committed
2549
	/* empty */	 { $$= 0; }
unknown's avatar
unknown committed
2550
	| table_options  { $$= $1;};
unknown's avatar
unknown committed
2551 2552 2553

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

unknown's avatar
unknown committed
2556
table_option:
unknown's avatar
unknown committed
2557
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
unknown's avatar
unknown committed
2558 2559

opt_if_not_exists:
unknown's avatar
unknown committed
2560
	/* empty */	 { $$= 0; }
2561
	| IF not EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
unknown's avatar
unknown committed
2562 2563 2564

opt_create_table_options:
	/* empty */
unknown's avatar
unknown committed
2565
	| create_table_options;
unknown's avatar
unknown committed
2566

unknown's avatar
unknown committed
2567 2568 2569 2570
create_table_options_space_separated:
	create_table_option
	| create_table_option create_table_options_space_separated;

unknown's avatar
unknown committed
2571 2572
create_table_options:
	create_table_option
2573
	| create_table_option     create_table_options
unknown's avatar
unknown committed
2574
	| create_table_option ',' create_table_options;
unknown's avatar
unknown committed
2575 2576

create_table_option:
2577 2578
	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; }
2579 2580 2581
	| MAX_ROWS opt_equal ulonglong_num	{ Lex->create_info.max_rows= $3; Lex->create_info.used_fields|= HA_CREATE_USED_MAX_ROWS;}
	| MIN_ROWS opt_equal ulonglong_num	{ Lex->create_info.min_rows= $3; Lex->create_info.used_fields|= HA_CREATE_USED_MIN_ROWS;}
	| AVG_ROW_LENGTH opt_equal ULONG_NUM	{ Lex->create_info.avg_row_length=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AVG_ROW_LENGTH;}
2582 2583
	| 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; }
2584 2585 2586
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
	| PACK_KEYS_SYM opt_equal ULONG_NUM	{ Lex->create_info.table_options|= $3 ? HA_OPTION_PACK_KEYS : HA_OPTION_NO_PACK_KEYS; Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;}
	| PACK_KEYS_SYM opt_equal DEFAULT	{ Lex->create_info.table_options&= ~(HA_OPTION_PACK_KEYS | HA_OPTION_NO_PACK_KEYS); Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;}
2587 2588 2589
	| 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; }
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
2590 2591 2592 2593
	| RAID_TYPE opt_equal raid_types	{ Lex->create_info.raid_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| RAID_CHUNKS opt_equal ULONG_NUM	{ Lex->create_info.raid_chunks= $3; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| RAID_CHUNKSIZE opt_equal ULONG_NUM	{ Lex->create_info.raid_chunksize= $3*RAID_BLOCK_SIZE; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| UNION_SYM opt_equal '(' table_list ')'
2594 2595 2596
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
2597 2598
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
2599
	    lex->create_info.merge_list.elements--;
2600 2601
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
2602
	    lex->select_lex.table_list.elements=1;
2603 2604 2605
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
2606
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
2607
	  }
2608 2609
	| default_charset
	| default_collation
2610
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
2611 2612 2613
	| DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.data_file_name= $4.str; Lex->create_info.used_fields|= HA_CREATE_USED_DATADIR; }
	| INDEX_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.index_file_name= $4.str;  Lex->create_info.used_fields|= HA_CREATE_USED_INDEXDIR; }
        ;
unknown's avatar
unknown committed
2614

2615 2616 2617 2618 2619 2620 2621 2622
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))
          {
2623 2624 2625
            my_error(ER_CONFLICTING_DECLARATIONS, MYF(0),
                     "CHARACTER SET ", cinfo->default_table_charset->csname,
                     "CHARACTER SET ", $4->csname);
2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639
            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))
            {
2640 2641
              my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $4->name, cinfo->default_table_charset->csname);
2642 2643 2644 2645 2646 2647
              YYABORT;
            }
            Lex->create_info.default_table_charset= $4;
            Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

unknown's avatar
unknown committed
2648
storage_engines:
2649 2650 2651 2652
	ident_or_text
	{
	  $$ = ha_resolve_by_name($1.str,$1.length);
	  if ($$ == DB_TYPE_UNKNOWN) {
2653
	    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
2654 2655 2656
	    YYABORT;
	  }
	};
unknown's avatar
unknown committed
2657 2658 2659 2660 2661

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
2662 2663 2664
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; }
	| REDUNDANT_SYM	{ $$= ROW_TYPE_REDUNDANT; }
	| COMPACT_SYM	{ $$= ROW_TYPE_COMPACT; };
unknown's avatar
unknown committed
2665 2666 2667 2668

raid_types:
	RAID_STRIPED_SYM { $$= RAID_TYPE_0; }
	| RAID_0_SYM	 { $$= RAID_TYPE_0; }
unknown's avatar
unknown committed
2669
	| ULONG_NUM	 { $$=$1;};
unknown's avatar
unknown committed
2670

2671 2672 2673
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
unknown's avatar
unknown committed
2674
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
2675

unknown's avatar
unknown committed
2676
opt_select_from:
2677
	opt_limit_clause {}
unknown's avatar
unknown committed
2678
	| select_from select_lock_type;
unknown's avatar
unknown committed
2679 2680

udf_func_type:
2681
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
unknown's avatar
unknown committed
2682
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
unknown's avatar
unknown committed
2683 2684 2685 2686

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
unknown's avatar
unknown committed
2687
        | DECIMAL_SYM {$$ = (int) DECIMAL_RESULT; }
unknown's avatar
unknown committed
2688
	| INT_SYM {$$ = (int) INT_RESULT; };
unknown's avatar
unknown committed
2689 2690 2691

field_list:
	  field_list_item
unknown's avatar
unknown committed
2692
	| field_list ',' field_list_item;
unknown's avatar
unknown committed
2693 2694 2695


field_list_item:
unknown's avatar
unknown committed
2696
	   column_def
2697 2698 2699 2700
         | key_def
         ;

column_def:
unknown's avatar
unknown committed
2701
	  field_spec opt_check_constraint
unknown's avatar
unknown committed
2702 2703 2704 2705
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
unknown's avatar
unknown committed
2706
	;
2707 2708

key_def:
unknown's avatar
unknown committed
2709
	key_type opt_ident key_alg '(' key_list ')'
unknown's avatar
unknown committed
2710
	  {
unknown's avatar
unknown committed
2711
	    LEX *lex=Lex;
2712
	    lex->key_list.push_back(new Key($1,$2, $3, 0, lex->col_list));
unknown's avatar
unknown committed
2713
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
2714
	  }
2715 2716 2717 2718
	| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
	  {
	    LEX *lex=Lex;
	    const char *key_name= $3 ? $3:$1;
2719 2720
	    lex->key_list.push_back(new Key($2, key_name, $4, 0,
				    lex->col_list));
2721 2722
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
2723
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
unknown's avatar
unknown committed
2724
	  {
2725
	    LEX *lex=Lex;
2726
	    lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
2727 2728 2729 2730 2731
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
2732 2733 2734
	    lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4 : $1,
					    HA_KEY_ALG_UNDEF, 1,
					    lex->col_list));
2735
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
2736
	  }
unknown's avatar
unknown committed
2737 2738 2739 2740
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
2741
	| opt_constraint check_constraint
unknown's avatar
unknown committed
2742 2743
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
2744 2745 2746
	  }
	;

unknown's avatar
unknown committed
2747
opt_check_constraint:
2748
	/* empty */
unknown's avatar
unknown committed
2749 2750 2751 2752 2753
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
2754
	;
unknown's avatar
unknown committed
2755 2756

opt_constraint:
2757
	/* empty */		{ $$=(char*) 0; }
unknown's avatar
unknown committed
2758 2759 2760 2761 2762 2763
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
unknown's avatar
unknown committed
2764 2765 2766 2767

field_spec:
	field_ident
	 {
unknown's avatar
unknown committed
2768
	   LEX *lex=Lex;
2769
	   lex->length=lex->dec=0; lex->type=0;
2770
	   lex->default_value= lex->on_update_value= 0;
2771
           lex->comment=null_lex_str;
2772
	   lex->charset=NULL;
unknown's avatar
unknown committed
2773 2774 2775
	 }
	type opt_attribute
	{
unknown's avatar
unknown committed
2776
	  LEX *lex=Lex;
unknown's avatar
unknown committed
2777
	  if (add_field_to_list(lex->thd, $1.str,
unknown's avatar
unknown committed
2778
				(enum enum_field_types) $3,
unknown's avatar
unknown committed
2779
				lex->length,lex->dec,lex->type,
2780
				lex->default_value, lex->on_update_value, 
2781
                                &lex->comment,
2782
				lex->change,&lex->interval_list,lex->charset,
unknown's avatar
unknown committed
2783
				lex->uint_geom_type))
unknown's avatar
unknown committed
2784
	    YYABORT;
unknown's avatar
unknown committed
2785
	};
unknown's avatar
unknown committed
2786 2787

type:
2788
	int_type opt_len field_options	{ $$=$1; }
unknown's avatar
unknown committed
2789 2790
	| real_type opt_precision field_options { $$=$1; }
	| FLOAT_SYM float_options field_options { $$=FIELD_TYPE_FLOAT; }
unknown's avatar
unknown committed
2791 2792 2793 2794
	| BIT_SYM			{ Lex->length= (char*) "1";
					  $$=FIELD_TYPE_BIT; }
	| BIT_SYM '(' NUM ')'		{ Lex->length= $3.str;
					  $$=FIELD_TYPE_BIT; }
unknown's avatar
unknown committed
2795 2796
	| BOOL_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
2797 2798
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
2799
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
unknown's avatar
unknown committed
2800 2801 2802
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
2803
	| nchar '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
2804
					  $$=FIELD_TYPE_STRING;
2805
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
2806
	| nchar				{ Lex->length=(char*) "1";
unknown's avatar
unknown committed
2807
					  $$=FIELD_TYPE_STRING;
2808
					  Lex->charset=national_charset_info; }
2809
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
2810
					  Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2811
					  $$=FIELD_TYPE_STRING; }
2812 2813 2814
	| BINARY			{ Lex->length= (char*) "1";
					  Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
2815
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
2816
					  $$= MYSQL_TYPE_VARCHAR; }
unknown's avatar
unknown committed
2817
	| nvarchar '(' NUM ')'		{ Lex->length=$3.str;
2818
					  $$= MYSQL_TYPE_VARCHAR;
2819
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
2820
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
unknown's avatar
unknown committed
2821
					  Lex->charset=&my_charset_bin;
2822
					  $$= MYSQL_TYPE_VARCHAR; }
2823
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
unknown's avatar
unknown committed
2824 2825
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
2826
	| TIMESTAMP opt_len
2827
	  {
2828
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
2829 2830
	      $$=FIELD_TYPE_DATETIME;
	    else
2831 2832 2833 2834 2835
            {
              /* 
                Unlike other types TIMESTAMP fields are NOT NULL by default.
              */
              Lex->type|= NOT_NULL_FLAG;
2836
	      $$=FIELD_TYPE_TIMESTAMP;
2837
            }
2838
	   }
unknown's avatar
unknown committed
2839
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
unknown's avatar
unknown committed
2840
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2841
					  $$=FIELD_TYPE_TINY_BLOB; }
unknown's avatar
unknown committed
2842
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2843
					  $$=FIELD_TYPE_BLOB; }
unknown's avatar
unknown committed
2844 2845
	| spatial_type
          {
unknown's avatar
unknown committed
2846
#ifdef HAVE_SPATIAL
unknown's avatar
unknown committed
2847 2848 2849
            Lex->charset=&my_charset_bin;
            Lex->uint_geom_type= (uint)$1;
            $$=FIELD_TYPE_GEOMETRY;
unknown's avatar
unknown committed
2850
#else
unknown's avatar
unknown committed
2851
            my_error(ER_FEATURE_DISABLED, MYF(0),
2852
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
2853
            YYABORT;
unknown's avatar
unknown committed
2854
#endif
unknown's avatar
unknown committed
2855
          }
unknown's avatar
unknown committed
2856
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2857
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
2858
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2859
					  $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
2860
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2861
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
2862 2863
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
2864
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
2865 2866
	| MEDIUMTEXT opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| LONGTEXT opt_binary		{ $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
2867
	| DECIMAL_SYM float_options field_options
unknown's avatar
unknown committed
2868
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
unknown's avatar
unknown committed
2869
	| NUMERIC_SYM float_options field_options
unknown's avatar
unknown committed
2870
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
2871
	| FIXED_SYM float_options field_options
unknown's avatar
unknown committed
2872
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
2873
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
2874
	  { $$=FIELD_TYPE_ENUM; }
2875
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
2876
	  { $$=FIELD_TYPE_SET; }
2877
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
2878 2879 2880 2881 2882 2883
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
2884
	;
unknown's avatar
unknown committed
2885

unknown's avatar
unknown committed
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
	| POINT_SYM           { $$= Field::GEOM_POINT; }
	| MULTIPOINT          { $$= Field::GEOM_MULTIPOINT; }
	| LINESTRING          { $$= Field::GEOM_LINESTRING; }
	| MULTILINESTRING     { $$= Field::GEOM_MULTILINESTRING; }
	| POLYGON             { $$= Field::GEOM_POLYGON; }
	| MULTIPOLYGON        { $$= Field::GEOM_MULTIPOLYGON; }
	;

unknown's avatar
unknown committed
2897 2898
char:
	CHAR_SYM {}
unknown's avatar
unknown committed
2899 2900 2901 2902 2903 2904
	;

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

unknown's avatar
unknown committed
2906 2907 2908
varchar:
	char VARYING {}
	| VARCHAR {}
unknown's avatar
unknown committed
2909 2910 2911 2912
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
unknown's avatar
unknown committed
2913
	| NVARCHAR_SYM {}
unknown's avatar
unknown committed
2914 2915 2916 2917
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
unknown's avatar
unknown committed
2918 2919 2920 2921 2922 2923

int_type:
	INT_SYM		{ $$=FIELD_TYPE_LONG; }
	| TINYINT	{ $$=FIELD_TYPE_TINY; }
	| SMALLINT	{ $$=FIELD_TYPE_SHORT; }
	| MEDIUMINT	{ $$=FIELD_TYPE_INT24; }
unknown's avatar
unknown committed
2924
	| BIGINT	{ $$=FIELD_TYPE_LONGLONG; };
unknown's avatar
unknown committed
2925 2926

real_type:
2927
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
unknown's avatar
unknown committed
2928 2929
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
unknown's avatar
unknown committed
2930
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
unknown's avatar
unknown committed
2931 2932 2933


float_options:
unknown's avatar
unknown committed
2934 2935
        /* empty */		{ Lex->dec=Lex->length= (char*)0; }
        | '(' NUM ')'		{ Lex->length=$2.str; Lex->dec= (char*)0; }
unknown's avatar
unknown committed
2936
	| precision		{};
unknown's avatar
unknown committed
2937 2938 2939 2940 2941 2942

precision:
	'(' NUM ',' NUM ')'
	{
	  LEX *lex=Lex;
	  lex->length=$2.str; lex->dec=$4.str;
unknown's avatar
unknown committed
2943
	};
unknown's avatar
unknown committed
2944 2945 2946

field_options:
	/* empty */		{}
unknown's avatar
unknown committed
2947
	| field_opt_list	{};
unknown's avatar
unknown committed
2948 2949 2950

field_opt_list:
	field_opt_list field_option {}
unknown's avatar
unknown committed
2951
	| field_option {};
unknown's avatar
unknown committed
2952 2953

field_option:
unknown's avatar
unknown committed
2954
	SIGNED_SYM	{}
2955
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
unknown's avatar
unknown committed
2956
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
unknown's avatar
unknown committed
2957 2958

opt_len:
2959 2960
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
unknown's avatar
unknown committed
2961 2962 2963

opt_precision:
	/* empty */	{}
unknown's avatar
unknown committed
2964
	| precision	{};
unknown's avatar
unknown committed
2965 2966 2967

opt_attribute:
	/* empty */ {}
unknown's avatar
unknown committed
2968
	| opt_attribute_list {};
unknown's avatar
unknown committed
2969 2970 2971

opt_attribute_list:
	opt_attribute_list attribute {}
unknown's avatar
unknown committed
2972
	| attribute;
unknown's avatar
unknown committed
2973 2974 2975

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
2976
	| not NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
2977 2978 2979
	| 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
2980
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
2981
	| SERIAL_SYM DEFAULT VALUE_SYM
2982 2983 2984
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
2985
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2986 2987 2988 2989 2990
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
2991
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2992 2993 2994 2995 2996
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
2997
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
2998 2999 3000 3001 3002
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
3003
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3004
	  }
3005
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= $2; }
3006
	| BINARY { Lex->type|= BINCMP_FLAG; }
unknown's avatar
unknown committed
3007 3008
	| COLLATE_SYM collation_name
	  {
unknown's avatar
unknown committed
3009
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
unknown's avatar
unknown committed
3010
	    {
3011 3012
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $2->name,Lex->charset->csname);
unknown's avatar
unknown committed
3013 3014 3015 3016 3017 3018 3019 3020
	      YYABORT;
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
unknown's avatar
unknown committed
3021

3022 3023 3024 3025 3026
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

3027 3028 3029 3030
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
unknown's avatar
unknown committed
3031

3032
charset_name:
3033
	ident_or_text
3034
	{
unknown's avatar
unknown committed
3035
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
3036
	  {
3037
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
3038 3039
	    YYABORT;
	  }
unknown's avatar
unknown committed
3040 3041 3042
	}
	| BINARY { $$= &my_charset_bin; }
	;
3043

3044 3045 3046
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
unknown's avatar
unknown committed
3047

unknown's avatar
unknown committed
3048 3049 3050 3051 3052 3053 3054

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)))
	  {
3055
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
unknown's avatar
unknown committed
3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
	    YYABORT;
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

3066
collation_name:
3067
	ident_or_text
3068 3069 3070
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
3071
	    my_error(ER_UNKNOWN_COLLATION, MYF(0), $1.str);
3072 3073 3074 3075
	    YYABORT;
	  }
	};

3076 3077
opt_collate:
	/* empty */	{ $$=NULL; }
unknown's avatar
unknown committed
3078
	| COLLATE_SYM collation_name_or_default { $$=$2; }
3079 3080
	;

3081 3082 3083 3084
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

3085 3086 3087 3088
opt_default:
	/* empty */	{}
	| DEFAULT	{};

unknown's avatar
unknown committed
3089
opt_binary:
3090
	/* empty */			{ Lex->charset=NULL; }
unknown's avatar
unknown committed
3091 3092
	| ASCII_SYM			{ Lex->charset=&my_charset_latin1; }
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
unknown's avatar
unknown committed
3093 3094
	| UNICODE_SYM
	{
unknown's avatar
unknown committed
3095 3096
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
unknown's avatar
unknown committed
3097
	  {
unknown's avatar
unknown committed
3098
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
unknown's avatar
unknown committed
3099 3100 3101
	    YYABORT;
	  }
	}
3102
	| charset charset_name	{ Lex->charset=$2; } ;
unknown's avatar
unknown committed
3103

unknown's avatar
unknown committed
3104 3105 3106
opt_primary:
	/* empty */
	| PRIMARY_SYM
3107
	;
unknown's avatar
unknown committed
3108

unknown's avatar
unknown committed
3109
references:
3110 3111
	REFERENCES table_ident
	{
3112
	  LEX *lex=Lex;
3113 3114 3115 3116 3117 3118
	  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
3119
	};
3120

3121
opt_ref_list:
unknown's avatar
unknown committed
3122
	/* empty */ opt_on_delete {}
unknown's avatar
unknown committed
3123
	| '(' ref_list ')' opt_on_delete {};
3124 3125 3126

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

unknown's avatar
unknown committed
3129 3130 3131

opt_on_delete:
	/* empty */ {}
unknown's avatar
unknown committed
3132
	| opt_on_delete_list {};
unknown's avatar
unknown committed
3133 3134 3135

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
unknown's avatar
unknown committed
3136
	| opt_on_delete_item {};
unknown's avatar
unknown committed
3137 3138

opt_on_delete_item:
3139 3140 3141 3142
	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
3143
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
unknown's avatar
unknown committed
3144 3145

delete_option:
3146 3147 3148 3149
	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
3150
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
unknown's avatar
unknown committed
3151 3152

key_type:
3153
	key_or_index			    { $$= Key::MULTIPLE; }
unknown's avatar
SCRUM  
unknown committed
3154 3155
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
unknown's avatar
unknown committed
3156 3157 3158 3159
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
3160 3161
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3162 3163 3164
	    YYABORT;
#endif
	  };
3165 3166 3167

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
unknown's avatar
SCRUM  
unknown committed
3168
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
unknown's avatar
unknown committed
3169 3170 3171

key_or_index:
	KEY_SYM {}
unknown's avatar
unknown committed
3172
	| INDEX_SYM {};
unknown's avatar
unknown committed
3173

unknown's avatar
SCRUM  
unknown committed
3174 3175 3176 3177 3178
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

unknown's avatar
unknown committed
3179 3180
keys_or_index:
	KEYS {}
unknown's avatar
unknown committed
3181
	| INDEX_SYM {}
unknown's avatar
unknown committed
3182
	| INDEXES {};
unknown's avatar
unknown committed
3183

3184
opt_unique_or_fulltext:
unknown's avatar
unknown committed
3185 3186
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
unknown's avatar
unknown committed
3187
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
unknown's avatar
unknown committed
3188 3189 3190 3191 3192
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
unknown's avatar
unknown committed
3193
            my_error(ER_FEATURE_DISABLED, MYF(0),
unknown's avatar
unknown committed
3194
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3195 3196 3197
	    YYABORT;
#endif
	  }
unknown's avatar
unknown committed
3198
        ;
unknown's avatar
unknown committed
3199 3200

key_alg:
3201
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
3202
	| USING opt_btree_or_rtree { $$= $2; }
unknown's avatar
unknown committed
3203
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
unknown's avatar
unknown committed
3204 3205 3206

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
unknown's avatar
unknown committed
3207 3208 3209 3210
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
unknown's avatar
unknown committed
3211
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
unknown's avatar
unknown committed
3212 3213 3214

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
unknown's avatar
unknown committed
3215
	| key_part order_dir		{ Lex->col_list.push_back($1); };
unknown's avatar
unknown committed
3216 3217 3218

key_part:
	ident			{ $$=new key_part_spec($1.str); }
3219 3220 3221 3222 3223
	| ident '(' NUM ')'	
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
3224
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
3225 3226 3227
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
unknown's avatar
unknown committed
3228 3229 3230

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

3233
opt_component:
3234 3235
        /* empty */      { $$= null_lex_str; }
        | '.' ident      { $$= $2; };
3236

unknown's avatar
unknown committed
3237 3238
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
unknown's avatar
unknown committed
3239
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
unknown's avatar
unknown committed
3240 3241 3242 3243 3244 3245 3246 3247

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
3248
	  THD *thd= YYTHD;
3249
	  LEX *lex= thd->lex;
3250 3251 3252
	  lex->sql_command= SQLCOM_ALTER_TABLE;
	  lex->name= 0;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
3253 3254
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
unknown's avatar
unknown committed
3255 3256 3257 3258
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
3259
          lex->select_lex.init_order();
3260
	  lex->select_lex.db=lex->name=0;
3261
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
unknown's avatar
unknown committed
3262
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
3263
	  lex->create_info.default_table_charset= NULL;
3264
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
3265
	  lex->alter_info.reset();
3266
	  lex->alter_info.flags= 0;
unknown's avatar
unknown committed
3267
	}
3268 3269
	alter_list
	{}
3270
	| ALTER DATABASE ident_or_empty
3271 3272 3273 3274 3275
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
          opt_create_database_options
3276 3277 3278
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
3279
	    lex->name= $3;
3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

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

3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    LEX *lex=Lex;

	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
	    lex->spname= $3;
	  }
	| ALTER algorithm VIEW_SYM table_ident
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_CREATE_VIEW;
	    lex->create_view_mode= VIEW_ALTER;
	    lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE;
	    /* first table in list is target VIEW name */
	    lex->select_lex.add_table_to_list(thd, $4, NULL, 0);
	  }
	  opt_view_list AS select_init check_option
	  {}
	;
3320

3321 3322 3323 3324
ident_or_empty:
	/* empty */  { $$= 0; }
	| ident      { $$= $1.str; };

unknown's avatar
unknown committed
3325
alter_list:
3326 3327
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
unknown's avatar
unknown committed
3328
        | alter_list_item
unknown's avatar
unknown committed
3329
	| alter_list ',' alter_list_item;
unknown's avatar
unknown committed
3330 3331

add_column:
3332
	ADD opt_column
3333 3334
	{
	  LEX *lex=Lex;
3335 3336
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
3337
	};
unknown's avatar
unknown committed
3338 3339

alter_list_item:
3340 3341 3342 3343
	add_column column_def opt_place { }
	| ADD key_def
	  {
	    Lex->alter_info.flags|= ALTER_ADD_INDEX;
3344
	  }
3345 3346 3347 3348
	| add_column '(' field_list ')'
          {
	    Lex->alter_info.flags|= ALTER_ADD_COLUMN | ALTER_ADD_INDEX;
          }
unknown's avatar
unknown committed
3349 3350 3351
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
3352
	     lex->change= $3.str;
3353
	     lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
3354
	  }
3355
          field_spec opt_place
unknown's avatar
unknown committed
3356 3357 3358
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
3359
            lex->length=lex->dec=0; lex->type=0;
3360
            lex->default_value= lex->on_update_value= 0;
3361
            lex->comment=null_lex_str;
3362
	    lex->charset= NULL;
3363
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
3364 3365 3366 3367
          }
          type opt_attribute
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
3368
            if (add_field_to_list(lex->thd,$3.str,
unknown's avatar
unknown committed
3369 3370
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
3371
                                  lex->default_value, lex->on_update_value,
3372
                                  &lex->comment,
3373
				  $3.str, &lex->interval_list, lex->charset,
unknown's avatar
unknown committed
3374
				  lex->uint_geom_type))
unknown's avatar
unknown committed
3375 3376 3377
	       YYABORT;
          }
          opt_place
unknown's avatar
unknown committed
3378
	| DROP opt_column field_ident opt_restrict
unknown's avatar
unknown committed
3379 3380
	  {
	    LEX *lex=Lex;
3381
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
3382
                                                               $3.str));
3383
	    lex->alter_info.flags|= ALTER_DROP_COLUMN;
unknown's avatar
unknown committed
3384
	  }
3385 3386 3387 3388
	| DROP FOREIGN KEY_SYM opt_ident
          {
	    Lex->alter_info.flags|= ALTER_DROP_INDEX;
          }
unknown's avatar
unknown committed
3389 3390 3391
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
3392 3393 3394
	    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
3395
	  }
unknown's avatar
unknown committed
3396
	| DROP key_or_index field_ident
unknown's avatar
unknown committed
3397 3398
	  {
	    LEX *lex=Lex;
3399 3400 3401
	    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
3402
	  }
3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414
	| 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
3415
	| ALTER opt_column field_ident SET DEFAULT signed_literal
unknown's avatar
unknown committed
3416 3417
	  {
	    LEX *lex=Lex;
3418
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,$6));
3419
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
3420
	  }
unknown's avatar
unknown committed
3421
	| ALTER opt_column field_ident DROP DEFAULT
unknown's avatar
unknown committed
3422 3423
	  {
	    LEX *lex=Lex;
3424 3425
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,
                                                                  (Item*) 0));
3426
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
3427
	  }
3428
	| RENAME opt_to table_ident
3429
	  {
3430
	    LEX *lex=Lex;
3431
	    lex->select_lex.db=$3->db.str;
3432
	    lex->name= $3->table.str;
3433 3434 3435
            if (check_table_name($3->table.str,$3->table.length) ||
                $3->db.str && check_db_name($3->db.str))
            {
3436
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3->table.str);
3437 3438
              YYABORT;
            }
3439
	    lex->alter_info.flags|= ALTER_RENAME;
3440
	  }
3441 3442 3443 3444 3445 3446 3447 3448 3449 3450
	| 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))
	    {
3451 3452
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $5->name, $4->csname);
3453 3454
	      YYABORT;
	    }
3455
	    LEX *lex= Lex;
3456
	    lex->create_info.table_charset=
unknown's avatar
unknown committed
3457 3458 3459
	      lex->create_info.default_table_charset= $5;
	    lex->create_info.used_fields|= (HA_CREATE_USED_CHARSET |
					    HA_CREATE_USED_DEFAULT_CHARSET);
3460
	    lex->alter_info.flags|= ALTER_CONVERT;
3461
	  }
3462
        | create_table_options_space_separated
3463 3464
	  {
	    LEX *lex=Lex;
3465
	    lex->alter_info.flags|= ALTER_OPTIONS;
3466
	  }
3467
	| order_clause
3468 3469
	  {
	    LEX *lex=Lex;
3470
	    lex->alter_info.flags|= ALTER_ORDER;
3471
	  };
unknown's avatar
unknown committed
3472 3473 3474

opt_column:
	/* empty */	{}
unknown's avatar
unknown committed
3475
	| COLUMN_SYM	{};
unknown's avatar
unknown committed
3476 3477

opt_ignore:
3478 3479 3480
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
unknown's avatar
unknown committed
3481 3482

opt_restrict:
3483 3484 3485 3486
	/* empty */	{ Lex->drop_mode= DROP_DEFAULT; }
	| RESTRICT	{ Lex->drop_mode= DROP_RESTRICT; }
	| CASCADE	{ Lex->drop_mode= DROP_CASCADE; }
	;
unknown's avatar
unknown committed
3487 3488 3489 3490

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
unknown's avatar
unknown committed
3491
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
unknown's avatar
unknown committed
3492 3493 3494 3495

opt_to:
	/* empty */	{}
	| TO_SYM	{}
3496
	| EQ		{}
unknown's avatar
unknown committed
3497
	| AS		{};
unknown's avatar
unknown committed
3498

unknown's avatar
unknown committed
3499
/*
3500
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
unknown's avatar
unknown committed
3501 3502
*/

unknown's avatar
unknown committed
3503
slave:
3504
	  START_SYM SLAVE slave_thread_opts
3505 3506 3507 3508 3509 3510
          {
	    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
3511
            /* If you change this code don't forget to update SLAVE START too */
3512 3513 3514
          }
          slave_until
          {}
unknown's avatar
unknown committed
3515 3516 3517 3518 3519
        | STOP_SYM SLAVE slave_thread_opts
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_STOP;
	    lex->type = 0;
unknown's avatar
unknown committed
3520
            /* If you change this code don't forget to update SLAVE STOP too */
unknown's avatar
unknown committed
3521
          }
3522 3523 3524 3525 3526
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
3527 3528 3529 3530 3531
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
3532 3533 3534 3535 3536 3537 3538 3539
	| SLAVE STOP_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_STOP;
	   lex->type = 0;
         }
        ;

unknown's avatar
unknown committed
3540

unknown's avatar
unknown committed
3541
start:
3542 3543 3544 3545 3546
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
           Lex->sql_command = SQLCOM_BEGIN;
           Lex->start_transaction_opt= $3;
        }
unknown's avatar
unknown committed
3547 3548
	;

3549 3550 3551 3552 3553 3554
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
3555
        ;
3556

unknown's avatar
unknown committed
3557
slave_thread_opts:
3558 3559
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
3560
        {}
unknown's avatar
unknown committed
3561
	;
3562 3563

slave_thread_opt_list:
unknown's avatar
unknown committed
3564
	slave_thread_opt
3565 3566
	| slave_thread_opt_list ',' slave_thread_opt
	;
3567 3568

slave_thread_opt:
3569
	/*empty*/	{}
unknown's avatar
unknown committed
3570
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
unknown's avatar
unknown committed
3571
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
unknown's avatar
unknown committed
3572
	;
3573

3574 3575 3576 3577 3578 3579 3580 3581 3582 3583
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
3584 3585
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


unknown's avatar
unknown committed
3597 3598 3599 3600 3601
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
	}
unknown's avatar
unknown committed
3602
	table_list FROM TEXT_STRING_sys
unknown's avatar
unknown committed
3603 3604
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
3605
        };
unknown's avatar
unknown committed
3606

unknown's avatar
unknown committed
3607 3608 3609 3610 3611
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
	}
unknown's avatar
unknown committed
3612
	table_list TO_SYM TEXT_STRING_sys
unknown's avatar
unknown committed
3613 3614
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
3615
        };
unknown's avatar
unknown committed
3616

3617 3618 3619 3620 3621 3622
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
3623 3624
	table_list opt_checksum_type
        {}
3625 3626
	;

3627 3628 3629 3630 3631 3632
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
3633
repair:
3634
	REPAIR opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3635
	{
unknown's avatar
unknown committed
3636 3637
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
3638
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3639
	   lex->check_opt.init();
unknown's avatar
unknown committed
3640
	}
3641 3642 3643
	table_list opt_mi_repair_type
	{}
	;
unknown's avatar
unknown committed
3644

unknown's avatar
unknown committed
3645
opt_mi_repair_type:
unknown's avatar
unknown committed
3646
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
3647
	| mi_repair_types {};
unknown's avatar
unknown committed
3648

unknown's avatar
unknown committed
3649 3650
mi_repair_types:
	mi_repair_type {}
unknown's avatar
unknown committed
3651
	| mi_repair_type mi_repair_types {};
3652

unknown's avatar
unknown committed
3653
mi_repair_type:
unknown's avatar
unknown committed
3654
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
3655
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
unknown's avatar
unknown committed
3656
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
unknown's avatar
unknown committed
3657 3658

analyze:
3659
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3660
	{
unknown's avatar
unknown committed
3661 3662
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
3663
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3664
	   lex->check_opt.init();
unknown's avatar
unknown committed
3665
	}
3666 3667 3668
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3669 3670

check:
unknown's avatar
unknown committed
3671
	CHECK_SYM table_or_tables
unknown's avatar
unknown committed
3672
	{
unknown's avatar
unknown committed
3673 3674 3675
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECK;
	   lex->check_opt.init();
unknown's avatar
unknown committed
3676
	}
3677 3678 3679
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3680

unknown's avatar
unknown committed
3681 3682
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
3683
	| mi_check_types {};
unknown's avatar
unknown committed
3684 3685 3686

mi_check_types:
	mi_check_type {}
unknown's avatar
unknown committed
3687
	| mi_check_type mi_check_types {};
unknown's avatar
unknown committed
3688 3689 3690 3691 3692 3693

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

unknown's avatar
unknown committed
3696
optimize:
3697
	OPTIMIZE opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3698
	{
unknown's avatar
unknown committed
3699 3700
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
unknown's avatar
unknown committed
3701
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3702
	   lex->check_opt.init();
unknown's avatar
unknown committed
3703
	}
3704 3705 3706
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3707

3708 3709 3710
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
3711
	| LOCAL_SYM  { $$= 1; }
3712 3713
	;

3714 3715 3716 3717 3718
rename:
	RENAME table_or_tables
	{
	   Lex->sql_command=SQLCOM_RENAME_TABLE;
	}
3719 3720
	table_to_table_list
	{}
3721 3722 3723 3724
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
3725
	;
3726

3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739
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;
          }
        ;

3740 3741
table_to_table_list:
	table_to_table
unknown's avatar
unknown committed
3742
	| table_to_table_list ',' table_to_table;
3743 3744 3745

table_to_table:
	table_ident TO_SYM table_ident
3746
	{
unknown's avatar
unknown committed
3747
	  LEX *lex=Lex;
unknown's avatar
unknown committed
3748
	  SELECT_LEX *sl= lex->current_select;
unknown's avatar
unknown committed
3749 3750 3751 3752
	  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))
3753
	    YYABORT;
3754
	};
3755

unknown's avatar
unknown committed
3756
keycache:
unknown's avatar
unknown committed
3757
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
unknown's avatar
unknown committed
3758 3759
        {
          LEX *lex=Lex;
3760
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
3761
	  lex->ident= $5;
unknown's avatar
unknown committed
3762 3763 3764 3765 3766 3767 3768 3769
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
3770
        table_ident cache_keys_spec
unknown's avatar
unknown committed
3771 3772 3773 3774 3775 3776
        {
          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(),
3777
                                      (List<String> *)0))
unknown's avatar
unknown committed
3778 3779 3780 3781
            YYABORT;
        }
        ;

3782 3783 3784
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
3785
	;
3786

unknown's avatar
unknown committed
3787
preload:
unknown's avatar
unknown committed
3788
	LOAD INDEX_SYM INTO CACHE_SYM
unknown's avatar
unknown committed
3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801
	{
	  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
3802
	table_ident cache_keys_spec opt_ignore_leaves
unknown's avatar
unknown committed
3803 3804 3805
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
unknown's avatar
unknown committed
3806 3807
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
unknown's avatar
unknown committed
3808 3809 3810 3811 3812 3813
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

3814
cache_keys_spec:
3815
        { Select->interval_list.empty(); }
3816 3817 3818 3819 3820 3821 3822
        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
3823

unknown's avatar
unknown committed
3824
cache_key_list_or_empty:
3825
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
3826
	| opt_key_or_index '(' key_usage_list2 ')'
3827 3828 3829 3830
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
3831 3832
	;

unknown's avatar
unknown committed
3833
opt_ignore_leaves:
unknown's avatar
unknown committed
3834 3835 3836 3837 3838
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

unknown's avatar
unknown committed
3839
/*
unknown's avatar
unknown committed
3840
  Select : retrieve data from table
unknown's avatar
unknown committed
3841 3842 3843 3844
*/


select:
3845 3846 3847 3848 3849
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
	  lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE;
unknown's avatar
unknown committed
3850 3851
	}
	;
unknown's avatar
unknown committed
3852

3853
/* Need select_init2 for subselects. */
unknown's avatar
unknown committed
3854
select_init:
3855
	SELECT_SYM select_init2
3856
	|
unknown's avatar
unknown committed
3857 3858 3859 3860
	'(' select_paren ')' union_opt;

select_paren:
	SELECT_SYM select_part2
3861
	  {
3862
	    LEX *lex= Lex;
unknown's avatar
unknown committed
3863
            SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
3864
	    if (sel->set_braces(1))
3865
	    {
3866
	      yyerror(ER(ER_SYNTAX_ERROR));
3867 3868
	      YYABORT;
	    }
3869 3870
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
3871
	  {
3872
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
3873 3874
	    YYABORT;
	  }
3875
            /* select in braces, can't contain global parameters */
unknown's avatar
unknown committed
3876 3877 3878
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
unknown's avatar
unknown committed
3879 3880
          }
	| '(' select_paren ')';
3881

3882 3883
select_init2:
	select_part2
3884
        {
3885
	  LEX *lex= Lex;
unknown's avatar
unknown committed
3886
          SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
3887
          if (lex->current_select->set_braces(0))
3888
	  {
3889
	    yyerror(ER(ER_SYNTAX_ERROR));
3890 3891
	    YYABORT;
	  }
3892 3893
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
3894
	  {
3895
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
3896 3897
	    YYABORT;
	  }
3898
	}
3899
	union_clause
3900 3901
	;

3902
select_part2:
unknown's avatar
unknown committed
3903
	{
unknown's avatar
unknown committed
3904 3905 3906
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
	  lex->lock_option= TL_READ;
unknown's avatar
unknown committed
3907 3908
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
3909
	  lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
3910 3911 3912
	}
	select_options select_item_list
	{
3913
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
3914
	}
unknown's avatar
unknown committed
3915
	select_into select_lock_type;
3916

unknown's avatar
unknown committed
3917
select_into:
3918 3919
	opt_limit_clause {}
        | into
unknown's avatar
unknown committed
3920
	| select_from
3921 3922
	| into select_from
	| select_from into;
unknown's avatar
unknown committed
3923 3924

select_from:
3925 3926
	  FROM join_table_list where_clause group_clause having_clause
	       opt_order_clause opt_limit_clause procedure_clause
unknown's avatar
unknown committed
3927 3928 3929 3930 3931
        | FROM DUAL_SYM opt_limit_clause
          /* 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 :) */
3932
	;
unknown's avatar
unknown committed
3933 3934 3935

select_options:
	/* empty*/
unknown's avatar
unknown committed
3936
	| select_option_list;
unknown's avatar
unknown committed
3937 3938 3939

select_option_list:
	select_option_list select_option
unknown's avatar
unknown committed
3940
	| select_option;
unknown's avatar
unknown committed
3941 3942

select_option:
3943
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
3944 3945 3946 3947 3948 3949
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
3950 3951 3952
	| DISTINCT	{ Select->options|= SELECT_DISTINCT; }
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964
	| 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;
	  }
3965
	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
3966 3967 3968 3969
	| SQL_CACHE_SYM
	  {
	    Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
	  }
3970 3971
	| ALL		{}
	;
unknown's avatar
unknown committed
3972

unknown's avatar
unknown committed
3973 3974 3975
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
3976 3977
	  {
	    LEX *lex=Lex;
3978
	    lex->current_select->set_lock_for_tables(TL_WRITE);
3979
	    lex->safe_to_cache_query=0;
3980
	  }
unknown's avatar
unknown committed
3981
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
3982 3983
	  {
	    LEX *lex=Lex;
3984 3985
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
3986
	    lex->safe_to_cache_query=0;
3987 3988
	  }
	;
unknown's avatar
unknown committed
3989

unknown's avatar
unknown committed
3990 3991 3992 3993 3994
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
3995 3996
	    THD *thd= YYTHD;
	    if (add_item_to_list(thd, new Item_field(NULL, NULL, "*")))
unknown's avatar
unknown committed
3997
	      YYABORT;
3998
	    (thd->lex->current_select->with_wild)++;
unknown's avatar
unknown committed
3999
	  };
unknown's avatar
unknown committed
4000 4001 4002 4003 4004


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
unknown's avatar
unknown committed
4005
	    if (add_item_to_list(YYTHD, $2))
unknown's avatar
unknown committed
4006 4007
	      YYABORT;
	    if ($4.str)
unknown's avatar
unknown committed
4008
	      $2->set_name($4.str,$4.length,system_charset_info);
4009 4010 4011 4012 4013 4014
	    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
4015
	  };
unknown's avatar
unknown committed
4016 4017

remember_name:
unknown's avatar
unknown committed
4018
	{ $$=(char*) Lex->tok_start; };
unknown's avatar
unknown committed
4019 4020

remember_end:
unknown's avatar
unknown committed
4021
	{ $$=(char*) Lex->tok_end; };
unknown's avatar
unknown committed
4022 4023 4024

select_item2:
	table_wild	{ $$=$1; } /* table.* */
unknown's avatar
unknown committed
4025
	| expr		{ $$=$1; };
unknown's avatar
unknown committed
4026 4027

select_alias:
4028
	/* empty */		{ $$=null_lex_str;}
unknown's avatar
unknown committed
4029 4030 4031 4032 4033
	| AS ident		{ $$=$2; }
	| AS TEXT_STRING_sys	{ $$=$2; }
	| ident			{ $$=$1; }
	| TEXT_STRING_sys	{ $$=$1; }
	;
unknown's avatar
unknown committed
4034 4035 4036

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

unknown's avatar
unknown committed
4039
/* all possible expressions */
unknown's avatar
SCRUM  
unknown committed
4040
expr:	
unknown's avatar
unknown committed
4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062
	  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); }
        ;
4063 4064

bool_term:
unknown's avatar
unknown committed
4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087
	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); }
        ;
4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104

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); }
4105 4106 4107 4108 4109
	| 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); }
4110 4111 4112 4113 4114 4115
	| predicate ;

predicate:
	 bit_expr IN_SYM '(' expr_list ')'
	  { $4->push_front($1); $$= new Item_func_in(*$4); }
	| bit_expr not IN_SYM '(' expr_list ')'
4116
	  { $5->push_front($1); $$= negate_expression(YYTHD, new Item_func_in(*$5)); }
4117 4118 4119
        | bit_expr IN_SYM in_subselect
	  { $$= new Item_in_subselect($1, $3); }
	| bit_expr not IN_SYM in_subselect
4120
          { $$= negate_expression(YYTHD, new Item_in_subselect($1, $4)); }
4121 4122 4123 4124
	| 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
	  { $$= negate_expression(YYTHD, new Item_func_between($1,$4,$6)); }
4125 4126 4127 4128 4129 4130 4131 4132 4133
	| 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
          { $$= new Item_func_like($1,$3,$4); }
	| bit_expr not LIKE simple_expr opt_escape
          { $$= new Item_func_not(new Item_func_like($1,$4,$5)); }
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
4134
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176
	| 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
4177

unknown's avatar
unknown committed
4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189
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; }
        ;

4190
interval_expr:
unknown's avatar
unknown committed
4191
         INTERVAL_SYM expr { $$=$2; }
4192 4193
        ;

unknown's avatar
unknown committed
4194 4195
simple_expr:
	simple_ident
4196
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
unknown's avatar
unknown committed
4197
	  {
unknown's avatar
unknown committed
4198 4199 4200
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
4201
                                                            YYTHD->charset()));
4202
	  }
unknown's avatar
unknown committed
4203
	| literal
unknown's avatar
unknown committed
4204
	| param_marker
unknown's avatar
unknown committed
4205
	| '@' ident_or_text SET_VAR expr
unknown's avatar
unknown committed
4206 4207
	  {
	    $$= new Item_func_set_user_var($2,$4);
4208 4209 4210
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4211
	  }
4212
	| '@' ident_or_text
unknown's avatar
unknown committed
4213 4214
	  {
	    $$= new Item_func_get_user_var($2);
4215 4216 4217
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4218
	  }
4219
	| '@' '@' opt_var_ident_type ident_or_text opt_component
unknown's avatar
unknown committed
4220
	  {
4221 4222 4223 4224 4225 4226

            if ($4.str && $5.str && check_reserved_words(&$4))
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
4227
	    if (!($$= get_system_var(YYTHD, (enum_var_type) $3, $4, $5)))
unknown's avatar
unknown committed
4228
	      YYABORT;
4229
	    Lex->variables_used= 1;
unknown's avatar
unknown committed
4230
	  }
unknown's avatar
unknown committed
4231
	| sum_expr
4232 4233 4234 4235 4236 4237
	| 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
4238
	| '(' expr ')'		{ $$= $2; }
4239 4240 4241 4242 4243
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
4244
	| ROW_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
4245
	  {
4246 4247
	    $5->push_front($3);
	    $$= new Item_row(*$5);
unknown's avatar
unknown committed
4248
	  }
unknown's avatar
unknown committed
4249
	| EXISTS exists_subselect { $$= $2; }
unknown's avatar
unknown committed
4250
	| singlerow_subselect   { $$= $1; }
unknown's avatar
unknown committed
4251
	| '{' ident expr '}'	{ $$= $3; }
4252
        | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
4253
          { $2->push_front($5);
4254 4255
            Select->add_ftfunc_to_list((Item_func_match*)
                                        ($$=new Item_func_match(*$2,$6))); }
unknown's avatar
unknown committed
4256
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
4257
	| BINARY simple_expr %prec NEG
unknown's avatar
unknown committed
4258
	  {
unknown's avatar
unknown committed
4259
            $$= create_func_cast($2, ITEM_CAST_CHAR, -1, 0, &my_charset_bin);
unknown's avatar
unknown committed
4260
	  }
4261
	| CAST_SYM '(' expr AS cast_type ')'
4262
	  {
unknown's avatar
unknown committed
4263
            LEX *lex= Lex;
4264
	    $$= create_func_cast($3, $5,
unknown's avatar
unknown committed
4265 4266 4267
                                 lex->length ? atoi(lex->length) : -1,
                                 lex->dec ? atoi(lex->dec) : 0,
                                 lex->charset);
4268
	  }
unknown's avatar
unknown committed
4269
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
4270
	  { $$= new Item_func_case(* $4, $2, $5 ); }
4271
	| CONVERT_SYM '(' expr ',' cast_type ')'
4272 4273 4274
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
unknown's avatar
unknown committed
4275
                                 Lex->dec ? atoi(Lex->dec) : 0,
4276 4277
				 Lex->charset);
	  }
4278 4279
	| CONVERT_SYM '(' expr USING charset_name ')'
	  { $$= new Item_func_conv_charset($3,$5); }
unknown's avatar
SCRUM  
unknown committed
4280 4281
	| DEFAULT '(' simple_ident ')'
	  { $$= new Item_default_value($3); }
unknown's avatar
unknown committed
4282 4283
	| VALUES '(' simple_ident ')'
	  { $$= new Item_insert_value($3); }
unknown's avatar
unknown committed
4284
	| FUNC_ARG0 '(' ')'
unknown's avatar
unknown committed
4285 4286 4287
	  {
	    if (!$1.symbol->create_func)
	    {
4288 4289 4290
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
4291 4292 4293 4294
	      YYABORT;
	    }
	    $$= ((Item*(*)(void))($1.symbol->create_func))();
	  }
unknown's avatar
unknown committed
4295
	| FUNC_ARG1 '(' expr ')'
unknown's avatar
unknown committed
4296 4297 4298
	  {
	    if (!$1.symbol->create_func)
	    {
4299 4300 4301
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
4302 4303 4304 4305
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);
	  }
unknown's avatar
unknown committed
4306
	| FUNC_ARG2 '(' expr ',' expr ')'
unknown's avatar
unknown committed
4307 4308 4309
	  {
	    if (!$1.symbol->create_func)
	    {
4310 4311 4312
	      my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
4313 4314 4315 4316
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);
	  }
unknown's avatar
unknown committed
4317
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
unknown's avatar
unknown committed
4318 4319 4320
	  {
	    if (!$1.symbol->create_func)
	    {
4321 4322 4323
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
4324 4325 4326 4327
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*,Item*))($1.symbol->create_func))($3,$5,$7);
	  }
unknown's avatar
unknown committed
4328 4329 4330 4331
	| 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); }
4332 4333
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
unknown's avatar
unknown committed
4334 4335 4336 4337 4338 4339
	| 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); }
unknown's avatar
unknown committed
4340 4341
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
unknown's avatar
unknown committed
4342 4343
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
unknown's avatar
unknown committed
4344 4345
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
unknown's avatar
unknown committed
4346 4347 4348
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
4349
	  { $5->push_front($3); $$= new Item_func_concat_ws(*$5); }
4350 4351
	| CONVERT_TZ_SYM '(' expr ',' expr ',' expr ')'
	  {
4352 4353
            if (Lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
4354 4355
	    $$= new Item_func_convert_tz($3, $5, $7);
	  }
unknown's avatar
unknown committed
4356
	| CURDATE optional_braces
4357
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
4358
	| CURTIME optional_braces
4359
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
4360
	| CURTIME '(' expr ')'
4361
	  {
4362
	    $$= new Item_func_curtime_local($3);
4363
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4364
	  }
unknown's avatar
unknown committed
4365
	| CURRENT_USER optional_braces
4366
          { $$= create_func_current_user(); }
4367 4368 4369 4370
	| 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
4371
	| DATABASE '(' ')'
4372
	  {
unknown's avatar
unknown committed
4373
	    $$= new Item_func_database();
4374
            Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4375
	  }
unknown's avatar
unknown committed
4376
	| DATE_SYM '(' expr ')'
unknown's avatar
unknown committed
4377
	  { $$= new Item_date_typecast($3); }
unknown's avatar
unknown committed
4378 4379
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
unknown's avatar
unknown committed
4380
	| ELT_FUNC '(' expr ',' expr_list ')'
4381
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
unknown's avatar
unknown committed
4382 4383
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
unknown's avatar
unknown committed
4384 4385 4386
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
4387
	    Lex->uncacheable(UNCACHEABLE_RAND);
unknown's avatar
unknown committed
4388
	  }
unknown's avatar
unknown committed
4389
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
4390
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
4391
	  { $$= new Item_func_decode($3,$5.str); }
4392
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
4393
	 { $$= new Item_func_encode($3,$5.str); }
unknown's avatar
unknown committed
4394
	| DES_DECRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
4395
        { $$= new Item_func_des_decrypt($3); }
unknown's avatar
unknown committed
4396
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
4397
        { $$= new Item_func_des_decrypt($3,$5); }
unknown's avatar
unknown committed
4398
	| DES_ENCRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
4399
        { $$= new Item_func_des_encrypt($3); }
unknown's avatar
unknown committed
4400
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
4401
        { $$= new Item_func_des_encrypt($3,$5); }
unknown's avatar
unknown committed
4402 4403 4404 4405 4406 4407
	| 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); }
4408 4409
	| FALSE_SYM
	  { $$= new Item_int((char*) "FALSE",0,1); }
unknown's avatar
unknown committed
4410 4411 4412 4413 4414 4415
	| 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
4416
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
unknown's avatar
unknown committed
4417 4418
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
4419
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
unknown's avatar
unknown committed
4420 4421 4422 4423 4424
	| geometry_function
	  {
#ifdef HAVE_SPATIAL
	    $$= $1;
#else
4425 4426
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
4427 4428 4429
	    YYABORT;
#endif
	  }
4430
	| GET_FORMAT '(' date_time_type  ',' expr ')'
4431
	  { $$= new Item_func_get_format($3, $5); }
unknown's avatar
unknown committed
4432 4433 4434 4435 4436 4437
	| 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); }
4438
	| interval_expr interval '+' expr
unknown's avatar
unknown committed
4439
	  /* we cannot put interval before - */
4440 4441 4442 4443 4444
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
4445
              yyerror(ER(ER_SYNTAX_ERROR));
4446 4447 4448 4449
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
unknown's avatar
unknown committed
4450 4451
	| LAST_INSERT_ID '(' ')'
	  {
4452
	    $$= new Item_func_last_insert_id();
4453
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
4454 4455 4456
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
4457
	    $$= new Item_func_last_insert_id($3);
4458
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
4459 4460 4461 4462 4463 4464 4465
	  }
	| 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
4466
	| GREATEST_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
4467 4468 4469
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
4470
	| LOG_SYM '(' expr ')'
unknown's avatar
unknown committed
4471
	  { $$= new Item_func_log($3); }
4472
	| LOG_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
4473
	  { $$= new Item_func_log($3, $5); }
4474
	| MASTER_POS_WAIT '(' expr ',' expr ')'
unknown's avatar
unknown committed
4475
	  {
4476
	    $$= new Item_master_pos_wait($3, $5);
unknown's avatar
unknown committed
4477
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4478
		  }
4479
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
unknown's avatar
unknown committed
4480
	  {
4481
	    $$= new Item_master_pos_wait($3, $5, $7);
unknown's avatar
unknown committed
4482
	    Lex->safe_to_cache_query=0;
4483
	  }
unknown's avatar
unknown committed
4484 4485
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
unknown's avatar
unknown committed
4486 4487
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
4488 4489
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
unknown's avatar
unknown committed
4490 4491 4492
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
	| NOW_SYM optional_braces
4493
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
4494
	| NOW_SYM '(' expr ')'
4495
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
4496
	| PASSWORD '(' expr ')'
4497
	  {
4498 4499
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
unknown's avatar
unknown committed
4500 4501 4502 4503
	      (Item *) new Item_func_password($3);
	  }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new Item_func_old_password($3); }
4504
	| POSITION_SYM '(' bit_expr IN_SYM expr ')'
unknown's avatar
unknown committed
4505
	  { $$ = new Item_func_locate($5,$3); }
4506 4507
	| QUARTER_SYM '(' expr ')'
	  { $$ = new Item_func_quarter($3); }
unknown's avatar
unknown committed
4508
	| RAND '(' expr ')'
4509
	  { $$= new Item_func_rand($3); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
4510
	| RAND '(' ')'
4511
	  { $$= new Item_func_rand(); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
4512 4513 4514 4515 4516 4517 4518
	| 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); }
4519 4520 4521 4522 4523
	| ROW_COUNT_SYM '(' ')'
	  {
	    $$= new Item_func_row_count();
	    Lex->safe_to_cache_query= 0;
	  }
unknown's avatar
unknown committed
4524 4525 4526 4527
	| 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
4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539
	| 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); }
unknown's avatar
unknown committed
4540
	| TIME_SYM '(' expr ')'
unknown's avatar
unknown committed
4541 4542 4543
	  { $$= new Item_time_typecast($3); }
	| TIMESTAMP '(' expr ')'
	  { $$= new Item_datetime_typecast($3); }
unknown's avatar
unknown committed
4544
	| TIMESTAMP '(' expr ',' expr ')'
unknown's avatar
unknown committed
4545
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
4546 4547 4548 4549
	| 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
4550
	| TRIM '(' expr ')'
4551 4552
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
unknown's avatar
unknown committed
4553
	  { $$= new Item_func_ltrim($6,$4); }
4554
	| TRIM '(' TRAILING expr FROM expr ')'
unknown's avatar
unknown committed
4555
	  { $$= new Item_func_rtrim($6,$4); }
4556
	| TRIM '(' BOTH expr FROM expr ')'
unknown's avatar
unknown committed
4557
	  { $$= new Item_func_trim($6,$4); }
4558 4559 4560 4561 4562 4563
	| 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
4564 4565
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
4566 4567
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
4568 4569 4570
	| TRUE_SYM
	  { $$= new Item_int((char*) "TRUE",1,1); }
	| ident '.' ident '(' udf_expr_list ')'
unknown's avatar
unknown committed
4571
	  {
4572 4573 4574
	    sp_name *name= new sp_name($1, $3);

	    name->init_qname(YYTHD);
4575
	    sp_add_to_hash(&Lex->spfuns, name);
4576 4577
	    if ($5)
	      $$= new Item_func_sp(name, *$5);
unknown's avatar
unknown committed
4578
	    else
4579
	      $$= new Item_func_sp(name);
unknown's avatar
unknown committed
4580
	  }
4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636
	| IDENT_sys '(' udf_expr_list ')'
          {
#ifdef HAVE_DLOPEN
            udf_func *udf;

            if (using_udf_functions && (udf=find_udf($1.str, $1.length)))
            {
              switch (udf->returns) {
              case STRING_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_str(udf, *$3);
                  else
                    $$ = new Item_func_udf_str(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_str(udf, *$3);
                  else
                    $$ = new Item_sum_udf_str(udf);
                }
                break;
              case REAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_float(udf, *$3);
                  else
                    $$ = new Item_func_udf_float(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_float(udf, *$3);
                  else
                    $$ = new Item_sum_udf_float(udf);
                }
                break;
              case INT_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_int(udf, *$3);
                  else
                    $$ = new Item_func_udf_int(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_int(udf, *$3);
                  else
                    $$ = new Item_sum_udf_int(udf);
                }
                break;
unknown's avatar
unknown committed
4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652
              case DECIMAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_decimal(udf, *$3);
                  else
                    $$ = new Item_func_udf_decimal(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_decimal(udf, *$3);
                  else
                    $$ = new Item_sum_udf_decimal(udf);
                }
                break;
4653 4654 4655 4656 4657 4658 4659 4660 4661
              default:
                YYABORT;
              }
            }
            else
#endif /* HAVE_DLOPEN */
            {
              sp_name *name= sp_name_current_db_new(YYTHD, $1);

4662
              sp_add_to_hash(&Lex->spfuns, name);
4663 4664 4665 4666 4667 4668
              if ($3)
                $$= new Item_func_sp(name, *$3);
              else
                $$= new Item_func_sp(name);
	    }
          }
unknown's avatar
unknown committed
4669
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
4670
	  {
unknown's avatar
unknown committed
4671 4672
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
unknown's avatar
unknown committed
4673
	| UNIX_TIMESTAMP '(' ')'
unknown's avatar
unknown committed
4674 4675
	  {
	    $$= new Item_func_unix_timestamp();
4676
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4677
	  }
unknown's avatar
unknown committed
4678 4679 4680
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
4681
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
4682 4683 4684 4685 4686 4687
	| 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
4688
	| WEEK_SYM '(' expr ')'
unknown's avatar
unknown committed
4689
	  {
4690
            $$= new Item_func_week($3,new Item_int((char*) "0",
unknown's avatar
unknown committed
4691
				   YYTHD->variables.default_week_format,1));
4692
          }
unknown's avatar
unknown committed
4693 4694 4695 4696 4697 4698 4699 4700 4701
	| WEEK_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_week($3,$5); }
	| YEAR_SYM '(' expr ')'
	  { $$= new Item_func_year($3); }
	| YEARWEEK '(' expr ')'
	  { $$= new Item_func_yearweek($3,new Item_int((char*) "0",0,1)); }
	| YEARWEEK '(' expr ',' expr ')'
	  { $$= new Item_func_yearweek($3, $5); }
	| BENCHMARK_SYM '(' ULONG_NUM ',' expr ')'
4702
	  {
unknown's avatar
unknown committed
4703
	    $$=new Item_func_benchmark($3,$5);
4704
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
4705
	  }
unknown's avatar
unknown committed
4706
	| EXTRACT_SYM '(' interval FROM expr ')'
unknown's avatar
unknown committed
4707
	{ $$=new Item_extract( $3, $5); };
unknown's avatar
unknown committed
4708

unknown's avatar
unknown committed
4709
geometry_function:
unknown's avatar
unknown committed
4710 4711 4712
	  CONTAINS_SYM '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_spatial_rel($3, $5, Item_func::SP_CONTAINS_FUNC)); }
	| GEOMFROMTEXT '(' expr ')'
unknown's avatar
unknown committed
4713 4714 4715 4716 4717 4718 4719 4720 4721
	  { $$= 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,
4722 4723
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
unknown's avatar
unknown committed
4724 4725
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4726
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
unknown's avatar
unknown committed
4727 4728
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
4729
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743
 	| 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,
4744
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
unknown's avatar
unknown committed
4745 4746
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4747
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
unknown's avatar
unknown committed
4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759
	| 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,
4760
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
4761 4762 4763 4764 4765 4766 4767 4768 4769 4770
 	| 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)); }
	;

4771 4772 4773 4774 4775 4776
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
4777
udf_expr_list:
4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808
	/* empty */	 { $$= NULL; }
	| udf_expr_list2 { $$= $1;}
	;

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

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

udf_expr:
	remember_name expr remember_end select_alias
	{
	  if ($4.str)
	    $2->set_name($4.str,$4.length,system_charset_info);
	  else
	    $2->set_name($1,(uint) ($3 - $1), YYTHD->charset());
	  $$= $2;
	}
	;
unknown's avatar
unknown committed
4809 4810 4811 4812

sum_expr:
	AVG_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_avg($3); }
4813 4814
	| AVG_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_avg_distinct($4); }
unknown's avatar
unknown committed
4815 4816 4817 4818
	| BIT_AND  '(' in_sum_expr ')'
	  { $$=new Item_sum_and($3); }
	| BIT_OR  '(' in_sum_expr ')'
	  { $$=new Item_sum_or($3); }
4819 4820
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
unknown's avatar
unknown committed
4821
	| COUNT_SYM '(' opt_all '*' ')'
unknown's avatar
unknown committed
4822 4823 4824
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
4825
	| COUNT_SYM '(' DISTINCT
unknown's avatar
unknown committed
4826
	  { Select->in_sum_expr++; }
4827
	   expr_list
unknown's avatar
unknown committed
4828
	  { Select->in_sum_expr--; }
4829 4830
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
unknown's avatar
unknown committed
4831 4832 4833 4834
	| 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); }
4835 4836 4837 4838 4839 4840 4841
/*
   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
4842 4843
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
4844 4845
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
unknown's avatar
unknown committed
4846
	| STD_SYM '(' in_sum_expr ')'
4847
	  { $$=new Item_sum_std($3, 0); }
unknown's avatar
unknown committed
4848
	| VARIANCE_SYM '(' in_sum_expr ')'
4849 4850 4851 4852 4853
	  { $$=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
4854
	| SUM_SYM '(' in_sum_expr ')'
4855
	  { $$=new Item_sum_sum($3); }
4856 4857
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
unknown's avatar
unknown committed
4858 4859 4860 4861 4862
	| GROUP_CONCAT_SYM '(' opt_distinct
	  { Select->in_sum_expr++; }
	  expr_list opt_gorder_clause
	  opt_gconcat_separator
	 ')'
unknown's avatar
unknown committed
4863
	  {
unknown's avatar
unknown committed
4864 4865 4866
	    Select->in_sum_expr--;
	    $$=new Item_func_group_concat($3,$5,Select->gorder_list,$7);
	    $5->empty();
4867 4868 4869 4870 4871 4872 4873
	  };

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

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

4877 4878

opt_gorder_clause:
4879 4880
	  /* empty */
	  {
unknown's avatar
unknown committed
4881
            Select->gorder_list = NULL;
4882 4883 4884
	  }
	| order_clause
          {
unknown's avatar
unknown committed
4885 4886 4887
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
4888
				     sizeof(st_sql_list));
unknown's avatar
unknown committed
4889
	    select->order_list.empty();
4890
	  };
unknown's avatar
unknown committed
4891

unknown's avatar
unknown committed
4892 4893

in_sum_expr:
unknown's avatar
unknown committed
4894
	opt_all
4895 4896 4897 4898
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
4899
	    yyerror(ER(ER_SYNTAX_ERROR));
4900
	    YYABORT;
4901 4902
	  }
	}
unknown's avatar
unknown committed
4903 4904
	expr
	{
unknown's avatar
unknown committed
4905
	  Select->in_sum_expr--;
4906
	  $$= $3;
unknown's avatar
unknown committed
4907
	};
unknown's avatar
unknown committed
4908

4909
cast_type:
unknown's avatar
unknown committed
4910 4911
        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; }
4912
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; Lex->dec=0; }
unknown's avatar
unknown committed
4913 4914 4915 4916 4917 4918 4919 4920
        | 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; }
4921 4922
	;

unknown's avatar
unknown committed
4923
expr_list:
4924
	{ Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
4925
	expr_list2
unknown's avatar
unknown committed
4926
	{ $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
4927 4928

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

unknown's avatar
unknown committed
4932 4933
ident_list_arg:
          ident_list          { $$= $1; }
unknown's avatar
unknown committed
4934
        | '(' ident_list ')'  { $$= $2; };
unknown's avatar
unknown committed
4935

unknown's avatar
unknown committed
4936
ident_list:
4937
        { Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
4938
        ident_list2
unknown's avatar
unknown committed
4939
        { $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
4940 4941

ident_list2:
4942
        simple_ident { Select->expr_list.head()->push_back($1); }
unknown's avatar
unknown committed
4943
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
unknown's avatar
unknown committed
4944 4945 4946

opt_expr:
	/* empty */      { $$= NULL; }
4947
	| expr           { $$= $1; };
unknown's avatar
unknown committed
4948 4949 4950

opt_else:
	/* empty */    { $$= NULL; }
unknown's avatar
unknown committed
4951
	| ELSE expr    { $$= $2; };
unknown's avatar
unknown committed
4952 4953

when_list:
unknown's avatar
unknown committed
4954
        { Select->when_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
4955
	when_list2
unknown's avatar
unknown committed
4956
	{ $$= Select->when_list.pop(); };
unknown's avatar
unknown committed
4957 4958

when_list2:
unknown's avatar
unknown committed
4959
	expr THEN_SYM expr
unknown's avatar
unknown committed
4960
	  {
unknown's avatar
unknown committed
4961
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
4962 4963
	    sel->when_list.head()->push_back($1);
	    sel->when_list.head()->push_back($3);
unknown's avatar
unknown committed
4964
	}
unknown's avatar
unknown committed
4965
	| when_list2 WHEN_SYM expr THEN_SYM expr
unknown's avatar
unknown committed
4966
	  {
unknown's avatar
unknown committed
4967
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
4968 4969
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
unknown's avatar
unknown committed
4970
	  };
unknown's avatar
unknown committed
4971

unknown's avatar
unknown committed
4972
/* Warning - may return NULL in case of incomplete SELECT */
4973 4974 4975
table_ref:
        table_factor            { $$=$1; }
        | join_table            { $$=$1; }
4976
          {
4977 4978 4979
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
4980
          }
4981 4982
        ;

unknown's avatar
unknown committed
4983
join_table_list:
unknown's avatar
unknown committed
4984 4985 4986 4987 4988
	derived_table_list		{ TEST_ASSERT($$=$1); }
	;
        
/* Warning - may return NULL in case of incomplete SELECT */
derived_table_list:
4989
        table_ref { $$=$1; }
unknown's avatar
unknown committed
4990 4991 4992 4993
        | derived_table_list ',' table_ref
          {
            TEST_ASSERT($1 && ($$=$3));
          }
4994 4995 4996
        ;

join_table:
unknown's avatar
unknown committed
4997
        table_ref normal_join table_ref { TEST_ASSERT($1 && ($$=$3)); }
4998
	| table_ref STRAIGHT_JOIN table_factor
unknown's avatar
unknown committed
4999
	  {  TEST_ASSERT($1 && ($$=$3)); $3->straight=1; }
5000
	| table_ref normal_join table_ref ON expr
unknown's avatar
unknown committed
5001
	  { TEST_ASSERT($1 && ($$=$3)); add_join_on($3,$5); }
5002
	| table_ref normal_join table_ref
unknown's avatar
unknown committed
5003
	  USING
unknown's avatar
unknown committed
5004
	  {
unknown's avatar
unknown committed
5005
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5006
            TEST_ASSERT($1 && $3);
5007
            sel->save_names_for_using_list($1, $3);
unknown's avatar
unknown committed
5008
	  }
unknown's avatar
unknown committed
5009 5010 5011
	  '(' using_list ')'
	  { add_join_on($3,$7); $$=$3; }

5012
	| table_ref LEFT opt_outer JOIN_SYM table_ref ON expr
unknown's avatar
unknown committed
5013
	  { TEST_ASSERT($1 && $5); add_join_on($5,$7); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
5014
	| table_ref LEFT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
5015
	  {
unknown's avatar
unknown committed
5016
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5017
            TEST_ASSERT($1 && $5);
5018
            sel->save_names_for_using_list($1, $5);
unknown's avatar
unknown committed
5019
	  }
unknown's avatar
unknown committed
5020
	  USING '(' using_list ')'
5021
	  { add_join_on($5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
5022
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
5023
	  {
unknown's avatar
unknown committed
5024
            TEST_ASSERT($1 && $6);
5025 5026
	    add_join_natural($1,$6);
	    $6->outer_join|=JOIN_TYPE_LEFT;
5027 5028
	    $$=$6;
	  }
5029 5030 5031
	| table_ref RIGHT opt_outer JOIN_SYM table_ref ON expr
          { 
	    LEX *lex= Lex;
unknown's avatar
unknown committed
5032
            TEST_ASSERT($1 && $5);
5033 5034 5035 5036 5037
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
            add_join_on($$, $7);
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
5038
	  {
unknown's avatar
unknown committed
5039
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5040
            TEST_ASSERT($1 && $5);
5041
            sel->save_names_for_using_list($1, $5);
unknown's avatar
unknown committed
5042
	  }
5043
	  USING '(' using_list ')'
5044 5045 5046 5047 5048 5049 5050
          { 
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
            add_join_on($$, $9);
          }
	| table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor
5051
	  {
unknown's avatar
unknown committed
5052
            TEST_ASSERT($1 && $6);
5053 5054 5055 5056
	    add_join_natural($6,$1);
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
5057
	  }
5058
	| table_ref NATURAL JOIN_SYM table_factor
unknown's avatar
unknown committed
5059
	  { TEST_ASSERT($1 && ($$=$4)); add_join_natural($1,$4); };
5060
        
unknown's avatar
unknown committed
5061 5062

normal_join:
unknown's avatar
unknown committed
5063 5064 5065 5066
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
unknown's avatar
unknown committed
5067

unknown's avatar
unknown committed
5068
/* Warning - may return NULL in case of incomplete SELECT */
5069
table_factor:
unknown's avatar
unknown committed
5070
	{
unknown's avatar
unknown committed
5071
	  SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5072
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
unknown's avatar
unknown committed
5073
	  sel->table_join_options= 0;
unknown's avatar
unknown committed
5074
	}
unknown's avatar
unknown committed
5075
        table_ident opt_table_alias opt_key_definition
unknown's avatar
unknown committed
5076
	{
unknown's avatar
unknown committed
5077
	  LEX *lex= Lex;
unknown's avatar
unknown committed
5078
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
5079
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
unknown's avatar
unknown committed
5080
					   sel->get_table_join_options(),
unknown's avatar
unknown committed
5081
					   lex->lock_option,
5082 5083
					   sel->get_use_index(),
					   sel->get_ignore_index())))
5084
	    YYABORT;
5085
          sel->add_joined_table($$);
unknown's avatar
unknown committed
5086
	}
unknown's avatar
unknown committed
5087 5088 5089
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref ON expr '}'
	  { TEST_ASSERT($3 && $7); add_join_on($7,$9); $7->outer_join|=JOIN_TYPE_LEFT; $$=$7; }
	| select_derived_init get_select_lex select_derived2
5090
          {
5091
            LEX *lex= Lex;
unknown's avatar
unknown committed
5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109
            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))
              YYABORT;            
            $$= 0;
            /* incomplete derived tables return NULL, we must be 
               nested in select_derived rule to be here. */
5110
          }
unknown's avatar
unknown committed
5111 5112 5113 5114 5115 5116 5117
	| '(' 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. */

          if (!($3 || $6) && $2->embedding && 
              !$2->embedding->nested_join->join_list.elements)
5118
          {
unknown's avatar
unknown committed
5119 5120 5121 5122 5123
            /* 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;
5124
          }
unknown's avatar
unknown committed
5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138
          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
5139

unknown's avatar
unknown committed
5140 5141 5142 5143 5144 5145 5146 5147
	      YYABORT;
            sel->add_joined_table($$);           
          }
	  else
          if ($4 || $6)
	  {
            /* simple nested joins cannot have aliases or unions */
            yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
5148
	    YYABORT;
unknown's avatar
unknown committed
5149 5150 5151 5152 5153
	  }
          else
            $$= $3;
	}
        ;
unknown's avatar
unknown committed
5154

unknown's avatar
unknown committed
5155
/* handle contents of parentheses in join expression */
5156
select_derived:
unknown's avatar
unknown committed
5157
	  get_select_lex
unknown's avatar
unknown committed
5158
	  {
unknown's avatar
unknown committed
5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172
            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 */
            
            if (!($$= $1->end_nested_join(lex->thd)) && $3)
              YYABORT;
            if (!$3 && $$)
            {
unknown's avatar
unknown committed
5173 5174
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
unknown's avatar
unknown committed
5175 5176
            }
          }
unknown's avatar
unknown committed
5177
 	;
unknown's avatar
unknown committed
5178

unknown's avatar
unknown committed
5179
select_derived2:
unknown's avatar
unknown committed
5180
        {
unknown's avatar
unknown committed
5181
	  LEX *lex= Lex;
5182
	  lex->derived_tables|= DERIVED_SUBQUERY;
unknown's avatar
unknown committed
5183 5184 5185
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
5186 5187
	  {
	    yyerror(ER(ER_SYNTAX_ERROR));
5188 5189
	    YYABORT;
	  }
5190
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
unknown's avatar
unknown committed
5191
              mysql_new_select(lex, 1))
unknown's avatar
unknown committed
5192 5193
	    YYABORT;
	  mysql_init_select(lex);
5194
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
5195
	  lex->current_select->parsing_place= SELECT_LIST;
5196 5197 5198
	}
        select_options select_item_list
	{
5199
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
5200
	}
unknown's avatar
unknown committed
5201
	opt_select_from
5202
        ;
unknown's avatar
unknown committed
5203

unknown's avatar
unknown committed
5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226
get_select_lex:
	/* Empty */ { $$= Select; }
        ;

select_derived_init:
          SELECT_SYM
          {
            LEX *lex= Lex;
            SELECT_LEX *sel= lex->current_select;        
            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
5227 5228
opt_outer:
	/* empty */	{}
unknown's avatar
unknown committed
5229
	| OUTER		{};
unknown's avatar
unknown committed
5230 5231 5232 5233

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
unknown's avatar
unknown committed
5234
          {
unknown's avatar
unknown committed
5235
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5236 5237 5238
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
5239 5240
	| FORCE_SYM key_usage_list
          {
unknown's avatar
unknown committed
5241
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5242 5243 5244 5245
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
unknown's avatar
unknown committed
5246
	| IGNORE_SYM key_usage_list
unknown's avatar
unknown committed
5247
	  {
unknown's avatar
unknown committed
5248
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5249 5250
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
unknown's avatar
unknown committed
5251
	  };
unknown's avatar
unknown committed
5252 5253

key_usage_list:
unknown's avatar
unknown committed
5254
	key_or_index { Select->interval_list.empty(); }
unknown's avatar
unknown committed
5255
        '(' key_list_or_empty ')'
unknown's avatar
unknown committed
5256
        { $$= &Select->interval_list; }
unknown's avatar
unknown committed
5257 5258 5259 5260 5261 5262
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
unknown's avatar
unknown committed
5263 5264 5265

key_usage_list2:
	key_usage_list2 ',' ident
unknown's avatar
unknown committed
5266
        { Select->
unknown's avatar
unknown committed
5267
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $3.str, $3.length,
5268
				    system_charset_info)); }
unknown's avatar
unknown committed
5269
	| ident
unknown's avatar
unknown committed
5270
        { Select->
unknown's avatar
unknown committed
5271
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $1.str, $1.length,
5272
				    system_charset_info)); }
unknown's avatar
unknown committed
5273
	| PRIMARY_SYM
unknown's avatar
unknown committed
5274
        { Select->
unknown's avatar
unknown committed
5275
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
5276
				    system_charset_info)); };
unknown's avatar
unknown committed
5277 5278 5279

using_list:
	ident
unknown's avatar
unknown committed
5280
	  {
unknown's avatar
unknown committed
5281
	    SELECT_LEX *sel= Select;
5282 5283 5284 5285
	    if (!($$= new Item_func_eq(new Item_field(sel->db1, sel->table1,
						      $1.str),
				       new Item_field(sel->db2, sel->table2,
						      $1.str))))
unknown's avatar
unknown committed
5286 5287 5288 5289
	      YYABORT;
	  }
	| using_list ',' ident
	  {
unknown's avatar
unknown committed
5290
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5291
	    if (!($$= new Item_cond_and(new Item_func_eq(new Item_field(sel->db1,sel->table1,$3.str), new Item_field(sel->db2,sel->table2,$3.str)), $1)))
unknown's avatar
unknown committed
5292
	      YYABORT;
unknown's avatar
unknown committed
5293
	  };
unknown's avatar
unknown committed
5294 5295

interval:
5296 5297
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
unknown's avatar
unknown committed
5298
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
unknown's avatar
unknown committed
5299 5300
	| DAY_MINUTE_SYM	{ $$=INTERVAL_DAY_MINUTE; }
	| DAY_SECOND_SYM	{ $$=INTERVAL_DAY_SECOND; }
unknown's avatar
unknown committed
5301
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
unknown's avatar
unknown committed
5302 5303
	| HOUR_MINUTE_SYM	{ $$=INTERVAL_HOUR_MINUTE; }
	| HOUR_SECOND_SYM	{ $$=INTERVAL_HOUR_SECOND; }
unknown's avatar
unknown committed
5304 5305
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
unknown's avatar
unknown committed
5306
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
5307 5308 5309 5310 5311 5312 5313 5314
	| 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
5315 5316
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
5317
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
unknown's avatar
unknown committed
5318
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
5319 5320
	| YEAR_SYM		{ $$=INTERVAL_YEAR; }
        ;
unknown's avatar
unknown committed
5321

5322
date_time_type:
5323 5324 5325 5326 5327
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
5328

unknown's avatar
unknown committed
5329 5330 5331
table_alias:
	/* empty */
	| AS
unknown's avatar
unknown committed
5332
	| EQ;
unknown's avatar
unknown committed
5333 5334 5335 5336

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

unknown's avatar
unknown committed
5339 5340 5341 5342
opt_all:
	/* empty */
	| ALL
	;
unknown's avatar
unknown committed
5343 5344

where_clause:
unknown's avatar
unknown committed
5345
	/* empty */  { Select->where= 0; }
unknown's avatar
unknown committed
5346 5347 5348 5349 5350
	| WHERE
          {
            Select->parsing_place= IN_WHERE;
          }
          expr
5351
	  {
unknown's avatar
unknown committed
5352 5353 5354 5355 5356
            SELECT_LEX *select= Select;
	    select->where= $3;
            select->parsing_place= NO_MATTER;
	    if ($3)
	      $3->top_level_item();
5357
	  }
unknown's avatar
unknown committed
5358
 	;
unknown's avatar
unknown committed
5359 5360 5361

having_clause:
	/* empty */
unknown's avatar
unknown committed
5362 5363
	| HAVING
	  {
5364
	    Select->parsing_place= IN_HAVING;
unknown's avatar
unknown committed
5365 5366
          }
	  expr
unknown's avatar
unknown committed
5367
	  {
unknown's avatar
unknown committed
5368
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5369
	    sel->having= $3;
5370
	    sel->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
5371 5372 5373
	    if ($3)
	      $3->top_level_item();
	  }
5374
	;
unknown's avatar
unknown committed
5375 5376

opt_escape:
5377 5378
	ESCAPE_SYM simple_expr { $$= $2; }
	| /* empty */
5379 5380 5381 5382 5383
          {

            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
5384 5385
          }
        ;
unknown's avatar
unknown committed
5386 5387 5388


/*
unknown's avatar
unknown committed
5389
   group by statement in select
unknown's avatar
unknown committed
5390 5391 5392 5393
*/

group_clause:
	/* empty */
5394
	| GROUP BY group_list olap_opt;
unknown's avatar
unknown committed
5395 5396

group_list:
unknown's avatar
unknown committed
5397
	group_list ',' order_ident order_dir
unknown's avatar
unknown committed
5398
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
5399
	| order_ident order_dir
unknown's avatar
unknown committed
5400
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
5401

5402 5403
olap_opt:
	/* empty */ {}
5404
	| WITH CUBE_SYM
5405
          {
5406
	    LEX *lex=Lex;
5407 5408
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
5409
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
5410 5411 5412
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
5413
	    lex->current_select->olap= CUBE_TYPE;
unknown's avatar
unknown committed
5414
	    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
5415
	    YYABORT;	/* To be deleted in 5.1 */
5416
	  }
5417
	| WITH ROLLUP_SYM
5418
          {
5419 5420 5421
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
5422
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
5423 5424 5425
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
5426
	    lex->current_select->olap= ROLLUP_TYPE;
5427
	  }
5428
	;
5429

unknown's avatar
unknown committed
5430
/*
unknown's avatar
unknown committed
5431
   Order by statement in select
unknown's avatar
unknown committed
5432 5433
*/

5434
opt_order_clause:
unknown's avatar
unknown committed
5435
	/* empty */
unknown's avatar
unknown committed
5436
	| order_clause;
5437 5438

order_clause:
5439 5440
	ORDER_SYM BY
        {
unknown's avatar
unknown committed
5441
	  LEX *lex=Lex;
5442
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
unknown's avatar
unknown committed
5443
	      lex->current_select->olap !=
5444
	      UNSPECIFIED_OLAP_TYPE)
5445
	  {
unknown's avatar
unknown committed
5446 5447
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
5448 5449
	    YYABORT;
	  }
unknown's avatar
unknown committed
5450
	} order_list;
unknown's avatar
unknown committed
5451 5452 5453

order_list:
	order_list ',' order_ident order_dir
unknown's avatar
unknown committed
5454
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
5455
	| order_ident order_dir
unknown's avatar
unknown committed
5456
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
5457 5458 5459

order_dir:
	/* empty */ { $$ =  1; }
unknown's avatar
unknown committed
5460
	| ASC  { $$ =1; }
unknown's avatar
unknown committed
5461
	| DESC { $$ =0; };
unknown's avatar
unknown committed
5462 5463


5464 5465 5466
opt_limit_clause_init:
	/* empty */
	{
5467 5468
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
5469
          sel->offset_limit= 0L;
5470
          sel->select_limit= HA_POS_ERROR;
5471
	}
5472 5473 5474
	| limit_clause {}
	;

5475 5476 5477 5478 5479
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

5480
limit_clause:
5481
	LIMIT limit_options {}
unknown's avatar
unknown committed
5482
	;
unknown's avatar
unknown committed
5483 5484 5485

limit_options:
	ULONG_NUM
unknown's avatar
unknown committed
5486
	  {
unknown's avatar
unknown committed
5487
            SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5488 5489
            sel->select_limit= $1;
            sel->offset_limit= 0L;
5490
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5491 5492 5493
	  }
	| ULONG_NUM ',' ULONG_NUM
	  {
unknown's avatar
unknown committed
5494
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5495 5496
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
5497
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5498
	  }
unknown's avatar
unknown committed
5499
	| ULONG_NUM OFFSET_SYM ULONG_NUM
unknown's avatar
unknown committed
5500
	  {
unknown's avatar
unknown committed
5501
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5502 5503
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
5504
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5505 5506
	  }
	;
unknown's avatar
unknown committed
5507 5508 5509 5510 5511


delete_limit_clause:
	/* empty */
	{
unknown's avatar
unknown committed
5512
	  LEX *lex=Lex;
5513
	  lex->current_select->select_limit= HA_POS_ERROR;
unknown's avatar
unknown committed
5514
	}
unknown's avatar
unknown committed
5515
	| LIMIT ulonglong_num
5516 5517 5518 5519 5520
	{
	  SELECT_LEX *sel= Select;
	  sel->select_limit= (ha_rows) $2;
	  sel->explicit_limit= 1;
	};
unknown's avatar
unknown committed
5521 5522

ULONG_NUM:
unknown's avatar
unknown committed
5523 5524 5525
	NUM	        { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| LONG_NUM      { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| ULONGLONG_NUM { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
5526
        | DECIMAL_NUM 	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
5527 5528
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
5529

unknown's avatar
unknown committed
5530
ulonglong_num:
unknown's avatar
unknown committed
5531 5532 5533
	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
5534
        | DECIMAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
5535 5536
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
5537 5538 5539 5540 5541 5542

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
5543 5544
	    if (&lex->select_lex != lex->current_select)
	    {
unknown's avatar
unknown committed
5545
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
5546 5547
	      YYABORT;
	    }
unknown's avatar
unknown committed
5548 5549 5550
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
unknown's avatar
unknown committed
5551
	    if (add_proc_to_list(lex->thd, new Item_field(NULL,NULL,$2.str)))
unknown's avatar
unknown committed
5552
	      YYABORT;
5553
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
5554
	  }
unknown's avatar
unknown committed
5555
	  '(' procedure_list ')';
unknown's avatar
unknown committed
5556 5557 5558 5559


procedure_list:
	/* empty */ {}
unknown's avatar
unknown committed
5560
	| procedure_list2 {};
unknown's avatar
unknown committed
5561 5562 5563

procedure_list2:
	procedure_list2 ',' procedure_item
unknown's avatar
unknown committed
5564
	| procedure_item;
unknown's avatar
unknown committed
5565 5566 5567 5568

procedure_item:
	  remember_name expr
	  {
unknown's avatar
unknown committed
5569 5570
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
unknown's avatar
unknown committed
5571 5572
	      YYABORT;
	    if (!$2->name)
unknown's avatar
unknown committed
5573
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1), YYTHD->charset());
5574 5575 5576 5577 5578 5579
	  }
          ;


select_var_list_init:
	   {
unknown's avatar
unknown committed
5580 5581
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
5582 5583
	        YYABORT;
	   }
5584
	   select_var_list
5585
	   {}
5586
           ;
unknown's avatar
unknown committed
5587

unknown's avatar
unknown committed
5588
select_var_list:
5589 5590 5591 5592
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

5593 5594 5595 5596 5597 5598 5599 5600 5601 5602
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
5603
           {
5604
             LEX *lex=Lex;
5605 5606 5607 5608
	     sp_pvar_t *t;

	     if (!lex->spcont || !(t=lex->spcont->find_pvar(&$1)))
	     {
5609
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
unknown's avatar
unknown committed
5610
	       YYABORT;
5611 5612 5613 5614 5615 5616 5617 5618
	     }
	     if (! lex->result)
	       YYABORT;
	     else
	     {
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($1,1,t->offset,t->type));
	       t->isset= TRUE;
	     }
unknown's avatar
unknown committed
5619
	   }
unknown's avatar
unknown committed
5620
           ;
unknown's avatar
unknown committed
5621

5622
into:
unknown's avatar
unknown committed
5623
        INTO OUTFILE TEXT_STRING_sys
unknown's avatar
unknown committed
5624
	{
unknown's avatar
unknown committed
5625
          LEX *lex= Lex;
5626 5627 5628 5629
          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
5630 5631
	}
	opt_field_term opt_line_term
unknown's avatar
unknown committed
5632
	| INTO DUMPFILE TEXT_STRING_sys
unknown's avatar
unknown committed
5633
	{
5634
	  LEX *lex=Lex;
unknown's avatar
unknown committed
5635 5636
	  if (!lex->describe)
	  {
5637
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
5638 5639 5640 5641 5642
	    if (!(lex->exchange= new sql_exchange($3.str,1)))
	      YYABORT;
	    if (!(lex->result= new select_dump(lex->exchange)))
	      YYABORT;
	  }
unknown's avatar
unknown committed
5643
	}
5644
        | INTO select_var_list_init
unknown's avatar
unknown committed
5645
	{
5646
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5647 5648
	}
        ;
unknown's avatar
unknown committed
5649

unknown's avatar
unknown committed
5650 5651 5652
/*
  DO statement
*/
unknown's avatar
unknown committed
5653

5654
do:	DO_SYM
unknown's avatar
unknown committed
5655 5656 5657
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
5658 5659 5660 5661 5662
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
unknown's avatar
unknown committed
5663
	}
5664 5665
	;

unknown's avatar
unknown committed
5666
/*
5667
  Drop : delete tables or index or user
unknown's avatar
unknown committed
5668 5669 5670
*/

drop:
unknown's avatar
unknown committed
5671
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
unknown's avatar
unknown committed
5672
	{
unknown's avatar
unknown committed
5673 5674
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
5675 5676
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
unknown's avatar
unknown committed
5677
	}
unknown's avatar
unknown committed
5678
	| DROP INDEX_SYM ident ON table_ident {}
unknown's avatar
unknown committed
5679
	  {
unknown's avatar
unknown committed
5680 5681
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
5682 5683 5684
	     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
5685 5686
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
unknown's avatar
unknown committed
5687 5688 5689 5690
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
unknown's avatar
unknown committed
5691 5692 5693 5694
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
	    lex->name=$4.str;
unknown's avatar
unknown committed
5695
	 }
5696
	| DROP FUNCTION_SYM if_exists sp_name
unknown's avatar
unknown committed
5697
	  {
unknown's avatar
unknown committed
5698
	    LEX *lex=Lex;
5699 5700
	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
5701
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
5702 5703
	      YYABORT;
	    }
unknown's avatar
unknown committed
5704
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
5705 5706 5707 5708 5709 5710 5711 5712
	    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
5713
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
5714 5715 5716 5717 5718
	      YYABORT;
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
5719
	  }
5720
	| DROP USER clear_privileges user_list
5721
	  {
5722 5723
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
5724 5725 5726 5727 5728 5729 5730 5731 5732 5733
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
        | DROP TRIGGER_SYM ident '.' ident
          {
            LEX *lex= Lex;
unknown's avatar
unknown committed
5734

5735 5736 5737 5738 5739 5740 5741 5742
            lex->sql_command= SQLCOM_DROP_TRIGGER;
            /* QQ: Could we loosen lock type in certain cases ? */
            if (!lex->select_lex.add_table_to_list(YYTHD,
                                                   new Table_ident($3),
                                                   (LEX_STRING*) 0,
                                                   TL_OPTION_UPDATING,
                                                   TL_WRITE))
              YYABORT;
5743
            lex->ident= $5;
5744 5745
          }
	;
unknown's avatar
unknown committed
5746 5747

table_list:
5748
	table_name
unknown's avatar
unknown committed
5749
	| table_list ',' table_name;
unknown's avatar
unknown committed
5750

5751
table_name:
unknown's avatar
unknown committed
5752
	table_ident
unknown's avatar
unknown committed
5753 5754 5755 5756 5757
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
unknown's avatar
unknown committed
5758 5759

if_exists:
5760
	/* empty */ { $$= 0; }
5761 5762
	| IF EXISTS { $$= 1; }
	;
unknown's avatar
unknown committed
5763

5764 5765 5766 5767
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
unknown's avatar
unknown committed
5768 5769 5770 5771 5772
/*
** Insert : add new data to table
*/

insert:
5773 5774 5775
	INSERT
	{
	  LEX *lex= Lex;
5776 5777
	  lex->sql_command= SQLCOM_INSERT;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
5778
	  mysql_init_select(lex);
5779 5780
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
5781
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
5782
	} insert_lock_option
5783
	opt_ignore insert2
unknown's avatar
unknown committed
5784
	{
unknown's avatar
unknown committed
5785
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
5786
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
5787
	}
5788
	insert_field_spec opt_insert_update
5789
	{}
unknown's avatar
unknown committed
5790
	;
unknown's avatar
unknown committed
5791 5792

replace:
5793 5794
	REPLACE
	{
5795
	  LEX *lex=Lex;
5796 5797
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
unknown's avatar
unknown committed
5798
	  mysql_init_select(lex);
5799
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
5800
	}
unknown's avatar
unknown committed
5801 5802
	replace_lock_option insert2
	{
unknown's avatar
unknown committed
5803
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
5804
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
5805 5806
	}
	insert_field_spec
5807
	{}
unknown's avatar
unknown committed
5808
	;
unknown's avatar
unknown committed
5809 5810

insert_lock_option:
unknown's avatar
unknown committed
5811 5812 5813 5814
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
5815
	;
unknown's avatar
unknown committed
5816 5817

replace_lock_option:
unknown's avatar
unknown committed
5818 5819
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
unknown's avatar
unknown committed
5820 5821 5822

insert2:
	INTO insert_table {}
unknown's avatar
unknown committed
5823
	| insert_table {};
unknown's avatar
unknown committed
5824 5825

insert_table:
5826
	table_name
unknown's avatar
unknown committed
5827
	{
unknown's avatar
unknown committed
5828 5829 5830 5831
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
unknown's avatar
unknown committed
5832
	};
unknown's avatar
unknown committed
5833 5834

insert_field_spec:
unknown's avatar
unknown committed
5835 5836 5837
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
unknown's avatar
unknown committed
5838 5839
	| SET
	  {
unknown's avatar
unknown committed
5840 5841 5842
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
5843 5844
	      YYABORT;
	   }
unknown's avatar
unknown committed
5845
	   ident_eq_list;
unknown's avatar
unknown committed
5846 5847 5848

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
unknown's avatar
unknown committed
5849
	| insert_ident		{ Lex->field_list.push_back($1); };
unknown's avatar
unknown committed
5850 5851 5852

insert_values:
	VALUES	values_list  {}
unknown's avatar
unknown committed
5853
	| VALUE_SYM values_list  {}
unknown's avatar
unknown committed
5854 5855
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
5856
        ;
unknown's avatar
unknown committed
5857 5858 5859

values_list:
	values_list ','  no_braces
unknown's avatar
unknown committed
5860
	| no_braces;
unknown's avatar
unknown committed
5861 5862 5863 5864

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
unknown's avatar
unknown committed
5865
	ident_eq_value;
unknown's avatar
unknown committed
5866 5867

ident_eq_value:
5868
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
5869
	 {
unknown's avatar
unknown committed
5870 5871 5872
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
unknown's avatar
unknown committed
5873
	    YYABORT;
unknown's avatar
unknown committed
5874
	 };
unknown's avatar
unknown committed
5875 5876

equal:	EQ		{}
unknown's avatar
unknown committed
5877 5878 5879 5880 5881 5882 5883
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
unknown's avatar
unknown committed
5884 5885 5886 5887 5888 5889 5890 5891 5892

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
unknown's avatar
unknown committed
5893 5894
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
5895
	    YYABORT;
unknown's avatar
unknown committed
5896
	 };
unknown's avatar
unknown committed
5897 5898 5899

opt_values:
	/* empty */ {}
unknown's avatar
unknown committed
5900
	| values;
unknown's avatar
unknown committed
5901 5902

values:
unknown's avatar
unknown committed
5903
	values ','  expr_or_default
unknown's avatar
unknown committed
5904 5905 5906 5907
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
unknown's avatar
unknown committed
5908 5909 5910 5911 5912 5913 5914 5915
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
5916
	expr	  { $$= $1;}
unknown's avatar
SCRUM  
unknown committed
5917
	| DEFAULT {$$= new Item_default_value(); }
unknown's avatar
unknown committed
5918
	;
unknown's avatar
unknown committed
5919

5920 5921
opt_insert_update:
        /* empty */
unknown's avatar
unknown committed
5922
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
unknown's avatar
unknown committed
5923
          KEY_SYM UPDATE_SYM insert_update_list
5924 5925
        ;

unknown's avatar
unknown committed
5926 5927 5928
/* Update rows in a table */

update:
5929 5930
	UPDATE_SYM
	{
5931
	  LEX *lex= Lex;
unknown's avatar
unknown committed
5932
	  mysql_init_select(lex);
5933
          lex->sql_command= SQLCOM_UPDATE;
unknown's avatar
unknown committed
5934
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
5935
	  lex->duplicates= DUP_ERROR; 
5936
        }
unknown's avatar
unknown committed
5937
        opt_low_priority opt_ignore join_table_list
unknown's avatar
unknown committed
5938
	SET update_list
unknown's avatar
unknown committed
5939
	{
5940 5941
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
5942
	  {
unknown's avatar
unknown committed
5943
            lex->sql_command= SQLCOM_UPDATE_MULTI;
unknown's avatar
unknown committed
5944
	    lex->multi_lock_option= $3;
5945
	  }
5946 5947 5948
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
5949 5950
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
5951 5952
	    YYABORT;
	  }
5953
	  else
unknown's avatar
unknown committed
5954
	    Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
5955
	}
5956
	where_clause opt_order_clause delete_limit_clause {}
unknown's avatar
unknown committed
5957
	;
unknown's avatar
unknown committed
5958 5959

update_list:
unknown's avatar
unknown committed
5960 5961 5962 5963
	update_list ',' update_elem
	| update_elem;

update_elem:
unknown's avatar
unknown committed
5964
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
5965
	{
unknown's avatar
unknown committed
5966
	  if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
unknown's avatar
unknown committed
5967
	    YYABORT;
unknown's avatar
unknown committed
5968 5969 5970 5971 5972 5973 5974
	};

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
unknown's avatar
unknown committed
5975
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
5976
	  {
unknown's avatar
unknown committed
5977 5978 5979
	  LEX *lex= Lex;
	  if (lex->update_list.push_back($1) || 
	      lex->value_list.push_back($3))
unknown's avatar
unknown committed
5980
	      YYABORT;
unknown's avatar
unknown committed
5981
	  };
unknown's avatar
unknown committed
5982 5983

opt_low_priority:
5984
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
5985
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
5986 5987 5988 5989

/* Delete rows from a table */

delete:
5990
	DELETE_SYM
5991
	{
5992 5993
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
unknown's avatar
unknown committed
5994
	  mysql_init_select(lex);
unknown's avatar
unknown committed
5995
	  lex->lock_option= lex->thd->update_lock_default;
5996
	  lex->ignore= 0;
5997
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
5998
	}
unknown's avatar
unknown committed
5999 6000
	opt_delete_options single_multi {}
	;
6001 6002

single_multi:
unknown's avatar
unknown committed
6003 6004
 	FROM table_ident
	{
unknown's avatar
unknown committed
6005 6006
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
unknown's avatar
unknown committed
6007 6008 6009
	    YYABORT;
	}
	where_clause opt_order_clause
6010
	delete_limit_clause {}
unknown's avatar
unknown committed
6011
	| table_wild_list
unknown's avatar
unknown committed
6012
	  { mysql_init_multi_delete(Lex); }
unknown's avatar
unknown committed
6013
          FROM join_table_list where_clause
unknown's avatar
unknown committed
6014 6015
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
6016 6017 6018
	  USING join_table_list where_clause
	  {}
	;
unknown's avatar
unknown committed
6019 6020 6021

table_wild_list:
	  table_wild_one {}
unknown's avatar
unknown committed
6022
	  | table_wild_list ',' table_wild_one {};
unknown's avatar
unknown committed
6023 6024

table_wild_one:
unknown's avatar
unknown committed
6025
	ident opt_wild opt_table_alias
unknown's avatar
unknown committed
6026
	{
unknown's avatar
unknown committed
6027 6028
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
					 TL_OPTION_UPDATING, Lex->lock_option))
unknown's avatar
unknown committed
6029 6030
	    YYABORT;
        }
unknown's avatar
unknown committed
6031
	| ident '.' ident opt_wild opt_table_alias
unknown's avatar
unknown committed
6032
	  {
unknown's avatar
unknown committed
6033 6034
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
unknown's avatar
unknown committed
6035 6036
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
unknown's avatar
unknown committed
6037
	      YYABORT;
unknown's avatar
unknown committed
6038
	  }
unknown's avatar
unknown committed
6039
	;
unknown's avatar
unknown committed
6040 6041

opt_wild:
6042
	/* empty */	{}
unknown's avatar
unknown committed
6043
	| '.' '*'	{};
unknown's avatar
unknown committed
6044 6045


6046
opt_delete_options:
unknown's avatar
unknown committed
6047
	/* empty */	{}
unknown's avatar
unknown committed
6048
	| opt_delete_option opt_delete_options {};
6049 6050

opt_delete_option:
6051
	QUICK		{ Select->options|= OPTION_QUICK; }
6052
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
6053
	| IGNORE_SYM	{ Lex->ignore= 1; };
6054

6055
truncate:
unknown's avatar
unknown committed
6056
	TRUNCATE_SYM opt_table_sym table_name
6057
	{
6058
	  LEX* lex= Lex;
6059
	  lex->sql_command= SQLCOM_TRUNCATE;
6060
	  lex->select_lex.options= 0;
6061
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
6062 6063
	}
	;
6064

unknown's avatar
unknown committed
6065 6066
opt_table_sym:
	/* empty */
unknown's avatar
unknown committed
6067
	| TABLE_SYM;
6068

unknown's avatar
unknown committed
6069 6070
/* Show things */

6071 6072
show:	SHOW
	{
unknown's avatar
unknown committed
6073 6074
	  LEX *lex=Lex;
	  lex->wild=0;
6075 6076 6077
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
6078 6079
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
unknown's avatar
unknown committed
6080
	show_param
6081 6082
	{}
	;
unknown's avatar
unknown committed
6083 6084

show_param:
6085
         DATABASES wild_and_where
6086 6087 6088 6089 6090 6091 6092
         {
           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;
         }
6093
         | opt_full TABLES opt_db wild_and_where
6094 6095 6096 6097
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLES;
6098
             lex->select_lex.db= $3;
6099 6100 6101
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLE_NAMES))
               YYABORT;
           }
6102
         | TABLE_SYM STATUS_SYM opt_db wild_and_where
6103 6104 6105 6106
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLE_STATUS;
6107
             lex->select_lex.db= $3;
6108 6109 6110
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLES))
               YYABORT;
           }
6111
        | OPEN_SYM TABLES opt_db wild_and_where
unknown's avatar
unknown committed
6112
	  {
6113
	    LEX *lex= Lex;
6114 6115
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_OPEN_TABLES;
6116
	    lex->select_lex.db= $3;
6117 6118
            if (prepare_schema_table(YYTHD, lex, 0, SCH_OPEN_TABLES))
              YYABORT;
6119
	  }
unknown's avatar
unknown committed
6120
	| ENGINE_SYM storage_engines 
unknown's avatar
unknown committed
6121 6122
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
6123
	| opt_full COLUMNS from_or_in table_ident opt_db wild_and_where
unknown's avatar
unknown committed
6124
	  {
6125 6126 6127
 	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SELECT;
	    lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
6128 6129 6130
	    if ($5)
	      $4->change_db($5);
	    if (prepare_schema_table(YYTHD, lex, $4, SCH_COLUMNS))
unknown's avatar
unknown committed
6131 6132
	      YYABORT;
	  }
6133
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
unknown's avatar
unknown committed
6134 6135
	  TEXT_STRING_sys AND_SYM MASTER_LOG_POS_SYM EQ ulonglong_num
	  AND_SYM MASTER_SERVER_ID_SYM EQ
unknown's avatar
unknown committed
6136 6137 6138 6139 6140
	ULONG_NUM
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
6141
	    Lex->mi.server_id = $16;
unknown's avatar
unknown committed
6142
          }
6143
        | master_or_binary LOGS_SYM
unknown's avatar
unknown committed
6144 6145
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
6146 6147 6148 6149 6150
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
unknown's avatar
unknown committed
6151
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
unknown's avatar
unknown committed
6152
          {
6153 6154
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
6155
          } opt_limit_clause_init
6156
        | keys_or_index from_or_in table_ident opt_db where_clause
6157 6158 6159 6160
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_KEYS;
6161 6162 6163
	    if ($4)
	      $3->change_db($4);
            if (prepare_schema_table(YYTHD, lex, $3, SCH_STATISTICS))
6164
              YYABORT;
unknown's avatar
unknown committed
6165
	  }
unknown's avatar
unknown committed
6166 6167 6168 6169 6170 6171 6172 6173
	| 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
6174 6175 6176 6177 6178 6179 6180
	    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
6181 6182 6183 6184 6185 6186
	  }
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
6187
        | COUNT_SYM '(' '*' ')' WARNINGS
6188
          { (void) create_select_for_variable("warning_count"); }
6189
        | COUNT_SYM '(' '*' ')' ERRORS
6190
	  { (void) create_select_for_variable("error_count"); }
6191
        | WARNINGS opt_limit_clause_init
unknown's avatar
unknown committed
6192
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
6193
        | ERRORS opt_limit_clause_init
6194
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
6195
        | opt_var_type STATUS_SYM wild_and_where
6196
          {
6197 6198 6199 6200 6201 6202
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS;
            lex->option_type= (enum_var_type) $1;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_STATUS))
              YYABORT;
6203
	  }	
unknown's avatar
unknown committed
6204
        | INNOBASE_SYM STATUS_SYM
unknown's avatar
unknown committed
6205
          { Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS; WARN_DEPRECATED("SHOW INNODB STATUS", "SHOW ENGINE INNODB STATUS"); }
unknown's avatar
unknown committed
6206 6207
        | MUTEX_SYM STATUS_SYM
          { Lex->sql_command = SQLCOM_SHOW_MUTEX_STATUS; }
unknown's avatar
unknown committed
6208 6209
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
6210
        | opt_var_type  VARIABLES wild_and_where
unknown's avatar
unknown committed
6211
	  {
6212 6213 6214 6215 6216 6217
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_VARIABLES;
            lex->option_type= (enum_var_type) $1;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_VARIABLES))
              YYABORT;
6218
	  }
6219
        | charset wild_and_where
6220 6221 6222 6223 6224 6225 6226
          {
            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;
          }
6227
        | COLLATION_SYM wild_and_where
6228 6229 6230 6231 6232 6233 6234
          {
            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;
          }
6235
	| BERKELEY_DB_SYM LOGS_SYM
unknown's avatar
unknown committed
6236
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW BDB LOGS", "SHOW ENGINE BDB LOGS"); }
unknown's avatar
unknown committed
6237
	| LOGS_SYM
unknown's avatar
unknown committed
6238
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW LOGS", "SHOW ENGINE BDB LOGS"); }
6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258
	| GRANTS
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    THD *thd= lex->thd;
	    LEX_USER *curr_user;
            if (!(curr_user= (LEX_USER*) thd->alloc(sizeof(st_lex_user))))
              YYABORT;
            curr_user->user.str= thd->priv_user;
            curr_user->user.length= strlen(thd->priv_user);
            if (*thd->priv_host != 0)
            {
              curr_user->host.str= thd->priv_host;
              curr_user->host.length= strlen(thd->priv_host);
            }
            else
            {
              curr_user->host.str= (char *) "%";
              curr_user->host.length= 1;
            }
6259
            curr_user->password=null_lex_str;
6260 6261
	    lex->grant_user= curr_user;
	  }
unknown's avatar
unknown committed
6262
	| GRANTS FOR_SYM user
unknown's avatar
unknown committed
6263 6264 6265 6266
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
6267
	    lex->grant_user->password=null_lex_str;
unknown's avatar
unknown committed
6268
	  }
unknown's avatar
unknown committed
6269
	| CREATE DATABASE opt_if_not_exists ident
unknown's avatar
unknown committed
6270 6271
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
unknown's avatar
unknown committed
6272 6273
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
unknown's avatar
unknown committed
6274
	  }
unknown's avatar
unknown committed
6275 6276
        | CREATE TABLE_SYM table_ident
          {
6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287
            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
6288
	      YYABORT;
6289
            lex->only_view= 1;
unknown's avatar
unknown committed
6290 6291 6292 6293
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
unknown's avatar
unknown committed
6294
          }
unknown's avatar
unknown committed
6295 6296 6297
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312
          }
	| 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;
	  }
6313
	| PROCEDURE STATUS_SYM wild_and_where
6314
	  {
6315 6316 6317
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_PROC;
6318 6319
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
6320 6321
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
6322
	  }
6323
	| FUNCTION_SYM STATUS_SYM wild_and_where
6324
	  {
6325 6326 6327
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_FUNC;
6328 6329
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
6330 6331
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
6332
	  };
unknown's avatar
unknown committed
6333

unknown's avatar
unknown committed
6334 6335 6336 6337 6338 6339 6340 6341
show_engine_param:
	STATUS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_INNODB:
	      Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;
	      break;
	    default:
unknown's avatar
unknown committed
6342
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "STATUS");
unknown's avatar
unknown committed
6343 6344 6345 6346 6347 6348 6349 6350 6351 6352
	      YYABORT;
	    }
	  }
	| LOGS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_BERKELEY_DB:
	      Lex->sql_command = SQLCOM_SHOW_LOGS;
	      break;
	    default:
unknown's avatar
unknown committed
6353
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "LOGS");
unknown's avatar
unknown committed
6354 6355 6356 6357
	      YYABORT;
	    }
	  };

6358 6359 6360 6361
master_or_binary:
	MASTER_SYM
	| BINARY;

unknown's avatar
unknown committed
6362 6363 6364 6365
opt_storage:
	/* empty */
	| STORAGE_SYM;

unknown's avatar
unknown committed
6366 6367
opt_db:
	/* empty */  { $$= 0; }
unknown's avatar
unknown committed
6368
	| from_or_in ident { $$= $2.str; };
unknown's avatar
unknown committed
6369

unknown's avatar
unknown committed
6370 6371
opt_full:
	/* empty */ { Lex->verbose=0; }
unknown's avatar
unknown committed
6372
	| FULL	    { Lex->verbose=1; };
unknown's avatar
unknown committed
6373

unknown's avatar
unknown committed
6374 6375
from_or_in:
	FROM
unknown's avatar
unknown committed
6376
	| IN_SYM;
unknown's avatar
unknown committed
6377

unknown's avatar
unknown committed
6378 6379
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
unknown's avatar
unknown committed
6380
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
unknown's avatar
unknown committed
6381 6382 6383

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

6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398
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
6399

unknown's avatar
unknown committed
6400 6401 6402 6403
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
6404 6405 6406 6407 6408 6409 6410 6411 6412
          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
6413 6414
	    YYABORT;
	}
6415
	opt_describe_column {}
unknown's avatar
unknown committed
6416 6417 6418
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
6419
          {
unknown's avatar
unknown committed
6420
	    LEX *lex=Lex;
6421
	    lex->select_lex.options|= SELECT_DESCRIBE;
unknown's avatar
unknown committed
6422 6423
	  }
	;
unknown's avatar
unknown committed
6424 6425 6426

describe_command:
	DESC
unknown's avatar
unknown committed
6427
	| DESCRIBE;
unknown's avatar
unknown committed
6428

unknown's avatar
unknown committed
6429 6430 6431 6432 6433
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
	;

unknown's avatar
unknown committed
6434 6435 6436
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
unknown's avatar
unknown committed
6437
	| ident
unknown's avatar
unknown committed
6438
	  { Lex->wild= new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info); };
unknown's avatar
unknown committed
6439 6440 6441 6442 6443


/* flush things */

flush:
6444
	FLUSH_SYM opt_no_write_to_binlog
unknown's avatar
unknown committed
6445 6446 6447
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
unknown's avatar
unknown committed
6448
          lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
6449
	}
6450 6451 6452
	flush_options
	{}
	;
unknown's avatar
unknown committed
6453 6454 6455

flush_options:
	flush_options ',' flush_option
unknown's avatar
unknown committed
6456
	| flush_option;
unknown's avatar
unknown committed
6457 6458

flush_option:
6459
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
unknown's avatar
unknown committed
6460
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
unknown's avatar
unknown committed
6461
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
unknown's avatar
unknown committed
6462 6463 6464 6465
	| 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
6466 6467
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
6468 6469
	| DES_KEY_FILE	{ Lex->type|= REFRESH_DES_KEY_FILE; }
 	| RESOURCES     { Lex->type|= REFRESH_USER_RESOURCES; };
unknown's avatar
unknown committed
6470

unknown's avatar
unknown committed
6471
opt_table_list:
6472 6473
	/* empty */  {;}
	| table_list {;};
unknown's avatar
unknown committed
6474

unknown's avatar
unknown committed
6475
reset:
unknown's avatar
unknown committed
6476 6477 6478 6479
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
6480 6481 6482 6483
	} reset_options
	{}
	;

unknown's avatar
unknown committed
6484 6485
reset_options:
	reset_options ',' reset_option
unknown's avatar
unknown committed
6486
	| reset_option;
unknown's avatar
unknown committed
6487 6488

reset_option:
unknown's avatar
unknown committed
6489 6490
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
unknown's avatar
unknown committed
6491
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
unknown's avatar
unknown committed
6492

unknown's avatar
unknown committed
6493
purge:
unknown's avatar
unknown committed
6494 6495 6496 6497
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
6498 6499 6500 6501 6502
	} purge_options
	{}
	;

purge_options:
6503
	master_or_binary LOGS_SYM purge_option
unknown's avatar
unknown committed
6504
	;
6505 6506

purge_option:
unknown's avatar
unknown committed
6507
        TO_SYM TEXT_STRING_sys
6508 6509 6510 6511 6512 6513
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
6514 6515 6516 6517
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
unknown's avatar
unknown committed
6518
	}
unknown's avatar
unknown committed
6519
	;
unknown's avatar
unknown committed
6520

unknown's avatar
unknown committed
6521 6522 6523
/* kill threads */

kill:
6524
	KILL_SYM kill_option expr
unknown's avatar
unknown committed
6525
	{
unknown's avatar
unknown committed
6526
	  LEX *lex=Lex;
6527 6528 6529
	  lex->value_list.empty();
	  lex->value_list.push_front($3);
          lex->sql_command= SQLCOM_KILL;
unknown's avatar
unknown committed
6530
	};
unknown's avatar
unknown committed
6531

6532 6533 6534 6535 6536
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; };

unknown's avatar
unknown committed
6537 6538 6539
/* change database */

use:	USE_SYM ident
unknown's avatar
unknown committed
6540 6541
	{
	  LEX *lex=Lex;
6542 6543
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
unknown's avatar
unknown committed
6544
	};
unknown's avatar
unknown committed
6545 6546 6547

/* import, export of files */

unknown's avatar
unknown committed
6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564
load:   LOAD DATA_SYM 
        {
          LEX *lex=Lex;
          lex->fname_start= lex->ptr;
        }
        load_data
        {}
        |
        LOAD TABLE_SYM table_ident FROM MASTER_SYM
        {
          Lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
          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
6565
	{
unknown's avatar
unknown committed
6566 6567
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
unknown's avatar
unknown committed
6568 6569
	  lex->lock_option= $1;
	  lex->local_file=  $2;
6570 6571
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
unknown's avatar
unknown committed
6572
	  if (!(lex->exchange= new sql_exchange($4.str, 0)))
unknown's avatar
unknown committed
6573
	    YYABORT;
unknown's avatar
unknown committed
6574 6575 6576 6577 6578
        }
        opt_duplicate INTO
        {
	  LEX *lex=Lex;
	  lex->fname_end= lex->ptr;
unknown's avatar
unknown committed
6579
	}
6580
        TABLE_SYM table_ident
unknown's avatar
unknown committed
6581
        {
6582 6583 6584 6585 6586 6587 6588
          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
6589
        }
6590 6591 6592
        opt_field_term opt_line_term opt_ignore_lines opt_field_or_var_spec
        opt_load_data_set_spec
        {}
6593
        |
unknown's avatar
unknown committed
6594
	FROM MASTER_SYM
6595 6596
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
unknown's avatar
unknown committed
6597
        };
unknown's avatar
unknown committed
6598 6599 6600

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

unknown's avatar
unknown committed
6603
load_data_lock:
6604
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
6605 6606
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
6607 6608


unknown's avatar
unknown committed
6609 6610 6611
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
6612
	| IGNORE_SYM	{ Lex->ignore= 1; };
unknown's avatar
unknown committed
6613 6614 6615

opt_field_term:
	/* empty */
unknown's avatar
unknown committed
6616
	| COLUMNS field_term_list;
unknown's avatar
unknown committed
6617 6618 6619

field_term_list:
	field_term_list field_term
unknown's avatar
unknown committed
6620
	| field_term;
unknown's avatar
unknown committed
6621 6622

field_term:
6623 6624
	TERMINATED BY text_string 
          {
6625
            DBUG_ASSERT(Lex->exchange != 0);
6626 6627
            Lex->exchange->field_term= $3;
          }
unknown's avatar
unknown committed
6628
	| OPTIONALLY ENCLOSED BY text_string
unknown's avatar
unknown committed
6629
	  {
unknown's avatar
unknown committed
6630
            LEX *lex= Lex;
6631
            DBUG_ASSERT(lex->exchange != 0);
unknown's avatar
unknown committed
6632 6633
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
unknown's avatar
unknown committed
6634
	  }
unknown's avatar
unknown committed
6635
        | ENCLOSED BY text_string
6636
          {
6637
            DBUG_ASSERT(Lex->exchange != 0);
6638 6639
            Lex->exchange->enclosed= $3;
          }
unknown's avatar
unknown committed
6640
        | ESCAPED BY text_string
6641
          {
6642
            DBUG_ASSERT(Lex->exchange != 0);
6643 6644
            Lex->exchange->escaped= $3;
          };
unknown's avatar
unknown committed
6645 6646 6647

opt_line_term:
	/* empty */
unknown's avatar
unknown committed
6648
	| LINES line_term_list;
unknown's avatar
unknown committed
6649 6650 6651

line_term_list:
	line_term_list line_term
unknown's avatar
unknown committed
6652
	| line_term;
unknown's avatar
unknown committed
6653 6654

line_term:
unknown's avatar
unknown committed
6655
        TERMINATED BY text_string
6656
          {
6657
            DBUG_ASSERT(Lex->exchange != 0);
6658 6659
            Lex->exchange->line_term= $3;
          }
unknown's avatar
unknown committed
6660
        | STARTING BY text_string
6661
          {
6662
            DBUG_ASSERT(Lex->exchange != 0);
6663 6664
            Lex->exchange->line_start= $3;
          };
unknown's avatar
unknown committed
6665 6666 6667

opt_ignore_lines:
	/* empty */
unknown's avatar
unknown committed
6668 6669
        | IGNORE_SYM NUM LINES
          {
6670
            DBUG_ASSERT(Lex->exchange != 0);
6671 6672
            Lex->exchange->skip_lines= atol($2.str);
          };
unknown's avatar
unknown committed
6673

unknown's avatar
unknown committed
6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696
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); }
        ;
        
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
6697 6698 6699
/* Common definitions */

text_literal:
6700
	TEXT_STRING_literal
unknown's avatar
unknown committed
6701 6702
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
6703
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
unknown's avatar
unknown committed
6704
	}
unknown's avatar
unknown committed
6705
	| NCHAR_STRING
6706
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
6707
	| UNDERSCORE_CHARSET TEXT_STRING
6708
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
6709
	| text_literal TEXT_STRING_literal
unknown's avatar
unknown committed
6710 6711
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
unknown's avatar
unknown committed
6712 6713

text_string:
6714
	TEXT_STRING_literal
unknown's avatar
unknown committed
6715
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
unknown's avatar
unknown committed
6716 6717
	| HEX_NUM
	  {
unknown's avatar
unknown committed
6718
	    Item *tmp= new Item_hex_string($1.str, $1.length);
6719
	    /*
unknown's avatar
unknown committed
6720
	      it is OK only emulate fix_fields, because we need only
6721 6722 6723
              value of constant
	    */
	    $$= tmp ?
unknown's avatar
unknown committed
6724
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6725
	      (String*) 0;
6726
	  }
unknown's avatar
unknown committed
6727 6728 6729 6730 6731 6732 6733 6734 6735 6736
        | 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;
          }
6737 6738
	;

unknown's avatar
unknown committed
6739
param_marker:
6740
        '?'
unknown's avatar
unknown committed
6741
        {
6742 6743 6744
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
          if (thd->command == COM_PREPARE)
6745
          {
6746 6747 6748 6749
            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
6750
	      my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
6751 6752
	      YYABORT;
            }
unknown's avatar
unknown committed
6753
          }
6754
          else
unknown's avatar
unknown committed
6755
          {
6756
            yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
6757 6758
            YYABORT;
          }
6759 6760 6761
        }
	;

unknown's avatar
unknown committed
6762 6763 6764
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
6765 6766
	| '-' NUM_literal
	  {
6767
	    $2->max_length++;
unknown's avatar
unknown committed
6768
	    $$= $2->neg();
6769
	  }
unknown's avatar
unknown committed
6770 6771 6772
	;


unknown's avatar
unknown committed
6773 6774
literal:
	text_literal	{ $$ =	$1; }
unknown's avatar
unknown committed
6775
	| NUM_literal	{ $$ = $1; }
unknown's avatar
unknown committed
6776
	| NULL_SYM	{ $$ =	new Item_null();
6777
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
unknown's avatar
unknown committed
6778 6779
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
6780
	| UNDERSCORE_CHARSET HEX_NUM
6781
	  {
unknown's avatar
unknown committed
6782
	    Item *tmp= new Item_hex_string($2.str, $2.length);
6783
	    /*
unknown's avatar
unknown committed
6784
	      it is OK only emulate fix_fieds, because we need only
6785 6786 6787
              value of constant
	    */
	    String *str= tmp ?
unknown's avatar
unknown committed
6788
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6789
	      (String*) 0;
unknown's avatar
unknown committed
6790 6791 6792
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
6793
	  }
unknown's avatar
unknown committed
6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807
	| 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
6808 6809
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
unknown's avatar
unknown committed
6810
	| TIMESTAMP text_literal { $$ = $2; };
unknown's avatar
unknown committed
6811

6812
NUM_literal:
6813 6814
	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); }
6815
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
unknown's avatar
unknown committed
6816
        | DECIMAL_NUM
6817
	{
unknown's avatar
unknown committed
6818
           $$= new Item_decimal($1.str, $1.length, YYTHD->charset());
6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
6832 6833
	;
	
unknown's avatar
unknown committed
6834 6835 6836 6837 6838
/**********************************************************************
** Createing different items.
**********************************************************************/

insert_ident:
6839
	simple_ident_nospvar { $$=$1; }
unknown's avatar
unknown committed
6840
	| table_wild	 { $$=$1; };
unknown's avatar
unknown committed
6841 6842

table_wild:
unknown's avatar
unknown committed
6843
	ident '.' '*'
6844 6845
	{
	  $$ = new Item_field(NullS,$1.str,"*");
unknown's avatar
unknown committed
6846
	  Lex->current_select->with_wild++;
6847
	}
unknown's avatar
unknown committed
6848
	| ident '.' ident '.' '*'
6849 6850 6851 6852
	{
	  $$ = new Item_field((YYTHD->client_capabilities &
   			     CLIENT_NO_SCHEMA ? NullS : $1.str),
			     $3.str,"*");
unknown's avatar
unknown committed
6853
	  Lex->current_select->with_wild++;
6854 6855
	}
	;
unknown's avatar
unknown committed
6856 6857

order_ident:
unknown's avatar
unknown committed
6858
	expr { $$=$1; };
unknown's avatar
unknown committed
6859 6860

simple_ident:
6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885
	ident
	{
	  sp_pvar_t *spv;
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;

	  if (spc && (spv = spc->find_pvar(&$1)))
	  { /* We're compiling a stored procedure and found a variable */
	    $$ = (Item*) new Item_splocal($1, spv->offset);
            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) ?
                 (Item*) new Item_field(NullS,NullS,$1.str) :
	         (Item*) new Item_ref(NullS,NullS,$1.str);
	  }
        }
        | simple_ident_q { $$= $1; }
	;

simple_ident_nospvar:
unknown's avatar
unknown committed
6886
	ident
unknown's avatar
unknown committed
6887
	{
unknown's avatar
unknown committed
6888
	  SELECT_LEX *sel=Select;
6889
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
6890 6891
	       sel->get_in_sum_expr() > 0) ?
              (Item*) new Item_field(NullS,NullS,$1.str) :
6892
	      (Item*) new Item_ref(NullS,NullS,$1.str);
unknown's avatar
unknown committed
6893
	}
6894 6895 6896 6897 6898
	| simple_ident_q { $$= $1; }
	;	

simple_ident_q:
	ident '.' ident
unknown's avatar
unknown committed
6899
	{
unknown's avatar
unknown committed
6900
	  THD *thd= YYTHD;
6901
	  LEX *lex= thd->lex;
6902 6903 6904 6905 6906 6907 6908 6909 6910 6911
         
          /*
            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 &&
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") || 
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
6912
            Item_trigger_field *trg_fld;
6913 6914 6915 6916 6917
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
            
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
unknown's avatar
unknown committed
6918
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
6919 6920 6921 6922 6923 6924
              YYABORT;
            }
            
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
unknown's avatar
unknown committed
6925
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
6926 6927 6928
              YYABORT;
            }
            
6929 6930 6931 6932
            if (!(trg_fld= new Item_trigger_field(new_row ?
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
                                                  $3.str)))
6933
              YYABORT;
6934 6935 6936 6937 6938 6939 6940
          
            /*
              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);
6941 6942 6943 6944 6945 6946 6947 6948
            
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
6949 6950
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
6951 6952 6953 6954 6955 6956 6957
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
	        (Item*) new Item_field(NullS,$1.str,$3.str) :
	        (Item*) new Item_ref(NullS,$1.str,$3.str);
          }
        }
unknown's avatar
unknown committed
6958
	| '.' ident '.' ident
unknown's avatar
unknown committed
6959
	{
unknown's avatar
unknown committed
6960
	  THD *thd= YYTHD;
6961
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
6962
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
6963 6964
	  if (sel->no_table_names_allowed)
	  {
6965 6966
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $2.str, thd->where);
unknown's avatar
unknown committed
6967
	  }
6968
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
6969 6970
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field(NullS,$2.str,$4.str) :
6971
              (Item*) new Item_ref(NullS, $2.str, $4.str);
unknown's avatar
unknown committed
6972
	}
unknown's avatar
unknown committed
6973
	| ident '.' ident '.' ident
unknown's avatar
unknown committed
6974
	{
unknown's avatar
unknown committed
6975
	  THD *thd= YYTHD;
6976
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
6977
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
6978 6979
	  if (sel->no_table_names_allowed)
	  {
6980 6981
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $3.str, thd->where);
unknown's avatar
unknown committed
6982
	  }
6983
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
6984 6985 6986 6987
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field((YYTHD->client_capabilities &
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
6988 6989
	      (Item*) new Item_ref((YYTHD->client_capabilities &
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
unknown's avatar
unknown committed
6990
                                   $3.str, $5.str);
unknown's avatar
unknown committed
6991
	};
unknown's avatar
unknown committed
6992 6993 6994 6995


field_ident:
	ident			{ $$=$1;}
6996
	| ident '.' ident	{ $$=$3;}	/* Skip schema name in create*/
unknown's avatar
unknown committed
6997
	| '.' ident		{ $$=$2;}	/* For Delphi */;
unknown's avatar
unknown committed
6998 6999 7000

table_ident:
	ident			{ $$=new Table_ident($1); }
unknown's avatar
unknown committed
7001
	| ident '.' ident	{ $$=new Table_ident(YYTHD, $1,$3,0);}
7002 7003 7004
	| '.' ident		{ $$=new Table_ident($2);} /* For Delphi */
        ;

unknown's avatar
unknown committed
7005
table_ident_nodb:
unknown's avatar
unknown committed
7006
	ident			{ LEX_STRING db={(char*) any_db,3}; $$=new Table_ident(YYTHD, db,$1,0); }
7007
        ;
unknown's avatar
unknown committed
7008

unknown's avatar
unknown committed
7009
IDENT_sys:
7010 7011 7012 7013 7014
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
7015 7016 7017 7018 7019 7020 7021
            {
              CHARSET_INFO *cs= system_charset_info;
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
                                                   $1.length);
              if (wlen < $1.length)
              {
7022 7023
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
7024 7025
                YYABORT;
              }
7026
	      $$= $1;
7027
            }
7028 7029 7030 7031
	    else
	      thd->convert_string(&$$, system_charset_info,
				  $1.str, $1.length, thd->charset());
	  }
unknown's avatar
unknown committed
7032 7033 7034 7035 7036 7037
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7038 7039
	  if (thd->charset_is_system_charset)
	    $$= $1;
unknown's avatar
unknown committed
7040
	  else
unknown's avatar
unknown committed
7041 7042
	    thd->convert_string(&$$, system_charset_info,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
7043 7044 7045
	}
	;

7046
TEXT_STRING_literal:
unknown's avatar
unknown committed
7047 7048 7049
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7050 7051
	  if (thd->charset_is_collation_connection)
	    $$= $1;
unknown's avatar
unknown committed
7052
	  else
unknown's avatar
unknown committed
7053 7054
	    thd->convert_string(&$$, thd->variables.collation_connection,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
7055 7056 7057 7058
	}
	;


unknown's avatar
unknown committed
7059
ident:
unknown's avatar
unknown committed
7060
	IDENT_sys	    { $$=$1; }
unknown's avatar
unknown committed
7061 7062
	| keyword
	{
unknown's avatar
unknown committed
7063 7064 7065
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
7066 7067
	}
	;
unknown's avatar
unknown committed
7068 7069

ident_or_text:
unknown's avatar
unknown committed
7070 7071 7072
	ident 			{ $$=$1;}
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
unknown's avatar
unknown committed
7073 7074 7075 7076

user:
	ident_or_text
	{
unknown's avatar
unknown committed
7077 7078
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
7079
	    YYABORT;
7080 7081 7082 7083
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
unknown's avatar
unknown committed
7084 7085
	| ident_or_text '@' ident_or_text
	  {
unknown's avatar
unknown committed
7086 7087
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
7088 7089
	      YYABORT;
	    $$->user = $1; $$->host=$3;
7090
	  }
unknown's avatar
unknown committed
7091
	| CURRENT_USER optional_braces
7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108
	{
          THD *thd= YYTHD;
          if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
            YYABORT;
          $$->user.str= thd->priv_user;
          $$->user.length= strlen(thd->priv_user);
          if (*thd->priv_host != 0)
          {
            $$->host.str= thd->priv_host;
            $$->host.length= strlen(thd->priv_host);
          }
          else
          {
            $$->host.str= (char *) "%";
            $$->host.length= 1;
          }
	};
unknown's avatar
unknown committed
7109 7110 7111 7112 7113

/* Keyword that we allow for identifiers */

keyword:
	ACTION			{}
unknown's avatar
unknown committed
7114
	| ADDDATE_SYM		{}
unknown's avatar
unknown committed
7115
	| AFTER_SYM		{}
7116
	| AGAINST		{}
unknown's avatar
unknown committed
7117
	| AGGREGATE_SYM		{}
7118
	| ALGORITHM_SYM		{}
unknown's avatar
unknown committed
7119
	| ANY_SYM		{}
unknown's avatar
unknown committed
7120
	| ASCII_SYM		{}
unknown's avatar
unknown committed
7121
	| AUTO_INC		{}
unknown's avatar
unknown committed
7122 7123
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
unknown's avatar
unknown committed
7124
	| BACKUP_SYM		{}
unknown's avatar
unknown committed
7125
	| BEGIN_SYM		{}
unknown's avatar
unknown committed
7126
	| BERKELEY_DB_SYM	{}
unknown's avatar
unknown committed
7127
	| BINLOG_SYM		{}
unknown's avatar
unknown committed
7128 7129
	| BIT_SYM		{}
	| BOOL_SYM		{}
unknown's avatar
unknown committed
7130
	| BOOLEAN_SYM		{}
7131
	| BYTE_SYM		{}
unknown's avatar
unknown committed
7132
	| BTREE_SYM		{}
unknown's avatar
unknown committed
7133
	| CACHE_SYM		{}
7134
	| CASCADED              {}
unknown's avatar
unknown committed
7135
	| CHAIN_SYM		{}
7136
	| CHANGED		{}
7137
	| CHARSET		{}
unknown's avatar
unknown committed
7138
	| CHECKSUM_SYM		{}
7139
	| CIPHER_SYM		{}
unknown's avatar
unknown committed
7140
	| CLIENT_SYM		{}
7141
	| CLOSE_SYM		{}
unknown's avatar
unknown committed
7142
	| COLLATION_SYM		{}
7143
        | COLUMNS               {}
unknown's avatar
unknown committed
7144
	| COMMENT_SYM		{}
unknown's avatar
unknown committed
7145
	| COMMITTED_SYM		{}
unknown's avatar
unknown committed
7146
	| COMMIT_SYM		{}
7147
	| COMPACT_SYM		{}
unknown's avatar
unknown committed
7148
	| COMPRESSED_SYM	{}
unknown's avatar
unknown committed
7149
	| CONCURRENT		{}
7150
	| CONSISTENT_SYM	{}
7151
	| CONTAINS_SYM          {}
7152
	| CUBE_SYM		{}
unknown's avatar
unknown committed
7153 7154 7155 7156
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
unknown's avatar
unknown committed
7157
        | DEALLOCATE_SYM        {}
7158
	| DEFINER_SYM		{}
unknown's avatar
unknown committed
7159
	| DELAY_KEY_WRITE_SYM	{}
unknown's avatar
unknown committed
7160 7161
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
unknown's avatar
unknown committed
7162
	| DISCARD		{}
unknown's avatar
unknown committed
7163
	| DO_SYM		{}
unknown's avatar
unknown committed
7164 7165
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
unknown's avatar
unknown committed
7166 7167 7168
	| DYNAMIC_SYM		{}
	| END			{}
	| ENUM			{}
unknown's avatar
unknown committed
7169
	| ENGINE_SYM		{}
unknown's avatar
unknown committed
7170
	| ENGINES_SYM		{}
7171
	| ERRORS		{}
unknown's avatar
unknown committed
7172
	| ESCAPE_SYM		{}
unknown's avatar
unknown committed
7173
	| EVENTS_SYM		{}
unknown's avatar
unknown committed
7174
	| EXECUTE_SYM		{}
7175
        | EXPANSION_SYM         {}
unknown's avatar
unknown committed
7176
	| EXTENDED_SYM		{}
7177
	| FAST_SYM		{}
7178
	| FOUND_SYM		{}
unknown's avatar
unknown committed
7179 7180
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
unknown's avatar
unknown committed
7181
	| FULL			{}
unknown's avatar
unknown committed
7182 7183 7184 7185
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
	| FLUSH_SYM		{}
7186
	| FRAC_SECOND_SYM	{}
7187
	| GEOMETRY_SYM		{}
7188
	| GEOMETRYCOLLECTION	{}
7189
	| GET_FORMAT		{}
unknown's avatar
unknown committed
7190
	| GRANTS		{}
unknown's avatar
unknown committed
7191
	| GLOBAL_SYM		{}
7192
	| HANDLER_SYM		{}
unknown's avatar
unknown committed
7193
	| HASH_SYM		{}
7194
	| HELP_SYM		{}
unknown's avatar
unknown committed
7195 7196 7197
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
7198
	| INVOKER_SYM		{}
unknown's avatar
unknown committed
7199
	| IMPORT		{}
unknown's avatar
unknown committed
7200
	| INDEXES		{}
unknown's avatar
unknown committed
7201
	| ISOLATION		{}
unknown's avatar
unknown committed
7202
	| ISSUER_SYM		{}
unknown's avatar
unknown committed
7203
	| INNOBASE_SYM		{}
7204
	| INSERT_METHOD		{}
unknown's avatar
unknown committed
7205
	| RELAY_THREAD		{}
7206 7207
	| LABEL_SYM             {}
	| LANGUAGE_SYM          {}
7208
	| LAST_SYM		{}
unknown's avatar
unknown committed
7209
	| LEAVES                {}
unknown's avatar
unknown committed
7210
	| LEVEL_SYM		{}
7211
	| LINESTRING		{}
unknown's avatar
unknown committed
7212
	| LOCAL_SYM		{}
unknown's avatar
unknown committed
7213
	| LOCKS_SYM		{}
unknown's avatar
unknown committed
7214 7215 7216 7217 7218 7219 7220 7221 7222
	| 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
7223
	| MASTER_SERVER_ID_SYM  {}
unknown's avatar
unknown committed
7224
	| MASTER_CONNECT_RETRY_SYM	{}
unknown's avatar
unknown committed
7225 7226 7227 7228 7229 7230
	| 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
7231 7232 7233
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
7234
	| MAX_USER_CONNECTIONS_SYM {}
7235
	| MEDIUM_SYM		{}
7236
	| MERGE_SYM		{}
unknown's avatar
unknown committed
7237
	| MICROSECOND_SYM	{}
7238
        | MIGRATE_SYM           {}
unknown's avatar
unknown committed
7239 7240 7241
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
unknown's avatar
unknown committed
7242
	| MODE_SYM		{}
unknown's avatar
unknown committed
7243
	| MONTH_SYM		{}
7244 7245 7246
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
unknown's avatar
unknown committed
7247
  | MUTEX_SYM   {}
7248
	| NAME_SYM              {}
unknown's avatar
unknown committed
7249
	| NAMES_SYM		{}
unknown's avatar
unknown committed
7250 7251
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
unknown's avatar
unknown committed
7252
	| NDBCLUSTER_SYM	{}
7253
	| NEXT_SYM		{}
unknown's avatar
unknown committed
7254
	| NEW_SYM		{}
unknown's avatar
unknown committed
7255
	| NO_SYM		{}
7256
	| NONE_SYM		{}
unknown's avatar
unknown committed
7257
	| NVARCHAR_SYM		{}
unknown's avatar
unknown committed
7258
	| OFFSET_SYM		{}
unknown's avatar
unknown committed
7259
	| OLD_PASSWORD		{}
7260
	| ONE_SHOT_SYM		{}
7261
        | ONE_SYM               {}
7262
	| OPEN_SYM		{}
unknown's avatar
unknown committed
7263
	| PACK_KEYS_SYM		{}
7264
	| PARTIAL		{}
unknown's avatar
unknown committed
7265
	| PASSWORD		{}
7266
        | PHASE_SYM             {}
7267
	| POINT_SYM		{}
7268
	| POLYGON		{}
unknown's avatar
unknown committed
7269
        | PREPARE_SYM           {}
7270
	| PREV_SYM		{}
7271
        | PRIVILEGES            {}
unknown's avatar
unknown committed
7272 7273
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
7274
	| QUARTER_SYM		{}
unknown's avatar
unknown committed
7275
	| QUERY_SYM		{}
7276
	| QUICK			{}
unknown's avatar
unknown committed
7277
	| RAID_0_SYM		{}
unknown's avatar
unknown committed
7278 7279
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
unknown's avatar
unknown committed
7280
	| RAID_STRIPED_SYM	{}
unknown's avatar
unknown committed
7281
	| RAID_TYPE		{}
7282
        | RECOVER_SYM           {}
unknown's avatar
unknown committed
7283
        | REDUNDANT_SYM         {}
unknown's avatar
unknown committed
7284 7285
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
unknown's avatar
unknown committed
7286 7287
	| RELOAD		{}
	| REPAIR		{}
unknown's avatar
unknown committed
7288
	| REPEATABLE_SYM	{}
unknown's avatar
unknown committed
7289
	| REPLICATION		{}
unknown's avatar
unknown committed
7290
	| RESET_SYM		{}
7291
	| RESOURCES		{}
unknown's avatar
unknown committed
7292
	| RESTORE_SYM		{}
7293
        | RESUME_SYM            {}
7294
	| RETURNS_SYM           {}
unknown's avatar
unknown committed
7295
	| ROLLBACK_SYM		{}
7296
	| ROLLUP_SYM		{}
7297
	| ROUTINE_SYM		{}
unknown's avatar
unknown committed
7298 7299 7300
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
unknown's avatar
unknown committed
7301
	| RTREE_SYM		{}
7302
	| SAVEPOINT_SYM		{}
unknown's avatar
unknown committed
7303
	| SECOND_SYM		{}
7304
	| SECURITY_SYM		{}
7305
	| SERIAL_SYM		{}
unknown's avatar
unknown committed
7306 7307
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
unknown's avatar
unknown committed
7308
	| SIGNED_SYM		{}
7309
	| SIMPLE_SYM		{}
unknown's avatar
unknown committed
7310
	| SHARE_SYM		{}
unknown's avatar
unknown committed
7311
	| SHUTDOWN		{}
unknown's avatar
unknown committed
7312
	| SLAVE			{}
7313
	| SNAPSHOT_SYM		{}
unknown's avatar
unknown committed
7314
	| SOUNDS_SYM		{}
unknown's avatar
unknown committed
7315
	| SQL_CACHE_SYM		{}
unknown's avatar
unknown committed
7316
	| SQL_BUFFER_RESULT	{}
unknown's avatar
unknown committed
7317
	| SQL_NO_CACHE_SYM	{}
unknown's avatar
unknown committed
7318
	| SQL_THREAD		{}
unknown's avatar
unknown committed
7319 7320 7321
	| START_SYM		{}
	| STATUS_SYM		{}
	| STOP_SYM		{}
unknown's avatar
unknown committed
7322
	| STORAGE_SYM		{}
unknown's avatar
unknown committed
7323
	| STRING_SYM		{}
unknown's avatar
unknown committed
7324
	| SUBDATE_SYM		{}
unknown's avatar
unknown committed
7325
	| SUBJECT_SYM		{}
unknown's avatar
unknown committed
7326
	| SUPER_SYM		{}
7327
        | SUSPEND_SYM           {}
7328
        | TABLES                {}
unknown's avatar
unknown committed
7329
	| TABLESPACE		{}
unknown's avatar
unknown committed
7330
	| TEMPORARY		{}
7331
	| TEMPTABLE_SYM		{}
unknown's avatar
unknown committed
7332
	| TEXT_SYM		{}
unknown's avatar
unknown committed
7333
	| TRANSACTION_SYM	{}
7334
	| TRUNCATE_SYM		{}
unknown's avatar
unknown committed
7335
	| TIMESTAMP		{}
7336 7337
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
unknown's avatar
unknown committed
7338 7339
	| TIME_SYM		{}
	| TYPE_SYM		{}
unknown's avatar
unknown committed
7340
	| TYPES_SYM		{}
unknown's avatar
unknown committed
7341
        | UDF_RETURNS_SYM       {}
7342
	| FUNCTION_SYM		{}
unknown's avatar
unknown committed
7343
	| UNCOMMITTED_SYM	{}
7344
	| UNDEFINED_SYM		{}
unknown's avatar
unknown committed
7345
	| UNICODE_SYM		{}
7346
	| UNKNOWN_SYM		{}
7347
	| UNTIL_SYM		{}
7348
	| USER			{}
unknown's avatar
unknown committed
7349
	| USE_FRM		{}
unknown's avatar
unknown committed
7350
	| VARIABLES		{}
7351
	| VIEW_SYM		{}
7352
	| VALUE_SYM		{}
7353
	| WARNINGS		{}
7354
	| WEEK_SYM		{}
unknown's avatar
unknown committed
7355
	| WORK_SYM		{}
7356
	| X509_SYM		{}
7357
        | XA_SYM                {}
unknown's avatar
unknown committed
7358 7359
	| YEAR_SYM		{}
	;
unknown's avatar
unknown committed
7360 7361 7362 7363 7364 7365

/* Option functions */

set:
	SET opt_option
	{
unknown's avatar
unknown committed
7366 7367
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
unknown's avatar
unknown committed
7368
	  mysql_init_select(lex);
7369
	  lex->option_type=OPT_SESSION;
unknown's avatar
unknown committed
7370
	  lex->var_list.empty();
7371
          lex->one_shot_set= 0;
unknown's avatar
unknown committed
7372
	}
7373 7374 7375
	option_value_list
	{}
	;
unknown's avatar
unknown committed
7376 7377 7378

opt_option:
	/* empty */ {}
unknown's avatar
unknown committed
7379
	| OPTION {};
unknown's avatar
unknown committed
7380 7381

option_value_list:
7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411
	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
              and sp_instr_set_trigger instructions share one LEX. 
              (Well, it is theoretically possible but adds some extra 
               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;
            
            /* 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;
          }
        }
unknown's avatar
unknown committed
7412
	option_type option_value
7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450
        {
          LEX *lex= Lex;
          
          if (lex->sphead)
          {
            sp_head *sp= lex->sphead;
            
	    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;
              
              if (!(i= new sp_instr_stmt(sp->instructions(), lex->spcont,
                                         lex)))
                YYABORT;
                
              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;
              
              if (!(qbuff.str= alloc_root(YYTHD->mem_root, qbuff.length + 5)))
                YYABORT;
                
              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
7451 7452 7453 7454 7455 7456

option_type:
	/* empty */	{}
	| GLOBAL_SYM	{ Lex->option_type= OPT_GLOBAL; }
	| LOCAL_SYM	{ Lex->option_type= OPT_SESSION; }
	| SESSION_SYM	{ Lex->option_type= OPT_SESSION; }
7457
	| ONE_SHOT_SYM	{ Lex->option_type= OPT_SESSION; Lex->one_shot_set= 1; }
unknown's avatar
unknown committed
7458 7459 7460 7461
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
unknown's avatar
unknown committed
7462
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
7463 7464 7465 7466 7467 7468
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

opt_var_ident_type:
	/* empty */		{ $$=OPT_DEFAULT; }
unknown's avatar
unknown committed
7469
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
7470 7471 7472
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
unknown's avatar
unknown committed
7473 7474

option_value:
7475 7476
	  '@' ident_or_text equal expr
	  {
7477
            Lex->var_list.push_back(new set_var_user(new Item_func_set_user_var($2,$4)));
7478
	  }
unknown's avatar
unknown committed
7479
	| internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
7480
	  {
unknown's avatar
unknown committed
7481
	    LEX *lex=Lex;
7482 7483 7484 7485 7486 7487 7488 7489

            if ($1.var == &trg_new_row_fake_var)
            {
              /* We are in trigger and assigning value to field of new row */
              Item *it;
              sp_instr_set_trigger_field *i;
              if (lex->query_tables)
              {
unknown's avatar
unknown committed
7490 7491
                my_message(ER_SP_SUBSELECT_NYI, ER(ER_SP_SUBSELECT_NYI),
                           MYF(0));
7492 7493 7494 7495 7496 7497 7498 7499 7500
                YYABORT;
              }
              if ($3)
                it= $3;
              else
              {
                /* QQ: Shouldn't this be field's default value ? */
                it= new Item_null();
              }
7501 7502 7503 7504
              
              if (!(i= new sp_instr_set_trigger_field(
                             lex->sphead->instructions(), lex->spcont,
                             $1.base_name, it)))
7505
                YYABORT;
7506 7507 7508 7509 7510 7511 7512 7513
              
              /*
                Let us add this item to list of all Item_trigger_field
                objects in trigger.
              */
              lex->trg_table_fields.link_in_list((byte *)&i->trigger_field,
                     (byte **)&i->trigger_field.next_trg_field);

7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537
              lex->sphead->add_instr(i);
            }
            else if ($1.var)
	    { /* System variable */
	      lex->var_list.push_back(new set_var(lex->option_type, $1.var,
						  &$1.base_name, $3));
	    }
            else
	    {
	      /* An SP local variable */
	      sp_pcontext *ctx= lex->spcont;
	      sp_pvar_t *spv;
              sp_instr_set *i;
	      Item *it;

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

	      if ($3)
	        it= $3;
	      else if (spv->dflt)
	        it= spv->dflt;
	      else
	        it= new Item_null();
              i= new sp_instr_set(lex->sphead->instructions(), ctx,
7538
	                          spv->offset, it, spv->type, lex, TRUE);
7539 7540 7541
	      lex->sphead->add_instr(i);
	      spv->isset= TRUE;
	    }
unknown's avatar
unknown committed
7542
	  }
unknown's avatar
unknown committed
7543
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
7544 7545
	  {
	    LEX *lex=Lex;
unknown's avatar
unknown committed
7546 7547
	    lex->var_list.push_back(new set_var((enum_var_type) $3, $4.var,
						&$4.base_name, $6));
unknown's avatar
unknown committed
7548 7549 7550 7551 7552
	  }
	| TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	  {
	    LEX *lex=Lex;
	    lex->var_list.push_back(new set_var(lex->option_type,
unknown's avatar
unknown committed
7553
						find_sys_var("tx_isolation"),
7554
						&null_lex_str,
unknown's avatar
unknown committed
7555
						new Item_int((int32) $4)));
unknown's avatar
unknown committed
7556
	  }
7557
	| charset old_or_new_charset_name_or_default
unknown's avatar
unknown committed
7558
	{
unknown's avatar
unknown committed
7559
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7560
	  LEX *lex= Lex;
7561
	  $2= $2 ? $2: global_system_variables.character_set_client;
7562
	  lex->var_list.push_back(new set_var_collation_client($2,thd->variables.collation_database,$2));
unknown's avatar
unknown committed
7563
	}
7564
	| NAMES_SYM charset_name_or_default opt_collate
7565
	{
unknown's avatar
unknown committed
7566
	  LEX *lex= Lex;
7567
	  $2= $2 ? $2 : global_system_variables.character_set_client;
7568 7569
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
7570
	  {
7571 7572
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
unknown's avatar
unknown committed
7573
	    YYABORT;
7574
	  }
7575
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
7576
	}
unknown's avatar
unknown committed
7577
	| PASSWORD equal text_or_password
unknown's avatar
unknown committed
7578
	  {
7579
	    THD *thd=YYTHD;
unknown's avatar
unknown committed
7580
	    LEX_USER *user;
unknown's avatar
unknown committed
7581
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
unknown's avatar
unknown committed
7582
	      YYABORT;
7583
	    user->host=null_lex_str;
unknown's avatar
unknown committed
7584
	    user->user.str=thd->priv_user;
7585
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
unknown's avatar
unknown committed
7586 7587
	  }
	| PASSWORD FOR_SYM user equal text_or_password
unknown's avatar
unknown committed
7588
	  {
unknown's avatar
unknown committed
7589 7590
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
7591
	;
unknown's avatar
unknown committed
7592

unknown's avatar
unknown committed
7593 7594 7595
internal_variable_name:
	ident
	{
7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607
	  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;
7608
	    $$.base_name= null_lex_str;
7609 7610 7611 7612
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
7613 7614 7615
            if (tmp == &sys_time_zone &&
                lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
7616 7617 7618 7619 7620 7621 7622
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
unknown's avatar
unknown committed
7623
	}
7624 7625
	| ident '.' ident
	  {
7626
            LEX *lex= Lex;
7627 7628
            if (check_reserved_words(&$1))
            {
7629
	      yyerror(ER(ER_SYNTAX_ERROR));
7630 7631
              YYABORT;
            }
7632 7633 7634 7635 7636 7637
            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
7638
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
7639 7640 7641 7642
                YYABORT;
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
unknown's avatar
unknown committed
7643 7644
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
7645 7646 7647 7648
                YYABORT;
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
unknown's avatar
unknown committed
7649
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661
                YYABORT;
              }
              /* This special combination will denote field of NEW row */
              $$.var= &trg_new_row_fake_var;
              $$.base_name= $3;
            }
            else
            {
              sys_var *tmp=find_sys_var($3.str, $3.length);
              if (!tmp)
                YYABORT;
              if (!tmp->is_struct())
7662
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
7663 7664 7665
              $$.var= tmp;
              $$.base_name= $1;
            }
7666 7667 7668 7669 7670 7671 7672
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
7673
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
unknown's avatar
unknown committed
7674 7675 7676
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
7677
	  }
7678
        ;
unknown's avatar
unknown committed
7679 7680 7681 7682 7683 7684 7685

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

unknown's avatar
unknown committed
7687 7688 7689 7690
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
7691
	    $$= $3.length ? YYTHD->variables.old_passwords ?
unknown's avatar
unknown committed
7692 7693 7694 7695 7696 7697 7698 7699
	        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;
7700 7701
	  }
          ;
unknown's avatar
unknown committed
7702

unknown's avatar
unknown committed
7703

unknown's avatar
unknown committed
7704
set_expr_or_default:
unknown's avatar
unknown committed
7705 7706
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
7707 7708
	| ON	  { $$=new Item_string("ON",  2, system_charset_info); }
	| ALL	  { $$=new Item_string("ALL", 3, system_charset_info); }
unknown's avatar
unknown committed
7709
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
unknown's avatar
unknown committed
7710
	;
unknown's avatar
unknown committed
7711 7712


unknown's avatar
unknown committed
7713 7714 7715 7716 7717
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
7718 7719 7720 7721 7722 7723 7724 7725
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
	    my_message(ER_SP_BADSTATEMENT, ER(ER_SP_BADSTATEMENT), MYF(0));
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_LOCK_TABLES;
unknown's avatar
unknown committed
7726
	}
7727 7728
	table_lock_list
	{}
7729
	;
unknown's avatar
unknown committed
7730 7731 7732

table_or_tables:
	TABLE_SYM
unknown's avatar
unknown committed
7733
	| TABLES;
unknown's avatar
unknown committed
7734 7735 7736

table_lock_list:
	table_lock
unknown's avatar
unknown committed
7737
	| table_lock_list ',' table_lock;
unknown's avatar
unknown committed
7738 7739 7740

table_lock:
	table_ident opt_table_alias lock_option
7741
	{
unknown's avatar
unknown committed
7742
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
7743 7744
	   YYABORT;
	}
7745
        ;
unknown's avatar
unknown committed
7746 7747 7748

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
7749
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
unknown's avatar
unknown committed
7750
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
7751 7752
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
unknown's avatar
unknown committed
7753 7754

unlock:
7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767
	UNLOCK_SYM
	{
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
	    my_message(ER_SP_BADSTATEMENT, ER(ER_SP_BADSTATEMENT), MYF(0));
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_UNLOCK_TABLES;
	}
	table_or_tables
	{}
7768
        ;
unknown's avatar
unknown committed
7769 7770


7771
/*
unknown's avatar
unknown committed
7772
** Handler: direct access to ISAM functions
7773 7774 7775 7776 7777
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
7778 7779
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_OPEN;
unknown's avatar
unknown committed
7780
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
7781 7782
	    YYABORT;
	}
unknown's avatar
unknown committed
7783
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
7784
	{
7785 7786
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_CLOSE;
unknown's avatar
unknown committed
7787
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
7788 7789
	    YYABORT;
	}
unknown's avatar
unknown committed
7790
	| HANDLER_SYM table_ident_nodb READ_SYM
7791
	{
unknown's avatar
unknown committed
7792 7793 7794
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
7795 7796
	  lex->current_select->select_limit= 1;
	  lex->current_select->offset_limit= 0L;
unknown's avatar
unknown committed
7797
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
7798
	    YYABORT;
unknown's avatar
unknown committed
7799
        }
7800
        handler_read_or_scan where_clause opt_limit_clause {}
7801
        ;
7802

unknown's avatar
unknown committed
7803
handler_read_or_scan:
7804 7805
	handler_scan_function         { Lex->ident= null_lex_str; }
        | ident handler_rkey_function { Lex->ident= $1; }
7806
        ;
unknown's avatar
unknown committed
7807 7808 7809

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
7810 7811
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
unknown's avatar
unknown committed
7812 7813 7814 7815 7816 7817

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;  }
7818 7819
	| handler_rkey_mode
	{
unknown's avatar
unknown committed
7820 7821
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
unknown's avatar
unknown committed
7822
	  lex->ha_rkey_mode=$1;
unknown's avatar
unknown committed
7823
	  if (!(lex->insert_list = new List_item))
7824
	    YYABORT;
7825 7826
	} '(' values ')' { }
        ;
7827 7828

handler_rkey_mode:
unknown's avatar
unknown committed
7829 7830 7831 7832
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
7833 7834
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
7835

unknown's avatar
unknown committed
7836 7837 7838
/* GRANT / REVOKE */

revoke:
7839
	REVOKE clear_privileges revoke_command
7840 7841 7842 7843
	{}
        ;

revoke_command:
7844 7845 7846 7847
	grant_privileges ON opt_table FROM grant_list
	{
	  Lex->sql_command = SQLCOM_REVOKE;
        }
7848
	|
7849
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
7850 7851 7852
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
7853
	;
unknown's avatar
unknown committed
7854 7855

grant:
7856
	GRANT clear_privileges grant_privileges ON opt_table TO_SYM grant_list
7857
	require_clause grant_options
7858
	{ Lex->sql_command= SQLCOM_GRANT; }
7859
	;
unknown's avatar
unknown committed
7860 7861

grant_privileges:
7862 7863 7864 7865 7866 7867
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
7868
        ;
unknown's avatar
unknown committed
7869

unknown's avatar
unknown committed
7870 7871 7872 7873 7874
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

7875 7876 7877
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
unknown's avatar
unknown committed
7878

7879
object_privilege:
unknown's avatar
unknown committed
7880
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
7881 7882 7883
	| 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
7884 7885
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
unknown's avatar
unknown committed
7886
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
unknown's avatar
unknown committed
7887 7888 7889
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
unknown's avatar
unknown committed
7890
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
unknown's avatar
unknown committed
7891 7892 7893 7894
	| 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
7895 7896 7897 7898 7899
	| 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; }
7900 7901 7902 7903
	| 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; }
7904 7905
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
unknown's avatar
unknown committed
7906
	;
unknown's avatar
unknown committed
7907

unknown's avatar
unknown committed
7908

7909 7910
opt_and:
	/* empty */	{}
unknown's avatar
unknown committed
7911
	| AND_SYM	{}
7912 7913 7914 7915 7916 7917 7918 7919 7920
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
unknown's avatar
unknown committed
7921 7922 7923 7924
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
unknown's avatar
unknown committed
7925
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
unknown's avatar
unknown committed
7926 7927 7928 7929 7930 7931 7932 7933 7934
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
unknown's avatar
unknown committed
7935
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
unknown's avatar
unknown committed
7936 7937 7938 7939 7940 7941 7942 7943 7944
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
unknown's avatar
unknown committed
7945
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
unknown's avatar
unknown committed
7946 7947 7948 7949 7950
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
7951

unknown's avatar
unknown committed
7952 7953 7954
opt_table:
	'*'
	  {
7955
	    LEX *lex= Lex;
unknown's avatar
unknown committed
7956
	    lex->current_select->db= lex->thd->db;
unknown's avatar
unknown committed
7957
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
7958 7959
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
7960
	    {
unknown's avatar
unknown committed
7961 7962
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
7963
	      YYABORT;
unknown's avatar
unknown committed
7964
	    }
unknown's avatar
unknown committed
7965 7966 7967
	  }
	| ident '.' '*'
	  {
7968
	    LEX *lex= Lex;
unknown's avatar
unknown committed
7969
	    lex->current_select->db = $1.str;
unknown's avatar
unknown committed
7970
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
7971 7972
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
7973
	    {
unknown's avatar
unknown committed
7974 7975
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
7976 7977 7978 7979 7980
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
7981
	    LEX *lex= Lex;
unknown's avatar
unknown committed
7982
	    lex->current_select->db = NULL;
unknown's avatar
unknown committed
7983 7984
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
unknown's avatar
unknown committed
7985
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
7986
	    {
unknown's avatar
unknown committed
7987 7988
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
7989 7990 7991 7992 7993
	      YYABORT;
	    }
	  }
	| table_ident
	  {
unknown's avatar
unknown committed
7994
	    LEX *lex=Lex;
unknown's avatar
unknown committed
7995
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
unknown's avatar
unknown committed
7996
	      YYABORT;
unknown's avatar
unknown committed
7997
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
7998
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
7999 8000
	  }
          ;
unknown's avatar
unknown committed
8001 8002 8003


user_list:
8004 8005 8006 8007 8008 8009 8010 8011 8012 8013
	user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;


grant_list:
8014
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
8015
	| grant_list ',' grant_user
8016 8017 8018 8019 8020
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
unknown's avatar
unknown committed
8021 8022 8023 8024 8025 8026 8027 8028


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
8029
             if (YYTHD->variables.old_passwords)
8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046
             {
               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
8047 8048 8049
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
8050
	  { $$= $1; $1->password= $5; }
unknown's avatar
unknown committed
8051
	| user
8052
	  { $$= $1; $1->password= null_lex_str; }
8053
        ;
unknown's avatar
unknown committed
8054 8055 8056


opt_column_list:
unknown's avatar
unknown committed
8057 8058 8059 8060 8061
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
unknown's avatar
unknown committed
8062
	| '(' column_list ')';
unknown's avatar
unknown committed
8063 8064 8065

column_list:
	column_list ',' column_list_id
unknown's avatar
unknown committed
8066
	| column_list_id;
unknown's avatar
unknown committed
8067 8068 8069 8070

column_list_id:
	ident
	{
unknown's avatar
unknown committed
8071
	  String *new_str = new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info);
unknown's avatar
unknown committed
8072 8073
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
unknown's avatar
unknown committed
8074
	  LEX *lex=Lex;
unknown's avatar
unknown committed
8075 8076
	  while ((point=iter++))
	  {
8077 8078
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
unknown's avatar
unknown committed
8079 8080
		break;
	  }
unknown's avatar
unknown committed
8081
	  lex->grant_tot_col|= lex->which_columns;
unknown's avatar
unknown committed
8082
	  if (point)
unknown's avatar
unknown committed
8083
	    point->rights |= lex->which_columns;
unknown's avatar
unknown committed
8084
	  else
unknown's avatar
unknown committed
8085
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
8086 8087
	}
        ;
unknown's avatar
unknown committed
8088

unknown's avatar
unknown committed
8089 8090

require_clause: /* empty */
8091
        | REQUIRE_SYM require_list
8092 8093 8094
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
8095
        | REQUIRE_SYM SSL_SYM
8096 8097 8098
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
8099
        | REQUIRE_SYM X509_SYM
8100 8101 8102 8103 8104 8105 8106
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
8107
          ;
unknown's avatar
unknown committed
8108

unknown's avatar
unknown committed
8109
grant_options:
unknown's avatar
unknown committed
8110
	/* empty */ {}
unknown's avatar
unknown committed
8111
	| WITH grant_option_list;
unknown's avatar
unknown committed
8112

unknown's avatar
unknown committed
8113 8114
grant_option_list:
	grant_option_list grant_option {}
8115 8116
	| grant_option {}
        ;
unknown's avatar
unknown committed
8117 8118 8119

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
8120
        | MAX_QUERIES_PER_HOUR ULONG_NUM
unknown's avatar
unknown committed
8121
        {
unknown's avatar
unknown committed
8122 8123 8124
	  LEX *lex=Lex;
	  lex->mqh.questions=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
8125
	}
8126
        | MAX_UPDATES_PER_HOUR ULONG_NUM
8127
        {
unknown's avatar
unknown committed
8128 8129 8130
	  LEX *lex=Lex;
	  lex->mqh.updates=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
8131
	}
8132
        | MAX_CONNECTIONS_PER_HOUR ULONG_NUM
8133
        {
unknown's avatar
unknown committed
8134 8135 8136
	  LEX *lex=Lex;
	  lex->mqh.conn_per_hour= $2;
	  lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
8137
	}
8138 8139
        | MAX_USER_CONNECTIONS_SYM ULONG_NUM
        {
unknown's avatar
unknown committed
8140 8141 8142
	  LEX *lex=Lex;
          lex->mqh.user_conn= $2;
          lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
8143 8144
	}
        ;
8145

unknown's avatar
unknown committed
8146
begin:
unknown's avatar
unknown committed
8147 8148 8149 8150 8151 8152 8153
	BEGIN_SYM  
        {
	  LEX *lex=Lex;
          lex->sql_command = SQLCOM_BEGIN;
          lex->start_transaction_opt= 0;
        }
        opt_work {}
8154
	;
unknown's avatar
unknown committed
8155 8156 8157

opt_work:
	/* empty */ {}
unknown's avatar
unknown committed
8158
	| WORK_SYM  {}
8159
        ;
unknown's avatar
unknown committed
8160

unknown's avatar
unknown committed
8161
opt_chain:
unknown's avatar
unknown committed
8162
	/* empty */ { $$= (YYTHD->variables.completion_type == 1); }
unknown's avatar
unknown committed
8163 8164 8165 8166 8167
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
unknown's avatar
unknown committed
8168
	/* empty */ { $$= (YYTHD->variables.completion_type == 2); }
unknown's avatar
unknown committed
8169 8170 8171 8172 8173 8174 8175 8176 8177
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

unknown's avatar
unknown committed
8178
commit:
unknown's avatar
unknown committed
8179
	COMMIT_SYM opt_work opt_chain opt_release
unknown's avatar
unknown committed
8180
	{
unknown's avatar
unknown committed
8181 8182 8183 8184
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_COMMIT;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
unknown's avatar
unknown committed
8185 8186
	}
	;
unknown's avatar
unknown committed
8187 8188

rollback:
unknown's avatar
unknown committed
8189
	ROLLBACK_SYM opt_work opt_chain opt_release
8190
	{
unknown's avatar
unknown committed
8191 8192 8193 8194
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
8195
	}
unknown's avatar
unknown committed
8196 8197
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
8198
	{
unknown's avatar
unknown committed
8199 8200 8201
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK_TO_SAVEPOINT;
	  lex->ident= $5;
unknown's avatar
unknown committed
8202 8203 8204
	}
	;

8205 8206 8207
savepoint:
	SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
8208 8209 8210
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SAVEPOINT;
	  lex->ident= $2;
unknown's avatar
unknown committed
8211 8212 8213 8214 8215 8216
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
8217 8218 8219
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RELEASE_SAVEPOINT;
	  lex->ident= $3;
unknown's avatar
unknown committed
8220 8221
	}
	;
unknown's avatar
unknown committed
8222
  
8223
/*
unknown's avatar
unknown committed
8224
   UNIONS : glue selects together
8225 8226 8227
*/


8228
union_clause:
8229
	/* empty */ {}
unknown's avatar
unknown committed
8230 8231
	| union_list
	;
8232 8233

union_list:
8234
	UNION_SYM union_option
8235 8236 8237 8238 8239
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
unknown's avatar
unknown committed
8240
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
8241
	    YYABORT;
8242
	  }
8243
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
8244
	  {
8245
            yyerror(ER(ER_SYNTAX_ERROR));
8246 8247
	    YYABORT;
	  }
8248
	  if (mysql_new_select(lex, 0))
8249
	    YYABORT;
unknown's avatar
unknown committed
8250
          mysql_init_select(lex);
8251
	  lex->current_select->linkage=UNION_TYPE;
8252 8253 8254
          if ($2) /* UNION DISTINCT - remember position */
            lex->current_select->master_unit()->union_distinct=
                                                      lex->current_select;
unknown's avatar
unknown committed
8255
	}
8256
	select_init {}
8257
	;
8258 8259

union_opt:
unknown's avatar
unknown committed
8260 8261 8262
	/* Empty */ { $$= 0; }
	| union_list { $$= 1; }
	| union_order_or_limit { $$= 1; }
unknown's avatar
unknown committed
8263
	;
8264

unknown's avatar
unknown committed
8265
union_order_or_limit:
8266
	  {
unknown's avatar
unknown committed
8267
	    THD *thd= YYTHD;
8268
	    LEX *lex= thd->lex;
unknown's avatar
unknown committed
8269
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
unknown's avatar
unknown committed
8270
	    SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
8271
	    SELECT_LEX_UNIT *unit= sel->master_unit();
unknown's avatar
unknown committed
8272
	    SELECT_LEX *fake= unit->fake_select_lex;
unknown's avatar
unknown committed
8273 8274 8275 8276 8277 8278
	    if (fake)
	    {
	      unit->global_parameters= fake;
	      fake->no_table_names_allowed= 1;
	      lex->current_select= fake;
	    }
unknown's avatar
unknown committed
8279
	    thd->where= "global ORDER clause";
8280
	  }
8281
	order_or_limit
unknown's avatar
unknown committed
8282 8283
          {
	    THD *thd= YYTHD;
8284
	    thd->lex->current_select->no_table_names_allowed= 0;
unknown's avatar
unknown committed
8285 8286
	    thd->where= "";
          }
8287 8288 8289
	;

order_or_limit:
8290 8291
	order_clause opt_limit_clause_init
	| limit_clause
8292
	;
unknown's avatar
unknown committed
8293 8294

union_option:
8295 8296 8297 8298
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
8299

unknown's avatar
unknown committed
8300 8301
singlerow_subselect:
	subselect_start singlerow_subselect_init
unknown's avatar
unknown committed
8302 8303 8304 8305
	subselect_end
	{
	  $$= $2;
	};
8306

unknown's avatar
unknown committed
8307
singlerow_subselect_init:
8308
	select_init2
unknown's avatar
unknown committed
8309
	{
8310 8311
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
unknown's avatar
unknown committed
8312
	};
unknown's avatar
unknown committed
8313 8314

exists_subselect:
unknown's avatar
unknown committed
8315 8316 8317 8318 8319
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
unknown's avatar
unknown committed
8320 8321

exists_subselect_init:
8322
	select_init2
unknown's avatar
unknown committed
8323
	{
8324 8325
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
unknown's avatar
unknown committed
8326
	};
8327

unknown's avatar
unknown committed
8328 8329 8330 8331 8332 8333 8334 8335
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
8336
  select_init2
unknown's avatar
unknown committed
8337
  {
unknown's avatar
unknown committed
8338
    $$= Lex->current_select->master_unit()->first_select();
unknown's avatar
unknown committed
8339 8340
  };

8341
subselect_start:
8342
	'(' SELECT_SYM
unknown's avatar
unknown committed
8343
	{
8344
	  LEX *lex=Lex;
8345
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
8346 8347
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
8348 8349
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
8350 8351
	    YYABORT;
	  }
unknown's avatar
unknown committed
8352 8353 8354
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
8355 8356

subselect_end:
unknown's avatar
unknown committed
8357 8358 8359
	')'
	{
	  LEX *lex=Lex;
8360
	  lex->current_select = lex->current_select->return_after_parsing();
unknown's avatar
unknown committed
8361
	};
unknown's avatar
unknown committed
8362

8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406
opt_view_list:
	/* empty */ {}
	| '(' view_list ')'
	;

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

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

algorithm:
	/* empty */
	  { Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED; }
	| ALGORITHM_SYM EQ 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; }
	;
check_option:
        /* empty */
          { 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; }
        ;

8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439
xa: XA_SYM begin_or_start xid opt_join_or_resume
      {
        LEX *lex= Lex;
        lex->sql_command = SQLCOM_XA_START;
      }
    | XA_SYM END xid opt_suspend_or_migrate
      {
        LEX *lex= Lex;
        lex->sql_command = SQLCOM_XA_END;
      }
    | XA_SYM PREPARE_SYM xid
      {
        LEX *lex= Lex;
        lex->sql_command = SQLCOM_XA_PREPARE;
      }
    | XA_SYM COMMIT_SYM xid opt_one_phase
      {
        LEX *lex= Lex;
        lex->sql_command = SQLCOM_XA_COMMIT;
      }
    | XA_SYM ROLLBACK_SYM xid
      {
        LEX *lex= Lex;
        lex->sql_command = SQLCOM_XA_ROLLBACK;
      }
    | XA_SYM RECOVER_SYM
      {
        LEX *lex= Lex;
        lex->sql_command = SQLCOM_XA_RECOVER;
      }
    ;

xid: ident_or_text              { Lex->ident=$1; }
8440
    ;
8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463

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

opt_suspend_or_migrate:
    /* nothing */           { Lex->xa_opt=XA_NONE;        }
    | SUSPEND_SYM           { Lex->xa_opt=XA_SUSPEND;     }
    | FOR_SYM MIGRATE_SYM   { Lex->xa_opt=XA_FOR_MIGRATE; }
    ;