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

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

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

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

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

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

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

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

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

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

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

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

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

%}
%union {
  int  num;
  ulong ulong_num;
unknown's avatar
unknown committed
75
  ulonglong ulonglong_number;
unknown's avatar
unknown committed
76
  longlong longlong_number;
unknown's avatar
unknown committed
77 78 79 80 81 82
  LEX_STRING lex_str;
  LEX_STRING *lex_str_ptr;
  LEX_SYMBOL symbol;
  Table_ident *table;
  char *simple_string;
  Item *item;
83
  Item_num *item_num;
unknown's avatar
unknown committed
84 85
  List<Item> *item_list;
  List<String> *string_list;
unknown's avatar
unknown committed
86 87 88 89 90
  String *string;
  key_part_spec *key_part;
  TABLE_LIST *table_list;
  udf_func *udf;
  LEX_USER *lex_user;
unknown's avatar
unknown committed
91
  struct sys_var_with_base variable;
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 178 179 180 181 182 183 184
%token  CHANGE
%token  CHANGED
%token  CHARSET
%token  CHAR_SYM
%token  CHECKSUM_SYM
%token  CHECK_SYM
%token  CIPHER_SYM
%token  CLIENT_SYM
%token  CLOSE_SYM
%token  COALESCE
%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
185
%token  COMPACT_SYM
186 187 188 189
%token  COMPRESSED_SYM
%token  CONCAT
%token  CONCAT_WS
%token  CONCURRENT
190
%token  CONDITION_SYM
191 192 193
%token  CONNECTION_SYM
%token  CONSISTENT_SYM
%token  CONSTRAINT
194 195
%token  CONTAINS_SYM
%token  CONTINUE_SYM
196 197 198 199 200 201 202
%token  CONVERT_SYM
%token  CONVERT_TZ_SYM
%token  COUNT_SYM
%token  CREATE
%token  CROSS
%token  CUBE_SYM
%token  CURDATE
203
%token  CURRENT_USER
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
%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
219
%token  DECIMAL_NUM
220
%token  DECIMAL_SYM
221
%token  DECLARE_SYM
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
%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
244
%token  DUPLICATE_SYM
245
%token  DYNAMIC_SYM
246
%token  EACH_SYM
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
%token  ELSEIF_SYM
%token  ELT_FUNC
%token  ENABLE_SYM
%token  ENCLOSED
%token  ENCODE_SYM
%token  ENCRYPT
%token  END
%token  ENGINES_SYM
%token  ENGINE_SYM
%token  ENUM
%token  EQ
%token  EQUAL_SYM
%token  ERRORS
%token  ESCAPED
%token  ESCAPE_SYM
%token  EVENTS_SYM
%token  EXECUTE_SYM
%token  EXISTS
265
%token  EXIT_SYM
266 267 268 269 270 271
%token  EXPANSION_SYM
%token  EXPORT_SET
%token  EXTENDED_SYM
%token  EXTRACT_SYM
%token  FALSE_SYM
%token  FAST_SYM
272
%token  FETCH_SYM
273 274 275 276 277 278 279 280 281 282 283
%token  FIELD_FUNC
%token  FILE_SYM
%token  FIRST_SYM
%token  FIXED_SYM
%token  FLOAT_NUM
%token  FLOAT_SYM
%token  FLUSH_SYM
%token  FORCE_SYM
%token  FOREIGN
%token  FORMAT_SYM
%token  FOR_SYM
284
%token  FOUND_SYM
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
%token  FRAC_SECOND_SYM
%token  FROM
%token  FROM_UNIXTIME
%token  FULL
%token  FULLTEXT_SYM
%token  FUNCTION_SYM
%token  FUNC_ARG0
%token  FUNC_ARG1
%token  FUNC_ARG2
%token  FUNC_ARG3
%token  GE
%token  GEOMCOLLFROMTEXT
%token  GEOMETRYCOLLECTION
%token  GEOMETRY_SYM
%token  GEOMFROMTEXT
%token  GEOMFROMWKB
%token  GET_FORMAT
%token  GLOBAL_SYM
%token  GOTO_SYM
%token  GRANT
%token  GRANTS
%token  GREATEST_SYM
%token  GROUP
%token  GROUP_CONCAT_SYM
%token  GROUP_UNIQUE_USERS
%token  GT_SYM
%token  HANDLER_SYM
%token  HASH_SYM
%token  HAVING
%token  HELP_SYM
%token  HEX_NUM
%token  HIGH_PRIORITY
%token  HOSTS_SYM
%token  HOUR_MICROSECOND_SYM
%token  HOUR_MINUTE_SYM
%token  HOUR_SECOND_SYM
%token  HOUR_SYM
%token  IDENT
%token  IDENTIFIED_SYM
%token  IDENT_QUOTED
%token  IF
%token  IGNORE_SYM
%token  IMPORT
%token  INDEXES
%token  INDEX_SYM
%token  INFILE
%token  INNER_SYM
%token  INNOBASE_SYM
333
%token  INOUT_SYM
334 335 336 337 338 339
%token  INSENSITIVE_SYM
%token  INSERT
%token  INSERT_METHOD
%token  INTERVAL_SYM
%token  INTO
%token  INT_SYM
340
%token  INVOKER_SYM
341 342 343 344 345 346 347 348 349 350
%token  IN_SYM
%token  IS
%token  ISOLATION
%token  ISSUER_SYM
%token  ITERATE_SYM
%token  JOIN_SYM
%token  KEYS
%token  KEY_SYM
%token  KILL_SYM
%token  LABEL_SYM
351
%token  LANGUAGE_SYM
352 353 354 355 356 357 358 359
%token  LAST_INSERT_ID
%token  LAST_SYM
%token  LE
%token  LEADING
%token  LEAST_SYM
%token  LEAVES
%token  LEAVE_SYM
%token  LEFT
360
%token  LESS_SYM
361 362 363 364
%token  LEVEL_SYM
%token  LEX_HOSTNAME
%token  LIKE
%token  LIMIT
365
%token  LINEAR_SYM
366 367 368
%token  LINEFROMTEXT
%token  LINES
%token  LINESTRING
369
%token  LIST_SYM
370 371 372
%token  LOAD
%token  LOCAL_SYM
%token  LOCATE
373
%token  LOCATOR_SYM
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 405 406 407
%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
408
%token  MAX_USER_CONNECTIONS_SYM
409
%token  MAX_VALUE_SYM
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
%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
433
%token  MUTEX_SYM
434 435 436 437 438 439
%token  NAMES_SYM
%token  NAME_SYM
%token  NATIONAL_SYM
%token  NATURAL
%token  NCHAR_STRING
%token  NCHAR_SYM
unknown's avatar
unknown committed
440
%token  NDBCLUSTER_SYM
441 442 443
%token  NE
%token  NEW_SYM
%token  NEXT_SYM
444
%token  NODEGROUP_SYM
445 446 447 448 449 450 451 452 453
%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
454
%token  NVARCHAR_SYM
455 456 457
%token  OFFSET_SYM
%token  OLD_PASSWORD
%token  ON
458
%token  ONE_SHOT_SYM
459 460 461 462 463 464 465 466 467 468 469
%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
470
%token  OUT_SYM
471 472
%token  PACK_KEYS_SYM
%token  PARTIAL
473 474
%token  PARTITION_SYM
%token  PARTITIONS_SYM
475
%token  PASSWORD
476
%token  PARAM_MARKER
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
%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
501
%token  RANGE_SYM
502 503 504 505
%token  READS_SYM
%token  READ_SYM
%token  REAL
%token  RECOVER_SYM
unknown's avatar
unknown committed
506
%token  REDUNDANT_SYM
507 508 509 510 511
%token  REFERENCES
%token  REGEXP
%token  RELAY_LOG_FILE_SYM
%token  RELAY_LOG_POS_SYM
%token  RELAY_THREAD
unknown's avatar
unknown committed
512
%token  RELEASE_SYM
513 514
%token  RELOAD
%token  RENAME
unknown's avatar
unknown committed
515
%token  REORGANISE_SYM
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
%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
534
%token  ROUTINE_SYM
535 536 537 538 539 540 541 542
%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
543
%token  SECURITY_SYM
544 545
%token  SELECT_SYM
%token  SENSITIVE_SYM
546
%token  SEPARATOR_SYM
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
%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
564 565 566 567
%token  SPECIFIC_SYM
%token  SQLEXCEPTION_SYM
%token  SQLSTATE_SYM
%token  SQLWARNING_SYM
568 569 570 571 572 573 574 575
%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
576
%token  SSL_SYM
577 578 579 580
%token  STARTING
%token  START_SYM
%token  STATUS_SYM
%token  STD_SYM
581
%token  STDDEV_SAMP_SYM
582 583 584 585 586 587
%token  STOP_SYM
%token  STORAGE_SYM
%token  STRAIGHT_JOIN
%token  STRING_SYM
%token  SUBDATE_SYM
%token  SUBJECT_SYM
588 589
%token  SUBPARTITION_SYM
%token  SUBPARTITIONS_SYM
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
%token  SUBSTRING
%token  SUBSTRING_INDEX
%token  SUM_SYM
%token  SUPER_SYM
%token  SUSPEND_SYM
%token  TABLES
%token  TABLESPACE
%token  TABLE_SYM
%token  TEMPORARY
%token  TEMPTABLE_SYM
%token  TERMINATED
%token  TEXT_STRING
%token  TEXT_SYM
%token  TIMESTAMP
%token  TIMESTAMP_ADD
%token  TIMESTAMP_DIFF
%token  TIME_SYM
%token  TINYBLOB
%token  TINYINT
%token  TINYTEXT
610
%token  THAN_SYM
611 612 613
%token  TO_SYM
%token  TRAILING
%token  TRANSACTION_SYM
614
%token  TRIGGER_SYM
615
%token  TRIGGERS_SYM
616 617 618
%token  TRIM
%token  TRUE_SYM
%token  TRUNCATE_SYM
unknown's avatar
unknown committed
619
%token  TYPES_SYM
620 621 622 623 624 625 626
%token  TYPE_SYM
%token  UDF_RETURNS_SYM
%token  UDF_SONAME_SYM
%token  ULONGLONG_NUM
%token  UNCOMMITTED_SYM
%token  UNDEFINED_SYM
%token  UNDERSCORE_CHARSET
627
%token  UNDO_SYM
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
%token  UNICODE_SYM
%token  UNION_SYM
%token  UNIQUE_SYM
%token  UNIQUE_USERS
%token  UNIX_TIMESTAMP
%token  UNKNOWN_SYM
%token  UNLOCK_SYM
%token  UNSIGNED
%token  UNTIL_SYM
%token  UPDATE_SYM
%token  USAGE
%token  USER
%token  USE_FRM
%token  USE_SYM
%token  USING
%token  UTC_DATE_SYM
%token  UTC_TIMESTAMP_SYM
%token  UTC_TIME_SYM
646
%token  VAR_SAMP_SYM
647 648 649 650 651 652 653 654 655 656 657 658
%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
659
%token  WHILE_SYM
660 661 662 663 664 665 666 667 668 669
%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
670

unknown's avatar
unknown committed
671
%left   SET_VAR
672 673
%left	OR_OR_SYM OR_SYM OR2_SYM XOR
%left	AND_SYM AND_AND_SYM
unknown's avatar
unknown committed
674 675 676 677 678 679
%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	'-' '+'
680
%left	'*' '/' '%' DIV_SYM MOD_SYM
unknown's avatar
unknown committed
681
%left   '^'
unknown's avatar
unknown committed
682
%left	NEG '~'
683
%right	NOT_SYM NOT2_SYM
unknown's avatar
unknown committed
684
%right	BINARY COLLATE_SYM
685

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

%type <lex_str_ptr>
	opt_table_alias

%type <table>
697
	table_ident table_ident_nodb references xid
unknown's avatar
unknown committed
698 699

%type <simple_string>
700
	remember_name remember_end opt_ident opt_db text_or_password
701
	opt_constraint constraint ident_or_empty
unknown's avatar
unknown committed
702 703

%type <string>
704
	text_string opt_gconcat_separator
unknown's avatar
unknown committed
705 706

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

%type <ulong_num>
unknown's avatar
unknown committed
716
	ulong_num raid_types merge_insert_types
unknown's avatar
unknown committed
717

unknown's avatar
unknown committed
718 719
%type <ulonglong_number>
	ulonglong_num
unknown's avatar
unknown committed
720

unknown's avatar
unknown committed
721 722 723
%type <longlong_number>
        part_bit_expr

unknown's avatar
unknown committed
724 725 726
%type <lock_type>
	replace_lock_option opt_low_priority insert_lock_option load_data_lock

unknown's avatar
unknown committed
727
%type <item>
unknown's avatar
unknown committed
728
	literal text_literal insert_ident order_ident
unknown's avatar
unknown committed
729
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
730 731 732
	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
733
	using_list expr_or_default set_expr_or_default interval_expr
unknown's avatar
unknown committed
734
	param_marker singlerow_subselect singlerow_subselect_init
unknown's avatar
unknown committed
735
	exists_subselect exists_subselect_init geometry_function
736
	signed_literal now_or_signed_literal opt_escape
737 738
	sp_opt_default
	simple_ident_nospvar simple_ident_q
739
        field_or_var limit_option
unknown's avatar
unknown committed
740
        part_func_expr
741 742 743

%type <item_num>
	NUM_literal
unknown's avatar
unknown committed
744 745

%type <item_list>
746 747
	expr_list udf_expr_list udf_expr_list2 when_list
	ident_list ident_list_arg
unknown's avatar
unknown committed
748 749

%type <key_type>
750
	key_type opt_unique_or_fulltext constraint_key_type
unknown's avatar
unknown committed
751

unknown's avatar
unknown committed
752 753 754
%type <key_alg>
	key_alg opt_btree_or_rtree

unknown's avatar
unknown committed
755 756 757 758 759 760 761
%type <string_list>
	key_usage_list

%type <key_part>
	key_part

%type <table_list>
762
	join_table_list  join_table
763
        table_factor table_ref
unknown's avatar
unknown committed
764
        select_derived derived_table_list
unknown's avatar
unknown committed
765

766
%type <date_time_type> date_time_type;
unknown's avatar
unknown committed
767 768
%type <interval> interval

769 770
%type <interval_time_st> interval_time_st

unknown's avatar
unknown committed
771
%type <db_type> storage_engines
unknown's avatar
unknown committed
772 773 774

%type <row_type> row_types

unknown's avatar
unknown committed
775
%type <tx_isolation> isolation_types
unknown's avatar
unknown committed
776

unknown's avatar
unknown committed
777 778
%type <ha_rkey_mode> handler_rkey_mode

779
%type <cast_type> cast_type
unknown's avatar
unknown committed
780

unknown's avatar
unknown committed
781 782
%type <udf_type> udf_func_type

783
%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword keyword_sp
unknown's avatar
unknown committed
784 785 786

%type <lex_user> user grant_user

787
%type <charset>
788
	opt_collate
789 790
	charset_name
	charset_name_or_default
unknown's avatar
unknown committed
791 792
	old_or_new_charset_name
	old_or_new_charset_name_or_default
793 794
	collation_name
	collation_name_or_default
795

unknown's avatar
unknown committed
796 797
%type <variable> internal_variable_name

unknown's avatar
unknown committed
798
%type <select_lex> in_subselect in_subselect_init
unknown's avatar
unknown committed
799
	get_select_lex
unknown's avatar
unknown committed
800

unknown's avatar
unknown committed
801 802
%type <boolfunc2creator> comp_op

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

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


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

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

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

unknown's avatar
unknown committed
930
deallocate:
931
        deallocate_or_drop PREPARE_SYM ident
unknown's avatar
unknown committed
932 933
        {
          THD *thd=YYTHD;
934
          LEX *lex= thd->lex;
935
          if (thd->command == COM_STMT_PREPARE)
unknown's avatar
unknown committed
936 937 938 939
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
940 941 942 943 944
          if (lex->sphead)
          {
            my_error(ER_SP_BADSTATEMENT, MYF(0), "DEALLOCATE");
            YYABORT;
          }
945
          lex->sql_command= SQLCOM_DEALLOCATE_PREPARE;
unknown's avatar
unknown committed
946 947 948
          lex->prepared_stmt_name= $3;
        };

949 950 951 952 953 954
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


unknown's avatar
unknown committed
955
prepare:
956
        PREPARE_SYM ident FROM prepare_src
unknown's avatar
unknown committed
957 958
        {
          THD *thd=YYTHD;
959
          LEX *lex= thd->lex;
960
          if (thd->command == COM_STMT_PREPARE)
unknown's avatar
unknown committed
961 962 963 964
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
965 966 967 968 969
          if (lex->sphead)
          {
            my_error(ER_SP_BADSTATEMENT, MYF(0), "PREPARE");
            YYABORT;
          }
970
          lex->sql_command= SQLCOM_PREPARE;
unknown's avatar
unknown committed
971 972 973
          lex->prepared_stmt_name= $2;
        };

974 975 976 977 978 979
prepare_src:
        TEXT_STRING_sys
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $1;
unknown's avatar
unknown committed
980
          lex->prepared_stmt_code_is_varref= FALSE;
981 982 983 984 985 986
        }
        | '@' ident_or_text
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $2;
unknown's avatar
unknown committed
987
          lex->prepared_stmt_code_is_varref= TRUE;
988
        };
989

unknown's avatar
unknown committed
990 991 992 993
execute:
        EXECUTE_SYM ident
        {
          THD *thd=YYTHD;
994
          LEX *lex= thd->lex;
995
          if (thd->command == COM_STMT_PREPARE)
unknown's avatar
unknown committed
996 997 998 999
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
1000 1001 1002 1003 1004
          if (lex->sphead)
          {
            my_error(ER_SP_BADSTATEMENT, MYF(0), "EXECUTE");
            YYABORT;
          }
1005
          lex->sql_command= SQLCOM_EXECUTE;
unknown's avatar
unknown committed
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
          lex->prepared_stmt_name= $2;
        }
        execute_using
        {}
        ;

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
1018 1019
        execute_var_list ',' execute_var_ident
        | execute_var_ident
unknown's avatar
unknown committed
1020 1021 1022 1023 1024 1025 1026 1027
        ;

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;
1028
        }
unknown's avatar
unknown committed
1029 1030
        ;

unknown's avatar
unknown committed
1031 1032
/* help */

1033
help:
1034
       HELP_SYM ident_or_text
unknown's avatar
unknown committed
1035
       {
1036 1037 1038
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
	  lex->help_arg= $2.str;
1039
       };
unknown's avatar
unknown committed
1040 1041 1042 1043

/* change master */

change:
unknown's avatar
unknown committed
1044
       CHANGE MASTER_SYM TO_SYM
unknown's avatar
unknown committed
1045 1046 1047
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
unknown's avatar
unknown committed
1048
	  bzero((char*) &lex->mi, sizeof(lex->mi));
1049 1050 1051 1052
        }
       master_defs
	{}
       ;
unknown's avatar
unknown committed
1053 1054 1055

master_defs:
       master_def
unknown's avatar
unknown committed
1056
       | master_defs ',' master_def;
unknown's avatar
unknown committed
1057

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
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
1074
       MASTER_PORT_SYM EQ ulong_num
1075 1076 1077 1078
       {
	 Lex->mi.port = $3;
       }
       |
unknown's avatar
unknown committed
1079
       MASTER_CONNECT_RETRY_SYM EQ ulong_num
1080 1081 1082
       {
	 Lex->mi.connect_retry = $3;
       }
unknown's avatar
unknown committed
1083
       | MASTER_SSL_SYM EQ ulong_num
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
         {
           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
1137
       | RELAY_LOG_POS_SYM EQ ulong_num
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
         {
           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;
1174 1175 1176 1177 1178
            if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
            {
              my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
              YYABORT;
            }
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
	    lex->sql_command= SQLCOM_CREATE_INDEX;
	    if (!lex->current_select->add_table_to_list(lex->thd, $7, NULL,
							TL_OPTION_UPDATING))
	      YYABORT;
	    lex->create_list.empty();
	    lex->key_list.empty();
	    lex->col_list.empty();
	    lex->change=NullS;
	  }
	   '(' key_list ')'
	  {
	    LEX *lex=Lex;

	    lex->key_list.push_back(new Key($2,$4.str, $5, 0, lex->col_list));
	    lex->col_list.empty();
	  }
	| CREATE DATABASE opt_if_not_exists ident
	  {
             Lex->create_info.default_table_charset= NULL;
             Lex->create_info.used_fields= 0;
          }
	  opt_create_database_options
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_CREATE_DB;
	    lex->name=$4.str;
            lex->create_info.options=$3;
	  }
	| CREATE udf_func_type FUNCTION_SYM sp_name
	  {
	    LEX *lex=Lex;
	    lex->spname= $4;
	    lex->udf.type= $2;
	  }
	  create_function_tail
	  {}
	| CREATE PROCEDURE sp_name
	  {
	    LEX *lex= Lex;
	    sp_head *sp;

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

	    sp->m_type= TYPE_ENUM_PROCEDURE;
	    lex->sphead= sp;
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
	    sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
	    YYTHD->client_capabilities &= (~CLIENT_MULTI_QUERIES);
	  }
          '('
	  {
	    LEX *lex= Lex;

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

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

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

	    if (sp->check_backpatch(YYTHD))
	      YYABORT;
	    sp->init_strings(YYTHD, lex, $3);
	    lex->sql_command= SQLCOM_CREATE_PROCEDURE;
	    /* Restore flag if it was cleared above */
	    if (sp->m_old_cmq)
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
	    sp->restore_thd_mem_root(YYTHD);
	  }
	| CREATE or_replace algorithm VIEW_SYM table_ident
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_CREATE_VIEW;
	    /* first table in list is target VIEW name */
	    if (!lex->select_lex.add_table_to_list(thd, $5, NULL, 0))
              YYABORT;
	  }
	  opt_view_list AS select_init check_option
	  {}
1286
        | CREATE TRIGGER_SYM sp_name trg_action_time trg_event 
1287 1288 1289 1290 1291 1292 1293
          ON table_ident FOR_SYM EACH_SYM ROW_SYM
          {
            LEX *lex= Lex;
            sp_head *sp;
           
            if (lex->sphead)
            {
unknown's avatar
unknown committed
1294
              my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "TRIGGER");
1295 1296
              YYABORT;
            }
1297 1298 1299

            if (!(sp= new sp_head()))
              YYABORT;
1300 1301 1302 1303 1304
            sp->reset_thd_mem_root(YYTHD);
            sp->init(lex);
            
            sp->m_type= TYPE_ENUM_TRIGGER;
            lex->sphead= sp;
1305
            lex->spname= $3;
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
            /*
              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;
1316
            lex->sphead->m_body_begin= lex->ptr;
1317 1318 1319 1320 1321 1322 1323
          }
          sp_proc_stmt
          {
            LEX *lex= Lex;
            sp_head *sp= lex->sphead;
            
            lex->sql_command= SQLCOM_CREATE_TRIGGER;
1324
            sp->init_strings(YYTHD, lex, $3);
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
            /* Restore flag if it was cleared above */
            if (sp->m_old_cmq)
              YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
            sp->restore_thd_mem_root(YYTHD);

            /*
              We have to do it after parsing trigger body, because some of
              sp_proc_stmt alternatives are not saving/restoring LEX, so
              lex->query_tables can be wiped out.
              
              QQ: What are other consequences of this?
              
              QQ: Could we loosen lock type in certain cases ?
            */
            if (!lex->select_lex.add_table_to_list(YYTHD, $7, 
                                                   (LEX_STRING*) 0,
                                                   TL_OPTION_UPDATING,
                                                   TL_WRITE))
              YYABORT;
          }
1345 1346 1347 1348
	| CREATE USER clear_privileges grant_list
	  {
	    Lex->sql_command = SQLCOM_CREATE_USER;
          }
1349 1350
	;

1351 1352 1353 1354 1355 1356 1357
clear_privileges:
        /* Nothing */
        {
          LEX *lex=Lex;
          lex->users_list.empty();
          lex->columns.empty();
          lex->grant= lex->grant_tot_col= 0;
1358
	  lex->all_privileges= 0;
1359 1360 1361 1362 1363 1364 1365
          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));
        }
        ;

1366
sp_name:
1367
	  ident '.' ident
1368 1369 1370 1371
	  {
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
1372
	| ident
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
	  {
	    $$= sp_name_current_db_new(YYTHD, $1);
	  }
	;

create_function_tail:
	  RETURNS_SYM udf_type UDF_SONAME_SYM TEXT_STRING_sys
	  {
	    LEX *lex=Lex;
	    lex->sql_command = SQLCOM_CREATE_FUNCTION;
	    lex->udf.name = lex->spname->m_name;
	    lex->udf.returns=(Item_result) $2;
	    lex->udf.dl=$4.str;
	  }
	| '('
	  {
	    LEX *lex= Lex;
	    sp_head *sp;

	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
1394
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
	      YYABORT;
	    }
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
	    sp->init(lex);

	    sp->m_type= TYPE_ENUM_FUNCTION;
	    lex->sphead= sp;
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
	    sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
	    YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
	    lex->sphead->m_param_begin= lex->tok_start+1;
	  }
          sp_fdparam_list ')'
	  {
	    LEX *lex= Lex;

	    lex->sphead->m_param_end= lex->tok_start;
	  }
	  RETURNS_SYM
	  {
	    LEX *lex= Lex;
unknown's avatar
unknown committed
1422 1423 1424 1425
	    lex->charset= NULL;
	    lex->length= lex->dec= NULL;
	    lex->interval_list.empty();
	    lex->type= 0;
1426 1427 1428 1429 1430
	  }
	  type
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
unknown's avatar
unknown committed
1431 1432 1433 1434 1435
            LEX_STRING cmt = { 0, 0 };
	    create_field *new_field;
	    uint unused1= 0;
	    int unused2= 0;

unknown's avatar
unknown committed
1436
	    if (!(new_field= new_create_field(YYTHD, (char*) "",
unknown's avatar
unknown committed
1437 1438 1439 1440 1441 1442 1443
					      (enum enum_field_types)$8,
			  		      lex->length, lex->dec, lex->type,
			  		      (Item *)0, (Item *) 0, &cmt, 0,
					      &lex->interval_list, 
			  		      (lex->charset ? lex->charset :
					       default_charset_info),
					      lex->uint_geom_type)))
unknown's avatar
unknown committed
1444 1445
	      YYABORT;

1446 1447
	    sp->m_returns_cs= new_field->charset;

unknown's avatar
unknown committed
1448
            if (new_field->interval_list.elements)
1449 1450 1451 1452 1453 1454
            {
	      new_field->interval= 
                sp->create_typelib(&new_field->interval_list);
            }
            sp_prepare_create_field(YYTHD, new_field);

unknown's avatar
unknown committed
1455 1456
	    if (prepare_create_field(new_field, &unused1, &unused2, &unused2,
				     0))
unknown's avatar
unknown committed
1457 1458 1459 1460 1461 1462
	      YYABORT;

	    sp->m_returns= new_field->sql_type;
	    sp->m_returns_cs= new_field->charset;
	    sp->m_returns_len= new_field->length;
	    sp->m_returns_pack= new_field->pack_flag;
1463 1464
            sp->m_returns_typelib= new_field->interval;
            new_field->interval= NULL;
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479

	    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;

1480 1481 1482 1483 1484 1485
	    if (sp->m_multi_results)
	    {
	      my_message(ER_SP_NO_RETSET_IN_FUNC, ER(ER_SP_NO_RETSET_IN_FUNC),
	                 MYF(0));
	      YYABORT;
	    }
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
	    if (sp->check_backpatch(YYTHD))
	      YYABORT;
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
	    sp->init_strings(YYTHD, lex, lex->spname);
	    /* Restore flag if it was cleared above */
	    if (sp->m_old_cmq)
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
	    sp->restore_thd_mem_root(YYTHD);
	  }
	;

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

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

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

/* Create characteristics */
sp_c_chistic:
	  sp_chistic            { }
	| DETERMINISTIC_SYM     { Lex->sp_chistics.detistic= TRUE; }
1529
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
	;

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();
1551
	    sp_add_used_routine(lex, YYTHD, $2, TYPE_ENUM_PROCEDURE);
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
	  }
          '(' sp_cparam_list ')' {}
	;

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

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

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

sp_fdparams:
	  sp_fdparams ',' sp_fdparam
	| sp_fdparam
	;

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

	    if (spc->find_pvar(&$1, TRUE))
	    {
1592
	      my_error(ER_SP_DUP_PARAM, MYF(0), $1.str);
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum enum_field_types)$2, sp_param_in);
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

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

	    if (spc->find_pvar(&$2, TRUE))
	    {
1618
	      my_error(ER_SP_DUP_PARAM, MYF(0), $2.str);
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
	      YYABORT;
	    }
	    spc->push_pvar(&$2, (enum enum_field_types)$3,
			   (sp_param_mode_t)$1);
	  }
	;

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

sp_proc_stmts:
	  /* Empty */ {}
1635
	| sp_proc_stmts  sp_proc_stmt ';'
1636 1637 1638 1639
	;

sp_proc_stmts1:
	  sp_proc_stmt ';' {}
1640
	| sp_proc_stmts1  sp_proc_stmt ';'
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
	;

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
1656 1657
	      my_message(ER_SP_VARCOND_AFTER_CURSHNDLR,
                         ER(ER_SP_VARCOND_AFTER_CURSHNDLR), MYF(0));
1658 1659 1660 1661
	      YYABORT;
	    }
	    if ($2.curs && $1.hndlrs)
	    { /* Cursor following handler */
unknown's avatar
unknown committed
1662 1663
	      my_message(ER_SP_CURSOR_AFTER_HANDLER,
                         ER(ER_SP_CURSOR_AFTER_HANDLER), MYF(0));
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
	      YYABORT;
	    }
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
	    $$.curs= $1.curs + $2.curs;
	  }
	;

sp_decl:
1674 1675 1676
	  DECLARE_SYM sp_decl_idents type 
          { Lex->sphead->reset_lex(YYTHD); }
          sp_opt_default
1677 1678 1679 1680 1681
	  {
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    uint max= ctx->context_pvars();
	    enum enum_field_types type= (enum enum_field_types)$3;
1682
	    Item *it= $5;
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693

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

1697 1698 1699 1700
                /*
                  The last instruction is assigned to be responsible for
                  freeing LEX.
                */
1701 1702 1703 1704 1705
	        lex->sphead->add_instr(in);
	        ctx->set_isset(i, TRUE);
		ctx->set_default(i, it);
	      }
	    }
