sql_yacc.yy 260 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
  Currently there are 245 shift/reduce conflicts.
  We should not introduce new conflicts any more.
463
*/
464
%expect 245
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
%left	XOR
1016
%left	AND_SYM AND_AND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1017 1018 1019 1020 1021 1022
%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	'-' '+'
1023
%left	'*' '/' '%' DIV_SYM MOD_SYM
1024
%left   '^'
monty@mysql.com's avatar
monty@mysql.com committed
1025
%left	NEG '~'
1026
%right	NOT_SYM NOT2_SYM
1027
%right	BINARY COLLATE_SYM
1028
%left   INTERVAL_SYM
1029

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

%type <lex_str_ptr>
	opt_table_alias

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

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

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

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

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

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

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

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

%type <item_list>
1086
	expr_list udf_expr_list udf_expr_list2 when_list
1087
	ident_list ident_list_arg opt_expr_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1088

1089 1090 1091
%type <var_type>
        option_type opt_var_type opt_var_ident_type

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1092
%type <key_type>
1093
	key_type opt_unique_or_fulltext constraint_key_type
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1094

1095 1096 1097
%type <key_alg>
	key_alg opt_btree_or_rtree

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1098
%type <string_list>
1099
	key_usage_list using_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1100 1101 1102 1103 1104

%type <key_part>
	key_part

%type <table_list>
1105
	join_table_list  join_table
1106
        table_factor table_ref
1107
        select_derived derived_table_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1108

1109
%type <date_time_type> date_time_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1110 1111
%type <interval> interval

1112 1113
%type <interval_time_st> interval_time_st

1114
%type <db_type> storage_engines
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1115 1116 1117

%type <row_type> row_types

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

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

1122
%type <cast_type> cast_type
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1123

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

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

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

1130
%type <charset>
1131
	opt_collate
1132 1133
	charset_name
	charset_name_or_default
1134 1135
	old_or_new_charset_name
	old_or_new_charset_name_or_default
1136 1137
	collation_name
	collation_name_or_default
1138
	opt_load_data_charset
1139

1140 1141
%type <variable> internal_variable_name

1142
%type <select_lex> subselect subselect_init
1143
	get_select_lex
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1144

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

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

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


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

verb_clause:
1223 1224 1225 1226 1227 1228
	  statement
	| begin
	;

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

sergefp@mysql.com's avatar
sergefp@mysql.com committed
1278
deallocate:
1279
        deallocate_or_drop PREPARE_SYM ident
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1280 1281
        {
          THD *thd=YYTHD;
1282 1283
          LEX *lex= thd->lex;
          lex->sql_command= SQLCOM_DEALLOCATE_PREPARE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1284 1285 1286
          lex->prepared_stmt_name= $3;
        };

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


sergefp@mysql.com's avatar
sergefp@mysql.com committed
1293
prepare:
1294
        PREPARE_SYM ident FROM prepare_src
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1295 1296
        {
          THD *thd=YYTHD;
1297 1298
          LEX *lex= thd->lex;
          lex->sql_command= SQLCOM_PREPARE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1299 1300 1301
          lex->prepared_stmt_name= $2;
        };

1302 1303 1304 1305 1306 1307
prepare_src:
        TEXT_STRING_sys
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $1;
1308
          lex->prepared_stmt_code_is_varref= FALSE;
1309 1310 1311 1312 1313 1314
        }
        | '@' ident_or_text
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $2;
1315
          lex->prepared_stmt_code_is_varref= TRUE;
1316
        };
1317

sergefp@mysql.com's avatar
sergefp@mysql.com committed
1318 1319 1320 1321
execute:
        EXECUTE_SYM ident
        {
          THD *thd=YYTHD;
1322 1323
          LEX *lex= thd->lex;
          lex->sql_command= SQLCOM_EXECUTE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
          lex->prepared_stmt_name= $2;
        }
        execute_using
        {}
        ;

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
1336 1337
        execute_var_list ',' execute_var_ident
        | execute_var_ident
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1338 1339 1340 1341 1342 1343 1344
        ;

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))
1345
              MYSQL_YYABORT;
1346
        }
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1347 1348
        ;

1349 1350
/* help */

1351
help:
1352 1353 1354 1355 1356
       HELP_SYM
       {
         if (Lex->sphead)
         {
           my_error(ER_SP_BADSTATEMENT, MYF(0), "HELP");
1357
           MYSQL_YYABORT;
1358 1359 1360
         }
       }
       ident_or_text
1361
       {
1362 1363
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
1364
	  lex->help_arg= $3.str;
1365
       };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1366 1367 1368 1369

/* change master */

change:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1370
       CHANGE MASTER_SYM TO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1371 1372 1373
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
1374
	  bzero((char*) &lex->mi, sizeof(lex->mi));
1375 1376 1377 1378
        }
       master_defs
	{}
       ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1379 1380 1381

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

1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
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
1400
       MASTER_PORT_SYM EQ ulong_num
1401 1402 1403 1404
       {
	 Lex->mi.port = $3;
       }
       |
serg@serg.mylan's avatar
serg@serg.mylan committed
1405
       MASTER_CONNECT_RETRY_SYM EQ ulong_num
1406 1407 1408
       {
	 Lex->mi.connect_retry = $3;
       }
serg@serg.mylan's avatar
serg@serg.mylan committed
1409
       | MASTER_SSL_SYM EQ ulong_num
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
         {
           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
1463
       | RELAY_LOG_POS_SYM EQ ulong_num
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
         {
           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,
dlenev@mockturtle.local's avatar
dlenev@mockturtle.local committed
1481
						 TL_WRITE))
1482
	    MYSQL_YYABORT;
1483
          lex->alter_info.reset();
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
	  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;
	}
	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))
1499
	      MYSQL_YYABORT;
1500 1501
            lex->alter_info.reset();
            lex->alter_info.flags= ALTER_ADD_INDEX;
1502 1503 1504 1505 1506 1507
	    lex->col_list.empty();
	    lex->change=NullS;
	  }
	   '(' key_list ')'
	  {
	    LEX *lex=Lex;
1508
            Key *key= new Key($2, $4.str, $5, 0, lex->col_list);
1509

1510
            lex->alter_info.key_list.push_back(key);
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
	    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;
	  }
1525
	| CREATE
1526
	  {
1527 1528 1529
            Lex->create_view_mode= VIEW_CREATE_NEW;
            Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED;
            Lex->create_view_suid= TRUE;
1530
	  }
1531
	  view_or_trigger_or_sp
1532
	  {}
1533 1534 1535 1536
	| CREATE USER clear_privileges grant_list
	  {
	    Lex->sql_command = SQLCOM_CREATE_USER;
          }
1537 1538
	;

1539 1540 1541 1542 1543 1544 1545
clear_privileges:
        /* Nothing */
        {
          LEX *lex=Lex;
          lex->users_list.empty();
          lex->columns.empty();
          lex->grant= lex->grant_tot_col= 0;
1546
	  lex->all_privileges= 0;
1547 1548 1549 1550 1551 1552 1553
          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));
        }
        ;

1554
sp_name:
1555
	  ident '.' ident
1556
	  {
1557 1558 1559
            if (!$1.str || check_db_name($1.str))
            {
	      my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
1560
	      MYSQL_YYABORT;
1561
	    }
1562
	    if (check_routine_name($3))
1563 1564
            {
	      my_error(ER_SP_WRONG_NAME, MYF(0), $3.str);
1565
	      MYSQL_YYABORT;
1566
	    }
1567
	    $$= new sp_name($1, $3, true);
1568 1569
	    $$->init_qname(YYTHD);
	  }
1570
	| ident
1571
	  {
1572
            LEX *lex= Lex;
1573
            LEX_STRING db;
1574
	    if (check_routine_name($1))
1575 1576
            {
	      my_error(ER_SP_WRONG_NAME, MYF(0), $1.str);
1577
	      MYSQL_YYABORT;
1578
	    }
1579
            if (lex->copy_db_to(&db.str, &db.length))
1580
              MYSQL_YYABORT;
1581
	    $$= new sp_name(db, $1, false);
1582 1583
            if ($$)
	      $$->init_qname(YYTHD);
1584 1585 1586 1587 1588 1589 1590
	  }
	;

create_function_tail:
	  RETURNS_SYM udf_type UDF_SONAME_SYM TEXT_STRING_sys
	  {
	    LEX *lex=Lex;
1591 1592 1593 1594 1595 1596 1597 1598 1599
            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");
1600
              MYSQL_YYABORT;
1601
            }
1602 1603 1604 1605 1606 1607 1608
	    lex->sql_command = SQLCOM_CREATE_FUNCTION;
	    lex->udf.name = lex->spname->m_name;
	    lex->udf.returns=(Item_result) $2;
	    lex->udf.dl=$4.str;
	  }
	| '('
	  {
1609 1610 1611
            THD *thd= YYTHD;
	    LEX *lex= thd->lex;
            Lex_input_stream *lip= thd->m_lip;
1612 1613
	    sp_head *sp;

1614 1615 1616 1617 1618 1619 1620
            /* 
              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));
1621
              MYSQL_YYABORT;
1622 1623
            }

1624 1625
	    if (lex->sphead)
	    {
1626
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1627
	      MYSQL_YYABORT;
1628 1629 1630
	    }
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
1631
	    sp->reset_thd_mem_root(thd);
1632
	    sp->init(lex);
1633
            sp->init_sp_name(thd, lex->spname);
1634 1635 1636 1637 1638 1639 1640 1641

	    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 ';'.
	     */
1642 1643 1644
	    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;
1645 1646 1647
	  }
          sp_fdparam_list ')'
	  {
1648 1649 1650
            THD *thd= YYTHD;
	    LEX *lex= thd->lex;
            Lex_input_stream *lip= thd->m_lip;
1651

1652
	    lex->sphead->m_param_end= lip->tok_start;
1653 1654 1655 1656
	  }
	  RETURNS_SYM
	  {
	    LEX *lex= Lex;
1657 1658 1659 1660
	    lex->charset= NULL;
	    lex->length= lex->dec= NULL;
	    lex->interval_list.empty();
	    lex->type= 0;
1661 1662 1663 1664 1665
	  }
	  type
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
1666

1667 1668 1669
            if (sp->fill_field_definition(YYTHD, lex,
                                          (enum enum_field_types) $8,
                                          &sp->m_return_field_def))
1670
              MYSQL_YYABORT;
1671 1672 1673 1674 1675

	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	  }
	  sp_c_chistics
	  {
1676 1677 1678
            THD *thd= YYTHD;
	    LEX *lex= thd->lex;
            Lex_input_stream *lip= thd->m_lip;
1679 1680

	    lex->sphead->m_chistics= &lex->sp_chistics;
1681
	    lex->sphead->m_body_begin= lip->tok_start;
1682 1683 1684 1685 1686 1687
	  }
	  sp_proc_stmt
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;

1688
            if (sp->is_not_allowed_in_function("function"))
1689
              MYSQL_YYABORT;
1690

1691
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
1692
	    sp->init_strings(YYTHD, lex);
1693 1694 1695
            if (!(sp->m_flags & sp_head::HAS_RETURN))
            {
              my_error(ER_SP_NORETURN, MYF(0), sp->m_qname.str);
1696
              MYSQL_YYABORT;
1697
            }
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
	    /* 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; }
1737
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
	;

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();
1759
	    sp_add_used_routine(lex, YYTHD, $2, TYPE_ENUM_PROCEDURE);
1760
	  }
1761
          opt_sp_cparam_list {}
1762 1763 1764
	;

/* CALL parameters */
1765
opt_sp_cparam_list:
1766
	  /* Empty */
1767
	| '(' opt_sp_cparams ')'
1768 1769
	;

1770 1771 1772 1773 1774
opt_sp_cparams:
          /* Empty */
        | sp_cparams
        ;

1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
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
	;

1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
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;
	  }
	;

1817
sp_fdparam:
1818
	  ident sp_init_param type
1819 1820 1821 1822
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

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

            if (lex->sphead->fill_field_definition(YYTHD, lex,
                                                   (enum enum_field_types) $3,
1834
                                                   &spvar->field_def))
1835
            {
1836
              MYSQL_YYABORT;
1837
            }
1838 1839
            spvar->field_def.field_name= spvar->name.str;
            spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
1855
	  sp_opt_inout sp_init_param ident type
1856 1857 1858 1859
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1860
	    if (spc->find_variable(&$3, TRUE))
1861
	    {
1862
	      my_error(ER_SP_DUP_PARAM, MYF(0), $3.str);
1863
	      MYSQL_YYABORT;
1864
	    }
1865 1866 1867
            sp_variable_t *spvar= spc->push_variable(&$3,
                                                     (enum enum_field_types)$4,
                                                     (sp_param_mode_t)$1);
1868 1869 1870

            if (lex->sphead->fill_field_definition(YYTHD, lex,
                                                   (enum enum_field_types) $4,
1871
                                                   &spvar->field_def))
1872
            {
1873
              MYSQL_YYABORT;
1874
            }
1875 1876
            spvar->field_def.field_name= spvar->name.str;
            spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
	  }
	;

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 */ {}
1889
	| sp_proc_stmts  sp_proc_stmt ';'
1890 1891 1892 1893
	;

sp_proc_stmts1:
	  sp_proc_stmt ';' {}
1894
	| sp_proc_stmts1  sp_proc_stmt ';'
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
	;

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
1910 1911
	      my_message(ER_SP_VARCOND_AFTER_CURSHNDLR,
                         ER(ER_SP_VARCOND_AFTER_CURSHNDLR), MYF(0));
1912
	      MYSQL_YYABORT;
1913 1914 1915
	    }
	    if ($2.curs && $1.hndlrs)
	    { /* Cursor following handler */
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1916 1917
	      my_message(ER_SP_CURSOR_AFTER_HANDLER,
                         ER(ER_SP_CURSOR_AFTER_HANDLER), MYF(0));
1918
	      MYSQL_YYABORT;
1919 1920 1921 1922 1923 1924 1925 1926 1927
	    }
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
	    $$.curs= $1.curs + $2.curs;
	  }
	;

sp_decl:
1928
          DECLARE_SYM sp_decl_idents
1929
          {
1930
            LEX *lex= Lex;
1931

1932 1933 1934
            lex->sphead->reset_lex(YYTHD);
            lex->spcont->declare_var_boundary($2);
          }
1935
          type
1936
          sp_opt_default
1937 1938
          {
            LEX *lex= Lex;
1939
            sp_pcontext *pctx= lex->spcont;
1940
            uint num_vars= pctx->context_var_count();
1941 1942 1943 1944
            enum enum_field_types var_type= (enum enum_field_types) $4;
            Item *dflt_value_item= $5;
            
            if (!dflt_value_item)
1945
            {
1946 1947 1948 1949 1950 1951
              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++)
            {
1952 1953
              uint var_idx= pctx->var_context2runtime(i);
              sp_variable_t *spvar= pctx->find_variable(var_idx);
1954
            
1955
              if (!spvar)
1956
                MYSQL_YYABORT;
1957
            
1958 1959
              spvar->type= var_type;
              spvar->dflt= dflt_value_item;
1960 1961
            
              if (lex->sphead->fill_field_definition(YYTHD, lex, var_type,
1962
                                                     &spvar->field_def))
1963
              {
1964
                MYSQL_YYABORT;
1965 1966
              }
            
1967 1968
              spvar->field_def.field_name= spvar->name.str;
              spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1969 1970 1971 1972 1973 1974 1975
            
              /* 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)));
1976
            }
1977 1978

            pctx->declare_var_boundary(0);
1979
            lex->sphead->restore_lex(YYTHD);
1980

1981 1982 1983
            $$.vars= $2;
            $$.conds= $$.hndlrs= $$.curs= 0;
          }
1984 1985 1986 1987 1988 1989 1990
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
1991
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
1992
	      MYSQL_YYABORT;
1993 1994 1995 1996 1997 1998 1999 2000 2001
	    }
	    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;
2002 2003 2004

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

2005 2006 2007
	    sp_pcontext *ctx= lex->spcont;
	    sp_instr_hpush_jump *i=
              new sp_instr_hpush_jump(sp->instructions(), ctx, $2,
2008
	                              ctx->current_var_count());
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023

	    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,
2024
	                              ctx->current_var_count());
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
	      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);
2035 2036 2037

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

2038 2039
	    $$.vars= $$.conds= $$.curs= 0;
	    $$.hndlrs= $6;
2040
	    lex->spcont->add_handlers($6);
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
	  }
	| 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))
	    {
2052
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
2053
	      delete $5;
2054
	      MYSQL_YYABORT;
2055
	    }
pem@mysql.com's avatar
pem@mysql.com committed
2056
            i= new sp_instr_cpush(sp->instructions(), ctx, $5,
2057
                                  ctx->current_cursor_count());
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
	    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
2080 2081
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
2082
	      MYSQL_YYABORT;
2083 2084 2085
	    }
	    if (lex->result)
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2086 2087
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
2088
	      MYSQL_YYABORT;
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
	    }
	    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:
2103 2104 2105 2106 2107 2108 2109
          sp_hcond_element
          { $$= 1; }
        | sp_hcond_list ',' sp_hcond_element
          { $$+= 1; }
        ;

sp_hcond_element:
2110 2111 2112 2113
	  sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
2114
	    sp_pcontext *ctx= lex->spcont->parent_context();
2115 2116 2117 2118

	    if (ctx->find_handler($1))
	    {
	      my_message(ER_SP_DUP_HANDLER, ER(ER_SP_DUP_HANDLER), MYF(0));
2119
	      MYSQL_YYABORT;
2120 2121 2122 2123 2124
	    }
	    else
	    {
	      sp_instr_hpush_jump *i=
                (sp_instr_hpush_jump *)sp->last_instruction();
2125

2126 2127 2128
	      i->add_condition($1);
	      ctx->push_handler($1);
	    }
2129 2130 2131 2132
	  }
	;

sp_cond:
serg@serg.mylan's avatar
serg@serg.mylan committed
2133
	  ulong_num
2134 2135 2136 2137 2138 2139 2140
	  {			/* 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 */
2141 2142 2143
	    if (!sp_cond_check(&$3))
	    {
	      my_error(ER_SP_BAD_SQLSTATE, MYF(0), $3.str);
2144
	      MYSQL_YYABORT;
2145
	    }
2146 2147
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
2148 2149
	    memcpy($$->sqlstate, $3.str, 5);
	    $$->sqlstate[5]= '\0';
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
	  }
	;

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
2168
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
2169
	      MYSQL_YYABORT;
2170 2171 2172 2173 2174 2175 2176
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
2177
	| not FOUND_SYM		/* SQLSTATEs 02??? */
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
	  {
	    $$= (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
	  {
2192 2193
            /* NOTE: field definition is filled in sp_decl section. */

2194 2195 2196
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

2197
	    if (spc->find_variable(&$1, TRUE))
2198
	    {
2199
	      my_error(ER_SP_DUP_VAR, MYF(0), $1.str);
2200
	      MYSQL_YYABORT;
2201
	    }
2202
	    spc->push_variable(&$1, (enum_field_types)0, sp_param_in);
2203 2204 2205 2206
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
2207 2208
            /* NOTE: field definition is filled in sp_decl section. */

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

2212
	    if (spc->find_variable(&$3, TRUE))
2213
	    {
2214
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
2215
	      MYSQL_YYABORT;
2216
	    }
2217
	    spc->push_variable(&$3, (enum_field_types)0, sp_param_in);
2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
	    $$= $1 + 1;
	  }
	;

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

sp_proc_stmt:
	  {
2229 2230 2231
            THD *thd= YYTHD;
	    LEX *lex= thd->lex;
            Lex_input_stream *lip= thd->m_lip;
2232

2233 2234
	    lex->sphead->reset_lex(thd);
	    lex->sphead->m_tmp_query= lip->tok_start;
2235 2236 2237
	  }
	  statement
	  {
2238 2239 2240
            THD *thd= YYTHD;
	    LEX *lex= thd->lex;
            Lex_input_stream *lip= thd->m_lip;
2241 2242
	    sp_head *sp= lex->sphead;

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

2261 2262 2263 2264 2265 2266
              /*
                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)
2267
                i->m_query.length= lip->ptr - sp->m_tmp_query;
2268
              else
2269 2270 2271
                i->m_query.length= lip->tok_end - sp->m_tmp_query;
              i->m_query.str= strmake_root(thd->mem_root,
                                           sp->m_tmp_query,
2272 2273
                                           i->m_query.length);
              sp->add_instr(i);
2274
            }
2275
	    sp->restore_lex(thd);
2276
          }
2277 2278 2279
          | RETURN_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr
2280 2281
	  {
	    LEX *lex= Lex;
2282
	    sp_head *sp= lex->sphead;
2283

2284
	    if (sp->m_type != TYPE_ENUM_FUNCTION)
2285
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2286
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
2287
	      MYSQL_YYABORT;
2288 2289 2290 2291 2292
	    }
	    else
	    {
	      sp_instr_freturn *i;

2293 2294
	      i= new sp_instr_freturn(sp->instructions(), lex->spcont, $3,
                                      sp->m_return_field_def.sql_type, lex);
2295
	      sp->add_instr(i);
2296
	      sp->m_flags|= sp_head::HAS_RETURN;
2297
	    }
2298
	    sp->restore_lex(YYTHD);
2299
	  }
2300 2301 2302 2303
        | IF
          { Lex->sphead->new_cont_backpatch(NULL); }
          sp_if END IF
          { Lex->sphead->do_cont_backpatch(); }
2304
        | case_stmt_specification
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
	| 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());
	  }
2318
	| LEAVE_SYM label_ident
2319 2320 2321 2322 2323 2324 2325 2326
	  {
	    LEX *lex= Lex;
	    sp_head *sp = lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab)
	    {
2327
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
2328
	      MYSQL_YYABORT;
2329 2330 2331 2332
	    }
	    else
	    {
	      sp_instr_jump *i;
2333 2334 2335
	      uint ip= sp->instructions();
	      uint n;

2336
	      n= ctx->diff_handlers(lab->ctx, TRUE);  /* Exclusive the dest. */
2337 2338
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
2339
	      n= ctx->diff_cursors(lab->ctx, TRUE);  /* Exclusive the dest. */
2340 2341
	      if (n)
	        sp->add_instr(new sp_instr_cpop(ip++, ctx, n));
2342 2343 2344 2345 2346
	      i= new sp_instr_jump(ip, ctx);
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
	    }
	  }
2347
	| ITERATE_SYM label_ident
2348 2349 2350 2351 2352 2353 2354 2355
	  {
	    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)
	    {
2356
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "ITERATE", $2.str);
2357
	      MYSQL_YYABORT;
2358 2359 2360 2361 2362 2363 2364
	    }
	    else
	    {
	      sp_instr_jump *i;
	      uint ip= sp->instructions();
	      uint n;

2365
	      n= ctx->diff_handlers(lab->ctx, FALSE);  /* Inclusive the dest. */
2366 2367
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
2368
	      n= ctx->diff_cursors(lab->ctx, FALSE);  /* Inclusive the dest. */
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383
	      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))
	    {
2384
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2385
	      MYSQL_YYABORT;
2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
	    }
	    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))
	    {
2399
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $3.str);
2400
	      MYSQL_YYABORT;
