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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

%pure_parser					/* We have threads */

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

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
%token  ABORT_SYM
%token  ACTION
%token  ADD
%token  ADDDATE_SYM
%token  AFTER_SYM
%token  AGAINST
%token  AGGREGATE_SYM
%token  ALGORITHM_SYM
%token  ALL
%token  ALTER
%token  ANALYZE_SYM
%token  AND_AND_SYM
%token  AND_SYM
%token  ANY_SYM
%token  AS
%token  ASC
%token  ASCII_SYM
%token  ASENSITIVE_SYM
%token  ATAN
%token  AUTO_INC
%token  AVG_ROW_LENGTH
%token  AVG_SYM
%token  BACKUP_SYM
%token  BEFORE_SYM
%token  BEGIN_SYM
%token  BENCHMARK_SYM
%token  BERKELEY_DB_SYM
%token  BIGINT
%token  BINARY
%token  BINLOG_SYM
unknown's avatar
unknown committed
150
%token  BIN_NUM
151 152 153 154 155 156 157 158 159 160 161 162 163 164
%token  BIT_AND
%token  BIT_OR
%token  BIT_SYM
%token  BIT_XOR
%token  BLOB_SYM
%token  BOOLEAN_SYM
%token  BOOL_SYM
%token  BOTH
%token  BTREE_SYM
%token  BY
%token  BYTE_SYM
%token  CACHE_SYM
%token  CALL_SYM
%token  CASCADE
165
%token  CASCADED
166
%token  CAST_SYM
unknown's avatar
unknown committed
167
%token  CHAIN_SYM
168 169 170 171 172 173 174 175 176 177
%token  CHANGE
%token  CHANGED
%token  CHARSET
%token  CHAR_SYM
%token  CHECKSUM_SYM
%token  CHECK_SYM
%token  CIPHER_SYM
%token  CLIENT_SYM
%token  CLOSE_SYM
%token  COALESCE
unknown's avatar
unknown committed
178
%token  CODE_SYM
179 180 181 182 183 184 185
%token  COLLATE_SYM
%token  COLLATION_SYM
%token  COLUMNS
%token  COLUMN_SYM
%token  COMMENT_SYM
%token  COMMITTED_SYM
%token  COMMIT_SYM
unknown's avatar
unknown committed
186
%token  COMPACT_SYM
187 188 189 190
%token  COMPRESSED_SYM
%token  CONCAT
%token  CONCAT_WS
%token  CONCURRENT
191
%token  CONDITION_SYM
192 193 194
%token  CONNECTION_SYM
%token  CONSISTENT_SYM
%token  CONSTRAINT
195 196
%token  CONTAINS_SYM
%token  CONTINUE_SYM
197 198 199 200 201 202 203
%token  CONVERT_SYM
%token  CONVERT_TZ_SYM
%token  COUNT_SYM
%token  CREATE
%token  CROSS
%token  CUBE_SYM
%token  CURDATE
204
%token  CURRENT_USER
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
%token  CURSOR_SYM
%token  CURTIME
%token  DATABASE
%token  DATABASES
%token  DATA_SYM
%token  DATETIME
%token  DATE_ADD_INTERVAL
%token  DATE_SUB_INTERVAL
%token  DATE_SYM
%token  DAY_HOUR_SYM
%token  DAY_MICROSECOND_SYM
%token  DAY_MINUTE_SYM
%token  DAY_SECOND_SYM
%token  DAY_SYM
%token  DEALLOCATE_SYM
unknown's avatar
unknown committed
220
%token  DECIMAL_NUM
221
%token  DECIMAL_SYM
222
%token  DECLARE_SYM
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
%token  DECODE_SYM
%token  DEFAULT
%token  DEFINER_SYM
%token  DELAYED_SYM
%token  DELAY_KEY_WRITE_SYM
%token  DELETE_SYM
%token  DESC
%token  DESCRIBE
%token  DES_DECRYPT_SYM
%token  DES_ENCRYPT_SYM
%token  DES_KEY_FILE
%token  DETERMINISTIC_SYM
%token  DIRECTORY_SYM
%token  DISABLE_SYM
%token  DISCARD
%token  DISTINCT
%token  DIV_SYM
%token  DOUBLE_SYM
%token  DO_SYM
%token  DROP
%token  DUAL_SYM
%token  DUMPFILE
unknown's avatar
unknown committed
245
%token  DUPLICATE_SYM
246
%token  DYNAMIC_SYM
247
%token  EACH_SYM
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
%token  ELSEIF_SYM
%token  ELT_FUNC
%token  ENABLE_SYM
%token  ENCLOSED
%token  ENCODE_SYM
%token  ENCRYPT
%token  END
%token  ENGINES_SYM
%token  ENGINE_SYM
%token  ENUM
%token  EQ
%token  EQUAL_SYM
%token  ERRORS
%token  ESCAPED
%token  ESCAPE_SYM
%token  EVENTS_SYM
%token  EXECUTE_SYM
%token  EXISTS
266
%token  EXIT_SYM
267 268 269 270 271 272
%token  EXPANSION_SYM
%token  EXPORT_SET
%token  EXTENDED_SYM
%token  EXTRACT_SYM
%token  FALSE_SYM
%token  FAST_SYM
273
%token  FETCH_SYM
274 275 276 277 278 279 280 281 282 283 284
%token  FIELD_FUNC
%token  FILE_SYM
%token  FIRST_SYM
%token  FIXED_SYM
%token  FLOAT_NUM
%token  FLOAT_SYM
%token  FLUSH_SYM
%token  FORCE_SYM
%token  FOREIGN
%token  FORMAT_SYM
%token  FOR_SYM
285
%token  FOUND_SYM
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
%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
334
%token  INOUT_SYM
335 336 337 338 339 340
%token  INSENSITIVE_SYM
%token  INSERT
%token  INSERT_METHOD
%token  INTERVAL_SYM
%token  INTO
%token  INT_SYM
341
%token  INVOKER_SYM
342 343 344 345 346 347 348 349 350 351
%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
352
%token  LANGUAGE_SYM
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
%token  LAST_INSERT_ID
%token  LAST_SYM
%token  LE
%token  LEADING
%token  LEAST_SYM
%token  LEAVES
%token  LEAVE_SYM
%token  LEFT
%token  LEVEL_SYM
%token  LEX_HOSTNAME
%token  LIKE
%token  LIMIT
%token  LINEFROMTEXT
%token  LINES
%token  LINESTRING
%token  LOAD
%token  LOCAL_SYM
%token  LOCATE
371
%token  LOCATOR_SYM
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
%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
406
%token  MAX_USER_CONNECTIONS_SYM
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
%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
430
%token  MUTEX_SYM
431 432 433 434 435 436
%token  NAMES_SYM
%token  NAME_SYM
%token  NATIONAL_SYM
%token  NATURAL
%token  NCHAR_STRING
%token  NCHAR_SYM
unknown's avatar
unknown committed
437
%token  NDBCLUSTER_SYM
438 439 440 441 442 443 444 445 446 447 448 449
%token  NE
%token  NEW_SYM
%token  NEXT_SYM
%token  NONE_SYM
%token  NOT2_SYM
%token  NOT_SYM
%token  NOW_SYM
%token  NO_SYM
%token  NO_WRITE_TO_BINLOG
%token  NULL_SYM
%token  NUM
%token  NUMERIC_SYM
unknown's avatar
unknown committed
450
%token  NVARCHAR_SYM
451 452 453
%token  OFFSET_SYM
%token  OLD_PASSWORD
%token  ON
454
%token  ONE_SHOT_SYM
455 456 457 458 459 460 461 462 463 464 465
%token  ONE_SYM
%token  OPEN_SYM
%token  OPTIMIZE
%token  OPTION
%token  OPTIONALLY
%token  OR2_SYM
%token  ORDER_SYM
%token  OR_OR_SYM
%token  OR_SYM
%token  OUTER
%token  OUTFILE
466
%token  OUT_SYM
467 468 469
%token  PACK_KEYS_SYM
%token  PARTIAL
%token  PASSWORD
470
%token  PARAM_MARKER
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
%token  PHASE_SYM
%token  POINTFROMTEXT
%token  POINT_SYM
%token  POLYFROMTEXT
%token  POLYGON
%token  POSITION_SYM
%token  PRECISION
%token  PREPARE_SYM
%token  PREV_SYM
%token  PRIMARY_SYM
%token  PRIVILEGES
%token  PROCEDURE
%token  PROCESS
%token  PROCESSLIST_SYM
%token  PURGE
%token  QUARTER_SYM
%token  QUERY_SYM
%token  QUICK
%token  RAID_0_SYM
%token  RAID_CHUNKS
%token  RAID_CHUNKSIZE
%token  RAID_STRIPED_SYM
%token  RAID_TYPE
%token  RAND
%token  READS_SYM
%token  READ_SYM
%token  REAL
%token  RECOVER_SYM
unknown's avatar
unknown committed
499
%token  REDUNDANT_SYM
500 501 502 503 504
%token  REFERENCES
%token  REGEXP
%token  RELAY_LOG_FILE_SYM
%token  RELAY_LOG_POS_SYM
%token  RELAY_THREAD
unknown's avatar
unknown committed
505
%token  RELEASE_SYM
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
%token  RELOAD
%token  RENAME
%token  REPAIR
%token  REPEATABLE_SYM
%token  REPEAT_SYM
%token  REPLACE
%token  REPLICATION
%token  REQUIRE_SYM
%token  RESET_SYM
%token  RESOURCES
%token  RESTORE_SYM
%token  RESTRICT
%token  RESUME_SYM
%token  RETURNS_SYM
%token  RETURN_SYM
%token  REVOKE
%token  RIGHT
%token  ROLLBACK_SYM
%token  ROLLUP_SYM
%token  ROUND
unknown's avatar
unknown committed
526
%token  ROUTINE_SYM
527 528 529 530 531 532 533 534
%token  ROWS_SYM
%token  ROW_COUNT_SYM
%token  ROW_FORMAT_SYM
%token  ROW_SYM
%token  RTREE_SYM
%token  SAVEPOINT_SYM
%token  SECOND_MICROSECOND_SYM
%token  SECOND_SYM
535
%token  SECURITY_SYM
536 537
%token  SELECT_SYM
%token  SENSITIVE_SYM
538
%token  SEPARATOR_SYM
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
%token  SERIALIZABLE_SYM
%token  SERIAL_SYM
%token  SESSION_SYM
%token  SET
%token  SET_VAR
%token  SHARE_SYM
%token  SHIFT_LEFT
%token  SHIFT_RIGHT
%token  SHOW
%token  SHUTDOWN
%token  SIGNED_SYM
%token  SIMPLE_SYM
%token  SLAVE
%token  SMALLINT
%token  SNAPSHOT_SYM
%token  SOUNDS_SYM
%token  SPATIAL_SYM
556 557 558 559
%token  SPECIFIC_SYM
%token  SQLEXCEPTION_SYM
%token  SQLSTATE_SYM
%token  SQLWARNING_SYM
560 561 562 563 564 565 566 567
%token  SQL_BIG_RESULT
%token  SQL_BUFFER_RESULT
%token  SQL_CACHE_SYM
%token  SQL_CALC_FOUND_ROWS
%token  SQL_NO_CACHE_SYM
%token  SQL_SMALL_RESULT
%token  SQL_SYM
%token  SQL_THREAD
568
%token  SSL_SYM
569 570 571 572
%token  STARTING
%token  START_SYM
%token  STATUS_SYM
%token  STD_SYM
573
%token  STDDEV_SAMP_SYM
574 575 576 577 578 579 580 581 582 583 584
%token  STOP_SYM
%token  STORAGE_SYM
%token  STRAIGHT_JOIN
%token  STRING_SYM
%token  SUBDATE_SYM
%token  SUBJECT_SYM
%token  SUBSTRING
%token  SUBSTRING_INDEX
%token  SUM_SYM
%token  SUPER_SYM
%token  SUSPEND_SYM
585
%token  SYSDATE
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
%token  TABLES
%token  TABLESPACE
%token  TABLE_SYM
%token  TEMPORARY
%token  TEMPTABLE_SYM
%token  TERMINATED
%token  TEXT_STRING
%token  TEXT_SYM
%token  TIMESTAMP
%token  TIMESTAMP_ADD
%token  TIMESTAMP_DIFF
%token  TIME_SYM
%token  TINYBLOB
%token  TINYINT
%token  TINYTEXT
%token  TO_SYM
%token  TRAILING
%token  TRANSACTION_SYM
604
%token  TRIGGER_SYM
605
%token  TRIGGERS_SYM
606 607 608
%token  TRIM
%token  TRUE_SYM
%token  TRUNCATE_SYM
unknown's avatar
unknown committed
609
%token  TYPES_SYM
610 611 612 613 614 615 616
%token  TYPE_SYM
%token  UDF_RETURNS_SYM
%token  UDF_SONAME_SYM
%token  ULONGLONG_NUM
%token  UNCOMMITTED_SYM
%token  UNDEFINED_SYM
%token  UNDERSCORE_CHARSET
617
%token  UNDO_SYM
618 619 620 621 622 623 624 625 626 627
%token  UNICODE_SYM
%token  UNION_SYM
%token  UNIQUE_SYM
%token  UNIQUE_USERS
%token  UNIX_TIMESTAMP
%token  UNKNOWN_SYM
%token  UNLOCK_SYM
%token  UNLOCK_SYM
%token  UNSIGNED
%token  UNTIL_SYM
628
%token  UNTIL_SYM
629 630 631 632 633 634 635 636 637 638
%token  UPDATE_SYM
%token  UPDATE_SYM
%token  USAGE
%token  USER
%token  USE_FRM
%token  USE_SYM
%token  USING
%token  UTC_DATE_SYM
%token  UTC_TIMESTAMP_SYM
%token  UTC_TIME_SYM
639
%token  VAR_SAMP_SYM
640 641 642 643 644 645 646 647 648 649 650 651
%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
652
%token  WHILE_SYM
653 654 655 656 657 658 659 660 661 662
%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
663

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

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

%type <lex_str_ptr>
	opt_table_alias

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

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

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

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

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

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

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

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

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

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

741 742 743
%type <var_type>
        option_type opt_var_type opt_var_ident_type

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

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

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

%type <key_part>
	key_part

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

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

764 765
%type <interval_time_st> interval_time_st

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

%type <row_type> row_types

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

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

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

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

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

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

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

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

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

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

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

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


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

verb_clause:
871 872 873 874 875 876
	  statement
	| begin
	;

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

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

940 941 942 943 944 945
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


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

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

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

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

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

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

unknown's avatar
unknown committed
1012 1013
/* help */

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

/* change master */

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

master_defs:
       master_def
unknown's avatar
unknown committed
1045
       | master_defs ',' master_def;
unknown's avatar
unknown committed
1046

1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
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
1063
       MASTER_PORT_SYM EQ ulong_num
1064 1065 1066 1067
       {
	 Lex->mi.port = $3;
       }
       |
unknown's avatar
unknown committed
1068
       MASTER_CONNECT_RETRY_SYM EQ ulong_num
1069 1070 1071
       {
	 Lex->mi.connect_retry = $3;
       }
unknown's avatar
unknown committed
1072
       | MASTER_SSL_SYM EQ ulong_num
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
         {
           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
1126
       | RELAY_LOG_POS_SYM EQ ulong_num
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
         {
           Lex->mi.relay_log_pos = $3;
           /* Adjust if < BIN_LOG_HEADER_SIZE (same comment as Lex->mi.pos) */
           Lex->mi.relay_log_pos = max(BIN_LOG_HEADER_SIZE, Lex->mi.relay_log_pos);
         }
       ;

/* create a table */

create:
	CREATE opt_table_options TABLE_SYM opt_if_not_exists table_ident
	{
	  THD *thd= YYTHD;
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_CREATE_TABLE;
	  if (!lex->select_lex.add_table_to_list(thd, $5, NULL,
						 TL_OPTION_UPDATING,
						 (using_update_log ?
						  TL_READ_NO_INSERT:
						  TL_READ)))
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
	  lex->change=NullS;
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	  lex->create_info.options=$2 | $4;
	  lex->create_info.db_type= (enum db_type) lex->thd->variables.table_type;
	  lex->create_info.default_table_charset= NULL;
	  lex->name=0;
	}
	create2
	  { Lex->current_select= &Lex->select_lex; }
	| CREATE opt_unique_or_fulltext INDEX_SYM ident key_alg ON table_ident
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_CREATE_INDEX;
	    if (!lex->current_select->add_table_to_list(lex->thd, $7, NULL,
							TL_OPTION_UPDATING))
	      YYABORT;
	    lex->create_list.empty();
	    lex->key_list.empty();
	    lex->col_list.empty();
	    lex->change=NullS;
	  }
	   '(' key_list ')'
	  {
	    LEX *lex=Lex;

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

	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
1206
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "PROCEDURE");
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
	      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);
	  }
1259
	| CREATE
1260
	  {
1261 1262 1263
            Lex->create_view_mode= VIEW_CREATE_NEW;
            Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED;
            Lex->create_view_suid= TRUE;
1264
	  }
1265
	  view_or_trigger
1266
	  {}
1267 1268 1269 1270
	| CREATE USER clear_privileges grant_list
	  {
	    Lex->sql_command = SQLCOM_CREATE_USER;
          }
1271 1272
	;

1273 1274 1275 1276 1277 1278 1279
clear_privileges:
        /* Nothing */
        {
          LEX *lex=Lex;
          lex->users_list.empty();
          lex->columns.empty();
          lex->grant= lex->grant_tot_col= 0;
1280
	  lex->all_privileges= 0;
1281 1282 1283 1284 1285 1286 1287
          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));
        }
        ;

1288
sp_name:
1289
	  ident '.' ident
1290 1291 1292 1293
	  {
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
1294
	| ident
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
	  {
	    $$= 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
1316
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
	      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
1344 1345 1346 1347
	    lex->charset= NULL;
	    lex->length= lex->dec= NULL;
	    lex->interval_list.empty();
	    lex->type= 0;
1348 1349 1350 1351 1352
	  }
	  type
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
unknown's avatar
unknown committed
1353 1354 1355 1356 1357
            LEX_STRING cmt = { 0, 0 };
	    create_field *new_field;
	    uint unused1= 0;
	    int unused2= 0;

unknown's avatar
unknown committed
1358
	    if (!(new_field= new_create_field(YYTHD, (char*) "",
unknown's avatar
unknown committed
1359 1360 1361 1362 1363 1364 1365
					      (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
1366 1367
	      YYABORT;

1368 1369
	    sp->m_returns_cs= new_field->charset;

unknown's avatar
unknown committed
1370
            if (new_field->interval_list.elements)
1371 1372 1373 1374 1375 1376
            {
	      new_field->interval= 
                sp->create_typelib(&new_field->interval_list);
            }
            sp_prepare_create_field(YYTHD, new_field);

unknown's avatar
unknown committed
1377
	    if (prepare_create_field(new_field, &unused1, &unused2, &unused2,
1378
				     HA_CAN_GEOMETRY))
unknown's avatar
unknown committed
1379 1380 1381 1382 1383 1384
	      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;
1385
            sp->m_returns_typelib= new_field->interval;
1386
            sp->m_geom_returns= new_field->geom_type;
1387
            new_field->interval= NULL;
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402

	    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;

1403 1404 1405
            if (sp->is_not_allowed_in_function("function"))
              YYABORT;

1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
	    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; }
1449
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
	;

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();
1471
	    sp_add_used_routine(lex, YYTHD, $2, TYPE_ENUM_PROCEDURE);
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
	  }
          '(' 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))
	    {
1512
	      my_error(ER_SP_DUP_PARAM, MYF(0), $1.str);
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
	      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))
	    {
1538
	      my_error(ER_SP_DUP_PARAM, MYF(0), $2.str);
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
	      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 */ {}
1555
	| sp_proc_stmts  sp_proc_stmt ';'
1556 1557 1558 1559
	;

sp_proc_stmts1:
	  sp_proc_stmt ';' {}
1560
	| sp_proc_stmts1  sp_proc_stmt ';'
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
	;

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
1576 1577
	      my_message(ER_SP_VARCOND_AFTER_CURSHNDLR,
                         ER(ER_SP_VARCOND_AFTER_CURSHNDLR), MYF(0));
1578 1579 1580 1581
	      YYABORT;
	    }
	    if ($2.curs && $1.hndlrs)
	    { /* Cursor following handler */
unknown's avatar
unknown committed
1582 1583
	      my_message(ER_SP_CURSOR_AFTER_HANDLER,
                         ER(ER_SP_CURSOR_AFTER_HANDLER), MYF(0));
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
	      YYABORT;
	    }
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
	    $$.curs= $1.curs + $2.curs;
	  }
	;

sp_decl:
1594
          DECLARE_SYM sp_decl_idents type 
1595 1596
          { Lex->sphead->reset_lex(YYTHD); }
          sp_opt_default
