sql_yacc.yy 289 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"
41
#include "event_data_objects.h"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
42
#include <myisam.h>
43
#include <myisammrg.h>
44

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

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

monty@mishka.local's avatar
monty@mishka.local committed
49
#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
50

51
#define YYERROR_UNLESS(A)               \
serg@serg.mylan's avatar
serg@serg.mylan committed
52
  if (!(A))                             \
53 54 55 56 57
  {					\
    yyerror(ER(ER_SYNTAX_ERROR));	\
    YYABORT;				\
  }

58 59
/* 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
60
{
61 62
  return new Item_func_if(create_func_ifnull(A,
	new Item_int((char *) (v2 ? "TRUE" : "FALSE"), v2, 1)),
63
	new Item_int((char *) (v1 ? "TRUE" : "FALSE"), v1, 1),
64
	new Item_int((char *) (v1 ? "FALSE" : "TRUE"),!v1, 1));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
65 66
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#ifndef DBUG_OFF
#define YYDEBUG 1
#else
#define YYDEBUG 0
#endif

#ifndef DBUG_OFF
void turn_parser_debug_on()
{
  /*
     MYSQLdebug is in sql/sql_yacc.cc, in bison generated code.
     Turning this option on is **VERY** verbose, and should be
     used when investigating a syntax error problem only.

     The syntax to run with bison traces is as follows :
     - Starting a server manually :
       mysqld --debug="d,parser_debug" ...
     - Running a test :
       mysql-test-run.pl --mysqld="--debug=d,parser_debug" ...

     The result will be in the process stderr (var/log/master.err)
   */

  extern int yydebug;
  yydebug= 1;
}
#endif

bk@work.mysql.com's avatar
bk@work.mysql.com committed
95 96 97 98
%}
%union {
  int  num;
  ulong ulong_num;
99
  ulonglong ulonglong_number;
100
  longlong longlong_number;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
101 102 103 104 105 106
  LEX_STRING lex_str;
  LEX_STRING *lex_str_ptr;
  LEX_SYMBOL symbol;
  Table_ident *table;
  char *simple_string;
  Item *item;
107
  Item_num *item_num;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
108 109
  List<Item> *item_list;
  List<String> *string_list;
110 111 112 113 114
  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
115
  struct sys_var_with_base variable;
116
  enum enum_var_type var_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
117
  Key::Keytype key_type;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
118
  enum ha_key_alg key_alg;
119
  handlerton *db_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
120
  enum row_type row_type;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
121
  enum ha_rkey_function ha_rkey_mode;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
122
  enum enum_tx_isolation tx_isolation;
123
  enum Cast_target cast_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
124
  enum Item_udftype udf_type;
125
  CHARSET_INFO *charset;
126
  thr_lock_type lock_type;
127
  interval_type interval, interval_time_st;
128
  timestamp_type date_time_type;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
129
  st_select_lex *select_lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
130
  chooser_compare_func_creator boolfunc2creator;
131 132 133 134
  struct sp_cond_type *spcondtype;
  struct { int vars, conds, hndlrs, curs; } spblock;
  sp_name *spname;
  struct st_lex *lex;
andrey@lmy004's avatar
andrey@lmy004 committed
135
  sp_head *sphead;
136
  struct p_elem_val *p_elem_value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
137 138 139
}

%{
140
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
141 142 143 144
%}

%pure_parser					/* We have threads */

serg@serg.mylan's avatar
serg@serg.mylan committed
145
%token  END_OF_INPUT
bk@work.mysql.com's avatar
bk@work.mysql.com committed
146

147
%token  ABORT_SYM
148
%token  ACCESSIBLE_SYM
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
%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
166
%token  AT_SYM
167
%token  ATAN
brian@grrr.local's avatar
brian@grrr.local committed
168
%token  AUTHORS_SYM
169
%token  AUTO_INC
170
%token  AUTOEXTEND_SIZE_SYM
171 172 173 174 175 176 177 178 179
%token  AVG_ROW_LENGTH
%token  AVG_SYM
%token  BACKUP_SYM
%token  BEFORE_SYM
%token  BEGIN_SYM
%token  BENCHMARK_SYM
%token  BIGINT
%token  BINARY
%token  BINLOG_SYM
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
180
%token  BIN_NUM
181 182 183 184 185 186 187 188 189 190 191 192 193 194
%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
195
%token  CASCADED
196
%token  CAST_SYM
serg@serg.mylan's avatar
serg@serg.mylan committed
197
%token  CHAIN_SYM
198 199 200 201 202 203 204 205 206 207
%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
pem@mysql.com's avatar
pem@mysql.com committed
208
%token  CODE_SYM
209 210 211 212 213 214 215
%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
216
%token  COMPACT_SYM
217
%token  COMPLETION_SYM
218 219 220 221
%token  COMPRESSED_SYM
%token  CONCAT
%token  CONCAT_WS
%token  CONCURRENT
222
%token  CONDITION_SYM
223 224 225
%token  CONNECTION_SYM
%token  CONSISTENT_SYM
%token  CONSTRAINT
226 227
%token  CONTAINS_SYM
%token  CONTINUE_SYM
228
%token  CONTRIBUTORS_SYM
229 230 231 232 233 234 235
%token  CONVERT_SYM
%token  CONVERT_TZ_SYM
%token  COUNT_SYM
%token  CREATE
%token  CROSS
%token  CUBE_SYM
%token  CURDATE
236
%token  CURRENT_USER
237 238 239 240
%token  CURSOR_SYM
%token  CURTIME
%token  DATABASE
%token  DATABASES
241
%token  DATAFILE_SYM
242 243 244 245 246 247 248 249 250 251 252
%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
253
%token  DECIMAL_NUM
254
%token  DECIMAL_SYM
255
%token  DECLARE_SYM
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
%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
271
%token  DISK_SYM
272 273 274 275 276 277 278
%token  DISTINCT
%token  DIV_SYM
%token  DOUBLE_SYM
%token  DO_SYM
%token  DROP
%token  DUAL_SYM
%token  DUMPFILE
279
%token  DUPLICATE_SYM
280
%token  DYNAMIC_SYM
281
%token  EACH_SYM
282 283 284 285 286 287 288
%token  ELSEIF_SYM
%token  ELT_FUNC
%token  ENABLE_SYM
%token  ENCLOSED
%token  ENCODE_SYM
%token  ENCRYPT
%token  END
289
%token  ENDS_SYM
290 291 292 293 294 295 296 297
%token  ENGINES_SYM
%token  ENGINE_SYM
%token  ENUM
%token  EQ
%token  EQUAL_SYM
%token  ERRORS
%token  ESCAPED
%token  ESCAPE_SYM
298
%token  EVENT_SYM
299
%token  EVENTS_SYM
300
%token  EVERY_SYM
301 302
%token  EXECUTE_SYM
%token  EXISTS
303
%token  EXIT_SYM
304 305 306
%token  EXPANSION_SYM
%token  EXPORT_SET
%token  EXTENDED_SYM
307
%token  EXTENT_SIZE_SYM
308 309 310
%token  EXTRACT_SYM
%token  FALSE_SYM
%token  FAST_SYM
311
%token  FETCH_SYM
312 313 314 315 316 317 318 319 320 321 322
%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
323
%token  FOUND_SYM
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
%token  FRAC_SECOND_SYM
%token  FROM
%token  FROM_UNIXTIME
%token  FULL
%token  FULLTEXT_SYM
%token  FUNCTION_SYM
%token  FUNC_ARG0
%token  FUNC_ARG1
%token  FUNC_ARG2
%token  FUNC_ARG3
%token  GE
%token  GEOMCOLLFROMTEXT
%token  GEOMETRYCOLLECTION
%token  GEOMETRY_SYM
%token  GEOMFROMTEXT
%token  GEOMFROMWKB
%token  GET_FORMAT
%token  GLOBAL_SYM
%token  GRANT
%token  GRANTS
%token  GREATEST_SYM
%token  GROUP
%token  GROUP_CONCAT_SYM
%token  GROUP_UNIQUE_USERS
%token  GT_SYM
%token  HANDLER_SYM
%token  HASH_SYM
%token  HAVING
%token  HELP_SYM
%token  HEX_NUM
%token  HIGH_PRIORITY
%token  HOSTS_SYM
%token  HOUR_MICROSECOND_SYM
%token  HOUR_MINUTE_SYM
%token  HOUR_SECOND_SYM
%token  HOUR_SYM
%token  IDENT
%token  IDENTIFIED_SYM
%token  IDENT_QUOTED
%token  IF
%token  IGNORE_SYM
%token  IMPORT
%token  INDEXES
%token  INDEX_SYM
%token  INFILE
369
%token  INITIAL_SIZE_SYM
370 371
%token  INNER_SYM
%token  INNOBASE_SYM
372
%token  INOUT_SYM
373 374 375
%token  INSENSITIVE_SYM
%token  INSERT
%token  INSERT_METHOD
376
%token  INSTALL_SYM
377 378 379
%token  INTERVAL_SYM
%token  INTO
%token  INT_SYM
380
%token  INVOKER_SYM
381 382 383 384 385 386 387 388
%token  IN_SYM
%token  IS
%token  ISOLATION
%token  ISSUER_SYM
%token  ITERATE_SYM
%token  JOIN_SYM
%token  KEYS
%token  KEY_SYM
389
%token  KEY_BLOCK_SIZE
390
%token  KILL_SYM
391
%token  LANGUAGE_SYM
392 393 394 395 396 397 398 399
%token  LAST_INSERT_ID
%token  LAST_SYM
%token  LE
%token  LEADING
%token  LEAST_SYM
%token  LEAVES
%token  LEAVE_SYM
%token  LEFT
400
%token  LESS_SYM
401 402 403 404
%token  LEVEL_SYM
%token  LEX_HOSTNAME
%token  LIKE
%token  LIMIT
405
%token  LINEAR_SYM
406 407 408
%token  LINEFROMTEXT
%token  LINES
%token  LINESTRING
409
%token  LIST_SYM
410 411 412
%token  LOAD
%token  LOCAL_SYM
%token  LOCATE
413
%token  LOCATOR_SYM
414 415
%token  LOCKS_SYM
%token  LOCK_SYM
416
%token  LOGFILE_SYM
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
%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
447
%token  MAX_SIZE_SYM
448 449
%token  MAX_SYM
%token  MAX_UPDATES_PER_HOUR
serg@serg.mylan's avatar
serg@serg.mylan committed
450
%token  MAX_USER_CONNECTIONS_SYM
451
%token  MAX_VALUE_SYM
452 453 454 455
%token  MEDIUMBLOB
%token  MEDIUMINT
%token  MEDIUMTEXT
%token  MEDIUM_SYM
456
%token  MEMORY_SYM
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
%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
476
%token  MUTEX_SYM
477 478 479 480 481 482
%token  NAMES_SYM
%token  NAME_SYM
%token  NATIONAL_SYM
%token  NATURAL
%token  NCHAR_STRING
%token  NCHAR_SYM
483
%token  NDBCLUSTER_SYM
484 485 486
%token  NE
%token  NEW_SYM
%token  NEXT_SYM
487
%token  NODEGROUP_SYM
488 489 490 491 492
%token  NONE_SYM
%token  NOT2_SYM
%token  NOT_SYM
%token  NOW_SYM
%token  NO_SYM
493
%token  NO_WAIT_SYM
494 495 496 497
%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
498
%token  NVARCHAR_SYM
499
%token  OFFSET_SYM
500
%token  OJ_SYM
501 502
%token  OLD_PASSWORD
%token  ON
503
%token  ONE_SHOT_SYM
504 505 506 507 508 509 510 511 512 513 514
%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
515
%token  OUT_SYM
516
%token  PACK_KEYS_SYM
517
%token  PARSER_SYM
518
%token  PARTIAL
519
%token  PARTITION_SYM
520
%token  PARTITIONING_SYM
521
%token  PARTITIONS_SYM
522
%token  PASSWORD
523
%token  PARAM_MARKER
524
%token  PHASE_SYM
525
%token  PLUGIN_SYM
526
%token  PLUGINS_SYM
527 528 529 530 531 532 533
%token  POINTFROMTEXT
%token  POINT_SYM
%token  POLYFROMTEXT
%token  POLYGON
%token  POSITION_SYM
%token  PRECISION
%token  PREPARE_SYM
534
%token  PRESERVE_SYM
535 536 537 538 539 540 541 542 543 544 545
%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  RAND
546
%token  RANGE_SYM
547
%token  READS_SYM
548
%token  READ_ONLY_SYM
549
%token  READ_SYM
550
%token  READ_WRITE_SYM
551
%token  REAL
552
%token  REBUILD_SYM
553
%token  RECOVER_SYM
554 555
%token  REDO_BUFFER_SIZE_SYM
%token  REDOFILE_SYM
serg@serg.mylan's avatar
serg@serg.mylan committed
556
%token  REDUNDANT_SYM
557 558 559 560 561
%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
562
%token  RELEASE_SYM
563
%token  RELOAD
564
%token  REMOVE_SYM
565
%token  RENAME
566
%token  REORGANIZE_SYM
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
%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
585
%token  ROUTINE_SYM
586 587 588 589 590 591
%token  ROWS_SYM
%token  ROW_COUNT_SYM
%token  ROW_FORMAT_SYM
%token  ROW_SYM
%token  RTREE_SYM
%token  SAVEPOINT_SYM
592
%token  SCHEDULE_SYM
593 594
%token  SECOND_MICROSECOND_SYM
%token  SECOND_SYM
595
%token  SECURITY_SYM
596 597
%token  SELECT_SYM
%token  SENSITIVE_SYM
598
%token  SEPARATOR_SYM
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
%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
614
%token  SONAME_SYM
615 616
%token  SOUNDS_SYM
%token  SPATIAL_SYM
617 618 619 620
%token  SPECIFIC_SYM
%token  SQLEXCEPTION_SYM
%token  SQLSTATE_SYM
%token  SQLWARNING_SYM
621 622 623 624 625 626 627 628
%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
629
%token  SSL_SYM
630 631
%token  STARTING
%token  START_SYM
632
%token  STARTS_SYM
633 634
%token  STATUS_SYM
%token  STD_SYM
635
%token  STDDEV_SAMP_SYM
636 637 638 639 640 641
%token  STOP_SYM
%token  STORAGE_SYM
%token  STRAIGHT_JOIN
%token  STRING_SYM
%token  SUBDATE_SYM
%token  SUBJECT_SYM
642 643
%token  SUBPARTITION_SYM
%token  SUBPARTITIONS_SYM
644 645 646 647 648
%token  SUBSTRING
%token  SUBSTRING_INDEX
%token  SUM_SYM
%token  SUPER_SYM
%token  SUSPEND_SYM
649
%token  SYSDATE
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
%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
665
%token  THAN_SYM
666 667 668
%token  TO_SYM
%token  TRAILING
%token  TRANSACTION_SYM
669
%token  TRIGGER_SYM
670
%token  TRIGGERS_SYM
671 672 673
%token  TRIM
%token  TRUE_SYM
%token  TRUNCATE_SYM
674
%token  TYPES_SYM
675 676 677 678 679
%token  TYPE_SYM
%token  UDF_RETURNS_SYM
%token  ULONGLONG_NUM
%token  UNCOMMITTED_SYM
%token  UNDEFINED_SYM
680 681
%token  UNDO_BUFFER_SIZE_SYM
%token  UNDOFILE_SYM
682
%token  UNDERSCORE_CHARSET
683
%token  UNDO_SYM
684
%token  UNICODE_SYM
685
%token  UNINSTALL_SYM
686 687 688 689 690 691 692 693 694
%token  UNION_SYM
%token  UNIQUE_SYM
%token  UNIQUE_USERS
%token  UNIX_TIMESTAMP
%token  UNKNOWN_SYM
%token  UNLOCK_SYM
%token  UNSIGNED
%token  UNTIL_SYM
%token  UPDATE_SYM
695
%token  UPGRADE_SYM
696 697 698 699 700 701 702 703
%token  USAGE
%token  USER
%token  USE_FRM
%token  USE_SYM
%token  USING
%token  UTC_DATE_SYM
%token  UTC_TIMESTAMP_SYM
%token  UTC_TIME_SYM
704
%token  VAR_SAMP_SYM
705 706 707 708 709 710 711 712
%token  VALUES
%token  VALUE_SYM
%token  VARBINARY
%token  VARCHAR
%token  VARIABLES
%token  VARIANCE_SYM
%token  VARYING
%token  VIEW_SYM
713
%token  WAIT_SYM
714 715 716 717
%token  WARNINGS
%token  WEEK_SYM
%token  WHEN_SYM
%token  WHERE
718
%token  WHILE_SYM
719 720 721 722 723 724 725 726 727 728
%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
729

730

731
%left   JOIN_SYM INNER_SYM STRAIGHT_JOIN CROSS LEFT RIGHT
timour@mysql.com's avatar
timour@mysql.com committed
732 733
/* A dummy token to force the priority of table_ref production in a join. */
%left   TABLE_REF_PRIORITY
bk@work.mysql.com's avatar
bk@work.mysql.com committed
734
%left   SET_VAR
735 736
%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
737 738 739 740 741 742
%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	'-' '+'
743
%left	'*' '/' '%' DIV_SYM MOD_SYM
744
%left   '^'
monty@mysql.com's avatar
monty@mysql.com committed
745
%left	NEG '~'
746
%right	NOT_SYM NOT2_SYM
747
%right	BINARY COLLATE_SYM
748

bk@work.mysql.com's avatar
bk@work.mysql.com committed
749
%type <lex_str>
750
        IDENT IDENT_QUOTED TEXT_STRING DECIMAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM
751
	LEX_HOSTNAME ULONGLONG_NUM field_ident select_alias ident ident_or_text
752
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
753
	NCHAR_STRING opt_component key_cache_name
754
        sp_opt_label BIN_NUM label_ident TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
755 756

%type <lex_str_ptr>
757
	opt_table_alias
bk@work.mysql.com's avatar
bk@work.mysql.com committed
758 759

%type <table>
760
	table_ident table_ident_nodb references xid
bk@work.mysql.com's avatar
bk@work.mysql.com committed
761 762

%type <simple_string>
763
	remember_name remember_end opt_ident opt_db text_or_password
764
	opt_constraint constraint ident_or_empty
bk@work.mysql.com's avatar
bk@work.mysql.com committed
765 766

%type <string>
767
	text_string opt_gconcat_separator
bk@work.mysql.com's avatar
bk@work.mysql.com committed
768 769

%type <num>
770
	type int_type real_type order_dir lock_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
771
	udf_type if_exists opt_local opt_table_options table_options
772 773
        table_option opt_if_not_exists opt_no_write_to_binlog
        delete_option opt_temporary all_or_any opt_distinct
774
        opt_ignore_leaves fulltext_options spatial_type union_option
serg@serg.mylan's avatar
serg@serg.mylan committed
775
        start_transaction_opts opt_chain opt_release
776
        union_opt select_derived_init option_type2
777 778 779
        opt_natural_language_mode opt_query_expansion
        opt_ev_status opt_ev_on_completion ev_on_completion opt_ev_comment
        ev_alter_on_schedule_completion opt_ev_rename_to opt_ev_sql_stmt
bk@work.mysql.com's avatar
bk@work.mysql.com committed
780 781

%type <ulong_num>
782
	ulong_num real_ulong_num merge_insert_types
bk@work.mysql.com's avatar
bk@work.mysql.com committed
783

784
%type <ulonglong_number>
785
	ulonglong_num real_ulonglong_num size_number
bk@work.mysql.com's avatar
bk@work.mysql.com committed
786

787
%type <p_elem_value>
788 789
        part_bit_expr

790 791 792
%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
793
%type <item>
794
	literal text_literal insert_ident order_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
795
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
796 797 798
	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
799
	expr_or_default set_expr_or_default interval_expr
800
	param_marker geometry_function
801
	signed_literal now_or_signed_literal opt_escape
802 803
	sp_opt_default
	simple_ident_nospvar simple_ident_q
804
        field_or_var limit_option
805
        part_func_expr
806 807 808

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

%type <item_list>
811 812
	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
813

814 815 816
%type <var_type>
        option_type opt_var_type opt_var_ident_type

bk@work.mysql.com's avatar
bk@work.mysql.com committed
817
%type <key_type>
818
	key_type opt_unique_or_fulltext constraint_key_type
bk@work.mysql.com's avatar
bk@work.mysql.com committed
819

820
%type <key_alg>
821
	btree_or_rtree
822

bk@work.mysql.com's avatar
bk@work.mysql.com committed
823
%type <string_list>
824
	key_usage_list using_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
825 826 827 828 829

%type <key_part>
	key_part

%type <table_list>
830
	join_table_list  join_table
831
        table_factor table_ref
832
        select_derived derived_table_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
833

834
%type <date_time_type> date_time_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
835 836
%type <interval> interval

837 838
%type <interval_time_st> interval_time_st

839
%type <db_type> storage_engines
bk@work.mysql.com's avatar
bk@work.mysql.com committed
840 841 842

%type <row_type> row_types

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

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

847
%type <cast_type> cast_type
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
848

bk@work.mysql.com's avatar
bk@work.mysql.com committed
849 850
%type <udf_type> udf_func_type

851
%type <symbol> FUNC_ARG0 FUNC_ARG1 FUNC_ARG2 FUNC_ARG3 keyword keyword_sp
bk@work.mysql.com's avatar
bk@work.mysql.com committed
852

853
%type <lex_user> user grant_user
bk@work.mysql.com's avatar
bk@work.mysql.com committed
854

855
%type <charset>
856
	opt_collate
857 858
	charset_name
	charset_name_or_default
859 860
	old_or_new_charset_name
	old_or_new_charset_name_or_default
861 862
	collation_name
	collation_name_or_default
863

864 865
%type <variable> internal_variable_name

866
%type <select_lex> subselect subselect_init
867
	get_select_lex
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
868

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
871
%type <NONE>
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
872
	query verb_clause create change select do drop insert replace insert2
873
	insert_values update delete truncate rename
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
874
	show describe load alter optimize keycache preload flush
875
	reset purge begin commit rollback savepoint release
876
	slave master_def master_defs master_file_def slave_until_opts
877
	repair restore backup analyze check start checksum
878
	field_list field_list_item field_spec kill column_def key_def
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
879
	keycache_list assign_to_keycache preload_list preload_keys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
880
	select_item_list select_item values_list no_braces
881
	opt_limit_clause delete_limit_clause fields opt_values values
bk@work.mysql.com's avatar
bk@work.mysql.com committed
882
	procedure_list procedure_list2 procedure_item
883
	when_list2 expr_list2 udf_expr_list3 handler
bk@work.mysql.com's avatar
bk@work.mysql.com committed
884 885
	opt_precision opt_ignore opt_column opt_restrict
	grant revoke set lock unlock string_list field_options field_option
886
	field_opt_list opt_binary table_lock_list table_lock
887
	ref_list opt_on_delete opt_on_delete_list opt_on_delete_item use
888
	opt_delete_options opt_delete_option varchar nchar nvarchar
889
	opt_outer table_list table_name opt_option opt_place
bk@work.mysql.com's avatar
bk@work.mysql.com committed
890
	opt_attribute opt_attribute_list attribute column_list column_list_id
891
	opt_column_list grant_privileges grant_ident grant_list grant_option
892
	object_privilege object_privilege_list user_list rename_list
893
	clear_privileges flush_options flush_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
894 895
	equal optional_braces opt_key_definition key_usage_list2
	opt_mi_check_type opt_to mi_check_types normal_join
896
	db_to_db table_to_table_list table_to_table opt_table_list opt_as
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
897
	handler_rkey_function handler_read_or_scan
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
898
	single_multi table_wild_list table_wild_one opt_wild
899
	union_clause union_list
900
	precision subselect_start opt_and charset
901
	subselect_end select_var_list select_var_list_init help opt_len
902
	opt_extended_describe
903
        prepare prepare_src execute deallocate
904
	statement sp_suid
905
	sp_c_chistics sp_a_chistics sp_chistic sp_c_chistic xa
906
        load_data opt_field_or_var_spec fields_or_vars opt_load_data_set_spec
907
        definer view_replace_or_algorithm view_replace view_algorithm_opt
908 909
        view_algorithm view_or_trigger_or_sp_or_event
        view_or_trigger_or_sp_or_event_tail
910 911
        view_suid view_tail view_list_opt view_list view_select
        view_check_option trigger_tail sp_tail
912
        install uninstall partition_entry binlog_base64_event
913
	init_key_options key_options key_opts key_opt key_using_alg
914
END_OF_INPUT
bk@work.mysql.com's avatar
bk@work.mysql.com committed
915

916
%type <NONE> call sp_proc_stmts sp_proc_stmts1 sp_proc_stmt
917 918 919
%type <NONE> sp_proc_stmt_statement sp_proc_stmt_return
%type <NONE> sp_proc_stmt_if sp_proc_stmt_case_simple sp_proc_stmt_case
%type <NONE> sp_labeled_control sp_proc_stmt_unlabeled sp_proc_stmt_leave
pem@mysql.com's avatar
pem@mysql.com committed
920
%type <NONE> sp_proc_stmt_iterate
921 922
%type <NONE> sp_proc_stmt_open sp_proc_stmt_fetch sp_proc_stmt_close

923 924 925 926 927 928
%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
929 930
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
931
	',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_SYM BETWEEN_SYM CASE_SYM
932
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM OR2_SYM AND_AND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
933 934 935 936 937
%%


query:
	END_OF_INPUT
938
	{
939
	   THD *thd= YYTHD;
940
	   if (!thd->bootstrap &&
941
	      (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
942
	   {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
943
	     my_message(ER_EMPTY_QUERY, ER(ER_EMPTY_QUERY), MYF(0));
944
	     YYABORT;
945
	   }
946 947
	   else
	   {
948
	     thd->lex->sql_command= SQLCOM_EMPTY_QUERY;
949
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
950
	}
951
	| verb_clause END_OF_INPUT {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
952 953

verb_clause:
954 955 956 957 958 959
	  statement
	| begin
	;

/* Verb clauses, except begin */
statement:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
960 961
	  alter
	| analyze
962
	| backup
963
	| binlog_base64_event
964
	| call
bk@work.mysql.com's avatar
bk@work.mysql.com committed
965 966
	| change
	| check
967
	| checksum
bk@work.mysql.com's avatar
bk@work.mysql.com committed
968 969
	| commit
	| create
970
        | deallocate
bk@work.mysql.com's avatar
bk@work.mysql.com committed
971 972
	| delete
	| describe
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
973
	| do
bk@work.mysql.com's avatar
bk@work.mysql.com committed
974
	| drop
975
        | execute
serg@serg.mylan's avatar
serg@serg.mylan committed
976
	| flush
bk@work.mysql.com's avatar
bk@work.mysql.com committed
977
	| grant
serg@serg.mylan's avatar
serg@serg.mylan committed
978 979
	| handler
	| help
bk@work.mysql.com's avatar
bk@work.mysql.com committed
980
	| insert
981
        | install
serg@serg.mylan's avatar
serg@serg.mylan committed
982
	| kill
bk@work.mysql.com's avatar
bk@work.mysql.com committed
983 984 985
	| load
	| lock
	| optimize
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
986
        | keycache
987
        | partition_entry
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
988
	| preload
989
        | prepare
990
	| purge
991
	| release
992
	| rename
serg@serg.mylan's avatar
serg@serg.mylan committed
993
	| repair
bk@work.mysql.com's avatar
bk@work.mysql.com committed
994
	| replace
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
995
	| reset
996
	| restore
bk@work.mysql.com's avatar
bk@work.mysql.com committed
997 998
	| revoke
	| rollback
999
	| savepoint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1000 1001
	| select
	| set
serg@serg.mylan's avatar
serg@serg.mylan committed
1002
	| show
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1003
	| slave
1004
	| start
1005
	| truncate
1006
        | uninstall
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1007 1008
	| unlock
	| update
1009
	| use
1010
	| xa
serg@serg.mylan's avatar
serg@serg.mylan committed
1011
        ;
1012

sergefp@mysql.com's avatar
sergefp@mysql.com committed
1013
deallocate:
1014
        deallocate_or_drop PREPARE_SYM ident
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1015 1016
        {
          THD *thd=YYTHD;
1017
          LEX *lex= thd->lex;
1018
          if (lex->stmt_prepare_mode)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1019 1020 1021 1022
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
1023
          lex->sql_command= SQLCOM_DEALLOCATE_PREPARE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1024 1025 1026
          lex->prepared_stmt_name= $3;
        };

1027 1028 1029 1030 1031 1032
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


sergefp@mysql.com's avatar
sergefp@mysql.com committed
1033
prepare:
1034
        PREPARE_SYM ident FROM prepare_src
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1035 1036
        {
          THD *thd=YYTHD;
1037
          LEX *lex= thd->lex;
1038
          if (lex->stmt_prepare_mode)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1039 1040 1041 1042
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
1043
          lex->sql_command= SQLCOM_PREPARE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1044 1045 1046
          lex->prepared_stmt_name= $2;
        };

1047 1048 1049 1050 1051 1052
prepare_src:
        TEXT_STRING_sys
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $1;
1053
          lex->prepared_stmt_code_is_varref= FALSE;
1054 1055 1056 1057 1058 1059
        }
        | '@' ident_or_text
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $2;
1060
          lex->prepared_stmt_code_is_varref= TRUE;
1061
        };
1062

sergefp@mysql.com's avatar
sergefp@mysql.com committed
1063 1064 1065 1066
execute:
        EXECUTE_SYM ident
        {
          THD *thd=YYTHD;
1067
          LEX *lex= thd->lex;
1068
          if (lex->stmt_prepare_mode)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1069 1070 1071 1072
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
1073
          lex->sql_command= SQLCOM_EXECUTE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
          lex->prepared_stmt_name= $2;
        }
        execute_using
        {}
        ;

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
1086 1087
        execute_var_list ',' execute_var_ident
        | execute_var_ident
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1088 1089 1090 1091 1092 1093 1094 1095
        ;

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;
1096
        }
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1097 1098
        ;

1099 1100
/* help */

1101
help:
1102 1103 1104 1105 1106 1107 1108 1109 1110
       HELP_SYM
       {
         if (Lex->sphead)
         {
           my_error(ER_SP_BADSTATEMENT, MYF(0), "HELP");
           YYABORT;
         }
       }
       ident_or_text
1111
       {
1112 1113
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
1114
	  lex->help_arg= $3.str;
1115
       };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1116 1117 1118 1119

/* change master */

change:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1120
       CHANGE MASTER_SYM TO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1121 1122 1123
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
1124
	  bzero((char*) &lex->mi, sizeof(lex->mi));
1125 1126 1127 1128
        }
       master_defs
	{}
       ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1129 1130 1131

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

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
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
1150
       MASTER_PORT_SYM EQ ulong_num
1151 1152 1153 1154
       {
	 Lex->mi.port = $3;
       }
       |
serg@serg.mylan's avatar
serg@serg.mylan committed
1155
       MASTER_CONNECT_RETRY_SYM EQ ulong_num
1156 1157 1158
       {
	 Lex->mi.connect_retry = $3;
       }
serg@serg.mylan's avatar
serg@serg.mylan committed
1159
       | MASTER_SSL_SYM EQ ulong_num
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
         {
           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
1213
       | RELAY_LOG_POS_SYM EQ ulong_num
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
         {
           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;
1241
	  lex->create_info.db_type= lex->thd->variables.table_type;
1242
	  lex->create_info.default_table_charset= NULL;
mikael@zim.(none)'s avatar
mikael@zim.(none) committed
1243 1244
	  lex->name= 0;
         lex->like_name= 0;
1245 1246 1247
	}
	create2
	  { Lex->current_select= &Lex->select_lex; }
1248 1249
	| CREATE opt_unique_or_fulltext INDEX_SYM ident key_alg ON
	  table_ident
1250 1251 1252
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_CREATE_INDEX;
1253 1254
	    if (!lex->current_select->add_table_to_list(lex->thd, $7,
							NULL,
1255 1256 1257 1258 1259 1260 1261
							TL_OPTION_UPDATING))
	      YYABORT;
	    lex->create_list.empty();
	    lex->key_list.empty();
	    lex->col_list.empty();
	    lex->change=NullS;
	  }
1262
	   '(' key_list ')' key_options
1263 1264
	  {
	    LEX *lex=Lex;
1265
	    if ($2 != Key::FULLTEXT && lex->key_create_info.parser_name.str)
1266 1267 1268 1269
	    {
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
	    }
1270
	    lex->key_list.push_back(new Key($2, $4.str, &lex->key_create_info, 0,
1271
					   lex->col_list));
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	    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;
	  }
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
	| CREATE
	  {
            Lex->create_view_mode= VIEW_CREATE_NEW;
            Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED;
            Lex->create_view_suid= TRUE;
	  }
	  view_or_trigger_or_sp_or_event
	  {}
	| CREATE USER clear_privileges grant_list
	  {
	    Lex->sql_command = SQLCOM_CREATE_USER;
          }
	| CREATE LOGFILE_SYM GROUP logfile_group_info 
          {
1300
            Lex->alter_tablespace_info->ts_cmd_type= CREATE_LOGFILE_GROUP;
1301 1302 1303
          }
        | CREATE TABLESPACE tablespace_info
          {
1304
            Lex->alter_tablespace_info->ts_cmd_type= CREATE_TABLESPACE;
1305 1306 1307 1308 1309 1310
          }
	;


event_tail:
          EVENT_SYM opt_if_not_exists sp_name
1311 1312
          /*
             BE CAREFUL when you add a new rule to update the block where
1313 1314
             YYTHD->client_capabilities is set back to original value
          */
