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

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

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

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

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

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

unknown's avatar
unknown committed
28 29 30
#define MYSQL_YACC
#define YYINITDEPTH 100
#define YYMAXDEPTH 3200				/* Because of 64K stack */
31
#define Lex (YYTHD->lex)
32
#define Select Lex->current_select
unknown's avatar
unknown committed
33
#include "mysql_priv.h"
unknown's avatar
unknown committed
34
#include "slave.h"
unknown's avatar
unknown committed
35
#include "lex_symbol.h"
36
#include "item_create.h"
37 38 39 40
#include "sp_head.h"
#include "sp_pcontext.h"
#include "sp_rcontext.h"
#include "sp.h"
unknown's avatar
unknown committed
41
#include <myisam.h>
42
#include <myisammrg.h>
43

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

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

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

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

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

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

%}
%union {
  int  num;
  ulong ulong_num;
unknown's avatar
unknown committed
75
  ulonglong ulonglong_number;
unknown's avatar
unknown committed
76 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;
91
  enum enum_var_type var_type;
unknown's avatar
unknown committed
92
  Key::Keytype key_type;
unknown's avatar
unknown committed
93
  enum ha_key_alg key_alg;
unknown's avatar
unknown committed
94 95
  enum db_type db_type;
  enum row_type row_type;
unknown's avatar
unknown committed
96
  enum ha_rkey_function ha_rkey_mode;
unknown's avatar
unknown committed
97
  enum enum_tx_isolation tx_isolation;
98
  enum Cast_target cast_type;
unknown's avatar
unknown committed
99
  enum Item_udftype udf_type;
100
  CHARSET_INFO *charset;
unknown's avatar
unknown committed
101
  thr_lock_type lock_type;
102
  interval_type interval, interval_time_st;
103
  timestamp_type date_time_type;
unknown's avatar
unknown committed
104
  st_select_lex *select_lex;
unknown's avatar
unknown committed
105
  chooser_compare_func_creator boolfunc2creator;
106 107 108 109
  struct sp_cond_type *spcondtype;
  struct { int vars, conds, hndlrs, curs; } spblock;
  sp_name *spname;
  struct st_lex *lex;
unknown's avatar
unknown committed
110 111 112
}

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

%pure_parser					/* We have threads */

unknown's avatar
unknown committed
118
%token  END_OF_INPUT
unknown's avatar
unknown committed
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 149
%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
150
%token  BIN_NUM
151 152 153 154 155 156 157 158 159 160 161 162 163 164
%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
165
%token  CASCADED
166
%token  CAST_SYM
unknown's avatar
unknown committed
167
%token  CHAIN_SYM
168 169 170 171 172 173 174 175 176 177
%token  CHANGE
%token  CHANGED
%token  CHARSET
%token  CHAR_SYM
%token  CHECKSUM_SYM
%token  CHECK_SYM
%token  CIPHER_SYM
%token  CLIENT_SYM
%token  CLOSE_SYM
%token  COALESCE
unknown's avatar
unknown committed
178
%token  CODE_SYM
179 180 181 182 183 184 185
%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
186
%token  COMPACT_SYM
187 188 189 190
%token  COMPRESSED_SYM
%token  CONCAT
%token  CONCAT_WS
%token  CONCURRENT
191
%token  CONDITION_SYM
192 193 194
%token  CONNECTION_SYM
%token  CONSISTENT_SYM
%token  CONSTRAINT
195 196
%token  CONTAINS_SYM
%token  CONTINUE_SYM
197 198 199 200 201 202 203
%token  CONVERT_SYM
%token  CONVERT_TZ_SYM
%token  COUNT_SYM
%token  CREATE
%token  CROSS
%token  CUBE_SYM
%token  CURDATE
204
%token  CURRENT_USER
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
%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
220
%token  DECIMAL_NUM
221
%token  DECIMAL_SYM
222
%token  DECLARE_SYM
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
%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
245
%token  DUPLICATE_SYM
246
%token  DYNAMIC_SYM
247
%token  EACH_SYM
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
%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
266
%token  EXIT_SYM
267 268 269 270 271 272
%token  EXPANSION_SYM
%token  EXPORT_SET
%token  EXTENDED_SYM
%token  EXTRACT_SYM
%token  FALSE_SYM
%token  FAST_SYM
273
%token  FETCH_SYM
274 275 276 277 278 279 280 281 282 283 284
%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
285
%token  FOUND_SYM
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  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
%token  OFFSET_SYM
451
%token  OJ_SYM
452 453
%token  OLD_PASSWORD
%token  ON
454
%token  ONE_SHOT_SYM
455 456 457 458 459 460 461 462 463 464 465
%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
466
%token  OUT_SYM
467 468 469
%token  PACK_KEYS_SYM
%token  PARTIAL
%token  PASSWORD
470
%token  PARAM_MARKER
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 497 498
%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
499
%token  REDUNDANT_SYM
500 501 502 503 504
%token  REFERENCES
%token  REGEXP
%token  RELAY_LOG_FILE_SYM
%token  RELAY_LOG_POS_SYM
%token  RELAY_THREAD
unknown's avatar
unknown committed
505
%token  RELEASE_SYM
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
%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
526
%token  ROUTINE_SYM
527 528 529 530 531 532 533 534
%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
535
%token  SECURITY_SYM
536 537
%token  SELECT_SYM
%token  SENSITIVE_SYM
538
%token  SEPARATOR_SYM
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
%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
556 557 558 559
%token  SPECIFIC_SYM
%token  SQLEXCEPTION_SYM
%token  SQLSTATE_SYM
%token  SQLWARNING_SYM
560 561 562 563 564 565 566 567
%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
568
%token  SSL_SYM
569 570 571 572
%token  STARTING
%token  START_SYM
%token  STATUS_SYM
%token  STD_SYM
573
%token  STDDEV_SAMP_SYM
574 575 576 577 578 579 580 581 582 583 584
%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
585
%token  SYSDATE
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
%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
604
%token  TRIGGER_SYM
605
%token  TRIGGERS_SYM
606 607 608
%token  TRIM
%token  TRUE_SYM
%token  TRUNCATE_SYM
unknown's avatar
unknown committed
609
%token  TYPES_SYM
610 611 612 613 614 615 616
%token  TYPE_SYM
%token  UDF_RETURNS_SYM
%token  UDF_SONAME_SYM
%token  ULONGLONG_NUM
%token  UNCOMMITTED_SYM
%token  UNDEFINED_SYM
%token  UNDERSCORE_CHARSET
617
%token  UNDO_SYM
618 619 620 621 622 623 624 625 626 627
%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
628
%token  UNTIL_SYM
629 630
%token  UPDATE_SYM
%token  UPDATE_SYM
unknown's avatar
unknown committed
631
%token  UPGRADE_SYM
632 633 634 635 636 637 638 639
%token  USAGE
%token  USER
%token  USE_FRM
%token  USE_SYM
%token  USING
%token  UTC_DATE_SYM
%token  UTC_TIMESTAMP_SYM
%token  UTC_TIME_SYM
640
%token  VAR_SAMP_SYM
641 642 643 644 645 646 647 648 649 650 651 652
%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
653
%token  WHILE_SYM
654 655 656 657 658 659 660 661 662 663
%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
664

665
%left   JOIN_SYM INNER_SYM STRAIGHT_JOIN CROSS LEFT RIGHT
unknown's avatar
unknown committed
666 667
/* A dummy token to force the priority of table_ref production in a join. */
%left   TABLE_REF_PRIORITY
unknown's avatar
unknown committed
668
%left   SET_VAR
669 670
%left	OR_OR_SYM OR_SYM OR2_SYM XOR
%left	AND_SYM AND_AND_SYM
unknown's avatar
unknown committed
671 672 673 674 675 676
%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	'-' '+'
677
%left	'*' '/' '%' DIV_SYM MOD_SYM
unknown's avatar
unknown committed
678
%left   '^'
unknown's avatar
unknown committed
679
%left	NEG '~'
680
%right	NOT_SYM NOT2_SYM
unknown's avatar
unknown committed
681
%right	BINARY COLLATE_SYM
682

unknown's avatar
unknown committed
683
%type <lex_str>
unknown's avatar
unknown committed
684
        IDENT IDENT_QUOTED TEXT_STRING DECIMAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM
685
	LEX_HOSTNAME ULONGLONG_NUM field_ident select_alias ident ident_or_text
686
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
687
	NCHAR_STRING opt_component key_cache_name
unknown's avatar
unknown committed
688
        sp_opt_label BIN_NUM label_ident TEXT_STRING_filesystem
unknown's avatar
unknown committed
689 690 691 692 693

%type <lex_str_ptr>
	opt_table_alias

%type <table>
694
	table_ident table_ident_nodb references xid
unknown's avatar
unknown committed
695 696

%type <simple_string>
697
	remember_name remember_end opt_ident opt_db text_or_password
698
	opt_constraint constraint ident_or_empty
unknown's avatar
unknown committed
699 700

%type <string>
701
	text_string opt_gconcat_separator
unknown's avatar
unknown committed
702 703

%type <num>
unknown's avatar
unknown committed
704
	type int_type real_type order_dir lock_option
unknown's avatar
unknown committed
705
	udf_type if_exists opt_local opt_table_options table_options
706 707
        table_option opt_if_not_exists opt_no_write_to_binlog
        delete_option opt_temporary all_or_any opt_distinct
708
        opt_ignore_leaves fulltext_options spatial_type union_option
unknown's avatar
unknown committed
709
        start_transaction_opts opt_chain opt_release
710
        union_opt select_derived_init option_type2
unknown's avatar
unknown committed
711 712

%type <ulong_num>
unknown's avatar
unknown committed
713
	ulong_num raid_types merge_insert_types
unknown's avatar
unknown committed
714

unknown's avatar
unknown committed
715 716
%type <ulonglong_number>
	ulonglong_num
unknown's avatar
unknown committed
717

unknown's avatar
unknown committed
718 719 720
%type <lock_type>
	replace_lock_option opt_low_priority insert_lock_option load_data_lock

unknown's avatar
unknown committed
721
%type <item>
unknown's avatar
unknown committed
722
	literal text_literal insert_ident order_ident
unknown's avatar
unknown committed
723
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
724 725 726
	bool_term bool_factor bool_test bool_pri 
	predicate bit_expr bit_term bit_factor value_expr term factor
	table_wild simple_expr udf_expr
unknown's avatar
unknown committed
727
	expr_or_default set_expr_or_default interval_expr
unknown's avatar
unknown committed
728
	param_marker singlerow_subselect singlerow_subselect_init
unknown's avatar
unknown committed
729
	exists_subselect exists_subselect_init geometry_function
730
	signed_literal now_or_signed_literal opt_escape
731 732
	sp_opt_default
	simple_ident_nospvar simple_ident_q
733
        field_or_var limit_option
734 735 736

%type <item_num>
	NUM_literal
unknown's avatar
unknown committed
737 738

%type <item_list>
739 740
	expr_list udf_expr_list udf_expr_list2 when_list
	ident_list ident_list_arg
unknown's avatar
unknown committed
741

742 743 744
%type <var_type>
        option_type opt_var_type opt_var_ident_type

unknown's avatar
unknown committed
745
%type <key_type>
746
	key_type opt_unique_or_fulltext constraint_key_type
unknown's avatar
unknown committed
747

unknown's avatar
unknown committed
748 749 750
%type <key_alg>
	key_alg opt_btree_or_rtree

unknown's avatar
unknown committed
751
%type <string_list>
unknown's avatar
unknown committed
752
	key_usage_list using_list
unknown's avatar
unknown committed
753 754 755 756 757

%type <key_part>
	key_part

%type <table_list>
758
	join_table_list  join_table
759
        table_factor table_ref
unknown's avatar
unknown committed
760
        select_derived derived_table_list
unknown's avatar
unknown committed
761

762
%type <date_time_type> date_time_type;
unknown's avatar
unknown committed
763 764
%type <interval> interval

765 766
%type <interval_time_st> interval_time_st

unknown's avatar
unknown committed
767
%type <db_type> storage_engines
unknown's avatar
unknown committed
768 769 770

%type <row_type> row_types

unknown's avatar
unknown committed
771
%type <tx_isolation> isolation_types
unknown's avatar
unknown committed
772

unknown's avatar
unknown committed
773 774
%type <ha_rkey_mode> handler_rkey_mode

775
%type <cast_type> cast_type
unknown's avatar
unknown committed
776

unknown's avatar
unknown committed
777 778
%type <udf_type> udf_func_type

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

781
%type <lex_user> user grant_user
unknown's avatar
unknown committed
782

783
%type <charset>
784
	opt_collate
785 786
	charset_name
	charset_name_or_default
unknown's avatar
unknown committed
787 788
	old_or_new_charset_name
	old_or_new_charset_name_or_default
789 790
	collation_name
	collation_name_or_default
791

unknown's avatar
unknown committed
792 793
%type <variable> internal_variable_name

unknown's avatar
unknown committed
794
%type <select_lex> in_subselect in_subselect_init
unknown's avatar
unknown committed
795
	get_select_lex
unknown's avatar
unknown committed
796

unknown's avatar
unknown committed
797 798
%type <boolfunc2creator> comp_op

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

841 842 843 844 845 846 847
%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
848 849
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
850
	',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_SYM BETWEEN_SYM CASE_SYM
851
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM OR2_SYM AND_AND_SYM
unknown's avatar
unknown committed
852 853 854 855 856
%%


query:
	END_OF_INPUT
unknown's avatar
unknown committed
857
	{
858
	   THD *thd= YYTHD;
859
	   if (!thd->bootstrap &&
860
	      (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
861
	   {
unknown's avatar
unknown committed
862
	     my_message(ER_EMPTY_QUERY, ER(ER_EMPTY_QUERY), MYF(0));
863
	     YYABORT;
864
	   }
865 866
	   else
	   {
867
	     thd->lex->sql_command= SQLCOM_EMPTY_QUERY;
868
	   }
unknown's avatar
unknown committed
869
	}
unknown's avatar
unknown committed
870
	| verb_clause END_OF_INPUT {};
unknown's avatar
unknown committed
871 872

verb_clause:
873 874 875 876 877 878
	  statement
	| begin
	;

/* Verb clauses, except begin */
statement:
unknown's avatar
unknown committed
879 880
	  alter
	| analyze
881
	| backup
882
	| call
unknown's avatar
unknown committed
883 884
	| change
	| check
885
	| checksum
unknown's avatar
unknown committed
886 887
	| commit
	| create
888
        | deallocate
unknown's avatar
unknown committed
889 890
	| delete
	| describe
unknown's avatar
unknown committed
891
	| do
unknown's avatar
unknown committed
892
	| drop
893
        | execute
unknown's avatar
unknown committed
894
	| flush
unknown's avatar
unknown committed
895
	| grant
unknown's avatar
unknown committed
896 897
	| handler
	| help
unknown's avatar
unknown committed
898
	| insert
unknown's avatar
unknown committed
899
	| kill
unknown's avatar
unknown committed
900 901 902
	| load
	| lock
	| optimize
unknown's avatar
unknown committed
903
        | keycache
unknown's avatar
unknown committed
904
	| preload
905
        | prepare
906
	| purge
unknown's avatar
unknown committed
907
	| release
908
	| rename
unknown's avatar
unknown committed
909
	| repair
unknown's avatar
unknown committed
910
	| replace
unknown's avatar
unknown committed
911
	| reset
912
	| restore
unknown's avatar
unknown committed
913 914
	| revoke
	| rollback
915
	| savepoint
unknown's avatar
unknown committed
916 917
	| select
	| set
unknown's avatar
unknown committed
918
	| show
unknown's avatar
unknown committed
919
	| slave
unknown's avatar
unknown committed
920
	| start
921
	| truncate
unknown's avatar
unknown committed
922 923
	| unlock
	| update
unknown's avatar
unknown committed
924
	| use
925
	| xa
unknown's avatar
unknown committed
926
        ;
927

unknown's avatar
unknown committed
928
deallocate:
929
        deallocate_or_drop PREPARE_SYM ident
unknown's avatar
unknown committed
930 931
        {
          THD *thd=YYTHD;
932
          LEX *lex= thd->lex;
933
          if (lex->stmt_prepare_mode)
unknown's avatar
unknown committed
934 935 936 937
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
938
          lex->sql_command= SQLCOM_DEALLOCATE_PREPARE;
unknown's avatar
unknown committed
939 940 941
          lex->prepared_stmt_name= $3;
        };

942 943 944 945 946 947
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


unknown's avatar
unknown committed
948
prepare:
949
        PREPARE_SYM ident FROM prepare_src
unknown's avatar
unknown committed
950 951
        {
          THD *thd=YYTHD;
952
          LEX *lex= thd->lex;
953
          if (lex->stmt_prepare_mode)
unknown's avatar
unknown committed
954 955 956 957
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
958
          lex->sql_command= SQLCOM_PREPARE;
unknown's avatar
unknown committed
959 960 961
          lex->prepared_stmt_name= $2;
        };

962 963 964 965 966 967
prepare_src:
        TEXT_STRING_sys
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $1;
unknown's avatar
unknown committed
968
          lex->prepared_stmt_code_is_varref= FALSE;
969 970 971 972 973 974
        }
        | '@' ident_or_text
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $2;
unknown's avatar
unknown committed
975
          lex->prepared_stmt_code_is_varref= TRUE;
976
        };
977

unknown's avatar
unknown committed
978 979 980 981
execute:
        EXECUTE_SYM ident
        {
          THD *thd=YYTHD;
982
          LEX *lex= thd->lex;
983
          if (lex->stmt_prepare_mode)
unknown's avatar
unknown committed
984 985 986 987
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
988
          lex->sql_command= SQLCOM_EXECUTE;
unknown's avatar
unknown committed
989 990 991 992 993 994 995 996 997 998 999 1000
          lex->prepared_stmt_name= $2;
        }
        execute_using
        {}
        ;

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
1001 1002
        execute_var_list ',' execute_var_ident
        | execute_var_ident
unknown's avatar
unknown committed
1003 1004 1005 1006 1007 1008 1009 1010
        ;

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;
1011
        }
unknown's avatar
unknown committed
1012 1013
        ;

unknown's avatar
unknown committed
1014 1015
/* help */

1016
help:
1017 1018 1019 1020 1021 1022 1023 1024 1025
       HELP_SYM
       {
         if (Lex->sphead)
         {
           my_error(ER_SP_BADSTATEMENT, MYF(0), "HELP");
           YYABORT;
         }
       }
       ident_or_text
unknown's avatar
unknown committed
1026
       {
1027 1028
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
1029
	  lex->help_arg= $3.str;
1030
       };
unknown's avatar
unknown committed
1031 1032 1033 1034

/* change master */

change:
unknown's avatar
unknown committed
1035
       CHANGE MASTER_SYM TO_SYM
unknown's avatar
unknown committed
1036 1037 1038
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
unknown's avatar
unknown committed
1039
	  bzero((char*) &lex->mi, sizeof(lex->mi));
1040 1041 1042 1043
        }
       master_defs
	{}
       ;
unknown's avatar
unknown committed
1044 1045 1046

master_defs:
       master_def
unknown's avatar
unknown committed
1047
       | master_defs ',' master_def;
unknown's avatar
unknown committed
1048

1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
master_def:
       MASTER_HOST_SYM EQ TEXT_STRING_sys
       {
	 Lex->mi.host = $3.str;
       }
       |
       MASTER_USER_SYM EQ TEXT_STRING_sys
       {
	 Lex->mi.user = $3.str;
       }
       |
       MASTER_PASSWORD_SYM EQ TEXT_STRING_sys
       {
	 Lex->mi.password = $3.str;
       }
       |
unknown's avatar
unknown committed
1065
       MASTER_PORT_SYM EQ ulong_num
1066 1067 1068 1069
       {
	 Lex->mi.port = $3;
       }
       |
unknown's avatar
unknown committed
1070
       MASTER_CONNECT_RETRY_SYM EQ ulong_num
1071 1072 1073
       {
	 Lex->mi.connect_retry = $3;
       }
unknown's avatar
unknown committed
1074
       | MASTER_SSL_SYM EQ ulong_num
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
         {
           Lex->mi.ssl= $3 ? 
               LEX_MASTER_INFO::SSL_ENABLE : LEX_MASTER_INFO::SSL_DISABLE;
         }
       | MASTER_SSL_CA_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_ca= $3.str;
         }
       | MASTER_SSL_CAPATH_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_capath= $3.str;
         }
       | MASTER_SSL_CERT_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_cert= $3.str;
         }
       | MASTER_SSL_CIPHER_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_cipher= $3.str;
         }
       | MASTER_SSL_KEY_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.ssl_key= $3.str;
	 }
       |
         master_file_def
       ;

master_file_def:
       MASTER_LOG_FILE_SYM EQ TEXT_STRING_sys
       {
	 Lex->mi.log_file_name = $3.str;
       }
       | MASTER_LOG_POS_SYM EQ ulonglong_num
         {
           Lex->mi.pos = $3;
           /* 
              If the user specified a value < BIN_LOG_HEADER_SIZE, adjust it
              instead of causing subsequent errors. 
              We need to do it in this file, because only there we know that 
              MASTER_LOG_POS has been explicitely specified. On the contrary
              in change_master() (sql_repl.cc) we cannot distinguish between 0
              (MASTER_LOG_POS explicitely specified as 0) and 0 (unspecified),
              whereas we want to distinguish (specified 0 means "read the binlog
              from 0" (4 in fact), unspecified means "don't change the position
              (keep the preceding value)").
           */
           Lex->mi.pos = max(BIN_LOG_HEADER_SIZE, Lex->mi.pos);
         }
       | RELAY_LOG_FILE_SYM EQ TEXT_STRING_sys
         {
           Lex->mi.relay_log_name = $3.str;
         }
unknown's avatar
unknown committed
1128
       | RELAY_LOG_POS_SYM EQ ulong_num
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 1185 1186 1187 1188 1189 1190 1191 1192
         {
           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;
	  }
1193
	| CREATE
1194
	  {
1195 1196 1197
            Lex->create_view_mode= VIEW_CREATE_NEW;
            Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED;
            Lex->create_view_suid= TRUE;
1198
	  }
1199
	  view_or_trigger_or_sp
1200
	  {}
1201 1202 1203 1204
	| CREATE USER clear_privileges grant_list
	  {
	    Lex->sql_command = SQLCOM_CREATE_USER;
          }
1205 1206
	;

1207 1208 1209 1210 1211 1212 1213
clear_privileges:
        /* Nothing */
        {
          LEX *lex=Lex;
          lex->users_list.empty();
          lex->columns.empty();
          lex->grant= lex->grant_tot_col= 0;
1214
	  lex->all_privileges= 0;
1215 1216 1217 1218 1219 1220 1221
          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));
        }
        ;

1222
sp_name:
1223
	  ident '.' ident
1224
	  {
1225 1226 1227 1228 1229
            if (!$1.str || check_db_name($1.str))
            {
	      my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
	      YYABORT;
	    }
unknown's avatar
unknown committed
1230
	    if (check_routine_name($3))
1231 1232 1233 1234
            {
	      my_error(ER_SP_WRONG_NAME, MYF(0), $3.str);
	      YYABORT;
	    }
1235 1236 1237
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
1238
	| ident
1239
	  {
unknown's avatar
unknown committed
1240 1241
            THD *thd= YYTHD;
            LEX_STRING db;
unknown's avatar
unknown committed
1242
	    if (check_routine_name($1))
1243 1244 1245 1246
            {
	      my_error(ER_SP_WRONG_NAME, MYF(0), $1.str);
	      YYABORT;
	    }
unknown's avatar
unknown committed
1247 1248 1249 1250 1251
            if (thd->copy_db_to(&db.str, &db.length))
              YYABORT;
	    $$= new sp_name(db, $1);
            if ($$)
	      $$->init_qname(YYTHD);
1252 1253 1254 1255 1256 1257 1258
	  }
	;

create_function_tail:
	  RETURNS_SYM udf_type UDF_SONAME_SYM TEXT_STRING_sys
	  {
	    LEX *lex=Lex;
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
            if (lex->definer != NULL)
            {
              /*
                 DEFINER is a concept meaningful when interpreting SQL code.
                 UDF functions are compiled.
                 Using DEFINER with UDF has therefore no semantic,
                 and is considered a parsing error.
              */
	      my_error(ER_WRONG_USAGE, MYF(0), "SONAME", "DEFINER");
              YYABORT;
            }
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
	    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;

1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
            /* 
              First check if AGGREGATE was used, in that case it's a
              syntax error.
            */
            if (lex->udf.type == UDFTYPE_AGGREGATE)
            {
              my_error(ER_SP_NO_AGGREGATE, MYF(0));
              YYABORT;
            }

1290 1291
	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
1292
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1293 1294 1295 1296 1297 1298
	      YYABORT;
	    }
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
	    sp->init(lex);
1299
            sp->init_sp_name(YYTHD, lex->spname);
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320

	    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
1321 1322 1323 1324
	    lex->charset= NULL;
	    lex->length= lex->dec= NULL;
	    lex->interval_list.empty();
	    lex->type= 0;
1325 1326 1327 1328 1329
	  }
	  type
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
unknown's avatar
unknown committed
1330

1331 1332 1333 1334
            if (sp->fill_field_definition(YYTHD, lex,
                                          (enum enum_field_types) $8,
                                          &sp->m_return_field_def))
              YYABORT;
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349

	    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;

1350 1351 1352
            if (sp->is_not_allowed_in_function("function"))
              YYABORT;

1353
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
1354
	    sp->init_strings(YYTHD, lex);
1355 1356 1357 1358 1359
            if (!(sp->m_flags & sp_head::HAS_RETURN))
            {
              my_error(ER_SP_NORETURN, MYF(0), sp->m_qname.str);
              YYABORT;
            }
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 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
	    /* 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; }
1399
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
	;

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();
1421
	    sp_add_used_routine(lex, YYTHD, $2, TYPE_ENUM_PROCEDURE);
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
	  }
          '(' 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
	;

1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
sp_init_param:
	  /* Empty */
	  {
	    LEX *lex= Lex;

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

1474
sp_fdparam:
1475
	  ident sp_init_param type
1476 1477 1478 1479
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1480
	    if (spc->find_variable(&$1, TRUE))
1481
	    {
1482
	      my_error(ER_SP_DUP_PARAM, MYF(0), $1.str);
1483 1484
	      YYABORT;
	    }
1485 1486 1487
            sp_variable_t *spvar= spc->push_variable(&$1,
                                                     (enum enum_field_types)$3,
                                                     sp_param_in);
1488 1489 1490

            if (lex->sphead->fill_field_definition(YYTHD, lex,
                                                   (enum enum_field_types) $3,
1491
                                                   &spvar->field_def))
1492 1493 1494
            {
              YYABORT;
            }
1495 1496
            spvar->field_def.field_name= spvar->name.str;
            spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
1512
	  sp_opt_inout sp_init_param ident type
1513 1514 1515 1516
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1517
	    if (spc->find_variable(&$3, TRUE))
1518
	    {
1519
	      my_error(ER_SP_DUP_PARAM, MYF(0), $3.str);
1520 1521
	      YYABORT;
	    }
1522 1523 1524
            sp_variable_t *spvar= spc->push_variable(&$3,
                                                     (enum enum_field_types)$4,
                                                     (sp_param_mode_t)$1);
1525 1526 1527

            if (lex->sphead->fill_field_definition(YYTHD, lex,
                                                   (enum enum_field_types) $4,
1528
                                                   &spvar->field_def))
1529 1530 1531
            {
              YYABORT;
            }
1532 1533
            spvar->field_def.field_name= spvar->name.str;
            spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
	  }
	;

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 */ {}
1546
	| sp_proc_stmts  sp_proc_stmt ';'
1547 1548 1549 1550
	;

sp_proc_stmts1:
	  sp_proc_stmt ';' {}
1551
	| sp_proc_stmts1  sp_proc_stmt ';'
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
	;

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
1567 1568
	      my_message(ER_SP_VARCOND_AFTER_CURSHNDLR,
                         ER(ER_SP_VARCOND_AFTER_CURSHNDLR), MYF(0));
1569 1570 1571 1572
	      YYABORT;
	    }
	    if ($2.curs && $1.hndlrs)
	    { /* Cursor following handler */
unknown's avatar
unknown committed
1573 1574
	      my_message(ER_SP_CURSOR_AFTER_HANDLER,
                         ER(ER_SP_CURSOR_AFTER_HANDLER), MYF(0));
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
	      YYABORT;
	    }
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
	    $$.curs= $1.curs + $2.curs;
	  }
	;

sp_decl:
1585
          DECLARE_SYM sp_decl_idents
1586
          {
1587
            LEX *lex= Lex;
1588

1589 1590 1591
            lex->sphead->reset_lex(YYTHD);
            lex->spcont->declare_var_boundary($2);
          }
1592
          type
1593
          sp_opt_default
1594 1595
          {
            LEX *lex= Lex;
1596
            sp_pcontext *pctx= lex->spcont;
1597
            uint num_vars= pctx->context_var_count();
1598 1599 1600 1601 1602
            enum enum_field_types var_type= (enum enum_field_types) $4;
            Item *dflt_value_item= $5;
            create_field *create_field_op;
            
            if (!dflt_value_item)
1603
            {
1604 1605 1606 1607 1608 1609
              dflt_value_item= new Item_null();
              /* QQ Set to the var_type with null_value? */
            }
            
            for (uint i = num_vars-$2 ; i < num_vars ; i++)
            {
1610 1611
              uint var_idx= pctx->var_context2runtime(i);
              sp_variable_t *spvar= pctx->find_variable(var_idx);
1612
            
1613
              if (!spvar)
1614 1615
                YYABORT;
            
1616 1617
              spvar->type= var_type;
              spvar->dflt= dflt_value_item;
1618 1619
            
              if (lex->sphead->fill_field_definition(YYTHD, lex, var_type,
1620
                                                     &spvar->field_def))
1621 1622 1623 1624
              {
                YYABORT;
              }
            
1625 1626
              spvar->field_def.field_name= spvar->name.str;
              spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1627 1628 1629 1630 1631 1632 1633
            
              /* The last instruction is responsible for freeing LEX. */

              lex->sphead->add_instr(
                new sp_instr_set(lex->sphead->instructions(), pctx, var_idx,
                                 dflt_value_item, var_type, lex,
                                 (i == num_vars - 1)));
1634
            }
1635 1636

            pctx->declare_var_boundary(0);
1637
            lex->sphead->restore_lex(YYTHD);
1638

1639 1640 1641
            $$.vars= $2;
            $$.conds= $$.hndlrs= $$.curs= 0;
          }
