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

bk@work.mysql.com's avatar
bk@work.mysql.com 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

bk@work.mysql.com's avatar
bk@work.mysql.com 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

bk@work.mysql.com's avatar
bk@work.mysql.com 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 */

tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
17
/* sql_yacc.yy */
bk@work.mysql.com's avatar
bk@work.mysql.com 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)

bk@work.mysql.com's avatar
bk@work.mysql.com 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
bk@work.mysql.com's avatar
bk@work.mysql.com committed
33
#include "mysql_priv.h"
34
#include "slave.h"
bk@work.mysql.com's avatar
bk@work.mysql.com 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"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
41
#include <myisam.h>
42
#include <myisammrg.h>
43

44
int yylex(void *yylval, void *yythd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
45

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

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

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

serg@serg.mylan's avatar
serg@serg.mylan committed
55 56
#define TEST_ASSERT(A)                  \
  if (!(A))                             \
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)
bk@work.mysql.com's avatar
bk@work.mysql.com 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));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
69 70 71 72 73 74
}

%}
%union {
  int  num;
  ulong ulong_num;
75
  ulonglong ulonglong_number;
bk@work.mysql.com's avatar
bk@work.mysql.com 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;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
83 84
  List<Item> *item_list;
  List<String> *string_list;
85 86 87 88 89
  String *string;
  key_part_spec *key_part;
  TABLE_LIST *table_list;
  udf_func *udf;
  LEX_USER *lex_user;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
90
  struct sys_var_with_base variable;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
91
  Key::Keytype key_type;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
92
  enum ha_key_alg key_alg;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
93 94
  enum db_type db_type;
  enum row_type row_type;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
95
  enum ha_rkey_function ha_rkey_mode;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
96
  enum enum_tx_isolation tx_isolation;
97
  enum Cast_target cast_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
98
  enum Item_udftype udf_type;
99
  CHARSET_INFO *charset;
100
  thr_lock_type lock_type;
101
  interval_type interval, interval_time_st;
102
  timestamp_type date_time_type;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
103
  st_select_lex *select_lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
104
  chooser_compare_func_creator boolfunc2creator;
105 106 107 108
  struct sp_cond_type *spcondtype;
  struct { int vars, conds, hndlrs, curs; } spblock;
  sp_name *spname;
  struct st_lex *lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
109 110 111
}

%{
112
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
113 114 115 116
%}

%pure_parser					/* We have threads */

serg@serg.mylan's avatar
serg@serg.mylan committed
117
%token  END_OF_INPUT
bk@work.mysql.com's avatar
bk@work.mysql.com committed
118

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

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
674
%type <lex_str>
675
        IDENT IDENT_QUOTED TEXT_STRING DECIMAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM
676
	LEX_HOSTNAME ULONGLONG_NUM field_ident select_alias ident ident_or_text
677
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
678
	NCHAR_STRING opt_component key_cache_name
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
679
        sp_opt_label BIN_NUM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
680 681 682 683 684

%type <lex_str_ptr>
	opt_table_alias

%type <table>
685
	table_ident table_ident_nodb references xid
bk@work.mysql.com's avatar
bk@work.mysql.com committed
686 687

%type <simple_string>
688
	remember_name remember_end opt_ident opt_db text_or_password
689
	opt_constraint constraint ident_or_empty
bk@work.mysql.com's avatar
bk@work.mysql.com committed
690 691

%type <string>
692
	text_string opt_gconcat_separator
bk@work.mysql.com's avatar
bk@work.mysql.com committed
693 694

%type <num>
695
	type int_type real_type order_dir lock_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
696
	udf_type if_exists opt_local opt_table_options table_options
697 698
        table_option opt_if_not_exists opt_no_write_to_binlog opt_var_type
        opt_var_ident_type delete_option opt_temporary all_or_any opt_distinct
699
        opt_ignore_leaves fulltext_options spatial_type union_option
serg@serg.mylan's avatar
serg@serg.mylan committed
700
        start_transaction_opts opt_chain opt_release
701
        union_opt select_derived_init option_type option_type2
bk@work.mysql.com's avatar
bk@work.mysql.com committed
702 703

%type <ulong_num>
serg@serg.mylan's avatar
serg@serg.mylan committed
704
	ulong_num raid_types merge_insert_types
bk@work.mysql.com's avatar
bk@work.mysql.com committed
705

706 707
%type <ulonglong_number>
	ulonglong_num
bk@work.mysql.com's avatar
bk@work.mysql.com committed
708

709 710 711
%type <lock_type>
	replace_lock_option opt_low_priority insert_lock_option load_data_lock

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

%type <item_num>
	NUM_literal
bk@work.mysql.com's avatar
bk@work.mysql.com committed
728 729

%type <item_list>
730 731
	expr_list udf_expr_list udf_expr_list2 when_list
	ident_list ident_list_arg
bk@work.mysql.com's avatar
bk@work.mysql.com committed
732 733

%type <key_type>
734
	key_type opt_unique_or_fulltext constraint_key_type
bk@work.mysql.com's avatar
bk@work.mysql.com committed
735

736 737 738
%type <key_alg>
	key_alg opt_btree_or_rtree

bk@work.mysql.com's avatar
bk@work.mysql.com committed
739 740 741 742 743 744 745
%type <string_list>
	key_usage_list

%type <key_part>
	key_part

%type <table_list>
746
	join_table_list  join_table
747
        table_factor table_ref
748
        select_derived derived_table_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
749

750
%type <date_time_type> date_time_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
751 752
%type <interval> interval

753 754
%type <interval_time_st> interval_time_st

755
%type <db_type> storage_engines
bk@work.mysql.com's avatar
bk@work.mysql.com committed
756 757 758

%type <row_type> row_types

759
%type <tx_isolation> isolation_types
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
760

monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
761 762
%type <ha_rkey_mode> handler_rkey_mode

763
%type <cast_type> cast_type
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
764

bk@work.mysql.com's avatar
bk@work.mysql.com committed
765 766 767 768 769 770
%type <udf_type> udf_func_type

%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword

%type <lex_user> user grant_user

771
%type <charset>
772
	opt_collate
773 774
	charset_name
	charset_name_or_default
775 776
	old_or_new_charset_name
	old_or_new_charset_name_or_default
777 778
	collation_name
	collation_name_or_default
779

780 781
%type <variable> internal_variable_name

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
782
%type <select_lex> in_subselect in_subselect_init
783
	get_select_lex
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
784

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
785 786
%type <boolfunc2creator> comp_op

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

825 826 827 828 829 830 831
%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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
832 833
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
834
	',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_SYM BETWEEN_SYM CASE_SYM
835
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM OR2_SYM AND_AND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
836 837 838 839 840
%%


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

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

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

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

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


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

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

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

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
985 986
        execute_var_list ',' execute_var_ident
        | execute_var_ident
sergefp@mysql.com's avatar
sergefp@mysql.com committed
987 988 989 990 991 992 993 994
        ;

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;
995
        }
sergefp@mysql.com's avatar
sergefp@mysql.com committed
996 997
        ;

998 999
/* help */

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

/* change master */

change:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1011
       CHANGE MASTER_SYM TO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1012 1013 1014
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
1015
	  bzero((char*) &lex->mi, sizeof(lex->mi));
1016 1017 1018 1019
        }
       master_defs
	{}
       ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1020 1021 1022

master_defs:
       master_def
1023
       | master_defs ',' master_def;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1024

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
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;
       }
       |
serg@serg.mylan's avatar
serg@serg.mylan committed
1041
       MASTER_PORT_SYM EQ ulong_num
1042 1043 1044 1045
       {
	 Lex->mi.port = $3;
       }
       |
serg@serg.mylan's avatar
serg@serg.mylan committed
1046
       MASTER_CONNECT_RETRY_SYM EQ ulong_num
1047 1048 1049
       {
	 Lex->mi.connect_retry = $3;
       }
serg@serg.mylan's avatar
serg@serg.mylan committed
1050
       | MASTER_SSL_SYM EQ ulong_num
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
         {
           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;
         }
serg@serg.mylan's avatar
serg@serg.mylan committed
1104
       | RELAY_LOG_POS_SYM EQ ulong_num
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
         {
           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)
	    {
1184
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "PROCEDURE");
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 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
	      YYABORT;
	    }
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
	    sp->init(lex);

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

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

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

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

	    if (sp->check_backpatch(YYTHD))
	      YYABORT;
	    sp->init_strings(YYTHD, lex, $3);
	    lex->sql_command= SQLCOM_CREATE_PROCEDURE;
	    /* Restore flag if it was cleared above */
	    if (sp->m_old_cmq)
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
	    sp->restore_thd_mem_root(YYTHD);
	  }
	| CREATE or_replace algorithm VIEW_SYM table_ident
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_CREATE_VIEW;
	    lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE;
	    /* first table in list is target VIEW name */
	    if (!lex->select_lex.add_table_to_list(thd, $5, NULL, 0))
              YYABORT;
	  }
	  opt_view_list AS select_init check_option
	  {}
        | CREATE TRIGGER_SYM ident trg_action_time trg_event 
          ON table_ident FOR_SYM EACH_SYM ROW_SYM
          {
            LEX *lex= Lex;
            sp_head *sp;
           
            if (lex->sphead)
            {
1257
              my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "TRIGGER");
1258 1259
              YYABORT;
            }
1260 1261 1262

            if (!(sp= new sp_head()))
              YYABORT;
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
            sp->reset_thd_mem_root(YYTHD);
            sp->init(lex);
            
            sp->m_type= TYPE_ENUM_TRIGGER;
            lex->sphead= sp;
            /*
              We have to turn of CLIENT_MULTI_QUERIES while parsing a
              stored procedure, otherwise yylex will chop it into pieces
              at each ';'.
            */
            sp->m_old_cmq= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
            YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
            
            bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
            lex->sphead->m_chistics= &lex->sp_chistics;
            lex->sphead->m_body_begin= lex->tok_start;
          }
          sp_proc_stmt
          {
            LEX *lex= Lex;
            sp_head *sp= lex->sphead;
            
            lex->sql_command= SQLCOM_CREATE_TRIGGER;
            sp->init_strings(YYTHD, lex, NULL);
            /* Restore flag if it was cleared above */
            if (sp->m_old_cmq)
              YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
            sp->restore_thd_mem_root(YYTHD);

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

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

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

1330
sp_name:
1331
	  ident '.' ident
1332 1333 1334 1335
	  {
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
1336
	| ident
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
	  {
	    $$= 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)
	    {
1358
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
	      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;
1386 1387 1388 1389
	    lex->charset= NULL;
	    lex->length= lex->dec= NULL;
	    lex->interval_list.empty();
	    lex->type= 0;
1390 1391 1392 1393 1394
	  }
	  type
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
1395 1396 1397 1398 1399
            LEX_STRING cmt = { 0, 0 };
	    create_field *new_field;
	    uint unused1= 0;
	    int unused2= 0;

monty@mysql.com's avatar
monty@mysql.com committed
1400
	    if (!(new_field= new_create_field(YYTHD, (char*) "",
monty@mysql.com's avatar
monty@mysql.com committed
1401 1402 1403 1404 1405 1406 1407
					      (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)))
1408 1409
	      YYABORT;

1410 1411
	    sp->m_returns_cs= new_field->charset;

1412
            if (new_field->interval_list.elements)
1413 1414 1415 1416 1417 1418
            {
	      new_field->interval= 
                sp->create_typelib(&new_field->interval_list);
            }
            sp_prepare_create_field(YYTHD, new_field);

monty@mysql.com's avatar
monty@mysql.com committed
1419 1420
	    if (prepare_create_field(new_field, &unused1, &unused2, &unused2,
				     0))
1421 1422 1423 1424 1425 1426
	      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;
1427 1428
            sp->m_returns_typelib= new_field->interval;
            new_field->interval= NULL;
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443

	    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;

1444 1445 1446 1447 1448 1449
	    if (sp->m_multi_results)
	    {
	      my_message(ER_SP_NO_RETSET_IN_FUNC, ER(ER_SP_NO_RETSET_IN_FUNC),
	                 MYF(0));
	      YYABORT;
	    }
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
	    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; }
1493
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
	;

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();
1515
	    sp_add_to_hash(&lex->spprocs, $2);
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
	  }
          '(' 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))
	    {
1556
	      my_error(ER_SP_DUP_PARAM, MYF(0), $1.str);
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
	      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))
	    {
1582
	      my_error(ER_SP_DUP_PARAM, MYF(0), $2.str);
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
	      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 */ {}
1599
	| sp_proc_stmts  sp_proc_stmt ';'
1600 1601 1602 1603
	;

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

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

sp_decl:
1638 1639 1640
	  DECLARE_SYM sp_decl_idents type 
          { Lex->sphead->reset_lex(YYTHD); }
          sp_opt_default
1641 1642 1643 1644 1645
	  {
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    uint max= ctx->context_pvars();
	    enum enum_field_types type= (enum enum_field_types)$3;
1646
	    Item *it= $5;
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657

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

1661 1662 1663 1664
                /*
                  The last instruction is assigned to be responsible for
                  freeing LEX.
                */
1665 1666 1667 1668 1669
	        lex->sphead->add_instr(in);
	        ctx->set_isset(i, TRUE);
		ctx->set_default(i, it);
	      }
	    }
1670
            lex->sphead->restore_lex(YYTHD);
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
	    $$.vars= $2;
	    $$.conds= $$.hndlrs= $$.curs= 0;
	  }
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1681
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
	      YYABORT;
	    }
	    YYTHD->lex->spcont->push_cond(&$2, $5);
	    $$.vars= $$.hndlrs= $$.curs= 0;
	    $$.conds= 1;
	  }
	| DECLARE_SYM sp_handler_type HANDLER_SYM FOR_SYM
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_instr_hpush_jump *i=
              new sp_instr_hpush_jump(sp->instructions(), ctx, $2,
	                              ctx->current_pvars());

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

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

	      sp->add_instr(i);
	      sp->push_backpatch(i, lex->spcont->last_label()); /* Block end */
	    }
	    lex->sphead->backpatch(hlab);
	    sp->m_in_handler= FALSE;
	    $$.vars= $$.conds= $$.curs= 0;
	    $$.hndlrs= $6;
1726
	    ctx->add_handlers($6);
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
	  }
	| 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))
	    {
1738
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
	      delete $5;
	      YYABORT;
	    }
            i= new sp_instr_cpush(sp->instructions(), ctx, $5);
	    sp->add_instr(i);
	    ctx->push_cursor(&$2);
	    $$.vars= $$.conds= $$.hndlrs= 0;
	    $$.curs= 1;
	  }
	;

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

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

	    if (lex->sql_command != SQLCOM_SELECT)
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1765 1766
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
1767 1768 1769 1770
	      YYABORT;
	    }
	    if (lex->result)
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1771 1772
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
	      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;
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
	    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();
1803

1804 1805 1806 1807
	      i->add_condition($1);
	      ctx->push_handler($1);
	      $$= 1;
	    }
1808 1809 1810 1811 1812
	  }
	| sp_hcond_list ',' sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
	    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();
1824

1825 1826 1827 1828
	      i->add_condition($3);
	      ctx->push_handler($3);
	      $$= $1 + 1;
	    }
1829 1830 1831 1832
	  }
	;

sp_cond:
serg@serg.mylan's avatar
serg@serg.mylan committed
1833
	  ulong_num
1834 1835 1836 1837 1838 1839 1840
	  {			/* 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 */
1841 1842 1843 1844 1845
	    if (!sp_cond_check(&$3))
	    {
	      my_error(ER_SP_BAD_SQLSTATE, MYF(0), $3.str);
	      YYABORT;
	    }
1846 1847
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
1848 1849
	    memcpy($$->sqlstate, $3.str, 5);
	    $$->sqlstate[5]= '\0';
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
	  }
	;

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
1868
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
1869 1870 1871 1872 1873 1874 1875 1876
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
1877
	| not FOUND_SYM		/* SQLSTATEs 02??? */
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
	  {
	    $$= (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))
	    {
1897
	      my_error(ER_SP_DUP_VAR, MYF(0), $1.str);
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
	      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))
	    {
1910
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
	      YYABORT;
	    }
	    spc->push_pvar(&$3, (enum_field_types)0, sp_param_in);
	    $$= $1 + 1;
	  }
	;

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

sp_proc_stmt:
	  {
	    LEX *lex= Lex;

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

	    if ((lex->sql_command == SQLCOM_SELECT && !lex->result) ||
	        sp_multi_results_command(lex->sql_command))
	    {
	      /* We maybe have one or more SELECT without INTO */
	      sp->m_multi_results= TRUE;
	    }
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1943
	      my_message(ER_SP_NO_USE, ER(ER_SP_NO_USE), MYF(0));
1944 1945
	      YYABORT;
	    }
1946 1947 1948 1949
	    /*
              Don't add an instruction for SET statements, since all
              instructions for them were already added during processing
              of "set" rule.
1950
	    */
1951 1952 1953
            DBUG_ASSERT(lex->sql_command != SQLCOM_SET_OPTION ||
                        lex->var_list.is_empty());
            if (lex->sql_command != SQLCOM_SET_OPTION)
1954
	    {
1955 1956
              sp_instr_stmt *i=new sp_instr_stmt(sp->instructions(),
                                                 lex->spcont, lex);
1957

1958
              /* Extract the query statement from the tokenizer:
1959
                 The end is either lex->tok_end or tok->ptr. */
1960 1961 1962 1963 1964 1965 1966 1967
              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);
1968 1969 1970
            }
	    sp->restore_lex(YYTHD);
          }
1971 1972 1973
          | RETURN_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr
1974 1975
	  {
	    LEX *lex= Lex;
1976
	    sp_head *sp= lex->sphead;
1977

1978
	    if (sp->m_type == TYPE_ENUM_PROCEDURE)
1979
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1980
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
1981 1982 1983 1984 1985 1986
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_freturn *i;

1987 1988 1989 1990
	      i= new sp_instr_freturn(sp->instructions(), lex->spcont,
		                      $3, sp->m_returns, lex);
	      sp->add_instr(i);
	      sp->m_has_return= TRUE;
1991
	    }
1992
	    sp->restore_lex(YYTHD);
1993 1994 1995 1996 1997 1998 1999
	  }
	| IF sp_if END IF {}
	| CASE_SYM WHEN_SYM
	  {
	    Lex->sphead->m_simple_case= FALSE;
	  }
	  sp_case END CASE_SYM {}
2000 2001 2002
        | CASE_SYM
          { Lex->sphead->reset_lex(YYTHD); }
          expr WHEN_SYM