1706
            lex->sphead->restore_lex(YYTHD);
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
	    $$.vars= $2;
	    $$.conds= $$.hndlrs= $$.curs= 0;
	  }
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1717
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
	      YYABORT;
	    }
	    YYTHD->lex->spcont->push_cond(&$2, $5);
	    $$.vars= $$.hndlrs= $$.curs= 0;
	    $$.conds= 1;
	  }
	| DECLARE_SYM sp_handler_type HANDLER_SYM FOR_SYM
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_instr_hpush_jump *i=
              new sp_instr_hpush_jump(sp->instructions(), ctx, $2,
	                              ctx->current_pvars());

	    sp->add_instr(i);
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
	    sp->m_in_handler= TRUE;
	  }
	  sp_hcond_list sp_proc_stmt
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *hlab= lex->spcont->pop_label(); /* After this hdlr */
	    sp_instr_hreturn *i;

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

	      sp->add_instr(i);
	      sp->push_backpatch(i, lex->spcont->last_label()); /* Block end */
	    }
	    lex->sphead->backpatch(hlab);
	    sp->m_in_handler= FALSE;
	    $$.vars= $$.conds= $$.curs= 0;
	    $$.hndlrs= $6;
1762
	    ctx->add_handlers($6);
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
	  }
	| 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))
	    {
1774
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
	      delete $5;
	      YYABORT;
	    }
            i= new sp_instr_cpush(sp->instructions(), ctx, $5);
	    sp->add_instr(i);
	    ctx->push_cursor(&$2);
	    $$.vars= $$.conds= $$.hndlrs= 0;
	    $$.curs= 1;
	  }
	;

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

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

	    if (lex->sql_command != SQLCOM_SELECT)
	    {
unknown's avatar
unknown committed
1801 1802
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
1803 1804 1805 1806
	      YYABORT;
	    }
	    if (lex->result)
	    {
unknown's avatar
unknown committed
1807 1808
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
	      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;
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
	    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();
1839

1840 1841 1842 1843
	      i->add_condition($1);
	      ctx->push_handler($1);
	      $$= 1;
	    }
1844 1845 1846 1847 1848
	  }
	| sp_hcond_list ',' sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
	    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();
1860

1861 1862 1863 1864
	      i->add_condition($3);
	      ctx->push_handler($3);
	      $$= $1 + 1;
	    }
1865 1866 1867 1868
	  }
	;

sp_cond:
unknown's avatar
unknown committed
1869
	  ulong_num
1870 1871 1872 1873 1874 1875 1876
	  {			/* 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 */
1877 1878 1879 1880 1881
	    if (!sp_cond_check(&$3))
	    {
	      my_error(ER_SP_BAD_SQLSTATE, MYF(0), $3.str);
	      YYABORT;
	    }
1882 1883
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
1884 1885
	    memcpy($$->sqlstate, $3.str, 5);
	    $$->sqlstate[5]= '\0';
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
	  }
	;

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
1904
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
1905 1906 1907 1908 1909 1910 1911 1912
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
1913
	| not FOUND_SYM		/* SQLSTATEs 02??? */
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::notfound;
	  }
	| SQLEXCEPTION_SYM	/* All other SQLSTATEs */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::exception;
	  }
	;

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

	    if (spc->find_pvar(&$1, TRUE))
	    {
1933
	      my_error(ER_SP_DUP_VAR, MYF(0), $1.str);
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
	      YYABORT;
	    }
	    spc->push_pvar(&$1, (enum_field_types)0, sp_param_in);
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_pvar(&$3, TRUE))
	    {
1946
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
	      YYABORT;
	    }
	    spc->push_pvar(&$3, (enum_field_types)0, sp_param_in);
	    $$= $1 + 1;
	  }
	;

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

sp_proc_stmt:
	  {
	    LEX *lex= Lex;

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

	    if ((lex->sql_command == SQLCOM_SELECT && !lex->result) ||
	        sp_multi_results_command(lex->sql_command))
	    {
	      /* We maybe have one or more SELECT without INTO */
	      sp->m_multi_results= TRUE;
	    }
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
unknown's avatar
unknown committed
1979
	      my_message(ER_SP_NO_USE, ER(ER_SP_NO_USE), MYF(0));
1980 1981
	      YYABORT;
	    }
1982 1983 1984 1985
	    /*
              Don't add an instruction for SET statements, since all
              instructions for them were already added during processing
              of "set" rule.
1986
	    */
1987 1988 1989
            DBUG_ASSERT(lex->sql_command != SQLCOM_SET_OPTION ||
                        lex->var_list.is_empty());
            if (lex->sql_command != SQLCOM_SET_OPTION)
1990
	    {
1991 1992
              sp_instr_stmt *i=new sp_instr_stmt(sp->instructions(),
                                                 lex->spcont, lex);
1993

1994
              /* Extract the query statement from the tokenizer:
1995
                 The end is either lex->tok_end or tok->ptr. */
1996 1997 1998 1999 2000 2001 2002 2003
              if (lex->ptr - lex->tok_end > 1)
                i->m_query.length= lex->ptr - sp->m_tmp_query;
              else
                i->m_query.length= lex->tok_end - sp->m_tmp_query;
              i->m_query.str= strmake_root(YYTHD->mem_root,
                                           (char *)sp->m_tmp_query,
                                           i->m_query.length);
              sp->add_instr(i);
2004 2005 2006
            }
	    sp->restore_lex(YYTHD);
          }
2007 2008 2009
          | RETURN_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr
2010 2011
	  {
	    LEX *lex= Lex;
2012
	    sp_head *sp= lex->sphead;
2013

2014
	    if (sp->m_type == TYPE_ENUM_PROCEDURE)
2015
	    {
unknown's avatar
unknown committed
2016
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
2017 2018 2019 2020 2021 2022
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_freturn *i;

2023 2024 2025 2026
	      i= new sp_instr_freturn(sp->instructions(), lex->spcont,
		                      $3, sp->m_returns, lex);
	      sp->add_instr(i);
	      sp->m_has_return= TRUE;
2027
	    }
2028
	    sp->restore_lex(YYTHD);
2029 2030 2031 2032 2033 2034 2035
	  }
	| IF sp_if END IF {}
	| CASE_SYM WHEN_SYM
	  {
	    Lex->sphead->m_simple_case= FALSE;
	  }
	  sp_case END CASE_SYM {}
2036 2037 2038
        | CASE_SYM
          { Lex->sphead->reset_lex(YYTHD); }
          expr WHEN_SYM
2039 2040 2041 2042 2043 2044 2045 2046
	  {
	    /* We "fake" this by using an anonymous variable which we
	       set to the expression. Note that all WHENs are evaluate
	       at the same frame level, so we then know that it's the
	       top-most variable in the frame. */
	    LEX *lex= Lex;
	    uint offset= lex->spcont->current_pvars();
	    sp_instr_set *i = new sp_instr_set(lex->sphead->instructions(),
2047 2048
                                               lex->spcont, offset, $3,
                                               MYSQL_TYPE_STRING, lex, TRUE);
2049
	    LEX_STRING dummy={(char*)"", 0};
2050 2051 2052 2053

	    lex->spcont->push_pvar(&dummy, MYSQL_TYPE_STRING, sp_param_in);
	    lex->sphead->add_instr(i);
	    lex->sphead->m_simple_case= TRUE;
2054
            lex->sphead->restore_lex(YYTHD);
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
	  }
	  sp_case END CASE_SYM
	  {
	    Lex->spcont->pop_pvar();
	  }
	| sp_labeled_control
	  {}
	| { /* Unlabeled controls get a secret label. */
	    LEX *lex= Lex;

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

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
2073
	| LEAVE_SYM label_ident
2074 2075 2076 2077 2078 2079 2080 2081
	  {
	    LEX *lex= Lex;
	    sp_head *sp = lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab)
	    {
2082
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
	      YYABORT;
	    }
	    else
	    {
	      uint ip= sp->instructions();
	      sp_instr_jump *i;
	      sp_instr_hpop *ih;
	      sp_instr_cpop *ic;

	      ih= new sp_instr_hpop(ip++, ctx, 0);
	      sp->push_backpatch(ih, lab);
	      sp->add_instr(ih);
	      ic= new sp_instr_cpop(ip++, ctx, 0);
	      sp->push_backpatch(ic, lab);
	      sp->add_instr(ic);
	      i= new sp_instr_jump(ip, ctx);
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
	    }
	  }
2103
	| ITERATE_SYM label_ident
2104 2105 2106 2107 2108 2109 2110 2111
	  {
	    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)
	    {
2112
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "ITERATE", $2.str);
2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_jump *i;
	      uint ip= sp->instructions();
	      uint n;

	      n= ctx->diff_handlers(lab->ctx);
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
	      n= ctx->diff_cursors(lab->ctx);
	      if (n)
	        sp->add_instr(new sp_instr_cpop(ip++, ctx, n));
	      i= new sp_instr_jump(ip, ctx, lab->ip); /* Jump back */
              sp->add_instr(i);
	    }
	  }
	| LABEL_SYM IDENT
	  {
2133
#ifdef SP_GOTO
2134 2135 2136 2137 2138 2139 2140
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (lab)
	    {
2141
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $2.str);
2142 2143 2144 2145 2146 2147 2148 2149 2150
	      YYABORT;
	    }
	    else
	    {
	      lab= ctx->push_label($2.str, sp->instructions());
	      lab->type= SP_LAB_GOTO;
	      lab->ctx= ctx;
              sp->backpatch(lab);
	    }
2151 2152 2153 2154
#else
	    yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
#endif
2155 2156 2157
	  }
	| GOTO_SYM IDENT
	  {
2158
#ifdef SP_GOTO
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab;
	    sp_instr_jump *i;
	    sp_instr_hpop *ih;
	    sp_instr_cpop *ic;

	    if (sp->m_in_handler)
	    {
unknown's avatar
unknown committed
2170
	      my_message(ER_SP_GOTO_IN_HNDLR, ER(ER_SP_GOTO_IN_HNDLR), MYF(0));
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
	      YYABORT;
	    }
	    lab= ctx->find_label($2.str);
	    if (! lab)
	    {
	      lab= (sp_label_t *)YYTHD->alloc(sizeof(sp_label_t));
	      lab->name= $2.str;
	      lab->ip= 0;
	      lab->type= SP_LAB_REF;
	      lab->ctx= ctx;

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

	      n= ctx->diff_handlers(lab->ctx);
	      if (n)
	      {
	        ih= new sp_instr_hpop(ip++, ctx, n);
	        sp->add_instr(ih);
	      }
	      n= ctx->diff_cursors(lab->ctx);
	      if (n)
	      {
	        ic= new sp_instr_cpop(ip++, ctx, n);
	        sp->add_instr(ic);
	      }
	      i= new sp_instr_jump(ip, ctx, lab->ip); /* Jump back */
	      sp->add_instr(i);
	    }
2211 2212 2213 2214
#else
	    yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
#endif
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
	  }
	| OPEN_SYM ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_copen *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2225
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
	      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))
	    {
2240
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $3.str);
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
	      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))
	    {
2257
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280
	      YYABORT;
	    }
	    i= new sp_instr_cclose(sp->instructions(), lex->spcont,  offset);
	    sp->add_instr(i);
	  }
	;

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

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

	    if (!spc || !(spv = spc->find_pvar(&$1)))
	    {
2281
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

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

	    if (!spc || !(spv = spc->find_pvar(&$3)))
	    {
2303
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $3.str);
2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

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

sp_if:
2318 2319
          { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2320 2321 2322 2323 2324
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= sp->instructions();
2325 2326
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, ctx,
                                                               $2, lex);
2327 2328 2329

	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
            sp->add_instr(i);
2330
            sp->restore_lex(YYTHD);
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357
	  }
	  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:
2358 2359
	  { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2360 2361 2362 2363 2364 2365 2366 2367
	  {
            LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

	    if (! sp->m_simple_case)
2368
	      i= new sp_instr_jump_if_not(ip, ctx, $2, lex);
2369 2370 2371 2372 2373 2374 2375 2376
	    else
	    { /* Simple case: <caseval> = <whenval> */
	      LEX_STRING ivar;

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

2379
	      i= new sp_instr_jump_if_not(ip, ctx, expr, lex);
2380 2381 2382 2383
              lex->variables_used= 1;
	    }
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
            sp->add_instr(i);
2384
            sp->restore_lex(YYTHD);
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
	  }
	  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
2404

2405 2406 2407 2408 2409 2410 2411
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
2412

2413 2414 2415 2416 2417
	    sp->add_instr(i);
	  }
	| ELSE sp_proc_stmts1 {}
	| WHEN_SYM sp_case {}
	;
unknown's avatar
unknown committed
2418

2419
sp_labeled_control:
2420
	  label_ident ':'
unknown's avatar
unknown committed
2421
	  {
2422 2423 2424 2425 2426 2427
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);

	    if (lab)
	    {
2428
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $1.str);
unknown's avatar
unknown committed
2429
	      YYABORT;
2430 2431 2432 2433 2434 2435 2436
	    }
	    else
	    {
	      lab= lex->spcont->push_label($1.str,
	                                   lex->sphead->instructions());
	      lab->type= SP_LAB_ITER;
	    }
unknown's avatar
unknown committed
2437
	  }
2438
	  sp_unlabeled_control sp_opt_label
unknown's avatar
unknown committed
2439
	  {
2440
	    LEX *lex= Lex;
unknown's avatar
unknown committed
2441

2442 2443 2444 2445 2446 2447 2448
	    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)
	      {
2449
	        my_error(ER_SP_LABEL_MISMATCH, MYF(0), $5.str);
2450 2451 2452 2453
	        YYABORT;
	      }
	    }
	    lex->sphead->backpatch(lex->spcont->pop_label());
unknown's avatar
unknown committed
2454
	  }
2455 2456 2457
	;

sp_opt_label:
2458
        /* Empty  */    { $$= null_lex_str; }
2459
        | label_ident   { $$= $1; }
2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475
	;

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
2476
	  {
2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
	    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
2492
	  {
2493 2494 2495 2496 2497 2498
	    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
2499
	  }
2500 2501 2502
        | WHILE_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr DO_SYM
unknown's avatar
unknown committed
2503
	  {
2504 2505 2506 2507
	    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,
2508
							       $3, lex);
2509 2510 2511 2512

	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
            sp->add_instr(i);
2513
            sp->restore_lex(YYTHD);
unknown's avatar
unknown committed
2514
	  }
2515
	  sp_proc_stmts1 END WHILE_SYM
unknown's avatar
unknown committed
2516
	  {
2517 2518 2519 2520 2521 2522 2523
	    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);
	  }
2524 2525 2526
        | REPEAT_SYM sp_proc_stmts1 UNTIL_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr END REPEAT_SYM
2527 2528 2529 2530 2531
	  {
	    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,
2532 2533
                                                               $5, lab->ip,
                                                               lex);
2534
            lex->sphead->add_instr(i);
2535
            lex->sphead->restore_lex(YYTHD);
2536
	  }
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552
	;

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; }
2553
          ;
unknown's avatar
unknown committed
2554 2555

create2:
unknown's avatar
unknown committed
2556
        '(' create2a {}
2557 2558 2559
        | opt_create_table_options
          opt_partitioning {} 
          create3 {}
unknown's avatar
unknown committed
2560 2561 2562 2563
        | LIKE table_ident
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$2))
unknown's avatar
unknown committed
2564
              YYABORT;
unknown's avatar
unknown committed
2565 2566 2567 2568 2569
          }
        | '(' LIKE table_ident ')'
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$3))
2570
              YYABORT;
unknown's avatar
unknown committed
2571
          }
unknown's avatar
unknown committed
2572
        ;
unknown's avatar
unknown committed
2573

unknown's avatar
unknown committed
2574
create2a:
2575 2576 2577 2578 2579 2580
        field_list ')' opt_create_table_options
          opt_partitioning {}
          create3 {}
        |  opt_partitioning {}
           create_select ')'
           { Select->set_braces(1);} union_opt {}
2581
        ;
unknown's avatar
unknown committed
2582 2583

create3:
2584
	/* empty */ {}
unknown's avatar
unknown committed
2585
	| opt_duplicate opt_as     create_select
unknown's avatar
unknown committed
2586
          { Select->set_braces(0);} union_clause {}
unknown's avatar
unknown committed
2587
	| opt_duplicate opt_as '(' create_select ')'
unknown's avatar
unknown committed
2588
          { Select->set_braces(1);} union_opt {}
2589 2590
        ;

2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
/*
 This part of the parser is about handling of the partition information.

 It's first version was written by Mikael Ronstrm with lots of answers to
 questions provided by Antony Curtis.

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

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

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

partitioning:
        PARTITION_SYM
unknown's avatar
unknown committed
2622 2623 2624 2625 2626 2627 2628 2629 2630
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
            my_error(ER_OUTOFMEMORY, MYF(0), sizeof(partition_info));
            YYABORT;
          }
        }
2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644
        partition
        ;

partition_entry:
        PARTITION_SYM
        {
          LEX *lex= Lex;
          if (lex->part_info)
          {
            /*
              We enter here when opening the frm file to translate
              partition info string into part_info data structure.
            */
            lex->part_info= new partition_info();
unknown's avatar
unknown committed
2645 2646 2647 2648 2649
            if (!lex->part_info)
            {
              my_error(ER_OUTOFMEMORY, MYF(0), sizeof(partition_info));
              YYABORT;
            }
2650 2651 2652 2653 2654 2655 2656
          }
          else
          {
            yyerror(ER(ER_PARTITION_ENTRY_ERROR));
            YYABORT;
          }
        }
unknown's avatar
unknown committed
2657 2658
        partition {}
        ;
2659 2660

partition:
unknown's avatar
unknown committed
2661 2662
        BY part_type_def opt_no_parts {} opt_sub_part {} part_defs
        ;
2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678

part_type_def:
        opt_linear KEY_SYM '(' part_field_list ')'
        {
          LEX *lex= Lex;
          lex->part_info->list_of_part_fields= TRUE;
          lex->part_info->part_type= HASH_PARTITION;
        }
        | opt_linear HASH_SYM
        { Lex->part_info->part_type= HASH_PARTITION; }
        part_func {}
        | RANGE_SYM
        { Lex->part_info->part_type= RANGE_PARTITION; }
        part_func {}
        | LIST_SYM
        { Lex->part_info->part_type= LIST_PARTITION; }
unknown's avatar
unknown committed
2679 2680
        part_func {}
        ;
2681 2682 2683 2684

opt_linear:
        /* empty */ {}
        | LINEAR_SYM
unknown's avatar
unknown committed
2685 2686
        { Lex->part_info->linear_hash_ind= TRUE;}
        ;
2687 2688 2689

part_field_list:
        part_field_item {}
unknown's avatar
unknown committed
2690 2691
        | part_field_list ',' part_field_item {}
        ;
2692 2693 2694 2695 2696

part_field_item:
        ident
        {
          Lex->part_info->part_field_list.push_back($1.str);
unknown's avatar
unknown committed
2697 2698
        }
        ;
2699 2700 2701 2702 2703 2704 2705 2706 2707 2708

part_func:
        '(' remember_name part_func_expr remember_end ')'
        {
          LEX *lex= Lex;
          uint expr_len= (uint)($4 - $2) - 1;
          lex->part_info->list_of_part_fields= FALSE;
          lex->part_info->part_expr= $3;
          lex->part_info->part_func_string= $2+1;
          lex->part_info->part_func_len= expr_len;
unknown's avatar
unknown committed
2709 2710
        }
        ;
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720

sub_part_func:
        '(' remember_name part_func_expr remember_end ')'
        {
          LEX *lex= Lex;
          uint expr_len= (uint)($4 - $2) - 1;
          lex->part_info->list_of_subpart_fields= FALSE;
          lex->part_info->subpart_expr= $3;
          lex->part_info->subpart_func_string= $2+1;
          lex->part_info->subpart_func_len= expr_len;
unknown's avatar
unknown committed
2721 2722
        }
        ;
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735


opt_no_parts:
        /* empty */ {}
        | PARTITIONS_SYM ulong_num 
        { 
          uint no_parts= $2;
          if (no_parts == 0)
          {
            my_error(ER_NO_PARTS_ERROR, MYF(0), "partitions");
            YYABORT;
          }
          Lex->part_info->no_parts= no_parts;
unknown's avatar
unknown committed
2736 2737
        }
        ;
2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750

opt_sub_part:
        /* empty */ {}
        | SUBPARTITION_SYM BY opt_linear HASH_SYM sub_part_func
        { Lex->part_info->subpart_type= HASH_PARTITION; }
        opt_no_subparts {}
        | SUBPARTITION_SYM BY opt_linear KEY_SYM
          '(' sub_part_field_list ')'
        {
          LEX *lex= Lex;
          lex->part_info->subpart_type= HASH_PARTITION;
          lex->part_info->list_of_subpart_fields= TRUE;
        }
unknown's avatar
unknown committed
2751 2752
        opt_no_subparts {}
        ;
2753 2754 2755

sub_part_field_list:
        sub_part_field_item {}
unknown's avatar
unknown committed
2756 2757
        | sub_part_field_list ',' sub_part_field_item {}
        ;
2758 2759 2760

sub_part_field_item:
        ident
unknown's avatar
unknown committed
2761 2762
        { Lex->part_info->subpart_field_list.push_back($1.str); }
        ;
2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777

part_func_expr:
        bit_expr
        {
          LEX *lex= Lex;
          bool not_corr_func;
          not_corr_func= !lex->safe_to_cache_query;
          lex->safe_to_cache_query= 1;
          if (not_corr_func)
          {
            yyerror(ER(ER_CONST_EXPR_IN_PARTITION_FUNC_ERROR));
            YYABORT;
          }
          $$=$1;
        }
unknown's avatar
unknown committed
2778
        ;
2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790

opt_no_subparts:
        /* empty */ {}
        | SUBPARTITIONS_SYM ulong_num
        {
          uint no_parts= $2;
          if (no_parts == 0)
          {
            my_error(ER_NO_PARTS_ERROR, MYF(0), "subpartitions");
            YYABORT;
          }
          Lex->part_info->no_subparts= no_parts;
unknown's avatar
unknown committed
2791 2792
        }
        ;
2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815

part_defs:
        /* empty */
        {}
        | '(' part_def_list ')'
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          if (part_info->no_parts != 0)
          {
            if (part_info->no_parts !=
                part_info->count_curr_parts)
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_PART_ERROR));
              YYABORT;
            }
          }
          else if (part_info->count_curr_parts > 0)
          {
            part_info->no_parts= part_info->count_curr_parts;
          }
          part_info->count_curr_subparts= 0;
          part_info->count_curr_parts= 0;
unknown's avatar
unknown committed
2816 2817
        }
        ;
2818 2819 2820

part_def_list:
        part_definition {}
unknown's avatar
unknown committed
2821 2822
        | part_def_list ',' part_definition {}
        ;
2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844

part_definition:
        PARTITION_SYM
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          partition_element *p_elem= new partition_element();
          if (!p_elem)
          {
            my_error(ER_OUTOFMEMORY, MYF(0), sizeof(partition_element));
            YYABORT;
          }
          part_info->curr_part_elem= p_elem;
          part_info->current_partition= p_elem;
          part_info->use_default_partitions= FALSE;
          part_info->partitions.push_back(p_elem);
          p_elem->engine_type= DB_TYPE_UNKNOWN;
          part_info->count_curr_parts++;
        }
        part_name {}
        opt_part_values {}
        opt_part_options {}
unknown's avatar
unknown committed
2845 2846
        opt_sub_partition {}
        ;
2847 2848 2849

part_name:
        ident_or_text
unknown's avatar
unknown committed
2850 2851
        { Lex->part_info->curr_part_elem->partition_name= $1.str; }
        ;
2852 2853 2854 2855 2856

opt_part_values:
        /* empty */
        {
          LEX *lex= Lex;
unknown's avatar
unknown committed
2857
          if (!is_partition_management(lex))
2858
          {
unknown's avatar
unknown committed
2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870
            if (lex->part_info->part_type == RANGE_PARTITION)
            {
              my_error(ER_PARTITION_REQUIRES_VALUES_ERROR, MYF(0),
              "RANGE", "LESS THAN");
              YYABORT;
            }
            if (lex->part_info->part_type == LIST_PARTITION)
            {
              my_error(ER_PARTITION_REQUIRES_VALUES_ERROR, MYF(0),
              "LIST", "IN");
              YYABORT;
            }
2871 2872 2873 2874
          }
        }
        | VALUES LESS_SYM THAN_SYM part_func_max
        {
unknown's avatar
unknown committed
2875 2876
          LEX *lex= Lex;
          if (!is_partition_management(lex))
2877
          {
unknown's avatar
unknown committed
2878 2879 2880 2881 2882 2883
            if (Lex->part_info->part_type != RANGE_PARTITION)
            {
              my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
              "RANGE", "LESS THAN");
              YYABORT;
            }
2884 2885 2886 2887
          }
        }
        | VALUES IN_SYM '(' part_list_func ')'
        {
unknown's avatar
unknown committed
2888 2889
          LEX *lex= Lex;
          if (!is_partition_management(lex))
2890
          {
unknown's avatar
unknown committed
2891 2892 2893 2894 2895 2896
            if (Lex->part_info->part_type != LIST_PARTITION)
            {
              my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
              "LIST", "IN");
              YYABORT;
            }
2897
          }
unknown's avatar
unknown committed
2898 2899
        }
        ;
2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910

part_func_max:
        MAX_VALUE_SYM
        {
          LEX *lex= Lex;
          if (lex->part_info->defined_max_value)
          {
            yyerror(ER(ER_PARTITION_MAXVALUE_ERROR));
            YYABORT;
          }
          lex->part_info->defined_max_value= TRUE;
unknown's avatar
unknown committed
2911
          lex->part_info->curr_part_elem->range_value= LONGLONG_MAX;
2912 2913 2914 2915 2916 2917 2918 2919
        }
        | part_range_func
        {
          if (Lex->part_info->defined_max_value)
          {
            yyerror(ER(ER_PARTITION_MAXVALUE_ERROR));
            YYABORT;
          }
unknown's avatar
unknown committed
2920 2921
        }
        ;
2922 2923 2924 2925

part_range_func:
        '(' part_bit_expr ')' 
        {
unknown's avatar
unknown committed
2926
          Lex->part_info->curr_part_elem->range_value= $2;
unknown's avatar
unknown committed
2927 2928
        }
        ;
2929 2930 2931

part_list_func:
        part_list_item {}
unknown's avatar
unknown committed
2932 2933
        | part_list_func ',' part_list_item {}
        ;
2934 2935 2936 2937

part_list_item:
        part_bit_expr
        {
unknown's avatar
unknown committed
2938
          longlong *value_ptr;
unknown's avatar
unknown committed
2939
          if (!(value_ptr= (longlong*)sql_alloc(sizeof(longlong))))
unknown's avatar
unknown committed
2940 2941 2942 2943 2944 2945
          {
            my_error(ER_OUTOFMEMORY, MYF(0), sizeof(longlong));
            YYABORT;
          }
          *value_ptr= $1;
          Lex->part_info->curr_part_elem->list_val_list.push_back(value_ptr);
unknown's avatar
unknown committed
2946 2947
        }
        ;
2948 2949 2950 2951 2952 2953 2954

part_bit_expr:
        bit_expr
        {
          Item *part_expr= $1;
          bool not_corr_func;
          LEX *lex= Lex;
unknown's avatar
unknown committed
2955
          longlong item_value;
2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968
          Name_resolution_context *context= &lex->current_select->context;
          TABLE_LIST *save_list= context->table_list;

          context->table_list= 0;
          part_expr->fix_fields(YYTHD, (Item**)0);
          context->table_list= save_list;
          not_corr_func= !part_expr->const_item() ||
                         !lex->safe_to_cache_query;
          if (not_corr_func)
          {
            yyerror(ER(ER_NO_CONST_EXPR_IN_RANGE_OR_LIST_ERROR));
            YYABORT;
          }
unknown's avatar
unknown committed
2969 2970 2971 2972 2973 2974 2975
          if (part_expr->result_type() != INT_RESULT)
          {
            yyerror(ER(ER_INCONSISTENT_TYPE_OF_FUNCTIONS_ERROR));
            YYABORT;
          }
          item_value= part_expr->val_int();
          $$= item_value; 
2976
        }
unknown's avatar
unknown committed
2977
        ;
2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998

opt_sub_partition:
        /* empty */ {}
        | '(' sub_part_list ')'
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          if (part_info->no_subparts != 0)
          {
            if (part_info->no_subparts !=
                part_info->count_curr_subparts)
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_SUBPART_ERROR));
              YYABORT;
            }
          }
          else if (part_info->count_curr_subparts > 0)
          {
            part_info->no_subparts= part_info->count_curr_subparts;
          }
          part_info->count_curr_subparts= 0;
unknown's avatar
unknown committed
2999 3000
        }
        ;
3001 3002 3003

sub_part_list:
        sub_part_definition {}
unknown's avatar
unknown committed
3004 3005
        | sub_part_list ',' sub_part_definition {}
        ;
3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023

sub_part_definition:
        SUBPARTITION_SYM
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          partition_element *p_elem= new partition_element();
          if (!p_elem)
          {
            my_error(ER_OUTOFMEMORY, MYF(0), sizeof(partition_element));
            YYABORT;
          }
          part_info->curr_part_elem= p_elem;
          part_info->current_partition->subpartitions.push_back(p_elem);
          part_info->use_default_subpartitions= FALSE;
          part_info->count_curr_subparts++;
          p_elem->engine_type= DB_TYPE_UNKNOWN;
        }
unknown's avatar
unknown committed
3024 3025
        sub_name opt_part_options {}
        ;
3026 3027 3028

sub_name:
        ident_or_text
unknown's avatar
unknown committed
3029 3030
        { Lex->part_info->curr_part_elem->partition_name= $1.str; }
        ;
3031 3032 3033

opt_part_options:
       /* empty */ {}
unknown's avatar
unknown committed
3034 3035
       | opt_part_option_list {}
       ;
3036 3037 3038

opt_part_option_list:
       opt_part_option_list opt_part_option {}
unknown's avatar
unknown committed
3039 3040
       | opt_part_option {}
       ;
3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057