2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415
	    }
	    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))
	    {
2416
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2417
	      MYSQL_YYABORT;
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435
	    }
	    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;
2436
	    sp_variable_t *spv;
2437

2438
	    if (!spc || !(spv = spc->find_variable(&$1)))
2439
	    {
2440
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2441
	      MYSQL_YYABORT;
2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
	    }
	    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;
2457
	    sp_variable_t *spv;
2458

2459
	    if (!spc || !(spv = spc->find_variable(&$3)))
2460
	    {
2461
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $3.str);
2462
	      MYSQL_YYABORT;
2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	    }
	  }
	;

sp_if:
2475 2476
          { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2477 2478 2479 2480 2481
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= sp->instructions();
2482 2483
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, ctx,
                                                               $2, lex);
2484 2485

	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
2486
            sp->add_cont_backpatch(i);
2487
            sp->add_instr(i);
2488
            sp->restore_lex(YYTHD);
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
	  }
	  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
	;

2515 2516 2517 2518
case_stmt_specification:
          simple_case_stmt
        | searched_case_stmt
        ;
2519

2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
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))
2531
              MYSQL_YYABORT;
2532

2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
            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);
          }
        ;
2544

2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559
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);
          }
        ;
2560

2561 2562 2563 2564
simple_when_clause_list:
          simple_when_clause
        | simple_when_clause_list simple_when_clause
        ;
2565

2566 2567 2568 2569
searched_when_clause_list:
          searched_when_clause
        | searched_when_clause_list searched_when_clause
        ;
2570

2571 2572 2573 2574 2575 2576 2577 2578
simple_when_clause:
          WHEN_SYM
          {
            Lex->sphead->reset_lex(YYTHD); /* For expr $3 */
          }
          expr
          {
            /* Simple case: <caseval> = <whenval> */
2579

2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
            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
2591

2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
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
2610

2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622
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
2623

2624
sp_labeled_control:
2625
	  label_ident ':'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2626
	  {
2627 2628 2629 2630 2631 2632
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);

	    if (lab)
	    {
2633
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $1.str);
2634
	      MYSQL_YYABORT;
2635 2636 2637 2638 2639 2640 2641
	    }
	    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
2642
	  }
2643
	  sp_unlabeled_control sp_opt_label
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2644
	  {
2645
	    LEX *lex= Lex;
2646

2647 2648 2649 2650 2651 2652 2653
	    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)
	      {
2654
	        my_error(ER_SP_LABEL_MISMATCH, MYF(0), $5.str);
2655
	        MYSQL_YYABORT;
2656 2657 2658
	      }
	    }
	    lex->sphead->backpatch(lex->spcont->pop_label());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2659
	  }
2660 2661 2662
	;

sp_opt_label:
2663
        /* Empty  */    { $$= null_lex_str; }
2664
        | label_ident   { $$= $1; }
2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675
	;

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;
2676
	    lex->spcont= lex->spcont->push_context(LABEL_DEFAULT_SCOPE);
2677 2678 2679 2680
	  }
	  sp_decls
	  sp_proc_stmts
	  END
2681
	  {
2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
	    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
2697
	  {
2698 2699 2700 2701 2702 2703
	    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
2704
	  }
2705 2706 2707
        | WHILE_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr DO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2708
	  {
2709 2710 2711 2712
	    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,
2713
							       $3, lex);
2714 2715 2716

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

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; }
2762
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2763 2764

create2:
serg@serg.mylan's avatar
serg@serg.mylan committed
2765 2766 2767 2768
        '(' create2a {}
        | opt_create_table_options create3 {}
        | LIKE table_ident
          {
2769 2770
            Lex->create_info.options|= HA_LEX_CREATE_TABLE_LIKE;
            if (!Lex->select_lex.add_table_to_list(YYTHD, $2, NULL, 0, TL_READ))
2771
              MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
2772 2773 2774
          }
        | '(' LIKE table_ident ')'
          {
2775 2776
            Lex->create_info.options|= HA_LEX_CREATE_TABLE_LIKE;
            if (!Lex->select_lex.add_table_to_list(YYTHD, $3, NULL, 0, TL_READ))
2777
              MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
2778
          }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2779
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2780

serg@serg.mylan's avatar
serg@serg.mylan committed
2781 2782
create2a:
        field_list ')' opt_create_table_options create3 {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2783
	|  create_select ')' { Select->set_braces(1);} union_opt {}
2784
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2785 2786

create3:
2787
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
2788
	| opt_duplicate opt_as     create_select
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2789
          { Select->set_braces(0);} union_clause {}
serg@serg.mylan's avatar
serg@serg.mylan committed
2790
	| opt_duplicate opt_as '(' create_select ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2791
          { Select->set_braces(1);} union_opt {}
2792 2793
        ;

serg@serg.mylan's avatar
serg@serg.mylan committed
2794
create_select:
2795
          SELECT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2796
          {
2797
	    LEX *lex=Lex;
2798
	    lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
2799 2800 2801 2802
	    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;
2803 2804 2805 2806
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
2807
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
2808
	    mysql_init_select(lex);
2809
	    lex->current_select->parsing_place= SELECT_LIST;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2810
          }
2811 2812
          select_options select_item_list
	  {
2813
	    Select->parsing_place= NO_MATTER;
2814
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2815
	  opt_select_from
2816 2817 2818 2819 2820 2821 2822
	  {
	    /*
              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
2823
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2824

2825 2826
opt_as:
	/* empty */ {}
2827
	| AS	    {};
2828

2829 2830 2831 2832 2833 2834 2835 2836 2837
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
2838 2839
	default_collation   {}
	| default_charset   {};
2840

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2841
opt_table_options:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2842
	/* empty */	 { $$= 0; }
2843
	| table_options  { $$= $1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2844 2845 2846

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2849
table_option:
2850
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2851 2852

opt_if_not_exists:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2853
	/* empty */	 { $$= 0; }
2854
	| IF not EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2855 2856 2857

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

2860 2861 2862 2863
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
2864 2865
create_table_options:
	create_table_option
2866
	| create_table_option     create_table_options
2867
	| create_table_option ',' create_table_options;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2868 2869

create_table_option:
2870 2871
	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; }
2872 2873
	| 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
2874
	| 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;}
2875
	| PASSWORD opt_equal TEXT_STRING_sys	{ Lex->create_info.password=$3.str; Lex->create_info.used_fields|= HA_CREATE_USED_PASSWORD; }
2876
	| COMMENT_SYM opt_equal TEXT_STRING_sys	{ Lex->create_info.comment=$3; Lex->create_info.used_fields|= HA_CREATE_USED_COMMENT; }
2877
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
2878 2879 2880 2881 2882 2883 2884 2885 2886 2887
        | 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:
2888 2889
                my_parse_error(ER(ER_SYNTAX_ERROR));
                MYSQL_YYABORT;
2890 2891 2892 2893 2894 2895 2896 2897 2898
            }
            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
2899 2900
	| 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; }
2901
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
2902 2903 2904
	| RAID_TYPE opt_equal raid_types
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_TYPE", "PARTITION");
2905
	    MYSQL_YYABORT;
2906 2907 2908 2909
	  }
	| RAID_CHUNKS opt_equal ulong_num
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_CHUNKS", "PARTITION");
2910
	    MYSQL_YYABORT;
2911 2912 2913 2914
	  }
	| RAID_CHUNKSIZE opt_equal ulong_num
	  {
	    my_error(ER_WARN_DEPRECATED_SYNTAX, MYF(0), "RAID_CHUNKSIZE", "PARTITION");
2915
	    MYSQL_YYABORT;
2916
	  }
2917
	| UNION_SYM opt_equal '(' table_list ')'
2918 2919 2920
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
2921 2922
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
2923
	    lex->create_info.merge_list.elements--;
2924 2925
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
2926
	    lex->select_lex.table_list.elements=1;
2927 2928 2929
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
2930
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
2931
	  }
2932 2933
	| default_charset
	| default_collation
2934
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
2935 2936
	| 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; }
2937
	| 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; }
2938
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2939

2940 2941 2942 2943 2944 2945 2946 2947
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))
          {
2948 2949 2950
            my_error(ER_CONFLICTING_DECLARATIONS, MYF(0),
                     "CHARACTER SET ", cinfo->default_table_charset->csname,
                     "CHARACTER SET ", $4->csname);
2951
            MYSQL_YYABORT;
2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
          }
	  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))
            {
2965 2966
              my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $4->name, cinfo->default_table_charset->csname);
2967
              MYSQL_YYABORT;
2968 2969 2970 2971 2972
            }
            Lex->create_info.default_table_charset= $4;
            Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

2973
storage_engines:
2974 2975 2976 2977
	ident_or_text
	{
	  $$ = ha_resolve_by_name($1.str,$1.length);
	  if ($$ == DB_TYPE_UNKNOWN) {
2978
	    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
2979
	    MYSQL_YYABORT;
2980 2981
	  }
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2982 2983 2984 2985 2986

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
2987 2988 2989
	| 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
2990 2991 2992 2993

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

2996 2997 2998
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
2999
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
3000

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3001
opt_select_from:
3002
	opt_limit_clause {}
3003
	| select_from select_lock_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3004 3005

udf_func_type:
3006
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
3007
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3008 3009 3010 3011

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
3012
        | DECIMAL_SYM {$$ = (int) DECIMAL_RESULT; }
3013
	| INT_SYM {$$ = (int) INT_RESULT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3014 3015 3016

field_list:
	  field_list_item
3017
	| field_list ',' field_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3018 3019 3020


field_list_item:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3021
	   column_def
3022 3023 3024 3025
         | key_def
         ;

column_def:
3026
	  field_spec opt_check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3027 3028 3029 3030
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3031
	;
3032 3033

key_def:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3034
	key_type opt_ident key_alg '(' key_list ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3035
	  {
3036
	    LEX *lex=Lex;
3037 3038 3039
            Key *key= new Key($1, $2, $3, 0, lex->col_list);
            lex->alter_info.key_list.push_back(key);

3040
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3041
	  }
3042 3043 3044 3045
	| opt_constraint constraint_key_type opt_ident key_alg '(' key_list ')'
	  {
	    LEX *lex=Lex;
	    const char *key_name= $3 ? $3:$1;
3046 3047
            Key *key= new Key($2, key_name, $4, 0, lex->col_list);
            lex->alter_info.key_list.push_back(key);
3048 3049
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
3050
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3051
	  {
3052
	    LEX *lex=Lex;
3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064
            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);
3065
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3066
	  }
3067 3068 3069 3070
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
3071
	| opt_constraint check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3072 3073
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
3074 3075 3076
	  }
	;

3077
opt_check_constraint:
3078
	/* empty */
3079 3080 3081 3082 3083
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
3084
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3085 3086

opt_constraint:
3087
	/* empty */		{ $$=(char*) 0; }
monty@mysql.com's avatar
monty@mysql.com committed
3088 3089 3090 3091 3092 3093
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3094 3095 3096 3097

field_spec:
	field_ident
	 {
3098
	   LEX *lex=Lex;
3099
	   lex->length=lex->dec=0; lex->type=0;
3100
	   lex->default_value= lex->on_update_value= 0;
3101
           lex->comment=null_lex_str;
3102
	   lex->charset=NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3103 3104 3105
	 }
	type opt_attribute
	{
3106
	  LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3107
	  if (add_field_to_list(lex->thd, $1.str,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3108
				(enum enum_field_types) $3,
3109
				lex->length,lex->dec,lex->type,
3110
				lex->default_value, lex->on_update_value, 
3111
                                &lex->comment,
3112
				lex->change,&lex->interval_list,lex->charset,
3113
				lex->uint_geom_type))
3114
	    MYSQL_YYABORT;
3115
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3116 3117

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

hf@deer.(none)'s avatar
hf@deer.(none) committed
3216 3217 3218
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
3219 3220 3221
	| POINT_SYM           { Lex->length= (char*)"21";
                                $$= Field::GEOM_POINT;
                              }
hf@deer.(none)'s avatar
hf@deer.(none) committed
3222 3223 3224 3225 3226 3227 3228
	| 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
3229 3230
char:
	CHAR_SYM {}
3231 3232 3233 3234 3235 3236
	;

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3238 3239 3240
varchar:
	char VARYING {}
	| VARCHAR {}
3241 3242 3243 3244
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3245
	| NVARCHAR_SYM {}
3246 3247 3248 3249
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3250 3251 3252 3253 3254 3255

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

real_type:
3259
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3260 3261
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
3262
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3263 3264 3265


float_options:
3266 3267
        /* empty */		{ Lex->dec=Lex->length= (char*)0; }
        | '(' NUM ')'		{ Lex->length=$2.str; Lex->dec= (char*)0; }
3268
	| precision		{};
3269 3270 3271 3272 3273 3274

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

field_options:
	/* empty */		{}
3279
	| field_opt_list	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3280 3281 3282

field_opt_list:
	field_opt_list field_option {}
3283
	| field_option {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3284 3285

field_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3286
	SIGNED_SYM	{}
3287
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
3288
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3289 3290

opt_len:
3291 3292
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3293 3294 3295

opt_precision:
	/* empty */	{}
3296
	| precision	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3297 3298 3299

opt_attribute:
	/* empty */ {}
3300
	| opt_attribute_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3301 3302 3303

opt_attribute_list:
	opt_attribute_list attribute {}
3304
	| attribute;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3305 3306 3307

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
3308
	| not NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
3309 3310 3311
	| 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
3312
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
3313
	| SERIAL_SYM DEFAULT VALUE_SYM
3314 3315 3316
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
3317
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3318 3319 3320 3321 3322
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
3323
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3324 3325 3326 3327 3328
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
3329
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3330 3331 3332 3333 3334
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
3335
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
3336
	  }
3337
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= $2; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3338 3339
	| COLLATE_SYM collation_name
	  {
3340
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3341
	    {
3342 3343
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $2->name,Lex->charset->csname);
3344
	      MYSQL_YYABORT;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3345 3346 3347 3348 3349 3350 3351
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3352

3353 3354 3355 3356 3357
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

3358 3359 3360 3361
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
3362

3363
charset_name:
3364
	ident_or_text
3365
	{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3366
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
3367
	  {
3368
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
3369
	    MYSQL_YYABORT;
3370
	  }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3371 3372 3373
	}
	| BINARY { $$= &my_charset_bin; }
	;
3374

3375 3376 3377
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3378

3379 3380 3381 3382
opt_load_data_charset:
	/* Empty */ { $$= NULL; }
	| charset charset_name_or_default { $$= $2; }
	;
3383 3384 3385 3386 3387 3388 3389

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)))
	  {
3390
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
3391
	    MYSQL_YYABORT;
3392 3393 3394 3395 3396 3397 3398 3399 3400
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

3401
collation_name:
3402
	ident_or_text
3403 3404 3405
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
3406
	    my_error(ER_UNKNOWN_COLLATION, MYF(0), $1.str);
3407
	    MYSQL_YYABORT;
3408 3409 3410
	  }
	};

3411 3412
opt_collate:
	/* empty */	{ $$=NULL; }
3413
	| COLLATE_SYM collation_name_or_default { $$=$2; }
3414 3415
	;

3416 3417 3418 3419
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

3420 3421 3422 3423
opt_default:
	/* empty */	{}
	| DEFAULT	{};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3424
opt_binary:
3425
	/* empty */			{ Lex->charset=NULL; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3426
	| ASCII_SYM opt_bin_mod		{ Lex->charset=&my_charset_latin1; }
3427
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3428 3429 3430 3431 3432 3433
	| 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");
3434
	    MYSQL_YYABORT;
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3435 3436 3437 3438 3439 3440 3441 3442 3443 3444
	  }
	}
	| 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:
3445
        /* empty */ { Lex->charset= NULL; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
3446
	| ASCII_SYM	{ Lex->charset=&my_charset_latin1; }
3447 3448
	| UNICODE_SYM
	{
3449 3450
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
3451
	  {
3452
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
3453
	    MYSQL_YYABORT;
3454 3455
	  }
	}
3456
	| charset charset_name	{ Lex->charset=$2; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3457

3458 3459 3460
opt_primary:
	/* empty */
	| PRIMARY_SYM
3461
	;
3462

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3463
references:
3464 3465
	REFERENCES table_ident
	{
3466
	  LEX *lex=Lex;
3467 3468 3469 3470 3471 3472
	  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
3473
	};
3474

3475
opt_ref_list:
3476
	/* empty */ opt_on_delete {}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3477
	| '(' ref_list ')' opt_on_delete {};
3478 3479 3480

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3483 3484 3485

opt_on_delete:
	/* empty */ {}
3486
	| opt_on_delete_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3487 3488 3489

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
3490
	| opt_on_delete_item {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3491 3492

opt_on_delete_item:
3493 3494 3495 3496
	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
3497
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3498 3499

delete_option:
3500 3501 3502 3503
	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
3504
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3505 3506

key_type:
3507
	key_or_index			    { $$= Key::MULTIPLE; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
3508 3509
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
hf@deer.(none)'s avatar
hf@deer.(none) committed
3510 3511 3512 3513
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
3514 3515
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
3516
	    MYSQL_YYABORT;
hf@deer.(none)'s avatar
hf@deer.(none) committed
3517 3518
#endif
	  };
3519 3520 3521

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
3522
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3523 3524 3525

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

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
3528 3529 3530 3531 3532
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3533 3534
keys_or_index:
	KEYS {}
3535
	| INDEX_SYM {}
3536
	| INDEXES {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3537

3538
opt_unique_or_fulltext:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3539 3540
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
serg@serg.mylan's avatar
serg@serg.mylan committed
3541
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
hf@deer.(none)'s avatar
hf@deer.(none) committed
3542 3543 3544 3545 3546
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
serg@serg.mylan's avatar
serg@serg.mylan committed
3547
            my_error(ER_FEATURE_DISABLED, MYF(0),
3548
                     sym_group_geom.name, sym_group_geom.needed_define);
3549
	    MYSQL_YYABORT;
hf@deer.(none)'s avatar
hf@deer.(none) committed
3550 3551
#endif
	  }
serg@serg.mylan's avatar
serg@serg.mylan committed
3552
        ;
3553 3554

key_alg:
3555
	/* empty */		   { $$= HA_KEY_ALG_UNDEF; }
3556
	| USING opt_btree_or_rtree { $$= $2; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3557
	| TYPE_SYM opt_btree_or_rtree  { $$= $2; };
3558 3559 3560

opt_btree_or_rtree:
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
hf@deer.(none)'s avatar
hf@deer.(none) committed
3561 3562 3563 3564
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3565
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3566 3567 3568

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
3569
	| key_part order_dir		{ Lex->col_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3570 3571 3572

key_part:
	ident			{ $$=new key_part_spec($1.str); }
3573 3574 3575 3576 3577
	| ident '(' NUM ')'	
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
3578
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
3579 3580 3581
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3582 3583 3584

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

3587
opt_component:
3588 3589
        /* empty */      { $$= null_lex_str; }
        | '.' ident      { $$= $2; };
3590

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3591 3592
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
3593
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3594 3595 3596 3597 3598 3599 3600 3601

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
3602
	  THD *thd= YYTHD;
3603
	  LEX *lex= thd->lex;
3604 3605 3606
	  lex->sql_command= SQLCOM_ALTER_TABLE;
	  lex->name= 0;
	  lex->duplicates= DUP_ERROR; 
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3607 3608
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
3609
	    MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3610
	  lex->col_list.empty();
3611
          lex->select_lex.init_order();
3612 3613 3614
	  lex->select_lex.db=
            ((TABLE_LIST*) lex->select_lex.table_list.first)->db;
          lex->name=0;
3615
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3616
	  lex->create_info.db_type= DB_TYPE_DEFAULT;
3617
	  lex->create_info.default_table_charset= NULL;
3618
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
3619
          lex->alter_info.reset();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3620
	}
3621 3622
	alter_list
	{}
3623
	| ALTER DATABASE ident_or_empty
3624 3625 3626 3627
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
3628
          create_database_options
3629 3630 3631
	  {
	    LEX *lex=Lex;
	    lex->sql_command=SQLCOM_ALTER_DB;
3632
	    lex->name= $3;
3633
            if (lex->name == NULL && lex->copy_db_to(&lex->name, NULL))
3634
              MYSQL_YYABORT;
3635 3636 3637 3638 3639
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

3640 3641 3642
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
3643
	      MYSQL_YYABORT;
3644
	    }
3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656
	    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;
3657

3658 3659 3660
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
3661
	      MYSQL_YYABORT;
3662
	    }
3663 3664 3665 3666 3667 3668 3669 3670 3671
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    LEX *lex=Lex;

	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
	    lex->spname= $3;
	  }
3672 3673
        | ALTER view_algorithm_opt definer view_suid
          VIEW_SYM table_ident
3674 3675 3676
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
3677 3678 3679 3680 3681
	    if (lex->sphead)
            {
              my_error(ER_SP_BADSTATEMENT, MYF(0), "ALTER VIEW");
              MYSQL_YYABORT;
            }
3682 3683 3684
	    lex->sql_command= SQLCOM_CREATE_VIEW;
	    lex->create_view_mode= VIEW_ALTER;
	    /* first table in list is target VIEW name */
3685
	    lex->select_lex.add_table_to_list(thd, $6, NULL, TL_OPTION_UPDATING);
3686
	  }
3687
	  view_list_opt AS view_select view_check_option
3688 3689
	  {}
	;
3690

3691 3692 3693 3694
ident_or_empty:
	/* empty */  { $$= 0; }
	| ident      { $$= $1.str; };

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3695
alter_list:
3696 3697
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
3698
        | alter_list_item
3699
	| alter_list ',' alter_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3700 3701

add_column:
3702
	ADD opt_column
3703 3704
	{
	  LEX *lex=Lex;
3705 3706
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
3707
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3708 3709

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

opt_column:
	/* empty */	{}
3854
	| COLUMN_SYM	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3855 3856

opt_ignore:
3857 3858 3859
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3860 3861

opt_restrict:
3862 3863 3864 3865
	/* 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
3866 3867 3868 3869

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
3870
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3871 3872 3873 3874

opt_to:
	/* empty */	{}
	| TO_SYM	{}
3875
	| EQ		{}
3876
	| AS		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3877

nick@mysql.com's avatar
nick@mysql.com committed
3878
/*
3879
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3880 3881
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3882
slave:
3883
	  START_SYM SLAVE slave_thread_opts
3884 3885 3886 3887 3888 3889
          {
	    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
3890
            /* If you change this code don't forget to update SLAVE START too */
3891 3892 3893
          }
          slave_until
          {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3894 3895 3896 3897 3898
        | 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
3899
            /* If you change this code don't forget to update SLAVE STOP too */
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3900
          }
3901 3902 3903 3904 3905
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
3906 3907 3908 3909 3910
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
3911 3912 3913 3914 3915 3916 3917 3918
	| 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
3919

3920
start:
3921 3922
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
3923 3924 3925
          LEX *lex= Lex;
          lex->sql_command= SQLCOM_BEGIN;
          lex->start_transaction_opt= $3;
3926
        }
