~stewart/drizzle/embedded-innodb-create-select-transaction-arrgh

« back to all changes in this revision

Viewing changes to sql/sql_lex.h

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2006 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/**
 
17
  @defgroup Semantic_Analysis Semantic Analysis
 
18
*/
 
19
 
 
20
/* YACC and LEX Definitions */
 
21
 
 
22
/* These may not be declared yet */
 
23
class Table_ident;
 
24
class sql_exchange;
 
25
class LEX_COLUMN;
 
26
class st_alter_tablespace;
 
27
 
 
28
#ifdef MYSQL_SERVER
 
29
/*
 
30
  The following hack is needed because mysql_yacc.cc does not define
 
31
  YYSTYPE before including this file
 
32
*/
 
33
 
 
34
#include "set_var.h"
 
35
 
 
36
#ifdef MYSQL_YACC
 
37
#define LEX_YYSTYPE void *
 
38
#else
 
39
#if MYSQL_LEX
 
40
#include "lex_symbol.h"
 
41
#include "sql_yacc.h"
 
42
#define LEX_YYSTYPE YYSTYPE *
 
43
#else
 
44
#define LEX_YYSTYPE void *
 
45
#endif
 
46
#endif
 
47
#endif
 
48
 
 
49
/*
 
50
  When a command is added here, be sure it's also added in mysqld.cc
 
51
  in "struct show_var_st status_vars[]= {" ...
 
52
 
 
53
  If the command returns a result set or is not allowed in stored
 
54
  functions or triggers, please also make sure that
 
55
  sp_get_flags_for_command (sp_head.cc) returns proper flags for the
 
56
  added SQLCOM_.
 
57
*/
 
58
 
 
59
enum enum_sql_command {
 
60
  SQLCOM_SELECT, SQLCOM_CREATE_TABLE, SQLCOM_CREATE_INDEX, SQLCOM_ALTER_TABLE,
 
61
  SQLCOM_UPDATE, SQLCOM_INSERT, SQLCOM_INSERT_SELECT,
 
62
  SQLCOM_DELETE, SQLCOM_TRUNCATE, SQLCOM_DROP_TABLE, SQLCOM_DROP_INDEX,
 
63
  SQLCOM_SHOW_DATABASES, SQLCOM_SHOW_TABLES, SQLCOM_SHOW_FIELDS,
 
64
  SQLCOM_SHOW_KEYS, SQLCOM_SHOW_VARIABLES, SQLCOM_SHOW_STATUS,
 
65
  SQLCOM_SHOW_ENGINE_LOGS, SQLCOM_SHOW_ENGINE_STATUS, SQLCOM_SHOW_ENGINE_MUTEX,
 
66
  SQLCOM_SHOW_PROCESSLIST, SQLCOM_SHOW_MASTER_STAT, SQLCOM_SHOW_SLAVE_STAT,
 
67
  SQLCOM_SHOW_CREATE, SQLCOM_SHOW_CHARSETS,
 
68
  SQLCOM_SHOW_COLLATIONS, SQLCOM_SHOW_CREATE_DB, SQLCOM_SHOW_TABLE_STATUS,
 
69
  SQLCOM_LOAD,SQLCOM_SET_OPTION,SQLCOM_LOCK_TABLES,SQLCOM_UNLOCK_TABLES,
 
70
  SQLCOM_CHANGE_DB, SQLCOM_CREATE_DB, SQLCOM_DROP_DB, SQLCOM_ALTER_DB,
 
71
  SQLCOM_REPAIR, SQLCOM_REPLACE, SQLCOM_REPLACE_SELECT,
 
72
  SQLCOM_CREATE_FUNCTION, SQLCOM_DROP_FUNCTION,
 
73
  SQLCOM_OPTIMIZE, SQLCOM_CHECK,
 
74
  SQLCOM_ASSIGN_TO_KEYCACHE, SQLCOM_PRELOAD_KEYS,
 
75
  SQLCOM_FLUSH, SQLCOM_KILL, SQLCOM_ANALYZE,
 
76
  SQLCOM_ROLLBACK, SQLCOM_ROLLBACK_TO_SAVEPOINT,
 
77
  SQLCOM_COMMIT, SQLCOM_SAVEPOINT, SQLCOM_RELEASE_SAVEPOINT,
 
78
  SQLCOM_SLAVE_START, SQLCOM_SLAVE_STOP,
 
79
  SQLCOM_BEGIN, SQLCOM_CHANGE_MASTER,
 
80
  SQLCOM_RENAME_TABLE,  
 
81
  SQLCOM_RESET, SQLCOM_PURGE, SQLCOM_PURGE_BEFORE, SQLCOM_SHOW_BINLOGS,
 
82
  SQLCOM_SHOW_OPEN_TABLES,
 
83
  SQLCOM_SHOW_SLAVE_HOSTS, SQLCOM_DELETE_MULTI, SQLCOM_UPDATE_MULTI,
 
84
  SQLCOM_SHOW_BINLOG_EVENTS, SQLCOM_SHOW_NEW_MASTER,
 
85
  SQLCOM_SHOW_WARNS, SQLCOM_EMPTY_QUERY, SQLCOM_SHOW_ERRORS,
 
86
  SQLCOM_SHOW_STORAGE_ENGINES,
 
87
  SQLCOM_CHECKSUM,
 
88
  SQLCOM_PREPARE, SQLCOM_EXECUTE, SQLCOM_DEALLOCATE_PREPARE,
 
89
  SQLCOM_XA_START, SQLCOM_XA_END, SQLCOM_XA_PREPARE,
 
90
  SQLCOM_XA_COMMIT, SQLCOM_XA_ROLLBACK, SQLCOM_XA_RECOVER,
 
91
  SQLCOM_ALTER_TABLESPACE,
 
92
  SQLCOM_SHOW_AUTHORS, SQLCOM_BINLOG_BASE64_EVENT,
 
93
  SQLCOM_SHOW_PLUGINS,
 
94
  SQLCOM_SHOW_CONTRIBUTORS,
 
95
  SQLCOM_ALTER_DB_UPGRADE,
 
96
  SQLCOM_SHOW_ARCHIVE,
 
97
 
 
98
  /*
 
99
    When a command is added here, be sure it's also added in mysqld.cc
 
100
    in "struct show_var_st status_vars[]= {" ...
 
101
  */
 
102
  /* This should be the last !!! */
 
103
  SQLCOM_END
 
104
};
 
105
 
 
106
// describe/explain types
 
107
#define DESCRIBE_NORMAL         1
 
108
#define DESCRIBE_EXTENDED       2
 
109
 
 
110
#ifdef MYSQL_SERVER
 
111
 
 
112
enum enum_sp_data_access
 
113
{
 
114
  SP_DEFAULT_ACCESS= 0,
 
115
  SP_CONTAINS_SQL,
 
116
  SP_NO_SQL,
 
117
  SP_READS_SQL_DATA,
 
118
  SP_MODIFIES_SQL_DATA
 
119
};
 
120
 
 
121
const LEX_STRING sp_data_access_name[]=
 
122
{
 
123
  { C_STRING_WITH_LEN("") },
 
124
  { C_STRING_WITH_LEN("CONTAINS SQL") },
 
125
  { C_STRING_WITH_LEN("NO SQL") },
 
126
  { C_STRING_WITH_LEN("READS SQL DATA") },
 
127
  { C_STRING_WITH_LEN("MODIFIES SQL DATA") }
 
128
};
 
129
 
 
130
#define DERIVED_NONE    0
 
131
#define DERIVED_SUBQUERY        1
 
132
 
 
133
enum enum_drop_mode
 
134
{
 
135
  DROP_DEFAULT, // mode is not specified
 
136
  DROP_CASCADE, // CASCADE option
 
137
  DROP_RESTRICT // RESTRICT option
 
138
};
 
139
 
 
140
typedef List<Item> List_item;
 
141
 
 
142
/* SERVERS CACHE CHANGES */
 
143
typedef struct st_lex_server_options
 
144
{
 
145
  long port;
 
146
  uint server_name_length;
 
147
  char *server_name, *host, *db, *username, *password, *scheme, *socket, *owner;
 
148
} LEX_SERVER_OPTIONS;
 
149
 
 
150
typedef struct st_lex_master_info
 
151
{
 
152
  char *host, *user, *password, *log_file_name;
 
153
  uint port, connect_retry;
 
154
  float heartbeat_period;
 
155
  ulonglong pos;
 
156
  ulong server_id;
 
157
  /*
 
158
    Enum is used for making it possible to detect if the user
 
159
    changed variable or if it should be left at old value
 
160
   */
 
161
  enum {LEX_MI_UNCHANGED, LEX_MI_DISABLE, LEX_MI_ENABLE}
 
162
    ssl, ssl_verify_server_cert, heartbeat_opt;
 
163
  char *ssl_key, *ssl_cert, *ssl_ca, *ssl_capath, *ssl_cipher;
 
164
  char *relay_log_name;
 
165
  ulong relay_log_pos;
 
166
} LEX_MASTER_INFO;
 
167
 
 
168
 
 
169
enum sub_select_type
 
170
{
 
171
  UNSPECIFIED_TYPE,UNION_TYPE, INTERSECT_TYPE,
 
172
  EXCEPT_TYPE, GLOBAL_OPTIONS_TYPE, DERIVED_TABLE_TYPE, OLAP_TYPE
 
173
};
 