1597 1598 1599 1600 1601 1602 1603
          {
            LEX *lex= Lex;
            sp_pcontext *ctx= lex->spcont;
            uint max= ctx->context_pvars();
            enum enum_field_types type= (enum enum_field_types)$3;
            Item *it= $5;
            bool has_default= (it != NULL);
1604

1605 1606 1607
            for (uint i = max-$2 ; i < max ; i++)
            {
              sp_instr_set *in;
1608
	      uint off= ctx->pvar_context2index(i);
1609

1610
              ctx->set_type(off, type);
1611 1612 1613 1614
              if (! has_default)
                it= new Item_null();  /* QQ Set to the type with null_value? */
              in = new sp_instr_set(lex->sphead->instructions(),
                                    ctx,
1615
                                    off,
1616 1617 1618 1619 1620 1621 1622 1623
                                    it, type, lex,
                                    (i == max - 1));

              /*
                The last instruction is assigned to be responsible for
                freeing LEX.
              */
              lex->sphead->add_instr(in);
1624
              ctx->set_default(off, it);
1625
            }
1626
            lex->sphead->restore_lex(YYTHD);
1627 1628 1629
            $$.vars= $2;
            $$.conds= $$.hndlrs= $$.curs= 0;
          }
1630 1631 1632 1633 1634 1635 1636
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1637
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
	      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));
1655
	    sp->m_flags|= sp_head::IN_HANDLER;
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
	  }
	  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);
1679
	    sp->m_flags&= ~sp_head::IN_HANDLER;
1680 1681
	    $$.vars= $$.conds= $$.curs= 0;
	    $$.hndlrs= $6;
1682
	    ctx->add_handlers($6);
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
	  }
	| 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))
	    {
1694
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
1695 1696 1697
	      delete $5;
	      YYABORT;
	    }
unknown's avatar
unknown committed
1698 1699
            i= new sp_instr_cpush(sp->instructions(), ctx, $5,
                                  ctx->current_cursors());
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
	    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
1722 1723
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
1724 1725 1726 1727
	      YYABORT;
	    }
	    if (lex->result)
	    {
unknown's avatar
unknown committed
1728 1729
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
	      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;
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
	    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();
1760

1761 1762 1763 1764
	      i->add_condition($1);
	      ctx->push_handler($1);
	      $$= 1;
	    }
1765 1766 1767 1768 1769
	  }
	| sp_hcond_list ',' sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
	    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();
1781

1782 1783 1784 1785
	      i->add_condition($3);
	      ctx->push_handler($3);
	      $$= $1 + 1;
	    }
1786 1787 1788 1789
	  }
	;

sp_cond:
unknown's avatar
unknown committed
1790
	  ulong_num
1791 1792 1793 1794 1795 1796 1797
	  {			/* 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 */
1798 1799 1800 1801 1802
	    if (!sp_cond_check(&$3))
	    {
	      my_error(ER_SP_BAD_SQLSTATE, MYF(0), $3.str);
	      YYABORT;
	    }
1803 1804
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
1805 1806
	    memcpy($$->sqlstate, $3.str, 5);
	    $$->sqlstate[5]= '\0';
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
	  }
	;

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
1825
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
1826 1827 1828 1829 1830 1831 1832 1833
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
1834
	| not FOUND_SYM		/* SQLSTATEs 02??? */
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
	  {
	    $$= (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))
	    {
1854
	      my_error(ER_SP_DUP_VAR, MYF(0), $1.str);
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
	      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))
	    {
1867
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
	      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;

1892
            sp->m_flags|= sp_get_flags_for_command(lex);
1893 1894
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
1895
	      my_error(ER_SP_BADSTATEMENT, MYF(0), "USE");
1896 1897
	      YYABORT;
	    }
1898 1899 1900 1901
	    /*
              Don't add an instruction for SET statements, since all
              instructions for them were already added during processing
              of "set" rule.
1902
	    */
1903 1904 1905
            DBUG_ASSERT(lex->sql_command != SQLCOM_SET_OPTION ||
                        lex->var_list.is_empty());
            if (lex->sql_command != SQLCOM_SET_OPTION)
1906
	    {
1907 1908
              sp_instr_stmt *i=new sp_instr_stmt(sp->instructions(),
                                                 lex->spcont, lex);
1909

1910
              /* Extract the query statement from the tokenizer:
1911
                 The end is either lex->tok_end or tok->ptr. */
1912 1913 1914 1915 1916 1917 1918 1919
              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);
1920 1921 1922
            }
	    sp->restore_lex(YYTHD);
          }
1923 1924 1925
          | RETURN_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr
1926 1927
	  {
	    LEX *lex= Lex;
1928
	    sp_head *sp= lex->sphead;
1929

1930
	    if (sp->m_type == TYPE_ENUM_PROCEDURE)
1931
	    {
unknown's avatar
unknown committed
1932
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
1933 1934 1935 1936 1937 1938
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_freturn *i;

1939 1940 1941
	      i= new sp_instr_freturn(sp->instructions(), lex->spcont,
		                      $3, sp->m_returns, lex);
	      sp->add_instr(i);
1942
	      sp->m_flags|= sp_head::HAS_RETURN;
1943
	    }
1944
	    sp->restore_lex(YYTHD);
1945 1946 1947 1948
	  }
	| IF sp_if END IF {}
	| CASE_SYM WHEN_SYM
	  {
1949
	    Lex->sphead->m_flags&= ~sp_head::IN_SIMPLE_CASE;
1950 1951
	  }
	  sp_case END CASE_SYM {}
1952 1953 1954
        | CASE_SYM
          { Lex->sphead->reset_lex(YYTHD); }
          expr WHEN_SYM
1955 1956 1957 1958 1959 1960 1961 1962
	  {
	    /* 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(),
1963 1964
                                               lex->spcont, offset, $3,
                                               MYSQL_TYPE_STRING, lex, TRUE);
1965
	    LEX_STRING dummy={(char*)"", 0};
1966 1967 1968

	    lex->spcont->push_pvar(&dummy, MYSQL_TYPE_STRING, sp_param_in);
	    lex->sphead->add_instr(i);
1969
	    lex->sphead->m_flags|= sp_head::IN_SIMPLE_CASE;
1970
            lex->sphead->restore_lex(YYTHD);
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
	  }
	  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());
	  }
1989
	| LEAVE_SYM label_ident
1990 1991 1992 1993 1994 1995 1996 1997
	  {
	    LEX *lex= Lex;
	    sp_head *sp = lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab)
	    {
1998
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
	      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);
	    }
	  }
2019
	| ITERATE_SYM label_ident
2020 2021 2022 2023 2024 2025 2026 2027
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab || lab->type != SP_LAB_ITER)
	    {
2028
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "ITERATE", $2.str);
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
	      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
	  {
2049
#ifdef SP_GOTO
2050 2051 2052 2053 2054 2055 2056
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

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

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2141
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
	      YYABORT;
	    }
	    i= new sp_instr_copen(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
	| FETCH_SYM sp_opt_fetch_noise ident INTO
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cfetch *i;

	    if (! lex->spcont->find_cursor(&$3, &offset))
	    {
2156
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $3.str);
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
	      YYABORT;
	    }
	    i= new sp_instr_cfetch(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
	  sp_fetch_list
	  { }
	| CLOSE_SYM ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cclose *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2173
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
	      YYABORT;
	    }
	    i= new sp_instr_cclose(sp->instructions(), lex->spcont,  offset);
	    sp->add_instr(i);
	  }
	;

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

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

	    if (!spc || !(spv = spc->find_pvar(&$1)))
	    {
2197
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

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

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

	      i->add_to_varlist(spv);
	    }
	  }
	;

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

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

2281
	    if (! (sp->m_flags & sp_head::IN_SIMPLE_CASE))
2282
	      i= new sp_instr_jump_if_not(ip, ctx, $2, lex);
2283 2284 2285 2286 2287 2288
	    else
	    { /* Simple case: <caseval> = <whenval> */
	      LEX_STRING ivar;

	      ivar.str= (char *)"_tmp_";
	      ivar.length= 5;
2289 2290
	      Item_splocal *var= new Item_splocal(ivar,
                                                  ctx->current_pvars()-1);
unknown's avatar
unknown committed
2291
#ifndef DBUG_OFF
2292 2293 2294
              if (var)
                var->owner= sp;
#endif
2295
	      Item *expr= new Item_func_eq(var, $2);
2296

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

2323 2324 2325 2326 2327 2328 2329
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
2330

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

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

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

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

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

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

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

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

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

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

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

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

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

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

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
2547 2548
	default_collation   {}
	| default_charset   {};
2549

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

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

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

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

opt_create_table_options:
	/* empty */
unknown's avatar
unknown committed
2567
	| create_table_options;
unknown's avatar
unknown committed
2568

unknown's avatar
unknown committed
2569 2570 2571 2572
create_table_options_space_separated:
	create_table_option
	| create_table_option create_table_options_space_separated;

unknown's avatar
unknown committed
2573 2574
create_table_options:
	create_table_option
2575
	| create_table_option     create_table_options
unknown's avatar
unknown committed
2576
	| create_table_option ',' create_table_options;
unknown's avatar
unknown committed
2577 2578

create_table_option:
2579 2580
	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; }
2581 2582
	| 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
2583
	| 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;}
2584 2585
	| 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; }
2586
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
        | PACK_KEYS_SYM opt_equal ulong_num
          {
            switch($3) {
            case 0:
                Lex->create_info.table_options|= HA_OPTION_NO_PACK_KEYS;
                break;
            case 1:
                Lex->create_info.table_options|= HA_OPTION_PACK_KEYS;
                break;
            default:
                yyerror(ER(ER_SYNTAX_ERROR));
                YYABORT;
            }
            Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;
          }
        | PACK_KEYS_SYM opt_equal DEFAULT
          {
            Lex->create_info.table_options&=
              ~(HA_OPTION_PACK_KEYS | HA_OPTION_NO_PACK_KEYS);
            Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;
          }
unknown's avatar
unknown committed
2608 2609
	| 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; }
2610
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
unknown's avatar
unknown committed
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625
	| RAID_TYPE opt_equal raid_types
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_TYPE", "PARTITION");
	    YYABORT;
	  }
	| RAID_CHUNKS opt_equal ulong_num
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_CHUNKS", "PARTITION");
	    YYABORT;
	  }
	| RAID_CHUNKSIZE opt_equal ulong_num
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_CHUNKSIZE", "PARTITION");
	    YYABORT;
	  }
2626
	| UNION_SYM opt_equal '(' table_list ')'
2627 2628 2629
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
2630 2631
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
2632
	    lex->create_info.merge_list.elements--;
2633 2634
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
2635
	    lex->select_lex.table_list.elements=1;
2636 2637 2638
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
2639
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
2640
	  }
2641 2642
	| default_charset
	| default_collation
2643
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
2644 2645
	| 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; }
2646
	| CONNECTION_SYM opt_equal TEXT_STRING_sys { Lex->create_info.connect_string.str= $3.str; Lex->create_info.connect_string.length= $3.length;  Lex->create_info.used_fields|= HA_CREATE_USED_CONNECTION; }
2647
        ;
unknown's avatar
unknown committed
2648

2649 2650 2651 2652 2653 2654 2655 2656
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))
          {
2657 2658 2659
            my_error(ER_CONFLICTING_DECLARATIONS, MYF(0),
                     "CHARACTER SET ", cinfo->default_table_charset->csname,
                     "CHARACTER SET ", $4->csname);
2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673
            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))
            {
2674 2675
              my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $4->name, cinfo->default_table_charset->csname);
2676 2677 2678 2679 2680 2681
              YYABORT;
            }
            Lex->create_info.default_table_charset= $4;
            Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

unknown's avatar
unknown committed
2682
storage_engines:
2683 2684 2685 2686
	ident_or_text
	{
	  $$ = ha_resolve_by_name($1.str,$1.length);
	  if ($$ == DB_TYPE_UNKNOWN) {
2687
	    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
2688 2689 2690
	    YYABORT;
	  }
	};
unknown's avatar
unknown committed
2691 2692 2693 2694 2695

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
2696 2697 2698
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; }
	| REDUNDANT_SYM	{ $$= ROW_TYPE_REDUNDANT; }
	| COMPACT_SYM	{ $$= ROW_TYPE_COMPACT; };
unknown's avatar
unknown committed
2699 2700 2701 2702

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

2705 2706 2707
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
unknown's avatar
unknown committed
2708
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
2709

unknown's avatar
unknown committed
2710
opt_select_from:
2711
	opt_limit_clause {}
unknown's avatar
unknown committed
2712
	| select_from select_lock_type;
unknown's avatar
unknown committed
2713 2714

udf_func_type:
2715
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
unknown's avatar
unknown committed
2716
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
unknown's avatar
unknown committed
2717 2718 2719 2720

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
unknown's avatar
unknown committed
2721
        | DECIMAL_SYM {$$ = (int) DECIMAL_RESULT; }
unknown's avatar
unknown committed
2722
	| INT_SYM {$$ = (int) INT_RESULT; };
unknown's avatar
unknown committed
2723 2724 2725

field_list:
	  field_list_item
unknown's avatar
unknown committed
2726
	| field_list ',' field_list_item;
unknown's avatar
unknown committed
2727 2728 2729


field_list_item:
unknown's avatar
unknown committed
2730
	   column_def
2731 2732 2733 2734
         | key_def
         ;

column_def:
unknown's avatar
unknown committed
2735
	  field_spec opt_check_constraint
unknown's avatar
unknown committed
2736 2737 2738 2739
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
unknown's avatar
unknown committed
2740
	;
2741 2742

key_def:
unknown's avatar
unknown committed
2743
	key_type opt_ident key_alg '(' key_list ')'
unknown's avatar
unknown committed
2744
	  {
unknown's avatar
unknown committed
2745
	    LEX *lex=Lex;
2746
	    lex->key_list.push_back(new Key($1,$2, $3, 0, lex->col_list));
unknown's avatar
unknown committed
2747
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
2748
	  }
2749 2750 2751 2752
	| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
	  {
	    LEX *lex=Lex;
	    const char *key_name= $3 ? $3:$1;
2753 2754
	    lex->key_list.push_back(new Key($2, key_name, $4, 0,
				    lex->col_list));
2755 2756
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
2757
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
unknown's avatar
unknown committed
2758
	  {
2759
	    LEX *lex=Lex;
2760
	    lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
2761 2762 2763 2764 2765
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
2766 2767 2768
	    lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4 : $1,
					    HA_KEY_ALG_UNDEF, 1,
					    lex->col_list));
2769
	    lex->col_list.empty();		/* Alloced by sql_alloc */
unknown's avatar
unknown committed
2770
	  }
unknown's avatar
unknown committed
2771 2772 2773 2774
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
2775
	| opt_constraint check_constraint
unknown's avatar
unknown committed
2776 2777
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
2778 2779 2780
	  }
	;

unknown's avatar
unknown committed
2781
opt_check_constraint:
2782
	/* empty */
unknown's avatar
unknown committed
2783 2784 2785 2786 2787
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
2788
	;
unknown's avatar
unknown committed
2789 2790

opt_constraint:
2791
	/* empty */		{ $$=(char*) 0; }
unknown's avatar
unknown committed
2792 2793 2794 2795 2796 2797
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
unknown's avatar
unknown committed
2798 2799 2800 2801

field_spec:
	field_ident
	 {
unknown's avatar
unknown committed
2802
	   LEX *lex=Lex;
2803
	   lex->length=lex->dec=0; lex->type=0;
2804
	   lex->default_value= lex->on_update_value= 0;
2805
           lex->comment=null_lex_str;
2806
	   lex->charset=NULL;
unknown's avatar
unknown committed
2807 2808 2809
	 }
	type opt_attribute
	{
unknown's avatar
unknown committed
2810
	  LEX *lex=Lex;
unknown's avatar
unknown committed
2811
	  if (add_field_to_list(lex->thd, $1.str,
unknown's avatar
unknown committed
2812
				(enum enum_field_types) $3,
unknown's avatar
unknown committed
2813
				lex->length,lex->dec,lex->type,
2814
				lex->default_value, lex->on_update_value, 
2815
                                &lex->comment,
2816
				lex->change,&lex->interval_list,lex->charset,
unknown's avatar
unknown committed
2817
				lex->uint_geom_type))
unknown's avatar
unknown committed
2818
	    YYABORT;
unknown's avatar
unknown committed
2819
	};
unknown's avatar
unknown committed
2820 2821

type:
2822
	int_type opt_len field_options	{ $$=$1; }
unknown's avatar
unknown committed
2823 2824
	| real_type opt_precision field_options { $$=$1; }
	| FLOAT_SYM float_options field_options { $$=FIELD_TYPE_FLOAT; }
unknown's avatar
unknown committed
2825 2826 2827 2828
	| BIT_SYM			{ Lex->length= (char*) "1";
					  $$=FIELD_TYPE_BIT; }
	| BIT_SYM '(' NUM ')'		{ Lex->length= $3.str;
					  $$=FIELD_TYPE_BIT; }
unknown's avatar
unknown committed
2829 2830
	| BOOL_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
2831 2832
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
2833
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
unknown's avatar
unknown committed
2834 2835 2836
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
2837
	| nchar '(' NUM ')' opt_bin_mod	{ Lex->length=$3.str;
unknown's avatar
unknown committed
2838
					  $$=FIELD_TYPE_STRING;
2839
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
2840
	| nchar opt_bin_mod		{ Lex->length=(char*) "1";
unknown's avatar
unknown committed
2841
					  $$=FIELD_TYPE_STRING;
2842
					  Lex->charset=national_charset_info; }
2843
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
unknown's avatar
unknown committed
2844
					  Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2845
					  $$=FIELD_TYPE_STRING; }
2846 2847 2848
	| BINARY			{ Lex->length= (char*) "1";
					  Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_STRING; }
unknown's avatar
unknown committed
2849
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
2850
					  $$= MYSQL_TYPE_VARCHAR; }
unknown's avatar
unknown committed
2851
	| nvarchar '(' NUM ')' opt_bin_mod { Lex->length=$3.str;
2852
					  $$= MYSQL_TYPE_VARCHAR;
2853
					  Lex->charset=national_charset_info; }
unknown's avatar
unknown committed
2854
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
unknown's avatar
unknown committed
2855
					  Lex->charset=&my_charset_bin;
2856
					  $$= MYSQL_TYPE_VARCHAR; }
2857
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
unknown's avatar
unknown committed
2858 2859
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
2860
	| TIMESTAMP opt_len
2861
	  {
2862
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
2863 2864
	      $$=FIELD_TYPE_DATETIME;
	    else
2865 2866 2867 2868 2869
            {
              /* 
                Unlike other types TIMESTAMP fields are NOT NULL by default.
              */
              Lex->type|= NOT_NULL_FLAG;
2870
	      $$=FIELD_TYPE_TIMESTAMP;
2871
            }
2872
	   }
unknown's avatar
unknown committed
2873
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
unknown's avatar
unknown committed
2874
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2875
					  $$=FIELD_TYPE_TINY_BLOB; }
unknown's avatar
unknown committed
2876
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2877
					  $$=FIELD_TYPE_BLOB; }