3927 3928
	;

3929 3930 3931 3932 3933 3934
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
3935
        ;
3936

3937
slave_thread_opts:
3938 3939
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
3940
        {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
3941
	;
3942 3943

slave_thread_opt_list:
3944
	slave_thread_opt
3945 3946
	| slave_thread_opt_list ',' slave_thread_opt
	;
3947 3948

slave_thread_opt:
3949
	/*empty*/	{}
3950
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
3951
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
3952
	;
3953

3954 3955 3956 3957 3958 3959 3960 3961 3962 3963
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
3964 3965
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
3966
               MYSQL_YYABORT;
3967 3968 3969 3970 3971 3972 3973 3974 3975 3976
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


3977 3978 3979 3980 3981
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
3982
	table_list FROM TEXT_STRING_sys
3983 3984
        {
	  Lex->backup_dir = $6.str;
3985
        };
3986

3987 3988 3989 3990 3991
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
3992
	table_list TO_SYM TEXT_STRING_sys
3993 3994
        {
	  Lex->backup_dir = $6.str;
3995
        };
3996

3997 3998 3999 4000 4001 4002
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
4003 4004
	table_list opt_checksum_type
        {}
4005 4006
	;

4007 4008 4009 4010 4011 4012
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
4013
repair:
4014
	REPAIR opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4015
	{
4016 4017
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
4018
           lex->no_write_to_binlog= $2;
4019
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4020
	}
4021 4022 4023
	table_list opt_mi_repair_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4024

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4025
opt_mi_repair_type:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4026
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
4027
	| mi_repair_types {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4028

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4029 4030
mi_repair_types:
	mi_repair_type {}
4031
	| mi_repair_type mi_repair_types {};
4032

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4033
mi_repair_type:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4034
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
4035
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
4036
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4037 4038

analyze:
4039
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4040
	{
4041 4042
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
4043
           lex->no_write_to_binlog= $2;
4044
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4045
	}
4046 4047 4048
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4049 4050

check:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4051
	CHECK_SYM table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4052
	{
4053 4054 4055 4056 4057
	  LEX *lex=Lex;

	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "CHECK");
4058
	    MYSQL_YYABORT;
4059 4060 4061
	  }
	  lex->sql_command = SQLCOM_CHECK;
	  lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4062
	}
4063 4064 4065
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4066

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4067 4068
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
4069
	| mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4070 4071 4072

mi_check_types:
	mi_check_type {}
4073
	| mi_check_type mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
4074 4075 4076 4077 4078 4079

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; }
4080 4081
	| 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
4082

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4083
optimize:
4084
	OPTIMIZE opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4085
	{
4086 4087
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4088
           lex->no_write_to_binlog= $2;
4089
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4090
	}
4091 4092 4093
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4094

4095 4096 4097
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
4098
	| LOCAL_SYM  { $$= 1; }
4099 4100
	;

4101 4102 4103
rename:
	RENAME table_or_tables
	{
4104
          Lex->sql_command= SQLCOM_RENAME_TABLE;
4105
	}
4106 4107
	table_to_table_list
	{}
4108 4109 4110 4111
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
4112
	;
4113

4114 4115 4116 4117
rename_list:
        user TO_SYM user
        {
          if (Lex->users_list.push_back($1) || Lex->users_list.push_back($3))
4118
            MYSQL_YYABORT;
4119 4120 4121 4122
        }
        | rename_list ',' user TO_SYM user
          {
            if (Lex->users_list.push_back($3) || Lex->users_list.push_back($5))
4123
              MYSQL_YYABORT;
4124 4125 4126
          }
        ;

4127 4128
table_to_table_list:
	table_to_table
4129
	| table_to_table_list ',' table_to_table;
4130 4131 4132

table_to_table:
	table_ident TO_SYM table_ident
4133
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4134
	  LEX *lex=Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4135
	  SELECT_LEX *sl= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4136 4137 4138 4139
	  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))
4140
	    MYSQL_YYABORT;
4141
	};
4142

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4143
keycache:
4144
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4145 4146
        {
          LEX *lex=Lex;
4147
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
4148
	  lex->ident= $5;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4149 4150 4151 4152 4153 4154 4155 4156
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
4157
        table_ident cache_keys_spec
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4158 4159 4160 4161 4162 4163
        {
          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(),
4164
                                      (List<String> *)0))
4165
            MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4166 4167 4168
        }
        ;

4169 4170 4171
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
4172
	;
4173

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4174
preload:
4175
	LOAD INDEX_SYM INTO CACHE_SYM
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188
	{
	  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
4189
	table_ident cache_keys_spec opt_ignore_leaves
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4190 4191 4192
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4193 4194
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4195 4196
                                      sel->get_use_index(),
                                      (List<String> *)0))
4197
            MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4198 4199 4200
	}
	;

4201
cache_keys_spec:
4202
        { Select->interval_list.empty(); }
4203 4204 4205 4206 4207 4208 4209
        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
4210

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4211
cache_key_list_or_empty:
4212
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
4213
	| opt_key_or_index '(' key_usage_list2 ')'
4214 4215 4216 4217
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4218 4219
	;

4220
opt_ignore_leaves:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
4221 4222 4223 4224 4225
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4226
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4227
  Select : retrieve data from table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4228 4229 4230 4231
*/


select:
4232 4233 4234 4235
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4236 4237
	}
	;
4238

4239
/* Need select_init2 for subselects. */
4240
select_init:
4241
	SELECT_SYM select_init2
4242
	|
4243 4244 4245 4246
	'(' select_paren ')' union_opt;

select_paren:
	SELECT_SYM select_part2
4247
	  {
4248
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4249
            SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4250
	    if (sel->set_braces(1))
4251
	    {
4252 4253
              my_parse_error(ER(ER_SYNTAX_ERROR));
	      MYSQL_YYABORT;
4254
	    }
4255 4256 4257 4258 4259
            if (sel->linkage == UNION_TYPE &&
                !sel->master_unit()->first_select()->braces &&
                sel->master_unit()->first_select()->linkage ==
                UNION_TYPE)
            {
4260 4261
              my_parse_error(ER(ER_SYNTAX_ERROR));
              MYSQL_YYABORT;
4262
            }
4263
            /* select in braces, can't contain global parameters */
4264 4265 4266
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
4267 4268
          }
	| '(' select_paren ')';
4269

4270 4271
select_init2:
	select_part2
4272
        {
4273
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4274
          SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4275
          if (lex->current_select->set_braces(0))
4276
	  {
4277 4278
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
4279
	  }
4280 4281
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4282
	  {
4283 4284
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4285
	  }
4286
	}
4287
	union_clause
4288 4289
	;

4290
select_part2:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4291
	{
4292 4293
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
4294 4295
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
4296
	  lex->current_select->parsing_place= SELECT_LIST;
4297 4298 4299
	}
	select_options select_item_list
	{
4300
	  Select->parsing_place= NO_MATTER;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4301
	}
4302
	select_into select_lock_type;
4303

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4304
select_into:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
4305
	opt_order_clause opt_limit_clause {}
4306
        | into
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4307
	| select_from
4308 4309
	| into select_from
	| select_from into;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4310 4311

select_from:
4312
        FROM join_table_list where_clause group_clause having_clause
4313
	       opt_order_clause opt_limit_clause procedure_clause
4314 4315 4316 4317 4318
          {
            Select->context.table_list=
              Select->context.first_name_resolution_table= 
                (TABLE_LIST *) Select->table_list.first;
          }
4319
        | FROM DUAL_SYM where_clause opt_limit_clause
timour@mysql.com's avatar
timour@mysql.com committed
4320 4321 4322 4323
          /* 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 :) */
4324
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4325 4326 4327

select_options:
	/* empty*/
monty@mysql.com's avatar
monty@mysql.com committed
4328 4329 4330 4331
	| select_option_list
	  {
	    if (test_all_bits(Select->options, SELECT_ALL | SELECT_DISTINCT))
	    {
monty@mysql.com's avatar
monty@mysql.com committed
4332
	      my_error(ER_WRONG_USAGE, MYF(0), "ALL", "DISTINCT");
4333
              MYSQL_YYABORT;
monty@mysql.com's avatar
monty@mysql.com committed
4334 4335
	    }
          }
monty@mysql.com's avatar
monty@mysql.com committed
4336
	  ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4337 4338 4339

select_option_list:
	select_option_list select_option
4340
	| select_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4341 4342

select_option:
4343
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
4344 4345 4346
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
4347
	      MYSQL_YYABORT;
4348 4349
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
monty@mysql.com's avatar
monty@mysql.com committed
4350
	| DISTINCT         { Select->options|= SELECT_DISTINCT; }
4351 4352
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
4353 4354 4355
	| SQL_BUFFER_RESULT
	  {
	    if (check_simple_select())
4356
	      MYSQL_YYABORT;
4357 4358 4359 4360 4361
	    Select->options|= OPTION_BUFFER_RESULT;
	  }
	| SQL_CALC_FOUND_ROWS
	  {
	    if (check_simple_select())
4362
	      MYSQL_YYABORT;
4363 4364
	    Select->options|= OPTION_FOUND_ROWS;
	  }
4365 4366 4367 4368 4369 4370
	| 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;
          }
4371 4372
	| SQL_CACHE_SYM
	  {
4373 4374 4375 4376 4377 4378
            /*
             Honor this flag only if SQL_NO_CACHE wasn't specified AND
             we are parsing the outermost SELECT in the query.
            */
            if (Lex->select_lex.sql_cache != SELECT_LEX::SQL_NO_CACHE &&
                Lex->current_select == &Lex->select_lex)
4379 4380 4381 4382 4383
            {
              Lex->safe_to_cache_query=1;
	      Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
              Lex->select_lex.sql_cache= SELECT_LEX::SQL_CACHE;
            }
4384
	  }
monty@mysql.com's avatar
monty@mysql.com committed
4385
	| ALL		    { Select->options|= SELECT_ALL; }
4386
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4387

monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
4388 4389 4390
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
4391 4392
	  {
	    LEX *lex=Lex;
4393
	    lex->current_select->set_lock_for_tables(TL_WRITE);
4394
	    lex->safe_to_cache_query=0;
4395
	  }
4396
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
4397 4398
	  {
	    LEX *lex=Lex;
4399 4400
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
4401
	    lex->safe_to_cache_query=0;
4402 4403
	  }
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
4404

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4405 4406 4407 4408 4409
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
4410
	    THD *thd= YYTHD;
4411 4412 4413 4414
	    if (add_item_to_list(thd,
                                 new Item_field(&thd->lex->current_select->
                                                context,
                                                NULL, NULL, "*")))
4415
	      MYSQL_YYABORT;
4416
	    (thd->lex->current_select->with_wild)++;
4417
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4418 4419 4420 4421 4422


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
4423 4424 4425 4426
            THD *thd= YYTHD;
            DBUG_ASSERT($1 < $3);

	    if (add_item_to_list(thd, $2))
4427
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4428
	    if ($4.str)
4429 4430
            {
              $2->is_autogenerated_name= FALSE;
4431
	      $2->set_name($4.str, $4.length, system_charset_info);
4432
            }
4433 4434 4435
	    else if (!$2->name)
            {
	      $2->set_name($1, (uint) ($3 - $1), thd->charset());
4436
	    }
4437
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4438

4439

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4440
remember_name:
4441 4442 4443 4444 4445
	{
          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
4446 4447

remember_end:
4448 4449 4450 4451 4452
	{
          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
4453 4454 4455

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

select_alias:
4459
	/* empty */		{ $$=null_lex_str;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4460 4461 4462 4463 4464
	| 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
4465 4466 4467

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4470
/* all possible expressions */
4471
expr:
4472
          expr or expr %prec OR_SYM
4473
          {
4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485
            /*
              Design notes:
              Do not use a manually maintained stack like thd->lex->xxx_list,
              but use the internal bison stack ($$, $1 and $3) instead.
              Using the bison stack is:
              - more robust to changes in the grammar,
              - guaranteed to be in sync with the parser state,
              - better for performances (no memory allocation).
            */
            Item_cond_or *item1;
            Item_cond_or *item3;
            if (is_cond_or($1))
4486
            {
4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513
              item1= (Item_cond_or*) $1;
              if (is_cond_or($3))
              {
                item3= (Item_cond_or*) $3;
                /*
                  (X1 OR X2) OR (Y1 OR Y2) ==> OR (X1, X2, Y1, Y2)
                */
                item3->add_at_head(item1->argument_list());
                $$ = $3;
              }
              else
              {
                /*
                  (X1 OR X2) OR Y ==> OR (X1, X2, Y)
                */
                item1->add($3);
                $$ = $1;
              }
            }
            else if (is_cond_or($3))
            {
              item3= (Item_cond_or*) $3;
              /*
                X OR (Y1 OR Y2) ==> OR (X, Y1, Y2)
              */
              item3->add_at_head($1);
              $$ = $3;
4514 4515
            }
            else
4516 4517 4518 4519
            {
              /* X OR Y */
              $$ = new (YYTHD->mem_root) Item_cond_or($1, $3);
            }
4520
          }
4521 4522 4523 4524 4525 4526
        | expr XOR expr %prec XOR
          {
            /* XOR is a proprietary extension */
            $$ = new (YYTHD->mem_root) Item_cond_xor($1, $3);
          }
        | expr and expr %prec AND_SYM
4527
          {
4528 4529 4530 4531
            /* See comments in rule expr: expr or expr */
            Item_cond_and *item1;
            Item_cond_and *item3;
            if (is_cond_and($1))
4532
            {
4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559
              item1= (Item_cond_and*) $1;
              if (is_cond_and($3))
              {
                item3= (Item_cond_and*) $3;
                /*
                  (X1 AND X2) AND (Y1 AND Y2) ==> AND (X1, X2, Y1, Y2)
                */
                item3->add_at_head(item1->argument_list());
                $$ = $3;
              }
              else
              {
                /*
                  (X1 AND X2) AND Y ==> AND (X1, X2, Y)
                */
                item1->add($3);
                $$ = $1;
              }
            }
            else if (is_cond_and($3))
            {
              item3= (Item_cond_and*) $3;
              /*
                X AND (Y1 AND Y2) ==> AND (X, Y1, Y2)
              */
              item3->add_at_head($1);
              $$ = $3;
4560 4561
            }
            else
4562 4563 4564 4565
            {
              /* X AND Y */
              $$ = new (YYTHD->mem_root) Item_cond_and($1, $3);
            }
4566
          }
4567 4568 4569
	| NOT_SYM expr %prec NOT_SYM
          { $$= negate_expression(YYTHD, $2); }
        | bool_pri IS TRUE_SYM %prec IS
4570
          { $$= new (YYTHD->mem_root) Item_func_istrue($1); }
4571
        | bool_pri IS not TRUE_SYM %prec IS
4572
          { $$= new (YYTHD->mem_root) Item_func_isnottrue($1); }
4573
        | bool_pri IS FALSE_SYM %prec IS
4574
          { $$= new (YYTHD->mem_root) Item_func_isfalse($1); }
4575
        | bool_pri IS not FALSE_SYM %prec IS
4576
          { $$= new (YYTHD->mem_root) Item_func_isnotfalse($1); }
4577 4578 4579 4580
        | bool_pri IS UNKNOWN_SYM %prec IS
          { $$= new Item_func_isnull($1); }
        | bool_pri IS not UNKNOWN_SYM %prec IS
          { $$= new Item_func_isnotnull($1); }
4581 4582
        | bool_pri
        ;
4583 4584

bool_pri:
4585 4586 4587 4588 4589 4590
	bool_pri IS NULL_SYM %prec IS
          { $$= new Item_func_isnull($1); }
	| bool_pri IS not NULL_SYM %prec IS
          { $$= new Item_func_isnotnull($1); }
	| bool_pri EQUAL_SYM predicate %prec EQUAL_SYM
          { $$= new Item_func_equal($1,$3); }
4591 4592
	| bool_pri comp_op predicate %prec EQ
	  { $$= (*$2)(0)->create($1,$3); }
4593 4594
	| bool_pri comp_op all_or_any '(' subselect ')' %prec EQ
	  { $$= all_any_subquery_creator($1, $2, $3, $5); }
4595 4596 4597
	| predicate ;

predicate:
4598 4599 4600 4601 4602 4603 4604 4605 4606 4607
          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);
          }
4608 4609
        | bit_expr IN_SYM '(' expr ')'
          {
4610
            $$= handle_sql2003_note184_exception(YYTHD, $1, true, $4);
4611
          }
4612 4613 4614 4615 4616
        | bit_expr IN_SYM '(' expr ',' expr_list ')'
          { 
            $6->push_front($4);
            $6->push_front($1);
            $$= new (YYTHD->mem_root) Item_func_in(*$6);
4617
          }
4618
        | bit_expr not IN_SYM '(' expr ')'
4619
          {
4620
            $$= handle_sql2003_note184_exception(YYTHD, $1, false, $5);
4621
          }
4622
        | bit_expr not IN_SYM '(' expr ',' expr_list ')'
4623
          {
4624 4625 4626 4627 4628
            $7->push_front($5);
            $7->push_front($1);
            Item_func_in *item = new (YYTHD->mem_root) Item_func_in(*$7);
            item->negate();
            $$= item;
4629
          }
4630 4631 4632
	| 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
4633 4634 4635 4636 4637
          {
            Item_func_between *item= new Item_func_between($1,$4,$6);
            item->negate();
            $$= item;
          }
4638 4639 4640 4641
	| 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
4642
          { $$= new Item_func_like($1,$3,$4,Lex->escape_used); }
4643
	| bit_expr not LIKE simple_expr opt_escape
4644
          { $$= new Item_func_not(new Item_func_like($1,$4,$5, Lex->escape_used)); }
4645 4646
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
4647
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
4648 4649 4650
	| bit_expr ;

bit_expr:
4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680
          bit_expr '|' bit_expr %prec '|'
          { $$= new Item_func_bit_or($1,$3); }
        | bit_expr '&' bit_expr %prec '&'
          { $$= new Item_func_bit_and($1,$3); }
        | bit_expr SHIFT_LEFT bit_expr %prec SHIFT_LEFT
          { $$= new Item_func_shift_left($1,$3); }
        | bit_expr SHIFT_RIGHT bit_expr %prec SHIFT_RIGHT
          { $$= new Item_func_shift_right($1,$3); }
        | bit_expr '+' bit_expr %prec '+'
          { $$= new Item_func_plus($1,$3); }
        | bit_expr '-' bit_expr %prec '-'
          { $$= new Item_func_minus($1,$3); }
        | bit_expr '+' interval_expr interval %prec '+'
          { $$= new Item_date_add_interval($1,$3,$4,0); }
        | bit_expr '-' interval_expr interval %prec '-'
          { $$= new Item_date_add_interval($1,$3,$4,1); }
        | bit_expr '*' bit_expr %prec '*'
          { $$= new Item_func_mul($1,$3); }
        | bit_expr '/' bit_expr %prec '/'
          { $$= new Item_func_div($1,$3); }
        | bit_expr '%' bit_expr %prec '%'
          { $$= new Item_func_mod($1,$3); }
        | bit_expr DIV_SYM bit_expr %prec DIV_SYM
          { $$= new Item_func_int_div($1,$3); }
        | bit_expr MOD_SYM bit_expr %prec MOD_SYM
          { $$= new Item_func_mod($1,$3); }
        | bit_expr '^' bit_expr
          { $$= new Item_func_bit_xor($1,$3); }
        | simple_expr
        ;
4681 4682 4683 4684 4685

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
4686

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698
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; }
        ;

4699
interval_expr:
4700 4701
          INTERVAL_SYM expr %prec INTERVAL_SYM
          { $$=$2; }
4702 4703
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4704 4705
simple_expr:
	simple_ident
4706
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4707
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4708 4709 4710
	    $$= new Item_func_set_collation($1,
					    new Item_string($3.str,
							    $3.length,
4711
                                                            YYTHD->charset()));
4712
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4713
	| literal
4714
	| param_marker
4715
	| variable
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4716
	| sum_expr
4717 4718 4719 4720 4721 4722
	| 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); }
