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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4
   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
5
   the Free Software Foundation; version 2 of the License.
6

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7 8 9 10
   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.
11

bk@work.mysql.com's avatar
bk@work.mysql.com committed
12 13 14 15
   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
16
/* sql_yacc.yy */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
17 18

%{
19 20 21
/* 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.
22 23
*/
#define YYPARSE_PARAM yythd
24
#define YYLEX_PARAM yythd
25 26
#define YYTHD ((THD *)yythd)

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

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

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

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

49
#undef 	WARN_DEPRECATED			/* this macro is also defined in mysql_priv.h */
serg@serg.mylan's avatar
serg@serg.mylan committed
50
#define WARN_DEPRECATED(A,B)                                        \
51
  push_warning_printf(((THD *)yythd), MYSQL_ERROR::WARN_LEVEL_WARN, \
serg@serg.mylan's avatar
serg@serg.mylan committed
52
		      ER_WARN_DEPRECATED_SYNTAX,                    \
53
		      ER(ER_WARN_DEPRECATED_SYNTAX), (A), (B));
54

55 56 57 58 59 60 61 62
#define MYSQL_YYABORT                         \
  do                                          \
  {                                           \
    LEX::cleanup_lex_after_parse_error(YYTHD);\
    YYABORT;                                  \
  } while (0)

#define MYSQL_YYABORT_UNLESS(A)         \
serg@serg.mylan's avatar
serg@serg.mylan committed
63
  if (!(A))                             \
64 65 66
  {                                     \
    my_parse_error(ER(ER_SYNTAX_ERROR));\
    MYSQL_YYABORT;                      \
67 68
  }

69 70 71 72 73 74
#ifndef DBUG_OFF
#define YYDEBUG 1
#else
#define YYDEBUG 0
#endif

75 76 77 78 79 80 81 82 83 84 85 86 87 88
/**
  @brief Push an error message into MySQL error stack with line
  and position information.

  This function provides semantic action implementers with a way
  to push the famous "You have a syntax error near..." error
  message into the error stack, which is normally produced only if
  a parse error is discovered internally by the Bison generated
  parser.
*/

void my_parse_error(const char *s)
{
  THD *thd= current_thd;
89
  Lex_input_stream *lip= thd->m_lip;
90

91
  const char *yytext= lip->tok_start;
92 93
  /* Push an error into the error stack */
  my_printf_error(ER_PARSE_ERROR,  ER(ER_PARSE_ERROR), MYF(0), s,
94 95
                  (yytext ? yytext : ""),
                  lip->yylineno);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
}

/**
  @brief Bison callback to report a syntax/OOM error

  This function is invoked by the bison-generated parser
  when a syntax error, a parse error or an out-of-memory
  condition occurs. This function is not invoked when the
  parser is requested to abort by semantic action code
  by means of YYABORT or YYACCEPT macros. This is why these
  macros should not be used (use MYSQL_YYABORT/MYSQL_YYACCEPT
  instead).

  The parser will abort immediately after invoking this callback.

  This function is not for use in semantic actions and is internal to
  the parser, as it performs some pre-return cleanup. 
  In semantic actions, please use my_parse_error or my_error to
  push an error into the error stack and MYSQL_YYABORT
  to abort from the parser.
*/

void MYSQLerror(const char *s)
{
  THD *thd= current_thd;

  /*
    Restore the original LEX if it was replaced when parsing
    a stored procedure. We must ensure that a parsing error
    does not leave any side effects in the THD.
  */
  LEX::cleanup_lex_after_parse_error(thd);

  /* "parse error" changed into "syntax error" between bison 1.75 and 1.875 */
  if (strcmp(s,"parse error") == 0 || strcmp(s,"syntax error") == 0)
    s= ER(ER_SYNTAX_ERROR);
  my_parse_error(s);
}


136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#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

158 159 160 161

/**
  Helper action for a case statement (entering the CASE).
  This helper is used for both 'simple' and 'searched' cases.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  This helper, with the other case_stmt_action_..., is executed when
  the following SQL code is parsed:
<pre>
CREATE PROCEDURE proc_19194_simple(i int)
BEGIN
  DECLARE str CHAR(10);

  CASE i
    WHEN 1 THEN SET str="1";
    WHEN 2 THEN SET str="2";
    WHEN 3 THEN SET str="3";
    ELSE SET str="unknown";
  END CASE;

  SELECT str;
END
</pre>
  The actions are used to generate the following code:
<pre>
SHOW PROCEDURE CODE proc_19194_simple;
Pos     Instruction
0       set str@1 NULL
1       set_case_expr (12) 0 i@0
2       jump_if_not 5(12) (case_expr@0 = 1)
3       set str@1 _latin1'1'
4       jump 12
5       jump_if_not 8(12) (case_expr@0 = 2)
6       set str@1 _latin1'2'
7       jump 12
8       jump_if_not 11(12) (case_expr@0 = 3)
9       set str@1 _latin1'3'
10      jump 12
11      set str@1 _latin1'unknown'
12      stmt 0 "SELECT str"
</pre>

198 199 200 201 202 203 204 205 206 207
  @param lex the parser lex context
*/

void case_stmt_action_case(LEX *lex)
{
  lex->sphead->new_cont_backpatch(NULL);

  /*
    BACKPATCH: Creating target label for the jump to
    "case_stmt_action_end_case"
208
    (Instruction 12 in the example)
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  */

  lex->spcont->push_label((char *)"", lex->sphead->instructions());
}

/**
  Helper action for a case expression statement (the expr in 'CASE expr').
  This helper is used for 'searched' cases only.
  @param lex the parser lex context
  @param expr the parsed expression
  @return 0 on success
*/

int case_stmt_action_expr(LEX *lex, Item* expr)
{
  sp_head *sp= lex->sphead;
  sp_pcontext *parsing_ctx= lex->spcont;
  int case_expr_id= parsing_ctx->register_case_expr();
  sp_instr_set_case_expr *i;

  if (parsing_ctx->push_case_expr_id(case_expr_id))
    return 1;

  i= new sp_instr_set_case_expr(sp->instructions(),
                                parsing_ctx, case_expr_id, expr, lex);

  sp->add_cont_backpatch(i);
  sp->add_instr(i);

  return 0;
}

/**
  Helper action for a case when condition.
  This helper is used for both 'simple' and 'searched' cases.
  @param lex the parser lex context
  @param when the parsed expression for the WHEN clause
  @param simple true for simple cases, false for searched cases
*/

void case_stmt_action_when(LEX *lex, Item *when, bool simple)
{
  sp_head *sp= lex->sphead;
  sp_pcontext *ctx= lex->spcont;
  uint ip= sp->instructions();
  sp_instr_jump_if_not *i;
  Item_case_expr *var;
  Item *expr;

  if (simple)
  {
    var= new Item_case_expr(ctx->get_current_case_expr_id());

#ifndef DBUG_OFF
    if (var)
    {
      var->m_sp= sp;
    }
#endif

    expr= new Item_func_eq(var, when);
    i= new sp_instr_jump_if_not(ip, ctx, expr, lex);
  }
  else
    i= new sp_instr_jump_if_not(ip, ctx, when, lex);

  /*
    BACKPATCH: Registering forward jump from
    "case_stmt_action_when" to "case_stmt_action_then"
278
    (jump_if_not from instruction 2 to 5, 5 to 8 ... in the example)
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  */

  sp->push_backpatch(i, ctx->push_label((char *)"", 0));
  sp->add_cont_backpatch(i);
  sp->add_instr(i);
}

/**
  Helper action for a case then statements.
  This helper is used for both 'simple' and 'searched' cases.
  @param lex the parser lex context
*/

void case_stmt_action_then(LEX *lex)
{
  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);

  /*
    BACKPATCH: Resolving forward jump from
    "case_stmt_action_when" to "case_stmt_action_then"
303
    (jump_if_not from instruction 2 to 5, 5 to 8 ... in the example)
304 305 306 307 308 309
  */

  sp->backpatch(ctx->pop_label());

  /*
    BACKPATCH: Registering forward jump from
310 311
    "case_stmt_action_then" to "case_stmt_action_end_case"
    (jump from instruction 4 to 12, 7 to 12 ... in the example)
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  */

  sp->push_backpatch(i, ctx->last_label());
}

/**
  Helper action for an end case.
  This helper is used for both 'simple' and 'searched' cases.
  @param lex the parser lex context
  @param simple true for simple cases, false for searched cases
*/

void case_stmt_action_end_case(LEX *lex, bool simple)
{
  /*
    BACKPATCH: Resolving forward jump from
    "case_stmt_action_then" to "case_stmt_action_end_case"
329
    (jump from instruction 4 to 12, 7 to 12 ... in the example)
330 331 332 333 334 335 336 337 338
  */
  lex->sphead->backpatch(lex->spcont->pop_label());

  if (simple)
    lex->spcont->pop_case_expr_id();

  lex->sphead->do_cont_backpatch();
}

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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
/**
  Helper to resolve the SQL:2003 Syntax exception 1) in <in predicate>.
  See SQL:2003, Part 2, section 8.4 <in predicate>, Note 184, page 383.
  This function returns the proper item for the SQL expression
  <code>left [NOT] IN ( expr )</code>
  @param thd the current thread
  @param left the in predicand
  @param equal true for IN predicates, false for NOT IN predicates
  @param expr first and only expression of the in value list
  @return an expression representing the IN predicate.
*/
Item* handle_sql2003_note184_exception(THD *thd, Item* left, bool equal,
                                       Item *expr)
{
  /*
    Relevant references for this issue:
    - SQL:2003, Part 2, section 8.4 <in predicate>, page 383,
    - SQL:2003, Part 2, section 7.2 <row value expression>, page 296,
    - SQL:2003, Part 2, section 6.3 <value expression primary>, page 174,
    - SQL:2003, Part 2, section 7.15 <subquery>, page 370,
    - SQL:2003 Feature F561, "Full value expressions".

    The exception in SQL:2003 Note 184 means:
    Item_singlerow_subselect, which corresponds to a <scalar subquery>,
    should be re-interpreted as an Item_in_subselect, which corresponds
    to a <table subquery> when used inside an <in predicate>.

    Our reading of Note 184 is reccursive, so that all:
    - IN (( <subquery> ))
    - IN ((( <subquery> )))
    - IN '('^N <subquery> ')'^N
    - etc
    should be interpreted as a <table subquery>, no matter how deep in the
    expression the <subquery> is.
  */

  Item *result;

  DBUG_ENTER("handle_sql2003_note184_exception");

  if (expr->type() == Item::SUBSELECT_ITEM)
  {
    Item_subselect *expr2 = (Item_subselect*) expr;

    if (expr2->substype() == Item_subselect::SINGLEROW_SUBS)
    {
      Item_singlerow_subselect *expr3 = (Item_singlerow_subselect*) expr2;
      st_select_lex *subselect;

      /*
        Implement the mandated change, by altering the semantic tree:
          left IN Item_singlerow_subselect(subselect)
        is modified to
          left IN (subselect)
        which is represented as
          Item_in_subselect(left, subselect)
      */
      subselect= expr3->invalidate_and_restore_select_lex();
      result= new (thd->mem_root) Item_in_subselect(left, subselect);

      if (! equal)
        result = negate_expression(thd, result);

      DBUG_RETURN(result);
    }
  }

  if (equal)
    result= new (thd->mem_root) Item_func_eq(left, expr);
  else
    result= new (thd->mem_root) Item_func_ne(left, expr);

  DBUG_RETURN(result);
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
414 415 416 417
%}
%union {
  int  num;
  ulong ulong_num;
418
  ulonglong ulonglong_number;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
419 420 421 422 423 424
  LEX_STRING lex_str;
  LEX_STRING *lex_str_ptr;
  LEX_SYMBOL symbol;
  Table_ident *table;
  char *simple_string;
  Item *item;
425
  Item_num *item_num;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
426 427
  List<Item> *item_list;
  List<String> *string_list;
428 429 430 431 432
  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
433
  struct sys_var_with_base variable;
434
  enum enum_var_type var_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
435
  Key::Keytype key_type;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
436
  enum ha_key_alg key_alg;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
437 438
  enum db_type db_type;
  enum row_type row_type;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
439
  enum ha_rkey_function ha_rkey_mode;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
440
  enum enum_tx_isolation tx_isolation;
441
  enum Cast_target cast_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
442
  enum Item_udftype udf_type;
443
  CHARSET_INFO *charset;
444
  thr_lock_type lock_type;
445
  interval_type interval, interval_time_st;
446
  timestamp_type date_time_type;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
447
  st_select_lex *select_lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
448
  chooser_compare_func_creator boolfunc2creator;
449 450 451 452
  struct sp_cond_type *spcondtype;
  struct { int vars, conds, hndlrs, curs; } spblock;
  sp_name *spname;
  struct st_lex *lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
453 454 455
}

%{
456
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
457 458 459
%}

%pure_parser					/* We have threads */
460 461 462 463 464
/*
  Currently there is 251 shift/reduce conflict. We should not introduce
  new conflicts any more.
*/
%expect 251
bk@work.mysql.com's avatar
bk@work.mysql.com committed
465

serg@serg.mylan's avatar
serg@serg.mylan committed
466
%token  END_OF_INPUT
bk@work.mysql.com's avatar
bk@work.mysql.com committed
467

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
%token  ABORT_SYM
%token  ACTION
%token  ADD
%token  ADDDATE_SYM
%token  AFTER_SYM
%token  AGAINST
%token  AGGREGATE_SYM
%token  ALGORITHM_SYM
%token  ALL
%token  ALTER
%token  ANALYZE_SYM
%token  AND_AND_SYM
%token  AND_SYM
%token  ANY_SYM
%token  AS
%token  ASC
%token  ASCII_SYM
%token  ASENSITIVE_SYM
%token  ATAN
%token  AUTO_INC
%token  AVG_ROW_LENGTH
%token  AVG_SYM
%token  BACKUP_SYM
%token  BEFORE_SYM
%token  BEGIN_SYM
%token  BENCHMARK_SYM
%token  BERKELEY_DB_SYM
%token  BIGINT
%token  BINARY
%token  BINLOG_SYM
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
498
%token  BIN_NUM
499 500 501 502 503 504 505 506 507 508 509 510 511 512
%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
513
%token  CASCADED
514
%token  CAST_SYM
serg@serg.mylan's avatar
serg@serg.mylan committed
515
%token  CHAIN_SYM
516 517 518 519 520 521 522 523 524 525
%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
526
%token  CODE_SYM
527 528 529 530 531 532 533
%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
534
%token  COMPACT_SYM
535 536 537 538
%token  COMPRESSED_SYM
%token  CONCAT
%token  CONCAT_WS
%token  CONCURRENT
539
%token  CONDITION_SYM
540 541 542
%token  CONNECTION_SYM
%token  CONSISTENT_SYM
%token  CONSTRAINT
543 544
%token  CONTAINS_SYM
%token  CONTINUE_SYM
545 546 547 548 549 550 551
%token  CONVERT_SYM
%token  CONVERT_TZ_SYM
%token  COUNT_SYM
%token  CREATE
%token  CROSS
%token  CUBE_SYM
%token  CURDATE
552
%token  CURRENT_USER
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
%token  CURSOR_SYM
%token  CURTIME
%token  DATABASE
%token  DATABASES
%token  DATA_SYM
%token  DATETIME
%token  DATE_ADD_INTERVAL
%token  DATE_SUB_INTERVAL
%token  DATE_SYM
%token  DAY_HOUR_SYM
%token  DAY_MICROSECOND_SYM
%token  DAY_MINUTE_SYM
%token  DAY_SECOND_SYM
%token  DAY_SYM
%token  DEALLOCATE_SYM
serg@serg.mylan's avatar
serg@serg.mylan committed
568
%token  DECIMAL_NUM
569
%token  DECIMAL_SYM
570
%token  DECLARE_SYM
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
%token  DECODE_SYM
%token  DEFAULT
%token  DEFINER_SYM
%token  DELAYED_SYM
%token  DELAY_KEY_WRITE_SYM
%token  DELETE_SYM
%token  DESC
%token  DESCRIBE
%token  DES_DECRYPT_SYM
%token  DES_ENCRYPT_SYM
%token  DES_KEY_FILE
%token  DETERMINISTIC_SYM
%token  DIRECTORY_SYM
%token  DISABLE_SYM
%token  DISCARD
%token  DISTINCT
%token  DIV_SYM
%token  DOUBLE_SYM
%token  DO_SYM
%token  DROP
%token  DUAL_SYM
%token  DUMPFILE
593
%token  DUPLICATE_SYM
594
%token  DYNAMIC_SYM
595
%token  EACH_SYM
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
%token  ELSEIF_SYM
%token  ELT_FUNC
%token  ENABLE_SYM
%token  ENCLOSED
%token  ENCODE_SYM
%token  ENCRYPT
%token  END
%token  ENGINES_SYM
%token  ENGINE_SYM
%token  ENUM
%token  EQ
%token  EQUAL_SYM
%token  ERRORS
%token  ESCAPED
%token  ESCAPE_SYM
%token  EVENTS_SYM
%token  EXECUTE_SYM
%token  EXISTS
614
%token  EXIT_SYM
615 616 617 618 619 620
%token  EXPANSION_SYM
%token  EXPORT_SET
%token  EXTENDED_SYM
%token  EXTRACT_SYM
%token  FALSE_SYM
%token  FAST_SYM
621
%token  FETCH_SYM
622 623 624 625 626 627 628 629 630 631 632
%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
633
%token  FOUND_SYM
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
%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
%token  INNER_SYM
%token  INNOBASE_SYM
681
%token  INOUT_SYM
682 683 684 685 686 687
%token  INSENSITIVE_SYM
%token  INSERT
%token  INSERT_METHOD
%token  INTERVAL_SYM
%token  INTO
%token  INT_SYM
688
%token  INVOKER_SYM
689 690 691 692 693 694 695 696 697 698
%token  IN_SYM
%token  IS
%token  ISOLATION
%token  ISSUER_SYM
%token  ITERATE_SYM
%token  JOIN_SYM
%token  KEYS
%token  KEY_SYM
%token  KILL_SYM
%token  LABEL_SYM
699
%token  LANGUAGE_SYM
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
%token  LAST_INSERT_ID
%token  LAST_SYM
%token  LE
%token  LEADING
%token  LEAST_SYM
%token  LEAVES
%token  LEAVE_SYM
%token  LEFT
%token  LEVEL_SYM
%token  LEX_HOSTNAME
%token  LIKE
%token  LIMIT
%token  LINEFROMTEXT
%token  LINES
%token  LINESTRING
%token  LOAD
%token  LOCAL_SYM
%token  LOCATE
718
%token  LOCATOR_SYM
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
%token  LOCKS_SYM
%token  LOCK_SYM
%token  LOGS_SYM
%token  LOG_SYM
%token  LONGBLOB
%token  LONGTEXT
%token  LONG_NUM
%token  LONG_SYM
%token  LOOP_SYM
%token  LOW_PRIORITY
%token  LT
%token  MAKE_SET_SYM
%token  MASTER_CONNECT_RETRY_SYM
%token  MASTER_HOST_SYM
%token  MASTER_LOG_FILE_SYM
%token  MASTER_LOG_POS_SYM
%token  MASTER_PASSWORD_SYM
%token  MASTER_PORT_SYM
%token  MASTER_POS_WAIT
%token  MASTER_SERVER_ID_SYM
%token  MASTER_SSL_CAPATH_SYM
%token  MASTER_SSL_CA_SYM
%token  MASTER_SSL_CERT_SYM
%token  MASTER_SSL_CIPHER_SYM
%token  MASTER_SSL_KEY_SYM
%token  MASTER_SSL_SYM
%token  MASTER_SYM
%token  MASTER_USER_SYM
%token  MATCH
%token  MAX_CONNECTIONS_PER_HOUR
%token  MAX_QUERIES_PER_HOUR
%token  MAX_ROWS
%token  MAX_SYM
%token  MAX_UPDATES_PER_HOUR
serg@serg.mylan's avatar
serg@serg.mylan committed
753
%token  MAX_USER_CONNECTIONS_SYM
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
%token  MEDIUMBLOB
%token  MEDIUMINT
%token  MEDIUMTEXT
%token  MEDIUM_SYM
%token  MERGE_SYM
%token  MICROSECOND_SYM
%token  MIGRATE_SYM
%token  MINUTE_MICROSECOND_SYM
%token  MINUTE_SECOND_SYM
%token  MINUTE_SYM
%token  MIN_ROWS
%token  MIN_SYM
%token  MLINEFROMTEXT
%token  MODE_SYM
%token  MODIFIES_SYM
%token  MODIFY_SYM
%token  MOD_SYM
%token  MONTH_SYM
%token  MPOINTFROMTEXT
%token  MPOLYFROMTEXT
%token  MULTILINESTRING
%token  MULTIPOINT
%token  MULTIPOLYGON
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
777
%token  MUTEX_SYM
778 779 780 781 782 783
%token  NAMES_SYM
%token  NAME_SYM
%token  NATIONAL_SYM
%token  NATURAL
%token  NCHAR_STRING
%token  NCHAR_SYM
784
%token  NDBCLUSTER_SYM
785 786 787 788 789 790 791 792 793 794 795 796
%token  NE
%token  NEW_SYM
%token  NEXT_SYM
%token  NONE_SYM
%token  NOT2_SYM
%token  NOT_SYM
%token  NOW_SYM
%token  NO_SYM
%token  NO_WRITE_TO_BINLOG
%token  NULL_SYM
%token  NUM
%token  NUMERIC_SYM
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
797
%token  NVARCHAR_SYM
798
%token  OFFSET_SYM
799
%token  OJ_SYM
800 801
%token  OLD_PASSWORD
%token  ON
802
%token  ONE_SHOT_SYM
803 804 805 806 807 808 809 810 811 812 813
%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
814
%token  OUT_SYM
815 816 817
%token  PACK_KEYS_SYM
%token  PARTIAL
%token  PASSWORD
818
%token  PARAM_MARKER
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
%token  PHASE_SYM
%token  POINTFROMTEXT
%token  POINT_SYM
%token  POLYFROMTEXT
%token  POLYGON
%token  POSITION_SYM
%token  PRECISION
%token  PREPARE_SYM
%token  PREV_SYM
%token  PRIMARY_SYM
%token  PRIVILEGES
%token  PROCEDURE
%token  PROCESS
%token  PROCESSLIST_SYM
%token  PURGE
%token  QUARTER_SYM
%token  QUERY_SYM
%token  QUICK
%token  RAID_0_SYM
%token  RAID_CHUNKS
%token  RAID_CHUNKSIZE
%token  RAID_STRIPED_SYM
%token  RAID_TYPE
%token  RAND
%token  READS_SYM
%token  READ_SYM
%token  REAL
%token  RECOVER_SYM
serg@serg.mylan's avatar
serg@serg.mylan committed
847
%token  REDUNDANT_SYM
848 849 850 851 852
%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
853
%token  RELEASE_SYM
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
%token  RELOAD
%token  RENAME
%token  REPAIR
%token  REPEATABLE_SYM
%token  REPEAT_SYM
%token  REPLACE
%token  REPLICATION
%token  REQUIRE_SYM
%token  RESET_SYM
%token  RESOURCES
%token  RESTORE_SYM
%token  RESTRICT
%token  RESUME_SYM
%token  RETURNS_SYM
%token  RETURN_SYM
%token  REVOKE
%token  RIGHT
%token  ROLLBACK_SYM
%token  ROLLUP_SYM
%token  ROUND
serg@serg.mylan's avatar
serg@serg.mylan committed
874
%token  ROUTINE_SYM
875 876 877 878 879 880 881 882
%token  ROWS_SYM
%token  ROW_COUNT_SYM
%token  ROW_FORMAT_SYM
%token  ROW_SYM
%token  RTREE_SYM
%token  SAVEPOINT_SYM
%token  SECOND_MICROSECOND_SYM
%token  SECOND_SYM
883
%token  SECURITY_SYM
884 885
%token  SELECT_SYM
%token  SENSITIVE_SYM
886
%token  SEPARATOR_SYM
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
%token  SERIALIZABLE_SYM
%token  SERIAL_SYM
%token  SESSION_SYM
%token  SET
%token  SET_VAR
%token  SHARE_SYM
%token  SHIFT_LEFT
%token  SHIFT_RIGHT
%token  SHOW
%token  SHUTDOWN
%token  SIGNED_SYM
%token  SIMPLE_SYM
%token  SLAVE
%token  SMALLINT
%token  SNAPSHOT_SYM
%token  SOUNDS_SYM
%token  SPATIAL_SYM
904 905 906 907
%token  SPECIFIC_SYM
%token  SQLEXCEPTION_SYM
%token  SQLSTATE_SYM
%token  SQLWARNING_SYM
908 909 910 911 912 913 914 915
%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
916
%token  SSL_SYM
917 918 919 920
%token  STARTING
%token  START_SYM
%token  STATUS_SYM
%token  STD_SYM
921
%token  STDDEV_SAMP_SYM
922 923 924 925 926 927 928 929 930 931 932
%token  STOP_SYM
%token  STORAGE_SYM
%token  STRAIGHT_JOIN
%token  STRING_SYM
%token  SUBDATE_SYM
%token  SUBJECT_SYM
%token  SUBSTRING
%token  SUBSTRING_INDEX
%token  SUM_SYM
%token  SUPER_SYM
%token  SUSPEND_SYM
933
%token  SYSDATE
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
%token  TABLES
%token  TABLESPACE
%token  TABLE_SYM
%token  TEMPORARY
%token  TEMPTABLE_SYM
%token  TERMINATED
%token  TEXT_STRING
%token  TEXT_SYM
%token  TIMESTAMP
%token  TIMESTAMP_ADD
%token  TIMESTAMP_DIFF
%token  TIME_SYM
%token  TINYBLOB
%token  TINYINT
%token  TINYTEXT
%token  TO_SYM
%token  TRAILING
%token  TRANSACTION_SYM
952
%token  TRIGGER_SYM
953
%token  TRIGGERS_SYM
954 955 956
%token  TRIM
%token  TRUE_SYM
%token  TRUNCATE_SYM
957
%token  TYPES_SYM
958 959 960 961 962 963 964
%token  TYPE_SYM
%token  UDF_RETURNS_SYM
%token  UDF_SONAME_SYM
%token  ULONGLONG_NUM
%token  UNCOMMITTED_SYM
%token  UNDEFINED_SYM
%token  UNDERSCORE_CHARSET
965
%token  UNDO_SYM
966 967 968 969 970 971 972 973 974 975
%token  UNICODE_SYM
%token  UNION_SYM
%token  UNIQUE_SYM
%token  UNIQUE_USERS
%token  UNIX_TIMESTAMP
%token  UNKNOWN_SYM
%token  UNLOCK_SYM
%token  UNSIGNED
%token  UNTIL_SYM
%token  UPDATE_SYM
976
%token  UPGRADE_SYM
977 978 979 980 981 982 983 984
%token  USAGE
%token  USER
%token  USE_FRM
%token  USE_SYM
%token  USING
%token  UTC_DATE_SYM
%token  UTC_TIMESTAMP_SYM
%token  UTC_TIME_SYM
985
%token  VAR_SAMP_SYM
986 987 988 989 990 991 992 993 994 995 996 997
%token  VALUES
%token  VALUE_SYM
%token  VARBINARY
%token  VARCHAR
%token  VARIABLES
%token  VARIANCE_SYM
%token  VARYING
%token  VIEW_SYM
%token  WARNINGS
%token  WEEK_SYM
%token  WHEN_SYM
%token  WHERE
998
%token  WHILE_SYM
999 1000 1001 1002 1003 1004 1005 1006 1007 1008
%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
1009

