sql_yacc.yy 219 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 3516 3517 3518
	| FORCE_SYM
	  {
	    Lex->alter_info.flags|= ALTER_FORCE;
	   }
3519
	| order_clause
3520 3521
	  {
	    LEX *lex=Lex;
3522
	    lex->alter_info.flags|= ALTER_ORDER;
3523
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3524 3525 3526

opt_column:
	/* empty */	{}
3527
	| COLUMN_SYM	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3528 3529

opt_ignore:
3530 3531 3532
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3533 3534

opt_restrict:
3535 3536 3537 3538
	/* 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
3539 3540 3541 3542

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
3543
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3544 3545 3546 3547

opt_to:
	/* empty */	{}
	| TO_SYM	{}
3548
	| EQ		{}
3549
	| AS		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3550

nick@mysql.com's avatar
nick@mysql.com committed
3551
/*
3552
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3553 3554
*/

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

3593
start:
3594 3595 3596 3597 3598
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
           Lex->sql_command = SQLCOM_BEGIN;
           Lex->start_transaction_opt= $3;
        }
3599 3600
	;

3601 3602 3603 3604 3605 3606
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
3607
        ;
3608

3609
slave_thread_opts:
3610 3611
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
3612
        {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3613
	;
3614 3615

slave_thread_opt_list:
3616
	slave_thread_opt
3617 3618
	| slave_thread_opt_list ',' slave_thread_opt
	;
3619 3620

slave_thread_opt:
3621
	/*empty*/	{}
3622
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
3623
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
3624
	;
3625

3626 3627 3628 3629 3630 3631 3632 3633 3634 3635
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
3636 3637
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


3649 3650 3651 3652 3653
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
3654
	table_list FROM TEXT_STRING_sys
3655 3656
        {
	  Lex->backup_dir = $6.str;
3657
        };
3658

3659 3660 3661 3662 3663
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
3664
	table_list TO_SYM TEXT_STRING_sys
3665 3666
        {
	  Lex->backup_dir = $6.str;
3667
        };
3668

3669 3670 3671 3672 3673 3674
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
3675 3676
	table_list opt_checksum_type
        {}
3677 3678
	;

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

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3697
opt_mi_repair_type:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3698
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
3699
	| mi_repair_types {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3700

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3701 3702
mi_repair_types:
	mi_repair_type {}
3703
	| mi_repair_type mi_repair_types {};
3704

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

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

check:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3723
	CHECK_SYM table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3724
	{
3725 3726 3727 3728 3729 3730 3731 3732 3733
	  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
3734
	}
3735 3736 3737
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3738

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3739 3740
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
3741
	| mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3742 3743 3744

mi_check_types:
	mi_check_type {}
3745
	| mi_check_type mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
3746 3747 3748 3749 3750 3751

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

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

3766 3767 3768
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
3769
	| LOCAL_SYM  { $$= 1; }
3770 3771
	;

3772 3773 3774 3775 3776
rename:
	RENAME table_or_tables
	{
	   Lex->sql_command=SQLCOM_RENAME_TABLE;
	}
3777 3778
	table_to_table_list
	{}
3779 3780 3781 3782
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
3783
	;
3784

3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797
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;
          }
        ;

3798 3799
table_to_table_list:
	table_to_table
3800
	| table_to_table_list ',' table_to_table;
3801 3802 3803

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

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

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

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

3840 3841 3842
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
3843
	;
3844

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3845
preload:
3846
	LOAD INDEX_SYM INTO CACHE_SYM
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859
	{
	  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
3860
	table_ident cache_keys_spec opt_ignore_leaves
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3861 3862 3863
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3864 3865
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3866 3867 3868 3869 3870 3871
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

3872
cache_keys_spec:
3873
        { Select->interval_list.empty(); }
3874 3875 3876 3877 3878 3879 3880
        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
3881

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

3891
opt_ignore_leaves:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3892 3893 3894 3895 3896
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3897
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3898
  Select : retrieve data from table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3899 3900 3901 3902
*/


select:
3903 3904 3905 3906 3907
	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
3908 3909
	}
	;
3910

3911
/* Need select_init2 for subselects. */
3912
select_init:
3913
	SELECT_SYM select_init2
3914
	|
3915 3916 3917 3918
	'(' select_paren ')' union_opt;

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

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

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3974
select_into:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
3975
	opt_order_clause opt_limit_clause {}
3976
        | into
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3977
	| select_from
3978 3979
	| into select_from
	| select_from into;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3980 3981

select_from:
3982 3983
	  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
3984 3985 3986 3987 3988
        | 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 :) */
3989
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3990 3991 3992

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

select_option_list:
	select_option_list select_option
4005
	| select_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4006 4007

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

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4055 4056 4057 4058 4059
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
4060 4061
	    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
4062
	      YYABORT;
4063
	    (thd->lex->current_select->with_wild)++;
4064
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4065 4066 4067 4068 4069


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

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

remember_end:
4086
	{ $$=(char*) Lex->tok_end; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4087 4088 4089

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

select_alias:
4093
	/* empty */		{ $$=null_lex_str;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4094 4095 4096 4097 4098
	| 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
4099 4100 4101

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

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

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

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); }
4170 4171 4172 4173 4174
	| 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); }
4175 4176 4177 4178 4179 4180
	| predicate ;

predicate:
	 bit_expr IN_SYM '(' expr_list ')'
	  { $4->push_front($1); $$= new Item_func_in(*$4); }
	| bit_expr not IN_SYM '(' expr_list ')'
4181
	  { $5->push_front($1); $$= negate_expression(YYTHD, new Item_func_in(*$5)); }
4182 4183 4184
        | bit_expr IN_SYM in_subselect
	  { $$= new Item_in_subselect($1, $3); }
	| bit_expr not IN_SYM in_subselect
4185
          { $$= negate_expression(YYTHD, new Item_in_subselect($1, $4)); }
4186 4187 4188 4189
	| 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)); }
4190 4191 4192 4193 4194 4195 4196 4197 4198
	| 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
4199
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
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 4238 4239 4240 4241
	| 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
4242

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254
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; }
        ;

4255
interval_expr:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4256
         INTERVAL_SYM expr { $$=$2; }
4257 4258
        ;

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

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

	    name->init_qname(YYTHD);
4637
	    sp_add_to_hash(&lex->spfuns, name);
4638 4639
	    if ($5)
	      $$= new Item_func_sp(name, *$5);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4640
	    else
4641
	      $$= new Item_func_sp(name);