opt_part_option:
        TABLESPACE opt_equal ident_or_text
        { Lex->part_info->curr_part_elem->tablespace_name= $3.str; }
        | opt_storage ENGINE_SYM opt_equal storage_engines
        { Lex->part_info->curr_part_elem->engine_type= $4; }
        | NODEGROUP_SYM opt_equal ulong_num
        { Lex->part_info->curr_part_elem->nodegroup_id= $3; }
        | MAX_ROWS opt_equal ulonglong_num
        { Lex->part_info->curr_part_elem->part_max_rows= $3; }
        | MIN_ROWS opt_equal ulonglong_num
        { Lex->part_info->curr_part_elem->part_min_rows= $3; }
        | DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys
        { Lex->part_info->curr_part_elem->data_file_name= $4.str; }
        | INDEX_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys
        { Lex->part_info->curr_part_elem->index_file_name= $4.str; }
        | COMMENT_SYM opt_equal TEXT_STRING_sys
unknown's avatar
unknown committed
3058 3059
        { Lex->part_info->curr_part_elem->part_comment= $3.str; }
        ;
3060 3061 3062 3063 3064

/*
 End of partition parser part
*/

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

3096 3097
opt_as:
	/* empty */ {}
unknown's avatar
unknown committed
3098
	| AS	    {};
3099

3100 3101 3102 3103 3104 3105 3106 3107 3108
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
3109 3110
	default_collation   {}
	| default_charset   {};
3111

unknown's avatar
unknown committed
3112
opt_table_options:
unknown's avatar
unknown committed
3113
	/* empty */	 { $$= 0; }
unknown's avatar
unknown committed
3114
	| table_options  { $$= $1;};
unknown's avatar
unknown committed
3115 3116 3117

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

unknown's avatar
unknown committed
3120
table_option:
unknown's avatar
unknown committed
3121
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
unknown's avatar
unknown committed
3122 3123

opt_if_not_exists:
unknown's avatar
unknown committed
3124
	/* empty */	 { $$= 0; }
3125
	| IF not EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
unknown's avatar
unknown committed
3126 3127 3128

opt_create_table_options:
	/* empty */
unknown's avatar
unknown committed
3129
	| create_table_options;
unknown's avatar
unknown committed
3130

unknown's avatar
unknown committed
3131 3132 3133 3134
create_table_options_space_separated:
	create_table_option
	| create_table_option create_table_options_space_separated;

unknown's avatar
unknown committed
3135 3136
create_table_options:
	create_table_option
3137
	| create_table_option     create_table_options
unknown's avatar
unknown committed
3138
	| create_table_option ',' create_table_options;
unknown's avatar
unknown committed
3139 3140

create_table_option:
3141 3142
	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; }
3143 3144
	| 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
3145
	| 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;}
3146 3147
	| PASSWORD opt_equal TEXT_STRING_sys	{ Lex->create_info.password=$3.str; Lex->create_info.used_fields|= HA_CREATE_USED_PASSWORD; }
	| COMMENT_SYM opt_equal TEXT_STRING_sys	{ Lex->create_info.comment=$3.str; Lex->create_info.used_fields|= HA_CREATE_USED_COMMENT; }
3148
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
unknown's avatar
unknown committed
3149
	| PACK_KEYS_SYM opt_equal ulong_num	{ Lex->create_info.table_options|= $3 ? HA_OPTION_PACK_KEYS : HA_OPTION_NO_PACK_KEYS; Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;}
3150
	| 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
3151 3152
	| 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; }
3153
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
3154
	| RAID_TYPE opt_equal raid_types	{ Lex->create_info.raid_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
unknown's avatar
unknown committed
3155 3156
	| RAID_CHUNKS opt_equal ulong_num	{ Lex->create_info.raid_chunks= $3; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| RAID_CHUNKSIZE opt_equal ulong_num	{ Lex->create_info.raid_chunksize= $3*RAID_BLOCK_SIZE; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
3157
	| UNION_SYM opt_equal '(' table_list ')'
3158 3159 3160
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
3161 3162
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
3163
	    lex->create_info.merge_list.elements--;
3164 3165
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
3166
	    lex->select_lex.table_list.elements=1;
3167 3168 3169
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
3170
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
3171
	  }
3172 3173
	| default_charset
	| default_collation
3174
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
3175 3176 3177
	| DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.data_file_name= $4.str; Lex->create_info.used_fields|= HA_CREATE_USED_DATADIR; }
	| INDEX_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys { Lex->create_info.index_file_name= $4.str;  Lex->create_info.used_fields|= HA_CREATE_USED_INDEXDIR; }
        ;
unknown's avatar
unknown committed
3178

3179 3180 3181 3182 3183 3184 3185 3186
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))
          {
3187 3188 3189
            my_error(ER_CONFLICTING_DECLARATIONS, MYF(0),
                     "CHARACTER SET ", cinfo->default_table_charset->csname,
                     "CHARACTER SET ", $4->csname);
3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
            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))
            {
3204 3205
              my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $4->name, cinfo->default_table_charset->csname);
3206 3207 3208 3209 3210 3211
              YYABORT;
            }
            Lex->create_info.default_table_charset= $4;
            Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

unknown's avatar
unknown committed
3212
storage_engines:
3213 3214 3215 3216
	ident_or_text
	{
	  $$ = ha_resolve_by_name($1.str,$1.length);
	  if ($$ == DB_TYPE_UNKNOWN) {
3217
	    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
3218 3219 3220
	    YYABORT;
	  }
	};
unknown's avatar
unknown committed
3221 3222 3223 3224 3225

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
3226 3227 3228
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; }
	| REDUNDANT_SYM	{ $$= ROW_TYPE_REDUNDANT; }
	| COMPACT_SYM	{ $$= ROW_TYPE_COMPACT; };
unknown's avatar
unknown committed
3229 3230 3231 3232

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

3235 3236 3237
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
unknown's avatar
unknown committed
3238
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
3239

unknown's avatar
unknown committed
3240
opt_select_from:
3241
	opt_limit_clause {}
unknown's avatar
unknown committed
3242
	| select_from select_lock_type;
unknown's avatar
unknown committed
3243 3244

udf_func_type:
3245
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
unknown's avatar
unknown committed
3246
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
unknown's avatar
unknown committed
3247 3248 3249 3250

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
unknown's avatar
unknown committed
3251
        | DECIMAL_SYM {$$ = (int) DECIMAL_RESULT; }
unknown's avatar
unknown committed
3252
	| INT_SYM {$$ = (int) INT_RESULT; };
unknown's avatar
unknown committed
3253 3254 3255

field_list:
	  field_list_item
unknown's avatar
unknown committed
3256
	| field_list ',' field_list_item;
unknown's avatar
unknown committed
3257 3258 3259


field_list_item:
unknown's avatar
unknown committed
3260
	   column_def
3261 3262 3263 3264
         | key_def
         ;

column_def:
unknown's avatar
unknown committed
3265
	  field_spec opt_check_constraint
unknown's avatar
unknown committed
3266 3267 3268 3269
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
unknown's avatar
unknown committed
3270
	;
3271 3272

key_def:
unknown's avatar
unknown committed
3273
	key_type opt_ident key_alg '(' key_list ')'
unknown's avatar
unknown committed
3274
	  {
unknown's avatar
unknown committed
3275
	    LEX *lex=Lex;
3276
	    lex->key_list.push_back(new Key($1,$2, $3, 0, lex->col_list));
unknown's avatar
unknown committed
3277
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
3278
	  }
3279 3280 3281 3282
	| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
	  {
	    LEX *lex=Lex;
	    const char *key_name= $3 ? $3:$1;
3283 3284
	    lex->key_list.push_back(new Key($2, key_name, $4, 0,
				    lex->col_list));
3285 3286
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
3287
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
unknown's avatar
unknown committed
3288
	  {
3289
	    LEX *lex=Lex;
3290
	    lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
3291 3292 3293 3294 3295
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
3296 3297 3298
	    lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4 : $1,
					    HA_KEY_ALG_UNDEF, 1,
					    lex->col_list));
3299
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
3300
	  }
unknown's avatar
unknown committed
3301 3302 3303 3304
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
3305
	| opt_constraint check_constraint
unknown's avatar
unknown committed
3306 3307
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
3308 3309 3310
	  }
	;

unknown's avatar
unknown committed
3311
opt_check_constraint:
3312
	/* empty */
unknown's avatar
unknown committed
3313 3314 3315 3316 3317
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
3318
	;
unknown's avatar
unknown committed
3319 3320

opt_constraint:
3321
	/* empty */		{ $$=(char*) 0; }
unknown's avatar
unknown committed
3322 3323 3324 3325 3326 3327
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
unknown's avatar
unknown committed
3328 3329 3330 3331

field_spec:
	field_ident
	 {
unknown's avatar
unknown committed
3332
	   LEX *lex=Lex;
3333
	   lex->length=lex->dec=0; lex->type=0;
3334
	   lex->default_value= lex->on_update_value= 0;
3335
           lex->comment=null_lex_str;
3336
	   lex->charset=NULL;
unknown's avatar
unknown committed
3337 3338 3339
	 }
	type opt_attribute
	{
unknown's avatar
unknown committed
3340
	  LEX *lex=Lex;
unknown's avatar
unknown committed
3341
	  if (add_field_to_list(lex->thd, $1.str,
unknown's avatar
unknown committed
3342
				(enum enum_field_types) $3,
unknown's avatar
unknown committed
3343
				lex->length,lex->dec,lex->type,
3344
				lex->default_value, lex->on_update_value, 
3345
                                &lex->comment,
3346
				lex->change,&lex->interval_list,lex->charset,
unknown's avatar
unknown committed
3347
				lex->uint_geom_type))
unknown's avatar
unknown committed
3348
	    YYABORT;
unknown's avatar
unknown committed
3349
	};
unknown's avatar
unknown committed
3350 3351

type:
3352
	int_type opt_len field_options	{ $$=$1; }
unknown's avatar
unknown committed
3353 3354
	| real_type opt_precision field_options { $$=$1; }
	| FLOAT_SYM float_options field_options { $$=FIELD_TYPE_FLOAT; }
unknown's avatar
unknown committed
3355 3356 3357 3358
	| BIT_SYM			{ Lex->length= (char*) "1";
					  $$=FIELD_TYPE_BIT; }
	| BIT_SYM '(' NUM ')'		{ Lex->length= $3.str;
					  $$=FIELD_TYPE_BIT; }
unknown's avatar
unknown committed
3359 3360
	| BOOL_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
3361 3362
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
3363
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
unknown's avatar
unknown committed
3364 3365 3366
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
3367
	| nchar '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
3368
					  $$=FIELD_TYPE_STRING;
3369
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
3370
	| nchar				{ Lex->length=(char*) "1";
unknown's avatar
unknown committed
3371
					  $$=FIELD_TYPE_STRING;
3372
					  Lex->charset=national_charset_info; }
3373
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
3374
					  Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
3375
					  $$=FIELD_TYPE_STRING; }
3376 3377 3378
	| BINARY			{ Lex->length= (char*) "1";
					  Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
3379
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
3380
					  $$= MYSQL_TYPE_VARCHAR; }
unknown's avatar
unknown committed
3381
	| nvarchar '(' NUM ')'		{ Lex->length=$3.str;
3382
					  $$= MYSQL_TYPE_VARCHAR;
3383
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
3384
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
unknown's avatar
unknown committed
3385
					  Lex->charset=&my_charset_bin;
3386
					  $$= MYSQL_TYPE_VARCHAR; }
3387
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
unknown's avatar
unknown committed
3388 3389
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
3390
	| TIMESTAMP opt_len
3391
	  {
3392
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
3393 3394
	      $$=FIELD_TYPE_DATETIME;
	    else
3395 3396 3397 3398 3399
            {
              /* 
                Unlike other types TIMESTAMP fields are NOT NULL by default.
              */
              Lex->type|= NOT_NULL_FLAG;
3400
	      $$=FIELD_TYPE_TIMESTAMP;
3401
            }
3402
	   }
unknown's avatar
unknown committed
3403
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
unknown's avatar
unknown committed
3404
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
3405
					  $$=FIELD_TYPE_TINY_BLOB; }
unknown's avatar
unknown committed
3406
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
3407
					  $$=FIELD_TYPE_BLOB; }
unknown's avatar
unknown committed
3408 3409
	| spatial_type
          {
unknown's avatar
unknown committed
3410
#ifdef HAVE_SPATIAL
unknown's avatar
unknown committed
3411 3412 3413
            Lex->charset=&my_charset_bin;
            Lex->uint_geom_type= (uint)$1;
            $$=FIELD_TYPE_GEOMETRY;
unknown's avatar
unknown committed
3414
#else
unknown's avatar
unknown committed
3415
            my_error(ER_FEATURE_DISABLED, MYF(0),
3416
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3417
            YYABORT;
unknown's avatar
unknown committed
3418
#endif
unknown's avatar
unknown committed
3419
          }
unknown's avatar
unknown committed
3420
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
3421
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
3422
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
3423
					  $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
3424
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
3425
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
3426 3427
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
3428
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
3429 3430
	| MEDIUMTEXT opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| LONGTEXT opt_binary		{ $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
3431
	| DECIMAL_SYM float_options field_options
unknown's avatar
unknown committed
3432
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
unknown's avatar
unknown committed
3433
	| NUMERIC_SYM float_options field_options
unknown's avatar
unknown committed
3434
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
3435
	| FIXED_SYM float_options field_options
unknown's avatar
unknown committed
3436
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
3437
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
3438
	  { $$=FIELD_TYPE_ENUM; }
3439
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
3440
	  { $$=FIELD_TYPE_SET; }
3441
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
3442 3443 3444 3445 3446 3447
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
3448
	;
unknown's avatar
unknown committed
3449

unknown's avatar
unknown committed
3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
	| POINT_SYM           { $$= Field::GEOM_POINT; }
	| MULTIPOINT          { $$= Field::GEOM_MULTIPOINT; }
	| LINESTRING          { $$= Field::GEOM_LINESTRING; }
	| MULTILINESTRING     { $$= Field::GEOM_MULTILINESTRING; }
	| POLYGON             { $$= Field::GEOM_POLYGON; }
	| MULTIPOLYGON        { $$= Field::GEOM_MULTIPOLYGON; }
	;

unknown's avatar
unknown committed
3461 3462
char:
	CHAR_SYM {}
unknown's avatar
unknown committed
3463 3464 3465 3466 3467 3468
	;

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

unknown's avatar
unknown committed
3470 3471 3472
varchar:
	char VARYING {}
	| VARCHAR {}
unknown's avatar
unknown committed
3473 3474 3475 3476
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
unknown's avatar
unknown committed
3477
	| NVARCHAR_SYM {}
unknown's avatar
unknown committed
3478 3479 3480 3481
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
unknown's avatar
unknown committed
3482 3483 3484 3485 3486 3487

int_type:
	INT_SYM		{ $$=FIELD_TYPE_LONG; }
	| TINYINT	{ $$=FIELD_TYPE_TINY; }
	| SMALLINT	{ $$=FIELD_TYPE_SHORT; }
	| MEDIUMINT	{ $$=FIELD_TYPE_INT24; }
unknown's avatar
unknown committed
3488
	| BIGINT	{ $$=FIELD_TYPE_LONGLONG; };
unknown's avatar
unknown committed
3489 3490

real_type:
3491
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
unknown's avatar
unknown committed
3492 3493
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
unknown's avatar
unknown committed
3494
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
unknown's avatar
unknown committed
3495 3496 3497


float_options:
unknown's avatar
unknown committed
3498 3499
        /* empty */		{ Lex->dec=Lex->length= (char*)0; }
        | '(' NUM ')'		{ Lex->length=$2.str; Lex->dec= (char*)0; }
unknown's avatar
unknown committed
3500
	| precision		{};
unknown's avatar
unknown committed
3501 3502 3503 3504 3505 3506

precision:
	'(' NUM ',' NUM ')'
	{
	  LEX *lex=Lex;
	  lex->length=$2.str; lex->dec=$4.str;
unknown's avatar
unknown committed
3507
	};
unknown's avatar
unknown committed
3508 3509 3510

field_options:
	/* empty */		{}
unknown's avatar
unknown committed
3511
	| field_opt_list	{};
unknown's avatar
unknown committed
3512 3513 3514

field_opt_list:
	field_opt_list field_option {}
unknown's avatar
unknown committed
3515
	| field_option {};
unknown's avatar
unknown committed
3516 3517

field_option:
unknown's avatar
unknown committed
3518
	SIGNED_SYM	{}
3519
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
unknown's avatar
unknown committed
3520
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
unknown's avatar
unknown committed
3521 3522

opt_len:
3523 3524
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
unknown's avatar
unknown committed
3525 3526 3527

opt_precision:
	/* empty */	{}
unknown's avatar
unknown committed
3528
	| precision	{};
unknown's avatar
unknown committed
3529 3530 3531

opt_attribute:
	/* empty */ {}
unknown's avatar
unknown committed
3532
	| opt_attribute_list {};
unknown's avatar
unknown committed
3533 3534 3535

opt_attribute_list:
	opt_attribute_list attribute {}
unknown's avatar
unknown committed
3536
	| attribute;
unknown's avatar
unknown committed
3537 3538 3539

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
3540
	| not NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
3541 3542 3543
	| 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
3544
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
3545
	| SERIAL_SYM DEFAULT VALUE_SYM
3546 3547 3548
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
3549
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3550 3551 3552 3553 3554
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
3555
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3556 3557 3558 3559 3560
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
3561
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3562 3563 3564 3565 3566
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
3567
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3568
	  }
3569
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= $2; }
3570
	| BINARY { Lex->type|= BINCMP_FLAG; }
unknown's avatar
unknown committed
3571 3572
	| COLLATE_SYM collation_name
	  {
unknown's avatar
unknown committed
3573
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
unknown's avatar
unknown committed
3574
	    {
3575 3576
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $2->name,Lex->charset->csname);
unknown's avatar
unknown committed
3577 3578 3579 3580 3581 3582 3583 3584
	      YYABORT;
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
unknown's avatar
unknown committed
3585

3586 3587 3588 3589 3590
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

3591 3592 3593 3594
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
unknown's avatar
unknown committed
3595

3596
charset_name:
3597
	ident_or_text
3598
	{
unknown's avatar
unknown committed
3599
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
3600
	  {
3601
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
3602 3603
	    YYABORT;
	  }
unknown's avatar
unknown committed
3604 3605 3606
	}
	| BINARY { $$= &my_charset_bin; }
	;
3607

3608 3609 3610
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
unknown's avatar
unknown committed
3611

unknown's avatar
unknown committed
3612 3613 3614 3615 3616 3617 3618

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)))
	  {
3619
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
unknown's avatar
unknown committed
3620 3621 3622 3623 3624 3625 3626 3627 3628 3629
	    YYABORT;
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

3630
collation_name:
3631
	ident_or_text
3632 3633 3634
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
3635
	    my_error(ER_UNKNOWN_COLLATION, MYF(0), $1.str);
3636 3637 3638 3639
	    YYABORT;
	  }
	};

3640 3641
opt_collate:
	/* empty */	{ $$=NULL; }
unknown's avatar
unknown committed
3642
	| COLLATE_SYM collation_name_or_default { $$=$2; }
3643 3644
	;

3645 3646 3647 3648
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

3649 3650 3651 3652
opt_default:
	/* empty */	{}
	| DEFAULT	{};

unknown's avatar
unknown committed
3653
opt_binary:
3654
	/* empty */			{ Lex->charset=NULL; }
unknown's avatar
unknown committed
3655 3656
	| ASCII_SYM			{ Lex->charset=&my_charset_latin1; }
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
unknown's avatar
unknown committed
3657 3658
	| UNICODE_SYM
	{
unknown's avatar
unknown committed
3659 3660
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
unknown's avatar
unknown committed
3661
	  {
unknown's avatar
unknown committed
3662
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
unknown's avatar
unknown committed
3663 3664 3665
	    YYABORT;
	  }
	}
3666
	| charset charset_name	{ Lex->charset=$2; } ;
unknown's avatar
unknown committed
3667

unknown's avatar
unknown committed
3668 3669 3670
opt_primary:
	/* empty */
	| PRIMARY_SYM
3671
	;
unknown's avatar
unknown committed
3672

unknown's avatar
unknown committed
3673
references:
3674 3675
	REFERENCES table_ident
	{
3676
	  LEX *lex=Lex;
3677 3678 3679 3680 3681 3682
	  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
3683
	};
3684

3685
opt_ref_list:
unknown's avatar
unknown committed
3686
	/* empty */ opt_on_delete {}
unknown's avatar
unknown committed
3687
	| '(' ref_list ')' opt_on_delete {};
3688 3689 3690

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

unknown's avatar
unknown committed
3693 3694 3695

opt_on_delete:
	/* empty */ {}
unknown's avatar
unknown committed
3696
	| opt_on_delete_list {};
unknown's avatar
unknown committed
3697 3698 3699

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
unknown's avatar
unknown committed
3700
	| opt_on_delete_item {};
unknown's avatar
unknown committed
3701 3702

opt_on_delete_item:
3703 3704 3705 3706
	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
3707
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
unknown's avatar
unknown committed
3708 3709

delete_option:
3710 3711 3712 3713
	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
3714
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
unknown's avatar
unknown committed
3715 3716

key_type:
3717
	key_or_index			    { $$= Key::MULTIPLE; }
unknown's avatar
SCRUM  
unknown committed
3718 3719
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
unknown's avatar
unknown committed
3720 3721 3722 3723
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
3724 3725
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3726 3727 3728
	    YYABORT;
#endif
	  };
3729 3730 3731

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
unknown's avatar
SCRUM  
unknown committed
3732
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
unknown's avatar
unknown committed
3733 3734 3735

key_or_index:
	KEY_SYM {}
unknown's avatar
unknown committed
3736
	| INDEX_SYM {};
unknown's avatar
unknown committed
3737

unknown's avatar
SCRUM  
unknown committed
3738 3739 3740 3741 3742
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

unknown's avatar
unknown committed
3743 3744
keys_or_index:
	KEYS {}
unknown's avatar
unknown committed
3745
	| INDEX_SYM {}
unknown's avatar
unknown committed
3746
	| INDEXES {};
unknown's avatar
unknown committed
3747

3748
opt_unique_or_fulltext:
unknown's avatar
unknown committed
3749 3750
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
unknown's avatar
unknown committed
3751
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
unknown's avatar
unknown committed
3752 3753 3754 3755 3756
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
unknown's avatar
unknown committed
3757
            my_error(ER_FEATURE_DISABLED, MYF(0),
unknown's avatar
unknown committed
3758
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3759 3760 3761
	    YYABORT;
#endif
	  }
unknown's avatar
unknown committed
3762
        ;
unknown's avatar
unknown committed
3763 3764

key_alg:
3765
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
3766
	| USING opt_btree_or_rtree { $$= $2; }
unknown's avatar
unknown committed
3767
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
unknown's avatar
unknown committed
3768 3769 3770

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
unknown's avatar
unknown committed
3771 3772 3773 3774
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
unknown's avatar
unknown committed
3775
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
unknown's avatar
unknown committed
3776 3777 3778

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
unknown's avatar
unknown committed
3779
	| key_part order_dir		{ Lex->col_list.push_back($1); };
unknown's avatar
unknown committed
3780 3781 3782

key_part:
	ident			{ $$=new key_part_spec($1.str); }
3783 3784 3785 3786 3787
	| ident '(' NUM ')'	
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
3788
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
3789 3790 3791
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
unknown's avatar
unknown committed
3792 3793 3794

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

3797
opt_component:
3798 3799
        /* empty */      { $$= null_lex_str; }
        | '.' ident      { $$= $2; };
3800

unknown's avatar
unknown committed
3801 3802
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
unknown's avatar
unknown committed
3803
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
unknown's avatar
unknown committed
3804 3805 3806 3807 3808 3809 3810 3811

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
3812
	  THD *thd= YYTHD;
3813
	  LEX *lex= thd->lex;
3814 3815 3816 3817 3818
          if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
          {
            my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
            YYABORT;
          }
3819 3820 3821
	  lex->sql_command= SQLCOM_ALTER_TABLE;
	  lex->name= 0;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
3822 3823
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
unknown's avatar
unknown committed
3824 3825 3826 3827
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
3828
          lex->select_lex.init_order();
3829
	  lex->select_lex.db=lex->name=0;
3830
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
unknown's avatar
unknown committed
3831
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
3832
	  lex->create_info.default_table_charset= NULL;
3833
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
3834
	  lex->alter_info.reset();
3835
	  lex->alter_info.flags= 0;
unknown's avatar
unknown committed
3836
	}
3837
	alter_commands
3838
	{}
3839
	| ALTER DATABASE ident_or_empty
3840 3841 3842 3843 3844
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
          opt_create_database_options
3845 3846 3847
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
3848
	    lex->name= $3;
3849 3850 3851 3852 3853
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

3854 3855 3856 3857 3858
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
	      YYABORT;
	    }
3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870
	    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;
3871

3872 3873 3874 3875 3876
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
	      YYABORT;
	    }
3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    LEX *lex=Lex;

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

3899 3900 3901 3902
ident_or_empty:
	/* empty */  { $$= 0; }
	| ident      { $$= $1.str; };

3903
alter_commands:
3904 3905
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
3906 3907 3908
        | alter_list
        opt_partitioning
        | partitioning
unknown's avatar
unknown committed
3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928
/*
  This part was added for release 5.1 by Mikael Ronstrm.
  From here we insert a number of commands to manage the partitions of a
  partitioned table such as adding partitions, dropping partitions,
  reorganising partitions in various manners. In future releases the list
  will be longer and also include moving partitions to a
  new table and so forth.
*/
        | add_partition_rule
        | DROP PARTITION_SYM alt_part_name_list
          {
	    Lex->alter_info.flags|= ALTER_DROP_PARTITION;
          }
        | COALESCE PARTITION_SYM ulong_num
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_COALESCE_PARTITION;
	    lex->alter_info.no_parts= $3;
          }
        | reorg_partition_rule
3929 3930
        ;

unknown's avatar
unknown committed
3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994
add_partition_rule:
        ADD PARTITION_SYM
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
            my_error(ER_OUTOFMEMORY, MYF(0), sizeof(partition_info));
            YYABORT;
          }
	  lex->alter_info.flags|= ALTER_ADD_PARTITION;
        }
        add_part_extra
        {}
        ;

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

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

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

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

/*
  End of management of partition commands
*/

3995 3996 3997 3998
alter_list:
        alter_list_item
	| alter_list ',' alter_list_item
        ;
unknown's avatar
unknown committed
3999 4000

add_column:
4001
	ADD opt_column
4002 4003
	{
	  LEX *lex=Lex;
4004 4005
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
4006
	};
unknown's avatar
unknown committed
4007 4008

alter_list_item:
4009 4010 4011 4012
	add_column column_def opt_place { }
	| ADD key_def
	  {
	    Lex->alter_info.flags|= ALTER_ADD_INDEX;
4013
	  }
4014 4015 4016 4017
	| add_column '(' field_list ')'
          {
	    Lex->alter_info.flags|= ALTER_ADD_COLUMN | ALTER_ADD_INDEX;
          }
unknown's avatar
unknown committed
4018 4019 4020
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
4021
	     lex->change= $3.str;
4022
	     lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
4023
	  }
4024
          field_spec opt_place
unknown's avatar
unknown committed
4025 4026 4027
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
4028
            lex->length=lex->dec=0; lex->type=0;
4029
            lex->default_value= lex->on_update_value= 0;
4030
            lex->comment=null_lex_str;
4031
	    lex->charset= NULL;
4032
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
4033 4034 4035 4036
          }
          type opt_attribute
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
4037
            if (add_field_to_list(lex->thd,$3.str,
unknown's avatar
unknown committed
4038 4039
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
4040
                                  lex->default_value, lex->on_update_value,
4041
                                  &lex->comment,
4042
				  $3.str, &lex->interval_list, lex->charset,
unknown's avatar
unknown committed
4043
				  lex->uint_geom_type))
unknown's avatar
unknown committed
4044 4045 4046
	       YYABORT;
          }
          opt_place
unknown's avatar
unknown committed
4047
	| DROP opt_column field_ident opt_restrict
unknown's avatar
unknown committed
4048 4049
	  {
	    LEX *lex=Lex;
4050
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
4051
                                                               $3.str));
4052
	    lex->alter_info.flags|= ALTER_DROP_COLUMN;
unknown's avatar
unknown committed
4053
	  }
4054 4055 4056 4057
	| DROP FOREIGN KEY_SYM opt_ident
          {
	    Lex->alter_info.flags|= ALTER_DROP_INDEX;
          }
unknown's avatar
unknown committed
4058 4059 4060
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
4061 4062 4063
	    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
4064
	  }
unknown's avatar
unknown committed
4065
	| DROP key_or_index field_ident
unknown's avatar
unknown committed
4066 4067
	  {
	    LEX *lex=Lex;
4068 4069 4070
	    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
4071
	  }
4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083
	| 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
4084
	| ALTER opt_column field_ident SET DEFAULT signed_literal
unknown's avatar
unknown committed
4085 4086
	  {
	    LEX *lex=Lex;
4087
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,$6));
4088
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
4089
	  }
unknown's avatar
unknown committed
4090
	| ALTER opt_column field_ident DROP DEFAULT
unknown's avatar
unknown committed
4091 4092
	  {
	    LEX *lex=Lex;
4093 4094
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,
                                                                  (Item*) 0));
4095
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
4096
	  }
4097
	| RENAME opt_to table_ident
4098
	  {
4099
	    LEX *lex=Lex;
4100
	    lex->select_lex.db=$3->db.str;
4101
	    lex->name= $3->table.str;
4102 4103 4104
            if (check_table_name($3->table.str,$3->table.length) ||
                $3->db.str && check_db_name($3->db.str))
            {
4105
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3->table.str);
4106 4107
              YYABORT;
            }
4108
	    lex->alter_info.flags|= ALTER_RENAME;
4109
	  }
4110 4111 4112 4113 4114 4115 4116 4117 4118 4119
	| 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))
	    {
4120 4121
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $5->name, $4->csname);
4122 4123
	      YYABORT;
	    }
4124
	    LEX *lex= Lex;
4125
	    lex->create_info.table_charset=
unknown's avatar
unknown committed
4126 4127 4128
	      lex->create_info.default_table_charset= $5;
	    lex->create_info.used_fields|= (HA_CREATE_USED_CHARSET |
					    HA_CREATE_USED_DEFAULT_CHARSET);
4129
	    lex->alter_info.flags|= ALTER_CONVERT;
4130
	  }
4131
        | create_table_options_space_separated
4132 4133
	  {
	    LEX *lex=Lex;
4134
	    lex->alter_info.flags|= ALTER_OPTIONS;
4135
	  }