1642 1643 1644 1645 1646 1647 1648
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1649
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
	      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,
1663
	                              ctx->current_var_count());
1664 1665 1666

	    sp->add_instr(i);
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
1667
	    sp->m_flags|= sp_head::IN_HANDLER;
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
	  }
	  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,
1680
	                              ctx->current_var_count());
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
	      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);
1691
	    sp->m_flags&= ~sp_head::IN_HANDLER;
1692 1693
	    $$.vars= $$.conds= $$.curs= 0;
	    $$.hndlrs= $6;
1694
	    ctx->add_handlers($6);
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
	  }
	| 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))
	    {
1706
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
1707 1708 1709
	      delete $5;
	      YYABORT;
	    }
unknown's avatar
unknown committed
1710
            i= new sp_instr_cpush(sp->instructions(), ctx, $5,
1711
                                  ctx->current_cursor_count());
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
	    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
1734 1735
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
1736 1737 1738 1739
	      YYABORT;
	    }
	    if (lex->result)
	    {
unknown's avatar
unknown committed
1740 1741
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
	      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;
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
	    sp_pcontext *ctx= lex->spcont;

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

1773 1774 1775 1776
	      i->add_condition($1);
	      ctx->push_handler($1);
	      $$= 1;
	    }
1777 1778 1779 1780 1781
	  }
	| sp_hcond_list ',' sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
	    sp_pcontext *ctx= lex->spcont;

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

1794 1795 1796 1797
	      i->add_condition($3);
	      ctx->push_handler($3);
	      $$= $1 + 1;
	    }
1798 1799 1800 1801
	  }
	;

sp_cond:
unknown's avatar
unknown committed
1802
	  ulong_num
1803 1804 1805 1806 1807 1808 1809
	  {			/* 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 */
1810 1811 1812 1813 1814
	    if (!sp_cond_check(&$3))
	    {
	      my_error(ER_SP_BAD_SQLSTATE, MYF(0), $3.str);
	      YYABORT;
	    }
1815 1816
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
1817 1818
	    memcpy($$->sqlstate, $3.str, 5);
	    $$->sqlstate[5]= '\0';
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
	  }
	;

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
1837
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
1838 1839 1840 1841 1842 1843 1844 1845
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
1846
	| not FOUND_SYM		/* SQLSTATEs 02??? */
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
	  {
	    $$= (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
	  {
1861 1862
            /* NOTE: field definition is filled in sp_decl section. */

1863 1864 1865
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1866
	    if (spc->find_variable(&$1, TRUE))
1867
	    {
1868
	      my_error(ER_SP_DUP_VAR, MYF(0), $1.str);
1869 1870
	      YYABORT;
	    }
1871
	    spc->push_variable(&$1, (enum_field_types)0, sp_param_in);
1872 1873 1874 1875
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
1876 1877
            /* NOTE: field definition is filled in sp_decl section. */

1878 1879 1880
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1881
	    if (spc->find_variable(&$3, TRUE))
1882
	    {
1883
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
1884 1885
	      YYABORT;
	    }
1886
	    spc->push_variable(&$3, (enum_field_types)0, sp_param_in);
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
	    $$= $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;

1908
            sp->m_flags|= sp_get_flags_for_command(lex);
1909 1910
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
1911
	      my_error(ER_SP_BADSTATEMENT, MYF(0), "USE");
1912 1913
	      YYABORT;
	    }
1914 1915 1916 1917
	    /*
              Don't add an instruction for SET statements, since all
              instructions for them were already added during processing
              of "set" rule.
1918
	    */
1919 1920 1921
            DBUG_ASSERT(lex->sql_command != SQLCOM_SET_OPTION ||
                        lex->var_list.is_empty());
            if (lex->sql_command != SQLCOM_SET_OPTION)
1922
	    {
1923 1924
              sp_instr_stmt *i=new sp_instr_stmt(sp->instructions(),
                                                 lex->spcont, lex);
1925

1926 1927 1928 1929 1930 1931
              /*
                Extract the query statement from the tokenizer.  The
                end is either lex->ptr, if there was no lookahead,
                lex->tok_end otherwise.
              */
              if (yychar == YYEMPTY)
1932 1933 1934 1935 1936 1937 1938
                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);
1939 1940 1941
            }
	    sp->restore_lex(YYTHD);
          }
1942 1943 1944
          | RETURN_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr
1945 1946
	  {
	    LEX *lex= Lex;
1947
	    sp_head *sp= lex->sphead;
1948

1949
	    if (sp->m_type != TYPE_ENUM_FUNCTION)
1950
	    {
unknown's avatar
unknown committed
1951
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
1952 1953 1954 1955 1956 1957
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_freturn *i;

1958 1959
	      i= new sp_instr_freturn(sp->instructions(), lex->spcont, $3,
                                      sp->m_return_field_def.sql_type, lex);
1960
	      sp->add_instr(i);
1961
	      sp->m_flags|= sp_head::HAS_RETURN;
1962
	    }
1963
	    sp->restore_lex(YYTHD);
1964
	  }
1965 1966 1967 1968
        | IF
          { Lex->sphead->new_cont_backpatch(NULL); }
          sp_if END IF
          { Lex->sphead->do_cont_backpatch(); }
1969 1970
	| CASE_SYM WHEN_SYM
	  {
1971
	    Lex->sphead->m_flags&= ~sp_head::IN_SIMPLE_CASE;
1972
            Lex->sphead->new_cont_backpatch(NULL);
1973
	  }
1974
          sp_case END CASE_SYM { Lex->sphead->do_cont_backpatch(); }
1975
        | CASE_SYM
1976 1977 1978 1979
          {
            Lex->sphead->reset_lex(YYTHD);
            Lex->sphead->new_cont_backpatch(NULL);
          }
1980
          expr WHEN_SYM
1981 1982
	  {
	    LEX *lex= Lex;
1983 1984 1985
	    sp_head *sp= lex->sphead;
	    sp_pcontext *parsing_ctx= lex->spcont;
	    int case_expr_id= parsing_ctx->register_case_expr();
1986
            sp_instr_set_case_expr *i;
1987 1988 1989
	    
	    if (parsing_ctx->push_case_expr_id(case_expr_id))
              YYABORT;
1990 1991 1992 1993 1994 1995 1996 1997

            i= new sp_instr_set_case_expr(sp->instructions(),
                                          parsing_ctx,
                                          case_expr_id,
                                          $3,
                                          lex);
            sp->add_cont_backpatch(i);
            sp->add_instr(i);
1998 1999
	    sp->m_flags|= sp_head::IN_SIMPLE_CASE;
	    sp->restore_lex(YYTHD);
2000 2001 2002
	  }
	  sp_case END CASE_SYM
	  {
2003
	    Lex->spcont->pop_case_expr_id();
2004
            Lex->sphead->do_cont_backpatch();
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
	  }
	| 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());
	  }
2019
	| LEAVE_SYM label_ident
2020 2021 2022 2023 2024 2025 2026 2027
	  {
	    LEX *lex= Lex;
	    sp_head *sp = lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab)
	    {
2028
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
2029 2030 2031 2032 2033
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_jump *i;
2034 2035 2036
	      uint ip= sp->instructions();
	      uint n;

2037
	      n= ctx->diff_handlers(lab->ctx, TRUE);  /* Exclusive the dest. */
2038 2039
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
2040
	      n= ctx->diff_cursors(lab->ctx, TRUE);  /* Exclusive the dest. */
2041 2042
	      if (n)
	        sp->add_instr(new sp_instr_cpop(ip++, ctx, n));
2043 2044 2045 2046 2047
	      i= new sp_instr_jump(ip, ctx);
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
	    }
	  }
2048
	| ITERATE_SYM label_ident
2049 2050 2051 2052 2053 2054 2055 2056
	  {
	    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)
	    {
2057
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "ITERATE", $2.str);
2058 2059 2060 2061 2062 2063 2064 2065
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_jump *i;
	      uint ip= sp->instructions();
	      uint n;

2066
	      n= ctx->diff_handlers(lab->ctx, FALSE);  /* Inclusive the dest. */
2067 2068
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
2069
	      n= ctx->diff_cursors(lab->ctx, FALSE);  /* Inclusive the dest. */
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
	      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);
	    }
	  }
	| OPEN_SYM ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_copen *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2085
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
	      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))
	    {
2100
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $3.str);
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
	      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))
	    {
2117
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
	      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;
2137
	    sp_variable_t *spv;
2138

2139
	    if (!spc || !(spv = spc->find_variable(&$1)))
2140
	    {
2141
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

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

2160
	    if (!spc || !(spv = spc->find_variable(&$3)))
2161
	    {
2162
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $3.str);
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	    }
	  }
	;

sp_if:
2176 2177
          { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2178 2179 2180 2181 2182
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= sp->instructions();
2183 2184
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, ctx,
                                                               $2, lex);
2185 2186

	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
2187
            sp->add_cont_backpatch(i);
2188
            sp->add_instr(i);
2189
            sp->restore_lex(YYTHD);
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
	  }
	  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:
2217 2218
	  { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2219 2220 2221 2222 2223 2224 2225
	  {
            LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

2226
	    if (! (sp->m_flags & sp_head::IN_SIMPLE_CASE))
2227
	      i= new sp_instr_jump_if_not(ip, ctx, $2, lex);
2228 2229 2230
	    else
	    { /* Simple case: <caseval> = <whenval> */

2231 2232 2233 2234 2235
	      Item_case_expr *var;
              Item *expr;

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

unknown's avatar
unknown committed
2236
#ifndef DBUG_OFF
2237
              if (var)
2238
                var->m_sp= sp;
2239
#endif
2240 2241

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

2243
	      i= new sp_instr_jump_if_not(ip, ctx, expr, lex);
2244 2245
	    }
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
2246
            sp->add_cont_backpatch(i);
2247
            sp->add_instr(i);
2248
            sp->restore_lex(YYTHD);
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
	  }
	  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
2268

2269 2270 2271 2272 2273 2274 2275
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
2276

2277 2278 2279 2280 2281
	    sp->add_instr(i);
	  }
	| ELSE sp_proc_stmts1 {}
	| WHEN_SYM sp_case {}
	;
unknown's avatar
unknown committed
2282

2283
sp_labeled_control:
2284
	  label_ident ':'
unknown's avatar
unknown committed
2285
	  {
2286 2287 2288 2289 2290 2291
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);

	    if (lab)
	    {
2292
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $1.str);
unknown's avatar
unknown committed
2293
	      YYABORT;
2294 2295 2296 2297 2298 2299 2300
	    }
	    else
	    {
	      lab= lex->spcont->push_label($1.str,
	                                   lex->sphead->instructions());
	      lab->type= SP_LAB_ITER;
	    }
unknown's avatar
unknown committed
2301
	  }
2302
	  sp_unlabeled_control sp_opt_label
unknown's avatar
unknown committed
2303
	  {
2304
	    LEX *lex= Lex;
unknown's avatar
unknown committed
2305

2306 2307 2308 2309 2310 2311 2312
	    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)
	      {
2313
	        my_error(ER_SP_LABEL_MISMATCH, MYF(0), $5.str);
2314 2315 2316 2317
	        YYABORT;
	      }
	    }
	    lex->sphead->backpatch(lex->spcont->pop_label());
unknown's avatar
unknown committed
2318
	  }
2319 2320 2321
	;

sp_opt_label:
2322
        /* Empty  */    { $$= null_lex_str; }
2323
        | label_ident   { $$= $1; }
2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
	;

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
2340
	  {
2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
	    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
2356
	  {
2357 2358 2359 2360 2361 2362
	    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
2363
	  }
2364 2365 2366
        | WHILE_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr DO_SYM
unknown's avatar
unknown committed
2367
	  {
2368 2369 2370 2371
	    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,
2372
							       $3, lex);
2373 2374 2375

	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
2376
            sp->new_cont_backpatch(i);
2377
            sp->add_instr(i);
2378
            sp->restore_lex(YYTHD);
unknown's avatar
unknown committed
2379
	  }
2380
	  sp_proc_stmts1 END WHILE_SYM
unknown's avatar
unknown committed
2381
	  {
2382 2383 2384 2385 2386 2387
	    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);
2388
            lex->sphead->do_cont_backpatch();
2389
	  }
2390 2391 2392
        | REPEAT_SYM sp_proc_stmts1 UNTIL_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr END REPEAT_SYM
2393 2394 2395 2396 2397
	  {
	    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,
2398 2399
                                                               $5, lab->ip,
                                                               lex);
2400
            lex->sphead->add_instr(i);
2401
            lex->sphead->restore_lex(YYTHD);
2402 2403
            /* We can shortcut the cont_backpatch here */
            i->m_cont_dest= ip+1;
2404
	  }
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420
	;

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; }
2421
          ;
unknown's avatar
unknown committed
2422 2423

create2:
unknown's avatar
unknown committed
2424 2425 2426 2427 2428
        '(' create2a {}
        | opt_create_table_options create3 {}
        | LIKE table_ident
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
2429
            THD *thd= lex->thd;
unknown's avatar
unknown committed
2430
            if (!(lex->name= (char *)$2))
unknown's avatar
unknown committed
2431
              YYABORT;
unknown's avatar
unknown committed
2432 2433 2434 2435 2436
            if ($2->db.str == NULL &&
                thd->copy_db_to(&($2->db.str), &($2->db.length)))
            {
              YYABORT;
            }
unknown's avatar
unknown committed
2437 2438 2439 2440
          }
        | '(' LIKE table_ident ')'
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
2441
            THD *thd= lex->thd;
unknown's avatar
unknown committed
2442
            if (!(lex->name= (char *)$3))
2443
              YYABORT;
unknown's avatar
unknown committed
2444 2445 2446 2447 2448
            if ($3->db.str == NULL &&
                thd->copy_db_to(&($3->db.str), &($3->db.length)))
            {
              YYABORT;
            }
unknown's avatar
unknown committed
2449
          }
unknown's avatar
unknown committed
2450
        ;
unknown's avatar
unknown committed
2451

unknown's avatar
unknown committed
2452 2453
create2a:
        field_list ')' opt_create_table_options create3 {}
unknown's avatar
unknown committed
2454
	|  create_select ')' { Select->set_braces(1);} union_opt {}
2455
        ;
unknown's avatar
unknown committed
2456 2457

create3:
2458
	/* empty */ {}
unknown's avatar
unknown committed
2459
	| opt_duplicate opt_as     create_select
unknown's avatar
unknown committed
2460
          { Select->set_braces(0);} union_clause {}
unknown's avatar
unknown committed
2461
	| opt_duplicate opt_as '(' create_select ')'
unknown's avatar
unknown committed
2462
          { Select->set_braces(1);} union_opt {}
2463 2464
        ;

unknown's avatar
unknown committed
2465
create_select:
2466
          SELECT_SYM
unknown's avatar
unknown committed
2467
          {
unknown's avatar
unknown committed
2468
	    LEX *lex=Lex;
2469
	    lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
2470 2471 2472 2473
	    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;
2474 2475 2476 2477
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
unknown's avatar
unknown committed
2478
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
unknown's avatar
unknown committed
2479
	    mysql_init_select(lex);
2480
	    lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
2481
          }
unknown's avatar
unknown committed
2482 2483
          select_options select_item_list
	  {
2484
	    Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
2485
	  }
unknown's avatar
unknown committed
2486
	  opt_select_from
2487 2488 2489 2490 2491 2492 2493
	  {
	    /*
              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
2494
        ;
unknown's avatar
unknown committed
2495

2496 2497
opt_as:
	/* empty */ {}
unknown's avatar
unknown committed
2498
	| AS	    {};
2499

2500 2501 2502 2503 2504 2505 2506 2507 2508
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
2509 2510
	default_collation   {}
	| default_charset   {};
2511

unknown's avatar
unknown committed
2512
opt_table_options:
unknown's avatar
unknown committed
2513
	/* empty */	 { $$= 0; }
unknown's avatar
unknown committed
2514
	| table_options  { $$= $1;};
unknown's avatar
unknown committed
2515 2516 2517

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

unknown's avatar
unknown committed
2520
table_option:
unknown's avatar
unknown committed
2521
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
unknown's avatar
unknown committed
2522 2523

opt_if_not_exists:
unknown's avatar
unknown committed
2524
	/* empty */	 { $$= 0; }
2525
	| IF not EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
unknown's avatar
unknown committed
2526 2527 2528

opt_create_table_options:
	/* empty */
unknown's avatar
unknown committed
2529
	| create_table_options;
unknown's avatar
unknown committed
2530

unknown's avatar
unknown committed
2531 2532 2533 2534
create_table_options_space_separated:
	create_table_option
	| create_table_option create_table_options_space_separated;

unknown's avatar
unknown committed
2535 2536
create_table_options:
	create_table_option
2537
	| create_table_option     create_table_options
unknown's avatar
unknown committed
2538
	| create_table_option ',' create_table_options;
unknown's avatar
unknown committed
2539 2540

create_table_option:
2541 2542
	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; }
2543 2544
	| MAX_ROWS opt_equal ulonglong_num	{ Lex->create_info.max_rows= $3; Lex->create_info.used_fields|= HA_CREATE_USED_MAX_ROWS;}
	| MIN_ROWS opt_equal ulonglong_num	{ Lex->create_info.min_rows= $3; Lex->create_info.used_fields|= HA_CREATE_USED_MIN_ROWS;}
unknown's avatar
unknown committed
2545
	| 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;}
2546
	| PASSWORD opt_equal TEXT_STRING_sys	{ Lex->create_info.password=$3.str; Lex->create_info.used_fields|= HA_CREATE_USED_PASSWORD; }
2547
	| COMMENT_SYM opt_equal TEXT_STRING_sys	{ Lex->create_info.comment=$3; Lex->create_info.used_fields|= HA_CREATE_USED_COMMENT; }
2548
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569
        | PACK_KEYS_SYM opt_equal ulong_num
          {
            switch($3) {
            case 0:
                Lex->create_info.table_options|= HA_OPTION_NO_PACK_KEYS;
                break;
            case 1:
                Lex->create_info.table_options|= HA_OPTION_PACK_KEYS;
                break;
            default:
                yyerror(ER(ER_SYNTAX_ERROR));
                YYABORT;
            }
            Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;
          }
        | PACK_KEYS_SYM opt_equal DEFAULT
          {
            Lex->create_info.table_options&=
              ~(HA_OPTION_PACK_KEYS | HA_OPTION_NO_PACK_KEYS);
            Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;
          }
unknown's avatar
unknown committed
2570 2571
	| 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; }
2572
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
unknown's avatar
unknown committed
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587
	| RAID_TYPE opt_equal raid_types
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_TYPE", "PARTITION");
	    YYABORT;
	  }
	| RAID_CHUNKS opt_equal ulong_num
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_CHUNKS", "PARTITION");
	    YYABORT;
	  }
	| RAID_CHUNKSIZE opt_equal ulong_num
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_CHUNKSIZE", "PARTITION");
	    YYABORT;
	  }
2588
	| UNION_SYM opt_equal '(' table_list ')'
2589 2590 2591
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
2592 2593
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
2594
	    lex->create_info.merge_list.elements--;
2595 2596
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
2597
	    lex->select_lex.table_list.elements=1;
2598 2599 2600
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
2601
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
2602
	  }
2603 2604
	| default_charset
	| default_collation
2605
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
2606 2607
	| 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; }
2608
	| CONNECTION_SYM opt_equal TEXT_STRING_sys { Lex->create_info.connect_string.str= $3.str; Lex->create_info.connect_string.length= $3.length;  Lex->create_info.used_fields|= HA_CREATE_USED_CONNECTION; }
2609
        ;
unknown's avatar
unknown committed
2610

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

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

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
2658 2659 2660
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; }
	| REDUNDANT_SYM	{ $$= ROW_TYPE_REDUNDANT; }
	| COMPACT_SYM	{ $$= ROW_TYPE_COMPACT; };
unknown's avatar
unknown committed
2661 2662 2663 2664

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

2667 2668 2669
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
unknown's avatar
unknown committed
2670
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
2671

unknown's avatar
unknown committed
2672
opt_select_from:
2673
	opt_limit_clause {}
unknown's avatar
unknown committed
2674
	| select_from select_lock_type;
unknown's avatar
unknown committed
2675 2676

udf_func_type:
2677
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
unknown's avatar
unknown committed
2678
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
unknown's avatar
unknown committed
2679 2680 2681 2682

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
unknown's avatar
unknown committed
2683
        | DECIMAL_SYM {$$ = (int) DECIMAL_RESULT; }
unknown's avatar
unknown committed
2684
	| INT_SYM {$$ = (int) INT_RESULT; };
unknown's avatar
unknown committed
2685 2686 2687

field_list:
	  field_list_item
unknown's avatar
unknown committed
2688
	| field_list ',' field_list_item;
unknown's avatar
unknown committed
2689 2690 2691


field_list_item:
unknown's avatar
unknown committed
2692
	   column_def
2693 2694 2695 2696
         | key_def
         ;

column_def:
unknown's avatar
unknown committed
2697
	  field_spec opt_check_constraint
unknown's avatar
unknown committed
2698 2699 2700 2701
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
unknown's avatar
unknown committed
2702
	;
2703 2704

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

unknown's avatar
unknown committed
2743
opt_check_constraint:
2744
	/* empty */
unknown's avatar
unknown committed
2745 2746 2747 2748 2749
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
2750
	;
unknown's avatar
unknown committed
2751 2752

opt_constraint:
2753
	/* empty */		{ $$=(char*) 0; }
unknown's avatar
unknown committed
2754 2755 2756 2757 2758 2759
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
unknown's avatar
unknown committed
2760 2761 2762 2763

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

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

unknown's avatar
unknown committed
2882 2883 2884
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
2885 2886 2887
	| POINT_SYM           { Lex->length= (char*)"21";
                                $$= Field::GEOM_POINT;
                              }
unknown's avatar
unknown committed
2888 2889 2890 2891 2892 2893 2894
	| 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
2895 2896
char:
	CHAR_SYM {}
unknown's avatar
unknown committed
2897 2898 2899 2900 2901 2902
	;

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

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

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

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

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


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

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

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

field_opt_list:
	field_opt_list field_option {}
unknown's avatar
unknown committed
2949
	| field_option {};
unknown's avatar
unknown committed
2950 2951

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

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

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

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

opt_attribute_list:
	opt_attribute_list attribute {}
unknown's avatar
unknown committed
2970
	| attribute;
unknown's avatar
unknown committed
2971 2972 2973

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

3019 3020 3021 3022 3023
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

3024 3025 3026 3027
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
unknown's avatar
unknown committed
3028

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

3041 3042 3043
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
unknown's avatar
unknown committed
3044

unknown's avatar
unknown committed
3045 3046 3047 3048 3049 3050 3051

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

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

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

3073 3074
opt_collate:
	/* empty */	{ $$=NULL; }
unknown's avatar
unknown committed
3075
	| COLLATE_SYM collation_name_or_default { $$=$2; }
3076 3077
	;

3078 3079 3080 3081
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

3082 3083 3084 3085
opt_default:
	/* empty */	{}
	| DEFAULT	{};

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

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

opt_bin_charset:
3107
        /* empty */ { Lex->charset= NULL; }
unknown's avatar
unknown committed
3108
	| ASCII_SYM	{ Lex->charset=&my_charset_latin1; }
unknown's avatar
unknown committed
3109 3110
	| UNICODE_SYM
	{
unknown's avatar
unknown committed
3111 3112
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
unknown's avatar
unknown committed
3113
	  {
unknown's avatar
unknown committed
3114
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
unknown's avatar
unknown committed
3115 3116 3117
	    YYABORT;
	  }
	}
3118
	| charset charset_name	{ Lex->charset=$2; } ;
unknown's avatar
unknown committed
3119

unknown's avatar
unknown committed
3120 3121 3122
opt_primary:
	/* empty */
	| PRIMARY_SYM
3123
	;
unknown's avatar
unknown committed
3124

unknown's avatar
unknown committed
3125
references:
3126 3127
	REFERENCES table_ident
	{
3128
	  LEX *lex=Lex;
3129 3130 3131 3132 3133 3134
	  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
3135
	};
3136

3137
opt_ref_list:
unknown's avatar
unknown committed
3138
	/* empty */ opt_on_delete {}
unknown's avatar
unknown committed
3139
	| '(' ref_list ')' opt_on_delete {};
3140 3141 3142

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

unknown's avatar
unknown committed
3145 3146 3147

opt_on_delete:
	/* empty */ {}
unknown's avatar
unknown committed
3148
	| opt_on_delete_list {};
unknown's avatar
unknown committed
3149 3150 3151

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
unknown's avatar
unknown committed
3152
	| opt_on_delete_item {};
unknown's avatar
unknown committed
3153 3154

opt_on_delete_item:
3155 3156 3157 3158
	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
3159
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
unknown's avatar
unknown committed
3160 3161

delete_option:
3162 3163 3164 3165
	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
3166
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
unknown's avatar
unknown committed
3167 3168

key_type:
3169
	key_or_index			    { $$= Key::MULTIPLE; }
unknown's avatar
SCRUM  
unknown committed
3170 3171
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
unknown's avatar
unknown committed
3172 3173 3174 3175
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
3176 3177
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3178 3179 3180
	    YYABORT;
#endif
	  };
3181 3182 3183

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
unknown's avatar
SCRUM  
unknown committed
3184
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
unknown's avatar
unknown committed
3185 3186 3187

key_or_index:
	KEY_SYM {}
unknown's avatar
unknown committed
3188
	| INDEX_SYM {};
unknown's avatar
unknown committed
3189

unknown's avatar
SCRUM  
unknown committed
3190 3191 3192 3193 3194
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

unknown's avatar
unknown committed
3195 3196
keys_or_index:
	KEYS {}
unknown's avatar
unknown committed
3197
	| INDEX_SYM {}
unknown's avatar
unknown committed
3198
	| INDEXES {};
unknown's avatar
unknown committed
3199

3200
opt_unique_or_fulltext:
unknown's avatar
unknown committed
3201 3202
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
unknown's avatar
unknown committed
3203
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
unknown's avatar
unknown committed
3204 3205 3206 3207 3208
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
unknown's avatar
unknown committed
3209
            my_error(ER_FEATURE_DISABLED, MYF(0),
unknown's avatar
unknown committed
3210
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3211 3212 3213
	    YYABORT;
#endif
	  }
unknown's avatar
unknown committed
3214
        ;
unknown's avatar
unknown committed
3215 3216

key_alg:
3217
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
3218
	| USING opt_btree_or_rtree { $$= $2; }
unknown's avatar
unknown committed
3219
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
unknown's avatar
unknown committed
3220 3221 3222

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
unknown's avatar
unknown committed
3223 3224 3225 3226
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
unknown's avatar
unknown committed
3227
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
unknown's avatar
unknown committed
3228 3229 3230

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
unknown's avatar
unknown committed
3231
	| key_part order_dir		{ Lex->col_list.push_back($1); };
unknown's avatar
unknown committed
3232 3233 3234

key_part:
	ident			{ $$=new key_part_spec($1.str); }
3235 3236 3237 3238 3239
	| ident '(' NUM ')'	
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
3240
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
3241 3242 3243
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
unknown's avatar
unknown committed
3244 3245 3246

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

3249
opt_component:
3250 3251
        /* empty */      { $$= null_lex_str; }
        | '.' ident      { $$= $2; };
3252

unknown's avatar
unknown committed
3253 3254
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
unknown's avatar
unknown committed
3255
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
unknown's avatar
unknown committed
3256 3257 3258 3259 3260 3261 3262 3263

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
3264
	  THD *thd= YYTHD;
3265
	  LEX *lex= thd->lex;
3266 3267 3268
	  lex->sql_command= SQLCOM_ALTER_TABLE;
	  lex->name= 0;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
3269 3270
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
unknown's avatar
unknown committed
3271 3272 3273 3274
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
3275
          lex->select_lex.init_order();
unknown's avatar
unknown committed
3276 3277 3278
	  lex->select_lex.db=
            ((TABLE_LIST*) lex->select_lex.table_list.first)->db;
          lex->name=0;
3279
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
unknown's avatar
unknown committed
3280
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
3281
	  lex->create_info.default_table_charset= NULL;
3282
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
3283
	  lex->alter_info.reset();
3284
	  lex->alter_info.flags= 0;
unknown's avatar
unknown committed
3285
	}
3286 3287
	alter_list
	{}
3288
	| ALTER DATABASE ident_or_empty
3289 3290 3291 3292 3293
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
          opt_create_database_options
3294 3295
	  {
	    LEX *lex=Lex;
unknown's avatar
unknown committed
3296
            THD *thd= Lex->thd;
3297
	    lex->sql_command=SQLCOM_ALTER_DB;
3298
	    lex->name= $3;
unknown's avatar
unknown committed
3299 3300
            if (lex->name == NULL && thd->copy_db_to(&lex->name, NULL))
              YYABORT;
3301 3302 3303 3304 3305
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

3306 3307 3308 3309 3310
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
	      YYABORT;
	    }
3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322
	    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;
3323

3324 3325 3326 3327 3328
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
	      YYABORT;
	    }
3329 3330 3331 3332 3333 3334 3335 3336 3337
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    LEX *lex=Lex;

	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
	    lex->spname= $3;
	  }
3338 3339
        | ALTER view_algorithm_opt definer view_suid
          VIEW_SYM table_ident
3340 3341 3342 3343 3344 3345
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_CREATE_VIEW;
	    lex->create_view_mode= VIEW_ALTER;
	    /* first table in list is target VIEW name */
3346
	    lex->select_lex.add_table_to_list(thd, $6, NULL, TL_OPTION_UPDATING);
3347
	  }