1010
%left   JOIN_SYM INNER_SYM STRAIGHT_JOIN CROSS LEFT RIGHT
timour@mysql.com's avatar
timour@mysql.com committed
1011 1012
/* 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
1013
%left   SET_VAR
1014 1015
%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
1016 1017 1018 1019 1020 1021
%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	'-' '+'
1022
%left	'*' '/' '%' DIV_SYM MOD_SYM
1023
%left   '^'
monty@mysql.com's avatar
monty@mysql.com committed
1024
%left	NEG '~'
1025
%right	NOT_SYM NOT2_SYM
1026
%right	BINARY COLLATE_SYM
1027

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1028
%type <lex_str>
1029
        IDENT IDENT_QUOTED TEXT_STRING DECIMAL_NUM FLOAT_NUM NUM LONG_NUM HEX_NUM
1030
	LEX_HOSTNAME ULONGLONG_NUM field_ident select_alias ident ident_or_text
1031
        UNDERSCORE_CHARSET IDENT_sys TEXT_STRING_sys TEXT_STRING_literal
1032
	NCHAR_STRING opt_component key_cache_name
bar@mysql.com's avatar
bar@mysql.com committed
1033
        sp_opt_label BIN_NUM label_ident TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1034 1035 1036 1037 1038

%type <lex_str_ptr>
	opt_table_alias

%type <table>
1039
	table_ident table_ident_nodb references xid
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1040 1041

%type <simple_string>
1042
	remember_name remember_end opt_ident opt_db text_or_password
1043
	opt_constraint constraint ident_or_empty
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1044 1045

%type <string>
1046
	text_string opt_gconcat_separator
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1047 1048

%type <num>
1049
	type int_type real_type order_dir lock_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1050
	udf_type if_exists opt_local opt_table_options table_options
1051 1052
        table_option opt_if_not_exists opt_no_write_to_binlog
        delete_option opt_temporary all_or_any opt_distinct
1053
        opt_ignore_leaves fulltext_options spatial_type union_option
serg@serg.mylan's avatar
serg@serg.mylan committed
1054
        start_transaction_opts opt_chain opt_release
1055
        union_opt select_derived_init option_type2
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1056 1057

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

1060 1061
%type <ulonglong_number>
	ulonglong_num
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1062

1063 1064 1065
%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
1066
%type <item>
1067
	literal text_literal insert_ident order_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1068
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
1069
	variable variable_aux bool_term bool_factor bool_test bool_pri 
1070 1071
	predicate bit_expr bit_term bit_factor value_expr term factor
	table_wild simple_expr udf_expr
1072
	expr_or_default set_expr_or_default interval_expr
1073
	param_marker geometry_function
1074
	signed_literal now_or_signed_literal opt_escape
1075 1076
	sp_opt_default
	simple_ident_nospvar simple_ident_q
1077
        field_or_var limit_option
1078 1079 1080

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

%type <item_list>
1083
	expr_list udf_expr_list udf_expr_list2 when_list
1084
	ident_list ident_list_arg opt_expr_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1085

1086 1087 1088
%type <var_type>
        option_type opt_var_type opt_var_ident_type

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1089
%type <key_type>
1090
	key_type opt_unique_or_fulltext constraint_key_type
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1091

1092 1093 1094
%type <key_alg>
	key_alg opt_btree_or_rtree

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1095
%type <string_list>
1096
	key_usage_list using_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1097 1098 1099 1100 1101

%type <key_part>
	key_part

%type <table_list>
1102
	join_table_list  join_table
1103
        table_factor table_ref
1104
        select_derived derived_table_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1105

1106
%type <date_time_type> date_time_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1107 1108
%type <interval> interval

1109 1110
%type <interval_time_st> interval_time_st

1111
%type <db_type> storage_engines
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1112 1113 1114

%type <row_type> row_types

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

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

1119
%type <cast_type> cast_type
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1120

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

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

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

1127
%type <charset>
1128
	opt_collate
1129 1130
	charset_name
	charset_name_or_default
1131 1132
	old_or_new_charset_name
	old_or_new_charset_name_or_default
1133 1134
	collation_name
	collation_name_or_default
1135
	opt_load_data_charset
1136

1137 1138
%type <variable> internal_variable_name

1139
%type <select_lex> subselect subselect_init
1140
	get_select_lex
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1141

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1144
%type <NONE>
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1145
	query verb_clause create change select do drop insert replace insert2
1146
	insert_values update delete truncate rename
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1147
	show describe load alter optimize keycache preload flush
1148
	reset purge begin commit rollback savepoint release
1149
	slave master_def master_defs master_file_def slave_until_opts
1150
	repair restore backup analyze check start checksum
1151
	field_list field_list_item field_spec kill column_def key_def
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1152
	keycache_list assign_to_keycache preload_list preload_keys
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1153
	select_item_list select_item values_list no_braces
1154
	opt_limit_clause delete_limit_clause fields opt_values values
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1155
	procedure_list procedure_list2 procedure_item
1156
	expr_list2 udf_expr_list3 handler
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1157 1158
	opt_precision opt_ignore opt_column opt_restrict
	grant revoke set lock unlock string_list field_options field_option
1159
	field_opt_list opt_binary table_lock_list table_lock
1160
	ref_list opt_on_delete opt_on_delete_list opt_on_delete_item use
1161
	opt_delete_options opt_delete_option varchar nchar nvarchar
1162
	opt_outer table_list table_name opt_option opt_place
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1163
	opt_attribute opt_attribute_list attribute column_list column_list_id
1164
	opt_column_list grant_privileges grant_ident grant_list grant_option
1165
	object_privilege object_privilege_list user_list rename_list
1166
	clear_privileges flush_options flush_option
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1167 1168
	equal optional_braces opt_key_definition key_usage_list2
	opt_mi_check_type opt_to mi_check_types normal_join
1169
	table_to_table_list table_to_table opt_table_list opt_as
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
1170
	handler_rkey_function handler_read_or_scan
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1171
	single_multi table_wild_list table_wild_one opt_wild
1172
	union_clause union_list
1173
	precision subselect_start opt_and charset
1174
	subselect_end select_var_list select_var_list_init help opt_len
1175
	opt_extended_describe
1176
        prepare prepare_src execute deallocate
1177
	statement sp_suid
1178
	sp_c_chistics sp_a_chistics sp_chistic sp_c_chistic xa
1179
        load_data opt_field_or_var_spec fields_or_vars opt_load_data_set_spec
1180
        definer view_replace_or_algorithm view_replace view_algorithm_opt
1181 1182 1183
        view_algorithm view_or_trigger_or_sp view_or_trigger_or_sp_tail
	view_suid view_tail view_list_opt view_list view_select
	view_check_option trigger_tail sp_tail
1184
        case_stmt_specification simple_case_stmt searched_case_stmt
1185
END_OF_INPUT
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1186

1187 1188 1189 1190 1191 1192 1193
%type <NONE> call sp_proc_stmts sp_proc_stmts1 sp_proc_stmt
%type <num>  sp_decl_idents sp_opt_inout sp_handler_type sp_hcond_list
%type <spcondtype> sp_cond sp_hcond
%type <spblock> sp_decls sp_decl
%type <lex> sp_cursor_stmt
%type <spname> sp_name

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1194 1195
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
1196
	',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_SYM BETWEEN_SYM CASE_SYM
1197
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM OR2_SYM AND_AND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1198 1199 1200 1201 1202
%%


query:
	END_OF_INPUT
1203
	{
1204
	   THD *thd= YYTHD;
1205
	   if (!thd->bootstrap &&
1206
	      (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
1207
	   {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1208
	     my_message(ER_EMPTY_QUERY, ER(ER_EMPTY_QUERY), MYF(0));
1209
	     MYSQL_YYABORT;
1210
	   }
1211 1212
	   else
	   {
1213
	     thd->lex->sql_command= SQLCOM_EMPTY_QUERY;
1214
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1215
	}
1216
	| verb_clause END_OF_INPUT {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1217 1218

verb_clause:
1219 1220 1221 1222 1223 1224
	  statement
	| begin
	;

/* Verb clauses, except begin */
statement:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1225 1226
	  alter
	| analyze
1227
	| backup
1228
	| call
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1229 1230
	| change
	| check
1231
	| checksum
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1232 1233
	| commit
	| create
1234
        | deallocate
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1235 1236
	| delete
	| describe
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1237
	| do
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1238
	| drop
1239
        | execute
serg@serg.mylan's avatar
serg@serg.mylan committed
1240
	| flush
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1241
	| grant
serg@serg.mylan's avatar
serg@serg.mylan committed
1242 1243
	| handler
	| help
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1244
	| insert
serg@serg.mylan's avatar
serg@serg.mylan committed
1245
	| kill
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1246 1247 1248
	| load
	| lock
	| optimize
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1249
        | keycache
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1250
	| preload
1251
        | prepare
1252
	| purge
1253
	| release
1254
	| rename
serg@serg.mylan's avatar
serg@serg.mylan committed
1255
	| repair
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1256
	| replace
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1257
	| reset
1258
	| restore
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1259 1260
	| revoke
	| rollback
1261
	| savepoint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1262 1263
	| select
	| set
serg@serg.mylan's avatar
serg@serg.mylan committed
1264
	| show
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1265
	| slave
1266
	| start
1267
	| truncate
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1268 1269
	| unlock
	| update
1270
	| use
1271
	| xa
serg@serg.mylan's avatar
serg@serg.mylan committed
1272
        ;
1273

sergefp@mysql.com's avatar
sergefp@mysql.com committed
1274
deallocate:
1275
        deallocate_or_drop PREPARE_SYM ident
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1276 1277
        {
          THD *thd=YYTHD;
1278
          LEX *lex= thd->lex;
1279
          if (lex->stmt_prepare_mode)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1280
          {
1281 1282
            my_parse_error(ER(ER_SYNTAX_ERROR));
            MYSQL_YYABORT;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1283
          }
1284
          lex->sql_command= SQLCOM_DEALLOCATE_PREPARE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1285 1286 1287
          lex->prepared_stmt_name= $3;
        };

1288 1289 1290 1291 1292 1293
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


sergefp@mysql.com's avatar
sergefp@mysql.com committed
1294
prepare:
1295
        PREPARE_SYM ident FROM prepare_src
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1296 1297
        {
          THD *thd=YYTHD;
1298
          LEX *lex= thd->lex;
1299
          if (lex->stmt_prepare_mode)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1300
          {
1301 1302
            my_parse_error(ER(ER_SYNTAX_ERROR));
            MYSQL_YYABORT;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1303
          }
1304
          lex->sql_command= SQLCOM_PREPARE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1305 1306 1307
          lex->prepared_stmt_name= $2;
        };

1308 1309 1310 1311 1312 1313
prepare_src:
        TEXT_STRING_sys
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $1;
1314
          lex->prepared_stmt_code_is_varref= FALSE;
1315 1316 1317 1318 1319 1320
        }
        | '@' ident_or_text
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $2;
1321
          lex->prepared_stmt_code_is_varref= TRUE;
1322
        };
1323

sergefp@mysql.com's avatar
sergefp@mysql.com committed
1324 1325 1326 1327
execute:
        EXECUTE_SYM ident
        {
          THD *thd=YYTHD;
1328
          LEX *lex= thd->lex;
1329
          if (lex->stmt_prepare_mode)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1330
          {
1331 1332
            my_parse_error(ER(ER_SYNTAX_ERROR));
            MYSQL_YYABORT;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1333
          }
1334
          lex->sql_command= SQLCOM_EXECUTE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
          lex->prepared_stmt_name= $2;
        }
        execute_using
        {}
        ;

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
1347 1348
        execute_var_list ',' execute_var_ident
        | execute_var_ident
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1349 1350 1351 1352 1353 1354 1355
        ;

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))
1356
              MYSQL_YYABORT;
1357
        }
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1358 1359
        ;

1360 1361
/* help */

1362
help:
1363 1364 1365 1366 1367
       HELP_SYM
       {
         if (Lex->sphead)
         {
           my_error(ER_SP_BADSTATEMENT, MYF(0), "HELP");
1368
           MYSQL_YYABORT;
1369 1370 1371
         }
       }
       ident_or_text
1372
       {
1373 1374
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
1375
	  lex->help_arg= $3.str;
1376
       };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1377 1378 1379 1380

/* change master */

change:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1381
       CHANGE MASTER_SYM TO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1382 1383 1384
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
1385
	  bzero((char*) &lex->mi, sizeof(lex->mi));
1386 1387 1388 1389
        }
       master_defs
	{}
       ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1390 1391 1392

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

1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
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
1411
       MASTER_PORT_SYM EQ ulong_num
1412 1413 1414 1415
       {
	 Lex->mi.port = $3;
       }
       |
serg@serg.mylan's avatar
serg@serg.mylan committed
1416
       MASTER_CONNECT_RETRY_SYM EQ ulong_num
1417 1418 1419
       {
	 Lex->mi.connect_retry = $3;
       }
serg@serg.mylan's avatar
serg@serg.mylan committed
1420
       | MASTER_SSL_SYM EQ ulong_num
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
         {
           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
1474
       | RELAY_LOG_POS_SYM EQ ulong_num
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
         {
           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)))
1495
	    MYSQL_YYABORT;
1496
          lex->alter_info.reset();
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
	  lex->col_list.empty();
	  lex->change=NullS;
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	  lex->create_info.options=$2 | $4;
	  lex->create_info.db_type= (enum db_type) lex->thd->variables.table_type;
	  lex->create_info.default_table_charset= NULL;
	  lex->name=0;
	}
	create2
	  { Lex->current_select= &Lex->select_lex; }
	| CREATE opt_unique_or_fulltext INDEX_SYM ident key_alg ON table_ident
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_CREATE_INDEX;
	    if (!lex->current_select->add_table_to_list(lex->thd, $7, NULL,
							TL_OPTION_UPDATING))
1513
	      MYSQL_YYABORT;
1514 1515
            lex->alter_info.reset();
            lex->alter_info.flags= ALTER_ADD_INDEX;
1516 1517 1518 1519 1520 1521
	    lex->col_list.empty();
	    lex->change=NullS;
	  }
	   '(' key_list ')'
	  {
	    LEX *lex=Lex;
1522
            Key *key= new Key($2, $4.str, $5, 0, lex->col_list);
1523

1524
            lex->alter_info.key_list.push_back(key);
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
	    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;
	  }
1539
	| CREATE
1540
	  {
1541 1542 1543
            Lex->create_view_mode= VIEW_CREATE_NEW;
            Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED;
            Lex->create_view_suid= TRUE;
1544
	  }
1545
	  view_or_trigger_or_sp
1546
	  {}
1547 1548 1549 1550
	| CREATE USER clear_privileges grant_list
	  {
	    Lex->sql_command = SQLCOM_CREATE_USER;
          }
1551 1552
	;

1553 1554 1555 1556 1557 1558 1559
clear_privileges:
        /* Nothing */
        {
          LEX *lex=Lex;
          lex->users_list.empty();
          lex->columns.empty();
          lex->grant= lex->grant_tot_col= 0;
1560
	  lex->all_privileges= 0;
1561 1562 1563 1564 1565 1566 1567
          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));
        }
        ;

1568
sp_name:
1569
	  ident '.' ident
1570
	  {
1571 1572 1573
            if (!$1.str || check_db_name($1.str))
            {
	      my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
1574
	      MYSQL_YYABORT;
1575
	    }
1576
	    if (check_routine_name($3))
1577 1578
            {
	      my_error(ER_SP_WRONG_NAME, MYF(0), $3.str);
1579
	      MYSQL_YYABORT;
1580
	    }
1581
	    $$= new sp_name($1, $3, true);
1582 1583
	    $$->init_qname(YYTHD);
	  }
1584
	| ident
1585
	  {
1586 1587
            THD *thd= YYTHD;
            LEX_STRING db;
1588
	    if (check_routine_name($1))
1589 1590
            {
	      my_error(ER_SP_WRONG_NAME, MYF(0), $1.str);
1591
	      MYSQL_YYABORT;
1592
	    }
1593
            if (thd->copy_db_to(&db.str, &db.length))
1594
              MYSQL_YYABORT;
1595
	    $$= new sp_name(db, $1, false);
1596 1597
            if ($$)
	      $$->init_qname(YYTHD);
1598 1599 1600 1601 1602 1603 1604
	  }
	;

create_function_tail:
	  RETURNS_SYM udf_type UDF_SONAME_SYM TEXT_STRING_sys
	  {
	    LEX *lex=Lex;
1605 1606 1607 1608 1609 1610 1611 1612 1613
            if (lex->definer != NULL)
            {
              /*
                 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.
              */
	      my_error(ER_WRONG_USAGE, MYF(0), "SONAME", "DEFINER");
1614
              MYSQL_YYABORT;
1615
            }
1616 1617 1618 1619 1620 1621 1622
	    lex->sql_command = SQLCOM_CREATE_FUNCTION;
	    lex->udf.name = lex->spname->m_name;
	    lex->udf.returns=(Item_result) $2;
	    lex->udf.dl=$4.str;
	  }
	| '('
	  {
1623 1624 1625
            THD *thd= YYTHD;
	    LEX *lex= thd->lex;
            Lex_input_stream *lip= thd->m_lip;
1626 1627
	    sp_head *sp;

1628 1629 1630 1631 1632 1633 1634
            /* 
              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));
1635
              MYSQL_YYABORT;
1636 1637
            }

1638 1639
	    if (lex->sphead)
	    {
1640
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1641
	      MYSQL_YYABORT;
1642 1643 1644
	    }
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
1645
	    sp->reset_thd_mem_root(thd);
1646
	    sp->init(lex);
1647
            sp->init_sp_name(thd, lex->spname);
1648 1649 1650 1651 1652 1653 1654 1655

	    sp->m_type= TYPE_ENUM_FUNCTION;
	    lex->sphead= sp;
	    /*
	     * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	     * stored procedure, otherwise yylex will chop it into pieces
	     * at each ';'.
	     */
1656 1657 1658
	    sp->m_old_cmq= thd->client_capabilities & CLIENT_MULTI_QUERIES;
	    thd->client_capabilities &= ~CLIENT_MULTI_QUERIES;
	    lex->sphead->m_param_begin= lip->tok_start+1;
1659 1660 1661
	  }
          sp_fdparam_list ')'
	  {
1662 1663 1664
            THD *thd= YYTHD;
	    LEX *lex= thd->lex;
            Lex_input_stream *lip= thd->m_lip;
1665

1666
	    lex->sphead->m_param_end= lip->tok_start;
1667 1668 1669 1670
	  }
	  RETURNS_SYM
	  {
	    LEX *lex= Lex;
1671 1672 1673 1674
	    lex->charset= NULL;
	    lex->length= lex->dec= NULL;
	    lex->interval_list.empty();
	    lex->type= 0;
1675 1676 1677 1678 1679
	  }
	  type
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
1680

1681 1682 1683
            if (sp->fill_field_definition(YYTHD, lex,
                                          (enum enum_field_types) $8,
                                          &sp->m_return_field_def))
1684
              MYSQL_YYABORT;
1685 1686 1687 1688 1689

	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	  }
	  sp_c_chistics
	  {
1690 1691 1692
            THD *thd= YYTHD;
	    LEX *lex= thd->lex;
            Lex_input_stream *lip= thd->m_lip;
1693 1694

	    lex->sphead->m_chistics= &lex->sp_chistics;
1695
	    lex->sphead->m_body_begin= lip->tok_start;
1696 1697 1698 1699 1700 1701
	  }
	  sp_proc_stmt
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;

1702
            if (sp->is_not_allowed_in_function("function"))
1703
              MYSQL_YYABORT;
1704

1705
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
1706
	    sp->init_strings(YYTHD, lex);
1707 1708 1709
            if (!(sp->m_flags & sp_head::HAS_RETURN))
            {
              my_error(ER_SP_NORETURN, MYF(0), sp->m_qname.str);
1710
              MYSQL_YYABORT;
1711
            }
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
	    /* Restore flag if it was cleared above */
	    if (sp->m_old_cmq)
	      YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
	    sp->restore_thd_mem_root(YYTHD);
	  }
	;

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

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

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

/* Create characteristics */
sp_c_chistic:
	  sp_chistic            { }
	| DETERMINISTIC_SYM     { Lex->sp_chistics.detistic= TRUE; }
1751
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
	;

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();
1773
	    sp_add_used_routine(lex, YYTHD, $2, TYPE_ENUM_PROCEDURE);
1774
	  }
1775
          opt_sp_cparam_list {}
1776 1777 1778
	;

/* CALL parameters */
1779
opt_sp_cparam_list:
1780
	  /* Empty */
1781
	| '(' opt_sp_cparams ')'
1782 1783
	;

1784 1785 1786 1787 1788
opt_sp_cparams:
          /* Empty */
        | sp_cparams
        ;

1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
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
	;

1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
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;
	  }
	;

1831
sp_fdparam:
1832
	  ident sp_init_param type
1833 1834 1835 1836
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1837
	    if (spc->find_variable(&$1, TRUE))
1838
	    {
1839
	      my_error(ER_SP_DUP_PARAM, MYF(0), $1.str);
1840
	      MYSQL_YYABORT;
1841
	    }
1842 1843 1844
            sp_variable_t *spvar= spc->push_variable(&$1,
                                                     (enum enum_field_types)$3,
                                                     sp_param_in);
1845 1846 1847

            if (lex->sphead->fill_field_definition(YYTHD, lex,
                                                   (enum enum_field_types) $3,
1848
                                                   &spvar->field_def))
1849
            {
1850
              MYSQL_YYABORT;
1851
            }
1852 1853
            spvar->field_def.field_name= spvar->name.str;
            spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
1869
	  sp_opt_inout sp_init_param ident type
1870 1871 1872 1873
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1874
	    if (spc->find_variable(&$3, TRUE))
1875
	    {
1876
	      my_error(ER_SP_DUP_PARAM, MYF(0), $3.str);
1877
	      MYSQL_YYABORT;
1878
	    }
1879 1880 1881
            sp_variable_t *spvar= spc->push_variable(&$3,
                                                     (enum enum_field_types)$4,
                                                     (sp_param_mode_t)$1);
1882 1883 1884

            if (lex->sphead->fill_field_definition(YYTHD, lex,
                                                   (enum enum_field_types) $4,
1885
                                                   &spvar->field_def))
1886
            {
1887
              MYSQL_YYABORT;
1888
            }
1889 1890
            spvar->field_def.field_name= spvar->name.str;
            spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
	  }
	;

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 */ {}
1903
	| sp_proc_stmts  sp_proc_stmt ';'
1904 1905 1906 1907
	;

sp_proc_stmts1:
	  sp_proc_stmt ';' {}
1908
	| sp_proc_stmts1  sp_proc_stmt ';'
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
	;

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
1924 1925
	      my_message(ER_SP_VARCOND_AFTER_CURSHNDLR,
                         ER(ER_SP_VARCOND_AFTER_CURSHNDLR), MYF(0));
1926
	      MYSQL_YYABORT;
1927 1928 1929
	    }
	    if ($2.curs && $1.hndlrs)
	    { /* Cursor following handler */
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1930 1931
	      my_message(ER_SP_CURSOR_AFTER_HANDLER,
                         ER(ER_SP_CURSOR_AFTER_HANDLER), MYF(0));
1932
	      MYSQL_YYABORT;
1933 1934 1935 1936 1937 1938 1939 1940 1941
	    }
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
	    $$.curs= $1.curs + $2.curs;
	  }
	;

sp_decl:
1942
          DECLARE_SYM sp_decl_idents
1943
          {
1944
            LEX *lex= Lex;
1945

1946 1947 1948
            lex->sphead->reset_lex(YYTHD);
            lex->spcont->declare_var_boundary($2);
          }
1949
          type
1950
          sp_opt_default
1951 1952
          {
            LEX *lex= Lex;
1953
            sp_pcontext *pctx= lex->spcont;
1954
            uint num_vars= pctx->context_var_count();
1955 1956 1957 1958
            enum enum_field_types var_type= (enum enum_field_types) $4;
            Item *dflt_value_item= $5;
            
            if (!dflt_value_item)
1959
            {
1960 1961 1962 1963 1964 1965
              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++)
            {
1966 1967
              uint var_idx= pctx->var_context2runtime(i);
              sp_variable_t *spvar= pctx->find_variable(var_idx);
1968
            
1969
              if (!spvar)
1970
                MYSQL_YYABORT;
1971
            
1972 1973
              spvar->type= var_type;
              spvar->dflt= dflt_value_item;
1974 1975
            
              if (lex->sphead->fill_field_definition(YYTHD, lex, var_type,
1976
                                                     &spvar->field_def))
1977
              {
1978
                MYSQL_YYABORT;
1979 1980
              }
            
1981 1982
              spvar->field_def.field_name= spvar->name.str;
              spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1983 1984 1985 1986 1987 1988 1989
            
              /* 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)));
1990
            }
1991 1992

            pctx->declare_var_boundary(0);
1993
            lex->sphead->restore_lex(YYTHD);
1994

1995 1996 1997
            $$.vars= $2;
            $$.conds= $$.hndlrs= $$.curs= 0;
          }
1998 1999 2000 2001 2002 2003 2004
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
2005
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
2006
	      MYSQL_YYABORT;
2007 2008 2009 2010 2011 2012 2013 2014 2015
	    }
	    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;
2016 2017 2018

            lex->spcont= lex->spcont->push_context(LABEL_HANDLER_SCOPE);

2019 2020 2021
	    sp_pcontext *ctx= lex->spcont;
	    sp_instr_hpush_jump *i=
              new sp_instr_hpush_jump(sp->instructions(), ctx, $2,
2022
	                              ctx->current_var_count());
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037

	    sp->add_instr(i);
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
	  }
	  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,
2038
	                              ctx->current_var_count());
2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
	      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);
2049 2050 2051

            lex->spcont= ctx->pop_context();

2052 2053
	    $$.vars= $$.conds= $$.curs= 0;
	    $$.hndlrs= $6;
2054
	    lex->spcont->add_handlers($6);
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
	  }
	| 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))
	    {
2066
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
2067
	      delete $5;
2068
	      MYSQL_YYABORT;
2069
	    }
pem@mysql.com's avatar
pem@mysql.com committed
2070
            i= new sp_instr_cpush(sp->instructions(), ctx, $5,
2071
                                  ctx->current_cursor_count());
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
	    sp->add_instr(i);
	    ctx->push_cursor(&$2);
	    $$.vars= $$.conds= $$.hndlrs= 0;
	    $$.curs= 1;
	  }
	;

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

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

	    if (lex->sql_command != SQLCOM_SELECT)
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2094 2095
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
2096
	      MYSQL_YYABORT;
2097 2098 2099
	    }
	    if (lex->result)
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2100 2101
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
2102
	      MYSQL_YYABORT;
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
	    }
	    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:
2117 2118 2119 2120 2121 2122 2123
          sp_hcond_element
          { $$= 1; }
        | sp_hcond_list ',' sp_hcond_element
          { $$+= 1; }
        ;

sp_hcond_element:
2124 2125 2126 2127
	  sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
2128
	    sp_pcontext *ctx= lex->spcont->parent_context();
2129 2130 2131 2132

	    if (ctx->find_handler($1))
	    {
	      my_message(ER_SP_DUP_HANDLER, ER(ER_SP_DUP_HANDLER), MYF(0));
2133
	      MYSQL_YYABORT;
2134 2135 2136 2137 2138
	    }
	    else
	    {
	      sp_instr_hpush_jump *i=
                (sp_instr_hpush_jump *)sp->last_instruction();
2139

2140 2141 2142
	      i->add_condition($1);
	      ctx->push_handler($1);
	    }
2143 2144 2145 2146
	  }
	;

sp_cond:
serg@serg.mylan's avatar
serg@serg.mylan committed
2147
	  ulong_num
2148 2149 2150 2151 2152 2153 2154
	  {			/* 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 */
2155 2156 2157
	    if (!sp_cond_check(&$3))
	    {
	      my_error(ER_SP_BAD_SQLSTATE, MYF(0), $3.str);
2158
	      MYSQL_YYABORT;
2159
	    }
2160 2161
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
2162 2163
	    memcpy($$->sqlstate, $3.str, 5);
	    $$->sqlstate[5]= '\0';
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
	  }
	;

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
2182
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
2183
	      MYSQL_YYABORT;
2184 2185 2186 2187 2188 2189 2190
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
2191
	| not FOUND_SYM		/* SQLSTATEs 02??? */
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
	  {
	    $$= (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
	  {
2206 2207
            /* NOTE: field definition is filled in sp_decl section. */

2208 2209 2210
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

2211
	    if (spc->find_variable(&$1, TRUE))
2212
	    {
2213
	      my_error(ER_SP_DUP_VAR, MYF(0), $1.str);
2214
	      MYSQL_YYABORT;
2215
	    }
2216
	    spc->push_variable(&$1, (enum_field_types)0, sp_param_in);
2217 2218 2219 2220
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
2221 2222
            /* NOTE: field definition is filled in sp_decl section. */

2223 2224 2225
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

2226
	    if (spc->find_variable(&$3, TRUE))
2227
	    {
2228
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
2229
	      MYSQL_YYABORT;
2230
	    }
2231
	    spc->push_variable(&$3, (enum_field_types)0, sp_param_in);
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
	    $$= $1 + 1;
	  }
	;

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

sp_proc_stmt:
	  {
2243 2244 2245
            THD *thd= YYTHD;
	    LEX *lex= thd->lex;
            Lex_input_stream *lip= thd->m_lip;
2246

2247 2248
	    lex->sphead->reset_lex(thd);
	    lex->sphead->m_tmp_query= lip->tok_start;
2249 2250 2251
	  }
	  statement
	  {
2252 2253 2254
            THD *thd= YYTHD;
	    LEX *lex= thd->lex;
            Lex_input_stream *lip= thd->m_lip;
2255 2256
	    sp_head *sp= lex->sphead;

2257
            sp->m_flags|= sp_get_flags_for_command(lex);
2258 2259
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
2260
	      my_error(ER_SP_BADSTATEMENT, MYF(0), "USE");
2261
	      MYSQL_YYABORT;
2262
	    }
2263 2264 2265 2266
	    /*
              Don't add an instruction for SET statements, since all
              instructions for them were already added during processing
              of "set" rule.
2267
	    */
2268 2269 2270
            DBUG_ASSERT(lex->sql_command != SQLCOM_SET_OPTION ||
                        lex->var_list.is_empty());
            if (lex->sql_command != SQLCOM_SET_OPTION)
2271
	    {
2272 2273
              sp_instr_stmt *i=new sp_instr_stmt(sp->instructions(),
                                                 lex->spcont, lex);
2274

2275 2276 2277 2278 2279 2280
              /*
                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)
2281
                i->m_query.length= lip->ptr - sp->m_tmp_query;
2282
              else
2283 2284 2285
                i->m_query.length= lip->tok_end - sp->m_tmp_query;
              i->m_query.str= strmake_root(thd->mem_root,
                                           sp->m_tmp_query,
2286 2287
                                           i->m_query.length);
              sp->add_instr(i);
2288
            }
2289
	    sp->restore_lex(thd);
2290
          }
2291 2292 2293
          | RETURN_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr
2294 2295
	  {
	    LEX *lex= Lex;
2296
	    sp_head *sp= lex->sphead;
2297

2298
	    if (sp->m_type != TYPE_ENUM_FUNCTION)
2299
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2300
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
2301
	      MYSQL_YYABORT;
2302 2303 2304 2305 2306
	    }
	    else
	    {
	      sp_instr_freturn *i;

2307 2308
	      i= new sp_instr_freturn(sp->instructions(), lex->spcont, $3,
                                      sp->m_return_field_def.sql_type, lex);
2309
	      sp->add_instr(i);
2310
	      sp->m_flags|= sp_head::HAS_RETURN;
2311
	    }
2312
	    sp->restore_lex(YYTHD);
2313
	  }
2314 2315 2316 2317
        | IF
          { Lex->sphead->new_cont_backpatch(NULL); }
          sp_if END IF
          { Lex->sphead->do_cont_backpatch(); }
2318
        | case_stmt_specification
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
	| sp_labeled_control
	  {}
	| { /* Unlabeled controls get a secret label. */
	    LEX *lex= Lex;

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

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
2332
	| LEAVE_SYM label_ident
2333 2334 2335 2336 2337 2338 2339 2340
	  {
	    LEX *lex= Lex;
	    sp_head *sp = lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab)
	    {
2341
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
2342
	      MYSQL_YYABORT;
2343 2344 2345 2346
	    }
	    else
	    {
	      sp_instr_jump *i;
2347 2348 2349
	      uint ip= sp->instructions();
	      uint n;

2350
	      n= ctx->diff_handlers(lab->ctx, TRUE);  /* Exclusive the dest. */
2351 2352
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
2353
	      n= ctx->diff_cursors(lab->ctx, TRUE);  /* Exclusive the dest. */
2354 2355
	      if (n)
	        sp->add_instr(new sp_instr_cpop(ip++, ctx, n));
2356 2357 2358 2359 2360
	      i= new sp_instr_jump(ip, ctx);
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
	    }
	  }
2361
	| ITERATE_SYM label_ident
2362 2363 2364 2365 2366 2367 2368 2369
	  {
	    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)
	    {
2370
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "ITERATE", $2.str);
2371
	      MYSQL_YYABORT;
2372 2373 2374 2375 2376 2377 2378
	    }
	    else
	    {
	      sp_instr_jump *i;
	      uint ip= sp->instructions();
	      uint n;

2379
	      n= ctx->diff_handlers(lab->ctx, FALSE);  /* Inclusive the dest. */
2380 2381
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
2382
	      n= ctx->diff_cursors(lab->ctx, FALSE);  /* Inclusive the dest. */
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
	      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);
	    }
	  }
	| OPEN_SYM ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_copen *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2398
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2399
	      MYSQL_YYABORT;