4136 4137 4138 4139
	| FORCE_SYM
	  {
	    Lex->alter_info.flags|= ALTER_FORCE;
	   }
4140
	| order_clause
4141 4142
	  {
	    LEX *lex=Lex;
4143
	    lex->alter_info.flags|= ALTER_ORDER;
4144
	  };
unknown's avatar
unknown committed
4145 4146 4147

opt_column:
	/* empty */	{}
unknown's avatar
unknown committed
4148
	| COLUMN_SYM	{};
unknown's avatar
unknown committed
4149 4150

opt_ignore:
4151 4152 4153
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
unknown's avatar
unknown committed
4154 4155

opt_restrict:
4156 4157 4158 4159
	/* empty */	{ Lex->drop_mode= DROP_DEFAULT; }
	| RESTRICT	{ Lex->drop_mode= DROP_RESTRICT; }
	| CASCADE	{ Lex->drop_mode= DROP_CASCADE; }
	;
unknown's avatar
unknown committed
4160 4161 4162 4163

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
unknown's avatar
unknown committed
4164
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
unknown's avatar
unknown committed
4165 4166 4167 4168

opt_to:
	/* empty */	{}
	| TO_SYM	{}
4169
	| EQ		{}
unknown's avatar
unknown committed
4170
	| AS		{};
unknown's avatar
unknown committed
4171

unknown's avatar
unknown committed
4172
/*
4173
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
unknown's avatar
unknown committed
4174 4175
*/

unknown's avatar
unknown committed
4176
slave:
4177
	  START_SYM SLAVE slave_thread_opts
4178 4179 4180 4181 4182 4183
          {
	    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
4184
            /* If you change this code don't forget to update SLAVE START too */
4185 4186 4187
          }
          slave_until
          {}
unknown's avatar
unknown committed
4188 4189 4190 4191 4192
        | STOP_SYM SLAVE slave_thread_opts
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_STOP;
	    lex->type = 0;
unknown's avatar
unknown committed
4193
            /* If you change this code don't forget to update SLAVE STOP too */
unknown's avatar
unknown committed
4194
          }
4195 4196 4197 4198 4199
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
4200 4201 4202 4203 4204
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
4205 4206 4207 4208 4209 4210 4211 4212
	| SLAVE STOP_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_STOP;
	   lex->type = 0;
         }
        ;

unknown's avatar
unknown committed
4213

unknown's avatar
unknown committed
4214
start:
4215 4216
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
4217 4218 4219 4220 4221 4222 4223 4224
          LEX *lex= Lex;
          if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
          {
            my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
            YYABORT;
          }
          lex->sql_command= SQLCOM_BEGIN;
          lex->start_transaction_opt= $3;
4225
        }
unknown's avatar
unknown committed
4226 4227
	;

4228 4229 4230 4231 4232 4233
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
4234
        ;
4235

unknown's avatar
unknown committed
4236
slave_thread_opts:
4237 4238
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
4239
        {}
unknown's avatar
unknown committed
4240
	;
4241 4242

slave_thread_opt_list:
unknown's avatar
unknown committed
4243
	slave_thread_opt
4244 4245
	| slave_thread_opt_list ',' slave_thread_opt
	;
4246 4247

slave_thread_opt:
4248
	/*empty*/	{}
unknown's avatar
unknown committed
4249
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
unknown's avatar
unknown committed
4250
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
unknown's avatar
unknown committed
4251
	;
4252

4253 4254 4255 4256 4257 4258 4259 4260 4261 4262
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
4263 4264
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


unknown's avatar
unknown committed
4276 4277 4278 4279 4280
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
	}
unknown's avatar
unknown committed
4281
	table_list FROM TEXT_STRING_sys
unknown's avatar
unknown committed
4282 4283
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
4284
        };
unknown's avatar
unknown committed
4285

unknown's avatar
unknown committed
4286 4287 4288 4289 4290
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
	}
unknown's avatar
unknown committed
4291
	table_list TO_SYM TEXT_STRING_sys
unknown's avatar
unknown committed
4292 4293
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
4294
        };
unknown's avatar
unknown committed
4295

4296 4297 4298 4299 4300 4301
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
4302 4303
	table_list opt_checksum_type
        {}
4304 4305
	;

4306 4307 4308 4309 4310 4311
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
4312
repair:
4313
	REPAIR opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
4314
	{
unknown's avatar
unknown committed
4315 4316
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
4317
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
4318
	   lex->check_opt.init();
unknown's avatar
unknown committed
4319
	}
4320 4321 4322
	table_list opt_mi_repair_type
	{}
	;
unknown's avatar
unknown committed
4323

unknown's avatar
unknown committed
4324
opt_mi_repair_type:
unknown's avatar
unknown committed
4325
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
4326
	| mi_repair_types {};
unknown's avatar
unknown committed
4327

unknown's avatar
unknown committed
4328 4329
mi_repair_types:
	mi_repair_type {}
unknown's avatar
unknown committed
4330
	| mi_repair_type mi_repair_types {};
4331

unknown's avatar
unknown committed
4332
mi_repair_type:
unknown's avatar
unknown committed
4333
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
4334
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
unknown's avatar
unknown committed
4335
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
unknown's avatar
unknown committed
4336 4337

analyze:
4338
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
4339
	{
unknown's avatar
unknown committed
4340 4341
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
4342
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
4343
	   lex->check_opt.init();
unknown's avatar
unknown committed
4344
	}
4345 4346 4347
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
4348 4349

check:
unknown's avatar
unknown committed
4350
	CHECK_SYM table_or_tables
unknown's avatar
unknown committed
4351
	{
4352 4353 4354 4355 4356 4357 4358 4359 4360
	  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
4361
	}
4362 4363 4364
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
4365

unknown's avatar
unknown committed
4366 4367
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
4368
	| mi_check_types {};
unknown's avatar
unknown committed
4369 4370 4371

mi_check_types:
	mi_check_type {}
unknown's avatar
unknown committed
4372
	| mi_check_type mi_check_types {};
unknown's avatar
unknown committed
4373 4374 4375 4376 4377 4378

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

unknown's avatar
unknown committed
4381
optimize:
4382
	OPTIMIZE opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
4383
	{
unknown's avatar
unknown committed
4384 4385
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
unknown's avatar
unknown committed
4386
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
4387
	   lex->check_opt.init();
unknown's avatar
unknown committed
4388
	}
4389 4390 4391
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
4392

4393 4394 4395
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
4396
	| LOCAL_SYM  { $$= 1; }
4397 4398
	;

4399 4400 4401
rename:
	RENAME table_or_tables
	{
4402 4403 4404 4405 4406 4407 4408
          LEX *lex= Lex;
          if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
          {
            my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
            YYABORT;
          }
          lex->sql_command=SQLCOM_RENAME_TABLE;
4409
	}
4410 4411
	table_to_table_list
	{}
4412 4413 4414 4415
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
4416
	;
4417

4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430
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;
          }
        ;

4431 4432
table_to_table_list:
	table_to_table
unknown's avatar
unknown committed
4433
	| table_to_table_list ',' table_to_table;
4434 4435 4436

table_to_table:
	table_ident TO_SYM table_ident
4437
	{
unknown's avatar
unknown committed
4438
	  LEX *lex=Lex;
unknown's avatar
unknown committed
4439
	  SELECT_LEX *sl= lex->current_select;
unknown's avatar
unknown committed
4440 4441 4442 4443
	  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))
4444
	    YYABORT;
4445
	};
4446

unknown's avatar
unknown committed
4447
keycache:
unknown's avatar
unknown committed
4448
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
unknown's avatar
unknown committed
4449 4450
        {
          LEX *lex=Lex;
4451
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
4452
	  lex->ident= $5;
unknown's avatar
unknown committed
4453 4454 4455 4456 4457 4458 4459 4460
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
4461
        table_ident cache_keys_spec
unknown's avatar
unknown committed
4462 4463 4464 4465 4466 4467
        {
          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(),
4468
                                      (List<String> *)0))
unknown's avatar
unknown committed
4469 4470 4471 4472
            YYABORT;
        }
        ;

4473 4474 4475
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
4476
	;
4477

unknown's avatar
unknown committed
4478
preload:
unknown's avatar
unknown committed
4479
	LOAD INDEX_SYM INTO CACHE_SYM
unknown's avatar
unknown committed
4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492
	{
	  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
4493
	table_ident cache_keys_spec opt_ignore_leaves
unknown's avatar
unknown committed
4494 4495 4496
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
unknown's avatar
unknown committed
4497 4498
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
unknown's avatar
unknown committed
4499 4500 4501 4502 4503 4504
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

4505
cache_keys_spec:
4506
        { Select->interval_list.empty(); }
4507 4508 4509 4510 4511 4512 4513
        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
4514

unknown's avatar
unknown committed
4515
cache_key_list_or_empty:
4516
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
4517
	| opt_key_or_index '(' key_usage_list2 ')'
4518 4519 4520 4521
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
4522 4523
	;

unknown's avatar
unknown committed
4524
opt_ignore_leaves:
unknown's avatar
unknown committed
4525 4526 4527 4528 4529
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

unknown's avatar
unknown committed
4530
/*
unknown's avatar
unknown committed
4531
  Select : retrieve data from table
unknown's avatar
unknown committed
4532 4533 4534 4535
*/


select:
4536 4537 4538 4539
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
unknown's avatar
unknown committed
4540 4541
	}
	;
unknown's avatar
unknown committed
4542

4543
/* Need select_init2 for subselects. */
unknown's avatar
unknown committed
4544
select_init:
4545
	SELECT_SYM select_init2
4546
	|
unknown's avatar
unknown committed
4547 4548 4549 4550
	'(' select_paren ')' union_opt;

select_paren:
	SELECT_SYM select_part2
4551
	  {
4552
	    LEX *lex= Lex;
unknown's avatar
unknown committed
4553
            SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
4554
	    if (sel->set_braces(1))
4555
	    {
4556
	      yyerror(ER(ER_SYNTAX_ERROR));
4557 4558
	      YYABORT;
	    }
4559 4560
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
4561
	  {
4562
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
4563 4564
	    YYABORT;
	  }
4565
            /* select in braces, can't contain global parameters */
unknown's avatar
unknown committed
4566 4567 4568
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
unknown's avatar
unknown committed
4569 4570
          }
	| '(' select_paren ')';
4571

4572 4573
select_init2:
	select_part2
4574
        {
4575
	  LEX *lex= Lex;
unknown's avatar
unknown committed
4576
          SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
4577
          if (lex->current_select->set_braces(0))
4578
	  {
4579
	    yyerror(ER(ER_SYNTAX_ERROR));
4580 4581
	    YYABORT;
	  }
4582 4583
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
4584
	  {
4585
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
4586 4587
	    YYABORT;
	  }
4588
	}
4589
	union_clause
4590 4591
	;

4592
select_part2:
unknown's avatar
unknown committed
4593
	{
unknown's avatar
unknown committed
4594 4595
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
4596 4597
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
4598
	  lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
4599 4600 4601
	}
	select_options select_item_list
	{
4602
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
4603
	}
unknown's avatar
unknown committed
4604
	select_into select_lock_type;
4605

unknown's avatar
unknown committed
4606
select_into:
unknown's avatar
unknown committed
4607
	opt_order_clause opt_limit_clause {}
4608
        | into
unknown's avatar
unknown committed
4609
	| select_from
4610 4611
	| into select_from
	| select_from into;
unknown's avatar
unknown committed
4612 4613

select_from:
4614 4615
	  FROM join_table_list where_clause group_clause having_clause
	       opt_order_clause opt_limit_clause procedure_clause
unknown's avatar
unknown committed
4616
        | FROM DUAL_SYM where_clause opt_limit_clause
unknown's avatar
unknown committed
4617 4618 4619 4620
          /* 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 :) */
4621
	;
unknown's avatar
unknown committed
4622 4623 4624

select_options:
	/* empty*/
unknown's avatar
unknown committed
4625 4626
	| select_option_list
	  {
unknown's avatar
unknown committed
4627
	    if (Select->options & SELECT_DISTINCT && Select->options & SELECT_ALL)
unknown's avatar
unknown committed
4628
	    {
unknown's avatar
unknown committed
4629
	      my_error(ER_WRONG_USAGE, MYF(0), "ALL", "DISTINCT");
unknown's avatar
unknown committed
4630 4631 4632
              YYABORT;
	    }
          }
unknown's avatar
unknown committed
4633
	  ;
unknown's avatar
unknown committed
4634 4635 4636

select_option_list:
	select_option_list select_option
unknown's avatar
unknown committed
4637
	| select_option;
unknown's avatar
unknown committed
4638 4639

select_option:
4640
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
4641 4642 4643 4644 4645 4646
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
unknown's avatar
unknown committed
4647
	| DISTINCT         { Select->options|= SELECT_DISTINCT; }
4648 4649
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661
	| 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;
	  }
4662
	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
4663 4664 4665 4666
	| SQL_CACHE_SYM
	  {
	    Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
	  }
unknown's avatar
unknown committed
4667
	| ALL		    { Select->options|= SELECT_ALL; }
4668
	;
unknown's avatar
unknown committed
4669

unknown's avatar
unknown committed
4670 4671 4672
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
4673 4674
	  {
	    LEX *lex=Lex;
4675
	    lex->current_select->set_lock_for_tables(TL_WRITE);
4676
	    lex->safe_to_cache_query=0;
4677
	  }
unknown's avatar
unknown committed
4678
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
4679 4680
	  {
	    LEX *lex=Lex;
4681 4682
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
4683
	    lex->safe_to_cache_query=0;
4684 4685
	  }
	;
unknown's avatar
unknown committed
4686

unknown's avatar
unknown committed
4687 4688 4689 4690 4691
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
4692
	    THD *thd= YYTHD;
4693 4694 4695 4696
	    if (add_item_to_list(thd,
                                 new Item_field(&thd->lex->current_select->
                                                context,
                                                NULL, NULL, "*")))
unknown's avatar
unknown committed
4697
	      YYABORT;
4698
	    (thd->lex->current_select->with_wild)++;
unknown's avatar
unknown committed
4699
	  };
unknown's avatar
unknown committed
4700 4701 4702 4703 4704


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
unknown's avatar
unknown committed
4705
	    if (add_item_to_list(YYTHD, $2))
unknown's avatar
unknown committed
4706 4707
	      YYABORT;
	    if ($4.str)
4708 4709 4710 4711
            {
	      $2->set_name($4.str, $4.length, system_charset_info);
              $2->is_autogenerated_name= FALSE;
            }
4712 4713 4714 4715 4716 4717
	    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
4718
	  };
unknown's avatar
unknown committed
4719 4720

remember_name:
unknown's avatar
unknown committed
4721
	{ $$=(char*) Lex->tok_start; };
unknown's avatar
unknown committed
4722 4723

remember_end:
unknown's avatar
unknown committed
4724
	{ $$=(char*) Lex->tok_end; };
unknown's avatar
unknown committed
4725 4726 4727

select_item2:
	table_wild	{ $$=$1; } /* table.* */
unknown's avatar
unknown committed
4728
	| expr		{ $$=$1; };
unknown's avatar
unknown committed
4729 4730

select_alias:
4731
	/* empty */		{ $$=null_lex_str;}
unknown's avatar
unknown committed
4732 4733 4734 4735 4736
	| AS ident		{ $$=$2; }
	| AS TEXT_STRING_sys	{ $$=$2; }
	| ident			{ $$=$1; }
	| TEXT_STRING_sys	{ $$=$1; }
	;
unknown's avatar
unknown committed
4737 4738 4739

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

unknown's avatar
unknown committed
4742
/* all possible expressions */
unknown's avatar
SCRUM  
unknown committed
4743
expr:	
unknown's avatar
unknown committed
4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765
	  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); }
        ;
4766 4767

bool_term:
unknown's avatar
unknown committed
4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790
	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); }
        ;
4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807

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); }
4808 4809 4810 4811 4812
	| 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); }
4813 4814 4815 4816
	| predicate ;

predicate:
	 bit_expr IN_SYM '(' expr_list ')'
unknown's avatar
unknown committed
4817 4818 4819 4820 4821 4822 4823 4824 4825
	  { 
            if ($4->elements == 1)
              $$= new Item_func_eq($1, $4->head());
            else
            {
              $4->push_front($1);
              $$= new Item_func_in(*$4);
            }
          }
4826
	| bit_expr not IN_SYM '(' expr_list ')'
unknown's avatar
unknown committed
4827 4828 4829 4830 4831 4832 4833 4834 4835
          {
            if ($5->elements == 1)
              $$= new Item_func_ne($1, $5->head());
            else
            {
              $5->push_front($1);
              $$= negate_expression(YYTHD, new Item_func_in(*$5));
            }            
          }
4836 4837 4838
        | bit_expr IN_SYM in_subselect
	  { $$= new Item_in_subselect($1, $3); }
	| bit_expr not IN_SYM in_subselect
4839
          { $$= negate_expression(YYTHD, new Item_in_subselect($1, $4)); }
4840 4841 4842 4843
	| bit_expr BETWEEN_SYM bit_expr AND_SYM predicate
	  { $$= new Item_func_between($1,$3,$5); }
	| bit_expr not BETWEEN_SYM bit_expr AND_SYM predicate
	  { $$= negate_expression(YYTHD, new Item_func_between($1,$4,$6)); }
4844 4845 4846 4847 4848 4849 4850 4851 4852
	| bit_expr SOUNDS_SYM LIKE bit_expr
	  { $$= new Item_func_eq(new Item_func_soundex($1),
				 new Item_func_soundex($4)); }
	| bit_expr LIKE simple_expr opt_escape
          { $$= new Item_func_like($1,$3,$4); }
	| bit_expr not LIKE simple_expr opt_escape
          { $$= new Item_func_not(new Item_func_like($1,$4,$5)); }
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
4853
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895
	| 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
4896

unknown's avatar
unknown committed
4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908
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; }
        ;

4909
interval_expr:
unknown's avatar
unknown committed
4910
         INTERVAL_SYM expr { $$=$2; }
4911 4912
        ;

unknown's avatar
unknown committed
4913 4914
simple_expr:
	simple_ident
4915
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
unknown's avatar
unknown committed
4916
	  {
unknown's avatar
unknown committed
4917 4918 4919
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
4920
                                                            YYTHD->charset()));
4921
	  }
unknown's avatar
unknown committed
4922
	| literal
unknown's avatar
unknown committed
4923
	| param_marker
unknown's avatar
unknown committed
4924
	| '@' ident_or_text SET_VAR expr
unknown's avatar
unknown committed
4925 4926
	  {
	    $$= new Item_func_set_user_var($2,$4);
4927 4928 4929
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4930
	  }
4931
	| '@' ident_or_text
unknown's avatar
unknown committed
4932 4933
	  {
	    $$= new Item_func_get_user_var($2);
4934 4935 4936
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4937
	  }
4938
	| '@' '@' opt_var_ident_type ident_or_text opt_component
unknown's avatar
unknown committed
4939
	  {
4940 4941 4942 4943 4944 4945

            if ($4.str && $5.str && check_reserved_words(&$4))
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
4946
	    if (!($$= get_system_var(YYTHD, (enum_var_type) $3, $4, $5)))
unknown's avatar
unknown committed
4947
	      YYABORT;
4948
	    Lex->variables_used= 1;
unknown's avatar
unknown committed
4949
	  }
unknown's avatar
unknown committed
4950
	| sum_expr
4951 4952 4953 4954 4955 4956
	| 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
4957
	| '(' expr ')'		{ $$= $2; }
4958 4959 4960 4961 4962
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
4963
	| ROW_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
4964
	  {
4965 4966
	    $5->push_front($3);
	    $$= new Item_row(*$5);
unknown's avatar
unknown committed
4967
	  }
unknown's avatar
unknown committed
4968
	| EXISTS exists_subselect { $$= $2; }
unknown's avatar
unknown committed
4969
	| singlerow_subselect   { $$= $1; }
unknown's avatar
unknown committed
4970
	| '{' ident expr '}'	{ $$= $3; }
4971
        | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
4972
          { $2->push_front($5);
4973 4974
            Select->add_ftfunc_to_list((Item_func_match*)
                                        ($$=new Item_func_match(*$2,$6))); }
unknown's avatar
unknown committed
4975
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
4976
	| BINARY simple_expr %prec NEG
unknown's avatar
unknown committed
4977
	  {
unknown's avatar
unknown committed
4978
            $$= create_func_cast($2, ITEM_CAST_CHAR, -1, 0, &my_charset_bin);
unknown's avatar
unknown committed
4979
	  }
4980
	| CAST_SYM '(' expr AS cast_type ')'
4981
	  {
unknown's avatar
unknown committed
4982
            LEX *lex= Lex;
4983
	    $$= create_func_cast($3, $5,
unknown's avatar
unknown committed
4984 4985 4986
                                 lex->length ? atoi(lex->length) : -1,
                                 lex->dec ? atoi(lex->dec) : 0,
                                 lex->charset);
4987
	  }
unknown's avatar
unknown committed
4988
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
4989
	  { $$= new Item_func_case(* $4, $2, $5 ); }
4990
	| CONVERT_SYM '(' expr ',' cast_type ')'
4991 4992 4993
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
unknown's avatar
unknown committed
4994
                                 Lex->dec ? atoi(Lex->dec) : 0,
4995 4996
				 Lex->charset);
	  }
4997 4998
	| CONVERT_SYM '(' expr USING charset_name ')'
	  { $$= new Item_func_conv_charset($3,$5); }
unknown's avatar
SCRUM  
unknown committed
4999
	| DEFAULT '(' simple_ident ')'
5000 5001 5002
	  {
	    if ($3->is_splocal())
	    {
unknown's avatar
unknown committed
5003
	      LEX_STRING *name;
5004 5005
	      Item_splocal *il= static_cast<Item_splocal *>($3);

unknown's avatar
unknown committed
5006 5007
	      name= il->my_name(NULL);
	      my_error(ER_WRONG_COLUMN_NAME, MYF(0), name->str);
5008 5009
	      YYABORT;
	    }
5010
	    $$= new Item_default_value(&Select->context, $3);
5011
	  }
unknown's avatar
unknown committed
5012
	| VALUES '(' simple_ident ')'
5013
	  { $$= new Item_insert_value(&Select->context, $3); }
unknown's avatar
unknown committed
5014
	| FUNC_ARG0 '(' ')'
unknown's avatar
unknown committed
5015 5016 5017
	  {
	    if (!$1.symbol->create_func)
	    {
5018 5019 5020
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
5021 5022 5023 5024
	      YYABORT;
	    }
	    $$= ((Item*(*)(void))($1.symbol->create_func))();
	  }
unknown's avatar
unknown committed
5025
	| FUNC_ARG1 '(' expr ')'
unknown's avatar
unknown committed
5026 5027 5028
	  {
	    if (!$1.symbol->create_func)
	    {
5029 5030 5031
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
5032 5033 5034 5035
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);
	  }
unknown's avatar
unknown committed
5036
	| FUNC_ARG2 '(' expr ',' expr ')'
unknown's avatar
unknown committed
5037 5038 5039
	  {
	    if (!$1.symbol->create_func)
	    {
5040 5041 5042
	      my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
5043 5044 5045 5046
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);
	  }
unknown's avatar
unknown committed
5047
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
unknown's avatar
unknown committed
5048 5049 5050
	  {
	    if (!$1.symbol->create_func)
	    {
5051 5052 5053
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
unknown's avatar
unknown committed
5054 5055 5056 5057
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*,Item*))($1.symbol->create_func))($3,$5,$7);
	  }
unknown's avatar
unknown committed
5058 5059 5060 5061
	| 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); }
5062 5063
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
unknown's avatar
unknown committed
5064 5065 5066 5067 5068 5069
	| ATAN	'(' expr ')'
	  { $$= new Item_func_atan($3); }
	| ATAN	'(' expr ',' expr ')'
	  { $$= new Item_func_atan($3,$5); }
	| CHAR_SYM '(' expr_list ')'
	  { $$= new Item_func_char(*$3); }
unknown's avatar
unknown committed
5070 5071
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
unknown's avatar
unknown committed
5072 5073
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
unknown's avatar
unknown committed
5074 5075
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
unknown's avatar
unknown committed
5076 5077 5078
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
5079
	  { $5->push_front($3); $$= new Item_func_concat_ws(*$5); }
5080 5081
	| CONVERT_TZ_SYM '(' expr ',' expr ',' expr ')'
	  {
5082 5083
            if (Lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
5084 5085
	    $$= new Item_func_convert_tz($3, $5, $7);
	  }
unknown's avatar
unknown committed
5086
	| CURDATE optional_braces
5087
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
5088
	| CURTIME optional_braces
5089
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
unknown's avatar
unknown committed
5090
	| CURTIME '(' expr ')'
5091
	  {
5092
	    $$= new Item_func_curtime_local($3);
5093
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
5094
	  }
unknown's avatar
unknown committed
5095
	| CURRENT_USER optional_braces
5096
          { $$= create_func_current_user(); }
5097 5098 5099 5100
	| 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
5101
	| DATABASE '(' ')'
5102
	  {
unknown's avatar
unknown committed
5103
	    $$= new Item_func_database();
5104
            Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
5105
	  }
unknown's avatar
unknown committed
5106
	| DATE_SYM '(' expr ')'
unknown's avatar
unknown committed
5107
	  { $$= new Item_date_typecast($3); }
unknown's avatar
unknown committed
5108 5109
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
unknown's avatar
unknown committed
5110
	| ELT_FUNC '(' expr ',' expr_list ')'
5111
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
unknown's avatar
unknown committed
5112 5113
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
unknown's avatar
unknown committed
5114 5115 5116
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
5117
	    Lex->uncacheable(UNCACHEABLE_RAND);
unknown's avatar
unknown committed
5118
	  }
unknown's avatar
unknown committed
5119
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
5120
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
5121
	  { $$= new Item_func_decode($3,$5.str); }
5122
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
unknown's avatar
unknown committed
5123
	 { $$= new Item_func_encode($3,$5.str); }
unknown's avatar
unknown committed
5124
	| DES_DECRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
5125
        { $$= new Item_func_des_decrypt($3); }
unknown's avatar
unknown committed
5126
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
5127
        { $$= new Item_func_des_decrypt($3,$5); }
unknown's avatar
unknown committed
5128
	| DES_ENCRYPT_SYM '(' expr ')'
unknown's avatar
unknown committed
5129
        { $$= new Item_func_des_encrypt($3); }
unknown's avatar
unknown committed
5130
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
5131
        { $$= new Item_func_des_encrypt($3,$5); }
unknown's avatar
unknown committed
5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143
	| 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
5144
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
unknown's avatar
unknown committed
5145 5146
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
5147
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
unknown's avatar
unknown committed
5148 5149 5150 5151 5152
	| geometry_function
	  {
#ifdef HAVE_SPATIAL
	    $$= $1;
#else
5153 5154
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
5155 5156 5157
	    YYABORT;
#endif
	  }
5158
	| GET_FORMAT '(' date_time_type  ',' expr ')'
5159
	  { $$= new Item_func_get_format($3, $5); }
unknown's avatar
unknown committed
5160 5161 5162 5163 5164 5165
	| 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); }
5166
	| interval_expr interval '+' expr
unknown's avatar
unknown committed
5167
	  /* we cannot put interval before - */
5168 5169 5170 5171 5172
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
5173
              yyerror(ER(ER_SYNTAX_ERROR));
5174 5175 5176 5177
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
unknown's avatar
unknown committed
5178 5179
	| LAST_INSERT_ID '(' ')'
	  {
5180
	    $$= new Item_func_last_insert_id();
5181
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
5182 5183 5184
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
5185
	    $$= new Item_func_last_insert_id($3);
5186
	    Lex->safe_to_cache_query= 0;
unknown's avatar
unknown committed
5187 5188 5189 5190 5191 5192 5193
	  }
	| 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
5194
	| GREATEST_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
5195 5196 5197
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
5198
	| LOG_SYM '(' expr ')'
unknown's avatar
unknown committed
5199
	  { $$= new Item_func_log($3); }
5200
	| LOG_SYM '(' expr ',' expr ')'
unknown's avatar
unknown committed
5201
	  { $$= new Item_func_log($3, $5); }
5202
	| MASTER_POS_WAIT '(' expr ',' expr ')'
unknown's avatar
unknown committed
5203
	  {
5204
	    $$= new Item_master_pos_wait($3, $5);
unknown's avatar
unknown committed
5205
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
5206
		  }
5207
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
unknown's avatar
unknown committed
5208
	  {
5209
	    $$= new Item_master_pos_wait($3, $5, $7);
unknown's avatar
unknown committed
5210
	    Lex->safe_to_cache_query=0;
5211
	  }
unknown's avatar
unknown committed
5212 5213
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
unknown's avatar
unknown committed
5214 5215
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
5216 5217
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
unknown's avatar
unknown committed
5218 5219 5220
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
	| NOW_SYM optional_braces
5221
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
5222
	| NOW_SYM '(' expr ')'
5223
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
unknown's avatar
unknown committed
5224
	| PASSWORD '(' expr ')'
5225
	  {
5226 5227
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
unknown's avatar
unknown committed
5228 5229 5230 5231
	      (Item *) new Item_func_password($3);
	  }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new Item_func_old_password($3); }
5232
	| POSITION_SYM '(' bit_expr IN_SYM expr ')'
unknown's avatar
unknown committed
5233
	  { $$ = new Item_func_locate($5,$3); }
5234 5235
	| QUARTER_SYM '(' expr ')'
	  { $$ = new Item_func_quarter($3); }
unknown's avatar
unknown committed
5236
	| RAND '(' expr ')'
5237
	  { $$= new Item_func_rand($3); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
5238
	| RAND '(' ')'
5239
	  { $$= new Item_func_rand(); Lex->uncacheable(UNCACHEABLE_RAND);}
unknown's avatar
unknown committed
5240 5241 5242 5243 5244 5245 5246
	| 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); }
5247 5248 5249 5250 5251
	| ROW_COUNT_SYM '(' ')'
	  {
	    $$= new Item_func_row_count();
	    Lex->safe_to_cache_query= 0;
	  }
unknown's avatar
unknown committed
5252 5253 5254 5255
	| 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
5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267
	| SECOND_SYM '(' expr ')'
	  { $$= new Item_func_second($3); }
	| SUBSTRING '(' expr ',' expr ',' expr ')'
	  { $$= new Item_func_substr($3,$5,$7); }
	| SUBSTRING '(' expr ',' expr ')'
	  { $$= new Item_func_substr($3,$5); }
	| SUBSTRING '(' expr FROM expr FOR_SYM expr ')'
	  { $$= new Item_func_substr($3,$5,$7); }
	| SUBSTRING '(' expr FROM expr ')'
	  { $$= new Item_func_substr($3,$5); }
	| SUBSTRING_INDEX '(' expr ',' expr ',' expr ')'
	  { $$= new Item_func_substr_index($3,$5,$7); }