3348
	  view_list_opt AS view_select view_check_option
3349 3350
	  {}
	;
3351

3352 3353 3354 3355
ident_or_empty:
	/* empty */  { $$= 0; }
	| ident      { $$= $1.str; };

unknown's avatar
unknown committed
3356
alter_list:
3357 3358
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
unknown's avatar
unknown committed
3359
        | alter_list_item
unknown's avatar
unknown committed
3360
	| alter_list ',' alter_list_item;
unknown's avatar
unknown committed
3361 3362

add_column:
3363
	ADD opt_column
3364 3365
	{
	  LEX *lex=Lex;
3366 3367
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
3368
	};
unknown's avatar
unknown committed
3369 3370

alter_list_item:
3371 3372 3373 3374
	add_column column_def opt_place { }
	| ADD key_def
	  {
	    Lex->alter_info.flags|= ALTER_ADD_INDEX;
3375
	  }
3376 3377 3378 3379
	| add_column '(' field_list ')'
          {
	    Lex->alter_info.flags|= ALTER_ADD_COLUMN | ALTER_ADD_INDEX;
          }
unknown's avatar
unknown committed
3380 3381 3382
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
3383
	     lex->change= $3.str;
3384
	     lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
3385
	  }
3386
          field_spec opt_place
unknown's avatar
unknown committed
3387 3388 3389
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
3390
            lex->length=lex->dec=0; lex->type=0;
3391
            lex->default_value= lex->on_update_value= 0;
3392
            lex->comment=null_lex_str;
3393
	    lex->charset= NULL;
3394
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
3395 3396 3397 3398
          }
          type opt_attribute
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
3399
            if (add_field_to_list(lex->thd,$3.str,
unknown's avatar
unknown committed
3400 3401
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
3402
                                  lex->default_value, lex->on_update_value,
3403
                                  &lex->comment,
3404
				  $3.str, &lex->interval_list, lex->charset,
unknown's avatar
unknown committed
3405
				  lex->uint_geom_type))
unknown's avatar
unknown committed
3406 3407 3408
	       YYABORT;
          }
          opt_place
unknown's avatar
unknown committed
3409
	| DROP opt_column field_ident opt_restrict
unknown's avatar
unknown committed
3410 3411
	  {
	    LEX *lex=Lex;
3412
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
3413
                                                               $3.str));
3414
	    lex->alter_info.flags|= ALTER_DROP_COLUMN;
unknown's avatar
unknown committed
3415
	  }
3416 3417 3418 3419
	| DROP FOREIGN KEY_SYM opt_ident
          {
	    Lex->alter_info.flags|= ALTER_DROP_INDEX;
          }
unknown's avatar
unknown committed
3420 3421 3422
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
3423 3424 3425
	    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
3426
	  }
unknown's avatar
unknown committed
3427
	| DROP key_or_index field_ident
unknown's avatar
unknown committed
3428 3429
	  {
	    LEX *lex=Lex;
3430 3431 3432
	    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
3433
	  }
3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445
	| 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
3446
	| ALTER opt_column field_ident SET DEFAULT signed_literal
unknown's avatar
unknown committed
3447 3448
	  {
	    LEX *lex=Lex;
3449
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,$6));
3450
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
3451
	  }
unknown's avatar
unknown committed
3452
	| ALTER opt_column field_ident DROP DEFAULT
unknown's avatar
unknown committed
3453 3454
	  {
	    LEX *lex=Lex;
3455 3456
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,
                                                                  (Item*) 0));
3457
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
3458
	  }
3459
	| RENAME opt_to table_ident
3460
	  {
3461
	    LEX *lex=Lex;
unknown's avatar
unknown committed
3462
            THD *thd= lex->thd;
3463
	    lex->select_lex.db=$3->db.str;
unknown's avatar
unknown committed
3464 3465 3466 3467 3468
            if (lex->select_lex.db == NULL &&
                thd->copy_db_to(&lex->select_lex.db, NULL))
            {
              YYABORT;
            }
3469 3470 3471
            if (check_table_name($3->table.str,$3->table.length) ||
                $3->db.str && check_db_name($3->db.str))
            {
3472
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3->table.str);
3473 3474
              YYABORT;
            }
unknown's avatar
unknown committed
3475
	    lex->name= $3->table.str;
3476
	    lex->alter_info.flags|= ALTER_RENAME;
3477
	  }
3478 3479 3480 3481 3482 3483 3484 3485 3486 3487
	| 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))
	    {
3488 3489
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $5->name, $4->csname);
3490 3491
	      YYABORT;
	    }
3492
	    LEX *lex= Lex;
3493
	    lex->create_info.table_charset=
unknown's avatar
unknown committed
3494 3495 3496
	      lex->create_info.default_table_charset= $5;
	    lex->create_info.used_fields|= (HA_CREATE_USED_CHARSET |
					    HA_CREATE_USED_DEFAULT_CHARSET);
3497
	    lex->alter_info.flags|= ALTER_CONVERT;
3498
	  }
3499
        | create_table_options_space_separated
3500 3501
	  {
	    LEX *lex=Lex;
3502
	    lex->alter_info.flags|= ALTER_OPTIONS;
3503
	  }
3504 3505 3506 3507
	| FORCE_SYM
	  {
	    Lex->alter_info.flags|= ALTER_FORCE;
	   }
3508
	| order_clause
3509 3510
	  {
	    LEX *lex=Lex;
3511
	    lex->alter_info.flags|= ALTER_ORDER;
3512
	  };
unknown's avatar
unknown committed
3513 3514 3515

opt_column:
	/* empty */	{}
unknown's avatar
unknown committed
3516
	| COLUMN_SYM	{};
unknown's avatar
unknown committed
3517 3518

opt_ignore:
3519 3520 3521
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
unknown's avatar
unknown committed
3522 3523

opt_restrict:
3524 3525 3526 3527
	/* empty */	{ Lex->drop_mode= DROP_DEFAULT; }
	| RESTRICT	{ Lex->drop_mode= DROP_RESTRICT; }
	| CASCADE	{ Lex->drop_mode= DROP_CASCADE; }
	;
unknown's avatar
unknown committed
3528 3529 3530 3531

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
unknown's avatar
unknown committed
3532
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
unknown's avatar
unknown committed
3533 3534 3535 3536

opt_to:
	/* empty */	{}
	| TO_SYM	{}
3537
	| EQ		{}
unknown's avatar
unknown committed
3538
	| AS		{};
unknown's avatar
unknown committed
3539

unknown's avatar
unknown committed
3540
/*
3541
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
unknown's avatar
unknown committed
3542 3543
*/

unknown's avatar
unknown committed
3544
slave:
3545
	  START_SYM SLAVE slave_thread_opts
3546 3547 3548 3549 3550 3551
          {
	    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
3552
            /* If you change this code don't forget to update SLAVE START too */
3553 3554 3555
          }
          slave_until
          {}
unknown's avatar
unknown committed
3556 3557 3558 3559 3560
        | STOP_SYM SLAVE slave_thread_opts
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_STOP;
	    lex->type = 0;
unknown's avatar
unknown committed
3561
            /* If you change this code don't forget to update SLAVE STOP too */
unknown's avatar
unknown committed
3562
          }
3563 3564 3565 3566 3567
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
3568 3569 3570 3571 3572
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
3573 3574 3575 3576 3577 3578 3579 3580
	| SLAVE STOP_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_STOP;
	   lex->type = 0;
         }
        ;

unknown's avatar
unknown committed
3581

unknown's avatar
unknown committed
3582
start:
3583 3584
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
3585 3586 3587
          LEX *lex= Lex;
          lex->sql_command= SQLCOM_BEGIN;
          lex->start_transaction_opt= $3;
3588
        }
unknown's avatar
unknown committed
3589 3590
	;

3591 3592 3593 3594 3595 3596
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
3597
        ;
3598

unknown's avatar
unknown committed
3599
slave_thread_opts:
3600 3601
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
3602
        {}
unknown's avatar
unknown committed
3603
	;
3604 3605

slave_thread_opt_list:
unknown's avatar
unknown committed
3606
	slave_thread_opt
3607 3608
	| slave_thread_opt_list ',' slave_thread_opt
	;
3609 3610

slave_thread_opt:
3611
	/*empty*/	{}
unknown's avatar
unknown committed
3612
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
unknown's avatar
unknown committed
3613
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
unknown's avatar
unknown committed
3614
	;
3615

3616 3617 3618 3619 3620 3621 3622 3623 3624 3625
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
3626 3627
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


unknown's avatar
unknown committed
3639 3640 3641 3642 3643
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
	}
unknown's avatar
unknown committed
3644
	table_list FROM TEXT_STRING_sys
unknown's avatar
unknown committed
3645 3646
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
3647
        };
unknown's avatar
unknown committed
3648

unknown's avatar
unknown committed
3649 3650 3651 3652 3653
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
	}
unknown's avatar
unknown committed
3654
	table_list TO_SYM TEXT_STRING_sys
unknown's avatar
unknown committed
3655 3656
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
3657
        };
unknown's avatar
unknown committed
3658

3659 3660 3661 3662 3663 3664
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
3665 3666
	table_list opt_checksum_type
        {}
3667 3668
	;

3669 3670 3671 3672 3673 3674
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
3675
repair:
3676
	REPAIR opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3677
	{
unknown's avatar
unknown committed
3678 3679
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
3680
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3681
	   lex->check_opt.init();
unknown's avatar
unknown committed
3682
	}
3683 3684 3685
	table_list opt_mi_repair_type
	{}
	;
unknown's avatar
unknown committed
3686

unknown's avatar
unknown committed
3687
opt_mi_repair_type:
unknown's avatar
unknown committed
3688
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
3689
	| mi_repair_types {};
unknown's avatar
unknown committed
3690

unknown's avatar
unknown committed
3691 3692
mi_repair_types:
	mi_repair_type {}
unknown's avatar
unknown committed
3693
	| mi_repair_type mi_repair_types {};
3694

unknown's avatar
unknown committed
3695
mi_repair_type:
unknown's avatar
unknown committed
3696
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
3697
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
unknown's avatar
unknown committed
3698
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
unknown's avatar
unknown committed
3699 3700

analyze:
3701
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3702
	{
unknown's avatar
unknown committed
3703 3704
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
3705
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3706
	   lex->check_opt.init();
unknown's avatar
unknown committed
3707
	}
3708 3709 3710
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3711 3712

check:
unknown's avatar
unknown committed
3713
	CHECK_SYM table_or_tables
unknown's avatar
unknown committed
3714
	{
3715 3716 3717 3718 3719 3720 3721 3722 3723
	  LEX *lex=Lex;

	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "CHECK");
	    YYABORT;
	  }
	  lex->sql_command = SQLCOM_CHECK;
	  lex->check_opt.init();
unknown's avatar
unknown committed
3724
	}
3725 3726 3727
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3728

unknown's avatar
unknown committed
3729 3730
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
3731
	| mi_check_types {};
unknown's avatar
unknown committed
3732 3733 3734

mi_check_types:
	mi_check_type {}
unknown's avatar
unknown committed
3735
	| mi_check_type mi_check_types {};
unknown's avatar
unknown committed
3736 3737 3738 3739 3740 3741

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
3742 3743
	| CHANGED  { Lex->check_opt.flags|= T_CHECK_ONLY_CHANGED; }
        | FOR_SYM UPGRADE_SYM { Lex->check_opt.sql_flags|= TT_FOR_UPGRADE; };
unknown's avatar
unknown committed
3744

unknown's avatar
unknown committed
3745
optimize:
3746
	OPTIMIZE opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3747
	{
unknown's avatar
unknown committed
3748 3749
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
unknown's avatar
unknown committed
3750
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3751
	   lex->check_opt.init();
unknown's avatar
unknown committed
3752
	}
3753 3754 3755
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3756

3757 3758 3759
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
3760
	| LOCAL_SYM  { $$= 1; }
3761 3762
	;

3763 3764 3765
rename:
	RENAME table_or_tables
	{
3766
          Lex->sql_command= SQLCOM_RENAME_TABLE;
3767
	}
3768 3769
	table_to_table_list
	{}
3770 3771 3772 3773
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
3774
	;
3775

3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788
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;
          }
        ;

3789 3790
table_to_table_list:
	table_to_table
unknown's avatar
unknown committed
3791
	| table_to_table_list ',' table_to_table;
3792 3793 3794

table_to_table:
	table_ident TO_SYM table_ident
3795
	{
unknown's avatar
unknown committed
3796
	  LEX *lex=Lex;
unknown's avatar
unknown committed
3797
	  SELECT_LEX *sl= lex->current_select;
unknown's avatar
unknown committed
3798 3799 3800 3801
	  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))
3802
	    YYABORT;
3803
	};
3804

unknown's avatar
unknown committed
3805
keycache:
unknown's avatar
unknown committed
3806
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
unknown's avatar
unknown committed
3807 3808
        {
          LEX *lex=Lex;
3809
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
3810
	  lex->ident= $5;
unknown's avatar
unknown committed
3811 3812 3813 3814 3815 3816 3817 3818
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
3819
        table_ident cache_keys_spec
unknown's avatar
unknown committed
3820 3821 3822 3823 3824 3825
        {
          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(),
3826
                                      (List<String> *)0))
unknown's avatar
unknown committed
3827 3828 3829 3830
            YYABORT;
        }
        ;

3831 3832 3833
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
3834
	;
3835

unknown's avatar
unknown committed
3836
preload:
unknown's avatar
unknown committed
3837
	LOAD INDEX_SYM INTO CACHE_SYM
unknown's avatar
unknown committed
3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850
	{
	  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
3851
	table_ident cache_keys_spec opt_ignore_leaves
unknown's avatar
unknown committed
3852 3853 3854
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
unknown's avatar
unknown committed
3855 3856
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
unknown's avatar
unknown committed
3857 3858 3859 3860 3861 3862
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

3863
cache_keys_spec:
3864
        { Select->interval_list.empty(); }
3865 3866 3867 3868 3869 3870 3871
        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
3872

unknown's avatar
unknown committed
3873
cache_key_list_or_empty:
3874
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
3875
	| opt_key_or_index '(' key_usage_list2 ')'
3876 3877 3878 3879
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
3880 3881
	;

unknown's avatar
unknown committed
3882
opt_ignore_leaves:
unknown's avatar
unknown committed
3883 3884 3885 3886 3887
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

unknown's avatar
unknown committed
3888
/*
unknown's avatar
unknown committed
3889
  Select : retrieve data from table
unknown's avatar
unknown committed
3890 3891 3892 3893
*/


select:
3894 3895 3896 3897
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
unknown's avatar
unknown committed
3898 3899
	}
	;
unknown's avatar
unknown committed
3900

3901
/* Need select_init2 for subselects. */
unknown's avatar
unknown committed
3902
select_init:
3903
	SELECT_SYM select_init2
3904
	|
unknown's avatar
unknown committed
3905 3906 3907 3908
	'(' select_paren ')' union_opt;

select_paren:
	SELECT_SYM select_part2
3909
	  {
3910
	    LEX *lex= Lex;
unknown's avatar
unknown committed
3911
            SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
3912
	    if (sel->set_braces(1))
3913
	    {
3914
	      yyerror(ER(ER_SYNTAX_ERROR));
3915 3916
	      YYABORT;
	    }
3917 3918
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
3919
	  {
3920
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
3921 3922
	    YYABORT;
	  }
3923
            /* select in braces, can't contain global parameters */
unknown's avatar
unknown committed
3924 3925 3926
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
unknown's avatar
unknown committed
3927 3928
          }
	| '(' select_paren ')';
3929

3930 3931
select_init2:
	select_part2
3932
        {
3933
	  LEX *lex= Lex;
unknown's avatar
unknown committed
3934
          SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
3935
          if (lex->current_select->set_braces(0))
3936
	  {
3937
	    yyerror(ER(ER_SYNTAX_ERROR));
3938 3939
	    YYABORT;
	  }
3940 3941
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
3942
	  {
3943
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
3944 3945
	    YYABORT;
	  }
3946
	}
3947
	union_clause
3948 3949
	;

3950
select_part2:
unknown's avatar
unknown committed
3951
	{
unknown's avatar
unknown committed
3952 3953
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
3954 3955
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
3956
	  lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
3957 3958 3959
	}
	select_options select_item_list
	{
3960
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
3961
	}
unknown's avatar
unknown committed
3962
	select_into select_lock_type;
3963

unknown's avatar
unknown committed
3964
select_into:
unknown's avatar
unknown committed
3965
	opt_order_clause opt_limit_clause {}
3966
        | into
unknown's avatar
unknown committed
3967
	| select_from
3968 3969
	| into select_from
	| select_from into;
unknown's avatar
unknown committed
3970 3971

select_from:
3972 3973
	  FROM join_table_list where_clause group_clause having_clause
	       opt_order_clause opt_limit_clause procedure_clause
unknown's avatar
unknown committed
3974
        | FROM DUAL_SYM where_clause opt_limit_clause
unknown's avatar
unknown committed
3975 3976 3977 3978
          /* 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 :) */
3979
	;
unknown's avatar
unknown committed
3980 3981 3982

select_options:
	/* empty*/
unknown's avatar
unknown committed
3983 3984 3985 3986
	| select_option_list
	  {
	    if (test_all_bits(Select->options, SELECT_ALL | SELECT_DISTINCT))
	    {
unknown's avatar
unknown committed
3987
	      my_error(ER_WRONG_USAGE, MYF(0), "ALL", "DISTINCT");
unknown's avatar
unknown committed
3988 3989 3990
              YYABORT;
	    }
          }
unknown's avatar
unknown committed
3991
	  ;
unknown's avatar
unknown committed
3992 3993 3994

select_option_list:
	select_option_list select_option
unknown's avatar
unknown committed
3995
	| select_option;
unknown's avatar
unknown committed
3996 3997

select_option:
3998
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
3999 4000 4001 4002 4003 4004
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
unknown's avatar
unknown committed
4005
	| DISTINCT         { Select->options|= SELECT_DISTINCT; }
4006 4007
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019
	| 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;
	  }
4020 4021 4022 4023 4024 4025
	| SQL_NO_CACHE_SYM
          {
            Lex->safe_to_cache_query=0;
	    Lex->select_lex.options&= ~OPTION_TO_QUERY_CACHE;
            Lex->select_lex.sql_cache= SELECT_LEX::SQL_NO_CACHE;
          }
4026 4027
	| SQL_CACHE_SYM
	  {
4028 4029 4030 4031 4032 4033 4034
            /* Honor this flag only if SQL_NO_CACHE wasn't specified. */
            if (Lex->select_lex.sql_cache != SELECT_LEX::SQL_NO_CACHE)
            {
              Lex->safe_to_cache_query=1;
	      Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
              Lex->select_lex.sql_cache= SELECT_LEX::SQL_CACHE;
            }
4035
	  }
unknown's avatar
unknown committed
4036
	| ALL		    { Select->options|= SELECT_ALL; }
4037
	;
unknown's avatar
unknown committed
4038

unknown's avatar
unknown committed
4039 4040 4041
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
4042 4043
	  {
	    LEX *lex=Lex;
4044
	    lex->current_select->set_lock_for_tables(TL_WRITE);
4045
	    lex->safe_to_cache_query=0;
4046
	  }
unknown's avatar
unknown committed
4047
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
4048 4049
	  {
	    LEX *lex=Lex;
4050 4051
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
4052
	    lex->safe_to_cache_query=0;
4053 4054
	  }
	;
unknown's avatar
unknown committed
4055

unknown's avatar
unknown committed
4056 4057 4058 4059 4060
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
4061
	    THD *thd= YYTHD;
4062 4063 4064 4065
	    if (add_item_to_list(thd,
                                 new Item_field(&thd->lex->current_select->
                                                context,
                                                NULL, NULL, "*")))
unknown's avatar
unknown committed
4066
	      YYABORT;
4067
	    (thd->lex->current_select->with_wild)++;
unknown's avatar
unknown committed
4068
	  };
unknown's avatar
unknown committed
4069 4070 4071 4072 4073


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
unknown's avatar
unknown committed
4074
	    if (add_item_to_list(YYTHD, $2))
unknown's avatar
unknown committed
4075 4076
	      YYABORT;
	    if ($4.str)
4077 4078
            {
              $2->is_autogenerated_name= FALSE;
4079
	      $2->set_name($4.str, $4.length, system_charset_info);
4080
            }
4081 4082 4083 4084 4085 4086
	    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
4087
	  };
unknown's avatar
unknown committed
4088 4089

remember_name:
unknown's avatar
unknown committed
4090
	{ $$=(char*) Lex->tok_start; };
unknown's avatar
unknown committed
4091 4092

remember_end:
unknown's avatar
unknown committed
4093
	{ $$=(char*) Lex->tok_end; };
unknown's avatar
unknown committed
4094 4095 4096

select_item2:
	table_wild	{ $$=$1; } /* table.* */
unknown's avatar
unknown committed
4097
	| expr		{ $$=$1; };
unknown's avatar
unknown committed
4098 4099

select_alias:
4100
	/* empty */		{ $$=null_lex_str;}
unknown's avatar
unknown committed
4101 4102 4103 4104 4105
	| AS ident		{ $$=$2; }
	| AS TEXT_STRING_sys	{ $$=$2; }
	| ident			{ $$=$1; }
	| TEXT_STRING_sys	{ $$=$1; }
	;
unknown's avatar
unknown committed
4106 4107 4108

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

unknown's avatar
unknown committed
4111
/* all possible expressions */
unknown's avatar
SCRUM  
unknown committed
4112
expr:	
unknown's avatar
unknown committed
4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134
	  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); }
        ;
4135 4136

bool_term:
unknown's avatar
unknown committed
4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159
	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); }
        ;
4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176

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); }
4177 4178 4179 4180 4181
	| 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); }
4182 4183 4184 4185
	| predicate ;

predicate:
	 bit_expr IN_SYM '(' expr_list ')'
unknown's avatar
unknown committed
4186 4187 4188 4189 4190 4191 4192 4193 4194
	  { 
            if ($4->elements == 1)
              $$= new Item_func_eq($1, $4->head());
            else
            {
              $4->push_front($1);
              $$= new Item_func_in(*$4);
            }
          }
4195
	| bit_expr not IN_SYM '(' expr_list ')'
unknown's avatar
unknown committed
4196 4197 4198 4199 4200 4201
          {
            if ($5->elements == 1)
              $$= new Item_func_ne($1, $5->head());
            else
            {
              $5->push_front($1);
4202 4203 4204
              Item_func_in *item = new Item_func_in(*$5);
              item->negate();
              $$= item;
unknown's avatar
unknown committed
4205 4206
            }            
          }
4207 4208 4209
        | bit_expr IN_SYM in_subselect
	  { $$= new Item_in_subselect($1, $3); }
	| bit_expr not IN_SYM in_subselect
4210
          { $$= negate_expression(YYTHD, new Item_in_subselect($1, $4)); }
4211 4212 4213
	| 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
4214 4215 4216 4217 4218
    {
      Item_func_between *item= new Item_func_between($1,$4,$6);
      item->negate();
      $$= item;
    }
4219 4220 4221 4222
	| 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
4223
          { $$= new Item_func_like($1,$3,$4,Lex->escape_used); }
4224
	| bit_expr not LIKE simple_expr opt_escape
4225
          { $$= new Item_func_not(new Item_func_like($1,$4,$5, Lex->escape_used)); }
4226 4227
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
4228
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270
	| 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
4271

unknown's avatar
unknown committed
4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283
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; }
        ;

4284
interval_expr:
unknown's avatar
unknown committed
4285
         INTERVAL_SYM expr { $$=$2; }
4286 4287
        ;

unknown's avatar
unknown committed
4288 4289
simple_expr:
	simple_ident
4290
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
unknown's avatar
unknown committed
4291
	  {
unknown's avatar
unknown committed
4292 4293 4294
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
4295
                                                            YYTHD->charset()));
4296
	  }
unknown's avatar
unknown committed
4297
	| literal
unknown's avatar
unknown committed
4298
	| param_marker
unknown's avatar
unknown committed
4299
	| '@' ident_or_text SET_VAR expr
unknown's avatar
unknown committed
4300 4301
	  {
	    $$= new Item_func_set_user_var($2,$4);
4302 4303 4304
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4305
	  }
4306
	| '@' ident_or_text
unknown's avatar
unknown committed
4307 4308
	  {
	    $$= new Item_func_get_user_var($2);
4309 4310 4311
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4312
	  }
4313
	| '@' '@' opt_var_ident_type ident_or_text opt_component
unknown's avatar
unknown committed
4314
	  {
4315 4316 4317 4318 4319 4320

            if ($4.str && $5.str && check_reserved_words(&$4))
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
4321
	    if (!($$= get_system_var(YYTHD, $3, $4, $5)))
unknown's avatar
unknown committed
4322
	      YYABORT;
4323
	    Lex->variables_used= 1;
unknown's avatar
unknown committed
4324
	  }
unknown's avatar
unknown committed
4325
	| sum_expr
4326 4327 4328 4329 4330 4331
	| 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
4332
	| '(' expr ')'		{ $$= $2; }
4333 4334 4335 4336 4337
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
4338
	| ROW_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
4339
	  {
4340 4341
	    $5->push_front($3);
	    $$= new Item_row(*$5);
unknown's avatar
unknown committed
4342
	  }
unknown's avatar
unknown committed
4343
	| EXISTS exists_subselect { $$= $2; }
unknown's avatar
unknown committed
4344
	| singlerow_subselect   { $$= $1; }
unknown's avatar
unknown committed
4345
	| '{' ident expr '}'	{ $$= $3; }
4346
        | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
4347
          { $2->push_front($5);
4348 4349
            Select->add_ftfunc_to_list((Item_func_match*)
                                        ($$=new Item_func_match(*$2,$6))); }
unknown's avatar
unknown committed
4350
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
4351
	| BINARY simple_expr %prec NEG
unknown's avatar
unknown committed
4352
	  {
unknown's avatar
unknown committed
4353
            $$= create_func_cast($2, ITEM_CAST_CHAR, -1, 0, &my_charset_bin);
unknown's avatar
unknown committed
4354
	  }
4355
	| CAST_SYM '(' expr AS cast_type ')'
4356
	  {
unknown's avatar
unknown committed
4357
            LEX *lex= Lex;
4358
	    $$= create_func_cast($3, $5,
unknown's avatar
unknown committed
4359 4360 4361
                                 lex->length ? atoi(lex->length) : -1,
                                 lex->dec ? atoi(lex->dec) : 0,
                                 lex->charset);
4362 4363
            if (!$$)
              YYABORT;
4364
	  }
unknown's avatar
unknown committed
4365
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
4366
	  { $$= new Item_func_case(* $4, $2, $5 ); }
4367
	| CONVERT_SYM '(' expr ',' cast_type ')'
4368 4369 4370
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
unknown's avatar
unknown committed
4371
                                 Lex->dec ? atoi(Lex->dec) : 0,
4372
				 Lex->charset);
4373 4374
            if (!$$)
              YYABORT;
4375
	  }
4376 4377
	| CONVERT_SYM '(' expr USING charset_name ')'
	  { $$= new Item_func_conv_charset($3,$5); }
unknown's avatar
SCRUM  
unknown committed
4378
	| DEFAULT '(' simple_ident ')'
4379 4380 4381 4382 4383
	  {
	    if ($3->is_splocal())
	    {
	      Item_splocal *il= static_cast<Item_splocal *>($3);

4384
	      my_error(ER_WRONG_COLUMN_NAME, MYF(0), il->my_name()->str);
4385 4386
	      YYABORT;
	    }
unknown's avatar
unknown committed
4387
	    $$= new Item_default_value(Lex->current_context(), $3);
4388
	  }
4389
	| VALUES '(' simple_ident_nospvar ')'
unknown's avatar
unknown committed
4390
	  { $$= new Item_insert_value(Lex->current_context(), $3); }
unknown's avatar
unknown committed
4391
	| FUNC_ARG0 '(' ')'
unknown's avatar
unknown committed
4392 4393 4394
	  {
	    if (!$1.symbol->create_func)
	    {
4395 4396 4397
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
4398 4399 4400 4401
	      YYABORT;
	    }
	    $$= ((Item*(*)(void))($1.symbol->create_func))();
	  }
unknown's avatar
unknown committed
4402
	| FUNC_ARG1 '(' expr ')'
unknown's avatar
unknown committed
4403 4404 4405
	  {
	    if (!$1.symbol->create_func)
	    {
4406 4407 4408
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
4409 4410 4411 4412
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);
	  }
unknown's avatar
unknown committed
4413
	| FUNC_ARG2 '(' expr ',' expr ')'
unknown's avatar
unknown committed
4414 4415 4416
	  {
	    if (!$1.symbol->create_func)
	    {
4417 4418 4419
	      my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
4420 4421 4422 4423
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);
	  }
unknown's avatar
unknown committed
4424
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
unknown's avatar
unknown committed
4425 4426 4427
	  {
	    if (!$1.symbol->create_func)
	    {
4428 4429 4430
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
4431 4432 4433 4434
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*,Item*))($1.symbol->create_func))($3,$5,$7);
	  }
unknown's avatar
unknown committed
4435 4436 4437 4438
	| 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); }
4439 4440
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
unknown's avatar
unknown committed
4441 4442 4443 4444 4445 4446
	| 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); }
4447 4448
	| CHAR_SYM '(' expr_list USING charset_name ')'
	  { $$= new Item_func_char(*$3, $5); }
unknown's avatar
unknown committed
4449 4450
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
unknown's avatar
unknown committed
4451 4452
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
unknown's avatar
unknown committed
4453 4454
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
unknown's avatar
unknown committed
4455 4456 4457
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
4458
	  { $5->push_front($3); $$= new Item_func_concat_ws(*$5); }