4723 4724 4725 4726
	| '(' subselect ')'   
          { 
            $$= new Item_singlerow_subselect($2); 
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4727
	| '(' expr ')'		{ $$= $2; }
4728 4729 4730 4731 4732
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
	    $$= new Item_row(*$4);
	  }
4733
	| ROW_SYM '(' expr ',' expr_list ')'
4734
	  {
4735 4736
	    $5->push_front($3);
	    $$= new Item_row(*$5);
4737
	  }
4738 4739 4740 4741
	| EXISTS '(' subselect ')' 
          {
            $$= new Item_exists_subselect($3); 
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4742
	| '{' ident expr '}'	{ $$= $3; }
4743
        | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
4744
          { $2->push_front($5);
4745 4746
            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
4747
	| ASCII_SYM '(' expr ')' { $$= new Item_func_ascii($3); }
4748
	| BINARY simple_expr %prec NEG
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4749
	  {
4750
            $$= create_func_cast($2, ITEM_CAST_CHAR, NULL, NULL, &my_charset_bin);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4751
	  }
4752
	| CAST_SYM '(' expr AS cast_type ')'
4753
	  {
4754
            LEX *lex= Lex;
4755
	    $$= create_func_cast($3, $5, lex->length, lex->dec, lex->charset);
4756
            if (!$$)
4757
              MYSQL_YYABORT;
4758
	  }
4759 4760
	| CASE_SYM opt_expr when_list opt_else END
	  { $$= new Item_func_case(* $3, $2, $4 ); }
4761
	| CONVERT_SYM '(' expr ',' cast_type ')'
4762
	  {
4763
	    $$= create_func_cast($3, $5, Lex->length, Lex->dec, Lex->charset);
4764
            if (!$$)
4765
              MYSQL_YYABORT;
4766
	  }
4767 4768
	| 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
4769
	| DEFAULT '(' simple_ident ')'
4770 4771 4772 4773 4774
	  {
	    if ($3->is_splocal())
	    {
	      Item_splocal *il= static_cast<Item_splocal *>($3);

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

	    name->init_qname(YYTHD);
5089
	    sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
5090
	    if ($5)
5091
	      $$= new Item_func_sp(Lex->current_context(), name, *$5);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5092
	    else
5093
	      $$= new Item_func_sp(Lex->current_context(), name);
5094
	    lex->safe_to_cache_query=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5095
	  }
5096
	| IDENT_sys '(' 
5097 5098
          {
#ifdef HAVE_DLOPEN
5099
            udf_func *udf= 0;
5100
            LEX *lex= Lex;
5101 5102 5103 5104 5105 5106
            if (using_udf_functions &&
                (udf= find_udf($1.str, $1.length)) &&
                udf->type == UDFTYPE_AGGREGATE)
            {
              if (lex->current_select->inc_in_sum_expr())
              {
5107 5108
                my_parse_error(ER(ER_SYNTAX_ERROR));
                MYSQL_YYABORT;
5109 5110
              }
            }
5111
            lex->current_select->udf_list.push_front(udf);
5112 5113 5114 5115
#endif
          }
          udf_expr_list ')'
          {
5116
            LEX *lex= Lex;
5117
#ifdef HAVE_DLOPEN
5118
            udf_func *udf;
5119

5120
            if (NULL != (udf= lex->current_select->udf_list.pop()))
5121
            {
5122 5123 5124
              if (udf->type == UDFTYPE_AGGREGATE)
                Select->in_sum_expr--;

5125 5126 5127 5128
              switch (udf->returns) {
              case STRING_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
5129 5130
                  if ($4 != NULL)
                    $$ = new Item_func_udf_str(udf, *$4);
5131 5132 5133 5134 5135
                  else
                    $$ = new Item_func_udf_str(udf);
                }
                else
                {
5136 5137
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_str(udf, *$4);
5138 5139 5140 5141 5142 5143 5144
                  else
                    $$ = new Item_sum_udf_str(udf);
                }
                break;
              case REAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
5145 5146
                  if ($4 != NULL)
                    $$ = new Item_func_udf_float(udf, *$4);
5147 5148 5149 5150 5151
                  else
                    $$ = new Item_func_udf_float(udf);
                }
                else
                {
5152 5153
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_float(udf, *$4);
5154 5155 5156 5157 5158 5159 5160
                  else
                    $$ = new Item_sum_udf_float(udf);
                }
                break;
              case INT_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
5161 5162
                  if ($4 != NULL)
                    $$ = new Item_func_udf_int(udf, *$4);
5163 5164 5165 5166 5167
                  else
                    $$ = new Item_func_udf_int(udf);
                }
                else
                {
5168 5169
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_int(udf, *$4);
5170 5171 5172 5173
                  else
                    $$ = new Item_sum_udf_int(udf);
                }
                break;
5174 5175 5176
              case DECIMAL_RESULT:
                if (udf->type == UDFTYPE_FUNCTION)
                {
5177 5178
                  if ($4 != NULL)
                    $$ = new Item_func_udf_decimal(udf, *$4);
5179 5180 5181 5182 5183
                  else
                    $$ = new Item_func_udf_decimal(udf);
                }
                else
                {
5184 5185
                  if ($4 != NULL)
                    $$ = new Item_sum_udf_decimal(udf, *$4);
5186 5187 5188 5189
                  else
                    $$ = new Item_sum_udf_decimal(udf);
                }
                break;
5190
              default:
5191
                MYSQL_YYABORT;
5192 5193 5194 5195 5196
              }
            }
            else
#endif /* HAVE_DLOPEN */
            {
5197 5198
              THD *thd= lex->thd;
              LEX_STRING db;
5199
              if (lex->copy_db_to(&db.str, &db.length))
5200
                MYSQL_YYABORT;
5201
              sp_name *name= new sp_name(db, $1, false);
5202 5203
              if (name)
                name->init_qname(thd);
5204

5205
              sp_add_used_routine(lex, YYTHD, name, TYPE_ENUM_FUNCTION);
5206 5207
              if ($4)
                $$= new Item_func_sp(Lex->current_context(), name, *$4);
5208
              else
5209
                $$= new Item_func_sp(Lex->current_context(), name);
5210 5211
            }          
          lex->safe_to_cache_query=0;
5212
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5213
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
5214
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5215 5216
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5217
	| UNIX_TIMESTAMP '(' ')'
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5218 5219
	  {
	    $$= new Item_func_unix_timestamp();
5220
	    Lex->safe_to_cache_query=0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5221
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5222 5223 5224
	| UNIX_TIMESTAMP '(' expr ')'
	  { $$= new Item_func_unix_timestamp($3); }
	| USER '(' ')'
5225
	  { $$= new Item_func_user(); Lex->safe_to_cache_query=0; }
5226 5227 5228 5229 5230 5231
	| 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
5232
	| WEEK_SYM '(' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5233
	  {
5234
            $$= new Item_func_week($3,new Item_int((char*) "0",
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5235
				   YYTHD->variables.default_week_format,1));
5236
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5237 5238 5239 5240 5241 5242 5243 5244
	| 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
5245
	| BENCHMARK_SYM '(' ulong_num ',' expr ')'
5246
	  {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5247
	    $$=new Item_func_benchmark($3,$5);
5248
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5249
	  }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5250
	| EXTRACT_SYM '(' interval FROM expr ')'
5251
	{ $$=new Item_extract( $3, $5); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5252

hf@deer.(none)'s avatar
hf@deer.(none) committed
5253
geometry_function:
serg@serg.mylan's avatar
serg@serg.mylan committed
5254 5255 5256
	  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
5257 5258 5259 5260 5261 5262 5263 5264 5265
	  { $$= 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,
5266 5267
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5268 5269
	| LINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
5270
                  Geometry::wkb_linestring, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5271 5272
 	| MULTILINESTRING '(' expr_list ')'
	  { $$= GEOM_NEW( Item_func_spatial_collection(* $3,
5273
                   Geometry::wkb_multilinestring, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287
 	| 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,
5288
                  Geometry::wkb_multipoint, Geometry::wkb_point)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5289 5290
 	| MULTIPOLYGON '(' expr_list ')'
	  { $$= GEOM_NEW(Item_func_spatial_collection(* $3,
5291
                  Geometry::wkb_multipolygon, Geometry::wkb_polygon)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303
	| 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,
5304
	          Geometry::wkb_polygon, Geometry::wkb_linestring)); }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5305 5306 5307 5308 5309 5310 5311 5312 5313 5314
 	| 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)); }
	;

5315 5316 5317 5318 5319 5320
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
5321
udf_expr_list:
5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345
	/* 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
	{
5346 5347 5348 5349 5350 5351 5352
          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.
          */
5353
	  if ($4.str)
5354
          {
5355 5356 5357 5358 5359 5360
            if (!udf)
            {
              /*
                Disallow using AS to specify explicit names for the arguments
                of stored routine calls
              */
5361 5362
              my_parse_error(ER(ER_SYNTAX_ERROR));
              MYSQL_YYABORT;
5363 5364
            }

5365
            $2->is_autogenerated_name= FALSE;
5366
	    $2->set_name($4.str, $4.length, system_charset_info);
5367
          }
5368
	  else if (udf)
5369
	    $2->set_name($1, (uint) ($3 - $1), YYTHD->charset());
5370 5371 5372
	  $$= $2;
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5373 5374 5375 5376

sum_expr:
	AVG_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_avg($3); }
5377 5378
	| AVG_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_avg_distinct($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5379 5380 5381 5382
	| BIT_AND  '(' in_sum_expr ')'
	  { $$=new Item_sum_and($3); }
	| BIT_OR  '(' in_sum_expr ')'
	  { $$=new Item_sum_or($3); }
5383 5384
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
5385
	| COUNT_SYM '(' opt_all '*' ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5386 5387 5388
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
5389
	| COUNT_SYM '(' DISTINCT
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5390
	  { Select->in_sum_expr++; }
5391
	   expr_list
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5392
	  { Select->in_sum_expr--; }
5393 5394
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5395 5396 5397 5398
	| 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); }
5399 5400 5401 5402 5403 5404 5405
/*
   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
5406 5407
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
5408 5409
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5410
	| STD_SYM '(' in_sum_expr ')'
5411
	  { $$=new Item_sum_std($3, 0); }
5412
	| VARIANCE_SYM '(' in_sum_expr ')'
5413 5414 5415 5416 5417
	  { $$=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
5418
	| SUM_SYM '(' in_sum_expr ')'
5419
	  { $$=new Item_sum_sum($3); }
5420 5421
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
monty@mysql.com's avatar
monty@mysql.com committed
5422 5423 5424 5425 5426
	| 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
5427
	  {
5428 5429
            SELECT_LEX *sel= Select;
	    sel->in_sum_expr--;
5430
	    $$=new Item_func_group_concat(Lex->current_context(), $3, $5,
5431
                                          sel->gorder_list, $7);
monty@mysql.com's avatar
monty@mysql.com committed
5432
	    $5->empty();
5433 5434
	  };

5435 5436 5437 5438 5439 5440
variable:
          '@'
          {
            if (! Lex->parsing_options.allows_variable)
            {
              my_error(ER_VIEW_SELECT_VARIABLE, MYF(0));
5441
              MYSQL_YYABORT;
5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466
            }
          }
          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))
            {
5467 5468
              my_parse_error(ER(ER_SYNTAX_ERROR));
              MYSQL_YYABORT;
5469 5470
            }
            if (!($$= get_system_var(YYTHD, $2, $3, $4)))
5471
              MYSQL_YYABORT;
5472 5473 5474
          }
        ;

5475 5476 5477 5478 5479
opt_distinct:
    /* empty */ { $$ = 0; }
    |DISTINCT   { $$ = 1; };

opt_gconcat_separator:
5480 5481 5482 5483 5484
    /* empty */
      {
        $$= new (YYTHD->mem_root) String(",", 1, &my_charset_latin1);
      }
    | SEPARATOR_SYM text_string { $$ = $2; };
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5485

5486 5487

opt_gorder_clause:
5488 5489
	  /* empty */
	  {
5490
            Select->gorder_list = NULL;
5491 5492 5493
	  }
	| order_clause
          {
5494 5495 5496
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
5497
				     sizeof(st_sql_list));
5498
	    select->order_list.empty();
5499
	  };
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5500

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5501 5502

in_sum_expr:
5503
	opt_all
5504 5505 5506 5507
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
5508 5509
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
5510 5511
	  }
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5512 5513
	expr
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5514
	  Select->in_sum_expr--;
5515
	  $$= $3;
5516
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5517

5518
cast_type:
5519 5520
        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; }
5521
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; Lex->dec=0; }
5522 5523 5524 5525 5526 5527 5528 5529
        | 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; }
5530 5531
	;

5532 5533 5534 5535 5536
opt_expr_list:
	/* empty */ { $$= NULL; }
	| expr_list { $$= $1;}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5537
expr_list:
5538
	{ Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5539
	expr_list2
5540
	{ $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5541 5542

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

5546 5547
ident_list_arg:
          ident_list          { $$= $1; }
5548
        | '(' ident_list ')'  { $$= $2; };
5549

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5550
ident_list:
5551
        { Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5552
        ident_list2
5553
        { $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5554 5555

ident_list2:
5556
        simple_ident { Select->expr_list.head()->push_back($1); }
5557
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5558 5559 5560

opt_expr:
	/* empty */      { $$= NULL; }
5561
	| expr           { $$= $1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5562 5563 5564

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

when_list:
5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580
          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
5581

5582
/* Warning - may return NULL in case of incomplete SELECT */
5583 5584
table_ref:
        table_factor            { $$=$1; }
5585
        | join_table
5586
          {
5587 5588
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
5589
              MYSQL_YYABORT;
5590
          }
5591 5592
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5593
join_table_list:
5594
	derived_table_list		{ MYSQL_YYABORT_UNLESS($$=$1); }
5595
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
5596

5597 5598
/* Warning - may return NULL in case of incomplete SELECT */
derived_table_list:
5599
        table_ref { $$=$1; }
5600 5601
        | derived_table_list ',' table_ref
          {
5602
            MYSQL_YYABORT_UNLESS($1 && ($$=$3));
5603
          }
5604 5605
        ;

5606 5607 5608 5609 5610 5611 5612
/*
  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.
*/
5613
join_table:
5614
/* INNER JOIN variants */
timour@mysql.com's avatar
timour@mysql.com committed
5615
        /*
5616 5617 5618
          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
5619
        */
5620
        table_ref normal_join table_ref %prec TABLE_REF_PRIORITY
5621
          { MYSQL_YYABORT_UNLESS($1 && ($$=$3)); }
5622
	| table_ref STRAIGHT_JOIN table_factor
5623
	  { MYSQL_YYABORT_UNLESS($1 && ($$=$3)); $3->straight=1; }
5624 5625 5626
	| table_ref normal_join table_ref
          ON
          {
5627
            MYSQL_YYABORT_UNLESS($1 && $3);
5628
            /* Change the current name resolution context to a local context. */
5629
            if (push_new_name_resolution_context(YYTHD, $1, $3))
5630
              MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5631
            Select->parsing_place= IN_ON;
5632 5633 5634 5635 5636
          }
          expr
	  {
            add_join_on($3,$6);
            Lex->pop_context();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5637
            Select->parsing_place= NO_MATTER;
5638 5639 5640 5641
          }
        | table_ref STRAIGHT_JOIN table_factor
          ON
          {
5642
            MYSQL_YYABORT_UNLESS($1 && $3);
5643
            /* Change the current name resolution context to a local context. */
5644
            if (push_new_name_resolution_context(YYTHD, $1, $3))
5645
              MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5646
            Select->parsing_place= IN_ON;
5647 5648 5649 5650 5651 5652
          }
          expr
          {
            $3->straight=1;
            add_join_on($3,$6);
            Lex->pop_context();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5653
            Select->parsing_place= NO_MATTER;
5654
          }
5655
	| table_ref normal_join table_ref
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5656
	  USING
5657
	  {
5658
            MYSQL_YYABORT_UNLESS($1 && $3);
5659
	  }
5660
	  '(' using_list ')'
5661
          { add_join_natural($1,$3,$7,Select); $$=$3; }
5662 5663
	| table_ref NATURAL JOIN_SYM table_factor
	  {
5664
            MYSQL_YYABORT_UNLESS($1 && ($$=$4));
5665
            add_join_natural($1,$4,NULL,Select);
5666 5667 5668
          }

/* LEFT JOIN variants */
5669 5670 5671
	| table_ref LEFT opt_outer JOIN_SYM table_ref
          ON
          {
5672
            MYSQL_YYABORT_UNLESS($1 && $5);
5673
            /* Change the current name resolution context to a local context. */
5674
            if (push_new_name_resolution_context(YYTHD, $1, $5))
5675
              MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5676
            Select->parsing_place= IN_ON;
5677 5678 5679 5680 5681 5682 5683
          }
          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
5684
            Select->parsing_place= NO_MATTER;
5685
          }
5686
	| table_ref LEFT opt_outer JOIN_SYM table_factor
5687
	  {
5688
            MYSQL_YYABORT_UNLESS($1 && $5);
5689
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5690
	  USING '(' using_list ')'
5691 5692 5693 5694 5695
          { 
            add_join_natural($1,$5,$9,Select); 
            $5->outer_join|=JOIN_TYPE_LEFT; 
            $$=$5; 
          }
5696
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
5697
	  {
5698
            MYSQL_YYABORT_UNLESS($1 && $6);
5699
 	    add_join_natural($1,$6,NULL,Select);
5700
	    $6->outer_join|=JOIN_TYPE_LEFT;
5701 5702
	    $$=$6;
	  }
5703 5704

/* RIGHT JOIN variants */
5705 5706 5707
	| table_ref RIGHT opt_outer JOIN_SYM table_ref
          ON
          {
5708
            MYSQL_YYABORT_UNLESS($1 && $5);
5709
            /* Change the current name resolution context to a local context. */
5710
            if (push_new_name_resolution_context(YYTHD, $1, $5))
5711
              MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5712
            Select->parsing_place= IN_ON;
5713 5714
          }
          expr
serg@serg.mylan's avatar
serg@serg.mylan committed
5715
          {
5716 5717
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
5718
              MYSQL_YYABORT;
5719 5720
            add_join_on($$, $8);
            Lex->pop_context();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5721
            Select->parsing_place= NO_MATTER;
5722 5723
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
5724
	  {
5725
            MYSQL_YYABORT_UNLESS($1 && $5);
5726
	  }
5727
	  USING '(' using_list ')'
serg@serg.mylan's avatar
serg@serg.mylan committed
5728
          {
5729 5730
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
5731
              MYSQL_YYABORT;
5732
            add_join_natural($$,$5,$9,Select);
5733 5734
          }
	| table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor
5735
	  {
5736
            MYSQL_YYABORT_UNLESS($1 && $6);
5737
	    add_join_natural($6,$1,NULL,Select);
5738 5739
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
5740
              MYSQL_YYABORT;
5741
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5742 5743

normal_join:
5744 5745 5746 5747
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5748

5749
/* Warning - may return NULL in case of incomplete SELECT */
5750
table_factor:
5751
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5752
	  SELECT_LEX *sel= Select;
5753
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5754
	  sel->table_join_options= 0;
5755
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5756
        table_ident opt_table_alias opt_key_definition
5757
	{
5758
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5759
	  SELECT_LEX *sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5760
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5761
					   sel->get_table_join_options(),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5762
					   lex->lock_option,
5763 5764
					   sel->get_use_index(),
					   sel->get_ignore_index())))
5765
	    MYSQL_YYABORT;
5766
          sel->add_joined_table($$);
5767
	}
5768 5769 5770 5771
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref
          ON
          {
            /* Change the current name resolution context to a local context. */
5772
            if (push_new_name_resolution_context(YYTHD, $3, $7))
5773
              MYSQL_YYABORT;
5774

5775 5776 5777
          }
          expr '}'
	  {
5778
	    LEX *lex= Lex;
5779
            MYSQL_YYABORT_UNLESS($3 && $7);
5780 5781 5782 5783
            add_join_on($7,$10);
            Lex->pop_context();
            $7->outer_join|=JOIN_TYPE_LEFT;
            $$=$7;
5784
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
5785
              MYSQL_YYABORT;
5786
          }
5787
	| select_derived_init get_select_lex select_derived2
5788
          {
5789
            LEX *lex= Lex;
5790 5791 5792 5793 5794
            SELECT_LEX *sel= lex->current_select;
            if ($1)
            {
	      if (sel->set_braces(1))
	      {
5795 5796
                my_parse_error(ER(ER_SYNTAX_ERROR));
	        MYSQL_YYABORT;
5797 5798 5799 5800 5801 5802 5803
	      }
              /* 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))
5804
              MYSQL_YYABORT;
5805
            $$= 0;
serg@serg.mylan's avatar
serg@serg.mylan committed
5806
            /* incomplete derived tables return NULL, we must be
5807
               nested in select_derived rule to be here. */
5808
          }
5809 5810 5811 5812 5813
	| '(' 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
5814
          if (!($3 || $6) && $2->embedding &&
5815
              !$2->embedding->nested_join->join_list.elements)
5816
          {
5817 5818 5819 5820 5821
            /* 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;
5822
          }
5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836
          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
5837

5838
	      MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
5839
            sel->add_joined_table($$);
5840
            lex->pop_context();
5841 5842 5843 5844 5845
          }
	  else
          if ($4 || $6)
	  {
            /* simple nested joins cannot have aliases or unions */
5846 5847
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
5848 5849 5850 5851 5852
	  }
          else
            $$= $3;
	}
        ;
5853

5854
/* handle contents of parentheses in join expression */
5855
select_derived:
5856
	  get_select_lex
5857
	  {
5858 5859
            LEX *lex= Lex;
            if ($1->init_nested_join(lex->thd))
5860
              MYSQL_YYABORT;
5861 5862 5863 5864 5865 5866
          }
          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
5867

5868
            if (!($$= $1->end_nested_join(lex->thd)) && $3)
5869
              MYSQL_YYABORT;
5870 5871
            if (!$3 && $$)
            {
5872 5873
              my_parse_error(ER(ER_SYNTAX_ERROR));
	      MYSQL_YYABORT;
5874 5875
            }
          }
monty@mysql.com's avatar
monty@mysql.com committed
5876
 	;
5877

monty@mysql.com's avatar
monty@mysql.com committed
5878
select_derived2:
5879
        {
5880
	  LEX *lex= Lex;
5881
	  lex->derived_tables|= DERIVED_SUBQUERY;
5882
          if (lex->sql_command == (int)SQLCOM_HA_READ ||
5883 5884
              lex->sql_command == (int)SQLCOM_KILL ||
              lex->sql_command == (int)SQLCOM_PURGE)
5885
	  {
5886 5887
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
5888
	  }
5889
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
5890
              mysql_new_select(lex, 1))
5891
	    MYSQL_YYABORT;
5892
	  mysql_init_select(lex);
5893
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
5894
	  lex->current_select->parsing_place= SELECT_LIST;
5895 5896 5897
	}
        select_options select_item_list
	{
5898
	  Select->parsing_place= NO_MATTER;
5899
	}
5900
	opt_select_from
5901
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5902

5903 5904 5905 5906 5907 5908 5909 5910
get_select_lex:
	/* Empty */ { $$= Select; }
        ;

select_derived_init:
          SELECT_SYM
          {
            LEX *lex= Lex;
5911 5912 5913 5914

            if (! lex->parsing_options.allows_derived)
            {
              my_error(ER_VIEW_SELECT_DERIVED, MYF(0));
5915
              MYSQL_YYABORT;
5916 5917
            }

serg@serg.mylan's avatar
serg@serg.mylan committed
5918
            SELECT_LEX *sel= lex->current_select;
5919 5920 5921 5922
            TABLE_LIST *embedding;
            if (!sel->embedding || sel->end_nested_join(lex->thd))
	    {
              /* we are not in parentheses */
5923 5924
              my_parse_error(ER(ER_SYNTAX_ERROR));
	      MYSQL_YYABORT;
5925 5926 5927 5928 5929 5930 5931 5932
	    }
            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
5933 5934
opt_outer:
	/* empty */	{}
5935
	| OUTER		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5936

5937 5938 5939 5940
opt_for_join:
        /* empty */
        | FOR_SYM JOIN_SYM;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5941 5942 5943
opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
5944
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5945
	    SELECT_LEX *sel= Select;
5946 5947 5948
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5949 5950
	| FORCE_SYM key_usage_list
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5951
	    SELECT_LEX *sel= Select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5952 5953 5954 5955
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
5956
	| IGNORE_SYM key_usage_list
5957
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5958
	    SELECT_LEX *sel= Select;
5959
	    sel->ignore_index= *$2;
5960
	    sel->ignore_index_ptr= &sel->ignore_index;
5961
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5962 5963

key_usage_list:
5964
        key_or_index opt_for_join      
5965
	{ Select->interval_list.empty(); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5966
        '(' key_list_or_empty ')'
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5967
        { $$= &Select->interval_list; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5968 5969 5970 5971 5972 5973
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5974 5975 5976

key_usage_list2:
	key_usage_list2 ',' ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5977
        { Select->
5978
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $3.str, $3.length,
5979
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5980
	| ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5981
        { Select->
5982
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $1.str, $1.length,
5983
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5984
	| PRIMARY_SYM
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5985
        { Select->
5986
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
5987
				    system_charset_info)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5988 5989 5990

using_list:
	ident
5991
	  {
5992
            if (!($$= new List<String>))
5993
	      MYSQL_YYABORT;
5994 5995 5996
            $$->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
5997 5998 5999
	  }
	| using_list ',' ident
	  {
6000 6001 6002 6003
            $1->push_back(new (YYTHD->mem_root)
                              String((const char *) $3.str, $3.length,
                                      system_charset_info));
            $$= $1;
6004
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6005 6006

interval:
6007 6008
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6009
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6010 6011
	| 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
6012
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6013 6014
	| 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
6015 6016
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6017
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
6018 6019 6020 6021 6022 6023 6024 6025
	| 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
6026 6027
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
6028
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6029
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
6030 6031
	| YEAR_SYM		{ $$=INTERVAL_YEAR; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6032

6033
date_time_type:
6034 6035 6036 6037 6038
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
6039

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6040 6041 6042
table_alias:
	/* empty */
	| AS
6043
	| EQ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6044 6045 6046 6047

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

6050 6051 6052 6053
opt_all:
	/* empty */
	| ALL
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6054 6055

where_clause:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6056
	/* empty */  { Select->where= 0; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6057 6058 6059 6060 6061
	| WHERE
          {
            Select->parsing_place= IN_WHERE;
          }
          expr
6062
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6063 6064 6065 6066 6067
            SELECT_LEX *select= Select;
	    select->where= $3;
            select->parsing_place= NO_MATTER;
	    if ($3)
	      $3->top_level_item();
6068
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6069
 	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6070 6071 6072

having_clause:
	/* empty */
6073 6074
	| HAVING
	  {
6075
	    Select->parsing_place= IN_HAVING;
6076 6077
          }
	  expr
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6078
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6079
	    SELECT_LEX *sel= Select;
6080
	    sel->having= $3;
6081
	    sel->parsing_place= NO_MATTER;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6082 6083 6084
	    if ($3)
	      $3->top_level_item();
	  }
6085
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6086 6087

opt_escape:
6088 6089 6090 6091 6092
	ESCAPE_SYM simple_expr 
          {
            Lex->escape_used= TRUE;
            $$= $2;
          }
6093
	| /* empty */
6094
          {
6095
            Lex->escape_used= FALSE;
6096 6097 6098
            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
6099 6100
          }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6101 6102 6103


/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6104
   group by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6105 6106 6107 6108
*/

group_clause:
	/* empty */
6109
	| GROUP BY group_list olap_opt;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6110 6111

group_list:
6112
	group_list ',' order_ident order_dir
6113
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) MYSQL_YYABORT; }
6114
	| order_ident order_dir
6115
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) MYSQL_YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6116

6117 6118
olap_opt:
	/* empty */ {}
6119
	| WITH CUBE_SYM
6120
          {
6121
	    LEX *lex=Lex;
6122 6123
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
6124
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
6125
		       "global union parameters");
6126
	      MYSQL_YYABORT;
6127
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6128
	    lex->current_select->olap= CUBE_TYPE;
6129
	    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
6130
	    MYSQL_YYABORT;	/* To be deleted in 5.1 */
6131
	  }
6132
	| WITH ROLLUP_SYM
6133
          {
6134 6135 6136
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
6137
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
6138
		       "global union parameters");
6139
	      MYSQL_YYABORT;
6140
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6141
	    lex->current_select->olap= ROLLUP_TYPE;
6142
	  }
6143
	;
6144

malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163
/*
  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))
6164
              MYSQL_YYABORT;
malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
6165 6166 6167
          }
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6168
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6169
   Order by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6170 6171
*/

6172
opt_order_clause:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6173
	/* empty */
6174
	| order_clause;
6175 6176

order_clause:
6177 6178
	ORDER_SYM BY
        {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6179
	  LEX *lex=Lex;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6180 6181 6182 6183
          SELECT_LEX *sel= lex->current_select;
          SELECT_LEX_UNIT *unit= sel-> master_unit();
	  if (sel->linkage != GLOBAL_OPTIONS_TYPE &&
	      sel->olap != UNSPECIFIED_OLAP_TYPE)
6184
	  {
6185 6186
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
6187
	    MYSQL_YYABORT;
6188
	  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201
          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
6202
                 first_sl->select_limit) &&            
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6203
                unit->add_fake_select_lex(lex->thd))
6204
              MYSQL_YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6205
          }
6206
	} order_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6207 6208 6209

order_list:
	order_list ',' order_ident order_dir
6210
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) MYSQL_YYABORT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6211
	| order_ident order_dir
6212
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) MYSQL_YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6213 6214 6215