1315
          {
1316
            Lex->create_info.options= $2;
1317

1318
            if (!(Lex->event_parse_data= Event_parse_data::new_instance(YYTHD)))
andrey@lmy004's avatar
andrey@lmy004 committed
1319
              YYABORT;
1320
            Lex->event_parse_data->identifier= $3;
1321

1322 1323 1324 1325 1326
            /*
              We have to turn of CLIENT_MULTI_QUERIES while parsing a
              stored procedure, otherwise yylex will chop it into pieces
              at each ';'.
            */
andrey@lmy004's avatar
andrey@lmy004 committed
1327 1328 1329
            $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
            YYTHD->client_capabilities &= (~CLIENT_MULTI_QUERIES);

1330
            Lex->sql_command= SQLCOM_CREATE_EVENT;
1331 1332
            /* We need that for disallowing subqueries */
            Lex->expr_allows_subselect= FALSE;
1333 1334
          }
          ON SCHEDULE_SYM ev_schedule_time
1335 1336 1337
          opt_ev_on_completion
          opt_ev_status
          opt_ev_comment
1338 1339
          DO_SYM ev_sql_stmt
          {
andrey@lmy004's avatar
andrey@lmy004 committed
1340
            /*
1341
              Restore flag if it was cleared above
1342 1343 1344 1345
              $1 - EVENT_SYM
              $2 - opt_if_not_exists
              $3 - sp_name
              $4 - the block above
andrey@lmy004's avatar
andrey@lmy004 committed
1346
            */
1347
            YYTHD->client_capabilities |= $<ulong_num>4;
1348

1349 1350 1351 1352
            /*
              sql_command is set here because some rules in ev_sql_stmt
              can overwrite it
            */
andrey@lmy004's avatar
andrey@lmy004 committed
1353
            Lex->sql_command= SQLCOM_CREATE_EVENT;
1354
            Lex->expr_allows_subselect= TRUE;
1355
          }
1356 1357 1358 1359


ev_schedule_time: EVERY_SYM expr interval
	  {
1360 1361
            Lex->event_parse_data->item_expression= $2;
            Lex->event_parse_data->interval= $3;
1362 1363 1364 1365 1366
          }
          ev_starts
          ev_ends
        | AT_SYM expr
          {
1367
            Lex->event_parse_data->item_execute_at= $2;
1368 1369
          }
      ;
1370 1371

opt_ev_status: /* empty */ { $$= 0; }
1372
        | ENABLE_SYM
1373
          {
1374
            Lex->event_parse_data->status= Event_parse_data::ENABLED;
1375
            $$= 1;
1376
          }
1377
        | DISABLE_SYM
1378
          {
1379
            Lex->event_parse_data->status= Event_parse_data::DISABLED;
1380
            $$= 1;
1381 1382 1383 1384
          }
      ;

ev_starts: /* empty */
1385
          {
1386
            Lex->event_parse_data->item_starts= new Item_func_now_local();
1387
          }
1388 1389
        | STARTS_SYM expr
          {
1390
            Lex->event_parse_data->item_starts= $2;
1391 1392 1393 1394 1395 1396
          }
      ;

ev_ends: /* empty */
        | ENDS_SYM expr
          {
1397
            Lex->event_parse_data->item_ends= $2;
1398 1399 1400
          }
      ;

1401
opt_ev_on_completion: /* empty */ { $$= 0; }
1402 1403 1404
        | ev_on_completion
      ;

1405
ev_on_completion:
1406
          ON COMPLETION_SYM PRESERVE_SYM
1407
          {
1408 1409
            Lex->event_parse_data->on_completion=
                                  Event_parse_data::ON_COMPLETION_PRESERVE;
1410
            $$= 1;
1411 1412 1413
          }
        | ON COMPLETION_SYM NOT_SYM PRESERVE_SYM
          {
1414 1415
            Lex->event_parse_data->on_completion=
                                  Event_parse_data::ON_COMPLETION_DROP;
1416
            $$= 1;
1417 1418
          }
      ;
1419

1420
opt_ev_comment: /* empty */ { $$= 0; }
1421 1422
        | COMMENT_SYM TEXT_STRING_sys
          {
1423
            Lex->comment= Lex->event_parse_data->comment= $2;
1424 1425 1426 1427 1428 1429
          }
      ;

ev_sql_stmt:
          {
            LEX *lex= Lex;
1430

1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
            /*
              This stops the following :
              - CREATE EVENT ... DO CREATE EVENT ...;
              - ALTER  EVENT ... DO CREATE EVENT ...;
              - CREATE EVENT ... DO ALTER EVENT DO ....;
              - CREATE PROCEDURE ... BEGIN CREATE EVENT ... END|
              This allows:
              - CREATE EVENT ... DO DROP EVENT yyy;
              - CREATE EVENT ... DO ALTER EVENT yyy;
                (the nested ALTER EVENT can have anything but DO clause)
              - ALTER  EVENT ... DO ALTER EVENT yyy;
                (the nested ALTER EVENT can have anything but DO clause)
              - ALTER  EVENT ... DO DROP EVENT yyy;
              - CREATE PROCEDURE ... BEGIN ALTER EVENT ... END|
                (the nested ALTER EVENT can have anything but DO clause)
              - CREATE PROCEDURE ... BEGIN DROP EVENT ... END|
            */
            if (lex->sphead)
andrey@lmy004's avatar
andrey@lmy004 committed
1449
            {
1450 1451 1452 1453 1454 1455
              my_error(ER_EVENT_RECURSIVITY_FORBIDDEN, MYF(0));
              YYABORT;
            }
              
            if (!(lex->sphead= new sp_head()))
              YYABORT;
1456

1457 1458
            lex->sphead->reset_thd_mem_root(YYTHD);
            lex->sphead->init(lex);
1459
            lex->sphead->init_sp_name(YYTHD, Lex->event_parse_data->identifier);
1460

1461
            lex->sphead->m_type= TYPE_ENUM_PROCEDURE;
1462

1463 1464
            bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
            lex->sphead->m_chistics= &lex->sp_chistics;
1465

1466
            lex->sphead->m_body_begin= lex->ptr;
1467 1468
            
            Lex->event_parse_data->body_begin= lex->ptr;
andrey@lmy004's avatar
andrey@lmy004 committed
1469

1470 1471 1472 1473
          }
          ev_sql_stmt_inner
          {
            LEX *lex=Lex;
1474

1475
            /* return back to the original memory root ASAP */
1476
            lex->sphead->init_strings(YYTHD, lex);
1477
            lex->sphead->restore_thd_mem_root(YYTHD);
1478

1479
            lex->sp_chistics.suid= SP_IS_SUID;  //always the definer!
1480

1481
            Lex->event_parse_data->init_body(YYTHD);
1482 1483
          }
      ;
1484

1485 1486 1487 1488 1489 1490
ev_sql_stmt_inner:
          sp_proc_stmt_statement
        | sp_proc_stmt_return
        | sp_proc_stmt_if
        | sp_proc_stmt_case_simple
        | sp_proc_stmt_case
1491
        | sp_labeled_control
1492 1493 1494 1495 1496 1497 1498
        | sp_proc_stmt_unlabeled
        | sp_proc_stmt_leave
        | sp_proc_stmt_iterate
        | sp_proc_stmt_open
        | sp_proc_stmt_fetch
        | sp_proc_stmt_close
      ;
1499

1500

1501 1502 1503 1504 1505 1506 1507
clear_privileges:
        /* Nothing */
        {
          LEX *lex=Lex;
          lex->users_list.empty();
          lex->columns.empty();
          lex->grant= lex->grant_tot_col= 0;
1508
	  lex->all_privileges= 0;
1509 1510 1511 1512 1513 1514 1515
          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));
        }
        ;

1516
sp_name:
1517
	  ident '.' ident
1518
	  {
1519 1520 1521 1522 1523
            if (!$1.str || check_db_name($1.str))
            {
	      my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
	      YYABORT;
	    }
1524
	    if (check_routine_name($3))
1525 1526 1527 1528
            {
	      my_error(ER_SP_WRONG_NAME, MYF(0), $3.str);
	      YYABORT;
	    }
1529 1530 1531
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
1532
	| ident
1533
	  {
1534 1535
            THD *thd= YYTHD;
            LEX_STRING db;
1536
	    if (check_routine_name($1))
1537 1538 1539 1540
            {
	      my_error(ER_SP_WRONG_NAME, MYF(0), $1.str);
	      YYABORT;
	    }
1541 1542 1543 1544 1545
            if (thd->copy_db_to(&db.str, &db.length))
              YYABORT;
	    $$= new sp_name(db, $1);
            if ($$)
	      $$->init_qname(YYTHD);
1546 1547 1548 1549
	  }
	;

create_function_tail:
1550
	  RETURNS_SYM udf_type SONAME_SYM TEXT_STRING_sys
1551 1552
	  {
	    LEX *lex=Lex;
1553 1554 1555
            if (lex->definer != NULL)
            {
              /*
1556 1557 1558 1559
                DEFINER is a concept meaningful when interpreting SQL code.
                UDF functions are compiled.
                Using DEFINER with UDF has therefore no semantic,
                and is considered a parsing error.
1560 1561 1562 1563
              */
	      my_error(ER_WRONG_USAGE, MYF(0), "SONAME", "DEFINER");
              YYABORT;
            }
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
	    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;

1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
            /* 
              First check if AGGREGATE was used, in that case it's a
              syntax error.
            */
            if (lex->udf.type == UDFTYPE_AGGREGATE)
            {
              my_error(ER_SP_NO_AGGREGATE, MYF(0));
              YYABORT;
            }

1584 1585
	    if (lex->sphead)
	    {
1586
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1587 1588 1589 1590 1591 1592
	      YYABORT;
	    }
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
	    sp->init(lex);
1593
            sp->init_sp_name(YYTHD, lex->spname);
1594 1595 1596 1597

	    sp->m_type= TYPE_ENUM_FUNCTION;
	    lex->sphead= sp;
	    /*
1598 1599 1600 1601
	      We have to turn off CLIENT_MULTI_QUERIES while parsing a
	      stored procedure, otherwise yylex will chop it into pieces
	      at each ';'.
	    */
andrey@lmy004's avatar
andrey@lmy004 committed
1602
            $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
	    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;
1615 1616 1617 1618
	    lex->charset= NULL;
	    lex->length= lex->dec= NULL;
	    lex->interval_list.empty();
	    lex->type= 0;
1619 1620 1621 1622 1623
	  }
	  type
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
            /*
              This was disabled in 5.1.12. See bug #20701
              When collation support in SP is implemented, then this test
              should be removed.
            */
            if (($8 == FIELD_TYPE_STRING || $8 == MYSQL_TYPE_VARCHAR)
                && (lex->type & BINCMP_FLAG))
            {
              my_error(ER_NOT_SUPPORTED_YET, MYF(0), "return value collation");
              YYABORT;
            }
1635

1636 1637 1638 1639
            if (sp->fill_field_definition(YYTHD, lex,
                                          (enum enum_field_types) $8,
                                          &sp->m_return_field_def))
              YYABORT;
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654

	    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;

1655 1656 1657
            if (sp->is_not_allowed_in_function("function"))
              YYABORT;

1658
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
1659
	    sp->init_strings(YYTHD, lex);
1660 1661 1662 1663 1664
            if (!(sp->m_flags & sp_head::HAS_RETURN))
            {
              my_error(ER_SP_NORETURN, MYF(0), sp->m_qname.str);
              YYABORT;
            }
1665
	    /* Restore flag if it was cleared above */
andrey@lmy004's avatar
andrey@lmy004 committed
1666
	    YYTHD->client_capabilities |= $<ulong_num>2;
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
	    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; }
1703
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
	;

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();
1725
	    sp_add_used_routine(lex, YYTHD, $2, TYPE_ENUM_PROCEDURE);
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
	  }
          '(' 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
	;

1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
sp_init_param:
	  /* Empty */
	  {
	    LEX *lex= Lex;

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

1778
sp_fdparam:
1779
	  ident sp_init_param type
1780 1781 1782 1783
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1784
	    if (spc->find_variable(&$1, TRUE))
1785
	    {
1786
	      my_error(ER_SP_DUP_PARAM, MYF(0), $1.str);
1787 1788
	      YYABORT;
	    }
1789 1790 1791
            sp_variable_t *spvar= spc->push_variable(&$1,
                                                     (enum enum_field_types)$3,
                                                     sp_param_in);
1792 1793 1794

            if (lex->sphead->fill_field_definition(YYTHD, lex,
                                                   (enum enum_field_types) $3,
1795
                                                   &spvar->field_def))
1796 1797 1798
            {
              YYABORT;
            }
1799 1800
            spvar->field_def.field_name= spvar->name.str;
            spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
1816
	  sp_opt_inout sp_init_param ident type
1817 1818 1819 1820
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1821
	    if (spc->find_variable(&$3, TRUE))
1822
	    {
1823
	      my_error(ER_SP_DUP_PARAM, MYF(0), $3.str);
1824 1825
	      YYABORT;
	    }
1826 1827 1828
            sp_variable_t *spvar= spc->push_variable(&$3,
                                                     (enum enum_field_types)$4,
                                                     (sp_param_mode_t)$1);
1829 1830 1831

            if (lex->sphead->fill_field_definition(YYTHD, lex,
                                                   (enum enum_field_types) $4,
1832
                                                   &spvar->field_def))
1833 1834 1835
            {
              YYABORT;
            }
1836 1837
            spvar->field_def.field_name= spvar->name.str;
            spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849
	  }
	;

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 */ {}
1850
	| sp_proc_stmts  sp_proc_stmt ';'
1851 1852 1853 1854
	;

sp_proc_stmts1:
	  sp_proc_stmt ';' {}
1855
	| sp_proc_stmts1  sp_proc_stmt ';'
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
	;

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
1871 1872
	      my_message(ER_SP_VARCOND_AFTER_CURSHNDLR,
                         ER(ER_SP_VARCOND_AFTER_CURSHNDLR), MYF(0));
1873 1874 1875 1876
	      YYABORT;
	    }
	    if ($2.curs && $1.hndlrs)
	    { /* Cursor following handler */
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1877 1878
	      my_message(ER_SP_CURSOR_AFTER_HANDLER,
                         ER(ER_SP_CURSOR_AFTER_HANDLER), MYF(0));
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
	      YYABORT;
	    }
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
	    $$.curs= $1.curs + $2.curs;
	  }
	;

sp_decl:
1889
          DECLARE_SYM sp_decl_idents
1890
          {
1891
            LEX *lex= Lex;
1892

1893 1894 1895
            lex->sphead->reset_lex(YYTHD);
            lex->spcont->declare_var_boundary($2);
          }
1896
          type
1897
          sp_opt_default
1898 1899
          {
            LEX *lex= Lex;
1900
            sp_pcontext *pctx= lex->spcont;
1901
            uint num_vars= pctx->context_var_count();
1902 1903 1904 1905 1906
            enum enum_field_types var_type= (enum enum_field_types) $4;
            Item *dflt_value_item= $5;
            create_field *create_field_op;
            
            if (!dflt_value_item)
1907
            {
1908 1909 1910 1911 1912 1913
              dflt_value_item= new Item_null();
              /* QQ Set to the var_type with null_value? */
            }
            
            for (uint i = num_vars-$2 ; i < num_vars ; i++)
            {
1914 1915
              uint var_idx= pctx->var_context2runtime(i);
              sp_variable_t *spvar= pctx->find_variable(var_idx);
1916
            
1917
              if (!spvar)
1918 1919
                YYABORT;
            
1920 1921
              spvar->type= var_type;
              spvar->dflt= dflt_value_item;
1922 1923
            
              if (lex->sphead->fill_field_definition(YYTHD, lex, var_type,
1924
                                                     &spvar->field_def))
1925 1926 1927 1928
              {
                YYABORT;
              }
            
1929 1930
              spvar->field_def.field_name= spvar->name.str;
              spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1931 1932 1933 1934 1935 1936 1937
            
              /* The last instruction is responsible for freeing LEX. */

              lex->sphead->add_instr(
                new sp_instr_set(lex->sphead->instructions(), pctx, var_idx,
                                 dflt_value_item, var_type, lex,
                                 (i == num_vars - 1)));
1938
            }
1939 1940

            pctx->declare_var_boundary(0);
1941
            lex->sphead->restore_lex(YYTHD);
1942

1943 1944 1945
            $$.vars= $2;
            $$.conds= $$.hndlrs= $$.curs= 0;
          }
1946 1947 1948 1949 1950 1951 1952
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1953
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
	      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,
1967
	                              ctx->current_var_count());
1968 1969 1970

	    sp->add_instr(i);
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
1971
	    sp->m_flags|= sp_head::IN_HANDLER;
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
	  }
	  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,
1984
	                              ctx->current_var_count());
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
	      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);
1995
	    sp->m_flags&= ~sp_head::IN_HANDLER;
1996 1997
	    $$.vars= $$.conds= $$.curs= 0;
	    $$.hndlrs= $6;
1998
	    ctx->add_handlers($6);
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
	  }
	| 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))
	    {
2010
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
2011 2012 2013
	      delete $5;
	      YYABORT;
	    }
pem@mysql.com's avatar
pem@mysql.com committed
2014
            i= new sp_instr_cpush(sp->instructions(), ctx, $5,
2015
                                  ctx->current_cursor_count());
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
	    sp->add_instr(i);
	    ctx->push_cursor(&$2);
	    $$.vars= $$.conds= $$.hndlrs= 0;
	    $$.curs= 1;
	  }
	;

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

2027 2028 2029 2030 2031 2032
	    /*
	      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.
	    */
2033 2034 2035 2036 2037
	  }
	  statement
	  {
	    LEX *lex= Lex;

2038 2039
	    if (lex->sql_command != SQLCOM_SELECT &&
	       !(sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND))
2040
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2041 2042
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
2043 2044 2045 2046
	      YYABORT;
	    }
	    if (lex->result)
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2047 2048
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
	      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;
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078
	    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();
2079

2080 2081 2082 2083
	      i->add_condition($1);
	      ctx->push_handler($1);
	      $$= 1;
	    }
2084 2085 2086 2087 2088
	  }
	| sp_hcond_list ',' sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
	    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();
2100

2101 2102 2103 2104
	      i->add_condition($3);
	      ctx->push_handler($3);
	      $$= $1 + 1;
	    }
2105 2106 2107 2108
	  }
	;

sp_cond:
serg@serg.mylan's avatar
serg@serg.mylan committed
2109
	  ulong_num
2110 2111 2112 2113 2114 2115 2116
	  {			/* 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 */
2117 2118 2119 2120 2121
	    if (!sp_cond_check(&$3))
	    {
	      my_error(ER_SP_BAD_SQLSTATE, MYF(0), $3.str);
	      YYABORT;
	    }
2122 2123
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
2124 2125
	    memcpy($$->sqlstate, $3.str, 5);
	    $$->sqlstate[5]= '\0';
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
	  }
	;

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
2144
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
2145 2146 2147 2148 2149 2150 2151 2152
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
2153
	| not FOUND_SYM		/* SQLSTATEs 02??? */
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
	  {
	    $$= (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
	  {
2168 2169
            /* NOTE: field definition is filled in sp_decl section. */

2170 2171 2172
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

2173
	    if (spc->find_variable(&$1, TRUE))
2174
	    {
2175
	      my_error(ER_SP_DUP_VAR, MYF(0), $1.str);
2176 2177
	      YYABORT;
	    }
2178
	    spc->push_variable(&$1, (enum_field_types)0, sp_param_in);
2179 2180 2181 2182
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
2183 2184
            /* NOTE: field definition is filled in sp_decl section. */

2185 2186 2187
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

2188
	    if (spc->find_variable(&$3, TRUE))
2189
	    {
2190
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
2191 2192
	      YYABORT;
	    }
2193
	    spc->push_variable(&$3, (enum_field_types)0, sp_param_in);
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
	    $$= $1 + 1;
	  }
	;

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

sp_proc_stmt:
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
	  sp_proc_stmt_statement
        | sp_proc_stmt_return
	| sp_proc_stmt_if
	| sp_proc_stmt_case_simple
        | sp_proc_stmt_case
	| sp_labeled_control
	| sp_proc_stmt_unlabeled
	| sp_proc_stmt_leave
	| sp_proc_stmt_iterate
	| sp_proc_stmt_open
	| sp_proc_stmt_fetch
        | sp_proc_stmt_close
        ;

sp_proc_stmt_if:
2219 2220 2221
        IF { Lex->sphead->new_cont_backpatch(NULL); }
        sp_if END IF
        { Lex->sphead->do_cont_backpatch(); }
2222 2223 2224
        ;
        
sp_proc_stmt_statement:
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
	  {
	    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;

2236
            sp->m_flags|= sp_get_flags_for_command(lex);
2237 2238
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
2239
	      my_error(ER_SP_BADSTATEMENT, MYF(0), "USE");
2240 2241
	      YYABORT;
	    }
2242 2243 2244 2245
	    /*
              Don't add an instruction for SET statements, since all
              instructions for them were already added during processing
              of "set" rule.
2246
	    */
2247 2248 2249
            DBUG_ASSERT(lex->sql_command != SQLCOM_SET_OPTION ||
                        lex->var_list.is_empty());
            if (lex->sql_command != SQLCOM_SET_OPTION)
2250
	    {
2251 2252
              sp_instr_stmt *i=new sp_instr_stmt(sp->instructions(),
                                                 lex->spcont, lex);
2253

2254 2255 2256 2257 2258 2259
              /*
                Extract the query statement from the tokenizer.  The
                end is either lex->ptr, if there was no lookahead,
                lex->tok_end otherwise.
              */
              if (yychar == YYEMPTY)
2260 2261 2262 2263 2264 2265 2266
                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);
2267 2268 2269
            }
	    sp->restore_lex(YYTHD);
          }
2270 2271 2272 2273
        ;

sp_proc_stmt_return:
        RETURN_SYM 
2274 2275
          { Lex->sphead->reset_lex(YYTHD); }
          expr
2276 2277
	  {
	    LEX *lex= Lex;
2278
	    sp_head *sp= lex->sphead;
2279

2280
	    if (sp->m_type != TYPE_ENUM_FUNCTION)
2281
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2282
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
2283 2284 2285 2286 2287 2288
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_freturn *i;

2289 2290
	      i= new sp_instr_freturn(sp->instructions(), lex->spcont, $3,
                                      sp->m_return_field_def.sql_type, lex);
2291
	      sp->add_instr(i);
2292
	      sp->m_flags|= sp_head::HAS_RETURN;
2293
	    }
2294
	    sp->restore_lex(YYTHD);
2295
	  }
2296 2297 2298 2299
        ;

sp_proc_stmt_case_simple:
	CASE_SYM WHEN_SYM
2300
	  {
2301
	    Lex->sphead->m_flags&= ~sp_head::IN_SIMPLE_CASE;
2302
            Lex->sphead->new_cont_backpatch(NULL);
2303
	  }
2304
          sp_case END CASE_SYM { Lex->sphead->do_cont_backpatch(); }
2305 2306 2307 2308
        ;
        
sp_proc_stmt_case:
          CASE_SYM
2309 2310 2311 2312
          {
            Lex->sphead->reset_lex(YYTHD);
            Lex->sphead->new_cont_backpatch(NULL);
          }
2313
          expr WHEN_SYM
2314 2315
	  {
	    LEX *lex= Lex;
2316 2317 2318
	    sp_head *sp= lex->sphead;
	    sp_pcontext *parsing_ctx= lex->spcont;
	    int case_expr_id= parsing_ctx->register_case_expr();
2319
            sp_instr_set_case_expr *i;
2320 2321 2322
	    
	    if (parsing_ctx->push_case_expr_id(case_expr_id))
              YYABORT;
2323 2324 2325 2326 2327 2328 2329 2330

            i= new sp_instr_set_case_expr(sp->instructions(),
                                          parsing_ctx,
                                          case_expr_id,
                                          $3,
                                          lex);
            sp->add_cont_backpatch(i);
            sp->add_instr(i);
2331 2332
	    sp->m_flags|= sp_head::IN_SIMPLE_CASE;
	    sp->restore_lex(YYTHD);
2333 2334 2335
	  }
	  sp_case END CASE_SYM
	  {
2336
	    Lex->spcont->pop_case_expr_id();
2337
            Lex->sphead->do_cont_backpatch();
2338
	  }
2339 2340 2341 2342
        ;

sp_proc_stmt_unlabeled:
	  { /* Unlabeled controls get a secret label. */
2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
	    LEX *lex= Lex;

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

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
2353 2354 2355 2356
        ;

sp_proc_stmt_leave:
	  LEAVE_SYM label_ident
2357 2358 2359 2360 2361 2362 2363 2364
	  {
	    LEX *lex= Lex;
	    sp_head *sp = lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab)
	    {
2365
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
2366 2367 2368 2369 2370
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_jump *i;
2371 2372 2373
	      uint ip= sp->instructions();
	      uint n;

2374
	      n= ctx->diff_handlers(lab->ctx, TRUE);  /* Exclusive the dest. */
2375 2376
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
2377
	      n= ctx->diff_cursors(lab->ctx, TRUE);  /* Exclusive the dest. */
2378 2379
	      if (n)
	        sp->add_instr(new sp_instr_cpop(ip++, ctx, n));
2380 2381 2382 2383 2384
	      i= new sp_instr_jump(ip, ctx);
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
	    }
	  }
2385 2386 2387 2388
        ;

sp_proc_stmt_iterate:
	  ITERATE_SYM label_ident
2389 2390 2391 2392 2393 2394 2395 2396
	  {
	    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)
	    {
2397
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "ITERATE", $2.str);
2398 2399 2400 2401 2402 2403 2404 2405
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_jump *i;
	      uint ip= sp->instructions();
	      uint n;

2406
	      n= ctx->diff_handlers(lab->ctx, FALSE);  /* Inclusive the dest. */
2407 2408
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
2409
	      n= ctx->diff_cursors(lab->ctx, FALSE);  /* Inclusive the dest. */
2410 2411 2412 2413 2414 2415
	      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);
	    }
	  }
2416 2417 2418 2419
        ;

sp_proc_stmt_open:
	  OPEN_SYM ident
2420 2421 2422 2423 2424 2425 2426 2427
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_copen *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2428
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2429 2430 2431 2432 2433
	      YYABORT;
	    }
	    i= new sp_instr_copen(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
2434 2435 2436 2437
        ;

sp_proc_stmt_fetch:
	  FETCH_SYM sp_opt_fetch_noise ident INTO
2438 2439 2440 2441 2442 2443 2444 2445
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cfetch *i;

	    if (! lex->spcont->find_cursor(&$3, &offset))
	    {
2446
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $3.str);
2447 2448 2449 2450 2451 2452 2453
	      YYABORT;
	    }
	    i= new sp_instr_cfetch(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
	  sp_fetch_list
	  { }
2454 2455 2456 2457
        ;

sp_proc_stmt_close:
	  CLOSE_SYM ident
2458 2459 2460 2461 2462 2463 2464 2465
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cclose *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2466
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485
	      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;
2486
	    sp_variable_t *spv;
2487

2488
	    if (!spc || !(spv = spc->find_variable(&$1)))
2489
	    {
2490
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

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

2509
	    if (!spc || !(spv = spc->find_variable(&$3)))
2510
	    {
2511
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $3.str);
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	    }
	  }
	;

sp_if:
2525 2526
          { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2527 2528 2529 2530 2531
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= sp->instructions();
2532 2533
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, ctx,
                                                               $2, lex);
2534 2535

	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
2536
            sp->add_cont_backpatch(i);
2537
            sp->add_instr(i);
2538
            sp->restore_lex(YYTHD);
2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
	  }
	  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:
2566 2567
	  { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2568 2569 2570 2571 2572 2573 2574
	  {
            LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

2575
	    if (! (sp->m_flags & sp_head::IN_SIMPLE_CASE))
2576
	      i= new sp_instr_jump_if_not(ip, ctx, $2, lex);
2577 2578 2579
	    else
	    { /* Simple case: <caseval> = <whenval> */

2580 2581 2582 2583 2584
	      Item_case_expr *var;
              Item *expr;

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

2585
#ifndef DBUG_OFF
2586
              if (var)
2587
                var->m_sp= sp;
2588
#endif
2589 2590

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

2592
	      i= new sp_instr_jump_if_not(ip, ctx, expr, lex);
2593 2594
	    }
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
2595
            sp->add_cont_backpatch(i);
2596
            sp->add_instr(i);
2597
            sp->restore_lex(YYTHD);
2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616
	  }
	  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
2617

2618 2619 2620 2621 2622 2623 2624
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
2625

2626 2627 2628 2629 2630
	    sp->add_instr(i);
	  }
	| ELSE sp_proc_stmts1 {}
	| WHEN_SYM sp_case {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2631

2632
sp_labeled_control:
2633
	  label_ident ':'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2634
	  {
2635 2636 2637 2638 2639 2640
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);

	    if (lab)
	    {
2641
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $1.str);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2642
	      YYABORT;
2643 2644 2645 2646 2647 2648 2649
	    }
	    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
2650
	  }
2651
	  sp_unlabeled_control sp_opt_label
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2652
	  {
2653
	    LEX *lex= Lex;
2654

2655 2656 2657 2658 2659 2660 2661
	    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)
	      {
2662
	        my_error(ER_SP_LABEL_MISMATCH, MYF(0), $5.str);
2663 2664 2665 2666
	        YYABORT;
	      }
	    }
	    lex->sphead->backpatch(lex->spcont->pop_label());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2667
	  }
2668 2669 2670
	;

sp_opt_label:
2671
        /* Empty  */    { $$= null_lex_str; }
2672
        | label_ident   { $$= $1; }
2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688
	;

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
2689
	  {
2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704
	    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
2705
	  {
2706 2707 2708 2709 2710 2711
	    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
2712
	  }
2713 2714 2715
        | WHILE_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr DO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2716
	  {
2717 2718 2719 2720
	    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,
2721
							       $3, lex);
2722 2723 2724

	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
2725
            sp->new_cont_backpatch(i);
2726
            sp->add_instr(i);
2727
            sp->restore_lex(YYTHD);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2728
	  }
2729
	  sp_proc_stmts1 END WHILE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2730
	  {
2731 2732 2733 2734 2735 2736
	    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);
2737
            lex->sphead->do_cont_backpatch();
2738
	  }
2739 2740 2741
        | REPEAT_SYM sp_proc_stmts1 UNTIL_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr END REPEAT_SYM
2742 2743 2744 2745 2746
	  {
	    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,
2747 2748
                                                               $5, lab->ip,
                                                               lex);
2749
            lex->sphead->add_instr(i);
2750
            lex->sphead->restore_lex(YYTHD);
2751 2752
            /* We can shortcut the cont_backpatch here */
            i->m_cont_dest= ip+1;
2753
	  }
2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769
	;

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; }
2770
          ;
2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037
/*
  This part of the parser contains common code for all TABLESPACE
  commands.
  CREATE TABLESPACE name ...
  ALTER TABLESPACE name CHANGE DATAFILE ...
  ALTER TABLESPACE name ADD DATAFILE ...
  ALTER TABLESPACE name access_mode
  CREATE LOGFILE GROUP name ...
  ALTER LOGFILE GROUP name ADD UNDOFILE ..
  ALTER LOGFILE GROUP name ADD REDOFILE ..
  DROP TABLESPACE name
  DROP LOGFILE GROUP name
*/
change_tablespace_access:
          tablespace_name
          ts_access_mode
          ;

change_tablespace_info:
          tablespace_name
          CHANGE ts_datafile
          change_ts_option_list
          ;

tablespace_info:
          tablespace_name
          ADD ts_datafile
          opt_logfile_group_name
          tablespace_option_list
          ;

opt_logfile_group_name:
          /* empty */ {}
          | USE_SYM LOGFILE_SYM GROUP ident
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->logfile_group_name= $4.str;
          };

alter_tablespace_info:
          tablespace_name
          ADD ts_datafile
          alter_tablespace_option_list 
	  { 
	    Lex->alter_tablespace_info->ts_alter_tablespace_type= ALTER_TABLESPACE_ADD_FILE; 
          }
          |
          tablespace_name
          DROP ts_datafile
          alter_tablespace_option_list 
	  { 
	    Lex->alter_tablespace_info->ts_alter_tablespace_type= ALTER_TABLESPACE_DROP_FILE; 
          };

logfile_group_info:
          logfile_group_name
          add_log_file
          logfile_group_option_list
          ;

alter_logfile_group_info:
          logfile_group_name
          add_log_file
          alter_logfile_group_option_list
          ;

add_log_file:
          ADD lg_undofile
          | ADD lg_redofile
          ;

change_ts_option_list:
          /* empty */ {}
          change_ts_options
          ;

change_ts_options:
          change_ts_option
          | change_ts_options change_ts_option
          | change_ts_options ',' change_ts_option
          ;

change_ts_option:
          opt_ts_initial_size
          | opt_ts_autoextend_size
          | opt_ts_max_size
          ;

tablespace_option_list:
          /* empty */ {}
          tablespace_options
          ;

tablespace_options:
          tablespace_option
          | tablespace_options tablespace_option
          | tablespace_options ',' tablespace_option
          ;

tablespace_option:
          opt_ts_initial_size
          | opt_ts_autoextend_size
          | opt_ts_max_size
          | opt_ts_extent_size
          | opt_ts_nodegroup
          | opt_ts_engine
          | ts_wait
          | opt_ts_comment
          ;

alter_tablespace_option_list:
          /* empty */ {}
          alter_tablespace_options
          ;

alter_tablespace_options:
          alter_tablespace_option
          | alter_tablespace_options alter_tablespace_option
          | alter_tablespace_options ',' alter_tablespace_option
          ;