174
 
 
175
enum olap_type 
 
176
{
 
177
  UNSPECIFIED_OLAP_TYPE, CUBE_TYPE, ROLLUP_TYPE
 
178
};
 
179
 
 
180
enum tablespace_op_type
 
181
{
 
182
  NO_TABLESPACE_OP, DISCARD_TABLESPACE, IMPORT_TABLESPACE
 
183
};
 
184
 
 
185
/* 
 
186
  String names used to print a statement with index hints.
 
187
  Keep in sync with index_hint_type.
 
188
*/
 
189
extern const char * index_hint_type_name[];
 
190
typedef uchar index_clause_map;
 
191
 
 
192
/*
 
193
  Bits in index_clause_map : one for each possible FOR clause in
 
194
  USE/FORCE/IGNORE INDEX index hint specification
 
195
*/
 
196
#define INDEX_HINT_MASK_JOIN  (1)
 
197
#define INDEX_HINT_MASK_GROUP (1 << 1)
 
198
#define INDEX_HINT_MASK_ORDER (1 << 2)
 
199
 
 
200
#define INDEX_HINT_MASK_ALL (INDEX_HINT_MASK_JOIN | INDEX_HINT_MASK_GROUP | \
 
201
                             INDEX_HINT_MASK_ORDER)
 
202
 
 
203
/* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint  */
 
204
class Index_hint : public Sql_alloc
 
205
{
 
206
public:
 
207
  /* The type of the hint : USE/FORCE/IGNORE */
 
208
  enum index_hint_type type;
 
209
  /* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
 
210
  index_clause_map clause;
 
211
  /* 
 
212
    The index name. Empty (str=NULL) name represents an empty list 
 
213
    USE INDEX () clause 
 
214
  */ 
 
215
  LEX_STRING key_name;
 
216
 
 
217
  Index_hint (enum index_hint_type type_arg, index_clause_map clause_arg,
 
218
              char *str, uint length) :
 
219
    type(type_arg), clause(clause_arg)
 
220
  {
 
221
    key_name.str= str;
 
222
    key_name.length= length;
 
223
  }
 
224
 
 
225
  void print(THD *thd, String *str);
 
226
}; 
 
227
 
 
228
/* 
 
229
  The state of the lex parsing for selects 
 
230
   
 
231
   master and slaves are pointers to select_lex.
 
232
   master is pointer to upper level node.
 
233
   slave is pointer to lower level node
 
234
   select_lex is a SELECT without union
 
235
   unit is container of either
 
236
     - One SELECT
 
237
     - UNION of selects
 
238
   select_lex and unit are both inherited form select_lex_node
 
239
   neighbors are two select_lex or units on the same level
 
240
 
 
241
   All select describing structures linked with following pointers:
 
242
   - list of neighbors (next/prev) (prev of first element point to slave
 
243
     pointer of upper structure)
 
244
     - For select this is a list of UNION's (or one element list)
 
245
     - For units this is a list of sub queries for the upper level select
 
246
 
 
247
   - pointer to master (master), which is
 
248
     If this is a unit
 
249
       - pointer to outer select_lex
 
250
     If this is a select_lex
 
251
       - pointer to outer unit structure for select
 
252
 
 
253
   - pointer to slave (slave), which is either:
 
254
     If this is a unit:
 
255
       - first SELECT that belong to this unit
 
256
     If this is a select_lex
 
257
       - first unit that belong to this SELECT (subquries or derived tables)
 
258
 
 
259
   - list of all select_lex (link_next/link_prev)
 
260
     This is to be used for things like derived tables creation, where we
 
261
     go through this list and create the derived tables.
 
262
 
 
263
   If unit contain several selects (UNION now, INTERSECT etc later)
 
264
   then it have special select_lex called fake_select_lex. It used for
 
265
   storing global parameters (like ORDER BY, LIMIT) and executing union.
 
266
   Subqueries used in global ORDER BY clause will be attached to this
 
267
   fake_select_lex, which will allow them correctly resolve fields of
 
268
   'upper' UNION and outer selects.
 
269
 
 
270
   For example for following query:
 
271
 
 
272
   select *
 
273
     from table1
 
274
     where table1.field IN (select * from table1_1_1 union
 
275
                            select * from table1_1_2)
 
276
     union
 
277
   select *
 
278
     from table2
 
279
     where table2.field=(select (select f1 from table2_1_1_1_1
 
280
                                   where table2_1_1_1_1.f2=table2_1_1.f3)
 
281
                           from table2_1_1
 
282
                           where table2_1_1.f1=table2.f2)
 
283
     union
 
284
   select * from table3;
 
285
 
 
286
   we will have following structure:
 
287
 
 
288
   select1: (select * from table1 ...)
 
289
   select2: (select * from table2 ...)
 
290
   select3: (select * from table3)
 
291
   select1.1.1: (select * from table1_1_1)
 
292
   ...
 
293
 
 
294
     main unit
 
295
     fake0
 
296
     select1 select2 select3
 
297
     |^^     |^
 
298
    s|||     ||master
 
299
    l|||     |+---------------------------------+
 
300
    a|||     +---------------------------------+|
 
301
    v|||master                         slave   ||
 
302
    e||+-------------------------+             ||
 
303
     V|            neighbor      |             V|
 
304
     unit1.1<+==================>unit1.2       unit2.1
 
305
     fake1.1
 
306
     select1.1.1 select 1.1.2    select1.2.1   select2.1.1
 
307
                                               |^
 
308
                                               ||
 
309
                                               V|
 
310
                                               unit2.1.1.1
 
311
                                               select2.1.1.1.1
 
312
 
 
313
 
 
314
   relation in main unit will be following:
 
315
   (bigger picture for:
 
316
      main unit
 
317
      fake0
 
318
      select1 select2 select3
 
319
   in the above picture)
 
320
 
 
321
         main unit
 
322
         |^^^^|fake_select_lex
 
323
         |||||+--------------------------------------------+
 
324
         ||||+--------------------------------------------+|
 
325
         |||+------------------------------+              ||
 
326
         ||+--------------+                |              ||
 
327
    slave||master         |                |              ||
 
328
         V|      neighbor |       neighbor |        master|V
 
329
         select1<========>select2<========>select3        fake0
 
330
 
 
331
    list of all select_lex will be following (as it will be constructed by
 
332
    parser):
 
333
 
 
334
    select1->select2->select3->select2.1.1->select 2.1.2->select2.1.1.1.1-+
 
335
                                                                          |
 
336
    +---------------------------------------------------------------------+
 
337
    |
 
338
    +->select1.1.1->select1.1.2
 
339
 
 
340
*/
 
341
 
 
342
/* 
 
343
    Base class for st_select_lex (SELECT_LEX) & 
 
344
    st_select_lex_unit (SELECT_LEX_UNIT)
 
345
*/
 
346
struct st_lex;
 
347
class st_select_lex;
 
348
class st_select_lex_unit;
 
349
class st_select_lex_node {
 
350
protected:
 
351
  st_select_lex_node *next, **prev,   /* neighbor list */
 
352
    *master, *slave,                  /* vertical links */
 
353
    *link_next, **link_prev;          /* list of whole SELECT_LEX */
 
354
public:
 
355
 
 
356
  ulonglong options;
 
357
 
 
358
  /*
 
359
    result of this query can't be cached, bit field, can be :
 
360
      UNCACHEABLE_DEPENDENT
 
361
      UNCACHEABLE_RAND
 
362
      UNCACHEABLE_SIDEEFFECT
 
363
      UNCACHEABLE_EXPLAIN
 
364
      UNCACHEABLE_PREPARE
 
365
  */
 
366
  uint8 uncacheable;
 
367
  enum sub_select_type linkage;
 
368
  bool no_table_names_allowed; /* used for global order by */
 
369
  bool no_error; /* suppress error message (convert it to warnings) */
 
370
 
 
371
  static void *operator new(size_t size)
 
372
  {
 
373
    return sql_alloc(size);
 
374
  }
 
375
  static void *operator new(size_t size, MEM_ROOT *mem_root)
 
376
  { return (void*) alloc_root(mem_root, (uint) size); }
 
377
  static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); }
 
378
  static void operator delete(void *ptr, MEM_ROOT *mem_root) {}
 
379
  st_select_lex_node(): linkage(UNSPECIFIED_TYPE) {}
 
380
  virtual ~st_select_lex_node() {}
 
381
  inline st_select_lex_node* get_master() { return master; }
 
382
  virtual void init_query();
 
383
  virtual void init_select();
 
384
  void include_down(st_select_lex_node *upper);
 
385
  void include_neighbour(st_select_lex_node *before);
 
386
  void include_standalone(st_select_lex_node *sel, st_select_lex_node **ref);
 
387
  void include_global(st_select_lex_node **plink);
 
388
  void exclude();
 
389
 
 
390
  virtual st_select_lex_unit* master_unit()= 0;
 
391
  virtual st_select_lex* outer_select()= 0;
 
392
  virtual st_select_lex* return_after_parsing()= 0;
 
393
 
 
394
  virtual bool set_braces(bool value);
 
395
  virtual bool inc_in_sum_expr();
 
396
  virtual uint get_in_sum_expr();
 
397
  virtual TABLE_LIST* get_table_list();
 
