24
24
@defgroup Semantic_Analysis Semantic Analysis
26
#include "drizzled/sql_udf.h"
27
#include "drizzled/name_resolution_context.h"
28
#include "drizzled/item/subselect.h"
29
#include "drizzled/item/param.h"
30
#include "drizzled/item/outer_ref.h"
31
#include "drizzled/table_list.h"
32
#include "drizzled/function/math/real.h"
33
#include "drizzled/alter_drop.h"
34
#include "drizzled/alter_column.h"
35
#include "drizzled/key.h"
36
#include "drizzled/foreign_key.h"
37
#include "drizzled/item/param.h"
38
#include "drizzled/index_hint.h"
40
class select_result_interceptor;
41
class virtual_column_info;
29
43
/* YACC and LEX Definitions */
31
45
/* These may not be declared yet */
38
52
The following hack is needed because mysql_yacc.cc does not define
39
53
YYSTYPE before including this file
45
#define LEX_YYSTYPE void *
47
#if defined(DRIZZLE_LEX)
48
#include "lex_symbol.h"
50
#define LEX_YYSTYPE YYSTYPE *
52
#define LEX_YYSTYPE void *
58
When a command is added here, be sure it's also added in mysqld.cc
59
in "struct show_var_st status_vars[]= {" ...
61
If the command returns a result set or is not allowed in stored
62
functions or triggers, please also make sure that
63
sp_get_flags_for_command (sp_head.cc) returns proper flags for the
67
enum enum_sql_command {
68
SQLCOM_SELECT, SQLCOM_CREATE_TABLE, SQLCOM_CREATE_INDEX, SQLCOM_ALTER_TABLE,
69
SQLCOM_UPDATE, SQLCOM_INSERT, SQLCOM_INSERT_SELECT,
70
SQLCOM_DELETE, SQLCOM_TRUNCATE, SQLCOM_DROP_TABLE, SQLCOM_DROP_INDEX,
71
SQLCOM_SHOW_DATABASES, SQLCOM_SHOW_TABLES, SQLCOM_SHOW_FIELDS,
72
SQLCOM_SHOW_KEYS, SQLCOM_SHOW_VARIABLES, SQLCOM_SHOW_STATUS,
73
SQLCOM_SHOW_ENGINE_LOGS, SQLCOM_SHOW_ENGINE_STATUS, SQLCOM_SHOW_ENGINE_MUTEX,
74
SQLCOM_SHOW_PROCESSLIST, SQLCOM_SHOW_MASTER_STAT, SQLCOM_SHOW_SLAVE_STAT,
75
SQLCOM_SHOW_CREATE, SQLCOM_SHOW_CHARSETS,
76
SQLCOM_SHOW_COLLATIONS, SQLCOM_SHOW_CREATE_DB, SQLCOM_SHOW_TABLE_STATUS,
77
SQLCOM_LOAD,SQLCOM_SET_OPTION,SQLCOM_LOCK_TABLES,SQLCOM_UNLOCK_TABLES,
78
SQLCOM_CHANGE_DB, SQLCOM_CREATE_DB, SQLCOM_DROP_DB, SQLCOM_ALTER_DB,
79
SQLCOM_REPAIR, SQLCOM_REPLACE, SQLCOM_REPLACE_SELECT,
80
SQLCOM_OPTIMIZE, SQLCOM_CHECK,
81
SQLCOM_ASSIGN_TO_KEYCACHE,
82
SQLCOM_FLUSH, SQLCOM_KILL, SQLCOM_ANALYZE,
83
SQLCOM_ROLLBACK, SQLCOM_ROLLBACK_TO_SAVEPOINT,
84
SQLCOM_COMMIT, SQLCOM_SAVEPOINT, SQLCOM_RELEASE_SAVEPOINT,
85
SQLCOM_SLAVE_START, SQLCOM_SLAVE_STOP,
86
SQLCOM_BEGIN, SQLCOM_CHANGE_MASTER,
88
SQLCOM_RESET, SQLCOM_PURGE, SQLCOM_PURGE_BEFORE, SQLCOM_SHOW_BINLOGS,
89
SQLCOM_SHOW_OPEN_TABLES,
90
SQLCOM_SHOW_SLAVE_HOSTS, SQLCOM_DELETE_MULTI, SQLCOM_UPDATE_MULTI,
91
SQLCOM_SHOW_BINLOG_EVENTS,
92
SQLCOM_SHOW_WARNS, SQLCOM_EMPTY_QUERY, SQLCOM_SHOW_ERRORS,
94
SQLCOM_BINLOG_BASE64_EVENT,
97
When a command is added here, be sure it's also added in mysqld.cc
98
in "struct show_var_st status_vars[]= {" ...
100
/* This should be the last !!! */
57
# include <drizzled/set_var.h>
58
# include <drizzled/item/func.h>
60
# define LEX_YYSTYPE void *
62
# if defined(DRIZZLE_LEX)
63
# include <drizzled/lex_symbol.h>
64
# include <drizzled/sql_yacc.h>
65
# define LEX_YYSTYPE YYSTYPE *
67
# define LEX_YYSTYPE void *
68
# endif /* defined(DRIZZLE_LEX) */
69
# endif /* DRIZZLE_YACC */
70
#endif /* DRIZZLE_SERVER */
104
72
// describe/explain types
105
73
#define DESCRIBE_NORMAL 1
120
88
char *server_name, *host, *db, *username, *password, *scheme, *owner;
121
89
} LEX_SERVER_OPTIONS;
123
typedef struct st_lex_master_info
125
char *host, *user, *password, *log_file_name;
126
uint32_t port, connect_retry;
127
float heartbeat_period;
131
Enum is used for making it possible to detect if the user
132
changed variable or if it should be left at old value
134
enum {LEX_MI_UNCHANGED, LEX_MI_DISABLE, LEX_MI_ENABLE}
135
ssl, ssl_verify_server_cert, heartbeat_opt;
136
char *ssl_key, *ssl_cert, *ssl_ca, *ssl_capath, *ssl_cipher;
137
char *relay_log_name;
138
uint32_t relay_log_pos;
142
92
enum sub_select_type
155
105
NO_TABLESPACE_OP, DISCARD_TABLESPACE, IMPORT_TABLESPACE
159
String names used to print a statement with index hints.
160
Keep in sync with index_hint_type.
162
extern const char * index_hint_type_name[];
163
typedef unsigned char index_clause_map;
166
Bits in index_clause_map : one for each possible FOR clause in
167
USE/FORCE/IGNORE INDEX index hint specification
169
#define INDEX_HINT_MASK_JOIN (1)
170
#define INDEX_HINT_MASK_GROUP (1 << 1)
171
#define INDEX_HINT_MASK_ORDER (1 << 2)
173
#define INDEX_HINT_MASK_ALL (INDEX_HINT_MASK_JOIN | INDEX_HINT_MASK_GROUP | \
174
INDEX_HINT_MASK_ORDER)
176
/* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint */
177
class Index_hint : public Sql_alloc
180
/* The type of the hint : USE/FORCE/IGNORE */
181
enum index_hint_type type;
182
/* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
183
index_clause_map clause;
185
The index name. Empty (str=NULL) name represents an empty list
190
Index_hint (enum index_hint_type type_arg, index_clause_map clause_arg,
191
char *str, uint32_t length) :
192
type(type_arg), clause(clause_arg)
195
key_name.length= length;
198
void print(THD *thd, String *str);
202
The state of the lex parsing for selects
109
The state of the lex parsing for selects
204
111
master and slaves are pointers to select_lex.
205
112
master is pointer to upper level node.
206
113
slave is pointer to lower level node
348
255
static void *operator new(size_t size, MEM_ROOT *mem_root)
349
256
{ return (void*) alloc_root(mem_root, (uint32_t) size); }
350
static void operator delete(void *ptr __attribute__((unused)),
351
size_t size __attribute__((unused)))
257
static void operator delete(void *, size_t)
352
258
{ TRASH(ptr, size); }
353
static void operator delete(void *ptr __attribute__((unused)),
354
MEM_ROOT *mem_root __attribute__((unused)))
259
static void operator delete(void *, MEM_ROOT *)
356
st_select_lex_node(): linkage(UNSPECIFIED_TYPE) {}
357
virtual ~st_select_lex_node() {}
358
inline st_select_lex_node* get_master() { return master; }
261
Select_Lex_Node(): linkage(UNSPECIFIED_TYPE) {}
262
virtual ~Select_Lex_Node() {}
263
inline Select_Lex_Node* get_master() { return master; }
359
264
virtual void init_query();
360
265
virtual void init_select();
361
void include_down(st_select_lex_node *upper);
362
void include_neighbour(st_select_lex_node *before);
363
void include_standalone(st_select_lex_node *sel, st_select_lex_node **ref);
364
void include_global(st_select_lex_node **plink);
266
void include_down(Select_Lex_Node *upper);
267
void include_neighbour(Select_Lex_Node *before);
268
void include_standalone(Select_Lex_Node *sel, Select_Lex_Node **ref);
269
void include_global(Select_Lex_Node **plink);
367
virtual st_select_lex_unit* master_unit()= 0;
368
virtual st_select_lex* outer_select()= 0;
369
virtual st_select_lex* return_after_parsing()= 0;
272
virtual Select_Lex_Unit* master_unit()= 0;
273
virtual Select_Lex* outer_select()= 0;
274
virtual Select_Lex* return_after_parsing()= 0;
371
276
virtual bool set_braces(bool value);
372
277
virtual bool inc_in_sum_expr();
374
279
virtual TableList* get_table_list();
375
280
virtual List<Item>* get_item_list();
376
281
virtual uint32_t get_table_join_options();
377
virtual TableList *add_table_to_list(THD *thd, Table_ident *table,
282
virtual TableList *add_table_to_list(Session *session, Table_ident *table,
378
283
LEX_STRING *alias,
379
284
uint32_t table_options,
380
285
thr_lock_type flags= TL_UNLOCK,
381
286
List<Index_hint> *hints= 0,
382
287
LEX_STRING *option= 0);
383
virtual void set_lock_for_tables(thr_lock_type lock_type __attribute__((unused)))
288
virtual void set_lock_for_tables(thr_lock_type)
386
friend class st_select_lex_unit;
387
friend bool mysql_new_select(struct st_lex *lex, bool move_down);
291
friend class Select_Lex_Unit;
292
friend bool mysql_new_select(LEX *lex, bool move_down);
389
294
void fast_exclude();
391
typedef class st_select_lex_node SELECT_LEX_NODE;
394
SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group
298
Select_Lex_Unit - unit of selects (UNION, INTERSECT, ...) group
398
302
class select_result;
400
304
class select_union;
401
class st_select_lex_unit: public st_select_lex_node {
305
class Select_Lex_Unit: public Select_Lex_Node {
403
307
TableList result_table_list;
404
308
select_union *union_result;
432
336
Pointer to 'last' select or pointer to unit where stored
433
337
global parameters for union
435
st_select_lex *global_parameters;
339
Select_Lex *global_parameters;
436
340
//node on wich we should return current_select pointer after parsing subquery
437
st_select_lex *return_to;
341
Select_Lex *return_to;
438
342
/* LIMIT clause runtime counters */
439
343
ha_rows select_limit_cnt, offset_limit_cnt;
440
344
/* not NULL if unit used in subselect, point to subselect item */
441
345
Item_subselect *item;
442
346
/* thread handler */
445
SELECT_LEX for hidden SELECT in onion which process global
349
Select_Lex for hidden SELECT in onion which process global
446
350
ORDER BY and LIMIT
448
st_select_lex *fake_select_lex;
352
Select_Lex *fake_select_lex;
450
st_select_lex *union_distinct; /* pointer to the last UNION DISTINCT */
354
Select_Lex *union_distinct; /* pointer to the last UNION DISTINCT */
451
355
bool describe; /* union exec() called for EXPLAIN */
453
357
void init_query();
454
st_select_lex_unit* master_unit();
455
st_select_lex* outer_select();
456
st_select_lex* first_select()
458
return reinterpret_cast<st_select_lex*>(slave);
460
st_select_lex_unit* next_unit()
462
return reinterpret_cast<st_select_lex_unit*>(next);
464
st_select_lex* return_after_parsing() { return return_to; }
358
Select_Lex_Unit* master_unit();
359
Select_Lex* outer_select();
360
Select_Lex* first_select()
362
return reinterpret_cast<Select_Lex*>(slave);
364
Select_Lex_Unit* next_unit()
366
return reinterpret_cast<Select_Lex_Unit*>(next);
368
Select_Lex* return_after_parsing() { return return_to; }
465
369
void exclude_level();
466
370
void exclude_tree();
468
372
/* UNION methods */
469
bool prepare(THD *thd, select_result *result, uint32_t additional_options);
373
bool prepare(Session *session, select_result *result,
374
uint64_t additional_options);
472
377
inline void unclean() { cleaned= 0; }
475
380
void print(String *str, enum_query_type query_type);
477
bool add_fake_select_lex(THD *thd);
478
void init_prepare_fake_select_lex(THD *thd);
382
bool add_fake_select_lex(Session *session);
383
void init_prepare_fake_select_lex(Session *session);
479
384
bool change_result(select_result_interceptor *result,
480
385
select_result_interceptor *old_result);
481
void set_limit(st_select_lex *values);
482
void set_thd(THD *thd_arg) { thd= thd_arg; }
483
inline bool is_union ();
386
void set_limit(Select_Lex *values);
387
void set_session(Session *session_arg) { session= session_arg; }
388
inline bool is_union ();
485
friend void lex_start(THD *thd);
390
friend void lex_start(Session *session);
486
391
friend int subselect_union_engine::exec();
488
393
List<Item> *get_unit_column_types();
491
typedef class st_select_lex_unit SELECT_LEX_UNIT;
494
SELECT_LEX - store information of parsed SELECT statment
397
Select_Lex - store information of parsed SELECT statment
496
class st_select_lex: public st_select_lex_node
399
class Select_Lex: public Select_Lex_Node
499
402
Name_resolution_context context;
616
514
uint8_t full_group_by_flag;
617
515
void init_query();
618
516
void init_select();
619
st_select_lex_unit* master_unit();
620
st_select_lex_unit* first_inner_unit()
622
return (st_select_lex_unit*) slave;
624
st_select_lex* outer_select();
625
st_select_lex* next_select() { return (st_select_lex*) next; }
626
st_select_lex* next_select_in_list()
628
return (st_select_lex*) link_next;
630
st_select_lex_node** next_select_in_list_addr()
517
Select_Lex_Unit* master_unit();
518
Select_Lex_Unit* first_inner_unit()
520
return (Select_Lex_Unit*) slave;
522
Select_Lex* outer_select();
523
Select_Lex* next_select() { return (Select_Lex*) next; }
524
Select_Lex* next_select_in_list()
526
return (Select_Lex*) link_next;
528
Select_Lex_Node** next_select_in_list_addr()
632
530
return &link_next;
634
st_select_lex* return_after_parsing()
532
Select_Lex* return_after_parsing()
636
534
return master_unit()->return_after_parsing();
639
void mark_as_dependent(st_select_lex *last);
537
void mark_as_dependent(Select_Lex *last);
641
539
bool set_braces(bool value);
642
540
bool inc_in_sum_expr();
643
541
uint32_t get_in_sum_expr();
645
bool add_item_to_list(THD *thd, Item *item);
646
bool add_group_to_list(THD *thd, Item *item, bool asc);
647
bool add_order_to_list(THD *thd, Item *item, bool asc);
648
TableList* add_table_to_list(THD *thd, Table_ident *table,
543
bool add_item_to_list(Session *session, Item *item);
544
bool add_group_to_list(Session *session, Item *item, bool asc);
545
bool add_order_to_list(Session *session, Item *item, bool asc);
546
TableList* add_table_to_list(Session *session, Table_ident *table,
649
547
LEX_STRING *alias,
650
548
uint32_t table_options,
651
549
thr_lock_type flags= TL_UNLOCK,
652
550
List<Index_hint> *hints= 0,
653
551
LEX_STRING *option= 0);
654
552
TableList* get_table_list();
655
bool init_nested_join(THD *thd);
656
TableList *end_nested_join(THD *thd);
657
TableList *nest_last_join(THD *thd);
553
bool init_nested_join(Session *session);
554
TableList *end_nested_join(Session *session);
555
TableList *nest_last_join(Session *session);
658
556
void add_joined_table(TableList *table);
659
557
TableList *convert_right_join();
660
558
List<Item>* get_item_list();
670
568
This method created for reiniting LEX in mysql_admin_table() and can be
671
used only if you are going remove all SELECT_LEX & units except belonger
569
used only if you are going remove all Select_Lex & units except belonger
672
570
to LEX (LEX::unit & LEX::select, for other purposes there are
673
SELECT_LEX_UNIT::exclude_level & SELECT_LEX_UNIT::exclude_tree
571
Select_Lex_Unit::exclude_level & Select_Lex_Unit::exclude_tree
675
573
void cut_subtree() { slave= 0; }
676
574
bool test_limit();
678
friend void lex_start(THD *thd);
679
st_select_lex() : n_sum_items(0), n_child_sum_items(0) {}
576
friend void lex_start(Session *session);
577
Select_Lex() : n_sum_items(0), n_child_sum_items(0) {}
680
578
void make_empty_select()
685
bool setup_ref_array(THD *thd, uint32_t order_group_num);
686
void print(THD *thd, String *str, enum_query_type query_type);
583
bool setup_ref_array(Session *session, uint32_t order_group_num);
584
void print(Session *session, String *str, enum_query_type query_type);
687
585
static void print_order(String *str,
689
587
enum_query_type query_type);
690
void print_limit(THD *thd, String *str, enum_query_type query_type);
691
void fix_prepare_information(THD *thd, Item **conds, Item **having_conds);
588
void print_limit(Session *session, String *str, enum_query_type query_type);
589
void fix_prepare_information(Session *session, Item **conds, Item **having_conds);
693
591
Destroy the used execution plan (JOIN) of this subtree (this
694
SELECT_LEX and all nested SELECT_LEXes and SELECT_LEX_UNITs).
592
Select_Lex and all nested Select_Lexes and Select_Lex_Units).
898
777
query_tables_own_last= 0;
903
Has the parser/scanner detected that this statement is unsafe?
905
inline bool is_stmt_unsafe() const {
906
return binlog_stmt_flags & (1U << BINLOG_STMT_FLAG_UNSAFE);
910
Flag the current (top-level) statement as unsafe.
912
The flag will be reset after the statement has finished.
915
inline void set_stmt_unsafe() {
916
binlog_stmt_flags|= (1U << BINLOG_STMT_FLAG_UNSAFE);
919
inline void clear_stmt_unsafe() {
920
binlog_stmt_flags&= ~(1U << BINLOG_STMT_FLAG_UNSAFE);
924
true if the parsed tree contains references to stored procedures
925
or functions, false otherwise
927
bool uses_stored_routines() const
928
{ return sroutines_list.elements != 0; }
931
enum enum_binlog_stmt_flag {
932
BINLOG_STMT_FLAG_UNSAFE,
933
BINLOG_STMT_FLAG_COUNT
937
Tells if the parsing stage detected properties of the statement,
938
for example: that some items require row-based binlogging to give
939
a reliable binlog/replication, or if we will use stored functions
940
or triggers which themselves need require row-based binlogging.
942
uint32_t binlog_stmt_flags;
1348
1182
Ending position of the TEXT_STRING or IDENT in the pre-processed
1351
NOTE: this member must be used within MYSQLlex() function only.
1185
NOTE: this member must be used within DRIZZLElex() function only.
1353
1187
const char *m_cpp_text_end;
1356
1190
Character set specified by the character-set-introducer.
1358
NOTE: this member must be used within MYSQLlex() function only.
1192
NOTE: this member must be used within DRIZZLElex() function only.
1360
1194
const CHARSET_INFO *m_underscore_cs;
1364
/* The state of the lex parsing. This is saved in the THD struct */
1198
/* The state of the lex parsing. This is saved in the Session struct */
1366
typedef struct st_lex : public Query_tables_list
1200
class LEX : public Query_tables_list
1368
SELECT_LEX_UNIT unit; /* most upper unit */
1369
SELECT_LEX select_lex; /* first SELECT_LEX */
1370
/* current SELECT_LEX in parsing */
1371
SELECT_LEX *current_select;
1372
/* list of all SELECT_LEX */
1373
SELECT_LEX *all_selects_list;
1203
Select_Lex_Unit unit; /* most upper unit */
1204
Select_Lex select_lex; /* first Select_Lex */
1205
/* current Select_Lex in parsing */
1206
Select_Lex *current_select;
1207
/* list of all Select_Lex */
1208
Select_Lex *all_selects_list;
1375
1210
char *length,*dec,*change;
1376
1211
LEX_STRING name;
1378
char* to_log; /* For PURGE MASTER LOGS TO */
1379
char* x509_subject,*x509_issuer,*ssl_cipher;
1381
sql_exchange *exchange;
1213
file_exchange *exchange;
1382
1214
select_result *result;
1383
1215
Item *default_value, *on_update_value;
1384
1216
LEX_STRING comment, ident;
1386
1218
unsigned char* yacc_yyss, *yacc_yyvs;
1389
/* maintain a list of used plugins for this LEX */
1390
DYNAMIC_ARRAY plugins;
1391
plugin_ref plugins_static_buffer[INITIAL_LEX_PLUGIN_LIST_SIZE];
1220
virtual_column_info *vcol_info;
1393
1222
const CHARSET_INFO *charset;
1394
1223
bool text_string_is_7bit;
1490
1325
Alter_info alter_info;
1493
field_list was created for view and should be removed before PS/SP
1496
bool empty_field_list_on_rset;
1499
1328
Pointers to part of LOAD DATA statement that should be rewritten
1500
1329
during replication ("LOCAL 'filename' REPLACE INTO" part).
1502
1331
const char *fname_start;
1503
1332
const char *fname_end;
1506
During name resolution search only in the table list given by
1335
During name resolution search only in the table list given by
1507
1336
Name_resolution_context::first_name_resolution_table and
1508
1337
Name_resolution_context::last_name_resolution_table
1509
(see Item_field::fix_fields()).
1338
(see Item_field::fix_fields()).
1511
1340
bool use_only_table_context;
1513
1342
bool escape_used;
1514
1343
bool is_lex_started; /* If lex_start() did run. For debugging. */
1520
1349
destroy_query_tables_list();
1521
plugin_unlock_list(NULL, (plugin_ref *)plugins.buffer, plugins.elements);
1522
delete_dynamic(&plugins);
1525
1352
TableList *unlink_first_table(bool *link_to_local);
1526
1353
void link_first_table_back(TableList *first, bool link_to_local);
1527
1354
void first_lists_tables_same();
1529
bool can_be_merged();
1530
bool can_use_merged();
1531
bool can_not_use_merged();
1532
1356
bool only_view_structure();
1533
1357
bool need_correct_ident();
1551
1375
return context_stack.head();
1554
Restore the LEX and THD in case of a parse error.
1378
Restore the LEX and Session in case of a parse error.
1556
static void cleanup_lex_after_parse_error(THD *thd);
1558
void reset_n_backup_query_tables_list(Query_tables_list *backup);
1559
void restore_backup_query_tables_list(Query_tables_list *backup);
1561
bool table_or_sp_used();
1380
static void cleanup_lex_after_parse_error(Session *session);
1564
1383
@brief check if the statement is a single-level join
1565
1384
@return result of the check
1566
@retval true The statement doesn't contain subqueries, unions and
1385
@retval true The statement doesn't contain subqueries, unions and
1567
1386
stored procedure calls.
1568
1387
@retval false There are subqueries, UNIONs or stored procedure calls.
1570
bool is_single_level_stmt()
1389
bool is_single_level_stmt()
1573
1392
This check exploits the fact that the last added to all_select_list is
1574
on its top. So select_lex (as the first added) will be at the tail
1393
on its top. So select_lex (as the first added) will be at the tail
1577
if (&select_lex == all_selects_list && !sroutines.records)
1396
if (&select_lex == all_selects_list)
1579
1398
assert(!all_selects_list->next_select_in_list());
1586
struct st_lex_local: public st_lex
1405
struct st_lex_local: public LEX
1588
1407
static void *operator new(size_t size) throw()