unknown's avatar
unknown committed
2878 2879
	| spatial_type
          {
unknown's avatar
unknown committed
2880
#ifdef HAVE_SPATIAL
unknown's avatar
unknown committed
2881 2882 2883
            Lex->charset=&my_charset_bin;
            Lex->uint_geom_type= (uint)$1;
            $$=FIELD_TYPE_GEOMETRY;
unknown's avatar
unknown committed
2884
#else
unknown's avatar
unknown committed
2885
            my_error(ER_FEATURE_DISABLED, MYF(0),
2886
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
2887
            YYABORT;
unknown's avatar
unknown committed
2888
#endif
unknown's avatar
unknown committed
2889
          }
unknown's avatar
unknown committed
2890
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2891
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
2892
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2893
					  $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
2894
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
unknown's avatar
unknown committed
2895
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
2896 2897
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
2898
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
2899 2900
	| MEDIUMTEXT opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| LONGTEXT opt_binary		{ $$=FIELD_TYPE_LONG_BLOB; }
unknown's avatar
unknown committed
2901
	| DECIMAL_SYM float_options field_options
unknown's avatar
unknown committed
2902
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
unknown's avatar
unknown committed
2903
	| NUMERIC_SYM float_options field_options
unknown's avatar
unknown committed
2904
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
2905
	| FIXED_SYM float_options field_options
unknown's avatar
unknown committed
2906
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
2907
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
2908
	  { $$=FIELD_TYPE_ENUM; }
2909
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
2910
	  { $$=FIELD_TYPE_SET; }
2911
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
unknown's avatar
unknown committed
2912 2913 2914 2915 2916 2917
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
2918
	;
unknown's avatar
unknown committed
2919

unknown's avatar
unknown committed
2920 2921 2922
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
2923 2924 2925
	| POINT_SYM           { Lex->length= (char*)"21";
                                $$= Field::GEOM_POINT;
                              }
unknown's avatar
unknown committed
2926 2927 2928 2929 2930 2931 2932
	| 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
2933 2934
char:
	CHAR_SYM {}
unknown's avatar
unknown committed
2935 2936 2937 2938 2939 2940
	;

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

unknown's avatar
unknown committed
2942 2943 2944
varchar:
	char VARYING {}
	| VARCHAR {}
unknown's avatar
unknown committed
2945 2946 2947 2948
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
unknown's avatar
unknown committed
2949
	| NVARCHAR_SYM {}
unknown's avatar
unknown committed
2950 2951 2952 2953
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
unknown's avatar
unknown committed
2954 2955 2956 2957 2958 2959

int_type:
	INT_SYM		{ $$=FIELD_TYPE_LONG; }
	| TINYINT	{ $$=FIELD_TYPE_TINY; }
	| SMALLINT	{ $$=FIELD_TYPE_SHORT; }
	| MEDIUMINT	{ $$=FIELD_TYPE_INT24; }
unknown's avatar
unknown committed
2960
	| BIGINT	{ $$=FIELD_TYPE_LONGLONG; };
unknown's avatar
unknown committed
2961 2962

real_type:
2963
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
unknown's avatar
unknown committed
2964 2965
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
unknown's avatar
unknown committed
2966
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
unknown's avatar
unknown committed
2967 2968 2969


float_options:
unknown's avatar
unknown committed
2970 2971
        /* empty */		{ Lex->dec=Lex->length= (char*)0; }
        | '(' NUM ')'		{ Lex->length=$2.str; Lex->dec= (char*)0; }
unknown's avatar
unknown committed
2972
	| precision		{};
unknown's avatar
unknown committed
2973 2974 2975 2976 2977 2978

precision:
	'(' NUM ',' NUM ')'
	{
	  LEX *lex=Lex;
	  lex->length=$2.str; lex->dec=$4.str;
unknown's avatar
unknown committed
2979
	};
unknown's avatar
unknown committed
2980 2981 2982

field_options:
	/* empty */		{}
unknown's avatar
unknown committed
2983
	| field_opt_list	{};
unknown's avatar
unknown committed
2984 2985 2986

field_opt_list:
	field_opt_list field_option {}
unknown's avatar
unknown committed
2987
	| field_option {};
unknown's avatar
unknown committed
2988 2989

field_option:
unknown's avatar
unknown committed
2990
	SIGNED_SYM	{}
2991
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
unknown's avatar
unknown committed
2992
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
unknown's avatar
unknown committed
2993 2994

opt_len:
2995 2996
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
unknown's avatar
unknown committed
2997 2998 2999

opt_precision:
	/* empty */	{}
unknown's avatar
unknown committed
3000
	| precision	{};
unknown's avatar
unknown committed
3001 3002 3003

opt_attribute:
	/* empty */ {}
unknown's avatar
unknown committed
3004
	| opt_attribute_list {};
unknown's avatar
unknown committed
3005 3006 3007

opt_attribute_list:
	opt_attribute_list attribute {}
unknown's avatar
unknown committed
3008
	| attribute;
unknown's avatar
unknown committed
3009 3010 3011

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
3012
	| not NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
3013 3014 3015
	| 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
3016
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
3017
	| SERIAL_SYM DEFAULT VALUE_SYM
3018 3019 3020
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
3021
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3022 3023 3024 3025 3026
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
3027
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3028 3029 3030 3031 3032
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
3033
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3034 3035 3036 3037 3038
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
3039
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3040
	  }
3041
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= $2; }
unknown's avatar
unknown committed
3042 3043
	| COLLATE_SYM collation_name
	  {
unknown's avatar
unknown committed
3044
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
unknown's avatar
unknown committed
3045
	    {
3046 3047
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $2->name,Lex->charset->csname);
unknown's avatar
unknown committed
3048 3049 3050 3051 3052 3053 3054 3055
	      YYABORT;
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
unknown's avatar
unknown committed
3056

3057 3058 3059 3060 3061
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

3062 3063 3064 3065
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
unknown's avatar
unknown committed
3066

3067
charset_name:
3068
	ident_or_text
3069
	{
unknown's avatar
unknown committed
3070
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
3071
	  {
3072
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
3073 3074
	    YYABORT;
	  }
unknown's avatar
unknown committed
3075 3076 3077
	}
	| BINARY { $$= &my_charset_bin; }
	;
3078

3079 3080 3081
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
unknown's avatar
unknown committed
3082

unknown's avatar
unknown committed
3083 3084 3085 3086 3087 3088 3089

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)))
	  {
3090
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
unknown's avatar
unknown committed
3091 3092 3093 3094 3095 3096 3097 3098 3099 3100
	    YYABORT;
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

3101
collation_name:
3102
	ident_or_text
3103 3104 3105
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
3106
	    my_error(ER_UNKNOWN_COLLATION, MYF(0), $1.str);
3107 3108 3109 3110
	    YYABORT;
	  }
	};

3111 3112
opt_collate:
	/* empty */	{ $$=NULL; }
unknown's avatar
unknown committed
3113
	| COLLATE_SYM collation_name_or_default { $$=$2; }
3114 3115
	;

3116 3117 3118 3119
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

3120 3121 3122 3123
opt_default:
	/* empty */	{}
	| DEFAULT	{};

unknown's avatar
unknown committed
3124
opt_binary:
3125
	/* empty */			{ Lex->charset=NULL; }
unknown's avatar
unknown committed
3126
	| ASCII_SYM opt_bin_mod		{ Lex->charset=&my_charset_latin1; }
unknown's avatar
unknown committed
3127
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
unknown's avatar
unknown committed
3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146
	| UNICODE_SYM opt_bin_mod
	{
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
	  {
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
	    YYABORT;
	  }
	}
	| charset charset_name opt_bin_mod	{ Lex->charset=$2; }
        | BINARY opt_bin_charset { Lex->type|= BINCMP_FLAG; };

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

opt_bin_charset:
	/* empty */ { }
	| ASCII_SYM	{ Lex->charset=&my_charset_latin1; }
unknown's avatar
unknown committed
3147 3148
	| UNICODE_SYM
	{
unknown's avatar
unknown committed
3149 3150
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
unknown's avatar
unknown committed
3151
	  {
unknown's avatar
unknown committed
3152
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
unknown's avatar
unknown committed
3153 3154 3155
	    YYABORT;
	  }
	}
3156
	| charset charset_name	{ Lex->charset=$2; } ;
unknown's avatar
unknown committed
3157

unknown's avatar
unknown committed
3158 3159 3160
opt_primary:
	/* empty */
	| PRIMARY_SYM
3161
	;
unknown's avatar
unknown committed
3162

unknown's avatar
unknown committed
3163
references:
3164 3165
	REFERENCES table_ident
	{
3166
	  LEX *lex=Lex;
3167 3168 3169 3170 3171 3172
	  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
3173
	};
3174

3175
opt_ref_list:
unknown's avatar
unknown committed
3176
	/* empty */ opt_on_delete {}
unknown's avatar
unknown committed
3177
	| '(' ref_list ')' opt_on_delete {};
3178 3179 3180

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

unknown's avatar
unknown committed
3183 3184 3185

opt_on_delete:
	/* empty */ {}
unknown's avatar
unknown committed
3186
	| opt_on_delete_list {};
unknown's avatar
unknown committed
3187 3188 3189

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
unknown's avatar
unknown committed
3190
	| opt_on_delete_item {};
unknown's avatar
unknown committed
3191 3192

opt_on_delete_item:
3193 3194 3195 3196
	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
3197
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
unknown's avatar
unknown committed
3198 3199

delete_option:
3200 3201 3202 3203
	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
3204
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
unknown's avatar
unknown committed
3205 3206

key_type:
3207
	key_or_index			    { $$= Key::MULTIPLE; }
unknown's avatar
SCRUM  
unknown committed
3208 3209
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
unknown's avatar
unknown committed
3210 3211 3212 3213
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
3214 3215
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3216 3217 3218
	    YYABORT;
#endif
	  };
3219 3220 3221

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
unknown's avatar
SCRUM  
unknown committed
3222
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
unknown's avatar
unknown committed
3223 3224 3225

key_or_index:
	KEY_SYM {}
unknown's avatar
unknown committed
3226
	| INDEX_SYM {};
unknown's avatar
unknown committed
3227

unknown's avatar
SCRUM  
unknown committed
3228 3229 3230 3231 3232
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

unknown's avatar
unknown committed
3233 3234
keys_or_index:
	KEYS {}
unknown's avatar
unknown committed
3235
	| INDEX_SYM {}
unknown's avatar
unknown committed
3236
	| INDEXES {};
unknown's avatar
unknown committed
3237

3238
opt_unique_or_fulltext:
unknown's avatar
unknown committed
3239 3240
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
unknown's avatar
unknown committed
3241
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
unknown's avatar
unknown committed
3242 3243 3244 3245 3246
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
unknown's avatar
unknown committed
3247
            my_error(ER_FEATURE_DISABLED, MYF(0),
unknown's avatar
unknown committed
3248
                     sym_group_geom.name, sym_group_geom.needed_define);
unknown's avatar
unknown committed
3249 3250 3251
	    YYABORT;
#endif
	  }
unknown's avatar
unknown committed
3252
        ;
unknown's avatar
unknown committed
3253 3254

key_alg:
3255
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
3256
	| USING opt_btree_or_rtree { $$= $2; }
unknown's avatar
unknown committed
3257
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
unknown's avatar
unknown committed
3258 3259 3260

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
unknown's avatar
unknown committed
3261 3262 3263 3264
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
unknown's avatar
unknown committed
3265
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
unknown's avatar
unknown committed
3266 3267 3268

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
unknown's avatar
unknown committed
3269
	| key_part order_dir		{ Lex->col_list.push_back($1); };
unknown's avatar
unknown committed
3270 3271 3272

key_part:
	ident			{ $$=new key_part_spec($1.str); }
3273 3274 3275 3276 3277
	| ident '(' NUM ')'	
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
3278
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
3279 3280 3281
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
unknown's avatar
unknown committed
3282 3283 3284

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

3287
opt_component:
3288 3289
        /* empty */      { $$= null_lex_str; }
        | '.' ident      { $$= $2; };
3290

unknown's avatar
unknown committed
3291 3292
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
unknown's avatar
unknown committed
3293
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
unknown's avatar
unknown committed
3294 3295 3296 3297 3298 3299 3300 3301

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
3302
	  THD *thd= YYTHD;
3303
	  LEX *lex= thd->lex;
3304 3305 3306
	  lex->sql_command= SQLCOM_ALTER_TABLE;
	  lex->name= 0;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
3307 3308
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
unknown's avatar
unknown committed
3309 3310 3311 3312
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
3313
          lex->select_lex.init_order();
3314
	  lex->select_lex.db=lex->name=0;
3315
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
unknown's avatar
unknown committed
3316
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
3317
	  lex->create_info.default_table_charset= NULL;
3318
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
3319
	  lex->alter_info.reset();
3320
	  lex->alter_info.flags= 0;
unknown's avatar
unknown committed
3321
	}
3322 3323
	alter_list
	{}
3324
	| ALTER DATABASE ident_or_empty
3325 3326 3327 3328 3329
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
          opt_create_database_options
3330 3331 3332
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
3333
	    lex->name= $3;
3334 3335 3336 3337 3338
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

3339 3340 3341 3342 3343
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
	      YYABORT;
	    }
3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355
	    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;
3356

3357 3358 3359 3360 3361
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
	      YYABORT;
	    }
3362 3363 3364 3365 3366 3367 3368 3369 3370
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    LEX *lex=Lex;

	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
	    lex->spname= $3;
	  }
3371 3372
        | ALTER view_algorithm_opt definer view_suid
          VIEW_SYM table_ident
3373 3374 3375 3376
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_CREATE_VIEW;
3377
            lex->create_view_start= thd->query;
3378 3379
	    lex->create_view_mode= VIEW_ALTER;
	    /* first table in list is target VIEW name */
3380
	    lex->select_lex.add_table_to_list(thd, $6, NULL, 0);
3381
	  }
3382
	  view_list_opt AS view_select view_check_option
3383 3384
	  {}
	;
3385

3386 3387 3388 3389
ident_or_empty:
	/* empty */  { $$= 0; }
	| ident      { $$= $1.str; };

unknown's avatar
unknown committed
3390
alter_list:
3391 3392
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
unknown's avatar
unknown committed
3393
        | alter_list_item
unknown's avatar
unknown committed
3394
	| alter_list ',' alter_list_item;
unknown's avatar
unknown committed
3395 3396

add_column:
3397
	ADD opt_column
3398 3399
	{
	  LEX *lex=Lex;
3400 3401
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
3402
	};
unknown's avatar
unknown committed
3403 3404

alter_list_item:
3405 3406 3407 3408
	add_column column_def opt_place { }
	| ADD key_def
	  {
	    Lex->alter_info.flags|= ALTER_ADD_INDEX;
3409
	  }
3410 3411 3412 3413
	| add_column '(' field_list ')'
          {
	    Lex->alter_info.flags|= ALTER_ADD_COLUMN | ALTER_ADD_INDEX;
          }
unknown's avatar
unknown committed
3414 3415 3416
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
3417
	     lex->change= $3.str;
3418
	     lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
3419
	  }
3420
          field_spec opt_place
unknown's avatar
unknown committed
3421 3422 3423
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
3424
            lex->length=lex->dec=0; lex->type=0;
3425
            lex->default_value= lex->on_update_value= 0;
3426
            lex->comment=null_lex_str;
3427
	    lex->charset= NULL;
3428
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
unknown's avatar
unknown committed
3429 3430 3431 3432
          }
          type opt_attribute
          {
            LEX *lex=Lex;
unknown's avatar
unknown committed
3433
            if (add_field_to_list(lex->thd,$3.str,
unknown's avatar
unknown committed
3434 3435
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
3436
                                  lex->default_value, lex->on_update_value,
3437
                                  &lex->comment,
3438
				  $3.str, &lex->interval_list, lex->charset,
unknown's avatar
unknown committed
3439
				  lex->uint_geom_type))
unknown's avatar
unknown committed
3440 3441 3442
	       YYABORT;
          }
          opt_place
unknown's avatar
unknown committed
3443
	| DROP opt_column field_ident opt_restrict
unknown's avatar
unknown committed
3444 3445
	  {
	    LEX *lex=Lex;
3446
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
3447
                                                               $3.str));
3448
	    lex->alter_info.flags|= ALTER_DROP_COLUMN;
unknown's avatar
unknown committed
3449
	  }
3450 3451 3452 3453
	| DROP FOREIGN KEY_SYM opt_ident
          {
	    Lex->alter_info.flags|= ALTER_DROP_INDEX;
          }
unknown's avatar
unknown committed
3454 3455 3456
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
3457 3458 3459
	    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
3460
	  }
unknown's avatar
unknown committed
3461
	| DROP key_or_index field_ident
unknown's avatar
unknown committed
3462 3463
	  {
	    LEX *lex=Lex;
3464 3465 3466
	    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
3467
	  }
3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479
	| 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
3480
	| ALTER opt_column field_ident SET DEFAULT signed_literal
unknown's avatar
unknown committed
3481 3482
	  {
	    LEX *lex=Lex;
3483
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,$6));
3484
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
3485
	  }
unknown's avatar
unknown committed
3486
	| ALTER opt_column field_ident DROP DEFAULT
unknown's avatar
unknown committed
3487 3488
	  {
	    LEX *lex=Lex;
3489 3490
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,
                                                                  (Item*) 0));
3491
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
unknown's avatar
unknown committed
3492
	  }
3493
	| RENAME opt_to table_ident
3494
	  {
3495
	    LEX *lex=Lex;
3496
	    lex->select_lex.db=$3->db.str;
3497
	    lex->name= $3->table.str;
3498 3499 3500
            if (check_table_name($3->table.str,$3->table.length) ||
                $3->db.str && check_db_name($3->db.str))
            {
3501
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3->table.str);
3502 3503
              YYABORT;
            }
3504
	    lex->alter_info.flags|= ALTER_RENAME;
3505
	  }
3506 3507 3508 3509 3510 3511 3512 3513 3514 3515
	| 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))
	    {
3516 3517
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $5->name, $4->csname);
3518 3519
	      YYABORT;
	    }
3520
	    LEX *lex= Lex;
3521
	    lex->create_info.table_charset=
unknown's avatar
unknown committed
3522 3523 3524
	      lex->create_info.default_table_charset= $5;
	    lex->create_info.used_fields|= (HA_CREATE_USED_CHARSET |
					    HA_CREATE_USED_DEFAULT_CHARSET);
3525
	    lex->alter_info.flags|= ALTER_CONVERT;
3526
	  }
3527
        | create_table_options_space_separated
3528 3529
	  {
	    LEX *lex=Lex;
3530
	    lex->alter_info.flags|= ALTER_OPTIONS;
3531
	  }
3532 3533 3534 3535
	| FORCE_SYM
	  {
	    Lex->alter_info.flags|= ALTER_FORCE;
	   }
3536
	| order_clause
3537 3538
	  {
	    LEX *lex=Lex;
3539
	    lex->alter_info.flags|= ALTER_ORDER;
3540
	  };
unknown's avatar
unknown committed
3541 3542 3543

opt_column:
	/* empty */	{}
unknown's avatar
unknown committed
3544
	| COLUMN_SYM	{};
unknown's avatar
unknown committed
3545 3546

opt_ignore:
3547 3548 3549
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
unknown's avatar
unknown committed
3550 3551

opt_restrict:
3552 3553 3554 3555
	/* empty */	{ Lex->drop_mode= DROP_DEFAULT; }
	| RESTRICT	{ Lex->drop_mode= DROP_RESTRICT; }
	| CASCADE	{ Lex->drop_mode= DROP_CASCADE; }
	;
unknown's avatar
unknown committed
3556 3557 3558 3559

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
unknown's avatar
unknown committed
3560
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
unknown's avatar
unknown committed
3561 3562 3563 3564

opt_to:
	/* empty */	{}
	| TO_SYM	{}
3565
	| EQ		{}
unknown's avatar
unknown committed
3566
	| AS		{};
unknown's avatar
unknown committed
3567

unknown's avatar
unknown committed
3568
/*
3569
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
unknown's avatar
unknown committed
3570 3571
*/

unknown's avatar
unknown committed
3572
slave:
3573
	  START_SYM SLAVE slave_thread_opts
3574 3575 3576 3577 3578 3579
          {
	    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
3580
            /* If you change this code don't forget to update SLAVE START too */
3581 3582 3583
          }
          slave_until
          {}
unknown's avatar
unknown committed
3584 3585 3586 3587 3588
        | STOP_SYM SLAVE slave_thread_opts
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_STOP;
	    lex->type = 0;
unknown's avatar
unknown committed
3589
            /* If you change this code don't forget to update SLAVE STOP too */
unknown's avatar
unknown committed
3590
          }
3591 3592 3593 3594 3595
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
3596 3597 3598 3599 3600
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
3601 3602 3603 3604 3605 3606 3607 3608
	| SLAVE STOP_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_STOP;
	   lex->type = 0;
         }
        ;

unknown's avatar
unknown committed
3609

unknown's avatar
unknown committed
3610
start:
3611 3612
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
3613 3614 3615
          LEX *lex= Lex;
          lex->sql_command= SQLCOM_BEGIN;
          lex->start_transaction_opt= $3;
3616
        }
unknown's avatar
unknown committed
3617 3618
	;

3619 3620 3621 3622 3623 3624
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
3625
        ;
3626

unknown's avatar
unknown committed
3627
slave_thread_opts:
3628 3629
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
3630
        {}
unknown's avatar
unknown committed
3631
	;
3632 3633

slave_thread_opt_list:
unknown's avatar
unknown committed
3634
	slave_thread_opt
3635 3636
	| slave_thread_opt_list ',' slave_thread_opt
	;
3637 3638

slave_thread_opt:
3639
	/*empty*/	{}
unknown's avatar
unknown committed
3640
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
unknown's avatar
unknown committed
3641
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
unknown's avatar
unknown committed
3642
	;
3643

3644 3645 3646 3647 3648 3649 3650 3651 3652 3653
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
3654 3655
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


unknown's avatar
unknown committed
3667 3668 3669 3670 3671
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
	}
unknown's avatar
unknown committed
3672
	table_list FROM TEXT_STRING_sys
unknown's avatar
unknown committed
3673 3674
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
3675
        };
unknown's avatar
unknown committed
3676

unknown's avatar
unknown committed
3677 3678 3679 3680 3681
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
	}
unknown's avatar
unknown committed
3682
	table_list TO_SYM TEXT_STRING_sys
unknown's avatar
unknown committed
3683 3684
        {
	  Lex->backup_dir = $6.str;
unknown's avatar
unknown committed
3685
        };
unknown's avatar
unknown committed
3686

3687 3688 3689 3690 3691 3692
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
3693 3694
	table_list opt_checksum_type
        {}
3695 3696
	;

3697 3698 3699 3700 3701 3702
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
3703
repair:
3704
	REPAIR opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3705
	{
unknown's avatar
unknown committed
3706 3707
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
3708
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3709
	   lex->check_opt.init();
unknown's avatar
unknown committed
3710
	}
3711 3712 3713
	table_list opt_mi_repair_type
	{}
	;
unknown's avatar
unknown committed
3714

unknown's avatar
unknown committed
3715
opt_mi_repair_type:
unknown's avatar
unknown committed
3716
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
3717
	| mi_repair_types {};
unknown's avatar
unknown committed
3718