398
  virtual List<Item>* get_item_list();
 
399
  virtual ulong get_table_join_options();
 
400
  virtual TABLE_LIST *add_table_to_list(THD *thd, Table_ident *table,
 
401
                                        LEX_STRING *alias,
 
402
                                        ulong table_options,
 
403
                                        thr_lock_type flags= TL_UNLOCK,
 
404
                                        List<Index_hint> *hints= 0,
 
405
                                        LEX_STRING *option= 0);
 
406
  virtual void set_lock_for_tables(thr_lock_type lock_type) {}
 
407
 
 
408
  friend class st_select_lex_unit;
 
409
  friend bool mysql_new_select(struct st_lex *lex, bool move_down);
 
410
private:
 
411
  void fast_exclude();
 
412
};
 
413
typedef class st_select_lex_node SELECT_LEX_NODE;
 
414
 
 
415
/* 
 
416
   SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group 
 
417
   SELECT_LEXs
 
418
*/
 
419
class THD;
 
420
class select_result;
 
421
class JOIN;
 
422
class select_union;
 
423
class st_select_lex_unit: public st_select_lex_node {
 
424
protected:
 
425
  TABLE_LIST result_table_list;
 
426
  select_union *union_result;
 
427
  TABLE *table; /* temporary table using for appending UNION results */
 
428
 
 
429
  select_result *result;
 
430
  ulonglong found_rows_for_union;
 
431
  bool saved_error;
 
432
 
 
433
public:
 
434
  bool  prepared, // prepare phase already performed for UNION (unit)
 
435
    optimized, // optimize phase already performed for UNION (unit)
 
436
    executed, // already executed
 
437
    cleaned;
 
438
 
 
439
  // list of fields which points to temporary table for union
 
440
  List<Item> item_list;
 
441
  /*
 
442
    list of types of items inside union (used for union & derived tables)
 
443
    
 
444
    Item_type_holders from which this list consist may have pointers to Field,
 
445
    pointers is valid only after preparing SELECTS of this unit and before
 
446
    any SELECT of this unit execution
 
447
 
 
448
    TODO:
 
449
    Possibly this member should be protected, and its direct use replaced
 
450
    by get_unit_column_types(). Check the places where it is used.
 
451
  */
 
452
  List<Item> types;
 
453
  /*
 
454
    Pointer to 'last' select or pointer to unit where stored
 
455
    global parameters for union
 
456
  */
 
457
  st_select_lex *global_parameters;
 
458
  //node on wich we should return current_select pointer after parsing subquery
 
459
  st_select_lex *return_to;
 
460
  /* LIMIT clause runtime counters */
 
461
  ha_rows select_limit_cnt, offset_limit_cnt;
 
462
  /* not NULL if unit used in subselect, point to subselect item */
 
463
  Item_subselect *item;
 
464
  /* thread handler */
 
465
  THD *thd;
 
466
  /*
 
467
    SELECT_LEX for hidden SELECT in onion which process global
 
468
    ORDER BY and LIMIT
 
469
  */
 
470
  st_select_lex *fake_select_lex;
 
471
 
 
472
  st_select_lex *union_distinct; /* pointer to the last UNION DISTINCT */
 
473
  bool describe; /* union exec() called for EXPLAIN */
 
474
 
 
475
  void init_query();
 
476
  st_select_lex_unit* master_unit();
 
477
  st_select_lex* outer_select();
 
478
  st_select_lex* first_select()
 
479
  {
 
480
    return my_reinterpret_cast(st_select_lex*)(slave);
 
481
  }
 
482
  st_select_lex_unit* next_unit()
 
483
  {
 
484
    return my_reinterpret_cast(st_select_lex_unit*)(next);
 
485
  }
 
486
  st_select_lex* return_after_parsing() { return return_to; }
 
487
  void exclude_level();
 
488
  void exclude_tree();
 
489
 
 
490
  /* UNION methods */
 
491
  bool prepare(THD *thd, select_result *result, ulong additional_options);
 
492
  bool exec();
 
493
  bool cleanup();
 
494
  inline void unclean() { cleaned= 0; }
 
495
  void reinit_exec_mechanism();
 
496
 
 
497
  void print(String *str, enum_query_type query_type);
 
498
 
 
499
  bool add_fake_select_lex(THD *thd);
 
500
  void init_prepare_fake_select_lex(THD *thd);
 
501
  bool change_result(select_result_interceptor *result,
 
502
                     select_result_interceptor *old_result);
 
503
  void set_limit(st_select_lex *values);
 
504
  void set_thd(THD *thd_arg) { thd= thd_arg; }
 
505
  inline bool is_union (); 
 
506
 
 
507
  friend void lex_start(THD *thd);
 
508
  friend int subselect_union_engine::exec();
 
509
 
 
510
  List<Item> *get_unit_column_types();
 
511
};
 
512
 
 
513
typedef class st_select_lex_unit SELECT_LEX_UNIT;
 
514
 
 
515
/*
 
516
  SELECT_LEX - store information of parsed SELECT statment
 
517
*/
 
518
class st_select_lex: public st_select_lex_node
 