2003 2004 2005 2006 2007 2008 2009 2010
	  {
	    /* 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(),
2011 2012
                                               lex->spcont, offset, $3,
                                               MYSQL_TYPE_STRING, lex, TRUE);
2013
	    LEX_STRING dummy={(char*)"", 0};
2014 2015 2016 2017

	    lex->spcont->push_pvar(&dummy, MYSQL_TYPE_STRING, sp_param_in);
	    lex->sphead->add_instr(i);
	    lex->sphead->m_simple_case= TRUE;
2018
            lex->sphead->restore_lex(YYTHD);
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
	  }
	  sp_case END CASE_SYM
	  {
	    Lex->spcont->pop_pvar();
	  }
	| sp_labeled_control
	  {}
	| { /* Unlabeled controls get a secret label. */
	    LEX *lex= Lex;

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

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
	| LEAVE_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_head *sp = lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab)
	    {
2046
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
	      YYABORT;
	    }
	    else
	    {
	      uint ip= sp->instructions();
	      sp_instr_jump *i;
	      sp_instr_hpop *ih;
	      sp_instr_cpop *ic;

	      ih= new sp_instr_hpop(ip++, ctx, 0);
	      sp->push_backpatch(ih, lab);
	      sp->add_instr(ih);
	      ic= new sp_instr_cpop(ip++, ctx, 0);
	      sp->push_backpatch(ic, lab);
	      sp->add_instr(ic);
	      i= new sp_instr_jump(ip, ctx);
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
	    }
	  }
	| ITERATE_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab || lab->type != SP_LAB_ITER)
	    {
2076
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "ITERATE", $2.str);
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_jump *i;
	      uint ip= sp->instructions();
	      uint n;

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

	    if (lab)
	    {
2104
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $2.str);
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
	      YYABORT;
	    }
	    else
	    {
	      lab= ctx->push_label($2.str, sp->instructions());
	      lab->type= SP_LAB_GOTO;
	      lab->ctx= ctx;
              sp->backpatch(lab);
	    }
	  }
	| GOTO_SYM IDENT
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab;
	    sp_instr_jump *i;
	    sp_instr_hpop *ih;
	    sp_instr_cpop *ic;

	    if (sp->m_in_handler)
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2128
	      my_message(ER_SP_GOTO_IN_HNDLR, ER(ER_SP_GOTO_IN_HNDLR), MYF(0));
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
	      YYABORT;
	    }
	    lab= ctx->find_label($2.str);
	    if (! lab)
	    {
	      lab= (sp_label_t *)YYTHD->alloc(sizeof(sp_label_t));
	      lab->name= $2.str;
	      lab->ip= 0;
	      lab->type= SP_LAB_REF;
	      lab->ctx= ctx;

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

	      n= ctx->diff_handlers(lab->ctx);
	      if (n)
	      {
	        ih= new sp_instr_hpop(ip++, ctx, n);
	        sp->add_instr(ih);
	      }
	      n= ctx->diff_cursors(lab->ctx);
	      if (n)
	      {
	        ic= new sp_instr_cpop(ip++, ctx, n);
	        sp->add_instr(ic);
	      }
	      i= new sp_instr_jump(ip, ctx, lab->ip); /* Jump back */
	      sp->add_instr(i);
	    }
	  }
	| OPEN_SYM ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_copen *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2179
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
	      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))
	    {
2194
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $3.str);
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
	      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))
	    {
2211
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
	      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)))
	    {
2235
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

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

	    if (!spc || !(spv = spc->find_pvar(&$3)))
	    {
2257
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $3.str);
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

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

sp_if:
2272 2273
          { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2274 2275 2276 2277 2278
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= sp->instructions();
2279 2280
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, ctx,
                                                               $2, lex);
2281 2282 2283

	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
            sp->add_instr(i);
2284
            sp->restore_lex(YYTHD);
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
	  }
	  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:
2312 2313
	  { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2314 2315 2316 2317 2318 2319 2320 2321
	  {
            LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

	    if (! sp->m_simple_case)
2322
	      i= new sp_instr_jump_if_not(ip, ctx, $2, lex);
2323 2324 2325 2326 2327 2328 2329 2330
	    else
	    { /* Simple case: <caseval> = <whenval> */
	      LEX_STRING ivar;

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

2333
	      i= new sp_instr_jump_if_not(ip, ctx, expr, lex);
2334 2335 2336 2337
              lex->variables_used= 1;
	    }
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
            sp->add_instr(i);
2338
            sp->restore_lex(YYTHD);
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357
	  }
	  sp_proc_stmts1
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump *i = new sp_instr_jump(ip, ctx);

	    sp->add_instr(i);
	    sp->backpatch(ctx->pop_label());
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
	  }
	  sp_whens
	  {
	    LEX *lex= Lex;

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2358

2359 2360 2361 2362 2363 2364 2365
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);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2366

2367 2368 2369 2370 2371
	    sp->add_instr(i);
	  }
	| ELSE sp_proc_stmts1 {}
	| WHEN_SYM sp_case {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2372

2373 2374
sp_labeled_control:
	  IDENT ':'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2375
	  {
2376 2377 2378 2379 2380 2381
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);

	    if (lab)
	    {
2382
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $1.str);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2383
	      YYABORT;
2384 2385 2386 2387 2388 2389 2390
	    }
	    else
	    {
	      lab= lex->spcont->push_label($1.str,
	                                   lex->sphead->instructions());
	      lab->type= SP_LAB_ITER;
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2391
	  }
2392
	  sp_unlabeled_control sp_opt_label
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2393
	  {
2394
	    LEX *lex= Lex;
2395

2396 2397 2398 2399 2400 2401 2402
	    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)
	      {
2403
	        my_error(ER_SP_LABEL_MISMATCH, MYF(0), $5.str);
2404 2405 2406 2407
	        YYABORT;
	      }
	    }
	    lex->sphead->backpatch(lex->spcont->pop_label());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2408
	  }
2409 2410 2411
	;

sp_opt_label:
2412 2413
        /* Empty  */    { $$= null_lex_str; }
        | IDENT         { $$= $1; }
2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
	;

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
2430
	  {
2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445
	    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
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2446
	  {
2447 2448 2449 2450 2451 2452
	    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);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2453
	  }
2454 2455 2456
        | WHILE_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr DO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2457
	  {
2458 2459 2460 2461
	    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,
2462
							       $3, lex);
2463 2464 2465 2466

	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
            sp->add_instr(i);
2467
            sp->restore_lex(YYTHD);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2468
	  }
2469
	  sp_proc_stmts1 END WHILE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2470
	  {
2471 2472 2473 2474 2475 2476 2477
	    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);
	  }
2478 2479 2480
        | REPEAT_SYM sp_proc_stmts1 UNTIL_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr END REPEAT_SYM
2481 2482 2483 2484 2485
	  {
	    LEX *lex= Lex;
	    uint ip= lex->sphead->instructions();
	    sp_label_t *lab= lex->spcont->last_label();  /* Jumping back */
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, lex->spcont,
2486 2487
                                                               $5, lab->ip,
                                                               lex);
2488
            lex->sphead->add_instr(i);
2489
            lex->sphead->restore_lex(YYTHD);
2490
	  }
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
	;

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; }
2507
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2508 2509

create2:
serg@serg.mylan's avatar
serg@serg.mylan committed
2510 2511 2512 2513 2514 2515
        '(' create2a {}
        | opt_create_table_options create3 {}
        | LIKE table_ident
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$2))
venu@myvenu.com's avatar
venu@myvenu.com committed
2516
              YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
2517 2518 2519 2520 2521
          }
        | '(' LIKE table_ident ')'
          {
            LEX *lex=Lex;
            if (!(lex->name= (char *)$3))
2522
              YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
2523
          }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2524
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2525

serg@serg.mylan's avatar
serg@serg.mylan committed
2526 2527
create2a:
        field_list ')' opt_create_table_options create3 {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2528
	|  create_select ')' { Select->set_braces(1);} union_opt {}
2529
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2530 2531

create3:
2532
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
2533
	| opt_duplicate opt_as     create_select
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2534
          { Select->set_braces(0);} union_clause {}
serg@serg.mylan's avatar
serg@serg.mylan committed
2535
	| opt_duplicate opt_as '(' create_select ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2536
          { Select->set_braces(1);} union_opt {}
2537 2538
        ;

serg@serg.mylan's avatar
serg@serg.mylan committed
2539
create_select:
2540
          SELECT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2541
          {
2542
	    LEX *lex=Lex;
2543
	    lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
2544 2545 2546 2547
	    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;
2548 2549 2550 2551
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
2552
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
2553
	    mysql_init_select(lex);
2554
	    lex->current_select->parsing_place= SELECT_LIST;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2555
          }
2556 2557
          select_options select_item_list
	  {
2558
	    Select->parsing_place= NO_MATTER;
2559
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2560
	  opt_select_from
2561 2562 2563 2564 2565 2566 2567
	  {
	    /*
              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);
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2568
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2569

2570 2571
opt_as:
	/* empty */ {}
2572
	| AS	    {};
2573

2574 2575 2576 2577 2578 2579 2580 2581 2582
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
2583 2584
	default_collation   {}
	| default_charset   {};
2585

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2586
opt_table_options:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2587
	/* empty */	 { $$= 0; }
2588
	| table_options  { $$= $1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2589 2590 2591

table_options:
	table_option	{ $$=$1; }
2592
	| table_option table_options { $$= $1 | $2; };
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2593

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2594
table_option:
2595
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2596 2597

opt_if_not_exists:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2598
	/* empty */	 { $$= 0; }
2599
	| IF not EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2600 2601 2602

opt_create_table_options:
	/* empty */
2603
	| create_table_options;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2604

2605 2606 2607 2608
create_table_options_space_separated:
	create_table_option
	| create_table_option create_table_options_space_separated;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2609 2610
create_table_options:
	create_table_option
2611
	| create_table_option     create_table_options
2612
	| create_table_option ',' create_table_options;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2613 2614

create_table_option:
2615 2616
	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; }
2617 2618
	| 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;}
serg@serg.mylan's avatar
serg@serg.mylan committed
2619
	| 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;}
2620 2621
	| 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; }
2622
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
serg@serg.mylan's avatar
serg@serg.mylan committed
2623
	| PACK_KEYS_SYM opt_equal ulong_num	{ Lex->create_info.table_options|= $3 ? HA_OPTION_PACK_KEYS : HA_OPTION_NO_PACK_KEYS; Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;}
2624
	| 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;}
serg@serg.mylan's avatar
serg@serg.mylan committed
2625 2626
	| 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; }