unknown's avatar
unknown committed
3719 3720
mi_repair_types:
	mi_repair_type {}
unknown's avatar
unknown committed
3721
	| mi_repair_type mi_repair_types {};
3722

unknown's avatar
unknown committed
3723
mi_repair_type:
unknown's avatar
unknown committed
3724
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
3725
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
unknown's avatar
unknown committed
3726
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
unknown's avatar
unknown committed
3727 3728

analyze:
3729
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3730
	{
unknown's avatar
unknown committed
3731 3732
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
3733
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3734
	   lex->check_opt.init();
unknown's avatar
unknown committed
3735
	}
3736 3737 3738
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3739 3740

check:
unknown's avatar
unknown committed
3741
	CHECK_SYM table_or_tables
unknown's avatar
unknown committed
3742
	{
3743 3744 3745 3746 3747 3748 3749 3750 3751
	  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
3752
	}
3753 3754 3755
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3756

unknown's avatar
unknown committed
3757 3758
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
unknown's avatar
unknown committed
3759
	| mi_check_types {};
unknown's avatar
unknown committed
3760 3761 3762

mi_check_types:
	mi_check_type {}
unknown's avatar
unknown committed
3763
	| mi_check_type mi_check_types {};
unknown's avatar
unknown committed
3764 3765 3766 3767 3768 3769

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

unknown's avatar
unknown committed
3772
optimize:
3773
	OPTIMIZE opt_no_write_to_binlog table_or_tables
unknown's avatar
unknown committed
3774
	{
unknown's avatar
unknown committed
3775
	   LEX *lex=Lex;
3776 3777 3778 3779 3780
	   if (lex->sphead)
	   {
	     my_error(ER_SP_BADSTATEMENT, MYF(0), "OPTIMIZE TABLE");
	     YYABORT;
	   }
unknown's avatar
unknown committed
3781
	   lex->sql_command = SQLCOM_OPTIMIZE;
unknown's avatar
unknown committed
3782
           lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
3783
	   lex->check_opt.init();
unknown's avatar
unknown committed
3784
	}
3785 3786 3787
	table_list opt_mi_check_type
	{}
	;
unknown's avatar
unknown committed
3788

3789 3790 3791
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
3792
	| LOCAL_SYM  { $$= 1; }
3793 3794
	;

3795 3796 3797
rename:
	RENAME table_or_tables
	{
3798
          Lex->sql_command= SQLCOM_RENAME_TABLE;
3799
	}
3800 3801
	table_to_table_list
	{}
3802 3803 3804 3805
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
3806
	;
3807

3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820
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;
          }
        ;

3821 3822
table_to_table_list:
	table_to_table
unknown's avatar
unknown committed
3823
	| table_to_table_list ',' table_to_table;
3824 3825 3826

table_to_table:
	table_ident TO_SYM table_ident
3827
	{
unknown's avatar
unknown committed
3828
	  LEX *lex=Lex;
unknown's avatar
unknown committed
3829
	  SELECT_LEX *sl= lex->current_select;
unknown's avatar
unknown committed
3830 3831 3832 3833
	  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))
3834
	    YYABORT;
3835
	};
3836

unknown's avatar
unknown committed
3837
keycache:
unknown's avatar
unknown committed
3838
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
unknown's avatar
unknown committed
3839 3840
        {
          LEX *lex=Lex;
3841
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
3842
	  lex->ident= $5;
unknown's avatar
unknown committed
3843 3844 3845 3846 3847 3848 3849 3850
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
3851
        table_ident cache_keys_spec
unknown's avatar
unknown committed
3852 3853 3854 3855 3856 3857
        {
          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(),
3858
                                      (List<String> *)0))
unknown's avatar
unknown committed
3859 3860 3861 3862
            YYABORT;
        }
        ;

3863 3864 3865
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
3866
	;
3867

unknown's avatar
unknown committed
3868
preload:
unknown's avatar
unknown committed
3869
	LOAD INDEX_SYM INTO CACHE_SYM
unknown's avatar
unknown committed
3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882
	{
	  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
3883
	table_ident cache_keys_spec opt_ignore_leaves
unknown's avatar
unknown committed
3884 3885 3886
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
unknown's avatar
unknown committed
3887 3888
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
unknown's avatar
unknown committed
3889 3890 3891 3892 3893 3894
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

3895
cache_keys_spec:
3896
        { Select->interval_list.empty(); }
3897 3898 3899 3900 3901 3902 3903
        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
3904

unknown's avatar
unknown committed
3905
cache_key_list_or_empty:
3906
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
3907
	| opt_key_or_index '(' key_usage_list2 ')'
3908 3909 3910 3911
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
unknown's avatar
unknown committed
3912 3913
	;

unknown's avatar
unknown committed
3914
opt_ignore_leaves:
unknown's avatar
unknown committed
3915 3916 3917 3918 3919
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

unknown's avatar
unknown committed
3920
/*
unknown's avatar
unknown committed
3921
  Select : retrieve data from table
unknown's avatar
unknown committed
3922 3923 3924 3925
*/


select:
3926 3927 3928 3929
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
unknown's avatar
unknown committed
3930 3931
	}
	;
unknown's avatar
unknown committed
3932

3933
/* Need select_init2 for subselects. */
unknown's avatar
unknown committed
3934
select_init:
3935
	SELECT_SYM select_init2
3936
	|
unknown's avatar
unknown committed
3937 3938 3939 3940
	'(' select_paren ')' union_opt;

select_paren:
	SELECT_SYM select_part2
3941
	  {
3942
	    LEX *lex= Lex;
unknown's avatar
unknown committed
3943
            SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
3944
	    if (sel->set_braces(1))
3945
	    {
3946
	      yyerror(ER(ER_SYNTAX_ERROR));
3947 3948
	      YYABORT;
	    }
3949 3950
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
3951
	  {
3952
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
3953 3954
	    YYABORT;
	  }
3955
            /* select in braces, can't contain global parameters */
unknown's avatar
unknown committed
3956 3957 3958
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
unknown's avatar
unknown committed
3959 3960
          }
	| '(' select_paren ')';
3961

3962 3963
select_init2:
	select_part2
3964
        {
3965
	  LEX *lex= Lex;
unknown's avatar
unknown committed
3966
          SELECT_LEX * sel= lex->current_select;
unknown's avatar
unknown committed
3967
          if (lex->current_select->set_braces(0))
3968
	  {
3969
	    yyerror(ER(ER_SYNTAX_ERROR));
3970 3971
	    YYABORT;
	  }
3972 3973
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
unknown's avatar
unknown committed
3974
	  {
3975
	    yyerror(ER(ER_SYNTAX_ERROR));
unknown's avatar
unknown committed
3976 3977
	    YYABORT;
	  }
3978
	}
3979
	union_clause
3980 3981
	;

3982
select_part2:
unknown's avatar
unknown committed
3983
	{
unknown's avatar
unknown committed
3984 3985
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
3986 3987
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
3988
	  lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
3989 3990 3991
	}
	select_options select_item_list
	{
3992
	  Select->parsing_place= NO_MATTER;
unknown's avatar
unknown committed
3993
	}
unknown's avatar
unknown committed
3994
	select_into select_lock_type;
3995

unknown's avatar
unknown committed
3996
select_into:
unknown's avatar
unknown committed
3997
	opt_order_clause opt_limit_clause {}
3998
        | into
unknown's avatar
unknown committed
3999
	| select_from
4000 4001
	| into select_from
	| select_from into;
unknown's avatar
unknown committed
4002 4003

select_from:
4004 4005
	  FROM join_table_list where_clause group_clause having_clause
	       opt_order_clause opt_limit_clause procedure_clause
unknown's avatar
unknown committed
4006
        | FROM DUAL_SYM where_clause opt_limit_clause
unknown's avatar
unknown committed
4007 4008 4009 4010
          /* 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 :) */
4011
	;
unknown's avatar
unknown committed
4012 4013 4014

select_options:
	/* empty*/
unknown's avatar
unknown committed
4015 4016 4017 4018
	| select_option_list
	  {
	    if (test_all_bits(Select->options, SELECT_ALL | SELECT_DISTINCT))
	    {
unknown's avatar
unknown committed
4019
	      my_error(ER_WRONG_USAGE, MYF(0), "ALL", "DISTINCT");
unknown's avatar
unknown committed
4020 4021 4022
              YYABORT;
	    }
          }
unknown's avatar
unknown committed
4023
	  ;
unknown's avatar
unknown committed
4024 4025 4026

select_option_list:
	select_option_list select_option
unknown's avatar
unknown committed
4027
	| select_option;
unknown's avatar
unknown committed
4028 4029

select_option:
4030
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
4031 4032 4033 4034 4035 4036
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
unknown's avatar
unknown committed
4037
	| DISTINCT         { Select->options|= SELECT_DISTINCT; }
4038 4039
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051
	| 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;
	  }
4052
	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
4053 4054 4055 4056
	| SQL_CACHE_SYM
	  {
	    Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
	  }
unknown's avatar
unknown committed
4057
	| ALL		    { Select->options|= SELECT_ALL; }
4058
	;
unknown's avatar
unknown committed
4059

unknown's avatar
unknown committed
4060 4061 4062
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
4063 4064
	  {
	    LEX *lex=Lex;
4065
	    lex->current_select->set_lock_for_tables(TL_WRITE);
4066
	    lex->safe_to_cache_query=0;
4067
	  }
unknown's avatar
unknown committed
4068
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
4069 4070
	  {
	    LEX *lex=Lex;
4071 4072
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
4073
	    lex->safe_to_cache_query=0;
4074 4075
	  }
	;
unknown's avatar
unknown committed
4076

unknown's avatar
unknown committed
4077 4078 4079 4080 4081
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
4082
	    THD *thd= YYTHD;
4083 4084 4085 4086
	    if (add_item_to_list(thd,
                                 new Item_field(&thd->lex->current_select->
                                                context,
                                                NULL, NULL, "*")))
unknown's avatar
unknown committed
4087
	      YYABORT;
4088
	    (thd->lex->current_select->with_wild)++;
unknown's avatar
unknown committed
4089
	  };
unknown's avatar
unknown committed
4090 4091 4092 4093 4094


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
unknown's avatar
unknown committed
4095
	    if (add_item_to_list(YYTHD, $2))
unknown's avatar
unknown committed
4096 4097
	      YYABORT;
	    if ($4.str)
4098 4099 4100 4101
            {
	      $2->set_name($4.str, $4.length, system_charset_info);
              $2->is_autogenerated_name= FALSE;
            }
4102 4103 4104 4105 4106 4107
	    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
4108
	  };
unknown's avatar
unknown committed
4109 4110

remember_name:
unknown's avatar
unknown committed
4111
	{ $$=(char*) Lex->tok_start; };
unknown's avatar
unknown committed
4112 4113

remember_end:
unknown's avatar
unknown committed
4114
	{ $$=(char*) Lex->tok_end; };
unknown's avatar
unknown committed
4115 4116 4117

select_item2:
	table_wild	{ $$=$1; } /* table.* */
unknown's avatar
unknown committed
4118
	| expr		{ $$=$1; };
unknown's avatar
unknown committed
4119 4120

select_alias:
4121
	/* empty */		{ $$=null_lex_str;}
unknown's avatar
unknown committed
4122 4123 4124 4125 4126
	| AS ident		{ $$=$2; }
	| AS TEXT_STRING_sys	{ $$=$2; }
	| ident			{ $$=$1; }
	| TEXT_STRING_sys	{ $$=$1; }
	;
unknown's avatar
unknown committed
4127 4128 4129

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

unknown's avatar
unknown committed
4132
/* all possible expressions */
unknown's avatar
SCRUM  
unknown committed
4133
expr:	
unknown's avatar
unknown committed
4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155
	  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); }
        ;
4156 4157

bool_term:
unknown's avatar
unknown committed
4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180
	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); }
        ;
4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197

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); }
4198 4199 4200 4201 4202
	| 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); }
4203 4204 4205 4206
	| predicate ;

predicate:
	 bit_expr IN_SYM '(' expr_list ')'
unknown's avatar
unknown committed
4207 4208 4209 4210 4211 4212 4213 4214 4215
	  { 
            if ($4->elements == 1)
              $$= new Item_func_eq($1, $4->head());
            else
            {
              $4->push_front($1);
              $$= new Item_func_in(*$4);
            }
          }
4216
	| bit_expr not IN_SYM '(' expr_list ')'
unknown's avatar
unknown committed
4217 4218 4219 4220 4221 4222
          {
            if ($5->elements == 1)
              $$= new Item_func_ne($1, $5->head());
            else
            {
              $5->push_front($1);
4223 4224 4225
              Item_func_in *item = new Item_func_in(*$5);
              item->negate();
              $$= item;
unknown's avatar
unknown committed
4226 4227
            }            
          }
4228 4229 4230
        | bit_expr IN_SYM in_subselect
	  { $$= new Item_in_subselect($1, $3); }
	| bit_expr not IN_SYM in_subselect
4231
          { $$= negate_expression(YYTHD, new Item_in_subselect($1, $4)); }
4232 4233 4234
	| 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
4235 4236 4237 4238 4239
    {
      Item_func_between *item= new Item_func_between($1,$4,$6);
      item->negate();
      $$= item;
    }
4240 4241 4242 4243
	| 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
4244
          { $$= new Item_func_like($1,$3,$4,Lex->escape_used); }
4245
	| bit_expr not LIKE simple_expr opt_escape
4246
          { $$= new Item_func_not(new Item_func_like($1,$4,$5, Lex->escape_used)); }
4247 4248
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
4249
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291
	| 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
4292

unknown's avatar
unknown committed
4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304
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; }
        ;

4305
interval_expr:
unknown's avatar
unknown committed
4306
         INTERVAL_SYM expr { $$=$2; }
4307 4308
        ;

unknown's avatar
unknown committed
4309 4310
simple_expr:
	simple_ident
4311
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
unknown's avatar
unknown committed
4312
	  {
unknown's avatar
unknown committed
4313 4314 4315
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
4316
                                                            YYTHD->charset()));
4317
	  }
unknown's avatar
unknown committed
4318
	| literal
unknown's avatar
unknown committed
4319
	| param_marker
unknown's avatar
unknown committed
4320
	| '@' ident_or_text SET_VAR expr
unknown's avatar
unknown committed
4321 4322
	  {
	    $$= new Item_func_set_user_var($2,$4);
4323 4324 4325
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4326
	  }
4327
	| '@' ident_or_text
unknown's avatar
unknown committed
4328 4329
	  {
	    $$= new Item_func_get_user_var($2);
4330 4331 4332
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
unknown's avatar
unknown committed
4333
	  }
4334
	| '@' '@' opt_var_ident_type ident_or_text opt_component
unknown's avatar
unknown committed
4335
	  {
4336 4337 4338 4339 4340 4341

            if ($4.str && $5.str && check_reserved_words(&$4))
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
4342
	    if (!($$= get_system_var(YYTHD, $3, $4, $5)))
unknown's avatar
unknown committed
4343
	      YYABORT;
4344
	    Lex->variables_used= 1;
unknown's avatar
unknown committed
4345
	  }
unknown's avatar
unknown committed
4346
	| sum_expr
4347 4348 4349 4350 4351 4352
	| 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
4353
	| '(' expr ')'		{ $$= $2; }
4354 4355 4356 4357 4358
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
4359
	| ROW_SYM '(' expr ',' expr_list ')'
unknown's avatar
unknown committed
4360
	  {
4361 4362
	    $5->push_front($3);
	    $$= new Item_row(*$5);
unknown's avatar
unknown committed
4363
	  }
unknown's avatar
unknown committed
4364
	| EXISTS exists_subselect { $$= $2; }
unknown's avatar
unknown committed
4365
	| singlerow_subselect   { $$= $1; }
unknown's avatar
unknown committed
4366
	| '{' ident expr '}'	{ $$= $3; }
4367
        | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
4368
          { $2->push_front($5);
4369 4370
            Select->add_ftfunc_to_list((Item_func_match*)
                                        ($$=new Item_func_match(*$2,$6))); }
unknown's avatar
unknown committed
4371
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
4372
	| BINARY simple_expr %prec NEG
unknown's avatar
unknown committed
4373
	  {
unknown's avatar
unknown committed
4374
            $$= create_func_cast($2, ITEM_CAST_CHAR, -1, 0, &my_charset_bin);
unknown's avatar
unknown committed
4375
	  }
4376
	| CAST_SYM '(' expr AS cast_type ')'
4377
	  {
unknown's avatar
unknown committed
4378
            LEX *lex= Lex;
4379
	    $$= create_func_cast($3, $5,
unknown's avatar
unknown committed
4380 4381 4382
                                 lex->length ? atoi(lex->length) : -1,
                                 lex->dec ? atoi(lex->dec) : 0,
                                 lex->charset);
4383
	  }
unknown's avatar
unknown committed
4384
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
4385
	  { $$= new Item_func_case(* $4, $2, $5 ); }
4386
	| CONVERT_SYM '(' expr ',' cast_type ')'
4387 4388 4389
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
unknown's avatar
unknown committed
4390
                                 Lex->dec ? atoi(Lex->dec) : 0,
4391 4392
				 Lex->charset);
	  }
4393 4394
	| CONVERT_SYM '(' expr USING charset_name ')'
	  { $$= new Item_func_conv_charset($3,$5); }
unknown's avatar
SCRUM  
unknown committed
4395
	| DEFAULT '(' simple_ident ')'
4396 4397 4398
	  {
	    if ($3->is_splocal())
	    {
unknown's avatar
unknown committed
4399
	      LEX_STRING *name;
4400 4401
	      Item_splocal *il= static_cast<Item_splocal *>($3);

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

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

4735
            if (udf)
4736
            {
4737 4738 4739
              if (udf->type == UDFTYPE_AGGREGATE)
                Select->in_sum_expr--;

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

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

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

4925 4926 4927 4928 4929 4930
fulltext_options:
        /* nothing */                   { $$= FT_NL;  }
        | WITH QUERY_SYM EXPANSION_SYM  { $$= FT_NL | FT_EXPAND; }
        | IN_SYM BOOLEAN_SYM MODE_SYM   { $$= FT_BOOL; }
        ;

unknown's avatar
unknown committed
4931
udf_expr_list:
4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956
	/* empty */	 { $$= NULL; }
	| udf_expr_list2 { $$= $1;}
	;

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

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

udf_expr:
	remember_name expr remember_end select_alias
	{
	  if ($4.str)
4957 4958 4959 4960
          {
	    $2->set_name($4.str, $4.length, system_charset_info);
            $2->is_autogenerated_name= FALSE;
          }
4961
	  else
4962
	    $2->set_name($1, (uint) ($3 - $1), YYTHD->charset());
4963 4964 4965
	  $$= $2;
	}
	;
unknown's avatar
unknown committed
4966 4967 4968 4969

sum_expr:
	AVG_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_avg($3); }
4970 4971
	| AVG_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_avg_distinct($4); }
unknown's avatar
unknown committed
4972 4973 4974 4975
	| BIT_AND  '(' in_sum_expr ')'
	  { $$=new Item_sum_and($3); }
	| BIT_OR  '(' in_sum_expr ')'
	  { $$=new Item_sum_or($3); }
4976 4977
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
unknown's avatar
unknown committed
4978
	| COUNT_SYM '(' opt_all '*' ')'
unknown's avatar
unknown committed
4979 4980 4981
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
4982
	| COUNT_SYM '(' DISTINCT
unknown's avatar
unknown committed
4983
	  { Select->in_sum_expr++; }
4984
	   expr_list
unknown's avatar
unknown committed
4985
	  { Select->in_sum_expr--; }
4986 4987
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
unknown's avatar
unknown committed
4988 4989 4990 4991
	| GROUP_UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' in_sum_expr ')'
	  { $$= new Item_sum_unique_users($3,atoi($5.str),atoi($7.str),$9); }
	| MIN_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_min($3); }
4992 4993 4994 4995 4996 4997 4998
/*
   According to ANSI SQL, DISTINCT is allowed and has
   no sence inside MIN and MAX grouping functions; so MIN|MAX(DISTINCT ...)
   is processed like an ordinary MIN | MAX()
 */
	| MIN_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_min($4); }
unknown's avatar
unknown committed
4999 5000
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
5001 5002
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
unknown's avatar
unknown committed
5003
	| STD_SYM '(' in_sum_expr ')'
5004
	  { $$=new Item_sum_std($3, 0); }
unknown's avatar
unknown committed
5005
	| VARIANCE_SYM '(' in_sum_expr ')'
5006 5007 5008 5009 5010
	  { $$=new Item_sum_variance($3, 0); }
	| STDDEV_SAMP_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_std($3, 1); }
	| VAR_SAMP_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_variance($3, 1); }
unknown's avatar
unknown committed
5011
	| SUM_SYM '(' in_sum_expr ')'
5012
	  { $$=new Item_sum_sum($3); }
5013 5014
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
unknown's avatar
unknown committed
5015 5016 5017 5018 5019
	| GROUP_CONCAT_SYM '(' opt_distinct
	  { Select->in_sum_expr++; }
	  expr_list opt_gorder_clause
	  opt_gconcat_separator
	 ')'
