1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23 /**
24 @defgroup Semantic_Analysis Semantic Analysis
25 */
26
27 #ifndef SQL_LEX_INCLUDED
28 #define SQL_LEX_INCLUDED
29
30 #include "my_global.h"
31 #include "mem_root_array.h" // Mem_root_array
32 #include "prealloced_array.h" // Prealloced_array
33 #include "thr_lock.h" // thr_lock_type
34 #include "violite.h" // SSL_type
35 #include "item.h" // Name_resolution_context
36 #include "item_subselect.h" // chooser_compare_func_creator
37 #include "lex_symbol.h" // LEX_SYMBOL
38 #include "parse_tree_node_base.h" // enum_parsing_context
39 #include "query_options.h" // OPTION_NO_CONST_TABLES
40 #include "sql_alloc.h" // Sql_alloc
41 #include "sql_chars.h"
42 #include "sql_alter.h" // Alter_info
43 #include "sql_connect.h" // USER_RESOURCES
44 #include "sql_data_change.h" // enum_duplicates
45 #include "sql_get_diagnostics.h" // Diagnostics_information
46 #include "sql_servers.h" // Server_options
47 #include "sql_signal.h" // enum_condition_item_name
48 #include "table.h" // TABLE_LIST
49 #include "trigger_def.h" // enum_trigger_action_time_type
50 #include "xa.h" // xa_option_words
51 #include "select_lex_visitor.h"
52 #include "parse_tree_hints.h"
53 #include "sql_admin.h"
54 #include <map>
55
56 #ifdef MYSQL_SERVER
57 #include "item_func.h" // Cast_target
58 #endif
59
60 /* YACC and LEX Definitions */
61
62 /* These may not be declared yet */
63 class Table_ident;
64 class sql_exchange;
65 class sp_head;
66 class sp_name;
67 class sp_instr;
68 class sp_pcontext;
69 class st_alter_tablespace;
70 class partition_info;
71 class Event_parse_data;
72 class set_var_base;
73 class sys_var;
74 class Item_func_match;
75 class File_parser;
76 class Key_part_spec;
77 class Query_result_interceptor;
78 class Item_func;
79 class Sql_cmd;
80 struct sql_digest_state;
81 typedef class st_select_lex SELECT_LEX;
82
83 const size_t INITIAL_LEX_PLUGIN_LIST_SIZE = 16;
84 class Opt_hints_global;
85 class Opt_hints_qb;
86
87 #ifdef MYSQL_SERVER
88 /*
89 There are 8 different type of table access so there is no more than
90 combinations 2^8 = 256:
91
92 . STMT_READS_TRANS_TABLE
93
94 . STMT_READS_NON_TRANS_TABLE
95
96 . STMT_READS_TEMP_TRANS_TABLE
97
98 . STMT_READS_TEMP_NON_TRANS_TABLE
99
100 . STMT_WRITES_TRANS_TABLE
101
102 . STMT_WRITES_NON_TRANS_TABLE
103
104 . STMT_WRITES_TEMP_TRANS_TABLE
105
106 . STMT_WRITES_TEMP_NON_TRANS_TABLE
107
108 The unsafe conditions for each combination is represented within a byte
109 and stores the status of the option --binlog-direct-non-trans-updates,
110 whether the trx-cache is empty or not, and whether the isolation level
111 is lower than ISO_REPEATABLE_READ:
112
113 . option (OFF/ON)
114 . trx-cache (empty/not empty)
115 . isolation (>= ISO_REPEATABLE_READ / < ISO_REPEATABLE_READ)
116
117 bits 0 : . OFF, . empty, . >= ISO_REPEATABLE_READ
118 bits 1 : . OFF, . empty, . < ISO_REPEATABLE_READ
119 bits 2 : . OFF, . not empty, . >= ISO_REPEATABLE_READ
120 bits 3 : . OFF, . not empty, . < ISO_REPEATABLE_READ
121 bits 4 : . ON, . empty, . >= ISO_REPEATABLE_READ
122 bits 5 : . ON, . empty, . < ISO_REPEATABLE_READ
123 bits 6 : . ON, . not empty, . >= ISO_REPEATABLE_READ
124 bits 7 : . ON, . not empty, . < ISO_REPEATABLE_READ
125 */
126 extern uint binlog_unsafe_map[256];
127 /*
128 Initializes the array with unsafe combinations and its respective
129 conditions.
130 */
131 void binlog_unsafe_map_init();
132 #endif
133
134 enum enum_yes_no_unknown
135 {
136 TVL_YES, TVL_NO, TVL_UNKNOWN
137 };
138
139 enum keytype {
140 KEYTYPE_PRIMARY= 0,
141 KEYTYPE_UNIQUE= 1,
142 KEYTYPE_MULTIPLE= 2,
143 KEYTYPE_FULLTEXT= 4,
144 KEYTYPE_SPATIAL= 8,
145 KEYTYPE_FOREIGN= 16,
146 KEYTYPE_CLUSTERING= 32,
147 };
148
149 enum enum_ha_read_modes { RFIRST, RNEXT, RPREV, RLAST, RKEY, RNEXT_SAME };
150
151 enum enum_filetype { FILETYPE_CSV, FILETYPE_XML };
152
153 enum fk_match_opt { FK_MATCH_UNDEF, FK_MATCH_FULL,
154 FK_MATCH_PARTIAL, FK_MATCH_SIMPLE};
155
156 enum fk_option { FK_OPTION_UNDEF, FK_OPTION_RESTRICT, FK_OPTION_CASCADE,
157 FK_OPTION_SET_NULL, FK_OPTION_NO_ACTION, FK_OPTION_DEFAULT};
158
159
160 /**
161 used by the parser to store internal variable name
162 */
163 struct sys_var_with_base
164 {
165 sys_var *var;
166 LEX_STRING base_name;
167 };
168
169
170 #define YYSTYPE_IS_DECLARED 1
171 union YYSTYPE;
172 typedef YYSTYPE *LEX_YYSTYPE;
173
174 // describe/explain types
175 #define DESCRIBE_NONE 0 // Not explain query
176 #define DESCRIBE_NORMAL 1
177
178 #ifdef MYSQL_SERVER
179
180 /*
181 If we encounter a diagnostics statement (GET DIAGNOSTICS, or e.g.
182 the old SHOW WARNINGS|ERRORS, or "diagnostics variables" such as
183 @@warning_count | @@error_count, we'll set some hints so this
184 information is not lost. DA_KEEP_UNSPECIFIED is used in LEX constructor to
185 avoid leaving variables uninitialized.
186 */
187 enum enum_keep_diagnostics
188 {
189 DA_KEEP_NOTHING= 0, /**< keep nothing */
190 DA_KEEP_DIAGNOSTICS, /**< keep the diagnostics area */
191 DA_KEEP_COUNTS, /**< keep @@warning_count / @error_count */
192 DA_KEEP_PARSE_ERROR, /**< keep diagnostics area after parse error */
193 DA_KEEP_UNSPECIFIED /**< keep semantics is unspecified */
194 };
195
196 enum enum_sp_suid_behaviour
197 {
198 SP_IS_DEFAULT_SUID= 0,
199 SP_IS_NOT_SUID,
200 SP_IS_SUID
201 };
202
203 enum enum_sp_data_access
204 {
205 SP_DEFAULT_ACCESS= 0,
206 SP_CONTAINS_SQL,
207 SP_NO_SQL,
208 SP_READS_SQL_DATA,
209 SP_MODIFIES_SQL_DATA
210 };
211
212 /**
213 enum_sp_type defines type codes of stored programs.
214
215 Events have the SP_TYPE_PROCEDURE type code.
216
217 @note these codes are used when dealing with the mysql.proc system table, so
218 they must not be changed.
219
220 @note the following macros were used previously for the same purpose. Now they
221 are used for ACL only.
222 */
223 enum enum_sp_type
224 {
225 SP_TYPE_FUNCTION= 1,
226 SP_TYPE_PROCEDURE,
227 SP_TYPE_TRIGGER,
228 SP_TYPE_EVENT
229 };
230
231 /*
232 Values for the type enum. This reflects the order of the enum declaration
233 in the CREATE TABLE command. These values are used to enumerate object types
234 for the ACL statements.
235
236 These values were also used for enumerating stored program types. However, now
237 enum_sp_type should be used for that instead of them.
238 */
239 #define TYPE_ENUM_FUNCTION 1
240 #define TYPE_ENUM_PROCEDURE 2
241 #define TYPE_ENUM_TRIGGER 3
242 #define TYPE_ENUM_PROXY 4
243
244 const LEX_STRING sp_data_access_name[]=
245 {
246 { C_STRING_WITH_LEN("") },
247 { C_STRING_WITH_LEN("CONTAINS SQL") },
248 { C_STRING_WITH_LEN("NO SQL") },
249 { C_STRING_WITH_LEN("READS SQL DATA") },
250 { C_STRING_WITH_LEN("MODIFIES SQL DATA") }
251 };
252
253 #define DERIVED_SUBQUERY 1
254 #define DERIVED_VIEW 2
255
256 enum enum_view_create_mode
257 {
258 VIEW_CREATE_NEW, // check that there are not such VIEW/table
259 VIEW_ALTER, // check that VIEW .frm with such name exists
260 VIEW_CREATE_OR_REPLACE // check only that there are not such table
261 };
262
263 enum enum_drop_mode
264 {
265 DROP_DEFAULT, // mode is not specified
266 DROP_CASCADE, // CASCADE option
267 DROP_RESTRICT // RESTRICT option
268 };
269
270 /* Options to add_table_to_list() */
271 #define TL_OPTION_UPDATING 1
272 #define TL_OPTION_FORCE_INDEX 2
273 #define TL_OPTION_IGNORE_LEAVES 4
274 #define TL_OPTION_ALIAS 8
275
276 typedef List<Item> List_item;
277 typedef Mem_root_array<ORDER*, true> Group_list_ptrs;
278
279
280 /**
281 Structure to hold parameters for CHANGE MASTER, START SLAVE, and STOP SLAVE.
282
283 Remark: this should not be confused with Master_info (and perhaps
284 would better be renamed to st_lex_replication_info). Some fields,
285 e.g., delay, are saved in Relay_log_info, not in Master_info.
286 */
287 typedef struct st_lex_master_info
288 {
289 /*
290 The array of IGNORE_SERVER_IDS has a preallocation, and is not expected
291 to grow to any significant size, so no instrumentation.
292 */
st_lex_master_infost_lex_master_info293 st_lex_master_info()
294 : repl_ignore_server_ids(PSI_NOT_INSTRUMENTED)
295 {
296 initialize();
297 }
298 char *host, *user, *password, *log_file_name, *bind_addr;
299 uint port, connect_retry;
300 float heartbeat_period;
301 int sql_delay;
302 ulonglong pos;
303 ulong server_id, retry_count;
304 char *gtid;
305 char* view_id;
306 const char* channel; // identifier similar to database name
307 enum {UNTIL_SQL_BEFORE_GTIDS= 0, UNTIL_SQL_AFTER_GTIDS} gtid_until_condition;
308 bool until_after_gaps;
309 bool slave_until;
310 bool for_channel;
311
312 /*
313 Enum is used for making it possible to detect if the user
314 changed variable or if it should be left at old value
315 */
316 enum {LEX_MI_UNCHANGED= 0, LEX_MI_DISABLE, LEX_MI_ENABLE}
317 ssl, ssl_verify_server_cert, heartbeat_opt, repl_ignore_server_ids_opt,
318 retry_count_opt, auto_position, port_opt;
319 char *ssl_key, *ssl_cert, *ssl_ca, *ssl_capath, *ssl_cipher;
320 char *ssl_crl, *ssl_crlpath, *tls_version;
321 char *relay_log_name;
322 ulong relay_log_pos;
323 Prealloced_array<ulong, 2, true> repl_ignore_server_ids;
324
325 /// Initializes everything to zero/NULL/empty.
326 void initialize();
327 /// Sets all fields to their "unspecified" value.
328 void set_unspecified();
329 private:
330 // Not copyable or assignable.
331 st_lex_master_info(const st_lex_master_info&);
332 st_lex_master_info &operator=(const st_lex_master_info&);
333 } LEX_MASTER_INFO;
334
335 typedef struct st_lex_reset_slave
336 {
337 bool all;
338 } LEX_RESET_SLAVE;
339
340 enum sub_select_type
341 {
342 UNSPECIFIED_TYPE,UNION_TYPE, INTERSECT_TYPE,
343 EXCEPT_TYPE, GLOBAL_OPTIONS_TYPE, DERIVED_TABLE_TYPE, OLAP_TYPE
344 };
345
346 enum olap_type
347 {
348 UNSPECIFIED_OLAP_TYPE, CUBE_TYPE, ROLLUP_TYPE
349 };
350
351 /*
352 String names used to print a statement with index hints.
353 Keep in sync with index_hint_type.
354 */
355 extern const char * index_hint_type_name[];
356 typedef uchar index_clause_map;
357
358 /*
359 Bits in index_clause_map : one for each possible FOR clause in
360 USE/FORCE/IGNORE INDEX index hint specification
361 */
362 #define INDEX_HINT_MASK_JOIN (1)
363 #define INDEX_HINT_MASK_GROUP (1 << 1)
364 #define INDEX_HINT_MASK_ORDER (1 << 2)
365
366 #define INDEX_HINT_MASK_ALL (INDEX_HINT_MASK_JOIN | INDEX_HINT_MASK_GROUP | \
367 INDEX_HINT_MASK_ORDER)
368
369 /* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint */
370 class Index_hint : public Sql_alloc
371 {
372 public:
373 /* The type of the hint : USE/FORCE/IGNORE */
374 enum index_hint_type type;
375 /* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
376 index_clause_map clause;
377 /*
378 The index name. Empty (str=NULL) name represents an empty list
379 USE INDEX () clause
380 */
381 LEX_STRING key_name;
382
Index_hint(const char * str,uint length)383 Index_hint (const char *str, uint length)
384 {
385 key_name.str= const_cast<char *>(str);
386 key_name.length= length;
387 }
388
389 void print(THD *thd, String *str);
390 };
391
392 /*
393 Class st_select_lex_unit represents a query expression.
394 Class st_select_lex represents a query block.
395 A query expression contains one or more query blocks (more than one means
396 that we have a UNION query).
397 These classes are connected as follows:
398 Both classes have a master, a slave, a next and a prev field.
399 For class st_select_lex, master and slave connect to objects of type
400 st_select_lex_unit, whereas for class st_select_lex_unit, they connect
401 to st_select_lex.
402 master is pointer to outer node.
403 slave is pointer to the first inner node
404
405 neighbors are two st_select_lex or st_select_lex_unit objects on
406 the same level.
407
408 The structures are linked with the following pointers:
409 - list of neighbors (next/prev) (prev of first element point to slave
410 pointer of outer structure)
411 - For st_select_lex, this is a list of query blocks.
412 - For st_select_lex_unit, this is a list of subqueries.
413
414 - pointer to outer node (master), which is
415 If this is st_select_lex_unit
416 - pointer to outer select_lex.
417 If this is st_select_lex
418 - pointer to outer st_select_lex_unit.
419
420 - pointer to inner objects (slave), which is either:
421 If this is an st_select_lex_unit:
422 - first query block that belong to this query expression.
423 If this is an st_select_lex
424 - first query expression that belong to this query block (subqueries).
425
426 - list of all st_select_lex objects (link_next/link_prev)
427 This is to be used for things like derived tables creation, where we
428 go through this list and create the derived tables.
429
430 If query expression contain several query blocks (UNION now,
431 INTERSECT etc later) then it has a special select_lex called
432 fake_select_lex. It used for storing global parameters (like ORDER BY,
433 LIMIT) and executing union.
434 Subqueries used in global ORDER BY clause will be attached to this
435 fake_select_lex, which will allow them to correctly resolve fields of
436 the containing UNION and outer selects.
437
438 For example for following query:
439
440 select *
441 from table1
442 where table1.field IN (select * from table1_1_1 union
443 select * from table1_1_2)
444 union
445 select *
446 from table2
447 where table2.field=(select (select f1 from table2_1_1_1_1
448 where table2_1_1_1_1.f2=table2_1_1.f3)
449 from table2_1_1
450 where table2_1_1.f1=table2.f2)
451 union
452 select * from table3;
453
454 we will have following structure:
455
456 select1: (select * from table1 ...)
457 select2: (select * from table2 ...)
458 select3: (select * from table3)
459 select1.1.1: (select * from table1_1_1)
460 ...
461
462 main unit
463 fake0
464 select1 select2 select3
465 |^^ |^
466 s||| ||master
467 l||| |+---------------------------------+
468 a||| +---------------------------------+|
469 v|||master slave ||
470 e||+-------------------------+ ||
471 V| neighbor | V|
472 unit1.1<+==================>unit1.2 unit2.1
473 fake1.1
474 select1.1.1 select 1.1.2 select1.2.1 select2.1.1
475 |^
476 ||
477 V|
478 unit2.1.1.1
479 select2.1.1.1.1
480
481
482 relation in main unit will be following:
483 (bigger picture for:
484 main unit
485 fake0
486 select1 select2 select3
487 in the above picture)
488
489 main unit
490 |^^^^|fake_select_lex
491 |||||+--------------------------------------------+
492 ||||+--------------------------------------------+|
493 |||+------------------------------+ ||
494 ||+--------------+ | ||
495 slave||master | | ||
496 V| neighbor | neighbor | master|V
497 select1<========>select2<========>select3 fake0
498
499 list of all select_lex will be following (as it will be constructed by
500 parser):
501
502 select1->select2->select3->select2.1.1->select 2.1.2->select2.1.1.1.1-+
503 |
504 +---------------------------------------------------------------------+
505 |
506 +->select1.1.1->select1.1.2
507
508 */
509
510
511 struct LEX;
512 class THD;
513 class Query_result;
514 class JOIN;
515 class Query_result_union;
516
517 /**
518 This class represents a query expression (one query block or
519 several query blocks combined with UNION).
520 */
521 class st_select_lex_unit: public Sql_alloc
522 {
523 /**
524 Intrusive double-linked list of all query expressions
525 immediately contained within the same query block.
526 */
527 st_select_lex_unit *next;
528 st_select_lex_unit **prev;
529
530 /**
531 The query block wherein this query expression is contained,
532 NULL if the query block is the outer-most one.
533 */
534 SELECT_LEX *master;
535 /// The first query block in this query expression.
536 SELECT_LEX *slave;
537 private:
538 /**
539 Marker for subqueries in WHERE, HAVING, ORDER BY, GROUP BY and
540 SELECT item lists.
541 Must be read/written when holding LOCK_query_plan.
542
543 See Item_subselect::explain_subquery_checker
544 */
545 enum_parsing_context explain_marker;
546
547 bool prepared; ///< All query blocks in query expression are prepared
548 bool optimized; ///< All query blocks in query expression are optimized
549 bool executed; ///< Query expression has been executed
550
551 TABLE_LIST result_table_list;
552 Query_result_union *union_result;
553 TABLE *table; /* temporary table using for appending UNION results */
554 /// Object to which the result for this query expression is sent
555 Query_result *m_query_result;
556
557 public:
558 /**
559 result of this query can't be cached, bit field, can be :
560 UNCACHEABLE_DEPENDENT
561 UNCACHEABLE_RAND
562 UNCACHEABLE_SIDEEFFECT
563 */
564 uint8 uncacheable;
565
566 explicit st_select_lex_unit(enum_parsing_context parsing_context);
567
568 /// @return true for a query expression without UNION or multi-level ORDER
is_simple()569 bool is_simple() const { return !(is_union() || fake_select_lex); }
570
571 /// Values for st_select_lex_unit::cleaned
572 enum enum_clean_state
573 {
574 UC_DIRTY, ///< Unit isn't cleaned
575 UC_PART_CLEAN,///< Unit were cleaned, except JOIN and JOIN_TABs were
576 ///< kept for possible EXPLAIN
577 UC_CLEAN ///< Unit completely cleaned, all underlying JOINs were
578 ///< freed
579 };
580 enum_clean_state cleaned; ///< cleanliness state
581
582 // list of fields which points to temporary table for union
583 List<Item> item_list;
584 /*
585 list of types of items inside union (used for union & derived tables)
586
587 Item_type_holders from which this list consist may have pointers to Field,
588 pointers is valid only after preparing SELECTS of this unit and before
589 any SELECT of this unit execution
590
591 TODO:
592 Possibly this member should be protected, and its direct use replaced
593 by get_unit_column_types(). Check the places where it is used.
594 */
595 List<Item> types;
596 /**
597 Pointer to query block containing global parameters for query.
598 Global parameters may include ORDER BY, LIMIT and OFFSET.
599
600 If this is a union of multiple query blocks, the global parameters are
601 stored in fake_select_lex. If the union doesn't use a temporary table,
602 st_select_lex_unit::prepare() nulls out fake_select_lex, but saves a copy
603 in saved_fake_select_lex in order to preserve the global parameters.
604
605 If this is not a union, and the query expression has no multi-level
606 ORDER BY/LIMIT, global parameters are in the single query block.
607
608 @return query block containing the global parameters
609 */
global_parameters()610 inline st_select_lex *global_parameters() const
611 {
612 if (fake_select_lex != NULL)
613 return fake_select_lex;
614 else if (saved_fake_select_lex != NULL)
615 return saved_fake_select_lex;
616 return first_select();
617 };
618 /* LIMIT clause runtime counters */
619 ha_rows select_limit_cnt, offset_limit_cnt;
620 /// Points to subquery if this query expression is used in one, otherwise NULL
621 Item_subselect *item;
622 THD *thd; ///< Thread handler
623 /**
624 Helper query block for query expression with UNION or multi-level
625 ORDER BY/LIMIT
626 */
627 st_select_lex *fake_select_lex;
628 /**
629 SELECT_LEX that stores LIMIT and OFFSET for UNION ALL when no
630 fake_select_lex is used.
631 */
632 st_select_lex *saved_fake_select_lex;
633 /// Points to last query block used by UNION DISTINCT query
634 st_select_lex *union_distinct;
635
636 /// @return true if query expression can be merged into an outer query
637 bool is_mergeable() const;
638
639 /// @return the query block this query expression belongs to as subquery
outer_select()640 st_select_lex* outer_select() const { return master; }
641
642 /// @return the first query block inside this query expression
first_select()643 st_select_lex* first_select() const { return slave; }
644
645 /// @return the next query expression within same query block (next subquery)
next_unit()646 st_select_lex_unit* next_unit() const { return next; }
647
648 /// @return the query result object in use for this query expression
query_result()649 Query_result *query_result() const { return m_query_result; }
650
651 /// Set new query result object for this query expression
set_query_result(Query_result * res)652 void set_query_result(Query_result *res) { m_query_result= res; }
653
654 bool prepare(THD *thd, Query_result *result, ulonglong added_options,
655 ulonglong removed_options);
656 bool optimize(THD *thd);
657 bool execute(THD *thd);
658 bool explain(THD *ethd);
659 bool cleanup(bool full);
unclean()660 inline void unclean() { cleaned= UC_DIRTY; }
661 void reinit_exec_mechanism();
662
663 void print(String *str, enum_query_type query_type);
664 bool accept(Select_lex_visitor *visitor);
665
666 bool add_fake_select_lex(THD *thd);
667 bool prepare_fake_select_lex(THD *thd);
set_prepared()668 void set_prepared() { prepared= true; }
set_optimized()669 void set_optimized() { optimized= true; }
set_executed()670 void set_executed() { executed= true; }
reset_executed()671 void reset_executed() { executed= false; }
is_prepared()672 bool is_prepared() const { return prepared; }
is_optimized()673 bool is_optimized() const { return optimized; }
is_executed()674 bool is_executed() const { return executed; }
675 bool change_query_result(Query_result_interceptor *result,
676 Query_result_interceptor *old_result);
677 void set_limit(st_select_lex *values);
set_thd(THD * thd_arg)678 void set_thd(THD *thd_arg) { thd= thd_arg; }
679
680 inline bool is_union () const;
681 bool union_needs_tmp_table();
682
683 /// Include a query expression below a query block.
684 void include_down(LEX *lex, st_select_lex *outer);
685
686 /// Include a chain of query expressions below a query block.
687 void include_chain(LEX *lex, st_select_lex *outer);
688
689 /// Exclude this unit and immediately contained select_lex objects
690 void exclude_level();
691
692 /// Exclude subtree of current unit from tree of SELECTs
693 void exclude_tree();
694
695 /// Renumber query blocks of a query expression according to supplied LEX
696 void renumber_selects(LEX *lex);
697
698 friend class st_select_lex;
699
700 List<Item> *get_unit_column_types();
701 List<Item> *get_field_list();
702
703 enum_parsing_context get_explain_marker() const;
704 void set_explain_marker(enum_parsing_context m);
705 void set_explain_marker_from(const st_select_lex_unit *u);
706
707 #ifndef NDEBUG
708 /**
709 Asserts that none of {this unit and its children units} is fully cleaned
710 up.
711 */
712 void assert_not_fully_clean();
713 #else
assert_not_fully_clean()714 void assert_not_fully_clean() {}
715 #endif
716 void invalidate();
717 /*
718 An exception: this is the only function that needs to adjust
719 explain_marker.
720 */
721 friend bool parse_view_definition(THD *thd, TABLE_LIST *view_ref);
722 };
723
724 typedef class st_select_lex_unit SELECT_LEX_UNIT;
725 typedef Bounds_checked_array<Item*> Ref_ptr_array;
726
727 /**
728 This class represents a query block, aka a query specification, which is
729 a query consisting of a SELECT keyword, followed by a table list,
730 optionally followed by a WHERE clause, a GROUP BY, etc.
731 */
732 class st_select_lex: public Sql_alloc
733 {
734 public:
735 /// @returns a slice of ref_pointer_array
ref_ptr_array_slice(size_t slice_num)736 Ref_ptr_array ref_ptr_array_slice(size_t slice_num)
737 {
738 size_t slice_sz= ref_pointer_array.size() / 5U;
739 assert(ref_pointer_array.size() % 5 == 0);
740 assert(slice_num < 5U);
741 return Ref_ptr_array(&ref_pointer_array[slice_num * slice_sz], slice_sz);
742 }
743
where_cond()744 Item *where_cond() const { return m_where_cond; }
set_where_cond(Item * cond)745 void set_where_cond(Item *cond) { m_where_cond= cond; }
where_cond_ref()746 Item **where_cond_ref() { return &m_where_cond; }
having_cond()747 Item *having_cond() const { return m_having_cond; }
set_having_cond(Item * cond)748 void set_having_cond(Item *cond) { m_having_cond= cond; }
749
set_query_result(Query_result * result)750 void set_query_result(Query_result *result) { m_query_result= result; }
query_result()751 Query_result *query_result() const { return m_query_result; }
752 bool change_query_result(Query_result_interceptor *new_result,
753 Query_result_interceptor *old_result);
754
755 /// Set base options for a query block (and active options too)
set_base_options(ulonglong options_arg)756 void set_base_options(ulonglong options_arg)
757 {
758 DBUG_EXECUTE_IF("no_const_tables", options_arg|= OPTION_NO_CONST_TABLES;);
759
760 // Make sure we do not overwrite options by accident
761 assert(m_base_options == 0 && m_active_options == 0);
762 m_base_options= options_arg;
763 m_active_options= options_arg;
764 }
765
766 /// Add base options to a query block, also update active options
add_base_options(ulonglong options)767 void add_base_options(ulonglong options)
768 {
769 assert(first_execution);
770 m_base_options|= options;
771 m_active_options|= options;
772 }
773
774 /**
775 Remove base options from a query block.
776 Active options are also updated, and we assume here that "extra" options
777 cannot override removed base options.
778 */
remove_base_options(ulonglong options)779 void remove_base_options(ulonglong options)
780 {
781 assert(first_execution);
782 m_base_options&= ~options;
783 m_active_options&= ~options;
784 }
785
786 /// Make active options from base options, supplied options and environment:
787 void make_active_options(ulonglong added_options, ulonglong removed_options);
788
789 /// Adjust the active option set
add_active_options(ulonglong options)790 void add_active_options(ulonglong options)
791 { m_active_options|= options; }
792
793 /// @return the active query options
active_options()794 ulonglong active_options() const { return m_active_options; }
795
796 private:
797 /**
798 Intrusive double-linked list of all query blocks within the same
799 query expression.
800 */
801 st_select_lex *next;
802 st_select_lex **prev;
803
804 /// The query expression containing this query block.
805 st_select_lex_unit *master;
806 /// The first query expression contained within this query block.
807 st_select_lex_unit *slave;
808
809 /// Intrusive double-linked global list of query blocks.
810 st_select_lex *link_next;
811 st_select_lex **link_prev;
812
813 /// Result of this query block
814 Query_result *m_query_result;
815
816 /**
817 Options assigned from parsing and throughout resolving,
818 should not be modified after resolving is done.
819 */
820 ulonglong m_base_options;
821 /**
822 Active options. Derived from base options, modifiers added during
823 resolving and values from session variable option_bits. Since the latter
824 may change, active options are refreshed per execution of a statement.
825 */
826 ulonglong m_active_options;
827
828 public:
829 /**
830 In sql_cache we store SQL_CACHE flag as specified by user to be
831 able to restore SELECT statement from internal structures.
832 */
833 enum e_sql_cache { SQL_CACHE_UNSPECIFIED, SQL_NO_CACHE, SQL_CACHE };
834 /// Query cache hint (should rather belong in LEX object?)
835 e_sql_cache sql_cache;
836 /**
837 result of this query can't be cached, bit field, can be :
838 UNCACHEABLE_DEPENDENT
839 UNCACHEABLE_RAND
840 UNCACHEABLE_SIDEEFFECT
841 */
842 uint8 uncacheable;
843
844 /// Describes context of this query block (e.g if it is a derived table).
845 enum sub_select_type linkage;
846 bool no_table_names_allowed; ///< used for global order by
847 /**
848 Context for name resolution for all column references except columns
849 from joined tables.
850 */
851 Name_resolution_context context;
852 /**
853 Pointer to first object in list of Name res context objects that have
854 this query block as the base query block.
855 Includes field "context" which is embedded in this query block.
856 */
857 Name_resolution_context *first_context;
858 /**
859 Three fields used by semi-join transformations to know when semi-join is
860 possible, and in which condition tree the subquery predicate is located.
861 */
862 enum Resolve_place { RESOLVE_NONE, RESOLVE_JOIN_NEST, RESOLVE_CONDITION,
863 RESOLVE_HAVING, RESOLVE_SELECT_LIST };
864 Resolve_place resolve_place; ///< Indicates part of query being resolved
865 TABLE_LIST *resolve_nest; ///< Used when resolving outer join condition
866 /**
867 Disables semi-join flattening when resolving a subtree in which flattening
868 is not allowed. The flag should be true while resolving items that are not
869 on the AND-top-level of a condition tree.
870 */
871 bool semijoin_disallowed;
872 char *db;
873 private:
874 /**
875 Condition to be evaluated after all tables in a query block are joined.
876 After all permanent transformations have been conducted by
877 SELECT_LEX::prepare(), this condition is "frozen", any subsequent changes
878 to it must be done with change_item_tree(), unless they only modify AND/OR
879 items and use a copy created by SELECT_LEX::get_optimizable_conditions().
880 Same is true for 'having_cond'.
881 */
882 Item *m_where_cond;
883
884 /// Condition to be evaluated on grouped rows after grouping.
885 Item *m_having_cond;
886
887 public:
888
889 /**
890 Saved values of the WHERE and HAVING clauses. Allowed values are:
891 - COND_UNDEF if the condition was not specified in the query or if it
892 has not been optimized yet
893 - COND_TRUE if the condition is always true
894 - COND_FALSE if the condition is impossible
895 - COND_OK otherwise
896 */
897 Item::cond_result cond_value;
898 Item::cond_result having_value;
899
900 /// Reference to LEX that this query block belongs to
901 LEX *parent_lex;
902 /// Indicates whether this query block contains the WITH ROLLUP clause
903 enum olap_type olap;
904 /// List of tables in FROM clause - use TABLE_LIST::next_local to traverse
905 SQL_I_List<TABLE_LIST> table_list;
906
907 /**
908 GROUP BY clause.
909 This list may be mutated during optimization (by remove_const()),
910 so for prepared statements, we keep a copy of the ORDER.next pointers in
911 group_list_ptrs, and re-establish the original list before each execution.
912 */
913 SQL_I_List<ORDER> group_list;
914 Group_list_ptrs *group_list_ptrs;
915
916 /**
917 List of columns and expressions:
918 SELECT: Columns and expressions in the SELECT list.
919 UPDATE: Columns in the SET clause.
920 */
921 List<Item> item_list;
922 bool is_item_list_lookup;
923
924 /// Number of GROUP BY expressions added to all_fields
925 int hidden_group_field_count;
926
927 List<Item> &fields_list; ///< hold field list
928 List<Item> all_fields; ///< to store all expressions used in query
929 /**
930 Usually a pointer to ftfunc_list_alloc, but in UNION this is used to create
931 fake select_lex that consolidates result fields of UNION
932 */
933 List<Item_func_match> *ftfunc_list;
934 List<Item_func_match> ftfunc_list_alloc;
935 /**
936 After optimization it is pointer to corresponding JOIN. This member
937 should be changed only when THD::LOCK_query_plan mutex is taken.
938 */
939 JOIN *join;
940 /// join list of the top level
941 List<TABLE_LIST> top_join_list;
942 /// list for the currently parsed join
943 List<TABLE_LIST> *join_list;
944 /// table embedding the above list
945 TABLE_LIST *embedding;
946 /// List of semi-join nests generated for this query block
947 List<TABLE_LIST> sj_nests;
948 /**
949 Points to first leaf table of query block. After setup_tables() is done,
950 this is a list of base tables and derived tables. After derived tables
951 processing is done, this is a list of base tables only.
952 Use TABLE_LIST::next_leaf to traverse the list.
953 */
954 TABLE_LIST *leaf_tables;
955 /// Number of leaf tables in this query block.
956 uint leaf_table_count;
957 /// Number of derived tables and views in this query block.
958 uint derived_table_count;
959 /// Number of materialized derived tables and views in this query block.
960 uint materialized_derived_table_count;
961 /**
962 True if query block has semi-join nests merged into it. Notice that this
963 is updated earlier than sj_nests, so check this if info is needed
964 before the full resolver process is complete.
965 */
966 bool has_sj_nests;
967 /// Number of partitioned tables
968 uint partitioned_table_count;
969 /**
970 SELECT_LEX type enum
971 */
972 enum type_enum {
973 SLT_NONE= 0,
974 SLT_PRIMARY,
975 SLT_SIMPLE,
976 SLT_DERIVED,
977 SLT_SUBQUERY,
978 SLT_UNION,
979 SLT_UNION_RESULT,
980 SLT_MATERIALIZED,
981 // Total:
982 SLT_total ///< fake type, total number of all valid types
983 // Don't insert new types below this line!
984 };
985
986 /**
987 ORDER BY clause.
988 This list may be mutated during optimization (by remove_const()),
989 so for prepared statements, we keep a copy of the ORDER.next pointers in
990 order_list_ptrs, and re-establish the original list before each execution.
991 */
992 SQL_I_List<ORDER> order_list;
993 Group_list_ptrs *order_list_ptrs;
994
995 /// LIMIT clause, NULL if no limit is given
996 Item *select_limit;
997 /// LIMIT ... OFFSET clause, NULL if no offset is given
998 Item *offset_limit;
999
1000 /// The complete ref pointer array, with 5 slices (see class JOIN too)
1001 Ref_ptr_array ref_pointer_array;
1002 /// Slice 0 of array, with pointers to all expressions in all_fields
1003 Ref_ptr_array ref_ptrs;
1004
1005 /**
1006 number of items in select_list and HAVING clause used to get number
1007 bigger then can be number of entries that will be added to all item
1008 list during split_sum_func
1009 */
1010 uint select_n_having_items;
1011 uint cond_count; ///< number of arguments of and/or/xor in where/having/on
1012 uint between_count; ///< number of between predicates in where/having/on
1013 uint max_equal_elems; ///< maximal number of elements in multiple equalities
1014 /**
1015 Number of fields used in select list or where clause of current select
1016 and all inner subselects.
1017 */
1018 uint select_n_where_fields;
1019
1020 /// Parse context: indicates where the current expression is being parsed
1021 enum_parsing_context parsing_place;
1022 /// Parse context: is inside a set function if this is positive
1023 uint in_sum_expr;
1024
1025 /**
1026 True if contains or aggregates set functions.
1027 @note this is wrong when a locally found set function is aggregated
1028 in an outer query block.
1029 */
1030 bool with_sum_func;
1031 /// Number of Item_sum-derived objects in this SELECT
1032 uint n_sum_items;
1033 /// Number of Item_sum-derived objects in children and descendant SELECTs
1034 uint n_child_sum_items;
1035
1036 uint select_number; ///< Query block number (used for EXPLAIN)
1037 /**
1038 Nesting level of query block, outer-most query block has level 0,
1039 its subqueries have level 1, etc. @see also sql/item_sum.h.
1040 */
1041 int nest_level;
1042 /// Circular linked list of sum func in nested selects
1043 Item_sum *inner_sum_func_list;
1044 /**
1045 Number of wildcards used in the SELECT list. For example,
1046 SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
1047 has 3 wildcards.
1048 */
1049 uint with_wild;
1050 bool braces; ///< SELECT ... UNION (SELECT ... ) <- this braces
1051 /// TRUE when having fix field called in processing of this query block
1052 bool having_fix_field;
1053 /// TRUE when GROUP BY fix field called in processing of this query block
1054 bool group_fix_field;
1055 /// List of references to fields referenced from inner query blocks
1056 List<Item_outer_ref> inner_refs_list;
1057
1058 /// explicit LIMIT clause is used
1059 bool explicit_limit;
1060 /**
1061 HAVING clause contains subquery => we can't close tables before
1062 query processing end even if we use temporary table
1063 */
1064 bool subquery_in_having;
1065 /**
1066 This variable is required to ensure proper work of subqueries and
1067 stored procedures. Generally, one should use the states of
1068 Query_arena to determine if it's a statement prepare or first
1069 execution of a stored procedure. However, in case when there was an
1070 error during the first execution of a stored procedure, the SP body
1071 is not expelled from the SP cache. Therefore, a deeply nested
1072 subquery might be left unoptimized. So we need this per-subquery
1073 variable to inidicate the optimization/execution state of every
1074 subquery. Prepared statements work OK in that regard, as in
1075 case of an error during prepare the PS is not created.
1076 */
1077 bool first_execution;
1078 /// True when semi-join pull-out processing is complete
1079 bool sj_pullout_done;
1080 /// exclude this query block from unique_table() check
1081 bool exclude_from_table_unique_test;
1082 /// Allow merge of immediate unnamed derived tables
1083 bool allow_merge_derived;
1084 /**
1085 This is a copy of the original JOIN USING list that comes from
1086 the parser. The parser :
1087 1. Sets the natural_join of the second TABLE_LIST in the join
1088 and the st_select_lex::prev_join_using.
1089 2. Makes a parent TABLE_LIST and sets its is_natural_join/
1090 join_using_fields members.
1091 3. Uses the wrapper TABLE_LIST as a table in the upper level.
1092 We cannot assign directly to join_using_fields in the parser because
1093 at stage (1.) the parent TABLE_LIST is not constructed yet and
1094 the assignment will override the JOIN USING fields of the lower level
1095 joins on the right.
1096 */
1097 List<String> *prev_join_using;
1098 /**
1099 The set of those tables whose fields are referenced in the select list of
1100 this select level.
1101 */
1102 table_map select_list_tables;
1103 table_map outer_join; ///< Bitmap of all inner tables from outer joins
1104
1105 /// Query-block-level hints, for this query block
1106 Opt_hints_qb *opt_hints_qb;
1107
1108
1109 /**
1110 @note the group_by and order_by lists below will probably be added to the
1111 constructor when the parser is converted into a true bottom-up design.
1112 */
1113 st_select_lex(TABLE_LIST *table_list, List<Item> *item_list,
1114 Item *where, Item *having, Item *limit, Item *offset
1115 //SQL_I_LIST<ORDER> *group_by, SQL_I_LIST<ORDER> order_by
1116 );
1117
master_unit()1118 st_select_lex_unit *master_unit() const { return master; }
first_inner_unit()1119 st_select_lex_unit *first_inner_unit() const { return slave; }
outer_select()1120 SELECT_LEX *outer_select() const { return master->outer_select(); }
next_select()1121 SELECT_LEX *next_select() const { return next; }
1122
last_select()1123 st_select_lex* last_select()
1124 {
1125 st_select_lex* mylast= this;
1126 for (; mylast->next_select(); mylast= mylast->next_select())
1127 {}
1128 return mylast;
1129 }
1130
next_select_in_list()1131 SELECT_LEX *next_select_in_list() const { return link_next; }
1132
1133 void mark_as_dependent(st_select_lex *last);
1134
1135 /// @return true if query block is explicitly grouped (non-empty GROUP BY)
is_explicitly_grouped()1136 bool is_explicitly_grouped() const { return group_list.elements > 0; }
1137
1138 /**
1139 @return true if this query block is implicitly grouped, ie it is not
1140 explicitly grouped but contains references to set functions.
1141 The query will return max. 1 row (@see also is_single_grouped()).
1142 */
is_implicitly_grouped()1143 bool is_implicitly_grouped() const
1144 { return m_agg_func_used && group_list.elements == 0; }
1145
1146 /**
1147 @return true if this query block is implicitly grouped and returns exactly
1148 one row, which happens when it does not have a HAVING clause.
1149 */
is_single_grouped()1150 bool is_single_grouped() const
1151 {
1152 return m_agg_func_used &&
1153 group_list.elements == 0 &&
1154 m_having_cond == NULL;
1155 }
1156
1157 /**
1158 @return true if this query block is explicitly or implicitly grouped.
1159 @note a query with DISTINCT is not considered to be aggregated.
1160 @note in standard SQL, a query with HAVING is defined as grouped, however
1161 MySQL allows HAVING without any aggregation to be the same as WHERE.
1162 */
is_grouped()1163 bool is_grouped() const
1164 { return group_list.elements > 0 || m_agg_func_used; }
1165
1166 /// @return true if this query block contains DISTINCT at start of select list
is_distinct()1167 bool is_distinct() const
1168 { return active_options() & SELECT_DISTINCT; }
1169
1170 /**
1171 @return true if this query block contains an ORDER BY clause.
1172
1173 @note returns false if ORDER BY has been eliminated, e.g if the query
1174 can return max. 1 row.
1175 */
is_ordered()1176 bool is_ordered() const
1177 { return order_list.elements > 0; }
1178
1179 /// @return true if this query block has a LIMIT clause
has_limit()1180 bool has_limit() const
1181 { return select_limit != NULL; }
1182
1183 /// @return true if query block references full-text functions
has_ft_funcs()1184 bool has_ft_funcs() const
1185 { return ftfunc_list->elements > 0; }
1186
1187 void invalidate();
1188
1189 bool set_braces(bool value);
get_in_sum_expr()1190 uint get_in_sum_expr() const { return in_sum_expr; }
1191
1192 bool add_item_to_list(THD *thd, Item *item);
1193 void add_group_to_list(ORDER *order);
1194 bool add_ftfunc_to_list(Item_func_match *func);
1195 void add_order_to_list(ORDER *order);
1196 TABLE_LIST* add_table_to_list(THD *thd, Table_ident *table,
1197 LEX_STRING *alias,
1198 ulong table_options,
1199 thr_lock_type flags= TL_UNLOCK,
1200 enum_mdl_type mdl_type= MDL_SHARED_READ,
1201 List<Index_hint> *hints= 0,
1202 List<String> *partition_names= 0,
1203 LEX_STRING *option= 0);
get_table_list()1204 TABLE_LIST* get_table_list() const { return table_list.first; }
1205 bool init_nested_join(THD *thd);
1206 TABLE_LIST *end_nested_join(THD *thd);
1207 TABLE_LIST *nest_last_join(THD *thd);
1208 void add_joined_table(TABLE_LIST *table);
1209 TABLE_LIST *convert_right_join();
get_item_list()1210 List<Item>* get_item_list() { return &item_list; }
1211
1212 // Check privileges for views that are merged into query block
1213 bool check_view_privileges(THD *thd,
1214 ulong want_privilege_first,
1215 ulong want_privilege_next);
1216
1217 // Resolve and prepare information about tables for one query block
1218 bool setup_tables(THD *thd, TABLE_LIST *tables, bool select_insert);
1219
1220 // Resolve derived table and view information for a query block
1221 bool resolve_derived(THD *thd, bool apply_semijoin);
1222
1223 // Propagate exclusion from table uniqueness test into subqueries
1224 void propagate_unique_test_exclusion();
1225
1226 // Add full-text function elements from a list into this query block
1227 bool add_ftfunc_list(List<Item_func_match> *ftfuncs);
1228
1229 void set_lock_for_tables(thr_lock_type lock_type);
init_order()1230 inline void init_order()
1231 {
1232 assert(order_list.elements == 0);
1233 order_list.elements= 0;
1234 order_list.first= 0;
1235 order_list.next= &order_list.first;
1236 }
1237 /*
1238 This method created for reiniting LEX in mysql_admin_table() and can be
1239 used only if you are going remove all SELECT_LEX & units except belonger
1240 to LEX (LEX::unit & LEX::select, for other purposes use
1241 SELECT_LEX_UNIT::exclude_level()
1242 */
cut_subtree()1243 void cut_subtree() { slave= 0; }
1244 bool test_limit();
1245 /**
1246 Get offset for LIMIT.
1247
1248 Evaluate offset item if necessary.
1249
1250 @return Number of rows to skip.
1251 */
1252 ha_rows get_offset();
1253 /**
1254 Get limit.
1255
1256 Evaluate limit item if necessary.
1257
1258 @return Limit of rows in result.
1259 */
1260 ha_rows get_limit();
1261
1262 /// Assign a default name resolution object for this query block.
1263 bool set_context(Name_resolution_context *outer_context);
1264
1265 bool setup_ref_array(THD *thd);
1266 void print(THD *thd, String *str, enum_query_type query_type);
1267 static void print_order(String *str,
1268 ORDER *order,
1269 enum_query_type query_type);
1270 void print_limit(THD *thd, String *str, enum_query_type query_type);
1271 void fix_prepare_information(THD *thd);
1272
1273 virtual bool accept(Select_lex_visitor *visitor);
1274
1275 /**
1276 Cleanup this subtree (this SELECT_LEX and all nested SELECT_LEXes and
1277 SELECT_LEX_UNITs).
1278 @param full if false only partial cleanup is done, JOINs and JOIN_TABs are
1279 kept to provide info for EXPLAIN CONNECTION; if true, complete cleanup is
1280 done, all JOINs are freed.
1281 */
1282 bool cleanup(bool full);
1283 /*
1284 Recursively cleanup the join of this select lex and of all nested
1285 select lexes. This is not a full cleanup.
1286 */
1287 void cleanup_all_joins();
1288
1289 /*
1290 Add a index hint to the tagged list of hints. The type and clause of the
1291 hint will be the current ones (set by set_index_hint())
1292 */
1293 bool add_index_hint (THD *thd, char *str, uint length);
1294
1295 /* make a list to hold index hints */
1296 void alloc_index_hints (THD *thd);
1297
1298 /// Return true if this query block is part of a UNION
is_part_of_union()1299 bool is_part_of_union() const { return master_unit()->is_union(); }
1300
1301 /*
1302 For MODE_ONLY_FULL_GROUP_BY we need to know if
1303 this query block is the aggregation query of at least one aggregate
1304 function.
1305 */
agg_func_used()1306 bool agg_func_used() const { return m_agg_func_used; }
json_agg_func_used()1307 bool json_agg_func_used() const { return m_json_agg_func_used; }
1308
set_agg_func_used(bool val)1309 void set_agg_func_used(bool val) { m_agg_func_used= val; }
1310
set_json_agg_func_used(bool val)1311 void set_json_agg_func_used(bool val) { m_json_agg_func_used= val; }
1312 /// Lookup for SELECT_LEX type
1313 type_enum type();
1314
1315 /// Lookup for a type string
get_type_str(const THD * thd)1316 const char *get_type_str(const THD *thd) { return type_str[type()]; }
get_type_str(type_enum type)1317 static const char *get_type_str(type_enum type) { return type_str[type]; }
1318
is_dependent()1319 bool is_dependent() const { return uncacheable & UNCACHEABLE_DEPENDENT; }
is_cacheable()1320 bool is_cacheable() const
1321 {
1322 return !uncacheable;
1323 }
1324
1325 /// Include query block inside a query expression.
1326 void include_down(LEX *lex, st_select_lex_unit *outer);
1327
1328 /// Include a query block next to another query block.
1329 void include_neighbour(LEX *lex, st_select_lex *before);
1330
1331 /// Include query block inside a query expression, but do not link.
1332 void include_standalone(st_select_lex_unit *sel, st_select_lex **ref);
1333
1334 /// Include query block into global list.
1335 void include_in_global(st_select_lex **plink);
1336
1337 /// Include chain of query blocks into global list.
1338 void include_chain_in_global(st_select_lex **start);
1339
1340 /// Renumber query blocks of contained query expressions
1341 void renumber(LEX *lex);
1342
1343 /**
1344 Set pointer to corresponding JOIN object.
1345 The function sets the pointer only after acquiring THD::LOCK_query_plan
1346 mutex. This is needed to avoid races when EXPLAIN FOR CONNECTION is used.
1347 */
1348 void set_join(JOIN *join_arg);
1349 /**
1350 Does permanent transformations which are local to a query block (which do
1351 not merge it to another block).
1352 */
1353 bool apply_local_transforms(THD *thd, bool prune);
1354
1355 bool get_optimizable_conditions(THD *thd,
1356 Item **new_where, Item **new_having);
1357
1358 bool validate_outermost_option(LEX *lex, const char *wrong_option) const;
1359 bool validate_base_options(LEX *lex, ulonglong options) const;
1360
1361 private:
1362 // Delete unused columns from merged derived tables
1363 void delete_unused_merged_columns(List<TABLE_LIST> *tables);
1364
1365 bool m_agg_func_used;
1366 bool m_json_agg_func_used;
1367
1368 /// Helper for fix_prepare_information()
1369 void fix_prepare_information_for_order(THD *thd,
1370 SQL_I_List<ORDER> *list,
1371 Group_list_ptrs **list_ptrs);
1372 static const char *type_str[SLT_total];
1373
1374 friend class st_select_lex_unit;
1375
1376 bool record_join_nest_info(List<TABLE_LIST> *tables);
1377 bool simplify_joins(THD *thd,
1378 List<TABLE_LIST> *join_list,
1379 bool top, bool in_sj,
1380 Item **new_conds,
1381 uint *changelog= NULL);
1382 /// Merge derived table into query block
1383 public:
1384 bool merge_derived(THD *thd, TABLE_LIST *derived_table);
1385 bool is_in_select_list(Item *cand);
1386 private:
1387 bool convert_subquery_to_semijoin(Item_exists_subselect *subq_pred);
1388 void remap_tables(THD *thd);
1389 bool resolve_subquery(THD *thd);
1390 bool resolve_rollup(THD *thd);
1391 bool change_group_ref(THD *thd, Item_func *expr, bool *changed);
1392 bool flatten_subqueries();
1393 bool setup_wild(THD *thd);
1394 bool setup_order_final(THD *thd);
1395 bool setup_group(THD *thd);
1396 void remove_redundant_subquery_clauses(THD *thd,
1397 int hidden_group_field_count);
1398 void repoint_contexts_of_join_nests(List<TABLE_LIST> join_list);
1399 void empty_order_list(SELECT_LEX *sl);
1400 /**
1401 Pointer to collection of subqueries candidate for semijoin
1402 conversion.
1403 Template parameter is "true": no need to run DTORs on pointers.
1404 */
1405 Mem_root_array<Item_exists_subselect*, true> *sj_candidates;
1406 public:
1407 /// How many expressions are part of the order by but not select list.
1408 int hidden_order_field_count;
1409
1410 bool fix_inner_refs(THD *thd);
1411 bool setup_conds(THD *thd);
1412 bool prepare(THD *thd);
1413 bool optimize(THD *thd);
1414 void reset_nj_counters(List<TABLE_LIST> *join_list= NULL);
1415 bool check_only_full_group_by(THD *thd);
1416
1417 /// Merge name resolution context objects of a subquery into its parent
1418 void merge_contexts(SELECT_LEX *inner);
1419
1420 /**
1421 Returns which subquery execution strategies can be used for this query block.
1422
1423 @param thd Pointer to THD object for session.
1424 Used to access optimizer_switch
1425
1426 @retval EXEC_MATERIALIZATION Subquery Materialization should be used
1427 @retval EXEC_EXISTS In-to-exists execution should be used
1428 @retval EXEC_EXISTS_OR_MAT A cost-based decision should be made
1429 */
1430 Item_exists_subselect::enum_exec_method subquery_strategy(THD *thd) const;
1431
1432 /**
1433 Returns whether semi-join is enabled for this query block
1434
1435 @see @c Opt_hints_qb::semijoin_enabled for details on how hints
1436 affect this decision. If there are no hints for this query block,
1437 optimizer_switch setting determines whether semi-join is used.
1438
1439 @param thd Pointer to THD object for session.
1440 Used to access optimizer_switch
1441
1442 @return true if semijoin is enabled,
1443 false otherwise
1444 */
1445 bool semijoin_enabled(THD *thd) const;
1446 /**
1447 Update available semijoin strategies for semijoin nests.
1448
1449 Available semijoin strategies needs to be updated on every execution since
1450 optimizer_switch setting may have changed.
1451
1452 @param thd Pointer to THD object for session.
1453 Used to access optimizer_switch
1454 */
1455 void update_semijoin_strategies(THD *thd);
1456
1457 /**
1458 Add item to the hidden part of select list
1459
1460 @param item item to add
1461
1462 @return Pointer to ref_ptr for the added item
1463 */
1464 Item **add_hidden_item(Item *item);
1465 };
1466 typedef class st_select_lex SELECT_LEX;
1467
is_union()1468 inline bool st_select_lex_unit::is_union() const
1469 {
1470 return first_select()->next_select() &&
1471 first_select()->next_select()->linkage == UNION_TYPE;
1472 }
1473
1474 #ifdef MYSQL_SERVER
1475
1476 struct Cast_type
1477 {
1478 Cast_target target;
1479 const CHARSET_INFO *charset;
1480 ulong type_flags;
1481 const char *length;
1482 const char *dec;
1483 };
1484
1485
1486 struct Limit_options
1487 {
1488 Item *limit;
1489 Item *opt_offset;
1490 /*
1491 true for "LIMIT offset,limit" and false for "LIMIT limit OFFSET offset"
1492 */
1493 bool is_offset_first;
1494 };
1495
1496
1497 struct Query_options {
1498 ulonglong query_spec_options;
1499 enum SELECT_LEX::e_sql_cache sql_cache;
1500
1501 bool merge(const Query_options &a, const Query_options &b);
1502 bool save_to(Parse_context *);
1503 };
1504
1505
1506 /**
1507 Argument values for PROCEDURE ANALYSE(...)
1508 */
1509
1510 struct Proc_analyse_params
1511 {
1512 uint max_tree_elements; //< maximum number of distinct values per column
1513 uint max_treemem; //< maximum amount of memory to allocate per column
1514
1515 static const uint default_max_tree_elements= 256;
1516 static const uint default_max_treemem= 8192;
1517 };
1518
1519
1520 struct Select_lock_type
1521 {
1522 bool is_set;
1523 thr_lock_type lock_type;
1524 bool is_safe_to_cache_query;
1525 };
1526
1527
1528 /**
1529 Helper for the sql_exchange class
1530 */
1531
1532 struct Line_separators
1533 {
1534 const String *line_term;
1535 const String *line_start;
1536
cleanupLine_separators1537 void cleanup() { line_term= line_start= NULL; }
merge_line_separatorsLine_separators1538 void merge_line_separators(const Line_separators &s)
1539 {
1540 if (s.line_term != NULL)
1541 line_term= s.line_term;
1542 if (s.line_start != NULL)
1543 line_start= s.line_start;
1544 }
1545 };
1546
1547
1548 /**
1549 Helper for the sql_exchange class
1550 */
1551
1552 struct Field_separators
1553 {
1554 const String *field_term;
1555 const String *escaped;
1556 const String *enclosed;
1557 bool opt_enclosed;
1558
cleanupField_separators1559 void cleanup()
1560 {
1561 field_term= escaped= enclosed= NULL;
1562 opt_enclosed= false;
1563 }
merge_field_separatorsField_separators1564 void merge_field_separators(const Field_separators &s)
1565 {
1566 if (s.field_term != NULL)
1567 field_term= s.field_term;
1568 if (s.escaped != NULL)
1569 escaped= s.escaped;
1570 if (s.enclosed != NULL)
1571 enclosed= s.enclosed;
1572 // TODO: a bug?
1573 // OPTIONALLY ENCLOSED BY x ENCLOSED BY y == OPTIONALLY ENCLOSED BY y
1574 if (s.opt_enclosed)
1575 opt_enclosed= s.opt_enclosed;
1576 }
1577 };
1578
1579
1580 enum delete_option_enum {
1581 DELETE_QUICK = 1 << 0,
1582 DELETE_LOW_PRIORITY = 1 << 1,
1583 DELETE_IGNORE = 1 << 2
1584 };
1585
1586
1587 union YYSTYPE {
1588 /*
1589 Hint parser section (sql_hints.yy)
1590 */
1591 opt_hints_enum hint_type;
1592 LEX_CSTRING hint_string;
1593 class PT_hint *hint;
1594 class PT_hint_list *hint_list;
1595 Hint_param_index_list hint_param_index_list;
1596 Hint_param_table hint_param_table;
1597 Hint_param_table_list hint_param_table_list;
1598
1599 /*
1600 Main parser section (sql_yacc.yy)
1601 */
1602 int num;
1603 ulong ulong_num;
1604 ulonglong ulonglong_number;
1605 longlong longlong_number;
1606 LEX_STRING lex_str;
1607 LEX_CSTRING lex_cstr;
1608 LEX_STRING *lex_str_ptr;
1609 LEX_SYMBOL symbol;
1610 Table_ident *table;
1611 char *simple_string;
1612 Item *item;
1613 Item_num *item_num;
1614 List<Item> *item_list;
1615 List<String> *string_list;
1616 String *string;
1617 Key_part_spec *key_part;
1618 TABLE_LIST *table_list;
1619 udf_func *udf;
1620 LEX_USER *lex_user;
1621 struct sys_var_with_base variable;
1622 enum enum_var_type var_type;
1623 keytype key_type;
1624 enum ha_key_alg key_alg;
1625 handlerton *db_type;
1626 enum row_type row_type;
1627 enum ha_rkey_function ha_rkey_mode;
1628 enum enum_ha_read_modes ha_read_mode;
1629 enum enum_tx_isolation tx_isolation;
1630 const char *c_str;
1631 struct
1632 {
1633 const CHARSET_INFO *charset;
1634 ulong type_flags;
1635 } charset_with_flags;
1636 struct
1637 {
1638 const char *length;
1639 const char *dec;
1640 } precision;
1641 struct Cast_type cast_type;
1642 enum Item_udftype udf_type;
1643 const CHARSET_INFO *charset;
1644 thr_lock_type lock_type;
1645 interval_type interval, interval_time_st;
1646 timestamp_type date_time_type;
1647 st_select_lex *select_lex;
1648 chooser_compare_func_creator boolfunc2creator;
1649 class sp_condition_value *spcondvalue;
1650 struct { int vars, conds, hndlrs, curs; } spblock;
1651 sp_name *spname;
1652 LEX *lex;
1653 sp_head *sphead;
1654 struct p_elem_val *p_elem_value;
1655 enum index_hint_type index_hint;
1656 enum enum_filetype filetype;
1657 enum fk_option m_fk_option;
1658 enum enum_yes_no_unknown m_yes_no_unk;
1659 enum_condition_item_name da_condition_item_name;
1660 Diagnostics_information::Which_area diag_area;
1661 Diagnostics_information *diag_info;
1662 Statement_information_item *stmt_info_item;
1663 Statement_information_item::Name stmt_info_item_name;
1664 List<Statement_information_item> *stmt_info_list;
1665 Condition_information_item *cond_info_item;
1666 Condition_information_item::Name cond_info_item_name;
1667 List<Condition_information_item> *cond_info_list;
1668 bool is_not_empty;
1669 Set_signal_information *signal_item_list;
1670 enum enum_trigger_order_type trigger_action_order_type;
1671 struct
1672 {
1673 enum enum_trigger_order_type ordering_clause;
1674 LEX_STRING anchor_trigger_name;
1675 } trg_characteristics;
1676 class Index_hint *key_usage_element;
1677 List<Index_hint> *key_usage_list;
1678 class PT_subselect *subselect;
1679 class PT_item_list *item_list2;
1680 class PT_order_expr *order_expr;
1681 class PT_order_list *order_list;
1682 struct Limit_options limit_options;
1683 Query_options select_options;
1684 class PT_limit_clause *limit_clause;
1685 Parse_tree_node *node;
1686 class PT_select_part2_derived *select_part2_derived;
1687 enum olap_type olap_type;
1688 class PT_group *group;
1689 class PT_order *order;
1690 struct Proc_analyse_params procedure_analyse_params;
1691 class PT_procedure_analyse *procedure_analyse;
1692 Select_lock_type select_lock_type;
1693 class PT_union_order_or_limit *union_order_or_limit;
1694 class PT_table_expression *table_expression;
1695 class PT_table_list *table_list2;
1696 class PT_join_table_list *join_table_list;
1697 class PT_select_paren_derived *select_paren_derived;
1698 class PT_select_lex *select_lex2;
1699 class PT_internal_variable_name *internal_variable_name;
1700 class PT_option_value_following_option_type *option_value_following_option_type;
1701 class PT_option_value_no_option_type *option_value_no_option_type;
1702 class PT_option_value_list_head *option_value_list;
1703 class PT_start_option_value_list *start_option_value_list;
1704 class PT_transaction_access_mode *transaction_access_mode;
1705 class PT_isolation_level *isolation_level;
1706 class PT_transaction_characteristics *transaction_characteristics;
1707 class PT_start_option_value_list_following_option_type
1708 *start_option_value_list_following_option_type;
1709 class PT_set *set;
1710 class PT_union_list *union_list;
1711 Line_separators line_separators;
1712 Field_separators field_separators;
1713 class PT_into_destination *into_destination;
1714 class PT_select_var *select_var_ident;
1715 class PT_select_var_list *select_var_list;
1716 class PT_select_options_and_item_list *select_options_and_item_list;
1717 class PT_select_part2 *select_part2;
1718 class PT_table_reference_list *table_reference_list;
1719 class PT_select_paren *select_paren;
1720 class PT_select_init *select_init;
1721 class PT_select_init2 *select_init2;
1722 class PT_select *select;
1723 class Item_param *param_marker;
1724 class PTI_text_literal *text_literal;
1725 XID *xid;
1726 enum xa_option_words xa_option_type;
1727 struct {
1728 Item *column;
1729 Item *value;
1730 } column_value_pair;
1731 struct {
1732 class PT_item_list *column_list;
1733 class PT_item_list *value_list;
1734 } column_value_list_pair;
1735 struct {
1736 class PT_item_list *column_list;
1737 class PT_insert_values_list *row_value_list;
1738 } column_row_value_list_pair;
1739 struct {
1740 class PT_item_list *column_list;
1741 class PT_insert_query_expression *insert_query_expression;
1742 } insert_from_subquery;
1743 class PT_create_select *create_select;
1744 class PT_insert_values_list *values_list;
1745 class PT_insert_query_expression *insert_query_expression;
1746 class PT_statement *statement;
1747 class Table_ident *table_ident;
1748 Mem_root_array_YY<Table_ident *> table_ident_list;
1749 delete_option_enum opt_delete_option;
1750 class PT_hint_list *optimizer_hints;
1751 enum alter_instance_action_enum alter_instance_action;
1752 };
1753
1754 #endif
1755
1756
1757 /**
1758 Utility RAII class to save/modify/restore the
1759 semijoin_disallowed flag.
1760 */
1761 class Disable_semijoin_flattening
1762 {
1763 public:
Disable_semijoin_flattening(SELECT_LEX * select_ptr,bool apply)1764 Disable_semijoin_flattening(SELECT_LEX *select_ptr, bool apply)
1765 : select(NULL), saved_value()
1766 {
1767 if (select_ptr && apply)
1768 {
1769 select= select_ptr;
1770 saved_value= select->semijoin_disallowed;
1771 select->semijoin_disallowed= true;
1772 }
1773 }
~Disable_semijoin_flattening()1774 ~Disable_semijoin_flattening()
1775 {
1776 if (select)
1777 select->semijoin_disallowed= saved_value;
1778 }
1779 private:
1780 SELECT_LEX *select;
1781 bool saved_value;
1782 };
1783
1784
1785 typedef struct struct_slave_connection
1786 {
1787 char *user;
1788 char *password;
1789 char *plugin_auth;
1790 char *plugin_dir;
1791
1792 void reset();
1793 } LEX_SLAVE_CONNECTION;
1794
1795 struct st_sp_chistics
1796 {
1797 LEX_STRING comment;
1798 enum enum_sp_suid_behaviour suid;
1799 bool detistic;
1800 enum enum_sp_data_access daccess;
1801 };
1802
1803 extern const LEX_STRING null_lex_str;
1804 extern const LEX_STRING empty_lex_str;
1805 extern const LEX_CSTRING null_lex_cstr;
1806 extern const LEX_CSTRING empty_lex_cstr;
1807
1808 struct st_trg_chistics
1809 {
1810 enum enum_trigger_action_time_type action_time;
1811 enum enum_trigger_event_type event;
1812
1813 /**
1814 FOLLOWS or PRECEDES as specified in the CREATE TRIGGER statement.
1815 */
1816 enum enum_trigger_order_type ordering_clause;
1817
1818 /**
1819 Trigger name referenced in the FOLLOWS/PRECEDES clause of the CREATE TRIGGER
1820 statement.
1821 */
1822 LEX_STRING anchor_trigger_name;
1823 };
1824
1825 extern sys_var *trg_new_row_fake_var;
1826
1827 extern const LEX_STRING null_lex_str;
1828
1829 class Sroutine_hash_entry;
1830
1831 /*
1832 Class representing list of all tables used by statement and other
1833 information which is necessary for opening and locking its tables,
1834 like SQL command for this statement.
1835
1836 Also contains information about stored functions used by statement
1837 since during its execution we may have to add all tables used by its
1838 stored functions/triggers to this list in order to pre-open and lock
1839 them.
1840
1841 Also used by LEX::reset_n_backup/restore_backup_query_tables_list()
1842 methods to save and restore this information.
1843 */
1844
1845 class Query_tables_list
1846 {
1847 public:
1848 /**
1849 SQL command for this statement. Part of this class since the
1850 process of opening and locking tables for the statement needs
1851 this information to determine correct type of lock for some of
1852 the tables.
1853 */
1854 enum_sql_command sql_command;
1855 /* Global list of all tables used by this statement */
1856 TABLE_LIST *query_tables;
1857 /* Pointer to next_global member of last element in the previous list. */
1858 TABLE_LIST **query_tables_last;
1859 /*
1860 If non-0 then indicates that query requires prelocking and points to
1861 next_global member of last own element in query table list (i.e. last
1862 table which was not added to it as part of preparation to prelocking).
1863 0 - indicates that this query does not need prelocking.
1864 */
1865 TABLE_LIST **query_tables_own_last;
1866 /*
1867 Set of stored routines called by statement.
1868 (Note that we use lazy-initialization for this hash).
1869 */
1870 enum { START_SROUTINES_HASH_SIZE= 16 };
1871 HASH sroutines;
1872 /*
1873 List linking elements of 'sroutines' set. Allows you to add new elements
1874 to this set as you iterate through the list of existing elements.
1875 'sroutines_list_own_last' is pointer to ::next member of last element of
1876 this list which represents routine which is explicitly used by query.
1877 'sroutines_list_own_elements' number of explicitly used routines.
1878 We use these two members for restoring of 'sroutines_list' to the state
1879 in which it was right after query parsing.
1880 */
1881 SQL_I_List<Sroutine_hash_entry> sroutines_list;
1882 Sroutine_hash_entry **sroutines_list_own_last;
1883 uint sroutines_list_own_elements;
1884
1885 /**
1886 Locking state of tables in this particular statement.
1887
1888 If we under LOCK TABLES or in prelocked mode we consider tables
1889 for the statement to be "locked" if there was a call to lock_tables()
1890 (which called handler::start_stmt()) for tables of this statement
1891 and there was no matching close_thread_tables() call.
1892
1893 As result this state may differ significantly from one represented
1894 by Open_tables_state::lock/locked_tables_mode more, which are always
1895 "on" under LOCK TABLES or in prelocked mode.
1896 */
1897 enum enum_lock_tables_state {
1898 LTS_NOT_LOCKED = 0,
1899 LTS_LOCKED
1900 };
1901 enum_lock_tables_state lock_tables_state;
is_query_tables_locked()1902 bool is_query_tables_locked()
1903 {
1904 return (lock_tables_state == LTS_LOCKED);
1905 }
1906
1907 /**
1908 Number of tables which were open by open_tables() and to be locked
1909 by lock_tables().
1910 Note that we set this member only in some cases, when this value
1911 needs to be passed from open_tables() to lock_tables() which are
1912 separated by some amount of code.
1913 */
1914 uint table_count;
1915
1916 /*
1917 These constructor and destructor serve for creation/destruction
1918 of Query_tables_list instances which are used as backup storage.
1919 */
Query_tables_list()1920 Query_tables_list() {}
~Query_tables_list()1921 ~Query_tables_list() {}
1922
1923 /* Initializes (or resets) Query_tables_list object for "real" use. */
1924 void reset_query_tables_list(bool init);
1925 void destroy_query_tables_list();
set_query_tables_list(Query_tables_list * state)1926 void set_query_tables_list(Query_tables_list *state)
1927 {
1928 *this= *state;
1929 }
1930
1931 /*
1932 Direct addition to the list of query tables.
1933 If you are using this function, you must ensure that the table
1934 object, in particular table->db member, is initialized.
1935 */
add_to_query_tables(TABLE_LIST * table)1936 void add_to_query_tables(TABLE_LIST *table)
1937 {
1938 *(table->prev_global= query_tables_last)= table;
1939 query_tables_last= &table->next_global;
1940 }
requires_prelocking()1941 bool requires_prelocking()
1942 {
1943 return MY_TEST(query_tables_own_last);
1944 }
mark_as_requiring_prelocking(TABLE_LIST ** tables_own_last)1945 void mark_as_requiring_prelocking(TABLE_LIST **tables_own_last)
1946 {
1947 query_tables_own_last= tables_own_last;
1948 }
1949 /* Return pointer to first not-own table in query-tables or 0 */
first_not_own_table()1950 TABLE_LIST* first_not_own_table()
1951 {
1952 return ( query_tables_own_last ? *query_tables_own_last : 0);
1953 }
chop_off_not_own_tables()1954 void chop_off_not_own_tables()
1955 {
1956 if (query_tables_own_last)
1957 {
1958 *query_tables_own_last= 0;
1959 query_tables_last= query_tables_own_last;
1960 query_tables_own_last= 0;
1961 }
1962 }
1963
1964 /** Return a pointer to the last element in query table list. */
last_table()1965 TABLE_LIST *last_table()
1966 {
1967 /* Don't use offsetof() macro in order to avoid warnings. */
1968 return query_tables ?
1969 (TABLE_LIST*) ((char*) query_tables_last -
1970 ((char*) &(query_tables->next_global) -
1971 (char*) query_tables)) :
1972 0;
1973 }
1974
1975 /**
1976 All types of unsafe statements.
1977
1978 @note The int values of the enum elements are used to point to
1979 bits in two bitmaps in two different places:
1980
1981 - Query_tables_list::binlog_stmt_flags
1982 - THD::binlog_unsafe_warning_flags
1983
1984 Hence in practice this is not an enum at all, but a map from
1985 symbols to bit indexes.
1986
1987 The ordering of elements in this enum must correspond to the order of
1988 elements in the array binlog_stmt_unsafe_errcode.
1989 */
1990 enum enum_binlog_stmt_unsafe {
1991 /**
1992 SELECT..LIMIT is unsafe because the set of rows returned cannot
1993 be predicted.
1994 */
1995 BINLOG_STMT_UNSAFE_LIMIT= 0,
1996 /**
1997 Access to log tables is unsafe because slave and master probably
1998 log different things.
1999 */
2000 BINLOG_STMT_UNSAFE_SYSTEM_TABLE,
2001 /**
2002 Inserting into an autoincrement column in a stored routine is unsafe.
2003 Even with just one autoincrement column, if the routine is invoked more than
2004 once slave is not guaranteed to execute the statement graph same way as
2005 the master.
2006 And since it's impossible to estimate how many times a routine can be invoked at
2007 the query pre-execution phase (see lock_tables), the statement is marked
2008 pessimistically unsafe.
2009 */
2010 BINLOG_STMT_UNSAFE_AUTOINC_COLUMNS,
2011 /**
2012 Using a UDF (user-defined function) is unsafe.
2013 */
2014 BINLOG_STMT_UNSAFE_UDF,
2015 /**
2016 Using most system variables is unsafe, because slave may run
2017 with different options than master.
2018 */
2019 BINLOG_STMT_UNSAFE_SYSTEM_VARIABLE,
2020 /**
2021 Using some functions is unsafe (e.g., UUID).
2022 */
2023 BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION,
2024
2025 /**
2026 Mixing transactional and non-transactional statements are unsafe if
2027 non-transactional reads or writes are occur after transactional
2028 reads or writes inside a transaction.
2029 */
2030 BINLOG_STMT_UNSAFE_NONTRANS_AFTER_TRANS,
2031
2032 /**
2033 Mixing self-logging and non-self-logging engines in a statement
2034 is unsafe.
2035 */
2036 BINLOG_STMT_UNSAFE_MULTIPLE_ENGINES_AND_SELF_LOGGING_ENGINE,
2037
2038 /**
2039 Statements that read from both transactional and non-transactional
2040 tables and write to any of them are unsafe.
2041 */
2042 BINLOG_STMT_UNSAFE_MIXED_STATEMENT,
2043
2044 /**
2045 INSERT...IGNORE SELECT is unsafe because which rows are ignored depends
2046 on the order that rows are retrieved by SELECT. This order cannot be
2047 predicted and may differ on master and the slave.
2048 */
2049 BINLOG_STMT_UNSAFE_INSERT_IGNORE_SELECT,
2050
2051 /**
2052 INSERT...SELECT...UPDATE is unsafe because which rows are updated depends
2053 on the order that rows are retrieved by SELECT. This order cannot be
2054 predicted and may differ on master and the slave.
2055 */
2056 BINLOG_STMT_UNSAFE_INSERT_SELECT_UPDATE,
2057
2058 /**
2059 Query that writes to a table with auto_inc column after selecting from
2060 other tables are unsafe as the order in which the rows are retrieved by
2061 select may differ on master and slave.
2062 */
2063 BINLOG_STMT_UNSAFE_WRITE_AUTOINC_SELECT,
2064
2065 /**
2066 INSERT...REPLACE SELECT is unsafe because which rows are replaced depends
2067 on the order that rows are retrieved by SELECT. This order cannot be
2068 predicted and may differ on master and the slave.
2069 */
2070 BINLOG_STMT_UNSAFE_REPLACE_SELECT,
2071
2072 /**
2073 CREATE TABLE... IGNORE... SELECT is unsafe because which rows are ignored
2074 depends on the order that rows are retrieved by SELECT. This order cannot
2075 be predicted and may differ on master and the slave.
2076 */
2077 BINLOG_STMT_UNSAFE_CREATE_IGNORE_SELECT,
2078
2079 /**
2080 CREATE TABLE...REPLACE... SELECT is unsafe because which rows are replaced
2081 depends on the order that rows are retrieved from SELECT. This order
2082 cannot be predicted and may differ on master and the slave
2083 */
2084 BINLOG_STMT_UNSAFE_CREATE_REPLACE_SELECT,
2085
2086 /**
2087 CREATE TABLE...SELECT on a table with auto-increment column is unsafe
2088 because which rows are replaced depends on the order that rows are
2089 retrieved from SELECT. This order cannot be predicted and may differ on
2090 master and the slave
2091 */
2092 BINLOG_STMT_UNSAFE_CREATE_SELECT_AUTOINC,
2093
2094 /**
2095 UPDATE...IGNORE is unsafe because which rows are ignored depends on the
2096 order that rows are updated. This order cannot be predicted and may differ
2097 on master and the slave.
2098 */
2099 BINLOG_STMT_UNSAFE_UPDATE_IGNORE,
2100
2101 /**
2102 INSERT... ON DUPLICATE KEY UPDATE on a table with more than one
2103 UNIQUE KEYS is unsafe.
2104 */
2105 BINLOG_STMT_UNSAFE_INSERT_TWO_KEYS,
2106
2107 /**
2108 INSERT into auto-inc field which is not the first part in composed
2109 primary key.
2110 */
2111 BINLOG_STMT_UNSAFE_AUTOINC_NOT_FIRST,
2112
2113 /**
2114 Using a plugin is unsafe.
2115 */
2116 BINLOG_STMT_UNSAFE_FULLTEXT_PLUGIN,
2117
2118 /**
2119 XA transactions and statements.
2120 */
2121 BINLOG_STMT_UNSAFE_XA,
2122
2123 /* The last element of this enumeration type. */
2124 BINLOG_STMT_UNSAFE_COUNT
2125 };
2126 /**
2127 This has all flags from 0 (inclusive) to BINLOG_STMT_FLAG_COUNT
2128 (exclusive) set.
2129 */
2130 static const int BINLOG_STMT_UNSAFE_ALL_FLAGS=
2131 ((1 << BINLOG_STMT_UNSAFE_COUNT) - 1);
2132
2133 /**
2134 Maps elements of enum_binlog_stmt_unsafe to error codes.
2135 */
2136 static const int binlog_stmt_unsafe_errcode[BINLOG_STMT_UNSAFE_COUNT];
2137
2138 /**
2139 Determine if this statement is marked as unsafe with
2140 specific type
2141
2142 @retval false if the statement is not marked as unsafe.
2143 @retval true if it is.
2144 */
is_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type)2145 inline bool is_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type) const {
2146 return ((binlog_stmt_flags & (1U << unsafe_type)) != 0);
2147 }
2148
2149 /**
2150 Determine if this statement is marked as unsafe.
2151
2152 @retval 0 if the statement is not marked as unsafe.
2153 @retval nonzero if the statement is marked as unsafe.
2154 */
is_stmt_unsafe()2155 inline bool is_stmt_unsafe() const {
2156 return get_stmt_unsafe_flags() != 0;
2157 }
2158
is_stmt_unsafe(enum_binlog_stmt_unsafe unsafe)2159 inline bool is_stmt_unsafe(enum_binlog_stmt_unsafe unsafe)
2160 {
2161 return binlog_stmt_flags & (1 << unsafe);
2162 }
2163
2164 /**
2165 Flag the current (top-level) statement as unsafe.
2166 The flag will be reset after the statement has finished.
2167
2168 @param unsafe_type The type of unsafety: one of the @c
2169 BINLOG_STMT_FLAG_UNSAFE_* flags in @c enum_binlog_stmt_flag.
2170 */
set_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type)2171 inline void set_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type) {
2172 DBUG_ENTER("set_stmt_unsafe");
2173 assert(unsafe_type >= 0 && unsafe_type < BINLOG_STMT_UNSAFE_COUNT);
2174 binlog_stmt_flags|= (1U << unsafe_type);
2175 DBUG_VOID_RETURN;
2176 }
2177
2178 /**
2179 Set the bits of binlog_stmt_flags determining the type of
2180 unsafeness of the current statement. No existing bits will be
2181 cleared, but new bits may be set.
2182
2183 @param flags A binary combination of zero or more bits, (1<<flag)
2184 where flag is a member of enum_binlog_stmt_unsafe.
2185 */
set_stmt_unsafe_flags(uint32 flags)2186 inline void set_stmt_unsafe_flags(uint32 flags) {
2187 DBUG_ENTER("set_stmt_unsafe_flags");
2188 assert((flags & ~BINLOG_STMT_UNSAFE_ALL_FLAGS) == 0);
2189 binlog_stmt_flags|= flags;
2190 DBUG_VOID_RETURN;
2191 }
2192
2193 /**
2194 Return a binary combination of all unsafe warnings for the
2195 statement. If the statement has been marked as unsafe by the
2196 'flag' member of enum_binlog_stmt_unsafe, then the return value
2197 from this function has bit (1<<flag) set to 1.
2198 */
get_stmt_unsafe_flags()2199 inline uint32 get_stmt_unsafe_flags() const {
2200 DBUG_ENTER("get_stmt_unsafe_flags");
2201 DBUG_RETURN(binlog_stmt_flags & BINLOG_STMT_UNSAFE_ALL_FLAGS);
2202 }
2203
2204 /**
2205 Mark the current statement as safe; i.e., clear all bits in
2206 binlog_stmt_flags that correspond to elements of
2207 enum_binlog_stmt_unsafe.
2208 */
clear_stmt_unsafe()2209 inline void clear_stmt_unsafe() {
2210 DBUG_ENTER("clear_stmt_unsafe");
2211 binlog_stmt_flags&= ~BINLOG_STMT_UNSAFE_ALL_FLAGS;
2212 DBUG_VOID_RETURN;
2213 }
2214
2215 /**
2216 Determine if this statement is a row injection.
2217
2218 @retval 0 if the statement is not a row injection
2219 @retval nonzero if the statement is a row injection
2220 */
is_stmt_row_injection()2221 inline bool is_stmt_row_injection() const {
2222 return binlog_stmt_flags &
2223 (1U << (BINLOG_STMT_UNSAFE_COUNT + BINLOG_STMT_TYPE_ROW_INJECTION));
2224 }
2225
2226 /**
2227 Flag the statement as a row injection. A row injection is either
2228 a BINLOG statement, or a row event in the relay log executed by
2229 the slave SQL thread.
2230 */
set_stmt_row_injection()2231 inline void set_stmt_row_injection() {
2232 DBUG_ENTER("set_stmt_row_injection");
2233 binlog_stmt_flags|=
2234 (1U << (BINLOG_STMT_UNSAFE_COUNT + BINLOG_STMT_TYPE_ROW_INJECTION));
2235 DBUG_VOID_RETURN;
2236 }
2237
2238 enum enum_stmt_accessed_table
2239 {
2240 /*
2241 If a transactional table is about to be read. Note that
2242 a write implies a read.
2243 */
2244 STMT_READS_TRANS_TABLE= 0,
2245 /*
2246 If a non-transactional table is about to be read. Note that
2247 a write implies a read.
2248 */
2249 STMT_READS_NON_TRANS_TABLE,
2250 /*
2251 If a temporary transactional table is about to be read. Note
2252 that a write implies a read.
2253 */
2254 STMT_READS_TEMP_TRANS_TABLE,
2255 /*
2256 If a temporary non-transactional table is about to be read. Note
2257 that a write implies a read.
2258 */
2259 STMT_READS_TEMP_NON_TRANS_TABLE,
2260 /*
2261 If a transactional table is about to be updated.
2262 */
2263 STMT_WRITES_TRANS_TABLE,
2264 /*
2265 If a non-transactional table is about to be updated.
2266 */
2267 STMT_WRITES_NON_TRANS_TABLE,
2268 /*
2269 If a temporary transactional table is about to be updated.
2270 */
2271 STMT_WRITES_TEMP_TRANS_TABLE,
2272 /*
2273 If a temporary non-transactional table is about to be updated.
2274 */
2275 STMT_WRITES_TEMP_NON_TRANS_TABLE,
2276 /*
2277 The last element of the enumeration. Please, if necessary add
2278 anything before this.
2279 */
2280 STMT_ACCESS_TABLE_COUNT
2281 };
2282
2283 #ifndef NDEBUG
stmt_accessed_table_string(enum_stmt_accessed_table accessed_table)2284 static inline const char *stmt_accessed_table_string(enum_stmt_accessed_table accessed_table)
2285 {
2286 switch (accessed_table)
2287 {
2288 case STMT_READS_TRANS_TABLE:
2289 return "STMT_READS_TRANS_TABLE";
2290 break;
2291 case STMT_READS_NON_TRANS_TABLE:
2292 return "STMT_READS_NON_TRANS_TABLE";
2293 break;
2294 case STMT_READS_TEMP_TRANS_TABLE:
2295 return "STMT_READS_TEMP_TRANS_TABLE";
2296 break;
2297 case STMT_READS_TEMP_NON_TRANS_TABLE:
2298 return "STMT_READS_TEMP_NON_TRANS_TABLE";
2299 break;
2300 case STMT_WRITES_TRANS_TABLE:
2301 return "STMT_WRITES_TRANS_TABLE";
2302 break;
2303 case STMT_WRITES_NON_TRANS_TABLE:
2304 return "STMT_WRITES_NON_TRANS_TABLE";
2305 break;
2306 case STMT_WRITES_TEMP_TRANS_TABLE:
2307 return "STMT_WRITES_TEMP_TRANS_TABLE";
2308 break;
2309 case STMT_WRITES_TEMP_NON_TRANS_TABLE:
2310 return "STMT_WRITES_TEMP_NON_TRANS_TABLE";
2311 break;
2312 case STMT_ACCESS_TABLE_COUNT:
2313 default:
2314 assert(0);
2315 break;
2316 }
2317 MY_ASSERT_UNREACHABLE();
2318 return "";
2319 }
2320 #endif /* DBUG */
2321
2322 #define BINLOG_DIRECT_ON 0xF0 /* unsafe when
2323 --binlog-direct-non-trans-updates
2324 is ON */
2325
2326 #define BINLOG_DIRECT_OFF 0xF /* unsafe when
2327 --binlog-direct-non-trans-updates
2328 is OFF */
2329
2330 #define TRX_CACHE_EMPTY 0x33 /* unsafe when trx-cache is empty */
2331
2332 #define TRX_CACHE_NOT_EMPTY 0xCC /* unsafe when trx-cache is not empty */
2333
2334 #define IL_LT_REPEATABLE 0xAA /* unsafe when < ISO_REPEATABLE_READ */
2335
2336 #define IL_GTE_REPEATABLE 0x55 /* unsafe when >= ISO_REPEATABLE_READ */
2337
2338 /**
2339 Sets the type of table that is about to be accessed while executing a
2340 statement.
2341
2342 @param accessed_table Enumeration type that defines the type of table,
2343 e.g. temporary, transactional, non-transactional.
2344 */
set_stmt_accessed_table(enum_stmt_accessed_table accessed_table)2345 inline void set_stmt_accessed_table(enum_stmt_accessed_table accessed_table)
2346 {
2347 DBUG_ENTER("LEX::set_stmt_accessed_table");
2348
2349 assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
2350 stmt_accessed_table_flag |= (1U << accessed_table);
2351
2352 DBUG_VOID_RETURN;
2353 }
2354
2355 /**
2356 Checks if a type of table is about to be accessed while executing a
2357 statement.
2358
2359 @param accessed_table Enumeration type that defines the type of table,
2360 e.g. temporary, transactional, non-transactional.
2361
2362 @return
2363 @retval TRUE if the type of the table is about to be accessed
2364 @retval FALSE otherwise
2365 */
stmt_accessed_table(enum_stmt_accessed_table accessed_table)2366 inline bool stmt_accessed_table(enum_stmt_accessed_table accessed_table)
2367 {
2368 DBUG_ENTER("LEX::stmt_accessed_table");
2369
2370 assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
2371
2372 DBUG_RETURN((stmt_accessed_table_flag & (1U << accessed_table)) != 0);
2373 }
2374
2375 /*
2376 Checks if a mixed statement is unsafe.
2377
2378
2379 @param in_multi_stmt_transaction_mode defines if there is an on-going
2380 multi-transactional statement.
2381 @param binlog_direct defines if --binlog-direct-non-trans-updates is
2382 active.
2383 @param trx_cache_is_not_empty defines if the trx-cache is empty or not.
2384 @param trx_isolation defines the isolation level.
2385
2386 @return
2387 @retval TRUE if the mixed statement is unsafe
2388 @retval FALSE otherwise
2389 */
is_mixed_stmt_unsafe(bool in_multi_stmt_transaction_mode,bool binlog_direct,bool trx_cache_is_not_empty,uint tx_isolation)2390 inline bool is_mixed_stmt_unsafe(bool in_multi_stmt_transaction_mode,
2391 bool binlog_direct,
2392 bool trx_cache_is_not_empty,
2393 uint tx_isolation)
2394 {
2395 bool unsafe= FALSE;
2396
2397 if (in_multi_stmt_transaction_mode)
2398 {
2399 uint condition=
2400 (binlog_direct ? BINLOG_DIRECT_ON : BINLOG_DIRECT_OFF) &
2401 (trx_cache_is_not_empty ? TRX_CACHE_NOT_EMPTY : TRX_CACHE_EMPTY) &
2402 (tx_isolation >= ISO_REPEATABLE_READ ? IL_GTE_REPEATABLE : IL_LT_REPEATABLE);
2403
2404 unsafe= (binlog_unsafe_map[stmt_accessed_table_flag] & condition);
2405
2406 #if !defined(NDEBUG)
2407 DBUG_PRINT("LEX::is_mixed_stmt_unsafe", ("RESULT %02X %02X %02X\n", condition,
2408 binlog_unsafe_map[stmt_accessed_table_flag],
2409 (binlog_unsafe_map[stmt_accessed_table_flag] & condition)));
2410
2411 int type_in= 0;
2412 for (; type_in < STMT_ACCESS_TABLE_COUNT; type_in++)
2413 {
2414 if (stmt_accessed_table((enum_stmt_accessed_table) type_in))
2415 DBUG_PRINT("LEX::is_mixed_stmt_unsafe", ("ACCESSED %s ",
2416 stmt_accessed_table_string((enum_stmt_accessed_table) type_in)));
2417 }
2418 #endif
2419 }
2420
2421 if (stmt_accessed_table(STMT_WRITES_NON_TRANS_TABLE) &&
2422 stmt_accessed_table(STMT_READS_TRANS_TABLE) &&
2423 tx_isolation < ISO_REPEATABLE_READ)
2424 unsafe= TRUE;
2425 else if (stmt_accessed_table(STMT_WRITES_TEMP_NON_TRANS_TABLE) &&
2426 stmt_accessed_table(STMT_READS_TRANS_TABLE) &&
2427 tx_isolation < ISO_REPEATABLE_READ)
2428 unsafe= TRUE;
2429
2430 return(unsafe);
2431 }
2432
2433 /**
2434 true if the parsed tree contains references to stored procedures
2435 or functions, false otherwise
2436 */
uses_stored_routines()2437 bool uses_stored_routines() const
2438 { return sroutines_list.elements != 0; }
2439
set_using_match()2440 void set_using_match() { using_match= TRUE; }
get_using_match()2441 bool get_using_match() { return using_match; }
2442 private:
2443
2444 /**
2445 Enumeration listing special types of statements.
2446
2447 Currently, the only possible type is ROW_INJECTION.
2448 */
2449 enum enum_binlog_stmt_type {
2450 /**
2451 The statement is a row injection (i.e., either a BINLOG
2452 statement or a row event executed by the slave SQL thread).
2453 */
2454 BINLOG_STMT_TYPE_ROW_INJECTION = 0,
2455
2456 /** The last element of this enumeration type. */
2457 BINLOG_STMT_TYPE_COUNT
2458 };
2459
2460 /**
2461 Bit field indicating the type of statement.
2462
2463 There are two groups of bits:
2464
2465 - The low BINLOG_STMT_UNSAFE_COUNT bits indicate the types of
2466 unsafeness that the current statement has.
2467
2468 - The next BINLOG_STMT_TYPE_COUNT bits indicate if the statement
2469 is of some special type.
2470
2471 This must be a member of LEX, not of THD: each stored procedure
2472 needs to remember its unsafeness state between calls and each
2473 stored procedure has its own LEX object (but no own THD object).
2474 */
2475 uint32 binlog_stmt_flags;
2476
2477 /**
2478 Bit field that determines the type of tables that are about to be
2479 be accessed while executing a statement.
2480 */
2481 uint32 stmt_accessed_table_flag;
2482
2483 /**
2484 It will be set TRUE if 'MATCH () AGAINST' is used in the statement.
2485 */
2486 bool using_match;
2487 };
2488
2489
2490 /*
2491 st_parsing_options contains the flags for constructions that are
2492 allowed in the current statement.
2493 */
2494
2495 struct st_parsing_options
2496 {
2497 bool allows_variable;
2498 bool allows_select_into;
2499 bool allows_select_procedure;
2500
st_parsing_optionsst_parsing_options2501 st_parsing_options() { reset(); }
2502 void reset();
2503 };
2504
2505
2506 /**
2507 The state of the lexical parser, when parsing comments.
2508 */
2509 enum enum_comment_state
2510 {
2511 /**
2512 Not parsing comments.
2513 */
2514 NO_COMMENT,
2515
2516 /**
2517 Parsing comments that need to be preserved.
2518 (Copy '/' '*' and '*' '/' sequences to the preprocessed buffer.)
2519 Typically, these are user comments '/' '*' ... '*' '/'.
2520 */
2521 PRESERVE_COMMENT,
2522
2523 /**
2524 Parsing comments that need to be discarded.
2525 (Don't copy '/' '*' '!' and '*' '/' sequences to the preprocessed buffer.)
2526 Typically, these are special comments '/' '*' '!' ... '*' '/',
2527 or '/' '*' '!' 'M' 'M' 'm' 'm' 'm' ... '*' '/', where the comment
2528 markers should not be expanded.
2529 */
2530 DISCARD_COMMENT
2531 };
2532
2533
2534 /**
2535 This class represents the character input stream consumed during lexical
2536 analysis.
2537
2538 In addition to consuming the input stream, this class performs some comment
2539 pre processing, by filtering out out-of-bound special text from the query
2540 input stream.
2541
2542 Two buffers, with pointers inside each, are maintained in parallel. The
2543 'raw' buffer is the original query text, which may contain out-of-bound
2544 comments. The 'cpp' (for comments pre processor) is the pre-processed buffer
2545 that contains only the query text that should be seen once out-of-bound data
2546 is removed.
2547 */
2548
2549 class Lex_input_stream
2550 {
2551 public:
2552
2553 /**
2554 Object initializer. Must be called before usage.
2555
2556 @retval FALSE OK
2557 @retval TRUE Error
2558 */
2559 bool init(THD *thd, const char *buff, size_t length);
2560
2561 void reset(const char *buff, size_t length);
2562
2563 /**
2564 Set the echo mode.
2565
2566 When echo is true, characters parsed from the raw input stream are
2567 preserved. When false, characters parsed are silently ignored.
2568 @param echo the echo mode.
2569 */
set_echo(bool echo)2570 void set_echo(bool echo)
2571 {
2572 m_echo= echo;
2573 }
2574
save_in_comment_state()2575 void save_in_comment_state()
2576 {
2577 m_echo_saved= m_echo;
2578 in_comment_saved= in_comment;
2579 }
2580
restore_in_comment_state()2581 void restore_in_comment_state()
2582 {
2583 m_echo= m_echo_saved;
2584 in_comment= in_comment_saved;
2585 }
2586
2587 /**
2588 Skip binary from the input stream.
2589 @param n number of bytes to accept.
2590 */
skip_binary(int n)2591 void skip_binary(int n)
2592 {
2593 assert(m_ptr + n <= m_end_of_query);
2594 if (m_echo)
2595 {
2596 memcpy(m_cpp_ptr, m_ptr, n);
2597 m_cpp_ptr += n;
2598 }
2599 m_ptr += n;
2600 }
2601
2602 /**
2603 Get a character, and advance in the stream.
2604 @return the next character to parse.
2605 */
yyGet()2606 unsigned char yyGet()
2607 {
2608 assert(m_ptr <= m_end_of_query);
2609 char c= *m_ptr++;
2610 if (m_echo)
2611 *m_cpp_ptr++ = c;
2612 return c;
2613 }
2614
2615 /**
2616 Get the last character accepted.
2617 @return the last character accepted.
2618 */
yyGetLast()2619 unsigned char yyGetLast()
2620 {
2621 return m_ptr[-1];
2622 }
2623
2624 /**
2625 Look at the next character to parse, but do not accept it.
2626 */
yyPeek()2627 unsigned char yyPeek()
2628 {
2629 assert(m_ptr <= m_end_of_query);
2630 return m_ptr[0];
2631 }
2632
2633 /**
2634 Look ahead at some character to parse.
2635 @param n offset of the character to look up
2636 */
yyPeekn(int n)2637 unsigned char yyPeekn(int n)
2638 {
2639 assert(m_ptr + n <= m_end_of_query);
2640 return m_ptr[n];
2641 }
2642
2643 /**
2644 Cancel the effect of the last yyGet() or yySkip().
2645 Note that the echo mode should not change between calls to yyGet / yySkip
2646 and yyUnget. The caller is responsible for ensuring that.
2647 */
yyUnget()2648 void yyUnget()
2649 {
2650 m_ptr--;
2651 if (m_echo)
2652 m_cpp_ptr--;
2653 }
2654
2655 /**
2656 Accept a character, by advancing the input stream.
2657 */
yySkip()2658 void yySkip()
2659 {
2660 assert(m_ptr <= m_end_of_query);
2661 if (m_echo)
2662 *m_cpp_ptr++ = *m_ptr++;
2663 else
2664 m_ptr++;
2665 }
2666
2667 /**
2668 Accept multiple characters at once.
2669 @param n the number of characters to accept.
2670 */
yySkipn(int n)2671 void yySkipn(int n)
2672 {
2673 assert(m_ptr + n <= m_end_of_query);
2674 if (m_echo)
2675 {
2676 memcpy(m_cpp_ptr, m_ptr, n);
2677 m_cpp_ptr += n;
2678 }
2679 m_ptr += n;
2680 }
2681
2682 /**
2683 Puts a character back into the stream, canceling
2684 the effect of the last yyGet() or yySkip().
2685 Note that the echo mode should not change between calls
2686 to unput, get, or skip from the stream.
2687 */
yyUnput(char ch)2688 char *yyUnput(char ch)
2689 {
2690 *--m_ptr= ch;
2691 if (m_echo)
2692 m_cpp_ptr--;
2693 return m_ptr;
2694 }
2695
2696 /**
2697 Inject a character into the pre-processed stream.
2698
2699 Note, this function is used to inject a space instead of multi-character
2700 C-comment. Thus there is no boundary checks here (basically, we replace
2701 N-chars by 1-char here).
2702 */
cpp_inject(char ch)2703 char *cpp_inject(char ch)
2704 {
2705 *m_cpp_ptr= ch;
2706 return ++m_cpp_ptr;
2707 }
2708
2709 /**
2710 End of file indicator for the query text to parse.
2711 @return true if there are no more characters to parse
2712 */
eof()2713 bool eof()
2714 {
2715 return (m_ptr >= m_end_of_query);
2716 }
2717
2718 /**
2719 End of file indicator for the query text to parse.
2720 @param n number of characters expected
2721 @return true if there are less than n characters to parse
2722 */
eof(int n)2723 bool eof(int n)
2724 {
2725 return ((m_ptr + n) >= m_end_of_query);
2726 }
2727
2728 /** Get the raw query buffer. */
get_buf()2729 const char *get_buf()
2730 {
2731 return m_buf;
2732 }
2733
2734 /** Get the pre-processed query buffer. */
get_cpp_buf()2735 const char *get_cpp_buf()
2736 {
2737 return m_cpp_buf;
2738 }
2739
2740 /** Get the end of the raw query buffer. */
get_end_of_query()2741 const char *get_end_of_query()
2742 {
2743 return m_end_of_query;
2744 }
2745
2746 /** Mark the stream position as the start of a new token. */
start_token()2747 void start_token()
2748 {
2749 m_tok_start= m_ptr;
2750 m_tok_end= m_ptr;
2751
2752 m_cpp_tok_start= m_cpp_ptr;
2753 m_cpp_tok_end= m_cpp_ptr;
2754 }
2755
2756 /**
2757 Adjust the starting position of the current token.
2758 This is used to compensate for starting whitespace.
2759 */
restart_token()2760 void restart_token()
2761 {
2762 m_tok_start= m_ptr;
2763 m_cpp_tok_start= m_cpp_ptr;
2764 }
2765
2766 /** Get the token start position, in the raw buffer. */
get_tok_start()2767 const char *get_tok_start()
2768 {
2769 return m_tok_start;
2770 }
2771
2772 /** Get the token start position, in the pre-processed buffer. */
get_cpp_tok_start()2773 const char *get_cpp_tok_start()
2774 {
2775 return m_cpp_tok_start;
2776 }
2777
2778 /** Get the token end position, in the raw buffer. */
get_tok_end()2779 const char *get_tok_end()
2780 {
2781 return m_tok_end;
2782 }
2783
2784 /** Get the token end position, in the pre-processed buffer. */
get_cpp_tok_end()2785 const char *get_cpp_tok_end()
2786 {
2787 return m_cpp_tok_end;
2788 }
2789
2790 /** Get the current stream pointer, in the raw buffer. */
get_ptr()2791 const char *get_ptr()
2792 {
2793 return m_ptr;
2794 }
2795
2796 /** Get the current stream pointer, in the pre-processed buffer. */
get_cpp_ptr()2797 const char *get_cpp_ptr()
2798 {
2799 return m_cpp_ptr;
2800 }
2801
2802 /** Get the length of the current token, in the raw buffer. */
yyLength()2803 uint yyLength()
2804 {
2805 /*
2806 The assumption is that the lexical analyser is always 1 character ahead,
2807 which the -1 account for.
2808 */
2809 assert(m_ptr > m_tok_start);
2810 return (uint) ((m_ptr - m_tok_start) - 1);
2811 }
2812
2813 /** Get the utf8-body string. */
get_body_utf8_str()2814 const char *get_body_utf8_str()
2815 {
2816 return m_body_utf8;
2817 }
2818
2819 /** Get the utf8-body length. */
get_body_utf8_length()2820 uint get_body_utf8_length()
2821 {
2822 return (uint) (m_body_utf8_ptr - m_body_utf8);
2823 }
2824
2825 void body_utf8_start(THD *thd, const char *begin_ptr);
2826 void body_utf8_append(const char *ptr);
2827 void body_utf8_append(const char *ptr, const char *end_ptr);
2828 void body_utf8_append_literal(THD *thd,
2829 const LEX_STRING *txt,
2830 const CHARSET_INFO *txt_cs,
2831 const char *end_ptr);
2832
2833 uint get_lineno(const char *raw_ptr);
2834
2835 /** Current thread. */
2836 THD *m_thd;
2837
2838 /** Current line number. */
2839 uint yylineno;
2840
2841 /** Length of the last token parsed. */
2842 uint yytoklen;
2843
2844 /** Interface with bison, value of the last token parsed. */
2845 LEX_YYSTYPE yylval;
2846
2847 /**
2848 LALR(2) resolution, look ahead token.
2849 Value of the next token to return, if any,
2850 or -1, if no token was parsed in advance.
2851 Note: 0 is a legal token, and represents YYEOF.
2852 */
2853 int lookahead_token;
2854
2855 /** LALR(2) resolution, value of the look ahead token.*/
2856 LEX_YYSTYPE lookahead_yylval;
2857
2858 /// Skip adding of the current token's digest since it is already added
2859 ///
2860 /// Usually we calculate a digest token by token at the top-level function
2861 /// of the lexer: MYSQLlex(). However, some complex ("hintable") tokens break
2862 /// that data flow: for example, the `SELECT /*+ HINT(t) */` is the single
2863 /// token from the main parser's point of view, and we add the "SELECT"
2864 /// keyword to the digest buffer right after the lex_one_token() call,
2865 /// but the "/*+ HINT(t) */" is a sequence of separate tokens from the hint
2866 /// parser's point of view, and we add those tokens to the digest buffer
2867 /// *inside* the lex_one_token() call. Thus, the usual data flow adds
2868 /// tokens from the "/*+ HINT(t) */" string first, and only than it appends
2869 /// the "SELECT" keyword token to that stream: "/*+ HINT(t) */ SELECT".
2870 /// This is not acceptable, since we use the digest buffer to restore
2871 /// query strings in their normalized forms, so the order of added tokens is
2872 /// important. Thus, we add tokens of "hintable" keywords to a digest buffer
2873 /// right in the hint parser and skip adding of them at the caller with the
2874 /// help of skip_digest flag.
2875 bool skip_digest;
2876
2877 void add_digest_token(uint token, LEX_YYSTYPE yylval);
2878
2879 void reduce_digest_token(uint token_left, uint token_right);
2880
2881 const CHARSET_INFO *query_charset;
2882
2883 private:
2884 /** Pointer to the current position in the raw input stream. */
2885 char *m_ptr;
2886
2887 /** Starting position of the last token parsed, in the raw buffer. */
2888 const char *m_tok_start;
2889
2890 /** Ending position of the previous token parsed, in the raw buffer. */
2891 const char *m_tok_end;
2892
2893 /** End of the query text in the input stream, in the raw buffer. */
2894 const char *m_end_of_query;
2895
2896 /** Begining of the query text in the input stream, in the raw buffer. */
2897 const char *m_buf;
2898
2899 /** Length of the raw buffer. */
2900 size_t m_buf_length;
2901
2902 /** Echo the parsed stream to the pre-processed buffer. */
2903 bool m_echo;
2904 bool m_echo_saved;
2905
2906 /** Pre-processed buffer. */
2907 char *m_cpp_buf;
2908
2909 /** Pointer to the current position in the pre-processed input stream. */
2910 char *m_cpp_ptr;
2911
2912 /**
2913 Starting position of the last token parsed,
2914 in the pre-processed buffer.
2915 */
2916 const char *m_cpp_tok_start;
2917
2918 /**
2919 Ending position of the previous token parsed,
2920 in the pre-processed buffer.
2921 */
2922 const char *m_cpp_tok_end;
2923
2924 /** UTF8-body buffer created during parsing. */
2925 char *m_body_utf8;
2926
2927 /** Pointer to the current position in the UTF8-body buffer. */
2928 char *m_body_utf8_ptr;
2929
2930 /**
2931 Position in the pre-processed buffer. The query from m_cpp_buf to
2932 m_cpp_utf_processed_ptr is converted to UTF8-body.
2933 */
2934 const char *m_cpp_utf8_processed_ptr;
2935
2936 public:
2937
2938 /** Current state of the lexical analyser. */
2939 enum my_lex_states next_state;
2940
2941 /**
2942 Position of ';' in the stream, to delimit multiple queries.
2943 This delimiter is in the raw buffer.
2944 */
2945 const char *found_semicolon;
2946
2947 /** Token character bitmaps, to detect 7bit strings. */
2948 uchar tok_bitmap;
2949
2950 /** SQL_MODE = IGNORE_SPACE. */
2951 bool ignore_space;
2952
2953 /**
2954 TRUE if we're parsing a prepared statement: in this mode
2955 we should allow placeholders.
2956 */
2957 bool stmt_prepare_mode;
2958 /**
2959 TRUE if we should allow multi-statements.
2960 */
2961 bool multi_statements;
2962
2963 /** State of the lexical analyser for comments. */
2964 enum_comment_state in_comment;
2965 enum_comment_state in_comment_saved;
2966
2967 /**
2968 Starting position of the TEXT_STRING or IDENT in the pre-processed
2969 buffer.
2970
2971 NOTE: this member must be used within MYSQLlex() function only.
2972 */
2973 const char *m_cpp_text_start;
2974
2975 /**
2976 Ending position of the TEXT_STRING or IDENT in the pre-processed
2977 buffer.
2978
2979 NOTE: this member must be used within MYSQLlex() function only.
2980 */
2981 const char *m_cpp_text_end;
2982
2983 /**
2984 Character set specified by the character-set-introducer.
2985
2986 NOTE: this member must be used within MYSQLlex() function only.
2987 */
2988 CHARSET_INFO *m_underscore_cs;
2989
2990 /**
2991 Current statement digest instrumentation.
2992 */
2993 sql_digest_state* m_digest;
2994
text_string_is_7bit()2995 bool text_string_is_7bit() const { return !(tok_bitmap & 0x80); }
2996 };
2997
2998
2999 class LEX_COLUMN : public Sql_alloc
3000 {
3001 public:
3002 String column;
3003 uint rights;
LEX_COLUMN(const String & x,const uint & y)3004 LEX_COLUMN (const String& x,const uint& y ): column (x),rights (y) {}
3005 };
3006
3007
3008 /* The state of the lex parsing. This is saved in the THD struct */
3009
3010 struct LEX: public Query_tables_list
3011 {
3012 friend bool lex_start(THD *thd);
3013
3014 SELECT_LEX_UNIT *unit; ///< Outer-most query expression
3015 /// @todo: select_lex can be replaced with unit->first-select()
3016 SELECT_LEX *select_lex; ///< First query block
3017 SELECT_LEX *all_selects_list; ///< List of all query blocks
3018 private:
3019 /* current SELECT_LEX in parsing */
3020 SELECT_LEX *m_current_select;
3021
3022 public:
current_selectLEX3023 inline SELECT_LEX *current_select() { return m_current_select; }
set_current_selectLEX3024 inline void set_current_select(SELECT_LEX *select)
3025 {
3026 // (2) Only owning thread could change m_current_select
3027 // (1) bypass for bootstrap and "new THD"
3028 assert(!current_thd || !thd || //(1)
3029 thd == current_thd); //(2)
3030 m_current_select= select;
3031 }
3032 /// @return true if this is an EXPLAIN statement
is_explainLEX3033 bool is_explain() const { return (describe & DESCRIBE_NORMAL); }
3034 char *length,*dec,*change;
3035 LEX_STRING name;
3036 char *help_arg;
3037 char* to_log; /* For PURGE MASTER LOGS TO */
3038 char* x509_subject,*x509_issuer,*ssl_cipher;
3039 String *wild;
3040 sql_exchange *exchange;
3041 Query_result *result;
3042 Item *default_value, *on_update_value;
3043 LEX_STRING comment, ident;
3044 LEX_USER *grant_user;
3045 LEX_ALTER alter_password;
3046 THD *thd;
3047 Generated_column *gcol_info;
3048
3049 /* Optimizer hints */
3050 Opt_hints_global *opt_hints_global;
3051
3052 /* maintain a list of used plugins for this LEX */
3053 typedef Prealloced_array<plugin_ref,
3054 INITIAL_LEX_PLUGIN_LIST_SIZE, true> Plugins_array;
3055 Plugins_array plugins;
3056
3057 const CHARSET_INFO *charset;
3058
3059 /// Table being inserted into (may be a view)
3060 TABLE_LIST *insert_table;
3061 /// Leaf table being inserted into (always a base table)
3062 TABLE_LIST *insert_table_leaf;
3063
3064 /** SELECT of CREATE VIEW statement */
3065 LEX_STRING create_view_select;
3066
3067 /** Start of 'ON table', in trigger statements. */
3068 const char* raw_trg_on_table_name_begin;
3069 /** End of 'ON table', in trigger statements. */
3070 const char* raw_trg_on_table_name_end;
3071
3072 /** Start of clause FOLLOWS/PRECEDES. */
3073 const char* trg_ordering_clause_begin;
3074 /** End (a char after the end) of clause FOLLOWS/PRECEDES. */
3075 const char* trg_ordering_clause_end;
3076
3077 /* Partition info structure filled in by PARTITION BY parse part */
3078 partition_info *part_info;
3079
3080 /*
3081 The definer of the object being created (view, trigger, stored routine).
3082 I.e. the value of DEFINER clause.
3083 */
3084 LEX_USER *definer;
3085
3086 List<Key_part_spec> col_list;
3087 List<Key_part_spec> ref_list;
3088 List<String> interval_list;
3089 List<LEX_USER> users_list;
3090 List<LEX_COLUMN> columns;
3091
3092 ulonglong bulk_insert_row_cnt;
3093
3094 // LOAD statement-specific fields:
3095
3096 List<Item> load_field_list;
3097 List<Item> load_update_list;
3098 List<Item> load_value_list;
3099 /*
3100 A list of strings is maintained to store the SET clause command user strings
3101 which are specified in load data operation. This list will be used
3102 during the reconstruction of "load data" statement at the time of writing
3103 to binary log.
3104 */
3105 List<String> load_set_str_list;
3106
3107 // PURGE statement-specific fields:
3108 List<Item> purge_value_list;
3109
3110 // KILL statement-specific fields:
3111 List<Item> kill_value_list;
3112
3113 // CALL statement-specific fields:
3114 List<Item> call_value_list;
3115
3116 // HANDLER statement-specific fields:
3117 List<Item> *handler_insert_list;
3118
3119 // other stuff:
3120 List<set_var_base> var_list;
3121 List<Item_func_set_user_var> set_var_list; // in-query assignment list
3122 List<Item_param> param_list;
3123 List<LEX_STRING> view_list; // view list (list of field names in view)
3124
insert_values_mapLEX3125 void insert_values_map(Field *f1, Field *f2)
3126 {
3127 if (!insert_update_values_map)
3128 insert_update_values_map= new std::map<Field*, Field*>;
3129 insert_update_values_map->insert(std::make_pair(f1, f2));
3130 }
clear_values_mapLEX3131 void clear_values_map()
3132 {
3133 if (insert_update_values_map)
3134 {
3135 insert_update_values_map->clear();
3136 delete insert_update_values_map;
3137 insert_update_values_map= NULL;
3138 }
3139 }
has_values_mapLEX3140 bool has_values_map() const
3141 {
3142 return insert_update_values_map != NULL;
3143 }
begin_values_mapLEX3144 std::map<Field *, Field *>::iterator begin_values_map()
3145 {
3146 return insert_update_values_map->begin();
3147 }
end_values_mapLEX3148 std::map<Field *, Field *>::iterator end_values_map()
3149 {
3150 return insert_update_values_map->end();
3151 }
3152
3153 private:
3154 /*
3155 With Visual Studio, an std::map will always allocate two small objects
3156 on the heap. Sometimes we put LEX objects in a MEM_ROOT, and never run
3157 the LEX DTOR. To avoid memory leaks, put this std::map on the heap,
3158 and call clear_values_map() at the end of each statement
3159 */
3160 std::map<Field *,Field *> *insert_update_values_map;
3161 public:
3162
3163 /*
3164 A stack of name resolution contexts for the query. This stack is used
3165 at parse time to set local name resolution contexts for various parts
3166 of a query. For example, in a JOIN ... ON (some_condition) clause the
3167 Items in 'some_condition' must be resolved only against the operands
3168 of the the join, and not against the whole clause. Similarly, Items in
3169 subqueries should be resolved against the subqueries (and outer queries).
3170 The stack is used in the following way: when the parser detects that
3171 all Items in some clause need a local context, it creates a new context
3172 and pushes it on the stack. All newly created Items always store the
3173 top-most context in the stack. Once the parser leaves the clause that
3174 required a local context, the parser pops the top-most context.
3175 */
3176 List<Name_resolution_context> context_stack;
3177 /* true if SET STATEMENT ... FOR ... statement is use, false otherwise */
3178 bool set_statement;
3179 /**
3180 Argument values for PROCEDURE ANALYSE(); is NULL for other queries
3181 */
3182 Proc_analyse_params *proc_analyse;
3183 SQL_I_List<TABLE_LIST> auxiliary_table_list, save_list;
3184 Create_field *last_field;
3185 Item_sum *in_sum_func;
3186 udf_func udf;
3187 HA_CHECK_OPT check_opt; // check/repair options
3188 HA_CREATE_INFO create_info;
3189 KEY_CREATE_INFO key_create_info;
3190 LEX_MASTER_INFO mi; // used by CHANGE MASTER
3191 LEX_SLAVE_CONNECTION slave_connection;
3192 Server_options server_options;
3193 USER_RESOURCES mqh;
3194 LEX_RESET_SLAVE reset_slave_info;
3195 ulong type;
3196 /*
3197 This variable is used in post-parse stage to declare that sum-functions,
3198 or functions which have sense only if GROUP BY is present, are allowed.
3199 For example in a query
3200 SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
3201 MIN(i) in the WHERE clause is not allowed in the opposite to MIN(i)
3202 in the HAVING clause. Due to possible nesting of select construct
3203 the variable can contain 0 or 1 for each nest level.
3204 */
3205 nesting_map allow_sum_func;
3206
3207 Sql_cmd *m_sql_cmd;
3208
3209 /*
3210 Usually `expr` rule of yacc is quite reused but some commands better
3211 not support subqueries which comes standard with this rule, like
3212 KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
3213 syntax error back.
3214 */
3215 bool expr_allows_subselect;
3216 /*
3217 A special command "PARSE_VCOL_EXPR" is defined for the parser
3218 to translate an expression statement of a generated column
3219 (stored in the *.frm file as a string) into an Item object.
3220 The following flag is used to prevent other applications to use
3221 this command.
3222 */
3223 bool parse_gcol_expr;
3224
3225 enum SSL_type ssl_type; /* defined in violite.h */
3226 enum enum_duplicates duplicates;
3227 enum enum_tx_isolation tx_isolation;
3228 enum enum_var_type option_type;
3229 enum enum_view_create_mode create_view_mode;
3230 enum enum_drop_mode drop_mode;
3231
3232 /// QUERY ID for SHOW PROFILE and EXPLAIN CONNECTION
3233 my_thread_id query_id;
3234 uint profile_options;
3235 uint uint_geom_type;
3236 uint grant, grant_tot_col, which_columns;
3237 enum fk_match_opt fk_match_option;
3238 enum fk_option fk_update_opt;
3239 enum fk_option fk_delete_opt;
3240 uint slave_thd_opt, start_transaction_opt;
3241 int select_number; ///< Number of query block (by EXPLAIN)
3242 uint8 describe;
3243 /*
3244 A flag that indicates what kinds of derived tables are present in the
3245 query (0 if no derived tables, otherwise a combination of flags
3246 DERIVED_SUBQUERY and DERIVED_VIEW).
3247 */
3248 uint8 derived_tables;
3249 uint8 create_view_algorithm;
3250 uint8 create_view_check;
3251 uint8 context_analysis_only;
3252 bool drop_if_exists, drop_temporary, local_file;
3253 bool autocommit;
3254 bool verbose, no_write_to_binlog;
3255
3256 enum enum_yes_no_unknown tx_chain, tx_release;
3257 bool safe_to_cache_query;
3258 bool subqueries;
3259 private:
3260 bool ignore;
3261 public:
is_ignoreLEX3262 bool is_ignore() const { return ignore; }
set_ignoreLEX3263 void set_ignore(bool ignore_param) { ignore= ignore_param; }
3264 st_parsing_options parsing_options;
3265 Alter_info alter_info;
3266 /*
3267 For CREATE TABLE statement last element of table list which is not
3268 part of SELECT or LIKE part (i.e. either element for table we are
3269 creating or last of tables referenced by foreign keys).
3270 */
3271 TABLE_LIST *create_last_non_select_table;
3272 /* Prepared statements SQL syntax:*/
3273 LEX_CSTRING prepared_stmt_name; /* Statement name (in all queries) */
3274 /*
3275 Prepared statement query text or name of variable that holds the
3276 prepared statement (in PREPARE ... queries)
3277 */
3278 LEX_STRING prepared_stmt_code;
3279 /* If true, prepared_stmt_code is a name of variable that holds the query */
3280 bool prepared_stmt_code_is_varref;
3281 /* Names of user variables holding parameters (in EXECUTE) */
3282 List<LEX_STRING> prepared_stmt_params;
3283 sp_head *sphead;
3284 sp_name *spname;
3285 bool sp_lex_in_use; /* Keep track on lex usage in SPs for error handling */
3286 bool all_privileges;
3287 bool proxy_priv;
3288 /*
3289 Temporary variable to distinguish SET PASSWORD command from others
3290 SQLCOM_SET_OPTION commands. Should be removed when WL#6409 is
3291 introduced.
3292 */
3293 bool is_set_password_sql;
3294 bool contains_plaintext_password;
3295 enum_keep_diagnostics keep_diagnostics;
3296
3297 /*
3298 Compression dictionary name (in column definition)
3299 CREATE TABLE t1(
3300 ...
3301 <column_name> BLOB COLUMN_FORMAT COMPRESSED
3302 WITH COMPRESSION_DICTIONARY <dict>
3303 ...
3304 );
3305 */
3306 LEX_CSTRING zip_dict_name;
3307
3308 private:
3309 bool m_broken; ///< see mark_broken()
3310 /// Current SP parsing context.
3311 /// @see also sp_head::m_root_parsing_ctx.
3312 sp_pcontext *sp_current_parsing_ctx;
3313
3314 public:
3315
is_brokenLEX3316 bool is_broken() const { return m_broken; }
3317 /**
3318 Certain permanent transformations (like in2exists), if they fail, may
3319 leave the LEX in an inconsistent state. They should call the
3320 following function, so that this LEX is not reused by another execution.
3321
3322 @todo If lex_start () were a member function of LEX, the "broken"
3323 argument could always be "true" and thus could be removed.
3324 */
3325 void mark_broken(bool broken= true)
3326 {
3327 if (broken)
3328 {
3329 /*
3330 "OPEN <cursor>" cannot be re-prepared if the cursor uses no tables
3331 ("SELECT FROM DUAL"). Indeed in that case cursor_query is left empty
3332 in constructions of sp_instr_cpush, and thus
3333 sp_lex_instr::parse_expr() cannot re-prepare. So we mark the statement
3334 as broken only if tables are used.
3335 */
3336 if (is_metadata_used())
3337 m_broken= true;
3338 }
3339 else
3340 m_broken= false;
3341 }
3342
get_sp_current_parsing_ctxLEX3343 sp_pcontext *get_sp_current_parsing_ctx()
3344 { return sp_current_parsing_ctx; }
3345
set_sp_current_parsing_ctxLEX3346 void set_sp_current_parsing_ctx(sp_pcontext *ctx)
3347 { sp_current_parsing_ctx= ctx; }
3348
3349 /// Check if the current statement uses meta-data (uses a table or a stored
3350 /// routine).
is_metadata_usedLEX3351 bool is_metadata_used() const
3352 { return query_tables != NULL || sroutines.records > 0; }
3353
3354 public:
3355 st_sp_chistics sp_chistics;
3356
3357 Event_parse_data *event_parse_data;
3358
3359 bool only_view; /* used for SHOW CREATE TABLE/VIEW */
3360 /*
3361 view created to be run from definer (standard behaviour)
3362 */
3363 uint8 create_view_suid;
3364
3365 /**
3366 Intended to point to the next word after DEFINER-clause in the
3367 following statements:
3368
3369 - CREATE TRIGGER (points to "TRIGGER");
3370 - CREATE PROCEDURE (points to "PROCEDURE");
3371 - CREATE FUNCTION (points to "FUNCTION" or "AGGREGATE");
3372 - CREATE EVENT (points to "EVENT")
3373
3374 This pointer is required to add possibly omitted DEFINER-clause to the
3375 DDL-statement before dumping it to the binlog.
3376 */
3377 const char *stmt_definition_begin;
3378 const char *stmt_definition_end;
3379
3380 /**
3381 During name resolution search only in the table list given by
3382 Name_resolution_context::first_name_resolution_table and
3383 Name_resolution_context::last_name_resolution_table
3384 (see Item_field::fix_fields()).
3385 */
3386 bool use_only_table_context;
3387
3388 /*
3389 Reference to a struct that contains information in various commands
3390 to add/create/drop/change table spaces.
3391 */
3392 st_alter_tablespace *alter_tablespace_info;
3393
3394 bool is_lex_started; /* If lex_start() did run. For debugging. */
3395 /// Set to true while resolving values in ON DUPLICATE KEY UPDATE clause
3396 bool in_update_value_clause;
3397
3398 /*
3399 The set of those tables whose fields are referenced in all subqueries
3400 of the query.
3401 TODO: possibly this it is incorrect to have used tables in LEX because
3402 with subquery, it is not clear what does the field mean. To fix this
3403 we should aggregate used tables information for selected expressions
3404 into the select_lex.
3405 */
3406 table_map used_tables;
3407
3408 class Explain_format *explain_format;
3409
3410 // Maximum execution time for a statement.
3411 ulong max_execution_time;
3412 /*
3413 To flag the current statement as dependent for binary logging
3414 on explicit_defaults_for_timestamp
3415 */
3416 bool binlog_need_explicit_defaults_ts;
3417 LEX();
3418
3419 virtual ~LEX();
3420
3421 /// Reset query context to initial state
3422 void reset();
3423
3424 /// Create an empty query block within this LEX object.
3425 st_select_lex *new_empty_query_block();
3426
3427 /// Create query expression object that contains one query block.
3428 st_select_lex *new_query(st_select_lex *curr_select);
3429
3430 /// Create query block and attach it to the current query expression.
3431 st_select_lex *new_union_query(st_select_lex *curr_select, bool distinct);
3432
3433 /// Create top-level query expression and query block.
3434 bool new_top_level_query();
3435
3436 /// Create query expression and query block in existing memory objects.
3437 void new_static_query(SELECT_LEX_UNIT *sel_unit, SELECT_LEX *select);
3438
is_ps_or_view_context_analysisLEX3439 inline bool is_ps_or_view_context_analysis()
3440 {
3441 return (context_analysis_only &
3442 (CONTEXT_ANALYSIS_ONLY_PREPARE |
3443 CONTEXT_ANALYSIS_ONLY_VIEW));
3444 }
3445
3446 /**
3447 Set the current query as uncacheable.
3448
3449 @param cause why this query is uncacheable.
3450
3451 @details
3452 All query blocks representing subqueries, from the current one up to
3453 the outer-most one, but excluding the main query block, are also set
3454 as uncacheable.
3455 */
set_uncacheableLEX3456 void set_uncacheable(SELECT_LEX *curr_select, uint8 cause)
3457 {
3458 safe_to_cache_query= false;
3459
3460 if (m_current_select == NULL)
3461 return;
3462 SELECT_LEX *sl;
3463 SELECT_LEX_UNIT *un;
3464 for (sl= curr_select, un= sl->master_unit();
3465 un != unit;
3466 sl= sl->outer_select(), un= sl->master_unit())
3467 {
3468 sl->uncacheable|= cause;
3469 un->uncacheable|= cause;
3470 }
3471 }
3472 void set_trg_event_type_for_tables();
3473
3474 TABLE_LIST *unlink_first_table(bool *link_to_local);
3475 void link_first_table_back(TABLE_LIST *first, bool link_to_local);
3476 void first_lists_tables_same();
3477
3478 bool can_use_merged();
3479 bool can_not_use_merged();
3480 bool only_view_structure();
3481 bool need_correct_ident();
3482 /*
3483 Is this update command where 'WHITH CHECK OPTION' clause is important
3484
3485 SYNOPSIS
3486 LEX::which_check_option_applicable()
3487
3488 RETURN
3489 TRUE have to take 'WHITH CHECK OPTION' clause into account
3490 FALSE 'WHITH CHECK OPTION' clause do not need
3491 */
which_check_option_applicableLEX3492 inline bool which_check_option_applicable()
3493 {
3494 switch (sql_command) {
3495 case SQLCOM_UPDATE:
3496 case SQLCOM_UPDATE_MULTI:
3497 case SQLCOM_INSERT:
3498 case SQLCOM_INSERT_SELECT:
3499 case SQLCOM_REPLACE:
3500 case SQLCOM_REPLACE_SELECT:
3501 case SQLCOM_LOAD:
3502 return TRUE;
3503 default:
3504 return FALSE;
3505 }
3506 }
3507
3508 void cleanup_after_one_table_open();
3509
push_contextLEX3510 bool push_context(Name_resolution_context *context)
3511 {
3512 return context_stack.push_front(context);
3513 }
3514
pop_contextLEX3515 void pop_context()
3516 {
3517 context_stack.pop();
3518 }
3519
3520 bool copy_db_to(char **p_db, size_t *p_db_length) const;
3521
current_contextLEX3522 Name_resolution_context *current_context()
3523 {
3524 return context_stack.head();
3525 }
3526 /*
3527 Restore the LEX and THD in case of a parse error.
3528 */
3529 static void cleanup_lex_after_parse_error(THD *thd);
3530
3531 void reset_n_backup_query_tables_list(Query_tables_list *backup);
3532 void restore_backup_query_tables_list(Query_tables_list *backup);
3533
3534 bool table_or_sp_used();
3535 bool is_partition_management() const;
3536
3537 /**
3538 @brief check if the statement is a single-level join
3539 @return result of the check
3540 @retval TRUE The statement doesn't contain subqueries, unions and
3541 stored procedure calls.
3542 @retval FALSE There are subqueries, UNIONs or stored procedure calls.
3543 */
is_single_level_stmtLEX3544 bool is_single_level_stmt()
3545 {
3546 /*
3547 This check exploits the fact that the last added to all_select_list is
3548 on its top. So select_lex (as the first added) will be at the tail
3549 of the list.
3550 */
3551 if (select_lex == all_selects_list && !sroutines.records)
3552 {
3553 assert(!all_selects_list->next_select_in_list());
3554 return TRUE;
3555 }
3556 return FALSE;
3557 }
3558
3559 void release_plugins();
3560
3561 bool accept(Select_lex_visitor *visitor);
3562
3563 Item* donor_transaction_id;
3564 };
3565
3566
3567 /**
3568 The internal state of the syntax parser.
3569 This object is only available during parsing,
3570 and is private to the syntax parser implementation (sql_yacc.yy).
3571 */
3572 class Yacc_state
3573 {
3574 public:
Yacc_state()3575 Yacc_state() : yacc_yyss(NULL), yacc_yyvs(NULL), yacc_yyls(NULL) { reset(); }
3576
reset()3577 void reset()
3578 {
3579 if (yacc_yyss != NULL) {
3580 my_free(yacc_yyss);
3581 yacc_yyss = NULL;
3582 }
3583 if (yacc_yyvs != NULL) {
3584 my_free(yacc_yyvs);
3585 yacc_yyvs = NULL;
3586 }
3587 if (yacc_yyls != NULL) {
3588 my_free(yacc_yyls);
3589 yacc_yyls = NULL;
3590 }
3591 m_lock_type= TL_READ_DEFAULT;
3592 m_mdl_type= MDL_SHARED_READ;
3593 m_ha_rkey_mode= HA_READ_KEY_EXACT;
3594 }
3595
3596 ~Yacc_state();
3597
3598 /**
3599 Reset part of the state which needs resetting before parsing
3600 substatement.
3601 */
reset_before_substatement()3602 void reset_before_substatement()
3603 {
3604 m_lock_type= TL_READ_DEFAULT;
3605 m_mdl_type= MDL_SHARED_READ;
3606 m_ha_rkey_mode= HA_READ_KEY_EXACT; /* Let us be future-proof. */
3607 }
3608
3609 /**
3610 Bison internal state stack, yyss, when dynamically allocated using
3611 my_yyoverflow().
3612 */
3613 uchar *yacc_yyss;
3614
3615 /**
3616 Bison internal semantic value stack, yyvs, when dynamically allocated using
3617 my_yyoverflow().
3618 */
3619 uchar *yacc_yyvs;
3620
3621 /**
3622 Bison internal location value stack, yyls, when dynamically allocated using
3623 my_yyoverflow().
3624 */
3625 uchar *yacc_yyls;
3626
3627 /**
3628 Type of lock to be used for tables being added to the statement's
3629 table list in table_factor, table_alias_ref, single_multi and
3630 table_wild_one rules.
3631 Statements which use these rules but require lock type different
3632 from one specified by this member have to override it by using
3633 st_select_lex::set_lock_for_tables() method.
3634
3635 The default value of this member is TL_READ_DEFAULT. The only two
3636 cases in which we change it are:
3637 - When parsing SELECT HIGH_PRIORITY.
3638 - Rule for DELETE. In which we use this member to pass information
3639 about type of lock from delete to single_multi part of rule.
3640
3641 We should try to avoid introducing new use cases as we would like
3642 to get rid of this member eventually.
3643 */
3644 thr_lock_type m_lock_type;
3645
3646 /**
3647 The type of requested metadata lock for tables added to
3648 the statement table list.
3649 */
3650 enum_mdl_type m_mdl_type;
3651
3652 /** Type of condition for key in HANDLER READ statement. */
3653 enum ha_rkey_function m_ha_rkey_mode;
3654
3655 /*
3656 TODO: move more attributes from the LEX structure here.
3657 */
3658 };
3659
3660 /**
3661 Input parameters to the parser.
3662 */
3663 struct Parser_input
3664 {
3665 /**
3666 True if the text parsed corresponds to an actual query,
3667 and not another text artifact.
3668 This flag is used to disable digest parsing of nested:
3669 - view definitions
3670 - table trigger definitions
3671 - table partition definitions
3672 - event scheduler event definitions
3673 */
3674 bool m_has_digest;
3675 /**
3676 True if the caller needs to compute a digest.
3677 This flag is used to request explicitly a digest computation,
3678 independently of the performance schema configuration.
3679 */
3680 bool m_compute_digest;
3681
Parser_inputParser_input3682 Parser_input()
3683 : m_has_digest(false), m_compute_digest(false)
3684 {}
3685 };
3686
3687 /**
3688 Internal state of the parser.
3689 The complete state consist of:
3690 - input parameters that control the parser behavior
3691 - state data used during lexical parsing,
3692 - state data used during syntactic parsing.
3693 */
3694 class Parser_state
3695 {
3696 public:
Parser_state()3697 Parser_state() :
3698 m_input(), m_lip(), m_yacc(), m_comment(false)
3699 {}
3700
3701 /**
3702 Object initializer. Must be called before usage.
3703
3704 @retval FALSE OK
3705 @retval TRUE Error
3706 */
init(THD * thd,const char * buff,size_t length)3707 bool init(THD *thd, const char *buff, size_t length)
3708 {
3709 return m_lip.init(thd, buff, length);
3710 }
3711
reset(const char * found_semicolon,size_t length)3712 void reset(const char *found_semicolon, size_t length)
3713 {
3714 m_lip.reset(found_semicolon, length);
3715 m_yacc.reset();
3716 }
3717
3718 /// Signal that the current query has a comment
add_comment()3719 void add_comment()
3720 {
3721 m_comment= true;
3722 }
3723 /// Check whether the current query has a comment
has_comment()3724 bool has_comment() const
3725 {
3726 return m_comment;
3727 }
3728
3729 public:
3730 Parser_input m_input;
3731 Lex_input_stream m_lip;
3732 Yacc_state m_yacc;
3733 /**
3734 Current performance digest instrumentation.
3735 */
3736 PSI_digest_locker* m_digest_psi;
3737
3738 private:
3739 bool m_comment; ///< True if current query contains comments
3740 };
3741
3742 extern sql_digest_state *
3743 digest_add_token(sql_digest_state *state, uint token, LEX_YYSTYPE yylval);
3744
3745 extern sql_digest_state *
3746 digest_reduce_token(sql_digest_state *state, uint token_left, uint token_right);
3747
3748 struct st_lex_local: public LEX
3749 {
newst_lex_local3750 static void *operator new(size_t size) throw()
3751 {
3752 return sql_alloc(size);
3753 }
newst_lex_local3754 static void *operator new(size_t size, MEM_ROOT *mem_root) throw()
3755 {
3756 return alloc_root(mem_root, size);
3757 }
deletest_lex_local3758 static void operator delete(void *ptr,size_t size)
3759 { TRASH(ptr, size); }
deletest_lex_local3760 static void operator delete(void *ptr, MEM_ROOT *mem_root)
3761 { /* Never called */ }
3762 };
3763
3764
3765 extern bool lex_init(void);
3766 extern void lex_free(void);
3767 extern bool lex_start(THD *thd);
3768 extern void lex_end(LEX *lex);
3769 extern int MYSQLlex(union YYSTYPE *yylval, struct YYLTYPE *yylloc,
3770 class THD *thd);
3771
3772 extern void trim_whitespace(const CHARSET_INFO *cs, LEX_STRING *str);
3773
3774 extern bool is_lex_native_function(const LEX_STRING *name);
3775
3776 /**
3777 @} (End of group Semantic_Analysis)
3778 */
3779
3780 void my_missing_function_error(const LEX_STRING &token, const char *name);
3781 bool is_keyword(const char *name, size_t len);
3782 bool db_is_default_db(const char *db, size_t db_len, const THD *thd);
3783
3784 #endif /* MYSQL_SERVER */
3785 #endif /* SQL_LEX_INCLUDED */
3786