4459 4460
	| CONVERT_TZ_SYM '(' expr ',' expr ',' expr ')'
	  {
4461 4462
            if (Lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
4463 4464
	    $$= new Item_func_convert_tz($3, $5, $7);
	  }
unknown's avatar
unknown committed
4465
	| CURDATE optional_braces
4466
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
4467
	| CURTIME optional_braces
4468
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
4469
	| CURTIME '(' expr ')'
4470
	  {
4471
	    $$= new Item_func_curtime_local($3);
4472
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4473
	  }
unknown's avatar
unknown committed
4474
	| CURRENT_USER optional_braces
4475 4476 4477 4478
          {
            $$= new Item_func_current_user(Lex->current_context());
            Lex->safe_to_cache_query= 0;
          }
4479 4480 4481 4482
	| 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
4483
	| DATABASE '(' ')'
4484
	  {
unknown's avatar
unknown committed
4485
	    $$= new Item_func_database();
4486
            Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4487
	  }
unknown's avatar
unknown committed
4488
	| DATE_SYM '(' expr ')'
unknown's avatar
unknown committed
4489
	  { $$= new Item_date_typecast($3); }
unknown's avatar
unknown committed
4490 4491
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
unknown's avatar
unknown committed
4492
	| ELT_FUNC '(' expr ',' expr_list ')'
4493
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
unknown's avatar
unknown committed
4494 4495
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
unknown's avatar
unknown committed
4496 4497 4498
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
4499
	    Lex->uncacheable(UNCACHEABLE_RAND);
unknown's avatar
unknown committed
4500
	  }
unknown's avatar
unknown committed
4501
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
4502
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
4503
	  { $$= new Item_func_decode($3,$5.str); }
4504
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
4505
	 { $$= new Item_func_encode($3,$5.str); }
unknown's avatar
unknown committed
4506
	| DES_DECRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
4507
        { $$= new Item_func_des_decrypt($3); }
unknown's avatar
unknown committed
4508
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
4509
        { $$= new Item_func_des_decrypt($3,$5); }
unknown's avatar
unknown committed
4510
	| DES_ENCRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
4511
        { $$= new Item_func_des_encrypt($3); }
unknown's avatar
unknown committed
4512
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
4513
        { $$= new Item_func_des_encrypt($3,$5); }
unknown's avatar
unknown committed
4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525
	| EXPORT_SET '(' expr ',' expr ',' expr ')'
		{ $$= new Item_func_export_set($3, $5, $7); }
	| EXPORT_SET '(' expr ',' expr ',' expr ',' expr ')'
		{ $$= new Item_func_export_set($3, $5, $7, $9); }
	| EXPORT_SET '(' expr ',' expr ',' expr ',' expr ',' expr ')'
		{ $$= new Item_func_export_set($3, $5, $7, $9, $11); }
	| FORMAT_SYM '(' expr ',' NUM ')'
	  { $$= new Item_func_format($3,atoi($5.str)); }
	| FROM_UNIXTIME '(' expr ')'
	  { $$= new Item_func_from_unixtime($3); }
	| FROM_UNIXTIME '(' expr ',' expr ')'
	  {
unknown's avatar
unknown committed
4526
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
unknown's avatar
unknown committed
4527 4528
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
4529
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
unknown's avatar
unknown committed
4530 4531 4532 4533 4534
	| geometry_function
	  {
#ifdef HAVE_SPATIAL
	    $$= $1;
#else
4535 4536
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
4537 4538 4539
	    YYABORT;
#endif
	  }
4540
	| GET_FORMAT '(' date_time_type  ',' expr ')'
4541
	  { $$= new Item_func_get_format($3, $5); }
unknown's avatar
unknown committed
4542 4543 4544 4545 4546 4547
	| 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); }
4548
	| interval_expr interval '+' expr
unknown's avatar
unknown committed
4549
	  /* we cannot put interval before - */
4550 4551 4552 4553 4554
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
4555
              yyerror(ER(ER_SYNTAX_ERROR));
4556 4557 4558 4559
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
unknown's avatar
unknown committed
4560 4561
	| LAST_INSERT_ID '(' ')'
	  {
4562
	    $$= new Item_func_last_insert_id();
4563
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
4564 4565 4566
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
4567
	    $$= new Item_func_last_insert_id($3);
4568
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
4569 4570 4571 4572 4573 4574 4575
	  }
	| 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
4576
	| GREATEST_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
4577 4578 4579
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
4580
	| LOG_SYM '(' expr ')'
unknown's avatar
unknown committed
4581
	  { $$= new Item_func_log($3); }
4582
	| LOG_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
4583
	  { $$= new Item_func_log($3, $5); }
4584
	| MASTER_POS_WAIT '(' expr ',' expr ')'
unknown's avatar
unknown committed
4585
	  {
4586
	    $$= new Item_master_pos_wait($3, $5);
unknown's avatar
unknown committed
4587
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4588
		  }
4589
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
unknown's avatar
unknown committed
4590
	  {
4591
	    $$= new Item_master_pos_wait($3, $5, $7);
unknown's avatar
unknown committed
4592
	    Lex->safe_to_cache_query=0;
4593
	  }
unknown's avatar
unknown committed
4594 4595
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
unknown's avatar
unknown committed
4596 4597
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
4598 4599
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
unknown's avatar
unknown committed
4600 4601 4602
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
	| NOW_SYM optional_braces
4603
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
4604
	| NOW_SYM '(' expr ')'
4605
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
4606
	| PASSWORD '(' expr ')'
4607
	  {
4608 4609
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
unknown's avatar
unknown committed
4610 4611 4612 4613
	      (Item *) new Item_func_password($3);
	  }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new Item_func_old_password($3); }
4614
	| POSITION_SYM '(' bit_expr IN_SYM expr ')'
unknown's avatar
unknown committed
4615
	  { $$ = new Item_func_locate($5,$3); }
4616 4617
	| QUARTER_SYM '(' expr ')'
	  { $$ = new Item_func_quarter($3); }
unknown's avatar
unknown committed
4618
	| RAND '(' expr ')'
4619
	  { $$= new Item_func_rand($3); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
4620
	| RAND '(' ')'
4621
	  { $$= new Item_func_rand(); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
4622 4623 4624 4625 4626 4627 4628
	| 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); }
4629 4630 4631 4632 4633
	| ROW_COUNT_SYM '(' ')'
	  {
	    $$= new Item_func_row_count();
	    Lex->safe_to_cache_query= 0;
	  }
unknown's avatar
unknown committed
4634 4635 4636 4637
	| 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
4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649
	| 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); }
4650
	| SYSDATE optional_braces
4651 4652 4653 4654 4655 4656
          {
            if (global_system_variables.sysdate_is_now == 0)
              $$= new Item_func_sysdate_local();
            else $$= new Item_func_now_local();
            Lex->safe_to_cache_query=0;
          }
4657
	| SYSDATE '(' expr ')'
4658 4659 4660 4661 4662 4663
          {
            if (global_system_variables.sysdate_is_now == 0)
              $$= new Item_func_sysdate_local($3);
            else $$= new Item_func_now_local($3);
            Lex->safe_to_cache_query=0;
          }
unknown's avatar
unknown committed
4664
	| TIME_SYM '(' expr ')'
unknown's avatar
unknown committed
4665 4666 4667
	  { $$= new Item_time_typecast($3); }
	| TIMESTAMP '(' expr ')'
	  { $$= new Item_datetime_typecast($3); }
unknown's avatar
unknown committed
4668
	| TIMESTAMP '(' expr ',' expr ')'
unknown's avatar
unknown committed
4669
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
4670 4671 4672 4673
	| 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
4674
	| TRIM '(' expr ')'
4675 4676
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
unknown's avatar
unknown committed
4677
	  { $$= new Item_func_ltrim($6,$4); }
4678
	| TRIM '(' TRAILING expr FROM expr ')'
unknown's avatar
unknown committed
4679
	  { $$= new Item_func_rtrim($6,$4); }
4680
	| TRIM '(' BOTH expr FROM expr ')'
unknown's avatar
unknown committed
4681
	  { $$= new Item_func_trim($6,$4); }
4682 4683 4684 4685 4686 4687
	| 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
4688 4689
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
4690 4691
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
4692
	| ident '.' ident '(' udf_expr_list ')'
unknown's avatar
unknown committed
4693
	  {
4694
	    LEX *lex= Lex;
4695 4696 4697
	    sp_name *name= new sp_name($1, $3);

	    name->init_qname(YYTHD);
4698
	    sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
4699
	    if ($5)
unknown's avatar
unknown committed
4700
	      $$= new Item_func_sp(Lex->current_context(), name, *$5);
unknown's avatar
unknown committed
4701
	    else
unknown's avatar
unknown committed
4702
	      $$= new Item_func_sp(Lex->current_context(), name);
4703
	    lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4704
	  }
4705
	| IDENT_sys '(' 
4706 4707
          {
#ifdef HAVE_DLOPEN
4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726
            udf_func *udf= 0;
            if (using_udf_functions &&
                (udf= find_udf($1.str, $1.length)) &&
                udf->type == UDFTYPE_AGGREGATE)
            {
              LEX *lex= Lex;
              if (lex->current_select->inc_in_sum_expr())
              {
                yyerror(ER(ER_SYNTAX_ERROR));
                YYABORT;
              }
            }
            $<udf>$= udf;
#endif
          }
          udf_expr_list ')'
          {
#ifdef HAVE_DLOPEN
            udf_func *udf= $<udf>3;
4727
            SELECT_LEX *sel= Select;
4728

4729
            if (udf)
4730
            {
4731 4732 4733
              if (udf->type == UDFTYPE_AGGREGATE)
                Select->in_sum_expr--;

4734 4735 4736 4737
              switch (udf->returns) {
              case STRING_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
4738 4739
                  if ($4 != NULL)
                    $$ = new Item_func_udf_str(udf, *$4);
4740 4741 4742 4743 4744
                  else
                    $$ = new Item_func_udf_str(udf);
                }
                else
                {
4745 4746
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_str(udf, *$4);
4747 4748 4749 4750 4751 4752 4753
                  else
                    $$ = new Item_sum_udf_str(udf);
                }
                break;
              case REAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
4754 4755
                  if ($4 != NULL)
                    $$ = new Item_func_udf_float(udf, *$4);
4756 4757 4758 4759 4760
                  else
                    $$ = new Item_func_udf_float(udf);
                }
                else
                {
4761 4762
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_float(udf, *$4);
4763 4764 4765 4766 4767 4768 4769
                  else
                    $$ = new Item_sum_udf_float(udf);
                }
                break;
              case INT_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
4770 4771
                  if ($4 != NULL)
                    $$ = new Item_func_udf_int(udf, *$4);
4772 4773 4774 4775 4776
                  else
                    $$ = new Item_func_udf_int(udf);
                }
                else
                {
4777 4778
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_int(udf, *$4);
4779 4780 4781 4782
                  else
                    $$ = new Item_sum_udf_int(udf);
                }
                break;
unknown's avatar
unknown committed
4783 4784 4785
              case DECIMAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
4786 4787
                  if ($4 != NULL)
                    $$ = new Item_func_udf_decimal(udf, *$4);
unknown's avatar
unknown committed
4788 4789 4790 4791 4792
                  else
                    $$ = new Item_func_udf_decimal(udf);
                }
                else
                {
4793 4794
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_decimal(udf, *$4);
unknown's avatar
unknown committed
4795 4796 4797 4798
                  else
                    $$ = new Item_sum_udf_decimal(udf);
                }
                break;
4799 4800 4801 4802 4803 4804 4805
              default:
                YYABORT;
              }
            }
            else
#endif /* HAVE_DLOPEN */
            {
4806
	      LEX *lex= Lex;
unknown's avatar
unknown committed
4807 4808 4809 4810 4811 4812 4813
              THD *thd= lex->thd;
              LEX_STRING db;
              if (thd->copy_db_to(&db.str, &db.length))
                YYABORT;
              sp_name *name= new sp_name(db, $1);
              if (name)
                name->init_qname(thd);
4814

4815
              sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
4816 4817
              if ($4)
                $$= new Item_func_sp(Lex->current_context(), name, *$4);
4818
              else
unknown's avatar
unknown committed
4819
                $$= new Item_func_sp(Lex->current_context(), name);
4820
	      lex->safe_to_cache_query=0;
4821 4822
	    }
          }
unknown's avatar
unknown committed
4823
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
4824
	  {
unknown's avatar
unknown committed
4825 4826
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
unknown's avatar
unknown committed
4827
	| UNIX_TIMESTAMP '(' ')'
unknown's avatar
unknown committed
4828 4829
	  {
	    $$= new Item_func_unix_timestamp();
4830
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
4831
	  }
unknown's avatar
unknown committed
4832 4833 4834
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
4835
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
4836 4837 4838 4839 4840 4841
	| 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
4842
	| WEEK_SYM '(' expr ')'
unknown's avatar
unknown committed
4843
	  {
4844
            $$= new Item_func_week($3,new Item_int((char*) "0",
unknown's avatar
unknown committed
4845
				   YYTHD->variables.default_week_format,1));
4846
          }
unknown's avatar
unknown committed
4847 4848 4849 4850 4851 4852 4853 4854
	| WEEK_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_week($3,$5); }
	| YEAR_SYM '(' expr ')'
	  { $$= new Item_func_year($3); }
	| YEARWEEK '(' expr ')'
	  { $$= new Item_func_yearweek($3,new Item_int((char*) "0",0,1)); }
	| YEARWEEK '(' expr ',' expr ')'
	  { $$= new Item_func_yearweek($3, $5); }
unknown's avatar
unknown committed
4855
	| BENCHMARK_SYM '(' ulong_num ',' expr ')'
4856
	  {
unknown's avatar
unknown committed
4857
	    $$=new Item_func_benchmark($3,$5);
4858
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
4859
	  }
unknown's avatar
unknown committed
4860
	| EXTRACT_SYM '(' interval FROM expr ')'
unknown's avatar
unknown committed
4861
	{ $$=new Item_extract( $3, $5); };
unknown's avatar
unknown committed
4862

unknown's avatar
unknown committed
4863
geometry_function:
unknown's avatar
unknown committed
4864 4865 4866
	  CONTAINS_SYM '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_spatial_rel($3, $5, Item_func::SP_CONTAINS_FUNC)); }
	| GEOMFROMTEXT '(' expr ')'
unknown's avatar
unknown committed
4867 4868 4869 4870 4871 4872 4873 4874 4875
	  { $$= 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,
4876 4877
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
unknown's avatar
unknown committed
4878 4879
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4880
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
unknown's avatar
unknown committed
4881 4882
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
4883
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897
 	| 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,
4898
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
unknown's avatar
unknown committed
4899 4900
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4901
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
unknown's avatar
unknown committed
4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913
	| 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,
4914
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
4915 4916 4917 4918 4919 4920 4921 4922 4923 4924
 	| 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)); }
	;

4925 4926 4927 4928 4929 4930
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
4931
udf_expr_list:
4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956
	/* 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)
4957 4958
          {
            $2->is_autogenerated_name= FALSE;
4959
	    $2->set_name($4.str, $4.length, system_charset_info);
4960
          }
4961
	  else
4962
	    $2->set_name($1, (uint) ($3 - $1), YYTHD->charset());
4963 4964 4965
	  $$= $2;
	}
	;
unknown's avatar
unknown committed
4966 4967 4968 4969

sum_expr:
	AVG_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_avg($3); }
4970 4971
	| AVG_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_avg_distinct($4); }
unknown's avatar
unknown committed
4972 4973 4974 4975
	| BIT_AND  '(' in_sum_expr ')'
	  { $$=new Item_sum_and($3); }
	| BIT_OR  '(' in_sum_expr ')'
	  { $$=new Item_sum_or($3); }
4976 4977
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
unknown's avatar
unknown committed
4978
	| COUNT_SYM '(' opt_all '*' ')'
unknown's avatar
unknown committed
4979 4980 4981
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
4982
	| COUNT_SYM '(' DISTINCT
unknown's avatar
unknown committed
4983
	  { Select->in_sum_expr++; }
4984
	   expr_list
unknown's avatar
unknown committed
4985
	  { Select->in_sum_expr--; }
4986 4987
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
unknown's avatar
unknown committed
4988 4989 4990 4991
	| 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); }
4992 4993 4994 4995 4996 4997 4998
/*
   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
4999 5000
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
5001 5002
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
unknown's avatar
unknown committed
5003
	| STD_SYM '(' in_sum_expr ')'
5004
	  { $$=new Item_sum_std($3, 0); }
unknown's avatar
unknown committed
5005
	| VARIANCE_SYM '(' in_sum_expr ')'
5006 5007 5008 5009 5010
	  { $$=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
5011
	| SUM_SYM '(' in_sum_expr ')'
5012
	  { $$=new Item_sum_sum($3); }
5013 5014
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
unknown's avatar
unknown committed
5015 5016 5017 5018 5019
	| GROUP_CONCAT_SYM '(' opt_distinct
	  { Select->in_sum_expr++; }
	  expr_list opt_gorder_clause
	  opt_gconcat_separator
	 ')'
unknown's avatar
unknown committed
5020
	  {
5021 5022
            SELECT_LEX *sel= Select;
	    sel->in_sum_expr--;
unknown's avatar
unknown committed
5023
	    $$=new Item_func_group_concat(Lex->current_context(), $3, $5,
5024
                                          sel->gorder_list, $7);
unknown's avatar
unknown committed
5025
	    $5->empty();
5026 5027 5028 5029 5030 5031 5032
	  };

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

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

5036 5037

opt_gorder_clause:
5038 5039
	  /* empty */
	  {
unknown's avatar
unknown committed
5040
            Select->gorder_list = NULL;
5041 5042 5043
	  }
	| order_clause
          {
unknown's avatar
unknown committed
5044 5045 5046
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
5047
				     sizeof(st_sql_list));
unknown's avatar
unknown committed
5048
	    select->order_list.empty();
5049
	  };
unknown's avatar
unknown committed
5050

unknown's avatar
unknown committed
5051 5052

in_sum_expr:
unknown's avatar
unknown committed
5053
	opt_all
5054 5055 5056 5057
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
5058
	    yyerror(ER(ER_SYNTAX_ERROR));
5059
	    YYABORT;
5060 5061
	  }
	}
unknown's avatar
unknown committed
5062 5063
	expr
	{
unknown's avatar
unknown committed
5064
	  Select->in_sum_expr--;
5065
	  $$= $3;
unknown's avatar
unknown committed
5066
	};
unknown's avatar
unknown committed
5067

5068
cast_type:
unknown's avatar
unknown committed
5069 5070
        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; }
5071
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; Lex->dec=0; }
unknown's avatar
unknown committed
5072 5073 5074 5075 5076 5077 5078 5079
        | 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; }
5080 5081
	;