unknown's avatar
unknown committed
5268
	| TIME_SYM '(' expr ')'
unknown's avatar
unknown committed
5269 5270 5271
	  { $$= new Item_time_typecast($3); }
	| TIMESTAMP '(' expr ')'
	  { $$= new Item_datetime_typecast($3); }
unknown's avatar
unknown committed
5272
	| TIMESTAMP '(' expr ',' expr ')'
unknown's avatar
unknown committed
5273
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
5274 5275 5276 5277
	| 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
5278
	| TRIM '(' expr ')'
5279 5280
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
unknown's avatar
unknown committed
5281
	  { $$= new Item_func_ltrim($6,$4); }
5282
	| TRIM '(' TRAILING expr FROM expr ')'
unknown's avatar
unknown committed
5283
	  { $$= new Item_func_rtrim($6,$4); }
5284
	| TRIM '(' BOTH expr FROM expr ')'
unknown's avatar
unknown committed
5285
	  { $$= new Item_func_trim($6,$4); }
5286 5287 5288 5289 5290 5291
	| 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
5292 5293
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
5294 5295
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
5296
	| ident '.' ident '(' udf_expr_list ')'
unknown's avatar
unknown committed
5297
	  {
5298
	    LEX *lex= Lex;
5299 5300 5301
	    sp_name *name= new sp_name($1, $3);

	    name->init_qname(YYTHD);
5302
	    sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
5303
	    if ($5)
5304
	      $$= new Item_func_sp(&lex->current_select->context, name, *$5);
unknown's avatar
unknown committed
5305
	    else
5306
	      $$= new Item_func_sp(&lex->current_select->context, name);
5307
	    lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
5308
	  }
5309 5310 5311 5312
	| IDENT_sys '(' udf_expr_list ')'
          {
#ifdef HAVE_DLOPEN
            udf_func *udf;
5313
            SELECT_LEX *sel= Select;
5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365

            if (using_udf_functions && (udf=find_udf($1.str, $1.length)))
            {
              switch (udf->returns) {
              case STRING_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_str(udf, *$3);
                  else
                    $$ = new Item_func_udf_str(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_str(udf, *$3);
                  else
                    $$ = new Item_sum_udf_str(udf);
                }
                break;
              case REAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_float(udf, *$3);
                  else
                    $$ = new Item_func_udf_float(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_float(udf, *$3);
                  else
                    $$ = new Item_sum_udf_float(udf);
                }
                break;
              case INT_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_int(udf, *$3);
                  else
                    $$ = new Item_func_udf_int(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_int(udf, *$3);
                  else
                    $$ = new Item_sum_udf_int(udf);
                }
                break;
unknown's avatar
unknown committed
5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381
              case DECIMAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_decimal(udf, *$3);
                  else
                    $$ = new Item_func_udf_decimal(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_decimal(udf, *$3);
                  else
                    $$ = new Item_sum_udf_decimal(udf);
                }
                break;
5382 5383 5384 5385 5386 5387 5388
              default:
                YYABORT;
              }
            }
            else
#endif /* HAVE_DLOPEN */
            {
5389
	      LEX *lex= Lex;
5390 5391
              sp_name *name= sp_name_current_db_new(YYTHD, $1);

5392
              sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
5393
              if ($3)
5394
                $$= new Item_func_sp(&lex->current_select->context, name, *$3);
5395
              else
5396
                $$= new Item_func_sp(&lex->current_select->context, name);
5397
	      lex->safe_to_cache_query=0;
5398 5399
	    }
          }
unknown's avatar
unknown committed
5400
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
5401
	  {
unknown's avatar
unknown committed
5402 5403
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
unknown's avatar
unknown committed
5404
	| UNIX_TIMESTAMP '(' ')'
unknown's avatar
unknown committed
5405 5406
	  {
	    $$= new Item_func_unix_timestamp();
5407
	    Lex->safe_to_cache_query=0;
unknown's avatar
unknown committed
5408
	  }
unknown's avatar
unknown committed
5409 5410 5411
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
5412
	  { $$= new Item_func_user(FALSE); Lex->safe_to_cache_query=0; }
5413 5414 5415 5416 5417 5418
	| 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
5419
	| WEEK_SYM '(' expr ')'
unknown's avatar
unknown committed
5420
	  {
5421
            $$= new Item_func_week($3,new Item_int((char*) "0",
unknown's avatar
unknown committed
5422
				   YYTHD->variables.default_week_format,1));
5423
          }
unknown's avatar
unknown committed
5424 5425 5426 5427 5428 5429 5430 5431
	| 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
5432
	| BENCHMARK_SYM '(' ulong_num ',' expr ')'
5433
	  {
unknown's avatar
unknown committed
5434
	    $$=new Item_func_benchmark($3,$5);
5435
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
5436
	  }
unknown's avatar
unknown committed
5437
	| EXTRACT_SYM '(' interval FROM expr ')'
unknown's avatar
unknown committed
5438
	{ $$=new Item_extract( $3, $5); };
unknown's avatar
unknown committed
5439

unknown's avatar
unknown committed
5440
geometry_function:
unknown's avatar
unknown committed
5441 5442 5443
	  CONTAINS_SYM '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_spatial_rel($3, $5, Item_func::SP_CONTAINS_FUNC)); }
	| GEOMFROMTEXT '(' expr ')'
unknown's avatar
unknown committed
5444 5445 5446 5447 5448 5449 5450 5451 5452
	  { $$= 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,
5453 5454
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
unknown's avatar
unknown committed
5455 5456
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
5457
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
unknown's avatar
unknown committed
5458 5459
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
5460
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474
 	| 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,
5475
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
unknown's avatar
unknown committed
5476 5477
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
5478
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
unknown's avatar
unknown committed
5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490
	| 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,
5491
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
unknown's avatar
unknown committed
5492 5493 5494 5495 5496 5497 5498 5499 5500 5501
 	| 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)); }
	;

5502 5503 5504 5505 5506 5507
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
5508
udf_expr_list:
5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533
	/* 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)
5534 5535 5536 5537
          {
	    $2->set_name($4.str, $4.length, system_charset_info);
            $2->is_autogenerated_name= FALSE;
          }
5538
	  else
5539
	    $2->set_name($1, (uint) ($3 - $1), YYTHD->charset());
5540 5541 5542
	  $$= $2;
	}
	;
unknown's avatar
unknown committed
5543 5544 5545 5546

sum_expr:
	AVG_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_avg($3); }
5547 5548
	| AVG_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_avg_distinct($4); }
unknown's avatar
unknown committed
5549 5550 5551 5552
	| BIT_AND  '(' in_sum_expr ')'
	  { $$=new Item_sum_and($3); }
	| BIT_OR  '(' in_sum_expr ')'
	  { $$=new Item_sum_or($3); }
5553 5554
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
unknown's avatar
unknown committed
5555
	| COUNT_SYM '(' opt_all '*' ')'
unknown's avatar
unknown committed
5556 5557 5558
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
5559
	| COUNT_SYM '(' DISTINCT
unknown's avatar
unknown committed
5560
	  { Select->in_sum_expr++; }
5561
	   expr_list
unknown's avatar
unknown committed
5562
	  { Select->in_sum_expr--; }
5563 5564
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
unknown's avatar
unknown committed
5565 5566 5567 5568
	| 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); }
5569 5570 5571 5572 5573 5574 5575
/*
   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
5576 5577
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
5578 5579
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
unknown's avatar
unknown committed
5580
	| STD_SYM '(' in_sum_expr ')'
5581
	  { $$=new Item_sum_std($3, 0); }
unknown's avatar
unknown committed
5582
	| VARIANCE_SYM '(' in_sum_expr ')'
5583 5584 5585 5586 5587
	  { $$=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
5588
	| SUM_SYM '(' in_sum_expr ')'
5589
	  { $$=new Item_sum_sum($3); }
5590 5591
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
unknown's avatar
unknown committed
5592 5593 5594 5595 5596
	| GROUP_CONCAT_SYM '(' opt_distinct
	  { Select->in_sum_expr++; }
	  expr_list opt_gorder_clause
	  opt_gconcat_separator
	 ')'
unknown's avatar
unknown committed
5597
	  {
5598 5599 5600 5601
            SELECT_LEX *sel= Select;
	    sel->in_sum_expr--;
	    $$=new Item_func_group_concat(&sel->context, $3, $5,
                                          sel->gorder_list, $7);
unknown's avatar
unknown committed
5602
	    $5->empty();
5603 5604 5605 5606 5607 5608 5609
	  };

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

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

5613 5614

opt_gorder_clause:
5615 5616
	  /* empty */
	  {
unknown's avatar
unknown committed
5617
            Select->gorder_list = NULL;
5618 5619 5620
	  }
	| order_clause
          {
unknown's avatar
unknown committed
5621 5622 5623
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
5624
				     sizeof(st_sql_list));
unknown's avatar
unknown committed
5625
	    select->order_list.empty();
5626
	  };
unknown's avatar
unknown committed
5627

unknown's avatar
unknown committed
5628 5629

in_sum_expr:
unknown's avatar
unknown committed
5630
	opt_all
5631 5632 5633 5634
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
5635
	    yyerror(ER(ER_SYNTAX_ERROR));
5636
	    YYABORT;
5637 5638
	  }
	}
unknown's avatar
unknown committed
5639 5640
	expr
	{
unknown's avatar
unknown committed
5641
	  Select->in_sum_expr--;
5642
	  $$= $3;
unknown's avatar
unknown committed
5643
	};
unknown's avatar
unknown committed
5644

5645
cast_type:
unknown's avatar
unknown committed
5646 5647
        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; }
5648
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; Lex->dec=0; }
unknown's avatar
unknown committed
5649 5650 5651 5652 5653 5654 5655 5656
        | 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; }
5657 5658
	;

unknown's avatar
unknown committed
5659
expr_list:
5660
	{ Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
5661
	expr_list2
unknown's avatar
unknown committed
5662
	{ $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
5663 5664

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

unknown's avatar
unknown committed
5668 5669
ident_list_arg:
          ident_list          { $$= $1; }
unknown's avatar
unknown committed
5670
        | '(' ident_list ')'  { $$= $2; };
unknown's avatar
unknown committed
5671

unknown's avatar
unknown committed
5672
ident_list:
5673
        { Select->expr_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
5674
        ident_list2
unknown's avatar
unknown committed
5675
        { $$= Select->expr_list.pop(); };
unknown's avatar
unknown committed
5676 5677

ident_list2:
5678
        simple_ident { Select->expr_list.head()->push_back($1); }
unknown's avatar
unknown committed
5679
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
unknown's avatar
unknown committed
5680 5681 5682

opt_expr:
	/* empty */      { $$= NULL; }
5683
	| expr           { $$= $1; };
unknown's avatar
unknown committed
5684 5685 5686

opt_else:
	/* empty */    { $$= NULL; }
unknown's avatar
unknown committed
5687
	| ELSE expr    { $$= $2; };
unknown's avatar
unknown committed
5688 5689

when_list:
unknown's avatar
unknown committed
5690
        { Select->when_list.push_front(new List<Item>); }
unknown's avatar
unknown committed
5691
	when_list2
unknown's avatar
unknown committed
5692
	{ $$= Select->when_list.pop(); };
unknown's avatar
unknown committed
5693 5694

when_list2:
unknown's avatar
unknown committed
5695
	expr THEN_SYM expr
unknown's avatar
unknown committed
5696
	  {
unknown's avatar
unknown committed
5697
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
5698 5699
	    sel->when_list.head()->push_back($1);
	    sel->when_list.head()->push_back($3);
unknown's avatar
unknown committed
5700
	}
unknown's avatar
unknown committed
5701
	| when_list2 WHEN_SYM expr THEN_SYM expr
unknown's avatar
unknown committed
5702
	  {
unknown's avatar
unknown committed
5703
	    SELECT_LEX *sel=Select;
unknown's avatar
unknown committed
5704 5705
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
unknown's avatar
unknown committed
5706
	  };
unknown's avatar
unknown committed
5707

unknown's avatar
unknown committed
5708
/* Warning - may return NULL in case of incomplete SELECT */
5709 5710 5711
table_ref:
        table_factor            { $$=$1; }
        | join_table            { $$=$1; }
5712
          {
5713 5714 5715
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
5716
          }
5717 5718
        ;

unknown's avatar
unknown committed
5719
join_table_list:
unknown's avatar
unknown committed
5720
	derived_table_list		{ YYERROR_UNLESS($$=$1); }
unknown's avatar
unknown committed
5721
	;
unknown's avatar
unknown committed
5722

unknown's avatar
unknown committed
5723 5724
/* Warning - may return NULL in case of incomplete SELECT */
derived_table_list:
5725
        table_ref { $$=$1; }
unknown's avatar
unknown committed
5726 5727
        | derived_table_list ',' table_ref
          {
unknown's avatar
unknown committed
5728
            YYERROR_UNLESS($1 && ($$=$3));
unknown's avatar
unknown committed
5729
          }
5730 5731 5732
        ;

join_table:
unknown's avatar
unknown committed
5733
        table_ref normal_join table_ref { YYERROR_UNLESS($1 && ($$=$3)); }
5734
	| table_ref STRAIGHT_JOIN table_factor
unknown's avatar
unknown committed
5735
	  { YYERROR_UNLESS($1 && ($$=$3)); $3->straight=1; }
5736
	| table_ref normal_join table_ref ON expr
unknown's avatar
unknown committed
5737
	  { YYERROR_UNLESS($1 && ($$=$3)); add_join_on($3,$5); }
unknown's avatar
unknown committed
5738
        | table_ref STRAIGHT_JOIN table_factor ON expr
unknown's avatar
unknown committed
5739
          { YYERROR_UNLESS($1 && ($$=$3)); $3->straight=1; add_join_on($3,$5); }
5740
	| table_ref normal_join table_ref
unknown's avatar
unknown committed
5741
	  USING
unknown's avatar
unknown committed
5742
	  {
unknown's avatar
unknown committed
5743
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5744
            YYERROR_UNLESS($1 && $3);
5745
            sel->save_names_for_using_list($1, $3);
unknown's avatar
unknown committed
5746
	  }
unknown's avatar
unknown committed
5747 5748 5749
	  '(' using_list ')'
	  { add_join_on($3,$7); $$=$3; }

5750
	| table_ref LEFT opt_outer JOIN_SYM table_ref ON expr
unknown's avatar
unknown committed
5751
	  { YYERROR_UNLESS($1 && $5); add_join_on($5,$7); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
5752
	| table_ref LEFT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
5753
	  {
unknown's avatar
unknown committed
5754
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5755
            YYERROR_UNLESS($1 && $5);
5756
            sel->save_names_for_using_list($1, $5);
unknown's avatar
unknown committed
5757
	  }
unknown's avatar
unknown committed
5758
	  USING '(' using_list ')'
5759
	  { add_join_on($5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
5760
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
5761
	  {
unknown's avatar
unknown committed
5762
            YYERROR_UNLESS($1 && $6);
5763 5764
	    add_join_natural($1,$6);
	    $6->outer_join|=JOIN_TYPE_LEFT;
5765 5766
	    $$=$6;
	  }
5767
	| table_ref RIGHT opt_outer JOIN_SYM table_ref ON expr
unknown's avatar
unknown committed
5768
          {
5769
	    LEX *lex= Lex;
unknown's avatar
unknown committed
5770
            YYERROR_UNLESS($1 && $5);
5771 5772 5773 5774 5775
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
            add_join_on($$, $7);
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
unknown's avatar
unknown committed
5776
	  {
unknown's avatar
unknown committed
5777
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5778
            YYERROR_UNLESS($1 && $5);
5779
            sel->save_names_for_using_list($1, $5);
unknown's avatar
unknown committed
5780
	  }
5781
	  USING '(' using_list ')'
unknown's avatar
unknown committed
5782
          {
5783 5784 5785 5786 5787 5788
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
            add_join_on($$, $9);
          }
	| table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor
5789
	  {
unknown's avatar
unknown committed
5790
            YYERROR_UNLESS($1 && $6);
5791 5792 5793 5794
	    add_join_natural($6,$1);
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
5795
	  }
5796
	| table_ref NATURAL JOIN_SYM table_factor
unknown's avatar
unknown committed
5797
	  { YYERROR_UNLESS($1 && ($$=$4)); add_join_natural($1,$4); };
unknown's avatar
unknown committed
5798

unknown's avatar
unknown committed
5799 5800

normal_join:
unknown's avatar
unknown committed
5801 5802 5803 5804
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
unknown's avatar
unknown committed
5805

unknown's avatar
unknown committed
5806
/* Warning - may return NULL in case of incomplete SELECT */
5807
table_factor:
unknown's avatar
unknown committed
5808
	{
unknown's avatar
unknown committed
5809
	  SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5810
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
unknown's avatar
unknown committed
5811
	  sel->table_join_options= 0;
unknown's avatar
unknown committed
5812
	}
unknown's avatar
unknown committed
5813
        table_ident opt_table_alias opt_key_definition
unknown's avatar
unknown committed
5814
	{
unknown's avatar
unknown committed
5815
	  LEX *lex= Lex;
unknown's avatar
unknown committed
5816
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
5817
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
unknown's avatar
unknown committed
5818
					   sel->get_table_join_options(),
unknown's avatar
unknown committed
5819
					   lex->lock_option,
5820 5821
					   sel->get_use_index(),
					   sel->get_ignore_index())))
5822
	    YYABORT;
5823
          sel->add_joined_table($$);
unknown's avatar
unknown committed
5824
	}
unknown's avatar
unknown committed
5825
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref ON expr '}'
unknown's avatar
unknown committed
5826
	  { YYERROR_UNLESS($3 && $7); add_join_on($7,$9); $7->outer_join|=JOIN_TYPE_LEFT; $$=$7; }
unknown's avatar
unknown committed
5827
	| select_derived_init get_select_lex select_derived2
5828
          {
5829
            LEX *lex= Lex;
unknown's avatar
unknown committed
5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843
            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
5844
              YYABORT;
unknown's avatar
unknown committed
5845
            $$= 0;
unknown's avatar
unknown committed
5846
            /* incomplete derived tables return NULL, we must be
unknown's avatar
unknown committed
5847
               nested in select_derived rule to be here. */
5848
          }
unknown's avatar
unknown committed
5849 5850 5851 5852 5853
	| '(' 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
5854
          if (!($3 || $6) && $2->embedding &&
unknown's avatar
unknown committed
5855
              !$2->embedding->nested_join->join_list.elements)
5856
          {
unknown's avatar
unknown committed
5857 5858 5859 5860 5861
            /* 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;
5862
          }
unknown's avatar
unknown committed
5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876
          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
5877

unknown's avatar
unknown committed
5878
	      YYABORT;
unknown's avatar
unknown committed
5879
            sel->add_joined_table($$);
unknown's avatar
unknown committed
5880 5881 5882 5883 5884 5885
          }
	  else
          if ($4 || $6)
	  {
            /* simple nested joins cannot have aliases or unions */
            yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
5886
	    YYABORT;
unknown's avatar
unknown committed
5887 5888 5889 5890 5891
	  }
          else
            $$= $3;
	}
        ;
unknown's avatar
unknown committed
5892

unknown's avatar
unknown committed
5893
/* handle contents of parentheses in join expression */
5894
select_derived:
unknown's avatar
unknown committed
5895
	  get_select_lex
unknown's avatar
unknown committed
5896
	  {
unknown's avatar
unknown committed
5897 5898 5899 5900 5901 5902 5903 5904 5905
            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
5906

unknown's avatar
unknown committed
5907 5908 5909 5910
            if (!($$= $1->end_nested_join(lex->thd)) && $3)
              YYABORT;
            if (!$3 && $$)
            {
unknown's avatar
unknown committed
5911 5912
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
unknown's avatar
unknown committed
5913 5914
            }
          }
unknown's avatar
unknown committed
5915
 	;
unknown's avatar
unknown committed
5916

unknown's avatar
unknown committed
5917
select_derived2:
unknown's avatar
unknown committed
5918
        {
unknown's avatar
unknown committed
5919
	  LEX *lex= Lex;
5920
	  lex->derived_tables|= DERIVED_SUBQUERY;
unknown's avatar
unknown committed
5921 5922 5923
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
5924 5925
	  {
	    yyerror(ER(ER_SYNTAX_ERROR));
5926 5927
	    YYABORT;
	  }
5928
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
unknown's avatar
unknown committed
5929
              mysql_new_select(lex, 1))
unknown's avatar
unknown committed
5930 5931
	    YYABORT;
	  mysql_init_select(lex);
5932
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
5933
	  lex->current_select->parsing_place= SELECT_LIST;
5934 5935 5936
	}
        select_options select_item_list
	{
5937
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
5938
	}
unknown's avatar
unknown committed
5939
	opt_select_from
5940
        ;
unknown's avatar
unknown committed
5941

unknown's avatar
unknown committed
5942 5943 5944 5945 5946 5947 5948 5949
get_select_lex:
	/* Empty */ { $$= Select; }
        ;

select_derived_init:
          SELECT_SYM
          {
            LEX *lex= Lex;
unknown's avatar
unknown committed
5950
            SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964
            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
5965 5966
opt_outer:
	/* empty */	{}
unknown's avatar
unknown committed
5967
	| OUTER		{};
unknown's avatar
unknown committed
5968 5969 5970 5971

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
unknown's avatar
unknown committed
5972
          {
unknown's avatar
unknown committed
5973
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5974 5975 5976
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
5977 5978
	| FORCE_SYM key_usage_list
          {
unknown's avatar
unknown committed
5979
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5980 5981 5982 5983
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
unknown's avatar
unknown committed
5984
	| IGNORE_SYM key_usage_list
unknown's avatar
unknown committed
5985
	  {
unknown's avatar
unknown committed
5986
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5987 5988
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
unknown's avatar
unknown committed
5989
	  };
unknown's avatar
unknown committed
5990 5991

key_usage_list:
unknown's avatar
unknown committed
5992
	key_or_index { Select->interval_list.empty(); }
unknown's avatar
unknown committed
5993
        '(' key_list_or_empty ')'
unknown's avatar
unknown committed
5994
        { $$= &Select->interval_list; }
unknown's avatar
unknown committed
5995 5996 5997 5998 5999 6000
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
unknown's avatar
unknown committed
6001 6002 6003

key_usage_list2:
	key_usage_list2 ',' ident
unknown's avatar
unknown committed
6004
        { Select->
unknown's avatar
unknown committed
6005
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $3.str, $3.length,
6006
				    system_charset_info)); }
unknown's avatar
unknown committed
6007
	| ident
unknown's avatar
unknown committed
6008
        { Select->
unknown's avatar
unknown committed
6009
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $1.str, $1.length,
6010
				    system_charset_info)); }
unknown's avatar
unknown committed
6011
	| PRIMARY_SYM
unknown's avatar
unknown committed
6012
        { Select->
unknown's avatar
unknown committed
6013
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
6014
				    system_charset_info)); };
unknown's avatar
unknown committed
6015 6016 6017

using_list:
	ident
unknown's avatar
unknown committed
6018
	  {
unknown's avatar
unknown committed
6019
	    SELECT_LEX *sel= Select;
6020 6021
	    if (!($$= new Item_func_eq(new Item_field(&sel->context,
                                                      sel->db1, sel->table1,
6022
						      $1.str),
6023 6024
				       new Item_field(&sel->context,
                                                      sel->db2, sel->table2,
6025
						      $1.str))))
unknown's avatar
unknown committed
6026 6027 6028 6029
	      YYABORT;
	  }
	| using_list ',' ident
	  {
unknown's avatar
unknown committed
6030
	    SELECT_LEX *sel= Select;
6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043
	    if (!($$=
                  new Item_cond_and(new
                                    Item_func_eq(new
                                                 Item_field(&sel->context,
                                                            sel->db1,
                                                            sel->table1,
                                                            $3.str),
                                                 new
                                                 Item_field(&sel->context,
                                                            sel->db2,
                                                            sel->table2,
                                                            $3.str)),
                                    $1)))
unknown's avatar
unknown committed
6044
	      YYABORT;
unknown's avatar
unknown committed
6045
	  };
unknown's avatar
unknown committed
6046 6047

interval:
6048 6049
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
unknown's avatar
unknown committed
6050
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
unknown's avatar
unknown committed
6051 6052
	| DAY_MINUTE_SYM	{ $$=INTERVAL_DAY_MINUTE; }
	| DAY_SECOND_SYM	{ $$=INTERVAL_DAY_SECOND; }
unknown's avatar
unknown committed
6053
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
unknown's avatar
unknown committed
6054 6055
	| HOUR_MINUTE_SYM	{ $$=INTERVAL_HOUR_MINUTE; }
	| HOUR_SECOND_SYM	{ $$=INTERVAL_HOUR_SECOND; }
unknown's avatar
unknown committed
6056 6057
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
unknown's avatar
unknown committed
6058
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
6059 6060 6061 6062 6063 6064 6065 6066
	| 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
6067 6068
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
6069
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
unknown's avatar
unknown committed
6070
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
6071 6072
	| YEAR_SYM		{ $$=INTERVAL_YEAR; }
        ;
unknown's avatar
unknown committed
6073

6074
date_time_type:
6075 6076 6077 6078 6079
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
6080

unknown's avatar
unknown committed
6081 6082 6083
table_alias:
	/* empty */
	| AS
unknown's avatar
unknown committed
6084
	| EQ;
unknown's avatar
unknown committed
6085 6086 6087 6088

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

unknown's avatar
unknown committed
6091 6092 6093 6094
opt_all:
	/* empty */
	| ALL
	;
unknown's avatar
unknown committed
6095 6096

where_clause:
unknown's avatar
unknown committed
6097
	/* empty */  { Select->where= 0; }
unknown's avatar
unknown committed
6098 6099 6100 6101 6102
	| WHERE
          {
            Select->parsing_place= IN_WHERE;
          }
          expr
6103
	  {
unknown's avatar
unknown committed
6104 6105 6106 6107 6108
            SELECT_LEX *select= Select;
	    select->where= $3;
            select->parsing_place= NO_MATTER;
	    if ($3)
	      $3->top_level_item();
6109
	  }
unknown's avatar
unknown committed
6110
 	;
unknown's avatar
unknown committed
6111 6112 6113

having_clause:
	/* empty */
unknown's avatar
unknown committed
6114 6115
	| HAVING
	  {
6116
	    Select->parsing_place= IN_HAVING;
unknown's avatar
unknown committed
6117 6118
          }
	  expr
unknown's avatar
unknown committed
6119
	  {
unknown's avatar
unknown committed
6120
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
6121
	    sel->having= $3;
6122
	    sel->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
6123 6124 6125
	    if ($3)
	      $3->top_level_item();
	  }
6126
	;
unknown's avatar
unknown committed
6127 6128

opt_escape:
6129 6130
	ESCAPE_SYM simple_expr { $$= $2; }
	| /* empty */
6131 6132 6133 6134 6135
          {

            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
6136 6137
          }
        ;
unknown's avatar
unknown committed
6138 6139 6140


/*
unknown's avatar
unknown committed
6141
   group by statement in select
unknown's avatar
unknown committed
6142 6143 6144 6145
*/

group_clause:
	/* empty */
6146
	| GROUP BY group_list olap_opt;
unknown's avatar
unknown committed
6147 6148

group_list:
unknown's avatar
unknown committed
6149
	group_list ',' order_ident order_dir
unknown's avatar
unknown committed
6150
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
6151
	| order_ident order_dir
unknown's avatar
unknown committed
6152
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
6153

6154 6155
olap_opt:
	/* empty */ {}
6156
	| WITH CUBE_SYM
6157
          {
6158
	    LEX *lex=Lex;
6159 6160
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
6161
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
6162 6163 6164
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
6165
	    lex->current_select->olap= CUBE_TYPE;
unknown's avatar
unknown committed
6166
	    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
6167
	    YYABORT;	/* To be deleted in 5.1 */
6168
	  }
6169
	| WITH ROLLUP_SYM
6170
          {
6171 6172 6173
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
unknown's avatar
unknown committed
6174
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
6175 6176 6177
		       "global union parameters");
	      YYABORT;
	    }
unknown's avatar
unknown committed
6178
	    lex->current_select->olap= ROLLUP_TYPE;
6179
	  }
6180
	;
6181

unknown's avatar
unknown committed
6182
/*
unknown's avatar
unknown committed
6183
   Order by statement in select
unknown's avatar
unknown committed
6184 6185
*/

6186
opt_order_clause:
unknown's avatar
unknown committed
6187
	/* empty */
unknown's avatar
unknown committed
6188
	| order_clause;
6189 6190

order_clause:
6191 6192
	ORDER_SYM BY
        {
unknown's avatar
unknown committed
6193
	  LEX *lex=Lex;
6194
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
unknown's avatar
unknown committed
6195
	      lex->current_select->olap !=
6196
	      UNSPECIFIED_OLAP_TYPE)
6197
	  {
unknown's avatar
unknown committed
6198 6199
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
6200 6201
	    YYABORT;
	  }
unknown's avatar
unknown committed
6202
	} order_list;
unknown's avatar
unknown committed
6203 6204 6205

order_list:
	order_list ',' order_ident order_dir
unknown's avatar
unknown committed
6206
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
6207
	| order_ident order_dir
unknown's avatar
unknown committed
6208
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
6209 6210 6211

order_dir:
	/* empty */ { $$ =  1; }
unknown's avatar
unknown committed
6212
	| ASC  { $$ =1; }
unknown's avatar
unknown committed
6213
	| DESC { $$ =0; };
unknown's avatar
unknown committed
6214 6215


6216 6217 6218
opt_limit_clause_init:
	/* empty */
	{
6219 6220
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
6221 6222
          sel->offset_limit= 0;
          sel->select_limit= 0;
6223
	}
6224 6225 6226
	| limit_clause {}
	;

6227 6228 6229 6230 6231
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

6232
limit_clause:
6233
	LIMIT limit_options {}
unknown's avatar
unknown committed
6234
	;
unknown's avatar
unknown committed
6235 6236

limit_options:
6237
	limit_option
unknown's avatar
unknown committed
6238
	  {
unknown's avatar
unknown committed
6239
            SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
6240
            sel->select_limit= $1;
6241
            sel->offset_limit= 0;
6242
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
6243
	  }