alter_tablespace_option:
          opt_ts_initial_size
          | opt_ts_autoextend_size
          | opt_ts_max_size
          | opt_ts_engine
          | ts_wait
          ;

logfile_group_option_list:
          /* empty */ {}
          logfile_group_options
          ;

logfile_group_options:
          logfile_group_option
          | logfile_group_options logfile_group_option
          | logfile_group_options ',' logfile_group_option
          ;

logfile_group_option:
          opt_ts_initial_size
          | opt_ts_undo_buffer_size
          | opt_ts_redo_buffer_size
          | opt_ts_nodegroup
          | opt_ts_engine
          | ts_wait
          | opt_ts_comment
          ;

alter_logfile_group_option_list:
          /* empty */ {}
          alter_logfile_group_options
          ;

alter_logfile_group_options:
          alter_logfile_group_option
          | alter_logfile_group_options alter_logfile_group_option
          | alter_logfile_group_options ',' alter_logfile_group_option
          ;

alter_logfile_group_option:
          opt_ts_initial_size
          | opt_ts_engine
          | ts_wait
          ;


ts_datafile:
          DATAFILE_SYM TEXT_STRING_sys
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->data_file_name= $2.str;
          };

lg_undofile:
          UNDOFILE_SYM TEXT_STRING_sys
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->undo_file_name= $2.str;
          };

lg_redofile:
          REDOFILE_SYM TEXT_STRING_sys
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->redo_file_name= $2.str;
          };

tablespace_name:
          ident
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info= new st_alter_tablespace();
            lex->alter_tablespace_info->tablespace_name= $1.str;
            lex->sql_command= SQLCOM_ALTER_TABLESPACE;
          };

logfile_group_name:
          ident
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info= new st_alter_tablespace();
            lex->alter_tablespace_info->logfile_group_name= $1.str;
            lex->sql_command= SQLCOM_ALTER_TABLESPACE;
          };

ts_access_mode:
          READ_ONLY_SYM
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_access_mode= TS_READ_ONLY;
          }
          | READ_WRITE_SYM
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_access_mode= TS_READ_WRITE;
          }
          | NOT_SYM ACCESSIBLE_SYM
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_access_mode= TS_NOT_ACCESSIBLE;
          };

opt_ts_initial_size:
          INITIAL_SIZE_SYM opt_equal size_number
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->initial_size= $3;
          };

opt_ts_autoextend_size:
          AUTOEXTEND_SIZE_SYM opt_equal size_number
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->autoextend_size= $3;
          };

opt_ts_max_size:
          MAX_SIZE_SYM opt_equal size_number
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->max_size= $3;
          };

opt_ts_extent_size:
          EXTENT_SIZE_SYM opt_equal size_number
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->extent_size= $3;
          };

opt_ts_undo_buffer_size:
          UNDO_BUFFER_SIZE_SYM opt_equal size_number
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->undo_buffer_size= $3;
          };

opt_ts_redo_buffer_size:
          REDO_BUFFER_SIZE_SYM opt_equal size_number
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->redo_buffer_size= $3;
          };

opt_ts_nodegroup:
3038
          NODEGROUP_SYM opt_equal real_ulong_num
3039 3040 3041 3042
          {
            LEX *lex= Lex;
            if (lex->alter_tablespace_info->nodegroup_id != UNDEF_NODEGROUP)
            {
jonas@perch.ndb.mysql.com's avatar
merge  
jonas@perch.ndb.mysql.com committed
3043
              my_error(ER_FILEGROUP_OPTION_ONLY_ONCE,MYF(0),"NODEGROUP");
3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054
              YYABORT;
            }
            lex->alter_tablespace_info->nodegroup_id= $3;
          };

opt_ts_comment:
          COMMENT_SYM opt_equal TEXT_STRING_sys
          {
            LEX *lex= Lex;
            if (lex->alter_tablespace_info->ts_comment != NULL)
            {
jonas@perch.ndb.mysql.com's avatar
merge  
jonas@perch.ndb.mysql.com committed
3055
              my_error(ER_FILEGROUP_OPTION_ONLY_ONCE,MYF(0),"COMMENT");
3056 3057 3058 3059 3060 3061 3062 3063 3064
              YYABORT;
            }
            lex->alter_tablespace_info->ts_comment= $3.str;
          };

opt_ts_engine:
          opt_storage ENGINE_SYM opt_equal storage_engines
          {
            LEX *lex= Lex;
3065
            if (lex->alter_tablespace_info->storage_engine != NULL)
3066
            {
jonas@perch.ndb.mysql.com's avatar
merge  
jonas@perch.ndb.mysql.com committed
3067
              my_error(ER_FILEGROUP_OPTION_ONLY_ONCE,MYF(0),
3068 3069 3070
                       "STORAGE ENGINE");
              YYABORT;
            }
3071
            lex->alter_tablespace_info->storage_engine= $4;
3072 3073 3074
          };

opt_ts_wait:
3075
          /* empty */
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089
          | ts_wait
          ;

ts_wait:
          WAIT_SYM
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->wait_until_completed= TRUE;
          }
          | NO_WAIT_SYM
          {
            LEX *lex= Lex;
            if (!(lex->alter_tablespace_info->wait_until_completed))
            {
jonas@perch.ndb.mysql.com's avatar
merge  
jonas@perch.ndb.mysql.com committed
3090
              my_error(ER_FILEGROUP_OPTION_ONLY_ONCE,MYF(0),"NO_WAIT");
3091 3092 3093 3094 3095 3096
              YYABORT;
            }
            lex->alter_tablespace_info->wait_until_completed= FALSE;
          };

size_number:
3097
          real_ulong_num { $$= $1;}
3098 3099 3100 3101 3102 3103 3104
          | IDENT
          {
            ulonglong number, test_number;
            uint text_shift_number= 0;
            longlong prefix_number;
            char *start_ptr= $1.str;
            uint str_len= strlen(start_ptr);
3105
            char *end_ptr= start_ptr + str_len;
3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146
            int error;
            prefix_number= my_strtoll10(start_ptr, &end_ptr, &error);
            if ((start_ptr + str_len - 1) == end_ptr)
            {
              switch (end_ptr[0])
              {
                case 'g':
                case 'G':
                  text_shift_number+=10;
                case 'm':
                case 'M':
                  text_shift_number+=10;
                case 'k':
                case 'K':
                  text_shift_number+=10;
                  break;
                default:
                {
                  my_error(ER_WRONG_SIZE_NUMBER, MYF(0));
                  YYABORT;
                }
              }
              if (prefix_number >> 31)
              {
                my_error(ER_SIZE_OVERFLOW_ERROR, MYF(0));
                YYABORT;
              }
              number= prefix_number << text_shift_number;
            }
            else
            {
              my_error(ER_WRONG_SIZE_NUMBER, MYF(0));
              YYABORT;
            }
            $$= number;
          }
          ;

/*
  End tablespace part
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3147 3148

create2:
serg@serg.mylan's avatar
serg@serg.mylan committed
3149
        '(' create2a {}
3150 3151 3152
        | opt_create_table_options
          opt_partitioning {} 
          create3 {}
serg@serg.mylan's avatar
serg@serg.mylan committed
3153 3154 3155
        | LIKE table_ident
          {
            LEX *lex=Lex;
3156
            THD *thd= lex->thd;
mikael@zim.(none)'s avatar
mikael@zim.(none) committed
3157
            if (!(lex->like_name= $2))
venu@myvenu.com's avatar
venu@myvenu.com committed
3158
              YYABORT;
3159 3160 3161 3162 3163
            if ($2->db.str == NULL &&
                thd->copy_db_to(&($2->db.str), &($2->db.length)))
            {
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
3164 3165 3166 3167
          }
        | '(' LIKE table_ident ')'
          {
            LEX *lex=Lex;
3168
            THD *thd= lex->thd;
mikael@zim.(none)'s avatar
mikael@zim.(none) committed
3169
            if (!(lex->like_name= $3))
3170
              YYABORT;
3171 3172 3173 3174 3175
            if ($3->db.str == NULL &&
                thd->copy_db_to(&($3->db.str), &($3->db.length)))
            {
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
3176
          }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3177
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3178

serg@serg.mylan's avatar
serg@serg.mylan committed
3179
create2a:
3180 3181 3182 3183 3184 3185
        field_list ')' opt_create_table_options
          opt_partitioning {}
          create3 {}
        |  opt_partitioning {}
           create_select ')'
           { Select->set_braces(1);} union_opt {}
3186
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3187 3188

create3:
3189
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
3190
	| opt_duplicate opt_as     create_select
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3191
          { Select->set_braces(0);} union_clause {}
serg@serg.mylan's avatar
serg@serg.mylan committed
3192
	| opt_duplicate opt_as '(' create_select ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3193
          { Select->set_braces(1);} union_opt {}
3194 3195
        ;

3196 3197 3198
/*
 This part of the parser is about handling of the partition information.

3199
 It's first version was written by Mikael Ronström with lots of answers to
3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226
 questions provided by Antony Curtis.

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

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

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

partitioning:
        PARTITION_SYM
3227 3228 3229 3230 3231
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
3232
            mem_alloc_error(sizeof(partition_info));
3233 3234
            YYABORT;
          }
3235 3236 3237 3238
          if (lex->sql_command == SQLCOM_ALTER_TABLE)
          {
            lex->alter_info.flags|= ALTER_PARTITION;
          }
3239
        }
3240 3241 3242 3243 3244 3245 3246
        partition
        ;

partition_entry:
        PARTITION_SYM
        {
          LEX *lex= Lex;
3247
          if (!lex->part_info)
3248 3249 3250 3251
          {
            yyerror(ER(ER_PARTITION_ENTRY_ERROR));
            YYABORT;
          }
3252 3253 3254 3255
          /*
            We enter here when opening the frm file to translate
            partition info string into part_info data structure.
          */
3256
        }
3257 3258
        partition {}
        ;
3259 3260

partition:
3261 3262
        BY part_type_def opt_no_parts {} opt_sub_part {} part_defs
        ;
3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278

part_type_def:
        opt_linear KEY_SYM '(' part_field_list ')'
        {
          LEX *lex= Lex;
          lex->part_info->list_of_part_fields= TRUE;
          lex->part_info->part_type= HASH_PARTITION;
        }
        | opt_linear HASH_SYM
        { Lex->part_info->part_type= HASH_PARTITION; }
        part_func {}
        | RANGE_SYM
        { Lex->part_info->part_type= RANGE_PARTITION; }
        part_func {}
        | LIST_SYM
        { Lex->part_info->part_type= LIST_PARTITION; }
3279 3280
        part_func {}
        ;
3281 3282 3283 3284

opt_linear:
        /* empty */ {}
        | LINEAR_SYM
3285 3286
        { Lex->part_info->linear_hash_ind= TRUE;}
        ;
3287 3288

part_field_list:
3289 3290 3291 3292 3293
        /* empty */ {}
        | part_field_item_list {}
        ;

part_field_item_list:
3294
        part_field_item {}
3295
        | part_field_item_list ',' part_field_item {}
3296
        ;
3297 3298 3299 3300

part_field_item:
        ident
        {
3301 3302 3303 3304 3305
          if (Lex->part_info->part_field_list.push_back($1.str))
          {
            mem_alloc_error(1);
            YYABORT;
          }
3306 3307
        }
        ;
3308 3309 3310 3311 3312 3313 3314 3315

part_func:
        '(' remember_name part_func_expr remember_end ')'
        {
          LEX *lex= Lex;
          uint expr_len= (uint)($4 - $2) - 1;
          lex->part_info->list_of_part_fields= FALSE;
          lex->part_info->part_expr= $3;
3316
          lex->part_info->part_func_string= (char* ) sql_memdup($2+1, expr_len);
3317
          lex->part_info->part_func_len= expr_len;
3318 3319
        }
        ;
3320 3321 3322 3323 3324 3325 3326 3327

sub_part_func:
        '(' remember_name part_func_expr remember_end ')'
        {
          LEX *lex= Lex;
          uint expr_len= (uint)($4 - $2) - 1;
          lex->part_info->list_of_subpart_fields= FALSE;
          lex->part_info->subpart_expr= $3;
3328
          lex->part_info->subpart_func_string= (char* ) sql_memdup($2+1, expr_len);        
3329
          lex->part_info->subpart_func_len= expr_len;
3330 3331
        }
        ;
3332 3333 3334 3335


opt_no_parts:
        /* empty */ {}
3336
        | PARTITIONS_SYM real_ulong_num 
3337 3338
        { 
          uint no_parts= $2;
3339
          LEX *lex= Lex;
3340 3341 3342 3343 3344
          if (no_parts == 0)
          {
            my_error(ER_NO_PARTS_ERROR, MYF(0), "partitions");
            YYABORT;
          }
3345 3346 3347

          lex->part_info->no_parts= no_parts;
          lex->part_info->use_default_no_partitions= FALSE;
3348 3349
        }
        ;
3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362

opt_sub_part:
        /* empty */ {}
        | SUBPARTITION_SYM BY opt_linear HASH_SYM sub_part_func
        { Lex->part_info->subpart_type= HASH_PARTITION; }
        opt_no_subparts {}
        | SUBPARTITION_SYM BY opt_linear KEY_SYM
          '(' sub_part_field_list ')'
        {
          LEX *lex= Lex;
          lex->part_info->subpart_type= HASH_PARTITION;
          lex->part_info->list_of_subpart_fields= TRUE;
        }
3363 3364
        opt_no_subparts {}
        ;
3365 3366 3367

sub_part_field_list:
        sub_part_field_item {}
3368 3369
        | sub_part_field_list ',' sub_part_field_item {}
        ;
3370 3371 3372

sub_part_field_item:
        ident
3373 3374 3375 3376 3377 3378 3379
        {
          if (Lex->part_info->subpart_field_list.push_back($1.str))
          {
            mem_alloc_error(1);
            YYABORT;
          }
        }
3380
        ;
3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395

part_func_expr:
        bit_expr
        {
          LEX *lex= Lex;
          bool not_corr_func;
          not_corr_func= !lex->safe_to_cache_query;
          lex->safe_to_cache_query= 1;
          if (not_corr_func)
          {
            yyerror(ER(ER_CONST_EXPR_IN_PARTITION_FUNC_ERROR));
            YYABORT;
          }
          $$=$1;
        }
3396
        ;
3397 3398 3399

opt_no_subparts:
        /* empty */ {}
3400
        | SUBPARTITIONS_SYM real_ulong_num
3401 3402
        {
          uint no_parts= $2;
3403
          LEX *lex= Lex;
3404 3405 3406 3407 3408
          if (no_parts == 0)
          {
            my_error(ER_NO_PARTS_ERROR, MYF(0), "subpartitions");
            YYABORT;
          }
3409 3410
          lex->part_info->no_subparts= no_parts;
          lex->part_info->use_default_no_subpartitions= FALSE;
3411 3412
        }
        ;
3413 3414 3415 3416 3417 3418 3419 3420

part_defs:
        /* empty */
        {}
        | '(' part_def_list ')'
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
3421
          uint count_curr_parts= part_info->partitions.elements;
3422 3423 3424
          if (part_info->no_parts != 0)
          {
            if (part_info->no_parts !=
3425
                count_curr_parts)
3426 3427 3428 3429 3430
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_PART_ERROR));
              YYABORT;
            }
          }
3431
          else if (count_curr_parts > 0)
3432
          {
3433
            part_info->no_parts= count_curr_parts;
3434 3435
          }
          part_info->count_curr_subparts= 0;
3436 3437
        }
        ;
3438 3439 3440

part_def_list:
        part_definition {}
3441 3442
        | part_def_list ',' part_definition {}
        ;
3443 3444 3445 3446 3447 3448 3449

part_definition:
        PARTITION_SYM
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          partition_element *p_elem= new partition_element();
3450 3451 3452
          uint part_id= part_info->partitions.elements;

          if (!p_elem || part_info->partitions.push_back(p_elem))
3453
          {
3454 3455
            mem_alloc_error(sizeof(partition_element));
            YYABORT;
3456
          }
3457
          p_elem->part_state= PART_NORMAL;
3458 3459 3460
          part_info->curr_part_elem= p_elem;
          part_info->current_partition= p_elem;
          part_info->use_default_partitions= FALSE;
3461
          part_info->use_default_no_partitions= FALSE;
3462 3463 3464 3465
        }
        part_name {}
        opt_part_values {}
        opt_part_options {}
3466 3467
        opt_sub_partition {}
        ;
3468 3469

part_name:
3470
        ident
3471 3472 3473 3474 3475 3476
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          partition_element *p_elem= part_info->curr_part_elem;
          p_elem->partition_name= $1.str;
        }
3477
        ;
3478 3479 3480 3481 3482

opt_part_values:
        /* empty */
        {
          LEX *lex= Lex;
3483
          if (!is_partition_management(lex))
3484
          {
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496
            if (lex->part_info->part_type == RANGE_PARTITION)
            {
              my_error(ER_PARTITION_REQUIRES_VALUES_ERROR, MYF(0),
              "RANGE", "LESS THAN");
              YYABORT;
            }
            if (lex->part_info->part_type == LIST_PARTITION)
            {
              my_error(ER_PARTITION_REQUIRES_VALUES_ERROR, MYF(0),
              "LIST", "IN");
              YYABORT;
            }
3497
          }
3498 3499
          else
            lex->part_info->part_type= HASH_PARTITION;
3500 3501 3502
        }
        | VALUES LESS_SYM THAN_SYM part_func_max
        {
3503 3504
          LEX *lex= Lex;
          if (!is_partition_management(lex))
3505
          {
3506 3507 3508 3509 3510 3511
            if (Lex->part_info->part_type != RANGE_PARTITION)
            {
              my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
              "RANGE", "LESS THAN");
              YYABORT;
            }
3512
          }
3513 3514
          else
            lex->part_info->part_type= RANGE_PARTITION;
3515 3516 3517
        }
        | VALUES IN_SYM '(' part_list_func ')'
        {
3518 3519
          LEX *lex= Lex;
          if (!is_partition_management(lex))
3520
          {
3521 3522 3523 3524 3525 3526
            if (Lex->part_info->part_type != LIST_PARTITION)
            {
              my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
              "LIST", "IN");
              YYABORT;
            }
3527
          }
3528 3529
          else
            lex->part_info->part_type= LIST_PARTITION;
3530 3531
        }
        ;
3532 3533

part_func_max:
3534
        max_value_sym
3535 3536 3537 3538 3539 3540 3541 3542
        {
          LEX *lex= Lex;
          if (lex->part_info->defined_max_value)
          {
            yyerror(ER(ER_PARTITION_MAXVALUE_ERROR));
            YYABORT;
          }
          lex->part_info->defined_max_value= TRUE;
3543
          lex->part_info->curr_part_elem->max_value= TRUE;
3544
          lex->part_info->curr_part_elem->range_value= LONGLONG_MAX;
3545 3546 3547 3548 3549 3550 3551 3552
        }
        | part_range_func
        {
          if (Lex->part_info->defined_max_value)
          {
            yyerror(ER(ER_PARTITION_MAXVALUE_ERROR));
            YYABORT;
          }
3553 3554 3555 3556 3557
          if (Lex->part_info->curr_part_elem->has_null_value)
          {
            yyerror(ER(ER_NULL_IN_VALUES_LESS_THAN));
            YYABORT;
          }
3558 3559
        }
        ;
3560

3561 3562 3563 3564 3565
max_value_sym:
        MAX_VALUE_SYM
        | '(' MAX_VALUE_SYM ')'
        ;
        
3566 3567 3568
part_range_func:
        '(' part_bit_expr ')' 
        {
3569 3570 3571 3572
          partition_info *part_info= Lex->part_info;
          if (!($2->unsigned_flag))
            part_info->curr_part_elem->signed_flag= TRUE;
          part_info->curr_part_elem->range_value= $2->value;
3573 3574
        }
        ;
3575 3576 3577

part_list_func:
        part_list_item {}
3578 3579
        | part_list_func ',' part_list_item {}
        ;
3580 3581 3582 3583

part_list_item:
        part_bit_expr
        {
3584
          part_elem_value *value_ptr= $1;
3585 3586 3587
          partition_info *part_info= Lex->part_info;
          if (!value_ptr->unsigned_flag)
            part_info->curr_part_elem->signed_flag= TRUE;
3588
          if (!value_ptr->null_value &&
3589 3590
             part_info->curr_part_elem->
              list_val_list.push_back(value_ptr))
3591
          {
3592
            mem_alloc_error(sizeof(part_elem_value));
3593 3594
            YYABORT;
          }
3595 3596
        }
        ;
3597 3598 3599 3600 3601 3602 3603

part_bit_expr:
        bit_expr
        {
          Item *part_expr= $1;
          bool not_corr_func;
          LEX *lex= Lex;
3604
          THD *thd= YYTHD;
3605
          longlong item_value;
3606 3607
          Name_resolution_context *context= &lex->current_select->context;
          TABLE_LIST *save_list= context->table_list;
3608
          const char *save_where= thd->where;
3609 3610

          context->table_list= 0;
3611
          thd->where= "partition function";
3612 3613 3614 3615 3616 3617 3618 3619 3620

          part_elem_value *value_ptr= 
            (part_elem_value*)sql_alloc(sizeof(part_elem_value));
          if (!value_ptr)
          {
            mem_alloc_error(sizeof(part_elem_value));
            YYABORT;
          }

3621 3622 3623 3624
          if (part_expr->fix_fields(YYTHD, (Item**)0) ||
              ((context->table_list= save_list), FALSE) ||
              (!part_expr->const_item()) ||
              (!lex->safe_to_cache_query))
3625 3626 3627 3628
          {
            yyerror(ER(ER_NO_CONST_EXPR_IN_RANGE_OR_LIST_ERROR));
            YYABORT;
          }
3629
          thd->where= save_where;
3630
          value_ptr->value= part_expr->val_int();
3631 3632 3633 3634
          value_ptr->unsigned_flag= TRUE;
          if (!part_expr->unsigned_flag &&
              value_ptr->value < 0)
            value_ptr->unsigned_flag= FALSE;
3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645
          if ((value_ptr->null_value= part_expr->null_value))
          {
            if (Lex->part_info->curr_part_elem->has_null_value)
            {
              my_error(ER_MULTIPLE_DEF_CONST_IN_LIST_PART_ERROR, MYF(0));
              YYABORT;
            }
            Lex->part_info->curr_part_elem->has_null_value= TRUE;
          }
          else if (part_expr->result_type() != INT_RESULT &&
                   !part_expr->null_value)
3646 3647 3648 3649
          {
            yyerror(ER(ER_INCONSISTENT_TYPE_OF_FUNCTIONS_ERROR));
            YYABORT;
          }
3650
          $$= value_ptr; 
3651
        }
3652
        ;
3653 3654

opt_sub_partition:
3655 3656 3657 3658 3659 3660 3661 3662 3663
        /* empty */
        {
          if (Lex->part_info->no_subparts != 0 &&
              !Lex->part_info->use_default_subpartitions)
          {
            yyerror(ER(ER_PARTITION_WRONG_NO_SUBPART_ERROR));
            YYABORT;
          }
        }
3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678
        | '(' sub_part_list ')'
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          if (part_info->no_subparts != 0)
          {
            if (part_info->no_subparts !=
                part_info->count_curr_subparts)
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_SUBPART_ERROR));
              YYABORT;
            }
          }
          else if (part_info->count_curr_subparts > 0)
          {
3679 3680 3681 3682 3683
            if (part_info->partitions.elements > 1)
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_SUBPART_ERROR));
              YYABORT;
            }
3684 3685 3686
            part_info->no_subparts= part_info->count_curr_subparts;
          }
          part_info->count_curr_subparts= 0;
3687 3688
        }
        ;
3689 3690 3691

sub_part_list:
        sub_part_definition {}
3692 3693
        | sub_part_list ',' sub_part_definition {}
        ;
3694 3695 3696 3697 3698 3699

sub_part_definition:
        SUBPARTITION_SYM
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
3700 3701 3702 3703
          partition_element *curr_part= part_info->current_partition;
          partition_element *sub_p_elem= new partition_element(curr_part);
          if (!sub_p_elem ||
           curr_part->subpartitions.push_back(sub_p_elem))
3704
          {
3705
            mem_alloc_error(sizeof(partition_element));
3706 3707
            YYABORT;
          }
3708
          part_info->curr_part_elem= sub_p_elem;
3709
          part_info->use_default_subpartitions= FALSE;
3710
          part_info->use_default_no_subpartitions= FALSE;
3711 3712
          part_info->count_curr_subparts++;
        }
3713 3714
        sub_name opt_part_options {}
        ;
3715 3716 3717

sub_name:
        ident_or_text
3718 3719
        { Lex->part_info->curr_part_elem->partition_name= $1.str; }
        ;
3720 3721 3722

opt_part_options:
       /* empty */ {}
3723 3724
       | opt_part_option_list {}
       ;
3725 3726 3727

opt_part_option_list:
       opt_part_option_list opt_part_option {}
3728 3729
       | opt_part_option {}
       ;
3730 3731 3732 3733 3734

opt_part_option:
        TABLESPACE opt_equal ident_or_text
        { Lex->part_info->curr_part_elem->tablespace_name= $3.str; }
        | opt_storage ENGINE_SYM opt_equal storage_engines
3735 3736 3737 3738 3739
        {
          LEX *lex= Lex;
          lex->part_info->curr_part_elem->engine_type= $4;
          lex->part_info->default_engine_type= $4;
        }
3740
        | NODEGROUP_SYM opt_equal real_ulong_num
3741
        { Lex->part_info->curr_part_elem->nodegroup_id= $3; }
3742
        | MAX_ROWS opt_equal real_ulonglong_num
3743
        { Lex->part_info->curr_part_elem->part_max_rows= $3; }
3744
        | MIN_ROWS opt_equal real_ulonglong_num
3745 3746 3747 3748 3749 3750
        { Lex->part_info->curr_part_elem->part_min_rows= $3; }
        | DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys
        { Lex->part_info->curr_part_elem->data_file_name= $4.str; }
        | INDEX_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys
        { Lex->part_info->curr_part_elem->index_file_name= $4.str; }
        | COMMENT_SYM opt_equal TEXT_STRING_sys
3751 3752
        { Lex->part_info->curr_part_elem->part_comment= $3.str; }
        ;
3753 3754 3755 3756 3757

/*
 End of partition parser part
*/

serg@serg.mylan's avatar
serg@serg.mylan committed
3758
create_select:
3759
          SELECT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3760
          {
3761
	    LEX *lex=Lex;
3762
	    lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
3763 3764 3765 3766
	    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;
3767 3768 3769 3770
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
3771
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
3772
	    mysql_init_select(lex);
3773
	    lex->current_select->parsing_place= SELECT_LIST;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3774
          }
3775 3776
          select_options select_item_list
	  {
3777
	    Select->parsing_place= NO_MATTER;
3778
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3779
	  opt_select_from
3780 3781 3782 3783 3784 3785 3786
	  {
	    /*
              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
3787
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3788

3789 3790
opt_as:
	/* empty */ {}
3791
	| AS	    {};
3792

3793 3794 3795 3796 3797 3798 3799 3800 3801
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
3802 3803
	default_collation   {}
	| default_charset   {};
3804

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3805
opt_table_options:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3806
	/* empty */	 { $$= 0; }
3807
	| table_options  { $$= $1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3808 3809 3810

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3813
table_option:
3814
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3815 3816

opt_if_not_exists:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3817
	/* empty */	 { $$= 0; }
3818
	| IF not EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3819 3820 3821

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

3824 3825 3826 3827
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
3828 3829
create_table_options:
	create_table_option
3830
	| create_table_option     create_table_options
3831
	| create_table_option ',' create_table_options;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3832 3833

create_table_option:
3834
	ENGINE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; }
3835 3836 3837 3838 3839 3840 3841
	| TYPE_SYM opt_equal storage_engines
          {
            Lex->create_info.db_type= $3;
            WARN_DEPRECATED(yythd, "5.2", "TYPE=storage_engine",
                            "'ENGINE=storage_engine'");
            Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE;
          }
3842 3843
	| 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
3844
	| 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;}
3845
	| PASSWORD opt_equal TEXT_STRING_sys	{ Lex->create_info.password=$3.str; Lex->create_info.used_fields|= HA_CREATE_USED_PASSWORD; }
3846
	| COMMENT_SYM opt_equal TEXT_STRING_sys	{ Lex->create_info.comment=$3; Lex->create_info.used_fields|= HA_CREATE_USED_COMMENT; }
3847
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868
        | PACK_KEYS_SYM opt_equal ulong_num
          {
            switch($3) {
            case 0:
                Lex->create_info.table_options|= HA_OPTION_NO_PACK_KEYS;
                break;
            case 1:
                Lex->create_info.table_options|= HA_OPTION_PACK_KEYS;
                break;
            default:
                yyerror(ER(ER_SYNTAX_ERROR));
                YYABORT;
            }
            Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;
          }
        | PACK_KEYS_SYM opt_equal DEFAULT
          {
            Lex->create_info.table_options&=
              ~(HA_OPTION_PACK_KEYS | HA_OPTION_NO_PACK_KEYS);
            Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;
          }
serg@serg.mylan's avatar
serg@serg.mylan committed
3869 3870
	| 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; }
3871
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
3872
	| UNION_SYM opt_equal '(' table_list ')'
3873 3874 3875
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
3876 3877
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
3878
	    lex->create_info.merge_list.elements--;
3879 3880
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
3881
	    lex->select_lex.table_list.elements=1;
3882 3883 3884
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
3885
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
3886
	  }
3887 3888
	| default_charset
	| default_collation
3889
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
3890 3891
	| 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; }
3892 3893 3894
        | TABLESPACE ident {Lex->create_info.tablespace= $2.str;}
        | STORAGE_SYM DISK_SYM {Lex->create_info.store_on_disk= TRUE;}
        | STORAGE_SYM MEMORY_SYM {Lex->create_info.store_on_disk= FALSE;}
3895
	| CONNECTION_SYM opt_equal TEXT_STRING_sys { Lex->create_info.connect_string.str= $3.str; Lex->create_info.connect_string.length= $3.length;  Lex->create_info.used_fields|= HA_CREATE_USED_CONNECTION; }
3896 3897 3898 3899 3900
	| KEY_BLOCK_SIZE opt_equal ulong_num
	  {
	    Lex->create_info.used_fields|= HA_CREATE_USED_KEY_BLOCK_SIZE;
	    Lex->create_info.key_block_size= $3;
	  }
3901
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3902

3903 3904 3905 3906 3907 3908 3909 3910
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))
          {
3911 3912 3913
            my_error(ER_CONFLICTING_DECLARATIONS, MYF(0),
                     "CHARACTER SET ", cinfo->default_table_charset->csname,
                     "CHARACTER SET ", $4->csname);
3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927
            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))
            {
3928 3929
              my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $4->name, cinfo->default_table_charset->csname);
3930 3931 3932 3933 3934 3935
              YYABORT;
            }
            Lex->create_info.default_table_charset= $4;
            Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

3936
storage_engines:
3937 3938
	ident_or_text
	{
3939
	  $$ = ha_resolve_by_name(YYTHD, &$1);
3940 3941
	  if ($$ == NULL)
          if (YYTHD->variables.sql_mode & MODE_NO_ENGINE_SUBSTITUTION)
3942
	  {
3943
	    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
3944 3945
	    YYABORT;
	  }
3946 3947 3948 3949 3950 3951
          else
          {
            push_warning_printf(YYTHD, MYSQL_ERROR::WARN_LEVEL_ERROR,
                                ER_UNKNOWN_STORAGE_ENGINE,
                                ER(ER_UNKNOWN_STORAGE_ENGINE), $1.str);
          }
3952
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3953 3954 3955 3956 3957

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
3958 3959 3960
	| 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
3961

3962 3963 3964
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
3965
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
3966

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3967
opt_select_from:
3968
	opt_limit_clause {}
3969
	| select_from select_lock_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3970 3971

udf_func_type:
3972
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
3973
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3974 3975 3976 3977

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
3978
        | DECIMAL_SYM {$$ = (int) DECIMAL_RESULT; }
3979
	| INT_SYM {$$ = (int) INT_RESULT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3980 3981 3982

field_list:
	  field_list_item
3983
	| field_list ',' field_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3984 3985 3986


field_list_item:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3987
	   column_def
3988 3989 3990 3991
         | key_def
         ;

column_def:
3992
	  field_spec opt_check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3993 3994 3995 3996
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3997
	;
3998 3999

key_def:
4000
	key_type opt_ident key_alg '(' key_list ')' key_options
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4001
	  {
4002
	    LEX *lex=Lex;
4003
	    if ($1 != Key::FULLTEXT && lex->key_create_info.parser_name.str)
4004 4005 4006 4007
	    {
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
	    }
4008
	    lex->key_list.push_back(new Key($1,$2, &lex->key_create_info, 0,
4009
					   lex->col_list));
4010
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4011
	  }
4012 4013
	| opt_constraint constraint_key_type opt_ident key_alg
	  '(' key_list ')' key_options
4014 4015
	  {
	    LEX *lex=Lex;
4016
	    const char *key_name= $3 ? $3 : $1;
4017
	    lex->key_list.push_back(new Key($2, key_name, &lex->key_create_info, 0,
4018
					    lex->col_list));
4019 4020
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
4021
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4022
	  {
4023
	    LEX *lex=Lex;
4024
	    lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
4025 4026 4027 4028 4029
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
4030
	    lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4 : $1,
4031
					    &default_key_create_info, 1,
4032
					    lex->col_list));
4033
	    lex->col_list.empty();		/* Alloced by sql_alloc */
4034 4035 4036

            /* Only used for ALTER TABLE. Ignored otherwise. */
            lex->alter_info.flags|= ALTER_FOREIGN_KEY;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4037
	  }
4038 4039 4040 4041
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
4042
	| opt_constraint check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4043 4044
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
4045 4046 4047
	  }
	;