order_dir:
	/* empty */ { $$ =  1; }
6216
	| ASC  { $$ =1; }
6217
	| DESC { $$ =0; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6218 6219


6220 6221 6222
opt_limit_clause_init:
	/* empty */
	{
6223 6224
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
6225 6226
          sel->offset_limit= 0;
          sel->select_limit= 0;
6227
	}
6228 6229 6230
	| limit_clause {}
	;

6231 6232 6233 6234 6235
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

6236
limit_clause:
6237
	LIMIT limit_options {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6238
	;
6239 6240

limit_options:
6241
	limit_option
6242
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6243
            SELECT_LEX *sel= Select;
6244
            sel->select_limit= $1;
6245
            sel->offset_limit= 0;
6246
	    sel->explicit_limit= 1;
6247
	  }
6248
	| limit_option ',' limit_option
6249
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6250
	    SELECT_LEX *sel= Select;
6251 6252
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
6253
	    sel->explicit_limit= 1;
6254
	  }
6255
	| limit_option OFFSET_SYM limit_option
6256
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6257
	    SELECT_LEX *sel= Select;
6258 6259
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
6260
	    sel->explicit_limit= 1;
6261 6262
	  }
	;
6263 6264
limit_option:
        param_marker
6265 6266 6267
        {
          ((Item_param *) $1)->set_strict_type(INT_RESULT);
        }
6268 6269 6270
        | 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
6271
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6272 6273 6274 6275

delete_limit_clause:
	/* empty */
	{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6276
	  LEX *lex=Lex;
6277
	  lex->current_select->select_limit= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6278
	}
6279
	| LIMIT limit_option
6280 6281
	{
	  SELECT_LEX *sel= Select;
6282
	  sel->select_limit= $2;
6283 6284
	  sel->explicit_limit= 1;
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6285

serg@serg.mylan's avatar
serg@serg.mylan committed
6286 6287
ulong_num:
          NUM           { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
6288
	| HEX_NUM       { $$= (ulong) strtol($1.str, (char**) 0, 16); }
monty@mysql.com's avatar
monty@mysql.com committed
6289 6290
	| 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
6291
        | DECIMAL_NUM   { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
monty@mysql.com's avatar
monty@mysql.com committed
6292 6293
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6294

6295
ulonglong_num:
monty@mysql.com's avatar
monty@mysql.com committed
6296 6297 6298
	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); }
6299
        | DECIMAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
monty@mysql.com's avatar
monty@mysql.com committed
6300 6301
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6302 6303 6304 6305 6306 6307

procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
6308 6309 6310 6311

            if (! lex->parsing_options.allows_select_procedure)
            {
              my_error(ER_VIEW_SELECT_CLAUSE, MYF(0), "PROCEDURE");
6312
              MYSQL_YYABORT;
6313 6314
            }

6315 6316
	    if (&lex->select_lex != lex->current_select)
	    {
6317
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
6318
	      MYSQL_YYABORT;
6319
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6320 6321 6322
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
6323 6324 6325 6326
	    if (add_proc_to_list(lex->thd, new Item_field(&lex->
                                                          current_select->
                                                          context,
                                                          NULL,NULL,$2.str)))
6327
	      MYSQL_YYABORT;
6328
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6329
	  }
6330
	  '(' procedure_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6331 6332 6333 6334


procedure_list:
	/* empty */ {}
6335
	| procedure_list2 {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6336 6337 6338

procedure_list2:
	procedure_list2 ',' procedure_item
6339
	| procedure_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6340 6341 6342 6343

procedure_item:
	  remember_name expr
	  {
6344 6345 6346 6347
            THD *thd= YYTHD;
            Lex_input_stream *lip= thd->m_lip;

	    if (add_proc_to_list(thd, $2))
6348
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6349
	    if (!$2->name)
6350 6351
	      $2->set_name($1,(uint) ((char*) lip->tok_end - $1),
                           thd->charset());
6352 6353 6354 6355 6356 6357
	  }
          ;


select_var_list_init:
	   {
6358 6359
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
6360
	        MYSQL_YYABORT;
6361
	   }
6362
	   select_var_list
6363
	   {}
6364
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6365

Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6366
select_var_list:
6367 6368 6369 6370
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

6371 6372 6373 6374 6375 6376 6377
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
6378 6379 6380 6381 6382
               /*
                 The parser won't create select_result instance only
	         if it's an EXPLAIN.
               */
               DBUG_ASSERT(lex->describe);
6383 6384
	   }
           | ident_or_text
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6385
           {
6386
             LEX *lex=Lex;
6387
	     sp_variable_t *t;
6388

6389
	     if (!lex->spcont || !(t=lex->spcont->find_variable(&$1)))
6390
	     {
6391
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
6392
	       MYSQL_YYABORT;
6393
	     }
6394 6395
	     if (lex->result)
             {
6396 6397 6398
               my_var *var;
	       ((select_dumpvar *)lex->result)->
                 var_list.push_back(var= new my_var($1,1,t->offset,t->type));
6399
#ifndef DBUG_OFF
6400
	       if (var)
6401
		 var->sp= lex->sphead;
6402
#endif
6403 6404 6405 6406 6407 6408 6409 6410
             }
	     else
	     {
               /*
                 The parser won't create select_result instance only
	         if it's an EXPLAIN.
               */
               DBUG_ASSERT(lex->describe);
6411
	     }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6412
	   }
6413
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6414

6415
into:
6416 6417 6418 6419 6420
        INTO
	{
          if (! Lex->parsing_options.allows_select_into)
          {
            my_error(ER_VIEW_SELECT_CLAUSE, MYF(0), "INTO");
6421
            MYSQL_YYABORT;
6422 6423 6424 6425 6426 6427 6428
          }
	}
        into_destination
        ;

into_destination:
        OUTFILE TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6429
	{
6430
          LEX *lex= Lex;
6431
          lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
6432
          if (!(lex->exchange= new sql_exchange($2.str, 0)) ||
6433
              !(lex->result= new select_export(lex->exchange)))
6434
            MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6435 6436
	}
	opt_field_term opt_line_term
6437
	| DUMPFILE TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6438
	{
6439
	  LEX *lex=Lex;
6440 6441
	  if (!lex->describe)
	  {
6442
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
6443
	    if (!(lex->exchange= new sql_exchange($2.str,1)))
6444
	      MYSQL_YYABORT;
6445
	    if (!(lex->result= new select_dump(lex->exchange)))
6446
	      MYSQL_YYABORT;
6447
	  }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6448
	}
6449
        | select_var_list_init
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6450
	{
6451
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
6452 6453
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6454

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6455 6456 6457
/*
  DO statement
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6458

6459
do:	DO_SYM
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6460 6461 6462
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
6463 6464 6465 6466 6467
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6468
	}
6469 6470
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6471
/*
6472
  Drop : delete tables or index or user
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6473 6474 6475
*/

drop:
serg@serg.mylan's avatar
serg@serg.mylan committed
6476
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6477
	{
6478 6479
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
6480 6481
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6482
	}
6483
	| DROP INDEX_SYM ident ON table_ident {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6484
	  {
6485 6486
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
6487 6488
             lex->alter_info.reset();
             lex->alter_info.flags= ALTER_DROP_INDEX;
6489 6490
	     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
6491 6492
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
6493
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6494 6495 6496
	  }
	| DROP DATABASE if_exists ident
	  {
6497 6498 6499 6500
	    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
6501
	 }
6502
	| DROP FUNCTION_SYM if_exists sp_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6503
	  {
6504
	    LEX *lex=Lex;
6505 6506
	    if (lex->sphead)
	    {
6507
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
6508
	      MYSQL_YYABORT;
6509
	    }
6510
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
6511 6512 6513 6514 6515 6516 6517 6518
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
	  }
	| DROP PROCEDURE if_exists sp_name
	  {
	    LEX *lex=Lex;
	    if (lex->sphead)
	    {
6519
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
6520
	      MYSQL_YYABORT;
6521 6522 6523 6524
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
6525
	  }
6526
	| DROP USER clear_privileges user_list
6527
	  {
6528 6529
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
6530 6531
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
6532
	    LEX *lex= Lex;
6533 6534 6535
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
6536
        | DROP TRIGGER_SYM if_exists sp_name
6537 6538 6539
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_DROP_TRIGGER;
6540 6541
            lex->drop_if_exists= $3;
            lex->spname= $4;
6542 6543
          }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6544 6545

table_list:
6546
	table_name
6547
	| table_list ',' table_name;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6548

6549
table_name:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6550
	table_ident
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6551 6552
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
6553
	    MYSQL_YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6554 6555
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6556

6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570
table_alias_ref_list:
        table_alias_ref
        | table_alias_ref_list ',' table_alias_ref;

table_alias_ref:
	table_ident
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL,
                                         TL_OPTION_UPDATING | TL_OPTION_ALIAS,
                                         Lex->lock_option ))
	    MYSQL_YYABORT;
	}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6571
if_exists:
6572
	/* empty */ { $$= 0; }
6573 6574
	| IF EXISTS { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6575

6576 6577 6578 6579
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6580 6581 6582 6583 6584
/*
** Insert : add new data to table
*/

insert:
6585 6586 6587
	INSERT
	{
	  LEX *lex= Lex;
6588 6589
	  lex->sql_command= SQLCOM_INSERT;
	  lex->duplicates= DUP_ERROR; 
6590
	  mysql_init_select(lex);
6591 6592 6593
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
	} insert_lock_option
6594
	opt_ignore insert2
6595
	{
6596
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6597
	  Lex->current_select= &Lex->select_lex;
6598
	}
6599
	insert_field_spec opt_insert_update
6600
	{}
6601
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6602 6603

replace:
6604 6605
	REPLACE
	{
6606
	  LEX *lex=Lex;
6607 6608
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
6609
	  mysql_init_select(lex);
6610
	}
6611 6612
	replace_lock_option insert2
	{
6613
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6614
	  Lex->current_select= &Lex->select_lex;
6615 6616
	}
	insert_field_spec
6617
	{}
6618
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6619 6620

insert_lock_option:
6621 6622 6623 6624 6625 6626 6627 6628
	/* 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.
            */
6629
            $$= (Lex->sphead ? TL_WRITE_DEFAULT : TL_WRITE_CONCURRENT_INSERT);
6630 6631 6632 6633
#else
            $$= TL_WRITE_CONCURRENT_INSERT;
#endif
          }
6634 6635 6636
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
6637
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6638 6639

replace_lock_option:
6640 6641
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6642 6643 6644

insert2:
	INTO insert_table {}
6645
	| insert_table {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6646 6647

insert_table:
6648
	table_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6649
	{
6650 6651 6652 6653
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
6654
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6655 6656

insert_field_spec:
serg@serg.mylan's avatar
serg@serg.mylan committed
6657 6658 6659
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6660 6661
	| SET
	  {
6662 6663 6664
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
6665
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6666
	   }
6667
	   ident_eq_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6668 6669 6670

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
6671
	| insert_ident		{ Lex->field_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6672 6673 6674

insert_values:
	VALUES	values_list  {}
6675
	| VALUE_SYM values_list  {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6676 6677
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
6678
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6679 6680 6681

values_list:
	values_list ','  no_braces
6682
	| no_braces;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6683 6684 6685 6686

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
6687
	ident_eq_value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6688 6689

ident_eq_value:
6690
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6691
	 {
6692 6693 6694
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
6695
	    MYSQL_YYABORT;
6696
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6697 6698

equal:	EQ		{}
6699 6700 6701 6702 6703 6704 6705
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6706 6707 6708 6709 6710

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
6711
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6712 6713 6714
	 }
	 opt_values ')'
	 {
6715 6716
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
6717
	    MYSQL_YYABORT;
6718
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6719 6720 6721

opt_values:
	/* empty */ {}
6722
	| values;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6723 6724

values:
6725
	values ','  expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6726 6727
	{
	  if (Lex->insert_list->push_back($3))
6728
	    MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6729
	}
6730 6731 6732
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
6733
	      MYSQL_YYABORT;
6734 6735 6736 6737
	  }
	;

expr_or_default:
6738
	expr	  { $$= $1;}
6739
	| DEFAULT {$$= new Item_default_value(Lex->current_context()); }
6740
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6741

6742 6743
opt_insert_update:
        /* empty */
monty@mysql.com's avatar
monty@mysql.com committed
6744
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
6745
          KEY_SYM UPDATE_SYM insert_update_list
6746 6747
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6748 6749 6750
/* Update rows in a table */

update:
6751 6752
	UPDATE_SYM
	{
6753
	  LEX *lex= Lex;
6754
	  mysql_init_select(lex);
6755
          lex->sql_command= SQLCOM_UPDATE;
6756
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
6757
	  lex->duplicates= DUP_ERROR; 
6758
        }
6759
        opt_low_priority opt_ignore join_table_list
6760
	SET update_list
6761
	{
6762 6763
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
6764
            lex->sql_command= SQLCOM_UPDATE_MULTI;
6765 6766 6767
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
6768 6769
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
6770
	    MYSQL_YYABORT;
6771
	  }
6772 6773 6774 6775 6776 6777
          /*
            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);
6778
	}
6779
	where_clause opt_order_clause delete_limit_clause {}
6780
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6781 6782

update_list:
6783 6784 6785 6786
	update_list ',' update_elem
	| update_elem;

update_elem:
monty@mishka.local's avatar
monty@mishka.local committed
6787
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6788
	{
monty@mysql.com's avatar
monty@mysql.com committed
6789
	  if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
6790
	    MYSQL_YYABORT;
6791 6792 6793 6794 6795 6796 6797
	};

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
monty@mishka.local's avatar
monty@mishka.local committed
6798
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6799
	  {
6800 6801 6802
	  LEX *lex= Lex;
	  if (lex->update_list.push_back($1) || 
	      lex->value_list.push_back($3))
6803
	      MYSQL_YYABORT;
6804
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6805 6806

opt_low_priority:
6807
	/* empty */	{ $$= TL_WRITE_DEFAULT; }
6808
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6809 6810 6811 6812

/* Delete rows from a table */

delete:
6813
	DELETE_SYM
6814
	{
6815 6816
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
6817
	  mysql_init_select(lex);
6818
	  lex->lock_option= TL_WRITE_DEFAULT;
6819
	  lex->ignore= 0;
6820
	  lex->select_lex.init_order();
6821
	}
6822 6823
	opt_delete_options single_multi {}
	;
6824 6825

single_multi:
6826 6827
 	FROM table_ident
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6828 6829
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
6830
	    MYSQL_YYABORT;
6831 6832
	}
	where_clause opt_order_clause
6833
	delete_limit_clause {}
6834
	| table_wild_list
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6835
	  { mysql_init_multi_delete(Lex); }
6836
          FROM join_table_list where_clause
6837 6838
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
6839
              MYSQL_YYABORT;
6840
          }
6841
	| FROM table_alias_ref_list
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6842
	  { mysql_init_multi_delete(Lex); }
6843
	  USING join_table_list where_clause
6844 6845
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
6846
              MYSQL_YYABORT;
6847
          }
6848
	;
6849 6850 6851

table_wild_list:
	  table_wild_one {}
6852
	  | table_wild_list ',' table_wild_one {};
6853 6854

table_wild_one:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6855
	ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6856
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6857
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
6858 6859
					 TL_OPTION_UPDATING | 
                                         TL_OPTION_ALIAS, Lex->lock_option))