2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
	    }
	    i= new sp_instr_copen(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
	| FETCH_SYM sp_opt_fetch_noise ident INTO
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cfetch *i;

	    if (! lex->spcont->find_cursor(&$3, &offset))
	    {
2413
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $3.str);
2414
	      MYSQL_YYABORT;
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
	    }
	    i= new sp_instr_cfetch(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
	  sp_fetch_list
	  { }
	| CLOSE_SYM ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cclose *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2430
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2431
	      MYSQL_YYABORT;
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
	    }
	    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;
2450
	    sp_variable_t *spv;
2451

2452
	    if (!spc || !(spv = spc->find_variable(&$1)))
2453
	    {
2454
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2455
	      MYSQL_YYABORT;
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
	    }
	    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;
2471
	    sp_variable_t *spv;
2472

2473
	    if (!spc || !(spv = spc->find_variable(&$3)))
2474
	    {
2475
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $3.str);
2476
	      MYSQL_YYABORT;
2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	    }
	  }
	;

sp_if:
2489 2490
          { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2491 2492 2493 2494 2495
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= sp->instructions();
2496 2497
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, ctx,
                                                               $2, lex);
2498 2499

	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
2500
            sp->add_cont_backpatch(i);
2501
            sp->add_instr(i);
2502
            sp->restore_lex(YYTHD);
2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
	  }
	  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
	;

2529 2530 2531 2532
case_stmt_specification:
          simple_case_stmt
        | searched_case_stmt
        ;
2533

2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544
simple_case_stmt:
          CASE_SYM
          {
            LEX *lex= Lex;
            case_stmt_action_case(lex);
            lex->sphead->reset_lex(YYTHD); /* For expr $3 */
          }
          expr
          {
            LEX *lex= Lex;
            if (case_stmt_action_expr(lex, $3))
2545
              MYSQL_YYABORT;
2546

2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
            lex->sphead->restore_lex(YYTHD); /* For expr $3 */
          }
          simple_when_clause_list
          else_clause_opt
          END
          CASE_SYM
          {
            LEX *lex= Lex;
            case_stmt_action_end_case(lex, true);
          }
        ;
2558

2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
searched_case_stmt:
          CASE_SYM
          {
            LEX *lex= Lex;
            case_stmt_action_case(lex);
          }
          searched_when_clause_list
          else_clause_opt
          END
          CASE_SYM
          {
            LEX *lex= Lex;
            case_stmt_action_end_case(lex, false);
          }
        ;
2574

2575 2576 2577 2578
simple_when_clause_list:
          simple_when_clause
        | simple_when_clause_list simple_when_clause
        ;
2579

2580 2581 2582 2583
searched_when_clause_list:
          searched_when_clause
        | searched_when_clause_list searched_when_clause
        ;
2584

2585 2586 2587 2588 2589 2590 2591 2592
simple_when_clause:
          WHEN_SYM
          {
            Lex->sphead->reset_lex(YYTHD); /* For expr $3 */
          }
          expr
          {
            /* Simple case: <caseval> = <whenval> */
2593

2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
            LEX *lex= Lex;
            case_stmt_action_when(lex, $3, true);
            lex->sphead->restore_lex(YYTHD); /* For expr $3 */
          }
          THEN_SYM
          sp_proc_stmts1
          {
            LEX *lex= Lex;
            case_stmt_action_then(lex);
          }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2605

2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623
searched_when_clause:
          WHEN_SYM
          {
            Lex->sphead->reset_lex(YYTHD); /* For expr $3 */
          }
          expr
          {
            LEX *lex= Lex;
            case_stmt_action_when(lex, $3, false);
            lex->sphead->restore_lex(YYTHD); /* For expr $3 */
          }
          THEN_SYM
          sp_proc_stmts1
          {
            LEX *lex= Lex;
            case_stmt_action_then(lex);
          }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2624

2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
else_clause_opt:
          /* empty */
          {
            LEX *lex= Lex;
            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);
            sp->add_instr(i);
          }
        | ELSE sp_proc_stmts1
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2637

2638
sp_labeled_control:
2639
	  label_ident ':'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2640
	  {
2641 2642 2643 2644 2645 2646
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);

	    if (lab)
	    {
2647
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $1.str);
2648
	      MYSQL_YYABORT;
2649 2650 2651 2652 2653 2654 2655
	    }
	    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
2656
	  }
2657
	  sp_unlabeled_control sp_opt_label
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2658
	  {
2659
	    LEX *lex= Lex;
2660

2661 2662 2663 2664 2665 2666 2667
	    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)
	      {
2668
	        my_error(ER_SP_LABEL_MISMATCH, MYF(0), $5.str);
2669
	        MYSQL_YYABORT;
2670 2671 2672
	      }
	    }
	    lex->sphead->backpatch(lex->spcont->pop_label());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2673
	  }
2674 2675 2676
	;

sp_opt_label:
2677
        /* Empty  */    { $$= null_lex_str; }
2678
        | label_ident   { $$= $1; }
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
	;

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

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

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; }
2776
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2777 2778

create2:
serg@serg.mylan's avatar
serg@serg.mylan committed
2779 2780 2781 2782 2783
        '(' create2a {}
        | opt_create_table_options create3 {}
        | LIKE table_ident
          {
            LEX *lex=Lex;
2784
            THD *thd= lex->thd;
serg@serg.mylan's avatar
serg@serg.mylan committed
2785
            if (!(lex->name= (char *)$2))
2786
              MYSQL_YYABORT;
2787 2788 2789
            if ($2->db.str == NULL &&
                thd->copy_db_to(&($2->db.str), &($2->db.length)))
            {
2790
              MYSQL_YYABORT;
2791
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
2792 2793 2794 2795
          }
        | '(' LIKE table_ident ')'
          {
            LEX *lex=Lex;
2796
            THD *thd= lex->thd;
serg@serg.mylan's avatar
serg@serg.mylan committed
2797
            if (!(lex->name= (char *)$3))
2798
              MYSQL_YYABORT;
2799 2800 2801
            if ($3->db.str == NULL &&
                thd->copy_db_to(&($3->db.str), &($3->db.length)))
            {
2802
              MYSQL_YYABORT;
2803
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
2804
          }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2805
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2806

serg@serg.mylan's avatar
serg@serg.mylan committed
2807 2808
create2a:
        field_list ')' opt_create_table_options create3 {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2809
	|  create_select ')' { Select->set_braces(1);} union_opt {}
2810
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2811 2812

create3:
2813
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
2814
	| opt_duplicate opt_as     create_select
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2815
          { Select->set_braces(0);} union_clause {}
serg@serg.mylan's avatar
serg@serg.mylan committed
2816
	| opt_duplicate opt_as '(' create_select ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2817
          { Select->set_braces(1);} union_opt {}
2818 2819
        ;

serg@serg.mylan's avatar
serg@serg.mylan committed
2820
create_select:
2821
          SELECT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2822
          {
2823
	    LEX *lex=Lex;
2824
	    lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
2825 2826 2827 2828
	    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;
2829 2830 2831 2832
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
2833
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
2834
	    mysql_init_select(lex);
2835
	    lex->current_select->parsing_place= SELECT_LIST;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2836
          }
2837 2838
          select_options select_item_list
	  {
2839
	    Select->parsing_place= NO_MATTER;
2840
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2841
	  opt_select_from
2842 2843 2844 2845 2846 2847 2848
	  {
	    /*
              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
2849
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2850

2851 2852
opt_as:
	/* empty */ {}
2853
	| AS	    {};
2854

2855 2856 2857 2858 2859 2860 2861 2862 2863
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
2864 2865
	default_collation   {}
	| default_charset   {};
2866

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2867
opt_table_options:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2868
	/* empty */	 { $$= 0; }
2869
	| table_options  { $$= $1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2870 2871 2872

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2875
table_option:
2876
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2877 2878

opt_if_not_exists:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2879
	/* empty */	 { $$= 0; }
2880
	| IF not EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2881 2882 2883

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

2886 2887 2888 2889
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
2890 2891
create_table_options:
	create_table_option
2892
	| create_table_option     create_table_options
2893
	| create_table_option ',' create_table_options;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2894 2895

create_table_option:
2896 2897
	ENGINE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; }
	| TYPE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; WARN_DEPRECATED("TYPE=storage_engine","ENGINE=storage_engine");   Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; }
2898 2899
	| 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
2900
	| 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;}
2901
	| PASSWORD opt_equal TEXT_STRING_sys	{ Lex->create_info.password=$3.str; Lex->create_info.used_fields|= HA_CREATE_USED_PASSWORD; }
2902
	| COMMENT_SYM opt_equal TEXT_STRING_sys	{ Lex->create_info.comment=$3; Lex->create_info.used_fields|= HA_CREATE_USED_COMMENT; }
2903
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913
        | 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:
2914 2915
                my_parse_error(ER(ER_SYNTAX_ERROR));
                MYSQL_YYABORT;
2916 2917 2918 2919 2920 2921 2922 2923 2924
            }
            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
2925 2926
	| 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; }
2927
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
2928 2929 2930
	| RAID_TYPE opt_equal raid_types
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_TYPE", "PARTITION");
2931
	    MYSQL_YYABORT;
2932 2933 2934 2935
	  }
	| RAID_CHUNKS opt_equal ulong_num
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_CHUNKS", "PARTITION");
2936
	    MYSQL_YYABORT;
2937 2938 2939 2940
	  }
	| RAID_CHUNKSIZE opt_equal ulong_num
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_CHUNKSIZE", "PARTITION");
2941
	    MYSQL_YYABORT;
2942
	  }
2943
	| UNION_SYM opt_equal '(' table_list ')'
2944 2945 2946
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
2947 2948
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
2949
	    lex->create_info.merge_list.elements--;
2950 2951
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
2952
	    lex->select_lex.table_list.elements=1;
2953 2954 2955
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
2956
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
2957
	  }
2958 2959
	| default_charset
	| default_collation
2960
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
2961 2962
	| 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; }
2963
	| 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; }
2964
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2965

2966 2967 2968 2969 2970 2971 2972 2973
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))
          {
2974 2975 2976
            my_error(ER_CONFLICTING_DECLARATIONS, MYF(0),
                     "CHARACTER SET ", cinfo->default_table_charset->csname,
                     "CHARACTER SET ", $4->csname);
2977
            MYSQL_YYABORT;
2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990
          }
	  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))
            {
2991 2992
              my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $4->name, cinfo->default_table_charset->csname);
2993
              MYSQL_YYABORT;
2994 2995 2996 2997 2998
            }
            Lex->create_info.default_table_charset= $4;
            Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

2999
storage_engines:
3000 3001 3002 3003
	ident_or_text
	{
	  $$ = ha_resolve_by_name($1.str,$1.length);
	  if ($$ == DB_TYPE_UNKNOWN) {
3004
	    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
3005
	    MYSQL_YYABORT;
3006 3007
	  }
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3008 3009 3010 3011 3012

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
3013 3014 3015
	| 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
3016 3017 3018 3019

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

3022 3023 3024
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
3025
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
3026

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3027
opt_select_from:
3028
	opt_limit_clause {}
3029
	| select_from select_lock_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3030 3031

udf_func_type:
3032
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
3033
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3034 3035 3036 3037

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
3038
        | DECIMAL_SYM {$$ = (int) DECIMAL_RESULT; }
3039
	| INT_SYM {$$ = (int) INT_RESULT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3040 3041 3042

field_list:
	  field_list_item
3043
	| field_list ',' field_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3044 3045 3046


field_list_item:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3047
	   column_def
3048 3049 3050 3051
         | key_def
         ;

column_def:
3052
	  field_spec opt_check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3053 3054 3055 3056
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3057
	;
3058 3059

key_def:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3060
	key_type opt_ident key_alg '(' key_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3061
	  {
3062
	    LEX *lex=Lex;
3063 3064 3065
            Key *key= new Key($1, $2, $3, 0, lex->col_list);
            lex->alter_info.key_list.push_back(key);

3066
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3067
	  }
3068 3069 3070 3071
	| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
	  {
	    LEX *lex=Lex;
	    const char *key_name= $3 ? $3:$1;
3072 3073
            Key *key= new Key($2, key_name, $4, 0, lex->col_list);
            lex->alter_info.key_list.push_back(key);
3074 3075
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
3076
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3077
	  {
3078
	    LEX *lex=Lex;
3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090
            const char *key_name= $4 ? $4 : $1;
            Key *key= new foreign_key(key_name, lex->col_list,
                                      $8,
                                      lex->ref_list,
                                      lex->fk_delete_opt,
                                      lex->fk_update_opt,
                                      lex->fk_match_option);
            lex->alter_info.key_list.push_back(key);
            key= new Key(Key::MULTIPLE, key_name,
                         HA_KEY_ALG_UNDEF, 1,
                         lex->col_list);
            lex->alter_info.key_list.push_back(key);
3091
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3092
	  }
3093 3094 3095 3096
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
3097
	| opt_constraint check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3098 3099
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
3100 3101 3102
	  }
	;

3103
opt_check_constraint:
3104
	/* empty */
3105 3106 3107 3108 3109
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
3110
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3111 3112

opt_constraint:
3113
	/* empty */		{ $$=(char*) 0; }
monty@mysql.com's avatar
monty@mysql.com committed
3114 3115 3116 3117 3118 3119
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3120 3121 3122 3123

field_spec:
	field_ident
	 {
3124
	   LEX *lex=Lex;
3125
	   lex->length=lex->dec=0; lex->type=0;
3126
	   lex->default_value= lex->on_update_value= 0;
3127
           lex->comment=null_lex_str;
3128
	   lex->charset=NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3129 3130 3131
	 }
	type opt_attribute
	{
3132
	  LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3133
	  if (add_field_to_list(lex->thd, $1.str,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3134
				(enum enum_field_types) $3,
3135
				lex->length,lex->dec,lex->type,
3136
				lex->default_value, lex->on_update_value, 
3137
                                &lex->comment,
3138
				lex->change,&lex->interval_list,lex->charset,
3139
				lex->uint_geom_type))
3140
	    MYSQL_YYABORT;
3141
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3142 3143

type:
3144
	int_type opt_len field_options	{ $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3145 3146
	| 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
3147 3148 3149 3150
	| 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
3151 3152
	| BOOL_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
3153 3154
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
3155
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3156 3157 3158
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3159
	| nchar '(' NUM ')' opt_bin_mod	{ Lex->length=$3.str;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3160
					  $$=FIELD_TYPE_STRING;
3161
					  Lex->charset=national_charset_info; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3162
	| nchar opt_bin_mod		{ Lex->length=(char*) "1";
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3163
					  $$=FIELD_TYPE_STRING;
3164
					  Lex->charset=national_charset_info; }
3165
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
3166
					  Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3167
					  $$=FIELD_TYPE_STRING; }
3168 3169 3170
	| BINARY			{ Lex->length= (char*) "1";
					  Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_STRING; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3171
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
3172
					  $$= MYSQL_TYPE_VARCHAR; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3173
	| nvarchar '(' NUM ')' opt_bin_mod { Lex->length=$3.str;
3174
					  $$= MYSQL_TYPE_VARCHAR;
3175
					  Lex->charset=national_charset_info; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3176
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
3177
					  Lex->charset=&my_charset_bin;
3178
					  $$= MYSQL_TYPE_VARCHAR; }
3179
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3180 3181
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
3182
	| TIMESTAMP opt_len
3183
	  {
3184
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
3185 3186
	      $$=FIELD_TYPE_DATETIME;
	    else
3187 3188 3189 3190 3191
            {
              /* 
                Unlike other types TIMESTAMP fields are NOT NULL by default.
              */
              Lex->type|= NOT_NULL_FLAG;
3192
	      $$=FIELD_TYPE_TIMESTAMP;
3193
            }
3194
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3195
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
3196
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3197
					  $$=FIELD_TYPE_TINY_BLOB; }
3198
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3199
					  $$=FIELD_TYPE_BLOB; }
3200 3201
	| spatial_type
          {
hf@deer.(none)'s avatar
hf@deer.(none) committed
3202
#ifdef HAVE_SPATIAL
3203 3204 3205
            Lex->charset=&my_charset_bin;
            Lex->uint_geom_type= (uint)$1;
            $$=FIELD_TYPE_GEOMETRY;
hf@deer.(none)'s avatar
hf@deer.(none) committed
3206
#else
serg@serg.mylan's avatar
serg@serg.mylan committed
3207
            my_error(ER_FEATURE_DISABLED, MYF(0),
3208
                     sym_group_geom.name, sym_group_geom.needed_define);
3209
            MYSQL_YYABORT;
hf@deer.(none)'s avatar
hf@deer.(none) committed
3210
#endif
3211
          }
3212
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3213
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
3214
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3215
					  $$=FIELD_TYPE_LONG_BLOB; }
3216
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3217
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
3218 3219
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
3220
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
3221 3222
	| 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
3223
	| DECIMAL_SYM float_options field_options
3224
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3225
	| NUMERIC_SYM float_options field_options
3226
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
3227
	| FIXED_SYM float_options field_options
3228
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
3229
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
3230
	  { $$=FIELD_TYPE_ENUM; }
3231
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
3232
	  { $$=FIELD_TYPE_SET; }
3233
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
3234 3235 3236 3237 3238 3239
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
3240
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3241

hf@deer.(none)'s avatar
hf@deer.(none) committed
3242 3243 3244
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
3245 3246 3247
	| POINT_SYM           { Lex->length= (char*)"21";
                                $$= Field::GEOM_POINT;
                              }
hf@deer.(none)'s avatar
hf@deer.(none) committed
3248 3249 3250 3251 3252 3253 3254
	| 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
3255 3256
char:
	CHAR_SYM {}
3257 3258 3259 3260 3261 3262
	;

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3264 3265 3266
varchar:
	char VARYING {}
	| VARCHAR {}
3267 3268 3269 3270
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3271
	| NVARCHAR_SYM {}
3272 3273 3274 3275
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3276 3277 3278 3279 3280 3281

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

real_type:
3285
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3286 3287
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
3288
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3289 3290 3291


float_options:
3292 3293
        /* empty */		{ Lex->dec=Lex->length= (char*)0; }
        | '(' NUM ')'		{ Lex->length=$2.str; Lex->dec= (char*)0; }
3294
	| precision		{};
3295 3296 3297 3298 3299 3300

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

field_options:
	/* empty */		{}
3305
	| field_opt_list	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3306 3307 3308

field_opt_list:
	field_opt_list field_option {}
3309
	| field_option {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3310 3311

field_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3312
	SIGNED_SYM	{}
3313
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
3314
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3315 3316

opt_len:
3317 3318
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3319 3320 3321

opt_precision:
	/* empty */	{}
3322
	| precision	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3323 3324 3325

opt_attribute:
	/* empty */ {}
3326
	| opt_attribute_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3327 3328 3329

opt_attribute_list:
	opt_attribute_list attribute {}
3330
	| attribute;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3331 3332 3333

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
3334
	| not NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
3335 3336 3337
	| 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
3338
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
3339
	| SERIAL_SYM DEFAULT VALUE_SYM
3340 3341 3342
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
3343
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3344 3345 3346 3347 3348
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
3349
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3350 3351 3352 3353 3354
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
3355
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3356 3357 3358 3359 3360
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
3361
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3362
	  }
3363
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= $2; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3364 3365
	| COLLATE_SYM collation_name
	  {
3366
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3367
	    {
3368 3369
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $2->name,Lex->charset->csname);
3370
	      MYSQL_YYABORT;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3371 3372 3373 3374 3375 3376 3377
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3378

3379 3380 3381 3382 3383
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

3384 3385 3386 3387
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
3388

3389
charset_name:
3390
	ident_or_text
3391
	{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3392
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
3393
	  {
3394
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
3395
	    MYSQL_YYABORT;
3396
	  }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3397 3398 3399
	}
	| BINARY { $$= &my_charset_bin; }
	;
3400

3401 3402 3403
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3404

3405 3406 3407 3408
opt_load_data_charset:
	/* Empty */ { $$= NULL; }
	| charset charset_name_or_default { $$= $2; }
	;
3409 3410 3411 3412 3413 3414 3415

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)))
	  {
3416
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
3417
	    MYSQL_YYABORT;
3418 3419 3420 3421 3422 3423 3424 3425 3426
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

3427
collation_name:
3428
	ident_or_text
3429 3430 3431
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
3432
	    my_error(ER_UNKNOWN_COLLATION, MYF(0), $1.str);
3433
	    MYSQL_YYABORT;
3434 3435 3436
	  }
	};

3437 3438
opt_collate:
	/* empty */	{ $$=NULL; }
3439
	| COLLATE_SYM collation_name_or_default { $$=$2; }
3440 3441
	;

3442 3443 3444 3445
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

3446 3447 3448 3449
opt_default:
	/* empty */	{}
	| DEFAULT	{};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3450
opt_binary:
3451
	/* empty */			{ Lex->charset=NULL; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3452
	| ASCII_SYM opt_bin_mod		{ Lex->charset=&my_charset_latin1; }
3453
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3454 3455 3456 3457 3458 3459
	| 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");
3460
	    MYSQL_YYABORT;
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3461 3462 3463 3464 3465 3466 3467 3468 3469 3470
	  }
	}
	| 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:
3471
        /* empty */ { Lex->charset= NULL; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3472
	| ASCII_SYM	{ Lex->charset=&my_charset_latin1; }
3473 3474
	| UNICODE_SYM
	{
3475 3476
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
3477
	  {
3478
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
3479
	    MYSQL_YYABORT;
3480 3481
	  }
	}
3482
	| charset charset_name	{ Lex->charset=$2; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3483

3484 3485 3486
opt_primary:
	/* empty */
	| PRIMARY_SYM
3487
	;
3488

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3489
references:
3490 3491
	REFERENCES table_ident
	{
3492
	  LEX *lex=Lex;
3493 3494 3495 3496 3497 3498
	  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
3499
	};
3500

3501
opt_ref_list:
3502
	/* empty */ opt_on_delete {}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3503
	| '(' ref_list ')' opt_on_delete {};
3504 3505 3506

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3509 3510 3511

opt_on_delete:
	/* empty */ {}
3512
	| opt_on_delete_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3513 3514 3515

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
3516
	| opt_on_delete_item {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3517 3518

opt_on_delete_item:
3519 3520 3521 3522
	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
3523
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3524 3525

delete_option:
3526 3527 3528 3529
	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
3530
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3531 3532

key_type:
3533
	key_or_index			    { $$= Key::MULTIPLE; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
3534 3535
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
hf@deer.(none)'s avatar
hf@deer.(none) committed
3536 3537 3538 3539
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
3540 3541
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
3542
	    MYSQL_YYABORT;
hf@deer.(none)'s avatar
hf@deer.(none) committed
3543 3544
#endif
	  };
3545 3546 3547

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
3548
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3549 3550 3551

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

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
3554 3555 3556 3557 3558
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3559 3560
keys_or_index:
	KEYS {}
3561
	| INDEX_SYM {}
3562
	| INDEXES {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3563

3564
opt_unique_or_fulltext:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3565 3566
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
serg@serg.mylan's avatar
serg@serg.mylan committed
3567
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
hf@deer.(none)'s avatar
hf@deer.(none) committed
3568 3569 3570 3571 3572
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
serg@serg.mylan's avatar
serg@serg.mylan committed
3573
            my_error(ER_FEATURE_DISABLED, MYF(0),
3574
                     sym_group_geom.name, sym_group_geom.needed_define);
3575
	    MYSQL_YYABORT;
hf@deer.(none)'s avatar
hf@deer.(none) committed
3576 3577
#endif
	  }
serg@serg.mylan's avatar
serg@serg.mylan committed
3578
        ;
3579 3580

key_alg:
3581
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
3582
	| USING opt_btree_or_rtree { $$= $2; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3583
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
3584 3585 3586

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
hf@deer.(none)'s avatar
hf@deer.(none) committed
3587 3588 3589 3590
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3591
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3592 3593 3594

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
3595
	| key_part order_dir		{ Lex->col_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3596 3597 3598

key_part:
	ident			{ $$=new key_part_spec($1.str); }
3599 3600 3601 3602 3603
	| ident '(' NUM ')'	
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
3604
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
3605 3606 3607
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3608 3609 3610

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

3613
opt_component:
3614 3615
        /* empty */      { $$= null_lex_str; }
        | '.' ident      { $$= $2; };
3616

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3617 3618
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
3619
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3620 3621 3622 3623 3624 3625 3626 3627

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
3628
	  THD *thd= YYTHD;
3629
	  LEX *lex= thd->lex;
3630 3631 3632
	  lex->sql_command= SQLCOM_ALTER_TABLE;
	  lex->name= 0;
	  lex->duplicates= DUP_ERROR; 
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3633 3634
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
3635
	    MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3636
	  lex->col_list.empty();
3637
          lex->select_lex.init_order();
3638 3639 3640
	  lex->select_lex.db=
            ((TABLE_LIST*) lex->select_lex.table_list.first)->db;
          lex->name=0;
3641
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3642
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
3643
	  lex->create_info.default_table_charset= NULL;
3644
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
3645
          lex->alter_info.reset();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3646
	}
3647 3648
	alter_list
	{}
3649
	| ALTER DATABASE ident_or_empty
3650 3651 3652 3653 3654
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
          opt_create_database_options
3655 3656
	  {
	    LEX *lex=Lex;
3657
            THD *thd= Lex->thd;
3658
	    lex->sql_command=SQLCOM_ALTER_DB;
3659
	    lex->name= $3;
3660
            if (lex->name == NULL && thd->copy_db_to(&lex->name, NULL))
3661
              MYSQL_YYABORT;
3662 3663 3664 3665 3666
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

3667 3668 3669
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
3670
	      MYSQL_YYABORT;
3671
	    }
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683
	    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;
3684

3685 3686 3687
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
3688
	      MYSQL_YYABORT;
3689
	    }
3690 3691 3692 3693 3694 3695 3696 3697 3698
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    LEX *lex=Lex;

	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
	    lex->spname= $3;
	  }
3699 3700
        | ALTER view_algorithm_opt definer view_suid
          VIEW_SYM table_ident
3701 3702 3703 3704 3705 3706
	  {
	    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 */
3707
	    lex->select_lex.add_table_to_list(thd, $6, NULL, TL_OPTION_UPDATING);
3708
	  }
3709
	  view_list_opt AS view_select view_check_option
3710 3711
	  {}
	;
3712

3713 3714 3715 3716
ident_or_empty:
	/* empty */  { $$= 0; }
	| ident      { $$= $1.str; };

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3717
alter_list:
3718 3719
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
3720
        | alter_list_item
3721
	| alter_list ',' alter_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3722 3723

add_column:
3724
	ADD opt_column
3725 3726
	{
	  LEX *lex=Lex;
3727 3728
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
3729
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3730 3731

alter_list_item:
3732 3733 3734 3735
	add_column column_def opt_place { }
	| ADD key_def
	  {
	    Lex->alter_info.flags|= ALTER_ADD_INDEX;
3736
	  }
3737 3738 3739 3740
	| add_column '(' field_list ')'
          {
	    Lex->alter_info.flags|= ALTER_ADD_COLUMN | ALTER_ADD_INDEX;
          }
3741 3742 3743
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
3744
	     lex->change= $3.str;
3745
	     lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
3746
	  }
3747
          field_spec opt_place
3748 3749 3750
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
3751
            lex->length=lex->dec=0; lex->type=0;
3752
            lex->default_value= lex->on_update_value= 0;
3753
            lex->comment=null_lex_str;
3754
	    lex->charset= NULL;
3755
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
3756 3757 3758 3759
          }
          type opt_attribute
          {
            LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3760
            if (add_field_to_list(lex->thd,$3.str,
3761 3762
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
3763
                                  lex->default_value, lex->on_update_value,
3764
                                  &lex->comment,
3765
				  $3.str, &lex->interval_list, lex->charset,
3766
				  lex->uint_geom_type))
3767
	       MYSQL_YYABORT;
3768 3769
          }
          opt_place
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3770
	| DROP opt_column field_ident opt_restrict
3771 3772
	  {
	    LEX *lex=Lex;
3773
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
3774
                                                               $3.str));