4048
opt_check_constraint:
4049
	/* empty */
4050 4051 4052 4053 4054
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
4055
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4056 4057

opt_constraint:
4058
	/* empty */		{ $$=(char*) 0; }
monty@mysql.com's avatar
monty@mysql.com committed
4059 4060 4061 4062 4063 4064
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4065 4066 4067 4068

field_spec:
	field_ident
	 {
4069
	   LEX *lex=Lex;
4070
	   lex->length=lex->dec=0; lex->type=0;
4071
	   lex->default_value= lex->on_update_value= 0;
4072
           lex->comment=null_lex_str;
4073
	   lex->charset=NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4074 4075 4076
	 }
	type opt_attribute
	{
4077
	  LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4078
	  if (add_field_to_list(lex->thd, $1.str,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4079
				(enum enum_field_types) $3,
4080
				lex->length,lex->dec,lex->type,
4081
				lex->default_value, lex->on_update_value, 
4082
                                &lex->comment,
4083
				lex->change,&lex->interval_list,lex->charset,
4084
				lex->uint_geom_type))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4085
	    YYABORT;
4086
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4087 4088

type:
4089
	int_type opt_len field_options	{ $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4090 4091
	| 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
4092 4093 4094 4095
	| 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
4096 4097
	| BOOL_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
4098 4099
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
4100
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4101 4102 4103
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4104
	| nchar '(' NUM ')' opt_bin_mod	{ Lex->length=$3.str;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4105
					  $$=FIELD_TYPE_STRING;
4106
					  Lex->charset=national_charset_info; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4107
	| nchar opt_bin_mod		{ Lex->length=(char*) "1";
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4108
					  $$=FIELD_TYPE_STRING;
4109
					  Lex->charset=national_charset_info; }
4110
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
4111
					  Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4112
					  $$=FIELD_TYPE_STRING; }
4113 4114 4115
	| BINARY			{ Lex->length= (char*) "1";
					  Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_STRING; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4116
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
4117
					  $$= MYSQL_TYPE_VARCHAR; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4118
	| nvarchar '(' NUM ')' opt_bin_mod { Lex->length=$3.str;
4119
					  $$= MYSQL_TYPE_VARCHAR;
4120
					  Lex->charset=national_charset_info; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4121
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
4122
					  Lex->charset=&my_charset_bin;
4123
					  $$= MYSQL_TYPE_VARCHAR; }
4124
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4125 4126
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
4127
	| TIMESTAMP opt_len
4128
	  {
4129
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
4130 4131
	      $$=FIELD_TYPE_DATETIME;
	    else
4132 4133 4134 4135 4136
            {
              /* 
                Unlike other types TIMESTAMP fields are NOT NULL by default.
              */
              Lex->type|= NOT_NULL_FLAG;
4137
	      $$=FIELD_TYPE_TIMESTAMP;
4138
            }
4139
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4140
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
4141
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4142
					  $$=FIELD_TYPE_TINY_BLOB; }
4143
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4144
					  $$=FIELD_TYPE_BLOB; }
4145 4146
	| spatial_type
          {
hf@deer.(none)'s avatar
hf@deer.(none) committed
4147
#ifdef HAVE_SPATIAL
4148 4149 4150
            Lex->charset=&my_charset_bin;
            Lex->uint_geom_type= (uint)$1;
            $$=FIELD_TYPE_GEOMETRY;
hf@deer.(none)'s avatar
hf@deer.(none) committed
4151
#else
serg@serg.mylan's avatar
serg@serg.mylan committed
4152
            my_error(ER_FEATURE_DISABLED, MYF(0),
4153
                     sym_group_geom.name, sym_group_geom.needed_define);
4154
            YYABORT;
hf@deer.(none)'s avatar
hf@deer.(none) committed
4155
#endif
4156
          }
4157
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4158
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
4159
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4160
					  $$=FIELD_TYPE_LONG_BLOB; }
4161
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4162
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
4163 4164
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
4165
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
4166 4167
	| 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
4168
	| DECIMAL_SYM float_options field_options
4169
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4170
	| NUMERIC_SYM float_options field_options
4171
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
4172
	| FIXED_SYM float_options field_options
4173
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
4174
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
4175
	  { $$=FIELD_TYPE_ENUM; }
4176
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
4177
	  { $$=FIELD_TYPE_SET; }
4178
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
4179 4180 4181 4182 4183 4184
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
4185
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4186

hf@deer.(none)'s avatar
hf@deer.(none) committed
4187 4188 4189
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
4190 4191 4192
	| POINT_SYM           { Lex->length= (char*)"21";
                                $$= Field::GEOM_POINT;
                              }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4193 4194 4195 4196 4197 4198 4199
	| 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
4200 4201
char:
	CHAR_SYM {}
4202 4203 4204 4205 4206 4207
	;

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4209 4210 4211
varchar:
	char VARYING {}
	| VARCHAR {}
4212 4213 4214 4215
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4216
	| NVARCHAR_SYM {}
4217 4218 4219 4220
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4221 4222 4223 4224 4225 4226

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

real_type:
4230
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4231 4232
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
4233
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4234 4235 4236


float_options:
4237 4238
        /* empty */		{ Lex->dec=Lex->length= (char*)0; }
        | '(' NUM ')'		{ Lex->length=$2.str; Lex->dec= (char*)0; }
4239
	| precision		{};
4240 4241 4242 4243 4244 4245

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

field_options:
	/* empty */		{}
4250
	| field_opt_list	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4251 4252 4253

field_opt_list:
	field_opt_list field_option {}
4254
	| field_option {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4255 4256

field_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4257
	SIGNED_SYM	{}
4258
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
4259
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4260 4261

opt_len:
4262 4263
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4264 4265 4266

opt_precision:
	/* empty */	{}
4267
	| precision	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4268 4269 4270

opt_attribute:
	/* empty */ {}
4271
	| opt_attribute_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4272 4273 4274

opt_attribute_list:
	opt_attribute_list attribute {}
4275
	| attribute;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4276 4277 4278

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
4279
	| not NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
4280 4281 4282
	| 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
4283
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
4284
	| SERIAL_SYM DEFAULT VALUE_SYM
4285 4286 4287
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
4288
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4289 4290 4291 4292 4293
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
4294
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4295 4296 4297 4298 4299
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
4300
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4301 4302 4303 4304 4305
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
4306
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4307
	  }
4308
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= $2; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4309 4310
	| COLLATE_SYM collation_name
	  {
4311
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4312
	    {
4313 4314
	      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
4315 4316 4317 4318 4319 4320 4321 4322
	      YYABORT;
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4323

4324 4325 4326 4327 4328
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

4329 4330 4331 4332
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
4333

4334
charset_name:
4335
	ident_or_text
4336
	{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4337
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
4338
	  {
4339
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
4340 4341
	    YYABORT;
	  }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4342 4343 4344
	}
	| BINARY { $$= &my_charset_bin; }
	;
4345

4346 4347 4348
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4349

4350 4351 4352 4353 4354 4355 4356

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)))
	  {
4357
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
4358 4359 4360 4361 4362 4363 4364 4365 4366 4367
	    YYABORT;
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

4368
collation_name:
4369
	ident_or_text
4370 4371 4372
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
4373
	    my_error(ER_UNKNOWN_COLLATION, MYF(0), $1.str);
4374 4375 4376 4377
	    YYABORT;
	  }
	};

4378 4379
opt_collate:
	/* empty */	{ $$=NULL; }
4380
	| COLLATE_SYM collation_name_or_default { $$=$2; }
4381 4382
	;

4383 4384 4385 4386
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

4387 4388 4389 4390
opt_default:
	/* empty */	{}
	| DEFAULT	{};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4391
opt_binary:
4392
	/* empty */			{ Lex->charset=NULL; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4393
	| ASCII_SYM opt_bin_mod		{ Lex->charset=&my_charset_latin1; }
4394
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411
	| UNICODE_SYM opt_bin_mod
	{
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
	  {
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
	    YYABORT;
	  }
	}
	| charset charset_name opt_bin_mod	{ Lex->charset=$2; }
        | BINARY opt_bin_charset { Lex->type|= BINCMP_FLAG; };

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

opt_bin_charset:
4412
        /* empty */ { Lex->charset= NULL; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4413
	| ASCII_SYM	{ Lex->charset=&my_charset_latin1; }
4414 4415
	| UNICODE_SYM
	{
4416 4417
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
4418
	  {
4419
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
4420 4421 4422
	    YYABORT;
	  }
	}
4423
	| charset charset_name	{ Lex->charset=$2; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4424

4425 4426 4427
opt_primary:
	/* empty */
	| PRIMARY_SYM
4428
	;
4429

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4430
references:
4431 4432
	REFERENCES table_ident
	{
4433
	  LEX *lex=Lex;
4434 4435 4436 4437 4438 4439
	  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
4440
	};
4441

4442
opt_ref_list:
4443
	/* empty */ opt_on_delete {}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4444
	| '(' ref_list ')' opt_on_delete {};
4445 4446 4447

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
4448
	| ident			{ Lex->ref_list.push_back(new key_part_spec($1.str)); };
4449

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4450 4451 4452

opt_on_delete:
	/* empty */ {}
4453
	| opt_on_delete_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4454 4455 4456

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
4457
	| opt_on_delete_item {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4458 4459

opt_on_delete_item:
4460 4461 4462 4463
	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
4464
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4465 4466

delete_option:
4467 4468 4469 4470
	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
4471
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4472 4473

key_type:
4474
	key_or_index			    { $$= Key::MULTIPLE; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4475 4476
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
hf@deer.(none)'s avatar
hf@deer.(none) committed
4477 4478 4479 4480
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
4481 4482
	    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
4483 4484 4485
	    YYABORT;
#endif
	  };
4486 4487 4488

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4489
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4490 4491 4492

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

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4495 4496 4497 4498 4499
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4500 4501
keys_or_index:
	KEYS {}
4502
	| INDEX_SYM {}
4503
	| INDEXES {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4504

4505
opt_unique_or_fulltext:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4506 4507
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
serg@serg.mylan's avatar
serg@serg.mylan committed
4508
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
hf@deer.(none)'s avatar
hf@deer.(none) committed
4509 4510 4511 4512 4513
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
serg@serg.mylan's avatar
serg@serg.mylan committed
4514
            my_error(ER_FEATURE_DISABLED, MYF(0),
4515
                     sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
4516 4517 4518
	    YYABORT;
#endif
	  }
serg@serg.mylan's avatar
serg@serg.mylan committed
4519
        ;
4520

4521 4522
init_key_options:
	{
4523
	  Lex->key_create_info= default_key_create_info;
4524 4525 4526 4527
	}
	;

/*
4528
  For now, key_alg initializies lex->key_create_info.
4529 4530 4531 4532
  In the future, when all key options are after key definition,
  we can remove key_alg and move init_key_options to key_options
*/

4533
key_alg:
4534
	/* empty */ init_key_options
4535
	| init_key_options key_using_alg
4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546
	;

key_options:
	/* empty */ {}
	| key_opts
	;

key_opts:
	key_opt
	| key_opts key_opt
	;
4547 4548 4549 4550 4551 4552

key_using_alg:
	USING btree_or_rtree       { Lex->key_create_info.algorithm= $2; }
	| TYPE_SYM btree_or_rtree  { Lex->key_create_info.algorithm= $2; }
        ;

4553
key_opt:
4554
        key_using_alg
4555
	| KEY_BLOCK_SIZE opt_equal ulong_num
4556
	  { Lex->key_create_info.block_size= $3; }
4557 4558 4559
	| WITH PARSER_SYM IDENT_sys
          {
            if (plugin_is_ready(&$3, MYSQL_FTPARSER_PLUGIN))
4560
              Lex->key_create_info.parser_name= $3;
4561 4562 4563 4564 4565 4566 4567 4568
            else
            {
              my_error(ER_FUNCTION_NOT_DEFINED, MYF(0), $3.str);
              YYABORT;
            }
          }
        ;

4569

4570
btree_or_rtree:
4571
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4572 4573 4574 4575
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4576
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4577 4578 4579

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
4580
	| key_part order_dir		{ Lex->col_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4581 4582 4583

key_part:
	ident			{ $$=new key_part_spec($1.str); }
4584
	| ident '(' NUM ')'
4585 4586 4587 4588
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
4589
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
4590 4591 4592
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4593 4594 4595

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

4598
opt_component:
4599 4600
        /* empty */      { $$= null_lex_str; }
        | '.' ident      { $$= $2; };
4601

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4602 4603
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
4604
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4605 4606 4607 4608 4609 4610 4611 4612

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
4613
	  THD *thd= YYTHD;
4614
	  LEX *lex= thd->lex;
mikael@zim.(none)'s avatar
mikael@zim.(none) committed
4615
         lex->name= 0;
4616 4617
	  lex->sql_command= SQLCOM_ALTER_TABLE;
	  lex->duplicates= DUP_ERROR; 
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4618 4619
	  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
4620 4621 4622 4623
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
4624
          lex->select_lex.init_order();
4625
	  lex->name= 0;
4626
	  lex->like_name= 0;
4627 4628
	  lex->select_lex.db=
            ((TABLE_LIST*) lex->select_lex.table_list.first)->db;
4629
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
4630
	  lex->create_info.db_type= 0;
4631
	  lex->create_info.default_table_charset= NULL;
4632
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
4633
	  lex->alter_info.reset();
4634
	  lex->alter_info.flags= 0;
4635
          lex->no_write_to_binlog= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4636
	}
4637
	alter_commands
4638
	{}
4639
	| ALTER DATABASE ident_or_empty
4640 4641 4642 4643 4644
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
          opt_create_database_options
4645 4646
	  {
	    LEX *lex=Lex;
4647
            THD *thd= Lex->thd;
4648
	    lex->sql_command=SQLCOM_ALTER_DB;
4649
	    lex->name= $3;
4650 4651
            if (lex->name == NULL && thd->copy_db_to(&lex->name, NULL))
              YYABORT;
4652 4653 4654 4655 4656
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

4657 4658 4659 4660 4661
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
	      YYABORT;
	    }
4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673
	    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;
4674

4675 4676 4677 4678 4679
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
	      YYABORT;
	    }
4680 4681 4682 4683 4684 4685 4686 4687 4688
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    LEX *lex=Lex;

	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
	    lex->spname= $3;
	  }
4689 4690
        | ALTER view_algorithm_opt definer view_suid
          VIEW_SYM table_ident
4691 4692 4693 4694 4695 4696
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_CREATE_VIEW;
	    lex->create_view_mode= VIEW_ALTER;
	    /* first table in list is target VIEW name */
4697
	    lex->select_lex.add_table_to_list(thd, $6, NULL, TL_OPTION_UPDATING);
4698
	  }
4699
	  view_list_opt AS view_select view_check_option
4700
	  {}
4701
	| ALTER EVENT_SYM sp_name
4702
          /*
4703 4704
            BE CAREFUL when you add a new rule to update the block where
            YYTHD->client_capabilities is set back to original value
4705
          */
4706
          {
4707
            /* 
4708 4709 4710 4711 4712
              It is safe to use Lex->spname because
              ALTER EVENT xxx RENATE TO yyy DO ALTER EVENT RENAME TO
              is not allowed. Lex->spname is used in the case of RENAME TO
              If it had to be supported spname had to be added to
              Event_parse_data.
4713
            */
4714

4715
            if (!(Lex->event_parse_data= Event_parse_data::new_instance(YYTHD)))
andrey@lmy004's avatar
andrey@lmy004 committed
4716
              YYABORT;
4717
            Lex->event_parse_data->identifier= $3;
4718

4719
            /*
4720
              We have to turn off CLIENT_MULTI_QUERIES while parsing a
4721 4722
              stored procedure, otherwise yylex will chop it into pieces
              at each ';'.
4723
            */
andrey@lmy004's avatar
andrey@lmy004 committed
4724
            $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
4725
            YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
4726 4727

            Lex->sql_command= SQLCOM_ALTER_EVENT;
4728 4729
            /* we need that for disallowing subqueries */
            Lex->expr_allows_subselect= FALSE;
4730
          }
4731 4732 4733 4734 4735
          ev_alter_on_schedule_completion
          opt_ev_rename_to
          opt_ev_status
          opt_ev_comment
          opt_ev_sql_stmt
4736
          {
4737 4738 4739 4740 4741 4742 4743 4744
            /*
              $1 - ALTER
              $2 - EVENT_SYM
              $3 - sp_name
              $4 - the block above
            */
            YYTHD->client_capabilities |= $<ulong_num>4;

4745
            if (!($5 || $6 || $7 || $8 || $9))
andrey@lmy004's avatar
andrey@lmy004 committed
4746 4747 4748 4749
            {
	      yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
4750 4751 4752 4753
            /*
              sql_command is set here because some rules in ev_sql_stmt
              can overwrite it
            */
andrey@lmy004's avatar
andrey@lmy004 committed
4754
            Lex->sql_command= SQLCOM_ALTER_EVENT;
4755
            Lex->expr_allows_subselect= TRUE;
andrey@lmy004's avatar
andrey@lmy004 committed
4756
          }
4757 4758 4759 4760 4761
        | ALTER TABLESPACE alter_tablespace_info
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= ALTER_TABLESPACE;
          }
4762
        | ALTER LOGFILE_SYM GROUP alter_logfile_group_info
4763 4764 4765 4766 4767 4768 4769 4770 4771
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= ALTER_LOGFILE_GROUP;
          }
        | ALTER TABLESPACE change_tablespace_info
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= CHANGE_FILE_TABLESPACE;
          }
4772
        | ALTER TABLESPACE change_tablespace_access
4773 4774 4775 4776
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= ALTER_ACCESS_MODE_TABLESPACE;
          }
4777
	;
4778

4779 4780 4781 4782
ev_alter_on_schedule_completion: /* empty */ { $$= 0;}
        | ON SCHEDULE_SYM ev_schedule_time { $$= 1; }
        | ev_on_completion { $$= 1; }
        | ON SCHEDULE_SYM ev_schedule_time ev_on_completion { $$= 1; }
4783
      ;
4784

4785
opt_ev_rename_to: /* empty */ { $$= 0;}
4786 4787
        | RENAME TO_SYM sp_name
          {
4788 4789 4790 4791 4792
            /*
              Use lex's spname to hold the new name.
              The original name is in the Event_parse_data object
            */
            Lex->spname= $3; 
4793
            $$= 1;
4794 4795
          }
      ;
4796 4797 4798

opt_ev_sql_stmt: /* empty*/ { $$= 0;}
        | DO_SYM ev_sql_stmt { $$= 1; }
4799
        ;
andrey@lmy004's avatar
andrey@lmy004 committed
4800

4801

4802 4803 4804 4805
ident_or_empty:
	/* empty */  { $$= 0; }
	| ident      { $$= $1.str; };

4806
alter_commands:
4807 4808
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
4809
        | alter_list
4810
          opt_partitioning
4811 4812 4813
        | alter_list
          remove_partitioning
        | remove_partitioning
4814
        | partitioning
4815
/*
4816
  This part was added for release 5.1 by Mikael Ronstrm.
4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827
  From here we insert a number of commands to manage the partitions of a
  partitioned table such as adding partitions, dropping partitions,
  reorganising partitions in various manners. In future releases the list
  will be longer and also include moving partitions to a
  new table and so forth.
*/
        | add_partition_rule
        | DROP PARTITION_SYM alt_part_name_list
          {
	    Lex->alter_info.flags|= ALTER_DROP_PARTITION;
          }
4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868
        | REBUILD_SYM PARTITION_SYM opt_no_write_to_binlog
          all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_REBUILD_PARTITION;
            lex->no_write_to_binlog= $3;
          }
        | OPTIMIZE PARTITION_SYM opt_no_write_to_binlog
          all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_OPTIMIZE_PARTITION;
            lex->no_write_to_binlog= $3;
            lex->check_opt.init();
          }
          opt_no_write_to_binlog opt_mi_check_type
        | ANALYZE_SYM PARTITION_SYM opt_no_write_to_binlog
          all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_ANALYZE_PARTITION;
            lex->no_write_to_binlog= $3;
            lex->check_opt.init();
          }
          opt_mi_check_type
        | CHECK_SYM PARTITION_SYM all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_CHECK_PARTITION;
            lex->check_opt.init();
          }
          opt_mi_check_type
        | REPAIR PARTITION_SYM opt_no_write_to_binlog
          all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_REPAIR_PARTITION;
            lex->no_write_to_binlog= $3;
            lex->check_opt.init();
          }
          opt_mi_repair_type
4869
        | COALESCE PARTITION_SYM opt_no_write_to_binlog real_ulong_num
4870 4871 4872
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_COALESCE_PARTITION;
4873 4874
            lex->no_write_to_binlog= $3;
	    lex->alter_info.no_parts= $4;
4875 4876
          }
        | reorg_partition_rule
4877 4878
        ;

4879 4880 4881 4882 4883 4884 4885
remove_partitioning:
        REMOVE_SYM PARTITIONING_SYM
        {
          Lex->alter_info.flags|= ALTER_REMOVE_PARTITIONING;
        }
        ;

4886
all_or_alt_part_name_list:
4887
        ALL
4888 4889 4890 4891 4892 4893
        {
	  Lex->alter_info.flags|= ALTER_ALL_PARTITION;
        }
        | alt_part_name_list
        ;

4894
add_partition_rule:
4895
        ADD PARTITION_SYM opt_no_write_to_binlog
4896 4897 4898 4899 4900
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
4901
            mem_alloc_error(sizeof(partition_info));
4902 4903 4904
            YYABORT;
          }
	  lex->alter_info.flags|= ALTER_ADD_PARTITION;
4905
          lex->no_write_to_binlog= $3;
4906 4907 4908 4909 4910 4911 4912 4913 4914
        }
        add_part_extra
        {}
        ;

add_part_extra:
        | '(' part_def_list ')'
        {
          LEX *lex= Lex;
4915
          lex->part_info->no_parts= lex->part_info->partitions.elements;
4916
        }
4917
        | PARTITIONS_SYM real_ulong_num
4918 4919 4920 4921 4922 4923 4924
        {
          LEX *lex= Lex;
          lex->part_info->no_parts= $2;
        }
        ;

reorg_partition_rule:
4925
        REORGANIZE_SYM PARTITION_SYM opt_no_write_to_binlog
4926 4927 4928 4929 4930
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
4931
            mem_alloc_error(sizeof(partition_info));
4932 4933
            YYABORT;
          }
4934
          lex->no_write_to_binlog= $3;
4935
        }
4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949
        reorg_parts_rule
        ;

reorg_parts_rule:
        /* empty */
        {
	  Lex->alter_info.flags|= ALTER_TABLE_REORG;
        }
        |
        alt_part_name_list
        {
	  Lex->alter_info.flags|= ALTER_REORGANIZE_PARTITION;
        }
        INTO '(' part_def_list ')'
4950 4951
        {
          LEX *lex= Lex;
4952
          lex->part_info->no_parts= lex->part_info->partitions.elements;
4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963
        }
        ;

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

alt_part_name_item:
        ident
        {
4964 4965 4966 4967 4968
          if (Lex->alter_info.partition_names.push_back($1.str))
          {
            mem_alloc_error(1);
            YYABORT;
          }
4969 4970 4971 4972 4973 4974 4975
        }
        ;

/*
  End of management of partition commands
*/

4976 4977 4978 4979
alter_list:
        alter_list_item
	| alter_list ',' alter_list_item
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4980 4981

add_column:
4982
	ADD opt_column
4983 4984
	{
	  LEX *lex=Lex;
4985 4986
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
4987
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4988 4989

alter_list_item:
4990 4991 4992 4993
	add_column column_def opt_place { }
	| ADD key_def
	  {
	    Lex->alter_info.flags|= ALTER_ADD_INDEX;
4994
	  }
4995 4996 4997 4998
	| add_column '(' field_list ')'
          {
	    Lex->alter_info.flags|= ALTER_ADD_COLUMN | ALTER_ADD_INDEX;
          }
4999 5000 5001
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
5002
	     lex->change= $3.str;
5003
	     lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
5004
	  }
5005
          field_spec opt_place
5006 5007 5008
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
5009
            lex->length=lex->dec=0; lex->type=0;
5010
            lex->default_value= lex->on_update_value= 0;
5011
            lex->comment=null_lex_str;
5012
	    lex->charset= NULL;
5013
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
5014 5015 5016 5017
          }
          type opt_attribute
          {
            LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5018
            if (add_field_to_list(lex->thd,$3.str,
5019 5020
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
5021
                                  lex->default_value, lex->on_update_value,
5022
                                  &lex->comment,
5023
				  $3.str, &lex->interval_list, lex->charset,
5024
				  lex->uint_geom_type))
5025 5026 5027
	       YYABORT;
          }
          opt_place
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5028
	| DROP opt_column field_ident opt_restrict
5029 5030
	  {
	    LEX *lex=Lex;
5031
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
5032
                                                               $3.str));
5033
	    lex->alter_info.flags|= ALTER_DROP_COLUMN;
5034
	  }
5035 5036
	| DROP FOREIGN KEY_SYM opt_ident
          {
5037
	    Lex->alter_info.flags|= ALTER_DROP_INDEX | ALTER_FOREIGN_KEY;
5038
          }
5039 5040 5041
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
5042 5043 5044
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
				               primary_key_name));
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
5045
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5046
	| DROP key_or_index field_ident
5047 5048
	  {
	    LEX *lex=Lex;
5049 5050 5051
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
					                       $3.str));
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
5052
	  }
5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064
	| 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;
          }
5065
	| ALTER opt_column field_ident SET DEFAULT signed_literal
5066 5067
	  {
	    LEX *lex=Lex;
5068
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,$6));
5069
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
5070
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5071
	| ALTER opt_column field_ident DROP DEFAULT
5072 5073
	  {
	    LEX *lex=Lex;
5074 5075
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,
                                                                  (Item*) 0));
5076
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
5077
	  }
5078
	| RENAME opt_to table_ident
5079
	  {
5080
	    LEX *lex=Lex;
5081
            THD *thd= lex->thd;
5082
	    lex->select_lex.db=$3->db.str;
5083 5084 5085 5086 5087
            if (lex->select_lex.db == NULL &&
                thd->copy_db_to(&lex->select_lex.db, NULL))
            {
              YYABORT;
            }
5088 5089 5090
            if (check_table_name($3->table.str,$3->table.length) ||
                $3->db.str && check_db_name($3->db.str))
            {
5091
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3->table.str);
5092 5093
              YYABORT;
            }
5094
	    lex->name= $3->table.str;
5095
	    lex->alter_info.flags|= ALTER_RENAME;
5096
	  }
5097 5098 5099 5100 5101 5102 5103 5104 5105 5106
	| 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))
	    {
5107 5108
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $5->name, $4->csname);
5109 5110
	      YYABORT;
	    }
5111
	    LEX *lex= Lex;
5112
	    lex->create_info.table_charset=
monty@mysql.com's avatar
monty@mysql.com committed
5113 5114 5115
	      lex->create_info.default_table_charset= $5;
	    lex->create_info.used_fields|= (HA_CREATE_USED_CHARSET |
					    HA_CREATE_USED_DEFAULT_CHARSET);
5116
	    lex->alter_info.flags|= ALTER_CONVERT;
5117
	  }
5118
        | create_table_options_space_separated
5119 5120
	  {
	    LEX *lex=Lex;
5121
	    lex->alter_info.flags|= ALTER_OPTIONS;
5122
	  }
5123 5124 5125 5126
	| FORCE_SYM
	  {
	    Lex->alter_info.flags|= ALTER_FORCE;
	   }
5127
	| order_clause
5128 5129
	  {
	    LEX *lex=Lex;
5130
	    lex->alter_info.flags|= ALTER_ORDER;
5131
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5132 5133 5134

opt_column:
	/* empty */	{}
5135
	| COLUMN_SYM	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5136 5137

opt_ignore:
5138 5139 5140
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5141 5142

opt_restrict:
5143 5144 5145 5146
	/* 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
5147 5148 5149 5150

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
5151
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5152 5153 5154 5155

opt_to:
	/* empty */	{}
	| TO_SYM	{}
5156
	| EQ		{}
5157
	| AS		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5158

nick@mysql.com's avatar
nick@mysql.com committed
5159
/*
5160
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5161 5162
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5163
slave:
5164
	  START_SYM SLAVE slave_thread_opts
5165 5166 5167 5168 5169 5170
          {
	    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
5171
            /* If you change this code don't forget to update SLAVE START too */
5172 5173 5174
          }
          slave_until
          {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5175 5176 5177 5178 5179
        | 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
5180
            /* If you change this code don't forget to update SLAVE STOP too */
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5181
          }
5182 5183 5184 5185 5186
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
5187 5188 5189 5190 5191
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
5192 5193 5194 5195 5196 5197 5198 5199
	| 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
5200

5201
start:
5202 5203
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
5204 5205 5206
          LEX *lex= Lex;
          lex->sql_command= SQLCOM_BEGIN;
          lex->start_transaction_opt= $3;
5207
        }
5208 5209
	;

5210 5211 5212 5213 5214 5215
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
5216
        ;
5217

5218
slave_thread_opts:
5219 5220
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
5221
        {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5222
	;
5223 5224

slave_thread_opt_list:
5225
	slave_thread_opt
5226 5227
	| slave_thread_opt_list ',' slave_thread_opt
	;
5228 5229

slave_thread_opt:
5230
	/*empty*/	{}
5231
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
5232
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
5233
	;
5234

5235 5236 5237 5238 5239 5240 5241 5242 5243 5244
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
5245 5246
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


5258 5259 5260 5261
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
5262 5263
           WARN_DEPRECATED(yythd, "5.2", "RESTORE TABLE",
                           "MySQL Administrator (mysqldump, mysql)");
5264
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5265
	table_list FROM TEXT_STRING_sys
5266 5267
        {
	  Lex->backup_dir = $6.str;
5268
        };
5269

5270 5271 5272 5273
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
5274 5275
           WARN_DEPRECATED(yythd, "5.2", "BACKUP TABLE",
                           "MySQL Administrator (mysqldump, mysql)");
5276
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5277
	table_list TO_SYM TEXT_STRING_sys
5278 5279
        {
	  Lex->backup_dir = $6.str;
5280
        };
5281

5282 5283 5284 5285 5286 5287
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
5288 5289
	table_list opt_checksum_type
        {}
5290 5291
	;

5292 5293 5294 5295 5296 5297
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
5298
repair:
5299
	REPAIR opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5300
	{
5301 5302
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
5303
           lex->no_write_to_binlog= $2;
5304
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5305
	}
5306 5307 5308
	table_list opt_mi_repair_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5309

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5310
opt_mi_repair_type:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5311
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
5312
	| mi_repair_types {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5313

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5314 5315
mi_repair_types:
	mi_repair_type {}
5316
	| mi_repair_type mi_repair_types {};
5317

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5318
mi_repair_type:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5319
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
5320
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
5321
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5322 5323

analyze:
5324
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5325
	{
5326 5327
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
5328
           lex->no_write_to_binlog= $2;
5329
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5330
	}
5331 5332 5333
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5334

5335 5336 5337 5338 5339 5340
binlog_base64_event:
        BINLOG_SYM TEXT_STRING_sys
        {
           Lex->sql_command = SQLCOM_BINLOG_BASE64_EVENT;
           Lex->comment= $2;
        }
5341
        ;
5342

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5343
check:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5344
	CHECK_SYM table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5345
	{
5346 5347 5348 5349 5350 5351 5352 5353 5354
	  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
5355
	}
5356 5357 5358
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5359

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5360 5361
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
5362
	| mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5363 5364 5365

mi_check_types:
	mi_check_type {}
5366
	| mi_check_type mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5367 5368 5369 5370 5371 5372

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; }
5373 5374
	| CHANGED  { Lex->check_opt.flags|= T_CHECK_ONLY_CHANGED; }
        | FOR_SYM UPGRADE_SYM { Lex->check_opt.sql_flags|= TT_FOR_UPGRADE; };
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5375

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5376
optimize:
5377
	OPTIMIZE opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5378
	{
5379 5380
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5381
           lex->no_write_to_binlog= $2;
5382
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5383
	}
5384 5385 5386
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5387

5388 5389 5390
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
5391
	| LOCAL_SYM  { $$= 1; }
5392 5393
	;

5394 5395 5396
rename:
	RENAME table_or_tables
	{
5397
          Lex->sql_command= SQLCOM_RENAME_TABLE;
5398
	}
5399 5400
	table_to_table_list
	{}
5401 5402 5403 5404 5405 5406 5407
	| RENAME DATABASE
          {
            Lex->db_list.empty();
            Lex->sql_command= SQLCOM_RENAME_DB;
          }
          db_to_db
          {}
5408 5409 5410 5411
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
5412
	;
5413

5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426
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;
          }
        ;

5427 5428
table_to_table_list:
	table_to_table
5429
	| table_to_table_list ',' table_to_table;
5430 5431 5432

table_to_table:
	table_ident TO_SYM table_ident
5433
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5434
	  LEX *lex=Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5435
	  SELECT_LEX *sl= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5436 5437 5438 5439
	  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))
5440
	    YYABORT;
5441
	};
5442

5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453
db_to_db:
	ident TO_SYM ident
	{
	  LEX *lex=Lex;
          if (Lex->db_list.push_back((LEX_STRING*)
                                     sql_memdup(&$1, sizeof(LEX_STRING))) ||
              Lex->db_list.push_back((LEX_STRING*)
                                     sql_memdup(&$3, sizeof(LEX_STRING))))
              YYABORT;
	};

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5454
keycache:
5455
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5456 5457
        {
          LEX *lex=Lex;
5458
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
5459
	  lex->ident= $5;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5460 5461 5462 5463 5464 5465 5466 5467
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
5468
        table_ident cache_keys_spec
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5469 5470 5471 5472 5473 5474
        {
          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(),
5475
                                      (List<String> *)0))
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5476 5477 5478 5479
            YYABORT;
        }
        ;