unknown's avatar
unknown committed
5082
expr_list:
5083
	{ Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
5084
	expr_list2
unknown's avatar
unknown committed
5085
	{ $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
5086 5087

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

unknown's avatar
unknown committed
5091 5092
ident_list_arg:
          ident_list          { $$= $1; }
unknown's avatar
unknown committed
5093
        | '(' ident_list ')'  { $$= $2; };
unknown's avatar
unknown committed
5094

unknown's avatar
unknown committed
5095
ident_list:
5096
        { Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
5097
        ident_list2
unknown's avatar
unknown committed
5098
        { $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
5099 5100

ident_list2:
5101
        simple_ident { Select->expr_list.head()->push_back($1); }
unknown's avatar
unknown committed
5102
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
unknown's avatar
unknown committed
5103 5104 5105

opt_expr:
	/* empty */      { $$= NULL; }
5106
	| expr           { $$= $1; };
unknown's avatar
unknown committed
5107 5108 5109

opt_else:
	/* empty */    { $$= NULL; }
unknown's avatar
unknown committed
5110
	| ELSE expr    { $$= $2; };
unknown's avatar
unknown committed
5111 5112

when_list:
unknown's avatar
unknown committed
5113
        { Select->when_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
5114
	when_list2
unknown's avatar
unknown committed
5115
	{ $$= Select->when_list.pop(); };
unknown's avatar
unknown committed
5116 5117

when_list2:
unknown's avatar
unknown committed
5118
	expr THEN_SYM expr
unknown's avatar
unknown committed
5119
	  {
unknown's avatar
unknown committed
5120
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
5121 5122
	    sel->when_list.head()->push_back($1);
	    sel->when_list.head()->push_back($3);
unknown's avatar
unknown committed
5123
	}
unknown's avatar
unknown committed
5124
	| when_list2 WHEN_SYM expr THEN_SYM expr
unknown's avatar
unknown committed
5125
	  {
unknown's avatar
unknown committed
5126
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
5127 5128
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
unknown's avatar
unknown committed
5129
	  };
unknown's avatar
unknown committed
5130

unknown's avatar
unknown committed
5131
/* Warning - may return NULL in case of incomplete SELECT */
5132 5133 5134
table_ref:
        table_factor            { $$=$1; }
        | join_table            { $$=$1; }
5135
          {
5136 5137 5138
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
5139
          }
5140 5141
        ;

unknown's avatar
unknown committed
5142
join_table_list:
unknown's avatar
unknown committed
5143
	derived_table_list		{ YYERROR_UNLESS($$=$1); }
unknown's avatar
unknown committed
5144
	;
unknown's avatar
unknown committed
5145

unknown's avatar
unknown committed
5146 5147
/* Warning - may return NULL in case of incomplete SELECT */
derived_table_list:
5148
        table_ref { $$=$1; }
unknown's avatar
unknown committed
5149 5150
        | derived_table_list ',' table_ref
          {
unknown's avatar
unknown committed
5151
            YYERROR_UNLESS($1 && ($$=$3));
unknown's avatar
unknown committed
5152
          }
5153 5154
        ;

5155 5156 5157 5158 5159 5160 5161
/*
  Notice that JOIN is a left-associative operation, and it must be parsed
  as such, that is, the parser must process first the left join operand
  then the right one. Such order of processing ensures that the parser
  produces correct join trees which is essential for semantic analysis
  and subsequent optimization phases.
*/
5162
join_table:
5163
/* INNER JOIN variants */
unknown's avatar
unknown committed
5164
        /*
5165 5166 5167
          Use %prec to evaluate production 'table_ref' before 'normal_join'
          so that [INNER | CROSS] JOIN is properly nested as other
          left-associative joins.
unknown's avatar
unknown committed
5168 5169
        */
        table_ref %prec TABLE_REF_PRIORITY normal_join table_ref
5170
          { YYERROR_UNLESS($1 && ($$=$3)); }
unknown's avatar
unknown committed
5171
	| table_ref STRAIGHT_JOIN table_factor
unknown's avatar
unknown committed
5172
	  { YYERROR_UNLESS($1 && ($$=$3)); $3->straight=1; }
unknown's avatar
unknown committed
5173 5174 5175 5176 5177
	| table_ref normal_join table_ref
          ON
          {
            YYERROR_UNLESS($1 && ($$=$3));
            /* Change the current name resolution context to a local context. */
5178
            if (push_new_name_resolution_context(YYTHD, $1, $3))
unknown's avatar
unknown committed
5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190
              YYABORT;
          }
          expr
	  {
            add_join_on($3,$6);
            Lex->pop_context();
          }
        | table_ref STRAIGHT_JOIN table_factor
          ON
          {
            YYERROR_UNLESS($1 && ($$=$3));
            /* Change the current name resolution context to a local context. */
5191
            if (push_new_name_resolution_context(YYTHD, $1, $3))
unknown's avatar
unknown committed
5192 5193 5194 5195 5196 5197 5198 5199
              YYABORT;
          }
          expr
          {
            $3->straight=1;
            add_join_on($3,$6);
            Lex->pop_context();
          }
5200
	| table_ref normal_join table_ref
unknown's avatar
unknown committed
5201
	  USING
unknown's avatar
unknown committed
5202
	  {
unknown's avatar
unknown committed
5203
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5204
            YYERROR_UNLESS($1 && $3);
unknown's avatar
unknown committed
5205
	  }
unknown's avatar
unknown committed
5206
	  '(' using_list ')'
unknown's avatar
unknown committed
5207
          { add_join_natural($1,$3,$7); $$=$3; }
5208 5209 5210 5211 5212 5213 5214
	| table_ref NATURAL JOIN_SYM table_factor
	  {
            YYERROR_UNLESS($1 && ($$=$4));
            add_join_natural($1,$4,NULL);
          }

/* LEFT JOIN variants */
unknown's avatar
unknown committed
5215 5216 5217
	| table_ref LEFT opt_outer JOIN_SYM table_ref
          ON
          {
5218
            YYERROR_UNLESS($1 && $5);
unknown's avatar
unknown committed
5219
            /* Change the current name resolution context to a local context. */
5220
            if (push_new_name_resolution_context(YYTHD, $1, $5))
unknown's avatar
unknown committed
5221 5222 5223 5224 5225 5226 5227 5228 5229
              YYABORT;
          }
          expr
	  {
            add_join_on($5,$8);
            Lex->pop_context();
            $5->outer_join|=JOIN_TYPE_LEFT;
            $$=$5;
          }
5230
	| table_ref LEFT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
5231
	  {
unknown's avatar
unknown committed
5232
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5233
            YYERROR_UNLESS($1 && $5);
unknown's avatar
unknown committed
5234
	  }
unknown's avatar
unknown committed
5235
	  USING '(' using_list ')'
unknown's avatar
unknown committed
5236
          { add_join_natural($1,$5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
5237
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
5238
	  {
unknown's avatar
unknown committed
5239
            YYERROR_UNLESS($1 && $6);
unknown's avatar
unknown committed
5240
 	    add_join_natural($1,$6,NULL);
5241
	    $6->outer_join|=JOIN_TYPE_LEFT;
5242 5243
	    $$=$6;
	  }
5244 5245

/* RIGHT JOIN variants */
unknown's avatar
unknown committed
5246 5247 5248
	| table_ref RIGHT opt_outer JOIN_SYM table_ref
          ON
          {
5249
            YYERROR_UNLESS($1 && $5);
unknown's avatar
unknown committed
5250
            /* Change the current name resolution context to a local context. */
5251
            if (push_new_name_resolution_context(YYTHD, $1, $5))
unknown's avatar
unknown committed
5252 5253 5254
              YYABORT;
          }
          expr
unknown's avatar
unknown committed
5255
          {
5256 5257 5258
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
unknown's avatar
unknown committed
5259 5260
            add_join_on($$, $8);
            Lex->pop_context();
5261 5262
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
5263
	  {
unknown's avatar
unknown committed
5264
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5265
            YYERROR_UNLESS($1 && $5);
unknown's avatar
unknown committed
5266
	  }
5267
	  USING '(' using_list ')'
unknown's avatar
unknown committed
5268
          {
5269 5270 5271
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
unknown's avatar
unknown committed
5272
            add_join_natural($$,$5,$9);
5273 5274
          }
	| table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor
5275
	  {
unknown's avatar
unknown committed
5276
            YYERROR_UNLESS($1 && $6);
unknown's avatar
unknown committed
5277
	    add_join_natural($6,$1,NULL);
5278 5279 5280
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
5281
	  };
unknown's avatar
unknown committed
5282 5283

normal_join:
unknown's avatar
unknown committed
5284 5285 5286 5287
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
unknown's avatar
unknown committed
5288

unknown's avatar
unknown committed
5289
/* Warning - may return NULL in case of incomplete SELECT */
5290
table_factor:
unknown's avatar
unknown committed
5291
	{
unknown's avatar
unknown committed
5292
	  SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5293
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
unknown's avatar
unknown committed
5294
	  sel->table_join_options= 0;
unknown's avatar
unknown committed
5295
	}
unknown's avatar
unknown committed
5296
        table_ident opt_table_alias opt_key_definition
unknown's avatar
unknown committed
5297
	{
unknown's avatar
unknown committed
5298
	  LEX *lex= Lex;
unknown's avatar
unknown committed
5299
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
5300
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
unknown's avatar
unknown committed
5301
					   sel->get_table_join_options(),
unknown's avatar
unknown committed
5302
					   lex->lock_option,
5303 5304
					   sel->get_use_index(),
					   sel->get_ignore_index())))
5305
	    YYABORT;
5306
          sel->add_joined_table($$);
unknown's avatar
unknown committed
5307
	}
unknown's avatar
unknown committed
5308 5309 5310 5311
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref
          ON
          {
            /* Change the current name resolution context to a local context. */
5312
            if (push_new_name_resolution_context(YYTHD, $3, $7))
unknown's avatar
unknown committed
5313
              YYABORT;
5314

unknown's avatar
unknown committed
5315 5316 5317
          }
          expr '}'
	  {
5318
	    LEX *lex= Lex;
unknown's avatar
unknown committed
5319 5320 5321 5322 5323
            YYERROR_UNLESS($3 && $7);
            add_join_on($7,$10);
            Lex->pop_context();
            $7->outer_join|=JOIN_TYPE_LEFT;
            $$=$7;
5324 5325
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
unknown's avatar
unknown committed
5326
          }
unknown's avatar
unknown committed
5327
	| select_derived_init get_select_lex select_derived2
5328
          {
5329
            LEX *lex= Lex;
unknown's avatar
unknown committed
5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343
            SELECT_LEX *sel= lex->current_select;
            if ($1)
            {
	      if (sel->set_braces(1))
	      {
	        yyerror(ER(ER_SYNTAX_ERROR));
	        YYABORT;
	      }
              /* select in braces, can't contain global parameters */
	      if (sel->master_unit()->fake_select_lex)
                sel->master_unit()->global_parameters=
                   sel->master_unit()->fake_select_lex;
            }
            if ($2->init_nested_join(lex->thd))
unknown's avatar
unknown committed
5344
              YYABORT;
unknown's avatar
unknown committed
5345
            $$= 0;
unknown's avatar
unknown committed
5346
            /* incomplete derived tables return NULL, we must be
unknown's avatar
unknown committed
5347
               nested in select_derived rule to be here. */
5348
          }
unknown's avatar
unknown committed
5349 5350 5351 5352 5353
	| '(' get_select_lex select_derived union_opt ')' opt_table_alias
	{
          /* Use $2 instead of Lex->current_select as derived table will
             alter value of Lex->current_select. */

unknown's avatar
unknown committed
5354
          if (!($3 || $6) && $2->embedding &&
unknown's avatar
unknown committed
5355
              !$2->embedding->nested_join->join_list.elements)
5356
          {
unknown's avatar
unknown committed
5357 5358 5359 5360 5361
            /* 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;
5362
          }
unknown's avatar
unknown committed
5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376
          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
5377

unknown's avatar
unknown committed
5378
	      YYABORT;
unknown's avatar
unknown committed
5379
            sel->add_joined_table($$);
unknown's avatar
unknown committed
5380
            lex->pop_context();
unknown's avatar
unknown committed
5381 5382 5383 5384 5385 5386
          }
	  else
          if ($4 || $6)
	  {
            /* simple nested joins cannot have aliases or unions */
            yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
5387
	    YYABORT;
unknown's avatar
unknown committed
5388 5389 5390 5391 5392
	  }
          else
            $$= $3;
	}
        ;
unknown's avatar
unknown committed
5393

unknown's avatar
unknown committed
5394
/* handle contents of parentheses in join expression */
5395
select_derived:
unknown's avatar
unknown committed
5396
	  get_select_lex
unknown's avatar
unknown committed
5397
	  {
unknown's avatar
unknown committed
5398 5399 5400 5401 5402 5403 5404 5405 5406
            LEX *lex= Lex;
            if ($1->init_nested_join(lex->thd))
              YYABORT;
          }
          derived_table_list
          {
            LEX *lex= Lex;
            /* for normal joins, $3 != NULL and end_nested_join() != NULL,
               for derived tables, both must equal NULL */
unknown's avatar
unknown committed
5407

unknown's avatar
unknown committed
5408 5409 5410 5411
            if (!($$= $1->end_nested_join(lex->thd)) && $3)
              YYABORT;
            if (!$3 && $$)
            {
unknown's avatar
unknown committed
5412 5413
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
unknown's avatar
unknown committed
5414 5415
            }
          }
unknown's avatar
unknown committed
5416
 	;
unknown's avatar
unknown committed
5417

unknown's avatar
unknown committed
5418
select_derived2:
unknown's avatar
unknown committed
5419
        {
unknown's avatar
unknown committed
5420
	  LEX *lex= Lex;
5421
	  lex->derived_tables|= DERIVED_SUBQUERY;
5422 5423
          if (lex->sql_command == (int)SQLCOM_HA_READ ||
              lex->sql_command == (int)SQLCOM_KILL)
5424 5425
	  {
	    yyerror(ER(ER_SYNTAX_ERROR));
5426 5427
	    YYABORT;
	  }
5428
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
unknown's avatar
unknown committed
5429
              mysql_new_select(lex, 1))
unknown's avatar
unknown committed
5430 5431
	    YYABORT;
	  mysql_init_select(lex);
5432
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
5433
	  lex->current_select->parsing_place= SELECT_LIST;
5434 5435 5436
	}
        select_options select_item_list
	{
5437
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
5438
	}
unknown's avatar
unknown committed
5439
	opt_select_from
5440
        ;
unknown's avatar
unknown committed
5441

unknown's avatar
unknown committed
5442 5443 5444 5445 5446 5447 5448 5449
get_select_lex:
	/* Empty */ { $$= Select; }
        ;

select_derived_init:
          SELECT_SYM
          {
            LEX *lex= Lex;
unknown's avatar
unknown committed
5450
            SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464
            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
5465 5466
opt_outer:
	/* empty */	{}
unknown's avatar
unknown committed
5467
	| OUTER		{};
unknown's avatar
unknown committed
5468 5469 5470 5471

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
unknown's avatar
unknown committed
5472
          {
unknown's avatar
unknown committed
5473
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5474 5475 5476
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
5477 5478
	| FORCE_SYM key_usage_list
          {
unknown's avatar
unknown committed
5479
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5480 5481 5482 5483
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
unknown's avatar
unknown committed
5484
	| IGNORE_SYM key_usage_list
unknown's avatar
unknown committed
5485
	  {
unknown's avatar
unknown committed
5486
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5487 5488
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
unknown's avatar
unknown committed
5489
	  };
unknown's avatar
unknown committed
5490 5491

key_usage_list:
unknown's avatar
unknown committed
5492
	key_or_index { Select->interval_list.empty(); }
unknown's avatar
unknown committed
5493
        '(' key_list_or_empty ')'
unknown's avatar
unknown committed
5494
        { $$= &Select->interval_list; }
unknown's avatar
unknown committed
5495 5496 5497 5498 5499 5500
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
unknown's avatar
unknown committed
5501 5502 5503

key_usage_list2:
	key_usage_list2 ',' ident
unknown's avatar
unknown committed
5504
        { Select->
unknown's avatar
unknown committed
5505
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $3.str, $3.length,
5506
				    system_charset_info)); }
unknown's avatar
unknown committed
5507
	| ident
unknown's avatar
unknown committed
5508
        { Select->
unknown's avatar
unknown committed
5509
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $1.str, $1.length,
5510
				    system_charset_info)); }
unknown's avatar
unknown committed
5511
	| PRIMARY_SYM
unknown's avatar
unknown committed
5512
        { Select->
unknown's avatar
unknown committed
5513
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
5514
				    system_charset_info)); };
unknown's avatar
unknown committed
5515 5516 5517

using_list:
	ident
unknown's avatar
unknown committed
5518
	  {
unknown's avatar
unknown committed
5519
            if (!($$= new List<String>))
unknown's avatar
unknown committed
5520
	      YYABORT;
unknown's avatar
unknown committed
5521 5522 5523
            $$->push_back(new (YYTHD->mem_root)
                              String((const char *) $1.str, $1.length,
                                      system_charset_info));
unknown's avatar
unknown committed
5524 5525 5526
	  }
	| using_list ',' ident
	  {
unknown's avatar
unknown committed
5527 5528 5529 5530
            $1->push_back(new (YYTHD->mem_root)
                              String((const char *) $3.str, $3.length,
                                      system_charset_info));
            $$= $1;
unknown's avatar
unknown committed
5531
	  };
unknown's avatar
unknown committed
5532 5533

interval:
5534 5535
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
unknown's avatar
unknown committed
5536
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
unknown's avatar
unknown committed
5537 5538
	| DAY_MINUTE_SYM	{ $$=INTERVAL_DAY_MINUTE; }
	| DAY_SECOND_SYM	{ $$=INTERVAL_DAY_SECOND; }
unknown's avatar
unknown committed
5539
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
unknown's avatar
unknown committed
5540 5541
	| HOUR_MINUTE_SYM	{ $$=INTERVAL_HOUR_MINUTE; }
	| HOUR_SECOND_SYM	{ $$=INTERVAL_HOUR_SECOND; }
unknown's avatar
unknown committed
5542 5543
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
unknown's avatar
unknown committed
5544
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
5545 5546 5547 5548 5549 5550 5551 5552
	| 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
5553 5554
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
5555
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
unknown's avatar
unknown committed
5556
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
5557 5558
	| YEAR_SYM		{ $$=INTERVAL_YEAR; }
        ;
unknown's avatar
unknown committed
5559

5560
date_time_type:
5561 5562 5563 5564 5565
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
5566

unknown's avatar
unknown committed
5567 5568 5569
table_alias:
	/* empty */
	| AS
unknown's avatar
unknown committed
5570
	| EQ;
unknown's avatar
unknown committed
5571 5572 5573 5574

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

unknown's avatar
unknown committed
5577 5578 5579 5580
opt_all:
	/* empty */
	| ALL
	;
unknown's avatar
unknown committed
5581 5582

where_clause:
unknown's avatar
unknown committed
5583
	/* empty */  { Select->where= 0; }
unknown's avatar
unknown committed
5584 5585 5586 5587 5588
	| WHERE
          {
            Select->parsing_place= IN_WHERE;
          }
          expr
5589
	  {
unknown's avatar
unknown committed
5590 5591 5592 5593 5594
            SELECT_LEX *select= Select;
	    select->where= $3;
            select->parsing_place= NO_MATTER;
	    if ($3)
	      $3->top_level_item();
5595
	  }
unknown's avatar
unknown committed
5596
 	;
unknown's avatar
unknown committed
5597 5598 5599

having_clause:
	/* empty */
unknown's avatar
unknown committed
5600 5601
	| HAVING
	  {
5602
	    Select->parsing_place= IN_HAVING;
unknown's avatar
unknown committed
5603 5604
          }
	  expr
unknown's avatar
unknown committed
5605
	  {
unknown's avatar
unknown committed
5606
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5607
	    sel->having= $3;
5608
	    sel->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
5609 5610 5611
	    if ($3)
	      $3->top_level_item();
	  }
5612
	;
unknown's avatar
unknown committed
5613 5614

opt_escape:
5615 5616 5617 5618 5619
	ESCAPE_SYM simple_expr 
          {
            Lex->escape_used= TRUE;
            $$= $2;
          }
5620
	| /* empty */
5621
          {
5622
            Lex->escape_used= FALSE;
5623 5624 5625
            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
5626 5627
          }
        ;
unknown's avatar
unknown committed
5628 5629 5630


/*
unknown's avatar
unknown committed
5631
   group by statement in select
unknown's avatar
unknown committed
5632 5633 5634 5635
*/

group_clause:
	/* empty */
5636
	| GROUP BY group_list olap_opt;
unknown's avatar
unknown committed
5637 5638

group_list:
unknown's avatar
unknown committed
5639
	group_list ',' order_ident order_dir
unknown's avatar
unknown committed
5640
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
5641
	| order_ident order_dir
unknown's avatar
unknown committed
5642
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
5643

5644 5645
olap_opt:
	/* empty */ {}
5646
	| WITH CUBE_SYM
5647
          {
5648
	    LEX *lex=Lex;
5649 5650
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
5651
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
5652 5653 5654
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
5655
	    lex->current_select->olap= CUBE_TYPE;
unknown's avatar
unknown committed
5656
	    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
5657
	    YYABORT;	/* To be deleted in 5.1 */
5658
	  }
5659
	| WITH ROLLUP_SYM
5660
          {
5661 5662 5663
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
5664
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
5665 5666 5667
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
5668
	    lex->current_select->olap= ROLLUP_TYPE;
5669
	  }
5670
	;
5671

unknown's avatar
unknown committed
5672
/*
unknown's avatar
unknown committed
5673
   Order by statement in select
unknown's avatar
unknown committed
5674 5675
*/

5676
opt_order_clause:
unknown's avatar
unknown committed
5677
	/* empty */
unknown's avatar
unknown committed
5678
	| order_clause;
5679 5680

order_clause:
5681 5682
	ORDER_SYM BY
        {
unknown's avatar
unknown committed
5683
	  LEX *lex=Lex;
unknown's avatar
unknown committed
5684 5685 5686 5687
          SELECT_LEX *sel= lex->current_select;
          SELECT_LEX_UNIT *unit= sel-> master_unit();
	  if (sel->linkage != GLOBAL_OPTIONS_TYPE &&
	      sel->olap != UNSPECIFIED_OLAP_TYPE)
5688
	  {
unknown's avatar
unknown committed
5689 5690
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
5691 5692
	    YYABORT;
	  }
unknown's avatar
unknown committed
5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705
          if (lex->sql_command != SQLCOM_ALTER_TABLE && !unit->fake_select_lex)
          {
            /*
              A query of the of the form (SELECT ...) ORDER BY order_list is
              executed in the same way as the query
              SELECT ... ORDER BY order_list
              unless the SELECT construct contains ORDER BY or LIMIT clauses.
              Otherwise we create a fake SELECT_LEX if it has not been created
              yet.
            */
            SELECT_LEX *first_sl= unit->first_select();
            if (!first_sl->next_select() &&
                (first_sl->order_list.elements || 
unknown's avatar
unknown committed
5706
                 first_sl->select_limit) &&            
unknown's avatar
unknown committed
5707 5708 5709
                unit->add_fake_select_lex(lex->thd))
              YYABORT;
          }
unknown's avatar
unknown committed
5710
	} order_list;
unknown's avatar
unknown committed
5711 5712 5713

order_list:
	order_list ',' order_ident order_dir
unknown's avatar
unknown committed
5714
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
5715
	| order_ident order_dir
unknown's avatar
unknown committed
5716
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
5717 5718 5719

order_dir:
	/* empty */ { $$ =  1; }
unknown's avatar
unknown committed
5720
	| ASC  { $$ =1; }
unknown's avatar
unknown committed
5721
	| DESC { $$ =0; };
unknown's avatar
unknown committed
5722 5723


5724 5725 5726
opt_limit_clause_init:
	/* empty */
	{
5727 5728
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
5729 5730
          sel->offset_limit= 0;
          sel->select_limit= 0;
5731
	}
5732 5733 5734
	| limit_clause {}
	;

5735 5736 5737 5738 5739
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

5740
limit_clause:
5741
	LIMIT limit_options {}
unknown's avatar
unknown committed
5742
	;
unknown's avatar
unknown committed
5743 5744

limit_options:
5745
	limit_option
unknown's avatar
unknown committed
5746
	  {
unknown's avatar
unknown committed
5747
            SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5748
            sel->select_limit= $1;
5749
            sel->offset_limit= 0;
5750
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5751
	  }
5752
	| limit_option ',' limit_option
unknown's avatar
unknown committed
5753
	  {
unknown's avatar
unknown committed
5754
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5755 5756
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
5757
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5758
	  }
5759
	| limit_option OFFSET_SYM limit_option
unknown's avatar
unknown committed
5760
	  {
unknown's avatar
unknown committed
5761
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5762 5763
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
5764
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5765 5766
	  }
	;
5767 5768 5769 5770 5771
limit_option:
        param_marker
        | ULONGLONG_NUM { $$= new Item_uint($1.str, $1.length); }
        | LONG_NUM     { $$= new Item_uint($1.str, $1.length); }
        | NUM           { $$= new Item_uint($1.str, $1.length); }
unknown's avatar
unknown committed
5772
        ;
unknown's avatar
unknown committed
5773 5774 5775 5776

delete_limit_clause:
	/* empty */
	{
unknown's avatar
unknown committed
5777
	  LEX *lex=Lex;
5778
	  lex->current_select->select_limit= 0;
unknown's avatar
unknown committed
5779
	}
5780
	| LIMIT limit_option
5781 5782
	{
	  SELECT_LEX *sel= Select;
5783
	  sel->select_limit= $2;
5784 5785
	  sel->explicit_limit= 1;
	};
unknown's avatar
unknown committed
5786

unknown's avatar
unknown committed
5787 5788
ulong_num:
          NUM           { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
5789
	| HEX_NUM       { $$= (ulong) strtol($1.str, (char**) 0, 16); }
unknown's avatar
unknown committed
5790 5791
	| 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
5792
        | DECIMAL_NUM   { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
5793 5794
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
5795

unknown's avatar
unknown committed
5796
ulonglong_num:
unknown's avatar
unknown committed
5797 5798 5799
	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
5800
        | DECIMAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
5801 5802
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
5803 5804 5805 5806 5807 5808

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
5809 5810
	    if (&lex->select_lex != lex->current_select)
	    {
unknown's avatar
unknown committed
5811
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
5812 5813
	      YYABORT;
	    }
unknown's avatar
unknown committed
5814 5815 5816
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
5817 5818 5819 5820
	    if (add_proc_to_list(lex->thd, new Item_field(&lex->
                                                          current_select->
                                                          context,
                                                          NULL,NULL,$2.str)))
unknown's avatar
unknown committed
5821
	      YYABORT;
5822
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
5823
	  }
unknown's avatar
unknown committed
5824
	  '(' procedure_list ')';
unknown's avatar
unknown committed
5825 5826 5827 5828


procedure_list:
	/* empty */ {}
unknown's avatar
unknown committed
5829
	| procedure_list2 {};
unknown's avatar
unknown committed
5830 5831 5832

procedure_list2:
	procedure_list2 ',' procedure_item
unknown's avatar
unknown committed
5833
	| procedure_item;
unknown's avatar
unknown committed
5834 5835 5836 5837

procedure_item:
	  remember_name expr
	  {
unknown's avatar
unknown committed
5838 5839
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
unknown's avatar
unknown committed
5840 5841
	      YYABORT;
	    if (!$2->name)
5842 5843
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1),
                           YYTHD->charset());
5844 5845 5846 5847 5848 5849
	  }
          ;


select_var_list_init:
	   {
unknown's avatar
unknown committed
5850 5851
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
5852 5853
	        YYABORT;
	   }
5854
	   select_var_list
5855
	   {}
5856
           ;
unknown's avatar
unknown committed
5857

unknown's avatar
unknown committed
5858
select_var_list:
5859 5860 5861 5862
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

5863 5864 5865 5866 5867 5868 5869
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
5870 5871 5872 5873 5874
               /*
                 The parser won't create select_result instance only
	         if it's an EXPLAIN.
               */
               DBUG_ASSERT(lex->describe);
5875 5876
	   }
           | ident_or_text
unknown's avatar
unknown committed
5877
           {
5878
             LEX *lex=Lex;
5879
	     sp_variable_t *t;
5880

5881
	     if (!lex->spcont || !(t=lex->spcont->find_variable(&$1)))
5882
	     {
5883
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
unknown's avatar
unknown committed
5884
	       YYABORT;
5885
	     }
5886 5887
	     if (lex->result)
             {
5888 5889 5890
               my_var *var;
	       ((select_dumpvar *)lex->result)->
                 var_list.push_back(var= new my_var($1,1,t->offset,t->type));
unknown's avatar
unknown committed
5891
#ifndef DBUG_OFF
5892
	       if (var)
5893
		 var->sp= lex->sphead;
5894
#endif
5895 5896 5897 5898 5899 5900 5901 5902
             }
	     else
	     {
               /*
                 The parser won't create select_result instance only
	         if it's an EXPLAIN.
               */
               DBUG_ASSERT(lex->describe);
5903
	     }
unknown's avatar
unknown committed
5904
	   }
unknown's avatar
unknown committed
5905
           ;
unknown's avatar
unknown committed
5906

5907
into:
unknown's avatar
unknown committed
5908
        INTO OUTFILE TEXT_STRING_filesystem
unknown's avatar
unknown committed
5909
	{
unknown's avatar
unknown committed
5910
          LEX *lex= Lex;
5911 5912 5913 5914
          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
5915 5916
	}
	opt_field_term opt_line_term
unknown's avatar
unknown committed
5917
	| INTO DUMPFILE TEXT_STRING_filesystem
unknown's avatar
unknown committed
5918
	{
5919
	  LEX *lex=Lex;
unknown's avatar
unknown committed
5920 5921
	  if (!lex->describe)
	  {
5922
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
5923 5924 5925 5926 5927
	    if (!(lex->exchange= new sql_exchange($3.str,1)))
	      YYABORT;
	    if (!(lex->result= new select_dump(lex->exchange)))
	      YYABORT;
	  }
unknown's avatar
unknown committed
5928
	}
5929
        | INTO select_var_list_init
unknown's avatar
unknown committed
5930
	{
5931
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5932 5933
	}
        ;
unknown's avatar
unknown committed
5934

unknown's avatar
unknown committed
5935 5936 5937
/*
  DO statement
*/
unknown's avatar
unknown committed
5938

5939
do:	DO_SYM
unknown's avatar
unknown committed
5940 5941 5942
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
5943 5944 5945 5946 5947
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
unknown's avatar
unknown committed
5948
	}
5949 5950
	;

unknown's avatar
unknown committed
5951
/*
5952
  Drop : delete tables or index or user
unknown's avatar
unknown committed
5953 5954 5955
*/

drop:
unknown's avatar
unknown committed
5956
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
unknown's avatar
unknown committed
5957
	{
unknown's avatar
unknown committed
5958 5959
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
5960 5961
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
unknown's avatar
unknown committed
5962
	}
unknown's avatar
unknown committed
5963
	| DROP INDEX_SYM ident ON table_ident {}
unknown's avatar
unknown committed
5964
	  {
unknown's avatar
unknown committed
5965 5966
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
5967 5968 5969
	     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
5970 5971
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
unknown's avatar
unknown committed
5972 5973 5974 5975
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
unknown's avatar
unknown committed
5976 5977 5978 5979
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
	    lex->name=$4.str;
unknown's avatar
unknown committed
5980
	 }
5981
	| DROP FUNCTION_SYM if_exists sp_name
unknown's avatar
unknown committed
5982
	  {
unknown's avatar
unknown committed
5983
	    LEX *lex=Lex;
5984 5985
	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
5986
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
5987 5988
	      YYABORT;
	    }
unknown's avatar
unknown committed
5989
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
5990 5991 5992 5993 5994 5995 5996 5997
	    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
5998
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
5999 6000 6001 6002 6003
	      YYABORT;
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
6004
	  }
6005
	| DROP USER clear_privileges user_list
6006
	  {
6007 6008
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
6009 6010
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
6011
	    LEX *lex= Lex;
6012 6013 6014
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
6015
        | DROP TRIGGER_SYM sp_name
6016 6017 6018
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_DROP_TRIGGER;
6019
            lex->spname= $3;
6020 6021
          }
	;
unknown's avatar
unknown committed
6022 6023

table_list:
6024
	table_name
unknown's avatar
unknown committed
6025
	| table_list ',' table_name;
unknown's avatar
unknown committed
6026

6027
table_name:
unknown's avatar
unknown committed
6028
	table_ident
unknown's avatar
unknown committed
6029 6030 6031 6032 6033
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
unknown's avatar
unknown committed
6034 6035

if_exists:
6036
	/* empty */ { $$= 0; }
6037 6038
	| IF EXISTS { $$= 1; }
	;
unknown's avatar
unknown committed
6039

6040 6041 6042 6043
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
unknown's avatar
unknown committed
6044 6045 6046 6047 6048
/*
** Insert : add new data to table
*/

insert:
6049 6050 6051
	INSERT
	{
	  LEX *lex= Lex;
6052 6053
	  lex->sql_command= SQLCOM_INSERT;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
6054
	  mysql_init_select(lex);
6055 6056 6057
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
	} insert_lock_option
6058
	opt_ignore insert2
unknown's avatar
unknown committed
6059
	{
unknown's avatar
unknown committed
6060
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
6061
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
6062
	}
6063
	insert_field_spec opt_insert_update
6064
	{}
unknown's avatar
unknown committed
6065
	;
unknown's avatar
unknown committed
6066 6067

replace:
6068 6069
	REPLACE
	{
6070
	  LEX *lex=Lex;
6071 6072
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
unknown's avatar
unknown committed
6073
	  mysql_init_select(lex);
6074
	}
unknown's avatar
unknown committed
6075 6076
	replace_lock_option insert2
	{
unknown's avatar
unknown committed
6077
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
6078
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
6079 6080
	}
	insert_field_spec
6081
	{}
unknown's avatar
unknown committed
6082
	;
unknown's avatar
unknown committed
6083 6084

insert_lock_option:
6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097
	/* empty */
          {
#ifdef HAVE_QUERY_CACHE
            /*
              If it is SP we do not allow insert optimisation whan result of
              insert visible only after the table unlocking but everyone can
              read table.
            */
            $$= (Lex->sphead ? TL_WRITE :TL_WRITE_CONCURRENT_INSERT);
#else
            $$= TL_WRITE_CONCURRENT_INSERT;
#endif
          }
unknown's avatar
unknown committed
6098 6099 6100
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
6101
	;
unknown's avatar
unknown committed
6102 6103

replace_lock_option:
unknown's avatar
unknown committed
6104 6105
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
unknown's avatar
unknown committed
6106 6107 6108

insert2:
	INTO insert_table {}
unknown's avatar
unknown committed
6109
	| insert_table {};
unknown's avatar
unknown committed
6110 6111

insert_table:
6112
	table_name
unknown's avatar
unknown committed
6113
	{
unknown's avatar
unknown committed
6114 6115 6116 6117
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
unknown's avatar
unknown committed
6118
	};
unknown's avatar
unknown committed
6119 6120

insert_field_spec:
unknown's avatar
unknown committed
6121 6122 6123
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
unknown's avatar
unknown committed
6124 6125
	| SET
	  {
unknown's avatar
unknown committed
6126 6127 6128
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
6129 6130
	      YYABORT;
	   }
unknown's avatar
unknown committed
6131
	   ident_eq_list;
unknown's avatar
unknown committed
6132 6133 6134

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
unknown's avatar
unknown committed
6135
	| insert_ident		{ Lex->field_list.push_back($1); };
unknown's avatar
unknown committed
6136 6137 6138

insert_values:
	VALUES	values_list  {}
unknown's avatar
unknown committed
6139
	| VALUE_SYM values_list  {}
unknown's avatar
unknown committed
6140 6141
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
6142
        ;
unknown's avatar
unknown committed
6143 6144 6145

values_list:
	values_list ','  no_braces
unknown's avatar
unknown committed
6146
	| no_braces;
unknown's avatar
unknown committed
6147 6148 6149 6150

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
unknown's avatar
unknown committed
6151
	ident_eq_value;
unknown's avatar
unknown committed
6152 6153

ident_eq_value:
6154
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
6155
	 {
unknown's avatar
unknown committed
6156 6157 6158
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
unknown's avatar
unknown committed
6159
	    YYABORT;
unknown's avatar
unknown committed
6160
	 };
unknown's avatar
unknown committed
6161 6162

equal:	EQ		{}
unknown's avatar
unknown committed
6163 6164 6165 6166 6167 6168 6169
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
unknown's avatar
unknown committed
6170 6171 6172 6173 6174 6175 6176 6177 6178

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
unknown's avatar
unknown committed
6179 6180
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
6181
	    YYABORT;
unknown's avatar
unknown committed
6182
	 };
unknown's avatar
unknown committed
6183 6184 6185

opt_values:
	/* empty */ {}
unknown's avatar
unknown committed
6186
	| values;
unknown's avatar
unknown committed
6187 6188

values:
unknown's avatar
unknown committed
6189
	values ','  expr_or_default
unknown's avatar
unknown committed
6190 6191 6192 6193
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
unknown's avatar
unknown committed
6194 6195 6196 6197 6198 6199 6200 6201
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
6202
	expr	  { $$= $1;}
unknown's avatar
unknown committed
6203
	| DEFAULT {$$= new Item_default_value(Lex->current_context()); }
unknown's avatar
unknown committed
6204
	;
unknown's avatar
unknown committed
6205

6206 6207
opt_insert_update:
        /* empty */
unknown's avatar
unknown committed
6208
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
unknown's avatar
unknown committed
6209
          KEY_SYM UPDATE_SYM insert_update_list
6210 6211
        ;

unknown's avatar
unknown committed
6212 6213 6214
/* Update rows in a table */

update:
6215 6216
	UPDATE_SYM
	{
6217
	  LEX *lex= Lex;
unknown's avatar
unknown committed
6218
	  mysql_init_select(lex);
6219
          lex->sql_command= SQLCOM_UPDATE;
unknown's avatar
unknown committed
6220
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
6221
	  lex->duplicates= DUP_ERROR; 
6222
        }
unknown's avatar
unknown committed
6223
        opt_low_priority opt_ignore join_table_list
unknown's avatar
unknown committed
6224
	SET update_list
unknown's avatar
unknown committed
6225
	{
6226 6227
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
unknown's avatar
unknown committed
6228
            lex->sql_command= SQLCOM_UPDATE_MULTI;
6229 6230 6231
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
6232 6233
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
6234 6235
	    YYABORT;
	  }
6236 6237 6238 6239 6240 6241
          /*
            In case of multi-update setting write lock for all tables may
            be too pessimistic. We will decrease lock level if possible in
            mysql_multi_update().
          */
          Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
6242
	}
6243
	where_clause opt_order_clause delete_limit_clause {}
unknown's avatar
unknown committed
6244
	;
unknown's avatar
unknown committed
6245 6246

update_list:
unknown's avatar
unknown committed
6247 6248 6249 6250
	update_list ',' update_elem
	| update_elem;

update_elem:
unknown's avatar
unknown committed
6251
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
6252
	{
unknown's avatar
unknown committed
6253
	  if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
unknown's avatar
unknown committed
6254
	    YYABORT;
unknown's avatar
unknown committed
6255 6256 6257 6258 6259 6260 6261
	};

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
unknown's avatar
unknown committed
6262
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
6263
	  {
unknown's avatar
unknown committed
6264 6265 6266
	  LEX *lex= Lex;
	  if (lex->update_list.push_back($1) || 
	      lex->value_list.push_back($3))
unknown's avatar
unknown committed
6267
	      YYABORT;
unknown's avatar
unknown committed
6268
	  };
unknown's avatar
unknown committed
6269 6270

opt_low_priority:
6271
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
6272
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
6273 6274 6275 6276

/* Delete rows from a table */

delete:
6277
	DELETE_SYM
6278
	{
6279 6280
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
unknown's avatar
unknown committed
6281
	  mysql_init_select(lex);
unknown's avatar
unknown committed
6282
	  lex->lock_option= lex->thd->update_lock_default;
6283
	  lex->ignore= 0;
6284
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
6285
	}
unknown's avatar
unknown committed
6286 6287
	opt_delete_options single_multi {}
	;
6288 6289

single_multi:
unknown's avatar
unknown committed
6290 6291
 	FROM table_ident
	{
unknown's avatar
unknown committed
6292 6293
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
unknown's avatar
unknown committed
6294 6295 6296
	    YYABORT;
	}
	where_clause opt_order_clause
6297
	delete_limit_clause {}
unknown's avatar
unknown committed
6298
	| table_wild_list
unknown's avatar
unknown committed
6299
	  { mysql_init_multi_delete(Lex); }
unknown's avatar
unknown committed
6300
          FROM join_table_list where_clause
6301 6302 6303 6304
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
unknown's avatar
unknown committed
6305 6306
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
6307
	  USING join_table_list where_clause
6308 6309 6310 6311
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
6312
	;
unknown's avatar
unknown committed
6313 6314 6315

table_wild_list:
	  table_wild_one {}
unknown's avatar
unknown committed
6316
	  | table_wild_list ',' table_wild_one {};
unknown's avatar
unknown committed
6317 6318

table_wild_one:
unknown's avatar
unknown committed
6319
	ident opt_wild opt_table_alias
unknown's avatar
unknown committed
6320
	{
unknown's avatar
unknown committed
6321 6322
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
					 TL_OPTION_UPDATING, Lex->lock_option))
unknown's avatar
unknown committed
6323 6324
	    YYABORT;
        }
unknown's avatar
unknown committed
6325
	| ident '.' ident opt_wild opt_table_alias