6860
	    MYSQL_YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6861
        }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6862
	| ident '.' ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6863
	  {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6864 6865
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
6866 6867 6868
					   $5, 
                                           TL_OPTION_UPDATING | 
                                           TL_OPTION_ALIAS,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6869
					   Lex->lock_option))
6870
	      MYSQL_YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6871
	  }
6872
	;
6873 6874

opt_wild:
6875
	/* empty */	{}
6876
	| '.' '*'	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6877 6878


6879
opt_delete_options:
6880
	/* empty */	{}
6881
	| opt_delete_option opt_delete_options {};
6882 6883

opt_delete_option:
6884
	QUICK		{ Select->options|= OPTION_QUICK; }
6885
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
6886
	| IGNORE_SYM	{ Lex->ignore= 1; };
6887

6888
truncate:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
6889
	TRUNCATE_SYM opt_table_sym table_name
6890
	{
6891
	  LEX* lex= Lex;
6892
	  lex->sql_command= SQLCOM_TRUNCATE;
6893
	  lex->select_lex.options= 0;
6894
          lex->select_lex.sql_cache= SELECT_LEX::SQL_CACHE_UNSPECIFIED;
6895
	  lex->select_lex.init_order();
6896 6897
	}
	;
6898

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6899 6900
opt_table_sym:
	/* empty */
6901
	| TABLE_SYM;
6902

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6903 6904
/* Show things */

6905 6906
show:	SHOW
	{
6907 6908
	  LEX *lex=Lex;
	  lex->wild=0;
6909 6910 6911
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
6912 6913
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6914
	show_param
6915 6916
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6917 6918

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

7185 7186 7187 7188
show_engine_param:
	STATUS_SYM
	  {
	    switch (Lex->create_info.db_type) {
7189 7190 7191
	    case DB_TYPE_NDBCLUSTER:
	      Lex->sql_command = SQLCOM_SHOW_NDBCLUSTER_STATUS;
	      break;
7192 7193 7194 7195
	    case DB_TYPE_INNODB:
	      Lex->sql_command = SQLCOM_SHOW_INNODB_STATUS;
	      break;
	    default:
7196
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "STATUS");
7197
	      MYSQL_YYABORT;
7198 7199 7200 7201 7202 7203 7204 7205 7206
	    }
	  }
	| LOGS_SYM
	  {
	    switch (Lex->create_info.db_type) {
	    case DB_TYPE_BERKELEY_DB:
	      Lex->sql_command = SQLCOM_SHOW_LOGS;
	      break;
	    default:
7207
	      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "LOGS");
7208
	      MYSQL_YYABORT;
7209 7210 7211
	    }
	  };

7212 7213 7214 7215
master_or_binary:
	MASTER_SYM
	| BINARY;

7216 7217 7218 7219
opt_storage:
	/* empty */
	| STORAGE_SYM;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7220 7221
opt_db:
	/* empty */  { $$= 0; }
7222
	| from_or_in ident { $$= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7223

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7224 7225
opt_full:
	/* empty */ { Lex->verbose=0; }
7226
	| FULL	    { Lex->verbose=1; };
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7227

7228 7229
from_or_in:
	FROM
7230
	| IN_SYM;
7231

7232 7233
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7234
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
7235 7236 7237

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

7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252
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();
        }
      ;

7253

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7254 7255 7256 7257
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
7258 7259 7260 7261 7262 7263 7264 7265 7266
          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))
7267
	    MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7268
	}
7269
	opt_describe_column {}
7270 7271 7272
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
7273
          {
7274
	    LEX *lex=Lex;
7275
	    lex->select_lex.options|= SELECT_DESCRIBE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7276 7277
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7278 7279 7280

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

7283 7284 7285 7286 7287
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7288 7289 7290
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
7291
	| ident
7292
	  { 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
7293 7294 7295 7296 7297


/* flush things */

flush:
7298
	FLUSH_SYM opt_no_write_to_binlog
7299 7300
	{
	  LEX *lex=Lex;
7301 7302
	  lex->sql_command= SQLCOM_FLUSH;
          lex->type= 0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7303
          lex->no_write_to_binlog= $2;
7304
	}
7305 7306 7307
	flush_options
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7308 7309 7310

flush_options:
	flush_options ',' flush_option
7311
	| flush_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7312 7313

flush_option:
7314
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7315
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7316
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7317 7318 7319 7320
	| 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
7321 7322
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
7323 7324
	| 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
7325

7326
opt_table_list:
7327 7328
	/* empty */  {;}
	| table_list {;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7329

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7330
reset:
7331 7332 7333 7334
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
7335 7336 7337 7338
	} reset_options
	{}
	;

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7339 7340
reset_options:
	reset_options ',' reset_option
7341
	| reset_option;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7342 7343

reset_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7344 7345
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
7346
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7347

7348
purge:
7349 7350 7351 7352
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
7353
          lex->sql_command = SQLCOM_PURGE;
7354 7355 7356 7357 7358
	} purge_options
	{}
	;

purge_options:
7359
	master_or_binary LOGS_SYM purge_option
7360
	;
7361 7362

purge_option:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7363
        TO_SYM TEXT_STRING_sys
7364 7365 7366 7367 7368
        {
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
7369 7370 7371 7372
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
7373
	}
7374
	;
7375

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7376 7377 7378
/* kill threads */

kill:
serg@serg.mylan's avatar
serg@serg.mylan committed
7379
	KILL_SYM { Lex->sql_command= SQLCOM_KILL; } kill_option expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7380
	{
7381
	  LEX *lex=Lex;
7382
	  lex->value_list.empty();
serg@serg.mylan's avatar
serg@serg.mylan committed
7383
	  lex->value_list.push_front($4);
7384
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7385

7386 7387 7388
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
serg@serg.mylan's avatar
serg@serg.mylan committed
7389 7390
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; }
        ;
7391

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7392 7393 7394
/* change database */

use:	USE_SYM ident
7395 7396
	{
	  LEX *lex=Lex;
7397 7398
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
7399
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7400 7401 7402

/* import, export of files */

serg@serg.mylan's avatar
serg@serg.mylan committed
7403
load:   LOAD DATA_SYM
7404
        {
7405 7406 7407 7408
          THD *thd= YYTHD;
          LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;

7409 7410 7411
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD DATA");
7412
	    MYSQL_YYABORT;
7413
	  }
7414
          lex->fname_start= lip->ptr;
7415 7416 7417 7418 7419 7420
        }
        load_data
        {}
        |
        LOAD TABLE_SYM table_ident FROM MASTER_SYM
        {
7421 7422 7423 7424
	  LEX *lex=Lex;
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD TABLE");
7425
	    MYSQL_YYABORT;
7426 7427
	  }
          lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
7428
          WARN_DEPRECATED("LOAD TABLE FROM MASTER",
7429 7430
                          "mysqldump or future "
                          "BACKUP/RESTORE DATABASE facility");
7431
          if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
7432
            MYSQL_YYABORT;
7433 7434 7435
        };

load_data:
bar@mysql.com's avatar
bar@mysql.com committed
7436
	load_data_lock opt_local INFILE TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7437
	{
7438 7439
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
7440 7441
	  lex->lock_option= $1;
	  lex->local_file=  $2;
7442 7443
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
7444
	  if (!(lex->exchange= new sql_exchange($4.str, 0)))
7445
	    MYSQL_YYABORT;
7446 7447 7448
        }
        opt_duplicate INTO
        {
7449 7450 7451 7452
          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
7453
	}
7454
        TABLE_SYM table_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7455
        {
7456 7457 7458
          LEX *lex=Lex;
          if (!Select->add_table_to_list(YYTHD, $10, NULL, TL_OPTION_UPDATING,
                                         lex->lock_option))
7459
            MYSQL_YYABORT;
7460 7461 7462
          lex->field_list.empty();
          lex->update_list.empty();
          lex->value_list.empty();
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7463
        }
7464 7465
        opt_load_data_charset
	{ Lex->exchange->cs= $12; }
7466 7467 7468
        opt_field_term opt_line_term opt_ignore_lines opt_field_or_var_spec
        opt_load_data_set_spec
        {}
7469
        |
7470
	FROM MASTER_SYM
7471 7472
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
7473
          WARN_DEPRECATED("LOAD DATA FROM MASTER",
7474 7475
                          "mysqldump or future "
                          "BACKUP/RESTORE DATABASE facility");
7476
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7477 7478 7479

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

7482
load_data_lock:
7483
	/* empty */	{ $$= TL_WRITE_DEFAULT; }
7484 7485 7486 7487 7488 7489 7490
	| CONCURRENT
          {
#ifdef HAVE_QUERY_CACHE
            /*
              Ignore this option in SP to avoid problem with query cache
            */
            if (Lex->sphead != 0)
7491
              $$= TL_WRITE_DEFAULT;
7492
            else
7493 7494 7495
#endif
              $$= TL_WRITE_CONCURRENT_INSERT;
          }
7496
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
7497 7498