5480 5481 5482
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
5483
	;
5484

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5485
preload:
5486
	LOAD INDEX_SYM INTO CACHE_SYM
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499
	{
	  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
5500
	table_ident cache_keys_spec opt_ignore_leaves
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5501 5502 5503
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5504 5505
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5506 5507 5508 5509 5510 5511
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

5512
cache_keys_spec:
5513
        { Select->interval_list.empty(); }
5514 5515 5516 5517 5518 5519 5520
        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
5521

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5522
cache_key_list_or_empty:
5523
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
5524
	| opt_key_or_index '(' key_usage_list2 ')'
5525 5526 5527 5528
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5529 5530
	;

5531
opt_ignore_leaves:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5532 5533 5534 5535 5536
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5537
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5538
  Select : retrieve data from table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5539 5540 5541 5542
*/


select:
5543 5544 5545 5546
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5547 5548
	}
	;
5549

5550
/* Need select_init2 for subselects. */
5551
select_init:
5552
	SELECT_SYM select_init2
5553
	|
5554 5555 5556 5557
	'(' select_paren ')' union_opt;

select_paren:
	SELECT_SYM select_part2
5558
	  {
5559
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5560
            SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5561
	    if (sel->set_braces(1))
5562
	    {
5563
	      yyerror(ER(ER_SYNTAX_ERROR));
5564 5565
	      YYABORT;
	    }
5566 5567 5568 5569 5570 5571 5572 5573
            if (sel->linkage == UNION_TYPE &&
                !sel->master_unit()->first_select()->braces &&
                sel->master_unit()->first_select()->linkage ==
                UNION_TYPE)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
5574
            /* select in braces, can't contain global parameters */
5575 5576 5577
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
5578 5579
          }
	| '(' select_paren ')';
5580

5581 5582
select_init2:
	select_part2
5583
        {
5584
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5585
          SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5586
          if (lex->current_select->set_braces(0))
5587
	  {
5588
	    yyerror(ER(ER_SYNTAX_ERROR));
5589 5590
	    YYABORT;
	  }
5591 5592
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5593
	  {
5594
	    yyerror(ER(ER_SYNTAX_ERROR));
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5595 5596
	    YYABORT;
	  }
5597
	}
5598
	union_clause
5599 5600
	;

5601
select_part2:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5602
	{
5603 5604
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
5605 5606
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
5607
	  lex->current_select->parsing_place= SELECT_LIST;
5608 5609 5610
	}
	select_options select_item_list
	{
5611
	  Select->parsing_place= NO_MATTER;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5612
	}
5613
	select_into select_lock_type;
5614

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5615
select_into:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5616
	opt_order_clause opt_limit_clause {}
5617
        | into
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5618
	| select_from
5619 5620
	| into select_from
	| select_from into;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5621 5622

select_from:
5623 5624
	  FROM join_table_list where_clause group_clause having_clause
	       opt_order_clause opt_limit_clause procedure_clause
5625
        | FROM DUAL_SYM where_clause opt_limit_clause
timour@mysql.com's avatar
timour@mysql.com committed
5626 5627 5628 5629
          /* 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 :) */
5630
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5631 5632 5633

select_options:
	/* empty*/
monty@mysql.com's avatar
monty@mysql.com committed
5634 5635
	| select_option_list
	  {
joreland@mysql.com's avatar
joreland@mysql.com committed
5636
	    if (Select->options & SELECT_DISTINCT && Select->options & SELECT_ALL)
monty@mysql.com's avatar
monty@mysql.com committed
5637
	    {
monty@mysql.com's avatar
monty@mysql.com committed
5638
	      my_error(ER_WRONG_USAGE, MYF(0), "ALL", "DISTINCT");
monty@mysql.com's avatar
monty@mysql.com committed
5639 5640 5641
              YYABORT;
	    }
          }
monty@mysql.com's avatar
monty@mysql.com committed
5642
	  ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5643 5644 5645

select_option_list:
	select_option_list select_option
5646
	| select_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5647 5648

select_option:
5649
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
5650 5651 5652 5653 5654 5655
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
monty@mysql.com's avatar
monty@mysql.com committed
5656
	| DISTINCT         { Select->options|= SELECT_DISTINCT; }
5657 5658
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670
	| 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;
	  }
5671 5672 5673 5674 5675 5676
	| SQL_NO_CACHE_SYM
          {
            Lex->safe_to_cache_query=0;
	    Lex->select_lex.options&= ~OPTION_TO_QUERY_CACHE;
            Lex->select_lex.sql_cache= SELECT_LEX::SQL_NO_CACHE;
          }
5677 5678
	| SQL_CACHE_SYM
	  {
5679 5680 5681 5682 5683 5684 5685
            /* Honor this flag only if SQL_NO_CACHE wasn't specified. */
            if (Lex->select_lex.sql_cache != SELECT_LEX::SQL_NO_CACHE)
            {
              Lex->safe_to_cache_query=1;
	      Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
              Lex->select_lex.sql_cache= SELECT_LEX::SQL_CACHE;
            }
5686
	  }
joreland@mysql.com's avatar
joreland@mysql.com committed
5687
	| ALL		    { Select->options|= SELECT_ALL; }
5688
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5689

monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5690 5691 5692
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
5693 5694
	  {
	    LEX *lex=Lex;
5695
	    lex->current_select->set_lock_for_tables(TL_WRITE);
5696
	    lex->safe_to_cache_query=0;
5697
	  }
5698
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
5699 5700
	  {
	    LEX *lex=Lex;
5701 5702
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
5703
	    lex->safe_to_cache_query=0;
5704 5705
	  }
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5706

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5707 5708 5709 5710 5711
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
5712
	    THD *thd= YYTHD;
5713 5714 5715 5716
	    if (add_item_to_list(thd,
                                 new Item_field(&thd->lex->current_select->
                                                context,
                                                NULL, NULL, "*")))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5717
	      YYABORT;
5718
	    (thd->lex->current_select->with_wild)++;
5719
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5720 5721 5722 5723 5724


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5725
	    if (add_item_to_list(YYTHD, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5726 5727
	      YYABORT;
	    if ($4.str)
5728 5729
            {
              $2->is_autogenerated_name= FALSE;
5730
	      $2->set_name($4.str, $4.length, system_charset_info);
5731
            }
5732 5733 5734 5735 5736 5737
	    else if (!$2->name) {
	      char *str = $1;
	      if (str[-1] == '`')
	        str--;
	      $2->set_name(str,(uint) ($3 - str), YYTHD->charset());
	    }
5738
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5739 5740

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

remember_end:
5744
	{ $$=(char*) Lex->tok_end; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5745 5746 5747

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

select_alias:
5751
	/* empty */		{ $$=null_lex_str;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5752 5753 5754 5755 5756
	| 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
5757 5758 5759

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5762
/* all possible expressions */
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
5763
expr:	
5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785
	  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); }
        ;
5786 5787

bool_term:
5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810
	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); }
        ;
5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827

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); }
5828 5829 5830
	| bool_pri EQUAL_SYM predicate	{ $$= new Item_func_equal($1,$3); }
	| bool_pri comp_op predicate %prec EQ
	  { $$= (*$2)(0)->create($1,$3); }
5831 5832
	| bool_pri comp_op all_or_any '(' subselect ')' %prec EQ
	  { $$= all_any_subquery_creator($1, $2, $3, $5); }
5833 5834 5835
	| predicate ;

predicate:
5836 5837 5838 5839 5840 5841 5842 5843 5844
        bit_expr IN_SYM '(' subselect ')'
	  { $$= new Item_in_subselect($1, $4); }
	| bit_expr not IN_SYM '(' subselect ')'
          { $$= negate_expression(YYTHD, new Item_in_subselect($1, $5)); }
        | bit_expr IN_SYM '(' expr ')'
          {
              $$= new Item_func_eq($1, $4);
          }
	| bit_expr IN_SYM '(' expr ',' expr_list ')'
5845
	  { 
5846 5847 5848
              $6->push_front($4);
              $6->push_front($1);
              $$= new Item_func_in(*$6);
5849
          }
5850
        | bit_expr not IN_SYM '(' expr ')'
5851
          {
5852 5853 5854 5855 5856 5857 5858
              $$= new Item_func_ne($1, $5);
          }
	| bit_expr not IN_SYM '(' expr ',' expr_list ')'
          {
              $7->push_front($5);
              $7->push_front($1);
              Item_func_in *item = new Item_func_in(*$7);
5859 5860
              item->negate();
              $$= item;
5861
          }
5862 5863 5864
	| 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
5865 5866 5867 5868 5869
    {
      Item_func_between *item= new Item_func_between($1,$4,$6);
      item->negate();
      $$= item;
    }
5870 5871 5872 5873
	| 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
5874
          { $$= new Item_func_like($1,$3,$4,Lex->escape_used); }
5875
	| bit_expr not LIKE simple_expr opt_escape
5876
          { $$= new Item_func_not(new Item_func_like($1,$4,$5, Lex->escape_used)); }
5877 5878
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
5879
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921
	| 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
5922

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934
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; }
        ;

5935
interval_expr:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5936
         INTERVAL_SYM expr { $$=$2; }
5937 5938
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5939 5940
simple_expr:
	simple_ident
5941
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5942
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5943 5944 5945
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
5946
                                                            YYTHD->charset()));
5947
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5948
	| literal
5949
	| param_marker
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5950
	| '@' ident_or_text SET_VAR expr
5951 5952
	  {
	    $$= new Item_func_set_user_var($2,$4);
5953 5954 5955
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5956
	  }
5957
	| '@' ident_or_text
5958 5959
	  {
	    $$= new Item_func_get_user_var($2);
5960 5961 5962
	    LEX *lex= Lex;
	    lex->uncacheable(UNCACHEABLE_RAND);
	    lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5963
	  }
5964
	| '@' '@' opt_var_ident_type ident_or_text opt_component
5965
	  {
5966 5967 5968 5969 5970 5971

            if ($4.str && $5.str && check_reserved_words(&$4))
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
5972
	    if (!($$= get_system_var(YYTHD, $3, $4, $5)))
5973
	      YYABORT;
5974
	    Lex->variables_used= 1;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5975
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5976
	| sum_expr
5977 5978 5979 5980 5981 5982
	| 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); }
5983 5984 5985 5986
	| '(' subselect ')'   
          { 
            $$= new Item_singlerow_subselect($2); 
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5987
	| '(' expr ')'		{ $$= $2; }
5988 5989 5990 5991 5992
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
5993
	| ROW_SYM '(' expr ',' expr_list ')'
5994
	  {
5995 5996
	    $5->push_front($3);
	    $$= new Item_row(*$5);
5997
	  }
5998 5999 6000 6001
	| EXISTS '(' subselect ')' 
          {
            $$= new Item_exists_subselect($3); 
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6002
	| '{' ident expr '}'	{ $$= $3; }
6003
        | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
6004
          { $2->push_front($5);
6005 6006
            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
6007
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
6008
	| BINARY simple_expr %prec NEG
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6009
	  {
6010
            $$= 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
6011
	  }
6012
	| CAST_SYM '(' expr AS cast_type ')'
6013
	  {
6014
            LEX *lex= Lex;
6015
	    $$= create_func_cast($3, $5,
6016 6017 6018
                                 lex->length ? atoi(lex->length) : -1,
                                 lex->dec ? atoi(lex->dec) : 0,
                                 lex->charset);
6019 6020
            if (!$$)
              YYABORT;
6021
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6022
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
6023
	  { $$= new Item_func_case(* $4, $2, $5 ); }
6024
	| CONVERT_SYM '(' expr ',' cast_type ')'
6025 6026 6027
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
6028
                                 Lex->dec ? atoi(Lex->dec) : 0,
6029
				 Lex->charset);
6030 6031
            if (!$$)
              YYABORT;
6032
	  }
6033 6034
	| 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
6035
	| DEFAULT '(' simple_ident ')'
6036 6037 6038 6039 6040
	  {
	    if ($3->is_splocal())
	    {
	      Item_splocal *il= static_cast<Item_splocal *>($3);

6041
	      my_error(ER_WRONG_COLUMN_NAME, MYF(0), il->my_name()->str);
6042 6043
	      YYABORT;
	    }
6044
	    $$= new Item_default_value(Lex->current_context(), $3);
6045
	  }
6046
	| VALUES '(' simple_ident_nospvar ')'
6047
	  { $$= new Item_insert_value(Lex->current_context(), $3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6048
	| FUNC_ARG0 '(' ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
6049 6050 6051
	  {
	    if (!$1.symbol->create_func)
	    {
6052 6053 6054
              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
6055 6056 6057 6058
	      YYABORT;
	    }
	    $$= ((Item*(*)(void))($1.symbol->create_func))();
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6059
	| FUNC_ARG1 '(' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
6060 6061 6062
	  {
	    if (!$1.symbol->create_func)
	    {
6063 6064 6065
              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
6066 6067 6068 6069
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6070
	| FUNC_ARG2 '(' expr ',' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
6071 6072 6073
	  {
	    if (!$1.symbol->create_func)
	    {
6074 6075 6076
	      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
6077 6078 6079 6080
	      YYABORT;
	    }
	    $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6081
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
6082 6083 6084
	  {
	    if (!$1.symbol->create_func)
	    {
6085 6086 6087
              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
6088 6089 6090 6091
	      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
6092 6093 6094 6095
	| 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); }
6096 6097
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6098 6099 6100 6101 6102 6103
	| 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); }
6104 6105
	| CHAR_SYM '(' expr_list USING charset_name ')'
	  { $$= new Item_func_char(*$3, $5); }
6106 6107
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6108 6109
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
6110 6111
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6112 6113 6114
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
6115
	  { $5->push_front($3); $$= new Item_func_concat_ws(*$5); }
6116 6117
	| CONVERT_TZ_SYM '(' expr ',' expr ',' expr ')'
	  {
6118 6119
            if (Lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
6120 6121
	    $$= new Item_func_convert_tz($3, $5, $7);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6122
	| CURDATE optional_braces
6123
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6124
	| CURTIME optional_braces
6125
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6126
	| CURTIME '(' expr ')'
6127
	  {
6128
	    $$= new Item_func_curtime_local($3);
6129
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6130
	  }
6131
	| CURRENT_USER optional_braces
6132 6133 6134 6135
          {
            $$= new Item_func_current_user(Lex->current_context());
            Lex->safe_to_cache_query= 0;
          }
6136 6137 6138 6139
	| 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
6140
	| DATABASE '(' ')'
6141
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6142
	    $$= new Item_func_database();
6143
            Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6144
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6145
	| DATE_SYM '(' expr ')'
6146
	  { $$= new Item_date_typecast($3); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6147 6148
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6149
	| ELT_FUNC '(' expr ',' expr_list ')'
6150
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6151 6152
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6153 6154 6155
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
6156
	    Lex->uncacheable(UNCACHEABLE_RAND);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6157
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6158
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
6159
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6160
	  { $$= new Item_func_decode($3,$5.str); }
6161
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6162
	 { $$= new Item_func_encode($3,$5.str); }
6163
	| DES_DECRYPT_SYM '(' expr ')'
6164
        { $$= new Item_func_des_decrypt($3); }
6165
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
6166
        { $$= new Item_func_des_decrypt($3,$5); }
6167
	| DES_ENCRYPT_SYM '(' expr ')'
6168
        { $$= new Item_func_des_encrypt($3); }
6169
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
6170
        { $$= new Item_func_des_encrypt($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182
	| 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
6183
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6184 6185
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
6186
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6187 6188 6189 6190 6191
	| geometry_function
	  {
#ifdef HAVE_SPATIAL
	    $$= $1;
#else
6192 6193
	    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
6194 6195 6196
	    YYABORT;
#endif
	  }
6197
	| GET_FORMAT '(' date_time_type  ',' expr ')'
6198
	  { $$= new Item_func_get_format($3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6199 6200 6201 6202 6203 6204
	| 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); }
6205
	| interval_expr interval '+' expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6206
	  /* we cannot put interval before - */
6207 6208 6209 6210 6211
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
6212
              yyerror(ER(ER_SYNTAX_ERROR));
6213 6214 6215 6216
              YYABORT;
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6217 6218
	| LAST_INSERT_ID '(' ')'
	  {
6219
	    $$= new Item_func_last_insert_id();
6220
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6221 6222 6223
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
6224
	    $$= new Item_func_last_insert_id($3);
6225
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6226 6227 6228 6229 6230 6231 6232
	  }
	| 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); }
6233
	| GREATEST_SYM '(' expr ',' expr_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6234 6235 6236
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
6237
	| LOG_SYM '(' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6238
	  { $$= new Item_func_log($3); }
6239
	| LOG_SYM '(' expr ',' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6240
	  { $$= new Item_func_log($3, $5); }
6241
	| MASTER_POS_WAIT '(' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6242
	  {
6243
	    $$= new Item_master_pos_wait($3, $5);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6244
	    Lex->safe_to_cache_query=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6245
		  }
6246
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6247
	  {
6248
	    $$= new Item_master_pos_wait($3, $5, $7);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6249
	    Lex->safe_to_cache_query=0;
6250
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6251 6252
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6253 6254
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
6255 6256
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6257 6258 6259
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
	| NOW_SYM optional_braces
6260
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6261
	| NOW_SYM '(' expr ')'
6262
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6263
	| PASSWORD '(' expr ')'
6264
	  {
6265 6266
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
6267 6268 6269 6270
	      (Item *) new Item_func_password($3);
	  }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new Item_func_old_password($3); }
6271
	| POSITION_SYM '(' bit_expr IN_SYM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6272
	  { $$ = new Item_func_locate($5,$3); }
6273 6274
	| QUARTER_SYM '(' expr ')'
	  { $$ = new Item_func_quarter($3); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6275
	| RAND '(' expr ')'
6276
	  { $$= new Item_func_rand($3); Lex->uncacheable(UNCACHEABLE_RAND);}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6277
	| RAND '(' ')'
6278
	  { $$= new Item_func_rand(); Lex->uncacheable(UNCACHEABLE_RAND);}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6279 6280 6281 6282 6283 6284 6285
	| 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); }
6286 6287 6288 6289 6290
	| 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
6291 6292 6293 6294
	| 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
6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306
	| 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); }
6307
	| SYSDATE optional_braces
6308 6309 6310 6311 6312 6313
          {
            if (global_system_variables.sysdate_is_now == 0)
              $$= new Item_func_sysdate_local();
            else $$= new Item_func_now_local();
            Lex->safe_to_cache_query=0;
          }
6314
	| SYSDATE '(' expr ')'
6315 6316 6317 6318 6319 6320
          {
            if (global_system_variables.sysdate_is_now == 0)
              $$= new Item_func_sysdate_local($3);
            else $$= new Item_func_now_local($3);
            Lex->safe_to_cache_query=0;
          }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6321
	| TIME_SYM '(' expr ')'
6322 6323 6324
	  { $$= 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
6325
	| TIMESTAMP '(' expr ',' expr ')'
6326
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
6327 6328 6329 6330
	| 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
6331
	| TRIM '(' expr ')'
6332 6333
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6334
	  { $$= new Item_func_ltrim($6,$4); }
6335
	| TRIM '(' TRAILING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6336
	  { $$= new Item_func_rtrim($6,$4); }
6337
	| TRIM '(' BOTH expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6338
	  { $$= new Item_func_trim($6,$4); }
6339 6340 6341 6342 6343 6344
	| 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
6345 6346
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
6347 6348
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
6349
	| ident '.' ident '(' udf_expr_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6350
	  {
6351
	    LEX *lex= Lex;
6352 6353 6354
	    sp_name *name= new sp_name($1, $3);

	    name->init_qname(YYTHD);
6355
	    sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
6356
	    if ($5)
6357
	      $$= new Item_func_sp(Lex->current_context(), name, *$5);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6358
	    else
6359
	      $$= new Item_func_sp(Lex->current_context(), name);
6360
	    lex->safe_to_cache_query=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6361
	  }
6362
	| IDENT_sys '(' 
6363 6364
          {
#ifdef HAVE_DLOPEN
6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383
            udf_func *udf= 0;
            if (using_udf_functions &&
                (udf= find_udf($1.str, $1.length)) &&
                udf->type == UDFTYPE_AGGREGATE)
            {
              LEX *lex= Lex;
              if (lex->current_select->inc_in_sum_expr())
              {
                yyerror(ER(ER_SYNTAX_ERROR));
                YYABORT;
              }
            }
            $<udf>$= udf;
#endif
          }
          udf_expr_list ')'
          {
#ifdef HAVE_DLOPEN
            udf_func *udf= $<udf>3;
6384
            SELECT_LEX *sel= Select;
6385

6386
            if (udf)
6387
            {
6388 6389 6390
              if (udf->type == UDFTYPE_AGGREGATE)
                Select->in_sum_expr--;

6391
              Lex->binlog_row_based_if_mixed= TRUE;
6392

6393 6394 6395 6396
              switch (udf->returns) {
              case STRING_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
6397 6398
                  if ($4 != NULL)
                    $$ = new Item_func_udf_str(udf, *$4);
6399 6400 6401 6402 6403
                  else
                    $$ = new Item_func_udf_str(udf);
                }
                else
                {
6404 6405
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_str(udf, *$4);
6406 6407 6408 6409 6410 6411 6412
                  else
                    $$ = new Item_sum_udf_str(udf);
                }
                break;
              case REAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
6413 6414
                  if ($4 != NULL)
                    $$ = new Item_func_udf_float(udf, *$4);
6415 6416 6417 6418 6419
                  else
                    $$ = new Item_func_udf_float(udf);
                }
                else
                {
6420 6421
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_float(udf, *$4);
6422 6423 6424 6425 6426 6427 6428
                  else
                    $$ = new Item_sum_udf_float(udf);
                }
                break;
              case INT_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
6429 6430
                  if ($4 != NULL)
                    $$ = new Item_func_udf_int(udf, *$4);
6431 6432 6433 6434 6435
                  else
                    $$ = new Item_func_udf_int(udf);
                }
                else
                {
6436 6437
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_int(udf, *$4);
6438 6439 6440 6441
                  else
                    $$ = new Item_sum_udf_int(udf);
                }
                break;
6442 6443 6444
              case DECIMAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
6445 6446
                  if ($4 != NULL)
                    $$ = new Item_func_udf_decimal(udf, *$4);
6447 6448 6449 6450 6451
                  else
                    $$ = new Item_func_udf_decimal(udf);
                }
                else
                {
6452 6453
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_decimal(udf, *$4);
6454 6455 6456 6457
                  else
                    $$ = new Item_sum_udf_decimal(udf);
                }
                break;
6458 6459 6460 6461 6462 6463 6464
              default:
                YYABORT;
              }
            }
            else
#endif /* HAVE_DLOPEN */
            {
6465
	      LEX *lex= Lex;
6466 6467 6468 6469 6470 6471 6472
              THD *thd= lex->thd;
              LEX_STRING db;
              if (thd->copy_db_to(&db.str, &db.length))
                YYABORT;
              sp_name *name= new sp_name(db, $1);
              if (name)
                name->init_qname(thd);
6473

6474
              sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
6475 6476
              if ($4)
                $$= new Item_func_sp(Lex->current_context(), name, *$4);
6477
              else
6478
                $$= new Item_func_sp(Lex->current_context(), name);
6479
	      lex->safe_to_cache_query=0;
6480 6481
	    }
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6482
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
6483
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6484 6485
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6486
	| UNIX_TIMESTAMP '(' ')'
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6487 6488
	  {
	    $$= new Item_func_unix_timestamp();
6489
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6490
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6491 6492 6493
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
6494
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
6495 6496 6497 6498 6499 6500
	| 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
6501
	| WEEK_SYM '(' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6502
	  {
6503
            $$= new Item_func_week($3,new Item_int((char*) "0",
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6504
				   YYTHD->variables.default_week_format,1));
6505
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6506 6507 6508 6509 6510 6511 6512 6513
	| 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
6514
	| BENCHMARK_SYM '(' ulong_num ',' expr ')'
6515
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6516
	    $$=new Item_func_benchmark($3,$5);
6517
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6518
	  }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6519
	| EXTRACT_SYM '(' interval FROM expr ')'
6520
	{ $$=new Item_extract( $3, $5); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6521

hf@deer.(none)'s avatar
hf@deer.(none) committed
6522
geometry_function:
serg@serg.mylan's avatar
serg@serg.mylan committed
6523 6524 6525
	  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
6526 6527 6528 6529 6530 6531 6532 6533 6534
	  { $$= 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,
6535 6536
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6537 6538
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
6539
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6540 6541
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
6542
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556
 	| 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,
6557
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6558 6559
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
6560
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572
	| 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,
6573
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6574 6575 6576 6577 6578 6579 6580 6581 6582 6583
 	| 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)); }
	;

6584
fulltext_options:
6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598
          opt_natural_language_mode opt_query_expansion
          { $$= $1 | $2; }
        | IN_SYM BOOLEAN_SYM MODE_SYM
          { $$= FT_BOOL; }
        ;

opt_natural_language_mode:
        /* nothing */                           { $$= FT_NL;  }
        | IN_SYM NATURAL LANGUAGE_SYM MODE_SYM  { $$= FT_NL; }
        ;

opt_query_expansion:
        /* nothing */                           { $$= 0;         }
        | WITH QUERY_SYM EXPANSION_SYM          { $$= FT_EXPAND; }
6599 6600
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6601
udf_expr_list:
6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626
	/* 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)
6627 6628
          {
            $2->is_autogenerated_name= FALSE;
6629
	    $2->set_name($4.str, $4.length, system_charset_info);
6630
          }
6631
	  else
6632
	    $2->set_name($1, (uint) ($3 - $1), YYTHD->charset());
6633 6634 6635
	  $$= $2;
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6636 6637 6638 6639

sum_expr:
	AVG_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_avg($3); }
6640 6641
	| AVG_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_avg_distinct($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6642 6643 6644 6645
	| BIT_AND  '(' in_sum_expr ')'
	  { $$=new Item_sum_and($3); }
	| BIT_OR  '(' in_sum_expr ')'
	  { $$=new Item_sum_or($3); }
6646 6647
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
6648
	| COUNT_SYM '(' opt_all '*' ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6649 6650 6651
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
6652
	| COUNT_SYM '(' DISTINCT
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6653
	  { Select->in_sum_expr++; }
6654
	   expr_list
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6655
	  { Select->in_sum_expr--; }
6656 6657
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6658 6659 6660 6661
	| 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); }
6662 6663 6664 6665 6666 6667 6668
/*
   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
6669 6670
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
6671 6672
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6673
	| STD_SYM '(' in_sum_expr ')'
6674
	  { $$=new Item_sum_std($3, 0); }
6675
	| VARIANCE_SYM '(' in_sum_expr ')'
6676 6677 6678 6679 6680
	  { $$=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
6681
	| SUM_SYM '(' in_sum_expr ')'
6682
	  { $$=new Item_sum_sum($3); }
6683 6684
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
monty@mysql.com's avatar
monty@mysql.com committed
6685 6686 6687 6688 6689
	| 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
6690
	  {
6691 6692
            SELECT_LEX *sel= Select;
	    sel->in_sum_expr--;
6693
	    $$=new Item_func_group_concat(Lex->current_context(), $3, $5,
6694
                                          sel->gorder_list, $7);
monty@mysql.com's avatar
monty@mysql.com committed
6695
	    $5->empty();
6696 6697 6698 6699 6700 6701 6702
	  };

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

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

6706 6707

opt_gorder_clause:
6708 6709
	  /* empty */
	  {
6710
            Select->gorder_list = NULL;
6711 6712 6713
	  }
	| order_clause
          {
6714 6715 6716
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
6717
				     sizeof(st_sql_list));
6718
	    select->order_list.empty();
6719
	  };
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6720

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6721 6722

in_sum_expr:
6723
	opt_all
6724 6725 6726 6727
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
6728
	    yyerror(ER(ER_SYNTAX_ERROR));
6729
	    YYABORT;
6730 6731
	  }
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6732 6733
	expr
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6734
	  Select->in_sum_expr--;
6735
	  $$= $3;
6736
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6737

6738
cast_type:
6739 6740
        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; }
6741
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; Lex->dec=0; }
6742 6743 6744 6745 6746 6747 6748 6749
        | 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; }
