1
/* Copyright (C) 2000-2006 MySQL AB
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.
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.
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 */
17
@defgroup Semantic_Analysis Semantic Analysis
20
/* YACC and LEX Definitions */
22
/* These may not be declared yet */
26
class st_alter_tablespace;
30
The following hack is needed because mysql_yacc.cc does not define
31
YYSTYPE before including this file
37
#define LEX_YYSTYPE void *
40
#include "lex_symbol.h"
42
#define LEX_YYSTYPE YYSTYPE *
44
#define LEX_YYSTYPE void *
50
When a command is added here, be sure it's also added in mysqld.cc
51
in "struct show_var_st status_vars[]= {" ...
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
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,
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,
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,
94
SQLCOM_SHOW_CONTRIBUTORS,
95
SQLCOM_ALTER_DB_UPGRADE,
99
When a command is added here, be sure it's also added in mysqld.cc
100
in "struct show_var_st status_vars[]= {" ...
102
/* This should be the last !!! */
106
// describe/explain types
107
#define DESCRIBE_NORMAL 1
108
#define DESCRIBE_EXTENDED 2
112
enum enum_sp_data_access
114
SP_DEFAULT_ACCESS= 0,
121
const LEX_STRING sp_data_access_name[]=
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") }
130
#define DERIVED_NONE 0
131
#define DERIVED_SUBQUERY 1
135
DROP_DEFAULT, // mode is not specified
136
DROP_CASCADE, // CASCADE option
137
DROP_RESTRICT // RESTRICT option
140
typedef List<Item> List_item;
142
/* SERVERS CACHE CHANGES */
143
typedef struct st_lex_server_options
146
uint server_name_length;
147
char *server_name, *host, *db, *username, *password, *scheme, *socket, *owner;
148
} LEX_SERVER_OPTIONS;
150
typedef struct st_lex_master_info
152
char *host, *user, *password, *log_file_name;
153
uint port, connect_retry;
154
float heartbeat_period;
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
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;
171
UNSPECIFIED_TYPE,UNION_TYPE, INTERSECT_TYPE,
172
EXCEPT_TYPE, GLOBAL_OPTIONS_TYPE, DERIVED_TABLE_TYPE, OLAP_TYPE
177
UNSPECIFIED_OLAP_TYPE, CUBE_TYPE, ROLLUP_TYPE
180
enum tablespace_op_type
182
NO_TABLESPACE_OP, DISCARD_TABLESPACE, IMPORT_TABLESPACE
186
String names used to print a statement with index hints.
187
Keep in sync with index_hint_type.
189
extern const char * index_hint_type_name[];
190
typedef uchar index_clause_map;
193
Bits in index_clause_map : one for each possible FOR clause in
194
USE/FORCE/IGNORE INDEX index hint specification
196
#define INDEX_HINT_MASK_JOIN (1)
197
#define INDEX_HINT_MASK_GROUP (1 << 1)
198
#define INDEX_HINT_MASK_ORDER (1 << 2)
200
#define INDEX_HINT_MASK_ALL (INDEX_HINT_MASK_JOIN | INDEX_HINT_MASK_GROUP | \
201
INDEX_HINT_MASK_ORDER)
203
/* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint */
204
class Index_hint : public Sql_alloc
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;
212
The index name. Empty (str=NULL) name represents an empty list
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)
222
key_name.length= length;
225
void print(THD *thd, String *str);
229
The state of the lex parsing for selects
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
238
select_lex and unit are both inherited form select_lex_node
239
neighbors are two select_lex or units on the same level
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
247
- pointer to master (master), which is
249
- pointer to outer select_lex
250
If this is a select_lex
251
- pointer to outer unit structure for select
253
- pointer to slave (slave), which is either:
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)
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.
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.
270
For example for following query:
274
where table1.field IN (select * from table1_1_1 union
275
select * from table1_1_2)
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)
282
where table2_1_1.f1=table2.f2)
284
select * from table3;
286
we will have following structure:
288
select1: (select * from table1 ...)
289
select2: (select * from table2 ...)
290
select3: (select * from table3)
291
select1.1.1: (select * from table1_1_1)
296
select1 select2 select3
299
l||| |+---------------------------------+
300
a||| +---------------------------------+|
302
e||+-------------------------+ ||
304
unit1.1<+==================>unit1.2 unit2.1
306
select1.1.1 select 1.1.2 select1.2.1 select2.1.1
314
relation in main unit will be following:
318
select1 select2 select3
319
in the above picture)
322
|^^^^|fake_select_lex
323
|||||+--------------------------------------------+
324
||||+--------------------------------------------+|
325
|||+------------------------------+ ||
326
||+--------------+ | ||
328
V| neighbor | neighbor | master|V
329
select1<========>select2<========>select3 fake0
331
list of all select_lex will be following (as it will be constructed by
334
select1->select2->select3->select2.1.1->select 2.1.2->select2.1.1.1.1-+
336
+---------------------------------------------------------------------+
338
+->select1.1.1->select1.1.2
343
Base class for st_select_lex (SELECT_LEX) &
344
st_select_lex_unit (SELECT_LEX_UNIT)
348
class st_select_lex_unit;
349
class st_select_lex_node {
351
st_select_lex_node *next, **prev, /* neighbor list */
352
*master, *slave, /* vertical links */
353
*link_next, **link_prev; /* list of whole SELECT_LEX */
359
result of this query can't be cached, bit field, can be :
360
UNCACHEABLE_DEPENDENT
362
UNCACHEABLE_SIDEEFFECT
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) */
371
static void *operator new(size_t size)
373
return sql_alloc(size);
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);
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;
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,
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) {}
408
friend class st_select_lex_unit;
409
friend bool mysql_new_select(struct st_lex *lex, bool move_down);
413
typedef class st_select_lex_node SELECT_LEX_NODE;
416
SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group
423
class st_select_lex_unit: public st_select_lex_node {
425
TABLE_LIST result_table_list;
426
select_union *union_result;
427
TABLE *table; /* temporary table using for appending UNION results */
429
select_result *result;
430
ulonglong found_rows_for_union;
434
bool prepared, // prepare phase already performed for UNION (unit)
435
optimized, // optimize phase already performed for UNION (unit)
436
executed, // already executed
439
// list of fields which points to temporary table for union
440
List<Item> item_list;
442
list of types of items inside union (used for union & derived tables)
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
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.
454
Pointer to 'last' select or pointer to unit where stored
455
global parameters for union
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;
467
SELECT_LEX for hidden SELECT in onion which process global
470
st_select_lex *fake_select_lex;
472
st_select_lex *union_distinct; /* pointer to the last UNION DISTINCT */
473
bool describe; /* union exec() called for EXPLAIN */
476
st_select_lex_unit* master_unit();
477
st_select_lex* outer_select();
478
st_select_lex* first_select()
480
return my_reinterpret_cast(st_select_lex*)(slave);
482
st_select_lex_unit* next_unit()
484
return my_reinterpret_cast(st_select_lex_unit*)(next);
486
st_select_lex* return_after_parsing() { return return_to; }
487
void exclude_level();
491
bool prepare(THD *thd, select_result *result, ulong additional_options);
494
inline void unclean() { cleaned= 0; }
495
void reinit_exec_mechanism();
497
void print(String *str, enum_query_type query_type);
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 ();
507
friend void lex_start(THD *thd);
508
friend int subselect_union_engine::exec();
510
List<Item> *get_unit_column_types();
513
typedef class st_select_lex_unit SELECT_LEX_UNIT;
516
SELECT_LEX - store information of parsed SELECT statment
518
class st_select_lex: public st_select_lex_node
521
Name_resolution_context context;
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 */
531
/* FROM clause - points to the beginning of the TABLE_LIST::next_local 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;
538
Despite their names, the following are used in unions. This should
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;
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.
553
TABLE_LIST *leaf_tables;
554
const char *type; /* type of select for EXPLAIN */
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;
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
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 */
572
Number of fields used in select list or where clause of current select
573
and all inner subselects.
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 */
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
582
bool conds_processed_with_permanent_arena;
584
ulong table_join_options;
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 */
597
/* Number of Item_sum-derived objects in children and descendant SELECTs */
598
uint n_child_sum_items;
600
/* explicit LIMIT clause was used */
603
there are subquery in HAVING clause => we can't close tables before
604
query processing end even if we use temporary table
606
bool subquery_in_having;
607
/* TRUE <=> this SELECT is correlated w.r.t. some ancestor select */
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.
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;
632
List<udf_func> udf_list; /* udf function calls stack */
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
646
List<String> *prev_join_using;
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.
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.
656
uint8 full_group_by_flag;
659
st_select_lex_unit* master_unit();
660
st_select_lex_unit* first_inner_unit()
662
return (st_select_lex_unit*) slave;
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()
668
return (st_select_lex*) link_next;
670
st_select_lex_node** next_select_in_list_addr()
674
st_select_lex* return_after_parsing()
676
return master_unit()->return_after_parsing();
679
void mark_as_dependent(st_select_lex *last);
681
bool set_braces(bool value);
682
bool inc_in_sum_expr();
683
uint get_in_sum_expr();
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,
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()
705
order_list.elements= 0;
707
order_list.next= (uchar**) &order_list.first;
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
715
void cut_subtree() { slave= 0; }
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()
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,
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);
733
Destroy the used execution plan (JOIN) of this subtree (this
734
SELECT_LEX and all nested SELECT_LEXes and SELECT_LEX_UNITs).
738
Recursively cleanup the join of this select lex and of all nested
741
void cleanup_all_joins(bool full);
743
void set_index_hint_type(enum index_hint_type type, index_clause_map clause);
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())
749
bool add_index_hint (THD *thd, char *str, uint length);
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)
756
List<Index_hint> *hints= index_hints;
761
void clear_index_hints(void) { index_hints= NULL; }
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;
770
typedef class st_select_lex SELECT_LEX;
772
inline bool st_select_lex_unit::is_union ()
774
return first_select()->next_select() &&
775
first_select()->next_select()->linkage == UNION_TYPE;
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)
800
@brief Parsing data for CREATE or ALTER TABLE.
802
This structure contains a list of columns or indexes to be created,
809
List<Alter_drop> drop_list;
810
List<Alter_column> alter_list;
812
List<Create_field> create_list;
814
enum enum_enable_or_disable keys_onoff;
815
enum tablespace_op_type tablespace_op;
817
enum ha_build_method build_method;
818
Create_field *datetime_field;
819
bool error_if_not_empty;
824
keys_onoff(LEAVE_AS_IS),
825
tablespace_op(NO_TABLESPACE_OP),
827
build_method(HA_BUILD_DEFAULT),
828
datetime_field(NULL),
829
error_if_not_empty(FALSE)
839
keys_onoff= LEAVE_AS_IS;
840
tablespace_op= NO_TABLESPACE_OP;
842
build_method= HA_BUILD_DEFAULT;
844
error_if_not_empty= FALSE;
846
Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root);
848
Alter_info &operator=(const Alter_info &rhs); // not implemented
849
Alter_info(const Alter_info &rhs); // not implemented
852
enum xa_option_words {XA_NONE, XA_JOIN, XA_RESUME, XA_ONE_PHASE,
853
XA_SUSPEND, XA_FOR_MIGRATE};
855
extern const LEX_STRING null_lex_str;
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
865
Also used by st_lex::reset_n_backup/restore_backup_query_tables_list()
866
methods to save and restore this information.
869
class Query_tables_list
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;
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.
882
TABLE_LIST **query_tables_own_last;
884
Set of stored routines called by statement.
885
(Note that we use lazy-initialization for this hash).
887
enum { START_SROUTINES_HASH_SIZE= 16 };
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.
898
SQL_LIST sroutines_list;
899
uchar **sroutines_list_own_last;
900
uint sroutines_list_own_elements;
903
These constructor and destructor serve for creation/destruction
904
of Query_tables_list instances which are used as backup storage.
906
Query_tables_list() {}
907
~Query_tables_list() {}
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)
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.
922
void add_to_query_tables(TABLE_LIST *table)
924
*(table->prev_global= query_tables_last)= table;
925
query_tables_last= &table->next_global;
927
/* Return pointer to first not-own table in query-tables or 0 */
928
TABLE_LIST* first_not_own_table()
930
return ( query_tables_own_last ? *query_tables_own_last : 0);
932
void chop_off_not_own_tables()
934
if (query_tables_own_last)
936
*query_tables_own_last= 0;
937
query_tables_last= query_tables_own_last;
938
query_tables_own_last= 0;
943
Has the parser/scanner detected that this statement is unsafe?
945
inline bool is_stmt_unsafe() const {
946
return binlog_stmt_flags & (1U << BINLOG_STMT_FLAG_UNSAFE);
950
Flag the current (top-level) statement as unsafe.
952
The flag will be reset after the statement has finished.
955
inline void set_stmt_unsafe() {
956
binlog_stmt_flags|= (1U << BINLOG_STMT_FLAG_UNSAFE);
959
inline void clear_stmt_unsafe() {
960
binlog_stmt_flags&= ~(1U << BINLOG_STMT_FLAG_UNSAFE);
964
true if the parsed tree contains references to stored procedures
965
or functions, false otherwise
967
bool uses_stored_routines() const
968
{ return sroutines_list.elements != 0; }
971
enum enum_binlog_stmt_flag {
972
BINLOG_STMT_FLAG_UNSAFE,
973
BINLOG_STMT_FLAG_COUNT
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.
982
uint32 binlog_stmt_flags;
987
st_parsing_options contains the flags for constructions that are
988
allowed in the current statement.
991
struct st_parsing_options
993
bool allows_variable;
994
bool allows_select_into;
995
bool allows_select_procedure;
998
st_parsing_options() { reset(); }
1004
The state of the lexical parser, when parsing comments.
1006
enum enum_comment_state
1009
Not parsing comments.
1013
Parsing comments that need to be preserved.
1014
Typically, these are user comments '/' '*' ... '*' '/'.
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.
1028
@brief This class represents the character input stream consumed during
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.
1041
class Lex_input_stream
1044
Lex_input_stream(THD *thd, const char* buff, unsigned int length);
1045
~Lex_input_stream();
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.
1054
void set_echo(bool echo)
1060
Skip binary from the input stream.
1061
@param n number of bytes to accept.
1063
void skip_binary(int n)
1067
memcpy(m_cpp_ptr, m_ptr, n);
1074
Get a character, and advance in the stream.
1075
@return the next character to parse.
1086
Get the last character accepted.
1087
@return the last character accepted.
1095
Look at the next character to parse, but do not accept it.
1103
Look ahead at some character to parse.
1104
@param n offset of the character to look up
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.
1124
Accept a character, by advancing the input stream.
1129
*m_cpp_ptr++ = *m_ptr++;
1135
Accept multiple characters at once.
1136
@param n the number of characters to accept.
1142
memcpy(m_cpp_ptr, m_ptr, n);
1149
End of file indicator for the query text to parse.
1150
@return true if there are no more characters to parse
1154
return (m_ptr >= m_end_of_query);
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
1164
return ((m_ptr + n) >= m_end_of_query);
1167
/** Get the raw query buffer. */
1168
const char *get_buf()
1173
/** Get the pre-processed query buffer. */
1174
const char *get_cpp_buf()
1179
/** Get the end of the raw query buffer. */
1180
const char *get_end_of_query()
1182
return m_end_of_query;
1185
/** Mark the stream position as the start of a new token. */
1188
m_tok_start_prev= m_tok_start;
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;
1198
Adjust the starting position of the current token.
1199
This is used to compensate for starting whitespace.
1201
void restart_token()
1204
m_cpp_tok_start= m_cpp_ptr;
1207
/** Get the token start position, in the raw buffer. */
1208
const char *get_tok_start()
1213
/** Get the token start position, in the pre-processed buffer. */
1214
const char *get_cpp_tok_start()
1216
return m_cpp_tok_start;
1219
/** Get the token end position, in the raw buffer. */
1220
const char *get_tok_end()
1225
/** Get the token end position, in the pre-processed buffer. */
1226
const char *get_cpp_tok_end()
1228
return m_cpp_tok_end;
1231
/** Get the previous token start position, in the raw buffer. */
1232
const char *get_tok_start_prev()
1234
return m_tok_start_prev;
1237
/** Get the current stream pointer, in the raw buffer. */
1238
const char *get_ptr()
1243
/** Get the current stream pointer, in the pre-processed buffer. */
1244
const char *get_cpp_ptr()
1249
/** Get the length of the current token, in the raw buffer. */
1253
The assumption is that the lexical analyser is always 1 character ahead,
1254
which the -1 account for.
1256
DBUG_ASSERT(m_ptr > m_tok_start);
1257
return (uint) ((m_ptr - m_tok_start) - 1);
1260
/** Get the utf8-body string. */
1261
const char *get_body_utf8_str()
1266
/** Get the utf8-body length. */
1267
uint get_body_utf8_length()
1269
return m_body_utf8_ptr - m_body_utf8;
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);
1280
/** Current thread. */
1283
/** Current line number. */
1286
/** Length of the last token parsed. */
1289
/** Interface with bison, value of the last token parsed. */
1292
/** LALR(2) resolution, look ahead token.*/
1293
int lookahead_token;
1295
/** LALR(2) resolution, value of the look ahead token.*/
1296
LEX_YYSTYPE lookahead_yylval;
1299
/** Pointer to the current position in the raw input stream. */
1302
/** Starting position of the last token parsed, in the raw buffer. */
1303
const char *m_tok_start;
1305
/** Ending position of the previous token parsed, in the raw buffer. */
1306
const char *m_tok_end;
1308
/** End of the query text in the input stream, in the raw buffer. */
1309
const char *m_end_of_query;
1311
/** Starting position of the previous token parsed, in the raw buffer. */
1312
const char *m_tok_start_prev;
1314
/** Begining of the query text in the input stream, in the raw buffer. */
1317
/** Length of the raw buffer. */
1320
/** Echo the parsed stream to the pre-processed buffer. */
1323
/** Pre-processed buffer. */
1326
/** Pointer to the current position in the pre-processed input stream. */
1330
Starting position of the last token parsed,
1331
in the pre-processed buffer.
1333
const char *m_cpp_tok_start;
1336
Starting position of the previous token parsed,
1337
in the pre-procedded buffer.
1339
const char *m_cpp_tok_start_prev;
1342
Ending position of the previous token parsed,
1343
in the pre-processed buffer.
1345
const char *m_cpp_tok_end;
1347
/** UTF8-body buffer created during parsing. */
1350
/** Pointer to the current position in the UTF8-body buffer. */
1351
char *m_body_utf8_ptr;
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.
1357
const char *m_cpp_utf8_processed_ptr;
1361
/** Current state of the lexical analyser. */
1362
enum my_lex_states next_state;
1365
Position of ';' in the stream, to delimit multiple queries.
1366
This delimiter is in the raw buffer.
1368
const char *found_semicolon;
1370
/** Token character bitmaps, to detect 7bit strings. */
1373
/** SQL_MODE = IGNORE_SPACE. */
1377
TRUE if we're parsing a prepared statement: in this mode
1378
we should allow placeholders and disallow multi-statements.
1380
bool stmt_prepare_mode;
1382
/** State of the lexical analyser for comments. */
1383
enum_comment_state in_comment;
1386
Starting position of the TEXT_STRING or IDENT in the pre-processed
1389
NOTE: this member must be used within MYSQLlex() function only.
1391
const char *m_cpp_text_start;
1394
Ending position of the TEXT_STRING or IDENT in the pre-processed
1397
NOTE: this member must be used within MYSQLlex() function only.
1399
const char *m_cpp_text_end;
1402
Character set specified by the character-set-introducer.
1404
NOTE: this member must be used within MYSQLlex() function only.
1406
CHARSET_INFO *m_underscore_cs;
1410
/* The state of the lex parsing. This is saved in the THD struct */
1412
typedef struct st_lex : public Query_tables_list
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;
1421
char *length,*dec,*change;
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;
1428
sql_exchange *exchange;
1429
select_result *result;
1430
Item *default_value, *on_update_value;
1431
LEX_STRING comment, ident;
1433
uchar* yacc_yyss, *yacc_yyvs;
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];
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;
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)
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.
1468
List<Name_resolution_context> context_stack;
1469
List<LEX_STRING> db_list;
1471
SQL_LIST proc_list, auxiliary_table_list, save_list;
1472
Create_field *last_field;
1473
Item_sum *in_sum_func;
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;
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.
1491
nesting_map allow_sum_func;
1492
enum_sql_command sql_command;
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
1499
bool expr_allows_subselect;
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;
1506
enum ha_rkey_function ha_rkey_mode;
1507
enum xa_option_words xa_opt;
1508
bool lock_transactional; /* For LOCK TABLE ... IN ... MODE */
1510
enum enum_var_type option_type;
1511
enum enum_drop_mode drop_mode;
1513
uint profile_query_id;
1514
uint profile_options;
1515
enum ha_storage_media storage_type;
1516
enum column_format_type column_format;
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;
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.
1531
A flag that indicates what kinds of derived tables are present in the
1532
query (0 if no derived tables, otherwise DERIVED_SUBQUERY).
1534
uint8 derived_tables;
1535
bool drop_if_exists, drop_temporary, local_file, one_shot_set;
1537
bool verbose, no_write_to_binlog;
1539
bool tx_chain, tx_release;
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.
1547
bool view_prepare_mode;
1548
bool subqueries, ignore;
1549
st_parsing_options parsing_options;
1550
Alter_info alter_info;
1553
field_list was created for view and should be removed before PS/SP
1556
bool empty_field_list_on_rset;
1559
Pointers to part of LOAD DATA statement that should be rewritten
1560
during replication ("LOCAL 'filename' REPLACE INTO" part).
1562
const char *fname_start;
1563
const char *fname_end;
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()).
1571
bool use_only_table_context;
1574
Reference to a struct that contains information in various commands
1575
to add/create/drop/change table spaces.
1577
st_alter_tablespace *alter_tablespace_info;
1580
bool is_lex_started; /* If lex_start() did run. For debugging. */
1586
destroy_query_tables_list();
1587
plugin_unlock_list(NULL, (plugin_ref *)plugins.buffer, plugins.elements);
1588
delete_dynamic(&plugins);
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();
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);
1602
Is this update command where 'WHITH CHECK OPTION' clause is important
1605
st_lex::which_check_option_applicable()
1608
TRUE have to take 'WHITH CHECK OPTION' clause into account
1609
FALSE 'WHITH CHECK OPTION' clause do not need
1611
inline bool which_check_option_applicable()
1613
switch (sql_command) {
1615
case SQLCOM_UPDATE_MULTI:
1617
case SQLCOM_INSERT_SELECT:
1618
case SQLCOM_REPLACE:
1619
case SQLCOM_REPLACE_SELECT:
1627
void cleanup_after_one_table_open();
1629
bool push_context(Name_resolution_context *context)
1631
return context_stack.push_front(context);
1636
context_stack.pop();
1639
bool copy_db_to(char **p_db, size_t *p_db_length) const;
1641
Name_resolution_context *current_context()
1643
return context_stack.head();
1646
Restore the LEX and THD in case of a parse error.
1648
static void cleanup_lex_after_parse_error(THD *thd);
1650
void reset_n_backup_query_tables_list(Query_tables_list *backup);
1651
void restore_backup_query_tables_list(Query_tables_list *backup);
1653
bool table_or_sp_used();
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.
1662
bool is_single_level_stmt()
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
1669
if (&select_lex == all_selects_list && !sroutines.records)
1671
DBUG_ASSERT(!all_selects_list->next_select_in_list());
1678
struct st_lex_local: public st_lex
1680
static void *operator new(size_t size) throw()
1682
return sql_alloc(size);
1684
static void *operator new(size_t size, MEM_ROOT *mem_root) throw()
1686
return (void*) alloc_root(mem_root, (uint) size);
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 */ }
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);
1700
extern void trim_whitespace(CHARSET_INFO *cs, LEX_STRING *str);
1702
extern bool is_lex_native_function(const LEX_STRING *name);
1705
@} (End of group Semantic_Analysis)
1708
#endif /* MYSQL_SERVER */