519
{
 
520
public:
 
521
  Name_resolution_context context;
 
522
  char *db;
 
523
  Item *where, *having;                         /* WHERE & HAVING clauses */
 
524
  Item *prep_where; /* saved WHERE clause for prepared statement processing */
 
525
  Item *prep_having;/* saved HAVING clause for prepared statement processing */
 
526
  /* Saved values of the WHERE and HAVING clauses*/
 
527
  Item::cond_result cond_value, having_value;
 
528
  /* point on lex in which it was created, used in view subquery detection */
 
529
  st_lex *parent_lex;
 
530
  enum olap_type olap;
 
531
  /* FROM clause - points to the beginning of the TABLE_LIST::next_local list. */
 
532
  SQL_LIST            table_list;
 
533
  SQL_LIST            group_list; /* GROUP BY clause. */
 
534
  List<Item>          item_list;  /* list of fields & expressions */
 
535
  List<String>        interval_list;
 
536
  bool                is_item_list_lookup;
 
537
  /* 
 
538
    Despite their names, the following are used in unions. This should 
 
539
    be rewritten. -Brian
 
540
  */
 
541
  List<Item_real_func> *ftfunc_list;
 
542
  List<Item_real_func> ftfunc_list_alloc;
 
543
  JOIN *join; /* after JOIN::prepare it is pointer to corresponding JOIN */
 
544
  List<TABLE_LIST> top_join_list; /* join list of the top level          */
 
545
  List<TABLE_LIST> *join_list;    /* list for the currently parsed join  */
 
546
  TABLE_LIST *embedding;          /* table embedding to the above list   */
 
547
  List<TABLE_LIST> sj_nests;
 
548
  /*
 
549
    Beginning of the list of leaves in a FROM clause, where the leaves
 
550
    inlcude all base tables including view tables. The tables are connected
 
551
    by TABLE_LIST::next_leaf, so leaf_tables points to the left-most leaf.
 
552
  */
 
553
  TABLE_LIST *leaf_tables;
 
554
  const char *type;               /* type of select for EXPLAIN          */
 
555
 
 
556
  SQL_LIST order_list;                /* ORDER clause */
 
557
  SQL_LIST *gorder_list;
 
558
  Item *select_limit, *offset_limit;  /* LIMIT clause parameters */
 
559
  // Arrays of pointers to top elements of all_fields list
 
560
  Item **ref_pointer_array;
 
561
 
 
562
  /*
 
563
    number of items in select_list and HAVING clause used to get number
 
564
    bigger then can be number of entries that will be added to all item
 
565
    list during split_sum_func
 
566
  */
 
567
  uint select_n_having_items;
 
568
  uint cond_count;    /* number of arguments of and/or/xor in where/having/on */
 
569
  uint between_count; /* number of between predicates in where/having/on      */
 
570
  uint max_equal_elems; /* maximal number of elements in multiple equalities  */   
 
571
  /*
 
572
    Number of fields used in select list or where clause of current select
 
573
    and all inner subselects.
 
574
  */
 
575
  uint select_n_where_fields;
 
576
  enum_parsing_place parsing_place; /* where we are parsing expression */
 
577
  bool with_sum_func;   /* sum function indicator */
 
578
  /* 
 
579
    PS or SP cond natural joins was alredy processed with permanent
 
580
    arena and all additional items which we need alredy stored in it
 
581
  */
 
582
  bool conds_processed_with_permanent_arena;
 
583
 
 
584
  ulong table_join_options;
 
585
  uint in_sum_expr;
 
586
  uint select_number; /* number of select (used for EXPLAIN) */
 
587
  int nest_level;     /* nesting level of select */
 
588
  Item_sum *inner_sum_func_list; /* list of sum func in nested selects */ 
 
589
  uint with_wild; /* item list contain '*' */
 
590
  bool  braces;         /* SELECT ... UNION (SELECT ... ) <- this braces */
 
591
  /* TRUE when having fix field called in processing of this SELECT */
 
592
  bool having_fix_field;
 
593
  /* List of references to fields referenced from inner selects */
 
594
  List<Item_outer_ref> inner_refs_list;
 
595
  /* Number of Item_sum-derived objects in this SELECT */
 
596
  uint n_sum_items;
 
597
  /* Number of Item_sum-derived objects in children and descendant SELECTs */
 
598
  uint n_child_sum_items;
 
599
 
 
600
  /* explicit LIMIT clause was used */
 
601
  bool explicit_limit;
 
602
  /*
 
603
    there are subquery in HAVING clause => we can't close tables before
 
604
    query processing end even if we use temporary table
 
605
  */
 
606
  bool subquery_in_having;
 
607
  /* TRUE <=> this SELECT is correlated w.r.t. some ancestor select */
 
608
  bool is_correlated;
 
609
  /*
 
610
    This variable is required to ensure proper work of subqueries and
 
611
    stored procedures. Generally, one should use the states of
 
612
    Query_arena to determine if it's a statement prepare or first
 
613
    execution of a stored procedure. However, in case when there was an
 
614
    error during the first execution of a stored procedure, the SP body
 
615
    is not expelled from the SP cache. Therefore, a deeply nested
 
616
    subquery might be left unoptimized. So we need this per-subquery
 
617
    variable to inidicate the optimization/execution state of every
 
618
    subquery. Prepared statements work OK in that regard, as in
 
619
    case of an error during prepare the PS is not created.
 
620
  */
 
621
  bool first_execution;
 
622
  bool first_cond_optimization;
 
623
  /* do not wrap view fields with Item_ref */
 
624
  bool no_wrap_view_item;
 
625
  /* exclude this select from check of unique_table() */
 
626
  bool exclude_from_table_unique_test;
 
627
  /* List of fields that aren't under an aggregate function */
 
628
  List<Item_field> non_agg_fields;
 
629
  /* index in the select list of the expression currently being fixed */
 
630
  int cur_pos_in_select_list;
 
631
 
 
632
  List<udf_func>     udf_list;                  /* udf function calls stack */
 
633
  /* 
 
634
    This is a copy of the original JOIN USING list that comes from
 
635
    the parser. The parser :
 
636
      1. Sets the natural_join of the second TABLE_LIST in the join
 
637
         and the st_select_lex::prev_join_using.
 
638
      2. Makes a parent TABLE_LIST and sets its is_natural_join/
 
639
       join_using_fields members.
 
640
      3. Uses the wrapper TABLE_LIST as a table in the upper level.
 
641
    We cannot assign directly to join_using_fields in the parser because
 
642
    at stage (1.) the parent TABLE_LIST is not constructed yet and
 
643
    the assignment will override the JOIN USING fields of the lower level
 
644
    joins on the right.
 
645
  */
 
646
  List<String> *prev_join_using;
 
647
  /*
 
648
    Bitmap used in the ONLY_FULL_GROUP_BY_MODE to prevent mixture of aggregate
 
649
    functions and non aggregated fields when GROUP BY list is absent.
 
650
    Bits:
 
651
      0 - non aggregated fields are used in this select,
 
652
          defined as NON_AGG_FIELD_USED.
 
653
      1 - aggregate functions are used in this select,
 
654
          defined as SUM_FUNC_USED.
 
655
  */
 
656
  uint8 full_group_by_flag;
 
657
  void init_query();
 
658
  void init_select();
 
659
  st_select_lex_unit* master_unit();
 
660
  st_select_lex_unit* first_inner_unit() 
 
661
  { 
 
662
    return (st_select_lex_unit*) slave; 
 
663
  }
 
664
  st_select_lex* outer_select();
 
665
  st_select_lex* next_select() { return (st_select_lex*) next; }
 
666
  st_select_lex* next_select_in_list() 
 
667
  {
 
668
    return (st_select_lex*) link_next;
 
669
  }
 
670
  st_select_lex_node** next_select_in_list_addr()
 
671
  {
 
672
    return &link_next;
 
673
  }
 
674
  st_select_lex* return_after_parsing()
 
675
  {
 
676
    return master_unit()->return_after_parsing();
 
677
  }
 
678
 
 
679
  void mark_as_dependent(st_select_lex *last);
 
680
 
 
681
  bool set_braces(bool value);
 
682
  bool inc_in_sum_expr();
 
683
  uint get_in_sum_expr();
 
684
 
 
685
  bool add_item_to_list(THD *thd, Item *item);
 
686
  bool add_group_to_list(THD *thd, Item *item, bool asc);
 
687
  bool add_order_to_list(THD *thd, Item *item, bool asc);
 
688
  TABLE_LIST* add_table_to_list(THD *thd, Table_ident *table,
 
689
                                LEX_STRING *alias,
 
690
                                ulong table_options,
 
691
                                thr_lock_type flags= TL_UNLOCK,
 
692
                                List<Index_hint> *hints= 0,
 
693
                                LEX_STRING *option= 0);
 
694
  TABLE_LIST* get_table_list();
 
695
  bool init_nested_join(THD *thd);
 
696
  TABLE_LIST *end_nested_join(THD *thd);
 
697
  TABLE_LIST *nest_last_join(THD *thd);
 
698
  void add_joined_table(TABLE_LIST *table);
 
699
  TABLE_LIST *convert_right_join();
 
700
  List<Item>* get_item_list();
 
701
  ulong get_table_join_options();
 
702
  void set_lock_for_tables(thr_lock_type lock_type);
 
703
  inline void init_order()
 
704
  {
 
705
    order_list.elements= 0;
 
706
    order_list.first= 0;
 
707
    order_list.next= (uchar**) &order_list.first;
 
708
  }
 
709
  /*
 
710
    This method created for reiniting LEX in mysql_admin_table() and can be
 
711
    used only if you are going remove all SELECT_LEX & units except belonger
 
712
    to LEX (LEX::unit & LEX::select, for other purposes there are
 
713
    SELECT_LEX_UNIT::exclude_level & SELECT_LEX_UNIT::exclude_tree
 
714
  */
 
715
  void cut_subtree() { slave= 0; }
 
716
  bool test_limit();
 
717
 
 
718
  friend void lex_start(THD *thd);
 
719
  st_select_lex() : n_sum_items(0), n_child_sum_items(0) {}
 
720
  void make_empty_select()
 
721
  {
 
722
    init_query();
 
723
    init_select();
 
724
  }
 
725
  bool setup_ref_array(THD *thd, uint order_group_num);
 
726
  void print(THD *thd, String *str, enum_query_type query_type);
 
727
  static void print_order(String *str,
 
728
                          ORDER *order,
 
729
                          enum_query_type query_type);
 
730
  void print_limit(THD *thd, String *str, enum_query_type query_type);
 
731
  void fix_prepare_information(THD *thd, Item **conds, Item **having_conds);
 
732
  /*
 
733
    Destroy the used execution plan (JOIN) of this subtree (this
 
734
    SELECT_LEX and all nested SELECT_LEXes and SELECT_LEX_UNITs).
 
735
  */
 
736
  bool cleanup();
 
737
  /*
 
738
    Recursively cleanup the join of this select lex and of all nested
 
739
    select lexes.
 
740
  */
 
741
  void cleanup_all_joins(bool full);
 
742
 
 
743
  void set_index_hint_type(enum index_hint_type type, index_clause_map clause);
 
744
 
 
745
  /* 
 
746
   Add a index hint to the tagged list of hints. The type and clause of the
 
747
   hint will be the current ones (set by set_index_hint()) 
 
748
  */
 
749
  bool add_index_hint (THD *thd, char *str, uint length);
 
750
 
 
751
  /* make a list to hold index hints */
 
752
  void alloc_index_hints (THD *thd);
 
753
  /* read and clear the index hints */
 
754
  List<Index_hint>* pop_index_hints(void) 
 
755
  {
 
756
    List<Index_hint> *hints= index_hints;
 
757
    index_hints= NULL;
 
758
    return hints;
 
759
  }
 
760
 
 
761
  void clear_index_hints(void) { index_hints= NULL; }
 
762
 
 
763
private:  
 
764
  /* current index hint kind. used in filling up index_hints */
 
765
  enum index_hint_type current_index_hint_type;
 
766
  index_clause_map current_index_hint_clause;
 
767
  /* a list of USE/FORCE/IGNORE INDEX */
 
768
  List<Index_hint> *index_hints;
 
769
};
 
770
typedef class st_select_lex SELECT_LEX;
 
771
 
 
772
inline bool st_select_lex_unit::is_union ()
 
773
 
774
  return first_select()->next_select() && 
 
775
    first_select()->next_select()->linkage == UNION_TYPE;
 
776
}
 
777
 
 
778
#define ALTER_ADD_COLUMN        (1L << 0)
 
779
#define ALTER_DROP_COLUMN       (1L << 1)
 
780
#define ALTER_CHANGE_COLUMN     (1L << 2)
 
781
#define ALTER_COLUMN_STORAGE    (1L << 3)
 
782
#define ALTER_COLUMN_FORMAT     (1L << 4)
 
783
#define ALTER_COLUMN_ORDER      (1L << 5)
 
784
#define ALTER_ADD_INDEX         (1L << 6)
 
785
#define ALTER_DROP_INDEX        (1L << 7)
 
786
#define ALTER_RENAME            (1L << 8)
 
787
#define ALTER_ORDER             (1L << 9)
 
788
#define ALTER_OPTIONS           (1L << 10)
 
789
#define ALTER_COLUMN_DEFAULT    (1L << 11)
 
790
#define ALTER_KEYS_ONOFF        (1L << 12)
 
791
#define ALTER_STORAGE           (1L << 13)
 