4642
	    lex->safe_to_cache_query=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
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 4696 4697 4698 4699
	| 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;
4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715
              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;
4716 4717 4718 4719 4720 4721 4722
              default:
                YYABORT;
              }
            }
            else
#endif /* HAVE_DLOPEN */
            {
4723
	      LEX *lex= Lex;
4724 4725
              sp_name *name= sp_name_current_db_new(YYTHD, $1);

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

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

4836 4837 4838 4839 4840 4841
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
4842
udf_expr_list:
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 4870 4871 4872 4873
	/* 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
4874 4875 4876 4877

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

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

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

4942 4943

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4957 4958

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

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4988
expr_list:
4989
	{ Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4990
	expr_list2
4991
	{ $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4992 4993

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

4997 4998
ident_list_arg:
          ident_list          { $$= $1; }
4999
        | '(' ident_list ')'  { $$= $2; };
5000

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

ident_list2:
5007
        simple_ident { Select->expr_list.head()->push_back($1); }
5008
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5009 5010 5011

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

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

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

when_list2:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5024
	expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5025
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5026
	    SELECT_LEX *sel=Select;
5027 5028
	    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
5029
	}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5030
	| when_list2 WHEN_SYM expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5031
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5032
	    SELECT_LEX *sel=Select;
5033 5034
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
5035
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5036

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5048
join_table_list:
5049 5050
	derived_table_list		{ TEST_ASSERT($$=$1); }
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
5051

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

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

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5126 5127

normal_join:
5128 5129 5130 5131
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5132

5133
/* Warning - may return NULL in case of incomplete SELECT */
5134
table_factor:
5135
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5136
	  SELECT_LEX *sel= Select;
5137
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5138
	  sel->table_join_options= 0;
5139
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5140
        table_ident opt_table_alias opt_key_definition
5141
	{
5142
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5143
	  SELECT_LEX *sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5144
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5145
					   sel->get_table_join_options(),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5146
					   lex->lock_option,
5147 5148
					   sel->get_use_index(),
					   sel->get_ignore_index())))
5149
	    YYABORT;
5150
          sel->add_joined_table($$);
5151
	}
5152 5153 5154
	| '{' 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
5155
          {
5156
            LEX *lex= Lex;
5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170
            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
5171
              YYABORT;
5172
            $$= 0;
serg@serg.mylan's avatar
serg@serg.mylan committed
5173
            /* incomplete derived tables return NULL, we must be
5174
               nested in select_derived rule to be here. */
5175
          }
5176 5177 5178 5179 5180
	| '(' 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
5181
          if (!($3 || $6) && $2->embedding &&
5182
              !$2->embedding->nested_join->join_list.elements)