3775
	    lex->alter_info.flags|= ALTER_DROP_COLUMN;
3776
	  }
3777 3778 3779 3780
	| DROP FOREIGN KEY_SYM opt_ident
          {
	    Lex->alter_info.flags|= ALTER_DROP_INDEX;
          }
3781 3782 3783
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
3784 3785 3786
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
				               primary_key_name));
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
3787
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3788
	| DROP key_or_index field_ident
3789 3790
	  {
	    LEX *lex=Lex;
3791 3792 3793
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
					                       $3.str));
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
3794
	  }
3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806
	| 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;
          }
3807
	| ALTER opt_column field_ident SET DEFAULT signed_literal
3808 3809
	  {
	    LEX *lex=Lex;
3810
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,$6));
3811
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
3812
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3813
	| ALTER opt_column field_ident DROP DEFAULT
3814 3815
	  {
	    LEX *lex=Lex;
3816 3817
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,
                                                                  (Item*) 0));
3818
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
3819
	  }
3820
	| RENAME opt_to table_ident
3821
	  {
3822
	    LEX *lex=Lex;
3823
            THD *thd= lex->thd;
3824
	    lex->select_lex.db=$3->db.str;
3825 3826 3827
            if (lex->select_lex.db == NULL &&
                thd->copy_db_to(&lex->select_lex.db, NULL))
            {
3828
              MYSQL_YYABORT;
3829
            }
3830 3831 3832
            if (check_table_name($3->table.str,$3->table.length) ||
                $3->db.str && check_db_name($3->db.str))
            {
3833
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3->table.str);
3834
              MYSQL_YYABORT;
3835
            }
3836
	    lex->name= $3->table.str;
3837
	    lex->alter_info.flags|= ALTER_RENAME;
3838
	  }
3839 3840 3841 3842 3843 3844 3845 3846 3847 3848
	| 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))
	    {
3849 3850
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $5->name, $4->csname);
3851
	      MYSQL_YYABORT;
3852
	    }
3853
	    LEX *lex= Lex;
3854
	    lex->create_info.table_charset=
monty@mysql.com's avatar
monty@mysql.com committed
3855 3856 3857
	      lex->create_info.default_table_charset= $5;
	    lex->create_info.used_fields|= (HA_CREATE_USED_CHARSET |
					    HA_CREATE_USED_DEFAULT_CHARSET);
3858
	    lex->alter_info.flags|= ALTER_CONVERT;
3859
	  }
3860
        | create_table_options_space_separated
3861 3862
	  {
	    LEX *lex=Lex;
3863
	    lex->alter_info.flags|= ALTER_OPTIONS;
3864
	  }
3865 3866 3867 3868
	| FORCE_SYM
	  {
	    Lex->alter_info.flags|= ALTER_FORCE;
	   }
malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
3869
	| alter_order_clause
3870 3871
	  {
	    LEX *lex=Lex;
3872
	    lex->alter_info.flags|= ALTER_ORDER;
3873
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3874 3875 3876

opt_column:
	/* empty */	{}
3877
	| COLUMN_SYM	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3878 3879

opt_ignore:
3880 3881 3882
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3883 3884

opt_restrict:
3885 3886 3887 3888
	/* 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
3889 3890 3891 3892

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
3893
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3894 3895 3896 3897

opt_to:
	/* empty */	{}
	| TO_SYM	{}
3898
	| EQ		{}
3899
	| AS		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3900

nick@mysql.com's avatar
nick@mysql.com committed
3901
/*
3902
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3903 3904
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3905
slave:
3906
	  START_SYM SLAVE slave_thread_opts
3907 3908 3909 3910 3911 3912
          {
	    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
3913
            /* If you change this code don't forget to update SLAVE START too */
3914 3915 3916
          }
          slave_until
          {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3917 3918 3919 3920 3921
        | 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
3922
            /* If you change this code don't forget to update SLAVE STOP too */
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3923
          }
3924 3925 3926 3927 3928
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
3929 3930 3931 3932 3933
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
3934 3935 3936 3937 3938 3939 3940 3941
	| 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
3942

3943
start:
3944 3945
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
3946 3947 3948
          LEX *lex= Lex;
          lex->sql_command= SQLCOM_BEGIN;
          lex->start_transaction_opt= $3;
3949
        }
3950 3951
	;

3952 3953 3954 3955 3956 3957
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
3958
        ;
3959

3960
slave_thread_opts:
3961 3962
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
3963
        {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3964
	;
3965 3966

slave_thread_opt_list:
3967
	slave_thread_opt
3968 3969
	| slave_thread_opt_list ',' slave_thread_opt
	;
3970 3971

slave_thread_opt:
3972
	/*empty*/	{}
3973
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
3974
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
3975
	;
3976

3977 3978 3979 3980 3981 3982 3983 3984 3985 3986
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
3987 3988
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
3989
               MYSQL_YYABORT;
3990 3991 3992 3993 3994 3995 3996 3997 3998 3999
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


4000 4001 4002 4003 4004
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4005
	table_list FROM TEXT_STRING_sys
4006 4007
        {
	  Lex->backup_dir = $6.str;
4008
        };
4009

4010 4011 4012 4013 4014
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4015
	table_list TO_SYM TEXT_STRING_sys
4016 4017
        {
	  Lex->backup_dir = $6.str;
4018
        };
4019

4020 4021 4022 4023 4024 4025
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
4026 4027
	table_list opt_checksum_type
        {}
4028 4029
	;

4030 4031 4032 4033 4034 4035
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
4036
repair:
4037
	REPAIR opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4038
	{
4039 4040
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
4041
           lex->no_write_to_binlog= $2;
4042
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4043
	}
4044 4045 4046
	table_list opt_mi_repair_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4047

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4048
opt_mi_repair_type:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4049
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
4050
	| mi_repair_types {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4051

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4052 4053
mi_repair_types:
	mi_repair_type {}
4054
	| mi_repair_type mi_repair_types {};
4055

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4056
mi_repair_type:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4057
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
4058
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
4059
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4060 4061

analyze:
4062
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4063
	{
4064 4065
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
4066
           lex->no_write_to_binlog= $2;
4067
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4068
	}
4069 4070 4071
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4072 4073

check:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4074
	CHECK_SYM table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4075
	{
4076 4077 4078 4079 4080
	  LEX *lex=Lex;

	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "CHECK");
4081
	    MYSQL_YYABORT;
4082 4083 4084
	  }
	  lex->sql_command = SQLCOM_CHECK;
	  lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4085
	}
4086 4087 4088
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4089

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4090 4091
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
4092
	| mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4093 4094 4095

mi_check_types:
	mi_check_type {}
4096
	| mi_check_type mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4097 4098 4099 4100 4101 4102

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; }
4103 4104
	| 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
4105

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4106
optimize:
4107
	OPTIMIZE opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4108
	{
4109 4110
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4111
           lex->no_write_to_binlog= $2;
4112
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4113
	}
4114 4115 4116
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4117

4118 4119 4120
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
4121
	| LOCAL_SYM  { $$= 1; }
4122 4123
	;

4124 4125 4126
rename:
	RENAME table_or_tables
	{
4127
          Lex->sql_command= SQLCOM_RENAME_TABLE;
4128
	}
4129 4130
	table_to_table_list
	{}
4131 4132 4133 4134
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
4135
	;
4136

4137 4138 4139 4140
rename_list:
        user TO_SYM user
        {
          if (Lex->users_list.push_back($1) || Lex->users_list.push_back($3))
4141
            MYSQL_YYABORT;
4142 4143 4144 4145
        }
        | rename_list ',' user TO_SYM user
          {
            if (Lex->users_list.push_back($3) || Lex->users_list.push_back($5))
4146
              MYSQL_YYABORT;
4147 4148 4149
          }
        ;

4150 4151
table_to_table_list:
	table_to_table
4152
	| table_to_table_list ',' table_to_table;
4153 4154 4155

table_to_table:
	table_ident TO_SYM table_ident
4156
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4157
	  LEX *lex=Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4158
	  SELECT_LEX *sl= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4159 4160 4161 4162
	  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))
4163
	    MYSQL_YYABORT;
4164
	};
4165

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4166
keycache:
4167
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4168 4169
        {
          LEX *lex=Lex;
4170
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
4171
	  lex->ident= $5;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4172 4173 4174 4175 4176 4177 4178 4179
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
4180
        table_ident cache_keys_spec
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4181 4182 4183 4184 4185 4186
        {
          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(),
4187
                                      (List<String> *)0))
4188
            MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4189 4190 4191
        }
        ;

4192 4193 4194
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
4195
	;
4196

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4197
preload:
4198
	LOAD INDEX_SYM INTO CACHE_SYM
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211
	{
	  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
4212
	table_ident cache_keys_spec opt_ignore_leaves
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4213 4214 4215
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4216 4217
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4218 4219
                                      sel->get_use_index(),
                                      (List<String> *)0))
4220
            MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4221 4222 4223
	}
	;

4224
cache_keys_spec:
4225
        { Select->interval_list.empty(); }
4226 4227 4228 4229 4230 4231 4232
        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
4233

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4234
cache_key_list_or_empty:
4235
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
4236
	| opt_key_or_index '(' key_usage_list2 ')'
4237 4238 4239 4240
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4241 4242
	;

4243
opt_ignore_leaves:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4244 4245 4246 4247 4248
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4249
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4250
  Select : retrieve data from table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4251 4252 4253 4254
*/


select:
4255 4256 4257 4258
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4259 4260
	}
	;
4261

4262
/* Need select_init2 for subselects. */
4263
select_init:
4264
	SELECT_SYM select_init2
4265
	|
4266 4267 4268 4269
	'(' select_paren ')' union_opt;

select_paren:
	SELECT_SYM select_part2
4270
	  {
4271
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4272
            SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4273
	    if (sel->set_braces(1))
4274
	    {
4275 4276
              my_parse_error(ER(ER_SYNTAX_ERROR));
	      MYSQL_YYABORT;
4277
	    }
4278 4279 4280 4281 4282
            if (sel->linkage == UNION_TYPE &&
                !sel->master_unit()->first_select()->braces &&
                sel->master_unit()->first_select()->linkage ==
                UNION_TYPE)
            {
4283 4284
              my_parse_error(ER(ER_SYNTAX_ERROR));
              MYSQL_YYABORT;
4285
            }
4286
            /* select in braces, can't contain global parameters */
4287 4288 4289
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
4290 4291
          }
	| '(' select_paren ')';
4292

4293 4294
select_init2:
	select_part2
4295
        {
4296
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4297
          SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4298
          if (lex->current_select->set_braces(0))
4299
	  {
4300 4301
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
4302
	  }
4303 4304
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4305
	  {
4306 4307
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4308
	  }
4309
	}
4310
	union_clause
4311 4312
	;

4313
select_part2:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4314
	{
4315 4316
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
4317 4318
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
4319
	  lex->current_select->parsing_place= SELECT_LIST;
4320 4321 4322
	}
	select_options select_item_list
	{
4323
	  Select->parsing_place= NO_MATTER;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4324
	}
4325
	select_into select_lock_type;
4326

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4327
select_into:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4328
	opt_order_clause opt_limit_clause {}
4329
        | into
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4330
	| select_from
4331 4332
	| into select_from
	| select_from into;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4333 4334

select_from:
4335
        FROM join_table_list where_clause group_clause having_clause
4336
	       opt_order_clause opt_limit_clause procedure_clause
4337 4338 4339 4340 4341
          {
            Select->context.table_list=
              Select->context.first_name_resolution_table= 
                (TABLE_LIST *) Select->table_list.first;
          }
4342
        | FROM DUAL_SYM where_clause opt_limit_clause
timour@mysql.com's avatar
timour@mysql.com committed
4343 4344 4345 4346
          /* 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 :) */
4347
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4348 4349 4350

select_options:
	/* empty*/
monty@mysql.com's avatar
monty@mysql.com committed
4351 4352 4353 4354
	| select_option_list
	  {
	    if (test_all_bits(Select->options, SELECT_ALL | SELECT_DISTINCT))
	    {
monty@mysql.com's avatar
monty@mysql.com committed
4355
	      my_error(ER_WRONG_USAGE, MYF(0), "ALL", "DISTINCT");
4356
              MYSQL_YYABORT;
monty@mysql.com's avatar
monty@mysql.com committed
4357 4358
	    }
          }
monty@mysql.com's avatar
monty@mysql.com committed
4359
	  ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4360 4361 4362

select_option_list:
	select_option_list select_option
4363
	| select_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4364 4365

select_option:
4366
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
4367 4368 4369
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
4370
	      MYSQL_YYABORT;
4371 4372
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
monty@mysql.com's avatar
monty@mysql.com committed
4373
	| DISTINCT         { Select->options|= SELECT_DISTINCT; }
4374 4375
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
4376 4377 4378
	| SQL_BUFFER_RESULT
	  {
	    if (check_simple_select())
4379
	      MYSQL_YYABORT;
4380 4381 4382 4383 4384
	    Select->options|= OPTION_BUFFER_RESULT;
	  }
	| SQL_CALC_FOUND_ROWS
	  {
	    if (check_simple_select())
4385
	      MYSQL_YYABORT;
4386 4387
	    Select->options|= OPTION_FOUND_ROWS;
	  }
4388 4389 4390 4391 4392 4393
	| 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;
          }
4394 4395
	| SQL_CACHE_SYM
	  {
4396 4397 4398 4399 4400 4401 4402
            /* 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;
            }
4403
	  }
monty@mysql.com's avatar
monty@mysql.com committed
4404
	| ALL		    { Select->options|= SELECT_ALL; }
4405
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4406

monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
4407 4408 4409
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
4410 4411
	  {
	    LEX *lex=Lex;
4412
	    lex->current_select->set_lock_for_tables(TL_WRITE);
4413
	    lex->safe_to_cache_query=0;
4414
	  }
4415
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
4416 4417
	  {
	    LEX *lex=Lex;
4418 4419
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
4420
	    lex->safe_to_cache_query=0;
4421 4422
	  }
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
4423

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4424 4425 4426 4427 4428
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
4429
	    THD *thd= YYTHD;
4430 4431 4432 4433
	    if (add_item_to_list(thd,
                                 new Item_field(&thd->lex->current_select->
                                                context,
                                                NULL, NULL, "*")))
4434
	      MYSQL_YYABORT;
4435
	    (thd->lex->current_select->with_wild)++;
4436
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4437 4438 4439 4440 4441


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
4442 4443 4444 4445
            THD *thd= YYTHD;
            DBUG_ASSERT($1 < $3);

	    if (add_item_to_list(thd, $2))
4446
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4447
	    if ($4.str)
4448 4449
            {
              $2->is_autogenerated_name= FALSE;
4450
	      $2->set_name($4.str, $4.length, system_charset_info);
4451
            }
4452 4453 4454
	    else if (!$2->name)
            {
	      $2->set_name($1, (uint) ($3 - $1), thd->charset());
4455
	    }
4456
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4457

4458

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4459
remember_name:
4460 4461 4462 4463 4464
	{
          THD *thd= YYTHD;
          Lex_input_stream *lip= thd->m_lip;
          $$= (char*) lip->tok_start;
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4465 4466

remember_end:
4467 4468 4469 4470 4471
	{
          THD *thd= YYTHD;
          Lex_input_stream *lip= thd->m_lip;
          $$=(char*) lip->tok_end;
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4472 4473 4474

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

select_alias:
4478
	/* empty */		{ $$=null_lex_str;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4479 4480 4481 4482 4483
	| 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
4484 4485 4486

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4489
/* all possible expressions */
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
4490
expr:	
4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512
	  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); }
        ;
4513 4514

bool_term:
4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537
	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); }
        ;
4538 4539 4540 4541 4542 4543

bool_factor:
	NOT_SYM bool_factor	{ $$= negate_expression(YYTHD, $2); }
	| bool_test ;

bool_test:
4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555
          bool_pri IS TRUE_SYM
          { $$= new (YYTHD->mem_root) Item_func_istrue($1); }
        | bool_pri IS not TRUE_SYM
          { $$= new (YYTHD->mem_root) Item_func_isnottrue($1); }
        | bool_pri IS FALSE_SYM
          { $$= new (YYTHD->mem_root) Item_func_isfalse($1); }
        | bool_pri IS not FALSE_SYM
          { $$= new (YYTHD->mem_root) Item_func_isnotfalse($1); }
        | bool_pri IS UNKNOWN_SYM { $$= new Item_func_isnull($1); }
        | bool_pri IS not UNKNOWN_SYM { $$= new Item_func_isnotnull($1); }
        | bool_pri
        ;
4556 4557 4558 4559

bool_pri:
	bool_pri IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| bool_pri IS not NULL_SYM { $$= new Item_func_isnotnull($1); }
4560 4561 4562
	| bool_pri EQUAL_SYM predicate	{ $$= new Item_func_equal($1,$3); }
	| bool_pri comp_op predicate %prec EQ
	  { $$= (*$2)(0)->create($1,$3); }
4563 4564
	| bool_pri comp_op all_or_any '(' subselect ')' %prec EQ
	  { $$= all_any_subquery_creator($1, $2, $3, $5); }
4565 4566 4567
	| predicate ;

predicate:
4568 4569 4570 4571 4572 4573 4574 4575 4576 4577
          bit_expr IN_SYM '(' subselect ')'
          {
            $$= new (YYTHD->mem_root) Item_in_subselect($1, $4);
          }
        | bit_expr not IN_SYM '(' subselect ')'
          {
            THD *thd= YYTHD;
            Item *item= new (thd->mem_root) Item_in_subselect($1, $5);
            $$= negate_expression(thd, item);
          }
4578 4579
        | bit_expr IN_SYM '(' expr ')'
          {
4580
            $$= handle_sql2003_note184_exception(YYTHD, $1, true, $4);
4581
          }
4582 4583 4584 4585 4586
        | bit_expr IN_SYM '(' expr ',' expr_list ')'
          { 
            $6->push_front($4);
            $6->push_front($1);
            $$= new (YYTHD->mem_root) Item_func_in(*$6);
4587
          }
4588
        | bit_expr not IN_SYM '(' expr ')'
4589
          {
4590
            $$= handle_sql2003_note184_exception(YYTHD, $1, false, $5);
4591
          }
4592
        | bit_expr not IN_SYM '(' expr ',' expr_list ')'
4593
          {
4594 4595 4596 4597 4598
            $7->push_front($5);
            $7->push_front($1);
            Item_func_in *item = new (YYTHD->mem_root) Item_func_in(*$7);
            item->negate();
            $$= item;
4599
          }
4600 4601 4602
	| 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
4603 4604 4605 4606 4607
    {
      Item_func_between *item= new Item_func_between($1,$4,$6);
      item->negate();
      $$= item;
    }
4608 4609 4610 4611
	| 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
4612
          { $$= new Item_func_like($1,$3,$4,Lex->escape_used); }
4613
	| bit_expr not LIKE simple_expr opt_escape
4614
          { $$= new Item_func_not(new Item_func_like($1,$4,$5, Lex->escape_used)); }
4615 4616
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
4617
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659
	| 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
4660

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672
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; }
        ;

4673
interval_expr:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4674
         INTERVAL_SYM expr { $$=$2; }
4675 4676
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4677 4678
simple_expr:
	simple_ident
4679
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4680
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4681 4682 4683
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
4684
                                                            YYTHD->charset()));
4685
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4686
	| literal
4687
	| param_marker
4688
	| variable
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4689
	| sum_expr
4690 4691 4692 4693 4694 4695
	| 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); }