bk@work.mysql.com's avatar
bk@work.mysql.com committed
7499 7500 7501
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
7502
	| IGNORE_SYM	{ Lex->ignore= 1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7503 7504 7505

opt_field_term:
	/* empty */
7506
	| COLUMNS field_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7507 7508 7509

field_term_list:
	field_term_list field_term
7510
	| field_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7511 7512

field_term:
7513 7514
	TERMINATED BY text_string 
          {
7515
            DBUG_ASSERT(Lex->exchange != 0);
7516 7517
            Lex->exchange->field_term= $3;
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7518
	| OPTIONALLY ENCLOSED BY text_string
7519
	  {
7520
            LEX *lex= Lex;
7521
            DBUG_ASSERT(lex->exchange != 0);
7522 7523
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
7524
	  }
7525
        | ENCLOSED BY text_string
7526
          {
7527
            DBUG_ASSERT(Lex->exchange != 0);
7528 7529
            Lex->exchange->enclosed= $3;
          }
7530
        | ESCAPED BY text_string
7531
          {
7532
            DBUG_ASSERT(Lex->exchange != 0);
7533 7534
            Lex->exchange->escaped= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7535 7536 7537

opt_line_term:
	/* empty */
7538
	| LINES line_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7539 7540 7541

line_term_list:
	line_term_list line_term
7542
	| line_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7543 7544

line_term:
7545
        TERMINATED BY text_string
7546
          {
7547
            DBUG_ASSERT(Lex->exchange != 0);
7548 7549
            Lex->exchange->line_term= $3;
          }
7550
        | STARTING BY text_string
7551
          {
7552
            DBUG_ASSERT(Lex->exchange != 0);
7553 7554
            Lex->exchange->line_start= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7555 7556 7557

opt_ignore_lines:
	/* empty */
7558 7559
        | IGNORE_SYM NUM LINES
          {
7560
            DBUG_ASSERT(Lex->exchange != 0);
7561 7562
            Lex->exchange->skip_lines= atol($2.str);
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7563

7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574
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
7575

7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586
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
7587 7588 7589
/* Common definitions */

text_literal:
7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637
        TEXT_STRING
        {
          LEX_STRING tmp;
          THD *thd= YYTHD;
          CHARSET_INFO *cs_con= thd->variables.collation_connection;
          CHARSET_INFO *cs_cli= thd->variables.character_set_client;
          uint repertoire= thd->lex->text_string_is_7bit &&
                             my_charset_is_ascii_based(cs_cli) ?
                           MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30;
          if (thd->charset_is_collation_connection ||
              (repertoire == MY_REPERTOIRE_ASCII &&
               my_charset_is_ascii_based(cs_con)))
            tmp= $1;
          else
            thd->convert_string(&tmp, cs_con, $1.str, $1.length, cs_cli);
          $$= new Item_string(tmp.str, tmp.length, cs_con,
                              DERIVATION_COERCIBLE, repertoire);
        }
        | NCHAR_STRING
        {
          uint repertoire= Lex->text_string_is_7bit ?
                           MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30;
          DBUG_ASSERT(my_charset_is_ascii_based(national_charset_info));
          $$= new Item_string($1.str, $1.length, national_charset_info,
                              DERIVATION_COERCIBLE, repertoire);
        }
        | UNDERSCORE_CHARSET TEXT_STRING
          {
            $$= new Item_string($2.str, $2.length, Lex->underscore_charset);
            ((Item_string*) $$)->set_repertoire_from_value();
          }
        | text_literal TEXT_STRING_literal
          {
            Item_string* item= (Item_string*) $1;
            item->append($2.str, $2.length);
            if (!(item->collation.repertoire & MY_REPERTOIRE_EXTENDED))
            {
              /*
                 If the string has been pure ASCII so far,
                 check the new part.
              */
              CHARSET_INFO *cs= YYTHD->variables.collation_connection;
              item->collation.repertoire|= my_string_repertoire(cs,
                                                                $2.str,
                                                                $2.length);
            }
          }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7638 7639

text_string:
7640
	TEXT_STRING_literal
7641
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7642 7643
	| HEX_NUM
	  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
7644
	    Item *tmp= new Item_hex_string($1.str, $1.length);
7645
	    /*
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
7646
	      it is OK only emulate fix_fields, because we need only
7647 7648 7649
              value of constant
	    */
	    $$= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7650
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
7651
	      (String*) 0;
7652
	  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
7653 7654 7655 7656 7657 7658 7659 7660 7661 7662
        | 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;
          }
7663 7664
	;

7665
param_marker:
7666
        PARAM_MARKER
7667
        {
7668
          THD *thd= YYTHD;
7669
	  LEX *lex= thd->lex;
7670
          Lex_input_stream *lip= thd->m_lip;
7671 7672 7673 7674
          Item_param *item;
          if (! lex->parsing_options.allows_variable)
          {
            my_error(ER_VIEW_SELECT_VARIABLE, MYF(0));
7675
            MYSQL_YYABORT;
7676
          }
7677
          item= new Item_param((uint) (lip->tok_start - thd->query));
7678
          if (!($$= item) || lex->param_list.push_back(item))
7679
          {
7680
            my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
7681
            MYSQL_YYABORT;
7682
          }
7683 7684 7685
        }
	;

7686 7687 7688
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
7689 7690
	| '-' NUM_literal
	  {
7691
	    $2->max_length++;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7692
	    $$= $2->neg();
7693
	  }
7694 7695 7696
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
7697 7698
literal:
	text_literal	{ $$ =	$1; }
7699
	| NUM_literal	{ $$ = $1; }
7700 7701 7702 7703 7704
	| NULL_SYM
          {
            $$ = new Item_null();
            YYTHD->m_lip->next_state=MY_LEX_OPERATOR_OR_IDENT;
          }
7705 7706
	| 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
7707 7708
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
7709 7710 7711 7712 7713
        | UNDERSCORE_CHARSET HEX_NUM
          {
            Item *tmp= new Item_hex_string($2.str, $2.length);
            /*
              it is OK only emulate fix_fieds, because we need only
7714
              value of constant
7715 7716 7717 7718 7719 7720 7721 7722 7723 7724
            */
            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->underscore_charset);
            if ($$)
              ((Item_string *) $$)->set_repertoire_from_value();
          }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738
	| 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
7739 7740
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
7741
	| TIMESTAMP text_literal { $$ = $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7742

7743
NUM_literal:
7744 7745
	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); }
7746
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
7747
        | DECIMAL_NUM
7748
	{
7749
           $$= new Item_decimal($1.str, $1.length, YYTHD->charset());
7750 7751
	   if (YYTHD->net.report_error)
	   {
7752
	     MYSQL_YYABORT;
7753 7754 7755 7756 7757 7758 7759
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
7760
	     MYSQL_YYABORT;
7761 7762
	   }
	}
7763
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
7764

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7765
/**********************************************************************
serg@serg.mylan's avatar
serg@serg.mylan committed
7766
** Creating different items.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7767 7768 7769
**********************************************************************/

insert_ident:
7770
	simple_ident_nospvar { $$=$1; }
7771
	| table_wild	 { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7772 7773

table_wild:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7774
	ident '.' '*'
7775
	{
7776
          SELECT_LEX *sel= Select;
7777
	  $$ = new Item_field(Lex->current_context(), NullS, $1.str, "*");
7778
	  sel->with_wild++;
7779
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7780
	| ident '.' ident '.' '*'
7781
	{
7782
          SELECT_LEX *sel= Select;
7783
	  $$ = new Item_field(Lex->current_context(), (YYTHD->client_capabilities &
serg@serg.mylan's avatar
serg@serg.mylan committed
7784 7785
                             CLIENT_NO_SCHEMA ? NullS : $1.str),
                             $3.str,"*");
7786
	  sel->with_wild++;
7787 7788
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7789 7790

order_ident:
7791
	expr { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7792 7793

simple_ident:
7794 7795
	ident
	{
7796 7797 7798
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;
7799
	  sp_variable_t *spv;
7800
          sp_pcontext *spc = lex->spcont;
7801
	  if (spc && (spv = spc->find_variable(&$1)))
7802 7803
	  {
            /* We're compiling a stored procedure and found a variable */
7804 7805 7806
            if (! lex->parsing_options.allows_variable)
            {
              my_error(ER_VIEW_SELECT_VARIABLE, MYF(0));
7807
              MYSQL_YYABORT;
7808 7809
            }

7810
            Item_splocal *splocal;
7811
            splocal= new Item_splocal($1, spv->offset, spv->type,
7812
                                      lip->tok_start_prev - 
gshchepa/uchum@gleb.loc's avatar
gshchepa/uchum@gleb.loc committed
7813 7814
                                      lex->sphead->m_tmp_query,
                                      lip->tok_end - lip->tok_start_prev);
7815
#ifndef DBUG_OFF
7816
            if (splocal)
7817
              splocal->m_sp= lex->sphead;
7818
#endif
7819
	    $$ = (Item*) splocal;
7820 7821 7822 7823 7824 7825 7826
	    lex->safe_to_cache_query=0;
	  }
	  else
	  {
	    SELECT_LEX *sel=Select;
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
7827 7828
                 (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	         (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
7829 7830 7831 7832 7833 7834
	  }
        }
        | simple_ident_q { $$= $1; }
	;

simple_ident_nospvar:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7835
	ident
7836
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7837
	  SELECT_LEX *sel=Select;
7838
	  $$= (sel->parsing_place != IN_HAVING ||
7839
	       sel->get_in_sum_expr() > 0) ?
7840 7841
              (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	      (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
7842
	}
7843
	| simple_ident_q { $$= $1; }
serg@serg.mylan's avatar
serg@serg.mylan committed
7844
	;
7845 7846 7847

simple_ident_q:
	ident '.' ident
7848
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7849
	  THD *thd= YYTHD;
7850
	  LEX *lex= thd->lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
7851

7852 7853 7854 7855 7856 7857
          /*
            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
7858
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") ||
7859 7860
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
7861
            Item_trigger_field *trg_fld;
7862
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
serg@serg.mylan's avatar
serg@serg.mylan committed
7863

7864 7865 7866
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
7867
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
7868
              MYSQL_YYABORT;
7869
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
7870

7871 7872 7873
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
7874
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
7875
              MYSQL_YYABORT;
7876
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
7877

7878 7879 7880 7881 7882
            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);
7883
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
7884
                                                  new_row ?
7885 7886
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
7887
                                                  $3.str,
7888 7889
                                                  SELECT_ACL,
                                                  read_only)))
7890
              MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
7891

7892 7893 7894 7895 7896 7897
            /*
              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
7898

7899 7900 7901 7902 7903 7904 7905
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
7906 7907
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
7908 7909 7910
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
7911 7912
	        (Item*) new Item_field(Lex->current_context(), NullS, $1.str, $3.str) :
	        (Item*) new Item_ref(Lex->current_context(), NullS, $1.str, $3.str);
7913 7914
          }
        }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7915
	| '.' ident '.' ident
7916
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7917
	  THD *thd= YYTHD;
7918
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7919
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7920 7921
	  if (sel->no_table_names_allowed)
	  {
7922 7923
	    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
7924
	  }
7925
	  $$= (sel->parsing_place != IN_HAVING ||
7926
	       sel->get_in_sum_expr() > 0) ?
7927 7928
	      (Item*) new Item_field(Lex->current_context(), NullS, $2.str, $4.str) :
              (Item*) new Item_ref(Lex->current_context(), NullS, $2.str, $4.str);
7929
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7930
	| ident '.' ident '.' ident
7931
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7932
	  THD *thd= YYTHD;
7933
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7934
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7935 7936
	  if (sel->no_table_names_allowed)
	  {
7937 7938
	    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
7939
	  }
7940
	  $$= (sel->parsing_place != IN_HAVING ||
7941
	       sel->get_in_sum_expr() > 0) ?
7942
	      (Item*) new Item_field(Lex->current_context(),
7943
                                     (YYTHD->client_capabilities &
7944 7945
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
7946
	      (Item*) new Item_ref(Lex->current_context(),
7947
                                   (YYTHD->client_capabilities &
7948
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
7949
                                   $3.str, $5.str);
7950
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7951 7952 7953 7954


field_ident:
	ident			{ $$=$1;}
7955 7956 7957 7958 7959
	| ident '.' ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->db))
            {
7960
              my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
7961
              MYSQL_YYABORT;
7962
            }
7963 7964
            if (my_strcasecmp(table_alias_charset, $3.str,
                              table->table_name))
7965
            {
7966
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3.str);
7967
              MYSQL_YYABORT;
7968 7969 7970 7971 7972 7973 7974 7975
            }
            $$=$5;
          }
	| ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->alias))
            {
7976
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $1.str);
7977
              MYSQL_YYABORT;
7978 7979 7980
            }
            $$=$3;
          }
7981
	| '.' ident		{ $$=$2;}	/* For Delphi */;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7982 7983 7984

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

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

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7993
IDENT_sys:
7994 7995 7996 7997 7998
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
7999 8000
            {
              CHARSET_INFO *cs= system_charset_info;
8001
              int dummy_error;
8002 8003
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
8004
                                                   $1.length, &dummy_error);
8005 8006
              if (wlen < $1.length)
              {
8007 8008
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
8009
                MYSQL_YYABORT;
8010
              }
8011
	      $$= $1;
8012
            }
8013 8014 8015 8016
	    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
8017 8018 8019 8020 8021 8022
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8023 8024
	  if (thd->charset_is_system_charset)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8025
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8026 8027
	    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
8028 8029 8030
	}
	;

8031
TEXT_STRING_literal:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8032 8033 8034
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8035 8036
	  if (thd->charset_is_collation_connection)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8037
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8038 8039
	    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
8040 8041 8042 8043
	}
	;


bar@mysql.com's avatar
bar@mysql.com committed
8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055
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
8056
ident:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8057
	IDENT_sys	    { $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8058 8059
	| keyword
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8060 8061 8062
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
8063 8064
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8065

8066 8067 8068 8069 8070 8071 8072 8073 8074 8075
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
8076
ident_or_text:
serg@serg.mylan's avatar
serg@serg.mylan committed
8077
        ident                   { $$=$1;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8078 8079
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8080 8081 8082 8083

user:
	ident_or_text
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8084 8085
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
8086
	    MYSQL_YYABORT;
8087 8088 8089
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
8090

8091
	  if (check_string_length(&$$->user,
gluh@mysql.com/gluh.(none)'s avatar
gluh@mysql.com/gluh.(none) committed
8092
                                  ER(ER_USERNAME), USERNAME_LENGTH))
8093
	    MYSQL_YYABORT;
8094
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8095 8096
	| ident_or_text '@' ident_or_text
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8097 8098
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
8099
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8100
	    $$->user = $1; $$->host=$3;
8101

8102
	    if (check_string_length(&$$->user,
gluh@mysql.com/gluh.(none)'s avatar
gluh@mysql.com/gluh.(none) committed
8103
                                    ER(ER_USERNAME), USERNAME_LENGTH) ||
8104
	        check_string_length(&$$->host,
gluh@mysql.com/gluh.(none)'s avatar
gluh@mysql.com/gluh.(none) committed
8105
                                    ER(ER_HOSTNAME), HOSTNAME_LENGTH))
8106
	      MYSQL_YYABORT;
8107
	  }
8108
	| CURRENT_USER optional_braces
8109
	{
8110
          if (!($$=(LEX_USER*) YYTHD->alloc(sizeof(st_lex_user))))
8111
            MYSQL_YYABORT;
8112 8113 8114 8115 8116 8117
          /* 
            empty LEX_USER means current_user and 
            will be handled in the  get_current_user() function
            later
          */
          bzero($$, sizeof(LEX_USER));
8118
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8119

8120
/* Keyword that we allow for identifiers (except SP labels) */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8121
keyword:
8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157
	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                {}
8158
        | UPGRADE_SYM           {}
8159 8160 8161 8162
	;

/*
 * Keywords that we allow for labels in SPs.
pem@mysql.com's avatar
pem@mysql.com committed
8163 8164 8165
 * Anything that's the beginning of a statement or characteristics
 * must be in keyword above, otherwise we get (harmful) shift/reduce
 * conflicts.
8166 8167
 */
keyword_sp:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8168
	ACTION			{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
8169
	| ADDDATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8170
	| AFTER_SYM		{}
8171
	| AGAINST		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8172
	| AGGREGATE_SYM		{}
8173
	| ALGORITHM_SYM		{}
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8174
	| ANY_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8175
	| AUTO_INC		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8176 8177
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
8178
	| BERKELEY_DB_SYM	{}
8179
	| BINLOG_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8180 8181
	| BIT_SYM		{}
	| BOOL_SYM		{}
8182
	| BOOLEAN_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8183
	| BTREE_SYM		{}
8184
	| CASCADED              {}
8185
	| CHAIN_SYM		{}
8186
	| CHANGED		{}
8187
	| CIPHER_SYM		{}
8188
	| CLIENT_SYM		{}
pem@mysql.com's avatar
pem@mysql.com committed
8189
        | CODE_SYM              {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8190
	| COLLATION_SYM		{}
8191
        | COLUMNS               {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8192
	| COMMITTED_SYM		{}
8193
	| COMPACT_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8194
	| COMPRESSED_SYM	{}
8195
	| CONCURRENT		{}
antony@ppcg5.local's avatar
antony@ppcg5.local committed
8196
	| CONNECTION_SYM	{}
8197
	| CONSISTENT_SYM	{}
8198
	| CUBE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8199 8200 8201 8202
	| DATA_SYM		{}
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
8203
	| DEFINER_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8204
	| DELAY_KEY_WRITE_SYM	{}
8205 8206
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
8207
	| DISCARD		{}
8208 8209
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8210 8211
	| DYNAMIC_SYM		{}
	| ENUM			{}
8212
	| ENGINE_SYM		{}
8213
	| ENGINES_SYM		{}
8214
	| ERRORS		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8215
	| ESCAPE_SYM		{}
8216
	| EVENTS_SYM		{}
8217
        | EXPANSION_SYM         {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8218
	| EXTENDED_SYM		{}
8219
	| FAST_SYM		{}
8220
	| FOUND_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8221 8222
	| DISABLE_SYM		{}
	| ENABLE_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8223
	| FULL			{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8224 8225 8226
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
8227
	| FRAC_SECOND_SYM	{}
8228
	| GEOMETRY_SYM		{}
8229
	| GEOMETRYCOLLECTION	{}
8230
	| GET_FORMAT		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8231
	| GRANTS		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8232
	| GLOBAL_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8233
	| HASH_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8234 8235 8236
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
8237
	| INVOKER_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
8238
	| IMPORT		{}
8239
	| INDEXES		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8240
	| ISOLATION		{}
8241
	| ISSUER_SYM		{}
8242
	| INNOBASE_SYM		{}
8243
	| INSERT_METHOD		{}
8244
	| RELAY_THREAD		{}
8245
	| LAST_SYM		{}
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
8246
	| LEAVES                {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8247
	| LEVEL_SYM		{}
8248
	| LINESTRING		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8249
	| LOCAL_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8250
	| LOCKS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8251 8252 8253 8254 8255 8256 8257 8258 8259
	| 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	{}
8260
	| MASTER_SERVER_ID_SYM  {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8261
	| MASTER_CONNECT_RETRY_SYM	{}
8262 8263 8264 8265 8266 8267
	| 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
8268 8269 8270
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
	| MAX_UPDATES_PER_HOUR	{}
8271
	| MAX_USER_CONNECTIONS_SYM {}
8272
	| MEDIUM_SYM		{}
8273
	| MERGE_SYM		{}
8274
	| MICROSECOND_SYM	{}
8275
        | MIGRATE_SYM           {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8276 8277 8278
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8279
	| MODE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8280
	| MONTH_SYM		{}
8281 8282 8283
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
8284
        | MUTEX_SYM             {}
8285
	| NAME_SYM              {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8286
	| NAMES_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8287 8288
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
8289
	| NDBCLUSTER_SYM	{}
8290
	| NEXT_SYM		{}
8291
	| NEW_SYM		{}
8292
	| NONE_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8293
	| NVARCHAR_SYM		{}
8294
	| OFFSET_SYM		{}
8295
	| OLD_PASSWORD		{}
8296
	| ONE_SHOT_SYM		{}
8297
        | ONE_SYM               {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8298
	| PACK_KEYS_SYM		{}
8299
	| PARTIAL		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8300
	| PASSWORD		{}
8301
        | PHASE_SYM             {}
8302
	| POINT_SYM		{}
8303
	| POLYGON		{}
8304
	| PREV_SYM		{}
8305
        | PRIVILEGES            {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8306 8307
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
8308
	| QUARTER_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8309
	| QUERY_SYM		{}
8310
	| QUICK			{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8311
	| RAID_0_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8312 8313
	| RAID_CHUNKS		{}
	| RAID_CHUNKSIZE	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8314
	| RAID_STRIPED_SYM	{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8315
	| RAID_TYPE		{}
8316
        | RECOVER_SYM           {}
serg@serg.mylan's avatar
serg@serg.mylan committed
8317
        | REDUNDANT_SYM         {}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8318 8319
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8320
	| RELOAD		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8321
	| REPEATABLE_SYM	{}
8322
	| REPLICATION		{}
8323
	| RESOURCES		{}
8324
        | RESUME_SYM            {}
8325
	| RETURNS_SYM           {}
8326
	| ROLLUP_SYM		{}
8327
	| ROUTINE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8328 8329 8330
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8331
	| RTREE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8332
	| SECOND_SYM		{}
8333
	| SERIAL_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8334 8335
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
8336
	| SIMPLE_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8337
	| SHARE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8338
	| SHUTDOWN		{}
8339
	| SNAPSHOT_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8340
	| SOUNDS_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8341
	| SQL_CACHE_SYM		{}
8342
	| SQL_BUFFER_RESULT	{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8343
	| SQL_NO_CACHE_SYM	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8344
	| SQL_THREAD		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8345
	| STATUS_SYM		{}
8346
	| STORAGE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8347
	| STRING_SYM		{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
8348
	| SUBDATE_SYM		{}
8349
	| SUBJECT_SYM		{}
8350
	| SUPER_SYM		{}
8351
        | SUSPEND_SYM           {}
8352
        | TABLES                {}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
8353
	| TABLESPACE		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8354
	| TEMPORARY		{}
8355
	| TEMPTABLE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8356
	| TEXT_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8357
	| TRANSACTION_SYM	{}
8358
	| TRIGGERS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8359
	| TIMESTAMP		{}
8360 8361
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8362
	| TIME_SYM		{}
8363
	| TYPES_SYM		{}
8364
        | TYPE_SYM              {}
serg@serg.mylan's avatar
serg@serg.mylan committed
8365
        | UDF_RETURNS_SYM       {}
8366
	| FUNCTION_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8367
	| UNCOMMITTED_SYM	{}
8368
	| UNDEFINED_SYM		{}
8369
	| UNKNOWN_SYM		{}
8370
	| UNTIL_SYM		{}
8371
	| USER			{}
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8372
	| USE_FRM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8373
	| VARIABLES		{}
8374
	| VIEW_SYM		{}
8375
	| VALUE_SYM		{}
8376
	| WARNINGS		{}
8377
	| WEEK_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8378
	| WORK_SYM		{}
8379
	| X509_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8380 8381
	| YEAR_SYM		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8382 8383 8384 8385 8386 8387

/* Option functions */

set:
	SET opt_option
	{
8388 8389
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
8390
	  mysql_init_select(lex);
8391
	  lex->option_type=OPT_SESSION;
8392
	  lex->var_list.empty();
8393
          lex->one_shot_set= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8394
	}
8395 8396 8397
	option_value_list
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8398 8399 8400

opt_option:
	/* empty */ {}
8401
	| OPTION {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8402 8403

option_value_list:
8404 8405 8406 8407 8408
	option_type_value
	| option_value_list ',' option_type_value;

option_type_value:
        {
8409 8410 8411 8412 8413
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;

          if (lex->sphead)
8414 8415 8416 8417
          {
            /*
              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
8418 8419
              and sp_instr_set_trigger instructions share one LEX.
              (Well, it is theoretically possible but adds some extra
8420 8421 8422 8423 8424
               overhead on preparation for execution stage and IMO less
               robust).

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

8428 8429 8430 8431 8432 8433
            /* 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;
8434
	    lex->sphead->m_tmp_query= lip->tok_start;
8435 8436
          }
        }
8437
	ext_option_value
8438
        {
8439 8440 8441
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;
serg@serg.mylan's avatar
serg@serg.mylan committed
8442

8443 8444 8445
          if (lex->sphead)
          {
            sp_head *sp= lex->sphead;
serg@serg.mylan's avatar
serg@serg.mylan committed
8446

8447 8448 8449 8450 8451 8452 8453 8454 8455
	    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
8456

8457 8458
              if (!(i= new sp_instr_stmt(sp->instructions(), lex->spcont,
                                         lex)))
8459
                MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
8460

8461 8462
              /*
                Extract the query statement from the tokenizer.  The
8463 8464
                end is either lip->ptr, if there was no lookahead,
                lip->tok_end otherwise.
8465 8466
              */
              if (yychar == YYEMPTY)
8467
                qbuff.length= lip->ptr - sp->m_tmp_query;
8468
              else
8469
                qbuff.length= lip->tok_end - sp->m_tmp_query;
serg@serg.mylan's avatar
serg@serg.mylan committed
8470

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

8474
              strmake(strmake(qbuff.str, "SET ", 4), sp->m_tmp_query,
8475 8476 8477 8478 8479
                      qbuff.length);
              qbuff.length+= 4;
              i->m_query= qbuff;
              sp->add_instr(i);
            }
8480
            lex->sphead->restore_lex(thd);
8481 8482
          }
        };
8483 8484

option_type:
8485 8486 8487 8488 8489 8490 8491 8492 8493
        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; }
8494 8495 8496 8497
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8498
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
8499 8500 8501 8502 8503 8504
	| 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
8505
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
8506 8507 8508
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8509

8510 8511 8512
ext_option_value:
        sys_option_value
        | option_type2 option_value;
8513

8514 8515 8516 8517
sys_option_value:
        option_type internal_variable_name equal set_expr_or_default
        {
          LEX *lex=Lex;
8518

8519 8520 8521 8522
          if ($2.var == &trg_new_row_fake_var)
          {
            /* We are in trigger and assigning value to field of new row */
            Item *it;
8523
            Item_trigger_field *trg_fld;
8524 8525
            sp_instr_set_trigger_field *sp_fld;
	    LINT_INIT(sp_fld);
8526 8527
            if ($1)
            {
8528 8529
              my_parse_error(ER(ER_SYNTAX_ERROR));
              MYSQL_YYABORT;
8530
            }
8531 8532
            if ($4)
              it= $4;
8533
            else
8534 8535 8536 8537
            {
              /* QQ: Shouldn't this be field's default value ? */
              it= new Item_null();
            }
8538

8539 8540 8541
            DBUG_ASSERT(lex->trg_chistics.action_time == TRG_ACTION_BEFORE &&
                        (lex->trg_chistics.event == TRG_EVENT_INSERT ||
                         lex->trg_chistics.event == TRG_EVENT_UPDATE));
8542
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
8543
                                                  Item_trigger_field::NEW_ROW,
8544
                                                  $2.base_name.str,
8545
                                                  UPDATE_ACL, FALSE)) ||
8546 8547 8548 8549 8550
                !(sp_fld= new sp_instr_set_trigger_field(lex->sphead->
                          	                         instructions(),
                                	                 lex->spcont,
							 trg_fld,
                                        	         it, lex)))
8551
              MYSQL_YYABORT;
8552

8553 8554 8555 8556
            /*
              Let us add this item to list of all Item_trigger_field
              objects in trigger.
            */
8557 8558
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
                                    (byte **)&trg_fld->next_trg_field);
8559

8560
            lex->sphead->add_instr(sp_fld);
8561 8562 8563 8564
          }
          else if ($2.var)
          { /* System variable */
            if ($1)
8565
              lex->option_type= $1;
8566 8567 8568 8569 8570 8571 8572
            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;
8573
            sp_variable_t *spv;
8574
            sp_instr_set *sp_set;
8575 8576 8577
            Item *it;
            if ($1)
            {
8578 8579
              my_parse_error(ER(ER_SYNTAX_ERROR));
              MYSQL_YYABORT;
8580 8581
            }

8582
            spv= ctx->find_variable(&$2.base_name);
8583 8584 8585 8586 8587 8588 8589

            if ($4)
              it= $4;
            else if (spv->dflt)
              it= spv->dflt;
            else
              it= new Item_null();
8590 8591 8592
            sp_set= new sp_instr_set(lex->sphead->instructions(), ctx,
                                     spv->offset, it, spv->type, lex, TRUE);
            lex->sphead->add_instr(sp_set);
8593 8594 8595 8596 8597
          }
        }
        | option_type TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	{
	  LEX *lex=Lex;
8598
          if ($1)
8599
            lex->option_type= $1;
8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611
	  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)));
	}
8612
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
8613 8614
	  {
	    LEX *lex=Lex;
8615
	    lex->var_list.push_back(new set_var($3, $4.var, &$4.base_name, $6));
8616
	  }
8617
	| charset old_or_new_charset_name_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8618
	{
8619
	  THD *thd= YYTHD;
8620
	  LEX *lex= Lex;
8621
	  $2= $2 ? $2: global_system_variables.character_set_client;
8622
	  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
8623
	}
8624 8625 8626 8627 8628 8629 8630 8631
        | NAMES_SYM equal expr
	  {
	    LEX *lex= Lex;
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING names;

	    names.str= (char *)"names";
	    names.length= 5;
8632
	    if (spc && spc->find_variable(&names))
8633
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), names.str);
8634
            else
8635
              my_parse_error(ER(ER_SYNTAX_ERROR));
8636

8637
	    MYSQL_YYABORT;
8638
	  }
8639
	| NAMES_SYM charset_name_or_default opt_collate
8640
	{
8641
	  LEX *lex= Lex;
8642
	  $2= $2 ? $2 : global_system_variables.character_set_client;
8643 8644
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
8645
	  {
8646 8647
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
8648
	    MYSQL_YYABORT;
8649
	  }
8650
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
8651
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8652
	| PASSWORD equal text_or_password
8653
	  {
8654
	    THD *thd=YYTHD;
8655
	    LEX_USER *user;
8656 8657 8658 8659 8660 8661
	    LEX *lex= Lex;	    
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING pw;

	    pw.str= (char *)"password";
	    pw.length= 8;
8662
	    if (spc && spc->find_variable(&pw))
8663 8664
	    {
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), pw.str);
8665
	      MYSQL_YYABORT;
8666
	    }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8667
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
8668
	      MYSQL_YYABORT;
8669
	    user->host=null_lex_str;
8670
	    user->user.str=thd->security_ctx->priv_user;
8671
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
8672 8673
	  }
	| PASSWORD FOR_SYM user equal text_or_password
8674
	  {
8675 8676
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
8677
	;
8678

8679 8680 8681
internal_variable_name:
	ident
	{
8682 8683
	  LEX *lex= Lex;
          sp_pcontext *spc= lex->spcont;
8684
	  sp_variable_t *spv;
8685 8686

	  /* We have to lookup here since local vars can shadow sysvars */
8687
	  if (!spc || !(spv = spc->find_variable(&$1)))
8688 8689 8690 8691
	  {
            /* Not an SP local variable */
	    sys_var *tmp=find_sys_var($1.str, $1.length);
	    if (!tmp)
8692
	      MYSQL_YYABORT;
8693
	    $$.var= tmp;
8694
	    $$.base_name= null_lex_str;
8695 8696 8697 8698
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
8699 8700
            if (tmp == &sys_time_zone &&
                lex->add_time_zone_tables_to_query_tables(YYTHD))
8701
              MYSQL_YYABORT;
8702 8703 8704 8705 8706 8707 8708 8709
            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;
            }
8710 8711 8712 8713 8714 8715 8716
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
8717
	}
8718 8719
	| ident '.' ident
	  {
8720
            LEX *lex= Lex;
8721 8722
            if (check_reserved_words(&$1))
            {
8723 8724
              my_parse_error(ER(ER_SYNTAX_ERROR));
              MYSQL_YYABORT;
8725
            }
8726 8727 8728 8729 8730 8731
            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')
              {
8732
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
8733
                MYSQL_YYABORT;
8734 8735 8736
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
8737 8738
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
8739
                MYSQL_YYABORT;
8740 8741 8742
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
8743
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
8744
                MYSQL_YYABORT;
8745 8746 8747 8748 8749 8750 8751 8752 8753
              }
              /* 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)
8754
                MYSQL_YYABORT;
8755
              if (!tmp->is_struct())
8756
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
8757 8758 8759
              $$.var= tmp;
              $$.base_name= $1;
            }
8760 8761 8762 8763 8764
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
8765
	      MYSQL_YYABORT;
8766
	    if (!tmp->is_struct())
8767
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8768 8769 8770
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
8771
	  }
8772
        ;
8773 8774 8775 8776 8777 8778 8779

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8781 8782 8783 8784
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
8785
	    $$= $3.length ? YYTHD->variables.old_passwords ?
8786 8787 8788 8789 8790 8791 8792 8793
	        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;
8794 8795
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8796

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

8798
set_expr_or_default:
8799 8800
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
8801 8802
	| 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
8803
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
8804
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
8805 8806


bk@work.mysql.com's avatar
bk@work.mysql.com committed
8807 8808 8809 8810 8811
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
8812 8813 8814 8815
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
8816
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOCK");
8817
	    MYSQL_YYABORT;
8818 8819
	  }
	  lex->sql_command= SQLCOM_LOCK_TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8820
	}
8821 8822
	table_lock_list
	{}
8823
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8824 8825 8826

table_or_tables:
	TABLE_SYM
8827
	| TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8828 8829 8830

table_lock_list:
	table_lock
8831
	| table_lock_list ',' table_lock;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8832 8833 8834

table_lock:
	table_ident opt_table_alias lock_option
8835
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8836
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
8837
	   MYSQL_YYABORT;
8838
	}
8839
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8840 8841 8842

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
8843
	| WRITE_SYM     { $$=TL_WRITE_DEFAULT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8844
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
8845 8846
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8847 8848

unlock:
8849 8850 8851 8852 8853 8854
	UNLOCK_SYM
	{
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
8855
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "UNLOCK");
8856
	    MYSQL_YYABORT;
8857 8858 8859 8860 8861
	  }
	  lex->sql_command= SQLCOM_UNLOCK_TABLES;
	}
	table_or_tables
	{}
8862
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8863 8864


8865
/*
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8866
** Handler: direct access to ISAM functions
8867 8868 8869 8870 8871
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
8872
	  LEX *lex= Lex;
8873 8874 8875
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
8876
	    MYSQL_YYABORT;
8877
	  }
8878
	  lex->sql_command = SQLCOM_HA_OPEN;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8879
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
8880
	    MYSQL_YYABORT;
8881
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
8882
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
8883
	{
8884
	  LEX *lex= Lex;
8885 8886 8887
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
8888
	    MYSQL_YYABORT;
8889
	  }
8890
	  lex->sql_command = SQLCOM_HA_CLOSE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8891
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
8892
	    MYSQL_YYABORT;
8893
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
8894
	| HANDLER_SYM table_ident_nodb READ_SYM
8895
	{
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
8896
	  LEX *lex=Lex;
8897 8898 8899
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
8900
	    MYSQL_YYABORT;
8901
	  }
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
8902 8903
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
8904
	  lex->current_select->select_limit= new Item_int((int32) 1);
8905
	  lex->current_select->offset_limit= 0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8906
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
8907
	    MYSQL_YYABORT;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8908
        }
8909
        handler_read_or_scan where_clause opt_limit_clause {}
8910
        ;
8911

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8912
handler_read_or_scan:
8913 8914
	handler_scan_function         { Lex->ident= null_lex_str; }
        | ident handler_rkey_function { Lex->ident= $1; }
8915
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8916 8917 8918

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
8919 8920
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8921 8922 8923 8924 8925 8926

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;  }
8927 8928
	| handler_rkey_mode
	{
8929 8930
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
8931
	  lex->ha_rkey_mode=$1;
8932
	  if (!(lex->insert_list = new List_item))
8933
	    MYSQL_YYABORT;
8934 8935
	} '(' values ')' { }
        ;
8936 8937

handler_rkey_mode:
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
8938 8939 8940 8941
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
8942 8943
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
8944

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8945 8946 8947
/* GRANT / REVOKE */

revoke:
8948
	REVOKE clear_privileges revoke_command
8949 8950 8951 8952
	{}
        ;

revoke_command:
8953
	grant_privileges ON opt_table grant_ident FROM grant_list
8954
	{
8955 8956 8957 8958 8959 8960 8961 8962 8963 8964
          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)
          {
8965 8966
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977
          }
	  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)
          {
8978 8979
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
8980 8981 8982
          }
	  lex->sql_command= SQLCOM_REVOKE;
          lex->type= TYPE_ENUM_PROCEDURE;
8983
        }
8984
	|
8985
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
8986 8987 8988
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
8989
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8990 8991

grant:
8992 8993 8994 8995 8996 8997
	GRANT clear_privileges grant_command
	{}
        ;

grant_command:
	grant_privileges ON opt_table grant_ident TO_SYM grant_list
8998
	require_clause grant_options
8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010
	{
          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)
          {
9011 9012
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023
          }
          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)
          {
9024 9025
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
9026 9027 9028 9029 9030
          }
          lex->sql_command= SQLCOM_GRANT;
          lex->type= TYPE_ENUM_PROCEDURE;
        }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9031

9032 9033 9034 9035
opt_table:
	/* Empty */
	| TABLE_SYM ;
        
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9036
grant_privileges:
9037 9038 9039 9040 9041 9042
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
9043
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9044

9045 9046 9047 9048 9049
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

9050 9051 9052
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9053

9054
object_privilege:
serg@serg.mylan's avatar
serg@serg.mylan committed
9055
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
9056 9057 9058
	| 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 {}
9059 9060
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
9061
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9062 9063 9064
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
9065
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9066 9067 9068 9069
	| RELOAD	{ Lex->grant |= RELOAD_ACL;}
	| SHUTDOWN	{ Lex->grant |= SHUTDOWN_ACL;}
	| PROCESS	{ Lex->grant |= PROCESS_ACL;}
	| FILE_SYM	{ Lex->grant |= FILE_ACL;}
9070 9071 9072 9073 9074
	| 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; }
9075 9076 9077 9078
	| 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; }
9079 9080
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
9081
	| CREATE USER { Lex->grant |= CREATE_USER_ACL; }
9082
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9083

9084

9085 9086
opt_and:
	/* empty */	{}
9087
	| AND_SYM	{}
9088 9089 9090 9091 9092 9093 9094 9095 9096
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
9097 9098 9099 9100
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
9101
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
9102
	    MYSQL_YYABORT;
9103 9104 9105 9106 9107 9108 9109 9110
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
9111
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
9112
	    MYSQL_YYABORT;
9113 9114 9115 9116 9117 9118 9119 9120
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
9121
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
9122
	    MYSQL_YYABORT;
9123 9124 9125 9126
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
9127

9128
grant_ident:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9129 9130
	'*'
	  {
9131
	    LEX *lex= Lex;
9132
            if (lex->copy_db_to(&lex->current_select->db, NULL))
9133
              MYSQL_YYABORT;
9134
	    if (lex->grant == GLOBAL_ACLS)
9135 9136
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9137
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9138 9139
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
9140
	      MYSQL_YYABORT;
9141
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9142 9143 9144
	  }
	| ident '.' '*'
	  {
9145
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9146
	    lex->current_select->db = $1.str;
9147
	    if (lex->grant == GLOBAL_ACLS)
9148 9149
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9150
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9151 9152
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
9153
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9154 9155 9156 9157
	    }
	  }
	| '*' '.' '*'
	  {
9158
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9159
	    lex->current_select->db = NULL;
9160 9161
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
9162
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9163
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9164 9165
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
9166
	      MYSQL_YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9167 9168 9169 9170
	    }
	  }
	| table_ident
	  {
9171
	    LEX *lex=Lex;
9172 9173
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,
                                                        TL_OPTION_UPDATING))
9174
	      MYSQL_YYABORT;
9175
	    if (lex->grant == GLOBAL_ACLS)
9176
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
9177 9178
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9179 9180 9181


user_list:
9182
	user  { if (Lex->users_list.push_back($1)) MYSQL_YYABORT;}
9183 9184 9185
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
9186
	      MYSQL_YYABORT;
9187 9188 9189 9190 9191
	  }
	;


grant_list:
9192
	grant_user  { if (Lex->users_list.push_back($1)) MYSQL_YYABORT;}
9193
	| grant_list ',' grant_user
9194 9195
	  {
	    if (Lex->users_list.push_back($3))
9196
	      MYSQL_YYABORT;
9197 9198
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9199 9200 9201 9202 9203 9204 9205 9206


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
9207
             if (YYTHD->variables.old_passwords)
9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224
             {
               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
9225 9226 9227
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
9228
	  { $$= $1; $1->password= $5; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9229
	| user
9230
	  { $$= $1; $1->password= null_lex_str; }
9231
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9232 9233 9234


opt_column_list:
9235 9236 9237 9238 9239
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
9240
	| '(' column_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9241 9242 9243

column_list:
	column_list ',' column_list_id
9244
	| column_list_id;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9245 9246 9247 9248

column_list_id:
	ident
	{
9249
	  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
9250 9251
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
9252
	  LEX *lex=Lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9253 9254
	  while ((point=iter++))
	  {
9255 9256
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9257 9258
		break;
	  }
9259
	  lex->grant_tot_col|= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9260
	  if (point)
9261
	    point->rights |= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9262
	  else
9263
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
9264 9265
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9266

tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
9267 9268

require_clause: /* empty */
9269
        | REQUIRE_SYM require_list
9270 9271 9272
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
9273
        | REQUIRE_SYM SSL_SYM
9274 9275 9276
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
9277
        | REQUIRE_SYM X509_SYM
9278 9279 9280 9281 9282 9283 9284
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
9285
          ;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
9286

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9287
grant_options:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9288
	/* empty */ {}
9289
	| WITH grant_option_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9290

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9291 9292
grant_option_list:
	grant_option_list grant_option {}
9293 9294
	| grant_option {}
        ;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9295 9296 9297

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
serg@serg.mylan's avatar
serg@serg.mylan committed
9298
        | MAX_QUERIES_PER_HOUR ulong_num
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9299
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
9300 9301 9302
	  LEX *lex=Lex;
	  lex->mqh.questions=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
9303
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
9304
        | MAX_UPDATES_PER_HOUR ulong_num
9305
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
9306 9307 9308
	  LEX *lex=Lex;
	  lex->mqh.updates=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
9309
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
9310
        | MAX_CONNECTIONS_PER_HOUR ulong_num
9311
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
9312 9313 9314
	  LEX *lex=Lex;
	  lex->mqh.conn_per_hour= $2;
	  lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
9315
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
9316
        | MAX_USER_CONNECTIONS_SYM ulong_num
9317
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
9318 9319 9320
	  LEX *lex=Lex;
          lex->mqh.user_conn= $2;
          lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
9321 9322
	}
        ;
9323

bk@work.mysql.com's avatar
bk@work.mysql.com committed
9324
begin:
serg@serg.mylan's avatar
serg@serg.mylan committed
9325 9326 9327 9328 9329 9330 9331
	BEGIN_SYM  
        {
	  LEX *lex=Lex;
          lex->sql_command = SQLCOM_BEGIN;
          lex->start_transaction_opt= 0;
        }
        opt_work {}
9332
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9333 9334 9335

opt_work:
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
9336
	| WORK_SYM  {}
9337
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9338

9339
opt_chain:
serg@serg.mylan's avatar
serg@serg.mylan committed
9340
	/* empty */ { $$= (YYTHD->variables.completion_type == 1); }
9341 9342 9343 9344 9345
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
serg@serg.mylan's avatar
serg@serg.mylan committed
9346
	/* empty */ { $$= (YYTHD->variables.completion_type == 2); }
9347 9348 9349 9350 9351 9352 9353 9354 9355
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
9356
commit:
serg@serg.mylan's avatar
serg@serg.mylan committed
9357
	COMMIT_SYM opt_work opt_chain opt_release
9358
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
9359 9360 9361 9362
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_COMMIT;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
9363 9364
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9365 9366

rollback:
serg@serg.mylan's avatar
serg@serg.mylan committed
9367
	ROLLBACK_SYM opt_work opt_chain opt_release
9368
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
9369 9370 9371 9372
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
9373
	}
9374 9375
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
9376
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
9377 9378 9379
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK_TO_SAVEPOINT;
	  lex->ident= $5;
9380 9381 9382
	}
	;

9383 9384 9385
savepoint:
	SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
9386 9387 9388
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SAVEPOINT;
	  lex->ident= $2;
9389 9390 9391 9392 9393 9394
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
9395 9396 9397
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RELEASE_SAVEPOINT;
	  lex->ident= $3;
9398 9399
	}
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
9400
  
9401
/*
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9402
   UNIONS : glue selects together
9403 9404 9405
*/


9406
union_clause:
9407
	/* empty */ {}
9408 9409
	| union_list
	;
9410 9411

union_list:
9412
	UNION_SYM union_option
9413 9414
	{
	  LEX *lex=Lex;
9415
	  if (lex->result)
9416 9417
	  {
	    /* Only the last SELECT can have  INTO...... */
9418
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
9419
	    MYSQL_YYABORT;
9420
	  }
9421
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
9422
	  {
9423 9424
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
9425
	  }
9426 9427
          /* This counter shouldn't be incremented for UNION parts */
          Lex->nest_level--;
9428
	  if (mysql_new_select(lex, 0))
9429
	    MYSQL_YYABORT;
9430
          mysql_init_select(lex);
9431
	  lex->current_select->linkage=UNION_TYPE;
9432 9433 9434
          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
9435
	}
9436 9437
	select_init
        {
9438 9439 9440 9441
          /*
	    Remove from the name resolution context stack the context of the
            last select in the union.
	  */
9442 9443
          Lex->pop_context();
        }
9444
	;
9445 9446

union_opt:
9447 9448 9449
	/* Empty */ { $$= 0; }
	| union_list { $$= 1; }
	| union_order_or_limit { $$= 1; }
9450
	;
9451

9452
union_order_or_limit:
9453
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9454
	    THD *thd= YYTHD;
9455
	    LEX *lex= thd->lex;
9456
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9457
	    SELECT_LEX *sel= lex->current_select;
9458
	    SELECT_LEX_UNIT *unit= sel->master_unit();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9459
	    SELECT_LEX *fake= unit->fake_select_lex;
9460 9461 9462 9463 9464 9465
	    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
9466
	    thd->where= "global ORDER clause";
9467
	  }
9468
	order_or_limit
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9469 9470
          {
	    THD *thd= YYTHD;
9471
	    thd->lex->current_select->no_table_names_allowed= 0;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9472 9473
	    thd->where= "";
          }
9474 9475 9476
	;

order_or_limit:
9477 9478
	order_clause opt_limit_clause_init
	| limit_clause
9479
	;
9480 9481

union_option:
9482 9483 9484 9485
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
9486

9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509
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
9510

9511
subselect_init:
9512
  select_init2
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9513
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9514
    $$= Lex->current_select->master_unit()->first_select();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9515 9516
  };

9517
subselect_start:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9518
	{
9519
	  LEX *lex=Lex;
9520
          if (lex->sql_command == (int)SQLCOM_HA_READ ||
9521 9522
              lex->sql_command == (int)SQLCOM_KILL ||
              lex->sql_command == (int)SQLCOM_PURGE)
9523
	  {
9524 9525
            my_parse_error(ER(ER_SYNTAX_ERROR));
	    MYSQL_YYABORT;
9526
	  }
9527 9528 9529 9530 9531 9532 9533
          /* 
            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
9534
	  if (mysql_new_select(Lex, 1))
9535
	    MYSQL_YYABORT;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9536
	};
9537 9538

subselect_end:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9539 9540
	{
	  LEX *lex=Lex;
9541
          lex->pop_context();
9542
          SELECT_LEX *child= lex->current_select;
9543
	  lex->current_select = lex->current_select->return_after_parsing();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
9544
          lex->nest_level--;
9545
          lex->current_select->n_child_sum_items += child->n_sum_items;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
9546 9547 9548 9549 9550 9551
          /*
            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
9552
	};
serg@serg.mylan's avatar
serg@serg.mylan committed
9553

9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581
/**************************************************************************

 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.

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

9582
definer:
9583
	/* empty */
9584
	{
9585 9586 9587 9588 9589 9590 9591 9592
          /*
            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;
9593
	}
9594
	| DEFINER_SYM EQ user
9595
	{
9596
	  YYTHD->lex->definer= get_current_user(YYTHD, $3);
9597 9598 9599 9600 9601
	}
	;

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

9602
 CREATE VIEW statement parts.
9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637

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

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 */
9638
	{ Lex->create_view_suid= VIEW_SUID_DEFAULT; }
9639
	| SQL_SYM SECURITY_SYM DEFINER_SYM
9640
	{ Lex->create_view_suid= VIEW_SUID_DEFINER; }
9641
	| SQL_SYM SECURITY_SYM INVOKER_SYM
9642
	{ Lex->create_view_suid= VIEW_SUID_INVOKER; }
9643 9644 9645 9646 9647 9648 9649 9650 9651
	;

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 */
9652
	  if (!lex->select_lex.add_table_to_list(thd, $3, NULL, TL_OPTION_UPDATING))
9653
	    MYSQL_YYABORT;
9654 9655 9656 9657 9658 9659 9660 9661
	}
	view_list_opt AS view_select view_check_option
	{}
	;

view_list_opt:
	/* empty */
	{}
9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677
	| '(' 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)));
	  }
	;