unknown's avatar
unknown committed
5020
	  {
5021 5022
            SELECT_LEX *sel= Select;
	    sel->in_sum_expr--;
unknown's avatar
unknown committed
5023
	    $$=new Item_func_group_concat(Lex->current_context(), $3, $5,
5024
                                          sel->gorder_list, $7);
unknown's avatar
unknown committed
5025
	    $5->empty();
5026 5027 5028 5029 5030 5031 5032
	  };

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

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

5036 5037

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

unknown's avatar
unknown committed
5051 5052

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

5068
cast_type:
unknown's avatar
unknown committed
5069 5070
        BINARY opt_len		{ $$=ITEM_CAST_CHAR; Lex->charset= &my_charset_bin; Lex->dec= 0; }
        | CHAR_SYM opt_len opt_binary	{ $$=ITEM_CAST_CHAR; Lex->dec= 0; }
5071
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; Lex->dec=0; }
unknown's avatar
unknown committed
5072 5073 5074 5075 5076 5077 5078 5079
        | SIGNED_SYM		{ $$=ITEM_CAST_SIGNED_INT; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | SIGNED_SYM INT_SYM	{ $$=ITEM_CAST_SIGNED_INT; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | UNSIGNED		{ $$=ITEM_CAST_UNSIGNED_INT; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | UNSIGNED INT_SYM	{ $$=ITEM_CAST_UNSIGNED_INT; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | DATE_SYM		{ $$=ITEM_CAST_DATE; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | TIME_SYM		{ $$=ITEM_CAST_TIME; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | DATETIME		{ $$=ITEM_CAST_DATETIME; Lex->charset= NULL; Lex->dec=Lex->length= (char*)0; }
        | DECIMAL_SYM float_options { $$=ITEM_CAST_DECIMAL; Lex->charset= NULL; }
5080 5081
	;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

unknown's avatar
unknown committed
5315 5316 5317 5318 5319 5320 5321 5322 5323
          }
          expr '}'
	  {
            YYERROR_UNLESS($3 && $7);
            add_join_on($7,$10);
            Lex->pop_context();
            $7->outer_join|=JOIN_TYPE_LEFT;
            $$=$7;
          }
unknown's avatar
unknown committed
5324
	| select_derived_init get_select_lex select_derived2
5325
          {
5326
            LEX *lex= Lex;
unknown's avatar
unknown committed
5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340
            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
5341
              YYABORT;
unknown's avatar
unknown committed
5342
            $$= 0;
unknown's avatar
unknown committed
5343
            /* incomplete derived tables return NULL, we must be
unknown's avatar
unknown committed
5344
               nested in select_derived rule to be here. */
5345
          }
unknown's avatar
unknown committed
5346 5347 5348 5349 5350
	| '(' 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
5351
          if (!($3 || $6) && $2->embedding &&
unknown's avatar
unknown committed
5352
              !$2->embedding->nested_join->join_list.elements)
5353
          {
unknown's avatar
unknown committed
5354 5355 5356 5357 5358
            /* 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;
5359
          }
unknown's avatar
unknown committed
5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373
          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
5374

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

unknown's avatar
unknown committed
5391
/* handle contents of parentheses in join expression */
5392
select_derived:
unknown's avatar
unknown committed
5393
	  get_select_lex
unknown's avatar
unknown committed
5394
	  {
unknown's avatar
unknown committed
5395 5396 5397 5398 5399 5400 5401 5402 5403
            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
5404

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


/*
unknown's avatar
unknown committed
5629
   group by statement in select
unknown's avatar
unknown committed
5630 5631 5632 5633
*/

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

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

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

unknown's avatar
unknown committed
5670
/*
unknown's avatar
unknown committed
5671
   Order by statement in select
unknown's avatar
unknown committed
5672 5673
*/

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

order_clause:
5679 5680
	ORDER_SYM BY
        {
unknown's avatar
unknown committed
5681
	  LEX *lex=Lex;
5682
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
unknown's avatar
unknown committed
5683
	      lex->current_select->olap !=
5684
	      UNSPECIFIED_OLAP_TYPE)
5685
	  {
unknown's avatar
unknown committed
5686 5687
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
5688 5689
	    YYABORT;
	  }
unknown's avatar
unknown committed
5690
	} order_list;
unknown's avatar
unknown committed
5691 5692 5693

order_list:
	order_list ',' order_ident order_dir
unknown's avatar
unknown committed
5694
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
unknown's avatar
unknown committed
5695
	| order_ident order_dir
unknown's avatar
unknown committed
5696
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
unknown's avatar
unknown committed
5697 5698 5699

order_dir:
	/* empty */ { $$ =  1; }
unknown's avatar
unknown committed
5700
	| ASC  { $$ =1; }
unknown's avatar
unknown committed
5701
	| DESC { $$ =0; };
unknown's avatar
unknown committed
5702 5703


5704 5705 5706
opt_limit_clause_init:
	/* empty */
	{
5707 5708
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
5709 5710
          sel->offset_limit= 0;
          sel->select_limit= 0;
5711
	}
5712 5713 5714
	| limit_clause {}
	;

5715 5716 5717 5718 5719
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

5720
limit_clause:
5721
	LIMIT limit_options {}
unknown's avatar
unknown committed
5722
	;
unknown's avatar
unknown committed
5723 5724

limit_options:
5725
	limit_option
unknown's avatar
unknown committed
5726
	  {
unknown's avatar
unknown committed
5727
            SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5728
            sel->select_limit= $1;
5729
            sel->offset_limit= 0;
5730
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5731
	  }
5732
	| limit_option ',' limit_option
unknown's avatar
unknown committed
5733
	  {
unknown's avatar
unknown committed
5734
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5735 5736
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
5737
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5738
	  }
5739
	| limit_option OFFSET_SYM limit_option
unknown's avatar
unknown committed
5740
	  {
unknown's avatar
unknown committed
5741
	    SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
5742 5743
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
5744
	    sel->explicit_limit= 1;
unknown's avatar
unknown committed
5745 5746
	  }
	;
5747 5748 5749 5750 5751
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
5752
        ;
unknown's avatar
unknown committed
5753 5754 5755 5756

delete_limit_clause:
	/* empty */
	{
unknown's avatar
unknown committed
5757
	  LEX *lex=Lex;
5758
	  lex->current_select->select_limit= 0;
unknown's avatar
unknown committed
5759
	}
5760
	| LIMIT limit_option
5761 5762
	{
	  SELECT_LEX *sel= Select;
5763
	  sel->select_limit= $2;
5764 5765
	  sel->explicit_limit= 1;
	};
unknown's avatar
unknown committed
5766

unknown's avatar
unknown committed
5767 5768
ulong_num:
          NUM           { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
5769
	| HEX_NUM       { $$= (ulong) strtol($1.str, (char**) 0, 16); }
unknown's avatar
unknown committed
5770 5771
	| 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
5772
        | DECIMAL_NUM   { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
5773 5774
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
5775

unknown's avatar
unknown committed
5776
ulonglong_num:
unknown's avatar
unknown committed
5777 5778 5779
	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
5780
        | DECIMAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
unknown's avatar
unknown committed
5781 5782
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
unknown's avatar
unknown committed
5783 5784 5785 5786 5787 5788

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
5789 5790
	    if (&lex->select_lex != lex->current_select)
	    {
unknown's avatar
unknown committed
5791
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
5792 5793
	      YYABORT;
	    }
unknown's avatar
unknown committed
5794 5795 5796
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
5797 5798 5799 5800
	    if (add_proc_to_list(lex->thd, new Item_field(&lex->
                                                          current_select->
                                                          context,
                                                          NULL,NULL,$2.str)))
unknown's avatar
unknown committed
5801
	      YYABORT;
5802
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
5803
	  }
unknown's avatar
unknown committed
5804
	  '(' procedure_list ')';
unknown's avatar
unknown committed
5805 5806 5807 5808


procedure_list:
	/* empty */ {}
unknown's avatar
unknown committed
5809
	| procedure_list2 {};
unknown's avatar
unknown committed
5810 5811 5812

procedure_list2:
	procedure_list2 ',' procedure_item
unknown's avatar
unknown committed
5813
	| procedure_item;
unknown's avatar
unknown committed
5814 5815 5816 5817

procedure_item:
	  remember_name expr
	  {
unknown's avatar
unknown committed
5818 5819
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
unknown's avatar
unknown committed
5820 5821
	      YYABORT;
	    if (!$2->name)
5822 5823
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1),
                           YYTHD->charset());
5824 5825 5826 5827 5828 5829
	  }
          ;


select_var_list_init:
	   {
unknown's avatar
unknown committed
5830 5831
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
5832 5833
	        YYABORT;
	   }
5834
	   select_var_list
5835
	   {}
5836
           ;
unknown's avatar
unknown committed
5837

unknown's avatar
unknown committed
5838
select_var_list:
5839 5840 5841 5842
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

5843 5844 5845 5846 5847 5848 5849 5850 5851 5852
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
5853
           {
5854
             LEX *lex=Lex;
5855 5856 5857 5858
	     sp_pvar_t *t;

	     if (!lex->spcont || !(t=lex->spcont->find_pvar(&$1)))
	     {
5859
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
unknown's avatar
unknown committed
5860
	       YYABORT;
5861 5862 5863 5864 5865
	     }
	     if (! lex->result)
	       YYABORT;
	     else
	     {
5866 5867 5868
               my_var *var;
	       ((select_dumpvar *)lex->result)->
                 var_list.push_back(var= new my_var($1,1,t->offset,t->type));
unknown's avatar
unknown committed
5869
#ifndef DBUG_OFF
5870 5871 5872
	       if (var)
		 var->owner= lex->sphead;
#endif
5873
	     }
unknown's avatar
unknown committed
5874
	   }
unknown's avatar
unknown committed
5875
           ;
unknown's avatar
unknown committed
5876

5877
into:
unknown's avatar
unknown committed
5878
        INTO OUTFILE TEXT_STRING_sys
unknown's avatar
unknown committed
5879
	{
unknown's avatar
unknown committed
5880
          LEX *lex= Lex;
5881 5882 5883 5884
          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
5885 5886
	}
	opt_field_term opt_line_term
unknown's avatar
unknown committed
5887
	| INTO DUMPFILE TEXT_STRING_sys
unknown's avatar
unknown committed
5888
	{
5889
	  LEX *lex=Lex;
unknown's avatar
unknown committed
5890 5891
	  if (!lex->describe)
	  {
5892
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
unknown's avatar
unknown committed
5893 5894 5895 5896 5897
	    if (!(lex->exchange= new sql_exchange($3.str,1)))
	      YYABORT;
	    if (!(lex->result= new select_dump(lex->exchange)))
	      YYABORT;
	  }
unknown's avatar
unknown committed
5898
	}
5899
        | INTO select_var_list_init
unknown's avatar
unknown committed
5900
	{
5901
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5902 5903
	}
        ;
unknown's avatar
unknown committed
5904

unknown's avatar
unknown committed
5905 5906 5907
/*
  DO statement
*/
unknown's avatar
unknown committed
5908

5909
do:	DO_SYM
unknown's avatar
unknown committed
5910 5911 5912
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
5913 5914 5915 5916 5917
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
unknown's avatar
unknown committed
5918
	}
5919 5920
	;

unknown's avatar
unknown committed
5921
/*
5922
  Drop : delete tables or index or user
unknown's avatar
unknown committed
5923 5924 5925
*/

drop:
unknown's avatar
unknown committed
5926
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
unknown's avatar
unknown committed
5927
	{
unknown's avatar
unknown committed
5928 5929
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
5930 5931
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
unknown's avatar
unknown committed
5932
	}
unknown's avatar
unknown committed
5933
	| DROP INDEX_SYM ident ON table_ident {}
unknown's avatar
unknown committed
5934
	  {
unknown's avatar
unknown committed
5935 5936
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
5937 5938 5939
	     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
5940 5941
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
unknown's avatar
unknown committed
5942 5943 5944 5945
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
unknown's avatar
unknown committed
5946 5947 5948 5949
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
	    lex->name=$4.str;
unknown's avatar
unknown committed
5950
	 }
5951
	| DROP FUNCTION_SYM if_exists sp_name
unknown's avatar
unknown committed
5952
	  {
unknown's avatar
unknown committed
5953
	    LEX *lex=Lex;
5954 5955
	    if (lex->sphead)
	    {
unknown's avatar
unknown committed
5956
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
5957 5958
	      YYABORT;
	    }
unknown's avatar
unknown committed
5959
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
5960 5961 5962 5963 5964 5965 5966 5967
	    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
5968
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
5969 5970 5971 5972 5973
	      YYABORT;
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
5974
	  }
5975
	| DROP USER clear_privileges user_list
5976
	  {
5977 5978
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
5979 5980
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
5981
	    LEX *lex= Lex;
5982 5983 5984
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
5985
        | DROP TRIGGER_SYM sp_name
5986 5987 5988
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_DROP_TRIGGER;
5989
            lex->spname= $3;
5990 5991
          }
	;
unknown's avatar
unknown committed
5992 5993

table_list:
5994
	table_name
unknown's avatar
unknown committed
5995
	| table_list ',' table_name;
unknown's avatar
unknown committed
5996

5997
table_name:
unknown's avatar
unknown committed
5998
	table_ident
unknown's avatar
unknown committed
5999 6000 6001 6002 6003
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
unknown's avatar
unknown committed
6004 6005

if_exists:
6006
	/* empty */ { $$= 0; }
6007 6008
	| IF EXISTS { $$= 1; }
	;
unknown's avatar
unknown committed
6009

6010 6011 6012 6013
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
unknown's avatar
unknown committed
6014 6015 6016 6017 6018
/*
** Insert : add new data to table
*/

insert:
6019 6020 6021
	INSERT
	{
	  LEX *lex= Lex;
6022 6023
	  lex->sql_command= SQLCOM_INSERT;
	  lex->duplicates= DUP_ERROR; 
unknown's avatar
unknown committed
6024
	  mysql_init_select(lex);
6025 6026 6027
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
	} insert_lock_option
6028
	opt_ignore insert2
unknown's avatar
unknown committed
6029
	{
unknown's avatar
unknown committed
6030
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
6031
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
6032
	}
6033
	insert_field_spec opt_insert_update
6034
	{}
unknown's avatar
unknown committed
6035
	;
unknown's avatar
unknown committed
6036 6037

replace:
6038 6039
	REPLACE
	{
6040
	  LEX *lex=Lex;
6041 6042
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
unknown's avatar
unknown committed
6043
	  mysql_init_select(lex);
6044
	}
unknown's avatar
unknown committed
6045 6046
	replace_lock_option insert2
	{
unknown's avatar
unknown committed
6047
	  Select->set_lock_for_tables($3);
unknown's avatar
unknown committed
6048
	  Lex->current_select= &Lex->select_lex;
unknown's avatar
unknown committed
6049 6050
	}
	insert_field_spec
6051
	{}
unknown's avatar
unknown committed
6052
	;
unknown's avatar
unknown committed
6053 6054

insert_lock_option:
unknown's avatar
unknown committed
6055 6056 6057 6058
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
6059
	;
unknown's avatar
unknown committed
6060 6061

replace_lock_option:
unknown's avatar
unknown committed
6062 6063
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
unknown's avatar
unknown committed
6064 6065 6066

insert2:
	INTO insert_table {}
unknown's avatar
unknown committed
6067
	| insert_table {};
unknown's avatar
unknown committed
6068 6069

insert_table:
6070
	table_name
unknown's avatar
unknown committed
6071
	{
unknown's avatar
unknown committed
6072 6073 6074 6075
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
unknown's avatar
unknown committed
6076
	};
unknown's avatar
unknown committed
6077 6078

insert_field_spec:
unknown's avatar
unknown committed
6079 6080 6081
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
unknown's avatar
unknown committed
6082 6083
	| SET
	  {
unknown's avatar
unknown committed
6084 6085 6086
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
6087 6088
	      YYABORT;
	   }
unknown's avatar
unknown committed
6089
	   ident_eq_list;
unknown's avatar
unknown committed
6090 6091 6092

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
unknown's avatar
unknown committed
6093
	| insert_ident		{ Lex->field_list.push_back($1); };
unknown's avatar
unknown committed
6094 6095 6096

insert_values:
	VALUES	values_list  {}
unknown's avatar
unknown committed
6097
	| VALUE_SYM values_list  {}
unknown's avatar
unknown committed
6098 6099
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
6100
        ;
unknown's avatar
unknown committed
6101 6102 6103

values_list:
	values_list ','  no_braces
unknown's avatar
unknown committed
6104
	| no_braces;
unknown's avatar
unknown committed
6105 6106 6107 6108

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
unknown's avatar
unknown committed
6109
	ident_eq_value;
unknown's avatar
unknown committed
6110 6111

ident_eq_value:
6112
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
6113
	 {
unknown's avatar
unknown committed
6114 6115 6116
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
unknown's avatar
unknown committed
6117
	    YYABORT;
unknown's avatar
unknown committed
6118
	 };
unknown's avatar
unknown committed
6119 6120

equal:	EQ		{}
unknown's avatar
unknown committed
6121 6122 6123 6124 6125 6126 6127
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
unknown's avatar
unknown committed
6128 6129 6130 6131 6132 6133 6134 6135 6136

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
unknown's avatar
unknown committed
6137 6138
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
unknown's avatar
unknown committed
6139
	    YYABORT;
unknown's avatar
unknown committed
6140
	 };
unknown's avatar
unknown committed
6141 6142 6143

opt_values:
	/* empty */ {}
unknown's avatar
unknown committed
6144
	| values;
unknown's avatar
unknown committed
6145 6146

values:
unknown's avatar
unknown committed
6147
	values ','  expr_or_default
unknown's avatar
unknown committed
6148 6149 6150 6151
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
unknown's avatar
unknown committed
6152 6153 6154 6155 6156 6157 6158 6159
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
6160
	expr	  { $$= $1;}
unknown's avatar
unknown committed
6161
	| DEFAULT {$$= new Item_default_value(Lex->current_context()); }
unknown's avatar
unknown committed
6162
	;
unknown's avatar
unknown committed
6163

6164 6165
opt_insert_update:
        /* empty */
unknown's avatar
unknown committed
6166
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
unknown's avatar
unknown committed
6167
          KEY_SYM UPDATE_SYM insert_update_list
6168 6169
        ;

unknown's avatar
unknown committed
6170 6171 6172
/* Update rows in a table */

update:
6173 6174
	UPDATE_SYM
	{
6175
	  LEX *lex= Lex;
unknown's avatar
unknown committed
6176
	  mysql_init_select(lex);
6177
          lex->sql_command= SQLCOM_UPDATE;
unknown's avatar
unknown committed
6178
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
6179
	  lex->duplicates= DUP_ERROR; 
6180
        }
unknown's avatar
unknown committed
6181
        opt_low_priority opt_ignore join_table_list
unknown's avatar
unknown committed
6182
	SET update_list
unknown's avatar
unknown committed
6183
	{
6184 6185
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
unknown's avatar
unknown committed
6186
            lex->sql_command= SQLCOM_UPDATE_MULTI;
6187 6188 6189
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
6190 6191
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
6192 6193
	    YYABORT;
	  }
6194 6195 6196 6197 6198 6199
          /*
            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
6200
	}
6201
	where_clause opt_order_clause delete_limit_clause {}
unknown's avatar
unknown committed
6202
	;
unknown's avatar
unknown committed
6203 6204

update_list:
unknown's avatar
unknown committed
6205 6206 6207 6208
	update_list ',' update_elem
	| update_elem;

update_elem:
unknown's avatar
unknown committed
6209
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
6210
	{
unknown's avatar
unknown committed
6211
	  if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
unknown's avatar
unknown committed
6212
	    YYABORT;
unknown's avatar
unknown committed
6213 6214 6215 6216 6217 6218 6219
	};

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
unknown's avatar
unknown committed
6220
	simple_ident_nospvar equal expr_or_default
unknown's avatar
unknown committed
6221
	  {
unknown's avatar
unknown committed
6222 6223 6224
	  LEX *lex= Lex;
	  if (lex->update_list.push_back($1) || 
	      lex->value_list.push_back($3))
unknown's avatar
unknown committed
6225
	      YYABORT;
unknown's avatar
unknown committed
6226
	  };
unknown's avatar
unknown committed
6227 6228

opt_low_priority:
6229
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
6230
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
6231 6232 6233 6234

/* Delete rows from a table */

delete:
6235
	DELETE_SYM
6236
	{
6237 6238
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
unknown's avatar
unknown committed
6239
	  mysql_init_select(lex);
unknown's avatar
unknown committed
6240
	  lex->lock_option= lex->thd->update_lock_default;
6241
	  lex->ignore= 0;
6242
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
6243
	}
unknown's avatar
unknown committed
6244 6245
	opt_delete_options single_multi {}
	;
6246 6247

single_multi:
unknown's avatar
unknown committed
6248 6249
 	FROM table_ident
	{
unknown's avatar
unknown committed
6250 6251
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
unknown's avatar
unknown committed
6252 6253 6254
	    YYABORT;
	}
	where_clause opt_order_clause
6255
	delete_limit_clause {}
unknown's avatar
unknown committed
6256
	| table_wild_list
unknown's avatar
unknown committed
6257
	  { mysql_init_multi_delete(Lex); }
unknown's avatar
unknown committed
6258
          FROM join_table_list where_clause
6259 6260 6261 6262
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
unknown's avatar
unknown committed
6263 6264
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
6265
	  USING join_table_list where_clause
6266 6267 6268 6269
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
6270
	;
unknown's avatar
unknown committed
6271 6272 6273

table_wild_list:
	  table_wild_one {}
unknown's avatar
unknown committed
6274
	  | table_wild_list ',' table_wild_one {};
unknown's avatar
unknown committed
6275 6276

table_wild_one:
unknown's avatar
unknown committed
6277
	ident opt_wild opt_table_alias
unknown's avatar
unknown committed
6278
	{
unknown's avatar
unknown committed
6279 6280
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
					 TL_OPTION_UPDATING, Lex->lock_option))
unknown's avatar
unknown committed
6281 6282
	    YYABORT;
        }
unknown's avatar
unknown committed
6283
	| ident '.' ident opt_wild opt_table_alias
unknown's avatar
unknown committed
6284
	  {
unknown's avatar
unknown committed
6285 6286
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
unknown's avatar
unknown committed
6287 6288
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
unknown's avatar
unknown committed
6289
	      YYABORT;
unknown's avatar
unknown committed
6290
	  }
unknown's avatar
unknown committed
6291
	;
unknown's avatar
unknown committed
6292 6293

opt_wild:
6294
	/* empty */	{}
unknown's avatar
unknown committed
6295
	| '.' '*'	{};
unknown's avatar
unknown committed
6296 6297


6298
opt_delete_options:
unknown's avatar
unknown committed
6299
	/* empty */	{}
unknown's avatar
unknown committed
6300
	| opt_delete_option opt_delete_options {};
6301 6302

opt_delete_option:
6303
	QUICK		{ Select->options|= OPTION_QUICK; }
6304
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
6305
	| IGNORE_SYM	{ Lex->ignore= 1; };
6306

6307
truncate:
unknown's avatar
unknown committed
6308
	TRUNCATE_SYM opt_table_sym table_name
6309
	{
6310
	  LEX* lex= Lex;
6311
	  lex->sql_command= SQLCOM_TRUNCATE;
6312
	  lex->select_lex.options= 0;
6313
	  lex->select_lex.init_order();
unknown's avatar
unknown committed
6314 6315
	}
	;
6316

unknown's avatar
unknown committed
6317 6318
opt_table_sym:
	/* empty */
unknown's avatar
unknown committed
6319
	| TABLE_SYM;
6320

unknown's avatar
unknown committed
6321 6322
/* Show things */

6323 6324
show:	SHOW
	{
unknown's avatar
unknown committed
6325 6326
	  LEX *lex=Lex;
	  lex->wild=0;
6327 6328 6329
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
unknown's avatar
unknown committed
6330 6331
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
unknown's avatar
unknown committed
6332
	show_param
6333 6334
	{}
	;
unknown's avatar
unknown committed
6335 6336

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

unknown's avatar
unknown committed
6617 6618 6619 6620
show_engine_param:
	STATUS_SYM
	  {
	    switch (Lex->create_info.db_type) {
6621 6622 6623
	    case DB_TYPE_NDBCLUSTER:
	      Lex->sql_command = SQLCOM_SHOW_NDBCLUSTER_STATUS;
	      break;
unknown's avatar
unknown committed
6624 6625 6626 6627
	    case DB_TYPE_INNODB:
	      Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;
	      break;
	    default:
unknown's avatar
unknown committed
6628
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "STATUS");
unknown's avatar
unknown committed
6629 6630 6631 6632 6633 6634 6635 6636 6637 6638
	      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
6639
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "LOGS");
unknown's avatar
unknown committed
6640 6641 6642 6643
	      YYABORT;
	    }
	  };