unknown's avatar
unknown committed
6326
	  {
unknown's avatar
unknown committed
6327 6328
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
unknown's avatar
unknown committed
6329 6330
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
unknown's avatar
unknown committed
6331
	      YYABORT;
unknown's avatar
unknown committed
6332
	  }
unknown's avatar
unknown committed
6333
	;
unknown's avatar
unknown committed
6334 6335

opt_wild:
6336
	/* empty */	{}
unknown's avatar
unknown committed
6337
	| '.' '*'	{};
unknown's avatar
unknown committed
6338 6339


6340
opt_delete_options:
unknown's avatar
unknown committed
6341
	/* empty */	{}
unknown's avatar
unknown committed
6342
	| opt_delete_option opt_delete_options {};
6343 6344

opt_delete_option:
6345
	QUICK		{ Select->options|= OPTION_QUICK; }
6346
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
6347
	| IGNORE_SYM	{ Lex->ignore= 1; };
6348

6349
truncate:
unknown's avatar
unknown committed
6350
	TRUNCATE_SYM opt_table_sym table_name
6351
	{
6352
	  LEX* lex= Lex;
6353
	  lex->sql_command= SQLCOM_TRUNCATE;
6354
	  lex->select_lex.options= 0;
6355
          lex->select_lex.sql_cache= SELECT_LEX::SQL_CACHE_UNSPECIFIED;
6356
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
6357 6358
	}
	;
6359

unknown's avatar
unknown committed
6360 6361
opt_table_sym:
	/* empty */
unknown's avatar
unknown committed
6362
	| TABLE_SYM;
6363

unknown's avatar
unknown committed
6364 6365
/* Show things */

6366 6367
show:	SHOW
	{
unknown's avatar
unknown committed
6368 6369
	  LEX *lex=Lex;
	  lex->wild=0;
6370 6371 6372
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
6373 6374
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
unknown's avatar
unknown committed
6375
	show_param
6376 6377
	{}
	;
unknown's avatar
unknown committed
6378 6379

show_param:
6380
         DATABASES wild_and_where
6381 6382 6383 6384 6385 6386 6387
         {
           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;
         }
6388
         | opt_full TABLES opt_db wild_and_where
6389 6390 6391 6392
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLES;
6393
             lex->select_lex.db= $3;
6394 6395 6396
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLE_NAMES))
               YYABORT;
           }
6397 6398 6399 6400 6401 6402 6403 6404 6405
         | opt_full TRIGGERS_SYM opt_db wild_and_where
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TRIGGERS;
             lex->select_lex.db= $3;
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TRIGGERS))
               YYABORT;
           }
6406
         | TABLE_SYM STATUS_SYM opt_db wild_and_where
6407 6408 6409 6410
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLE_STATUS;
6411
             lex->select_lex.db= $3;
6412 6413 6414
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLES))
               YYABORT;
           }
6415
        | OPEN_SYM TABLES opt_db wild_and_where
unknown's avatar
unknown committed
6416
	  {
6417
	    LEX *lex= Lex;
6418 6419
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_OPEN_TABLES;
6420
	    lex->select_lex.db= $3;
6421 6422
            if (prepare_schema_table(YYTHD, lex, 0, SCH_OPEN_TABLES))
              YYABORT;
6423
	  }
unknown's avatar
unknown committed
6424
	| ENGINE_SYM storage_engines 
unknown's avatar
unknown committed
6425 6426
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
6427
	| opt_full COLUMNS from_or_in table_ident opt_db wild_and_where
unknown's avatar
unknown committed
6428
	  {
6429 6430 6431
 	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SELECT;
	    lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
6432 6433 6434
	    if ($5)
	      $4->change_db($5);
	    if (prepare_schema_table(YYTHD, lex, $4, SCH_COLUMNS))
unknown's avatar
unknown committed
6435 6436
	      YYABORT;
	  }
6437
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
unknown's avatar
unknown committed
6438 6439
	  TEXT_STRING_sys AND_SYM MASTER_LOG_POS_SYM EQ ulonglong_num
	  AND_SYM MASTER_SERVER_ID_SYM EQ
unknown's avatar
unknown committed
6440
	ulong_num
unknown's avatar
unknown committed
6441 6442 6443 6444
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
6445
	    Lex->mi.server_id = $16;
unknown's avatar
unknown committed
6446
          }
6447
        | master_or_binary LOGS_SYM
unknown's avatar
unknown committed
6448 6449
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
6450 6451 6452 6453 6454
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
unknown's avatar
unknown committed
6455
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
unknown's avatar
unknown committed
6456
          {
6457 6458
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
6459
          } opt_limit_clause_init
6460
        | keys_or_index from_or_in table_ident opt_db where_clause
6461 6462 6463 6464
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_KEYS;
6465 6466 6467
	    if ($4)
	      $3->change_db($4);
            if (prepare_schema_table(YYTHD, lex, $3, SCH_STATISTICS))
6468
              YYABORT;
unknown's avatar
unknown committed
6469
	  }
unknown's avatar
unknown committed
6470 6471 6472 6473 6474 6475 6476 6477
	| 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
6478 6479 6480 6481 6482 6483 6484
	    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
6485 6486 6487 6488 6489 6490
	  }
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
6491
        | COUNT_SYM '(' '*' ')' WARNINGS
6492
          { (void) create_select_for_variable("warning_count"); }
6493
        | COUNT_SYM '(' '*' ')' ERRORS
6494
	  { (void) create_select_for_variable("error_count"); }
6495
        | WARNINGS opt_limit_clause_init
unknown's avatar
unknown committed
6496
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
6497
        | ERRORS opt_limit_clause_init
6498
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
6499
        | opt_var_type STATUS_SYM wild_and_where
6500
          {
6501 6502 6503
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS;
6504
            lex->option_type= $1;
6505 6506
            if (prepare_schema_table(YYTHD, lex, 0, SCH_STATUS))
              YYABORT;
6507
	  }	
unknown's avatar
unknown committed
6508
        | INNOBASE_SYM STATUS_SYM
unknown's avatar
unknown committed
6509
          { Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS; WARN_DEPRECATED("SHOW INNODB STATUS", "SHOW ENGINE INNODB STATUS"); }
unknown's avatar
unknown committed
6510 6511
        | MUTEX_SYM STATUS_SYM
          { Lex->sql_command = SQLCOM_SHOW_MUTEX_STATUS; }
unknown's avatar
unknown committed
6512 6513
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
6514
        | opt_var_type  VARIABLES wild_and_where
unknown's avatar
unknown committed
6515
	  {
6516 6517 6518
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_VARIABLES;
6519
            lex->option_type= $1;
6520 6521
            if (prepare_schema_table(YYTHD, lex, 0, SCH_VARIABLES))
              YYABORT;
6522
	  }
6523
        | charset wild_and_where
6524 6525 6526 6527 6528 6529 6530
          {
            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;
          }
6531
        | COLLATION_SYM wild_and_where
6532 6533 6534 6535 6536 6537 6538
          {
            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;
          }
6539
	| BERKELEY_DB_SYM LOGS_SYM
unknown's avatar
unknown committed
6540
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW BDB LOGS", "SHOW ENGINE BDB LOGS"); }
unknown's avatar
unknown committed
6541
	| LOGS_SYM
unknown's avatar
unknown committed
6542
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW LOGS", "SHOW ENGINE BDB LOGS"); }
6543 6544 6545 6546 6547
	| GRANTS
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    LEX_USER *curr_user;
6548
            if (!(curr_user= (LEX_USER*) lex->thd->alloc(sizeof(st_lex_user))))
6549
              YYABORT;
6550
            bzero(curr_user, sizeof(st_lex_user));
6551 6552
	    lex->grant_user= curr_user;
	  }
unknown's avatar
unknown committed
6553
	| GRANTS FOR_SYM user
unknown's avatar
unknown committed
6554 6555 6556 6557
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
6558
	    lex->grant_user->password=null_lex_str;
unknown's avatar
unknown committed
6559
	  }
unknown's avatar
unknown committed
6560
	| CREATE DATABASE opt_if_not_exists ident
unknown's avatar
unknown committed
6561 6562
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
unknown's avatar
unknown committed
6563 6564
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
unknown's avatar
unknown committed
6565
	  }
unknown's avatar
unknown committed
6566 6567
        | CREATE TABLE_SYM table_ident
          {
6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578
            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
6579
	      YYABORT;
6580
            lex->only_view= 1;
unknown's avatar
unknown committed
6581 6582 6583 6584
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
unknown's avatar
unknown committed
6585
          }
unknown's avatar
unknown committed
6586 6587 6588
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603
          }
	| 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;
	  }
6604
	| PROCEDURE STATUS_SYM wild_and_where
6605
	  {
6606 6607 6608
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_PROC;
6609 6610
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
6611 6612
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
6613
	  }
6614
	| FUNCTION_SYM STATUS_SYM wild_and_where
6615
	  {
6616 6617 6618
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_FUNC;
6619 6620
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
6621 6622
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
unknown's avatar
unknown committed
6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644
	  }
        | PROCEDURE CODE_SYM sp_name
          {
#ifdef DBUG_OFF
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
#else
            Lex->sql_command= SQLCOM_SHOW_PROC_CODE;
	    Lex->spname= $3;
#endif
          }
        | FUNCTION_SYM CODE_SYM sp_name
          {
#ifdef DBUG_OFF
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
#else
            Lex->sql_command= SQLCOM_SHOW_FUNC_CODE;
	    Lex->spname= $3;
#endif
          }
        ;
unknown's avatar
unknown committed
6645

unknown's avatar
unknown committed
6646 6647 6648 6649
show_engine_param:
	STATUS_SYM
	  {
	    switch (Lex->create_info.db_type) {
6650 6651 6652
	    case DB_TYPE_NDBCLUSTER:
	      Lex->sql_command = SQLCOM_SHOW_NDBCLUSTER_STATUS;
	      break;
unknown's avatar
unknown committed
6653 6654 6655 6656
	    case DB_TYPE_INNODB:
	      Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;
	      break;
	    default:
unknown's avatar
unknown committed
6657
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "STATUS");
unknown's avatar
unknown committed
6658 6659 6660 6661 6662 6663 6664 6665 6666 6667
	      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
6668
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "LOGS");
unknown's avatar
unknown committed
6669 6670 6671 6672
	      YYABORT;
	    }
	  };

6673 6674 6675 6676
master_or_binary:
	MASTER_SYM
	| BINARY;

unknown's avatar
unknown committed
6677 6678 6679 6680
opt_storage:
	/* empty */
	| STORAGE_SYM;

unknown's avatar
unknown committed
6681 6682
opt_db:
	/* empty */  { $$= 0; }
unknown's avatar
unknown committed
6683
	| from_or_in ident { $$= $2.str; };
unknown's avatar
unknown committed
6684

unknown's avatar
unknown committed
6685 6686
opt_full:
	/* empty */ { Lex->verbose=0; }
unknown's avatar
unknown committed
6687
	| FULL	    { Lex->verbose=1; };
unknown's avatar
unknown committed
6688

unknown's avatar
unknown committed
6689 6690
from_or_in:
	FROM
unknown's avatar
unknown committed
6691
	| IN_SYM;
unknown's avatar
unknown committed
6692

unknown's avatar
unknown committed
6693 6694
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
unknown's avatar
unknown committed
6695
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
unknown's avatar
unknown committed
6696 6697 6698

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

6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713
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
6714

unknown's avatar
unknown committed
6715 6716 6717 6718
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
6719 6720 6721 6722 6723 6724 6725 6726 6727
          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
6728 6729
	    YYABORT;
	}
6730
	opt_describe_column {}
unknown's avatar
unknown committed
6731 6732 6733
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
6734
          {
unknown's avatar
unknown committed
6735
	    LEX *lex=Lex;
6736
	    lex->select_lex.options|= SELECT_DESCRIBE;
unknown's avatar
unknown committed
6737 6738
	  }
	;
unknown's avatar
unknown committed
6739 6740 6741

describe_command:
	DESC
unknown's avatar
unknown committed
6742
	| DESCRIBE;
unknown's avatar
unknown committed
6743

unknown's avatar
unknown committed
6744 6745 6746 6747 6748
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
	;

unknown's avatar
unknown committed
6749 6750 6751
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
unknown's avatar
unknown committed
6752
	| ident
unknown's avatar
unknown committed
6753
	  { Lex->wild= new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info); };
unknown's avatar
unknown committed
6754 6755 6756 6757 6758


/* flush things */

flush:
6759
	FLUSH_SYM opt_no_write_to_binlog
unknown's avatar
unknown committed
6760 6761
	{
	  LEX *lex=Lex;
6762
	  if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
6763
	  {
6764 6765 6766 6767 6768 6769
            /*
              Note that both FLUSH TABLES and FLUSH PRIVILEGES will break
              execution in prelocked mode. So it is better to disable
              FLUSH in stored functions and triggers completely.
            */
            my_error(ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0), "FLUSH");
6770 6771
	    YYABORT;
	  }
unknown's avatar
unknown committed
6772
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
unknown's avatar
unknown committed
6773
          lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
6774
	}
6775 6776 6777
	flush_options
	{}
	;
unknown's avatar
unknown committed
6778 6779 6780

flush_options:
	flush_options ',' flush_option
unknown's avatar
unknown committed
6781
	| flush_option;
unknown's avatar
unknown committed
6782 6783

flush_option:
6784
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
unknown's avatar
unknown committed
6785
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
unknown's avatar
unknown committed
6786
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
unknown's avatar
unknown committed
6787 6788 6789 6790
	| 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
6791 6792
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
6793 6794
	| DES_KEY_FILE	{ Lex->type|= REFRESH_DES_KEY_FILE; }
 	| RESOURCES     { Lex->type|= REFRESH_USER_RESOURCES; };
unknown's avatar
unknown committed
6795

unknown's avatar
unknown committed
6796
opt_table_list:
6797 6798
	/* empty */  {;}
	| table_list {;};
unknown's avatar
unknown committed
6799

unknown's avatar
unknown committed
6800
reset:
unknown's avatar
unknown committed
6801 6802 6803 6804
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
6805 6806 6807 6808
	} reset_options
	{}
	;

unknown's avatar
unknown committed
6809 6810
reset_options:
	reset_options ',' reset_option
unknown's avatar
unknown committed
6811
	| reset_option;
unknown's avatar
unknown committed
6812 6813

reset_option:
unknown's avatar
unknown committed
6814 6815
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
unknown's avatar
unknown committed
6816
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
unknown's avatar
unknown committed
6817

unknown's avatar
unknown committed
6818
purge:
unknown's avatar
unknown committed
6819 6820 6821 6822
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
6823 6824 6825 6826 6827
	} purge_options
	{}
	;

purge_options:
6828
	master_or_binary LOGS_SYM purge_option
unknown's avatar
unknown committed
6829
	;
6830 6831

purge_option:
unknown's avatar
unknown committed
6832
        TO_SYM TEXT_STRING_sys
6833 6834 6835 6836 6837 6838
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
6839 6840 6841 6842
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
unknown's avatar
unknown committed
6843
	}
unknown's avatar
unknown committed
6844
	;
unknown's avatar
unknown committed
6845

unknown's avatar
unknown committed
6846 6847 6848
/* kill threads */

kill:
unknown's avatar
unknown committed
6849
	KILL_SYM { Lex->sql_command= SQLCOM_KILL; } kill_option expr
unknown's avatar
unknown committed
6850
	{
unknown's avatar
unknown committed
6851
	  LEX *lex=Lex;
6852
	  lex->value_list.empty();
unknown's avatar
unknown committed
6853
	  lex->value_list.push_front($4);
unknown's avatar
unknown committed
6854
	};
unknown's avatar
unknown committed
6855

6856 6857 6858
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
unknown's avatar
unknown committed
6859 6860
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; }
        ;
6861

unknown's avatar
unknown committed
6862 6863 6864
/* change database */

use:	USE_SYM ident
unknown's avatar
unknown committed
6865 6866
	{
	  LEX *lex=Lex;
6867 6868
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
unknown's avatar
unknown committed
6869
	};
unknown's avatar
unknown committed
6870 6871 6872

/* import, export of files */

unknown's avatar
unknown committed
6873
load:   LOAD DATA_SYM
unknown's avatar
unknown committed
6874 6875
        {
          LEX *lex=Lex;
6876 6877 6878 6879 6880
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD DATA");
	    YYABORT;
	  }
unknown's avatar
unknown committed
6881 6882 6883 6884 6885 6886 6887
          lex->fname_start= lex->ptr;
        }
        load_data
        {}
        |
        LOAD TABLE_SYM table_ident FROM MASTER_SYM
        {
6888 6889 6890 6891 6892 6893 6894
	  LEX *lex=Lex;
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD TABLE");
	    YYABORT;
	  }
          lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
unknown's avatar
unknown committed
6895 6896 6897 6898 6899
          if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
            YYABORT;
        };

load_data:
unknown's avatar
unknown committed
6900
	load_data_lock opt_local INFILE TEXT_STRING_filesystem
unknown's avatar
unknown committed
6901
	{
unknown's avatar
unknown committed
6902 6903
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
unknown's avatar
unknown committed
6904 6905
	  lex->lock_option= $1;
	  lex->local_file=  $2;
6906 6907
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
unknown's avatar
unknown committed
6908
	  if (!(lex->exchange= new sql_exchange($4.str, 0)))
unknown's avatar
unknown committed
6909
	    YYABORT;
unknown's avatar
unknown committed
6910 6911 6912 6913 6914
        }
        opt_duplicate INTO
        {
	  LEX *lex=Lex;
	  lex->fname_end= lex->ptr;
unknown's avatar
unknown committed
6915
	}
6916
        TABLE_SYM table_ident
unknown's avatar
unknown committed
6917
        {
6918 6919 6920 6921 6922 6923 6924
          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
6925
        }
6926 6927 6928
        opt_field_term opt_line_term opt_ignore_lines opt_field_or_var_spec
        opt_load_data_set_spec
        {}
6929
        |
unknown's avatar
unknown committed
6930
	FROM MASTER_SYM
6931 6932
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
unknown's avatar
unknown committed
6933
        };
unknown's avatar
unknown committed
6934 6935 6936

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

unknown's avatar
unknown committed
6939
load_data_lock:
6940
	/* empty */	{ $$= YYTHD->update_lock_default; }
6941 6942 6943 6944 6945 6946 6947 6948 6949 6950
	| CONCURRENT
          {
#ifdef HAVE_QUERY_CACHE
            /*
              Ignore this option in SP to avoid problem with query cache
            */
            if (Lex->sphead != 0)
#endif
              $$= TL_WRITE_CONCURRENT_INSERT;
          }
unknown's avatar
unknown committed
6951
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
6952 6953


unknown's avatar
unknown committed
6954 6955 6956
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
6957
	| IGNORE_SYM	{ Lex->ignore= 1; };
unknown's avatar
unknown committed
6958 6959 6960

opt_field_term:
	/* empty */
unknown's avatar
unknown committed
6961
	| COLUMNS field_term_list;
unknown's avatar
unknown committed
6962 6963 6964

field_term_list:
	field_term_list field_term
unknown's avatar
unknown committed
6965
	| field_term;
unknown's avatar
unknown committed
6966 6967

field_term:
6968 6969
	TERMINATED BY text_string 
          {
6970
            DBUG_ASSERT(Lex->exchange != 0);
6971 6972
            Lex->exchange->field_term= $3;
          }
unknown's avatar
unknown committed
6973
	| OPTIONALLY ENCLOSED BY text_string
unknown's avatar
unknown committed
6974
	  {
unknown's avatar
unknown committed
6975
            LEX *lex= Lex;
6976
            DBUG_ASSERT(lex->exchange != 0);
unknown's avatar
unknown committed
6977 6978
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
unknown's avatar
unknown committed
6979
	  }
unknown's avatar
unknown committed
6980
        | ENCLOSED BY text_string
6981
          {
6982
            DBUG_ASSERT(Lex->exchange != 0);
6983 6984
            Lex->exchange->enclosed= $3;
          }
unknown's avatar
unknown committed
6985
        | ESCAPED BY text_string
6986
          {
6987
            DBUG_ASSERT(Lex->exchange != 0);
6988 6989
            Lex->exchange->escaped= $3;
          };
unknown's avatar
unknown committed
6990 6991 6992

opt_line_term:
	/* empty */
unknown's avatar
unknown committed
6993
	| LINES line_term_list;
unknown's avatar
unknown committed
6994 6995 6996

line_term_list:
	line_term_list line_term
unknown's avatar
unknown committed
6997
	| line_term;
unknown's avatar
unknown committed
6998 6999

line_term:
unknown's avatar
unknown committed
7000
        TERMINATED BY text_string
7001
          {
7002
            DBUG_ASSERT(Lex->exchange != 0);
7003 7004
            Lex->exchange->line_term= $3;
          }
unknown's avatar
unknown committed
7005
        | STARTING BY text_string
7006
          {
7007
            DBUG_ASSERT(Lex->exchange != 0);
7008 7009
            Lex->exchange->line_start= $3;
          };
unknown's avatar
unknown committed
7010 7011 7012

opt_ignore_lines:
	/* empty */
unknown's avatar
unknown committed
7013 7014
        | IGNORE_SYM NUM LINES
          {
7015
            DBUG_ASSERT(Lex->exchange != 0);
7016 7017
            Lex->exchange->skip_lines= atol($2.str);
          };
unknown's avatar
unknown committed
7018

unknown's avatar
unknown committed
7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029
opt_field_or_var_spec:
	/* empty */	          { }
	| '(' fields_or_vars ')'  { }
	| '(' ')'	          { };

fields_or_vars:
        fields_or_vars ',' field_or_var
          { Lex->field_list.push_back($3); }
        | field_or_var
          { Lex->field_list.push_back($1); }
        ;
unknown's avatar
unknown committed
7030

unknown's avatar
unknown committed
7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041
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
7042 7043 7044
/* Common definitions */

text_literal:
7045
	TEXT_STRING_literal
unknown's avatar
unknown committed
7046 7047
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7048
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
unknown's avatar
unknown committed
7049
	}
unknown's avatar
unknown committed
7050
	| NCHAR_STRING
7051
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
7052
	| UNDERSCORE_CHARSET TEXT_STRING
7053
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
7054
	| text_literal TEXT_STRING_literal
unknown's avatar
unknown committed
7055 7056
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
unknown's avatar
unknown committed
7057 7058

text_string:
7059
	TEXT_STRING_literal
unknown's avatar
unknown committed
7060
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
unknown's avatar
unknown committed
7061 7062
	| HEX_NUM
	  {
unknown's avatar
unknown committed
7063
	    Item *tmp= new Item_hex_string($1.str, $1.length);
7064
	    /*
unknown's avatar
unknown committed
7065
	      it is OK only emulate fix_fields, because we need only
7066 7067 7068
              value of constant
	    */
	    $$= tmp ?
unknown's avatar
unknown committed
7069
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
7070
	      (String*) 0;
7071
	  }
unknown's avatar
unknown committed
7072 7073 7074 7075 7076 7077 7078 7079 7080 7081
        | 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;
          }
7082 7083
	;

unknown's avatar
unknown committed
7084
param_marker:
7085
        PARAM_MARKER
unknown's avatar
unknown committed
7086
        {
7087 7088
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
7089 7090 7091
          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
7092
          {
7093
            my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
unknown's avatar
unknown committed
7094 7095
            YYABORT;
          }
7096 7097 7098
        }
	;

unknown's avatar
unknown committed
7099 7100 7101
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
7102 7103
	| '-' NUM_literal
	  {
7104
	    $2->max_length++;
unknown's avatar
unknown committed
7105
	    $$= $2->neg();
7106
	  }
unknown's avatar
unknown committed
7107 7108 7109
	;


unknown's avatar
unknown committed
7110 7111
literal:
	text_literal	{ $$ =	$1; }
unknown's avatar
unknown committed
7112
	| NUM_literal	{ $$ = $1; }
unknown's avatar
unknown committed
7113
	| NULL_SYM	{ $$ =	new Item_null();
7114
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
7115 7116
	| FALSE_SYM	{ $$= new Item_int((char*) "FALSE",0,1); }
	| TRUE_SYM	{ $$= new Item_int((char*) "TRUE",1,1); }
unknown's avatar
unknown committed
7117 7118
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
7119
	| UNDERSCORE_CHARSET HEX_NUM
7120
	  {
unknown's avatar
unknown committed
7121
	    Item *tmp= new Item_hex_string($2.str, $2.length);
7122
	    /*
unknown's avatar
unknown committed
7123
	      it is OK only emulate fix_fieds, because we need only
7124 7125 7126
              value of constant
	    */
	    String *str= tmp ?
unknown's avatar
unknown committed
7127
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
7128
	      (String*) 0;
unknown's avatar
unknown committed
7129 7130 7131
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
7132
	  }
unknown's avatar
unknown committed
7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146
	| 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
7147 7148
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
unknown's avatar
unknown committed
7149
	| TIMESTAMP text_literal { $$ = $2; };
unknown's avatar
unknown committed
7150

7151
NUM_literal:
7152 7153
	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); }
7154
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
unknown's avatar
unknown committed
7155
        | DECIMAL_NUM
7156
	{
unknown's avatar
unknown committed
7157
           $$= new Item_decimal($1.str, $1.length, YYTHD->charset());
7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
7171
	;
unknown's avatar
unknown committed
7172

unknown's avatar
unknown committed
7173
/**********************************************************************
unknown's avatar
unknown committed
7174
** Creating different items.
unknown's avatar
unknown committed
7175 7176 7177
**********************************************************************/

insert_ident:
7178
	simple_ident_nospvar { $$=$1; }
unknown's avatar
unknown committed
7179
	| table_wild	 { $$=$1; };
unknown's avatar
unknown committed
7180 7181

table_wild:
unknown's avatar
unknown committed
7182
	ident '.' '*'
7183
	{
7184
          SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7185
	  $$ = new Item_field(Lex->current_context(), NullS, $1.str, "*");
7186
	  sel->with_wild++;
7187
	}
unknown's avatar
unknown committed
7188
	| ident '.' ident '.' '*'
7189
	{
7190
          SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7191
	  $$ = new Item_field(Lex->current_context(), (YYTHD->client_capabilities &
unknown's avatar
unknown committed
7192 7193
                             CLIENT_NO_SCHEMA ? NullS : $1.str),
                             $3.str,"*");
7194
	  sel->with_wild++;
7195 7196
	}
	;
unknown's avatar
unknown committed
7197 7198

order_ident:
unknown's avatar
unknown committed
7199
	expr { $$=$1; };
unknown's avatar
unknown committed
7200 7201

simple_ident:
7202 7203
	ident
	{
7204
	  sp_variable_t *spv;
7205 7206
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;
7207
	  if (spc && (spv = spc->find_variable(&$1)))
7208 7209 7210
	  {
            /* We're compiling a stored procedure and found a variable */
            Item_splocal *splocal;
7211 7212
            splocal= new Item_splocal($1, spv->offset, spv->type,
                                      lex->tok_start_prev - 
7213
                                      lex->sphead->m_tmp_query);
unknown's avatar
unknown committed
7214
#ifndef DBUG_OFF
7215
            if (splocal)
7216
              splocal->m_sp= lex->sphead;
7217
#endif
7218
	    $$ = (Item*) splocal;
7219 7220 7221 7222 7223 7224 7225 7226
            lex->variables_used= 1;
	    lex->safe_to_cache_query=0;
	  }
	  else
	  {
	    SELECT_LEX *sel=Select;
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
7227 7228
                 (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	         (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
7229 7230 7231 7232 7233 7234
	  }
        }
        | simple_ident_q { $$= $1; }
	;

simple_ident_nospvar:
unknown's avatar
unknown committed
7235
	ident
unknown's avatar
unknown committed
7236
	{
unknown's avatar
unknown committed
7237
	  SELECT_LEX *sel=Select;
7238
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
7239
	       sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
7240 7241
              (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	      (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
unknown's avatar
unknown committed
7242
	}
7243
	| simple_ident_q { $$= $1; }
unknown's avatar
unknown committed
7244
	;
7245 7246 7247

simple_ident_q:
	ident '.' ident
unknown's avatar
unknown committed
7248
	{
unknown's avatar
unknown committed
7249
	  THD *thd= YYTHD;
7250
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
7251

7252 7253 7254 7255 7256 7257
          /*
            FIXME This will work ok in simple_ident_nospvar case because
            we can't meet simple_ident_nospvar in trigger now. But it
            should be changed in future.
          */
          if (lex->sphead && lex->sphead->m_type == TYPE_ENUM_TRIGGER &&
unknown's avatar
unknown committed
7258
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") ||
7259 7260
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
7261
            Item_trigger_field *trg_fld;
7262
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
unknown's avatar
unknown committed
7263

7264 7265 7266
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
unknown's avatar
unknown committed
7267
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
7268 7269
              YYABORT;
            }
unknown's avatar
unknown committed
7270

7271 7272 7273
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
unknown's avatar
unknown committed
7274
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
7275 7276
              YYABORT;
            }
unknown's avatar
unknown committed
7277

7278 7279 7280 7281 7282
            DBUG_ASSERT(!new_row ||
                        (lex->trg_chistics.event == TRG_EVENT_INSERT ||
                         lex->trg_chistics.event == TRG_EVENT_UPDATE));
            const bool read_only=
              !(new_row && lex->trg_chistics.action_time == TRG_ACTION_BEFORE);
unknown's avatar
unknown committed
7283
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
7284
                                                  new_row ?
7285 7286
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
unknown's avatar
unknown committed
7287
                                                  $3.str,
7288 7289
                                                  SELECT_ACL,
                                                  read_only)))
7290
              YYABORT;
unknown's avatar
unknown committed
7291

7292 7293 7294 7295 7296 7297
            /*
              Let us add this item to list of all Item_trigger_field objects
              in trigger.
            */
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
              (byte**)&trg_fld->next_trg_field);
unknown's avatar
unknown committed
7298

7299 7300 7301 7302 7303 7304 7305
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
7306 7307
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
7308 7309 7310
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
7311 7312
	        (Item*) new Item_field(Lex->current_context(), NullS, $1.str, $3.str) :
	        (Item*) new Item_ref(Lex->current_context(), NullS, $1.str, $3.str);
7313 7314
          }
        }