5183
          {
5184 5185 5186 5187 5188
            /* 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;
5189
          }
5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203
          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
5204

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

5220
/* handle contents of parentheses in join expression */
5221
select_derived:
5222
	  get_select_lex
5223
	  {
5224 5225 5226 5227 5228 5229 5230 5231 5232
            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
5233

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

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

5269 5270 5271 5272 5273 5274 5275 5276
get_select_lex:
	/* Empty */ { $$= Select; }
        ;

select_derived_init:
          SELECT_SYM
          {
            LEX *lex= Lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
5277
            SELECT_LEX *sel= lex->current_select;
5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291
            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
5292 5293
opt_outer:
	/* empty */	{}
5294
	| OUTER		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5295 5296 5297 5298

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
5299
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5300
	    SELECT_LEX *sel= Select;
5301 5302 5303
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5304 5305
	| FORCE_SYM key_usage_list
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5306
	    SELECT_LEX *sel= Select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5307 5308 5309 5310
	    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
5311
	| IGNORE_SYM key_usage_list
5312
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5313
	    SELECT_LEX *sel= Select;
5314 5315
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
5316
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5317 5318

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

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5328 5329 5330

key_usage_list2:
	key_usage_list2 ',' 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*) $3.str, $3.length,
5333
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5334
	| ident
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((const char*) $1.str, $1.length,
5337
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5338
	| PRIMARY_SYM
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5339
        { Select->
5340
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
5341
				    system_charset_info)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5342 5343 5344

using_list:
	ident
5345
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5346
	    SELECT_LEX *sel= Select;
5347 5348 5349 5350
	    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
5351 5352 5353 5354
	      YYABORT;
	  }
	| using_list ',' ident
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5355
	    SELECT_LEX *sel= Select;
5356
	    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
5357
	      YYABORT;
5358
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5359 5360

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

5387
date_time_type:
5388 5389 5390 5391 5392
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
5393

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5394 5395 5396
table_alias:
	/* empty */
	| AS
5397
	| EQ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5398 5399 5400 5401

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

5404 5405 5406 5407
opt_all:
	/* empty */
	| ALL
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5408 5409

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

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

opt_escape:
5442 5443
	ESCAPE_SYM simple_expr { $$= $2; }
	| /* empty */
5444 5445 5446 5447 5448
          {

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


/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5454
   group by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5455 5456 5457 5458
*/

group_clause:
	/* empty */
5459
	| GROUP BY group_list olap_opt;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5460 5461

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

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5495
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5496
   Order by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5497 5498
*/

5499
opt_order_clause:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5500
	/* empty */
5501
	| order_clause;
5502 5503

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

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

order_dir:
	/* empty */ { $$ =  1; }
5525
	| ASC  { $$ =1; }
5526
	| DESC { $$ =0; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5527 5528


5529 5530 5531
opt_limit_clause_init:
	/* empty */
	{
5532 5533
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
5534
          sel->offset_limit= 0L;
5535
          sel->select_limit= HA_POS_ERROR;
5536
	}
5537 5538 5539
	| limit_clause {}
	;

5540 5541 5542 5543 5544
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

5545
limit_clause:
5546
	LIMIT limit_options {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5547
	;
5548 5549

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


delete_limit_clause:
	/* empty */
	{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5577
	  LEX *lex=Lex;
5578
	  lex->current_select->select_limit= HA_POS_ERROR;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5579
	}
5580
	| LIMIT ulonglong_num
5581 5582 5583 5584 5585
	{
	  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
5586

serg@serg.mylan's avatar
serg@serg.mylan committed
5587 5588 5589
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
5590 5591
	| 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
5592
        | DECIMAL_NUM   { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
monty@mysql.com's avatar
monty@mysql.com committed
5593 5594
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5595

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

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
5609 5610
	    if (&lex->select_lex != lex->current_select)
	    {
5611
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
5612 5613
	      YYABORT;
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5614 5615 5616
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
5617
	    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
5618
	      YYABORT;
5619
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5620
	  }
5621
	  '(' procedure_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5622 5623 5624 5625


procedure_list:
	/* empty */ {}
5626
	| procedure_list2 {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5627 5628 5629

procedure_list2:
	procedure_list2 ',' procedure_item
5630
	| procedure_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5631 5632 5633 5634

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


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

Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5654
select_var_list:
5655 5656 5657 5658
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

5659 5660 5661 5662 5663 5664 5665 5666 5667 5668
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
5669
           {
5670
             LEX *lex=Lex;
5671 5672 5673 5674
	     sp_pvar_t *t;

	     if (!lex->spcont || !(t=lex->spcont->find_pvar(&$1)))
	     {
5675
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5676
	       YYABORT;
5677 5678 5679 5680 5681 5682 5683 5684
	     }
	     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
5685
	   }
5686
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5687

5688
into:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5689
        INTO OUTFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5690
	{
5691
          LEX *lex= Lex;
5692 5693 5694 5695
          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
5696 5697
	}
	opt_field_term opt_line_term
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5698
	| INTO DUMPFILE TEXT_STRING_sys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5699
	{
5700
	  LEX *lex=Lex;
5701 5702
	  if (!lex->describe)
	  {
5703
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5704 5705 5706 5707 5708
	    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
5709
	}
5710
        | INTO select_var_list_init
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5711
	{
5712
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
5713 5714
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5715

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5716 5717 5718
/*
  DO statement
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5719

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5732
/*
5733
  Drop : delete tables or index or user
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5734 5735 5736
*/

drop:
serg@serg.mylan's avatar
serg@serg.mylan committed
5737
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5738
	{
5739 5740
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
5741 5742
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5743
	}
5744
	| DROP INDEX_SYM ident ON table_ident {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5745
	  {
5746 5747
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
5748 5749 5750
	     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
5751 5752
	     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
5753 5754 5755 5756
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
5757 5758 5759 5760
	    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
5761
	 }
5762
	| DROP FUNCTION_SYM if_exists sp_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5763
	  {
5764
	    LEX *lex=Lex;
5765 5766
	    if (lex->sphead)
	    {
5767
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
5768 5769
	      YYABORT;
	    }
5770
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
5771 5772 5773 5774 5775 5776 5777 5778
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
	  }
	| DROP PROCEDURE if_exists sp_name
	  {
	    LEX *lex=Lex;
	    if (lex->sphead)
	    {
5779
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
5780 5781 5782 5783 5784
	      YYABORT;
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
5785
	  }
5786
	| DROP USER clear_privileges user_list
5787
	  {
5788 5789
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
5790 5791 5792 5793 5794 5795 5796 5797 5798 5799
	| 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
5800

5801 5802 5803 5804 5805 5806 5807 5808
            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;
5809
            lex->ident= $5;
5810 5811
          }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5812 5813

table_list:
5814
	table_name
5815
	| table_list ',' table_name;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5816

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

if_exists:
5826
	/* empty */ { $$= 0; }
5827 5828
	| IF EXISTS { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5829

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

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

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

insert_lock_option:
5877 5878 5879 5880
	/* empty */	{ $$= TL_WRITE_CONCURRENT_INSERT; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
5881
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5882 5883

replace_lock_option:
5884 5885
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5886 5887 5888

insert2:
	INTO insert_table {}
5889
	| insert_table {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5890 5891

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

insert_field_spec:
serg@serg.mylan's avatar
serg@serg.mylan committed
5901 5902 5903
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5904 5905
	| SET
	  {
5906 5907 5908
	    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
5909 5910
	      YYABORT;
	   }
5911
	   ident_eq_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5912 5913 5914

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
5915
	| insert_ident		{ Lex->field_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5916 5917 5918

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

values_list:
	values_list ','  no_braces
5926
	| no_braces;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5927 5928 5929 5930

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
5931
	ident_eq_value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5932 5933

ident_eq_value:
5934
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5935
	 {
5936 5937 5938
	  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
5939
	    YYABORT;
5940
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5941 5942

equal:	EQ		{}
5943 5944 5945 5946 5947 5948 5949
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5950 5951 5952 5953 5954 5955 5956 5957 5958

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

opt_values:
	/* empty */ {}
5966
	| values;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5967 5968

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

expr_or_default:
5982
	expr	  { $$= $1;}
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
5983
	| DEFAULT {$$= new Item_default_value(); }
5984
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5985

5986 5987
opt_insert_update:
        /* empty */
monty@mysql.com's avatar
monty@mysql.com committed
5988
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
5989
          KEY_SYM UPDATE_SYM insert_update_list
5990 5991
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5992 5993 5994
/* Update rows in a table */

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

update_list:
6027 6028 6029 6030
	update_list ',' update_elem
	| update_elem;

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

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
monty@mishka.local's avatar
monty@mishka.local committed
6042
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6043
	  {
6044 6045 6046
	  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
6047
	      YYABORT;
6048
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6049 6050

opt_low_priority:
6051
	/* empty */	{ $$= YYTHD->update_lock_default; }
6052
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6053 6054 6055 6056

/* Delete rows from a table */

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

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

table_wild_list:
	  table_wild_one {}
6089
	  | table_wild_list ',' table_wild_one {};
6090 6091

table_wild_one:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6092
	ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6093
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6094 6095
	  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
6096 6097
	    YYABORT;
        }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6098
	| ident '.' ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6099
	  {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6100 6101
	    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
6102 6103
					   $5, TL_OPTION_UPDATING,
					   Lex->lock_option))
6104
	      YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6105
	  }
6106
	;
6107 6108

opt_wild:
6109
	/* empty */	{}
6110
	| '.' '*'	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6111 6112


6113
opt_delete_options:
6114
	/* empty */	{}
6115
	| opt_delete_option opt_delete_options {};
6116 6117

opt_delete_option:
6118
	QUICK		{ Select->options|= OPTION_QUICK; }
6119
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
6120
	| IGNORE_SYM	{ Lex->ignore= 1; };
6121

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

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6132 6133
opt_table_sym:
	/* empty */
6134
	| TABLE_SYM;
6135

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6136 6137
/* Show things */

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

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

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

6425 6426 6427 6428
master_or_binary:
	MASTER_SYM
	| BINARY;

6429 6430 6431 6432
opt_storage:
	/* empty */
	| STORAGE_SYM;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6433 6434
opt_db:
	/* empty */  { $$= 0; }
6435
	| from_or_in ident { $$= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6436

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6437 6438
opt_full:
	/* empty */ { Lex->verbose=0; }
6439
	| FULL	    { Lex->verbose=1; };
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6440

6441 6442
from_or_in:
	FROM
6443
	| IN_SYM;
6444

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

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

6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465
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();
        }
      ;

6466

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

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

6496 6497 6498 6499 6500
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6501 6502 6503
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
6504
	| ident
6505
	  { 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
6506 6507 6508 6509 6510


/* flush things */

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

flush_options:
	flush_options ',' flush_option
6523
	| flush_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6524 6525

flush_option:
6526
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6527
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6528
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6529 6530 6531 6532
	| 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
6533 6534
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
6535 6536
	| 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
6537

6538
opt_table_list:
6539 6540
	/* empty */  {;}
	| table_list {;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6541

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

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6551 6552
reset_options:
	reset_options ',' reset_option
6553
	| reset_option;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6554 6555

reset_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6556 6557
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
6558
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6559

6560
purge:
6561 6562 6563 6564
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
6565 6566 6567 6568 6569
	} purge_options
	{}
	;

purge_options:
6570
	master_or_binary LOGS_SYM purge_option
6571
	;
6572 6573

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6588 6589 6590
/* kill threads */

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

6599 6600 6601 6602 6603
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
6604 6605 6606
/* change database */

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

/* import, export of files */

6615 6616 6617
load:   LOAD DATA_SYM 
        {
          LEX *lex=Lex;
6618 6619 6620 6621 6622
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD DATA");
	    YYABORT;
	  }
6623 6624 6625 6626 6627 6628 6629
          lex->fname_start= lex->ptr;
        }
        load_data
        {}
        |
        LOAD TABLE_SYM table_ident FROM MASTER_SYM
        {
6630 6631 6632 6633 6634 6635 6636
	  LEX *lex=Lex;
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD TABLE");
	    YYABORT;
	  }
          lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
6637 6638 6639 6640 6641 6642
          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
6643
	{
6644 6645
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
6646 6647
	  lex->lock_option= $1;
	  lex->local_file=  $2;
6648 6649
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
6650
	  if (!(lex->exchange= new sql_exchange($4.str, 0)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6651
	    YYABORT;
6652 6653 6654 6655 6656
        }
        opt_duplicate INTO
        {
	  LEX *lex=Lex;
	  lex->fname_end= lex->ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6657
	}
6658
        TABLE_SYM table_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6659
        {
6660 6661 6662 6663 6664 6665 6666
          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
6667
        }
6668 6669 6670
        opt_field_term opt_line_term opt_ignore_lines opt_field_or_var_spec
        opt_load_data_set_spec
        {}
6671
        |
6672
	FROM MASTER_SYM
6673 6674
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
6675
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6676 6677 6678

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

6681
load_data_lock:
6682
	/* empty */	{ $$= YYTHD->update_lock_default; }
6683 6684
	| CONCURRENT	{ $$= TL_WRITE_CONCURRENT_INSERT ; }
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
6685 6686


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6687 6688 6689
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
6690
	| IGNORE_SYM	{ Lex->ignore= 1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6691 6692 6693

opt_field_term:
	/* empty */
6694
	| COLUMNS field_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6695 6696 6697

field_term_list:
	field_term_list field_term
6698
	| field_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6699 6700

field_term:
6701 6702
	TERMINATED BY text_string 
          {
6703
            DBUG_ASSERT(Lex->exchange != 0);
6704 6705
            Lex->exchange->field_term= $3;
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6706
	| OPTIONALLY ENCLOSED BY text_string
6707
	  {
6708
            LEX *lex= Lex;
6709
            DBUG_ASSERT(lex->exchange != 0);
6710 6711
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
6712
	  }
6713
        | ENCLOSED BY text_string
6714
          {
6715
            DBUG_ASSERT(Lex->exchange != 0);
6716 6717
            Lex->exchange->enclosed= $3;
          }
6718
        | ESCAPED BY text_string
6719
          {
6720
            DBUG_ASSERT(Lex->exchange != 0);
6721 6722
            Lex->exchange->escaped= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6723 6724 6725

opt_line_term:
	/* empty */
6726
	| LINES line_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6727 6728 6729

line_term_list:
	line_term_list line_term
6730
	| line_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6731 6732

line_term:
6733
        TERMINATED BY text_string
6734
          {
6735
            DBUG_ASSERT(Lex->exchange != 0);
6736 6737
            Lex->exchange->line_term= $3;
          }
6738
        | STARTING BY text_string
6739
          {
6740
            DBUG_ASSERT(Lex->exchange != 0);
6741 6742
            Lex->exchange->line_start= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6743 6744 6745

opt_ignore_lines:
	/* empty */
6746 6747
        | IGNORE_SYM NUM LINES
          {
6748
            DBUG_ASSERT(Lex->exchange != 0);
6749 6750
            Lex->exchange->skip_lines= atol($2.str);
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6751

6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762
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
6763

6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774
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
6775 6776 6777
/* Common definitions */

text_literal:
6778
	TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6779 6780
	{
	  THD *thd= YYTHD;
6781
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6782
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6783
	| NCHAR_STRING
6784
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
6785
	| UNDERSCORE_CHARSET TEXT_STRING
6786
	  { $$ = new Item_string($2.str,$2.length,Lex->charset); }
6787
	| text_literal TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6788 6789
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6790 6791

text_string:
6792
	TEXT_STRING_literal
6793
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6794 6795
	| HEX_NUM
	  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
6796
	    Item *tmp= new Item_hex_string($1.str, $1.length);
6797
	    /*
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
6798
	      it is OK only emulate fix_fields, because we need only
6799 6800 6801
              value of constant
	    */
	    $$= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6802
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6803
	      (String*) 0;
6804
	  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
6805 6806 6807 6808 6809 6810 6811 6812 6813 6814
        | 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;
          }
6815 6816
	;

6817
param_marker:
6818
        '?'
6819
        {
6820 6821 6822
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
          if (thd->command == COM_PREPARE)
6823
          {
6824 6825 6826 6827
            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
6828
	      my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
6829 6830
	      YYABORT;
            }
6831
          }
6832
          else
6833
          {
6834
            yyerror(ER(ER_SYNTAX_ERROR));
6835 6836
            YYABORT;
          }
6837 6838 6839
        }
	;

6840 6841 6842
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
6843 6844
	| '-' NUM_literal
	  {
6845
	    $2->max_length++;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6846
	    $$= $2->neg();
6847
	  }
6848 6849 6850
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6851 6852
literal:
	text_literal	{ $$ =	$1; }
6853
	| NUM_literal	{ $$ = $1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6854
	| NULL_SYM	{ $$ =	new Item_null();
6855
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
6856 6857
	| 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
6858 6859
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
6860
	| UNDERSCORE_CHARSET HEX_NUM
6861
	  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
6862
	    Item *tmp= new Item_hex_string($2.str, $2.length);
6863
	    /*
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6864
	      it is OK only emulate fix_fieds, because we need only
6865 6866 6867
              value of constant
	    */
	    String *str= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6868
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
6869
	      (String*) 0;
6870 6871 6872
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
				Lex->charset);
6873
	  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887
	| 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
6888 6889
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
6890
	| TIMESTAMP text_literal { $$ = $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6891

6892
NUM_literal:
6893 6894
	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); }
6895
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
6896
        | DECIMAL_NUM
6897
	{
6898
           $$= new Item_decimal($1.str, $1.length, YYTHD->charset());
6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
6912
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
6913

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6914
/**********************************************************************
serg@serg.mylan's avatar
serg@serg.mylan committed
6915
** Creating different items.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6916 6917 6918
**********************************************************************/

insert_ident:
6919
	simple_ident_nospvar { $$=$1; }
6920
	| table_wild	 { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6921 6922

table_wild:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6923
	ident '.' '*'
6924 6925
	{
	  $$ = new Item_field(NullS,$1.str,"*");
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6926
	  Lex->current_select->with_wild++;
6927
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6928
	| ident '.' ident '.' '*'
6929 6930
	{
	  $$ = new Item_field((YYTHD->client_capabilities &
serg@serg.mylan's avatar
serg@serg.mylan committed
6931 6932
                             CLIENT_NO_SCHEMA ? NullS : $1.str),
                             $3.str,"*");
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6933
	  Lex->current_select->with_wild++;
6934 6935
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6936 6937

order_ident:
6938
	expr { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6939 6940

simple_ident:
6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965
	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
6966
	ident
6967
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6968
	  SELECT_LEX *sel=Select;
6969
	  $$= (sel->parsing_place != IN_HAVING ||
6970 6971
	       sel->get_in_sum_expr() > 0) ?
              (Item*) new Item_field(NullS,NullS,$1.str) :
6972
	      (Item*) new Item_ref(NullS,NullS,$1.str);
6973
	}
6974
	| simple_ident_q { $$= $1; }
serg@serg.mylan's avatar
serg@serg.mylan committed
6975
	;
6976 6977 6978

simple_ident_q:
	ident '.' ident
6979
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6980
	  THD *thd= YYTHD;
6981
	  LEX *lex= thd->lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
6982

6983 6984 6985 6986 6987 6988
          /*
            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
6989
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") ||
6990 6991
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
6992
            Item_trigger_field *trg_fld;
6993
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
serg@serg.mylan's avatar
serg@serg.mylan committed
6994

6995 6996 6997
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
6998
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
6999 7000
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
7001

7002 7003 7004
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
7005
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
7006 7007
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
7008

7009 7010 7011 7012
            if (!(trg_fld= new Item_trigger_field(new_row ?
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
                                                  $3.str)))
7013
              YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
7014

7015 7016 7017 7018 7019 7020
            /*
              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
7021

7022 7023 7024 7025 7026 7027 7028
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
7029 7030
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
7031 7032 7033 7034 7035 7036 7037
	    }
	    $$= (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
7038
	| '.' 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), $2.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
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field(NullS,$2.str,$4.str) :
7051
              (Item*) new Item_ref(NullS, $2.str, $4.str);
7052
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7053
	| ident '.' ident '.' ident
7054
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7055
	  THD *thd= YYTHD;
7056
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7057
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7058 7059
	  if (sel->no_table_names_allowed)
	  {
7060 7061
	    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
7062
	  }
7063
	  $$= (sel->parsing_place != IN_HAVING ||
7064 7065 7066 7067
	       sel->get_in_sum_expr() > 0) ?
	      (Item*) new Item_field((YYTHD->client_capabilities &
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
7068 7069
	      (Item*) new Item_ref((YYTHD->client_capabilities &
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
7070
                                   $3.str, $5.str);
7071
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7072 7073 7074 7075


field_ident:
	ident			{ $$=$1;}
7076
	| ident '.' ident	{ $$=$3;}	/* Skip schema name in create*/
7077
	| '.' ident		{ $$=$2;}	/* For Delphi */;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7078 7079 7080

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

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

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7089
IDENT_sys:
7090 7091 7092 7093 7094
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
7095 7096
            {
              CHARSET_INFO *cs= system_charset_info;
7097
              int dummy_error;
7098 7099
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
7100
                                                   $1.length, &dummy_error);
7101 7102
              if (wlen < $1.length)
              {
7103 7104
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
7105 7106
                YYABORT;
              }
7107
	      $$= $1;
7108
            }
7109 7110 7111 7112
	    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
7113 7114 7115 7116 7117 7118
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7119 7120
	  if (thd->charset_is_system_charset)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7121
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7122 7123
	    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
7124 7125 7126
	}
	;

7127
TEXT_STRING_literal:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7128 7129 7130
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7131 7132
	  if (thd->charset_is_collation_connection)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7133
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7134 7135
	    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
7136 7137 7138 7139
	}
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
7140
ident:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7141
	IDENT_sys	    { $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7142 7143
	| keyword
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7144 7145 7146
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
7147 7148
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7149 7150

ident_or_text:
serg@serg.mylan's avatar
serg@serg.mylan committed
7151
        ident                   { $$=$1;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7152 7153
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7154 7155 7156 7157

user:
	ident_or_text
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7158 7159
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7160
	    YYABORT;
7161 7162 7163 7164
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7165 7166
	| ident_or_text '@' ident_or_text
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7167 7168
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7169 7170
	      YYABORT;
	    $$->user = $1; $$->host=$3;
7171
	  }
7172
	| CURRENT_USER optional_braces
7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189
	{
          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
7190 7191 7192 7193 7194

/* Keyword that we allow for identifiers */

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

/* Option functions */

set:
	SET opt_option
	{
7447 7448
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
7449
	  mysql_init_select(lex);
7450
	  lex->option_type=OPT_SESSION;
7451
	  lex->var_list.empty();
7452
          lex->one_shot_set= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7453
	}
7454 7455 7456
	option_value_list
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7457 7458 7459

opt_option:
	/* empty */ {}
7460
	| OPTION {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7461 7462

option_value_list:
7463 7464 7465 7466 7467 7468 7469 7470 7471 7472
	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
7473 7474
              and sp_instr_set_trigger instructions share one LEX.
              (Well, it is theoretically possible but adds some extra
7475 7476 7477 7478 7479 7480 7481 7482
               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
7483

7484 7485 7486 7487 7488 7489 7490 7491 7492
            /* 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;
          }
        }
7493
	ext_option_value
7494 7495
        {
          LEX *lex= Lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
7496

7497 7498 7499
          if (lex->sphead)
          {
            sp_head *sp= lex->sphead;
serg@serg.mylan's avatar
serg@serg.mylan committed
7500

7501 7502 7503 7504 7505 7506 7507 7508 7509
	    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
7510

7511 7512 7513
              if (!(i= new sp_instr_stmt(sp->instructions(), lex->spcont,
                                         lex)))
                YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
7514

7515 7516 7517 7518
              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
7519

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

7523 7524 7525 7526 7527 7528 7529 7530 7531
              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);
          }
        };
7532 7533

option_type:
7534 7535 7536 7537 7538 7539 7540 7541 7542
        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; }
7543 7544 7545 7546
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7547
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
7548 7549 7550 7551 7552 7553
	| 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
7554
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
7555 7556 7557
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7558

7559 7560 7561
ext_option_value:
        sys_option_value
        | option_type2 option_value;
7562

7563 7564 7565 7566
sys_option_value:
        option_type internal_variable_name equal set_expr_or_default
        {
          LEX *lex=Lex;
7567

7568 7569 7570 7571
          if ($2.var == &trg_new_row_fake_var)
          {
            /* We are in trigger and assigning value to field of new row */
            Item *it;
7572
            Item_trigger_field *trg_fld;
7573 7574 7575 7576 7577
            sp_instr_set_trigger_field *i;
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
7578
            }
7579 7580 7581 7582 7583 7584 7585 7586
            if (lex->query_tables)
            {
              my_message(ER_SP_SUBSELECT_NYI, ER(ER_SP_SUBSELECT_NYI),
              MYF(0));
              YYABORT;
            }
            if ($4)
              it= $4;
7587
            else
7588 7589 7590 7591
            {
              /* QQ: Shouldn't this be field's default value ? */
              it= new Item_null();
            }
7592

7593 7594 7595 7596 7597
            if (!(trg_fld= new Item_trigger_field(Item_trigger_field::NEW_ROW,
                                                  $2.base_name.str)) ||
                !(i= new sp_instr_set_trigger_field(
                           lex->sphead->instructions(), lex->spcont,
                           trg_fld, it)))
7598
              YYABORT;
7599

7600 7601 7602 7603
            /*
              Let us add this item to list of all Item_trigger_field
              objects in trigger.
            */
7604 7605
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
                                    (byte **)&trg_fld->next_trg_field);
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 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659

            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)));
	}
7660
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
7661 7662
	  {
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7663 7664
	    lex->var_list.push_back(new set_var((enum_var_type) $3, $4.var,
						&$4.base_name, $6));
7665
	  }
7666
	| charset old_or_new_charset_name_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7667
	{
7668
	  THD *thd= YYTHD;
7669
	  LEX *lex= Lex;
7670
	  $2= $2 ? $2: global_system_variables.character_set_client;
7671
	  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
7672
	}
7673
	| NAMES_SYM charset_name_or_default opt_collate
7674
	{
7675
	  LEX *lex= Lex;
7676
	  $2= $2 ? $2 : global_system_variables.character_set_client;
7677 7678
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
7679
	  {
7680 7681
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
7682
	    YYABORT;
7683
	  }
7684
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
7685
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7686
	| PASSWORD equal text_or_password
7687
	  {
7688
	    THD *thd=YYTHD;
7689
	    LEX_USER *user;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7690
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
7691
	      YYABORT;
7692
	    user->host=null_lex_str;
7693
	    user->user.str=thd->priv_user;
7694
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
7695 7696
	  }
	| PASSWORD FOR_SYM user equal text_or_password
7697
	  {
7698 7699
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
7700
	;
7701

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

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7796 7797 7798 7799
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
7800
	    $$= $3.length ? YYTHD->variables.old_passwords ?
7801 7802 7803 7804 7805 7806 7807 7808
	        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;
7809 7810
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7811

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

7813
set_expr_or_default:
7814 7815
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
7816 7817
	| 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
7818
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
7819
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
7820 7821


bk@work.mysql.com's avatar
bk@work.mysql.com committed
7822 7823 7824 7825 7826
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
7827 7828 7829 7830
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
7831
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOCK");
7832 7833 7834
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_LOCK_TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7835
	}
7836 7837
	table_lock_list
	{}
7838
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7839 7840 7841

table_or_tables:
	TABLE_SYM
7842
	| TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7843 7844 7845

table_lock_list:
	table_lock
7846
	| table_lock_list ',' table_lock;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7847 7848 7849

table_lock:
	table_ident opt_table_alias lock_option
7850
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7851
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
7852 7853
	   YYABORT;
	}
7854
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7855 7856 7857

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
7858
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7859
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
7860 7861
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7862 7863

unlock:
7864 7865 7866 7867 7868 7869
	UNLOCK_SYM
	{
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
7870
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "UNLOCK");
7871 7872 7873 7874 7875 7876
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_UNLOCK_TABLES;
	}
	table_or_tables
	{}
7877
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7878 7879


7880
/*
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7881
** Handler: direct access to ISAM functions
7882 7883 7884 7885 7886
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
7887 7888
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_OPEN;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7889
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
7890 7891
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
7892
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
7893
	{
7894 7895
	  LEX *lex= Lex;
	  lex->sql_command = SQLCOM_HA_CLOSE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7896
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
7897 7898
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
7899
	| HANDLER_SYM table_ident_nodb READ_SYM
7900
	{
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
7901 7902 7903
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
7904 7905
	  lex->current_select->select_limit= 1;
	  lex->current_select->offset_limit= 0L;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7906
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
7907
	    YYABORT;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7908
        }
7909
        handler_read_or_scan where_clause opt_limit_clause {}
7910
        ;
7911

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7912
handler_read_or_scan:
7913 7914
	handler_scan_function         { Lex->ident= null_lex_str; }
        | ident handler_rkey_function { Lex->ident= $1; }
7915
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7916 7917 7918

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
7919 7920
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
7921 7922 7923 7924 7925 7926

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;  }
7927 7928
	| handler_rkey_mode
	{
7929 7930
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
7931
	  lex->ha_rkey_mode=$1;
7932
	  if (!(lex->insert_list = new List_item))
7933
	    YYABORT;
7934 7935
	} '(' values ')' { }
        ;
7936 7937

handler_rkey_mode:
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
7938 7939 7940 7941
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
7942 7943
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
7944

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7945 7946 7947
/* GRANT / REVOKE */

revoke:
7948
	REVOKE clear_privileges revoke_command
7949 7950 7951 7952
	{}
        ;

revoke_command:
7953
	grant_privileges ON opt_table grant_ident FROM grant_list
7954
	{
7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982
          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;
7983
        }
7984
	|
7985
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
7986 7987 7988
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
7989
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7990 7991

grant:
7992 7993 7994 7995 7996 7997
	GRANT clear_privileges grant_command
	{}
        ;

grant_command:
	grant_privileges ON opt_table grant_ident TO_SYM grant_list
7998
	require_clause grant_options
7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030
	{
          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
8031

8032 8033 8034 8035
opt_table:
	/* Empty */
	| TABLE_SYM ;
        
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8036
grant_privileges:
8037 8038 8039 8040 8041 8042
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
8043
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8044

8045 8046 8047 8048 8049
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

8050 8051 8052
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8053

8054
object_privilege:
serg@serg.mylan's avatar
serg@serg.mylan committed
8055
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
8056 8057 8058
	| 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 {}
8059 8060
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
8061
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8062 8063 8064
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
8065
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8066 8067 8068 8069
	| RELOAD	{ Lex->grant |= RELOAD_ACL;}
	| SHUTDOWN	{ Lex->grant |= SHUTDOWN_ACL;}
	| PROCESS	{ Lex->grant |= PROCESS_ACL;}
	| FILE_SYM	{ Lex->grant |= FILE_ACL;}
8070 8071 8072 8073 8074
	| 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; }
8075 8076 8077 8078
	| 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; }
8079 8080
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
8081
	| CREATE USER { Lex->grant |= CREATE_USER_ACL; }
8082
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8083

8084

8085 8086
opt_and:
	/* empty */	{}
8087
	| AND_SYM	{}
8088 8089 8090 8091 8092 8093 8094 8095 8096
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
8097 8098 8099 8100
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
8101
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
8102 8103 8104 8105 8106 8107 8108 8109 8110
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
8111
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
8112 8113 8114 8115 8116 8117 8118 8119 8120
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
8121
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
8122 8123 8124 8125 8126
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
8127

8128
grant_ident:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8129 8130
	'*'
	  {
8131
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8132
	    lex->current_select->db= lex->thd->db;
8133
	    if (lex->grant == GLOBAL_ACLS)
8134 8135
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8136
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8137 8138
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
8139
	      YYABORT;
8140
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8141 8142 8143
	  }
	| ident '.' '*'
	  {
8144
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8145
	    lex->current_select->db = $1.str;
8146
	    if (lex->grant == GLOBAL_ACLS)
8147 8148
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8149
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8150 8151
	      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
8152 8153 8154 8155 8156
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
8157
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8158
	    lex->current_select->db = NULL;
8159 8160
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
8161
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8162
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8163 8164
	      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
8165 8166 8167 8168 8169
	      YYABORT;
	    }
	  }
	| table_ident
	  {
8170
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8171
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8172
	      YYABORT;
8173
	    if (lex->grant == GLOBAL_ACLS)
8174
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
8175 8176
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8177 8178 8179


user_list:
8180 8181 8182 8183 8184 8185 8186 8187 8188 8189
	user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;


grant_list:
8190
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
8191
	| grant_list ',' grant_user
8192 8193 8194 8195 8196
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8197 8198 8199 8200 8201 8202 8203 8204


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
8205
             if (YYTHD->variables.old_passwords)
8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222
             {
               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
8223 8224 8225
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
8226
	  { $$= $1; $1->password= $5; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8227
	| user
8228
	  { $$= $1; $1->password= null_lex_str; }
8229
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8230 8231 8232


opt_column_list:
8233 8234 8235 8236 8237
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
8238
	| '(' column_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8239 8240 8241

column_list:
	column_list ',' column_list_id
8242
	| column_list_id;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8243 8244 8245 8246

column_list_id:
	ident
	{
8247
	  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
8248 8249
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
8250
	  LEX *lex=Lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8251 8252
	  while ((point=iter++))
	  {
8253 8254
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8255 8256
		break;
	  }
8257
	  lex->grant_tot_col|= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8258
	  if (point)
8259
	    point->rights |= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8260
	  else
8261
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
8262 8263
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8264

tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
8265 8266

require_clause: /* empty */
8267
        | REQUIRE_SYM require_list
8268 8269 8270
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
8271
        | REQUIRE_SYM SSL_SYM
8272 8273 8274
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
8275
        | REQUIRE_SYM X509_SYM
8276 8277 8278 8279 8280 8281 8282
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
8283
          ;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
8284

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8285
grant_options:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8286
	/* empty */ {}
8287
	| WITH grant_option_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8288

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8289 8290
grant_option_list:
	grant_option_list grant_option {}
8291 8292
	| grant_option {}
        ;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8293 8294 8295

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
serg@serg.mylan's avatar
serg@serg.mylan committed
8296
        | MAX_QUERIES_PER_HOUR ulong_num
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8297
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
8298 8299 8300
	  LEX *lex=Lex;
	  lex->mqh.questions=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
8301
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
8302
        | MAX_UPDATES_PER_HOUR ulong_num
8303
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
8304 8305 8306
	  LEX *lex=Lex;
	  lex->mqh.updates=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
8307
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
8308
        | MAX_CONNECTIONS_PER_HOUR ulong_num
8309
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
8310 8311 8312
	  LEX *lex=Lex;
	  lex->mqh.conn_per_hour= $2;
	  lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
8313
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
8314
        | MAX_USER_CONNECTIONS_SYM ulong_num
8315
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
8316 8317 8318
	  LEX *lex=Lex;
          lex->mqh.user_conn= $2;
          lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
8319 8320
	}
        ;
8321

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8322
begin:
serg@serg.mylan's avatar
serg@serg.mylan committed
8323 8324 8325 8326 8327 8328 8329
	BEGIN_SYM  
        {
	  LEX *lex=Lex;
          lex->sql_command = SQLCOM_BEGIN;
          lex->start_transaction_opt= 0;
        }
        opt_work {}
8330
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8331 8332 8333

opt_work:
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
8334
	| WORK_SYM  {}
8335
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8336

8337
opt_chain:
serg@serg.mylan's avatar
serg@serg.mylan committed
8338
	/* empty */ { $$= (YYTHD->variables.completion_type == 1); }
8339 8340 8341 8342 8343
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
serg@serg.mylan's avatar
serg@serg.mylan committed
8344
	/* empty */ { $$= (YYTHD->variables.completion_type == 2); }
8345 8346 8347 8348 8349 8350 8351 8352 8353
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8354
commit:
serg@serg.mylan's avatar
serg@serg.mylan committed
8355
	COMMIT_SYM opt_work opt_chain opt_release
8356
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
8357 8358 8359 8360
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_COMMIT;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
8361 8362
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8363 8364

rollback:
serg@serg.mylan's avatar
serg@serg.mylan committed
8365
	ROLLBACK_SYM opt_work opt_chain opt_release
8366
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
8367 8368 8369 8370
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
8371
	}
8372 8373
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
8374
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
8375 8376 8377
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK_TO_SAVEPOINT;
	  lex->ident= $5;
8378 8379 8380
	}
	;

8381 8382 8383
savepoint:
	SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
8384 8385 8386
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SAVEPOINT;
	  lex->ident= $2;
8387 8388 8389 8390 8391 8392
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
8393 8394 8395
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RELEASE_SAVEPOINT;
	  lex->ident= $3;
8396 8397
	}
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
8398
  
8399
/*
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8400
   UNIONS : glue selects together
8401 8402 8403
*/


8404
union_clause:
8405
	/* empty */ {}
8406 8407
	| union_list
	;
8408 8409

union_list:
8410
	UNION_SYM union_option
8411 8412 8413 8414 8415
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
8416
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
8417
	    YYABORT;
8418
	  }
8419
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
8420
	  {
8421
            yyerror(ER(ER_SYNTAX_ERROR));
8422 8423
	    YYABORT;
	  }
8424
	  if (mysql_new_select(lex, 0))
8425
	    YYABORT;
8426
          mysql_init_select(lex);
8427
	  lex->current_select->linkage=UNION_TYPE;
8428 8429 8430
          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
8431
	}
8432
	select_init {}
8433
	;
8434 8435

union_opt:
8436 8437 8438
	/* Empty */ { $$= 0; }
	| union_list { $$= 1; }
	| union_order_or_limit { $$= 1; }
8439
	;
8440

8441
union_order_or_limit:
8442
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8443
	    THD *thd= YYTHD;
8444
	    LEX *lex= thd->lex;
8445
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8446
	    SELECT_LEX *sel= lex->current_select;
8447
	    SELECT_LEX_UNIT *unit= sel->master_unit();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8448
	    SELECT_LEX *fake= unit->fake_select_lex;
8449 8450 8451 8452 8453 8454
	    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
8455
	    thd->where= "global ORDER clause";
8456
	  }
8457
	order_or_limit
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8458 8459
          {
	    THD *thd= YYTHD;
8460
	    thd->lex->current_select->no_table_names_allowed= 0;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8461 8462
	    thd->where= "";
          }
8463 8464 8465
	;

order_or_limit:
8466 8467
	order_clause opt_limit_clause_init
	| limit_clause
8468
	;
8469 8470

union_option:
8471 8472 8473 8474
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
8475

8476 8477
singlerow_subselect:
	subselect_start singlerow_subselect_init
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8478 8479 8480 8481
	subselect_end
	{
	  $$= $2;
	};
8482

8483
singlerow_subselect_init:
8484
	select_init2
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8485
	{
8486 8487
	  $$= new Item_singlerow_subselect(Lex->current_select->
					   master_unit()->first_select());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8488
	};
8489 8490

exists_subselect:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8491 8492 8493 8494 8495
	subselect_start exists_subselect_init
	subselect_end
	{
	  $$= $2;
	};
8496 8497

exists_subselect_init:
8498
	select_init2
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8499
	{
8500 8501
	  $$= new Item_exists_subselect(Lex->current_select->master_unit()->
					first_select());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8502
	};
8503

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8504 8505 8506 8507 8508 8509 8510 8511
in_subselect:
  subselect_start in_subselect_init
  subselect_end
  {
    $$= $2;
  };

in_subselect_init:
8512
  select_init2
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8513
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8514
    $$= Lex->current_select->master_unit()->first_select();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8515 8516
  };

8517
subselect_start:
8518
	'(' SELECT_SYM
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8519
	{
8520
	  LEX *lex=Lex;
8521
	  if (((int)lex->sql_command >= (int)SQLCOM_HA_OPEN &&
8522 8523
	       lex->sql_command <= (int)SQLCOM_HA_READ) ||
	       lex->sql_command == (int)SQLCOM_KILL)
8524 8525
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
8526 8527
	    YYABORT;
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8528 8529 8530
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
8531 8532

subselect_end:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8533 8534 8535
	')'
	{
	  LEX *lex=Lex;
8536
	  lex->current_select = lex->current_select->return_after_parsing();
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
8537
	};
serg@serg.mylan's avatar
serg@serg.mylan committed
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 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582
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; }
        ;

8583 8584
xa: XA_SYM begin_or_start xid opt_join_or_resume
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
8585
        Lex->sql_command = SQLCOM_XA_START;
8586 8587 8588
      }
    | XA_SYM END xid opt_suspend_or_migrate
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
8589
        Lex->sql_command = SQLCOM_XA_END;
8590 8591 8592
      }
    | XA_SYM PREPARE_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
8593
        Lex->sql_command = SQLCOM_XA_PREPARE;
8594 8595 8596
      }
    | XA_SYM COMMIT_SYM xid opt_one_phase
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
8597
        Lex->sql_command = SQLCOM_XA_COMMIT;
8598 8599 8600
      }
    | XA_SYM ROLLBACK_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
8601
        Lex->sql_command = SQLCOM_XA_ROLLBACK;
8602 8603 8604
      }
    | XA_SYM RECOVER_SYM
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
8605
        Lex->sql_command = SQLCOM_XA_RECOVER;
8606 8607 8608
      }
    ;

serg@serg.mylan's avatar
serg@serg.mylan committed
8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630
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());
     }
     ;
8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653

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