4696 4697 4698 4699
	| '(' subselect ')'   
          { 
            $$= new Item_singlerow_subselect($2); 
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4700
	| '(' expr ')'		{ $$= $2; }
4701 4702 4703 4704 4705
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
4706
	| ROW_SYM '(' expr ',' expr_list ')'
4707
	  {
4708 4709
	    $5->push_front($3);
	    $$= new Item_row(*$5);
4710
	  }
4711 4712 4713 4714
	| EXISTS '(' subselect ')' 
          {
            $$= new Item_exists_subselect($3); 
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4715
	| '{' ident expr '}'	{ $$= $3; }
4716
        | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
4717
          { $2->push_front($5);
4718 4719
            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
4720
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
4721
	| BINARY simple_expr %prec NEG
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4722
	  {
4723
            $$= 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
4724
	  }
4725
	| CAST_SYM '(' expr AS cast_type ')'
4726
	  {
4727
            LEX *lex= Lex;
4728
	    $$= create_func_cast($3, $5,
4729 4730 4731
                                 lex->length ? atoi(lex->length) : -1,
                                 lex->dec ? atoi(lex->dec) : 0,
                                 lex->charset);
4732
            if (!$$)
4733
              MYSQL_YYABORT;
4734
	  }
4735 4736
	| CASE_SYM opt_expr when_list opt_else END
	  { $$= new Item_func_case(* $3, $2, $4 ); }
4737
	| CONVERT_SYM '(' expr ',' cast_type ')'
4738 4739 4740
	  {
	    $$= create_func_cast($3, $5,
				 Lex->length ? atoi(Lex->length) : -1,
4741
                                 Lex->dec ? atoi(Lex->dec) : 0,
4742
				 Lex->charset);
4743
            if (!$$)
4744
              MYSQL_YYABORT;
4745
	  }
4746 4747
	| 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
4748
	| DEFAULT '(' simple_ident ')'
4749 4750 4751 4752 4753
	  {
	    if ($3->is_splocal())
	    {
	      Item_splocal *il= static_cast<Item_splocal *>($3);

4754
	      my_error(ER_WRONG_COLUMN_NAME, MYF(0), il->my_name()->str);
4755
	      MYSQL_YYABORT;
4756
	    }
4757
	    $$= new Item_default_value(Lex->current_context(), $3);
4758
	  }
4759
	| VALUES '(' simple_ident_nospvar ')'
4760
	  { $$= new Item_insert_value(Lex->current_context(), $3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4761
	| FUNC_ARG0 '(' ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
4762 4763 4764
	  {
	    if (!$1.symbol->create_func)
	    {
4765 4766 4767
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
4768
	      MYSQL_YYABORT;
hf@deer.(none)'s avatar
hf@deer.(none) committed
4769 4770 4771
	    }
	    $$= ((Item*(*)(void))($1.symbol->create_func))();
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4772
	| FUNC_ARG1 '(' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
4773 4774 4775
	  {
	    if (!$1.symbol->create_func)
	    {
4776 4777 4778
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
4779
	      MYSQL_YYABORT;
hf@deer.(none)'s avatar
hf@deer.(none) committed
4780 4781 4782
	    }
	    $$= ((Item*(*)(Item*))($1.symbol->create_func))($3);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4783
	| FUNC_ARG2 '(' expr ',' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
4784 4785 4786
	  {
	    if (!$1.symbol->create_func)
	    {
4787 4788 4789
	      my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
4790
	      MYSQL_YYABORT;
hf@deer.(none)'s avatar
hf@deer.(none) committed
4791 4792 4793
	    }
	    $$= ((Item*(*)(Item*,Item*))($1.symbol->create_func))($3,$5);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4794
	| FUNC_ARG3 '(' expr ',' expr ',' expr ')'
hf@deer.(none)'s avatar
hf@deer.(none) committed
4795 4796 4797
	  {
	    if (!$1.symbol->create_func)
	    {
4798 4799 4800
              my_error(ER_FEATURE_DISABLED, MYF(0),
                       $1.symbol->group->name,
                       $1.symbol->group->needed_define);
4801
	      MYSQL_YYABORT;
hf@deer.(none)'s avatar
hf@deer.(none) committed
4802 4803 4804
	    }
	    $$= ((Item*(*)(Item*,Item*,Item*))($1.symbol->create_func))($3,$5,$7);
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4805 4806 4807 4808
	| 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); }
4809 4810
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_repeat($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4811 4812 4813 4814 4815 4816
	| 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); }
4817 4818
	| CHAR_SYM '(' expr_list USING charset_name ')'
	  { $$= new Item_func_char(*$3, $5); }
4819 4820
	| CHARSET '(' expr ')'
	  { $$= new Item_func_charset($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4821 4822
	| COALESCE '(' expr_list ')'
	  { $$= new Item_func_coalesce(* $3); }
4823 4824
	| COLLATION_SYM '(' expr ')'
	  { $$= new Item_func_collation($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4825 4826 4827
	| CONCAT '(' expr_list ')'
	  { $$= new Item_func_concat(* $3); }
	| CONCAT_WS '(' expr ',' expr_list ')'
4828
	  { $5->push_front($3); $$= new Item_func_concat_ws(*$5); }
4829 4830
	| CONVERT_TZ_SYM '(' expr ',' expr ',' expr ')'
	  {
4831
            if (Lex->add_time_zone_tables_to_query_tables(YYTHD))
4832
              MYSQL_YYABORT;
4833 4834
	    $$= new Item_func_convert_tz($3, $5, $7);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4835
	| CURDATE optional_braces
4836
	  { $$= new Item_func_curdate_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4837
	| CURTIME optional_braces
4838
	  { $$= new Item_func_curtime_local(); Lex->safe_to_cache_query=0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4839
	| CURTIME '(' expr ')'
4840
	  {
4841
	    $$= new Item_func_curtime_local($3);
4842
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4843
	  }
4844
	| CURRENT_USER optional_braces
4845 4846 4847 4848
          {
            $$= new Item_func_current_user(Lex->current_context());
            Lex->safe_to_cache_query= 0;
          }
4849 4850 4851 4852
	| 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
4853
	| DATABASE '(' ')'
4854
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4855
	    $$= new Item_func_database();
4856
            Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4857
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4858
	| DATE_SYM '(' expr ')'
4859
	  { $$= new Item_date_typecast($3); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4860 4861
	| DAY_SYM '(' expr ')'
	  { $$= new Item_func_dayofmonth($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4862
	| ELT_FUNC '(' expr ',' expr_list ')'
4863
	  { $5->push_front($3); $$= new Item_func_elt(*$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4864 4865
	| MAKE_SET_SYM '(' expr ',' expr_list ')'
	  { $$= new Item_func_make_set($3, *$5); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4866 4867 4868
	| ENCRYPT '(' expr ')'
	  {
	    $$= new Item_func_encrypt($3);
4869
	    Lex->uncacheable(UNCACHEABLE_RAND);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4870
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4871
	| ENCRYPT '(' expr ',' expr ')'   { $$= new Item_func_encrypt($3,$5); }
4872
	| DECODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4873
	  { $$= new Item_func_decode($3,$5.str); }
4874
	| ENCODE_SYM '(' expr ',' TEXT_STRING_literal ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4875
	 { $$= new Item_func_encode($3,$5.str); }
4876
	| DES_DECRYPT_SYM '(' expr ')'
4877
        { $$= new Item_func_des_decrypt($3); }
4878
	| DES_DECRYPT_SYM '(' expr ',' expr ')'
4879
        { $$= new Item_func_des_decrypt($3,$5); }
4880
	| DES_ENCRYPT_SYM '(' expr ')'
4881
        { $$= new Item_func_des_encrypt($3); }
4882
	| DES_ENCRYPT_SYM '(' expr ',' expr ')'
4883
        { $$= new Item_func_des_encrypt($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895
	| 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
4896
	    $$= new Item_func_date_format (new Item_func_from_unixtime($3),$5,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4897 4898
	  }
	| FIELD_FUNC '(' expr ',' expr_list ')'
4899
	  { $5->push_front($3); $$= new Item_func_field(*$5); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4900 4901 4902 4903 4904
	| geometry_function
	  {
#ifdef HAVE_SPATIAL
	    $$= $1;
#else
4905 4906
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
4907
	    MYSQL_YYABORT;
hf@deer.(none)'s avatar
hf@deer.(none) committed
4908 4909
#endif
	  }
4910
	| GET_FORMAT '(' date_time_type  ',' expr ')'
4911
	  { $$= new Item_func_get_format($3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4912 4913 4914 4915 4916 4917
	| 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); }
4918
	| interval_expr interval '+' expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4919
	  /* we cannot put interval before - */
4920 4921 4922 4923 4924
	  { $$= new Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
	  {
            if ($1->type() != Item::ROW_ITEM)
            {
4925 4926
              my_parse_error(ER(ER_SYNTAX_ERROR));
              MYSQL_YYABORT;
4927 4928 4929
            }
            $$= new Item_func_interval((Item_row *)$1);
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4930 4931
	| LAST_INSERT_ID '(' ')'
	  {
4932
	    $$= new Item_func_last_insert_id();
4933
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4934 4935 4936
	  }
	| LAST_INSERT_ID '(' expr ')'
	  {
4937
	    $$= new Item_func_last_insert_id($3);
4938
	    Lex->safe_to_cache_query= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4939 4940 4941 4942 4943 4944 4945
	  }
	| 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); }
4946
	| GREATEST_SYM '(' expr ',' expr_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4947 4948 4949
	  { $5->push_front($3); $$= new Item_func_max(*$5); }
	| LEAST_SYM '(' expr ',' expr_list ')'
	  { $5->push_front($3); $$= new Item_func_min(*$5); }
4950
	| LOG_SYM '(' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4951
	  { $$= new Item_func_log($3); }
4952
	| LOG_SYM '(' expr ',' expr ')'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4953
	  { $$= new Item_func_log($3, $5); }
4954
	| MASTER_POS_WAIT '(' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4955
	  {
4956
	    $$= new Item_master_pos_wait($3, $5);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4957
	    Lex->safe_to_cache_query=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4958
		  }
4959
	| MASTER_POS_WAIT '(' expr ',' expr ',' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4960
	  {
4961
	    $$= new Item_master_pos_wait($3, $5, $7);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4962
	    Lex->safe_to_cache_query=0;
4963
	  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
4964 4965
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new Item_func_microsecond($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4966 4967
	| MINUTE_SYM '(' expr ')'
	  { $$= new Item_func_minute($3); }
4968 4969
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new Item_func_mod( $3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4970 4971 4972
	| MONTH_SYM '(' expr ')'
	  { $$= new Item_func_month($3); }
	| NOW_SYM optional_braces
4973
	  { $$= new Item_func_now_local(); Lex->safe_to_cache_query=0;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4974
	| NOW_SYM '(' expr ')'
4975
	  { $$= new Item_func_now_local($3); Lex->safe_to_cache_query=0;}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4976
	| PASSWORD '(' expr ')'
4977
	  {
4978 4979
	    $$= YYTHD->variables.old_passwords ?
              (Item *) new Item_func_old_password($3) :
4980 4981 4982 4983
	      (Item *) new Item_func_password($3);
	  }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new Item_func_old_password($3); }
4984
	| POSITION_SYM '(' bit_expr IN_SYM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4985
	  { $$ = new Item_func_locate($5,$3); }
4986 4987
	| QUARTER_SYM '(' expr ')'
	  { $$ = new Item_func_quarter($3); }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4988
	| RAND '(' expr ')'
4989
	  { $$= new Item_func_rand($3); Lex->uncacheable(UNCACHEABLE_RAND);}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4990
	| RAND '(' ')'
4991
	  { $$= new Item_func_rand(); Lex->uncacheable(UNCACHEABLE_RAND);}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4992 4993 4994 4995 4996 4997 4998
	| 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); }
4999 5000 5001 5002 5003
	| 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
5004 5005 5006 5007
	| 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
5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019
	| 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); }
5020
	| SYSDATE optional_braces
5021 5022 5023 5024 5025 5026
          {
            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;
          }
5027
	| SYSDATE '(' expr ')'
5028 5029 5030 5031 5032 5033
          {
            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
5034
	| TIME_SYM '(' expr ')'
5035 5036 5037
	  { $$= 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
5038
	| TIMESTAMP '(' expr ',' expr ')'
5039
	  { $$= new Item_func_add_time($3, $5, 1, 0); }
5040 5041 5042 5043
	| 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
5044
	| TRIM '(' expr ')'
5045 5046
	  { $$= new Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5047
	  { $$= new Item_func_ltrim($6,$4); }
5048
	| TRIM '(' TRAILING expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5049
	  { $$= new Item_func_rtrim($6,$4); }
5050
	| TRIM '(' BOTH expr FROM expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5051
	  { $$= new Item_func_trim($6,$4); }
5052 5053 5054 5055 5056 5057
	| 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
5058 5059
	| TRIM '(' expr FROM expr ')'
	  { $$= new Item_func_trim($5,$3); }
5060 5061
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new Item_func_round($3,$5,1); }
5062
	| ident '.' ident '(' opt_expr_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5063
	  {
5064
	    LEX *lex= Lex;
5065
	    sp_name *name= new sp_name($1, $3, true);
5066 5067

	    name->init_qname(YYTHD);
5068
	    sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
5069
	    if ($5)
5070
	      $$= new Item_func_sp(Lex->current_context(), name, *$5);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5071
	    else
5072
	      $$= new Item_func_sp(Lex->current_context(), name);
5073
	    lex->safe_to_cache_query=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5074
	  }
5075
	| IDENT_sys '(' 
5076 5077
          {
#ifdef HAVE_DLOPEN
5078
            udf_func *udf= 0;
5079
            LEX *lex= Lex;
5080 5081 5082 5083 5084 5085
            if (using_udf_functions &&
                (udf= find_udf($1.str, $1.length)) &&
                udf->type == UDFTYPE_AGGREGATE)
            {
              if (lex->current_select->inc_in_sum_expr())
              {
5086 5087
                my_parse_error(ER(ER_SYNTAX_ERROR));
                MYSQL_YYABORT;
5088 5089
              }
            }
5090
            lex->current_select->udf_list.push_front(udf);
5091 5092 5093 5094
#endif
          }
          udf_expr_list ')'
          {
5095
            LEX *lex= Lex;
5096
#ifdef HAVE_DLOPEN
5097
            udf_func *udf;
5098

5099
            if (NULL != (udf= lex->current_select->udf_list.pop()))
5100
            {
5101 5102 5103
              if (udf->type == UDFTYPE_AGGREGATE)
                Select->in_sum_expr--;

5104 5105 5106 5107
              switch (udf->returns) {
              case STRING_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
5108 5109
                  if ($4 != NULL)
                    $$ = new Item_func_udf_str(udf, *$4);
5110 5111 5112 5113 5114
                  else
                    $$ = new Item_func_udf_str(udf);
                }
                else
                {
5115 5116
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_str(udf, *$4);
5117 5118 5119 5120 5121 5122 5123
                  else
                    $$ = new Item_sum_udf_str(udf);
                }
                break;
              case REAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
5124 5125
                  if ($4 != NULL)
                    $$ = new Item_func_udf_float(udf, *$4);
5126 5127 5128 5129 5130
                  else
                    $$ = new Item_func_udf_float(udf);
                }
                else
                {
5131 5132
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_float(udf, *$4);
5133 5134 5135 5136 5137 5138 5139
                  else
                    $$ = new Item_sum_udf_float(udf);
                }
                break;
              case INT_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
5140 5141
                  if ($4 != NULL)
                    $$ = new Item_func_udf_int(udf, *$4);
5142 5143 5144 5145 5146
                  else
                    $$ = new Item_func_udf_int(udf);
                }
                else
                {
5147 5148
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_int(udf, *$4);
5149 5150 5151 5152
                  else
                    $$ = new Item_sum_udf_int(udf);
                }
                break;
5153 5154 5155
              case DECIMAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
5156 5157
                  if ($4 != NULL)
                    $$ = new Item_func_udf_decimal(udf, *$4);
5158 5159 5160 5161 5162
                  else
                    $$ = new Item_func_udf_decimal(udf);
                }
                else
                {
5163 5164
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_decimal(udf, *$4);
5165 5166 5167 5168
                  else
                    $$ = new Item_sum_udf_decimal(udf);
                }
                break;
5169
              default:
5170
                MYSQL_YYABORT;
5171 5172 5173 5174 5175
              }
            }
            else
#endif /* HAVE_DLOPEN */
            {
5176 5177 5178
              THD *thd= lex->thd;
              LEX_STRING db;
              if (thd->copy_db_to(&db.str, &db.length))
5179
                MYSQL_YYABORT;
5180
              sp_name *name= new sp_name(db, $1, false);
5181 5182
              if (name)
                name->init_qname(thd);
5183

5184
              sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
5185 5186
              if ($4)
                $$= new Item_func_sp(Lex->current_context(), name, *$4);
5187
              else
5188
                $$= new Item_func_sp(Lex->current_context(), name);
5189
	      lex->safe_to_cache_query=0;
5190 5191
	    }
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5192
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
5193
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5194 5195
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5196
	| UNIX_TIMESTAMP '(' ')'
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5197 5198
	  {
	    $$= new Item_func_unix_timestamp();
5199
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5200
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5201 5202 5203
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
5204
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
5205 5206 5207 5208 5209 5210
	| 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
5211
	| WEEK_SYM '(' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5212
	  {
5213
            $$= new Item_func_week($3,new Item_int((char*) "0",
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5214
				   YYTHD->variables.default_week_format,1));
5215
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5216 5217 5218 5219 5220 5221 5222 5223
	| 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
5224
	| BENCHMARK_SYM '(' ulong_num ',' expr ')'
5225
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5226
	    $$=new Item_func_benchmark($3,$5);
5227
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5228
	  }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5229
	| EXTRACT_SYM '(' interval FROM expr ')'
5230
	{ $$=new Item_extract( $3, $5); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5231

hf@deer.(none)'s avatar
hf@deer.(none) committed
5232
geometry_function:
serg@serg.mylan's avatar
serg@serg.mylan committed
5233 5234 5235
	  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
5236 5237 5238 5239 5240 5241 5242 5243 5244
	  { $$= 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,
5245 5246
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5247 5248
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
5249
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5250 5251
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
5252
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266
 	| 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,
5267
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5268 5269
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
5270
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282
	| 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,
5283
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5284 5285 5286 5287 5288 5289 5290 5291 5292 5293
 	| 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)); }
	;

5294 5295 5296 5297 5298 5299
fulltext_options:
        /* nothing */                   { $$= FT_NL;  }
        | WITH QUERY_SYM EXPANSION_SYM  { $$= FT_NL | FT_EXPAND; }
        | IN_SYM BOOLEAN_SYM MODE_SYM   { $$= FT_BOOL; }
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5300
udf_expr_list:
5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324
	/* 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
	{
5325 5326 5327 5328 5329 5330 5331
          udf_func *udf= Select->udf_list.head();
          /*
           Use Item::name as a storage for the attribute value of user
           defined function argument. It is safe to use Item::name
           because the syntax will not allow having an explicit name here.
           See WL#1017 re. udf attributes.
          */
5332
	  if ($4.str)
5333
          {
5334 5335 5336 5337 5338 5339
            if (!udf)
            {
              /*
                Disallow using AS to specify explicit names for the arguments
                of stored routine calls
              */
5340 5341
              my_parse_error(ER(ER_SYNTAX_ERROR));
              MYSQL_YYABORT;
5342 5343
            }

5344
            $2->is_autogenerated_name= FALSE;
5345
	    $2->set_name($4.str, $4.length, system_charset_info);
5346
          }
5347
	  else if (udf)
5348
	    $2->set_name($1, (uint) ($3 - $1), YYTHD->charset());
5349 5350 5351
	  $$= $2;
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5352 5353 5354 5355

sum_expr:
	AVG_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_avg($3); }
5356 5357
	| AVG_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_avg_distinct($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5358 5359 5360 5361
	| BIT_AND  '(' in_sum_expr ')'
	  { $$=new Item_sum_and($3); }
	| BIT_OR  '(' in_sum_expr ')'
	  { $$=new Item_sum_or($3); }
5362 5363
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
5364
	| COUNT_SYM '(' opt_all '*' ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5365 5366 5367
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
5368
	| COUNT_SYM '(' DISTINCT
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5369
	  { Select->in_sum_expr++; }
5370
	   expr_list
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5371
	  { Select->in_sum_expr--; }
5372 5373
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5374 5375 5376 5377
	| 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); }
5378 5379 5380 5381 5382 5383 5384
/*
   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
5385 5386
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
5387 5388
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5389
	| STD_SYM '(' in_sum_expr ')'
5390
	  { $$=new Item_sum_std($3, 0); }
5391
	| VARIANCE_SYM '(' in_sum_expr ')'
5392 5393 5394 5395 5396
	  { $$=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
5397
	| SUM_SYM '(' in_sum_expr ')'
5398
	  { $$=new Item_sum_sum($3); }
5399 5400
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
monty@mysql.com's avatar
monty@mysql.com committed
5401 5402 5403 5404 5405
	| 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
5406
	  {
5407 5408
            SELECT_LEX *sel= Select;
	    sel->in_sum_expr--;
5409
	    $$=new Item_func_group_concat(Lex->current_context(), $3, $5,
5410
                                          sel->gorder_list, $7);
monty@mysql.com's avatar
monty@mysql.com committed
5411
	    $5->empty();
5412 5413
	  };

5414 5415 5416 5417 5418 5419
variable:
          '@'
          {
            if (! Lex->parsing_options.allows_variable)
            {
              my_error(ER_VIEW_SELECT_VARIABLE, MYF(0));
5420
              MYSQL_YYABORT;
5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445
            }
          }
          variable_aux
          {
            $$= $3;
          }
          ;

variable_aux:
          ident_or_text SET_VAR expr
          {
            $$= new Item_func_set_user_var($1, $3);
            LEX *lex= Lex;
            lex->uncacheable(UNCACHEABLE_RAND);
          }
        | ident_or_text
          {
            $$= new Item_func_get_user_var($1);
            LEX *lex= Lex;
            lex->uncacheable(UNCACHEABLE_RAND);
          }
        | '@' opt_var_ident_type ident_or_text opt_component
          {
            if ($3.str && $4.str && check_reserved_words(&$3))
            {
5446 5447
              my_parse_error(ER(ER_SYNTAX_ERROR));
              MYSQL_YYABORT;
5448 5449
            }
            if (!($$= get_system_var(YYTHD, $2, $3, $4)))
5450
              MYSQL_YYABORT;
5451 5452 5453
          }
        ;

5454 5455 5456 5457 5458
opt_distinct:
    /* empty */ { $$ = 0; }
    |DISTINCT   { $$ = 1; };

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

5462 5463

opt_gorder_clause:
5464 5465
	  /* empty */
	  {
5466
            Select->gorder_list = NULL;
5467 5468 5469
	  }
	| order_clause
          {
5470 5471 5472
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
5473
				     sizeof(st_sql_list));
5474
	    select->order_list.empty();
5475
	  };
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5476

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5477 5478

in_sum_expr:
5479
	opt_all
5480 5481 5482 5483
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
5484 5485
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
5486 5487
	  }
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5488 5489
	expr
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5490
	  Select->in_sum_expr--;
5491
	  $$= $3;
5492
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5493

5494
cast_type:
5495 5496
        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; }
5497
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; Lex->dec=0; }
5498 5499 5500 5501 5502 5503 5504 5505
        | 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; }
5506 5507
	;

5508 5509 5510 5511 5512
opt_expr_list:
	/* empty */ { $$= NULL; }
	| expr_list { $$= $1;}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5513
expr_list:
5514
	{ Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5515
	expr_list2
5516
	{ $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5517 5518

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

5522 5523
ident_list_arg:
          ident_list          { $$= $1; }
5524
        | '(' ident_list ')'  { $$= $2; };
5525

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5526
ident_list:
5527
        { Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5528
        ident_list2
5529
        { $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5530 5531

ident_list2:
5532
        simple_ident { Select->expr_list.head()->push_back($1); }
5533
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5534 5535 5536

opt_expr:
	/* empty */      { $$= NULL; }
5537
	| expr           { $$= $1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5538 5539 5540

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

when_list:
5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556
          WHEN_SYM expr THEN_SYM expr
          {
            $$= new List<Item>;
            $$->push_back($2);
            $$->push_back($4);
          }
        | when_list WHEN_SYM expr THEN_SYM expr
          {
            $1->push_back($3);
            $1->push_back($5);
            $$= $1;
          }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5557

5558
/* Warning - may return NULL in case of incomplete SELECT */
5559 5560
table_ref:
        table_factor            { $$=$1; }
5561
        | join_table
5562
          {
5563 5564
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
5565
              MYSQL_YYABORT;
5566
          }
5567 5568
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5569
join_table_list:
5570
	derived_table_list		{ MYSQL_YYABORT_UNLESS($$=$1); }
5571
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
5572

5573 5574
/* Warning - may return NULL in case of incomplete SELECT */
derived_table_list:
5575
        table_ref { $$=$1; }
5576 5577
        | derived_table_list ',' table_ref
          {
5578
            MYSQL_YYABORT_UNLESS($1 && ($$=$3));
5579
          }
5580 5581
        ;

5582 5583 5584 5585 5586 5587 5588
/*
  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.
*/
5589
join_table:
5590
/* INNER JOIN variants */
timour@mysql.com's avatar
timour@mysql.com committed
5591
        /*
5592 5593 5594
          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
5595 5596
        */
        table_ref %prec TABLE_REF_PRIORITY normal_join table_ref
5597
          { MYSQL_YYABORT_UNLESS($1 && ($$=$3)); }
5598
	| table_ref STRAIGHT_JOIN table_factor
5599
	  { MYSQL_YYABORT_UNLESS($1 && ($$=$3)); $3->straight=1; }
5600 5601 5602
	| table_ref normal_join table_ref
          ON
          {
5603
            MYSQL_YYABORT_UNLESS($1 && $3);
5604
            /* Change the current name resolution context to a local context. */
5605
            if (push_new_name_resolution_context(YYTHD, $1, $3))
5606
              MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5607
            Select->parsing_place= IN_ON;
5608 5609 5610 5611 5612
          }
          expr
	  {
            add_join_on($3,$6);
            Lex->pop_context();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5613
            Select->parsing_place= NO_MATTER;
5614 5615 5616 5617
          }
        | table_ref STRAIGHT_JOIN table_factor
          ON
          {
5618
            MYSQL_YYABORT_UNLESS($1 && $3);
5619
            /* Change the current name resolution context to a local context. */
5620
            if (push_new_name_resolution_context(YYTHD, $1, $3))
5621
              MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5622
            Select->parsing_place= IN_ON;
5623 5624 5625 5626 5627 5628
          }
          expr
          {
            $3->straight=1;
            add_join_on($3,$6);
            Lex->pop_context();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5629
            Select->parsing_place= NO_MATTER;
5630
          }
5631
	| table_ref normal_join table_ref
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5632
	  USING
5633
	  {
5634
            MYSQL_YYABORT_UNLESS($1 && $3);
5635
	  }
5636
	  '(' using_list ')'
5637
          { add_join_natural($1,$3,$7,Select); $$=$3; }
5638 5639
	| table_ref NATURAL JOIN_SYM table_factor
	  {
5640
            MYSQL_YYABORT_UNLESS($1 && ($$=$4));
5641
            add_join_natural($1,$4,NULL,Select);
5642 5643 5644
          }

/* LEFT JOIN variants */
5645 5646 5647
	| table_ref LEFT opt_outer JOIN_SYM table_ref
          ON
          {
5648
            MYSQL_YYABORT_UNLESS($1 && $5);
5649
            /* Change the current name resolution context to a local context. */
5650
            if (push_new_name_resolution_context(YYTHD, $1, $5))
5651
              MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5652
            Select->parsing_place= IN_ON;
5653 5654 5655 5656 5657 5658 5659
          }
          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
5660
            Select->parsing_place= NO_MATTER;
5661
          }
5662
	| table_ref LEFT opt_outer JOIN_SYM table_factor
5663
	  {
5664
            MYSQL_YYABORT_UNLESS($1 && $5);
5665
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5666
	  USING '(' using_list ')'
5667 5668 5669 5670 5671
          { 
            add_join_natural($1,$5,$9,Select); 
            $5->outer_join|=JOIN_TYPE_LEFT; 
            $$=$5; 
          }
5672
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
5673
	  {
5674
            MYSQL_YYABORT_UNLESS($1 && $6);
5675
 	    add_join_natural($1,$6,NULL,Select);
5676
	    $6->outer_join|=JOIN_TYPE_LEFT;
5677 5678
	    $$=$6;
	  }
5679 5680

/* RIGHT JOIN variants */
5681 5682 5683
	| table_ref RIGHT opt_outer JOIN_SYM table_ref
          ON
          {
5684
            MYSQL_YYABORT_UNLESS($1 && $5);
5685
            /* Change the current name resolution context to a local context. */
5686
            if (push_new_name_resolution_context(YYTHD, $1, $5))
5687
              MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5688
            Select->parsing_place= IN_ON;
5689 5690
          }
          expr
serg@serg.mylan's avatar
serg@serg.mylan committed
5691
          {
5692 5693
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
5694
              MYSQL_YYABORT;
5695 5696
            add_join_on($$, $8);
            Lex->pop_context();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5697
            Select->parsing_place= NO_MATTER;
5698 5699
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
5700
	  {
5701
            MYSQL_YYABORT_UNLESS($1 && $5);
5702
	  }
5703
	  USING '(' using_list ')'
serg@serg.mylan's avatar
serg@serg.mylan committed
5704
          {
5705 5706
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
5707
              MYSQL_YYABORT;
5708
            add_join_natural($$,$5,$9,Select);
5709 5710
          }
	| table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor
5711
	  {
5712
            MYSQL_YYABORT_UNLESS($1 && $6);
5713
	    add_join_natural($6,$1,NULL,Select);
5714 5715
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
5716
              MYSQL_YYABORT;
5717
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5718 5719

normal_join:
5720 5721 5722 5723
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5724

5725
/* Warning - may return NULL in case of incomplete SELECT */
5726
table_factor:
5727
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5728
	  SELECT_LEX *sel= Select;
5729
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5730
	  sel->table_join_options= 0;
5731
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5732
        table_ident opt_table_alias opt_key_definition
5733
	{
5734
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5735
	  SELECT_LEX *sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5736
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5737
					   sel->get_table_join_options(),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5738
					   lex->lock_option,
5739 5740
					   sel->get_use_index(),
					   sel->get_ignore_index())))
5741
	    MYSQL_YYABORT;
5742
          sel->add_joined_table($$);
5743
	}
5744 5745 5746 5747
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref
          ON
          {
            /* Change the current name resolution context to a local context. */
5748
            if (push_new_name_resolution_context(YYTHD, $3, $7))
5749
              MYSQL_YYABORT;
5750

5751 5752 5753
          }
          expr '}'
	  {
5754
	    LEX *lex= Lex;
5755
            MYSQL_YYABORT_UNLESS($3 && $7);
5756 5757 5758 5759
            add_join_on($7,$10);
            Lex->pop_context();
            $7->outer_join|=JOIN_TYPE_LEFT;
            $$=$7;
5760
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
5761
              MYSQL_YYABORT;
5762
          }
5763
	| select_derived_init get_select_lex select_derived2
5764
          {
5765
            LEX *lex= Lex;
5766 5767 5768 5769 5770
            SELECT_LEX *sel= lex->current_select;
            if ($1)
            {
	      if (sel->set_braces(1))
	      {
5771 5772
                my_parse_error(ER(ER_SYNTAX_ERROR));
	        MYSQL_YYABORT;
5773 5774 5775 5776 5777 5778 5779
	      }
              /* 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))
5780
              MYSQL_YYABORT;
5781
            $$= 0;
serg@serg.mylan's avatar
serg@serg.mylan committed
5782
            /* incomplete derived tables return NULL, we must be
5783
               nested in select_derived rule to be here. */
5784
          }
5785 5786 5787 5788 5789
	| '(' 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
5790
          if (!($3 || $6) && $2->embedding &&
5791
              !$2->embedding->nested_join->join_list.elements)
5792
          {
5793 5794 5795 5796 5797
            /* 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;
5798
          }
5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812
          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
5813

5814
	      MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
5815
            sel->add_joined_table($$);
5816
            lex->pop_context();
5817 5818 5819 5820 5821
          }
	  else
          if ($4 || $6)
	  {
            /* simple nested joins cannot have aliases or unions */
5822 5823
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
5824 5825 5826 5827 5828
	  }
          else
            $$= $3;
	}
        ;
5829

5830
/* handle contents of parentheses in join expression */
5831
select_derived:
5832
	  get_select_lex
5833
	  {
5834 5835
            LEX *lex= Lex;
            if ($1->init_nested_join(lex->thd))
5836
              MYSQL_YYABORT;
5837 5838 5839 5840 5841 5842
          }
          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
5843

5844
            if (!($$= $1->end_nested_join(lex->thd)) && $3)
5845
              MYSQL_YYABORT;
5846 5847
            if (!$3 && $$)
            {
5848 5849
              my_parse_error(ER(ER_SYNTAX_ERROR));
	      MYSQL_YYABORT;
5850 5851
            }
          }
monty@mysql.com's avatar
monty@mysql.com committed
5852
 	;
5853

monty@mysql.com's avatar
monty@mysql.com committed
5854
select_derived2:
5855
        {
5856
	  LEX *lex= Lex;
5857
	  lex->derived_tables|= DERIVED_SUBQUERY;
5858 5859
          if (lex->sql_command == (int)SQLCOM_HA_READ ||
              lex->sql_command == (int)SQLCOM_KILL)
5860
	  {
5861 5862
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
5863
	  }
5864
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
5865
              mysql_new_select(lex, 1))
5866
	    MYSQL_YYABORT;
5867
	  mysql_init_select(lex);
5868
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
5869
	  lex->current_select->parsing_place= SELECT_LIST;
5870 5871 5872
	}
        select_options select_item_list
	{
5873
	  Select->parsing_place= NO_MATTER;
5874
	}
5875
	opt_select_from
5876
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5877

5878 5879 5880 5881 5882 5883 5884 5885
get_select_lex:
	/* Empty */ { $$= Select; }
        ;

select_derived_init:
          SELECT_SYM
          {
            LEX *lex= Lex;
5886 5887 5888 5889

            if (! lex->parsing_options.allows_derived)
            {
              my_error(ER_VIEW_SELECT_DERIVED, MYF(0));
5890
              MYSQL_YYABORT;
5891 5892
            }

serg@serg.mylan's avatar
serg@serg.mylan committed
5893
            SELECT_LEX *sel= lex->current_select;
5894 5895 5896 5897
            TABLE_LIST *embedding;
            if (!sel->embedding || sel->end_nested_join(lex->thd))
	    {
              /* we are not in parentheses */
5898 5899
              my_parse_error(ER(ER_SYNTAX_ERROR));
	      MYSQL_YYABORT;
5900 5901 5902 5903 5904 5905 5906 5907
	    }
            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
5908 5909
opt_outer:
	/* empty */	{}
5910
	| OUTER		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5911

5912 5913 5914 5915
opt_for_join:
        /* empty */
        | FOR_SYM JOIN_SYM;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5916 5917 5918
opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
5919
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5920
	    SELECT_LEX *sel= Select;
5921 5922 5923
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5924 5925
	| FORCE_SYM key_usage_list
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5926
	    SELECT_LEX *sel= Select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5927 5928 5929 5930
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
5931
	| IGNORE_SYM key_usage_list
5932
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5933
	    SELECT_LEX *sel= Select;
5934
	    sel->ignore_index= *$2;
5935
	    sel->ignore_index_ptr= &sel->ignore_index;
5936
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5937 5938

key_usage_list:
5939
        key_or_index opt_for_join      
5940
	{ Select->interval_list.empty(); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5941
        '(' key_list_or_empty ')'
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5942
        { $$= &Select->interval_list; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5943 5944 5945 5946 5947 5948
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5949 5950 5951

key_usage_list2:
	key_usage_list2 ',' ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5952
        { Select->
5953
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $3.str, $3.length,
5954
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5955
	| ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5956
        { Select->
5957
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $1.str, $1.length,
5958
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5959
	| PRIMARY_SYM
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5960
        { Select->
5961
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
5962
				    system_charset_info)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5963 5964 5965

using_list:
	ident
5966
	  {
5967
            if (!($$= new List<String>))
5968
	      MYSQL_YYABORT;
5969 5970 5971
            $$->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
5972 5973 5974
	  }
	| using_list ',' ident
	  {
5975 5976 5977 5978
            $1->push_back(new (YYTHD->mem_root)
                              String((const char *) $3.str, $3.length,
                                      system_charset_info));
            $$= $1;
5979
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5980 5981

interval:
5982 5983
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
5984
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5985 5986
	| 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
5987
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5988 5989
	| 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
5990 5991
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5992
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
5993 5994 5995 5996 5997 5998 5999 6000
	| 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
6001 6002
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
6003
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6004
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
6005 6006
	| YEAR_SYM		{ $$=INTERVAL_YEAR; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6007

6008
date_time_type:
6009 6010 6011 6012 6013
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
6014

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6015 6016 6017
table_alias:
	/* empty */
	| AS
6018
	| EQ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6019 6020 6021 6022

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

6025 6026 6027 6028
opt_all:
	/* empty */
	| ALL
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6029 6030

where_clause:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6031
	/* empty */  { Select->where= 0; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6032 6033 6034 6035 6036
	| WHERE
          {
            Select->parsing_place= IN_WHERE;
          }
          expr
6037
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6038 6039 6040 6041 6042
            SELECT_LEX *select= Select;
	    select->where= $3;
            select->parsing_place= NO_MATTER;
	    if ($3)
	      $3->top_level_item();
6043
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6044
 	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6045 6046 6047

having_clause:
	/* empty */
6048 6049
	| HAVING
	  {
6050
	    Select->parsing_place= IN_HAVING;
6051 6052
          }
	  expr
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6053
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6054
	    SELECT_LEX *sel= Select;
6055
	    sel->having= $3;
6056
	    sel->parsing_place= NO_MATTER;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6057 6058 6059
	    if ($3)
	      $3->top_level_item();
	  }
6060
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6061 6062

opt_escape:
6063 6064 6065 6066 6067
	ESCAPE_SYM simple_expr 
          {
            Lex->escape_used= TRUE;
            $$= $2;
          }
6068
	| /* empty */
6069
          {
6070
            Lex->escape_used= FALSE;
6071 6072 6073
            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
6074 6075
          }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6076 6077 6078


/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6079
   group by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6080 6081 6082 6083
*/

group_clause:
	/* empty */
6084
	| GROUP BY group_list olap_opt;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6085 6086

group_list:
6087
	group_list ',' order_ident order_dir
6088
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) MYSQL_YYABORT; }
6089
	| order_ident order_dir
6090
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) MYSQL_YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6091

6092 6093
olap_opt:
	/* empty */ {}
6094
	| WITH CUBE_SYM
6095
          {
6096
	    LEX *lex=Lex;
6097 6098
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
6099
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
6100
		       "global union parameters");
6101
	      MYSQL_YYABORT;
6102
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6103
	    lex->current_select->olap= CUBE_TYPE;
6104
	    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
6105
	    MYSQL_YYABORT;	/* To be deleted in 5.1 */
6106
	  }
6107
	| WITH ROLLUP_SYM
6108
          {
6109 6110 6111
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
6112
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
6113
		       "global union parameters");
6114
	      MYSQL_YYABORT;
6115
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6116
	    lex->current_select->olap= ROLLUP_TYPE;
6117
	  }
6118
	;
6119

malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138
/*
  Order by statement in ALTER TABLE
*/

alter_order_clause:
          ORDER_SYM BY alter_order_list
        ;

alter_order_list:
          alter_order_list ',' alter_order_item
        | alter_order_item
        ;

alter_order_item:
          simple_ident_nospvar order_dir
          {
            THD *thd= YYTHD;
            bool ascending= ($2 == 1) ? true : false;
            if (add_order_to_list(thd, $1, ascending))
6139
              MYSQL_YYABORT;
malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
6140 6141 6142
          }
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6143
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6144
   Order by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6145 6146
*/

6147
opt_order_clause:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6148
	/* empty */
6149
	| order_clause;
6150 6151

order_clause:
6152 6153
	ORDER_SYM BY
        {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6154
	  LEX *lex=Lex;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6155 6156 6157 6158
          SELECT_LEX *sel= lex->current_select;
          SELECT_LEX_UNIT *unit= sel-> master_unit();
	  if (sel->linkage != GLOBAL_OPTIONS_TYPE &&
	      sel->olap != UNSPECIFIED_OLAP_TYPE)
6159
	  {
6160 6161
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
6162
	    MYSQL_YYABORT;
6163
	  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176
          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
6177
                 first_sl->select_limit) &&            
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6178
                unit->add_fake_select_lex(lex->thd))
6179
              MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6180
          }
6181
	} order_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6182 6183 6184

order_list:
	order_list ',' order_ident order_dir
6185
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) MYSQL_YYABORT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6186
	| order_ident order_dir
6187
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) MYSQL_YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6188 6189 6190

order_dir:
	/* empty */ { $$ =  1; }
6191
	| ASC  { $$ =1; }
6192
	| DESC { $$ =0; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6193 6194


6195 6196 6197
opt_limit_clause_init:
	/* empty */
	{
6198 6199
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
6200 6201
          sel->offset_limit= 0;
          sel->select_limit= 0;
6202
	}
6203 6204 6205
	| limit_clause {}
	;

6206 6207 6208 6209 6210
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

6211
limit_clause:
6212
	LIMIT limit_options {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6213
	;
6214 6215

limit_options:
6216
	limit_option
6217
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6218
            SELECT_LEX *sel= Select;
6219
            sel->select_limit= $1;
6220
            sel->offset_limit= 0;
6221
	    sel->explicit_limit= 1;
6222
	  }
6223
	| limit_option ',' limit_option
6224
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6225
	    SELECT_LEX *sel= Select;
6226 6227
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
6228
	    sel->explicit_limit= 1;
6229
	  }
6230
	| limit_option OFFSET_SYM limit_option
6231
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6232
	    SELECT_LEX *sel= Select;
6233 6234
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
6235
	    sel->explicit_limit= 1;
6236 6237
	  }
	;
6238 6239 6240 6241 6242
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
6243
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6244 6245 6246 6247

delete_limit_clause:
	/* empty */
	{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6248
	  LEX *lex=Lex;
6249
	  lex->current_select->select_limit= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6250
	}
6251
	| LIMIT limit_option
6252 6253
	{
	  SELECT_LEX *sel= Select;
6254
	  sel->select_limit= $2;
6255 6256
	  sel->explicit_limit= 1;
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6257

serg@serg.mylan's avatar
serg@serg.mylan committed
6258 6259
ulong_num:
          NUM           { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
6260
	| HEX_NUM       { $$= (ulong) strtol($1.str, (char**) 0, 16); }
monty@mysql.com's avatar
monty@mysql.com committed
6261 6262
	| 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
6263
        | DECIMAL_NUM   { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
monty@mysql.com's avatar
monty@mysql.com committed
6264 6265
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6266

6267
ulonglong_num:
monty@mysql.com's avatar
monty@mysql.com committed
6268 6269 6270
	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); }
6271
        | DECIMAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
monty@mysql.com's avatar
monty@mysql.com committed
6272 6273
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6274 6275 6276 6277 6278 6279

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
6280 6281 6282 6283

            if (! lex->parsing_options.allows_select_procedure)
            {
              my_error(ER_VIEW_SELECT_CLAUSE, MYF(0), "PROCEDURE");
6284
              MYSQL_YYABORT;
6285 6286
            }

6287 6288
	    if (&lex->select_lex != lex->current_select)
	    {
6289
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
6290
	      MYSQL_YYABORT;
6291
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6292 6293 6294
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
6295 6296 6297 6298
	    if (add_proc_to_list(lex->thd, new Item_field(&lex->
                                                          current_select->
                                                          context,
                                                          NULL,NULL,$2.str)))
6299
	      MYSQL_YYABORT;
6300
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6301
	  }
6302
	  '(' procedure_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6303 6304 6305 6306


procedure_list:
	/* empty */ {}
6307
	| procedure_list2 {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6308 6309 6310

procedure_list2:
	procedure_list2 ',' procedure_item
6311
	| procedure_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6312 6313 6314 6315

procedure_item:
	  remember_name expr
	  {
6316 6317 6318 6319
            THD *thd= YYTHD;
            Lex_input_stream *lip= thd->m_lip;

	    if (add_proc_to_list(thd, $2))
6320
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6321
	    if (!$2->name)
6322 6323
	      $2->set_name($1,(uint) ((char*) lip->tok_end - $1),
                           thd->charset());
6324 6325 6326 6327 6328 6329
	  }
          ;


select_var_list_init:
	   {
6330 6331
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
6332
	        MYSQL_YYABORT;
6333
	   }
6334
	   select_var_list
6335
	   {}
6336
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6337

Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6338
select_var_list:
6339 6340 6341 6342
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

6343 6344 6345 6346 6347 6348 6349
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
6350 6351 6352 6353 6354
               /*
                 The parser won't create select_result instance only
	         if it's an EXPLAIN.
               */
               DBUG_ASSERT(lex->describe);
6355 6356
	   }
           | ident_or_text
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6357
           {
6358
             LEX *lex=Lex;
6359
	     sp_variable_t *t;
6360

6361
	     if (!lex->spcont || !(t=lex->spcont->find_variable(&$1)))
6362
	     {
6363
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
6364
	       MYSQL_YYABORT;
6365
	     }
6366 6367
	     if (lex->result)
             {
6368 6369 6370
               my_var *var;
	       ((select_dumpvar *)lex->result)->
                 var_list.push_back(var= new my_var($1,1,t->offset,t->type));
6371
#ifndef DBUG_OFF
6372
	       if (var)
6373
		 var->sp= lex->sphead;
6374
#endif
6375 6376 6377 6378 6379 6380 6381 6382
             }
	     else
	     {
               /*
                 The parser won't create select_result instance only
	         if it's an EXPLAIN.
               */
               DBUG_ASSERT(lex->describe);
6383
	     }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6384
	   }
6385
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6386

6387
into:
6388 6389 6390 6391 6392
        INTO
	{
          if (! Lex->parsing_options.allows_select_into)
          {
            my_error(ER_VIEW_SELECT_CLAUSE, MYF(0), "INTO");
6393
            MYSQL_YYABORT;
6394 6395 6396 6397 6398 6399 6400
          }
	}
        into_destination
        ;

into_destination:
        OUTFILE TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6401
	{
6402
          LEX *lex= Lex;
6403
          lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
6404
          if (!(lex->exchange= new sql_exchange($2.str, 0)) ||
6405
              !(lex->result= new select_export(lex->exchange)))
6406
            MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6407 6408
	}
	opt_field_term opt_line_term
6409
	| DUMPFILE TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6410
	{
6411
	  LEX *lex=Lex;
6412 6413
	  if (!lex->describe)
	  {
6414
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
6415
	    if (!(lex->exchange= new sql_exchange($2.str,1)))
6416
	      MYSQL_YYABORT;
6417
	    if (!(lex->result= new select_dump(lex->exchange)))
6418
	      MYSQL_YYABORT;
6419
	  }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6420
	}
6421
        | select_var_list_init
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6422
	{
6423
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
6424 6425
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6426

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6427 6428 6429
/*
  DO statement
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6430

6431
do:	DO_SYM
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6432 6433 6434
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
6435 6436 6437 6438 6439
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6440
	}
6441 6442
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6443
/*
6444
  Drop : delete tables or index or user
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6445 6446 6447
*/

drop:
serg@serg.mylan's avatar
serg@serg.mylan committed
6448
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6449
	{
6450 6451
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
6452 6453
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6454
	}
6455
	| DROP INDEX_SYM ident ON table_ident {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6456
	  {
6457 6458
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
6459 6460
             lex->alter_info.reset();
             lex->alter_info.flags= ALTER_DROP_INDEX;
6461 6462
	     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
6463 6464
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
6465
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6466 6467 6468
	  }
	| DROP DATABASE if_exists ident
	  {
6469 6470 6471 6472
	    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
6473
	 }
6474
	| DROP FUNCTION_SYM if_exists sp_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6475
	  {
6476
	    LEX *lex=Lex;
6477 6478
	    if (lex->sphead)
	    {
6479
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
6480
	      MYSQL_YYABORT;
6481
	    }
6482
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
6483 6484 6485 6486 6487 6488 6489 6490
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
	  }
	| DROP PROCEDURE if_exists sp_name
	  {
	    LEX *lex=Lex;
	    if (lex->sphead)
	    {
6491
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
6492
	      MYSQL_YYABORT;
6493 6494 6495 6496
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
6497
	  }
6498
	| DROP USER clear_privileges user_list
6499
	  {
6500 6501
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
6502 6503
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
6504
	    LEX *lex= Lex;
6505 6506 6507
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
6508
        | DROP TRIGGER_SYM if_exists sp_name
6509 6510 6511
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_DROP_TRIGGER;
6512 6513
            lex->drop_if_exists= $3;
            lex->spname= $4;
6514 6515
          }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6516 6517

table_list:
6518
	table_name
6519
	| table_list ',' table_name;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6520

6521
table_name:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6522
	table_ident
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6523 6524
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
6525
	    MYSQL_YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6526 6527
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6528 6529

if_exists:
6530
	/* empty */ { $$= 0; }
6531 6532
	| IF EXISTS { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6533

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

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

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

insert_lock_option:
6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591
	/* 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
          }
6592 6593 6594
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
6595
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6596 6597

replace_lock_option:
6598 6599
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6600 6601 6602

insert2:
	INTO insert_table {}
6603
	| insert_table {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6604 6605

insert_table:
6606
	table_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6607
	{
6608 6609 6610 6611
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
6612
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6613 6614

insert_field_spec:
serg@serg.mylan's avatar
serg@serg.mylan committed
6615 6616 6617
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6618 6619
	| SET
	  {
6620 6621 6622
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
6623
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6624
	   }
6625
	   ident_eq_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6626 6627 6628

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
6629
	| insert_ident		{ Lex->field_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6630 6631 6632

insert_values:
	VALUES	values_list  {}
6633
	| VALUE_SYM values_list  {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6634 6635
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
6636
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6637 6638 6639

values_list:
	values_list ','  no_braces
6640
	| no_braces;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6641 6642 6643 6644

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
6645
	ident_eq_value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6646 6647

ident_eq_value:
6648
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6649
	 {
6650 6651 6652
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
6653
	    MYSQL_YYABORT;
6654
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6655 6656

equal:	EQ		{}
6657 6658 6659 6660 6661 6662 6663
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6664 6665 6666 6667 6668

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
6669
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6670 6671 6672
	 }
	 opt_values ')'
	 {
6673 6674
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
6675
	    MYSQL_YYABORT;
6676
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6677 6678 6679

opt_values:
	/* empty */ {}
6680
	| values;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6681 6682

values:
6683
	values ','  expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6684 6685
	{
	  if (Lex->insert_list->push_back($3))
6686
	    MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6687
	}
6688 6689 6690
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
6691
	      MYSQL_YYABORT;
6692 6693 6694 6695
	  }
	;

expr_or_default:
6696
	expr	  { $$= $1;}
6697
	| DEFAULT {$$= new Item_default_value(Lex->current_context()); }
6698
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6699

6700 6701
opt_insert_update:
        /* empty */
monty@mysql.com's avatar
monty@mysql.com committed
6702
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
6703
          KEY_SYM UPDATE_SYM insert_update_list
6704 6705
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6706 6707 6708
/* Update rows in a table */

update:
6709 6710
	UPDATE_SYM
	{
6711
	  LEX *lex= Lex;
6712
	  mysql_init_select(lex);
6713
          lex->sql_command= SQLCOM_UPDATE;
6714
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
6715
	  lex->duplicates= DUP_ERROR; 
6716
        }
6717
        opt_low_priority opt_ignore join_table_list
6718
	SET update_list
6719
	{
6720 6721
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
6722
            lex->sql_command= SQLCOM_UPDATE_MULTI;
6723 6724 6725
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
6726 6727
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
6728
	    MYSQL_YYABORT;
6729
	  }
6730 6731 6732 6733 6734 6735
          /*
            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);
6736
	}
6737
	where_clause opt_order_clause delete_limit_clause {}
6738
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6739 6740

update_list:
6741 6742 6743 6744
	update_list ',' update_elem
	| update_elem;

update_elem:
monty@mishka.local's avatar
monty@mishka.local committed
6745
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6746
	{
monty@mysql.com's avatar
monty@mysql.com committed
6747
	  if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
6748
	    MYSQL_YYABORT;
6749 6750 6751 6752 6753 6754 6755
	};

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
monty@mishka.local's avatar
monty@mishka.local committed
6756
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6757
	  {
6758 6759 6760
	  LEX *lex= Lex;
	  if (lex->update_list.push_back($1) || 
	      lex->value_list.push_back($3))
6761
	      MYSQL_YYABORT;
6762
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6763 6764

opt_low_priority:
6765
	/* empty */	{ $$= YYTHD->update_lock_default; }
6766
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6767 6768 6769 6770

/* Delete rows from a table */

delete:
6771
	DELETE_SYM
6772
	{
6773 6774
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
6775
	  mysql_init_select(lex);
6776
	  lex->lock_option= lex->thd->update_lock_default;
6777
	  lex->ignore= 0;
6778
	  lex->select_lex.init_order();
6779
	}
6780 6781
	opt_delete_options single_multi {}
	;
6782 6783

single_multi:
6784 6785
 	FROM table_ident
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6786 6787
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
6788
	    MYSQL_YYABORT;
6789 6790
	}
	where_clause opt_order_clause
6791
	delete_limit_clause {}
6792
	| table_wild_list
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6793
	  { mysql_init_multi_delete(Lex); }
6794
          FROM join_table_list where_clause
6795 6796
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
6797
              MYSQL_YYABORT;
6798
          }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6799 6800
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
6801
	  USING join_table_list where_clause
6802 6803
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
6804
              MYSQL_YYABORT;
6805
          }
6806
	;
6807 6808 6809

table_wild_list:
	  table_wild_one {}
6810
	  | table_wild_list ',' table_wild_one {};
6811 6812

table_wild_one:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6813
	ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6814
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6815
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
6816 6817
					 TL_OPTION_UPDATING | 
                                         TL_OPTION_ALIAS, Lex->lock_option))
6818
	    MYSQL_YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6819
        }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6820
	| ident '.' ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6821
	  {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6822 6823
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
6824 6825 6826
					   $5, 
                                           TL_OPTION_UPDATING | 
                                           TL_OPTION_ALIAS,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6827
					   Lex->lock_option))
6828
	      MYSQL_YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6829
	  }
6830
	;
6831 6832

opt_wild:
6833
	/* empty */	{}
6834
	| '.' '*'	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6835 6836


6837
opt_delete_options:
6838
	/* empty */	{}
6839
	| opt_delete_option opt_delete_options {};
6840 6841

opt_delete_option:
6842
	QUICK		{ Select->options|= OPTION_QUICK; }
6843
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
6844
	| IGNORE_SYM	{ Lex->ignore= 1; };
6845

6846
truncate:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
6847
	TRUNCATE_SYM opt_table_sym table_name
6848
	{
6849
	  LEX* lex= Lex;
6850
	  lex->sql_command= SQLCOM_TRUNCATE;
6851
	  lex->select_lex.options= 0;
6852
          lex->select_lex.sql_cache= SELECT_LEX::SQL_CACHE_UNSPECIFIED;
6853
	  lex->select_lex.init_order();
6854 6855
	}
	;
6856

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6857 6858
opt_table_sym:
	/* empty */
6859
	| TABLE_SYM;
6860

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6861 6862
/* Show things */

6863 6864
show:	SHOW
	{
6865 6866
	  LEX *lex=Lex;
	  lex->wild=0;
6867 6868 6869
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
6870 6871
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6872
	show_param
6873 6874
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6875 6876

show_param:
6877
         DATABASES wild_and_where
6878 6879 6880 6881 6882
         {
           LEX *lex= Lex;
           lex->sql_command= SQLCOM_SELECT;
           lex->orig_sql_command= SQLCOM_SHOW_DATABASES;
           if (prepare_schema_table(YYTHD, lex, 0, SCH_SCHEMATA))
6883
             MYSQL_YYABORT;
6884
         }
6885
         | opt_full TABLES opt_db wild_and_where
6886 6887 6888 6889
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLES;
6890
             lex->select_lex.db= $3;
6891
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLE_NAMES))
6892
               MYSQL_YYABORT;
6893
           }
6894 6895 6896 6897 6898 6899 6900
         | opt_full TRIGGERS_SYM opt_db wild_and_where
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TRIGGERS;
             lex->select_lex.db= $3;
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TRIGGERS))
6901
               MYSQL_YYABORT;
6902
           }
6903
         | TABLE_SYM STATUS_SYM opt_db wild_and_where
6904 6905 6906 6907
           {
             LEX *lex= Lex;
             lex->sql_command= SQLCOM_SELECT;
             lex->orig_sql_command= SQLCOM_SHOW_TABLE_STATUS;
6908
             lex->select_lex.db= $3;
6909
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLES))
6910
               MYSQL_YYABORT;
6911
           }
6912
        | OPEN_SYM TABLES opt_db wild_and_where
6913
	  {
6914
	    LEX *lex= Lex;
6915 6916
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_OPEN_TABLES;
6917
	    lex->select_lex.db= $3;
6918
            if (prepare_schema_table(YYTHD, lex, 0, SCH_OPEN_TABLES))
6919
              MYSQL_YYABORT;
6920
	  }
6921
	| ENGINE_SYM storage_engines 
6922 6923
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
6924
	| opt_full COLUMNS from_or_in table_ident opt_db wild_and_where
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6925
	  {
6926 6927 6928
 	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SELECT;
	    lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
6929 6930 6931
	    if ($5)
	      $4->change_db($5);
	    if (prepare_schema_table(YYTHD, lex, $4, SCH_COLUMNS))
6932
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6933
	  }
6934
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
6935 6936
	  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
6937
	ulong_num
6938 6939 6940 6941
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
6942
	    Lex->mi.server_id = $16;
6943
          }
6944
        | master_or_binary LOGS_SYM
6945 6946
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
6947 6948 6949 6950 6951
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
6952
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
6953
          {
6954 6955
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
6956
          } opt_limit_clause_init
6957
        | keys_or_index from_or_in table_ident opt_db where_clause
6958 6959 6960 6961
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_KEYS;
6962 6963 6964
	    if ($4)
	      $3->change_db($4);
            if (prepare_schema_table(YYTHD, lex, $3, SCH_STATISTICS))
6965
              MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6966
	  }