unknown's avatar
unknown committed
7315
	| '.' ident '.' ident
unknown's avatar
unknown committed
7316
	{
unknown's avatar
unknown committed
7317
	  THD *thd= YYTHD;
7318
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
7319
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
7320 7321
	  if (sel->no_table_names_allowed)
	  {
7322 7323
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $2.str, thd->where);
unknown's avatar
unknown committed
7324
	  }
7325
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
7326
	       sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
7327 7328
	      (Item*) new Item_field(Lex->current_context(), NullS, $2.str, $4.str) :
              (Item*) new Item_ref(Lex->current_context(), NullS, $2.str, $4.str);
unknown's avatar
unknown committed
7329
	}
unknown's avatar
unknown committed
7330
	| ident '.' ident '.' ident
unknown's avatar
unknown committed
7331
	{
unknown's avatar
unknown committed
7332
	  THD *thd= YYTHD;
7333
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
7334
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
7335 7336
	  if (sel->no_table_names_allowed)
	  {
7337 7338
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $3.str, thd->where);
unknown's avatar
unknown committed
7339
	  }
7340
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
7341
	       sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
7342
	      (Item*) new Item_field(Lex->current_context(),
7343
                                     (YYTHD->client_capabilities &
unknown's avatar
unknown committed
7344 7345
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
unknown's avatar
unknown committed
7346
	      (Item*) new Item_ref(Lex->current_context(),
7347
                                   (YYTHD->client_capabilities &
7348
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
unknown's avatar
unknown committed
7349
                                   $3.str, $5.str);
unknown's avatar
unknown committed
7350
	};
unknown's avatar
unknown committed
7351 7352 7353 7354


field_ident:
	ident			{ $$=$1;}
7355 7356 7357 7358 7359
	| ident '.' ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->db))
            {
7360
              my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
7361 7362
              YYABORT;
            }
7363 7364
            if (my_strcasecmp(table_alias_charset, $3.str,
                              table->table_name))
7365
            {
7366
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3.str);
7367 7368 7369 7370 7371 7372 7373 7374 7375
              YYABORT;
            }
            $$=$5;
          }
	| ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->alias))
            {
7376
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $1.str);
7377 7378 7379 7380
              YYABORT;
            }
            $$=$3;
          }
unknown's avatar
unknown committed
7381
	| '.' ident		{ $$=$2;}	/* For Delphi */;
unknown's avatar
unknown committed
7382 7383 7384

table_ident:
	ident			{ $$=new Table_ident($1); }
unknown's avatar
unknown committed
7385
	| ident '.' ident	{ $$=new Table_ident(YYTHD, $1,$3,0);}
7386 7387 7388
	| '.' ident		{ $$=new Table_ident($2);} /* For Delphi */
        ;

unknown's avatar
unknown committed
7389
table_ident_nodb:
unknown's avatar
unknown committed
7390
	ident			{ LEX_STRING db={(char*) any_db,3}; $$=new Table_ident(YYTHD, db,$1,0); }
7391
        ;
unknown's avatar
unknown committed
7392

unknown's avatar
unknown committed
7393
IDENT_sys:
7394 7395 7396 7397 7398
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
7399 7400
            {
              CHARSET_INFO *cs= system_charset_info;
7401
              int dummy_error;
7402 7403
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
7404
                                                   $1.length, &dummy_error);
7405 7406
              if (wlen < $1.length)
              {
7407 7408
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
7409 7410
                YYABORT;
              }
7411
	      $$= $1;
7412
            }
7413 7414 7415 7416
	    else
	      thd->convert_string(&$$, system_charset_info,
				  $1.str, $1.length, thd->charset());
	  }
unknown's avatar
unknown committed
7417 7418 7419 7420 7421 7422
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7423 7424
	  if (thd->charset_is_system_charset)
	    $$= $1;
unknown's avatar
unknown committed
7425
	  else
unknown's avatar
unknown committed
7426 7427
	    thd->convert_string(&$$, system_charset_info,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
7428 7429 7430
	}
	;

7431
TEXT_STRING_literal:
unknown's avatar
unknown committed
7432 7433 7434
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7435 7436
	  if (thd->charset_is_collation_connection)
	    $$= $1;
unknown's avatar
unknown committed
7437
	  else
unknown's avatar
unknown committed
7438 7439
	    thd->convert_string(&$$, thd->variables.collation_connection,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
7440 7441 7442 7443
	}
	;


unknown's avatar
unknown committed
7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455
TEXT_STRING_filesystem:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
	  if (thd->charset_is_character_set_filesystem)
	    $$= $1;
	  else
	    thd->convert_string(&$$, thd->variables.character_set_filesystem,
				$1.str, $1.length, thd->charset());
	}
	;

unknown's avatar
unknown committed
7456
ident:
unknown's avatar
unknown committed
7457
	IDENT_sys	    { $$=$1; }
unknown's avatar
unknown committed
7458 7459
	| keyword
	{
unknown's avatar
unknown committed
7460 7461 7462
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
7463 7464
	}
	;
unknown's avatar
unknown committed
7465

7466 7467 7468 7469 7470 7471 7472 7473 7474 7475
label_ident:
	IDENT_sys	    { $$=$1; }
	| keyword_sp
	{
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
	}
	;

unknown's avatar
unknown committed
7476
ident_or_text:
unknown's avatar
unknown committed
7477
        ident                   { $$=$1;}
unknown's avatar
unknown committed
7478 7479
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
unknown's avatar
unknown committed
7480 7481 7482 7483

user:
	ident_or_text
	{
unknown's avatar
unknown committed
7484 7485
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
7486
	    YYABORT;
7487 7488 7489 7490
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
unknown's avatar
unknown committed
7491 7492
	| ident_or_text '@' ident_or_text
	  {
unknown's avatar
unknown committed
7493 7494
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
7495 7496
	      YYABORT;
	    $$->user = $1; $$->host=$3;
7497
	  }
unknown's avatar
unknown committed
7498
	| CURRENT_USER optional_braces
7499
	{
7500
          if (!($$=(LEX_USER*) YYTHD->alloc(sizeof(st_lex_user))))
7501
            YYABORT;
7502 7503 7504 7505 7506 7507
          /* 
            empty LEX_USER means current_user and 
            will be handled in the  get_current_user() function
            later
          */
          bzero($$, sizeof(LEX_USER));
7508
	};
unknown's avatar
unknown committed
7509

7510
/* Keyword that we allow for identifiers (except SP labels) */
unknown's avatar
unknown committed
7511
keyword:
7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551
	keyword_sp		{}
	| ASCII_SYM		{}
	| BACKUP_SYM		{}
	| BEGIN_SYM		{}
	| BYTE_SYM		{}
	| CACHE_SYM		{}
	| CHARSET		{}
	| CHECKSUM_SYM		{}
	| CLOSE_SYM		{}
	| COMMENT_SYM		{}
	| COMMIT_SYM		{}
	| CONTAINS_SYM          {}
        | DEALLOCATE_SYM        {}
	| DO_SYM		{}
	| END			{}
	| EXECUTE_SYM		{}
	| FLUSH_SYM		{}
	| HANDLER_SYM		{}
	| HELP_SYM		{}
	| LANGUAGE_SYM          {}
	| NO_SYM		{}
	| OPEN_SYM		{}
        | PREPARE_SYM           {}
	| REPAIR		{}
	| RESET_SYM		{}
	| RESTORE_SYM		{}
	| ROLLBACK_SYM		{}
	| SAVEPOINT_SYM		{}
	| SECURITY_SYM		{}
	| SIGNED_SYM		{}
	| SLAVE			{}
	| START_SYM		{}
	| STOP_SYM		{}
	| TRUNCATE_SYM		{}
	| UNICODE_SYM		{}
        | XA_SYM                {}
	;

/*
 * Keywords that we allow for labels in SPs.
unknown's avatar
unknown committed
7552 7553 7554
 * Anything that's the beginning of a statement or characteristics
 * must be in keyword above, otherwise we get (harmful) shift/reduce
 * conflicts.
7555 7556
 */
keyword_sp:
unknown's avatar
unknown committed
7557
	ACTION			{}
unknown's avatar
unknown committed
7558
	| ADDDATE_SYM		{}
unknown's avatar
unknown committed
7559
	| AFTER_SYM		{}
7560
	| AGAINST		{}
unknown's avatar
unknown committed
7561
	| AGGREGATE_SYM		{}
7562
	| ALGORITHM_SYM		{}
unknown's avatar
unknown committed
7563
	| ANY_SYM		{}
unknown's avatar
unknown committed
7564
	| AUTO_INC		{}
unknown's avatar
unknown committed
7565 7566
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
unknown's avatar
unknown committed
7567
	| BERKELEY_DB_SYM	{}
unknown's avatar
unknown committed
7568
	| BINLOG_SYM		{}
unknown's avatar
unknown committed
7569 7570
	| BIT_SYM		{}
	| BOOL_SYM		{}
unknown's avatar
unknown committed
7571
	| BOOLEAN_SYM		{}
unknown's avatar
unknown committed
7572
	| BTREE_SYM		{}
7573
	| CASCADED              {}
unknown's avatar
unknown committed
7574
	| CHAIN_SYM		{}
7575
	| CHANGED		{}
7576
	| CIPHER_SYM		{}
unknown's avatar
unknown committed
7577
	| CLIENT_SYM		{}
unknown's avatar
unknown committed
7578
        | CODE_SYM              {}
unknown's avatar
unknown committed
7579
	| COLLATION_SYM		{}
7580
        | COLUMNS               {}
unknown's avatar
unknown committed
7581
	| COMMITTED_SYM		{}
7582
	| COMPACT_SYM		{}
unknown's avatar
unknown committed
7583
	| COMPRESSED_SYM	{}
unknown's avatar
unknown committed
7584
	| CONCURRENT		{}
7585
	| CONSISTENT_SYM	{}
7586
	| CUBE_SYM		{}
unknown's avatar
unknown committed
7587 7588 7589 7590
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
7591
	| DEFINER_SYM		{}
unknown's avatar
unknown committed
7592
	| DELAY_KEY_WRITE_SYM	{}
unknown's avatar
unknown committed
7593 7594
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
unknown's avatar
unknown committed
7595
	| DISCARD		{}
unknown's avatar
unknown committed
7596 7597
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
unknown's avatar
unknown committed
7598 7599
	| DYNAMIC_SYM		{}
	| ENUM			{}
unknown's avatar
unknown committed
7600
	| ENGINE_SYM		{}
unknown's avatar
unknown committed
7601
	| ENGINES_SYM		{}
7602
	| ERRORS		{}
unknown's avatar
unknown committed
7603
	| ESCAPE_SYM		{}
unknown's avatar
unknown committed
7604
	| EVENTS_SYM		{}
7605
        | EXPANSION_SYM         {}
unknown's avatar
unknown committed
7606
	| EXTENDED_SYM		{}
7607
	| FAST_SYM		{}
7608
	| FOUND_SYM		{}
unknown's avatar
unknown committed
7609 7610
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
unknown's avatar
unknown committed
7611
	| FULL			{}
unknown's avatar
unknown committed
7612 7613 7614
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
7615
	| FRAC_SECOND_SYM	{}
7616
	| GEOMETRY_SYM		{}
7617
	| GEOMETRYCOLLECTION	{}
7618
	| GET_FORMAT		{}
unknown's avatar
unknown committed
7619
	| GRANTS		{}
unknown's avatar
unknown committed
7620
	| GLOBAL_SYM		{}
unknown's avatar
unknown committed
7621
	| HASH_SYM		{}
unknown's avatar
unknown committed
7622 7623 7624
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
7625
	| INVOKER_SYM		{}
unknown's avatar
unknown committed
7626
	| IMPORT		{}
unknown's avatar
unknown committed
7627
	| INDEXES		{}
unknown's avatar
unknown committed
7628
	| ISOLATION		{}
unknown's avatar
unknown committed
7629
	| ISSUER_SYM		{}
unknown's avatar
unknown committed
7630
	| INNOBASE_SYM		{}
7631
	| INSERT_METHOD		{}
unknown's avatar
unknown committed
7632
	| RELAY_THREAD		{}
7633
	| LAST_SYM		{}
unknown's avatar
unknown committed
7634
	| LEAVES                {}
unknown's avatar
unknown committed
7635
	| LEVEL_SYM		{}
7636
	| LINESTRING		{}
unknown's avatar
unknown committed
7637
	| LOCAL_SYM		{}
unknown's avatar
unknown committed
7638
	| LOCKS_SYM		{}
unknown's avatar
unknown committed
7639 7640 7641 7642 7643 7644 7645 7646 7647
	| 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
7648
	| MASTER_SERVER_ID_SYM  {}
unknown's avatar
unknown committed
7649
	| MASTER_CONNECT_RETRY_SYM	{}
unknown's avatar
unknown committed
7650 7651 7652 7653 7654 7655
	| 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
7656 7657 7658
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
7659
	| MAX_USER_CONNECTIONS_SYM {}
7660
	| MEDIUM_SYM		{}
7661
	| MERGE_SYM		{}
unknown's avatar
unknown committed
7662
	| MICROSECOND_SYM	{}
7663
        | MIGRATE_SYM           {}
unknown's avatar
unknown committed
7664 7665 7666
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
unknown's avatar
unknown committed
7667
	| MODE_SYM		{}
unknown's avatar
unknown committed
7668
	| MONTH_SYM		{}
7669 7670 7671
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
7672
        | MUTEX_SYM             {}
7673
	| NAME_SYM              {}
unknown's avatar
unknown committed
7674
	| NAMES_SYM		{}
unknown's avatar
unknown committed
7675 7676
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
unknown's avatar
unknown committed
7677
	| NDBCLUSTER_SYM	{}
7678
	| NEXT_SYM		{}
unknown's avatar
unknown committed
7679
	| NEW_SYM		{}
7680
	| NONE_SYM		{}
unknown's avatar
unknown committed
7681
	| NVARCHAR_SYM		{}
unknown's avatar
unknown committed
7682
	| OFFSET_SYM		{}
unknown's avatar
unknown committed
7683
	| OLD_PASSWORD		{}
7684
	| ONE_SHOT_SYM		{}
7685
        | ONE_SYM               {}
unknown's avatar
unknown committed
7686
	| PACK_KEYS_SYM		{}
7687
	| PARTIAL		{}
unknown's avatar
unknown committed
7688
	| PASSWORD		{}
7689
        | PHASE_SYM             {}
7690
	| POINT_SYM		{}
7691
	| POLYGON		{}
7692
	| PREV_SYM		{}
7693
        | PRIVILEGES            {}
unknown's avatar
unknown committed
7694 7695
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
7696
	| QUARTER_SYM		{}
unknown's avatar
unknown committed
7697
	| QUERY_SYM		{}
7698
	| QUICK			{}
unknown's avatar
unknown committed
7699
	| RAID_0_SYM		{}
unknown's avatar
unknown committed
7700 7701
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
unknown's avatar
unknown committed
7702
	| RAID_STRIPED_SYM	{}
unknown's avatar
unknown committed
7703
	| RAID_TYPE		{}
7704
        | RECOVER_SYM           {}
unknown's avatar
unknown committed
7705
        | REDUNDANT_SYM         {}
unknown's avatar
unknown committed
7706 7707
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
unknown's avatar
unknown committed
7708
	| RELOAD		{}
unknown's avatar
unknown committed
7709
	| REPEATABLE_SYM	{}
unknown's avatar
unknown committed
7710
	| REPLICATION		{}
7711
	| RESOURCES		{}
7712
        | RESUME_SYM            {}
7713
	| RETURNS_SYM           {}
7714
	| ROLLUP_SYM		{}
7715
	| ROUTINE_SYM		{}
unknown's avatar
unknown committed
7716 7717 7718
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
unknown's avatar
unknown committed
7719
	| RTREE_SYM		{}
unknown's avatar
unknown committed
7720
	| SECOND_SYM		{}
7721
	| SERIAL_SYM		{}
unknown's avatar
unknown committed
7722 7723
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
7724
	| SIMPLE_SYM		{}
unknown's avatar
unknown committed
7725
	| SHARE_SYM		{}
unknown's avatar
unknown committed
7726
	| SHUTDOWN		{}
7727
	| SNAPSHOT_SYM		{}
unknown's avatar
unknown committed
7728
	| SOUNDS_SYM		{}
unknown's avatar
unknown committed
7729
	| SQL_CACHE_SYM		{}
unknown's avatar
unknown committed
7730
	| SQL_BUFFER_RESULT	{}
unknown's avatar
unknown committed
7731
	| SQL_NO_CACHE_SYM	{}
unknown's avatar
unknown committed
7732
	| SQL_THREAD		{}
unknown's avatar
unknown committed
7733
	| STATUS_SYM		{}
unknown's avatar
unknown committed
7734
	| STORAGE_SYM		{}
unknown's avatar
unknown committed
7735
	| STRING_SYM		{}
unknown's avatar
unknown committed
7736
	| SUBDATE_SYM		{}
unknown's avatar
unknown committed
7737
	| SUBJECT_SYM		{}
unknown's avatar
unknown committed
7738
	| SUPER_SYM		{}
7739
        | SUSPEND_SYM           {}
7740
        | TABLES                {}
unknown's avatar
unknown committed
7741
	| TABLESPACE		{}
unknown's avatar
unknown committed
7742
	| TEMPORARY		{}
7743
	| TEMPTABLE_SYM		{}
unknown's avatar
unknown committed
7744
	| TEXT_SYM		{}
unknown's avatar
unknown committed
7745
	| TRANSACTION_SYM	{}
7746
	| TRIGGERS_SYM		{}
unknown's avatar
unknown committed
7747
	| TIMESTAMP		{}
7748 7749
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
unknown's avatar
unknown committed
7750
	| TIME_SYM		{}
unknown's avatar
unknown committed
7751
	| TYPES_SYM		{}
7752
        | TYPE_SYM              {}
unknown's avatar
unknown committed
7753
        | UDF_RETURNS_SYM       {}
7754
	| FUNCTION_SYM		{}
unknown's avatar
unknown committed
7755
	| UNCOMMITTED_SYM	{}
7756
	| UNDEFINED_SYM		{}
7757
	| UNKNOWN_SYM		{}
7758
	| UNTIL_SYM		{}
7759
	| USER			{}
unknown's avatar
unknown committed
7760
	| USE_FRM		{}
unknown's avatar
unknown committed
7761
	| VARIABLES		{}
7762
	| VIEW_SYM		{}
7763
	| VALUE_SYM		{}
7764
	| WARNINGS		{}
7765
	| WEEK_SYM		{}
unknown's avatar
unknown committed
7766
	| WORK_SYM		{}
7767
	| X509_SYM		{}
unknown's avatar
unknown committed
7768 7769
	| YEAR_SYM		{}
	;
unknown's avatar
unknown committed
7770 7771 7772 7773 7774 7775

/* Option functions */

set:
	SET opt_option
	{
unknown's avatar
unknown committed
7776 7777
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
unknown's avatar
unknown committed
7778
	  mysql_init_select(lex);
7779
	  lex->option_type=OPT_SESSION;
unknown's avatar
unknown committed
7780
	  lex->var_list.empty();
7781
          lex->one_shot_set= 0;
unknown's avatar
unknown committed
7782
	}
7783 7784 7785
	option_value_list
	{}
	;
unknown's avatar
unknown committed
7786 7787 7788

opt_option:
	/* empty */ {}
unknown's avatar
unknown committed
7789
	| OPTION {};
unknown's avatar
unknown committed
7790 7791

option_value_list:
7792 7793 7794 7795 7796 7797 7798 7799 7800 7801
	option_type_value
	| option_value_list ',' option_type_value;

option_type_value:
        {
          if (Lex->sphead)
          {
            /*
              If we are in SP we want have own LEX for each assignment.
              This is mostly because it is hard for several sp_instr_set
unknown's avatar
unknown committed
7802 7803
              and sp_instr_set_trigger instructions share one LEX.
              (Well, it is theoretically possible but adds some extra
7804 7805 7806 7807 7808 7809 7810 7811
               overhead on preparation for execution stage and IMO less
               robust).

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

7813 7814 7815 7816 7817 7818 7819 7820 7821
            /* 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;
          }
        }
7822
	ext_option_value
7823 7824
        {
          LEX *lex= Lex;
unknown's avatar
unknown committed
7825

7826 7827 7828
          if (lex->sphead)
          {
            sp_head *sp= lex->sphead;
unknown's avatar
unknown committed
7829

7830 7831 7832 7833 7834 7835 7836 7837 7838
	    if (!lex->var_list.is_empty())
	    {
              /*
                We have assignment to user or system variable or
                option setting, so we should construct sp_instr_stmt
                for it.
              */
              LEX_STRING qbuff;
	      sp_instr_stmt *i;
unknown's avatar
unknown committed
7839

7840 7841 7842
              if (!(i= new sp_instr_stmt(sp->instructions(), lex->spcont,
                                         lex)))
                YYABORT;
unknown's avatar
unknown committed
7843

7844 7845 7846 7847 7848 7849
              /*
                Extract the query statement from the tokenizer.  The
                end is either lex->ptr, if there was no lookahead,
                lex->tok_end otherwise.
              */
              if (yychar == YYEMPTY)
7850 7851 7852
                qbuff.length= lex->ptr - sp->m_tmp_query;
              else
                qbuff.length= lex->tok_end - sp->m_tmp_query;
unknown's avatar
unknown committed
7853

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

7857 7858 7859 7860 7861 7862 7863 7864 7865
              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
7866 7867

option_type:
7868 7869 7870 7871 7872 7873 7874 7875 7876
        option_type2    {}
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

option_type2:
	/* empty */	{ $$= OPT_DEFAULT; }
	| ONE_SHOT_SYM	{ Lex->one_shot_set= 1; $$= OPT_SESSION; }
unknown's avatar
unknown committed
7877 7878 7879 7880
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
unknown's avatar
unknown committed
7881
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
7882 7883 7884 7885 7886 7887
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

opt_var_ident_type:
	/* empty */		{ $$=OPT_DEFAULT; }
unknown's avatar
unknown committed
7888
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
7889 7890 7891
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
unknown's avatar
unknown committed
7892

7893 7894 7895
ext_option_value:
        sys_option_value
        | option_type2 option_value;
7896

7897 7898 7899 7900
sys_option_value:
        option_type internal_variable_name equal set_expr_or_default
        {
          LEX *lex=Lex;
7901

7902 7903 7904 7905
          if ($2.var == &trg_new_row_fake_var)
          {
            /* We are in trigger and assigning value to field of new row */
            Item *it;
7906
            Item_trigger_field *trg_fld;
7907 7908
            sp_instr_set_trigger_field *sp_fld;
	    LINT_INIT(sp_fld);
7909 7910 7911 7912
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
7913
            }
7914 7915
            if ($4)
              it= $4;
7916
            else
7917 7918 7919 7920
            {
              /* QQ: Shouldn't this be field's default value ? */
              it= new Item_null();
            }
7921

7922 7923 7924
            DBUG_ASSERT(lex->trg_chistics.action_time == TRG_ACTION_BEFORE &&
                        (lex->trg_chistics.event == TRG_EVENT_INSERT ||
                         lex->trg_chistics.event == TRG_EVENT_UPDATE));
unknown's avatar
unknown committed
7925
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
7926
                                                  Item_trigger_field::NEW_ROW,
unknown's avatar
unknown committed
7927
                                                  $2.base_name.str,
7928
                                                  UPDATE_ACL, FALSE)) ||
7929 7930 7931 7932 7933
                !(sp_fld= new sp_instr_set_trigger_field(lex->sphead->
                          	                         instructions(),
                                	                 lex->spcont,
							 trg_fld,
                                        	         it, lex)))
7934
              YYABORT;
7935

7936 7937 7938 7939
            /*
              Let us add this item to list of all Item_trigger_field
              objects in trigger.
            */
7940 7941
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
                                    (byte **)&trg_fld->next_trg_field);
7942

7943
            lex->sphead->add_instr(sp_fld);
7944 7945 7946 7947
          }
          else if ($2.var)
          { /* System variable */
            if ($1)
7948
              lex->option_type= $1;
7949 7950 7951 7952 7953 7954 7955
            lex->var_list.push_back(new set_var(lex->option_type, $2.var,
                                    &$2.base_name, $4));
          }
          else
          {
            /* An SP local variable */
            sp_pcontext *ctx= lex->spcont;
7956
            sp_variable_t *spv;
7957
            sp_instr_set *sp_set;
7958 7959 7960 7961 7962 7963 7964
            Item *it;
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }

7965
            spv= ctx->find_variable(&$2.base_name);
7966 7967 7968 7969 7970 7971 7972

            if ($4)
              it= $4;
            else if (spv->dflt)
              it= spv->dflt;
            else
              it= new Item_null();
7973 7974 7975
            sp_set= new sp_instr_set(lex->sphead->instructions(), ctx,
                                     spv->offset, it, spv->type, lex, TRUE);
            lex->sphead->add_instr(sp_set);
7976 7977 7978 7979 7980
          }
        }
        | option_type TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	{
	  LEX *lex=Lex;
7981
          if ($1)
7982
            lex->option_type= $1;
7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994
	  lex->var_list.push_back(new set_var(lex->option_type,
                                              find_sys_var("tx_isolation"),
                                              &null_lex_str,
                                              new Item_int((int32) $5)));
	}
        ;

option_value:
	'@' ident_or_text equal expr
	{
          Lex->var_list.push_back(new set_var_user(new Item_func_set_user_var($2,$4)));
	}
unknown's avatar
unknown committed
7995
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
7996 7997
	  {
	    LEX *lex=Lex;
7998
	    lex->var_list.push_back(new set_var($3, $4.var, &$4.base_name, $6));
unknown's avatar
unknown committed
7999
	  }
8000
	| charset old_or_new_charset_name_or_default
unknown's avatar
unknown committed
8001
	{
unknown's avatar
unknown committed
8002
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
8003
	  LEX *lex= Lex;
8004
	  $2= $2 ? $2: global_system_variables.character_set_client;
8005
	  lex->var_list.push_back(new set_var_collation_client($2,thd->variables.collation_database,$2));
unknown's avatar
unknown committed
8006
	}
8007 8008 8009 8010 8011 8012 8013 8014
        | NAMES_SYM equal expr
	  {
	    LEX *lex= Lex;
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING names;

	    names.str= (char *)"names";
	    names.length= 5;
8015
	    if (spc && spc->find_variable(&names))
8016
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), names.str);
8017 8018 8019
            else
              yyerror(ER(ER_SYNTAX_ERROR));

8020 8021
	    YYABORT;
	  }
8022
	| NAMES_SYM charset_name_or_default opt_collate
8023
	{
unknown's avatar
unknown committed
8024
	  LEX *lex= Lex;
8025
	  $2= $2 ? $2 : global_system_variables.character_set_client;
8026 8027
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
8028
	  {
8029 8030
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
unknown's avatar
unknown committed
8031
	    YYABORT;
8032
	  }
8033
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
8034
	}
unknown's avatar
unknown committed
8035
	| PASSWORD equal text_or_password
unknown's avatar
unknown committed
8036
	  {
8037
	    THD *thd=YYTHD;
unknown's avatar
unknown committed
8038
	    LEX_USER *user;
8039 8040 8041 8042 8043 8044
	    LEX *lex= Lex;	    
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING pw;

	    pw.str= (char *)"password";
	    pw.length= 8;
8045
	    if (spc && spc->find_variable(&pw))
8046 8047 8048 8049
	    {
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), pw.str);
	      YYABORT;
	    }
unknown's avatar
unknown committed
8050
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
unknown's avatar
unknown committed
8051
	      YYABORT;
8052
	    user->host=null_lex_str;
8053
	    user->user.str=thd->security_ctx->priv_user;
8054
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
unknown's avatar
unknown committed
8055 8056
	  }
	| PASSWORD FOR_SYM user equal text_or_password
unknown's avatar
unknown committed
8057
	  {
unknown's avatar
unknown committed
8058 8059
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
8060
	;
unknown's avatar
unknown committed
8061

unknown's avatar
unknown committed
8062 8063 8064
internal_variable_name:
	ident
	{
8065 8066
	  LEX *lex= Lex;
          sp_pcontext *spc= lex->spcont;
8067
	  sp_variable_t *spv;
8068 8069

	  /* We have to lookup here since local vars can shadow sysvars */
8070
	  if (!spc || !(spv = spc->find_variable(&$1)))
8071 8072 8073 8074 8075 8076
	  {
            /* Not an SP local variable */
	    sys_var *tmp=find_sys_var($1.str, $1.length);
	    if (!tmp)
	      YYABORT;
	    $$.var= tmp;
8077
	    $$.base_name= null_lex_str;
8078 8079 8080 8081
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
8082 8083 8084
            if (tmp == &sys_time_zone &&
                lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
8085 8086 8087 8088 8089 8090 8091 8092
            else if (spc && tmp == &sys_autocommit)
            {
              /*
                We don't allow setting AUTOCOMMIT from a stored function
		or trigger.
              */
              lex->sphead->m_flags|= sp_head::HAS_SET_AUTOCOMMIT_STMT;
            }
8093 8094 8095 8096 8097 8098 8099
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
unknown's avatar
unknown committed
8100
	}
8101 8102
	| ident '.' ident
	  {
8103
            LEX *lex= Lex;
8104 8105
            if (check_reserved_words(&$1))
            {
8106
	      yyerror(ER(ER_SYNTAX_ERROR));
8107 8108
              YYABORT;
            }
8109 8110 8111 8112 8113 8114
            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
8115
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
8116 8117 8118 8119
                YYABORT;
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
unknown's avatar
unknown committed
8120 8121
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
8122 8123 8124 8125
                YYABORT;
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
unknown's avatar
unknown committed
8126
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138
                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())
8139
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
8140 8141 8142
              $$.var= tmp;
              $$.base_name= $1;
            }
8143 8144 8145 8146 8147 8148 8149
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
8150
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
unknown's avatar
unknown committed
8151 8152 8153
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
8154
	  }
8155
        ;
unknown's avatar
unknown committed
8156 8157 8158 8159 8160 8161 8162

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

unknown's avatar
unknown committed
8164 8165 8166 8167
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
8168
	    $$= $3.length ? YYTHD->variables.old_passwords ?
unknown's avatar
unknown committed
8169 8170 8171 8172 8173 8174 8175 8176
	        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;
8177 8178
	  }
          ;