6644 6645 6646 6647
master_or_binary:
	MASTER_SYM
	| BINARY;

unknown's avatar
unknown committed
6648 6649 6650 6651
opt_storage:
	/* empty */
	| STORAGE_SYM;

unknown's avatar
unknown committed
6652 6653
opt_db:
	/* empty */  { $$= 0; }
unknown's avatar
unknown committed
6654
	| from_or_in ident { $$= $2.str; };
unknown's avatar
unknown committed
6655

unknown's avatar
unknown committed
6656 6657
opt_full:
	/* empty */ { Lex->verbose=0; }
unknown's avatar
unknown committed
6658
	| FULL	    { Lex->verbose=1; };
unknown's avatar
unknown committed
6659

unknown's avatar
unknown committed
6660 6661
from_or_in:
	FROM
unknown's avatar
unknown committed
6662
	| IN_SYM;
unknown's avatar
unknown committed
6663

unknown's avatar
unknown committed
6664 6665
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
unknown's avatar
unknown committed
6666
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
unknown's avatar
unknown committed
6667 6668 6669

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

6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684
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
6685

unknown's avatar
unknown committed
6686 6687 6688 6689
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
6690 6691 6692 6693 6694 6695 6696 6697 6698
          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
6699 6700
	    YYABORT;
	}
6701
	opt_describe_column {}
unknown's avatar
unknown committed
6702 6703 6704
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
6705
          {
unknown's avatar
unknown committed
6706
	    LEX *lex=Lex;
6707
	    lex->select_lex.options|= SELECT_DESCRIBE;
unknown's avatar
unknown committed
6708 6709
	  }
	;
unknown's avatar
unknown committed
6710 6711 6712

describe_command:
	DESC
unknown's avatar
unknown committed
6713
	| DESCRIBE;
unknown's avatar
unknown committed
6714

unknown's avatar
unknown committed
6715 6716 6717 6718 6719
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
	;

unknown's avatar
unknown committed
6720 6721 6722
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
unknown's avatar
unknown committed
6723
	| ident
unknown's avatar
unknown committed
6724
	  { Lex->wild= new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info); };
unknown's avatar
unknown committed
6725 6726 6727 6728 6729


/* flush things */

flush:
6730
	FLUSH_SYM opt_no_write_to_binlog
unknown's avatar
unknown committed
6731 6732
	{
	  LEX *lex=Lex;
6733
	  if (lex->sphead && lex->sphead->m_type != TYPE_ENUM_PROCEDURE)
6734
	  {
6735 6736 6737 6738 6739 6740
            /*
              Note that both FLUSH TABLES and FLUSH PRIVILEGES will break
              execution in prelocked mode. So it is better to disable
              FLUSH in stored functions and triggers completely.
            */
            my_error(ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0), "FLUSH");
6741 6742
	    YYABORT;
	  }
unknown's avatar
unknown committed
6743
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
unknown's avatar
unknown committed
6744
          lex->no_write_to_binlog= $2;
unknown's avatar
unknown committed
6745
	}
6746 6747 6748
	flush_options
	{}
	;
unknown's avatar
unknown committed
6749 6750 6751

flush_options:
	flush_options ',' flush_option
unknown's avatar
unknown committed
6752
	| flush_option;
unknown's avatar
unknown committed
6753 6754

flush_option:
6755
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
unknown's avatar
unknown committed
6756
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
unknown's avatar
unknown committed
6757
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
unknown's avatar
unknown committed
6758 6759 6760 6761
	| 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
6762 6763
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
6764 6765
	| DES_KEY_FILE	{ Lex->type|= REFRESH_DES_KEY_FILE; }
 	| RESOURCES     { Lex->type|= REFRESH_USER_RESOURCES; };
unknown's avatar
unknown committed
6766

unknown's avatar
unknown committed
6767
opt_table_list:
6768 6769
	/* empty */  {;}
	| table_list {;};
unknown's avatar
unknown committed
6770

unknown's avatar
unknown committed
6771
reset:
unknown's avatar
unknown committed
6772 6773 6774 6775
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
6776 6777 6778 6779
	} reset_options
	{}
	;

unknown's avatar
unknown committed
6780 6781
reset_options:
	reset_options ',' reset_option
unknown's avatar
unknown committed
6782
	| reset_option;
unknown's avatar
unknown committed
6783 6784

reset_option:
unknown's avatar
unknown committed
6785 6786
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
unknown's avatar
unknown committed
6787
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
unknown's avatar
unknown committed
6788

unknown's avatar
unknown committed
6789
purge:
unknown's avatar
unknown committed
6790 6791 6792 6793
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
6794 6795 6796 6797 6798
	} purge_options
	{}
	;

purge_options:
6799
	master_or_binary LOGS_SYM purge_option
unknown's avatar
unknown committed
6800
	;
6801 6802

purge_option:
unknown's avatar
unknown committed
6803
        TO_SYM TEXT_STRING_sys
6804 6805 6806 6807 6808 6809
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
6810 6811 6812 6813
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
unknown's avatar
unknown committed
6814
	}
unknown's avatar
unknown committed
6815
	;
unknown's avatar
unknown committed
6816

unknown's avatar
unknown committed
6817 6818 6819
/* kill threads */

kill:
6820
	KILL_SYM kill_option expr
unknown's avatar
unknown committed
6821
	{
unknown's avatar
unknown committed
6822
	  LEX *lex=Lex;
6823 6824 6825
	  lex->value_list.empty();
	  lex->value_list.push_front($3);
          lex->sql_command= SQLCOM_KILL;
unknown's avatar
unknown committed
6826
	};
unknown's avatar
unknown committed
6827

6828 6829 6830 6831 6832
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; };

unknown's avatar
unknown committed
6833 6834 6835
/* change database */

use:	USE_SYM ident
unknown's avatar
unknown committed
6836 6837
	{
	  LEX *lex=Lex;
6838 6839
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
unknown's avatar
unknown committed
6840
	};
unknown's avatar
unknown committed
6841 6842 6843

/* import, export of files */

unknown's avatar
unknown committed
6844 6845 6846
load:   LOAD DATA_SYM 
        {
          LEX *lex=Lex;
6847 6848 6849 6850 6851
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD DATA");
	    YYABORT;
	  }
unknown's avatar
unknown committed
6852 6853 6854 6855 6856 6857 6858
          lex->fname_start= lex->ptr;
        }
        load_data
        {}
        |
        LOAD TABLE_SYM table_ident FROM MASTER_SYM
        {
6859 6860 6861 6862 6863 6864 6865
	  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
6866 6867 6868 6869 6870 6871
          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
6872
	{
unknown's avatar
unknown committed
6873 6874
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
unknown's avatar
unknown committed
6875 6876
	  lex->lock_option= $1;
	  lex->local_file=  $2;
6877 6878
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
unknown's avatar
unknown committed
6879
	  if (!(lex->exchange= new sql_exchange($4.str, 0)))
unknown's avatar
unknown committed
6880
	    YYABORT;
unknown's avatar
unknown committed
6881 6882 6883 6884 6885
        }
        opt_duplicate INTO
        {
	  LEX *lex=Lex;
	  lex->fname_end= lex->ptr;
unknown's avatar
unknown committed
6886
	}
6887
        TABLE_SYM table_ident
unknown's avatar
unknown committed
6888
        {
6889 6890 6891 6892 6893 6894 6895
          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
6896
        }
6897 6898 6899
        opt_field_term opt_line_term opt_ignore_lines opt_field_or_var_spec
        opt_load_data_set_spec
        {}
6900
        |
unknown's avatar
unknown committed
6901
	FROM MASTER_SYM
6902 6903
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
unknown's avatar
unknown committed
6904
        };
unknown's avatar
unknown committed
6905 6906 6907

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

unknown's avatar
unknown committed
6910
load_data_lock:
6911
	/* empty */	{ $$= YYTHD->update_lock_default; }
unknown's avatar
unknown committed
6912 6913
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
unknown's avatar
unknown committed
6914 6915


unknown's avatar
unknown committed
6916 6917 6918
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
6919
	| IGNORE_SYM	{ Lex->ignore= 1; };
unknown's avatar
unknown committed
6920 6921 6922

opt_field_term:
	/* empty */
unknown's avatar
unknown committed
6923
	| COLUMNS field_term_list;
unknown's avatar
unknown committed
6924 6925 6926

field_term_list:
	field_term_list field_term
unknown's avatar
unknown committed
6927
	| field_term;
unknown's avatar
unknown committed
6928 6929

field_term:
6930 6931
	TERMINATED BY text_string 
          {
6932
            DBUG_ASSERT(Lex->exchange != 0);
6933 6934
            Lex->exchange->field_term= $3;
          }
unknown's avatar
unknown committed
6935
	| OPTIONALLY ENCLOSED BY text_string
unknown's avatar
unknown committed
6936
	  {
unknown's avatar
unknown committed
6937
            LEX *lex= Lex;
6938
            DBUG_ASSERT(lex->exchange != 0);
unknown's avatar
unknown committed
6939 6940
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
unknown's avatar
unknown committed
6941
	  }
unknown's avatar
unknown committed
6942
        | ENCLOSED BY text_string
6943
          {
6944
            DBUG_ASSERT(Lex->exchange != 0);
6945 6946
            Lex->exchange->enclosed= $3;
          }
unknown's avatar
unknown committed
6947
        | ESCAPED BY text_string
6948
          {
6949
            DBUG_ASSERT(Lex->exchange != 0);
6950 6951
            Lex->exchange->escaped= $3;
          };
unknown's avatar
unknown committed
6952 6953 6954

opt_line_term:
	/* empty */
unknown's avatar
unknown committed
6955
	| LINES line_term_list;
unknown's avatar
unknown committed
6956 6957 6958

line_term_list:
	line_term_list line_term
unknown's avatar
unknown committed
6959
	| line_term;
unknown's avatar
unknown committed
6960 6961

line_term:
unknown's avatar
unknown committed
6962
        TERMINATED BY text_string
6963
          {
6964
            DBUG_ASSERT(Lex->exchange != 0);
6965 6966
            Lex->exchange->line_term= $3;
          }
unknown's avatar
unknown committed
6967
        | STARTING BY text_string
6968
          {
6969
            DBUG_ASSERT(Lex->exchange != 0);
6970 6971
            Lex->exchange->line_start= $3;
          };
unknown's avatar
unknown committed
6972 6973 6974

opt_ignore_lines:
	/* empty */
unknown's avatar
unknown committed
6975 6976
        | IGNORE_SYM NUM LINES
          {
6977
            DBUG_ASSERT(Lex->exchange != 0);
6978 6979
            Lex->exchange->skip_lines= atol($2.str);
          };
unknown's avatar
unknown committed
6980

unknown's avatar
unknown committed
6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991
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
6992

unknown's avatar
unknown committed
6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003
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
7004 7005 7006
/* Common definitions */

text_literal:
7007
	TEXT_STRING_literal
unknown's avatar
unknown committed
7008 7009
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7010
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
unknown's avatar
unknown committed
7011
	}
unknown's avatar
unknown committed
7012
	| NCHAR_STRING
7013
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
7014
	| UNDERSCORE_CHARSET TEXT_STRING
7015
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
7016
	| text_literal TEXT_STRING_literal
unknown's avatar
unknown committed
7017 7018
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
unknown's avatar
unknown committed
7019 7020

text_string:
7021
	TEXT_STRING_literal
unknown's avatar
unknown committed
7022
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
unknown's avatar
unknown committed
7023 7024
	| HEX_NUM
	  {
unknown's avatar
unknown committed
7025
	    Item *tmp= new Item_hex_string($1.str, $1.length);
7026
	    /*
unknown's avatar
unknown committed
7027
	      it is OK only emulate fix_fields, because we need only
7028 7029 7030
              value of constant
	    */
	    $$= tmp ?
unknown's avatar
unknown committed
7031
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
7032
	      (String*) 0;
7033
	  }
unknown's avatar
unknown committed
7034 7035 7036 7037 7038 7039 7040 7041 7042 7043
        | 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;
          }
7044 7045
	;

unknown's avatar
unknown committed
7046
param_marker:
7047
        PARAM_MARKER
unknown's avatar
unknown committed
7048
        {
7049 7050
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
7051 7052 7053
          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
7054
          {
7055
            my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
unknown's avatar
unknown committed
7056 7057
            YYABORT;
          }
7058 7059 7060
        }
	;

unknown's avatar
unknown committed
7061 7062 7063
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
7064 7065
	| '-' NUM_literal
	  {
7066
	    $2->max_length++;
unknown's avatar
unknown committed
7067
	    $$= $2->neg();
7068
	  }
unknown's avatar
unknown committed
7069 7070 7071
	;


unknown's avatar
unknown committed
7072 7073
literal:
	text_literal	{ $$ =	$1; }
unknown's avatar
unknown committed
7074
	| NUM_literal	{ $$ = $1; }
unknown's avatar
unknown committed
7075
	| NULL_SYM	{ $$ =	new Item_null();
7076
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
7077 7078
	| FALSE_SYM	{ $$= new Item_int((char*) "FALSE",0,1); }
	| TRUE_SYM	{ $$= new Item_int((char*) "TRUE",1,1); }
unknown's avatar
unknown committed
7079 7080
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
7081
	| UNDERSCORE_CHARSET HEX_NUM
7082
	  {
unknown's avatar
unknown committed
7083
	    Item *tmp= new Item_hex_string($2.str, $2.length);
7084
	    /*
unknown's avatar
unknown committed
7085
	      it is OK only emulate fix_fieds, because we need only
7086 7087 7088
              value of constant
	    */
	    String *str= tmp ?
unknown's avatar
unknown committed
7089
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
7090
	      (String*) 0;
unknown's avatar
unknown committed
7091 7092 7093
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
7094
	  }
unknown's avatar
unknown committed
7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108
	| 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
7109 7110
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
unknown's avatar
unknown committed
7111
	| TIMESTAMP text_literal { $$ = $2; };
unknown's avatar
unknown committed
7112

7113
NUM_literal:
7114 7115
	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); }
7116
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
unknown's avatar
unknown committed
7117
        | DECIMAL_NUM
