MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_lex.h
1 /* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights
2  reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16 
21 #ifndef SQL_LEX_INCLUDED
22 #define SQL_LEX_INCLUDED
23 
24 #include "violite.h" /* SSL_type */
25 #include "sql_trigger.h"
26 #include "item.h" /* From item_subselect.h: subselect_union_engine */
27 #include "thr_lock.h" /* thr_lock_type, TL_UNLOCK */
28 #include "sql_array.h"
29 #include "mem_root_array.h"
30 #include "sql_alter.h" // Alter_info
31 
32 /* YACC and LEX Definitions */
33 
34 /* These may not be declared yet */
35 class Table_ident;
36 class sql_exchange;
37 class LEX_COLUMN;
38 class sp_head;
39 class sp_name;
40 class sp_instr;
41 class sp_pcontext;
43 class partition_info;
44 class Event_parse_data;
45 class set_var_base;
46 class sys_var;
47 class Item_func_match;
48 class File_parser;
49 class Key_part_spec;
50 
51 #ifdef MYSQL_SERVER
52 /*
53  There are 8 different type of table access so there is no more than
54  combinations 2^8 = 256:
55 
56  . STMT_READS_TRANS_TABLE
57 
58  . STMT_READS_NON_TRANS_TABLE
59 
60  . STMT_READS_TEMP_TRANS_TABLE
61 
62  . STMT_READS_TEMP_NON_TRANS_TABLE
63 
64  . STMT_WRITES_TRANS_TABLE
65 
66  . STMT_WRITES_NON_TRANS_TABLE
67 
68  . STMT_WRITES_TEMP_TRANS_TABLE
69 
70  . STMT_WRITES_TEMP_NON_TRANS_TABLE
71 
72  The unsafe conditions for each combination is represented within a byte
73  and stores the status of the option --binlog-direct-non-trans-updates,
74  whether the trx-cache is empty or not, and whether the isolation level
75  is lower than ISO_REPEATABLE_READ:
76 
77  . option (OFF/ON)
78  . trx-cache (empty/not empty)
79  . isolation (>= ISO_REPEATABLE_READ / < ISO_REPEATABLE_READ)
80 
81  bits 0 : . OFF, . empty, . >= ISO_REPEATABLE_READ
82  bits 1 : . OFF, . empty, . < ISO_REPEATABLE_READ
83  bits 2 : . OFF, . not empty, . >= ISO_REPEATABLE_READ
84  bits 3 : . OFF, . not empty, . < ISO_REPEATABLE_READ
85  bits 4 : . ON, . empty, . >= ISO_REPEATABLE_READ
86  bits 5 : . ON, . empty, . < ISO_REPEATABLE_READ
87  bits 6 : . ON, . not empty, . >= ISO_REPEATABLE_READ
88  bits 7 : . ON, . not empty, . < ISO_REPEATABLE_READ
89 */
90 extern uint binlog_unsafe_map[256];
91 /*
92  Initializes the array with unsafe combinations and its respective
93  conditions.
94 */
95 void binlog_unsafe_map_init();
96 #endif
97 
102 {
103  sys_var *var;
104  LEX_STRING base_name;
105 };
106 
107 #ifdef MYSQL_SERVER
108 /*
109  The following hack is needed because mysql_yacc.cc does not define
110  YYSTYPE before including this file
111 */
112 #ifdef MYSQL_YACC
113 #define LEX_YYSTYPE void *
114 #else
115 #include "lex_symbol.h"
116 #if MYSQL_LEX
117 #include "item_func.h" /* Cast_target used in sql_yacc.h */
118 #include "sql_get_diagnostics.h" /* Types used in sql_yacc.h */
119 #include "sql_yacc.h"
120 #define LEX_YYSTYPE YYSTYPE *
121 #else
122 #define LEX_YYSTYPE void *
123 #endif
124 #endif
125 #endif
126 
127 #include "sql_cmd.h"
128 
129 // describe/explain types
130 #define DESCRIBE_NONE 0 // Not explain query
131 #define DESCRIBE_NORMAL 1
132 #define DESCRIBE_EXTENDED 2
133 /*
134  This is not within #ifdef because we want "EXPLAIN PARTITIONS ..." to produce
135  additional "partitions" column even if partitioning is not compiled in.
136 */
137 #define DESCRIBE_PARTITIONS 4
138 
139 #ifdef MYSQL_SERVER
140 
141 enum enum_sp_suid_behaviour
142 {
143  SP_IS_DEFAULT_SUID= 0,
144  SP_IS_NOT_SUID,
145  SP_IS_SUID
146 };
147 
148 enum enum_sp_data_access
149 {
150  SP_DEFAULT_ACCESS= 0,
151  SP_CONTAINS_SQL,
152  SP_NO_SQL,
153  SP_READS_SQL_DATA,
154  SP_MODIFIES_SQL_DATA
155 };
156 
168 enum enum_sp_type
169 {
170  SP_TYPE_FUNCTION= 1,
171  SP_TYPE_PROCEDURE,
172  SP_TYPE_TRIGGER
173 };
174 
175 /*
176  Values for the type enum. This reflects the order of the enum declaration
177  in the CREATE TABLE command. These values are used to enumerate object types
178  for the ACL statements.
179 
180  These values were also used for enumerating stored program types. However, now
181  enum_sp_type should be used for that instead of them.
182 */
183 #define TYPE_ENUM_FUNCTION 1
184 #define TYPE_ENUM_PROCEDURE 2
185 #define TYPE_ENUM_TRIGGER 3
186 #define TYPE_ENUM_PROXY 4
187 
188 const LEX_STRING sp_data_access_name[]=
189 {
190  { C_STRING_WITH_LEN("") },
191  { C_STRING_WITH_LEN("CONTAINS SQL") },
192  { C_STRING_WITH_LEN("NO SQL") },
193  { C_STRING_WITH_LEN("READS SQL DATA") },
194  { C_STRING_WITH_LEN("MODIFIES SQL DATA") }
195 };
196 
197 #define DERIVED_SUBQUERY 1
198 #define DERIVED_VIEW 2
199 
200 enum enum_view_create_mode
201 {
202  VIEW_CREATE_NEW, // check that there are not such VIEW/table
203  VIEW_ALTER, // check that VIEW .frm with such name exists
204  VIEW_CREATE_OR_REPLACE // check only that there are not such table
205 };
206 
207 enum enum_drop_mode
208 {
209  DROP_DEFAULT, // mode is not specified
210  DROP_CASCADE, // CASCADE option
211  DROP_RESTRICT // RESTRICT option
212 };
213 
214 /* Options to add_table_to_list() */
215 #define TL_OPTION_UPDATING 1
216 #define TL_OPTION_FORCE_INDEX 2
217 #define TL_OPTION_IGNORE_LEAVES 4
218 #define TL_OPTION_ALIAS 8
219 
220 typedef List<Item> List_item;
221 typedef Mem_root_array<ORDER*, true> Group_list_ptrs;
222 
223 /* SERVERS CACHE CHANGES */
224 typedef struct st_lex_server_options
225 {
226  long port;
227  uint server_name_length;
228  char *server_name, *host, *db, *username, *password, *scheme, *socket, *owner;
229 } LEX_SERVER_OPTIONS;
230 
231 
239 typedef struct st_lex_master_info
240 {
241  char *host, *user, *password, *log_file_name, *bind_addr;
242  uint port, connect_retry;
243  float heartbeat_period;
244  int sql_delay;
245  ulonglong pos;
246  ulong server_id, retry_count;
247  char *gtid;
248  enum {UNTIL_SQL_BEFORE_GTIDS= 0, UNTIL_SQL_AFTER_GTIDS} gtid_until_condition;
249  bool until_after_gaps;
250 
251  /*
252  Enum is used for making it possible to detect if the user
253  changed variable or if it should be left at old value
254  */
255  enum {LEX_MI_UNCHANGED= 0, LEX_MI_DISABLE, LEX_MI_ENABLE}
256  ssl, ssl_verify_server_cert, heartbeat_opt, repl_ignore_server_ids_opt,
257  retry_count_opt, auto_position;
258  char *ssl_key, *ssl_cert, *ssl_ca, *ssl_capath, *ssl_cipher;
259  char *ssl_crl, *ssl_crlpath;
260  char *relay_log_name;
261  ulong relay_log_pos;
262  DYNAMIC_ARRAY repl_ignore_server_ids;
263  ulong server_ids_buffer[2];
264  void set_unspecified();
265 } LEX_MASTER_INFO;
266 
267 typedef struct st_lex_reset_slave
268 {
269  bool all;
270 } LEX_RESET_SLAVE;
271 
272 enum sub_select_type
273 {
274  UNSPECIFIED_TYPE,UNION_TYPE, INTERSECT_TYPE,
275  EXCEPT_TYPE, GLOBAL_OPTIONS_TYPE, DERIVED_TABLE_TYPE, OLAP_TYPE
276 };
277 
278 enum olap_type
279 {
280  UNSPECIFIED_OLAP_TYPE, CUBE_TYPE, ROLLUP_TYPE
281 };
282 
283 /*
284  String names used to print a statement with index hints.
285  Keep in sync with index_hint_type.
286 */
287 extern const char * index_hint_type_name[];
288 typedef uchar index_clause_map;
289 
290 /*
291  Bits in index_clause_map : one for each possible FOR clause in
292  USE/FORCE/IGNORE INDEX index hint specification
293 */
294 #define INDEX_HINT_MASK_JOIN (1)
295 #define INDEX_HINT_MASK_GROUP (1 << 1)
296 #define INDEX_HINT_MASK_ORDER (1 << 2)
297 
298 #define INDEX_HINT_MASK_ALL (INDEX_HINT_MASK_JOIN | INDEX_HINT_MASK_GROUP | \
299  INDEX_HINT_MASK_ORDER)
300 
301 /* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint */
302 class Index_hint : public Sql_alloc
303 {
304 public:
305  /* The type of the hint : USE/FORCE/IGNORE */
306  enum index_hint_type type;
307  /* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
308  index_clause_map clause;
309  /*
310  The index name. Empty (str=NULL) name represents an empty list
311  USE INDEX () clause
312  */
313  LEX_STRING key_name;
314 
315  Index_hint (enum index_hint_type type_arg, index_clause_map clause_arg,
316  char *str, uint length) :
317  type(type_arg), clause(clause_arg)
318  {
319  key_name.str= str;
320  key_name.length= length;
321  }
322 
323  void print(THD *thd, String *str);
324 };
325 
326 /*
327  The state of the lex parsing for selects
328 
329  master and slaves are pointers to select_lex.
330  master is pointer to upper level node.
331  slave is pointer to lower level node
332  select_lex is a SELECT without union
333  unit is container of either
334  - One SELECT
335  - UNION of selects
336  select_lex and unit are both inherited form select_lex_node
337  neighbors are two select_lex or units on the same level
338 
339  All select describing structures linked with following pointers:
340  - list of neighbors (next/prev) (prev of first element point to slave
341  pointer of upper structure)
342  - For select this is a list of UNION's (or one element list)
343  - For units this is a list of sub queries for the upper level select
344 
345  - pointer to master (master), which is
346  If this is a unit
347  - pointer to outer select_lex
348  If this is a select_lex
349  - pointer to outer unit structure for select
350 
351  - pointer to slave (slave), which is either:
352  If this is a unit:
353  - first SELECT that belong to this unit
354  If this is a select_lex
355  - first unit that belong to this SELECT (subquries or derived tables)
356 
357  - list of all select_lex (link_next/link_prev)
358  This is to be used for things like derived tables creation, where we
359  go through this list and create the derived tables.
360 
361  If unit contain several selects (UNION now, INTERSECT etc later)
362  then it have special select_lex called fake_select_lex. It used for
363  storing global parameters (like ORDER BY, LIMIT) and executing union.
364  Subqueries used in global ORDER BY clause will be attached to this
365  fake_select_lex, which will allow them correctly resolve fields of
366  'upper' UNION and outer selects.
367 
368  For example for following query:
369 
370  select *
371  from table1
372  where table1.field IN (select * from table1_1_1 union
373  select * from table1_1_2)
374  union
375  select *
376  from table2
377  where table2.field=(select (select f1 from table2_1_1_1_1
378  where table2_1_1_1_1.f2=table2_1_1.f3)
379  from table2_1_1
380  where table2_1_1.f1=table2.f2)
381  union
382  select * from table3;
383 
384  we will have following structure:
385 
386  select1: (select * from table1 ...)
387  select2: (select * from table2 ...)
388  select3: (select * from table3)
389  select1.1.1: (select * from table1_1_1)
390  ...
391 
392  main unit
393  fake0
394  select1 select2 select3
395  |^^ |^
396  s||| ||master
397  l||| |+---------------------------------+
398  a||| +---------------------------------+|
399  v|||master slave ||
400  e||+-------------------------+ ||
401  V| neighbor | V|
402  unit1.1<+==================>unit1.2 unit2.1
403  fake1.1
404  select1.1.1 select 1.1.2 select1.2.1 select2.1.1
405  |^
406  ||
407  V|
408  unit2.1.1.1
409  select2.1.1.1.1
410 
411 
412  relation in main unit will be following:
413  (bigger picture for:
414  main unit
415  fake0
416  select1 select2 select3
417  in the above picture)
418 
419  main unit
420  |^^^^|fake_select_lex
421  |||||+--------------------------------------------+
422  ||||+--------------------------------------------+|
423  |||+------------------------------+ ||
424  ||+--------------+ | ||
425  slave||master | | ||
426  V| neighbor | neighbor | master|V
427  select1<========>select2<========>select3 fake0
428 
429  list of all select_lex will be following (as it will be constructed by
430  parser):
431 
432  select1->select2->select3->select2.1.1->select 2.1.2->select2.1.1.1.1-+
433  |
434  +---------------------------------------------------------------------+
435  |
436  +->select1.1.1->select1.1.2
437 
438 */
439 
440 /*
441  Base class for st_select_lex (SELECT_LEX) &
442  st_select_lex_unit (SELECT_LEX_UNIT)
443 */
444 struct LEX;
445 class st_select_lex;
446 class st_select_lex_unit;
447 
448 
449 class st_select_lex_node {
450 protected:
451  st_select_lex_node *next, **prev, /* neighbor list */
452  *master, *slave, /* vertical links */
453  *link_next, **link_prev; /* list of whole SELECT_LEX */
454 public:
455 
456  ulonglong options;
457 
458  /*
459  In sql_cache we store SQL_CACHE flag as specified by user to be
460  able to restore SELECT statement from internal structures.
461  */
462  enum e_sql_cache { SQL_CACHE_UNSPECIFIED, SQL_NO_CACHE, SQL_CACHE };
463  e_sql_cache sql_cache;
464 
465  /*
466  result of this query can't be cached, bit field, can be :
467  UNCACHEABLE_DEPENDENT
468  UNCACHEABLE_RAND
469  UNCACHEABLE_SIDEEFFECT
470  UNCACHEABLE_EXPLAIN
471  UNCACHEABLE_PREPARE
472  */
473  uint8 uncacheable;
474  enum sub_select_type linkage;
475  bool no_table_names_allowed; /* used for global order by */
476  bool no_error; /* suppress error message (convert it to warnings) */
477 
478  static void *operator new(size_t size) throw ()
479  {
480  return sql_alloc(size);
481  }
482  static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
483  { return (void*) alloc_root(mem_root, (uint) size); }
484  static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); }
485  static void operator delete(void *ptr, MEM_ROOT *mem_root) {}
486 
487  // Ensures that at least all members used during cleanup() are initialized.
488  st_select_lex_node()
489  : next(NULL), prev(NULL),
490  master(NULL), slave(NULL),
491  link_next(NULL), link_prev(NULL),
492  linkage(UNSPECIFIED_TYPE)
493  {
494  }
495  virtual ~st_select_lex_node() {}
496 
497  inline st_select_lex_node* get_master() { return master; }
498  virtual void init_query();
499  virtual void init_select();
500  void include_down(st_select_lex_node *upper);
501  void include_neighbour(st_select_lex_node *before);
502  void include_standalone(st_select_lex_node *sel, st_select_lex_node **ref);
503  void include_global(st_select_lex_node **plink);
504  void exclude();
505 
506  virtual st_select_lex_unit* master_unit()= 0;
507  virtual st_select_lex* outer_select()= 0;
508 
509  virtual bool set_braces(bool value);
510  virtual bool inc_in_sum_expr();
511  virtual uint get_in_sum_expr();
512  virtual TABLE_LIST* get_table_list();
513  virtual List<Item>* get_item_list();
514  virtual ulong get_table_join_options();
515  virtual TABLE_LIST *add_table_to_list(THD *thd, Table_ident *table,
516  LEX_STRING *alias,
517  ulong table_options,
518  thr_lock_type flags= TL_UNLOCK,
519  enum_mdl_type mdl_type= MDL_SHARED_READ,
520  List<Index_hint> *hints= 0,
521  List<String> *partition_names= 0,
522  LEX_STRING *option= 0);
523  virtual void set_lock_for_tables(thr_lock_type lock_type) {}
524 
525  friend class st_select_lex_unit;
526  friend bool mysql_new_select(LEX *lex, bool move_down);
527  friend bool mysql_make_view(THD *thd, TABLE_SHARE *share, TABLE_LIST *table,
528  bool open_view_no_parse);
529 private:
530  void fast_exclude();
531 };
532 typedef class st_select_lex_node SELECT_LEX_NODE;
533 
534 /*
535  SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group
536  SELECT_LEXs
537 */
538 class THD;
539 class select_result;
540 class JOIN;
541 class select_union;
542 
543 
544 class st_select_lex_unit: public st_select_lex_node {
545 protected:
546  TABLE_LIST result_table_list;
547  select_union *union_result;
548  TABLE *table; /* temporary table using for appending UNION results */
549 
550  select_result *result;
551  ulonglong found_rows_for_union;
552  bool saved_error;
553 
554 public:
555  // Ensures that at least all members used during cleanup() are initialized.
556  st_select_lex_unit()
557  : union_result(NULL), table(NULL), result(NULL),
558  cleaned(false),
559  fake_select_lex(NULL),
560  explain_marker(0)
561  {
562  }
563 
564  bool prepared, // prepare phase already performed for UNION (unit)
565  optimized, // optimize phase already performed for UNION (unit)
566  executed, // already executed
567  cleaned;
568 
569  // list of fields which points to temporary table for union
570  List<Item> item_list;
571  /*
572  list of types of items inside union (used for union & derived tables)
573 
574  Item_type_holders from which this list consist may have pointers to Field,
575  pointers is valid only after preparing SELECTS of this unit and before
576  any SELECT of this unit execution
577 
578  TODO:
579  Possibly this member should be protected, and its direct use replaced
580  by get_unit_column_types(). Check the places where it is used.
581  */
582  List<Item> types;
583  /*
584  Pointer to 'last' select or pointer to unit where stored
585  global parameters for union
586  */
587  st_select_lex *global_parameters;
588  /* LIMIT clause runtime counters */
589  ha_rows select_limit_cnt, offset_limit_cnt;
590  /* not NULL if unit used in subselect, point to subselect item */
591  Item_subselect *item;
592  /* thread handler */
593  THD *thd;
594  /*
595  SELECT_LEX for hidden SELECT in onion which process global
596  ORDER BY and LIMIT
597  */
598  st_select_lex *fake_select_lex;
599 
600  st_select_lex *union_distinct; /* pointer to the last UNION DISTINCT */
601  bool describe; /* union exec() called for EXPLAIN */
602 
612  int explain_marker;
613 
614  void init_query();
615  st_select_lex_unit* master_unit();
616  st_select_lex* outer_select();
617  st_select_lex* first_select()
618  {
619  return reinterpret_cast<st_select_lex*>(slave);
620  }
621  st_select_lex_unit* next_unit()
622  {
623  return reinterpret_cast<st_select_lex_unit*>(next);
624  }
625  void exclude_level();
626  void exclude_tree();
627  inline select_result *get_result() { return result; }
628 
629  /* UNION methods */
630  bool prepare(THD *thd, select_result *result, ulong additional_options);
631  bool optimize();
632  bool exec();
633  bool explain();
634  bool cleanup();
635  bool cleanup_level();
636  inline void unclean() { cleaned= 0; }
637  void reinit_exec_mechanism();
638 
639  void print(String *str, enum_query_type query_type);
640 
641  bool add_fake_select_lex(THD *thd);
642  bool init_prepare_fake_select_lex(THD *thd, bool no_const_tables);
643  inline bool is_prepared() { return prepared; }
644  bool change_result(select_result_interceptor *result,
645  select_result_interceptor *old_result);
646  void set_limit(st_select_lex *values);
647  void set_thd(THD *thd_arg) { thd= thd_arg; }
648  inline bool is_union ();
649 
650  friend void lex_start(THD *thd);
651  friend bool subselect_union_engine::exec();
652 
653  List<Item> *get_unit_column_types();
654  List<Item> *get_field_list();
655 private:
656  void invalidate();
657 };
658 
659 typedef class st_select_lex_unit SELECT_LEX_UNIT;
661 
662 /*
663  SELECT_LEX - store information of parsed SELECT statment
664 */
665 class st_select_lex: public st_select_lex_node
666 {
667 public:
668  Name_resolution_context context;
669  /*
670  Two fields used by semi-join transformations to know when semi-join is
671  possible, and in which condition tree the subquery predicate is located.
672  */
673  enum Resolve_place { RESOLVE_NONE, RESOLVE_JOIN_NEST, RESOLVE_CONDITION,
674  RESOLVE_HAVING };
675  Resolve_place resolve_place; // Indicates part of query being resolved
676  TABLE_LIST *resolve_nest; // Used when resolving outer join condition
677  char *db;
678  Item *where, *having; /* WHERE & HAVING clauses */
679  Item *prep_where; /* saved WHERE clause for prepared statement processing */
680  Item *prep_having;/* saved HAVING clause for prepared statement processing */
689  Item::cond_result cond_value, having_value;
690  /* point on lex in which it was created, used in view subquery detection */
691  LEX *parent_lex;
692  enum olap_type olap;
693  /* FROM clause - points to the beginning of the TABLE_LIST::next_local list. */
694  SQL_I_List<TABLE_LIST> table_list;
695 
696  /*
697  GROUP BY clause.
698  This list may be mutated during optimization (by remove_const()),
699  so for prepared statements, we keep a copy of the ORDER.next pointers in
700  group_list_ptrs, and re-establish the original list before each execution.
701  */
702  SQL_I_List<ORDER> group_list;
703  Group_list_ptrs *group_list_ptrs;
704 
711  List<Item> item_list;
712  List<String> interval_list;
713  bool is_item_list_lookup;
714  /*
715  Usualy it is pointer to ftfunc_list_alloc, but in union used to create fake
716  select_lex for calling mysql_select under results of union
717  */
718  List<Item_func_match> *ftfunc_list;
719  List<Item_func_match> ftfunc_list_alloc;
720  JOIN *join; /* after JOIN::prepare it is pointer to corresponding JOIN */
721  List<TABLE_LIST> top_join_list; /* join list of the top level */
722  List<TABLE_LIST> *join_list; /* list for the currently parsed join */
723  TABLE_LIST *embedding; /* table embedding to the above list */
725  List<TABLE_LIST> sj_nests;
726  //Dynamic_array<TABLE_LIST*> sj_nests; psergey-5:
727  /*
728  Beginning of the list of leaves in a FROM clause, where the leaves
729  inlcude all base tables including view tables. The tables are connected
730  by TABLE_LIST::next_leaf, so leaf_tables points to the left-most leaf.
731  */
732  TABLE_LIST *leaf_tables;
736  enum type_enum {
737  SLT_NONE= 0,
738  SLT_PRIMARY,
739  SLT_SIMPLE,
740  SLT_DERIVED,
741  SLT_SUBQUERY,
742  SLT_UNION,
743  SLT_UNION_RESULT,
744  SLT_MATERIALIZED,
745  // Total:
746  SLT_total
747  // Don't insert new types below this line!
748  };
749 
750  /*
751  ORDER BY clause.
752  This list may be mutated during optimization (by remove_const()),
753  so for prepared statements, we keep a copy of the ORDER.next pointers in
754  order_list_ptrs, and re-establish the original list before each execution.
755  */
756  SQL_I_List<ORDER> order_list;
757  Group_list_ptrs *order_list_ptrs;
758 
759  SQL_I_List<ORDER> gorder_list;
760  Item *select_limit, *offset_limit; /* LIMIT clause parameters */
761 
763  Ref_ptr_array ref_pointer_array;
764 
766  uint derived_table_count;
768  uint materialized_table_count;
770  uint partitioned_table_count;
771  /*
772  number of items in select_list and HAVING clause used to get number
773  bigger then can be number of entries that will be added to all item
774  list during split_sum_func
775  */
776  uint select_n_having_items;
777  uint cond_count; /* number of arguments of and/or/xor in where/having/on */
778  uint between_count; /* number of between predicates in where/having/on */
779  uint max_equal_elems; /* maximal number of elements in multiple equalities */
780  /*
781  Number of fields used in select list or where clause of current select
782  and all inner subselects.
783  */
784  uint select_n_where_fields;
785  enum_parsing_place parsing_place; /* where we are parsing expression */
786  bool with_sum_func; /* sum function indicator */
787 
788  ulong table_join_options;
789  uint in_sum_expr;
790  uint select_number; /* number of select (used for EXPLAIN) */
795  int nest_level;
796  /* Circularly linked list of sum func in nested selects */
797  Item_sum *inner_sum_func_list;
798  uint with_wild; /* item list contain '*' */
799  bool braces; /* SELECT ... UNION (SELECT ... ) <- this braces */
800  /* TRUE when having fix field called in processing of this SELECT */
801  bool having_fix_field;
802  /* TRUE when GROUP BY fix field called in processing of this SELECT */
803  bool group_fix_field;
804  /* List of references to fields referenced from inner selects */
805  List<Item_outer_ref> inner_refs_list;
806  /* Number of Item_sum-derived objects in this SELECT */
807  uint n_sum_items;
808  /* Number of Item_sum-derived objects in children and descendant SELECTs */
809  uint n_child_sum_items;
810 
811  /* explicit LIMIT clause was used */
812  bool explicit_limit;
813  /*
814  there are subquery in HAVING clause => we can't close tables before
815  query processing end even if we use temporary table
816  */
817  bool subquery_in_having;
818  /*
819  This variable is required to ensure proper work of subqueries and
820  stored procedures. Generally, one should use the states of
821  Query_arena to determine if it's a statement prepare or first
822  execution of a stored procedure. However, in case when there was an
823  error during the first execution of a stored procedure, the SP body
824  is not expelled from the SP cache. Therefore, a deeply nested
825  subquery might be left unoptimized. So we need this per-subquery
826  variable to inidicate the optimization/execution state of every
827  subquery. Prepared statements work OK in that regard, as in
828  case of an error during prepare the PS is not created.
829  */
830  bool first_execution;
831  bool first_natural_join_processing;
832  bool first_cond_optimization;
833  /* do not wrap view fields with Item_ref */
834  bool no_wrap_view_item;
835  /* exclude this select from check of unique_table() */
836  bool exclude_from_table_unique_test;
837  /* List of table columns which are not under an aggregate function */
838  List<Item_field> non_agg_fields;
839 
841  static const int ALL_FIELDS_UNDEF_POS= INT_MIN;
842 
853  int cur_pos_in_all_fields;
854 
855  List<udf_func> udf_list; /* udf function calls stack */
856 
857  /*
858  This is a copy of the original JOIN USING list that comes from
859  the parser. The parser :
860  1. Sets the natural_join of the second TABLE_LIST in the join
861  and the st_select_lex::prev_join_using.
862  2. Makes a parent TABLE_LIST and sets its is_natural_join/
863  join_using_fields members.
864  3. Uses the wrapper TABLE_LIST as a table in the upper level.
865  We cannot assign directly to join_using_fields in the parser because
866  at stage (1.) the parent TABLE_LIST is not constructed yet and
867  the assignment will override the JOIN USING fields of the lower level
868  joins on the right.
869  */
870  List<String> *prev_join_using;
875  table_map select_list_tables;
877  st_select_lex *removed_select;
878 
879  void init_query();
880  void init_select();
881  st_select_lex_unit* master_unit();
882  st_select_lex_unit* first_inner_unit()
883  {
884  return (st_select_lex_unit*) slave;
885  }
886  st_select_lex* outer_select();
887  st_select_lex* next_select() { return (st_select_lex*) next; }
888 
889  st_select_lex* last_select()
890  {
891  st_select_lex* mylast= this;
892  for (; mylast->next_select(); mylast= mylast->next_select())
893  {}
894  return mylast;
895  }
896 
897  st_select_lex* next_select_in_list()
898  {
899  return (st_select_lex*) link_next;
900  }
901  st_select_lex_node** next_select_in_list_addr()
902  {
903  return &link_next;
904  }
905  void invalidate();
906  void mark_as_dependent(st_select_lex *last);
907 
908  bool set_braces(bool value);
909  bool inc_in_sum_expr();
910  uint get_in_sum_expr();
911 
912  bool add_item_to_list(THD *thd, Item *item);
913  bool add_group_to_list(THD *thd, Item *item, bool asc);
914  bool add_ftfunc_to_list(Item_func_match *func);
915  bool add_order_to_list(THD *thd, Item *item, bool asc);
916  bool add_gorder_to_list(THD *thd, Item *item, bool asc);
917  TABLE_LIST* add_table_to_list(THD *thd, Table_ident *table,
918  LEX_STRING *alias,
919  ulong table_options,
920  thr_lock_type flags= TL_UNLOCK,
921  enum_mdl_type mdl_type= MDL_SHARED_READ,
922  List<Index_hint> *hints= 0,
923  List<String> *partition_names= 0,
924  LEX_STRING *option= 0);
925  TABLE_LIST* get_table_list();
926  bool init_nested_join(THD *thd);
927  TABLE_LIST *end_nested_join(THD *thd);
928  TABLE_LIST *nest_last_join(THD *thd);
929  void add_joined_table(TABLE_LIST *table);
930  TABLE_LIST *convert_right_join();
931  List<Item>* get_item_list();
932  ulong get_table_join_options();
933  void set_lock_for_tables(thr_lock_type lock_type);
934  inline void init_order()
935  {
936  order_list.elements= 0;
937  order_list.first= 0;
938  order_list.next= &order_list.first;
939  }
940  /*
941  This method created for reiniting LEX in mysql_admin_table() and can be
942  used only if you are going remove all SELECT_LEX & units except belonger
943  to LEX (LEX::unit & LEX::select, for other purposes there are
944  SELECT_LEX_UNIT::exclude_level & SELECT_LEX_UNIT::exclude_tree
945  */
946  void cut_subtree() { slave= 0; }
947  bool test_limit();
948 
949  friend void lex_start(THD *thd);
950  st_select_lex() : group_list_ptrs(NULL), order_list_ptrs(NULL),
951  n_sum_items(0), n_child_sum_items(0),
952  cur_pos_in_all_fields(ALL_FIELDS_UNDEF_POS)
953  {}
954  void make_empty_select()
955  {
956  init_query();
957  init_select();
958  }
959  bool setup_ref_array(THD *thd, uint order_group_num);
960  void print(THD *thd, String *str, enum_query_type query_type);
961  static void print_order(String *str,
962  ORDER *order,
963  enum_query_type query_type);
964  void print_limit(THD *thd, String *str, enum_query_type query_type);
965  void fix_prepare_information(THD *thd, Item **conds, Item **having_conds);
966  /*
967  Destroy the used execution plan (JOIN) of this subtree (this
968  SELECT_LEX and all nested SELECT_LEXes and SELECT_LEX_UNITs).
969  */
970  bool cleanup();
971  bool cleanup_level();
972  /*
973  Recursively cleanup the join of this select lex and of all nested
974  select lexes.
975  */
976  void cleanup_all_joins(bool full);
977 
978  void set_index_hint_type(enum index_hint_type type, index_clause_map clause);
979 
980  /*
981  Add a index hint to the tagged list of hints. The type and clause of the
982  hint will be the current ones (set by set_index_hint())
983  */
984  bool add_index_hint (THD *thd, char *str, uint length);
985 
986  /* make a list to hold index hints */
987  void alloc_index_hints (THD *thd);
988  /* read and clear the index hints */
989  List<Index_hint>* pop_index_hints(void)
990  {
991  List<Index_hint> *hints= index_hints;
992  index_hints= NULL;
993  return hints;
994  }
995 
996  void clear_index_hints(void) { index_hints= NULL; }
997  bool handle_derived(LEX *lex, bool (*processor)(THD*, LEX*, TABLE_LIST*));
998  bool is_part_of_union() { return master_unit()->is_union(); }
999 
1000  /*
1001  For MODE_ONLY_FULL_GROUP_BY we need to maintain two flags:
1002  - Non-aggregated fields are used in this select.
1003  - Aggregate functions are used in this select.
1004  In MODE_ONLY_FULL_GROUP_BY only one of these may be true.
1005  */
1006  bool non_agg_field_used() const { return m_non_agg_field_used; }
1007  bool agg_func_used() const { return m_agg_func_used; }
1008 
1009  void set_non_agg_field_used(bool val) { m_non_agg_field_used= val; }
1010  void set_agg_func_used(bool val) { m_agg_func_used= val; }
1011 
1013  type_enum type(const THD *thd);
1014 
1016  const char *get_type_str(const THD *thd) { return type_str[type(thd)]; }
1017  static const char *get_type_str(type_enum type) { return type_str[type]; }
1018 
1019  bool is_dependent() const { return uncacheable & UNCACHEABLE_DEPENDENT; }
1020  bool is_cacheable() const
1021  {
1022  // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
1023  return !(uncacheable & ~UNCACHEABLE_EXPLAIN);
1024  }
1025 
1026 private:
1027  bool m_non_agg_field_used;
1028  bool m_agg_func_used;
1029 
1030  /* current index hint kind. used in filling up index_hints */
1031  enum index_hint_type current_index_hint_type;
1032  index_clause_map current_index_hint_clause;
1033  /* a list of USE/FORCE/IGNORE INDEX */
1034  List<Index_hint> *index_hints;
1035 
1036  static const char *type_str[SLT_total];
1037 };
1038 typedef class st_select_lex SELECT_LEX;
1039 
1040 inline bool st_select_lex_unit::is_union ()
1041 {
1042  return first_select()->next_select() &&
1043  first_select()->next_select()->linkage == UNION_TYPE;
1044 }
1045 
1047 class Switch_resolve_place
1048 {
1049 public:
1050  Switch_resolve_place(SELECT_LEX::Resolve_place *rp_ptr,
1051  SELECT_LEX::Resolve_place new_rp,
1052  bool apply)
1053  : rp(NULL), saved_rp()
1054  {
1055  if (apply)
1056  {
1057  rp= rp_ptr;
1058  saved_rp= *rp;
1059  *rp= new_rp;
1060  }
1061  }
1062  ~Switch_resolve_place()
1063  {
1064  if (rp)
1065  *rp= saved_rp;
1066  }
1067 private:
1068  SELECT_LEX::Resolve_place *rp;
1069  SELECT_LEX::Resolve_place saved_rp;
1070 };
1071 
1072 
1073 typedef struct struct_slave_connection
1074 {
1075  char *user;
1076  char *password;
1077  char *plugin_auth;
1078  char *plugin_dir;
1079 
1080  void reset();
1081 } LEX_SLAVE_CONNECTION;
1082 
1083 struct st_sp_chistics
1084 {
1085  LEX_STRING comment;
1086  enum enum_sp_suid_behaviour suid;
1087  bool detistic;
1088  enum enum_sp_data_access daccess;
1089 };
1090 
1091 extern const LEX_STRING null_lex_str;
1092 extern const LEX_STRING empty_lex_str;
1093 
1094 struct st_trg_chistics
1095 {
1096  enum trg_action_time_type action_time;
1097  enum trg_event_type event;
1098 };
1099 
1100 extern sys_var *trg_new_row_fake_var;
1101 
1102 enum xa_option_words {XA_NONE, XA_JOIN, XA_RESUME, XA_ONE_PHASE,
1103  XA_SUSPEND, XA_FOR_MIGRATE};
1104 
1105 extern const LEX_STRING null_lex_str;
1106 
1107 class Sroutine_hash_entry;
1108 
1109 /*
1110  Class representing list of all tables used by statement and other
1111  information which is necessary for opening and locking its tables,
1112  like SQL command for this statement.
1113 
1114  Also contains information about stored functions used by statement
1115  since during its execution we may have to add all tables used by its
1116  stored functions/triggers to this list in order to pre-open and lock
1117  them.
1118 
1119  Also used by LEX::reset_n_backup/restore_backup_query_tables_list()
1120  methods to save and restore this information.
1121 */
1122 
1123 class Query_tables_list
1124 {
1125 public:
1132  enum_sql_command sql_command;
1133  /* Global list of all tables used by this statement */
1134  TABLE_LIST *query_tables;
1135  /* Pointer to next_global member of last element in the previous list. */
1136  TABLE_LIST **query_tables_last;
1137  /*
1138  If non-0 then indicates that query requires prelocking and points to
1139  next_global member of last own element in query table list (i.e. last
1140  table which was not added to it as part of preparation to prelocking).
1141  0 - indicates that this query does not need prelocking.
1142  */
1143  TABLE_LIST **query_tables_own_last;
1144  /*
1145  Set of stored routines called by statement.
1146  (Note that we use lazy-initialization for this hash).
1147  */
1148  enum { START_SROUTINES_HASH_SIZE= 16 };
1149  HASH sroutines;
1150  /*
1151  List linking elements of 'sroutines' set. Allows you to add new elements
1152  to this set as you iterate through the list of existing elements.
1153  'sroutines_list_own_last' is pointer to ::next member of last element of
1154  this list which represents routine which is explicitly used by query.
1155  'sroutines_list_own_elements' number of explicitly used routines.
1156  We use these two members for restoring of 'sroutines_list' to the state
1157  in which it was right after query parsing.
1158  */
1159  SQL_I_List<Sroutine_hash_entry> sroutines_list;
1160  Sroutine_hash_entry **sroutines_list_own_last;
1161  uint sroutines_list_own_elements;
1162 
1175  enum enum_lock_tables_state {
1176  LTS_NOT_LOCKED = 0,
1177  LTS_LOCKED
1178  };
1179  enum_lock_tables_state lock_tables_state;
1180  bool is_query_tables_locked()
1181  {
1182  return (lock_tables_state == LTS_LOCKED);
1183  }
1184 
1192  uint table_count;
1193 
1194  /*
1195  These constructor and destructor serve for creation/destruction
1196  of Query_tables_list instances which are used as backup storage.
1197  */
1198  Query_tables_list() {}
1199  ~Query_tables_list() {}
1200 
1201  /* Initializes (or resets) Query_tables_list object for "real" use. */
1202  void reset_query_tables_list(bool init);
1203  void destroy_query_tables_list();
1204  void set_query_tables_list(Query_tables_list *state)
1205  {
1206  *this= *state;
1207  }
1208 
1209  /*
1210  Direct addition to the list of query tables.
1211  If you are using this function, you must ensure that the table
1212  object, in particular table->db member, is initialized.
1213  */
1214  void add_to_query_tables(TABLE_LIST *table)
1215  {
1216  *(table->prev_global= query_tables_last)= table;
1217  query_tables_last= &table->next_global;
1218  }
1219  bool requires_prelocking()
1220  {
1221  return test(query_tables_own_last);
1222  }
1223  void mark_as_requiring_prelocking(TABLE_LIST **tables_own_last)
1224  {
1225  query_tables_own_last= tables_own_last;
1226  }
1227  /* Return pointer to first not-own table in query-tables or 0 */
1228  TABLE_LIST* first_not_own_table()
1229  {
1230  return ( query_tables_own_last ? *query_tables_own_last : 0);
1231  }
1232  void chop_off_not_own_tables()
1233  {
1234  if (query_tables_own_last)
1235  {
1236  *query_tables_own_last= 0;
1237  query_tables_last= query_tables_own_last;
1238  query_tables_own_last= 0;
1239  }
1240  }
1241 
1243  TABLE_LIST *last_table()
1244  {
1245  /* Don't use offsetof() macro in order to avoid warnings. */
1246  return query_tables ?
1247  (TABLE_LIST*) ((char*) query_tables_last -
1248  ((char*) &(query_tables->next_global) -
1249  (char*) query_tables)) :
1250  0;
1251  }
1252 
1260  enum enum_binlog_stmt_unsafe {
1265  BINLOG_STMT_UNSAFE_LIMIT= 0,
1270  BINLOG_STMT_UNSAFE_INSERT_DELAYED,
1275  BINLOG_STMT_UNSAFE_SYSTEM_TABLE,
1285  BINLOG_STMT_UNSAFE_AUTOINC_COLUMNS,
1289  BINLOG_STMT_UNSAFE_UDF,
1294  BINLOG_STMT_UNSAFE_SYSTEM_VARIABLE,
1298  BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION,
1299 
1305  BINLOG_STMT_UNSAFE_NONTRANS_AFTER_TRANS,
1306 
1311  BINLOG_STMT_UNSAFE_MULTIPLE_ENGINES_AND_SELF_LOGGING_ENGINE,
1312 
1317  BINLOG_STMT_UNSAFE_MIXED_STATEMENT,
1318 
1324  BINLOG_STMT_UNSAFE_INSERT_IGNORE_SELECT,
1325 
1331  BINLOG_STMT_UNSAFE_INSERT_SELECT_UPDATE,
1332 
1338  BINLOG_STMT_UNSAFE_WRITE_AUTOINC_SELECT,
1339 
1345  BINLOG_STMT_UNSAFE_REPLACE_SELECT,
1346 
1352  BINLOG_STMT_UNSAFE_CREATE_IGNORE_SELECT,
1353 
1359  BINLOG_STMT_UNSAFE_CREATE_REPLACE_SELECT,
1360 
1367  BINLOG_STMT_UNSAFE_CREATE_SELECT_AUTOINC,
1368 
1374  BINLOG_STMT_UNSAFE_UPDATE_IGNORE,
1375 
1380  BINLOG_STMT_UNSAFE_INSERT_TWO_KEYS,
1381 
1386  BINLOG_STMT_UNSAFE_AUTOINC_NOT_FIRST,
1387 
1388  /* The last element of this enumeration type. */
1389  BINLOG_STMT_UNSAFE_COUNT
1390  };
1395  static const int BINLOG_STMT_UNSAFE_ALL_FLAGS=
1396  ((1 << BINLOG_STMT_UNSAFE_COUNT) - 1);
1397 
1401  static const int binlog_stmt_unsafe_errcode[BINLOG_STMT_UNSAFE_COUNT];
1402 
1409  inline bool is_stmt_unsafe() const {
1410  return get_stmt_unsafe_flags() != 0;
1411  }
1412 
1420  inline void set_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type) {
1421  DBUG_ENTER("set_stmt_unsafe");
1422  DBUG_ASSERT(unsafe_type >= 0 && unsafe_type < BINLOG_STMT_UNSAFE_COUNT);
1423  binlog_stmt_flags|= (1U << unsafe_type);
1424  DBUG_VOID_RETURN;
1425  }
1426 
1435  inline void set_stmt_unsafe_flags(uint32 flags) {
1436  DBUG_ENTER("set_stmt_unsafe_flags");
1437  DBUG_ASSERT((flags & ~BINLOG_STMT_UNSAFE_ALL_FLAGS) == 0);
1438  binlog_stmt_flags|= flags;
1439  DBUG_VOID_RETURN;
1440  }
1441 
1448  inline uint32 get_stmt_unsafe_flags() const {
1449  DBUG_ENTER("get_stmt_unsafe_flags");
1450  DBUG_RETURN(binlog_stmt_flags & BINLOG_STMT_UNSAFE_ALL_FLAGS);
1451  }
1452 
1458  inline void clear_stmt_unsafe() {
1459  DBUG_ENTER("clear_stmt_unsafe");
1460  binlog_stmt_flags&= ~BINLOG_STMT_UNSAFE_ALL_FLAGS;
1461  DBUG_VOID_RETURN;
1462  }
1463 
1470  inline bool is_stmt_row_injection() const {
1471  return binlog_stmt_flags &
1472  (1U << (BINLOG_STMT_UNSAFE_COUNT + BINLOG_STMT_TYPE_ROW_INJECTION));
1473  }
1474 
1480  inline void set_stmt_row_injection() {
1481  DBUG_ENTER("set_stmt_row_injection");
1482  binlog_stmt_flags|=
1483  (1U << (BINLOG_STMT_UNSAFE_COUNT + BINLOG_STMT_TYPE_ROW_INJECTION));
1484  DBUG_VOID_RETURN;
1485  }
1486 
1487  enum enum_stmt_accessed_table
1488  {
1489  /*
1490  If a transactional table is about to be read. Note that
1491  a write implies a read.
1492  */
1493  STMT_READS_TRANS_TABLE= 0,
1494  /*
1495  If a non-transactional table is about to be read. Note that
1496  a write implies a read.
1497  */
1498  STMT_READS_NON_TRANS_TABLE,
1499  /*
1500  If a temporary transactional table is about to be read. Note
1501  that a write implies a read.
1502  */
1503  STMT_READS_TEMP_TRANS_TABLE,
1504  /*
1505  If a temporary non-transactional table is about to be read. Note
1506  that a write implies a read.
1507  */
1508  STMT_READS_TEMP_NON_TRANS_TABLE,
1509  /*
1510  If a transactional table is about to be updated.
1511  */
1512  STMT_WRITES_TRANS_TABLE,
1513  /*
1514  If a non-transactional table is about to be updated.
1515  */
1516  STMT_WRITES_NON_TRANS_TABLE,
1517  /*
1518  If a temporary transactional table is about to be updated.
1519  */
1520  STMT_WRITES_TEMP_TRANS_TABLE,
1521  /*
1522  If a temporary non-transactional table is about to be updated.
1523  */
1524  STMT_WRITES_TEMP_NON_TRANS_TABLE,
1525  /*
1526  The last element of the enumeration. Please, if necessary add
1527  anything before this.
1528  */
1529  STMT_ACCESS_TABLE_COUNT
1530  };
1531 
1532 #ifndef DBUG_OFF
1533  static inline const char *stmt_accessed_table_string(enum_stmt_accessed_table accessed_table)
1534  {
1535  switch (accessed_table)
1536  {
1537  case STMT_READS_TRANS_TABLE:
1538  return "STMT_READS_TRANS_TABLE";
1539  break;
1540  case STMT_READS_NON_TRANS_TABLE:
1541  return "STMT_READS_NON_TRANS_TABLE";
1542  break;
1543  case STMT_READS_TEMP_TRANS_TABLE:
1544  return "STMT_READS_TEMP_TRANS_TABLE";
1545  break;
1546  case STMT_READS_TEMP_NON_TRANS_TABLE:
1547  return "STMT_READS_TEMP_NON_TRANS_TABLE";
1548  break;
1549  case STMT_WRITES_TRANS_TABLE:
1550  return "STMT_WRITES_TRANS_TABLE";
1551  break;
1552  case STMT_WRITES_NON_TRANS_TABLE:
1553  return "STMT_WRITES_NON_TRANS_TABLE";
1554  break;
1555  case STMT_WRITES_TEMP_TRANS_TABLE:
1556  return "STMT_WRITES_TEMP_TRANS_TABLE";
1557  break;
1558  case STMT_WRITES_TEMP_NON_TRANS_TABLE:
1559  return "STMT_WRITES_TEMP_NON_TRANS_TABLE";
1560  break;
1561  case STMT_ACCESS_TABLE_COUNT:
1562  default:
1563  DBUG_ASSERT(0);
1564  break;
1565  }
1566  MY_ASSERT_UNREACHABLE();
1567  return "";
1568  }
1569 #endif /* DBUG */
1570 
1571  #define BINLOG_DIRECT_ON 0xF0 /* unsafe when
1572  --binlog-direct-non-trans-updates
1573  is ON */
1574 
1575  #define BINLOG_DIRECT_OFF 0xF /* unsafe when
1576  --binlog-direct-non-trans-updates
1577  is OFF */
1578 
1579  #define TRX_CACHE_EMPTY 0x33 /* unsafe when trx-cache is empty */
1580 
1581  #define TRX_CACHE_NOT_EMPTY 0xCC /* unsafe when trx-cache is not empty */
1582 
1583  #define IL_LT_REPEATABLE 0xAA /* unsafe when < ISO_REPEATABLE_READ */
1584 
1585  #define IL_GTE_REPEATABLE 0x55 /* unsafe when >= ISO_REPEATABLE_READ */
1586 
1594  inline void set_stmt_accessed_table(enum_stmt_accessed_table accessed_table)
1595  {
1596  DBUG_ENTER("LEX::set_stmt_accessed_table");
1597 
1598  DBUG_ASSERT(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
1599  stmt_accessed_table_flag |= (1U << accessed_table);
1600 
1601  DBUG_VOID_RETURN;
1602  }
1603 
1615  inline bool stmt_accessed_table(enum_stmt_accessed_table accessed_table)
1616  {
1617  DBUG_ENTER("LEX::stmt_accessed_table");
1618 
1619  DBUG_ASSERT(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
1620 
1621  DBUG_RETURN((stmt_accessed_table_flag & (1U << accessed_table)) != 0);
1622  }
1623 
1624  /*
1625  Checks if a mixed statement is unsafe.
1626 
1627 
1628  @param in_multi_stmt_transaction_mode defines if there is an on-going
1629  multi-transactional statement.
1630  @param binlog_direct defines if --binlog-direct-non-trans-updates is
1631  active.
1632  @param trx_cache_is_not_empty defines if the trx-cache is empty or not.
1633  @param trx_isolation defines the isolation level.
1634 
1635  @return
1636  @retval TRUE if the mixed statement is unsafe
1637  @retval FALSE otherwise
1638  */
1639  inline bool is_mixed_stmt_unsafe(bool in_multi_stmt_transaction_mode,
1640  bool binlog_direct,
1641  bool trx_cache_is_not_empty,
1642  uint tx_isolation)
1643  {
1644  bool unsafe= FALSE;
1645 
1646  if (in_multi_stmt_transaction_mode)
1647  {
1648  uint condition=
1649  (binlog_direct ? BINLOG_DIRECT_ON : BINLOG_DIRECT_OFF) &
1650  (trx_cache_is_not_empty ? TRX_CACHE_NOT_EMPTY : TRX_CACHE_EMPTY) &
1651  (tx_isolation >= ISO_REPEATABLE_READ ? IL_GTE_REPEATABLE : IL_LT_REPEATABLE);
1652 
1653  unsafe= (binlog_unsafe_map[stmt_accessed_table_flag] & condition);
1654 
1655 #if !defined(DBUG_OFF)
1656  DBUG_PRINT("LEX::is_mixed_stmt_unsafe", ("RESULT %02X %02X %02X\n", condition,
1657  binlog_unsafe_map[stmt_accessed_table_flag],
1658  (binlog_unsafe_map[stmt_accessed_table_flag] & condition)));
1659 
1660  int type_in= 0;
1661  for (; type_in < STMT_ACCESS_TABLE_COUNT; type_in++)
1662  {
1663  if (stmt_accessed_table((enum_stmt_accessed_table) type_in))
1664  DBUG_PRINT("LEX::is_mixed_stmt_unsafe", ("ACCESSED %s ",
1665  stmt_accessed_table_string((enum_stmt_accessed_table) type_in)));
1666  }
1667 #endif
1668  }
1669 
1670  if (stmt_accessed_table(STMT_WRITES_NON_TRANS_TABLE) &&
1671  stmt_accessed_table(STMT_READS_TRANS_TABLE) &&
1672  tx_isolation < ISO_REPEATABLE_READ)
1673  unsafe= TRUE;
1674  else if (stmt_accessed_table(STMT_WRITES_TEMP_NON_TRANS_TABLE) &&
1675  stmt_accessed_table(STMT_READS_TRANS_TABLE) &&
1676  tx_isolation < ISO_REPEATABLE_READ)
1677  unsafe= TRUE;
1678 
1679  return(unsafe);
1680  }
1681 
1686  bool uses_stored_routines() const
1687  { return sroutines_list.elements != 0; }
1688 
1689 private:
1690 
1696  enum enum_binlog_stmt_type {
1701  BINLOG_STMT_TYPE_ROW_INJECTION = 0,
1702 
1704  BINLOG_STMT_TYPE_COUNT
1705  };
1706 
1722  uint32 binlog_stmt_flags;
1723 
1728  uint32 stmt_accessed_table_flag;
1729 };
1730 
1731 
1732 /*
1733  st_parsing_options contains the flags for constructions that are
1734  allowed in the current statement.
1735 */
1736 
1737 struct st_parsing_options
1738 {
1739  bool allows_variable;
1740  bool allows_select_into;
1741  bool allows_select_procedure;
1742  bool allows_derived;
1743 
1744  st_parsing_options() { reset(); }
1745  void reset();
1746 };
1747 
1748 
1752 enum enum_comment_state
1753 {
1757  NO_COMMENT,
1762  PRESERVE_COMMENT,
1769  DISCARD_COMMENT
1770 };
1771 
1772 
1787 class Lex_input_stream
1788 {
1789 public:
1790  Lex_input_stream()
1791  {
1792  }
1793 
1794  ~Lex_input_stream()
1795  {
1796  }
1797 
1804  bool init(THD *thd, char *buff, unsigned int length);
1805 
1806  void reset(char *buff, unsigned int length);
1807 
1815  void set_echo(bool echo)
1816  {
1817  m_echo= echo;
1818  }
1819 
1820  void save_in_comment_state()
1821  {
1822  m_echo_saved= m_echo;
1823  in_comment_saved= in_comment;
1824  }
1825 
1826  void restore_in_comment_state()
1827  {
1828  m_echo= m_echo_saved;
1829  in_comment= in_comment_saved;
1830  }
1831 
1836  void skip_binary(int n)
1837  {
1838  if (m_echo)
1839  {
1840  memcpy(m_cpp_ptr, m_ptr, n);
1841  m_cpp_ptr += n;
1842  }
1843  m_ptr += n;
1844  }
1845 
1850  unsigned char yyGet()
1851  {
1852  char c= *m_ptr++;
1853  if (m_echo)
1854  *m_cpp_ptr++ = c;
1855  return c;
1856  }
1857 
1862  unsigned char yyGetLast()
1863  {
1864  return m_ptr[-1];
1865  }
1866 
1870  unsigned char yyPeek()
1871  {
1872  return m_ptr[0];
1873  }
1874 
1879  unsigned char yyPeekn(int n)
1880  {
1881  return m_ptr[n];
1882  }
1883 
1889  void yyUnget()
1890  {
1891  m_ptr--;
1892  if (m_echo)
1893  m_cpp_ptr--;
1894  }
1895 
1899  void yySkip()
1900  {
1901  if (m_echo)
1902  *m_cpp_ptr++ = *m_ptr++;
1903  else
1904  m_ptr++;
1905  }
1906 
1911  void yySkipn(int n)
1912  {
1913  if (m_echo)
1914  {
1915  memcpy(m_cpp_ptr, m_ptr, n);
1916  m_cpp_ptr += n;
1917  }
1918  m_ptr += n;
1919  }
1920 
1927  char *yyUnput(char ch)
1928  {
1929  *--m_ptr= ch;
1930  if (m_echo)
1931  m_cpp_ptr--;
1932  return m_ptr;
1933  }
1934 
1942  char *cpp_inject(char ch)
1943  {
1944  *m_cpp_ptr= ch;
1945  return ++m_cpp_ptr;
1946  }
1947 
1952  bool eof()
1953  {
1954  return (m_ptr >= m_end_of_query);
1955  }
1956 
1962  bool eof(int n)
1963  {
1964  return ((m_ptr + n) >= m_end_of_query);
1965  }
1966 
1968  const char *get_buf()
1969  {
1970  return m_buf;
1971  }
1972 
1974  const char *get_cpp_buf()
1975  {
1976  return m_cpp_buf;
1977  }
1978 
1980  const char *get_end_of_query()
1981  {
1982  return m_end_of_query;
1983  }
1984 
1986  void start_token()
1987  {
1988  m_tok_start_prev= m_tok_start;
1989  m_tok_start= m_ptr;
1990  m_tok_end= m_ptr;
1991 
1992  m_cpp_tok_start_prev= m_cpp_tok_start;
1993  m_cpp_tok_start= m_cpp_ptr;
1994  m_cpp_tok_end= m_cpp_ptr;
1995  }
1996 
2001  void restart_token()
2002  {
2003  m_tok_start= m_ptr;
2004  m_cpp_tok_start= m_cpp_ptr;
2005  }
2006 
2008  const char *get_tok_start()
2009  {
2010  return m_tok_start;
2011  }
2012 
2014  const char *get_cpp_tok_start()
2015  {
2016  return m_cpp_tok_start;
2017  }
2018 
2020  const char *get_tok_end()
2021  {
2022  return m_tok_end;
2023  }
2024 
2026  const char *get_cpp_tok_end()
2027  {
2028  return m_cpp_tok_end;
2029  }
2030 
2032  const char *get_tok_start_prev()
2033  {
2034  return m_tok_start_prev;
2035  }
2036 
2038  const char *get_ptr()
2039  {
2040  return m_ptr;
2041  }
2042 
2044  const char *get_cpp_ptr()
2045  {
2046  return m_cpp_ptr;
2047  }
2048 
2050  uint yyLength()
2051  {
2052  /*
2053  The assumption is that the lexical analyser is always 1 character ahead,
2054  which the -1 account for.
2055  */
2056  DBUG_ASSERT(m_ptr > m_tok_start);
2057  return (uint) ((m_ptr - m_tok_start) - 1);
2058  }
2059 
2061  const char *get_body_utf8_str()
2062  {
2063  return m_body_utf8;
2064  }
2065 
2067  uint get_body_utf8_length()
2068  {
2069  return (uint) (m_body_utf8_ptr - m_body_utf8);
2070  }
2071 
2072  void body_utf8_start(THD *thd, const char *begin_ptr);
2073  void body_utf8_append(const char *ptr);
2074  void body_utf8_append(const char *ptr, const char *end_ptr);
2075  void body_utf8_append_literal(THD *thd,
2076  const LEX_STRING *txt,
2077  const CHARSET_INFO *txt_cs,
2078  const char *end_ptr);
2079 
2081  THD *m_thd;
2082 
2084  uint yylineno;
2085 
2087  uint yytoklen;
2088 
2090  LEX_YYSTYPE yylval;
2091 
2098  int lookahead_token;
2099 
2101  LEX_YYSTYPE lookahead_yylval;
2102 
2103 private:
2105  char *m_ptr;
2106 
2108  const char *m_tok_start;
2109 
2111  const char *m_tok_end;
2112 
2114  const char *m_end_of_query;
2115 
2117  const char *m_tok_start_prev;
2118 
2120  const char *m_buf;
2121 
2123  uint m_buf_length;
2124 
2126  bool m_echo;
2127  bool m_echo_saved;
2128 
2130  char *m_cpp_buf;
2131 
2133  char *m_cpp_ptr;
2134 
2139  const char *m_cpp_tok_start;
2140 
2145  const char *m_cpp_tok_start_prev;
2146 
2151  const char *m_cpp_tok_end;
2152 
2154  char *m_body_utf8;
2155 
2157  char *m_body_utf8_ptr;
2158 
2163  const char *m_cpp_utf8_processed_ptr;
2164 
2165 public:
2166 
2168  enum my_lex_states next_state;
2169 
2174  const char *found_semicolon;
2175 
2177  uchar tok_bitmap;
2178 
2180  bool ignore_space;
2181 
2186  bool stmt_prepare_mode;
2190  bool multi_statements;
2191 
2193  enum_comment_state in_comment;
2194  enum_comment_state in_comment_saved;
2195 
2202  const char *m_cpp_text_start;
2203 
2210  const char *m_cpp_text_end;
2211 
2217  CHARSET_INFO *m_underscore_cs;
2218 
2222  PSI_digest_locker* m_digest_psi;
2223 };
2224 
2225 
2230 struct Proc_analyse_params: public Sql_alloc
2231 {
2232  uint max_tree_elements; //< maximum number of distinct values per column
2233  uint max_treemem; //< maximum amount of memory to allocate per column
2234 
2235  Proc_analyse_params()
2236  : max_tree_elements(256),
2237  max_treemem(8192)
2238  {}
2239 };
2240 
2241 
2242 /* The state of the lex parsing. This is saved in the THD struct */
2243 
2244 struct LEX: public Query_tables_list
2245 {
2246  SELECT_LEX_UNIT unit; /* most upper unit */
2247  SELECT_LEX select_lex; /* first SELECT_LEX */
2248  /* current SELECT_LEX in parsing */
2249  SELECT_LEX *current_select;
2250  /* list of all SELECT_LEX */
2251  SELECT_LEX *all_selects_list;
2252 
2253  char *length,*dec,*change;
2254  LEX_STRING name;
2255  char *help_arg;
2256  char* to_log; /* For PURGE MASTER LOGS TO */
2257  char* x509_subject,*x509_issuer,*ssl_cipher;
2258  String *wild;
2259  sql_exchange *exchange;
2260  select_result *result;
2261  Item *default_value, *on_update_value;
2262  LEX_STRING comment, ident;
2263  LEX_USER *grant_user;
2264  XID *xid;
2265  THD *thd;
2266 
2267  /* maintain a list of used plugins for this LEX */
2268  DYNAMIC_ARRAY plugins;
2269  plugin_ref plugins_static_buffer[INITIAL_LEX_PLUGIN_LIST_SIZE];
2270 
2271  const CHARSET_INFO *charset;
2272  bool text_string_is_7bit;
2273  /* store original leaf_tables for INSERT SELECT and PS/SP */
2274  TABLE_LIST *leaf_tables_insert;
2275 
2277  LEX_STRING create_view_select;
2278 
2280  const char* raw_trg_on_table_name_begin;
2282  const char* raw_trg_on_table_name_end;
2283 
2284  /* Partition info structure filled in by PARTITION BY parse part */
2285  partition_info *part_info;
2286 
2287  /*
2288  The definer of the object being created (view, trigger, stored routine).
2289  I.e. the value of DEFINER clause.
2290  */
2291  LEX_USER *definer;
2292 
2293  List<Key_part_spec> col_list;
2294  List<Key_part_spec> ref_list;
2295  /*
2296  A list of strings is maintained to store the SET clause command user strings
2297  which are specified in load data operation. This list will be used
2298  during the reconstruction of "load data" statement at the time of writing
2299  to binary log.
2300  */
2301  List<String> load_set_str_list;
2302  List<String> interval_list;
2303  List<LEX_USER> users_list;
2304  List<LEX_COLUMN> columns;
2305  List<Item> *insert_list,field_list,value_list,update_list;
2306  List<List_item> many_values;
2307  List<set_var_base> var_list;
2308  List<Item_func_set_user_var> set_var_list; // in-query assignment list
2309  List<Item_param> param_list;
2310  List<LEX_STRING> view_list; // view list (list of field names in view)
2311  /*
2312  A stack of name resolution contexts for the query. This stack is used
2313  at parse time to set local name resolution contexts for various parts
2314  of a query. For example, in a JOIN ... ON (some_condition) clause the
2315  Items in 'some_condition' must be resolved only against the operands
2316  of the the join, and not against the whole clause. Similarly, Items in
2317  subqueries should be resolved against the subqueries (and outer queries).
2318  The stack is used in the following way: when the parser detects that
2319  all Items in some clause need a local context, it creates a new context
2320  and pushes it on the stack. All newly created Items always store the
2321  top-most context in the stack. Once the parser leaves the clause that
2322  required a local context, the parser pops the top-most context.
2323  */
2324  List<Name_resolution_context> context_stack;
2325 
2329  Proc_analyse_params *proc_analyse;
2330  SQL_I_List<TABLE_LIST> auxiliary_table_list, save_list;
2331  Create_field *last_field;
2332  Item_sum *in_sum_func;
2333  udf_func udf;
2334  HA_CHECK_OPT check_opt; // check/repair options
2335  HA_CREATE_INFO create_info;
2336  KEY_CREATE_INFO key_create_info;
2337  LEX_MASTER_INFO mi; // used by CHANGE MASTER
2338  LEX_SLAVE_CONNECTION slave_connection;
2339  LEX_SERVER_OPTIONS server_options;
2340  USER_RESOURCES mqh;
2341  LEX_RESET_SLAVE reset_slave_info;
2342  ulong type;
2343  /*
2344  This variable is used in post-parse stage to declare that sum-functions,
2345  or functions which have sense only if GROUP BY is present, are allowed.
2346  For example in a query
2347  SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
2348  MIN(i) in the WHERE clause is not allowed in the opposite to MIN(i)
2349  in the HAVING clause. Due to possible nesting of select construct
2350  the variable can contain 0 or 1 for each nest level.
2351  */
2352  nesting_map allow_sum_func;
2353 
2354  Sql_cmd *m_sql_cmd;
2355 
2356  /*
2357  Usually `expr` rule of yacc is quite reused but some commands better
2358  not support subqueries which comes standard with this rule, like
2359  KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
2360  syntax error back.
2361  */
2362  bool expr_allows_subselect;
2363 
2364  enum SSL_type ssl_type; /* defined in violite.h */
2365  enum enum_duplicates duplicates;
2366  enum enum_tx_isolation tx_isolation;
2367  enum xa_option_words xa_opt;
2368  enum enum_var_type option_type;
2369  enum enum_view_create_mode create_view_mode;
2370  enum enum_drop_mode drop_mode;
2371 
2372  uint profile_query_id;
2373  uint profile_options;
2374  uint uint_geom_type;
2375  uint grant, grant_tot_col, which_columns;
2376  enum Foreign_key::fk_match_opt fk_match_option;
2377  enum Foreign_key::fk_option fk_update_opt;
2378  enum Foreign_key::fk_option fk_delete_opt;
2379  uint slave_thd_opt, start_transaction_opt;
2380  int nest_level;
2381  uint8 describe;
2382  /*
2383  A flag that indicates what kinds of derived tables are present in the
2384  query (0 if no derived tables, otherwise a combination of flags
2385  DERIVED_SUBQUERY and DERIVED_VIEW).
2386  */
2387  uint8 derived_tables;
2388  uint8 create_view_algorithm;
2389  uint8 create_view_check;
2390  uint8 context_analysis_only;
2391  bool drop_if_exists, drop_temporary, local_file, one_shot_set;
2392  bool autocommit;
2393  bool verbose, no_write_to_binlog;
2394 
2395  enum enum_yes_no_unknown tx_chain, tx_release;
2396  bool safe_to_cache_query;
2397  bool subqueries, ignore;
2398  st_parsing_options parsing_options;
2399  Alter_info alter_info;
2400  /*
2401  For CREATE TABLE statement last element of table list which is not
2402  part of SELECT or LIKE part (i.e. either element for table we are
2403  creating or last of tables referenced by foreign keys).
2404  */
2405  TABLE_LIST *create_last_non_select_table;
2406  /* Prepared statements SQL syntax:*/
2407  LEX_STRING prepared_stmt_name; /* Statement name (in all queries) */
2408  /*
2409  Prepared statement query text or name of variable that holds the
2410  prepared statement (in PREPARE ... queries)
2411  */
2412  LEX_STRING prepared_stmt_code;
2413  /* If true, prepared_stmt_code is a name of variable that holds the query */
2414  bool prepared_stmt_code_is_varref;
2415  /* Names of user variables holding parameters (in EXECUTE) */
2416  List<LEX_STRING> prepared_stmt_params;
2417  sp_head *sphead;
2418  sp_name *spname;
2419  bool sp_lex_in_use; /* Keep track on lex usage in SPs for error handling */
2420  bool all_privileges;
2421  bool proxy_priv;
2422  bool is_change_password;
2423  /*
2424  Temporary variable to distinguish SET PASSWORD command from others
2425  SQLCOM_SET_OPTION commands. Should be removed when WL#6409 is
2426  introduced.
2427  */
2428  bool is_set_password_sql;
2429  bool contains_plaintext_password;
2430 
2431 private:
2432  bool m_broken;
2433 
2434 
2435  sp_pcontext *sp_current_parsing_ctx;
2436 
2437 public:
2438 
2439  bool is_broken() const { return m_broken; }
2448  void mark_broken(bool broken= true)
2449  {
2450  if (broken)
2451  {
2452  /*
2453  "OPEN <cursor>" cannot be re-prepared if the cursor uses no tables
2454  ("SELECT FROM DUAL"). Indeed in that case cursor_query is left empty
2455  in constructions of sp_instr_cpush, and thus
2456  sp_lex_instr::parse_expr() cannot re-prepare. So we mark the statement
2457  as broken only if tables are used.
2458  */
2459  if (is_metadata_used())
2460  m_broken= true;
2461  }
2462  else
2463  m_broken= false;
2464  }
2465 
2466  sp_pcontext *get_sp_current_parsing_ctx()
2467  { return sp_current_parsing_ctx; }
2468 
2469  void set_sp_current_parsing_ctx(sp_pcontext *ctx)
2470  { sp_current_parsing_ctx= ctx; }
2471 
2474  bool is_metadata_used() const
2475  { return query_tables != NULL || sroutines.records > 0; }
2476 
2477 public:
2478  st_sp_chistics sp_chistics;
2479 
2480  Event_parse_data *event_parse_data;
2481 
2482  bool only_view; /* used for SHOW CREATE TABLE/VIEW */
2483  /*
2484  field_list was created for view and should be removed before PS/SP
2485  rexecuton
2486  */
2487  bool empty_field_list_on_rset;
2488  /*
2489  view created to be run from definer (standard behaviour)
2490  */
2491  uint8 create_view_suid;
2492 
2493  /*
2494  stmt_definition_begin is intended to point to the next word after
2495  DEFINER-clause in the following statements:
2496  - CREATE TRIGGER (points to "TRIGGER");
2497  - CREATE PROCEDURE (points to "PROCEDURE");
2498  - CREATE FUNCTION (points to "FUNCTION" or "AGGREGATE");
2499  - CREATE EVENT (points to "EVENT")
2500 
2501  This pointer is required to add possibly omitted DEFINER-clause to the
2502  DDL-statement before dumping it to the binlog.
2503 
2504  keyword_delayed_begin_offset is the offset to the beginning of the DELAYED
2505  keyword in INSERT DELAYED statement. keyword_delayed_end_offset is the
2506  offset to the character right after the DELAYED keyword.
2507  */
2508  union {
2509  const char *stmt_definition_begin;
2510  uint keyword_delayed_begin_offset;
2511  };
2512 
2513  union {
2514  const char *stmt_definition_end;
2515  uint keyword_delayed_end_offset;
2516  };
2517 
2524  bool use_only_table_context;
2525 
2526  /*
2527  Reference to a struct that contains information in various commands
2528  to add/create/drop/change table spaces.
2529  */
2530  st_alter_tablespace *alter_tablespace_info;
2531 
2532  bool escape_used;
2533  bool is_lex_started; /* If lex_start() did run. For debugging. */
2534 
2535  /*
2536  The set of those tables whose fields are referenced in all subqueries
2537  of the query.
2538  TODO: possibly this it is incorrect to have used tables in LEX because
2539  with subquery, it is not clear what does the field mean. To fix this
2540  we should aggregate used tables information for selected expressions
2541  into the select_lex.
2542  */
2543  table_map used_tables;
2544 
2545  class Explain_format *explain_format;
2546 
2547  LEX();
2548 
2549  virtual ~LEX()
2550  {
2551  destroy_query_tables_list();
2552  plugin_unlock_list(NULL, (plugin_ref *)plugins.buffer, plugins.elements);
2553  delete_dynamic(&plugins);
2554  }
2555 
2556  inline bool is_ps_or_view_context_analysis()
2557  {
2558  return (context_analysis_only &
2559  (CONTEXT_ANALYSIS_ONLY_PREPARE |
2560  CONTEXT_ANALYSIS_ONLY_VIEW));
2561  }
2562 
2563  inline void uncacheable(uint8 cause)
2564  {
2565  safe_to_cache_query= 0;
2566 
2567  /*
2568  There are no sense to mark select_lex and union fields of LEX,
2569  but we should merk all subselects as uncacheable from current till
2570  most upper
2571  */
2572  SELECT_LEX *sl;
2573  SELECT_LEX_UNIT *un;
2574  for (sl= current_select, un= sl->master_unit();
2575  un != &unit;
2576  sl= sl->outer_select(), un= sl->master_unit())
2577  {
2578  sl->uncacheable|= cause;
2579  un->uncacheable|= cause;
2580  }
2581  }
2582  void set_trg_event_type_for_tables();
2583 
2584  TABLE_LIST *unlink_first_table(bool *link_to_local);
2585  void link_first_table_back(TABLE_LIST *first, bool link_to_local);
2586  void first_lists_tables_same();
2587 
2588  bool can_be_merged();
2589  bool can_use_merged();
2590  bool can_not_use_merged();
2591  bool only_view_structure();
2592  bool need_correct_ident();
2593  uint8 get_effective_with_check(TABLE_LIST *view);
2594  /*
2595  Is this update command where 'WHITH CHECK OPTION' clause is important
2596 
2597  SYNOPSIS
2598  LEX::which_check_option_applicable()
2599 
2600  RETURN
2601  TRUE have to take 'WHITH CHECK OPTION' clause into account
2602  FALSE 'WHITH CHECK OPTION' clause do not need
2603  */
2604  inline bool which_check_option_applicable()
2605  {
2606  switch (sql_command) {
2607  case SQLCOM_UPDATE:
2608  case SQLCOM_UPDATE_MULTI:
2609  case SQLCOM_INSERT:
2610  case SQLCOM_INSERT_SELECT:
2611  case SQLCOM_REPLACE:
2612  case SQLCOM_REPLACE_SELECT:
2613  case SQLCOM_LOAD:
2614  return TRUE;
2615  default:
2616  return FALSE;
2617  }
2618  }
2619 
2620  void cleanup_after_one_table_open();
2621 
2622  bool push_context(Name_resolution_context *context)
2623  {
2624  return context_stack.push_front(context);
2625  }
2626 
2627  void pop_context()
2628  {
2629  context_stack.pop();
2630  }
2631 
2632  bool copy_db_to(char **p_db, size_t *p_db_length) const;
2633 
2634  Name_resolution_context *current_context()
2635  {
2636  return context_stack.head();
2637  }
2638  /*
2639  Restore the LEX and THD in case of a parse error.
2640  */
2641  static void cleanup_lex_after_parse_error(THD *thd);
2642 
2643  void reset_n_backup_query_tables_list(Query_tables_list *backup);
2644  void restore_backup_query_tables_list(Query_tables_list *backup);
2645 
2646  bool table_or_sp_used();
2647  bool is_partition_management() const;
2648 
2656  bool is_single_level_stmt()
2657  {
2658  /*
2659  This check exploits the fact that the last added to all_select_list is
2660  on its top. So select_lex (as the first added) will be at the tail
2661  of the list.
2662  */
2663  if (&select_lex == all_selects_list && !sroutines.records)
2664  {
2665  DBUG_ASSERT(!all_selects_list->next_select_in_list());
2666  return TRUE;
2667  }
2668  return FALSE;
2669  }
2670 };
2671 
2672 
2678 class Set_signal_information
2679 {
2680 public:
2682  Set_signal_information() {}
2683 
2685  Set_signal_information(const Set_signal_information& set);
2686 
2688  ~Set_signal_information()
2689  {}
2690 
2692  void clear();
2693 
2699  Item *m_item[LAST_DIAG_SET_PROPERTY+1];
2700 };
2701 
2702 
2708 class Yacc_state
2709 {
2710 public:
2711  Yacc_state()
2712  {
2713  reset();
2714  }
2715 
2716  void reset()
2717  {
2718  yacc_yyss= NULL;
2719  yacc_yyvs= NULL;
2720  m_set_signal_info.clear();
2721  m_lock_type= TL_READ_DEFAULT;
2722  m_mdl_type= MDL_SHARED_READ;
2723  m_ha_rkey_mode= HA_READ_KEY_EXACT;
2724  }
2725 
2726  ~Yacc_state();
2727 
2732  void reset_before_substatement()
2733  {
2734  m_lock_type= TL_READ_DEFAULT;
2735  m_mdl_type= MDL_SHARED_READ;
2736  m_ha_rkey_mode= HA_READ_KEY_EXACT; /* Let us be future-proof. */
2737  }
2738 
2743  uchar *yacc_yyss;
2744 
2749  uchar *yacc_yyvs;
2750 
2755  Set_signal_information m_set_signal_info;
2756 
2774  thr_lock_type m_lock_type;
2775 
2780  enum_mdl_type m_mdl_type;
2781 
2783  enum ha_rkey_function m_ha_rkey_mode;
2784 
2785  /*
2786  TODO: move more attributes from the LEX structure here.
2787  */
2788 };
2789 
2796 class Parser_state
2797 {
2798 public:
2799  Parser_state()
2800  : m_yacc()
2801  {}
2802 
2809  bool init(THD *thd, char *buff, unsigned int length)
2810  {
2811  return m_lip.init(thd, buff, length);
2812  }
2813 
2814  ~Parser_state()
2815  {}
2816 
2817  Lex_input_stream m_lip;
2818  Yacc_state m_yacc;
2819 
2820  void reset(char *found_semicolon, unsigned int length)
2821  {
2822  m_lip.reset(found_semicolon, length);
2823  m_yacc.reset();
2824  }
2825 };
2826 
2827 
2828 struct st_lex_local: public LEX
2829 {
2830  static void *operator new(size_t size) throw()
2831  {
2832  return sql_alloc(size);
2833  }
2834  static void *operator new(size_t size, MEM_ROOT *mem_root) throw()
2835  {
2836  return (void*) alloc_root(mem_root, (uint) size);
2837  }
2838  static void operator delete(void *ptr,size_t size)
2839  { TRASH(ptr, size); }
2840  static void operator delete(void *ptr, MEM_ROOT *mem_root)
2841  { /* Never called */ }
2842 };
2843 
2844 extern void lex_init(void);
2845 extern void lex_free(void);
2846 extern void lex_start(THD *thd);
2847 extern void lex_end(LEX *lex);
2848 extern int MYSQLlex(void *arg, void *yythd);
2849 
2850 extern void trim_whitespace(const CHARSET_INFO *cs, LEX_STRING *str);
2851 
2852 extern bool is_lex_native_function(const LEX_STRING *name);
2853 
2858 void my_missing_function_error(const LEX_STRING &token, const char *name);
2859 bool is_keyword(const char *name, uint len);
2860 bool db_is_default_db(const char *db, size_t db_len, const THD *thd);
2861 
2862 #endif /* MYSQL_SERVER */
2863 #endif /* SQL_LEX_INCLUDED */