2627
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
2628
	| RAID_TYPE opt_equal raid_types	{ Lex->create_info.raid_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
serg@serg.mylan's avatar
serg@serg.mylan committed
2629 2630
	| RAID_CHUNKS opt_equal ulong_num	{ Lex->create_info.raid_chunks= $3; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
	| RAID_CHUNKSIZE opt_equal ulong_num	{ Lex->create_info.raid_chunksize= $3*RAID_BLOCK_SIZE; Lex->create_info.used_fields|= HA_CREATE_USED_RAID;}
2631
	| UNION_SYM opt_equal '(' table_list ')'
2632 2633 2634
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
2635 2636
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
2637
	    lex->create_info.merge_list.elements--;
2638 2639
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
2640
	    lex->select_lex.table_list.elements=1;
2641 2642 2643
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
2644
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
2645
	  }
2646 2647
	| default_charset
	| default_collation
2648
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
2649 2650 2651
	| 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; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2652

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

2686
storage_engines:
2687 2688 2689 2690
	ident_or_text
	{
	  $$ = ha_resolve_by_name($1.str,$1.length);
	  if ($$ == DB_TYPE_UNKNOWN) {
2691
	    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
2692 2693 2694
	    YYABORT;
	  }
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2695 2696 2697 2698 2699

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
2700 2701 2702
	| COMPRESSED_SYM { $$= ROW_TYPE_COMPRESSED; }
	| REDUNDANT_SYM	{ $$= ROW_TYPE_REDUNDANT; }
	| COMPACT_SYM	{ $$= ROW_TYPE_COMPACT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2703 2704 2705 2706

raid_types:
	RAID_STRIPED_SYM { $$= RAID_TYPE_0; }
	| RAID_0_SYM	 { $$= RAID_TYPE_0; }
serg@serg.mylan's avatar
serg@serg.mylan committed
2707
	| ulong_num	 { $$=$1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2708

2709 2710 2711
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
2712
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
2713

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2714
opt_select_from:
2715
	opt_limit_clause {}
2716
	| select_from select_lock_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2717 2718

udf_func_type:
2719
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
2720
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2721 2722 2723 2724

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
2725
        | DECIMAL_SYM {$$ = (int) DECIMAL_RESULT; }
2726
	| INT_SYM {$$ = (int) INT_RESULT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2727 2728 2729

field_list:
	  field_list_item
2730
	| field_list ',' field_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2731 2732 2733


field_list_item:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2734
	   column_def
2735 2736 2737 2738
         | key_def
         ;

column_def:
2739
	  field_spec opt_check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2740 2741 2742 2743
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2744
	;
2745 2746

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

2785
opt_check_constraint:
2786
	/* empty */
2787 2788 2789 2790 2791
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
2792
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2793 2794

opt_constraint:
2795
	/* empty */		{ $$=(char*) 0; }
monty@mysql.com's avatar
monty@mysql.com committed
2796 2797 2798 2799 2800 2801
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2802 2803 2804 2805

field_spec:
	field_ident
	 {
2806
	   LEX *lex=Lex;
2807
	   lex->length=lex->dec=0; lex->type=0;
2808
	   lex->default_value= lex->on_update_value= 0;
2809
           lex->comment=null_lex_str;
2810
	   lex->charset=NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2811 2812 2813
	 }
	type opt_attribute
	{
2814
	  LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2815
	  if (add_field_to_list(lex->thd, $1.str,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2816
				(enum enum_field_types) $3,
2817
				lex->length,lex->dec,lex->type,
2818
				lex->default_value, lex->on_update_value, 
2819
                                &lex->comment,
2820
				lex->change,&lex->interval_list,lex->charset,
2821
				lex->uint_geom_type))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2822
	    YYABORT;
2823
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2824 2825

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

hf@deer.(none)'s avatar
hf@deer.(none) committed
2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
	| POINT_SYM           { $$= Field::GEOM_POINT; }
	| MULTIPOINT          { $$= Field::GEOM_MULTIPOINT; }
	| LINESTRING          { $$= Field::GEOM_LINESTRING; }
	| MULTILINESTRING     { $$= Field::GEOM_MULTILINESTRING; }
	| POLYGON             { $$= Field::GEOM_POLYGON; }
	| MULTIPOLYGON        { $$= Field::GEOM_MULTIPOLYGON; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2935 2936
char:
	CHAR_SYM {}
2937 2938 2939 2940 2941 2942
	;

nchar:
	NCHAR_SYM {}
	| NATIONAL_SYM CHAR_SYM {}
	;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2943

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2944 2945 2946
varchar:
	char VARYING {}
	| VARCHAR {}
2947 2948 2949 2950
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2951
	| NVARCHAR_SYM {}
2952 2953 2954 2955
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2956 2957 2958 2959 2960 2961

int_type:
	INT_SYM		{ $$=FIELD_TYPE_LONG; }
	| TINYINT	{ $$=FIELD_TYPE_TINY; }
	| SMALLINT	{ $$=FIELD_TYPE_SHORT; }
	| MEDIUMINT	{ $$=FIELD_TYPE_INT24; }
2962
	| BIGINT	{ $$=FIELD_TYPE_LONGLONG; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2963 2964

real_type:
2965
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2966 2967
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
2968
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2969 2970 2971


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

precision:
	'(' NUM ',' NUM ')'
	{
	  LEX *lex=Lex;
	  lex->length=$2.str; lex->dec=$4.str;
2981
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2982 2983 2984

field_options:
	/* empty */		{}
2985
	| field_opt_list	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2986 2987 2988

field_opt_list:
	field_opt_list field_option {}
2989
	| field_option {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2990 2991

field_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2992
	SIGNED_SYM	{}
2993
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
2994
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2995 2996

opt_len:
2997 2998
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2999 3000 3001

opt_precision:
	/* empty */	{}
3002
	| precision	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3003 3004 3005

opt_attribute:
	/* empty */ {}
3006
	| opt_attribute_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3007 3008 3009

opt_attribute_list:
	opt_attribute_list attribute {}
3010
	| attribute;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3011 3012 3013

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

3060 3061 3062 3063 3064
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

3065 3066 3067 3068
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
3069

3070
charset_name:
3071
	ident_or_text
3072
	{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3073
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
3074
	  {
3075
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
3076 3077
	    YYABORT;
	  }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3078 3079 3080
	}
	| BINARY { $$= &my_charset_bin; }
	;
3081

3082 3083 3084
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3085

3086 3087 3088 3089 3090 3091 3092

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)))
	  {
3093
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
3094 3095 3096 3097 3098 3099 3100 3101 3102 3103
	    YYABORT;
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

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

3114 3115
opt_collate:
	/* empty */	{ $$=NULL; }
3116
	| COLLATE_SYM collation_name_or_default { $$=$2; }
3117 3118
	;

3119 3120 3121 3122
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

3123 3124 3125 3126
opt_default:
	/* empty */	{}
	| DEFAULT	{};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3127
opt_binary:
3128
	/* empty */			{ Lex->charset=NULL; }
3129 3130
	| ASCII_SYM			{ Lex->charset=&my_charset_latin1; }
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
3131 3132
	| UNICODE_SYM
	{
3133 3134
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
3135
	  {
3136
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
3137 3138 3139
	    YYABORT;
	  }
	}
3140
	| charset charset_name	{ Lex->charset=$2; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3141

3142 3143 3144
opt_primary:
	/* empty */
	| PRIMARY_SYM
3145
	;
3146

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3147
references:
3148 3149
	REFERENCES table_ident
	{
3150
	  LEX *lex=Lex;
3151 3152 3153 3154 3155 3156
	  lex->fk_delete_opt= lex->fk_update_opt= lex->fk_match_option= 0;
	  lex->ref_list.empty();
	}
	opt_ref_list
	{
	  $$=$2;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3157
	};
3158

3159
opt_ref_list:
3160
	/* empty */ opt_on_delete {}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3161
	| '(' ref_list ')' opt_on_delete {};
3162 3163 3164

ref_list:
	ref_list ',' ident	{ Lex->ref_list.push_back(new key_part_spec($3.str)); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3165
	| ident			{ Lex->ref_list.push_back(new key_part_spec($1.str)); };
3166

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3167 3168 3169

opt_on_delete:
	/* empty */ {}
3170
	| opt_on_delete_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3171 3172 3173

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
3174
	| opt_on_delete_item {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3175 3176

opt_on_delete_item:
3177 3178 3179 3180
	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; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3181
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3182 3183

delete_option:
3184 3185 3186 3187
	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; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3188
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3189 3190

key_type:
3191
	key_or_index			    { $$= Key::MULTIPLE; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
3192 3193
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
hf@deer.(none)'s avatar
hf@deer.(none) committed
3194 3195 3196 3197
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
3198 3199
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
3200 3201 3202
	    YYABORT;
#endif
	  };
3203 3204 3205

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
3206
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3207 3208 3209

key_or_index:
	KEY_SYM {}
3210
	| INDEX_SYM {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3211

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
3212 3213 3214 3215 3216
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3217 3218
keys_or_index:
	KEYS {}
3219
	| INDEX_SYM {}
3220
	| INDEXES {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3221

3222
opt_unique_or_fulltext:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3223 3224
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
serg@serg.mylan's avatar
serg@serg.mylan committed
3225
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
hf@deer.(none)'s avatar
hf@deer.(none) committed
3226 3227 3228 3229 3230
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
serg@serg.mylan's avatar
serg@serg.mylan committed
3231
            my_error(ER_FEATURE_DISABLED, MYF(0),
3232
                     sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
3233 3234 3235
	    YYABORT;
#endif
	  }
serg@serg.mylan's avatar
serg@serg.mylan committed
3236
        ;
3237 3238

key_alg:
3239
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
3240
	| USING opt_btree_or_rtree { $$= $2; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3241
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
3242 3243 3244

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
hf@deer.(none)'s avatar
hf@deer.(none) committed
3245 3246 3247 3248
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3249
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3250 3251 3252

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
3253
	| key_part order_dir		{ Lex->col_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3254 3255 3256

key_part:
	ident			{ $$=new key_part_spec($1.str); }
3257 3258 3259 3260 3261
	| ident '(' NUM ')'	
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
3262
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
3263 3264 3265
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3266 3267 3268

opt_ident:
	/* empty */	{ $$=(char*) 0; }	/* Defaultlength */
3269
	| field_ident	{ $$=$1.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3270

3271
opt_component:
3272 3273
        /* empty */      { $$= null_lex_str; }
        | '.' ident      { $$= $2; };
3274

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3275 3276
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
3277
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3278 3279 3280 3281 3282 3283 3284 3285

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
3286
	  THD *thd= YYTHD;
3287
	  LEX *lex= thd->lex;
3288 3289 3290
	  lex->sql_command= SQLCOM_ALTER_TABLE;
	  lex->name= 0;
	  lex->duplicates= DUP_ERROR; 
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3291 3292
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3293 3294 3295 3296
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
3297
          lex->select_lex.init_order();
3298
	  lex->select_lex.db=lex->name=0;
3299
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3300
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
3301
	  lex->create_info.default_table_charset= NULL;
3302
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
3303
	  lex->alter_info.reset();
3304
	  lex->alter_info.flags= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3305
	}
3306 3307
	alter_list
	{}
3308
	| ALTER DATABASE ident_or_empty
3309 3310 3311 3312 3313
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
          opt_create_database_options
3314 3315 3316
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
3317
	    lex->name= $3;
3318 3319 3320 3321 3322
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

3323 3324 3325 3326 3327
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
	      YYABORT;
	    }
3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339
	    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;
3340

3341 3342 3343 3344 3345
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
	      YYABORT;
	    }
3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    LEX *lex=Lex;

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

3369 3370 3371 3372
ident_or_empty:
	/* empty */  { $$= 0; }
	| ident      { $$= $1.str; };

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3373
alter_list:
3374 3375
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
3376
        | alter_list_item
3377
	| alter_list ',' alter_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3378 3379

add_column:
3380
	ADD opt_column
3381 3382
	{
	  LEX *lex=Lex;
3383 3384
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
3385
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3386 3387

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

opt_column:
	/* empty */	{}
3523
	| COLUMN_SYM	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3524 3525

opt_ignore:
3526 3527 3528
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3529 3530

opt_restrict:
3531 3532 3533 3534
	/* empty */	{ Lex->drop_mode= DROP_DEFAULT; }
	| RESTRICT	{ Lex->drop_mode= DROP_RESTRICT; }
	| CASCADE	{ Lex->drop_mode= DROP_CASCADE; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3535 3536 3537 3538

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
3539
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3540 3541 3542 3543

opt_to:
	/* empty */	{}
	| TO_SYM	{}
3544
	| EQ		{}
3545
	| AS		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3546

nick@mysql.com's avatar
nick@mysql.com committed
3547
/*
3548
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3549 3550
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3551
slave:
3552
	  START_SYM SLAVE slave_thread_opts
3553 3554 3555 3556 3557 3558
          {
	    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));
guilhem@gbichot2.local's avatar
guilhem@gbichot2.local committed
3559
            /* If you change this code don't forget to update SLAVE START too */
3560 3561 3562
          }
          slave_until
          {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3563 3564 3565 3566 3567
        | STOP_SYM SLAVE slave_thread_opts
          {
	    LEX *lex=Lex;
            lex->sql_command = SQLCOM_SLAVE_STOP;
	    lex->type = 0;
guilhem@gbichot2.local's avatar
guilhem@gbichot2.local committed
3568
            /* If you change this code don't forget to update SLAVE STOP too */
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3569
          }
3570 3571 3572 3573 3574
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
3575 3576 3577 3578 3579
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
3580 3581 3582 3583 3584 3585 3586 3587
	| SLAVE STOP_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_STOP;
	   lex->type = 0;
         }
        ;

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3588

3589
start:
3590 3591 3592 3593 3594
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
           Lex->sql_command = SQLCOM_BEGIN;
           Lex->start_transaction_opt= $3;
        }
3595 3596
	;

3597 3598 3599 3600 3601 3602
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
3603
        ;
3604

3605
slave_thread_opts:
3606 3607
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
3608
        {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3609
	;
3610 3611

slave_thread_opt_list:
3612
	slave_thread_opt
3613 3614
	| slave_thread_opt_list ',' slave_thread_opt
	;
3615 3616

slave_thread_opt:
3617
	/*empty*/	{}
3618
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
3619
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
3620
	;
3621

3622 3623 3624 3625 3626 3627 3628 3629 3630 3631
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)))
            {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3632 3633
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


3645 3646 3647 3648 3649
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3650
	table_list FROM TEXT_STRING_sys
3651 3652
        {
	  Lex->backup_dir = $6.str;
3653
        };
3654

3655 3656 3657 3658 3659
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3660
	table_list TO_SYM TEXT_STRING_sys
3661 3662
        {
	  Lex->backup_dir = $6.str;
3663
        };
3664

3665 3666 3667 3668 3669 3670
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
3671 3672
	table_list opt_checksum_type
        {}
3673 3674
	;

3675 3676 3677 3678 3679 3680
opt_checksum_type:
        /* nothing */  { Lex->check_opt.flags= 0; }
	| QUICK        { Lex->check_opt.flags= T_QUICK; }
	| EXTENDED_SYM { Lex->check_opt.flags= T_EXTEND; }
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3681
repair:
3682
	REPAIR opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3683
	{
3684 3685
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
3686
           lex->no_write_to_binlog= $2;
3687
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3688
	}
3689 3690 3691
	table_list opt_mi_repair_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3692

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3693
opt_mi_repair_type:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3694
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
3695
	| mi_repair_types {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3696

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3697 3698
mi_repair_types:
	mi_repair_type {}
3699
	| mi_repair_type mi_repair_types {};
3700

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3701
mi_repair_type:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3702
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
3703
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
3704
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3705 3706

analyze:
3707
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3708
	{
3709 3710
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
3711
           lex->no_write_to_binlog= $2;
3712
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3713
	}
3714 3715 3716
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3717 3718

check:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3719
	CHECK_SYM table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3720
	{
3721 3722 3723 3724 3725 3726 3727 3728 3729
	  LEX *lex=Lex;

	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "CHECK");
	    YYABORT;
	  }
	  lex->sql_command = SQLCOM_CHECK;
	  lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3730
	}
3731 3732 3733
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3734

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3735 3736
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
3737
	| mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3738 3739 3740

mi_check_types:
	mi_check_type {}
3741
	| mi_check_type mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3742 3743 3744 3745 3746 3747

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; }
3748
	| CHANGED  { Lex->check_opt.flags|= T_CHECK_ONLY_CHANGED; };
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3749

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3750
optimize:
3751
	OPTIMIZE opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3752
	{
3753 3754
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3755
           lex->no_write_to_binlog= $2;
3756
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3757
	}
3758 3759 3760
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3761

3762 3763 3764
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
3765
	| LOCAL_SYM  { $$= 1; }
3766 3767
	;

3768 3769 3770 3771 3772
rename:
	RENAME table_or_tables
	{
	   Lex->sql_command=SQLCOM_RENAME_TABLE;
	}
3773 3774
	table_to_table_list
	{}
3775 3776 3777 3778
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
3779
	;
3780

3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793
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;
          }
        ;

3794 3795
table_to_table_list:
	table_to_table
3796
	| table_to_table_list ',' table_to_table;
3797 3798 3799

table_to_table:
	table_ident TO_SYM table_ident
3800
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3801
	  LEX *lex=Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3802
	  SELECT_LEX *sl= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3803 3804 3805 3806
	  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))
3807
	    YYABORT;
3808
	};
3809

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3810
keycache:
3811
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3812 3813
        {
          LEX *lex=Lex;
3814
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
3815
	  lex->ident= $5;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3816 3817 3818 3819 3820 3821 3822 3823
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
3824
        table_ident cache_keys_spec
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3825 3826 3827 3828 3829 3830
        {
          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(),
3831
                                      (List<String> *)0))
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3832 3833 3834 3835
            YYABORT;
        }
        ;

3836 3837 3838
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
3839
	;
3840

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3841
preload:
3842
	LOAD INDEX_SYM INTO CACHE_SYM
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855
	{
	  LEX *lex=Lex;
	  lex->sql_command=SQLCOM_PRELOAD_KEYS;
	}
	preload_list
	{}
	;

preload_list:
	preload_keys
	| preload_list ',' preload_keys;

preload_keys:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3856
	table_ident cache_keys_spec opt_ignore_leaves
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3857 3858 3859
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3860 3861
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3862 3863 3864 3865 3866 3867
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

3868
cache_keys_spec:
3869
        { Select->interval_list.empty(); }
3870 3871 3872 3873 3874 3875 3876
        cache_key_list_or_empty
        {
          LEX *lex=Lex;
          SELECT_LEX *sel= &lex->select_lex;
          sel->use_index= sel->interval_list;
        }
        ;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3877

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3878
cache_key_list_or_empty:
3879
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
3880
	| opt_key_or_index '(' key_usage_list2 ')'
3881 3882 3883 3884
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3885 3886
	;

3887
opt_ignore_leaves:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3888 3889 3890 3891 3892
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3893
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3894
  Select : retrieve data from table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3895 3896 3897 3898
*/


select:
3899 3900 3901 3902 3903
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
	  lex->select_lex.resolve_mode= SELECT_LEX::SELECT_MODE;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3904 3905
	}
	;
3906

3907
/* Need select_init2 for subselects. */
3908
select_init:
3909
	SELECT_SYM select_init2
3910
	|
3911 3912 3913 3914
	'(' select_paren ')' union_opt;

select_paren:
	SELECT_SYM select_part2
3915
	  {
3916
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3917
            SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3918
	    if (sel->set_braces(1))
3919
	    {
3920
	      yyerror(ER(ER_SYNTAX_ERROR));
3921 3922
	      YYABORT;
	    }
3923 3924
	  if (sel->linkage == UNION_TYPE &&
	      !sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3925
	  {
3926
	    yyerror(ER(ER_SYNTAX_ERROR));
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3927 3928
	    YYABORT;
	  }
3929
            /* select in braces, can't contain global parameters */
3930 3931 3932
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
3933 3934
          }
	| '(' select_paren ')';
3935

3936 3937
select_init2:
	select_part2
3938
        {
3939
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3940
          SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3941
          if (lex->current_select->set_braces(0))
3942
	  {
3943
	    yyerror(ER(ER_SYNTAX_ERROR));
3944 3945
	    YYABORT;
	  }
3946 3947
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3948
	  {
3949
	    yyerror(ER(ER_SYNTAX_ERROR));
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3950 3951
	    YYABORT;
	  }
3952
	}
3953
	union_clause
3954 3955
	;

3956
select_part2:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3957
	{
3958 3959
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
3960 3961
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
3962
	  lex->current_select->parsing_place= SELECT_LIST;
3963 3964 3965
	}
	select_options select_item_list
	{
3966
	  Select->parsing_place= NO_MATTER;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3967
	}
3968
	select_into select_lock_type;
3969

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3970
select_into:
3971 3972
	opt_limit_clause {}
        | into
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3973
	| select_from
3974 3975
	| into select_from
	| select_from into;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3976 3977

select_from:
3978 3979
	  FROM join_table_list where_clause group_clause having_clause
	       opt_order_clause opt_limit_clause procedure_clause
timour@mysql.com's avatar
timour@mysql.com committed
3980 3981 3982 3983 3984
        | FROM DUAL_SYM opt_limit_clause
          /* oracle compatibility: oracle always requires FROM clause,
             and DUAL is system table without fields.
             Is "SELECT 1 FROM DUAL" any better than "SELECT 1" ?
          Hmmm :) */
3985
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3986 3987 3988

select_options:
	/* empty*/
monty@mysql.com's avatar
monty@mysql.com committed
3989 3990 3991 3992
	| select_option_list
	  {
	    if (test_all_bits(Select->options, SELECT_ALL | SELECT_DISTINCT))
	    {
monty@mysql.com's avatar
monty@mysql.com committed
3993
	      my_error(ER_WRONG_USAGE, MYF(0), "ALL", "DISTINCT");
monty@mysql.com's avatar
monty@mysql.com committed
3994 3995 3996
              YYABORT;
	    }
          }
monty@mysql.com's avatar
monty@mysql.com committed
3997
	  ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3998 3999 4000

select_option_list:
	select_option_list select_option
4001
	| select_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4002 4003

select_option:
4004
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
4005 4006 4007 4008 4009 4010
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
monty@mysql.com's avatar
monty@mysql.com committed
4011
	| DISTINCT         { Select->options|= SELECT_DISTINCT; }
4012 4013
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025
	| 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;
	  }
4026
	| SQL_NO_CACHE_SYM { Lex->safe_to_cache_query=0; }
4027 4028 4029 4030
	| SQL_CACHE_SYM
	  {
	    Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
	  }
monty@mysql.com's avatar
monty@mysql.com committed
4031
	| ALL		    { Select->options|= SELECT_ALL; }
4032
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4033

monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
4034 4035 4036
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
4037 4038
	  {
	    LEX *lex=Lex;
4039
	    lex->current_select->set_lock_for_tables(TL_WRITE);
4040
	    lex->safe_to_cache_query=0;
4041
	  }
4042
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
4043 4044
	  {
	    LEX *lex=Lex;
4045 4046
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
4047
	    lex->safe_to_cache_query=0;
4048 4049
	  }
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
4050

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4051 4052 4053 4054 4055
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
4056 4057
	    THD *thd= YYTHD;
	    if (add_item_to_list(thd, new Item_field(NULL, NULL, "*")))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4058
	      YYABORT;
4059
	    (thd->lex->current_select->with_wild)++;
4060
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4061 4062 4063 4064 4065


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4066
	    if (add_item_to_list(YYTHD, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4067 4068
	      YYABORT;
	    if ($4.str)
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4069
	      $2->set_name($4.str,$4.length,system_charset_info);
4070 4071 4072 4073 4074 4075
	    else if (!$2->name) {
	      char *str = $1;
	      if (str[-1] == '`')
	        str--;
	      $2->set_name(str,(uint) ($3 - str), YYTHD->charset());
	    }
4076
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4077 4078

remember_name:
4079
	{ $$=(char*) Lex->tok_start; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4080 4081

remember_end:
4082
	{ $$=(char*) Lex->tok_end; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4083 4084 4085

select_item2:
	table_wild	{ $$=$1; } /* table.* */
4086
	| expr		{ $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4087 4088

select_alias:
4089
	/* empty */		{ $$=null_lex_str;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4090 4091 4092 4093 4094
	| AS ident		{ $$=$2; }
	| AS TEXT_STRING_sys	{ $$=$2; }
	| ident			{ $$=$1; }
	| TEXT_STRING_sys	{ $$=$1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4095 4096 4097

optional_braces:
	/* empty */ {}
4098
	| '(' ')' {};
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4099

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4100
/* all possible expressions */
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
4101
expr:	
4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123
	  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); }
        ;
4124 4125

bool_term:
4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148
	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); }
        ;
4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165

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); }
4166 4167 4168 4169 4170
	| 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); }
4171 4172 4173 4174 4175 4176
	| predicate ;

predicate:
	 bit_expr IN_SYM '(' expr_list ')'
	  { $4->push_front($1); $$= new Item_func_in(*$4); }
	| bit_expr not IN_SYM '(' expr_list ')'
4177
	  { $5->push_front($1); $$= negate_expression(YYTHD, new Item_func_in(*$5)); }
4178 4179 4180
        | bit_expr IN_SYM in_subselect
	  { $$= new Item_in_subselect($1, $3); }
	| bit_expr not IN_SYM in_subselect
4181
          { $$= negate_expression(YYTHD, new Item_in_subselect($1, $4)); }
4182 4183 4184 4185
	| bit_expr BETWEEN_SYM bit_expr AND_SYM predicate
	  { $$= new Item_func_between($1,$3,$5); }
	| bit_expr not BETWEEN_SYM bit_expr AND_SYM predicate
	  { $$= negate_expression(YYTHD, new Item_func_between($1,$4,$6)); }
4186 4187 4188 4189 4190 4191 4192 4193 4194
	| bit_expr SOUNDS_SYM LIKE bit_expr
	  { $$= new Item_func_eq(new Item_func_soundex($1),
				 new Item_func_soundex($4)); }
	| bit_expr LIKE simple_expr opt_escape
          { $$= new Item_func_like($1,$3,$4); }
	| bit_expr not LIKE simple_expr opt_escape
          { $$= new Item_func_not(new Item_func_like($1,$4,$5)); }
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
4195
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237
	| 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;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4238

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250
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; }
        ;

4251
interval_expr:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4252
         INTERVAL_SYM expr { $$=$2; }
4253 4254
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4255 4256
simple_expr:
	simple_ident
4257
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4258
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4259 4260 4261
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
4262
                                                            YYTHD->charset()));
4263
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4264
	| literal
4265
	| param_marker
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4266
	| '@' ident_or_text SET_VAR expr
4267 4268
	  {
	    $$= new Item_func_set_user_var($2,$4);
4269 4270 4271
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4272
	  }
4273
	| '@' ident_or_text
4274 4275
	  {
	    $$= new Item_func_get_user_var($2);
4276 4277 4278
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4279
	  }
4280
	| '@' '@' opt_var_ident_type ident_or_text opt_component
4281
	  {
4282 4283 4284 4285 4286 4287

            if ($4.str && $5.str && check_reserved_words(&$4))
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
4288
	    if (!($$= get_system_var(YYTHD, (enum_var_type) $3, $4, $5)))
4289
	      YYABORT;
4290
	    Lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4291
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4292
	| sum_expr
4293 4294 4295 4296 4297 4298
	| 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); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4299
	| '(' expr ')'		{ $$= $2; }
4300 4301 4302 4303 4304
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
4305
	| ROW_SYM '(' expr ',' expr_list ')'
4306
	  {
4307 4308
	    $5->push_front($3);
	    $$= new Item_row(*$5);
4309
	  }
4310
	| EXISTS exists_subselect { $$= $2; }
4311
	| singlerow_subselect   { $$= $1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4312
	| '{' ident expr '}'	{ $$= $3; }
4313
        | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
4314
          { $2->push_front($5);
4315 4316
            Select->add_ftfunc_to_list((Item_func_match*)
                                        ($$=new Item_func_match(*$2,$6))); }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4317
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
4318
	| BINARY simple_expr %prec NEG
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4319
	  {
4320
            $$= create_func_cast($2, ITEM_CAST_CHAR, -1, 0, &my_charset_bin);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4321
	  }
4322
	| CAST_SYM '(' expr AS cast_type ')'
4323
	  {
4324
            LEX *lex= Lex;
4325
	    $$= create_func_cast($3, $5,
4326 4327 4328
                                 lex->length ? atoi(lex->length) : -1,
                                 lex->dec ? atoi(lex->dec) : 0,
                                 lex->charset);
4329
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4330
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
4331
	  { $$= new Item_func_case(* $4, $2, $5 ); }
4332
	| CONVERT_SYM '(' expr ',' cast_type ')'
4333 4334 4335
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
4336
                                 Lex->dec ? atoi(Lex->dec) : 0,
4337 4338
				 Lex->charset);
	  }