unknown's avatar
unknown committed
8179

unknown's avatar
unknown committed
8180

unknown's avatar
unknown committed
8181
set_expr_or_default:
unknown's avatar
unknown committed
8182 8183
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
8184 8185
	| ON	  { $$=new Item_string("ON",  2, system_charset_info); }
	| ALL	  { $$=new Item_string("ALL", 3, system_charset_info); }
unknown's avatar
unknown committed
8186
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
unknown's avatar
unknown committed
8187
	;
unknown's avatar
unknown committed
8188 8189


unknown's avatar
unknown committed
8190 8191 8192 8193 8194
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
8195 8196 8197 8198
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
8199
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOCK");
8200 8201 8202
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_LOCK_TABLES;
unknown's avatar
unknown committed
8203
	}
8204 8205
	table_lock_list
	{}
8206
	;
unknown's avatar
unknown committed
8207 8208 8209

table_or_tables:
	TABLE_SYM
unknown's avatar
unknown committed
8210
	| TABLES;
unknown's avatar
unknown committed
8211 8212 8213

table_lock_list:
	table_lock
unknown's avatar
unknown committed
8214
	| table_lock_list ',' table_lock;
unknown's avatar
unknown committed
8215 8216 8217

table_lock:
	table_ident opt_table_alias lock_option
8218
	{
unknown's avatar
unknown committed
8219
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
8220 8221
	   YYABORT;
	}
8222
        ;
unknown's avatar
unknown committed
8223 8224 8225

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
8226
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
unknown's avatar
unknown committed
8227
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
8228 8229
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
unknown's avatar
unknown committed
8230 8231

unlock:
8232 8233 8234 8235 8236 8237
	UNLOCK_SYM
	{
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
8238
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "UNLOCK");
8239 8240 8241 8242 8243 8244
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_UNLOCK_TABLES;
	}
	table_or_tables
	{}
8245
        ;
unknown's avatar
unknown committed
8246 8247


8248
/*
unknown's avatar
unknown committed
8249
** Handler: direct access to ISAM functions
8250 8251 8252 8253 8254
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
8255
	  LEX *lex= Lex;
8256 8257 8258 8259 8260
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
8261
	  lex->sql_command = SQLCOM_HA_OPEN;
unknown's avatar
unknown committed
8262
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
8263 8264
	    YYABORT;
	}
unknown's avatar
unknown committed
8265
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
8266
	{
8267
	  LEX *lex= Lex;
8268 8269 8270 8271 8272
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
8273
	  lex->sql_command = SQLCOM_HA_CLOSE;
unknown's avatar
unknown committed
8274
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
8275 8276
	    YYABORT;
	}
unknown's avatar
unknown committed
8277
	| HANDLER_SYM table_ident_nodb READ_SYM
8278
	{
unknown's avatar
unknown committed
8279
	  LEX *lex=Lex;
8280 8281 8282 8283 8284
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
unknown's avatar
unknown committed
8285 8286
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
8287
	  lex->current_select->select_limit= new Item_int((int32) 1);
8288
	  lex->current_select->offset_limit= 0;
unknown's avatar
unknown committed
8289
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
8290
	    YYABORT;
unknown's avatar
unknown committed
8291
        }
8292
        handler_read_or_scan where_clause opt_limit_clause {}
8293
        ;
8294

unknown's avatar
unknown committed
8295
handler_read_or_scan:
8296 8297
	handler_scan_function         { Lex->ident= null_lex_str; }
        | ident handler_rkey_function { Lex->ident= $1; }
8298
        ;
unknown's avatar
unknown committed
8299 8300 8301

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
8302 8303
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
unknown's avatar
unknown committed
8304 8305 8306 8307 8308 8309

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;  }
8310 8311
	| handler_rkey_mode
	{
unknown's avatar
unknown committed
8312 8313
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
unknown's avatar
unknown committed
8314
	  lex->ha_rkey_mode=$1;
unknown's avatar
unknown committed
8315
	  if (!(lex->insert_list = new List_item))
8316
	    YYABORT;
8317 8318
	} '(' values ')' { }
        ;
8319 8320

handler_rkey_mode:
unknown's avatar
unknown committed
8321 8322 8323 8324
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
8325 8326
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
8327

unknown's avatar
unknown committed
8328 8329 8330
/* GRANT / REVOKE */

revoke:
8331
	REVOKE clear_privileges revoke_command
8332 8333 8334 8335
	{}
        ;

revoke_command:
8336
	grant_privileges ON opt_table grant_ident FROM grant_list
8337
	{
8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365
          LEX *lex= Lex;
	  lex->sql_command= SQLCOM_REVOKE;
          lex->type= 0;
        }
        |
        grant_privileges ON FUNCTION_SYM grant_ident FROM grant_list
        {
          LEX *lex= Lex;
          if (lex->columns.elements)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
          }
	  lex->sql_command= SQLCOM_REVOKE;
          lex->type= TYPE_ENUM_FUNCTION;
          
        }
	|
        grant_privileges ON PROCEDURE grant_ident FROM grant_list
        {
          LEX *lex= Lex;
          if (lex->columns.elements)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
          }
	  lex->sql_command= SQLCOM_REVOKE;
          lex->type= TYPE_ENUM_PROCEDURE;
8366
        }
8367
	|
8368
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
8369 8370 8371
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
8372
	;
unknown's avatar
unknown committed
8373 8374

grant:
8375 8376 8377 8378 8379 8380
	GRANT clear_privileges grant_command
	{}
        ;

grant_command:
	grant_privileges ON opt_table grant_ident TO_SYM grant_list
8381
	require_clause grant_options
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 8407 8408 8409 8410 8411 8412 8413
	{
          LEX *lex= Lex;
          lex->sql_command= SQLCOM_GRANT;
          lex->type= 0;
        }
        |
	grant_privileges ON FUNCTION_SYM grant_ident TO_SYM grant_list
	require_clause grant_options
	{
          LEX *lex= Lex;
          if (lex->columns.elements)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
          }
          lex->sql_command= SQLCOM_GRANT;
          lex->type= TYPE_ENUM_FUNCTION;
        }
        |
	grant_privileges ON PROCEDURE grant_ident TO_SYM grant_list
	require_clause grant_options
	{
          LEX *lex= Lex;
          if (lex->columns.elements)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
          }
          lex->sql_command= SQLCOM_GRANT;
          lex->type= TYPE_ENUM_PROCEDURE;
        }
        ;
unknown's avatar
unknown committed
8414

8415 8416 8417 8418
opt_table:
	/* Empty */
	| TABLE_SYM ;
        
unknown's avatar
unknown committed
8419
grant_privileges:
8420 8421 8422 8423 8424 8425
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
8426
        ;
unknown's avatar
unknown committed
8427

unknown's avatar
unknown committed
8428 8429 8430 8431 8432
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

8433 8434 8435
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
unknown's avatar
unknown committed
8436

8437
object_privilege:
unknown's avatar
unknown committed
8438
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
8439 8440 8441
	| 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
8442 8443
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
unknown's avatar
unknown committed
8444
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
unknown's avatar
unknown committed
8445 8446 8447
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
unknown's avatar
unknown committed
8448
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
unknown's avatar
unknown committed
8449 8450 8451 8452
	| 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
8453 8454 8455 8456 8457
	| 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; }
8458 8459 8460 8461
	| 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; }
8462 8463
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
8464
	| CREATE USER { Lex->grant |= CREATE_USER_ACL; }
unknown's avatar
unknown committed
8465
	;
unknown's avatar
unknown committed
8466

unknown's avatar
unknown committed
8467

8468 8469
opt_and:
	/* empty */	{}
unknown's avatar
unknown committed
8470
	| AND_SYM	{}
8471 8472 8473 8474 8475 8476 8477 8478 8479
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
unknown's avatar
unknown committed
8480 8481 8482 8483
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
unknown's avatar
unknown committed
8484
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
unknown's avatar
unknown committed
8485 8486 8487 8488 8489 8490 8491 8492 8493
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
unknown's avatar
unknown committed
8494
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
unknown's avatar
unknown committed
8495 8496 8497 8498 8499 8500 8501 8502 8503
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
unknown's avatar
unknown committed
8504
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
unknown's avatar
unknown committed
8505 8506 8507 8508 8509
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
8510

8511
grant_ident:
unknown's avatar
unknown committed
8512 8513
	'*'
	  {
8514
	    LEX *lex= Lex;
unknown's avatar
unknown committed
8515 8516 8517
            THD *thd= lex->thd;
            if (thd->copy_db_to(&lex->current_select->db, NULL))
              YYABORT;
unknown's avatar
unknown committed
8518
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
8519 8520
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
8521
	    {
unknown's avatar
unknown committed
8522 8523
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
8524
	      YYABORT;
unknown's avatar
unknown committed
8525
	    }
unknown's avatar
unknown committed
8526 8527 8528
	  }
	| ident '.' '*'
	  {
8529
	    LEX *lex= Lex;
unknown's avatar
unknown committed
8530
	    lex->current_select->db = $1.str;
unknown's avatar
unknown committed
8531
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
8532 8533
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
8534
	    {
unknown's avatar
unknown committed
8535 8536
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
8537 8538 8539 8540 8541
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
8542
	    LEX *lex= Lex;
unknown's avatar
unknown committed
8543
	    lex->current_select->db = NULL;
unknown's avatar
unknown committed
8544 8545
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
unknown's avatar
unknown committed
8546
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
8547
	    {
unknown's avatar
unknown committed
8548 8549
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
8550 8551 8552 8553 8554
	      YYABORT;
	    }
	  }
	| table_ident
	  {
unknown's avatar
unknown committed
8555
	    LEX *lex=Lex;
unknown's avatar
unknown committed
8556
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
unknown's avatar
unknown committed
8557
	      YYABORT;
unknown's avatar
unknown committed
8558
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
8559
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
8560 8561
	  }
          ;
unknown's avatar
unknown committed
8562 8563 8564


user_list:
8565 8566 8567 8568 8569 8570 8571 8572 8573 8574
	user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;


grant_list:
8575
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
8576
	| grant_list ',' grant_user
8577 8578 8579 8580 8581
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
unknown's avatar
unknown committed
8582 8583 8584 8585 8586 8587 8588 8589


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
8590
             if (YYTHD->variables.old_passwords)
8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607
             {
               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
8608 8609 8610
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
8611
	  { $$= $1; $1->password= $5; }
unknown's avatar
unknown committed
8612
	| user
8613
	  { $$= $1; $1->password= null_lex_str; }
8614
        ;
unknown's avatar
unknown committed
8615 8616 8617


opt_column_list:
unknown's avatar
unknown committed
8618 8619 8620 8621 8622
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
unknown's avatar
unknown committed
8623
	| '(' column_list ')';
unknown's avatar
unknown committed
8624 8625 8626

column_list:
	column_list ',' column_list_id
unknown's avatar
unknown committed
8627
	| column_list_id;
unknown's avatar
unknown committed
8628 8629 8630 8631

column_list_id:
	ident
	{
unknown's avatar
unknown committed
8632
	  String *new_str = new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info);
unknown's avatar
unknown committed
8633 8634
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
unknown's avatar
unknown committed
8635
	  LEX *lex=Lex;
unknown's avatar
unknown committed
8636 8637
	  while ((point=iter++))
	  {
8638 8639
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
unknown's avatar
unknown committed
8640 8641
		break;
	  }
unknown's avatar
unknown committed
8642
	  lex->grant_tot_col|= lex->which_columns;
unknown's avatar
unknown committed
8643
	  if (point)
unknown's avatar
unknown committed
8644
	    point->rights |= lex->which_columns;
unknown's avatar
unknown committed
8645
	  else
unknown's avatar
unknown committed
8646
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
8647 8648
	}
        ;
unknown's avatar
unknown committed
8649

unknown's avatar
unknown committed
8650 8651

require_clause: /* empty */
8652
        | REQUIRE_SYM require_list
8653 8654 8655
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
8656
        | REQUIRE_SYM SSL_SYM
8657 8658 8659
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
8660
        | REQUIRE_SYM X509_SYM
8661 8662 8663 8664 8665 8666 8667
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
8668
          ;
unknown's avatar
unknown committed
8669

unknown's avatar
unknown committed
8670
grant_options:
unknown's avatar
unknown committed
8671
	/* empty */ {}
unknown's avatar
unknown committed
8672
	| WITH grant_option_list;
unknown's avatar
unknown committed
8673

unknown's avatar
unknown committed
8674 8675
grant_option_list:
	grant_option_list grant_option {}
8676 8677
	| grant_option {}
        ;
unknown's avatar
unknown committed
8678 8679 8680

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
unknown's avatar
unknown committed
8681
        | MAX_QUERIES_PER_HOUR ulong_num
unknown's avatar
unknown committed
8682
        {
unknown's avatar
unknown committed
8683 8684 8685
	  LEX *lex=Lex;
	  lex->mqh.questions=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
8686
	}
unknown's avatar
unknown committed
8687
        | MAX_UPDATES_PER_HOUR ulong_num
8688
        {
unknown's avatar
unknown committed
8689 8690 8691
	  LEX *lex=Lex;
	  lex->mqh.updates=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
8692
	}
unknown's avatar
unknown committed
8693
        | MAX_CONNECTIONS_PER_HOUR ulong_num
8694
        {
unknown's avatar
unknown committed
8695 8696 8697
	  LEX *lex=Lex;
	  lex->mqh.conn_per_hour= $2;
	  lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
8698
	}
unknown's avatar
unknown committed
8699
        | MAX_USER_CONNECTIONS_SYM ulong_num
8700
        {
unknown's avatar
unknown committed
8701 8702 8703
	  LEX *lex=Lex;
          lex->mqh.user_conn= $2;
          lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
8704 8705
	}
        ;
8706

unknown's avatar
unknown committed
8707
begin:
unknown's avatar
unknown committed
8708 8709 8710 8711 8712 8713 8714
	BEGIN_SYM  
        {
	  LEX *lex=Lex;
          lex->sql_command = SQLCOM_BEGIN;
          lex->start_transaction_opt= 0;
        }
        opt_work {}
8715
	;
unknown's avatar
unknown committed
8716 8717 8718

opt_work:
	/* empty */ {}
unknown's avatar
unknown committed
8719
	| WORK_SYM  {}
8720
        ;
unknown's avatar
unknown committed
8721

unknown's avatar
unknown committed
8722
opt_chain:
unknown's avatar
unknown committed
8723
	/* empty */ { $$= (YYTHD->variables.completion_type == 1); }
unknown's avatar
unknown committed
8724 8725 8726 8727 8728
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
unknown's avatar
unknown committed
8729
	/* empty */ { $$= (YYTHD->variables.completion_type == 2); }
unknown's avatar
unknown committed
8730 8731 8732 8733 8734 8735 8736 8737 8738
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

unknown's avatar
unknown committed
8739
commit:
unknown's avatar
unknown committed
8740
	COMMIT_SYM opt_work opt_chain opt_release
unknown's avatar
unknown committed
8741
	{
unknown's avatar
unknown committed
8742 8743 8744 8745
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_COMMIT;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
unknown's avatar
unknown committed
8746 8747
	}
	;
unknown's avatar
unknown committed
8748 8749

rollback:
unknown's avatar
unknown committed
8750
	ROLLBACK_SYM opt_work opt_chain opt_release
8751
	{
unknown's avatar
unknown committed
8752 8753 8754 8755
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
8756
	}
unknown's avatar
unknown committed
8757 8758
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
8759
	{
unknown's avatar
unknown committed
8760 8761 8762
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK_TO_SAVEPOINT;
	  lex->ident= $5;
unknown's avatar
unknown committed
8763 8764 8765
	}
	;

8766 8767 8768
savepoint:
	SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
8769 8770 8771
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SAVEPOINT;
	  lex->ident= $2;
unknown's avatar
unknown committed
8772 8773 8774 8775 8776 8777
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
8778 8779 8780
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RELEASE_SAVEPOINT;
	  lex->ident= $3;
unknown's avatar
unknown committed
8781 8782
	}
	;
unknown's avatar
unknown committed
8783
  
8784
/*
unknown's avatar
unknown committed
8785
   UNIONS : glue selects together
8786 8787 8788
*/


8789
union_clause:
8790
	/* empty */ {}
unknown's avatar
unknown committed
8791 8792
	| union_list
	;
8793 8794

union_list:
8795
	UNION_SYM union_option
8796 8797 8798 8799 8800
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
unknown's avatar
unknown committed
8801
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
8802
	    YYABORT;
8803
	  }
8804
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
8805
	  {
8806
            yyerror(ER(ER_SYNTAX_ERROR));
8807 8808
	    YYABORT;
	  }
8809 8810
          /* This counter shouldn't be incremented for UNION parts */
          Lex->nest_level--;
8811
	  if (mysql_new_select(lex, 0))
8812
	    YYABORT;
unknown's avatar
unknown committed
8813
          mysql_init_select(lex);
8814
	  lex->current_select->linkage=UNION_TYPE;
8815 8816 8817
          if ($2) /* UNION DISTINCT - remember position */
            lex->current_select->master_unit()->union_distinct=
                                                      lex->current_select;
unknown's avatar
unknown committed
8818
	}
unknown's avatar
unknown committed
8819 8820
	select_init
        {
8821 8822 8823 8824
          /*
	    Remove from the name resolution context stack the context of the
            last select in the union.
	  */
unknown's avatar
unknown committed
8825 8826
          Lex->pop_context();
        }
8827
	;
8828 8829

union_opt:
unknown's avatar
unknown committed
8830 8831 8832
	/* Empty */ { $$= 0; }
	| union_list { $$= 1; }
	| union_order_or_limit { $$= 1; }
unknown's avatar
unknown committed
8833
	;
8834

unknown's avatar
unknown committed
8835
union_order_or_limit:
8836
	  {
unknown's avatar
unknown committed
8837
	    THD *thd= YYTHD;
8838
	    LEX *lex= thd->lex;
unknown's avatar
unknown committed
8839
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
unknown's avatar
unknown committed
8840
	    SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
8841
	    SELECT_LEX_UNIT *unit= sel->master_unit();
unknown's avatar
unknown committed
8842
	    SELECT_LEX *fake= unit->fake_select_lex;
unknown's avatar
unknown committed
8843 8844 8845 8846 8847 8848
	    if (fake)
	    {
	      unit->global_parameters= fake;
	      fake->no_table_names_allowed= 1;
	      lex->current_select= fake;
	    }
unknown's avatar
unknown committed
8849
	    thd->where= "global ORDER clause";
8850
	  }
8851
	order_or_limit
unknown's avatar
unknown committed
8852 8853
          {
	    THD *thd= YYTHD;
8854
	    thd->lex->current_select->no_table_names_allowed= 0;
unknown's avatar
unknown committed
8855 8856
	    thd->where= "";
          }
8857 8858 8859
	;

order_or_limit:
8860 8861
	order_clause opt_limit_clause_init
	| limit_clause
8862
	;
unknown's avatar
unknown committed
8863 8864

union_option:
8865 8866 8867 8868
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
8869

unknown's avatar
unknown committed
8870 8871
singlerow_subselect:
	subselect_start singlerow_subselect_init
unknown's avatar
unknown committed
8872 8873 8874 8875
	subselect_end
	{
	  $$= $2;
	};
8876

unknown's avatar
unknown committed
8877
singlerow_subselect_init:
8878
	select_init2
unknown's avatar
unknown committed
8879
	{
8880 8881
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
unknown's avatar
unknown committed
8882
	};
unknown's avatar
unknown committed
8883 8884

exists_subselect:
unknown's avatar
unknown committed
8885 8886 8887 8888 8889
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
unknown's avatar
unknown committed
8890 8891

exists_subselect_init:
8892
	select_init2
unknown's avatar
unknown committed
8893
	{
8894 8895
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
unknown's avatar
unknown committed
8896
	};
8897

unknown's avatar
unknown committed
8898 8899 8900 8901 8902 8903 8904 8905
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
8906
  select_init2
unknown's avatar
unknown committed
8907
  {
unknown's avatar
unknown committed
8908
    $$= Lex->current_select->master_unit()->first_select();
unknown's avatar
unknown committed
8909 8910
  };

8911
subselect_start:
8912
	'(' SELECT_SYM
unknown's avatar
unknown committed
8913
	{
8914
	  LEX *lex=Lex;
8915 8916
          if (lex->sql_command == (int)SQLCOM_HA_READ ||
              lex->sql_command == (int)SQLCOM_KILL)
8917 8918
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
8919 8920
	    YYABORT;
	  }
unknown's avatar
unknown committed
8921 8922 8923
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
8924 8925

subselect_end:
unknown's avatar
unknown committed
8926 8927 8928
	')'
	{
	  LEX *lex=Lex;
unknown's avatar
unknown committed
8929
          lex->pop_context();
8930
          SELECT_LEX *child= lex->current_select;
8931
	  lex->current_select = lex->current_select->return_after_parsing();
unknown's avatar
unknown committed
8932
          lex->nest_level--;
8933
          lex->current_select->n_child_sum_items += child->n_sum_items;
unknown's avatar
unknown committed
8934
	};
unknown's avatar
unknown committed
8935

8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963
/**************************************************************************

 CREATE VIEW | TRIGGER | PROCEDURE statements.

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

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

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

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

 DEFINER clause support.

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

8964
definer:
8965
	/* empty */
8966
	{
8967 8968 8969 8970 8971 8972 8973 8974
          /*
            We have to distinguish missing DEFINER-clause from case when
            CURRENT_USER specified as definer explicitly in order to properly
            handle CREATE TRIGGER statements which come to replication thread
            from older master servers (i.e. to create non-suid trigger in this
            case).
           */
          YYTHD->lex->definer= 0;
8975
	}
8976
	| DEFINER_SYM EQ CURRENT_USER optional_braces
8977
	{
8978 8979
          if (! (YYTHD->lex->definer= create_default_definer(YYTHD)))
            YYABORT;
8980 8981 8982
	}
	| DEFINER_SYM EQ ident_or_text '@' ident_or_text
	{
8983 8984
          if (!(YYTHD->lex->definer= create_definer(YYTHD, &$3, &$5)))
            YYABORT;
8985 8986 8987 8988 8989
	}
	;

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

8990
 CREATE VIEW statement parts.
8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025

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

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

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

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

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

view_suid:
	/* empty */
9026
	{ Lex->create_view_suid= VIEW_SUID_DEFAULT; }
9027
	| SQL_SYM SECURITY_SYM DEFINER_SYM
9028
	{ Lex->create_view_suid= VIEW_SUID_DEFINER; }
9029
	| SQL_SYM SECURITY_SYM INVOKER_SYM
9030
	{ Lex->create_view_suid= VIEW_SUID_INVOKER; }
9031 9032 9033 9034 9035 9036 9037 9038 9039
	;

view_tail:
	view_suid VIEW_SYM table_ident
	{
	  THD *thd= YYTHD;
	  LEX *lex= thd->lex;
	  lex->sql_command= SQLCOM_CREATE_VIEW;
	  /* first table in list is target VIEW name */
9040
	  if (!lex->select_lex.add_table_to_list(thd, $3, NULL, TL_OPTION_UPDATING))
9041 9042 9043 9044 9045 9046 9047 9048 9049
	    YYABORT;
	}
	view_list_opt AS view_select view_check_option
	{}
	;

view_list_opt:
	/* empty */
	{}
9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065
	| '(' 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)));
	  }
	;

9066 9067 9068
view_select:
	SELECT_SYM remember_name select_init2
	{
9069 9070 9071 9072 9073 9074
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          char *stmt_beg= (lex->sphead ?
                           (char *)lex->sphead->m_tmp_query :
                           thd->query);
	  lex->create_view_select_start= $2 - stmt_beg;
9075 9076 9077
	}
	| '(' remember_name select_paren ')' union_opt
	{
9078 9079 9080 9081 9082 9083
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          char *stmt_beg= (lex->sphead ?
                           (char *)lex->sphead->m_tmp_query :
                           thd->query);
	  lex->create_view_select_start= $2 - stmt_beg;
9084
	}
9085 9086
	;

9087
view_check_option:
9088
	/* empty */
9089 9090 9091 9092 9093 9094 9095
	{ 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; }
9096
	;
9097

9098 9099 9100 9101 9102 9103 9104
/**************************************************************************

 CREATE TRIGGER statement parts.

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

trigger_tail:
9105
	TRIGGER_SYM remember_name sp_name trg_action_time trg_event
9106
	ON remember_name table_ident FOR_SYM remember_name EACH_SYM ROW_SYM
9107 9108 9109 9110 9111
	{
	  LEX *lex= Lex;
	  sp_head *sp;
	 
	  if (lex->sphead)
9112
	  {
9113 9114
	    my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "TRIGGER");
	    YYABORT;
9115
	  }
9116 9117 9118 9119 9120
	
	  if (!(sp= new sp_head()))
	    YYABORT;
	  sp->reset_thd_mem_root(YYTHD);
	  sp->init(lex);
9121
          sp->init_sp_name(YYTHD, $3);
9122
	
9123
	  lex->stmt_definition_begin= $2;
9124
          lex->ident.str= $7;
9125
          lex->ident.length= $10 - $7;
9126

9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140
	  sp->m_type= TYPE_ENUM_TRIGGER;
	  lex->sphead= sp;
	  lex->spname= $3;
	  /*
	    We have to turn of CLIENT_MULTI_QUERIES while parsing a
	    stored procedure, otherwise yylex will chop it into pieces
	    at each ';'.
	  */
	  sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
	  YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
	  
	  bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	  lex->sphead->m_chistics= &lex->sp_chistics;
	  lex->sphead->m_body_begin= lex->ptr;
9141 9142
          while (my_isspace(system_charset_info, lex->sphead->m_body_begin[0]))
            ++lex->sphead->m_body_begin;
9143 9144 9145 9146 9147 9148 9149
	}
	sp_proc_stmt
	{
	  LEX *lex= Lex;
	  sp_head *sp= lex->sphead;
	  
	  lex->sql_command= SQLCOM_CREATE_TRIGGER;
9150
	  sp->init_strings(YYTHD, lex);
9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163
	  /* Restore flag if it was cleared above */
	  if (sp->m_old_cmq)
	    YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
	  sp->restore_thd_mem_root(YYTHD);
	
	  if (sp->is_not_allowed_in_function("trigger"))
	      YYABORT;
	
	  /*
	    We have to do it after parsing trigger body, because some of
	    sp_proc_stmt alternatives are not saving/restoring LEX, so
	    lex->query_tables can be wiped out.
	  */
9164
	  if (!lex->select_lex.add_table_to_list(YYTHD, $8,
9165 9166
	                                         (LEX_STRING*) 0,
	                                         TL_OPTION_UPDATING,
9167
                                                 TL_IGNORE))
9168 9169
	    YYABORT;
	}
9170 9171
	;

9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197
/**************************************************************************

 CREATE FUNCTION | PROCEDURE statements parts.

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

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

	  if (lex->sphead)
	  {
	    my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "PROCEDURE");
	    YYABORT;
	  }
9198

9199
	  lex->stmt_definition_begin= $2;
9200

9201 9202 9203 9204
	  /* Order is important here: new - reset - init */
	  sp= new sp_head();
	  sp->reset_thd_mem_root(YYTHD);
	  sp->init(lex);
9205
          sp->init_sp_name(YYTHD, $3);
9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242

	  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;

9243
	  sp->init_strings(YYTHD, lex);
9244 9245 9246 9247 9248 9249 9250 9251
	  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);
	}
	;

9252
/*************************************************************************/
9253

9254 9255
xa: XA_SYM begin_or_start xid opt_join_or_resume
      {
unknown's avatar
unknown committed
9256
        Lex->sql_command = SQLCOM_XA_START;
9257
      }
9258
    | XA_SYM END xid opt_suspend
9259
      {
unknown's avatar
unknown committed
9260
        Lex->sql_command = SQLCOM_XA_END;
9261 9262 9263
      }
    | XA_SYM PREPARE_SYM xid
      {
unknown's avatar
unknown committed
9264
        Lex->sql_command = SQLCOM_XA_PREPARE;
9265 9266 9267
      }
    | XA_SYM COMMIT_SYM xid opt_one_phase
      {
unknown's avatar
unknown committed
9268
        Lex->sql_command = SQLCOM_XA_COMMIT;
9269 9270 9271
      }
    | XA_SYM ROLLBACK_SYM xid
      {
unknown's avatar
unknown committed
9272
        Lex->sql_command = SQLCOM_XA_ROLLBACK;
9273 9274 9275
      }
    | XA_SYM RECOVER_SYM
      {
unknown's avatar
unknown committed
9276
        Lex->sql_command = SQLCOM_XA_RECOVER;
9277 9278 9279
      }
    ;

unknown's avatar
unknown committed
9280 9281
xid: text_string
     {
unknown's avatar
unknown committed
9282
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE);
unknown's avatar
unknown committed
9283 9284 9285 9286 9287 9288
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set(1L, $1->ptr(), $1->length(), 0, 0);
     }
     | text_string ',' text_string
     {
unknown's avatar
unknown committed
9289
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
unknown's avatar
unknown committed
9290 9291 9292 9293 9294 9295
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set(1L, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     | text_string ',' text_string ',' ulong_num
     {
unknown's avatar
unknown committed
9296
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
unknown's avatar
unknown committed
9297 9298 9299 9300 9301
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set($5, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     ;
9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317

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

9318
opt_suspend:
9319 9320
    /* nothing */           { Lex->xa_opt=XA_NONE;        }
    | SUSPEND_SYM           { Lex->xa_opt=XA_SUSPEND;     }
9321 9322 9323 9324 9325
      opt_migrate
    ;

opt_migrate:
    /* nothing */           { }
9326 9327 9328 9329
    | FOR_SYM MIGRATE_SYM   { Lex->xa_opt=XA_FOR_MIGRATE; }
    ;