6750 6751
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6752
expr_list:
6753
	{ Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6754
	expr_list2
6755
	{ $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6756 6757

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

6761 6762
ident_list_arg:
          ident_list          { $$= $1; }
6763
        | '(' ident_list ')'  { $$= $2; };
6764

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6765
ident_list:
6766
        { Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6767
        ident_list2
6768
        { $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6769 6770

ident_list2:
6771
        simple_ident { Select->expr_list.head()->push_back($1); }
6772
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6773 6774 6775

opt_expr:
	/* empty */      { $$= NULL; }
6776
	| expr           { $$= $1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6777 6778 6779

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

when_list:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6783
        { Select->when_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6784
	when_list2
6785
	{ $$= Select->when_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6786 6787

when_list2:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6788
	expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6789
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6790
	    SELECT_LEX *sel=Select;
6791 6792
	    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
6793
	}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6794
	| when_list2 WHEN_SYM expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6795
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6796
	    SELECT_LEX *sel=Select;
6797 6798
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
6799
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6800

6801
/* Warning - may return NULL in case of incomplete SELECT */
6802 6803 6804
table_ref:
        table_factor            { $$=$1; }
        | join_table            { $$=$1; }
6805
          {
6806 6807 6808
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
6809
          }
6810 6811
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6812
join_table_list:
serg@serg.mylan's avatar
serg@serg.mylan committed
6813
	derived_table_list		{ YYERROR_UNLESS($$=$1); }
6814
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
6815

6816 6817
/* Warning - may return NULL in case of incomplete SELECT */
derived_table_list:
6818
        table_ref { $$=$1; }
6819 6820
        | derived_table_list ',' table_ref
          {
serg@serg.mylan's avatar
serg@serg.mylan committed
6821
            YYERROR_UNLESS($1 && ($$=$3));
6822
          }
6823 6824
        ;

6825 6826 6827 6828 6829 6830 6831
/*
  Notice that JOIN is a left-associative operation, and it must be parsed
  as such, that is, the parser must process first the left join operand
  then the right one. Such order of processing ensures that the parser
  produces correct join trees which is essential for semantic analysis
  and subsequent optimization phases.
*/
6832
join_table:
6833
/* INNER JOIN variants */
timour@mysql.com's avatar
timour@mysql.com committed
6834
        /*
6835 6836 6837
          Use %prec to evaluate production 'table_ref' before 'normal_join'
          so that [INNER | CROSS] JOIN is properly nested as other
          left-associative joins.
timour@mysql.com's avatar
timour@mysql.com committed
6838 6839
        */
        table_ref %prec TABLE_REF_PRIORITY normal_join table_ref
6840
          { YYERROR_UNLESS($1 && ($$=$3)); }
6841
	| table_ref STRAIGHT_JOIN table_factor
serg@serg.mylan's avatar
serg@serg.mylan committed
6842
	  { YYERROR_UNLESS($1 && ($$=$3)); $3->straight=1; }
6843 6844 6845 6846 6847
	| table_ref normal_join table_ref
          ON
          {
            YYERROR_UNLESS($1 && ($$=$3));
            /* Change the current name resolution context to a local context. */
6848
            if (push_new_name_resolution_context(YYTHD, $1, $3))
6849
              YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6850
            Select->parsing_place= IN_ON;
6851 6852 6853 6854 6855
          }
          expr
	  {
            add_join_on($3,$6);
            Lex->pop_context();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6856
            Select->parsing_place= NO_MATTER;
6857 6858 6859 6860 6861 6862
          }
        | table_ref STRAIGHT_JOIN table_factor
          ON
          {
            YYERROR_UNLESS($1 && ($$=$3));
            /* Change the current name resolution context to a local context. */
6863
            if (push_new_name_resolution_context(YYTHD, $1, $3))
6864
              YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6865
            Select->parsing_place= IN_ON;
6866 6867 6868 6869 6870 6871
          }
          expr
          {
            $3->straight=1;
            add_join_on($3,$6);
            Lex->pop_context();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6872
            Select->parsing_place= NO_MATTER;
6873
          }
6874
	| table_ref normal_join table_ref
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6875
	  USING
6876
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6877
	    SELECT_LEX *sel= Select;
serg@serg.mylan's avatar
serg@serg.mylan committed
6878
            YYERROR_UNLESS($1 && $3);
6879
	  }
6880
	  '(' using_list ')'
6881
          { add_join_natural($1,$3,$7); $$=$3; }
6882 6883 6884 6885 6886 6887 6888
	| table_ref NATURAL JOIN_SYM table_factor
	  {
            YYERROR_UNLESS($1 && ($$=$4));
            add_join_natural($1,$4,NULL);
          }

/* LEFT JOIN variants */
6889 6890 6891
	| table_ref LEFT opt_outer JOIN_SYM table_ref
          ON
          {
6892
            YYERROR_UNLESS($1 && $5);
6893
            /* Change the current name resolution context to a local context. */
6894
            if (push_new_name_resolution_context(YYTHD, $1, $5))
6895
              YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6896
            Select->parsing_place= IN_ON;
6897 6898 6899 6900 6901 6902 6903
          }
          expr
	  {
            add_join_on($5,$8);
            Lex->pop_context();
            $5->outer_join|=JOIN_TYPE_LEFT;
            $$=$5;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6904
            Select->parsing_place= NO_MATTER;
6905
          }
6906
	| table_ref LEFT opt_outer JOIN_SYM table_factor
6907
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6908
	    SELECT_LEX *sel= Select;
serg@serg.mylan's avatar
serg@serg.mylan committed
6909
            YYERROR_UNLESS($1 && $5);
6910
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6911
	  USING '(' using_list ')'
6912
          { add_join_natural($1,$5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
6913
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
6914
	  {
serg@serg.mylan's avatar
serg@serg.mylan committed
6915
            YYERROR_UNLESS($1 && $6);
6916
 	    add_join_natural($1,$6,NULL);
6917
	    $6->outer_join|=JOIN_TYPE_LEFT;
6918 6919
	    $$=$6;
	  }
6920 6921

/* RIGHT JOIN variants */
6922 6923 6924
	| table_ref RIGHT opt_outer JOIN_SYM table_ref
          ON
          {
6925
            YYERROR_UNLESS($1 && $5);
6926
            /* Change the current name resolution context to a local context. */
6927
            if (push_new_name_resolution_context(YYTHD, $1, $5))
6928
              YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6929
            Select->parsing_place= IN_ON;
6930 6931
          }
          expr
serg@serg.mylan's avatar
serg@serg.mylan committed
6932
          {
6933 6934 6935
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
6936 6937
            add_join_on($$, $8);
            Lex->pop_context();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6938
            Select->parsing_place= NO_MATTER;
6939 6940
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
6941
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6942
	    SELECT_LEX *sel= Select;
serg@serg.mylan's avatar
serg@serg.mylan committed
6943
            YYERROR_UNLESS($1 && $5);
6944
	  }
6945
	  USING '(' using_list ')'
serg@serg.mylan's avatar
serg@serg.mylan committed
6946
          {
6947 6948 6949
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
6950
            add_join_natural($$,$5,$9);
6951 6952
          }
	| table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor
6953
	  {
serg@serg.mylan's avatar
serg@serg.mylan committed
6954
            YYERROR_UNLESS($1 && $6);
6955
	    add_join_natural($6,$1,NULL);
6956 6957 6958
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
6959
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6960 6961

normal_join:
6962 6963 6964 6965
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6966

6967
/* Warning - may return NULL in case of incomplete SELECT */
6968
table_factor:
6969
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6970
	  SELECT_LEX *sel= Select;
6971
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6972
	  sel->table_join_options= 0;
6973
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6974
        table_ident opt_table_alias opt_key_definition
6975
	{
6976
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6977
	  SELECT_LEX *sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6978
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6979
					   sel->get_table_join_options(),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6980
					   lex->lock_option,
6981 6982
					   sel->get_use_index(),
					   sel->get_ignore_index())))
6983
	    YYABORT;
6984
          sel->add_joined_table($$);
6985
	}
6986 6987 6988 6989
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref
          ON
          {
            /* Change the current name resolution context to a local context. */
6990
            if (push_new_name_resolution_context(YYTHD, $3, $7))
6991
              YYABORT;
6992

6993 6994 6995
          }
          expr '}'
	  {
6996
	    LEX *lex= Lex;
6997 6998 6999 7000 7001
            YYERROR_UNLESS($3 && $7);
            add_join_on($7,$10);
            Lex->pop_context();
            $7->outer_join|=JOIN_TYPE_LEFT;
            $$=$7;
7002 7003
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
7004
          }
7005
	| select_derived_init get_select_lex select_derived2
7006
          {
7007
            LEX *lex= Lex;
7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021
            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
7022
              YYABORT;
7023
            $$= 0;
serg@serg.mylan's avatar
serg@serg.mylan committed
7024
            /* incomplete derived tables return NULL, we must be
7025
               nested in select_derived rule to be here. */
7026
          }
7027 7028 7029 7030 7031
	| '(' 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
7032
          if (!($3 || $6) && $2->embedding &&
7033
              !$2->embedding->nested_join->join_list.elements)
7034
          {
7035 7036 7037 7038 7039
            /* 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;
7040
          }
7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054
          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
7055

7056
	      YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
7057
            sel->add_joined_table($$);
7058
            lex->pop_context();
7059 7060 7061 7062 7063 7064
          }
	  else
          if ($4 || $6)
	  {
            /* simple nested joins cannot have aliases or unions */
            yyerror(ER(ER_SYNTAX_ERROR));
7065
	    YYABORT;
7066 7067 7068 7069 7070
	  }
          else
            $$= $3;
	}
        ;
7071

7072
/* handle contents of parentheses in join expression */
7073
select_derived:
7074
	  get_select_lex
7075
	  {
7076 7077 7078 7079 7080 7081 7082 7083 7084
            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
7085

7086 7087 7088 7089
            if (!($$= $1->end_nested_join(lex->thd)) && $3)
              YYABORT;
            if (!$3 && $$)
            {
7090 7091
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
7092 7093
            }
          }
monty@mysql.com's avatar
monty@mysql.com committed
7094
 	;
7095

monty@mysql.com's avatar
monty@mysql.com committed
7096
select_derived2:
7097
        {
7098
	  LEX *lex= Lex;
7099
	  lex->derived_tables|= DERIVED_SUBQUERY;
7100
          if (!lex->expr_allows_subselect)
7101 7102
	  {
	    yyerror(ER(ER_SYNTAX_ERROR));
7103 7104
	    YYABORT;
	  }
7105
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
7106
              mysql_new_select(lex, 1))
7107 7108
	    YYABORT;
	  mysql_init_select(lex);
7109
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
7110
	  lex->current_select->parsing_place= SELECT_LIST;
7111 7112 7113
	}
        select_options select_item_list
	{
7114
	  Select->parsing_place= NO_MATTER;
7115
	}
7116
	opt_select_from
7117
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7118

7119 7120 7121 7122 7123 7124 7125 7126
get_select_lex:
	/* Empty */ { $$= Select; }
        ;

select_derived_init:
          SELECT_SYM
          {
            LEX *lex= Lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
7127
            SELECT_LEX *sel= lex->current_select;
7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141
            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
7142 7143
opt_outer:
	/* empty */	{}
7144
	| OUTER		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7145 7146 7147 7148

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
7149
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7150
	    SELECT_LEX *sel= Select;
7151 7152 7153
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7154 7155
	| FORCE_SYM key_usage_list
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7156
	    SELECT_LEX *sel= Select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7157 7158 7159 7160
	    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
7161
	| IGNORE_SYM key_usage_list
7162
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7163
	    SELECT_LEX *sel= Select;
7164 7165
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
7166
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7167 7168

key_usage_list:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7169
	key_or_index { Select->interval_list.empty(); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7170
        '(' key_list_or_empty ')'
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7171
        { $$= &Select->interval_list; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7172 7173 7174 7175 7176 7177
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7178 7179 7180

key_usage_list2:
	key_usage_list2 ',' ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7181
        { Select->
7182
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $3.str, $3.length,
7183
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7184
	| ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7185
        { Select->
7186
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $1.str, $1.length,
7187
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7188
	| PRIMARY_SYM
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7189
        { Select->
7190
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
7191
				    system_charset_info)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7192 7193 7194

using_list:
	ident
7195
	  {
7196
            if (!($$= new List<String>))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7197
	      YYABORT;
7198 7199 7200
            $$->push_back(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
7201 7202 7203
	  }
	| using_list ',' ident
	  {
7204 7205 7206 7207
            $1->push_back(new (YYTHD->mem_root)
                              String((const char *) $3.str, $3.length,
                                      system_charset_info));
            $$= $1;
7208
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7209 7210

interval:
7211 7212
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
7213
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7214 7215
	| 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
7216
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7217 7218
	| 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
7219 7220
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7221
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
7222 7223 7224 7225 7226 7227 7228 7229
	| 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
7230 7231
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
7232
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7233
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
7234 7235
	| YEAR_SYM		{ $$=INTERVAL_YEAR; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7236

7237
date_time_type:
7238 7239 7240 7241 7242
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
7243

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7244 7245 7246
table_alias:
	/* empty */
	| AS
7247
	| EQ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7248 7249 7250 7251

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

7254 7255 7256 7257
opt_all:
	/* empty */
	| ALL
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7258 7259

where_clause:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7260
	/* empty */  { Select->where= 0; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7261 7262 7263 7264 7265
	| WHERE
          {
            Select->parsing_place= IN_WHERE;
          }
          expr
7266
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7267 7268 7269 7270 7271
            SELECT_LEX *select= Select;
	    select->where= $3;
            select->parsing_place= NO_MATTER;
	    if ($3)
	      $3->top_level_item();
7272
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7273
 	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7274 7275 7276

having_clause:
	/* empty */
7277 7278
	| HAVING
	  {
7279
	    Select->parsing_place= IN_HAVING;
7280 7281
          }
	  expr
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7282
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7283
	    SELECT_LEX *sel= Select;
7284
	    sel->having= $3;
7285
	    sel->parsing_place= NO_MATTER;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7286 7287 7288
	    if ($3)
	      $3->top_level_item();
	  }
7289
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7290 7291

opt_escape:
7292 7293 7294 7295 7296
	ESCAPE_SYM simple_expr 
          {
            Lex->escape_used= TRUE;
            $$= $2;
          }
7297
	| /* empty */
7298
          {
7299
            Lex->escape_used= FALSE;
7300 7301 7302
            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
7303 7304
          }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7305 7306 7307


/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7308
   group by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7309 7310 7311 7312
*/

group_clause:
	/* empty */
7313
	| GROUP BY group_list olap_opt;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7314 7315

group_list:
7316
	group_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7317
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
7318
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7319
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7320

7321 7322
olap_opt:
	/* empty */ {}
7323
	| WITH CUBE_SYM
7324
          {
7325
	    LEX *lex=Lex;
7326 7327
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
7328
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
7329 7330 7331
		       "global union parameters");
	      YYABORT;
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7332
	    lex->current_select->olap= CUBE_TYPE;
7333
	    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
7334
	    YYABORT;	/* To be deleted in 5.1 */
7335
	  }
7336
	| WITH ROLLUP_SYM
7337
          {
7338 7339 7340
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
7341
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
7342 7343 7344
		       "global union parameters");
	      YYABORT;
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7345
	    lex->current_select->olap= ROLLUP_TYPE;
7346
	  }
7347
	;
7348

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7349
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7350
   Order by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7351 7352
*/

7353
opt_order_clause:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7354
	/* empty */
7355
	| order_clause;
7356 7357

order_clause:
7358 7359
	ORDER_SYM BY
        {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7360
	  LEX *lex=Lex;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
7361 7362 7363 7364
          SELECT_LEX *sel= lex->current_select;
          SELECT_LEX_UNIT *unit= sel-> master_unit();
	  if (sel->linkage != GLOBAL_OPTIONS_TYPE &&
	      sel->olap != UNSPECIFIED_OLAP_TYPE)
7365
	  {
7366 7367
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
7368 7369
	    YYABORT;
	  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382
          if (lex->sql_command != SQLCOM_ALTER_TABLE && !unit->fake_select_lex)
          {
            /*
              A query of the of the form (SELECT ...) ORDER BY order_list is
              executed in the same way as the query
              SELECT ... ORDER BY order_list
              unless the SELECT construct contains ORDER BY or LIMIT clauses.
              Otherwise we create a fake SELECT_LEX if it has not been created
              yet.
            */
            SELECT_LEX *first_sl= unit->first_select();
            if (!first_sl->next_select() &&
                (first_sl->order_list.elements || 
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
7383
                 first_sl->select_limit) &&            
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
7384 7385 7386
                unit->add_fake_select_lex(lex->thd))
              YYABORT;
          }
7387
	} order_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7388 7389 7390

order_list:
	order_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7391
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7392
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7393
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7394 7395 7396

order_dir:
	/* empty */ { $$ =  1; }
7397
	| ASC  { $$ =1; }
7398
	| DESC { $$ =0; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7399 7400


7401 7402 7403
opt_limit_clause_init:
	/* empty */
	{
7404 7405
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
7406 7407
          sel->offset_limit= 0;
          sel->select_limit= 0;
7408
	}
7409 7410 7411
	| limit_clause {}
	;

7412 7413 7414 7415 7416
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

7417
limit_clause:
7418
	LIMIT limit_options {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7419
	;
7420 7421

limit_options:
7422
	limit_option
7423
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7424
            SELECT_LEX *sel= Select;
7425
            sel->select_limit= $1;
7426
            sel->offset_limit= 0;
7427
	    sel->explicit_limit= 1;
7428
	  }
7429
	| limit_option ',' limit_option
7430
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7431
	    SELECT_LEX *sel= Select;
7432 7433
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
7434
	    sel->explicit_limit= 1;
7435
	  }
7436
	| limit_option OFFSET_SYM limit_option
7437
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7438
	    SELECT_LEX *sel= Select;
7439 7440
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
7441
	    sel->explicit_limit= 1;
7442 7443
	  }
	;
7444 7445 7446 7447 7448
limit_option:
        param_marker
        | ULONGLONG_NUM { $$= new Item_uint($1.str, $1.length); }
        | LONG_NUM     { $$= new Item_uint($1.str, $1.length); }
        | NUM           { $$= new Item_uint($1.str, $1.length); }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
7449
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7450 7451 7452 7453

delete_limit_clause:
	/* empty */
	{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7454
	  LEX *lex=Lex;
7455
	  lex->current_select->select_limit= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7456
	}
7457
	| LIMIT limit_option
7458 7459
	{
	  SELECT_LEX *sel= Select;
7460
	  sel->select_limit= $2;
7461 7462
	  sel->explicit_limit= 1;
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7463

serg@serg.mylan's avatar
serg@serg.mylan committed
7464 7465
ulong_num:
          NUM           { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
7466
	| HEX_NUM       { $$= (ulong) strtol($1.str, (char**) 0, 16); }
monty@mysql.com's avatar
monty@mysql.com committed
7467 7468
	| 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
7469
        | DECIMAL_NUM   { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
monty@mysql.com's avatar
monty@mysql.com committed
7470
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
7471 7472 7473 7474 7475 7476 7477 7478 7479
        ;

real_ulong_num:
          NUM           { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| HEX_NUM       { $$= (ulong) strtol($1.str, (char**) 0, 16); }
	| LONG_NUM      { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	| ULONGLONG_NUM { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
        | dec_num_error { YYABORT; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7480

7481
ulonglong_num:
monty@mysql.com's avatar
monty@mysql.com committed
7482 7483 7484
	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); }
7485
        | DECIMAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
monty@mysql.com's avatar
monty@mysql.com committed
7486 7487
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7488

7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505
real_ulonglong_num:
	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); }
        | dec_num_error { YYABORT; }
        ;

dec_num_error:
        dec_num
        { yyerror(ER(ER_ONLY_INTEGERS_ALLOWED)); }
        ;

dec_num:
        DECIMAL_NUM
	| FLOAT_NUM
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7506 7507 7508 7509 7510
procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
7511 7512
	    if (&lex->select_lex != lex->current_select)
	    {
7513
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
7514 7515
	      YYABORT;
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7516 7517 7518
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
7519 7520 7521 7522
	    if (add_proc_to_list(lex->thd, new Item_field(&lex->
                                                          current_select->
                                                          context,
                                                          NULL,NULL,$2.str)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7523
	      YYABORT;
7524
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7525
	  }
7526
	  '(' procedure_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7527 7528 7529 7530


procedure_list:
	/* empty */ {}
7531
	| procedure_list2 {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7532 7533 7534

procedure_list2:
	procedure_list2 ',' procedure_item
7535
	| procedure_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7536 7537 7538 7539

procedure_item:
	  remember_name expr
	  {
7540 7541
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7542 7543
	      YYABORT;
	    if (!$2->name)
7544 7545
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1),
                           YYTHD->charset());
7546 7547 7548 7549 7550 7551
	  }
          ;


select_var_list_init:
	   {
7552 7553
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
7554 7555
	        YYABORT;
	   }
7556
	   select_var_list
7557
	   {}
7558
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7559

Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7560
select_var_list:
7561 7562 7563 7564
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

7565 7566 7567 7568 7569 7570 7571
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
7572 7573 7574 7575 7576
               /*
                 The parser won't create select_result instance only
	         if it's an EXPLAIN.
               */
               DBUG_ASSERT(lex->describe);
7577 7578
	   }
           | ident_or_text
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7579
           {
7580
             LEX *lex=Lex;
7581
	     sp_variable_t *t;
7582

7583
	     if (!lex->spcont || !(t=lex->spcont->find_variable(&$1)))
7584
	     {
7585
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7586
	       YYABORT;
7587
	     }
7588 7589
	     if (lex->result)
             {
7590 7591 7592
               my_var *var;
	       ((select_dumpvar *)lex->result)->
                 var_list.push_back(var= new my_var($1,1,t->offset,t->type));
7593
#ifndef DBUG_OFF
7594
	       if (var)
7595
		 var->sp= lex->sphead;
7596
#endif
7597 7598 7599 7600 7601 7602 7603 7604
             }
	     else
	     {
               /*
                 The parser won't create select_result instance only
	         if it's an EXPLAIN.
               */
               DBUG_ASSERT(lex->describe);
7605
	     }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7606
	   }
7607
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7608

7609
into:
7610
        INTO OUTFILE TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7611
	{
7612
          LEX *lex= Lex;
7613 7614 7615 7616
          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
7617 7618
	}
	opt_field_term opt_line_term
7619
	| INTO DUMPFILE TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7620
	{
7621
	  LEX *lex=Lex;
7622 7623
	  if (!lex->describe)
	  {
7624
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
7625 7626 7627 7628 7629
	    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
7630
	}
7631
        | INTO select_var_list_init
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7632
	{
7633
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
7634 7635
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7636

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7637 7638 7639
/*
  DO statement
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7640

7641
do:	DO_SYM
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7642 7643 7644
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
7645 7646 7647 7648 7649
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7650
	}
7651 7652
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7653
/*
7654
  Drop : delete tables or index or user
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7655 7656 7657
*/

drop:
serg@serg.mylan's avatar
serg@serg.mylan committed
7658
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7659
	{
7660 7661
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
7662 7663
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7664
	}
7665
	| DROP INDEX_SYM ident ON table_ident {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7666
	  {
7667 7668
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
7669 7670 7671
	     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
7672 7673
	     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
7674 7675 7676 7677
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
7678 7679 7680 7681
	    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
7682
	 }
7683
	| DROP FUNCTION_SYM if_exists sp_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7684
	  {
7685
	    LEX *lex=Lex;
7686 7687
	    if (lex->sphead)
	    {
7688
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
7689 7690
	      YYABORT;
	    }
7691
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
7692 7693 7694 7695 7696 7697 7698 7699
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
	  }
	| DROP PROCEDURE if_exists sp_name
	  {
	    LEX *lex=Lex;
	    if (lex->sphead)
	    {
7700
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
7701 7702 7703 7704 7705
	      YYABORT;
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
7706
	  }
7707
	| DROP USER clear_privileges user_list
7708
	  {
7709 7710
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
7711 7712
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
7713
	    LEX *lex= Lex;
7714 7715 7716
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
7717 7718
        | DROP EVENT_SYM if_exists sp_name
          {
7719 7720 7721
            Lex->drop_if_exists= $3;
            Lex->spname= $4;
            Lex->sql_command = SQLCOM_DROP_EVENT;
7722
          }
7723
        | DROP TRIGGER_SYM sp_name
7724 7725 7726
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_DROP_TRIGGER;
7727
            lex->spname= $3;
7728 7729 7730 7731 7732 7733 7734 7735 7736 7737
	  }
        | DROP TABLESPACE tablespace_name opt_ts_engine opt_ts_wait
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= DROP_TABLESPACE;
          }
        | DROP LOGFILE_SYM GROUP logfile_group_name opt_ts_engine opt_ts_wait
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= DROP_LOGFILE_GROUP;
7738 7739
          }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7740 7741

table_list:
7742
	table_name
7743
	| table_list ',' table_name;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7744

7745
table_name:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7746
	table_ident
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7747 7748 7749 7750 7751
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7752 7753

if_exists:
7754
	/* empty */ { $$= 0; }
7755 7756
	| IF EXISTS { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7757

7758 7759 7760 7761
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7762 7763 7764 7765 7766
/*
** Insert : add new data to table
*/

insert:
7767 7768 7769
	INSERT
	{
	  LEX *lex= Lex;
7770 7771
	  lex->sql_command= SQLCOM_INSERT;
	  lex->duplicates= DUP_ERROR; 
7772
	  mysql_init_select(lex);
7773 7774 7775
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
	} insert_lock_option
7776
	opt_ignore insert2
7777
	{
7778
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7779
	  Lex->current_select= &Lex->select_lex;
7780
	}
7781
	insert_field_spec opt_insert_update
7782
	{}
7783
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7784 7785

replace:
7786 7787
	REPLACE
	{
7788
	  LEX *lex=Lex;
7789 7790
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
7791
	  mysql_init_select(lex);
7792
	}
7793 7794
	replace_lock_option insert2
	{
7795
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7796
	  Lex->current_select= &Lex->select_lex;
7797 7798
	}
	insert_field_spec
7799
	{}
7800
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7801 7802

insert_lock_option:
7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815
	/* empty */
          {
#ifdef HAVE_QUERY_CACHE
            /*
              If it is SP we do not allow insert optimisation whan result of
              insert visible only after the table unlocking but everyone can
              read table.
            */
            $$= (Lex->sphead ? TL_WRITE :TL_WRITE_CONCURRENT_INSERT);
#else
            $$= TL_WRITE_CONCURRENT_INSERT;
#endif
          }
7816 7817 7818
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
7819
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7820 7821

replace_lock_option:
7822 7823
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7824 7825 7826

insert2:
	INTO insert_table {}
7827
	| insert_table {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7828 7829

insert_table:
7830
	table_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7831
	{
7832 7833 7834 7835
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
7836
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7837 7838

insert_field_spec:
serg@serg.mylan's avatar
serg@serg.mylan committed
7839 7840 7841
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7842 7843
	| SET
	  {
7844 7845 7846
	    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
7847 7848
	      YYABORT;
	   }
7849
	   ident_eq_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7850 7851 7852

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
7853
	| insert_ident		{ Lex->field_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7854 7855 7856

insert_values:
	VALUES	values_list  {}
7857
	| VALUE_SYM values_list  {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7858 7859
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
7860
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7861 7862 7863

values_list:
	values_list ','  no_braces
7864
	| no_braces;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7865 7866 7867 7868

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
7869
	ident_eq_value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7870 7871

ident_eq_value:
7872
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7873
	 {
7874 7875 7876
	  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
7877
	    YYABORT;
7878
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7879 7880

equal:	EQ		{}
7881 7882 7883 7884 7885 7886 7887
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7888 7889 7890 7891 7892 7893 7894 7895 7896

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
7897 7898
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7899
	    YYABORT;
7900
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7901 7902 7903

opt_values:
	/* empty */ {}
7904
	| values;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7905 7906

values:
7907
	values ','  expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7908 7909 7910 7911
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
7912 7913 7914 7915 7916 7917 7918 7919
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
7920
	expr	  { $$= $1;}
7921
	| DEFAULT {$$= new Item_default_value(Lex->current_context()); }
7922
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7923

7924 7925
opt_insert_update:
        /* empty */
monty@mysql.com's avatar
monty@mysql.com committed
7926
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
7927
          KEY_SYM UPDATE_SYM insert_update_list
7928 7929
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7930 7931 7932
/* Update rows in a table */

update:
7933 7934
	UPDATE_SYM
	{
7935
	  LEX *lex= Lex;
7936
	  mysql_init_select(lex);
7937
          lex->sql_command= SQLCOM_UPDATE;
7938
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
7939
	  lex->duplicates= DUP_ERROR; 
7940
        }
7941
        opt_low_priority opt_ignore join_table_list
7942
	SET update_list
7943
	{
7944 7945
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
7946
            lex->sql_command= SQLCOM_UPDATE_MULTI;
7947 7948 7949
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
7950 7951
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
7952 7953
	    YYABORT;
	  }
7954 7955 7956 7957 7958 7959
          /*
            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);
7960
	}
7961
	where_clause opt_order_clause delete_limit_clause {}
7962
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7963 7964

update_list:
7965 7966 7967 7968
	update_list ',' update_elem
	| update_elem;

update_elem:
monty@mishka.local's avatar
monty@mishka.local committed
7969
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7970
	{
monty@mysql.com's avatar
monty@mysql.com committed
7971
	  if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7972
	    YYABORT;
7973 7974 7975 7976 7977 7978 7979
	};

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
monty@mishka.local's avatar
monty@mishka.local committed
7980
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7981
	  {
7982 7983 7984
	  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
7985
	      YYABORT;
7986
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7987 7988

opt_low_priority:
7989
	/* empty */	{ $$= YYTHD->update_lock_default; }
7990
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7991 7992 7993 7994

/* Delete rows from a table */

delete:
7995
	DELETE_SYM
7996
	{
7997 7998
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
7999
	  mysql_init_select(lex);
8000
	  lex->lock_option= lex->thd->update_lock_default;
8001
	  lex->ignore= 0;
8002
	  lex->select_lex.init_order();
8003
	}
8004 8005
	opt_delete_options single_multi {}
	;
8006 8007

single_multi:
8008 8009
 	FROM table_ident
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8010 8011
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
8012 8013 8014
	    YYABORT;
	}
	where_clause opt_order_clause
8015
	delete_limit_clause {}
8016
	| table_wild_list
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8017
	  { mysql_init_multi_delete(Lex); }
8018
          FROM join_table_list where_clause
8019 8020 8021 8022
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8023 8024
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
8025
	  USING join_table_list where_clause
8026 8027 8028 8029
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
8030
	;
8031 8032 8033

table_wild_list:
	  table_wild_one {}
8034
	  | table_wild_list ',' table_wild_one {};
8035 8036

table_wild_one:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
8037
	ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8038
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8039
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
8040 8041
					 TL_OPTION_UPDATING | 
                                         TL_OPTION_ALIAS, Lex->lock_option))
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8042 8043
	    YYABORT;
        }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
8044
	| ident '.' ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8045
	  {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8046 8047
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
8048 8049 8050
					   $5, 
                                           TL_OPTION_UPDATING | 
                                           TL_OPTION_ALIAS,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8051
					   Lex->lock_option))
8052
	      YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8053
	  }
8054
	;
8055 8056

opt_wild:
8057
	/* empty */	{}
8058
	| '.' '*'	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8059 8060


8061
opt_delete_options:
8062
	/* empty */	{}
8063
	| opt_delete_option opt_delete_options {};
8064 8065

opt_delete_option:
8066
	QUICK		{ Select->options|= OPTION_QUICK; }
8067
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
8068
	| IGNORE_SYM	{ Lex->ignore= 1; };
8069

8070
truncate:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8071
	TRUNCATE_SYM opt_table_sym table_name
8072
	{
8073
	  LEX* lex= Lex;
8074
	  lex->sql_command= SQLCOM_TRUNCATE;
8075
	  lex->select_lex.options= 0;
8076
          lex->select_lex.sql_cache= SELECT_LEX::SQL_CACHE_UNSPECIFIED;
8077
	  lex->select_lex.init_order();
8078 8079
	}
	;
8080

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8081 8082
opt_table_sym:
	/* empty */
8083
	| TABLE_SYM;
8084

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8085 8086
/* Show things */

8087 8088
show:	SHOW
	{
8089 8090
	  LEX *lex=Lex;
	  lex->wild=0;
8091 8092 8093
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
8094 8095
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8096
	show_param
8097 8098
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8099 8100

show_param:
8101
         DATABASES wild_and_where
8102 8103
         {
           LEX *lex= Lex;
8104
           lex->sql_command= SQLCOM_SHOW_DATABASES;
8105 8106 8107
           if (prepare_schema_table(YYTHD, lex, 0, SCH_SCHEMATA))
             YYABORT;
         }
8108
         | opt_full TABLES opt_db wild_and_where
8109 8110
           {
             LEX *lex= Lex;
8111
             lex->sql_command= SQLCOM_SHOW_TABLES;
8112
             lex->select_lex.db= $3;
8113 8114 8115
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLE_NAMES))
               YYABORT;
           }
8116 8117 8118
         | opt_full TRIGGERS_SYM opt_db wild_and_where
           {
             LEX *lex= Lex;
8119
             lex->sql_command= SQLCOM_SHOW_TRIGGERS;
8120 8121 8122 8123
             lex->select_lex.db= $3;
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TRIGGERS))
               YYABORT;
           }
8124
         | EVENTS_SYM opt_db wild_and_where
8125 8126
           {
             LEX *lex= Lex;
8127
             lex->sql_command= SQLCOM_SHOW_EVENTS;
8128
             lex->select_lex.db= $2;
8129 8130 8131
             if (prepare_schema_table(YYTHD, lex, 0, SCH_EVENTS))
               YYABORT;
           }
8132
         | TABLE_SYM STATUS_SYM opt_db wild_and_where
8133 8134
           {
             LEX *lex= Lex;
8135
             lex->sql_command= SQLCOM_SHOW_TABLE_STATUS;
8136
             lex->select_lex.db= $3;
8137 8138 8139
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLES))
               YYABORT;
           }
8140
        | OPEN_SYM TABLES opt_db wild_and_where
8141
	  {
8142
	    LEX *lex= Lex;
8143
            lex->sql_command= SQLCOM_SHOW_OPEN_TABLES;
8144
	    lex->select_lex.db= $3;
8145 8146
            if (prepare_schema_table(YYTHD, lex, 0, SCH_OPEN_TABLES))
              YYABORT;
8147
	  }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
8148
        | opt_full PLUGIN_SYM
8149 8150 8151
	  {
	    LEX *lex= Lex;
	    WARN_DEPRECATED(yythd, "5.2", "SHOW PLUGIN", "'SHOW PLUGINS'");
8152
            lex->sql_command= SQLCOM_SHOW_PLUGINS;
8153 8154 8155 8156
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PLUGINS))
              YYABORT;
	  }
        | PLUGINS_SYM
8157 8158
	  {
	    LEX *lex= Lex;
8159
            lex->sql_command= SQLCOM_SHOW_PLUGINS;
8160 8161 8162
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PLUGINS))
              YYABORT;
	  }
8163
	| ENGINE_SYM storage_engines 
8164 8165
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
8166
	| ENGINE_SYM ALL 
8167
	  { Lex->create_info.db_type= NULL; }
8168
	  show_engine_param
8169
	| opt_full COLUMNS from_or_in table_ident opt_db wild_and_where
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8170
	  {
8171
 	    LEX *lex= Lex;
8172
	    lex->sql_command= SQLCOM_SHOW_FIELDS;
8173 8174 8175
	    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
8176 8177
	      YYABORT;
	  }
8178
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
8179 8180
	  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
8181
	ulong_num
8182 8183 8184 8185
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
8186
	    Lex->mi.server_id = $16;
8187
          }
8188
        | master_or_binary LOGS_SYM
8189 8190
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
8191 8192 8193 8194 8195
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
8196
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
8197
          {
8198 8199
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
8200
          } opt_limit_clause_init
8201
        | keys_or_index from_or_in table_ident opt_db where_clause
8202 8203
          {
            LEX *lex= Lex;
8204
            lex->sql_command= SQLCOM_SHOW_KEYS;
8205 8206 8207
	    if ($4)
	      $3->change_db($4);
            if (prepare_schema_table(YYTHD, lex, $3, SCH_STATISTICS))
8208
              YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8209
	  }
8210 8211 8212 8213 8214 8215 8216 8217
	| COLUMN_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_COLUMN_TYPES;
	  }
	| TABLE_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
8218
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
8219
	    WARN_DEPRECATED(yythd, "5.2", "SHOW TABLE TYPES", "'SHOW [STORAGE] ENGINES'");
8220 8221 8222 8223 8224
	  }
	| opt_storage ENGINES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
8225 8226
            if (prepare_schema_table(YYTHD, lex, 0, SCH_ENGINES))
              YYABORT;
8227
	  }
brian@grrr.local's avatar
brian@grrr.local committed
8228 8229 8230 8231 8232
	| AUTHORS_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_AUTHORS;
	  }
8233 8234 8235 8236 8237
	| CONTRIBUTORS_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_CONTRIBUTORS;
	  }
8238 8239 8240 8241 8242
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
8243
        | COUNT_SYM '(' '*' ')' WARNINGS
8244
          { (void) create_select_for_variable("warning_count"); }
8245
        | COUNT_SYM '(' '*' ')' ERRORS
8246
	  { (void) create_select_for_variable("error_count"); }
8247
        | WARNINGS opt_limit_clause_init
8248
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
8249
        | ERRORS opt_limit_clause_init
8250
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
8251
        | opt_var_type STATUS_SYM wild_and_where
8252
          {
8253
            LEX *lex= Lex;
8254
            lex->sql_command= SQLCOM_SHOW_STATUS;
8255
            lex->option_type= $1;
8256 8257
            if (prepare_schema_table(YYTHD, lex, 0, SCH_STATUS))
              YYABORT;
8258
	  }
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
8259
        | INNOBASE_SYM STATUS_SYM
8260 8261 8262
          {
            LEX *lex= Lex;
            lex->sql_command = SQLCOM_SHOW_ENGINE_STATUS;
8263 8264 8265 8266 8267 8268
            if (!(lex->create_info.db_type=
                  ha_resolve_by_legacy_type(YYTHD, DB_TYPE_INNODB)))
            {
	      my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), "InnoDB");
	      YYABORT;
            }
8269
            WARN_DEPRECATED(yythd, "5.2", "SHOW INNODB STATUS", "'SHOW ENGINE INNODB STATUS'");
8270
	  }
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
8271
        | MUTEX_SYM STATUS_SYM