4339 4340
	| CONVERT_SYM '(' expr USING charset_name ')'
	  { $$= new Item_func_conv_charset($3,$5); }
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
4341 4342
	| DEFAULT '(' simple_ident ')'
	  { $$= new Item_default_value($3); }
4343 4344
	| VALUES '(' simple_ident ')'
	  { $$= new Item_insert_value($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4345
	| FUNC_ARG0 '(' ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
4346 4347 4348
	  {
	    if (!$1.symbol->create_func)
	    {
4349 4350 4351
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
4352 4353 4354 4355
	      YYABORT;
	    }
	    $$= ((Item*(*)(void))($1.symbol->create_func))();
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4356
	| FUNC_ARG1 '(' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
4357 4358 4359
	  {
	    if (!$1.symbol->create_func)
	    {
4360 4361 4362
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
4363 4364 4365 4366
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4367
	| FUNC_ARG2 '(' expr ',' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
4368 4369 4370
	  {
	    if (!$1.symbol->create_func)
	    {
4371 4372 4373
	      my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
4374 4375 4376 4377
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4378
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
4379 4380 4381
	  {
	    if (!$1.symbol->create_func)
	    {
4382 4383 4384
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
4385 4386 4387 4388
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*,Item*))($1.symbol->create_func))($3,$5,$7);
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4389 4390 4391 4392
	| 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); }
4393 4394
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4395 4396 4397 4398 4399 4400
	| 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); }
4401 4402
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4403 4404
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
4405 4406
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4407 4408 4409
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
4410
	  { $5->push_front($3); $$= new Item_func_concat_ws(*$5); }
4411 4412
	| CONVERT_TZ_SYM '(' expr ',' expr ',' expr ')'
	  {
4413 4414
            if (Lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
4415 4416
	    $$= new Item_func_convert_tz($3, $5, $7);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4417
	| CURDATE optional_braces
4418
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4419
	| CURTIME optional_braces
4420
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4421
	| CURTIME '(' expr ')'
4422
	  {
4423
	    $$= new Item_func_curtime_local($3);
4424
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4425
	  }
4426
	| CURRENT_USER optional_braces
4427
          { $$= create_func_current_user(); }
4428 4429 4430 4431
	| 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); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4432
	| DATABASE '(' ')'
4433
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4434
	    $$= new Item_func_database();
4435
            Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4436
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4437
	| DATE_SYM '(' expr ')'
4438
	  { $$= new Item_date_typecast($3); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4439 4440
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4441
	| ELT_FUNC '(' expr ',' expr_list ')'
4442
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4443 4444
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4445 4446 4447
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
4448
	    Lex->uncacheable(UNCACHEABLE_RAND);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4449
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4450
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
4451
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4452
	  { $$= new Item_func_decode($3,$5.str); }
4453
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4454
	 { $$= new Item_func_encode($3,$5.str); }
4455
	| DES_DECRYPT_SYM '(' expr ')'
4456
        { $$= new Item_func_des_decrypt($3); }
4457
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
4458
        { $$= new Item_func_des_decrypt($3,$5); }
4459
	| DES_ENCRYPT_SYM '(' expr ')'
4460
        { $$= new Item_func_des_encrypt($3); }
4461
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
4462
        { $$= new Item_func_des_encrypt($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474
	| 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 ')'
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4475
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4476 4477
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
4478
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4479 4480 4481 4482 4483
	| geometry_function
	  {
#ifdef HAVE_SPATIAL
	    $$= $1;
#else
4484 4485
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
4486 4487 4488
	    YYABORT;
#endif
	  }
4489
	| GET_FORMAT '(' date_time_type  ',' expr ')'
4490
	  { $$= new Item_func_get_format($3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4491 4492 4493 4494 4495 4496
	| 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); }
4497
	| interval_expr interval '+' expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4498
	  /* we cannot put interval before - */
4499 4500 4501 4502 4503
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
4504
              yyerror(ER(ER_SYNTAX_ERROR));
4505 4506 4507 4508
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4509 4510
	| LAST_INSERT_ID '(' ')'
	  {
4511
	    $$= new Item_func_last_insert_id();
4512
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4513 4514 4515
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
4516
	    $$= new Item_func_last_insert_id($3);
4517
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4518 4519 4520 4521 4522 4523 4524
	  }
	| 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); }
4525
	| GREATEST_SYM '(' expr ',' expr_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4526 4527 4528
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
4529
	| LOG_SYM '(' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4530
	  { $$= new Item_func_log($3); }
4531
	| LOG_SYM '(' expr ',' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4532
	  { $$= new Item_func_log($3, $5); }
4533
	| MASTER_POS_WAIT '(' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4534
	  {
4535
	    $$= new Item_master_pos_wait($3, $5);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4536
	    Lex->safe_to_cache_query=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4537
		  }
4538
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4539
	  {
4540
	    $$= new Item_master_pos_wait($3, $5, $7);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4541
	    Lex->safe_to_cache_query=0;
4542
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4543 4544
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4545 4546
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
4547 4548
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4549 4550 4551
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
	| NOW_SYM optional_braces
4552
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4553
	| NOW_SYM '(' expr ')'
4554
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4555
	| PASSWORD '(' expr ')'
4556
	  {
4557 4558
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
4559 4560 4561 4562
	      (Item *) new Item_func_password($3);
	  }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new Item_func_old_password($3); }
4563
	| POSITION_SYM '(' bit_expr IN_SYM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4564
	  { $$ = new Item_func_locate($5,$3); }
4565 4566
	| QUARTER_SYM '(' expr ')'
	  { $$ = new Item_func_quarter($3); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4567
	| RAND '(' expr ')'
4568
	  { $$= new Item_func_rand($3); Lex->uncacheable(UNCACHEABLE_RAND);}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4569
	| RAND '(' ')'
4570
	  { $$= new Item_func_rand(); Lex->uncacheable(UNCACHEABLE_RAND);}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4571 4572 4573 4574 4575 4576 4577
	| 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); }
4578 4579 4580 4581 4582
	| ROW_COUNT_SYM '(' ')'
	  {
	    $$= new Item_func_row_count();
	    Lex->safe_to_cache_query= 0;
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4583 4584 4585 4586
	| 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); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598
	| 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); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4599
	| TIME_SYM '(' expr ')'
4600 4601 4602
	  { $$= new Item_time_typecast($3); }
	| TIMESTAMP '(' expr ')'
	  { $$= new Item_datetime_typecast($3); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4603
	| TIMESTAMP '(' expr ',' expr ')'
4604
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
4605 4606 4607 4608
	| 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); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4609
	| TRIM '(' expr ')'
4610 4611
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4612
	  { $$= new Item_func_ltrim($6,$4); }
4613
	| TRIM '(' TRAILING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4614
	  { $$= new Item_func_rtrim($6,$4); }
4615
	| TRIM '(' BOTH expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4616
	  { $$= new Item_func_trim($6,$4); }
4617 4618 4619 4620 4621 4622
	| 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); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4623 4624
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
4625 4626
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
4627
	| ident '.' ident '(' udf_expr_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4628
	  {
4629
	    LEX *lex= Lex;
4630 4631 4632
	    sp_name *name= new sp_name($1, $3);

	    name->init_qname(YYTHD);
4633
	    sp_add_to_hash(&lex->spfuns, name);
4634 4635
	    if ($5)
	      $$= new Item_func_sp(name, *$5);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4636
	    else
4637
	      $$= new Item_func_sp(name);
4638
	    lex->safe_to_cache_query=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4639
	  }
4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695
	| IDENT_sys '(' udf_expr_list ')'
          {
#ifdef HAVE_DLOPEN
            udf_func *udf;

            if (using_udf_functions && (udf=find_udf($1.str, $1.length)))
            {
              switch (udf->returns) {
              case STRING_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_str(udf, *$3);
                  else
                    $$ = new Item_func_udf_str(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_str(udf, *$3);
                  else
                    $$ = new Item_sum_udf_str(udf);
                }
                break;
              case REAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_float(udf, *$3);
                  else
                    $$ = new Item_func_udf_float(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_float(udf, *$3);
                  else
                    $$ = new Item_sum_udf_float(udf);
                }
                break;
              case INT_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_int(udf, *$3);
                  else
                    $$ = new Item_func_udf_int(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_int(udf, *$3);
                  else
                    $$ = new Item_sum_udf_int(udf);
                }
                break;
4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711
              case DECIMAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
                  if ($3 != NULL)
                    $$ = new Item_func_udf_decimal(udf, *$3);
                  else
                    $$ = new Item_func_udf_decimal(udf);
                }
                else
                {
                  if ($3 != NULL)
                    $$ = new Item_sum_udf_decimal(udf, *$3);
                  else
                    $$ = new Item_sum_udf_decimal(udf);
                }
                break;
4712 4713 4714 4715 4716 4717 4718
              default:
                YYABORT;
              }
            }
            else
#endif /* HAVE_DLOPEN */
            {
4719
	      LEX *lex= Lex;
4720 4721
              sp_name *name= sp_name_current_db_new(YYTHD, $1);

4722
              sp_add_to_hash(&lex->spfuns, name);
4723 4724 4725 4726
              if ($3)
                $$= new Item_func_sp(name, *$3);
              else
                $$= new Item_func_sp(name);
4727
	      lex->safe_to_cache_query=0;
4728 4729
	    }
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4730
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
4731
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4732 4733
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4734
	| UNIX_TIMESTAMP '(' ')'
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4735 4736
	  {
	    $$= new Item_func_unix_timestamp();
4737
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4738
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4739 4740 4741
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
4742
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
4743 4744 4745 4746 4747 4748
	| 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;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4749
	| WEEK_SYM '(' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4750
	  {
4751
            $$= new Item_func_week($3,new Item_int((char*) "0",
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4752
				   YYTHD->variables.default_week_format,1));
4753
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4754 4755 4756 4757 4758 4759 4760 4761
	| 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); }
serg@serg.mylan's avatar
serg@serg.mylan committed
4762
	| BENCHMARK_SYM '(' ulong_num ',' expr ')'
4763
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4764
	    $$=new Item_func_benchmark($3,$5);
4765
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4766
	  }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4767
	| EXTRACT_SYM '(' interval FROM expr ')'
4768
	{ $$=new Item_extract( $3, $5); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4769

hf@deer.(none)'s avatar
hf@deer.(none) committed
4770
geometry_function:
serg@serg.mylan's avatar
serg@serg.mylan committed
4771 4772 4773
	  CONTAINS_SYM '(' expr ',' expr ')'
	  { $$= GEOM_NEW(Item_func_spatial_rel($3, $5, Item_func::SP_CONTAINS_FUNC)); }
	| GEOMFROMTEXT '(' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
4774 4775 4776 4777 4778 4779 4780 4781 4782
	  { $$= 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,
4783 4784
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4785 4786
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4787
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4788 4789
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
4790
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804
 	| 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,
4805
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4806 4807
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
4808
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820
	| 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,
4821
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4822 4823 4824 4825 4826 4827 4828 4829 4830 4831
 	| 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)); }
	;

4832 4833 4834 4835 4836 4837
fulltext_options:
        /* nothing */                   { $$= FT_NL;  }
        | WITH QUERY_SYM EXPANSION_SYM  { $$= FT_NL | FT_EXPAND; }
        | IN_SYM BOOLEAN_SYM MODE_SYM   { $$= FT_BOOL; }
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4838
udf_expr_list:
4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869
	/* empty */	 { $$= NULL; }
	| udf_expr_list2 { $$= $1;}
	;

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

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

udf_expr:
	remember_name expr remember_end select_alias
	{
	  if ($4.str)
	    $2->set_name($4.str,$4.length,system_charset_info);
	  else
	    $2->set_name($1,(uint) ($3 - $1), YYTHD->charset());
	  $$= $2;
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4870 4871 4872 4873

sum_expr:
	AVG_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_avg($3); }
4874 4875
	| AVG_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_avg_distinct($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4876 4877 4878 4879
	| BIT_AND  '(' in_sum_expr ')'
	  { $$=new Item_sum_and($3); }
	| BIT_OR  '(' in_sum_expr ')'
	  { $$=new Item_sum_or($3); }
4880 4881
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
4882
	| COUNT_SYM '(' opt_all '*' ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4883 4884 4885
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
4886
	| COUNT_SYM '(' DISTINCT
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4887
	  { Select->in_sum_expr++; }
4888
	   expr_list
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4889
	  { Select->in_sum_expr--; }
4890 4891
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4892 4893 4894 4895
	| 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); }
4896 4897 4898 4899 4900 4901 4902
/*
   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); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4903 4904
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
4905 4906
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4907
	| STD_SYM '(' in_sum_expr ')'
4908
	  { $$=new Item_sum_std($3, 0); }
4909
	| VARIANCE_SYM '(' in_sum_expr ')'
4910 4911 4912 4913 4914
	  { $$=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); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4915
	| SUM_SYM '(' in_sum_expr ')'
4916
	  { $$=new Item_sum_sum($3); }
4917 4918
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
monty@mysql.com's avatar
monty@mysql.com committed
4919 4920 4921 4922 4923
	| GROUP_CONCAT_SYM '(' opt_distinct
	  { Select->in_sum_expr++; }
	  expr_list opt_gorder_clause
	  opt_gconcat_separator
	 ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4924
	  {
monty@mysql.com's avatar
monty@mysql.com committed
4925 4926 4927
	    Select->in_sum_expr--;
	    $$=new Item_func_group_concat($3,$5,Select->gorder_list,$7);
	    $5->empty();
4928 4929 4930 4931 4932 4933 4934
	  };

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

opt_gconcat_separator:
4935
    /* empty */        { $$ = new (YYTHD->mem_root) String(",",1,default_charset_info); }
4936
    |SEPARATOR_SYM text_string  { $$ = $2; };
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4937

4938 4939

opt_gorder_clause:
4940 4941
	  /* empty */
	  {
4942
            Select->gorder_list = NULL;
4943 4944 4945
	  }
	| order_clause
          {
4946 4947 4948
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
4949
				     sizeof(st_sql_list));
4950
	    select->order_list.empty();
4951
	  };
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4952

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4953 4954

in_sum_expr:
4955
	opt_all
4956 4957 4958 4959
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
4960
	    yyerror(ER(ER_SYNTAX_ERROR));
4961
	    YYABORT;
4962 4963
	  }
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4964 4965
	expr
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4966
	  Select->in_sum_expr--;
4967
	  $$= $3;
4968
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4969

4970
cast_type:
4971 4972
        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; }
4973
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; Lex->dec=0; }
4974 4975 4976 4977 4978 4979 4980 4981
        | 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; }