6967 6968 6969 6970 6971 6972 6973 6974
	| COLUMN_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_COLUMN_TYPES;
	  }
	| TABLE_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
6975 6976 6977 6978 6979 6980 6981
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
	    WARN_DEPRECATED("SHOW TABLE TYPES", "SHOW [STORAGE] ENGINES");
	  }
	| opt_storage ENGINES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
6982 6983 6984 6985 6986 6987
	  }
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
6988
        | COUNT_SYM '(' '*' ')' WARNINGS
6989
          { (void) create_select_for_variable("warning_count"); }
6990
        | COUNT_SYM '(' '*' ')' ERRORS
6991
	  { (void) create_select_for_variable("error_count"); }
6992
        | WARNINGS opt_limit_clause_init
6993
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
6994
        | ERRORS opt_limit_clause_init
6995
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
6996
        | opt_var_type STATUS_SYM wild_and_where
6997
          {
6998 6999 7000
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS;
7001
            lex->option_type= $1;
7002
            if (prepare_schema_table(YYTHD, lex, 0, SCH_STATUS))
7003
              MYSQL_YYABORT;
7004
	  }	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
7005
        | INNOBASE_SYM STATUS_SYM
7006
          { Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS; WARN_DEPRECATED("SHOW INNODB STATUS", "SHOW ENGINE INNODB STATUS"); }
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
7007 7008
        | MUTEX_SYM STATUS_SYM
          { Lex->sql_command = SQLCOM_SHOW_MUTEX_STATUS; }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7009 7010
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
7011
        | opt_var_type  VARIABLES wild_and_where
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7012
	  {
7013 7014 7015
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_VARIABLES;
7016
            lex->option_type= $1;
7017
            if (prepare_schema_table(YYTHD, lex, 0, SCH_VARIABLES))
7018
              MYSQL_YYABORT;
7019
	  }
7020
        | charset wild_and_where
7021 7022 7023 7024 7025
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_CHARSETS;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_CHARSETS))
7026
              MYSQL_YYABORT;
7027
          }
7028
        | COLLATION_SYM wild_and_where
7029 7030 7031 7032 7033
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_COLLATIONS;
            if (prepare_schema_table(YYTHD, lex, 0, SCH_COLLATIONS))
7034
              MYSQL_YYABORT;
7035
          }
7036
	| BERKELEY_DB_SYM LOGS_SYM
7037
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW BDB LOGS", "SHOW ENGINE BDB LOGS"); }
tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
7038
	| LOGS_SYM
7039
	  { Lex->sql_command= SQLCOM_SHOW_LOGS; WARN_DEPRECATED("SHOW LOGS", "SHOW ENGINE BDB LOGS"); }
7040 7041 7042 7043 7044
	| GRANTS
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    LEX_USER *curr_user;
7045
            if (!(curr_user= (LEX_USER*) lex->thd->alloc(sizeof(st_lex_user))))
7046
              MYSQL_YYABORT;
7047
            bzero(curr_user, sizeof(st_lex_user));
7048 7049
	    lex->grant_user= curr_user;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7050
	| GRANTS FOR_SYM user
7051 7052 7053 7054
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
7055
	    lex->grant_user->password=null_lex_str;
7056
	  }
7057
	| CREATE DATABASE opt_if_not_exists ident
7058 7059
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
7060 7061
	    Lex->create_info.options=$3;
	    Lex->name=$4.str;
7062
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7063 7064
        | CREATE TABLE_SYM table_ident
          {
7065 7066 7067
            LEX *lex= Lex;
	    lex->sql_command = SQLCOM_SHOW_CREATE;
	    if (!lex->select_lex.add_table_to_list(YYTHD, $3, NULL,0))
7068
	      MYSQL_YYABORT;
7069 7070 7071 7072 7073 7074 7075
            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))
7076
	      MYSQL_YYABORT;
7077
            lex->only_view= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7078 7079 7080 7081
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7082
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7083 7084 7085
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100
          }
	| 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;
	  }
7101
	| PROCEDURE STATUS_SYM wild_and_where
7102
	  {
7103 7104 7105
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_PROC;
7106
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
7107
	      MYSQL_YYABORT;
7108
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
7109
              MYSQL_YYABORT;
7110
	  }
7111
	| FUNCTION_SYM STATUS_SYM wild_and_where
7112
	  {
7113 7114 7115
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_SELECT;
            lex->orig_sql_command= SQLCOM_SHOW_STATUS_FUNC;
7116
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
7117
	      MYSQL_YYABORT;
7118
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
7119
              MYSQL_YYABORT;
pem@mysql.com's avatar
pem@mysql.com committed
7120 7121 7122 7123
	  }
        | PROCEDURE CODE_SYM sp_name
          {
#ifdef DBUG_OFF
7124 7125
            my_parse_error(ER(ER_SYNTAX_ERROR));
            MYSQL_YYABORT;
pem@mysql.com's avatar
pem@mysql.com committed
7126 7127 7128 7129 7130 7131 7132 7133
#else
            Lex->sql_command= SQLCOM_SHOW_PROC_CODE;
	    Lex->spname= $3;
#endif
          }
        | FUNCTION_SYM CODE_SYM sp_name
          {
#ifdef DBUG_OFF
7134 7135
            my_parse_error(ER(ER_SYNTAX_ERROR));
            MYSQL_YYABORT;
pem@mysql.com's avatar
pem@mysql.com committed
7136 7137 7138 7139 7140 7141
#else
            Lex->sql_command= SQLCOM_SHOW_FUNC_CODE;
	    Lex->spname= $3;
#endif
          }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7142

7143 7144 7145 7146
show_engine_param:
	STATUS_SYM
	  {
	    switch (Lex->create_info.db_type) {
7147 7148 7149
	    case DB_TYPE_NDBCLUSTER:
	      Lex->sql_command = SQLCOM_SHOW_NDBCLUSTER_STATUS;
	      break;
7150 7151 7152 7153
	    case DB_TYPE_INNODB:
	      Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;
	      break;
	    default:
7154
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "STATUS");
7155
	      MYSQL_YYABORT;
7156 7157 7158 7159 7160 7161 7162 7163 7164
	    }
	  }
	| LOGS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_BERKELEY_DB:
	      Lex->sql_command = SQLCOM_SHOW_LOGS;
	      break;
	    default:
7165
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "LOGS");
7166
	      MYSQL_YYABORT;
7167 7168 7169
	    }
	  };

7170 7171 7172 7173
master_or_binary:
	MASTER_SYM
	| BINARY;

7174 7175 7176 7177
opt_storage:
	/* empty */
	| STORAGE_SYM;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7178 7179
opt_db:
	/* empty */  { $$= 0; }
7180
	| from_or_in ident { $$= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7181

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7182 7183
opt_full:
	/* empty */ { Lex->verbose=0; }
7184
	| FULL	    { Lex->verbose=1; };
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7185

7186 7187
from_or_in:
	FROM
7188
	| IN_SYM;
7189

7190 7191
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7192
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
7193 7194 7195

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

7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210
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();
        }
      ;

7211

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7212 7213 7214 7215
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
7216 7217 7218 7219 7220 7221 7222 7223 7224
          LEX *lex= Lex;
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
          lex->sql_command= SQLCOM_SELECT;
          lex->orig_sql_command= SQLCOM_SHOW_FIELDS;
          lex->select_lex.db= 0;
          lex->verbose= 0;
          if (prepare_schema_table(YYTHD, lex, $2, SCH_COLUMNS))
7225
	    MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7226
	}
7227
	opt_describe_column {}
7228 7229 7230
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
7231
          {
7232
	    LEX *lex=Lex;
7233
	    lex->select_lex.options|= SELECT_DESCRIBE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7234 7235
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7236 7237 7238

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

7241 7242 7243 7244 7245
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7246 7247 7248
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
7249
	| ident
7250
	  { 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
7251 7252 7253 7254 7255


/* flush things */

flush:
7256
	FLUSH_SYM opt_no_write_to_binlog
7257 7258
	{
	  LEX *lex=Lex;
7259 7260
	  lex->sql_command= SQLCOM_FLUSH;
          lex->type= 0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7261
          lex->no_write_to_binlog= $2;
7262
	}
7263 7264 7265
	flush_options
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7266 7267 7268

flush_options:
	flush_options ',' flush_option
7269
	| flush_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7270 7271

flush_option:
7272
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7273
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7274
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7275 7276 7277 7278
	| 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
7279 7280
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
7281 7282
	| 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
7283

7284
opt_table_list:
7285 7286
	/* empty */  {;}
	| table_list {;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7287

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7288
reset:
7289 7290 7291 7292
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
7293 7294 7295 7296
	} reset_options
	{}
	;

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7297 7298
reset_options:
	reset_options ',' reset_option
7299
	| reset_option;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7300 7301

reset_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7302 7303
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
7304
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7305

7306
purge:
7307 7308 7309 7310
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
7311 7312 7313 7314 7315
	} purge_options
	{}
	;

purge_options:
7316
	master_or_binary LOGS_SYM purge_option
7317
	;
7318 7319

purge_option:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7320
        TO_SYM TEXT_STRING_sys
7321 7322 7323 7324 7325 7326
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
7327 7328 7329 7330
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
7331
	}
7332
	;
7333

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7334 7335 7336
/* kill threads */

kill:
serg@serg.mylan's avatar
serg@serg.mylan committed
7337
	KILL_SYM { Lex->sql_command= SQLCOM_KILL; } kill_option expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7338
	{
7339
	  LEX *lex=Lex;
7340
	  lex->value_list.empty();
serg@serg.mylan's avatar
serg@serg.mylan committed
7341
	  lex->value_list.push_front($4);
7342
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7343

7344 7345 7346
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
serg@serg.mylan's avatar
serg@serg.mylan committed
7347 7348
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; }
        ;
7349

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7350 7351 7352
/* change database */

use:	USE_SYM ident
7353 7354
	{
	  LEX *lex=Lex;
7355 7356
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
7357
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7358 7359 7360

/* import, export of files */

serg@serg.mylan's avatar
serg@serg.mylan committed
7361
load:   LOAD DATA_SYM
7362
        {
7363 7364 7365 7366
          THD *thd= YYTHD;
          LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;

7367 7368 7369
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD DATA");
7370
	    MYSQL_YYABORT;
7371
	  }
7372
          lex->fname_start= lip->ptr;
7373 7374 7375 7376 7377 7378
        }
        load_data
        {}
        |
        LOAD TABLE_SYM table_ident FROM MASTER_SYM
        {
7379 7380 7381 7382
	  LEX *lex=Lex;
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD TABLE");
7383
	    MYSQL_YYABORT;
7384 7385
	  }
          lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
7386
          WARN_DEPRECATED("LOAD TABLE FROM MASTER",
7387 7388
                          "mysqldump or future "
                          "BACKUP/RESTORE DATABASE facility");