792
#define ALTER_ROW_FORMAT        (1L << 14)
 
793
#define ALTER_CONVERT           (1L << 15)
 
794
#define ALTER_FORCE             (1L << 16)
 
795
#define ALTER_RECREATE          (1L << 17)
 
796
#define ALTER_TABLE_REORG        (1L << 24)
 
797
#define ALTER_FOREIGN_KEY         (1L << 31)
 
798
 
 
799
/**
 
800
  @brief Parsing data for CREATE or ALTER TABLE.
 
801
 
 
802
  This structure contains a list of columns or indexes to be created,
 
803
  altered or dropped.
 
804
*/
 
805
 
 
806
class Alter_info
 
807
{
 
808
public:
 
809
  List<Alter_drop>              drop_list;
 
810
  List<Alter_column>            alter_list;
 
811
  List<Key>                     key_list;
 
812
  List<Create_field>            create_list;
 
813
  uint                          flags;
 
814
  enum enum_enable_or_disable   keys_onoff;
 
815
  enum tablespace_op_type       tablespace_op;
 
816
  uint                          no_parts;
 
817
  enum ha_build_method          build_method;
 
818
  Create_field                 *datetime_field;
 
819
  bool                          error_if_not_empty;
 
820
 
 
821
 
 
822
  Alter_info() :
 
823
    flags(0),
 
824
    keys_onoff(LEAVE_AS_IS),
 
825
    tablespace_op(NO_TABLESPACE_OP),
 
826
    no_parts(0),
 
827
    build_method(HA_BUILD_DEFAULT),
 
828
    datetime_field(NULL),
 
829
    error_if_not_empty(FALSE)
 
830
  {}
 
831
 
 
832
  void reset()
 
833
  {
 
834
    drop_list.empty();
 
835
    alter_list.empty();
 
836
    key_list.empty();
 
837
    create_list.empty();
 
838
    flags= 0;
 
839
    keys_onoff= LEAVE_AS_IS;
 
840
    tablespace_op= NO_TABLESPACE_OP;
 
841
    no_parts= 0;
 
842
    build_method= HA_BUILD_DEFAULT;
 
843
    datetime_field= 0;
 
844
    error_if_not_empty= FALSE;
 
845
  }
 
846
  Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root);
 
847
private:
 
848
  Alter_info &operator=(const Alter_info &rhs); // not implemented
 
849
  Alter_info(const Alter_info &rhs);            // not implemented
 
850
};
 
851
 
 
852
enum xa_option_words {XA_NONE, XA_JOIN, XA_RESUME, XA_ONE_PHASE,
 
853
                      XA_SUSPEND, XA_FOR_MIGRATE};
 
854
 
 
855
extern const LEX_STRING null_lex_str;
 
856
 
 
857
 
 
858
/*
 
859
  Class representing list of all tables used by statement.
 
860
  It also contains information about stored functions used by statement
 
861
  since during its execution we may have to add all tables used by its
 
862
  stored functions/triggers to this list in order to pre-open and lock
 
863
  them.
 
864
 
 
865
  Also used by st_lex::reset_n_backup/restore_backup_query_tables_list()
 
866
  methods to save and restore this information.
 
867
*/
 
868
 
 
869
class Query_tables_list
 
870
{
 
871
public:
 
872
  /* Global list of all tables used by this statement */
 
873
  TABLE_LIST *query_tables;
 
874
  /* Pointer to next_global member of last element in the previous list. */
 
875
  TABLE_LIST **query_tables_last;
 
876
  /*
 
877
    If non-0 then indicates that query requires prelocking and points to
 
878
    next_global member of last own element in query table list (i.e. last
 
879
    table which was not added to it as part of preparation to prelocking).
 
880
    0 - indicates that this query does not need prelocking.
 
881
  */
 
882
  TABLE_LIST **query_tables_own_last;
 
883
  /*
 
884
    Set of stored routines called by statement.
 
885
    (Note that we use lazy-initialization for this hash).
 
886
  */
 
887
  enum { START_SROUTINES_HASH_SIZE= 16 };
 
888
  HASH sroutines;
 
889
  /*
 
890
    List linking elements of 'sroutines' set. Allows you to add new elements
 
891
    to this set as you iterate through the list of existing elements.
 
892
    'sroutines_list_own_last' is pointer to ::next member of last element of
 
893
    this list which represents routine which is explicitly used by query.
 
894
    'sroutines_list_own_elements' number of explicitly used routines.
 
895
    We use these two members for restoring of 'sroutines_list' to the state
 
896
    in which it was right after query parsing.
 
897
  */
 
898
  SQL_LIST sroutines_list;
 
899
  uchar    **sroutines_list_own_last;
 
900
  uint     sroutines_list_own_elements;
 
901
 
 
902
  /*
 
903
    These constructor and destructor serve for creation/destruction
 
904
    of Query_tables_list instances which are used as backup storage.
 
905
  */
 
906
  Query_tables_list() {}
 
907
  ~Query_tables_list() {}
 
908
 
 
909
  /* Initializes (or resets) Query_tables_list object for "real" use. */
 
910
  void reset_query_tables_list(bool init);
 
911
  void destroy_query_tables_list();
 
912
  void set_query_tables_list(Query_tables_list *state)
 
913
  {
 
914
    *this= *state;
 
915
  }
 
916
 
 
917
  /*
 
918
    Direct addition to the list of query tables.
 
919
    If you are using this function, you must ensure that the table
 
920
    object, in particular table->db member, is initialized.
 
921
  */
 
922
  void add_to_query_tables(TABLE_LIST *table)
 
923
  {
 
924
    *(table->prev_global= query_tables_last)= table;
 
925
    query_tables_last= &table->next_global;
 
926
  }
 
927
  /* Return pointer to first not-own table in query-tables or 0 */
 
928
  TABLE_LIST* first_not_own_table()
 
929
  {
 
930
    return ( query_tables_own_last ? *query_tables_own_last : 0);
 
931
  }
 
932
  void chop_off_not_own_tables()
 
933
  {
 
934
    if (query_tables_own_last)
 
935
    {
 
936
      *query_tables_own_last= 0;
 
937
      query_tables_last= query_tables_own_last;
 
938
      query_tables_own_last= 0;
 
939
    }
 
940
  }
 
941
 
 
942
  /**
 
943
     Has the parser/scanner detected that this statement is unsafe?
 
944
   */
 
945
  inline bool is_stmt_unsafe() const {
 
946
    return binlog_stmt_flags & (1U << BINLOG_STMT_FLAG_UNSAFE);
 
947
  }
 
948
 
 
949
  /**
 
950
     Flag the current (top-level) statement as unsafe.
 
951
 
 
952
     The flag will be reset after the statement has finished.
 
953
 
 
954
   */
 
955
  inline void set_stmt_unsafe() {
 
956
    binlog_stmt_flags|= (1U << BINLOG_STMT_FLAG_UNSAFE);
 
957
  }
 
958
 
 
959
  inline void clear_stmt_unsafe() {
 
960
    binlog_stmt_flags&= ~(1U << BINLOG_STMT_FLAG_UNSAFE);
 
961
  }
 
962
 
 
963
  /**
 
964
    true if the parsed tree contains references to stored procedures
 
965
    or functions, false otherwise
 
966
  */
 
967
  bool uses_stored_routines() const
 
968
  { return sroutines_list.elements != 0; }
 
969
 
 
970
private:
 
971
  enum enum_binlog_stmt_flag {
 
972
    BINLOG_STMT_FLAG_UNSAFE,
 
973
    BINLOG_STMT_FLAG_COUNT
 
974
  };
 
975
 
 
976
  /*
 
977
    Tells if the parsing stage detected properties of the statement,
 
978
    for example: that some items require row-based binlogging to give
 
979
    a reliable binlog/replication, or if we will use stored functions
 
980
    or triggers which themselves need require row-based binlogging.
 
981
  */
 
982
  uint32 binlog_stmt_flags;
 
983
};
 
984
 
 
985
 
 
986
/*
 
987
  st_parsing_options contains the flags for constructions that are
 
988
  allowed in the current statement.
 
989
*/
 
990
 
 
991
struct st_parsing_options
 
992
{
 
993
  bool allows_variable;
 
994
  bool allows_select_into;
 
995
  bool allows_select_procedure;
 
996
  bool allows_derived;
 
997
 
 
998
  st_parsing_options() { reset(); }
 
999
  void reset();
 
1000
};
 
1001
 
 
1002
 
 
1003
/**
 
1004
  The state of the lexical parser, when parsing comments.
 
1005
*/
 
1006
enum enum_comment_state
 
1007
{
 
1008
  /**
 
1009
    Not parsing comments.
 
1010
  */
 
1011
  NO_COMMENT,
 
1012
  /**
 
1013
    Parsing comments that need to be preserved.
 
1014
    Typically, these are user comments '/' '*' ... '*' '/'.
 
1015
  */
 
1016
  PRESERVE_COMMENT,
 
1017
  /**
 
1018
    Parsing comments that need to be discarded.
 
1019
    Typically, these are special comments '/' '*' '!' ... '*' '/',
 
1020
    or '/' '*' '!' 'M' 'M' 'm' 'm' 'm' ... '*' '/', where the comment
 
1021
    markers should not be expanded.
 
1022
  */
 
1023
  DISCARD_COMMENT
 
1024
};
 