7118
	{
unknown's avatar
unknown committed
7119
           $$= new Item_decimal($1.str, $1.length, YYTHD->charset());
7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
7133
	;
unknown's avatar
unknown committed
7134

unknown's avatar
unknown committed
7135
/**********************************************************************
unknown's avatar
unknown committed
7136
** Creating different items.
unknown's avatar
unknown committed
7137 7138 7139
**********************************************************************/

insert_ident:
7140
	simple_ident_nospvar { $$=$1; }
unknown's avatar
unknown committed
7141
	| table_wild	 { $$=$1; };
unknown's avatar
unknown committed
7142 7143

table_wild:
unknown's avatar
unknown committed
7144
	ident '.' '*'
7145
	{
7146
          SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7147
	  $$ = new Item_field(Lex->current_context(), NullS, $1.str, "*");
7148
	  sel->with_wild++;
7149
	}
unknown's avatar
unknown committed
7150
	| ident '.' ident '.' '*'
7151
	{
7152
          SELECT_LEX *sel= Select;
unknown's avatar
unknown committed
7153
	  $$ = new Item_field(Lex->current_context(), (YYTHD->client_capabilities &
unknown's avatar
unknown committed
7154 7155
                             CLIENT_NO_SCHEMA ? NullS : $1.str),
                             $3.str,"*");
7156
	  sel->with_wild++;
7157 7158
	}
	;
unknown's avatar
unknown committed
7159 7160

order_ident:
unknown's avatar
unknown committed
7161
	expr { $$=$1; };
unknown's avatar
unknown committed
7162 7163

simple_ident:
7164 7165 7166 7167 7168 7169
	ident
	{
	  sp_pvar_t *spv;
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;
	  if (spc && (spv = spc->find_pvar(&$1)))
7170 7171 7172 7173 7174
	  {
            /* We're compiling a stored procedure and found a variable */
            Item_splocal *splocal;
            splocal= new Item_splocal($1, spv->offset, lex->tok_start_prev - 
                                      lex->sphead->m_tmp_query);
unknown's avatar
unknown committed
7175
#ifndef DBUG_OFF
7176 7177 7178
            if (splocal)
              splocal->owner= lex->sphead;
#endif
7179
	    $$ = (Item*) splocal;
7180 7181 7182 7183 7184 7185 7186 7187
            lex->variables_used= 1;
	    lex->safe_to_cache_query=0;
	  }
	  else
	  {
	    SELECT_LEX *sel=Select;
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
7188 7189
                 (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	         (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
7190 7191 7192 7193 7194 7195
	  }
        }
        | simple_ident_q { $$= $1; }
	;

simple_ident_nospvar:
unknown's avatar
unknown committed
7196
	ident
unknown's avatar
unknown committed
7197
	{
unknown's avatar
unknown committed
7198
	  SELECT_LEX *sel=Select;
7199
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
7200
	       sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
7201 7202
              (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	      (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
unknown's avatar
unknown committed
7203
	}
7204
	| simple_ident_q { $$= $1; }
unknown's avatar
unknown committed
7205
	;
7206 7207 7208

simple_ident_q:
	ident '.' ident
unknown's avatar
unknown committed
7209
	{
unknown's avatar
unknown committed
7210
	  THD *thd= YYTHD;
7211
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
7212

7213 7214 7215 7216 7217 7218
          /*
            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
7219
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") ||
7220 7221
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
7222
            Item_trigger_field *trg_fld;
7223
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
unknown's avatar
unknown committed
7224

7225 7226 7227
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
unknown's avatar
unknown committed
7228
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
7229 7230
              YYABORT;
            }
unknown's avatar
unknown committed
7231

7232 7233 7234
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
unknown's avatar
unknown committed
7235
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
7236 7237
              YYABORT;
            }
unknown's avatar
unknown committed
7238

unknown's avatar
unknown committed
7239
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
7240
                                                  new_row ?
7241 7242 7243
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
                                                  $3.str)))
7244
              YYABORT;
unknown's avatar
unknown committed
7245

7246 7247 7248 7249 7250 7251
            /*
              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
7252

7253 7254 7255 7256 7257 7258 7259
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
7260 7261
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
7262 7263 7264
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
7265 7266
	        (Item*) new Item_field(Lex->current_context(), NullS, $1.str, $3.str) :
	        (Item*) new Item_ref(Lex->current_context(), NullS, $1.str, $3.str);
7267 7268
          }
        }
unknown's avatar
unknown committed
7269
	| '.' ident '.' ident
unknown's avatar
unknown committed
7270
	{
unknown's avatar
unknown committed
7271
	  THD *thd= YYTHD;
7272
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
7273
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
7274 7275
	  if (sel->no_table_names_allowed)
	  {
7276 7277
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $2.str, thd->where);
unknown's avatar
unknown committed
7278
	  }
7279
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
7280
	       sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
7281 7282
	      (Item*) new Item_field(Lex->current_context(), NullS, $2.str, $4.str) :
              (Item*) new Item_ref(Lex->current_context(), NullS, $2.str, $4.str);
unknown's avatar
unknown committed
7283
	}
unknown's avatar
unknown committed
7284
	| ident '.' ident '.' ident
unknown's avatar
unknown committed
7285
	{
unknown's avatar
unknown committed
7286
	  THD *thd= YYTHD;
7287
	  LEX *lex= thd->lex;
unknown's avatar
unknown committed
7288
	  SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
7289 7290
	  if (sel->no_table_names_allowed)
	  {
7291 7292
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $3.str, thd->where);
unknown's avatar
unknown committed
7293
	  }
7294
	  $$= (sel->parsing_place != IN_HAVING ||
unknown's avatar
unknown committed
7295
	       sel->get_in_sum_expr() > 0) ?
unknown's avatar
unknown committed
7296
	      (Item*) new Item_field(Lex->current_context(),
7297
                                     (YYTHD->client_capabilities &
unknown's avatar
unknown committed
7298 7299
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
unknown's avatar
unknown committed
7300
	      (Item*) new Item_ref(Lex->current_context(),
7301
                                   (YYTHD->client_capabilities &
7302
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
unknown's avatar
unknown committed
7303
                                   $3.str, $5.str);
unknown's avatar
unknown committed
7304
	};
unknown's avatar
unknown committed
7305 7306 7307 7308


field_ident:
	ident			{ $$=$1;}
7309 7310 7311 7312 7313
	| ident '.' ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->db))
            {
7314
              my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
7315 7316
              YYABORT;
            }
7317 7318
            if (my_strcasecmp(table_alias_charset, $3.str,
                              table->table_name))
7319
            {
7320
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3.str);
7321 7322 7323 7324 7325 7326 7327 7328 7329
              YYABORT;
            }
            $$=$5;
          }
	| ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->alias))
            {
7330
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $1.str);
7331 7332 7333 7334
              YYABORT;
            }
            $$=$3;
          }
unknown's avatar
unknown committed
7335
	| '.' ident		{ $$=$2;}	/* For Delphi */;
unknown's avatar
unknown committed
7336 7337 7338

table_ident:
	ident			{ $$=new Table_ident($1); }
unknown's avatar
unknown committed
7339
	| ident '.' ident	{ $$=new Table_ident(YYTHD, $1,$3,0);}
7340 7341 7342
	| '.' ident		{ $$=new Table_ident($2);} /* For Delphi */
        ;

unknown's avatar
unknown committed
7343
table_ident_nodb:
unknown's avatar
unknown committed
7344
	ident			{ LEX_STRING db={(char*) any_db,3}; $$=new Table_ident(YYTHD, db,$1,0); }
7345
        ;
unknown's avatar
unknown committed
7346

unknown's avatar
unknown committed
7347
IDENT_sys:
7348 7349 7350 7351 7352
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
7353 7354
            {
              CHARSET_INFO *cs= system_charset_info;
7355
              int dummy_error;
7356 7357
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
7358
                                                   $1.length, &dummy_error);
7359 7360
              if (wlen < $1.length)
              {
7361 7362
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
7363 7364
                YYABORT;
              }
7365
	      $$= $1;
7366
            }
7367 7368 7369 7370
	    else
	      thd->convert_string(&$$, system_charset_info,
				  $1.str, $1.length, thd->charset());
	  }
unknown's avatar
unknown committed
7371 7372 7373 7374 7375 7376
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7377 7378
	  if (thd->charset_is_system_charset)
	    $$= $1;
unknown's avatar
unknown committed
7379
	  else
unknown's avatar
unknown committed
7380 7381
	    thd->convert_string(&$$, system_charset_info,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
7382 7383 7384
	}
	;

7385
TEXT_STRING_literal:
unknown's avatar
unknown committed
7386 7387 7388
	TEXT_STRING
	{
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7389 7390
	  if (thd->charset_is_collation_connection)
	    $$= $1;
unknown's avatar
unknown committed
7391
	  else
unknown's avatar
unknown committed
7392 7393
	    thd->convert_string(&$$, thd->variables.collation_connection,
				$1.str, $1.length, thd->charset());
unknown's avatar
unknown committed
7394 7395 7396 7397
	}
	;


unknown's avatar
unknown committed
7398
ident:
unknown's avatar
unknown committed
7399
	IDENT_sys	    { $$=$1; }
unknown's avatar
unknown committed
7400 7401
	| keyword
	{
unknown's avatar
unknown committed
7402 7403 7404
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
7405 7406
	}
	;
unknown's avatar
unknown committed
7407

7408 7409 7410 7411 7412 7413 7414 7415 7416 7417
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
7418
ident_or_text:
unknown's avatar
unknown committed
7419
        ident                   { $$=$1;}
unknown's avatar
unknown committed
7420 7421
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
unknown's avatar
unknown committed
7422 7423 7424 7425

user:
	ident_or_text
	{
unknown's avatar
unknown committed
7426 7427
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
7428
	    YYABORT;
7429 7430 7431 7432
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
unknown's avatar
unknown committed
7433 7434
	| ident_or_text '@' ident_or_text
	  {
unknown's avatar
unknown committed
7435 7436
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
unknown's avatar
unknown committed
7437 7438
	      YYABORT;
	    $$->user = $1; $$->host=$3;
7439
	  }
unknown's avatar
unknown committed
7440
	| CURRENT_USER optional_braces
7441 7442
	{
          THD *thd= YYTHD;
7443
          Security_context *sctx= thd->security_ctx;
7444 7445
          if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
            YYABORT;
7446 7447 7448
          $$->user.str= sctx->priv_user;
          $$->user.length= strlen(sctx->priv_user);
          if (*sctx->priv_host != 0)
7449
          {
7450 7451
            $$->host.str= sctx->priv_host;
            $$->host.length= strlen(sctx->priv_host);
7452 7453 7454 7455 7456 7457 7458
          }
          else
          {
            $$->host.str= (char *) "%";
            $$->host.length= 1;
          }
	};
unknown's avatar
unknown committed
7459

7460
/* Keyword that we allow for identifiers (except SP labels) */
unknown's avatar
unknown committed
7461
keyword:
7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501
	keyword_sp		{}
	| ASCII_SYM		{}
	| BACKUP_SYM		{}
	| BEGIN_SYM		{}
	| BYTE_SYM		{}
	| CACHE_SYM		{}
	| CHARSET		{}
	| CHECKSUM_SYM		{}
	| CLOSE_SYM		{}
	| COMMENT_SYM		{}
	| COMMIT_SYM		{}
	| CONTAINS_SYM          {}
        | DEALLOCATE_SYM        {}
	| DO_SYM		{}
	| END			{}
	| EXECUTE_SYM		{}
	| FLUSH_SYM		{}
	| HANDLER_SYM		{}
	| HELP_SYM		{}
	| LANGUAGE_SYM          {}
	| NO_SYM		{}
	| OPEN_SYM		{}
        | PREPARE_SYM           {}
	| REPAIR		{}
	| RESET_SYM		{}
	| RESTORE_SYM		{}
	| ROLLBACK_SYM		{}
	| SAVEPOINT_SYM		{}
	| SECURITY_SYM		{}
	| SIGNED_SYM		{}
	| SLAVE			{}
	| START_SYM		{}
	| STOP_SYM		{}
	| TRUNCATE_SYM		{}
	| UNICODE_SYM		{}
        | XA_SYM                {}
	;

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

/* Option functions */

set:
	SET opt_option
	{
unknown's avatar
unknown committed
7726 7727
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
unknown's avatar
unknown committed
7728
	  mysql_init_select(lex);
7729
	  lex->option_type=OPT_SESSION;
unknown's avatar
unknown committed
7730
	  lex->var_list.empty();
7731
          lex->one_shot_set= 0;
unknown's avatar
unknown committed
7732
	}
7733 7734 7735
	option_value_list
	{}
	;
unknown's avatar
unknown committed
7736 7737 7738

opt_option:
	/* empty */ {}
unknown's avatar
unknown committed
7739
	| OPTION {};
unknown's avatar
unknown committed
7740 7741

option_value_list:
7742 7743 7744 7745 7746 7747 7748 7749 7750 7751
	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
7752 7753
              and sp_instr_set_trigger instructions share one LEX.
              (Well, it is theoretically possible but adds some extra
7754 7755 7756 7757 7758 7759 7760 7761
               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
7762

7763 7764 7765 7766 7767 7768 7769 7770 7771
            /* 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;
          }
        }
7772
	ext_option_value
7773 7774
        {
          LEX *lex= Lex;
unknown's avatar
unknown committed
7775

7776 7777 7778
          if (lex->sphead)
          {
            sp_head *sp= lex->sphead;
unknown's avatar
unknown committed
7779

7780 7781 7782 7783 7784 7785 7786 7787 7788
	    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
7789

7790 7791 7792
              if (!(i= new sp_instr_stmt(sp->instructions(), lex->spcont,
                                         lex)))
                YYABORT;
unknown's avatar
unknown committed
7793

7794 7795 7796 7797
              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
7798

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

7802 7803 7804 7805 7806 7807 7808 7809 7810
              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
7811 7812

option_type:
7813 7814 7815 7816 7817 7818 7819 7820 7821
        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
7822 7823 7824 7825
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
unknown's avatar
unknown committed
7826
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
7827 7828 7829 7830 7831 7832
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

opt_var_ident_type:
	/* empty */		{ $$=OPT_DEFAULT; }
unknown's avatar
unknown committed
7833
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
unknown's avatar
unknown committed
7834 7835 7836
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
unknown's avatar
unknown committed
7837

7838 7839 7840
ext_option_value:
        sys_option_value
        | option_type2 option_value;
7841

7842 7843 7844 7845
sys_option_value:
        option_type internal_variable_name equal set_expr_or_default
        {
          LEX *lex=Lex;
7846

7847 7848 7849 7850
          if ($2.var == &trg_new_row_fake_var)
          {
            /* We are in trigger and assigning value to field of new row */
            Item *it;
7851
            Item_trigger_field *trg_fld;
7852 7853
            sp_instr_set_trigger_field *sp_fld;
	    LINT_INIT(sp_fld);
7854 7855 7856 7857
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
7858
            }
7859 7860
            if ($4)
              it= $4;
7861
            else
7862 7863 7864 7865
            {
              /* QQ: Shouldn't this be field's default value ? */
              it= new Item_null();
            }
7866

unknown's avatar
unknown committed
7867
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
7868
                                                  Item_trigger_field::NEW_ROW,
7869
                                                  $2.base_name.str)) ||
7870 7871 7872 7873 7874
                !(sp_fld= new sp_instr_set_trigger_field(lex->sphead->
                          	                         instructions(),
                                	                 lex->spcont,
							 trg_fld,
                                        	         it, lex)))
7875
              YYABORT;
7876

7877 7878 7879 7880
            /*
              Let us add this item to list of all Item_trigger_field
              objects in trigger.
            */
7881 7882
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
                                    (byte **)&trg_fld->next_trg_field);
7883

7884
            lex->sphead->add_instr(sp_fld);
7885 7886 7887 7888
          }
          else if ($2.var)
          { /* System variable */
            if ($1)
7889
              lex->option_type= $1;
7890 7891 7892 7893 7894 7895 7896 7897
            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;
7898
            sp_instr_set *sp_set;
7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913
            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();
7914 7915 7916
            sp_set= new sp_instr_set(lex->sphead->instructions(), ctx,
                                     spv->offset, it, spv->type, lex, TRUE);
            lex->sphead->add_instr(sp_set);
7917 7918 7919 7920 7921
          }
        }
        | option_type TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	{
	  LEX *lex=Lex;
7922
          if ($1)
7923
            lex->option_type= $1;
7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935
	  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
7936
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
unknown's avatar
unknown committed
7937 7938
	  {
	    LEX *lex=Lex;
7939
	    lex->var_list.push_back(new set_var($3, $4.var, &$4.base_name, $6));
unknown's avatar
unknown committed
7940
	  }
7941
	| charset old_or_new_charset_name_or_default
unknown's avatar
unknown committed
7942
	{
unknown's avatar
unknown committed
7943
	  THD *thd= YYTHD;
unknown's avatar
unknown committed
7944
	  LEX *lex= Lex;
7945
	  $2= $2 ? $2: global_system_variables.character_set_client;
7946
	  lex->var_list.push_back(new set_var_collation_client($2,thd->variables.collation_database,$2));
unknown's avatar
unknown committed
7947
	}
7948 7949 7950 7951 7952 7953 7954 7955 7956 7957
        | NAMES_SYM equal expr
	  {
	    LEX *lex= Lex;
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING names;

	    names.str= (char *)"names";
	    names.length= 5;
	    if (spc && spc->find_pvar(&names))
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), names.str);
7958 7959 7960
            else
              yyerror(ER(ER_SYNTAX_ERROR));

7961 7962
	    YYABORT;
	  }
7963
	| NAMES_SYM charset_name_or_default opt_collate
7964
	{
unknown's avatar
unknown committed
7965
	  LEX *lex= Lex;
7966
	  $2= $2 ? $2 : global_system_variables.character_set_client;
7967 7968
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
7969
	  {
7970 7971
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
unknown's avatar
unknown committed
7972
	    YYABORT;
7973
	  }
7974
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
7975
	}
unknown's avatar
unknown committed
7976
	| PASSWORD equal text_or_password
unknown's avatar
unknown committed
7977
	  {
7978
	    THD *thd=YYTHD;
unknown's avatar
unknown committed
7979
	    LEX_USER *user;
7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990
	    LEX *lex= Lex;	    
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING pw;

	    pw.str= (char *)"password";
	    pw.length= 8;
	    if (spc && spc->find_pvar(&pw))
	    {
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), pw.str);
	      YYABORT;
	    }
unknown's avatar
unknown committed
7991
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
unknown's avatar
unknown committed
7992
	      YYABORT;
7993
	    user->host=null_lex_str;
7994
	    user->user.str=thd->security_ctx->priv_user;
7995
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
unknown's avatar
unknown committed
7996 7997
	  }
	| PASSWORD FOR_SYM user equal text_or_password
unknown's avatar
unknown committed
7998
	  {
unknown's avatar
unknown committed
7999 8000
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
8001
	;
unknown's avatar
unknown committed
8002

unknown's avatar
unknown committed
8003 8004 8005
internal_variable_name:
	ident
	{
8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017
	  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;
8018
	    $$.base_name= null_lex_str;
8019 8020 8021 8022
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
8023 8024 8025
            if (tmp == &sys_time_zone &&
                lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
8026 8027 8028 8029 8030 8031 8032 8033
            else if (spc && tmp == &sys_autocommit)
            {
              /*
                We don't allow setting AUTOCOMMIT from a stored function
		or trigger.
              */
              lex->sphead->m_flags|= sp_head::HAS_SET_AUTOCOMMIT_STMT;
            }
8034 8035 8036 8037 8038 8039 8040
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
unknown's avatar
unknown committed
8041
	}
8042 8043
	| ident '.' ident
	  {
8044
            LEX *lex= Lex;
8045 8046
            if (check_reserved_words(&$1))
            {
8047
	      yyerror(ER(ER_SYNTAX_ERROR));
8048 8049
              YYABORT;
            }
8050 8051 8052 8053 8054 8055
            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
8056
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
8057 8058 8059 8060
                YYABORT;
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
unknown's avatar
unknown committed
8061 8062
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
8063 8064 8065 8066
                YYABORT;
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
unknown's avatar
unknown committed
8067
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079
                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())
8080
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
8081 8082 8083
              $$.var= tmp;
              $$.base_name= $1;
            }
8084 8085 8086 8087 8088 8089 8090
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
8091
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
unknown's avatar
unknown committed
8092 8093 8094
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
8095
	  }
8096
        ;
unknown's avatar
unknown committed
8097 8098 8099 8100 8101 8102 8103

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

unknown's avatar
unknown committed
8105 8106 8107 8108
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
8109
	    $$= $3.length ? YYTHD->variables.old_passwords ?
unknown's avatar
unknown committed
8110 8111 8112 8113 8114 8115 8116 8117
	        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;
8118 8119
	  }
          ;
unknown's avatar
unknown committed
8120

unknown's avatar
unknown committed
8121

unknown's avatar
unknown committed
8122
set_expr_or_default:
unknown's avatar
unknown committed
8123 8124
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
8125 8126
	| ON	  { $$=new Item_string("ON",  2, system_charset_info); }
	| ALL	  { $$=new Item_string("ALL", 3, system_charset_info); }
unknown's avatar
unknown committed
8127
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
unknown's avatar
unknown committed
8128
	;
unknown's avatar
unknown committed
8129 8130


unknown's avatar
unknown committed
8131 8132 8133 8134 8135
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
8136 8137 8138 8139
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
8140
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOCK");
8141 8142 8143
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_LOCK_TABLES;
unknown's avatar
unknown committed
8144
	}
8145 8146
	table_lock_list
	{}
8147
	;
unknown's avatar
unknown committed
8148 8149 8150

table_or_tables:
	TABLE_SYM
unknown's avatar
unknown committed
8151
	| TABLES;
unknown's avatar
unknown committed
8152 8153 8154

table_lock_list:
	table_lock
unknown's avatar
unknown committed
8155
	| table_lock_list ',' table_lock;
unknown's avatar
unknown committed
8156 8157 8158

table_lock:
	table_ident opt_table_alias lock_option
8159
	{
unknown's avatar
unknown committed
8160
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
8161 8162
	   YYABORT;
	}
8163
        ;
unknown's avatar
unknown committed
8164 8165 8166

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
8167
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
unknown's avatar
unknown committed
8168
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
8169 8170
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
unknown's avatar
unknown committed
8171 8172

unlock:
8173 8174 8175 8176 8177 8178
	UNLOCK_SYM
	{
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
8179
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "UNLOCK");
8180 8181 8182 8183 8184 8185
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_UNLOCK_TABLES;
	}
	table_or_tables
	{}
8186
        ;
unknown's avatar
unknown committed
8187 8188


8189
/*
unknown's avatar
unknown committed
8190
** Handler: direct access to ISAM functions
8191 8192 8193 8194 8195
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
8196
	  LEX *lex= Lex;
8197 8198 8199 8200 8201
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
8202
	  lex->sql_command = SQLCOM_HA_OPEN;
unknown's avatar
unknown committed
8203
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
8204 8205
	    YYABORT;
	}
unknown's avatar
unknown committed
8206
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
8207
	{
8208
	  LEX *lex= Lex;
8209 8210 8211 8212 8213
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
8214
	  lex->sql_command = SQLCOM_HA_CLOSE;
unknown's avatar
unknown committed
8215
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
8216 8217
	    YYABORT;
	}
unknown's avatar
unknown committed
8218
	| HANDLER_SYM table_ident_nodb READ_SYM
8219
	{
unknown's avatar
unknown committed
8220
	  LEX *lex=Lex;
8221 8222 8223 8224 8225
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
unknown's avatar
unknown committed
8226 8227
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
8228
	  lex->current_select->select_limit= new Item_int((int32) 1);
8229
	  lex->current_select->offset_limit= 0;
unknown's avatar
unknown committed
8230
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
8231
	    YYABORT;
unknown's avatar
unknown committed
8232
        }
8233
        handler_read_or_scan where_clause opt_limit_clause {}
8234
        ;
8235

unknown's avatar
unknown committed
8236
handler_read_or_scan:
8237 8238
	handler_scan_function         { Lex->ident= null_lex_str; }
        | ident handler_rkey_function { Lex->ident= $1; }
8239
        ;
unknown's avatar
unknown committed
8240 8241 8242

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
8243 8244
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
unknown's avatar
unknown committed
8245 8246 8247 8248 8249 8250

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;  }
8251 8252
	| handler_rkey_mode
	{
unknown's avatar
unknown committed
8253 8254
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
unknown's avatar
unknown committed
8255
	  lex->ha_rkey_mode=$1;
unknown's avatar
unknown committed
8256
	  if (!(lex->insert_list = new List_item))
8257
	    YYABORT;
8258 8259
	} '(' values ')' { }
        ;
8260 8261

handler_rkey_mode:
unknown's avatar
unknown committed
8262 8263 8264 8265
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
8266 8267
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
8268

unknown's avatar
unknown committed
8269 8270 8271
/* GRANT / REVOKE */