9678
view_select:
9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696
        {
          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:
9697 9698
	SELECT_SYM remember_name select_init2
	{
9699 9700 9701 9702 9703 9704
          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;
9705 9706 9707
	}
	| '(' remember_name select_paren ')' union_opt
	{
9708 9709 9710 9711 9712 9713
          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;
9714
	}
9715 9716
	;

9717
view_check_option:
9718
	/* empty */
9719 9720 9721 9722 9723 9724 9725
	{ 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; }
9726
	;
9727

9728 9729 9730 9731 9732 9733 9734
/**************************************************************************

 CREATE TRIGGER statement parts.

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

trigger_tail:
9735
	TRIGGER_SYM remember_name sp_name trg_action_time trg_event
9736
	ON remember_name table_ident FOR_SYM remember_name EACH_SYM ROW_SYM
9737
	{
9738 9739 9740
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;
9741 9742 9743
	  sp_head *sp;
	 
	  if (lex->sphead)
9744
	  {
9745
	    my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "TRIGGER");
9746
	    MYSQL_YYABORT;
9747
	  }
9748 9749
	
	  if (!(sp= new sp_head()))
9750
	    MYSQL_YYABORT;
9751
	  sp->reset_thd_mem_root(thd);
9752
	  sp->init(lex);
9753
	  sp->m_type= TYPE_ENUM_TRIGGER;
9754
          sp->init_sp_name(thd, $3);
9755
	
9756
	  lex->stmt_definition_begin= $2;
9757
          lex->ident.str= $7;
9758
          lex->ident.length= $10 - $7;
9759

9760 9761 9762 9763 9764 9765 9766
	  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 ';'.
	  */
9767 9768
	  sp->m_old_cmq= thd->client_capabilities & CLIENT_MULTI_QUERIES;
	  thd->client_capabilities &= ~CLIENT_MULTI_QUERIES;
9769 9770 9771
	  
	  bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	  lex->sphead->m_chistics= &lex->sp_chistics;
9772
	  lex->sphead->m_body_begin= lip->ptr;
9773 9774
          while (my_isspace(system_charset_info, lex->sphead->m_body_begin[0]))
            ++lex->sphead->m_body_begin;
9775 9776 9777 9778 9779 9780 9781
	}
	sp_proc_stmt
	{
	  LEX *lex= Lex;
	  sp_head *sp= lex->sphead;
	  
	  lex->sql_command= SQLCOM_CREATE_TRIGGER;
9782
	  sp->init_strings(YYTHD, lex);
9783 9784 9785 9786 9787 9788
	  /* 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"))
9789
	      MYSQL_YYABORT;
9790 9791 9792 9793 9794 9795
	
	  /*
	    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.
	  */
9796
	  if (!lex->select_lex.add_table_to_list(YYTHD, $8,
9797 9798
	                                         (LEX_STRING*) 0,
	                                         TL_OPTION_UPDATING,
9799
                                                 TL_IGNORE))
9800
	    MYSQL_YYABORT;
9801
	}
9802 9803
	;

9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827
/**************************************************************************

 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");
9828
	    MYSQL_YYABORT;
9829
	  }
9830

9831
	  lex->stmt_definition_begin= $2;
9832

9833 9834 9835 9836
	  /* Order is important here: new - reset - init */
	  sp= new sp_head();
	  sp->reset_thd_mem_root(YYTHD);
	  sp->init(lex);
9837
	  sp->m_type= TYPE_ENUM_PROCEDURE;
9838
          sp->init_sp_name(YYTHD, $3);
9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850

	  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);
	}
        '('
	{
9851 9852 9853
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;
9854

9855
	  lex->sphead->m_param_begin= lip->tok_start+1;
9856 9857 9858 9859
	}
	sp_pdparam_list
	')'
	{
9860 9861 9862
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;
9863

9864
	  lex->sphead->m_param_end= lip->tok_start;
9865 9866 9867 9868
	  bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	}
	sp_c_chistics
	{
9869 9870 9871
          THD *thd= YYTHD;
	  LEX *lex= thd->lex;
          Lex_input_stream *lip= thd->m_lip;
9872 9873

	  lex->sphead->m_chistics= &lex->sp_chistics;
9874
	  lex->sphead->m_body_begin= lip->tok_start;
9875 9876 9877 9878 9879 9880
	}
	sp_proc_stmt
	{
	  LEX *lex= Lex;
	  sp_head *sp= lex->sphead;

9881
	  sp->init_strings(YYTHD, lex);
9882 9883 9884 9885 9886 9887 9888 9889
	  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);
	}
	;

9890
/*************************************************************************/
9891

9892 9893
xa: XA_SYM begin_or_start xid opt_join_or_resume
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
9894
        Lex->sql_command = SQLCOM_XA_START;
9895
      }
9896
    | XA_SYM END xid opt_suspend
9897
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
9898
        Lex->sql_command = SQLCOM_XA_END;
9899 9900 9901
      }
    | XA_SYM PREPARE_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
9902
        Lex->sql_command = SQLCOM_XA_PREPARE;
9903 9904 9905
      }
    | XA_SYM COMMIT_SYM xid opt_one_phase
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
9906
        Lex->sql_command = SQLCOM_XA_COMMIT;
9907 9908 9909
      }
    | XA_SYM ROLLBACK_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
9910
        Lex->sql_command = SQLCOM_XA_ROLLBACK;
9911 9912 9913
      }
    | XA_SYM RECOVER_SYM
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
9914
        Lex->sql_command = SQLCOM_XA_RECOVER;
9915 9916 9917
      }
    ;

serg@serg.mylan's avatar
serg@serg.mylan committed
9918 9919
xid: text_string
     {
9920
       MYSQL_YYABORT_UNLESS($1->length() <= MAXGTRIDSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
9921
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
9922
         MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
9923 9924 9925 9926
       Lex->xid->set(1L, $1->ptr(), $1->length(), 0, 0);
     }
     | text_string ',' text_string
     {
9927
       MYSQL_YYABORT_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
9928
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
9929
         MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
9930 9931 9932 9933
       Lex->xid->set(1L, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     | text_string ',' text_string ',' ulong_num
     {
9934
       MYSQL_YYABORT_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
9935
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
9936
         MYSQL_YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
9937 9938 9939
       Lex->xid->set($5, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     ;
9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955

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

9956
opt_suspend:
9957 9958
    /* nothing */           { Lex->xa_opt=XA_NONE;        }
    | SUSPEND_SYM           { Lex->xa_opt=XA_SUSPEND;     }
9959 9960 9961 9962 9963
      opt_migrate
    ;

opt_migrate:
    /* nothing */           { }
9964 9965 9966 9967
    | FOR_SYM MIGRATE_SYM   { Lex->xa_opt=XA_FOR_MIGRATE; }
    ;