1025
 
 
1026
 
 
1027
/**
 
1028
  @brief This class represents the character input stream consumed during
 
1029
  lexical analysis.
 
1030
 
 
1031
  In addition to consuming the input stream, this class performs some
 
1032
  comment pre processing, by filtering out out of bound special text
 
1033
  from the query input stream.
 
1034
  Two buffers, with pointers inside each buffers, are maintained in
 
1035
  parallel. The 'raw' buffer is the original query text, which may
 
1036
  contain out-of-bound comments. The 'cpp' (for comments pre processor)
 
1037
  is the pre-processed buffer that contains only the query text that
 
1038
  should be seen once out-of-bound data is removed.
 
1039
*/
 
1040
 
 
1041
class Lex_input_stream
 
1042
{
 
1043
public:
 
1044
  Lex_input_stream(THD *thd, const char* buff, unsigned int length);
 
1045
  ~Lex_input_stream();
 
1046
 
 
1047
  /**
 
1048
    Set the echo mode.
 
1049
 
 
1050
    When echo is true, characters parsed from the raw input stream are
 
1051
    preserved. When false, characters parsed are silently ignored.
 
1052
    @param echo the echo mode.
 
1053
  */
 
1054
  void set_echo(bool echo)
 
1055
  {
 
1056
    m_echo= echo;
 
1057
  }
 
1058
 
 
1059
  /**
 
1060
    Skip binary from the input stream.
 
1061
    @param n number of bytes to accept.
 
1062
  */
 
1063
  void skip_binary(int n)
 
1064
  {
 
1065
    if (m_echo)
 
1066
    {
 
1067
      memcpy(m_cpp_ptr, m_ptr, n);
 
1068
      m_cpp_ptr += n;
 
1069
    }
 
1070
    m_ptr += n;
 
1071
  }
 
1072
 
 
1073
  /**
 
1074
    Get a character, and advance in the stream.
 
1075
    @return the next character to parse.
 
1076
  */
 
1077
  char yyGet()
 
1078
  {
 
1079
    char c= *m_ptr++;
 
1080
    if (m_echo)
 
1081
      *m_cpp_ptr++ = c;
 
1082
    return c;
 
1083
  }
 
1084
 
 
1085
  /**
 
1086
    Get the last character accepted.
 
1087
    @return the last character accepted.
 
1088
  */
 
1089
  char yyGetLast()
 
1090
  {
 
1091
    return m_ptr[-1];
 
1092
  }
 
1093
 
 
1094
  /**
 
1095
    Look at the next character to parse, but do not accept it.
 
1096
  */
 
1097
  char yyPeek()
 
1098
  {
 
1099
    return m_ptr[0];
 
1100
  }
 
1101
 
 
1102
  /**
 
1103
    Look ahead at some character to parse.
 
1104
    @param n offset of the character to look up
 
1105
  */
 
1106
  char yyPeekn(int n)
 
1107
  {
 
1108
    return m_ptr[n];
 
1109
  }
 
1110
 
 
1111
  /**
 
1112
    Cancel the effect of the last yyGet() or yySkip().
 
1113
    Note that the echo mode should not change between calls to yyGet / yySkip
 
1114
    and yyUnget. The caller is responsible for ensuring that.
 
1115
  */
 
1116
  void yyUnget()
 
1117
  {
 
1118
    m_ptr--;
 
1119
    if (m_echo)
 
1120
      m_cpp_ptr--;
 
1121
  }
 
1122
 
 
1123
  /**
 
1124
    Accept a character, by advancing the input stream.
 
1125
  */
 
1126
  void yySkip()
 
1127
  {
 
1128
    if (m_echo)
 
1129
      *m_cpp_ptr++ = *m_ptr++;
 
1130
    else
 
1131
      m_ptr++;
 
1132
  }
 
1133
 
 
1134
  /**
 
1135
    Accept multiple characters at once.
 
1136
    @param n the number of characters to accept.
 
1137
  */
 
1138
  void yySkipn(int n)
 
1139
  {
 
1140
    if (m_echo)
 
1141
    {
 
1142
      memcpy(m_cpp_ptr, m_ptr, n);
 
1143
      m_cpp_ptr += n;
 
1144
    }
 
1145
    m_ptr += n;
 
1146
  }
 
1147
 
 
1148
  /**
 
1149
    End of file indicator for the query text to parse.
 
1150
    @return true if there are no more characters to parse
 
1151
  */
 
1152
  bool eof()
 
1153
  {
 
1154
    return (m_ptr >= m_end_of_query);
 
1155
  }
 
1156
 
 
1157
  /**
 
1158
    End of file indicator for the query text to parse.
 
1159
    @param n number of characters expected
 
1160
    @return true if there are less than n characters to parse
 
1161
  */
 
1162
  bool eof(int n)
 
1163
  {
 
1164
    return ((m_ptr + n) >= m_end_of_query);
 
1165
  }
 
1166
 
 
1167
  /** Get the raw query buffer. */
 
1168
  const char *get_buf()
 
1169
  {
 
1170
    return m_buf;
 
1171
  }
 
1172
 
 
1173
  /** Get the pre-processed query buffer. */
 
1174
  const char *get_cpp_buf()
 
1175
  {
 
1176
    return m_cpp_buf;
 
1177
  }
 
1178
 
 
1179
  /** Get the end of the raw query buffer. */
 
1180
  const char *get_end_of_query()
 
1181
  {
 
1182
    return m_end_of_query;
 
1183
  }
 
1184
 
 
1185
  /** Mark the stream position as the start of a new token. */
 
1186
  void start_token()
 
1187
  {
 
1188
    m_tok_start_prev= m_tok_start;
 
1189
    m_tok_start= m_ptr;
 
1190
    m_tok_end= m_ptr;
 
1191
 
 
1192
    m_cpp_tok_start_prev= m_cpp_tok_start;
 
1193
    m_cpp_tok_start= m_cpp_ptr;
 
1194
    m_cpp_tok_end= m_cpp_ptr;
 
1195
  }
 
1196
 
 
1197
  /**
 
1198
    Adjust the starting position of the current token.
 
1199
    This is used to compensate for starting whitespace.
 
1200
  */
 
1201
  void restart_token()
 
1202
  {
 
1203
    m_tok_start= m_ptr;
 
1204
    m_cpp_tok_start= m_cpp_ptr;
 
1205
  }
 
1206
 
 
1207
  /** Get the token start position, in the raw buffer. */
 
1208
  const char *get_tok_start()
 
1209
  {
 
1210
    return m_tok_start;
 
1211
  }
 
1212
 
 
1213
  /** Get the token start position, in the pre-processed buffer. */
 
1214
  const char *get_cpp_tok_start()
 
1215
  {
 
1216
    return m_cpp_tok_start;
 
1217
  }
 
1218
 
 
1219
  /** Get the token end position, in the raw buffer. */
 
1220
  const char *get_tok_end()
 
1221
  {
 
1222
    return m_tok_end;
 
1223
  }
 
1224
 
 
1225
  /** Get the token end position, in the pre-processed buffer. */
 
1226
  const char *get_cpp_tok_end()
 
1227
  {
 
1228
    return m_cpp_tok_end;
 
1229
  }
 
1230
 
 
1231
  /** Get the previous token start position, in the raw buffer. */
 
1232
  const char *get_tok_start_prev()
 
1233
  {
 
1234
    return m_tok_start_prev;
 
1235
  }
 
1236
 
 
1237
  /** Get the current stream pointer, in the raw buffer. */
 
1238
  const char *get_ptr()
 
1239
  {
 
1240
    return m_ptr;
 
1241
  }
 
1242
 
 
1243
  /** Get the current stream pointer, in the pre-processed buffer. */
 
1244
  const char *get_cpp_ptr()
 
1245
  {
 
1246
    return m_cpp_ptr;
 
1247
  }
 
1248
 
 
1249
  /** Get the length of the current token, in the raw buffer. */
 
1250
  uint yyLength()
 
1251
  {
 
1252
    /*
 
1253
      The assumption is that the lexical analyser is always 1 character ahead,
 
1254
      which the -1 account for.
 
1255
    */
 
1256
    DBUG_ASSERT(m_ptr > m_tok_start);
 
1257
    return (uint) ((m_ptr - m_tok_start) - 1);
 
1258
  }
 
1259
 
 
1260
  /** Get the utf8-body string. */
 
1261
  const char *get_body_utf8_str()
 
1262
  {
 
1263
    return m_body_utf8;
 
1264
  }
 
1265
 
 
1266
  /** Get the utf8-body length. */
 
1267
  uint get_body_utf8_length()
 
1268
  {
 
1269
    return m_body_utf8_ptr - m_body_utf8;
 
1270
  }
 
1271
 
 
1272
  void body_utf8_start(THD *thd, const char *begin_ptr);
 
1273
  void body_utf8_append(const char *ptr);
 
1274
  void body_utf8_append(const char *ptr, const char *end_ptr);
 
1275
  void body_utf8_append_literal(THD *thd,
 
1276
                                const LEX_STRING *txt,
 
1277
                                CHARSET_INFO *txt_cs,
 
1278
                                const char *end_ptr);
 
1279
 
 
1280
  /** Current thread. */
 
1281
  THD *m_thd;
 
1282
 
 
1283
  /** Current line number. */
 
1284
  uint yylineno;
 
1285
 
 
1286
  /** Length of the last token parsed. */
 
1287
  uint yytoklen;
 
1288
 
 
1289
  /** Interface with bison, value of the last token parsed. */
 
1290
  LEX_YYSTYPE yylval;
 
1291
 
 
1292
  /** LALR(2) resolution, look ahead token.*/
 
1293
  int lookahead_token;
 
1294
 
 
1295
  /** LALR(2) resolution, value of the look ahead token.*/
 
1296
  LEX_YYSTYPE lookahead_yylval;
 
1297
 
 
1298
private:
 
1299
  /** Pointer to the current position in the raw input stream. */
 
1300
  const char *m_ptr;
 
1301
 
 
1302
  /** Starting position of the last token parsed, in the raw buffer. */
 
1303
  const char *m_tok_start;
 
1304
 
 
1305
  /** Ending position of the previous token parsed, in the raw buffer. */
 
1306
  const char *m_tok_end;
 
1307
 
 
1308
  /** End of the query text in the input stream, in the raw buffer. */
 
1309
  const char *m_end_of_query;
 
1310
 
 
1311
  /** Starting position of the previous token parsed, in the raw buffer. */
 
1312
  const char *m_tok_start_prev;
 
1313
 
 
1314
  /** Begining of the query text in the input stream, in the raw buffer. */
 
1315
  const char *m_buf;
 
1316
 
 
1317
  /** Length of the raw buffer. */
 
1318
  uint m_buf_length;
 
1319
 
 
1320
  /** Echo the parsed stream to the pre-processed buffer. */
 
1321
  bool m_echo;
 
1322
 
 
1323
  /** Pre-processed buffer. */
 
1324
  char *m_cpp_buf;
 
1325
 
 
1326
  /** Pointer to the current position in the pre-processed input stream. */
 
1327
  char *m_cpp_ptr;
 
1328
 
 
1329
  /**
 
1330
    Starting position of the last token parsed,
 
1331
    in the pre-processed buffer.
 
1332
  */
 
1333
  const char *m_cpp_tok_start;
 
1334
 
 
1335
  /**
 
1336
    Starting position of the previous token parsed,
 
1337
    in the pre-procedded buffer.
 
1338
  */
 
1339
  const char *m_cpp_tok_start_prev;
 
1340
 
 
1341
  /**
 
1342
    Ending position of the previous token parsed,
 
1343
    in the pre-processed buffer.
 
1344
  */
 
1345
  const char *m_cpp_tok_end;
 
1346
 
 
1347
  /** UTF8-body buffer created during parsing. */
 
1348
  char *m_body_utf8;
 
1349
 
 
1350
  /** Pointer to the current position in the UTF8-body buffer. */
 
1351
  char *m_body_utf8_ptr;
 
1352
 
 
1353
  /**
 
1354
    Position in the pre-processed buffer. The query from m_cpp_buf to
 
1355
    m_cpp_utf_processed_ptr is converted to UTF8-body.
 
1356
  */
 
1357
  const char *m_cpp_utf8_processed_ptr;
 
1358
 
 
1359
public:
 
1360
 
 
1361
  /** Current state of the lexical analyser. */
 
1362
  enum my_lex_states next_state;
 
1363
 
 
1364
  /**
 
1365
    Position of ';' in the stream, to delimit multiple queries.
 
1366
    This delimiter is in the raw buffer.
 
1367
  */
 
1368
  const char *found_semicolon;
 
1369
 
 
1370
  /** Token character bitmaps, to detect 7bit strings. */
 
1371
  uchar tok_bitmap;
 
1372
 
 
1373
  /** SQL_MODE = IGNORE_SPACE. */
 
1374
  bool ignore_space;
 
1375
 
 
1376
  /**
 
1377
    TRUE if we're parsing a prepared statement: in this mode
 
1378
    we should allow placeholders and disallow multi-statements.
 
1379
  */
 
1380
  bool stmt_prepare_mode;
 
1381
 
 
1382
  /** State of the lexical analyser for comments. */
 
1383
  enum_comment_state in_comment;
 
1384
 
 
1385
  /**
 
1386
    Starting position of the TEXT_STRING or IDENT in the pre-processed
 
1387
    buffer.
 
1388
 
 
1389
    NOTE: this member must be used within MYSQLlex() function only.
 
1390
  */
 
1391
  const char *m_cpp_text_start;
 
1392
 
 
1393
  /**
 
1394
    Ending position of the TEXT_STRING or IDENT in the pre-processed
 
1395
    buffer.
 
1396
 
 
1397
    NOTE: this member must be used within MYSQLlex() function only.
 
1398
    */
 
1399
  const char *m_cpp_text_end;
 
1400
 
 
1401
  /**
 
1402
    Character set specified by the character-set-introducer.
 
1403
 
 
1404
    NOTE: this member must be used within MYSQLlex() function only.
 
1405
  */
 
1406
  CHARSET_INFO *m_underscore_cs;
 
1407
};
 