7389
          if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
7390
            MYSQL_YYABORT;
7391 7392 7393
        };

load_data:
bar@mysql.com's avatar
bar@mysql.com committed
7394
	load_data_lock opt_local INFILE TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7395
	{
7396 7397
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
7398 7399
	  lex->lock_option= $1;
	  lex->local_file=  $2;
7400 7401
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
7402
	  if (!(lex->exchange= new sql_exchange($4.str, 0)))
7403
	    MYSQL_YYABORT;
7404 7405 7406
        }
        opt_duplicate INTO
        {
7407 7408 7409 7410
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;
	  lex->fname_end= lip->ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7411
	}
7412
        TABLE_SYM table_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7413
        {
7414 7415 7416
          LEX *lex=Lex;
          if (!Select->add_table_to_list(YYTHD, $10, NULL, TL_OPTION_UPDATING,
                                         lex->lock_option))
7417
            MYSQL_YYABORT;
7418 7419 7420
          lex->field_list.empty();
          lex->update_list.empty();
          lex->value_list.empty();
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7421
        }
7422 7423
        opt_load_data_charset
	{ Lex->exchange->cs= $12; }
7424 7425 7426
        opt_field_term opt_line_term opt_ignore_lines opt_field_or_var_spec
        opt_load_data_set_spec
        {}
7427
        |
7428
	FROM MASTER_SYM
7429 7430
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
7431
          WARN_DEPRECATED("LOAD DATA FROM MASTER",
7432 7433
                          "mysqldump or future "
                          "BACKUP/RESTORE DATABASE facility");
7434
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7435 7436 7437

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

7440
load_data_lock:
7441
	/* empty */	{ $$= YYTHD->update_lock_default; }
7442 7443 7444 7445 7446 7447 7448
	| CONCURRENT
          {
#ifdef HAVE_QUERY_CACHE
            /*
              Ignore this option in SP to avoid problem with query cache
            */
            if (Lex->sphead != 0)
7449 7450
              $$= YYTHD->update_lock_default;
            else
7451 7452 7453
#endif
              $$= TL_WRITE_CONCURRENT_INSERT;
          }
7454
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
7455 7456


bk@work.mysql.com's avatar
bk@work.mysql.com committed
7457 7458 7459
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
7460
	| IGNORE_SYM	{ Lex->ignore= 1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7461 7462 7463

opt_field_term:
	/* empty */
7464
	| COLUMNS field_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7465 7466 7467

field_term_list:
	field_term_list field_term
7468
	| field_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7469 7470

field_term:
7471 7472
	TERMINATED BY text_string 
          {
7473
            DBUG_ASSERT(Lex->exchange != 0);
7474 7475
            Lex->exchange->field_term= $3;
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7476
	| OPTIONALLY ENCLOSED BY text_string
7477
	  {
7478
            LEX *lex= Lex;
7479
            DBUG_ASSERT(lex->exchange != 0);
7480 7481
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
7482
	  }
7483
        | ENCLOSED BY text_string
7484
          {
7485
            DBUG_ASSERT(Lex->exchange != 0);
7486 7487
            Lex->exchange->enclosed= $3;
          }
7488
        | ESCAPED BY text_string
7489
          {
7490
            DBUG_ASSERT(Lex->exchange != 0);
7491 7492
            Lex->exchange->escaped= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7493 7494 7495

opt_line_term:
	/* empty */
7496
	| LINES line_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7497 7498 7499

line_term_list:
	line_term_list line_term
7500
	| line_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7501 7502

line_term:
7503
        TERMINATED BY text_string
7504
          {
7505
            DBUG_ASSERT(Lex->exchange != 0);
7506 7507
            Lex->exchange->line_term= $3;
          }
7508
        | STARTING BY text_string
7509
          {
7510
            DBUG_ASSERT(Lex->exchange != 0);
7511 7512
            Lex->exchange->line_start= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7513 7514 7515

opt_ignore_lines:
	/* empty */
7516 7517
        | IGNORE_SYM NUM LINES
          {
7518
            DBUG_ASSERT(Lex->exchange != 0);
7519 7520
            Lex->exchange->skip_lines= atol($2.str);
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7521

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

7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544
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
7545 7546 7547
/* Common definitions */

text_literal:
7548
	TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7549 7550
	{
	  THD *thd= YYTHD;
7551
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7552
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7553
	| NCHAR_STRING
7554
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
7555
	| UNDERSCORE_CHARSET TEXT_STRING
7556
	  { $$ = new Item_string($2.str,$2.length,Lex->underscore_charset); }
7557
	| text_literal TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7558 7559
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7560 7561

text_string:
7562
	TEXT_STRING_literal
7563
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7564 7565
	| HEX_NUM
	  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
7566
	    Item *tmp= new Item_hex_string($1.str, $1.length);
7567
	    /*
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
7568
	      it is OK only emulate fix_fields, because we need only
7569 7570 7571
              value of constant
	    */
	    $$= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7572
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
7573
	      (String*) 0;
7574
	  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
7575 7576 7577 7578 7579 7580 7581 7582 7583 7584
        | 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;
          }
7585 7586
	;

7587
param_marker:
7588
        PARAM_MARKER
7589
        {
7590
          THD *thd= YYTHD;
7591
	  LEX *lex= thd->lex;
7592
          Lex_input_stream *lip= thd->m_lip;
7593 7594 7595 7596
          Item_param *item;
          if (! lex->parsing_options.allows_variable)
          {
            my_error(ER_VIEW_SELECT_VARIABLE, MYF(0));
7597
            MYSQL_YYABORT;
7598
          }
7599
          item= new Item_param((uint) (lip->tok_start - thd->query));
7600
          if (!($$= item) || lex->param_list.push_back(item))
7601
          {
7602
            my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
7603
            MYSQL_YYABORT;
7604
          }
7605 7606 7607
        }
	;

7608 7609 7610
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
7611 7612
	| '-' NUM_literal
	  {
7613
	    $2->max_length++;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7614
	    $$= $2->neg();
7615
	  }
7616 7617 7618
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
7619 7620
literal:
	text_literal	{ $$ =	$1; }
7621
	| NUM_literal	{ $$ = $1; }
7622 7623 7624 7625 7626
	| NULL_SYM
          {
            $$ = new Item_null();
            YYTHD->m_lip->next_state=MY_LEX_OPERATOR_OR_IDENT;
          }
7627 7628
	| 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
7629 7630
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
7631
	| UNDERSCORE_CHARSET HEX_NUM
7632
	  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
7633
	    Item *tmp= new Item_hex_string($2.str, $2.length);
7634
	    /*
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7635
	      it is OK only emulate fix_fieds, because we need only
7636 7637 7638
              value of constant
	    */
	    String *str= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7639
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
7640
	      (String*) 0;
7641 7642
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
7643
				Lex->underscore_charset);
7644
	  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658
	| 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
7659 7660
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
7661
	| TIMESTAMP text_literal { $$ = $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7662

7663
NUM_literal:
7664 7665
	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); }
7666
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
7667
        | DECIMAL_NUM
7668
	{
7669
           $$= new Item_decimal($1.str, $1.length, YYTHD->charset());
7670 7671
	   if (YYTHD->net.report_error)
	   {
7672
	     MYSQL_YYABORT;
7673 7674 7675 7676 7677 7678 7679
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
7680
	     MYSQL_YYABORT;
7681 7682
	   }
	}
7683
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
7684

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7685
/**********************************************************************
serg@serg.mylan's avatar
serg@serg.mylan committed
7686
** Creating different items.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7687 7688 7689
**********************************************************************/

insert_ident:
7690
	simple_ident_nospvar { $$=$1; }
7691
	| table_wild	 { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7692 7693

table_wild:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7694
	ident '.' '*'
7695
	{
7696
          SELECT_LEX *sel= Select;
7697
	  $$ = new Item_field(Lex->current_context(), NullS, $1.str, "*");
7698
	  sel->with_wild++;
7699
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7700
	| ident '.' ident '.' '*'
7701
	{
7702
          SELECT_LEX *sel= Select;
7703
	  $$ = new Item_field(Lex->current_context(), (YYTHD->client_capabilities &
serg@serg.mylan's avatar
serg@serg.mylan committed
7704 7705
                             CLIENT_NO_SCHEMA ? NullS : $1.str),
                             $3.str,"*");
7706
	  sel->with_wild++;
7707 7708
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7709 7710

order_ident:
7711
	expr { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7712 7713

simple_ident:
7714 7715
	ident
	{
7716 7717 7718
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;
7719
	  sp_variable_t *spv;
7720
          sp_pcontext *spc = lex->spcont;
7721
	  if (spc && (spv = spc->find_variable(&$1)))
7722 7723
	  {
            /* We're compiling a stored procedure and found a variable */
7724 7725 7726
            if (! lex->parsing_options.allows_variable)
            {
              my_error(ER_VIEW_SELECT_VARIABLE, MYF(0));
7727
              MYSQL_YYABORT;
7728 7729
            }

7730
            Item_splocal *splocal;
7731
            splocal= new Item_splocal($1, spv->offset, spv->type,
7732
                                      lip->tok_start_prev - 
7733
                                      lex->sphead->m_tmp_query);
7734
#ifndef DBUG_OFF
7735
            if (splocal)
7736
              splocal->m_sp= lex->sphead;
7737
#endif
7738
	    $$ = (Item*) splocal;
7739 7740 7741 7742 7743 7744 7745
	    lex->safe_to_cache_query=0;
	  }
	  else
	  {
	    SELECT_LEX *sel=Select;
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
7746 7747
                 (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	         (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
7748 7749 7750 7751 7752 7753
	  }
        }
        | simple_ident_q { $$= $1; }
	;

simple_ident_nospvar:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7754
	ident
7755
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7756
	  SELECT_LEX *sel=Select;
7757
	  $$= (sel->parsing_place != IN_HAVING ||
7758
	       sel->get_in_sum_expr() > 0) ?
7759 7760
              (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	      (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
7761
	}
7762
	| simple_ident_q { $$= $1; }
serg@serg.mylan's avatar
serg@serg.mylan committed
7763
	;
7764 7765 7766

simple_ident_q:
	ident '.' ident
7767
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7768
	  THD *thd= YYTHD;
7769
	  LEX *lex= thd->lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
7770

7771 7772 7773 7774 7775 7776
          /*
            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
7777
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") ||
7778 7779
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
7780
            Item_trigger_field *trg_fld;
7781
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
serg@serg.mylan's avatar
serg@serg.mylan committed
7782

7783 7784 7785
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
7786
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
7787
              MYSQL_YYABORT;
7788
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
7789

7790 7791 7792
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
7793
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
7794
              MYSQL_YYABORT;
7795
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
7796

7797 7798 7799 7800 7801
            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);
7802
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
7803
                                                  new_row ?
7804 7805
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
7806
                                                  $3.str,
7807 7808
                                                  SELECT_ACL,
                                                  read_only)))
7809
              MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
7810

7811 7812 7813 7814 7815 7816
            /*
              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
7817

7818 7819 7820 7821 7822 7823 7824
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
7825 7826
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
7827 7828 7829
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
7830 7831
	        (Item*) new Item_field(Lex->current_context(), NullS, $1.str, $3.str) :
	        (Item*) new Item_ref(Lex->current_context(), NullS, $1.str, $3.str);
7832 7833
          }
        }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7834
	| '.' ident '.' ident
7835
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7836
	  THD *thd= YYTHD;
7837
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7838
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7839 7840
	  if (sel->no_table_names_allowed)
	  {
7841 7842
	    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
7843
	  }
7844
	  $$= (sel->parsing_place != IN_HAVING ||
7845
	       sel->get_in_sum_expr() > 0) ?
7846 7847
	      (Item*) new Item_field(Lex->current_context(), NullS, $2.str, $4.str) :
              (Item*) new Item_ref(Lex->current_context(), NullS, $2.str, $4.str);
7848
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7849
	| ident '.' ident '.' ident
7850
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7851
	  THD *thd= YYTHD;
7852
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7853
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7854 7855
	  if (sel->no_table_names_allowed)
	  {
7856 7857
	    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
7858
	  }
7859
	  $$= (sel->parsing_place != IN_HAVING ||
7860
	       sel->get_in_sum_expr() > 0) ?
7861
	      (Item*) new Item_field(Lex->current_context(),
7862
                                     (YYTHD->client_capabilities &
7863 7864
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
7865
	      (Item*) new Item_ref(Lex->current_context(),
7866
                                   (YYTHD->client_capabilities &
7867
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
7868
                                   $3.str, $5.str);
7869
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7870 7871 7872 7873


field_ident:
	ident			{ $$=$1;}
7874 7875 7876 7877 7878
	| ident '.' ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->db))
            {
7879
              my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
7880
              MYSQL_YYABORT;
7881
            }
7882 7883
            if (my_strcasecmp(table_alias_charset, $3.str,
                              table->table_name))
7884
            {
7885
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3.str);
7886
              MYSQL_YYABORT;
7887 7888 7889 7890 7891 7892 7893 7894
            }
            $$=$5;
          }
	| ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->alias))
            {
7895
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $1.str);
7896
              MYSQL_YYABORT;
7897 7898 7899
            }
            $$=$3;
          }
7900
	| '.' ident		{ $$=$2;}	/* For Delphi */;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7901 7902 7903

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

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

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7912
IDENT_sys:
7913 7914 7915 7916 7917
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
7918 7919
            {
              CHARSET_INFO *cs= system_charset_info;
7920
              int dummy_error;
7921 7922
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
7923
                                                   $1.length, &dummy_error);
7924 7925
              if (wlen < $1.length)
              {
7926 7927
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
7928
                MYSQL_YYABORT;
7929
              }
7930
	      $$= $1;
7931
            }
7932 7933 7934 7935
	    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
7936 7937 7938 7939 7940 7941
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7942 7943
	  if (thd->charset_is_system_charset)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7944
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7945 7946
	    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
7947 7948 7949
	}
	;

7950
TEXT_STRING_literal:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7951 7952 7953
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7954 7955
	  if (thd->charset_is_collation_connection)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7956
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7957 7958
	    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
7959 7960 7961 7962
	}
	;


bar@mysql.com's avatar
bar@mysql.com committed
7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974
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
7975
ident:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7976
	IDENT_sys	    { $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7977 7978
	| keyword
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7979 7980 7981
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
7982 7983
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7984

7985 7986 7987 7988 7989 7990 7991 7992 7993 7994
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
7995
ident_or_text:
serg@serg.mylan's avatar
serg@serg.mylan committed
7996
        ident                   { $$=$1;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7997 7998
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7999 8000 8001 8002

user:
	ident_or_text
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8003 8004
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
8005
	    MYSQL_YYABORT;
8006 8007 8008
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
8009

8010
	  if (check_string_length(&$$->user,
gluh@mysql.com/gluh.(none)'s avatar
gluh@mysql.com/gluh.(none) committed
8011
                                  ER(ER_USERNAME), USERNAME_LENGTH))
8012
	    MYSQL_YYABORT;
8013
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8014 8015
	| ident_or_text '@' ident_or_text
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8016 8017
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
8018
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8019
	    $$->user = $1; $$->host=$3;
8020

8021
	    if (check_string_length(&$$->user,
gluh@mysql.com/gluh.(none)'s avatar
gluh@mysql.com/gluh.(none) committed
8022
                                    ER(ER_USERNAME), USERNAME_LENGTH) ||
8023
	        check_string_length(&$$->host,
gluh@mysql.com/gluh.(none)'s avatar
gluh@mysql.com/gluh.(none) committed
8024
                                    ER(ER_HOSTNAME), HOSTNAME_LENGTH))
8025
	      MYSQL_YYABORT;
8026
	  }
8027
	| CURRENT_USER optional_braces
8028
	{
8029
          if (!($$=(LEX_USER*) YYTHD->alloc(sizeof(st_lex_user))))
8030
            MYSQL_YYABORT;
8031 8032 8033 8034 8035 8036
          /* 
            empty LEX_USER means current_user and 
            will be handled in the  get_current_user() function
            later
          */
          bzero($$, sizeof(LEX_USER));
8037
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8038

8039
/* Keyword that we allow for identifiers (except SP labels) */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8040
keyword:
8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076
	keyword_sp		{}
	| ASCII_SYM		{}
	| BACKUP_SYM		{}
	| BEGIN_SYM		{}
	| BYTE_SYM		{}
	| CACHE_SYM		{}
	| CHARSET		{}
	| CHECKSUM_SYM		{}
	| CLOSE_SYM		{}
	| COMMENT_SYM		{}
	| COMMIT_SYM		{}
	| CONTAINS_SYM          {}
        | DEALLOCATE_SYM        {}
	| DO_SYM		{}
	| END			{}
	| EXECUTE_SYM		{}
	| FLUSH_SYM		{}
	| HANDLER_SYM		{}
	| HELP_SYM		{}
	| LANGUAGE_SYM          {}
	| NO_SYM		{}
	| OPEN_SYM		{}
        | PREPARE_SYM           {}
	| REPAIR		{}
	| RESET_SYM		{}
	| RESTORE_SYM		{}
	| ROLLBACK_SYM		{}
	| SAVEPOINT_SYM		{}
	| SECURITY_SYM		{}
	| SIGNED_SYM		{}
	| SLAVE			{}
	| START_SYM		{}
	| STOP_SYM		{}
	| TRUNCATE_SYM		{}
	| UNICODE_SYM		{}
        | XA_SYM                {}
8077
        | UPGRADE_SYM           {}
8078 8079 8080 8081
	;

/*
 * Keywords that we allow for labels in SPs.
pem@mysql.com's avatar
pem@mysql.com committed
8082 8083 8084
 * Anything that's the beginning of a statement or characteristics
 * must be in keyword above, otherwise we get (harmful) shift/reduce
 * conflicts.
8085 8086
 */
keyword_sp:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8087
	ACTION			{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
8088
	| ADDDATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8089
	| AFTER_SYM		{}
8090
	| AGAINST		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8091
	| AGGREGATE_SYM		{}
8092
	| ALGORITHM_SYM		{}
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8093
	| ANY_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8094
	| AUTO_INC		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8095 8096
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
8097
	| BERKELEY_DB_SYM	{}
8098
	| BINLOG_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8099 8100
	| BIT_SYM		{}
	| BOOL_SYM		{}
8101
	| BOOLEAN_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8102
	| BTREE_SYM		{}
8103
	| CASCADED              {}
8104
	| CHAIN_SYM		{}
8105
	| CHANGED		{}
8106
	| CIPHER_SYM		{}
8107
	| CLIENT_SYM		{}
pem@mysql.com's avatar
pem@mysql.com committed
8108
        | CODE_SYM              {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8109
	| COLLATION_SYM		{}
8110
        | COLUMNS               {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8111
	| COMMITTED_SYM		{}
8112
	| COMPACT_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8113
	| COMPRESSED_SYM	{}
8114
	| CONCURRENT		{}
antony@ppcg5.local's avatar
antony@ppcg5.local committed
8115
	| CONNECTION_SYM	{}
8116
	| CONSISTENT_SYM	{}
8117
	| CUBE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8118 8119 8120 8121
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
8122
	| DEFINER_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8123
	| DELAY_KEY_WRITE_SYM	{}
8124 8125
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
8126
	| DISCARD		{}
8127 8128
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8129 8130
	| DYNAMIC_SYM		{}
	| ENUM			{}
8131
	| ENGINE_SYM		{}
8132
	| ENGINES_SYM		{}
8133
	| ERRORS		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8134
	| ESCAPE_SYM		{}
8135
	| EVENTS_SYM		{}
8136
        | EXPANSION_SYM         {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8137
	| EXTENDED_SYM		{}
8138
	| FAST_SYM		{}
8139
	| FOUND_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8140 8141
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8142
	| FULL			{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8143 8144 8145
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
8146
	| FRAC_SECOND_SYM	{}
8147
	| GEOMETRY_SYM		{}
8148
	| GEOMETRYCOLLECTION	{}
8149
	| GET_FORMAT		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8150
	| GRANTS		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8151
	| GLOBAL_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8152
	| HASH_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8153 8154 8155
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
8156
	| INVOKER_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
8157
	| IMPORT		{}
8158
	| INDEXES		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8159
	| ISOLATION		{}
8160
	| ISSUER_SYM		{}
8161
	| INNOBASE_SYM		{}
8162
	| INSERT_METHOD		{}
8163
	| RELAY_THREAD		{}
8164
	| LAST_SYM		{}
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
8165
	| LEAVES                {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8166
	| LEVEL_SYM		{}
8167
	| LINESTRING		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8168
	| LOCAL_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8169
	| LOCKS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8170 8171 8172 8173 8174 8175 8176 8177 8178
	| 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	{}
8179
	| MASTER_SERVER_ID_SYM  {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8180
	| MASTER_CONNECT_RETRY_SYM	{}
8181 8182 8183 8184 8185 8186
	| 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
8187 8188 8189
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
8190
	| MAX_USER_CONNECTIONS_SYM {}
8191
	| MEDIUM_SYM		{}
8192
	| MERGE_SYM		{}
8193
	| MICROSECOND_SYM	{}
8194
        | MIGRATE_SYM           {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8195 8196 8197
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8198
	| MODE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8199
	| MONTH_SYM		{}
8200 8201 8202
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
8203
        | MUTEX_SYM             {}
8204
	| NAME_SYM              {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8205
	| NAMES_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8206 8207
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
8208
	| NDBCLUSTER_SYM	{}
8209
	| NEXT_SYM		{}
8210
	| NEW_SYM		{}
8211
	| NONE_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8212
	| NVARCHAR_SYM		{}
8213
	| OFFSET_SYM		{}
8214
	| OLD_PASSWORD		{}
8215
	| ONE_SHOT_SYM		{}
8216
        | ONE_SYM               {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8217
	| PACK_KEYS_SYM		{}
8218
	| PARTIAL		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8219
	| PASSWORD		{}
8220
        | PHASE_SYM             {}
8221
	| POINT_SYM		{}
8222
	| POLYGON		{}
8223
	| PREV_SYM		{}
8224
        | PRIVILEGES            {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8225 8226
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
8227
	| QUARTER_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8228
	| QUERY_SYM		{}
8229
	| QUICK			{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8230
	| RAID_0_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8231 8232
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8233
	| RAID_STRIPED_SYM	{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8234
	| RAID_TYPE		{}
8235
        | RECOVER_SYM           {}
serg@serg.mylan's avatar
serg@serg.mylan committed
8236
        | REDUNDANT_SYM         {}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8237 8238
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8239
	| RELOAD		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8240
	| REPEATABLE_SYM	{}
8241
	| REPLICATION		{}
8242
	| RESOURCES		{}
8243
        | RESUME_SYM            {}
8244
	| RETURNS_SYM           {}
8245
	| ROLLUP_SYM		{}
8246
	| ROUTINE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8247 8248 8249
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8250
	| RTREE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8251
	| SECOND_SYM		{}
8252
	| SERIAL_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8253 8254
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
8255
	| SIMPLE_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8256
	| SHARE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8257
	| SHUTDOWN		{}
8258
	| SNAPSHOT_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8259
	| SOUNDS_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8260
	| SQL_CACHE_SYM		{}
8261
	| SQL_BUFFER_RESULT	{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8262
	| SQL_NO_CACHE_SYM	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8263
	| SQL_THREAD		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8264
	| STATUS_SYM		{}
8265
	| STORAGE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8266
	| STRING_SYM		{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
8267
	| SUBDATE_SYM		{}
8268
	| SUBJECT_SYM		{}
8269
	| SUPER_SYM		{}
8270
        | SUSPEND_SYM           {}
8271
        | TABLES                {}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
8272
	| TABLESPACE		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8273
	| TEMPORARY		{}
8274
	| TEMPTABLE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8275
	| TEXT_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8276
	| TRANSACTION_SYM	{}
8277
	| TRIGGERS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8278
	| TIMESTAMP		{}
8279 8280
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8281
	| TIME_SYM		{}
8282
	| TYPES_SYM		{}
8283
        | TYPE_SYM              {}
serg@serg.mylan's avatar
serg@serg.mylan committed
8284
        | UDF_RETURNS_SYM       {}
8285
	| FUNCTION_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8286
	| UNCOMMITTED_SYM	{}
8287
	| UNDEFINED_SYM		{}
8288
	| UNKNOWN_SYM		{}
8289
	| UNTIL_SYM		{}
8290
	| USER			{}
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8291
	| USE_FRM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8292
	| VARIABLES		{}
8293
	| VIEW_SYM		{}
8294
	| VALUE_SYM		{}
8295
	| WARNINGS		{}
8296
	| WEEK_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8297
	| WORK_SYM		{}
8298
	| X509_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8299 8300
	| YEAR_SYM		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8301 8302 8303 8304 8305 8306

/* Option functions */

set:
	SET opt_option
	{
8307 8308
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
8309
	  mysql_init_select(lex);
8310
	  lex->option_type=OPT_SESSION;
8311
	  lex->var_list.empty();
8312
          lex->one_shot_set= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8313
	}
8314 8315 8316
	option_value_list
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8317 8318 8319

opt_option:
	/* empty */ {}
8320
	| OPTION {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8321 8322

option_value_list:
8323 8324 8325 8326 8327
	option_type_value
	| option_value_list ',' option_type_value;

option_type_value:
        {
8328 8329 8330 8331 8332
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;

          if (lex->sphead)
8333 8334 8335 8336
          {
            /*
              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
8337 8338
              and sp_instr_set_trigger instructions share one LEX.
              (Well, it is theoretically possible but adds some extra
8339 8340 8341 8342 8343
               overhead on preparation for execution stage and IMO less
               robust).

              QQ: May be we should simply prohibit group assignments in SP?
            */
8344 8345
            Lex->sphead->reset_lex(thd);
            lex= thd->lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
8346

8347 8348 8349 8350 8351 8352
            /* 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;
8353
	    lex->sphead->m_tmp_query= lip->tok_start;
8354 8355
          }
        }
8356
	ext_option_value
8357
        {
8358 8359 8360
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;
serg@serg.mylan's avatar
serg@serg.mylan committed
8361

8362 8363 8364
          if (lex->sphead)
          {
            sp_head *sp= lex->sphead;
serg@serg.mylan's avatar
serg@serg.mylan committed
8365

8366 8367 8368 8369 8370 8371 8372 8373 8374
	    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
8375

8376 8377
              if (!(i= new sp_instr_stmt(sp->instructions(), lex->spcont,
                                         lex)))
8378
                MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
8379

8380 8381
              /*
                Extract the query statement from the tokenizer.  The
8382 8383
                end is either lip->ptr, if there was no lookahead,
                lip->tok_end otherwise.
8384 8385
              */
              if (yychar == YYEMPTY)
8386
                qbuff.length= lip->ptr - sp->m_tmp_query;
8387
              else
8388
                qbuff.length= lip->tok_end - sp->m_tmp_query;
serg@serg.mylan's avatar
serg@serg.mylan committed
8389

8390
              if (!(qbuff.str= alloc_root(thd->mem_root, qbuff.length + 5)))
8391
                MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
8392

8393
              strmake(strmake(qbuff.str, "SET ", 4), sp->m_tmp_query,
8394 8395 8396 8397 8398
                      qbuff.length);
              qbuff.length+= 4;
              i->m_query= qbuff;
              sp->add_instr(i);
            }
8399
            lex->sphead->restore_lex(thd);
8400 8401
          }
        };
8402 8403

option_type:
8404 8405 8406 8407 8408 8409 8410 8411 8412
        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; }
8413 8414 8415 8416
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8417
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
8418 8419 8420 8421 8422 8423
	| 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
8424
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
8425 8426 8427
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8428

8429 8430 8431
ext_option_value:
        sys_option_value
        | option_type2 option_value;
8432

8433 8434 8435 8436
sys_option_value:
        option_type internal_variable_name equal set_expr_or_default
        {
          LEX *lex=Lex;
8437

8438 8439 8440 8441
          if ($2.var == &trg_new_row_fake_var)
          {
            /* We are in trigger and assigning value to field of new row */
            Item *it;
8442
            Item_trigger_field *trg_fld;
8443 8444
            sp_instr_set_trigger_field *sp_fld;
	    LINT_INIT(sp_fld);
8445 8446
            if ($1)
            {
8447 8448
              my_parse_error(ER(ER_SYNTAX_ERROR));
              MYSQL_YYABORT;
8449
            }
8450 8451
            if ($4)
              it= $4;
8452
            else
8453 8454 8455 8456
            {
              /* QQ: Shouldn't this be field's default value ? */
              it= new Item_null();
            }
8457

8458 8459 8460
            DBUG_ASSERT(lex->trg_chistics.action_time == TRG_ACTION_BEFORE &&
                        (lex->trg_chistics.event == TRG_EVENT_INSERT ||
                         lex->trg_chistics.event == TRG_EVENT_UPDATE));
8461
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
8462
                                                  Item_trigger_field::NEW_ROW,
8463
                                                  $2.base_name.str,
8464
                                                  UPDATE_ACL, FALSE)) ||
8465 8466 8467 8468 8469
                !(sp_fld= new sp_instr_set_trigger_field(lex->sphead->
                          	                         instructions(),
                                	                 lex->spcont,
							 trg_fld,
                                        	         it, lex)))
8470
              MYSQL_YYABORT;
8471

8472 8473 8474 8475
            /*
              Let us add this item to list of all Item_trigger_field
              objects in trigger.
            */
8476 8477
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
                                    (byte **)&trg_fld->next_trg_field);
8478

8479
            lex->sphead->add_instr(sp_fld);
8480 8481 8482 8483
          }
          else if ($2.var)
          { /* System variable */
            if ($1)
8484
              lex->option_type= $1;
8485 8486 8487 8488 8489 8490 8491
            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;
8492
            sp_variable_t *spv;
8493
            sp_instr_set *sp_set;
8494 8495 8496
            Item *it;
            if ($1)
            {
8497 8498
              my_parse_error(ER(ER_SYNTAX_ERROR));
              MYSQL_YYABORT;
8499 8500
            }

8501
            spv= ctx->find_variable(&$2.base_name);
8502 8503 8504 8505 8506 8507 8508

            if ($4)
              it= $4;
            else if (spv->dflt)
              it= spv->dflt;
            else
              it= new Item_null();
8509 8510 8511
            sp_set= new sp_instr_set(lex->sphead->instructions(), ctx,
                                     spv->offset, it, spv->type, lex, TRUE);
            lex->sphead->add_instr(sp_set);
8512 8513 8514 8515 8516
          }
        }
        | option_type TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	{
	  LEX *lex=Lex;
8517
          if ($1)
8518
            lex->option_type= $1;
8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530
	  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)));
	}
8531
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
8532 8533
	  {
	    LEX *lex=Lex;
8534
	    lex->var_list.push_back(new set_var($3, $4.var, &$4.base_name, $6));
8535
	  }
8536
	| charset old_or_new_charset_name_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8537
	{
8538
	  THD *thd= YYTHD;
8539
	  LEX *lex= Lex;
8540
	  $2= $2 ? $2: global_system_variables.character_set_client;
8541
	  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
8542
	}
8543 8544 8545 8546 8547 8548 8549 8550
        | NAMES_SYM equal expr
	  {
	    LEX *lex= Lex;
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING names;

	    names.str= (char *)"names";
	    names.length= 5;
8551
	    if (spc && spc->find_variable(&names))
8552
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), names.str);
8553
            else
8554
              my_parse_error(ER(ER_SYNTAX_ERROR));
8555

8556
	    MYSQL_YYABORT;
8557
	  }
8558
	| NAMES_SYM charset_name_or_default opt_collate
8559
	{
8560
	  LEX *lex= Lex;
8561
	  $2= $2 ? $2 : global_system_variables.character_set_client;
8562 8563
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
8564
	  {
8565 8566
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
8567
	    MYSQL_YYABORT;
8568
	  }
8569
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
8570
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8571
	| PASSWORD equal text_or_password
8572
	  {
8573
	    THD *thd=YYTHD;
8574
	    LEX_USER *user;
8575 8576 8577 8578 8579 8580
	    LEX *lex= Lex;	    
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING pw;

	    pw.str= (char *)"password";
	    pw.length= 8;
8581
	    if (spc && spc->find_variable(&pw))
8582 8583
	    {
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), pw.str);
8584
	      MYSQL_YYABORT;
8585
	    }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8586
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
8587
	      MYSQL_YYABORT;
8588
	    user->host=null_lex_str;
8589
	    user->user.str=thd->security_ctx->priv_user;
8590
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
8591 8592
	  }
	| PASSWORD FOR_SYM user equal text_or_password
8593
	  {
8594 8595
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
8596
	;
8597

8598 8599 8600
internal_variable_name:
	ident
	{
8601 8602
	  LEX *lex= Lex;
          sp_pcontext *spc= lex->spcont;
8603
	  sp_variable_t *spv;
8604 8605

	  /* We have to lookup here since local vars can shadow sysvars */
8606
	  if (!spc || !(spv = spc->find_variable(&$1)))
8607 8608 8609 8610
	  {
            /* Not an SP local variable */
	    sys_var *tmp=find_sys_var($1.str, $1.length);
	    if (!tmp)
8611
	      MYSQL_YYABORT;
8612
	    $$.var= tmp;
8613
	    $$.base_name= null_lex_str;
8614 8615 8616 8617
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
8618 8619
            if (tmp == &sys_time_zone &&
                lex->add_time_zone_tables_to_query_tables(YYTHD))
8620
              MYSQL_YYABORT;
8621 8622 8623 8624 8625 8626 8627 8628
            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;
            }
8629 8630 8631 8632 8633 8634 8635
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
8636
	}
8637 8638
	| ident '.' ident
	  {
8639
            LEX *lex= Lex;
8640 8641
            if (check_reserved_words(&$1))
            {
8642 8643
              my_parse_error(ER(ER_SYNTAX_ERROR));
              MYSQL_YYABORT;
8644
            }
8645 8646 8647 8648 8649 8650
            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')
              {
8651
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
8652
                MYSQL_YYABORT;
8653 8654 8655
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
8656 8657
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
8658
                MYSQL_YYABORT;
8659 8660 8661
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
8662
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
8663
                MYSQL_YYABORT;
8664 8665 8666 8667 8668 8669 8670 8671 8672
              }
              /* This special combination will denote field of NEW row */
              $$.var= &trg_new_row_fake_var;
              $$.base_name= $3;
            }
            else
            {
              sys_var *tmp=find_sys_var($3.str, $3.length);
              if (!tmp)
8673
                MYSQL_YYABORT;
8674
              if (!tmp->is_struct())
8675
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
8676 8677 8678
              $$.var= tmp;
              $$.base_name= $1;
            }
8679 8680 8681 8682 8683
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
8684
	      MYSQL_YYABORT;
8685
	    if (!tmp->is_struct())
8686
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8687 8688 8689
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
8690
	  }
8691
        ;
8692 8693 8694 8695 8696 8697 8698

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8700 8701 8702 8703
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
8704
	    $$= $3.length ? YYTHD->variables.old_passwords ?
8705 8706 8707 8708 8709 8710 8711 8712
	        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;
8713 8714
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8715

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

8717
set_expr_or_default:
8718 8719
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
8720 8721
	| 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
8722
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
8723
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8724 8725


bk@work.mysql.com's avatar
bk@work.mysql.com committed
8726 8727 8728 8729 8730
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
8731 8732 8733 8734
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
8735
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOCK");
8736
	    MYSQL_YYABORT;
8737 8738
	  }
	  lex->sql_command= SQLCOM_LOCK_TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8739
	}
8740 8741
	table_lock_list
	{}
8742
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8743 8744 8745

table_or_tables:
	TABLE_SYM
8746
	| TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8747 8748 8749

table_lock_list:
	table_lock
8750
	| table_lock_list ',' table_lock;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8751 8752 8753

table_lock:
	table_ident opt_table_alias lock_option
8754
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8755
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
8756
	   MYSQL_YYABORT;
8757
	}
8758
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8759 8760 8761

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
8762
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8763
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
8764 8765
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8766 8767

unlock:
8768 8769 8770 8771 8772 8773
	UNLOCK_SYM
	{
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
8774
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "UNLOCK");
8775
	    MYSQL_YYABORT;
8776 8777 8778 8779 8780
	  }
	  lex->sql_command= SQLCOM_UNLOCK_TABLES;
	}
	table_or_tables
	{}
8781
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8782 8783


8784
/*
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8785
** Handler: direct access to ISAM functions
8786 8787 8788 8789 8790
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
8791
	  LEX *lex= Lex;
8792 8793 8794
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
8795
	    MYSQL_YYABORT;
8796
	  }
8797
	  lex->sql_command = SQLCOM_HA_OPEN;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8798
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
8799
	    MYSQL_YYABORT;
8800
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
8801
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
8802
	{
8803
	  LEX *lex= Lex;
8804 8805 8806
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
8807
	    MYSQL_YYABORT;
8808
	  }
8809
	  lex->sql_command = SQLCOM_HA_CLOSE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8810
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
8811
	    MYSQL_YYABORT;
8812
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
8813
	| HANDLER_SYM table_ident_nodb READ_SYM
8814
	{
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
8815
	  LEX *lex=Lex;
8816 8817 8818
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
8819
	    MYSQL_YYABORT;
8820
	  }
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
8821 8822
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
8823
	  lex->current_select->select_limit= new Item_int((int32) 1);
8824
	  lex->current_select->offset_limit= 0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8825
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
8826
	    MYSQL_YYABORT;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8827
        }
8828
        handler_read_or_scan where_clause opt_limit_clause {}
8829
        ;
8830

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8831
handler_read_or_scan:
8832 8833
	handler_scan_function         { Lex->ident= null_lex_str; }
        | ident handler_rkey_function { Lex->ident= $1; }
8834
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8835 8836 8837

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
8838 8839
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8840 8841 8842 8843 8844 8845

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;  }
8846 8847
	| handler_rkey_mode
	{
8848 8849
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
8850
	  lex->ha_rkey_mode=$1;
8851
	  if (!(lex->insert_list = new List_item))
8852
	    MYSQL_YYABORT;
8853 8854
	} '(' values ')' { }
        ;
8855 8856

handler_rkey_mode:
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
8857 8858 8859 8860
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
8861 8862
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
8863

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8864 8865 8866
/* GRANT / REVOKE */

revoke:
8867
	REVOKE clear_privileges revoke_command
8868 8869 8870 8871
	{}
        ;

revoke_command:
8872
	grant_privileges ON opt_table grant_ident FROM grant_list
8873
	{
8874 8875 8876 8877 8878 8879 8880 8881 8882 8883
          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)
          {
8884 8885
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896
          }
	  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)
          {
8897 8898
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
8899 8900 8901
          }
	  lex->sql_command= SQLCOM_REVOKE;
          lex->type= TYPE_ENUM_PROCEDURE;
8902
        }
8903
	|
8904
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
8905 8906 8907
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
8908
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8909 8910

grant:
8911 8912 8913 8914 8915 8916
	GRANT clear_privileges grant_command
	{}
        ;

grant_command:
	grant_privileges ON opt_table grant_ident TO_SYM grant_list
8917
	require_clause grant_options
8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929
	{
          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)
          {
8930 8931
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942
          }
          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)
          {
8943 8944
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
8945 8946 8947 8948 8949
          }
          lex->sql_command= SQLCOM_GRANT;
          lex->type= TYPE_ENUM_PROCEDURE;
        }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8950

8951 8952 8953 8954
opt_table:
	/* Empty */
	| TABLE_SYM ;
        
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8955
grant_privileges:
8956 8957 8958 8959 8960 8961
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
8962
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8963

8964 8965 8966 8967 8968
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

8969 8970 8971
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8972

8973
object_privilege:
serg@serg.mylan's avatar
serg@serg.mylan committed
8974
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
8975 8976 8977
	| 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 {}
8978 8979
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
8980
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8981 8982 8983
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
8984
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8985 8986 8987 8988
	| RELOAD	{ Lex->grant |= RELOAD_ACL;}
	| SHUTDOWN	{ Lex->grant |= SHUTDOWN_ACL;}
	| PROCESS	{ Lex->grant |= PROCESS_ACL;}
	| FILE_SYM	{ Lex->grant |= FILE_ACL;}
8989 8990 8991 8992 8993
	| 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; }
8994 8995 8996 8997
	| 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; }
8998 8999
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
9000
	| CREATE USER { Lex->grant |= CREATE_USER_ACL; }
9001
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9002

9003

9004 9005
opt_and:
	/* empty */	{}
9006
	| AND_SYM	{}
9007 9008 9009 9010 9011 9012 9013 9014 9015
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
9016 9017 9018 9019
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
9020
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
9021
	    MYSQL_YYABORT;
9022 9023 9024 9025 9026 9027 9028 9029
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
9030
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
9031
	    MYSQL_YYABORT;
9032 9033 9034 9035 9036 9037 9038 9039
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
9040
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
9041
	    MYSQL_YYABORT;
9042 9043 9044 9045
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
9046

9047
grant_ident:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9048 9049
	'*'
	  {
9050
	    LEX *lex= Lex;
9051 9052
            THD *thd= lex->thd;
            if (thd->copy_db_to(&lex->current_select->db, NULL))
9053
              MYSQL_YYABORT;
9054
	    if (lex->grant == GLOBAL_ACLS)
9055 9056
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9057
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9058 9059
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
9060
	      MYSQL_YYABORT;
9061
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9062 9063 9064
	  }
	| ident '.' '*'
	  {
9065
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9066
	    lex->current_select->db = $1.str;
9067
	    if (lex->grant == GLOBAL_ACLS)
9068 9069
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9070
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9071 9072
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
9073
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9074 9075 9076 9077
	    }
	  }
	| '*' '.' '*'
	  {
9078
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9079
	    lex->current_select->db = NULL;
9080 9081
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
9082
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9083
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9084 9085
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
9086
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9087 9088 9089 9090
	    }
	  }
	| table_ident
	  {
9091
	    LEX *lex=Lex;
9092 9093
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,
                                                        TL_OPTION_UPDATING))