4982 4983
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4984
expr_list:
4985
	{ Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4986
	expr_list2
4987
	{ $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4988 4989

expr_list2:
4990
	expr { Select->expr_list.head()->push_back($1); }
4991
	| expr_list2 ',' expr { Select->expr_list.head()->push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4992

4993 4994
ident_list_arg:
          ident_list          { $$= $1; }
4995
        | '(' ident_list ')'  { $$= $2; };
4996

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4997
ident_list:
4998
        { Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4999
        ident_list2
5000
        { $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5001 5002

ident_list2:
5003
        simple_ident { Select->expr_list.head()->push_back($1); }
5004
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5005 5006 5007

opt_expr:
	/* empty */      { $$= NULL; }
5008
	| expr           { $$= $1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5009 5010 5011

opt_else:
	/* empty */    { $$= NULL; }
5012
	| ELSE expr    { $$= $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5013 5014

when_list:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5015
        { Select->when_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5016
	when_list2
5017
	{ $$= Select->when_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5018 5019

when_list2:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5020
	expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5021
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5022
	    SELECT_LEX *sel=Select;
5023 5024
	    sel->when_list.head()->push_back($1);
	    sel->when_list.head()->push_back($3);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5025
	}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5026
	| when_list2 WHEN_SYM expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5027
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5028
	    SELECT_LEX *sel=Select;
5029 5030
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
5031
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5032

5033
/* Warning - may return NULL in case of incomplete SELECT */
5034 5035 5036
table_ref:
        table_factor            { $$=$1; }
        | join_table            { $$=$1; }
5037
          {
5038 5039 5040
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
5041
          }
5042 5043
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5044
join_table_list:
5045 5046
	derived_table_list		{ TEST_ASSERT($$=$1); }
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
5047

5048 5049
/* Warning - may return NULL in case of incomplete SELECT */
derived_table_list:
5050
        table_ref { $$=$1; }
5051 5052 5053 5054
        | derived_table_list ',' table_ref
          {
            TEST_ASSERT($1 && ($$=$3));
          }
5055 5056 5057
        ;

join_table:
5058
        table_ref normal_join table_ref { TEST_ASSERT($1 && ($$=$3)); }
5059
	| table_ref STRAIGHT_JOIN table_factor
5060
	  {  TEST_ASSERT($1 && ($$=$3)); $3->straight=1; }
5061
	| table_ref normal_join table_ref ON expr
5062
	  { TEST_ASSERT($1 && ($$=$3)); add_join_on($3,$5); }
5063
	| table_ref normal_join table_ref
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5064
	  USING
5065
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5066
	    SELECT_LEX *sel= Select;
5067
            TEST_ASSERT($1 && $3);
5068
            sel->save_names_for_using_list($1, $3);
5069
	  }
5070 5071 5072
	  '(' using_list ')'
	  { add_join_on($3,$7); $$=$3; }

5073
	| table_ref LEFT opt_outer JOIN_SYM table_ref ON expr
5074
	  { TEST_ASSERT($1 && $5); add_join_on($5,$7); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
5075
	| table_ref LEFT opt_outer JOIN_SYM table_factor
5076
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5077
	    SELECT_LEX *sel= Select;
5078
            TEST_ASSERT($1 && $5);
5079
            sel->save_names_for_using_list($1, $5);
5080
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5081
	  USING '(' using_list ')'
5082
	  { add_join_on($5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
5083
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
5084
	  {
5085
            TEST_ASSERT($1 && $6);
5086 5087
	    add_join_natural($1,$6);
	    $6->outer_join|=JOIN_TYPE_LEFT;
5088 5089
	    $$=$6;
	  }
5090
	| table_ref RIGHT opt_outer JOIN_SYM table_ref ON expr
serg@serg.mylan's avatar
serg@serg.mylan committed
5091
          {
5092
	    LEX *lex= Lex;
5093
            TEST_ASSERT($1 && $5);
5094 5095 5096 5097 5098
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
            add_join_on($$, $7);
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
5099
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5100
	    SELECT_LEX *sel= Select;
5101
            TEST_ASSERT($1 && $5);
5102
            sel->save_names_for_using_list($1, $5);
5103
	  }
5104
	  USING '(' using_list ')'
serg@serg.mylan's avatar
serg@serg.mylan committed
5105
          {
5106 5107 5108 5109 5110 5111
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
            add_join_on($$, $9);
          }
	| table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor
5112
	  {
5113
            TEST_ASSERT($1 && $6);
5114 5115 5116 5117
	    add_join_natural($6,$1);
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
5118
	  }
5119
	| table_ref NATURAL JOIN_SYM table_factor
5120
	  { TEST_ASSERT($1 && ($$=$4)); add_join_natural($1,$4); };
serg@serg.mylan's avatar
serg@serg.mylan committed
5121

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5122 5123

normal_join:
5124 5125 5126 5127
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5128

5129
/* Warning - may return NULL in case of incomplete SELECT */
5130
table_factor:
5131
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5132
	  SELECT_LEX *sel= Select;
5133
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5134
	  sel->table_join_options= 0;
5135
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5136
        table_ident opt_table_alias opt_key_definition
5137
	{
5138
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5139
	  SELECT_LEX *sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5140
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5141
					   sel->get_table_join_options(),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5142
					   lex->lock_option,
5143 5144
					   sel->get_use_index(),
					   sel->get_ignore_index())))
5145
	    YYABORT;
5146
          sel->add_joined_table($$);
5147
	}
5148 5149 5150
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref ON expr '}'
	  { TEST_ASSERT($3 && $7); add_join_on($7,$9); $7->outer_join|=JOIN_TYPE_LEFT; $$=$7; }
	| select_derived_init get_select_lex select_derived2
5151
          {
5152
            LEX *lex= Lex;
5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166
            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))
serg@serg.mylan's avatar
serg@serg.mylan committed
5167
              YYABORT;
5168
            $$= 0;
serg@serg.mylan's avatar
serg@serg.mylan committed
5169
            /* incomplete derived tables return NULL, we must be
5170
               nested in select_derived rule to be here. */
5171
          }
5172 5173 5174 5175 5176
	| '(' 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. */

serg@serg.mylan's avatar
serg@serg.mylan committed
5177
          if (!($3 || $6) && $2->embedding &&
5178
              !$2->embedding->nested_join->join_list.elements)
5179
          {
5180 5181 5182 5183 5184
            /* 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;
5185
          }
5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199
          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)))
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5200

5201
	      YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
5202
            sel->add_joined_table($$);
5203 5204 5205 5206 5207 5208
          }
	  else
          if ($4 || $6)
	  {
            /* simple nested joins cannot have aliases or unions */
            yyerror(ER(ER_SYNTAX_ERROR));
5209
	    YYABORT;
5210 5211 5212 5213 5214
	  }
          else
            $$= $3;
	}
        ;
5215

5216
/* handle contents of parentheses in join expression */
5217
select_derived:
5218
	  get_select_lex
5219
	  {
5220 5221 5222 5223 5224 5225 5226 5227 5228
            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 */
serg@serg.mylan's avatar
serg@serg.mylan committed
5229

5230 5231 5232 5233
            if (!($$= $1->end_nested_join(lex->thd)) && $3)
              YYABORT;
            if (!$3 && $$)
            {
5234 5235
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
5236 5237
            }
          }
monty@mysql.com's avatar
monty@mysql.com committed
5238
 	;
5239

monty@mysql.com's avatar
monty@mysql.com committed
5240
select_derived2:
5241
        {
5242
	  LEX *lex= Lex;
5243
	  lex->derived_tables|= DERIVED_SUBQUERY;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5244 5245 5246
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
5247 5248
	  {
	    yyerror(ER(ER_SYNTAX_ERROR));
5249 5250
	    YYABORT;
	  }
5251
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
5252
              mysql_new_select(lex, 1))
5253 5254
	    YYABORT;
	  mysql_init_select(lex);
5255
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
5256
	  lex->current_select->parsing_place= SELECT_LIST;
5257 5258 5259
	}
        select_options select_item_list
	{
5260
	  Select->parsing_place= NO_MATTER;
5261
	}
5262
	opt_select_from
5263
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5264

5265 5266 5267 5268 5269 5270 5271 5272
get_select_lex:
	/* Empty */ { $$= Select; }
        ;

select_derived_init:
          SELECT_SYM
          {
            LEX *lex= Lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
5273
            SELECT_LEX *sel= lex->current_select;
5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287
            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 */
          }
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5288 5289
opt_outer:
	/* empty */	{}
5290
	| OUTER		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5291 5292 5293 5294

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
5295
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5296
	    SELECT_LEX *sel= Select;
5297 5298 5299
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5300 5301
	| FORCE_SYM key_usage_list
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5302
	    SELECT_LEX *sel= Select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5303 5304 5305 5306
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5307
	| IGNORE_SYM key_usage_list
5308
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5309
	    SELECT_LEX *sel= Select;
5310 5311
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
5312
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5313 5314

key_usage_list:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5315
	key_or_index { Select->interval_list.empty(); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5316
        '(' key_list_or_empty ')'
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5317
        { $$= &Select->interval_list; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5318 5319 5320 5321 5322 5323
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5324 5325 5326

key_usage_list2:
	key_usage_list2 ',' ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5327
        { Select->
5328
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $3.str, $3.length,
5329
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5330
	| ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5331
        { Select->
5332
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $1.str, $1.length,
5333
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5334
	| PRIMARY_SYM
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5335
        { Select->
5336
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
5337
				    system_charset_info)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5338 5339 5340

using_list:
	ident
5341
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5342
	    SELECT_LEX *sel= Select;
5343 5344 5345 5346
	    if (!($$= new Item_func_eq(new Item_field(sel->db1, sel->table1,
						      $1.str),
				       new Item_field(sel->db2, sel->table2,
						      $1.str))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5347 5348 5349 5350
	      YYABORT;
	  }
	| using_list ',' ident
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5351
	    SELECT_LEX *sel= Select;
5352
	    if (!($$= new Item_cond_and(new Item_func_eq(new Item_field(sel->db1,sel->table1,$3.str), new Item_field(sel->db2,sel->table2,$3.str)), $1)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5353
	      YYABORT;
5354
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5355 5356

interval:
5357 5358
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
5359
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5360 5361
	| DAY_MINUTE_SYM	{ $$=INTERVAL_DAY_MINUTE; }
	| DAY_SECOND_SYM	{ $$=INTERVAL_DAY_SECOND; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
5362
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5363 5364
	| HOUR_MINUTE_SYM	{ $$=INTERVAL_HOUR_MINUTE; }
	| HOUR_SECOND_SYM	{ $$=INTERVAL_HOUR_SECOND; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
5365 5366
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5367
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
5368 5369 5370 5371 5372 5373 5374 5375
	| 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; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5376 5377
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
5378
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5379
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
5380 5381
	| YEAR_SYM		{ $$=INTERVAL_YEAR; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5382

5383
date_time_type:
5384 5385 5386 5387 5388
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
5389

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5390 5391 5392
table_alias:
	/* empty */
	| AS
5393
	| EQ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5394 5395 5396 5397

opt_table_alias:
	/* empty */		{ $$=0; }
	| table_alias ident
5398
	  { $$= (LEX_STRING*) sql_memdup(&$2,sizeof(LEX_STRING)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5399

5400 5401 5402 5403
opt_all:
	/* empty */
	| ALL
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5404 5405

where_clause:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5406
	/* empty */  { Select->where= 0; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5407 5408 5409 5410 5411
	| WHERE
          {
            Select->parsing_place= IN_WHERE;
          }
          expr
5412
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5413 5414 5415 5416 5417
            SELECT_LEX *select= Select;
	    select->where= $3;
            select->parsing_place= NO_MATTER;
	    if ($3)
	      $3->top_level_item();
5418
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5419
 	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5420 5421 5422

having_clause:
	/* empty */
5423 5424
	| HAVING
	  {
5425
	    Select->parsing_place= IN_HAVING;
5426 5427
          }
	  expr
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5428
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5429
	    SELECT_LEX *sel= Select;
5430
	    sel->having= $3;
5431
	    sel->parsing_place= NO_MATTER;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5432 5433 5434
	    if ($3)
	      $3->top_level_item();
	  }
5435
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5436 5437

opt_escape:
5438 5439
	ESCAPE_SYM simple_expr { $$= $2; }
	| /* empty */
5440 5441 5442 5443 5444
          {

            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
5445 5446
          }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5447 5448 5449


/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5450
   group by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5451 5452 5453 5454
*/

group_clause:
	/* empty */
5455
	| GROUP BY group_list olap_opt;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5456 5457

group_list:
5458
	group_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5459
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
5460
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5461
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5462

5463 5464
olap_opt:
	/* empty */ {}
5465
	| WITH CUBE_SYM
5466
          {
5467
	    LEX *lex=Lex;
5468 5469
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
5470
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
5471 5472 5473
		       "global union parameters");
	      YYABORT;
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5474
	    lex->current_select->olap= CUBE_TYPE;
5475
	    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
5476
	    YYABORT;	/* To be deleted in 5.1 */
5477
	  }
5478
	| WITH ROLLUP_SYM
5479
          {
5480 5481 5482
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
5483
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
5484 5485 5486
		       "global union parameters");
	      YYABORT;
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5487
	    lex->current_select->olap= ROLLUP_TYPE;
5488
	  }
5489
	;
5490

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5491
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5492
   Order by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5493 5494
*/

5495
opt_order_clause:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5496
	/* empty */
5497
	| order_clause;
5498 5499

order_clause:
5500 5501
	ORDER_SYM BY
        {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5502
	  LEX *lex=Lex;
5503
	  if (lex->current_select->linkage != GLOBAL_OPTIONS_TYPE &&
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5504
	      lex->current_select->olap !=
5505
	      UNSPECIFIED_OLAP_TYPE)
5506
	  {
5507 5508
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
5509 5510
	    YYABORT;
	  }
5511
	} order_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5512 5513 5514

order_list:
	order_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5515
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5516
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5517
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5518 5519 5520

order_dir:
	/* empty */ { $$ =  1; }
5521
	| ASC  { $$ =1; }
5522
	| DESC { $$ =0; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5523 5524


5525 5526 5527
opt_limit_clause_init:
	/* empty */
	{
5528 5529
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
5530
          sel->offset_limit= 0L;
5531
          sel->select_limit= HA_POS_ERROR;
5532
	}
5533 5534 5535
	| limit_clause {}
	;

5536 5537 5538 5539 5540
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

5541
limit_clause:
5542
	LIMIT limit_options {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5543
	;
5544 5545

limit_options:
serg@serg.mylan's avatar
serg@serg.mylan committed
5546
	ulong_num
5547
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5548
            SELECT_LEX *sel= Select;
5549 5550
            sel->select_limit= $1;
            sel->offset_limit= 0L;
5551
	    sel->explicit_limit= 1;
5552
	  }
serg@serg.mylan's avatar
serg@serg.mylan committed
5553
	| ulong_num ',' ulong_num
5554
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5555
	    SELECT_LEX *sel= Select;
5556 5557
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
5558
	    sel->explicit_limit= 1;
5559
	  }
serg@serg.mylan's avatar
serg@serg.mylan committed
5560
	| ulong_num OFFSET_SYM ulong_num
5561
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5562
	    SELECT_LEX *sel= Select;
5563 5564
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
5565
	    sel->explicit_limit= 1;
5566 5567
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5568 5569 5570 5571 5572


delete_limit_clause:
	/* empty */
	{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5573
	  LEX *lex=Lex;
5574
	  lex->current_select->select_limit= HA_POS_ERROR;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5575
	}
5576
	| LIMIT ulonglong_num
5577 5578 5579 5580 5581
	{
	  SELECT_LEX *sel= Select;
	  sel->select_limit= (ha_rows) $2;
	  sel->explicit_limit= 1;
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5582

serg@serg.mylan's avatar
serg@serg.mylan committed
5583 5584 5585
ulong_num:
          NUM           { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| HEX_NUM       { int error; $$= (ulong) strtol($1.str, (char**) 0, 16); }
monty@mysql.com's avatar
monty@mysql.com committed
5586 5587
	| LONG_NUM      { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| ULONGLONG_NUM { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
serg@serg.mylan's avatar
serg@serg.mylan committed
5588
        | DECIMAL_NUM   { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
monty@mysql.com's avatar
monty@mysql.com committed
5589 5590
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5591

5592
ulonglong_num:
monty@mysql.com's avatar
monty@mysql.com committed
5593 5594 5595
	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); }
5596
        | DECIMAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
monty@mysql.com's avatar
monty@mysql.com committed
5597 5598
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5599 5600 5601 5602 5603 5604

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
5605 5606
	    if (&lex->select_lex != lex->current_select)
	    {
5607
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
5608 5609
	      YYABORT;
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5610 5611 5612
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
5613
	    if (add_proc_to_list(lex->thd, new Item_field(NULL,NULL,$2.str)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5614
	      YYABORT;
5615
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5616
	  }
5617
	  '(' procedure_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5618 5619 5620 5621


procedure_list:
	/* empty */ {}
5622
	| procedure_list2 {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5623 5624 5625

procedure_list2:
	procedure_list2 ',' procedure_item
5626
	| procedure_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5627 5628 5629 5630

procedure_item:
	  remember_name expr
	  {
5631 5632
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5633 5634
	      YYABORT;
	    if (!$2->name)
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5635
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1), YYTHD->charset());
5636 5637 5638 5639 5640 5641
	  }
          ;


select_var_list_init:
	   {
5642 5643
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
5644 5645
	        YYABORT;
	   }
5646
	   select_var_list
5647
	   {}
5648
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5649

Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5650
select_var_list:
5651 5652 5653 5654
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

5655 5656 5657 5658 5659 5660 5661 5662 5663 5664
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
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5665
           {
5666
             LEX *lex=Lex;
5667 5668 5669 5670
	     sp_pvar_t *t;

	     if (!lex->spcont || !(t=lex->spcont->find_pvar(&$1)))
	     {
5671
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5672
	       YYABORT;
5673 5674 5675 5676 5677 5678 5679 5680
	     }
	     if (! lex->result)
	       YYABORT;
	     else
	     {
	       ((select_dumpvar *)lex->result)->var_list.push_back( new my_var($1,1,t->offset,t->type));
	       t->isset= TRUE;
	     }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5681
	   }
5682
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5683

5684
into:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5685
        INTO OUTFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5686
	{
5687
          LEX *lex= Lex;
5688 5689 5690 5691
          lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
          if (!(lex->exchange= new sql_exchange($3.str, 0)) ||
              !(lex->result= new select_export(lex->exchange)))
            YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5692 5693
	}
	opt_field_term opt_line_term
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5694
	| INTO DUMPFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5695
	{
5696
	  LEX *lex=Lex;
5697 5698
	  if (!lex->describe)
	  {
5699
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5700 5701 5702 5703 5704
	    if (!(lex->exchange= new sql_exchange($3.str,1)))
	      YYABORT;
	    if (!(lex->result= new select_dump(lex->exchange)))
	      YYABORT;
	  }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5705
	}
5706
        | INTO select_var_list_init
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5707
	{
5708
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5709 5710
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5711

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5712 5713 5714
/*
  DO statement
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5715

5716
do:	DO_SYM
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5717 5718 5719
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
5720 5721 5722 5723 5724
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5725
	}
5726 5727
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5728
/*
5729
  Drop : delete tables or index or user
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5730 5731 5732
*/

drop:
serg@serg.mylan's avatar
serg@serg.mylan committed
5733
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5734
	{
5735 5736
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
5737 5738
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5739
	}
5740
	| DROP INDEX_SYM ident ON table_ident {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5741
	  {
5742 5743
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
5744 5745 5746
	     lex->alter_info.drop_list.empty();
	     lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
                                                                $3.str));
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5747 5748
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5749 5750 5751 5752
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
5753 5754 5755 5756
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
	    lex->name=$4.str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5757
	 }
5758
	| DROP FUNCTION_SYM if_exists sp_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5759
	  {
5760
	    LEX *lex=Lex;
5761 5762
	    if (lex->sphead)
	    {
5763
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
5764 5765
	      YYABORT;
	    }
5766
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
5767 5768 5769 5770 5771 5772 5773 5774
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
	  }
	| DROP PROCEDURE if_exists sp_name
	  {
	    LEX *lex=Lex;
	    if (lex->sphead)
	    {
5775
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
5776 5777 5778 5779 5780
	      YYABORT;
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
5781
	  }
5782
	| DROP USER clear_privileges user_list
5783
	  {
5784 5785
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
5786 5787 5788 5789 5790 5791 5792 5793 5794 5795
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
        | DROP TRIGGER_SYM ident '.' ident
          {
            LEX *lex= Lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5796

5797 5798 5799 5800 5801 5802 5803 5804
            lex->sql_command= SQLCOM_DROP_TRIGGER;
            /* QQ: Could we loosen lock type in certain cases ? */
            if (!lex->select_lex.add_table_to_list(YYTHD,
                                                   new Table_ident($3),
                                                   (LEX_STRING*) 0,
                                                   TL_OPTION_UPDATING,
                                                   TL_WRITE))
              YYABORT;
5805
            lex->ident= $5;
5806 5807
          }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5808 5809

table_list:
5810
	table_name
5811
	| table_list ',' table_name;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5812

5813
table_name:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5814
	table_ident
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5815 5816 5817 5818 5819
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5820 5821

if_exists:
5822
	/* empty */ { $$= 0; }
5823 5824
	| IF EXISTS { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5825

5826 5827 5828 5829
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5830 5831 5832 5833 5834
/*
** Insert : add new data to table
*/

insert:
5835 5836 5837
	INSERT
	{
	  LEX *lex= Lex;
5838 5839
	  lex->sql_command= SQLCOM_INSERT;
	  lex->duplicates= DUP_ERROR; 
5840
	  mysql_init_select(lex);
5841 5842
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
5843
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
5844
	} insert_lock_option
5845
	opt_ignore insert2
5846
	{
5847
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5848
	  Lex->current_select= &Lex->select_lex;
5849
	}
5850
	insert_field_spec opt_insert_update
5851
	{}
5852
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5853 5854

replace:
5855 5856
	REPLACE
	{
5857
	  LEX *lex=Lex;
5858 5859
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
5860
	  mysql_init_select(lex);
5861
	  lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE;
5862
	}
5863 5864
	replace_lock_option insert2
	{
5865
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5866
	  Lex->current_select= &Lex->select_lex;
5867 5868
	}
	insert_field_spec
5869
	{}
5870
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5871 5872

insert_lock_option:
5873 5874 5875 5876
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
5877
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5878 5879

replace_lock_option:
5880 5881
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5882 5883 5884

insert2:
	INTO insert_table {}
5885
	| insert_table {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5886 5887

insert_table:
5888
	table_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5889
	{
5890 5891 5892 5893
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
5894
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5895 5896

insert_field_spec:
serg@serg.mylan's avatar
serg@serg.mylan committed
5897 5898 5899
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5900 5901
	| SET
	  {
5902 5903 5904
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5905 5906
	      YYABORT;
	   }
5907
	   ident_eq_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5908 5909 5910

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
5911
	| insert_ident		{ Lex->field_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5912 5913 5914

insert_values:
	VALUES	values_list  {}
5915
	| VALUE_SYM values_list  {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5916 5917
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
5918
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5919 5920 5921

values_list:
	values_list ','  no_braces
5922
	| no_braces;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5923 5924 5925 5926

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
5927
	ident_eq_value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5928 5929

ident_eq_value:
5930
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5931
	 {
5932 5933 5934
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5935
	    YYABORT;
5936
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5937 5938

equal:	EQ		{}
5939 5940 5941 5942 5943 5944 5945
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5946 5947 5948 5949 5950 5951 5952 5953 5954

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
5955 5956
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5957
	    YYABORT;
5958
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5959 5960 5961

opt_values:
	/* empty */ {}
5962
	| values;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5963 5964

values:
5965
	values ','  expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5966 5967 5968 5969
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
5970 5971 5972 5973 5974 5975 5976 5977
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
5978
	expr	  { $$= $1;}
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
5979
	| DEFAULT {$$= new Item_default_value(); }
5980
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5981

5982 5983
opt_insert_update:
        /* empty */
monty@mysql.com's avatar
monty@mysql.com committed
5984
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
5985
          KEY_SYM UPDATE_SYM insert_update_list
5986 5987
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5988 5989 5990
/* Update rows in a table */

update:
5991 5992
	UPDATE_SYM
	{
5993
	  LEX *lex= Lex;
5994
	  mysql_init_select(lex);
5995
          lex->sql_command= SQLCOM_UPDATE;
5996
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
5997
	  lex->duplicates= DUP_ERROR; 
5998
        }
5999
        opt_low_priority opt_ignore join_table_list
6000
	SET update_list
6001
	{
6002 6003
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
6004
            lex->sql_command= SQLCOM_UPDATE_MULTI;
6005 6006 6007
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
6008 6009
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
6010 6011
	    YYABORT;
	  }
6012 6013 6014 6015 6016 6017
          /*
            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);
6018
	}
6019
	where_clause opt_order_clause delete_limit_clause {}
6020
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6021 6022

update_list:
6023 6024 6025 6026
	update_list ',' update_elem
	| update_elem;

update_elem:
monty@mishka.local's avatar
monty@mishka.local committed
6027
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6028
	{
monty@mysql.com's avatar
monty@mysql.com committed
6029
	  if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6030
	    YYABORT;
6031 6032 6033 6034 6035 6036 6037
	};

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
monty@mishka.local's avatar
monty@mishka.local committed
6038
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6039
	  {
6040 6041 6042
	  LEX *lex= Lex;
	  if (lex->update_list.push_back($1) || 
	      lex->value_list.push_back($3))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6043
	      YYABORT;
6044
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6045 6046

opt_low_priority:
6047
	/* empty */	{ $$= YYTHD->update_lock_default; }
6048
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6049 6050 6051 6052

/* Delete rows from a table */

delete:
6053
	DELETE_SYM
6054
	{
6055 6056
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
6057
	  mysql_init_select(lex);
6058
	  lex->lock_option= lex->thd->update_lock_default;
6059
	  lex->ignore= 0;
6060
	  lex->select_lex.init_order();
6061
	}
6062 6063
	opt_delete_options single_multi {}
	;
6064 6065

single_multi:
6066 6067
 	FROM table_ident
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6068 6069
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
6070 6071 6072
	    YYABORT;
	}
	where_clause opt_order_clause
6073
	delete_limit_clause {}
6074
	| table_wild_list
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6075
	  { mysql_init_multi_delete(Lex); }
6076
          FROM join_table_list where_clause
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6077 6078
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
6079 6080 6081
	  USING join_table_list where_clause
	  {}
	;
6082 6083 6084

table_wild_list:
	  table_wild_one {}
6085
	  | table_wild_list ',' table_wild_one {};
6086 6087

table_wild_one:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6088
	ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6089
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6090 6091
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
					 TL_OPTION_UPDATING, Lex->lock_option))
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6092 6093
	    YYABORT;
        }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6094
	| ident '.' ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6095
	  {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6096 6097
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6098 6099
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
6100
	      YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6101
	  }
6102
	;
6103 6104

opt_wild:
6105
	/* empty */	{}
6106
	| '.' '*'	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6107 6108


6109
opt_delete_options:
6110
	/* empty */	{}
6111
	| opt_delete_option opt_delete_options {};
6112 6113

opt_delete_option:
6114
	QUICK		{ Select->options|= OPTION_QUICK; }
6115
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
6116
	| IGNORE_SYM	{ Lex->ignore= 1; };
6117

6118
truncate:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
6119
	TRUNCATE_SYM opt_table_sym table_name
6120
	{
6121
	  LEX* lex= Lex;
6122
	  lex->sql_command= SQLCOM_TRUNCATE;
6123
	  lex->select_lex.options= 0;
6124
	  lex->select_lex.init_order();
6125 6126
	}
	;
6127

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6128 6129
opt_table_sym:
	/* empty */
6130
	| TABLE_SYM;
6131

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6132 6133
/* Show things */

6134 6135
show:	SHOW
	{
6136 6137
	  LEX *lex=Lex;
	  lex->wild=0;
6138 6139 6140
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
6141 6142
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6143
	show_param
6144 6145
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6146 6147

show_param:
6148
         DATABASES wild_and_where
6149 6150 6151 6152 6153 6154 6155
         {
           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;
         }
6156
         | opt_full TABLES opt_db wild_and_where
6157 6158 6159 6160
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLES;
6161
             lex->select_lex.db= $3;
6162 6163 6164
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLE_NAMES))
               YYABORT;
           }
6165
         | TABLE_SYM STATUS_SYM opt_db wild_and_where
6166 6167 6168 6169
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLE_STATUS;
6170
             lex->select_lex.db= $3;
6171 6172 6173
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLES))
               YYABORT;
           }
6174
        | OPEN_SYM TABLES opt_db wild_and_where
6175
	  {
6176
	    LEX *lex= Lex;
6177 6178
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_OPEN_TABLES;
6179
	    lex->select_lex.db= $3;
6180 6181
            if (prepare_schema_table(YYTHD, lex, 0, SCH_OPEN_TABLES))
              YYABORT;
6182
	  }
6183
	| ENGINE_SYM storage_engines 
6184 6185
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
6186
	| opt_full COLUMNS from_or_in table_ident opt_db wild_and_where
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6187
	  {
6188 6189 6190
 	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SELECT;
	    lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
6191 6192 6193
	    if ($5)
	      $4->change_db($5);
	    if (prepare_schema_table(YYTHD, lex, $4, SCH_COLUMNS))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6194 6195
	      YYABORT;
	  }
6196
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
6197 6198
	  TEXT_STRING_sys AND_SYM MASTER_LOG_POS_SYM EQ ulonglong_num
	  AND_SYM MASTER_SERVER_ID_SYM EQ
serg@serg.mylan's avatar
serg@serg.mylan committed
6199
	ulong_num
6200 6201 6202 6203
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
6204
	    Lex->mi.server_id = $16;
6205
          }
6206
        | master_or_binary LOGS_SYM
6207 6208
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
6209 6210 6211 6212 6213
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
6214
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
6215
          {
6216 6217
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
6218
          } opt_limit_clause_init
6219
        | keys_or_index from_or_in table_ident opt_db where_clause
6220 6221 6222 6223
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_KEYS;
6224 6225 6226
	    if ($4)
	      $3->change_db($4);
            if (prepare_schema_table(YYTHD, lex, $3, SCH_STATISTICS))
6227
              YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6228
	  }
6229 6230 6231 6232 6233 6234 6235 6236
	| COLUMN_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_COLUMN_TYPES;
	  }
	| TABLE_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
6237 6238 6239 6240 6241 6242 6243
	    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;
6244 6245 6246 6247 6248 6249
	  }
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
6250
        | COUNT_SYM '(' '*' ')' WARNINGS
6251
          { (void) create_select_for_variable("warning_count"); }
6252
        | COUNT_SYM '(' '*' ')' ERRORS
6253
	  { (void) create_select_for_variable("error_count"); }
6254
        | WARNINGS opt_limit_clause_init
6255
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
6256
        | ERRORS opt_limit_clause_init
6257
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
6258
        | opt_var_type STATUS_SYM wild_and_where
6259
          {
6260 6261 6262 6263 6264 6265
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS;
            lex->option_type= (enum_var_type) $1;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_STATUS))
              YYABORT;
6266
	  }	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
6267
        | INNOBASE_SYM STATUS_SYM
6268
          { Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS; WARN_DEPRECATED("SHOW INNODB STATUS", "SHOW ENGINE INNODB STATUS"); }
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
6269 6270
        | MUTEX_SYM STATUS_SYM
          { Lex->sql_command = SQLCOM_SHOW_MUTEX_STATUS; }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6271 6272
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
6273
        | opt_var_type  VARIABLES wild_and_where
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6274
	  {
6275 6276 6277 6278 6279 6280
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_VARIABLES;
            lex->option_type= (enum_var_type) $1;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_VARIABLES))
              YYABORT;
6281
	  }
6282
        | charset wild_and_where
6283 6284 6285 6286 6287 6288 6289
          {
            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;
          }
6290
        | COLLATION_SYM wild_and_where
6291 6292 6293 6294 6295 6296 6297
          {
            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;
          }
6298
	| BERKELEY_DB_SYM LOGS_SYM
6299
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW BDB LOGS", "SHOW ENGINE BDB LOGS"); }
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
6300
	| LOGS_SYM
6301
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW LOGS", "SHOW ENGINE BDB LOGS"); }
6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321
	| GRANTS
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    THD *thd= lex->thd;
	    LEX_USER *curr_user;
            if (!(curr_user= (LEX_USER*) thd->alloc(sizeof(st_lex_user))))
              YYABORT;
            curr_user->user.str= thd->priv_user;
            curr_user->user.length= strlen(thd->priv_user);
            if (*thd->priv_host != 0)
            {
              curr_user->host.str= thd->priv_host;
              curr_user->host.length= strlen(thd->priv_host);
            }
            else
            {
              curr_user->host.str= (char *) "%";
              curr_user->host.length= 1;
            }
6322
            curr_user->password=null_lex_str;
6323 6324
	    lex->grant_user= curr_user;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6325
	| GRANTS FOR_SYM user
6326 6327 6328 6329
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
6330
	    lex->grant_user->password=null_lex_str;
6331
	  }
6332
	| CREATE DATABASE opt_if_not_exists ident
6333 6334
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
6335 6336
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
6337
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6338 6339
        | CREATE TABLE_SYM table_ident
          {
6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350
            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))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6351
	      YYABORT;
6352
            lex->only_view= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6353 6354 6355 6356
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6357
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6358 6359 6360
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375
          }
	| 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;
	  }
6376
	| PROCEDURE STATUS_SYM wild_and_where
6377
	  {
6378 6379 6380
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_PROC;
6381 6382
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
6383 6384
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
6385
	  }
6386
	| FUNCTION_SYM STATUS_SYM wild_and_where
6387
	  {
6388 6389 6390
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_FUNC;
6391 6392
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
6393 6394
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
6395
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6396

6397 6398 6399 6400 6401 6402 6403 6404
show_engine_param:
	STATUS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_INNODB:
	      Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;
	      break;
	    default:
6405
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "STATUS");
6406 6407 6408 6409 6410 6411 6412 6413 6414 6415
	      YYABORT;
	    }
	  }
	| LOGS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_BERKELEY_DB:
	      Lex->sql_command = SQLCOM_SHOW_LOGS;
	      break;
	    default:
6416
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "LOGS");
6417 6418 6419 6420
	      YYABORT;
	    }
	  };