6244
	| limit_option ',' limit_option
unknown's avatar
unknown committed
6245
	  {
unknown's avatar
unknown committed
6246
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
6247 6248
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
6249
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
6250
	  }
6251
	| limit_option OFFSET_SYM limit_option
unknown's avatar
unknown committed
6252
	  {
unknown's avatar
unknown committed
6253
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
6254 6255
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
6256
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
6257 6258
	  }
	;
6259 6260 6261 6262 6263
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
6264
        ;
unknown's avatar
unknown committed
6265 6266 6267 6268

delete_limit_clause:
	/* empty */
	{
unknown's avatar
unknown committed
6269
	  LEX *lex=Lex;
6270
	  lex->current_select->select_limit= 0;
unknown's avatar
unknown committed
6271
	}
6272
	| LIMIT limit_option
6273 6274
	{
	  SELECT_LEX *sel= Select;
6275
	  sel->select_limit= $2;
6276 6277
	  sel->explicit_limit= 1;
	};
unknown's avatar
unknown committed
6278

unknown's avatar
unknown committed
6279 6280
ulong_num:
          NUM           { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
6281
	| HEX_NUM       { $$= (ulong) strtol($1.str, (char**) 0, 16); }
unknown's avatar
unknown committed
6282 6283
	| 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
6284
        | DECIMAL_NUM   { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
6285 6286
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
6287

unknown's avatar
unknown committed
6288
ulonglong_num:
unknown's avatar
unknown committed
6289 6290 6291
	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
6292
        | DECIMAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
6293 6294
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
6295 6296 6297 6298 6299 6300

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
6301 6302
	    if (&lex->select_lex != lex->current_select)
	    {
unknown's avatar
unknown committed
6303
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
6304 6305
	      YYABORT;
	    }
unknown's avatar
unknown committed
6306 6307 6308
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
6309 6310 6311 6312
	    if (add_proc_to_list(lex->thd, new Item_field(&lex->
                                                          current_select->
                                                          context,
                                                          NULL,NULL,$2.str)))
unknown's avatar
unknown committed
6313
	      YYABORT;
6314
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
6315
	  }
unknown's avatar
unknown committed
6316
	  '(' procedure_list ')';
unknown's avatar
unknown committed
6317 6318 6319 6320


procedure_list:
	/* empty */ {}
unknown's avatar
unknown committed
6321
	| procedure_list2 {};
unknown's avatar
unknown committed
6322 6323 6324

procedure_list2:
	procedure_list2 ',' procedure_item
unknown's avatar
unknown committed
6325
	| procedure_item;
unknown's avatar
unknown committed
6326 6327 6328 6329

procedure_item:
	  remember_name expr
	  {
unknown's avatar
unknown committed
6330 6331
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
unknown's avatar
unknown committed
6332 6333
	      YYABORT;
	    if (!$2->name)
6334 6335
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1),
                           YYTHD->charset());
6336 6337 6338 6339 6340 6341
	  }
          ;


select_var_list_init:
	   {
unknown's avatar
unknown committed
6342 6343
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
6344 6345
	        YYABORT;
	   }
6346
	   select_var_list
6347
	   {}
6348
           ;
unknown's avatar
unknown committed
6349

unknown's avatar
unknown committed
6350
select_var_list:
6351 6352 6353 6354
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

6355 6356 6357 6358 6359 6360 6361 6362 6363 6364
select_var_ident:  
	   '@' ident_or_text
           {
             LEX *lex=Lex;
	     if (lex->result) 
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($2,0,0,(enum_field_types)0));
	     else
	       YYABORT;
	   }
           | ident_or_text
unknown's avatar
unknown committed
6365
           {
6366
             LEX *lex=Lex;
6367 6368 6369 6370
	     sp_pvar_t *t;

	     if (!lex->spcont || !(t=lex->spcont->find_pvar(&$1)))
	     {
6371
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
unknown's avatar
unknown committed
6372
	       YYABORT;
6373 6374 6375 6376 6377 6378 6379 6380
	     }
	     if (! lex->result)
	       YYABORT;
	     else
	     {
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($1,1,t->offset,t->type));
	       t->isset= TRUE;
	     }
unknown's avatar
unknown committed
6381
	   }
unknown's avatar
unknown committed
6382
           ;
unknown's avatar
unknown committed
6383

6384
into:
unknown's avatar
unknown committed
6385
        INTO OUTFILE TEXT_STRING_sys
unknown's avatar
unknown committed
6386
	{
unknown's avatar
unknown committed
6387
          LEX *lex= Lex;
6388 6389 6390 6391
          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
6392 6393
	}
	opt_field_term opt_line_term
unknown's avatar
unknown committed
6394
	| INTO DUMPFILE TEXT_STRING_sys
unknown's avatar
unknown committed
6395
	{
6396
	  LEX *lex=Lex;
unknown's avatar
unknown committed
6397 6398
	  if (!lex->describe)
	  {
6399
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
6400 6401 6402 6403 6404
	    if (!(lex->exchange= new sql_exchange($3.str,1)))
	      YYABORT;
	    if (!(lex->result= new select_dump(lex->exchange)))
	      YYABORT;
	  }
unknown's avatar
unknown committed
6405
	}
6406
        | INTO select_var_list_init
unknown's avatar
unknown committed
6407
	{
6408
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
6409 6410
	}
        ;
unknown's avatar
unknown committed
6411

unknown's avatar
unknown committed
6412 6413 6414
/*
  DO statement
*/
unknown's avatar
unknown committed
6415

6416
do:	DO_SYM
unknown's avatar
unknown committed
6417 6418 6419
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
6420 6421 6422 6423 6424
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
unknown's avatar
unknown committed
6425
	}
6426 6427
	;

unknown's avatar
unknown committed
6428
/*
6429
  Drop : delete tables or index or user
unknown's avatar
unknown committed
6430 6431 6432
*/

drop:
unknown's avatar
unknown committed
6433
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
unknown's avatar
unknown committed
6434
	{
unknown's avatar
unknown committed
6435 6436
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
6437 6438
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
6439 6440 6441 6442 6443 6444
          if (!lex->drop_temporary && lex->sphead &&
              lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
          {
            my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
            YYABORT;
          }
unknown's avatar
unknown committed
6445
	}
unknown's avatar
unknown committed
6446
	| DROP INDEX_SYM ident ON table_ident {}
unknown's avatar
unknown committed
6447
	  {
unknown's avatar
unknown committed
6448
	     LEX *lex=Lex;
6449 6450 6451 6452 6453
             if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
             {
               my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
               YYABORT;
             }
unknown's avatar
unknown committed
6454
	     lex->sql_command= SQLCOM_DROP_INDEX;
6455 6456 6457
	     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
6458 6459
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
unknown's avatar
unknown committed
6460 6461 6462 6463
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
unknown's avatar
unknown committed
6464 6465 6466 6467
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
	    lex->name=$4.str;
unknown's avatar
unknown committed
6468
	 }
6469
	| DROP FUNCTION_SYM if_exists sp_name
unknown's avatar
unknown committed
6470
	  {
unknown's avatar
unknown committed
6471
	    LEX *lex=Lex;
6472 6473
	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
6474
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
6475 6476
	      YYABORT;
	    }
unknown's avatar
unknown committed
6477
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
6478 6479 6480 6481 6482 6483 6484 6485
	    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
6486
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
6487 6488 6489 6490 6491
	      YYABORT;
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
6492
	  }
6493
	| DROP USER clear_privileges user_list
6494
	  {
6495 6496
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
6497 6498 6499 6500
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
6501 6502 6503 6504 6505
            if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
            {
              my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
              YYABORT;
            }
6506 6507 6508
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
6509
        | DROP TRIGGER_SYM sp_name
6510 6511 6512
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_DROP_TRIGGER;
6513
            lex->spname= $3;
6514 6515
          }
	;
unknown's avatar
unknown committed
6516 6517

table_list:
6518
	table_name
unknown's avatar
unknown committed
6519
	| table_list ',' table_name;
unknown's avatar
unknown committed
6520

6521
table_name:
unknown's avatar
unknown committed
6522
	table_ident
unknown's avatar
unknown committed
6523 6524 6525 6526 6527
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
unknown's avatar
unknown committed
6528 6529

if_exists:
6530
	/* empty */ { $$= 0; }
6531 6532
	| IF EXISTS { $$= 1; }
	;
unknown's avatar
unknown committed
6533

6534 6535 6536 6537
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
unknown's avatar
unknown committed
6538 6539 6540 6541 6542
/*
** Insert : add new data to table
*/

insert:
6543 6544 6545
	INSERT
	{
	  LEX *lex= Lex;
6546 6547
	  lex->sql_command= SQLCOM_INSERT;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
6548
	  mysql_init_select(lex);
6549 6550 6551
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
	} insert_lock_option
6552
	opt_ignore insert2
unknown's avatar
unknown committed
6553
	{
unknown's avatar
unknown committed
6554
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
6555
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
6556
	}
6557
	insert_field_spec opt_insert_update
6558
	{}
unknown's avatar
unknown committed
6559
	;
unknown's avatar
unknown committed
6560 6561

replace:
6562 6563
	REPLACE
	{
6564
	  LEX *lex=Lex;
6565 6566
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
unknown's avatar
unknown committed
6567
	  mysql_init_select(lex);
6568
	}
unknown's avatar
unknown committed
6569 6570
	replace_lock_option insert2
	{
unknown's avatar
unknown committed
6571
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
6572
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
6573 6574
	}
	insert_field_spec
6575
	{}
unknown's avatar
unknown committed
6576
	;
unknown's avatar
unknown committed
6577 6578

insert_lock_option:
unknown's avatar
unknown committed
6579 6580 6581 6582
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
6583
	;
unknown's avatar
unknown committed
6584 6585

replace_lock_option:
unknown's avatar
unknown committed
6586 6587
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
unknown's avatar
unknown committed
6588 6589 6590

insert2:
	INTO insert_table {}
unknown's avatar
unknown committed
6591
	| insert_table {};
unknown's avatar
unknown committed
6592 6593

insert_table:
6594
	table_name
unknown's avatar
unknown committed
6595
	{
unknown's avatar
unknown committed
6596 6597 6598 6599
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
unknown's avatar
unknown committed
6600
	};
unknown's avatar
unknown committed
6601 6602

insert_field_spec:
unknown's avatar
unknown committed
6603 6604 6605
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
unknown's avatar
unknown committed
6606 6607
	| SET
	  {
unknown's avatar
unknown committed
6608 6609 6610
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
6611 6612
	      YYABORT;
	   }
unknown's avatar
unknown committed
6613
	   ident_eq_list;
unknown's avatar
unknown committed
6614 6615 6616

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
unknown's avatar
unknown committed
6617
	| insert_ident		{ Lex->field_list.push_back($1); };
unknown's avatar
unknown committed
6618 6619 6620

insert_values:
	VALUES	values_list  {}
unknown's avatar
unknown committed
6621
	| VALUE_SYM values_list  {}
unknown's avatar
unknown committed
6622 6623
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
6624
        ;
unknown's avatar
unknown committed
6625 6626 6627

values_list:
	values_list ','  no_braces
unknown's avatar
unknown committed
6628
	| no_braces;
unknown's avatar
unknown committed
6629 6630 6631 6632

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
unknown's avatar
unknown committed
6633
	ident_eq_value;
unknown's avatar
unknown committed
6634 6635

ident_eq_value:
6636
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
6637
	 {
unknown's avatar
unknown committed
6638 6639 6640
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
unknown's avatar
unknown committed
6641
	    YYABORT;
unknown's avatar
unknown committed
6642
	 };
unknown's avatar
unknown committed
6643 6644

equal:	EQ		{}
unknown's avatar
unknown committed
6645 6646 6647 6648 6649 6650 6651
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
unknown's avatar
unknown committed
6652 6653 6654 6655 6656 6657 6658 6659 6660

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
unknown's avatar
unknown committed
6661 6662
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
6663
	    YYABORT;
unknown's avatar
unknown committed
6664
	 };
unknown's avatar
unknown committed
6665 6666 6667

opt_values:
	/* empty */ {}
unknown's avatar
unknown committed
6668
	| values;
unknown's avatar
unknown committed
6669 6670

values:
unknown's avatar
unknown committed
6671
	values ','  expr_or_default
unknown's avatar
unknown committed
6672 6673 6674 6675
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
unknown's avatar
unknown committed
6676 6677 6678 6679 6680 6681 6682 6683
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
6684
	expr	  { $$= $1;}
6685
	| DEFAULT {$$= new Item_default_value(&Select->context); }
unknown's avatar
unknown committed
6686
	;
unknown's avatar
unknown committed
6687

6688 6689
opt_insert_update:
        /* empty */
unknown's avatar
unknown committed
6690
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
unknown's avatar
unknown committed
6691
          KEY_SYM UPDATE_SYM insert_update_list
6692 6693
        ;

unknown's avatar
unknown committed
6694 6695 6696
/* Update rows in a table */

update:
6697 6698
	UPDATE_SYM
	{
6699
	  LEX *lex= Lex;
unknown's avatar
unknown committed
6700
	  mysql_init_select(lex);
6701
          lex->sql_command= SQLCOM_UPDATE;
unknown's avatar
unknown committed
6702
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
6703
	  lex->duplicates= DUP_ERROR; 
6704
        }
unknown's avatar
unknown committed
6705
        opt_low_priority opt_ignore join_table_list
unknown's avatar
unknown committed
6706
	SET update_list
unknown's avatar
unknown committed
6707
	{
6708 6709
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
unknown's avatar
unknown committed
6710
            lex->sql_command= SQLCOM_UPDATE_MULTI;
6711 6712 6713
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
6714 6715
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
6716 6717
	    YYABORT;
	  }
6718 6719 6720 6721 6722 6723
          /*
            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
6724
	}
6725
	where_clause opt_order_clause delete_limit_clause {}
unknown's avatar
unknown committed
6726
	;
unknown's avatar
unknown committed
6727 6728

update_list:
unknown's avatar
unknown committed
6729 6730 6731 6732
	update_list ',' update_elem
	| update_elem;

update_elem:
unknown's avatar
unknown committed
6733
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
6734
	{
unknown's avatar
unknown committed
6735
	  if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
unknown's avatar
unknown committed
6736
	    YYABORT;
unknown's avatar
unknown committed
6737 6738 6739 6740 6741 6742 6743
	};

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
unknown's avatar
unknown committed
6744
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
6745
	  {
unknown's avatar
unknown committed
6746 6747 6748
	  LEX *lex= Lex;
	  if (lex->update_list.push_back($1) || 
	      lex->value_list.push_back($3))
unknown's avatar
unknown committed
6749
	      YYABORT;
unknown's avatar
unknown committed
6750
	  };
unknown's avatar
unknown committed
6751 6752

opt_low_priority:
6753
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
6754
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
6755 6756 6757 6758

/* Delete rows from a table */

delete:
6759
	DELETE_SYM
6760
	{
6761 6762
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
unknown's avatar
unknown committed
6763
	  mysql_init_select(lex);
unknown's avatar
unknown committed
6764
	  lex->lock_option= lex->thd->update_lock_default;
6765
	  lex->ignore= 0;
6766
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
6767
	}
unknown's avatar
unknown committed
6768 6769
	opt_delete_options single_multi {}
	;
6770 6771

single_multi:
unknown's avatar
unknown committed
6772 6773
 	FROM table_ident
	{
unknown's avatar
unknown committed
6774 6775
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
unknown's avatar
unknown committed
6776 6777 6778
	    YYABORT;
	}
	where_clause opt_order_clause
6779
	delete_limit_clause {}
unknown's avatar
unknown committed
6780
	| table_wild_list
unknown's avatar
unknown committed
6781
	  { mysql_init_multi_delete(Lex); }
unknown's avatar
unknown committed
6782
          FROM join_table_list where_clause
6783 6784 6785 6786
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
unknown's avatar
unknown committed
6787 6788
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
6789
	  USING join_table_list where_clause
6790 6791 6792 6793
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
6794
	;
unknown's avatar
unknown committed
6795 6796 6797

table_wild_list:
	  table_wild_one {}
unknown's avatar
unknown committed
6798
	  | table_wild_list ',' table_wild_one {};
unknown's avatar
unknown committed
6799 6800

table_wild_one:
unknown's avatar
unknown committed
6801
	ident opt_wild opt_table_alias
unknown's avatar
unknown committed
6802
	{
unknown's avatar
unknown committed
6803 6804
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
					 TL_OPTION_UPDATING, Lex->lock_option))
unknown's avatar
unknown committed
6805 6806
	    YYABORT;
        }
unknown's avatar
unknown committed
6807
	| ident '.' ident opt_wild opt_table_alias
unknown's avatar
unknown committed
6808
	  {
unknown's avatar
unknown committed
6809 6810
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
unknown's avatar
unknown committed
6811 6812
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
unknown's avatar
unknown committed
6813
	      YYABORT;
unknown's avatar
unknown committed
6814
	  }
unknown's avatar
unknown committed
6815
	;
unknown's avatar
unknown committed
6816 6817

opt_wild:
6818
	/* empty */	{}
unknown's avatar
unknown committed
6819
	| '.' '*'	{};
unknown's avatar
unknown committed
6820 6821


6822
opt_delete_options:
unknown's avatar
unknown committed
6823
	/* empty */	{}
unknown's avatar
unknown committed
6824
	| opt_delete_option opt_delete_options {};
6825 6826

opt_delete_option:
6827
	QUICK		{ Select->options|= OPTION_QUICK; }
6828
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
6829
	| IGNORE_SYM	{ Lex->ignore= 1; };
6830

6831
truncate:
unknown's avatar
unknown committed
6832
	TRUNCATE_SYM opt_table_sym table_name
6833
	{
6834
	  LEX* lex= Lex;
6835
	  lex->sql_command= SQLCOM_TRUNCATE;
6836
	  lex->select_lex.options= 0;
6837
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
6838 6839
	}
	;
6840

unknown's avatar
unknown committed
6841 6842
opt_table_sym:
	/* empty */
unknown's avatar
unknown committed
6843
	| TABLE_SYM;
6844

unknown's avatar
unknown committed
6845 6846
/* Show things */

6847 6848
show:	SHOW
	{
unknown's avatar
unknown committed
6849 6850
	  LEX *lex=Lex;
	  lex->wild=0;
6851 6852 6853
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
6854 6855
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
unknown's avatar
unknown committed
6856
	show_param
6857 6858
	{}
	;
unknown's avatar
unknown committed
6859 6860

show_param:
6861
         DATABASES wild_and_where
6862 6863 6864 6865 6866 6867 6868
         {
           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;
         }
6869
         | opt_full TABLES opt_db wild_and_where
6870 6871 6872 6873
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLES;
6874
             lex->select_lex.db= $3;
6875 6876 6877
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLE_NAMES))
               YYABORT;
           }
6878 6879 6880 6881 6882 6883 6884 6885 6886
         | 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;
           }
6887
         | TABLE_SYM STATUS_SYM opt_db wild_and_where
6888 6889 6890 6891
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLE_STATUS;
6892
             lex->select_lex.db= $3;
6893 6894 6895
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLES))
               YYABORT;
           }
6896
        | OPEN_SYM TABLES opt_db wild_and_where
unknown's avatar
unknown committed
6897
	  {
6898
	    LEX *lex= Lex;
6899 6900
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_OPEN_TABLES;
6901
	    lex->select_lex.db= $3;
6902 6903
            if (prepare_schema_table(YYTHD, lex, 0, SCH_OPEN_TABLES))
              YYABORT;
6904
	  }
unknown's avatar
unknown committed
6905
	| ENGINE_SYM storage_engines 
unknown's avatar
unknown committed
6906 6907
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
6908
	| opt_full COLUMNS from_or_in table_ident opt_db wild_and_where
unknown's avatar
unknown committed
6909
	  {
6910 6911 6912
 	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SELECT;
	    lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
6913 6914 6915
	    if ($5)
	      $4->change_db($5);
	    if (prepare_schema_table(YYTHD, lex, $4, SCH_COLUMNS))
unknown's avatar
unknown committed
6916 6917
	      YYABORT;
	  }
6918
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
unknown's avatar
unknown committed
6919 6920
	  TEXT_STRING_sys AND_SYM MASTER_LOG_POS_SYM EQ ulonglong_num
	  AND_SYM MASTER_SERVER_ID_SYM EQ
unknown's avatar
unknown committed
6921
	ulong_num
unknown's avatar
unknown committed
6922 6923 6924 6925
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
6926
	    Lex->mi.server_id = $16;
unknown's avatar
unknown committed
6927
          }
6928
        | master_or_binary LOGS_SYM
unknown's avatar
unknown committed
6929 6930
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
6931 6932 6933 6934 6935
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
unknown's avatar
unknown committed
6936
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
unknown's avatar
unknown committed
6937
          {
6938 6939
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
6940
          } opt_limit_clause_init
6941
        | keys_or_index from_or_in table_ident opt_db where_clause
6942 6943 6944 6945
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_KEYS;
6946 6947 6948
	    if ($4)
	      $3->change_db($4);
            if (prepare_schema_table(YYTHD, lex, $3, SCH_STATISTICS))
6949
              YYABORT;
unknown's avatar
unknown committed
6950
	  }
unknown's avatar
unknown committed
6951 6952 6953 6954 6955 6956 6957 6958
	| 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
6959 6960 6961 6962 6963 6964 6965
	    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
6966 6967 6968 6969 6970 6971
	  }
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
6972
        | COUNT_SYM '(' '*' ')' WARNINGS
6973
          { (void) create_select_for_variable("warning_count"); }
6974
        | COUNT_SYM '(' '*' ')' ERRORS
6975
	  { (void) create_select_for_variable("error_count"); }
6976
        | WARNINGS opt_limit_clause_init
unknown's avatar
unknown committed
6977
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
6978
        | ERRORS opt_limit_clause_init
6979
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
6980
        | opt_var_type STATUS_SYM wild_and_where
6981
          {
6982 6983 6984 6985 6986 6987
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS;
            lex->option_type= (enum_var_type) $1;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_STATUS))
              YYABORT;
6988
	  }	
unknown's avatar
unknown committed
6989
        | INNOBASE_SYM STATUS_SYM
unknown's avatar
unknown committed
6990
          { Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS; WARN_DEPRECATED("SHOW INNODB STATUS", "SHOW ENGINE INNODB STATUS"); }
unknown's avatar
unknown committed
6991 6992
        | MUTEX_SYM STATUS_SYM
          { Lex->sql_command = SQLCOM_SHOW_MUTEX_STATUS; }
unknown's avatar
unknown committed
6993 6994
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
6995
        | opt_var_type  VARIABLES wild_and_where
unknown's avatar
unknown committed
6996
	  {
6997 6998 6999 7000 7001 7002
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_VARIABLES;
            lex->option_type= (enum_var_type) $1;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_VARIABLES))
              YYABORT;
7003
	  }
7004
        | charset wild_and_where
7005 7006 7007 7008 7009 7010 7011
          {
            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;
          }
7012
        | COLLATION_SYM wild_and_where
7013 7014 7015 7016 7017 7018 7019
          {
            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;
          }
7020
	| BERKELEY_DB_SYM LOGS_SYM
unknown's avatar
unknown committed
7021
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW BDB LOGS", "SHOW ENGINE BDB LOGS"); }
unknown's avatar
unknown committed
7022
	| LOGS_SYM
unknown's avatar
unknown committed
7023
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW LOGS", "SHOW ENGINE BDB LOGS"); }
7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043
	| GRANTS
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    THD *thd= lex->thd;
	    LEX_USER *curr_user;
            if (!(curr_user= (LEX_USER*) thd->alloc(sizeof(st_lex_user))))
              YYABORT;
            curr_user->user.str= thd->priv_user;
            curr_user->user.length= strlen(thd->priv_user);
            if (*thd->priv_host != 0)
            {
              curr_user->host.str= thd->priv_host;
              curr_user->host.length= strlen(thd->priv_host);
            }
            else
            {
              curr_user->host.str= (char *) "%";
              curr_user->host.length= 1;
            }
7044
            curr_user->password=null_lex_str;
7045 7046
	    lex->grant_user= curr_user;
	  }
unknown's avatar
unknown committed
7047
	| GRANTS FOR_SYM user
unknown's avatar
unknown committed
7048 7049 7050 7051
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
7052
	    lex->grant_user->password=null_lex_str;
unknown's avatar
unknown committed
7053
	  }
unknown's avatar
unknown committed
7054
	| CREATE DATABASE opt_if_not_exists ident
unknown's avatar
unknown committed
7055 7056
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
unknown's avatar
unknown committed
7057 7058
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
unknown's avatar
unknown committed
7059
	  }
unknown's avatar
unknown committed
7060 7061
        | CREATE TABLE_SYM table_ident
          {
7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072
            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
7073
	      YYABORT;
7074
            lex->only_view= 1;
unknown's avatar
unknown committed
7075 7076 7077 7078
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
unknown's avatar
unknown committed
7079
          }
unknown's avatar
unknown committed
7080 7081 7082
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097
          }
	| 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;
	  }
7098
	| PROCEDURE STATUS_SYM wild_and_where
7099
	  {
7100 7101 7102
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_PROC;
7103 7104
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
7105 7106
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
7107
	  }
7108
	| FUNCTION_SYM STATUS_SYM wild_and_where
7109
	  {
7110 7111 7112
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_FUNC;
7113 7114
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
7115 7116
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
7117
	  };
unknown's avatar
unknown committed
7118

unknown's avatar
unknown committed
7119 7120 7121 7122 7123 7124 7125 7126
show_engine_param:
	STATUS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_INNODB:
	      Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;
	      break;
	    default:
unknown's avatar
unknown committed
7127
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "STATUS");
unknown's avatar
unknown committed
7128 7129 7130 7131 7132 7133 7134 7135 7136 7137
	      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
7138
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "LOGS");
unknown's avatar
unknown committed
7139 7140 7141 7142
	      YYABORT;
	    }
	  };

7143 7144 7145 7146
master_or_binary:
	MASTER_SYM
	| BINARY;

unknown's avatar
unknown committed
7147 7148 7149 7150
opt_storage:
	/* empty */
	| STORAGE_SYM;

unknown's avatar
unknown committed
7151 7152
opt_db:
	/* empty */  { $$= 0; }
unknown's avatar
unknown committed
7153
	| from_or_in ident { $$= $2.str; };
unknown's avatar
unknown committed
7154

unknown's avatar
unknown committed
7155 7156
opt_full:
	/* empty */ { Lex->verbose=0; }
unknown's avatar
unknown committed
7157
	| FULL	    { Lex->verbose=1; };
unknown's avatar
unknown committed
7158

unknown's avatar
unknown committed
7159 7160
from_or_in:
	FROM
unknown's avatar
unknown committed
7161
	| IN_SYM;
unknown's avatar
unknown committed
7162

unknown's avatar
unknown committed
7163 7164
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
unknown's avatar
unknown committed
7165
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
unknown's avatar
unknown committed
7166 7167 7168

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

7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183
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
7184

unknown's avatar
unknown committed
7185 7186 7187 7188
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
7189 7190 7191 7192 7193 7194 7195 7196 7197
          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
7198 7199
	    YYABORT;
	}
7200
	opt_describe_column {}
unknown's avatar
unknown committed
7201 7202 7203
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
7204
          {
unknown's avatar
unknown committed
7205
	    LEX *lex=Lex;
7206
	    lex->select_lex.options|= SELECT_DESCRIBE;
unknown's avatar
unknown committed
7207 7208
	  }
	;
unknown's avatar
unknown committed
7209 7210 7211

describe_command:
	DESC
unknown's avatar
unknown committed
7212
	| DESCRIBE;
unknown's avatar
unknown committed
7213

unknown's avatar
unknown committed
7214 7215 7216 7217 7218
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
	;

unknown's avatar
unknown committed
7219 7220 7221
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
unknown's avatar
unknown committed
7222
	| ident
unknown's avatar
unknown committed
7223
	  { Lex->wild= new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info); };
unknown's avatar
unknown committed
7224 7225 7226 7227 7228


/* flush things */

flush:
7229
	FLUSH_SYM opt_no_write_to_binlog
unknown's avatar
unknown committed
7230 7231
	{
	  LEX *lex=Lex;
7232 7233 7234 7235 7236
	  if (lex->sphead && lex->sphead->m_type == TYPE_ENUM_FUNCTION)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "FLUSH");
	    YYABORT;
	  }
unknown's avatar
unknown committed
7237
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
unknown's avatar
unknown committed
7238
          lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
7239
	}
7240 7241 7242
	flush_options
	{}
	;
unknown's avatar
unknown committed
7243 7244 7245

flush_options:
	flush_options ',' flush_option
unknown's avatar
unknown committed
7246
	| flush_option;
unknown's avatar
unknown committed
7247 7248

flush_option:
7249
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
unknown's avatar
unknown committed
7250
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
unknown's avatar
unknown committed
7251
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
unknown's avatar
unknown committed
7252 7253 7254 7255
	| 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
7256 7257
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
7258 7259
	| DES_KEY_FILE	{ Lex->type|= REFRESH_DES_KEY_FILE; }
 	| RESOURCES     { Lex->type|= REFRESH_USER_RESOURCES; };
unknown's avatar
unknown committed
7260

unknown's avatar
unknown committed
7261
opt_table_list:
7262 7263
	/* empty */  {;}
	| table_list {;};
unknown's avatar
unknown committed
7264

unknown's avatar
unknown committed
7265
reset:
unknown's avatar
unknown committed
7266 7267 7268 7269
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
7270 7271 7272 7273
	} reset_options
	{}
	;

unknown's avatar
unknown committed
7274 7275
reset_options:
	reset_options ',' reset_option
unknown's avatar
unknown committed
7276
	| reset_option;
unknown's avatar
unknown committed
7277 7278

reset_option:
unknown's avatar
unknown committed
7279 7280
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
unknown's avatar
unknown committed
7281
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
unknown's avatar
unknown committed
7282

unknown's avatar
unknown committed
7283
purge:
unknown's avatar
unknown committed
7284 7285 7286 7287
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
7288 7289 7290 7291 7292
	} purge_options
	{}
	;

purge_options:
7293
	master_or_binary LOGS_SYM purge_option
unknown's avatar
unknown committed
7294
	;
7295 7296

purge_option:
unknown's avatar
unknown committed
7297
        TO_SYM TEXT_STRING_sys
7298 7299 7300 7301 7302 7303
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
7304 7305 7306 7307
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
unknown's avatar
unknown committed
7308
	}
unknown's avatar
unknown committed
7309
	;
unknown's avatar
unknown committed
7310

