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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
7

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
12

bk@work.mysql.com's avatar
bk@work.mysql.com committed
13 14 15 16
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

tim@cane.mysql.fi's avatar
tim@cane.mysql.fi committed
17
/* sql_yacc.yy */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
18 19

%{
20 21 22
/* thd is passed as an arg to yyparse(), and subsequently to yylex().
** The type will be void*, so it must be  cast to (THD*) when used.
** Use the YYTHD macro for this.
23 24
*/
#define YYPARSE_PARAM yythd
25
#define YYLEX_PARAM yythd
26 27
#define YYTHD ((THD *)yythd)

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

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

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

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

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

58
/* Helper for parsing "IS [NOT] truth_value" */
59
inline Item *is_truth_value(THD *thd, Item *A, bool v1, bool v2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
60
{
61 62 63 64 65 66 67 68 69
  Item *v1_t= new (thd->mem_root) Item_int((char *) (v1 ? "TRUE" : "FALSE"),
                                           v1, 1);
  Item *v1_f= new (thd->mem_root) Item_int((char *) (v1 ? "FALSE" : "TRUE"),
                                           !v1, 1);
  Item *v2_t= new (thd->mem_root) Item_int((char *) (v2 ? "TRUE" : "FALSE"),
                                           v2, 1);
  Item *ifnull= new (thd->mem_root) Item_func_ifnull(A, v2_t);

  return new (thd->mem_root) Item_func_if(ifnull, v1_t, v1_f);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
70 71
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#ifndef DBUG_OFF
#define YYDEBUG 1
#else
#define YYDEBUG 0
#endif

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

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

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

  extern int yydebug;
  yydebug= 1;
}
#endif

100 101 102 103 104 105 106 107 108 109 110
static bool is_native_function(THD *thd, const LEX_STRING *name)
{
  if (find_native_function_builder(thd, *name))
    return true;

  if (is_lex_native_function(name))
    return true;

  return false;
}

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

%{
156
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
157 158 159 160
%}

%pure_parser					/* We have threads */

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/*
   Comments for TOKENS.
   For each token, please include in the same line a comment that contains
   the following tags:
   SQL-2003-R : Reserved keyword as per SQL-2003
   SQL-2003-N : Non Reserved keyword as per SQL-2003
   SQL-1999-R : Reserved keyword as per SQL-1999
   SQL-1999-N : Non Reserved keyword as per SQL-1999
   MYSQL      : MySQL extention (unspecified)
   MYSQL-FUNC : MySQL extention, function
   INTERNAL   : Not a real token, lex optimization
   OPERATOR   : SQL operator
   FUTURE-USE : Reserved for futur use

   This makes the code grep-able, and helps maintenance.
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
177

178
%token  ABORT_SYM                     /* INTERNAL (used in lex) */
179
%token  ACCESSIBLE_SYM
180 181 182 183
%token  ACTION                        /* SQL-2003-N */
%token  ADD                           /* SQL-2003-R */
%token  ADDDATE_SYM                   /* MYSQL-FUNC */
%token  AFTER_SYM                     /* SQL-2003-N */
184 185 186
%token  AGAINST
%token  AGGREGATE_SYM
%token  ALGORITHM_SYM
187 188
%token  ALL                           /* SQL-2003-R */
%token  ALTER                         /* SQL-2003-R */
189
%token  ANALYZE_SYM
190 191 192 193 194 195 196 197
%token  AND_AND_SYM                   /* OPERATOR */
%token  AND_SYM                       /* SQL-2003-R */
%token  ANY_SYM                       /* SQL-2003-R */
%token  AS                            /* SQL-2003-R */
%token  ASC                           /* SQL-2003-N */
%token  ASCII_SYM                     /* MYSQL-FUNC */
%token  ASENSITIVE_SYM                /* FUTURE-USE */
%token  AT_SYM                        /* SQL-2003-R */
brian@grrr.local's avatar
brian@grrr.local committed
198
%token  AUTHORS_SYM
199
%token  AUTOEXTEND_SIZE_SYM
200
%token  AUTO_INC
201
%token  AVG_ROW_LENGTH
202
%token  AVG_SYM                       /* SQL-2003-N */
203
%token  BACKUP_SYM
204 205 206 207 208
%token  BEFORE_SYM                    /* SQL-2003-N */
%token  BEGIN_SYM                     /* SQL-2003-R */
%token  BETWEEN_SYM                   /* SQL-2003-R */
%token  BIGINT                        /* SQL-2003-R */
%token  BINARY                        /* SQL-2003-R */
209
%token  BINLOG_SYM
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
210
%token  BIN_NUM
211 212 213 214 215 216
%token  BIT_AND                       /* MYSQL-FUNC */
%token  BIT_OR                        /* MYSQL-FUNC */
%token  BIT_SYM                       /* MYSQL-FUNC */
%token  BIT_XOR                       /* MYSQL-FUNC */
%token  BLOB_SYM                      /* SQL-2003-R */
%token  BOOLEAN_SYM                   /* SQL-2003-R */
217
%token  BOOL_SYM
218
%token  BOTH                          /* SQL-2003-R */
219
%token  BTREE_SYM
220
%token  BY                            /* SQL-2003-R */
221 222
%token  BYTE_SYM
%token  CACHE_SYM
223 224 225 226 227 228
%token  CALL_SYM                      /* SQL-2003-R */
%token  CASCADE                       /* SQL-2003-N */
%token  CASCADED                      /* SQL-2003-R */
%token  CASE_SYM                      /* SQL-2003-R */
%token  CAST_SYM                      /* SQL-2003-R */
%token  CHAIN_SYM                     /* SQL-2003-N */
229 230 231
%token  CHANGE
%token  CHANGED
%token  CHARSET
232
%token  CHAR_SYM                      /* SQL-2003-R */
233
%token  CHECKSUM_SYM
234
%token  CHECK_SYM                     /* SQL-2003-R */
235 236
%token  CIPHER_SYM
%token  CLIENT_SYM
237 238
%token  CLOSE_SYM                     /* SQL-2003-R */
%token  COALESCE                      /* SQL-2003-N */
pem@mysql.com's avatar
pem@mysql.com committed
239
%token  CODE_SYM
240 241
%token  COLLATE_SYM                   /* SQL-2003-R */
%token  COLLATION_SYM                 /* SQL-2003-N */
242
%token  COLUMNS
243
%token  COLUMN_SYM                    /* SQL-2003-R */
244
%token  COMMENT_SYM
245 246
%token  COMMITTED_SYM                 /* SQL-2003-N */
%token  COMMIT_SYM                    /* SQL-2003-R */
serg@serg.mylan's avatar
serg@serg.mylan committed
247
%token  COMPACT_SYM
248
%token  COMPLETION_SYM
249 250
%token  COMPRESSED_SYM
%token  CONCURRENT
251
%token  CONDITION_SYM                 /* SQL-2003-N */
252 253
%token  CONNECTION_SYM
%token  CONSISTENT_SYM
254 255 256
%token  CONSTRAINT                    /* SQL-2003-R */
%token  CONTAINS_SYM                  /* SQL-2003-N */
%token  CONTINUE_SYM                  /* SQL-2003-R */
257
%token  CONTRIBUTORS_SYM
258 259 260 261 262 263 264 265 266
%token  CONVERT_SYM                   /* SQL-2003-N */
%token  COUNT_SYM                     /* SQL-2003-N */
%token  CREATE                        /* SQL-2003-R */
%token  CROSS                         /* SQL-2003-R */
%token  CUBE_SYM                      /* SQL-2003-R */
%token  CURDATE                       /* MYSQL-FUNC */
%token  CURRENT_USER                  /* SQL-2003-R */
%token  CURSOR_SYM                    /* SQL-2003-R */
%token  CURTIME                       /* MYSQL-FUNC */
267 268
%token  DATABASE
%token  DATABASES
269
%token  DATAFILE_SYM
270
%token  DATA_SYM                      /* SQL-2003-N */
271
%token  DATETIME
272 273 274
%token  DATE_ADD_INTERVAL             /* MYSQL-FUNC */
%token  DATE_SUB_INTERVAL             /* MYSQL-FUNC */
%token  DATE_SYM                      /* SQL-2003-R */
275 276 277 278
%token  DAY_HOUR_SYM
%token  DAY_MICROSECOND_SYM
%token  DAY_MINUTE_SYM
%token  DAY_SECOND_SYM
279 280
%token  DAY_SYM                       /* SQL-2003-R */
%token  DEALLOCATE_SYM                /* SQL-2003-R */
serg@serg.mylan's avatar
serg@serg.mylan committed
281
%token  DECIMAL_NUM
282 283 284
%token  DECIMAL_SYM                   /* SQL-2003-R */
%token  DECLARE_SYM                   /* SQL-2003-R */
%token  DEFAULT                       /* SQL-2003-R */
285 286 287
%token  DEFINER_SYM
%token  DELAYED_SYM
%token  DELAY_KEY_WRITE_SYM
288 289 290
%token  DELETE_SYM                    /* SQL-2003-R */
%token  DESC                          /* SQL-2003-N */
%token  DESCRIBE                      /* SQL-2003-R */
291
%token  DES_KEY_FILE
292
%token  DETERMINISTIC_SYM             /* SQL-2003-R */
293 294 295
%token  DIRECTORY_SYM
%token  DISABLE_SYM
%token  DISCARD
296
%token  DISK_SYM
297
%token  DISTINCT                      /* SQL-2003-R */
298
%token  DIV_SYM
299
%token  DOUBLE_SYM                    /* SQL-2003-R */
300
%token  DO_SYM
301
%token  DROP                          /* SQL-2003-R */
302 303
%token  DUAL_SYM
%token  DUMPFILE
304
%token  DUPLICATE_SYM
305 306 307
%token  DYNAMIC_SYM                   /* SQL-2003-R */
%token  EACH_SYM                      /* SQL-2003-R */
%token  ELSE                          /* SQL-2003-R */
308 309 310
%token  ELSEIF_SYM
%token  ENABLE_SYM
%token  ENCLOSED
311
%token  END                           /* SQL-2003-R */
312
%token  ENDS_SYM
313
%token  END_OF_INPUT                  /* INTERNAL */
314 315 316
%token  ENGINES_SYM
%token  ENGINE_SYM
%token  ENUM
317 318
%token  EQ                            /* OPERATOR */
%token  EQUAL_SYM                     /* OPERATOR */
319 320
%token  ERRORS
%token  ESCAPED
321
%token  ESCAPE_SYM                    /* SQL-2003-R */
322
%token  EVENTS_SYM
323 324 325 326
%token  EVENT_SYM
%token  EVERY_SYM                     /* SQL-2003-N */
%token  EXECUTE_SYM                   /* SQL-2003-R */
%token  EXISTS                        /* SQL-2003-R */
327
%token  EXIT_SYM
328 329
%token  EXPANSION_SYM
%token  EXTENDED_SYM
330
%token  EXTENT_SIZE_SYM
331 332
%token  EXTRACT_SYM                   /* SQL-2003-N */
%token  FALSE_SYM                     /* SQL-2003-R */
333
%token  FAST_SYM
334
%token  FETCH_SYM                     /* SQL-2003-R */
335
%token  FILE_SYM
336
%token  FIRST_SYM                     /* SQL-2003-N */
337 338
%token  FIXED_SYM
%token  FLOAT_NUM
339
%token  FLOAT_SYM                     /* SQL-2003-R */
340 341
%token  FLUSH_SYM
%token  FORCE_SYM
342 343 344
%token  FOREIGN                       /* SQL-2003-R */
%token  FOR_SYM                       /* SQL-2003-R */
%token  FOUND_SYM                     /* SQL-2003-R */
345 346
%token  FRAC_SECOND_SYM
%token  FROM
347
%token  FULL                          /* SQL-2003-R */
348
%token  FULLTEXT_SYM
349
%token  FUNCTION_SYM                  /* SQL-2003-R */
350 351 352
%token  GE
%token  GEOMETRYCOLLECTION
%token  GEOMETRY_SYM
353 354 355
%token  GET_FORMAT                    /* MYSQL-FUNC */
%token  GLOBAL_SYM                    /* SQL-2003-R */
%token  GRANT                         /* SQL-2003-R */
356
%token  GRANTS
357
%token  GROUP                         /* SQL-2003-R */
358 359
%token  GROUP_CONCAT_SYM
%token  GROUP_UNIQUE_USERS
360
%token  GT_SYM                        /* OPERATOR */
361 362
%token  HANDLER_SYM
%token  HASH_SYM
363
%token  HAVING                        /* SQL-2003-R */
364 365 366
%token  HELP_SYM
%token  HEX_NUM
%token  HIGH_PRIORITY
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
367
%token  HOST_SYM
368 369 370 371
%token  HOSTS_SYM
%token  HOUR_MICROSECOND_SYM
%token  HOUR_MINUTE_SYM
%token  HOUR_SECOND_SYM
372
%token  HOUR_SYM                      /* SQL-2003-R */
373 374 375 376 377 378 379 380 381
%token  IDENT
%token  IDENTIFIED_SYM
%token  IDENT_QUOTED
%token  IF
%token  IGNORE_SYM
%token  IMPORT
%token  INDEXES
%token  INDEX_SYM
%token  INFILE
382
%token  INITIAL_SIZE_SYM
383
%token  INNER_SYM                     /* SQL-2003-R */
384
%token  INNOBASE_SYM
385 386 387
%token  INOUT_SYM                     /* SQL-2003-R */
%token  INSENSITIVE_SYM               /* SQL-2003-R */
%token  INSERT                        /* SQL-2003-R */
388
%token  INSERT_METHOD
389
%token  INSTALL_SYM
390 391 392
%token  INTERVAL_SYM                  /* SQL-2003-R */
%token  INTO                          /* SQL-2003-R */
%token  INT_SYM                       /* SQL-2003-R */
393
%token  INVOKER_SYM
394 395 396
%token  IN_SYM                        /* SQL-2003-R */
%token  IS                            /* SQL-2003-R */
%token  ISOLATION                     /* SQL-2003-R */
397 398
%token  ISSUER_SYM
%token  ITERATE_SYM
399
%token  JOIN_SYM                      /* SQL-2003-R */
400
%token  KEYS
401
%token  KEY_BLOCK_SIZE
402
%token  KEY_SYM                       /* SQL-2003-N */
403
%token  KILL_SYM
404 405 406 407
%token  LANGUAGE_SYM                  /* SQL-2003-R */
%token  LAST_SYM                      /* SQL-2003-N */
%token  LE                            /* OPERATOR */
%token  LEADING                       /* SQL-2003-R */
408 409
%token  LEAVES
%token  LEAVE_SYM
410
%token  LEFT                          /* SQL-2003-R */
411
%token  LESS_SYM
412 413
%token  LEVEL_SYM
%token  LEX_HOSTNAME
414
%token  LIKE                          /* SQL-2003-R */
415
%token  LIMIT
416
%token  LINEAR_SYM
417 418
%token  LINES
%token  LINESTRING
419
%token  LIST_SYM
420
%token  LOAD
421 422
%token  LOCAL_SYM                     /* SQL-2003-R */
%token  LOCATOR_SYM                   /* SQL-2003-N */
423 424
%token  LOCKS_SYM
%token  LOCK_SYM
425
%token  LOGFILE_SYM
426 427 428 429 430 431 432
%token  LOGS_SYM
%token  LONGBLOB
%token  LONGTEXT
%token  LONG_NUM
%token  LONG_SYM
%token  LOOP_SYM
%token  LOW_PRIORITY
433
%token  LT                            /* OPERATOR */
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
%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_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
449
%token  MATCH                         /* SQL-2003-R */
450 451 452
%token  MAX_CONNECTIONS_PER_HOUR
%token  MAX_QUERIES_PER_HOUR
%token  MAX_ROWS
453
%token  MAX_SIZE_SYM
454
%token  MAX_SYM                       /* SQL-2003-N */
455
%token  MAX_UPDATES_PER_HOUR
serg@serg.mylan's avatar
serg@serg.mylan committed
456
%token  MAX_USER_CONNECTIONS_SYM
457
%token  MAX_VALUE_SYM                 /* SQL-2003-N */
458 459 460 461
%token  MEDIUMBLOB
%token  MEDIUMINT
%token  MEDIUMTEXT
%token  MEDIUM_SYM
462
%token  MEMORY_SYM
463 464
%token  MERGE_SYM                     /* SQL-2003-R */
%token  MICROSECOND_SYM               /* MYSQL-FUNC */
465 466 467
%token  MIGRATE_SYM
%token  MINUTE_MICROSECOND_SYM
%token  MINUTE_SECOND_SYM
468
%token  MINUTE_SYM                    /* SQL-2003-R */
469
%token  MIN_ROWS
470
%token  MIN_SYM                       /* SQL-2003-N */
471
%token  MODE_SYM
472
%token  MODIFIES_SYM                  /* SQL-2003-R */
473
%token  MODIFY_SYM
474 475
%token  MOD_SYM                       /* SQL-2003-N */
%token  MONTH_SYM                     /* SQL-2003-R */
476 477 478
%token  MULTILINESTRING
%token  MULTIPOINT
%token  MULTIPOLYGON
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
479
%token  MUTEX_SYM
480 481 482 483
%token  NAMES_SYM                     /* SQL-2003-N */
%token  NAME_SYM                      /* SQL-2003-N */
%token  NATIONAL_SYM                  /* SQL-2003-R */
%token  NATURAL                       /* SQL-2003-R */
484
%token  NCHAR_STRING
485
%token  NCHAR_SYM                     /* SQL-2003-R */
486
%token  NDBCLUSTER_SYM
487 488 489 490
%token  NE                            /* OPERATOR */
%token  NEG
%token  NEW_SYM                       /* SQL-2003-R */
%token  NEXT_SYM                      /* SQL-2003-N */
491
%token  NODEGROUP_SYM
492
%token  NONE_SYM                      /* SQL-2003-R */
493
%token  NOT2_SYM
494
%token  NOT_SYM                       /* SQL-2003-R */
495
%token  NOW_SYM
496
%token  NO_SYM                        /* SQL-2003-R */
497
%token  NO_WAIT_SYM
498
%token  NO_WRITE_TO_BINLOG
499
%token  NULL_SYM                      /* SQL-2003-R */
500
%token  NUM
501
%token  NUMERIC_SYM                   /* SQL-2003-R */
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
502
%token  NVARCHAR_SYM
503 504
%token  OFFSET_SYM
%token  OLD_PASSWORD
505
%token  ON                            /* SQL-2003-R */
506
%token  ONE_SHOT_SYM
507
%token  ONE_SYM
508
%token  OPEN_SYM                      /* SQL-2003-R */
509
%token  OPTIMIZE
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
510
%token  OPTIONS_SYM
511
%token  OPTION                        /* SQL-2003-N */
512 513
%token  OPTIONALLY
%token  OR2_SYM
514 515 516
%token  ORDER_SYM                     /* SQL-2003-R */
%token  OR_OR_SYM                     /* OPERATOR */
%token  OR_SYM                        /* SQL-2003-R */
517 518
%token  OUTER
%token  OUTFILE
519
%token  OUT_SYM                       /* SQL-2003-R */
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
520
%token  OWNER_SYM
521
%token  PACK_KEYS_SYM
522
%token  PARAM_MARKER
523
%token  PARSER_SYM
524
%token  PARTIAL                       /* SQL-2003-N */
525
%token  PARTITIONING_SYM
526
%token  PARTITIONS_SYM
527
%token  PARTITION_SYM                 /* SQL-2003-R */
528 529
%token  PASSWORD
%token  PHASE_SYM
530
%token  PLUGINS_SYM
531
%token  PLUGIN_SYM
532 533
%token  POINT_SYM
%token  POLYGON
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
534
%token  PORT_SYM
535 536 537
%token  POSITION_SYM                  /* SQL-2003-N */
%token  PRECISION                     /* SQL-2003-R */
%token  PREPARE_SYM                   /* SQL-2003-R */
538
%token  PRESERVE_SYM
539
%token  PREV_SYM
540 541 542
%token  PRIMARY_SYM                   /* SQL-2003-R */
%token  PRIVILEGES                    /* SQL-2003-N */
%token  PROCEDURE                     /* SQL-2003-R */
543 544 545 546 547 548
%token  PROCESS
%token  PROCESSLIST_SYM
%token  PURGE
%token  QUARTER_SYM
%token  QUERY_SYM
%token  QUICK
549 550
%token  RANGE_SYM                     /* SQL-2003-R */
%token  READS_SYM                     /* SQL-2003-R */
551
%token  READ_ONLY_SYM
552
%token  READ_SYM                      /* SQL-2003-N */
553
%token  READ_WRITE_SYM
554
%token  REAL                          /* SQL-2003-R */
555
%token  REBUILD_SYM
556
%token  RECOVER_SYM
557
%token  REDOFILE_SYM
558
%token  REDO_BUFFER_SIZE_SYM
serg@serg.mylan's avatar
serg@serg.mylan committed
559
%token  REDUNDANT_SYM
560
%token  REFERENCES                    /* SQL-2003-R */
561 562 563 564
%token  REGEXP
%token  RELAY_LOG_FILE_SYM
%token  RELAY_LOG_POS_SYM
%token  RELAY_THREAD
565
%token  RELEASE_SYM                   /* SQL-2003-R */
566
%token  RELOAD
567
%token  REMOVE_SYM
568
%token  RENAME
569
%token  REORGANIZE_SYM
570
%token  REPAIR
571 572 573
%token  REPEATABLE_SYM                /* SQL-2003-N */
%token  REPEAT_SYM                    /* MYSQL-FUNC */
%token  REPLACE                       /* MYSQL-FUNC */
574 575 576 577 578 579 580
%token  REPLICATION
%token  REQUIRE_SYM
%token  RESET_SYM
%token  RESOURCES
%token  RESTORE_SYM
%token  RESTRICT
%token  RESUME_SYM
581 582 583 584 585 586 587 588
%token  RETURNS_SYM                   /* SQL-2003-R */
%token  RETURN_SYM                    /* SQL-2003-R */
%token  REVOKE                        /* SQL-2003-R */
%token  RIGHT                         /* SQL-2003-R */
%token  ROLLBACK_SYM                  /* SQL-2003-R */
%token  ROLLUP_SYM                    /* SQL-2003-R */
%token  ROUTINE_SYM                   /* SQL-2003-N */
%token  ROWS_SYM                      /* SQL-2003-R */
589
%token  ROW_FORMAT_SYM
590
%token  ROW_SYM                       /* SQL-2003-R */
591
%token  RTREE_SYM
592
%token  SAVEPOINT_SYM                 /* SQL-2003-R */
593
%token  SCHEDULE_SYM
594
%token  SECOND_MICROSECOND_SYM
595 596 597 598
%token  SECOND_SYM                    /* SQL-2003-R */
%token  SECURITY_SYM                  /* SQL-2003-N */
%token  SELECT_SYM                    /* SQL-2003-R */
%token  SENSITIVE_SYM                 /* FUTURE-USE */
599
%token  SEPARATOR_SYM
600
%token  SERIALIZABLE_SYM              /* SQL-2003-N */
601
%token  SERIAL_SYM
602
%token  SESSION_SYM                   /* SQL-2003-N */
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
603 604
%token  SERVER_SYM
%token  SERVER_OPTIONS
605
%token  SET                           /* SQL-2003-R */
606 607
%token  SET_VAR
%token  SHARE_SYM
608 609
%token  SHIFT_LEFT                    /* OPERATOR */
%token  SHIFT_RIGHT                   /* OPERATOR */
610 611 612
%token  SHOW
%token  SHUTDOWN
%token  SIGNED_SYM
613
%token  SIMPLE_SYM                    /* SQL-2003-N */
614
%token  SLAVE
615
%token  SMALLINT                      /* SQL-2003-R */
616
%token  SNAPSHOT_SYM
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
617
%token  SOCKET_SYM
618
%token  SONAME_SYM
619 620
%token  SOUNDS_SYM
%token  SPATIAL_SYM
621 622 623 624
%token  SPECIFIC_SYM                  /* SQL-2003-R */
%token  SQLEXCEPTION_SYM              /* SQL-2003-R */
%token  SQLSTATE_SYM                  /* SQL-2003-R */
%token  SQLWARNING_SYM                /* SQL-2003-R */
625 626 627 628 629 630
%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
631
%token  SQL_SYM                       /* SQL-2003-R */
632
%token  SQL_THREAD
633
%token  SSL_SYM
634
%token  STARTING
635
%token  STARTS_SYM
636
%token  START_SYM                     /* SQL-2003-R */
637
%token  STATUS_SYM
638
%token  STDDEV_SAMP_SYM               /* SQL-2003-N */
639 640 641 642 643 644 645
%token  STD_SYM
%token  STOP_SYM
%token  STORAGE_SYM
%token  STRAIGHT_JOIN
%token  STRING_SYM
%token  SUBDATE_SYM
%token  SUBJECT_SYM
646
%token  SUBPARTITIONS_SYM
647 648 649
%token  SUBPARTITION_SYM
%token  SUBSTRING                     /* SQL-2003-N */
%token  SUM_SYM                       /* SQL-2003-N */
650 651
%token  SUPER_SYM
%token  SUSPEND_SYM
652
%token  SYSDATE
653 654
%token  TABLES
%token  TABLESPACE
655 656 657
%token  TABLE_REF_PRIORITY
%token  TABLE_SYM                     /* SQL-2003-R */
%token  TEMPORARY                     /* SQL-2003-N */
658 659 660 661
%token  TEMPTABLE_SYM
%token  TERMINATED
%token  TEXT_STRING
%token  TEXT_SYM
662 663 664
%token  THAN_SYM
%token  THEN_SYM                      /* SQL-2003-R */
%token  TIMESTAMP                     /* SQL-2003-R */
665 666
%token  TIMESTAMP_ADD
%token  TIMESTAMP_DIFF
667
%token  TIME_SYM                      /* SQL-2003-R */
668 669 670
%token  TINYBLOB
%token  TINYINT
%token  TINYTEXT
671 672
%token  TO_SYM                        /* SQL-2003-R */
%token  TRAILING                      /* SQL-2003-R */
673
%token  TRANSACTION_SYM
674
%token  TRIGGERS_SYM
675 676 677
%token  TRIGGER_SYM                   /* SQL-2003-R */
%token  TRIM                          /* SQL-2003-N */
%token  TRUE_SYM                      /* SQL-2003-R */
678
%token  TRUNCATE_SYM
679
%token  TYPES_SYM
680
%token  TYPE_SYM                      /* SQL-2003-N */
681 682
%token  UDF_RETURNS_SYM
%token  ULONGLONG_NUM
683
%token  UNCOMMITTED_SYM               /* SQL-2003-N */
684 685
%token  UNDEFINED_SYM
%token  UNDERSCORE_CHARSET
686 687 688
%token  UNDOFILE_SYM
%token  UNDO_BUFFER_SIZE_SYM
%token  UNDO_SYM                      /* FUTURE-USE */
689
%token  UNICODE_SYM
690
%token  UNINSTALL_SYM
691
%token  UNION_SYM                     /* SQL-2003-R */
692 693
%token  UNIQUE_SYM
%token  UNIQUE_USERS
694
%token  UNKNOWN_SYM                   /* SQL-2003-R */
695 696 697
%token  UNLOCK_SYM
%token  UNSIGNED
%token  UNTIL_SYM
698
%token  UPDATE_SYM                    /* SQL-2003-R */
699
%token  UPGRADE_SYM
700 701
%token  USAGE                         /* SQL-2003-N */
%token  USER                          /* SQL-2003-R */
702 703
%token  USE_FRM
%token  USE_SYM
704
%token  USING                         /* SQL-2003-R */
705 706 707
%token  UTC_DATE_SYM
%token  UTC_TIMESTAMP_SYM
%token  UTC_TIME_SYM
708 709
%token  VALUES                        /* SQL-2003-R */
%token  VALUE_SYM                     /* SQL-2003-R */
710
%token  VARBINARY
711
%token  VARCHAR                       /* SQL-2003-R */
712 713
%token  VARIABLES
%token  VARIANCE_SYM
714 715 716
%token  VARYING                       /* SQL-2003-R */
%token  VAR_SAMP_SYM
%token  VIEW_SYM                      /* SQL-2003-N */
717
%token  WAIT_SYM
718 719
%token  WARNINGS
%token  WEEK_SYM
720 721
%token  WHEN_SYM                      /* SQL-2003-R */
%token  WHERE                         /* SQL-2003-R */
722
%token  WHILE_SYM
723 724
%token  WITH                          /* SQL-2003-R */
%token  WORK_SYM                      /* SQL-2003-N */
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
725
%token  WRAPPER_SYM
726
%token  WRITE_SYM                     /* SQL-2003-N */
727 728 729 730
%token  X509_SYM
%token  XA_SYM
%token  XOR
%token  YEAR_MONTH_SYM
731
%token  YEAR_SYM                      /* SQL-2003-R */
732
%token  ZEROFILL
733

734
%left   JOIN_SYM INNER_SYM STRAIGHT_JOIN CROSS LEFT RIGHT
timour@mysql.com's avatar
timour@mysql.com committed
735 736
/* 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
737
%left   SET_VAR
738 739
%left	OR_OR_SYM OR_SYM OR2_SYM XOR
%left	AND_SYM AND_AND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
740 741 742 743 744 745
%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	'-' '+'
746
%left	'*' '/' '%' DIV_SYM MOD_SYM
747
%left   '^'
monty@mysql.com's avatar
monty@mysql.com committed
748
%left	NEG '~'
749
%right	NOT_SYM NOT2_SYM
750
%right	BINARY COLLATE_SYM
751

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

%type <lex_str_ptr>
760
	opt_table_alias
bk@work.mysql.com's avatar
bk@work.mysql.com committed
761 762

%type <table>
763
	table_ident table_ident_nodb references xid
bk@work.mysql.com's avatar
bk@work.mysql.com committed
764 765

%type <simple_string>
766
	remember_name remember_end opt_ident opt_db text_or_password
767
	opt_constraint constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
768 769

%type <string>
770
	text_string opt_gconcat_separator
bk@work.mysql.com's avatar
bk@work.mysql.com committed
771 772

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

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

787
%type <ulonglong_number>
788
	ulonglong_num real_ulonglong_num size_number
bk@work.mysql.com's avatar
bk@work.mysql.com committed
789

790
%type <p_elem_value>
791 792
        part_bit_expr

793 794 795
%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
796
%type <item>
797
	literal text_literal insert_ident order_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
798
	simple_ident select_item2 expr opt_expr opt_else sum_expr in_sum_expr
799
	variable variable_aux bool_term bool_factor bool_test bool_pri 
800 801
	predicate bit_expr bit_term bit_factor value_expr term factor
	table_wild simple_expr udf_expr
802
	expr_or_default set_expr_or_default interval_expr
803
	param_marker geometry_function
804
	signed_literal now_or_signed_literal opt_escape
805 806
	sp_opt_default
	simple_ident_nospvar simple_ident_q
807
        field_or_var limit_option
808
        part_func_expr
809 810 811 812
        function_call_keyword
        function_call_nonkeyword
        function_call_generic
        function_call_conflict
813 814 815

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

%type <item_list>
818
	expr_list udf_expr_list udf_expr_list2 when_list
819
	ident_list ident_list_arg opt_expr_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
820

821 822 823
%type <var_type>
        option_type opt_var_type opt_var_ident_type

bk@work.mysql.com's avatar
bk@work.mysql.com committed
824
%type <key_type>
825
	key_type opt_unique_or_fulltext constraint_key_type
bk@work.mysql.com's avatar
bk@work.mysql.com committed
826

827
%type <key_alg>
828
	btree_or_rtree
829

bk@work.mysql.com's avatar
bk@work.mysql.com committed
830
%type <string_list>
831
	key_usage_list using_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
832 833 834 835 836

%type <key_part>
	key_part

%type <table_list>
837
	join_table_list  join_table
838
        table_factor table_ref
839
        select_derived derived_table_list
bk@work.mysql.com's avatar
bk@work.mysql.com committed
840

841
%type <date_time_type> date_time_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
842 843
%type <interval> interval

844 845
%type <interval_time_st> interval_time_st

846
%type <db_type> storage_engines
bk@work.mysql.com's avatar
bk@work.mysql.com committed
847 848 849

%type <row_type> row_types

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

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

854
%type <cast_type> cast_type
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
855

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

858
%type <symbol> keyword keyword_sp
bk@work.mysql.com's avatar
bk@work.mysql.com committed
859

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

862
%type <charset>
863
	opt_collate
864 865
	charset_name
	charset_name_or_default
866 867
	old_or_new_charset_name
	old_or_new_charset_name_or_default
868 869
	collation_name
	collation_name_or_default
870

871 872
%type <variable> internal_variable_name

873
%type <select_lex> subselect subselect_init
874
	get_select_lex
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
875

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

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

924
%type <NONE> call sp_proc_stmts sp_proc_stmts1 sp_proc_stmt
925 926 927
%type <NONE> sp_proc_stmt_statement sp_proc_stmt_return
%type <NONE> sp_proc_stmt_if sp_proc_stmt_case_simple sp_proc_stmt_case
%type <NONE> sp_labeled_control sp_proc_stmt_unlabeled sp_proc_stmt_leave
pem@mysql.com's avatar
pem@mysql.com committed
928
%type <NONE> sp_proc_stmt_iterate
929 930
%type <NONE> sp_proc_stmt_open sp_proc_stmt_fetch sp_proc_stmt_close

931 932 933 934 935 936
%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
937 938
%type <NONE>
	'-' '+' '*' '/' '%' '(' ')'
939
	',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_SYM BETWEEN_SYM CASE_SYM
940
	THEN_SYM WHEN_SYM DIV_SYM MOD_SYM OR2_SYM AND_AND_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
941 942 943 944 945
%%


query:
	END_OF_INPUT
946
	{
947
	   THD *thd= YYTHD;
948
	   if (!thd->bootstrap &&
949
	      (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
950
	   {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
951
	     my_message(ER_EMPTY_QUERY, ER(ER_EMPTY_QUERY), MYF(0));
952
	     YYABORT;
953
	   }
954 955
	   else
	   {
956
	     thd->lex->sql_command= SQLCOM_EMPTY_QUERY;
957
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
958
	}
959
	| verb_clause END_OF_INPUT {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
960 961

verb_clause:
962 963 964 965 966 967
	  statement
	| begin
	;

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

sergefp@mysql.com's avatar
sergefp@mysql.com committed
1021
deallocate:
1022
        deallocate_or_drop PREPARE_SYM ident
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1023 1024
        {
          THD *thd=YYTHD;
1025
          LEX *lex= thd->lex;
1026
          if (lex->stmt_prepare_mode)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1027 1028 1029 1030
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
1031
          lex->sql_command= SQLCOM_DEALLOCATE_PREPARE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1032 1033 1034
          lex->prepared_stmt_name= $3;
        };

1035 1036 1037 1038 1039 1040
deallocate_or_drop:
	DEALLOCATE_SYM |
	DROP
	;


sergefp@mysql.com's avatar
sergefp@mysql.com committed
1041
prepare:
1042
        PREPARE_SYM ident FROM prepare_src
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1043 1044
        {
          THD *thd=YYTHD;
1045
          LEX *lex= thd->lex;
1046
          if (lex->stmt_prepare_mode)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1047 1048 1049 1050
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
1051
          lex->sql_command= SQLCOM_PREPARE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1052 1053 1054
          lex->prepared_stmt_name= $2;
        };

1055 1056 1057 1058 1059 1060
prepare_src:
        TEXT_STRING_sys
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $1;
1061
          lex->prepared_stmt_code_is_varref= FALSE;
1062 1063 1064 1065 1066 1067
        }
        | '@' ident_or_text
        {
          THD *thd=YYTHD;
          LEX *lex= thd->lex;
          lex->prepared_stmt_code= $2;
1068
          lex->prepared_stmt_code_is_varref= TRUE;
1069
        };
1070

sergefp@mysql.com's avatar
sergefp@mysql.com committed
1071 1072 1073 1074
execute:
        EXECUTE_SYM ident
        {
          THD *thd=YYTHD;
1075
          LEX *lex= thd->lex;
1076
          if (lex->stmt_prepare_mode)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1077 1078 1079 1080
          {
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
          }
1081
          lex->sql_command= SQLCOM_EXECUTE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
          lex->prepared_stmt_name= $2;
        }
        execute_using
        {}
        ;

execute_using:
        /* nothing */
        | USING execute_var_list
        ;

execute_var_list:
1094 1095
        execute_var_list ',' execute_var_ident
        | execute_var_ident
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1096 1097 1098 1099 1100 1101 1102 1103
        ;

execute_var_ident: '@' ident_or_text
        {
          LEX *lex=Lex;
          LEX_STRING *lexstr= (LEX_STRING*)sql_memdup(&$2, sizeof(LEX_STRING));
          if (!lexstr || lex->prepared_stmt_params.push_back(lexstr))
              YYABORT;
1104
        }
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1105 1106
        ;

1107 1108
/* help */

1109
help:
1110 1111 1112 1113 1114 1115 1116 1117 1118
       HELP_SYM
       {
         if (Lex->sphead)
         {
           my_error(ER_SP_BADSTATEMENT, MYF(0), "HELP");
           YYABORT;
         }
       }
       ident_or_text
1119
       {
1120 1121
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_HELP;
1122
	  lex->help_arg= $3.str;
1123
       };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1124 1125 1126 1127

/* change master */

change:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
1128
       CHANGE MASTER_SYM TO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1129 1130 1131
        {
	  LEX *lex = Lex;
	  lex->sql_command = SQLCOM_CHANGE_MASTER;
1132
	  bzero((char*) &lex->mi, sizeof(lex->mi));
1133 1134 1135 1136
        }
       master_defs
	{}
       ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1137 1138 1139

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

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
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
1158
       MASTER_PORT_SYM EQ ulong_num
1159 1160 1161 1162
       {
	 Lex->mi.port = $3;
       }
       |
serg@serg.mylan's avatar
serg@serg.mylan committed
1163
       MASTER_CONNECT_RETRY_SYM EQ ulong_num
1164 1165 1166
       {
	 Lex->mi.connect_retry = $3;
       }
serg@serg.mylan's avatar
serg@serg.mylan committed
1167
       | MASTER_SSL_SYM EQ ulong_num
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
         {
           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
1221
       | RELAY_LOG_POS_SYM EQ ulong_num
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
         {
           Lex->mi.relay_log_pos = $3;
           /* Adjust if < BIN_LOG_HEADER_SIZE (same comment as Lex->mi.pos) */
           Lex->mi.relay_log_pos = max(BIN_LOG_HEADER_SIZE, Lex->mi.relay_log_pos);
         }
       ;

/* create a table */

create:
	CREATE opt_table_options TABLE_SYM opt_if_not_exists table_ident
	{
	  THD *thd= YYTHD;
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_CREATE_TABLE;
	  if (!lex->select_lex.add_table_to_list(thd, $5, NULL,
						 TL_OPTION_UPDATING,
						 (using_update_log ?
						  TL_READ_NO_INSERT:
						  TL_READ)))
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
	  lex->change=NullS;
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	  lex->create_info.options=$2 | $4;
1249
	  lex->create_info.db_type= lex->thd->variables.table_type;
1250
	  lex->create_info.default_table_charset= NULL;
1251 1252
	  lex->name.str= 0;
          lex->name.length= 0;
mikael@zim.(none)'s avatar
mikael@zim.(none) committed
1253
         lex->like_name= 0;
1254 1255 1256
	}
	create2
	  { Lex->current_select= &Lex->select_lex; }
1257 1258
	| CREATE opt_unique_or_fulltext INDEX_SYM ident key_alg ON
	  table_ident
1259 1260 1261
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_CREATE_INDEX;
1262 1263
	    if (!lex->current_select->add_table_to_list(lex->thd, $7,
							NULL,
1264 1265 1266 1267 1268 1269 1270
							TL_OPTION_UPDATING))
	      YYABORT;
	    lex->create_list.empty();
	    lex->key_list.empty();
	    lex->col_list.empty();
	    lex->change=NullS;
	  }
1271
	   '(' key_list ')' key_options
1272 1273
	  {
	    LEX *lex=Lex;
1274
	    if ($2 != Key::FULLTEXT && lex->key_create_info.parser_name.str)
1275 1276 1277 1278
	    {
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
	    }
1279
	    lex->key_list.push_back(new Key($2, $4.str, &lex->key_create_info, 0,
1280
					   lex->col_list));
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
	    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;
1292
	    lex->name= $4;
1293 1294
            lex->create_info.options=$3;
	  }
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
	| CREATE
	  {
            Lex->create_view_mode= VIEW_CREATE_NEW;
            Lex->create_view_algorithm= VIEW_ALGORITHM_UNDEFINED;
            Lex->create_view_suid= TRUE;
	  }
	  view_or_trigger_or_sp_or_event
	  {}
	| CREATE USER clear_privileges grant_list
	  {
	    Lex->sql_command = SQLCOM_CREATE_USER;
          }
	| CREATE LOGFILE_SYM GROUP logfile_group_info 
          {
1309
            Lex->alter_tablespace_info->ts_cmd_type= CREATE_LOGFILE_GROUP;
1310 1311 1312
          }
        | CREATE TABLESPACE tablespace_info
          {
1313
            Lex->alter_tablespace_info->ts_cmd_type= CREATE_TABLESPACE;
1314
          }
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1315 1316 1317 1318
	| CREATE server_def
	  {
	    Lex->sql_command= SQLCOM_CREATE_SERVER;
          }
1319
	;
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1320 1321 1322 1323 1324 1325 1326 1327
server_def:
        SERVER_SYM ident_or_text FOREIGN DATA_SYM WRAPPER_SYM ident_or_text OPTIONS_SYM '(' server_options_list ')'
        {
          Lex->server_options.server_name= $2.str;
          Lex->server_options.server_name_length= $2.length;
          Lex->server_options.scheme= $6.str;
        }
        ;
1328

patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
server_options_list:
        server_option
        | server_options_list ',' server_option
        ;

server_option:
        USER TEXT_STRING_sys
        {
          Lex->server_options.username= $2.str;
        }
        |
        HOST_SYM TEXT_STRING_sys
        {
          Lex->server_options.host= $2.str;
        }
        |
        DATABASE TEXT_STRING_sys
        {
          Lex->server_options.db= $2.str;
        }
        |
        OWNER_SYM TEXT_STRING_sys
        {
          Lex->server_options.owner= $2.str;
        }
        |
        PASSWORD TEXT_STRING_sys
        {
          Lex->server_options.password= $2.str;
        }
        |
        SOCKET_SYM TEXT_STRING_sys
        {
          Lex->server_options.socket= $2.str;
        }
        |
        PORT_SYM ulong_num
        {
          Lex->server_options.port= $2;
        }
        ;
1370 1371 1372

event_tail:
          EVENT_SYM opt_if_not_exists sp_name
1373 1374
          /*
             BE CAREFUL when you add a new rule to update the block where
1375 1376
             YYTHD->client_capabilities is set back to original value
          */
1377
          {
1378
            Lex->create_info.options= $2;
1379

1380
            if (!(Lex->event_parse_data= Event_parse_data::new_instance(YYTHD)))
andrey@lmy004's avatar
andrey@lmy004 committed
1381
              YYABORT;
1382
            Lex->event_parse_data->identifier= $3;
1383

1384 1385 1386 1387 1388
            /*
              We have to turn of CLIENT_MULTI_QUERIES while parsing a
              stored procedure, otherwise yylex will chop it into pieces
              at each ';'.
            */
andrey@lmy004's avatar
andrey@lmy004 committed
1389 1390 1391
            $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
            YYTHD->client_capabilities &= (~CLIENT_MULTI_QUERIES);

1392
            Lex->sql_command= SQLCOM_CREATE_EVENT;
1393
            /* We need that for disallowing subqueries */
1394 1395
          }
          ON SCHEDULE_SYM ev_schedule_time
1396 1397 1398
          opt_ev_on_completion
          opt_ev_status
          opt_ev_comment
1399 1400
          DO_SYM ev_sql_stmt
          {
andrey@lmy004's avatar
andrey@lmy004 committed
1401
            /*
1402
              Restore flag if it was cleared above
1403 1404 1405 1406
              $1 - EVENT_SYM
              $2 - opt_if_not_exists
              $3 - sp_name
              $4 - the block above
andrey@lmy004's avatar
andrey@lmy004 committed
1407
            */
1408
            YYTHD->client_capabilities |= $<ulong_num>4;
1409

1410 1411 1412 1413
            /*
              sql_command is set here because some rules in ev_sql_stmt
              can overwrite it
            */
andrey@lmy004's avatar
andrey@lmy004 committed
1414
            Lex->sql_command= SQLCOM_CREATE_EVENT;
1415
          }
1416
      ;
1417 1418 1419

ev_schedule_time: EVERY_SYM expr interval
	  {
1420 1421
            Lex->event_parse_data->item_expression= $2;
            Lex->event_parse_data->interval= $3;
1422 1423 1424 1425 1426
          }
          ev_starts
          ev_ends
        | AT_SYM expr
          {
1427
            Lex->event_parse_data->item_execute_at= $2;
1428
          }
1429
        ;
1430 1431

opt_ev_status: /* empty */ { $$= 0; }
1432
        | ENABLE_SYM
1433
          {
1434
            Lex->event_parse_data->status= Event_parse_data::ENABLED;
1435
            $$= 1;
1436
          }
1437
        | DISABLE_SYM
1438
          {
1439
            Lex->event_parse_data->status= Event_parse_data::DISABLED;
1440
            $$= 1;
1441 1442 1443 1444
          }
      ;

ev_starts: /* empty */
1445
          {
1446
            Lex->event_parse_data->item_starts= new Item_func_now_local();
1447
          }
1448 1449
        | STARTS_SYM expr
          {
1450
            Lex->event_parse_data->item_starts= $2;
1451 1452 1453 1454 1455 1456
          }
      ;

ev_ends: /* empty */
        | ENDS_SYM expr
          {
1457
            Lex->event_parse_data->item_ends= $2;
1458 1459 1460
          }
      ;

1461
opt_ev_on_completion: /* empty */ { $$= 0; }
1462 1463 1464
        | ev_on_completion
      ;

1465
ev_on_completion:
1466
          ON COMPLETION_SYM PRESERVE_SYM
1467
          {
1468 1469
            Lex->event_parse_data->on_completion=
                                  Event_parse_data::ON_COMPLETION_PRESERVE;
1470
            $$= 1;
1471 1472 1473
          }
        | ON COMPLETION_SYM NOT_SYM PRESERVE_SYM
          {
1474 1475
            Lex->event_parse_data->on_completion=
                                  Event_parse_data::ON_COMPLETION_DROP;
1476
            $$= 1;
1477 1478
          }
      ;
1479

1480
opt_ev_comment: /* empty */ { $$= 0; }
1481 1482
        | COMMENT_SYM TEXT_STRING_sys
          {
1483
            Lex->comment= Lex->event_parse_data->comment= $2;
1484
            $$= 1;
1485 1486 1487 1488 1489 1490
          }
      ;

ev_sql_stmt:
          {
            LEX *lex= Lex;
1491

1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
            /*
              This stops the following :
              - CREATE EVENT ... DO CREATE EVENT ...;
              - ALTER  EVENT ... DO CREATE EVENT ...;
              - CREATE EVENT ... DO ALTER EVENT DO ....;
              - CREATE PROCEDURE ... BEGIN CREATE EVENT ... END|
              This allows:
              - CREATE EVENT ... DO DROP EVENT yyy;
              - CREATE EVENT ... DO ALTER EVENT yyy;
                (the nested ALTER EVENT can have anything but DO clause)
              - ALTER  EVENT ... DO ALTER EVENT yyy;
                (the nested ALTER EVENT can have anything but DO clause)
              - ALTER  EVENT ... DO DROP EVENT yyy;
              - CREATE PROCEDURE ... BEGIN ALTER EVENT ... END|
                (the nested ALTER EVENT can have anything but DO clause)
              - CREATE PROCEDURE ... BEGIN DROP EVENT ... END|
            */
            if (lex->sphead)
andrey@lmy004's avatar
andrey@lmy004 committed
1510
            {
1511 1512 1513 1514 1515 1516
              my_error(ER_EVENT_RECURSIVITY_FORBIDDEN, MYF(0));
              YYABORT;
            }
              
            if (!(lex->sphead= new sp_head()))
              YYABORT;
1517

1518 1519
            lex->sphead->reset_thd_mem_root(YYTHD);
            lex->sphead->init(lex);
1520
            lex->sphead->init_sp_name(YYTHD, Lex->event_parse_data->identifier);
1521

1522
            lex->sphead->m_type= TYPE_ENUM_PROCEDURE;
1523

1524 1525
            bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
            lex->sphead->m_chistics= &lex->sp_chistics;
1526

1527
            lex->sphead->m_body_begin= lex->ptr;
1528 1529
            
            Lex->event_parse_data->body_begin= lex->ptr;
andrey@lmy004's avatar
andrey@lmy004 committed
1530

1531 1532 1533 1534
          }
          ev_sql_stmt_inner
          {
            LEX *lex=Lex;
1535

1536
            /* return back to the original memory root ASAP */
1537
            lex->sphead->init_strings(YYTHD, lex);
1538
            lex->sphead->restore_thd_mem_root(YYTHD);
1539

1540
            lex->sp_chistics.suid= SP_IS_SUID;  //always the definer!
1541

1542
            Lex->event_parse_data->init_body(YYTHD);
1543 1544
          }
      ;
1545

1546 1547 1548 1549 1550 1551
ev_sql_stmt_inner:
          sp_proc_stmt_statement
        | sp_proc_stmt_return
        | sp_proc_stmt_if
        | sp_proc_stmt_case_simple
        | sp_proc_stmt_case
1552
        | sp_labeled_control
1553 1554 1555 1556 1557 1558 1559
        | sp_proc_stmt_unlabeled
        | sp_proc_stmt_leave
        | sp_proc_stmt_iterate
        | sp_proc_stmt_open
        | sp_proc_stmt_fetch
        | sp_proc_stmt_close
      ;
1560

1561

1562 1563 1564 1565 1566 1567 1568
clear_privileges:
        /* Nothing */
        {
          LEX *lex=Lex;
          lex->users_list.empty();
          lex->columns.empty();
          lex->grant= lex->grant_tot_col= 0;
1569
	  lex->all_privileges= 0;
1570 1571 1572 1573 1574 1575 1576
          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));
        }
        ;

1577
sp_name:
1578
	  ident '.' ident
1579
	  {
1580
            if (!$1.str || check_db_name(&$1))
1581 1582 1583 1584
            {
	      my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
	      YYABORT;
	    }
1585
	    if (check_routine_name($3))
1586 1587 1588 1589
            {
	      my_error(ER_SP_WRONG_NAME, MYF(0), $3.str);
	      YYABORT;
	    }
1590 1591 1592
	    $$= new sp_name($1, $3);
	    $$->init_qname(YYTHD);
	  }
1593
	| ident
1594
	  {
1595 1596
            THD *thd= YYTHD;
            LEX_STRING db;
1597
	    if (check_routine_name($1))
1598 1599 1600 1601
            {
	      my_error(ER_SP_WRONG_NAME, MYF(0), $1.str);
	      YYABORT;
	    }
1602 1603 1604 1605 1606
            if (thd->copy_db_to(&db.str, &db.length))
              YYABORT;
	    $$= new sp_name(db, $1);
            if ($$)
	      $$->init_qname(YYTHD);
1607 1608 1609 1610
	  }
	;

create_function_tail:
1611
	  RETURNS_SYM udf_type SONAME_SYM TEXT_STRING_sys
1612
	  {
1613
            THD *thd= YYTHD;
1614
	    LEX *lex=Lex;
1615 1616 1617
            if (lex->definer != NULL)
            {
              /*
1618 1619 1620 1621
                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.
1622 1623 1624 1625
              */
	      my_error(ER_WRONG_USAGE, MYF(0), "SONAME", "DEFINER");
              YYABORT;
            }
1626 1627 1628 1629 1630 1631
            if (is_native_function(thd, & lex->spname->m_name))
            {
              my_error(ER_NATIVE_FCT_NAME_COLLISION, MYF(0),
                       lex->spname->m_name.str);
              YYABORT;
            }
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
	    lex->sql_command = SQLCOM_CREATE_FUNCTION;
	    lex->udf.name = lex->spname->m_name;
	    lex->udf.returns=(Item_result) $2;
	    lex->udf.dl=$4.str;
	  }
	| '('
	  {
	    LEX *lex= Lex;
	    sp_head *sp;

1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
            /* 
              First check if AGGREGATE was used, in that case it's a
              syntax error.
            */
            if (lex->udf.type == UDFTYPE_AGGREGATE)
            {
              my_error(ER_SP_NO_AGGREGATE, MYF(0));
              YYABORT;
            }

1652 1653
	    if (lex->sphead)
	    {
1654
	      my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "FUNCTION");
1655 1656 1657 1658 1659 1660
	      YYABORT;
	    }
	    /* Order is important here: new - reset - init */
	    sp= new sp_head();
	    sp->reset_thd_mem_root(YYTHD);
	    sp->init(lex);
1661
            sp->init_sp_name(YYTHD, lex->spname);
1662 1663 1664 1665

	    sp->m_type= TYPE_ENUM_FUNCTION;
	    lex->sphead= sp;
	    /*
1666 1667 1668 1669
	      We have to turn off CLIENT_MULTI_QUERIES while parsing a
	      stored procedure, otherwise yylex will chop it into pieces
	      at each ';'.
	    */
andrey@lmy004's avatar
andrey@lmy004 committed
1670
            $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
	    YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
	    lex->sphead->m_param_begin= lex->tok_start+1;
	  }
          sp_fdparam_list ')'
	  {
	    LEX *lex= Lex;

	    lex->sphead->m_param_end= lex->tok_start;
	  }
	  RETURNS_SYM
	  {
	    LEX *lex= Lex;
1683 1684 1685 1686
	    lex->charset= NULL;
	    lex->length= lex->dec= NULL;
	    lex->interval_list.empty();
	    lex->type= 0;
1687 1688 1689 1690 1691
	  }
	  type
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
            /*
              This was disabled in 5.1.12. See bug #20701
              When collation support in SP is implemented, then this test
              should be removed.
            */
            if (($8 == FIELD_TYPE_STRING || $8 == MYSQL_TYPE_VARCHAR)
                && (lex->type & BINCMP_FLAG))
            {
              my_error(ER_NOT_SUPPORTED_YET, MYF(0), "return value collation");
              YYABORT;
            }
1703

1704 1705 1706 1707
            if (sp->fill_field_definition(YYTHD, lex,
                                          (enum enum_field_types) $8,
                                          &sp->m_return_field_def))
              YYABORT;
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719

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

	    lex->sphead->m_chistics= &lex->sp_chistics;
	    lex->sphead->m_body_begin= lex->tok_start;
	  }
	  sp_proc_stmt
	  {
1720
            THD *thd= YYTHD;
1721 1722 1723
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;

1724 1725 1726
            if (sp->is_not_allowed_in_function("function"))
              YYABORT;

1727
	    lex->sql_command= SQLCOM_CREATE_SPFUNCTION;
1728
	    sp->init_strings(thd, lex);
1729 1730 1731 1732 1733
            if (!(sp->m_flags & sp_head::HAS_RETURN))
            {
              my_error(ER_SP_NORETURN, MYF(0), sp->m_qname.str);
              YYABORT;
            }
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
            if (is_native_function(thd, & sp->m_name))
            {
              /*
                This warning will be printed when
                [1] A client query is parsed,
                [2] A stored function is loaded by db_load_routine.
                Printing the warning for [2] is intentional, to cover the
                following scenario:
                - A user define a SF 'foo' using MySQL 5.N
                - An application uses select foo(), and works.
                - MySQL 5.{N+1} defines a new native function 'foo', as
                part of a new feature.
                - MySQL 5.{N+1} documentation is updated, and should mention
                that there is a potential incompatible change in case of
                existing stored function named 'foo'.
                - The user deploys 5.{N+1}. At this point, 'select foo()'
                means something different, and the user code is most likely
                broken (it's only safe if the code is 'select db.foo()').
                With a warning printed when the SF is loaded (which has to occur
                before the call), the warning will provide a hint explaining
                the root cause of a later failure of 'select foo()'.
                With no warning printed, the user code will fail with no
                apparent reason.
                Printing a warning each time db_load_routine is executed for
                an ambiguous function is annoying, since that can happen a lot,
                but in practice should not happen unless there *are* name
                collisions.
                If a collision exists, it should not be silenced but fixed.
              */
              push_warning_printf(thd,
                                  MYSQL_ERROR::WARN_LEVEL_NOTE,
                                  ER_NATIVE_FCT_NAME_COLLISION,
                                  ER(ER_NATIVE_FCT_NAME_COLLISION),
                                  sp->m_name.str);
            }
1769
	    /* Restore flag if it was cleared above */
1770 1771
	    thd->client_capabilities |= $<ulong_num>2;
	    sp->restore_thd_mem_root(thd);
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
	  }
	;

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; }
1807
	| not DETERMINISTIC_SYM { Lex->sp_chistics.detistic= FALSE; }
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
	;

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();
1829
	    sp_add_used_routine(lex, YYTHD, $2, TYPE_ENUM_PROCEDURE);
1830
	  }
1831
          opt_sp_cparam_list {}
1832 1833 1834
	;

/* CALL parameters */
1835
opt_sp_cparam_list:
1836
	  /* Empty */
1837
	| '(' opt_sp_cparams ')'
1838 1839
	;

1840 1841 1842 1843 1844
opt_sp_cparams:
          /* Empty */
        | sp_cparams
        ;

1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
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
	;

1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
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;
	  }
	;

1887
sp_fdparam:
1888
	  ident sp_init_param type
1889 1890 1891 1892
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1893
	    if (spc->find_variable(&$1, TRUE))
1894
	    {
1895
	      my_error(ER_SP_DUP_PARAM, MYF(0), $1.str);
1896 1897
	      YYABORT;
	    }
1898 1899 1900
            sp_variable_t *spvar= spc->push_variable(&$1,
                                                     (enum enum_field_types)$3,
                                                     sp_param_in);
1901 1902 1903

            if (lex->sphead->fill_field_definition(YYTHD, lex,
                                                   (enum enum_field_types) $3,
1904
                                                   &spvar->field_def))
1905 1906 1907
            {
              YYABORT;
            }
1908 1909
            spvar->field_def.field_name= spvar->name.str;
            spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
	  }
	;

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

sp_pdparams:
	  sp_pdparams ',' sp_pdparam
	| sp_pdparam
	;

sp_pdparam:
1925
	  sp_opt_inout sp_init_param ident type
1926 1927 1928 1929
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

1930
	    if (spc->find_variable(&$3, TRUE))
1931
	    {
1932
	      my_error(ER_SP_DUP_PARAM, MYF(0), $3.str);
1933 1934
	      YYABORT;
	    }
1935 1936 1937
            sp_variable_t *spvar= spc->push_variable(&$3,
                                                     (enum enum_field_types)$4,
                                                     (sp_param_mode_t)$1);
1938 1939 1940

            if (lex->sphead->fill_field_definition(YYTHD, lex,
                                                   (enum enum_field_types) $4,
1941
                                                   &spvar->field_def))
1942 1943 1944
            {
              YYABORT;
            }
1945 1946
            spvar->field_def.field_name= spvar->name.str;
            spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
	  }
	;

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 */ {}
1959
	| sp_proc_stmts  sp_proc_stmt ';'
1960 1961 1962 1963
	;

sp_proc_stmts1:
	  sp_proc_stmt ';' {}
1964
	| sp_proc_stmts1  sp_proc_stmt ';'
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
	;

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
1980 1981
	      my_message(ER_SP_VARCOND_AFTER_CURSHNDLR,
                         ER(ER_SP_VARCOND_AFTER_CURSHNDLR), MYF(0));
1982 1983 1984 1985
	      YYABORT;
	    }
	    if ($2.curs && $1.hndlrs)
	    { /* Cursor following handler */
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1986 1987
	      my_message(ER_SP_CURSOR_AFTER_HANDLER,
                         ER(ER_SP_CURSOR_AFTER_HANDLER), MYF(0));
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
	      YYABORT;
	    }
	    $$.vars= $1.vars + $2.vars;
	    $$.conds= $1.conds + $2.conds;
	    $$.hndlrs= $1.hndlrs + $2.hndlrs;
	    $$.curs= $1.curs + $2.curs;
	  }
	;

sp_decl:
1998
          DECLARE_SYM sp_decl_idents
1999
          {
2000
            LEX *lex= Lex;
2001

2002 2003 2004
            lex->sphead->reset_lex(YYTHD);
            lex->spcont->declare_var_boundary($2);
          }
2005
          type
2006
          sp_opt_default
2007 2008
          {
            LEX *lex= Lex;
2009
            sp_pcontext *pctx= lex->spcont;
2010
            uint num_vars= pctx->context_var_count();
2011 2012 2013 2014 2015
            enum enum_field_types var_type= (enum enum_field_types) $4;
            Item *dflt_value_item= $5;
            create_field *create_field_op;
            
            if (!dflt_value_item)
2016
            {
2017 2018 2019 2020 2021 2022
              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++)
            {
2023 2024
              uint var_idx= pctx->var_context2runtime(i);
              sp_variable_t *spvar= pctx->find_variable(var_idx);
2025
            
2026
              if (!spvar)
2027 2028
                YYABORT;
            
2029 2030
              spvar->type= var_type;
              spvar->dflt= dflt_value_item;
2031 2032
            
              if (lex->sphead->fill_field_definition(YYTHD, lex, var_type,
2033
                                                     &spvar->field_def))
2034 2035 2036 2037
              {
                YYABORT;
              }
            
2038 2039
              spvar->field_def.field_name= spvar->name.str;
              spvar->field_def.pack_flag |= FIELDFLAG_MAYBE_NULL;
2040 2041 2042 2043 2044 2045 2046
            
              /* 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)));
2047
            }
2048 2049

            pctx->declare_var_boundary(0);
2050
            lex->sphead->restore_lex(YYTHD);
2051

2052 2053 2054
            $$.vars= $2;
            $$.conds= $$.hndlrs= $$.curs= 0;
          }
2055 2056 2057 2058 2059 2060 2061
	| DECLARE_SYM ident CONDITION_SYM FOR_SYM sp_cond
	  {
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

	    if (spc->find_cond(&$2, TRUE))
	    {
2062
	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
	      YYABORT;
	    }
	    YYTHD->lex->spcont->push_cond(&$2, $5);
	    $$.vars= $$.hndlrs= $$.curs= 0;
	    $$.conds= 1;
	  }
	| DECLARE_SYM sp_handler_type HANDLER_SYM FOR_SYM
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_instr_hpush_jump *i=
              new sp_instr_hpush_jump(sp->instructions(), ctx, $2,
2076
	                              ctx->current_var_count());
2077 2078 2079

	    sp->add_instr(i);
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
2080
	    sp->m_flags|= sp_head::IN_HANDLER;
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
	  }
	  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,
2093
	                              ctx->current_var_count());
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
	      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);
2104
	    sp->m_flags&= ~sp_head::IN_HANDLER;
2105 2106
	    $$.vars= $$.conds= $$.curs= 0;
	    $$.hndlrs= $6;
2107
	    ctx->add_handlers($6);
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
	  }
	| 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))
	    {
2119
	      my_error(ER_SP_DUP_CURS, MYF(0), $2.str);
2120 2121 2122
	      delete $5;
	      YYABORT;
	    }
pem@mysql.com's avatar
pem@mysql.com committed
2123
            i= new sp_instr_cpush(sp->instructions(), ctx, $5,
2124
                                  ctx->current_cursor_count());
2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
	    sp->add_instr(i);
	    ctx->push_cursor(&$2);
	    $$.vars= $$.conds= $$.hndlrs= 0;
	    $$.curs= 1;
	  }
	;

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

2136 2137 2138 2139 2140 2141
	    /*
	      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.
	    */
2142 2143 2144 2145 2146
	  }
	  statement
	  {
	    LEX *lex= Lex;

2147 2148
	    if (lex->sql_command != SQLCOM_SELECT &&
	       !(sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND))
2149
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2150 2151
	      my_message(ER_SP_BAD_CURSOR_QUERY, ER(ER_SP_BAD_CURSOR_QUERY),
                         MYF(0));
2152 2153 2154 2155
	      YYABORT;
	    }
	    if (lex->result)
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2156 2157
	      my_message(ER_SP_BAD_CURSOR_SELECT, ER(ER_SP_BAD_CURSOR_SELECT),
                         MYF(0));
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
	      YYABORT;
	    }
	    lex->sp_lex_in_use= TRUE;
	    $$= lex;
	    lex->sphead->restore_lex(YYTHD);
	  }
	;

sp_handler_type:
	  EXIT_SYM      { $$= SP_HANDLER_EXIT; }
	| CONTINUE_SYM  { $$= SP_HANDLER_CONTINUE; }
/*	| UNDO_SYM      { QQ No yet } */
	;

sp_hcond_list:
	  sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
	    sp_pcontext *ctx= lex->spcont;

	    if (ctx->find_handler($1))
	    {
	      my_message(ER_SP_DUP_HANDLER, ER(ER_SP_DUP_HANDLER), MYF(0));
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_hpush_jump *i=
                (sp_instr_hpush_jump *)sp->last_instruction();
2188

2189 2190 2191 2192
	      i->add_condition($1);
	      ctx->push_handler($1);
	      $$= 1;
	    }
2193 2194 2195 2196 2197
	  }
	| sp_hcond_list ',' sp_hcond
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
	    sp_pcontext *ctx= lex->spcont;

	    if (ctx->find_handler($3))
	    {
	      my_message(ER_SP_DUP_HANDLER, ER(ER_SP_DUP_HANDLER), MYF(0));
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_hpush_jump *i=
	        (sp_instr_hpush_jump *)sp->last_instruction();
2209

2210 2211 2212 2213
	      i->add_condition($3);
	      ctx->push_handler($3);
	      $$= $1 + 1;
	    }
2214 2215 2216 2217
	  }
	;

sp_cond:
serg@serg.mylan's avatar
serg@serg.mylan committed
2218
	  ulong_num
2219 2220 2221 2222 2223 2224 2225
	  {			/* 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 */
2226 2227 2228 2229 2230
	    if (!sp_cond_check(&$3))
	    {
	      my_error(ER_SP_BAD_SQLSTATE, MYF(0), $3.str);
	      YYABORT;
	    }
2231 2232
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::state;
2233 2234
	    memcpy($$->sqlstate, $3.str, 5);
	    $$->sqlstate[5]= '\0';
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
	  }
	;

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

sp_hcond:
	  sp_cond
	  {
	    $$= $1;
	  }
	| ident			/* CONDITION name */
	  {
	    $$= Lex->spcont->find_cond(&$1);
	    if ($$ == NULL)
	    {
2253
	      my_error(ER_SP_COND_MISMATCH, MYF(0), $1.str);
2254 2255 2256 2257 2258 2259 2260 2261
	      YYABORT;
	    }
	  }
	| SQLWARNING_SYM	/* SQLSTATEs 01??? */
	  {
	    $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
	    $$->type= sp_cond_type_t::warning;
	  }
2262
	| not FOUND_SYM		/* SQLSTATEs 02??? */
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
	  {
	    $$= (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
	  {
2277 2278
            /* NOTE: field definition is filled in sp_decl section. */

2279 2280 2281
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

2282
	    if (spc->find_variable(&$1, TRUE))
2283
	    {
2284
	      my_error(ER_SP_DUP_VAR, MYF(0), $1.str);
2285 2286
	      YYABORT;
	    }
2287
	    spc->push_variable(&$1, (enum_field_types)0, sp_param_in);
2288 2289 2290 2291
	    $$= 1;
	  }
	| sp_decl_idents ',' ident
	  {
2292 2293
            /* NOTE: field definition is filled in sp_decl section. */

2294 2295 2296
	    LEX *lex= Lex;
	    sp_pcontext *spc= lex->spcont;

2297
	    if (spc->find_variable(&$3, TRUE))
2298
	    {
2299
	      my_error(ER_SP_DUP_VAR, MYF(0), $3.str);
2300 2301
	      YYABORT;
	    }
2302
	    spc->push_variable(&$3, (enum_field_types)0, sp_param_in);
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
	    $$= $1 + 1;
	  }
	;

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

sp_proc_stmt:
2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
	  sp_proc_stmt_statement
        | sp_proc_stmt_return
	| sp_proc_stmt_if
	| sp_proc_stmt_case_simple
        | sp_proc_stmt_case
	| sp_labeled_control
	| sp_proc_stmt_unlabeled
	| sp_proc_stmt_leave
	| sp_proc_stmt_iterate
	| sp_proc_stmt_open
	| sp_proc_stmt_fetch
        | sp_proc_stmt_close
        ;

sp_proc_stmt_if:
2328 2329 2330
        IF { Lex->sphead->new_cont_backpatch(NULL); }
        sp_if END IF
        { Lex->sphead->do_cont_backpatch(); }
2331 2332 2333
        ;
        
sp_proc_stmt_statement:
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
	  {
	    LEX *lex= Lex;

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

2345
            sp->m_flags|= sp_get_flags_for_command(lex);
2346 2347
	    if (lex->sql_command == SQLCOM_CHANGE_DB)
	    { /* "USE db" doesn't work in a procedure */
2348
	      my_error(ER_SP_BADSTATEMENT, MYF(0), "USE");
2349 2350
	      YYABORT;
	    }
2351 2352 2353 2354
	    /*
              Don't add an instruction for SET statements, since all
              instructions for them were already added during processing
              of "set" rule.
2355
	    */
2356 2357 2358
            DBUG_ASSERT(lex->sql_command != SQLCOM_SET_OPTION ||
                        lex->var_list.is_empty());
            if (lex->sql_command != SQLCOM_SET_OPTION)
2359
	    {
2360 2361
              sp_instr_stmt *i=new sp_instr_stmt(sp->instructions(),
                                                 lex->spcont, lex);
2362

2363 2364 2365 2366 2367 2368
              /*
                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)
2369 2370 2371 2372 2373 2374 2375
                i->m_query.length= lex->ptr - sp->m_tmp_query;
              else
                i->m_query.length= lex->tok_end - sp->m_tmp_query;
              i->m_query.str= strmake_root(YYTHD->mem_root,
                                           (char *)sp->m_tmp_query,
                                           i->m_query.length);
              sp->add_instr(i);
2376 2377 2378
            }
	    sp->restore_lex(YYTHD);
          }
2379 2380 2381 2382
        ;

sp_proc_stmt_return:
        RETURN_SYM 
2383 2384
          { Lex->sphead->reset_lex(YYTHD); }
          expr
2385 2386
	  {
	    LEX *lex= Lex;
2387
	    sp_head *sp= lex->sphead;
2388

2389
	    if (sp->m_type != TYPE_ENUM_FUNCTION)
2390
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2391
	      my_message(ER_SP_BADRETURN, ER(ER_SP_BADRETURN), MYF(0));
2392 2393 2394 2395 2396 2397
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_freturn *i;

2398 2399
	      i= new sp_instr_freturn(sp->instructions(), lex->spcont, $3,
                                      sp->m_return_field_def.sql_type, lex);
2400
	      sp->add_instr(i);
2401
	      sp->m_flags|= sp_head::HAS_RETURN;
2402
	    }
2403
	    sp->restore_lex(YYTHD);
2404
	  }
2405 2406 2407 2408
        ;

sp_proc_stmt_case_simple:
	CASE_SYM WHEN_SYM
2409
	  {
2410
	    Lex->sphead->m_flags&= ~sp_head::IN_SIMPLE_CASE;
2411
            Lex->sphead->new_cont_backpatch(NULL);
2412
	  }
2413
          sp_case END CASE_SYM { Lex->sphead->do_cont_backpatch(); }
2414 2415 2416 2417
        ;
        
sp_proc_stmt_case:
          CASE_SYM
2418 2419 2420 2421
          {
            Lex->sphead->reset_lex(YYTHD);
            Lex->sphead->new_cont_backpatch(NULL);
          }
2422
          expr WHEN_SYM
2423 2424
	  {
	    LEX *lex= Lex;
2425 2426 2427
	    sp_head *sp= lex->sphead;
	    sp_pcontext *parsing_ctx= lex->spcont;
	    int case_expr_id= parsing_ctx->register_case_expr();
2428
            sp_instr_set_case_expr *i;
2429 2430 2431
	    
	    if (parsing_ctx->push_case_expr_id(case_expr_id))
              YYABORT;
2432 2433 2434 2435 2436 2437 2438 2439

            i= new sp_instr_set_case_expr(sp->instructions(),
                                          parsing_ctx,
                                          case_expr_id,
                                          $3,
                                          lex);
            sp->add_cont_backpatch(i);
            sp->add_instr(i);
2440 2441
	    sp->m_flags|= sp_head::IN_SIMPLE_CASE;
	    sp->restore_lex(YYTHD);
2442 2443 2444
	  }
	  sp_case END CASE_SYM
	  {
2445
	    Lex->spcont->pop_case_expr_id();
2446
            Lex->sphead->do_cont_backpatch();
2447
	  }
2448 2449 2450 2451
        ;

sp_proc_stmt_unlabeled:
	  { /* Unlabeled controls get a secret label. */
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461
	    LEX *lex= Lex;

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

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
2462 2463 2464 2465
        ;

sp_proc_stmt_leave:
	  LEAVE_SYM label_ident
2466 2467 2468 2469 2470 2471 2472 2473
	  {
	    LEX *lex= Lex;
	    sp_head *sp = lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($2.str);

	    if (! lab)
	    {
2474
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "LEAVE", $2.str);
2475 2476 2477 2478 2479
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_jump *i;
2480 2481 2482
	      uint ip= sp->instructions();
	      uint n;

2483
	      n= ctx->diff_handlers(lab->ctx, TRUE);  /* Exclusive the dest. */
2484 2485
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
2486
	      n= ctx->diff_cursors(lab->ctx, TRUE);  /* Exclusive the dest. */
2487 2488
	      if (n)
	        sp->add_instr(new sp_instr_cpop(ip++, ctx, n));
2489 2490 2491 2492 2493
	      i= new sp_instr_jump(ip, ctx);
	      sp->push_backpatch(i, lab);  /* Jumping forward */
              sp->add_instr(i);
	    }
	  }
2494 2495 2496 2497
        ;

sp_proc_stmt_iterate:
	  ITERATE_SYM label_ident
2498 2499 2500 2501 2502 2503 2504 2505
	  {
	    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)
	    {
2506
	      my_error(ER_SP_LILABEL_MISMATCH, MYF(0), "ITERATE", $2.str);
2507 2508 2509 2510 2511 2512 2513 2514
	      YYABORT;
	    }
	    else
	    {
	      sp_instr_jump *i;
	      uint ip= sp->instructions();
	      uint n;

2515
	      n= ctx->diff_handlers(lab->ctx, FALSE);  /* Inclusive the dest. */
2516 2517
	      if (n)
	        sp->add_instr(new sp_instr_hpop(ip++, ctx, n));
2518
	      n= ctx->diff_cursors(lab->ctx, FALSE);  /* Inclusive the dest. */
2519 2520 2521 2522 2523 2524
	      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);
	    }
	  }
2525 2526 2527 2528
        ;

sp_proc_stmt_open:
	  OPEN_SYM ident
2529 2530 2531 2532 2533 2534 2535 2536
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_copen *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2537
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2538 2539 2540 2541 2542
	      YYABORT;
	    }
	    i= new sp_instr_copen(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
2543 2544 2545 2546
        ;

sp_proc_stmt_fetch:
	  FETCH_SYM sp_opt_fetch_noise ident INTO
2547 2548 2549 2550 2551 2552 2553 2554
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cfetch *i;

	    if (! lex->spcont->find_cursor(&$3, &offset))
	    {
2555
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $3.str);
2556 2557 2558 2559 2560 2561 2562
	      YYABORT;
	    }
	    i= new sp_instr_cfetch(sp->instructions(), lex->spcont, offset);
	    sp->add_instr(i);
	  }
	  sp_fetch_list
	  { }
2563 2564 2565 2566
        ;

sp_proc_stmt_close:
	  CLOSE_SYM ident
2567 2568 2569 2570 2571 2572 2573 2574
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    uint offset;
	    sp_instr_cclose *i;

	    if (! lex->spcont->find_cursor(&$2, &offset))
	    {
2575
	      my_error(ER_SP_CURSOR_MISMATCH, MYF(0), $2.str);
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
	      YYABORT;
	    }
	    i= new sp_instr_cclose(sp->instructions(), lex->spcont,  offset);
	    sp->add_instr(i);
	  }
	;

sp_opt_fetch_noise:
	  /* Empty */
	| NEXT_SYM FROM
	| FROM
	;

sp_fetch_list:
	  ident
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *spc= lex->spcont;
2595
	    sp_variable_t *spv;
2596

2597
	    if (!spc || !(spv = spc->find_variable(&$1)))
2598
	    {
2599
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

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

2618
	    if (!spc || !(spv = spc->find_variable(&$3)))
2619
	    {
2620
	      my_error(ER_SP_UNDECLARED_VAR, MYF(0), $3.str);
2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633
	      YYABORT;
	    }
	    else
	    {
	      /* An SP local variable */
	      sp_instr_cfetch *i= (sp_instr_cfetch *)sp->last_instruction();

	      i->add_to_varlist(spv);
	    }
	  }
	;

sp_if:
2634 2635
          { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2636 2637 2638 2639 2640
	  {
	    LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= lex->spcont;
	    uint ip= sp->instructions();
2641 2642
	    sp_instr_jump_if_not *i = new sp_instr_jump_if_not(ip, ctx,
                                                               $2, lex);
2643 2644

	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
2645
            sp->add_cont_backpatch(i);
2646
            sp->add_instr(i);
2647
            sp->restore_lex(YYTHD);
2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
	  }
	  sp_proc_stmts1
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump *i = new sp_instr_jump(ip, ctx);

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

	    lex->sphead->backpatch(lex->spcont->pop_label());
	  }
	;

sp_elseifs:
	  /* Empty */
	| ELSEIF_SYM sp_if
	| ELSE sp_proc_stmts1
	;

sp_case:
2675 2676
	  { Lex->sphead->reset_lex(YYTHD); }
          expr THEN_SYM
2677 2678 2679 2680 2681 2682 2683
	  {
            LEX *lex= Lex;
	    sp_head *sp= lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump_if_not *i;

2684
	    if (! (sp->m_flags & sp_head::IN_SIMPLE_CASE))
2685
	      i= new sp_instr_jump_if_not(ip, ctx, $2, lex);
2686 2687 2688
	    else
	    { /* Simple case: <caseval> = <whenval> */

2689 2690 2691 2692 2693
	      Item_case_expr *var;
              Item *expr;

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

2694
#ifndef DBUG_OFF
2695
              if (var)
2696
                var->m_sp= sp;
2697
#endif
2698 2699

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

2701
	      i= new sp_instr_jump_if_not(ip, ctx, expr, lex);
2702 2703
	    }
	    sp->push_backpatch(i, ctx->push_label((char *)"", 0));
2704
            sp->add_cont_backpatch(i);
2705
            sp->add_instr(i);
2706
            sp->restore_lex(YYTHD);
2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725
	  }
	  sp_proc_stmts1
	  {
	    sp_head *sp= Lex->sphead;
	    sp_pcontext *ctx= Lex->spcont;
	    uint ip= sp->instructions();
	    sp_instr_jump *i = new sp_instr_jump(ip, ctx);

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

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

2727 2728 2729 2730 2731 2732 2733
sp_whens:
	  /* Empty */
	  {
	    sp_head *sp= Lex->sphead;
	    uint ip= sp->instructions();
	    sp_instr_error *i= new sp_instr_error(ip, Lex->spcont,
						  ER_SP_CASE_NOT_FOUND);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2734

2735 2736 2737 2738 2739
	    sp->add_instr(i);
	  }
	| ELSE sp_proc_stmts1 {}
	| WHEN_SYM sp_case {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2740

2741
sp_labeled_control:
2742
	  label_ident ':'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2743
	  {
2744 2745 2746 2747 2748 2749
	    LEX *lex= Lex;
	    sp_pcontext *ctx= lex->spcont;
	    sp_label_t *lab= ctx->find_label($1.str);

	    if (lab)
	    {
2750
	      my_error(ER_SP_LABEL_REDEFINE, MYF(0), $1.str);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2751
	      YYABORT;
2752 2753 2754 2755 2756 2757 2758
	    }
	    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
2759
	  }
2760
	  sp_unlabeled_control sp_opt_label
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2761
	  {
2762
	    LEX *lex= Lex;
2763

2764 2765 2766 2767 2768 2769 2770
	    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)
	      {
2771
	        my_error(ER_SP_LABEL_MISMATCH, MYF(0), $5.str);
2772 2773 2774 2775
	        YYABORT;
	      }
	    }
	    lex->sphead->backpatch(lex->spcont->pop_label());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2776
	  }
2777 2778 2779
	;

sp_opt_label:
2780
        /* Empty  */    { $$= null_lex_str; }
2781
        | label_ident   { $$= $1; }
2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
	;

sp_unlabeled_control:
	  BEGIN_SYM
	  { /* QQ This is just a dummy for grouping declarations and statements
	       together. No [[NOT] ATOMIC] yet, and we need to figure out how
	       make it coexist with the existing BEGIN COMMIT/ROLLBACK. */
	    LEX *lex= Lex;
	    sp_label_t *lab= lex->spcont->last_label();

	    lab->type= SP_LAB_BEGIN;
	    lex->spcont= lex->spcont->push_context();
	  }
	  sp_decls
	  sp_proc_stmts
	  END
2798
	  {
2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813
	    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
2814
	  {
2815 2816 2817 2818 2819 2820
	    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
2821
	  }
2822 2823 2824
        | WHILE_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr DO_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2825
	  {
2826 2827 2828 2829
	    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,
2830
							       $3, lex);
2831 2832 2833

	    /* Jumping forward */
	    sp->push_backpatch(i, lex->spcont->last_label());
2834
            sp->new_cont_backpatch(i);
2835
            sp->add_instr(i);
2836
            sp->restore_lex(YYTHD);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2837
	  }
2838
	  sp_proc_stmts1 END WHILE_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2839
	  {
2840 2841 2842 2843 2844 2845
	    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);
2846
            lex->sphead->do_cont_backpatch();
2847
	  }
2848 2849 2850
        | REPEAT_SYM sp_proc_stmts1 UNTIL_SYM 
          { Lex->sphead->reset_lex(YYTHD); }
          expr END REPEAT_SYM
2851 2852 2853 2854 2855
	  {
	    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,
2856 2857
                                                               $5, lab->ip,
                                                               lex);
2858
            lex->sphead->add_instr(i);
2859
            lex->sphead->restore_lex(YYTHD);
2860 2861
            /* We can shortcut the cont_backpatch here */
            i->m_cont_dest= ip+1;
2862
	  }
2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878
	;

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; }
2879
          ;
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146
/*
  This part of the parser contains common code for all TABLESPACE
  commands.
  CREATE TABLESPACE name ...
  ALTER TABLESPACE name CHANGE DATAFILE ...
  ALTER TABLESPACE name ADD DATAFILE ...
  ALTER TABLESPACE name access_mode
  CREATE LOGFILE GROUP name ...
  ALTER LOGFILE GROUP name ADD UNDOFILE ..
  ALTER LOGFILE GROUP name ADD REDOFILE ..
  DROP TABLESPACE name
  DROP LOGFILE GROUP name
*/
change_tablespace_access:
          tablespace_name
          ts_access_mode
          ;

change_tablespace_info:
          tablespace_name
          CHANGE ts_datafile
          change_ts_option_list
          ;

tablespace_info:
          tablespace_name
          ADD ts_datafile
          opt_logfile_group_name
          tablespace_option_list
          ;

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

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

logfile_group_info:
          logfile_group_name
          add_log_file
          logfile_group_option_list
          ;

alter_logfile_group_info:
          logfile_group_name
          add_log_file
          alter_logfile_group_option_list
          ;

add_log_file:
          ADD lg_undofile
          | ADD lg_redofile
          ;

change_ts_option_list:
          /* empty */ {}
          change_ts_options
          ;

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

change_ts_option:
          opt_ts_initial_size
          | opt_ts_autoextend_size
          | opt_ts_max_size
          ;

tablespace_option_list:
          /* empty */ {}
          tablespace_options
          ;

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

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

alter_tablespace_option_list:
          /* empty */ {}
          alter_tablespace_options
          ;

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

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

logfile_group_option_list:
          /* empty */ {}
          logfile_group_options
          ;

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

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

alter_logfile_group_option_list:
          /* empty */ {}
          alter_logfile_group_options
          ;

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

alter_logfile_group_option:
          opt_ts_initial_size
          | opt_ts_engine
          | ts_wait
          ;


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

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

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

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

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

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

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

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

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

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

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

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

opt_ts_nodegroup:
3147
          NODEGROUP_SYM opt_equal real_ulong_num
3148 3149 3150 3151
          {
            LEX *lex= Lex;
            if (lex->alter_tablespace_info->nodegroup_id != UNDEF_NODEGROUP)
            {
jonas@perch.ndb.mysql.com's avatar
merge  
jonas@perch.ndb.mysql.com committed
3152
              my_error(ER_FILEGROUP_OPTION_ONLY_ONCE,MYF(0),"NODEGROUP");
3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
              YYABORT;
            }
            lex->alter_tablespace_info->nodegroup_id= $3;
          };

opt_ts_comment:
          COMMENT_SYM opt_equal TEXT_STRING_sys
          {
            LEX *lex= Lex;
            if (lex->alter_tablespace_info->ts_comment != NULL)
            {
jonas@perch.ndb.mysql.com's avatar
merge  
jonas@perch.ndb.mysql.com committed
3164
              my_error(ER_FILEGROUP_OPTION_ONLY_ONCE,MYF(0),"COMMENT");
3165 3166 3167 3168 3169 3170 3171 3172 3173
              YYABORT;
            }
            lex->alter_tablespace_info->ts_comment= $3.str;
          };

opt_ts_engine:
          opt_storage ENGINE_SYM opt_equal storage_engines
          {
            LEX *lex= Lex;
3174
            if (lex->alter_tablespace_info->storage_engine != NULL)
3175
            {
jonas@perch.ndb.mysql.com's avatar
merge  
jonas@perch.ndb.mysql.com committed
3176
              my_error(ER_FILEGROUP_OPTION_ONLY_ONCE,MYF(0),
3177 3178 3179
                       "STORAGE ENGINE");
              YYABORT;
            }
3180
            lex->alter_tablespace_info->storage_engine= $4;
3181 3182 3183
          };

opt_ts_wait:
3184
          /* empty */
3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198
          | ts_wait
          ;

ts_wait:
          WAIT_SYM
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->wait_until_completed= TRUE;
          }
          | NO_WAIT_SYM
          {
            LEX *lex= Lex;
            if (!(lex->alter_tablespace_info->wait_until_completed))
            {
jonas@perch.ndb.mysql.com's avatar
merge  
jonas@perch.ndb.mysql.com committed
3199
              my_error(ER_FILEGROUP_OPTION_ONLY_ONCE,MYF(0),"NO_WAIT");
3200 3201 3202 3203 3204 3205
              YYABORT;
            }
            lex->alter_tablespace_info->wait_until_completed= FALSE;
          };

size_number:
3206
          real_ulong_num { $$= $1;}
3207 3208 3209 3210 3211 3212
          | IDENT
          {
            ulonglong number, test_number;
            uint text_shift_number= 0;
            longlong prefix_number;
            char *start_ptr= $1.str;
3213
            uint str_len= $1.length;
3214
            char *end_ptr= start_ptr + str_len;
3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255
            int error;
            prefix_number= my_strtoll10(start_ptr, &end_ptr, &error);
            if ((start_ptr + str_len - 1) == end_ptr)
            {
              switch (end_ptr[0])
              {
                case 'g':
                case 'G':
                  text_shift_number+=10;
                case 'm':
                case 'M':
                  text_shift_number+=10;
                case 'k':
                case 'K':
                  text_shift_number+=10;
                  break;
                default:
                {
                  my_error(ER_WRONG_SIZE_NUMBER, MYF(0));
                  YYABORT;
                }
              }
              if (prefix_number >> 31)
              {
                my_error(ER_SIZE_OVERFLOW_ERROR, MYF(0));
                YYABORT;
              }
              number= prefix_number << text_shift_number;
            }
            else
            {
              my_error(ER_WRONG_SIZE_NUMBER, MYF(0));
              YYABORT;
            }
            $$= number;
          }
          ;

/*
  End tablespace part
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3256 3257

create2:
serg@serg.mylan's avatar
serg@serg.mylan committed
3258
        '(' create2a {}
3259 3260 3261
        | opt_create_table_options
          opt_partitioning {} 
          create3 {}
serg@serg.mylan's avatar
serg@serg.mylan committed
3262 3263 3264
        | LIKE table_ident
          {
            LEX *lex=Lex;
3265
            THD *thd= lex->thd;
mikael@zim.(none)'s avatar
mikael@zim.(none) committed
3266
            if (!(lex->like_name= $2))
venu@myvenu.com's avatar
venu@myvenu.com committed
3267
              YYABORT;
3268 3269 3270 3271 3272
            if ($2->db.str == NULL &&
                thd->copy_db_to(&($2->db.str), &($2->db.length)))
            {
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
3273 3274 3275 3276
          }
        | '(' LIKE table_ident ')'
          {
            LEX *lex=Lex;
3277
            THD *thd= lex->thd;
mikael@zim.(none)'s avatar
mikael@zim.(none) committed
3278
            if (!(lex->like_name= $3))
3279
              YYABORT;
3280 3281 3282 3283 3284
            if ($3->db.str == NULL &&
                thd->copy_db_to(&($3->db.str), &($3->db.length)))
            {
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
3285
          }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3286
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3287

serg@serg.mylan's avatar
serg@serg.mylan committed
3288
create2a:
3289 3290 3291 3292 3293 3294
        field_list ')' opt_create_table_options
          opt_partitioning {}
          create3 {}
        |  opt_partitioning {}
           create_select ')'
           { Select->set_braces(1);} union_opt {}
3295
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3296 3297

create3:
3298
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
3299
	| opt_duplicate opt_as     create_select
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3300
          { Select->set_braces(0);} union_clause {}
serg@serg.mylan's avatar
serg@serg.mylan committed
3301
	| opt_duplicate opt_as '(' create_select ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3302
          { Select->set_braces(1);} union_opt {}
3303 3304
        ;

3305 3306 3307
/*
 This part of the parser is about handling of the partition information.

3308
 It's first version was written by Mikael Ronström with lots of answers to
3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335
 questions provided by Antony Curtis.

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

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

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

partitioning:
        PARTITION_SYM
3336
        {
3337
#ifdef WITH_PARTITION_STORAGE_ENGINE
3338 3339 3340 3341
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
3342
            mem_alloc_error(sizeof(partition_info));
3343 3344
            YYABORT;
          }
3345 3346 3347 3348
          if (lex->sql_command == SQLCOM_ALTER_TABLE)
          {
            lex->alter_info.flags|= ALTER_PARTITION;
          }
3349 3350 3351 3352 3353 3354
#else
          my_error(ER_FEATURE_DISABLED, MYF(0),
       	           "partitioning", "--with-partition");
          YYABORT;
#endif

3355
        }
3356 3357 3358 3359 3360 3361 3362
        partition
        ;

partition_entry:
        PARTITION_SYM
        {
          LEX *lex= Lex;
3363
          if (!lex->part_info)
3364 3365 3366 3367
          {
            yyerror(ER(ER_PARTITION_ENTRY_ERROR));
            YYABORT;
          }
3368 3369 3370 3371
          /*
            We enter here when opening the frm file to translate
            partition info string into part_info data structure.
          */
3372
        }
3373 3374
        partition {}
        ;
3375 3376

partition:
3377 3378
        BY part_type_def opt_no_parts {} opt_sub_part {} part_defs
        ;
3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394

part_type_def:
        opt_linear KEY_SYM '(' part_field_list ')'
        {
          LEX *lex= Lex;
          lex->part_info->list_of_part_fields= TRUE;
          lex->part_info->part_type= HASH_PARTITION;
        }
        | opt_linear HASH_SYM
        { Lex->part_info->part_type= HASH_PARTITION; }
        part_func {}
        | RANGE_SYM
        { Lex->part_info->part_type= RANGE_PARTITION; }
        part_func {}
        | LIST_SYM
        { Lex->part_info->part_type= LIST_PARTITION; }
3395 3396
        part_func {}
        ;
3397 3398 3399 3400

opt_linear:
        /* empty */ {}
        | LINEAR_SYM
3401 3402
        { Lex->part_info->linear_hash_ind= TRUE;}
        ;
3403 3404

part_field_list:
3405 3406 3407 3408 3409
        /* empty */ {}
        | part_field_item_list {}
        ;

part_field_item_list:
3410
        part_field_item {}
3411
        | part_field_item_list ',' part_field_item {}
3412
        ;
3413 3414 3415 3416

part_field_item:
        ident
        {
3417 3418 3419 3420 3421
          if (Lex->part_info->part_field_list.push_back($1.str))
          {
            mem_alloc_error(1);
            YYABORT;
          }
3422 3423
        }
        ;
3424 3425 3426 3427 3428 3429 3430 3431

part_func:
        '(' remember_name part_func_expr remember_end ')'
        {
          LEX *lex= Lex;
          uint expr_len= (uint)($4 - $2) - 1;
          lex->part_info->list_of_part_fields= FALSE;
          lex->part_info->part_expr= $3;
3432
          lex->part_info->part_func_string= (char* ) sql_memdup($2+1, expr_len);
3433
          lex->part_info->part_func_len= expr_len;
3434 3435
        }
        ;
3436 3437 3438 3439 3440 3441 3442 3443

sub_part_func:
        '(' remember_name part_func_expr remember_end ')'
        {
          LEX *lex= Lex;
          uint expr_len= (uint)($4 - $2) - 1;
          lex->part_info->list_of_subpart_fields= FALSE;
          lex->part_info->subpart_expr= $3;
3444
          lex->part_info->subpart_func_string= (char* ) sql_memdup($2+1, expr_len);        
3445
          lex->part_info->subpart_func_len= expr_len;
3446 3447
        }
        ;
3448 3449 3450 3451


opt_no_parts:
        /* empty */ {}
3452
        | PARTITIONS_SYM real_ulong_num 
3453 3454
        { 
          uint no_parts= $2;
3455
          LEX *lex= Lex;
3456 3457 3458 3459 3460
          if (no_parts == 0)
          {
            my_error(ER_NO_PARTS_ERROR, MYF(0), "partitions");
            YYABORT;
          }
3461 3462 3463

          lex->part_info->no_parts= no_parts;
          lex->part_info->use_default_no_partitions= FALSE;
3464 3465
        }
        ;
3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478

opt_sub_part:
        /* empty */ {}
        | SUBPARTITION_SYM BY opt_linear HASH_SYM sub_part_func
        { Lex->part_info->subpart_type= HASH_PARTITION; }
        opt_no_subparts {}
        | SUBPARTITION_SYM BY opt_linear KEY_SYM
          '(' sub_part_field_list ')'
        {
          LEX *lex= Lex;
          lex->part_info->subpart_type= HASH_PARTITION;
          lex->part_info->list_of_subpart_fields= TRUE;
        }
3479 3480
        opt_no_subparts {}
        ;
3481 3482 3483

sub_part_field_list:
        sub_part_field_item {}
3484 3485
        | sub_part_field_list ',' sub_part_field_item {}
        ;
3486 3487 3488

sub_part_field_item:
        ident
3489 3490 3491 3492 3493 3494 3495
        {
          if (Lex->part_info->subpart_field_list.push_back($1.str))
          {
            mem_alloc_error(1);
            YYABORT;
          }
        }
3496
        ;
3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511

part_func_expr:
        bit_expr
        {
          LEX *lex= Lex;
          bool not_corr_func;
          not_corr_func= !lex->safe_to_cache_query;
          lex->safe_to_cache_query= 1;
          if (not_corr_func)
          {
            yyerror(ER(ER_CONST_EXPR_IN_PARTITION_FUNC_ERROR));
            YYABORT;
          }
          $$=$1;
        }
3512
        ;
3513 3514 3515

opt_no_subparts:
        /* empty */ {}
3516
        | SUBPARTITIONS_SYM real_ulong_num
3517 3518
        {
          uint no_parts= $2;
3519
          LEX *lex= Lex;
3520 3521 3522 3523 3524
          if (no_parts == 0)
          {
            my_error(ER_NO_PARTS_ERROR, MYF(0), "subpartitions");
            YYABORT;
          }
3525 3526
          lex->part_info->no_subparts= no_parts;
          lex->part_info->use_default_no_subpartitions= FALSE;
3527 3528
        }
        ;
3529 3530 3531 3532 3533 3534 3535 3536

part_defs:
        /* empty */
        {}
        | '(' part_def_list ')'
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
3537
          uint count_curr_parts= part_info->partitions.elements;
3538 3539 3540
          if (part_info->no_parts != 0)
          {
            if (part_info->no_parts !=
3541
                count_curr_parts)
3542 3543 3544 3545 3546
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_PART_ERROR));
              YYABORT;
            }
          }
3547
          else if (count_curr_parts > 0)
3548
          {
3549
            part_info->no_parts= count_curr_parts;
3550 3551
          }
          part_info->count_curr_subparts= 0;
3552 3553
        }
        ;
3554 3555 3556

part_def_list:
        part_definition {}
3557 3558
        | part_def_list ',' part_definition {}
        ;
3559 3560 3561 3562 3563 3564 3565

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

          if (!p_elem || part_info->partitions.push_back(p_elem))
3569
          {
3570 3571
            mem_alloc_error(sizeof(partition_element));
            YYABORT;
3572
          }
3573
          p_elem->part_state= PART_NORMAL;
3574 3575 3576
          part_info->curr_part_elem= p_elem;
          part_info->current_partition= p_elem;
          part_info->use_default_partitions= FALSE;
3577
          part_info->use_default_no_partitions= FALSE;
3578 3579 3580 3581
        }
        part_name {}
        opt_part_values {}
        opt_part_options {}
3582 3583
        opt_sub_partition {}
        ;
3584 3585

part_name:
3586
        ident
3587 3588 3589 3590 3591 3592
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          partition_element *p_elem= part_info->curr_part_elem;
          p_elem->partition_name= $1.str;
        }
3593
        ;
3594 3595 3596 3597 3598

opt_part_values:
        /* empty */
        {
          LEX *lex= Lex;
3599
          if (!is_partition_management(lex))
3600
          {
3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612
            if (lex->part_info->part_type == RANGE_PARTITION)
            {
              my_error(ER_PARTITION_REQUIRES_VALUES_ERROR, MYF(0),
              "RANGE", "LESS THAN");
              YYABORT;
            }
            if (lex->part_info->part_type == LIST_PARTITION)
            {
              my_error(ER_PARTITION_REQUIRES_VALUES_ERROR, MYF(0),
              "LIST", "IN");
              YYABORT;
            }
3613
          }
3614 3615
          else
            lex->part_info->part_type= HASH_PARTITION;
3616 3617 3618
        }
        | VALUES LESS_SYM THAN_SYM part_func_max
        {
3619 3620
          LEX *lex= Lex;
          if (!is_partition_management(lex))
3621
          {
3622 3623 3624 3625 3626 3627
            if (Lex->part_info->part_type != RANGE_PARTITION)
            {
              my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
              "RANGE", "LESS THAN");
              YYABORT;
            }
3628
          }
3629 3630
          else
            lex->part_info->part_type= RANGE_PARTITION;
3631 3632 3633
        }
        | VALUES IN_SYM '(' part_list_func ')'
        {
3634 3635
          LEX *lex= Lex;
          if (!is_partition_management(lex))
3636
          {
3637 3638 3639 3640 3641 3642
            if (Lex->part_info->part_type != LIST_PARTITION)
            {
              my_error(ER_PARTITION_WRONG_VALUES_ERROR, MYF(0),
              "LIST", "IN");
              YYABORT;
            }
3643
          }
3644 3645
          else
            lex->part_info->part_type= LIST_PARTITION;
3646 3647
        }
        ;
3648 3649

part_func_max:
3650
        max_value_sym
3651 3652 3653 3654 3655 3656 3657 3658
        {
          LEX *lex= Lex;
          if (lex->part_info->defined_max_value)
          {
            yyerror(ER(ER_PARTITION_MAXVALUE_ERROR));
            YYABORT;
          }
          lex->part_info->defined_max_value= TRUE;
3659
          lex->part_info->curr_part_elem->max_value= TRUE;
3660
          lex->part_info->curr_part_elem->range_value= LONGLONG_MAX;
3661 3662 3663 3664 3665 3666 3667 3668
        }
        | part_range_func
        {
          if (Lex->part_info->defined_max_value)
          {
            yyerror(ER(ER_PARTITION_MAXVALUE_ERROR));
            YYABORT;
          }
3669 3670 3671 3672 3673
          if (Lex->part_info->curr_part_elem->has_null_value)
          {
            yyerror(ER(ER_NULL_IN_VALUES_LESS_THAN));
            YYABORT;
          }
3674 3675
        }
        ;
3676

3677 3678 3679 3680 3681
max_value_sym:
        MAX_VALUE_SYM
        | '(' MAX_VALUE_SYM ')'
        ;
        
3682 3683 3684
part_range_func:
        '(' part_bit_expr ')' 
        {
3685 3686 3687 3688
          partition_info *part_info= Lex->part_info;
          if (!($2->unsigned_flag))
            part_info->curr_part_elem->signed_flag= TRUE;
          part_info->curr_part_elem->range_value= $2->value;
3689 3690
        }
        ;
3691 3692 3693

part_list_func:
        part_list_item {}
3694 3695
        | part_list_func ',' part_list_item {}
        ;
3696 3697 3698 3699

part_list_item:
        part_bit_expr
        {
3700
          part_elem_value *value_ptr= $1;
3701 3702 3703
          partition_info *part_info= Lex->part_info;
          if (!value_ptr->unsigned_flag)
            part_info->curr_part_elem->signed_flag= TRUE;
3704
          if (!value_ptr->null_value &&
3705 3706
             part_info->curr_part_elem->
              list_val_list.push_back(value_ptr))
3707
          {
3708
            mem_alloc_error(sizeof(part_elem_value));
3709 3710
            YYABORT;
          }
3711 3712
        }
        ;
3713 3714 3715 3716 3717 3718

part_bit_expr:
        bit_expr
        {
          Item *part_expr= $1;
          bool not_corr_func;
3719
          int part_expression_ok= 1;
3720
          LEX *lex= Lex;
3721
          THD *thd= YYTHD;
3722
          longlong item_value;
3723 3724
          Name_resolution_context *context= &lex->current_select->context;
          TABLE_LIST *save_list= context->table_list;
3725
          const char *save_where= thd->where;
3726 3727

          context->table_list= 0;
3728
          thd->where= "partition function";
3729 3730 3731 3732 3733 3734 3735 3736

          part_elem_value *value_ptr= 
            (part_elem_value*)sql_alloc(sizeof(part_elem_value));
          if (!value_ptr)
          {
            mem_alloc_error(sizeof(part_elem_value));
            YYABORT;
          }
3737 3738
          if (part_expr->walk(&Item::check_partition_func_processor, 0,
                              NULL))
3739 3740 3741 3742
          {
            my_error(ER_PARTITION_FUNCTION_IS_NOT_ALLOWED, MYF(0));
            YYABORT;
          }
3743 3744 3745 3746
          if (part_expr->fix_fields(YYTHD, (Item**)0) ||
              ((context->table_list= save_list), FALSE) ||
              (!part_expr->const_item()) ||
              (!lex->safe_to_cache_query))
3747
          {
3748
            my_error(ER_NO_CONST_EXPR_IN_RANGE_OR_LIST_ERROR, MYF(0));
3749 3750
            YYABORT;
          }
3751
          thd->where= save_where;
3752
          value_ptr->value= part_expr->val_int();
3753 3754 3755 3756
          value_ptr->unsigned_flag= TRUE;
          if (!part_expr->unsigned_flag &&
              value_ptr->value < 0)
            value_ptr->unsigned_flag= FALSE;
3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767
          if ((value_ptr->null_value= part_expr->null_value))
          {
            if (Lex->part_info->curr_part_elem->has_null_value)
            {
              my_error(ER_MULTIPLE_DEF_CONST_IN_LIST_PART_ERROR, MYF(0));
              YYABORT;
            }
            Lex->part_info->curr_part_elem->has_null_value= TRUE;
          }
          else if (part_expr->result_type() != INT_RESULT &&
                   !part_expr->null_value)
3768 3769 3770 3771
          {
            yyerror(ER(ER_INCONSISTENT_TYPE_OF_FUNCTIONS_ERROR));
            YYABORT;
          }
3772
          $$= value_ptr; 
3773
        }
3774
        ;
3775 3776

opt_sub_partition:
3777 3778 3779 3780 3781 3782 3783 3784 3785
        /* empty */
        {
          if (Lex->part_info->no_subparts != 0 &&
              !Lex->part_info->use_default_subpartitions)
          {
            yyerror(ER(ER_PARTITION_WRONG_NO_SUBPART_ERROR));
            YYABORT;
          }
        }
3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800
        | '(' sub_part_list ')'
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
          if (part_info->no_subparts != 0)
          {
            if (part_info->no_subparts !=
                part_info->count_curr_subparts)
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_SUBPART_ERROR));
              YYABORT;
            }
          }
          else if (part_info->count_curr_subparts > 0)
          {
3801 3802 3803 3804 3805
            if (part_info->partitions.elements > 1)
            {
              yyerror(ER(ER_PARTITION_WRONG_NO_SUBPART_ERROR));
              YYABORT;
            }
3806 3807 3808
            part_info->no_subparts= part_info->count_curr_subparts;
          }
          part_info->count_curr_subparts= 0;
3809 3810
        }
        ;
3811 3812 3813

sub_part_list:
        sub_part_definition {}
3814 3815
        | sub_part_list ',' sub_part_definition {}
        ;
3816 3817 3818 3819 3820 3821

sub_part_definition:
        SUBPARTITION_SYM
        {
          LEX *lex= Lex;
          partition_info *part_info= lex->part_info;
3822 3823 3824 3825
          partition_element *curr_part= part_info->current_partition;
          partition_element *sub_p_elem= new partition_element(curr_part);
          if (!sub_p_elem ||
           curr_part->subpartitions.push_back(sub_p_elem))
3826
          {
3827
            mem_alloc_error(sizeof(partition_element));
3828 3829
            YYABORT;
          }
3830
          part_info->curr_part_elem= sub_p_elem;
3831
          part_info->use_default_subpartitions= FALSE;
3832
          part_info->use_default_no_subpartitions= FALSE;
3833 3834
          part_info->count_curr_subparts++;
        }
3835 3836
        sub_name opt_part_options {}
        ;
3837 3838 3839

sub_name:
        ident_or_text
3840 3841
        { Lex->part_info->curr_part_elem->partition_name= $1.str; }
        ;
3842 3843 3844

opt_part_options:
       /* empty */ {}
3845 3846
       | opt_part_option_list {}
       ;
3847 3848 3849

opt_part_option_list:
       opt_part_option_list opt_part_option {}
3850 3851
       | opt_part_option {}
       ;
3852 3853 3854 3855 3856

opt_part_option:
        TABLESPACE opt_equal ident_or_text
        { Lex->part_info->curr_part_elem->tablespace_name= $3.str; }
        | opt_storage ENGINE_SYM opt_equal storage_engines
3857 3858 3859 3860 3861
        {
          LEX *lex= Lex;
          lex->part_info->curr_part_elem->engine_type= $4;
          lex->part_info->default_engine_type= $4;
        }
3862
        | NODEGROUP_SYM opt_equal real_ulong_num
3863
        { Lex->part_info->curr_part_elem->nodegroup_id= $3; }
3864
        | MAX_ROWS opt_equal real_ulonglong_num
3865
        { Lex->part_info->curr_part_elem->part_max_rows= $3; }
3866
        | MIN_ROWS opt_equal real_ulonglong_num
3867 3868 3869 3870 3871 3872
        { Lex->part_info->curr_part_elem->part_min_rows= $3; }
        | DATA_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys
        { Lex->part_info->curr_part_elem->data_file_name= $4.str; }
        | INDEX_SYM DIRECTORY_SYM opt_equal TEXT_STRING_sys
        { Lex->part_info->curr_part_elem->index_file_name= $4.str; }
        | COMMENT_SYM opt_equal TEXT_STRING_sys
3873 3874
        { Lex->part_info->curr_part_elem->part_comment= $3.str; }
        ;
3875 3876 3877 3878 3879

/*
 End of partition parser part
*/

serg@serg.mylan's avatar
serg@serg.mylan committed
3880
create_select:
3881
          SELECT_SYM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3882
          {
3883
	    LEX *lex=Lex;
3884
	    lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
3885 3886 3887 3888
	    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;
3889 3890 3891 3892
	    /*
              The following work only with the local list, the global list
              is created correctly in this case
	    */
3893
	    lex->current_select->table_list.save_and_clear(&lex->save_list);
3894
	    mysql_init_select(lex);
3895
	    lex->current_select->parsing_place= SELECT_LIST;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3896
          }
3897 3898
          select_options select_item_list
	  {
3899
	    Select->parsing_place= NO_MATTER;
3900
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3901
	  opt_select_from
3902 3903 3904 3905 3906 3907 3908
	  {
	    /*
              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
3909
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3910

3911 3912
opt_as:
	/* empty */ {}
3913
	| AS	    {};
3914

3915 3916 3917 3918 3919 3920 3921 3922 3923
opt_create_database_options:
	/* empty */			{}
	| create_database_options	{};

create_database_options:
	create_database_option					{}
	| create_database_options create_database_option	{};

create_database_option:
3924 3925
	default_collation   {}
	| default_charset   {};
3926

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3927
opt_table_options:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3928
	/* empty */	 { $$= 0; }
3929
	| table_options  { $$= $1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3930 3931 3932

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3935
table_option:
3936
	TEMPORARY	{ $$=HA_LEX_CREATE_TMP_TABLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3937 3938

opt_if_not_exists:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
3939
	/* empty */	 { $$= 0; }
3940
	| IF not EXISTS	 { $$=HA_LEX_CREATE_IF_NOT_EXISTS; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3941 3942 3943

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

3946 3947 3948 3949
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
3950 3951
create_table_options:
	create_table_option
3952
	| create_table_option     create_table_options
3953
	| create_table_option ',' create_table_options;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3954 3955

create_table_option:
3956
	ENGINE_SYM opt_equal storage_engines    { Lex->create_info.db_type= $3; Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE; }
3957 3958 3959 3960 3961 3962 3963
	| TYPE_SYM opt_equal storage_engines
          {
            Lex->create_info.db_type= $3;
            WARN_DEPRECATED(yythd, "5.2", "TYPE=storage_engine",
                            "'ENGINE=storage_engine'");
            Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE;
          }
3964 3965
	| 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
3966
	| 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;}
3967
	| PASSWORD opt_equal TEXT_STRING_sys	{ Lex->create_info.password=$3.str; Lex->create_info.used_fields|= HA_CREATE_USED_PASSWORD; }
3968
	| COMMENT_SYM opt_equal TEXT_STRING_sys	{ Lex->create_info.comment=$3; Lex->create_info.used_fields|= HA_CREATE_USED_COMMENT; }
3969
	| AUTO_INC opt_equal ulonglong_num	{ Lex->create_info.auto_increment_value=$3; Lex->create_info.used_fields|= HA_CREATE_USED_AUTO;}
3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990
        | PACK_KEYS_SYM opt_equal ulong_num
          {
            switch($3) {
            case 0:
                Lex->create_info.table_options|= HA_OPTION_NO_PACK_KEYS;
                break;
            case 1:
                Lex->create_info.table_options|= HA_OPTION_PACK_KEYS;
                break;
            default:
                yyerror(ER(ER_SYNTAX_ERROR));
                YYABORT;
            }
            Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;
          }
        | PACK_KEYS_SYM opt_equal DEFAULT
          {
            Lex->create_info.table_options&=
              ~(HA_OPTION_PACK_KEYS | HA_OPTION_NO_PACK_KEYS);
            Lex->create_info.used_fields|= HA_CREATE_USED_PACK_KEYS;
          }
serg@serg.mylan's avatar
serg@serg.mylan committed
3991 3992
	| 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; }
3993
	| ROW_FORMAT_SYM opt_equal row_types	{ Lex->create_info.row_type= $3;  Lex->create_info.used_fields|= HA_CREATE_USED_ROW_FORMAT; }
3994
	| UNION_SYM opt_equal '(' table_list ')'
3995 3996 3997
	  {
	    /* Move the union list to the merge_list */
	    LEX *lex=Lex;
3998 3999
	    TABLE_LIST *table_list= lex->select_lex.get_table_list();
	    lex->create_info.merge_list= lex->select_lex.table_list;
4000
	    lex->create_info.merge_list.elements--;
4001 4002
	    lex->create_info.merge_list.first=
	      (byte*) (table_list->next_local);
4003
	    lex->select_lex.table_list.elements=1;
4004 4005 4006
	    lex->select_lex.table_list.next=
	      (byte**) &(table_list->next_local);
	    table_list->next_local= 0;
4007
	    lex->create_info.used_fields|= HA_CREATE_USED_UNION;
4008
	  }
4009 4010
	| default_charset
	| default_collation
4011
	| INSERT_METHOD opt_equal merge_insert_types   { Lex->create_info.merge_insert_method= $3; Lex->create_info.used_fields|= HA_CREATE_USED_INSERT_METHOD;}
4012 4013
	| 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; }
4014 4015 4016
        | TABLESPACE ident {Lex->create_info.tablespace= $2.str;}
        | STORAGE_SYM DISK_SYM {Lex->create_info.store_on_disk= TRUE;}
        | STORAGE_SYM MEMORY_SYM {Lex->create_info.store_on_disk= FALSE;}
4017
	| 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; }
4018 4019 4020 4021 4022
	| KEY_BLOCK_SIZE opt_equal ulong_num
	  {
	    Lex->create_info.used_fields|= HA_CREATE_USED_KEY_BLOCK_SIZE;
	    Lex->create_info.key_block_size= $3;
	  }
4023
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4024

4025 4026 4027 4028 4029 4030 4031 4032
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))
          {
4033 4034 4035
            my_error(ER_CONFLICTING_DECLARATIONS, MYF(0),
                     "CHARACTER SET ", cinfo->default_table_charset->csname,
                     "CHARACTER SET ", $4->csname);
4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049
            YYABORT;
          }
	  Lex->create_info.default_table_charset= $4;
          Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

default_collation:
        opt_default COLLATE_SYM opt_equal collation_name_or_default
        {
          HA_CREATE_INFO *cinfo= &Lex->create_info;
          if ((cinfo->used_fields & HA_CREATE_USED_DEFAULT_CHARSET) &&
               cinfo->default_table_charset && $4 &&
               !my_charset_same(cinfo->default_table_charset,$4))
            {
4050 4051
              my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $4->name, cinfo->default_table_charset->csname);
4052 4053 4054 4055 4056 4057
              YYABORT;
            }
            Lex->create_info.default_table_charset= $4;
            Lex->create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
        };

4058
storage_engines:
4059 4060
	ident_or_text
	{
4061
	  $$ = ha_resolve_by_name(YYTHD, &$1);
4062 4063
	  if ($$ == NULL)
          if (YYTHD->variables.sql_mode & MODE_NO_ENGINE_SUBSTITUTION)
4064
	  {
4065
	    my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
4066 4067
	    YYABORT;
	  }
4068 4069 4070 4071 4072 4073
          else
          {
            push_warning_printf(YYTHD, MYSQL_ERROR::WARN_LEVEL_ERROR,
                                ER_UNKNOWN_STORAGE_ENGINE,
                                ER(ER_UNKNOWN_STORAGE_ENGINE), $1.str);
          }
4074
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4075 4076 4077 4078 4079

row_types:
	DEFAULT		{ $$= ROW_TYPE_DEFAULT; }
	| FIXED_SYM	{ $$= ROW_TYPE_FIXED; }
	| DYNAMIC_SYM	{ $$= ROW_TYPE_DYNAMIC; }
4080 4081 4082
	| 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
4083

4084 4085 4086
merge_insert_types:
       NO_SYM            { $$= MERGE_INSERT_DISABLED; }
       | FIRST_SYM       { $$= MERGE_INSERT_TO_FIRST; }
4087
       | LAST_SYM        { $$= MERGE_INSERT_TO_LAST; };
4088

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4089
opt_select_from:
4090
	opt_limit_clause {}
4091
	| select_from select_lock_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4092 4093

udf_func_type:
4094
	/* empty */	{ $$ = UDFTYPE_FUNCTION; }
4095
	| AGGREGATE_SYM { $$ = UDFTYPE_AGGREGATE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4096 4097 4098 4099

udf_type:
	STRING_SYM {$$ = (int) STRING_RESULT; }
	| REAL {$$ = (int) REAL_RESULT; }
4100
        | DECIMAL_SYM {$$ = (int) DECIMAL_RESULT; }
4101
	| INT_SYM {$$ = (int) INT_RESULT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4102 4103 4104

field_list:
	  field_list_item
4105
	| field_list ',' field_list_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4106 4107 4108


field_list_item:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4109
	   column_def
4110 4111 4112 4113
         | key_def
         ;

column_def:
4114
	  field_spec opt_check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4115 4116 4117 4118
	| field_spec references
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4119
	;
4120 4121

key_def:
4122
	key_type opt_ident key_alg '(' key_list ')' key_options
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4123
	  {
4124
	    LEX *lex=Lex;
4125
	    if ($1 != Key::FULLTEXT && lex->key_create_info.parser_name.str)
4126 4127 4128 4129
	    {
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
	    }
4130
	    lex->key_list.push_back(new Key($1,$2, &lex->key_create_info, 0,
4131
					   lex->col_list));
4132
	    lex->col_list.empty();		/* Alloced by sql_alloc */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4133
	  }
4134 4135
	| opt_constraint constraint_key_type opt_ident key_alg
	  '(' key_list ')' key_options
4136 4137
	  {
	    LEX *lex=Lex;
4138
	    const char *key_name= $3 ? $3 : $1;
4139
	    lex->key_list.push_back(new Key($2, key_name, &lex->key_create_info, 0,
4140
					    lex->col_list));
4141 4142
	    lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
4143
	| opt_constraint FOREIGN KEY_SYM opt_ident '(' key_list ')' references
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4144
	  {
4145
	    LEX *lex=Lex;
4146
	    lex->key_list.push_back(new foreign_key($4 ? $4:$1, lex->col_list,
4147 4148 4149 4150 4151
				    $8,
				    lex->ref_list,
				    lex->fk_delete_opt,
				    lex->fk_update_opt,
				    lex->fk_match_option));
4152
	    lex->key_list.push_back(new Key(Key::MULTIPLE, $4 ? $4 : $1,
4153
					    &default_key_create_info, 1,
4154
					    lex->col_list));
4155
	    lex->col_list.empty();		/* Alloced by sql_alloc */
4156 4157 4158

            /* Only used for ALTER TABLE. Ignored otherwise. */
            lex->alter_info.flags|= ALTER_FOREIGN_KEY;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4159
	  }
4160 4161 4162 4163
	| constraint opt_check_constraint
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
	  }
4164
	| opt_constraint check_constraint
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4165 4166
	  {
	    Lex->col_list.empty();		/* Alloced by sql_alloc */
4167 4168 4169
	  }
	;

4170
opt_check_constraint:
4171
	/* empty */
4172 4173 4174 4175 4176
	| check_constraint
	;

check_constraint:
	CHECK_SYM expr
4177
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4178 4179

opt_constraint:
4180
	/* empty */		{ $$=(char*) 0; }
monty@mysql.com's avatar
monty@mysql.com committed
4181 4182 4183 4184 4185 4186
	| constraint		{ $$= $1; }
	;

constraint:
	CONSTRAINT opt_ident	{ $$=$2; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4187 4188 4189 4190

field_spec:
	field_ident
	 {
4191
	   LEX *lex=Lex;
4192
	   lex->length=lex->dec=0; lex->type=0;
4193
	   lex->default_value= lex->on_update_value= 0;
4194
           lex->comment=null_lex_str;
4195
	   lex->charset=NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4196 4197 4198
	 }
	type opt_attribute
	{
4199
	  LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4200
	  if (add_field_to_list(lex->thd, $1.str,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4201
				(enum enum_field_types) $3,
4202
				lex->length,lex->dec,lex->type,
4203
				lex->default_value, lex->on_update_value, 
4204
                                &lex->comment,
4205
				lex->change,&lex->interval_list,lex->charset,
4206
				lex->uint_geom_type))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4207
	    YYABORT;
4208
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4209 4210

type:
4211
	int_type opt_len field_options	{ $$=$1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4212 4213
	| 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
4214 4215 4216 4217
	| 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
4218 4219
	| BOOL_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
4220 4221
	| BOOLEAN_SYM			{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_TINY; }
4222
	| char '(' NUM ')' opt_binary	{ Lex->length=$3.str;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4223 4224 4225
					  $$=FIELD_TYPE_STRING; }
	| char opt_binary		{ Lex->length=(char*) "1";
					  $$=FIELD_TYPE_STRING; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4226
	| nchar '(' NUM ')' opt_bin_mod	{ Lex->length=$3.str;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4227
					  $$=FIELD_TYPE_STRING;
4228
					  Lex->charset=national_charset_info; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4229
	| nchar opt_bin_mod		{ Lex->length=(char*) "1";
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4230
					  $$=FIELD_TYPE_STRING;
4231
					  Lex->charset=national_charset_info; }
4232
	| BINARY '(' NUM ')'		{ Lex->length=$3.str;
4233
					  Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4234
					  $$=FIELD_TYPE_STRING; }
4235 4236 4237
	| BINARY			{ Lex->length= (char*) "1";
					  Lex->charset=&my_charset_bin;
					  $$=FIELD_TYPE_STRING; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4238
	| varchar '(' NUM ')' opt_binary { Lex->length=$3.str;
4239
					  $$= MYSQL_TYPE_VARCHAR; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4240
	| nvarchar '(' NUM ')' opt_bin_mod { Lex->length=$3.str;
4241
					  $$= MYSQL_TYPE_VARCHAR;
4242
					  Lex->charset=national_charset_info; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4243
	| VARBINARY '(' NUM ')' 	{ Lex->length=$3.str;
4244
					  Lex->charset=&my_charset_bin;
4245
					  $$= MYSQL_TYPE_VARCHAR; }
4246
	| YEAR_SYM opt_len field_options { $$=FIELD_TYPE_YEAR; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4247 4248
	| DATE_SYM			{ $$=FIELD_TYPE_DATE; }
	| TIME_SYM			{ $$=FIELD_TYPE_TIME; }
4249
	| TIMESTAMP opt_len
4250
	  {
4251
	    if (YYTHD->variables.sql_mode & MODE_MAXDB)
4252 4253
	      $$=FIELD_TYPE_DATETIME;
	    else
4254 4255 4256 4257 4258
            {
              /* 
                Unlike other types TIMESTAMP fields are NOT NULL by default.
              */
              Lex->type|= NOT_NULL_FLAG;
4259
	      $$=FIELD_TYPE_TIMESTAMP;
4260
            }
4261
	   }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4262
	| DATETIME			{ $$=FIELD_TYPE_DATETIME; }
4263
	| TINYBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4264
					  $$=FIELD_TYPE_TINY_BLOB; }
4265
	| BLOB_SYM opt_len		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4266
					  $$=FIELD_TYPE_BLOB; }
4267 4268
	| spatial_type
          {
hf@deer.(none)'s avatar
hf@deer.(none) committed
4269
#ifdef HAVE_SPATIAL
4270 4271 4272
            Lex->charset=&my_charset_bin;
            Lex->uint_geom_type= (uint)$1;
            $$=FIELD_TYPE_GEOMETRY;
hf@deer.(none)'s avatar
hf@deer.(none) committed
4273
#else
serg@serg.mylan's avatar
serg@serg.mylan committed
4274
            my_error(ER_FEATURE_DISABLED, MYF(0),
4275
                     sym_group_geom.name, sym_group_geom.needed_define);
4276
            YYABORT;
hf@deer.(none)'s avatar
hf@deer.(none) committed
4277
#endif
4278
          }
4279
	| MEDIUMBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4280
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
4281
	| LONGBLOB			{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4282
					  $$=FIELD_TYPE_LONG_BLOB; }
4283
	| LONG_SYM VARBINARY		{ Lex->charset=&my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4284
					  $$=FIELD_TYPE_MEDIUM_BLOB; }
4285 4286
	| LONG_SYM varchar opt_binary	{ $$=FIELD_TYPE_MEDIUM_BLOB; }
	| TINYTEXT opt_binary		{ $$=FIELD_TYPE_TINY_BLOB; }
4287
	| TEXT_SYM opt_len opt_binary	{ $$=FIELD_TYPE_BLOB; }
4288 4289
	| 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
4290
	| DECIMAL_SYM float_options field_options
4291
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4292
	| NUMERIC_SYM float_options field_options
4293
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
4294
	| FIXED_SYM float_options field_options
4295
                                        { $$=FIELD_TYPE_NEWDECIMAL;}
4296
	| ENUM {Lex->interval_list.empty();} '(' string_list ')' opt_binary
4297
	  { $$=FIELD_TYPE_ENUM; }
4298
	| SET { Lex->interval_list.empty();} '(' string_list ')' opt_binary
4299
	  { $$=FIELD_TYPE_SET; }
4300
	| LONG_SYM opt_binary		{ $$=FIELD_TYPE_MEDIUM_BLOB; }
4301 4302 4303 4304 4305 4306
	| SERIAL_SYM
	  {
	    $$=FIELD_TYPE_LONGLONG;
	    Lex->type|= (AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG |
		         UNIQUE_FLAG);
	  }
4307
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4308

hf@deer.(none)'s avatar
hf@deer.(none) committed
4309 4310 4311
spatial_type:
	GEOMETRY_SYM	      { $$= Field::GEOM_GEOMETRY; }
	| GEOMETRYCOLLECTION  { $$= Field::GEOM_GEOMETRYCOLLECTION; }
4312 4313 4314
	| POINT_SYM           { Lex->length= (char*)"21";
                                $$= Field::GEOM_POINT;
                              }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4315 4316 4317 4318 4319 4320 4321
	| 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
4322 4323
char:
	CHAR_SYM {}
4324 4325 4326 4327 4328 4329
	;

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4331 4332 4333
varchar:
	char VARYING {}
	| VARCHAR {}
4334 4335 4336 4337
	;

nvarchar:
	NATIONAL_SYM VARCHAR {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4338
	| NVARCHAR_SYM {}
4339 4340 4341 4342
	| NCHAR_SYM VARCHAR {}
	| NATIONAL_SYM CHAR_SYM VARYING {}
	| NCHAR_SYM VARYING {}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4343 4344 4345 4346 4347 4348

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

real_type:
4352
	REAL		{ $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4353 4354
			      FIELD_TYPE_FLOAT : FIELD_TYPE_DOUBLE; }
	| DOUBLE_SYM	{ $$=FIELD_TYPE_DOUBLE; }
4355
	| DOUBLE_SYM PRECISION { $$=FIELD_TYPE_DOUBLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4356 4357 4358


float_options:
4359 4360
        /* empty */		{ Lex->dec=Lex->length= (char*)0; }
        | '(' NUM ')'		{ Lex->length=$2.str; Lex->dec= (char*)0; }
4361
	| precision		{};
4362 4363 4364 4365 4366 4367

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

field_options:
	/* empty */		{}
4372
	| field_opt_list	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4373 4374 4375

field_opt_list:
	field_opt_list field_option {}
4376
	| field_option {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4377 4378

field_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4379
	SIGNED_SYM	{}
4380
	| UNSIGNED	{ Lex->type|= UNSIGNED_FLAG;}
4381
	| ZEROFILL	{ Lex->type|= UNSIGNED_FLAG | ZEROFILL_FLAG; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4382 4383

opt_len:
4384 4385
	/* empty */	{ Lex->length=(char*) 0; } /* use default length */
	| '(' NUM ')'	{ Lex->length= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4386 4387 4388

opt_precision:
	/* empty */	{}
4389
	| precision	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4390 4391 4392

opt_attribute:
	/* empty */ {}
4393
	| opt_attribute_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4394 4395 4396

opt_attribute_list:
	opt_attribute_list attribute {}
4397
	| attribute;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4398 4399 4400

attribute:
	NULL_SYM	  { Lex->type&= ~ NOT_NULL_FLAG; }
4401
	| not NULL_SYM	  { Lex->type|= NOT_NULL_FLAG; }
4402 4403 4404
	| 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
4405
	| AUTO_INC	  { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; }
4406
	| SERIAL_SYM DEFAULT VALUE_SYM
4407 4408 4409
	  { 
	    LEX *lex=Lex;
	    lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; 
4410
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4411 4412 4413 4414 4415
	  }
	| opt_primary KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; 
4416
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4417 4418 4419 4420 4421
	  }
	| UNIQUE_SYM	  
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_FLAG; 
4422
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4423 4424 4425 4426 4427
	  }
	| UNIQUE_SYM KEY_SYM 
	  {
	    LEX *lex=Lex;
	    lex->type|= UNIQUE_KEY_FLAG; 
4428
	    lex->alter_info.flags|= ALTER_ADD_INDEX; 
4429
	  }
4430
	| COMMENT_SYM TEXT_STRING_sys { Lex->comment= $2; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4431 4432
	| COLLATE_SYM collation_name
	  {
4433
	    if (Lex->charset && !my_charset_same(Lex->charset,$2))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4434
	    {
4435 4436
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $2->name,Lex->charset->csname);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4437 4438 4439 4440 4441 4442 4443 4444
	      YYABORT;
	    }
	    else
	    {
	      Lex->charset=$2;
	    }
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4445

4446 4447 4448 4449 4450
now_or_signed_literal:
        NOW_SYM optional_braces { $$= new Item_func_now_local(); }
        | signed_literal { $$=$1; }
        ;

4451 4452 4453 4454
charset:
	CHAR_SYM SET	{}
	| CHARSET	{}
	;
4455

4456
charset_name:
4457
	ident_or_text
4458
	{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4459
	  if (!($$=get_charset_by_csname($1.str,MY_CS_PRIMARY,MYF(0))))
4460
	  {
4461
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
4462 4463
	    YYABORT;
	  }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4464 4465 4466
	}
	| BINARY { $$= &my_charset_bin; }
	;
4467

4468 4469 4470
charset_name_or_default:
	charset_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4471

4472 4473 4474 4475 4476 4477 4478

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)))
	  {
4479
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), $1.str);
4480 4481 4482 4483 4484 4485 4486 4487 4488 4489
	    YYABORT;
	  }
	}
	| BINARY { $$= &my_charset_bin; }
	;

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

4490
collation_name:
4491
	ident_or_text
4492 4493 4494
	{
	  if (!($$=get_charset_by_name($1.str,MYF(0))))
	  {
4495
	    my_error(ER_UNKNOWN_COLLATION, MYF(0), $1.str);
4496 4497 4498 4499
	    YYABORT;
	  }
	};

4500 4501
opt_collate:
	/* empty */	{ $$=NULL; }
4502
	| COLLATE_SYM collation_name_or_default { $$=$2; }
4503 4504
	;

4505 4506 4507 4508
collation_name_or_default:
	collation_name { $$=$1;   }
	| DEFAULT    { $$=NULL; } ;

4509 4510 4511 4512
opt_default:
	/* empty */	{}
	| DEFAULT	{};

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4513
opt_binary:
4514
	/* empty */			{ Lex->charset=NULL; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4515
	| ASCII_SYM opt_bin_mod		{ Lex->charset=&my_charset_latin1; }
4516
	| BYTE_SYM			{ Lex->charset=&my_charset_bin; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533
	| UNICODE_SYM opt_bin_mod
	{
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
	  {
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
	    YYABORT;
	  }
	}
	| charset charset_name opt_bin_mod	{ Lex->charset=$2; }
        | BINARY opt_bin_charset { Lex->type|= BINCMP_FLAG; };

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

opt_bin_charset:
4534
        /* empty */ { Lex->charset= NULL; }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
4535
	| ASCII_SYM	{ Lex->charset=&my_charset_latin1; }
4536 4537
	| UNICODE_SYM
	{
4538 4539
	  if (!(Lex->charset=get_charset_by_csname("ucs2",
                                                   MY_CS_PRIMARY,MYF(0))))
4540
	  {
4541
	    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), "ucs2");
4542 4543 4544
	    YYABORT;
	  }
	}
4545
	| charset charset_name	{ Lex->charset=$2; } ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4546

4547 4548 4549
opt_primary:
	/* empty */
	| PRIMARY_SYM
4550
	;
4551

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4552
references:
4553 4554
	REFERENCES table_ident
	{
4555
	  LEX *lex=Lex;
4556 4557 4558 4559 4560 4561
	  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
4562
	};
4563

4564
opt_ref_list:
4565
	/* empty */ opt_on_delete {}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4566
	| '(' ref_list ')' opt_on_delete {};
4567 4568 4569

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4572 4573 4574

opt_on_delete:
	/* empty */ {}
4575
	| opt_on_delete_list {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4576 4577 4578

opt_on_delete_list:
	opt_on_delete_list opt_on_delete_item {}
4579
	| opt_on_delete_item {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4580 4581

opt_on_delete_item:
4582 4583 4584 4585
	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
4586
	| MATCH SIMPLE_SYM { Lex->fk_match_option= foreign_key::FK_MATCH_SIMPLE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4587 4588

delete_option:
4589 4590 4591 4592
	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
4593
	| SET DEFAULT    { $$= (int) foreign_key::FK_OPTION_DEFAULT;  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4594 4595

key_type:
4596
	key_or_index			    { $$= Key::MULTIPLE; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4597 4598
	| FULLTEXT_SYM opt_key_or_index	    { $$= Key::FULLTEXT; }
	| SPATIAL_SYM opt_key_or_index
hf@deer.(none)'s avatar
hf@deer.(none) committed
4599 4600 4601 4602
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
4603 4604
	    my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
4605 4606 4607
	    YYABORT;
#endif
	  };
4608 4609 4610

constraint_key_type:
	PRIMARY_SYM KEY_SYM  { $$= Key::PRIMARY; }
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4611
	| UNIQUE_SYM opt_key_or_index { $$= Key::UNIQUE; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4612 4613 4614

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

hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
4617 4618 4619 4620 4621
opt_key_or_index:
	/* empty */ {}
	| key_or_index
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4622 4623
keys_or_index:
	KEYS {}
4624
	| INDEX_SYM {}
4625
	| INDEXES {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4626

4627
opt_unique_or_fulltext:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4628 4629
	/* empty */	{ $$= Key::MULTIPLE; }
	| UNIQUE_SYM	{ $$= Key::UNIQUE; }
serg@serg.mylan's avatar
serg@serg.mylan committed
4630
	| FULLTEXT_SYM	{ $$= Key::FULLTEXT;}
hf@deer.(none)'s avatar
hf@deer.(none) committed
4631 4632 4633 4634 4635
	| SPATIAL_SYM
	  {
#ifdef HAVE_SPATIAL
	    $$= Key::SPATIAL;
#else
serg@serg.mylan's avatar
serg@serg.mylan committed
4636
            my_error(ER_FEATURE_DISABLED, MYF(0),
4637
                     sym_group_geom.name, sym_group_geom.needed_define);
hf@deer.(none)'s avatar
hf@deer.(none) committed
4638 4639 4640
	    YYABORT;
#endif
	  }
serg@serg.mylan's avatar
serg@serg.mylan committed
4641
        ;
4642

4643 4644
init_key_options:
	{
4645
	  Lex->key_create_info= default_key_create_info;
4646 4647 4648 4649
	}
	;

/*
4650
  For now, key_alg initializies lex->key_create_info.
4651 4652 4653 4654
  In the future, when all key options are after key definition,
  we can remove key_alg and move init_key_options to key_options
*/

4655
key_alg:
4656
	/* empty */ init_key_options
4657
	| init_key_options key_using_alg
4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668
	;

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

key_opts:
	key_opt
	| key_opts key_opt
	;
4669 4670 4671 4672 4673 4674

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

4675
key_opt:
4676
        key_using_alg
4677
	| KEY_BLOCK_SIZE opt_equal ulong_num
4678
	  { Lex->key_create_info.block_size= $3; }
4679 4680 4681
	| WITH PARSER_SYM IDENT_sys
          {
            if (plugin_is_ready(&$3, MYSQL_FTPARSER_PLUGIN))
4682
              Lex->key_create_info.parser_name= $3;
4683 4684 4685 4686 4687 4688 4689 4690
            else
            {
              my_error(ER_FUNCTION_NOT_DEFINED, MYF(0), $3.str);
              YYABORT;
            }
          }
        ;

4691

4692
btree_or_rtree:
4693
	BTREE_SYM	{ $$= HA_KEY_ALG_BTREE; }
hf@deer.(none)'s avatar
hf@deer.(none) committed
4694 4695 4696 4697
	| RTREE_SYM
	  {
	    $$= HA_KEY_ALG_RTREE;
	  }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4698
	| HASH_SYM	{ $$= HA_KEY_ALG_HASH; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4699 4700 4701

key_list:
	key_list ',' key_part order_dir { Lex->col_list.push_back($3); }
4702
	| key_part order_dir		{ Lex->col_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4703 4704 4705

key_part:
	ident			{ $$=new key_part_spec($1.str); }
4706
	| ident '(' NUM ')'
4707 4708 4709 4710
        {
          int key_part_len= atoi($3.str);
          if (!key_part_len)
          {
4711
            my_error(ER_KEY_PART_0, MYF(0), $1.str);
4712 4713 4714
          }
          $$=new key_part_spec($1.str,(uint) key_part_len);
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4715 4716 4717

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

4720
opt_component:
4721 4722
        /* empty */      { $$= null_lex_str; }
        | '.' ident      { $$= $2; };
4723

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4724 4725
string_list:
	text_string			{ Lex->interval_list.push_back($1); }
4726
	| string_list ',' text_string	{ Lex->interval_list.push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4727 4728 4729 4730 4731 4732 4733 4734

/*
** Alter table
*/

alter:
	ALTER opt_ignore TABLE_SYM table_ident
	{
4735
	  THD *thd= YYTHD;
4736
	  LEX *lex= thd->lex;
4737 4738
          lex->name.str= 0;
          lex->name.length= 0;
4739 4740
	  lex->sql_command= SQLCOM_ALTER_TABLE;
	  lex->duplicates= DUP_ERROR; 
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4741 4742
	  if (!lex->select_lex.add_table_to_list(thd, $4, NULL,
						 TL_OPTION_UPDATING))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4743 4744 4745 4746
	    YYABORT;
	  lex->create_list.empty();
	  lex->key_list.empty();
	  lex->col_list.empty();
4747
          lex->select_lex.init_order();
4748
	  lex->like_name= 0;
4749 4750
	  lex->select_lex.db=
            ((TABLE_LIST*) lex->select_lex.table_list.first)->db;
4751
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
4752
	  lex->create_info.db_type= 0;
4753
	  lex->create_info.default_table_charset= NULL;
4754
	  lex->create_info.row_type= ROW_TYPE_NOT_USED;
4755
	  lex->alter_info.reset();
4756
	  lex->alter_info.flags= 0;
4757
          lex->no_write_to_binlog= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4758
	}
4759
	alter_commands
4760
	{}
4761
	| ALTER DATABASE ident_or_empty
4762 4763 4764 4765 4766
          {
            Lex->create_info.default_table_charset= NULL;
            Lex->create_info.used_fields= 0;
          }
          opt_create_database_options
4767 4768
	  {
	    LEX *lex=Lex;
4769
            THD *thd= Lex->thd;
4770
	    lex->sql_command=SQLCOM_ALTER_DB;
4771
	    lex->name= $3;
4772 4773
            if (lex->name.str == NULL &&
                thd->copy_db_to(&lex->name.str, &lex->name.length))
4774
              YYABORT;
4775 4776 4777 4778 4779
	  }
	| ALTER PROCEDURE sp_name
	  {
	    LEX *lex= Lex;

4780 4781 4782 4783 4784
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
	      YYABORT;
	    }
4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796
	    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;
4797

4798 4799 4800 4801 4802
	    if (lex->sphead)
	    {
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
	      YYABORT;
	    }
4803 4804 4805 4806 4807 4808 4809 4810 4811
	    bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
          }
	  sp_a_chistics
	  {
	    LEX *lex=Lex;

	    lex->sql_command= SQLCOM_ALTER_FUNCTION;
	    lex->spname= $3;
	  }
4812 4813
        | ALTER view_algorithm_opt definer view_suid
          VIEW_SYM table_ident
4814 4815 4816 4817 4818 4819
	  {
	    THD *thd= YYTHD;
	    LEX *lex= thd->lex;
	    lex->sql_command= SQLCOM_CREATE_VIEW;
	    lex->create_view_mode= VIEW_ALTER;
	    /* first table in list is target VIEW name */
4820
	    lex->select_lex.add_table_to_list(thd, $6, NULL, TL_OPTION_UPDATING);
4821
	  }
4822
	  view_list_opt AS view_select view_check_option
4823
	  {}
4824
	| ALTER EVENT_SYM sp_name
4825
          /*
4826 4827
            BE CAREFUL when you add a new rule to update the block where
            YYTHD->client_capabilities is set back to original value
4828
          */
4829
          {
4830
            /* 
4831 4832 4833 4834 4835
              It is safe to use Lex->spname because
              ALTER EVENT xxx RENATE TO yyy DO ALTER EVENT RENAME TO
              is not allowed. Lex->spname is used in the case of RENAME TO
              If it had to be supported spname had to be added to
              Event_parse_data.
4836
            */
4837

4838
            if (!(Lex->event_parse_data= Event_parse_data::new_instance(YYTHD)))
andrey@lmy004's avatar
andrey@lmy004 committed
4839
              YYABORT;
4840
            Lex->event_parse_data->identifier= $3;
4841

4842
            /*
4843
              We have to turn off CLIENT_MULTI_QUERIES while parsing a
4844 4845
              stored procedure, otherwise yylex will chop it into pieces
              at each ';'.
4846
            */
andrey@lmy004's avatar
andrey@lmy004 committed
4847
            $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
4848
            YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
4849 4850

            Lex->sql_command= SQLCOM_ALTER_EVENT;
4851
          }
4852 4853 4854 4855 4856
          ev_alter_on_schedule_completion
          opt_ev_rename_to
          opt_ev_status
          opt_ev_comment
          opt_ev_sql_stmt
4857
          {
4858 4859 4860 4861 4862 4863 4864 4865
            /*
              $1 - ALTER
              $2 - EVENT_SYM
              $3 - sp_name
              $4 - the block above
            */
            YYTHD->client_capabilities |= $<ulong_num>4;

4866
            if (!($5 || $6 || $7 || $8 || $9))
andrey@lmy004's avatar
andrey@lmy004 committed
4867 4868 4869 4870
            {
	      yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
4871 4872 4873 4874
            /*
              sql_command is set here because some rules in ev_sql_stmt
              can overwrite it
            */
andrey@lmy004's avatar
andrey@lmy004 committed
4875
            Lex->sql_command= SQLCOM_ALTER_EVENT;
andrey@lmy004's avatar
andrey@lmy004 committed
4876
          }
4877 4878 4879 4880 4881
        | ALTER TABLESPACE alter_tablespace_info
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= ALTER_TABLESPACE;
          }
4882
        | ALTER LOGFILE_SYM GROUP alter_logfile_group_info
4883 4884 4885 4886 4887 4888 4889 4890 4891
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= ALTER_LOGFILE_GROUP;
          }
        | ALTER TABLESPACE change_tablespace_info
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= CHANGE_FILE_TABLESPACE;
          }
4892
        | ALTER TABLESPACE change_tablespace_access
4893 4894 4895 4896
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= ALTER_ACCESS_MODE_TABLESPACE;
          }
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
4897 4898 4899 4900 4901 4902 4903
        | ALTER SERVER_SYM ident_or_text OPTIONS_SYM '(' server_options_list ')'
          {
            LEX *lex= Lex;
            Lex->sql_command= SQLCOM_ALTER_SERVER;
            Lex->server_options.server_name= $3.str;
            Lex->server_options.server_name_length= $3.length;
          }
4904
	;
4905

4906 4907 4908 4909
ev_alter_on_schedule_completion: /* empty */ { $$= 0;}
        | ON SCHEDULE_SYM ev_schedule_time { $$= 1; }
        | ev_on_completion { $$= 1; }
        | ON SCHEDULE_SYM ev_schedule_time ev_on_completion { $$= 1; }
4910
      ;
4911

4912
opt_ev_rename_to: /* empty */ { $$= 0;}
4913 4914
        | RENAME TO_SYM sp_name
          {
4915 4916 4917 4918 4919
            /*
              Use lex's spname to hold the new name.
              The original name is in the Event_parse_data object
            */
            Lex->spname= $3; 
4920
            $$= 1;
4921 4922
          }
      ;
4923 4924 4925

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

4928

4929
ident_or_empty:
4930 4931
	/* empty */  { $$.str= 0; $$.length= 0; }
	| ident      { $$= $1; };
4932

4933
alter_commands:
4934 4935
	| DISCARD TABLESPACE { Lex->alter_info.tablespace_op= DISCARD_TABLESPACE; }
	| IMPORT TABLESPACE { Lex->alter_info.tablespace_op= IMPORT_TABLESPACE; }
4936
        | alter_list
4937
          opt_partitioning
4938 4939 4940
        | alter_list
          remove_partitioning
        | remove_partitioning
4941
        | partitioning
4942
/*
4943
  This part was added for release 5.1 by Mikael Ronstrm.
4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954
  From here we insert a number of commands to manage the partitions of a
  partitioned table such as adding partitions, dropping partitions,
  reorganising partitions in various manners. In future releases the list
  will be longer and also include moving partitions to a
  new table and so forth.
*/
        | add_partition_rule
        | DROP PARTITION_SYM alt_part_name_list
          {
	    Lex->alter_info.flags|= ALTER_DROP_PARTITION;
          }
4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995
        | REBUILD_SYM PARTITION_SYM opt_no_write_to_binlog
          all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_REBUILD_PARTITION;
            lex->no_write_to_binlog= $3;
          }
        | OPTIMIZE PARTITION_SYM opt_no_write_to_binlog
          all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_OPTIMIZE_PARTITION;
            lex->no_write_to_binlog= $3;
            lex->check_opt.init();
          }
          opt_no_write_to_binlog opt_mi_check_type
        | ANALYZE_SYM PARTITION_SYM opt_no_write_to_binlog
          all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_ANALYZE_PARTITION;
            lex->no_write_to_binlog= $3;
            lex->check_opt.init();
          }
          opt_mi_check_type
        | CHECK_SYM PARTITION_SYM all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_CHECK_PARTITION;
            lex->check_opt.init();
          }
          opt_mi_check_type
        | REPAIR PARTITION_SYM opt_no_write_to_binlog
          all_or_alt_part_name_list
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_REPAIR_PARTITION;
            lex->no_write_to_binlog= $3;
            lex->check_opt.init();
          }
          opt_mi_repair_type
4996
        | COALESCE PARTITION_SYM opt_no_write_to_binlog real_ulong_num
4997 4998 4999
          {
            LEX *lex= Lex;
	    lex->alter_info.flags|= ALTER_COALESCE_PARTITION;
5000 5001
            lex->no_write_to_binlog= $3;
	    lex->alter_info.no_parts= $4;
5002 5003
          }
        | reorg_partition_rule
5004 5005
        ;

5006 5007 5008 5009 5010 5011 5012
remove_partitioning:
        REMOVE_SYM PARTITIONING_SYM
        {
          Lex->alter_info.flags|= ALTER_REMOVE_PARTITIONING;
        }
        ;

5013
all_or_alt_part_name_list:
5014
        ALL
5015 5016 5017 5018 5019 5020
        {
	  Lex->alter_info.flags|= ALTER_ALL_PARTITION;
        }
        | alt_part_name_list
        ;

5021
add_partition_rule:
5022
        ADD PARTITION_SYM opt_no_write_to_binlog
5023 5024 5025 5026 5027
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
5028
            mem_alloc_error(sizeof(partition_info));
5029 5030 5031
            YYABORT;
          }
	  lex->alter_info.flags|= ALTER_ADD_PARTITION;
5032
          lex->no_write_to_binlog= $3;
5033 5034 5035 5036 5037 5038 5039 5040 5041
        }
        add_part_extra
        {}
        ;

add_part_extra:
        | '(' part_def_list ')'
        {
          LEX *lex= Lex;
5042
          lex->part_info->no_parts= lex->part_info->partitions.elements;
5043
        }
5044
        | PARTITIONS_SYM real_ulong_num
5045 5046 5047 5048 5049 5050 5051
        {
          LEX *lex= Lex;
          lex->part_info->no_parts= $2;
        }
        ;

reorg_partition_rule:
5052
        REORGANIZE_SYM PARTITION_SYM opt_no_write_to_binlog
5053 5054 5055 5056 5057
        {
          LEX *lex= Lex;
          lex->part_info= new partition_info();
          if (!lex->part_info)
          {
5058
            mem_alloc_error(sizeof(partition_info));
5059 5060
            YYABORT;
          }
5061
          lex->no_write_to_binlog= $3;
5062
        }
5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076
        reorg_parts_rule
        ;

reorg_parts_rule:
        /* empty */
        {
	  Lex->alter_info.flags|= ALTER_TABLE_REORG;
        }
        |
        alt_part_name_list
        {
	  Lex->alter_info.flags|= ALTER_REORGANIZE_PARTITION;
        }
        INTO '(' part_def_list ')'
5077 5078
        {
          LEX *lex= Lex;
5079
          lex->part_info->no_parts= lex->part_info->partitions.elements;
5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090
        }
        ;

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

alt_part_name_item:
        ident
        {
5091 5092 5093 5094 5095
          if (Lex->alter_info.partition_names.push_back($1.str))
          {
            mem_alloc_error(1);
            YYABORT;
          }
5096 5097 5098 5099 5100 5101 5102
        }
        ;

/*
  End of management of partition commands
*/

5103 5104 5105 5106
alter_list:
        alter_list_item
	| alter_list ',' alter_list_item
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5107 5108

add_column:
5109
	ADD opt_column
5110 5111
	{
	  LEX *lex=Lex;
5112 5113
	  lex->change=0;
	  lex->alter_info.flags|= ALTER_ADD_COLUMN;
5114
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5115 5116

alter_list_item:
5117 5118 5119 5120
	add_column column_def opt_place { }
	| ADD key_def
	  {
	    Lex->alter_info.flags|= ALTER_ADD_INDEX;
5121
	  }
5122 5123 5124 5125
	| add_column '(' field_list ')'
          {
	    Lex->alter_info.flags|= ALTER_ADD_COLUMN | ALTER_ADD_INDEX;
          }
5126 5127 5128
	| CHANGE opt_column field_ident
	  {
	     LEX *lex=Lex;
5129
	     lex->change= $3.str;
5130
	     lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
5131
	  }
5132
          field_spec opt_place
5133 5134 5135
        | MODIFY_SYM opt_column field_ident
          {
            LEX *lex=Lex;
5136
            lex->length=lex->dec=0; lex->type=0;
5137
            lex->default_value= lex->on_update_value= 0;
5138
            lex->comment=null_lex_str;
5139
	    lex->charset= NULL;
5140
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN;
5141 5142 5143 5144
          }
          type opt_attribute
          {
            LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5145
            if (add_field_to_list(lex->thd,$3.str,
5146 5147
                                  (enum enum_field_types) $5,
                                  lex->length,lex->dec,lex->type,
5148
                                  lex->default_value, lex->on_update_value,
5149
                                  &lex->comment,
5150
				  $3.str, &lex->interval_list, lex->charset,
5151
				  lex->uint_geom_type))
5152 5153 5154
	       YYABORT;
          }
          opt_place
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5155
	| DROP opt_column field_ident opt_restrict
5156 5157
	  {
	    LEX *lex=Lex;
5158
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::COLUMN,
5159
                                                               $3.str));
5160
	    lex->alter_info.flags|= ALTER_DROP_COLUMN;
5161
	  }
5162 5163
	| DROP FOREIGN KEY_SYM opt_ident
          {
5164
	    Lex->alter_info.flags|= ALTER_DROP_INDEX | ALTER_FOREIGN_KEY;
5165
          }
5166 5167 5168
	| DROP PRIMARY_SYM KEY_SYM
	  {
	    LEX *lex=Lex;
5169 5170 5171
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
				               primary_key_name));
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
5172
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5173
	| DROP key_or_index field_ident
5174 5175
	  {
	    LEX *lex=Lex;
5176 5177 5178
	    lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
					                       $3.str));
	    lex->alter_info.flags|= ALTER_DROP_INDEX;
5179
	  }
5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191
	| 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;
          }
5192
	| ALTER opt_column field_ident SET DEFAULT signed_literal
5193 5194
	  {
	    LEX *lex=Lex;
5195
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,$6));
5196
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
5197
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5198
	| ALTER opt_column field_ident DROP DEFAULT
5199 5200
	  {
	    LEX *lex=Lex;
5201 5202
	    lex->alter_info.alter_list.push_back(new Alter_column($3.str,
                                                                  (Item*) 0));
5203
	    lex->alter_info.flags|= ALTER_CHANGE_COLUMN_DEFAULT;
5204
	  }
5205
	| RENAME opt_to table_ident
5206
	  {
5207
	    LEX *lex=Lex;
5208
            THD *thd= lex->thd;
5209
	    uint dummy;
5210
	    lex->select_lex.db=$3->db.str;
5211
            if (lex->select_lex.db == NULL &&
5212
                thd->copy_db_to(&lex->select_lex.db, &dummy))
5213 5214 5215
            {
              YYABORT;
            }
5216
            if (check_table_name($3->table.str,$3->table.length) ||
5217
                $3->db.str && check_db_name(&$3->db))
5218
            {
5219
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3->table.str);
5220 5221
              YYABORT;
            }
5222
	    lex->name= $3->table;
5223
	    lex->alter_info.flags|= ALTER_RENAME;
5224
	  }
5225 5226 5227 5228 5229 5230 5231 5232 5233 5234
	| 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))
	    {
5235 5236
	      my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                       $5->name, $4->csname);
5237 5238
	      YYABORT;
	    }
5239
	    LEX *lex= Lex;
5240
	    lex->create_info.table_charset=
monty@mysql.com's avatar
monty@mysql.com committed
5241 5242 5243
	      lex->create_info.default_table_charset= $5;
	    lex->create_info.used_fields|= (HA_CREATE_USED_CHARSET |
					    HA_CREATE_USED_DEFAULT_CHARSET);
5244
	    lex->alter_info.flags|= ALTER_CONVERT;
5245
	  }
5246
        | create_table_options_space_separated
5247 5248
	  {
	    LEX *lex=Lex;
5249
	    lex->alter_info.flags|= ALTER_OPTIONS;
5250
	  }
5251 5252 5253 5254
	| FORCE_SYM
	  {
	    Lex->alter_info.flags|= ALTER_FORCE;
	   }
5255
	| order_clause
5256 5257
	  {
	    LEX *lex=Lex;
5258
	    lex->alter_info.flags|= ALTER_ORDER;
5259
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5260 5261 5262

opt_column:
	/* empty */	{}
5263
	| COLUMN_SYM	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5264 5265

opt_ignore:
5266 5267 5268
	/* empty */	{ Lex->ignore= 0;}
	| IGNORE_SYM	{ Lex->ignore= 1;}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5269 5270

opt_restrict:
5271 5272 5273 5274
	/* 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
5275 5276 5277 5278

opt_place:
	/* empty */	{}
	| AFTER_SYM ident { store_position_for_column($2.str); }
5279
	| FIRST_SYM	  { store_position_for_column(first_keyword); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5280 5281 5282 5283

opt_to:
	/* empty */	{}
	| TO_SYM	{}
5284
	| EQ		{}
5285
	| AS		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5286

nick@mysql.com's avatar
nick@mysql.com committed
5287
/*
5288
  SLAVE START and SLAVE STOP are deprecated. We keep them for compatibility.
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5289 5290
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5291
slave:
5292
	  START_SYM SLAVE slave_thread_opts
5293 5294 5295 5296 5297 5298
          {
	    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
5299
            /* If you change this code don't forget to update SLAVE START too */
5300 5301 5302
          }
          slave_until
          {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5303 5304 5305 5306 5307
        | 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
5308
            /* If you change this code don't forget to update SLAVE STOP too */
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5309
          }
5310 5311 5312 5313 5314
	| SLAVE START_SYM slave_thread_opts
         {
	   LEX *lex=Lex;
           lex->sql_command = SQLCOM_SLAVE_START;
	   lex->type = 0;
5315 5316 5317 5318 5319
	    /* We'll use mi structure for UNTIL options */
	    bzero((char*) &lex->mi, sizeof(lex->mi));
          }
          slave_until
          {}
5320 5321 5322 5323 5324 5325 5326 5327
	| 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
5328

5329
start:
5330 5331
	START_SYM TRANSACTION_SYM start_transaction_opts
        {
5332 5333 5334
          LEX *lex= Lex;
          lex->sql_command= SQLCOM_BEGIN;
          lex->start_transaction_opt= $3;
5335
        }
5336 5337
	;

5338 5339 5340 5341 5342 5343
start_transaction_opts:
        /*empty*/ { $$ = 0; }
        | WITH CONSISTENT_SYM SNAPSHOT_SYM
        {
           $$= MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT;
        }
5344
        ;
5345

5346
slave_thread_opts:
5347 5348
	{ Lex->slave_thd_opt= 0; }
	slave_thread_opt_list
5349
        {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5350
	;
5351 5352

slave_thread_opt_list:
5353
	slave_thread_opt
5354 5355
	| slave_thread_opt_list ',' slave_thread_opt
	;
5356 5357

slave_thread_opt:
5358
	/*empty*/	{}
5359
	| SQL_THREAD	{ Lex->slave_thd_opt|=SLAVE_SQL; }
5360
	| RELAY_THREAD 	{ Lex->slave_thd_opt|=SLAVE_IO; }
5361
	;
5362

5363 5364 5365 5366 5367 5368 5369 5370 5371 5372
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
5373 5374
               my_message(ER_BAD_SLAVE_UNTIL_COND,
                          ER(ER_BAD_SLAVE_UNTIL_COND), MYF(0));
5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385
               YYABORT;
            }

          }
	;

slave_until_opts:
       master_file_def
       | slave_until_opts ',' master_file_def ;


5386 5387 5388 5389
restore:
	RESTORE_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_RESTORE_TABLE;
5390 5391
           WARN_DEPRECATED(yythd, "5.2", "RESTORE TABLE",
                           "MySQL Administrator (mysqldump, mysql)");
5392
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5393
	table_list FROM TEXT_STRING_sys
5394 5395
        {
	  Lex->backup_dir = $6.str;
5396
        };
5397

5398 5399 5400 5401
backup:
	BACKUP_SYM table_or_tables
	{
	   Lex->sql_command = SQLCOM_BACKUP_TABLE;
5402 5403
           WARN_DEPRECATED(yythd, "5.2", "BACKUP TABLE",
                           "MySQL Administrator (mysqldump, mysql)");
5404
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5405
	table_list TO_SYM TEXT_STRING_sys
5406 5407
        {
	  Lex->backup_dir = $6.str;
5408
        };
5409

5410 5411 5412 5413 5414 5415
checksum:
        CHECKSUM_SYM table_or_tables
	{
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_CHECKSUM;
	}
5416 5417
	table_list opt_checksum_type
        {}
5418 5419
	;

5420 5421 5422 5423 5424 5425
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
5426
repair:
5427
	REPAIR opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5428
	{
5429 5430
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_REPAIR;
5431
           lex->no_write_to_binlog= $2;
5432
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5433
	}
5434 5435 5436
	table_list opt_mi_repair_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5437

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5438
opt_mi_repair_type:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5439
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
5440
	| mi_repair_types {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5441

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5442 5443
mi_repair_types:
	mi_repair_type {}
5444
	| mi_repair_type mi_repair_types {};
5445

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5446
mi_repair_type:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5447
	QUICK          { Lex->check_opt.flags|= T_QUICK; }
5448
	| EXTENDED_SYM { Lex->check_opt.flags|= T_EXTEND; }
5449
        | USE_FRM      { Lex->check_opt.sql_flags|= TT_USEFRM; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5450 5451

analyze:
5452
	ANALYZE_SYM opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5453
	{
5454 5455
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_ANALYZE;
5456
           lex->no_write_to_binlog= $2;
5457
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5458
	}
5459 5460 5461
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5462

5463 5464 5465 5466 5467 5468
binlog_base64_event:
        BINLOG_SYM TEXT_STRING_sys
        {
           Lex->sql_command = SQLCOM_BINLOG_BASE64_EVENT;
           Lex->comment= $2;
        }
5469
        ;
5470

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5471
check:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5472
	CHECK_SYM table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5473
	{
5474 5475 5476 5477 5478 5479 5480 5481 5482
	  LEX *lex=Lex;

	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "CHECK");
	    YYABORT;
	  }
	  lex->sql_command = SQLCOM_CHECK;
	  lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5483
	}
5484 5485 5486
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5487

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5488 5489
opt_mi_check_type:
	/* empty */ { Lex->check_opt.flags = T_MEDIUM; }
5490
	| mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5491 5492 5493

mi_check_types:
	mi_check_type {}
5494
	| mi_check_type mi_check_types {};
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
5495 5496 5497 5498 5499 5500

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; }
5501 5502
	| 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
5503

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5504
optimize:
5505
	OPTIMIZE opt_no_write_to_binlog table_or_tables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5506
	{
5507 5508
	   LEX *lex=Lex;
	   lex->sql_command = SQLCOM_OPTIMIZE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5509
           lex->no_write_to_binlog= $2;
5510
	   lex->check_opt.init();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5511
	}
5512 5513 5514
	table_list opt_mi_check_type
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5515

5516 5517 5518
opt_no_write_to_binlog:
	/* empty */        { $$= 0; }
	| NO_WRITE_TO_BINLOG  { $$= 1; }
5519
	| LOCAL_SYM  { $$= 1; }
5520 5521
	;

5522 5523 5524
rename:
	RENAME table_or_tables
	{
5525
          Lex->sql_command= SQLCOM_RENAME_TABLE;
5526
	}
5527 5528
	table_to_table_list
	{}
5529 5530 5531 5532 5533 5534 5535
	| RENAME DATABASE
          {
            Lex->db_list.empty();
            Lex->sql_command= SQLCOM_RENAME_DB;
          }
          db_to_db
          {}
5536 5537 5538 5539
	| RENAME USER clear_privileges rename_list
          {
	    Lex->sql_command = SQLCOM_RENAME_USER;
          }
5540
	;
5541

5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554
rename_list:
        user TO_SYM user
        {
          if (Lex->users_list.push_back($1) || Lex->users_list.push_back($3))
            YYABORT;
        }
        | rename_list ',' user TO_SYM user
          {
            if (Lex->users_list.push_back($3) || Lex->users_list.push_back($5))
              YYABORT;
          }
        ;

5555 5556
table_to_table_list:
	table_to_table
5557
	| table_to_table_list ',' table_to_table;
5558 5559 5560

table_to_table:
	table_ident TO_SYM table_ident
5561
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5562
	  LEX *lex=Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5563
	  SELECT_LEX *sl= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5564 5565 5566 5567
	  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))
5568
	    YYABORT;
5569
	};
5570

5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581
db_to_db:
	ident TO_SYM ident
	{
	  LEX *lex=Lex;
          if (Lex->db_list.push_back((LEX_STRING*)
                                     sql_memdup(&$1, sizeof(LEX_STRING))) ||
              Lex->db_list.push_back((LEX_STRING*)
                                     sql_memdup(&$3, sizeof(LEX_STRING))))
              YYABORT;
	};

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5582
keycache:
5583
        CACHE_SYM INDEX_SYM keycache_list IN_SYM key_cache_name
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5584 5585
        {
          LEX *lex=Lex;
5586
          lex->sql_command= SQLCOM_ASSIGN_TO_KEYCACHE;
5587
	  lex->ident= $5;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5588 5589 5590 5591 5592 5593 5594 5595
        }
        ;

keycache_list:
        assign_to_keycache
        | keycache_list ',' assign_to_keycache;

assign_to_keycache:
5596
        table_ident cache_keys_spec
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5597 5598 5599 5600 5601 5602
        {
          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(),
5603
                                      (List<String> *)0))
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5604 5605 5606 5607
            YYABORT;
        }
        ;

5608 5609 5610
key_cache_name:
	ident	   { $$= $1; }
	| DEFAULT  { $$ = default_key_cache_base; }
5611
	;
5612

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5613
preload:
5614
	LOAD INDEX_SYM INTO CACHE_SYM
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627
	{
	  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
5628
	table_ident cache_keys_spec opt_ignore_leaves
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5629 5630 5631
	{
	  LEX *lex=Lex;
	  SELECT_LEX *sel= &lex->select_lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5632 5633
	  if (!sel->add_table_to_list(lex->thd, $1, NULL, $3,
                                      TL_READ,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5634 5635 5636 5637 5638 5639
                                      sel->get_use_index(),
                                      (List<String> *)0))
            YYABORT;
	}
	;

5640
cache_keys_spec:
5641
        { Select->interval_list.empty(); }
5642 5643 5644 5645 5646 5647 5648
        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
5649

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5650
cache_key_list_or_empty:
5651
	/* empty */	{ Lex->select_lex.use_index_ptr= 0; }
5652
	| opt_key_or_index '(' key_usage_list2 ')'
5653 5654 5655 5656
	  {
            SELECT_LEX *sel= &Lex->select_lex;
	    sel->use_index_ptr= &sel->use_index;
	  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5657 5658
	;

5659
opt_ignore_leaves:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5660 5661 5662 5663 5664
	/* empty */
	{ $$= 0; }
	| IGNORE_SYM LEAVES { $$= TL_OPTION_IGNORE_LEAVES; }
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5665
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5666
  Select : retrieve data from table
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5667 5668 5669 5670
*/


select:
5671 5672 5673 5674
	select_init
	{
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_SELECT;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5675 5676
	}
	;
5677

5678
/* Need select_init2 for subselects. */
5679
select_init:
5680
	SELECT_SYM select_init2
5681
	|
5682 5683 5684 5685
	'(' select_paren ')' union_opt;

select_paren:
	SELECT_SYM select_part2
5686
	  {
5687
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5688
            SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5689
	    if (sel->set_braces(1))
5690
	    {
5691
	      yyerror(ER(ER_SYNTAX_ERROR));
5692 5693
	      YYABORT;
	    }
5694 5695 5696 5697 5698 5699 5700 5701
            if (sel->linkage == UNION_TYPE &&
                !sel->master_unit()->first_select()->braces &&
                sel->master_unit()->first_select()->linkage ==
                UNION_TYPE)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
5702
            /* select in braces, can't contain global parameters */
5703 5704 5705
	    if (sel->master_unit()->fake_select_lex)
              sel->master_unit()->global_parameters=
                 sel->master_unit()->fake_select_lex;
5706 5707
          }
	| '(' select_paren ')';
5708

5709 5710
select_init2:
	select_part2
5711
        {
5712
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
5713
          SELECT_LEX * sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5714
          if (lex->current_select->set_braces(0))
5715
	  {
5716
	    yyerror(ER(ER_SYNTAX_ERROR));
5717 5718
	    YYABORT;
	  }
5719 5720
	  if (sel->linkage == UNION_TYPE &&
	      sel->master_unit()->first_select()->braces)
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5721
	  {
5722
	    yyerror(ER(ER_SYNTAX_ERROR));
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5723 5724
	    YYABORT;
	  }
5725
	}
5726
	union_clause
5727 5728
	;

5729
select_part2:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5730
	{
5731 5732
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
5733 5734
	  if (sel->linkage != UNION_TYPE)
	    mysql_init_select(lex);
5735
	  lex->current_select->parsing_place= SELECT_LIST;
5736 5737 5738
	}
	select_options select_item_list
	{
5739
	  Select->parsing_place= NO_MATTER;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5740
	}
5741
	select_into select_lock_type;
5742

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5743
select_into:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
5744
	opt_order_clause opt_limit_clause {}
5745
        | into
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5746
	| select_from
5747 5748
	| into select_from
	| select_from into;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5749 5750

select_from:
5751 5752
	  FROM join_table_list where_clause group_clause having_clause
	       opt_order_clause opt_limit_clause procedure_clause
5753
        | FROM DUAL_SYM where_clause opt_limit_clause
timour@mysql.com's avatar
timour@mysql.com committed
5754 5755 5756 5757
          /* 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 :) */
5758
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5759 5760 5761

select_options:
	/* empty*/
monty@mysql.com's avatar
monty@mysql.com committed
5762 5763
	| select_option_list
	  {
joreland@mysql.com's avatar
joreland@mysql.com committed
5764
	    if (Select->options & SELECT_DISTINCT && Select->options & SELECT_ALL)
monty@mysql.com's avatar
monty@mysql.com committed
5765
	    {
monty@mysql.com's avatar
monty@mysql.com committed
5766
	      my_error(ER_WRONG_USAGE, MYF(0), "ALL", "DISTINCT");
monty@mysql.com's avatar
monty@mysql.com committed
5767 5768 5769
              YYABORT;
	    }
          }
monty@mysql.com's avatar
monty@mysql.com committed
5770
	  ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5771 5772 5773

select_option_list:
	select_option_list select_option
5774
	| select_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5775 5776

select_option:
5777
	STRAIGHT_JOIN { Select->options|= SELECT_STRAIGHT_JOIN; }
5778 5779 5780 5781 5782 5783
	| HIGH_PRIORITY
	  {
	    if (check_simple_select())
	      YYABORT;
	    Lex->lock_option= TL_READ_HIGH_PRIORITY;
	  }
monty@mysql.com's avatar
monty@mysql.com committed
5784
	| DISTINCT         { Select->options|= SELECT_DISTINCT; }
5785 5786
	| SQL_SMALL_RESULT { Select->options|= SELECT_SMALL_RESULT; }
	| SQL_BIG_RESULT { Select->options|= SELECT_BIG_RESULT; }
5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798
	| SQL_BUFFER_RESULT
	  {
	    if (check_simple_select())
	      YYABORT;
	    Select->options|= OPTION_BUFFER_RESULT;
	  }
	| SQL_CALC_FOUND_ROWS
	  {
	    if (check_simple_select())
	      YYABORT;
	    Select->options|= OPTION_FOUND_ROWS;
	  }
5799 5800 5801 5802 5803 5804
	| 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;
          }
5805 5806
	| SQL_CACHE_SYM
	  {
5807 5808 5809 5810 5811 5812 5813
            /* Honor this flag only if SQL_NO_CACHE wasn't specified. */
            if (Lex->select_lex.sql_cache != SELECT_LEX::SQL_NO_CACHE)
            {
              Lex->safe_to_cache_query=1;
	      Lex->select_lex.options|= OPTION_TO_QUERY_CACHE;
              Lex->select_lex.sql_cache= SELECT_LEX::SQL_CACHE;
            }
5814
	  }
joreland@mysql.com's avatar
joreland@mysql.com committed
5815
	| ALL		    { Select->options|= SELECT_ALL; }
5816
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5817

monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5818 5819 5820
select_lock_type:
	/* empty */
	| FOR_SYM UPDATE_SYM
5821 5822
	  {
	    LEX *lex=Lex;
5823
	    lex->current_select->set_lock_for_tables(TL_WRITE);
5824
	    lex->safe_to_cache_query=0;
5825
	  }
5826
	| LOCK_SYM IN_SYM SHARE_SYM MODE_SYM
5827 5828
	  {
	    LEX *lex=Lex;
5829 5830
	    lex->current_select->
	      set_lock_for_tables(TL_READ_WITH_SHARED_LOCKS);
5831
	    lex->safe_to_cache_query=0;
5832 5833
	  }
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
5834

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5835 5836 5837 5838 5839
select_item_list:
	  select_item_list ',' select_item
	| select_item
	| '*'
	  {
5840
	    THD *thd= YYTHD;
5841 5842 5843 5844
	    if (add_item_to_list(thd,
                                 new Item_field(&thd->lex->current_select->
                                                context,
                                                NULL, NULL, "*")))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5845
	      YYABORT;
5846
	    (thd->lex->current_select->with_wild)++;
5847
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5848 5849 5850 5851 5852


select_item:
	  remember_name select_item2 remember_end select_alias
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5853
	    if (add_item_to_list(YYTHD, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5854 5855
	      YYABORT;
	    if ($4.str)
5856 5857
            {
              $2->is_autogenerated_name= FALSE;
5858
	      $2->set_name($4.str, $4.length, system_charset_info);
5859
            }
5860 5861 5862 5863 5864 5865
	    else if (!$2->name) {
	      char *str = $1;
	      if (str[-1] == '`')
	        str--;
	      $2->set_name(str,(uint) ($3 - str), YYTHD->charset());
	    }
5866
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5867 5868

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

remember_end:
5872
	{ $$=(char*) Lex->tok_end; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5873 5874 5875

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

select_alias:
5879
	/* empty */		{ $$=null_lex_str;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5880 5881 5882 5883 5884
	| 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
5885 5886 5887

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5890
/* all possible expressions */
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
5891
expr:	
5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913
	  bool_term { Select->expr_list.push_front(new List<Item>); }
          bool_or_expr
          {
            List<Item> *list= Select->expr_list.pop();
            if (list->elements)
            {
              list->push_front($1);
              $$= new Item_cond_or(*list);
              /* optimize construction of logical OR to reduce
                 amount of objects for complex expressions */
            }
            else
              $$= $1;
            delete list;
          }
	;

bool_or_expr:
	/* empty */
        | bool_or_expr or bool_term
          { Select->expr_list.head()->push_back($3); }
        ;
5914 5915

bool_term:
5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938
	bool_term XOR bool_term { $$= new Item_cond_xor($1,$3); }
	| bool_factor { Select->expr_list.push_front(new List<Item>); }
          bool_and_expr
          {
            List<Item> *list= Select->expr_list.pop();
            if (list->elements)
            {
              list->push_front($1);
              $$= new Item_cond_and(*list);
              /* optimize construction of logical AND to reduce
                 amount of objects for complex expressions */
            }
            else
              $$= $1;
            delete list;
          }
	;

bool_and_expr:
	/* empty */
        | bool_and_expr and bool_factor
          { Select->expr_list.head()->push_back($3); }
        ;
5939 5940 5941 5942 5943 5944

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

bool_test:
5945 5946 5947 5948
	bool_pri IS TRUE_SYM	{ $$= is_truth_value(YYTHD, $1,1,0); }
	| bool_pri IS not TRUE_SYM { $$= is_truth_value(YYTHD, $1,0,0); }
	| bool_pri IS FALSE_SYM	{ $$= is_truth_value(YYTHD, $1,0,1); }
	| bool_pri IS not FALSE_SYM { $$= is_truth_value(YYTHD, $1,1,1); }
5949 5950 5951 5952 5953 5954 5955
	| bool_pri IS UNKNOWN_SYM { $$= new Item_func_isnull($1); }
	| bool_pri IS not UNKNOWN_SYM { $$= new Item_func_isnotnull($1); }
	| bool_pri ;

bool_pri:
	bool_pri IS NULL_SYM	{ $$= new Item_func_isnull($1); }
	| bool_pri IS not NULL_SYM { $$= new Item_func_isnotnull($1); }
5956 5957 5958
	| bool_pri EQUAL_SYM predicate	{ $$= new Item_func_equal($1,$3); }
	| bool_pri comp_op predicate %prec EQ
	  { $$= (*$2)(0)->create($1,$3); }
5959 5960
	| bool_pri comp_op all_or_any '(' subselect ')' %prec EQ
	  { $$= all_any_subquery_creator($1, $2, $3, $5); }
5961 5962 5963
	| predicate ;

predicate:
5964 5965 5966 5967 5968 5969 5970 5971 5972
        bit_expr IN_SYM '(' subselect ')'
	  { $$= new Item_in_subselect($1, $4); }
	| bit_expr not IN_SYM '(' subselect ')'
          { $$= negate_expression(YYTHD, new Item_in_subselect($1, $5)); }
        | bit_expr IN_SYM '(' expr ')'
          {
              $$= new Item_func_eq($1, $4);
          }
	| bit_expr IN_SYM '(' expr ',' expr_list ')'
5973
	  { 
5974 5975 5976
              $6->push_front($4);
              $6->push_front($1);
              $$= new Item_func_in(*$6);
5977
          }
5978
        | bit_expr not IN_SYM '(' expr ')'
5979
          {
5980 5981 5982 5983 5984 5985 5986
              $$= new Item_func_ne($1, $5);
          }
	| bit_expr not IN_SYM '(' expr ',' expr_list ')'
          {
              $7->push_front($5);
              $7->push_front($1);
              Item_func_in *item = new Item_func_in(*$7);
5987 5988
              item->negate();
              $$= item;
5989
          }
5990 5991 5992
	| 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
5993 5994 5995 5996 5997
    {
      Item_func_between *item= new Item_func_between($1,$4,$6);
      item->negate();
      $$= item;
    }
5998 5999 6000 6001
	| 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
6002
          { $$= new Item_func_like($1,$3,$4,Lex->escape_used); }
6003
	| bit_expr not LIKE simple_expr opt_escape
6004
          { $$= new Item_func_not(new Item_func_like($1,$4,$5, Lex->escape_used)); }
6005 6006
	| bit_expr REGEXP bit_expr	{ $$= new Item_func_regex($1,$3); }
	| bit_expr not REGEXP bit_expr
6007
          { $$= negate_expression(YYTHD, new Item_func_regex($1,$4)); }
6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049
	| bit_expr ;

bit_expr:
	bit_expr '|' bit_term	{ $$= new Item_func_bit_or($1,$3); }
	| bit_term ;

bit_term:
	bit_term '&' bit_factor	{ $$= new Item_func_bit_and($1,$3); }
	| bit_factor ;

bit_factor:
	bit_factor SHIFT_LEFT value_expr
	  { $$= new Item_func_shift_left($1,$3); }
	| bit_factor SHIFT_RIGHT value_expr 
	  { $$= new Item_func_shift_right($1,$3); }
	| value_expr ;

value_expr:
	value_expr '+' term	{ $$= new Item_func_plus($1,$3); }
	| value_expr '-' term	{ $$= new Item_func_minus($1,$3); }
	| value_expr '+' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,0); }
	| value_expr '-' interval_expr interval
	  { $$= new Item_date_add_interval($1,$3,$4,1); }
	| term ;

term:
	term '*' factor		{ $$= new Item_func_mul($1,$3); }
	| term '/' factor	{ $$= new Item_func_div($1,$3); }
	| term '%' factor	{ $$= new Item_func_mod($1,$3); }
	| term DIV_SYM factor	{ $$= new Item_func_int_div($1,$3); }
	| term MOD_SYM factor	{ $$= new Item_func_mod($1,$3); }
	| factor ;

factor:
        factor '^' simple_expr	{ $$= new Item_func_bit_xor($1,$3); }
	| simple_expr ;

or:	OR_SYM | OR2_SYM;
and:	AND_SYM | AND_AND_SYM;
not:	NOT_SYM | NOT2_SYM;
not2:	'!' | NOT2_SYM;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6050

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062
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; }
        ;

6063
interval_expr:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6064
         INTERVAL_SYM expr { $$=$2; }
6065 6066
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6067 6068
simple_expr:
	simple_ident
6069 6070 6071 6072
        | function_call_keyword
        | function_call_nonkeyword
        | function_call_generic
        | function_call_conflict
6073
 	| simple_expr COLLATE_SYM ident_or_text %prec NEG
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6074
	  {
6075 6076 6077 6078 6079
            THD *thd= YYTHD;
            Item *i1= new (thd->mem_root) Item_string($3.str,
                                                      $3.length,
                                                      thd->charset());
	    $$= new (thd->mem_root) Item_func_set_collation($1, i1);
6080
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6081
	| literal
6082
	| param_marker
6083
	| variable
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6084
	| sum_expr
6085
	| simple_expr OR_OR_SYM simple_expr
6086
	  { $$= new (YYTHD->mem_root) Item_func_concat($1, $3); }
6087
	| '+' simple_expr %prec NEG	{ $$= $2; }
6088 6089 6090 6091 6092 6093
	| '-' simple_expr %prec NEG
          { $$= new (YYTHD->mem_root) Item_func_neg($2); }
	| '~' simple_expr %prec NEG
          { $$= new (YYTHD->mem_root) Item_func_bit_neg($2); }
	| not2 simple_expr %prec NEG
          { $$= negate_expression(YYTHD, $2); }
6094 6095
	| '(' subselect ')'   
          { 
6096
            $$= new (YYTHD->mem_root) Item_singlerow_subselect($2); 
6097
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6098
	| '(' expr ')'		{ $$= $2; }
6099 6100 6101
	| '(' expr ',' expr_list ')'
	  {
	    $4->push_front($2);
6102
	    $$= new (YYTHD->mem_root) Item_row(*$4);
6103
	  }
6104
	| ROW_SYM '(' expr ',' expr_list ')'
6105
	  {
6106
	    $5->push_front($3);
6107
	    $$= new (YYTHD->mem_root) Item_row(*$5);
6108
	  }
6109 6110
	| EXISTS '(' subselect ')' 
          {
6111
            $$= new (YYTHD->mem_root) Item_exists_subselect($3); 
6112
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6113
	| '{' ident expr '}'	{ $$= $3; }
6114
        | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
6115 6116 6117 6118 6119 6120
          {
            $2->push_front($5);
            Item_func_match *i1= new (YYTHD->mem_root) Item_func_match(*$2, $6);
            Select->add_ftfunc_to_list(i1);
            $$= i1;
          }
6121
	| BINARY simple_expr %prec NEG
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6122
	  {
6123 6124
            $$= create_func_cast(YYTHD, $2, ITEM_CAST_CHAR, -1, 0,
                                 &my_charset_bin);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6125
	  }
6126
	| CAST_SYM '(' expr AS cast_type ')'
6127
	  {
6128
            LEX *lex= Lex;
6129
	    $$= create_func_cast(YYTHD, $3, $5,
6130 6131 6132
                                 lex->length ? atoi(lex->length) : -1,
                                 lex->dec ? atoi(lex->dec) : 0,
                                 lex->charset);
6133 6134
            if (!$$)
              YYABORT;
6135
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6136
	| CASE_SYM opt_expr WHEN_SYM when_list opt_else END
6137
	  { $$= new (YYTHD->mem_root) Item_func_case(* $4, $2, $5 ); }
6138
	| CONVERT_SYM '(' expr ',' cast_type ')'
6139
	  {
6140
	    $$= create_func_cast(YYTHD, $3, $5,
6141
				 Lex->length ? atoi(Lex->length) : -1,
6142
                                 Lex->dec ? atoi(Lex->dec) : 0,
6143
				 Lex->charset);
6144 6145
            if (!$$)
              YYABORT;
6146
	  }
6147
	| CONVERT_SYM '(' expr USING charset_name ')'
6148
	  { $$= new (YYTHD->mem_root) Item_func_conv_charset($3,$5); }
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
6149
	| DEFAULT '(' simple_ident ')'
6150 6151 6152 6153 6154
	  {
	    if ($3->is_splocal())
	    {
	      Item_splocal *il= static_cast<Item_splocal *>($3);

6155
	      my_error(ER_WRONG_COLUMN_NAME, MYF(0), il->my_name()->str);
6156 6157
	      YYABORT;
	    }
6158 6159
	    $$= new (YYTHD->mem_root) Item_default_value(Lex->current_context(),
                                                         $3);
6160
	  }
6161
	| VALUES '(' simple_ident_nospvar ')'
6162 6163 6164 6165 6166 6167
	  { $$= new (YYTHD->mem_root) Item_insert_value(Lex->current_context(),
                                                        $3); }
	| interval_expr interval '+' expr
	  /* we cannot put interval before - */
	  { $$= new (YYTHD->mem_root) Item_date_add_interval($4,$1,$2,0); }
	| interval_expr
hf@deer.(none)'s avatar
hf@deer.(none) committed
6168
	  {
6169 6170 6171
            if ($1->type() != Item::ROW_ITEM)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
6172
              YYABORT;
6173 6174 6175 6176
            }
            $$= new (YYTHD->mem_root) Item_func_interval((Item_row *)$1);
          }
	| UNIQUE_USERS '(' text_literal ',' NUM ',' NUM ',' expr_list ')'
6177
	  {
6178
            $$= new Item_func_unique_users($3,atoi($5.str),atoi($7.str), * $9);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6179
	  }
6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192
        ;

/*
  Function call syntax using official SQL 2003 keywords.
  Because the function name is an official token,
  a dedicated grammar rule is needed in the parser.
  There is no potential for conflicts
*/
function_call_keyword:
	  CHAR_SYM '(' expr_list ')'
	  { $$= new (YYTHD->mem_root) Item_func_char(*$3); }
	| CHAR_SYM '(' expr_list USING charset_name ')'
	  { $$= new (YYTHD->mem_root) Item_func_char(*$3, $5); }
6193
	| CURRENT_USER optional_braces
6194
          {
6195
            $$= new (YYTHD->mem_root) Item_func_current_user(Lex->current_context());
6196 6197
            Lex->safe_to_cache_query= 0;
          }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6198
	| DATE_SYM '(' expr ')'
6199
	  { $$= new (YYTHD->mem_root) Item_date_typecast($3); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6200
	| DAY_SYM '(' expr ')'
6201
	  { $$= new (YYTHD->mem_root) Item_func_dayofmonth($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6202
	| HOUR_SYM '(' expr ')'
6203
	  { $$= new (YYTHD->mem_root) Item_func_hour($3); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6204
	| INSERT '(' expr ',' expr ',' expr ',' expr ')'
6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238
	  { $$= new (YYTHD->mem_root) Item_func_insert($3,$5,$7,$9); }
	| LEFT '(' expr ',' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_left($3,$5); }
	| MINUTE_SYM '(' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_minute($3); }
	| MONTH_SYM '(' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_month($3); }
	| RIGHT '(' expr ',' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_right($3,$5); }
	| SECOND_SYM '(' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_second($3); }
	| TIME_SYM '(' expr ')'
	  { $$= new (YYTHD->mem_root) Item_time_typecast($3); }
	| TIMESTAMP '(' expr ')'
	  { $$= new (YYTHD->mem_root) Item_datetime_typecast($3); }
	| TIMESTAMP '(' expr ',' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_add_time($3, $5, 1, 0); }
	| TRIM '(' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_trim($3); }
	| TRIM '(' LEADING expr FROM expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_ltrim($6,$4); }
	| TRIM '(' TRAILING expr FROM expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_rtrim($6,$4); }
	| TRIM '(' BOTH expr FROM expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_trim($6,$4); }
	| TRIM '(' LEADING FROM expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_ltrim($5); }
	| TRIM '(' TRAILING FROM expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_rtrim($5); }
	| TRIM '(' BOTH FROM expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_trim($5); }
	| TRIM '(' expr FROM expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_trim($5,$3); }
	| USER '(' ')'
6239
	  {
6240 6241
            $$= new (YYTHD->mem_root) Item_func_user();
            Lex->safe_to_cache_query=0;
6242
          }
6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260
	| YEAR_SYM '(' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_year($3); }
        ;

/*
  Function calls using non reserved keywords, with special syntaxic forms.
  Dedicated grammar rules are needed because of the syntax,
  but also have the potential to cause incompatibilities with other
  parts of the language.
  MAINTAINER:
  The only reasons a function should be added here are:
  - for compatibility reasons with another SQL syntax (CURDATE),
  - for typing reasons (GET_FORMAT)
  Any other 'Syntaxic sugar' enhancements should be *STRONGLY*
  discouraged.
*/
function_call_nonkeyword:
	  ADDDATE_SYM '(' expr ',' expr ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6261
	  {
6262 6263 6264 6265 6266 6267
            $$= new (YYTHD->mem_root) Item_date_add_interval($3, $5,
                                                             INTERVAL_DAY, 0);
          }
	| ADDDATE_SYM '(' expr ',' INTERVAL_SYM expr interval ')'
	  { $$= new (YYTHD->mem_root) Item_date_add_interval($3, $6, $7, 0); }
	| CURDATE optional_braces
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6268
	  {
6269 6270 6271 6272 6273 6274 6275 6276 6277
            $$= new (YYTHD->mem_root) Item_func_curdate_local();
            Lex->safe_to_cache_query=0;
          }
	| CURTIME optional_braces
	  {
            $$= new (YYTHD->mem_root) Item_func_curtime_local();
            Lex->safe_to_cache_query=0;
          }
	| CURTIME '(' expr ')'
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6278
	  {
6279
	    $$= new (YYTHD->mem_root) Item_func_curtime_local($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6280
	    Lex->safe_to_cache_query=0;
6281
	  }
6282 6283 6284 6285 6286 6287 6288 6289
	| DATE_ADD_INTERVAL '(' expr ',' interval_expr interval ')'
	  { $$= new (YYTHD->mem_root) Item_date_add_interval($3,$5,$6,0); }
	| DATE_SUB_INTERVAL '(' expr ',' interval_expr interval ')'
	  { $$= new (YYTHD->mem_root) Item_date_add_interval($3,$5,$6,1); }
	| EXTRACT_SYM '(' interval FROM expr ')'
	  { $$=new (YYTHD->mem_root) Item_extract( $3, $5); }
	| GET_FORMAT '(' date_time_type  ',' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_get_format($3, $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6290
	| NOW_SYM optional_braces
6291 6292 6293 6294
	  {
            $$= new (YYTHD->mem_root) Item_func_now_local();
            Lex->safe_to_cache_query=0;
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6295
	| NOW_SYM '(' expr ')'
6296
	  {
6297 6298 6299
            $$= new (YYTHD->mem_root) Item_func_now_local($3);
            Lex->safe_to_cache_query=0;
          }
6300
	| POSITION_SYM '(' bit_expr IN_SYM expr ')'
6301
	  { $$ = new (YYTHD->mem_root) Item_func_locate($5,$3); }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6302
	| SUBDATE_SYM '(' expr ',' expr ')'
6303 6304 6305 6306
	  {
            $$= new (YYTHD->mem_root) Item_date_add_interval($3, $5,
                                                             INTERVAL_DAY, 1);
          }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
6307
	| SUBDATE_SYM '(' expr ',' INTERVAL_SYM expr interval ')'
6308
	  { $$= new (YYTHD->mem_root) Item_date_add_interval($3, $6, $7, 1); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6309
	| SUBSTRING '(' expr ',' expr ',' expr ')'
6310
	  { $$= new (YYTHD->mem_root) Item_func_substr($3,$5,$7); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6311
	| SUBSTRING '(' expr ',' expr ')'
6312
	  { $$= new (YYTHD->mem_root) Item_func_substr($3,$5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6313
	| SUBSTRING '(' expr FROM expr FOR_SYM expr ')'
6314
	  { $$= new (YYTHD->mem_root) Item_func_substr($3,$5,$7); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6315
	| SUBSTRING '(' expr FROM expr ')'
6316
	  { $$= new (YYTHD->mem_root) Item_func_substr($3,$5); }
6317
	| SYSDATE optional_braces
6318 6319
          {
            if (global_system_variables.sysdate_is_now == 0)
6320 6321 6322
              $$= new (YYTHD->mem_root) Item_func_sysdate_local();
            else
              $$= new (YYTHD->mem_root) Item_func_now_local();
6323 6324
            Lex->safe_to_cache_query=0;
          }
6325
	| SYSDATE '(' expr ')'
6326 6327
          {
            if (global_system_variables.sysdate_is_now == 0)
6328 6329 6330
              $$= new (YYTHD->mem_root) Item_func_sysdate_local($3);
            else
              $$= new (YYTHD->mem_root) Item_func_now_local($3);
6331 6332
            Lex->safe_to_cache_query=0;
          }
6333
	| TIMESTAMP_ADD '(' interval_time_st ',' expr ',' expr ')'
6334
	  { $$= new (YYTHD->mem_root) Item_date_add_interval($7,$5,$3,0); }
6335
	| TIMESTAMP_DIFF '(' interval_time_st ',' expr ',' expr ')'
6336 6337
	  { $$= new (YYTHD->mem_root) Item_func_timestamp_diff($5,$7,$3); }
	| UTC_DATE_SYM optional_braces
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6338
	  {
6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352
            $$= new (YYTHD->mem_root) Item_func_curdate_utc();
            Lex->safe_to_cache_query=0;
          }
	| UTC_TIME_SYM optional_braces
	  {
            $$= new (YYTHD->mem_root) Item_func_curtime_utc();
            Lex->safe_to_cache_query=0;
          }
	| UTC_TIMESTAMP_SYM optional_braces
	  {
            $$= new (YYTHD->mem_root) Item_func_now_utc();
            Lex->safe_to_cache_query=0;
          }
        ;
6353

6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389
/*
  Functions calls using a non reserved keywork, and using a regular syntax.
  Because the non reserved keyword is used in another part of the grammar,
  a dedicated rule is needed here.
*/
function_call_conflict:
	  ASCII_SYM '(' expr ')'
          { $$= new (YYTHD->mem_root) Item_func_ascii($3); }
	| CHARSET '(' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_charset($3); }
	| COALESCE '(' expr_list ')'
	  { $$= new (YYTHD->mem_root) Item_func_coalesce(* $3); }
	| COLLATION_SYM '(' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_collation($3); }
	| DATABASE '(' ')'
	  {
	    $$= new (YYTHD->mem_root) Item_func_database();
            Lex->safe_to_cache_query=0;
	  }
	| IF '(' expr ',' expr ',' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_if($3,$5,$7); }
	| MICROSECOND_SYM '(' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_microsecond($3); }
	| MOD_SYM '(' expr ',' expr ')'
	  { $$ = new (YYTHD->mem_root) Item_func_mod( $3, $5); }
	| OLD_PASSWORD '(' expr ')'
	  { $$=  new (YYTHD->mem_root) Item_func_old_password($3); }
	| PASSWORD '(' expr ')'
	  {
            THD *thd= YYTHD;
            Item* i1;
            if (thd->variables.old_passwords)
              i1= new (thd->mem_root) Item_func_old_password($3);
            else
              i1= new (thd->mem_root) Item_func_password($3);
            $$= i1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6390
	  }
6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410
	| QUARTER_SYM '(' expr ')'
	  { $$ = new (YYTHD->mem_root) Item_func_quarter($3); }
	| REPEAT_SYM '(' expr ',' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_repeat($3,$5); }
	| REPLACE '(' expr ',' expr ',' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_replace($3,$5,$7); }
	| TRUNCATE_SYM '(' expr ',' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_round($3,$5,1); }
	| WEEK_SYM '(' expr ')'
	  {
            THD *thd= YYTHD;
            Item *i1= new (thd->mem_root) Item_int((char*) "0",
                                           thd->variables.default_week_format,
                                                   1);

            $$= new (thd->mem_root) Item_func_week($3, i1);
          }
	| WEEK_SYM '(' expr ',' expr ')'
	  { $$= new (YYTHD->mem_root) Item_func_week($3,$5); }
        | geometry_function
6411
          {
6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486
#ifdef HAVE_SPATIAL
            $$= $1;
#else
            my_error(ER_FEATURE_DISABLED, MYF(0),
                     sym_group_geom.name, sym_group_geom.needed_define);
            YYABORT;
#endif
          }
        ;

geometry_function:
	  CONTAINS_SYM '(' expr ',' expr ')'
	  {
            $$= GEOM_NEW(YYTHD,
                         Item_func_spatial_rel($3, $5,
                                               Item_func::SP_CONTAINS_FUNC));
          }
	| GEOMETRYCOLLECTION '(' expr_list ')'
	  {
            $$= GEOM_NEW(YYTHD,
                         Item_func_spatial_collection(* $3,
                           Geometry::wkb_geometrycollection,
                           Geometry::wkb_point));
          }
	| LINESTRING '(' expr_list ')'
	  {
            $$= GEOM_NEW(YYTHD,
                         Item_func_spatial_collection(* $3,
                           Geometry::wkb_linestring,
                           Geometry::wkb_point));
          }
 	| MULTILINESTRING '(' expr_list ')'
	  {
            $$= GEOM_NEW(YYTHD,
                         Item_func_spatial_collection(* $3,
                           Geometry::wkb_multilinestring,
                           Geometry::wkb_linestring));
          }
	| MULTIPOINT '(' expr_list ')'
	  {
            $$= GEOM_NEW(YYTHD,
                         Item_func_spatial_collection(* $3,
                           Geometry::wkb_multipoint,
                           Geometry::wkb_point));
          }
 	| MULTIPOLYGON '(' expr_list ')'
	  {
            $$= GEOM_NEW(YYTHD,
                         Item_func_spatial_collection(* $3,
                           Geometry::wkb_multipolygon,
                           Geometry::wkb_polygon));
          }
	| POINT_SYM '(' expr ',' expr ')'
	  { $$= GEOM_NEW(YYTHD, Item_func_point($3,$5)); }
	| POLYGON '(' expr_list ')'
	  {
            $$= GEOM_NEW(YYTHD,
                         Item_func_spatial_collection(* $3,
	                   Geometry::wkb_polygon,
                           Geometry::wkb_linestring));
          }
        ;

/*
  Regular function calls.
  The function name is *not* a token, and therefore is guaranteed to not
  introduce side effects to the language in general.
  MAINTAINER:
  All the new functions implemented for new features should fit into
  this category. The place to implement the function itself is
  in sql/item_create.cc
*/
function_call_generic:
        IDENT_sys '('
        {
6487
#ifdef HAVE_DLOPEN
6488
          udf_func *udf= 0;
6489
          LEX *lex= Lex;
6490 6491 6492 6493 6494
          if (using_udf_functions &&
              (udf= find_udf($1.str, $1.length)) &&
              udf->type == UDFTYPE_AGGREGATE)
          {
            if (lex->current_select->inc_in_sum_expr())
6495
            {
6496 6497
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
6498
            }
6499 6500
          }
          /* Temporary placing the result of find_udf in $3 */
6501
          lex->current_select->udf_list.push_front(udf);
6502
#endif
6503
        }
6504
        udf_expr_list ')'
6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523
        {
          THD *thd= YYTHD;
          LEX *lex= Lex;
          Create_func *builder;
          Item *item= NULL;

          /*
            Implementation note:
            names are resolved with the following order:
            - MySQL native functions,
            - User Defined Functions,
            - Stored Functions (assuming the current <use> database)

            This will be revised with WL#2128 (SQL PATH)
          */
          builder= find_native_function_builder(thd, $1);
          if (builder)
          {
            item= builder->create(thd, $1, $4);
6524
          }
6525
          else
6526 6527
          {
#ifdef HAVE_DLOPEN
6528
            /* Retrieving the result of find_udf */
6529
            udf_func *udf;
6530
            LEX *lex= Lex;
6531

6532
            if (NULL != (udf= lex->current_select->udf_list.pop()))
6533
            {
6534
              if (udf->type == UDFTYPE_AGGREGATE)
6535
              {
6536
                Select->in_sum_expr--;
6537
              }
6538 6539

              item= Create_udf_func::s_singleton.create(thd, udf, $4);
6540 6541
            }
            else
6542
#endif
6543
            {
6544 6545 6546 6547
              builder= find_qualified_function_builder(thd);
              DBUG_ASSERT(builder);
              item= builder->create(thd, $1, $4);
            }
6548
          }
6549 6550 6551 6552

          if (! ($$= item))
          {
            YYABORT;
6553
          }
6554
        }
6555
	| ident '.' ident '(' opt_expr_list ')'
6556 6557 6558 6559
	{
          THD *thd= YYTHD;
          Create_qfunc *builder;
          Item *item= NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6560

6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584
          /*
            The following in practice calls:
            <code>Create_sp_func::create()</code>
            and builds a stored function.

            However, it's important to maintain the interface between the
            parser and the implementation in item_create.cc clean,
            since this will change with WL#2128 (SQL PATH):
            - INFORMATION_SCHEMA.version() is the SQL 99 syntax for the native
            funtion version(),
            - MySQL.version() is the SQL 2003 syntax for the native function
            version() (a vendor can specify any schema).
          */

          builder= find_qualified_function_builder(thd);
          DBUG_ASSERT(builder);
          item= builder->create(thd, $1, $3, $5);

          if (! ($$= item))
          {
            YYABORT;
          }
	}
        ;
hf@deer.(none)'s avatar
hf@deer.(none) committed
6585

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

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

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6603
udf_expr_list:
6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627
	/* 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
	{
6628 6629 6630 6631 6632 6633 6634
          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.
          */
6635
	  if ($4.str)
6636
          {
6637 6638 6639 6640 6641 6642 6643 6644 6645 6646
            if (!udf)
            {
              /*
                Disallow using AS to specify explicit names for the arguments
                of stored routine calls
              */
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }

6647
            $2->is_autogenerated_name= FALSE;
6648
	    $2->set_name($4.str, $4.length, system_charset_info);
6649
          }
6650
	  else if (udf)
6651
	    $2->set_name($1, (uint) ($3 - $1), YYTHD->charset());
6652 6653 6654
	  $$= $2;
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6655 6656 6657 6658

sum_expr:
	AVG_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_avg($3); }
6659 6660
	| AVG_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_avg_distinct($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6661 6662 6663 6664
	| BIT_AND  '(' in_sum_expr ')'
	  { $$=new Item_sum_and($3); }
	| BIT_OR  '(' in_sum_expr ')'
	  { $$=new Item_sum_or($3); }
6665 6666
	| BIT_XOR  '(' in_sum_expr ')'
	  { $$=new Item_sum_xor($3); }
6667
	| COUNT_SYM '(' opt_all '*' ')'
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6668 6669 6670
	  { $$=new Item_sum_count(new Item_int((int32) 0L,1)); }
	| COUNT_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_count($3); }
6671
	| COUNT_SYM '(' DISTINCT
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6672
	  { Select->in_sum_expr++; }
6673
	   expr_list
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6674
	  { Select->in_sum_expr--; }
6675 6676
	  ')'
	  { $$=new Item_sum_count_distinct(* $5); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6677 6678 6679 6680
	| 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); }
6681 6682 6683 6684 6685 6686 6687
/*
   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
6688 6689
	| MAX_SYM '(' in_sum_expr ')'
	  { $$=new Item_sum_max($3); }
6690 6691
	| MAX_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_max($4); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6692
	| STD_SYM '(' in_sum_expr ')'
6693
	  { $$=new Item_sum_std($3, 0); }
6694
	| VARIANCE_SYM '(' in_sum_expr ')'
6695 6696 6697 6698 6699
	  { $$=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
6700
	| SUM_SYM '(' in_sum_expr ')'
6701
	  { $$=new Item_sum_sum($3); }
6702 6703
	| SUM_SYM '(' DISTINCT in_sum_expr ')'
	  { $$=new Item_sum_sum_distinct($4); }
monty@mysql.com's avatar
monty@mysql.com committed
6704 6705 6706 6707 6708
	| 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
6709
	  {
6710 6711
            SELECT_LEX *sel= Select;
	    sel->in_sum_expr--;
6712
	    $$=new Item_func_group_concat(Lex->current_context(), $3, $5,
6713
                                          sel->gorder_list, $7);
monty@mysql.com's avatar
monty@mysql.com committed
6714
	    $5->empty();
6715 6716
	  };

6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756
variable:
          '@'
          {
            if (! Lex->parsing_options.allows_variable)
            {
              my_error(ER_VIEW_SELECT_VARIABLE, MYF(0));
              YYABORT;
            }
          }
          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))
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }
            if (!($$= get_system_var(YYTHD, $2, $3, $4)))
              YYABORT;
          }
        ;

6757 6758 6759 6760 6761
opt_distinct:
    /* empty */ { $$ = 0; }
    |DISTINCT   { $$ = 1; };

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

6765 6766

opt_gorder_clause:
6767 6768
	  /* empty */
	  {
6769
            Select->gorder_list = NULL;
6770 6771 6772
	  }
	| order_clause
          {
6773 6774 6775
            SELECT_LEX *select= Select;
            select->gorder_list=
	      (SQL_LIST*) sql_memdup((char*) &select->order_list,
6776
				     sizeof(st_sql_list));
6777
	    select->order_list.empty();
6778
	  };
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6779

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6780 6781

in_sum_expr:
6782
	opt_all
6783 6784 6785 6786
	{
	  LEX *lex= Lex;
	  if (lex->current_select->inc_in_sum_expr())
	  {
6787
	    yyerror(ER(ER_SYNTAX_ERROR));
6788
	    YYABORT;
6789 6790
	  }
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6791 6792
	expr
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6793
	  Select->in_sum_expr--;
6794
	  $$= $3;
6795
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6796

6797
cast_type:
6798 6799
        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; }
6800
	| NCHAR_SYM opt_len	{ $$=ITEM_CAST_CHAR; Lex->charset= national_charset_info; Lex->dec=0; }
6801 6802 6803 6804 6805 6806 6807 6808
        | 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; }
6809 6810
	;

6811 6812 6813 6814
opt_expr_list:
	/* empty */ { $$= NULL; }
	| expr_list { $$= $1;}
	;
6815

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6816
expr_list:
6817
	{ Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6818
	expr_list2
6819
	{ $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6820 6821

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

6825 6826
ident_list_arg:
          ident_list          { $$= $1; }
6827
        | '(' ident_list ')'  { $$= $2; };
6828

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6829
ident_list:
6830
        { Select->expr_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6831
        ident_list2
6832
        { $$= Select->expr_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6833 6834

ident_list2:
6835
        simple_ident { Select->expr_list.head()->push_back($1); }
6836
        | ident_list2 ',' simple_ident { Select->expr_list.head()->push_back($3); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6837 6838 6839

opt_expr:
	/* empty */      { $$= NULL; }
6840
	| expr           { $$= $1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6841 6842 6843

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

when_list:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
6847
        { Select->when_list.push_front(new List<Item>); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6848
	when_list2
6849
	{ $$= Select->when_list.pop(); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6850 6851

when_list2:
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6852
	expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6853
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6854
	    SELECT_LEX *sel=Select;
6855 6856
	    sel->when_list.head()->push_back($1);
	    sel->when_list.head()->push_back($3);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6857
	}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
6858
	| when_list2 WHEN_SYM expr THEN_SYM expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6859
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6860
	    SELECT_LEX *sel=Select;
6861 6862
	    sel->when_list.head()->push_back($3);
	    sel->when_list.head()->push_back($5);
6863
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6864

6865
/* Warning - may return NULL in case of incomplete SELECT */
6866 6867
table_ref:
        table_factor            { $$=$1; }
6868
        | join_table
6869
          {
6870 6871 6872
	    LEX *lex= Lex;
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
6873
          }
6874 6875
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6876
join_table_list:
serg@serg.mylan's avatar
serg@serg.mylan committed
6877
	derived_table_list		{ YYERROR_UNLESS($$=$1); }
6878
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
6879

6880 6881
/* Warning - may return NULL in case of incomplete SELECT */
derived_table_list:
6882
        table_ref { $$=$1; }
6883 6884
        | derived_table_list ',' table_ref
          {
serg@serg.mylan's avatar
serg@serg.mylan committed
6885
            YYERROR_UNLESS($1 && ($$=$3));
6886
          }
6887 6888
        ;

6889 6890 6891 6892 6893 6894 6895
/*
  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.
*/
6896
join_table:
6897
/* INNER JOIN variants */
timour@mysql.com's avatar
timour@mysql.com committed
6898
        /*
6899 6900 6901
          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
6902 6903
        */
        table_ref %prec TABLE_REF_PRIORITY normal_join table_ref
6904
          { YYERROR_UNLESS($1 && ($$=$3)); }
6905
	| table_ref STRAIGHT_JOIN table_factor
serg@serg.mylan's avatar
serg@serg.mylan committed
6906
	  { YYERROR_UNLESS($1 && ($$=$3)); $3->straight=1; }
6907 6908 6909
	| table_ref normal_join table_ref
          ON
          {
6910
            YYERROR_UNLESS($1 && $3);
6911
            /* Change the current name resolution context to a local context. */
6912
            if (push_new_name_resolution_context(YYTHD, $1, $3))
6913
              YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6914
            Select->parsing_place= IN_ON;
6915 6916 6917 6918 6919
          }
          expr
	  {
            add_join_on($3,$6);
            Lex->pop_context();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6920
            Select->parsing_place= NO_MATTER;
6921 6922 6923 6924
          }
        | table_ref STRAIGHT_JOIN table_factor
          ON
          {
6925
            YYERROR_UNLESS($1 && $3);
6926
            /* Change the current name resolution context to a local context. */
6927
            if (push_new_name_resolution_context(YYTHD, $1, $3))
6928
              YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6929
            Select->parsing_place= IN_ON;
6930 6931 6932 6933 6934 6935
          }
          expr
          {
            $3->straight=1;
            add_join_on($3,$6);
            Lex->pop_context();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6936
            Select->parsing_place= NO_MATTER;
6937
          }
6938
	| table_ref normal_join table_ref
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6939
	  USING
6940
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6941
	    SELECT_LEX *sel= Select;
serg@serg.mylan's avatar
serg@serg.mylan committed
6942
            YYERROR_UNLESS($1 && $3);
6943
	  }
6944
	  '(' using_list ')'
6945
          { add_join_natural($1,$3,$7); $$=$3; }
6946 6947 6948 6949 6950 6951 6952
	| table_ref NATURAL JOIN_SYM table_factor
	  {
            YYERROR_UNLESS($1 && ($$=$4));
            add_join_natural($1,$4,NULL);
          }

/* LEFT JOIN variants */
6953 6954 6955
	| table_ref LEFT opt_outer JOIN_SYM table_ref
          ON
          {
6956
            YYERROR_UNLESS($1 && $5);
6957
            /* Change the current name resolution context to a local context. */
6958
            if (push_new_name_resolution_context(YYTHD, $1, $5))
6959
              YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6960
            Select->parsing_place= IN_ON;
6961 6962 6963 6964 6965 6966 6967
          }
          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
6968
            Select->parsing_place= NO_MATTER;
6969
          }
6970
	| table_ref LEFT opt_outer JOIN_SYM table_factor
6971
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
6972
	    SELECT_LEX *sel= Select;
serg@serg.mylan's avatar
serg@serg.mylan committed
6973
            YYERROR_UNLESS($1 && $5);
6974
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6975
	  USING '(' using_list ')'
6976
          { add_join_natural($1,$5,$9); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; }
6977
	| table_ref NATURAL LEFT opt_outer JOIN_SYM table_factor
6978
	  {
serg@serg.mylan's avatar
serg@serg.mylan committed
6979
            YYERROR_UNLESS($1 && $6);
6980
 	    add_join_natural($1,$6,NULL);
6981
	    $6->outer_join|=JOIN_TYPE_LEFT;
6982 6983
	    $$=$6;
	  }
6984 6985

/* RIGHT JOIN variants */
6986 6987 6988
	| table_ref RIGHT opt_outer JOIN_SYM table_ref
          ON
          {
6989
            YYERROR_UNLESS($1 && $5);
6990
            /* Change the current name resolution context to a local context. */
6991
            if (push_new_name_resolution_context(YYTHD, $1, $5))
6992
              YYABORT;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6993
            Select->parsing_place= IN_ON;
6994 6995
          }
          expr
serg@serg.mylan's avatar
serg@serg.mylan committed
6996
          {
6997 6998 6999
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
7000 7001
            add_join_on($$, $8);
            Lex->pop_context();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
7002
            Select->parsing_place= NO_MATTER;
7003 7004
          }
	| table_ref RIGHT opt_outer JOIN_SYM table_factor
7005
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7006
	    SELECT_LEX *sel= Select;
serg@serg.mylan's avatar
serg@serg.mylan committed
7007
            YYERROR_UNLESS($1 && $5);
7008
	  }
7009
	  USING '(' using_list ')'
serg@serg.mylan's avatar
serg@serg.mylan committed
7010
          {
7011 7012 7013
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
7014
            add_join_natural($$,$5,$9);
7015 7016
          }
	| table_ref NATURAL RIGHT opt_outer JOIN_SYM table_factor
7017
	  {
serg@serg.mylan's avatar
serg@serg.mylan committed
7018
            YYERROR_UNLESS($1 && $6);
7019
	    add_join_natural($6,$1,NULL);
7020 7021 7022
	    LEX *lex= Lex;
            if (!($$= lex->current_select->convert_right_join()))
              YYABORT;
7023
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7024 7025

normal_join:
7026 7027 7028 7029
	JOIN_SYM		{}
	| INNER_SYM JOIN_SYM	{}
	| CROSS JOIN_SYM	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7030

7031
/* Warning - may return NULL in case of incomplete SELECT */
7032
table_factor:
7033
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7034
	  SELECT_LEX *sel= Select;
7035
	  sel->use_index_ptr=sel->ignore_index_ptr=0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7036
	  sel->table_join_options= 0;
7037
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7038
        table_ident opt_table_alias opt_key_definition
7039
	{
7040
	  LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7041
	  SELECT_LEX *sel= lex->current_select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7042
	  if (!($$= sel->add_table_to_list(lex->thd, $2, $3,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7043
					   sel->get_table_join_options(),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7044
					   lex->lock_option,
7045 7046
					   sel->get_use_index(),
					   sel->get_ignore_index())))
7047
	    YYABORT;
7048
          sel->add_joined_table($$);
7049
	}
7050 7051 7052 7053
	| '{' ident table_ref LEFT OUTER JOIN_SYM table_ref
          ON
          {
            /* Change the current name resolution context to a local context. */
7054
            if (push_new_name_resolution_context(YYTHD, $3, $7))
7055
              YYABORT;
7056

7057 7058 7059
          }
          expr '}'
	  {
7060
	    LEX *lex= Lex;
7061 7062 7063 7064 7065
            YYERROR_UNLESS($3 && $7);
            add_join_on($7,$10);
            Lex->pop_context();
            $7->outer_join|=JOIN_TYPE_LEFT;
            $$=$7;
7066 7067
            if (!($$= lex->current_select->nest_last_join(lex->thd)))
              YYABORT;
7068
          }
7069
	| select_derived_init get_select_lex select_derived2
7070
          {
7071
            LEX *lex= Lex;
7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085
            SELECT_LEX *sel= lex->current_select;
            if ($1)
            {
	      if (sel->set_braces(1))
	      {
	        yyerror(ER(ER_SYNTAX_ERROR));
	        YYABORT;
	      }
              /* select in braces, can't contain global parameters */
	      if (sel->master_unit()->fake_select_lex)
                sel->master_unit()->global_parameters=
                   sel->master_unit()->fake_select_lex;
            }
            if ($2->init_nested_join(lex->thd))
serg@serg.mylan's avatar
serg@serg.mylan committed
7086
              YYABORT;
7087
            $$= 0;
serg@serg.mylan's avatar
serg@serg.mylan committed
7088
            /* incomplete derived tables return NULL, we must be
7089
               nested in select_derived rule to be here. */
7090
          }
7091 7092 7093 7094 7095
	| '(' 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
7096
          if (!($3 || $6) && $2->embedding &&
7097
              !$2->embedding->nested_join->join_list.elements)
7098
          {
7099 7100 7101 7102 7103
            /* 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;
7104
          }
7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118
          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
7119

7120
	      YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
7121
            sel->add_joined_table($$);
7122
            lex->pop_context();
7123 7124 7125 7126 7127 7128
          }
	  else
          if ($4 || $6)
	  {
            /* simple nested joins cannot have aliases or unions */
            yyerror(ER(ER_SYNTAX_ERROR));
7129
	    YYABORT;
7130 7131 7132 7133 7134
	  }
          else
            $$= $3;
	}
        ;
7135

7136
/* handle contents of parentheses in join expression */
7137
select_derived:
7138
	  get_select_lex
7139
	  {
7140 7141 7142 7143 7144 7145 7146 7147 7148
            LEX *lex= Lex;
            if ($1->init_nested_join(lex->thd))
              YYABORT;
          }
          derived_table_list
          {
            LEX *lex= Lex;
            /* for normal joins, $3 != NULL and end_nested_join() != NULL,
               for derived tables, both must equal NULL */
serg@serg.mylan's avatar
serg@serg.mylan committed
7149

7150 7151 7152 7153
            if (!($$= $1->end_nested_join(lex->thd)) && $3)
              YYABORT;
            if (!$3 && $$)
            {
7154 7155
	      yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
7156 7157
            }
          }
monty@mysql.com's avatar
monty@mysql.com committed
7158
 	;
7159

monty@mysql.com's avatar
monty@mysql.com committed
7160
select_derived2:
7161
        {
7162
	  LEX *lex= Lex;
7163
	  lex->derived_tables|= DERIVED_SUBQUERY;
7164
          if (!lex->expr_allows_subselect)
7165 7166
	  {
	    yyerror(ER(ER_SYNTAX_ERROR));
7167 7168
	    YYABORT;
	  }
7169
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE ||
7170
              mysql_new_select(lex, 1))
7171 7172
	    YYABORT;
	  mysql_init_select(lex);
7173
	  lex->current_select->linkage= DERIVED_TABLE_TYPE;
7174
	  lex->current_select->parsing_place= SELECT_LIST;
7175 7176 7177
	}
        select_options select_item_list
	{
7178
	  Select->parsing_place= NO_MATTER;
7179
	}
7180
	opt_select_from
7181
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7182

7183 7184 7185 7186 7187 7188 7189 7190
get_select_lex:
	/* Empty */ { $$= Select; }
        ;

select_derived_init:
          SELECT_SYM
          {
            LEX *lex= Lex;
7191 7192 7193 7194 7195 7196 7197

            if (! lex->parsing_options.allows_derived)
            {
              my_error(ER_VIEW_SELECT_DERIVED, MYF(0));
              YYABORT;
            }

serg@serg.mylan's avatar
serg@serg.mylan committed
7198
            SELECT_LEX *sel= lex->current_select;
7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212
            TABLE_LIST *embedding;
            if (!sel->embedding || sel->end_nested_join(lex->thd))
	    {
              /* we are not in parentheses */
              yyerror(ER(ER_SYNTAX_ERROR));
	      YYABORT;
	    }
            embedding= Select->embedding;
            $$= embedding &&
                !embedding->nested_join->join_list.elements;
            /* return true if we are deeply nested */
          }
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7213 7214
opt_outer:
	/* empty */	{}
7215
	| OUTER		{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7216 7217 7218 7219

opt_key_definition:
	/* empty */	{}
	| USE_SYM    key_usage_list
7220
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7221
	    SELECT_LEX *sel= Select;
7222 7223 7224
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7225 7226
	| FORCE_SYM key_usage_list
          {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7227
	    SELECT_LEX *sel= Select;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7228 7229 7230 7231
	    sel->use_index= *$2;
	    sel->use_index_ptr= &sel->use_index;
	    sel->table_join_options|= TL_OPTION_FORCE_INDEX;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7232
	| IGNORE_SYM key_usage_list
7233
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7234
	    SELECT_LEX *sel= Select;
7235 7236
	    sel->ignore_index= *$2;
	    sel->ignore_index_ptr= &sel->ignore_index;
7237
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7238 7239

key_usage_list:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7240
	key_or_index { Select->interval_list.empty(); }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7241
        '(' key_list_or_empty ')'
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7242
        { $$= &Select->interval_list; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7243 7244 7245 7246 7247 7248
	;

key_list_or_empty:
	/* empty */ 		{}
	| key_usage_list2	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7249 7250 7251

key_usage_list2:
	key_usage_list2 ',' ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7252
        { Select->
7253
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $3.str, $3.length,
7254
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7255
	| ident
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7256
        { Select->
7257
	    interval_list.push_back(new (YYTHD->mem_root) String((const char*) $1.str, $1.length,
7258
				    system_charset_info)); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7259
	| PRIMARY_SYM
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7260
        { Select->
7261
	    interval_list.push_back(new (YYTHD->mem_root) String("PRIMARY", 7,
7262
				    system_charset_info)); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7263 7264 7265

using_list:
	ident
7266
	  {
7267
            if (!($$= new List<String>))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7268
	      YYABORT;
7269 7270 7271
            $$->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
7272 7273 7274
	  }
	| using_list ',' ident
	  {
7275 7276 7277 7278
            $1->push_back(new (YYTHD->mem_root)
                              String((const char *) $3.str, $3.length,
                                      system_charset_info));
            $$= $1;
7279
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7280 7281

interval:
7282 7283
	interval_time_st	{}
	| DAY_HOUR_SYM		{ $$=INTERVAL_DAY_HOUR; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
7284
	| DAY_MICROSECOND_SYM	{ $$=INTERVAL_DAY_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7285 7286
	| 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
7287
	| HOUR_MICROSECOND_SYM	{ $$=INTERVAL_HOUR_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7288 7289
	| 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
7290 7291
	| MICROSECOND_SYM	{ $$=INTERVAL_MICROSECOND; }
	| MINUTE_MICROSECOND_SYM	{ $$=INTERVAL_MINUTE_MICROSECOND; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7292
	| MINUTE_SECOND_SYM	{ $$=INTERVAL_MINUTE_SECOND; }
7293 7294 7295 7296 7297 7298 7299 7300
	| 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
7301 7302
	| MINUTE_SYM		{ $$=INTERVAL_MINUTE; }
	| MONTH_SYM		{ $$=INTERVAL_MONTH; }
7303
	| QUARTER_SYM		{ $$=INTERVAL_QUARTER; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7304
	| SECOND_SYM		{ $$=INTERVAL_SECOND; }
7305 7306
	| YEAR_SYM		{ $$=INTERVAL_YEAR; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7307

7308
date_time_type:
7309 7310 7311 7312 7313
          DATE_SYM              {$$=MYSQL_TIMESTAMP_DATE;}
        | TIME_SYM              {$$=MYSQL_TIMESTAMP_TIME;}
        | DATETIME              {$$=MYSQL_TIMESTAMP_DATETIME;}
        | TIMESTAMP             {$$=MYSQL_TIMESTAMP_DATETIME;}
        ;
7314

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7315 7316 7317
table_alias:
	/* empty */
	| AS
7318
	| EQ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7319 7320 7321 7322

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

7325 7326 7327 7328
opt_all:
	/* empty */
	| ALL
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7329 7330

where_clause:
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7331
	/* empty */  { Select->where= 0; }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7332 7333 7334 7335 7336
	| WHERE
          {
            Select->parsing_place= IN_WHERE;
          }
          expr
7337
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7338 7339 7340 7341 7342
            SELECT_LEX *select= Select;
	    select->where= $3;
            select->parsing_place= NO_MATTER;
	    if ($3)
	      $3->top_level_item();
7343
	  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7344
 	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7345 7346 7347

having_clause:
	/* empty */
7348 7349
	| HAVING
	  {
7350
	    Select->parsing_place= IN_HAVING;
7351 7352
          }
	  expr
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7353
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7354
	    SELECT_LEX *sel= Select;
7355
	    sel->having= $3;
7356
	    sel->parsing_place= NO_MATTER;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7357 7358 7359
	    if ($3)
	      $3->top_level_item();
	  }
7360
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7361 7362

opt_escape:
7363 7364 7365 7366 7367
	ESCAPE_SYM simple_expr 
          {
            Lex->escape_used= TRUE;
            $$= $2;
          }
7368
	| /* empty */
7369
          {
7370
            Lex->escape_used= FALSE;
7371 7372 7373
            $$= ((YYTHD->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
		 new Item_string("", 0, &my_charset_latin1) :
                 new Item_string("\\", 1, &my_charset_latin1));
7374 7375
          }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7376 7377 7378


/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7379
   group by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7380 7381 7382 7383
*/

group_clause:
	/* empty */
7384
	| GROUP BY group_list olap_opt;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7385 7386

group_list:
7387
	group_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7388
	  { if (add_group_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
7389
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7390
	  { if (add_group_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7391

7392 7393
olap_opt:
	/* empty */ {}
7394
	| WITH CUBE_SYM
7395
          {
7396
	    LEX *lex=Lex;
7397 7398
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
7399
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
7400 7401 7402
		       "global union parameters");
	      YYABORT;
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7403
	    lex->current_select->olap= CUBE_TYPE;
7404
	    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
7405
	    YYABORT;	/* To be deleted in 5.1 */
7406
	  }
7407
	| WITH ROLLUP_SYM
7408
          {
7409 7410 7411
	    LEX *lex= Lex;
	    if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
	    {
7412
	      my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
7413 7414 7415
		       "global union parameters");
	      YYABORT;
	    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7416
	    lex->current_select->olap= ROLLUP_TYPE;
7417
	  }
7418
	;
7419

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7420
/*
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7421
   Order by statement in select
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7422 7423
*/

7424
opt_order_clause:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7425
	/* empty */
7426
	| order_clause;
7427 7428

order_clause:
7429 7430
	ORDER_SYM BY
        {
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7431
	  LEX *lex=Lex;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
7432 7433 7434 7435
          SELECT_LEX *sel= lex->current_select;
          SELECT_LEX_UNIT *unit= sel-> master_unit();
	  if (sel->linkage != GLOBAL_OPTIONS_TYPE &&
	      sel->olap != UNSPECIFIED_OLAP_TYPE)
7436
	  {
7437 7438
	    my_error(ER_WRONG_USAGE, MYF(0),
                     "CUBE/ROLLUP", "ORDER BY");
7439 7440
	    YYABORT;
	  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453
          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
7454
                 first_sl->select_limit) &&            
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
7455 7456 7457
                unit->add_fake_select_lex(lex->thd))
              YYABORT;
          }
7458
	} order_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7459 7460 7461

order_list:
	order_list ',' order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7462
	  { if (add_order_to_list(YYTHD, $3,(bool) $4)) YYABORT; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7463
	| order_ident order_dir
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7464
	  { if (add_order_to_list(YYTHD, $1,(bool) $2)) YYABORT; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7465 7466 7467

order_dir:
	/* empty */ { $$ =  1; }
7468
	| ASC  { $$ =1; }
7469
	| DESC { $$ =0; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7470 7471


7472 7473 7474
opt_limit_clause_init:
	/* empty */
	{
7475 7476
	  LEX *lex= Lex;
	  SELECT_LEX *sel= lex->current_select;
7477 7478
          sel->offset_limit= 0;
          sel->select_limit= 0;
7479
	}
7480 7481 7482
	| limit_clause {}
	;

7483 7484 7485 7486 7487
opt_limit_clause:
	/* empty */	{}
	| limit_clause	{}
	;

7488
limit_clause:
7489
	LIMIT limit_options {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7490
	;
7491 7492

limit_options:
7493
	limit_option
7494
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7495
            SELECT_LEX *sel= Select;
7496
            sel->select_limit= $1;
7497
            sel->offset_limit= 0;
7498
	    sel->explicit_limit= 1;
7499
	  }
7500
	| limit_option ',' limit_option
7501
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7502
	    SELECT_LEX *sel= Select;
7503 7504
	    sel->select_limit= $3;
	    sel->offset_limit= $1;
7505
	    sel->explicit_limit= 1;
7506
	  }
7507
	| limit_option OFFSET_SYM limit_option
7508
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
7509
	    SELECT_LEX *sel= Select;
7510 7511
	    sel->select_limit= $1;
	    sel->offset_limit= $3;
7512
	    sel->explicit_limit= 1;
7513 7514
	  }
	;
7515 7516 7517 7518 7519
limit_option:
        param_marker
        | ULONGLONG_NUM { $$= new Item_uint($1.str, $1.length); }
        | LONG_NUM     { $$= new Item_uint($1.str, $1.length); }
        | NUM           { $$= new Item_uint($1.str, $1.length); }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
7520
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7521 7522 7523 7524

delete_limit_clause:
	/* empty */
	{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7525
	  LEX *lex=Lex;
7526
	  lex->current_select->select_limit= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7527
	}
7528
	| LIMIT limit_option
7529 7530
	{
	  SELECT_LEX *sel= Select;
7531
	  sel->select_limit= $2;
7532 7533
	  sel->explicit_limit= 1;
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7534

serg@serg.mylan's avatar
serg@serg.mylan committed
7535 7536
ulong_num:
          NUM           { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
7537
	| HEX_NUM       { $$= (ulong) strtol($1.str, (char**) 0, 16); }
monty@mysql.com's avatar
monty@mysql.com committed
7538 7539
	| 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
7540
        | DECIMAL_NUM   { int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
monty@mysql.com's avatar
monty@mysql.com committed
7541
	| FLOAT_NUM	{ int error; $$= (ulong) my_strtoll10($1.str, (char**) 0, &error); }
7542 7543 7544 7545 7546 7547 7548 7549 7550
        ;

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

7552
ulonglong_num:
monty@mysql.com's avatar
monty@mysql.com committed
7553 7554 7555
	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); }
7556
        | DECIMAL_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
monty@mysql.com's avatar
monty@mysql.com committed
7557 7558
	| FLOAT_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7559

7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576
real_ulonglong_num:
	NUM	    { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| ULONGLONG_NUM { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
	| LONG_NUM  { int error; $$= (ulonglong) my_strtoll10($1.str, (char**) 0, &error); }
        | dec_num_error { YYABORT; }
        ;

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

dec_num:
        DECIMAL_NUM
	| FLOAT_NUM
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7577 7578 7579 7580 7581
procedure_clause:
	/* empty */
	| PROCEDURE ident			/* Procedure name */
	  {
	    LEX *lex=Lex;
7582 7583 7584 7585 7586 7587 7588

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

7589 7590
	    if (&lex->select_lex != lex->current_select)
	    {
7591
	      my_error(ER_WRONG_USAGE, MYF(0), "PROCEDURE", "subquery");
7592 7593
	      YYABORT;
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7594 7595 7596
	    lex->proc_list.elements=0;
	    lex->proc_list.first=0;
	    lex->proc_list.next= (byte**) &lex->proc_list.first;
7597 7598 7599 7600
	    if (add_proc_to_list(lex->thd, new Item_field(&lex->
                                                          current_select->
                                                          context,
                                                          NULL,NULL,$2.str)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7601
	      YYABORT;
7602
	    Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7603
	  }
7604
	  '(' procedure_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7605 7606 7607 7608


procedure_list:
	/* empty */ {}
7609
	| procedure_list2 {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7610 7611 7612

procedure_list2:
	procedure_list2 ',' procedure_item
7613
	| procedure_item;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7614 7615 7616 7617

procedure_item:
	  remember_name expr
	  {
7618 7619
	    LEX *lex= Lex;
	    if (add_proc_to_list(lex->thd, $2))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7620 7621
	      YYABORT;
	    if (!$2->name)
7622 7623
	      $2->set_name($1,(uint) ((char*) lex->tok_end - $1),
                           YYTHD->charset());
7624 7625 7626 7627 7628 7629
	  }
          ;


select_var_list_init:
	   {
7630 7631
             LEX *lex=Lex;
	     if (!lex->describe && (!(lex->result= new select_dumpvar())))
7632 7633
	        YYABORT;
	   }
7634
	   select_var_list
7635
	   {}
7636
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7637

Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7638
select_var_list:
7639 7640 7641 7642
	   select_var_list ',' select_var_ident
	   | select_var_ident {}
           ;

7643 7644 7645 7646 7647 7648 7649
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
7650 7651 7652 7653 7654
               /*
                 The parser won't create select_result instance only
	         if it's an EXPLAIN.
               */
               DBUG_ASSERT(lex->describe);
7655 7656
	   }
           | ident_or_text
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7657
           {
7658
             LEX *lex=Lex;
7659
	     sp_variable_t *t;
7660

7661
	     if (!lex->spcont || !(t=lex->spcont->find_variable(&$1)))
7662
	     {
7663
	       my_error(ER_SP_UNDECLARED_VAR, MYF(0), $1.str);
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7664
	       YYABORT;
7665
	     }
7666 7667
	     if (lex->result)
             {
7668 7669 7670
               my_var *var;
	       ((select_dumpvar *)lex->result)->
                 var_list.push_back(var= new my_var($1,1,t->offset,t->type));
7671
#ifndef DBUG_OFF
7672
	       if (var)
7673
		 var->sp= lex->sphead;
7674
#endif
7675 7676 7677 7678 7679 7680 7681 7682
             }
	     else
	     {
               /*
                 The parser won't create select_result instance only
	         if it's an EXPLAIN.
               */
               DBUG_ASSERT(lex->describe);
7683
	     }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7684
	   }
7685
           ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7686

7687
into:
7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700
        INTO
	{
          if (! Lex->parsing_options.allows_select_into)
          {
            my_error(ER_VIEW_SELECT_CLAUSE, MYF(0), "INTO");
            YYABORT;
          }
	}
        into_destination
        ;

into_destination:
        OUTFILE TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7701
	{
7702
          LEX *lex= Lex;
7703
          lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
7704
          if (!(lex->exchange= new sql_exchange($2.str, 0)) ||
7705 7706
              !(lex->result= new select_export(lex->exchange)))
            YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7707 7708
	}
	opt_field_term opt_line_term
7709
	| DUMPFILE TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7710
	{
7711
	  LEX *lex=Lex;
7712 7713
	  if (!lex->describe)
	  {
7714
	    lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
7715
	    if (!(lex->exchange= new sql_exchange($2.str,1)))
7716 7717 7718 7719
	      YYABORT;
	    if (!(lex->result= new select_dump(lex->exchange)))
	      YYABORT;
	  }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7720
	}
7721
        | select_var_list_init
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
7722
	{
7723
	  Lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
7724 7725
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7726

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7727 7728 7729
/*
  DO statement
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7730

7731
do:	DO_SYM
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7732 7733 7734
	{
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DO;
7735 7736 7737 7738 7739
	  mysql_init_select(lex);
	}
	expr_list
	{
	  Lex->insert_list= $3;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7740
	}
7741 7742
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7743
/*
7744
  Drop : delete tables or index or user
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7745 7746 7747
*/

drop:
serg@serg.mylan's avatar
serg@serg.mylan committed
7748
	DROP opt_temporary table_or_tables if_exists table_list opt_restrict
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7749
	{
7750 7751
	  LEX *lex=Lex;
	  lex->sql_command = SQLCOM_DROP_TABLE;
7752 7753
	  lex->drop_temporary= $2;
	  lex->drop_if_exists= $4;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7754
	}
7755
	| DROP INDEX_SYM ident ON table_ident {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7756
	  {
7757 7758
	     LEX *lex=Lex;
	     lex->sql_command= SQLCOM_DROP_INDEX;
7759 7760 7761
	     lex->alter_info.drop_list.empty();
	     lex->alter_info.drop_list.push_back(new Alter_drop(Alter_drop::KEY,
                                                                $3.str));
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7762 7763
	     if (!lex->current_select->add_table_to_list(lex->thd, $5, NULL,
							TL_OPTION_UPDATING))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7764 7765 7766 7767
	      YYABORT;
	  }
	| DROP DATABASE if_exists ident
	  {
7768 7769 7770
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_DROP_DB;
	    lex->drop_if_exists=$3;
7771
	    lex->name= $4;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7772
	 }
7773
	| DROP FUNCTION_SYM if_exists sp_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7774
	  {
7775
	    LEX *lex=Lex;
7776 7777
	    if (lex->sphead)
	    {
7778
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "FUNCTION");
7779 7780
	      YYABORT;
	    }
7781
	    lex->sql_command = SQLCOM_DROP_FUNCTION;
7782 7783 7784 7785 7786 7787 7788 7789
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
	  }
	| DROP PROCEDURE if_exists sp_name
	  {
	    LEX *lex=Lex;
	    if (lex->sphead)
	    {
7790
	      my_error(ER_SP_NO_DROP_SP, MYF(0), "PROCEDURE");
7791 7792 7793 7794 7795
	      YYABORT;
	    }
	    lex->sql_command = SQLCOM_DROP_PROCEDURE;
	    lex->drop_if_exists= $3;
	    lex->spname= $4;
7796
	  }
7797
	| DROP USER clear_privileges user_list
7798
	  {
7799 7800
	    Lex->sql_command = SQLCOM_DROP_USER;
          }
7801 7802
	| DROP VIEW_SYM if_exists table_list opt_restrict
	  {
7803
	    LEX *lex= Lex;
7804 7805 7806
	    lex->sql_command= SQLCOM_DROP_VIEW;
	    lex->drop_if_exists= $3;
	  }
7807 7808
        | DROP EVENT_SYM if_exists sp_name
          {
7809 7810 7811
            Lex->drop_if_exists= $3;
            Lex->spname= $4;
            Lex->sql_command = SQLCOM_DROP_EVENT;
7812
          }
7813
        | DROP TRIGGER_SYM if_exists sp_name
7814 7815 7816
          {
            LEX *lex= Lex;
            lex->sql_command= SQLCOM_DROP_TRIGGER;
7817 7818
            lex->drop_if_exists= $3;
            lex->spname= $4;
7819 7820 7821 7822 7823 7824 7825 7826 7827 7828
	  }
        | DROP TABLESPACE tablespace_name opt_ts_engine opt_ts_wait
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= DROP_TABLESPACE;
          }
        | DROP LOGFILE_SYM GROUP logfile_group_name opt_ts_engine opt_ts_wait
          {
            LEX *lex= Lex;
            lex->alter_tablespace_info->ts_cmd_type= DROP_LOGFILE_GROUP;
7829
         }
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
7830 7831 7832 7833 7834 7835 7836
        | DROP SERVER_SYM if_exists ident_or_text
        {
          Lex->sql_command = SQLCOM_DROP_SERVER;
          Lex->drop_if_exists= $3;
          Lex->server_options.server_name= $4.str;
          Lex->server_options.server_name_length= $4.length;
        }
7837
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7838 7839

table_list:
7840
	table_name
7841
	| table_list ',' table_name;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7842

7843
table_name:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7844
	table_ident
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7845 7846 7847 7848 7849
	{
	  if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
	    YYABORT;
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7850 7851

if_exists:
7852
	/* empty */ { $$= 0; }
7853 7854
	| IF EXISTS { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7855

7856 7857 7858 7859
opt_temporary:
	/* empty */ { $$= 0; }
	| TEMPORARY { $$= 1; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7860 7861 7862 7863 7864
/*
** Insert : add new data to table
*/

insert:
7865 7866 7867
	INSERT
	{
	  LEX *lex= Lex;
7868 7869
	  lex->sql_command= SQLCOM_INSERT;
	  lex->duplicates= DUP_ERROR; 
7870
	  mysql_init_select(lex);
7871 7872 7873
	  /* for subselects */
          lex->lock_option= (using_update_log) ? TL_READ_NO_INSERT : TL_READ;
	} insert_lock_option
7874
	opt_ignore insert2
7875
	{
7876
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7877
	  Lex->current_select= &Lex->select_lex;
7878
	}
7879
	insert_field_spec opt_insert_update
7880
	{}
7881
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7882 7883

replace:
7884 7885
	REPLACE
	{
7886
	  LEX *lex=Lex;
7887 7888
	  lex->sql_command = SQLCOM_REPLACE;
	  lex->duplicates= DUP_REPLACE;
7889
	  mysql_init_select(lex);
7890
	}
7891 7892
	replace_lock_option insert2
	{
7893
	  Select->set_lock_for_tables($3);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7894
	  Lex->current_select= &Lex->select_lex;
7895 7896
	}
	insert_field_spec
7897
	{}
7898
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7899 7900

insert_lock_option:
7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913
	/* empty */
          {
#ifdef HAVE_QUERY_CACHE
            /*
              If it is SP we do not allow insert optimisation whan result of
              insert visible only after the table unlocking but everyone can
              read table.
            */
            $$= (Lex->sphead ? TL_WRITE :TL_WRITE_CONCURRENT_INSERT);
#else
            $$= TL_WRITE_CONCURRENT_INSERT;
#endif
          }
7914 7915 7916
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; }
	| DELAYED_SYM	{ $$= TL_WRITE_DELAYED; }
	| HIGH_PRIORITY { $$= TL_WRITE; }
7917
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7918 7919

replace_lock_option:
7920 7921
	opt_low_priority { $$= $1; }
	| DELAYED_SYM	 { $$= TL_WRITE_DELAYED; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7922 7923 7924

insert2:
	INTO insert_table {}
7925
	| insert_table {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7926 7927

insert_table:
7928
	table_name
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7929
	{
7930 7931 7932 7933
	  LEX *lex=Lex;
	  lex->field_list.empty();
	  lex->many_values.empty();
	  lex->insert_list=0;
7934
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7935 7936

insert_field_spec:
serg@serg.mylan's avatar
serg@serg.mylan committed
7937 7938 7939
	insert_values {}
	| '(' ')' insert_values {}
	| '(' fields ')' insert_values {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7940 7941
	| SET
	  {
7942 7943 7944
	    LEX *lex=Lex;
	    if (!(lex->insert_list = new List_item) ||
		lex->many_values.push_back(lex->insert_list))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7945 7946
	      YYABORT;
	   }
7947
	   ident_eq_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7948 7949 7950

fields:
	fields ',' insert_ident { Lex->field_list.push_back($3); }
7951
	| insert_ident		{ Lex->field_list.push_back($1); };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7952 7953 7954

insert_values:
	VALUES	values_list  {}
7955
	| VALUE_SYM values_list  {}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
7956 7957
	|     create_select     { Select->set_braces(0);} union_clause {}
	| '(' create_select ')' { Select->set_braces(1);} union_opt {}
7958
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7959 7960 7961

values_list:
	values_list ','  no_braces
7962
	| no_braces;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7963 7964 7965 7966

ident_eq_list:
	ident_eq_list ',' ident_eq_value
	|
7967
	ident_eq_value;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7968 7969

ident_eq_value:
7970
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7971
	 {
7972 7973 7974
	  LEX *lex=Lex;
	  if (lex->field_list.push_back($1) ||
	      lex->insert_list->push_back($3))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7975
	    YYABORT;
7976
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7977 7978

equal:	EQ		{}
7979 7980 7981 7982 7983 7984 7985
	| SET_VAR	{}
	;

opt_equal:
	/* empty */	{}
	| equal		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7986 7987 7988 7989 7990 7991 7992 7993 7994

no_braces:
	 '('
	 {
	    if (!(Lex->insert_list = new List_item))
	      YYABORT;
	 }
	 opt_values ')'
	 {
7995 7996
	  LEX *lex=Lex;
	  if (lex->many_values.push_back(lex->insert_list))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7997
	    YYABORT;
7998
	 };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7999 8000 8001

opt_values:
	/* empty */ {}
8002
	| values;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8003 8004

values:
8005
	values ','  expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8006 8007 8008 8009
	{
	  if (Lex->insert_list->push_back($3))
	    YYABORT;
	}
8010 8011 8012 8013 8014 8015 8016 8017
	| expr_or_default
	  {
	    if (Lex->insert_list->push_back($1))
	      YYABORT;
	  }
	;

expr_or_default:
8018
	expr	  { $$= $1;}
8019
	| DEFAULT {$$= new Item_default_value(Lex->current_context()); }
8020
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8021

8022 8023
opt_insert_update:
        /* empty */
monty@mysql.com's avatar
monty@mysql.com committed
8024
        | ON DUPLICATE_SYM	{ Lex->duplicates= DUP_UPDATE; }
8025
          KEY_SYM UPDATE_SYM insert_update_list
8026 8027
        ;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8028 8029 8030
/* Update rows in a table */

update:
8031 8032
	UPDATE_SYM
	{
8033
	  LEX *lex= Lex;
8034
	  mysql_init_select(lex);
8035
          lex->sql_command= SQLCOM_UPDATE;
8036
	  lex->lock_option= TL_UNLOCK; 	/* Will be set later */
8037
	  lex->duplicates= DUP_ERROR; 
8038
        }
8039
        opt_low_priority opt_ignore join_table_list
8040
	SET update_list
8041
	{
8042 8043
	  LEX *lex= Lex;
          if (lex->select_lex.table_list.elements > 1)
8044
            lex->sql_command= SQLCOM_UPDATE_MULTI;
8045 8046 8047
	  else if (lex->select_lex.get_table_list()->derived)
	  {
	    /* it is single table update and it is update of derived table */
8048 8049
	    my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
                     lex->select_lex.get_table_list()->alias, "UPDATE");
8050 8051
	    YYABORT;
	  }
8052 8053 8054 8055 8056 8057
          /*
            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);
8058
	}
8059
	where_clause opt_order_clause delete_limit_clause {}
8060
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8061 8062

update_list:
8063 8064 8065 8066
	update_list ',' update_elem
	| update_elem;

update_elem:
monty@mishka.local's avatar
monty@mishka.local committed
8067
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8068
	{
monty@mysql.com's avatar
monty@mysql.com committed
8069
	  if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8070
	    YYABORT;
8071 8072 8073 8074 8075 8076 8077
	};

insert_update_list:
	insert_update_list ',' insert_update_elem
	| insert_update_elem;

insert_update_elem:
monty@mishka.local's avatar
monty@mishka.local committed
8078
	simple_ident_nospvar equal expr_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8079
	  {
8080 8081 8082
	  LEX *lex= Lex;
	  if (lex->update_list.push_back($1) || 
	      lex->value_list.push_back($3))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8083
	      YYABORT;
8084
	  };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8085 8086

opt_low_priority:
8087
	/* empty */	{ $$= YYTHD->update_lock_default; }
8088
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8089 8090 8091 8092

/* Delete rows from a table */

delete:
8093
	DELETE_SYM
8094
	{
8095 8096
	  LEX *lex= Lex;
	  lex->sql_command= SQLCOM_DELETE;
8097
	  mysql_init_select(lex);
8098
	  lex->lock_option= lex->thd->update_lock_default;
8099
	  lex->ignore= 0;
8100
	  lex->select_lex.init_order();
8101
	}
8102 8103
	opt_delete_options single_multi {}
	;
8104 8105

single_multi:
8106 8107
 	FROM table_ident
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8108 8109
	  if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
					 Lex->lock_option))
8110 8111 8112
	    YYABORT;
	}
	where_clause opt_order_clause
8113
	delete_limit_clause {}
8114
	| table_wild_list
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8115
	  { mysql_init_multi_delete(Lex); }
8116
          FROM join_table_list where_clause
8117 8118 8119 8120
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8121 8122
	| FROM table_wild_list
	  { mysql_init_multi_delete(Lex); }
8123
	  USING join_table_list where_clause
8124 8125 8126 8127
          { 
            if (multi_delete_set_locks_and_link_aux_tables(Lex))
              YYABORT;
          }
8128
	;
8129 8130 8131

table_wild_list:
	  table_wild_one {}
8132
	  | table_wild_list ',' table_wild_one {};
8133 8134

table_wild_one:
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
8135
	ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8136
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8137
	  if (!Select->add_table_to_list(YYTHD, new Table_ident($1), $3,
8138 8139
					 TL_OPTION_UPDATING | 
                                         TL_OPTION_ALIAS, Lex->lock_option))
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8140 8141
	    YYABORT;
        }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
8142
	| ident '.' ident opt_wild opt_table_alias
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8143
	  {
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8144 8145
	    if (!Select->add_table_to_list(YYTHD,
					   new Table_ident(YYTHD, $1, $3, 0),
8146 8147 8148
					   $5, 
                                           TL_OPTION_UPDATING | 
                                           TL_OPTION_ALIAS,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8149
					   Lex->lock_option))
8150
	      YYABORT;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8151
	  }
8152
	;
8153 8154

opt_wild:
8155
	/* empty */	{}
8156
	| '.' '*'	{};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8157 8158


8159
opt_delete_options:
8160
	/* empty */	{}
8161
	| opt_delete_option opt_delete_options {};
8162 8163

opt_delete_option:
8164
	QUICK		{ Select->options|= OPTION_QUICK; }
8165
	| LOW_PRIORITY	{ Lex->lock_option= TL_WRITE_LOW_PRIORITY; }
8166
	| IGNORE_SYM	{ Lex->ignore= 1; };
8167

8168
truncate:
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
8169
	TRUNCATE_SYM opt_table_sym table_name
8170
	{
8171
	  LEX* lex= Lex;
8172
	  lex->sql_command= SQLCOM_TRUNCATE;
8173
	  lex->select_lex.options= 0;
8174
          lex->select_lex.sql_cache= SELECT_LEX::SQL_CACHE_UNSPECIFIED;
8175
	  lex->select_lex.init_order();
8176 8177
	}
	;
8178

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8179 8180
opt_table_sym:
	/* empty */
8181
	| TABLE_SYM;
8182

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8183 8184
/* Show things */

8185 8186
show:	SHOW
	{
8187 8188
	  LEX *lex=Lex;
	  lex->wild=0;
8189 8190 8191
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
8192 8193
	  bzero((char*) &lex->create_info,sizeof(lex->create_info));
	}
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8194
	show_param
8195 8196
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8197 8198

show_param:
8199
         DATABASES wild_and_where
8200 8201
         {
           LEX *lex= Lex;
8202
           lex->sql_command= SQLCOM_SHOW_DATABASES;
8203 8204 8205
           if (prepare_schema_table(YYTHD, lex, 0, SCH_SCHEMATA))
             YYABORT;
         }
8206
         | opt_full TABLES opt_db wild_and_where
8207 8208
           {
             LEX *lex= Lex;
8209
             lex->sql_command= SQLCOM_SHOW_TABLES;
8210
             lex->select_lex.db= $3;
8211 8212 8213
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLE_NAMES))
               YYABORT;
           }
8214 8215 8216
         | opt_full TRIGGERS_SYM opt_db wild_and_where
           {
             LEX *lex= Lex;
8217
             lex->sql_command= SQLCOM_SHOW_TRIGGERS;
8218 8219 8220 8221
             lex->select_lex.db= $3;
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TRIGGERS))
               YYABORT;
           }
8222
         | EVENTS_SYM opt_db wild_and_where
8223 8224
           {
             LEX *lex= Lex;
8225
             lex->sql_command= SQLCOM_SHOW_EVENTS;
8226
             lex->select_lex.db= $2;
8227 8228 8229
             if (prepare_schema_table(YYTHD, lex, 0, SCH_EVENTS))
               YYABORT;
           }
8230
         | TABLE_SYM STATUS_SYM opt_db wild_and_where
8231 8232
           {
             LEX *lex= Lex;
8233
             lex->sql_command= SQLCOM_SHOW_TABLE_STATUS;
8234
             lex->select_lex.db= $3;
8235 8236 8237
             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLES))
               YYABORT;
           }
8238
        | OPEN_SYM TABLES opt_db wild_and_where
8239
	  {
8240
	    LEX *lex= Lex;
8241
            lex->sql_command= SQLCOM_SHOW_OPEN_TABLES;
8242
	    lex->select_lex.db= $3;
8243 8244
            if (prepare_schema_table(YYTHD, lex, 0, SCH_OPEN_TABLES))
              YYABORT;
8245
	  }
acurtis@xiphis.org's avatar
acurtis@xiphis.org committed
8246
        | opt_full PLUGIN_SYM
8247 8248 8249
	  {
	    LEX *lex= Lex;
	    WARN_DEPRECATED(yythd, "5.2", "SHOW PLUGIN", "'SHOW PLUGINS'");
8250
            lex->sql_command= SQLCOM_SHOW_PLUGINS;
8251 8252 8253 8254
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PLUGINS))
              YYABORT;
	  }
        | PLUGINS_SYM
8255 8256
	  {
	    LEX *lex= Lex;
8257
            lex->sql_command= SQLCOM_SHOW_PLUGINS;
8258 8259 8260
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PLUGINS))
              YYABORT;
	  }
8261
	| ENGINE_SYM storage_engines 
8262 8263
	  { Lex->create_info.db_type= $2; }
	  show_engine_param
8264
	| ENGINE_SYM ALL 
8265
	  { Lex->create_info.db_type= NULL; }
8266
	  show_engine_param
8267
	| opt_full COLUMNS from_or_in table_ident opt_db wild_and_where
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8268
	  {
8269
 	    LEX *lex= Lex;
8270
	    lex->sql_command= SQLCOM_SHOW_FIELDS;
8271 8272 8273
	    if ($5)
	      $4->change_db($5);
	    if (prepare_schema_table(YYTHD, lex, $4, SCH_COLUMNS))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8274 8275
	      YYABORT;
	  }
8276
        | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ
8277 8278
	  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
8279
	ulong_num
8280 8281 8282 8283
          {
	    Lex->sql_command = SQLCOM_SHOW_NEW_MASTER;
	    Lex->mi.log_file_name = $8.str;
	    Lex->mi.pos = $12;
8284
	    Lex->mi.server_id = $16;
8285
          }
8286
        | master_or_binary LOGS_SYM
8287 8288
          {
	    Lex->sql_command = SQLCOM_SHOW_BINLOGS;
8289 8290 8291 8292 8293
          }
        | SLAVE HOSTS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_HOSTS;
          }
8294
        | BINLOG_SYM EVENTS_SYM binlog_in binlog_from
8295
          {
8296 8297
	    LEX *lex= Lex;
	    lex->sql_command= SQLCOM_SHOW_BINLOG_EVENTS;
8298
          } opt_limit_clause_init
8299
        | keys_or_index from_or_in table_ident opt_db where_clause
8300 8301
          {
            LEX *lex= Lex;
8302
            lex->sql_command= SQLCOM_SHOW_KEYS;
8303 8304 8305
	    if ($4)
	      $3->change_db($4);
            if (prepare_schema_table(YYTHD, lex, $3, SCH_STATISTICS))
8306
              YYABORT;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8307
	  }
8308 8309 8310 8311 8312 8313 8314 8315
	| COLUMN_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_COLUMN_TYPES;
	  }
	| TABLE_SYM TYPES_SYM
	  {
	    LEX *lex=Lex;
8316
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
8317
	    WARN_DEPRECATED(yythd, "5.2", "SHOW TABLE TYPES", "'SHOW [STORAGE] ENGINES'");
8318 8319 8320 8321 8322
	  }
	| opt_storage ENGINES_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
8323 8324
            if (prepare_schema_table(YYTHD, lex, 0, SCH_ENGINES))
              YYABORT;
8325
	  }
brian@grrr.local's avatar
brian@grrr.local committed
8326 8327 8328 8329 8330
	| AUTHORS_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_AUTHORS;
	  }
8331 8332 8333 8334 8335
	| CONTRIBUTORS_SYM
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_CONTRIBUTORS;
	  }
8336 8337 8338 8339 8340
	| PRIVILEGES
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_PRIVILEGES;
	  }
8341
        | COUNT_SYM '(' '*' ')' WARNINGS
8342
          { (void) create_select_for_variable("warning_count"); }
8343
        | COUNT_SYM '(' '*' ')' ERRORS
8344
	  { (void) create_select_for_variable("error_count"); }
8345
        | WARNINGS opt_limit_clause_init
8346
          { Lex->sql_command = SQLCOM_SHOW_WARNS;}
8347
        | ERRORS opt_limit_clause_init
8348
          { Lex->sql_command = SQLCOM_SHOW_ERRORS;}
8349
        | opt_var_type STATUS_SYM wild_and_where
8350
          {
8351
            LEX *lex= Lex;
8352
            lex->sql_command= SQLCOM_SHOW_STATUS;
8353
            lex->option_type= $1;
8354 8355
            if (prepare_schema_table(YYTHD, lex, 0, SCH_STATUS))
              YYABORT;
8356
	  }
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
8357
        | INNOBASE_SYM STATUS_SYM
8358 8359 8360
          {
            LEX *lex= Lex;
            lex->sql_command = SQLCOM_SHOW_ENGINE_STATUS;
8361 8362 8363 8364 8365 8366
            if (!(lex->create_info.db_type=
                  ha_resolve_by_legacy_type(YYTHD, DB_TYPE_INNODB)))
            {
	      my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), "InnoDB");
	      YYABORT;
            }
8367
            WARN_DEPRECATED(yythd, "5.2", "SHOW INNODB STATUS", "'SHOW ENGINE INNODB STATUS'");
8368
	  }
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
8369
        | MUTEX_SYM STATUS_SYM
8370 8371
          {
	    LEX *lex= Lex;
8372
            lex->sql_command = SQLCOM_SHOW_ENGINE_MUTEX;
8373 8374 8375 8376 8377 8378
            if (!(lex->create_info.db_type=
                  ha_resolve_by_legacy_type(YYTHD, DB_TYPE_INNODB)))
            {
	      my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), "InnoDB");
	      YYABORT;
            }
8379
            WARN_DEPRECATED(yythd, "5.2", "SHOW MUTEX STATUS", "'SHOW ENGINE INNODB MUTEX'");
8380
	  }
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8381 8382
	| opt_full PROCESSLIST_SYM
	  { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
8383
        | opt_var_type  VARIABLES wild_and_where
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
8384
	  {
8385
            LEX *lex= Lex;
8386
            lex->sql_command= SQLCOM_SHOW_VARIABLES;
8387
            lex->option_type= $1;
8388 8389
            if (prepare_schema_table(YYTHD, lex, 0, SCH_VARIABLES))
              YYABORT;
8390
	  }
8391
        | charset wild_and_where
8392 8393
          {
            LEX *lex= Lex;
8394
            lex->sql_command= SQLCOM_SHOW_CHARSETS;
8395 8396 8397
            if (prepare_schema_table(YYTHD, lex, 0, SCH_CHARSETS))
              YYABORT;
          }
8398
        | COLLATION_SYM wild_and_where
8399 8400
          {
            LEX *lex= Lex;
8401
            lex->sql_command= SQLCOM_SHOW_COLLATIONS;
8402 8403 8404
            if (prepare_schema_table(YYTHD, lex, 0, SCH_COLLATIONS))
              YYABORT;
          }
8405 8406 8407 8408 8409
	| GRANTS
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    LEX_USER *curr_user;
8410
            if (!(curr_user= (LEX_USER*) lex->thd->alloc(sizeof(st_lex_user))))
8411
              YYABORT;
8412
            bzero(curr_user, sizeof(st_lex_user));
8413 8414
	    lex->grant_user= curr_user;
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8415
	| GRANTS FOR_SYM user
8416 8417 8418 8419
	  {
	    LEX *lex=Lex;
	    lex->sql_command= SQLCOM_SHOW_GRANTS;
	    lex->grant_user=$3;
8420
	    lex->grant_user->password=null_lex_str;
8421
	  }
8422
	| CREATE DATABASE opt_if_not_exists ident
8423 8424
	  {
	    Lex->sql_command=SQLCOM_SHOW_CREATE_DB;
8425
	    Lex->create_info.options=$3;
8426
	    Lex->name= $4;
8427
	  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8428 8429
        | CREATE TABLE_SYM table_ident
          {
8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440
            LEX *lex= Lex;
	    lex->sql_command = SQLCOM_SHOW_CREATE;
	    if (!lex->select_lex.add_table_to_list(YYTHD, $3, NULL,0))
	      YYABORT;
            lex->only_view= 0;
	  }
        | CREATE VIEW_SYM table_ident
          {
            LEX *lex= Lex;
	    lex->sql_command = SQLCOM_SHOW_CREATE;
	    if (!lex->select_lex.add_table_to_list(YYTHD, $3, NULL, 0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8441
	      YYABORT;
8442
            lex->only_view= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8443 8444 8445 8446
	  }
        | MASTER_SYM STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_MASTER_STAT;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8447
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8448 8449 8450
        | SLAVE STATUS_SYM
          {
	    Lex->sql_command = SQLCOM_SHOW_SLAVE_STAT;
8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465
          }
	| 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;
	  }
8466
	| PROCEDURE STATUS_SYM wild_and_where
8467
	  {
8468
            LEX *lex= Lex;
8469
            lex->sql_command= SQLCOM_SHOW_STATUS_PROC;
8470 8471
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
8472 8473
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
8474
	  }
8475
	| FUNCTION_SYM STATUS_SYM wild_and_where
8476
	  {
8477
            LEX *lex= Lex;
8478
            lex->sql_command= SQLCOM_SHOW_STATUS_FUNC;
8479 8480
	    if (!sp_add_to_query_tables(YYTHD, lex, "mysql", "proc", TL_READ))
	      YYABORT;
8481 8482
            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
              YYABORT;
pem@mysql.com's avatar
pem@mysql.com committed
8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503
	  }
        | PROCEDURE CODE_SYM sp_name
          {
#ifdef DBUG_OFF
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
#else
            Lex->sql_command= SQLCOM_SHOW_PROC_CODE;
	    Lex->spname= $3;
#endif
          }
        | FUNCTION_SYM CODE_SYM sp_name
          {
#ifdef DBUG_OFF
            yyerror(ER(ER_SYNTAX_ERROR));
            YYABORT;
#else
            Lex->sql_command= SQLCOM_SHOW_FUNC_CODE;
	    Lex->spname= $3;
#endif
          }
8504 8505
        | CREATE EVENT_SYM sp_name
          {
andrey@lmy004's avatar
andrey@lmy004 committed
8506
            Lex->spname= $3;
8507
            Lex->sql_command = SQLCOM_SHOW_CREATE_EVENT;
8508 8509
          }
      ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8510

8511 8512
show_engine_param:
	STATUS_SYM
8513 8514 8515
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_STATUS; }
	| MUTEX_SYM
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_MUTEX; }
8516
	| LOGS_SYM
8517
	  { Lex->sql_command= SQLCOM_SHOW_ENGINE_LOGS; };
8518

8519 8520 8521 8522
master_or_binary:
	MASTER_SYM
	| BINARY;

8523 8524 8525 8526
opt_storage:
	/* empty */
	| STORAGE_SYM;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8527 8528
opt_db:
	/* empty */  { $$= 0; }
8529
	| from_or_in ident { $$= $2.str; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8530

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8531 8532
opt_full:
	/* empty */ { Lex->verbose=0; }
8533
	| FULL	    { Lex->verbose=1; };
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8534

8535 8536
from_or_in:
	FROM
8537
	| IN_SYM;
8538

8539 8540
binlog_in:
	/* empty */ { Lex->mi.log_file_name = 0; }
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8541
        | IN_SYM TEXT_STRING_sys { Lex->mi.log_file_name = $2.str; };
8542 8543 8544

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

8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559
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();
        }
      ;

8560

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8561 8562 8563 8564
/* A Oracle compatible synonym for show */
describe:
	describe_command table_ident
	{
8565 8566 8567 8568
          LEX *lex= Lex;
          lex->lock_option= TL_READ;
          mysql_init_select(lex);
          lex->current_select->parsing_place= SELECT_LIST;
8569
          lex->sql_command= SQLCOM_SHOW_FIELDS;
8570 8571 8572
          lex->select_lex.db= 0;
          lex->verbose= 0;
          if (prepare_schema_table(YYTHD, lex, $2, SCH_COLUMNS))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8573 8574
	    YYABORT;
	}
8575
	opt_describe_column {}
8576 8577 8578
	| describe_command opt_extended_describe
	  { Lex->describe|= DESCRIBE_NORMAL; }
	  select
8579
          {
8580
	    LEX *lex=Lex;
8581
	    lex->select_lex.options|= SELECT_DESCRIBE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8582 8583
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8584 8585 8586

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

8589 8590 8591
opt_extended_describe:
	/* empty */ {}
	| EXTENDED_SYM { Lex->describe|= DESCRIBE_EXTENDED; }
8592
	| PARTITIONS_SYM { Lex->describe|= DESCRIBE_PARTITIONS; }
8593 8594
	;

8595

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8596 8597 8598
opt_describe_column:
	/* empty */	{}
	| text_string	{ Lex->wild= $1; }
8599
	| ident
8600
	  { 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
8601 8602 8603 8604 8605


/* flush things */

flush:
8606
	FLUSH_SYM opt_no_write_to_binlog
8607 8608
	{
	  LEX *lex=Lex;
8609 8610
	  lex->sql_command= SQLCOM_FLUSH;
          lex->type= 0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8611
          lex->no_write_to_binlog= $2;
8612
	}
8613 8614 8615
	flush_options
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8616 8617 8618

flush_options:
	flush_options ',' flush_option
8619
	| flush_option;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8620 8621

flush_option:
8622
	table_or_tables	{ Lex->type|= REFRESH_TABLES; } opt_table_list {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8623
	| TABLES WITH READ_SYM LOCK_SYM { Lex->type|= REFRESH_TABLES | REFRESH_READ_LOCK; }
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8624
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE_FREE; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8625 8626 8627 8628
	| 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
8629 8630
        | SLAVE         { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM    { Lex->type|= REFRESH_MASTER; }
8631 8632
	| 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
8633

8634
opt_table_list:
8635 8636
	/* empty */  {;}
	| table_list {;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8637

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8638
reset:
8639 8640 8641 8642
	RESET_SYM
	{
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RESET; lex->type=0;
8643 8644 8645 8646
	} reset_options
	{}
	;

monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8647 8648
reset_options:
	reset_options ',' reset_option
8649
	| reset_option;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8650 8651

reset_option:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8652 8653
        SLAVE                 { Lex->type|= REFRESH_SLAVE; }
        | MASTER_SYM          { Lex->type|= REFRESH_MASTER; }
8654
	| QUERY_SYM CACHE_SYM { Lex->type|= REFRESH_QUERY_CACHE;};
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8655

8656
purge:
8657 8658 8659 8660
	PURGE
	{
	  LEX *lex=Lex;
	  lex->type=0;
8661 8662 8663 8664 8665
	} purge_options
	{}
	;

purge_options:
8666
	master_or_binary LOGS_SYM purge_option
8667
	;
8668 8669

purge_option:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8670
        TO_SYM TEXT_STRING_sys
8671 8672 8673 8674 8675 8676
        {
	   Lex->sql_command = SQLCOM_PURGE;
	   Lex->to_log = $2.str;
        }
	| BEFORE_SYM expr
	{
8677 8678 8679 8680
	  LEX *lex= Lex;
	  lex->value_list.empty();
	  lex->value_list.push_front($2);
	  lex->sql_command= SQLCOM_PURGE_BEFORE;
8681
	}
8682
	;
8683

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8684 8685 8686
/* kill threads */

kill:
8687
	KILL_SYM kill_option expr
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8688
	{
8689
	  LEX *lex=Lex;
8690
	  lex->value_list.empty();
8691 8692
	  lex->value_list.push_front($3);
          lex->sql_command= SQLCOM_KILL;
8693
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8694

8695 8696 8697
kill_option:
	/* empty */	 { Lex->type= 0; }
	| CONNECTION_SYM { Lex->type= 0; }
serg@serg.mylan's avatar
serg@serg.mylan committed
8698 8699
	| QUERY_SYM      { Lex->type= ONLY_KILL_QUERY; }
        ;
8700

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8701 8702 8703
/* change database */

use:	USE_SYM ident
8704 8705
	{
	  LEX *lex=Lex;
8706 8707
	  lex->sql_command=SQLCOM_CHANGE_DB;
	  lex->select_lex.db= $2.str;
8708
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8709 8710 8711

/* import, export of files */

serg@serg.mylan's avatar
serg@serg.mylan committed
8712
load:   LOAD DATA_SYM
8713 8714
        {
          LEX *lex=Lex;
8715 8716 8717 8718 8719
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD DATA");
	    YYABORT;
	  }
8720 8721 8722 8723 8724 8725 8726
          lex->fname_start= lex->ptr;
        }
        load_data
        {}
        |
        LOAD TABLE_SYM table_ident FROM MASTER_SYM
        {
8727
	  LEX *lex=Lex;
8728 8729
          WARN_DEPRECATED(yythd, "5.2", "LOAD TABLE FROM MASTER",
                          "MySQL Administrator (mysqldump, mysql)");
8730
          if (lex->sphead)
8731 8732 8733 8734 8735
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOAD TABLE");
	    YYABORT;
	  }
          lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
8736 8737 8738 8739 8740
          if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
            YYABORT;
        };

load_data:
8741
	load_data_lock opt_local INFILE TEXT_STRING_filesystem
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8742
	{
8743 8744
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_LOAD;
8745 8746
	  lex->lock_option= $1;
	  lex->local_file=  $2;
8747 8748
	  lex->duplicates= DUP_ERROR;
	  lex->ignore= 0;
8749
	  if (!(lex->exchange= new sql_exchange($4.str, 0)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8750
	    YYABORT;
8751 8752 8753 8754 8755
        }
        opt_duplicate INTO
        {
	  LEX *lex=Lex;
	  lex->fname_end= lex->ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8756
	}
8757
        TABLE_SYM table_ident
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8758
        {
8759 8760 8761 8762 8763 8764 8765
          LEX *lex=Lex;
          if (!Select->add_table_to_list(YYTHD, $10, NULL, TL_OPTION_UPDATING,
                                         lex->lock_option))
            YYABORT;
          lex->field_list.empty();
          lex->update_list.empty();
          lex->value_list.empty();
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
8766
        }
8767 8768 8769
        opt_field_term opt_line_term opt_ignore_lines opt_field_or_var_spec
        opt_load_data_set_spec
        {}
8770
        |
8771
	FROM MASTER_SYM
8772 8773
        {
	  Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
8774
          WARN_DEPRECATED(yythd, "5.2", "LOAD DATA FROM MASTER",
8775 8776
                          "mysqldump or future "
                          "BACKUP/RESTORE DATABASE facility");
8777
        };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8778 8779 8780

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

8783
load_data_lock:
8784
	/* empty */	{ $$= YYTHD->update_lock_default; }
8785 8786 8787 8788 8789 8790 8791
	| CONCURRENT
          {
#ifdef HAVE_QUERY_CACHE
            /*
              Ignore this option in SP to avoid problem with query cache
            */
            if (Lex->sphead != 0)
8792 8793
              $$= YYTHD->update_lock_default;
            else
8794 8795 8796
#endif
              $$= TL_WRITE_CONCURRENT_INSERT;
          }
8797
	| LOW_PRIORITY	{ $$= TL_WRITE_LOW_PRIORITY; };
8798 8799


bk@work.mysql.com's avatar
bk@work.mysql.com committed
8800 8801 8802
opt_duplicate:
	/* empty */	{ Lex->duplicates=DUP_ERROR; }
	| REPLACE	{ Lex->duplicates=DUP_REPLACE; }
8803
	| IGNORE_SYM	{ Lex->ignore= 1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8804 8805 8806

opt_field_term:
	/* empty */
8807
	| COLUMNS field_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8808 8809 8810

field_term_list:
	field_term_list field_term
8811
	| field_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8812 8813

field_term:
8814 8815
	TERMINATED BY text_string 
          {
8816
            DBUG_ASSERT(Lex->exchange != 0);
8817 8818
            Lex->exchange->field_term= $3;
          }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8819
	| OPTIONALLY ENCLOSED BY text_string
8820
	  {
8821
            LEX *lex= Lex;
8822
            DBUG_ASSERT(lex->exchange != 0);
8823 8824
            lex->exchange->enclosed= $4;
            lex->exchange->opt_enclosed= 1;
8825
	  }
8826
        | ENCLOSED BY text_string
8827
          {
8828
            DBUG_ASSERT(Lex->exchange != 0);
8829 8830
            Lex->exchange->enclosed= $3;
          }
8831
        | ESCAPED BY text_string
8832
          {
8833
            DBUG_ASSERT(Lex->exchange != 0);
8834 8835
            Lex->exchange->escaped= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8836 8837 8838

opt_line_term:
	/* empty */
8839
	| LINES line_term_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8840 8841 8842

line_term_list:
	line_term_list line_term
8843
	| line_term;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8844 8845

line_term:
8846
        TERMINATED BY text_string
8847
          {
8848
            DBUG_ASSERT(Lex->exchange != 0);
8849 8850
            Lex->exchange->line_term= $3;
          }
8851
        | STARTING BY text_string
8852
          {
8853
            DBUG_ASSERT(Lex->exchange != 0);
8854 8855
            Lex->exchange->line_start= $3;
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8856 8857 8858

opt_ignore_lines:
	/* empty */
8859 8860
        | IGNORE_SYM NUM LINES
          {
8861
            DBUG_ASSERT(Lex->exchange != 0);
8862 8863
            Lex->exchange->skip_lines= atol($2.str);
          };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8864

8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875
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
8876

8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887
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
8888 8889 8890
/* Common definitions */

text_literal:
8891
	TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8892 8893
	{
	  THD *thd= YYTHD;
8894
	  $$ = new Item_string($1.str,$1.length,thd->variables.collation_connection);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8895
	}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8896
	| NCHAR_STRING
8897
	{ $$=  new Item_string($1.str,$1.length,national_charset_info); }
8898
	| UNDERSCORE_CHARSET TEXT_STRING
8899
	  { $$ = new Item_string($2.str,$2.length,Lex->underscore_charset); }
8900
	| text_literal TEXT_STRING_literal
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
8901 8902
	  { ((Item_string*) $1)->append($2.str,$2.length); }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8903 8904

text_string:
8905
	TEXT_STRING_literal
8906
	{ $$=  new (YYTHD->mem_root) String($1.str,$1.length,YYTHD->variables.collation_connection); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8907 8908
	| HEX_NUM
	  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8909
	    Item *tmp= new Item_hex_string($1.str, $1.length);
8910
	    /*
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8911
	      it is OK only emulate fix_fields, because we need only
8912 8913 8914
              value of constant
	    */
	    $$= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8915
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
8916
	      (String*) 0;
8917
	  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8918 8919 8920 8921 8922 8923 8924 8925 8926 8927
        | 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;
          }
8928 8929
	;

8930
param_marker:
8931
        PARAM_MARKER
8932
        {
8933 8934
          THD *thd=YYTHD;
	  LEX *lex= thd->lex;
8935 8936 8937 8938 8939 8940 8941
          Item_param *item;
          if (! lex->parsing_options.allows_variable)
          {
            my_error(ER_VIEW_SELECT_VARIABLE, MYF(0));
            YYABORT;
          }
          item= new Item_param((uint) (lex->tok_start - (uchar *) thd->query));
8942
          if (!($$= item) || lex->param_list.push_back(item))
8943
          {
8944
            my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
8945 8946
            YYABORT;
          }
8947 8948 8949
        }
	;

8950 8951 8952
signed_literal:
	literal		{ $$ = $1; }
	| '+' NUM_literal { $$ = $2; }
8953 8954
	| '-' NUM_literal
	  {
8955
	    $2->max_length++;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8956
	    $$= $2->neg();
8957
	  }
8958 8959 8960
	;


bk@work.mysql.com's avatar
bk@work.mysql.com committed
8961 8962
literal:
	text_literal	{ $$ =	$1; }
8963
	| NUM_literal	{ $$ = $1; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8964
	| NULL_SYM	{ $$ =	new Item_null();
8965
			  Lex->next_state=MY_LEX_OPERATOR_OR_IDENT;}
8966 8967
	| 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
8968 8969
	| HEX_NUM	{ $$ =	new Item_hex_string($1.str, $1.length);}
	| BIN_NUM	{ $$= new Item_bin_string($1.str, $1.length); }
8970
	| UNDERSCORE_CHARSET HEX_NUM
8971
	  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8972
	    Item *tmp= new Item_hex_string($2.str, $2.length);
8973
	    /*
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8974
	      it is OK only emulate fix_fieds, because we need only
8975 8976 8977
              value of constant
	    */
	    String *str= tmp ?
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
8978
	      tmp->quick_fix_field(), tmp->val_str((String*) 0) :
8979
	      (String*) 0;
8980 8981
	    $$= new Item_string(str ? str->ptr() : "",
				str ? str->length() : 0,
8982
				Lex->underscore_charset);
8983
	  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997
	| 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
8998 8999
	| DATE_SYM text_literal { $$ = $2; }
	| TIME_SYM text_literal { $$ = $2; }
9000
	| TIMESTAMP text_literal { $$ = $2; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9001

9002
NUM_literal:
9003 9004
	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); }
9005
	| ULONGLONG_NUM	{ $$ =	new Item_uint($1.str, $1.length); }
9006
        | DECIMAL_NUM
9007
	{
9008
           $$= new Item_decimal($1.str, $1.length, YYTHD->charset());
9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
	| FLOAT_NUM
	{
	   $$ =	new Item_float($1.str, $1.length);
	   if (YYTHD->net.report_error)
	   {
	     YYABORT;
	   }
	}
9022
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
9023

bk@work.mysql.com's avatar
bk@work.mysql.com committed
9024
/**********************************************************************
serg@serg.mylan's avatar
serg@serg.mylan committed
9025
** Creating different items.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9026 9027 9028
**********************************************************************/

insert_ident:
9029
	simple_ident_nospvar { $$=$1; }
9030
	| table_wild	 { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9031 9032

table_wild:
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9033
	ident '.' '*'
9034
	{
9035
          SELECT_LEX *sel= Select;
9036
	  $$ = new Item_field(Lex->current_context(), NullS, $1.str, "*");
9037
	  sel->with_wild++;
9038
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9039
	| ident '.' ident '.' '*'
9040
	{
9041
          SELECT_LEX *sel= Select;
9042
	  $$ = new Item_field(Lex->current_context(), (YYTHD->client_capabilities &
serg@serg.mylan's avatar
serg@serg.mylan committed
9043 9044
                             CLIENT_NO_SCHEMA ? NullS : $1.str),
                             $3.str,"*");
9045
	  sel->with_wild++;
9046 9047
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9048 9049

order_ident:
9050
	expr { $$=$1; };
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9051 9052

simple_ident:
9053 9054
	ident
	{
9055
	  sp_variable_t *spv;
9056 9057
	  LEX *lex = Lex;
          sp_pcontext *spc = lex->spcont;
9058
	  if (spc && (spv = spc->find_variable(&$1)))
9059 9060
	  {
            /* We're compiling a stored procedure and found a variable */
9061 9062 9063 9064 9065 9066
            if (! lex->parsing_options.allows_variable)
            {
              my_error(ER_VIEW_SELECT_VARIABLE, MYF(0));
              YYABORT;
            }

9067
            Item_splocal *splocal;
9068 9069
            splocal= new Item_splocal($1, spv->offset, spv->type,
                                      lex->tok_start_prev - 
9070
                                      lex->sphead->m_tmp_query);
9071
#ifndef DBUG_OFF
9072
            if (splocal)
9073
              splocal->m_sp= lex->sphead;
9074
#endif
9075
	    $$ = (Item*) splocal;
9076 9077 9078 9079 9080 9081 9082
	    lex->safe_to_cache_query=0;
	  }
	  else
	  {
	    SELECT_LEX *sel=Select;
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
9083 9084
                 (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	         (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
9085 9086 9087 9088 9089 9090
	  }
        }
        | simple_ident_q { $$= $1; }
	;

simple_ident_nospvar:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9091
	ident
9092
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9093
	  SELECT_LEX *sel=Select;
9094
	  $$= (sel->parsing_place != IN_HAVING ||
9095
	       sel->get_in_sum_expr() > 0) ?
9096 9097
              (Item*) new Item_field(Lex->current_context(), NullS, NullS, $1.str) :
	      (Item*) new Item_ref(Lex->current_context(), NullS, NullS, $1.str);
9098
	}
9099
	| simple_ident_q { $$= $1; }
serg@serg.mylan's avatar
serg@serg.mylan committed
9100
	;
9101 9102 9103

simple_ident_q:
	ident '.' ident
9104
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9105
	  THD *thd= YYTHD;
9106
	  LEX *lex= thd->lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
9107

9108 9109 9110 9111 9112 9113
          /*
            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
9114
              (!my_strcasecmp(system_charset_info, $1.str, "NEW") ||
9115 9116
               !my_strcasecmp(system_charset_info, $1.str, "OLD")))
          {
9117
            Item_trigger_field *trg_fld;
9118
            bool new_row= ($1.str[0]=='N' || $1.str[0]=='n');
serg@serg.mylan's avatar
serg@serg.mylan committed
9119

9120 9121 9122
            if (lex->trg_chistics.event == TRG_EVENT_INSERT &&
                !new_row)
            {
9123
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "OLD", "on INSERT");
9124 9125
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
9126

9127 9128 9129
            if (lex->trg_chistics.event == TRG_EVENT_DELETE &&
                new_row)
            {
9130
              my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0), "NEW", "on DELETE");
9131 9132
              YYABORT;
            }
serg@serg.mylan's avatar
serg@serg.mylan committed
9133

9134 9135 9136 9137 9138
            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);
9139
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
9140
                                                  new_row ?
9141 9142
                                                  Item_trigger_field::NEW_ROW:
                                                  Item_trigger_field::OLD_ROW,
9143
                                                  $3.str,
9144 9145
                                                  SELECT_ACL,
                                                  read_only)))
9146
              YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
9147

9148 9149 9150 9151 9152 9153
            /*
              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
9154

9155 9156 9157 9158 9159 9160 9161
            $$= (Item *)trg_fld;
          }
          else
          {
	    SELECT_LEX *sel= lex->current_select;
	    if (sel->no_table_names_allowed)
	    {
9162 9163
	      my_error(ER_TABLENAME_NOT_ALLOWED_HERE,
                       MYF(0), $1.str, thd->where);
9164 9165 9166
	    }
	    $$= (sel->parsing_place != IN_HAVING ||
	         sel->get_in_sum_expr() > 0) ?
9167 9168
	        (Item*) new Item_field(Lex->current_context(), NullS, $1.str, $3.str) :
	        (Item*) new Item_ref(Lex->current_context(), NullS, $1.str, $3.str);
9169 9170
          }
        }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9171
	| '.' ident '.' ident
9172
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9173
	  THD *thd= YYTHD;
9174
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9175
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9176 9177
	  if (sel->no_table_names_allowed)
	  {
9178 9179
	    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
9180
	  }
9181
	  $$= (sel->parsing_place != IN_HAVING ||
9182
	       sel->get_in_sum_expr() > 0) ?
9183 9184
	      (Item*) new Item_field(Lex->current_context(), NullS, $2.str, $4.str) :
              (Item*) new Item_ref(Lex->current_context(), NullS, $2.str, $4.str);
9185
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9186
	| ident '.' ident '.' ident
9187
	{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9188
	  THD *thd= YYTHD;
9189
	  LEX *lex= thd->lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9190
	  SELECT_LEX *sel= lex->current_select;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9191 9192
	  if (sel->no_table_names_allowed)
	  {
9193 9194
	    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
9195
	  }
9196
	  $$= (sel->parsing_place != IN_HAVING ||
9197
	       sel->get_in_sum_expr() > 0) ?
9198
	      (Item*) new Item_field(Lex->current_context(),
9199
                                     (YYTHD->client_capabilities &
9200 9201
				      CLIENT_NO_SCHEMA ? NullS : $1.str),
				     $3.str, $5.str) :
9202
	      (Item*) new Item_ref(Lex->current_context(),
9203
                                   (YYTHD->client_capabilities &
9204
				    CLIENT_NO_SCHEMA ? NullS : $1.str),
9205
                                   $3.str, $5.str);
9206
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9207 9208 9209 9210


field_ident:
	ident			{ $$=$1;}
9211 9212 9213 9214 9215
	| ident '.' ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->db))
            {
9216
              my_error(ER_WRONG_DB_NAME, MYF(0), $1.str);
9217 9218
              YYABORT;
            }
9219 9220
            if (my_strcasecmp(table_alias_charset, $3.str,
                              table->table_name))
9221
            {
9222
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $3.str);
9223 9224 9225 9226 9227 9228 9229 9230 9231
              YYABORT;
            }
            $$=$5;
          }
	| ident '.' ident
          {
            TABLE_LIST *table= (TABLE_LIST*) Select->table_list.first;
            if (my_strcasecmp(table_alias_charset, $1.str, table->alias))
            {
9232
              my_error(ER_WRONG_TABLE_NAME, MYF(0), $1.str);
9233 9234 9235 9236
              YYABORT;
            }
            $$=$3;
          }
9237
	| '.' ident		{ $$=$2;}	/* For Delphi */;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9238 9239 9240

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

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

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9249
IDENT_sys:
9250 9251 9252 9253 9254
	IDENT { $$= $1; }
	| IDENT_QUOTED
	  {
	    THD *thd= YYTHD;
	    if (thd->charset_is_system_charset)
9255 9256
            {
              CHARSET_INFO *cs= system_charset_info;
9257
              int dummy_error;
9258 9259
              uint wlen= cs->cset->well_formed_len(cs, $1.str,
                                                   $1.str+$1.length,
9260
                                                   $1.length, &dummy_error);
9261 9262
              if (wlen < $1.length)
              {
9263 9264
                my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
                         cs->csname, $1.str + wlen);
9265 9266
                YYABORT;
              }
9267
	      $$= $1;
9268
            }
9269 9270 9271 9272
	    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
9273 9274 9275 9276 9277 9278
	;

TEXT_STRING_sys:
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9279 9280
	  if (thd->charset_is_system_charset)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9281
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9282 9283
	    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
9284 9285 9286
	}
	;

9287
TEXT_STRING_literal:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9288 9289 9290
	TEXT_STRING
	{
	  THD *thd= YYTHD;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9291 9292
	  if (thd->charset_is_collation_connection)
	    $$= $1;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9293
	  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9294 9295
	    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
9296 9297 9298 9299
	}
	;


9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311
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
9312
ident:
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9313
	IDENT_sys	    { $$=$1; }
9314 9315 9316 9317 9318 9319
	| READ_ONLY_SYM
	{
	  THD *thd= YYTHD;
	  $$.str= thd->strmake("read_only",9);
	  $$.length= 9;
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9320 9321
	| keyword
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9322 9323 9324
	  THD *thd= YYTHD;
	  $$.str=    thd->strmake($1.str, $1.length);
	  $$.length= $1.length;
9325 9326
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9327

9328 9329 9330 9331 9332 9333 9334 9335 9336 9337
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
9338
ident_or_text:
serg@serg.mylan's avatar
serg@serg.mylan committed
9339
        ident                   { $$=$1;}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9340 9341
	| TEXT_STRING_sys	{ $$=$1;}
	| LEX_HOSTNAME		{ $$=$1;};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9342 9343 9344 9345

user:
	ident_or_text
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9346 9347
	  THD *thd= YYTHD;
	  if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9348
	    YYABORT;
9349 9350 9351
	  $$->user = $1;
	  $$->host.str= (char *) "%";
	  $$->host.length= 1;
9352

9353
	  if (check_string_length(&$$->user,
gluh@mysql.com/gluh.(none)'s avatar
gluh@mysql.com/gluh.(none) committed
9354
                                  ER(ER_USERNAME), USERNAME_LENGTH))
9355
	    YYABORT;
9356
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9357 9358
	| ident_or_text '@' ident_or_text
	  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9359 9360
	    THD *thd= YYTHD;
	    if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9361 9362
	      YYABORT;
	    $$->user = $1; $$->host=$3;
9363

9364
	    if (check_string_length(&$$->user,
gluh@mysql.com/gluh.(none)'s avatar
gluh@mysql.com/gluh.(none) committed
9365
                                    ER(ER_USERNAME), USERNAME_LENGTH) ||
9366
	        check_string_length(&$$->host,
gluh@mysql.com/gluh.(none)'s avatar
gluh@mysql.com/gluh.(none) committed
9367
                                    ER(ER_HOSTNAME), HOSTNAME_LENGTH))
9368
	      YYABORT;
9369
	  }
9370
	| CURRENT_USER optional_braces
9371
	{
9372
          if (!($$=(LEX_USER*) YYTHD->alloc(sizeof(st_lex_user))))
9373
            YYABORT;
9374 9375 9376 9377 9378 9379
          /* 
            empty LEX_USER means current_user and 
            will be handled in the  get_current_user() function
            later
          */
          bzero($$, sizeof(LEX_USER));
9380
	};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9381

9382
/* Keyword that we allow for identifiers (except SP labels) */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9383
keyword:
9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402
	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		{}
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
9403
	| HOST_SYM              {}
9404
        | INSTALL_SYM           {}
9405 9406 9407
	| LANGUAGE_SYM          {}
	| NO_SYM		{}
	| OPEN_SYM		{}
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
9408
	| OPTIONS_SYM		{}
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
9409
	| OWNER_SYM		{}
9410
        | PARSER_SYM            {}
9411
	| PARTITION_SYM		{}
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
9412
        | PORT_SYM              {}
9413
        | PREPARE_SYM           {}
9414
	| REMOVE_SYM		{}
9415 9416 9417 9418 9419 9420
	| REPAIR		{}
	| RESET_SYM		{}
	| RESTORE_SYM		{}
	| ROLLBACK_SYM		{}
	| SAVEPOINT_SYM		{}
	| SECURITY_SYM		{}
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
9421
        | SERVER_SYM            {}
9422
	| SIGNED_SYM		{}
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
9423
	| SOCKET_SYM		{}
9424
	| SLAVE			{}
9425
        | SONAME_SYM            {}
9426 9427 9428 9429
	| START_SYM		{}
	| STOP_SYM		{}
	| TRUNCATE_SYM		{}
	| UNICODE_SYM		{}
9430
        | UNINSTALL_SYM         {}
patg@radha.tangent.org's avatar
patg@radha.tangent.org committed
9431 9432
        | USER                  {}
	| WRAPPER_SYM		{}
9433
        | XA_SYM                {}
9434
        | UPGRADE_SYM           {}
9435 9436 9437 9438
	;

/*
 * Keywords that we allow for labels in SPs.
pem@mysql.com's avatar
pem@mysql.com committed
9439 9440 9441
 * Anything that's the beginning of a statement or characteristics
 * must be in keyword above, otherwise we get (harmful) shift/reduce
 * conflicts.
9442 9443
 */
keyword_sp:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9444
	ACTION			{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
9445
	| ADDDATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9446
	| AFTER_SYM		{}
9447
	| AGAINST		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9448
	| AGGREGATE_SYM		{}
9449
	| ALGORITHM_SYM		{}
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
9450
	| ANY_SYM		{}
andrey@lmy004's avatar
andrey@lmy004 committed
9451
	| AT_SYM                {}
9452
	| AUTHORS_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9453
	| AUTO_INC		{}
9454
	| AUTOEXTEND_SIZE_SYM   {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9455 9456
	| AVG_ROW_LENGTH	{}
	| AVG_SYM		{}
9457
	| BINLOG_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9458 9459
	| BIT_SYM		{}
	| BOOL_SYM		{}
9460
	| BOOLEAN_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9461
	| BTREE_SYM		{}
9462
	| CASCADED              {}
9463
	| CHAIN_SYM		{}
9464
	| CHANGED		{}
9465
	| CIPHER_SYM		{}
9466
	| CLIENT_SYM		{}
9467
	| COALESCE		{}
9468
	| CODE_SYM              {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9469
	| COLLATION_SYM		{}
9470
        | COLUMNS               {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9471
	| COMMITTED_SYM		{}
9472
	| COMPACT_SYM		{}
9473
	| COMPLETION_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9474
	| COMPRESSED_SYM	{}
9475
	| CONCURRENT		{}
9476
	| CONSISTENT_SYM	{}
9477
	| CONTRIBUTORS_SYM	{}
9478
	| CUBE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9479
	| DATA_SYM		{}
9480
	| DATAFILE_SYM          {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9481 9482 9483
	| DATETIME		{}
	| DATE_SYM		{}
	| DAY_SYM		{}
9484
	| DEFINER_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9485
	| DELAY_KEY_WRITE_SYM	{}
9486 9487
	| DES_KEY_FILE		{}
	| DIRECTORY_SYM		{}
9488
	| DISABLE_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
9489
	| DISCARD		{}
9490
	| DISK_SYM              {}
9491 9492
	| DUMPFILE		{}
	| DUPLICATE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9493
	| DYNAMIC_SYM		{}
9494
	| ENDS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9495
	| ENUM			{}
9496
	| ENGINE_SYM		{}
9497
	| ENGINES_SYM		{}
9498
	| ERRORS		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9499
	| ESCAPE_SYM		{}
9500
	| EVENT_SYM		{}
9501
	| EVENTS_SYM		{}
andrey@lmy004's avatar
andrey@lmy004 committed
9502 9503
	| EVERY_SYM             {}
	| EXPANSION_SYM         {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9504
	| EXTENDED_SYM		{}
9505
	| EXTENT_SIZE_SYM       {}
9506
	| FAST_SYM		{}
9507
	| FOUND_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9508
	| ENABLE_SYM		{}
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
9509
	| FULL			{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9510 9511 9512
	| FILE_SYM		{}
	| FIRST_SYM		{}
	| FIXED_SYM		{}
9513
	| FRAC_SECOND_SYM	{}
9514
	| GEOMETRY_SYM		{}
9515
	| GEOMETRYCOLLECTION	{}
9516
	| GET_FORMAT		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9517
	| GRANTS		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9518
	| GLOBAL_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9519
	| HASH_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9520 9521 9522
	| HOSTS_SYM		{}
	| HOUR_SYM		{}
	| IDENTIFIED_SYM	{}
9523
	| INVOKER_SYM		{}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
9524
	| IMPORT		{}
9525
	| INDEXES		{}
9526
	| INITIAL_SIZE_SYM      {}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9527
	| ISOLATION		{}
9528
	| ISSUER_SYM		{}
9529
	| INNOBASE_SYM		{}
9530
	| INSERT_METHOD		{}
9531
	| KEY_BLOCK_SIZE	{}
9532
	| LAST_SYM		{}
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
9533
	| LEAVES                {}
9534
	| LESS_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9535
	| LEVEL_SYM		{}
9536
	| LINESTRING		{}
9537
	| LIST_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9538
	| LOCAL_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9539
	| LOCKS_SYM		{}
9540
	| LOGFILE_SYM           {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9541 9542 9543 9544 9545 9546 9547 9548 9549
	| 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	{}
9550
	| MASTER_SERVER_ID_SYM  {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9551
	| MASTER_CONNECT_RETRY_SYM	{}
9552 9553 9554 9555 9556 9557
	| 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
9558 9559
	| MAX_CONNECTIONS_PER_HOUR	 {}
	| MAX_QUERIES_PER_HOUR	{}
9560
	| MAX_SIZE_SYM          {}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9561
	| MAX_UPDATES_PER_HOUR	{}
9562
	| MAX_USER_CONNECTIONS_SYM {}
9563
	| MAX_VALUE_SYM         {}
9564
	| MEDIUM_SYM		{}
9565
	| MEMORY_SYM		{}
9566
	| MERGE_SYM		{}
9567
	| MICROSECOND_SYM	{}
9568
        | MIGRATE_SYM           {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9569 9570 9571
	| MINUTE_SYM		{}
	| MIN_ROWS		{}
	| MODIFY_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9572
	| MODE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9573
	| MONTH_SYM		{}
9574 9575 9576
	| MULTILINESTRING	{}
	| MULTIPOINT		{}
	| MULTIPOLYGON		{}
9577
        | MUTEX_SYM             {}
9578
	| NAME_SYM              {}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9579
	| NAMES_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9580 9581
	| NATIONAL_SYM		{}
	| NCHAR_SYM		{}
9582
	| NDBCLUSTER_SYM	{}
9583
	| NEXT_SYM		{}
9584
	| NEW_SYM		{}
9585 9586
	| NO_WAIT_SYM           {}
	| NODEGROUP_SYM         {}
9587
	| NONE_SYM		{}
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
9588
	| NVARCHAR_SYM		{}
9589
	| OFFSET_SYM		{}
9590
	| OLD_PASSWORD		{}
9591
	| ONE_SHOT_SYM		{}
9592
        | ONE_SYM               {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9593
	| PACK_KEYS_SYM		{}
9594
	| PARTIAL		{}
9595
	| PARTITIONING_SYM	{}
9596
	| PARTITIONS_SYM	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9597
	| PASSWORD		{}
9598
        | PHASE_SYM             {}
9599 9600
        | PLUGIN_SYM            {}
        | PLUGINS_SYM           {}
9601
	| POINT_SYM		{}
9602
	| POLYGON		{}
9603
        | PRESERVE_SYM          {}
9604
	| PREV_SYM		{}
9605
        | PRIVILEGES            {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9606 9607
	| PROCESS		{}
	| PROCESSLIST_SYM	{}
9608
	| QUARTER_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9609
	| QUERY_SYM		{}
9610
	| QUICK			{}
9611
        | REBUILD_SYM           {}
9612
        | RECOVER_SYM           {}
9613
	| REDO_BUFFER_SIZE_SYM	{}
9614
	| REDOFILE_SYM          {}
serg@serg.mylan's avatar
serg@serg.mylan committed
9615
        | REDUNDANT_SYM         {}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9616 9617
	| RELAY_LOG_FILE_SYM	{}
	| RELAY_LOG_POS_SYM	{}
9618
	| RELAY_THREAD		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9619
	| RELOAD		{}
9620
	| REORGANIZE_SYM	{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9621
	| REPEATABLE_SYM	{}
9622
	| REPLICATION		{}
9623
	| RESOURCES		{}
9624
        | RESUME_SYM            {}
9625
	| RETURNS_SYM           {}
9626
	| ROLLUP_SYM		{}
9627
	| ROUTINE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9628 9629 9630
	| ROWS_SYM		{}
	| ROW_FORMAT_SYM	{}
	| ROW_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9631
	| RTREE_SYM		{}
andrey@lmy004's avatar
andrey@lmy004 committed
9632
	| SCHEDULE_SYM		{}	
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9633
	| SECOND_SYM		{}
9634
	| SERIAL_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9635 9636
	| SERIALIZABLE_SYM	{}
	| SESSION_SYM		{}
9637
	| SIMPLE_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9638
	| SHARE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9639
	| SHUTDOWN		{}
9640
	| SNAPSHOT_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9641
	| SOUNDS_SYM		{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9642
	| SQL_CACHE_SYM		{}
9643
	| SQL_BUFFER_RESULT	{}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
9644
	| SQL_NO_CACHE_SYM	{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9645
	| SQL_THREAD		{}
9646
	| STARTS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9647
	| STATUS_SYM		{}
9648
	| STORAGE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9649
	| STRING_SYM		{}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
9650
	| SUBDATE_SYM		{}
9651
	| SUBJECT_SYM		{}
9652 9653
	| SUBPARTITION_SYM	{}
	| SUBPARTITIONS_SYM	{}
9654
	| SUPER_SYM		{}
9655
        | SUSPEND_SYM           {}
9656
        | TABLES                {}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
9657
	| TABLESPACE		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9658
	| TEMPORARY		{}
9659
	| TEMPTABLE_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9660
	| TEXT_SYM		{}
9661
	| THAN_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9662
	| TRANSACTION_SYM	{}
9663
	| TRIGGERS_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9664
	| TIMESTAMP		{}
9665 9666
	| TIMESTAMP_ADD		{}
	| TIMESTAMP_DIFF	{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9667
	| TIME_SYM		{}
9668
	| TYPES_SYM		{}
9669
        | TYPE_SYM              {}
serg@serg.mylan's avatar
serg@serg.mylan committed
9670
        | UDF_RETURNS_SYM       {}
9671
	| FUNCTION_SYM		{}
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
9672
	| UNCOMMITTED_SYM	{}
9673
	| UNDEFINED_SYM		{}
9674 9675
	| UNDO_BUFFER_SIZE_SYM	{}
	| UNDOFILE_SYM  	{}
9676
	| UNKNOWN_SYM		{}
9677
	| UNTIL_SYM		{}
9678
	| USER			{}
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
9679
	| USE_FRM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9680
	| VARIABLES		{}
9681
	| VIEW_SYM		{}
9682
	| VALUE_SYM		{}
9683
	| WARNINGS		{}
9684
	| WAIT_SYM              {}
9685
	| WEEK_SYM		{}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9686
	| WORK_SYM		{}
9687
	| X509_SYM		{}
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9688 9689
	| YEAR_SYM		{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9690 9691 9692 9693 9694 9695

/* Option functions */

set:
	SET opt_option
	{
9696 9697
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SET_OPTION;
9698
	  mysql_init_select(lex);
9699
	  lex->option_type=OPT_SESSION;
9700
	  lex->var_list.empty();
9701
          lex->one_shot_set= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9702
	}
9703 9704 9705
	option_value_list
	{}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9706 9707 9708

opt_option:
	/* empty */ {}
9709
	| OPTION {};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9710 9711

option_value_list:
9712 9713 9714 9715 9716 9717 9718 9719 9720 9721
	option_type_value
	| option_value_list ',' option_type_value;

option_type_value:
        {
          if (Lex->sphead)
          {
            /*
              If we are in SP we want have own LEX for each assignment.
              This is mostly because it is hard for several sp_instr_set
serg@serg.mylan's avatar
serg@serg.mylan committed
9722 9723
              and sp_instr_set_trigger instructions share one LEX.
              (Well, it is theoretically possible but adds some extra
9724 9725 9726 9727 9728 9729 9730 9731
               overhead on preparation for execution stage and IMO less
               robust).

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

9733 9734 9735 9736 9737 9738 9739 9740 9741
            /* Set new LEX as if we at start of set rule. */
	    lex->sql_command= SQLCOM_SET_OPTION;
	    mysql_init_select(lex);
	    lex->option_type=OPT_SESSION;
	    lex->var_list.empty();
            lex->one_shot_set= 0;
	    lex->sphead->m_tmp_query= lex->tok_start;
          }
        }
9742
	ext_option_value
9743 9744
        {
          LEX *lex= Lex;
serg@serg.mylan's avatar
serg@serg.mylan committed
9745

9746 9747 9748
          if (lex->sphead)
          {
            sp_head *sp= lex->sphead;
serg@serg.mylan's avatar
serg@serg.mylan committed
9749

9750 9751 9752 9753 9754 9755 9756 9757 9758
	    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
9759

9760 9761 9762
              if (!(i= new sp_instr_stmt(sp->instructions(), lex->spcont,
                                         lex)))
                YYABORT;
serg@serg.mylan's avatar
serg@serg.mylan committed
9763

9764 9765 9766 9767 9768 9769
              /*
                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)
9770 9771 9772
                qbuff.length= lex->ptr - sp->m_tmp_query;
              else
                qbuff.length= lex->tok_end - sp->m_tmp_query;
serg@serg.mylan's avatar
serg@serg.mylan committed
9773

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

9777 9778 9779 9780 9781 9782 9783 9784 9785
              strmake(strmake(qbuff.str, "SET ", 4), (char *)sp->m_tmp_query,
                      qbuff.length);
              qbuff.length+= 4;
              i->m_query= qbuff;
              sp->add_instr(i);
            }
            lex->sphead->restore_lex(YYTHD);
          }
        };
9786 9787

option_type:
9788 9789 9790 9791 9792 9793 9794 9795 9796
        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; }
9797 9798 9799 9800
	;

opt_var_type:
	/* empty */	{ $$=OPT_SESSION; }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
9801
	| GLOBAL_SYM	{ $$=OPT_GLOBAL; }
9802 9803 9804 9805 9806 9807
	| 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
9808
	| GLOBAL_SYM '.'	{ $$=OPT_GLOBAL; }
9809 9810 9811
	| LOCAL_SYM '.'		{ $$=OPT_SESSION; }
	| SESSION_SYM '.'	{ $$=OPT_SESSION; }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9812

9813 9814 9815
ext_option_value:
        sys_option_value
        | option_type2 option_value;
9816

9817 9818 9819 9820
sys_option_value:
        option_type internal_variable_name equal set_expr_or_default
        {
          LEX *lex=Lex;
9821

9822
          if ($2.var == trg_new_row_fake_var)
9823 9824 9825
          {
            /* We are in trigger and assigning value to field of new row */
            Item *it;
9826
            Item_trigger_field *trg_fld;
9827 9828
            sp_instr_set_trigger_field *sp_fld;
	    LINT_INIT(sp_fld);
9829 9830 9831 9832
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
9833
            }
9834 9835
            if ($4)
              it= $4;
9836
            else
9837 9838 9839 9840
            {
              /* QQ: Shouldn't this be field's default value ? */
              it= new Item_null();
            }
9841

9842 9843 9844
            DBUG_ASSERT(lex->trg_chistics.action_time == TRG_ACTION_BEFORE &&
                        (lex->trg_chistics.event == TRG_EVENT_INSERT ||
                         lex->trg_chistics.event == TRG_EVENT_UPDATE));
9845
            if (!(trg_fld= new Item_trigger_field(Lex->current_context(),
9846
                                                  Item_trigger_field::NEW_ROW,
9847
                                                  $2.base_name.str,
9848
                                                  UPDATE_ACL, FALSE)) ||
9849 9850 9851 9852 9853
                !(sp_fld= new sp_instr_set_trigger_field(lex->sphead->
                          	                         instructions(),
                                	                 lex->spcont,
							 trg_fld,
                                        	         it, lex)))
9854
              YYABORT;
9855

9856 9857 9858 9859
            /*
              Let us add this item to list of all Item_trigger_field
              objects in trigger.
            */
9860 9861
            lex->trg_table_fields.link_in_list((byte *)trg_fld,
                                    (byte **)&trg_fld->next_trg_field);
9862

9863
            lex->sphead->add_instr(sp_fld);
9864 9865 9866 9867
          }
          else if ($2.var)
          { /* System variable */
            if ($1)
9868
              lex->option_type= $1;
9869 9870 9871 9872 9873 9874 9875
            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;
9876
            sp_variable_t *spv;
9877
            sp_instr_set *sp_set;
9878 9879 9880 9881 9882 9883 9884
            Item *it;
            if ($1)
            {
              yyerror(ER(ER_SYNTAX_ERROR));
              YYABORT;
            }

9885
            spv= ctx->find_variable(&$2.base_name);
9886 9887 9888 9889 9890 9891 9892

            if ($4)
              it= $4;
            else if (spv->dflt)
              it= spv->dflt;
            else
              it= new Item_null();
9893 9894 9895
            sp_set= new sp_instr_set(lex->sphead->instructions(), ctx,
                                     spv->offset, it, spv->type, lex, TRUE);
            lex->sphead->add_instr(sp_set);
9896 9897 9898 9899 9900
          }
        }
        | option_type TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
	{
	  LEX *lex=Lex;
9901
	  lex->option_type= $1;
9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913
	  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)));
	}
9914
	| '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
9915 9916
	  {
	    LEX *lex=Lex;
9917
	    lex->var_list.push_back(new set_var($3, $4.var, &$4.base_name, $6));
9918
	  }
9919
	| charset old_or_new_charset_name_or_default
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9920
	{
9921
	  THD *thd= YYTHD;
9922
	  LEX *lex= Lex;
9923
	  $2= $2 ? $2: global_system_variables.character_set_client;
9924
	  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
9925
	}
9926 9927 9928 9929 9930 9931 9932 9933
        | NAMES_SYM equal expr
	  {
	    LEX *lex= Lex;
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING names;

	    names.str= (char *)"names";
	    names.length= 5;
9934
	    if (spc && spc->find_variable(&names))
9935
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), names.str);
9936 9937 9938
            else
              yyerror(ER(ER_SYNTAX_ERROR));

9939 9940
	    YYABORT;
	  }
9941
	| NAMES_SYM charset_name_or_default opt_collate
9942
	{
9943
	  LEX *lex= Lex;
9944
	  $2= $2 ? $2 : global_system_variables.character_set_client;
9945 9946
	  $3= $3 ? $3 : $2;
	  if (!my_charset_same($2,$3))
9947
	  {
9948 9949
	    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
                     $3->name, $2->csname);
9950
	    YYABORT;
9951
	  }
9952
	  lex->var_list.push_back(new set_var_collation_client($3,$3,$3));
9953
	}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9954
	| PASSWORD equal text_or_password
9955
	  {
9956
	    THD *thd=YYTHD;
9957
	    LEX_USER *user;
9958 9959 9960 9961 9962 9963
	    LEX *lex= Lex;	    
            sp_pcontext *spc= lex->spcont;
	    LEX_STRING pw;

	    pw.str= (char *)"password";
	    pw.length= 8;
9964
	    if (spc && spc->find_variable(&pw))
9965 9966 9967 9968
	    {
              my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), pw.str);
	      YYABORT;
	    }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9969
	    if (!(user=(LEX_USER*) thd->alloc(sizeof(LEX_USER))))
9970
	      YYABORT;
9971
	    user->host=null_lex_str;
9972
	    user->user.str=thd->security_ctx->priv_user;
9973
	    thd->lex->var_list.push_back(new set_var_password(user, $3));
9974 9975
	  }
	| PASSWORD FOR_SYM user equal text_or_password
9976
	  {
9977 9978
	    Lex->var_list.push_back(new set_var_password($3,$5));
	  }
9979
	;
9980

9981 9982 9983
internal_variable_name:
	ident
	{
9984 9985
	  LEX *lex= Lex;
          sp_pcontext *spc= lex->spcont;
9986
	  sp_variable_t *spv;
9987 9988

	  /* We have to lookup here since local vars can shadow sysvars */
9989
	  if (!spc || !(spv = spc->find_variable(&$1)))
9990 9991 9992 9993 9994 9995
	  {
            /* Not an SP local variable */
	    sys_var *tmp=find_sys_var($1.str, $1.length);
	    if (!tmp)
	      YYABORT;
	    $$.var= tmp;
9996
	    $$.base_name= null_lex_str;
9997 9998 9999 10000
            /*
              If this is time_zone variable we should open time zone
              describing tables 
            */
10001 10002 10003
            if (tmp == &sys_time_zone &&
                lex->add_time_zone_tables_to_query_tables(YYTHD))
              YYABORT;
10004 10005 10006 10007 10008 10009 10010 10011
            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;
            }
10012 10013 10014 10015 10016 10017 10018
	  }
	  else
	  {
            /* An SP local variable */
	    $$.var= NULL;
	    $$.base_name= $1;
	  }
10019
	}
10020 10021
	| ident '.' ident
	  {
10022
            LEX *lex= Lex;
10023 10024
            if (check_reserved_words(&$1))
            {
10025
	      yyerror(ER(ER_SYNTAX_ERROR));
10026 10027
              YYABORT;
            }
10028 10029 10030 10031 10032 10033
            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')
              {
10034
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
10035 10036 10037 10038
                YYABORT;
              }
              if (lex->trg_chistics.event == TRG_EVENT_DELETE)
              {
10039 10040
                my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
                         "NEW", "on DELETE");
10041 10042 10043 10044
                YYABORT;
              }
              if (lex->trg_chistics.action_time == TRG_ACTION_AFTER)
              {
10045
                my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
10046 10047 10048
                YYABORT;
              }
              /* This special combination will denote field of NEW row */
10049
              $$.var= trg_new_row_fake_var;
10050 10051 10052 10053 10054 10055 10056 10057
              $$.base_name= $3;
            }
            else
            {
              sys_var *tmp=find_sys_var($3.str, $3.length);
              if (!tmp)
                YYABORT;
              if (!tmp->is_struct())
10058
                my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
10059 10060 10061
              $$.var= tmp;
              $$.base_name= $1;
            }
10062 10063 10064 10065 10066 10067 10068
	  }
	| DEFAULT '.' ident
	  {
	    sys_var *tmp=find_sys_var($3.str, $3.length);
	    if (!tmp)
	      YYABORT;
	    if (!tmp->is_struct())
10069
	      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), $3.str);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
10070 10071 10072
	    $$.var= tmp;
	    $$.base_name.str=    (char*) "default";
	    $$.base_name.length= 7;
10073
	  }
10074
        ;
10075 10076 10077 10078 10079 10080 10081

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
10083 10084 10085 10086
text_or_password:
	TEXT_STRING { $$=$1.str;}
	| PASSWORD '(' TEXT_STRING ')'
	  {
10087
	    $$= $3.length ? YYTHD->variables.old_passwords ?
10088 10089 10090 10091 10092 10093 10094 10095
	        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;
10096 10097
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10098

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

10100
set_expr_or_default:
10101 10102
	expr      { $$=$1; }
	| DEFAULT { $$=0; }
10103 10104
	| 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
10105
	| BINARY  { $$=new Item_string("binary", 6, system_charset_info); }
10106
	;
monty@tik.mysql.fi's avatar
monty@tik.mysql.fi committed
10107 10108


bk@work.mysql.com's avatar
bk@work.mysql.com committed
10109 10110 10111 10112 10113
/* Lock function */

lock:
	LOCK_SYM table_or_tables
	{
10114 10115 10116 10117
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
10118
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "LOCK");
10119 10120 10121
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_LOCK_TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10122
	}
10123 10124
	table_lock_list
	{}
10125
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10126 10127 10128

table_or_tables:
	TABLE_SYM
10129
	| TABLES;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10130 10131 10132

table_lock_list:
	table_lock
10133
	| table_lock_list ',' table_lock;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10134 10135 10136

table_lock:
	table_ident opt_table_alias lock_option
10137
	{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
10138
	  if (!Select->add_table_to_list(YYTHD, $1, $2, 0, (thr_lock_type) $3))
10139 10140
	   YYABORT;
	}
10141
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10142 10143 10144

lock_option:
	READ_SYM	{ $$=TL_READ_NO_INSERT; }
10145
	| WRITE_SYM     { $$=YYTHD->update_lock_default; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10146
	| LOW_PRIORITY WRITE_SYM { $$=TL_WRITE_LOW_PRIORITY; }
10147 10148
	| READ_SYM LOCAL_SYM { $$= TL_READ; }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10149 10150

unlock:
10151 10152 10153 10154 10155 10156
	UNLOCK_SYM
	{
	  LEX *lex= Lex;

	  if (lex->sphead)
	  {
10157
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "UNLOCK");
10158 10159 10160 10161 10162 10163
	    YYABORT;
	  }
	  lex->sql_command= SQLCOM_UNLOCK_TABLES;
	}
	table_or_tables
	{}
10164
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10165 10166


10167
/*
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10168
** Handler: direct access to ISAM functions
10169 10170 10171 10172 10173
*/

handler:
	HANDLER_SYM table_ident OPEN_SYM opt_table_alias
	{
10174
	  LEX *lex= Lex;
10175 10176 10177 10178 10179
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
10180
	  lex->sql_command = SQLCOM_HA_OPEN;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
10181
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, $4, 0))
10182 10183
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
10184
	| HANDLER_SYM table_ident_nodb CLOSE_SYM
10185
	{
10186
	  LEX *lex= Lex;
10187 10188 10189 10190 10191
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
10192
	  lex->sql_command = SQLCOM_HA_CLOSE;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
10193
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
10194 10195
	    YYABORT;
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
10196
	| HANDLER_SYM table_ident_nodb READ_SYM
10197
	{
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
10198
	  LEX *lex=Lex;
10199 10200 10201 10202 10203
	  if (lex->sphead)
	  {
	    my_error(ER_SP_BADSTATEMENT, MYF(0), "HANDLER");
	    YYABORT;
	  }
10204
          lex->expr_allows_subselect= FALSE;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
10205 10206
	  lex->sql_command = SQLCOM_HA_READ;
	  lex->ha_rkey_mode= HA_READ_KEY_EXACT;	/* Avoid purify warnings */
10207
	  lex->current_select->select_limit= new Item_int((int32) 1);
10208
	  lex->current_select->offset_limit= 0;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
10209
	  if (!lex->current_select->add_table_to_list(lex->thd, $2, 0, 0))
10210
	    YYABORT;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10211
        }
10212 10213 10214 10215
        handler_read_or_scan where_clause opt_limit_clause
        {
          Lex->expr_allows_subselect= TRUE;
        }
10216
        ;
10217

serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10218
handler_read_or_scan:
10219 10220
	handler_scan_function         { Lex->ident= null_lex_str; }
        | ident handler_rkey_function { Lex->ident= $1; }
10221
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10222 10223 10224

handler_scan_function:
	FIRST_SYM  { Lex->ha_read_mode = RFIRST; }
10225 10226
	| NEXT_SYM { Lex->ha_read_mode = RNEXT;  }
        ;
serg@serg.mysql.com's avatar
serg@serg.mysql.com committed
10227 10228 10229 10230 10231 10232

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;  }
10233 10234
	| handler_rkey_mode
	{
10235 10236
	  LEX *lex=Lex;
	  lex->ha_read_mode = RKEY;
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
10237
	  lex->ha_rkey_mode=$1;
10238
	  if (!(lex->insert_list = new List_item))
10239
	    YYABORT;
10240 10241
	} '(' values ')' { }
        ;
10242 10243

handler_rkey_mode:
monty@bitch.mysql.fi's avatar
monty@bitch.mysql.fi committed
10244 10245 10246 10247
	  EQ     { $$=HA_READ_KEY_EXACT;   }
	| GE     { $$=HA_READ_KEY_OR_NEXT; }
	| LE     { $$=HA_READ_KEY_OR_PREV; }
	| GT_SYM { $$=HA_READ_AFTER_KEY;   }
10248 10249
	| LT     { $$=HA_READ_BEFORE_KEY;  }
        ;
10250

bk@work.mysql.com's avatar
bk@work.mysql.com committed
10251 10252 10253
/* GRANT / REVOKE */

revoke:
10254
	REVOKE clear_privileges revoke_command
10255 10256 10257 10258
	{}
        ;

revoke_command:
10259
	grant_privileges ON opt_table grant_ident FROM grant_list
10260
	{
10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288
          LEX *lex= Lex;
	  lex->sql_command= SQLCOM_REVOKE;
          lex->type= 0;
        }
        |
        grant_privileges ON FUNCTION_SYM grant_ident FROM grant_list
        {
          LEX *lex= Lex;
          if (lex->columns.elements)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
          }
	  lex->sql_command= SQLCOM_REVOKE;
          lex->type= TYPE_ENUM_FUNCTION;
          
        }
	|
        grant_privileges ON PROCEDURE grant_ident FROM grant_list
        {
          LEX *lex= Lex;
          if (lex->columns.elements)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
          }
	  lex->sql_command= SQLCOM_REVOKE;
          lex->type= TYPE_ENUM_PROCEDURE;
10289
        }
10290
	|
10291
	ALL opt_privileges ',' GRANT OPTION FROM grant_list
10292 10293 10294
	{
	  Lex->sql_command = SQLCOM_REVOKE_ALL;
	}
10295
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10296 10297

grant:
10298 10299 10300 10301 10302 10303
	GRANT clear_privileges grant_command
	{}
        ;

grant_command:
	grant_privileges ON opt_table grant_ident TO_SYM grant_list
10304
	require_clause grant_options
10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336
	{
          LEX *lex= Lex;
          lex->sql_command= SQLCOM_GRANT;
          lex->type= 0;
        }
        |
	grant_privileges ON FUNCTION_SYM grant_ident TO_SYM grant_list
	require_clause grant_options
	{
          LEX *lex= Lex;
          if (lex->columns.elements)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
          }
          lex->sql_command= SQLCOM_GRANT;
          lex->type= TYPE_ENUM_FUNCTION;
        }
        |
	grant_privileges ON PROCEDURE grant_ident TO_SYM grant_list
	require_clause grant_options
	{
          LEX *lex= Lex;
          if (lex->columns.elements)
          {
            yyerror(ER(ER_SYNTAX_ERROR));
	    YYABORT;
          }
          lex->sql_command= SQLCOM_GRANT;
          lex->type= TYPE_ENUM_PROCEDURE;
        }
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10337

10338 10339 10340 10341
opt_table:
	/* Empty */
	| TABLE_SYM ;
        
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10342
grant_privileges:
10343 10344 10345 10346 10347 10348
	object_privilege_list { }
	| ALL opt_privileges
        { 
          Lex->all_privileges= 1; 
          Lex->grant= GLOBAL_ACLS;
        }
10349
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10350

10351 10352 10353 10354 10355
opt_privileges:
	/* empty */
	| PRIVILEGES
	;

10356 10357 10358
object_privilege_list:
	object_privilege
	| object_privilege_list ',' object_privilege;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10359

10360
object_privilege:
serg@serg.mylan's avatar
serg@serg.mylan committed
10361
	SELECT_SYM	{ Lex->which_columns = SELECT_ACL;} opt_column_list {}
10362 10363 10364
	| 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 {}
10365 10366
	| DELETE_SYM	{ Lex->grant |= DELETE_ACL;}
	| USAGE		{}
10367
	| INDEX_SYM	{ Lex->grant |= INDEX_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10368 10369 10370
	| ALTER		{ Lex->grant |= ALTER_ACL;}
	| CREATE	{ Lex->grant |= CREATE_ACL;}
	| DROP		{ Lex->grant |= DROP_ACL;}
10371
	| EXECUTE_SYM	{ Lex->grant |= EXECUTE_ACL;}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10372 10373 10374 10375
	| RELOAD	{ Lex->grant |= RELOAD_ACL;}
	| SHUTDOWN	{ Lex->grant |= SHUTDOWN_ACL;}
	| PROCESS	{ Lex->grant |= PROCESS_ACL;}
	| FILE_SYM	{ Lex->grant |= FILE_ACL;}
10376 10377 10378 10379 10380
	| 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; }
10381 10382 10383 10384
	| 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; }
10385 10386
	| CREATE ROUTINE_SYM { Lex->grant |= CREATE_PROC_ACL; }
	| ALTER ROUTINE_SYM { Lex->grant |= ALTER_PROC_ACL; }
10387
	| CREATE USER { Lex->grant |= CREATE_USER_ACL; }
10388
        | EVENT_SYM { Lex->grant |= EVENT_ACL;}
10389
        | TRIGGER_SYM { Lex->grant |= TRIGGER_ACL; }
10390
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10391

10392

10393 10394
opt_and:
	/* empty */	{}
10395
	| AND_SYM	{}
10396 10397 10398 10399 10400 10401 10402 10403 10404
	;

require_list:
	 require_list_element opt_and require_list
	 | require_list_element
	 ;

require_list_element:
	SUBJECT_SYM TEXT_STRING
10405 10406 10407 10408
	{
	  LEX *lex=Lex;
	  if (lex->x509_subject)
	  {
10409
	    my_error(ER_DUP_ARGUMENT, MYF(0), "SUBJECT");
10410 10411 10412 10413 10414 10415 10416 10417 10418
	    YYABORT;
	  }
	  lex->x509_subject=$2.str;
	}
	| ISSUER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->x509_issuer)
	  {
10419
	    my_error(ER_DUP_ARGUMENT, MYF(0), "ISSUER");
10420 10421 10422 10423 10424 10425 10426 10427 10428
	    YYABORT;
	  }
	  lex->x509_issuer=$2.str;
	}
	| CIPHER_SYM TEXT_STRING
	{
	  LEX *lex=Lex;
	  if (lex->ssl_cipher)
	  {
10429
	    my_error(ER_DUP_ARGUMENT, MYF(0), "CIPHER");
10430 10431 10432 10433 10434
	    YYABORT;
	  }
	  lex->ssl_cipher=$2.str;
	}
	;
10435

10436
grant_ident:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10437 10438
	'*'
	  {
10439
	    LEX *lex= Lex;
10440
            THD *thd= lex->thd;
10441 10442
            uint dummy;
            if (thd->copy_db_to(&lex->current_select->db, &dummy))
10443
              YYABORT;
10444
	    if (lex->grant == GLOBAL_ACLS)
10445 10446
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10447
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10448 10449
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
10450
	      YYABORT;
10451
	    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10452 10453 10454
	  }
	| ident '.' '*'
	  {
10455
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10456
	    lex->current_select->db = $1.str;
10457
	    if (lex->grant == GLOBAL_ACLS)
10458 10459
	      lex->grant = DB_ACLS & ~GRANT_ACL;
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10460
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10461 10462
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10463 10464 10465 10466 10467
	      YYABORT;
	    }
	  }
	| '*' '.' '*'
	  {
10468
	    LEX *lex= Lex;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10469
	    lex->current_select->db = NULL;
10470 10471
	    if (lex->grant == GLOBAL_ACLS)
	      lex->grant= GLOBAL_ACLS & ~GRANT_ACL;
10472
	    else if (lex->columns.elements)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10473
	    {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10474 10475
	      my_message(ER_ILLEGAL_GRANT_FOR_TABLE,
                         ER(ER_ILLEGAL_GRANT_FOR_TABLE), MYF(0));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10476 10477 10478 10479 10480
	      YYABORT;
	    }
	  }
	| table_ident
	  {
10481
	    LEX *lex=Lex;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
10482
	    if (!lex->current_select->add_table_to_list(lex->thd, $1,NULL,0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10483
	      YYABORT;
10484
	    if (lex->grant == GLOBAL_ACLS)
10485
	      lex->grant =  TABLE_ACLS & ~GRANT_ACL;
10486 10487
	  }
          ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10488 10489 10490


user_list:
10491 10492 10493 10494 10495 10496 10497 10498 10499 10500
	user  { if (Lex->users_list.push_back($1)) YYABORT;}
	| user_list ',' user
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;


grant_list:
10501
	grant_user  { if (Lex->users_list.push_back($1)) YYABORT;}
10502
	| grant_list ',' grant_user
10503 10504 10505 10506 10507
	  {
	    if (Lex->users_list.push_back($3))
	      YYABORT;
	  }
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10508 10509 10510 10511 10512 10513 10514 10515


grant_user:
	user IDENTIFIED_SYM BY TEXT_STRING
	{
	   $$=$1; $1->password=$4;
	   if ($4.length)
	   {
10516
             if (YYTHD->variables.old_passwords)
10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533
             {
               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
10534 10535 10536
	  }
	}
	| user IDENTIFIED_SYM BY PASSWORD TEXT_STRING
10537
	  { $$= $1; $1->password= $5; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10538
	| user
10539
	  { $$= $1; $1->password= null_lex_str; }
10540
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10541 10542 10543


opt_column_list:
10544 10545 10546 10547 10548
	/* empty */
	{
	  LEX *lex=Lex;
	  lex->grant |= lex->which_columns;
	}
10549
	| '(' column_list ')';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10550 10551 10552

column_list:
	column_list ',' column_list_id
10553
	| column_list_id;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10554 10555 10556 10557

column_list_id:
	ident
	{
10558
	  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
10559 10560
	  List_iterator <LEX_COLUMN> iter(Lex->columns);
	  class LEX_COLUMN *point;
10561
	  LEX *lex=Lex;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10562 10563
	  while ((point=iter++))
	  {
10564 10565
	    if (!my_strcasecmp(system_charset_info,
                               point->column.ptr(), new_str->ptr()))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10566 10567
		break;
	  }
10568
	  lex->grant_tot_col|= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10569
	  if (point)
10570
	    point->rights |= lex->which_columns;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10571
	  else
10572
	    lex->columns.push_back(new LEX_COLUMN (*new_str,lex->which_columns));
10573 10574
	}
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10575

tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
10576 10577

require_clause: /* empty */
10578
        | REQUIRE_SYM require_list
10579 10580 10581
          {
            Lex->ssl_type=SSL_TYPE_SPECIFIED;
          }
10582
        | REQUIRE_SYM SSL_SYM
10583 10584 10585
          {
            Lex->ssl_type=SSL_TYPE_ANY;
          }
10586
        | REQUIRE_SYM X509_SYM
10587 10588 10589 10590 10591 10592 10593
          {
            Lex->ssl_type=SSL_TYPE_X509;
          }
	| REQUIRE_SYM NONE_SYM
	  {
	    Lex->ssl_type=SSL_TYPE_NONE;
	  }
10594
          ;
tonu@x153.internalnet's avatar
tonu@x153.internalnet committed
10595

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10596
grant_options:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10597
	/* empty */ {}
10598
	| WITH grant_option_list;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10599

monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10600 10601
grant_option_list:
	grant_option_list grant_option {}
10602 10603
	| grant_option {}
        ;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10604 10605 10606

grant_option:
	GRANT OPTION { Lex->grant |= GRANT_ACL;}
serg@serg.mylan's avatar
serg@serg.mylan committed
10607
        | MAX_QUERIES_PER_HOUR ulong_num
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10608
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
10609 10610 10611
	  LEX *lex=Lex;
	  lex->mqh.questions=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
10612
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
10613
        | MAX_UPDATES_PER_HOUR ulong_num
10614
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
10615 10616 10617
	  LEX *lex=Lex;
	  lex->mqh.updates=$2;
	  lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
10618
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
10619
        | MAX_CONNECTIONS_PER_HOUR ulong_num
10620
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
10621 10622 10623
	  LEX *lex=Lex;
	  lex->mqh.conn_per_hour= $2;
	  lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
10624
	}
serg@serg.mylan's avatar
serg@serg.mylan committed
10625
        | MAX_USER_CONNECTIONS_SYM ulong_num
10626
        {
serg@serg.mylan's avatar
serg@serg.mylan committed
10627 10628 10629
	  LEX *lex=Lex;
          lex->mqh.user_conn= $2;
          lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
10630 10631
	}
        ;
10632

bk@work.mysql.com's avatar
bk@work.mysql.com committed
10633
begin:
serg@serg.mylan's avatar
serg@serg.mylan committed
10634 10635 10636 10637 10638 10639 10640
	BEGIN_SYM  
        {
	  LEX *lex=Lex;
          lex->sql_command = SQLCOM_BEGIN;
          lex->start_transaction_opt= 0;
        }
        opt_work {}
10641
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10642 10643 10644

opt_work:
	/* empty */ {}
serg@serg.mylan's avatar
serg@serg.mylan committed
10645
	| WORK_SYM  {}
10646
        ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10647

10648
opt_chain:
serg@serg.mylan's avatar
serg@serg.mylan committed
10649
	/* empty */ { $$= (YYTHD->variables.completion_type == 1); }
10650 10651 10652 10653 10654
	| AND_SYM NO_SYM CHAIN_SYM	{ $$=0; }
	| AND_SYM CHAIN_SYM		{ $$=1; }
	;

opt_release:
serg@serg.mylan's avatar
serg@serg.mylan committed
10655
	/* empty */ { $$= (YYTHD->variables.completion_type == 2); }
10656 10657 10658 10659 10660 10661 10662 10663 10664
	| RELEASE_SYM 			{ $$=1; }
	| NO_SYM RELEASE_SYM 		{ $$=0; }
	;
	
opt_savepoint:
	/* empty */	{}
	| SAVEPOINT_SYM {}
	;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
10665
commit:
serg@serg.mylan's avatar
serg@serg.mylan committed
10666
	COMMIT_SYM opt_work opt_chain opt_release
10667
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10668 10669 10670 10671
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_COMMIT;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
10672 10673
	}
	;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
10674 10675

rollback:
serg@serg.mylan's avatar
serg@serg.mylan committed
10676
	ROLLBACK_SYM opt_work opt_chain opt_release
10677
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10678 10679 10680 10681
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK;
	  lex->tx_chain= $3; 
	  lex->tx_release= $4;
10682
	}
10683 10684
	| ROLLBACK_SYM opt_work
	  TO_SYM opt_savepoint ident
10685
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10686 10687 10688
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_ROLLBACK_TO_SAVEPOINT;
	  lex->ident= $5;
10689 10690 10691
	}
	;

10692 10693 10694
savepoint:
	SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10695 10696 10697
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_SAVEPOINT;
	  lex->ident= $2;
10698 10699 10700 10701 10702 10703
	}
	;

release:
	RELEASE_SYM SAVEPOINT_SYM ident
	{
serg@serg.mylan's avatar
serg@serg.mylan committed
10704 10705 10706
	  LEX *lex=Lex;
	  lex->sql_command= SQLCOM_RELEASE_SAVEPOINT;
	  lex->ident= $3;
10707 10708
	}
	;
serg@serg.mylan's avatar
serg@serg.mylan committed
10709
  
10710
/*
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10711
   UNIONS : glue selects together
10712 10713 10714
*/


10715
union_clause:
10716
	/* empty */ {}
10717 10718
	| union_list
	;
10719 10720

union_list:
10721
	UNION_SYM union_option
10722 10723 10724 10725 10726
	{
	  LEX *lex=Lex;
	  if (lex->exchange)
	  {
	    /* Only the last SELECT can have  INTO...... */
10727
	    my_error(ER_WRONG_USAGE, MYF(0), "UNION", "INTO");
10728
	    YYABORT;
10729
	  }
10730
	  if (lex->current_select->linkage == GLOBAL_OPTIONS_TYPE)
10731
	  {
10732
            yyerror(ER(ER_SYNTAX_ERROR));
10733 10734
	    YYABORT;
	  }
10735 10736
          /* This counter shouldn't be incremented for UNION parts */
          Lex->nest_level--;
10737
	  if (mysql_new_select(lex, 0))
10738
	    YYABORT;
10739
          mysql_init_select(lex);
10740
	  lex->current_select->linkage=UNION_TYPE;
10741 10742 10743
          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
10744
	}
10745 10746
	select_init
        {
10747 10748 10749 10750
          /*
	    Remove from the name resolution context stack the context of the
            last select in the union.
	  */
10751 10752
          Lex->pop_context();
        }
10753
	;
10754 10755

union_opt:
10756 10757 10758
	/* Empty */ { $$= 0; }
	| union_list { $$= 1; }
	| union_order_or_limit { $$= 1; }
10759
	;
10760

10761
union_order_or_limit:
10762
	  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10763
	    THD *thd= YYTHD;
10764
	    LEX *lex= thd->lex;
10765
	    DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10766
	    SELECT_LEX *sel= lex->current_select;
10767
	    SELECT_LEX_UNIT *unit= sel->master_unit();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10768
	    SELECT_LEX *fake= unit->fake_select_lex;
10769 10770 10771 10772 10773 10774
	    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
10775
	    thd->where= "global ORDER clause";
10776
	  }
10777
	order_or_limit
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10778 10779
          {
	    THD *thd= YYTHD;
10780
	    thd->lex->current_select->no_table_names_allowed= 0;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10781 10782
	    thd->where= "";
          }
10783 10784 10785
	;

order_or_limit:
10786 10787
	order_clause opt_limit_clause_init
	| limit_clause
10788
	;
10789 10790

union_option:
10791 10792 10793 10794
	/* empty */ { $$=1; }
	| DISTINCT  { $$=1; }
	| ALL       { $$=0; }
        ;
10795

10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819
subselect:
        SELECT_SYM subselect_start subselect_init subselect_end
        {
          $$= $3;
        }
        | '(' subselect_start subselect ')'
          {
            LEX *lex= Lex;
	    THD *thd= YYTHD;
            /*
              note that a local variable can't be used for
              $3 as it's used in local variable construction
              and some compilers can't guarnatee the order
              in which the local variables are initialized.
            */
            List_iterator<Item> it($3->item_list);
            Item *item;
            /*
              we must fill the items list for the "derived table".
            */
            while ((item= it++))
              add_item_to_list(thd, item);
          }
          union_clause subselect_end { $$= $3; };
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10820

10821
subselect_init:
10822
  select_init2
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10823
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10824
    $$= Lex->current_select->master_unit()->first_select();
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
10825 10826
  };

10827
subselect_start:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10828
	{
10829
	  LEX *lex=Lex;
10830
          if (!lex->expr_allows_subselect)
10831 10832
	  {
            yyerror(ER(ER_SYNTAX_ERROR));
10833 10834
	    YYABORT;
	  }
10835 10836 10837 10838 10839 10840 10841
          /* 
            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
10842 10843 10844
	  if (mysql_new_select(Lex, 1))
	    YYABORT;
	};
10845 10846

subselect_end:
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
10847 10848
	{
	  LEX *lex=Lex;
10849
          lex->pop_context();
10850
          SELECT_LEX *child= lex->current_select;
10851
	  lex->current_select = lex->current_select->return_after_parsing();
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
10852
          lex->nest_level--;
10853
          lex->current_select->n_child_sum_items += child->n_sum_items;
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
10854
	};
serg@serg.mylan's avatar
serg@serg.mylan committed
10855

10856 10857 10858 10859 10860 10861
/**************************************************************************

 CREATE VIEW | TRIGGER | PROCEDURE statements.

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

10862 10863
view_or_trigger_or_sp_or_event:
	definer view_or_trigger_or_sp_or_event_tail
10864 10865 10866 10867 10868
	{}
	| view_replace_or_algorithm definer view_tail
	{}
	;

10869
view_or_trigger_or_sp_or_event_tail:
10870 10871 10872 10873 10874 10875
	view_tail
	{}
	| trigger_tail
	{}
	| sp_tail
	{}
10876 10877
	| event_tail
	{}
10878 10879 10880 10881 10882 10883 10884 10885
	;

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

 DEFINER clause support.

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

10886
definer:
10887
	/* empty */
10888
	{
10889 10890 10891 10892 10893 10894 10895 10896
          /*
            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;
10897
	}
10898
	| DEFINER_SYM EQ user
10899
	{
10900
	  YYTHD->lex->definer= get_current_user(YYTHD, $3);
10901 10902 10903 10904 10905
	}
	;

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

10906
 CREATE VIEW statement parts.
10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941

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

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 */
10942
	{ Lex->create_view_suid= VIEW_SUID_DEFAULT; }
10943
	| SQL_SYM SECURITY_SYM DEFINER_SYM
10944
	{ Lex->create_view_suid= VIEW_SUID_DEFINER; }
10945
	| SQL_SYM SECURITY_SYM INVOKER_SYM
10946
	{ Lex->create_view_suid= VIEW_SUID_INVOKER; }
10947 10948 10949 10950 10951 10952 10953 10954 10955
	;

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 */
10956
	  if (!lex->select_lex.add_table_to_list(thd, $3, NULL, TL_OPTION_UPDATING))
10957 10958 10959 10960 10961 10962 10963 10964 10965
	    YYABORT;
	}
	view_list_opt AS view_select view_check_option
	{}
	;

view_list_opt:
	/* empty */
	{}
10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981
	| '(' 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)));
	  }
	;

10982
view_select:
10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000
        {
          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:
11001 11002
	SELECT_SYM remember_name select_init2
	{
11003 11004 11005 11006 11007 11008
          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;
11009 11010 11011
	}
	| '(' remember_name select_paren ')' union_opt
	{
11012 11013 11014 11015 11016 11017
          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;
11018
	}
11019 11020
	;

11021
view_check_option:
11022
	/* empty */
11023 11024 11025 11026 11027 11028 11029
	{ 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; }
11030
	;
11031

11032 11033 11034 11035 11036 11037 11038
/**************************************************************************

 CREATE TRIGGER statement parts.

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

trigger_tail:
11039
	TRIGGER_SYM remember_name sp_name trg_action_time trg_event
11040
	ON remember_name table_ident FOR_SYM remember_name EACH_SYM ROW_SYM
11041 11042 11043
	{
	  LEX *lex= Lex;
	  sp_head *sp;
11044

11045
	  if (lex->sphead)
11046
	  {
11047 11048
	    my_error(ER_SP_NO_RECURSIVE_CREATE, MYF(0), "TRIGGER");
	    YYABORT;
11049
	  }
11050

11051 11052 11053 11054
	  if (!(sp= new sp_head()))
	    YYABORT;
	  sp->reset_thd_mem_root(YYTHD);
	  sp->init(lex);
11055
          sp->init_sp_name(YYTHD, $3);
11056
	  lex->stmt_definition_begin= $2;
11057
          lex->ident.str= $7;
11058
          lex->ident.length= $10 - $7;
11059

11060 11061 11062 11063 11064 11065 11066 11067
	  sp->m_type= TYPE_ENUM_TRIGGER;
	  lex->sphead= sp;
	  lex->spname= $3;
	  /*
	    We have to turn of CLIENT_MULTI_QUERIES while parsing a
	    stored procedure, otherwise yylex will chop it into pieces
	    at each ';'.
	  */
andrey@lmy004's avatar
andrey@lmy004 committed
11068
	  $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
11069
	  YYTHD->client_capabilities &= ~CLIENT_MULTI_QUERIES;
11070

11071 11072 11073
	  bzero((char *)&lex->sp_chistics, sizeof(st_sp_chistics));
	  lex->sphead->m_chistics= &lex->sp_chistics;
	  lex->sphead->m_body_begin= lex->ptr;
11074 11075
          while (my_isspace(system_charset_info, lex->sphead->m_body_begin[0]))
            ++lex->sphead->m_body_begin;
11076 11077 11078 11079 11080
	}
	sp_proc_stmt
	{
	  LEX *lex= Lex;
	  sp_head *sp= lex->sphead;
11081

11082
	  lex->sql_command= SQLCOM_CREATE_TRIGGER;
11083
	  sp->init_strings(YYTHD, lex);
11084
	  /* Restore flag if it was cleared above */
andrey@lmy004's avatar
andrey@lmy004 committed
11085

11086
	  YYTHD->client_capabilities |= $<ulong_num>13;
11087
	  sp->restore_thd_mem_root(YYTHD);
11088

11089 11090
	  if (sp->is_not_allowed_in_function("trigger"))
	      YYABORT;
11091

11092 11093 11094 11095 11096
	  /*
	    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.
	  */
11097
	  if (!lex->select_lex.add_table_to_list(YYTHD, $8,
11098 11099
	                                         (LEX_STRING*) 0,
	                                         TL_OPTION_UPDATING,
11100
                                                 TL_IGNORE))
11101 11102
	    YYABORT;
	}
11103 11104
	;

11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130
/**************************************************************************

 CREATE FUNCTION | PROCEDURE statements parts.

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

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

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

11132
	  lex->stmt_definition_begin= $2;
11133

11134 11135 11136 11137
	  /* Order is important here: new - reset - init */
	  sp= new sp_head();
	  sp->reset_thd_mem_root(YYTHD);
	  sp->init(lex);
11138
          sp->init_sp_name(YYTHD, $3);
11139 11140 11141 11142 11143 11144 11145 11146

	  sp->m_type= TYPE_ENUM_PROCEDURE;
	  lex->sphead= sp;
	  /*
	   * We have to turn of CLIENT_MULTI_QUERIES while parsing a
	   * stored procedure, otherwise yylex will chop it into pieces
	   * at each ';'.
	   */
11147
          $<ulong_num>$= YYTHD->client_capabilities & CLIENT_MULTI_QUERIES;
11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175
	  YYTHD->client_capabilities &= (~CLIENT_MULTI_QUERIES);
	}
        '('
	{
	  LEX *lex= Lex;

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

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

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

11176
	  sp->init_strings(YYTHD, lex);
11177
	  lex->sql_command= SQLCOM_CREATE_PROCEDURE;
11178 11179 11180 11181 11182 11183
          /*
            Restore flag if it was cleared above
            Be careful with counting. the block where we save the value
            is $4.
          */
          YYTHD->client_capabilities |= $<ulong_num>4;
11184 11185 11186 11187
	  sp->restore_thd_mem_root(YYTHD);
	}
	;

11188
/*************************************************************************/
11189

11190 11191
xa: XA_SYM begin_or_start xid opt_join_or_resume
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
11192
        Lex->sql_command = SQLCOM_XA_START;
11193
      }
11194
    | XA_SYM END xid opt_suspend
11195
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
11196
        Lex->sql_command = SQLCOM_XA_END;
11197 11198 11199
      }
    | XA_SYM PREPARE_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
11200
        Lex->sql_command = SQLCOM_XA_PREPARE;
11201 11202 11203
      }
    | XA_SYM COMMIT_SYM xid opt_one_phase
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
11204
        Lex->sql_command = SQLCOM_XA_COMMIT;
11205 11206 11207
      }
    | XA_SYM ROLLBACK_SYM xid
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
11208
        Lex->sql_command = SQLCOM_XA_ROLLBACK;
11209 11210 11211
      }
    | XA_SYM RECOVER_SYM
      {
serg@serg.mylan's avatar
serg@serg.mylan committed
11212
        Lex->sql_command = SQLCOM_XA_RECOVER;
11213 11214 11215
      }
    ;

serg@serg.mylan's avatar
serg@serg.mylan committed
11216 11217
xid: text_string
     {
serg@serg.mylan's avatar
serg@serg.mylan committed
11218
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
11219 11220 11221 11222 11223 11224
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set(1L, $1->ptr(), $1->length(), 0, 0);
     }
     | text_string ',' text_string
     {
serg@serg.mylan's avatar
serg@serg.mylan committed
11225
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
11226 11227 11228 11229 11230 11231
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set(1L, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     | text_string ',' text_string ',' ulong_num
     {
serg@serg.mylan's avatar
serg@serg.mylan committed
11232
       YYERROR_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
serg@serg.mylan's avatar
serg@serg.mylan committed
11233 11234 11235 11236 11237
       if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
         YYABORT;
       Lex->xid->set($5, $1->ptr(), $1->length(), $3->ptr(), $3->length());
     }
     ;
11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253

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

11254
opt_suspend:
11255 11256
    /* nothing */           { Lex->xa_opt=XA_NONE;        }
    | SUSPEND_SYM           { Lex->xa_opt=XA_SUSPEND;     }
11257 11258 11259 11260 11261
      opt_migrate
    ;

opt_migrate:
    /* nothing */           { }
11262 11263 11264
    | FOR_SYM MIGRATE_SYM   { Lex->xa_opt=XA_FOR_MIGRATE; }
    ;

11265
install:
11266
  INSTALL_SYM PLUGIN_SYM ident SONAME_SYM TEXT_STRING_sys
11267 11268 11269 11270 11271 11272
  {
    LEX *lex= Lex;
    lex->sql_command= SQLCOM_INSTALL_PLUGIN;
    lex->comment= $3;
    lex->ident= $5;
  };
11273

11274
uninstall:
11275
  UNINSTALL_SYM PLUGIN_SYM ident
11276 11277 11278 11279 11280
  {
    LEX *lex= Lex;
    lex->sql_command= SQLCOM_UNINSTALL_PLUGIN;
    lex->comment= $3;
  };