6421 6422 6423 6424
master_or_binary:
	MASTER_SYM
	| BINARY;

6425 6426 6427 6428
opt_storage:
	/* empty */
	| STORAGE_SYM;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6429 6430
opt_db:
	/* empty */  { $$= 0; }
6431
	| from_or_in ident { $$= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6432

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6433 6434
opt_full:
	/* empty */ { Lex->verbose=0; }
6435
	| FULL	    { Lex->verbose=1; };
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6436

6437 6438
from_or_in:
	FROM
6439
	| IN_SYM;
6440

6441 6442
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6443
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
6444 6445 6446

binlog_from:
	/* empty */ { Lex->mi.pos = 4; /* skip magic number */ }
6447
        | FROM ulonglong_num { Lex->mi.pos = $2; };
6448

6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461
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();
        }
      ;

6462

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6463 6464 6465 6466
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
6467 6468 6469 6470 6471 6472 6473 6474 6475
          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))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6476 6477
	    YYABORT;
	}
6478
	opt_describe_column {}
6479 6480 6481
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
6482
          {
6483
	    LEX *lex=Lex;
6484
	    lex->select_lex.options|= SELECT_DESCRIBE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6485 6486
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6487 6488 6489

describe_command:
	DESC
6490
	| DESCRIBE;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6491

6492 6493 6494 6495 6496
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6497 6498 6499
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
6500
	| ident
6501
	  { Lex->wild= new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6502 6503 6504 6505 6506


/* flush things */

flush:
6507
	FLUSH_SYM opt_no_write_to_binlog
6508 6509 6510
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_FLUSH; lex->type=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6511
          lex->no_write_to_binlog= $2;
6512
	}
6513 6514 6515
	flush_options
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6516 6517 6518

flush_options:
	flush_options ',' flush_option
6519
	| flush_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6520 6521

flush_option:
6522
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6523
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6524
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6525 6526 6527 6528
	| HOSTS_SYM	{ Lex->type|= REFRESH_HOSTS; }
	| PRIVILEGES	{ Lex->type|= REFRESH_GRANT; }
	| LOGS_SYM	{ Lex->type|= REFRESH_LOG; }
	| STATUS_SYM	{ Lex->type|= REFRESH_STATUS; }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6529 6530
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
6531 6532
	| DES_KEY_FILE	{ Lex->type|= REFRESH_DES_KEY_FILE; }
 	| RESOURCES     { Lex->type|= REFRESH_USER_RESOURCES; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6533

6534
opt_table_list:
6535 6536
	/* empty */  {;}
	| table_list {;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6537

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6538
reset:
6539 6540 6541 6542
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
6543 6544 6545 6546
	} reset_options
	{}
	;

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6547 6548
reset_options:
	reset_options ',' reset_option
6549
	| reset_option;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6550 6551

reset_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6552 6553
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
6554
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6555

6556
purge:
6557 6558 6559 6560
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
6561 6562 6563 6564 6565
	} purge_options
	{}
	;

purge_options:
6566
	master_or_binary LOGS_SYM purge_option
6567
	;
6568 6569

purge_option:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6570
        TO_SYM TEXT_STRING_sys
6571 6572 6573 6574 6575 6576
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
6577 6578 6579 6580
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
6581
	}
6582
	;
6583

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6584 6585 6586
/* kill threads */

kill:
6587
	KILL_SYM kill_option expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6588
	{
6589
	  LEX *lex=Lex;
6590 6591 6592
	  lex->value_list.empty();
	  lex->value_list.push_front($3);
          lex->sql_command= SQLCOM_KILL;
6593
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6594

6595 6596 6597 6598 6599
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; };

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6600 6601 6602
/* change database */