unknown's avatar
unknown committed
7311 7312 7313
/* kill threads */

kill:
7314
	KILL_SYM kill_option expr
unknown's avatar
unknown committed
7315
	{
unknown's avatar
unknown committed
7316
	  LEX *lex=Lex;
7317 7318 7319
	  lex->value_list.empty();
	  lex->value_list.push_front($3);
          lex->sql_command= SQLCOM_KILL;
unknown's avatar
unknown committed
7320
	};
unknown's avatar
unknown committed
7321

7322 7323 7324 7325 7326
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; };

unknown's avatar
unknown committed
7327 7328 7329
/* change database */

use:	USE_SYM ident
unknown's avatar
unknown committed
7330 7331
	{
	  LEX *lex=Lex;
7332 7333
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
unknown's avatar
unknown committed
7334
	};
unknown's avatar
unknown committed
7335 7336 7337

/* import, export of files */

unknown's avatar
unknown committed
7338 7339 7340
load:   LOAD DATA_SYM 
        {
          LEX *lex=Lex;
7341 7342 7343 7344 7345
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD DATA");
	    YYABORT;
	  }
unknown's avatar
unknown committed
7346 7347 7348 7349 7350 7351 7352
          lex->fname_start= lex->ptr;
        }
        load_data
        {}
        |
        LOAD TABLE_SYM table_ident FROM MASTER_SYM
        {
7353 7354 7355 7356 7357 7358 7359
	  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
7360 7361 7362 7363 7364 7365
          if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
            YYABORT;
        };

load_data:
	load_data_lock opt_local INFILE TEXT_STRING_sys
unknown's avatar
unknown committed
7366
	{
unknown's avatar
unknown committed
7367 7368
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
unknown's avatar
unknown committed
7369 7370
	  lex->lock_option= $1;
	  lex->local_file=  $2;
7371 7372
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
unknown's avatar
unknown committed
7373
	  if (!(lex->exchange= new sql_exchange($4.str, 0)))
unknown's avatar
unknown committed
7374
	    YYABORT;
unknown's avatar
unknown committed
7375 7376 7377 7378 7379
        }
        opt_duplicate INTO
        {
	  LEX *lex=Lex;
	  lex->fname_end= lex->ptr;
unknown's avatar
unknown committed
7380
	}
7381
        TABLE_SYM table_ident
unknown's avatar
unknown committed
7382
        {
7383 7384 7385 7386 7387 7388 7389
          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
7390
        }
7391 7392 7393
        opt_field_term opt_line_term opt_ignore_lines opt_field_or_var_spec
        opt_load_data_set_spec
        {}
7394
        |
unknown's avatar
unknown committed
7395
	FROM MASTER_SYM
7396 7397
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
unknown's avatar
unknown committed
7398
        };
unknown's avatar
unknown committed
7399 7400 7401

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

unknown's avatar
unknown committed
7404
load_data_lock:
7405
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
7406 7407
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
7408 7409


unknown's avatar
unknown committed
7410 7411 7412
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
7413
	| IGNORE_SYM	{ Lex->ignore= 1; };
unknown's avatar
unknown committed
7414 7415 7416

opt_field_term:
	/* empty */
unknown's avatar
unknown committed
7417
	| COLUMNS field_term_list;
unknown's avatar
unknown committed
7418 7419 7420

field_term_list:
	field_term_list field_term
unknown's avatar
unknown committed
7421
	| field_term;
unknown's avatar
unknown committed
7422 7423

field_term:
7424 7425
	TERMINATED BY text_string 
          {
7426
            DBUG_ASSERT(Lex->exchange != 0);
7427 7428
            Lex->exchange->field_term= $3;
          }
unknown's avatar
unknown committed
7429
	| OPTIONALLY ENCLOSED BY text_string
unknown's avatar
unknown committed
7430
	  {
unknown's avatar
unknown committed
7431
            LEX *lex= Lex;
7432
            DBUG_ASSERT(lex->exchange != 0);
unknown's avatar
unknown committed
7433 7434
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
unknown's avatar
unknown committed
7435
	  }
unknown's avatar
unknown committed
7436
        | ENCLOSED BY text_string
7437
          {
7438
            DBUG_ASSERT(Lex->exchange != 0);
7439 7440
            Lex->exchange->enclosed= $3;
          }
unknown's avatar
unknown committed
7441
        | ESCAPED BY text_string
7442
          {
7443
            DBUG_ASSERT(Lex->exchange != 0);
7444 7445
            Lex->exchange->escaped= $3;
          };
unknown's avatar
unknown committed
7446 7447 7448

opt_line_term:
	/* empty */
unknown's avatar
unknown committed
7449
	| LINES line_term_list;
unknown's avatar
unknown committed
7450 7451 7452

line_term_list:
	line_term_list line_term
unknown's avatar
unknown committed
7453
	| line_term;
unknown's avatar
unknown committed
7454 7455

line_term:
unknown's avatar
unknown committed
7456
        TERMINATED BY text_string
7457
          {
7458
            DBUG_ASSERT(Lex->exchange != 0);
7459 7460
            Lex->exchange->line_term= $3;
          }
unknown's avatar
unknown committed
7461
        | STARTING BY text_string
7462
          {
7463
            DBUG_ASSERT(Lex->exchange != 0);
7464 7465
            Lex->exchange->line_start= $3;
          };
unknown's avatar
unknown committed
7466 7467 7468

opt_ignore_lines:
	/* empty */
unknown's avatar
unknown committed
7469 7470
        | IGNORE_SYM NUM LINES
          {
7471
            DBUG_ASSERT(Lex->exchange != 0);
7472 7473
            Lex->exchange->skip_lines= atol($2.str);
          };
unknown's avatar
unknown committed
7474

unknown's avatar
unknown committed
7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485
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
7486

unknown's avatar
unknown committed
7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497
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
7498 7499 7500
/* Common definitions */

text_literal:
7501
	TEXT_STRING_literal
unknown's avatar
unknown committed
7502 7503
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7504
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
unknown's avatar
unknown committed
7505
	}
unknown's avatar
unknown committed
7506
	| NCHAR_STRING
7507
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
7508
	| UNDERSCORE_CHARSET TEXT_STRING
7509
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
7510
	| text_literal TEXT_STRING_literal
unknown's avatar
unknown committed
7511 7512
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
unknown's avatar
unknown committed
7513 7514

text_string:
7515
	TEXT_STRING_literal
unknown's avatar
unknown committed
7516
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
unknown's avatar
unknown committed
7517 7518
	| HEX_NUM
	  {
unknown's avatar
unknown committed
7519
	    Item *tmp= new Item_hex_string($1.str, $1.length);
7520
	    /*
unknown's avatar
unknown committed
7521
	      it is OK only emulate fix_fields, because we need only
7522 7523 7524
              value of constant
	    */
	    $$= tmp ?
unknown's avatar
unknown committed
7525
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
7526
	      (String*) 0;
7527
	  }
unknown's avatar
unknown committed
7528 7529 7530 7531 7532 7533 7534 7535 7536 7537
        | 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;
          }
7538 7539
	;

unknown's avatar
unknown committed
7540
param_marker:
7541
        PARAM_MARKER
unknown's avatar
unknown committed
7542
        {
7543 7544
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
7545 7546 7547
          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
7548
          {
7549
            my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
unknown's avatar
unknown committed
7550 7551
            YYABORT;
          }
7552 7553 7554
        }
	;

unknown's avatar
unknown committed
7555 7556 7557
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
7558 7559
	| '-' NUM_literal
	  {
7560
	    $2->max_length++;
unknown's avatar
unknown committed
7561
	    $$= $2->neg();
7562
	  }
unknown's avatar
unknown committed
7563 7564 7565
	;


unknown's avatar
unknown committed
7566 7567
literal:
	text_literal	{ $$ =	$1; }
unknown's avatar
unknown committed
7568
	| NUM_literal	{ $$ = $1; }
unknown's avatar
unknown committed
7569
	| NULL_SYM	{ $$ =	new Item_null();
7570
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
7571 7572
	| FALSE_SYM	{ $$= new Item_int((char*) "FALSE",0,1); }
	| TRUE_SYM	{ $$= new Item_int((char*) "TRUE",1,1); }
unknown's avatar
unknown committed
7573 7574
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
7575
	| UNDERSCORE_CHARSET HEX_NUM
7576
	  {
unknown's avatar
unknown committed
7577
	    Item *tmp= new Item_hex_string($2.str, $2.length);
7578
	    /*
unknown's avatar
unknown committed
7579
	      it is OK only emulate fix_fieds, because we need only
7580 7581 7582
              value of constant
	    */
	    String *str= tmp ?
unknown's avatar
unknown committed
7583
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
7584
	      (String*) 0;
unknown's avatar
unknown committed
7585 7586 7587
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
7588
	  }
unknown's avatar
unknown committed
7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602
	| 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
7603 7604
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
unknown's avatar
unknown committed
7605
	| TIMESTAMP text_literal { $$ = $2; };
unknown's avatar
unknown committed
7606

7607
NUM_literal:
7608 7609
	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); }
7610
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
unknown's avatar
unknown committed
7611
        | DECIMAL_NUM
7612
	{
unknown's avatar
unknown committed
7613
           $$= new Item_decimal($1.str, $1.length, YYTHD->charset());
7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
7627
	;
unknown's avatar
unknown committed
7628

unknown's avatar
unknown committed
7629
/**********************************************************************
unknown's avatar
unknown committed
7630
** Creating different items.
unknown's avatar
unknown committed
7631 7632 7633
**********************************************************************/

insert_ident:
7634
	simple_ident_nospvar { $$=$1; }
unknown's avatar
unknown committed
7635
	| table_wild	 { $$=$1; };
unknown's avatar
unknown committed
7636 7637

table_wild:
unknown's avatar
unknown committed
7638
	ident '.' '*'
7639
	{
7640 7641 7642
          SELECT_LEX *sel= Select;
	  $$ = new Item_field(&sel->context, NullS, $1.str, "*");
	  sel->with_wild++;
7643
	}
unknown's avatar
unknown committed
7644
	| ident '.' ident '.' '*'
7645
	{
7646 7647
          SELECT_LEX *sel= Select;
	  $$ = new Item_field(&sel->context, (YYTHD->client_capabilities &
unknown's avatar
unknown committed
7648 7649
                             CLIENT_NO_SCHEMA ? NullS : $1.str),
                             $3.str,"*");
7650
	  sel->with_wild++;
7651 7652
	}
	;
unknown's avatar
unknown committed
7653 7654

order_ident:
unknown's avatar
unknown committed
7655
	expr { $$=$1; };
unknown's avatar
unknown committed
7656 7657

simple_ident:
7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674
	ident
	{
	  sp_pvar_t *spv;
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;

	  if (spc && (spv = spc->find_pvar(&$1)))
	  { /* We're compiling a stored procedure and found a variable */
	    $$ = (Item*) new Item_splocal($1, spv->offset);
            lex->variables_used= 1;
	    lex->safe_to_cache_query=0;
	  }
	  else
	  {
	    SELECT_LEX *sel=Select;
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
7675 7676
                 (Item*) new Item_field(&sel->context, NullS, NullS, $1.str) :
	         (Item*) new Item_ref(&sel->context, NullS, NullS, $1.str);
7677 7678 7679 7680 7681 7682
	  }
        }
        | simple_ident_q { $$= $1; }
	;

simple_ident_nospvar:
unknown's avatar
unknown committed
7683
	ident
unknown's avatar
unknown committed
7684
	{
unknown's avatar
unknown committed
7685
	  SELECT_LEX *sel=Select;
7686
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
7687
	       sel->get_in_sum_expr() > 0) ?
7688 7689
              (Item*) new Item_field(&sel->context, NullS, NullS, $1.str) :
	      (Item*) new Item_ref(&sel->context, NullS, NullS, $1.str);
unknown's avatar
unknown committed
7690
	}
7691
	| simple_ident_q { $$= $1; }
unknown's avatar
unknown committed
7692
	;
7693 7694 7695

simple_ident_q:
	ident '.' ident
unknown's avatar
unknown committed
7696
	{
unknown's avatar
unknown committed
7697
	  THD *thd= YYTHD;
7698
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
7699

7700 7701 7702 7703 7704 7705
          /*
            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
7706
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") ||
7707 7708
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
7709
            Item_trigger_field *trg_fld;
7710
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
unknown's avatar
unknown committed
7711

7712 7713 7714
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
unknown's avatar
unknown committed
7715
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
7716 7717
              YYABORT;
            }
unknown's avatar
unknown committed
7718

7719 7720 7721
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
unknown's avatar
unknown committed
7722
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
7723 7724
              YYABORT;
            }
unknown's avatar
unknown committed
7725

7726 7727
            if (!(trg_fld= new Item_trigger_field(&lex->current_select->context,
                                                  new_row ?
7728 7729 7730
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
                                                  $3.str)))
7731
              YYABORT;
unknown's avatar
unknown committed
7732

7733 7734 7735 7736 7737 7738
            /*
              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
7739

7740 7741 7742 7743 7744 7745 7746
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
7747 7748
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
7749 7750 7751
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
7752 7753
	        (Item*) new Item_field(&sel->context, NullS, $1.str, $3.str) :
	        (Item*) new Item_ref(&sel->context, NullS, $1.str, $3.str);
7754 7755
          }
        }
unknown's avatar
unknown committed
7756
	| '.' ident '.' ident
unknown's avatar
unknown committed
7757
	{
unknown's avatar
unknown committed
7758
	  THD *thd= YYTHD;
7759
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
7760
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
7761 7762
	  if (sel->no_table_names_allowed)
	  {
7763 7764
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $2.str, thd->where);
unknown's avatar
unknown committed
7765
	  }
7766
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
7767
	       sel->get_in_sum_expr() > 0) ?
7768 7769
	      (Item*) new Item_field(&sel->context, NullS, $2.str, $4.str) :
              (Item*) new Item_ref(&sel->context, NullS, $2.str, $4.str);
unknown's avatar
unknown committed
7770
	}
unknown's avatar
unknown committed
7771
	| ident '.' ident '.' ident
unknown's avatar
unknown committed
7772
	{
unknown's avatar
unknown committed
7773
	  THD *thd= YYTHD;
7774
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
7775
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
7776 7777
	  if (sel->no_table_names_allowed)
	  {
7778 7779
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $3.str, thd->where);
unknown's avatar
unknown committed
7780
	  }
7781
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
7782
	       sel->get_in_sum_expr() > 0) ?
7783 7784
	      (Item*) new Item_field(&sel->context,
                                     (YYTHD->client_capabilities &
unknown's avatar
unknown committed
7785 7786
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
7787 7788
	      (Item*) new Item_ref(&sel->context,
                                   (YYTHD->client_capabilities &
7789
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
unknown's avatar
unknown committed
7790
                                   $3.str, $5.str);
unknown's avatar
unknown committed
7791
	};
unknown's avatar
unknown committed
7792 7793 7794 7795


field_ident:
	ident			{ $$=$1;}
7796 7797 7798 7799 7800
	| ident '.' ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->db))
            {
7801
              my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
7802 7803
              YYABORT;
            }
7804 7805
            if (my_strcasecmp(table_alias_charset, $3.str,
                              table->table_name))
7806
            {
7807
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3.str);
7808 7809 7810 7811 7812 7813 7814 7815 7816
              YYABORT;
            }
            $$=$5;
          }
	| ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->alias))
            {
7817
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $1.str);
7818 7819 7820 7821
              YYABORT;
            }
            $$=$3;
          }
unknown's avatar
unknown committed
7822
	| '.' ident		{ $$=$2;}	/* For Delphi */;
unknown's avatar
unknown committed
7823 7824 7825

table_ident:
	ident			{ $$=new Table_ident($1); }
unknown's avatar
unknown committed
7826
	| ident '.' ident	{ $$=new Table_ident(YYTHD, $1,$3,0);}
7827 7828 7829
	| '.' ident		{ $$=new Table_ident($2);} /* For Delphi */
        ;

unknown's avatar
unknown committed
7830
table_ident_nodb:
unknown's avatar
unknown committed
7831
	ident			{ LEX_STRING db={(char*) any_db,3}; $$=new Table_ident(YYTHD, db,$1,0); }
7832
        ;
unknown's avatar
unknown committed
7833

unknown's avatar
unknown committed
7834
IDENT_sys:
7835 7836 7837 7838 7839
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
7840 7841
            {
              CHARSET_INFO *cs= system_charset_info;
7842
              int dummy_error;
7843 7844
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
7845
                                                   $1.length, &dummy_error);
7846 7847
              if (wlen < $1.length)
              {
7848 7849
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
7850 7851
                YYABORT;
              }
7852
	      $$= $1;
7853
            }
7854 7855 7856 7857
	    else
	      thd->convert_string(&$$, system_charset_info,
				  $1.str, $1.length, thd->charset());
	  }
unknown's avatar
unknown committed
7858 7859 7860 7861 7862 7863
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7864 7865
	  if (thd->charset_is_system_charset)
	    $$= $1;
unknown's avatar
unknown committed
7866
	  else
unknown's avatar
unknown committed
7867 7868
	    thd->convert_string(&$$, system_charset_info,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
7869 7870 7871
	}
	;

7872
TEXT_STRING_literal:
unknown's avatar
unknown committed
7873 7874 7875
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7876 7877
	  if (thd->charset_is_collation_connection)
	    $$= $1;
unknown's avatar
unknown committed
7878
	  else
unknown's avatar
unknown committed
7879 7880
	    thd->convert_string(&$$, thd->variables.collation_connection,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
7881 7882 7883 7884
	}
	;


unknown's avatar
unknown committed
7885
ident:
unknown's avatar
unknown committed
7886
	IDENT_sys	    { $$=$1; }
unknown's avatar
unknown committed
7887 7888
	| keyword
	{
unknown's avatar
unknown committed
7889 7890 7891
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
7892 7893
	}
	;
unknown's avatar
unknown committed
7894

7895 7896 7897 7898 7899 7900 7901 7902 7903 7904
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
7905
ident_or_text:
unknown's avatar
unknown committed
7906
        ident                   { $$=$1;}
unknown's avatar
unknown committed
7907 7908
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
unknown's avatar
unknown committed
7909 7910 7911 7912

user:
	ident_or_text
	{
unknown's avatar
unknown committed
7913 7914
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
7915
	    YYABORT;
7916 7917 7918 7919
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
unknown's avatar
unknown committed
7920 7921
	| ident_or_text '@' ident_or_text
	  {
unknown's avatar
unknown committed
7922 7923
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
7924 7925
	      YYABORT;
	    $$->user = $1; $$->host=$3;
7926
	  }
unknown's avatar
unknown committed
7927
	| CURRENT_USER optional_braces
7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944
	{
          THD *thd= YYTHD;
          if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
            YYABORT;
          $$->user.str= thd->priv_user;
          $$->user.length= strlen(thd->priv_user);
          if (*thd->priv_host != 0)
          {
            $$->host.str= thd->priv_host;
            $$->host.length= strlen(thd->priv_host);
          }
          else
          {
            $$->host.str= (char *) "%";
            $$->host.length= 1;
          }
	};
unknown's avatar
unknown committed
7945

7946
/* Keyword that we allow for identifiers (except SP labels) */
unknown's avatar
unknown committed
7947
keyword:
7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969
	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		{}
7970
	| PARTITION_SYM		{}
7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988
        | 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
7989 7990 7991
 * Anything that's the beginning of a statement or characteristics
 * must be in keyword above, otherwise we get (harmful) shift/reduce
 * conflicts.
7992 7993
 */
keyword_sp:
unknown's avatar
unknown committed
7994
	ACTION			{}
unknown's avatar
unknown committed
7995
	| ADDDATE_SYM		{}
unknown's avatar
unknown committed
7996
	| AFTER_SYM		{}
7997
	| AGAINST		{}
unknown's avatar
unknown committed
7998
	| AGGREGATE_SYM		{}
7999
	| ALGORITHM_SYM		{}
unknown's avatar
unknown committed
8000
	| ANY_SYM		{}
unknown's avatar
unknown committed
8001
	| AUTO_INC		{}
unknown's avatar
unknown committed
8002 8003
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
unknown's avatar
unknown committed
8004
	| BERKELEY_DB_SYM	{}
unknown's avatar
unknown committed
8005
	| BINLOG_SYM		{}
unknown's avatar
unknown committed
8006 8007
	| BIT_SYM		{}
	| BOOL_SYM		{}
unknown's avatar
unknown committed
8008
	| BOOLEAN_SYM		{}
unknown's avatar
unknown committed
8009
	| BTREE_SYM		{}
8010
	| CASCADED              {}
unknown's avatar
unknown committed
8011
	| CHAIN_SYM		{}
8012
	| CHANGED		{}
8013
	| CIPHER_SYM		{}
unknown's avatar
unknown committed
8014
	| CLIENT_SYM		{}
unknown's avatar
unknown committed
8015
	| COALESCE		{}
unknown's avatar
unknown committed
8016
	| COLLATION_SYM		{}
8017
        | COLUMNS               {}
unknown's avatar
unknown committed
8018
	| COMMITTED_SYM		{}
8019
	| COMPACT_SYM		{}
unknown's avatar
unknown committed
8020
	| COMPRESSED_SYM	{}
unknown's avatar
unknown committed
8021
	| CONCURRENT		{}
8022
	| CONSISTENT_SYM	{}
8023
	| CUBE_SYM		{}
unknown's avatar
unknown committed
8024 8025 8026 8027
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
8028
	| DEFINER_SYM		{}
unknown's avatar
unknown committed
8029
	| DELAY_KEY_WRITE_SYM	{}
unknown's avatar
unknown committed
8030 8031
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
unknown's avatar
unknown committed
8032
	| DISCARD		{}
unknown's avatar
unknown committed
8033 8034
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
unknown's avatar
unknown committed
8035 8036
	| DYNAMIC_SYM		{}
	| ENUM			{}
unknown's avatar
unknown committed
8037
	| ENGINE_SYM		{}
unknown's avatar
unknown committed
8038
	| ENGINES_SYM		{}
8039
	| ERRORS		{}
unknown's avatar
unknown committed
8040
	| ESCAPE_SYM		{}
unknown's avatar
unknown committed
8041
	| EVENTS_SYM		{}
8042
        | EXPANSION_SYM         {}
unknown's avatar
unknown committed
8043
	| EXTENDED_SYM		{}
8044
	| FAST_SYM		{}
8045
	| FOUND_SYM		{}
unknown's avatar
unknown committed
8046 8047
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
unknown's avatar
unknown committed
8048
	| FULL			{}
unknown's avatar
unknown committed
8049 8050 8051
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
8052
	| FRAC_SECOND_SYM	{}
8053
	| GEOMETRY_SYM		{}
8054
	| GEOMETRYCOLLECTION	{}
8055
	| GET_FORMAT		{}
unknown's avatar
unknown committed
8056
	| GRANTS		{}
unknown's avatar
unknown committed
8057
	| GLOBAL_SYM		{}
unknown's avatar
unknown committed
8058
	| HASH_SYM		{}
unknown's avatar
unknown committed
8059 8060 8061
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
8062
	| INVOKER_SYM		{}
unknown's avatar
unknown committed
8063
	| IMPORT		{}
unknown's avatar
unknown committed
8064
	| INDEXES		{}
unknown's avatar
unknown committed
8065
	| ISOLATION		{}
unknown's avatar
unknown committed
8066
	| ISSUER_SYM		{}
unknown's avatar
unknown committed
8067
	| INNOBASE_SYM		{}
8068
	| INSERT_METHOD		{}
unknown's avatar
unknown committed
8069
	| RELAY_THREAD		{}
8070
	| LAST_SYM		{}
unknown's avatar
unknown committed
8071
	| LEAVES                {}
8072
	| LESS_SYM		{}
unknown's avatar
unknown committed
8073
	| LEVEL_SYM		{}
8074
	| LINESTRING		{}
8075
	| LIST_SYM		{}
unknown's avatar
unknown committed
8076
	| LOCAL_SYM		{}
unknown's avatar
unknown committed
8077
	| LOCKS_SYM		{}
unknown's avatar
unknown committed
8078 8079 8080 8081 8082 8083 8084 8085 8086
	| 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
8087
	| MASTER_SERVER_ID_SYM  {}
unknown's avatar
unknown committed
8088
	| MASTER_CONNECT_RETRY_SYM	{}
unknown's avatar
unknown committed
8089 8090 8091 8092 8093 8094
	| 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
8095 8096 8097
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
8098
	| MAX_USER_CONNECTIONS_SYM {}
8099
	| MAX_VALUE_SYM         {}
8100
	| MEDIUM_SYM		{}
8101
	| MERGE_SYM		{}
unknown's avatar
unknown committed
8102
	| MICROSECOND_SYM	{}
8103
        | MIGRATE_SYM           {}
unknown's avatar
unknown committed
8104 8105 8106
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
unknown's avatar
unknown committed
8107
	| MODE_SYM		{}
unknown's avatar
unknown committed
8108
	| MONTH_SYM		{}
8109 8110 8111
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
8112
        | MUTEX_SYM             {}
8113
	| NAME_SYM              {}
unknown's avatar
unknown committed
8114
	| NAMES_SYM		{}
unknown's avatar
unknown committed
8115 8116
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
unknown's avatar
unknown committed
8117
	| NDBCLUSTER_SYM	{}
8118
	| NEXT_SYM		{}
unknown's avatar
unknown committed
8119
	| NEW_SYM		{}
8120
	| NODEGROUP_SYM		{}
8121
	| NONE_SYM		{}
unknown's avatar
unknown committed
8122
	| NVARCHAR_SYM		{}
unknown's avatar
unknown committed
8123
	| OFFSET_SYM		{}
unknown's avatar
unknown committed
8124
	| OLD_PASSWORD		{}
8125
	| ONE_SHOT_SYM		{}
8126
        | ONE_SYM               {}
unknown's avatar
unknown committed
8127
	| PACK_KEYS_SYM		{}
8128
	| PARTIAL		{}
8129
	| PARTITIONS_SYM	{}
unknown's avatar
unknown committed
8130
	| PASSWORD		{}
8131
        | PHASE_SYM             {}
8132
	| POINT_SYM		{}
8133
	| POLYGON		{}
8134
	| PREV_SYM		{}
8135
        | PRIVILEGES            {}
unknown's avatar
unknown committed
8136 8137
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
8138
	| QUARTER_SYM		{}
unknown's avatar
unknown committed
8139
	| QUERY_SYM		{}
8140
	| QUICK			{}
unknown's avatar
unknown committed
8141
	| RAID_0_SYM		{}
unknown's avatar
unknown committed
8142 8143
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
unknown's avatar
unknown committed
8144
	| RAID_STRIPED_SYM	{}
unknown's avatar
unknown committed
8145
	| RAID_TYPE		{}
8146
        | RECOVER_SYM           {}
unknown's avatar
unknown committed
8147
        | REDUNDANT_SYM         {}
unknown's avatar
unknown committed
8148 8149
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
unknown's avatar
unknown committed
8150
	| RELOAD		{}
unknown's avatar
unknown committed
8151
	| REORGANISE_SYM	{}
unknown's avatar
unknown committed
8152
	| REPEATABLE_SYM	{}
unknown's avatar
unknown committed
8153
	| REPLICATION		{}
8154
	| RESOURCES		{}
8155
        | RESUME_SYM            {}
8156
	| RETURNS_SYM           {}
8157
	| ROLLUP_SYM		{}
8158
	| ROUTINE_SYM		{}
unknown's avatar
unknown committed
8159 8160 8161
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
unknown's avatar
unknown committed
8162
	| RTREE_SYM		{}
unknown's avatar
unknown committed
8163
	| SECOND_SYM		{}
8164
	| SERIAL_SYM		{}
unknown's avatar
unknown committed
8165 8166
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
8167
	| SIMPLE_SYM		{}
unknown's avatar
unknown committed
8168
	| SHARE_SYM		{}
unknown's avatar
unknown committed
8169
	| SHUTDOWN		{}
8170
	| SNAPSHOT_SYM		{}
unknown's avatar
unknown committed
8171
	| SOUNDS_SYM		{}
unknown's avatar
unknown committed
8172
	| SQL_CACHE_SYM		{}
unknown's avatar
unknown committed
8173
	| SQL_BUFFER_RESULT	{}
unknown's avatar
unknown committed
8174
	| SQL_NO_CACHE_SYM	{}
unknown's avatar
unknown committed
8175
	| SQL_THREAD		{}
unknown's avatar
unknown committed
8176
	| STATUS_SYM		{}
unknown's avatar
unknown committed
8177
	| STORAGE_SYM		{}
unknown's avatar
unknown committed
8178
	| STRING_SYM		{}
unknown's avatar
unknown committed
8179
	| SUBDATE_SYM		{}
unknown's avatar
unknown committed
8180
	| SUBJECT_SYM		{}
8181 8182
	| SUBPARTITION_SYM	{}
	| SUBPARTITIONS_SYM	{}
unknown's avatar
unknown committed
8183
	| SUPER_SYM		{}
8184
        | SUSPEND_SYM           {}
8185
        | TABLES                {}
unknown's avatar
unknown committed
8186
	| TABLESPACE		{}
unknown's avatar
unknown committed
8187
	| TEMPORARY		{}
8188
	| TEMPTABLE_SYM		{}
unknown's avatar
unknown committed
8189
	| TEXT_SYM		{}
8190
	| THAN_SYM		{}
unknown's avatar
unknown committed
8191
	| TRANSACTION_SYM	{}
8192
	| TRIGGERS_SYM		{}
unknown's avatar
unknown committed
8193
	| TIMESTAMP		{}
8194 8195
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
unknown's avatar
unknown committed
8196
	| TIME_SYM		{}
unknown's avatar
unknown committed
8197
	| TYPES_SYM		{}
8198
        | TYPE_SYM              {}
unknown's avatar
unknown committed
8199
        | UDF_RETURNS_SYM       {}
8200
	| FUNCTION_SYM		{}
unknown's avatar
unknown committed
8201
	| UNCOMMITTED_SYM	{}
8202
	| UNDEFINED_SYM		{}
8203
	| UNKNOWN_SYM		{}
8204
	| UNTIL_SYM		{}
8205
	| USER			{}
unknown's avatar
unknown committed
8206
	| USE_FRM		{}
unknown's avatar
unknown committed
8207
	| VARIABLES		{}
8208
	| VIEW_SYM		{}
8209
	| VALUE_SYM		{}
8210
	| WARNINGS		{}
8211
	| WEEK_SYM		{}
unknown's avatar
unknown committed
8212
	| WORK_SYM		{}
8213
	| X509_SYM		{}
unknown's avatar
unknown committed
8214 8215
	| YEAR_SYM		{}
	;
unknown's avatar
unknown committed
8216 8217 8218 8219 8220 8221

/* Option functions */

set:
	SET opt_option
	{
unknown's avatar
unknown committed
8222 8223
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
unknown's avatar
unknown committed
8224
	  mysql_init_select(lex);
8225
	  lex->option_type=OPT_SESSION;
unknown's avatar
unknown committed
8226
	  lex->var_list.empty();
8227
          lex->one_shot_set= 0;
unknown's avatar
unknown committed
8228
	}
8229 8230 8231
	option_value_list
	{}
	;
unknown's avatar
unknown committed
8232 8233 8234

opt_option:
	/* empty */ {}
unknown's avatar
unknown committed
8235
	| OPTION {};
unknown's avatar
unknown committed
8236 8237

option_value_list:
8238 8239 8240 8241 8242 8243 8244 8245 8246 8247
	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
8248 8249
              and sp_instr_set_trigger instructions share one LEX.
              (Well, it is theoretically possible but adds some extra
8250 8251 8252 8253 8254 8255 8256 8257
               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
8258

8259 8260 8261 8262 8263 8264 8265 8266 8267
            /* 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;
          }
        }
8268
	ext_option_value
8269 8270
        {
          LEX *lex= Lex;
unknown's avatar
unknown committed
8271

8272 8273 8274
          if (lex->sphead)
          {
            sp_head *sp= lex->sphead;
unknown's avatar
unknown committed
8275

8276 8277 8278 8279 8280 8281 8282 8283 8284
	    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
8285

8286 8287 8288
              if (!(i= new sp_instr_stmt(sp->instructions(), lex->spcont,
                                         lex)))
                YYABORT;
unknown's avatar
unknown committed
8289

8290 8291 8292 8293
              if (lex->ptr - lex->tok_end > 1)
                qbuff.length= lex->ptr - sp->m_tmp_query;
              else
                qbuff.length= lex->tok_end - sp->m_tmp_query;
unknown's avatar
unknown committed
8294

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

8298 8299 8300 8301 8302 8303 8304 8305 8306
              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
8307 8308

option_type:
8309 8310 8311 8312 8313 8314 8315 8316 8317
        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
8318 8319 8320 8321
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
unknown's avatar
unknown committed
8322
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
8323 8324 8325 8326 8327 8328
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

opt_var_ident_type:
	/* empty */		{ $$=OPT_DEFAULT; }
unknown's avatar
unknown committed
8329
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
8330 8331 8332
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
unknown's avatar
unknown committed
8333

8334 8335 8336
ext_option_value:
        sys_option_value
        | option_type2 option_value;
8337

8338 8339 8340 8341
sys_option_value:
        option_type internal_variable_name equal set_expr_or_default
        {
          LEX *lex=Lex;
8342

8343 8344 8345 8346
          if ($2.var == &trg_new_row_fake_var)
          {
            /* We are in trigger and assigning value to field of new row */
            Item *it;
8347
            Item_trigger_field *trg_fld;
8348 8349 8350 8351 8352
            sp_instr_set_trigger_field *i;
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
8353
            }
8354 8355
            if ($4)
              it= $4;
8356
            else
8357 8358 8359 8360
            {
              /* QQ: Shouldn't this be field's default value ? */
              it= new Item_null();
            }
8361

unknown's avatar
unknown committed
8362 8363
            if (!(trg_fld= new Item_trigger_field(&lex->current_select->
                                                  context,
8364
                                                  Item_trigger_field::NEW_ROW,
8365
                                                  $2.base_name.str)) ||
unknown's avatar
unknown committed
8366 8367
                !(i= new sp_instr_set_trigger_field(lex->sphead->
                                                    instructions(),
8368 8369
                                                    lex->spcont, trg_fld,
                                                    it, lex)))
8370
              YYABORT;
8371

8372 8373 8374 8375
            /*
              Let us add this item to list of all Item_trigger_field
              objects in trigger.
            */
8376 8377
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
                                    (byte **)&trg_fld->next_trg_field);
8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431

            lex->sphead->add_instr(i);
          }
          else if ($2.var)
          { /* System variable */
            if ($1)
              lex->option_type= (enum_var_type)$1;
            lex->var_list.push_back(new set_var(lex->option_type, $2.var,
                                    &$2.base_name, $4));
          }
          else
          {
            /* An SP local variable */
            sp_pcontext *ctx= lex->spcont;
            sp_pvar_t *spv;
            sp_instr_set *i;
            Item *it;
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }

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

            if ($4)
              it= $4;
            else if (spv->dflt)
              it= spv->dflt;
            else
              it= new Item_null();
            i= new sp_instr_set(lex->sphead->instructions(), ctx,
                                spv->offset, it, spv->type, lex, TRUE);
            lex->sphead->add_instr(i);
            spv->isset= TRUE;
          }
        }
        | option_type TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	{
	  LEX *lex=Lex;
          if (!$1)
            lex->option_type= (enum_var_type)$1;
	  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
8432
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
8433 8434
	  {
	    LEX *lex=Lex;
unknown's avatar
unknown committed
8435 8436
	    lex->var_list.push_back(new set_var((enum_var_type) $3, $4.var,
						&$4.base_name, $6));
unknown's avatar
unknown committed
8437
	  }
8438
	| charset old_or_new_charset_name_or_default
unknown's avatar
unknown committed
8439
	{
unknown's avatar
unknown committed
8440
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
8441
	  LEX *lex= Lex;
8442
	  $2= $2 ? $2: global_system_variables.character_set_client;
8443
	  lex->var_list.push_back(new set_var_collation_client($2,thd->variables.collation_database,$2));
unknown's avatar
unknown committed
8444
	}
8445
	| NAMES_SYM charset_name_or_default opt_collate
8446
	{
unknown's avatar
unknown committed
8447
	  LEX *lex= Lex;
8448
	  $2= $2 ? $2 : global_system_variables.character_set_client;
8449 8450
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
8451
	  {
8452 8453
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
unknown's avatar
unknown committed
8454
	    YYABORT;
8455
	  }
8456
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
8457
	}
unknown's avatar
unknown committed
8458
	| PASSWORD equal text_or_password
unknown's avatar
unknown committed
8459
	  {
8460
	    THD *thd=YYTHD;
unknown's avatar
unknown committed
8461
	    LEX_USER *user;
unknown's avatar
unknown committed
8462
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
unknown's avatar
unknown committed
8463
	      YYABORT;
8464
	    user->host=null_lex_str;
unknown's avatar
unknown committed
8465
	    user->user.str=thd->priv_user;
8466
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
unknown's avatar
unknown committed
8467 8468
	  }
	| PASSWORD FOR_SYM user equal text_or_password
unknown's avatar
unknown committed
8469
	  {
unknown's avatar
unknown committed
8470 8471
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
8472
	;
unknown's avatar
unknown committed
8473

unknown's avatar
unknown committed
8474 8475 8476
internal_variable_name:
	ident
	{
8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488
	  LEX *lex= Lex;
          sp_pcontext *spc= lex->spcont;
	  sp_pvar_t *spv;

	  /* We have to lookup here since local vars can shadow sysvars */
	  if (!spc || !(spv = spc->find_pvar(&$1)))
	  {
            /* Not an SP local variable */
	    sys_var *tmp=find_sys_var($1.str, $1.length);
	    if (!tmp)
	      YYABORT;
	    $$.var= tmp;
8489
	    $$.base_name= null_lex_str;
8490 8491 8492 8493
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
8494 8495 8496
            if (tmp == &sys_time_zone &&
                lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
8497 8498 8499 8500 8501 8502 8503
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
unknown's avatar
unknown committed
8504
	}
8505 8506
	| ident '.' ident
	  {
8507
            LEX *lex= Lex;
8508 8509
            if (check_reserved_words(&$1))
            {
8510
	      yyerror(ER(ER_SYNTAX_ERROR));
8511 8512
              YYABORT;
            }
8513 8514 8515 8516 8517 8518
            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
8519
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
8520 8521 8522 8523
                YYABORT;
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
unknown's avatar
unknown committed
8524 8525
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
8526 8527 8528 8529
                YYABORT;
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
unknown's avatar
unknown committed
8530
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542
                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())
8543
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
8544 8545 8546
              $$.var= tmp;
              $$.base_name= $1;
            }
8547 8548 8549 8550 8551 8552 8553
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
8554
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
unknown's avatar
unknown committed
8555 8556 8557
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
8558
	  }
8559
        ;
unknown's avatar
unknown committed
8560 8561 8562 8563 8564 8565 8566

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

unknown's avatar
unknown committed
8568 8569 8570 8571
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
8572
	    $$= $3.length ? YYTHD->variables.old_passwords ?
unknown's avatar
unknown committed
8573 8574 8575 8576 8577 8578 8579 8580
	        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;
8581 8582
	  }
          ;
unknown's avatar
unknown committed
8583

unknown's avatar
unknown committed
8584

unknown's avatar
unknown committed
8585
set_expr_or_default:
unknown's avatar
unknown committed
8586 8587
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
8588 8589
	| ON	  { $$=new Item_string("ON",  2, system_charset_info); }
	| ALL	  { $$=new Item_string("ALL", 3, system_charset_info); }
unknown's avatar
unknown committed
8590
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
unknown's avatar
unknown committed
8591
	;
unknown's avatar
unknown committed
8592 8593


unknown's avatar
unknown committed
8594 8595 8596 8597 8598
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
8599 8600 8601 8602
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
8603
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOCK");
8604 8605 8606
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_LOCK_TABLES;
unknown's avatar
unknown committed
8607
	}