revoke:
8272
	REVOKE clear_privileges revoke_command
8273 8274 8275 8276
	{}
        ;

revoke_command:
8277
	grant_privileges ON opt_table grant_ident FROM grant_list
8278
	{
8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306
          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;
8307
        }
8308
	|
8309
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
8310 8311 8312
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
8313
	;
unknown's avatar
unknown committed
8314 8315

grant:
8316 8317 8318 8319 8320 8321
	GRANT clear_privileges grant_command
	{}
        ;

grant_command:
	grant_privileges ON opt_table grant_ident TO_SYM grant_list
8322
	require_clause grant_options
8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354
	{
          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
8355

8356 8357 8358 8359
opt_table:
	/* Empty */
	| TABLE_SYM ;
        
unknown's avatar
unknown committed
8360
grant_privileges:
8361 8362 8363 8364 8365 8366
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
8367
        ;
unknown's avatar
unknown committed
8368

unknown's avatar
unknown committed
8369 8370 8371 8372 8373
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

8374 8375 8376
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
unknown's avatar
unknown committed
8377

8378
object_privilege:
unknown's avatar
unknown committed
8379
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
8380 8381 8382
	| 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
8383 8384
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
unknown's avatar
unknown committed
8385
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
unknown's avatar
unknown committed
8386 8387 8388
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
unknown's avatar
unknown committed
8389
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
unknown's avatar
unknown committed
8390 8391 8392 8393
	| 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
8394 8395 8396 8397 8398
	| 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; }
8399 8400 8401 8402
	| 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; }
8403 8404
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
8405
	| CREATE USER { Lex->grant |= CREATE_USER_ACL; }
unknown's avatar
unknown committed
8406
	;
unknown's avatar
unknown committed
8407

unknown's avatar
unknown committed
8408

8409 8410
opt_and:
	/* empty */	{}
unknown's avatar
unknown committed
8411
	| AND_SYM	{}
8412 8413 8414 8415 8416 8417 8418 8419 8420
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
unknown's avatar
unknown committed
8421 8422 8423 8424
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
unknown's avatar
unknown committed
8425
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
unknown's avatar
unknown committed
8426 8427 8428 8429 8430 8431 8432 8433 8434
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
unknown's avatar
unknown committed
8435
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
unknown's avatar
unknown committed
8436 8437 8438 8439 8440 8441 8442 8443 8444
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
unknown's avatar
unknown committed
8445
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
unknown's avatar
unknown committed
8446 8447 8448 8449 8450
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
8451

8452
grant_ident:
unknown's avatar
unknown committed
8453 8454
	'*'
	  {
8455
	    LEX *lex= Lex;
unknown's avatar
unknown committed
8456
	    lex->current_select->db= lex->thd->db;
unknown's avatar
unknown committed
8457
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
8458 8459
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
8460
	    {
unknown's avatar
unknown committed
8461 8462
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
8463
	      YYABORT;
unknown's avatar
unknown committed
8464
	    }
unknown's avatar
unknown committed
8465 8466 8467
	  }
	| ident '.' '*'
	  {
8468
	    LEX *lex= Lex;
unknown's avatar
unknown committed
8469
	    lex->current_select->db = $1.str;
unknown's avatar
unknown committed
8470
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
8471 8472
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
8473
	    {
unknown's avatar
unknown committed
8474 8475
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
8476 8477 8478 8479 8480
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
8481
	    LEX *lex= Lex;
unknown's avatar
unknown committed
8482
	    lex->current_select->db = NULL;
unknown's avatar
unknown committed
8483 8484
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
unknown's avatar
unknown committed
8485
	    else if (lex->columns.elements)
unknown's avatar
unknown committed
8486
	    {
unknown's avatar
unknown committed
8487 8488
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
unknown's avatar
unknown committed
8489 8490 8491 8492 8493
	      YYABORT;
	    }
	  }
	| table_ident
	  {
unknown's avatar
unknown committed
8494
	    LEX *lex=Lex;
unknown's avatar
unknown committed
8495
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
unknown's avatar
unknown committed
8496
	      YYABORT;
unknown's avatar
unknown committed
8497
	    if (lex->grant == GLOBAL_ACLS)
unknown's avatar
unknown committed
8498
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
8499 8500
	  }
          ;
unknown's avatar
unknown committed
8501 8502 8503


user_list:
8504 8505 8506 8507 8508 8509 8510 8511 8512 8513
	user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;


grant_list:
8514
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
8515
	| grant_list ',' grant_user
8516 8517 8518 8519 8520
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
unknown's avatar
unknown committed
8521 8522 8523 8524 8525 8526 8527 8528


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
8529
             if (YYTHD->variables.old_passwords)
8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546
             {
               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
8547 8548 8549
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
8550
	  { $$= $1; $1->password= $5; }
unknown's avatar
unknown committed
8551
	| user
8552
	  { $$= $1; $1->password= null_lex_str; }
8553
        ;
unknown's avatar
unknown committed
8554 8555 8556


opt_column_list:
unknown's avatar
unknown committed
8557 8558 8559 8560 8561
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
unknown's avatar
unknown committed
8562
	| '(' column_list ')';
unknown's avatar
unknown committed
8563 8564 8565

column_list:
	column_list ',' column_list_id
unknown's avatar
unknown committed
8566
	| column_list_id;
unknown's avatar
unknown committed
8567 8568 8569 8570

column_list_id:
	ident
	{
unknown's avatar
unknown committed
8571
	  String *new_str = new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info);
unknown's avatar
unknown committed
8572 8573
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
unknown's avatar
unknown committed
8574
	  LEX *lex=Lex;
unknown's avatar
unknown committed
8575 8576
	  while ((point=iter++))
	  {
8577 8578
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
unknown's avatar
unknown committed
8579 8580
		break;
	  }
unknown's avatar
unknown committed
8581
	  lex->grant_tot_col|= lex->which_columns;
unknown's avatar
unknown committed
8582
	  if (point)
unknown's avatar
unknown committed
8583
	    point->rights |= lex->which_columns;
unknown's avatar
unknown committed
8584
	  else
unknown's avatar
unknown committed
8585
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
8586 8587
	}
        ;
unknown's avatar
unknown committed
8588

unknown's avatar
unknown committed
8589 8590

require_clause: /* empty */
8591
        | REQUIRE_SYM require_list
8592 8593 8594
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
8595
        | REQUIRE_SYM SSL_SYM
8596 8597 8598
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
8599
        | REQUIRE_SYM X509_SYM
8600 8601 8602 8603 8604 8605 8606
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
8607
          ;
unknown's avatar
unknown committed
8608

unknown's avatar
unknown committed
8609
grant_options:
unknown's avatar
unknown committed
8610
	/* empty */ {}
unknown's avatar
unknown committed
8611
	| WITH grant_option_list;
unknown's avatar
unknown committed
8612

unknown's avatar
unknown committed
8613 8614
grant_option_list:
	grant_option_list grant_option {}
8615 8616
	| grant_option {}
        ;
unknown's avatar
unknown committed
8617 8618 8619

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
unknown's avatar
unknown committed
8620
        | MAX_QUERIES_PER_HOUR ulong_num
unknown's avatar
unknown committed
8621
        {
unknown's avatar
unknown committed
8622 8623 8624
	  LEX *lex=Lex;
	  lex->mqh.questions=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
8625
	}
unknown's avatar
unknown committed
8626
        | MAX_UPDATES_PER_HOUR ulong_num
8627
        {
unknown's avatar
unknown committed
8628 8629 8630
	  LEX *lex=Lex;
	  lex->mqh.updates=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
8631
	}
unknown's avatar
unknown committed
8632
        | MAX_CONNECTIONS_PER_HOUR ulong_num
8633
        {
unknown's avatar
unknown committed
8634 8635 8636
	  LEX *lex=Lex;
	  lex->mqh.conn_per_hour= $2;
	  lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
8637
	}
unknown's avatar
unknown committed
8638
        | MAX_USER_CONNECTIONS_SYM ulong_num
8639
        {
unknown's avatar
unknown committed
8640 8641 8642
	  LEX *lex=Lex;
          lex->mqh.user_conn= $2;
          lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
8643 8644
	}
        ;
8645

unknown's avatar
unknown committed
8646
begin:
unknown's avatar
unknown committed
8647 8648 8649 8650 8651 8652 8653
	BEGIN_SYM  
        {
	  LEX *lex=Lex;
          lex->sql_command = SQLCOM_BEGIN;
          lex->start_transaction_opt= 0;
        }
        opt_work {}
8654
	;
unknown's avatar
unknown committed
8655 8656 8657

opt_work:
	/* empty */ {}
unknown's avatar
unknown committed
8658
	| WORK_SYM  {}
8659
        ;
unknown's avatar
unknown committed
8660

unknown's avatar
unknown committed
8661
opt_chain:
unknown's avatar
unknown committed
8662
	/* empty */ { $$= (YYTHD->variables.completion_type == 1); }
unknown's avatar
unknown committed
8663 8664 8665 8666 8667
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
unknown's avatar
unknown committed
8668
	/* empty */ { $$= (YYTHD->variables.completion_type == 2); }
unknown's avatar
unknown committed
8669 8670 8671 8672 8673 8674 8675 8676 8677
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

unknown's avatar
unknown committed
8678
commit:
unknown's avatar
unknown committed
8679
	COMMIT_SYM opt_work opt_chain opt_release
unknown's avatar
unknown committed
8680
	{
unknown's avatar
unknown committed
8681 8682 8683 8684
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_COMMIT;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
unknown's avatar
unknown committed
8685 8686
	}
	;
unknown's avatar
unknown committed
8687 8688

rollback:
unknown's avatar
unknown committed
8689
	ROLLBACK_SYM opt_work opt_chain opt_release
8690
	{
unknown's avatar
unknown committed
8691 8692 8693 8694
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
8695
	}
unknown's avatar
unknown committed
8696 8697
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
8698
	{
unknown's avatar
unknown committed
8699 8700 8701
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK_TO_SAVEPOINT;
	  lex->ident= $5;
unknown's avatar
unknown committed
8702 8703 8704
	}
	;

8705 8706 8707
savepoint:
	SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
8708 8709 8710
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SAVEPOINT;
	  lex->ident= $2;
unknown's avatar
unknown committed
8711 8712 8713 8714 8715 8716
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
unknown's avatar
unknown committed
8717 8718 8719
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RELEASE_SAVEPOINT;
	  lex->ident= $3;
unknown's avatar
unknown committed
8720 8721
	}
	;
unknown's avatar
unknown committed
8722
  
8723
/*
unknown's avatar
unknown committed
8724
   UNIONS : glue selects together
8725 8726 8727
*/


8728
union_clause:
8729
	/* empty */ {}
unknown's avatar
unknown committed
8730 8731
	| union_list
	;
8732 8733

union_list:
8734
	UNION_SYM union_option
8735 8736 8737 8738 8739
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
unknown's avatar
unknown committed
8740
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
8741
	    YYABORT;
8742
	  }
8743
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
8744
	  {
8745
            yyerror(ER(ER_SYNTAX_ERROR));
8746 8747
	    YYABORT;
	  }
8748
	  if (mysql_new_select(lex, 0))
8749
	    YYABORT;
unknown's avatar
unknown committed
8750
          mysql_init_select(lex);
8751
	  lex->current_select->linkage=UNION_TYPE;
8752 8753 8754
          if ($2) /* UNION DISTINCT - remember position */
            lex->current_select->master_unit()->union_distinct=
                                                      lex->current_select;
unknown's avatar
unknown committed
8755
	}
unknown's avatar
unknown committed
8756 8757
	select_init
        {
8758 8759 8760 8761
          /*
	    Remove from the name resolution context stack the context of the
            last select in the union.
	  */
unknown's avatar
unknown committed
8762 8763
          Lex->pop_context();
        }
8764
	;
8765 8766

union_opt:
unknown's avatar
unknown committed
8767 8768 8769
	/* Empty */ { $$= 0; }
	| union_list { $$= 1; }
	| union_order_or_limit { $$= 1; }
unknown's avatar
unknown committed
8770
	;
8771

unknown's avatar
unknown committed
8772
union_order_or_limit:
8773
	  {
unknown's avatar
unknown committed
8774
	    THD *thd= YYTHD;
8775
	    LEX *lex= thd->lex;
unknown's avatar
unknown committed
8776
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
unknown's avatar
unknown committed
8777
	    SELECT_LEX *sel= lex->current_select;
unknown's avatar
unknown committed
8778
	    SELECT_LEX_UNIT *unit= sel->master_unit();
unknown's avatar
unknown committed
8779
	    SELECT_LEX *fake= unit->fake_select_lex;
unknown's avatar
unknown committed
8780 8781 8782 8783 8784 8785
	    if (fake)
	    {
	      unit->global_parameters= fake;
	      fake->no_table_names_allowed= 1;
	      lex->current_select= fake;
	    }
unknown's avatar
unknown committed
8786
	    thd->where= "global ORDER clause";
8787
	  }
8788
	order_or_limit
unknown's avatar
unknown committed
8789 8790
          {
	    THD *thd= YYTHD;
8791
	    thd->lex->current_select->no_table_names_allowed= 0;
unknown's avatar
unknown committed
8792 8793
	    thd->where= "";
          }
8794 8795 8796
	;

order_or_limit:
8797 8798
	order_clause opt_limit_clause_init
	| limit_clause
8799
	;
unknown's avatar
unknown committed
8800 8801

union_option:
8802 8803 8804 8805
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
8806

unknown's avatar
unknown committed
8807 8808
singlerow_subselect:
	subselect_start singlerow_subselect_init
unknown's avatar
unknown committed
8809 8810 8811 8812
	subselect_end
	{
	  $$= $2;
	};
8813

unknown's avatar
unknown committed
8814
singlerow_subselect_init:
8815
	select_init2
unknown's avatar
unknown committed
8816
	{
8817 8818
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
unknown's avatar
unknown committed
8819
	};
unknown's avatar
unknown committed
8820 8821

exists_subselect:
unknown's avatar
unknown committed
8822 8823 8824 8825 8826
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
unknown's avatar
unknown committed
8827 8828

exists_subselect_init:
8829
	select_init2
unknown's avatar
unknown committed
8830
	{
8831 8832
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
unknown's avatar
unknown committed
8833
	};
8834

unknown's avatar
unknown committed
8835 8836 8837 8838 8839 8840 8841 8842
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
8843
  select_init2
unknown's avatar
unknown committed
8844
  {
unknown's avatar
unknown committed
8845
    $$= Lex->current_select->master_unit()->first_select();
unknown's avatar
unknown committed
8846 8847
  };

8848
subselect_start:
8849
	'(' SELECT_SYM
unknown's avatar
unknown committed
8850
	{
8851
	  LEX *lex=Lex;
8852
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
8853 8854
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
8855 8856
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
8857 8858
	    YYABORT;
	  }
unknown's avatar
unknown committed
8859 8860 8861
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
8862 8863

subselect_end:
unknown's avatar
unknown committed
8864 8865 8866
	')'
	{
	  LEX *lex=Lex;
unknown's avatar
unknown committed
8867
          lex->pop_context();
8868
	  lex->current_select = lex->current_select->return_after_parsing();
unknown's avatar
unknown committed
8869
	};
unknown's avatar
unknown committed
8870

8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983
definer:
	get_definer
	{
	  THD *thd= YYTHD;
	  
	  if (! (thd->lex->definer= create_definer(thd, &$1->user, &$1->host)))
	    YYABORT;
	}
	;

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

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

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

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

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

 CREATE VIEW statement options.

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

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

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

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

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

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

view_or_trigger_tail:
	view_tail
	{}
	| trigger_tail
	{}
	;

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

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

view_list_opt:
	/* empty */
	{}
8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999
	| '(' 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)));
	  }
	;

9000 9001 9002 9003 9004 9005 9006 9007 9008
view_select:
	SELECT_SYM remember_name select_init2
	{
	  Lex->create_view_select_start= $2;
	}
	| '(' remember_name select_paren ')' union_opt
	{
	  Lex->create_view_select_start= $2;
	}
9009 9010
	;

9011
view_check_option:
9012
	/* empty */
9013 9014 9015 9016 9017 9018 9019
	{ 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; }
9020
	;
9021

9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035
/**************************************************************************

 CREATE TRIGGER statement parts.

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

trigger_tail:
	TRIGGER_SYM remember_name sp_name trg_action_time trg_event 
	ON table_ident FOR_SYM EACH_SYM ROW_SYM
	{
	  LEX *lex= Lex;
	  sp_head *sp;
	 
	  if (lex->sphead)
9036
	  {
9037 9038
	    my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "TRIGGER");
	    YYABORT;
9039
	  }
9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092
	
	  if (!(sp= new sp_head()))
	    YYABORT;
	  sp->reset_thd_mem_root(YYTHD);
	  sp->init(lex);
	
	  lex->trigger_definition_begin= $2;
	  
	  sp->m_type= TYPE_ENUM_TRIGGER;
	  lex->sphead= sp;
	  lex->spname= $3;
	  /*
	    We have to turn of CLIENT_MULTI_QUERIES while parsing a
	    stored procedure, otherwise yylex will chop it into pieces
	    at each ';'.
	  */
	  sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
	  YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
	  
	  bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	  lex->sphead->m_chistics= &lex->sp_chistics;
	  lex->sphead->m_body_begin= lex->ptr;
	}
	sp_proc_stmt
	{
	  LEX *lex= Lex;
	  sp_head *sp= lex->sphead;
	  
	  lex->sql_command= SQLCOM_CREATE_TRIGGER;
	  sp->init_strings(YYTHD, lex, $3);
	  /* Restore flag if it was cleared above */
	  if (sp->m_old_cmq)
	    YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
	  sp->restore_thd_mem_root(YYTHD);
	
	  if (sp->is_not_allowed_in_function("trigger"))
	      YYABORT;
	
	  /*
	    We have to do it after parsing trigger body, because some of
	    sp_proc_stmt alternatives are not saving/restoring LEX, so
	    lex->query_tables can be wiped out.
	    
	    QQ: What are other consequences of this?
	    
	    QQ: Could we loosen lock type in certain cases ?
	  */
	  if (!lex->select_lex.add_table_to_list(YYTHD, $7, 
	                                         (LEX_STRING*) 0,
	                                         TL_OPTION_UPDATING,
	                                         TL_WRITE))
	    YYABORT;
	}
9093 9094
	;

9095
/*************************************************************************/
9096

9097 9098
xa: XA_SYM begin_or_start xid opt_join_or_resume
      {
unknown's avatar
unknown committed
9099
        Lex->sql_command = SQLCOM_XA_START;
9100
      }
9101
    | XA_SYM END xid opt_suspend
9102
      {
unknown's avatar
unknown committed
9103
        Lex->sql_command = SQLCOM_XA_END;
9104 9105 9106
      }
    | XA_SYM PREPARE_SYM xid
      {
unknown's avatar
unknown committed
9107
        Lex->sql_command = SQLCOM_XA_PREPARE;
9108 9109 9110
      }
    | XA_SYM COMMIT_SYM xid opt_one_phase
      {
unknown's avatar
unknown committed
9111
        Lex->sql_command = SQLCOM_XA_COMMIT;
9112 9113 9114
      }
    | XA_SYM ROLLBACK_SYM xid
      {
unknown's avatar
unknown committed
9115
        Lex->sql_command = SQLCOM_XA_ROLLBACK;
9116 9117 9118
      }
    | XA_SYM RECOVER_SYM
      {
unknown's avatar
unknown committed
9119
        Lex->sql_command = SQLCOM_XA_RECOVER;
9120 9121 9122
      }
    ;

unknown's avatar
unknown committed
9123 9124
xid: text_string
     {
unknown's avatar
unknown committed
9125
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE);
unknown's avatar
unknown committed
9126 9127 9128 9129 9130 9131
       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
9132
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
unknown's avatar
unknown committed
9133 9134 9135 9136 9137 9138
       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
9139
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
unknown's avatar
unknown committed
9140 9141 9142 9143 9144
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set($5, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     ;
9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160

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

9161
opt_suspend:
9162 9163
    /* nothing */           { Lex->xa_opt=XA_NONE;        }
    | SUSPEND_SYM           { Lex->xa_opt=XA_SUSPEND;     }
9164 9165 9166 9167 9168
      opt_migrate
    ;

opt_migrate:
    /* nothing */           { }
9169 9170 9171 9172
    | FOR_SYM MIGRATE_SYM   { Lex->xa_opt=XA_FOR_MIGRATE; }
    ;