1408
 
 
1409
 
 
1410
/* The state of the lex parsing. This is saved in the THD struct */
 
1411
 
 
1412
typedef struct st_lex : public Query_tables_list
 
1413
{
 
1414
  SELECT_LEX_UNIT unit;                         /* most upper unit */
 
1415
  SELECT_LEX select_lex;                        /* first SELECT_LEX */
 
1416
  /* current SELECT_LEX in parsing */
 
1417
  SELECT_LEX *current_select;
 
1418
  /* list of all SELECT_LEX */
 
1419
  SELECT_LEX *all_selects_list;
 
1420
 
 
1421
  char *length,*dec,*change;
 
1422
  LEX_STRING name;
 
1423
  char *help_arg;
 
1424
  LEX_STRING backup_dir;                                /* For RESTORE/BACKUP */
 
1425
  char* to_log;                                 /* For PURGE MASTER LOGS TO */
 
1426
  char* x509_subject,*x509_issuer,*ssl_cipher;
 
1427
  String *wild;
 
1428
  sql_exchange *exchange;
 
1429
  select_result *result;
 
1430
  Item *default_value, *on_update_value;
 
1431
  LEX_STRING comment, ident;
 
1432
  XID *xid;
 
1433
  uchar* yacc_yyss, *yacc_yyvs;
 
1434
  THD *thd;
 
1435
 
 
1436
  /* maintain a list of used plugins for this LEX */
 
1437
  DYNAMIC_ARRAY plugins;
 
1438
  plugin_ref plugins_static_buffer[INITIAL_LEX_PLUGIN_LIST_SIZE];
 
1439
 
 
1440
  CHARSET_INFO *charset;
 
1441
  bool text_string_is_7bit;
 
1442
  /* store original leaf_tables for INSERT SELECT and PS/SP */
 
1443
  TABLE_LIST *leaf_tables_insert;
 
1444
 
 
1445
  List<Key_part_spec> col_list;
 
1446
  List<Key_part_spec> ref_list;
 
1447
  List<String>        interval_list;
 
1448
  List<LEX_USER>      users_list;
 
1449
  List<LEX_COLUMN>    columns;
 
1450
  List<Item>          *insert_list,field_list,value_list,update_list;
 
1451
  List<List_item>     many_values;
 
1452
  List<set_var_base>  var_list;
 
1453
  List<Item_param>    param_list;
 
1454
  List<LEX_STRING>    view_list; // view list (list of field names in view)
 
1455
  /*
 
1456
    A stack of name resolution contexts for the query. This stack is used
 
1457
    at parse time to set local name resolution contexts for various parts
 
1458
    of a query. For example, in a JOIN ... ON (some_condition) clause the
 
1459
    Items in 'some_condition' must be resolved only against the operands
 
1460
    of the the join, and not against the whole clause. Similarly, Items in
 
1461
    subqueries should be resolved against the subqueries (and outer queries).
 
1462
    The stack is used in the following way: when the parser detects that
 
1463
    all Items in some clause need a local context, it creates a new context
 
1464
    and pushes it on the stack. All newly created Items always store the
 
1465
    top-most context in the stack. Once the parser leaves the clause that
 
1466
    required a local context, the parser pops the top-most context.
 
1467
  */
 
1468
  List<Name_resolution_context> context_stack;
 
1469
  List<LEX_STRING>     db_list;
 
1470
 
 
1471
  SQL_LIST            proc_list, auxiliary_table_list, save_list;
 
1472
  Create_field        *last_field;
 
1473
  Item_sum *in_sum_func;
 
1474
  udf_func udf;
 
1475
  HA_CHECK_OPT   check_opt;                     // check/repair options
 
1476
  HA_CREATE_INFO create_info;
 
1477
  KEY_CREATE_INFO key_create_info;
 
1478
  LEX_MASTER_INFO mi;                           // used by CHANGE MASTER
 
1479
  LEX_SERVER_OPTIONS server_options;
 
1480
  USER_RESOURCES mqh;
 
1481
  ulong type;
 
1482
  /*
 
1483
    This variable is used in post-parse stage to declare that sum-functions,
 
1484
    or functions which have sense only if GROUP BY is present, are allowed.
 
1485
    For example in a query
 
1486
    SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
 
1487
    MIN(i) in the WHERE clause is not allowed in the opposite to MIN(i)
 
1488
    in the HAVING clause. Due to possible nesting of select construct
 
1489
    the variable can contain 0 or 1 for each nest level.
 
1490
  */
 
1491
  nesting_map allow_sum_func;
 
1492
  enum_sql_command sql_command;
 
1493
  /*
 
1494
    Usually `expr` rule of yacc is quite reused but some commands better
 
1495
    not support subqueries which comes standard with this rule, like
 
1496
    KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
 
1497
    syntax error back.
 
1498
  */
 
1499
  bool expr_allows_subselect;
 
1500
 
 
1501
  thr_lock_type lock_option;
 
1502
  enum enum_duplicates duplicates;
 
1503
  enum enum_tx_isolation tx_isolation;
 
1504
  enum enum_ha_read_modes ha_read_mode;
 
1505
  union {
 
1506
    enum ha_rkey_function ha_rkey_mode;
 
1507
    enum xa_option_words xa_opt;
 
1508
    bool lock_transactional;            /* For LOCK TABLE ... IN ... MODE */
 
1509
  };
 
1510
  enum enum_var_type option_type;
 
1511
  enum enum_drop_mode drop_mode;
 
1512
 
 
1513
  uint profile_query_id;
 
1514
  uint profile_options;
 
1515
  enum ha_storage_media storage_type;
 
1516
  enum column_format_type column_format;
 
1517
  uint which_columns;
 
1518
  enum Foreign_key::fk_match_opt fk_match_option;
 
1519
  enum Foreign_key::fk_option fk_update_opt;
 
1520
  enum Foreign_key::fk_option fk_delete_opt;
 
1521
  uint slave_thd_opt, start_transaction_opt;
 
1522
  int nest_level;
 
1523
  /*
 
1524
    In LEX representing update which were transformed to multi-update
 
1525
    stores total number of tables. For LEX representing multi-delete
 
1526
    holds number of tables from which we will delete records.
 
1527
  */
 
1528
  uint table_count;
 
1529
  uint8 describe;
 
1530
  /*
 
1531
    A flag that indicates what kinds of derived tables are present in the
 
1532
    query (0 if no derived tables, otherwise DERIVED_SUBQUERY).
 
1533
  */
 
1534
  uint8 derived_tables;
 
1535
  bool drop_if_exists, drop_temporary, local_file, one_shot_set;
 
1536
  bool autocommit;
 
1537
  bool verbose, no_write_to_binlog;
 
1538
 
 
1539
  bool tx_chain, tx_release;
 
1540
  /*
 
1541
    Special JOIN::prepare mode: changing of query is prohibited.
 
1542
    When creating a view, we need to just check its syntax omitting
 
1543
    any optimizations: afterwards definition of the view will be
 
1544
    reconstructed by means of ::print() methods and written to
 
1545
    to an .frm file. We need this definition to stay untouched.
 
1546
  */
 
1547
  bool view_prepare_mode;
 
1548
  bool subqueries, ignore;
 
1549
  st_parsing_options parsing_options;
 
1550
  Alter_info alter_info;
 
1551
 
 
1552
  /*
 
1553
    field_list was created for view and should be removed before PS/SP
 
1554
    rexecuton
 
1555
  */
 
1556
  bool empty_field_list_on_rset;
 
1557
 
 
1558
  /*
 
1559
    Pointers to part of LOAD DATA statement that should be rewritten
 
1560
    during replication ("LOCAL 'filename' REPLACE INTO" part).
 
1561
  */
 
1562
  const char *fname_start;
 
1563
  const char *fname_end;
 
1564
  
 
1565
  /**
 
1566
    During name resolution search only in the table list given by 
 
1567
    Name_resolution_context::first_name_resolution_table and
 
1568
    Name_resolution_context::last_name_resolution_table
 
1569
    (see Item_field::fix_fields()). 
 
1570
  */
 
1571
  bool use_only_table_context;
 
1572
 
 
1573
  /*
 
1574
    Reference to a struct that contains information in various commands
 
1575
    to add/create/drop/change table spaces.
 
1576
  */
 
1577
  st_alter_tablespace *alter_tablespace_info;
 
1578
  
 
1579
  bool escape_used;
 
1580
  bool is_lex_started; /* If lex_start() did run. For debugging. */
 
1581
 
 
1582
  st_lex();
 
1583
 
 
1584
  virtual ~st_lex()
 
1585
  {
 
1586
    destroy_query_tables_list();
 
1587
    plugin_unlock_list(NULL, (plugin_ref *)plugins.buffer, plugins.elements);
 
1588
    delete_dynamic(&plugins);
 
1589
  }
 
1590
 
 
1591
  TABLE_LIST *unlink_first_table(bool *link_to_local);
 
1592
  void link_first_table_back(TABLE_LIST *first, bool link_to_local);
 
1593
  void first_lists_tables_same();
 
1594
 
 
1595
  bool can_be_merged();
 
1596
  bool can_use_merged();
 
1597
  bool can_not_use_merged();
 
1598
  bool only_view_structure();
 
1599
  bool need_correct_ident();
 
1600
  uint8 get_effective_with_check(TABLE_LIST *view);
 
1601
  /*
 
1602
    Is this update command where 'WHITH CHECK OPTION' clause is important
 
1603
 
 
1604
    SYNOPSIS
 
1605
      st_lex::which_check_option_applicable()
 
1606
 
 
1607
    RETURN
 
1608
      TRUE   have to take 'WHITH CHECK OPTION' clause into account
 
1609
      FALSE  'WHITH CHECK OPTION' clause do not need
 
1610
  */
 
1611
  inline bool which_check_option_applicable()
 
1612
  {
 
1613
    switch (sql_command) {
 
1614
    case SQLCOM_UPDATE:
 
1615
    case SQLCOM_UPDATE_MULTI:
 
1616
    case SQLCOM_INSERT:
 
1617
    case SQLCOM_INSERT_SELECT:
 
1618
    case SQLCOM_REPLACE:
 
1619
    case SQLCOM_REPLACE_SELECT:
 
1620
    case SQLCOM_LOAD:
 
1621
      return TRUE;
 
1622
    default:
 
1623
      return FALSE;
 
1624
    }
 
1625
  }
 
1626
 
 
1627
  void cleanup_after_one_table_open();
 
1628
 
 
1629
  bool push_context(Name_resolution_context *context)
 
1630
  {
 
1631
    return context_stack.push_front(context);
 
1632
  }
 
1633
 
 
1634
  void pop_context()
 
1635
  {
 
1636
    context_stack.pop();
 
1637
  }
 
1638
 
 
1639
  bool copy_db_to(char **p_db, size_t *p_db_length) const;
 
1640
 
 
1641
  Name_resolution_context *current_context()
 
1642
  {
 
1643
    return context_stack.head();
 
1644
  }
 
1645
  /*
 
1646
    Restore the LEX and THD in case of a parse error.
 
1647
  */
 
1648
  static void cleanup_lex_after_parse_error(THD *thd);
 
1649
 
 
1650
  void reset_n_backup_query_tables_list(Query_tables_list *backup);
 
1651
  void restore_backup_query_tables_list(Query_tables_list *backup);
 
1652
 
 
1653
  bool table_or_sp_used();
 
1654
 
 
1655
  /**
 
1656
    @brief check if the statement is a single-level join
 
1657
    @return result of the check
 
1658
      @retval TRUE  The statement doesn't contain subqueries, unions and 
 
1659
                    stored procedure calls.
 
1660
      @retval FALSE There are subqueries, UNIONs or stored procedure calls.
 
1661
  */
 
1662
  bool is_single_level_stmt() 
 
1663
  { 
 
1664
    /* 
 
1665
      This check exploits the fact that the last added to all_select_list is
 
1666
      on its top. So select_lex (as the first added) will be at the tail 
 
1667
      of the list.
 
1668
    */ 
 
1669
    if (&select_lex == all_selects_list && !sroutines.records)
 
1670
    {
 
1671
      DBUG_ASSERT(!all_selects_list->next_select_in_list());
 
1672
      return TRUE;
 
1673
    }
 
1674
    return FALSE;
 
1675
  }
 
1676
} LEX;
 