9094
	      MYSQL_YYABORT;
9095
	    if (lex->grant == GLOBAL_ACLS)
9096
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
9097 9098
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9099 9100 9101


user_list:
9102
	user  { if (Lex->users_list.push_back($1)) MYSQL_YYABORT;}
9103 9104 9105
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
9106
	      MYSQL_YYABORT;
9107 9108 9109 9110 9111
	  }
	;


grant_list:
9112
	grant_user  { if (Lex->users_list.push_back($1)) MYSQL_YYABORT;}
9113
	| grant_list ',' grant_user
9114 9115
	  {
	    if (Lex->users_list.push_back($3))
9116
	      MYSQL_YYABORT;
9117 9118
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9119 9120 9121 9122 9123 9124 9125 9126


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
9127
             if (YYTHD->variables.old_passwords)
9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144
             {
               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
9145 9146 9147
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
9148
	  { $$= $1; $1->password= $5; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9149
	| user
9150
	  { $$= $1; $1->password= null_lex_str; }
9151
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9152 9153 9154


opt_column_list:
9155 9156 9157 9158 9159
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
9160
	| '(' column_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9161 9162 9163

column_list:
	column_list ',' column_list_id
9164
	| column_list_id;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9165 9166 9167 9168

column_list_id:
	ident
	{
9169
	  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
9170 9171
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
9172
	  LEX *lex=Lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9173 9174
	  while ((point=iter++))
	  {
9175 9176
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9177 9178
		break;
	  }
9179
	  lex->grant_tot_col|= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9180
	  if (point)
9181
	    point->rights |= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9182
	  else
9183
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
9184 9185
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9186

tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
9187 9188

require_clause: /* empty */
9189
        | REQUIRE_SYM require_list
9190 9191 9192
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
9193
        | REQUIRE_SYM SSL_SYM
9194 9195 9196
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
9197
        | REQUIRE_SYM X509_SYM
9198 9199 9200 9201 9202 9203 9204
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
9205
          ;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
9206

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9207
grant_options:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9208
	/* empty */ {}
9209
	| WITH grant_option_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9210

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9211 9212
grant_option_list:
	grant_option_list grant_option {}
9213 9214
	| grant_option {}
        ;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9215 9216 9217

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
serg@serg.mylan's avatar
serg@serg.mylan committed
9218
        | MAX_QUERIES_PER_HOUR ulong_num
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9219
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
9220 9221 9222
	  LEX *lex=Lex;
	  lex->mqh.questions=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
9223
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
9224
        | MAX_UPDATES_PER_HOUR ulong_num
9225
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
9226 9227 9228
	  LEX *lex=Lex;
	  lex->mqh.updates=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
9229
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
9230
        | MAX_CONNECTIONS_PER_HOUR ulong_num
9231
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
9232 9233 9234
	  LEX *lex=Lex;
	  lex->mqh.conn_per_hour= $2;
	  lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
9235
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
9236
        | MAX_USER_CONNECTIONS_SYM ulong_num
9237
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
9238 9239 9240
	  LEX *lex=Lex;
          lex->mqh.user_conn= $2;
          lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
9241 9242
	}
        ;
9243

bk@work.mysql.com's avatar
bk@work.mysql.com committed
9244
begin:
serg@serg.mylan's avatar
serg@serg.mylan committed
9245 9246 9247 9248 9249 9250 9251
	BEGIN_SYM  
        {
	  LEX *lex=Lex;
          lex->sql_command = SQLCOM_BEGIN;
          lex->start_transaction_opt= 0;
        }
        opt_work {}
9252
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9253 9254 9255

opt_work:
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
9256
	| WORK_SYM  {}
9257
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9258

9259
opt_chain:
serg@serg.mylan's avatar
serg@serg.mylan committed
9260
	/* empty */ { $$= (YYTHD->variables.completion_type == 1); }
9261 9262 9263 9264 9265
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
serg@serg.mylan's avatar
serg@serg.mylan committed
9266
	/* empty */ { $$= (YYTHD->variables.completion_type == 2); }
9267 9268 9269 9270 9271 9272 9273 9274 9275
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
9276
commit:
serg@serg.mylan's avatar
serg@serg.mylan committed
9277
	COMMIT_SYM opt_work opt_chain opt_release
9278
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
9279 9280 9281 9282
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_COMMIT;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
9283 9284
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9285 9286

rollback:
serg@serg.mylan's avatar
serg@serg.mylan committed
9287
	ROLLBACK_SYM opt_work opt_chain opt_release
9288
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
9289 9290 9291 9292
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
9293
	}
9294 9295
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
9296
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
9297 9298 9299
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK_TO_SAVEPOINT;
	  lex->ident= $5;
9300 9301 9302
	}
	;

9303 9304 9305
savepoint:
	SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
9306 9307 9308
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SAVEPOINT;
	  lex->ident= $2;
9309 9310 9311 9312 9313 9314
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
9315 9316 9317
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RELEASE_SAVEPOINT;
	  lex->ident= $3;
9318 9319
	}
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
9320
  
9321
/*
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9322
   UNIONS : glue selects together
9323 9324 9325
*/


9326
union_clause:
9327
	/* empty */ {}
9328 9329
	| union_list
	;
9330 9331

union_list:
9332
	UNION_SYM union_option
9333 9334
	{
	  LEX *lex=Lex;
9335
	  if (lex->result)
9336 9337
	  {
	    /* Only the last SELECT can have  INTO...... */
9338
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
9339
	    MYSQL_YYABORT;
9340
	  }
9341
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
9342
	  {
9343 9344
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
9345
	  }
9346 9347
          /* This counter shouldn't be incremented for UNION parts */
          Lex->nest_level--;
9348
	  if (mysql_new_select(lex, 0))
9349
	    MYSQL_YYABORT;
9350
          mysql_init_select(lex);
9351
	  lex->current_select->linkage=UNION_TYPE;
9352 9353 9354
          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
9355
	}
9356 9357
	select_init
        {
9358 9359 9360 9361
          /*
	    Remove from the name resolution context stack the context of the
            last select in the union.
	  */
9362 9363
          Lex->pop_context();
        }
9364
	;
9365 9366

union_opt:
9367 9368 9369
	/* Empty */ { $$= 0; }
	| union_list { $$= 1; }
	| union_order_or_limit { $$= 1; }
9370
	;
9371

9372
union_order_or_limit:
9373
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9374
	    THD *thd= YYTHD;
9375
	    LEX *lex= thd->lex;
9376
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9377
	    SELECT_LEX *sel= lex->current_select;
9378
	    SELECT_LEX_UNIT *unit= sel->master_unit();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9379
	    SELECT_LEX *fake= unit->fake_select_lex;
9380 9381 9382 9383 9384 9385
	    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
9386
	    thd->where= "global ORDER clause";
9387
	  }
9388
	order_or_limit
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9389 9390
          {
	    THD *thd= YYTHD;
9391
	    thd->lex->current_select->no_table_names_allowed= 0;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9392 9393
	    thd->where= "";
          }
9394 9395 9396
	;

order_or_limit:
9397 9398
	order_clause opt_limit_clause_init
	| limit_clause
9399
	;
9400 9401

union_option:
9402 9403 9404 9405
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
9406

9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429
subselect:
        SELECT_SYM subselect_start subselect_init subselect_end
        {
          $$= $3;
        }
        | '(' subselect_start subselect ')'
          {
	    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
9430

9431
subselect_init:
9432
  select_init2
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9433
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9434
    $$= Lex->current_select->master_unit()->first_select();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9435 9436
  };

9437
subselect_start:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9438
	{
9439
	  LEX *lex=Lex;
9440 9441
          if (lex->sql_command == (int)SQLCOM_HA_READ ||
              lex->sql_command == (int)SQLCOM_KILL)
9442
	  {
9443 9444
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
9445
	  }
9446 9447 9448 9449 9450 9451 9452
          /* 
            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
9453
	  if (mysql_new_select(Lex, 1))
9454
	    MYSQL_YYABORT;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9455
	};
9456 9457

subselect_end:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9458 9459
	{
	  LEX *lex=Lex;
9460
          lex->pop_context();
9461
          SELECT_LEX *child= lex->current_select;
9462
	  lex->current_select = lex->current_select->return_after_parsing();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
9463
          lex->nest_level--;
9464
          lex->current_select->n_child_sum_items += child->n_sum_items;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
9465 9466 9467 9468 9469 9470
          /*
            A subselect can add fields to an outer select. Reserve space for
            them.
          */
          lex->current_select->select_n_where_fields+=
            child->select_n_where_fields;
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
9471
	};
serg@serg.mylan's avatar
serg@serg.mylan committed
9472

9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500
/**************************************************************************

 CREATE VIEW | TRIGGER | PROCEDURE statements.

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

view_or_trigger_or_sp:
	definer view_or_trigger_or_sp_tail
	{}
	| view_replace_or_algorithm definer view_tail
	{}
	;

view_or_trigger_or_sp_tail:
	view_tail
	{}
	| trigger_tail
	{}
	| sp_tail
	{}
	;

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

 DEFINER clause support.

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

9501
definer:
9502
	/* empty */
9503
	{
9504 9505 9506 9507 9508 9509 9510 9511
          /*
            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;
9512
	}
9513
	| DEFINER_SYM EQ user
9514
	{
9515
	  YYTHD->lex->definer= get_current_user(YYTHD, $3);
9516 9517 9518 9519 9520
	}
	;

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

9521
 CREATE VIEW statement parts.
9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556

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

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 */
9557
	{ Lex->create_view_suid= VIEW_SUID_DEFAULT; }
9558
	| SQL_SYM SECURITY_SYM DEFINER_SYM
9559
	{ Lex->create_view_suid= VIEW_SUID_DEFINER; }
9560
	| SQL_SYM SECURITY_SYM INVOKER_SYM
9561
	{ Lex->create_view_suid= VIEW_SUID_INVOKER; }
9562 9563 9564 9565 9566 9567 9568 9569 9570
	;

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 */
9571
	  if (!lex->select_lex.add_table_to_list(thd, $3, NULL, TL_OPTION_UPDATING))
9572
	    MYSQL_YYABORT;
9573 9574 9575 9576 9577 9578 9579 9580
	}
	view_list_opt AS view_select view_check_option
	{}
	;

view_list_opt:
	/* empty */
	{}
9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596
	| '(' 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)));
	  }
	;

9597
view_select:
9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615
        {
          LEX *lex= Lex;
          lex->parsing_options.allows_variable= FALSE;
          lex->parsing_options.allows_select_into= FALSE;
          lex->parsing_options.allows_select_procedure= FALSE;
          lex->parsing_options.allows_derived= FALSE;
        }        
        view_select_aux
        {
          LEX *lex= Lex;
          lex->parsing_options.allows_variable= TRUE;
          lex->parsing_options.allows_select_into= TRUE;
          lex->parsing_options.allows_select_procedure= TRUE;
          lex->parsing_options.allows_derived= TRUE;
        }
        ;

view_select_aux:
9616 9617
	SELECT_SYM remember_name select_init2
	{
9618 9619 9620 9621 9622 9623
          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;
9624 9625 9626
	}
	| '(' remember_name select_paren ')' union_opt
	{
9627 9628 9629 9630 9631 9632
          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;
9633
	}
9634 9635
	;

9636
view_check_option:
9637
	/* empty */
9638 9639 9640 9641 9642 9643 9644
	{ 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; }
9645
	;
9646

9647 9648 9649 9650 9651 9652 9653
/**************************************************************************

 CREATE TRIGGER statement parts.

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

trigger_tail:
9654
	TRIGGER_SYM remember_name sp_name trg_action_time trg_event
9655
	ON remember_name table_ident FOR_SYM remember_name EACH_SYM ROW_SYM
9656
	{
9657 9658 9659
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;
9660 9661 9662
	  sp_head *sp;
	 
	  if (lex->sphead)
9663
	  {
9664
	    my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "TRIGGER");
9665
	    MYSQL_YYABORT;
9666
	  }
9667 9668
	
	  if (!(sp= new sp_head()))
9669
	    MYSQL_YYABORT;
9670
	  sp->reset_thd_mem_root(thd);
9671
	  sp->init(lex);
9672
          sp->init_sp_name(thd, $3);
9673
	
9674
	  lex->stmt_definition_begin= $2;
9675
          lex->ident.str= $7;
9676
          lex->ident.length= $10 - $7;
9677

9678 9679 9680 9681 9682 9683 9684 9685
	  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 ';'.
	  */
9686 9687
	  sp->m_old_cmq= thd->client_capabilities & CLIENT_MULTI_QUERIES;
	  thd->client_capabilities &= ~CLIENT_MULTI_QUERIES;
9688 9689 9690
	  
	  bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	  lex->sphead->m_chistics= &lex->sp_chistics;
9691
	  lex->sphead->m_body_begin= lip->ptr;
9692 9693
          while (my_isspace(system_charset_info, lex->sphead->m_body_begin[0]))
            ++lex->sphead->m_body_begin;
9694 9695 9696 9697 9698 9699 9700
	}
	sp_proc_stmt
	{
	  LEX *lex= Lex;
	  sp_head *sp= lex->sphead;
	  
	  lex->sql_command= SQLCOM_CREATE_TRIGGER;
9701
	  sp->init_strings(YYTHD, lex);
9702 9703 9704 9705 9706 9707
	  /* Restore flag if it was cleared above */
	  if (sp->m_old_cmq)
	    YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
	  sp->restore_thd_mem_root(YYTHD);
	
	  if (sp->is_not_allowed_in_function("trigger"))
9708
	      MYSQL_YYABORT;
9709 9710 9711 9712 9713 9714
	
	  /*
	    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.
	  */
9715
	  if (!lex->select_lex.add_table_to_list(YYTHD, $8,
9716 9717
	                                         (LEX_STRING*) 0,
	                                         TL_OPTION_UPDATING,
9718
                                                 TL_IGNORE))
9719
	    MYSQL_YYABORT;
9720
	}
9721 9722
	;

9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746
/**************************************************************************

 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");
9747
	    MYSQL_YYABORT;
9748
	  }
9749

9750
	  lex->stmt_definition_begin= $2;
9751

9752 9753 9754 9755
	  /* Order is important here: new - reset - init */
	  sp= new sp_head();
	  sp->reset_thd_mem_root(YYTHD);
	  sp->init(lex);
9756
          sp->init_sp_name(YYTHD, $3);
9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769

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

9774
	  lex->sphead->m_param_begin= lip->tok_start+1;
9775 9776 9777 9778
	}
	sp_pdparam_list
	')'
	{
9779 9780 9781
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;
9782

9783
	  lex->sphead->m_param_end= lip->tok_start;
9784 9785 9786 9787
	  bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	}
	sp_c_chistics
	{
9788 9789 9790
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;
9791 9792

	  lex->sphead->m_chistics= &lex->sp_chistics;
9793
	  lex->sphead->m_body_begin= lip->tok_start;
9794 9795 9796 9797 9798 9799
	}
	sp_proc_stmt
	{
	  LEX *lex= Lex;
	  sp_head *sp= lex->sphead;

9800
	  sp->init_strings(YYTHD, lex);
9801 9802 9803 9804 9805 9806 9807 9808
	  lex->sql_command= SQLCOM_CREATE_PROCEDURE;
	  /* Restore flag if it was cleared above */
	  if (sp->m_old_cmq)
	    YYTHD->client_capabilities |= CLIENT_MULTI_QUERIES;
	  sp->restore_thd_mem_root(YYTHD);
	}
	;

9809
/*************************************************************************/
9810

9811 9812
xa: XA_SYM begin_or_start xid opt_join_or_resume
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
9813
        Lex->sql_command = SQLCOM_XA_START;
9814
      }
9815
    | XA_SYM END xid opt_suspend
9816
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
9817
        Lex->sql_command = SQLCOM_XA_END;
9818 9819 9820
      }
    | XA_SYM PREPARE_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
9821
        Lex->sql_command = SQLCOM_XA_PREPARE;
9822 9823 9824
      }
    | XA_SYM COMMIT_SYM xid opt_one_phase
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
9825
        Lex->sql_command = SQLCOM_XA_COMMIT;
9826 9827 9828
      }
    | XA_SYM ROLLBACK_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
9829
        Lex->sql_command = SQLCOM_XA_ROLLBACK;
9830 9831 9832
      }
    | XA_SYM RECOVER_SYM
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
9833
        Lex->sql_command = SQLCOM_XA_RECOVER;
9834 9835 9836
      }
    ;

serg@serg.mylan's avatar
serg@serg.mylan committed
9837 9838
xid: text_string
     {
9839
       MYSQL_YYABORT_UNLESS($1->length() <= MAXGTRIDSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
9840
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
9841
         MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
9842 9843 9844 9845
       Lex->xid->set(1L, $1->ptr(), $1->length(), 0, 0);
     }
     | text_string ',' text_string
     {
9846
       MYSQL_YYABORT_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
9847
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
9848
         MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
9849 9850 9851 9852
       Lex->xid->set(1L, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     | text_string ',' text_string ',' ulong_num
     {
9853
       MYSQL_YYABORT_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
9854
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
9855
         MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
9856 9857 9858
       Lex->xid->set($5, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     ;
9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874

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

9875
opt_suspend:
9876 9877
    /* nothing */           { Lex->xa_opt=XA_NONE;        }
    | SUSPEND_SYM           { Lex->xa_opt=XA_SUSPEND;     }
9878 9879 9880 9881 9882
      opt_migrate
    ;

opt_migrate:
    /* nothing */           { }
9883 9884 9885 9886
    | FOR_SYM MIGRATE_SYM   { Lex->xa_opt=XA_FOR_MIGRATE; }
    ;