use:	USE_SYM ident
6603 6604
	{
	  LEX *lex=Lex;
6605 6606
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
6607
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6608 6609 6610

/* import, export of files */

6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627
load:   LOAD DATA_SYM 
        {
          LEX *lex=Lex;
          lex->fname_start= lex->ptr;
        }
        load_data
        {}
        |
        LOAD TABLE_SYM table_ident FROM MASTER_SYM
        {
          Lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
          if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
            YYABORT;
        };

load_data:
	load_data_lock opt_local INFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6628
	{
6629 6630
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
6631 6632
	  lex->lock_option= $1;
	  lex->local_file=  $2;
6633 6634
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
6635
	  if (!(lex->exchange= new sql_exchange($4.str, 0)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6636
	    YYABORT;
6637 6638 6639 6640 6641
        }
        opt_duplicate INTO
        {
	  LEX *lex=Lex;
	  lex->fname_end= lex->ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6642
	}
6643
        TABLE_SYM table_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6644
        {
6645 6646 6647 6648 6649 6650 6651
          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();
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6652
        }
6653 6654 6655
        opt_field_term opt_line_term opt_ignore_lines opt_field_or_var_spec
        opt_load_data_set_spec
        {}
6656
        |
6657
	FROM MASTER_SYM
6658 6659
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
6660
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6661 6662 6663

opt_local:
	/* empty */	{ $$=0;}
6664
	| LOCAL_SYM	{ $$=1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6665

6666
load_data_lock:
6667
	/* empty */	{ $$= YYTHD->update_lock_default; }
6668 6669
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
6670 6671


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6672 6673 6674
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
6675
	| IGNORE_SYM	{ Lex->ignore= 1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6676 6677 6678

opt_field_term:
	/* empty */
6679
	| COLUMNS field_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6680 6681 6682

field_term_list:
	field_term_list field_term
6683
	| field_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6684 6685

field_term:
6686 6687
	TERMINATED BY text_string 
          {
6688
            DBUG_ASSERT(Lex->exchange != 0);
6689 6690
            Lex->exchange->field_term= $3;
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6691
	| OPTIONALLY ENCLOSED BY text_string
6692
	  {
6693
            LEX *lex= Lex;
6694
            DBUG_ASSERT(lex->exchange != 0);
6695 6696
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
6697
	  }
6698
        | ENCLOSED BY text_string
6699
          {
6700
            DBUG_ASSERT(Lex->exchange != 0);
6701 6702
            Lex->exchange->enclosed= $3;
          }
6703
        | ESCAPED BY text_string
6704
          {
6705
            DBUG_ASSERT(Lex->exchange != 0);
6706 6707
            Lex->exchange->escaped= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6708 6709 6710

opt_line_term:
	/* empty */
6711
	| LINES line_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6712 6713 6714

line_term_list:
	line_term_list line_term
6715
	| line_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6716 6717

line_term:
6718
        TERMINATED BY text_string
6719
          {
6720
            DBUG_ASSERT(Lex->exchange != 0);
6721 6722
            Lex->exchange->line_term= $3;
          }
6723
        | STARTING BY text_string
6724
          {
6725
            DBUG_ASSERT(Lex->exchange != 0);
6726 6727
            Lex->exchange->line_start= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6728 6729 6730

opt_ignore_lines:
	/* empty */
6731 6732
        | IGNORE_SYM NUM LINES
          {
6733
            DBUG_ASSERT(Lex->exchange != 0);
6734 6735
            Lex->exchange->skip_lines= atol($2.str);
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6736

6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747
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); }
        ;
serg@serg.mylan's avatar
serg@serg.mylan committed
6748

6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759
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  { };


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6760 6761 6762
/* Common definitions */

text_literal:
6763
	TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6764 6765
	{
	  THD *thd= YYTHD;
6766
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6767
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6768
	| NCHAR_STRING
6769
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
6770
	| UNDERSCORE_CHARSET TEXT_STRING
6771
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
6772
	| text_literal TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6773 6774
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6775 6776

text_string:
6777
	TEXT_STRING_literal
6778
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6779 6780
	| HEX_NUM
	  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
6781
	    Item *tmp= new Item_hex_string($1.str, $1.length);
6782
	    /*
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
6783
	      it is OK only emulate fix_fields, because we need only
6784 6785 6786
              value of constant
	    */
	    $$= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6787
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6788
	      (String*) 0;
6789
	  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
6790 6791 6792 6793 6794 6795 6796 6797 6798 6799
        | 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;
          }
6800 6801
	;

6802
param_marker:
6803
        '?'
6804
        {
6805 6806 6807
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
          if (thd->command == COM_PREPARE)
6808
          {
6809 6810 6811 6812
            Item_param *item= new Item_param((uint) (lex->tok_start -
                                                     (uchar *) thd->query));
            if (!($$= item) || lex->param_list.push_back(item))
            {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6813
	      my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
6814 6815
	      YYABORT;
            }
6816
          }
6817
          else
6818
          {
6819
            yyerror(ER(ER_SYNTAX_ERROR));
6820 6821
            YYABORT;
          }
6822 6823 6824
        }
	;

6825 6826 6827
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
6828 6829
	| '-' NUM_literal
	  {
6830
	    $2->max_length++;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6831
	    $$= $2->neg();
6832
	  }
6833 6834 6835
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6836 6837
literal:
	text_literal	{ $$ =	$1; }
6838
	| NUM_literal	{ $$ = $1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6839
	| NULL_SYM	{ $$ =	new Item_null();
6840
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
6841 6842
	| FALSE_SYM	{ $$= new Item_int((char*) "FALSE",0,1); }
	| TRUE_SYM	{ $$= new Item_int((char*) "TRUE",1,1); }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
6843 6844
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
6845
	| UNDERSCORE_CHARSET HEX_NUM
6846
	  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
6847
	    Item *tmp= new Item_hex_string($2.str, $2.length);
6848
	    /*
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6849
	      it is OK only emulate fix_fieds, because we need only
6850 6851 6852
              value of constant
	    */
	    String *str= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6853
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6854
	      (String*) 0;
6855 6856 6857
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
6858
	  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872
	| 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);
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6873 6874
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
6875
	| TIMESTAMP text_literal { $$ = $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6876

6877
NUM_literal:
6878 6879
	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); }
6880
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
6881
        | DECIMAL_NUM
6882
	{
6883
           $$= new Item_decimal($1.str, $1.length, YYTHD->charset());
6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
6897
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
6898

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6899
/**********************************************************************
serg@serg.mylan's avatar
serg@serg.mylan committed
6900
** Creating different items.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6901 6902 6903
**********************************************************************/

insert_ident:
6904
	simple_ident_nospvar { $$=$1; }
6905
	| table_wild	 { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6906 6907

table_wild:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6908
	ident '.' '*'
6909 6910
	{
	  $$ = new Item_field(NullS,$1.str,"*");
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6911
	  Lex->current_select->with_wild++;
6912
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6913
	| ident '.' ident '.' '*'
6914 6915
	{
	  $$ = new Item_field((YYTHD->client_capabilities &
serg@serg.mylan's avatar
serg@serg.mylan committed
6916 6917
                             CLIENT_NO_SCHEMA ? NullS : $1.str),
                             $3.str,"*");
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6918
	  Lex->current_select->with_wild++;
6919 6920
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6921 6922

order_ident:
6923
	expr { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6924 6925

simple_ident:
6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950
	ident
	{
	  sp_pvar_t *spv;
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;

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

simple_ident_nospvar:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6951
	ident
6952
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6953
	  SELECT_LEX *sel=Select;
6954
	  $$= (sel->parsing_place != IN_HAVING ||
6955 6956
	       sel->get_in_sum_expr() > 0) ?
              (Item*) new Item_field(NullS,NullS,$1.str) :
6957
	      (Item*) new Item_ref(NullS,NullS,$1.str);
6958
	}
6959
	| simple_ident_q { $$= $1; }
serg@serg.mylan's avatar
serg@serg.mylan committed
6960
	;
6961 6962 6963

simple_ident_q:
	ident '.' ident
6964
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6965
	  THD *thd= YYTHD;
6966
	  LEX *lex= thd->lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
6967

6968 6969 6970 6971 6972 6973
          /*
            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 &&
serg@serg.mylan's avatar
serg@serg.mylan committed
6974
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") ||
6975 6976
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
6977
            Item_trigger_field *trg_fld;
6978
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
serg@serg.mylan's avatar
serg@serg.mylan committed
6979

6980 6981 6982
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
6983
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
6984 6985
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
6986

6987 6988 6989
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
6990
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
6991 6992
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
6993

6994 6995 6996 6997
            if (!(trg_fld= new Item_trigger_field(new_row ?
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
                                                  $3.str)))
6998
              YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
6999

7000 7001 7002 7003 7004 7005
            /*
              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);
serg@serg.mylan's avatar
serg@serg.mylan committed
7006

7007 7008 7009 7010 7011 7012 7013
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
7014 7015
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
7016 7017 7018 7019 7020 7021 7022
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
	        (Item*) new Item_field(NullS,$1.str,$3.str) :
	        (Item*) new Item_ref(NullS,$1.str,$3.str);
          }
        }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7023
	| '.' ident '.' ident
7024
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7025
	  THD *thd= YYTHD;
7026
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7027
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7028 7029
	  if (sel->no_table_names_allowed)
	  {
7030 7031
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $2.str, thd->where);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7032
	  }
7033
	  $$= (sel->parsing_place != IN_HAVING ||
7034 7035
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field(NullS,$2.str,$4.str) :
7036
              (Item*) new Item_ref(NullS, $2.str, $4.str);
7037
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7038
	| ident '.' ident '.' ident
7039
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7040
	  THD *thd= YYTHD;
7041
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7042
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7043 7044
	  if (sel->no_table_names_allowed)
	  {
7045 7046
	    my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                     MYF(0), $3.str, thd->where);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7047
	  }
7048
	  $$= (sel->parsing_place != IN_HAVING ||
7049 7050 7051 7052
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field((YYTHD->client_capabilities &
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
7053 7054
	      (Item*) new Item_ref((YYTHD->client_capabilities &
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
7055
                                   $3.str, $5.str);
7056
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7057 7058 7059 7060


field_ident:
	ident			{ $$=$1;}
7061
	| ident '.' ident	{ $$=$3;}	/* Skip schema name in create*/
7062
	| '.' ident		{ $$=$2;}	/* For Delphi */;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7063 7064 7065

table_ident:
	ident			{ $$=new Table_ident($1); }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7066
	| ident '.' ident	{ $$=new Table_ident(YYTHD, $1,$3,0);}
7067 7068 7069
	| '.' ident		{ $$=new Table_ident($2);} /* For Delphi */
        ;

serg@serg.mylan's avatar
serg@serg.mylan committed
7070
table_ident_nodb:
monty@mysql.com's avatar
monty@mysql.com committed
7071
	ident			{ LEX_STRING db={(char*) any_db,3}; $$=new Table_ident(YYTHD, db,$1,0); }
7072
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7073

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7074
IDENT_sys:
7075 7076 7077 7078 7079
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
7080 7081
            {
              CHARSET_INFO *cs= system_charset_info;
7082
              int dummy_error;
7083 7084
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
7085
                                                   $1.length, &dummy_error);
7086 7087
              if (wlen < $1.length)
              {
7088 7089
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
7090 7091
                YYABORT;
              }
7092
	      $$= $1;
7093
            }
7094 7095 7096 7097
	    else
	      thd->convert_string(&$$, system_charset_info,
				  $1.str, $1.length, thd->charset());
	  }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7098 7099 7100 7101 7102 7103
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7104 7105
	  if (thd->charset_is_system_charset)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7106
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7107 7108
	    thd->convert_string(&$$, system_charset_info,
				$1.str, $1.length, thd->charset());
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7109 7110 7111
	}
	;

7112
TEXT_STRING_literal:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7113 7114 7115
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7116 7117
	  if (thd->charset_is_collation_connection)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7118
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7119 7120
	    thd->convert_string(&$$, thd->variables.collation_connection,
				$1.str, $1.length, thd->charset());
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7121 7122 7123 7124
	}
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
7125
ident:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7126
	IDENT_sys	    { $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7127 7128
	| keyword
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7129 7130 7131
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
7132 7133
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7134 7135

ident_or_text:
serg@serg.mylan's avatar
serg@serg.mylan committed
7136
        ident                   { $$=$1;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7137 7138
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7139 7140 7141 7142

user:
	ident_or_text
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7143 7144
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7145
	    YYABORT;
7146 7147 7148 7149
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7150 7151
	| ident_or_text '@' ident_or_text
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7152 7153
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7154 7155
	      YYABORT;
	    $$->user = $1; $$->host=$3;
7156
	  }
7157
	| CURRENT_USER optional_braces
7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174
	{
          THD *thd= YYTHD;
          if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
            YYABORT;
          $$->user.str= thd->priv_user;
          $$->user.length= strlen(thd->priv_user);
          if (*thd->priv_host != 0)
          {
            $$->host.str= thd->priv_host;
            $$->host.length= strlen(thd->priv_host);
          }
          else
          {
            $$->host.str= (char *) "%";
            $$->host.length= 1;
          }
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7175 7176 7177 7178 7179

/* Keyword that we allow for identifiers */

keyword:
	ACTION			{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
7180
	| ADDDATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7181
	| AFTER_SYM		{}
7182
	| AGAINST		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7183
	| AGGREGATE_SYM		{}
7184
	| ALGORITHM_SYM		{}
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7185
	| ANY_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7186
	| ASCII_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7187
	| AUTO_INC		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7188 7189
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
7190
	| BACKUP_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7191
	| BEGIN_SYM		{}
7192
	| BERKELEY_DB_SYM	{}
7193
	| BINLOG_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7194 7195
	| BIT_SYM		{}
	| BOOL_SYM		{}
7196
	| BOOLEAN_SYM		{}
7197
	| BYTE_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7198
	| BTREE_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7199
	| CACHE_SYM		{}
7200
	| CASCADED              {}
7201
	| CHAIN_SYM		{}
7202
	| CHANGED		{}
7203
	| CHARSET		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7204
	| CHECKSUM_SYM		{}
7205
	| CIPHER_SYM		{}
7206
	| CLIENT_SYM		{}
7207
	| CLOSE_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7208
	| COLLATION_SYM		{}
7209
        | COLUMNS               {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7210
	| COMMENT_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7211
	| COMMITTED_SYM		{}
7212
	| COMMIT_SYM		{}
7213
	| COMPACT_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7214
	| COMPRESSED_SYM	{}
7215
	| CONCURRENT		{}
7216
	| CONSISTENT_SYM	{}
7217
	| CONTAINS_SYM          {}
7218
	| CUBE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7219 7220 7221 7222
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
sergefp@mysql.com's avatar
sergefp@mysql.com committed
7223
        | DEALLOCATE_SYM        {}
7224
	| DEFINER_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7225
	| DELAY_KEY_WRITE_SYM	{}
7226 7227
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
7228
	| DISCARD		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7229
	| DO_SYM		{}
7230 7231
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7232 7233 7234
	| DYNAMIC_SYM		{}
	| END			{}
	| ENUM			{}
7235
	| ENGINE_SYM		{}
7236
	| ENGINES_SYM		{}
7237
	| ERRORS		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7238
	| ESCAPE_SYM		{}
7239
	| EVENTS_SYM		{}
7240
	| EXECUTE_SYM		{}
7241
        | EXPANSION_SYM         {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7242
	| EXTENDED_SYM		{}
7243
	| FAST_SYM		{}
7244
	| FOUND_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7245 7246
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7247
	| FULL			{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7248 7249 7250 7251
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
	| FLUSH_SYM		{}
7252
	| FRAC_SECOND_SYM	{}
7253
	| GEOMETRY_SYM		{}
7254
	| GEOMETRYCOLLECTION	{}
7255
	| GET_FORMAT		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7256
	| GRANTS		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7257
	| GLOBAL_SYM		{}
7258
	| HANDLER_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7259
	| HASH_SYM		{}
7260
	| HELP_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7261 7262 7263
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
7264
	| INVOKER_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
7265
	| IMPORT		{}
7266
	| INDEXES		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7267
	| ISOLATION		{}
7268
	| ISSUER_SYM		{}
7269
	| INNOBASE_SYM		{}
7270
	| INSERT_METHOD		{}
7271
	| RELAY_THREAD		{}
7272 7273
	| LABEL_SYM             {}
	| LANGUAGE_SYM          {}
7274
	| LAST_SYM		{}
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
7275
	| LEAVES                {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7276
	| LEVEL_SYM		{}
7277
	| LINESTRING		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7278
	| LOCAL_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7279
	| LOCKS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7280 7281 7282 7283 7284 7285 7286 7287 7288
	| 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	{}
7289
	| MASTER_SERVER_ID_SYM  {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7290
	| MASTER_CONNECT_RETRY_SYM	{}
7291 7292 7293 7294 7295 7296
	| MASTER_SSL_SYM	{}
	| MASTER_SSL_CA_SYM	{}
	| MASTER_SSL_CAPATH_SYM	{}
	| MASTER_SSL_CERT_SYM	{}
	| MASTER_SSL_CIPHER_SYM	{}
	| MASTER_SSL_KEY_SYM	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7297 7298 7299
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
7300
	| MAX_USER_CONNECTIONS_SYM {}
7301
	| MEDIUM_SYM		{}
7302
	| MERGE_SYM		{}
7303
	| MICROSECOND_SYM	{}
7304
        | MIGRATE_SYM           {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7305 7306 7307
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7308
	| MODE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7309
	| MONTH_SYM		{}
7310 7311 7312
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
7313
        | MUTEX_SYM             {}
7314
	| NAME_SYM              {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7315
	| NAMES_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7316 7317
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
7318
	| NDBCLUSTER_SYM	{}
7319
	| NEXT_SYM		{}
7320
	| NEW_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7321
	| NO_SYM		{}
7322
	| NONE_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7323
	| NVARCHAR_SYM		{}
7324
	| OFFSET_SYM		{}
7325
	| OLD_PASSWORD		{}
7326
	| ONE_SHOT_SYM		{}
7327
        | ONE_SYM               {}
7328
	| OPEN_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7329
	| PACK_KEYS_SYM		{}
7330
	| PARTIAL		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7331
	| PASSWORD		{}
7332
        | PHASE_SYM             {}
7333
	| POINT_SYM		{}
7334
	| POLYGON		{}
sergefp@mysql.com's avatar
sergefp@mysql.com committed
7335
        | PREPARE_SYM           {}
7336
	| PREV_SYM		{}
7337
        | PRIVILEGES            {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7338 7339
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
7340
	| QUARTER_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7341
	| QUERY_SYM		{}
7342
	| QUICK			{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7343
	| RAID_0_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7344 7345
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7346
	| RAID_STRIPED_SYM	{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7347
	| RAID_TYPE		{}
7348
        | RECOVER_SYM           {}
serg@serg.mylan's avatar
serg@serg.mylan committed
7349
        | REDUNDANT_SYM         {}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7350 7351
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7352 7353
	| RELOAD		{}
	| REPAIR		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7354
	| REPEATABLE_SYM	{}
7355
	| REPLICATION		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7356
	| RESET_SYM		{}
7357
	| RESOURCES		{}
7358
	| RESTORE_SYM		{}
7359
        | RESUME_SYM            {}
7360
	| RETURNS_SYM           {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7361
	| ROLLBACK_SYM		{}
7362
	| ROLLUP_SYM		{}
7363
	| ROUTINE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7364 7365 7366
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7367
	| RTREE_SYM		{}
7368
	| SAVEPOINT_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7369
	| SECOND_SYM		{}
7370
	| SECURITY_SYM		{}
7371
	| SERIAL_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7372 7373
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7374
	| SIGNED_SYM		{}
7375
	| SIMPLE_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7376
	| SHARE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7377
	| SHUTDOWN		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7378
	| SLAVE			{}
7379
	| SNAPSHOT_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7380
	| SOUNDS_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7381
	| SQL_CACHE_SYM		{}
7382
	| SQL_BUFFER_RESULT	{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7383
	| SQL_NO_CACHE_SYM	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7384
	| SQL_THREAD		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7385 7386 7387
	| START_SYM		{}
	| STATUS_SYM		{}
	| STOP_SYM		{}
7388
	| STORAGE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7389
	| STRING_SYM		{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
7390
	| SUBDATE_SYM		{}
7391
	| SUBJECT_SYM		{}
7392
	| SUPER_SYM		{}
7393
        | SUSPEND_SYM           {}
7394
        | TABLES                {}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
7395
	| TABLESPACE		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7396
	| TEMPORARY		{}
7397
	| TEMPTABLE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7398
	| TEXT_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7399
	| TRANSACTION_SYM	{}
7400
	| TRUNCATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7401
	| TIMESTAMP		{}
7402 7403
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7404
	| TIME_SYM		{}
7405
	| TYPES_SYM		{}
7406
        | TYPE_SYM              {}
serg@serg.mylan's avatar
serg@serg.mylan committed
7407
        | UDF_RETURNS_SYM       {}
7408
	| FUNCTION_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7409
	| UNCOMMITTED_SYM	{}
7410
	| UNDEFINED_SYM		{}
7411
	| UNICODE_SYM		{}
7412
	| UNKNOWN_SYM		{}
7413
	| UNTIL_SYM		{}
7414
	| USER			{}
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7415
	| USE_FRM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7416
	| VARIABLES		{}
7417
	| VIEW_SYM		{}
7418
	| VALUE_SYM		{}
7419
	| WARNINGS		{}
7420
	| WEEK_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7421
	| WORK_SYM		{}
7422
	| X509_SYM		{}
7423
        | XA_SYM                {}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7424 7425
	| YEAR_SYM		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7426 7427 7428 7429 7430 7431

/* Option functions */

set:
	SET opt_option
	{
7432 7433
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
7434
	  mysql_init_select(lex);
7435
	  lex->option_type=OPT_SESSION;
7436
	  lex->var_list.empty();
7437
          lex->one_shot_set= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7438
	}
7439 7440 7441
	option_value_list
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7442 7443 7444

opt_option:
	/* empty */ {}
7445
	| OPTION {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7446 7447

option_value_list:
7448 7449 7450 7451 7452 7453 7454 7455 7456 7457
	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
serg@serg.mylan's avatar
serg@serg.mylan committed
7458 7459
              and sp_instr_set_trigger instructions share one LEX.
              (Well, it is theoretically possible but adds some extra
7460 7461 7462 7463 7464 7465 7466 7467
               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;
serg@serg.mylan's avatar
serg@serg.mylan committed
7468

7469 7470 7471 7472 7473 7474 7475 7476 7477
            /* 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;
          }
        }
7478
	ext_option_value
7479 7480
        {
          LEX *lex= Lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
7481

7482 7483 7484
          if (lex->sphead)
          {
            sp_head *sp= lex->sphead;
serg@serg.mylan's avatar
serg@serg.mylan committed
7485

7486 7487 7488 7489 7490 7491 7492 7493 7494
	    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;
serg@serg.mylan's avatar
serg@serg.mylan committed
7495

7496 7497 7498
              if (!(i= new sp_instr_stmt(sp->instructions(), lex->spcont,
                                         lex)))
                YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
7499

7500 7501 7502 7503
              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;
serg@serg.mylan's avatar
serg@serg.mylan committed
7504

7505 7506
              if (!(qbuff.str= alloc_root(YYTHD->mem_root, qbuff.length + 5)))
                YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
7507

7508 7509 7510 7511 7512 7513 7514 7515 7516
              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);
          }
        };
7517 7518

option_type:
7519 7520 7521 7522 7523 7524 7525 7526 7527
        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; }
7528 7529 7530 7531
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7532
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
7533 7534 7535 7536 7537 7538
	| LOCAL_SYM	{ $$=OPT_SESSION; }
	| SESSION_SYM	{ $$=OPT_SESSION; }
	;

opt_var_ident_type:
	/* empty */		{ $$=OPT_DEFAULT; }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7539
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
7540 7541 7542
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7543

7544 7545 7546
ext_option_value:
        sys_option_value
        | option_type2 option_value;
7547

7548 7549 7550 7551
sys_option_value:
        option_type internal_variable_name equal set_expr_or_default
        {
          LEX *lex=Lex;
7552

7553 7554 7555 7556 7557 7558 7559 7560 7561
          if ($2.var == &trg_new_row_fake_var)
          {
            /* We are in trigger and assigning value to field of new row */
            Item *it;
            sp_instr_set_trigger_field *i;
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
7562
            }
7563 7564 7565 7566 7567 7568 7569 7570
            if (lex->query_tables)
            {
              my_message(ER_SP_SUBSELECT_NYI, ER(ER_SP_SUBSELECT_NYI),
              MYF(0));
              YYABORT;
            }
            if ($4)
              it= $4;
7571
            else
7572 7573 7574 7575
            {
              /* QQ: Shouldn't this be field's default value ? */
              it= new Item_null();
            }
7576

7577 7578 7579 7580
            if (!(i= new sp_instr_set_trigger_field(
                lex->sphead->instructions(), lex->spcont,
                $2.base_name, it)))
              YYABORT;
7581

7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641
            /*
              Let us add this item to list of all Item_trigger_field
              objects in trigger.
            */
            lex->trg_table_fields.link_in_list((byte *)&i->trigger_field,
            (byte **)&i->trigger_field.next_trg_field);

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

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

            if ($4)
              it= $4;
            else if (spv->dflt)
              it= spv->dflt;
            else
              it= new Item_null();
            i= new sp_instr_set(lex->sphead->instructions(), ctx,
                                spv->offset, it, spv->type, lex, TRUE);
            lex->sphead->add_instr(i);
            spv->isset= TRUE;
          }
        }
        | option_type TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	{
	  LEX *lex=Lex;
          if (!$1)
            lex->option_type= (enum_var_type)$1;
	  lex->var_list.push_back(new set_var(lex->option_type,
                                              find_sys_var("tx_isolation"),
                                              &null_lex_str,
                                              new Item_int((int32) $5)));
	}
        ;

option_value:
	'@' ident_or_text equal expr
	{
          Lex->var_list.push_back(new set_var_user(new Item_func_set_user_var($2,$4)));
	}
7642
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
7643 7644
	  {
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7645 7646
	    lex->var_list.push_back(new set_var((enum_var_type) $3, $4.var,
						&$4.base_name, $6));
7647
	  }
7648
	| charset old_or_new_charset_name_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7649
	{
7650
	  THD *thd= YYTHD;
7651
	  LEX *lex= Lex;
7652
	  $2= $2 ? $2: global_system_variables.character_set_client;
7653
	  lex->var_list.push_back(new set_var_collation_client($2,thd->variables.collation_database,$2));
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7654
	}
7655
	| NAMES_SYM charset_name_or_default opt_collate
7656
	{
7657
	  LEX *lex= Lex;
7658
	  $2= $2 ? $2 : global_system_variables.character_set_client;
7659 7660
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
7661
	  {
7662 7663
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
7664
	    YYABORT;
7665
	  }
7666
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
7667
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7668
	| PASSWORD equal text_or_password
7669
	  {
7670
	    THD *thd=YYTHD;
7671
	    LEX_USER *user;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7672
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
7673
	      YYABORT;
7674
	    user->host=null_lex_str;
7675
	    user->user.str=thd->priv_user;
7676
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
7677 7678
	  }
	| PASSWORD FOR_SYM user equal text_or_password
7679
	  {
7680 7681
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
7682
	;
7683

7684 7685 7686
internal_variable_name:
	ident
	{
7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698
	  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;
7699
	    $$.base_name= null_lex_str;
7700 7701 7702 7703
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
7704 7705 7706
            if (tmp == &sys_time_zone &&
                lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
7707 7708 7709 7710 7711 7712 7713
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
7714
	}
7715 7716
	| ident '.' ident
	  {
7717
            LEX *lex= Lex;
7718 7719
            if (check_reserved_words(&$1))
            {
7720
	      yyerror(ER(ER_SYNTAX_ERROR));
7721 7722
              YYABORT;
            }
7723 7724 7725 7726 7727 7728
            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')
              {
7729
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
7730 7731 7732 7733
                YYABORT;
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
7734 7735
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
7736 7737 7738 7739
                YYABORT;
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
7740
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752
                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())
7753
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
7754 7755 7756
              $$.var= tmp;
              $$.base_name= $1;
            }
7757 7758 7759 7760 7761 7762 7763
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
7764
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7765 7766 7767
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
7768
	  }
7769
        ;
7770 7771 7772 7773 7774 7775 7776

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7778 7779 7780 7781
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
7782
	    $$= $3.length ? YYTHD->variables.old_passwords ?
7783 7784 7785 7786 7787 7788 7789 7790
	        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;
7791 7792
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7793

monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7794

7795
set_expr_or_default:
7796 7797
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
7798 7799
	| ON	  { $$=new Item_string("ON",  2, system_charset_info); }
	| ALL	  { $$=new Item_string("ALL", 3, system_charset_info); }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7800
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
7801
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7802 7803


bk@work.mysql.com's avatar
bk@work.mysql.com committed
7804 7805 7806 7807 7808
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
7809 7810 7811 7812
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
7813
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOCK");
7814 7815 7816
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_LOCK_TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7817
	}
7818 7819
	table_lock_list
	{}
7820
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7821 7822 7823

table_or_tables:
	TABLE_SYM
7824
	| TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7825 7826 7827

table_lock_list:
	table_lock
7828
	| table_lock_list ',' table_lock;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7829 7830 7831

table_lock:
	table_ident opt_table_alias lock_option
7832
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7833
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
7834 7835
	   YYABORT;
	}
7836
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7837 7838 7839

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
7840
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7841
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
7842 7843
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7844 7845

unlock:
7846 7847 7848 7849 7850 7851
	UNLOCK_SYM
	{
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
7852
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "UNLOCK");
7853 7854 7855 7856 7857 7858
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_UNLOCK_TABLES;
	}
	table_or_tables
	{}
7859
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7860 7861


7862
/*
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7863
** Handler: direct access to ISAM functions
7864 7865 7866 7867 7868
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
7869 7870
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_OPEN;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7871
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
7872 7873
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
7874
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
7875
	{
7876 7877
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_CLOSE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7878
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
7879 7880
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
7881
	| HANDLER_SYM table_ident_nodb READ_SYM
7882
	{
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
7883 7884 7885
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
7886 7887
	  lex->current_select->select_limit= 1;
	  lex->current_select->offset_limit= 0L;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7888
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
7889
	    YYABORT;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7890
        }
7891
        handler_read_or_scan where_clause opt_limit_clause {}
7892
        ;
7893

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7894
handler_read_or_scan:
7895 7896
	handler_scan_function         { Lex->ident= null_lex_str; }
        | ident handler_rkey_function { Lex->ident= $1; }
7897
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7898 7899 7900

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
7901 7902
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7903 7904 7905 7906 7907 7908

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;  }
7909 7910
	| handler_rkey_mode
	{
7911 7912
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
7913
	  lex->ha_rkey_mode=$1;
7914
	  if (!(lex->insert_list = new List_item))
7915
	    YYABORT;
7916 7917
	} '(' values ')' { }
        ;
7918 7919

handler_rkey_mode:
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
7920 7921 7922 7923
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
7924 7925
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
7926

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7927 7928 7929
/* GRANT / REVOKE */

revoke:
7930
	REVOKE clear_privileges revoke_command
7931 7932 7933 7934
	{}
        ;

revoke_command:
7935
	grant_privileges ON opt_table grant_ident FROM grant_list
7936
	{
7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964
          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;
7965
        }
7966
	|
7967
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
7968 7969 7970
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
7971
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7972 7973

grant:
7974 7975 7976 7977 7978 7979
	GRANT clear_privileges grant_command
	{}
        ;

grant_command:
	grant_privileges ON opt_table grant_ident TO_SYM grant_list
7980
	require_clause grant_options
7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012
	{
          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;
        }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8013

8014 8015 8016 8017
opt_table:
	/* Empty */
	| TABLE_SYM ;
        
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8018
grant_privileges:
8019 8020 8021 8022 8023 8024
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
8025
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8026

8027 8028 8029 8030 8031
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

8032 8033 8034
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8035

8036
object_privilege:
serg@serg.mylan's avatar
serg@serg.mylan committed
8037
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
8038 8039 8040
	| 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 {}
8041 8042
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
8043
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8044 8045 8046
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
8047
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8048 8049 8050 8051
	| RELOAD	{ Lex->grant |= RELOAD_ACL;}
	| SHUTDOWN	{ Lex->grant |= SHUTDOWN_ACL;}
	| PROCESS	{ Lex->grant |= PROCESS_ACL;}
	| FILE_SYM	{ Lex->grant |= FILE_ACL;}
8052 8053 8054 8055 8056
	| 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; }
8057 8058 8059 8060
	| 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; }
8061 8062
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
8063
	| CREATE USER { Lex->grant |= CREATE_USER_ACL; }
8064
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8065

8066

8067 8068
opt_and:
	/* empty */	{}
8069
	| AND_SYM	{}
8070 8071 8072 8073 8074 8075 8076 8077 8078
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
8079 8080 8081 8082
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
8083
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
8084 8085 8086 8087 8088 8089 8090 8091 8092
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
8093
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
8094 8095 8096 8097 8098 8099 8100 8101 8102
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
8103
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
8104 8105 8106 8107 8108
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
8109

8110
grant_ident:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8111 8112
	'*'
	  {
8113
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8114
	    lex->current_select->db= lex->thd->db;
8115
	    if (lex->grant == GLOBAL_ACLS)
8116 8117
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8118
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8119 8120
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
8121
	      YYABORT;
8122
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8123 8124 8125
	  }
	| ident '.' '*'
	  {
8126
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8127
	    lex->current_select->db = $1.str;
8128
	    if (lex->grant == GLOBAL_ACLS)
8129 8130
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8131
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8132 8133
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8134 8135 8136 8137 8138
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
8139
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8140
	    lex->current_select->db = NULL;
8141 8142
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
8143
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8144
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8145 8146
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8147 8148 8149 8150 8151
	      YYABORT;
	    }
	  }
	| table_ident
	  {
8152
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8153
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8154
	      YYABORT;
8155
	    if (lex->grant == GLOBAL_ACLS)
8156
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
8157 8158
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8159 8160 8161


user_list:
8162 8163 8164 8165 8166 8167 8168 8169 8170 8171
	user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;


grant_list:
8172
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
8173
	| grant_list ',' grant_user
8174 8175 8176 8177 8178
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8179 8180 8181 8182 8183 8184 8185 8186


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
8187
             if (YYTHD->variables.old_passwords)
8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204
             {
               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;
             }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8205 8206 8207
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
8208
	  { $$= $1; $1->password= $5; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8209
	| user
8210
	  { $$= $1; $1->password= null_lex_str; }
8211
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8212 8213 8214


opt_column_list:
8215 8216 8217 8218 8219
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
8220
	| '(' column_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8221 8222 8223

column_list:
	column_list ',' column_list_id
8224
	| column_list_id;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8225 8226 8227 8228

column_list_id:
	ident
	{
8229
	  String *new_str = new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8230 8231
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
8232
	  LEX *lex=Lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8233 8234
	  while ((point=iter++))
	  {
8235 8236
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8237 8238
		break;
	  }
8239
	  lex->grant_tot_col|= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8240
	  if (point)
8241
	    point->rights |= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8242
	  else
8243
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
8244 8245
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8246

tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
8247 8248

require_clause: /* empty */
8249
        | REQUIRE_SYM require_list
8250 8251 8252
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
8253
        | REQUIRE_SYM SSL_SYM
8254 8255 8256
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
8257
        | REQUIRE_SYM X509_SYM
8258 8259 8260 8261 8262 8263 8264
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
8265
          ;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
8266

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8267
grant_options:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8268
	/* empty */ {}
8269
	| WITH grant_option_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8270

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8271 8272
grant_option_list:
	grant_option_list grant_option {}
8273 8274
	| grant_option {}
        ;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8275 8276 8277

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
serg@serg.mylan's avatar
serg@serg.mylan committed
8278
        | MAX_QUERIES_PER_HOUR ulong_num
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8279
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
8280 8281 8282
	  LEX *lex=Lex;
	  lex->mqh.questions=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
8283
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
8284
        | MAX_UPDATES_PER_HOUR ulong_num
8285
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
8286 8287 8288
	  LEX *lex=Lex;
	  lex->mqh.updates=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
8289
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
8290
        | MAX_CONNECTIONS_PER_HOUR ulong_num
8291
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
8292 8293 8294
	  LEX *lex=Lex;
	  lex->mqh.conn_per_hour= $2;
	  lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
8295
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
8296
        | MAX_USER_CONNECTIONS_SYM ulong_num
8297
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
8298 8299 8300
	  LEX *lex=Lex;
          lex->mqh.user_conn= $2;
          lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
8301 8302
	}
        ;
8303

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8304
begin:
serg@serg.mylan's avatar
serg@serg.mylan committed
8305 8306 8307 8308 8309 8310 8311
	BEGIN_SYM  
        {
	  LEX *lex=Lex;
          lex->sql_command = SQLCOM_BEGIN;
          lex->start_transaction_opt= 0;
        }
        opt_work {}
8312
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8313 8314 8315

opt_work:
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
8316
	| WORK_SYM  {}
8317
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8318

8319
opt_chain:
serg@serg.mylan's avatar
serg@serg.mylan committed
8320
	/* empty */ { $$= (YYTHD->variables.completion_type == 1); }
8321 8322 8323 8324 8325
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
serg@serg.mylan's avatar
serg@serg.mylan committed
8326
	/* empty */ { $$= (YYTHD->variables.completion_type == 2); }
8327 8328 8329 8330 8331 8332 8333 8334 8335
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8336
commit:
serg@serg.mylan's avatar
serg@serg.mylan committed
8337
	COMMIT_SYM opt_work opt_chain opt_release
8338
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
8339 8340 8341 8342
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_COMMIT;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
8343 8344
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8345 8346

rollback:
serg@serg.mylan's avatar
serg@serg.mylan committed
8347
	ROLLBACK_SYM opt_work opt_chain opt_release
8348
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
8349 8350 8351 8352
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
8353
	}
8354 8355
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
8356
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
8357 8358 8359
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK_TO_SAVEPOINT;
	  lex->ident= $5;
8360 8361 8362
	}
	;

8363 8364 8365
savepoint:
	SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
8366 8367 8368
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SAVEPOINT;
	  lex->ident= $2;
8369 8370 8371 8372 8373 8374
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
8375 8376 8377
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RELEASE_SAVEPOINT;
	  lex->ident= $3;
8378 8379
	}
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
8380
  
8381
/*
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8382
   UNIONS : glue selects together
8383 8384 8385
*/


8386
union_clause:
8387
	/* empty */ {}
8388 8389
	| union_list
	;
8390 8391

union_list:
8392
	UNION_SYM union_option
8393 8394 8395 8396 8397
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
8398
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
8399
	    YYABORT;
8400
	  }
8401
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
8402
	  {
8403
            yyerror(ER(ER_SYNTAX_ERROR));
8404 8405
	    YYABORT;
	  }
8406
	  if (mysql_new_select(lex, 0))
8407
	    YYABORT;
8408
          mysql_init_select(lex);
8409
	  lex->current_select->linkage=UNION_TYPE;
8410 8411 8412
          if ($2) /* UNION DISTINCT - remember position */
            lex->current_select->master_unit()->union_distinct=
                                                      lex->current_select;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8413
	}
8414
	select_init {}
8415
	;
8416 8417

union_opt:
8418 8419 8420
	/* Empty */ { $$= 0; }
	| union_list { $$= 1; }
	| union_order_or_limit { $$= 1; }
8421
	;
8422

8423
union_order_or_limit:
8424
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8425
	    THD *thd= YYTHD;
8426
	    LEX *lex= thd->lex;
8427
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8428
	    SELECT_LEX *sel= lex->current_select;
8429
	    SELECT_LEX_UNIT *unit= sel->master_unit();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8430
	    SELECT_LEX *fake= unit->fake_select_lex;
8431 8432 8433 8434 8435 8436
	    if (fake)
	    {
	      unit->global_parameters= fake;
	      fake->no_table_names_allowed= 1;
	      lex->current_select= fake;
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8437
	    thd->where= "global ORDER clause";
8438
	  }
8439
	order_or_limit
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8440 8441
          {
	    THD *thd= YYTHD;
8442
	    thd->lex->current_select->no_table_names_allowed= 0;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8443 8444
	    thd->where= "";
          }
8445 8446 8447
	;

order_or_limit:
8448 8449
	order_clause opt_limit_clause_init
	| limit_clause
8450
	;
8451 8452

union_option:
8453 8454 8455 8456
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
8457

8458 8459
singlerow_subselect:
	subselect_start singlerow_subselect_init
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8460 8461 8462 8463
	subselect_end
	{
	  $$= $2;
	};
8464

8465
singlerow_subselect_init:
8466
	select_init2
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8467
	{
8468 8469
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8470
	};
8471 8472

exists_subselect:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8473 8474 8475 8476 8477
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
8478 8479

exists_subselect_init:
8480
	select_init2
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8481
	{
8482 8483
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8484
	};
8485

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8486 8487 8488 8489 8490 8491 8492 8493
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
8494
  select_init2
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8495
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8496
    $$= Lex->current_select->master_unit()->first_select();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8497 8498
  };

8499
subselect_start:
8500
	'(' SELECT_SYM
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8501
	{
8502
	  LEX *lex=Lex;
8503
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
8504 8505
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
8506 8507
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
8508 8509
	    YYABORT;
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8510 8511 8512
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
8513 8514

subselect_end:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8515 8516 8517
	')'
	{
	  LEX *lex=Lex;
8518
	  lex->current_select = lex->current_select->return_after_parsing();
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
8519
	};
serg@serg.mylan's avatar
serg@serg.mylan committed
8520

8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564
opt_view_list:
	/* empty */ {}
	| '(' view_list ')'
	;

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

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

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

8565 8566
xa: XA_SYM begin_or_start xid opt_join_or_resume
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
8567
        Lex->sql_command = SQLCOM_XA_START;
8568 8569 8570
      }
    | XA_SYM END xid opt_suspend_or_migrate
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
8571
        Lex->sql_command = SQLCOM_XA_END;
8572 8573 8574
      }
    | XA_SYM PREPARE_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
8575
        Lex->sql_command = SQLCOM_XA_PREPARE;
8576 8577 8578
      }
    | XA_SYM COMMIT_SYM xid opt_one_phase
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
8579
        Lex->sql_command = SQLCOM_XA_COMMIT;
8580 8581 8582
      }
    | XA_SYM ROLLBACK_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
8583
        Lex->sql_command = SQLCOM_XA_ROLLBACK;
8584 8585 8586
      }
    | XA_SYM RECOVER_SYM
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
8587
        Lex->sql_command = SQLCOM_XA_RECOVER;
8588 8589 8590
      }
    ;

serg@serg.mylan's avatar
serg@serg.mylan committed
8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612
xid: text_string
     {
       TEST_ASSERT($1->length() <= MAXGTRIDSIZE);
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set(1L, $1->ptr(), $1->length(), 0, 0);
     }
     | text_string ',' text_string
     {
       TEST_ASSERT($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
       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
     {
       TEST_ASSERT($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set($5, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     ;
8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635

begin_or_start:   BEGIN_SYM {}
    |             START_SYM {}
    ;

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

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

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