1677
 
 
1678
struct st_lex_local: public st_lex
 
1679
{
 
1680
  static void *operator new(size_t size) throw()
 
1681
  {
 
1682
    return sql_alloc(size);
 
1683
  }
 
1684
  static void *operator new(size_t size, MEM_ROOT *mem_root) throw()
 
1685
  {
 
1686
    return (void*) alloc_root(mem_root, (uint) size);
 
1687
  }
 
1688
  static void operator delete(void *ptr,size_t size)
 
1689
  { TRASH(ptr, size); }
 
1690
  static void operator delete(void *ptr, MEM_ROOT *mem_root)
 
1691
  { /* Never called */ }
 
1692
};
 
1693
 
 
1694
extern void lex_init(void);
 
1695
extern void lex_free(void);
 
1696
extern void lex_start(THD *thd);
 
1697
extern void lex_end(LEX *lex);
 
1698
extern int MYSQLlex(void *arg, void *yythd);
 
1699
 
 
1700
extern void trim_whitespace(CHARSET_INFO *cs, LEX_STRING *str);
 
1701
 
 
1702
extern bool is_lex_native_function(const LEX_STRING *name);
 
1703
 
 
1704
/**
 
1705
  @} (End of group Semantic_Analysis)
 
1706
*/
 
1707
 
 
1708
#endif /* MYSQL_SERVER */