8272 8273
          {
	    LEX *lex= Lex;
8274
            lex->sql_command = SQLCOM_SHOW_ENGINE_MUTEX;
8275 8276 8277 8278 8279 8280
            if (!(lex->create_info.db_type=
                  ha_resolve_by_legacy_type(YYTHD, DB_TYPE_INNODB)))
            {
	      my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), "InnoDB");
	      YYABORT;
            }
8281
            WARN_DEPRECATED(yythd, "5.2", "SHOW MUTEX STATUS", "'SHOW ENGINE INNODB MUTEX'");
8282
	  }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8283 8284
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
8285
        | opt_var_type  VARIABLES wild_and_where
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8286
	  {
8287
            LEX *lex= Lex;
8288
            lex->sql_command= SQLCOM_SHOW_VARIABLES;
8289
            lex->option_type= $1;
8290 8291
            if (prepare_schema_table(YYTHD, lex, 0, SCH_VARIABLES))
              YYABORT;
8292
	  }
8293
        | charset wild_and_where
8294 8295
          {
            LEX *lex= Lex;
8296
            lex->sql_command= SQLCOM_SHOW_CHARSETS;
8297 8298 8299
            if (prepare_schema_table(YYTHD, lex, 0, SCH_CHARSETS))
              YYABORT;
          }
8300
        | COLLATION_SYM wild_and_where
8301 8302
          {
            LEX *lex= Lex;
8303
            lex->sql_command= SQLCOM_SHOW_COLLATIONS;
8304 8305 8306
            if (prepare_schema_table(YYTHD, lex, 0, SCH_COLLATIONS))
              YYABORT;
          }
8307 8308 8309 8310 8311
	| GRANTS
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    LEX_USER *curr_user;
8312
            if (!(curr_user= (LEX_USER*) lex->thd->alloc(sizeof(st_lex_user))))
8313
              YYABORT;
8314
            bzero(curr_user, sizeof(st_lex_user));
8315 8316
	    lex->grant_user= curr_user;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8317
	| GRANTS FOR_SYM user
8318 8319 8320 8321
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
8322
	    lex->grant_user->password=null_lex_str;
8323
	  }
8324
	| CREATE DATABASE opt_if_not_exists ident
8325 8326
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
8327 8328
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
8329
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8330 8331
        | CREATE TABLE_SYM table_ident
          {
8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342
            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
8343
	      YYABORT;
8344
            lex->only_view= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8345 8346 8347 8348
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8349
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8350 8351 8352
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367
          }
	| 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;
	  }
8368
	| PROCEDURE STATUS_SYM wild_and_where
8369
	  {
8370
            LEX *lex= Lex;
8371
            lex->sql_command= SQLCOM_SHOW_STATUS_PROC;
8372 8373
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
8374 8375
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
8376
	  }
8377
	| FUNCTION_SYM STATUS_SYM wild_and_where
8378
	  {
8379
            LEX *lex= Lex;
8380
            lex->sql_command= SQLCOM_SHOW_STATUS_FUNC;
8381 8382
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
8383 8384
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
pem@mysql.com's avatar
pem@mysql.com committed
8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405
	  }
        | PROCEDURE CODE_SYM sp_name
          {
#ifdef DBUG_OFF
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
#else
            Lex->sql_command= SQLCOM_SHOW_PROC_CODE;
	    Lex->spname= $3;
#endif
          }
        | FUNCTION_SYM CODE_SYM sp_name
          {
#ifdef DBUG_OFF
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
#else
            Lex->sql_command= SQLCOM_SHOW_FUNC_CODE;
	    Lex->spname= $3;
#endif
          }
8406 8407
        | CREATE EVENT_SYM sp_name
          {
andrey@lmy004's avatar
andrey@lmy004 committed
8408
            Lex->spname= $3;
8409
            Lex->sql_command = SQLCOM_SHOW_CREATE_EVENT;
8410 8411
          }
      ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8412

8413 8414
show_engine_param:
	STATUS_SYM
8415 8416 8417
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_STATUS; }
	| MUTEX_SYM
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_MUTEX; }
8418
	| LOGS_SYM
8419
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_LOGS; };
8420

8421 8422 8423 8424
master_or_binary:
	MASTER_SYM
	| BINARY;

8425 8426 8427 8428
opt_storage:
	/* empty */
	| STORAGE_SYM;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8429 8430
opt_db:
	/* empty */  { $$= 0; }
8431
	| from_or_in ident { $$= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8432

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8433 8434
opt_full:
	/* empty */ { Lex->verbose=0; }
8435
	| FULL	    { Lex->verbose=1; };
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8436

8437 8438
from_or_in:
	FROM
8439
	| IN_SYM;
8440

8441 8442
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8443
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
8444 8445 8446

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

8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461
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();
        }
      ;

8462

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8463 8464 8465 8466
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
8467 8468 8469 8470
          LEX *lex= Lex;
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
8471
          lex->sql_command= SQLCOM_SHOW_FIELDS;
8472 8473 8474
          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
8475 8476
	    YYABORT;
	}
8477
	opt_describe_column {}
8478 8479 8480
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
8481
          {
8482
	    LEX *lex=Lex;
8483
	    lex->select_lex.options|= SELECT_DESCRIBE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8484 8485
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8486 8487 8488

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

8491 8492 8493
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
8494
	| PARTITIONS_SYM { Lex->describe|= DESCRIBE_PARTITIONS; }
8495 8496
	;

8497

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8498 8499 8500
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
8501
	| ident
8502
	  { 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
8503 8504 8505 8506 8507


/* flush things */

flush:
8508
	FLUSH_SYM opt_no_write_to_binlog
8509 8510
	{
	  LEX *lex=Lex;
8511 8512
	  lex->sql_command= SQLCOM_FLUSH;
          lex->type= 0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8513
          lex->no_write_to_binlog= $2;
8514
	}
8515 8516 8517
	flush_options
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8518 8519 8520

flush_options:
	flush_options ',' flush_option
8521
	| flush_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8522 8523

flush_option:
8524
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8525
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8526
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8527 8528 8529 8530
	| 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
8531 8532
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
8533 8534
	| 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
8535

8536
opt_table_list:
8537 8538
	/* empty */  {;}
	| table_list {;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8539

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8540
reset:
8541 8542 8543 8544
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
8545 8546 8547 8548
	} reset_options
	{}
	;

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8549 8550
reset_options:
	reset_options ',' reset_option
8551
	| reset_option;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8552 8553

reset_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8554 8555
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
8556
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8557

8558
purge:
8559 8560 8561 8562
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
8563 8564 8565 8566 8567
	} purge_options
	{}
	;

purge_options:
8568
	master_or_binary LOGS_SYM purge_option
8569
	;
8570 8571

purge_option:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8572
        TO_SYM TEXT_STRING_sys
8573 8574 8575 8576 8577 8578
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
8579 8580 8581 8582
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
8583
	}
8584
	;
8585

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8586 8587 8588
/* kill threads */

kill:
8589 8590 8591 8592 8593 8594
	KILL_SYM
        {
          Lex->sql_command= SQLCOM_KILL;
          Lex->expr_allows_subselect= FALSE;
        }
        kill_option expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8595
	{
8596
	  LEX *lex=Lex;
8597
	  lex->value_list.empty();
serg@serg.mylan's avatar
serg@serg.mylan committed
8598
	  lex->value_list.push_front($4);
8599
          Lex->expr_allows_subselect= TRUE;
8600
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8601

8602 8603 8604
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
serg@serg.mylan's avatar
serg@serg.mylan committed
8605 8606
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; }
        ;
8607

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8608 8609 8610
/* change database */

use:	USE_SYM ident
8611 8612
	{
	  LEX *lex=Lex;
8613 8614
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
8615
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8616 8617 8618

/* import, export of files */

serg@serg.mylan's avatar
serg@serg.mylan committed
8619
load:   LOAD DATA_SYM
8620 8621
        {
          LEX *lex=Lex;
8622 8623 8624 8625 8626
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD DATA");
	    YYABORT;
	  }
8627 8628 8629 8630 8631 8632 8633
          lex->fname_start= lex->ptr;
        }
        load_data
        {}
        |
        LOAD TABLE_SYM table_ident FROM MASTER_SYM
        {
8634
	  LEX *lex=Lex;
8635 8636
          WARN_DEPRECATED(yythd, "5.2", "LOAD TABLE FROM MASTER",
                          "MySQL Administrator (mysqldump, mysql)");
8637
          if (lex->sphead)
8638 8639 8640 8641 8642
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD TABLE");
	    YYABORT;
	  }
          lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
8643 8644 8645 8646 8647
          if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
            YYABORT;
        };