8608 8609
	table_lock_list
	{}
8610
	;
unknown's avatar
unknown committed
8611 8612 8613

table_or_tables:
	TABLE_SYM
unknown's avatar
unknown committed
8614
	| TABLES;
unknown's avatar
unknown committed
8615 8616 8617

table_lock_list:
	table_lock
unknown's avatar
unknown committed
8618
	| table_lock_list ',' table_lock;
unknown's avatar
unknown committed
8619 8620 8621

table_lock:
	table_ident opt_table_alias lock_option
8622
	{
unknown's avatar
unknown committed
8623
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
8624 8625
	   YYABORT;
	}
8626
        ;
unknown's avatar
unknown committed
8627 8628 8629

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
8630
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
unknown's avatar
unknown committed
8631
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
8632 8633
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
unknown's avatar
unknown committed
8634 8635

unlock:
8636 8637 8638 8639 8640 8641
	UNLOCK_SYM
	{
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
8642
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "UNLOCK");
8643 8644 8645 8646 8647 8648
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_UNLOCK_TABLES;
	}
	table_or_tables
	{}
8649
        ;
unknown's avatar
unknown committed
8650 8651


8652
/*
unknown's avatar
unknown committed
8653
** Handler: direct access to ISAM functions
8654 8655 8656 8657 8658
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
8659 8660
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_OPEN;
unknown's avatar
unknown committed
8661
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
8662 8663
	    YYABORT;
	}
unknown's avatar
unknown committed
8664
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
8665
	{
8666 8667
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_CLOSE;
unknown's avatar
unknown committed
8668
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
8669 8670
	    YYABORT;
	}
unknown's avatar
unknown committed
8671
	| HANDLER_SYM table_ident_nodb READ_SYM
8672
	{
unknown's avatar
unknown committed
8673 8674 8675
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
8676
	  lex->current_select->select_limit= new Item_int((int32) 1);
8677
	  lex->current_select->offset_limit= 0;
unknown's avatar
unknown committed
8678
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
8679
	    YYABORT;
unknown's avatar
unknown committed
8680
        }
8681
        handler_read_or_scan where_clause opt_limit_clause {}
8682
        ;
8683

unknown's avatar
unknown committed
8684
handler_read_or_scan:
8685 8686
	handler_scan_function         { Lex->ident= null_lex_str; }
        | ident handler_rkey_function { Lex->ident= $1; }
8687
        ;
unknown's avatar
unknown committed
8688 8689 8690

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
8691 8692
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
unknown's avatar
unknown committed
8693 8694 8695 8696 8697 8698

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;  }
8699 8700
	| handler_rkey_mode
	{
unknown's avatar
unknown committed
8701 8702
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
unknown's avatar
unknown committed
8703
	  lex->ha_rkey_mode=$1;
unknown's avatar
unknown committed
8704
	  if (!(lex->insert_list = new List_item))
8705
	    YYABORT;
8706 8707
	} '(' values ')' { }
        ;
8708 8709

handler_rkey_mode:
unknown's avatar
unknown committed
8710 8711 8712 8713
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
8714 8715
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
8716

unknown's avatar
unknown committed
8717 8718 8719
/* GRANT / REVOKE */

revoke:
8720
	REVOKE clear_privileges revoke_command
8721 8722 8723 8724
	{}
        ;

revoke_command:
8725
	grant_privileges ON opt_table grant_ident FROM grant_list
8726
	{
8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754
          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;
8755
        }
8756
	|
8757
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
8758 8759 8760
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
8761
	;
unknown's avatar
unknown committed
8762 8763

grant:
8764 8765 8766 8767 8768 8769
	GRANT clear_privileges grant_command
	{}
        ;

grant_command:
	grant_privileges ON opt_table grant_ident TO_SYM grant_list
8770
	require_clause grant_options
8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802
	{
          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
8803

8804 8805 8806 8807
opt_table:
	/* Empty */
	| TABLE_SYM ;
        
unknown's avatar
unknown committed
8808
grant_privileges:
8809 8810 8811 8812 8813 8814
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
8815
        ;
unknown's avatar
unknown committed
8816

unknown's avatar
unknown committed
8817 8818 8819 8820 8821
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

8822 8823 8824
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
unknown's avatar
unknown committed
8825

8826
object_privilege:
unknown's avatar
unknown committed
8827
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
8828 8829 8830
	| 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
8831 8832
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
unknown's avatar
unknown committed
8833
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
unknown's avatar
unknown committed
8834 8835 8836
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
unknown's avatar
unknown committed
8837
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
unknown's avatar
unknown committed
8838 8839 8840 8841
	| 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
8842 8843 8844 8845 8846
	| 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; }
8847 8848 8849 8850
	| 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; }
8851 8852
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
8853
	| CREATE USER { Lex->grant |= CREATE_USER_ACL; }
unknown's avatar
unknown committed
8854
	;
unknown's avatar
unknown committed
8855

unknown's avatar
unknown committed
8856

8857 8858
opt_and:
	/* empty */	{}
unknown's avatar
unknown committed
8859
	| AND_SYM	{}
8860 8861 8862 8863 8864 8865 8866 8867 8868
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
unknown's avatar
unknown committed
8869 8870 8871 8872
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
unknown's avatar
unknown committed
8873
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
unknown's avatar
unknown committed
8874 8875 8876 8877 8878 8879 8880 8881 8882
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
unknown's avatar
unknown committed
8883
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
unknown's avatar
unknown committed
8884 8885 8886 8887 8888 8889 8890 8891 8892
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
unknown's avatar
unknown committed
8893
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
unknown's avatar
unknown committed
8894 8895 8896 8897 8898
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
8899

8900
grant_ident:
unknown's avatar
unknown committed
8901 8902
	'*'
	  {
8903
	    LEX *lex= Lex;
unknown's avatar
unknown committed
8904
	    lex->current_select->db= lex->thd->db;
unknown's avatar
unknown committed
8905
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
8906 8907
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
8908
	    {
unknown's avatar
unknown committed
8909 8910
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
8911
	      YYABORT;
unknown's avatar
unknown committed
8912
	    }
unknown's avatar
unknown committed
8913 8914 8915
	  }
	| ident '.' '*'
	  {
8916
	    LEX *lex= Lex;
unknown's avatar
unknown committed
8917
	    lex->current_select->db = $1.str;
unknown's avatar
unknown committed
8918
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
8919 8920
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
8921
	    {
unknown's avatar
unknown committed
8922 8923
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
8924 8925 8926 8927 8928
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
8929
	    LEX *lex= Lex;
unknown's avatar
unknown committed
8930
	    lex->current_select->db = NULL;
unknown's avatar
unknown committed
8931 8932
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
unknown's avatar
unknown committed
8933
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
8934
	    {
unknown's avatar
unknown committed
8935 8936
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
8937 8938 8939 8940 8941
	      YYABORT;
	    }
	  }
	| table_ident
	  {
unknown's avatar
unknown committed
8942
	    LEX *lex=Lex;
unknown's avatar
unknown committed
8943
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
unknown's avatar
unknown committed
8944
	      YYABORT;
unknown's avatar
unknown committed
8945
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
8946
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
8947 8948
	  }
          ;
unknown's avatar
unknown committed
8949 8950 8951


user_list:
8952 8953 8954 8955 8956 8957 8958 8959 8960 8961
	user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;


grant_list:
8962
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
8963
	| grant_list ',' grant_user
8964 8965 8966 8967 8968
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
unknown's avatar
unknown committed
8969 8970 8971 8972 8973 8974 8975 8976


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
8977
             if (YYTHD->variables.old_passwords)
8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994
             {
               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
8995 8996 8997
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
8998
	  { $$= $1; $1->password= $5; }
unknown's avatar
unknown committed
8999
	| user
9000
	  { $$= $1; $1->password= null_lex_str; }
9001
        ;
unknown's avatar
unknown committed
9002 9003 9004


opt_column_list:
unknown's avatar
unknown committed
9005 9006 9007 9008 9009
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
unknown's avatar
unknown committed
9010
	| '(' column_list ')';
unknown's avatar
unknown committed
9011 9012 9013

column_list:
	column_list ',' column_list_id
unknown's avatar
unknown committed
9014
	| column_list_id;
unknown's avatar
unknown committed
9015 9016 9017 9018

column_list_id:
	ident
	{
unknown's avatar
unknown committed
9019
	  String *new_str = new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info);
unknown's avatar
unknown committed
9020 9021
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
unknown's avatar
unknown committed
9022
	  LEX *lex=Lex;
unknown's avatar
unknown committed
9023 9024
	  while ((point=iter++))
	  {
9025 9026
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
unknown's avatar
unknown committed
9027 9028
		break;
	  }
unknown's avatar
unknown committed
9029
	  lex->grant_tot_col|= lex->which_columns;
unknown's avatar
unknown committed
9030
	  if (point)
unknown's avatar
unknown committed
9031
	    point->rights |= lex->which_columns;
unknown's avatar
unknown committed
9032
	  else
unknown's avatar
unknown committed
9033
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
9034 9035
	}
        ;
unknown's avatar
unknown committed
9036

unknown's avatar
unknown committed
9037 9038

require_clause: /* empty */
9039
        | REQUIRE_SYM require_list
9040 9041 9042
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
9043
        | REQUIRE_SYM SSL_SYM
9044 9045 9046
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
9047
        | REQUIRE_SYM X509_SYM
9048 9049 9050 9051 9052 9053 9054
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
9055
          ;
unknown's avatar
unknown committed
9056

unknown's avatar
unknown committed
9057
grant_options:
unknown's avatar
unknown committed
9058
	/* empty */ {}
unknown's avatar
unknown committed
9059
	| WITH grant_option_list;
unknown's avatar
unknown committed
9060

unknown's avatar
unknown committed
9061 9062
grant_option_list:
	grant_option_list grant_option {}
9063 9064
	| grant_option {}
        ;
unknown's avatar
unknown committed
9065 9066 9067

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
unknown's avatar
unknown committed
9068
        | MAX_QUERIES_PER_HOUR ulong_num
unknown's avatar
unknown committed
9069
        {
unknown's avatar
unknown committed
9070 9071 9072
	  LEX *lex=Lex;
	  lex->mqh.questions=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
9073
	}
unknown's avatar
unknown committed
9074
        | MAX_UPDATES_PER_HOUR ulong_num
9075
        {
unknown's avatar
unknown committed
9076 9077 9078
	  LEX *lex=Lex;
	  lex->mqh.updates=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
9079
	}
unknown's avatar
unknown committed
9080
        | MAX_CONNECTIONS_PER_HOUR ulong_num
9081
        {
unknown's avatar
unknown committed
9082 9083 9084
	  LEX *lex=Lex;
	  lex->mqh.conn_per_hour= $2;
	  lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
9085
	}
unknown's avatar
unknown committed
9086
        | MAX_USER_CONNECTIONS_SYM ulong_num
9087
        {
unknown's avatar
unknown committed
9088 9089 9090
	  LEX *lex=Lex;
          lex->mqh.user_conn= $2;
          lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
9091 9092
	}
        ;
9093

unknown's avatar
unknown committed
9094
begin:
unknown's avatar
unknown committed
9095 9096 9097
	BEGIN_SYM  
        {
	  LEX *lex=Lex;
9098 9099 9100 9101 9102
          if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
          {
            my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
            YYABORT;
          }
unknown's avatar
unknown committed
9103 9104 9105 9106
          lex->sql_command = SQLCOM_BEGIN;
          lex->start_transaction_opt= 0;
        }
        opt_work {}
9107
	;
unknown's avatar
unknown committed
9108 9109 9110

opt_work:
	/* empty */ {}
unknown's avatar
unknown committed
9111
	| WORK_SYM  {}
9112
        ;
unknown's avatar
unknown committed
9113

unknown's avatar
unknown committed
9114
opt_chain:
unknown's avatar
unknown committed
9115
	/* empty */ { $$= (YYTHD->variables.completion_type == 1); }
unknown's avatar
unknown committed
9116 9117 9118 9119 9120
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
unknown's avatar
unknown committed
9121
	/* empty */ { $$= (YYTHD->variables.completion_type == 2); }
unknown's avatar
unknown committed
9122 9123 9124 9125 9126 9127 9128 9129 9130
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

unknown's avatar
unknown committed
9131
commit:
unknown's avatar
unknown committed
9132
	COMMIT_SYM opt_work opt_chain opt_release
unknown's avatar
unknown committed
9133
	{
unknown's avatar
unknown committed
9134
	  LEX *lex=Lex;
9135 9136 9137 9138 9139
          if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
          {
            my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
            YYABORT;
          }
unknown's avatar
unknown committed
9140 9141 9142
	  lex->sql_command= SQLCOM_COMMIT;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
unknown's avatar
unknown committed
9143 9144
	}
	;
unknown's avatar
unknown committed
9145 9146

rollback:
unknown's avatar
unknown committed
9147
	ROLLBACK_SYM opt_work opt_chain opt_release
9148
	{
unknown's avatar
unknown committed
9149
	  LEX *lex=Lex;
9150 9151 9152 9153 9154
          if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
          {
            my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
            YYABORT;
          }
unknown's avatar
unknown committed
9155 9156 9157
	  lex->sql_command= SQLCOM_ROLLBACK;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
9158
	}
unknown's avatar
unknown committed
9159 9160
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
9161
	{
unknown's avatar
unknown committed
9162 9163 9164
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK_TO_SAVEPOINT;
	  lex->ident= $5;
unknown's avatar
unknown committed
9165 9166 9167
	}
	;

9168 9169 9170
savepoint:
	SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
9171 9172 9173
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SAVEPOINT;
	  lex->ident= $2;
unknown's avatar
unknown committed
9174 9175 9176 9177 9178 9179
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
9180 9181 9182
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RELEASE_SAVEPOINT;
	  lex->ident= $3;
unknown's avatar
unknown committed
9183 9184
	}
	;
unknown's avatar
unknown committed
9185
  
9186
/*
unknown's avatar
unknown committed
9187
   UNIONS : glue selects together
9188 9189 9190
*/


9191
union_clause:
9192
	/* empty */ {}
unknown's avatar
unknown committed
9193 9194
	| union_list
	;
9195 9196

union_list:
9197
	UNION_SYM union_option
9198 9199 9200 9201 9202
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
unknown's avatar
unknown committed
9203
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
9204
	    YYABORT;
9205
	  }
9206
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
9207
	  {
9208
            yyerror(ER(ER_SYNTAX_ERROR));
9209 9210
	    YYABORT;
	  }
9211
	  if (mysql_new_select(lex, 0))
9212
	    YYABORT;
unknown's avatar
unknown committed
9213
          mysql_init_select(lex);
9214
	  lex->current_select->linkage=UNION_TYPE;
9215 9216 9217
          if ($2) /* UNION DISTINCT - remember position */
            lex->current_select->master_unit()->union_distinct=
                                                      lex->current_select;
unknown's avatar
unknown committed
9218
	}
9219
	select_init {}
9220
	;
9221 9222

union_opt:
unknown's avatar
unknown committed
9223 9224 9225
	/* Empty */ { $$= 0; }
	| union_list { $$= 1; }
	| union_order_or_limit { $$= 1; }
unknown's avatar
unknown committed
9226
	;
9227

unknown's avatar
unknown committed
9228
union_order_or_limit:
9229
	  {
unknown's avatar
unknown committed
9230
	    THD *thd= YYTHD;
9231
	    LEX *lex= thd->lex;
unknown's avatar
unknown committed
9232
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
unknown's avatar
unknown committed
9233
	    SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
9234
	    SELECT_LEX_UNIT *unit= sel->master_unit();
unknown's avatar
unknown committed
9235
	    SELECT_LEX *fake= unit->fake_select_lex;
unknown's avatar
unknown committed
9236 9237 9238 9239 9240 9241
	    if (fake)
	    {
	      unit->global_parameters= fake;
	      fake->no_table_names_allowed= 1;
	      lex->current_select= fake;
	    }
unknown's avatar
unknown committed
9242
	    thd->where= "global ORDER clause";
9243
	  }
9244
	order_or_limit
unknown's avatar
unknown committed
9245 9246
          {
	    THD *thd= YYTHD;
9247
	    thd->lex->current_select->no_table_names_allowed= 0;
unknown's avatar
unknown committed
9248 9249
	    thd->where= "";
          }
9250 9251 9252
	;

order_or_limit:
9253 9254
	order_clause opt_limit_clause_init
	| limit_clause
9255
	;
unknown's avatar
unknown committed
9256 9257

union_option:
9258 9259 9260 9261
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
9262

unknown's avatar
unknown committed
9263 9264
singlerow_subselect:
	subselect_start singlerow_subselect_init
unknown's avatar
unknown committed
9265 9266 9267 9268
	subselect_end
	{
	  $$= $2;
	};
9269

unknown's avatar
unknown committed
9270
singlerow_subselect_init:
9271
	select_init2
unknown's avatar
unknown committed
9272
	{
9273 9274
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
unknown's avatar
unknown committed
9275
	};
unknown's avatar
unknown committed
9276 9277

exists_subselect:
unknown's avatar
unknown committed
9278 9279 9280 9281 9282
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
unknown's avatar
unknown committed
9283 9284

exists_subselect_init:
9285
	select_init2
unknown's avatar
unknown committed
9286
	{
9287 9288
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
unknown's avatar
unknown committed
9289
	};
9290

unknown's avatar
unknown committed
9291 9292 9293 9294 9295 9296 9297 9298
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
9299
  select_init2
unknown's avatar
unknown committed
9300
  {
unknown's avatar
unknown committed
9301
    $$= Lex->current_select->master_unit()->first_select();
unknown's avatar
unknown committed
9302 9303
  };

9304
subselect_start:
9305
	'(' SELECT_SYM
unknown's avatar
unknown committed
9306
	{
9307
	  LEX *lex=Lex;
9308
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
9309 9310
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
9311 9312
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
9313 9314
	    YYABORT;
	  }
unknown's avatar
unknown committed
9315 9316 9317
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
9318 9319

subselect_end:
unknown's avatar
unknown committed
9320 9321 9322
	')'
	{
	  LEX *lex=Lex;
9323
	  lex->current_select = lex->current_select->return_after_parsing();
unknown's avatar
unknown committed
9324
	};
unknown's avatar
unknown committed
9325

9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369
opt_view_list:
	/* empty */ {}
	| '(' view_list ')'
	;

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

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

algorithm:
	/* empty */
	  { Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED; }
	| ALGORITHM_SYM EQ UNDEFINED_SYM
	  { Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED; }
	| ALGORITHM_SYM EQ MERGE_SYM
	  { Lex->create_view_algorithm= VIEW_ALGORITHM_MERGE; }
	| ALGORITHM_SYM EQ TEMPTABLE_SYM
	  { Lex->create_view_algorithm= VIEW_ALGORITHM_TMPTABLE; }
	;
check_option:
        /* empty */
          { Lex->create_view_check= VIEW_CHECK_NONE; }
        | WITH CHECK_SYM OPTION
          { Lex->create_view_check= VIEW_CHECK_CASCADED; }
        | WITH CASCADED CHECK_SYM OPTION
          { Lex->create_view_check= VIEW_CHECK_CASCADED; }
        | WITH LOCAL_SYM CHECK_SYM OPTION
          { Lex->create_view_check= VIEW_CHECK_LOCAL; }
        ;

9370 9371
xa: XA_SYM begin_or_start xid opt_join_or_resume
      {
unknown's avatar
unknown committed
9372
        Lex->sql_command = SQLCOM_XA_START;
9373 9374 9375
      }
    | XA_SYM END xid opt_suspend_or_migrate
      {
unknown's avatar
unknown committed
9376
        Lex->sql_command = SQLCOM_XA_END;
9377 9378 9379
      }
    | XA_SYM PREPARE_SYM xid
      {
unknown's avatar
unknown committed
9380
        Lex->sql_command = SQLCOM_XA_PREPARE;
9381 9382 9383
      }
    | XA_SYM COMMIT_SYM xid opt_one_phase
      {
unknown's avatar
unknown committed
9384
        Lex->sql_command = SQLCOM_XA_COMMIT;
9385 9386 9387
      }
    | XA_SYM ROLLBACK_SYM xid
      {
unknown's avatar
unknown committed
9388
        Lex->sql_command = SQLCOM_XA_ROLLBACK;
9389 9390 9391
      }
    | XA_SYM RECOVER_SYM
      {
unknown's avatar
unknown committed
9392
        Lex->sql_command = SQLCOM_XA_RECOVER;
9393 9394 9395
      }
    ;

unknown's avatar
unknown committed
9396 9397
xid: text_string
     {
unknown's avatar
unknown committed
9398
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE);
unknown's avatar
unknown committed
9399 9400 9401 9402 9403 9404
       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
9405
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
unknown's avatar
unknown committed
9406 9407 9408 9409 9410 9411
       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
9412
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
unknown's avatar
unknown committed
9413 9414 9415 9416 9417
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set($5, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     ;
9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440

begin_or_start:   BEGIN_SYM {}
    |             START_SYM {}
    ;

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

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

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