load_data:
8648
	load_data_lock opt_local INFILE TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8649
	{
8650 8651
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
8652 8653
	  lex->lock_option= $1;
	  lex->local_file=  $2;
8654 8655
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
8656
	  if (!(lex->exchange= new sql_exchange($4.str, 0)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8657
	    YYABORT;
8658 8659 8660 8661 8662
        }
        opt_duplicate INTO
        {
	  LEX *lex=Lex;
	  lex->fname_end= lex->ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8663
	}
8664
        TABLE_SYM table_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8665
        {
8666 8667 8668 8669 8670 8671 8672
          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
8673
        }
8674 8675 8676
        opt_field_term opt_line_term opt_ignore_lines opt_field_or_var_spec
        opt_load_data_set_spec
        {}
8677
        |
8678
	FROM MASTER_SYM
8679 8680
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
8681
          WARN_DEPRECATED(yythd, "5.2", "LOAD DATA FROM MASTER",
8682 8683
                          "mysqldump or future "
                          "BACKUP/RESTORE DATABASE facility");
8684
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8685 8686 8687

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

8690
load_data_lock:
8691
	/* empty */	{ $$= YYTHD->update_lock_default; }
8692 8693 8694 8695 8696 8697 8698 8699 8700 8701
	| CONCURRENT
          {
#ifdef HAVE_QUERY_CACHE
            /*
              Ignore this option in SP to avoid problem with query cache
            */
            if (Lex->sphead != 0)
#endif
              $$= TL_WRITE_CONCURRENT_INSERT;
          }
8702
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
8703 8704


bk@work.mysql.com's avatar
bk@work.mysql.com committed
8705 8706 8707
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
8708
	| IGNORE_SYM	{ Lex->ignore= 1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8709 8710 8711

opt_field_term:
	/* empty */
8712
	| COLUMNS field_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8713 8714 8715

field_term_list:
	field_term_list field_term
8716
	| field_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8717 8718

field_term:
8719 8720
	TERMINATED BY text_string 
          {
8721
            DBUG_ASSERT(Lex->exchange != 0);
8722 8723
            Lex->exchange->field_term= $3;
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8724
	| OPTIONALLY ENCLOSED BY text_string
8725
	  {
8726
            LEX *lex= Lex;
8727
            DBUG_ASSERT(lex->exchange != 0);
8728 8729
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
8730
	  }
8731
        | ENCLOSED BY text_string
8732
          {
8733
            DBUG_ASSERT(Lex->exchange != 0);
8734 8735
            Lex->exchange->enclosed= $3;
          }
8736
        | ESCAPED BY text_string
8737
          {
8738
            DBUG_ASSERT(Lex->exchange != 0);
8739 8740
            Lex->exchange->escaped= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8741 8742 8743

opt_line_term:
	/* empty */
8744
	| LINES line_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8745 8746 8747

line_term_list:
	line_term_list line_term
8748
	| line_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8749 8750

line_term:
8751
        TERMINATED BY text_string
8752
          {
8753
            DBUG_ASSERT(Lex->exchange != 0);
8754 8755
            Lex->exchange->line_term= $3;
          }
8756
        | STARTING BY text_string
8757
          {
8758
            DBUG_ASSERT(Lex->exchange != 0);
8759 8760
            Lex->exchange->line_start= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8761 8762 8763

opt_ignore_lines:
	/* empty */
8764 8765
        | IGNORE_SYM NUM LINES
          {
8766
            DBUG_ASSERT(Lex->exchange != 0);
8767 8768
            Lex->exchange->skip_lines= atol($2.str);
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8769

8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780
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
8781

8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792
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
8793 8794 8795
/* Common definitions */

text_literal:
8796
	TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8797 8798
	{
	  THD *thd= YYTHD;
8799
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8800
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8801
	| NCHAR_STRING
8802
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
8803
	| UNDERSCORE_CHARSET TEXT_STRING
8804
	  { $$ = new Item_string($2.str,$2.length,Lex->underscore_charset); }
8805
	| text_literal TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8806 8807
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8808 8809

text_string:
8810
	TEXT_STRING_literal
8811
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8812 8813
	| HEX_NUM
	  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8814
	    Item *tmp= new Item_hex_string($1.str, $1.length);
8815
	    /*
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8816
	      it is OK only emulate fix_fields, because we need only
8817 8818 8819
              value of constant
	    */
	    $$= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8820
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
8821
	      (String*) 0;
8822
	  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8823 8824 8825 8826 8827 8828 8829 8830 8831 8832
        | 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;
          }
8833 8834
	;

8835
param_marker:
8836
        PARAM_MARKER
8837
        {
8838 8839
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
8840 8841 8842
          Item_param *item= new Item_param((uint) (lex->tok_start -
                                                   (uchar *) thd->query));
          if (!($$= item) || lex->param_list.push_back(item))
8843
          {
8844
            my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
8845 8846
            YYABORT;
          }
8847 8848 8849
        }
	;

8850 8851 8852
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
8853 8854
	| '-' NUM_literal
	  {
8855
	    $2->max_length++;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8856
	    $$= $2->neg();
8857
	  }
8858 8859 8860
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
8861 8862
literal:
	text_literal	{ $$ =	$1; }
8863
	| NUM_literal	{ $$ = $1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8864
	| NULL_SYM	{ $$ =	new Item_null();
8865
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
8866 8867
	| 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
8868 8869
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
8870
	| UNDERSCORE_CHARSET HEX_NUM
8871
	  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8872
	    Item *tmp= new Item_hex_string($2.str, $2.length);
8873
	    /*
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8874
	      it is OK only emulate fix_fieds, because we need only
8875 8876 8877
              value of constant
	    */
	    String *str= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8878
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
8879
	      (String*) 0;
8880 8881
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
8882
				Lex->underscore_charset);
8883
	  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897
	| 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
8898 8899
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
8900
	| TIMESTAMP text_literal { $$ = $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8901

8902
NUM_literal:
8903 8904
	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); }
8905
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
8906
        | DECIMAL_NUM
8907
	{
8908
           $$= new Item_decimal($1.str, $1.length, YYTHD->charset());
8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
8922
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
8923

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8924
/**********************************************************************
serg@serg.mylan's avatar
serg@serg.mylan committed
8925
** Creating different items.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8926 8927 8928
**********************************************************************/

insert_ident:
8929
	simple_ident_nospvar { $$=$1; }
8930
	| table_wild	 { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8931 8932

table_wild:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8933
	ident '.' '*'
8934
	{
8935
          SELECT_LEX *sel= Select;
8936
	  $$ = new Item_field(Lex->current_context(), NullS, $1.str, "*");
8937
	  sel->with_wild++;
8938
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8939
	| ident '.' ident '.' '*'
8940
	{
8941
          SELECT_LEX *sel= Select;
8942
	  $$ = new Item_field(Lex->current_context(), (YYTHD->client_capabilities &
serg@serg.mylan's avatar
serg@serg.mylan committed
8943 8944
                             CLIENT_NO_SCHEMA ? NullS : $1.str),
                             $3.str,"*");
8945
	  sel->with_wild++;
8946 8947
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8948 8949

order_ident:
8950
	expr { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8951 8952

simple_ident:
8953 8954
	ident
	{
8955
	  sp_variable_t *spv;
8956 8957
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;
8958
	  if (spc && (spv = spc->find_variable(&$1)))
8959 8960 8961
	  {
            /* We're compiling a stored procedure and found a variable */
            Item_splocal *splocal;
8962 8963
            splocal= new Item_splocal($1, spv->offset, spv->type,
                                      lex->tok_start_prev - 
8964
                                      lex->sphead->m_tmp_query);
8965
#ifndef DBUG_OFF
8966
            if (splocal)
8967
              splocal->m_sp= lex->sphead;
8968
#endif
8969
	    $$ = (Item*) splocal;
8970 8971 8972 8973 8974 8975 8976 8977
            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) ?
8978 8979
                 (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	         (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
8980 8981 8982 8983 8984 8985
	  }
        }
        | simple_ident_q { $$= $1; }
	;

simple_ident_nospvar:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8986
	ident
8987
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8988
	  SELECT_LEX *sel=Select;
8989
	  $$= (sel->parsing_place != IN_HAVING ||
8990
	       sel->get_in_sum_expr() > 0) ?
8991 8992
              (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	      (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
8993
	}
8994
	| simple_ident_q { $$= $1; }
serg@serg.mylan's avatar
serg@serg.mylan committed
8995
	;
8996 8997 8998

simple_ident_q:
	ident '.' ident
8999
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9000
	  THD *thd= YYTHD;
9001
	  LEX *lex= thd->lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
9002

9003 9004 9005 9006 9007 9008
          /*
            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
9009
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") ||
9010 9011
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
9012
            Item_trigger_field *trg_fld;
9013
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
serg@serg.mylan's avatar
serg@serg.mylan committed
9014

9015 9016 9017
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
9018
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
9019 9020
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
9021

9022 9023 9024
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
9025
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
9026 9027
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
9028

9029 9030 9031 9032 9033
            DBUG_ASSERT(!new_row ||
                        (lex->trg_chistics.event == TRG_EVENT_INSERT ||
                         lex->trg_chistics.event == TRG_EVENT_UPDATE));
            const bool read_only=
              !(new_row && lex->trg_chistics.action_time == TRG_ACTION_BEFORE);
9034
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
9035
                                                  new_row ?
9036 9037
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
9038
                                                  $3.str,
9039 9040
                                                  SELECT_ACL,
                                                  read_only)))
9041
              YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
9042

9043 9044 9045 9046 9047 9048
            /*
              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
9049

9050 9051 9052 9053 9054 9055 9056
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
9057 9058
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
9059 9060 9061
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
9062 9063
	        (Item*) new Item_field(Lex->current_context(), NullS, $1.str, $3.str) :
	        (Item*) new Item_ref(Lex->current_context(), NullS, $1.str, $3.str);
9064 9065
          }
        }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9066
	| '.' ident '.' ident
9067
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9068
	  THD *thd= YYTHD;
9069
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9070
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9071 9072
	  if (sel->no_table_names_allowed)
	  {
9073 9074
	    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
9075
	  }
9076
	  $$= (sel->parsing_place != IN_HAVING ||
9077
	       sel->get_in_sum_expr() > 0) ?
9078 9079
	      (Item*) new Item_field(Lex->current_context(), NullS, $2.str, $4.str) :
              (Item*) new Item_ref(Lex->current_context(), NullS, $2.str, $4.str);
9080
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9081
	| ident '.' ident '.' ident
9082
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9083
	  THD *thd= YYTHD;
9084
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9085
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9086 9087
	  if (sel->no_table_names_allowed)
	  {
9088 9089
	    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
9090
	  }
9091
	  $$= (sel->parsing_place != IN_HAVING ||
9092
	       sel->get_in_sum_expr() > 0) ?
9093
	      (Item*) new Item_field(Lex->current_context(),
9094
                                     (YYTHD->client_capabilities &
9095 9096
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
9097
	      (Item*) new Item_ref(Lex->current_context(),
9098
                                   (YYTHD->client_capabilities &
9099
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
9100
                                   $3.str, $5.str);
9101
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9102 9103 9104 9105


field_ident:
	ident			{ $$=$1;}
9106 9107 9108 9109 9110
	| ident '.' ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->db))
            {
9111
              my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
9112 9113
              YYABORT;
            }
9114 9115
            if (my_strcasecmp(table_alias_charset, $3.str,
                              table->table_name))
9116
            {
9117
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3.str);
9118 9119 9120 9121 9122 9123 9124 9125 9126
              YYABORT;
            }
            $$=$5;
          }
	| ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->alias))
            {
9127
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $1.str);
9128 9129 9130 9131
              YYABORT;
            }
            $$=$3;
          }
9132
	| '.' ident		{ $$=$2;}	/* For Delphi */;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9133 9134 9135

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

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

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9144
IDENT_sys:
9145 9146 9147 9148 9149
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
9150 9151
            {
              CHARSET_INFO *cs= system_charset_info;
9152
              int dummy_error;
9153 9154
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
9155
                                                   $1.length, &dummy_error);
9156 9157
              if (wlen < $1.length)
              {
9158 9159
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
9160 9161
                YYABORT;
              }
9162
	      $$= $1;
9163
            }
9164 9165 9166 9167
	    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
9168 9169 9170 9171 9172 9173
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9174 9175
	  if (thd->charset_is_system_charset)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9176
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9177 9178
	    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
9179 9180 9181
	}
	;

9182
TEXT_STRING_literal:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9183 9184 9185
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9186 9187
	  if (thd->charset_is_collation_connection)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9188
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9189 9190
	    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
9191 9192 9193 9194
	}
	;


9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206
TEXT_STRING_filesystem:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
	  if (thd->charset_is_character_set_filesystem)
	    $$= $1;
	  else
	    thd->convert_string(&$$, thd->variables.character_set_filesystem,
				$1.str, $1.length, thd->charset());
	}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
9207
ident:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9208
	IDENT_sys	    { $$=$1; }
9209 9210 9211 9212 9213 9214
	| READ_ONLY_SYM
	{
	  THD *thd= YYTHD;
	  $$.str= thd->strmake("read_only",9);
	  $$.length= 9;
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9215 9216
	| keyword
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9217 9218 9219
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
9220 9221
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9222

9223 9224 9225 9226 9227 9228 9229 9230 9231 9232
label_ident:
	IDENT_sys	    { $$=$1; }
	| keyword_sp
	{
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
	}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
9233
ident_or_text:
serg@serg.mylan's avatar
serg@serg.mylan committed
9234
        ident                   { $$=$1;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9235 9236
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9237 9238 9239 9240

user:
	ident_or_text
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9241 9242
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9243
	    YYABORT;
9244 9245 9246
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
9247

gluh@mysql.com/gluh.(none)'s avatar
gluh@mysql.com/gluh.(none) committed
9248 9249
	  if (check_string_length(system_charset_info, &$$->user,
                                  ER(ER_USERNAME), USERNAME_LENGTH))
9250
	    YYABORT;
9251
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9252 9253
	| ident_or_text '@' ident_or_text
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9254 9255
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9256 9257
	      YYABORT;
	    $$->user = $1; $$->host=$3;
9258

gluh@mysql.com/gluh.(none)'s avatar
gluh@mysql.com/gluh.(none) committed
9259 9260 9261 9262
	    if (check_string_length(system_charset_info, &$$->user,
                                    ER(ER_USERNAME), USERNAME_LENGTH) ||
	        check_string_length(&my_charset_latin1, &$$->host,
                                    ER(ER_HOSTNAME), HOSTNAME_LENGTH))
9263
	      YYABORT;
9264
	  }
9265
	| CURRENT_USER optional_braces
9266
	{
9267
          if (!($$=(LEX_USER*) YYTHD->alloc(sizeof(st_lex_user))))
9268
            YYABORT;
9269 9270 9271 9272 9273 9274
          /* 
            empty LEX_USER means current_user and 
            will be handled in the  get_current_user() function
            later
          */
          bzero($$, sizeof(LEX_USER));
9275
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9276

9277
/* Keyword that we allow for identifiers (except SP labels) */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9278
keyword:
9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297
	keyword_sp		{}
	| ASCII_SYM		{}
	| BACKUP_SYM		{}
	| BEGIN_SYM		{}
	| BYTE_SYM		{}
	| CACHE_SYM		{}
	| CHARSET		{}
	| CHECKSUM_SYM		{}
	| CLOSE_SYM		{}
	| COMMENT_SYM		{}
	| COMMIT_SYM		{}
	| CONTAINS_SYM          {}
        | DEALLOCATE_SYM        {}
	| DO_SYM		{}
	| END			{}
	| EXECUTE_SYM		{}
	| FLUSH_SYM		{}
	| HANDLER_SYM		{}
	| HELP_SYM		{}
9298
        | INSTALL_SYM           {}
9299 9300 9301
	| LANGUAGE_SYM          {}
	| NO_SYM		{}
	| OPEN_SYM		{}
9302
        | PARSER_SYM            {}
9303
	| PARTITION_SYM		{}
9304
        | PREPARE_SYM           {}
9305
	| REMOVE_SYM		{}
9306 9307 9308 9309 9310 9311 9312 9313
	| REPAIR		{}
	| RESET_SYM		{}
	| RESTORE_SYM		{}
	| ROLLBACK_SYM		{}
	| SAVEPOINT_SYM		{}
	| SECURITY_SYM		{}
	| SIGNED_SYM		{}
	| SLAVE			{}
9314
        | SONAME_SYM            {}
9315 9316 9317 9318
	| START_SYM		{}
	| STOP_SYM		{}
	| TRUNCATE_SYM		{}
	| UNICODE_SYM		{}
9319
        | UNINSTALL_SYM         {}
9320
        | XA_SYM                {}
9321
        | UPGRADE_SYM           {}
9322 9323 9324 9325
	;

/*
 * Keywords that we allow for labels in SPs.
pem@mysql.com's avatar
pem@mysql.com committed
9326 9327 9328
 * Anything that's the beginning of a statement or characteristics
 * must be in keyword above, otherwise we get (harmful) shift/reduce
 * conflicts.
9329 9330
 */
keyword_sp:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9331
	ACTION			{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
9332
	| ADDDATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9333
	| AFTER_SYM		{}
9334
	| AGAINST		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9335
	| AGGREGATE_SYM		{}
9336
	| ALGORITHM_SYM		{}
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9337
	| ANY_SYM		{}
andrey@lmy004's avatar
andrey@lmy004 committed
9338
	| AT_SYM                {}
9339
	| AUTHORS_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9340
	| AUTO_INC		{}
9341
	| AUTOEXTEND_SIZE_SYM   {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9342 9343
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
9344
	| BINLOG_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9345 9346
	| BIT_SYM		{}
	| BOOL_SYM		{}
9347
	| BOOLEAN_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9348
	| BTREE_SYM		{}
9349
	| CASCADED              {}
9350
	| CHAIN_SYM		{}
9351
	| CHANGED		{}
9352
	| CIPHER_SYM		{}
9353
	| CLIENT_SYM		{}
9354
	| COALESCE		{}
9355
	| CODE_SYM              {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9356
	| COLLATION_SYM		{}
9357
        | COLUMNS               {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9358
	| COMMITTED_SYM		{}
9359
	| COMPACT_SYM		{}
9360
	| COMPLETION_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9361
	| COMPRESSED_SYM	{}
9362
	| CONCURRENT		{}
9363
	| CONSISTENT_SYM	{}
9364
	| CONTRIBUTORS_SYM	{}
9365
	| CUBE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9366
	| DATA_SYM		{}
9367
	| DATAFILE_SYM          {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9368 9369 9370
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
9371
	| DEFINER_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9372
	| DELAY_KEY_WRITE_SYM	{}
9373 9374
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
9375
	| DISABLE_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
9376
	| DISCARD		{}
9377
	| DISK_SYM              {}
9378 9379
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9380
	| DYNAMIC_SYM		{}
9381
	| ENDS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9382
	| ENUM			{}
9383
	| ENGINE_SYM		{}
9384
	| ENGINES_SYM		{}
9385
	| ERRORS		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9386
	| ESCAPE_SYM		{}
9387
	| EVENT_SYM		{}
9388
	| EVENTS_SYM		{}
andrey@lmy004's avatar
andrey@lmy004 committed
9389 9390
	| EVERY_SYM             {}
	| EXPANSION_SYM         {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9391
	| EXTENDED_SYM		{}
9392
	| EXTENT_SIZE_SYM       {}
9393
	| FAST_SYM		{}
9394
	| FOUND_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9395
	| ENABLE_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
9396
	| FULL			{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9397 9398 9399
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
9400
	| FRAC_SECOND_SYM	{}
9401
	| GEOMETRY_SYM		{}
9402
	| GEOMETRYCOLLECTION	{}
9403
	| GET_FORMAT		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9404
	| GRANTS		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9405
	| GLOBAL_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9406
	| HASH_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9407 9408 9409
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
9410
	| INVOKER_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
9411
	| IMPORT		{}
9412
	| INDEXES		{}
9413
	| INITIAL_SIZE_SYM      {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9414
	| ISOLATION		{}
9415
	| ISSUER_SYM		{}
9416
	| INNOBASE_SYM		{}
9417
	| INSERT_METHOD		{}
9418
	| KEY_BLOCK_SIZE	{}
9419
	| LAST_SYM		{}
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
9420
	| LEAVES                {}
9421
	| LESS_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9422
	| LEVEL_SYM		{}
9423
	| LINESTRING		{}
9424
	| LIST_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9425
	| LOCAL_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9426
	| LOCKS_SYM		{}
9427
	| LOGFILE_SYM           {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9428 9429 9430 9431 9432 9433 9434 9435 9436
	| 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	{}
9437
	| MASTER_SERVER_ID_SYM  {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9438
	| MASTER_CONNECT_RETRY_SYM	{}
9439 9440 9441 9442 9443 9444
	| 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
9445 9446
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
9447
	| MAX_SIZE_SYM          {}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9448
	| MAX_UPDATES_PER_HOUR	{}
9449
	| MAX_USER_CONNECTIONS_SYM {}
9450
	| MAX_VALUE_SYM         {}
9451
	| MEDIUM_SYM		{}
9452
	| MEMORY_SYM		{}
9453
	| MERGE_SYM		{}
9454
	| MICROSECOND_SYM	{}
9455
        | MIGRATE_SYM           {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9456 9457 9458
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9459
	| MODE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9460
	| MONTH_SYM		{}
9461 9462 9463
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
9464
        | MUTEX_SYM             {}
9465
	| NAME_SYM              {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9466
	| NAMES_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9467 9468
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
9469
	| NDBCLUSTER_SYM	{}
9470
	| NEXT_SYM		{}
9471
	| NEW_SYM		{}
9472 9473
	| NO_WAIT_SYM           {}
	| NODEGROUP_SYM         {}
9474
	| NONE_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9475
	| NVARCHAR_SYM		{}
9476
	| OFFSET_SYM		{}
9477
	| OLD_PASSWORD		{}
9478
	| ONE_SHOT_SYM		{}
9479
        | ONE_SYM               {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9480
	| PACK_KEYS_SYM		{}
9481
	| PARTIAL		{}
9482
	| PARTITIONING_SYM	{}
9483
	| PARTITIONS_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9484
	| PASSWORD		{}
9485
        | PHASE_SYM             {}
9486 9487
        | PLUGIN_SYM            {}
        | PLUGINS_SYM           {}
9488
	| POINT_SYM		{}
9489
	| POLYGON		{}
9490
        | PRESERVE_SYM          {}
9491
	| PREV_SYM		{}
9492
        | PRIVILEGES            {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9493 9494
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
9495
	| QUARTER_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9496
	| QUERY_SYM		{}
9497
	| QUICK			{}
9498
        | REBUILD_SYM           {}
9499
        | RECOVER_SYM           {}
9500
	| REDO_BUFFER_SIZE_SYM	{}
9501
	| REDOFILE_SYM          {}
serg@serg.mylan's avatar
serg@serg.mylan committed
9502
        | REDUNDANT_SYM         {}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9503 9504
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
9505
	| RELAY_THREAD		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9506
	| RELOAD		{}
9507
	| REORGANIZE_SYM	{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9508
	| REPEATABLE_SYM	{}
9509
	| REPLICATION		{}
9510
	| RESOURCES		{}
9511
        | RESUME_SYM            {}
9512
	| RETURNS_SYM           {}
9513
	| ROLLUP_SYM		{}
9514
	| ROUTINE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9515 9516 9517
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9518
	| RTREE_SYM		{}
andrey@lmy004's avatar
andrey@lmy004 committed
9519
	| SCHEDULE_SYM		{}	
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9520
	| SECOND_SYM		{}
9521
	| SERIAL_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9522 9523
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
9524
	| SIMPLE_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9525
	| SHARE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9526
	| SHUTDOWN		{}
9527
	| SNAPSHOT_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9528
	| SOUNDS_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9529
	| SQL_CACHE_SYM		{}
9530
	| SQL_BUFFER_RESULT	{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9531
	| SQL_NO_CACHE_SYM	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9532
	| SQL_THREAD		{}
9533
	| STARTS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9534
	| STATUS_SYM		{}
9535
	| STORAGE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9536
	| STRING_SYM		{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
9537
	| SUBDATE_SYM		{}
9538
	| SUBJECT_SYM		{}
9539 9540
	| SUBPARTITION_SYM	{}
	| SUBPARTITIONS_SYM	{}
9541
	| SUPER_SYM		{}
9542
        | SUSPEND_SYM           {}
9543
        | TABLES                {}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
9544
	| TABLESPACE		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9545
	| TEMPORARY		{}
9546
	| TEMPTABLE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9547
	| TEXT_SYM		{}
9548
	| THAN_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9549
	| TRANSACTION_SYM	{}
9550
	| TRIGGERS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9551
	| TIMESTAMP		{}
9552 9553
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9554
	| TIME_SYM		{}
9555
	| TYPES_SYM		{}
9556
        | TYPE_SYM              {}
serg@serg.mylan's avatar
serg@serg.mylan committed
9557
        | UDF_RETURNS_SYM       {}
9558
	| FUNCTION_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9559
	| UNCOMMITTED_SYM	{}
9560
	| UNDEFINED_SYM		{}
9561 9562
	| UNDO_BUFFER_SIZE_SYM	{}
	| UNDOFILE_SYM  	{}
9563
	| UNKNOWN_SYM		{}
9564
	| UNTIL_SYM		{}
9565
	| USER			{}
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
9566
	| USE_FRM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9567
	| VARIABLES		{}
9568
	| VIEW_SYM		{}
9569
	| VALUE_SYM		{}
9570
	| WARNINGS		{}
9571
	| WAIT_SYM              {}
9572
	| WEEK_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9573
	| WORK_SYM		{}
9574
	| X509_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9575 9576
	| YEAR_SYM		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9577 9578 9579 9580 9581 9582

/* Option functions */

set:
	SET opt_option
	{
9583 9584
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
9585
	  mysql_init_select(lex);
9586
	  lex->option_type=OPT_SESSION;
9587
	  lex->var_list.empty();
9588
          lex->one_shot_set= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9589
	}
9590 9591 9592
	option_value_list
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9593 9594 9595

opt_option:
	/* empty */ {}
9596
	| OPTION {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9597 9598

option_value_list:
9599 9600 9601 9602 9603 9604 9605 9606 9607 9608
	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
9609 9610
              and sp_instr_set_trigger instructions share one LEX.
              (Well, it is theoretically possible but adds some extra
9611 9612 9613 9614 9615 9616 9617 9618
               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
9619

9620 9621 9622 9623 9624 9625 9626 9627 9628
            /* 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;
          }
        }
9629
	ext_option_value
9630 9631
        {
          LEX *lex= Lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
9632

9633 9634 9635
          if (lex->sphead)
          {
            sp_head *sp= lex->sphead;
serg@serg.mylan's avatar
serg@serg.mylan committed
9636

9637 9638 9639 9640 9641 9642 9643 9644 9645
	    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
9646

9647 9648 9649
              if (!(i= new sp_instr_stmt(sp->instructions(), lex->spcont,
                                         lex)))
                YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
9650

9651 9652 9653 9654 9655 9656
              /*
                Extract the query statement from the tokenizer.  The
                end is either lex->ptr, if there was no lookahead,
                lex->tok_end otherwise.
              */
              if (yychar == YYEMPTY)
9657 9658 9659
                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
9660

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

9664 9665 9666 9667 9668 9669 9670 9671 9672
              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);
          }
        };
9673 9674

option_type:
9675 9676 9677 9678 9679 9680 9681 9682 9683
        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; }
9684 9685 9686 9687
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9688
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
9689 9690 9691 9692 9693 9694
	| 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
9695
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
9696 9697 9698
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9699

9700 9701 9702
ext_option_value:
        sys_option_value
        | option_type2 option_value;
9703

9704 9705 9706 9707
sys_option_value:
        option_type internal_variable_name equal set_expr_or_default
        {
          LEX *lex=Lex;
9708

9709
          if ($2.var == trg_new_row_fake_var)
9710 9711 9712
          {
            /* We are in trigger and assigning value to field of new row */
            Item *it;
9713
            Item_trigger_field *trg_fld;
9714 9715
            sp_instr_set_trigger_field *sp_fld;
	    LINT_INIT(sp_fld);
9716 9717 9718 9719
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
9720
            }
9721 9722
            if ($4)
              it= $4;
9723
            else
9724 9725 9726 9727
            {
              /* QQ: Shouldn't this be field's default value ? */
              it= new Item_null();
            }
9728

9729 9730 9731
            DBUG_ASSERT(lex->trg_chistics.action_time == TRG_ACTION_BEFORE &&
                        (lex->trg_chistics.event == TRG_EVENT_INSERT ||
                         lex->trg_chistics.event == TRG_EVENT_UPDATE));
9732
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
9733
                                                  Item_trigger_field::NEW_ROW,
9734
                                                  $2.base_name.str,
9735
                                                  UPDATE_ACL, FALSE)) ||
9736 9737 9738 9739 9740
                !(sp_fld= new sp_instr_set_trigger_field(lex->sphead->
                          	                         instructions(),
                                	                 lex->spcont,
							 trg_fld,
                                        	         it, lex)))
9741
              YYABORT;
9742

9743 9744 9745 9746
            /*
              Let us add this item to list of all Item_trigger_field
              objects in trigger.
            */
9747 9748
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
                                    (byte **)&trg_fld->next_trg_field);
9749

9750
            lex->sphead->add_instr(sp_fld);
9751 9752 9753 9754
          }
          else if ($2.var)
          { /* System variable */
            if ($1)
9755
              lex->option_type= $1;
9756 9757 9758 9759 9760 9761 9762
            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;
9763
            sp_variable_t *spv;
9764
            sp_instr_set *sp_set;
9765 9766 9767 9768 9769 9770 9771
            Item *it;
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }

9772
            spv= ctx->find_variable(&$2.base_name);
9773 9774 9775 9776 9777 9778 9779

            if ($4)
              it= $4;
            else if (spv->dflt)
              it= spv->dflt;
            else
              it= new Item_null();
9780 9781 9782
            sp_set= new sp_instr_set(lex->sphead->instructions(), ctx,
                                     spv->offset, it, spv->type, lex, TRUE);
            lex->sphead->add_instr(sp_set);
9783 9784 9785 9786 9787
          }
        }
        | option_type TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	{
	  LEX *lex=Lex;
9788
	  lex->option_type= $1;
9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800
	  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)));
	}
9801
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
9802 9803
	  {
	    LEX *lex=Lex;
9804
	    lex->var_list.push_back(new set_var($3, $4.var, &$4.base_name, $6));
9805
	  }
9806
	| charset old_or_new_charset_name_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9807
	{
9808
	  THD *thd= YYTHD;
9809
	  LEX *lex= Lex;
9810
	  $2= $2 ? $2: global_system_variables.character_set_client;
9811
	  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
9812
	}
9813 9814 9815 9816 9817 9818 9819 9820
        | NAMES_SYM equal expr
	  {
	    LEX *lex= Lex;
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING names;

	    names.str= (char *)"names";
	    names.length= 5;
9821
	    if (spc && spc->find_variable(&names))
9822
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), names.str);
9823 9824 9825
            else
              yyerror(ER(ER_SYNTAX_ERROR));

9826 9827
	    YYABORT;
	  }
9828
	| NAMES_SYM charset_name_or_default opt_collate
9829
	{
9830
	  LEX *lex= Lex;
9831
	  $2= $2 ? $2 : global_system_variables.character_set_client;
9832 9833
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
9834
	  {
9835 9836
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
9837
	    YYABORT;
9838
	  }
9839
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
9840
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9841
	| PASSWORD equal text_or_password
9842
	  {
9843
	    THD *thd=YYTHD;
9844
	    LEX_USER *user;
9845 9846 9847 9848 9849 9850
	    LEX *lex= Lex;	    
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING pw;

	    pw.str= (char *)"password";
	    pw.length= 8;
9851
	    if (spc && spc->find_variable(&pw))
9852 9853 9854 9855
	    {
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), pw.str);
	      YYABORT;
	    }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9856
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
9857
	      YYABORT;
9858
	    user->host=null_lex_str;
9859
	    user->user.str=thd->security_ctx->priv_user;
9860
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
9861 9862
	  }
	| PASSWORD FOR_SYM user equal text_or_password
9863
	  {
9864 9865
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
9866
	;
9867

9868 9869 9870
internal_variable_name:
	ident
	{
9871 9872
	  LEX *lex= Lex;
          sp_pcontext *spc= lex->spcont;
9873
	  sp_variable_t *spv;
9874 9875

	  /* We have to lookup here since local vars can shadow sysvars */
9876
	  if (!spc || !(spv = spc->find_variable(&$1)))
9877 9878 9879 9880 9881 9882
	  {
            /* Not an SP local variable */
	    sys_var *tmp=find_sys_var($1.str, $1.length);
	    if (!tmp)
	      YYABORT;
	    $$.var= tmp;
9883
	    $$.base_name= null_lex_str;
9884 9885 9886 9887
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
9888 9889 9890
            if (tmp == &sys_time_zone &&
                lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
9891 9892 9893 9894 9895 9896 9897 9898
            else if (spc && tmp == &sys_autocommit)
            {
              /*
                We don't allow setting AUTOCOMMIT from a stored function
		or trigger.
              */
              lex->sphead->m_flags|= sp_head::HAS_SET_AUTOCOMMIT_STMT;
            }
9899 9900 9901 9902 9903 9904 9905
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
9906
	}
9907 9908
	| ident '.' ident
	  {
9909
            LEX *lex= Lex;
9910 9911
            if (check_reserved_words(&$1))
            {
9912
	      yyerror(ER(ER_SYNTAX_ERROR));
9913 9914
              YYABORT;
            }
9915 9916 9917 9918 9919 9920
            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')
              {
9921
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
9922 9923 9924 9925
                YYABORT;
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
9926 9927
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
9928 9929 9930 9931
                YYABORT;
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
9932
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
9933 9934 9935
                YYABORT;
              }
              /* This special combination will denote field of NEW row */
9936
              $$.var= trg_new_row_fake_var;
9937 9938 9939 9940 9941 9942 9943 9944
              $$.base_name= $3;
            }
            else
            {
              sys_var *tmp=find_sys_var($3.str, $3.length);
              if (!tmp)
                YYABORT;
              if (!tmp->is_struct())
9945
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
9946 9947 9948
              $$.var= tmp;
              $$.base_name= $1;
            }
9949 9950 9951 9952 9953 9954 9955
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
9956
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9957 9958 9959
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
9960
	  }
9961
        ;
9962 9963 9964 9965 9966 9967 9968

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
9970 9971 9972 9973
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
9974
	    $$= $3.length ? YYTHD->variables.old_passwords ?
9975 9976 9977 9978 9979 9980 9981 9982
	        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;
9983 9984
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9985

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

9987
set_expr_or_default:
9988 9989
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
9990 9991
	| 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
9992
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
9993
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9994 9995


bk@work.mysql.com's avatar
bk@work.mysql.com committed
9996 9997 9998 9999 10000
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
10001 10002 10003 10004
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
10005
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOCK");
10006 10007 10008
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_LOCK_TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10009
	}
10010 10011
	table_lock_list
	{}
10012
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10013 10014 10015

table_or_tables:
	TABLE_SYM
10016
	| TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10017 10018 10019

table_lock_list:
	table_lock
10020
	| table_lock_list ',' table_lock;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10021 10022 10023

table_lock:
	table_ident opt_table_alias lock_option
10024
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
10025
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
10026 10027
	   YYABORT;
	}
10028
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10029 10030 10031

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
10032
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10033
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
10034 10035
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10036 10037

unlock:
10038 10039 10040 10041 10042 10043
	UNLOCK_SYM
	{
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
10044
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "UNLOCK");
10045 10046 10047 10048 10049 10050
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_UNLOCK_TABLES;
	}
	table_or_tables
	{}
10051
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10052 10053


10054
/*
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10055
** Handler: direct access to ISAM functions
10056 10057 10058 10059 10060
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
10061
	  LEX *lex= Lex;
10062 10063 10064 10065 10066
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
10067
	  lex->sql_command = SQLCOM_HA_OPEN;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
10068
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
10069 10070
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
10071
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
10072
	{
10073
	  LEX *lex= Lex;
10074 10075 10076 10077 10078
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
10079
	  lex->sql_command = SQLCOM_HA_CLOSE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
10080
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
10081 10082
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
10083
	| HANDLER_SYM table_ident_nodb READ_SYM
10084
	{
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
10085
	  LEX *lex=Lex;
10086 10087 10088 10089 10090
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
10091
          lex->expr_allows_subselect= FALSE;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
10092 10093
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
10094
	  lex->current_select->select_limit= new Item_int((int32) 1);
10095
	  lex->current_select->offset_limit= 0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
10096
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
10097
	    YYABORT;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10098
        }
10099 10100 10101 10102
        handler_read_or_scan where_clause opt_limit_clause
        {
          Lex->expr_allows_subselect= TRUE;
        }
10103
        ;
10104

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10105
handler_read_or_scan:
10106 10107
	handler_scan_function         { Lex->ident= null_lex_str; }
        | ident handler_rkey_function { Lex->ident= $1; }
10108
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10109 10110 10111

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
10112 10113
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10114 10115 10116 10117 10118 10119

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;  }
10120 10121
	| handler_rkey_mode
	{
10122 10123
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
10124
	  lex->ha_rkey_mode=$1;
10125
	  if (!(lex->insert_list = new List_item))
10126
	    YYABORT;
10127 10128
	} '(' values ')' { }
        ;
10129 10130

handler_rkey_mode:
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
10131 10132 10133 10134
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
10135 10136
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
10137

bk@work.mysql.com's avatar
bk@work.mysql.com committed
10138 10139 10140
/* GRANT / REVOKE */

revoke:
10141
	REVOKE clear_privileges revoke_command
10142 10143 10144 10145
	{}
        ;

revoke_command:
10146
	grant_privileges ON opt_table grant_ident FROM grant_list
10147
	{
10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175
          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;
10176
        }
10177
	|
10178
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
10179 10180 10181
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
10182
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10183 10184

grant:
10185 10186 10187 10188 10189 10190
	GRANT clear_privileges grant_command
	{}
        ;

grant_command:
	grant_privileges ON opt_table grant_ident TO_SYM grant_list
10191
	require_clause grant_options
10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223
	{
          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
10224

10225 10226 10227 10228
opt_table:
	/* Empty */
	| TABLE_SYM ;
        
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10229
grant_privileges:
10230 10231 10232 10233 10234 10235
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
10236
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10237

10238 10239 10240 10241 10242
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

10243 10244 10245
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10246

10247
object_privilege:
serg@serg.mylan's avatar
serg@serg.mylan committed
10248
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
10249 10250 10251
	| 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 {}
10252 10253
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
10254
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10255 10256 10257
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
10258
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10259 10260 10261 10262
	| RELOAD	{ Lex->grant |= RELOAD_ACL;}
	| SHUTDOWN	{ Lex->grant |= SHUTDOWN_ACL;}
	| PROCESS	{ Lex->grant |= PROCESS_ACL;}
	| FILE_SYM	{ Lex->grant |= FILE_ACL;}
10263 10264 10265 10266 10267
	| 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; }
10268 10269 10270 10271
	| 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; }
10272 10273
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
10274
	| CREATE USER { Lex->grant |= CREATE_USER_ACL; }
10275
        | EVENT_SYM { Lex->grant |= EVENT_ACL;}
10276
        | TRIGGER_SYM { Lex->grant |= TRIGGER_ACL; }
10277
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10278

10279

10280 10281
opt_and:
	/* empty */	{}
10282
	| AND_SYM	{}
10283 10284 10285 10286 10287 10288 10289 10290 10291
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
10292 10293 10294 10295
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
10296
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
10297 10298 10299 10300 10301 10302 10303 10304 10305
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
10306
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
10307 10308 10309 10310 10311 10312 10313 10314 10315
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
10316
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
10317 10318 10319 10320 10321
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
10322

10323
grant_ident:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10324 10325
	'*'
	  {
10326
	    LEX *lex= Lex;
10327 10328 10329
            THD *thd= lex->thd;
            if (thd->copy_db_to(&lex->current_select->db, NULL))
              YYABORT;
10330
	    if (lex->grant == GLOBAL_ACLS)
10331 10332
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10333
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10334 10335
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
10336
	      YYABORT;
10337
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10338 10339 10340
	  }
	| ident '.' '*'
	  {
10341
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10342
	    lex->current_select->db = $1.str;
10343
	    if (lex->grant == GLOBAL_ACLS)
10344 10345
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10346
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10347 10348
	      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
10349 10350 10351 10352 10353
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
10354
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10355
	    lex->current_select->db = NULL;
10356 10357
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
10358
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10359
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10360 10361
	      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
10362 10363 10364 10365 10366
	      YYABORT;
	    }
	  }
	| table_ident
	  {
10367
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
10368
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10369
	      YYABORT;
10370
	    if (lex->grant == GLOBAL_ACLS)
10371
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
10372 10373
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10374 10375 10376


user_list:
10377 10378 10379 10380 10381 10382 10383 10384 10385 10386
	user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;


grant_list:
10387
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
10388
	| grant_list ',' grant_user
10389 10390 10391 10392 10393
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10394 10395 10396 10397 10398 10399 10400 10401


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
10402
             if (YYTHD->variables.old_passwords)
10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419
             {
               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
10420 10421 10422
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
10423
	  { $$= $1; $1->password= $5; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10424
	| user
10425
	  { $$= $1; $1->password= null_lex_str; }
10426
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10427 10428 10429


opt_column_list:
10430 10431 10432 10433 10434
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
10435
	| '(' column_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10436 10437 10438

column_list:
	column_list ',' column_list_id
10439
	| column_list_id;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10440 10441 10442 10443

column_list_id:
	ident
	{
10444
	  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
10445 10446
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
10447
	  LEX *lex=Lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10448 10449
	  while ((point=iter++))
	  {
10450 10451
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10452 10453
		break;
	  }
10454
	  lex->grant_tot_col|= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10455
	  if (point)
10456
	    point->rights |= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10457
	  else
10458
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
10459 10460
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10461

tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
10462 10463

require_clause: /* empty */
10464
        | REQUIRE_SYM require_list
10465 10466 10467
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
10468
        | REQUIRE_SYM SSL_SYM
10469 10470 10471
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
10472
        | REQUIRE_SYM X509_SYM
10473 10474 10475 10476 10477 10478 10479
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
10480
          ;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
10481

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10482
grant_options:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10483
	/* empty */ {}
10484
	| WITH grant_option_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10485

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10486 10487
grant_option_list:
	grant_option_list grant_option {}
10488 10489
	| grant_option {}
        ;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10490 10491 10492

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
serg@serg.mylan's avatar
serg@serg.mylan committed
10493
        | MAX_QUERIES_PER_HOUR ulong_num
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10494
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
10495 10496 10497
	  LEX *lex=Lex;
	  lex->mqh.questions=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
10498
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
10499
        | MAX_UPDATES_PER_HOUR ulong_num
10500
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
10501 10502 10503
	  LEX *lex=Lex;
	  lex->mqh.updates=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
10504
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
10505
        | MAX_CONNECTIONS_PER_HOUR ulong_num
10506
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
10507 10508 10509
	  LEX *lex=Lex;
	  lex->mqh.conn_per_hour= $2;
	  lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
10510
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
10511
        | MAX_USER_CONNECTIONS_SYM ulong_num
10512
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
10513 10514 10515
	  LEX *lex=Lex;
          lex->mqh.user_conn= $2;
          lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
10516 10517
	}
        ;
10518

bk@work.mysql.com's avatar
bk@work.mysql.com committed
10519
begin:
serg@serg.mylan's avatar
serg@serg.mylan committed
10520 10521 10522 10523 10524 10525 10526
	BEGIN_SYM  
        {
	  LEX *lex=Lex;
          lex->sql_command = SQLCOM_BEGIN;
          lex->start_transaction_opt= 0;
        }
        opt_work {}
10527
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10528 10529 10530

opt_work:
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
10531
	| WORK_SYM  {}
10532
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10533

10534
opt_chain:
serg@serg.mylan's avatar
serg@serg.mylan committed
10535
	/* empty */ { $$= (YYTHD->variables.completion_type == 1); }
10536 10537 10538 10539 10540
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
serg@serg.mylan's avatar
serg@serg.mylan committed
10541
	/* empty */ { $$= (YYTHD->variables.completion_type == 2); }
10542 10543 10544 10545 10546 10547 10548 10549 10550
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
10551
commit:
serg@serg.mylan's avatar
serg@serg.mylan committed
10552
	COMMIT_SYM opt_work opt_chain opt_release
10553
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10554 10555 10556 10557
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_COMMIT;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
10558 10559
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10560 10561

rollback:
serg@serg.mylan's avatar
serg@serg.mylan committed
10562
	ROLLBACK_SYM opt_work opt_chain opt_release
10563
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10564 10565 10566 10567
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
10568
	}
10569 10570
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
10571
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10572 10573 10574
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK_TO_SAVEPOINT;
	  lex->ident= $5;
10575 10576 10577
	}
	;

10578 10579 10580
savepoint:
	SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10581 10582 10583
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SAVEPOINT;
	  lex->ident= $2;
10584 10585 10586 10587 10588 10589
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10590 10591 10592
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RELEASE_SAVEPOINT;
	  lex->ident= $3;
10593 10594
	}
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
10595
  
10596
/*
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10597
   UNIONS : glue selects together
10598 10599 10600
*/


10601
union_clause:
10602
	/* empty */ {}
10603 10604
	| union_list
	;
10605 10606

union_list:
10607
	UNION_SYM union_option
10608 10609 10610 10611 10612
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
10613
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
10614
	    YYABORT;
10615
	  }
10616
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
10617
	  {
10618
            yyerror(ER(ER_SYNTAX_ERROR));
10619 10620
	    YYABORT;
	  }
10621 10622
          /* This counter shouldn't be incremented for UNION parts */
          Lex->nest_level--;
10623
	  if (mysql_new_select(lex, 0))
10624
	    YYABORT;
10625
          mysql_init_select(lex);
10626
	  lex->current_select->linkage=UNION_TYPE;
10627 10628 10629
          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
10630
	}
10631 10632
	select_init
        {
10633 10634 10635 10636
          /*
	    Remove from the name resolution context stack the context of the
            last select in the union.
	  */
10637 10638
          Lex->pop_context();
        }
10639
	;
10640 10641

union_opt:
10642 10643 10644
	/* Empty */ { $$= 0; }
	| union_list { $$= 1; }
	| union_order_or_limit { $$= 1; }
10645
	;
10646

10647
union_order_or_limit:
10648
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10649
	    THD *thd= YYTHD;
10650
	    LEX *lex= thd->lex;
10651
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10652
	    SELECT_LEX *sel= lex->current_select;
10653
	    SELECT_LEX_UNIT *unit= sel->master_unit();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10654
	    SELECT_LEX *fake= unit->fake_select_lex;
10655 10656 10657 10658 10659 10660
	    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
10661
	    thd->where= "global ORDER clause";
10662
	  }
10663
	order_or_limit
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10664 10665
          {
	    THD *thd= YYTHD;
10666
	    thd->lex->current_select->no_table_names_allowed= 0;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10667 10668
	    thd->where= "";
          }
10669 10670 10671
	;

order_or_limit:
10672 10673
	order_clause opt_limit_clause_init
	| limit_clause
10674
	;
10675 10676

union_option:
10677 10678 10679 10680
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
10681

10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705
subselect:
        SELECT_SYM subselect_start subselect_init subselect_end
        {
          $$= $3;
        }
        | '(' subselect_start subselect ')'
          {
            LEX *lex= Lex;
	    THD *thd= YYTHD;
            /*
              note that a local variable can't be used for
              $3 as it's used in local variable construction
              and some compilers can't guarnatee the order
              in which the local variables are initialized.
            */
            List_iterator<Item> it($3->item_list);
            Item *item;
            /*
              we must fill the items list for the "derived table".
            */
            while ((item= it++))
              add_item_to_list(thd, item);
          }
          union_clause subselect_end { $$= $3; };
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10706

10707
subselect_init:
10708
  select_init2
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10709
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10710
    $$= Lex->current_select->master_unit()->first_select();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10711 10712
  };

10713
subselect_start:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10714
	{
10715
	  LEX *lex=Lex;
10716
          if (!lex->expr_allows_subselect)
10717 10718
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
10719 10720
	    YYABORT;
	  }
10721 10722 10723 10724 10725 10726 10727
          /* 
            we are making a "derived table" for the parenthesis
            as we need to have a lex level to fit the union 
            after the parenthesis, e.g. 
            (SELECT .. ) UNION ...  becomes 
            SELECT * FROM ((SELECT ...) UNION ...)
          */
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10728 10729 10730
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
10731 10732

subselect_end:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10733 10734
	{
	  LEX *lex=Lex;
10735
          lex->pop_context();
10736
          SELECT_LEX *child= lex->current_select;
10737
	  lex->current_select = lex->current_select->return_after_parsing();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
10738
          lex->nest_level--;
10739
          lex->current_select->n_child_sum_items += child->n_sum_items;
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
10740
	};
serg@serg.mylan's avatar
serg@serg.mylan committed
10741

10742 10743 10744 10745 10746 10747
/**************************************************************************

 CREATE VIEW | TRIGGER | PROCEDURE statements.

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

10748 10749
view_or_trigger_or_sp_or_event:
	definer view_or_trigger_or_sp_or_event_tail
10750 10751 10752 10753 10754
	{}
	| view_replace_or_algorithm definer view_tail
	{}
	;

10755
view_or_trigger_or_sp_or_event_tail:
10756 10757 10758 10759 10760 10761
	view_tail
	{}
	| trigger_tail
	{}
	| sp_tail
	{}
10762 10763
	| event_tail
	{}
10764 10765 10766 10767 10768 10769 10770 10771
	;

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

 DEFINER clause support.

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

10772
definer:
10773
	/* empty */
10774
	{
10775 10776 10777 10778 10779 10780 10781 10782
          /*
            We have to distinguish missing DEFINER-clause from case when
            CURRENT_USER specified as definer explicitly in order to properly
            handle CREATE TRIGGER statements which come to replication thread
            from older master servers (i.e. to create non-suid trigger in this
            case).
           */
          YYTHD->lex->definer= 0;
10783
	}
10784
	| DEFINER_SYM EQ user
10785
	{
10786
	  YYTHD->lex->definer= get_current_user(YYTHD, $3);
10787 10788 10789 10790 10791
	}
	;

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

10792
 CREATE VIEW statement parts.
10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827

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

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

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

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

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

view_suid:
	/* empty */
10828
	{ Lex->create_view_suid= VIEW_SUID_DEFAULT; }
10829
	| SQL_SYM SECURITY_SYM DEFINER_SYM
10830
	{ Lex->create_view_suid= VIEW_SUID_DEFINER; }
10831
	| SQL_SYM SECURITY_SYM INVOKER_SYM
10832
	{ Lex->create_view_suid= VIEW_SUID_INVOKER; }
10833 10834 10835 10836 10837 10838 10839 10840 10841
	;

view_tail:
	view_suid VIEW_SYM table_ident
	{
	  THD *thd= YYTHD;
	  LEX *lex= thd->lex;
	  lex->sql_command= SQLCOM_CREATE_VIEW;
	  /* first table in list is target VIEW name */
10842
	  if (!lex->select_lex.add_table_to_list(thd, $3, NULL, TL_OPTION_UPDATING))
10843 10844 10845 10846 10847 10848 10849 10850 10851
	    YYABORT;
	}
	view_list_opt AS view_select view_check_option
	{}
	;

view_list_opt:
	/* empty */
	{}
10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867
	| '(' 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)));
	  }
	;

10868 10869 10870
view_select:
	SELECT_SYM remember_name select_init2
	{
10871 10872 10873 10874 10875 10876
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          char *stmt_beg= (lex->sphead ?
                           (char *)lex->sphead->m_tmp_query :
                           thd->query);
	  lex->create_view_select_start= $2 - stmt_beg;
10877 10878 10879
	}
	| '(' remember_name select_paren ')' union_opt
	{
10880 10881 10882 10883 10884 10885
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          char *stmt_beg= (lex->sphead ?
                           (char *)lex->sphead->m_tmp_query :
                           thd->query);
	  lex->create_view_select_start= $2 - stmt_beg;
10886
	}
10887 10888
	;

10889
view_check_option:
10890
	/* empty */
10891 10892 10893 10894 10895 10896 10897
	{ 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; }
10898
	;
10899

10900 10901 10902 10903 10904 10905 10906
/**************************************************************************

 CREATE TRIGGER statement parts.

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

trigger_tail:
10907
	TRIGGER_SYM remember_name sp_name trg_action_time trg_event
10908
	ON remember_name table_ident FOR_SYM remember_name EACH_SYM ROW_SYM
10909 10910 10911
	{
	  LEX *lex= Lex;
	  sp_head *sp;
10912

10913
	  if (lex->sphead)
10914
	  {
10915 10916
	    my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "TRIGGER");
	    YYABORT;
10917
	  }
10918

10919 10920 10921 10922
	  if (!(sp= new sp_head()))
	    YYABORT;
	  sp->reset_thd_mem_root(YYTHD);
	  sp->init(lex);
10923
          sp->init_sp_name(YYTHD, $3);
10924
	  lex->stmt_definition_begin= $2;
10925
          lex->ident.str= $7;
10926
          lex->ident.length= $10 - $7;
10927

10928 10929 10930 10931 10932 10933 10934 10935
	  sp->m_type= TYPE_ENUM_TRIGGER;
	  lex->sphead= sp;
	  lex->spname= $3;
	  /*
	    We have to turn of CLIENT_MULTI_QUERIES while parsing a
	    stored procedure, otherwise yylex will chop it into pieces
	    at each ';'.
	  */
andrey@lmy004's avatar
andrey@lmy004 committed
10936
	  $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
10937
	  YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
10938

10939 10940 10941
	  bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	  lex->sphead->m_chistics= &lex->sp_chistics;
	  lex->sphead->m_body_begin= lex->ptr;
10942 10943
          while (my_isspace(system_charset_info, lex->sphead->m_body_begin[0]))
            ++lex->sphead->m_body_begin;
10944 10945 10946 10947 10948
	}
	sp_proc_stmt
	{
	  LEX *lex= Lex;
	  sp_head *sp= lex->sphead;
10949

10950
	  lex->sql_command= SQLCOM_CREATE_TRIGGER;
10951
	  sp->init_strings(YYTHD, lex);
10952
	  /* Restore flag if it was cleared above */
andrey@lmy004's avatar
andrey@lmy004 committed
10953

10954
	  YYTHD->client_capabilities |= $<ulong_num>13;
10955
	  sp->restore_thd_mem_root(YYTHD);
10956

10957 10958
	  if (sp->is_not_allowed_in_function("trigger"))
	      YYABORT;
10959

10960 10961 10962 10963 10964
	  /*
	    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.
	  */
10965
	  if (!lex->select_lex.add_table_to_list(YYTHD, $8,
10966 10967
	                                         (LEX_STRING*) 0,
	                                         TL_OPTION_UPDATING,
10968
                                                 TL_IGNORE))
10969 10970
	    YYABORT;
	}
10971 10972
	;

10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998
/**************************************************************************

 CREATE FUNCTION | PROCEDURE statements parts.

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

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

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

11000
	  lex->stmt_definition_begin= $2;
11001

11002 11003 11004 11005
	  /* Order is important here: new - reset - init */
	  sp= new sp_head();
	  sp->reset_thd_mem_root(YYTHD);
	  sp->init(lex);
11006
          sp->init_sp_name(YYTHD, $3);
11007 11008 11009 11010 11011 11012 11013 11014

	  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 ';'.
	   */
11015
          $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043
	  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;

11044
	  sp->init_strings(YYTHD, lex);
11045
	  lex->sql_command= SQLCOM_CREATE_PROCEDURE;
11046 11047 11048 11049 11050 11051
          /*
            Restore flag if it was cleared above
            Be careful with counting. the block where we save the value
            is $4.
          */
          YYTHD->client_capabilities |= $<ulong_num>4;
11052 11053 11054 11055
	  sp->restore_thd_mem_root(YYTHD);
	}
	;

11056
/*************************************************************************/
11057

11058 11059
xa: XA_SYM begin_or_start xid opt_join_or_resume
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
11060
        Lex->sql_command = SQLCOM_XA_START;
11061
      }
11062
    | XA_SYM END xid opt_suspend
11063
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
11064
        Lex->sql_command = SQLCOM_XA_END;
11065 11066 11067
      }
    | XA_SYM PREPARE_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
11068
        Lex->sql_command = SQLCOM_XA_PREPARE;
11069 11070 11071
      }
    | XA_SYM COMMIT_SYM xid opt_one_phase
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
11072
        Lex->sql_command = SQLCOM_XA_COMMIT;
11073 11074 11075
      }
    | XA_SYM ROLLBACK_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
11076
        Lex->sql_command = SQLCOM_XA_ROLLBACK;
11077 11078 11079
      }
    | XA_SYM RECOVER_SYM
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
11080
        Lex->sql_command = SQLCOM_XA_RECOVER;
11081 11082 11083
      }
    ;

serg@serg.mylan's avatar
serg@serg.mylan committed
11084 11085
xid: text_string
     {
serg@serg.mylan's avatar
serg@serg.mylan committed
11086
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
11087 11088 11089 11090 11091 11092
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set(1L, $1->ptr(), $1->length(), 0, 0);
     }
     | text_string ',' text_string
     {
serg@serg.mylan's avatar
serg@serg.mylan committed
11093
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
11094 11095 11096 11097 11098 11099
       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
     {
serg@serg.mylan's avatar
serg@serg.mylan committed
11100
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
11101 11102 11103 11104 11105
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set($5, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     ;
11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121

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

11122
opt_suspend:
11123 11124
    /* nothing */           { Lex->xa_opt=XA_NONE;        }
    | SUSPEND_SYM           { Lex->xa_opt=XA_SUSPEND;     }
11125 11126 11127 11128 11129
      opt_migrate
    ;

opt_migrate:
    /* nothing */           { }
11130 11131 11132
    | FOR_SYM MIGRATE_SYM   { Lex->xa_opt=XA_FOR_MIGRATE; }
    ;

11133
install:
11134
  INSTALL_SYM PLUGIN_SYM ident SONAME_SYM TEXT_STRING_sys
11135 11136 11137 11138 11139 11140
  {
    LEX *lex= Lex;
    lex->sql_command= SQLCOM_INSTALL_PLUGIN;
    lex->comment= $3;
    lex->ident= $5;
  };
11141

11142
uninstall:
11143
  UNINSTALL_SYM PLUGIN_SYM ident
11144 11145 11146 11147 11148
  {
    LEX *lex= Lex;
    lex->sql_command= SQLCOM_UNINSTALL_PLUGIN;
    lex->comment= $3;
  };